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
49bf78b8c14eda5be4f8878d39a6f03c743bfc8b
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/tactic_at_types.lean
fb59b72acd6338b16e74e18aabd598e4c978c0f4
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
929
lean
import tactic set_option simplifier::heq true set_option pp::implicit true -- to be able to parse output produced by Lean variable vec : Nat → Type variables n m : Nat variable v1 : vec (n + m) variable v2 : vec (m + n) rewrite_set S add_rewrite Nat::add_comm Nat::add_assoc Nat::add_left_comm eq_id : S axiom Ax1 : v1 = cast (by simp S) v2 variable Ax2 : v2 = cast (by simp S) v1 variable concat {n m : Nat} (v : vec n) (w : vec m) : vec (n + m) infixl 65 ; : concat variable empty : vec 0 axiom concat_assoc {n1 n2 n3 : Nat} (v1 : vec n1) (v2 : vec n2) (v3 : vec n3) : (v1 ; v2) ; v3 = cast (by simp S) (v1 ; (v2 ; v3)) add_rewrite Nat::add_zeror Nat::add_zerol : S axiom concat_empty {n : Nat} (v : vec n) : v ; empty = cast (by simp S) v theorem T1 (n m : Nat) (v : vec (n + m)) (w : vec (m + n)) (H : v = cast (by simp S) w) : w = cast (by simp S) v := (by simp S)
a4ac2acc2de1287fcd76736fc7000d2318651957
64874bd1010548c7f5a6e3e8902efa63baaff785
/library/data/unit.lean
1ff796278ecd86e7f786448652e1363e454cd79f
[ "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
728
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: data.unit Author: Leonardo de Moura -/ import logic.eq logic.subsingleton namespace unit notation `⋆` := star protected theorem equal (a b : unit) : a = b := rec_on a (rec_on b rfl) theorem eq_star (a : unit) : a = star := equal a star protected theorem subsingleton [instance] : subsingleton unit := subsingleton.intro (λ a b, equal a b) protected definition is_inhabited [instance] : inhabited unit := inhabited.mk unit.star protected definition has_decidable_eq [instance] : decidable_eq unit := take (a b : unit), decidable.inl (equal a b) end unit
b43e84abfca766779a4301f27faeacc05998dc87
022547453607c6244552158ff25ab3bf17361760
/src/group_theory/sylow.lean
aaba9dfaec210b4c92d4de90f369065c58c650b5
[ "Apache-2.0" ]
permissive
1293045656/mathlib
5f81741a7c1ff1873440ec680b3680bfb6b7b048
4709e61525a60189733e72a50e564c58d534bed8
refs/heads/master
1,687,010,200,553
1,626,245,646,000
1,626,245,646,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,275
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 /-! # Sylow theorems The Sylow theorems are the following results for every finite group `G` and every prime number `p`. * There exists a Sylow `p`-subgroup of `G`. * All Sylow `p`-subgroups of `G` are conjugate to each other. * Let `nₚ` be the number of Sylow `p`-subgroups of `G`, then `nₚ` divides the index of the Sylow `p`-subgroup, `nₚ ≡ 1 [MOD p]`, and `nₚ` is equal to the index of the normalizer of the Sylow `p`-subgroup in `G`. In this file, currently only the first of these results is proven. ## Main statements * `exists_prime_order_of_dvd_card`: For every prime `p` dividing the order of `G` there exists an element of order `p` in `G`. This is known as Cauchy`s theorem. * `exists_subgroup_card_pow_prime`: A generalisation of the first of the Sylow theorems: For every prime power `pⁿ` dividing `G`, there exists a subgroup of `G` of order `pⁿ`. ## TODO * Prove the second and third of the Sylow theorems. * Sylow theorems for infinite groups -/ open equiv fintype finset mul_action function open equiv.perm subgroup list quotient_group open_locale big_operators 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 ... = ∑ a : quotient (orbit_rel G α), card {x // quotient.mk' x = a} : card_sigma _ ... ≡ ∑ a : fixed_points G α, 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).1 this with ⟨k, _, hk⟩, have hb' :¬ p ^ 1 ∣ p ^ k, { rw [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 (pow_dvd_pow p) hb'))), refine ⟨⟨b, mem_fixed_points_iff_card_orbit_eq_one.2 $ by rw [hk, this, 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 : subgroup G) (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_injective (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 _ _ $ 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.1.one_lt⟩ in have hn : p = n + 1 := nat.succ_sub hp.1.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_injective _), card_vector], have hzmod : fintype.card (multiplicative (zmod p)) = p ^ 1, by { rw 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 pow_one ... ∣ card G ^ (n : ℕ) : 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.1.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 ∃ 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_nat_cast, ← 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.1.2 _ (order_of_dvd_of_pow_eq_one this)).resolve_left (λ h, ha1 (order_of_eq_one_iff.1 h))⟩ open subgroup submonoid is_group_hom mul_action lemma mem_fixed_points_mul_left_cosets_iff_mem_normalizer {H : subgroup G} [fintype ((H : set G) : Type u)] {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 _ _ _ _inst_2 _ (λ n (hn : n ∈ H), have (n⁻¹ * x)⁻¹ * x ∈ H := quotient_group.eq.1 (ha (mem_orbit _ ⟨n⁻¹, H.inv_mem hn⟩)), show _ ∈ H, by {rw [mul_inv_rev, inv_inv] at this, convert this, rw inv_inv} )), λ (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_left H (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 : subgroup G) [fintype (H : set G)] : mul_action.fixed_points H (quotient H) ≃ quotient (subgroup.comap ((normalizer H).subtype : normalizer H →* G) H) := @subtype_quotient_equiv_quotient_subtype G (normalizer H : set G) (id _) (id _) (fixed_points _ _) (λ a, (@mem_fixed_points_mul_left_cosets_iff_mem_normalizer _ _ _ _inst_2 _).symm) (by intros; refl) /-- The first of the **Sylow theorems** -/ theorem exists_subgroup_card_pow_prime [fintype G] (p : ℕ) : ∀ {n : ℕ} [hp : fact p.prime] (hdvd : p ^ n ∣ card G), ∃ H : subgroup G, fintype.card H = p ^ n | 0 := λ _ _, ⟨(⊥ : subgroup G), by convert card_bot⟩ | (n+1) := λ hp hdvd, let ⟨H, hH2⟩ := @exists_subgroup_card_pow_prime _ hp (dvd.trans (pow_dvd_pow _ (nat.le_succ _)) hdvd) in let ⟨s, hs⟩ := exists_eq_mul_left_of_dvd hdvd in have hcard : card (quotient H) = s * p := (nat.mul_left_inj (show card H > 0, from fintype.card_pos_iff.2 ⟨⟨1, H.one_mem⟩⟩)).1 (by rwa [← card_eq_card_quotient_mul_card_subgroup H, hH2, hs, pow_succ', mul_assoc, mul_comm p]), have hm : s * p % p = card (quotient (subgroup.comap ((normalizer H).subtype : normalizer H →* G) H)) % p := card_congr (fixed_points_mul_left_cosets_equiv_quotient H) ▸ hcard ▸ @card_modeq_card_fixed_points _ _ _ _ _ _ _ p _ hp hH2, have hm' : p ∣ card (quotient (subgroup.comap ((normalizer H).subtype : normalizer H →* G) 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.quotient.group _) _ _ hp hm' in have hequiv : H ≃ (subgroup.comap ((normalizer H).subtype : normalizer H →* G) H) := ⟨λ a, ⟨⟨a.1, le_normalizer a.2⟩, a.2⟩, λ a, ⟨a.1.1, a.2⟩, λ ⟨_, _⟩, rfl, λ ⟨⟨_, _⟩, _⟩, rfl⟩, -- begin proof of ∃ H : subgroup G, fintype.card H = p ^ n ⟨subgroup.map ((normalizer H).subtype) (subgroup.comap (quotient_group.mk' (comap H.normalizer.subtype H)) (gpowers x)), begin show card ↥(map H.normalizer.subtype (comap (mk' (comap H.normalizer.subtype H)) (subgroup.gpowers x))) = p ^ (n + 1), suffices : card ↥(subtype.val '' ((subgroup.comap (mk' (comap H.normalizer.subtype H)) (gpowers x)) : set (↥(H.normalizer)))) = p^(n+1), { convert this using 2 }, rw [set.card_image_of_injective (subgroup.comap (mk' (comap H.normalizer.subtype H)) (gpowers x) : set (H.normalizer)) subtype.val_injective, 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⟩ end sylow
7bf96a08d506b62c223e0959e4a2e24d38fa8e05
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/indicator_function.lean
61699e39795f039dcba5f179a3ad0d71a0833c29
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
14,758
lean
/- Copyright (c) 2020 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import algebra.group.pi import group_theory.group_action import data.support import data.finset.lattice /-! # Indicator function `indicator (s : set α) (f : α → β) (a : α)` is `f a` if `a ∈ s` and is `0` otherwise. ## Implementation note In mathematics, an indicator function or a characteristic function is a function used to indicate membership of an element in a set `s`, having the value `1` for all elements of `s` and the value `0` otherwise. But since it is usually used to restrict a function to a certain set `s`, we let the indicator function take the value `f x` for some function `f`, instead of `1`. If the usual indicator function is needed, just set `f` to be the constant function `λx, 1`. ## Tags indicator, characteristic -/ noncomputable theory open_locale classical big_operators open function variables {α α' β γ : Type*} namespace set section has_zero variables [has_zero β] {s t : set α} {f g : α → β} {a : α} /-- `indicator s f a` is `f a` if `a ∈ s`, `0` otherwise. -/ @[reducible] def indicator (s : set α) (f : α → β) : α → β := λ x, if x ∈ s then f x else 0 @[simp] lemma piecewise_eq_indicator {s : set α} : s.piecewise f 0 = s.indicator f := rfl lemma indicator_apply (s : set α) (f : α → β) (a : α) : indicator s f a = if a ∈ s then f a else 0 := rfl @[simp] lemma indicator_of_mem (h : a ∈ s) (f : α → β) : indicator s f a = f a := if_pos h @[simp] lemma indicator_of_not_mem (h : a ∉ s) (f : α → β) : indicator s f a = 0 := if_neg h lemma indicator_eq_zero_or_self (s : set α) (f : α → β) (a : α) : indicator s f a = 0 ∨ indicator s f a = f a := if h : a ∈ s then or.inr (indicator_of_mem h f) else or.inl (indicator_of_not_mem h f) /-- If an indicator function is nonzero at a point, that point is in the set. -/ lemma mem_of_indicator_ne_zero (h : indicator s f a ≠ 0) : a ∈ s := not_imp_comm.1 (λ hn, indicator_of_not_mem hn f) h lemma eq_on_indicator : eq_on (indicator s f) f s := λ x hx, indicator_of_mem hx f lemma support_indicator : function.support (s.indicator f) ⊆ s := λ x hx, hx.imp_symm (λ h, indicator_of_not_mem h f) lemma indicator_of_support_subset (h : support f ⊆ s) : s.indicator f = f := begin ext x, by_cases hx : f x = 0, { rw hx, by_contradiction H, have := mem_of_indicator_ne_zero H, rw [indicator_of_mem this f, hx] at H, exact H rfl }, { exact indicator_of_mem (h hx) f } end @[simp] lemma indicator_support : (support f).indicator f = f := indicator_of_support_subset $ subset.refl _ @[simp] lemma indicator_range_comp {ι : Sort*} (f : ι → α) (g : α → β) : indicator (range f) g ∘ f = g ∘ f := piecewise_range_comp _ _ _ lemma indicator_congr (h : ∀ a ∈ s, f a = g a) : indicator s f = indicator s g := funext $ λx, by { simp only [indicator], split_ifs, { exact h _ h_1 }, refl } @[simp] lemma indicator_univ (f : α → β) : indicator (univ : set α) f = f := funext $ λx, indicator_of_mem (mem_univ _) f @[simp] lemma indicator_empty (f : α → β) : indicator (∅ : set α) f = λa, 0 := funext $ λx, indicator_of_not_mem (not_mem_empty _) f variable (β) @[simp] lemma indicator_zero (s : set α) : indicator s (λx, (0:β)) = λx, (0:β) := funext $ λx, by { simp only [indicator], split_ifs, refl, refl } @[simp] lemma indicator_zero' {s : set α} : s.indicator (0 : α → β) = 0 := indicator_zero β s variable {β} lemma indicator_indicator (s t : set α) (f : α → β) : indicator s (indicator t f) = indicator (s ∩ t) f := funext $ λx, by { simp only [indicator], split_ifs, repeat {simp * at * {contextual := tt}} } lemma comp_indicator (h : β → γ) (f : α → β) {s : set α} {x : α} : h (s.indicator f x) = s.piecewise (h ∘ f) (const α (h 0)) x := s.comp_piecewise h lemma indicator_comp_right {s : set α} (f : γ → α) {g : α → β} {x : γ} : indicator (f ⁻¹' s) (g ∘ f) x = indicator s g (f x) := by { simp only [indicator], split_ifs; refl } lemma indicator_comp_of_zero [has_zero γ] {g : β → γ} (hg : g 0 = 0) : indicator s (g ∘ f) = g ∘ (indicator s f) := begin funext, simp only [indicator], split_ifs; simp [*] end lemma indicator_preimage (s : set α) (f : α → β) (B : set β) : (indicator s f)⁻¹' B = s ∩ f ⁻¹' B ∪ sᶜ ∩ (λa:α, (0:β)) ⁻¹' B := piecewise_preimage s f 0 B lemma indicator_preimage_of_not_mem (s : set α) (f : α → β) {t : set β} (ht : (0:β) ∉ t) : (indicator s f)⁻¹' t = s ∩ f ⁻¹' t := by simp [indicator_preimage, set.preimage_const_of_not_mem ht] lemma mem_range_indicator {r : β} {s : set α} {f : α → β} : r ∈ range (indicator s f) ↔ (r = 0 ∧ s ≠ univ) ∨ (r ∈ f '' s) := by simp [indicator, ite_eq_iff, exists_or_distrib, eq_univ_iff_forall, and_comm, or_comm, @eq_comm _ r 0] lemma indicator_rel_indicator {r : β → β → Prop} (h0 : r 0 0) (ha : a ∈ s → r (f a) (g a)) : r (indicator s f a) (indicator s g a) := by { simp only [indicator], split_ifs with has has, exacts [ha has, h0] } /-- Consider a sum of `g i (f i)` over a `finset`. Suppose `g` is a function such as multiplication, which maps a second argument of 0 to 0. (A typical use case would be a weighted sum of `f i * h i` or `f i • h i`, where `f` gives the weights that are multiplied by some other function `h`.) Then if `f` is replaced by the corresponding indicator function, the `finset` may be replaced by a possibly larger `finset` without changing the value of the sum. -/ lemma sum_indicator_subset_of_eq_zero {γ : Type*} [add_comm_monoid γ] (f : α → β) (g : α → β → γ) {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) (hg : ∀ a, g a 0 = 0) : ∑ i in s₁, g i (f i) = ∑ i in s₂, g i (indicator ↑s₁ f i) := begin rw ←finset.sum_subset h _, { apply finset.sum_congr rfl, intros i hi, congr, symmetry, exact indicator_of_mem hi _ }, { refine λ i hi hn, _, convert hg i, exact indicator_of_not_mem hn _ } end /-- Summing an indicator function over a possibly larger `finset` is the same as summing the original function over the original `finset`. -/ lemma sum_indicator_subset {γ : Type*} [add_comm_monoid γ] (f : α → γ) {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : ∑ i in s₁, f i = ∑ i in s₂, indicator ↑s₁ f i := sum_indicator_subset_of_eq_zero _ (λ a b, b) h (λ _, rfl) end has_zero section add_monoid variables [add_monoid β] {s t : set α} {f g : α → β} {a : α} lemma indicator_union_of_not_mem_inter (h : a ∉ s ∩ t) (f : α → β) : indicator (s ∪ t) f a = indicator s f a + indicator t f a := by { simp only [indicator], split_ifs, repeat {simp * at * {contextual := tt}} } lemma indicator_union_of_disjoint (h : disjoint s t) (f : α → β) : indicator (s ∪ t) f = λa, indicator s f a + indicator t f a := funext $ λa, indicator_union_of_not_mem_inter (by { convert not_mem_empty a, have := disjoint.eq_bot h, assumption }) _ lemma indicator_add (s : set α) (f g : α → β) : indicator s (λa, f a + g a) = λa, indicator s f a + indicator s g a := by { funext, simp only [indicator], split_ifs, { refl }, rw add_zero } @[simp] lemma indicator_compl_add_self_apply (s : set α) (f : α → β) (a : α) : indicator sᶜ f a + indicator s f a = f a := classical.by_cases (λ ha : a ∈ s, by simp [ha]) (λ ha, by simp [ha]) @[simp] lemma indicator_compl_add_self (s : set α) (f : α → β) : indicator sᶜ f + indicator s f = f := funext $ indicator_compl_add_self_apply s f @[simp] lemma indicator_self_add_compl_apply (s : set α) (f : α → β) (a : α) : indicator s f a + indicator sᶜ f a = f a := classical.by_cases (λ ha : a ∈ s, by simp [ha]) (λ ha, by simp [ha]) @[simp] lemma indicator_self_add_compl (s : set α) (f : α → β) : indicator s f + indicator sᶜ f = f := funext $ indicator_self_add_compl_apply s f variables (β) instance is_add_monoid_hom.indicator (s : set α) : is_add_monoid_hom (λf:α → β, indicator s f) := { map_add := λ _ _, indicator_add _ _ _, map_zero := indicator_zero _ _ } variables {β} {𝕜 : Type*} [monoid 𝕜] [distrib_mul_action 𝕜 β] lemma indicator_smul (s : set α) (r : 𝕜) (f : α → β) : indicator s (λ (x : α), r • f x) = λ (x : α), r • indicator s f x := by { simp only [indicator], funext, split_ifs, refl, exact (smul_zero r).symm } lemma indicator_add_eq_left {f g : α → β} (h : univ ⊆ f ⁻¹' {0} ∪ g ⁻¹' {0}) : (f ⁻¹' {0})ᶜ.indicator (f + g) = f := begin ext x, by_cases hx : x ∈ (f ⁻¹' {0})ᶜ, { have : g x = 0, { simp at hx, specialize h (mem_univ x), simpa [hx] using h }, simp [hx, this] }, { simp * at * } end lemma indicator_add_eq_right {f g : α → β} (h : univ ⊆ f ⁻¹' {0} ∪ g ⁻¹' {0}) : (g ⁻¹' {0})ᶜ.indicator (f + g) = g := begin ext x, by_cases hx : x ∈ (g ⁻¹' {0})ᶜ, { have : f x = 0, { simp at hx, specialize h (mem_univ x), simpa [hx] using h }, simp [hx, this] }, { simp * at * } end end add_monoid section add_group variables [add_group β] {s t : set α} {f g : α → β} {a : α} variables (β) instance is_add_group_hom.indicator (s : set α) : is_add_group_hom (λf:α → β, indicator s f) := { .. is_add_monoid_hom.indicator β s } variables {β} lemma indicator_neg (s : set α) (f : α → β) : indicator s (λa, - f a) = λa, - indicator s f a := show indicator s (- f) = - indicator s f, from is_add_group_hom.map_neg _ _ lemma indicator_sub (s : set α) (f g : α → β) : indicator s (λa, f a - g a) = λa, indicator s f a - indicator s g a := show indicator s (f - g) = indicator s f - indicator s g, from is_add_group_hom.map_sub _ _ _ lemma indicator_compl (s : set α) (f : α → β) : indicator sᶜ f = f - indicator s f := eq_sub_of_add_eq $ s.indicator_compl_add_self f lemma indicator_finset_sum {β} [add_comm_monoid β] {ι : Type*} (I : finset ι) (s : set α) (f : ι → α → β) : indicator s (∑ i in I, f i) = ∑ i in I, indicator s (f i) := begin convert (finset.sum_hom _ _).symm, split, exact indicator_zero _ _ end lemma indicator_finset_bUnion {β} [add_comm_monoid β] {ι} (I : finset ι) (s : ι → set α) {f : α → β} : (∀ (i ∈ I) (j ∈ I), i ≠ j → s i ∩ s j = ∅) → indicator (⋃ i ∈ I, s i) f = λ a, ∑ i in I, indicator (s i) f a := begin refine finset.induction_on I _ _, assume h, { funext, simp }, assume a I haI ih hI, funext, simp only [haI, finset.sum_insert, not_false_iff], rw [finset.bUnion_insert, indicator_union_of_not_mem_inter, ih _], { assume i hi j hj hij, exact hI i (finset.mem_insert_of_mem hi) j (finset.mem_insert_of_mem hj) hij }, simp only [not_exists, exists_prop, mem_Union, mem_inter_eq, not_and], assume hx a' ha', have := hI a (finset.mem_insert_self _ _) a' (finset.mem_insert_of_mem ha') _, { assume h, have h := mem_inter hx h, rw this at h, exact not_mem_empty _ h }, { assume h, rw h at haI, contradiction } end end add_group section mul_zero_class variables [mul_zero_class β] {s t : set α} {f g : α → β} {a : α} lemma indicator_mul (s : set α) (f g : α → β) : indicator s (λa, f a * g a) = λa, indicator s f a * indicator s g a := by { funext, simp only [indicator], split_ifs, { refl }, rw mul_zero } lemma indicator_mul_left (s : set α) (f g : α → β) : indicator s (λa, f a * g a) a = indicator s f a * g a := by { simp only [indicator], split_ifs, { refl }, rw [zero_mul] } lemma indicator_mul_right (s : set α) (f g : α → β) : indicator s (λa, f a * g a) a = f a * indicator s g a := by { simp only [indicator], split_ifs, { refl }, rw [mul_zero] } end mul_zero_class section monoid_with_zero variables [monoid_with_zero β] lemma indicator_prod_one {s : set α} {t : set α'} {x : α} {y : α'} : (s.prod t).indicator (1 : _ → β) (x, y) = s.indicator 1 x * t.indicator 1 y := by simp [indicator, ← ite_and] end monoid_with_zero section order variables [has_zero β] [preorder β] {s t : set α} {f g : α → β} {a : α} lemma indicator_nonneg' (h : a ∈ s → 0 ≤ f a) : 0 ≤ indicator s f a := by { rw indicator_apply, split_ifs with as, { exact h as }, refl } lemma indicator_nonneg (h : ∀ a ∈ s, 0 ≤ f a) : ∀ a, 0 ≤ indicator s f a := λ a, indicator_nonneg' (h a) lemma indicator_nonpos' (h : a ∈ s → f a ≤ 0) : indicator s f a ≤ 0 := by { rw indicator_apply, split_ifs with as, { exact h as }, refl } lemma indicator_nonpos (h : ∀ a ∈ s, f a ≤ 0) : ∀ a, indicator s f a ≤ 0 := λ a, indicator_nonpos' (h a) lemma indicator_le' (hfg : ∀ a ∈ s, f a ≤ g a) (hg : ∀ a ∉ s, 0 ≤ g a) : indicator s f ≤ g := λ a, if ha : a ∈ s then by simpa [ha] using hfg a ha else by simpa [ha] using hg a ha @[mono] lemma indicator_le_indicator (h : f a ≤ g a) : indicator s f a ≤ indicator s g a := indicator_rel_indicator (le_refl _) (λ _, h) lemma indicator_le_indicator_of_subset (h : s ⊆ t) (hf : ∀a, 0 ≤ f a) (a : α) : indicator s f a ≤ indicator t f a := begin simp only [indicator], split_ifs with h₁, { refl }, { have := h h₁, contradiction }, { exact hf a }, { refl } end lemma indicator_le_self' (hf : ∀ x ∉ s, 0 ≤ f x) : indicator s f ≤ f := indicator_le' (λ _ _, le_refl _) hf lemma indicator_le_self {β} [canonically_ordered_add_monoid β] (s : set α) (f : α → β) : indicator s f ≤ f := indicator_le_self' $ λ _ _, zero_le _ lemma indicator_le {β} [canonically_ordered_add_monoid β] {s : set α} {f g : α → β} (hfg : ∀ a ∈ s, f a ≤ g a) : indicator s f ≤ g := indicator_le' hfg $ λ _ _, zero_le _ lemma indicator_Union_apply {ι β} [complete_lattice β] [has_zero β] (h0 : (⊥:β) = 0) (s : ι → set α) (f : α → β) (x : α) : indicator (⋃ i, s i) f x = ⨆ i, indicator (s i) f x := begin by_cases hx : x ∈ ⋃ i, s i, { rw [indicator_of_mem hx], rw [mem_Union] at hx, refine le_antisymm _ (supr_le $ λ i, indicator_le_self' (λ x hx, h0 ▸ bot_le) x), rcases hx with ⟨i, hi⟩, exact le_supr_of_le i (ge_of_eq $ indicator_of_mem hi _) }, { rw [indicator_of_not_mem hx], simp only [mem_Union, not_exists] at hx, simp [hx, ← h0] } end end order end set lemma add_monoid_hom.map_indicator {M N : Type*} [add_monoid M] [add_monoid N] (f : M →+ N) (s : set α) (g : α → M) (x : α) : f (s.indicator g x) = s.indicator (f ∘ g) x := congr_fun (set.indicator_comp_of_zero f.map_zero).symm x
4d505b3c2727a6af81df0d37bb2aaa0f1fcdc4d0
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/category/TopCommRing.lean
edddbbd6b5fd42f973b6c2050292bc976464d4e1
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
3,581
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.Ring.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
71662233ace007df726b6c04416ad4f3216457c8
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1684.lean
7cf51fb17114f59b40971b05f80af846530d5ea3
[ "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
394
lean
set_option trace.Compiler.result true def Iff.elim1.{u} {a b : Prop} {motive : Sort u} (t : a ↔ b) (h : (mp : a → b) → (mpr : b → a) → motive) : motive := match t with | ⟨hab, hba⟩ => h hab hba def Iff.elim2.{u} {a b : Prop} {motive : Sort u} (t : a ↔ b) (h : (mp : a → b) → (mpr : b → a) → motive) : motive := Iff.casesOn (motive:= fun _ : a ↔ b => motive) t h
c5e3c91a366e3ec46b351df35c99d553fe90163a
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/zmod/basic.lean
f5ac2b0c934376cf4913754cc63edccd002e6f4f
[ "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
32,723
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import algebra.char_p.basic import ring_theory.ideal.operations import tactic.fin_cases /-! # Integers mod `n` Definition of the integers mod n, and the field structure on the integers mod p. ## Definitions * `zmod n`, which is for integers modulo a nat `n : ℕ` * `val a` is defined as a natural number: - for `a : zmod 0` it is the absolute value of `a` - for `a : zmod n` with `0 < n` it is the least natural number in the equivalence class * `val_min_abs` returns the integer closest to zero in the equivalence class. * A coercion `cast` is defined from `zmod n` into any ring. This is a ring hom if the ring has characteristic dividing `n` -/ namespace fin /-! ## Ring structure on `fin n` We define a commutative ring structure on `fin n`, but we do not register it as instance. Afterwords, when we define `zmod n` in terms of `fin n`, we use these definitions to register the ring structure on `zmod n` as type class instance. -/ open nat.modeq int /-- Multiplicative commutative semigroup structure on `fin (n+1)`. -/ instance (n : ℕ) : comm_semigroup (fin (n+1)) := { mul_assoc := λ ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, fin.eq_of_veq (calc ((a * b) % (n+1) * c) ≡ a * b * c [MOD (n+1)] : (nat.mod_modeq _ _).mul_right _ ... ≡ a * (b * c) [MOD (n+1)] : by rw mul_assoc ... ≡ a * (b * c % (n+1)) [MOD (n+1)] : (nat.mod_modeq _ _).symm.mul_left _), mul_comm := λ ⟨a, _⟩ ⟨b, _⟩, fin.eq_of_veq (show (a * b) % (n+1) = (b * a) % (n+1), by rw mul_comm), ..fin.has_mul } private lemma left_distrib_aux (n : ℕ) : ∀ a b c : fin (n+1), a * (b + c) = a * b + a * c := λ ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, fin.eq_of_veq (calc a * ((b + c) % (n+1)) ≡ a * (b + c) [MOD (n+1)] : (nat.mod_modeq _ _).mul_left _ ... ≡ a * b + a * c [MOD (n+1)] : by rw mul_add ... ≡ (a * b) % (n+1) + (a * c) % (n+1) [MOD (n+1)] : (nat.mod_modeq _ _).symm.add (nat.mod_modeq _ _).symm) /-- Commutative ring structure on `fin (n+1)`. -/ instance (n : ℕ) : comm_ring (fin (n+1)) := { one_mul := fin.one_mul, mul_one := fin.mul_one, left_distrib := left_distrib_aux n, right_distrib := λ a b c, by rw [mul_comm, left_distrib_aux, mul_comm _ b, mul_comm]; refl, ..fin.has_one, ..fin.add_comm_group n, ..fin.comm_semigroup n } end fin /-- The integers modulo `n : ℕ`. -/ def zmod : ℕ → Type | 0 := ℤ | (n+1) := fin (n+1) namespace zmod instance fintype : Π (n : ℕ) [fact (0 < n)], fintype (zmod n) | 0 h := false.elim $ nat.not_lt_zero 0 h.1 | (n+1) _ := fin.fintype (n+1) @[simp] lemma card (n : ℕ) [fact (0 < n)] : fintype.card (zmod n) = n := begin casesI n, { exfalso, exact nat.not_lt_zero 0 (fact.out _) }, { exact fintype.card_fin (n+1) } end instance decidable_eq : Π (n : ℕ), decidable_eq (zmod n) | 0 := int.decidable_eq | (n+1) := fin.decidable_eq _ instance has_repr : Π (n : ℕ), has_repr (zmod n) | 0 := int.has_repr | (n+1) := fin.has_repr _ instance comm_ring : Π (n : ℕ), comm_ring (zmod n) | 0 := int.comm_ring | (n+1) := fin.comm_ring n instance inhabited (n : ℕ) : inhabited (zmod n) := ⟨0⟩ /-- `val a` is a natural number defined as: - for `a : zmod 0` it is the absolute value of `a` - for `a : zmod n` with `0 < n` it is the least natural number in the equivalence class See `zmod.val_min_abs` for a variant that takes values in the integers. -/ def val : Π {n : ℕ}, zmod n → ℕ | 0 := int.nat_abs | (n+1) := (coe : fin (n + 1) → ℕ) lemma val_lt {n : ℕ} [fact (0 < n)] (a : zmod n) : a.val < n := begin casesI n, { exfalso, exact nat.not_lt_zero 0 (fact.out _) }, exact fin.is_lt a end lemma val_le {n : ℕ} [fact (0 < n)] (a : zmod n) : a.val ≤ n := a.val_lt.le @[simp] lemma val_zero : ∀ {n}, (0 : zmod n).val = 0 | 0 := rfl | (n+1) := rfl @[simp] lemma val_one' : (1 : zmod 0).val = 1 := rfl @[simp] lemma val_neg' {n : zmod 0} : (-n).val = n.val := by simp [val] @[simp] lemma val_mul' {m n : zmod 0} : (m * n).val = m.val * n.val := by simp [val, int.nat_abs_mul] lemma val_nat_cast {n : ℕ} (a : ℕ) : (a : zmod n).val = a % n := begin casesI n, { rw [nat.mod_zero, int.nat_cast_eq_coe_nat], exact int.nat_abs_of_nat a, }, rw ← fin.of_nat_eq_coe, refl end instance (n : ℕ) : char_p (zmod n) n := { cast_eq_zero_iff := begin intro k, cases n, { simp only [int.nat_cast_eq_coe_nat, zero_dvd_iff, int.coe_nat_eq_zero], }, rw [fin.eq_iff_veq], show (k : zmod (n+1)).val = (0 : zmod (n+1)).val ↔ _, rw [val_nat_cast, val_zero, nat.dvd_iff_mod_eq_zero], end } @[simp] lemma nat_cast_self (n : ℕ) : (n : zmod n) = 0 := char_p.cast_eq_zero (zmod n) n @[simp] lemma nat_cast_self' (n : ℕ) : (n + 1 : zmod (n + 1)) = 0 := by rw [← nat.cast_add_one, nat_cast_self (n + 1)] section universal_property variables {n : ℕ} {R : Type*} section variables [has_zero R] [has_one R] [has_add R] [has_neg R] /-- Cast an integer modulo `n` to another semiring. This function is a morphism if the characteristic of `R` divides `n`. See `zmod.cast_hom` for a bundled version. -/ def cast : Π {n : ℕ}, zmod n → R | 0 := int.cast | (n+1) := λ i, i.val -- see Note [coercion into rings] @[priority 900] instance (n : ℕ) : has_coe_t (zmod n) R := ⟨cast⟩ @[simp] lemma cast_zero : ((0 : zmod n) : R) = 0 := by { cases n; refl } variables {S : Type*} [has_zero S] [has_one S] [has_add S] [has_neg S] @[simp] lemma _root_.prod.fst_zmod_cast (a : zmod n) : (a : R × S).fst = a := by cases n; simp @[simp] lemma _root_.prod.snd_zmod_cast (a : zmod n) : (a : R × S).snd = a := by cases n; simp end /-- So-named because the coercion is `nat.cast` into `zmod`. For `nat.cast` into an arbitrary ring, see `zmod.nat_cast_val`. -/ lemma nat_cast_zmod_val {n : ℕ} [fact (0 < n)] (a : zmod n) : (a.val : zmod n) = a := begin casesI n, { exfalso, exact nat.not_lt_zero 0 (fact.out _) }, { apply fin.coe_coe_eq_self } end lemma nat_cast_right_inverse [fact (0 < n)] : function.right_inverse val (coe : ℕ → zmod n) := nat_cast_zmod_val lemma nat_cast_zmod_surjective [fact (0 < n)] : function.surjective (coe : ℕ → zmod n) := nat_cast_right_inverse.surjective /-- So-named because the outer coercion is `int.cast` into `zmod`. For `int.cast` into an arbitrary ring, see `zmod.int_cast_cast`. -/ lemma int_cast_zmod_cast (a : zmod n) : ((a : ℤ) : zmod n) = a := begin cases n, { rw [int.cast_id a, int.cast_id a], }, { rw [coe_coe, int.nat_cast_eq_coe_nat, int.cast_coe_nat, fin.coe_coe_eq_self] } end lemma int_cast_right_inverse : function.right_inverse (coe : zmod n → ℤ) (coe : ℤ → zmod n) := int_cast_zmod_cast lemma int_cast_surjective : function.surjective (coe : ℤ → zmod n) := int_cast_right_inverse.surjective @[norm_cast] lemma cast_id : ∀ n (i : zmod n), ↑i = i | 0 i := int.cast_id i | (n+1) i := nat_cast_zmod_val i @[simp] lemma cast_id' : (coe : zmod n → zmod n) = id := funext (cast_id n) variables (R) [ring R] /-- The coercions are respectively `nat.cast` and `zmod.cast`. -/ @[simp] lemma nat_cast_comp_val [fact (0 < n)] : (coe : ℕ → R) ∘ (val : zmod n → ℕ) = coe := begin casesI n, { exfalso, exact nat.not_lt_zero 0 (fact.out _) }, refl end /-- The coercions are respectively `int.cast`, `zmod.cast`, and `zmod.cast`. -/ @[simp] lemma int_cast_comp_cast : (coe : ℤ → R) ∘ (coe : zmod n → ℤ) = coe := begin cases n, { exact congr_arg ((∘) int.cast) zmod.cast_id', }, { ext, simp } end variables {R} @[simp] lemma nat_cast_val [fact (0 < n)] (i : zmod n) : (i.val : R) = i := congr_fun (nat_cast_comp_val R) i @[simp] lemma int_cast_cast (i : zmod n) : ((i : ℤ) : R) = i := congr_fun (int_cast_comp_cast R) i lemma coe_add_eq_ite {n : ℕ} (a b : zmod n) : (↑(a + b) : ℤ) = if (n : ℤ) ≤ a + b then a + b - n else a + b := begin cases n, { simp }, simp only [coe_coe, fin.coe_add_eq_ite, int.nat_cast_eq_coe_nat, ← int.coe_nat_add, ← int.coe_nat_succ, int.coe_nat_le], split_ifs with h, { exact int.coe_nat_sub h }, { refl } end section char_dvd /-! If the characteristic of `R` divides `n`, then `cast` is a homomorphism. -/ variables {n} {m : ℕ} [char_p R m] @[simp] lemma cast_one (h : m ∣ n) : ((1 : zmod n) : R) = 1 := begin casesI n, { exact int.cast_one }, show ((1 % (n+1) : ℕ) : R) = 1, cases n, { rw [nat.dvd_one] at h, substI m, apply subsingleton.elim }, rw nat.mod_eq_of_lt, { exact nat.cast_one }, exact nat.lt_of_sub_eq_succ rfl end lemma cast_add (h : m ∣ n) (a b : zmod n) : ((a + b : zmod n) : R) = a + b := begin casesI n, { apply int.cast_add }, simp only [coe_coe], symmetry, erw [fin.coe_add, ← nat.cast_add, ← sub_eq_zero, ← nat.cast_sub (nat.mod_le _ _), @char_p.cast_eq_zero_iff R _ _ m], exact h.trans (nat.dvd_sub_mod _), end lemma cast_mul (h : m ∣ n) (a b : zmod n) : ((a * b : zmod n) : R) = a * b := begin casesI n, { apply int.cast_mul }, simp only [coe_coe], symmetry, erw [fin.coe_mul, ← nat.cast_mul, ← sub_eq_zero, ← nat.cast_sub (nat.mod_le _ _), @char_p.cast_eq_zero_iff R _ _ m], exact h.trans (nat.dvd_sub_mod _), end /-- The canonical ring homomorphism from `zmod n` to a ring of characteristic `n`. See also `zmod.lift` (in `data.zmod.quotient`) for a generalized version working in `add_group`s. -/ def cast_hom (h : m ∣ n) (R : Type*) [ring R] [char_p R m] : zmod n →+* R := { to_fun := coe, map_zero' := cast_zero, map_one' := cast_one h, map_add' := cast_add h, map_mul' := cast_mul h } @[simp] lemma cast_hom_apply {h : m ∣ n} (i : zmod n) : cast_hom h R i = i := rfl @[simp, norm_cast] lemma cast_sub (h : m ∣ n) (a b : zmod n) : ((a - b : zmod n) : R) = a - b := (cast_hom h R).map_sub a b @[simp, norm_cast] lemma cast_neg (h : m ∣ n) (a : zmod n) : ((-a : zmod n) : R) = -a := (cast_hom h R).map_neg a @[simp, norm_cast] lemma cast_pow (h : m ∣ n) (a : zmod n) (k : ℕ) : ((a ^ k : zmod n) : R) = a ^ k := (cast_hom h R).map_pow a k @[simp, norm_cast] lemma cast_nat_cast (h : m ∣ n) (k : ℕ) : ((k : zmod n) : R) = k := (cast_hom h R).map_nat_cast k @[simp, norm_cast] lemma cast_int_cast (h : m ∣ n) (k : ℤ) : ((k : zmod n) : R) = k := (cast_hom h R).map_int_cast k end char_dvd section char_eq /-! Some specialised simp lemmas which apply when `R` has characteristic `n`. -/ variable [char_p R n] @[simp] lemma cast_one' : ((1 : zmod n) : R) = 1 := cast_one dvd_rfl @[simp] lemma cast_add' (a b : zmod n) : ((a + b : zmod n) : R) = a + b := cast_add dvd_rfl a b @[simp] lemma cast_mul' (a b : zmod n) : ((a * b : zmod n) : R) = a * b := cast_mul dvd_rfl a b @[simp] lemma cast_sub' (a b : zmod n) : ((a - b : zmod n) : R) = a - b := cast_sub dvd_rfl a b @[simp] lemma cast_pow' (a : zmod n) (k : ℕ) : ((a ^ k : zmod n) : R) = a ^ k := cast_pow dvd_rfl a k @[simp, norm_cast] lemma cast_nat_cast' (k : ℕ) : ((k : zmod n) : R) = k := cast_nat_cast dvd_rfl k @[simp, norm_cast] lemma cast_int_cast' (k : ℤ) : ((k : zmod n) : R) = k := cast_int_cast dvd_rfl k variables (R) lemma cast_hom_injective : function.injective (zmod.cast_hom (dvd_refl n) R) := begin rw ring_hom.injective_iff, intro x, obtain ⟨k, rfl⟩ := zmod.int_cast_surjective x, rw [ring_hom.map_int_cast, char_p.int_cast_eq_zero_iff R n, char_p.int_cast_eq_zero_iff (zmod n) n], exact id end lemma cast_hom_bijective [fintype R] (h : fintype.card R = n) : function.bijective (zmod.cast_hom (dvd_refl n) R) := begin haveI : fact (0 < n) := ⟨begin rw [pos_iff_ne_zero], intro hn, rw hn at h, exact (fintype.card_eq_zero_iff.mp h).elim' 0 end⟩, rw [fintype.bijective_iff_injective_and_card, zmod.card, h, eq_self_iff_true, and_true], apply zmod.cast_hom_injective end /-- The unique ring isomorphism between `zmod n` and a ring `R` of characteristic `n` and cardinality `n`. -/ noncomputable def ring_equiv [fintype R] (h : fintype.card R = n) : zmod n ≃+* R := ring_equiv.of_bijective _ (zmod.cast_hom_bijective R h) end char_eq end universal_property lemma int_coe_eq_int_coe_iff (a b : ℤ) (c : ℕ) : (a : zmod c) = (b : zmod c) ↔ a ≡ b [ZMOD c] := char_p.int_coe_eq_int_coe_iff (zmod c) c a b lemma int_coe_eq_int_coe_iff' (a b : ℤ) (c : ℕ) : (a : zmod c) = (b : zmod c) ↔ a % c = b % c := zmod.int_coe_eq_int_coe_iff a b c lemma nat_coe_eq_nat_coe_iff (a b c : ℕ) : (a : zmod c) = (b : zmod c) ↔ a ≡ b [MOD c] := begin convert zmod.int_coe_eq_int_coe_iff a b c, simp [nat.modeq_iff_dvd, int.modeq_iff_dvd], end lemma nat_coe_eq_nat_coe_iff' (a b c : ℕ) : (a : zmod c) = (b : zmod c) ↔ a % c = b % c := zmod.nat_coe_eq_nat_coe_iff a b c lemma int_coe_zmod_eq_zero_iff_dvd (a : ℤ) (b : ℕ) : (a : zmod b) = 0 ↔ (b : ℤ) ∣ a := begin change (a : zmod b) = ((0 : ℤ) : zmod b) ↔ (b : ℤ) ∣ a, rw [zmod.int_coe_eq_int_coe_iff, int.modeq_zero_iff_dvd], end lemma nat_coe_zmod_eq_zero_iff_dvd (a b : ℕ) : (a : zmod b) = 0 ↔ b ∣ a := begin change (a : zmod b) = ((0 : ℕ) : zmod b) ↔ b ∣ a, rw [zmod.nat_coe_eq_nat_coe_iff, nat.modeq_zero_iff_dvd], end @[push_cast, simp] lemma int_cast_mod (a : ℤ) (b : ℕ) : ((a % b : ℤ) : zmod b) = (a : zmod b) := begin rw zmod.int_coe_eq_int_coe_iff, apply int.mod_modeq, end lemma ker_int_cast_add_hom (n : ℕ) : (int.cast_add_hom (zmod n)).ker = add_subgroup.zmultiples n := by { ext, rw [int.mem_zmultiples_iff, add_monoid_hom.mem_ker, int.coe_cast_add_hom, int_coe_zmod_eq_zero_iff_dvd] } lemma ker_int_cast_ring_hom (n : ℕ) : (int.cast_ring_hom (zmod n)).ker = ideal.span ({n} : set ℤ) := by { ext, rw [ideal.mem_span_singleton, ring_hom.mem_ker, int.coe_cast_ring_hom, int_coe_zmod_eq_zero_iff_dvd] } local attribute [semireducible] int.nonneg @[simp] lemma nat_cast_to_nat (p : ℕ) : ∀ {z : ℤ} (h : 0 ≤ z), (z.to_nat : zmod p) = z | (n : ℕ) h := by simp only [int.cast_coe_nat, int.to_nat_coe_nat] | -[1+n] h := false.elim h lemma val_injective (n : ℕ) [fact (0 < n)] : function.injective (zmod.val : zmod n → ℕ) := begin casesI n, { exfalso, exact nat.not_lt_zero 0 (fact.out _) }, assume a b h, ext, exact h end lemma val_one_eq_one_mod (n : ℕ) : (1 : zmod n).val = 1 % n := by rw [← nat.cast_one, val_nat_cast] lemma val_one (n : ℕ) [fact (1 < n)] : (1 : zmod n).val = 1 := by { rw val_one_eq_one_mod, exact nat.mod_eq_of_lt (fact.out _) } lemma val_add {n : ℕ} [fact (0 < n)] (a b : zmod n) : (a + b).val = (a.val + b.val) % n := begin casesI n, { exfalso, exact nat.not_lt_zero 0 (fact.out _) }, { apply fin.val_add } end lemma val_mul {n : ℕ} (a b : zmod n) : (a * b).val = (a.val * b.val) % n := begin cases n, { rw nat.mod_zero, apply int.nat_abs_mul }, { apply fin.val_mul } end instance nontrivial (n : ℕ) [fact (1 < n)] : nontrivial (zmod n) := ⟨⟨0, 1, assume h, zero_ne_one $ calc 0 = (0 : zmod n).val : by rw val_zero ... = (1 : zmod n).val : congr_arg zmod.val h ... = 1 : val_one n ⟩⟩ /-- The inversion on `zmod n`. It is setup in such a way that `a * a⁻¹` is equal to `gcd a.val n`. In particular, if `a` is coprime to `n`, and hence a unit, `a * a⁻¹ = 1`. -/ def inv : Π (n : ℕ), zmod n → zmod n | 0 i := int.sign i | (n+1) i := nat.gcd_a i.val (n+1) instance (n : ℕ) : has_inv (zmod n) := ⟨inv n⟩ lemma inv_zero : ∀ (n : ℕ), (0 : zmod n)⁻¹ = 0 | 0 := int.sign_zero | (n+1) := show (nat.gcd_a _ (n+1) : zmod (n+1)) = 0, by { rw val_zero, unfold nat.gcd_a nat.xgcd nat.xgcd_aux, refl } lemma mul_inv_eq_gcd {n : ℕ} (a : zmod n) : a * a⁻¹ = nat.gcd a.val n := begin cases n, { calc a * a⁻¹ = a * int.sign a : rfl ... = a.nat_abs : by rw [int.mul_sign, int.nat_cast_eq_coe_nat] ... = a.val.gcd 0 : by rw nat.gcd_zero_right; refl }, { set k := n.succ, calc a * a⁻¹ = a * a⁻¹ + k * nat.gcd_b (val a) k : by rw [nat_cast_self, zero_mul, add_zero] ... = ↑(↑a.val * nat.gcd_a (val a) k + k * nat.gcd_b (val a) k) : by { push_cast, rw nat_cast_zmod_val, refl } ... = nat.gcd a.val k : (congr_arg coe (nat.gcd_eq_gcd_ab a.val k)).symm, } end @[simp] lemma nat_cast_mod (n : ℕ) (a : ℕ) : ((a % n : ℕ) : zmod n) = a := by conv {to_rhs, rw ← nat.mod_add_div a n}; simp lemma eq_iff_modeq_nat (n : ℕ) {a b : ℕ} : (a : zmod n) = b ↔ a ≡ b [MOD n] := begin cases n, { simp only [nat.modeq, int.coe_nat_inj', nat.mod_zero, int.nat_cast_eq_coe_nat], }, { rw [fin.ext_iff, nat.modeq, ← val_nat_cast, ← val_nat_cast], exact iff.rfl, } end lemma coe_mul_inv_eq_one {n : ℕ} (x : ℕ) (h : nat.coprime x n) : (x * x⁻¹ : zmod n) = 1 := begin rw [nat.coprime, nat.gcd_comm, nat.gcd_rec] at h, rw [mul_inv_eq_gcd, val_nat_cast, h, nat.cast_one], end /-- `unit_of_coprime` makes an element of `units (zmod n)` given a natural number `x` and a proof that `x` is coprime to `n` -/ def unit_of_coprime {n : ℕ} (x : ℕ) (h : nat.coprime x n) : units (zmod n) := ⟨x, x⁻¹, coe_mul_inv_eq_one x h, by rw [mul_comm, coe_mul_inv_eq_one x h]⟩ @[simp] lemma coe_unit_of_coprime {n : ℕ} (x : ℕ) (h : nat.coprime x n) : (unit_of_coprime x h : zmod n) = x := rfl lemma val_coe_unit_coprime {n : ℕ} (u : units (zmod n)) : nat.coprime (u : zmod n).val n := begin cases n, { rcases int.units_eq_one_or u with rfl|rfl; simp }, apply nat.coprime_of_mul_modeq_one ((u⁻¹ : units (zmod (n+1))) : zmod (n+1)).val, have := units.ext_iff.1 (mul_right_inv u), rw [units.coe_one] at this, rw [← eq_iff_modeq_nat, nat.cast_one, ← this], clear this, rw [← nat_cast_zmod_val ((u * u⁻¹ : units (zmod (n+1))) : zmod (n+1))], rw [units.coe_mul, val_mul, nat_cast_mod], end @[simp] lemma inv_coe_unit {n : ℕ} (u : units (zmod n)) : (u : zmod n)⁻¹ = (u⁻¹ : units (zmod n)) := begin have := congr_arg (coe : ℕ → zmod n) (val_coe_unit_coprime u), rw [← mul_inv_eq_gcd, nat.cast_one] at this, let u' : units (zmod n) := ⟨u, (u : zmod n)⁻¹, this, by rwa mul_comm⟩, have h : u = u', { apply units.ext, refl }, rw h, refl end lemma mul_inv_of_unit {n : ℕ} (a : zmod n) (h : is_unit a) : a * a⁻¹ = 1 := begin rcases h with ⟨u, rfl⟩, rw [inv_coe_unit, u.mul_inv], end lemma inv_mul_of_unit {n : ℕ} (a : zmod n) (h : is_unit a) : a⁻¹ * a = 1 := by rw [mul_comm, mul_inv_of_unit a h] /-- Equivalence between the units of `zmod n` and the subtype of terms `x : zmod n` for which `x.val` is comprime to `n` -/ def units_equiv_coprime {n : ℕ} [fact (0 < n)] : units (zmod n) ≃ {x : zmod n // nat.coprime x.val n} := { to_fun := λ x, ⟨x, val_coe_unit_coprime x⟩, inv_fun := λ x, unit_of_coprime x.1.val x.2, left_inv := λ ⟨_, _, _, _⟩, units.ext (nat_cast_zmod_val _), right_inv := λ ⟨_, _⟩, by simp } /-- The **Chinese remainder theorem**. For a pair of coprime natural numbers, `m` and `n`, the rings `zmod (m * n)` and `zmod m × zmod n` are isomorphic. See `ideal.quotient_inf_ring_equiv_pi_quotient` for the Chinese remainder theorem for ideals in any ring. -/ def chinese_remainder {m n : ℕ} (h : m.coprime n) : zmod (m * n) ≃+* zmod m × zmod n := let to_fun : zmod (m * n) → zmod m × zmod n := zmod.cast_hom (show m.lcm n ∣ m * n, by simp [nat.lcm_dvd_iff]) (zmod m × zmod n) in let inv_fun : zmod m × zmod n → zmod (m * n) := λ x, if m * n = 0 then if m = 1 then ring_hom.snd _ _ x else ring_hom.fst _ _ x else nat.chinese_remainder h x.1.val x.2.val in have inv : function.left_inverse inv_fun to_fun ∧ function.right_inverse inv_fun to_fun := if hmn0 : m * n = 0 then begin rcases h.eq_of_mul_eq_zero hmn0 with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩; simp [inv_fun, to_fun, function.left_inverse, function.right_inverse, ring_hom.eq_int_cast, prod.ext_iff] end else begin haveI : fact (0 < (m * n)) := ⟨nat.pos_of_ne_zero hmn0⟩, haveI : fact (0 < m) := ⟨nat.pos_of_ne_zero $ left_ne_zero_of_mul hmn0⟩, haveI : fact (0 < n) := ⟨nat.pos_of_ne_zero $ right_ne_zero_of_mul hmn0⟩, have left_inv : function.left_inverse inv_fun to_fun, { intro x, dsimp only [dvd_mul_left, dvd_mul_right, zmod.cast_hom_apply, coe_coe, inv_fun, to_fun], conv_rhs { rw ← zmod.nat_cast_zmod_val x }, rw [if_neg hmn0, zmod.eq_iff_modeq_nat, ← nat.modeq_and_modeq_iff_modeq_mul h, prod.fst_zmod_cast, prod.snd_zmod_cast], refine ⟨(nat.chinese_remainder h (x : zmod m).val (x : zmod n).val).2.left.trans _, (nat.chinese_remainder h (x : zmod m).val (x : zmod n).val).2.right.trans _⟩, { rw [← zmod.eq_iff_modeq_nat, zmod.nat_cast_zmod_val, zmod.nat_cast_val] }, { rw [← zmod.eq_iff_modeq_nat, zmod.nat_cast_zmod_val, zmod.nat_cast_val] } }, exact ⟨left_inv, fintype.right_inverse_of_left_inverse_of_card_le left_inv (by simp)⟩, end, { to_fun := to_fun, inv_fun := inv_fun, map_mul' := ring_hom.map_mul _, map_add' := ring_hom.map_add _, left_inv := inv.1, right_inv := inv.2 } instance subsingleton_units : subsingleton (units (zmod 2)) := ⟨λ x y, begin ext1, cases x with x xi hx1 hx2, cases y with y yi hy1 hy2, revert hx1 hx2 hy1 hy2, fin_cases x; fin_cases y; simp end⟩ lemma le_div_two_iff_lt_neg (n : ℕ) [hn : fact ((n : ℕ) % 2 = 1)] {x : zmod n} (hx0 : x ≠ 0) : x.val ≤ (n / 2 : ℕ) ↔ (n / 2 : ℕ) < (-x).val := begin haveI npos : fact (0 < n) := ⟨by { apply (nat.eq_zero_or_pos n).resolve_left, unfreezingI { rintro rfl }, simpa [fact_iff] using hn, }⟩, have hn2 : (n : ℕ) / 2 < n := nat.div_lt_of_lt_mul ((lt_mul_iff_one_lt_left npos.1).2 dec_trivial), have hn2' : (n : ℕ) - n / 2 = n / 2 + 1, { conv {to_lhs, congr, rw [← nat.succ_sub_one n, nat.succ_sub npos.1]}, rw [← nat.two_mul_odd_div_two hn.1, two_mul, ← nat.succ_add, add_tsub_cancel_right], }, have hxn : (n : ℕ) - x.val < n, { rw [tsub_lt_iff_tsub_lt x.val_le le_rfl, tsub_self], rw ← zmod.nat_cast_zmod_val x at hx0, exact nat.pos_of_ne_zero (λ h, by simpa [h] using hx0) }, by conv {to_rhs, rw [← nat.succ_le_iff, nat.succ_eq_add_one, ← hn2', ← zero_add (- x), ← zmod.nat_cast_self, ← sub_eq_add_neg, ← zmod.nat_cast_zmod_val x, ← nat.cast_sub x.val_le, zmod.val_nat_cast, nat.mod_eq_of_lt hxn, tsub_le_tsub_iff_left x.val_le] } end lemma ne_neg_self (n : ℕ) [hn : fact ((n : ℕ) % 2 = 1)] {a : zmod n} (ha : a ≠ 0) : a ≠ -a := λ h, have a.val ≤ n / 2 ↔ (n : ℕ) / 2 < (-a).val := le_div_two_iff_lt_neg n ha, by rwa [← h, ← not_lt, not_iff_self] at this lemma neg_one_ne_one {n : ℕ} [fact (2 < n)] : (-1 : zmod n) ≠ 1 := char_p.neg_one_ne_one (zmod n) n @[simp] lemma neg_eq_self_mod_two (a : zmod 2) : -a = a := by fin_cases a; ext; simp [fin.coe_neg, int.nat_mod]; norm_num @[simp] lemma nat_abs_mod_two (a : ℤ) : (a.nat_abs : zmod 2) = a := begin cases a, { simp only [int.nat_abs_of_nat, int.cast_coe_nat, int.of_nat_eq_coe] }, { simp only [neg_eq_self_mod_two, nat.cast_succ, int.nat_abs, int.cast_neg_succ_of_nat] } end @[simp] lemma val_eq_zero : ∀ {n : ℕ} (a : zmod n), a.val = 0 ↔ a = 0 | 0 a := int.nat_abs_eq_zero | (n+1) a := by { rw fin.ext_iff, exact iff.rfl } lemma val_cast_of_lt {n : ℕ} {a : ℕ} (h : a < n) : (a : zmod n).val = a := by rw [val_nat_cast, nat.mod_eq_of_lt h] lemma neg_val' {n : ℕ} [fact (0 < n)] (a : zmod n) : (-a).val = (n - a.val) % n := calc (-a).val = val (-a) % n : by rw nat.mod_eq_of_lt ((-a).val_lt) ... = (n - val a) % n : nat.modeq.add_right_cancel' _ (by rw [nat.modeq, ←val_add, add_left_neg, tsub_add_cancel_of_le a.val_le, nat.mod_self, val_zero]) lemma neg_val {n : ℕ} [fact (0 < n)] (a : zmod n) : (-a).val = if a = 0 then 0 else n - a.val := begin rw neg_val', by_cases h : a = 0, { rw [if_pos h, h, val_zero, tsub_zero, nat.mod_self] }, rw if_neg h, apply nat.mod_eq_of_lt, apply nat.sub_lt (fact.out (0 < n)), contrapose! h, rwa [nat.le_zero_iff, val_eq_zero] at h, end /-- `val_min_abs x` returns the integer in the same equivalence class as `x` that is closest to `0`, The result will be in the interval `(-n/2, n/2]`. -/ def val_min_abs : Π {n : ℕ}, zmod n → ℤ | 0 x := x | n@(_+1) x := if x.val ≤ n / 2 then x.val else (x.val : ℤ) - n @[simp] lemma val_min_abs_def_zero (x : zmod 0) : val_min_abs x = x := rfl lemma val_min_abs_def_pos {n : ℕ} [fact (0 < n)] (x : zmod n) : val_min_abs x = if x.val ≤ n / 2 then x.val else x.val - n := begin casesI n, { exfalso, exact nat.not_lt_zero 0 (fact.out (0 < 0)) }, { refl } end @[simp] lemma coe_val_min_abs : ∀ {n : ℕ} (x : zmod n), (x.val_min_abs : zmod n) = x | 0 x := int.cast_id x | k@(n+1) x := begin rw val_min_abs_def_pos, split_ifs, { rw [int.cast_coe_nat, nat_cast_zmod_val] }, { rw [int.cast_sub, int.cast_coe_nat, nat_cast_zmod_val, int.cast_coe_nat, nat_cast_self, sub_zero] } end lemma nat_abs_val_min_abs_le {n : ℕ} [fact (0 < n)] (x : zmod n) : x.val_min_abs.nat_abs ≤ n / 2 := begin rw zmod.val_min_abs_def_pos, split_ifs with h, { exact h }, have : (x.val - n : ℤ) ≤ 0, { rw [sub_nonpos, int.coe_nat_le], exact x.val_le, }, rw [← int.coe_nat_le, int.of_nat_nat_abs_of_nonpos this, neg_sub], conv_lhs { congr, rw [← nat.mod_add_div n 2, int.coe_nat_add, int.coe_nat_mul, int.coe_nat_bit0, int.coe_nat_one] }, suffices : ((n % 2 : ℕ) + (n / 2) : ℤ) ≤ (val x), { rw ← sub_nonneg at this ⊢, apply le_trans this (le_of_eq _), ring_nf, ring }, norm_cast, calc (n : ℕ) % 2 + n / 2 ≤ 1 + n / 2 : nat.add_le_add_right (nat.le_of_lt_succ (nat.mod_lt _ dec_trivial)) _ ... ≤ x.val : by { rw add_comm, exact nat.succ_le_of_lt (lt_of_not_ge h) } end @[simp] lemma val_min_abs_zero : ∀ n, (0 : zmod n).val_min_abs = 0 | 0 := by simp only [val_min_abs_def_zero] | (n+1) := by simp only [val_min_abs_def_pos, if_true, int.coe_nat_zero, zero_le, val_zero] @[simp] lemma val_min_abs_eq_zero {n : ℕ} (x : zmod n) : x.val_min_abs = 0 ↔ x = 0 := begin cases n, { simp }, split, { simp only [val_min_abs_def_pos, int.coe_nat_succ], split_ifs with h h; assume h0, { apply val_injective, rwa [int.coe_nat_eq_zero] at h0, }, { apply absurd h0, rw sub_eq_zero, apply ne_of_lt, exact_mod_cast x.val_lt } }, { rintro rfl, rw val_min_abs_zero } end lemma nat_cast_nat_abs_val_min_abs {n : ℕ} [fact (0 < n)] (a : zmod n) : (a.val_min_abs.nat_abs : zmod n) = if a.val ≤ (n : ℕ) / 2 then a else -a := begin have : (a.val : ℤ) - n ≤ 0, by { erw [sub_nonpos, int.coe_nat_le], exact a.val_le, }, rw [zmod.val_min_abs_def_pos], split_ifs, { rw [int.nat_abs_of_nat, nat_cast_zmod_val] }, { rw [← int.cast_coe_nat, int.of_nat_nat_abs_of_nonpos this, int.cast_neg, int.cast_sub], rw [int.cast_coe_nat, int.cast_coe_nat, nat_cast_self, sub_zero, nat_cast_zmod_val], } end @[simp] lemma nat_abs_val_min_abs_neg {n : ℕ} (a : zmod n) : (-a).val_min_abs.nat_abs = a.val_min_abs.nat_abs := begin cases n, { simp only [int.nat_abs_neg, val_min_abs_def_zero], }, by_cases ha0 : a = 0, { rw [ha0, neg_zero] }, by_cases haa : -a = a, { rw [haa] }, suffices hpa : (n+1 : ℕ) - a.val ≤ (n+1) / 2 ↔ (n+1 : ℕ) / 2 < a.val, { rw [val_min_abs_def_pos, val_min_abs_def_pos], rw ← not_le at hpa, simp only [if_neg ha0, neg_val, hpa, int.coe_nat_sub a.val_le], split_ifs, all_goals { rw [← int.nat_abs_neg], congr' 1, ring } }, suffices : (((n+1 : ℕ) % 2) + 2 * ((n + 1) / 2)) - a.val ≤ (n+1) / 2 ↔ (n+1 : ℕ) / 2 < a.val, by rwa [nat.mod_add_div] at this, suffices : (n + 1) % 2 + (n + 1) / 2 ≤ val a ↔ (n + 1) / 2 < val a, by rw [tsub_le_iff_tsub_le, two_mul, ← add_assoc, add_tsub_cancel_right, this], cases (n + 1 : ℕ).mod_two_eq_zero_or_one with hn0 hn1, { split, { assume h, apply lt_of_le_of_ne (le_trans (nat.le_add_left _ _) h), contrapose! haa, rw [← zmod.nat_cast_zmod_val a, ← haa, neg_eq_iff_add_eq_zero, ← nat.cast_add], rw [char_p.cast_eq_zero_iff (zmod (n+1)) (n+1)], rw [← two_mul, ← zero_add (2 * _), ← hn0, nat.mod_add_div] }, { rw [hn0, zero_add], exact le_of_lt } }, { rw [hn1, add_comm, nat.succ_le_iff] } end lemma val_eq_ite_val_min_abs {n : ℕ} [fact (0 < n)] (a : zmod n) : (a.val : ℤ) = a.val_min_abs + if a.val ≤ n / 2 then 0 else n := by { rw [zmod.val_min_abs_def_pos], split_ifs; simp only [add_zero, sub_add_cancel] } lemma prime_ne_zero (p q : ℕ) [hp : fact p.prime] [hq : fact q.prime] (hpq : p ≠ q) : (q : zmod p) ≠ 0 := by rwa [← nat.cast_zero, ne.def, eq_iff_modeq_nat, nat.modeq_zero_iff_dvd, ← hp.1.coprime_iff_not_dvd, nat.coprime_primes hp.1 hq.1] end zmod namespace zmod variables (p : ℕ) [fact p.prime] private lemma mul_inv_cancel_aux (a : zmod p) (h : a ≠ 0) : a * a⁻¹ = 1 := begin obtain ⟨k, rfl⟩ := nat_cast_zmod_surjective a, apply coe_mul_inv_eq_one, apply nat.coprime.symm, rwa [nat.prime.coprime_iff_not_dvd (fact.out p.prime), ← char_p.cast_eq_zero_iff (zmod p)] end /-- Field structure on `zmod p` if `p` is prime. -/ instance : field (zmod p) := { mul_inv_cancel := mul_inv_cancel_aux p, inv_zero := inv_zero p, .. zmod.comm_ring p, .. zmod.has_inv p, .. zmod.nontrivial p } /-- `zmod p` is an integral domain when `p` is prime. -/ instance (p : ℕ) [hp : fact p.prime] : is_domain (zmod p) := begin -- We need `cases p` here in order to resolve which `comm_ring` instance is being used. unfreezingI { cases p, { exfalso, rcases hp with ⟨⟨⟨⟩⟩⟩, }, }, exact @field.is_domain (zmod _) (zmod.field _) end end zmod lemma ring_hom.ext_zmod {n : ℕ} {R : Type*} [semiring R] (f g : (zmod n) →+* R) : f = g := begin ext a, obtain ⟨k, rfl⟩ := zmod.int_cast_surjective a, let φ : ℤ →+* R := f.comp (int.cast_ring_hom (zmod n)), let ψ : ℤ →+* R := g.comp (int.cast_ring_hom (zmod n)), show φ k = ψ k, rw φ.ext_int ψ, end namespace zmod variables {n : ℕ} {R : Type*} instance subsingleton_ring_hom [semiring R] : subsingleton ((zmod n) →+* R) := ⟨ring_hom.ext_zmod⟩ instance subsingleton_ring_equiv [semiring R] : subsingleton (zmod n ≃+* R) := ⟨λ f g, by { rw ring_equiv.coe_ring_hom_inj_iff, apply ring_hom.ext_zmod _ _ }⟩ @[simp] lemma ring_hom_map_cast [ring R] (f : R →+* (zmod n)) (k : zmod n) : f k = k := by { cases n; simp } lemma ring_hom_right_inverse [ring R] (f : R →+* (zmod n)) : function.right_inverse (coe : zmod n → R) f := ring_hom_map_cast f lemma ring_hom_surjective [ring R] (f : R →+* (zmod n)) : function.surjective f := (ring_hom_right_inverse f).surjective lemma ring_hom_eq_of_ker_eq [comm_ring R] (f g : R →+* (zmod n)) (h : f.ker = g.ker) : f = g := begin have := f.lift_of_right_inverse_comp _ (zmod.ring_hom_right_inverse f) ⟨g, le_of_eq h⟩, rw subtype.coe_mk at this, rw [←this, ring_hom.ext_zmod (f.lift_of_right_inverse _ _ _) (ring_hom.id _), ring_hom.id_comp], end section lift variables (n) {A : Type*} [add_group A] /-- The map from `zmod n` induced by `f : ℤ →+ A` that maps `n` to `0`. -/ @[simps] def lift : {f : ℤ →+ A // f n = 0} ≃ (zmod n →+ A) := (equiv.subtype_equiv_right $ begin intro f, rw ker_int_cast_add_hom, split, { rintro hf _ ⟨x, rfl⟩, simp only [f.map_zsmul, zsmul_zero, f.mem_ker, hf] }, { intro h, refine h (add_subgroup.mem_zmultiples _) } end).trans $ ((int.cast_add_hom (zmod n)).lift_of_right_inverse coe int_cast_zmod_cast) variables (f : {f : ℤ →+ A // f n = 0}) @[simp] lemma lift_coe (x : ℤ) : lift n f (x : zmod n) = f x := add_monoid_hom.lift_of_right_inverse_comp_apply _ _ _ _ _ lemma lift_cast_add_hom (x : ℤ) : lift n f (int.cast_add_hom (zmod n) x) = f x := add_monoid_hom.lift_of_right_inverse_comp_apply _ _ _ _ _ @[simp] lemma lift_comp_coe : zmod.lift n f ∘ coe = f := funext $ lift_coe _ _ @[simp] lemma lift_comp_cast_add_hom : (zmod.lift n f).comp (int.cast_add_hom (zmod n)) = f := add_monoid_hom.ext $ lift_cast_add_hom _ _ end lift end zmod
7570bdbac4f7f6d22753e4e280771a601dc1f827
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/ring/boolean_ring.lean
3851071263fab2c1c8d6654d898421e29f85d261
[ "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
15,487
lean
/- Copyright (c) 2021 Bryan Gin-ge Chen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bryan Gin-ge Chen, Yaël Dillies -/ import algebra.punit_instances import order.hom.lattice import tactic.abel import tactic.ring /-! # Boolean rings A Boolean ring is a ring where multiplication is idempotent. They are equivalent to Boolean algebras. ## Main declarations * `boolean_ring`: a typeclass for rings where multiplication is idempotent. * `boolean_ring.to_boolean_algebra`: Turn a Boolean ring into a Boolean algebra. * `boolean_algebra.to_boolean_ring`: Turn a Boolean algebra into a Boolean ring. * `as_boolalg`: Type-synonym for the Boolean algebra associated to a Boolean ring. * `as_boolring`: Type-synonym for the Boolean ring associated to a Boolean algebra. ## Implementation notes We provide two ways of turning a Boolean algebra/ring into a Boolean ring/algebra: * Instances on the same type accessible in locales `boolean_algebra_of_boolean_ring` and `boolean_ring_of_boolean_algebra`. * Type-synonyms `as_boolalg` and `as_boolring`. At this point in time, it is not clear the first way is useful, but we keep it for educational purposes and because it is easier than dealing with `of_boolalg`/`to_boolalg`/`of_boolring`/`to_boolring` explicitly. ## Tags boolean ring, boolean algebra -/ variables {α β γ : Type*} /-- A Boolean ring is a ring where multiplication is idempotent. -/ class boolean_ring α extends ring α := (mul_self : ∀ a : α, a * a = a) section boolean_ring variables [boolean_ring α] (a b : α) instance : is_idempotent α (*) := ⟨boolean_ring.mul_self⟩ @[simp] lemma mul_self : a * a = a := boolean_ring.mul_self _ @[simp] lemma add_self : a + a = 0 := have a + a = a + a + (a + a) := calc a + a = (a+a) * (a+a) : by rw mul_self ... = a*a + a*a + (a*a + a*a) : by rw [add_mul, mul_add] ... = a + a + (a + a) : by rw mul_self, by rwa self_eq_add_left at this @[simp] lemma neg_eq : -a = a := calc -a = -a + 0 : by rw add_zero ... = -a + -a + a : by rw [←neg_add_self, add_assoc] ... = a : by rw [add_self, zero_add] lemma add_eq_zero : a + b = 0 ↔ a = b := calc a + b = 0 ↔ a = -b : add_eq_zero_iff_eq_neg ... ↔ a = b : by rw neg_eq @[simp] lemma mul_add_mul : a*b + b*a = 0 := have a + b = a + b + (a*b + b*a) := calc a + b = (a + b) * (a + b) : by rw mul_self ... = a*a + a*b + (b*a + b*b) : by rw [add_mul, mul_add, mul_add] ... = a + a*b + (b*a + b) : by simp only [mul_self] ... = a + b + (a*b + b*a) : by abel, by rwa self_eq_add_right at this @[simp] lemma sub_eq_add : a - b = a + b := by rw [sub_eq_add_neg, add_right_inj, neg_eq] @[simp] lemma mul_one_add_self : a * (1 + a) = 0 := by rw [mul_add, mul_one, mul_self, add_self] @[priority 100] -- Note [lower instance priority] instance boolean_ring.to_comm_ring : comm_ring α := { mul_comm := λ a b, by rw [←add_eq_zero, mul_add_mul], .. (infer_instance : boolean_ring α) } end boolean_ring instance : boolean_ring punit := ⟨λ _, subsingleton.elim _ _⟩ /-! ### Turning a Boolean ring into a Boolean algebra -/ section ring_to_algebra /-- Type synonym to view a Boolean ring as a Boolean algebra. -/ def as_boolalg (α : Type*) := α /-- The "identity" equivalence between `as_boolalg α` and `α`. -/ def to_boolalg : α ≃ as_boolalg α := equiv.refl _ /-- The "identity" equivalence between `α` and `as_boolalg α`. -/ def of_boolalg : as_boolalg α ≃ α := equiv.refl _ @[simp] lemma to_boolalg_symm_eq : (@to_boolalg α).symm = of_boolalg := rfl @[simp] lemma of_boolalg_symm_eq : (@of_boolalg α).symm = to_boolalg := rfl @[simp] lemma to_boolalg_of_boolalg (a : as_boolalg α) : to_boolalg (of_boolalg a) = a := rfl @[simp] lemma of_boolalg_to_boolalg (a : α) : of_boolalg (to_boolalg a) = a := rfl @[simp] lemma to_boolalg_inj {a b : α} : to_boolalg a = to_boolalg b ↔ a = b := iff.rfl @[simp] lemma of_boolalg_inj {a b : as_boolalg α} : of_boolalg a = of_boolalg b ↔ a = b := iff.rfl instance [inhabited α] : inhabited (as_boolalg α) := ‹inhabited α› variables [boolean_ring α] [boolean_ring β] [boolean_ring γ] namespace boolean_ring /-- The join operation in a Boolean ring is `x + y + x * y`. -/ def has_sup : has_sup α := ⟨λ x y, x + y + x * y⟩ /-- The meet operation in a Boolean ring is `x * y`. -/ def has_inf : has_inf α := ⟨(*)⟩ -- Note [lower instance priority] localized "attribute [instance, priority 100] boolean_ring.has_sup" in boolean_algebra_of_boolean_ring localized "attribute [instance, priority 100] boolean_ring.has_inf" in boolean_algebra_of_boolean_ring lemma sup_comm (a b : α) : a ⊔ b = b ⊔ a := by { dsimp only [(⊔)], ring } lemma inf_comm (a b : α) : a ⊓ b = b ⊓ a := by { dsimp only [(⊓)], ring } lemma sup_assoc (a b c : α) : a ⊔ b ⊔ c = a ⊔ (b ⊔ c) := by { dsimp only [(⊔)], ring } lemma inf_assoc (a b c : α) : a ⊓ b ⊓ c = a ⊓ (b ⊓ c) := by { dsimp only [(⊓)], ring } lemma sup_inf_self (a b : α) : a ⊔ a ⊓ b = a := by { dsimp only [(⊔), (⊓)], assoc_rw [mul_self, add_self, add_zero] } lemma inf_sup_self (a b : α) : a ⊓ (a ⊔ b) = a := by { dsimp only [(⊔), (⊓)], assoc_rw [mul_add, mul_add, mul_self, mul_self, add_self, add_zero] } lemma le_sup_inf_aux (a b c : α) : (a + b + a * b) * (a + c + a * c) = a + b * c + a * (b * c) := calc (a + b + a * b) * (a + c + a * c) = a * a + b * c + a * (b * c) + (a * b + (a * a) * b) + (a * c + (a * a) * c) + (a * b * c + (a * a) * b * c) : by ring ... = a + b * c + a * (b * c) : by simp only [mul_self, add_self, add_zero] lemma le_sup_inf (a b c : α) : (a ⊔ b) ⊓ (a ⊔ c) ⊔ (a ⊔ b ⊓ c) = a ⊔ b ⊓ c := by { dsimp only [(⊔), (⊓)], rw [le_sup_inf_aux, add_self, mul_self, zero_add] } /-- The Boolean algebra structure on a Boolean ring. The data is defined so that: * `a ⊔ b` unfolds to `a + b + a * b` * `a ⊓ b` unfolds to `a * b` * `a ≤ b` unfolds to `a + b + a * b = b` * `⊥` unfolds to `0` * `⊤` unfolds to `1` * `aᶜ` unfolds to `1 + a` * `a \ b` unfolds to `a * (1 + b)` -/ def to_boolean_algebra : boolean_algebra α := boolean_algebra.of_core { le_sup_inf := le_sup_inf, top := 1, le_top := λ a, show a + 1 + a * 1 = 1, by assoc_rw [mul_one, add_comm, add_self, add_zero], bot := 0, bot_le := λ a, show 0 + a + 0 * a = a, by rw [zero_mul, zero_add, add_zero], compl := λ a, 1 + a, inf_compl_le_bot := λ a, show a*(1+a) + 0 + a*(1+a)*0 = 0, by norm_num [mul_add, mul_self, add_self], top_le_sup_compl := λ a, begin change 1 + (a + (1+a) + a*(1+a)) + 1*(a + (1+a) + a*(1+a)) = a + (1+a) + a*(1+a), norm_num [mul_add, mul_self], rw [←add_assoc, add_self], end, .. lattice.mk' sup_comm sup_assoc inf_comm inf_assoc sup_inf_self inf_sup_self } localized "attribute [instance, priority 100] boolean_ring.to_boolean_algebra" in boolean_algebra_of_boolean_ring end boolean_ring instance : boolean_algebra (as_boolalg α) := @boolean_ring.to_boolean_algebra α _ @[simp] lemma of_boolalg_top : of_boolalg (⊤ : as_boolalg α) = 1 := rfl @[simp] lemma of_boolalg_bot : of_boolalg (⊥ : as_boolalg α) = 0 := rfl @[simp] lemma of_boolalg_sup (a b : as_boolalg α) : of_boolalg (a ⊔ b) = of_boolalg a + of_boolalg b + of_boolalg a * of_boolalg b := rfl @[simp] lemma of_boolalg_inf (a b : as_boolalg α) : of_boolalg (a ⊓ b) = of_boolalg a * of_boolalg b := rfl @[simp] lemma of_boolalg_compl (a : as_boolalg α) : of_boolalg aᶜ = 1 + of_boolalg a := rfl @[simp] lemma of_boolalg_sdiff (a b : as_boolalg α) : of_boolalg (a \ b) = of_boolalg a * (1 + of_boolalg b) := rfl private lemma of_boolalg_symm_diff_aux (a b : α) : (a + b + a * b) * (1 + a * b) = a + b := calc (a + b + a * b) * (1 + a * b) = a + b + (a * b + (a * b) * (a * b)) + (a * (b * b) + (a * a) * b) : by ring ... = a + b : by simp only [mul_self, add_self, add_zero] @[simp] lemma of_boolalg_symm_diff (a b : as_boolalg α) : of_boolalg (a ∆ b) = of_boolalg a + of_boolalg b := by { rw symm_diff_eq_sup_sdiff_inf, exact of_boolalg_symm_diff_aux _ _ } @[simp] lemma of_boolalg_mul_of_boolalg_eq_left_iff {a b : as_boolalg α} : of_boolalg a * of_boolalg b = of_boolalg a ↔ a ≤ b := @inf_eq_left (as_boolalg α) _ _ _ @[simp] lemma to_boolalg_zero : to_boolalg (0 : α) = ⊥ := rfl @[simp] lemma to_boolalg_one : to_boolalg (1 : α) = ⊤ := rfl @[simp] lemma to_boolalg_mul (a b : α) : to_boolalg (a * b) = to_boolalg a ⊓ to_boolalg b := rfl -- `to_boolalg_add` simplifies the LHS but this lemma is eligible to `dsimp` @[simp, nolint simp_nf] lemma to_boolalg_add_add_mul (a b : α) : to_boolalg (a + b + a * b) = to_boolalg a ⊔ to_boolalg b := rfl @[simp] lemma to_boolalg_add (a b : α) : to_boolalg (a + b) = to_boolalg a ∆ to_boolalg b := (of_boolalg_symm_diff _ _).symm /-- Turn a ring homomorphism from Boolean rings `α` to `β` into a bounded lattice homomorphism from `α` to `β` considered as Boolean algebras. -/ @[simps] protected def ring_hom.as_boolalg (f : α →+* β) : bounded_lattice_hom (as_boolalg α) (as_boolalg β) := { to_fun := to_boolalg ∘ f ∘ of_boolalg, map_sup' := λ a b, begin dsimp, simp_rw [map_add f, map_mul f], refl, end, map_inf' := f.map_mul', map_top' := f.map_one', map_bot' := f.map_zero' } @[simp] lemma ring_hom.as_boolalg_id : (ring_hom.id α).as_boolalg = bounded_lattice_hom.id _ := rfl @[simp] lemma ring_hom.as_boolalg_comp (g : β →+* γ) (f : α →+* β) : (g.comp f).as_boolalg = g.as_boolalg.comp f.as_boolalg := rfl end ring_to_algebra /-! ### Turning a Boolean algebra into a Boolean ring -/ section algebra_to_ring /-- Type synonym to view a Boolean ring as a Boolean algebra. -/ def as_boolring (α : Type*) := α /-- The "identity" equivalence between `as_boolring α` and `α`. -/ def to_boolring : α ≃ as_boolring α := equiv.refl _ /-- The "identity" equivalence between `α` and `as_boolring α`. -/ def of_boolring : as_boolring α ≃ α := equiv.refl _ @[simp] lemma to_boolring_symm_eq : (@to_boolring α).symm = of_boolring := rfl @[simp] lemma of_boolring_symm_eq : (@of_boolring α).symm = to_boolring := rfl @[simp] lemma to_boolring_of_boolring (a : as_boolring α) : to_boolring (of_boolring a) = a := rfl @[simp] lemma of_boolring_to_boolring (a : α) : of_boolring (to_boolring a) = a := rfl @[simp] lemma to_boolring_inj {a b : α} : to_boolring a = to_boolring b ↔ a = b := iff.rfl @[simp] lemma of_boolring_inj {a b : as_boolring α} : of_boolring a = of_boolring b ↔ a = b := iff.rfl instance [inhabited α] : inhabited (as_boolring α) := ‹inhabited α› /-- Every generalized Boolean algebra has the structure of a non unital commutative ring with the following data: * `a + b` unfolds to `a ∆ b` (symmetric difference) * `a * b` unfolds to `a ⊓ b` * `-a` unfolds to `a` * `0` unfolds to `⊥` -/ @[reducible] -- See note [reducible non-instances] def generalized_boolean_algebra.to_non_unital_comm_ring [generalized_boolean_algebra α] : non_unital_comm_ring α := { add := (∆), add_assoc := symm_diff_assoc, zero := ⊥, zero_add := bot_symm_diff, add_zero := symm_diff_bot, zero_mul := λ _, bot_inf_eq, mul_zero := λ _, inf_bot_eq, neg := id, add_left_neg := symm_diff_self, add_comm := symm_diff_comm, mul := (⊓), mul_assoc := λ _ _ _, inf_assoc, mul_comm := λ _ _, inf_comm, left_distrib := inf_symm_diff_distrib_left, right_distrib := inf_symm_diff_distrib_right } instance [generalized_boolean_algebra α] : non_unital_comm_ring (as_boolring α) := @generalized_boolean_algebra.to_non_unital_comm_ring α _ variables [boolean_algebra α] [boolean_algebra β] [boolean_algebra γ] /-- Every Boolean algebra has the structure of a Boolean ring with the following data: * `a + b` unfolds to `a ∆ b` (symmetric difference) * `a * b` unfolds to `a ⊓ b` * `-a` unfolds to `a` * `0` unfolds to `⊥` * `1` unfolds to `⊤` -/ @[reducible] -- See note [reducible non-instances] def boolean_algebra.to_boolean_ring : boolean_ring α := { one := ⊤, one_mul := λ _, top_inf_eq, mul_one := λ _, inf_top_eq, mul_self := λ b, inf_idem, ..generalized_boolean_algebra.to_non_unital_comm_ring } localized "attribute [instance, priority 100] generalized_boolean_algebra.to_non_unital_comm_ring boolean_algebra.to_boolean_ring" in boolean_ring_of_boolean_algebra instance : boolean_ring (as_boolring α) := @boolean_algebra.to_boolean_ring α _ @[simp] lemma of_boolring_zero : of_boolring (0 : as_boolring α) = ⊥ := rfl @[simp] lemma of_boolring_one : of_boolring (1 : as_boolring α) = ⊤ := rfl -- `sub_eq_add` proves this lemma but it is eligible for `dsimp` @[simp, nolint simp_nf] lemma of_boolring_neg (a : as_boolring α) : of_boolring (-a) = of_boolring a := rfl @[simp] lemma of_boolring_add (a b : as_boolring α) : of_boolring (a + b) = of_boolring a ∆ of_boolring b := rfl -- `sub_eq_add` simplifies the LHS but this lemma is eligible for `dsimp` @[simp, nolint simp_nf] lemma of_boolring_sub (a b : as_boolring α) : of_boolring (a - b) = of_boolring a ∆ of_boolring b := rfl @[simp] lemma of_boolring_mul (a b : as_boolring α) : of_boolring (a * b) = of_boolring a ⊓ of_boolring b := rfl @[simp] lemma of_boolring_le_of_boolring_iff {a b : as_boolring α} : of_boolring a ≤ of_boolring b ↔ a * b = a := inf_eq_left.symm @[simp] lemma to_boolring_bot : to_boolring (⊥ : α) = 0 := rfl @[simp] lemma to_boolring_top : to_boolring (⊤ : α) = 1 := rfl @[simp] lemma to_boolring_inf (a b : α) : to_boolring (a ⊓ b) = to_boolring a * to_boolring b := rfl @[simp] lemma to_boolring_symm_diff (a b : α) : to_boolring (a ∆ b) = to_boolring a + to_boolring b := rfl /-- Turn a bounded lattice homomorphism from Boolean algebras `α` to `β` into a ring homomorphism from `α` to `β` considered as Boolean rings. -/ @[simps] protected def bounded_lattice_hom.as_boolring (f : bounded_lattice_hom α β) : as_boolring α →+* as_boolring β := { to_fun := to_boolring ∘ f ∘ of_boolring, map_zero' := f.map_bot', map_one' := f.map_top', map_add' := map_symm_diff f, map_mul' := f.map_inf' } @[simp] lemma bounded_lattice_hom.as_boolring_id : (bounded_lattice_hom.id α).as_boolring = ring_hom.id _ := rfl @[simp] lemma bounded_lattice_hom.as_boolring_comp (g : bounded_lattice_hom β γ) (f : bounded_lattice_hom α β) : (g.comp f).as_boolring = g.as_boolring.comp f.as_boolring := rfl end algebra_to_ring /-! ### Equivalence between Boolean rings and Boolean algebras -/ /-- Order isomorphism between `α` considered as a Boolean ring considered as a Boolean algebra and `α`. -/ @[simps] def order_iso.as_boolalg_as_boolring (α : Type*) [boolean_algebra α] : as_boolalg (as_boolring α) ≃o α := ⟨of_boolalg.trans of_boolring, λ a b, of_boolring_le_of_boolring_iff.trans of_boolalg_mul_of_boolalg_eq_left_iff⟩ /-- Ring isomorphism between `α` considered as a Boolean algebra considered as a Boolean ring and `α`. -/ @[simps] def ring_equiv.as_boolring_as_boolalg (α : Type*) [boolean_ring α] : as_boolring (as_boolalg α) ≃+* α := { map_mul' := λ a b, rfl, map_add' := of_boolalg_symm_diff, ..of_boolring.trans of_boolalg }
253f9f27e628068398a9627d9fac69e47f0e81d3
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/1658.lean
7ef87ee1e8b381d03b8c92d99510196ebc381a99
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
224
lean
constant p : bool → bool constant P : bool → Prop noncomputable def lp : bool → bool | ff := p ff | tt := p tt def foo (b : bool) : Prop := P (lp b) constant T : bool → Type def boo (b : bool) : Type := T (lp b)
70e914b2930b21b7e453810e75f20ae81bde38a1
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Parser/Term.lean
18da324c2b13a50e6792181e3ea42be2618a6626
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
28,303
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Parser.Attr import Lean.Parser.Level namespace Lean namespace Parser namespace Command def commentBody : Parser := { fn := rawFn (finishCommentBlock 1) (trailingWs := true) } @[combinatorParenthesizer Lean.Parser.Command.commentBody] def commentBody.parenthesizer := PrettyPrinter.Parenthesizer.visitToken @[combinatorFormatter Lean.Parser.Command.commentBody] def commentBody.formatter := PrettyPrinter.Formatter.visitAtom Name.anonymous def docComment := leading_parser ppDedent $ "/--" >> ppSpace >> commentBody >> ppLine end Command builtin_initialize registerBuiltinParserAttribute `builtinTacticParser ``Category.tactic .both registerBuiltinDynamicParserAttribute `tacticParser `tactic @[inline] def tacticParser (rbp : Nat := 0) : Parser := categoryParser `tactic rbp @[inline] def convParser (rbp : Nat := 0) : Parser := categoryParser `conv rbp namespace Tactic def tacticSeq1Indented : Parser := leading_parser many1Indent (group (ppLine >> tacticParser >> optional ";")) /-- The syntax `{ tacs }` is an alternative syntax for `· tacs`. It runs the tactics in sequence, and fails if the goal is not solved. -/ def tacticSeqBracketed : Parser := leading_parser "{" >> many (group (ppLine >> tacticParser >> optional ";")) >> ppDedent (ppLine >> "}") def tacticSeq := leading_parser tacticSeqBracketed <|> tacticSeq1Indented /-- Raw sequence for quotation and grouping -/ def seq1 := node `Lean.Parser.Tactic.seq1 $ sepBy1 tacticParser ";\n" (allowTrailingSep := true) end Tactic def darrow : Parser := " => " def semicolonOrLinebreak := ";" <|> checkLinebreakBefore >> pushNone namespace Term /-! # Built-in parsers -/ /-- `by tac` constructs a term of the expected type by running the tactic(s) `tac`. -/ @[builtinTermParser] def byTactic := leading_parser:leadPrec ppAllowUngrouped >> "by " >> Tactic.tacticSeq /-- This is the same as `byTactic`, but it uses a different syntax kind. This is used by `show` and `suffices` instead of `byTactic` because these syntaxes don't support arbitrary terms where `byTactic` is accepted. Mathport uses this to e.g. safely find-replace `by exact $e` by `$e` in any context without causing incorrect syntax when the full expression is `show $T by exact $e`. -/ def byTactic' := leading_parser "by " >> Tactic.tacticSeq -- TODO: rename to e.g. `afterSemicolonOrLinebreak` def optSemicolon (p : Parser) : Parser := ppDedent $ semicolonOrLinebreak >> ppLine >> p -- `checkPrec` necessary for the pretty printer @[builtinTermParser] def ident := checkPrec maxPrec >> Parser.ident @[builtinTermParser] def num : Parser := checkPrec maxPrec >> numLit @[builtinTermParser] def scientific : Parser := checkPrec maxPrec >> scientificLit @[builtinTermParser] def str : Parser := checkPrec maxPrec >> strLit @[builtinTermParser] def char : Parser := checkPrec maxPrec >> charLit /-- A type universe. `Type ≡ Type 0`, `Type u ≡ Sort (u + 1)`. -/ @[builtinTermParser] def type := leading_parser "Type" >> optional (checkWsBefore "" >> checkPrec leadPrec >> checkColGt >> levelParser maxPrec) /-- A specific universe in Lean's infinite hierarchy of universes. -/ @[builtinTermParser] def sort := leading_parser "Sort" >> optional (checkWsBefore "" >> checkPrec leadPrec >> checkColGt >> levelParser maxPrec) /-- The universe of propositions. `Prop ≡ Sort 0`. -/ @[builtinTermParser] def prop := leading_parser "Prop" /-- A placeholder term, to be synthesized by unification. -/ @[builtinTermParser] def hole := leading_parser "_" @[builtinTermParser] def syntheticHole := leading_parser "?" >> (ident <|> hole) /-- A temporary placeholder for a missing proof or value. -/ @[builtinTermParser] def «sorry» := leading_parser "sorry" @[builtinTermParser] def cdot := leading_parser symbol "·" <|> "." def typeAscription := leading_parser " : " >> termParser def tupleTail := leading_parser ", " >> sepBy1 termParser ", " def parenSpecial : Parser := optional (tupleTail <|> typeAscription) /-- You can use parentheses for - Grouping expressions, e.g., `a * (b + c)`. - Creating tuples, e.g., `(a, b, c)` is notation for `Prod.mk a (Prod.mk b c)`. - Performing type ascription, e.g., `(0 : Int)` instructs Lean to process `0` as a value of type `Int`. - Creating `Unit.unit`, `()` is just a shorthand for `Unit.unit`. - Creating simple functions when combined with `·`. Here are some examples: - `(· + 1)` is shorthand for `fun x => x + 1` - `(· + ·)` is shorthand for `fun x y => x + y` - `(f · a b)` is shorthand for `fun x => f x a b` - `(h (· + 1) ·)` is shorthand for `fun x => h (fun y => y + 1) x` -/ @[builtinTermParser] def paren := leading_parser "(" >> (withoutPosition (withoutForbidden (optional (ppDedentIfGrouped termParser >> parenSpecial)))) >> ")" /-- The *anonymous constructor* `⟨e, ...⟩` is equivalent to `c e ...` if the expected type is an inductive type with a single constructor `c`. If more terms are given than `c` has parameters, the remaining arguments are turned into a new anonymous constructor application. For example, `⟨a, b, c⟩ : α × (β × γ)` is equivalent to `⟨a, ⟨b, c⟩⟩`. -/ @[builtinTermParser] def anonymousCtor := leading_parser "⟨" >> sepBy termParser ", " >> "⟩" def optIdent : Parser := optional (atomic (ident >> " : ")) def fromTerm := leading_parser "from " >> termParser def showRhs := fromTerm <|> byTactic' def sufficesDecl := leading_parser optIdent >> termParser >> ppSpace >> showRhs @[builtinTermParser] def «suffices» := leading_parser:leadPrec withPosition ("suffices " >> sufficesDecl) >> optSemicolon termParser @[builtinTermParser] def «show» := leading_parser:leadPrec "show " >> termParser >> ppSpace >> showRhs def structInstArrayRef := leading_parser "[" >> termParser >>"]" def structInstLVal := leading_parser (ident <|> fieldIdx <|> structInstArrayRef) >> many (group ("." >> (ident <|> fieldIdx)) <|> structInstArrayRef) def structInstField := ppGroup $ leading_parser structInstLVal >> " := " >> termParser def structInstFieldAbbrev := leading_parser atomic (ident >> notFollowedBy ("." <|> ":=" <|> symbol "[") "invalid field abbreviation") -- `x` is an abbreviation for `x := x` def optEllipsis := leading_parser optional ".." /-- Structure instance. `{ x := e, ... }` assigns `e` to field `x`, which may be inherited. If `e` is itself a variable called `x`, it can be elided: `fun y => { x := 1, y }`. A *structure update* of an existing value can be given via `with`: `{ point with x := 1 }`. The structure type can be specified if not inferable: `{ x := 1, y := 2 : Point }`. -/ @[builtinTermParser] def structInst := leading_parser "{" >> ppHardSpace >> optional (atomic (sepBy1 termParser ", " >> " with ")) >> sepByIndent (structInstFieldAbbrev <|> structInstField) ", " (allowTrailingSep := true) >> optEllipsis >> optional (" : " >> termParser) >> " }" def typeSpec := leading_parser " : " >> termParser def optType : Parser := optional typeSpec /-- `@x` disables automatic insertion of implicit parameters of the constant `x`. `@e` for any term `e` also disables the insertion of implicit lambdas at this position. -/ @[builtinTermParser] def explicit := leading_parser "@" >> termParser maxPrec @[builtinTermParser] def inaccessible := leading_parser ".(" >> termParser >> ")" def binderIdent : Parser := ident <|> hole def binderType (requireType := false) : Parser := if requireType then node nullKind (" : " >> termParser) else optional (" : " >> termParser) def binderTactic := leading_parser atomic (symbol " := " >> " by ") >> Tactic.tacticSeq def binderDefault := leading_parser " := " >> termParser def explicitBinder (requireType := false) := ppGroup $ leading_parser "(" >> many1 binderIdent >> binderType requireType >> optional (binderTactic <|> binderDefault) >> ")" def implicitBinder (requireType := false) := ppGroup $ leading_parser "{" >> many1 binderIdent >> binderType requireType >> "}" def strictImplicitLeftBracket := atomic (group (symbol "{" >> "{")) <|> "⦃" def strictImplicitRightBracket := atomic (group (symbol "}" >> "}")) <|> "⦄" def strictImplicitBinder (requireType := false) := ppGroup $ leading_parser strictImplicitLeftBracket >> many1 binderIdent >> binderType requireType >> strictImplicitRightBracket def instBinder := ppGroup $ leading_parser "[" >> optIdent >> termParser >> "]" def bracketedBinder (requireType := false) := withAntiquot (mkAntiquot "bracketedBinder" `Lean.Parser.Term.bracketedBinder (isPseudoKind := true)) <| explicitBinder requireType <|> strictImplicitBinder requireType <|> implicitBinder requireType <|> instBinder /-- It is feasible to support dependent arrows such as `{α} → α → α` without sacrificing the quality of the error messages for the longer case. `{α} → α → α` would be short for `{α : Type} → α → α` Here is the encoding: ``` def implicitShortBinder := node `Lean.Parser.Term.implicitBinder $ "{" >> many1 binderIdent >> pushNone >> "}" def depArrowShortPrefix := try (implicitShortBinder >> unicodeSymbol " → " " -> ") def depArrowLongPrefix := bracketedBinder true >> unicodeSymbol " → " " -> " def depArrowPrefix := depArrowShortPrefix <|> depArrowLongPrefix @[builtinTermParser] def depArrow := leading_parser depArrowPrefix >> termParser ``` Note that no changes in the elaborator are needed. We decided to not use it because terms such as `{α} → α → α` may look too cryptic. Note that we did not add a `explicitShortBinder` parser since `(α) → α → α` is really cryptic as a short for `(α : Type) → α → α`. -/ @[builtinTermParser] def depArrow := leading_parser:25 bracketedBinder true >> unicodeSymbol " → " " -> " >> termParser @[builtinTermParser] def «forall» := leading_parser:leadPrec unicodeSymbol "∀" "forall" >> many1 (ppSpace >> (binderIdent <|> bracketedBinder)) >> optType >> ", " >> termParser def matchAlt (rhsParser : Parser := termParser) : Parser := leading_parser (withAnonymousAntiquot := false) "| " >> ppIndent (sepBy1 (sepBy1 termParser ", ") "|" >> darrow >> checkColGe "alternative right-hand-side to start in a column greater than or equal to the corresponding '|'" >> rhsParser) /-- Useful for syntax quotations. Note that generic patterns such as `` `(matchAltExpr| | ... => $rhs) `` should also work with other `rhsParser`s (of arity 1). -/ def matchAltExpr := matchAlt instance : Coe (TSyntax ``matchAltExpr) (TSyntax ``matchAlt) where coe stx := ⟨stx.raw⟩ def matchAlts (rhsParser : Parser := termParser) : Parser := leading_parser withPosition $ many1Indent (ppLine >> matchAlt rhsParser) def matchDiscr := leading_parser optional (atomic (ident >> " : ")) >> termParser def trueVal := leading_parser nonReservedSymbol "true" def falseVal := leading_parser nonReservedSymbol "false" def generalizingParam := leading_parser atomic ("(" >> nonReservedSymbol "generalizing") >> " := " >> (trueVal <|> falseVal) >> ")" >> ppSpace def motive := leading_parser atomic ("(" >> nonReservedSymbol "motive" >> " := ") >> termParser >> ")" >> ppSpace /-- Pattern matching. `match e, ... with | p, ... => f | ...` matches each given term `e` against each pattern `p` of a match alternative. When all patterns of an alternative match, the `match` term evaluates to the value of the corresponding right-hand side `f` with the pattern variables bound to the respective matched values. When not constructing a proof, `match` does not automatically substitute variables matched on in dependent variables' types. Use `match (generalizing := true) ...` to enforce this. Syntax quotations can also be used in a pattern match. This matches a `Syntax` value against quotations, pattern variables, or `_`. Quoted identifiers only match identical identifiers - custom matching such as by the preresolved names only should be done explicitly. `Syntax.atom`s are ignored during matching by default except when part of a built-in literal. For users introducing new atoms, we recommend wrapping them in dedicated syntax kinds if they should participate in matching. For example, in ```lean syntax "c" ("foo" <|> "bar") ... ``` `foo` and `bar` are indistinguishable during matching, but in ```lean syntax foo := "foo" syntax "c" (foo <|> "bar") ... ``` they are not. -/ @[builtinTermParser] def «match» := leading_parser:leadPrec "match " >> optional generalizingParam >> optional motive >> sepBy1 matchDiscr ", " >> " with " >> ppDedent matchAlts /-- Empty match/ex falso. `nomatch e` is of arbitrary type `α : Sort u` if Lean can show that an empty set of patterns is exhaustive given `e`'s type, e.g. because it has no constructors. -/ @[builtinTermParser] def «nomatch» := leading_parser:leadPrec "nomatch " >> termParser def funImplicitBinder := withAntiquot (mkAntiquot "implicitBinder" ``implicitBinder) <| atomic (lookahead ("{" >> many1 binderIdent >> (symbol " : " <|> "}"))) >> implicitBinder def funStrictImplicitBinder := atomic (lookahead (strictImplicitLeftBracket >> many1 binderIdent >> (symbol " : " <|> strictImplicitRightBracket))) >> strictImplicitBinder def funBinder : Parser := withAntiquot (mkAntiquot "funBinder" `Lean.Parser.Term.funBinder (isPseudoKind := true)) (funStrictImplicitBinder <|> funImplicitBinder <|> instBinder <|> termParser maxPrec) -- NOTE: we disable anonymous antiquotations to ensure that `fun $b => ...` remains a `term` antiquotation def basicFun : Parser := leading_parser (withAnonymousAntiquot := false) ppGroup (many1 (ppSpace >> funBinder) >> optType >> " =>") >> ppSpace >> termParser @[builtinTermParser] def «fun» := leading_parser:maxPrec ppAllowUngrouped >> unicodeSymbol "λ" "fun" >> (basicFun <|> matchAlts) def optExprPrecedence := optional (atomic ":" >> termParser maxPrec) def withAnonymousAntiquot := leading_parser atomic ("(" >> nonReservedSymbol "withAnonymousAntiquot" >> " := ") >> (trueVal <|> falseVal) >> ")" >> ppSpace @[builtinTermParser] def «leading_parser» := leading_parser:leadPrec "leading_parser " >> optExprPrecedence >> optional withAnonymousAntiquot >> termParser @[builtinTermParser] def «trailing_parser» := leading_parser:leadPrec "trailing_parser " >> optExprPrecedence >> optExprPrecedence >> termParser @[builtinTermParser] def borrowed := leading_parser "@& " >> termParser leadPrec /-- A literal of type `Name`. -/ @[builtinTermParser] def quotedName := leading_parser nameLit /-- A resolved name literal. Evaluates to the full name of the given constant if existent in the current context, or else fails. -/ -- use `rawCh` because ``"`" >> ident`` overlaps with `nameLit`, with the latter being preferred by the tokenizer -- note that we cannot use ```"``"``` as a new token either because it would break `precheckedQuot` @[builtinTermParser] def doubleQuotedName := leading_parser "`" >> checkNoWsBefore >> rawCh '`' (trailingWs := false) >> ident def letIdBinder := withAntiquot (mkAntiquot "letIdBinder" `Lean.Parser.Term.letIdBinder (isPseudoKind := true)) (binderIdent <|> bracketedBinder) /-- Remark: we use `checkWsBefore` to ensure `let x[i] := e; b` is not parsed as `let x [i] := e; b` where `[i]` is an `instBinder`. -/ def letIdLhs : Parser := ident >> notFollowedBy (checkNoWsBefore "" >> "[") "space is required before instance '[...]' binders to distinguish them from array updates `let x[i] := e; ...`" >> many (ppSpace >> letIdBinder) >> optType def letIdDecl := leading_parser (withAnonymousAntiquot := false) atomic (letIdLhs >> " := ") >> termParser def letPatDecl := leading_parser (withAnonymousAntiquot := false) atomic (termParser >> pushNone >> optType >> " := ") >> termParser /-- Remark: the following `(" := " <|> matchAlts)` is a hack we use to produce a better error message at `letDecl`. Consider this following example ``` def myFun (n : Nat) : IO Nat := let q ← (10 : Nat) n + q ``` Without the hack, we get the error `expected '|'` at `←`. Reason: at `letDecl`, we use the parser `(letIdDecl <|> letPatDecl <|> letEqnsDecl)`, `letIdDecl` and `letEqnsDecl` have the same prefix `letIdLhs`, but `letIdDecl` uses `atomic`. Note that the hack relies on the fact that the parser `":="` never succeeds at `(" := " <|> matchAlts)`. It is there just to make sure we produce the error `expected ':=' or '|'` -/ def letEqnsDecl := leading_parser (withAnonymousAntiquot := false) letIdLhs >> (" := " <|> matchAlts) -- Remark: we disable anonymous antiquotations here to make sure anonymous antiquotations (e.g., `$x`) are not `letDecl` def letDecl := leading_parser (withAnonymousAntiquot := false) notFollowedBy (nonReservedSymbol "rec") "rec" >> (letIdDecl <|> letPatDecl <|> letEqnsDecl) /-- `let` is used to declare a local definition. Example: ``` let x := 1 let y := x + 1 x + y ``` Since functions are first class citizens in Lean, you can use `let` to declare local functions too. ``` let double := fun x => 2*x double (double 3) ``` For recursive definitions, you should use `let rec`. You can also perform pattern matching using `let`. For example, assume `p` has type `Nat × Nat`, then you can write ``` let (x, y) := p x + y ``` -/ @[builtinTermParser] def «let» := leading_parser:leadPrec withPosition ("let " >> letDecl) >> optSemicolon termParser /-- `let_fun x := v; b` is syntax sugar for `(fun x => b) v`. It is very similar to `let x := v; b`, but they are not equivalent. In `let_fun`, the value `v` has been abstracted away and cannot be accessed in `b`. -/ @[builtinTermParser] def «let_fun» := leading_parser:leadPrec withPosition ((symbol "let_fun " <|> "let_λ") >> letDecl) >> optSemicolon termParser /-- `let_delayed x := v; b` is similar to `let x := v; b`, but `b` is elaborated before `v`. -/ @[builtinTermParser] def «let_delayed» := leading_parser:leadPrec withPosition ("let_delayed " >> letDecl) >> optSemicolon termParser /-- `let`-declaration that is only included in the elaborated term if variable is still there. It is often used when building macros. -/ @[builtinTermParser] def «let_tmp» := leading_parser:leadPrec withPosition ("let_tmp " >> letDecl) >> optSemicolon termParser instance : Coe (TSyntax ``letIdBinder) (TSyntax ``funBinder) where coe stx := ⟨stx⟩ /-- like `let_fun` but with optional name -/ def haveIdLhs := optional (ident >> many (ppSpace >> letIdBinder)) >> optType def haveIdDecl := leading_parser (withAnonymousAntiquot := false) atomic (haveIdLhs >> " := ") >> termParser def haveEqnsDecl := leading_parser (withAnonymousAntiquot := false) haveIdLhs >> matchAlts def haveDecl := leading_parser (withAnonymousAntiquot := false) haveIdDecl <|> letPatDecl <|> haveEqnsDecl @[builtinTermParser] def «have» := leading_parser:leadPrec withPosition ("have " >> haveDecl) >> optSemicolon termParser def «scoped» := leading_parser "scoped " def «local» := leading_parser "local " def attrKind := leading_parser optional («scoped» <|> «local») def attrInstance := ppGroup $ leading_parser attrKind >> attrParser def attributes := leading_parser "@[" >> sepBy1 attrInstance ", " >> "]" def letRecDecl := leading_parser optional Command.docComment >> optional «attributes» >> letDecl def letRecDecls := leading_parser sepBy1 letRecDecl ", " @[builtinTermParser] def «letrec» := leading_parser:leadPrec withPosition (group ("let " >> nonReservedSymbol "rec ") >> letRecDecls) >> optSemicolon termParser @[runBuiltinParserAttributeHooks] def whereDecls := leading_parser " where" >> sepBy1Indent (ppGroup letRecDecl) "; " (allowTrailingSep := true) @[runBuiltinParserAttributeHooks] def matchAltsWhereDecls := leading_parser matchAlts >> optional whereDecls @[builtinTermParser] def noindex := leading_parser "no_index " >> termParser maxPrec @[builtinTermParser] def binrel := leading_parser "binrel% " >> ident >> ppSpace >> termParser maxPrec >> termParser maxPrec /-- Similar to `binrel`, but coerce `Prop` arguments into `Bool`. -/ @[builtinTermParser] def binrel_no_prop := leading_parser "binrel_no_prop% " >> ident >> ppSpace >> termParser maxPrec >> termParser maxPrec @[builtinTermParser] def binop := leading_parser "binop% " >> ident >> ppSpace >> termParser maxPrec >> termParser maxPrec @[builtinTermParser] def binop_lazy := leading_parser "binop_lazy% " >> ident >> ppSpace >> termParser maxPrec >> termParser maxPrec @[builtinTermParser] def forInMacro := leading_parser "for_in% " >> termParser maxPrec >> termParser maxPrec >> termParser maxPrec @[builtinTermParser] def forInMacro' := leading_parser "for_in'% " >> termParser maxPrec >> termParser maxPrec >> termParser maxPrec /-- A macro which evaluates to the name of the currently elaborating declaration. -/ @[builtinTermParser] def declName := leading_parser "decl_name%" /-- * `with_decl_name% id e` elaborates `e` in a context while changing the effective declaration name to `id`. * `with_decl_name% ?id e` does the same, but resolves `id` as a new definition name (appending the current namespaces). -/ @[builtinTermParser] def withDeclName := leading_parser "with_decl_name% " >> optional "?" >> ident >> termParser @[builtinTermParser] def typeOf := leading_parser "type_of% " >> termParser maxPrec @[builtinTermParser] def ensureTypeOf := leading_parser "ensure_type_of% " >> termParser maxPrec >> strLit >> termParser @[builtinTermParser] def ensureExpectedType := leading_parser "ensure_expected_type% " >> strLit >> termParser maxPrec @[builtinTermParser] def noImplicitLambda := leading_parser "no_implicit_lambda% " >> termParser maxPrec @[builtinTermParser] def letMVar := leading_parser "let_mvar% " >> "?" >> ident >> " := " >> termParser >> "; " >> termParser @[builtinTermParser] def waitIfTypeMVar := leading_parser "wait_if_type_mvar% " >> "?" >> ident >> "; " >> termParser @[builtinTermParser] def waitIfTypeContainsMVar := leading_parser "wait_if_type_contains_mvar% " >> "?" >> ident >> "; " >> termParser @[builtinTermParser] def waitIfContainsMVar := leading_parser "wait_if_contains_mvar% " >> "?" >> ident >> "; " >> termParser @[builtinTermParser] def defaultOrOfNonempty := leading_parser "default_or_ofNonempty% " >> optional "unsafe" /-- Helper parser for marking `match`-alternatives that should not trigger errors if unused. We use them to implement `macro_rules` and `elab_rules` -/ @[builtinTermParser] def noErrorIfUnused := leading_parser "no_error_if_unused%" >> termParser def namedArgument := leading_parser (withAnonymousAntiquot := false) atomic ("(" >> ident >> " := ") >> termParser >> ")" def ellipsis := leading_parser (withAnonymousAntiquot := false) ".." def argument := checkWsBefore "expected space" >> checkColGt "expected to be indented" >> (namedArgument <|> ellipsis <|> termParser argPrec) -- `app` precedence is `lead` (cannot be used as argument) -- `lhs` precedence is `max` (i.e. does not accept `arg` precedence) -- argument precedence is `arg` (i.e. does not accept `lead` precedence) @[builtinTermParser] def app := trailing_parser:leadPrec:maxPrec many1 argument @[builtinTermParser] def proj := trailing_parser checkNoWsBefore >> "." >> checkNoWsBefore >> (fieldIdx <|> rawIdent) @[builtinTermParser] def completion := trailing_parser checkNoWsBefore >> "." @[builtinTermParser] def arrow := trailing_parser checkPrec 25 >> unicodeSymbol " → " " -> " >> termParser 25 def isIdent (stx : Syntax) : Bool := -- antiquotations should also be allowed where an identifier is expected stx.isAntiquot || stx.isIdent /-- `x.{u, ...}` explicitly specifies the universes `u, ...` of the constant `x`. -/ @[builtinTermParser] def explicitUniv : TrailingParser := trailing_parser checkStackTop isIdent "expected preceding identifier" >> checkNoWsBefore "no space before '.{'" >> ".{" >> sepBy1 levelParser ", " >> "}" /-- `x@e` matches the pattern `e` and binds its value to the identifier `x`. -/ @[builtinTermParser] def namedPattern : TrailingParser := trailing_parser checkStackTop isIdent "expected preceding identifier" >> checkNoWsBefore "no space before '@'" >> "@" >> optional (atomic (ident >> ":")) >> termParser maxPrec /-- `e |>.x` is a shorthand for `(e).x`. It is especially useful for avoiding parentheses with repeated applications. -/ @[builtinTermParser] def pipeProj := trailing_parser:minPrec " |>." >> checkNoWsBefore >> (fieldIdx <|> rawIdent) >> many argument @[builtinTermParser] def pipeCompletion := trailing_parser:minPrec " |>." /-- `h ▸ e` is a macro built on top of `Eq.rec` and `Eq.symm` definitions. Given `h : a = b` and `e : p a`, the term `h ▸ e` has type `p b`. You can also view `h ▸ e` as a "type casting" operation where you change the type of `e` by using `h`. See the Chapter "Quantifiers and Equality" in the manual "Theorem Proving in Lean" for additional information. -/ @[builtinTermParser] def subst := trailing_parser:75 " ▸ " >> sepBy1 (termParser 75) " ▸ " -- NOTE: Doesn't call `categoryParser` directly in contrast to most other "static" quotations, so call `evalInsideQuot` explicitly @[builtinTermParser] def funBinder.quot : Parser := leading_parser "`(funBinder|" >> incQuotDepth (evalInsideQuot ``funBinder funBinder) >> ")" def bracketedBinderF := bracketedBinder -- no default arg @[builtinTermParser] def bracketedBinder.quot : Parser := leading_parser "`(bracketedBinder|" >> incQuotDepth (evalInsideQuot ``bracketedBinderF bracketedBinder) >> ")" @[builtinTermParser] def matchDiscr.quot : Parser := leading_parser "`(matchDiscr|" >> incQuotDepth (evalInsideQuot ``matchDiscr matchDiscr) >> ")" @[builtinTermParser] def attr.quot : Parser := leading_parser "`(attr|" >> incQuotDepth attrParser >> ")" /-- `panic! msg` formally evaluates to `@Inhabited.default α` if the expected type `α` implements `Inhabited`. At runtime, `msg` and the file position are printed to stderr unless the C function `lean_set_panic_messages(false)` has been executed before. If the C function `lean_set_exit_on_panic(true)` has been executed before, the process is then aborted. -/ @[builtinTermParser] def panic := leading_parser:leadPrec "panic! " >> termParser /-- A shorthand for `panic! "unreachable code has been reached"`. -/ @[builtinTermParser] def unreachable := leading_parser:leadPrec "unreachable!" /-- `dbg_trace e; body` evaluates to `body` and prints `e` (which can be an interpolated string literal) to stderr. It should only be used for debugging. -/ @[builtinTermParser] def dbgTrace := leading_parser:leadPrec withPosition ("dbg_trace" >> ((interpolatedStr termParser) <|> termParser)) >> optSemicolon termParser /-- `assert! cond` panics if `cond` evaluates to `false`. -/ @[builtinTermParser] def assert := leading_parser:leadPrec withPosition ("assert! " >> termParser) >> optSemicolon termParser def macroArg := termParser maxPrec def macroDollarArg := leading_parser "$" >> termParser 10 def macroLastArg := macroDollarArg <|> macroArg -- Macro for avoiding exponentially big terms when using `STWorld` @[builtinTermParser] def stateRefT := leading_parser "StateRefT" >> macroArg >> macroLastArg @[builtinTermParser] def dynamicQuot := leading_parser "`(" >> ident >> "|" >> incQuotDepth (parserOfStack 1) >> ")" @[builtinTermParser] def dotIdent := leading_parser "." >> checkNoWsBefore >> rawIdent end Term @[builtinTermParser default+1] def Tactic.quot : Parser := leading_parser "`(tactic|" >> incQuotDepth tacticParser >> ")" @[builtinTermParser] def Tactic.quotSeq : Parser := leading_parser "`(tactic|" >> incQuotDepth Tactic.seq1 >> ")" @[builtinTermParser] def Level.quot : Parser := leading_parser "`(level|" >> incQuotDepth levelParser >> ")" open Term in builtin_initialize register_parser_alias letDecl register_parser_alias haveDecl register_parser_alias sufficesDecl register_parser_alias letRecDecls register_parser_alias hole register_parser_alias syntheticHole register_parser_alias matchDiscr register_parser_alias bracketedBinder register_parser_alias attrKind end Parser end Lean
a912e59a7a3e200a5de7da4819d32cdbcd363ca5
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/ring_theory/non_unital_subsemiring/basic.lean
2d262812f2738fbe43f93903d1d675d0ec4494d6
[ "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
34,063
lean
/- Copyright (c) 2022 Jireh Loreaux All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux -/ import algebra.ring.equiv import algebra.ring.prod import data.set.finite import group_theory.submonoid.membership import group_theory.subsemigroup.membership import group_theory.subsemigroup.centralizer /-! # Bundled non-unital subsemirings We define bundled non-unital subsemirings and some standard constructions: `complete_lattice` structure, `subtype` and `inclusion` ring homomorphisms, non-unital subsemiring `map`, `comap` and range (`srange`) of a `non_unital_ring_hom` etc. -/ open_locale big_operators universes u v w variables {R : Type u} {S : Type v} {T : Type w} [non_unital_non_assoc_semiring R] (M : subsemigroup R) /-- `non_unital_subsemiring_class S R` states that `S` is a type of subsets `s ⊆ R` that are both an additive submonoid and also a multiplicative subsemigroup. -/ class non_unital_subsemiring_class (S : Type*) (R : out_param $ Type u) [non_unital_non_assoc_semiring R] [set_like S R] extends add_submonoid_class S R := (mul_mem : ∀ {s : S} {a b : R}, a ∈ s → b ∈ s → a * b ∈ s) @[priority 100] -- See note [lower instance priority] instance non_unital_subsemiring_class.mul_mem_class (S : Type*) (R : out_param $ Type u) [non_unital_non_assoc_semiring R] [set_like S R] [h : non_unital_subsemiring_class S R] : mul_mem_class S R := { .. h } namespace non_unital_subsemiring_class variables [set_like S R] [non_unital_subsemiring_class S R] (s : S) include R S open add_submonoid_class /-- A non-unital subsemiring of a `non_unital_non_assoc_semiring` inherits a `non_unital_non_assoc_semiring` structure -/ @[priority 75] /- Prefer subclasses of `non_unital_non_assoc_semiring` over subclasses of `non_unital_subsemiring_class`. -/ instance to_non_unital_non_assoc_semiring : non_unital_non_assoc_semiring s := subtype.coe_injective.non_unital_non_assoc_semiring coe rfl (by simp) (λ _ _, rfl) (λ _ _, rfl) instance no_zero_divisors [no_zero_divisors R] : no_zero_divisors s := subtype.coe_injective.no_zero_divisors coe rfl (λ x y, rfl) /-- The natural non-unital ring hom from a non-unital subsemiring of a non-unital semiring `R` to `R`. -/ def subtype : s →ₙ+* R := { to_fun := coe, .. add_submonoid_class.subtype s, .. mul_mem_class.subtype s } @[simp] theorem coe_subtype : (subtype s : s → R) = coe := rfl omit R S /-- A non-unital subsemiring of a `non_unital_semiring` is a `non_unital_semiring`. -/ instance to_non_unital_semiring {R} [non_unital_semiring R] [set_like S R] [non_unital_subsemiring_class S R] : non_unital_semiring s := subtype.coe_injective.non_unital_semiring coe rfl (by simp) (λ _ _, rfl) (λ _ _, rfl) /-- A non-unital subsemiring of a `non_unital_comm_semiring` is a `non_unital_comm_semiring`. -/ instance to_non_unital_comm_semiring {R} [non_unital_comm_semiring R] [set_like S R] [non_unital_subsemiring_class S R] : non_unital_comm_semiring s := subtype.coe_injective.non_unital_comm_semiring coe rfl (by simp) (λ _ _, rfl) (λ _ _, rfl) /-! Note: currently, there are no ordered versions of non-unital rings. -/ end non_unital_subsemiring_class variables [non_unital_non_assoc_semiring S] [non_unital_non_assoc_semiring T] set_option old_structure_cmd true /-- A non-unital subsemiring of a non-unital semiring `R` is a subset `s` that is both an additive submonoid and a semigroup. -/ structure non_unital_subsemiring (R : Type u) [non_unital_non_assoc_semiring R] extends add_submonoid R, subsemigroup R /-- Reinterpret a `non_unital_subsemiring` as a `subsemigroup`. -/ add_decl_doc non_unital_subsemiring.to_subsemigroup /-- Reinterpret a `non_unital_subsemiring` as an `add_submonoid`. -/ add_decl_doc non_unital_subsemiring.to_add_submonoid namespace non_unital_subsemiring instance : set_like (non_unital_subsemiring R) R := { coe := non_unital_subsemiring.carrier, coe_injective' := λ p q h, by cases p; cases q; congr' } instance : non_unital_subsemiring_class (non_unital_subsemiring R) R := { zero_mem := zero_mem', add_mem := add_mem', mul_mem := mul_mem' } @[simp] lemma mem_carrier {s : non_unital_subsemiring R} {x : R} : x ∈ s.carrier ↔ x ∈ s := iff.rfl /-- Two non-unital subsemirings are equal if they have the same elements. -/ @[ext] theorem ext {S T : non_unital_subsemiring R} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h /-- Copy of a non-unital subsemiring with a new `carrier` equal to the old one. Useful to fix definitional equalities.-/ protected def copy (S : non_unital_subsemiring R) (s : set R) (hs : s = ↑S) : non_unital_subsemiring R := { carrier := s, ..S.to_add_submonoid.copy s hs, ..S.to_subsemigroup.copy s hs } @[simp] lemma coe_copy (S : non_unital_subsemiring R) (s : set R) (hs : s = ↑S) : (S.copy s hs : set R) = s := rfl lemma copy_eq (S : non_unital_subsemiring R) (s : set R) (hs : s = ↑S) : S.copy s hs = S := set_like.coe_injective hs lemma to_subsemigroup_injective : function.injective (to_subsemigroup : non_unital_subsemiring R → subsemigroup R) | r s h := ext (set_like.ext_iff.mp h : _) @[mono] lemma to_subsemigroup_strict_mono : strict_mono (to_subsemigroup : non_unital_subsemiring R → subsemigroup R) := λ _ _, id @[mono] lemma to_subsemigroup_mono : monotone (to_subsemigroup : non_unital_subsemiring R → subsemigroup R) := to_subsemigroup_strict_mono.monotone lemma to_add_submonoid_injective : function.injective (to_add_submonoid : non_unital_subsemiring R → add_submonoid R) | r s h := ext (set_like.ext_iff.mp h : _) @[mono] lemma to_add_submonoid_strict_mono : strict_mono (to_add_submonoid : non_unital_subsemiring R → add_submonoid R) := λ _ _, id @[mono] lemma to_add_submonoid_mono : monotone (to_add_submonoid : non_unital_subsemiring R → add_submonoid R) := to_add_submonoid_strict_mono.monotone /-- Construct a `non_unital_subsemiring R` from a set `s`, a subsemigroup `sg`, and an additive submonoid `sa` such that `x ∈ s ↔ x ∈ sg ↔ x ∈ sa`. -/ protected def mk' (s : set R) (sg : subsemigroup R) (hg : ↑sg = s) (sa : add_submonoid R) (ha : ↑sa = s) : non_unital_subsemiring R := { carrier := s, zero_mem' := ha ▸ sa.zero_mem, add_mem' := λ x y, by simpa only [← ha] using sa.add_mem, mul_mem' := λ x y, by simpa only [← hg] using sg.mul_mem } @[simp] lemma coe_mk' {s : set R} {sg : subsemigroup R} (hg : ↑sg = s) {sa : add_submonoid R} (ha : ↑sa = s) : (non_unital_subsemiring.mk' s sg hg sa ha : set R) = s := rfl @[simp] lemma mem_mk' {s : set R} {sg : subsemigroup R} (hg : ↑sg = s) {sa : add_submonoid R} (ha : ↑sa = s) {x : R} : x ∈ non_unital_subsemiring.mk' s sg hg sa ha ↔ x ∈ s := iff.rfl @[simp] lemma mk'_to_subsemigroup {s : set R} {sg : subsemigroup R} (hg : ↑sg = s) {sa : add_submonoid R} (ha : ↑sa = s) : (non_unital_subsemiring.mk' s sg hg sa ha).to_subsemigroup = sg := set_like.coe_injective hg.symm @[simp] lemma mk'_to_add_submonoid {s : set R} {sg : subsemigroup R} (hg : ↑sg = s) {sa : add_submonoid R} (ha : ↑sa =s) : (non_unital_subsemiring.mk' s sg hg sa ha).to_add_submonoid = sa := set_like.coe_injective ha.symm end non_unital_subsemiring namespace non_unital_subsemiring variables {F G : Type*} [non_unital_ring_hom_class F R S] [non_unital_ring_hom_class G S T] (s : non_unital_subsemiring R) @[simp, norm_cast] lemma coe_zero : ((0 : s) : R) = (0 : R) := rfl @[simp, norm_cast] lemma coe_add (x y : s) : ((x + y : s) : R) = (x + y : R) := rfl @[simp, norm_cast] lemma coe_mul (x y : s) : ((x * y : s) : R) = (x * y : R) := rfl /-! Note: currently, there are no ordered versions of non-unital rings. -/ @[simp] lemma mem_to_subsemigroup {s : non_unital_subsemiring R} {x : R} : x ∈ s.to_subsemigroup ↔ x ∈ s := iff.rfl @[simp] lemma coe_to_subsemigroup (s : non_unital_subsemiring R) : (s.to_subsemigroup : set R) = s := rfl @[simp] lemma mem_to_add_submonoid {s : non_unital_subsemiring R} {x : R} : x ∈ s.to_add_submonoid ↔ x ∈ s := iff.rfl @[simp] lemma coe_to_add_submonoid (s : non_unital_subsemiring R) : (s.to_add_submonoid : set R) = s := rfl /-- The non-unital subsemiring `R` of the non-unital semiring `R`. -/ instance : has_top (non_unital_subsemiring R) := ⟨{ .. (⊤ : subsemigroup R), .. (⊤ : add_submonoid R) }⟩ @[simp] lemma mem_top (x : R) : x ∈ (⊤ : non_unital_subsemiring R) := set.mem_univ x @[simp] lemma coe_top : ((⊤ : non_unital_subsemiring R) : set R) = set.univ := rfl /-- The preimage of a non-unital subsemiring along a non-unital ring homomorphism is a non-unital subsemiring. -/ def comap (f : F) (s : non_unital_subsemiring S) : non_unital_subsemiring R := { carrier := f ⁻¹' s, .. s.to_subsemigroup.comap (f : mul_hom R S), .. s.to_add_submonoid.comap (f : R →+ S) } @[simp] lemma coe_comap (s : non_unital_subsemiring S) (f : F) : (s.comap f : set R) = f ⁻¹' s := rfl @[simp] lemma mem_comap {s : non_unital_subsemiring S} {f : F} {x : R} : x ∈ s.comap f ↔ f x ∈ s := iff.rfl -- this has some nasty coercions, how to deal with it? lemma comap_comap (s : non_unital_subsemiring T) (g : G) (f : F) : ((s.comap g : non_unital_subsemiring S).comap f : non_unital_subsemiring R) = s.comap ((g : S →ₙ+* T).comp (f : R →ₙ+* S)) := rfl /-- The image of a non-unital subsemiring along a ring homomorphism is a non-unital subsemiring. -/ def map (f : F) (s : non_unital_subsemiring R) : non_unital_subsemiring S := { carrier := f '' s, .. s.to_subsemigroup.map (f : R →ₙ* S), .. s.to_add_submonoid.map (f : R →+ S) } @[simp] lemma coe_map (f : F) (s : non_unital_subsemiring R) : (s.map f : set S) = f '' s := rfl @[simp] lemma mem_map {f : F} {s : non_unital_subsemiring R} {y : S} : y ∈ s.map f ↔ ∃ x ∈ s, f x = y := set.mem_image_iff_bex @[simp] lemma map_id : s.map (non_unital_ring_hom.id R) = s := set_like.coe_injective $ set.image_id _ -- unavoidable coercions? lemma map_map (g : G) (f : F) : (s.map (f : R →ₙ+* S)).map (g : S →ₙ+* T) = s.map ((g : S →ₙ+* T).comp (f : R →ₙ+* S)) := set_like.coe_injective $ set.image_image _ _ _ lemma map_le_iff_le_comap {f : F} {s : non_unital_subsemiring R} {t : non_unital_subsemiring S} : s.map f ≤ t ↔ s ≤ t.comap f := set.image_subset_iff lemma gc_map_comap (f : F) : @galois_connection (non_unital_subsemiring R) (non_unital_subsemiring S) _ _ (map f) (comap f) := λ S T, map_le_iff_le_comap /-- A non-unital subsemiring is isomorphic to its image under an injective function -/ noncomputable def equiv_map_of_injective (f : F) (hf : function.injective (f : R → S)) : s ≃+* s.map f := { map_mul' := λ _ _, subtype.ext (map_mul f _ _), map_add' := λ _ _, subtype.ext (map_add f _ _), ..equiv.set.image f s hf } @[simp] lemma coe_equiv_map_of_injective_apply (f : F) (hf : function.injective f) (x : s) : (equiv_map_of_injective s f hf x : S) = f x := rfl end non_unital_subsemiring namespace non_unital_ring_hom open non_unital_subsemiring variables {F G : Type*} [non_unital_ring_hom_class F R S] [non_unital_ring_hom_class G S T] (f : F) (g : G) /-- The range of a non-unital ring homomorphism is a non-unital subsemiring. See note [range copy pattern]. -/ def srange : non_unital_subsemiring S := ((⊤ : non_unital_subsemiring R).map (f : R →ₙ+* S)).copy (set.range f) set.image_univ.symm @[simp] lemma coe_srange : (@srange R S _ _ _ _ f : set S) = set.range f := rfl @[simp] lemma mem_srange {f : F} {y : S} : y ∈ (@srange R S _ _ _ _ f) ↔ ∃ x, f x = y := iff.rfl lemma srange_eq_map : @srange R S _ _ _ _ f = (⊤ : non_unital_subsemiring R).map f := by { ext, simp } lemma mem_srange_self (f : F) (x : R) : f x ∈ @srange R S _ _ _ _ f := mem_srange.mpr ⟨x, rfl⟩ lemma map_srange (g : S →ₙ+* T) (f : R →ₙ+* S) : map g (srange f) = srange (g.comp f) := by simpa only [srange_eq_map] using (⊤ : non_unital_subsemiring R).map_map g f /-- The range of a morphism of non-unital semirings is finite if the domain is a finite. -/ instance finite_srange [finite R] (f : F) : finite (srange f : non_unital_subsemiring S) := (set.finite_range f).to_subtype end non_unital_ring_hom namespace non_unital_subsemiring -- should we define this as the range of the zero homomorphism? instance : has_bot (non_unital_subsemiring R) := ⟨{ carrier := {0}, add_mem' := λ _ _ _ _, by simp * at *, zero_mem' := set.mem_singleton 0, mul_mem' := λ _ _ _ _, by simp * at *}⟩ instance : inhabited (non_unital_subsemiring R) := ⟨⊥⟩ lemma coe_bot : ((⊥ : non_unital_subsemiring R) : set R) = {0} := rfl lemma mem_bot {x : R} : x ∈ (⊥ : non_unital_subsemiring R) ↔ x = 0 := set.mem_singleton_iff /-- The inf of two non-unital subsemirings is their intersection. -/ instance : has_inf (non_unital_subsemiring R) := ⟨λ s t, { carrier := s ∩ t, .. s.to_subsemigroup ⊓ t.to_subsemigroup, .. s.to_add_submonoid ⊓ t.to_add_submonoid }⟩ @[simp] lemma coe_inf (p p' : non_unital_subsemiring R) : ((p ⊓ p' : non_unital_subsemiring R) : set R) = p ∩ p' := rfl @[simp] lemma mem_inf {p p' : non_unital_subsemiring R} {x : R} :x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl instance : has_Inf (non_unital_subsemiring R) := ⟨λ s, non_unital_subsemiring.mk' (⋂ t ∈ s, ↑t) (⨅ t ∈ s, non_unital_subsemiring.to_subsemigroup t) (by simp) (⨅ t ∈ s, non_unital_subsemiring.to_add_submonoid t) (by simp)⟩ @[simp, norm_cast] lemma coe_Inf (S : set (non_unital_subsemiring R)) : ((Inf S : non_unital_subsemiring R) : set R) = ⋂ s ∈ S, ↑s := rfl lemma mem_Inf {S : set (non_unital_subsemiring R)} {x : R} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_Inter₂ @[simp] lemma Inf_to_subsemigroup (s : set (non_unital_subsemiring R)) : (Inf s).to_subsemigroup = ⨅ t ∈ s, non_unital_subsemiring.to_subsemigroup t := mk'_to_subsemigroup _ _ @[simp] lemma Inf_to_add_submonoid (s : set (non_unital_subsemiring R)) : (Inf s).to_add_submonoid = ⨅ t ∈ s, non_unital_subsemiring.to_add_submonoid t := mk'_to_add_submonoid _ _ /-- Non-unital subsemirings of a non-unital semiring form a complete lattice. -/ instance : complete_lattice (non_unital_subsemiring R) := { bot := (⊥), bot_le := λ s x hx, (mem_bot.mp hx).symm ▸ zero_mem s, top := (⊤), le_top := λ s x hx, trivial, inf := (⊓), inf_le_left := λ s t x, and.left, inf_le_right := λ s t x, and.right, le_inf := λ s t₁ t₂ h₁ h₂ x hx, ⟨h₁ hx, h₂ hx⟩, .. complete_lattice_of_Inf (non_unital_subsemiring R) (λ s, is_glb.of_image (λ s t, show (s : set R) ≤ t ↔ s ≤ t, from set_like.coe_subset_coe) is_glb_binfi)} lemma eq_top_iff' (A : non_unital_subsemiring R) : A = ⊤ ↔ ∀ x : R, x ∈ A := eq_top_iff.trans ⟨λ h m, h $ mem_top m, λ h m _, h m⟩ section center /-- The center of a semiring `R` is the set of elements that commute with everything in `R` -/ def center (R) [non_unital_semiring R] : non_unital_subsemiring R := { carrier := set.center R, zero_mem' := set.zero_mem_center R, add_mem' := λ a b, set.add_mem_center, .. subsemigroup.center R } lemma coe_center (R) [non_unital_semiring R] : ↑(center R) = set.center R := rfl @[simp] lemma center_to_subsemigroup (R) [non_unital_semiring R] : (center R).to_subsemigroup = subsemigroup.center R := rfl lemma mem_center_iff {R} [non_unital_semiring R] {z : R} : z ∈ center R ↔ ∀ g, g * z = z * g := iff.rfl instance decidable_mem_center {R} [non_unital_semiring R] [decidable_eq R] [fintype R] : decidable_pred (∈ center R) := λ _, decidable_of_iff' _ mem_center_iff @[simp] lemma center_eq_top (R) [non_unital_comm_semiring R] : center R = ⊤ := set_like.coe_injective (set.center_eq_univ R) /-- The center is commutative. -/ instance {R} [non_unital_semiring R] : non_unital_comm_semiring (center R) := { ..subsemigroup.center.comm_semigroup, ..(non_unital_subsemiring_class.to_non_unital_semiring (center R)) } end center section centralizer /-- The centralizer of a set as non-unital subsemiring. -/ def centralizer {R} [non_unital_semiring R] (s : set R) : non_unital_subsemiring R := { carrier := s.centralizer, zero_mem' := set.zero_mem_centralizer _, add_mem' := λ x y hx hy, set.add_mem_centralizer hx hy, ..subsemigroup.centralizer s } @[simp, norm_cast] lemma coe_centralizer {R} [non_unital_semiring R] (s : set R) : (centralizer s : set R) = s.centralizer := rfl lemma centralizer_to_subsemigroup {R} [non_unital_semiring R] (s : set R) : (centralizer s).to_subsemigroup = subsemigroup.centralizer s := rfl lemma mem_centralizer_iff {R} [non_unital_semiring R] {s : set R} {z : R} : z ∈ centralizer s ↔ ∀ g ∈ s, g * z = z * g := iff.rfl lemma centralizer_le {R} [non_unital_semiring R] (s t : set R) (h : s ⊆ t) : centralizer t ≤ centralizer s := set.centralizer_subset h @[simp] lemma centralizer_univ {R} [non_unital_semiring R] : centralizer set.univ = center R := set_like.ext' (set.centralizer_univ R) end centralizer /-- The `non_unital_subsemiring` generated by a set. -/ def closure (s : set R) : non_unital_subsemiring R := Inf {S | s ⊆ S} lemma mem_closure {x : R} {s : set R} : x ∈ closure s ↔ ∀ S : non_unital_subsemiring R, s ⊆ S → x ∈ S := mem_Inf /-- The non-unital subsemiring generated by a set includes the set. -/ @[simp] lemma subset_closure {s : set R} : s ⊆ closure s := λ x hx, mem_closure.2 $ λ S hS, hS hx lemma not_mem_of_not_mem_closure {s : set R} {P : R} (hP : P ∉ closure s) : P ∉ s := λ h, hP (subset_closure h) /-- A non-unital subsemiring `S` includes `closure s` if and only if it includes `s`. -/ @[simp] lemma closure_le {s : set R} {t : non_unital_subsemiring R} : closure s ≤ t ↔ s ⊆ t := ⟨set.subset.trans subset_closure, λ h, Inf_le h⟩ /-- Subsemiring closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`. -/ lemma closure_mono ⦃s t : set R⦄ (h : s ⊆ t) : closure s ≤ closure t := closure_le.2 $ set.subset.trans h subset_closure lemma closure_eq_of_le {s : set R} {t : non_unital_subsemiring R} (h₁ : s ⊆ t) (h₂ : t ≤ closure s) : closure s = t := le_antisymm (closure_le.2 h₁) h₂ lemma mem_map_equiv {f : R ≃+* S} {K : non_unital_subsemiring R} {x : S} : x ∈ K.map (f : R →ₙ+* S) ↔ f.symm x ∈ K := @set.mem_image_equiv _ _ ↑K f.to_equiv x lemma map_equiv_eq_comap_symm (f : R ≃+* S) (K : non_unital_subsemiring R) : K.map (f : R →ₙ+* S) = K.comap f.symm := set_like.coe_injective (f.to_equiv.image_eq_preimage K) lemma comap_equiv_eq_map_symm (f : R ≃+* S) (K : non_unital_subsemiring S) : K.comap (f : R →ₙ+* S) = K.map f.symm := (map_equiv_eq_comap_symm f.symm K).symm end non_unital_subsemiring namespace subsemigroup /-- The additive closure of a non-unital subsemigroup is a non-unital subsemiring. -/ def non_unital_subsemiring_closure (M : subsemigroup R) : non_unital_subsemiring R := { mul_mem' := λ x y, mul_mem_class.mul_mem_add_closure, ..add_submonoid.closure (M : set R)} lemma non_unital_subsemiring_closure_coe : (M.non_unital_subsemiring_closure : set R) = add_submonoid.closure (M : set R) := rfl lemma non_unital_subsemiring_closure_to_add_submonoid : M.non_unital_subsemiring_closure.to_add_submonoid = add_submonoid.closure (M : set R) := rfl /-- The `non_unital_subsemiring` generated by a multiplicative subsemigroup coincides with the `non_unital_subsemiring.closure` of the subsemigroup itself . -/ lemma non_unital_subsemiring_closure_eq_closure : M.non_unital_subsemiring_closure = non_unital_subsemiring.closure (M : set R) := begin ext, refine ⟨λ hx, _, λ hx, (non_unital_subsemiring.mem_closure.mp hx) M.non_unital_subsemiring_closure (λ s sM, _)⟩; rintros - ⟨H1, rfl⟩; rintros - ⟨H2, rfl⟩, { exact add_submonoid.mem_closure.mp hx H1.to_add_submonoid H2 }, { exact H2 sM } end end subsemigroup namespace non_unital_subsemiring @[simp] lemma closure_subsemigroup_closure (s : set R) : closure ↑(subsemigroup.closure s) = closure s := le_antisymm (closure_le.mpr (λ y hy, (subsemigroup.mem_closure.mp hy) (closure s).to_subsemigroup subset_closure)) (closure_mono (subsemigroup.subset_closure)) /-- The elements of the non-unital subsemiring closure of `M` are exactly the elements of the additive closure of a multiplicative subsemigroup `M`. -/ lemma coe_closure_eq (s : set R) : (closure s : set R) = add_submonoid.closure (subsemigroup.closure s : set R) := by simp [← subsemigroup.non_unital_subsemiring_closure_to_add_submonoid, subsemigroup.non_unital_subsemiring_closure_eq_closure] lemma mem_closure_iff {s : set R} {x} : x ∈ closure s ↔ x ∈ add_submonoid.closure (subsemigroup.closure s : set R) := set.ext_iff.mp (coe_closure_eq s) x @[simp] lemma closure_add_submonoid_closure {s : set R} : closure ↑(add_submonoid.closure s) = closure s := begin ext x, refine ⟨λ hx, _, λ hx, closure_mono add_submonoid.subset_closure hx⟩, rintros - ⟨H, rfl⟩, rintros - ⟨J, rfl⟩, refine (add_submonoid.mem_closure.mp (mem_closure_iff.mp hx)) H.to_add_submonoid (λ y hy, _), refine (subsemigroup.mem_closure.mp hy) H.to_subsemigroup (λ z hz, _), exact (add_submonoid.mem_closure.mp hz) H.to_add_submonoid (λ w hw, J hw), end /-- An induction principle for closure membership. If `p` holds for `0`, `1`, and all elements of `s`, and is preserved under addition and multiplication, then `p` holds for all elements of the closure of `s`. -/ @[elab_as_eliminator] lemma closure_induction {s : set R} {p : R → Prop} {x} (h : x ∈ closure s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (Hadd : ∀ x y, p x → p y → p (x + y)) (Hmul : ∀ x y, p x → p y → p (x * y)) : p x := (@closure_le _ _ _ ⟨p, Hadd, H0, Hmul⟩).2 Hs h /-- An induction principle for closure membership for predicates with two arguments. -/ @[elab_as_eliminator] lemma closure_induction₂ {s : set R} {p : R → R → Prop} {x} {y : R} (hx : x ∈ closure s) (hy : y ∈ closure s) (Hs : ∀ (x ∈ s) (y ∈ s), p x y) (H0_left : ∀ x, p 0 x) (H0_right : ∀ x, p x 0) (Hadd_left : ∀ x₁ x₂ y, p x₁ y → p x₂ y → p (x₁ + x₂) y) (Hadd_right : ∀ x y₁ y₂, p x y₁ → p x y₂ → p x (y₁ + y₂)) (Hmul_left : ∀ x₁ x₂ y, p x₁ y → p x₂ y → p (x₁ * x₂) y) (Hmul_right : ∀ x y₁ y₂, p x y₁ → p x y₂ → p x (y₁ * y₂)) : p x y := closure_induction hx (λ x₁ x₁s, closure_induction hy (Hs x₁ x₁s) (H0_right x₁) (Hadd_right x₁) (Hmul_right x₁)) (H0_left y) (λ z z', Hadd_left z z' y) (λ z z', Hmul_left z z' y) variable (R) /-- `closure` forms a Galois insertion with the coercion to set. -/ protected def gi : galois_insertion (@closure R _) coe := { choice := λ s _, closure s, gc := λ s t, closure_le, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variables {R} {F : Type*} [non_unital_ring_hom_class F R S] /-- Closure of a non-unital subsemiring `S` equals `S`. -/ lemma closure_eq (s : non_unital_subsemiring R) : closure (s : set R) = s := (non_unital_subsemiring.gi R).l_u_eq s @[simp] lemma closure_empty : closure (∅ : set R) = ⊥ := (non_unital_subsemiring.gi R).gc.l_bot @[simp] lemma closure_univ : closure (set.univ : set R) = ⊤ := @coe_top R _ ▸ closure_eq ⊤ lemma closure_union (s t : set R) : closure (s ∪ t) = closure s ⊔ closure t := (non_unital_subsemiring.gi R).gc.l_sup lemma closure_Union {ι} (s : ι → set R) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (non_unital_subsemiring.gi R).gc.l_supr lemma closure_sUnion (s : set (set R)) : closure (⋃₀ s) = ⨆ t ∈ s, closure t := (non_unital_subsemiring.gi R).gc.l_Sup lemma map_sup (s t : non_unital_subsemiring R) (f : F) : (map f (s ⊔ t) : non_unital_subsemiring S) = map f s ⊔ map f t := @galois_connection.l_sup _ _ s t _ _ _ _ (gc_map_comap f) lemma map_supr {ι : Sort*} (f : F) (s : ι → non_unital_subsemiring R) : (map f (supr s) : non_unital_subsemiring S) = ⨆ i, map f (s i) := @galois_connection.l_supr _ _ _ _ _ _ _ (gc_map_comap f) s lemma comap_inf (s t : non_unital_subsemiring S) (f : F) : (comap f (s ⊓ t) : non_unital_subsemiring R) = comap f s ⊓ comap f t := @galois_connection.u_inf _ _ s t _ _ _ _ (gc_map_comap f) lemma comap_infi {ι : Sort*} (f : F) (s : ι → non_unital_subsemiring S) : (comap f (infi s) : non_unital_subsemiring R) = ⨅ i, comap f (s i) := @galois_connection.u_infi _ _ _ _ _ _ _ (gc_map_comap f) s @[simp] lemma map_bot (f : F) : map f (⊥ : non_unital_subsemiring R) = (⊥ : non_unital_subsemiring S) := (gc_map_comap f).l_bot @[simp] lemma comap_top (f : F) : comap f (⊤ : non_unital_subsemiring S) = (⊤ : non_unital_subsemiring R) := (gc_map_comap f).u_top /-- Given `non_unital_subsemiring`s `s`, `t` of semirings `R`, `S` respectively, `s.prod t` is `s × t` as a non-unital subsemiring of `R × S`. -/ def prod (s : non_unital_subsemiring R) (t : non_unital_subsemiring S) : non_unital_subsemiring (R × S) := { carrier := (s : set R) ×ˢ (t : set S), .. s.to_subsemigroup.prod t.to_subsemigroup, .. s.to_add_submonoid.prod t.to_add_submonoid} @[norm_cast] lemma coe_prod (s : non_unital_subsemiring R) (t : non_unital_subsemiring S) : (s.prod t : set (R × S)) = (s : set R) ×ˢ (t : set S) := rfl lemma mem_prod {s : non_unital_subsemiring R} {t : non_unital_subsemiring S} {p : R × S} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[mono] lemma prod_mono ⦃s₁ s₂ : non_unital_subsemiring R⦄ (hs : s₁ ≤ s₂) ⦃t₁ t₂ : non_unital_subsemiring S⦄ (ht : t₁ ≤ t₂) : s₁.prod t₁ ≤ s₂.prod t₂ := set.prod_mono hs ht lemma prod_mono_right (s : non_unital_subsemiring R) : monotone (λ t : non_unital_subsemiring S, s.prod t) := prod_mono (le_refl s) lemma prod_mono_left (t : non_unital_subsemiring S) : monotone (λ s : non_unital_subsemiring R, s.prod t) := λ s₁ s₂ hs, prod_mono hs (le_refl t) lemma prod_top (s : non_unital_subsemiring R) : s.prod (⊤ : non_unital_subsemiring S) = s.comap (non_unital_ring_hom.fst R S) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] lemma top_prod (s : non_unital_subsemiring S) : (⊤ : non_unital_subsemiring R).prod s = s.comap (non_unital_ring_hom.snd R S) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp] lemma top_prod_top : (⊤ : non_unital_subsemiring R).prod (⊤ : non_unital_subsemiring S) = ⊤ := (top_prod _).trans $ comap_top _ /-- Product of non-unital subsemirings is isomorphic to their product as semigroups. -/ def prod_equiv (s : non_unital_subsemiring R) (t : non_unital_subsemiring S) : s.prod t ≃+* s × t := { map_mul' := λ x y, rfl, map_add' := λ x y, rfl, .. equiv.set.prod ↑s ↑t } lemma mem_supr_of_directed {ι} [hι : nonempty ι] {S : ι → non_unital_subsemiring R} (hS : directed (≤) S) {x : R} : x ∈ (⨆ i, S i) ↔ ∃ i, x ∈ S i := begin refine ⟨_, λ ⟨i, hi⟩, (set_like.le_def.1 $ le_supr S i) hi⟩, let U : non_unital_subsemiring R := non_unital_subsemiring.mk' (⋃ i, (S i : set R)) (⨆ i, (S i).to_subsemigroup) (subsemigroup.coe_supr_of_directed $ hS.mono_comp _ (λ _ _, id)) (⨆ i, (S i).to_add_submonoid) (add_submonoid.coe_supr_of_directed $ hS.mono_comp _ (λ _ _, id)), suffices : (⨆ i, S i) ≤ U, by simpa using @this x, exact supr_le (λ i x hx, set.mem_Union.2 ⟨i, hx⟩), end lemma coe_supr_of_directed {ι} [hι : nonempty ι] {S : ι → non_unital_subsemiring R} (hS : directed (≤) S) : ((⨆ i, S i : non_unital_subsemiring R) : set R) = ⋃ i, ↑(S i) := set.ext $ λ x, by simp [mem_supr_of_directed hS] lemma mem_Sup_of_directed_on {S : set (non_unital_subsemiring R)} (Sne : S.nonempty) (hS : directed_on (≤) S) {x : R} : x ∈ Sup S ↔ ∃ s ∈ S, x ∈ s := begin haveI : nonempty S := Sne.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed hS.directed_coe, set_coe.exists, subtype.coe_mk] end lemma coe_Sup_of_directed_on {S : set (non_unital_subsemiring R)} (Sne : S.nonempty) (hS : directed_on (≤) S) : (↑(Sup S) : set R) = ⋃ s ∈ S, ↑s := set.ext $ λ x, by simp [mem_Sup_of_directed_on Sne hS] end non_unital_subsemiring namespace non_unital_ring_hom variables {F : Type*} [non_unital_non_assoc_semiring T] [non_unital_ring_hom_class F R S] {s : non_unital_subsemiring R} open non_unital_subsemiring_class non_unital_subsemiring /-- Restriction of a non-unital ring homomorphism to a non-unital subsemiring of the codomain. -/ def cod_restrict (f : F) (s : non_unital_subsemiring S) (h : ∀ x, f x ∈ s) : R →ₙ+* s := { to_fun := λ n, ⟨f n, h n⟩, .. (f : R →ₙ* S).cod_restrict s.to_subsemigroup h, .. (f : R →+ S).cod_restrict s.to_add_submonoid h } /-- Restriction of a non-unital ring homomorphism to its range interpreted as a non-unital subsemiring. This is the bundled version of `set.range_factorization`. -/ def srange_restrict (f : F) : R →ₙ+* (srange f : non_unital_subsemiring S) := cod_restrict f (srange f) (mem_srange_self f) @[simp] lemma coe_srange_restrict (f : F) (x : R) : (srange_restrict f x : S) = f x := rfl lemma srange_restrict_surjective (f : F) : function.surjective (srange_restrict f : R → (srange f : non_unital_subsemiring S)) := λ ⟨y, hy⟩, let ⟨x, hx⟩ := mem_srange.mp hy in ⟨x, subtype.ext hx⟩ lemma srange_top_iff_surjective {f : F} : srange f = (⊤ : non_unital_subsemiring S) ↔ function.surjective (f : R → S):= set_like.ext'_iff.trans $ iff.trans (by rw [coe_srange, coe_top]) set.range_iff_surjective /-- The range of a surjective non-unital ring homomorphism is the whole of the codomain. -/ lemma srange_top_of_surjective (f : F) (hf : function.surjective (f : R → S)) : srange f = (⊤ : non_unital_subsemiring S) := srange_top_iff_surjective.2 hf /-- The non-unital subsemiring of elements `x : R` such that `f x = g x` -/ def eq_slocus (f g : F) : non_unital_subsemiring R := { carrier := {x | f x = g x}, .. (f : R →ₙ* S).eq_mlocus (g : R →ₙ* S), .. (f : R →+ S).eq_mlocus g } /-- If two non-unital ring homomorphisms are equal on a set, then they are equal on its non-unital subsemiring closure. -/ lemma eq_on_sclosure {f g : F} {s : set R} (h : set.eq_on (f : R → S) (g : R → S) s) : set.eq_on f g (closure s) := show closure s ≤ eq_slocus f g, from closure_le.2 h lemma eq_of_eq_on_stop {f g : F} (h : set.eq_on (f : R → S) (g : R → S) (⊤ : non_unital_subsemiring R)) : f = g := fun_like.ext _ _ (λ x, h trivial) lemma eq_of_eq_on_sdense {s : set R} (hs : closure s = ⊤) {f g : F} (h : s.eq_on (f : R → S) (g : R → S)) : f = g := eq_of_eq_on_stop $ hs ▸ eq_on_sclosure h lemma sclosure_preimage_le (f : F) (s : set S) : closure ((f : R → S) ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, set_like.mem_coe.2 $ mem_comap.2 $ subset_closure hx /-- The image under a ring homomorphism of the subsemiring generated by a set equals the subsemiring generated by the image of the set. -/ lemma map_sclosure (f : F) (s : set R) : (closure s).map f = closure ((f : R → S) '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _) (sclosure_preimage_le _ _)) (closure_le.2 $ set.image_subset _ subset_closure) end non_unital_ring_hom namespace non_unital_subsemiring open non_unital_ring_hom non_unital_subsemiring_class /-- The non-unital ring homomorphism associated to an inclusion of non-unital subsemirings. -/ def inclusion {S T : non_unital_subsemiring R} (h : S ≤ T) : S →ₙ+* T := cod_restrict (subtype S) _ (λ x, h x.2) @[simp] lemma srange_subtype (s : non_unital_subsemiring R) : (subtype s).srange = s := set_like.coe_injective $ (coe_srange _).trans subtype.range_coe @[simp] lemma range_fst : (fst R S).srange = ⊤ := non_unital_ring_hom.srange_top_of_surjective (fst R S) prod.fst_surjective @[simp] lemma range_snd : (snd R S).srange = ⊤ := non_unital_ring_hom.srange_top_of_surjective (snd R S) $ prod.snd_surjective end non_unital_subsemiring namespace ring_equiv open non_unital_ring_hom non_unital_subsemiring_class variables {s t : non_unital_subsemiring R} variables {F : Type*} [non_unital_ring_hom_class F R S] /-- Makes the identity isomorphism from a proof two non-unital subsemirings of a multiplicative monoid are equal. -/ def non_unital_subsemiring_congr (h : s = t) : s ≃+* t := { map_mul' := λ _ _, rfl, map_add' := λ _ _, rfl, ..equiv.set_congr $ congr_arg _ h } /-- Restrict a non-unital ring homomorphism with a left inverse to a ring isomorphism to its `non_unital_ring_hom.srange`. -/ def sof_left_inverse' {g : S → R} {f : F} (h : function.left_inverse g f) : R ≃+* srange f := { to_fun := srange_restrict f, inv_fun := λ x, g (subtype (srange f) x), left_inv := h, right_inv := λ x, subtype.ext $ let ⟨x', hx'⟩ := non_unital_ring_hom.mem_srange.mp x.prop in show f (g x) = x, by rw [←hx', h x'], ..(srange_restrict f) } @[simp] lemma sof_left_inverse'_apply {g : S → R} {f : F} (h : function.left_inverse g f) (x : R) : ↑(sof_left_inverse' h x) = f x := rfl @[simp] lemma sof_left_inverse'_symm_apply {g : S → R} {f : F} (h : function.left_inverse g f) (x : srange f) : (sof_left_inverse' h).symm x = g x := rfl /-- Given an equivalence `e : R ≃+* S` of non-unital semirings and a non-unital subsemiring `s` of `R`, `non_unital_subsemiring_map e s` is the induced equivalence between `s` and `s.map e` -/ @[simps] def non_unital_subsemiring_map (e : R ≃+* S) (s : non_unital_subsemiring R) : s ≃+* non_unital_subsemiring.map e.to_non_unital_ring_hom s := { ..e.to_add_equiv.add_submonoid_map s.to_add_submonoid, ..e.to_mul_equiv.subsemigroup_map s.to_subsemigroup } end ring_equiv
d3e1c6953726f157ad3f81cd0177928c7c8de984
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Lean/Elab/Match.lean
13f56d0cc0961a178034cf9126b8086dc380f0d2
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
59,020
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.CollectFVars import Lean.Parser.Term import Lean.Meta.Match.MatchPatternAttr import Lean.Meta.Match.Match import Lean.Meta.GeneralizeVars import Lean.Meta.ForEachExpr import Lean.Elab.SyntheticMVars import Lean.Elab.Arg import Lean.Elab.PatternVar import Lean.Elab.AuxDiscr import Lean.Elab.BindersUtil namespace Lean.Elab.Term open Meta open Lean.Parser.Term private def expandSimpleMatch (stx : Syntax) (discr : Term) (lhsVar : Ident) (rhs : Term) (expectedType? : Option Expr) : TermElabM Expr := do let newStx ← `(let $lhsVar := $discr; $rhs) withMacroExpansion stx newStx <| elabTerm newStx expectedType? private def mkUserNameFor (e : Expr) : TermElabM Name := do match e with /- Remark: we use `mkFreshUserName` to make sure we don't add a variable to the local context that can be resolved to `e`. -/ | .fvar fvarId => mkFreshUserName (← fvarId.getUserName) | _ => mkFreshBinderName /-- We treat `@x` as atomic to avoid unnecessary extra local declarations from being inserted into the local context. Recall that `expandMatchAltsIntoMatch` uses `@` modifier. Thus this is kind of discriminant is quite common. Remark: if the discriminat is `Systax.missing`, we abort the elaboration of the `match`-expression. This can happen due to error recovery. Example ``` example : (p ∨ p) → p := fun h => match ``` If we don't abort, the elaborator loops because we will keep trying to expand ``` match ``` into ``` let d := <Syntax.missing>; match ``` Recall that `Syntax.setArg stx i arg` is a no-op when `i` is out-of-bounds. -/ def isAtomicDiscr? (discr : Syntax) : TermElabM (Option Expr) := do match discr with | `($x:ident) => isLocalIdent? x | `(@$x:ident) => isLocalIdent? x | _ => if discr.isMissing then throwAbortTerm else return none -- See expandNonAtomicDiscrs? private def elabAtomicDiscr (discr : Syntax) : TermElabM Expr := do let term := discr[1] match (← isAtomicDiscr? term) with | some e@(Expr.fvar fvarId) => let localDecl ← fvarId.getDecl if !isAuxDiscrName localDecl.userName then addTermInfo term e -- it is not an auxiliary local created by `expandNonAtomicDiscrs?` else instantiateMVars localDecl.value | _ => throwErrorAt discr "unexpected discriminant" structure Discr where expr : Expr /-- `some h` if discriminant is annotated with the `h : ` notation. -/ h? : Option Syntax := none deriving Inhabited structure ElabMatchTypeAndDiscrsResult where discrs : Array Discr matchType : Expr /-- `true` when performing dependent elimination. We use this to decide whether we optimize the "match unit" case. See `isMatchUnit?`. -/ isDep : Bool alts : Array MatchAltView private partial def elabMatchTypeAndDiscrs (discrStxs : Array Syntax) (matchOptMotive : Syntax) (matchAltViews : Array MatchAltView) (expectedType : Expr) : TermElabM ElabMatchTypeAndDiscrsResult := do if matchOptMotive.isNone then elabDiscrs 0 #[] else -- motive := leading_parser atomic ("(" >> nonReservedSymbol "motive" >> " := ") >> termParser >> ")" let matchTypeStx := matchOptMotive[0][3] let matchType ← elabType matchTypeStx let (discrs, isDep) ← elabDiscrsWitMatchType matchType return { discrs := discrs, matchType := matchType, isDep := isDep, alts := matchAltViews } where /-- Easy case: elaborate discriminant when the match-type has been explicitly provided by the user. -/ elabDiscrsWitMatchType (matchType : Expr) : TermElabM (Array Discr × Bool) := do let mut discrs := #[] let mut i := 0 let mut matchType := matchType let mut isDep := false for discrStx in discrStxs do i := i + 1 matchType ← whnf matchType match matchType with | Expr.forallE _ d b _ => let discr ← fullApproxDefEq <| elabTermEnsuringType discrStx[1] d trace[Elab.match] "discr #{i} {discr} : {d}" if b.hasLooseBVars then isDep := true matchType := b.instantiate1 discr discrs := discrs.push { expr := discr } | _ => throwError "invalid motive provided to match-expression, function type with arity #{discrStxs.size} expected" return (discrs, isDep) markIsDep (r : ElabMatchTypeAndDiscrsResult) := { r with isDep := true } /-- Elaborate discriminants inferring the match-type -/ elabDiscrs (i : Nat) (discrs : Array Discr) : TermElabM ElabMatchTypeAndDiscrsResult := do if h : i < discrStxs.size then let discrStx := discrStxs.get ⟨i, h⟩ let discr ← elabAtomicDiscr discrStx let discr ← instantiateMVars discr let userName ← mkUserNameFor discr let h? := if discrStx[0].isNone then none else some discrStx[0][0] let discrs := discrs.push { expr := discr, h? } let mut result ← elabDiscrs (i + 1) discrs let matchTypeBody ← kabstract result.matchType discr if matchTypeBody.hasLooseBVars then result := markIsDep result /- We use `transform (usedLetOnly := true)` to eliminate unnecessary let-expressions. This transformation was added to address issue #1155, and avoid an unnecessary dependency. In issue #1155, `discrType` was of the form `let _discr := OfNat.ofNat ... 0 ?m; ...`, and not removing the unnecessary `let-expr` was introducing an artificial dependency to `?m`. TODO: make sure that even when this kind of artificial dependecy occurs we catch it before sending the term to the kernel. -/ let discrType ← transform (usedLetOnly := true) (← instantiateMVars (← inferType discr)) let matchType := Lean.mkForall userName BinderInfo.default discrType matchTypeBody return { result with matchType } else return { discrs, alts := matchAltViews, isDep := false, matchType := expectedType } def expandMacrosInPatterns (matchAlts : Array MatchAltView) : MacroM (Array MatchAltView) := do matchAlts.mapM fun matchAlt => do let patterns ← matchAlt.patterns.mapM expandMacros pure { matchAlt with patterns := patterns } private def getMatchGeneralizing? : Syntax → Option Bool | `(match (generalizing := true) $[$motive]? $_discrs,* with $_alts:matchAlt*) => some true | `(match (generalizing := false) $[$motive]? $_discrs,* with $_alts:matchAlt*) => some false | _ => none /-- Given `stx` a match-expression, return its alternatives. -/ private def getMatchAlts : Syntax → Array MatchAltView | `(match $[$gen]? $[$motive]? $_discrs,* with $alts:matchAlt*) => alts.filterMap fun alt => match alt with | `(matchAltExpr| | $patterns,* => $rhs) => some { ref := alt, patterns := patterns, rhs := rhs } | _ => none | _ => #[] @[builtinTermElab inaccessible] def elabInaccessible : TermElab := fun stx expectedType? => do let e ← elabTerm stx[1] expectedType? return mkInaccessible e open Lean.Elab.Term.Quotation in @[builtinQuotPrecheck Lean.Parser.Term.match] def precheckMatch : Precheck | `(match $[$discrs:term],* with $[| $[$patss],* => $rhss]*) => do discrs.forM precheck for (pats, rhs) in patss.zip rhss do let vars ← try getPatternsVars pats catch | _ => return -- can happen in case of pattern antiquotations Quotation.withNewLocals (getPatternVarNames vars) <| precheck rhs | _ => throwUnsupportedSyntax /-- We convert the collected `PatternVar`s intro `PatternVarDecl` -/ structure PatternVarDecl where fvarId : FVarId private partial def withPatternVars {α} (pVars : Array PatternVar) (k : Array PatternVarDecl → TermElabM α) : TermElabM α := let rec loop (i : Nat) (decls : Array PatternVarDecl) (userNames : Array Name) := do if h : i < pVars.size then let var := pVars.get ⟨i, h⟩ let type ← mkFreshTypeMVar withLocalDecl var.getId BinderInfo.default type fun x => loop (i+1) (decls.push { fvarId := x.fvarId! }) (userNames.push Name.anonymous) else k decls loop 0 #[] #[] /-! Remark: when performing dependent pattern matching, we often had to write code such as ```lean def Vec.map' (f : α → β) (xs : Vec α n) : Vec β n := match n, xs with | _, nil => nil | _, cons a as => cons (f a) (map' f as) ``` We had to include `n` and the `_`s because the type of `xs` depends on `n`. Moreover, `nil` and `cons a as` have different types. This was quite tedious. So, we have implemented an automatic "discriminant refinement procedure". The procedure is based on the observation that we get a type error whenenver we forget to include `_`s and the indices a discriminant depends on. So, we catch the exception, check whether the type of the discriminant is an indexed family, and add their indices as new discriminants. The current implementation, adds indices as they are found, and does not try to "sort" the new discriminants. If the refinement process fails, we report the original error message. -/ /-- Auxiliary structure for storing an type mismatch exception when processing the pattern #`idx` of some alternative. -/ structure PatternElabException where ex : Exception patternIdx : Nat -- Discriminant that sh pathToIndex : List Nat -- Path to the problematic inductive type index that produced the type mismatch /-- This method is part of the "discriminant refinement" procedure. It in invoked when the type of the `pattern` does not match the expected type. The expected type is based on the motive computed using the `match` discriminants. It tries to compute a path to an index of the discriminant type. For example, suppose the user has written ``` inductive Mem (a : α) : List α → Prop where | head {as} : Mem a (a::as) | tail {as} : Mem a as → Mem a (a'::as) infix:50 " ∈ " => Mem example (a b : Nat) (h : a ∈ [b]) : b = a := match h with | Mem.head => rfl ``` The motive for the match is `a ∈ [b] → b = a`, and get a type mismatch between the type of `Mem.head` and `a ∈ [b]`. This procedure return the path `[2, 1]` to the index `b`. We use it to produce the following refinement ``` example (a b : Nat) (h : a ∈ [b]) : b = a := match b, h with | _, Mem.head => rfl ``` which produces the new motive `(x : Nat) → a ∈ [x] → x = a` After this refinement step, the `match` is elaborated successfully. This method relies on the fact that the dependent pattern matcher compiler solves equations between indices of indexed inductive families. The following kinds of equations are supported by this compiler: - `x = t` - `t = x` - `ctor ... = ctor ...` where `x` is a free variable, `t` is an arbitrary term, and `ctor` is constructor. Our procedure ensures that "information" is not lost, and will *not* succeed in an example such as ``` example (a b : Nat) (f : Nat → Nat) (h : f a ∈ [f b]) : f b = f a := match h with | Mem.head => rfl ``` and will not add `f b` as a new discriminant. We may add an option in the future to enable this more liberal form of refinement. -/ private partial def findDiscrRefinementPath (pattern : Expr) (expected : Expr) : OptionT MetaM (List Nat) := do goType (← instantiateMVars (← inferType pattern)) expected where checkCompatibleApps (t d : Expr) : OptionT MetaM Unit := do guard d.isApp guard <| t.getAppNumArgs == d.getAppNumArgs let tFn := t.getAppFn let dFn := d.getAppFn guard <| tFn.isConst && dFn.isConst guard (← isDefEq tFn dFn) -- Visitor for inductive types goType (t d : Expr) : OptionT MetaM (List Nat) := do let t ← whnf t let d ← whnf d checkCompatibleApps t d matchConstInduct t.getAppFn (fun _ => failure) fun info _ => do let tArgs := t.getAppArgs let dArgs := d.getAppArgs for i in [:info.numParams] do let tArg := tArgs[i]! let dArg := dArgs[i]! unless (← isDefEq tArg dArg) do return i :: (← goType tArg dArg) for i in [info.numParams : tArgs.size] do let tArg := tArgs[i]! let dArg := dArgs[i]! unless (← isDefEq tArg dArg) do return i :: (← goIndex tArg dArg) failure -- Visitor for indexed families goIndex (t d : Expr) : OptionT MetaM (List Nat) := do let t ← whnfD t let d ← whnfD d if t.isFVar || d.isFVar then return [] -- Found refinement path else checkCompatibleApps t d matchConstCtor t.getAppFn (fun _ => failure) fun info _ => do let tArgs := t.getAppArgs let dArgs := d.getAppArgs for i in [:info.numParams] do let tArg := tArgs[i]! let dArg := dArgs[i]! unless (← isDefEq tArg dArg) do failure for i in [info.numParams : tArgs.size] do let tArg := tArgs[i]! let dArg := dArgs[i]! unless (← isDefEq tArg dArg) do return i :: (← goIndex tArg dArg) failure private partial def eraseIndices (type : Expr) : MetaM Expr := do let type' ← whnfD type matchConstInduct type'.getAppFn (fun _ => return type) fun info _ => do let args := type'.getAppArgs let params ← args[:info.numParams].toArray.mapM eraseIndices let result := mkAppN type'.getAppFn params let resultType ← inferType result let (newIndices, _, _) ← forallMetaTelescopeReducing resultType (some (args.size - info.numParams)) return mkAppN result newIndices private def withPatternElabConfig (x : TermElabM α) : TermElabM α := withoutErrToSorry <| withReader (fun ctx => { ctx with inPattern := true }) <| x private def elabPatterns (patternStxs : Array Syntax) (matchType : Expr) : ExceptT PatternElabException TermElabM (Array Expr × Expr) := withReader (fun ctx => { ctx with implicitLambda := false }) do let mut patterns := #[] let mut matchType := matchType for idx in [:patternStxs.size] do let patternStx := patternStxs[idx]! matchType ← whnf matchType match matchType with | Expr.forallE _ d b _ => let pattern ← do let s ← saveState try liftM <| withSynthesize <| withPatternElabConfig <| elabTermEnsuringType patternStx d catch ex : Exception => restoreState s match (← liftM <| commitIfNoErrors? <| withPatternElabConfig do elabTermAndSynthesize patternStx (← eraseIndices d)) with | some pattern => match (← findDiscrRefinementPath pattern d |>.run) with | some path => restoreState s -- Wrap the type mismatch exception for the "discriminant refinement" feature. throwThe PatternElabException { ex := ex, patternIdx := idx, pathToIndex := path } | none => restoreState s; throw ex | none => throw ex matchType := b.instantiate1 pattern patterns := patterns.push pattern | _ => throwError "unexpected match type" return (patterns, matchType) open Meta.Match (Pattern Pattern.var Pattern.inaccessible Pattern.ctor Pattern.as Pattern.val Pattern.arrayLit AltLHS MatcherResult) namespace ToDepElimPattern private def throwInvalidPattern (e : Expr) : MetaM α := throwError "invalid pattern {indentExpr e}" structure State where patternVars : Array Expr := #[] structure Context where /-- When visiting an assigned metavariable, if it has an user-name. We save it here. We want to preserve these user-names when generating new pattern variables. -/ userName : Name := Name.anonymous /-- Pattern variables that were explicitly provided by the user. Recall that implicit parameters and `_` are elaborated as metavariables, and then converted into pattern variables by the `normalize` procedure. -/ explicitPatternVars : Array FVarId := #[] abbrev M := ReaderT Context $ StateRefT State TermElabM /-- Return true iff `e` is an explicit pattern variable provided by the user. -/ def isExplicitPatternVar (e : Expr) : M Bool := do if e.isFVar then return (← read).explicitPatternVars.any (· == e.fvarId!) else return false /-- Helper function for "saving" the user name associated with `mvarId` (if it is not "anonymous") before visiting `x` The auto generalization feature will uses synthetic holes to preserve the name of the free variable included during generalization. For example, if we are generalizing a free variable `bla`, we add the synthetic hole `?bla` for the pattern. We use synthetic hole because we don't know whether `?bla` will become an inaccessible pattern or not. The `withMVar` method makes sure we don't "lose" this name when `isDefEq` perform assignments of the form `?bla := ?m` where `?m` has no user name. This can happen, for example, when the user provides a `_` pattern, or for implicit fields. -/ private def withMVar (mvarId : MVarId) (x : M α) : M α := do let localDecl ← getMVarDecl mvarId if !localDecl.userName.isAnonymous && (← read).userName.isAnonymous then withReader (fun ctx => { ctx with userName := localDecl.userName }) x else x /-- Creating a mapping containing `b ↦ e'` where `patternWithRef e' = some (stx, b)`, and `e'` is a subterm of `e`. This is a helper function for `whnfPreservingPatternRef`. -/ private def mkPatternRefMap (e : Expr) : ExprMap Expr := runST go where go (σ) : ST σ (ExprMap Expr) := do let map : ST.Ref σ (ExprMap Expr) ← ST.mkRef {} e.forEach fun e => do match patternWithRef? e with | some (_, b) => map.modify (·.insert b e) | none => return () map.get /-- Try to restore `Syntax` ref information stored in `map` after applying `whnf` at `whnfPreservingPatternRef`. It assumes `map` has been constructed using `mkPatternRefMap`. -/ private def applyRefMap (e : Expr) (map : ExprMap Expr) : Expr := e.replace fun e => match patternWithRef? e with | some _ => some e -- stop `e` already has annotation | none => match map.find? e with | some eWithRef => some eWithRef -- stop `e` found annotation | none => none -- continue /-- Applies `whnf` but tries to preserve `PatternWithRef` information. This is a bit hackish, but it is necessary for providing proper jump-to-definition information in examples such as ``` def f (x : Nat) : Nat := match x with | 0 => 1 | y + 1 => y ``` Without this trick, the `PatternWithRef` is lost for the `y` at the pattern `y+1`. -/ private def whnfPreservingPatternRef (e : Expr) : MetaM Expr := do let eNew ← whnf e if eNew.isConstructorApp (← getEnv) then return eNew else return applyRefMap eNew (mkPatternRefMap e) /-- Normalize the pattern and collect all patterns variables (explicit and implicit). This method is the one that decides where the inaccessible annotations must be inserted. The pattern variables are both free variables (for explicit pattern variables) and metavariables (for implicit ones). Recall that `mkLambdaFVars` now allows us to abstract both free variables and metavariables. -/ partial def normalize (e : Expr) : M Expr := do match inaccessible? e with | some e => processInaccessible e | none => match patternWithRef? e with | some (ref, e) => return mkPatternWithRef (← normalize e) ref | none => match e.arrayLit? with | some (α, lits) => mkArrayLit α (← lits.mapM normalize) | none => if let some e := Match.isNamedPattern? e then let x := e.getArg! 1 let p := e.getArg! 2 let h := e.getArg! 3 unless x.consumeMData.isFVar && h.consumeMData.isFVar do throwError "unexpected occurrence of auxiliary declaration 'namedPattern'" addVar x let p ← normalize p addVar h return mkApp4 e.getAppFn (e.getArg! 0) x p h else if isMatchValue e then return e else if e.isFVar then if (← isExplicitPatternVar e) then processVar e else return mkInaccessible e else if e.getAppFn.isMVar then let eNew ← instantiateMVars e if eNew != e then withMVar e.getAppFn.mvarId! <| normalize eNew else if e.isMVar then withMVar e.mvarId! <| processVar e else throwInvalidPattern e else let eNew ← whnfPreservingPatternRef e if eNew != e then normalize eNew else matchConstCtor e.getAppFn (fun _ => return mkInaccessible (← eraseInaccessibleAnnotations (← instantiateMVars e))) (fun v _ => do let args := e.getAppArgs unless args.size == v.numParams + v.numFields do throwInvalidPattern e let params := args.extract 0 v.numParams let params ← params.mapM fun p => instantiateMVars p let fields := args.extract v.numParams args.size let fields ← fields.mapM normalize return mkAppN e.getAppFn (params ++ fields)) where addVar (e : Expr) : M Unit := do let e ← erasePatternRefAnnotations e unless (← get).patternVars.contains e do modify fun s => { s with patternVars := s.patternVars.push e } processVar (e : Expr) : M Expr := do let e' ← erasePatternRefAnnotations e if (← get).patternVars.contains e' then return mkInaccessible (← eraseInaccessibleAnnotations e) else if e'.isMVar then e'.mvarId!.setTag (← read).userName modify fun s => { s with patternVars := s.patternVars.push e' } return e processInaccessible (e : Expr) : M Expr := do let e' ← erasePatternRefAnnotations e match e' with | Expr.fvar _ => if (← isExplicitPatternVar e') then processVar e else return mkInaccessible e | _ => if e'.getAppFn.isMVar then let eNew ← instantiateMVars e' if eNew != e' then withMVar e'.getAppFn.mvarId! <| processInaccessible eNew else if e'.isMVar then withMVar e'.mvarId! <| processVar e' else throwInvalidPattern e else return mkInaccessible (← eraseInaccessibleAnnotations (← instantiateMVars e)) /-- Auxiliary function for combining the `matchType` and all patterns into a single expression. We use it before we abstract all patterns variables. -/ private partial def packMatchTypePatterns (matchType : Expr) (ps : Array Expr) : MetaM Expr := ps.foldlM (init := matchType) fun result p => mkAppM ``PProd.mk #[result, p] /-- The inverse of `packMatchTypePatterns`. -/ private partial def unpackMatchTypePatterns (p : Expr) : Expr × Array Expr := if p.isAppOf ``PProd.mk then let (matchType, ps) := unpackMatchTypePatterns (p.getArg! 2) (matchType, ps.push (p.getArg! 3)) else (p, #[]) /-- Convert a (normalized) pattern encoded as an `Expr` into a `Pattern`. This method assumes that `e` has been normalized and the explicit and implicit (i.e., metavariables) pattern variables have already been abstracted and converted back into new free variables. -/ private partial def toPattern (e : Expr) : MetaM Pattern := do match inaccessible? e with | some e => return Pattern.inaccessible e | none => match e.arrayLit? with | some (α, lits) => return Pattern.arrayLit α (← lits.mapM toPattern) | none => if let some e := Match.isNamedPattern? e then let p ← toPattern <| e.getArg! 2 match e.getArg! 1, e.getArg! 3 with | Expr.fvar x, Expr.fvar h => return Pattern.as x p h | _, _ => throwError "unexpected occurrence of auxiliary declaration 'namedPattern'" else if isMatchValue e then return Pattern.val e else if e.isFVar then return Pattern.var e.fvarId! else matchConstCtor e.getAppFn (fun _ => unreachable!) fun v us => do let args := e.getAppArgs let params := args.extract 0 v.numParams let params ← params.mapM fun p => instantiateMVars p let fields := args.extract v.numParams args.size let fields ← fields.mapM toPattern return Pattern.ctor v.name us params.toList fields.toList structure TopSort.State where visitedFVars : FVarIdSet := {} visitedMVars : MVarIdSet := {} result : Array Expr := #[] abbrev TopSortM := StateRefT TopSort.State TermElabM /-- Topological sort. We need it because inaccessible patterns may contain pattern variables that are declared later. That is, processing patterns from left to right to do not guarantee that the pattern variables are collected in the "right" order. "Right" here means pattern `x` must occur befor pattern `y` if `y`s type depends on `x`. -/ private partial def topSort (patternVars : Array Expr) : TermElabM (Array Expr) := do let (_, s) ← patternVars.mapM visit |>.run {} return s.result where visit (e : Expr) : TopSortM Unit := do match e with | Expr.proj _ _ e => visit e | Expr.forallE _ d b _ => visit d; visit b | Expr.lam _ d b _ => visit d; visit b | Expr.letE _ t v b _ => visit t; visit v; visit b | Expr.app f a => visit f; visit a | Expr.mdata _ b => visit b | Expr.mvar mvarId => let v ← instantiateMVars e if !v.isMVar then visit v else if patternVars.contains e then unless (← get).visitedMVars.contains mvarId do modify fun s => { s with visitedMVars := s.visitedMVars.insert mvarId } let mvarDecl ← getMVarDecl mvarId visit mvarDecl.type modify fun s => { s with result := s.result.push e } | Expr.fvar fvarId => if patternVars.contains e then unless (← get).visitedFVars.contains fvarId do modify fun s => { s with visitedFVars := s.visitedFVars.insert fvarId } visit (← fvarId.getType) modify fun s => { s with result := s.result.push e } | _ => return () /-- Save pattern information in the info tree, and remove `patternWithRef?` annotations. -/ partial def savePatternInfo (p : Expr) : TermElabM Expr := go p |>.run false where /-- The `Bool` context is true iff we are inside of an "inaccessible" pattern. -/ go (p : Expr) : ReaderT Bool TermElabM Expr := do match p with | .forallE n d b bi => withLocalDecl n bi (← go d) fun x => do mkForallFVars #[x] (← go (b.instantiate1 x)) | .lam n d b bi => withLocalDecl n bi (← go d) fun x => do mkLambdaFVars #[x] (← go (b.instantiate1 x)) | .letE n t v b .. => withLetDecl n (← go t) (← go v) fun x => do mkLetFVars #[x] (← go (b.instantiate1 x)) | .app f a => return mkApp (← go f) (← go a) | .proj _ _ b => return p.updateProj! (← go b) | .mdata k b => if inaccessible? p |>.isSome then return mkMData k (← withReader (fun _ => false) (go b)) else if let some (stx, p) := patternWithRef? p then Elab.withInfoContext' (go p) fun p => do /- If `p` is a free variable and we are not inside of an "inaccessible" pattern, this `p` is a binder. -/ mkTermInfo Name.anonymous stx p (isBinder := p.isFVar && !(← read)) else return mkMData k (← go b) | _ => return p /-- Main method for `withDepElimPatterns`. - `PatternVarDecls`: are the explicit pattern variables provided by the user. - `ps`: are the patterns provided by the user. - `matchType`: the expected typ for this branch. It depends on the explicit pattern variables and the implicit ones that are still represented as metavariables, and are found by this function. - `k` is the continuation that is executed in an updated local context with the all pattern variables (explicit and implicit). Note that, `patternVarDecls` are all replaced since they may depend on implicit pattern variables (i.e., metavariables) that are converted into new free variables by this method. -/ partial def main (patternVarDecls : Array PatternVarDecl) (ps : Array Expr) (matchType : Expr) (k : Array LocalDecl → Array Pattern → Expr → TermElabM α) : TermElabM α := do let explicitPatternVars := patternVarDecls.map fun decl => decl.fvarId let (ps, s) ← ps.mapM normalize |>.run { explicitPatternVars } |>.run {} let patternVars ← topSort s.patternVars trace[Elab.match] "patternVars after topSort: {patternVars}" for explicit in explicitPatternVars do unless patternVars.any (· == mkFVar explicit) do withInPattern do throwError "invalid patterns, `{mkFVar explicit}` is an explicit pattern variable, but it only occurs in positions that are inaccessible to pattern matching{indentD (MessageData.joinSep (ps.toList.map (MessageData.ofExpr .)) m!"\n\n")}" let packed ← pack patternVars ps matchType trace[Elab.match] "packed: {packed}" let lctx := explicitPatternVars.foldl (init := (← getLCtx)) fun lctx d => lctx.erase d withTheReader Meta.Context (fun ctx => { ctx with lctx := lctx }) do check packed unpack packed fun patternVars patterns matchType => do let localDecls ← patternVars.mapM fun x => x.fvarId!.getDecl trace[Elab.match] "patternVars: {patternVars}, matchType: {matchType}" k localDecls (← patterns.mapM fun p => toPattern p) matchType where pack (patternVars : Array Expr) (ps : Array Expr) (matchType : Expr) : MetaM Expr := do /- Recall that some of the `patternVars` are metavariables without a user facing name. Thus, this method tries to infer names for them using `ps` before performing the `mkLambdaFVars` abstraction. Let `?m` be a metavariable in `patternVars` without a user facing name. The heuristic uses the patterns `ps`. We traverse the patterns from right to left searching for applications `f ... ?m`. The name for the corresponding `f`-parameter is used to name `?m`. We search from right to left to make sure we visit a pattern before visiting its indices. Example: ``` #[@List.cons α i ?m, @HList.cons α β i ?m a as, @Member.head α i ?m] ``` -/ let setMVarsAt (e : Expr) : StateRefT (Array MVarId) MetaM Unit := do let mvarIds ← setMVarUserNamesAt (← erasePatternRefAnnotations e) patternVars modify (· ++ mvarIds) let go : StateRefT (Array MVarId) MetaM Expr := do try for p in ps.reverse do setMVarsAt p mkLambdaFVars patternVars (← packMatchTypePatterns matchType ps) (binderInfoForMVars := BinderInfo.default) finally resetMVarUserNames (← get) go |>.run' #[] unpack (packed : Expr) (k : (patternVars : Array Expr) → (patterns : Array Expr) → (matchType : Expr) → TermElabM α) : TermElabM α := let rec go (packed : Expr) (patternVars : Array Expr) : TermElabM α := do match packed with | .lam n d b _ => withLocalDeclD n (← erasePatternRefAnnotations (← eraseInaccessibleAnnotations d)) fun patternVar => go (b.instantiate1 patternVar) (patternVars.push patternVar) | _ => let (matchType, patterns) := unpackMatchTypePatterns packed let matchType ← erasePatternRefAnnotations (← eraseInaccessibleAnnotations matchType) let patterns ← patterns.mapM (savePatternInfo ·) k patternVars patterns matchType go packed #[] end ToDepElimPattern def withDepElimPatterns (patternVarDecls : Array PatternVarDecl) (ps : Array Expr) (matchType : Expr) (k : Array LocalDecl → Array Pattern → Expr → TermElabM α) : TermElabM α := do ToDepElimPattern.main patternVarDecls ps matchType k private def withElaboratedLHS {α} (ref : Syntax) (patternVarDecls : Array PatternVarDecl) (patternStxs : Array Syntax) (matchType : Expr) (k : AltLHS → Expr → TermElabM α) : ExceptT PatternElabException TermElabM α := do let (patterns, matchType) ← withSynthesize <| elabPatterns patternStxs matchType id (α := TermElabM α) do trace[Elab.match] "patterns: {patterns}" withDepElimPatterns patternVarDecls patterns matchType fun localDecls patterns matchType => do k { ref := ref, fvarDecls := localDecls.toList, patterns := patterns.toList } matchType /-- Try to clear the free variables in `toClear` and auxiliary discriminants, and then execute `k` in the updated local context. If `type` or another local variables depends on a free variable in `toClear`, then it is not cleared. -/ private def withToClear (toClear : Array FVarId) (type : Expr) (k : TermElabM α) : TermElabM α := do if toClear.isEmpty then k else let toClear ← sortFVarIds toClear trace[Elab.match] ">> toClear {toClear.map mkFVar}" let mut lctx ← getLCtx let mut localInsts ← getLocalInstances for fvarId in toClear.reverse do if !(← dependsOn type fvarId) then if !(← lctx.anyM fun localDecl => pure (localDecl.fvarId != fvarId) <&&> localDeclDependsOn localDecl fvarId) then lctx := lctx.erase fvarId localInsts := localInsts.filter fun localInst => localInst.fvar.fvarId! != fvarId withLCtx lctx localInsts k private def withoutAuxDiscrs (matchType : Expr) (k : TermElabM α) : TermElabM α := do let mut toClear := #[] for localDecl in (← getLCtx) do if isAuxDiscrName localDecl.userName || isAuxFunDiscrName localDecl.userName then toClear := toClear.push localDecl.fvarId withToClear toClear matchType k /-- Generate equalities `h : discr = pattern` for discriminants annotated with `h :`. We use these equalities to elaborate the right-hand-side of a `match` alternative. -/ private def withEqs (discrs : Array Discr) (patterns : List Pattern) (k : Array Expr → TermElabM α) : TermElabM α := do go 0 patterns #[] where go (i : Nat) (ps : List Pattern) (eqs : Array Expr) : TermElabM α := do match ps with | [] => k eqs | p::ps => if h : i < discrs.size then let discr := discrs.get ⟨i, h⟩ if let some h := discr.h? then withLocalDeclD h.getId (← mkEqHEq discr.expr (← p.toExpr)) fun eq => do addTermInfo' h eq (isBinder := true) go (i+1) ps (eqs.push eq) else go (i+1) ps eqs else k eqs /-- Elaborate the `match` alternative `alt` using the given `matchType`. The array `toClear` contains variables that must be cleared before elaborating the `rhs` because they have been generalized/refined. -/ private def elabMatchAltView (discrs : Array Discr) (alt : MatchAltView) (matchType : Expr) (toClear : Array FVarId) : ExceptT PatternElabException TermElabM (AltLHS × Expr) := withRef alt.ref do withoutAuxDiscrs matchType do let (patternVars, alt) ← collectPatternVars alt trace[Elab.match] "patternVars: {patternVars}" withPatternVars patternVars fun patternVarDecls => do withElaboratedLHS alt.ref patternVarDecls alt.patterns matchType fun altLHS matchType => withEqs discrs altLHS.patterns fun eqs => withLocalInstances altLHS.fvarDecls do trace[Elab.match] "elabMatchAltView: {matchType}" -- connect match-generalized pattern fvars, which are a suffix of `latLHS.fvarDecls`, -- to their original fvars (independently of whether they were cleared successfully) in the info tree for (fvar, baseId) in altLHS.fvarDecls.toArray.reverse.zip toClear.reverse do pushInfoLeaf <| .ofFVarAliasInfo { id := fvar.fvarId, baseId } let matchType ← instantiateMVars matchType -- If `matchType` is of the form `@m ...`, we create a new metavariable with the current scope. -- This improves the effectiveness of the `isDefEq` default approximations let matchType' ← if matchType.getAppFn.isMVar then mkFreshTypeMVar else pure matchType withToClear toClear matchType' do let rhs ← elabTermEnsuringType alt.rhs matchType' -- We use all approximations to ensure the auxiliary type is defeq to the original one. unless (← fullApproxDefEq <| isDefEq matchType' matchType) do throwError "type mistmatch, alternative {← mkHasTypeButIsExpectedMsg matchType' matchType}" let xs := altLHS.fvarDecls.toArray.map LocalDecl.toExpr ++ eqs let rhs ← if xs.isEmpty then pure <| mkSimpleThunk rhs else mkLambdaFVars xs rhs trace[Elab.match] "rhs: {rhs}" return (altLHS, rhs) /-- Collect problematic index for the "discriminant refinement feature". This method is invoked when we detect a type mismatch at a pattern #`idx` of some alternative. -/ private partial def getIndexToInclude? (discr : Expr) (pathToIndex : List Nat) : TermElabM (Option Expr) := do go (← inferType discr) pathToIndex |>.run where go (e : Expr) (path : List Nat) : OptionT MetaM Expr := do match path with | [] => return e | i::path => let e ← whnfD e guard <| e.isApp && i < e.getAppNumArgs go (e.getArg! i) path structure GeneralizeResult where discrs : Array Discr /-- `FVarId`s of the variables that have been generalized. We store them to clear after in each branch. -/ toClear : Array FVarId := #[] matchType : Expr altViews : Array MatchAltView refined : Bool := false /-- "Generalize" variables that depend on the discriminants. Remarks and limitations: - We currently do not generalize let-decls. - We abort generalization if the new `matchType` is type incorrect. - Only discriminants that are free variables are considered during specialization. - We "generalize" by adding new discriminants and pattern variables. We do not "clear" the generalized variables, but they become inaccessible since they are shadowed by the patterns variables. We assume this is ok since this is the exact behavior users would get if they had written it by hand. Recall there is no `clear` in term mode. -/ private def generalize (discrs : Array Discr) (matchType : Expr) (altViews : Array MatchAltView) (generalizing? : Option Bool) : TermElabM GeneralizeResult := do let gen := if let some g := generalizing? then g else true if !gen then return { discrs, matchType, altViews } else let discrExprs := discrs.map (·.expr) /- let-decls are currently being ignored by the generalizer. -/ let ysFVarIds ← getFVarsToGeneralize discrExprs (ignoreLetDecls := true) if ysFVarIds.isEmpty then return { discrs, matchType, altViews } else let ys := ysFVarIds.map mkFVar let matchType' ← forallBoundedTelescope matchType discrs.size fun ds type => do let type ← mkForallFVars ys type let (discrs', ds') := Array.unzip <| Array.zip discrExprs ds |>.filter fun (di, _) => di.isFVar let type := type.replaceFVars discrs' ds' mkForallFVars ds type if (← isTypeCorrect matchType') then let discrs := discrs ++ ys.map fun y => { expr := y : Discr } let altViews ← altViews.mapM fun altView => do let patternVars ← getPatternsVars altView.patterns -- We traverse backwards because we want to keep the most recent names. -- For example, if `ys` contains `#[h, h]`, we want to make sure `mkFreshUsername is applied to the first `h`, -- since it is already shadowed by the second. let ysUserNames ← ys.foldrM (init := #[]) fun ys ysUserNames => do let yDecl ← ys.fvarId!.getDecl let mut yUserName := yDecl.userName if ysUserNames.contains yUserName then yUserName ← mkFreshUserName yUserName -- Explicitly provided pattern variables shadow `y` else if patternVars.any fun x => x.getId == yUserName then yUserName ← mkFreshUserName yUserName return ysUserNames.push yUserName let ysIds ← ysUserNames.reverse.mapM fun n => return mkIdentFrom (← getRef) n return { altView with patterns := altView.patterns ++ ysIds } return { discrs, toClear := ysFVarIds, matchType := matchType', altViews, refined := true } else return { discrs, matchType, altViews } private partial def elabMatchAltViews (generalizing? : Option Bool) (discrs : Array Discr) (matchType : Expr) (altViews : Array MatchAltView) : TermElabM (Array Discr × Expr × Array (AltLHS × Expr) × Bool) := do loop discrs #[] matchType altViews none where /-- "Discriminant refinement" main loop. `first?` contains the first error message we found before updated the `discrs`. -/ loop (discrs : Array Discr) (toClear : Array FVarId) (matchType : Expr) (altViews : Array MatchAltView) (first? : Option (SavedState × Exception)) : TermElabM (Array Discr × Expr × Array (AltLHS × Expr) × Bool) := do let s ← saveState let { discrs := discrs', toClear := toClear', matchType := matchType', altViews := altViews', refined } ← generalize discrs matchType altViews generalizing? match (← altViews'.mapM (fun altView => elabMatchAltView discrs' altView matchType' (toClear ++ toClear')) |>.run) with | Except.ok alts => return (discrs', matchType', alts, first?.isSome || refined) | Except.error { patternIdx := patternIdx, pathToIndex := pathToIndex, ex := ex } => let discr := discrs[patternIdx]! let some index ← getIndexToInclude? discr.expr pathToIndex | throwEx (← updateFirst first? ex) trace[Elab.match] "index to include: {index}" if (← discrs.anyM fun discr => isDefEq discr.expr index) then throwEx (← updateFirst first? ex) let first ← updateFirst first? ex s.restore (restoreInfo := true) let indices ← collectDeps #[index] (discrs.map (·.expr)) let matchType ← try updateMatchType indices matchType catch _ => throwEx first let ref ← getRef trace[Elab.match] "new indices to add as discriminants: {indices}" let wildcards ← indices.mapM fun index => do if index.isFVar then let localDecl ← index.fvarId!.getDecl if localDecl.userName.hasMacroScopes then return mkHole ref else let id := mkIdentFrom ref localDecl.userName `(?$id) else return mkHole ref let altViews := altViews.map fun altView => { altView with patterns := wildcards ++ altView.patterns } let indDiscrs ← indices.mapM fun i => do match discr.h? with | none => return { expr := i : Discr } | some h => -- If the discriminant that introduced this index is annotated with `h : discr`, then we should annotate the new discriminant too. let h := mkIdentFrom h (← mkFreshUserName `h) return { expr := i, h? := h : Discr } let discrs := indDiscrs ++ discrs let indexFVarIds := indices.filterMap fun | .fvar fvarId .. => some fvarId | _ => none loop discrs (toClear ++ indexFVarIds) matchType altViews first throwEx {α} (p : SavedState × Exception) : TermElabM α := do p.1.restore (restoreInfo := true); throw p.2 updateFirst (first? : Option (SavedState × Exception)) (ex : Exception) : TermElabM (SavedState × Exception) := do match first? with | none => return (← saveState, ex) | some first => return first containsFVar (es : Array Expr) (fvarId : FVarId) : Bool := es.any fun e => e.isFVar && e.fvarId! == fvarId /-- Update `indices` by including any free variable `x` s.t. - Type of some `discr` depends on `x`. - Type of `x` depends on some free variable in `indices`. If we don't include these extra variables in indices, then `updateMatchType` will generate a type incorrect term. For example, suppose `discr` contains `h : @HEq α a α b`, and `indices` is `#[α, b]`, and `matchType` is `@HEq α a α b → B`. `updateMatchType indices matchType` produces the type `(α' : Type) → (b : α') → @HEq α' a α' b → B` which is type incorrect because we have `a : α`. The method `collectDeps` will include `a` into `indices`. This method does not handle dependencies among non-free variables. We rely on the type checking method `check` at `updateMatchType`. Remark: `indices : Array Expr` does not need to be an array anymore. We should cleanup this code, and use `index : Expr` instead. -/ collectDeps (indices : Array Expr) (discrs : Array Expr) : TermElabM (Array Expr) := do let mut s : CollectFVars.State := {} for discr in discrs do s := collectFVars s (← instantiateMVars (← inferType discr)) let (indicesFVar, indicesNonFVar) := indices.split Expr.isFVar let indicesFVar := indicesFVar.map Expr.fvarId! let mut toAdd := #[] for fvarId in s.fvarSet.toList do unless containsFVar discrs fvarId || containsFVar indices fvarId do let localDecl ← fvarId.getDecl for indexFVarId in indicesFVar do if (← localDeclDependsOn localDecl indexFVarId) then toAdd := toAdd.push fvarId let indicesFVar ← sortFVarIds (indicesFVar ++ toAdd) return indicesFVar.map mkFVar ++ indicesNonFVar updateMatchType (indices : Array Expr) (matchType : Expr) : TermElabM Expr := do let matchType ← indices.foldrM (init := matchType) fun index matchType => do let indexType ← inferType index let matchTypeBody ← kabstract matchType index let userName ← mkUserNameFor index return Lean.mkForall userName BinderInfo.default indexType matchTypeBody check matchType return matchType def mkMatcher (input : Meta.Match.MkMatcherInput) : TermElabM MatcherResult := Meta.Match.mkMatcher input register_builtin_option match.ignoreUnusedAlts : Bool := { defValue := false descr := "if true, do not generate error if an alternative is not used" } def reportMatcherResultErrors (altLHSS : List AltLHS) (result : MatcherResult) : TermElabM Unit := do unless result.counterExamples.isEmpty do withHeadRefOnly <| logError m!"missing cases:\n{Meta.Match.counterExamplesToMessageData result.counterExamples}" return () unless match.ignoreUnusedAlts.get (← getOptions) || result.unusedAltIdxs.isEmpty do let mut i := 0 for alt in altLHSS do if result.unusedAltIdxs.contains i then withRef alt.ref do logError "redundant alternative" i := i + 1 /-- If `altLHSS + rhss` is encoding `| PUnit.unit => rhs[0]`, return `rhs[0]` Otherwise, return none. -/ private def isMatchUnit? (altLHSS : List Match.AltLHS) (rhss : Array Expr) : MetaM (Option Expr) := do assert! altLHSS.length == rhss.size match altLHSS with | [ { fvarDecls := [], patterns := [ Pattern.ctor `PUnit.unit .. ], .. } ] => /- Recall that for alternatives of the form `| PUnit.unit => rhs`, `rhss[0]` is of the form `fun _ : Unit => b`. -/ match rhss[0]! with | Expr.lam _ _ b _ => return if b.hasLooseBVars then none else b | _ => return none | _ => return none private def elabMatchAux (generalizing? : Option Bool) (discrStxs : Array Syntax) (altViews : Array MatchAltView) (matchOptMotive : Syntax) (expectedType : Expr) : TermElabM Expr := do let mut generalizing? := generalizing? if !matchOptMotive.isNone then if generalizing? == some true then throwError "the '(generalizing := true)' parameter is not supported when the 'match' motive is explicitly provided" generalizing? := some false let (discrs, matchType, altLHSS, isDep, rhss) ← commitIfDidNotPostpone do let ⟨discrs, matchType, isDep, altViews⟩ ← elabMatchTypeAndDiscrs discrStxs matchOptMotive altViews expectedType let matchAlts ← liftMacroM <| expandMacrosInPatterns altViews trace[Elab.match] "matchType: {matchType}" let (discrs, matchType, alts, refined) ← elabMatchAltViews generalizing? discrs matchType matchAlts let isDep := isDep || refined /- We should not use `synthesizeSyntheticMVarsNoPostponing` here. Otherwise, we will not be able to elaborate examples such as: ``` def f (x : Nat) : Option Nat := none def g (xs : List (Nat × Nat)) : IO Unit := xs.forM fun x => match f x.fst with | _ => pure () ``` If `synthesizeSyntheticMVarsNoPostponing`, the example above fails at `x.fst` because the type of `x` is only available after we proces the last argument of `List.forM`. We apply pending default types to make sure we can process examples such as ``` let (a, b) := (0, 0) ``` -/ synthesizeSyntheticMVarsUsingDefault let rhss := alts.map Prod.snd let matchType ← instantiateMVars matchType let altLHSS ← alts.toList.mapM fun alt => do let altLHS ← Match.instantiateAltLHSMVars alt.1 /- Remark: we try to postpone before throwing an error. The combinator `commitIfDidNotPostpone` ensures we backtrack any updates that have been performed. The quick-check `waitExpectedTypeAndDiscrs` minimizes the number of scenarios where we have to postpone here. Here is an example that passes the `waitExpectedTypeAndDiscrs` test, but postpones here. ``` def bad (ps : Array (Nat × Nat)) : Array (Nat × Nat) := (ps.filter fun (p : Prod _ _) => match p with | (x, y) => x == 0) ++ ps ``` When we try to elaborate `fun (p : Prod _ _) => ...` for the first time, we haven't propagated the type of `ps` yet because `Array.filter` has type `{α : Type u_1} → (α → Bool) → (as : Array α) → optParam Nat 0 → optParam Nat (Array.size as) → Array α` However, the partial type annotation `(p : Prod _ _)` makes sure we succeed at the quick-check `waitExpectedTypeAndDiscrs`. -/ withRef altLHS.ref do for d in altLHS.fvarDecls do if d.hasExprMVar then tryPostpone withExistingLocalDecls altLHS.fvarDecls do runPendingTacticsAt d.type if (← instantiateMVars d.type).hasExprMVar then throwMVarError m!"invalid match-expression, type of pattern variable '{d.toExpr}' contains metavariables{indentExpr d.type}" for p in altLHS.patterns do if (← Match.instantiatePatternMVars p).hasExprMVar then tryPostpone withExistingLocalDecls altLHS.fvarDecls do throwMVarError m!"invalid match-expression, pattern contains metavariables{indentExpr (← p.toExpr)}" pure altLHS return (discrs, matchType, altLHSS, isDep, rhss) if let some r ← if isDep then pure none else isMatchUnit? altLHSS rhss then return r else let numDiscrs := discrs.size let matcherName ← mkAuxName `match let matcherResult ← mkMatcher { matcherName, matchType, discrInfos := discrs.map fun discr => { hName? := discr.h?.map (·.getId) }, lhss := altLHSS } reportMatcherResultErrors altLHSS matcherResult matcherResult.addMatcher let motive ← forallBoundedTelescope matchType numDiscrs fun xs matchType => mkLambdaFVars xs matchType let r := mkApp matcherResult.matcher motive let r := mkAppN r (discrs.map (·.expr)) let r := mkAppN r rhss trace[Elab.match] "result: {r}" return r -- leading_parser "match " >> optional generalizingParam >> optional motive >> sepBy1 matchDiscr ", " >> " with " >> ppDedent matchAlts private def getDiscrs (matchStx : Syntax) : Array Syntax := matchStx[3].getSepArgs private def getMatchOptMotive (matchStx : Syntax) : Syntax := matchStx[2] open TSyntax.Compat in private def expandNonAtomicDiscrs? (matchStx : Syntax) : TermElabM (Option Syntax) := let matchOptMotive := getMatchOptMotive matchStx if matchOptMotive.isNone then do let discrs := getDiscrs matchStx let allLocal ← discrs.allM fun discr => Option.isSome <$> isAtomicDiscr? discr[1] if allLocal then return none else -- We use `foundFVars` to make sure the discriminants are distinct variables. -- See: code for computing "matchType" at `elabMatchTypeAndDiscrs` let rec loop (discrs : List Syntax) (discrsNew : Array Syntax) (foundFVars : FVarIdSet) := do match discrs with | [] => let discrs := Syntax.mkSep discrsNew (mkAtomFrom matchStx ", ") pure (matchStx.setArg 3 discrs) | discr :: discrs => -- Recall that -- matchDiscr := leading_parser optional (ident >> ":") >> termParser let term := discr[1] let addAux : TermElabM Syntax := withFreshMacroScope do let d ← mkAuxDiscr unless isAuxDiscrName d.getId do -- Use assertion? throwError "unexpected internal auxiliary discriminant name" let discrNew := discr.setArg 1 d let r ← loop discrs (discrsNew.push discrNew) foundFVars `(let $d := $term; $r) match (← isAtomicDiscr? term) with | some x => if x.isFVar then loop discrs (discrsNew.push discr) (foundFVars.insert x.fvarId!) else addAux | none => addAux return some (← loop discrs.toList #[] {}) else -- We do not pull non atomic discriminants when match type is provided explicitly by the user return none private def waitExpectedType (expectedType? : Option Expr) : TermElabM Expr := do tryPostponeIfNoneOrMVar expectedType? match expectedType? with | some expectedType => pure expectedType | none => mkFreshTypeMVar private def tryPostponeIfDiscrTypeIsMVar (matchStx : Syntax) : TermElabM Unit := do -- We don't wait for the discriminants types when match type is provided by user if getMatchOptMotive matchStx |>.isNone then let discrs := getDiscrs matchStx for discr in discrs do let term := discr[1] match (← isAtomicDiscr? term) with | none => throwErrorAt discr "unexpected discriminant" -- see `expandNonAtomicDiscrs? | some d => let dType ← inferType d trace[Elab.match] "discr {d} : {dType}" tryPostponeIfMVar dType /-- We (try to) elaborate a `match` only when the expected type is available. If the `matchType` has not been provided by the user, we also try to postpone elaboration if the type of a discriminant is not available. That is, it is of the form `(?m ...)`. We use `expandNonAtomicDiscrs?` to make sure all discriminants are local variables. This is a standard trick we use in the elaborator, and it is also used to elaborate structure instances. Suppose, we are trying to elaborate ``` match g x with | ... => ... ``` `expandNonAtomicDiscrs?` converts it intro ``` let _discr := g x match _discr with | ... => ... ``` Thus, at `tryPostponeIfDiscrTypeIsMVar` we only need to check whether the type of `_discr` is not of the form `(?m ...)`. Note that, the auxiliary variable `_discr` is expanded at `elabAtomicDiscr`. This elaboration technique is needed to elaborate terms such as: ```lean xs.filter fun (a, b) => a > b ``` which are syntax sugar for ```lean List.filter (fun p => match p with | (a, b) => a > b) xs ``` When we visit `match p with | (a, b) => a > b`, we don't know the type of `p` yet. -/ private def waitExpectedTypeAndDiscrs (matchStx : Syntax) (expectedType? : Option Expr) : TermElabM Expr := do tryPostponeIfNoneOrMVar expectedType? tryPostponeIfDiscrTypeIsMVar matchStx match expectedType? with | some expectedType => return expectedType | none => mkFreshTypeMVar /-- ``` leading_parser "match " >> optional generalizingParam >> optional motive >> sepBy1 matchDiscr ", " >> " with " >> ppDedent matchAlts ``` Remark the `optIdent` must be `none` at `matchDiscr`. They are expanded by `expandMatchDiscr?`. -/ private def elabMatchCore (stx : Syntax) (expectedType? : Option Expr) : TermElabM Expr := do let expectedType ← waitExpectedTypeAndDiscrs stx expectedType? let discrStxs := (getDiscrs stx).map fun d => d let gen? := getMatchGeneralizing? stx let altViews := getMatchAlts stx let matchOptMotive := getMatchOptMotive stx elabMatchAux gen? discrStxs altViews matchOptMotive expectedType private def isPatternVar (stx : Syntax) : TermElabM Bool := do match (← resolveId? stx "pattern") with | none => return isAtomicIdent stx | some f => match f with | Expr.const fName _ => match (← getEnv).find? fName with | some (ConstantInfo.ctorInfo _) => return false | some _ => return !hasMatchPatternAttribute (← getEnv) fName | _ => return isAtomicIdent stx | _ => return isAtomicIdent stx where isAtomicIdent (stx : Syntax) : Bool := stx.isIdent && stx.getId.eraseMacroScopes.isAtomic @[builtinTermElab «match»] def elabMatch : TermElab := fun stx expectedType? => do match stx with | `(match $discr:term with | $y:ident => $rhs) => if (← isPatternVar y) then expandSimpleMatch stx discr y rhs expectedType? else elabMatchDefault stx expectedType? | _ => elabMatchDefault stx expectedType? where elabMatchDefault (stx : Syntax) (expectedType? : Option Expr) : TermElabM Expr := do match (← liftMacroM <| expandMatchAlts? stx) with | some stxNew => withMacroExpansion stx stxNew <| elabTerm stxNew expectedType? | none => match (← expandNonAtomicDiscrs? stx) with | some stxNew => withMacroExpansion stx stxNew <| elabTerm stxNew expectedType? | none => let discrs := getDiscrs stx let matchOptMotive := getMatchOptMotive stx if !matchOptMotive.isNone && discrs.any fun d => !d[0].isNone then throwErrorAt matchOptMotive "match motive should not be provided when discriminants with equality proofs are used" elabMatchCore stx expectedType? builtin_initialize registerTraceClass `Elab.match -- leading_parser:leadPrec "nomatch " >> termParser @[builtinTermElab «nomatch»] def elabNoMatch : TermElab := fun stx expectedType? => do match stx with | `(nomatch $discrExpr) => match (← isLocalIdent? discrExpr) with | some _ => let expectedType ← waitExpectedType expectedType? let discr := mkNode ``Lean.Parser.Term.matchDiscr #[mkNullNode, discrExpr] elabMatchAux none #[discr] #[] mkNullNode expectedType | _ => let d ← mkAuxDiscr let stxNew ← `(let $d := $discrExpr; nomatch $d) withMacroExpansion stx stxNew <| elabTerm stxNew expectedType? | _ => throwUnsupportedSyntax end Lean.Elab.Term
26f1af555f0ee205c20b9b68baa1800c89a7b08c
c777c32c8e484e195053731103c5e52af26a25d1
/src/number_theory/cyclotomic/discriminant.lean
f92f560b9693549b8a41613d2a5f420bc486dcc5
[ "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
11,112
lean
/- Copyright (c) 2022 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca -/ import number_theory.cyclotomic.primitive_roots import ring_theory.discriminant /-! # Discriminant of cyclotomic fields We compute the discriminant of a `p ^ n`-th cyclotomic extension. ## Main results * `is_cyclotomic_extension.discr_odd_prime` : if `p` is an odd prime such that `is_cyclotomic_extension {p} K L` and `irreducible (cyclotomic p K)`, then `discr K (hζ.power_basis K).basis = (-1) ^ ((p - 1) / 2) * p ^ (p - 2)` for any `hζ : is_primitive_root ζ p`. -/ universes u v open algebra polynomial nat is_primitive_root power_basis open_locale polynomial cyclotomic namespace is_primitive_root variables {n : ℕ+} {K : Type u} [field K] [char_zero K] {ζ : K} variables [is_cyclotomic_extension {n} ℚ K] /-- The discriminant of the power basis given by a primitive root of unity `ζ` is the same as the discriminant of the power basis given by `ζ - 1`. -/ lemma discr_zeta_eq_discr_zeta_sub_one (hζ : is_primitive_root ζ n) : discr ℚ (hζ.power_basis ℚ).basis = discr ℚ (hζ.sub_one_power_basis ℚ).basis := begin haveI : number_field K := number_field.mk, have H₁ : (aeval (hζ.power_basis ℚ).gen) (X - 1 : ℤ[X]) = (hζ.sub_one_power_basis ℚ).gen := by simp, have H₂ : (aeval (hζ.sub_one_power_basis ℚ).gen) (X + 1 : ℤ[X]) = (hζ.power_basis ℚ).gen := by simp, refine discr_eq_discr_of_to_matrix_coeff_is_integral _ (λ i j, to_matrix_is_integral H₁ _ _ _ _) (λ i j, to_matrix_is_integral H₂ _ _ _ _), { exact hζ.is_integral n.pos }, { refine minpoly.is_integrally_closed_eq_field_fractions' _ (hζ.is_integral n.pos) }, { exact is_integral_sub (hζ.is_integral n.pos) is_integral_one }, { refine minpoly.is_integrally_closed_eq_field_fractions' _ _, exact is_integral_sub (hζ.is_integral n.pos) is_integral_one } end end is_primitive_root namespace is_cyclotomic_extension variables {p : ℕ+} {k : ℕ} {K : Type u} {L : Type v} {ζ : L} [field K] [field L] variables [algebra K L] /-- If `p` is a prime and `is_cyclotomic_extension {p ^ (k + 1)} K L`, then the discriminant of `hζ.power_basis K` is `(-1) ^ ((p ^ (k + 1).totient) / 2) * p ^ (p ^ k * ((p - 1) * (k + 1) - 1))` if `irreducible (cyclotomic (p ^ (k + 1)) K))`, and `p ^ (k + 1) ≠ 2`. -/ lemma discr_prime_pow_ne_two [is_cyclotomic_extension {p ^ (k + 1)} K L] [hp : fact (p : ℕ).prime] (hζ : is_primitive_root ζ ↑(p ^ (k + 1))) (hirr : irreducible (cyclotomic (↑(p ^ (k + 1)) : ℕ) K)) (hk : p ^ (k + 1) ≠ 2) : discr K (hζ.power_basis K).basis = (-1) ^ (((p ^ (k + 1) : ℕ).totient) / 2) * p ^ ((p : ℕ) ^ k * ((p - 1) * (k + 1) - 1)) := begin haveI hne := is_cyclotomic_extension.ne_zero' (p ^ (k + 1)) K L, rw [discr_power_basis_eq_norm, finrank L hirr, hζ.power_basis_gen _, ← hζ.minpoly_eq_cyclotomic_of_irreducible hirr, pnat.pow_coe, totient_prime_pow hp.out (succ_pos k), succ_sub_one], have hp2 : p = 2 → k ≠ 0, { unfreezingI { rintro rfl rfl }, exact absurd rfl hk }, congr' 1, { unfreezingI { rcases eq_or_ne p 2 with rfl | hp2 }, { unfreezingI { rcases nat.exists_eq_succ_of_ne_zero (hp2 rfl) with ⟨k, rfl⟩ }, rw [pnat.coe_bit0, pnat.one_coe, succ_sub_succ_eq_sub, tsub_zero, mul_one, pow_succ, mul_assoc, nat.mul_div_cancel_left _ zero_lt_two, nat.mul_div_cancel_left _ zero_lt_two], unfreezingI { cases k }, { simp }, { rw [pow_succ, (even_two.mul_right _).neg_one_pow, ((even_two.mul_right _).mul_right _).neg_one_pow] } }, { replace hp2 : (p : ℕ) ≠ 2, { rwa [ne.def, ← pnat.one_coe, ← pnat.coe_bit0, pnat.coe_inj] }, have hpo : odd (p : ℕ) := hp.out.odd_of_ne_two hp2, obtain ⟨a, ha⟩ := (hp.out.even_sub_one hp2).two_dvd, rw [ha, mul_left_comm, mul_assoc, nat.mul_div_cancel_left _ two_pos, nat.mul_div_cancel_left _ two_pos, mul_right_comm, pow_mul, (hpo.pow.mul _).neg_one_pow, pow_mul, hpo.pow.neg_one_pow], refine nat.even.sub_odd _ (even_two_mul _) odd_one, rw [mul_left_comm, ← ha], exact one_le_mul (one_le_pow _ _ hp.1.pos) (succ_le_iff.2 $ tsub_pos_of_lt hp.1.one_lt) } }, { have H := congr_arg derivative (cyclotomic_prime_pow_mul_X_pow_sub_one K p k), rw [derivative_mul, derivative_sub, derivative_one, sub_zero, derivative_X_pow, C_eq_nat_cast, derivative_sub, derivative_one, sub_zero, derivative_X_pow, C_eq_nat_cast, ← pnat.pow_coe, hζ.minpoly_eq_cyclotomic_of_irreducible hirr] at H, replace H := congr_arg (λ P, aeval ζ P) H, simp only [aeval_add, aeval_mul, minpoly.aeval, zero_mul, add_zero, aeval_nat_cast, _root_.map_sub, aeval_one, aeval_X_pow] at H, replace H := congr_arg (algebra.norm K) H, have hnorm : (norm K) (ζ ^ (p : ℕ) ^ k - 1) = p ^ ((p : ℕ) ^ k), { by_cases hp : p = 2, { exact hζ.pow_sub_one_norm_prime_pow_of_ne_zero hirr le_rfl (hp2 hp) }, { exact hζ.pow_sub_one_norm_prime_ne_two hirr le_rfl hp } }, rw [monoid_hom.map_mul, hnorm, monoid_hom.map_mul, ← map_nat_cast (algebra_map K L), algebra.norm_algebra_map, finrank L hirr, pnat.pow_coe, totient_prime_pow hp.out (succ_pos k), nat.sub_one, nat.pred_succ, ← hζ.minpoly_eq_cyclotomic_of_irreducible hirr, map_pow, hζ.norm_eq_one hk hirr, one_pow, mul_one, cast_pow, ← coe_coe, ← pow_mul, ← mul_assoc, mul_comm (k + 1), mul_assoc] at H, { have := mul_pos (succ_pos k) (tsub_pos_of_lt hp.out.one_lt), rw [← succ_pred_eq_of_pos this, mul_succ, pow_add _ _ ((p : ℕ) ^ k)] at H, replace H := (mul_left_inj' (λ h, _)).1 H, { simpa only [← pnat.pow_coe, H, mul_comm _ (k + 1)] }, { replace h := pow_eq_zero h, rw [coe_coe] at h, simpa using hne.1 } } } end /-- If `p` is a prime and `is_cyclotomic_extension {p ^ (k + 1)} K L`, then the discriminant of `hζ.power_basis K` is `(-1) ^ (p ^ k * (p - 1) / 2) * p ^ (p ^ k * ((p - 1) * (k + 1) - 1))` if `irreducible (cyclotomic (p ^ (k + 1)) K))`, and `p ^ (k + 1) ≠ 2`. -/ lemma discr_prime_pow_ne_two' [is_cyclotomic_extension {p ^ (k + 1)} K L] [hp : fact (p : ℕ).prime] (hζ : is_primitive_root ζ ↑(p ^ (k + 1))) (hirr : irreducible (cyclotomic (↑(p ^ (k + 1)) : ℕ) K)) (hk : p ^ (k + 1) ≠ 2) : discr K (hζ.power_basis K).basis = (-1) ^ (((p : ℕ) ^ k * (p - 1)) / 2) * p ^ ((p : ℕ) ^ k * ((p - 1) * (k + 1) - 1)) := by simpa [totient_prime_pow hp.out (succ_pos k)] using discr_prime_pow_ne_two hζ hirr hk /-- If `p` is a prime and `is_cyclotomic_extension {p ^ k} K L`, then the discriminant of `hζ.power_basis K` is `(-1) ^ ((p ^ k).totient / 2) * p ^ (p ^ (k - 1) * ((p - 1) * k - 1))` if `irreducible (cyclotomic (p ^ k) K))`. Beware that in the cases `p ^ k = 1` and `p ^ k = 2` the formula uses `1 / 2 = 0` and `0 - 1 = 0`. It is useful only to have a uniform result. See also `is_cyclotomic_extension.discr_prime_pow_eq_unit_mul_pow`. -/ lemma discr_prime_pow [hcycl : is_cyclotomic_extension {p ^ k} K L] [hp : fact (p : ℕ).prime] (hζ : is_primitive_root ζ ↑(p ^ k)) (hirr : irreducible (cyclotomic (↑(p ^ k) : ℕ) K)) : discr K (hζ.power_basis K).basis = (-1) ^ (((p ^ k : ℕ).totient) / 2) * p ^ ((p : ℕ) ^ (k - 1) * ((p - 1) * k - 1)) := begin unfreezingI { cases k }, { simp only [coe_basis, pow_zero, power_basis_gen, totient_one, mul_zero, mul_one, show 1 / 2 = 0, by refl, discr, trace_matrix], have hζone : ζ = 1 := by simpa using hζ, rw [hζ.power_basis_dim _, hζone, ← (algebra_map K L).map_one, minpoly.eq_X_sub_C_of_algebra_map_inj _ (algebra_map K L).injective, nat_degree_X_sub_C], simp only [trace_matrix, map_one, one_pow, matrix.det_unique, trace_form_apply, mul_one], rw [← (algebra_map K L).map_one, trace_algebra_map, finrank _ hirr], { simp }, { apply_instance }, { exact hcycl } }, { by_cases hk : p ^ (k + 1) = 2, { have hp : p = 2, { rw [← pnat.coe_inj, pnat.coe_bit0, pnat.one_coe, pnat.pow_coe, ← pow_one 2] at hk, replace hk := eq_of_prime_pow_eq (prime_iff.1 hp.out) (prime_iff.1 nat.prime_two) (succ_pos _) hk, rwa [show 2 = ((2 : ℕ+) : ℕ), by simp, pnat.coe_inj] at hk }, rw [hp, ← pnat.coe_inj, pnat.pow_coe, pnat.coe_bit0, pnat.one_coe] at hk, nth_rewrite 1 [← pow_one 2] at hk, replace hk := nat.pow_right_injective rfl.le hk, rw [add_left_eq_self] at hk, simp only [hp, hk, pow_one, pnat.coe_bit0, pnat.one_coe] at hζ, simp only [hp, hk, show 1 / 2 = 0, by refl, coe_basis, pow_one, power_basis_gen, pnat.coe_bit0, pnat.one_coe, totient_two, pow_zero, mul_one, mul_zero], rw [power_basis_dim, hζ.eq_neg_one_of_two_right, show (-1 : L) = algebra_map K L (-1), by simp, minpoly.eq_X_sub_C_of_algebra_map_inj _ (algebra_map K L).injective, nat_degree_X_sub_C], simp only [discr, trace_matrix_apply, matrix.det_unique, fin.default_eq_zero, fin.coe_zero, pow_zero, trace_form_apply, mul_one], rw [← (algebra_map K L).map_one, trace_algebra_map, finrank _ hirr, hp, hk], { simp }, { apply_instance }, { exact hcycl } }, { exact discr_prime_pow_ne_two hζ hirr hk } } end /-- If `p` is a prime and `is_cyclotomic_extension {p ^ k} K L`, then there are `u : ℤˣ` and `n : ℕ` such that the discriminant of `hζ.power_basis K` is `u * p ^ n`. Often this is enough and less cumbersome to use than `is_cyclotomic_extension.discr_prime_pow`. -/ lemma discr_prime_pow_eq_unit_mul_pow [is_cyclotomic_extension {p ^ k} K L] [hp : fact (p : ℕ).prime] (hζ : is_primitive_root ζ ↑(p ^ k)) (hirr : irreducible (cyclotomic (↑(p ^ k) : ℕ) K)) : ∃ (u : ℤˣ) (n : ℕ), discr K (hζ.power_basis K).basis = u * p ^ n := begin rw [discr_prime_pow hζ hirr], by_cases heven : even (((p ^ k : ℕ).totient) / 2), { refine ⟨1, (p : ℕ) ^ (k - 1) * ((p - 1) * k - 1), by simp [heven.neg_one_pow]⟩ }, { exact ⟨-1, (p : ℕ) ^ (k - 1) * ((p - 1) * k - 1), by simp [(odd_iff_not_even.2 heven).neg_one_pow]⟩ }, end /-- If `p` is an odd prime and `is_cyclotomic_extension {p} K L`, then `discr K (hζ.power_basis K).basis = (-1) ^ ((p - 1) / 2) * p ^ (p - 2)` if `irreducible (cyclotomic p K)`. -/ lemma discr_odd_prime [is_cyclotomic_extension {p} K L] [hp : fact (p : ℕ).prime] (hζ : is_primitive_root ζ p) (hirr : irreducible (cyclotomic p K)) (hodd : p ≠ 2) : discr K (hζ.power_basis K).basis = (-1) ^ (((p : ℕ) - 1) / 2) * p ^ ((p : ℕ) - 2) := begin haveI : is_cyclotomic_extension {p ^ (0 + 1)} K L, { rw [zero_add, pow_one], apply_instance }, have hζ' : is_primitive_root ζ ↑(p ^ (0 + 1)) := by simpa using hζ, convert discr_prime_pow_ne_two hζ' (by simpa [hirr]) (by simp [hodd]), { rw [zero_add, pow_one, totient_prime hp.out] }, { rw [pow_zero, one_mul, zero_add, mul_one, nat.sub_sub] } end end is_cyclotomic_extension
f61899636fb4df8aff0ea015730b41c28cd67948
1d265c7dd8cb3d0e1d645a19fd6157a2084c3921
/src/chapter_exercises/chap5_exercises.lean
0e01a6668332a98b06e171f5f66354718affc2b0
[ "MIT" ]
permissive
hanzhi713/lean-proofs
de432372f220d302be09b5ca4227f8986567e4fd
4d8356a878645b9ba7cb036f87737f3f1e68ede5
refs/heads/master
1,585,580,245,658
1,553,646,623,000
1,553,646,623,000
151,342,188
0
1
null
null
null
null
UTF-8
Lean
false
false
980
lean
-- Exercise 1 -- Go back to the exercises in Chapter 3 and Chapter 4 and redo as many as you can now with tactic proofs, using also rw and simp as appropriate. -- Actually I'm already using tactics 😁 -- Use tactic combinators to obtain a one line proof of the following: -- Exercise 2 example (p q r : Prop) (hp : p) : (p ∨ q ∨ r) ∧ (q ∨ p ∨ r) ∧ (q ∨ r ∨ p) := by {split, left, assumption, split, right, left, assumption, right, right, assumption} -- Alternatively, example (p q r : Prop) (hp : p) : (p ∨ q ∨ r) ∧ (q ∨ p ∨ r) ∧ (q ∨ r ∨ p) := by {split, left, assumption, split, repeat {right, {left, assumption} <|> {right, assumption}}} -- Alternatively, meta def left_middle_right : tactic unit := `[ repeat { {left, assumption} <|> right <|> assumption } ] example (p q r : Prop) (hp : p) : (p ∨ q ∨ r) ∧ (q ∨ p ∨ r) ∧ (q ∨ r ∨ p) := by {split, left_middle_right, split, repeat {left_middle_right}}
1bc76652dd026c0afcb8cc1b1691388e1e1fc4ba
471bedbd023d35c9d078c2f936dd577ace7f5813
/library/init/meta/instance_cache.lean
b09f9aa8ae839fbaa18d58a3b47e18efee2e7fdb
[ "Apache-2.0" ]
permissive
lambdaxymox/lean
e06f0fa503666df827edd9867d7f49ca017aae64
fc13c8c72a15dab71a2c2b31410c2cadc3526bd7
refs/heads/master
1,666,785,407,985
1,666,153,673,000
1,666,153,673,000
310,165,986
0
0
Apache-2.0
1,604,542,096,000
1,604,542,095,000
null
UTF-8
Lean
false
false
3,495
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ prelude import init.meta.tactic init.meta.interactive /-! # Instance cache tactics For performance reasons, Lean does not automatically update its database of class instances during a proof. The group of tactics in this file helps to force such updates. -/ open lean.parser open interactive interactive.types local postfix `?`:9001 := optional local postfix *:9001 := many namespace tactic /-- Reset the instance cache for the main goal. -/ meta def reset_instance_cache : tactic unit := do unfreeze_local_instances, freeze_local_instances /-- Unfreeze the local instances while executing `tac` on the main goal. -/ meta def unfreezing {α} (tac : tactic α) : tactic α := focus1 $ unfreeze_local_instances *> tac <* all_goals freeze_local_instances /-- Unfreeze local instances while executing `tac`, if the passed expression is amongst the frozen instances. -/ meta def unfreezing_hyp (h : expr) (tac : tactic unit) : tactic unit := do frozen ← frozen_local_instances, if h ∈ frozen.get_or_else [] then unfreezing tac else tac namespace interactive /-- `unfreezingI { tac }` executes tac while temporarily unfreezing the instance cache. -/ meta def unfreezingI (tac : itactic) := unfreezing tac /-- Reset the instance cache. This allows any new instances added to the context to be used in typeclass inference. -/ meta def resetI := reset_instance_cache /-- Like `revert`, but can also revert instance arguments. -/ meta def revertI (ids : parse ident*) : tactic unit := unfreezingI (revert ids) /-- Like `subst`, but can also substitute in instance arguments. -/ meta def substI (q : parse texpr) : tactic unit := unfreezingI (subst q) /-- Like `cases`, but can also be used with instance arguments. -/ meta def casesI (p : parse cases_arg_p) (q : parse with_ident_list) : tactic unit := unfreezingI (cases p q) /-- Like `intro`, but uses the introduced variable in typeclass inference. -/ meta def introI (p : parse ident_?) : tactic unit := intro p >> reset_instance_cache /-- Like `intros`, but uses the introduced variable(s) in typeclass inference. -/ meta def introsI (p : parse ident_*) : tactic unit := intros p >> reset_instance_cache /-- Used to add typeclasses to the context so that they can be used in typeclass inference. The syntax is the same as `have`. -/ meta def haveI (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse (tk ":=" *> texpr)?) : tactic unit := do h ← match h with | none := get_unused_name "_inst" | some a := return a end, «have» (some h) q₁ q₂, match q₂ with | none := swap >> reset_instance_cache >> swap | some p₂ := reset_instance_cache end /-- Used to add typeclasses to the context so that they can be used in typeclass inference. The syntax is the same as `let`. -/ meta def letI (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) : tactic unit := do h ← match h with | none := get_unused_name "_inst" | some a := return a end, «let» (some h) q₁ q₂, match q₂ with | none := swap >> reset_instance_cache >> swap | some p₂ := reset_instance_cache end /-- Like `exact`, but uses all variables in the context for typeclass inference. -/ meta def exactI (q : parse texpr) : tactic unit := reset_instance_cache >> exact q end interactive end tactic
61d6537ea2f80a7ab78e502b0bb7a06cd1ca32af
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/simpCasesOnCtorBug.lean
8e7c90d3a12b804334f802b5df7865c3b8600746
[ "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
442
lean
import Lean def test1 (y : Nat) : Nat := let x := 3 match x with | 0 => y+1 | .succ x => y + x #eval Lean.Compiler.compile #[``test1] def test2 (y : Nat) : Nat := let x := 3 match x with | 0 => y+1 | .succ x => match x with | 0 => y+2 | .succ x => y + x #eval Lean.Compiler.compile #[``test2] set_option trace.Compiler.result true #eval Lean.Compiler.compile #[``test1] #eval Lean.Compiler.compile #[``test2]
f49b20efecf3789d52d13330c93829c372104b55
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/scope_bug.lean
223572f06f30ca74ab4816d9e95a128b0b67f5c2
[ "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
127
lean
definition s : Type := sorry example (A : Type) (s : A) : A := by exact s example (A : Type) : A → A := by intro s; exact s
1c517f462f4739a192e4290a94aaef7dd36688df
c61b91f85121053c627318ad8fcde30dfb8637d2
/Chapter4/4-1.lean
595c427fe32de7546026844bffa79a2f9bece208
[]
no_license
robkorn/theorem-proving-in-lean-exercises
9e2256360eaf6f8df6cdd8fd656e63dfb04c8cdb
9c51da587105ee047a9db55d52709d881a39be7a
refs/heads/master
1,585,403,341,988
1,540,142,619,000
1,540,142,619,000
148,431,678
2
0
null
null
null
null
UTF-8
Lean
false
false
800
lean
namespace uni variables (α : Type) (p q : α → Prop) example : (∀x : α, p x ∧ q x) → ∀ y : α, p y := λ h : ∀ x : α, p x ∧ q x, λ y : α, show p y, from (h y).left end uni namespace trans variables (α : Type) (r : α → α → Prop) variables trans_r : ∀ {x y z}, r x y → r y z → r x z variables a b c : α variables (hab : r a b) (hbc : r b c) #check trans_r #check trans_r #check trans_r hab #check trans_r hab hbc end trans namespace equivalence variables (α : Type) (r : α → α → Prop) variable refl_r : ∀ x, r x x variable symm_r : ∀ {x y}, r x y → r y x variable trans_r : ∀ {x y z}, r x y → r y z → r x z example (a b c d : α) (hab : r a b) (hcb : r c b) (hcd : r c d) : r a d := trans_r (trans_r hab $ symm_r hcb) hcd end equivalence
0d690f8c83c798d4abd4681ec57718133788b5ef
1a61aba1b67cddccce19532a9596efe44be4285f
/hott/init/nat.hlean
9187f27880e74f35b79840c8ed1568eb8b5ae793
[ "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
10,297
hlean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura -/ prelude import init.wf init.tactic init.hedberg init.util init.types open eq decidable sum lift is_trunc namespace nat notation `ℕ` := nat /- basic definitions on natural numbers -/ inductive le (a : ℕ) : ℕ → Type₀ := | refl : le a a | step : Π {b}, le a b → le a (succ b) infix `≤` := le attribute le.refl [refl] definition lt [reducible] (n m : ℕ) := succ n ≤ m definition ge [reducible] (n m : ℕ) := m ≤ n definition gt [reducible] (n m : ℕ) := succ m ≤ n infix `<` := lt infix `≥` := ge infix `>` := gt definition pred [unfold 1] (a : nat) : nat := nat.cases_on a zero (λ a₁, a₁) -- add is defined in init.num definition sub (a b : nat) : nat := nat.rec_on b a (λ b₁ r, pred r) definition mul (a b : nat) : nat := nat.rec_on b zero (λ b₁ r, r + a) notation a - b := sub a b notation a * b := mul a b /- properties of ℕ -/ protected definition is_inhabited [instance] : inhabited nat := inhabited.mk zero protected definition has_decidable_eq [instance] : ∀ x y : nat, decidable (x = y) | has_decidable_eq zero zero := inl rfl | has_decidable_eq (succ x) zero := inr (by contradiction) | has_decidable_eq zero (succ y) := inr (by contradiction) | has_decidable_eq (succ x) (succ y) := match has_decidable_eq x y with | inl xeqy := inl (by rewrite xeqy) | inr xney := inr (λ h : succ x = succ y, by injection h with xeqy; exact absurd xeqy xney) end /- properties of inequality -/ definition le_of_eq {n m : ℕ} (p : n = m) : n ≤ m := p ▸ le.refl n definition le_succ (n : ℕ) : n ≤ succ n := by repeat constructor definition pred_le (n : ℕ) : pred n ≤ n := by cases n;all_goals (repeat constructor) definition le.trans [trans] {n m k : ℕ} (H1 : n ≤ m) (H2 : m ≤ k) : n ≤ k := by induction H2 with n H2 IH;exact H1;exact le.step IH definition le_succ_of_le {n m : ℕ} (H : n ≤ m) : n ≤ succ m := le.trans H !le_succ definition le_of_succ_le {n m : ℕ} (H : succ n ≤ m) : n ≤ m := le.trans !le_succ H definition le_of_lt {n m : ℕ} (H : n < m) : n ≤ m := le_of_succ_le H definition succ_le_succ [unfold 3] {n m : ℕ} (H : n ≤ m) : succ n ≤ succ m := by induction H;reflexivity;exact le.step v_0 definition pred_le_pred [unfold 3] {n m : ℕ} (H : n ≤ m) : pred n ≤ pred m := by induction H;reflexivity;cases b;exact v_0;exact le.step v_0 definition le_of_succ_le_succ [unfold 3] {n m : ℕ} (H : succ n ≤ succ m) : n ≤ m := pred_le_pred H definition le_succ_of_pred_le [unfold 1] {n m : ℕ} (H : pred n ≤ m) : n ≤ succ m := by cases n;exact le.step H;exact succ_le_succ H definition not_succ_le_self {n : ℕ} : ¬succ n ≤ n := by induction n with n IH;all_goals intros;cases a;apply IH;exact le_of_succ_le_succ a definition zero_le (n : ℕ) : 0 ≤ n := by induction n with n IH;apply le.refl;exact le.step IH definition lt.step {n m : ℕ} (H : n < m) : n < succ m := le.step H definition zero_lt_succ (n : ℕ) : 0 < succ n := by induction n with n IH;apply le.refl;exact le.step IH definition lt.trans [trans] {n m k : ℕ} (H1 : n < m) (H2 : m < k) : n < k := le.trans (le.step H1) H2 definition lt_of_le_of_lt [trans] {n m k : ℕ} (H1 : n ≤ m) (H2 : m < k) : n < k := le.trans (succ_le_succ H1) H2 definition lt_of_lt_of_le [trans] {n m k : ℕ} (H1 : n < m) (H2 : m ≤ k) : n < k := le.trans H1 H2 definition le.antisymm {n m : ℕ} (H1 : n ≤ m) (H2 : m ≤ n) : n = m := begin cases H1 with m' H1', { reflexivity}, { cases H2 with n' H2', { reflexivity}, { exfalso, apply not_succ_le_self, exact lt.trans H1' H2'}}, end definition not_succ_le_zero (n : ℕ) : ¬succ n ≤ zero := by intro H; cases H definition lt.irrefl (n : ℕ) : ¬n < n := not_succ_le_self definition self_lt_succ (n : ℕ) : n < succ n := !le.refl definition lt.base (n : ℕ) : n < succ n := !le.refl definition le_lt_antisymm {n m : ℕ} (H1 : n ≤ m) (H2 : m < n) : empty := !lt.irrefl (lt_of_le_of_lt H1 H2) definition lt_le_antisymm {n m : ℕ} (H1 : n < m) (H2 : m ≤ n) : empty := le_lt_antisymm H2 H1 definition lt.asymm {n m : ℕ} (H1 : n < m) (H2 : m < n) : empty := le_lt_antisymm (le_of_lt H1) H2 definition lt.trichotomy (a b : ℕ) : a < b ⊎ a = b ⊎ b < a := begin revert b, induction a with a IH, { intro b, cases b, exact inr (inl idp), exact inl !zero_lt_succ}, { intro b, cases b with b, exact inr (inr !zero_lt_succ), { cases IH b with H H, exact inl (succ_le_succ H), cases H with H H, exact inr (inl (ap succ H)), exact inr (inr (succ_le_succ H))}} end definition lt.by_cases {a b : ℕ} {P : Type} (H1 : a < b → P) (H2 : a = b → P) (H3 : b < a → P) : P := by induction (lt.trichotomy a b) with H H; exact H1 H; cases H with H H; exact H2 H;exact H3 H definition lt_ge_by_cases {a b : ℕ} {P : Type} (H1 : a < b → P) (H2 : a ≥ b → P) : P := lt.by_cases H1 (λH, H2 (le_of_eq H⁻¹)) (λH, H2 (le_of_lt H)) definition lt_or_ge (a b : ℕ) : (a < b) ⊎ (a ≥ b) := lt_ge_by_cases inl inr definition not_lt_zero (a : ℕ) : ¬ a < zero := by intro H; cases H -- less-than is well-founded definition lt.wf [instance] : well_founded lt := begin constructor, intro n, induction n with n IH, { constructor, intros n H, exfalso, exact !not_lt_zero H}, { constructor, intros m H, assert aux : ∀ {n₁} (hlt : m < n₁), succ n = n₁ → acc lt m, { intros n₁ hlt, induction hlt, { intro p, injection p with q, exact q ▸ IH}, { intro p, injection p with q, exact (acc.inv (q ▸ IH) a)}}, apply aux H idp}, end definition measure {A : Type} (f : A → ℕ) : A → A → Type₀ := inv_image lt f definition measure.wf {A : Type} (f : A → ℕ) : well_founded (measure f) := inv_image.wf f lt.wf definition succ_lt_succ {a b : ℕ} (H : a < b) : succ a < succ b := succ_le_succ H definition lt_of_succ_lt {a b : ℕ} (H : succ a < b) : a < b := le_of_succ_le H definition lt_of_succ_lt_succ {a b : ℕ} (H : succ a < succ b) : a < b := le_of_succ_le_succ H definition decidable_le [instance] : decidable_rel le := begin intros n, induction n with n IH, { intro m, left, apply zero_le}, { intro m, cases m with m, { right, apply not_succ_le_zero}, { let H := IH m, clear IH, cases H with H H, left, exact succ_le_succ H, right, intro H2, exact H (le_of_succ_le_succ H2)}} end definition decidable_lt [instance] : decidable_rel lt := _ definition decidable_gt [instance] : decidable_rel gt := _ definition decidable_ge [instance] : decidable_rel ge := _ definition eq_or_lt_of_le {a b : ℕ} (H : a ≤ b) : a = b ⊎ a < b := by cases H with b' H; exact sum.inl rfl; exact sum.inr (succ_le_succ H) definition le_of_eq_or_lt {a b : ℕ} (H : a = b ⊎ a < b) : a ≤ b := by cases H with H H; exact le_of_eq H; exact le_of_lt H definition eq_or_lt_of_not_lt {a b : ℕ} (hnlt : ¬ a < b) : a = b ⊎ b < a := sum.rec_on (lt.trichotomy a b) (λ hlt, absurd hlt hnlt) (λ h, h) definition lt_succ_of_le {a b : ℕ} (h : a ≤ b) : a < succ b := succ_le_succ h definition lt_of_succ_le {a b : ℕ} (h : succ a ≤ b) : a < b := h definition succ_le_of_lt {a b : ℕ} (h : a < b) : succ a ≤ b := h definition max (a b : ℕ) : ℕ := if a < b then b else a definition min (a b : ℕ) : ℕ := if a < b then a else b definition max_self (a : ℕ) : max a a = a := eq.rec_on !if_t_t rfl definition max_eq_right {a b : ℕ} (H : a < b) : max a b = b := if_pos H definition max_eq_left {a b : ℕ} (H : ¬ a < b) : max a b = a := if_neg H definition eq_max_right {a b : ℕ} (H : a < b) : b = max a b := eq.rec_on (max_eq_right H) rfl definition eq_max_left {a b : ℕ} (H : ¬ a < b) : a = max a b := eq.rec_on (max_eq_left H) rfl definition le_max_left (a b : ℕ) : a ≤ max a b := by_cases (λ h : a < b, le_of_lt (eq.rec_on (eq_max_right h) h)) (λ h : ¬ a < b, eq.rec_on (eq_max_left h) !le.refl) definition le_max_right (a b : ℕ) : b ≤ max a b := by_cases (λ h : a < b, eq.rec_on (eq_max_right h) !le.refl) (λ h : ¬ a < b, sum.rec_on (eq_or_lt_of_not_lt h) (λ heq, eq.rec_on heq (eq.rec_on (inverse (max_self a)) !le.refl)) (λ h : b < a, have aux : a = max a b, from eq_max_left (lt.asymm h), eq.rec_on aux (le_of_lt h))) definition succ_sub_succ_eq_sub (a b : ℕ) : succ a - succ b = a - b := by induction b with b IH; reflexivity; apply ap pred IH definition sub_eq_succ_sub_succ (a b : ℕ) : a - b = succ a - succ b := eq.rec_on (succ_sub_succ_eq_sub a b) rfl definition zero_sub_eq_zero (a : ℕ) : zero - a = zero := nat.rec_on a rfl (λ a₁ (ih : zero - a₁ = zero), ap pred ih) definition zero_eq_zero_sub (a : ℕ) : zero = zero - a := eq.rec_on (zero_sub_eq_zero a) rfl definition sub_lt {a b : ℕ} : zero < a → zero < b → a - b < a := have aux : Π {a}, zero < a → Π {b}, zero < b → a - b < a, from λa h₁, le.rec_on h₁ (λb h₂, le.cases_on h₂ (lt.base zero) (λ b₁ bpos, eq.rec_on (sub_eq_succ_sub_succ zero b₁) (eq.rec_on (zero_eq_zero_sub b₁) (lt.base zero)))) (λa₁ apos ih b h₂, le.cases_on h₂ (lt.base a₁) (λ b₁ bpos, eq.rec_on (sub_eq_succ_sub_succ a₁ b₁) (lt.trans (@ih b₁ bpos) (lt.base a₁)))), λ h₁ h₂, aux h₁ h₂ definition sub_le (a b : ℕ) : a - b ≤ a := nat.rec_on b (le.refl a) (λ b₁ ih, le.trans !pred_le ih) lemma sub_lt_succ (a b : ℕ) : a - b < succ a := lt_succ_of_le (sub_le a b) end nat namespace nat_esimp open nat attribute add mul sub [unfold 2] attribute of_num [unfold 1] end nat_esimp
b29845d6539d5d224d8bc412ba131e0b67941366
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/assoc_flat.lean
b72a625fe76d0058fc77af5e8498054df718037f
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
2,289
lean
open tactic expr meta definition is_op_app (op : expr) (e : expr) : option (expr × expr) := match e with | (app (app fn a1) a2) := if op = fn then some (a1, a2) else none | e := none end meta definition flat_with : expr → expr → expr → expr → tactic (expr × expr) | op assoc e rhs := match (is_op_app op e) with | (some (a1, a2)) := do -- H₁ is a proof for a2 + rhs = rhs₁ (rhs₁, H₁) ← flat_with op assoc a2 rhs, -- H₂ is a proof for a1 + rhs₁ = rhs₂ (new_app, H₂) ← flat_with op assoc a1 rhs₁, -- We need to generate a proof that (a1 + a2) + rhs = a1 + (a2 + rhs) -- H₃ is a proof for (a1 + a2) + rhs = a1 + (a2 + rhs) H₃ ← return $ assoc a1 a2 rhs, -- H₃ is a proof for a1 + (a2 + rhs) = a1 + rhs1 H₄ ← to_expr ``(congr_arg %%(app op a1) %%H₁), H₅ ← to_expr ``(eq.trans %%H₃ %%H₄), H ← to_expr ``(eq.trans %%H₅ %%H₂), return (new_app, H) | none := do new_app ← return $ op e rhs, H ← to_expr ``(eq.refl %%new_app), return (new_app, H) end meta definition flat : expr → expr → expr → tactic (expr × expr) | op assoc e := match (is_op_app op e) with | (some (a1, a2)) := do -- H₁ is a proof that a2 = new_a2 (new_a2, H₁) ← flat op assoc a2, -- H₂ is a proof that a1 + new_a2 = new_app (new_app, H₂) ← flat_with op assoc a1 new_a2, -- We need a proof that (a1 + a2) = new_app -- H₃ is a proof for a1 + a2 = a1 + new_a2 H₃ : expr ← to_expr ``(congr_arg %%(app op a1) %%H₁), H ← to_expr ``(eq.trans %%H₃ %%H₂), return (new_app, H) | none := do pr ← to_expr ``(eq.refl %%e), return (e, pr) end local infix (name := and) `+` := nat.add set_option trace.app_builder true set_option pp.all true example (a b c d e f g : nat) : ((a + b) + c) + ((d + e) + (f + g)) = a + (b + (c + (d + (e + (f + g))))) := by do assoc : expr ← mk_const `nat.add_assoc, op : expr ← mk_const `nat.add, tgt ← target, match is_eq tgt with | (some (lhs, rhs)) := do r ← flat op assoc lhs, exact r.2 | none := failed end
d28509d8aa9dfd7626521d2f5d988f6e08865982
4f065978c49388d188224610d9984673079f7d91
/cauchy_real.lean
a13065983c09d008764bc78dcf4b92b173d022e2
[]
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
75,852
lean
import algebra.archimedean data.rat algebra.module theorem congr_arg₂ {α β γ : Type*} (f : α → β → γ) {x₁ x₂ : α} {y₁ y₂ : β} (Hx : x₁ = x₂) (Hy : y₁ = y₂) : f x₁ y₁ = f x₂ y₂ := eq.drec (eq.drec rfl Hy) Hx section rat theorem lt_two_pow (n : nat) : n < 2 ^ n := nat.rec_on n dec_trivial $ λ n ih, calc n + 1 < 2^n + 1 : nat.add_lt_add_right ih 1 ... ≤ 2^n + 2^n : nat.add_le_add_left (nat.pow_le_pow_of_le_right dec_trivial $ nat.zero_le n) (2^n) ... = 2^n * 2 : eq.symm $ mul_two (2^n) ... = 2^(n+1) : eq.symm $ nat.pow_succ 2 n theorem rat.coe_pow (m n : nat) : (m : ℚ) ^ n = (m^n : ℕ) := nat.rec_on n rfl $ λ n ih, by simp [pow_succ', ih, nat.pow_succ] theorem rat.num_pos_of_pos (r : rat) (H : r > 0) : r.num > 0 := int.cast_lt.1 $ calc (r.num : ℚ) = r.num / (r.denom:ℤ) * r.denom : eq.symm $ div_mul_cancel _ $ ne_of_gt $ nat.cast_pos.2 r.pos ... = r * r.denom : by rw [← rat.mk_eq_div, ← rat.num_denom r] ... > 0 : mul_pos H $ nat.cast_pos.2 r.pos theorem rat.one_le_num_of_pos (r : rat) (H : r > 0) : 1 ≤ (r.num : ℚ) := have H1 : ((0+1:ℤ):ℚ) = (1:ℚ), from rfl, H1 ▸ int.cast_le.2 $ int.add_one_le_of_lt $ rat.num_pos_of_pos r H theorem rat.lt (r : ℚ) (H : r > 0) : (1 / 2^r.denom : ℚ) < r := calc (1 / 2^r.denom : ℚ) < 1 / r.denom : one_div_lt_one_div_of_lt (nat.cast_pos.2 r.pos) (trans_rel_left _ (nat.cast_lt.2 $ lt_two_pow _) (rat.coe_pow 2 _).symm) ... ≤ r.num / r.denom : div_le_div_of_le_of_pos (rat.one_le_num_of_pos r H) (nat.cast_pos.2 r.pos) ... = r.num / (r.denom:ℤ) : rfl ... = r : by rw [← rat.mk_eq_div, ← rat.num_denom r] end rat section list_max_min variables {α : Type*} [decidable_linear_order α] [inhabited α] (L : list α) def list.max : α := list.foldr max (inhabited.default _) L def list.min : α := list.foldr min (inhabited.default _) L theorem list.le_max : ∀ x ∈ L, x ≤ L.max := list.rec_on L (λ _, false.elim) $ λ hd tl ih x hx, or.cases_on hx (assume H : x = hd, H.symm ▸ le_max_left hd tl.max) (assume H : x ∈ tl, le_trans (ih x H) (le_max_right hd tl.max)) theorem list.min_le : ∀ x ∈ L, L.min ≤ x := list.rec_on L (λ _, false.elim) $ λ hd tl ih x hx, or.cases_on hx (assume H : x = hd, H.symm ▸ min_le_left hd tl.min) (assume H : x ∈ tl, le_trans (min_le_right hd tl.min) (ih x H)) end list_max_min instance rat.seq : comm_ring (ℕ → ℚ) := by refine { add := λ f g n, f n + g n, zero := λ n, 0, neg := λ f n, -f n, mul := λ f g n, f n * g n, one := λ n, 1, .. }; { intros, { simp [mul_assoc, mul_add, add_mul] } <|> simp [mul_comm] } def rat.cau_seq : set (ℕ → ℚ) := { f : ℕ → ℚ | ∀ ε > 0, ∃ N, ∀ m > N, ∀ n > N, abs (f m - f n) < ε } namespace rat.cau_seq variables (f g : ℕ → ℚ) (hf : f ∈ rat.cau_seq) (hg : g ∈ rat.cau_seq) theorem add : f + g ∈ rat.cau_seq := λ ε Hε, let ⟨n1, h1⟩ := hf (ε/2) (half_pos Hε) in let ⟨n2, h2⟩ := hg (ε/2) (half_pos Hε) in ⟨max n1 n2, λ m hm n hn, have H1 : _ := h1 m (lt_of_le_of_lt (le_max_left _ _) hm) n (lt_of_le_of_lt (le_max_left _ _) hn), have H2 : _ := h2 m (lt_of_le_of_lt (le_max_right _ _) hm) n (lt_of_le_of_lt (le_max_right _ _) hn), calc abs ((f m + g m) - (f n + g n)) = abs ((f m - f n) + (g m - g n)) : by simp ... ≤ abs (f m - f n) + abs (g m - g n) : abs_add _ _ ... < (ε/2) + (ε/2) : add_lt_add H1 H2 ... = ε : add_halves _⟩ theorem mul : f * g ∈ rat.cau_seq := λ ε Hε, let ⟨n1, h1⟩ := hf ε Hε in let ⟨n2, h2⟩ := hg ε Hε in have H1 : ε + abs (f (n1 + 1)) > 0, from add_pos_of_pos_of_nonneg Hε $ abs_nonneg _, have H2 : ε + abs (g (n2 + 1)) > 0, from add_pos_of_pos_of_nonneg Hε $ abs_nonneg _, let ⟨n3, h3⟩ := hf (ε/2 / (ε + abs (g (n2 + 1)))) (div_pos (half_pos Hε) H2) in let ⟨n4, h4⟩ := hg (ε/2 / (ε + abs (f (n1 + 1)))) (div_pos (half_pos Hε) H1) in ⟨max (max n1 n2) (max n3 n4), λ m hm n hn, have H3 : _ := h1 n (lt_of_le_of_lt (le_trans (le_max_left _ _) (le_max_left _ _)) hn) (n1 + 1) (nat.lt_succ_self n1), have H4 : _ := h2 m (lt_of_le_of_lt (le_trans (le_max_right _ _) (le_max_left _ _)) hm) (n2 + 1) (nat.lt_succ_self n2), have H5 : _ := h3 m (lt_of_le_of_lt (le_trans (le_max_left _ _) (le_max_right _ _)) hm) n (lt_of_le_of_lt (le_trans (le_max_left _ _) (le_max_right _ _)) hn), have H6 : _ := h4 m (lt_of_le_of_lt (le_trans (le_max_right _ _) (le_max_right _ _)) hm) n (lt_of_le_of_lt (le_trans (le_max_right _ _) (le_max_right _ _)) hn), calc abs ((f m * g m) - (f n * g n)) = abs ((f m - f n) * g m + f n * (g m - g n)) : by simp [add_mul, mul_add] ... ≤ abs ((f m - f n) * g m) + abs (f n * (g m - g n)) : abs_add _ _ ... = abs (f m - f n) * abs (g m) + abs (f n) * abs (g m - g n) : by rw [abs_mul, abs_mul] ... = abs (f m - f n) * abs (g m - g (n2 + 1) + g (n2 + 1)) + abs (f n - f (n1 + 1) + f (n1 + 1)) * abs (g m - g n) : by simp ... ≤ abs (f m - f n) * (abs (g m - g (n2 + 1)) + abs (g (n2 + 1))) + (abs (f n - f (n1 + 1)) + abs (f (n1 + 1))) * abs (g m - g n) : add_le_add (mul_le_mul_of_nonneg_left (abs_add _ _) (abs_nonneg _)) (mul_le_mul_of_nonneg_right (abs_add _ _) (abs_nonneg _)) ... ≤ abs (f m - f n) * (ε + abs (g (n2 + 1))) + (ε + abs (f (n1 + 1))) * abs (g m - g n) : add_le_add (mul_le_mul_of_nonneg_left (le_of_lt $ add_lt_add_right H4 _) (abs_nonneg _)) (mul_le_mul_of_nonneg_right (le_of_lt $ add_lt_add_right H3 _) (abs_nonneg _)) ... < (ε/2 / (ε + abs (g (n2 + 1)))) * (ε + abs (g (n2 + 1))) + (ε + abs (f (n1 + 1))) * (ε/2 / (ε + abs (f (n1 + 1)))) : add_lt_add (mul_lt_mul_of_pos_right H5 H2) (mul_lt_mul_of_pos_left H6 H1) ... = ε/2 + ε/2 : by rw [div_mul_cancel _ (ne_of_gt H2), mul_div_cancel' _ (ne_of_gt H1)] ... = ε : add_halves _⟩ theorem neg_one : (-1 : ℕ → ℚ) ∈ rat.cau_seq := λ ε Hε, ⟨0, λ m hm n hn, show abs (-1 - (-1)) < ε, by simpa using Hε⟩ instance : comm_ring rat.cau_seq := by refine { add := λ f g, ⟨f.1 + g.1, add _ _ f.2 g.2⟩, zero := ⟨0, have H : (-1 : ℕ → ℚ) + (-1) * (-1) = 0, by simp, H ▸ add _ _ neg_one $ mul _ _ neg_one neg_one⟩, neg := λ f, ⟨-f.1, have H : (-1) * f.1 = -f.1, by simp, H ▸ mul _ _ neg_one f.2⟩, mul := λ f g, ⟨f.1 * g.1, mul _ _ f.2 g.2⟩, one := ⟨1, have H : (-1 : ℕ → ℚ) * (-1) = 1, by simp, H ▸ mul _ _ neg_one neg_one⟩, .. }; { intros, { simp [mul_assoc, mul_add, add_mul] } <|> simp [mul_comm] } protected def abs : rat.cau_seq := ⟨λ n, abs (f n), λ ε Hε, let ⟨N, HN⟩ := hf ε Hε in ⟨N, λ m hm n hn, lt_of_le_of_lt (abs_abs_sub_abs_le_abs_sub _ _) (HN m hm n hn)⟩⟩ end rat.cau_seq def rat.null : set rat.cau_seq := { f : rat.cau_seq | ∀ ε > 0, ∃ N, ∀ n > N, abs (f.1 n) < ε } namespace rat.null variables (f g : rat.cau_seq) (hf : f ∈ rat.null) (hg : g ∈ rat.null) theorem add : f + g ∈ rat.null := λ ε Hε, let ⟨n1, h1⟩ := hf (ε/2) (half_pos Hε) in let ⟨n2, h2⟩ := hg (ε/2) (half_pos Hε) in ⟨max n1 n2, λ n hn, have H1 : _ := h1 n (lt_of_le_of_lt (le_max_left _ _) hn), have H2 : _ := h2 n (lt_of_le_of_lt (le_max_right _ _) hn), calc abs (f.1 n + g.1 n) ≤ abs (f.1 n) + abs (g.1 n) : abs_add _ _ ... < (ε/2) + (ε/2) : add_lt_add H1 H2 ... = ε : add_halves _⟩ theorem zero : (0 : rat.cau_seq) ∈ rat.null := λ ε Hε, ⟨0, λ n hn, show abs 0 < ε, by simpa using Hε⟩ theorem mul : f * g ∈ rat.null := λ ε Hε, let ⟨n1, h1⟩ := f.2 ε Hε in have H1 : ε + abs (f.1 (n1 + 1)) > 0, from add_pos_of_pos_of_nonneg Hε $ abs_nonneg _, let ⟨n2, h2⟩ := hg (ε / (ε + abs (f.1 (n1 + 1)))) (div_pos Hε H1) in ⟨max n1 n2, λ n hn, have H2 : _ := h1 n (lt_of_le_of_lt (le_max_left _ _) hn) (n1 + 1) (nat.lt_succ_self n1), have H3 : _ := h2 n (lt_of_le_of_lt (le_max_right _ _) hn), calc abs (f.1 n * g.1 n) = abs (f.1 n) * abs (g.1 n) : abs_mul _ _ ... = abs (f.1 n - f.1 (n1 + 1) + f.1 (n1 + 1)) * abs (g.1 n) : by simp ... ≤ (abs (f.1 n - f.1 (n1 + 1)) + abs (f.1 (n1 + 1))) * abs (g.1 n) : mul_le_mul_of_nonneg_right (abs_add _ _) (abs_nonneg _) ... ≤ (ε + abs (f.1 (n1 + 1))) * abs (g.1 n) : mul_le_mul_of_nonneg_right (add_le_add_right (le_of_lt H2) _) (abs_nonneg _) ... < (ε + abs (f.1 (n1 + 1))) * (ε / (ε + abs (f.val (n1 + 1)))) : mul_lt_mul_of_pos_left H3 H1 ... = ε : mul_div_cancel' _ $ ne_of_gt H1⟩ protected theorem abs : rat.cau_seq.abs f.1 f.2 ∈ rat.null := λ ε Hε, let ⟨N, HN⟩ := hf ε Hε in ⟨N, λ n hn, show abs (abs (f.1 n)) < ε, from (abs_abs (f.1 n)).symm ▸ HN n hn⟩ theorem of_abs (HF : rat.cau_seq.abs f.1 f.2 ∈ rat.null) : f ∈ rat.null := λ ε Hε, let ⟨N, HN⟩ := HF ε Hε in ⟨N, λ n hn, (abs_abs (f.1 n)) ▸ HN n hn⟩ local attribute [instance] classical.prop_decidable theorem abs_pos_of_not_null (H : f ∉ rat.null) : ∃ ε > 0, ∃ N, ∀ n > N, abs (f.1 n) > ε := let ⟨ε, Hε⟩ := not_forall.1 H in let ⟨Hε1, Hε2⟩ := not_imp.1 Hε in let ⟨N1, HN1⟩ := f.2 (ε/2) (half_pos Hε1) in let ⟨N2, HN2⟩ := not_forall.1 $ not_exists.1 Hε2 N1 in let ⟨HN3, HN4⟩ := not_imp.1 HN2 in ⟨ε/2, half_pos Hε1, N2, λ n hn, have H : _ := HN1 n (lt_trans HN3 hn) N2 HN3, calc abs (f.1 n) = abs (f.1 N2 - (f.1 N2 - f.1 n)) : congr_arg abs $ eq.symm $ sub_sub_cancel _ _ ... ≥ abs (f.1 N2) - abs (f.1 N2 - f.1 n) : abs_sub_abs_le_abs_sub _ _ ... > ε - ε/2 : sub_lt_sub_of_le_of_lt (le_of_not_gt HN4) (abs_sub (f.1 n) (f.1 N2) ▸ H) ... = ε / 2 : sub_half ε⟩ end rat.null instance real.setoid : setoid rat.cau_seq := ⟨λ f g, f - g ∈ rat.null, λ f, by simpa using rat.null.zero, λ f g hfg, have H : (-1) * (f - g) = g - f, by simp, show g - f ∈ rat.null, from H ▸ rat.null.mul _ _ hfg, λ f g h h1 h2, have H : (f - g) + (g - h) = f - h, by simp, show f - h ∈ rat.null, from H ▸ rat.null.add _ _ h1 h2⟩ def real : Type := quotient real.setoid theorem real.eq_of_eq : ∀ {x y : rat.cau_seq}, x = y → ⟦x⟧ = ⟦y⟧ | _ _ rfl := quotient.sound $ setoid.refl _ instance real.comm_ring : comm_ring real := by refine { add := λ x y, quotient.lift_on₂ x y (λ f g, ⟦f + g⟧) $ λ f1 f2 g1 g2 hf hg, quotient.sound $ have H : (f1 - g1) + (f2 - g2) = (f1 + f2) - (g1 + g2), by simp, show (f1 + f2) - (g1 + g2) ∈ rat.null, from H ▸ rat.null.add _ _ hf hg, zero := ⟦0⟧, neg := λ x, quotient.lift_on x (λ f, ⟦-f⟧) $ λ f1 f2 hf, quotient.sound $ have H : (-1) * (f1 - f2) = (-f1) - (-f2), by simp, show (-f1) - (-f2) ∈ rat.null, from H ▸ rat.null.mul _ _ hf, mul := λ x y, quotient.lift_on₂ x y (λ f g, ⟦f * g⟧) $ λ f1 f2 g1 g2 hf hg, quotient.sound $ have H : f2 * (f1 - g1) + g1 * (f2 - g2) = f1 * f2 - g1 * g2, by simp [mul_add, add_mul, mul_comm], show f1 * f2 - g1 * g2 ∈ rat.null, from H ▸ rat.null.add _ _ (rat.null.mul _ _ hf) (rat.null.mul _ _ hg), one := ⟦1⟧, .. }; { intros, try { apply quotient.induction_on a, intro f }, try { apply quotient.induction_on b, intro g }, try { apply quotient.induction_on c, intro h }, apply real.eq_of_eq, { simp [mul_assoc, mul_add, add_mul] } <|> simp [mul_comm] } namespace rat.cau_seq variables (f g h : ℕ → ℚ) def lt : Prop := ∃ ε > 0, ∃ N, ∀ n > N, f n + ε < g n protected theorem lt_trans (H1 : lt f g) (H2 : lt g h) : lt f h := let ⟨ε1, Hε1, N1, HN1⟩ := H1 in let ⟨ε2, Hε2, N2, HN2⟩ := H2 in ⟨ε1, Hε1, max N1 N2, λ n hn, have H3 : n > N1 := (lt_of_le_of_lt (le_max_left _ _) hn), have H4 : n > N2 := (lt_of_le_of_lt (le_max_right _ _) hn), calc f n + ε1 < g n : HN1 n H3 ... < g n + ε2 : lt_add_of_pos_right _ Hε2 ... < h n : HN2 n H4⟩ theorem lt_asymm (H1 : lt f g) (H2 : lt g f) : false := let ⟨ε1, Hε1, N1, HN1⟩ := H1 in let ⟨ε2, Hε2, N2, HN2⟩ := H2 in have H1 : _ := HN1 (N1 + N2 + 1) (nat.succ_le_succ $ nat.le_add_right _ _), have H2 : _ := HN2 (N1 + N2 + 1) (nat.succ_le_succ $ nat.le_add_left _ _), lt_asymm (lt_trans (lt_add_of_pos_right _ Hε1) H1) (lt_trans (lt_add_of_pos_right _ Hε2) H2) protected theorem add_lt_add_left (H : lt g h) : lt (f + g) (f + h) := let ⟨ε, Hε, N, HN⟩ := H in ⟨ε, Hε, N, λ n hn, show f n + g n + ε < f n + h n, from (add_assoc (f n) (g n) ε).symm ▸ add_lt_add_left (HN n hn) _⟩ protected theorem mul_pos (Hf : lt 0 f) (Hg : lt 0 g) : lt 0 (f * g) := let ⟨ε1, Hε1, N1, HN1⟩ := Hf in let ⟨ε2, Hε2, N2, HN2⟩ := Hg in ⟨ε1 * ε2, mul_pos Hε1 Hε2, max N1 N2, λ n hn, have H1 : n > N1 := (lt_of_le_of_lt (le_max_left _ _) hn), have H2 : n > N2 := (lt_of_le_of_lt (le_max_right _ _) hn), have H3 : ε1 < f n := (zero_add ε1) ▸ HN1 n H1, have H4 : ε2 < g n := (zero_add ε2) ▸ HN2 n H2, show 0 + ε1 * ε2 < f n * g n, from (zero_add $ ε1 * ε2).symm ▸ mul_lt_mul H3 (le_of_lt H4) Hε2 (le_of_lt (lt_trans Hε1 H3))⟩ theorem pos_or_neg_of_not_null (f : rat.cau_seq) (H : f ∉ rat.null) : lt 0 f.1 ∨ lt f.1 0 := let ⟨ε, Hε, N1, HN1⟩ := rat.null.abs_pos_of_not_null f H in let ⟨N2, HN2⟩ := f.2 (ε/2) (half_pos Hε) in have H1 : _ := HN1 (N1 + N2 + 1) (nat.succ_le_succ $ nat.le_add_right _ _), or.cases_on (lt_max_iff.1 H1) (assume H : ε < f.1 (N1 + N2 + 1), or.inl ⟨ε/2, half_pos Hε, N1 + N2, λ n hn, have H2 : _ := HN2 n (lt_of_le_of_lt (nat.le_add_left _ _) hn) (N1 + N2 + 1) (nat.succ_le_succ $ nat.le_add_left _ _), calc 0 + ε/2 = ε/2 : zero_add _ ... = ε - ε/2 : eq.symm $ sub_half ε ... < f.1 (N1 + N2 + 1) - -(f.1 n - f.1 (N1 + N2 + 1)) : sub_lt_sub H (neg_lt.1 (abs_lt.1 H2).1) ... = f.1 (N1 + N2 + 1) + (f.1 n - f.1 (N1 + N2 + 1)) : sub_neg_eq_add _ _ ... = f.1 n : add_sub_cancel'_right _ _⟩) (assume H : ε < -f.1 (N1 + N2 + 1), or.inr ⟨ε/2, half_pos Hε, N1 + N2, λ n hn, have H2 : _ := HN2 n (lt_of_le_of_lt (nat.le_add_left _ _) hn) (N1 + N2 + 1) (nat.succ_le_succ $ nat.le_add_left _ _), calc f.1 n + ε/2 = f.1 n - f.1 (N1 + N2 + 1) + f.1 (N1 + N2 + 1) + ε/2 : by rw sub_add_cancel ... < ε/2 + -ε + ε/2 : add_lt_add_right (add_lt_add (abs_lt.1 H2).2 (lt_neg_of_lt_neg H)) _ ... = ε/2 + ε/2 + -ε : add_right_comm _ _ _ ... = 0 : by rw [add_halves, add_neg_self]⟩) variables {f1 f2 g1 g2 : rat.cau_seq} variables (hf : f1 ≈ g1) variables (hg : f2 ≈ g2) theorem lt_of_lt (H : lt f1.1 f2.1) : lt g1.1 g2.1 := let ⟨ε, Hε, N, HN⟩ := H in let ⟨N1, HN1⟩ := hf (ε/2/2) (half_pos $ half_pos Hε) in let ⟨N2, HN2⟩ := hg (ε/2/2) (half_pos $ half_pos Hε) in ⟨ε/2, half_pos Hε, max N (max N1 N2), λ n hn, have H1 : _ := HN n (lt_of_le_of_lt (le_max_left _ _) hn), have H2 : _ := HN1 n (lt_of_le_of_lt (le_trans (le_max_left _ _) (le_max_right _ _)) hn), have H3 : _ := HN2 n (lt_of_le_of_lt (le_trans (le_max_right _ _) (le_max_right _ _)) hn), calc g1.1 n + ε/2 = f1.1 n - (f1.1 n - g1.1 n) + ε/2 : by rw sub_sub_cancel ... < f1.1 n - -(ε/2/2) + ε/2 : add_lt_add_right (sub_lt_sub_left (abs_lt.1 H2).1 _) _ ... = f1.1 n + (ε/2/2 + (ε/2/2 + ε/2/2) + ε/2/2 - ε/2/2) : by rw [sub_neg_eq_add, add_halves, add_sub_cancel, add_assoc] ... = f1.1 n + ((ε/2/2 + ε/2/2) + (ε/2/2 + ε/2/2) - ε/2/2) : by repeat {rw add_assoc} ... = f1.1 n + (ε - ε/2/2) : by repeat {rw add_halves} ... = (f1.1 n + ε) - ε/2/2 : eq.symm $ add_sub_assoc _ _ _ ... < f2.1 n - (f2.1 n - g2.1 n) : sub_lt_sub H1 (abs_lt.1 H3).2 ... = g2.1 n : sub_sub_cancel _ _⟩ end rat.cau_seq namespace real def lt (x y : real) : Prop := quotient.lift_on₂ x y (λ f g, rat.cau_seq.lt f.1 g.1) $ λ f1 f2 g1 g2 hf hg, propext ⟨λ H, rat.cau_seq.lt_of_lt hf hg H, λ H, rat.cau_seq.lt_of_lt (setoid.symm hf) (setoid.symm hg) H⟩ protected theorem lt_trans (x y z : real) : lt x y → lt y z → lt x z := quotient.induction_on₃ x y z $ λ f g h H1 H2, rat.cau_seq.lt_trans f.1 g.1 h.1 H1 H2 theorem lt_asymm (x y : real) : lt x y → lt y x → false := quotient.induction_on₂ x y $ λ f g H1 H2, rat.cau_seq.lt_asymm f.1 g.1 H1 H2 theorem lt_trichotomy (x y : real) : lt x y ∨ x = y ∨ lt y x := classical.by_cases (assume h : x = y, or.inr $ or.inl h) $ quotient.induction_on₂ x y $ λ f g, assume h : ⟦f⟧ ≠ ⟦g⟧, or.cases_on (rat.cau_seq.pos_or_neg_of_not_null (f - g) (λ H, h $ quotient.sound H)) (assume H : rat.cau_seq.lt 0 (f - g), or.inr $ or.inr $ have H1 : _ := rat.cau_seq.add_lt_add_left g _ _ H, by simpa using H1) (assume H : rat.cau_seq.lt (f - g) 0, or.inl $ have H1 : _ := rat.cau_seq.add_lt_add_left g _ _ H, by simpa using H1) theorem mul_pos (x y : real) : lt 0 x → lt 0 y → lt 0 (x * y) := quotient.induction_on₂ x y $ λ f g hf hg, rat.cau_seq.mul_pos f g hf hg end real instance real.partial_order : partial_order real := { lt := real.lt, le := λ x y, real.lt x y ∨ x = y, lt_iff_le_not_le := λ x y, ⟨assume hxy : real.lt x y, ⟨or.inl hxy, assume hyx : real.lt y x ∨ y = x, real.lt_asymm x y hxy $ or.cases_on hyx id $ λ H, by subst H; from hxy⟩, assume hxy : _, or.cases_on hxy.1 (assume H : real.lt x y, H) (assume H : x = y, false.elim $ hxy.2 $ or.inr $ eq.symm H)⟩, le_refl := λ x, or.inr rfl, le_trans := λ x y z hxy hyz, or.cases_on hxy (assume hxy : real.lt x y, or.cases_on hyz (assume hyz : real.lt y z, or.inl $ real.lt_trans _ _ _ hxy hyz) (assume hyz : y = z, hyz ▸ or.inl hxy)) (assume hxy : x = y, hxy.symm ▸ hyz), le_antisymm := λ x y hxy hyx, or.cases_on hxy (assume hxy : real.lt x y, or.cases_on hyx (assume hyx : real.lt y x, false.elim $ real.lt_asymm _ _ hxy hyx) (assume hyx : y = x, eq.symm hyx)) (assume hxy : x = y, hxy) } instance real.linear_ordered_comm_ring : linear_ordered_comm_ring real := { add_le_add_left := λ x y hxy c, (quotient.induction_on₃ x y c $ λ f g h hfg, or.cases_on hfg (assume hfg : real.lt ⟦f⟧ ⟦g⟧, or.inl $ rat.cau_seq.add_lt_add_left _ _ _ hfg) (assume hfg : ⟦f⟧ = ⟦g⟧, or.inr $ hfg ▸ rfl)) hxy, add_lt_add_left := λ x y hxy c, (quotient.induction_on₃ x y c $ λ f g h hfg, rat.cau_seq.add_lt_add_left _ _ _ hfg) hxy, zero_lt_one := ⟨0.5, dec_trivial, 0, λ n hn, dec_trivial⟩, mul_nonneg := λ x y hx hy, or.cases_on hx (assume hx : 0 < x, or.cases_on hy (assume hy : 0 < y, or.inl $ real.mul_pos _ _ hx hy) (assume hy : 0 = y, or.inr $ eq.symm $ hy ▸ mul_zero _)) (assume hx : 0 = x, or.inr $ eq.symm $ hx ▸ zero_mul _), mul_pos := real.mul_pos, le_total := λ x y, or.cases_on (real.lt_trichotomy x y) (assume hxy : x < y, or.inl $ or.inl hxy) (assume hxy, or.cases_on hxy (assume hxy : x = y, or.inl $ or.inr hxy) (assume hxy : y < x, or.inr $ or.inl hxy)), zero_ne_one := λ H, let ⟨N, HN⟩ := quotient.exact H.symm 0.5 dec_trivial in absurd (HN (N + 1) (nat.lt_succ_self N)) dec_trivial, .. real.comm_ring, .. real.partial_order } instance real.inhabited : inhabited real := ⟨0⟩ namespace rat.cau_seq variables (f : rat.cau_seq) (Hf : f ∉ rat.null) theorem inv.of_not_null : (λ n, 1 / f.1 n) ∈ rat.cau_seq := λ ε Hε, let ⟨ε', Hε', N1, HN1⟩ := rat.null.abs_pos_of_not_null f Hf in let ⟨N2, HN2⟩ := f.2 (ε * ε' * ε') (mul_pos (mul_pos Hε Hε') Hε') in ⟨max N1 N2, λ m hm n hn, have H : _ := HN2 n (lt_of_le_of_lt (le_max_right _ _) hn) m (lt_of_le_of_lt (le_max_right _ _) hm), have H1 : _ := HN1 m (lt_of_le_of_lt (le_max_left _ _) hm), have H2 : _ := HN1 n (lt_of_le_of_lt (le_max_left _ _) hn), have H3 : abs (f.1 m) > 0 := lt_trans Hε' H1, have H4 : abs (f.1 n) > 0 := lt_trans Hε' H2, calc abs (1 / f.1 m - 1 / f.1 n) = abs ((1 / f.1 m) * (f.1 n - f.1 m) * (1 / f.1 n)) : congr_arg abs $ eq.symm $ one_div_mul_sub_mul_one_div_eq_one_div_add_one_div (ne_zero_of_abs_ne_zero $ ne_of_gt H3) (ne_zero_of_abs_ne_zero $ ne_of_gt H4) ... = (1 / abs (f.1 m)) * abs (f.1 n - f.1 m) * (1 / abs (f.1 n)) : by rw [abs_mul, abs_mul, abs_one_div, abs_one_div] ... < (1 / ε') * (ε * ε' * ε') * (1 / ε') : mul_lt_mul (mul_lt_mul' (one_div_le_one_div_of_le Hε' $ le_of_lt H1) H (abs_nonneg _) (one_div_pos_of_pos Hε')) (one_div_le_one_div_of_le Hε' $ le_of_lt H2) (one_div_pos_of_pos H4) (le_of_lt $ mul_pos (one_div_pos_of_pos Hε') (mul_pos (mul_pos Hε Hε') Hε')) ... = ε : by rw [mul_assoc, mul_assoc, mul_one_div_cancel (ne_of_gt Hε'), mul_one, mul_comm, mul_assoc, mul_one_div_cancel (ne_of_gt Hε'), mul_one]⟩ def inv : rat.cau_seq := ⟨_, inv.of_not_null f Hf⟩ variables (g : rat.cau_seq) (Hg : g ∉ rat.null) theorem inv.well_defined (H : f - g ∈ rat.null) : inv f Hf - inv g Hg ∈ rat.null := λ ε Hε, let ⟨ε1, Hε1, N1, HN1⟩ := rat.null.abs_pos_of_not_null f Hf in let ⟨ε2, Hε2, N2, HN2⟩ := rat.null.abs_pos_of_not_null g Hg in let ⟨N, HN⟩ := H (ε * ε1 * ε2) (mul_pos (mul_pos Hε Hε1) Hε2) in ⟨max N (max N1 N2), λ n hn, have H1 : _ := HN1 n (lt_of_le_of_lt (le_trans (le_max_left _ _) (le_max_right _ _)) hn), have H2 : _ := HN2 n (lt_of_le_of_lt (le_trans (le_max_right _ _) (le_max_right _ _)) hn), have H3 : _ := HN n (lt_of_le_of_lt (le_max_left _ _) hn), calc abs (1 / f.1 n - 1 / g.1 n) = abs ((1 / f.1 n) * (g.1 n - f.1 n) * (1 / g.1 n)) : congr_arg abs $ eq.symm $ one_div_mul_sub_mul_one_div_eq_one_div_add_one_div (ne_zero_of_abs_ne_zero $ ne_of_gt (lt_trans Hε1 H1)) (ne_zero_of_abs_ne_zero $ ne_of_gt (lt_trans Hε2 H2)) ... = (1 / abs (f.1 n)) * abs (f.1 n - g.1 n) * (1 / abs (g.1 n)) : by rw [abs_mul, abs_mul, abs_one_div, abs_one_div, abs_sub] ... < (1 / ε1) * (ε * ε1 * ε2) * (1 / ε2) : mul_lt_mul (mul_lt_mul' (one_div_le_one_div_of_le Hε1 $ le_of_lt H1) H3 (abs_nonneg _) (one_div_pos_of_pos Hε1)) (one_div_le_one_div_of_le Hε2 $ le_of_lt H2) (one_div_pos_of_pos (lt_trans Hε2 H2)) (le_of_lt $ mul_pos (one_div_pos_of_pos Hε1) (mul_pos (mul_pos Hε Hε1) Hε2)) ... = ε : by rw [mul_assoc, mul_assoc, mul_one_div_cancel (ne_of_gt Hε2), mul_one, mul_comm, mul_assoc, mul_one_div_cancel (ne_of_gt Hε1), mul_one]⟩ theorem mul_inv_cancel : f * inv f Hf - 1 ∈ rat.null := let ⟨ε, Hε, N, HN⟩ := rat.null.abs_pos_of_not_null f Hf in λ ε' Hε', ⟨N, λ n hn, have H1 : abs (f.1 n) ≠ 0, from ne_of_gt $ lt_trans Hε $ HN n hn, have H2 : f.1 n ≠ 0, from H1 ∘ abs_eq_zero.2, have H3 : f.1 n * (1 / f.1 n) - 1 = 0, by rw [mul_one_div_cancel H2, sub_self], calc abs (f.1 n * (1 / f.1 n) - 1) = 0 : abs_eq_zero.2 H3 ... < ε' : Hε'⟩ end rat.cau_seq namespace real -- short circuits instance : has_zero real := by apply_instance instance : has_one real := by apply_instance instance : has_add real := by apply_instance instance : has_neg real := by apply_instance instance : has_mul real := by apply_instance instance : has_scalar real real := by apply_instance instance : add_comm_group real := by apply_instance instance : add_comm_semigroup real := by apply_instance instance : add_comm_monoid real := by apply_instance instance : add_group real := by apply_instance instance : add_left_cancel_semigroup real := by apply_instance instance : add_monoid real := by apply_instance instance : add_right_cancel_semigroup real := by apply_instance instance : add_semigroup real := by apply_instance instance : char_zero real := by apply_instance instance : comm_semigroup real := by apply_instance instance : comm_monoid real := by apply_instance instance : distrib real := by apply_instance instance : domain real := by apply_instance instance : integral_domain real := by apply_instance instance : linear_order real := by apply_instance instance : linear_ordered_ring real := by apply_instance instance : linear_ordered_semiring real := by apply_instance instance : module real real := by apply_instance instance : monoid real := by apply_instance instance : mul_zero_class real := by apply_instance instance : no_bot_order real := by apply_instance instance : no_top_order real := by apply_instance instance : no_zero_divisors real := by apply_instance instance : ordered_cancel_comm_monoid real := by apply_instance instance : ordered_comm_monoid real := by apply_instance instance : ordered_comm_group real := by apply_instance instance : ordered_ring real := by apply_instance instance : ordered_semiring real := by apply_instance instance : ring real := by apply_instance instance : semigroup real := by apply_instance instance : semiring real := by apply_instance instance : zero_ne_one_class real := by apply_instance local attribute [instance] classical.prop_decidable noncomputable def inv (x : real) : real := quotient.lift_on x (λ f, dite _ (assume H : f ∈ rat.null, (0 : real)) (assume H : f ∉ rat.null, ⟦rat.cau_seq.inv f H⟧)) $ λ f g hfg, dite _ (assume H : f ∈ rat.null, have H1 : f + (-1) * (f - g) = g := by rw [neg_one_mul, ← sub_eq_add_neg, sub_sub_cancel], have H2 : g ∈ rat.null, from H1 ▸ rat.null.add _ _ H (rat.null.mul _ _ hfg), (dif_pos H).trans (dif_pos H2).symm) (assume H : f ∉ rat.null, have H1 : g + (f - g) = f := add_sub_cancel'_right _ _, have H2 : g ∉ rat.null, from λ h, H $ H1 ▸ rat.null.add _ _ h hfg, (dif_neg H).trans $ eq.symm $ (dif_neg H2).trans $ eq.symm $ quotient.sound $ rat.cau_seq.inv.well_defined _ _ _ _ hfg) theorem mul_inv_cancel {x : real} : x ≠ 0 → x * real.inv x = 1 := quotient.induction_on x $ λ f hf, have H : f ∉ rat.null, from λ h, hf $ quotient.sound $ show f - 0 ∈ rat.null, from (sub_zero f).symm ▸ h, (congr_arg _ (dif_neg H)).trans $ quotient.sound $ rat.cau_seq.mul_inv_cancel _ _ noncomputable instance : discrete_linear_ordered_field real := { inv := real.inv, mul_inv_cancel := @real.mul_inv_cancel, inv_mul_cancel := λ x, (mul_comm _ _).trans ∘ real.mul_inv_cancel, decidable_le := λ _ _, classical.prop_decidable _, inv_zero := dif_pos rat.null.zero, .. real.linear_ordered_comm_ring } end real theorem real.mk_eq_coe_nat (q : ℕ) : (q : real) = ⟦⟨λ n, q, λ ε Hε, ⟨0, λ m hm n hn, show abs (q - q : ℚ) < ε, from (sub_self (q:ℚ)).symm ▸ (@abs_zero ℚ _).symm ▸ Hε⟩⟩⟧ := nat.rec_on q rfl $ λ n ih, show (n : real) + 1 = _, by rw ih; refl theorem real.mk_eq_coe_int (q : ℤ) : (q : real) = ⟦⟨λ n, q, λ ε Hε, ⟨0, λ m hm n hn, show abs (q - q : ℚ) < ε, from (sub_self (q:ℚ)).symm ▸ (@abs_zero ℚ _).symm ▸ Hε⟩⟩⟧ := int.cases_on q real.mk_eq_coe_nat $ λ n, show -((n : real) + 1) = _, by rw real.mk_eq_coe_nat; refl theorem rat.mk_eq_coe_nat (n : ℕ) : (n : ℚ) = rat.mk' n 1 dec_trivial (nat.coprime_one_right n) := nat.rec_on n rfl $ λ n ih, show (n : ℚ) + rat.mk' 1 1 _ _ = _, by rw [ih]; dsimp [(+), (1:ℚ)]; unfold rat.add; unfold rat.mk_pnat; dsimp; congr; simp [-add_comm]; refl theorem rat.mk_eq_coe_int (n : ℤ) : (n : ℚ) = rat.mk' n 1 dec_trivial (nat.coprime_one_right (int.nat_abs n)) := int.cases_on n rat.mk_eq_coe_nat $ λ i, show -((i:ℚ) + rat.mk' 1 1 _ _) = _, by rw rat.mk_eq_coe_nat; dsimp [(+), has_neg.neg, (1:ℚ)]; unfold rat.add; unfold rat.mk_pnat; unfold rat.neg; dsimp; congr; simp; rw [add_comm, int.neg_succ_of_nat_coe]; simp theorem real.mk_eq_coe_rat (q : ℚ) : (q : real) = ⟦⟨λ n, q, λ ε Hε, ⟨0, λ m hm n hn, show abs (q - q) < ε, from (sub_self q).symm ▸ (@abs_zero ℚ _).symm ▸ Hε⟩⟩⟧ := rat.cases_on q $ λ n d p c, (div_eq_iff_mul_eq $ ne_of_gt $ (@nat.cast_pos real _ d).2 p).2 $ by rw [real.mk_eq_coe_int, real.mk_eq_coe_nat]; apply real.eq_of_eq; apply subtype.eq; funext m; dsimp; change rat.mk' n d p c * d = (n:ℚ); rw [rat.mk_eq_coe_nat, rat.mk_eq_coe_int]; dsimp [(*)]; unfold rat.mul; unfold rat.mk_pnat; simp; split; rw [nat.gcd_eq_right]; [rw int.mul_div_cancel, simp [int.nat_abs_mul], rw nat.div_self p, simp [int.nat_abs_mul]]; apply ne_of_gt; apply int.coe_nat_pos.2 p theorem real.rat_eq_of_coe_eq {x y : ℚ} (H : (x : real) = y) : x = y := rat.cast_inj.1 H theorem real.rat_lt_of_coe_lt {x y : ℚ} (H : (x : real) < y) : x < y := rat.cast_lt.1 H theorem real.ex_rat_lt (x : real) : ∃ q : ℚ, (q : real) < x := quotient.induction_on x $ λ f, let ⟨N, HN⟩ := f.2 1 zero_lt_one in ⟨f.1 (N + 1) - 2, trans_rel_right _ (real.mk_eq_coe_rat _) ⟨1, zero_lt_one, N, λ n hn, calc f.1 (N + 1) - 2 + 1 = f.1 (N + 1) - (2 - 1) : sub_add _ _ _ ... = f.1 (N + 1) - 1 : congr_arg _ $ add_sub_cancel _ _ ... = f.1 (N + 1) - f.1 n + f.1 n - 1 : by rw sub_add_cancel ... < 1 + f.1 n - 1 : sub_lt_sub_right (add_lt_add_right (abs_lt.1 $ HN _ (nat.lt_succ_self N) _ hn).2 _) _ ... = f.1 n : add_sub_cancel' _ _⟩⟩ theorem real.ex_lt_rat (x : real) : ∃ q : ℚ, x < (q : real) := quotient.induction_on x $ λ f, let ⟨N, HN⟩ := f.2 1 zero_lt_one in ⟨f.1 (N + 1) + 2, by rw real.mk_eq_coe_rat; from ⟨1, zero_lt_one, N, λ n hn, calc f.1 n + 1 = f.1 n - f.1 (N + 1) + f.1 (N + 1) + 1 : by rw sub_add_cancel ... < 1 + f.1 (N + 1) + 1 : add_lt_add_right (add_lt_add_right (abs_lt.1 $ HN _ hn _ (nat.lt_succ_self N)).2 _) _ ... = f.1 (N + 1) + 2 : by rw [add_comm (1:ℚ), add_assoc]; refl⟩⟩ instance real.archimedean : archimedean real := archimedean_iff_rat_lt.2 real.ex_lt_rat noncomputable instance real.floor_ring : floor_ring real := archimedean.floor_ring real section completeness local attribute [instance] classical.prop_decidable parameters (A : set real) (x ub : real) parameters (H1 : x ∈ A) (H2 : ∀ x ∈ A, x ≤ ub) noncomputable def bin_div : ℕ → ℚ × ℚ | 0 := (classical.some $ real.ex_rat_lt x, classical.some $ real.ex_lt_rat ub) | (n+1) := if ∀ x ∈ A, x < (((bin_div n).1 + (bin_div n).2)/2 : ℚ) then ((bin_div n).1, ((bin_div n).1 + (bin_div n).2)/2) else (((bin_div n).1 + (bin_div n).2)/2, (bin_div n).2) theorem bin_div.snd_sub_fst (n : nat) : (bin_div n).2 - (bin_div n).1 = ((bin_div 0).2 - (bin_div 0).1) / 2^n := nat.rec_on n (div_one _).symm $ λ n ih, if H : ∀ x ∈ A, x < (((bin_div n).1 + (bin_div n).2)/2 : ℚ) then have H1 : bin_div (n+1) = ((bin_div n).1, ((bin_div n).1 + (bin_div n).2)/2), by dsimp [bin_div]; rw [if_pos H], calc (bin_div (n+1)).2 - (bin_div (n+1)).1 = (((bin_div n).1 + (bin_div n).2) - ((bin_div n).1 + (bin_div n).1))/2 : by rw [H1, sub_div, add_self_div_two] ... = ((bin_div n).2 - (bin_div n).1)/2 : by rw add_sub_add_left_eq_sub ... = (((bin_div 0).2 - (bin_div 0).1) / 2^n) / 2 : by rw ih ... = ((bin_div 0).2 - (bin_div 0).1) / 2^(n+1) : by rw [div_div_eq_div_mul, pow_add]; refl else have H1 : bin_div (n+1) = (((bin_div n).1 + (bin_div n).2)/2, (bin_div n).2), by dsimp [bin_div]; rw [if_neg H], calc (bin_div (n+1)).2 - (bin_div (n+1)).1 = (((bin_div n).2 + (bin_div n).2) - ((bin_div n).1 + (bin_div n).2))/2 : by rw [H1, sub_div, add_self_div_two] ... = ((bin_div n).2 - (bin_div n).1)/2 : by rw add_sub_add_right_eq_sub ... = (((bin_div 0).2 - (bin_div 0).1) / 2^n) / 2 : by rw ih ... = ((bin_div 0).2 - (bin_div 0).1) / 2^(n+1) : by rw [div_div_eq_div_mul, pow_add]; refl theorem bin_div.zero : (bin_div 0).1 < (bin_div 0).2 := real.rat_lt_of_coe_lt $ calc ((bin_div 0).1 : real) < x : classical.some_spec $ real.ex_rat_lt x ... ≤ ub : H2 x H1 ... < (bin_div 0).2 : classical.some_spec $ real.ex_lt_rat ub theorem bin_div.fst_lt_snd_self (n : nat) : (bin_div n).1 < (bin_div n).2 := lt_of_sub_pos $ trans_rel_left _ (div_pos (sub_pos_of_lt bin_div.zero) $ pow_pos two_pos _) (bin_div.snd_sub_fst n).symm theorem bin_div.lt_snd (r) (hr : r ∈ A) (n : nat) : r < ((bin_div n).2 : real) := nat.rec_on n (calc r ≤ ub : H2 r hr ... < (bin_div 0).2 : classical.some_spec $ real.ex_lt_rat ub) $ λ n ih, if H : ∀ x ∈ A, x < (((bin_div n).1 + (bin_div n).2)/2 : ℚ) then have H1 : (bin_div (n+1)).2 = ((bin_div n).1 + (bin_div n).2)/2, by dsimp [bin_div]; rw [if_pos H], trans_rel_left _ (H r hr) $ congr_arg _ H1.symm else have H1 : (bin_div (n+1)).2 = (bin_div n).2, by dsimp [bin_div]; rw [if_neg H], trans_rel_left _ ih $ congr_arg _ H1.symm theorem bin_div.ex_fst_le (n : nat) : ∃ x ∈ A, ((bin_div n).1 : real) ≤ x := nat.rec_on n ⟨x, H1, le_of_lt $ classical.some_spec $ real.ex_rat_lt x⟩ $ λ n ih, if H : ∀ x ∈ A, x < (((bin_div n).1 + (bin_div n).2)/2 : ℚ) then have H1 : (bin_div (n+1)).1 = (bin_div n).1, by dsimp [bin_div]; rw [if_pos H], let ⟨y, hy1, hy2⟩ := ih in ⟨y, hy1, trans_rel_right _ (congr_arg _ H1) hy2⟩ else have H1 : (bin_div (n+1)).1 = ((bin_div n).1 + (bin_div n).2)/2, by dsimp [bin_div]; rw [if_neg H], by simpa [not_forall, H1] using H theorem bin_div.fst_le_succ (n : nat) : (bin_div n).1 ≤ (bin_div (n+1)).1 := if H : ∀ x ∈ A, x < (((bin_div n).1 + (bin_div n).2)/2 : ℚ) then have H1 : (bin_div (n+1)).1 = (bin_div n).1, by dsimp [bin_div]; rw [if_pos H], le_of_eq H1.symm else have H1 : (bin_div (n+1)).1 = ((bin_div n).1 + (bin_div n).2)/2, by dsimp [bin_div]; rw [if_neg H], trans_rel_left _ (trans_rel_right _ (add_self_div_two _).symm $ (div_le_div_right two_pos).2 $ add_le_add_left (le_of_lt $ bin_div.fst_lt_snd_self n) _) H1.symm theorem bin_div.snd_le_succ (n : nat) : (bin_div (n+1)).2 ≤ (bin_div n).2 := if H : ∀ x ∈ A, x < (((bin_div n).1 + (bin_div n).2)/2 : ℚ) then have H1 : (bin_div (n+1)).2 = ((bin_div n).1 + (bin_div n).2)/2, by dsimp [bin_div]; rw [if_pos H], trans_rel_right _ H1 $ le_of_lt $ trans_rel_left _ ((div_lt_div_right two_pos).2 $ add_lt_add_right (bin_div.fst_lt_snd_self n) _) (add_self_div_two _) else have H1 : (bin_div (n+1)).2 = (bin_div n).2, by dsimp [bin_div]; rw [if_neg H], le_of_eq H1 theorem bin_div.fst_le : ∀ n m : nat, n ≤ m → (bin_div n).1 ≤ (bin_div m).1 | _ _ (nat.less_than_or_equal.refl n) := le_refl _ | n (m+1) (nat.less_than_or_equal.step H) := le_trans (bin_div.fst_le n m H) (bin_div.fst_le_succ m) theorem bin_div.snd_le : ∀ n m : nat, n ≤ m → (bin_div m).2 ≤ (bin_div n).2 | _ _ (nat.less_than_or_equal.refl n) := le_refl _ | n (m+1) (nat.less_than_or_equal.step H) := le_trans (bin_div.snd_le_succ m) (bin_div.snd_le n m H) theorem bin_div.fst_lt_snd (n m : nat) : (bin_div n).1 < (bin_div m).2 := or.cases_on (@nat.le_total m n) (assume H : m ≤ n, lt_of_lt_of_le (bin_div.fst_lt_snd_self n) $ bin_div.snd_le m n H) (assume H : n ≤ m, lt_of_le_of_lt (bin_div.fst_le n m H) (bin_div.fst_lt_snd_self m)) theorem bin_div.snd_sub_fst_le : ∀ n m : nat, n ≤ m → (bin_div n).2 - (bin_div m).1 ≤ ((bin_div 0).2 - (bin_div 0).1) / 2^n | _ _ (nat.less_than_or_equal.refl n) := le_of_eq $ bin_div.snd_sub_fst n | n (m+1) (nat.less_than_or_equal.step H) := le_trans (sub_le_sub_left (bin_div.fst_le_succ m) _) (bin_div.snd_sub_fst_le n m H) theorem bin_div.fst_sub_fst_lt_pow (n m : nat) : (bin_div n).1 - (bin_div m).1 < ((bin_div 0).2 - (bin_div 0).1) / 2^m := trans_rel_left _ (sub_lt_sub_right (bin_div.fst_lt_snd n m) _) (bin_div.snd_sub_fst m) theorem bin_div.snd_sub_snd_lt_pow (n m : nat) : (bin_div n).2 - (bin_div m).2 < ((bin_div 0).2 - (bin_div 0).1) / 2^n := trans_rel_left _ (sub_lt_sub_left (bin_div.fst_lt_snd n m) _) (bin_div.snd_sub_fst n) theorem bin_div.fst_cau_seq : prod.fst ∘ bin_div ∈ rat.cau_seq := λ ε Hε, let N := (ε / ((bin_div 0).2 - (bin_div 0).1)).denom in ⟨N, λ m hm n hn, have H1 : (bin_div m).1 - (bin_div (N+1)).1 ≥ 0, from sub_nonneg_of_le $ bin_div.fst_le (N+1) m hm, have H2 : (bin_div n).1 - (bin_div (N+1)).1 ≥ 0, from sub_nonneg_of_le $ bin_div.fst_le (N+1) n hn, calc abs ((bin_div m).1 - (bin_div n).1) ≤ abs ((bin_div m).1 - (bin_div (N+1)).1) + abs ((bin_div (N+1)).1 - (bin_div n).1) : abs_sub_le _ _ _ ... = abs ((bin_div m).1 - (bin_div (N+1)).1) + abs ((bin_div n).1 - (bin_div (N+1)).1) : by rw abs_sub (bin_div n).1 ... = ((bin_div m).1 - (bin_div (N+1)).1) + ((bin_div n).1 - (bin_div (N+1)).1) : by rw [abs_of_nonneg H1, abs_of_nonneg H2] ... < _ : add_lt_add (bin_div.fst_sub_fst_lt_pow m (N+1)) (bin_div.fst_sub_fst_lt_pow n (N+1)) ... = ((bin_div 0).2 - (bin_div 0).1) / (2^N) : by rw [pow_succ', ← div_div_eq_div_mul, add_halves] ... = ((bin_div 0).2 - (bin_div 0).1) * (1 / (2^N)) : div_eq_mul_one_div _ _ ... < ((bin_div 0).2 - (bin_div 0).1) * (ε / ((bin_div 0).2 - (bin_div 0).1)) : mul_lt_mul_of_pos_left (rat.lt _ $ div_pos Hε $ sub_pos_of_lt bin_div.zero) (sub_pos_of_lt bin_div.zero) ... = ε : mul_div_cancel' _ $ ne_of_gt $ sub_pos_of_lt bin_div.zero⟩ theorem bin_div.snd_cau_seq : prod.snd ∘ bin_div ∈ rat.cau_seq := λ ε Hε, let N := (ε / ((bin_div 0).2 - (bin_div 0).1)).denom in ⟨N, λ m hm n hn, have H1 : (bin_div (N+1)).2 - (bin_div m).2 ≥ 0, from sub_nonneg_of_le $ bin_div.snd_le (N+1) m hm, have H2 : (bin_div (N+1)).2 - (bin_div n).2 ≥ 0, from sub_nonneg_of_le $ bin_div.snd_le (N+1) n hn, calc abs ((bin_div m).2 - (bin_div n).2) ≤ abs ((bin_div m).2 - (bin_div (N+1)).2) + abs ((bin_div (N+1)).2 - (bin_div n).2) : abs_sub_le _ _ _ ... = abs ((bin_div (N+1)).2 - (bin_div m).2) + abs ((bin_div (N+1)).2 - (bin_div n).2) : by rw abs_sub ... = ((bin_div (N+1)).2 - (bin_div m).2) + ((bin_div (N+1)).2 - (bin_div n).2) : by rw [abs_of_nonneg H1, abs_of_nonneg H2] ... < _ : add_lt_add (bin_div.snd_sub_snd_lt_pow (N+1) m) (bin_div.snd_sub_snd_lt_pow (N+1) n) ... = ((bin_div 0).2 - (bin_div 0).1) / (2^N) : by rw [pow_succ', ← div_div_eq_div_mul, add_halves] ... = ((bin_div 0).2 - (bin_div 0).1) * (1 / (2^N)) : div_eq_mul_one_div _ _ ... < ((bin_div 0).2 - (bin_div 0).1) * (ε / ((bin_div 0).2 - (bin_div 0).1)) : mul_lt_mul_of_pos_left (rat.lt _ $ div_pos Hε $ sub_pos_of_lt bin_div.zero) (sub_pos_of_lt bin_div.zero) ... = ε : mul_div_cancel' _ $ ne_of_gt $ sub_pos_of_lt bin_div.zero⟩ noncomputable def sup : real := ⟦⟨prod.snd ∘ bin_div, bin_div.snd_cau_seq⟩⟧ theorem bin_div.sup_le_snd (n : nat) : sup ≤ (bin_div n).2 := (real.mk_eq_coe_rat (bin_div n).2).symm ▸ (le_of_not_gt $ λ ⟨ε, Hε, N, HN⟩, lt_asymm (HN (n+N+1) (nat.succ_le_succ $ nat.le_add_left _ _)) (lt_of_le_of_lt (bin_div.snd_le n (n+N+1) (nat.le_add_right n (N+1))) (lt_add_of_pos_right _ Hε))) theorem bin_div.mk_fst_eq_sup : (⟦⟨prod.fst ∘ bin_div, bin_div.fst_cau_seq⟩⟧ : real) = sup := quotient.sound $ λ ε Hε, let N := (ε / ((bin_div 0).2 - (bin_div 0).1)).denom in ⟨N, λ n hn, have H1 : (2:ℚ)^n > (2:ℚ)^N, from calc (2:ℚ)^n = ((2^n:ℕ):ℚ) : rat.coe_pow 2 n ... > ((2^N:ℕ):ℚ) : nat.cast_lt.2 $ nat.pow_lt_pow_of_lt_right (nat.lt_succ_self 1) hn ... = (2:ℚ)^N : eq.symm $ rat.coe_pow 2 N, have H2 : (2:ℚ)^n > (2:ℚ)^N, from calc (2:ℚ)^n = ((2^n:ℕ):ℚ) : rat.coe_pow 2 n ... > ((2^N:ℕ):ℚ) : nat.cast_lt.2 $ nat.pow_lt_pow_of_lt_right (nat.lt_succ_self 1) hn ... = (2:ℚ)^N : eq.symm $ rat.coe_pow 2 N, calc abs ((bin_div n).1 - (bin_div n).2) = abs ((bin_div n).2 - (bin_div n).1) : abs_sub _ _ ... = (bin_div n).2 - (bin_div n).1 : abs_of_pos $ sub_pos_of_lt $ bin_div.fst_lt_snd_self n ... = ((bin_div 0).2 - (bin_div 0).1) / 2^n : bin_div.snd_sub_fst n ... < ((bin_div 0).2 - (bin_div 0).1) / 2^N : (div_lt_div_left (sub_pos_of_lt bin_div.zero) (pow_pos two_pos _) (pow_pos two_pos _)).2 H1 ... = ((bin_div 0).2 - (bin_div 0).1) * (1 / (2^N)) : div_eq_mul_one_div _ _ ... < ((bin_div 0).2 - (bin_div 0).1) * (ε / ((bin_div 0).2 - (bin_div 0).1)) : mul_lt_mul_of_pos_left (rat.lt _ $ div_pos Hε $ sub_pos_of_lt bin_div.zero) (sub_pos_of_lt bin_div.zero) ... = ε : mul_div_cancel' _ $ ne_of_gt $ sub_pos_of_lt bin_div.zero⟩ theorem le_sup (r : real) : r ∈ A → r ≤ sup := quotient.induction_on r $ λ f hf, le_of_not_gt $ λ ⟨ε1, Hε1, N1, HN1⟩, let ⟨N2, HN2⟩ := f.2 (ε1/2) (half_pos Hε1) in let ⟨ε3, Hε3, N3, HN3⟩ := trans_rel_left _ (bin_div.lt_snd _ hf (N1+N2+1)) (real.mk_eq_coe_rat _) in have H1 : _ := HN1 (N1+N2+1) (nat.succ_le_succ $ nat.le_add_right _ _), have H2 : _ := HN2 (N1+N2+1) (nat.succ_le_succ $ nat.le_add_left _ _) (N2+N3+1) (nat.succ_le_succ $ nat.le_add_right _ _), have H3 : _ := HN3 (N2+N3+1) (nat.succ_le_succ $ nat.le_add_left _ _), lt_irrefl _ $ calc f.1 (N2+N3+1) + ε1 < f.1 (N2+N3+1) + ε3 + ε1 : add_lt_add_right (lt_add_of_pos_right _ Hε3) _ ... < (bin_div (N1+N2+1)).2 + ε1 : add_lt_add_right H3 _ ... < f.1 (N1+N2+1) : H1 ... = f.1 (N2+N3+1) + (f.1 (N1+N2+1) - f.1 (N2+N3+1)) : eq.symm $ add_sub_cancel'_right _ _ ... < f.1 (N2+N3+1) + ε1/2 : add_lt_add_left (abs_lt.1 H2).2 _ ... < f.1 (N2+N3+1) + ε1 : add_lt_add_left (half_lt_self Hε1) _ theorem sup_le (r : real) : (∀ x ∈ A, x ≤ r) → sup ≤ r := quotient.induction_on r $ λ f hf, bin_div.mk_fst_eq_sup ▸ (le_of_not_gt $ λ ⟨ε, Hε, N1, HN1⟩, let ⟨N2, HN2⟩ := f.2 (ε/2) (half_pos Hε) in let ⟨r, hr1, hr2⟩ := bin_div.ex_fst_le (N1+N2+1) in have H1 : _ := HN1 (N1+N2+1) (nat.succ_le_succ $ nat.le_add_right _ _), not_lt_of_ge (le_trans hr2 $ hf r hr1) $ (real.mk_eq_coe_rat (bin_div (N1+N2+1)).1).symm ▸ ⟨ε/2, half_pos Hε, N2, λ n hn, have H2 : _ := HN2 n hn (N1+N2+1) (nat.succ_le_succ $ nat.le_add_left _ _), calc f.1 n + ε/2 = f.1 (N1+N2+1) + (f.1 n - f.1 (N1+N2+1)) + ε/2 : by rw add_sub_cancel'_right ... < f.1 (N1+N2+1) + ε/2 + ε/2 : add_lt_add_right (add_lt_add_left (abs_lt.1 $ H2).2 _) _ ... = f.1 (N1+N2+1) + (ε/2 + ε/2) : add_assoc _ _ _ ... = f.1 (N1+N2+1) + ε : by rw add_halves ... < (bin_div (N1+N2+1)).1 : H1⟩) theorem ex_sup_sub_le (ε : real) (Hε : ε > 0) : ∃ r ∈ A, sup - ε ≤ r := classical.by_contradiction $ λ H, have H3 : sup ≤ sup - ε, from sup_le (sup - ε) $ λ r hr, le_of_not_le $ not_bex.1 H r hr, not_lt_of_le H3 $ sub_lt_self sup Hε end completeness section inf parameters (A : set real) (x lb : real) parameters (H1 : x ∈ A) (H2 : ∀ x ∈ A, lb ≤ x) noncomputable def inf : real := -sup (has_neg.neg ⁻¹' A) (-x) (-lb) (show -(-x) ∈ A, from (neg_neg x).symm ▸ H1) (λ r hr, le_neg_of_le_neg $ H2 (-r) hr) theorem inf_le (r : real) (H : r ∈ A) : inf ≤ r := neg_le_of_neg_le $ le_sup _ _ _ _ _ (-r) $ show -(-r) ∈ A, from (neg_neg r).symm ▸ H theorem le_inf (r : real) (H : ∀ x ∈ A, r ≤ x) : r ≤ inf := le_neg_of_le_neg $ sup_le _ _ _ _ _ (-r) $ λ x hx, le_neg_of_le_neg $ H (-x) hx end inf instance real.seq : comm_ring (ℕ → real) := by refine { add := λ f g n, f n + g n, zero := λ n, 0, neg := λ f n, -f n, mul := λ f g n, f n * g n, one := λ n, 1, .. }; { intros, { simp [mul_assoc, mul_add, add_mul] } <|> simp [mul_comm] } namespace real def cau_seq : set (ℕ → real) := { f | ∀ ε > 0, ∃ N, ∀ m > N, ∀ n > N, abs (f m - f n) < ε } noncomputable def lim_sup (f : ℕ → real) (ub lb : real) (H1 : ∀ m, f m ≤ ub) (H2 : ∀ m, ∃ k ≥ m, lb ≤ f m) : real := inf { x | ∃ m, x = sup { y | ∃ k ≥ m, y = f k } (f m) ub ⟨m, nat.le_refl _, rfl⟩ (λ r ⟨k, hr1, hr2⟩, hr2.symm ▸ H1 k) } _ lb ⟨0, rfl⟩ (λ r ⟨m, hr⟩, let ⟨k, hk1, hk2⟩ := H2 m in le_trans hk2 $ hr.symm ▸ le_sup _ _ _ _ _ _ ⟨m, nat.le_refl _, rfl⟩) variable (f : cau_seq) theorem cau_seq.ub : ∃ ub, ∀ n, f.1 n ≤ ub := let ⟨N, HN⟩ := f.2 1 zero_lt_one in ⟨max (((list.range (N+1)).map f.1).max) (f.1 (N+1) + 1), λ n, or.cases_on (nat.lt_or_ge n (N+1)) (assume H : n < N + 1, le_max_left_of_le $ list.le_max _ _ $ list.mem_map_of_mem _ $ list.mem_range.2 H) (assume H : n ≥ N + 1, le_max_right_of_le $ le_of_lt $ calc f.1 n = f.1 (N+1) + (f.1 n - f.1 (N+1)) : eq.symm $ add_sub_cancel'_right _ _ ... < f.1 (N+1) + 1 : add_lt_add_left (abs_lt.1 $ HN _ (nat.lt_of_succ_le H) (N+1) (nat.lt_succ_self _)).2 _ )⟩ theorem cau_seq.lb : ∃ lb, ∀ n, lb ≤ f.1 n := let ⟨N, HN⟩ := f.2 1 zero_lt_one in ⟨min (((list.range (N+1)).map f.1).min) (f.1 (N+1) - 1), λ n, or.cases_on (nat.lt_or_ge n (N+1)) (assume H : n < N + 1, min_le_left_of_le $ list.min_le _ _ $ list.mem_map_of_mem _ $ list.mem_range.2 H) (assume H : n ≥ N + 1, min_le_right_of_le $ le_of_lt $ calc f.1 n = f.1 (N+1) + (f.1 n - f.1 (N+1)) : eq.symm $ add_sub_cancel'_right _ _ ... > f.1 (N+1) + -1 : add_lt_add_left (abs_lt.1 $ HN _ (nat.lt_of_succ_le H) (N+1) (nat.lt_succ_self _)).1 _ )⟩ noncomputable def lim : real := lim_sup f.1 (classical.some $ cau_seq.ub f) (classical.some $ cau_seq.lb f) (classical.some_spec $ cau_seq.ub f) (λ m, ⟨m, nat.le_refl _, classical.some_spec (cau_seq.lb f) m⟩) def close (f : ℕ → real) (L : real) : Prop := ∀ ε > 0, ∃ N, ∀ n > N, abs (f n - L) < ε theorem lim_close : close f.1 (lim f) := λ ε Hε, let ⟨N, HN⟩ := f.2 (ε/2) (half_pos Hε) in ⟨N, λ n hn, abs_lt_of_lt_of_neg_lt (have H1 : f.1 n - ε/2 ≤ lim f, from le_inf _ _ _ _ _ _ $ λ r ⟨m, hr⟩, hr.symm ▸ le_trans (le_of_lt $ sub_lt_of_sub_lt (abs_lt.1 $ HN n hn _ $ lt_of_lt_of_le hn $ le_max_right _ _).2) (le_sup _ _ _ _ _ _ ⟨max m n, le_max_left _ _, rfl⟩), sub_lt_of_sub_lt $ lt_of_lt_of_le (sub_lt_sub_left (half_lt_self Hε) _) H1) (calc -(f.1 n - lim f) = lim f - f.1 n : neg_sub _ _ ... ≤ _ - f.1 n : sub_le_sub_right (inf_le _ _ _ _ _ _ ⟨n, rfl⟩) _ ... ≤ (f.1 n + ε/2) - f.1 n : sub_le_sub_right (sup_le _ _ _ _ _ _ $ λ r ⟨k, hk1, hk2⟩, hk2.symm ▸ le_of_lt $ lt_add_of_sub_left_lt (abs_lt.1 $ HN k (gt_of_ge_of_gt hk1 hn) n hn).2) _ ... = ε/2 : add_sub_cancel' _ _ ... < ε : half_lt_self Hε)⟩ theorem lim_def (L : real) : lim f = L ↔ close f.1 L := iff.intro (assume H : lim f = L, H ▸ lim_close f) (assume H : close f.1 L, classical.by_contradiction $ assume H1 : lim f ≠ L, have H2 : abs (lim f - L) > 0, from abs_pos_of_ne_zero $ mt eq_of_sub_eq_zero H1, let ⟨N1, HN1⟩ := H _ (half_pos H2) in let ⟨N2, HN2⟩ := lim_close f _ (half_pos H2) in have H3 : _ := HN1 (N1 + N2 + 1) (nat.succ_le_succ $ nat.le_add_right _ _), have H4 : _ := HN2 (N1 + N2 + 1) (nat.succ_le_succ $ nat.le_add_left _ _), not_lt_of_ge (abs_sub_le (lim f) (f.1 (N1+N2+1)) L) $ calc abs (lim f - f.1 (N1+N2+1)) + abs (f.1 (N1+N2+1) - L) = abs (f.1 (N1+N2+1) - lim f) + abs (f.1 (N1+N2+1) - L) : by rw abs_sub ... < _ + _ : add_lt_add H4 H3 ... = _ : add_halves _) def cau_seq.of_close (f : ℕ → real) (H : ∃ L, close f L) : cau_seq := ⟨f, λ ε Hε, let ⟨L, H⟩ := H in let ⟨N, HN⟩ := H (ε/2) (half_pos Hε) in ⟨N, λ m hm n hn, calc abs (f m - f n) ≤ abs (f m - L) + abs (L - f n) : abs_sub_le _ _ _ ... = abs (f m - L) + abs (f n - L) : by rw abs_sub (f n) ... < ε/2 + ε/2 : add_lt_add (HN m hm) (HN n hn) ... = ε : add_halves ε⟩⟩ theorem lim.of_close (f : ℕ → real) (L : real) (H : close f L) : lim (cau_seq.of_close f ⟨L, H⟩) = L := (lim_def _ _).2 H theorem ex_seq_to_sup (A : set real) (x ub : real) (H1 : x ∈ A) (H2 : ∀ x ∈ A, x ≤ ub) : ∃ f : cau_seq, (∀ n, f.1 n ∈ A) ∧ lim f = sup A x ub H1 H2 := have H3 : ∀ n : ℕ, ∃ r ∈ A, sup A x ub H1 H2 - (1/(n+1)) ≤ r, from λ n, ex_sup_sub_le _ _ _ _ _ _ (one_div_pos_of_pos $ (@nat.cast_lt real _ 0 (n+1)).2 $ nat.zero_lt_succ n), ⟨cau_seq.of_close (λ n, classical.some $ H3 n) ⟨sup A x ub H1 H2, λ ε Hε, ⟨int.nat_abs ⌈1/ε⌉, λ n hn, let r := classical.some $ H3 n in let (⟨hr1, hr2⟩ : ∃ (H : r ∈ A), sup A x ub H1 H2 - _ ≤ r) := classical.some_spec $ H3 n in have H4 : ⌈1/ε⌉ > 0, from lt_ceil.2 $ one_div_pos_of_pos Hε, calc abs (r - sup A x ub H1 H2) = abs (sup A x ub H1 H2 - r) : abs_sub _ _ ... = sup A x ub H1 H2 - r : abs_of_nonneg $ sub_nonneg_of_le $ le_sup _ _ _ _ _ _ hr1 ... ≤ 1/(n+1) : sub_le_of_sub_le hr2 ... < 1/(int.nat_abs ⌈1/ε⌉:int) : one_div_lt_one_div_of_lt ((@nat.cast_lt real _ 0 _).2 $ int.nat_abs_pos_of_ne_zero $ ne_of_gt H4) ((@nat.cast_lt real _ _ (n+1)).2 $ lt_trans hn $ nat.lt_succ_self n) ... = 1/⌈1/ε⌉ : by rw int.nat_abs_of_nonneg (le_of_lt H4) ... ≤ 1/(1/ε) : (div_le_div_left (@zero_lt_one real _) ((@int.cast_lt real _ 0 _).2 H4) (one_div_pos_of_pos Hε)).2 $ le_ceil _ ... = ε : one_div_one_div _⟩⟩, λ n, (classical.some_spec $ H3 n).fst, lim.of_close _ _ _⟩ end real namespace real.cau_seq variables (r : real) (f g : ℕ → real) (hf : f ∈ real.cau_seq) (hg : g ∈ real.cau_seq) theorem add : f + g ∈ real.cau_seq := λ ε Hε, let ⟨n1, h1⟩ := hf (ε/2) (half_pos Hε) in let ⟨n2, h2⟩ := hg (ε/2) (half_pos Hε) in ⟨max n1 n2, λ m hm n hn, have H1 : _ := h1 m (lt_of_le_of_lt (le_max_left _ _) hm) n (lt_of_le_of_lt (le_max_left _ _) hn), have H2 : _ := h2 m (lt_of_le_of_lt (le_max_right _ _) hm) n (lt_of_le_of_lt (le_max_right _ _) hn), calc abs ((f m + g m) - (f n + g n)) = abs ((f m - f n) + (g m - g n)) : by simp ... ≤ abs (f m - f n) + abs (g m - g n) : abs_add _ _ ... < (ε/2) + (ε/2) : add_lt_add H1 H2 ... = ε : add_halves _⟩ theorem mul : f * g ∈ real.cau_seq := λ ε Hε, let ⟨n1, h1⟩ := hf ε Hε in let ⟨n2, h2⟩ := hg ε Hε in have H1 : ε + abs (f (n1 + 1)) > 0, from add_pos_of_pos_of_nonneg Hε $ abs_nonneg _, have H2 : ε + abs (g (n2 + 1)) > 0, from add_pos_of_pos_of_nonneg Hε $ abs_nonneg _, let ⟨n3, h3⟩ := hf (ε/2 / (ε + abs (g (n2 + 1)))) (div_pos (half_pos Hε) H2) in let ⟨n4, h4⟩ := hg (ε/2 / (ε + abs (f (n1 + 1)))) (div_pos (half_pos Hε) H1) in ⟨max (max n1 n2) (max n3 n4), λ m hm n hn, have H3 : _ := h1 n (lt_of_le_of_lt (le_trans (le_max_left _ _) (le_max_left _ _)) hn) (n1 + 1) (nat.lt_succ_self n1), have H4 : _ := h2 m (lt_of_le_of_lt (le_trans (le_max_right _ _) (le_max_left _ _)) hm) (n2 + 1) (nat.lt_succ_self n2), have H5 : _ := h3 m (lt_of_le_of_lt (le_trans (le_max_left _ _) (le_max_right _ _)) hm) n (lt_of_le_of_lt (le_trans (le_max_left _ _) (le_max_right _ _)) hn), have H6 : _ := h4 m (lt_of_le_of_lt (le_trans (le_max_right _ _) (le_max_right _ _)) hm) n (lt_of_le_of_lt (le_trans (le_max_right _ _) (le_max_right _ _)) hn), calc abs ((f m * g m) - (f n * g n)) = abs ((f m - f n) * g m + f n * (g m - g n)) : by simp [add_mul, mul_add] ... ≤ abs ((f m - f n) * g m) + abs (f n * (g m - g n)) : abs_add _ _ ... = abs (f m - f n) * abs (g m) + abs (f n) * abs (g m - g n) : by rw [abs_mul, abs_mul] ... = abs (f m - f n) * abs (g m - g (n2 + 1) + g (n2 + 1)) + abs (f n - f (n1 + 1) + f (n1 + 1)) * abs (g m - g n) : by simp ... ≤ abs (f m - f n) * (abs (g m - g (n2 + 1)) + abs (g (n2 + 1))) + (abs (f n - f (n1 + 1)) + abs (f (n1 + 1))) * abs (g m - g n) : add_le_add (mul_le_mul_of_nonneg_left (abs_add _ _) (abs_nonneg _)) (mul_le_mul_of_nonneg_right (abs_add _ _) (abs_nonneg _)) ... ≤ abs (f m - f n) * (ε + abs (g (n2 + 1))) + (ε + abs (f (n1 + 1))) * abs (g m - g n) : add_le_add (mul_le_mul_of_nonneg_left (le_of_lt $ add_lt_add_right H4 _) (abs_nonneg _)) (mul_le_mul_of_nonneg_right (le_of_lt $ add_lt_add_right H3 _) (abs_nonneg _)) ... < (ε/2 / (ε + abs (g (n2 + 1)))) * (ε + abs (g (n2 + 1))) + (ε + abs (f (n1 + 1))) * (ε/2 / (ε + abs (f (n1 + 1)))) : add_lt_add (mul_lt_mul_of_pos_right H5 H2) (mul_lt_mul_of_pos_left H6 H1) ... = ε/2 + ε/2 : by rw [div_mul_cancel _ (ne_of_gt H2), mul_div_cancel' _ (ne_of_gt H1)] ... = ε : add_halves _⟩ theorem neg_one : (-1 : ℕ → real) ∈ real.cau_seq := λ ε Hε, ⟨0, λ m hm n hn, show abs (-1 - (-1)) < ε, by simpa using Hε⟩ theorem real_mul : (λ n, r * f n) ∈ real.cau_seq := λ ε Hε, classical.by_cases (assume H : r = 0, ⟨0, λ m hm n hn, by simpa [H] using Hε⟩) (assume H : r ≠ 0, let ⟨N, HN⟩ := hf (ε / abs r) (div_pos Hε $ abs_pos_iff.2 H) in ⟨N, λ m hm n hn, calc abs (r * f m - r * f n) = abs (r * (f m - f n)) : congr_arg _ $ eq.symm $ mul_sub _ _ _ ... = abs r * abs (f m - f n) : abs_mul _ _ ... < abs r * (ε / abs r) : mul_lt_mul_of_pos_left (HN m hm n hn) $ abs_pos_iff.2 H ... = ε : mul_div_cancel' _ $ mt eq_zero_of_abs_eq_zero H⟩) theorem neg : -f ∈ real.cau_seq := (neg_one_mul f) ▸ mul _ _ neg_one hf theorem sub : f - g ∈ real.cau_seq := add _ _ hf (neg g hg) theorem one : (1 : ℕ → real) ∈ real.cau_seq := (neg_neg (1 : ℕ → real)) ▸ neg _ neg_one theorem shift (k : nat) : (λ n, f (n + k)) ∈ real.cau_seq := λ ε Hε, let ⟨N, HN⟩ := hf ε Hε in ⟨N, λ m hm n hn, HN (m + k) (lt_of_lt_of_le hm $ nat.le_add_right m k) (n + k) (lt_of_lt_of_le hn $ nat.le_add_right n k)⟩ theorem of_increasing_of_bounded_above (H1 : ∃ N, ∀ n > N, f n ≤ f (n + 1)) (H2 : ∃ L N, ∀ n > N, f n ≤ L) : f ∈ real.cau_seq := let ⟨N1, H3⟩ := H1 in let ⟨L, N2, H4⟩ := H2 in have H5 : ∀ m > N1, ∀ n, m ≤ n → f m ≤ f n, from λ m hm n H, @nat.less_than_or_equal.rec_on m (λ b, f m ≤ f b) n H (le_refl _) $ λ b H ih, le_trans ih $ H3 b $ lt_of_lt_of_le hm H, subtype.property $ of_close f $ let S := { x | ∃ n > max N1 N2, f n = x } in let M := sup S (f (max N1 N2 + 1)) L ⟨_, nat.lt_succ_self _, rfl⟩ $ λ x ⟨n, hn1, hn2⟩, hn2 ▸ H4 n $ lt_of_le_of_lt (le_max_right _ _) hn1 in ⟨M, λ ε Hε, let (⟨r, ⟨N3, HN3, HN4⟩, hr2⟩ : ∃ r ∈ S, M - (ε/2) ≤ r) := ex_sup_sub_le _ _ _ _ _ (ε/2) (half_pos Hε) in ⟨N3, λ n hn, have hn1 : f n ≤ M, from le_sup _ _ _ _ _ _ ⟨_, lt_trans HN3 hn, rfl⟩, have hn2 : M - f n ≤ ε/2, from sub_le_of_sub_le $ le_trans hr2 $ HN4 ▸ (H5 N3 (lt_of_le_of_lt (le_max_left _ _) HN3) n $ le_of_lt hn), abs_sub_lt_iff.2 ⟨lt_of_le_of_lt (sub_nonpos_of_le hn1) Hε, lt_of_le_of_lt hn2 $ half_lt_self Hε⟩⟩⟩ theorem of_decreasing_of_bounded_below (H1 : ∃ N, ∀ n > N, f (n + 1) ≤ f n) (H2 : ∃ L N, ∀ n > N, L ≤ f n) : f ∈ real.cau_seq := let ⟨N1, H3⟩ := H1 in let ⟨L, N2, H4⟩ := H2 in neg_neg f ▸ (neg_eq_neg_one_mul (-f)).symm ▸ (mul _ _ neg_one $ of_increasing_of_bounded_above (-f) ⟨N1, λ n hn, neg_le_neg $ H3 n hn⟩ ⟨-L, N2, λ n hn, neg_le_neg $ H4 n hn⟩) theorem of_eventually_equal (H : ∃ N, ∀ n > N, f n = g n) : g ∈ real.cau_seq := λ ε Hε, let ⟨N1, HN1⟩ := hf ε Hε in let ⟨N2, HN2⟩ := H in ⟨max N1 N2, λ m hm n hn, HN2 m (lt_of_le_of_lt (le_max_right N1 N2) hm) ▸ HN2 n (lt_of_le_of_lt (le_max_right N1 N2) hn) ▸ HN1 m (lt_of_le_of_lt (le_max_left N1 N2) hm) n (lt_of_le_of_lt (le_max_left N1 N2) hn)⟩ instance : comm_ring real.cau_seq := by refine { add := λ f g, ⟨f.1 + g.1, add _ _ f.2 g.2⟩, zero := ⟨0, have H : (-1 : ℕ → real) + (-1) * (-1) = 0, by simp, H ▸ add _ _ neg_one $ mul _ _ neg_one neg_one⟩, neg := λ f, ⟨-f.1, have H : (-1) * f.1 = -f.1, by simp, H ▸ mul _ _ neg_one f.2⟩, mul := λ f g, ⟨f.1 * g.1, mul _ _ f.2 g.2⟩, one := ⟨1, have H : (-1 : ℕ → real) * (-1) = 1, by simp, H ▸ mul _ _ neg_one neg_one⟩, .. }; { intros, { simp [mul_assoc, mul_add, add_mul] } <|> simp [mul_comm] } instance : has_coe_to_fun real.cau_seq := ⟨_, λ f, f.1⟩ instance : module real real.cau_seq := { smul := λ r f, ⟨λ n, r * f n, real_mul r f.1 f.2⟩, smul_add := λ r x y, subtype.eq $ funext $ λ n, mul_add _ _ _, add_smul := λ r s x, subtype.eq $ funext $ λ n, add_mul _ _ _, mul_smul := λ r s x, subtype.eq $ funext $ λ n, mul_assoc _ _ _, one_smul := λ r, subtype.eq $ funext $ λ n, one_mul _ } instance : has_coe real real.cau_seq := ⟨λ r, ⟨λ n, r, λ ε Hε, ⟨0, λ m hm n hn, show abs (r - r) < ε, from (sub_self r).symm ▸ (@abs_zero real _).symm ▸ Hε⟩⟩⟩ theorem coe_inj (x y : real) (H : (x : real.cau_seq) = y) : x = y := congr_fun (congr_arg subtype.val H) 0 -- which direction is more useful? theorem smul_eq_coe_mul (r : real) (f : real.cau_seq) : r • f = r * f := rfl theorem coe_zero : ((0 : real) : real.cau_seq) = 0 := rfl theorem coe_one : ((1 : real) : real.cau_seq) = 1 := rfl theorem coe_neg_one : ((-1 : real) : real.cau_seq) = -1 := rfl theorem coe_add (x y : real) : ((x + y : real) : real.cau_seq) = x + y := rfl theorem coe_neg (x : real) : ((-x : real) : real.cau_seq) = -x := rfl theorem coe_sub (x y : real) : ((x - y : real) : real.cau_seq) = x - y := rfl theorem coe_mul (x y : real) : ((x * y : real) : real.cau_seq) = x * y := rfl end real.cau_seq namespace real /- @[elab_as_eliminator] protected theorem induction_on {C : real → Prop} (r : real) (x : real) (H1 : C x) -- nonempty (H2 : ∀ t, C t → ∃ ε > 0, ∀ x, abs (x - t) < ε → C x) -- open (H3 : ∀ f : cau_seq, (∀ n, C (f n)) → C (lim f)) : -- closed C r := classical.by_contradiction $ assume H4 : ¬ C r, let S := { ε | ∀ b, abs (x - b) < ε → C b } in let M := sup S 0 (abs (x - r)) (λ b hb, false.elim $ not_lt_of_ge (abs_nonneg _) hb) (λ ε Hε, le_of_not_gt $ λ H5, H4 $ Hε r H5) in _ -/ def shift (f : real.cau_seq) (k : nat) : real.cau_seq := ⟨_, cau_seq.shift f.1 f.2 k⟩ section calculus_of_limits variables (r : real) (f g : cau_seq) (s : real) theorem lim_add : lim (f + g) = lim f + lim g := (lim_def _ _).2 $ λ ε Hε, let ⟨N1, HN1⟩ := lim_close f (ε/2) (half_pos Hε) in let ⟨N2, HN2⟩ := lim_close g (ε/2) (half_pos Hε) in ⟨max N1 N2, λ n hn, have H1 : abs (f n - lim f) < ε/2, from HN1 n (lt_of_le_of_lt (le_max_left _ _) hn), have H2 : abs (g n - lim g) < ε/2, from HN2 n (lt_of_le_of_lt (le_max_right _ _) hn), calc abs ((f n + g n) - (lim f + lim g)) = abs ((f n - lim f) + (g n - lim g)) : by simp ... ≤ abs (f n - lim f) + abs (g n - lim g) : abs_add _ _ ... < ε/2 + ε/2 : add_lt_add H1 H2 ... = ε : add_halves ε⟩ theorem lim_const : lim r = r := (lim_def _ _).2 $ λ ε Hε, ⟨0, λ n hn, calc abs (r - r) = abs 0 : congr_arg abs $ sub_self r ... = 0 : abs_zero ... < ε : Hε⟩ theorem lim_const_mul : lim (r * f) = r * lim f := (lim_def _ _).2 $ λ ε Hε, have H1 : abs r + 1 > 0, from add_pos_of_nonneg_of_pos (abs_nonneg r) zero_lt_one, let ⟨N, HN⟩ := lim_close f (ε / (abs r + 1)) (div_pos Hε H1) in ⟨N, λ n hn, calc abs (r * f n - r * lim f) = abs r * abs (f n - lim f) : by rw [← mul_sub, abs_mul] ... < (abs r + 1) * (ε / (abs r + 1)) : mul_lt_mul' (le_of_lt $ lt_add_one _) (HN n hn) (abs_nonneg _) H1 ... = ε : mul_div_cancel' _ $ ne_of_gt H1⟩ theorem lim_mul_const : lim (f * s) = lim f * s := by simpa [mul_comm] using lim_const_mul s f theorem lim_neg : lim (-f) = -lim f := calc lim (-f) = lim ((-1) * f) : congr_arg lim $ neg_eq_neg_one_mul f ... = lim ((-1 : real) * f) : rfl ... = (-1) * lim f : lim_const_mul (-1) f ... = -lim f : neg_one_mul (lim f) theorem lim_sub : lim (f - g) = lim f - lim g := (lim_add f (-g)).trans (congr_arg _ $ lim_neg g) theorem lim_mul_zero (Hf : lim f = 0) (Hg : lim g = 0) : lim (f * g) = 0 := (lim_def _ _).2 $ λ ε Hε, let ⟨N1, HN1⟩ := (lim_def _ _).1 Hf ε Hε in let ⟨N2, HN2⟩ := (lim_def _ _).1 Hg 1 zero_lt_one in ⟨max N1 N2, λ n hn, have H1 : abs (f n) < ε, by simpa using HN1 n (lt_of_le_of_lt (le_max_left _ _) hn), have H2 : abs (g n) < 1, by simpa using HN2 n (lt_of_le_of_lt (le_max_right _ _) hn), calc abs (f n * g n - 0) = abs (f n) * abs (g n) : by rw [sub_zero, abs_mul] ... < ε * 1 : mul_lt_mul' (le_of_lt H1) H2 (abs_nonneg (g n)) Hε ... = ε : mul_one ε⟩ theorem lim_mul : lim (f * g) = lim f * lim g := have Hf : lim (f - lim f) = 0, by rw [lim_sub, lim_const, sub_self], have Hg : lim (g - lim g) = 0, by rw [lim_sub, lim_const, sub_self], calc lim (f * g) = lim ((f - lim f + lim f) * (g - lim g + lim g)) : by simp ... = lim (((f - lim f) * (g - lim g) + (f - lim f) * (lim g)) + ((lim f) * (g - lim g) + (lim f) * (lim g))) : by rw [add_mul, mul_add, mul_add] ... = ((lim ((f - lim f) * (g - lim g)) + lim ((f - lim f) * (lim g))) + (lim ((lim f) * (g - lim g)) + lim ((lim f) * (lim g)))) : by rw [lim_add, lim_add, lim_add] ... = lim f * lim g : by rw [lim_mul_zero _ _ Hf Hg, lim_mul_const, lim_const_mul, Hf, Hg, zero_mul, mul_zero, ← cau_seq.coe_mul, lim_const, zero_add, zero_add, zero_add] theorem lim_shift (k : nat) : lim (shift f k) = lim f := (lim_def _ _).2 $ λ ε Hε, let ⟨N, HN⟩ := lim_close f ε Hε in ⟨N, λ n hn, HN (n + k) $ lt_of_lt_of_le hn $ nat.le_add_right n k⟩ theorem lim_le (H : ∃ N, ∀ n > N, f n ≤ g n) : lim f ≤ lim g := le_of_not_gt $ assume H1 : lim g < lim f, have H2 : (lim f - lim g) / 2 > 0, from half_pos $ sub_pos_of_lt H1, let ⟨N, HN⟩ := H in let ⟨N1, HN1⟩ := lim_close f _ H2 in let ⟨N2, HN2⟩ := lim_close g _ H2 in not_lt_of_ge (HN (N+(N1+N2)+1) $ nat.succ_le_succ $ nat.le_add_right _ _) $ calc g (N+(N1+N2)+1) = (g (N+(N1+N2)+1) - lim g) + (lim f - f (N+(N1+N2)+1)) + (lim g - lim f + f (N+(N1+N2)+1)) : by simp [add_assoc] ... < (lim f - lim g) / 2 + (lim f - lim g) / 2 + (lim g - lim f + f (N+(N1+N2)+1)) : add_lt_add_right (add_lt_add (abs_sub_lt_iff.1 $ HN2 _ $ nat.succ_le_succ $ le_trans (nat.le_add_left _ _) (nat.le_add_left _ _)).1 (abs_sub_lt_iff.1 $ HN1 _ $ nat.succ_le_succ $ le_trans (nat.le_add_right _ _) (nat.le_add_left _ _)).2) _ ... = f (N+(N1+N2)+1) : by simp theorem squeeze.close (a b c : cau_seq) (L : real) (Ha : lim a = L) (Hc : lim c = L) (Hb : ∃ N, ∀ n > N, a n ≤ b n ∧ b n ≤ c n) : close b L := λ ε Hε, let ⟨N1, HN1⟩ := (lim_def _ _).1 Ha ε Hε in let ⟨N2, HN2⟩ := (lim_def _ _).1 Hc ε Hε in let ⟨N, HN⟩ := Hb in ⟨max N (max N1 N2), λ n hn, have H1 : b n - L < ε, from calc b n - L ≤ c n - L : sub_le_sub_right (HN _ $ lt_of_le_of_lt (le_max_left _ _) hn).2 _ ... < ε : (abs_sub_lt_iff.1 $ HN2 n $ lt_of_le_of_lt (le_trans (le_max_right _ _) (le_max_right _ _)) hn).1, have H2 : L - b n < ε, from calc L - b n ≤ L - a n : sub_le_sub_left (HN _ $ lt_of_le_of_lt (le_max_left _ _) hn).1 _ ... < ε : (abs_sub_lt_iff.1 $ HN1 n $ lt_of_le_of_lt (le_trans (le_max_left _ _) (le_max_right _ _)) hn).2, abs_sub_lt_iff.2 ⟨H1, H2⟩⟩ def squeeze.cau_seq (a b c : cau_seq) (L : real) (Ha : lim a = L) (Hc : lim c = L) (Hb : ∃ N, ∀ n > N, a n ≤ b n ∧ b n ≤ c n) : cau_seq := cau_seq.of_close b ⟨L, squeeze.close a b c L Ha Hc Hb⟩ theorem squeeze (a b c : cau_seq) (L : real) (Ha : lim a = L) (Hc : lim c = L) (Hb : ∃ N, ∀ n > N, a n ≤ b n ∧ b n ≤ c n) : lim (squeeze.cau_seq a b c L Ha Hc Hb) = L := lim.of_close _ _ $ squeeze.close a b c L Ha Hc Hb end calculus_of_limits end real instance real.fun : comm_ring (real → real) := by refine { add := λ f g x, f x + g x, zero := λ x, 0, neg := λ f x, -f x, mul := λ f g x, f x * g x, one := λ x, 1, .. }; { intros, { simp [mul_assoc, mul_add, add_mul] } <|> simp [mul_comm] } def real.cts_at (a : real) : set (real → real) := { f | ∀ ε > 0, ∃ δ > 0, ∀ x, abs (x - a) < δ → abs (f x - f a) < ε } namespace real.cts_at variables (a : real) (f g : real → real) (Hf : f ∈ real.cts_at a) (Hg : g ∈ real.cts_at a) theorem add : f + g ∈ real.cts_at a := λ ε Hε, let ⟨δ1, Hδ1, Hδf⟩ := Hf (ε/2) (half_pos Hε) in let ⟨δ2, Hδ2, Hδg⟩ := Hg (ε/2) (half_pos Hε) in ⟨min δ1 δ2, lt_min Hδ1 Hδ2, λ x hx, have H1 : abs (f x - f a) < ε/2, from Hδf x (lt_min_iff.1 hx).1, have H2 : abs (g x - g a) < ε/2, from Hδg x (lt_min_iff.1 hx).2, calc abs ((f x + g x) - (f a + g a)) = abs ((f x - f a) + (g x - g a)) : by simp ... ≤ abs (f x - f a) + abs (g x - g a) : abs_add _ _ ... < ε/2 + ε/2 : add_lt_add H1 H2 ... = ε : add_halves ε⟩ theorem neg : -f ∈ real.cts_at a := λ ε Hε, let ⟨δ, Hδ, Hδf⟩ := Hf ε Hε in ⟨δ, Hδ, λ x hx, calc abs (-f x - -f a) = abs (-(-f x - -f a)) : eq.symm $ abs_neg _ ... = abs (f x - f a) : congr_arg abs $ neg_neg_sub_neg _ _ ... < ε : Hδf x hx⟩ theorem mul : f * g ∈ real.cts_at a := λ ε Hε, let ε1 := ε/2/(abs (g a) + 1) in have Hε1 : ε1 > 0, from div_pos (half_pos Hε) (add_pos_of_nonneg_of_pos (abs_nonneg (g a)) zero_lt_one), let ε2 := ε/2/(ε1 + abs (f a)) in have Hε2 : ε2 > 0, from div_pos (half_pos Hε) (add_pos_of_pos_of_nonneg Hε1 (abs_nonneg (f a))), let ⟨δ1, Hδ1, Hδf⟩ := Hf ε1 Hε1 in let ⟨δ2, Hδ2, Hδg⟩ := Hg ε2 Hε2 in ⟨min δ1 δ2, lt_min Hδ1 Hδ2, λ x hx, have H1 : abs (f x - f a) < ε1, from Hδf x (lt_min_iff.1 hx).1, have H2 : abs (g x - g a) < ε2, from Hδg x (lt_min_iff.1 hx).2, calc abs ((f x * g x) - (f a * g a)) = abs (f x * (g x - g a) + (f x - f a) * g a) : by rw [mul_sub, sub_mul, ← add_sub_assoc, sub_add_cancel] ... ≤ abs (f x * (g x - g a)) + abs ((f x - f a) * g a) : abs_add _ _ ... = abs (f x - f a + f a) * abs (g x - g a) + abs (f x - f a) * abs (g a) : by rw [abs_mul, abs_mul, sub_add_cancel] ... < (abs (f x - f a) + abs (f a)) * ε2 + ε1 * (abs (g a) + 1) : add_lt_add_of_le_of_lt (mul_le_mul (abs_add _ _) (le_of_lt H2) (abs_nonneg _) (add_nonneg (abs_nonneg _) (abs_nonneg _))) (mul_lt_mul' (le_of_lt H1) (lt_add_one _) (abs_nonneg _) Hε1) ... < (ε1 + abs (f a)) * ε2 + ε1 * (abs (g a) + 1) : add_lt_add_right (mul_lt_mul_of_pos_right (add_lt_add_right H1 _) Hε2) _ ... = ε/2 + ε/2 : congr_arg₂ (+) (mul_div_cancel' _ $ ne_of_gt $ add_pos_of_pos_of_nonneg Hε1 $ abs_nonneg _) (div_mul_cancel _ $ ne_of_gt $ add_pos_of_nonneg_of_pos (abs_nonneg _) zero_lt_one) ... = ε : add_halves ε⟩ end real.cts_at def real.cts : set (real → real) := { f | ∀ a, f ∈ real.cts_at a } namespace real.cts variables (f g : real → real) (Hf : f ∈ real.cts) (Hg : g ∈ real.cts) theorem add : f + g ∈ real.cts := λ a, real.cts_at.add a f g (Hf a) (Hg a) theorem mul : f * g ∈ real.cts := λ a, real.cts_at.mul a f g (Hf a) (Hg a) theorem neg_one : (-1 : real → real) ∈ real.cts := λ a ε Hε, ⟨1, zero_lt_one, λ x hx, show abs (-1 - -1) < ε, from trans_rel_right _ (by simp) Hε⟩ theorem id : id ∈ real.cts := λ a ε Hε, ⟨ε, Hε, λ x, id⟩ instance : comm_ring real.cts := by refine { add := λ f g, ⟨f.1 + g.1, add _ _ f.2 g.2⟩, zero := ⟨0, have H : (-1 : real → real) + (-1) * (-1) = 0, by simp, H ▸ add _ _ neg_one $ mul _ _ neg_one neg_one⟩, neg := λ f, ⟨-f.1, have H : (-1) * f.1 = -f.1, by simp, H ▸ mul _ _ neg_one f.2⟩, mul := λ f g, ⟨f.1 * g.1, mul _ _ f.2 g.2⟩, one := ⟨1, have H : (-1 : real → real) * (-1) = 1, by simp, H ▸ mul _ _ neg_one neg_one⟩, .. }; { intros, { simp [mul_assoc, mul_add, add_mul] } <|> simp [mul_comm] } theorem pow (n : nat) : (λ x, x^n) ∈ real.cts := nat.rec_on n (1 : real.cts).2 $ λ n ih, real.cts.mul _ _ id ih end real.cts section IVT parameters (f : real → real) (hf : f ∈ real.cts) (a b c : real) parameters (H1 : a < b) (H2 : f a < c) (H3 : c < f b) noncomputable def real.cts_sol : real := sup { x | x < b ∧ f x < c } a b ⟨H1, H2⟩ $ λ x hx, le_of_lt hx.1 theorem real.cts_sol.lt : real.cts_sol < b := have H4 : real.cts_sol ≤ b := sup_le _ _ _ _ _ _ $ λ x hx, le_of_lt hx.1, or.resolve_right H4 $ assume H5 : real.cts_sol = b, let ⟨δ, Hδ1, Hδ2⟩ := hf b (f b - c) (sub_pos_of_lt H3) in have H6 : real.cts_sol ≤ b - δ, from sup_le _ _ _ _ _ _ $ λ x ⟨hx1, hx2⟩, le_of_not_gt $ λ hx3, have H7 : abs (x - b) < δ, from abs_sub_lt_iff.2 ⟨lt_trans (sub_neg_of_lt hx1) Hδ1, sub_lt_of_sub_lt hx3⟩, have H8 : _ := (abs_sub_lt_iff.1 (Hδ2 x H7)).2, lt_asymm H8 $ sub_lt_sub_left hx2 _, not_lt_of_ge H6 $ (trans_rel_left _ (sub_lt_self _ Hδ1) H5.symm) theorem real.cts_sol.sol : f real.cts_sol = c := classical.by_contradiction $ assume H : f real.cts_sol ≠ c, or.cases_on (lt_or_gt_of_ne H) (assume H4 : f real.cts_sol < c, let ⟨δ, Hδ1, Hδ2⟩ := hf real.cts_sol (c - f real.cts_sol) (sub_pos_of_lt H4) in let x := min (δ/2) ((b - real.cts_sol)/2) in have H5 : x > 0, from lt_min (half_pos Hδ1) (half_pos $ sub_pos_of_lt real.cts_sol.lt), have H6 : x < δ, from min_lt_iff.2 $ or.inl $ half_lt_self Hδ1, have H7 : real.cts_sol + x < b, from add_lt_of_lt_sub_left $ min_lt_iff.2 $ or.inr $ half_lt_self $ sub_pos_of_lt real.cts_sol.lt, have H8 : _ := Hδ2 (real.cts_sol + x) $ by simp; rw abs_of_pos H5; from H6, have H9 : real.cts_sol + x ≤ real.cts_sol, from le_sup _ _ _ _ _ _ ⟨H7, ((sub_lt_sub_iff_right _).1 (abs_sub_lt_iff.1 H8).1)⟩, not_lt_of_ge H9 $ lt_add_of_pos_right _ H5) (assume H4 : f real.cts_sol > c, let ⟨δ, Hδ1, Hδ2⟩ := hf real.cts_sol (f real.cts_sol - c) (sub_pos_of_lt H4) in have H5 : real.cts_sol ≤ real.cts_sol - δ, from sup_le _ _ _ _ _ _ $ λ r ⟨hr1, hr2⟩, le_of_not_gt $ λ H5, have H6 : r ≤ real.cts_sol, from le_sup _ _ _ _ _ _ ⟨hr1, hr2⟩, have H7 : r - real.cts_sol < δ, from lt_of_le_of_lt (sub_nonpos_of_le H6) Hδ1, have H8 : _ := Hδ2 r $ abs_sub_lt_iff.2 ⟨H7, sub_lt_of_sub_lt H5⟩, lt_asymm (abs_sub_lt_iff.1 H8).2 $ sub_lt_sub_left hr2 _, not_lt_of_ge H5 $ sub_lt_self _ Hδ1) end IVT -- TODO: make a computable version and prove equality noncomputable def sqrt2 : real := real.cts_sol (λ x, x^2) 0 2 2 two_pos $ trans_rel_right _ (mul_zero 0) two_pos theorem sqrt2_pow : sqrt2 ^ 2 = 2 := real.cts_sol.sol (λ x, x^2) (real.cts.pow 2) 0 2 2 _ _ $ trans_rel_left _ (lt_add_of_pos_right _ two_pos) (mul_two 2).symm theorem sqrt2_mul_sqrt2 : sqrt2 * sqrt2 = 2 := (pow_two _).symm.trans sqrt2_pow namespace real theorem pow.cau_seq.nonneg (x : real) (Hx1 : 0 ≤ x) (Hx2 : x < 1) : (λ n, x ^ n) ∈ cau_seq := cau_seq.of_decreasing_of_bounded_below _ ⟨0, λ n hn, trans_rel_left _ (mul_le_mul (le_of_lt Hx2) (le_refl _) (pow_nonneg Hx1 n) zero_le_one) (one_mul _)⟩ ⟨0, 0, λ n hn, pow_nonneg Hx1 n⟩ def pow.nonneg (x : real) (Hx1 : 0 ≤ x) (Hx2 : x < 1) : cau_seq := ⟨_, pow.cau_seq.nonneg x Hx1 Hx2⟩ theorem pow.lim.nonneg (x : real) (Hx1 : 0 ≤ x) (Hx2 : x < 1) : lim (pow.nonneg x Hx1 Hx2) = 0 := have H1 : _, from calc x * lim (pow.nonneg x Hx1 Hx2) = lim (x * pow.nonneg x Hx1 Hx2) : eq.symm $ lim_const_mul _ _ ... = lim (shift (pow.nonneg x Hx1 Hx2) 1) : congr_arg lim $ subtype.eq rfl ... = lim (pow.nonneg x Hx1 Hx2) : lim_shift _ 1, have H2 : (x - 1) * lim (pow.nonneg x Hx1 Hx2) = 0, by rw [sub_mul, H1, one_mul, sub_self], or.resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero H2) $ assume H3 : x - 1 = 0, lt_irrefl (1:real) $ calc 1 = x - (x - 1) : eq.symm $ sub_sub_cancel x 1 ... = x - 0 : congr_arg _ H3 ... = x : sub_zero x ... < 1 : Hx2 def pow.cau_seq (x : real) (Hx : abs x < 1) : cau_seq := cau_seq.of_close (λ n, x^n) ⟨0, λ ε Hε, let ⟨N, HN⟩ := (lim_def _ _).1 (pow.lim.nonneg (abs x) (abs_nonneg _) Hx) ε Hε in ⟨N, λ n hn, calc abs (x^n - 0) = abs ((abs x)^n -0) : by simp [pow_abs, abs_abs] ... < ε : HN n hn⟩⟩ theorem pow.lim (x : real) (Hx : abs x < 1) : lim (pow.cau_seq x Hx) = 0 := lim.of_close _ _ $ λ ε Hε, let ⟨N, HN⟩ := (lim_def _ _).1 (pow.lim.nonneg (abs x) (abs_nonneg _) Hx) ε Hε in ⟨N, λ n hn, calc abs (x^n - 0) = abs ((abs x)^n -0) : by simp [pow_abs, abs_abs] ... < ε : HN n hn⟩ variables (f g : ℕ → real) def partial_sum : ℕ → real | 0 := 0 | (n+1) := partial_sum n + f n theorem close_zero_of_sum_cau_seq (H : partial_sum f ∈ cau_seq) : close f 0 := λ ε Hε, let ⟨N, HN⟩ := H ε Hε in ⟨N, λ n hn, calc abs (f n - 0) = abs (f n) : congr_arg abs $ sub_zero _ ... = abs (partial_sum f (n+1) - partial_sum f n) : congr_arg abs $ eq.symm $ add_sub_cancel' _ _ ... < ε : HN (n+1) (nat.lt_succ_of_lt hn) n hn⟩ theorem lim_zero_of_sum_cau_seq (H : partial_sum f ∈ cau_seq) : lim (cau_seq.of_close f ⟨0, close_zero_of_sum_cau_seq f H⟩) = 0 := lim.of_close _ _ _ theorem partial_sum_pow (x : real) (n : nat) (H : x ≠ 1) : partial_sum (λ n, x^n) n = (1 - x^n) / (1 - x) := have H1 : 1 + -x ≠ 0, from λ H2, H $ eq.symm $ eq_of_sub_eq_zero H2, by induction n with n ih; simp [partial_sum]; simp [ih, add_div_eq_mul_add_div _ _ H1, mul_add, pow_succ'] def partial_sum_pow.cau_seq (x : real) (H : abs x < 1) : cau_seq := ⟨partial_sum (λ n, x^n), have H1 : x ≠ 1, from λ H1, by subst H1; simp at H; from lt_irrefl _ H, have H2 : partial_sum (λ n, x^n) = ((1:real) - (λ n, x^n)) * (1 / (1 - x) : real), from funext $ λ n, (partial_sum_pow x n H1).trans $ div_eq_mul_one_div _ _, H2.symm ▸ cau_seq.mul _ _ (cau_seq.sub _ _ cau_seq.one (pow.cau_seq x H).2) (((1 / (1 - x) : real) : cau_seq).2)⟩ theorem partial_sum_pow.lim (x : real) (H : abs x < 1) : lim (partial_sum_pow.cau_seq x H) = 1 / (1 - x) := have H1 : x ≠ 1, from λ H1, by subst H1; simp at H; from lt_irrefl _ H, calc lim (partial_sum_pow.cau_seq x H) = lim (((1 : real) - pow.cau_seq x H) * (1 / (1 - x) : real)) : congr_arg lim $ subtype.eq $ funext $ λ n, (partial_sum_pow x n H1).trans $ div_eq_mul_one_div _ _ ... = 1 / (1 - x) : by rw [lim_mul, lim_sub, lim_const, lim_const, pow.lim, sub_zero, one_mul] theorem partial_sum.add : partial_sum (f + g) = partial_sum f + partial_sum g := funext $ λ n, show partial_sum (f + g) n = partial_sum f n + partial_sum g n, from nat.rec_on n (zero_add _).symm $ λ n ih, show partial_sum (f + g) n + (f n + g n) = (partial_sum f n + f n) + (partial_sum g n + g n), by rw ih; ac_refl theorem partial_sum.const_mul (r : real) (f : ℕ → real) : partial_sum (r * f) = r * partial_sum f := funext $ λ n, show partial_sum (r * f) n = r * partial_sum f n, from nat.rec_on n (mul_zero r).symm $ λ n ih, show partial_sum (r * f) n + r * f n = r * (partial_sum f n + f n), by rw [ih, mul_add] theorem partial_sum.const (r : real) (n : nat) : partial_sum r n = n * r := nat.rec_on n (zero_mul r).symm $ λ n ih, show partial_sum r n + r = (n + 1) * r, by rw [ih, add_mul, one_mul] theorem partial_sum.neg : partial_sum (-f) = -partial_sum f := calc partial_sum (-f) = partial_sum ((-1) * f) : by rw [neg_eq_neg_one_mul] ... = partial_sum ((-1 : real) * f) : rfl ... = (-1) * partial_sum f : partial_sum.const_mul _ _ ... = -partial_sum f : neg_one_mul _ theorem partial_sum.sub : partial_sum (f - g) = partial_sum f - partial_sum g := calc partial_sum (f - g) = partial_sum f + partial_sum (-g) : partial_sum.add _ _ ... = partial_sum f - partial_sum g : congr_arg _ $ partial_sum.neg _ def partial_sum.cau_seq_of_nonneg_of_bounded (H1 : ∃ N, ∀ n > N, f n ≥ 0) (H2 : ∃ L N, ∀ n > N, partial_sum f n ≤ L) : cau_seq := ⟨partial_sum f, let ⟨N1, H3⟩ := H1 in let ⟨L, N2, H4⟩ := H2 in cau_seq.of_increasing_of_bounded_above _ ⟨N1, λ n hn, le_add_of_nonneg_right $ H3 n hn⟩ ⟨L, N2, λ n hn, H4 n hn⟩⟩ theorem partial_sum.nonneg_of_nonneg (f : ℕ → real) (Hf : partial_sum f ∈ cau_seq) (H : ∀ n, f n ≥ 0) : lim ⟨_, Hf⟩ ≥ 0 := (lim_const 0) ▸ lim_le _ _ ⟨0, λ n hn, nat.rec_on n (le_refl _) $ λ n ih, add_nonneg ih $ H n⟩ end real
84d110fbc89f2c8a5499a16d6b234d2411c670cd
a7dd8b83f933e72c40845fd168dde330f050b1c9
/src/category_theory/instances/Top/opens.lean
f5904eeb578363b9760b49ac5f8bd2abd1f784aa
[ "Apache-2.0" ]
permissive
NeilStrickland/mathlib
10420e92ee5cb7aba1163c9a01dea2f04652ed67
3efbd6f6dff0fb9b0946849b43b39948560a1ffe
refs/heads/master
1,589,043,046,346
1,558,938,706,000
1,558,938,706,000
181,285,984
0
0
Apache-2.0
1,568,941,848,000
1,555,233,833,000
Lean
UTF-8
Lean
false
false
3,905
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.instances.Top.basic import category_theory.natural_isomorphism import category_theory.opposites import category_theory.eq_to_hom import topology.opens open category_theory open category_theory.instances open topological_space universe u open category_theory.instances opposite namespace topological_space.opens variables {X Y Z : Top.{u}} instance opens_category : category.{u+1} (opens X) := { hom := λ U V, ulift (plift (U ≤ V)), id := λ X, ⟨ ⟨ le_refl X ⟩ ⟩, comp := λ X Y Z f g, ⟨ ⟨ le_trans f.down.down g.down.down ⟩ ⟩ } def to_Top (X : Top.{u}) : opens X ⥤ Top := { obj := λ U, ⟨U.val, infer_instance⟩, map := λ U V i, ⟨λ x, ⟨x.1, i.down.down x.2⟩, (embedding.continuous_iff embedding_subtype_val).2 continuous_induced_dom⟩ } /-- `opens.map f` gives the functor from open sets in Y to open set in X, given by taking preimages under f. -/ def map (f : X ⟶ Y) : opens Y ⥤ opens X := { obj := λ U, ⟨ f.val ⁻¹' U.val, f.property _ U.property ⟩, map := λ U V i, ⟨ ⟨ λ a b, i.down.down b ⟩ ⟩ }. @[simp] lemma map_id_obj' (U) (p) : (map (𝟙 X)).obj ⟨U, p⟩ = ⟨U, p⟩ := rfl @[simp] lemma map_id_obj (U : opens X) : (map (𝟙 X)).obj U = U := by { ext, refl } -- not quite `rfl`, since we don't have eta for records @[simp] lemma map_id_obj_unop (U : (opens X)ᵒᵖ) : (map (𝟙 X)).obj (unop U) = unop U := by simp @[simp] lemma op_map_id_obj (U : (opens X)ᵒᵖ) : (map (𝟙 X)).op.obj U = U := by simp section variable (X) def map_id : map (𝟙 X) ≅ functor.id (opens X) := { hom := { app := λ U, eq_to_hom (map_id_obj U) }, inv := { app := λ U, eq_to_hom (map_id_obj U).symm } } @[simp] lemma map_id_hom_app (U) : (map_id X).hom.app U = eq_to_hom (map_id_obj U) := rfl @[simp] lemma map_id_inv_app (U) : (map_id X).inv.app U = eq_to_hom (map_id_obj U).symm := rfl end @[simp] lemma map_comp_obj' (f : X ⟶ Y) (g : Y ⟶ Z) (U) (p) : (map (f ≫ g)).obj ⟨U, p⟩ = (map f).obj ((map g).obj ⟨U, p⟩) := rfl @[simp] lemma map_comp_obj (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).obj U = (map f).obj ((map g).obj U) := by { ext, refl } -- not quite `rfl`, since we don't have eta for records @[simp] lemma map_comp_obj_unop (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).obj (unop U) = (map f).obj ((map g).obj (unop U)) := by simp @[simp] lemma op_map_comp_obj (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).op.obj U = (map f).op.obj ((map g).op.obj U) := by simp def map_comp (f : X ⟶ Y) (g : Y ⟶ Z) : map (f ≫ g) ≅ map g ⋙ map f := { hom := { app := λ U, eq_to_hom (map_comp_obj f g U) }, inv := { app := λ U, eq_to_hom (map_comp_obj f g U).symm } } @[simp] lemma map_comp_hom_app (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map_comp f g).hom.app U = eq_to_hom (map_comp_obj f g U) := rfl @[simp] lemma map_comp_inv_app (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map_comp f g).inv.app U = eq_to_hom (map_comp_obj f g U).symm := rfl -- We could make f g implicit here, but it's nice to be able to see when -- they are the identity (often!) def map_iso (f g : X ⟶ Y) (h : f = g) : map f ≅ map g := nat_iso.of_components (λ U, eq_to_iso (congr_fun (congr_arg functor.obj (congr_arg map h)) U) ) (by obviously) @[simp] lemma map_iso_refl (f : X ⟶ Y) (h) : map_iso f f h = iso.refl (map _) := rfl @[simp] lemma map_iso_hom_app (f g : X ⟶ Y) (h : f = g) (U : opens Y) : (map_iso f g h).hom.app U = eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h)) U) := rfl @[simp] lemma map_iso_inv_app (f g : X ⟶ Y) (h : f = g) (U : opens Y) : (map_iso f g h).inv.app U = eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h.symm)) U) := rfl end topological_space.opens
cd1cbdea56d51e55e50ae46da205c6cfcac74679
b9a81ebb9de684db509231c4469a7d2c88915808
/src/super/prover.lean
edb6c9e24729c73c47871da002564cd969f0fc3c
[]
no_license
leanprover/super
3dd81ce8d9ac3cba20bce55e84833fadb2f5716e
47b107b4cec8f3b41d72daba9cbda2f9d54025de
refs/heads/master
1,678,482,996,979
1,676,526,367,000
1,676,526,367,000
92,215,900
12
6
null
1,513,327,539,000
1,495,570,640,000
Lean
UTF-8
Lean
false
false
4,069
lean
/- Copyright (c) 2017 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import .clause .prover_state import .misc_preprocessing import .selection import .trim -- default inferences -- 0 import .clausifier -- 10 import .demod import .inhabited import .datatypes -- 20 import .subsumption -- 30 import .splitting -- 40 import .factoring import .resolution import .superposition import .equality import .simp import .defs open monad tactic expr declare_trace super namespace super meta def trace_clauses : prover unit := do state ← get, trace state meta def run_prover_loop (literal_selection : selection_strategy) (clause_selection : clause_selection_strategy) (preprocessing_rules : list (prover unit)) (inference_rules : list inference) (max_iters : ℕ) : ℕ → prover (option expr) | i := do sequence' preprocessing_rules, new ← take_newly_derived, new.mmap' register_as_passive, when (is_trace_enabled_for `super) $ new.mmap' $ λn, tactic.trace { n with c := { n.c with proof := const (mk_simple_name "derived") [] } }, needs_sat_run ← flip (<$>) get (λst, st.needs_sat_run), if needs_sat_run then do res ← do_sat_run, match res with | some proof := return (some proof) | none := do model ← flip (<$>) get (λst, st.current_model), when (is_trace_enabled_for `super) (do pp_model ← pp (model.to_list.map (λlit, if lit.2 = tt then lit.1 else `(not %%lit.1))), trace $ to_fmt "sat model: " ++ pp_model), run_prover_loop i end else do passive ← get_passive, if passive.size = 0 then return none else do given_name ← clause_selection i, given ← option.to_monad (passive.find given_name), -- trace_clauses, remove_passive given_name, given ← literal_selection given, when (is_trace_enabled_for `super) (do fmt ← pp given, trace (to_fmt "given: " ++ fmt)), add_active given, seq_inferences inference_rules given, when (i ≥ max_iters) $ fail "reached maximum number of iterations in saturation loop", run_prover_loop (i+1) meta def default_preprocessing : list (prover unit) := [ clausify_pre, clause_normalize_pre, factor_dup_lits_pre, remove_duplicates_pre, refl_r_pre, diff_constr_eq_l_pre, tautology_removal_pre, subsumption_interreduction_pre, forward_subsumption_pre, return () ] end super open super meta structure super.opts := (max_iters : ℕ := 200) (timeout : ℕ := 20000) meta def super (sos_lemmas : list expr) (os : super.opts) : tactic unit := do try_for os.timeout $ with_trim $ do as_refutation, local_false ← target, clauses ← clauses_of_context, sos_clauses ← sos_lemmas.mmap (clause.of_proof local_false), initial_state ← prover_state.initial local_false (clauses ++ sos_clauses), inf_names ← attribute.get_instances `super.inf, infs ← inf_names.mmap $ λn, eval_expr inf_decl (const n []), infs ← return $ list.map inf_decl.inf $ list.sort_on inf_decl.prio infs, res ← (run_prover_loop selection21 (age_weight_clause_selection 3 4) default_preprocessing infs os.max_iters 0).run initial_state, match res with | (some empty_clause, st) := apply empty_clause >> skip | (none, saturation) := do sat_fmt ← pp saturation, fail $ to_fmt "saturation:" ++ format.line ++ sat_fmt end namespace tactic.interactive open lean.parser open interactive open interactive.types meta def with_lemmas (ls : parse $ many ident) : tactic unit := ls.mmap' $ λ l, do eqn_ls ← get_eqn_lemmas_for tt l <|> return [], (l :: eqn_ls).mmap' $ λ l, do p ← mk_const l, t ← infer_type p, n ← get_unused_name p.get_app_fn.const_name none, tactic.assertv n t p meta def super (extra_clause_names : parse $ many ident) (extra_lemma_names : parse with_ident_list) (opts : super.opts := {}) : tactic unit := do with_lemmas extra_clause_names, extra_lemmas ← extra_lemma_names.mmap (λ c, do eqn_ls ← get_eqn_lemmas_for tt c, (c::eqn_ls).mmap mk_const), _root_.super extra_lemmas.join opts end tactic.interactive
e421d865d56e6ae9b45a4cc2fb41230c339df21d
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/algebra/category/Module/adjunctions.lean
89c974cb0398633a09598d83a2d1f391c65ff015
[ "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,057
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johan Commelin -/ import algebra.category.Module.monoidal import category_theory.monoidal.functorial import category_theory.monoidal.types import linear_algebra.direct_sum.finsupp import category_theory.linear.linear_functor /-! The functor of forming finitely supported functions on a type with values in a `[ring R]` is the left adjoint of the forgetful functor from `R`-modules to types. -/ noncomputable theory open category_theory namespace Module universe u open_locale classical variables (R : Type u) section variables [ring R] /-- The free functor `Type u ⥤ Module R` sending a type `X` to the free `R`-module with generators `x : X`, implemented as the type `X →₀ R`. -/ @[simps] def free : Type u ⥤ Module R := { obj := λ X, Module.of R (X →₀ R), map := λ X Y f, finsupp.lmap_domain _ _ f, map_id' := by { intros, exact finsupp.lmap_domain_id _ _ }, map_comp' := by { intros, exact finsupp.lmap_domain_comp _ _ _ _, } } /-- The free-forgetful adjunction for R-modules. -/ def adj : free R ⊣ forget (Module.{u} R) := adjunction.mk_of_hom_equiv { hom_equiv := λ X M, (finsupp.lift M R X).to_equiv.symm, hom_equiv_naturality_left_symm' := λ _ _ M f g, finsupp.lhom_ext' (λ x, linear_map.ext_ring (finsupp.sum_map_domain_index_add_monoid_hom (λ y, ((smul_add_hom R M).flip) (g y))).symm) } instance : is_right_adjoint (forget (Module.{u} R)) := ⟨_, adj R⟩ end namespace free variables [comm_ring R] local attribute [ext] tensor_product.ext /-- (Implementation detail) The unitor for `free R`. -/ def ε : 𝟙_ (Module.{u} R) ⟶ (free R).obj (𝟙_ (Type u)) := finsupp.lsingle punit.star /-- (Implementation detail) The tensorator for `free R`. -/ def μ (α β : Type u) : (free R).obj α ⊗ (free R).obj β ⟶ (free R).obj (α ⊗ β) := (finsupp_tensor_finsupp' R α β).to_linear_map lemma μ_natural {X Y X' Y' : Type u} (f : X ⟶ Y) (g : X' ⟶ Y') : ((free R).map f ⊗ (free R).map g) ≫ (μ R Y Y') = (μ R X X') ≫ (free R).map (f ⊗ g) := begin intros, ext x x' ⟨y, y'⟩, dsimp [μ], simp_rw [finsupp.map_domain_single, finsupp_tensor_finsupp'_single_tmul_single, mul_one, finsupp.map_domain_single, category_theory.tensor_apply], end lemma left_unitality (X : Type u) : (λ_ ((free R).obj X)).hom = (ε R ⊗ 𝟙 ((free R).obj X)) ≫ μ R (𝟙_ (Type u)) X ≫ map (free R).obj (λ_ X).hom := begin intros, ext, dsimp [ε, μ], simp_rw [finsupp_tensor_finsupp'_single_tmul_single, Module.monoidal_category.left_unitor_hom_apply, finsupp.smul_single', mul_one, finsupp.map_domain_single, category_theory.left_unitor_hom_apply], end lemma right_unitality (X : Type u) : (ρ_ ((free R).obj X)).hom = (𝟙 ((free R).obj X) ⊗ ε R) ≫ μ R X (𝟙_ (Type u)) ≫ map (free R).obj (ρ_ X).hom := begin intros, ext, dsimp [ε, μ], simp_rw [finsupp_tensor_finsupp'_single_tmul_single, Module.monoidal_category.right_unitor_hom_apply, finsupp.smul_single', mul_one, finsupp.map_domain_single, category_theory.right_unitor_hom_apply], end lemma associativity (X Y Z : Type u) : (μ R X Y ⊗ 𝟙 ((free R).obj Z)) ≫ μ R (X ⊗ Y) Z ≫ map (free R).obj (α_ X Y Z).hom = (α_ ((free R).obj X) ((free R).obj Y) ((free R).obj Z)).hom ≫ (𝟙 ((free R).obj X) ⊗ μ R Y Z) ≫ μ R X (Y ⊗ Z) := begin intros, ext, dsimp [μ], simp_rw [finsupp_tensor_finsupp'_single_tmul_single, finsupp.map_domain_single, mul_one, category_theory.associator_hom_apply], end /-- The free R-module functor is lax monoidal. -/ -- In fact, it's strong monoidal, but we don't yet have a typeclass for that. instance : lax_monoidal.{u} (free R).obj := { -- Send `R` to `punit →₀ R` ε := ε R, -- Send `(α →₀ R) ⊗ (β →₀ R)` to `α × β →₀ R` μ := μ R, μ_natural' := λ X Y X' Y' f g, μ_natural R f g, left_unitality' := left_unitality R, right_unitality' := right_unitality R, associativity' := associativity R, } end free end Module namespace category_theory universes v u /-- `Free R C` is a type synonym for `C`, which, given `[comm_ring R]` and `[category C]`, we will equip with a category structure where the morphisms are formal `R`-linear combinations of the morphisms in `C`. -/ @[nolint unused_arguments has_inhabited_instance] def Free (R : Type*) (C : Type u) := C /-- Consider an object of `C` as an object of the `R`-linear completion. -/ def Free.of (R : Type*) {C : Type u} (X : C) : Free R C := X variables (R : Type*) [comm_ring R] (C : Type u) [category.{v} C] open finsupp -- Conceptually, it would be nice to construct this via "transport of enrichment", -- using the fact that `Module.free R : Type ⥤ Module R` and `Module.forget` are both lax monoidal. -- This still seems difficult, so we just do it by hand. instance category_Free : category (Free R C) := { hom := λ (X Y : C), (X ⟶ Y) →₀ R, id := λ (X : C), finsupp.single (𝟙 X) 1, comp := λ (X Y Z : C) f g, f.sum (λ f' s, g.sum (λ g' t, finsupp.single (f' ≫ g') (s * t))), assoc' := λ W X Y Z f g h, begin dsimp, -- This imitates the proof of associativity for `monoid_algebra`. simp only [sum_sum_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, add_mul, mul_add, category.assoc, mul_assoc, zero_mul, mul_zero, sum_zero, sum_add], end }. namespace Free section local attribute [simp] category_theory.category_Free @[simp] lemma single_comp_single {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) (r s : R) : (single f r ≫ single g s : (Free.of R X) ⟶ (Free.of R Z)) = single (f ≫ g) (r * s) := by { dsimp, simp, } instance : preadditive (Free R C) := { hom_group := λ X Y, finsupp.add_comm_group, add_comp' := λ X Y Z f f' g, begin dsimp, rw [finsupp.sum_add_index]; { simp [add_mul], } end, comp_add' := λ X Y Z f g g', begin dsimp, rw ← finsupp.sum_add, congr, ext r h, rw [finsupp.sum_add_index]; { simp [mul_add], }, end, } instance : linear R (Free R C) := { hom_module := λ X Y, finsupp.module (X ⟶ Y) R, smul_comp' := λ X Y Z r f g, begin dsimp, rw [finsupp.sum_smul_index]; simp [finsupp.smul_sum, mul_assoc], end, comp_smul' := λ X Y Z f r g, begin dsimp, simp_rw [finsupp.smul_sum], congr, ext h s, rw [finsupp.sum_smul_index]; simp [finsupp.smul_sum, mul_left_comm], end, } end /-- A category embeds into its `R`-linear completion. -/ @[simps] def embedding : C ⥤ Free R C := { obj := λ X, X, map := λ X Y f, finsupp.single f 1, map_id' := λ X, rfl, map_comp' := λ X Y Z f g, by simp, } variables (R) {C} {D : Type u} [category.{v} D] [preadditive D] [linear R D] open preadditive linear /-- A functor to a preadditive category lifts to a functor from its `R`-linear completion. -/ @[simps] def lift (F : C ⥤ D) : Free R C ⥤ D := { obj := λ X, F.obj X, map := λ X Y f, f.sum (λ f' r, r • (F.map f')), map_id' := by { dsimp [category_theory.category_Free], simp }, map_comp' := λ X Y Z f g, begin apply finsupp.induction_linear f, { simp, }, { intros f₁ f₂ w₁ w₂, rw add_comp, rw [finsupp.sum_add_index, finsupp.sum_add_index], { simp [w₁, w₂, add_comp], }, { simp, }, { intros, simp only [add_smul], }, { simp, }, { intros, simp only [add_smul], }, }, { intros f' r, apply finsupp.induction_linear g, { simp, }, { intros f₁ f₂ w₁ w₂, rw comp_add, rw [finsupp.sum_add_index, finsupp.sum_add_index], { simp [w₁, w₂, add_comp], }, { simp, }, { intros, simp only [add_smul], }, { simp, }, { intros, simp only [add_smul], }, }, { intros g' s, erw single_comp_single, simp [mul_comm r s, mul_smul], } } end, } @[simp] lemma lift_map_single (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) (r : R) : (lift R F).map (single f r) = r • F.map f := by simp instance lift_additive (F : C ⥤ D) : (lift R F).additive := { map_zero' := by simp, map_add' := λ X Y f g, begin dsimp, rw finsupp.sum_add_index; simp [add_smul] end, } instance lift_linear (F : C ⥤ D) : (lift R F).linear R := { map_smul' := λ X Y f r, begin dsimp, rw finsupp.sum_smul_index; simp [finsupp.smul_sum, mul_smul], end, } /-- The embedding into the `R`-linear completion, followed by the lift, is isomorphic to the original functor. -/ def embedding_lift_iso (F : C ⥤ D) : embedding R C ⋙ lift R F ≅ F := nat_iso.of_components (λ X, iso.refl _) (by tidy) /-- Two `R`-linear functors out of the `R`-linear completion are isomorphic iff their compositions with the embedding functor are isomorphic. -/ @[ext] def ext {F G : Free R C ⥤ D} [F.additive] [F.linear R] [G.additive] [G.linear R] (α : embedding R C ⋙ F ≅ embedding R C ⋙ G) : F ≅ G := nat_iso.of_components (λ X, α.app X) begin intros X Y f, apply finsupp.induction_linear f, { simp, }, { intros f₁ f₂ w₁ w₂, simp only [F.map_add, G.map_add, add_comp, comp_add, w₁, w₂], }, { intros f' r, rw [iso.app_hom, iso.app_hom, ←smul_single_one, F.map_smul, G.map_smul, smul_comp, comp_smul], change r • (embedding R C ⋙ F).map f' ≫ _ = r • _ ≫ (embedding R C ⋙ G).map f', rw α.hom.naturality f', apply_instance, -- Why are these not picked up automatically when we rewrite? apply_instance, } end /-- `Free.lift` is unique amongst `R`-linear functors `Free R C ⥤ D` which compose with `embedding ℤ C` to give the original functor. -/ def lift_unique (F : C ⥤ D) (L : Free R C ⥤ D) [L.additive] [L.linear R] (α : embedding R C ⋙ L ≅ F) : L ≅ lift R F := ext R (α.trans (embedding_lift_iso R F).symm) end Free end category_theory
9e2bc7bfad73cb494f94dcf8971f9cc496797c14
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/rename_var.lean
2914bf630560dc83c9dbe944d77a209c53489083
[]
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
1,418
lean
/- Copyright (c) 2019 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.interactive import Mathlib.PostPort namespace Mathlib /-! # Rename bound variable tactic This files defines a tactic `rename_var` whose main purpose is to teach renaming of bound variables. * `rename_var old new` renames all bound variables named `old` to `new` in the goal. * `rename_var old new at h` does the same in hypothesis `h`. ```lean example (P : ℕ → ℕ → Prop) (h : ∀ n, ∃ m, P n m) : ∀ l, ∃ m, P l m := begin rename_var n q at h, -- h is now ∀ (q : ℕ), ∃ (m : ℕ), P q m, rename_var m n, -- goal is now ∀ (l : ℕ), ∃ (n : ℕ), P k n, exact h -- Lean does not care about those bound variable names end ``` ## Tags teaching, tactic -/ /-- Rename bound variable `old` to `new` in an `expr`-/ namespace tactic /-- Rename bound variable `old` to `new` in goal -/ /-- Rename bound variable `old` to `new` in assumption `h` -/ end tactic namespace tactic.interactive /-- `rename_var old new` renames all bound variables named `old` to `new` in the goal. `rename_var old new at h` does the same in hypothesis `h`. -/ end tactic.interactive /-- `rename_var old new` renames all bound variables named `old` to `new` in the goal.
e594d4397702d1e180f37246e8297f07b587be23
88fb7558b0636ec6b181f2a548ac11ad3919f8a5
/tests/lean/run/run_tactic1.lean
041314a4b38b193fd7a0732d29f914e630bb5ffe
[ "Apache-2.0" ]
permissive
moritayasuaki/lean
9f666c323cb6fa1f31ac597d777914aed41e3b7a
ae96ebf6ee953088c235ff7ae0e8c95066ba8001
refs/heads/master
1,611,135,440,814
1,493,852,869,000
1,493,852,869,000
90,269,903
0
0
null
1,493,906,291,000
1,493,906,291,000
null
UTF-8
Lean
false
false
717
lean
open tactic run_cmd tactic.trace "hello world" run_cmd do N ← to_expr `(nat), v ← to_expr `(10), add_decl (declaration.defn `val10 [] N v reducibility_hints.opaque tt) #eval val10 example : val10 = 10 := rfl meta definition mk_defs : nat → command | 0 := skip | (n+1) := do N ← to_expr `(nat), v ← to_expr (quote n), add_decl (declaration.defn (name.append_after `val n) [] N v reducibility_hints.opaque tt), mk_defs n run_cmd mk_defs 10 example : val_1 = 1 := rfl example : val_2 = 2 := rfl example : val_3 = 3 := rfl example : val_4 = 4 := rfl example : val_5 = 5 := rfl example : val_6 = 6 := rfl example : val_7 = 7 := rfl example : val_8 = 8 := rfl example : val_9 = 9 := rfl
6375385edcaca7244de444dd831d913f08600f39
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/topology/sheaves/local_predicate.lean
9eb31994e76fcf463edeac8400008e21e5b0ebcc
[ "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
11,616
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Scott Morrison, Adam Topaz -/ import topology.sheaves.sheaf_of_functions import topology.sheaves.stalks import topology.sheaves.sheaf_condition.unique_gluing /-! # Functions satisfying a local predicate form a sheaf. At this stage, in `topology/sheaves/sheaf_of_functions.lean` we've proved that not-necessarily-continuous functions from a topological space into some type (or type family) form a sheaf. Why do the continuous functions form a sheaf? The point is just that continuity is a local condition, so one can use the lifting condition for functions to provide a candidate lift, then verify that the lift is actually continuous by using the factorisation condition for the lift (which guarantees that on each open set it agrees with the functions being lifted, which were assumed to be continuous). This file abstracts this argument to work for any collection of dependent functions on a topological space satisfying a "local predicate". As an application, we check that continuity is a local predicate in this sense, and provide * `Top.sheaf_to_Top`: continuous functions into a topological space form a sheaf A sheaf constructed in this way has a natural map `stalk_to_fiber` from the stalks to the types in the ambient type family. We give conditions sufficient to show that this map is injective and/or surjective. -/ universe v noncomputable theory variables {X : Top.{v}} variables (T : X → Type v) open topological_space open opposite open category_theory open category_theory.limits open category_theory.limits.types namespace Top /-- Given a topological space `X : Top` and a type family `T : X → Type`, a `P : prelocal_predicate T` consists of: * a family of predicates `P.pred`, one for each `U : opens X`, of the form `(Π x : U, T x) → Prop` * a proof that if `f : Π x : V, T x` satisfies the predicate on `V : opens X`, then the restriction of `f` to any open subset `U` also satisfies the predicate. -/ structure prelocal_predicate := (pred : Π {U : opens X}, (Π x : U, T x) → Prop) (res : ∀ {U V : opens X} (i : U ⟶ V) (f : Π x : V, T x) (h : pred f), pred (λ x : U, f (i x))) variables (X) /-- Continuity is a "prelocal" predicate on functions to a fixed topological space `T`. -/ @[simps] def continuous_prelocal (T : Top.{v}) : prelocal_predicate (λ x : X, T) := { pred := λ U f, continuous f, res := λ U V i f h, continuous.comp h (opens.open_embedding_of_le i.le).continuous, } /-- Satisfying the inhabited linter. -/ instance inhabited_prelocal_predicate (T : Top.{v}) : inhabited (prelocal_predicate (λ x : X, T)) := ⟨continuous_prelocal X T⟩ variables {X} /-- Given a topological space `X : Top` and a type family `T : X → Type`, a `P : local_predicate T` consists of: * a family of predicates `P.pred`, one for each `U : opens X`, of the form `(Π x : U, T x) → Prop` * a proof that if `f : Π x : V, T x` satisfies the predicate on `V : opens X`, then the restriction of `f` to any open subset `U` also satisfies the predicate, and * a proof that given some `f : Π x : U, T x`, if for every `x : U` we can find an open set `x ∈ V ≤ U` so that the restriction of `f` to `V` satisfies the predicate, then `f` itself satisfies the predicate. -/ structure local_predicate extends prelocal_predicate T := (locality : ∀ {U : opens X} (f : Π x : U, T x) (w : ∀ x : U, ∃ (V : opens X) (m : x.1 ∈ V) (i : V ⟶ U), pred (λ x : V, f (i x : U))), pred f) variables (X) /-- Continuity is a "local" predicate on functions to a fixed topological space `T`. -/ def continuous_local (T : Top.{v}) : local_predicate (λ x : X, T) := { locality := λ U f w, begin apply continuous_iff_continuous_at.2, intro x, specialize w x, rcases w with ⟨V, m, i, w⟩, dsimp at w, rw continuous_iff_continuous_at at w, specialize w ⟨x, m⟩, simpa using (opens.open_embedding_of_le i.le).continuous_at_iff.1 w, end, ..continuous_prelocal X T } /-- Satisfying the inhabited linter. -/ instance inhabited_local_predicate (T : Top.{v}) : inhabited (local_predicate _) := ⟨continuous_local X T⟩ variables {X T} /-- Given a `P : prelocal_predicate`, we can always construct a `local_predicate` by asking that the condition from `P` holds locally near every point. -/ def prelocal_predicate.sheafify {T : X → Type v} (P : prelocal_predicate T) : local_predicate T := { pred := λ U f, ∀ x : U, ∃ (V : opens X) (m : x.1 ∈ V) (i : V ⟶ U), P.pred (λ x : V, f (i x : U)), res := λ V U i f w x, begin specialize w (i x), rcases w with ⟨V', m', i', p⟩, refine ⟨V ⊓ V', ⟨x.2,m'⟩, opens.inf_le_left _ _, _⟩, convert P.res (opens.inf_le_right V V') _ p, end, locality := λ U f w x, begin specialize w x, rcases w with ⟨V, m, i, p⟩, specialize p ⟨x.1, m⟩, rcases p with ⟨V', m', i', p'⟩, exact ⟨V', m', i' ≫ i, p'⟩, end } lemma prelocal_predicate.sheafify_of {T : X → Type v} {P : prelocal_predicate T} {U : opens X} {f : Π x : U, T x} (h : P.pred f) : P.sheafify.pred f := λ x, ⟨U, x.2, 𝟙 _, by { convert h, ext ⟨y, w⟩, refl, }⟩ /-- The subpresheaf of dependent functions on `X` satisfying the "pre-local" predicate `P`. -/ @[simps] def subpresheaf_to_Types (P : prelocal_predicate T) : presheaf (Type v) X := { obj := λ U, { f : Π x : unop U, T x // P.pred f }, map := λ U V i f, ⟨λ x, f.1 (i.unop x), P.res i.unop f.1 f.2⟩ }. namespace subpresheaf_to_Types variables (P : prelocal_predicate T) /-- The natural transformation including the subpresheaf of functions satisfying a local predicate into the presheaf of all functions. -/ def subtype : subpresheaf_to_Types P ⟶ presheaf_to_Types X T := { app := λ U f, f.1 } open Top.presheaf /-- The functions satisfying a local predicate satisfy the sheaf condition. -/ lemma is_sheaf (P : local_predicate T) : (subpresheaf_to_Types P.to_prelocal_predicate).is_sheaf := presheaf.is_sheaf_of_is_sheaf_unique_gluing_types _ $ λ ι U sf sf_comp, begin -- We show the sheaf condition in terms of unique gluing. -- First we obtain a family of sections for the underlying sheaf of functions, -- by forgetting that the prediacte holds let sf' : Π i : ι, (presheaf_to_Types X T).obj (op (U i)) := λ i, (sf i).val, -- Since our original family is compatible, this one is as well have sf'_comp : (presheaf_to_Types X T).is_compatible U sf' := λ i j, congr_arg subtype.val (sf_comp i j), -- So, we can obtain a unique gluing obtain ⟨gl,gl_spec,gl_uniq⟩ := (sheaf_to_Types X T).exists_unique_gluing U sf' sf'_comp, refine ⟨⟨gl,_⟩,_,_⟩, { -- Our first goal is to show that this chosen gluing satisfies the -- predicate. Of course, we use locality of the predicate. apply P.locality, rintros ⟨x, mem⟩, -- Once we're at a particular point `x`, we can select some open set `x ∈ U i`. choose i hi using opens.mem_supr.mp mem, -- We claim that the predicate holds in `U i` use [U i, hi, opens.le_supr U i], -- This follows, since our original family `sf` satisfies the predicate convert (sf i).property, exact gl_spec i }, -- It remains to show that the chosen lift is really a gluing for the subsheaf and -- that it is unique. Both of which follow immediately from the corresponding facts -- in the sheaf of functions without the local predicate. { intro i, ext1, exact (gl_spec i) }, { intros gl' hgl', ext1, exact gl_uniq gl'.1 (λ i, congr_arg subtype.val (hgl' i)) }, end end subpresheaf_to_Types /-- The subsheaf of the sheaf of all dependently typed functions satisfying the local predicate `P`. -/ @[simps] def subsheaf_to_Types (P : local_predicate T) : sheaf (Type v) X := ⟨subpresheaf_to_Types P.to_prelocal_predicate, subpresheaf_to_Types.is_sheaf P⟩ /-- There is a canonical map from the stalk to the original fiber, given by evaluating sections. -/ def stalk_to_fiber (P : local_predicate T) (x : X) : (subsheaf_to_Types P).1.stalk x ⟶ T x := begin refine colimit.desc _ { X := T x, ι := { app := λ U f, _, naturality' := _ } }, { exact f.1 ⟨x, (unop U).2⟩, }, { tidy, } end @[simp] lemma stalk_to_fiber_germ (P : local_predicate T) (U : opens X) (x : U) (f) : stalk_to_fiber P x ((subsheaf_to_Types P).1.germ x f) = f.1 x := begin dsimp [presheaf.germ, stalk_to_fiber], cases x, simp, refl, end /-- The `stalk_to_fiber` map is surjective at `x` if every point in the fiber `T x` has an allowed section passing through it. -/ lemma stalk_to_fiber_surjective (P : local_predicate T) (x : X) (w : ∀ (t : T x), ∃ (U : open_nhds x) (f : Π y : U.1, T y) (h : P.pred f), f ⟨x, U.2⟩ = t) : function.surjective (stalk_to_fiber P x) := λ t, begin rcases w t with ⟨U, f, h, rfl⟩, fsplit, { exact (subsheaf_to_Types P).1.germ ⟨x, U.2⟩ ⟨f, h⟩, }, { exact stalk_to_fiber_germ _ U.1 ⟨x, U.2⟩ ⟨f, h⟩, } end /-- The `stalk_to_fiber` map is injective at `x` if any two allowed sections which agree at `x` agree on some neighborhood of `x`. -/ lemma stalk_to_fiber_injective (P : local_predicate T) (x : X) (w : ∀ (U V : open_nhds x) (fU : Π y : U.1, T y) (hU : P.pred fU) (fV : Π y : V.1, T y) (hV : P.pred fV) (e : fU ⟨x, U.2⟩ = fV ⟨x, V.2⟩), ∃ (W : open_nhds x) (iU : W ⟶ U) (iV : W ⟶ V), ∀ (w : W.1), fU (iU w : U.1) = fV (iV w : V.1)) : function.injective (stalk_to_fiber P x) := λ tU tV h, begin -- We promise to provide all the ingredients of the proof later: let Q : ∃ (W : (open_nhds x)ᵒᵖ) (s : Π w : (unop W).1, T w) (hW : P.pred s), tU = (subsheaf_to_Types P).1.germ ⟨x, (unop W).2⟩ ⟨s, hW⟩ ∧ tV = (subsheaf_to_Types P).1.germ ⟨x, (unop W).2⟩ ⟨s, hW⟩ := _, { choose W s hW e using Q, exact e.1.trans e.2.symm, }, -- Then use induction to pick particular representatives of `tU tV : stalk x` obtain ⟨U, ⟨fU, hU⟩, rfl⟩ := jointly_surjective' tU, obtain ⟨V, ⟨fV, hV⟩, rfl⟩ := jointly_surjective' tV, { -- Decompose everything into its constituent parts: dsimp, simp only [stalk_to_fiber, types.colimit.ι_desc_apply] at h, specialize w (unop U) (unop V) fU hU fV hV h, rcases w with ⟨W, iU, iV, w⟩, -- and put it back together again in the correct order. refine ⟨(op W), (λ w, fU (iU w : (unop U).1)), P.res _ _ hU, _⟩, rcases W with ⟨W, m⟩, exact ⟨colimit_sound iU.op (subtype.eq rfl), colimit_sound iV.op (subtype.eq (funext w).symm)⟩, }, end /-- Some repackaging: the presheaf of functions satisfying `continuous_prelocal` is just the same thing as the presheaf of continuous functions. -/ def subpresheaf_continuous_prelocal_iso_presheaf_to_Top (T : Top.{v}) : subpresheaf_to_Types (continuous_prelocal X T) ≅ presheaf_to_Top X T := nat_iso.of_components (λ X, { hom := by { rintro ⟨f, c⟩, exact ⟨f, c⟩, }, inv := by { rintro ⟨f, c⟩, exact ⟨f, c⟩, }, hom_inv_id' := by { ext ⟨f, p⟩ x, refl, }, inv_hom_id' := by { ext ⟨f, p⟩ x, refl, }, }) (by tidy) /-- The sheaf of continuous functions on `X` with values in a space `T`. -/ def sheaf_to_Top (T : Top.{v}) : sheaf (Type v) X := ⟨presheaf_to_Top X T, presheaf.is_sheaf_of_iso (subpresheaf_continuous_prelocal_iso_presheaf_to_Top T) (subpresheaf_to_Types.is_sheaf (continuous_local X T))⟩ end Top
0c047bbd9636adc458b91a3670af3651d6083618
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/multiset/pi.lean
ba0ab37525e660aced3e198b0c55f326a4ef3c90
[ "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
4,794
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl -/ import data.multiset.nodup /-! # The cartesian product of multisets -/ namespace multiset section pi variables {α : Type*} [decidable_eq α] {δ : α → Type*} open function /-- Given `δ : α → Type*`, a multiset `m` and a term `a`, as well as a term `b : δ a` and a function `f` such that `f a' : δ a'` for all `a'` in `m`, `pi.cons m a b f` is a function `g` such that `g a'' : δ a''` for all `a''` in `a :: m`. -/ def pi.cons (m : multiset α) (a : α) (b : δ a) (f : Πa∈m, δ a) : Πa'∈a::m, δ a' := λa' ha', if h : a' = a then eq.rec b h.symm else f a' $ (mem_cons.1 ha').resolve_left h /-- Given `δ : α → Type*`, `pi.empty δ` is the trivial dependent function out of the empty multiset. -/ def pi.empty (δ : α → Type*) : (Πa∈(0:multiset α), δ a) . lemma pi.cons_same {m : multiset α} {a : α} {b : δ a} {f : Πa∈m, δ a} (h : a ∈ a :: m) : pi.cons m a b f a h = b := dif_pos rfl lemma pi.cons_ne {m : multiset α} {a a' : α} {b : δ a} {f : Πa∈m, δ a} (h' : a' ∈ a :: m) (h : a' ≠ a) : pi.cons m a b f a' h' = f a' ((mem_cons.1 h').resolve_left h) := dif_neg h lemma pi.cons_swap {a a' : α} {b : δ a} {b' : δ a'} {m : multiset α} {f : Πa∈m, δ a} (h : a ≠ a') : pi.cons (a' :: m) a b (pi.cons m a' b' f) == pi.cons (a :: m) a' b' (pi.cons m a b f) := begin apply hfunext, { refl }, intros a'' _ h, subst h, apply hfunext, { rw [cons_swap] }, intros ha₁ ha₂ h, by_cases h₁ : a'' = a; by_cases h₂ : a'' = a'; simp [*, pi.cons_same, pi.cons_ne] at *, { subst h₁, rw [pi.cons_same, pi.cons_same] }, { subst h₂, rw [pi.cons_same, pi.cons_same] } end /-- `pi m t` constructs the Cartesian product over `t` indexed by `m`. -/ def pi (m : multiset α) (t : Πa, multiset (δ a)) : multiset (Πa∈m, δ a) := m.rec_on {pi.empty δ} (λa m (p : multiset (Πa∈m, δ a)), (t a).bind $ λb, p.map $ pi.cons m a b) begin intros a a' m n, by_cases eq : a = a', { subst eq }, { simp [map_bind, bind_bind (t a') (t a)], apply bind_hcongr, { rw [cons_swap a a'] }, intros b hb, apply bind_hcongr, { rw [cons_swap a a'] }, intros b' hb', apply map_hcongr, { rw [cons_swap a a'] }, intros f hf, exact pi.cons_swap eq } end @[simp] lemma pi_zero (t : Πa, multiset (δ a)) : pi 0 t = pi.empty δ :: 0 := rfl @[simp] lemma pi_cons (m : multiset α) (t : Πa, multiset (δ a)) (a : α) : pi (a :: m) t = ((t a).bind $ λb, (pi m t).map $ pi.cons m a b) := rec_on_cons a m lemma pi_cons_injective {a : α} {b : δ a} {s : multiset α} (hs : a ∉ s) : function.injective (pi.cons s a b) := assume f₁ f₂ eq, funext $ assume a', funext $ assume h', have ne : a ≠ a', from assume h, hs $ h.symm ▸ h', have a' ∈ a :: s, from mem_cons_of_mem h', calc f₁ a' h' = pi.cons s a b f₁ a' this : by rw [pi.cons_ne this ne.symm] ... = pi.cons s a b f₂ a' this : by rw [eq] ... = f₂ a' h' : by rw [pi.cons_ne this ne.symm] lemma card_pi (m : multiset α) (t : Πa, multiset (δ a)) : card (pi m t) = prod (m.map $ λa, card (t a)) := multiset.induction_on m (by simp) (by simp [mul_comm] {contextual := tt}) lemma nodup_pi {s : multiset α} {t : Πa, multiset (δ a)} : nodup s → (∀a∈s, nodup (t a)) → nodup (pi s t) := multiset.induction_on s (assume _ _, nodup_singleton _) begin assume a s ih hs ht, have has : a ∉ s, by simp at hs; exact hs.1, have hs : nodup s, by simp at hs; exact hs.2, simp, split, { assume b hb, from nodup_map (pi_cons_injective has) (ih hs $ assume a' h', ht a' $ mem_cons_of_mem h') }, { apply pairwise_of_nodup _ (ht a $ mem_cons_self _ _), from assume b₁ hb₁ b₂ hb₂ neb, disjoint_map_map.2 (assume f hf g hg eq, have pi.cons s a b₁ f a (mem_cons_self _ _) = pi.cons s a b₂ g a (mem_cons_self _ _), by rw [eq], neb $ show b₁ = b₂, by rwa [pi.cons_same, pi.cons_same] at this) } end lemma mem_pi (m : multiset α) (t : Πa, multiset (δ a)) : ∀f:Πa∈m, δ a, (f ∈ pi m t) ↔ (∀a (h : a ∈ m), f a h ∈ t a) := begin refine multiset.induction_on m (λ f, _) (λ a m ih f, _), { simpa using show f = pi.empty δ, by funext a ha; exact ha.elim }, simp, split, { rintro ⟨b, hb, f', hf', rfl⟩ a' ha', rw [ih] at hf', by_cases a' = a, { subst h, rwa [pi.cons_same] }, { rw [pi.cons_ne _ h], apply hf' } }, { intro hf, refine ⟨_, hf a (mem_cons_self a _), λa ha, f a (mem_cons_of_mem ha), (ih _).2 (λ a' h', hf _ _), _⟩, funext a' h', by_cases a' = a, { subst h, rw [pi.cons_same] }, { rw [pi.cons_ne _ h] } } end end pi end multiset
e1ac1cf7c92b8483de10bb861503795d3aff3684
8cd4726d66eec7673bcc0325fed07d5ba5bf17c4
/hw8.lean
e6957a6350371938afd7d3f2c704aa2a6d7c8626
[]
no_license
justinqcai/CS2102
8c5fddedffa6147fedd4b6ee7d5d39fc21f0ddab
d309f0db3f1df52eb77206ee1e8665a3b49d7a0c
refs/heads/master
1,590,108,991,894
1,557,610,044,000
1,557,610,044,000
186,064,169
0
0
null
null
null
null
UTF-8
Lean
false
false
8,320
lean
/- Justin Cai, jc5pz -/ /- 1a. Implement a function, bool_implies, that takes two bool values, b1 and b2 (not propositions), and that returns the the value of b1 → b2. Here the → operator means implication in Boolean algebra. To know what your function should do, write out the truth table for implication. You may not use Lean's → operator (which does work for bool values, by the way) in your answer. -/ def bool_implies : bool → bool → bool | tt tt := tt | tt ff := ff | ff tt := tt | ff ff := tt /- 1b. Given your implementation of bool_implies, prove the following proposition. Use induction rather than the cases tactic to do case analysis on Boolean values. -/ example : ∀ b1 b2 : bool, (bool_implies b1 b2 = tt) → (b2 = tt ∨ b1 = ff) := begin intros b1 b2, assume bitt, induction b1, apply or.inr, exact rfl, induction b2, contradiction, apply or.inl, exact rfl, end /- 2a. A tree of natural numbers is either empty or it is constructed from a natural number and two smaller trees of natural numbers. Give an inductive definition of the type of such trees. Call you datatype tree_nat. Your type will have two constructors. Call the first one "empty", and call the second "node". Hint: The second will take three arguments. -/ inductive tree_nat : Type | empty : tree_nat | node : ℕ → tree_nat → tree_nat → tree_nat /- 2b. Define aTree to be a node containing the value 3 and two trees, the first one empty and the second one a node containing the value 2 and two empty trees. -/ def aTree := tree_nat.node 3 tree_nat.empty (tree_nat.node 2 tree_nat.empty tree_nat.empty) /- 3. Define a polymorphic type, "tree", just like tree_nat, but where the value stored in a node can be of any type, T. Then define aTree' to be the same as aTree except that it's now of type tree rather than of type tree_nat. Make the type argument implicit. Finally define a tree of strings, aTree'', just like aTree' except that 3 is replaced by "Hi!" and 2 is replaced by "Jo". -/ -- Your answer here inductive tree {T: Type}: Type | empty : tree | node : T → tree → tree → tree def aTree' := tree.node 3 tree.empty (tree.node 2 tree.empty tree.empty) def aTree'' := tree.node("Hi!") tree.empty (tree.node "Jo" tree.empty tree.empty) /- 4. Write a recursive function, num_nodes, that takes a value of type tree T, as an argument, where T is some type, and that returns the number of nodes in the tree. The number of nodes in an empty tree is zero, while the number of nodes in a non-empty tree is one (for the "top" node) plus the number of nodes in each of the subtrees. The "at sign" before "tree" in the following function signature tells Lean that even though tree takes its type argument implicitly, in this case we want to give it explicitly. We need to specify T explicitly here because Lean has no way of knowing that's what we want. -/ def num_nodes : ∀ {T : Type}, @tree T → nat | T tree.empty := 0 | T (tree.node n1 n2 n3) := 1 + num_nodes(n2) + num_nodes(n3) /- The following questions use our definition of the nas to practice proof by induction. Here is our nat type and the implementations of addition and multiplication. -/ inductive mynat : Type | zero : mynat | succ : mynat → mynat def add_mynat: mynat → mynat → mynat | mynat.zero m := m | (mynat.succ n') m := mynat.succ (add_mynat n' m) def mult_mynat: mynat → mynat → mynat | mynat.zero m := mynat.zero | (mynat.succ n') m := add_mynat m (mult_mynat n' m) /- Here's a proof that zero is a right identity for addition. We explain details in comments. You will want to use some of the same ideas in your proofs. -/ theorem zero_right_id_add : ∀ (n : mynat), add_mynat n mynat.zero = n := begin -- forall introduction intro n, -- induction induction n with n' ih, -- base case exact rfl, -- recursive case -- first simplify based on definition of add_mynat simp [add_mynat], -- now apply induction hypothesis exact ih, end /- #5 Prove the following by induction on n in Lean. -/ -- 5b. Prove that succ (n + m) = n + (succ m). theorem add_succ': ∀ (n m : mynat) , mynat.succ (add_mynat n m) = add_mynat n (mynat.succ m) := begin intros n m, induction n with n' ih, simp[add_mynat], simp[add_mynat], assumption, end theorem add_succ : ∀ (n m : mynat), mynat.succ (add_mynat n m) = add_mynat n (mynat.succ m) := begin intros n m, induction n with n' ih, exact rfl, simp [add_mynat], assumption, end -- 5a. Prove zero is a right identity for mult. theorem zero_right_absorb_mult' : ∀ (n : mynat), mult_mynat n mynat.zero = mynat.zero := begin intro n, induction n with n' ih, trivial, simp[mult_mynat], assumption, end theorem zero_right_absorb_mult : ∀ (n : mynat), mult_mynat n mynat.zero = mynat.zero := begin intros n, induction n with n' ih, exact rfl, induction n', exact rfl, simp [mult_mynat], assumption, end -- 5b. Prove addition is associative. theorem addition_assoc' : ∀ (n m p : mynat), add_mynat (add_mynat n m) p = add_mynat n (add_mynat m p) := begin intros n m p, induction n with n' ih, trivial, simp[add_mynat], assumption, end theorem addition_assoc : ∀ (n m p : mynat), add_mynat (add_mynat n m) p = add_mynat n (add_mynat m p) := begin intros n m p, induction n, exact rfl, simp [add_mynat], assumption, end -- 5c. Prove addition is commutative. lemma add_n_succ_m': ∀ n m : mynat, add_mynat n (mynat.succ m) = mynat.succ (add_mynat n m) := begin intros n m, induction n with n' ih, trivial, simp[add_mynat], assumption, end lemma add_n_succ_m : ∀ n m : mynat, add_mynat n (mynat.succ m) = mynat.succ (add_mynat n m) := begin intros n m, induction n with n' h, apply rfl, simp[add_mynat], assumption, end theorem addition_comm' : ∀ (n m : mynat), add_mynat n m = add_mynat m n := begin intros n m, induction n, simp[add_mynat], induction m, trivial, simp[add_mynat], assumption, simp[add_mynat], rw n_ih, rw add_succ, end theorem addition_comm : ∀ (n m : mynat), add_mynat n m = add_mynat m n := begin intros n m, induction n with n' ih, induction m with m' ih2, exact rfl, simp[add_mynat], assumption, simp[add_mynat], rw ih, rw add_n_succ_m, -- induction m with m' ih', -- simp [add_mynat], -- simp[add_mynat], end /- 6a. Complete then test the following definition of a function that computes the n'th Fibonacci number when given n as an argument. -/ def fib' : ℕ → ℕ | nat.zero := 0 | (nat.succ nat.zero) := 1 | (n + 2) := fib'(n + 1) + fib'(n) def fib : ℕ → ℕ | nat.zero := 0 | (nat.succ nat.zero) := 1 | (n + 2 ) := (fib (n+1)) + fib n #eval fib 8 /- 6b. Implement the factorial function. You will need to define the function for both its base and recursive cases. -/ def fac : ℕ → ℕ | nat.zero := 1 | (nat.succ n) := (nat.succ n) * (fac n) /- 7. Give an *informal* proof by induction of the proposition that forall natural numbers, n, the sum of the natural numbers from 0 to n is n * (n + 1) / 2. 1. Let n be a number. Prove by induction a. Base case 1. The sum of all numbers from 0-n is equal to n * (n + 1) / 2 2. The sum of all numbers from 0-n is 0 if n = 0 b. Inductive case 1. Assume relationship is true for P(n) and prove for P(n+1) 2. 0+1+...+(n-1)+n = n*(n + 1)/2 3. Substitute 0+1+...+(n-1)+n with n * (n + 1) / 2 4. Simplify left side by commutative property: (n + 1) * (n + 2) / 2 5. Distribute: (n^2 + 3n + 2) / 2 6. Distribute the left side: (n^2 + 3n + 2) / 2 + (n + 1) = (n^2 + 3n + 2) 7. Separate terms: (n^2) / (2+)n / 2 + (n+1) = (n^2) / (2+3n) / (2+2) / 2 8. Eliminate from both sides: (n + 1) = 2n / 2 + 2 / 2 9. Simplify: (n + 1) = n + 1 2. Proven for base and induction case, therefore this is true for all natrual numbers -/
49b8a55c2c6cb9f495a7da1729d8fe493a5aaf6c
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/number_theory/zeta_function.lean
cda925a279f9f805af3c95b36a8186a1dc5ac675
[ "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
41,361
lean
/- Copyright (c) 2023 David Loeffler. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Loeffler -/ import analysis.special_functions.gamma.beta import number_theory.modular_forms.jacobi_theta.basic import number_theory.zeta_values /-! # Definition of the Riemann zeta function > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Main definitions: * `riemann_zeta`: the Riemann zeta function `ζ : ℂ → ℂ`. * `riemann_completed_zeta`: the completed zeta function `Λ : ℂ → ℂ`, which satisfies `Λ(s) = π ^ (-s / 2) Γ(s / 2) ζ(s)` (away from the poles of `Γ(s / 2)`). * `riemann_completed_zeta₀`: the entire function `Λ₀` satisfying `Λ₀(s) = Λ(s) + 1 / (s - 1) - 1 / s` wherever the RHS is defined. Note that mathematically `ζ(s)` is undefined at `s = 1`, while `Λ(s)` is undefined at both `s = 0` and `s = 1`. Our construction assigns some values at these points (which are not arbitrary, but I haven't checked exactly what they are). ## Main results: * `differentiable_completed_zeta₀` : the function `Λ₀(s)` is entire. * `differentiable_at_completed_zeta` : the function `Λ(s)` is differentiable away from `s = 0` and `s = 1`. * `differentiable_at_riemann_zeta` : the function `ζ(s)` is differentiable away from `s = 1`. * `zeta_eq_tsum_of_one_lt_re` : for `1 < re s`, we have `ζ(s) = ∑' (n : ℕ), 1 / (n + 1) ^ s`. * `riemann_completed_zeta₀_one_sub`, `riemann_completed_zeta_one_sub`, and `riemann_zeta_one_sub` : functional equation relating values at `s` and `1 - s` * `riemann_zeta_neg_nat_eq_bernoulli` : for any `k ∈ ℕ` we have the formula `riemann_zeta (-k) = (-1) ^ k * bernoulli (k + 1) / (k + 1)` * `riemann_zeta_two_mul_nat`: formula for `ζ(2 * k)` for `k ∈ ℕ, k ≠ 0` in terms of Bernoulli numbers ## Outline of proofs: We define two related functions on the reals, `zeta_kernel₁` and `zeta_kernel₂`. The first is `(θ (t * I) - 1) / 2`, where `θ` is Jacobi's theta function; its Mellin transform is exactly the completed zeta function. The second is obtained by subtracting a linear combination of powers on the interval `Ioc 0 1` to give a function with exponential decay at both `0` and `∞`. We then define `riemann_completed_zeta₀` as the Mellin transform of the second zeta kernel, and define `riemann_completed_zeta` and `riemann_zeta` from this. Since `zeta_kernel₂` has rapid decay and satisfies a functional equation relating its values at `t` and `1 / t`, we deduce the analyticity of `riemann_completed_zeta₀` and the functional equation relating its values at `s` and `1 - s`. On the other hand, since `zeta_kernel₁` can be expanded in powers of `exp (-π * t)` and the Mellin transform integrated term-by-term, we obtain the relation to the naive Dirichlet series `∑' (n : ℕ), 1 / (n + 1) ^ s`. -/ open measure_theory set filter asymptotics topological_space real asymptotics open complex (hiding exp norm_eq_abs abs_of_nonneg abs_two continuous_exp) open_locale topology real nat noncomputable theory /-! ## Definition of the Riemann zeta function and related functions -/ /-- Function whose Mellin transform is `π ^ (-s) * Γ(s) * zeta (2 * s)`, for `1 / 2 < Re s`. -/ def zeta_kernel₁ (t : ℝ) : ℂ := ∑' (n : ℕ), rexp (-π * t * (n + 1) ^ 2) /-- Modified zeta kernel, whose Mellin transform is entire. --/ def zeta_kernel₂ : ℝ → ℂ := zeta_kernel₁ + indicator (Ioc 0 1) (λ t, (1 - 1 / sqrt t) / 2) /-- The completed Riemann zeta function with its poles removed, `Λ(s) + 1 / s - 1 / (s - 1)`. -/ def riemann_completed_zeta₀ (s : ℂ) : ℂ := mellin zeta_kernel₂ (s / 2) /-- The completed Riemann zeta function, `Λ(s)`, which satisfies `Λ(s) = π ^ (-s / 2) Γ(s / 2) ζ(s)` (up to a minor correction at `s = 0`). -/ def riemann_completed_zeta (s : ℂ) : ℂ := riemann_completed_zeta₀ s - 1 / s + 1 / (s - 1) /-- The Riemann zeta function `ζ(s)`. We set this to be irreducible to hide messy implementation details. -/ @[irreducible] def riemann_zeta := function.update (λ s : ℂ, ↑π ^ (s / 2) * riemann_completed_zeta s / Gamma (s / 2)) 0 (-1 / 2) /- Note the next lemma is true by definition; what's hard is to show that with this definition, `ζ` is continuous (and indeed analytic) at 0, see `differentiable_riemann_zeta` below. -/ /-- We have `ζ(0) = -1 / 2`. -/ lemma riemann_zeta_zero : riemann_zeta 0 = -1 / 2 := begin unfold riemann_zeta, exact function.update_same _ _ _ end /-! ## First properties of the zeta kernels -/ /-- The sum defining `zeta_kernel₁` is convergent. -/ lemma summable_exp_neg_pi_mul_nat_sq {t : ℝ} (ht : 0 < t) : summable (λ n : ℕ, rexp (-π * t * (n + 1) ^ 2)) := begin have : 0 < (↑t * I).im, by rwa [of_real_mul_im, I_im, mul_one], convert summable_norm_iff.mpr (has_sum_nat_jacobi_theta this).summable, ext1 n, rw [complex.norm_eq_abs, complex.abs_exp], rw show ↑π * I * (↑n + 1) ^ 2 * (↑t * I) = ↑(π * t * (n + 1) ^ 2) * I ^ 2, by { push_cast, ring }, rw [I_sq, mul_neg_one, ←of_real_neg, of_real_re, neg_mul, neg_mul], end /-- Relate `zeta_kernel₁` to the Jacobi theta function on `ℍ`. (We don't use this as the definition of `zeta_kernel₁`, since the sum over `ℕ` rather than `ℤ` is more convenient for relating zeta to the Dirichlet series for `re s > 1`.) -/ lemma zeta_kernel₁_eq_jacobi_theta {t : ℝ} (ht : 0 < t) : zeta_kernel₁ t = (jacobi_theta (t * I) - 1) / 2 := begin rw [jacobi_theta_eq_tsum_nat ((mul_I_im t).symm ▸ ht : 0 < (↑t * I).im), add_comm, add_sub_cancel, mul_div_cancel_left _ (two_ne_zero' ℂ), zeta_kernel₁], congr' 1 with n : 1, push_cast, rw [(by ring : ↑π * I * (n + 1) ^ 2 * (t * I) = I ^ 2 * π * t * (n + 1) ^ 2), I_sq, neg_one_mul], end /-- Continuity of `zeta_kernel₁`. -/ lemma continuous_at_zeta_kernel₁ {t : ℝ} (ht : 0 < t) : continuous_at zeta_kernel₁ t := begin have : continuous_at (λ u : ℝ, (jacobi_theta (u * I) - 1) / 2) t, { refine (continuous_at.sub _ continuous_at_const).div_const _, refine (continuous_at_jacobi_theta _).comp (continuous_at.mul _ continuous_at_const), { rwa [mul_I_im, of_real_re] }, { exact continuous_of_real.continuous_at } }, refine this.congr (eventually_of_mem (Ioi_mem_nhds ht) (λ u hu, _)), rw zeta_kernel₁_eq_jacobi_theta hu, end /-- Local integrability of `zeta_kernel₁`. -/ lemma locally_integrable_zeta_kernel₁ : locally_integrable_on zeta_kernel₁ (Ioi 0) := (continuous_at.continuous_on $ λ t ht, continuous_at_zeta_kernel₁ ht).locally_integrable_on measurable_set_Ioi /-- Local integrability of `zeta_kernel₂`. -/ lemma locally_integrable_zeta_kernel₂ : locally_integrable_on zeta_kernel₂ (Ioi 0) := begin refine (locally_integrable_on_iff (or.inr is_open_Ioi)).mpr (λ k hk hk', integrable.add _ _), { refine continuous_on.integrable_on_compact hk' _, exact continuous_at.continuous_on (λ x hx, continuous_at_zeta_kernel₁ (hk hx)) }, { refine (integrable_indicator_iff measurable_set_Ioc).mpr _, rw [integrable_on, measure.restrict_restrict, ←integrable_on], swap, { exact measurable_set_Ioc }, apply continuous_on.integrable_on_compact, { convert (is_compact_Icc : is_compact $ Icc 0 1).inter hk' using 1, exact set.ext (λ t, ⟨λ h, ⟨Ioc_subset_Icc_self h.1, h.2⟩, λ h, ⟨⟨hk h.2, h.1.2⟩, h.2⟩⟩) }, { refine continuous_on.mono _ ((inter_subset_right _ _).trans hk), refine (continuous_on_const.sub _).div_const _, refine continuous_on.div continuous_on_const _ (λ x hx, _), { exact (continuous_of_real.comp continuous_sqrt).continuous_on }, exact of_real_ne_zero.mpr (sqrt_ne_zero'.mpr hx) } } end /-- Functional equation for `zeta_kernel₂`. -/ lemma zeta_kernel₂_one_div {t : ℝ} (ht : 0 < t) : zeta_kernel₂ (1 / t) = sqrt t * zeta_kernel₂ t := begin have aux : ∀ {u : ℝ} (hu : 1 < u), zeta_kernel₂ (1 / u) = sqrt u * zeta_kernel₂ u, { intros u hu, simp_rw [zeta_kernel₂, pi.add_apply], rw [indicator_of_mem, indicator_of_not_mem (not_mem_Ioc_of_gt hu), add_zero], swap, { exact ⟨one_div_pos.mpr (zero_lt_one.trans hu), (one_div u).symm ▸ (inv_le_one hu.le)⟩ }, rw [zeta_kernel₁_eq_jacobi_theta (one_div_pos.mpr $ zero_lt_one.trans hu), zeta_kernel₁_eq_jacobi_theta (zero_lt_one.trans hu), ←add_div, ←mul_div_assoc, add_sub, sub_add_cancel, sqrt_div zero_le_one, sqrt_one, one_div (sqrt _), of_real_inv, ←one_div, one_div_one_div, mul_sub, mul_one], congr' 2, let τ : upper_half_plane := ⟨u * I, (mul_I_im u).symm ▸ (zero_lt_one.trans hu)⟩, convert jacobi_theta_S_smul τ using 2, { rw [upper_half_plane.modular_S_smul, upper_half_plane.coe_mk, subtype.coe_mk, ←neg_inv, mul_inv, inv_I, mul_neg, neg_neg, one_div, of_real_inv], }, { rw [subtype.coe_mk, mul_comm, mul_assoc, mul_neg, I_mul_I, neg_neg, mul_one, sqrt_eq_rpow, of_real_cpow (zero_lt_one.trans hu).le], push_cast } }, rcases lt_trichotomy 1 t with h | rfl | h, { exact aux h }, { simp only [div_self, ne.def, one_ne_zero, not_false_iff, sqrt_one, of_real_one, one_mul], }, { have := aux (show 1 < 1 / t, by rwa [lt_one_div (zero_lt_one' ℝ) ht, div_one]), rw one_div_one_div at this, rw [this, ←mul_assoc, ←of_real_mul, ←sqrt_mul ht.le, mul_one_div_cancel ht.ne', sqrt_one, of_real_one, one_mul] }, end /-! ## Bounds for zeta kernels We now establish asymptotic bounds for the zeta kernels as `t → ∞` and `t → 0`, and use these to show holomorphy of their Mellin transforms (for `1 / 2 < re s` for `zeta_kernel₁`, and all `s` for `zeta_kernel₂`). -/ /-- Bound for `zeta_kernel₁` for large `t`. -/ lemma is_O_at_top_zeta_kernel₁ : is_O at_top zeta_kernel₁ (λ t, exp (-π * t)) := begin have h := (is_O_at_im_infty_jacobi_theta_sub_one).const_mul_left (1 / 2), simp_rw [(mul_comm (1 / 2 : ℂ) _), mul_one_div] at h, have h' : tendsto (λ t : ℝ, ↑t * I) at_top (comap im at_top), { rw tendsto_comap_iff, convert tendsto_id, ext1 t, rw [function.comp_app, mul_I_im, of_real_re, id.def] }, convert ((h.norm_left.comp_tendsto h').congr' (eventually_of_mem (Ioi_mem_at_top 0) (λ t ht, _)) (eventually_of_mem (Ioi_mem_at_top 0) (λ t ht, _))).of_norm_left, { rw [function.comp_app, ←zeta_kernel₁_eq_jacobi_theta ht] }, { rw [function.comp_app, mul_I_im, of_real_re] } end /-- Bound for `zeta_kernel₂` for large `t`. -/ lemma is_O_at_top_zeta_kernel₂ : is_O at_top zeta_kernel₂ (λ t, exp (-π * t)) := begin refine (eventually_eq_of_mem (Ioi_mem_at_top (1 : ℝ)) (λ t ht, _)).trans_is_O is_O_at_top_zeta_kernel₁, rw [zeta_kernel₂, pi.add_apply, indicator_of_not_mem (not_mem_Ioc_of_gt ht), add_zero], end /-- Precise but awkward-to-use bound for `zeta_kernel₂` for `t → 0`. -/ lemma is_O_zero_zeta_kernel₂ : is_O (𝓝[>] 0) zeta_kernel₂ (λ t, exp (-π / t) / sqrt t) := begin have h1 := (is_O_at_top_zeta_kernel₂).comp_tendsto tendsto_inv_zero_at_top, simp_rw ←one_div at h1, have h2 : (zeta_kernel₂ ∘ has_div.div 1) =ᶠ[𝓝[>] 0] λ t, sqrt t * zeta_kernel₂ t, from eventually_of_mem self_mem_nhds_within (λ t ht, by simp_rw ←zeta_kernel₂_one_div ht), have h3 := (h1.congr' h2 (eventually_eq.refl _ _)), have h4 := h3.mul (is_O_refl (λ t : ℝ, 1 / (sqrt t : ℂ)) (𝓝[>] 0)).norm_right, refine h4.congr' _ _, { refine eventually_of_mem self_mem_nhds_within (λ x hx, _), simp_rw [←mul_assoc], rw [mul_comm, ←mul_assoc, one_div_mul_cancel, one_mul], exact of_real_ne_zero.mpr ((sqrt_ne_zero $ le_of_lt hx).mpr (ne_of_gt hx)) }, { refine eventually_of_mem self_mem_nhds_within (λ x hx, _), dsimp only, rw [function.comp_app, mul_one_div, one_div (↑(sqrt _)), ←of_real_inv, is_R_or_C.norm_of_real, abs_inv, abs_of_nonneg (sqrt_nonneg _), ←div_eq_mul_inv] }, end /-- Weaker but more usable bound for `zeta_kernel₂` for `t → 0`. -/ lemma is_O_zero_zeta_kernel₂_rpow (a : ℝ) : is_O (𝓝[>] 0) zeta_kernel₂ (λ t, t ^ a) := begin have aux1 : is_O at_top (λ t, exp (-π * t)) (λ t, t ^ (-a - 1 / 2)), from (is_o_exp_neg_mul_rpow_at_top pi_pos _).is_O, have aux2 : is_O at_top (λ t, exp (-π * t) * sqrt t) (λ t, t ^ (-a)), { refine (aux1.mul (is_O_refl sqrt _)).congr' (eventually_eq.refl _ _) _, refine (eventually_gt_at_top 0).mp (eventually_of_forall (λ t ht, _)), simp_rw [sqrt_eq_rpow, ←rpow_add ht, sub_add_cancel] }, refine is_O_zero_zeta_kernel₂.trans ((aux2.comp_tendsto tendsto_inv_zero_at_top).congr' _ _), { refine eventually_of_mem self_mem_nhds_within (λ x hx, _), simp_rw [function.comp_app, sqrt_inv, ←div_eq_mul_inv] }, { refine eventually_of_mem self_mem_nhds_within (λ x hx, _), simp_rw [function.comp_app, inv_rpow (le_of_lt hx), rpow_neg (le_of_lt hx), inv_inv] } end /-- Bound for `zeta_kernel₁` for `t → 0`. -/ lemma is_O_zero_zeta_kernel₁ : is_O (𝓝[>] 0) zeta_kernel₁ (λ t, t ^ (-(1 / 2) : ℝ)) := begin have : zeta_kernel₁ =ᶠ[𝓝[>] 0] zeta_kernel₂ + (λ t, (1 / sqrt t - 1) / 2), { refine eventually_eq_of_mem (Ioc_mem_nhds_within_Ioi $ left_mem_Ico.mpr zero_lt_one) (λ t h, _), rw [pi.add_apply, zeta_kernel₂, pi.add_apply, indicator_of_mem h], ring }, refine ((is_O_zero_zeta_kernel₂_rpow _).add _).congr' this.symm (eventually_eq.refl _ _), simp_rw sub_div, apply is_O.sub, { apply is_O.of_norm_left, simp_rw [norm_div, norm_one, div_eq_mul_inv, one_mul, mul_comm _ (‖(2 : ℂ)‖)⁻¹], refine ((is_O_refl _ _).congr' (eventually_eq.refl _ _) (eventually_eq_of_mem self_mem_nhds_within (λ x hx, _))).const_mul_left _, rw [is_R_or_C.norm_of_real, abs_of_nonneg (sqrt_nonneg _)], simp_rw [sqrt_eq_rpow, rpow_neg (le_of_lt hx), one_div] }, { refine is_O_iff.mpr ⟨‖(1 / 2 : ℂ)‖, _⟩, refine eventually_of_mem (Ioc_mem_nhds_within_Ioi $ left_mem_Ico.mpr zero_lt_one) (λ t ht, _), refine le_mul_of_one_le_right (norm_nonneg _) _, rw [norm_of_nonneg (rpow_nonneg_of_nonneg ht.1.le _), rpow_neg ht.1.le], exact one_le_inv (rpow_pos_of_pos ht.1 _) (rpow_le_one ht.1.le ht.2 one_half_pos.le) } end /-! ## Differentiability of the completed zeta function -/ /-- The Mellin transform of the first zeta kernel is holomorphic for `1 / 2 < re s`. -/ lemma differentiable_at_mellin_zeta_kernel₁ {s : ℂ} (hs : 1 / 2 < s.re) : differentiable_at ℂ (mellin zeta_kernel₁) s := mellin_differentiable_at_of_is_O_rpow_exp pi_pos locally_integrable_zeta_kernel₁ is_O_at_top_zeta_kernel₁ is_O_zero_zeta_kernel₁ hs /-- The Mellin transform of the second zeta kernel is entire. -/ lemma differentiable_mellin_zeta_kernel₂ : differentiable ℂ (mellin zeta_kernel₂) := λ s, mellin_differentiable_at_of_is_O_rpow_exp pi_pos locally_integrable_zeta_kernel₂ is_O_at_top_zeta_kernel₂ (is_O_zero_zeta_kernel₂_rpow _) ((sub_lt_self_iff _).mpr zero_lt_one) /-- The modified completed Riemann zeta function `Λ(s) + 1 / s - 1 / (s - 1)` is entire. -/ theorem differentiable_completed_zeta₀ : differentiable ℂ riemann_completed_zeta₀ := differentiable_mellin_zeta_kernel₂.comp (differentiable.div_const differentiable_id 2) /-- The completed Riemann zeta function `Λ(s)` is differentiable away from `s = 0` and `s = 1` (where it has simple poles). -/ theorem differentiable_at_completed_zeta {s : ℂ} (hs : s ≠ 0) (hs' : s ≠ 1) : differentiable_at ℂ riemann_completed_zeta s := begin refine (differentiable_completed_zeta₀.differentiable_at.sub _).add _, { exact (differentiable.differentiable_at (differentiable_const _)).div differentiable_at_id hs }, { refine ((differentiable_const _).differentiable_at).div _ (sub_ne_zero.mpr hs'), exact differentiable_at_id.sub (differentiable_at_const _) }, end /-- The Riemann zeta function is differentiable away from `s = 1`. -/ theorem differentiable_at_riemann_zeta {s : ℂ} (hs' : s ≠ 1) : differentiable_at ℂ riemann_zeta s := begin /- First claim: the result holds at `t` for `t ≠ 0`. Note we will need to use this for the case `s = 0` also, as a hypothesis for the removable-singularity criterion. -/ have c1 : ∀ (t : ℂ) (ht : t ≠ 0) (ht' : t ≠ 1), differentiable_at ℂ (λ u : ℂ, ↑π ^ (u / 2) * riemann_completed_zeta u / Gamma (u / 2)) t, { intros t ht ht', apply differentiable_at.mul, { refine (differentiable_at.const_cpow _ _).mul (differentiable_at_completed_zeta ht ht'), { exact differentiable_at.div_const differentiable_at_id _ }, { exact or.inl (of_real_ne_zero.mpr pi_pos.ne') } }, { refine differentiable_one_div_Gamma.differentiable_at.comp t _, exact differentiable_at.div_const differentiable_at_id _ } }, /- Second claim: the limit at `s = 0` exists and is equal to `-1 / 2`. -/ have c2 : tendsto (λ s : ℂ, ↑π ^ (s / 2) * riemann_completed_zeta s / Gamma (s / 2)) (𝓝[≠] 0) (𝓝 $ -1 / 2), { have h1 : tendsto (λ z : ℂ, (π : ℂ) ^ (z / 2)) (𝓝 0) (𝓝 1), { convert (continuous_at_const_cpow (of_real_ne_zero.mpr pi_pos.ne')).comp _, { simp_rw [function.comp_app, zero_div, cpow_zero] }, { exact continuous_at_id.div continuous_at_const two_ne_zero } }, suffices h2 : tendsto (λ z, riemann_completed_zeta z / Gamma (z / 2)) (𝓝[≠] 0) (𝓝 $ -1 / 2), { convert (h1.mono_left nhds_within_le_nhds).mul h2, { ext1 x, rw mul_div }, { simp only [one_mul] } }, suffices h3 : tendsto (λ z, (riemann_completed_zeta z * (z / 2)) / (z / 2 * Gamma (z / 2))) (𝓝[≠] 0) (𝓝 $ -1 / 2), { refine tendsto.congr' (eventually_eq_of_mem self_mem_nhds_within (λ z hz, _)) h3, rw [←div_div, mul_div_cancel _ (div_ne_zero hz two_ne_zero)] }, have h4 : tendsto (λ z : ℂ, z / 2 * Gamma (z / 2)) (𝓝[≠] 0) (𝓝 1), { refine tendsto_self_mul_Gamma_nhds_zero.comp _, rw [tendsto_nhds_within_iff, (by simp : 𝓝 (0 : ℂ) = 𝓝 (0 / 2))], exact ⟨(tendsto_id.div_const _).mono_left nhds_within_le_nhds, eventually_of_mem self_mem_nhds_within (λ x hx, div_ne_zero hx two_ne_zero)⟩ }, suffices : tendsto (λ z, riemann_completed_zeta z * z / 2) (𝓝[≠] 0) (𝓝 (-1 / 2 : ℂ)), { have := this.div h4 one_ne_zero, simp_rw [div_one, mul_div_assoc] at this, exact this }, refine tendsto.div _ tendsto_const_nhds two_ne_zero, simp_rw [riemann_completed_zeta, add_mul, sub_mul], rw show 𝓝 (-1 : ℂ) = 𝓝 (0 - 1 + 0), by rw [zero_sub, add_zero], refine (tendsto.sub _ _).add _, { refine tendsto.mono_left _ nhds_within_le_nhds, have : continuous_at riemann_completed_zeta₀ 0, from (differentiable_completed_zeta₀).continuous.continuous_at, simpa only [id.def, mul_zero] using tendsto.mul this tendsto_id }, { refine tendsto_const_nhds.congr' (eventually_eq_of_mem self_mem_nhds_within (λ t ht, _)), simp_rw one_div_mul_cancel ht }, { refine tendsto.mono_left _ nhds_within_le_nhds, suffices : continuous_at (λ z : ℂ, 1 / (z - 1)) 0, by simpa only [id.def, mul_zero] using tendsto.mul this tendsto_id, refine continuous_at_const.div (continuous_at_id.sub continuous_at_const) _, simpa only [zero_sub] using neg_ne_zero.mpr one_ne_zero } }, -- Now the main proof. rcases ne_or_eq s 0 with hs | rfl, { -- The easy case: `s ≠ 0` have : {(0 : ℂ)}ᶜ ∈ 𝓝 s, from is_open_compl_singleton.mem_nhds hs, refine (c1 s hs hs').congr_of_eventually_eq (eventually_eq_of_mem this (λ x hx, _)), unfold riemann_zeta, apply function.update_noteq hx }, { -- The hard case: `s = 0`. rw [riemann_zeta, ←(lim_eq_iff ⟨-1 / 2, c2⟩).mpr c2], have S_nhds : {(1 : ℂ)}ᶜ ∈ 𝓝 (0 : ℂ), from is_open_compl_singleton.mem_nhds hs', refine ((complex.differentiable_on_update_lim_of_is_o S_nhds (λ t ht, (c1 t ht.2 ht.1).differentiable_within_at) _) 0 hs').differentiable_at S_nhds, simp only [zero_div, div_zero, complex.Gamma_zero, mul_zero, cpow_zero, sub_zero], -- Remains to show completed zeta is `o (s ^ (-1))` near 0. refine (is_O_const_of_tendsto c2 $ one_ne_zero' ℂ).trans_is_o _, rw is_o_iff_tendsto', { exact tendsto.congr (λ x, by rw [←one_div, one_div_one_div]) nhds_within_le_nhds }, { exact eventually_of_mem self_mem_nhds_within (λ x hx hx', (hx $ inv_eq_zero.mp hx').elim) } } end /-- The trivial zeroes of the zeta function. -/ lemma riemann_zeta_neg_two_mul_nat_add_one (n : ℕ) : riemann_zeta (-2 * (n + 1)) = 0 := begin have : (-2 : ℂ) * (n + 1) ≠ 0, from mul_ne_zero (neg_ne_zero.mpr two_ne_zero) (nat.cast_add_one_ne_zero n), rw [riemann_zeta, function.update_noteq this, (show (-2) * ((n : ℂ) + 1) / 2 = -↑(n + 1), by { push_cast, ring }), complex.Gamma_neg_nat_eq_zero, div_zero], end /-- A formal statement of the Riemann hypothesis – constructing a term of this type is worth a million dollars. -/ def riemann_hypothesis : Prop := ∀ (s : ℂ) (hs : riemann_completed_zeta s = 0) (hs' : ¬∃ (n : ℕ), s = -2 * (n + 1)), s.re = 1 / 2 /-! ## Relating the Mellin transforms of the two zeta kernels -/ lemma has_mellin_one_div_sqrt_Ioc {s : ℂ} (hs : 1 / 2 < re s) : has_mellin (indicator (Ioc 0 1) (λ t, 1 / ↑(sqrt t) : ℝ → ℂ)) s (1 / (s - 1 / 2)) := begin have h1 : eq_on (λ t, 1 / ↑(sqrt t) : ℝ → ℂ) (λ t, ↑t ^ (-1 / 2 : ℂ)) (Ioc 0 1), { intros t ht, simp_rw [neg_div, cpow_neg, ←one_div, sqrt_eq_rpow, of_real_cpow ht.1.le], push_cast }, simp_rw [indicator_congr h1, (by ring : s - 1/2 = s + (-1) / 2)], convert has_mellin_cpow_Ioc (-1 / 2) _, rwa [(by push_cast : (-1 / 2 : ℂ) = (-1 / 2 : ℝ)), of_real_re, neg_div, ←sub_eq_add_neg, sub_pos] end /-- Evaluate the Mellin transform of the "fudge factor" in `zeta_kernel₂` -/ lemma has_mellin_one_div_sqrt_sub_one_div_two_Ioc {s : ℂ} (hs : 1 / 2 < s.re) : has_mellin ((Ioc 0 1).indicator (λ t, (1 - 1 / (sqrt t : ℂ)) / 2)) s (1 / (2 * s) - 1 / (2 * s - 1)) := begin have step1 : has_mellin (indicator (Ioc 0 1) (λ t, 1 - 1 / ↑(sqrt t) : ℝ → ℂ)) s (1 / s - 1 / (s - 1 / 2)), { have a := has_mellin_one_Ioc (one_half_pos.trans hs), have b := has_mellin_one_div_sqrt_Ioc hs, simpa only [a.2, b.2, ←indicator_sub] using has_mellin_sub a.1 b.1 }, -- todo: implement something like "indicator.const_div" (blocked by the port for now) rw (show (Ioc 0 1).indicator (λ t, (1 - 1 / (sqrt t : ℂ)) / 2) = λ t, ((Ioc 0 1).indicator (λ t, (1 - 1 / (sqrt t : ℂ))) t) / 2, by { ext1 t, simp_rw [div_eq_inv_mul, indicator_mul_right] }), simp_rw [has_mellin, mellin_div_const, step1.2, sub_div, div_div], refine ⟨step1.1.div_const _, _⟩, rw [mul_comm, sub_mul, div_mul_cancel _ (two_ne_zero' ℂ), mul_comm s 2], end lemma mellin_zeta_kernel₂_eq_of_lt_re {s : ℂ} (hs : 1 / 2 < s.re) : mellin zeta_kernel₂ s = mellin zeta_kernel₁ s + 1 / (2 * s) - 1 / (2 * s - 1) := begin have h := mellin_convergent_of_is_O_rpow_exp pi_pos locally_integrable_zeta_kernel₁ is_O_at_top_zeta_kernel₁ is_O_zero_zeta_kernel₁ hs, have h' := has_mellin_one_div_sqrt_sub_one_div_two_Ioc hs, simp_rw [zeta_kernel₂, pi.add_def, add_sub_assoc, (has_mellin_add h h'.1).2, h'.2], end lemma completed_zeta_eq_mellin_of_one_lt_re {s : ℂ} (hs : 1 < re s) : riemann_completed_zeta s = mellin zeta_kernel₁ (s / 2) := begin have : 1 / 2 < (s / 2).re, { rw (show s / 2 = ↑(2⁻¹ : ℝ) * s, by { push_cast, rw mul_comm, refl }), rwa [of_real_mul_re, ←div_eq_inv_mul, div_lt_div_right (zero_lt_two' ℝ)] }, rw [riemann_completed_zeta, riemann_completed_zeta₀, mellin_zeta_kernel₂_eq_of_lt_re this, sub_add, sub_sub, ←add_sub], conv_rhs { rw ←add_zero (mellin zeta_kernel₁ $ s / 2) }, congr' 1, rw mul_div_cancel' _ (two_ne_zero' ℂ), abel end /-! ## Relating the first zeta kernel to the Dirichlet series -/ /-- Auxiliary lemma for `mellin_zeta_kernel₁_eq_tsum`, computing the Mellin transform of an individual term in the series. -/ lemma integral_cpow_mul_exp_neg_pi_mul_sq {s : ℂ} (hs : 0 < s.re) (n : ℕ) : ∫ t : ℝ in Ioi 0, (t : ℂ) ^ (s - 1) * rexp (-π * t * (n + 1) ^ 2) = ↑π ^ -s * complex.Gamma s * (1 / (n + 1) ^ (2 * s)) := begin rw [complex.Gamma_eq_integral hs, Gamma_integral_eq_mellin], conv_rhs { congr, rw [←smul_eq_mul, ←mellin_comp_mul_left _ _ pi_pos] }, have : (1 / ((n : ℂ) + 1) ^ (2 * s)) = ↑(((n : ℝ) + 1) ^ (2 : ℝ)) ^ (-s), { rw [(by push_cast: ((n : ℂ) + 1) = ↑( (n : ℝ) + 1)), (by push_cast : (2 * s) = (↑(2 : ℝ) * s)), cpow_mul_of_real_nonneg, one_div, cpow_neg], rw [←nat.cast_succ], exact nat.cast_nonneg _ }, conv_rhs { rw [this, mul_comm, ←smul_eq_mul] }, rw [← mellin_comp_mul_right _ _ (show 0 < ((n : ℝ) + 1) ^ (2 : ℝ), by positivity)], refine set_integral_congr measurable_set_Ioi (λ t ht, _), simp_rw smul_eq_mul, congr' 3, conv_rhs { rw [←nat.cast_two, rpow_nat_cast] }, ring end lemma mellin_zeta_kernel₁_eq_tsum {s : ℂ} (hs : 1 / 2 < s.re): mellin zeta_kernel₁ s = π ^ (-s) * Gamma s * ∑' (n : ℕ), 1 / (n + 1) ^ (2 * s) := begin let bd : ℕ → ℝ → ℝ := λ n t, t ^ (s.re - 1) * exp (-π * t * (n + 1) ^ 2), let f : ℕ → ℝ → ℂ := λ n t, t ^ (s - 1) * exp (-π * t * (n + 1) ^ 2), have hm : measurable_set (Ioi (0:ℝ)), from measurable_set_Ioi, have h_norm : ∀ (n : ℕ) {t : ℝ} (ht : 0 < t), ‖f n t‖ = bd n t, { intros n t ht, rw [norm_mul, complex.norm_eq_abs, complex.norm_eq_abs, complex.abs_of_nonneg (exp_pos _).le, abs_cpow_eq_rpow_re_of_pos ht, sub_re, one_re] }, have hf_meas : ∀ (n : ℕ), ae_strongly_measurable (f n) (volume.restrict $ Ioi 0), { intro n, refine (continuous_on.mul _ _).ae_strongly_measurable hm, { exact (continuous_at.continuous_on (λ x hx, continuous_at_of_real_cpow_const _ _ $ or.inr $ ne_of_gt hx)) }, { apply continuous.continuous_on, exact continuous_of_real.comp (continuous_exp.comp ((continuous_const.mul continuous_id').mul continuous_const)) } }, have h_le : ∀ (n : ℕ), ∀ᵐ (t : ℝ) ∂volume.restrict (Ioi 0), ‖f n t‖ ≤ bd n t, from λ n, (ae_restrict_iff' hm).mpr (ae_of_all _ (λ t ht, le_of_eq (h_norm n ht))), have h_sum0 : ∀ {t : ℝ} (ht : 0 < t), has_sum (λ n, f n t) (t ^ (s - 1) * zeta_kernel₁ t), { intros t ht, have := (has_sum_of_real.mpr (summable_exp_neg_pi_mul_nat_sq ht).has_sum).mul_left ((t : ℂ) ^ (s - 1)), simpa only [of_real_mul, ←mul_assoc, of_real_bit0, of_real_one, mul_comm _ (2 : ℂ), of_real_sub, of_real_one, of_real_tsum] using this }, have h_sum' : ∀ᵐ (t : ℝ) ∂volume.restrict (Ioi 0), has_sum (λ (n : ℕ), f n t) (t ^ (s - 1) * zeta_kernel₁ t), from (ae_restrict_iff' hm).mpr (ae_of_all _ (λ t ht, h_sum0 ht)), have h_sum : ∀ᵐ (t : ℝ) ∂volume.restrict (Ioi 0), summable (λ n : ℕ, bd n t), { refine (ae_restrict_iff' hm).mpr (ae_of_all _ (λ t ht, _)), simpa only [λ n, h_norm n ht] using summable_norm_iff.mpr (h_sum0 ht).summable }, have h_int : integrable (λ t : ℝ, ∑' (n : ℕ), bd n t) (volume.restrict (Ioi 0)), { refine integrable_on.congr_fun (mellin_convergent_of_is_O_rpow_exp pi_pos locally_integrable_zeta_kernel₁ is_O_at_top_zeta_kernel₁ is_O_zero_zeta_kernel₁ hs).norm (λ t ht, _) hm, simp_rw [tsum_mul_left, norm_smul, complex.norm_eq_abs ((t : ℂ) ^ _), abs_cpow_eq_rpow_re_of_pos ht, sub_re, one_re], rw [zeta_kernel₁, ←of_real_tsum, complex.norm_eq_abs, complex.abs_of_nonneg], exact tsum_nonneg (λ n, (exp_pos _).le) }, simpa only [integral_cpow_mul_exp_neg_pi_mul_sq (one_half_pos.trans hs), tsum_mul_left] using (has_sum_integral_of_dominated_convergence bd hf_meas h_le h_sum h_int h_sum').tsum_eq.symm, end lemma completed_zeta_eq_tsum_of_one_lt_re {s : ℂ} (hs : 1 < re s) : riemann_completed_zeta s = π ^ (-s / 2) * Gamma (s / 2) * ∑' (n : ℕ), 1 / (n + 1) ^ s := begin rw [completed_zeta_eq_mellin_of_one_lt_re hs, mellin_zeta_kernel₁_eq_tsum, neg_div, mul_div_cancel' _ (two_ne_zero' ℂ)], rw (show s / 2 = ↑(2⁻¹ : ℝ) * s, by { push_cast, rw mul_comm, refl }), rwa [of_real_mul_re, ←div_eq_inv_mul, div_lt_div_right (zero_lt_two' ℝ)] end /-- The Riemann zeta function agrees with the naive Dirichlet-series definition when the latter converges. (Note that this is false without the assumption: when `re s ≤ 1` the sum is divergent, and we use a different definition to obtain the analytic continuation to all `s`.) -/ theorem zeta_eq_tsum_one_div_nat_add_one_cpow {s : ℂ} (hs : 1 < re s) : riemann_zeta s = ∑' (n : ℕ), 1 / (n + 1) ^ s := begin have : s ≠ 0, by { contrapose! hs, rw [hs, zero_re], exact zero_le_one }, rw [riemann_zeta, function.update_noteq this, completed_zeta_eq_tsum_of_one_lt_re hs, ←mul_assoc, neg_div, cpow_neg, mul_inv_cancel_left₀, mul_div_cancel_left], { apply Gamma_ne_zero_of_re_pos, rw [←of_real_one, ←of_real_bit0, div_eq_mul_inv, ←of_real_inv, mul_comm, of_real_mul_re], exact mul_pos (inv_pos_of_pos two_pos) (zero_lt_one.trans hs), }, { rw [ne.def, cpow_eq_zero_iff, not_and_distrib, ←ne.def, of_real_ne_zero], exact or.inl (pi_pos.ne') } end /-- Alternate formulation of `zeta_eq_tsum_one_div_nat_add_one_cpow` without the `+ 1`, using the fact that for `s ≠ 0` we define `0 ^ s = 0`. -/ lemma zeta_eq_tsum_one_div_nat_cpow {s : ℂ} (hs : 1 < re s) : riemann_zeta s = ∑' (n : ℕ), 1 / n ^ s := begin have hs' : s ≠ 0, by { contrapose! hs, rw [hs, zero_re], exact zero_le_one }, rw [tsum_eq_zero_add], { simp_rw [nat.cast_zero, zero_cpow hs', div_zero, zero_add, zeta_eq_tsum_one_div_nat_add_one_cpow hs, nat.cast_add, nat.cast_one] }, { rw ←summable_norm_iff, simp_rw [norm_div, norm_one, complex.norm_eq_abs, ←of_real_nat_cast, abs_cpow_eq_rpow_re_of_nonneg (nat.cast_nonneg _) (zero_lt_one.trans hs).ne', summable_one_div_nat_rpow], assumption } end /-- Special case of `zeta_eq_tsum_one_div_nat_cpow` when the argument is in `ℕ`, so the power function can be expressed using naïve `pow` rather than `cpow`. -/ lemma zeta_nat_eq_tsum_of_gt_one {k : ℕ} (hk : 1 < k) : riemann_zeta k = ∑' (n : ℕ), 1 / n ^ k := by simp only [zeta_eq_tsum_one_div_nat_cpow (by rwa [←of_real_nat_cast, of_real_re, ←nat.cast_one, nat.cast_lt] : 1 < re k), cpow_nat_cast] /-- Explicit formula for `ζ (2 * k)`, for `k ∈ ℕ` with `k ≠ 0`: we have `ζ (2 * k) = (-1) ^ (k + 1) * 2 ^ (2 * k - 1) * π ^ (2 * k) * bernoulli (2 * k) / (2 * k)!`. Compare `has_sum_zeta_nat` for a version formulated explicitly as a sum, and `riemann_zeta_neg_nat_eq_bernoulli` for values at negative integers (equivalent to the above via the functional equation). -/ lemma riemann_zeta_two_mul_nat {k : ℕ} (hk : k ≠ 0) : riemann_zeta (2 * k) = (-1) ^ (k + 1) * 2 ^ (2 * k - 1) * π ^ (2 * k) * bernoulli (2 * k) / (2 * k)! := begin convert congr_arg (coe : ℝ → ℂ) (has_sum_zeta_nat hk).tsum_eq, { rw [←nat.cast_two, ←nat.cast_mul, zeta_nat_eq_tsum_of_gt_one], { push_cast }, { refine (one_lt_two).trans_le _, conv_lhs { rw ←mul_one 2 }, rwa [mul_le_mul_left (zero_lt_two' ℕ), nat.one_le_iff_ne_zero] } }, { push_cast } end lemma riemann_zeta_two : riemann_zeta 2 = π ^ 2 / 6 := begin convert congr_arg coe has_sum_zeta_two.tsum_eq, { rw [←nat.cast_two, zeta_nat_eq_tsum_of_gt_one one_lt_two, of_real_tsum], push_cast }, { push_cast } end lemma riemann_zeta_four : riemann_zeta 4 = π ^ 4 / 90 := begin convert congr_arg coe has_sum_zeta_four.tsum_eq, { rw [←nat.cast_one, ←nat.cast_bit0, ←nat.cast_bit0, zeta_nat_eq_tsum_of_gt_one (by norm_num : 1 < 4), of_real_tsum], push_cast }, { push_cast } end /-! ## Functional equation -/ /-- Riemann zeta functional equation, formulated for `Λ₀`: for any complex `s` we have `Λ₀(1 - s) = Λ₀ s`. -/ lemma riemann_completed_zeta₀_one_sub (s : ℂ) : riemann_completed_zeta₀ (1 - s) = riemann_completed_zeta₀ s := begin have := mellin_comp_rpow (zeta_kernel₂) (s / 2 - 1 / 2) neg_one_lt_zero.ne, simp_rw [rpow_neg_one, ←one_div, abs_neg, abs_one, div_one, one_smul, of_real_neg, of_real_one, div_neg, div_one, neg_sub] at this, conv_lhs { rw [riemann_completed_zeta₀, sub_div, ←this] }, refine set_integral_congr measurable_set_Ioi (λ t ht, _), simp_rw [zeta_kernel₂_one_div ht, smul_eq_mul, ←mul_assoc, sqrt_eq_rpow, of_real_cpow (le_of_lt ht), ←cpow_add _ _ (of_real_ne_zero.mpr $ ne_of_gt ht)], congr' 2, push_cast, ring, end /-- Riemann zeta functional equation, formulated for `Λ`: for any complex `s` we have `Λ (1 - s) = Λ s`. -/ lemma riemann_completed_zeta_one_sub (s : ℂ) : riemann_completed_zeta (1 - s) = riemann_completed_zeta s := by simp_rw [riemann_completed_zeta, riemann_completed_zeta₀_one_sub, sub_add, (by abel : 1 - s - 1 = -s), (by abel : 1 - s = -(s - 1)), div_neg, neg_sub_neg] /-- Riemann zeta functional equation, formulated for `ζ`: if `1 - s ∉ ℕ`, then we have `ζ (1 - s) = 2 ^ (1 - s) * π ^ (-s) * Γ s * sin (π * (1 - s) / 2) * ζ s`. -/ lemma riemann_zeta_one_sub {s : ℂ} (hs : ∀ (n : ℕ), s ≠ -n) (hs' : s ≠ 1) : riemann_zeta (1 - s) = 2 ^ (1 - s) * π ^ (-s) * Gamma s * sin (π * (1 - s) / 2) * riemann_zeta s := begin -- Deducing this from the previous formulations is quite involved. The proof uses two -- nontrivial facts (the doubling formula and reflection formula for Gamma) and a lot of careful -- rearrangement, requiring several non-vanishing statements as input to `field_simp`. have hs_ne : s ≠ 0, by { contrapose! hs, rw hs, exact ⟨0, by rw [nat.cast_zero, neg_zero]⟩ }, have h_sqrt : (sqrt π : ℂ) ≠ 0, from of_real_ne_zero.mpr (sqrt_ne_zero'.mpr pi_pos), have h_pow : (2 : ℂ) ^ (s - 1) ≠ 0, { rw [ne.def, cpow_eq_zero_iff, not_and_distrib], exact or.inl two_ne_zero }, have h_Ga_ne1 : Gamma (s / 2) ≠ 0, { rw [ne.def, complex.Gamma_eq_zero_iff], contrapose! hs, obtain ⟨m, hm⟩ := hs, rw [div_eq_iff (two_ne_zero' ℂ), ←nat.cast_two, neg_mul, ←nat.cast_mul] at hm, exact ⟨m * 2, by rw hm⟩ }, have h_Ga_eq : Gamma s = Gamma (s / 2) * Gamma ((s + 1) / 2) * 2 ^ (s - 1) / sqrt π, { rw [add_div, complex.Gamma_mul_Gamma_add_half, mul_div_cancel' _ (two_ne_zero' ℂ), (by ring : 1 - s = -(s - 1)), cpow_neg, ←div_eq_mul_inv, eq_div_iff h_sqrt, div_mul_eq_mul_div₀, div_mul_cancel _ h_pow] }, have h_Ga_ne3 : Gamma ((s + 1) / 2) ≠ 0, { have h_Ga_aux : Gamma s ≠ 0, from complex.Gamma_ne_zero hs, contrapose! h_Ga_aux, rw [h_Ga_eq, h_Ga_aux, mul_zero, zero_mul, zero_div] }, rw [riemann_zeta, function.update_noteq (by rwa [sub_ne_zero, ne_comm] : 1 - s ≠ 0), function.update_noteq hs_ne, riemann_completed_zeta_one_sub, mul_div, eq_div_iff h_Ga_ne1, mul_comm, ←mul_div_assoc], -- Now rule out case of s = positive odd integer & deduce further non-vanishing statements by_cases hs_pos_odd : ∃ (n : ℕ), s = 1 + 2 * n, { -- Note the case n = 0 (i.e. s = 1) works OK here, but only because we have used -- `function.update_noteq` to change the goal; the original goal is genuinely false for s = 1. obtain ⟨n, rfl⟩ := hs_pos_odd, have : (1 - (1 + 2 * (n : ℂ))) / 2 = -↑n, { rw [←sub_sub, sub_self, zero_sub, neg_div, mul_div_cancel_left _ (two_ne_zero' ℂ)] }, rw [this, complex.Gamma_neg_nat_eq_zero, div_zero], have : (π : ℂ) * (1 - (1 + 2 * ↑n)) / 2 = ↑(-n : ℤ) * π, { push_cast, field_simp, ring }, rw [this, complex.sin_int_mul_pi, mul_zero, zero_mul] }, have h_Ga_ne4 : Gamma ((1 - s) / 2) ≠ 0, { rw [ne.def, complex.Gamma_eq_zero_iff], contrapose! hs_pos_odd, obtain ⟨m, hm⟩ := hs_pos_odd, rw [div_eq_iff (two_ne_zero' ℂ), sub_eq_iff_eq_add, neg_mul, ←sub_eq_neg_add, eq_sub_iff_add_eq] at hm, exact ⟨m, by rw [←hm, mul_comm]⟩ }, -- At last the main proof rw show sin (↑π * (1 - s) / 2) = π * (Gamma ((1 - s) / 2) * Gamma (s / 2 + 1 / 2))⁻¹, by { have := congr_arg has_inv.inv (complex.Gamma_mul_Gamma_one_sub ((1 - s) / 2)).symm, rwa [(by ring : 1 - (1 - s) / 2 = s / 2 + 1 / 2), inv_div, div_eq_iff (of_real_ne_zero.mpr pi_pos.ne'), mul_comm _ ↑π, mul_div_assoc'] at this }, rw [(by rw ←neg_sub : (2 : ℂ) ^ (1 - s) = 2 ^ -(s - 1)), cpow_neg, h_Ga_eq], suffices : (π : ℂ) ^ ((1 - s) / 2) = π ^ -s * sqrt π * π ^ (s / 2), { rw this, field_simp, ring_nf, rw [←of_real_pow, sq_sqrt pi_pos.le], ring }, simp_rw [sqrt_eq_rpow, of_real_cpow pi_pos.le, ←cpow_add _ _ (of_real_ne_zero.mpr pi_pos.ne')], congr' 1, push_cast, field_simp, ring, end lemma riemann_zeta_neg_nat_eq_bernoulli (k : ℕ) : riemann_zeta (-k) = (-1) ^ k * bernoulli (k + 1) / (k + 1) := begin rcases nat.even_or_odd' k with ⟨m, rfl | rfl⟩, { cases m, { -- k = 0 : evaluate explicitly rw [mul_zero, nat.cast_zero, pow_zero, one_mul, zero_add, neg_zero, zero_add, div_one, bernoulli_one, riemann_zeta_zero, rat.cast_div, rat.cast_neg, rat.cast_one, rat.cast_bit0, rat.cast_one] }, { -- k = 2 * (m + 1) : both sides "trivially" zero rw [nat.cast_mul, ←neg_mul, nat.cast_two, nat.cast_succ, riemann_zeta_neg_two_mul_nat_add_one, bernoulli_eq_bernoulli'_of_ne_one], swap, { apply ne_of_gt, norm_num }, rw [bernoulli'_odd_eq_zero ⟨m + 1, rfl⟩ (by norm_num), rat.cast_zero, mul_zero, zero_div] } }, { -- k = 2 * m + 1 : the interesting case rw odd.neg_one_pow ⟨m, rfl⟩, rw (show -(↑(2 * m + 1) : ℂ) = 1 - (2 * m + 2), by { push_cast, ring }), rw riemann_zeta_one_sub, rotate, { intro n, rw [(by norm_cast : (2 * (m : ℂ) + 2) = ↑(2 * m + 2)), ←int.cast_neg_nat_cast, ←int.cast_coe_nat, ne.def, int.cast_inj], apply ne_of_gt, refine lt_of_le_of_lt (by norm_num : (-n : ℤ) ≤ 0) (by positivity) }, { rw [(by norm_cast : (2 * (m : ℂ) + 2) = ↑(2 * m + 2)), ne.def, nat.cast_eq_one], norm_num }, -- get rid of sine term rw show complex.sin (↑π * (1 - (2 * ↑m + 2)) / 2) = -(-1) ^ m, { rw (by { field_simp, ring } : (π : ℂ) * (1 - (2 * ↑m + 2)) / 2 = π / 2 - (π * m + π)), rw [complex.sin_pi_div_two_sub, complex.cos_add_pi, neg_inj], rcases nat.even_or_odd' m with ⟨t, rfl | rfl⟩, { rw [pow_mul, neg_one_sq, one_pow], convert complex.cos_nat_mul_two_pi t using 2, push_cast, ring }, { rw [pow_add, pow_one, pow_mul, neg_one_sq, one_pow, one_mul], convert complex.cos_nat_mul_two_pi_add_pi t using 2, push_cast, ring } }, -- substitute in what we know about zeta values at positive integers have step1 := congr_arg (coe : ℝ → ℂ) (has_sum_zeta_nat (by norm_num : m + 1 ≠ 0)).tsum_eq, have step2 := zeta_nat_eq_tsum_of_gt_one (by { rw mul_add, norm_num } : 1 < 2 * (m + 1)), simp_rw [of_real_tsum, of_real_div, of_real_one, of_real_pow, of_real_nat_cast] at step1, rw [step1, (by norm_cast : (↑(2 * (m + 1)) : ℂ) = 2 * ↑m + 2)] at step2, rw [step2, mul_div], -- now the rest is just a lengthy but elementary rearrangement rw show ((2 * (m + 1))! : ℂ) = Gamma (2 * m + 2) * (↑(2 * m + 1) + 1), by { rw [(by { push_cast, ring } : (2 * m + 2 : ℂ) = ↑(2 * m + 1) + 1), complex.Gamma_nat_eq_factorial, (by ring : 2 * (m + 1) = (2 * m + 1) + 1), nat.factorial_succ, nat.cast_mul, mul_comm], push_cast }, rw [←div_div, neg_one_mul], congr' 1, rw [div_eq_iff (Gamma_ne_zero_of_re_pos _)], swap, { rw [(by push_cast : 2 * (m : ℂ) + 2 = ↑(2 * (m : ℝ) + 2)), of_real_re], positivity }, simp_rw [of_real_mul, ←mul_assoc, of_real_rat_cast, mul_add, nat.add_assoc, mul_one, one_add_one_eq_two, mul_neg, neg_mul, neg_inj], conv_rhs { rw mul_comm }, congr' 1, rw [of_real_pow, of_real_neg, of_real_one, pow_add, neg_one_sq, mul_one], conv_lhs { congr, congr, rw [mul_assoc, ←pow_add, ←two_mul, pow_mul, neg_one_sq, one_pow, mul_one] }, rw show (2 : ℂ) ^ (1 - (2 * (m : ℂ) + 2)) = (↑((2 : ℝ) ^ ((2 * m + 2) - 1)))⁻¹, { rw [of_real_pow, ←cpow_nat_cast, ←cpow_neg, of_real_bit0, of_real_one], congr' 1, rw [nat.add_sub_assoc one_le_two, nat.cast_add, nat.cast_mul, nat.cast_two, (by norm_num : 2 - 1 = 1)], push_cast, ring }, rw show (π : ℂ) ^ -(2 * (m : ℂ) + 2) = (↑(π ^ (2 * m + 2)))⁻¹, { rw [of_real_pow, ←cpow_nat_cast, ←cpow_neg, nat.cast_add, nat.cast_mul, nat.cast_two] }, rw (by { intros, ring } : ∀ (a b c d e : ℂ), a * b * c * d * e = (a * d) * (b * e) * c), rw [inv_mul_cancel (of_real_ne_zero.mpr $ pow_ne_zero _ pi_pos.ne'), inv_mul_cancel (of_real_ne_zero.mpr $ pow_ne_zero _ two_ne_zero), one_mul, one_mul] } end
c2ac1e941c756917d944f5df8a8e7ad7550877d7
c3f2fcd060adfa2ca29f924839d2d925e8f2c685
/library/data/unit.lean
6c856819363b99cf71b2dade9cf60de2689e290b
[ "Apache-2.0" ]
permissive
respu/lean
6582d19a2f2838a28ecd2b3c6f81c32d07b5341d
8c76419c60b63d0d9f7bc04ebb0b99812d0ec654
refs/heads/master
1,610,882,451,231
1,427,747,084,000
1,427,747,429,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
743
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: data.unit Author: Leonardo de Moura -/ import logic.eq logic.subsingleton namespace unit notation `⋆` := star protected theorem equal (a b : unit) : a = b := unit.rec_on a (unit.rec_on b rfl) theorem eq_star (a : unit) : a = star := unit.equal a star protected theorem subsingleton [instance] : subsingleton unit := subsingleton.intro (λ a b, equal a b) protected definition is_inhabited [instance] : inhabited unit := inhabited.mk unit.star protected definition has_decidable_eq [instance] : decidable_eq unit := take (a b : unit), decidable.inl (equal a b) end unit
3f4ca3bf1de5be8ebfad529968bb84c10d9ea9f4
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/project_1_a_decrire/lean-scheme-submission/src/sheaves/sheaf_of_rings_on_standard_basis.lean
b3d583d2b705ea29b4382c9d699f677200dd5549
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
25,161
lean
/- Extension of a sheaf of rings on the basis to a sheaf of rings on the whole space. https://stacks.math.columbia.edu/tag/009M https://stacks.math.columbia.edu/tag/009N -/ import topology.opens import sheaves.stalk_of_rings import sheaves.stalk_of_rings_on_standard_basis import sheaves.presheaf_of_rings_on_basis import sheaves.presheaf_of_rings_extension import sheaves.sheaf_on_basis import sheaves.sheaf_on_standard_basis import sheaves.sheaf_of_rings open topological_space classical noncomputable theory universe u section presheaf_of_rings_extension variables {α : Type u} [T : topological_space α] variables {B : set (opens α)} {HB : opens.is_basis B} variables (Bstd : opens.univ ∈ B ∧ ∀ {U V}, U ∈ B → V ∈ B → U ∩ V ∈ B) include Bstd theorem extension_is_sheaf_of_rings (F : presheaf_of_rings_on_basis α HB) (HF : sheaf_on_standard_basis.is_sheaf_on_standard_basis Bstd F.to_presheaf_on_basis) : is_sheaf_of_rings (F ᵣₑₓₜ Bstd) := begin show is_sheaf (F ᵣₑₓₜ Bstd).to_presheaf, constructor, { intros U OC s t Hres, apply subtype.eq, apply funext, intros x, apply funext, intros HxU, rw OC.Hcov.symm at HxU, rcases HxU with ⟨Uj1, ⟨⟨⟨Uj2, OUj⟩, ⟨⟨j, HUj⟩, Heq⟩⟩, HxUj⟩⟩, rcases Heq, rcases Heq, have Hstj := congr_fun (subtype.mk_eq_mk.1 (Hres j)), have HxUj1 : x ∈ OC.Uis j := HUj.symm ▸ HxUj, have Hstjx := congr_fun (Hstj x) HxUj1, exact Hstjx, }, { intros U OC s Hsec, existsi (global_section (F.to_presheaf_on_basis) U OC s Hsec), intros i, apply subtype.eq, apply funext, intros x, apply funext, intros HxUi, have HxU : x ∈ U := OC.Hcov ▸ (opens_supr_subset OC.Uis i) HxUi, let HyUi := λ t, ∃ (H : t ∈ set.range OC.Uis), x ∈ t, dunfold presheaf_of_rings_extension; dsimp, dunfold global_section; dsimp, -- Same process of dealing with subtype.rec. let HyUi := λ t, ∃ (H : t ∈ subtype.val '' set.range OC.Uis), x ∈ t, rcases (classical.indefinite_description HyUi _) with ⟨S, HS⟩; dsimp, let HyS := λ H : S ∈ subtype.val '' set.range OC.Uis, x ∈ S, rcases (classical.indefinite_description HyS HS) with ⟨HSUiR, HySUiR⟩; dsimp, let HOUksub := λ t : subtype is_open, t ∈ set.range (OC.Uis) ∧ t.val = S, rcases (classical.indefinite_description HOUksub _) with ⟨OUl, ⟨HOUl, HOUleq⟩⟩; dsimp, let HSUi := λ i, OC.Uis i = OUl, cases (classical.indefinite_description HSUi _) with l HSUil; dsimp, -- Now we just need to apply Hsec in the right way. dunfold presheaf_of_rings_extension at Hsec, dunfold res_to_inter_left at Hsec, dunfold res_to_inter_right at Hsec, dsimp at Hsec, replace Hsec := Hsec i l, rw subtype.ext at Hsec, dsimp at Hsec, replace Hsec := congr_fun Hsec x, dsimp at Hsec, replace Hsec := congr_fun Hsec, have HxOUk : x ∈ OUl.val := HOUleq.symm ▸ HySUiR, have HxUl : x ∈ OC.Uis l := HSUil.symm ▸ HxOUk, exact (Hsec ⟨HxUi, HxUl⟩).symm, }, end section extension_coincides -- The extension is done in a way that F(U) ≅ Fext(U). -- The map ψ : F(U) → Π x ∈ U, Fx def to_stalk_product (F : presheaf_on_basis α HB) {U : opens α} (BU : U ∈ B) : F.F BU → Π (x ∈ U), stalk_on_basis F x := λ s x Hx, ⟦{U := U, BU := BU, Hx := Hx, s := s}⟧ lemma to_stalk_product.injective (F : presheaf_on_basis α HB) (HF : sheaf_on_standard_basis.is_sheaf_on_standard_basis Bstd F) {U : opens α} (BU : U ∈ B) : function.injective (to_stalk_product Bstd F BU) := begin intros s₁ s₂ Hs, have Hsx := λ (HxU : U), congr_fun (congr_fun Hs HxU.1) HxU.2, let OC : covering_standard_basis B U := { γ := U, Uis := λ HxU, some (quotient.eq.1 (Hsx HxU)), BUis := λ HxU, some (some_spec (quotient.eq.1 (Hsx HxU))), Hcov := begin ext z, split, { rintros ⟨Ui, ⟨⟨OUi, ⟨⟨i, HUi⟩, HUival⟩⟩, HzUi⟩⟩, rw [←HUival, ←HUi] at HzUi, exact some (some_spec (some_spec (some_spec (quotient.eq.1 (Hsx i))))) HzUi, }, { intros Hz, use [(some (quotient.eq.1 (Hsx ⟨z, Hz⟩))).val], have Hin : (some (quotient.eq.1 (Hsx ⟨z, Hz⟩))).val ∈ subtype.val '' set.range (λ (HxU : U), some ((quotient.eq.1 (Hsx HxU)))), use [classical.some ((quotient.eq.1 (Hsx ⟨z, Hz⟩)))], split, { use ⟨z, Hz⟩, }, { refl, }, use Hin, exact some (some_spec (some_spec (quotient.eq.1 (Hsx ⟨z, Hz⟩)))), }, end, }, apply (HF BU OC).1, intros i, replace Hs := congr_fun (congr_fun Hs i.1) i.2, exact some_spec (some_spec (some_spec (some_spec (some_spec (quotient.eq.1 (Hsx i)))))), end -- The map φ : F(U) → im(ψ). def to_presheaf_of_rings_extension (F : presheaf_of_rings_on_basis α HB) {U : opens α} (BU : U ∈ B) : F.F BU → (F ᵣₑₓₜ Bstd).F U := λ s, ⟨to_stalk_product Bstd F.to_presheaf_on_basis BU s, λ x Hx, ⟨U, BU, Hx, s, λ y Hy, funext $ λ Hy', quotient.sound $ ⟨U, BU, Hy', set.subset.refl U, set.subset.refl U, rfl⟩⟩⟩ lemma to_presheaf_of_rings_extension.injective (F : presheaf_of_rings_on_basis α HB) (HF : sheaf_on_standard_basis.is_sheaf_on_standard_basis Bstd F.to_presheaf_on_basis) {U : opens α} (BU : U ∈ B) : function.injective (to_presheaf_of_rings_extension Bstd F BU) := begin intros s₁ s₂ Hs, erw subtype.mk_eq_mk at Hs, have Hinj := to_stalk_product.injective Bstd F.to_presheaf_on_basis (λ V BV OC, HF BV OC) BU, exact Hinj Hs, end lemma to_presheaf_of_rings_extension.surjective (F : presheaf_of_rings_on_basis α HB) (HF : sheaf_on_standard_basis.is_sheaf_on_standard_basis Bstd F.to_presheaf_on_basis) {U : opens α} (BU : U ∈ B) : function.surjective (to_presheaf_of_rings_extension Bstd F BU) := begin intros s, let V := λ (HxU : U), some (s.2 HxU.1 HxU.2), let BV := λ (HxU : U), some (some_spec (s.2 HxU.1 HxU.2)), let HxV := λ (HxU : U), some (some_spec (some_spec (s.2 HxU.1 HxU.2))), let σ := λ (HxU : U), some (some_spec (some_spec (some_spec (s.2 HxU.1 HxU.2)))), let Hσ := λ (HxU : U), some_spec (some_spec (some_spec (some_spec (s.2 HxU.1 HxU.2)))), let OC : covering_standard_basis B U := { γ := U, Uis := λ HxU, (V HxU) ∩ U, BUis := λ HxU, Bstd.2 (BV HxU) BU, Hcov := begin ext z, split, { rintros ⟨Ui, ⟨⟨OUi, ⟨⟨i, HUi⟩, HUival⟩⟩, HzUi⟩⟩, rw [←HUival, ←HUi] at HzUi, exact HzUi.2, }, { intros Hz, use [(some (s.2 z Hz) ∩ U).val], have Hin : (some (s.2 z Hz) ∩ U).val ∈ subtype.val '' set.range (λ (HxU : U), ((some (s.2 HxU.1 HxU.2) ∩ U : opens α))), use [(some (s.2 z Hz) ∩ U : opens α)], split, { use ⟨z, Hz⟩, }, { refl, }, use Hin, exact ⟨some (some_spec (some_spec (s.2 z Hz))), Hz⟩, }, end, }, -- Now let's try to apply sheaf condition. let res := λ (HxU : OC.γ), F.res (BV HxU) (Bstd.2 (BV HxU) BU) (set.inter_subset_left _ _), let sx := λ (HxU : OC.γ), res HxU (σ HxU), have Hglue := (HF BU OC).2 sx, have Hsx : ∀ j k, sheaf_on_standard_basis.res_to_inter_left Bstd F.to_presheaf_on_basis (OC.BUis j) (OC.BUis k) (sx j) = sheaf_on_standard_basis.res_to_inter_right Bstd F.to_presheaf_on_basis (OC.BUis j) (OC.BUis k) (sx k), intros j k, dsimp only [sheaf_on_standard_basis.res_to_inter_left], dsimp only [sheaf_on_standard_basis.res_to_inter_right], dsimp only [sx, res], iterate 2 { rw ←presheaf_on_basis.Hcomp', }, show (F.to_presheaf_on_basis).res (BV j) (Bstd.2 (OC.BUis j) (OC.BUis k)) _ (σ j) = (F.to_presheaf_on_basis).res (BV k) (Bstd.2 (OC.BUis j) (OC.BUis k)) _ (σ k), -- We can cover the U ∩ Vj ∩ Vk and use locality. -- But first let's check that all the stalks coincide in the intersectons. have Hstalks : ∀ {y} (Hy : y ∈ (OC.Uis j) ∩ (OC.Uis k)), (⟦{U := V j, BU := BV j, Hx := Hy.1.1, s := σ j}⟧ : stalk_on_basis F.to_presheaf_on_basis y) = ⟦{U := V k, BU := BV k, Hx := Hy.2.1, s := σ k}⟧, intros y Hy, have Hj := congr_fun (Hσ j y ⟨Hy.1.2, Hy.1.1⟩) Hy.1.2; dsimp at Hj, have Hk := congr_fun (Hσ k y ⟨Hy.2.2, Hy.2.1⟩) Hy.2.2; dsimp at Hk, erw [←Hj, ←Hk], -- Therefore there exists Wjk where σj|Wjk = σk|Wjk. We will use these as a cover. let Ujk : opens α := (OC.Uis j) ∩ (OC.Uis k), let BUjk := Bstd.2 (OC.BUis j) (OC.BUis k), -- Again, all the information we will need but on Uj ∩ Uk. let Hjk := λ (HxUjk : Ujk), quotient.eq.1 (Hstalks HxUjk.2), let Wjk := λ (HxUjk : Ujk), some (Hjk HxUjk) ∩ U, let BWjk := λ (HxUjk : Ujk), Bstd.2 (some (some_spec (Hjk HxUjk))) BU, let HxWjk := λ (HxUjk : Ujk), some (some_spec (some_spec (Hjk HxUjk))), let HWjkUj := λ (HxUjk : Ujk), some (some_spec (some_spec (some_spec (Hjk HxUjk)))), let HWjkUk := λ (HxUjk : Ujk), some (some_spec (some_spec (some_spec (some_spec (Hjk HxUjk))))), let HWjk := λ (HxUjk : Ujk), some_spec (some_spec (some_spec (some_spec (some_spec (Hjk HxUjk))))), let OCjk : covering_standard_basis B ((OC.Uis j) ∩ (OC.Uis k)) := { γ := Ujk, Uis := Wjk, BUis := BWjk, Hcov := begin ext z, split, { rintros ⟨W, ⟨⟨OW, ⟨⟨i, HWi⟩, HWival⟩⟩, HzW⟩⟩, rw [←HWival, ←HWi] at HzW, have HzUj := (HWjkUj i) HzW.1, have HzUk := (HWjkUk i) HzW.1, exact ⟨⟨HzUj, HzW.2⟩, ⟨HzUk, HzW.2⟩⟩, }, { intros Hz, use [(some (Hjk ⟨z, Hz⟩) ∩ U).val], have Hin : (some (Hjk ⟨z, Hz⟩) ∩ U).val ∈ subtype.val '' set.range (λ (HxUjk : Ujk), ((some (Hjk HxUjk) ∩ U : opens α))), use [(some (Hjk ⟨z, Hz⟩) ∩ U : opens α)], split, { use ⟨z, Hz⟩, }, { refl, }, use Hin, have HzWjk := HxWjk ⟨z, Hz⟩, have HzU := Hz.1.2, exact ⟨HzWjk, HzU⟩, } end, }, apply (HF BUjk OCjk).1, intros i, rw ←presheaf_on_basis.Hcomp', rw ←presheaf_on_basis.Hcomp', have Hres : F.res (some (some_spec (Hjk i))) (BWjk i) (set.inter_subset_left _ _) (F.res (BV j) (some (some_spec (Hjk i))) (HWjkUj i) (σ j)) = F.res (some (some_spec (Hjk i))) (BWjk i) (set.inter_subset_left _ _) (F.res (BV k) (some (some_spec (Hjk i))) (HWjkUk i) (σ k)), rw (HWjk i), rw ←presheaf_on_basis.Hcomp' at Hres, rw ←presheaf_on_basis.Hcomp' at Hres, use Hres, -- Ready to prove it. rcases (Hglue Hsx) with ⟨S, HS⟩, existsi S, apply subtype.eq, dsimp [to_presheaf_of_rings_extension], apply funext, intros x, dsimp [to_stalk_product], apply funext, intros Hx, replace HS := HS ⟨x, Hx⟩, dsimp [sx, res] at HS, rw Hσ ⟨x, Hx⟩, swap, { exact ⟨Hx, HxV ⟨x, Hx⟩⟩, }, dsimp, apply quotient.sound, use [(V ⟨x, Hx⟩) ∩ U], use [Bstd.2 (BV ⟨x, Hx⟩) BU], use [⟨HxV ⟨x, Hx⟩, Hx⟩], use [set.inter_subset_right _ _], use [set.inter_subset_left _ _], dsimp, erw HS, end lemma to_presheaf_of_rings_extension.bijective (F : presheaf_of_rings_on_basis α HB) (HF : sheaf_on_standard_basis.is_sheaf_on_standard_basis Bstd F.to_presheaf_on_basis) {U : opens α} (BU : U ∈ B) : function.bijective (to_presheaf_of_rings_extension Bstd F BU) := ⟨to_presheaf_of_rings_extension.injective Bstd F (λ U BU OC, HF BU OC) BU, to_presheaf_of_rings_extension.surjective Bstd F (λ U BU OC, HF BU OC) BU ⟩ -- We now that they are equivalent as sets. -- Now we to assert that they're isomorphic as rings. -- It suffices to show that it is a ring homomorphism. lemma to_presheaf_of_rings_extension.is_ring_hom (F : presheaf_of_rings_on_basis α HB) {U : opens α} (BU : U ∈ B) : is_ring_hom (to_presheaf_of_rings_extension Bstd F BU) := { map_one := begin apply subtype.eq, funext x Hx, apply quotient.sound, use [U, BU, Hx, set.subset.refl _, set.subset_univ _], iterate 2 { erw (F.res_is_ring_hom _ _ _).map_one, }, end, map_mul := begin intros x y, apply subtype.eq, funext z Hz, apply quotient.sound, use [U, BU, Hz], use [set.subset.refl _, set.subset_inter (set.subset.refl _) (set.subset.refl _)], erw ←(F.res_is_ring_hom _ _ _).map_mul, erw ←presheaf_on_basis.Hcomp', end, map_add := begin intros x y, apply subtype.eq, funext z Hz, apply quotient.sound, use [U, BU, Hz], use [set.subset.refl _, set.subset_inter (set.subset.refl _) (set.subset.refl _)], erw ←(F.res_is_ring_hom _ _ _).map_add, erw ←presheaf_on_basis.Hcomp', end, } lemma to_presheaf_of_rings_extension.ring_equiv (F : presheaf_of_rings_on_basis α HB) (HF : sheaf_on_standard_basis.is_sheaf_on_standard_basis Bstd F.to_presheaf_on_basis) {U : opens α} (BU : U ∈ B) : F BU ≃+* (presheaf_of_rings_extension Bstd F) U := begin have H := function.bijective_iff_has_inverse.1 (to_presheaf_of_rings_extension.bijective Bstd F @HF BU), rcases (classical.indefinite_description _ H) with ⟨inv, Hlinv, Hrinv⟩, use [(to_presheaf_of_rings_extension Bstd F BU), inv, Hlinv, Hrinv], { apply (to_presheaf_of_rings_extension.is_ring_hom Bstd F BU).map_mul, }, { apply (to_presheaf_of_rings_extension.is_ring_hom Bstd F BU).map_add, } end -- Moreover, for all x, Fₓ ≅ Fextₓ. -- We will need this to show that the stalks of the structure sheaf on -- Spec(R) are local rings. open stalk_of_rings_on_standard_basis lemma to_stalk_extension (F : presheaf_of_rings_on_basis α HB) (x : α) : stalk_of_rings_on_standard_basis Bstd F x → stalk_of_rings (F ᵣₑₓₜ Bstd) x := begin intros BUs, let Us := quotient.out BUs, exact ⟦{U := Us.U, HxU := Us.Hx, s := (to_presheaf_of_rings_extension Bstd F Us.BU) Us.s}⟧, end lemma to_stalk_extension.injective (F : presheaf_of_rings_on_basis α HB) (x : α) : function.injective (to_stalk_extension Bstd F x) := begin intros Us₁' Us₂', apply quotient.induction_on₂ Us₁' Us₂', rintros Us₁ Us₂ HUs, rcases (quotient.mk_out Us₁) with ⟨W₁, BW₁, HxW₁, HW₁Us₁, HW₁U₁, Hres₁⟩, rcases (quotient.mk_out Us₂) with ⟨W₂, BW₂, HxW₂, HW₂Us₂, HW₂U₂, Hres₂⟩, dunfold to_stalk_extension at HUs, rw quotient.eq at HUs, rcases HUs with ⟨W, HxW, HWU₁, HWU₂, Hres⟩, dsimp at HWU₁, dsimp at HWU₂, dunfold to_presheaf_of_rings_extension at Hres, dunfold to_stalk_product at Hres, erw subtype.mk.inj_eq at Hres, replace Hres := congr_fun (congr_fun Hres x) HxW, dsimp at Hres, rw quotient.eq at Hres, rcases Hres with ⟨W₃, BW₃, HxW₃, HW₃U₁, HW₃U₂, Hres₃⟩, dsimp at HW₃U₁, dsimp at HW₃U₂, dsimp at Hres₃, apply quotient.sound, have BW₁₂₃ : W₁ ∩ W₂ ∩ W₃ ∈ B := Bstd.2 (Bstd.2 BW₁ BW₂) BW₃, have HW₁₂₃U₁ : W₁ ∩ W₂ ∩ W₃ ⊆ Us₁.U := λ x Hx, HW₁U₁ Hx.1.1, have HW₁₂₃U₂ : W₁ ∩ W₂ ∩ W₃ ⊆ Us₂.U := λ x Hx, HW₂U₂ Hx.1.2, use [W₁ ∩ W₂ ∩ W₃, BW₁₂₃, ⟨⟨HxW₁, HxW₂⟩, HxW₃⟩, HW₁₂₃U₁, HW₁₂₃U₂], have HW₁W₁₂₃ : W₁ ∩ W₂ ∩ W₃ ⊆ W₁ := λ x Hx, Hx.1.1, have HW₂W₁₂₃ : W₁ ∩ W₂ ∩ W₃ ⊆ W₂ := λ x Hx, Hx.1.2, have HW₃W₁₂₃ : W₁ ∩ W₂ ∩ W₃ ⊆ W₃ := λ x Hx, Hx.2, replace Hres₁ := congr_arg (F.res BW₁ BW₁₂₃ HW₁W₁₂₃) Hres₁, replace Hres₂ := congr_arg (F.res BW₂ BW₁₂₃ HW₂W₁₂₃) Hres₂, replace Hres₃ := congr_arg (F.res BW₃ BW₁₂₃ HW₃W₁₂₃) Hres₃, iterate 2 { rw ←presheaf_on_basis.Hcomp' at Hres₁, }, iterate 2 { rw ←presheaf_on_basis.Hcomp' at Hres₂, }, iterate 2 { rw ←presheaf_on_basis.Hcomp' at Hres₃, }, erw [←Hres₁, ←Hres₂], exact Hres₃, end lemma to_stalk_extension.surjective (F : presheaf_of_rings_on_basis α HB) (x : α) : function.surjective (to_stalk_extension Bstd F x) := begin intros Us', apply quotient.induction_on Us', rintros ⟨U, HxU, s⟩, rcases (s.2 x HxU) with ⟨V, BV, HxV, t, Ht⟩, let Vt : stalk_on_basis.elem (F.to_presheaf_on_basis) x := {U := V, BU := BV, Hx := HxV, s := t}, use ⟦Vt⟧, dunfold to_stalk_extension, apply quotient.sound, rcases (quotient.mk_out Vt) with ⟨W, BW, HxW, HWVtV, HWV, Hres⟩, have HUVWV : U ∩ V ∩ W ⊆ (quotient.out ⟦Vt⟧).U := λ x Hx, HWVtV Hx.2, have HUVWU : U ∩ V ∩ W ⊆ U := λ x Hx, Hx.1.1, use [U ∩ V ∩ W, ⟨⟨HxU, HxV⟩, HxW⟩, HUVWV, HUVWU], apply subtype.eq, dsimp only [presheaf_of_rings_extension], dsimp only [to_presheaf_of_rings_extension], dsimp only [to_stalk_product], funext y Hy, rw (Ht y Hy.1), apply quotient.sound, have BVW : V ∩ W ∈ B := Bstd.2 BV BW, have HVWVtV : V ∩ W ⊆ (quotient.out ⟦Vt⟧).U := λ x Hx, HWVtV Hx.2, have HVWV : V ∩ W ⊆ V := λ x Hx, Hx.1, use [V ∩ W, BVW, ⟨Hy.1.2,Hy.2⟩, HVWVtV, HVWV], have HVWW : V ∩ W ⊆ W := λ x Hx, Hx.2, replace Hres := congr_arg (F.res BW BVW HVWW) Hres, iterate 2 { rw ←presheaf_on_basis.Hcomp' at Hres, }, exact Hres, end lemma to_stalk_extension.bijective (F : presheaf_of_rings_on_basis α HB) (x : α) : function.bijective (to_stalk_extension Bstd F x) := ⟨to_stalk_extension.injective Bstd F x, to_stalk_extension.surjective Bstd F x⟩ lemma to_stalk_extension.is_ring_hom (F : presheaf_of_rings_on_basis α HB) (x : α) : is_ring_hom (to_stalk_extension Bstd F x) := { map_one := begin dunfold to_stalk_extension, let one.elem : stalk_on_basis.elem F.to_presheaf_on_basis x := {U := opens.univ, BU := Bstd.1, Hx := trivial, s:= 1}, let one.stalk : stalk_of_rings_on_standard_basis Bstd F x := ⟦one.elem⟧, let one := quotient.out one.stalk, apply quotient.sound, rcases (quotient.mk_out one.elem) with ⟨W₁, BW₁, HxW₁, HW₁Uout, HW₁U, Hres₁⟩, have BUW₁ : one.U ∩ W₁ ∈ B := Bstd.2 one.BU BW₁, have HUUW₁ : one.U ∩ W₁ ⊆ one.U := set.inter_subset_left _ _, use [one.U ∩ W₁, ⟨one.Hx, HxW₁⟩, HUUW₁, set.subset_univ _], apply subtype.eq, dsimp only [presheaf_of_rings_extension], dsimp only [to_presheaf_of_rings_extension], dsimp only [to_stalk_product], funext z Hz, apply quotient.sound, use [one.U ∩ W₁, BUW₁, Hz, set.inter_subset_left _ _, set.subset_univ _], dsimp, have HUW₁W₁ : one.U ∩ W₁ ⊆ W₁ := set.inter_subset_right _ _, replace Hres₁ := congr_arg (F.res BW₁ BUW₁ HUW₁W₁) Hres₁, iterate 2 { rw ←presheaf_on_basis.Hcomp' at Hres₁, }, exact Hres₁, end, map_mul := begin intros y z, apply quotient.induction_on₂ y z, intros Us₁ Us₂, simp, let Us₃ : stalk_on_basis.elem F.to_presheaf_on_basis x := { U := Us₁.U ∩ Us₂.U, BU := Bstd.2 Us₁.BU Us₂.BU, Hx := ⟨Us₁.Hx, Us₂.Hx⟩, s := F.res Us₁.BU _ (set.inter_subset_left _ _) Us₁.s * F.res Us₂.BU _ (set.inter_subset_right _ _) Us₂.s }, dunfold to_stalk_extension, apply quotient.sound, rcases (quotient.mk_out Us₁) with ⟨W₁, BW₁, HxW₁, HW₁U₁out, HW₁U₁, Hres₁⟩, rcases (quotient.mk_out Us₂) with ⟨W₂, BW₂, HxW₂, HW₂U₂out, HW₂U₂, Hres₂⟩, rcases (quotient.mk_out Us₃) with ⟨W₃, BW₃, HxW₃, HW₃U₃out, HW₃U₃, Hres₃⟩, let W := W₁ ∩ W₂ ∩ W₃, have HxW : x ∈ W := ⟨⟨HxW₁, HxW₂⟩, HxW₃⟩, have HWW₁ : W ⊆ W₁ := λ x Hx, Hx.1.1, have HWW₂ : W ⊆ W₂ := λ x Hx, Hx.1.2, have HWW₃ : W ⊆ W₃ := λ x Hx, Hx.2, have HWU₁out : W ⊆ (quotient.out ⟦Us₁⟧).U := set.subset.trans HWW₁ HW₁U₁out, have HWU₂out : W ⊆ (quotient.out ⟦Us₂⟧).U := set.subset.trans HWW₂ HW₂U₂out, have HWU₃out : W ⊆ (quotient.out ⟦Us₃⟧).U := set.subset.trans HWW₃ HW₃U₃out, have HWU₁₂out : W ⊆ (quotient.out ⟦Us₁⟧).U ∩ (quotient.out ⟦Us₂⟧).U := set.subset_inter HWU₁out HWU₂out, use [W, HxW, HWU₃out, HWU₁₂out], apply subtype.eq, dsimp only [presheaf_of_rings_extension], dsimp only [to_presheaf_of_rings_extension], dsimp only [to_stalk_product], funext z HzW, apply quotient.sound, have BW : W ∈ B := Bstd.2 (Bstd.2 BW₁ BW₂) BW₃, use [W, BW, HzW, HWU₃out, HWU₁₂out], rw (presheaf_of_rings_on_basis.res_is_ring_hom _ _ _ _).map_mul, rw ←presheaf_on_basis.Hcomp', rw ←presheaf_on_basis.Hcomp', replace Hres₁ := congr_arg (F.res BW₁ BW HWW₁) Hres₁, replace Hres₂ := congr_arg (F.res BW₂ BW HWW₂) Hres₂, replace Hres₃ := congr_arg (F.res BW₃ BW HWW₃) Hres₃, iterate 2 { rw ←presheaf_on_basis.Hcomp' at Hres₁, }, iterate 2 { rw ←presheaf_on_basis.Hcomp' at Hres₂, }, iterate 2 { rw ←presheaf_on_basis.Hcomp' at Hres₃, }, erw [Hres₁, Hres₂, Hres₃], rw (presheaf_of_rings_on_basis.res_is_ring_hom _ _ _ _).map_mul, rw ←presheaf_on_basis.Hcomp', rw ←presheaf_on_basis.Hcomp', end, map_add := begin intros y z, apply quotient.induction_on₂ y z, intros Us₁ Us₂, simp, let Us₃ : stalk_on_basis.elem F.to_presheaf_on_basis x := { U := Us₁.U ∩ Us₂.U, BU := Bstd.2 Us₁.BU Us₂.BU, Hx := ⟨Us₁.Hx, Us₂.Hx⟩, s := F.res Us₁.BU _ (set.inter_subset_left _ _) Us₁.s + F.res Us₂.BU _ (set.inter_subset_right _ _) Us₂.s }, dunfold to_stalk_extension, apply quotient.sound, rcases (quotient.mk_out Us₁) with ⟨W₁, BW₁, HxW₁, HW₁U₁out, HW₁U₁, Hres₁⟩, rcases (quotient.mk_out Us₂) with ⟨W₂, BW₂, HxW₂, HW₂U₂out, HW₂U₂, Hres₂⟩, rcases (quotient.mk_out Us₃) with ⟨W₃, BW₃, HxW₃, HW₃U₃out, HW₃U₃, Hres₃⟩, let W := W₁ ∩ W₂ ∩ W₃, have HxW : x ∈ W := ⟨⟨HxW₁, HxW₂⟩, HxW₃⟩, have HWW₁ : W ⊆ W₁ := λ x Hx, Hx.1.1, have HWW₂ : W ⊆ W₂ := λ x Hx, Hx.1.2, have HWW₃ : W ⊆ W₃ := λ x Hx, Hx.2, have HWU₁out : W ⊆ (quotient.out ⟦Us₁⟧).U := set.subset.trans HWW₁ HW₁U₁out, have HWU₂out : W ⊆ (quotient.out ⟦Us₂⟧).U := set.subset.trans HWW₂ HW₂U₂out, have HWU₃out : W ⊆ (quotient.out ⟦Us₃⟧).U := set.subset.trans HWW₃ HW₃U₃out, have HWU₁₂out : W ⊆ (quotient.out ⟦Us₁⟧).U ∩ (quotient.out ⟦Us₂⟧).U := set.subset_inter HWU₁out HWU₂out, use [W, HxW, HWU₃out, HWU₁₂out], apply subtype.eq, dsimp only [presheaf_of_rings_extension], dsimp only [to_presheaf_of_rings_extension], dsimp only [to_stalk_product], funext z HzW, apply quotient.sound, have BW : W ∈ B := Bstd.2 (Bstd.2 BW₁ BW₂) BW₃, use [W, BW, HzW, HWU₃out, HWU₁₂out], rw (presheaf_of_rings_on_basis.res_is_ring_hom _ _ _ _).map_add, rw ←presheaf_on_basis.Hcomp', rw ←presheaf_on_basis.Hcomp', replace Hres₁ := congr_arg (F.res BW₁ BW HWW₁) Hres₁, replace Hres₂ := congr_arg (F.res BW₂ BW HWW₂) Hres₂, replace Hres₃ := congr_arg (F.res BW₃ BW HWW₃) Hres₃, iterate 2 { rw ←presheaf_on_basis.Hcomp' at Hres₁, }, iterate 2 { rw ←presheaf_on_basis.Hcomp' at Hres₂, }, iterate 2 { rw ←presheaf_on_basis.Hcomp' at Hres₃, }, erw [Hres₁, Hres₂, Hres₃], rw (presheaf_of_rings_on_basis.res_is_ring_hom _ _ _ _).map_add, rw ←presheaf_on_basis.Hcomp', rw ←presheaf_on_basis.Hcomp', end, } lemma to_stalk_extension.ring_equiv (F : presheaf_of_rings_on_basis α HB) (x : α) : stalk_of_rings_on_standard_basis Bstd F x ≃+* stalk_of_rings (presheaf_of_rings_extension Bstd F) x := begin have H := function.bijective_iff_has_inverse.1 (to_stalk_extension.bijective Bstd F x), rcases (classical.indefinite_description _ H) with ⟨inv, Hlinv, Hrinv⟩, use [(to_stalk_extension Bstd F x), inv, Hlinv, Hrinv], { apply (to_stalk_extension.is_ring_hom Bstd F x).map_mul, }, { apply (to_stalk_extension.is_ring_hom Bstd F x).map_add, } end end extension_coincides end presheaf_of_rings_extension
c09a0c12f51fb9fd62db722747405de3389d07ab
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/data/subtype.lean
3686487744f34becdbcab3b0fff544d36f894626
[ "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
5,999
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import tactic.lint import tactic.ext import tactic.simps open function namespace subtype variables {α : Sort*} {β : Sort*} {γ : Sort*} {p : α → Prop} {q : α → Prop} /-- See Note [custom simps projection] -/ def simps.val (x : subtype p) : α := x initialize_simps_projections subtype (val → coe) /-- A version of `x.property` or `x.2` where `p` is syntactically applied to the coercion of `x` instead of `x.1`. A similar result is `subtype.mem` in `data.set.basic`. -/ lemma prop (x : subtype p) : p x := x.2 @[simp] lemma val_eq_coe {x : subtype p} : x.1 = ↑x := rfl @[simp] protected theorem «forall» {q : {a // p a} → Prop} : (∀ x, q x) ↔ (∀ a b, q ⟨a, b⟩) := ⟨assume h a b, h ⟨a, b⟩, assume h ⟨a, b⟩, h a b⟩ /-- An alternative version of `subtype.forall`. This one is useful if Lean cannot figure out `q` when using `subtype.forall` from right to left. -/ protected theorem forall' {q : ∀x, p x → Prop} : (∀ x h, q x h) ↔ (∀ x : {a // p a}, q x x.2) := (@subtype.forall _ _ (λ x, q x.1 x.2)).symm @[simp] protected theorem «exists» {q : {a // p a} → Prop} : (∃ x, q x) ↔ (∃ a b, q ⟨a, b⟩) := ⟨assume ⟨⟨a, b⟩, h⟩, ⟨a, b, h⟩, assume ⟨a, b, h⟩, ⟨⟨a, b⟩, h⟩⟩ @[ext] protected lemma ext : ∀ {a1 a2 : {x // p x}}, (a1 : α) = (a2 : α) → a1 = a2 | ⟨x, h1⟩ ⟨.(x), h2⟩ rfl := rfl lemma ext_iff {a1 a2 : {x // p x}} : a1 = a2 ↔ (a1 : α) = (a2 : α) := ⟨congr_arg _, subtype.ext⟩ lemma heq_iff_coe_eq (h : ∀ x, p x ↔ q x) {a1 : {x // p x}} {a2 : {x // q x}} : a1 == a2 ↔ (a1 : α) = (a2 : α) := eq.rec (λ a2', heq_iff_eq.trans ext_iff) (funext $ λ x, propext (h x)) a2 lemma ext_val {a1 a2 : {x // p x}} : a1.1 = a2.1 → a1 = a2 := subtype.ext lemma ext_iff_val {a1 a2 : {x // p x}} : a1 = a2 ↔ a1.1 = a2.1 := ext_iff @[simp] theorem coe_eta (a : {a // p a}) (h : p a) : mk ↑a h = a := subtype.ext rfl @[simp] theorem coe_mk (a h) : (@mk α p a h : α) = a := rfl @[simp, nolint simp_nf] -- built-in reduction doesn't always work theorem mk_eq_mk {a h a' h'} : @mk α p a h = @mk α p a' h' ↔ a = a' := ext_iff theorem coe_eq_iff {a : {a // p a}} {b : α} : ↑a = b ↔ ∃ h, a = ⟨b, h⟩ := ⟨λ h, h ▸ ⟨a.2, (coe_eta _ _).symm⟩, λ ⟨hb, ha⟩, ha.symm ▸ rfl⟩ theorem coe_injective : injective (coe : subtype p → α) := λ a b, subtype.ext theorem val_injective : injective (@val _ p) := coe_injective /-- Restrict a (dependent) function to a subtype -/ def restrict {α} {β : α → Type*} (f : Πx, β x) (p : α → Prop) (x : subtype p) : β x.1 := f x lemma restrict_apply {α} {β : α → Type*} (f : Πx, β x) (p : α → Prop) (x : subtype p) : restrict f p x = f x.1 := by refl lemma restrict_def {α β} (f : α → β) (p : α → Prop) : restrict f p = f ∘ coe := by refl lemma restrict_injective {α β} {f : α → β} (p : α → Prop) (h : injective f) : injective (restrict f p) := h.comp coe_injective /-- Defining a map into a subtype, this can be seen as an "coinduction principle" of `subtype`-/ @[simps] def coind {α β} (f : α → β) {p : β → Prop} (h : ∀a, p (f a)) : α → subtype p := λ a, ⟨f a, h a⟩ theorem coind_injective {α β} {f : α → β} {p : β → Prop} (h : ∀a, p (f a)) (hf : injective f) : injective (coind f h) := λ x y hxy, hf $ by apply congr_arg subtype.val hxy theorem coind_surjective {α β} {f : α → β} {p : β → Prop} (h : ∀a, p (f a)) (hf : surjective f) : surjective (coind f h) := λ x, let ⟨a, ha⟩ := hf x in ⟨a, coe_injective ha⟩ theorem coind_bijective {α β} {f : α → β} {p : β → Prop} (h : ∀a, p (f a)) (hf : bijective f) : bijective (coind f h) := ⟨coind_injective h hf.1, coind_surjective h hf.2⟩ /-- Restriction of a function to a function on subtypes. -/ @[simps] def map {p : α → Prop} {q : β → Prop} (f : α → β) (h : ∀a, p a → q (f a)) : subtype p → subtype q := λ x, ⟨f x, h x x.prop⟩ theorem map_comp {p : α → Prop} {q : β → Prop} {r : γ → Prop} {x : subtype p} (f : α → β) (h : ∀a, p a → q (f a)) (g : β → γ) (l : ∀a, q a → r (g a)) : map g l (map f h x) = map (g ∘ f) (assume a ha, l (f a) $ h a ha) x := rfl theorem map_id {p : α → Prop} {h : ∀a, p a → p (id a)} : map (@id α) h = id := funext $ assume ⟨v, h⟩, rfl lemma map_injective {p : α → Prop} {q : β → Prop} {f : α → β} (h : ∀a, p a → q (f a)) (hf : injective f) : injective (map f h) := coind_injective _ $ hf.comp coe_injective lemma map_involutive {p : α → Prop} {f : α → α} (h : ∀a, p a → p (f a)) (hf : involutive f) : involutive (map f h) := λ x, subtype.ext (hf x) instance [has_equiv α] (p : α → Prop) : has_equiv (subtype p) := ⟨λ s t, (s : α) ≈ (t : α)⟩ theorem equiv_iff [has_equiv α] {p : α → Prop} {s t : subtype p} : s ≈ t ↔ (s : α) ≈ (t : α) := iff.rfl variables [setoid α] protected theorem refl (s : subtype p) : s ≈ s := setoid.refl ↑s protected theorem symm {s t : subtype p} (h : s ≈ t) : t ≈ s := setoid.symm h protected theorem trans {s t u : subtype p} (h₁ : s ≈ t) (h₂ : t ≈ u) : s ≈ u := setoid.trans h₁ h₂ theorem equivalence (p : α → Prop) : equivalence (@has_equiv.equiv (subtype p) _) := mk_equivalence _ subtype.refl (@subtype.symm _ p _) (@subtype.trans _ p _) instance (p : α → Prop) : setoid (subtype p) := setoid.mk (≈) (equivalence p) end subtype namespace subtype /-! Some facts about sets, which require that `α` is a type. -/ variables {α : Type*} {β : Type*} {γ : Type*} {p : α → Prop} @[simp] lemma coe_prop {S : set α} (a : {a // a ∈ S}) : ↑a ∈ S := a.prop lemma val_prop {S : set α} (a : {a // a ∈ S}) : a.val ∈ S := a.property end subtype
e1fc8c8795bea3f19411b6594d75cdff0293e6c9
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/sequences.lean
d82cd1eee584e006757814db6aaf632daf5b13db
[ "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
19,547
lean
/- Copyright (c) 2018 Jan-David Salchow. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jan-David Salchow, Patrick Massot, Yury Kudryashov -/ import topology.subset_properties import topology.metric_space.basic /-! # Sequences in topological spaces > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define sequences in topological spaces and show how they are related to filters and the topology. ## Main definitions ### Set operation * `seq_closure s`: sequential closure of a set, the set of limits of sequences of points of `s`; ### Predicates * `is_seq_closed s`: predicate saying that a set is sequentially closed, i.e., `seq_closure s ⊆ s`; * `seq_continuous f`: predicate saying that a function is sequentially continuous, i.e., for any sequence `u : ℕ → X` that converges to a point `x`, the sequence `f ∘ u` converges to `f x`; * `is_seq_compact s`: predicate saying that a set is sequentially compact, i.e., every sequence taking values in `s` has a converging subsequence. ### Type classes * `frechet_urysohn_space X`: a typeclass saying that a topological space is a *Fréchet-Urysohn space*, i.e., the sequential closure of any set is equal to its closure. * `sequential_space X`: a typeclass saying that a topological space is a *sequential space*, i.e., any sequentially closed set in this space is closed. This condition is weaker than being a Fréchet-Urysohn space. * `seq_compact_space X`: a typeclass saying that a topological space is sequentially compact, i.e., every sequence in `X` has a converging subsequence. ## Main results * `seq_closure_subset_closure`: closure of a set includes its sequential closure; * `is_closed.is_seq_closed`: a closed set is sequentially closed; * `is_seq_closed.seq_closure_eq`: sequential closure of a sequentially closed set `s` is equal to `s`; * `seq_closure_eq_closure`: in a Fréchet-Urysohn space, the sequential closure of a set is equal to its closure; * `tendsto_nhds_iff_seq_tendsto`, `frechet_urysohn_space.of_seq_tendsto_imp_tendsto`: a topological space is a Fréchet-Urysohn space if and only if sequential convergence implies convergence; * `topological_space.first_countable_topology.frechet_urysohn_space`: every topological space with first countable topology is a Fréchet-Urysohn space; * `frechet_urysohn_space.to_sequential_space`: every Fréchet-Urysohn space is a sequential space; * `is_seq_compact.is_compact`: a sequentially compact set in a uniform space with countably generated uniformity is compact. ## Tags sequentially closed, sequentially compact, sequential space -/ open set function filter topological_space open_locale topology filter variables {X Y : Type*} /-! ### Sequential closures, sequential continuity, and sequential spaces. -/ section topological_space variables [topological_space X] [topological_space Y] /-- The sequential closure of a set `s : set X` in a topological space `X` is the set of all `a : X` which arise as limit of sequences in `s`. Note that the sequential closure of a set is not guaranteed to be sequentially closed. -/ def seq_closure (s : set X) : set X := {a | ∃ x : ℕ → X, (∀ n : ℕ, x n ∈ s) ∧ tendsto x at_top (𝓝 a)} lemma subset_seq_closure {s : set X} : s ⊆ seq_closure s := λ p hp, ⟨const ℕ p, λ _, hp, tendsto_const_nhds⟩ /-- The sequential closure of a set is contained in the closure of that set. The converse is not true. -/ lemma seq_closure_subset_closure {s : set X} : seq_closure s ⊆ closure s := λ p ⟨x, xM, xp⟩, mem_closure_of_tendsto xp (univ_mem' xM) /-- A set `s` is sequentially closed if for any converging sequence `x n` of elements of `s`, the limit belongs to `s` as well. Note that the sequential closure of a set is not guaranteed to be sequentially closed. -/ def is_seq_closed (s : set X) : Prop := ∀ ⦃x : ℕ → X⦄ ⦃p : X⦄, (∀ n, x n ∈ s) → tendsto x at_top (𝓝 p) → p ∈ s /-- The sequential closure of a sequentially closed set is the set itself. -/ lemma is_seq_closed.seq_closure_eq {s : set X} (hs : is_seq_closed s) : seq_closure s = s := subset.antisymm (λ p ⟨x, hx, hp⟩, hs hx hp) subset_seq_closure /-- If a set is equal to its sequential closure, then it is sequentially closed. -/ lemma is_seq_closed_of_seq_closure_eq {s : set X} (hs : seq_closure s = s) : is_seq_closed s := λ x p hxs hxp, hs ▸ ⟨x, hxs, hxp⟩ /-- A set is sequentially closed iff it is equal to its sequential closure. -/ lemma is_seq_closed_iff {s : set X} : is_seq_closed s ↔ seq_closure s = s := ⟨is_seq_closed.seq_closure_eq, is_seq_closed_of_seq_closure_eq⟩ /-- A set is sequentially closed if it is closed. -/ protected lemma is_closed.is_seq_closed {s : set X} (hc : is_closed s) : is_seq_closed s := λ u x hu hx, hc.mem_of_tendsto hx (eventually_of_forall hu) /-- A topological space is called a *Fréchet-Urysohn space*, if the sequential closure of any set is equal to its closure. Since one of the inclusions is trivial, we require only the non-trivial one in the definition. -/ class frechet_urysohn_space (X : Type*) [topological_space X] : Prop := (closure_subset_seq_closure : ∀ s : set X, closure s ⊆ seq_closure s) lemma seq_closure_eq_closure [frechet_urysohn_space X] (s : set X) : seq_closure s = closure s := seq_closure_subset_closure.antisymm $ frechet_urysohn_space.closure_subset_seq_closure s /-- In a Fréchet-Urysohn space, a point belongs to the closure of a set iff it is a limit of a sequence taking values in this set. -/ lemma mem_closure_iff_seq_limit [frechet_urysohn_space X] {s : set X} {a : X} : a ∈ closure s ↔ ∃ x : ℕ → X, (∀ n : ℕ, x n ∈ s) ∧ tendsto x at_top (𝓝 a) := by { rw [← seq_closure_eq_closure], refl } /-- If the domain of a function `f : α → β` is a Fréchet-Urysohn space, then convergence is equivalent to sequential convergence. See also `filter.tendsto_iff_seq_tendsto` for a version that works for any pair of filters assuming that the filter in the domain is countably generated. This property is equivalent to the definition of `frechet_urysohn_space`, see `frechet_urysohn_space.of_seq_tendsto_imp_tendsto`. -/ lemma tendsto_nhds_iff_seq_tendsto [frechet_urysohn_space X] {f : X → Y} {a : X} {b : Y} : tendsto f (𝓝 a) (𝓝 b) ↔ ∀ u : ℕ → X, tendsto u at_top (𝓝 a) → tendsto (f ∘ u) at_top (𝓝 b) := begin refine ⟨λ hf u hu, hf.comp hu, λ h, ((nhds_basis_closeds _).tendsto_iff (nhds_basis_closeds _)).2 _⟩, rintro s ⟨hbs, hsc⟩, refine ⟨closure (f ⁻¹' s), ⟨mt _ hbs, is_closed_closure⟩, λ x, mt $ λ hx, subset_closure hx⟩, rw [← seq_closure_eq_closure], rintro ⟨u, hus, hu⟩, exact hsc.mem_of_tendsto (h u hu) (eventually_of_forall hus) end /-- An alternative construction for `frechet_urysohn_space`: if sequential convergence implies convergence, then the space is a Fréchet-Urysohn space. -/ lemma frechet_urysohn_space.of_seq_tendsto_imp_tendsto (h : ∀ (f : X → Prop) (a : X), (∀ u : ℕ → X, tendsto u at_top (𝓝 a) → tendsto (f ∘ u) at_top (𝓝 (f a))) → continuous_at f a) : frechet_urysohn_space X := begin refine ⟨λ s x hcx, _⟩, specialize h (∉ s) x, by_cases hx : x ∈ s, { exact subset_seq_closure hx }, simp_rw [(∘), continuous_at, hx, not_false_iff, nhds_true, tendsto_pure, eq_true_iff, ← mem_compl_iff, eventually_mem_set, ← mem_interior_iff_mem_nhds, interior_compl] at h, rw [mem_compl_iff, imp_not_comm] at h, simp only [not_forall, not_eventually, mem_compl_iff, not_not] at h, rcases h hcx with ⟨u, hux, hus⟩, rcases extraction_of_frequently_at_top hus with ⟨φ, φ_mono, hφ⟩, exact ⟨u ∘ φ, hφ, hux.comp φ_mono.tendsto_at_top⟩ end /-- Every first-countable space is a Fréchet-Urysohn space. -/ @[priority 100] -- see Note [lower instance priority] instance topological_space.first_countable_topology.frechet_urysohn_space [first_countable_topology X] : frechet_urysohn_space X := frechet_urysohn_space.of_seq_tendsto_imp_tendsto $ λ f a, tendsto_iff_seq_tendsto.2 /-- A topological space is said to be a *sequential space* if any sequentially closed set in this space is closed. This condition is weaker than being a Fréchet-Urysohn space. -/ class sequential_space (X : Type*) [topological_space X] : Prop := (is_closed_of_seq : ∀ s : set X, is_seq_closed s → is_closed s) /-- Every Fréchet-Urysohn space is a sequential space. -/ @[priority 100] -- see Note [lower instance priority] instance frechet_urysohn_space.to_sequential_space [frechet_urysohn_space X] : sequential_space X := ⟨λ s hs, by rw [← closure_eq_iff_is_closed, ← seq_closure_eq_closure, hs.seq_closure_eq]⟩ /-- In a sequential space, a sequentially closed set is closed. -/ protected lemma is_seq_closed.is_closed [sequential_space X] {s : set X} (hs : is_seq_closed s) : is_closed s := sequential_space.is_closed_of_seq s hs /-- In a sequential space, a set is closed iff it's sequentially closed. -/ lemma is_seq_closed_iff_is_closed [sequential_space X] {M : set X} : is_seq_closed M ↔ is_closed M := ⟨is_seq_closed.is_closed, is_closed.is_seq_closed⟩ /-- A function between topological spaces is sequentially continuous if it commutes with limit of convergent sequences. -/ def seq_continuous (f : X → Y) : Prop := ∀ ⦃x : ℕ → X⦄ ⦃p : X⦄, tendsto x at_top (𝓝 p) → tendsto (f ∘ x) at_top (𝓝 (f p)) /-- The preimage of a sequentially closed set under a sequentially continuous map is sequentially closed. -/ lemma is_seq_closed.preimage {f : X → Y} {s : set Y} (hs : is_seq_closed s) (hf : seq_continuous f) : is_seq_closed (f ⁻¹' s) := λ x p hx hp, hs hx (hf hp) /- A continuous function is sequentially continuous. -/ protected lemma continuous.seq_continuous {f : X → Y} (hf : continuous f) : seq_continuous f := λ x p hx, (hf.tendsto p).comp hx /-- A sequentially continuous function defined on a sequential space is continuous. -/ protected lemma seq_continuous.continuous [sequential_space X] {f : X → Y} (hf : seq_continuous f) : continuous f := continuous_iff_is_closed.mpr $ λ s hs, (hs.is_seq_closed.preimage hf).is_closed /-- If the domain of a function is a sequential space, then continuity of this function is equivalent to its sequential continuity. -/ lemma continuous_iff_seq_continuous [sequential_space X] {f : X → Y} : continuous f ↔ seq_continuous f := ⟨continuous.seq_continuous, seq_continuous.continuous⟩ lemma quotient_map.sequential_space [sequential_space X] {f : X → Y} (hf : quotient_map f) : sequential_space Y := ⟨λ s hs, hf.is_closed_preimage.mp $ (hs.preimage $ hf.continuous.seq_continuous).is_closed⟩ /-- The quotient of a sequential space is a sequential space. -/ instance [sequential_space X] {s : setoid X} : sequential_space (quotient s) := quotient_map_quot_mk.sequential_space end topological_space section seq_compact open topological_space topological_space.first_countable_topology variables [topological_space X] /-- A set `s` is sequentially compact if every sequence taking values in `s` has a converging subsequence. -/ def is_seq_compact (s : set X) := ∀ ⦃x : ℕ → X⦄, (∀ n, x n ∈ s) → ∃ (a ∈ s) (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (x ∘ φ) at_top (𝓝 a) /-- A space `X` is sequentially compact if every sequence in `X` has a converging subsequence. -/ @[mk_iff] class seq_compact_space (X : Type*) [topological_space X] : Prop := (seq_compact_univ : is_seq_compact (univ : set X)) export seq_compact_space (seq_compact_univ) lemma is_seq_compact.subseq_of_frequently_in {s : set X} (hs : is_seq_compact s) {x : ℕ → X} (hx : ∃ᶠ n in at_top, x n ∈ s) : ∃ (a ∈ s) (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (x ∘ φ) at_top (𝓝 a) := let ⟨ψ, hψ, huψ⟩ := extraction_of_frequently_at_top hx, ⟨a, a_in, φ, hφ, h⟩ := hs huψ in ⟨a, a_in, ψ ∘ φ, hψ.comp hφ, h⟩ lemma seq_compact_space.tendsto_subseq [seq_compact_space X] (x : ℕ → X) : ∃ a (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (x ∘ φ) at_top (𝓝 a) := let ⟨a, _, φ, mono, h⟩ := seq_compact_univ (λ n, mem_univ (x n)) in ⟨a, φ, mono, h⟩ section first_countable_topology variables [first_countable_topology X] open topological_space.first_countable_topology protected lemma is_compact.is_seq_compact {s : set X} (hs : is_compact s) : is_seq_compact s := λ x x_in, let ⟨a, a_in, ha⟩ := hs (tendsto_principal.mpr (eventually_of_forall x_in)) in ⟨a, a_in, tendsto_subseq ha⟩ lemma is_compact.tendsto_subseq' {s : set X} {x : ℕ → X} (hs : is_compact s) (hx : ∃ᶠ n in at_top, x n ∈ s) : ∃ (a ∈ s) (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (x ∘ φ) at_top (𝓝 a) := hs.is_seq_compact.subseq_of_frequently_in hx lemma is_compact.tendsto_subseq {s : set X} {x : ℕ → X} (hs : is_compact s) (hx : ∀ n, x n ∈ s) : ∃ (a ∈ s) (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (x ∘ φ) at_top (𝓝 a) := hs.is_seq_compact hx @[priority 100] -- see Note [lower instance priority] instance first_countable_topology.seq_compact_of_compact [compact_space X] : seq_compact_space X := ⟨is_compact_univ.is_seq_compact⟩ lemma compact_space.tendsto_subseq [compact_space X] (x : ℕ → X) : ∃ a (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (x ∘ φ) at_top (𝓝 a) := seq_compact_space.tendsto_subseq x end first_countable_topology end seq_compact section uniform_space_seq_compact open_locale uniformity open uniform_space prod variables [uniform_space X] {s : set X} lemma is_seq_compact.exists_tendsto_of_frequently_mem (hs : is_seq_compact s) {u : ℕ → X} (hu : ∃ᶠ n in at_top, u n ∈ s) (huc : cauchy_seq u) : ∃ x ∈ s, tendsto u at_top (𝓝 x) := let ⟨x, hxs, φ, φ_mono, hx⟩ := hs.subseq_of_frequently_in hu in ⟨x, hxs, tendsto_nhds_of_cauchy_seq_of_subseq huc φ_mono.tendsto_at_top hx⟩ lemma is_seq_compact.exists_tendsto (hs : is_seq_compact s) {u : ℕ → X} (hu : ∀ n, u n ∈ s) (huc : cauchy_seq u) : ∃ x ∈ s, tendsto u at_top (𝓝 x) := hs.exists_tendsto_of_frequently_mem (frequently_of_forall hu) huc /-- A sequentially compact set in a uniform space is totally bounded. -/ protected lemma is_seq_compact.totally_bounded (h : is_seq_compact s) : totally_bounded s := begin intros V V_in, unfold is_seq_compact at h, contrapose! h, obtain ⟨u, u_in, hu⟩ : ∃ u : ℕ → X, (∀ n, u n ∈ s) ∧ ∀ n m, m < n → u m ∉ ball (u n) V, { simp only [not_subset, mem_Union₂, not_exists, exists_prop] at h, simpa only [forall_and_distrib, ball_image_iff, not_and] using seq_of_forall_finite_exists h }, refine ⟨u, u_in, λ x x_in φ hφ huφ, _⟩, obtain ⟨N, hN⟩ : ∃ N, ∀ p q, p ≥ N → q ≥ N → (u (φ p), u (φ q)) ∈ V, from huφ.cauchy_seq.mem_entourage V_in, exact hu (φ $ N + 1) (φ N) (hφ $ lt_add_one N) (hN (N + 1) N N.le_succ le_rfl) end variables [is_countably_generated (𝓤 X)] /-- A sequentially compact set in a uniform set with countably generated uniformity filter is complete. -/ protected lemma is_seq_compact.is_complete (hs : is_seq_compact s) : is_complete s := begin intros l hl hls, haveI := hl.1, rcases exists_antitone_basis (𝓤 X) with ⟨V, hV⟩, choose W hW hWV using λ n, comp_mem_uniformity_sets (hV.mem n), have hWV' : ∀ n, W n ⊆ V n, from λ n ⟨x, y⟩ hx, @hWV n (x, y) ⟨x, refl_mem_uniformity $ hW _, hx⟩, obtain ⟨t, ht_anti, htl, htW, hts⟩ : ∃ t : ℕ → set X, antitone t ∧ (∀ n, t n ∈ l) ∧ (∀ n, t n ×ˢ t n ⊆ W n) ∧ (∀ n, t n ⊆ s), { have : ∀ n, ∃ t ∈ l, t ×ˢ t ⊆ W n ∧ t ⊆ s, { rw [le_principal_iff] at hls, have : ∀ n, W n ∩ s ×ˢ s ∈ l ×ᶠ l := λ n, inter_mem (hl.2 (hW n)) (prod_mem_prod hls hls), simpa only [l.basis_sets.prod_self.mem_iff, true_implies_iff, subset_inter_iff, prod_self_subset_prod_self, and.assoc] using this }, choose t htl htW hts, have : ∀ n, (⋂ k ≤ n, t k) ⊆ t n, from λ n, Inter₂_subset _ le_rfl, exact ⟨λ n, ⋂ k ≤ n, t k, λ m n h, bInter_subset_bInter_left (λ k (hk : k ≤ m), hk.trans h), λ n, (bInter_mem (finite_le_nat n)).2 (λ k hk, htl k), λ n, (prod_mono (this n) (this n)).trans (htW n), λ n, (this n).trans (hts n)⟩ }, choose u hu using λ n, filter.nonempty_of_mem (htl n), have huc : cauchy_seq u := hV.to_has_basis.cauchy_seq_iff.2 (λ N hN, ⟨N, λ m hm n hn, hWV' _ $ @htW N (_, _) ⟨ht_anti hm (hu _), (ht_anti hn (hu _))⟩⟩), rcases hs.exists_tendsto (λ n, hts n (hu n)) huc with ⟨x, hxs, hx⟩, refine ⟨x, hxs, (nhds_basis_uniformity' hV.to_has_basis).ge_iff.2 $ λ N hN, _⟩, obtain ⟨n, hNn, hn⟩ : ∃ n, N ≤ n ∧ u n ∈ ball x (W N), from ((eventually_ge_at_top N).and (hx $ ball_mem_nhds x (hW N))).exists, refine mem_of_superset (htl n) (λ y hy, hWV N ⟨u n, _, htW N ⟨_, _⟩⟩), exacts [hn, ht_anti hNn (hu n), ht_anti hNn hy] end /-- If `𝓤 β` is countably generated, then any sequentially compact set is compact. -/ protected lemma is_seq_compact.is_compact (hs : is_seq_compact s) : is_compact s := is_compact_iff_totally_bounded_is_complete.2 ⟨hs.totally_bounded, hs.is_complete⟩ /-- A version of Bolzano-Weistrass: in a uniform space with countably generated uniformity filter (e.g., in a metric space), a set is compact if and only if it is sequentially compact. -/ protected lemma uniform_space.is_compact_iff_is_seq_compact : is_compact s ↔ is_seq_compact s := ⟨λ H, H.is_seq_compact, λ H, H.is_compact⟩ lemma uniform_space.compact_space_iff_seq_compact_space : compact_space X ↔ seq_compact_space X := by simp only [← is_compact_univ_iff, seq_compact_space_iff, uniform_space.is_compact_iff_is_seq_compact] end uniform_space_seq_compact section metric_seq_compact variables [pseudo_metric_space X] open metric lemma seq_compact.lebesgue_number_lemma_of_metric {ι : Sort*} {c : ι → set X} {s : set X} (hs : is_seq_compact s) (hc₁ : ∀ i, is_open (c i)) (hc₂ : s ⊆ ⋃ i, c i) : ∃ δ > 0, ∀ a ∈ s, ∃ i, ball a δ ⊆ c i := lebesgue_number_lemma_of_metric hs.is_compact hc₁ hc₂ variables [proper_space X] {s : set X} /-- A version of **Bolzano-Weistrass**: in a proper metric space (eg. $ℝ^n$), every bounded sequence has a converging subsequence. This version assumes only that the sequence is frequently in some bounded set. -/ lemma tendsto_subseq_of_frequently_bounded (hs : bounded s) {x : ℕ → X} (hx : ∃ᶠ n in at_top, x n ∈ s) : ∃ a ∈ closure s, ∃ φ : ℕ → ℕ, strict_mono φ ∧ tendsto (x ∘ φ) at_top (𝓝 a) := have hcs : is_seq_compact (closure s), from hs.is_compact_closure.is_seq_compact, have hu' : ∃ᶠ n in at_top, x n ∈ closure s, from hx.mono (λ n hn, subset_closure hn), hcs.subseq_of_frequently_in hu' /-- A version of Bolzano-Weistrass: in a proper metric space (eg. $ℝ^n$), every bounded sequence has a converging subsequence. -/ lemma tendsto_subseq_of_bounded (hs : bounded s) {x : ℕ → X} (hx : ∀ n, x n ∈ s) : ∃ a ∈ closure s, ∃ φ : ℕ → ℕ, strict_mono φ ∧ tendsto (x ∘ φ) at_top (𝓝 a) := tendsto_subseq_of_frequently_bounded hs $ frequently_of_forall hx end metric_seq_compact
2b214596d9af4de8cf6e1db7420f042d13d65700
0c1546a496eccfb56620165cad015f88d56190c5
/tests/lean/run/isabelle.lean
6031a294abcad1de92481eb806445d597816dc57
[ "Apache-2.0" ]
permissive
Solertis/lean
491e0939957486f664498fbfb02546e042699958
84188c5aa1673fdf37a082b2de8562dddf53df3f
refs/heads/master
1,610,174,257,606
1,486,263,620,000
1,486,263,620,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,450
lean
/- Isabelle style tactics. This test is based on a file created by Gabriel Ebner. -/ universe variables u v inductive lazy_list (α : Type u) : Type u | nil {} : lazy_list | cons : α → thunk (lazy_list) → lazy_list namespace lazy_list variables {α : Type u} {β : Type v} def singleton : α → lazy_list α | a := cons a nil def of_list : list α → lazy_list α | [] := nil | (h::t) := cons h (of_list t) def append : lazy_list α → thunk (lazy_list α) → lazy_list α | nil l := l () | (cons h t) l := cons h (append (t ()) (l ())) def map (f : α → β) : lazy_list α → lazy_list β | nil := nil | (cons h t) := cons (f h) (map (t ())) def join : lazy_list (lazy_list α) → lazy_list α | nil := nil | (cons h t) := append h (join (t ())) def for (l : lazy_list α) (f : α → β) : lazy_list β := map f l def approx : nat → lazy_list α → list α | 0 l := [] | _ nil := [] | (a+1) (cons h t) := h :: approx a (t ()) meta def above : nat → lazy_list nat | i := cons i (above (i+1)) end lazy_list meta def lazy_tactic (α : Type u) := tactic_state → lazy_list (α × tactic_state) namespace lazy_tactic open lazy_list meta def of_tactic {α : Type u} (t : tactic α) : lazy_tactic α := λ s, match t s with | tactic_result.success a new_s := lazy_list.singleton (a, new_s) | tactic_result.exception .α f e s := lazy_list.nil end meta instance {α : Type} : has_coe (tactic α) (lazy_tactic α) := ⟨of_tactic⟩ protected meta def return {α} (a : α) : lazy_tactic α := λ s, lazy_list.singleton (a, s) protected meta def map {α β} (f : α → β) : lazy_tactic α → lazy_tactic β | t s := (t s)^.for (λ ⟨a, new_s⟩, (f a, new_s)) protected meta def bind {α β} : lazy_tactic α → (α → lazy_tactic β) → lazy_tactic β := λ t₁ t₂ s, join (for (t₁ s) (λ ⟨a, new_s⟩, t₂ a new_s)) protected meta def orelse {α} (t₁ t₂ : lazy_tactic α) : lazy_tactic α := λ s, append (t₁ s) (t₂ s) protected meta def failure {α} : lazy_tactic α := λ s, nil meta instance : monad lazy_tactic := { ret := @lazy_tactic.return, bind := @lazy_tactic.bind, map := @lazy_tactic.map } meta instance : alternative lazy_tactic := { map := @lazy_tactic.map, pure := @lazy_tactic.return, seq := @fapp _ _, failure := @lazy_tactic.failure, orelse := @lazy_tactic.orelse } meta def choose {α} (xs : list α) : lazy_tactic α := λ s, of_list $ xs^.for (λ a, (a, s)) meta def run {α} (t : lazy_tactic α) : tactic α := λ s, match t s with | nil := tactic.failed s | cons (a, new_s) ss := tactic_result.success a new_s end open tactic private meta def try_constructors : list name → lazy_tactic unit | [] := failure | (c::cs) := (mk_const c >>= apply : tactic unit) <|> try_constructors cs /- Backtracking version of constructor -/ meta def constructor : lazy_tactic unit := do t ← target, cs ← get_constructors_for t, try_constructors cs end lazy_tactic open lazy_tactic example (p q : Prop) : q → p ∨ q := by run $ do tactic.intros, constructor, tactic.trace_state, tactic.assumption meta def naive_instantiation : lazy_tactic unit := let vals := [`(1),`(2),`(3)] in do x ← choose vals, y ← choose vals, e ← tactic.to_expr `(nat.add_comm %%x %%y), tactic.trace e, tactic.exact e lemma ex : 1 + 3 = 3 + 1 := by naive_instantiation^.run
0f2341e4624ce466d33c5d7382460d5330e9de9f
c777c32c8e484e195053731103c5e52af26a25d1
/src/ring_theory/artinian.lean
ed598587452c912e411b1b455e966ea0f9e94061
[ "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
18,970
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.nakayama import data.set_like.fintype /-! # Artinian rings and modules A module satisfying these equivalent conditions is said to be an *Artinian* R-module if every decreasing chain of submodules is eventually constant, or equivalently, if the relation `<` on submodules is well founded. A ring is said to be left (or right) Artinian if it is Artinian as a left (or right) module over itself, or simply Artinian if it is both left and right Artinian. ## Main definitions Let `R` be a ring and let `M` and `P` be `R`-modules. Let `N` be an `R`-submodule of `M`. * `is_artinian R M` is the proposition that `M` is a Artinian `R`-module. It is a class, implemented as the predicate that the `<` relation on submodules is well founded. * `is_artinian_ring R` is the proposition that `R` is a left Artinian ring. ## References * [M. F. Atiyah and I. G. Macdonald, *Introduction to commutative algebra*][atiyah-macdonald] * [samuel] ## Tags Artinian, artinian, Artinian ring, Artinian module, artinian ring, artinian module -/ open set open_locale big_operators pointwise /-- `is_artinian R M` is the proposition that `M` is an Artinian `R`-module, implemented as the well-foundedness of submodule inclusion. -/ class is_artinian (R M) [semiring R] [add_comm_monoid M] [module R M] : Prop := (well_founded_submodule_lt [] : well_founded ((<) : submodule R M → submodule R M → Prop)) section variables {R M P N : Type*} variables [ring R] [add_comm_group M] [add_comm_group P] [add_comm_group N] variables [module R M] [module R P] [module R N] open is_artinian include R theorem is_artinian_of_injective (f : M →ₗ[R] P) (h : function.injective f) [is_artinian R P] : is_artinian R M := ⟨subrelation.wf (λ A B hAB, show A.map f < B.map f, from submodule.map_strict_mono_of_injective h hAB) (inv_image.wf (submodule.map f) (is_artinian.well_founded_submodule_lt R P))⟩ instance is_artinian_submodule' [is_artinian R M] (N : submodule R M) : is_artinian R N := is_artinian_of_injective N.subtype subtype.val_injective lemma is_artinian_of_le {s t : submodule R M} [ht : is_artinian R t] (h : s ≤ t) : is_artinian R s := is_artinian_of_injective (submodule.of_le h) (submodule.of_le_injective h) variable (M) theorem is_artinian_of_surjective (f : M →ₗ[R] P) (hf : function.surjective f) [is_artinian R M] : is_artinian R P := ⟨subrelation.wf (λ A B hAB, show A.comap f < B.comap f, from submodule.comap_strict_mono_of_surjective hf hAB) (inv_image.wf (submodule.comap f) (is_artinian.well_founded_submodule_lt _ _))⟩ variable {M} theorem is_artinian_of_linear_equiv (f : M ≃ₗ[R] P) [is_artinian R M] : is_artinian R P := is_artinian_of_surjective _ f.to_linear_map f.to_equiv.surjective theorem is_artinian_of_range_eq_ker [is_artinian R M] [is_artinian R P] (f : M →ₗ[R] N) (g : N →ₗ[R] P) (hf : function.injective f) (hg : function.surjective g) (h : f.range = g.ker) : is_artinian R N := ⟨well_founded_lt_exact_sequence (is_artinian.well_founded_submodule_lt _ _) (is_artinian.well_founded_submodule_lt _ _) f.range (submodule.map f) (submodule.comap f) (submodule.comap g) (submodule.map g) (submodule.gci_map_comap hf) (submodule.gi_map_comap hg) (by simp [submodule.map_comap_eq, inf_comm]) (by simp [submodule.comap_map_eq, h])⟩ instance is_artinian_prod [is_artinian R M] [is_artinian R P] : is_artinian R (M × P) := is_artinian_of_range_eq_ker (linear_map.inl R M P) (linear_map.snd R M P) linear_map.inl_injective linear_map.snd_surjective (linear_map.range_inl R M P) @[priority 100] instance is_artinian_of_finite [finite M] : is_artinian R M := ⟨finite.well_founded_of_trans_of_irrefl _⟩ local attribute [elab_as_eliminator] finite.induction_empty_option instance is_artinian_pi {R ι : Type*} [finite ι] : Π {M : ι → Type*} [ring R] [Π i, add_comm_group (M i)], by exactI Π [Π i, module R (M i)], by exactI Π [∀ i, is_artinian R (M i)], is_artinian R (Π i, M i) := finite.induction_empty_option (begin introsI α β e hα M _ _ _ _, exact is_artinian_of_linear_equiv (linear_equiv.Pi_congr_left R M e) end) (by { introsI M _ _ _ _, apply_instance }) (begin introsI α _ ih M _ _ _ _, exact is_artinian_of_linear_equiv (linear_equiv.pi_option_equiv_prod R).symm, end) ι /-- A version of `is_artinian_pi` for non-dependent functions. We need this instance because sometimes Lean fails to apply the dependent version in non-dependent settings (e.g., it fails to prove that `ι → ℝ` is finite dimensional over `ℝ`). -/ instance is_artinian_pi' {R ι M : Type*} [ring R] [add_comm_group M] [module R M] [finite ι] [is_artinian R M] : is_artinian R (ι → M) := is_artinian_pi end open is_artinian submodule function section ring variables {R M : Type*} [ring R] [add_comm_group M] [module R M] theorem is_artinian_iff_well_founded : is_artinian R M ↔ well_founded ((<) : submodule R M → submodule R M → Prop) := ⟨λ h, h.1, is_artinian.mk⟩ variables {R M} lemma is_artinian.finite_of_linear_independent [nontrivial R] [is_artinian R M] {s : set M} (hs : linear_independent R (coe : s → M)) : s.finite := begin refine classical.by_contradiction (λ hf, (rel_embedding.well_founded_iff_no_descending_seq.1 (well_founded_submodule_lt R M)).elim' _), have f : ℕ ↪ s, from set.infinite.nat_embedding s hf, have : ∀ n, (coe ∘ f) '' {m | n ≤ m} ⊆ s, { rintros n x ⟨y, hy₁, rfl⟩, exact (f y).2 }, have : ∀ a b : ℕ, a ≤ b ↔ span R ((coe ∘ f) '' {m | b ≤ m}) ≤ span R ((coe ∘ f) '' {m | a ≤ m}), { assume a b, rw [span_le_span_iff hs (this b) (this a), set.image_subset_image_iff (subtype.coe_injective.comp f.injective), set.subset_def], simp only [set.mem_set_of_eq], exact ⟨λ hab x, le_trans hab, λ h, (h _ le_rfl)⟩ }, exact ⟨⟨λ n, span R ((coe ∘ f) '' {m | n ≤ m}), λ x y, by simp [le_antisymm_iff, (this _ _).symm] {contextual := tt}⟩, begin intros a b, conv_rhs { rw [gt, lt_iff_le_not_le, this, this, ← lt_iff_le_not_le] }, simp end⟩ end /-- A module is Artinian iff every nonempty set of submodules has a minimal submodule among them. -/ theorem set_has_minimal_iff_artinian : (∀ a : set $ submodule R M, a.nonempty → ∃ M' ∈ a, ∀ I ∈ a, ¬ I < M') ↔ is_artinian R M := by rw [is_artinian_iff_well_founded, well_founded.well_founded_iff_has_min] theorem is_artinian.set_has_minimal [is_artinian R M] (a : set $ submodule R M) (ha : a.nonempty) : ∃ M' ∈ a, ∀ I ∈ a, ¬ I < M' := set_has_minimal_iff_artinian.mpr ‹_› a ha /-- A module is Artinian iff every decreasing chain of submodules stabilizes. -/ theorem monotone_stabilizes_iff_artinian : (∀ (f : ℕ →o (submodule R M)ᵒᵈ), ∃ n, ∀ m, n ≤ m → f n = f m) ↔ is_artinian R M := by { rw is_artinian_iff_well_founded, exact well_founded.monotone_chain_condition.symm } namespace is_artinian variables [is_artinian R M] theorem monotone_stabilizes (f : ℕ →o (submodule R M)ᵒᵈ) : ∃ n, ∀ m, n ≤ m → f n = f m := monotone_stabilizes_iff_artinian.mpr ‹_› f /-- If `∀ I > J, P I` implies `P J`, then `P` holds for all submodules. -/ lemma induction {P : submodule R M → Prop} (hgt : ∀ I, (∀ J < I, P J) → P I) (I : submodule R M) : P I := (well_founded_submodule_lt R M).recursion I hgt /-- For any endomorphism of a Artinian module, there is some nontrivial iterate with disjoint kernel and range. -/ theorem exists_endomorphism_iterate_ker_sup_range_eq_top (f : M →ₗ[R] M) : ∃ n : ℕ, n ≠ 0 ∧ (f ^ n).ker ⊔ (f ^ n).range = ⊤ := begin obtain ⟨n, w⟩ := monotone_stabilizes (f.iterate_range.comp ⟨λ n, n+1, λ n m w, by linarith⟩), specialize w ((n + 1) + n) (by linarith), dsimp at w, refine ⟨n + 1, nat.succ_ne_zero _, _⟩, simp_rw [eq_top_iff', mem_sup], intro x, have : (f^(n + 1)) x ∈ (f ^ ((n + 1) + n + 1)).range, { rw ← w, exact mem_range_self _ }, rcases this with ⟨y, hy⟩, use x - (f ^ (n+1)) y, split, { rw [linear_map.mem_ker, linear_map.map_sub, ← hy, sub_eq_zero, pow_add], simp [iterate_add_apply], }, { use (f^ (n+1)) y, simp } end /-- Any injective endomorphism of an Artinian module is surjective. -/ theorem surjective_of_injective_endomorphism (f : M →ₗ[R] M) (s : injective f) : surjective f := begin obtain ⟨n, ne, w⟩ := exists_endomorphism_iterate_ker_sup_range_eq_top f, rw [linear_map.ker_eq_bot.mpr (linear_map.iterate_injective s n), bot_sup_eq, linear_map.range_eq_top] at w, exact linear_map.surjective_of_iterate_surjective ne w, end /-- Any injective endomorphism of an Artinian module is bijective. -/ theorem bijective_of_injective_endomorphism (f : M →ₗ[R] M) (s : injective f) : bijective f := ⟨s, surjective_of_injective_endomorphism f s⟩ /-- A sequence `f` of submodules of a artinian module, with the supremum `f (n+1)` and the infinum of `f 0`, ..., `f n` being ⊤, is eventually ⊤. -/ lemma disjoint_partial_infs_eventually_top (f : ℕ → submodule R M) (h : ∀ n, disjoint (partial_sups (order_dual.to_dual ∘ f) n) (order_dual.to_dual (f (n+1)))) : ∃ n : ℕ, ∀ m, n ≤ m → f m = ⊤ := begin -- A little off-by-one cleanup first: rsuffices ⟨n, w⟩ : ∃ n : ℕ, ∀ m, n ≤ m → order_dual.to_dual f (m+1) = ⊤, { use n+1, rintros (_|m) p, { cases p, }, { apply w, exact nat.succ_le_succ_iff.mp p }, }, obtain ⟨n, w⟩ := monotone_stabilizes (partial_sups (order_dual.to_dual ∘ f)), refine ⟨n, λ m p, _⟩, exact (h m).eq_bot_of_ge (sup_eq_left.1 $ (w (m + 1) $ le_add_right p).symm.trans $ w m p) end end is_artinian end ring section comm_ring variables {R : Type*} (M : Type*) [comm_ring R] [add_comm_group M] [module R M] [is_artinian R M] namespace is_artinian lemma range_smul_pow_stabilizes (r : R) : ∃ n : ℕ, ∀ m, n ≤ m → (r^n • linear_map.id : M →ₗ[R] M).range = (r^m • linear_map.id : M →ₗ[R] M).range := monotone_stabilizes ⟨λ n, (r^n • linear_map.id : M →ₗ[R] M).range, λ n m h x ⟨y, hy⟩, ⟨r ^ (m - n) • y, by { dsimp at ⊢ hy, rw [←smul_assoc, smul_eq_mul, ←pow_add, ←hy, add_tsub_cancel_of_le h] }⟩⟩ variables {M} lemma exists_pow_succ_smul_dvd (r : R) (x : M) : ∃ (n : ℕ) (y : M), r ^ n.succ • y = r ^ n • x := begin obtain ⟨n, hn⟩ := is_artinian.range_smul_pow_stabilizes M r, simp_rw [set_like.ext_iff] at hn, exact ⟨n, by simpa using hn n.succ n.le_succ (r ^ n • x)⟩, end end is_artinian end comm_ring -- TODO: Prove this for artinian modules -- /-- -- If `M ⊕ N` embeds into `M`, for `M` noetherian over `R`, then `N` is trivial. -- -/ -- universe w -- variables {N : Type w} [add_comm_group N] [module R N] -- noncomputable def is_noetherian.equiv_punit_of_prod_injective [is_noetherian R M] -- (f : M × N →ₗ[R] M) (i : injective f) : N ≃ₗ[R] punit.{w+1} := -- begin -- apply nonempty.some, -- obtain ⟨n, w⟩ := is_noetherian.disjoint_partial_sups_eventually_bot (f.tailing i) -- (f.tailings_disjoint_tailing i), -- specialize w n (le_refl n), -- apply nonempty.intro, -- refine (f.tailing_linear_equiv i n).symm.trans _, -- rw w, -- exact submodule.bot_equiv_punit, -- end /-- A ring is Artinian if it is Artinian as a module over itself. Strictly speaking, this should be called `is_left_artinian_ring` but we omit the `left_` for convenience in the commutative case. For a right Artinian ring, use `is_artinian Rᵐᵒᵖ R`. -/ @[reducible] def is_artinian_ring (R) [ring R] := is_artinian R R theorem is_artinian_ring_iff {R} [ring R] : is_artinian_ring R ↔ is_artinian R R := iff.rfl theorem ring.is_artinian_of_zero_eq_one {R} [ring R] (h01 : (0 : R) = 1) : is_artinian_ring R := have _ := subsingleton_of_zero_eq_one h01, by exactI infer_instance theorem is_artinian_of_submodule_of_artinian (R M) [ring R] [add_comm_group M] [module R M] (N : submodule R M) (h : is_artinian R M) : is_artinian R N := by apply_instance theorem is_artinian_of_quotient_of_artinian (R) [ring R] (M) [add_comm_group M] [module R M] (N : submodule R M) (h : is_artinian R M) : is_artinian R (M ⧸ N) := is_artinian_of_surjective M (submodule.mkq N) (submodule.quotient.mk_surjective N) /-- If `M / S / R` is a scalar tower, and `M / R` is Artinian, then `M / S` is also Artinian. -/ theorem is_artinian_of_tower (R) {S M} [comm_ring R] [ring S] [add_comm_group M] [algebra R S] [module S M] [module R M] [is_scalar_tower R S M] (h : is_artinian R M) : is_artinian S M := begin rw is_artinian_iff_well_founded at h ⊢, refine (submodule.restrict_scalars_embedding R S M).well_founded h end theorem is_artinian_of_fg_of_artinian {R M} [ring R] [add_comm_group M] [module R M] (N : submodule R M) [is_artinian_ring R] (hN : N.fg) : is_artinian R N := let ⟨s, hs⟩ := hN in begin haveI := classical.dec_eq M, haveI := classical.dec_eq R, have : ∀ x ∈ s, x ∈ N, from λ x hx, hs ▸ submodule.subset_span hx, refine @@is_artinian_of_surjective ((↑s : set M) → R) _ _ _ (pi.module _ _ _) _ _ _ is_artinian_pi, { fapply linear_map.mk, { exact λ f, ⟨∑ i in s.attach, f i • i.1, N.sum_mem (λ c _, N.smul_mem _ $ this _ c.2)⟩ }, { intros f g, apply subtype.eq, change ∑ i in s.attach, (f i + g i) • _ = _, simp only [add_smul, finset.sum_add_distrib], refl }, { intros c f, apply subtype.eq, change ∑ i in s.attach, (c • f i) • _ = _, simp only [smul_eq_mul, mul_smul], exact finset.smul_sum.symm } }, rintro ⟨n, hn⟩, change n ∈ N at hn, rw [← hs, ← set.image_id ↑s, finsupp.mem_span_image_iff_total] at hn, rcases hn with ⟨l, hl1, hl2⟩, refine ⟨λ x, l x, subtype.ext _⟩, change ∑ i in s.attach, l i • (i : M) = n, rw [@finset.sum_attach M M s _ (λ i, l i • i), ← hl2, finsupp.total_apply, finsupp.sum, eq_comm], refine finset.sum_subset hl1 (λ x _ hx, _), rw [finsupp.not_mem_support_iff.1 hx, zero_smul] end lemma is_artinian_of_fg_of_artinian' {R M} [ring R] [add_comm_group M] [module R M] [is_artinian_ring R] (h : (⊤ : submodule R M).fg) : is_artinian R M := have is_artinian R (⊤ : submodule R M), from is_artinian_of_fg_of_artinian _ h, by exactI is_artinian_of_linear_equiv (linear_equiv.of_top (⊤ : submodule R M) rfl) /-- In a module over a artinian ring, the submodule generated by finitely many vectors is artinian. -/ theorem is_artinian_span_of_finite (R) {M} [ring R] [add_comm_group M] [module R M] [is_artinian_ring R] {A : set M} (hA : A.finite) : is_artinian R (submodule.span R A) := is_artinian_of_fg_of_artinian _ (submodule.fg_def.mpr ⟨A, hA, rfl⟩) theorem function.surjective.is_artinian_ring {R} [ring R] {S} [ring S] {F} [ring_hom_class F R S] {f : F} (hf : function.surjective f) [H : is_artinian_ring R] : is_artinian_ring S := begin rw [is_artinian_ring_iff, is_artinian_iff_well_founded] at H ⊢, exact (ideal.order_embedding_of_surjective f hf).well_founded H, end instance is_artinian_ring_range {R} [ring R] {S} [ring S] (f : R →+* S) [is_artinian_ring R] : is_artinian_ring f.range := f.range_restrict_surjective.is_artinian_ring namespace is_artinian_ring open is_artinian variables {R : Type*} [comm_ring R] [is_artinian_ring R] lemma is_nilpotent_jacobson_bot : is_nilpotent (ideal.jacobson (⊥ : ideal R)) := begin let Jac := ideal.jacobson (⊥ : ideal R), let f : ℕ →o (ideal R)ᵒᵈ := ⟨λ n, Jac ^ n, λ _ _ h, ideal.pow_le_pow h⟩, obtain ⟨n, hn⟩ : ∃ n, ∀ m, n ≤ m → Jac ^ n = Jac ^ m := is_artinian.monotone_stabilizes f, refine ⟨n, _⟩, let J : ideal R := annihilator (Jac ^ n), suffices : J = ⊤, { have hJ : J • Jac ^ n = ⊥ := annihilator_smul (Jac ^ n), simpa only [this, top_smul, ideal.zero_eq_bot] using hJ }, by_contradiction hJ, change J ≠ ⊤ at hJ, rcases is_artinian.set_has_minimal {J' : ideal R | J < J'} ⟨⊤, hJ.lt_top⟩ with ⟨J', hJJ' : J < J', hJ' : ∀ I, J < I → ¬ I < J'⟩, rcases set_like.exists_of_lt hJJ' with ⟨x, hxJ', hxJ⟩, obtain rfl : J ⊔ ideal.span {x} = J', { apply eq_of_le_of_not_lt _ (hJ' (J ⊔ ideal.span {x}) _), { exact (sup_le hJJ'.le (span_le.2 (singleton_subset_iff.2 hxJ'))) }, { rw set_like.lt_iff_le_and_exists, exact ⟨le_sup_left, ⟨x, mem_sup_right (mem_span_singleton_self x), hxJ⟩⟩ } }, have : J ⊔ Jac • ideal.span {x} ≤ J ⊔ ideal.span {x}, from sup_le_sup_left (smul_le.2 (λ _ _ _, submodule.smul_mem _ _)) _, have : Jac * ideal.span {x} ≤ J, --Need version 4 of Nakayamas lemma on Stacks { classical, by_contradiction H, refine H (smul_sup_le_of_le_smul_of_le_jacobson_bot (fg_span_singleton _) le_rfl (this.eq_of_not_lt (hJ' _ _)).ge), exact lt_of_le_of_ne le_sup_left (λ h, H $ h.symm ▸ le_sup_right) }, have : ideal.span {x} * Jac ^ (n + 1) ≤ ⊥, calc ideal.span {x} * Jac ^ (n + 1) = ideal.span {x} * Jac * Jac ^ n : by rw [pow_succ, ← mul_assoc] ... ≤ J * Jac ^ n : mul_le_mul (by rwa mul_comm) le_rfl ... = ⊥ : by simp [J], refine hxJ (mem_annihilator.2 (λ y hy, (mem_bot R).1 _)), refine this (mul_mem_mul (mem_span_singleton_self x) _), rwa [← hn (n + 1) (nat.le_succ _)] end section localization variables (S : submonoid R) (L : Type*) [comm_ring L] [algebra R L] [is_localization S L] include S /-- Localizing an artinian ring can only reduce the amount of elements. -/ theorem localization_surjective : function.surjective (algebra_map R L) := begin intro r', obtain ⟨r₁, s, rfl⟩ := is_localization.mk'_surjective S r', obtain ⟨r₂, h⟩ : ∃ r : R, is_localization.mk' L 1 s = algebra_map R L r, swap, { exact ⟨r₁ * r₂, by rw [is_localization.mk'_eq_mul_mk'_one, map_mul, h]⟩ }, obtain ⟨n, r, hr⟩ := is_artinian.exists_pow_succ_smul_dvd (s : R) (1 : R), use r, rw [smul_eq_mul, smul_eq_mul, pow_succ', mul_assoc] at hr, apply_fun algebra_map R L at hr, simp only [map_mul, ←submonoid.coe_pow] at hr, rw [←is_localization.mk'_one L, is_localization.mk'_eq_iff_eq, mul_one, submonoid.coe_one, ←(is_localization.map_units L (s ^ n)).mul_left_cancel hr, map_mul], end lemma localization_artinian : is_artinian_ring L := (localization_surjective S L).is_artinian_ring /-- `is_artinian_ring.localization_artinian` can't be made an instance, as it would make `S` + `R` into metavariables. However, this is safe. -/ instance : is_artinian_ring (localization S) := localization_artinian S _ end localization end is_artinian_ring
bc5a81514a9b3a52947b19e242f42eb66b79d0ef
d9ed0fce1c218297bcba93e046cb4e79c83c3af8
/library/tools/super/trim.lean
d928ed28ba1b751625adf7cf2d57fbed3a59ee4d
[ "Apache-2.0" ]
permissive
leodemoura/lean_clone
005c63aa892a6492f2d4741ee3c2cb07a6be9d7f
cc077554b584d39bab55c360bc12a6fe7957afe6
refs/heads/master
1,610,506,475,484
1,482,348,354,000
1,482,348,543,000
77,091,586
0
0
null
null
null
null
UTF-8
Lean
false
false
1,963
lean
/- Copyright (c) 2016 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import .utils open monad expr tactic namespace super meta def count_var_occs : unsigned → expr → ℕ | k (var k') := if k = k' then 1 else 0 | _ (sort _) := 0 | _ (const _ _) := 0 | _ (mvar _ _) := 0 | k (local_const _ _ _ t) := count_var_occs k t | k (app a b) := count_var_occs k a + count_var_occs k b | k (lam _ _ t b) := count_var_occs k t + count_var_occs k^.succ b | k (pi _ _ t b) := count_var_occs k t + count_var_occs k^.succ b | k (elet _ t v b) := count_var_occs k t + count_var_occs k v + count_var_occs k^.succ b | _ (macro _ _ _) := 0 -- TODO(gabriel) -- TODO(gabriel): rewrite using conversions meta def trim : expr → tactic expr | (app (lam n m d b) arg) := if has_var b = ff ∨ count_var_occs 0 b ≤ 1 then trim (instantiate_var b arg) else lift₂ app (trim (lam n m d b)) (trim arg) | (app a b) := lift₂ app (trim a) (trim b) | (lam n m d b) := do x ← mk_local' `x m d, b' ← trim (instantiate_var b x), return $ lam n m d (abstract_local b' x^.local_uniq_name) | (elet n t v b) := if has_var b then do x ← mk_local_def `x t, b' ← trim (instantiate_var b x), return $ elet n t v (abstract_local b' x^.local_uniq_name) else trim b | e := return e -- iterate trim until convergence meta def trim' : expr → tactic expr | e := do e' ← trim e, if e =ₐ e' then return e else trim' e' open tactic meta def with_trim {α} (tac : tactic α) : tactic α := do gs ← get_goals, match gs with | (g::gs) := do g' ← infer_type g >>= mk_meta_var, set_goals [g'], r ← tac, now, set_goals (g::gs), instantiate_mvars g' >>= trim' >>= exact, return r | [] := fail "no goal" end end super
4d1f3b5ed34302e40630268612ee375513a5cc50
6b45072eb2b3db3ecaace2a7a0241ce81f815787
/algebra/order.lean
98e4431f6171de1fe5b3c62a19c608d28d12e25c
[]
no_license
avigad/library_dev
27b47257382667b5eb7e6476c4f5b0d685dd3ddc
9d8ac7c7798ca550874e90fed585caad030bbfac
refs/heads/master
1,610,452,468,791
1,500,712,839,000
1,500,713,478,000
69,311,142
1
0
null
1,474,942,903,000
1,474,942,902,000
null
UTF-8
Lean
false
false
2,673
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad -/ open eq function /- TODO: automatic construction of dual definitions / theorems -/ universes u v w variables {α : Type u} {β : Type v} {γ : Type w} section monotone variables [weak_order α] [weak_order β] [weak_order γ] def monotone (f : α → β) := ∀{{a b}}, a ≤ b → f a ≤ f b lemma monotone_id : @monotone α α _ _ id := assume x y h, h lemma monotone_const {b : β} : monotone (λ(a:α), b) := assume x y h, le_refl b lemma monotone_comp {f : α → β} {g : β → γ} (m_f : monotone f) (m_g : monotone g) : monotone (g ∘ f) := assume a b h, m_g (m_f h) end monotone section variable [decidable_linear_order α] variables {a b c d : α} open decidable theorem le_max_left_iff_true (a b : α) : a ≤ max a b ↔ true := iff_true_intro (le_max_left a b) theorem le_max_right_iff_true (a b : α) : b ≤ max a b ↔ true := iff_true_intro (le_max_right a b) theorem min_right_comm (a b c : α) : min (min a b) c = min (min a c) b := right_comm min min_comm min_assoc a b c theorem max.left_comm (a b c : α) : max a (max b c) = max b (max a c) := left_comm max max_comm max_assoc a b c theorem max.right_comm (a b c : α) : max (max a b) c = max (max a c) b := right_comm max max_comm max_assoc a b c end /- order instances -/ instance weak_order_fun [weak_order β] : weak_order (α → β) := { le := λx y, ∀b, x b ≤ y b, le_refl := λf b, le_refl (f b), le_trans := λf g h h1 h2 b, le_trans (h1 b) (h2 b), le_antisymm := λf g h1 h2, funext (λb, le_antisymm (h1 b) (h2 b)) } definition weak_order_dual (wo : weak_order α) : weak_order α := { le := λx y, y ≤ x, le_refl := le_refl, le_trans := assume a b c h₁ h₂, le_trans h₂ h₁, le_antisymm := assume a b h₁ h₂, le_antisymm h₂ h₁ } lemma le_dual_eq_le {α : Type} (wo : weak_order α) (a b : α) : @has_le.le _ (@weak_order.to_has_le _ (weak_order_dual wo)) a b = @has_le.le _ (@weak_order.to_has_le _ wo) b a := rfl lemma comp_le_comp_left_of_monotone [weak_order α] [weak_order β] [weak_order γ] {f : β → α} {g h : γ → β} (m_f : monotone f) (le_gh : g ≤ h) : has_le.le.{max w u} (f ∘ g) (f ∘ h) := assume x, m_f (le_gh x) section monotone variables [weak_order α] [weak_order γ] lemma monotone_lam {f : α → β → γ} (m : ∀b, monotone (λa, f a b)) : monotone f := assume a a' h b, m b h lemma monotone_app (f : β → α → γ) (b : β) (m : monotone (λa b, f b a)) : monotone (f b) := assume a a' h, m h b end monotone
ff81fe567aee7b04923f9219d06687652890597c
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/record10.lean
8611ce44656b5385acf9edb5ca1dbc2706cf9144
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
564
lean
structure [class] semigroup (A : Type) extends has_mul A := (assoc : ∀ a b c, mul (mul a b) c = mul a (mul b c)) print prefix semigroup print "=======================" structure [class] has_two_muls (A : Type) extends has_mul A renaming mul→mul1, private has_mul A renaming mul→mul2 print prefix has_two_muls print "=======================" structure [class] another_two_muls (A : Type) extends has_mul A renaming mul→mul1, has_mul A renaming mul→mul2
2b6674258eb94ecd2fe4337f1e973db482f71e92
1a61aba1b67cddccce19532a9596efe44be4285f
/hott/types/sigma.hlean
3ec7e10f1e3f18203bb96593ed92305c9f5992fe
[ "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
15,412
hlean
/- Copyright (c) 2014-15 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Partially ported from Coq HoTT Theorems about sigma-types (dependent sums) -/ import types.prod open eq sigma sigma.ops equiv is_equiv function is_trunc namespace sigma variables {A A' : Type} {B : A → Type} {B' : A' → Type} {C : Πa, B a → Type} {D : Πa b, C a b → Type} {a a' a'' : A} {b b₁ b₂ : B a} {b' : B a'} {b'' : B a''} {u v w : Σa, B a} definition destruct := @sigma.cases_on /- Paths in a sigma-type -/ protected definition eta : Π (u : Σa, B a), ⟨u.1 , u.2⟩ = u | eta ⟨u₁, u₂⟩ := idp definition eta2 : Π (u : Σa b, C a b), ⟨u.1, u.2.1, u.2.2⟩ = u | eta2 ⟨u₁, u₂, u₃⟩ := idp definition eta3 : Π (u : Σa b c, D a b c), ⟨u.1, u.2.1, u.2.2.1, u.2.2.2⟩ = u | eta3 ⟨u₁, u₂, u₃, u₄⟩ := idp definition dpair_eq_dpair (p : a = a') (q : b =[p] b') : ⟨a, b⟩ = ⟨a', b'⟩ := by induction q; reflexivity definition sigma_eq (p : u.1 = v.1) (q : u.2 =[p] v.2) : u = v := by induction u; induction v; exact (dpair_eq_dpair p q) definition eq_pr1 [unfold 5] (p : u = v) : u.1 = v.1 := ap pr1 p postfix `..1`:(max+1) := eq_pr1 definition eq_pr2 (p : u = v) : u.2 =[p..1] v.2 := by induction p; exact idpo postfix `..2`:(max+1) := eq_pr2 definition dpair_sigma_eq (p : u.1 = v.1) (q : u.2 =[p] v.2) : ⟨(sigma_eq p q)..1, (sigma_eq p q)..2⟩ = ⟨p, q⟩ := by induction u; induction v;esimp at *;induction q;esimp definition sigma_eq_pr1 (p : u.1 = v.1) (q : u.2 =[p] v.2) : (sigma_eq p q)..1 = p := (dpair_sigma_eq p q)..1 definition sigma_eq_pr2 (p : u.1 = v.1) (q : u.2 =[p] v.2) : (sigma_eq p q)..2 =[sigma_eq_pr1 p q] q := (dpair_sigma_eq p q)..2 definition sigma_eq_eta (p : u = v) : sigma_eq (p..1) (p..2) = p := by induction p; induction u; reflexivity definition tr_pr1_sigma_eq {B' : A → Type} (p : u.1 = v.1) (q : u.2 =[p] v.2) : transport (λx, B' x.1) (sigma_eq p q) = transport B' p := by induction u; induction v; esimp at *;induction q; reflexivity /- the uncurried version of sigma_eq. We will prove that this is an equivalence -/ definition sigma_eq_unc : Π (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2), u = v | sigma_eq_unc ⟨pq₁, pq₂⟩ := sigma_eq pq₁ pq₂ definition dpair_sigma_eq_unc : Π (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2), ⟨(sigma_eq_unc pq)..1, (sigma_eq_unc pq)..2⟩ = pq | dpair_sigma_eq_unc ⟨pq₁, pq₂⟩ := dpair_sigma_eq pq₁ pq₂ definition sigma_eq_pr1_unc (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2) : (sigma_eq_unc pq)..1 = pq.1 := (dpair_sigma_eq_unc pq)..1 definition sigma_eq_pr2_unc (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2) : (sigma_eq_unc pq)..2 =[sigma_eq_pr1_unc pq] pq.2 := (dpair_sigma_eq_unc pq)..2 definition sigma_eq_eta_unc (p : u = v) : sigma_eq_unc ⟨p..1, p..2⟩ = p := sigma_eq_eta p definition tr_sigma_eq_pr1_unc {B' : A → Type} (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2) : transport (λx, B' x.1) (@sigma_eq_unc A B u v pq) = transport B' pq.1 := destruct pq tr_pr1_sigma_eq definition is_equiv_sigma_eq [instance] (u v : Σa, B a) : is_equiv (@sigma_eq_unc A B u v) := adjointify sigma_eq_unc (λp, ⟨p..1, p..2⟩) sigma_eq_eta_unc dpair_sigma_eq_unc definition sigma_eq_equiv (u v : Σa, B a) : (u = v) ≃ (Σ(p : u.1 = v.1), u.2 =[p] v.2) := (equiv.mk sigma_eq_unc _)⁻¹ᵉ definition dpair_eq_dpair_con (p1 : a = a' ) (q1 : b =[p1] b' ) (p2 : a' = a'') (q2 : b' =[p2] b'') : dpair_eq_dpair (p1 ⬝ p2) (q1 ⬝o q2) = dpair_eq_dpair p1 q1 ⬝ dpair_eq_dpair p2 q2 := by induction q1; induction q2; reflexivity definition sigma_eq_con (p1 : u.1 = v.1) (q1 : u.2 =[p1] v.2) (p2 : v.1 = w.1) (q2 : v.2 =[p2] w.2) : sigma_eq (p1 ⬝ p2) (q1 ⬝o q2) = sigma_eq p1 q1 ⬝ sigma_eq p2 q2 := by induction u; induction v; induction w; apply dpair_eq_dpair_con local attribute dpair_eq_dpair [reducible] definition dpair_eq_dpair_con_idp (p : a = a') (q : b =[p] b') : dpair_eq_dpair p q = dpair_eq_dpair p !pathover_tr ⬝ dpair_eq_dpair idp (pathover_idp_of_eq (tr_eq_of_pathover q)) := by induction q; reflexivity /- eq_pr1 commutes with the groupoid structure. -/ definition eq_pr1_idp (u : Σa, B a) : (refl u) ..1 = refl (u.1) := idp definition eq_pr1_con (p : u = v) (q : v = w) : (p ⬝ q) ..1 = (p..1) ⬝ (q..1) := !ap_con definition eq_pr1_inv (p : u = v) : p⁻¹ ..1 = (p..1)⁻¹ := !ap_inv /- Applying dpair to one argument is the same as dpair_eq_dpair with reflexivity in the first place. -/ definition ap_dpair (q : b₁ = b₂) : ap (sigma.mk a) q = dpair_eq_dpair idp (pathover_idp_of_eq q) := by induction q; reflexivity /- Dependent transport is the same as transport along a sigma_eq. -/ definition transportD_eq_transport (p : a = a') (c : C a b) : p ▸D c = transport (λu, C (u.1) (u.2)) (dpair_eq_dpair p !pathover_tr) c := by induction p; reflexivity definition sigma_eq_eq_sigma_eq {p1 q1 : a = a'} {p2 : b =[p1] b'} {q2 : b =[q1] b'} (r : p1 = q1) (s : p2 =[r] q2) : sigma_eq p1 p2 = sigma_eq q1 q2 := by induction s; reflexivity /- A path between paths in a total space is commonly shown component wise. -/ definition sigma_eq2 {p q : u = v} (r : p..1 = q..1) (s : p..2 =[r] q..2) : p = q := begin revert q r s, induction p, induction u with u1 u2, intro q r s, transitivity sigma_eq q..1 q..2, apply sigma_eq_eq_sigma_eq r s, apply sigma_eq_eta, end definition sigma_eq2_unc {p q : u = v} (rs : Σ(r : p..1 = q..1), p..2 =[r] q..2) : p = q := destruct rs sigma_eq2 /- Transport -/ /- The concrete description of transport in sigmas (and also pis) is rather trickier than in the other types. In particular, these cannot be described just in terms of transport in simpler types; they require also the dependent transport [transportD]. In particular, this indicates why `transport` alone cannot be fully defined by induction on the structure of types, although Id-elim/transportD can be (cf. Observational Type Theory). A more thorough set of lemmas, along the lines of the present ones but dealing with Id-elim rather than just transport, might be nice to have eventually? -/ definition sigma_transport (p : a = a') (bc : Σ(b : B a), C a b) : p ▸ bc = ⟨p ▸ bc.1, p ▸D bc.2⟩ := by induction p; induction bc; reflexivity /- The special case when the second variable doesn't depend on the first is simpler. -/ definition sigma_transport_nondep {B : Type} {C : A → B → Type} (p : a = a') (bc : Σ(b : B), C a b) : p ▸ bc = ⟨bc.1, p ▸ bc.2⟩ := by induction p; induction bc; reflexivity /- Or if the second variable contains a first component that doesn't depend on the first. -/ definition sigma_transport2_nondep {C : A → Type} {D : Π a:A, B a → C a → Type} (p : a = a') (bcd : Σ(b : B a) (c : C a), D a b c) : p ▸ bcd = ⟨p ▸ bcd.1, p ▸ bcd.2.1, p ▸D2 bcd.2.2⟩ := begin induction p, induction bcd with b cd, induction cd, reflexivity end /- Pathovers -/ definition etao (p : a = a') (bc : Σ(b : B a), C a b) : bc =[p] ⟨p ▸ bc.1, p ▸D bc.2⟩ := by induction p; induction bc; apply idpo definition sigma_pathover (p : a = a') (u : Σ(b : B a), C a b) (v : Σ(b : B a'), C a' b) (r : u.1 =[p] v.1) (s : u.2 =[apo011 C p r] v.2) : u =[p] v := begin induction u, induction v, esimp at *, induction r, esimp [apo011] at s, induction s using idp_rec_on, apply idpo end /- TODO: * define the projections from the type u =[p] v * show that the uncurried version of sigma_pathover is an equivalence -/ /- Functorial action -/ variables (f : A → A') (g : Πa, B a → B' (f a)) definition sigma_functor [unfold 7] (u : Σa, B a) : Σa', B' a' := ⟨f u.1, g u.1 u.2⟩ /- Equivalences -/ definition is_equiv_sigma_functor [H1 : is_equiv f] [H2 : Π a, is_equiv (g a)] : is_equiv (sigma_functor f g) := adjointify (sigma_functor f g) (sigma_functor f⁻¹ (λ(a' : A') (b' : B' a'), ((g (f⁻¹ a'))⁻¹ (transport B' (right_inv f a')⁻¹ b')))) begin intro u', induction u' with a' b', apply sigma_eq (right_inv f a'), rewrite [▸*,right_inv (g (f⁻¹ a')),▸*], apply tr_pathover end begin intro u, induction u with a b, apply (sigma_eq (left_inv f a)), apply pathover_of_tr_eq, rewrite [▸*,adj f,-(fn_tr_eq_tr_fn (left_inv f a) (λ a, (g a)⁻¹)), ▸*,tr_compose B' f,tr_inv_tr,left_inv] end definition sigma_equiv_sigma_of_is_equiv [H1 : is_equiv f] [H2 : Π a, is_equiv (g a)] : (Σa, B a) ≃ (Σa', B' a') := equiv.mk (sigma_functor f g) !is_equiv_sigma_functor definition sigma_equiv_sigma (Hf : A ≃ A') (Hg : Π a, B a ≃ B' (to_fun Hf a)) : (Σa, B a) ≃ (Σa', B' a') := sigma_equiv_sigma_of_is_equiv (to_fun Hf) (λ a, to_fun (Hg a)) definition sigma_equiv_sigma_id {B' : A → Type} (Hg : Π a, B a ≃ B' a) : (Σa, B a) ≃ Σa, B' a := sigma_equiv_sigma equiv.refl Hg definition ap_sigma_functor_eq_dpair (p : a = a') (q : b =[p] b') : ap (sigma_functor f g) (sigma_eq p q) = sigma_eq (ap f p) (pathover.rec_on q idpo) := by induction q; reflexivity -- definition ap_sigma_functor_eq (p : u.1 = v.1) (q : u.2 =[p] v.2) -- : ap (sigma_functor f g) (sigma_eq p q) = -- sigma_eq (ap f p) -- ((tr_compose B' f p (g u.1 u.2))⁻¹ ⬝ (fn_tr_eq_tr_fn p g u.2)⁻¹ ⬝ ap (g v.1) q) := -- by induction u; induction v; apply ap_sigma_functor_eq_dpair /- definition 3.11.9(i): Summing up a contractible family of types does nothing. -/ definition is_equiv_pr1 [instance] (B : A → Type) [H : Π a, is_contr (B a)] : is_equiv (@pr1 A B) := adjointify pr1 (λa, ⟨a, !center⟩) (λa, idp) (λu, sigma_eq idp (pathover_idp_of_eq !center_eq)) definition sigma_equiv_of_is_contr_pr2 [H : Π a, is_contr (B a)] : (Σa, B a) ≃ A := equiv.mk pr1 _ /- definition 3.11.9(ii): Dually, summing up over a contractible type does nothing. -/ definition sigma_equiv_of_is_contr_pr1 (B : A → Type) [H : is_contr A] : (Σa, B a) ≃ B (center A) := equiv.mk _ (adjointify (λu, (center_eq u.1)⁻¹ ▸ u.2) (λb, ⟨!center, b⟩) (λb, ap (λx, x ▸ b) !hprop_eq_of_is_contr) (λu, sigma_eq !center_eq !tr_pathover)) /- Associativity -/ --this proof is harder than in Coq because we don't have eta definitionally for sigma definition sigma_assoc_equiv (C : (Σa, B a) → Type) : (Σa b, C ⟨a, b⟩) ≃ (Σu, C u) := equiv.mk _ (adjointify (λav, ⟨⟨av.1, av.2.1⟩, av.2.2⟩) (λuc, ⟨uc.1.1, uc.1.2, !sigma.eta⁻¹ ▸ uc.2⟩) begin intro uc, induction uc with u c, induction u, reflexivity end begin intro av, induction av with a v, induction v, reflexivity end) open prod prod.ops definition assoc_equiv_prod (C : (A × A') → Type) : (Σa a', C (a,a')) ≃ (Σu, C u) := equiv.mk _ (adjointify (λav, ⟨(av.1, av.2.1), av.2.2⟩) (λuc, ⟨pr₁ (uc.1), pr₂ (uc.1), !prod.eta⁻¹ ▸ uc.2⟩) proof (λuc, destruct uc (λu, prod.destruct u (λa b c, idp))) qed proof (λav, destruct av (λa v, destruct v (λb c, idp))) qed) /- Symmetry -/ definition comm_equiv_unc (C : A × A' → Type) : (Σa a', C (a, a')) ≃ (Σa' a, C (a, a')) := calc (Σa a', C (a, a')) ≃ Σu, C u : assoc_equiv_prod ... ≃ Σv, C (flip v) : sigma_equiv_sigma !prod_comm_equiv (λu, prod.destruct u (λa a', equiv.refl)) ... ≃ (Σa' a, C (a, a')) : assoc_equiv_prod definition sigma_comm_equiv (C : A → A' → Type) : (Σa a', C a a') ≃ (Σa' a, C a a') := comm_equiv_unc (λu, C (prod.pr1 u) (prod.pr2 u)) definition equiv_prod (A B : Type) : (Σ(a : A), B) ≃ A × B := equiv.mk _ (adjointify (λs, (s.1, s.2)) (λp, ⟨pr₁ p, pr₂ p⟩) proof (λp, prod.destruct p (λa b, idp)) qed proof (λs, destruct s (λa b, idp)) qed) definition comm_equiv_nondep (A B : Type) : (Σ(a : A), B) ≃ Σ(b : B), A := calc (Σ(a : A), B) ≃ A × B : equiv_prod ... ≃ B × A : prod_comm_equiv ... ≃ Σ(b : B), A : equiv_prod /- ** Universal mapping properties -/ /- *** The positive universal property. -/ section definition is_equiv_sigma_rec [instance] (C : (Σa, B a) → Type) : is_equiv (sigma.rec : (Πa b, C ⟨a, b⟩) → Πab, C ab) := adjointify _ (λ g a b, g ⟨a, b⟩) (λ g, proof eq_of_homotopy (λu, destruct u (λa b, idp)) qed) (λ f, refl f) definition equiv_sigma_rec (C : (Σa, B a) → Type) : (Π(a : A) (b: B a), C ⟨a, b⟩) ≃ (Πxy, C xy) := equiv.mk sigma.rec _ /- *** The negative universal property. -/ protected definition coind_unc (fg : Σ(f : Πa, B a), Πa, C a (f a)) (a : A) : Σ(b : B a), C a b := ⟨fg.1 a, fg.2 a⟩ protected definition coind (f : Π a, B a) (g : Π a, C a (f a)) (a : A) : Σ(b : B a), C a b := sigma.coind_unc ⟨f, g⟩ a --is the instance below dangerous? --in Coq this can be done without function extensionality definition is_equiv_coind [instance] (C : Πa, B a → Type) : is_equiv (@sigma.coind_unc _ _ C) := adjointify _ (λ h, ⟨λa, (h a).1, λa, (h a).2⟩) (λ h, proof eq_of_homotopy (λu, !sigma.eta) qed) (λfg, destruct fg (λ(f : Π (a : A), B a) (g : Π (x : A), C x (f x)), proof idp qed)) definition sigma_pi_equiv_pi_sigma : (Σ(f : Πa, B a), Πa, C a (f a)) ≃ (Πa, Σb, C a b) := equiv.mk sigma.coind_unc _ end /- Subtypes (sigma types whose second components are hprops) -/ definition subtype [reducible] {A : Type} (P : A → Type) [H : Πa, is_hprop (P a)] := Σ(a : A), P a notation [parsing-only] `{` binder `|` r:(scoped:1 P, subtype P) `}` := r /- To prove equality in a subtype, we only need equality of the first component. -/ definition subtype_eq [H : Πa, is_hprop (B a)] (u v : {a | B a}) : u.1 = v.1 → u = v := sigma_eq_unc ∘ inv pr1 definition is_equiv_subtype_eq [H : Πa, is_hprop (B a)] (u v : {a | B a}) : is_equiv (subtype_eq u v) := !is_equiv_compose local attribute is_equiv_subtype_eq [instance] definition equiv_subtype [H : Πa, is_hprop (B a)] (u v : {a | B a}) : (u.1 = v.1) ≃ (u = v) := equiv.mk !subtype_eq _ /- truncatedness -/ definition is_trunc_sigma (B : A → Type) (n : trunc_index) [HA : is_trunc n A] [HB : Πa, is_trunc n (B a)] : is_trunc n (Σa, B a) := begin revert A B HA HB, induction n with n IH, { intro A B HA HB, fapply is_trunc_equiv_closed_rev, apply sigma_equiv_of_is_contr_pr1}, { intro A B HA HB, apply is_trunc_succ_intro, intro u v, apply is_trunc_equiv_closed_rev, apply sigma_eq_equiv, exact IH _ _ _ _} end end sigma attribute sigma.is_trunc_sigma [instance] [priority 1490]
56bb8c1fd43383461fe6b366a115a558d1698b7f
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/ring_theory/witt_vector/frobenius.lean
6c21c3444b937426ecab1353d5590b9669ddd41d
[ "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
13,398
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import data.nat.multiplicity import ring_theory.witt_vector.basic import ring_theory.witt_vector.is_poly /-! ## The Frobenius operator If `R` has characteristic `p`, then there is a ring endomorphism `frobenius R p` that raises `r : R` to the power `p`. By applying `witt_vector.map` to `frobenius R p`, we obtain a ring endomorphism `𝕎 R →+* 𝕎 R`. It turns out that this endomorphism can be described by polynomials over `ℤ` that do not depend on `R` or the fact that it has characteristic `p`. In this way, we obtain a Frobenius endomorphism `witt_vector.frobenius_fun : 𝕎 R → 𝕎 R` for every commutative ring `R`. Unfortunately, the aforementioned polynomials can not be obtained using the machinery of `witt_structure_int` that was developed in `structure_polynomial.lean`. We therefore have to define the polynomials by hand, and check that they have the required property. In case `R` has characteristic `p`, we show in `frobenius_fun_eq_map_frobenius` that `witt_vector.frobenius_fun` is equal to `witt_vector.map (frobenius R p)`. ### Main definitions and results * `frobenius_poly`: the polynomials that describe the coefficients of `frobenius_fun`; * `frobenius_fun`: the Frobenius endomorphism on Witt vectors; * `frobenius_fun_is_poly`: the tautological assertion that Frobenius is a polynomial function; * `frobenius_fun_eq_map_frobenius`: the fact that in characteristic `p`, Frobenius is equal to `witt_vector.map (frobenius R p)`. TODO: Show that `witt_vector.frobenius_fun` is a ring homomorphism, and bundle it into `witt_vector.frobenius`. ## References * [Hazewinkel, *Witt Vectors*][Haze09] * [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21] -/ namespace witt_vector variables {p : ℕ} {R S : Type*} [hp : fact p.prime] [comm_ring R] [comm_ring S] local notation `𝕎` := witt_vector p -- type as `\bbW` noncomputable theory open mv_polynomial finset open_locale big_operators variables (p) include hp /-- The rational polynomials that give the coefficients of `frobenius x`, in terms of the coefficients of `x`. These polynomials actually have integral coefficients, see `frobenius_poly` and `map_frobenius_poly`. -/ def frobenius_poly_rat (n : ℕ) : mv_polynomial ℕ ℚ := bind₁ (witt_polynomial p ℚ ∘ λ n, n + 1) (X_in_terms_of_W p ℚ n) lemma bind₁_frobenius_poly_rat_witt_polynomial (n : ℕ) : bind₁ (frobenius_poly_rat p) (witt_polynomial p ℚ n) = (witt_polynomial p ℚ (n+1)) := begin delta frobenius_poly_rat, rw [← bind₁_bind₁, bind₁_X_in_terms_of_W_witt_polynomial, bind₁_X_right], end /-- An auxiliary definition, to avoid an excessive amount of finiteness proofs for `multiplicity p n`. -/ private def pnat_multiplicity (n : ℕ+) : ℕ := (multiplicity p n).get $ multiplicity.finite_nat_iff.mpr $ ⟨ne_of_gt hp.1.one_lt, n.2⟩ local notation `v` := pnat_multiplicity /-- An auxiliary polynomial over the integers, that satisfies `p * (frobenius_poly_aux p n) + X n ^ p = frobenius_poly p n`. This makes it easy to show that `frobenius_poly p n` is congruent to `X n ^ p` modulo `p`. -/ noncomputable def frobenius_poly_aux : ℕ → mv_polynomial ℕ ℤ | n := X (n + 1) - ∑ i : fin n, have _ := i.is_lt, ∑ j in range (p ^ (n - i)), (X i ^ p) ^ (p ^ (n - i) - (j + 1)) * (frobenius_poly_aux i) ^ (j + 1) * C ↑((p ^ (n - i)).choose (j + 1) / (p ^ (n - i - v p ⟨j + 1, nat.succ_pos j⟩)) * ↑p ^ (j - v p ⟨j + 1, nat.succ_pos j⟩) : ℕ) lemma frobenius_poly_aux_eq (n : ℕ) : frobenius_poly_aux p n = X (n + 1) - ∑ i in range n, ∑ j in range (p ^ (n - i)), (X i ^ p) ^ (p ^ (n - i) - (j + 1)) * (frobenius_poly_aux p i) ^ (j + 1) * C ↑((p ^ (n - i)).choose (j + 1) / (p ^ (n - i - v p ⟨j + 1, nat.succ_pos j⟩)) * ↑p ^ (j - v p ⟨j + 1, nat.succ_pos j⟩) : ℕ) := by { rw [frobenius_poly_aux, ← fin.sum_univ_eq_sum_range] } /-- The polynomials that give the coefficients of `frobenius x`, in terms of the coefficients of `x`. -/ def frobenius_poly (n : ℕ) : mv_polynomial ℕ ℤ := X n ^ p + C ↑p * (frobenius_poly_aux p n) /- Our next goal is to prove ``` lemma map_frobenius_poly (n : ℕ) : mv_polynomial.map (int.cast_ring_hom ℚ) (frobenius_poly p n) = frobenius_poly_rat p n ``` This lemma has a rather long proof, but it mostly boils down to applying induction, and then using the following two key facts at the right point. -/ /-- A key divisibility fact for the proof of `witt_vector.map_frobenius_poly`. -/ lemma map_frobenius_poly.key₁ (n j : ℕ) (hj : j < p ^ (n)) : p ^ (n - v p ⟨j + 1, j.succ_pos⟩) ∣ (p ^ n).choose (j + 1) := begin apply multiplicity.pow_dvd_of_le_multiplicity, have aux : (multiplicity p ((p ^ n).choose (j + 1))).dom, { rw [← multiplicity.finite_iff_dom, multiplicity.finite_nat_iff], exact ⟨hp.1.ne_one, nat.choose_pos hj⟩, }, rw [← enat.coe_get aux, enat.coe_le_coe, sub_le_iff_left, ← enat.coe_le_coe, nat.cast_add, pnat_multiplicity, enat.coe_get, enat.coe_get, add_comm], exact (hp.1.multiplicity_choose_prime_pow hj j.succ_pos).ge, end /-- A key numerical identity needed for the proof of `witt_vector.map_frobenius_poly`. -/ lemma map_frobenius_poly.key₂ {n i j : ℕ} (hi : i < n) (hj : j < p ^ (n - i)) : j - (v p ⟨j + 1, j.succ_pos⟩) + n = i + j + (n - i - v p ⟨j + 1, j.succ_pos⟩) := begin generalize h : (v p ⟨j + 1, j.succ_pos⟩) = m, suffices : m ≤ n - i ∧ m ≤ j, { rw [←nat.sub_add_comm this.2, add_comm i j, nat.add_sub_assoc (this.1.trans (nat.sub_le n i)), add_assoc, nat.sub.right_comm, add_comm i, nat.sub_add_cancel (le_sub_of_add_le_right' ((le_sub_iff_left hi.le).mp this.1))] }, split, { rw [← h, ← enat.coe_le_coe, pnat_multiplicity, enat.coe_get, ← hp.1.multiplicity_choose_prime_pow hj j.succ_pos], apply le_add_left, refl }, { obtain ⟨c, hc⟩ : p ^ m ∣ j + 1, { rw [← h], exact multiplicity.pow_multiplicity_dvd _, }, obtain ⟨c, rfl⟩ : ∃ k : ℕ, c = k + 1, { apply nat.exists_eq_succ_of_ne_zero, rintro rfl, simpa only using hc }, rw [mul_add, mul_one] at hc, apply nat.le_of_lt_succ, calc m < p ^ m : nat.lt_pow_self hp.1.one_lt m ... ≤ j + 1 : by { rw ← sub_eq_of_eq_add_rev hc, apply nat.sub_le } } end lemma map_frobenius_poly (n : ℕ) : mv_polynomial.map (int.cast_ring_hom ℚ) (frobenius_poly p n) = frobenius_poly_rat p n := begin rw [frobenius_poly, ring_hom.map_add, ring_hom.map_mul, ring_hom.map_pow, map_C, map_X, ring_hom.eq_int_cast, int.cast_coe_nat, frobenius_poly_rat], apply nat.strong_induction_on n, clear n, intros n IH, rw [X_in_terms_of_W_eq], simp only [alg_hom.map_sum, alg_hom.map_sub, alg_hom.map_mul, alg_hom.map_pow, bind₁_C_right], have h1 : (↑p ^ n) * (⅟ (↑p : ℚ) ^ n) = 1 := by rw [←mul_pow, mul_inv_of_self, one_pow], rw [bind₁_X_right, function.comp_app, witt_polynomial_eq_sum_C_mul_X_pow, sum_range_succ, sum_range_succ, nat.sub_self, nat.add_sub_cancel_left, pow_zero, pow_one, pow_one, sub_mul, add_mul, add_mul, mul_right_comm, mul_right_comm (C (↑p ^ (n + 1))), ←C_mul, ←C_mul, pow_succ, mul_assoc ↑p (↑p ^ n), h1, mul_one, C_1, one_mul, add_comm _ (X n ^ p), add_assoc, ←add_sub, add_right_inj, frobenius_poly_aux_eq, ring_hom.map_sub, map_X, mul_sub, sub_eq_add_neg, add_comm _ (C ↑p * X (n + 1)), ←add_sub, add_right_inj, neg_eq_iff_neg_eq, neg_sub], simp only [ring_hom.map_sum, mul_sum, sum_mul, ←sum_sub_distrib], apply sum_congr rfl, intros i hi, rw mem_range at hi, rw [← IH i hi], clear IH, rw [add_comm (X i ^ p), add_pow, sum_range_succ', pow_zero, nat.sub_zero, nat.choose_zero_right, one_mul, nat.cast_one, mul_one, mul_add, add_mul, nat.succ_sub (le_of_lt hi), nat.succ_eq_add_one (n - i), pow_succ, pow_mul, add_sub_cancel, mul_sum, sum_mul], apply sum_congr rfl, intros j hj, rw mem_range at hj, rw [ring_hom.map_mul, ring_hom.map_mul, ring_hom.map_pow, ring_hom.map_pow, ring_hom.map_pow, ring_hom.map_pow, ring_hom.map_pow, map_C, map_X, mul_pow], rw [mul_comm (C ↑p ^ i), mul_comm _ ((X i ^ p) ^ _), mul_comm (C ↑p ^ (j + 1)), mul_comm (C ↑p)], simp only [mul_assoc], apply congr_arg, apply congr_arg, rw [←C_eq_coe_nat], simp only [←ring_hom.map_pow, ←C_mul], rw C_inj, simp only [inv_of_eq_inv, ring_hom.eq_int_cast, inv_pow₀, int.cast_coe_nat, nat.cast_mul], rw [rat.coe_nat_div _ _ (map_frobenius_poly.key₁ p (n - i) j hj)], simp only [nat.cast_pow, pow_add, pow_one], suffices : ((p ^ (n - i)).choose (j + 1) * p ^ (j - v p ⟨j + 1, j.succ_pos⟩) * p * p ^ n : ℚ) = p ^ j * p * ((p ^ (n - i)).choose (j + 1) * p ^ i) * p ^ (n - i - v p ⟨j + 1, j.succ_pos⟩), { have aux : ∀ k : ℕ, (p ^ k : ℚ) ≠ 0, { intro, apply pow_ne_zero, exact_mod_cast hp.1.ne_zero }, simpa [aux, -one_div] with field_simps using this.symm }, rw [mul_comm _ (p : ℚ), mul_assoc, mul_assoc, ← pow_add, map_frobenius_poly.key₂ p hi hj], ring_exp end lemma frobenius_poly_zmod (n : ℕ) : mv_polynomial.map (int.cast_ring_hom (zmod p)) (frobenius_poly p n) = X n ^ p := begin rw [frobenius_poly, ring_hom.map_add, ring_hom.map_pow, ring_hom.map_mul, map_X, map_C], simp only [int.cast_coe_nat, add_zero, ring_hom.eq_int_cast, zmod.nat_cast_self, zero_mul, C_0], end @[simp] lemma bind₁_frobenius_poly_witt_polynomial (n : ℕ) : bind₁ (frobenius_poly p) (witt_polynomial p ℤ n) = (witt_polynomial p ℤ (n+1)) := begin apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective, simp only [map_bind₁, map_frobenius_poly, bind₁_frobenius_poly_rat_witt_polynomial, map_witt_polynomial], end variables {p} /-- `frobenius_fun` is the function underlying the ring endomorphism `frobenius : 𝕎 R →+* frobenius 𝕎 R`. -/ def frobenius_fun (x : 𝕎 R) : 𝕎 R := mk p $ λ n, mv_polynomial.aeval x.coeff (frobenius_poly p n) lemma coeff_frobenius_fun (x : 𝕎 R) (n : ℕ) : coeff (frobenius_fun x) n = mv_polynomial.aeval x.coeff (frobenius_poly p n) := by rw [frobenius_fun, coeff_mk] variables (p) /-- `frobenius_fun` is tautologically a polynomial function. See also `frobenius_is_poly`. -/ @[is_poly] lemma frobenius_fun_is_poly : is_poly p (λ R _Rcr, @frobenius_fun p R _ _Rcr) := ⟨⟨frobenius_poly p, by { introsI, funext n, apply coeff_frobenius_fun }⟩⟩ variable {p} @[ghost_simps] lemma ghost_component_frobenius_fun (n : ℕ) (x : 𝕎 R) : ghost_component n (frobenius_fun x) = ghost_component (n + 1) x := by simp only [ghost_component_apply, frobenius_fun, coeff_mk, ← bind₁_frobenius_poly_witt_polynomial, aeval_bind₁] /-- If `R` has characteristic `p`, then there is a ring endomorphism that raises `r : R` to the power `p`. By applying `witt_vector.map` to this endomorphism, we obtain a ring endomorphism `frobenius R p : 𝕎 R →+* 𝕎 R`. The underlying function of this morphism is `witt_vector.frobenius_fun`. -/ def frobenius : 𝕎 R →+* 𝕎 R := { to_fun := frobenius_fun, map_zero' := begin refine is_poly.ext ((frobenius_fun_is_poly p).comp (witt_vector.zero_is_poly)) ((witt_vector.zero_is_poly).comp (frobenius_fun_is_poly p)) _ _ 0, ghost_simp end, map_one' := begin refine is_poly.ext ((frobenius_fun_is_poly p).comp (witt_vector.one_is_poly)) ((witt_vector.one_is_poly).comp (frobenius_fun_is_poly p)) _ _ 0, ghost_simp end, map_add' := by ghost_calc _ _; ghost_simp, map_mul' := by ghost_calc _ _; ghost_simp } lemma coeff_frobenius (x : 𝕎 R) (n : ℕ) : coeff (frobenius x) n = mv_polynomial.aeval x.coeff (frobenius_poly p n) := coeff_frobenius_fun _ _ @[ghost_simps] lemma ghost_component_frobenius (n : ℕ) (x : 𝕎 R) : ghost_component n (frobenius x) = ghost_component (n + 1) x := ghost_component_frobenius_fun _ _ variables (p) /-- `frobenius` is tautologically a polynomial function. -/ @[is_poly] lemma frobenius_is_poly : is_poly p (λ R _Rcr, @frobenius p R _ _Rcr) := frobenius_fun_is_poly _ section char_p variables [char_p R p] @[simp] lemma coeff_frobenius_char_p (x : 𝕎 R) (n : ℕ) : coeff (frobenius x) n = (x.coeff n) ^ p := begin rw [coeff_frobenius], -- outline of the calculation, proofs follow below calc aeval (λ k, x.coeff k) (frobenius_poly p n) = aeval (λ k, x.coeff k) (mv_polynomial.map (int.cast_ring_hom (zmod p)) (frobenius_poly p n)) : _ ... = aeval (λ k, x.coeff k) (X n ^ p : mv_polynomial ℕ (zmod p)) : _ ... = (x.coeff n) ^ p : _, { conv_rhs { rw [aeval_eq_eval₂_hom, eval₂_hom_map_hom] }, apply eval₂_hom_congr (ring_hom.ext_int _ _) rfl rfl }, { rw frobenius_poly_zmod }, { rw [alg_hom.map_pow, aeval_X] } end lemma frobenius_eq_map_frobenius : @frobenius p R _ _ = map (_root_.frobenius R p) := begin ext x n, simp only [coeff_frobenius_char_p, map_coeff, frobenius_def], end @[simp] lemma frobenius_zmodp (x : 𝕎 (zmod p)) : (frobenius x) = x := by simp only [ext_iff, coeff_frobenius_char_p, zmod.pow_card, eq_self_iff_true, forall_const] end char_p end witt_vector
e1583a0aa60c0f53e9d9b97fa1d5a736cebb8f8b
8c433fccc85bae2c68bb34c8969dd5f6fe94945b
/src/mywork/practice_2.lean
20b72bc2c3dc283cf8e441cd42c41d0fd70c6e6e
[]
no_license
arh4uwe/cs2120f21
f2a844538124e9739b94bdb99a86221866c854ac
2cfb17c7b84a07a672b792fc85824f3de7c27e7a
refs/heads/main
1,691,945,681,056
1,632,839,762,000
1,632,839,762,000
400,231,555
0
0
null
null
null
null
UTF-8
Lean
false
false
5,275
lean
/- Prove the following simple logical conjectures. Give a formal and an English proof of each one. Your English language proofs should be complete in the sense that they identify all the axioms and/or theorems that you use. -/ example : true := true.intro example : false := _ -- trick question? why? example : ∀ (P : Prop), P ∨ P ↔ P := begin assume P, apply iff.intro _ _, -- forward assume porp, apply or.elim porp, -- left disjunct is true assume p, exact p, -- right disjunct is true assume p, exact p, -- backwards assume p, exact or.intro_left P p, end example : ∀ (P : Prop), P ∧ P ↔ P := begin assume P, apply iff.intro, assume p_p, apply and.elim_left p_p, assume p, apply and.intro, exact p, exact p, end example : ∀ (P Q : Prop), P ∨ Q ↔ Q ∨ P := begin assume P Q, apply iff.intro, assume porq, apply or.elim porq, assume p, apply or.intro_right, exact p, assume q, apply or.intro_left, exact q, assume qorp, apply or.elim qorp, assume q, apply or.intro_right, exact q, assume p, apply or.intro_left, exact p, end example : ∀ (P Q : Prop), P ∧ Q ↔ Q ∧ P := begin assume P Q, apply iff.intro, -- forwards assume pandq, apply and.intro, apply and.elim_right pandq, apply and.elim_left pandq, -- backwards assume qandp, apply and.intro, apply and.elim_right qandp, apply and.elim_left qandp, end example : ∀ (P Q R : Prop), P ∧ (Q ∨ R) ↔ (P ∧ Q) ∨ (P ∧ R) := begin assume P Q R, apply iff.intro, -- forwards assume p_and_qorr, have p : P := and.elim_left p_and_qorr, have qorr : Q ∨ R := and.elim_right p_and_qorr, apply or.elim qorr, -- if Q is true assume q, apply or.intro_left, apply and.intro, exact p, exact q, -- if R is true assume r, apply or.intro_right, apply and.intro, exact p, exact r, -- backwards assume pandq_or_pandr, apply or.elim pandq_or_pandr, -- if P ∧ Q is true assume pandq, have p := and.elim_left pandq, have q := and.elim_right pandq, apply and.intro, exact p, apply or.intro_left, exact q, -- if P ∧ R is true assume pandr, have p := and.elim_left pandr, have r := and.elim_right pandr, apply and.intro, exact p, apply or.intro_right, exact r, end example : ∀ (P Q R : Prop), P ∨ (Q ∧ R) ↔ (P ∨ Q) ∧ (P ∨ R) := begin assume P Q R, apply iff.intro, -- forwards assume p_or_qandr, apply or.elim p_or_qandr, -- if P is true assume p, apply and.intro, apply or.intro_left, exact p, apply or.intro_left, exact p, -- if Q ∧ R is true assume qandr, have q : Q := and.elim_left qandr, have r : R := and.elim_right qandr, apply and.intro, apply or.intro_right, exact q, apply or.intro_right, exact r, -- backwards assume porq_and_porr, have porq : P ∨ Q := and.elim_left porq_and_porr, have porr : P ∨ R := and.elim_right porq_and_porr, apply or.elim porq, -- if P is true assume p, apply or.intro_left, exact p, -- if Q is true assume q, apply or.elim porr, -- if P is true assume p, apply or.intro_left, exact p, -- if R is true assume r, apply or.intro_right, apply and.intro, exact q, exact r, end example : ∀ (P Q : Prop), P ∧ (P ∨ Q) ↔ P := begin assume P Q, apply iff.intro, -- forwards assume p_and_porq, exact and.elim_left p_and_porq, -- backwards, assume p, apply and.intro, exact p, apply or.intro_left, exact p, end example : ∀ (P Q : Prop), P ∨ (P ∧ Q) ↔ P := begin assume P Q, apply iff.intro, -- forwards assume p_or_pandq, apply or.elim p_or_pandq, -- if P is true assume p, exact p, -- if P ∧ Q is true assume pandq, exact and.elim_left pandq, -- backwards assume p, apply or.intro_left, exact p, end example : ∀ (P : Prop), P ∨ true ↔ true := begin assume P, apply iff.intro, -- forwards assume pandtrue, apply true.intro, -- backwards assume tr, apply or.intro_right, apply true.intro, end example : ∀ (P : Prop), P ∨ false ↔ P := begin assume P, apply iff.intro, -- forwards assume pandfalse, apply or.elim pandfalse, -- if P is true assume p, exact p, -- if false is true assume fa, apply false.elim fa, -- backwards assume p, apply or.intro_left, exact p, end example : ∀ (P : Prop), P ∧ true ↔ P := begin assume p, apply iff.intro, -- forwards assume pandtrue, exact and.elim_left pandtrue, -- backwards assume p, apply and.intro, exact p, apply true.intro, end example : ∀ (P : Prop), P ∧ false ↔ false := begin assume P, apply iff.intro, -- forwards assume pandfalse, exact and.elim_right pandfalse, -- backwards assume fa, apply false.elim, exact fa, end
bf71837f0039898c04ef7916f65f49806bd6b6f8
3bd26f8e9c7eeb6ae77ac4ba709b5b3c65b8d7cf
/axiom_int.lean
63c59eac5b6a8066c80c6817085f5e85581b9c54
[]
no_license
koba-e964/lean-work
afac5677efef6905fce29cac44f36f309c3bcd62
6ab0506b9bd4e5a2e1ba6312d4ac6bdaf6ae1594
refs/heads/master
1,659,773,150,740
1,659,289,453,000
1,659,289,453,000
100,273,655
0
0
null
null
null
null
UTF-8
Lean
false
false
4,570
lean
constant myint: Type namespace myint constant zero: myint constant succ: myint -> myint constant pred: myint -> myint constant add: myint -> myint -> myint constant mul: myint -> myint -> myint constant neg: myint -> myint noncomputable def has_add: has_add myint := ⟨add⟩ -- def has_add: has_add myint := sorry notation `O` := zero notation `S` := succ notation `P` := pred reserve infixl ` ++ `:65 reserve infixl ` ** `:67 notation a `++` b := add a b notation a `**` b := mul a b notation `neg` := neg axiom succ_of_pred: forall x, S (P x) = x axiom pred_of_succ: forall x, P (S x) = x axiom add_of_zero: forall x: myint, add x zero = x axiom add_of_succ: forall x y, add x (S y) = S (add x y) axiom add_of_pred: forall x y, add x (P y) = P (add x y) axiom neg_of_zero: neg zero = zero axiom neg_of_succ: forall x, neg (S x) = P (neg x) axiom neg_of_pred: forall x, neg (P x) = S (neg x) axiom mul_of_zero: forall x, x ** zero = zero axiom mul_of_succ: forall x y, x ** S y = add (x ** y) x axiom mul_of_pred: forall x y, x ** P y = add (x ** y) (neg x) axiom rec: forall Q: myint -> Prop, Q zero -> (forall x, Q x -> Q (S x)) -> (forall x, Q x -> Q (P x)) -> forall x, Q x @[simp] lemma succ_of_pred_simp: forall x, S (P x) = x := succ_of_pred @[simp] lemma pred_of_succ_simp: forall x, P (S x) = x := pred_of_succ @[simp] lemma add_of_zero_simp: forall x, x ++ O = x := add_of_zero @[simp] lemma add_of_succ_simp: forall x y, x ++ S y = S (x ++ y) := add_of_succ @[simp] lemma add_of_pred_simp: forall x y, x ++ P y = P (x ++ y) := add_of_pred @[simp] lemma neg_of_zero_simp: neg O = O := neg_of_zero @[simp] lemma neg_of_succ_simp: forall x, neg (S x) = P (neg x) := neg_of_succ @[simp] lemma neg_of_pred_simp: forall x, neg (P x) = S (neg x) := neg_of_pred @[simp] lemma mul_of_zero_simp: forall x, x ** O = O := mul_of_zero @[simp] lemma mul_of_succ_simp: forall x y, x ** S y = x ** y ++ x := mul_of_succ @[simp] lemma mul_of_pred_simp: forall x y, x ** P y = x ** y ++ neg x := mul_of_pred lemma add_comm: forall x y: myint, add x y = add y x := begin intro x, induction x with x' ih x' ih, intro y, induction y with y' ih2 y' ih2, reflexivity, rw add_of_succ, rw ih2, simp, rw add_of_pred, rw ih2, simp, intro y, induction y with y' ih2 y' ih2, rw add_of_zero, simp, rw <- ih O, simp, simp, rw ih2, rw <- ih, simp, rw ih, simp, rw ih2, rw <- ih, simp, rw ih, intro y, induction y with y' ih2 y' ih2, simp, rw <- ih, simp, simp [ih2], rw <- ih, rw <- ih, simp, simp [ih2], rw <- ih, rw <- ih, simp, end lemma add_assoc: forall x y z: myint, (x ++ y) ++ z = x ++ (y ++ z) := fun x y z, begin induction z with z' ih z' ih; { simp [ih] <|> simp }, end @[simp] lemma add_of_neg: forall x, x ++ neg x = O := fun x, begin induction x with _ a _ a, simp, simp, rw add_comm, simp, rw add_comm, rw a, simp, rw add_comm, simp, rw add_comm, rw a, end @[simp] lemma neg_of_neg {x: myint}: neg (neg x) = x := begin induction x with x' ih x' ih; { simp [ih] <|> simp }, end lemma neg_of_add {x y: myint}: neg x ++ neg y = neg (x ++ y) := begin induction y with y' ih y' ih; { simp [ih] <|> simp }, end lemma mul_comm {x y: myint}: x ** y = y ** x := begin revert y, induction x with x' ih x' ih; intro y, induction y with y' ih y' ih; { simp [ih] <|> simp }, all_goals { induction y with y' ih2 y' ih2, simp, rw <- ih, simp, all_goals { simp, rw ih2, simp, rw <- ih, rw <- ih, simp, repeat { rw add_assoc }, repeat { apply congr_arg }, rw add_comm, }, }, end lemma mul_add_dist_right {x y z: myint}: x ** z ++ y ** z = (x ++ y) ** z := begin induction z with z' ih z' ih, simp, all_goals { simp, rw <- ih, repeat { rw add_assoc }, rw <- add_assoc _ (y ** z'), rw add_comm _ (y ** z'), rw add_assoc (y ** z'), }, repeat { apply congr_arg }, apply neg_of_add, end lemma mul_add_dist_left {x y z: myint}: x ** y ++ x ** z = x ** (y ++ z) := begin repeat { rw @mul_comm x }, exact mul_add_dist_right, end @[simp] lemma mul_neg {x y: myint}: x ** neg y = neg (x ** y) := begin induction y with y' ih y' ih; simp, rw ih, rw neg_of_add, rw ih, rw <- neg_of_add, simp, end lemma mul_assoc {x y z: myint}: x ** y ** z = x ** (y ** z) := begin induction z with z' ih z' ih, simp, simp [ih], rw mul_add_dist_left, simp [ih], rw <- mul_add_dist_left, simp, end end myint
ab66ef50eabe8d156ae7974602391fd84ba2075d
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Lean/Meta/LevelDefEq.lean
7f17b569f5fc322a4b84faa9bad8a207e0d83f02
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
8,381
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 -/ prelude import Init.Lean.Meta.Basic namespace Lean namespace Meta private partial def decAux? : Level → MetaM (Option Level) | Level.zero _ => pure none | Level.param _ _ => pure none | Level.mvar mvarId _ => do mctx ← getMCtx; match mctx.getLevelAssignment? mvarId with | some u => decAux? u | none => condM (isReadOnlyLevelMVar mvarId) (pure none) $ do u ← mkFreshLevelMVar; assignLevelMVar mvarId (mkLevelSucc u); pure u | Level.succ u _ => pure u | u => let process (u v : Level) : MetaM (Option Level) := do { u? ← decAux? u; match u? with | none => pure none | some u => do v? ← decAux? v; match v? with | none => pure none | some v => pure $ mkLevelMax u v }; match u with | Level.max u v _ => process u v /- Remark: If `decAux? v` returns `some ...`, then `imax u v` is equivalent to `max u v`. -/ | Level.imax u v _ => process u v | _ => unreachable! partial def decLevel? (u : Level) : MetaM (Option Level) := do mctx ← getMCtx; result? ← decAux? u; match result? with | some v => pure $ some v | none => do modify $ fun s => { mctx := mctx, .. s }; pure none private def strictOccursMaxAux (lvl : Level) : Level → Bool | Level.max u v _ => strictOccursMaxAux u || strictOccursMaxAux v | u => u != lvl && lvl.occurs u /-- Return true iff `lvl` occurs in `max u_1 ... u_n` and `lvl != u_i` for all `i in [1, n]`. That is, `lvl` is a proper level subterm of some `u_i`. -/ private def strictOccursMax (lvl : Level) : Level → Bool | Level.max u v _ => strictOccursMaxAux lvl u || strictOccursMaxAux lvl v | _ => false /-- `mkMaxArgsDiff mvarId (max u_1 ... (mvar mvarId) ... u_n) v` => `max v u_1 ... u_n` -/ private def mkMaxArgsDiff (mvarId : MVarId) : Level → Level → Level | Level.max u v _, acc => mkMaxArgsDiff v $ mkMaxArgsDiff u acc | l@(Level.mvar id _), acc => if id != mvarId then mkLevelMax acc l else acc | l, acc => mkLevelMax acc l /-- Solve `?m =?= max ?m v` by creating a fresh metavariable `?n` and assigning `?m := max ?n v` -/ private def solveSelfMax (mvarId : MVarId) (v : Level) : MetaM Unit := do n ← mkFreshLevelMVar; assignLevelMVar mvarId $ mkMaxArgsDiff mvarId v n private def postponeIsLevelDefEq (lhs : Level) (rhs : Level) : MetaM Unit := modify $ fun s => { postponed := s.postponed.push { lhs := lhs, rhs := rhs }, .. s } inductive LevelConstraintKind | mvarEq -- ?m =?= l where ?m does not occur in l | mvarEqSelfMax -- ?m =?= max ?m l where ?m does not occur in l | other private def getLevelConstraintKind (u v : Level) : MetaM LevelConstraintKind := match u with | Level.mvar mvarId _ => condM (isReadOnlyLevelMVar mvarId) (pure LevelConstraintKind.other) (if !u.occurs v then pure LevelConstraintKind.mvarEq else if !strictOccursMax u v then pure LevelConstraintKind.mvarEqSelfMax else pure LevelConstraintKind.other) | _ => pure LevelConstraintKind.other partial def isLevelDefEqAux : Level → Level → MetaM Bool | Level.succ lhs _, Level.succ rhs _ => isLevelDefEqAux lhs rhs | lhs, rhs => if lhs == rhs then pure true else do trace! `Meta.isLevelDefEq.step (lhs ++ " =?= " ++ rhs); lhs' ← instantiateLevelMVars lhs; let lhs' := lhs'.normalize; rhs' ← instantiateLevelMVars rhs; let rhs' := rhs'.normalize; if lhs != lhs' || rhs != rhs' then isLevelDefEqAux lhs' rhs' else do mctx ← getMCtx; if !mctx.hasAssignableLevelMVar lhs && !mctx.hasAssignableLevelMVar rhs then do ctx ← read; if ctx.config.isDefEqStuckEx && (lhs.isMVar || rhs.isMVar) then do trace! `Meta.isLevelDefEq.stuck (lhs ++ " =?= " ++ rhs); throwEx $ Exception.isLevelDefEqStuck lhs rhs else pure false else do k ← getLevelConstraintKind lhs rhs; match k with | LevelConstraintKind.mvarEq => do assignLevelMVar lhs.mvarId! rhs; pure true | LevelConstraintKind.mvarEqSelfMax => do solveSelfMax lhs.mvarId! rhs; pure true | _ => do k ← getLevelConstraintKind rhs lhs; match k with | LevelConstraintKind.mvarEq => do assignLevelMVar rhs.mvarId! lhs; pure true | LevelConstraintKind.mvarEqSelfMax => do solveSelfMax rhs.mvarId! lhs; pure true | _ => if lhs.isMVar || rhs.isMVar then pure false else let isSuccEq (u v : Level) : MetaM Bool := match u with | Level.succ u _ => do v? ← decLevel? v; match v? with | some v => isLevelDefEqAux u v | none => pure false | _ => pure false; condM (isSuccEq lhs rhs) (pure true) $ condM (isSuccEq rhs lhs) (pure true) $ do postponeIsLevelDefEq lhs rhs; pure true def isListLevelDefEqAux : List Level → List Level → MetaM Bool | [], [] => pure true | u::us, v::vs => isLevelDefEqAux u v <&&> isListLevelDefEqAux us vs | _, _ => pure false private def getNumPostponed : MetaM Nat := do s ← get; pure s.postponed.size private def getResetPostponed : MetaM (PersistentArray PostponedEntry) := do s ← get; let ps := s.postponed; modify $ fun s => { postponed := {}, .. s }; pure ps private def processPostponedStep : MetaM Bool := traceCtx `Meta.isLevelDefEq.postponed.step $ do ps ← getResetPostponed; ps.foldlM (fun (r : Bool) (p : PostponedEntry) => if r then isLevelDefEqAux p.lhs p.rhs else pure false) true private partial def processPostponedAux : Unit → MetaM Bool | _ => do numPostponed ← getNumPostponed; if numPostponed == 0 then pure true else do trace! `Meta.isLevelDefEq.postponed ("processing #" ++ toString numPostponed ++ " postponed is-def-eq level constraints"); r ← processPostponedStep; if !r then pure r else do numPostponed' ← getNumPostponed; if numPostponed' == 0 then pure true else if numPostponed' < numPostponed then processPostponedAux () else do trace! `Meta.isLevelDefEq.postponed (format "no progress solving pending is-def-eq level constraints"); pure false private def processPostponed : MetaM Bool := do numPostponed ← getNumPostponed; if numPostponed == 0 then pure true else traceCtx `Meta.isLevelDefEq.postponed $ processPostponedAux () def restore (env : Environment) (mctx : MetavarContext) (postponed : PersistentArray PostponedEntry) : MetaM Unit := modify $ fun s => { env := env, mctx := mctx, postponed := postponed, .. s } /-- `commitWhen x` executes `x` and process all postponed universe level constraints produced by `x`. We keep the modifications only if both return `true`. Remark: postponed universe level constraints must be solved before returning. Otherwise, we don't know whether `x` really succeeded. -/ @[specialize] def commitWhen (x : MetaM Bool) : MetaM Bool := do s ← get; let env := s.env; let mctx := s.mctx; let postponed := s.postponed; modify $ fun s => { postponed := {}, .. s }; catch (condM x (condM processPostponed (pure true) (do restore env mctx postponed; pure false)) (do restore env mctx postponed; pure false)) (fun ex => do restore env mctx postponed; throw ex) /- Public interface -/ def isLevelDefEq (u v : Level) : MetaM Bool := traceCtx `Meta.isLevelDefEq $ do b ← commitWhen $ isLevelDefEqAux u v; trace! `Meta.isLevelDefEq (u ++ " =?= " ++ v ++ " ... " ++ if b then "success" else "failure"); pure b def isExprDefEq (t s : Expr) : MetaM Bool := traceCtx `Meta.isDefEq $ do b ← commitWhen $ isExprDefEqAux t s; trace! `Meta.isDefEq (t ++ " =?= " ++ s ++ " ... " ++ if b then "success" else "failure"); pure b abbrev isDefEq := @isExprDefEq @[init] private def regTraceClasses : IO Unit := do registerTraceClass `Meta.isLevelDefEq; registerTraceClass `Meta.isLevelDefEq.step; registerTraceClass `Meta.isLevelDefEq.postponed end Meta end Lean
4c4cb4d0621c36de24b3dec315ac6c0b3ee45411
8e6cad62ec62c6c348e5faaa3c3f2079012bdd69
/src/measure_theory/borel_space.lean
a09e4eae768355118195ffd34c5f6b3173f9e801
[ "Apache-2.0" ]
permissive
benjamindavidson/mathlib
8cc81c865aa8e7cf4462245f58d35ae9a56b150d
fad44b9f670670d87c8e25ff9cdf63af87ad731e
refs/heads/master
1,679,545,578,362
1,615,343,014,000
1,615,343,014,000
312,926,983
0
0
Apache-2.0
1,615,360,301,000
1,605,399,418,000
Lean
UTF-8
Lean
false
false
65,829
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury Kudryashov -/ import measure_theory.measure_space import measure_theory.ae_measurable_sequence import analysis.complex.basic import analysis.normed_space.finite_dimension import topology.G_delta /-! # Borel (measurable) space ## Main definitions * `borel α` : the least `σ`-algebra that contains all open sets; * `class borel_space` : a space with `topological_space` and `measurable_space` structures such that `‹measurable_space α› = borel α`; * `class opens_measurable_space` : a space with `topological_space` and `measurable_space` structures such that all open sets are measurable; equivalently, `borel α ≤ ‹measurable_space α›`. * `borel_space` instances on `empty`, `unit`, `bool`, `nat`, `int`, `rat`; * `measurable` and `borel_space` instances on `ℝ`, `ℝ≥0`, `ℝ≥0∞`. * A measure is `regular` if it is finite on compact sets, inner regular and outer regular. ## Main statements * `is_open.measurable_set`, `is_closed.measurable_set`: open and closed sets are measurable; * `continuous.measurable` : a continuous function is measurable; * `continuous.measurable2` : if `f : α → β` and `g : α → γ` are measurable and `op : β × γ → δ` is continuous, then `λ x, op (f x, g y)` is measurable; * `measurable.add` etc : dot notation for arithmetic operations on `measurable` predicates, and similarly for `dist` and `edist`; * `ae_measurable.add` : similar dot notation for almost everywhere measurable functions; * `measurable.ennreal*` : special cases for arithmetic operations on `ℝ≥0∞`. -/ noncomputable theory open classical set filter measure_theory open_locale classical big_operators topological_space nnreal ennreal universes u v w x y variables {α β γ γ₂ δ : Type*} {ι : Sort y} {s t u : set α} open measurable_space topological_space /-- `measurable_space` structure generated by `topological_space`. -/ def borel (α : Type u) [topological_space α] : measurable_space α := generate_from {s : set α | is_open s} lemma borel_eq_top_of_discrete [topological_space α] [discrete_topology α] : borel α = ⊤ := top_le_iff.1 $ λ s hs, generate_measurable.basic s (is_open_discrete s) lemma borel_eq_top_of_encodable [topological_space α] [t1_space α] [encodable α] : borel α = ⊤ := begin refine (top_le_iff.1 $ λ s hs, bUnion_of_singleton s ▸ _), apply measurable_set.bUnion s.countable_encodable, intros x hx, apply measurable_set.of_compl, apply generate_measurable.basic, exact is_closed_singleton end lemma borel_eq_generate_from_of_subbasis {s : set (set α)} [t : topological_space α] [second_countable_topology α] (hs : t = generate_from s) : borel α = generate_from s := le_antisymm (generate_from_le $ assume u (hu : t.is_open u), begin rw [hs] at hu, induction hu, case generate_open.basic : u hu { exact generate_measurable.basic u hu }, case generate_open.univ { exact @measurable_set.univ α (generate_from s) }, case generate_open.inter : s₁ s₂ _ _ hs₁ hs₂ { exact @measurable_set.inter α (generate_from s) _ _ hs₁ hs₂ }, case generate_open.sUnion : f hf ih { rcases is_open_sUnion_countable f (by rwa hs) with ⟨v, hv, vf, vu⟩, rw ← vu, exact @measurable_set.sUnion α (generate_from s) _ hv (λ x xv, ih _ (vf xv)) } end) (generate_from_le $ assume u hu, generate_measurable.basic _ $ show t.is_open u, by rw [hs]; exact generate_open.basic _ hu) lemma is_pi_system_is_open [topological_space α] : is_pi_system (is_open : set α → Prop) := λ s t hs ht hst, is_open_inter hs ht section order_topology variable (α) variables [topological_space α] [second_countable_topology α] [linear_order α] [order_topology α] lemma borel_eq_generate_Iio : borel α = generate_from (range Iio) := begin refine le_antisymm _ (generate_from_le _), { rw borel_eq_generate_from_of_subbasis (@order_topology.topology_eq_generate_intervals α _ _ _), letI : measurable_space α := measurable_space.generate_from (range Iio), have H : ∀ a : α, measurable_set (Iio a) := λ a, generate_measurable.basic _ ⟨_, rfl⟩, refine generate_from_le _, rintro _ ⟨a, rfl | rfl⟩; [skip, apply H], by_cases h : ∃ a', ∀ b, a < b ↔ a' ≤ b, { rcases h with ⟨a', ha'⟩, rw (_ : Ioi a = (Iio a')ᶜ), { exact (H _).compl }, simp [set.ext_iff, ha'] }, { rcases is_open_Union_countable (λ a' : {a' : α // a < a'}, {b | a'.1 < b}) (λ a', is_open_lt' _) with ⟨v, ⟨hv⟩, vu⟩, simp [set.ext_iff] at vu, have : Ioi a = ⋃ x : v, (Iio x.1.1)ᶜ, { simp [set.ext_iff], refine λ x, ⟨λ ax, _, λ ⟨a', ⟨h, av⟩, ax⟩, lt_of_lt_of_le h ax⟩, rcases (vu x).2 _ with ⟨a', h₁, h₂⟩, { exact ⟨a', h₁, le_of_lt h₂⟩ }, refine not_imp_comm.1 (λ h, _) h, exact ⟨x, λ b, ⟨λ ab, le_of_not_lt (λ h', h ⟨b, ab, h'⟩), lt_of_lt_of_le ax⟩⟩ }, rw this, resetI, apply measurable_set.Union, exact λ _, (H _).compl } }, { rw forall_range_iff, intro a, exact generate_measurable.basic _ is_open_Iio } end lemma borel_eq_generate_Ioi : borel α = generate_from (range Ioi) := @borel_eq_generate_Iio (order_dual α) _ (by apply_instance : second_countable_topology α) _ _ end order_topology lemma borel_comap {f : α → β} {t : topological_space β} : @borel α (t.induced f) = (@borel β t).comap f := comap_generate_from.symm lemma continuous.borel_measurable [topological_space α] [topological_space β] {f : α → β} (hf : continuous f) : @measurable α β (borel α) (borel β) f := measurable.of_le_map $ generate_from_le $ λ s hs, generate_measurable.basic (f ⁻¹' s) (hs.preimage hf) /-- A space with `measurable_space` and `topological_space` structures such that all open sets are measurable. -/ class opens_measurable_space (α : Type*) [topological_space α] [h : measurable_space α] : Prop := (borel_le : borel α ≤ h) /-- A space with `measurable_space` and `topological_space` structures such that the `σ`-algebra of measurable sets is exactly the `σ`-algebra generated by open sets. -/ class borel_space (α : Type*) [topological_space α] [measurable_space α] : Prop := (measurable_eq : ‹measurable_space α› = borel α) /-- In a `borel_space` all open sets are measurable. -/ @[priority 100] instance borel_space.opens_measurable {α : Type*} [topological_space α] [measurable_space α] [borel_space α] : opens_measurable_space α := ⟨ge_of_eq $ borel_space.measurable_eq⟩ instance subtype.borel_space {α : Type*} [topological_space α] [measurable_space α] [hα : borel_space α] (s : set α) : borel_space s := ⟨by { rw [hα.1, subtype.measurable_space, ← borel_comap], refl }⟩ instance subtype.opens_measurable_space {α : Type*} [topological_space α] [measurable_space α] [h : opens_measurable_space α] (s : set α) : opens_measurable_space s := ⟨by { rw [borel_comap], exact comap_mono h.1 }⟩ section variables [topological_space α] [measurable_space α] [opens_measurable_space α] [topological_space β] [measurable_space β] [opens_measurable_space β] [topological_space γ] [measurable_space γ] [borel_space γ] [topological_space γ₂] [measurable_space γ₂] [borel_space γ₂] [measurable_space δ] lemma is_open.measurable_set (h : is_open s) : measurable_set s := opens_measurable_space.borel_le _ $ generate_measurable.basic _ h lemma measurable_set_interior : measurable_set (interior s) := is_open_interior.measurable_set lemma is_Gδ.measurable_set (h : is_Gδ s) : measurable_set s := begin rcases h with ⟨S, hSo, hSc, rfl⟩, exact measurable_set.sInter hSc (λ t ht, (hSo t ht).measurable_set) end lemma measurable_set_of_continuous_at {β} [emetric_space β] (f : α → β) : measurable_set {x | continuous_at f x} := (is_Gδ_set_of_continuous_at f).measurable_set lemma is_closed.measurable_set (h : is_closed s) : measurable_set s := h.measurable_set.of_compl lemma is_compact.measurable_set [t2_space α] (h : is_compact s) : measurable_set s := h.is_closed.measurable_set lemma measurable_set_closure : measurable_set (closure s) := is_closed_closure.measurable_set lemma measurable_of_is_open {f : δ → γ} (hf : ∀ s, is_open s → measurable_set (f ⁻¹' s)) : measurable f := by { rw [‹borel_space γ›.measurable_eq], exact measurable_generate_from hf } lemma measurable_of_is_closed {f : δ → γ} (hf : ∀ s, is_closed s → measurable_set (f ⁻¹' s)) : measurable f := begin apply measurable_of_is_open, intros s hs, rw [← measurable_set.compl_iff, ← preimage_compl], apply hf, rw [is_closed_compl_iff], exact hs end lemma measurable_of_is_closed' {f : δ → γ} (hf : ∀ s, is_closed s → s.nonempty → s ≠ univ → measurable_set (f ⁻¹' s)) : measurable f := begin apply measurable_of_is_closed, intros s hs, cases eq_empty_or_nonempty s with h1 h1, { simp [h1] }, by_cases h2 : s = univ, { simp [h2] }, exact hf s hs h1 h2 end instance nhds_is_measurably_generated (a : α) : (𝓝 a).is_measurably_generated := begin rw [nhds, infi_subtype'], refine @filter.infi_is_measurably_generated _ _ _ _ (λ i, _), exact i.2.2.measurable_set.principal_is_measurably_generated end /-- If `s` is a measurable set, then `𝓝[s] a` is a measurably generated filter for each `a`. This cannot be an `instance` because it depends on a non-instance `hs : measurable_set s`. -/ lemma measurable_set.nhds_within_is_measurably_generated {s : set α} (hs : measurable_set s) (a : α) : (𝓝[s] a).is_measurably_generated := by haveI := hs.principal_is_measurably_generated; exact filter.inf_is_measurably_generated _ _ @[priority 100] -- see Note [lower instance priority] instance opens_measurable_space.to_measurable_singleton_class [t1_space α] : measurable_singleton_class α := ⟨λ x, is_closed_singleton.measurable_set⟩ instance pi.opens_measurable_space {ι : Type*} {π : ι → Type*} [fintype ι] [t' : Π i, topological_space (π i)] [Π i, measurable_space (π i)] [∀ i, second_countable_topology (π i)] [∀ i, opens_measurable_space (π i)] : opens_measurable_space (Π i, π i) := begin constructor, choose g hc he ho hu hinst using λ i, is_open_generated_countable_inter (π i), have : Pi.topological_space = generate_from {t | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, s a ∈ g a) ∧ t = pi ↑i s}, { rw [funext hinst, pi_generate_from_eq] }, rw [borel_eq_generate_from_of_subbasis this], apply generate_from_le, rintros _ ⟨s, i, hi, rfl⟩, refine measurable_set.pi i.countable_to_set (λ a ha, is_open.measurable_set _), rw [hinst], exact generate_open.basic _ (hi a ha) end instance prod.opens_measurable_space [second_countable_topology α] [second_countable_topology β] : opens_measurable_space (α × β) := begin constructor, rcases is_open_generated_countable_inter α with ⟨a, ha₁, ha₂, ha₃, ha₄, ha₅⟩, rcases is_open_generated_countable_inter β with ⟨b, hb₁, hb₂, hb₃, hb₄, hb₅⟩, have : prod.topological_space = generate_from {g | ∃u∈a, ∃v∈b, g = set.prod u v}, { rw [ha₅, hb₅], exact prod_generate_from_generate_from_eq ha₄ hb₄ }, rw [borel_eq_generate_from_of_subbasis this], apply generate_from_le, rintros _ ⟨u, hu, v, hv, rfl⟩, have hu : is_open u, by { rw [ha₅], exact generate_open.basic _ hu }, have hv : is_open v, by { rw [hb₅], exact generate_open.basic _ hv }, exact hu.measurable_set.prod hv.measurable_set end section open measure_theory end section preorder variables [preorder α] [order_closed_topology α] {a b : α} @[simp] lemma measurable_set_Ici : measurable_set (Ici a) := is_closed_Ici.measurable_set @[simp] lemma measurable_set_Iic : measurable_set (Iic a) := is_closed_Iic.measurable_set @[simp] lemma measurable_set_Icc : measurable_set (Icc a b) := is_closed_Icc.measurable_set instance nhds_within_Ici_is_measurably_generated : (𝓝[Ici b] a).is_measurably_generated := measurable_set_Ici.nhds_within_is_measurably_generated _ instance nhds_within_Iic_is_measurably_generated : (𝓝[Iic b] a).is_measurably_generated := measurable_set_Iic.nhds_within_is_measurably_generated _ instance at_top_is_measurably_generated : (filter.at_top : filter α).is_measurably_generated := @filter.infi_is_measurably_generated _ _ _ _ $ λ a, (measurable_set_Ici : measurable_set (Ici a)).principal_is_measurably_generated instance at_bot_is_measurably_generated : (filter.at_bot : filter α).is_measurably_generated := @filter.infi_is_measurably_generated _ _ _ _ $ λ a, (measurable_set_Iic : measurable_set (Iic a)).principal_is_measurably_generated end preorder section partial_order variables [partial_order α] [order_closed_topology α] [second_countable_topology α] {a b : α} lemma measurable_set_le' : measurable_set {p : α × α | p.1 ≤ p.2} := order_closed_topology.is_closed_le'.measurable_set lemma measurable_set_le {f g : δ → α} (hf : measurable f) (hg : measurable g) : measurable_set {a | f a ≤ g a} := hf.prod_mk hg measurable_set_le' end partial_order section linear_order variables [linear_order α] [order_closed_topology α] {a b : α} @[simp] lemma measurable_set_Iio : measurable_set (Iio a) := is_open_Iio.measurable_set @[simp] lemma measurable_set_Ioi : measurable_set (Ioi a) := is_open_Ioi.measurable_set @[simp] lemma measurable_set_Ioo : measurable_set (Ioo a b) := is_open_Ioo.measurable_set @[simp] lemma measurable_set_Ioc : measurable_set (Ioc a b) := measurable_set_Ioi.inter measurable_set_Iic @[simp] lemma measurable_set_Ico : measurable_set (Ico a b) := measurable_set_Ici.inter measurable_set_Iio instance nhds_within_Ioi_is_measurably_generated : (𝓝[Ioi b] a).is_measurably_generated := measurable_set_Ioi.nhds_within_is_measurably_generated _ instance nhds_within_Iio_is_measurably_generated : (𝓝[Iio b] a).is_measurably_generated := measurable_set_Iio.nhds_within_is_measurably_generated _ variables [second_countable_topology α] lemma measurable_set_lt' : measurable_set {p : α × α | p.1 < p.2} := (is_open_lt continuous_fst continuous_snd).measurable_set lemma measurable_set_lt {f g : δ → α} (hf : measurable f) (hg : measurable g) : measurable_set {a | f a < g a} := hf.prod_mk hg measurable_set_lt' end linear_order section linear_order variables [linear_order α] [order_closed_topology α] lemma measurable_set_interval {a b : α} : measurable_set (interval a b) := measurable_set_Icc variables [second_countable_topology α] lemma measurable.max {f g : δ → α} (hf : measurable f) (hg : measurable g) : measurable (λ a, max (f a) (g a)) := hf.piecewise (measurable_set_le hg hf) hg lemma ae_measurable.max {f g : δ → α} {μ : measure δ} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, max (f a) (g a)) μ := ⟨λ a, max (hf.mk f a) (hg.mk g a), hf.measurable_mk.max hg.measurable_mk, eventually_eq.comp₂ hf.ae_eq_mk _ hg.ae_eq_mk⟩ lemma measurable.min {f g : δ → α} (hf : measurable f) (hg : measurable g) : measurable (λ a, min (f a) (g a)) := hf.piecewise (measurable_set_le hf hg) hg lemma ae_measurable.min {f g : δ → α} {μ : measure δ} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, min (f a) (g a)) μ := ⟨λ a, min (hf.mk f a) (hg.mk g a), hf.measurable_mk.min hg.measurable_mk, eventually_eq.comp₂ hf.ae_eq_mk _ hg.ae_eq_mk⟩ end linear_order /-- A continuous function from an `opens_measurable_space` to a `borel_space` is measurable. -/ lemma continuous.measurable {f : α → γ} (hf : continuous f) : measurable f := hf.borel_measurable.mono opens_measurable_space.borel_le (le_of_eq $ borel_space.measurable_eq) section homeomorph /-- A homeomorphism between two Borel spaces is a measurable equivalence.-/ def homeomorph.to_measurable_equiv (h : γ ≃ₜ γ₂) : γ ≃ᵐ γ₂ := { measurable_to_fun := h.continuous_to_fun.measurable, measurable_inv_fun := h.continuous_inv_fun.measurable, .. h } @[simp] lemma homeomorph.to_measurable_equiv_coe (h : γ ≃ₜ γ₂) : (h.to_measurable_equiv : γ → γ₂) = h := rfl @[simp] lemma homeomorph.to_measurable_equiv_symm_coe (h : γ ≃ₜ γ₂) : (h.to_measurable_equiv.symm : γ₂ → γ) = h.symm := rfl end homeomorph lemma measurable_of_continuous_on_compl_singleton [t1_space α] {f : α → γ} (a : α) (hf : continuous_on f {x | x ≠ a}) : measurable f := measurable_of_measurable_on_compl_singleton a (continuous_on_iff_continuous_restrict.1 hf).measurable lemma continuous.measurable2 [second_countable_topology α] [second_countable_topology β] {f : δ → α} {g : δ → β} {c : α → β → γ} (h : continuous (λ p : α × β, c p.1 p.2)) (hf : measurable f) (hg : measurable g) : measurable (λ a, c (f a) (g a)) := h.measurable.comp (hf.prod_mk hg) lemma continuous.ae_measurable2 [second_countable_topology α] [second_countable_topology β] {f : δ → α} {g : δ → β} {c : α → β → γ} {μ : measure δ} (h : continuous (λ p : α × β, c p.1 p.2)) (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, c (f a) (g a)) μ := h.measurable.comp_ae_measurable (hf.prod_mk hg) lemma measurable.smul [semiring α] [second_countable_topology α] [add_comm_monoid γ] [second_countable_topology γ] [semimodule α γ] [topological_semimodule α γ] {f : δ → α} {g : δ → γ} (hf : measurable f) (hg : measurable g) : measurable (λ c, f c • g c) := continuous_smul.measurable2 hf hg lemma ae_measurable.smul [semiring α] [second_countable_topology α] [add_comm_monoid γ] [second_countable_topology γ] [semimodule α γ] [topological_semimodule α γ] {f : δ → α} {g : δ → γ} {μ : measure δ} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ c, f c • g c) μ := continuous_smul.ae_measurable2 hf hg lemma measurable.const_smul {R M : Type*} [topological_space R] [semiring R] [add_comm_monoid M] [semimodule R M] [topological_space M] [topological_semimodule R M] [measurable_space M] [borel_space M] {f : δ → M} (hf : measurable f) (c : R) : measurable (λ x, c • f x) := (continuous_const.smul continuous_id).measurable.comp hf lemma ae_measurable.const_smul {R M : Type*} [topological_space R] [semiring R] [add_comm_monoid M] [semimodule R M] [topological_space M] [topological_semimodule R M] [measurable_space M] [borel_space M] {f : δ → M} {μ : measure δ} (hf : ae_measurable f μ) (c : R) : ae_measurable (λ x, c • f x) μ := (continuous_const.smul continuous_id).measurable.comp_ae_measurable hf lemma measurable_const_smul_iff {α : Type*} [topological_space α] [division_ring α] [add_comm_monoid γ] [semimodule α γ] [topological_semimodule α γ] {f : δ → γ} {c : α} (hc : c ≠ 0) : measurable (λ x, c • f x) ↔ measurable f := ⟨λ h, by simpa only [smul_smul, inv_mul_cancel hc, one_smul] using h.const_smul c⁻¹, λ h, h.const_smul c⟩ lemma ae_measurable_const_smul_iff {α : Type*} [topological_space α] [division_ring α] [add_comm_monoid γ] [semimodule α γ] [topological_semimodule α γ] {f : δ → γ} {μ : measure δ} {c : α} (hc : c ≠ 0) : ae_measurable (λ x, c • f x) μ ↔ ae_measurable f μ := ⟨λ h, by simpa only [smul_smul, inv_mul_cancel hc, one_smul] using h.const_smul c⁻¹, λ h, h.const_smul c⟩ end section borel_space variables [topological_space α] [measurable_space α] [borel_space α] [topological_space β] [measurable_space β] [borel_space β] [topological_space γ] [measurable_space γ] [borel_space γ] [measurable_space δ] lemma pi_le_borel_pi {ι : Type*} {π : ι → Type*} [Π i, topological_space (π i)] [Π i, measurable_space (π i)] [∀ i, borel_space (π i)] : measurable_space.pi ≤ borel (Π i, π i) := begin have : ‹Π i, measurable_space (π i)› = λ i, borel (π i) := funext (λ i, borel_space.measurable_eq), rw [this], exact supr_le (λ i, comap_le_iff_le_map.2 $ (continuous_apply i).borel_measurable) end lemma prod_le_borel_prod : prod.measurable_space ≤ borel (α × β) := begin rw [‹borel_space α›.measurable_eq, ‹borel_space β›.measurable_eq], refine sup_le _ _, { exact comap_le_iff_le_map.mpr continuous_fst.borel_measurable }, { exact comap_le_iff_le_map.mpr continuous_snd.borel_measurable } end instance pi.borel_space {ι : Type*} {π : ι → Type*} [fintype ι] [t' : Π i, topological_space (π i)] [Π i, measurable_space (π i)] [∀ i, second_countable_topology (π i)] [∀ i, borel_space (π i)] : borel_space (Π i, π i) := ⟨le_antisymm pi_le_borel_pi opens_measurable_space.borel_le⟩ instance prod.borel_space [second_countable_topology α] [second_countable_topology β] : borel_space (α × β) := ⟨le_antisymm prod_le_borel_prod opens_measurable_space.borel_le⟩ @[to_additive] lemma measurable_mul [has_mul α] [has_continuous_mul α] [second_countable_topology α] : measurable (λ p : α × α, p.1 * p.2) := continuous_mul.measurable @[to_additive] lemma measurable.mul [has_mul α] [has_continuous_mul α] [second_countable_topology α] {f : δ → α} {g : δ → α} : measurable f → measurable g → measurable (λ a, f a * g a) := (@continuous_mul α _ _ _).measurable2 @[to_additive] lemma ae_measurable.mul [has_mul α] [has_continuous_mul α] [second_countable_topology α] {f : δ → α} {g : δ → α} {μ : measure δ} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, f a * g a) μ := (@continuous_mul α _ _ _).ae_measurable2 hf hg /-- A variant of `measurable.mul` that uses `*` on functions -/ @[to_additive] lemma measurable.mul' [has_mul α] [has_continuous_mul α] [second_countable_topology α] {f : δ → α} {g : δ → α} : measurable f → measurable g → measurable (f * g) := measurable.mul @[to_additive] lemma measurable_mul_left [has_mul α] [has_continuous_mul α] (x : α) : measurable (λ y : α, x * y) := continuous.measurable $ continuous_const.mul continuous_id @[to_additive] lemma measurable_mul_right [has_mul α] [has_continuous_mul α] (x : α) : measurable (λ y : α, y * x) := continuous.measurable $ continuous_id.mul continuous_const @[to_additive] lemma measurable.const_mul [has_mul α] [has_continuous_mul α] {f : δ → α} (hf : measurable f) (c : α) : measurable (λ x, c * f x) := (measurable_mul_left _).comp hf @[to_additive] lemma measurable.mul_const [has_mul α] [has_continuous_mul α] {f : δ → α} (hf : measurable f) (c : α) : measurable (λ x, f x * c) := (measurable_mul_right _).comp hf lemma measurable.sub_const [sub_neg_monoid α] [has_continuous_add α] {f : δ → α} (hf : measurable f) (c : α) : measurable (λ x, f x - c) := by simpa only [sub_eq_add_neg] using hf.add_const (-c) @[to_additive] lemma finset.measurable_prod {ι : Type*} [comm_monoid α] [has_continuous_mul α] [second_countable_topology α] {f : ι → δ → α} (s : finset ι) (hf : ∀i, measurable (f i)) : measurable (λ a, ∏ i in s, f i a) := begin simp_rw ← finset.prod_apply, exact finset.prod_induction f measurable (λ f g hf' hg', measurable.mul' hf' hg') (@measurable_one α δ _ _ _) (λ i _, hf i), end @[to_additive] lemma finset.ae_measurable_prod {ι : Type*} [comm_monoid α] [has_continuous_mul α] [second_countable_topology α] {f : ι → δ → α} {μ : measure δ} (s : finset ι) (hf : ∀i, ae_measurable (f i) μ) : ae_measurable (λ a, ∏ i in s, f i a) μ := begin simp_rw ← finset.prod_apply, exact finset.prod_induction f (λ f, ae_measurable f μ) (λ f g hf' hg', ae_measurable.mul hf' hg') (@measurable_one α δ _ _ _).ae_measurable (λ i _, hf i), end @[to_additive] lemma measurable_inv [group α] [topological_group α] : measurable (has_inv.inv : α → α) := continuous_inv.measurable @[to_additive] lemma measurable.inv [group α] [topological_group α] {f : δ → α} (hf : measurable f) : measurable (λ a, (f a)⁻¹) := measurable_inv.comp hf @[to_additive] lemma ae_measurable.inv [group α] [topological_group α] {f : δ → α} {μ : measure δ} (hf : ae_measurable f μ) : ae_measurable (λ a, (f a)⁻¹) μ := measurable_inv.comp_ae_measurable hf lemma measurable_inv' {α : Type*} [normed_field α] [measurable_space α] [borel_space α] : measurable (has_inv.inv : α → α) := measurable_of_continuous_on_compl_singleton 0 continuous_on_inv' lemma measurable.inv' {α : Type*} [normed_field α] [measurable_space α] [borel_space α] {f : δ → α} (hf : measurable f) : measurable (λ a, (f a)⁻¹) := measurable_inv'.comp hf lemma measurable.div {α : Type*} [normed_field α] [measurable_space α] [borel_space α] [second_countable_topology α] {f g : δ → α} (hf : measurable f) (hg : measurable g) : measurable (λ a, f a / g a) := hf.mul hg.inv' @[to_additive] lemma measurable.of_inv [group α] [topological_group α] {f : δ → α} (hf : measurable (λ a, (f a)⁻¹)) : measurable f := by simpa only [inv_inv] using hf.inv @[simp, to_additive] lemma measurable_inv_iff [group α] [topological_group α] {f : δ → α} : measurable (λ a, (f a)⁻¹) ↔ measurable f := ⟨measurable.of_inv, measurable.inv⟩ lemma measurable.sub [add_group α] [topological_add_group α] [second_countable_topology α] {f g : δ → α} (hf : measurable f) (hg : measurable g) : measurable (λ x, f x - g x) := by simpa only [sub_eq_add_neg] using hf.add hg.neg lemma ae_measurable.sub [add_group α] [topological_add_group α] [second_countable_topology α] {f g : δ → α} {μ : measure δ} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ x, f x - g x) μ := by simpa only [sub_eq_add_neg] using hf.add hg.neg lemma closed_embedding.measurable_inv_fun [n : nonempty β] {g : β → γ} (hg : closed_embedding g) : measurable (function.inv_fun g) := begin refine measurable_of_is_closed (λ s hs, _), by_cases h : classical.choice n ∈ s, { rw preimage_inv_fun_of_mem hg.to_embedding.inj h, exact (hg.closed_iff_image_closed.mp hs).measurable_set.union hg.closed_range.measurable_set.compl }, { rw preimage_inv_fun_of_not_mem hg.to_embedding.inj h, exact (hg.closed_iff_image_closed.mp hs).measurable_set } end lemma measurable_comp_iff_of_closed_embedding {f : δ → β} (g : β → γ) (hg : closed_embedding g) : measurable (g ∘ f) ↔ measurable f := begin refine ⟨λ hf, _, λ hf, hg.continuous.measurable.comp hf⟩, apply measurable_of_is_closed, intros s hs, convert hf (hg.is_closed_map s hs).measurable_set, rw [@preimage_comp _ _ _ f g, preimage_image_eq _ hg.to_embedding.inj] end lemma ae_measurable_comp_iff_of_closed_embedding {f : δ → β} {μ : measure δ} (g : β → γ) (hg : closed_embedding g) : ae_measurable (g ∘ f) μ ↔ ae_measurable f μ := begin by_cases h : nonempty β, { resetI, refine ⟨λ hf, _, λ hf, hg.continuous.measurable.comp_ae_measurable hf⟩, convert hg.measurable_inv_fun.comp_ae_measurable hf, ext x, exact (function.left_inverse_inv_fun hg.to_embedding.inj (f x)).symm }, { have H : ¬ nonempty δ, by { contrapose! h, exact nonempty.map f h }, simp [(measurable_of_not_nonempty H (g ∘ f)).ae_measurable, (measurable_of_not_nonempty H f).ae_measurable] } end section linear_order variables [linear_order α] [order_topology α] [second_countable_topology α] lemma measurable_of_Iio {f : δ → α} (hf : ∀ x, measurable_set (f ⁻¹' Iio x)) : measurable f := begin convert measurable_generate_from _, exact borel_space.measurable_eq.trans (borel_eq_generate_Iio _), rintro _ ⟨x, rfl⟩, exact hf x end lemma measurable_of_Ioi {f : δ → α} (hf : ∀ x, measurable_set (f ⁻¹' Ioi x)) : measurable f := begin convert measurable_generate_from _, exact borel_space.measurable_eq.trans (borel_eq_generate_Ioi _), rintro _ ⟨x, rfl⟩, exact hf x end lemma measurable_of_Iic {f : δ → α} (hf : ∀ x, measurable_set (f ⁻¹' Iic x)) : measurable f := begin apply measurable_of_Ioi, simp_rw [← compl_Iic, preimage_compl, measurable_set.compl_iff], assumption end lemma measurable_of_Ici {f : δ → α} (hf : ∀ x, measurable_set (f ⁻¹' Ici x)) : measurable f := begin apply measurable_of_Iio, simp_rw [← compl_Ici, preimage_compl, measurable_set.compl_iff], assumption end lemma measurable.is_lub {ι} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, measurable (f i)) (hg : ∀ b, is_lub {a | ∃ i, f i b = a} (g b)) : measurable g := begin change ∀ b, is_lub (range $ λ i, f i b) (g b) at hg, rw [‹borel_space α›.measurable_eq, borel_eq_generate_Ioi α], apply measurable_generate_from, rintro _ ⟨a, rfl⟩, simp_rw [set.preimage, mem_Ioi, lt_is_lub_iff (hg _), exists_range_iff, set_of_exists], exact measurable_set.Union (λ i, hf i (is_open_lt' _).measurable_set) end private lemma ae_measurable.is_lub_of_nonempty {ι} (hι : nonempty ι) {μ : measure δ} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, ae_measurable (f i) μ) (hg : ∀ᵐ b ∂μ, is_lub {a | ∃ i, f i b = a} (g b)) : ae_measurable g μ := begin let p : δ → (ι → α) → Prop := λ x f', is_lub {a | ∃ i, f' i = a} (g x), let g_seq := λ x, ite (x ∈ ae_seq_set hf p) (g x) (⟨g x⟩ : nonempty α).some, have hg_seq : ∀ b, is_lub {a | ∃ i, ae_seq hf p i b = a} (g_seq b), { intro b, haveI hα : nonempty α := nonempty.map g ⟨b⟩, simp only [ae_seq, g_seq], split_ifs, { have h_set_eq : {a : α | ∃ (i : ι), (hf i).mk (f i) b = a} = {a : α | ∃ (i : ι), f i b = a}, { ext x, simp_rw [set.mem_set_of_eq, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h], }, rw h_set_eq, exact ae_seq.fun_prop_of_mem_ae_seq_set hf h, }, { have h_singleton : {a : α | ∃ (i : ι), hα.some = a} = {hα.some}, { ext1 x, exact ⟨λ hx, hx.some_spec.symm, λ hx, ⟨hι.some, hx.symm⟩⟩, }, rw h_singleton, exact is_lub_singleton, }, }, refine ⟨g_seq, measurable.is_lub (ae_seq.measurable hf p) hg_seq, _⟩, exact (ite_ae_eq_of_measure_compl_zero g (λ x, (⟨g x⟩ : nonempty α).some) (ae_seq_set hf p) (ae_seq.measure_compl_ae_seq_set_eq_zero hf hg)).symm, end lemma ae_measurable.is_lub {ι} {μ : measure δ} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, ae_measurable (f i) μ) (hg : ∀ᵐ b ∂μ, is_lub {a | ∃ i, f i b = a} (g b)) : ae_measurable g μ := begin by_cases hμ : μ = 0, { rw hμ, exact ae_measurable_of_zero_measure }, haveI : μ.ae.ne_bot := by simpa [ne_bot_iff], by_cases hι : nonempty ι, { exact ae_measurable.is_lub_of_nonempty hι hf hg, }, suffices : ∃ x, g =ᵐ[μ] λ y, g x, by { exact ⟨(λ y, g this.some), measurable_const, this.some_spec⟩, }, have h_empty : ∀ x, {a : α | ∃ (i : ι), f i x = a} = ∅, { intro x, ext1 y, rw [set.mem_set_of_eq, set.mem_empty_eq, iff_false], exact λ hi, hι (nonempty_of_exists hi), }, simp_rw h_empty at hg, exact ⟨hg.exists.some, hg.mono (λ y hy, is_lub.unique hy hg.exists.some_spec)⟩, end lemma measurable.is_glb {ι} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, measurable (f i)) (hg : ∀ b, is_glb {a | ∃ i, f i b = a} (g b)) : measurable g := begin change ∀ b, is_glb (range $ λ i, f i b) (g b) at hg, rw [‹borel_space α›.measurable_eq, borel_eq_generate_Iio α], apply measurable_generate_from, rintro _ ⟨a, rfl⟩, simp_rw [set.preimage, mem_Iio, is_glb_lt_iff (hg _), exists_range_iff, set_of_exists], exact measurable_set.Union (λ i, hf i (is_open_gt' _).measurable_set) end private lemma ae_measurable.is_glb_of_nonempty {ι} (hι : nonempty ι) {μ : measure δ} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, ae_measurable (f i) μ) (hg : ∀ᵐ b ∂μ, is_glb {a | ∃ i, f i b = a} (g b)) : ae_measurable g μ := begin let p : δ → (ι → α) → Prop := λ x f', is_glb {a | ∃ i, f' i = a} (g x), let g_seq := λ x, ite (x ∈ ae_seq_set hf p) (g x) (⟨g x⟩ : nonempty α).some, have hg_seq : ∀ b, is_glb {a | ∃ i, ae_seq hf p i b = a} (g_seq b), { intro b, haveI hα : nonempty α := nonempty.map g ⟨b⟩, simp only [ae_seq, g_seq], split_ifs, { have h_set_eq : {a : α | ∃ (i : ι), (hf i).mk (f i) b = a} = {a : α | ∃ (i : ι), f i b = a}, { ext x, simp_rw [set.mem_set_of_eq, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h], }, rw h_set_eq, exact ae_seq.fun_prop_of_mem_ae_seq_set hf h, }, { have h_singleton : {a : α | ∃ (i : ι), hα.some = a} = {hα.some}, { ext1 x, exact ⟨λ hx, hx.some_spec.symm, λ hx, ⟨hι.some, hx.symm⟩⟩, }, rw h_singleton, exact is_glb_singleton, }, }, refine ⟨g_seq, measurable.is_glb (ae_seq.measurable hf p) hg_seq, _⟩, exact (ite_ae_eq_of_measure_compl_zero g (λ x, (⟨g x⟩ : nonempty α).some) (ae_seq_set hf p) (ae_seq.measure_compl_ae_seq_set_eq_zero hf hg)).symm, end lemma ae_measurable.is_glb {ι} {μ : measure δ} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, ae_measurable (f i) μ) (hg : ∀ᵐ b ∂μ, is_glb {a | ∃ i, f i b = a} (g b)) : ae_measurable g μ := begin by_cases hμ : μ = 0, { rw hμ, exact ae_measurable_of_zero_measure }, haveI : μ.ae.ne_bot := by simpa [ne_bot_iff], by_cases hι : nonempty ι, { exact ae_measurable.is_glb_of_nonempty hι hf hg, }, suffices : ∃ x, g =ᵐ[μ] λ y, g x, by { exact ⟨(λ y, g this.some), measurable_const, this.some_spec⟩, }, have h_empty : ∀ x, {a : α | ∃ (i : ι), f i x = a} = ∅, { intro x, ext1 y, rw [set.mem_set_of_eq, set.mem_empty_eq, iff_false], exact λ hi, hι (nonempty_of_exists hi), }, simp_rw h_empty at hg, exact ⟨hg.exists.some, hg.mono (λ y hy, is_glb.unique hy hg.exists.some_spec)⟩, end end linear_order lemma measurable.supr_Prop {α} [measurable_space α] [complete_lattice α] (p : Prop) {f : δ → α} (hf : measurable f) : measurable (λ b, ⨆ h : p, f b) := classical.by_cases (assume h : p, begin convert hf, funext, exact supr_pos h end) (assume h : ¬p, begin convert measurable_const, funext, exact supr_neg h end) lemma measurable.infi_Prop {α} [measurable_space α] [complete_lattice α] (p : Prop) {f : δ → α} (hf : measurable f) : measurable (λ b, ⨅ h : p, f b) := classical.by_cases (assume h : p, begin convert hf, funext, exact infi_pos h end ) (assume h : ¬p, begin convert measurable_const, funext, exact infi_neg h end) section complete_linear_order variables [complete_linear_order α] [order_topology α] [second_countable_topology α] lemma measurable_supr {ι} [encodable ι] {f : ι → δ → α} (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨆ i, f i b) := measurable.is_lub hf $ λ b, is_lub_supr lemma ae_measurable_supr {ι} {μ : measure δ} [encodable ι] {f : ι → δ → α} (hf : ∀ i, ae_measurable (f i) μ) : ae_measurable (λ b, ⨆ i, f i b) μ := ae_measurable.is_lub hf $ (ae_of_all μ (λ b, is_lub_supr)) lemma measurable_infi {ι} [encodable ι] {f : ι → δ → α} (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨅ i, f i b) := measurable.is_glb hf $ λ b, is_glb_infi lemma ae_measurable_infi {ι} {μ : measure δ} [encodable ι] {f : ι → δ → α} (hf : ∀ i, ae_measurable (f i) μ) : ae_measurable (λ b, ⨅ i, f i b) μ := ae_measurable.is_glb hf $ (ae_of_all μ (λ b, is_glb_infi)) lemma measurable_bsupr {ι} (s : set ι) {f : ι → δ → α} (hs : countable s) (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨆ i ∈ s, f i b) := by { haveI : encodable s := hs.to_encodable, simp only [supr_subtype'], exact measurable_supr (λ i, hf i) } lemma ae_measurable_bsupr {ι} {μ : measure δ} (s : set ι) {f : ι → δ → α} (hs : countable s) (hf : ∀ i, ae_measurable (f i) μ) : ae_measurable (λ b, ⨆ i ∈ s, f i b) μ := begin haveI : encodable s := hs.to_encodable, simp only [supr_subtype'], exact ae_measurable_supr (λ i, hf i), end lemma measurable_binfi {ι} (s : set ι) {f : ι → δ → α} (hs : countable s) (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨅ i ∈ s, f i b) := by { haveI : encodable s := hs.to_encodable, simp only [infi_subtype'], exact measurable_infi (λ i, hf i) } lemma ae_measurable_binfi {ι} {μ : measure δ} (s : set ι) {f : ι → δ → α} (hs : countable s) (hf : ∀ i, ae_measurable (f i) μ) : ae_measurable (λ b, ⨅ i ∈ s, f i b) μ := begin haveI : encodable s := hs.to_encodable, simp only [infi_subtype'], exact ae_measurable_infi (λ i, hf i), end /-- `liminf` over a general filter is measurable. See `measurable_liminf` for the version over `ℕ`. -/ lemma measurable_liminf' {ι ι'} {f : ι → δ → α} {u : filter ι} (hf : ∀ i, measurable (f i)) {p : ι' → Prop} {s : ι' → set ι} (hu : u.has_countable_basis p s) (hs : ∀ i, (s i).countable) : measurable (λ x, liminf u (λ i, f i x)) := begin simp_rw [hu.to_has_basis.liminf_eq_supr_infi], refine measurable_bsupr _ hu.countable _, exact λ i, measurable_binfi _ (hs i) hf end /-- `limsup` over a general filter is measurable. See `measurable_limsup` for the version over `ℕ`. -/ lemma measurable_limsup' {ι ι'} {f : ι → δ → α} {u : filter ι} (hf : ∀ i, measurable (f i)) {p : ι' → Prop} {s : ι' → set ι} (hu : u.has_countable_basis p s) (hs : ∀ i, (s i).countable) : measurable (λ x, limsup u (λ i, f i x)) := begin simp_rw [hu.to_has_basis.limsup_eq_infi_supr], refine measurable_binfi _ hu.countable _, exact λ i, measurable_bsupr _ (hs i) hf end /-- `liminf` over `ℕ` is measurable. See `measurable_liminf'` for a version with a general filter. -/ lemma measurable_liminf {f : ℕ → δ → α} (hf : ∀ i, measurable (f i)) : measurable (λ x, liminf at_top (λ i, f i x)) := measurable_liminf' hf at_top_countable_basis (λ i, countable_encodable _) /-- `limsup` over `ℕ` is measurable. See `measurable_limsup'` for a version with a general filter. -/ lemma measurable_limsup {f : ℕ → δ → α} (hf : ∀ i, measurable (f i)) : measurable (λ x, limsup at_top (λ i, f i x)) := measurable_limsup' hf at_top_countable_basis (λ i, countable_encodable _) end complete_linear_order section conditionally_complete_linear_order variables [conditionally_complete_linear_order α] [second_countable_topology α] [order_topology α] lemma measurable_cSup {ι} {f : ι → δ → α} {s : set ι} (hs : s.countable) (hf : ∀ i, measurable (f i)) (bdd : ∀ x, bdd_above ((λ i, f i x) '' s)) : measurable (λ x, Sup ((λ i, f i x) '' s)) := begin cases eq_empty_or_nonempty s with h2s h2s, { simp [h2s, measurable_const] }, { apply measurable_of_Iic, intro y, simp_rw [preimage, mem_Iic, cSup_le_iff (bdd _) (h2s.image _), ball_image_iff, set_of_forall], exact measurable_set.bInter hs (λ i hi, measurable_set_le (hf i) measurable_const) } end end conditionally_complete_linear_order /-- Convert a `homeomorph` to a `measurable_equiv`. -/ def homemorph.to_measurable_equiv (h : α ≃ₜ β) : α ≃ᵐ β := { to_equiv := h.to_equiv, measurable_to_fun := h.continuous_to_fun.measurable, measurable_inv_fun := h.continuous_inv_fun.measurable } end borel_space instance empty.borel_space : borel_space empty := ⟨borel_eq_top_of_discrete.symm⟩ instance unit.borel_space : borel_space unit := ⟨borel_eq_top_of_discrete.symm⟩ instance bool.borel_space : borel_space bool := ⟨borel_eq_top_of_discrete.symm⟩ instance nat.borel_space : borel_space ℕ := ⟨borel_eq_top_of_discrete.symm⟩ instance int.borel_space : borel_space ℤ := ⟨borel_eq_top_of_discrete.symm⟩ instance rat.borel_space : borel_space ℚ := ⟨borel_eq_top_of_encodable.symm⟩ instance real.measurable_space : measurable_space ℝ := borel ℝ instance real.borel_space : borel_space ℝ := ⟨rfl⟩ instance nnreal.measurable_space : measurable_space ℝ≥0 := subtype.measurable_space instance nnreal.borel_space : borel_space ℝ≥0 := subtype.borel_space _ instance ennreal.measurable_space : measurable_space ℝ≥0∞ := borel ℝ≥0∞ instance ennreal.borel_space : borel_space ℝ≥0∞ := ⟨rfl⟩ instance complex.measurable_space : measurable_space ℂ := borel ℂ instance complex.borel_space : borel_space ℂ := ⟨rfl⟩ section metric_space variables [metric_space α] [measurable_space α] [opens_measurable_space α] variables [measurable_space β] {x : α} {ε : ℝ} open metric lemma measurable_set_ball : measurable_set (metric.ball x ε) := metric.is_open_ball.measurable_set lemma measurable_set_closed_ball : measurable_set (metric.closed_ball x ε) := metric.is_closed_ball.measurable_set lemma measurable_inf_dist {s : set α} : measurable (λ x, inf_dist x s) := (continuous_inf_dist_pt s).measurable lemma measurable.inf_dist {f : β → α} (hf : measurable f) {s : set α} : measurable (λ x, inf_dist (f x) s) := measurable_inf_dist.comp hf lemma measurable_inf_nndist {s : set α} : measurable (λ x, inf_nndist x s) := (continuous_inf_nndist_pt s).measurable lemma measurable.inf_nndist {f : β → α} (hf : measurable f) {s : set α} : measurable (λ x, inf_nndist (f x) s) := measurable_inf_nndist.comp hf variables [second_countable_topology α] lemma measurable_dist : measurable (λ p : α × α, dist p.1 p.2) := continuous_dist.measurable lemma measurable.dist {f g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λ b, dist (f b) (g b)) := (@continuous_dist α _).measurable2 hf hg lemma measurable_nndist : measurable (λ p : α × α, nndist p.1 p.2) := continuous_nndist.measurable lemma measurable.nndist {f g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λ b, nndist (f b) (g b)) := (@continuous_nndist α _).measurable2 hf hg end metric_space section emetric_space variables [emetric_space α] [measurable_space α] [opens_measurable_space α] variables [measurable_space β] {x : α} {ε : ℝ≥0∞} open emetric lemma measurable_set_eball : measurable_set (emetric.ball x ε) := emetric.is_open_ball.measurable_set lemma measurable_edist_right : measurable (edist x) := (continuous_const.edist continuous_id).measurable lemma measurable_edist_left : measurable (λ y, edist y x) := (continuous_id.edist continuous_const).measurable lemma measurable_inf_edist {s : set α} : measurable (λ x, inf_edist x s) := continuous_inf_edist.measurable lemma measurable.inf_edist {f : β → α} (hf : measurable f) {s : set α} : measurable (λ x, inf_edist (f x) s) := measurable_inf_edist.comp hf variables [second_countable_topology α] lemma measurable_edist : measurable (λ p : α × α, edist p.1 p.2) := continuous_edist.measurable lemma measurable.edist {f g : β → α} (hf : measurable f) (hg : measurable g) : measurable (λ b, edist (f b) (g b)) := (@continuous_edist α _).measurable2 hf hg lemma ae_measurable.edist {f g : β → α} {μ : measure β} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, edist (f a) (g a)) μ := (@continuous_edist α _).ae_measurable2 hf hg end emetric_space namespace real open measurable_space measure_theory lemma borel_eq_generate_from_Ioo_rat : borel ℝ = generate_from (⋃(a b : ℚ) (h : a < b), {Ioo a b}) := borel_eq_generate_from_of_subbasis is_topological_basis_Ioo_rat.2.2 lemma measure_ext_Ioo_rat {μ ν : measure ℝ} [locally_finite_measure μ] (h : ∀ a b : ℚ, μ (Ioo a b) = ν (Ioo a b)) : μ = ν := begin refine measure.ext_of_generate_from_of_cover_subset borel_eq_generate_from_Ioo_rat _ (subset.refl _) _ _ _ _, { simp only [is_pi_system, mem_Union, mem_singleton_iff], rintros _ _ ⟨a₁, b₁, h₁, rfl⟩ ⟨a₂, b₂, h₂, rfl⟩ ne, simp only [Ioo_inter_Ioo, sup_eq_max, inf_eq_min, ← rat.cast_max, ← rat.cast_min, nonempty_Ioo] at ne ⊢, refine ⟨_, _, _, rfl⟩, assumption_mod_cast }, { exact countable_Union (λ a, (countable_encodable _).bUnion $ λ _ _, countable_singleton _) }, { exact is_topological_basis_Ioo_rat.2.1 }, { simp only [mem_Union, mem_singleton_iff], rintros _ ⟨a, b, h, rfl⟩, refine (measure_mono subset_closure).trans_lt _, rw [closure_Ioo], exacts [compact_Icc.finite_measure, rat.cast_lt.2 h] }, { simp only [mem_Union, mem_singleton_iff], rintros _ ⟨a, b, hab, rfl⟩, exact h a b } end lemma borel_eq_generate_from_Iio_rat : borel ℝ = generate_from (⋃ a : ℚ, {Iio a}) := begin let g, swap, apply le_antisymm (_ : _ ≤ g) (measurable_space.generate_from_le (λ t, _)), { rw borel_eq_generate_from_Ioo_rat, refine generate_from_le (λ t, _), simp only [mem_Union], rintro ⟨a, b, h, H⟩, rw [mem_singleton_iff.1 H], rw (set.ext (λ x, _) : Ioo (a : ℝ) b = (⋃c>a, (Iio c)ᶜ) ∩ Iio b), { have hg : ∀ q : ℚ, g.measurable_set' (Iio q) := λ q, generate_measurable.basic (Iio q) (by { simp, exact ⟨_, rfl⟩ }), refine @measurable_set.inter _ g _ _ _ (hg _), refine @measurable_set.bUnion _ _ g _ _ (countable_encodable _) (λ c h, _), exact @measurable_set.compl _ _ g (hg _) }, { suffices : x < ↑b → (↑a < x ↔ ∃ (i : ℚ), a < i ∧ ↑i ≤ x), by simpa, refine λ _, ⟨λ h, _, λ ⟨i, hai, hix⟩, (rat.cast_lt.2 hai).trans_le hix⟩, rcases exists_rat_btwn h with ⟨c, ac, cx⟩, exact ⟨c, rat.cast_lt.1 ac, cx.le⟩ } }, { simp, rintro r rfl, exact is_open_Iio.measurable_set } end end real variable [measurable_space α] lemma measurable.sub_nnreal {f g : α → ℝ≥0} : measurable f → measurable g → measurable (λ a, f a - g a) := (@continuous_sub ℝ≥0 _ _ _).measurable2 lemma measurable.nnreal_of_real {f : α → ℝ} (hf : measurable f) : measurable (λ x, nnreal.of_real (f x)) := nnreal.continuous_of_real.measurable.comp hf lemma nnreal.measurable_coe : measurable (coe : ℝ≥0 → ℝ) := nnreal.continuous_coe.measurable lemma measurable.nnreal_coe {f : α → ℝ≥0} (hf : measurable f) : measurable (λ x, (f x : ℝ)) := nnreal.measurable_coe.comp hf lemma measurable.ennreal_coe {f : α → ℝ≥0} (hf : measurable f) : measurable (λ x, (f x : ℝ≥0∞)) := ennreal.continuous_coe.measurable.comp hf lemma ae_measurable.ennreal_coe {f : α → ℝ≥0} {μ : measure α} (hf : ae_measurable f μ) : ae_measurable (λ x, (f x : ℝ≥0∞)) μ := ennreal.continuous_coe.measurable.comp_ae_measurable hf lemma measurable.ennreal_of_real {f : α → ℝ} (hf : measurable f) : measurable (λ x, ennreal.of_real (f x)) := ennreal.continuous_of_real.measurable.comp hf /-- The set of finite `ℝ≥0∞` numbers is `measurable_equiv` to `ℝ≥0`. -/ def measurable_equiv.ennreal_equiv_nnreal : {r : ℝ≥0∞ | r ≠ ∞} ≃ᵐ ℝ≥0 := ennreal.ne_top_homeomorph_nnreal.to_measurable_equiv namespace ennreal lemma measurable_coe : measurable (coe : ℝ≥0 → ℝ≥0∞) := measurable_id.ennreal_coe lemma measurable_of_measurable_nnreal {f : ℝ≥0∞ → α} (h : measurable (λ p : ℝ≥0, f p)) : measurable f := measurable_of_measurable_on_compl_singleton ∞ (measurable_equiv.ennreal_equiv_nnreal.symm.measurable_coe_iff.1 h) /-- `ℝ≥0∞` is `measurable_equiv` to `ℝ≥0 ⊕ unit`. -/ def ennreal_equiv_sum : ℝ≥0∞ ≃ᵐ ℝ≥0 ⊕ unit := { measurable_to_fun := measurable_of_measurable_nnreal measurable_inl, measurable_inv_fun := measurable_sum measurable_coe (@measurable_const ℝ≥0∞ unit _ _ ∞), .. equiv.option_equiv_sum_punit ℝ≥0 } open function (uncurry) lemma measurable_of_measurable_nnreal_prod [measurable_space β] [measurable_space γ] {f : ℝ≥0∞ × β → γ} (H₁ : measurable (λ p : ℝ≥0 × β, f (p.1, p.2))) (H₂ : measurable (λ x, f (∞, x))) : measurable f := let e : ℝ≥0∞ × β ≃ᵐ ℝ≥0 × β ⊕ unit × β := (ennreal_equiv_sum.prod_congr (measurable_equiv.refl β)).trans (measurable_equiv.sum_prod_distrib _ _ _) in e.symm.measurable_coe_iff.1 $ measurable_sum H₁ (H₂.comp measurable_id.snd) lemma measurable_of_measurable_nnreal_nnreal [measurable_space β] {f : ℝ≥0∞ × ℝ≥0∞ → β} (h₁ : measurable (λ p : ℝ≥0 × ℝ≥0, f (p.1, p.2))) (h₂ : measurable (λ r : ℝ≥0, f (∞, r))) (h₃ : measurable (λ r : ℝ≥0, f (r, ∞))) : measurable f := measurable_of_measurable_nnreal_prod (measurable_swap_iff.1 $ measurable_of_measurable_nnreal_prod (h₁.comp measurable_swap) h₃) (measurable_of_measurable_nnreal h₂) lemma measurable_of_real : measurable ennreal.of_real := ennreal.continuous_of_real.measurable lemma measurable_to_real : measurable ennreal.to_real := ennreal.measurable_of_measurable_nnreal nnreal.measurable_coe lemma measurable_to_nnreal : measurable ennreal.to_nnreal := ennreal.measurable_of_measurable_nnreal measurable_id lemma measurable_mul : measurable (λ p : ℝ≥0∞ × ℝ≥0∞, p.1 * p.2) := begin apply measurable_of_measurable_nnreal_nnreal, { simp only [← ennreal.coe_mul, measurable_mul.ennreal_coe] }, { simp only [ennreal.top_mul, ennreal.coe_eq_zero], exact measurable_const.piecewise (measurable_set_singleton _) measurable_const }, { simp only [ennreal.mul_top, ennreal.coe_eq_zero], exact measurable_const.piecewise (measurable_set_singleton _) measurable_const } end lemma measurable_sub : measurable (λ p : ℝ≥0∞ × ℝ≥0∞, p.1 - p.2) := by apply measurable_of_measurable_nnreal_nnreal; simp [← ennreal.coe_sub, continuous_sub.measurable.ennreal_coe] lemma measurable_inv : measurable (has_inv.inv : ℝ≥0∞ → ℝ≥0∞) := ennreal.continuous_inv.measurable lemma measurable_div : measurable (λ p : ℝ≥0∞ × ℝ≥0∞, p.1 / p.2) := ennreal.measurable_mul.comp $ measurable_fst.prod_mk $ ennreal.measurable_inv.comp measurable_snd end ennreal lemma measurable.to_nnreal {f : α → ℝ≥0∞} (hf : measurable f) : measurable (λ x, (f x).to_nnreal) := ennreal.measurable_to_nnreal.comp hf lemma measurable_ennreal_coe_iff {f : α → ℝ≥0} : measurable (λ x, (f x : ℝ≥0∞)) ↔ measurable f := ⟨λ h, h.to_nnreal, λ h, h.ennreal_coe⟩ lemma measurable.to_real {f : α → ℝ≥0∞} (hf : measurable f) : measurable (λ x, ennreal.to_real (f x)) := ennreal.measurable_to_real.comp hf lemma ae_measurable.to_real {f : α → ℝ≥0∞} {μ : measure α} (hf : ae_measurable f μ) : ae_measurable (λ x, ennreal.to_real (f x)) μ := ennreal.measurable_to_real.comp_ae_measurable hf lemma measurable.ennreal_mul {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) : measurable (λ a, f a * g a) := ennreal.measurable_mul.comp (hf.prod_mk hg) lemma ae_measurable.ennreal_mul {f g : α → ℝ≥0∞} {μ : measure α} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, f a * g a) μ := ennreal.measurable_mul.comp_ae_measurable (hf.prod_mk hg) lemma measurable.ennreal_sub {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) : measurable (λ a, f a - g a) := ennreal.measurable_sub.comp (hf.prod_mk hg) /-- note: `ℝ≥0∞` can probably be generalized in a future version of this lemma. -/ lemma measurable.ennreal_tsum {ι} [encodable ι] {f : ι → α → ℝ≥0∞} (h : ∀ i, measurable (f i)) : measurable (λ x, ∑' i, f i x) := by { simp_rw [ennreal.tsum_eq_supr_sum], apply measurable_supr, exact λ s, s.measurable_sum h } lemma ae_measurable.ennreal_tsum {ι} [encodable ι] {f : ι → α → ℝ≥0∞} {μ : measure α} (h : ∀ i, ae_measurable (f i) μ) : ae_measurable (λ x, ∑' i, f i x) μ := by { simp_rw [ennreal.tsum_eq_supr_sum], apply ae_measurable_supr, exact λ s, finset.ae_measurable_sum s h } lemma measurable.ennreal_inv {f : α → ℝ≥0∞} (hf : measurable f) : measurable (λ a, (f a)⁻¹) := ennreal.measurable_inv.comp hf lemma measurable.ennreal_div {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) : measurable (λ a, f a / g a) := ennreal.measurable_div.comp $ hf.prod_mk hg section normed_group variables [normed_group α] [opens_measurable_space α] [measurable_space β] lemma measurable_norm : measurable (norm : α → ℝ) := continuous_norm.measurable lemma measurable.norm {f : β → α} (hf : measurable f) : measurable (λ a, norm (f a)) := measurable_norm.comp hf lemma ae_measurable.norm {f : β → α} {μ : measure β} (hf : ae_measurable f μ) : ae_measurable (λ a, norm (f a)) μ := measurable_norm.comp_ae_measurable hf lemma measurable_nnnorm : measurable (nnnorm : α → ℝ≥0) := continuous_nnnorm.measurable lemma measurable.nnnorm {f : β → α} (hf : measurable f) : measurable (λ a, nnnorm (f a)) := measurable_nnnorm.comp hf lemma ae_measurable.nnnorm {f : β → α} {μ : measure β} (hf : ae_measurable f μ) : ae_measurable (λ a, nnnorm (f a)) μ := measurable_nnnorm.comp_ae_measurable hf lemma measurable_ennnorm : measurable (λ x : α, (nnnorm x : ℝ≥0∞)) := measurable_nnnorm.ennreal_coe lemma measurable.ennnorm {f : β → α} (hf : measurable f) : measurable (λ a, (nnnorm (f a) : ℝ≥0∞)) := hf.nnnorm.ennreal_coe lemma ae_measurable.ennnorm {f : β → α} {μ : measure β} (hf : ae_measurable f μ) : ae_measurable (λ a, (nnnorm (f a) : ℝ≥0∞)) μ := measurable_ennnorm.comp_ae_measurable hf end normed_group section limits variables [measurable_space β] [metric_space β] [borel_space β] open metric /-- A limit (over a general filter) of measurable `ℝ≥0` valued functions is measurable. The assumption `hs` can be dropped using `filter.is_countably_generated.has_antimono_basis`, but we don't need that case yet. -/ lemma measurable_of_tendsto_nnreal' {ι ι'} {f : ι → α → ℝ≥0} {g : α → ℝ≥0} (u : filter ι) [ne_bot u] (hf : ∀ i, measurable (f i)) (lim : tendsto f u (𝓝 g)) {p : ι' → Prop} {s : ι' → set ι} (hu : u.has_countable_basis p s) (hs : ∀ i, (s i).countable) : measurable g := begin rw [tendsto_pi] at lim, rw [← measurable_ennreal_coe_iff], have : ∀ x, liminf u (λ n, (f n x : ℝ≥0∞)) = (g x : ℝ≥0∞) := λ x, ((ennreal.continuous_coe.tendsto (g x)).comp (lim x)).liminf_eq, simp_rw [← this], show measurable (λ x, liminf u (λ n, (f n x : ℝ≥0∞))), exact measurable_liminf' (λ i, (hf i).ennreal_coe) hu hs, end /-- A sequential limit of measurable `ℝ≥0` valued functions is measurable. -/ lemma measurable_of_tendsto_nnreal {f : ℕ → α → ℝ≥0} {g : α → ℝ≥0} (hf : ∀ i, measurable (f i)) (lim : tendsto f at_top (𝓝 g)) : measurable g := measurable_of_tendsto_nnreal' at_top hf lim at_top_countable_basis (λ i, countable_encodable _) /-- A limit (over a general filter) of measurable functions valued in a metric space is measurable. The assumption `hs` can be dropped using `filter.is_countably_generated.has_antimono_basis`, but we don't need that case yet. -/ lemma measurable_of_tendsto_metric' {ι ι'} {f : ι → α → β} {g : α → β} (u : filter ι) [ne_bot u] (hf : ∀ i, measurable (f i)) (lim : tendsto f u (𝓝 g)) {p : ι' → Prop} {s : ι' → set ι} (hu : u.has_countable_basis p s) (hs : ∀ i, (s i).countable) : measurable g := begin apply measurable_of_is_closed', intros s h1s h2s h3s, have : measurable (λ x, inf_nndist (g x) s), { refine measurable_of_tendsto_nnreal' u (λ i, (hf i).inf_nndist) _ hu hs, swap, rw [tendsto_pi], rw [tendsto_pi] at lim, intro x, exact ((continuous_inf_nndist_pt s).tendsto (g x)).comp (lim x) }, have h4s : g ⁻¹' s = (λ x, inf_nndist (g x) s) ⁻¹' {0}, { ext x, simp [h1s, ← mem_iff_inf_dist_zero_of_closed h1s h2s, ← nnreal.coe_eq_zero] }, rw [h4s], exact this (measurable_set_singleton 0), end /-- A sequential limit of measurable functions valued in a metric space is measurable. -/ lemma measurable_of_tendsto_metric {f : ℕ → α → β} {g : α → β} (hf : ∀ i, measurable (f i)) (lim : tendsto f at_top (𝓝 g)) : measurable g := measurable_of_tendsto_metric' at_top hf lim at_top_countable_basis (λ i, countable_encodable _) lemma ae_measurable_of_tendsto_metric_ae {μ : measure α} {f : ℕ → α → β} {g : α → β} (hf : ∀ n, ae_measurable (f n) μ) (h_ae_tendsto : ∀ᵐ x ∂μ, filter.at_top.tendsto (λ n, f n x) (𝓝 (g x))) : ae_measurable g μ := begin let p : α → (ℕ → β) → Prop := λ x f', filter.at_top.tendsto (λ n, f' n) (𝓝 (g x)), let hp : ∀ᵐ x ∂μ, p x (λ n, f n x), from h_ae_tendsto, let ae_seq_lim := λ x, ite (x ∈ ae_seq_set hf p) (g x) (⟨f 0 x⟩ : nonempty β).some, refine ⟨ae_seq_lim, _, (ite_ae_eq_of_measure_compl_zero g (λ x, (⟨f 0 x⟩ : nonempty β).some) (ae_seq_set hf p) (ae_seq.measure_compl_ae_seq_set_eq_zero hf hp)).symm⟩, refine measurable_of_tendsto_metric (@ae_seq.measurable α β _ _ _ f μ hf p) _, refine tendsto_pi.mpr (λ x, _), simp_rw [ae_seq, ae_seq_lim], split_ifs with hx, { simp_rw ae_seq.mk_eq_fun_of_mem_ae_seq_set hf hx, exact @ae_seq.fun_prop_of_mem_ae_seq_set α β _ _ _ _ _ _ hf x hx, }, { exact tendsto_const_nhds, }, end lemma measurable_of_tendsto_metric_ae {μ : measure α} [μ.is_complete] {f : ℕ → α → β} {g : α → β} (hf : ∀ n, measurable (f n)) (h_ae_tendsto : ∀ᵐ x ∂μ, filter.at_top.tendsto (λ n, f n x) (𝓝 (g x))) : measurable g := ae_measurable_iff_measurable.mp (ae_measurable_of_tendsto_metric_ae (λ i, (hf i).ae_measurable) h_ae_tendsto) lemma measurable_limit_of_tendsto_metric_ae {μ : measure α} {f : ℕ → α → β} (hf : ∀ n, ae_measurable (f n) μ) (h_ae_tendsto : ∀ᵐ x ∂μ, ∃ l : β, filter.at_top.tendsto (λ n, f n x) (𝓝 l)) : ∃ (f_lim : α → β) (hf_lim_meas : measurable f_lim), ∀ᵐ x ∂μ, filter.at_top.tendsto (λ n, f n x) (𝓝 (f_lim x)) := begin let p : α → (ℕ → β) → Prop := λ x f', ∃ l : β, filter.at_top.tendsto (λ n, f' n) (𝓝 l), have hp_mem : ∀ x, x ∈ ae_seq_set hf p → p x (λ n, f n x), from λ x hx, ae_seq.fun_prop_of_mem_ae_seq_set hf hx, have hμ_compl : μ (ae_seq_set hf p)ᶜ = 0, from ae_seq.measure_compl_ae_seq_set_eq_zero hf h_ae_tendsto, let f_lim : α → β := λ x, dite (x ∈ ae_seq_set hf p) (λ h, (hp_mem x h).some) (λ h, (⟨f 0 x⟩ : nonempty β).some), have hf_lim_conv : ∀ x, x ∈ ae_seq_set hf p → filter.at_top.tendsto (λ n, f n x) (𝓝 (f_lim x)), { intros x hx_conv, simp only [f_lim, hx_conv, dif_pos], exact (hp_mem x hx_conv).some_spec, }, have hf_lim : ∀ x, filter.at_top.tendsto (λ n, ae_seq hf p n x) (𝓝 (f_lim x)), { intros x, simp only [f_lim, ae_seq], split_ifs, { rw funext (λ n, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h n), exact (hp_mem x h).some_spec, }, { exact tendsto_const_nhds, }, }, have h_ae_tendsto_f_lim : ∀ᵐ x ∂μ, filter.at_top.tendsto (λ n, f n x) (𝓝 (f_lim x)), { refine le_antisymm (le_of_eq (measure_mono_null _ hμ_compl)) (zero_le _), exact set.compl_subset_compl.mpr (λ x hx, hf_lim_conv x hx), }, have h_f_lim_meas : measurable f_lim, from measurable_of_tendsto_metric (ae_seq.measurable hf p) (tendsto_pi.mpr (λ x, hf_lim x)), exact ⟨f_lim, h_f_lim_meas, h_ae_tendsto_f_lim⟩, end end limits namespace continuous_linear_map variables {𝕜 : Type*} [normed_field 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] [measurable_space E] variables [opens_measurable_space E] variables {F : Type*} [normed_group F] [normed_space 𝕜 F] [measurable_space F] [borel_space F] protected lemma measurable (L : E →L[𝕜] F) : measurable L := L.continuous.measurable lemma measurable_comp (L : E →L[𝕜] F) {φ : α → E} (φ_meas : measurable φ) : measurable (λ (a : α), L (φ a)) := L.measurable.comp φ_meas end continuous_linear_map section normed_space variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] [measurable_space 𝕜] variables [borel_space 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] [measurable_space E] [borel_space E] lemma measurable_smul_const {f : α → 𝕜} {c : E} (hc : c ≠ 0) : measurable (λ x, f x • c) ↔ measurable f := measurable_comp_iff_of_closed_embedding (λ y : 𝕜, y • c) (closed_embedding_smul_left hc) lemma ae_measurable_smul_const {f : α → 𝕜} {μ : measure α} {c : E} (hc : c ≠ 0) : ae_measurable (λ x, f x • c) μ ↔ ae_measurable f μ := ae_measurable_comp_iff_of_closed_embedding (λ y : 𝕜, y • c) (closed_embedding_smul_left hc) end normed_space namespace measure_theory namespace measure variables [topological_space α] {μ : measure α} /-- A measure `μ` is regular if - it is finite on all compact sets; - it is outer regular: `μ(A) = inf { μ(U) | A ⊆ U open }` for `A` measurable; - it is inner regular: `μ(U) = sup { μ(K) | K ⊆ U compact }` for `U` open. -/ structure regular (μ : measure α) : Prop := (lt_top_of_is_compact : ∀ {{K : set α}}, is_compact K → μ K < ∞) (outer_regular : ∀ {{A : set α}}, measurable_set A → (⨅ (U : set α) (h : is_open U) (h2 : A ⊆ U), μ U) ≤ μ A) (inner_regular : ∀ {{U : set α}}, is_open U → μ U ≤ ⨆ (K : set α) (h : is_compact K) (h2 : K ⊆ U), μ K) namespace regular lemma outer_regular_eq (hμ : μ.regular) {{A : set α}} (hA : measurable_set A) : (⨅ (U : set α) (h : is_open U) (h2 : A ⊆ U), μ U) = μ A := le_antisymm (hμ.outer_regular hA) $ le_infi $ λ s, le_infi $ λ hs, le_infi $ λ h2s, μ.mono h2s lemma inner_regular_eq (hμ : μ.regular) {{U : set α}} (hU : is_open U) : (⨆ (K : set α) (h : is_compact K) (h2 : K ⊆ U), μ K) = μ U := le_antisymm (supr_le $ λ s, supr_le $ λ hs, supr_le $ λ h2s, μ.mono h2s) (hμ.inner_regular hU) lemma exists_compact_not_null (hμ : regular μ) : (∃ K, is_compact K ∧ μ K ≠ 0) ↔ μ ≠ 0 := by simp_rw [ne.def, ← measure_univ_eq_zero, ← hμ.inner_regular_eq is_open_univ, ennreal.supr_eq_zero, not_forall, exists_prop, subset_univ, true_and] protected lemma map [opens_measurable_space α] [measurable_space β] [topological_space β] [t2_space β] [borel_space β] (hμ : μ.regular) (f : α ≃ₜ β) : (measure.map f μ).regular := begin have hf := f.continuous.measurable, have h2f := f.to_equiv.injective.preimage_surjective, have h3f := f.to_equiv.surjective, split, { intros K hK, rw [map_apply hf hK.measurable_set], apply hμ.lt_top_of_is_compact, rwa f.compact_preimage }, { intros A hA, rw [map_apply hf hA, ← hμ.outer_regular_eq (hf hA)], refine le_of_eq _, apply infi_congr (preimage f) h2f, intro U, apply infi_congr_Prop f.is_open_preimage, intro hU, apply infi_congr_Prop h3f.preimage_subset_preimage_iff, intro h2U, rw [map_apply hf hU.measurable_set], }, { intros U hU, rw [map_apply hf hU.measurable_set, ← hμ.inner_regular_eq (hU.preimage f.continuous)], refine ge_of_eq _, apply supr_congr (preimage f) h2f, intro K, apply supr_congr_Prop f.compact_preimage, intro hK, apply supr_congr_Prop h3f.preimage_subset_preimage_iff, intro h2U, rw [map_apply hf hK.measurable_set] } end protected lemma smul (hμ : μ.regular) {x : ℝ≥0∞} (hx : x < ∞) : (x • μ).regular := begin split, { intros K hK, exact ennreal.mul_lt_top hx (hμ.lt_top_of_is_compact hK) }, { intros A hA, rw [coe_smul], refine le_trans _ (ennreal.mul_left_mono $ hμ.outer_regular hA), simp only [infi_and'], simp only [infi_subtype'], haveI : nonempty {s : set α // is_open s ∧ A ⊆ s} := ⟨⟨set.univ, is_open_univ, subset_univ _⟩⟩, rw [ennreal.mul_infi], refl', exact ne_of_lt hx }, { intros U hU, rw [coe_smul], refine le_trans (ennreal.mul_left_mono $ hμ.inner_regular hU) _, simp only [supr_and'], simp only [supr_subtype'], rw [ennreal.mul_supr], refl' } end /-- A regular measure in a σ-compact space is σ-finite. -/ protected lemma sigma_finite [opens_measurable_space α] [t2_space α] [sigma_compact_space α] (hμ : regular μ) : sigma_finite μ := ⟨⟨{ set := compact_covering α, set_mem := λ n, (is_compact_compact_covering α n).measurable_set, finite := λ n, hμ.lt_top_of_is_compact $ is_compact_compact_covering α n, spanning := Union_compact_covering α }⟩⟩ end regular end measure end measure_theory lemma is_compact.measure_lt_top_of_nhds_within [topological_space α] {s : set α} {μ : measure α} (h : is_compact s) (hμ : ∀ x ∈ s, μ.finite_at_filter (𝓝[s] x)) : μ s < ∞ := is_compact.induction_on h (by simp) (λ s t hst ht, (measure_mono hst).trans_lt ht) (λ s t hs ht, (measure_union_le s t).trans_lt (ennreal.add_lt_top.2 ⟨hs, ht⟩)) hμ lemma is_compact.measure_lt_top [topological_space α] {s : set α} {μ : measure α} [locally_finite_measure μ] (h : is_compact s) : μ s < ∞ := h.measure_lt_top_of_nhds_within $ λ x hx, μ.finite_at_nhds_within _ _
56b75c6a2c03496aa21647d2638b9e3b8ea24db4
3adda22358e3c0fbae44c6c35fdddbebf9358ef4
/src/Q3.lean
cb89ba9ed2c6a9ae40ff2fd962c7f18ef2695a2b
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/M1F-exam-may-2018
1539951b055cea5bac915bdb6fa1969e2f323402
8b5eca2037d4a14d6cfac3da1858b6c4119216d3
refs/heads/master
1,586,895,978,182
1,557,175,794,000
1,557,175,794,000
164,093,611
2
0
null
null
null
null
UTF-8
Lean
false
false
7,114
lean
import tactic.norm_num import data.fintype /- M1F May exam 2018, question 3. Solutions by Abhimanyu Pallavi Sudhir, part (d) generalised to all types of size 2 by KMB -/ universe u local attribute [instance, priority 0] classical.prop_decidable --QUESTION 3 variable {S : Type u} -- Q3(a)(i) -- answer variable (binary_relation : S → S → Prop) local infix ` ~ `:1000 := binary_relation -- Q3(a)(ii) def reflexivity := ∀ x, x ~ x def symmetry := ∀ (x y), x ~ y → y ~ x def transitivity := ∀ (x y z), x ~ y → y ~ z → x ~ z -- answer def is_equivalence := reflexivity binary_relation ∧ symmetry binary_relation ∧ transitivity binary_relation -- Q3(a)(iii) variable {binary_relation} -- answer def cl (h : is_equivalence binary_relation) (a : S) : set S := { x | x ~ a } -- Q3(b) theorem classes_injective2 (h : is_equivalence binary_relation) (a b : S) : (cl h a = cl h b) ∨ (cl h a ∩ cl h b) = ∅ := -- answer begin /-duplicate h so we can continue using it as a parameter to cl, then unpack hDupe-/ have hDupe : is_equivalence binary_relation := h, cases hDupe with hR hST, cases hST with hS hT, rw reflexivity at hR, rw symmetry at hS, rw transitivity at hT, /-if one of them is true (if they exclude) we don't need to bother-/ cases classical.em (cl h a ∩ cl h b = ∅) with excl intsct, --case excl right, exact excl, --case intsct left, /-prove that if something isn't empty it must have stuff in it-/ rw set.eq_empty_iff_forall_not_mem at intsct, rw not_forall_not at intsct, /-clean stuff up-/ cases intsct with x intsctX, cases intsctX with intsctXa intsctXb, rw cl at intsctXa, rw cl at intsctXb, change binary_relation x a at intsctXa, change binary_relation x b at intsctXb, rename intsctXa Hrxa, rename intsctXb Hrxb, rw cl, rw cl, /-now do the actual math-/ have Hrax : binary_relation a x, apply hS x a, exact Hrxa, have Hrab : binary_relation a b, apply hT a x b, exact Hrax, exact Hrxb, have Hrba : binary_relation b a, apply hS a b, exact Hrab, /-definition of set equivalence-/ apply set.eq_of_subset_of_subset, --split 1 /-clean things up again-/ intro y, intro Hrya, change binary_relation y a at Hrya, change binary_relation y b, /-do math again-/ apply hT y a b, exact Hrya, exact Hrab, --split 2 /-clean things up again-/ intro y, intro Hryb, change binary_relation y b at Hryb, change binary_relation y a, /-do math again-/ have Hrby : binary_relation b y, apply hS y b, exact Hryb, apply hT y b a, exact Hryb, exact Hrba, end -- Q3(c) inductive double_cosets : ℤ → ℤ → Prop | cond1 : ∀ x, double_cosets x (x + 3) | cond2 : ∀ x, double_cosets x (x - 5) | condT : ∀ x y z, double_cosets x y → double_cosets y z → double_cosets x z -- transitivity local infix ` ⋆ `:1001 := double_cosets theorem double_cosets_reflexive : reflexivity double_cosets := ---answer begin rw reflexivity, intro x, /-get some trivial things out of the way-/ have H0 : x - 5 - 5 - 5 + 3 + 3 + 3 + 3 + 3 = x, norm_num, /-start moving-/ have Hx5x : x ⋆ (x - 5), exact double_cosets.cond2 x, have H5x10x : (x - 5) ⋆ (x - 5 - 5), exact double_cosets.cond2 (x - 5), have H10x15x : (x - 5 - 5) ⋆ (x - 5 - 5 - 5), exact double_cosets.cond2 (x - 5 - 5), /-transitivity is hopper fare-/ have Hx10x : x ⋆ (x - 5 - 5), apply double_cosets.condT x (x - 5) (x - 5 - 5), exact Hx5x, exact H5x10x, have Hx15x : x ⋆ (x - 5 - 5 - 5), apply double_cosets.condT x (x - 5 - 5) (x - 5 - 5 - 5), exact Hx10x, exact H10x15x, /-now come back-/ have H15x12x : (x - 5 - 5 - 5) ⋆ (x - 5 - 5 - 5 + 3), exact double_cosets.cond1 (x - 5 - 5 - 5), have H12x9x : (x - 5 - 5 - 5 + 3) ⋆ (x - 5 - 5 - 5 + 3 + 3), exact double_cosets.cond1 (x - 5 - 5 - 5 + 3), have H9x6x : (x - 5 - 5 - 5 + 3 + 3) ⋆ (x - 5 - 5 - 5 + 3 + 3 + 3), exact double_cosets.cond1 (x - 5 - 5 - 5 + 3 + 3), have H6x3x : (x - 5 - 5 - 5 + 3 + 3 + 3) ⋆ (x - 5 - 5 - 5 + 3 + 3 + 3 + 3), exact double_cosets.cond1 (x - 5 - 5 - 5 + 3 + 3 + 3), have H3xx : (x - 5 - 5 - 5 + 3 + 3 + 3 + 3) ⋆ (x - 5 - 5 - 5 + 3 + 3 + 3 + 3 + 3), exact double_cosets.cond1 (x - 5 - 5 - 5 + 3 + 3 + 3 + 3), /-are we still within 1 hour?-/ have H15x9x : (x - 5 - 5 - 5) ⋆ (x - 5 - 5 - 5 + 3 + 3), apply double_cosets.condT (x - 5 - 5 - 5) (x - 5 - 5 - 5 + 3) (x - 5 - 5 - 5 + 3 + 3), exact H15x12x, exact H12x9x, have H15x6x : (x - 5 - 5 - 5) ⋆ (x - 5 - 5 - 5 + 3 + 3 + 3), apply double_cosets.condT (x - 5 - 5 - 5) (x - 5 - 5 - 5 + 3 + 3) (x - 5 - 5 - 5 + 3 + 3 + 3), exact H15x9x, exact H9x6x, have H15x3x : (x - 5 - 5 - 5) ⋆ (x - 5 - 5 - 5 + 3 + 3 + 3 + 3), apply double_cosets.condT (x - 5 - 5 - 5) (x - 5 - 5 - 5 + 3 + 3 + 3) (x - 5 - 5 - 5 + 3 + 3 + 3 + 3), exact H15x6x, exact H6x3x, have H15xx : (x - 5 - 5 - 5) ⋆ (x - 5 - 5 - 5 + 3 + 3 + 3 + 3 + 3), apply double_cosets.condT (x - 5 - 5 - 5) (x - 5 - 5 - 5 + 3 + 3 + 3 + 3) (x - 5 - 5 - 5 + 3 + 3 + 3 + 3 + 3), exact H15x3x, exact H3xx, have Hxx : x ⋆ (x - 5 - 5 - 5 + 3 + 3 + 3 + 3 + 3), apply double_cosets.condT x (x - 5 - 5 - 5) (x - 5 - 5 - 5 + 3 + 3 + 3 + 3 + 3), exact Hx15x, exact H15xx, /-show that we're back-/ rw H0 at Hxx, exact Hxx, end -- Q3(d) preparation -- first prove the result for `bool`, a concrete set with two elements theorem trans_of_refl_aux (r : bool → bool → Prop) (hr : reflexivity r) : transitivity r := begin rw transitivity, intros x y z, have Hxyyzzx : x = y ∨ y = z ∨ z = x, cases x; cases y; cases z; simp, rcases Hxyyzzx with rfl | rfl | rfl, { intros h hxz, exact hxz}, { intros hxy h, exact hxy}, { intros h1 h2, exact hr z} end -- now deduce it for an arbitrary set with two elements variable [fintype S] -- Q3(d) theorem trans_of_refl (h : fintype.card S = 2) (r : S → S → Prop) (hr : reflexivity r) : transitivity r := begin have Heq : S ≃ bool, { have h1 := trunc.out (fintype.equiv_fin S), rw h at h1, have h2 := trunc.out (fintype.equiv_fin bool), rw fintype.card_bool at h2, exact h1.trans h2.symm, }, -- transport needs to done manually, counterintuitive to mathematicians let rb : bool → bool → Prop := λ b1 b2, r (Heq.symm b1) (Heq.symm b2), have Hr : r = λ x y, rb (Heq x) (Heq y), { ext x y, congr'; simp }, have hrb : reflexivity rb, { intro s, exact hr (Heq.symm s) }, have htb : transitivity rb := trans_of_refl_aux rb hrb, intros x y z hxy hyz, rw Hr, apply htb (Heq x) (Heq y) (Heq z), convert hxy, rw Hr, convert hyz, rw Hr, end
00bb5b4d446a042136d1f612a568aa06ae872b54
367134ba5a65885e863bdc4507601606690974c1
/src/linear_algebra/dimension.lean
9fd7992fadf0545a205c79847aee153ae39fd67f
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
25,903
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro, Johannes Hölzl, Sander Dahmen -/ import linear_algebra.basis import linear_algebra.std_basis import set_theory.cardinal_ordinal /-! # Dimension of modules and vector spaces ## Main definitions * The dimension of a vector space is defined as `vector_space.dim : cardinal`. ## Main statements * `mk_eq_mk_of_basis`: the dimension theorem, any two bases of the same vector space have the same cardinality. * `dim_quotient_add_dim`: if V₁ is a submodule of V, then dim (V/V₁) + dim V₁ = dim V. * `dim_range_add_dim_ker`: the rank-nullity theorem. ## Implementation notes Many theorems in this file are not universe-generic when they relate dimensions in different universes. They should be as general as they can be without inserting `lift`s. The types `V`, `V'`, ... all live in different universes, and `V₁`, `V₂`, ... all live in the same universe. -/ noncomputable theory universes u v v' v'' u₁' w w' variables {K : Type u} {V V₁ V₂ V₃ : Type v} {V' V'₁ : Type v'} {V'' : Type v''} variables {ι : Type w} {ι' : Type w'} {η : Type u₁'} {φ : η → Type*} open_locale classical big_operators section vector_space variables [field K] [add_comm_group V] [vector_space K V] [add_comm_group V₁] [vector_space K V₁] include K open submodule function set variables (K V) /-- the dimension of a vector space, defined as a term of type `cardinal` -/ def vector_space.dim : cardinal := cardinal.min (nonempty_subtype.2 (@exists_is_basis K V _ _ _)) (λ b, cardinal.mk b.1) variables {K V} open vector_space section theorem is_basis.le_span {v : ι → V} {J : set V} (hv : is_basis K v) (hJ : span K J = ⊤) : cardinal.mk (range v) ≤ cardinal.mk J := begin cases le_or_lt cardinal.omega (cardinal.mk J) with oJ oJ, { have := cardinal.mk_range_eq_of_injective (linear_independent.injective hv.1), let S : J → set ι := λ j, ↑(is_basis.repr hv j).support, let S' : J → set V := λ j, v '' S j, have hs : range v ⊆ ⋃ j, S' j, { intros b hb, rcases mem_range.1 hb with ⟨i, hi⟩, have : span K J ≤ comap hv.repr (finsupp.supported K K (⋃ j, S j)) := span_le.2 (λ j hj x hx, ⟨_, ⟨⟨j, hj⟩, rfl⟩, hx⟩), rw hJ at this, replace : hv.repr (v i) ∈ (finsupp.supported K K (⋃ j, S j)) := this trivial, rw [hv.repr_eq_single, finsupp.mem_supported, finsupp.support_single_ne_zero one_ne_zero] at this, { subst b, rcases mem_Union.1 (this (finset.mem_singleton_self _)) with ⟨j, hj⟩, exact mem_Union.2 ⟨j, (mem_image _ _ _).2 ⟨i, hj, rfl⟩⟩ }, { apply_instance } }, refine le_of_not_lt (λ IJ, _), suffices : cardinal.mk (⋃ j, S' j) < cardinal.mk (range v), { exact not_le_of_lt this ⟨set.embedding_of_subset _ _ hs⟩ }, refine lt_of_le_of_lt (le_trans cardinal.mk_Union_le_sum_mk (cardinal.sum_le_sum _ (λ _, cardinal.omega) _)) _, { exact λ j, le_of_lt (cardinal.lt_omega_iff_finite.2 $ (finset.finite_to_set _).image _) }, { rwa [cardinal.sum_const, cardinal.mul_eq_max oJ (le_refl _), max_eq_left oJ] } }, { rcases exists_finite_card_le_of_finite_of_linear_independent_of_span (cardinal.lt_omega_iff_finite.1 oJ) hv.1.to_subtype_range _ with ⟨fI, hi⟩, { rwa [← cardinal.nat_cast_le, cardinal.finset_card, set.finite.coe_to_finset, cardinal.finset_card, set.finite.coe_to_finset] at hi, }, { rw hJ, apply set.subset_univ } }, end end /-- dimension theorem -/ theorem mk_eq_mk_of_basis {v : ι → V} {v' : ι' → V} (hv : is_basis K v) (hv' : is_basis K v') : cardinal.lift.{w w'} (cardinal.mk ι) = cardinal.lift.{w' w} (cardinal.mk ι') := begin rw ←cardinal.lift_inj.{(max w w') v}, rw [cardinal.lift_lift, cardinal.lift_lift], apply le_antisymm, { convert cardinal.lift_le.{v (max w w')}.2 (hv.le_span hv'.2), { rw cardinal.lift_max.{w v w'}, apply (cardinal.mk_range_eq_of_injective hv.injective).symm, }, { rw cardinal.lift_max.{w' v w}, apply (cardinal.mk_range_eq_of_injective hv'.injective).symm, }, }, { convert cardinal.lift_le.{v (max w w')}.2 (hv'.le_span hv.2), { rw cardinal.lift_max.{w' v w}, apply (cardinal.mk_range_eq_of_injective hv'.injective).symm, }, { rw cardinal.lift_max.{w v w'}, apply (cardinal.mk_range_eq_of_injective hv.injective).symm, }, } end theorem mk_eq_mk_of_basis' {ι' : Type w} {v : ι → V} {v' : ι' → V} (hv : is_basis K v) (hv' : is_basis K v') : cardinal.mk ι = cardinal.mk ι' := cardinal.lift_inj.1 $ mk_eq_mk_of_basis hv hv' theorem is_basis.mk_eq_dim'' {ι : Type v} {v : ι → V} (h : is_basis K v) : cardinal.mk ι = dim K V := begin obtain ⟨v', e : dim K V = _⟩ := cardinal.min_eq _ _, rw e, rw ← cardinal.mk_range_eq _ h.injective, exact mk_eq_mk_of_basis' h.range v'.2 end theorem is_basis.mk_range_eq_dim {v : ι → V} (h : is_basis K v) : cardinal.mk (range v) = dim K V := h.range.mk_eq_dim'' theorem is_basis.mk_eq_dim {v : ι → V} (h : is_basis K v) : cardinal.lift.{w v} (cardinal.mk ι) = cardinal.lift.{v w} (dim K V) := by rw [←h.mk_range_eq_dim, cardinal.mk_range_eq_of_injective h.injective] theorem {m} is_basis.mk_eq_dim' {v : ι → V} (h : is_basis K v) : cardinal.lift.{w (max v m)} (cardinal.mk ι) = cardinal.lift.{v (max w m)} (dim K V) := by simpa using h.mk_eq_dim theorem dim_le {n : ℕ} (H : ∀ s : finset V, linear_independent K (λ i : (↑s : set V), (i : V)) → s.card ≤ n) : dim K V ≤ n := let ⟨b, hb⟩ := exists_is_basis K V in hb.mk_eq_dim'' ▸ cardinal.card_le_of (λ s, @finset.card_map _ _ ⟨_, subtype.val_injective⟩ s ▸ H _ (by { refine hb.1.mono (λ y h, _), rw [finset.mem_coe, finset.mem_map] at h, rcases h with ⟨x, hx, rfl⟩, exact x.2 } )) variables [add_comm_group V'] [vector_space K V'] /-- Two linearly equivalent vector spaces have the same dimension, a version with different universes. -/ theorem linear_equiv.lift_dim_eq (f : V ≃ₗ[K] V') : cardinal.lift.{v v'} (dim K V) = cardinal.lift.{v' v} (dim K V') := let ⟨b, hb⟩ := exists_is_basis K V in calc cardinal.lift.{v v'} (dim K V) = cardinal.lift.{v v'} (cardinal.mk b) : congr_arg _ hb.mk_eq_dim''.symm ... = cardinal.lift.{v' v} (dim K V') : (f.is_basis hb).mk_eq_dim /-- Two linearly equivalent vector spaces have the same dimension. -/ theorem linear_equiv.dim_eq (f : V ≃ₗ[K] V₁) : dim K V = dim K V₁ := cardinal.lift_inj.1 f.lift_dim_eq /-- Two vector spaces are isomorphic if they have the same dimension. -/ theorem nonempty_linear_equiv_of_lift_dim_eq (cond : cardinal.lift.{v v'} (dim K V) = cardinal.lift.{v' v} (dim K V')) : nonempty (V ≃ₗ[K] V') := begin obtain ⟨B, h⟩ := exists_is_basis K V, obtain ⟨B', h'⟩ := exists_is_basis K V', have : cardinal.lift.{v v'} (cardinal.mk B) = cardinal.lift.{v' v} (cardinal.mk B'), by rw [h.mk_eq_dim'', cond, h'.mk_eq_dim''], exact (cardinal.lift_mk_eq.{v v' 0}.1 this).map (linear_equiv_of_is_basis h h') end /-- Two vector spaces are isomorphic if they have the same dimension. -/ theorem nonempty_linear_equiv_of_dim_eq (cond : dim K V = dim K V₁) : nonempty (V ≃ₗ[K] V₁) := nonempty_linear_equiv_of_lift_dim_eq $ congr_arg _ cond section variables (V V' V₁) /-- Two vector spaces are isomorphic if they have the same dimension. -/ def linear_equiv.of_lift_dim_eq (cond : cardinal.lift.{v v'} (dim K V) = cardinal.lift.{v' v} (dim K V')) : V ≃ₗ[K] V' := classical.choice (nonempty_linear_equiv_of_lift_dim_eq cond) /-- Two vector spaces are isomorphic if they have the same dimension. -/ def linear_equiv.of_dim_eq (cond : dim K V = dim K V₁) : V ≃ₗ[K] V₁ := classical.choice (nonempty_linear_equiv_of_dim_eq cond) end /-- Two vector spaces are isomorphic if and only if they have the same dimension. -/ theorem linear_equiv.nonempty_equiv_iff_lift_dim_eq : nonempty (V ≃ₗ[K] V') ↔ cardinal.lift.{v v'} (dim K V) = cardinal.lift.{v' v} (dim K V') := ⟨λ ⟨h⟩, linear_equiv.lift_dim_eq h, λ h, nonempty_linear_equiv_of_lift_dim_eq h⟩ /-- Two vector spaces are isomorphic if and only if they have the same dimension. -/ theorem linear_equiv.nonempty_equiv_iff_dim_eq : nonempty (V ≃ₗ[K] V₁) ↔ dim K V = dim K V₁ := ⟨λ ⟨h⟩, linear_equiv.dim_eq h, λ h, nonempty_linear_equiv_of_dim_eq h⟩ @[simp] lemma dim_bot : dim K (⊥ : submodule K V) = 0 := by letI := classical.dec_eq V; rw [← cardinal.lift_inj, ← (is_basis_empty (⊥ : submodule K V) not_nonempty_pempty).mk_eq_dim, cardinal.mk_pempty] @[simp] lemma dim_top : dim K (⊤ : submodule K V) = dim K V := linear_equiv.dim_eq (linear_equiv.of_top _ rfl) lemma dim_of_field (K : Type*) [field K] : dim K K = 1 := by rw [←cardinal.lift_inj, ← (@is_basis_singleton_one punit K _ _).mk_eq_dim, cardinal.mk_punit] lemma dim_span {v : ι → V} (hv : linear_independent K v) : dim K ↥(span K (range v)) = cardinal.mk (range v) := by rw [←cardinal.lift_inj, ← (is_basis_span hv).mk_eq_dim, cardinal.mk_range_eq_of_injective (@linear_independent.injective ι K V v _ _ _ _ hv)] lemma dim_span_set {s : set V} (hs : linear_independent K (λ x, x : s → V)) : dim K ↥(span K s) = cardinal.mk s := by { rw [← @set_of_mem_eq _ s, ← subtype.range_coe_subtype], exact dim_span hs } lemma {m} cardinal_lift_le_dim_of_linear_independent {ι : Type w} {v : ι → V} (hv : linear_independent K v) : cardinal.lift.{w (max v m)} (cardinal.mk ι) ≤ cardinal.lift.{v (max w m)} (dim K V) := begin obtain ⟨ι', v', is⟩ := exists_sum_is_basis hv, rw [← cardinal.lift_umax, ← cardinal.lift_umax.{v}], simpa using le_trans (cardinal.lift_mk_le.{w _ (max v m)}.2 ⟨@function.embedding.inl ι ι'⟩) (le_of_eq $ is_basis.mk_eq_dim'.{_ _ _ (max w m)} is), end lemma cardinal_le_dim_of_linear_independent {ι : Type v} {v : ι → V} (hv : linear_independent K v) : (cardinal.mk ι) ≤ (dim.{u v} K V) := by simpa using cardinal_lift_le_dim_of_linear_independent hv lemma cardinal_le_dim_of_linear_independent' {s : set V} (hs : linear_independent K (λ x, x : s → V)) : cardinal.mk s ≤ dim K V := begin -- extend s to a basis obtain ⟨b, ss, h⟩ := exists_subset_is_basis hs, rw [←h.mk_range_eq_dim, subtype.range_coe], apply cardinal.mk_le_of_injective (inclusion_injective ss), end lemma dim_span_le (s : set V) : dim K (span K s) ≤ cardinal.mk s := begin classical, rcases exists_linear_independent (linear_independent_empty K V) (set.empty_subset s) with ⟨b, hb, _, hsb, hlib⟩, have hsab : span K s = span K b, from span_eq_of_le _ hsb (span_le.2 (λ x hx, subset_span (hb hx))), convert cardinal.mk_le_mk_of_subset hb, rw [hsab, dim_span_set hlib] end lemma dim_span_of_finset (s : finset V) : dim K (span K (↑s : set V)) < cardinal.omega := calc dim K (span K (↑s : set V)) ≤ cardinal.mk (↑s : set V) : dim_span_le ↑s ... = s.card : by rw ←cardinal.finset_card ... < cardinal.omega : cardinal.nat_lt_omega _ theorem dim_prod : dim K (V × V₁) = dim K V + dim K V₁ := begin letI := classical.dec_eq V, letI := classical.dec_eq V₁, rcases exists_is_basis K V with ⟨b, hb⟩, rcases exists_is_basis K V₁ with ⟨c, hc⟩, rw [← cardinal.lift_inj, ← @is_basis.mk_eq_dim K (V × V₁) _ _ _ _ _ (is_basis_inl_union_inr hb hc), cardinal.lift_add, cardinal.lift_mk, ← hb.mk_eq_dim, ← hc.mk_eq_dim, cardinal.lift_mk, cardinal.lift_mk, cardinal.add_def (ulift b) (ulift c)], exact cardinal.lift_inj.1 (cardinal.lift_mk_eq.2 ⟨equiv.ulift.trans (equiv.sum_congr (@equiv.ulift b) (@equiv.ulift c)).symm ⟩), end theorem dim_quotient_add_dim (p : submodule K V) : dim K p.quotient + dim K p = dim K V := by classical; exact let ⟨f⟩ := quotient_prod_linear_equiv p in dim_prod.symm.trans f.dim_eq theorem dim_quotient_le (p : submodule K V) : dim K p.quotient ≤ dim K V := by { rw ← dim_quotient_add_dim p, exact self_le_add_right _ _ } /-- rank-nullity theorem -/ theorem dim_range_add_dim_ker (f : V →ₗ[K] V₁) : dim K f.range + dim K f.ker = dim K V := begin haveI := λ (p : submodule K V), classical.dec_eq p.quotient, rw [← f.quot_ker_equiv_range.dim_eq, dim_quotient_add_dim] end lemma dim_range_le (f : V →ₗ[K] V₁) : dim K f.range ≤ dim K V := by { rw ← dim_range_add_dim_ker f, exact self_le_add_right _ _ } lemma dim_map_le (f : V →ₗ V₁) (p : submodule K V) : dim K (p.map f) ≤ dim K p := begin have h := dim_range_le (f.comp (submodule.subtype p)), rwa [linear_map.range_comp, range_subtype] at h, end lemma dim_range_of_surjective (f : V →ₗ[K] V') (h : surjective f) : dim K f.range = dim K V' := begin refine linear_equiv.dim_eq (linear_equiv.of_bijective (submodule.subtype _) _ _), exact linear_map.ker_eq_bot.2 subtype.val_injective, rwa [range_subtype, linear_map.range_eq_top] end lemma dim_eq_of_surjective (f : V →ₗ[K] V₁) (h : surjective f) : dim K V = dim K V₁ + dim K f.ker := by rw [← dim_range_add_dim_ker f, ← dim_range_of_surjective f h] lemma dim_le_of_surjective (f : V →ₗ[K] V₁) (h : surjective f) : dim K V₁ ≤ dim K V := by { rw [dim_eq_of_surjective f h], refine self_le_add_right _ _ } lemma dim_eq_of_injective (f : V →ₗ[K] V₁) (h : injective f) : dim K V = dim K f.range := by rw [← dim_range_add_dim_ker f, linear_map.ker_eq_bot.2 h]; simp [dim_bot] lemma dim_submodule_le (s : submodule K V) : dim K s ≤ dim K V := by { rw ← dim_quotient_add_dim s, exact self_le_add_left _ _ } lemma dim_le_of_injective (f : V →ₗ[K] V₁) (h : injective f) : dim K V ≤ dim K V₁ := by { rw [dim_eq_of_injective f h], exact dim_submodule_le _ } lemma dim_le_of_submodule (s t : submodule K V) (h : s ≤ t) : dim K s ≤ dim K t := dim_le_of_injective (of_le h) $ assume ⟨x, hx⟩ ⟨y, hy⟩ eq, subtype.eq $ show x = y, from subtype.ext_iff_val.1 eq lemma linear_independent_le_dim {v : ι → V} (hv : linear_independent K v) : cardinal.lift.{w v} (cardinal.mk ι) ≤ cardinal.lift.{v w} (dim K V) := calc cardinal.lift.{w v} (cardinal.mk ι) = cardinal.lift.{v w} (cardinal.mk (set.range v)) : (cardinal.mk_range_eq_of_injective (linear_independent.injective hv)).symm ... = cardinal.lift.{v w} (dim K (submodule.span K (set.range v))) : by rw (dim_span hv).symm ... ≤ cardinal.lift.{v w} (dim K V) : cardinal.lift_le.2 (dim_submodule_le (submodule.span K _)) theorem {u₁} linear_independent_le_dim' {v : ι → V} (hs : linear_independent K v) : ((cardinal.mk ι).lift : cardinal.{(max w v u₁)}) ≤ ((vector_space.dim K V).lift : cardinal.{(max v w u₁)}) := cardinal.mk_range_eq_lift hs.injective ▸ dim_span hs ▸ cardinal.lift_le.2 (dim_submodule_le _) section variables [add_comm_group V₂] [vector_space K V₂] variables [add_comm_group V₃] [vector_space K V₃] open linear_map /-- This is mostly an auxiliary lemma for `dim_sup_add_dim_inf_eq`. -/ lemma dim_add_dim_split (db : V₂ →ₗ[K] V) (eb : V₃ →ₗ[K] V) (cd : V₁ →ₗ[K] V₂) (ce : V₁ →ₗ[K] V₃) (hde : ⊤ ≤ db.range ⊔ eb.range) (hgd : ker cd = ⊥) (eq : db.comp cd = eb.comp ce) (eq₂ : ∀d e, db d = eb e → (∃c, cd c = d ∧ ce c = e)) : dim K V + dim K V₁ = dim K V₂ + dim K V₃ := have hf : surjective (coprod db eb), begin refine (range_eq_top.1 $ top_unique $ _), rwa [← map_top, ← prod_top, map_coprod_prod] end, begin conv {to_rhs, rw [← dim_prod, dim_eq_of_surjective _ hf] }, congr' 1, apply linear_equiv.dim_eq, refine linear_equiv.of_bijective _ _ _, { refine cod_restrict _ (prod cd (- ce)) _, { assume c, simp only [add_eq_zero_iff_eq_neg, prod_apply, mem_ker, coprod_apply, neg_neg, map_neg, neg_apply], exact linear_map.ext_iff.1 eq c } }, { rw [ker_cod_restrict, ker_prod, hgd, bot_inf_eq] }, { rw [eq_top_iff, range_cod_restrict, ← map_le_iff_le_comap, map_top, range_subtype], rintros ⟨d, e⟩, have h := eq₂ d (-e), simp only [add_eq_zero_iff_eq_neg, prod_apply, mem_ker, mem_coe, prod.mk.inj_iff, coprod_apply, map_neg, neg_apply, linear_map.mem_range] at ⊢ h, assume hde, rcases h hde with ⟨c, h₁, h₂⟩, refine ⟨c, h₁, _⟩, rw [h₂, _root_.neg_neg] } end lemma dim_sup_add_dim_inf_eq (s t : submodule K V) : dim K (s ⊔ t : submodule K V) + dim K (s ⊓ t : submodule K V) = dim K s + dim K t := dim_add_dim_split (of_le le_sup_left) (of_le le_sup_right) (of_le inf_le_left) (of_le inf_le_right) begin rw [← map_le_map_iff' (ker_subtype $ s ⊔ t), map_sup, map_top, ← linear_map.range_comp, ← linear_map.range_comp, subtype_comp_of_le, subtype_comp_of_le, range_subtype, range_subtype, range_subtype], exact le_refl _ end (ker_of_le _ _ _) begin ext ⟨x, hx⟩, refl end begin rintros ⟨b₁, hb₁⟩ ⟨b₂, hb₂⟩ eq, have : b₁ = b₂ := congr_arg subtype.val eq, subst this, exact ⟨⟨b₁, hb₁, hb₂⟩, rfl, rfl⟩ end lemma dim_add_le_dim_add_dim (s t : submodule K V) : dim K (s ⊔ t : submodule K V) ≤ dim K s + dim K t := by { rw [← dim_sup_add_dim_inf_eq], exact self_le_add_right _ _ } end section fintype variable [fintype η] variables [∀i, add_comm_group (φ i)] [∀i, vector_space K (φ i)] open linear_map lemma dim_pi : vector_space.dim K (Πi, φ i) = cardinal.sum (λi, vector_space.dim K (φ i)) := begin choose b hb using assume i, exists_is_basis K (φ i), have : is_basis K (λ (ji : Σ j, b j), std_basis K (λ j, φ j) ji.fst ji.snd.val), by apply pi.is_basis_std_basis _ hb, rw [←cardinal.lift_inj, ← this.mk_eq_dim], simp [λ i, (hb i).mk_range_eq_dim.symm, cardinal.sum_mk] end lemma dim_fun {V η : Type u} [fintype η] [add_comm_group V] [vector_space K V] : vector_space.dim K (η → V) = fintype.card η * vector_space.dim K V := by rw [dim_pi, cardinal.sum_const, cardinal.fintype_card] lemma dim_fun_eq_lift_mul : vector_space.dim K (η → V) = (fintype.card η : cardinal.{max u₁' v}) * cardinal.lift.{v u₁'} (vector_space.dim K V) := by rw [dim_pi, cardinal.sum_const_eq_lift_mul, cardinal.fintype_card, cardinal.lift_nat_cast] lemma dim_fun' : vector_space.dim K (η → K) = fintype.card η := by rw [dim_fun_eq_lift_mul, dim_of_field K, cardinal.lift_one, mul_one, cardinal.nat_cast_inj] lemma dim_fin_fun (n : ℕ) : dim K (fin n → K) = n := by simp [dim_fun'] end fintype lemma exists_mem_ne_zero_of_ne_bot {s : submodule K V} (h : s ≠ ⊥) : ∃ b : V, b ∈ s ∧ b ≠ 0 := begin classical, by_contradiction hex, have : ∀x∈s, (x:V) = 0, { simpa only [not_exists, not_and, not_not, ne.def] using hex }, exact (h $ bot_unique $ assume s hs, (submodule.mem_bot K).2 $ this s hs) end lemma exists_mem_ne_zero_of_dim_pos {s : submodule K V} (h : 0 < vector_space.dim K s) : ∃ b : V, b ∈ s ∧ b ≠ 0 := exists_mem_ne_zero_of_ne_bot $ assume eq, by rw [eq, dim_bot] at h; exact lt_irrefl _ h lemma exists_is_basis_fintype (h : dim K V < cardinal.omega) : ∃ s : (set V), (is_basis K (subtype.val : s → V)) ∧ nonempty (fintype s) := begin cases exists_is_basis K V with s hs, rw [←cardinal.lift_lt, ← is_basis.mk_eq_dim hs, cardinal.lift_lt, cardinal.lt_omega_iff_fintype] at h, exact ⟨s, hs, h⟩ end section rank /-- `rank f` is the rank of a `linear_map f`, defined as the dimension of `f.range`. -/ def rank (f : V →ₗ[K] V') : cardinal := dim K f.range lemma rank_le_domain (f : V →ₗ[K] V₁) : rank f ≤ dim K V := by { rw [← dim_range_add_dim_ker f], exact self_le_add_right _ _ } lemma rank_le_range (f : V →ₗ[K] V₁) : rank f ≤ dim K V₁ := dim_submodule_le _ lemma rank_add_le (f g : V →ₗ[K] V') : rank (f + g) ≤ rank f + rank g := calc rank (f + g) ≤ dim K (f.range ⊔ g.range : submodule K V') : begin refine dim_le_of_submodule _ _ _, exact (linear_map.range_le_iff_comap.2 $ eq_top_iff'.2 $ assume x, show f x + g x ∈ (f.range ⊔ g.range : submodule K V'), from mem_sup.2 ⟨_, mem_image_of_mem _ (mem_univ _), _, mem_image_of_mem _ (mem_univ _), rfl⟩) end ... ≤ rank f + rank g : dim_add_le_dim_add_dim _ _ @[simp] lemma rank_zero : rank (0 : V →ₗ[K] V') = 0 := by rw [rank, linear_map.range_zero, dim_bot] lemma rank_finset_sum_le {η} (s : finset η) (f : η → V →ₗ[K] V') : rank (∑ d in s, f d) ≤ ∑ d in s, rank (f d) := @finset.sum_hom_rel _ _ _ _ _ (λa b, rank a ≤ b) f (λ d, rank (f d)) s (le_of_eq rank_zero) (λ i g c h, le_trans (rank_add_le _ _) (add_le_add_left h _)) variables [add_comm_group V''] [vector_space K V''] lemma rank_comp_le1 (g : V →ₗ[K] V') (f : V' →ₗ[K] V'') : rank (f.comp g) ≤ rank f := begin refine dim_le_of_submodule _ _ _, rw [linear_map.range_comp], exact image_subset _ (subset_univ _) end variables [add_comm_group V'₁] [vector_space K V'₁] lemma rank_comp_le2 (g : V →ₗ[K] V') (f : V' →ₗ V'₁) : rank (f.comp g) ≤ rank g := by rw [rank, rank, linear_map.range_comp]; exact dim_map_le _ _ end rank lemma dim_zero_iff_forall_zero : vector_space.dim K V = 0 ↔ ∀ x : V, x = 0 := begin split, { intros h x, cases exists_is_basis K V with w hw, have card_mk_range := hw.mk_range_eq_dim, rw [h, cardinal.mk_emptyc_iff, subtype.range_coe] at card_mk_range, simpa [card_mk_range] using hw.mem_span x }, { intro h, have : (⊤ : submodule K V) = ⊥, { ext x, simp [h x] }, rw [←dim_top, this, dim_bot] } end lemma dim_zero_iff : vector_space.dim K V = 0 ↔ subsingleton V := dim_zero_iff_forall_zero.trans (subsingleton_iff_forall_eq 0).symm lemma is_basis_of_dim_eq_zero {ι : Type*} (h : ¬ nonempty ι) (hV : dim K V = 0) : is_basis K (λ x : ι, (0 : V)) := begin haveI : subsingleton V := dim_zero_iff.1 hV, exact is_basis_empty _ h end lemma is_basis_of_dim_eq_zero' (hV : dim K V = 0) : is_basis K (λ x : fin 0, (0 : V)) := is_basis_of_dim_eq_zero (finset.univ_eq_empty.mp rfl) hV lemma dim_pos_iff_exists_ne_zero : 0 < vector_space.dim K V ↔ ∃ x : V, x ≠ 0 := begin rw ←not_iff_not, simpa using dim_zero_iff_forall_zero end lemma dim_pos_iff_nontrivial : 0 < vector_space.dim K V ↔ nontrivial V := begin rw dim_pos_iff_exists_ne_zero, split, { rintros ⟨x, h⟩, exact ⟨⟨x, 0, h⟩⟩ }, { introsI, exact exists_ne 0 } end lemma dim_pos [h : nontrivial V] : 0 < vector_space.dim K V := dim_pos_iff_nontrivial.2 h /-- A vector space has dimension at most `1` if and only if there is a single vector of which all vectors are multiples. -/ lemma dim_le_one_iff : dim K V ≤ 1 ↔ ∃ v₀ : V, ∀ v, ∃ r : K, r • v₀ = v := begin obtain ⟨b, h⟩ := exists_is_basis K V, split, { intro hd, rw [←is_basis.mk_eq_dim'' h, cardinal.le_one_iff_subsingleton, subsingleton_coe] at hd, rcases eq_empty_or_nonempty b with rfl | ⟨⟨v₀, hv₀⟩⟩, { use 0, have h' : ∀ v : V, v = 0, { simpa [submodule.eq_bot_iff] using h.2.symm }, intro v, simp [h' v] }, { use v₀, have h' : (K ∙ v₀) = ⊤, { simpa [hd.eq_singleton_of_mem hv₀] using h.2 }, intro v, have hv : v ∈ (⊤ : submodule K V) := mem_top, rwa [←h', mem_span_singleton] at hv } }, { rintros ⟨v₀, hv₀⟩, have h : (K ∙ v₀) = ⊤, { ext, simp [mem_span_singleton, hv₀] }, rw [←dim_top, ←h], convert dim_span_le _, simp } end /-- A submodule has dimension at most `1` if and only if there is a single vector in the submodule such that the submodule is contained in its span. -/ lemma dim_submodule_le_one_iff (s : submodule K V) : dim K s ≤ 1 ↔ ∃ v₀ ∈ s, s ≤ K ∙ v₀ := begin simp_rw [dim_le_one_iff, le_span_singleton_iff], split, { rintro ⟨⟨v₀, hv₀⟩, h⟩, use [v₀, hv₀], intros v hv, obtain ⟨r, hr⟩ := h ⟨v, hv⟩, use r, simp_rw [subtype.ext_iff, coe_smul, coe_mk] at hr, exact hr }, { rintro ⟨v₀, hv₀, h⟩, use ⟨v₀, hv₀⟩, rintro ⟨v, hv⟩, obtain ⟨r, hr⟩ := h v hv, use r, simp_rw [subtype.ext_iff, coe_smul, coe_mk], exact hr } end /-- A submodule has dimension at most `1` if and only if there is a single vector, not necessarily in the submodule, such that the submodule is contained in its span. -/ lemma dim_submodule_le_one_iff' (s : submodule K V) : dim K s ≤ 1 ↔ ∃ v₀, s ≤ K ∙ v₀ := begin rw dim_submodule_le_one_iff, split, { rintros ⟨v₀, hv₀, h⟩, exact ⟨v₀, h⟩ }, { rintros ⟨v₀, h⟩, by_cases hw : ∃ w : V, w ∈ s ∧ w ≠ 0, { rcases hw with ⟨w, hw, hw0⟩, use [w, hw], rcases mem_span_singleton.1 (h hw) with ⟨r', rfl⟩, have h0 : r' ≠ 0, { rintro rfl, simpa using hw0 }, rwa span_singleton_smul_eq _ h0 }, { push_neg at hw, rw ←submodule.eq_bot_iff at hw, simp [hw] } } end end vector_space section unconstrained_universes variables {E : Type v'} variables [field K] [add_comm_group V] [vector_space K V] [add_comm_group E] [vector_space K E] open vector_space /-- Version of linear_equiv.dim_eq without universe constraints. -/ theorem linear_equiv.dim_eq_lift (f : V ≃ₗ[K] E) : cardinal.lift.{v v'} (dim K V) = cardinal.lift.{v' v} (dim K E) := begin cases exists_is_basis K V with b hb, rw [← cardinal.lift_inj.1 hb.mk_eq_dim, ← (f.is_basis hb).mk_eq_dim, cardinal.lift_mk], end end unconstrained_universes
f0490172e8eec3bf65e2e7cff101bb69049a0eb1
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/ring_theory/subring.lean
ab97947a34a68e96aacf2ac021e74d50faf9ea93
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
8,270
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 := { exists_pair_ne := ⟨0, 1, mt subtype.ext_iff_val.1 zero_ne_one⟩, eq_zero_or_eq_zero_of_mul_eq_zero := λ ⟨x, hx⟩ ⟨y, hy⟩, by { simp only [subtype.ext_iff_val, 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, f.map_one], 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
093bc9e5abc8783e5b2b229c1ab54e4d2f726ea4
e2fc96178628c7451e998a0db2b73877d0648be5
/src/classes/unrestricted/normal_forms/kuroda.lean
cf71e0a6b20e07ae98fd8e0b01a634cec71dc4ca
[ "BSD-2-Clause" ]
permissive
madvorak/grammars
cd324ae19b28f7b8be9c3ad010ef7bf0fabe5df2
1447343a45fcb7821070f1e20b57288d437323a6
refs/heads/main
1,692,383,644,884
1,692,032,429,000
1,692,032,429,000
453,948,141
7
0
null
null
null
null
UTF-8
Lean
false
false
1,272
lean
import classes.unrestricted.basics.definition /-- Transformation rule for a grammar in the Kuroda normal form. -/ inductive kuroda_rule (T : Type) (N : Type) | two_two (A B C D : N) : kuroda_rule | one_two (A B C : N) : kuroda_rule | one_one (A : N) (t : T) : kuroda_rule | one_nil (A : N) : kuroda_rule /-- Grammar in the Kuroda normal form that generates words over the alphabet `T` (a type of terminals). -/ structure kuroda_grammar (T : Type) := (nt : Type) (initial : nt) (rules : list (kuroda_rule T nt)) def grule_of_kuroda_rule {T : Type} {N : Type} : kuroda_rule T N → grule T N | (kuroda_rule.two_two A B C D) := grule.mk [] A [symbol.nonterminal B] [symbol.nonterminal C, symbol.nonterminal D] | (kuroda_rule.one_two A B C) := grule.mk [] A [] [symbol.nonterminal B, symbol.nonterminal C] | (kuroda_rule.one_one A t) := grule.mk [] A [] [symbol.terminal t] | (kuroda_rule.one_nil A) := grule.mk [] A [] [] def grammar_of_kuroda_grammar {T : Type} (k : kuroda_grammar T) : grammar T := grammar.mk k.nt k.initial (list.map grule_of_kuroda_rule k.rules) theorem kuroda_grammar_always_exists {T : Type} (L : language T) : is_RE L → ∃ k : kuroda_grammar T, grammar_language (grammar_of_kuroda_grammar k) = L := sorry
5eee545e97d335c8f1d21b902f06fbb1662ae9e3
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/group/to_additive.lean
310d1a7ce4d8d41839c1926e36e735557caa985d
[ "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
28,225
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Yury Kudryashov, Floris van Doorn -/ import tactic.transform_decl import tactic.algebra import tactic.lint.basic import tactic.alias /-! # Transport multiplicative to additive This file defines an attribute `to_additive` that can be used to automatically transport theorems and definitions (but not inductive types and structures) from a multiplicative theory to an additive theory. Usage information is contained in the doc string of `to_additive.attr`. ### Missing features * Automatically transport structures and other inductive types. * For structures, automatically generate theorems like `group α ↔ add_group (additive α)`. -/ namespace to_additive open tactic setup_tactic_parser section performance_hack -- see Note [user attribute parameters] local attribute [semireducible] reflected /-- Temporarily change the `has_reflect` instance for `name`. -/ local attribute [instance, priority 9000] meta def hacky_name_reflect : has_reflect name := λ n, `(id %%(expr.const n []) : name) /-- An auxiliary attribute used to store the names of the additive versions of declarations that have been processed by `to_additive`. -/ @[user_attribute] meta def aux_attr : user_attribute (name_map name) name := { name := `to_additive_aux, descr := "Auxiliary attribute for `to_additive`. DON'T USE IT", parser := failed, cache_cfg := ⟨λ ns, ns.mfoldl (λ dict n', do let n := match n' with | name.mk_string s pre := if s = "_to_additive" then pre else n' | _ := n' end, param ← aux_attr.get_param_untyped n', pure $ dict.insert n param.app_arg.const_name) mk_name_map, []⟩ } end performance_hack section extra_attributes /-- An attribute that tells `@[to_additive]` that certain arguments of this definition are not involved when using `@[to_additive]`. This helps the heuristic of `@[to_additive]` by also transforming definitions if `ℕ` or another fixed type occurs as one of these arguments. -/ @[user_attribute] meta def ignore_args_attr : user_attribute (name_map $ list ℕ) (list ℕ) := { name := `to_additive_ignore_args, descr := "Auxiliary attribute for `to_additive` stating that certain arguments are not additivized.", cache_cfg := ⟨λ ns, ns.mfoldl (λ dict n, do param ← ignore_args_attr.get_param_untyped n, -- see Note [user attribute parameters] return $ dict.insert n (param.to_list expr.to_nat).iget) mk_name_map, []⟩, parser := (lean.parser.small_nat)* } /-- An attribute that is automatically added to declarations tagged with `@[to_additive]`, if needed. This attribute tells which argument is the type where this declaration uses the multiplicative structure. If there are multiple argument, we typically tag the first one. If this argument contains a fixed type, this declaration will note be additivized. See the Heuristics section of `to_additive.attr` for more details. If a declaration is not tagged, it is presumed that the first argument is relevant. `@[to_additive]` uses the function `to_additive.first_multiplicative_arg` to automatically tag declarations. It is ok to update it manually if the automatic tagging made an error. Implementation note: we only allow exactly 1 relevant argument, even though some declarations (like `prod.group`) have multiple arguments with a multiplicative structure on it. The reason is that whether we additivize a declaration is an all-or-nothing decision, and if we will not be able to additivize declarations that (e.g.) talk about multiplication on `ℕ × α` anyway. Warning: adding `@[to_additive_reorder]` with an equal or smaller number than the number in this attribute is currently not supported. -/ @[user_attribute] meta def relevant_arg_attr : user_attribute (name_map ℕ) ℕ := { name := `to_additive_relevant_arg, descr := "Auxiliary attribute for `to_additive` stating which arguments are the types with a " ++ "multiplicative structure.", cache_cfg := ⟨λ ns, ns.mfoldl (λ dict n, do param ← relevant_arg_attr.get_param_untyped n, -- see Note [user attribute parameters] -- we subtract 1 from the values provided by the user. return $ dict.insert n $ param.to_nat.iget.pred) mk_name_map, []⟩, parser := lean.parser.small_nat } /-- An attribute that stores all the declarations that needs their arguments reordered when applying `@[to_additive]`. Currently, we only support swapping consecutive arguments. The list of the natural numbers contains the positions of the first of the two arguments to be swapped. If the first two arguments are swapped, the first two universe variables are also swapped. Example: `@[to_additive_reorder 1 4]` swaps the first two arguments and the arguments in positions 4 and 5. -/ @[user_attribute] meta def reorder_attr : user_attribute (name_map $ list ℕ) (list ℕ) := { name := `to_additive_reorder, descr := "Auxiliary attribute for `to_additive` that stores arguments that need to be reordered.", cache_cfg := ⟨λ ns, ns.mfoldl (λ dict n, do param ← reorder_attr.get_param_untyped n, -- see Note [user attribute parameters] return $ dict.insert n (param.to_list expr.to_nat).iget) mk_name_map, []⟩, parser := do l ← (lean.parser.small_nat)*, guard (l.all (≠ 0)) <|> exceptional.fail "The reorder positions must be positive", return l } end extra_attributes /-- Find the first argument of `nm` that has a multiplicative type-class on it. Returns 1 if there are no types with a multiplicative class as arguments. E.g. `prod.group` returns 1, and `pi.has_one` returns 2. -/ meta def first_multiplicative_arg (nm : name) : tactic ℕ := do d ← get_decl nm, let (es, _) := d.type.pi_binders, l ← es.mmap_with_index $ λ n bi, do { let tgt := bi.type.pi_codomain, let n_bi := bi.type.pi_binders.fst.length, tt ← has_attribute' `to_additive tgt.get_app_fn.const_name | return none, let n2 := tgt.get_app_args.head.get_app_fn.match_var.map $ λ m, n + n_bi - m, return $ n2 }, let l := l.reduce_option, return $ if l = [] then 1 else l.foldr min l.head /-- A command that can be used to have future uses of `to_additive` change the `src` namespace to the `tgt` namespace. For example: ``` run_cmd to_additive.map_namespace `quotient_group `quotient_add_group ``` Later uses of `to_additive` on declarations in the `quotient_group` namespace will be created in the `quotient_add_group` namespaces. -/ meta def map_namespace (src tgt : name) : command := do let n := src.mk_string "_to_additive", let decl := declaration.thm n [] `(unit) (pure (reflect ())), add_decl decl, aux_attr.set n tgt tt /-- `value_type` is the type of the arguments that can be provided to `to_additive`. `to_additive.parser` parses the provided arguments: * `replace_all`: replace all multiplicative declarations, do not use the heuristic. * `trace`: output the generated additive declaration. * `tgt : name`: the name of the target (the additive declaration). * `doc`: an optional doc string. * if `allow_auto_name` is `ff` (default) then `@[to_additive]` will check whether the given name can be auto-generated. -/ @[derive has_reflect, derive inhabited] structure value_type : Type := (replace_all : bool) (trace : bool) (tgt : name) (doc : option string) (allow_auto_name : bool) /-- `add_comm_prefix x s` returns `"comm_" ++ s` if `x = tt` and `s` otherwise. -/ meta def add_comm_prefix : bool → string → string | tt s := "comm_" ++ s | ff s := s /-- Dictionary used by `to_additive.guess_name` to autogenerate names. -/ meta def tr : bool → list string → list string | is_comm ("one" :: "le" :: s) := add_comm_prefix is_comm "nonneg" :: tr ff s | is_comm ("one" :: "lt" :: s) := add_comm_prefix is_comm "pos" :: tr ff s | is_comm ("le" :: "one" :: s) := add_comm_prefix is_comm "nonpos" :: tr ff s | is_comm ("lt" :: "one" :: s) := add_comm_prefix is_comm "neg" :: tr ff s | is_comm ("mul" :: "single" :: s) := add_comm_prefix is_comm "single" :: tr ff s | is_comm ("mul" :: "support" :: s) := add_comm_prefix is_comm "support" :: tr ff s | is_comm ("mul" :: "tsupport" :: s) := add_comm_prefix is_comm "tsupport" :: tr ff s | is_comm ("mul" :: "indicator" :: s) := add_comm_prefix is_comm "indicator" :: tr ff s | is_comm ("mul" :: s) := add_comm_prefix is_comm "add" :: tr ff s | is_comm ("smul" :: s) := add_comm_prefix is_comm "vadd" :: tr ff s | is_comm ("inv" :: s) := add_comm_prefix is_comm "neg" :: tr ff s | is_comm ("div" :: s) := add_comm_prefix is_comm "sub" :: tr ff s | is_comm ("one" :: s) := add_comm_prefix is_comm "zero" :: tr ff s | is_comm ("prod" :: s) := add_comm_prefix is_comm "sum" :: tr ff s | is_comm ("finprod" :: s) := add_comm_prefix is_comm "finsum" :: tr ff s | is_comm ("pow" :: s) := add_comm_prefix is_comm "nsmul" :: tr ff s | is_comm ("npow" :: s) := add_comm_prefix is_comm "nsmul" :: tr ff s | is_comm ("zpow" :: s) := add_comm_prefix is_comm "zsmul" :: tr ff s | is_comm ("is" :: "square" :: s) := add_comm_prefix is_comm "even" :: tr ff s | is_comm ("is" :: "scalar" :: "tower" :: s) := add_comm_prefix is_comm "vadd_assoc_class" :: tr ff s | is_comm ("is" :: "regular" :: s) := add_comm_prefix is_comm "is_add_regular" :: tr ff s | is_comm ("is" :: "left" :: "regular" :: s) := add_comm_prefix is_comm "is_add_left_regular" :: tr ff s | is_comm ("is" :: "right" :: "regular" :: s) := add_comm_prefix is_comm "is_add_right_regular" :: tr ff s | is_comm ("monoid" :: s) := ("add_" ++ add_comm_prefix is_comm "monoid") :: tr ff s | is_comm ("submonoid" :: s) := ("add_" ++ add_comm_prefix is_comm "submonoid") :: tr ff s | is_comm ("group" :: s) := ("add_" ++ add_comm_prefix is_comm "group") :: tr ff s | is_comm ("subgroup" :: s) := ("add_" ++ add_comm_prefix is_comm "subgroup") :: tr ff s | is_comm ("semigroup" :: s) := ("add_" ++ add_comm_prefix is_comm "semigroup") :: tr ff s | is_comm ("magma" :: s) := ("add_" ++ add_comm_prefix is_comm "magma") :: tr ff s | is_comm ("haar" :: s) := ("add_" ++ add_comm_prefix is_comm "haar") :: tr ff s | is_comm ("prehaar" :: s) := ("add_" ++ add_comm_prefix is_comm "prehaar") :: tr ff s | is_comm ("unit" :: s) := ("add_" ++ add_comm_prefix is_comm "unit") :: tr ff s | is_comm ("units" :: s) := ("add_" ++ add_comm_prefix is_comm "units") :: tr ff s | is_comm ("comm" :: s) := tr tt s | is_comm ("root" :: s) := add_comm_prefix is_comm "div" :: tr ff s | is_comm ("rootable" :: s) := add_comm_prefix is_comm "divisible" :: tr ff s | is_comm (x :: s) := (add_comm_prefix is_comm x :: tr ff s) | tt [] := ["comm"] | ff [] := [] /-- Autogenerate target name for `to_additive`. -/ meta def guess_name : string → string := string.map_tokens ''' $ λ s, string.intercalate (string.singleton '_') $ tr ff (s.split_on '_') /-- Return the provided target name or autogenerate one if one was not provided. -/ meta def target_name (src tgt : name) (dict : name_map name) (allow_auto_name : bool) : tactic name := (if tgt.get_prefix ≠ name.anonymous ∨ allow_auto_name -- `tgt` is a full name then pure tgt else match src with | (name.mk_string s pre) := do let tgt_auto := guess_name s, guard (tgt.to_string ≠ tgt_auto ∨ tgt = src) <|> trace ("`to_additive " ++ src.to_string ++ "`: correctly autogenerated target " ++ "name, you may remove the explicit " ++ tgt_auto ++ " argument."), pure $ name.mk_string (if tgt = name.anonymous then tgt_auto else tgt.to_string) (pre.map_prefix dict.find) | _ := fail ("to_additive: can't transport " ++ src.to_string) end) >>= (λ res, if res = src ∧ tgt ≠ src then fail ("to_additive: can't transport " ++ src.to_string ++ " to itself. Give the desired additive name explicitly using `@[to_additive additive_name]`. ") else pure res) /-- the parser for the arguments to `to_additive`. -/ meta def parser : lean.parser value_type := do bang ← option.is_some <$> (tk "!")?, ques ← option.is_some <$> (tk "?")?, tgt ← ident?, e ← texpr?, doc ← match e with | some pe := some <$> ((to_expr pe >>= eval_expr string) : tactic string) | none := pure none end, return ⟨bang, ques, tgt.get_or_else name.anonymous, doc, ff⟩ private meta def proceed_fields_aux (src tgt : name) (prio : ℕ) (f : name → tactic (list string)) : command := do src_fields ← f src, tgt_fields ← f tgt, guard (src_fields.length = tgt_fields.length) <|> fail ("Failed to map fields of " ++ src.to_string), (src_fields.zip tgt_fields).mmap' $ λ names, guard (names.fst = names.snd) <|> aux_attr.set (src.append names.fst) (tgt.append names.snd) tt prio /-- Add the `aux_attr` attribute to the structure fields of `src` so that future uses of `to_additive` will map them to the corresponding `tgt` fields. -/ meta def proceed_fields (env : environment) (src tgt : name) (prio : ℕ) : command := let aux := proceed_fields_aux src tgt prio in do aux (λ n, pure $ list.map name.to_string $ (env.structure_fields n).get_or_else []) >> aux (λ n, (list.map (λ (x : name), "to_" ++ x.to_string) <$> get_tagged_ancestors n)) >> aux (λ n, (env.constructors_of n).mmap $ λ cs, match cs with | (name.mk_string s pre) := (guard (pre = n) <|> fail "Bad constructor name") >> pure s | _ := fail "Bad constructor name" end) /-- The attribute `to_additive` can be used to automatically transport theorems and definitions (but not inductive types and structures) from a multiplicative theory to an additive theory. To use this attribute, just write: ``` @[to_additive] theorem mul_comm' {α} [comm_semigroup α] (x y : α) : x * y = y * x := comm_semigroup.mul_comm ``` This code will generate a theorem named `add_comm'`. It is also possible to manually specify the name of the new declaration: ``` @[to_additive add_foo] theorem foo := sorry ``` An existing documentation string will _not_ be automatically used, so if the theorem or definition has a doc string, a doc string for the additive version should be passed explicitly to `to_additive`. ``` /-- Multiplication is commutative -/ @[to_additive "Addition is commutative"] theorem mul_comm' {α} [comm_semigroup α] (x y : α) : x * y = y * x := comm_semigroup.mul_comm ``` The transport tries to do the right thing in most cases using several heuristics described below. However, in some cases it fails, and requires manual intervention. If the declaration to be transported has attributes which need to be copied to the additive version, then `to_additive` should come last: ``` @[simp, to_additive] lemma mul_one' {G : Type*} [group G] (x : G) : x * 1 = x := mul_one x ``` The following attributes are supported and should be applied correctly by `to_additive` to the new additivized declaration, if they were present on the original one: ``` reducible, _refl_lemma, simp, norm_cast, instance, refl, symm, trans, elab_as_eliminator, no_rsimp, continuity, ext, ematch, measurability, alias, _ext_core, _ext_lemma_core, nolint ``` The exception to this rule is the `simps` attribute, which should come after `to_additive`: ``` @[to_additive, simps] instance {M N} [has_mul M] [has_mul N] : has_mul (M × N) := ⟨λ p q, ⟨p.1 * q.1, p.2 * q.2⟩⟩ ``` Additionally the `mono` attribute is not handled by `to_additive` and should be applied afterwards to both the original and additivized lemma. ## Implementation notes The transport process generally works by taking all the names of identifiers appearing in the name, type, and body of a declaration and creating a new declaration by mapping those names to additive versions using a simple string-based dictionary and also using all declarations that have previously been labeled with `to_additive`. In the `mul_comm'` example above, `to_additive` maps: * `mul_comm'` to `add_comm'`, * `comm_semigroup` to `add_comm_semigroup`, * `x * y` to `x + y` and `y * x` to `y + x`, and * `comm_semigroup.mul_comm'` to `add_comm_semigroup.add_comm'`. ### Heuristics `to_additive` uses heuristics to determine whether a particular identifier has to be mapped to its additive version. The basic heuristic is * Only map an identifier to its additive version if its first argument doesn't contain any unapplied identifiers. Examples: * `@has_mul.mul ℕ n m` (i.e. `(n * m : ℕ)`) will not change to `+`, since its first argument is `ℕ`, an identifier not applied to any arguments. * `@has_mul.mul (α × β) x y` will change to `+`. It's first argument contains only the identifier `prod`, but this is applied to arguments, `α` and `β`. * `@has_mul.mul (α × ℤ) x y` will not change to `+`, since its first argument contains `ℤ`. The reasoning behind the heuristic is that the first argument is the type which is "additivized", and this usually doesn't make sense if this is on a fixed type. There are some exceptions to this heuristic: * Identifiers that have the `@[to_additive]` attribute are ignored. For example, multiplication in `↥Semigroup` is replaced by addition in `↥AddSemigroup`. * If an identifier `d` has attribute `@[to_additive_relevant_arg n]` then the argument in position `n` is checked for a fixed type, instead of checking the first argument. `@[to_additive]` will automatically add the attribute `@[to_additive_relevant_arg n]` to a declaration when the first argument has no multiplicative type-class, but argument `n` does. * If an identifier has attribute `@[to_additive_ignore_args n1 n2 ...]` then all the arguments in positions `n1`, `n2`, ... will not be checked for unapplied identifiers (start counting from 1). For example, `cont_mdiff_map` has attribute `@[to_additive_ignore_args 21]`, which means that its 21st argument `(n : ℕ∞)` can contain `ℕ` (usually in the form `has_top.top ℕ ...`) and still be additivized. So `@has_mul.mul (C^∞⟮I, N; I', G⟯) _ f g` will be additivized. ### Troubleshooting If `@[to_additive]` fails because the additive declaration raises a type mismatch, there are various things you can try. The first thing to do is to figure out what `@[to_additive]` did wrong by looking at the type mismatch error. * Option 1: It additivized a declaration `d` that should remain multiplicative. Solution: * Make sure the first argument of `d` is a type with a multiplicative structure. If not, can you reorder the (implicit) arguments of `d` so that the first argument becomes a type with a multiplicative structure (and not some indexing type)? The reason is that `@[to_additive]` doesn't additivize declarations if their first argument contains fixed types like `ℕ` or `ℝ`. See section Heuristics. If the first argument is not the argument with a multiplicative type-class, `@[to_additive]` should have automatically added the attribute `@[to_additive_relevant_arg]` to the declaration. You can test this by running the following (where `d` is the full name of the declaration): ``` run_cmd to_additive.relevant_arg_attr.get_param `d >>= tactic.trace ``` The expected output is `n` where the `n`-th argument of `d` is a type (family) with a multiplicative structure on it. If you get a different output (or a failure), you could add the attribute `@[to_additive_relevant_arg n]` manually, where `n` is an argument with a multiplicative structure. * Option 2: It didn't additivize a declaration that should be additivized. This happened because the heuristic applied, and the first argument contains a fixed type, like `ℕ` or `ℝ`. Solutions: * If the fixed type has an additive counterpart (like `↥Semigroup`), give it the `@[to_additive]` attribute. * If the fixed type occurs inside the `k`-th argument of a declaration `d`, and the `k`-th argument is not connected to the multiplicative structure on `d`, consider adding attribute `[to_additive_ignore_args k]` to `d`. * If you want to disable the heuristic and replace all multiplicative identifiers with their additive counterpart, use `@[to_additive!]`. * Option 3: Arguments / universe levels are incorrectly ordered in the additive version. This likely only happens when the multiplicative declaration involves `pow`/`^`. Solutions: * Ensure that the order of arguments of all relevant declarations are the same for the multiplicative and additive version. This might mean that arguments have an "unnatural" order (e.g. `monoid.npow n x` corresponds to `x ^ n`, but it is convenient that `monoid.npow` has this argument order, since it matches `add_monoid.nsmul n x`. * If this is not possible, add the `[to_additive_reorder k]` to the multiplicative declaration to indicate that the `k`-th and `(k+1)`-st arguments are reordered in the additive version. If neither of these solutions work, and `to_additive` is unable to automatically generate the additive version of a declaration, manually write and prove the additive version. Often the proof of a lemma/theorem can just be the multiplicative version of the lemma applied to `multiplicative G`. Afterwards, apply the attribute manually: ``` attribute [to_additive foo_add_bar] foo_bar ``` This will allow future uses of `to_additive` to recognize that `foo_bar` should be replaced with `foo_add_bar`. ### Handling of hidden definitions Before transporting the “main” declaration `src`, `to_additive` first scans its type and value for names starting with `src`, and transports them. This includes auxiliary definitions like `src._match_1`, `src._proof_1`. In addition to transporting the “main” declaration, `to_additive` transports its equational lemmas and tags them as equational lemmas for the new declaration, attributes present on the original equational lemmas are also transferred first (notably `_refl_lemma`). ### Structure fields and constructors If `src` is a structure, then `to_additive` automatically adds structure fields to its mapping, and similarly for constructors of inductive types. For new structures this means that `to_additive` automatically handles coercions, and for old structures it does the same, if ancestry information is present in `@[ancestor]` attributes. The `ancestor` attribute must come before the `to_additive` attribute, and it is essential that the order of the base structures passed to `ancestor` matches between the multiplicative and additive versions of the structure. ### Name generation * If `@[to_additive]` is called without a `name` argument, then the new name is autogenerated. First, it takes the longest prefix of the source name that is already known to `to_additive`, and replaces this prefix with its additive counterpart. Second, it takes the last part of the name (i.e., after the last dot), and replaces common name parts (“mul”, “one”, “inv”, “prod”) with their additive versions. * Namespaces can be transformed using `map_namespace`. For example: ``` run_cmd to_additive.map_namespace `quotient_group `quotient_add_group ``` Later uses of `to_additive` on declarations in the `quotient_group` namespace will be created in the `quotient_add_group` namespaces. * If `@[to_additive]` is called with a `name` argument `new_name` /without a dot/, then `to_additive` updates the prefix as described above, then replaces the last part of the name with `new_name`. * If `@[to_additive]` is called with a `name` argument `new_namespace.new_name` /with a dot/, then `to_additive` uses this new name as is. As a safety check, in the first case `to_additive` double checks that the new name differs from the original one. -/ @[user_attribute] protected meta def attr : user_attribute unit value_type := { name := `to_additive, descr := "Transport multiplicative to additive", parser := parser, after_set := some $ λ src prio persistent, do guard persistent <|> fail "`to_additive` can't be used as a local attribute", env ← get_env, val ← attr.get_param src, dict ← aux_attr.get_cache, ignore ← ignore_args_attr.get_cache, relevant ← relevant_arg_attr.get_cache, reorder ← reorder_attr.get_cache, tgt ← target_name src val.tgt dict val.allow_auto_name, aux_attr.set src tgt tt, let dict := dict.insert src tgt, first_mult_arg ← first_multiplicative_arg src, when (first_mult_arg ≠ 1) $ relevant_arg_attr.set src first_mult_arg tt, if env.contains tgt then proceed_fields env src tgt prio else do transform_decl_with_prefix_dict dict val.replace_all val.trace relevant ignore reorder src tgt [`reducible, `_refl_lemma, `simp, `norm_cast, `instance, `refl, `symm, `trans, `elab_as_eliminator, `no_rsimp, `continuity, `ext, `ematch, `measurability, `alias, `_ext_core, `_ext_lemma_core, `nolint, `protected], mwhen (has_attribute' `simps src) (trace "Apply the simps attribute after the to_additive attribute"), mwhen (has_attribute' `mono src) (trace $ "to_additive does not work with mono, apply the mono attribute to both" ++ "versions after"), match val.doc with | some doc := add_doc_string tgt doc | none := do some alias_target ← tactic.alias.get_alias_target src | skip, let alias_name := alias_target.to_name, some add_alias_name ← pure (dict.find alias_name) | skip, add_doc_string tgt alias_target.to_string end } add_tactic_doc { name := "to_additive", category := doc_category.attr, decl_names := [`to_additive.attr], tags := ["transport", "environment", "lemma derivation"] } end to_additive /- map operations -/ attribute [to_additive] has_mul has_one has_inv has_div /- the following types are supported by `@[to_additive]` and mapped to themselves. -/ attribute [to_additive empty] empty attribute [to_additive pempty] pempty attribute [to_additive punit] punit attribute [to_additive unit] unit section linter open tactic expr /-- A linter that checks that multiplicative and additive lemmas have both doc strings if one of them has one -/ @[linter] meta def linter.to_additive_doc : linter := { test := (λ d, do let mul_name := d.to_name, dict ← to_additive.aux_attr.get_cache, match dict.find mul_name with | some add_name := do mul_doc ← try_core $ doc_string mul_name, add_doc ← try_core $ doc_string add_name, match mul_doc.is_some, add_doc.is_some with | tt, ff := return $ some $ "declaration has a docstring, but its additive version `" ++ add_name.to_string ++ "` does not. You might want to pass a string argument to " ++ "`to_additive`." | ff, tt := return $ some $ "declaration has no docstring, but its additive version `" ++ add_name.to_string ++ "` does. You might want to add a doc string to the declaration." | _, _ := return none end | none := return none end), auto_decls := ff, no_errors_found := "Multiplicative and additive lemmas are consistently documented", errors_found := "The following declarations have doc strings, but their additive versions do " ++ "not (or vice versa).", is_fast := ff } end linter
db57ccd644a53c336f426fcb99592635471a540c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/congr.lean
7fa453083b5d89ec6f17ab2d45d20c507749c090
[]
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
5,139
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.lint.default import Mathlib.tactic.ext import Mathlib.PostPort namespace Mathlib /-! # Congruence and related tactics This file contains the tactic `congr'`, which is an extension of `congr`, and various tactics using `congr'` internally. `congr'` has some advantages over `congr`: * It turns `↔` to equalities, before trying another congr lemma * You can write `congr' n` to give the maximal depth of recursive applications. This is useful if `congr` breaks down the goal to aggressively, and the resulting goals are false. * You can write `congr' with ...` to do `congr', ext ...` in a single tactic. Other tactics in this file: * `rcongr`: repeatedly apply `congr'` and `ext.` * `convert`: like `exact`, but produces an equality goal if the type doesn't match. * `convert_to`: changes the goal, if you prove an equality between the old goal and the new goal. * `ac_change`: like `convert_to`, but uses `ac_refl` to discharge the goals. -/ namespace tactic /-- Apply the constant `iff_of_eq` to the goal. -/ /-- The main part of the body for the loop in `congr'`. This will try to replace a goal `f x = f y` with `x = y`. Also has support for `==` and `↔`. -/ /-- The main function in `convert_to`. Changes the goal to `r` and a proof obligation that the goal is equal to `r`. -/ /-- Same as the `congr` tactic, but takes an optional argument which gives the depth of recursive applications. * This is useful when `congr` is too aggressive in breaking down the goal. * For example, given `⊢ f (g (x + y)) = f (g (y + x))`, `congr'` produces the goals `⊢ x = y` and `⊢ y = x`, while `congr' 2` produces the intended `⊢ x + y = y + x`. * If, at any point, a subgoal matches a hypothesis then the subgoal will be closed. -/ namespace interactive /-- Same as the `congr` tactic, but takes an optional argument which gives the depth of recursive applications. * This is useful when `congr` is too aggressive in breaking down the goal. * For example, given `⊢ f (g (x + y)) = f (g (y + x))`, `congr'` produces the goals `⊢ x = y` and `⊢ y = x`, while `congr' 2` produces the intended `⊢ x + y = y + x`. * If, at any point, a subgoal matches a hypothesis then the subgoal will be closed. * You can use `congr' with p (: n)?` to call `ext p (: n)?` to all subgoals generated by `congr'`. For example, if the goal is `⊢ f '' s = g '' s` then `congr' with x` generates the goal `x : α ⊢ f x = g x`. -/ /-- Repeatedly and apply `congr'` and `ext`, using the the given patterns as arguments for `ext`. There are two ways this tactic stops: * `congr'` fails (makes no progress), after having already applied `ext`. * `congr'` canceled out the last usage of `ext`. In this case, the state is reverted to before the `congr'` was applied. For example, when the goal is ```lean ⊢ (λ x, f x + 3) '' s = (λ x, g x + 3) '' s ``` then `rcongr x` produces the goal ```lean x : α ⊢ f x = g x ``` This gives the same result as `congr', ext x, congr'`. In contrast, `congr'` would produce ```lean ⊢ (λ x, f x + 3) = (λ x, g x + 3) ``` and `congr' with x` (or `congr', ext x`) would produce ```lean x : α ⊢ f x + 3 = g x + 3 ``` -/ /-- The `exact e` and `refine e` tactics require a term `e` whose type is definitionally equal to the goal. `convert e` is similar to `refine e`, but the type of `e` is not required to exactly match the goal. Instead, new goals are created for differences between the type of `e` and the goal. For example, in the proof state ```lean n : ℕ, e : prime (2 * n + 1) ⊢ prime (n + n + 1) ``` the tactic `convert e` will change the goal to ```lean ⊢ n + n = 2 * n ``` In this example, the new goal can be solved using `ring`. If `x y : t`, and an instance `subsingleton t` is in scope, then any goals of the form `x = y` are solved automatically. The syntax `convert ← e` will reverse the direction of the new goals (producing `⊢ 2 * n = n + n` in this example). Internally, `convert e` works by creating a new goal asserting that the goal equals the type of `e`, then simplifying it using `congr'`. The syntax `convert e using n` can be used to control the depth of matching (like `congr' n`). In the example, `convert e using 1` would produce a new goal `⊢ n + n + 1 = 2 * n + 1`. -/ /-- `convert_to g using n` attempts to change the current goal to `g`, but unlike `change`, it will generate equality proof obligations using `congr' n` to resolve discrepancies. `convert_to g` defaults to using `congr' 1`. `ac_change` is `convert_to` followed by `ac_refl`. It is useful for rearranging/reassociating e.g. sums: ```lean example (a b c d e f g N : ℕ) : (a + b) + (c + d) + (e + f) + g ≤ N := begin ac_change a + d + e + f + c + g + b ≤ _, -- ⊢ a + d + e + f + c + g + b ≤ N -- ⊢ a + d + e + f + c + g + b ≤ N end ``` -/ /-- `ac_change g using n` is `convert_to g using n; try {ac_refl}`. -/
70704df57788866b2bc445f13a54c20753bf8479
bb31430994044506fa42fd667e2d556327e18dfe
/src/group_theory/group_action/opposite.lean
e66543bb1f8fc05e93a7cec529bfc75e10aab423
[ "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
5,947
lean
/- Copyright (c) 2020 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import algebra.group.opposite import group_theory.group_action.defs /-! # Scalar actions on and by `Mᵐᵒᵖ` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the actions on the opposite type `has_smul R Mᵐᵒᵖ`, and actions by the opposite type, `has_smul Rᵐᵒᵖ M`. Note that `mul_opposite.has_smul` is provided in an earlier file as it is needed to provide the `add_monoid.nsmul` and `add_comm_group.gsmul` fields. -/ variables (α : Type*) /-! ### Actions _on_ the opposite type Actions on the opposite type just act on the underlying type. -/ namespace mul_opposite @[to_additive] instance (R : Type*) [monoid R] [mul_action R α] : mul_action R αᵐᵒᵖ := { one_smul := λ x, unop_injective $ one_smul R (unop x), mul_smul := λ r₁ r₂ x, unop_injective $ mul_smul r₁ r₂ (unop x), .. mul_opposite.has_smul α R } instance (R : Type*) [monoid R] [add_monoid α] [distrib_mul_action R α] : distrib_mul_action R αᵐᵒᵖ := { smul_add := λ r x₁ x₂, unop_injective $ smul_add r (unop x₁) (unop x₂), smul_zero := λ r, unop_injective $ smul_zero r, .. mul_opposite.mul_action α R } instance (R : Type*) [monoid R] [monoid α] [mul_distrib_mul_action R α] : mul_distrib_mul_action R αᵐᵒᵖ := { smul_mul := λ r x₁ x₂, unop_injective $ smul_mul' r (unop x₂) (unop x₁), smul_one := λ r, unop_injective $ smul_one r, .. mul_opposite.mul_action α R } @[to_additive] instance {M N} [has_smul M N] [has_smul M α] [has_smul N α] [is_scalar_tower M N α] : is_scalar_tower M N αᵐᵒᵖ := ⟨λ x y z, unop_injective $ smul_assoc _ _ _⟩ @[to_additive] instance {M N} [has_smul M α] [has_smul N α] [smul_comm_class M N α] : smul_comm_class M N αᵐᵒᵖ := ⟨λ x y z, unop_injective $ smul_comm _ _ _⟩ @[to_additive] instance (R : Type*) [has_smul R α] [has_smul Rᵐᵒᵖ α] [is_central_scalar R α] : is_central_scalar R αᵐᵒᵖ := ⟨λ r m, unop_injective $ op_smul_eq_smul _ _⟩ lemma op_smul_eq_op_smul_op {R : Type*} [has_smul R α] [has_smul Rᵐᵒᵖ α] [is_central_scalar R α] (r : R) (a : α) : op (r • a) = op r • op a := (op_smul_eq_smul r (op a)).symm lemma unop_smul_eq_unop_smul_unop {R : Type*} [has_smul R α] [has_smul Rᵐᵒᵖ α] [is_central_scalar R α] (r : Rᵐᵒᵖ) (a : αᵐᵒᵖ) : unop (r • a) = unop r • unop a := (unop_smul_eq_smul r (unop a)).symm end mul_opposite /-! ### Actions _by_ the opposite type (right actions) In `has_mul.to_has_smul` in another file, we define the left action `a₁ • a₂ = a₁ * a₂`. For the multiplicative opposite, we define `mul_opposite.op a₁ • a₂ = a₂ * a₁`, with the multiplication reversed. -/ open mul_opposite /-- Like `has_mul.to_has_smul`, but multiplies on the right. See also `monoid.to_opposite_mul_action` and `monoid_with_zero.to_opposite_mul_action_with_zero`. -/ @[to_additive "Like `has_add.to_has_vadd`, but adds on the right. See also `add_monoid.to_opposite_add_action`."] instance has_mul.to_has_opposite_smul [has_mul α] : has_smul αᵐᵒᵖ α := ⟨λ c x, x * c.unop⟩ @[to_additive] lemma op_smul_eq_mul [has_mul α] {a a' : α} : op a • a' = a' * a := rfl @[simp, to_additive] lemma mul_opposite.smul_eq_mul_unop [has_mul α] {a : αᵐᵒᵖ} {a' : α} : a • a' = a' * a.unop := rfl /-- The right regular action of a group on itself is transitive. -/ @[to_additive "The right regular action of an additive group on itself is transitive."] instance mul_action.opposite_regular.is_pretransitive {G : Type*} [group G] : mul_action.is_pretransitive Gᵐᵒᵖ G := ⟨λ x y, ⟨op (x⁻¹ * y), mul_inv_cancel_left _ _⟩⟩ @[to_additive] instance semigroup.opposite_smul_comm_class [semigroup α] : smul_comm_class αᵐᵒᵖ α α := { smul_comm := λ x y z, (mul_assoc _ _ _) } @[to_additive] instance semigroup.opposite_smul_comm_class' [semigroup α] : smul_comm_class α αᵐᵒᵖ α := smul_comm_class.symm _ _ _ instance comm_semigroup.is_central_scalar [comm_semigroup α] : is_central_scalar α α := ⟨λ r m, mul_comm _ _⟩ /-- Like `monoid.to_mul_action`, but multiplies on the right. -/ @[to_additive "Like `add_monoid.to_add_action`, but adds on the right."] instance monoid.to_opposite_mul_action [monoid α] : mul_action αᵐᵒᵖ α := { smul := (•), one_smul := mul_one, mul_smul := λ x y r, (mul_assoc _ _ _).symm } @[to_additive] instance is_scalar_tower.opposite_mid {M N} [has_mul N] [has_smul M N] [smul_comm_class M N N] : is_scalar_tower M Nᵐᵒᵖ N := ⟨λ x y z, mul_smul_comm _ _ _⟩ @[to_additive] instance smul_comm_class.opposite_mid {M N} [has_mul N] [has_smul M N] [is_scalar_tower M N N] : smul_comm_class M Nᵐᵒᵖ N := ⟨λ x y z, by { induction y using mul_opposite.rec, simp [smul_mul_assoc] }⟩ -- The above instance does not create an unwanted diamond, the two paths to -- `mul_action αᵐᵒᵖ αᵐᵒᵖ` are defeq. example [monoid α] : monoid.to_mul_action αᵐᵒᵖ = mul_opposite.mul_action α αᵐᵒᵖ := rfl /-- `monoid.to_opposite_mul_action` is faithful on cancellative monoids. -/ @[to_additive "`add_monoid.to_opposite_add_action` is faithful on cancellative monoids."] instance left_cancel_monoid.to_has_faithful_opposite_scalar [left_cancel_monoid α] : has_faithful_smul αᵐᵒᵖ α := ⟨λ x y h, unop_injective $ mul_left_cancel (h 1)⟩ /-- `monoid.to_opposite_mul_action` is faithful on nontrivial cancellative monoids with zero. -/ instance cancel_monoid_with_zero.to_has_faithful_opposite_scalar [cancel_monoid_with_zero α] [nontrivial α] : has_faithful_smul αᵐᵒᵖ α := ⟨λ x y h, unop_injective $ mul_left_cancel₀ one_ne_zero (h 1)⟩
a4fba9357a24e89dabbcde7636b188e520a66006
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/finsupp/well_founded.lean
a5099ed0ffbf7ca8fcb28910fd066f0d6416f7cc
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
3,317
lean
/- Copyright (c) 2022 Junyan Xu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Junyan Xu -/ import data.dfinsupp.well_founded import data.finsupp.lex /-! # Well-foundedness of the lexicographic and product orders on `finsupp` `finsupp.lex.well_founded` and the two variants that follow it essentially say that if `(>)` is a well order on `α`, `(<)` is well-founded on `N`, and `0` is a bottom element in `N`, then the lexicographic `(<)` is well-founded on `α →₀ N`. `finsupp.lex.well_founded_lt_of_finite` says that if `α` is finite and equipped with a linear order and `(<)` is well-founded on `N`, then the lexicographic `(<)` is well-founded on `α →₀ N`. `finsupp.well_founded_lt` and `well_founded_lt_of_finite` state the same results for the product order `(<)`, but without the ordering conditions on `α`. All results are transferred from `dfinsupp` via `finsupp.to_dfinsupp`. -/ variables {α N : Type*} namespace finsupp variables [hz : has_zero N] {r : α → α → Prop} {s : N → N → Prop} (hbot : ∀ ⦃n⦄, ¬ s n 0) (hs : well_founded s) include hbot hs /-- Transferred from `dfinsupp.lex.acc`. See the top of that file for an explanation for the appearance of the relation `rᶜ ⊓ (≠)`. -/ lemma lex.acc (x : α →₀ N) (h : ∀ a ∈ x.support, acc (rᶜ ⊓ (≠)) a) : acc (finsupp.lex r s) x := begin rw lex_eq_inv_image_dfinsupp_lex, classical, refine inv_image.accessible to_dfinsupp (dfinsupp.lex.acc (λ a, hbot) (λ a, hs) _ _), simpa only [to_dfinsupp_support] using h, end theorem lex.well_founded (hr : well_founded $ rᶜ ⊓ (≠)) : well_founded (finsupp.lex r s) := ⟨λ x, lex.acc hbot hs x $ λ a _, hr.apply a⟩ theorem lex.well_founded' [is_trichotomous α r] (hr : well_founded r.swap) : well_founded (finsupp.lex r s) := (lex_eq_inv_image_dfinsupp_lex r s).symm ▸ inv_image.wf _ (dfinsupp.lex.well_founded' (λ a, hbot) (λ a, hs) hr) omit hbot hs instance lex.well_founded_lt [has_lt α] [is_trichotomous α (<)] [hα : well_founded_gt α] [canonically_ordered_add_monoid N] [hN : well_founded_lt N] : well_founded_lt (lex (α →₀ N)) := ⟨lex.well_founded' (λ n, (zero_le n).not_lt) hN.wf hα.wf⟩ variable (r) theorem lex.well_founded_of_finite [is_strict_total_order α r] [finite α] [has_zero N] (hs : well_founded s) : well_founded (finsupp.lex r s) := inv_image.wf (@equiv_fun_on_finite α N _ _) (pi.lex.well_founded r $ λ a, hs) theorem lex.well_founded_lt_of_finite [linear_order α] [finite α] [has_zero N] [has_lt N] [hwf : well_founded_lt N] : well_founded_lt (lex (α →₀ N)) := ⟨finsupp.lex.well_founded_of_finite (<) hwf.1⟩ protected theorem well_founded_lt [has_zero N] [preorder N] [well_founded_lt N] (hbot : ∀ n : N, ¬ n < 0) : well_founded_lt (α →₀ N) := ⟨inv_image.wf to_dfinsupp (dfinsupp.well_founded_lt $ λ i a, hbot a).wf⟩ instance well_founded_lt' [canonically_ordered_add_monoid N] [well_founded_lt N] : well_founded_lt (α →₀ N) := finsupp.well_founded_lt $ λ a, (zero_le a).not_lt instance well_founded_lt_of_finite [finite α] [has_zero N] [preorder N] [well_founded_lt N] : well_founded_lt (α →₀ N) := ⟨inv_image.wf equiv_fun_on_finite function.well_founded_lt.wf⟩ end finsupp
5404b08a3c17af6da68ddbc4fb6f626a8a3203d4
fecda8e6b848337561d6467a1e30cf23176d6ad0
/src/ring_theory/localization.lean
bc567b81d52cdbb1e37125b4a380a94ecc1637fc
[ "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
64,629
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, Amelia Livingston -/ import data.equiv.ring import group_theory.monoid_localization import ring_theory.algebraic import ring_theory.integral_closure import ring_theory.non_zero_divisors /-! # Localizations of commutative rings We characterize the localization of a commutative ring `R` at a submonoid `M` up to isomorphism; that is, a commutative ring `S` is the localization of `R` at `M` iff we can find a ring homomorphism `f : R →+* S` satisfying 3 properties: 1. For all `y ∈ M`, `f y` is a unit; 2. For all `z : S`, there exists `(x, y) : R × M` such that `z * f y = f x`; 3. For all `x, y : R`, `f x = f y` iff there exists `c ∈ M` such that `x * c = y * c`. Given such a localization map `f : R →+* S`, we can define the surjection `localization_map.mk'` sending `(x, y) : R × M` to `f x * (f y)⁻¹`, and `localization_map.lift`, the homomorphism from `S` induced by a homomorphism from `R` which maps elements of `M` to invertible elements of the codomain. Similarly, given commutative rings `P, Q`, a submonoid `T` of `P` and a localization map for `T` from `P` to `Q`, then a homomorphism `g : R →+* P` such that `g(M) ⊆ T` induces a homomorphism of localizations, `localization_map.map`, from `S` to `Q`. We treat the special case of localizing away from an element in the sections `away_map` and `away`. We show the localization as a quotient type, defined in `group_theory.monoid_localization` as `submonoid.localization`, is a `comm_ring` and that the natural ring hom `of : R →+* localization M` is a localization map. We show that a localization at the complement of a prime ideal is a local ring. We prove some lemmas about the `R`-algebra structure of `S`. When `R` is an integral domain, we define `fraction_map R K` as an abbreviation for `localization (non_zero_divisors R) K`, the natural map to `R`'s field of fractions. We show that a `comm_ring` `K` which is the localization of an integral domain `R` at `R \ {0}` is a field. We use this to show the field of fractions as a quotient type, `fraction_ring`, is a field. ## Implementation notes In maths it is natural to reason up to isomorphism, but in Lean we cannot naturally `rewrite` one structure with an isomorphic one; one way around this is to isolate a predicate characterizing a structure up to isomorphism, and reason about things that satisfy the predicate. A ring localization map is defined to be a localization map of the underlying `comm_monoid` (a `submonoid.localization_map`) which is also a ring hom. To prove most lemmas about a `localization_map` `f` in this file we invoke the corresponding proof for the underlying `comm_monoid` localization map `f.to_localization_map`, which can be found in `group_theory.monoid_localization` and the namespace `submonoid.localization_map`. To apply a localization map `f` as a function, we use `f.to_map`, as coercions don't work well for this structure. To reason about the localization as a quotient type, use `mk_eq_of_mk'` and associated lemmas. These show the quotient map `mk : R → M → localization M` equals the surjection `localization_map.mk'` induced by the map `of : localization_map M (localization M)` (where `of` establishes the localization as a quotient type satisfies the characteristic predicate). The lemma `mk_eq_of_mk'` hence gives you access to the results in the rest of the file, which are about the `localization_map.mk'` induced by any localization map. We define a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that instances on `S` induced by `f` can 'know' the map needed to induce the instance. The proof that "a `comm_ring` `K` which is the localization of an integral domain `R` at `R \ {0}` is a field" is a `def` rather than an `instance`, so if you want to reason about a field of fractions `K`, assume `[field K]` instead of just `[comm_ring K]`. ## Tags localization, ring localization, commutative ring localization, characteristic predicate, commutative ring, field of fractions -/ variables {R : Type*} [comm_ring R] (M : submonoid R) (S : Type*) [comm_ring S] {P : Type*} [comm_ring P] open function set_option old_structure_cmd true /-- The type of ring homomorphisms satisfying the characteristic predicate: if `f : R →+* S` satisfies this predicate, then `S` is isomorphic to the localization of `R` at `M`. We later define an instance coercing a localization map `f` to its codomain `S` so that instances on `S` induced by `f` can 'know' the map needed to induce the instance. -/ @[nolint has_inhabited_instance] structure localization_map extends ring_hom R S, submonoid.localization_map M S /-- The ring hom underlying a `localization_map`. -/ add_decl_doc localization_map.to_ring_hom /-- The `comm_monoid` `localization_map` underlying a `comm_ring` `localization_map`. See `group_theory.monoid_localization` for its definition. -/ add_decl_doc localization_map.to_localization_map variables {M S} namespace ring_hom /-- Makes a localization map from a `comm_ring` hom satisfying the characteristic predicate. -/ def to_localization_map (f : R →+* S) (H1 : ∀ y : M, is_unit (f y)) (H2 : ∀ z, ∃ x : R × M, z * f x.2 = f x.1) (H3 : ∀ x y, f x = f y ↔ ∃ c : M, x * c = y * c) : localization_map M S := { map_units' := H1, surj' := H2, eq_iff_exists' := H3, .. f } end ring_hom /-- Makes a `comm_ring` localization map from an additive `comm_monoid` localization map of `comm_ring`s. -/ def submonoid.localization_map.to_ring_localization (f : submonoid.localization_map M S) (h : ∀ x y, f.to_map (x + y) = f.to_map x + f.to_map y) : localization_map M S := { ..ring_hom.mk' f.to_monoid_hom h, ..f } namespace localization_map variables (f : localization_map M S) /-- We define a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that instances on `S` induced by `f` can 'know` the map needed to induce the instance. -/ @[nolint unused_arguments has_inhabited_instance] def codomain (f : localization_map M S) := S instance : comm_ring f.codomain := by assumption instance {K : Type*} [field K] (f : localization_map M K) : field f.codomain := by assumption /-- Short for `to_ring_hom`; used for applying a localization map as a function. -/ abbreviation to_map := f.to_ring_hom lemma map_units (y : M) : is_unit (f.to_map y) := f.6 y lemma surj (z) : ∃ x : R × M, z * f.to_map x.2 = f.to_map x.1 := f.7 z lemma eq_iff_exists {x y} : f.to_map x = f.to_map y ↔ ∃ c : M, x * c = y * c := f.8 x y @[ext] lemma ext {f g : localization_map M S} (h : ∀ x, f.to_map x = g.to_map x) : f = g := begin cases f, cases g, simp only at *, exact funext h end lemma ext_iff {f g : localization_map M S} : f = g ↔ ∀ x, f.to_map x = g.to_map x := ⟨λ h x, h ▸ rfl, ext⟩ lemma to_map_injective : injective (@localization_map.to_map _ _ M S _) := λ _ _ h, ext $ ring_hom.ext_iff.1 h /-- Given `a : S`, `S` a localization of `R`, `is_integer a` iff `a` is in the image of the localization map from `R` to `S`. -/ def is_integer (a : S) : Prop := a ∈ set.range f.to_map variables {f} lemma is_integer_add {a b} (ha : f.is_integer a) (hb : f.is_integer b) : f.is_integer (a + b) := begin rcases ha with ⟨a', ha⟩, rcases hb with ⟨b', hb⟩, use a' + b', rw [f.to_map.map_add, ha, hb] end lemma is_integer_mul {a b} (ha : f.is_integer a) (hb : f.is_integer b) : f.is_integer (a * b) := begin rcases ha with ⟨a', ha⟩, rcases hb with ⟨b', hb⟩, use a' * b', rw [f.to_map.map_mul, ha, hb] end lemma is_integer_smul {a : R} {b} (hb : f.is_integer b) : f.is_integer (f.to_map a * b) := begin rcases hb with ⟨b', hb⟩, use a * b', rw [←hb, f.to_map.map_mul] end variables (f) /-- Each element `a : S` has an `M`-multiple which is an integer. This version multiplies `a` on the right, matching the argument order in `localization_map.surj`. -/ lemma exists_integer_multiple' (a : S) : ∃ (b : M), is_integer f (a * f.to_map b) := let ⟨⟨num, denom⟩, h⟩ := f.surj a in ⟨denom, set.mem_range.mpr ⟨num, h.symm⟩⟩ /-- Each element `a : S` has an `M`-multiple which is an integer. This version multiplies `a` on the left, matching the argument order in the `has_scalar` instance. -/ lemma exists_integer_multiple (a : S) : ∃ (b : M), is_integer f (f.to_map b * a) := by { simp_rw mul_comm _ a, apply exists_integer_multiple' } /-- Given `z : S`, `f.to_localization_map.sec z` is defined to be a pair `(x, y) : R × M` such that `z * f y = f x` (so this lemma is true by definition). -/ lemma sec_spec {f : localization_map M S} (z : S) : z * f.to_map (f.to_localization_map.sec z).2 = f.to_map (f.to_localization_map.sec z).1 := classical.some_spec $ f.surj z /-- Given `z : S`, `f.to_localization_map.sec z` is defined to be a pair `(x, y) : R × M` such that `z * f y = f x`, so this lemma is just an application of `S`'s commutativity. -/ lemma sec_spec' {f : localization_map M S} (z : S) : f.to_map (f.to_localization_map.sec z).1 = f.to_map (f.to_localization_map.sec z).2 * z := by rw [mul_comm, sec_spec] open_locale big_operators /-- We can clear the denominators of a finite set of fractions. -/ lemma exist_integer_multiples_of_finset (s : finset S) : ∃ (b : M), ∀ a ∈ s, is_integer f (f.to_map b * a) := begin haveI := classical.prop_decidable, use ∏ a in s, (f.to_localization_map.sec a).2, intros a ha, use (∏ x in s.erase a, (f.to_localization_map.sec x).2) * (f.to_localization_map.sec a).1, rw [ring_hom.map_mul, sec_spec', ←mul_assoc, ←f.to_map.map_mul], congr' 2, refine trans _ ((submonoid.subtype M).map_prod _ _).symm, rw [mul_comm, ←finset.prod_insert (s.not_mem_erase a), finset.insert_erase ha], refl, end lemma map_right_cancel {x y} {c : M} (h : f.to_map (c * x) = f.to_map (c * y)) : f.to_map x = f.to_map y := f.to_localization_map.map_right_cancel h lemma map_left_cancel {x y} {c : M} (h : f.to_map (x * c) = f.to_map (y * c)) : f.to_map x = f.to_map y := f.to_localization_map.map_left_cancel h lemma eq_zero_of_fst_eq_zero {z x} {y : M} (h : z * f.to_map y = f.to_map x) (hx : x = 0) : z = 0 := by rw [hx, f.to_map.map_zero] at h; exact (f.map_units y).mul_left_eq_zero.1 h /-- Given a localization map `f : R →+* S`, the surjection sending `(x, y) : R × M` to `f x * (f y)⁻¹`. -/ noncomputable def mk' (f : localization_map M S) (x : R) (y : M) : S := f.to_localization_map.mk' x y @[simp] lemma mk'_sec (z : S) : f.mk' (f.to_localization_map.sec z).1 (f.to_localization_map.sec z).2 = z := f.to_localization_map.mk'_sec _ lemma mk'_mul (x₁ x₂ : R) (y₁ y₂ : M) : f.mk' (x₁ * x₂) (y₁ * y₂) = f.mk' x₁ y₁ * f.mk' x₂ y₂ := f.to_localization_map.mk'_mul _ _ _ _ lemma mk'_one (x) : f.mk' x (1 : M) = f.to_map x := f.to_localization_map.mk'_one _ @[simp] lemma mk'_spec (x) (y : M) : f.mk' x y * f.to_map y = f.to_map x := f.to_localization_map.mk'_spec _ _ @[simp] lemma mk'_spec' (x) (y : M) : f.to_map y * f.mk' x y = f.to_map x := f.to_localization_map.mk'_spec' _ _ theorem eq_mk'_iff_mul_eq {x} {y : M} {z} : z = f.mk' x y ↔ z * f.to_map y = f.to_map x := f.to_localization_map.eq_mk'_iff_mul_eq theorem mk'_eq_iff_eq_mul {x} {y : M} {z} : f.mk' x y = z ↔ f.to_map x = z * f.to_map y := f.to_localization_map.mk'_eq_iff_eq_mul lemma mk'_surjective (z : S) : ∃ x (y : M), f.mk' x y = z := let ⟨r, hr⟩ := f.surj z in ⟨r.1, r.2, (f.eq_mk'_iff_mul_eq.2 hr).symm⟩ lemma mk'_eq_iff_eq {x₁ x₂} {y₁ y₂ : M} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ f.to_map (x₁ * y₂) = f.to_map (x₂ * y₁) := f.to_localization_map.mk'_eq_iff_eq lemma mk'_mem_iff {x} {y : M} {I : ideal S} : f.mk' x y ∈ I ↔ f.to_map x ∈ I := begin split; intro h, { rw [← mk'_spec f x y, mul_comm], exact I.smul_mem (f.to_map y) h }, { rw ← mk'_spec f x y at h, obtain ⟨b, hb⟩ := is_unit_iff_exists_inv.1 (map_units f y), have := I.smul_mem b h, rwa [smul_eq_mul, mul_comm, mul_assoc, hb, mul_one] at this } end protected lemma eq {a₁ b₁} {a₂ b₂ : M} : f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ ∃ c : M, a₁ * b₂ * c = b₁ * a₂ * c := f.to_localization_map.eq lemma eq_iff_eq (g : localization_map M P) {x y} : f.to_map x = f.to_map y ↔ g.to_map x = g.to_map y := f.to_localization_map.eq_iff_eq g.to_localization_map lemma mk'_eq_iff_mk'_eq (g : localization_map M P) {x₁ x₂} {y₁ y₂ : M} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ g.mk' x₁ y₁ = g.mk' x₂ y₂ := f.to_localization_map.mk'_eq_iff_mk'_eq g.to_localization_map lemma mk'_eq_of_eq {a₁ b₁ : R} {a₂ b₂ : M} (H : b₁ * a₂ = a₁ * b₂) : f.mk' a₁ a₂ = f.mk' b₁ b₂ := f.to_localization_map.mk'_eq_of_eq H @[simp] lemma mk'_self {x : R} (hx : x ∈ M) : f.mk' x ⟨x, hx⟩ = 1 := f.to_localization_map.mk'_self _ hx @[simp] lemma mk'_self' {x : M} : f.mk' x x = 1 := f.to_localization_map.mk'_self' _ lemma mk'_self'' {x : M} : f.mk' x.1 x = 1 := f.mk'_self' lemma mul_mk'_eq_mk'_of_mul (x y : R) (z : M) : f.to_map x * f.mk' y z = f.mk' (x * y) z := f.to_localization_map.mul_mk'_eq_mk'_of_mul _ _ _ lemma mk'_eq_mul_mk'_one (x : R) (y : M) : f.mk' x y = f.to_map x * f.mk' 1 y := (f.to_localization_map.mul_mk'_one_eq_mk' _ _).symm @[simp] lemma mk'_mul_cancel_left (x : R) (y : M) : f.mk' (y * x) y = f.to_map x := f.to_localization_map.mk'_mul_cancel_left _ _ lemma mk'_mul_cancel_right (x : R) (y : M) : f.mk' (x * y) y = f.to_map x := f.to_localization_map.mk'_mul_cancel_right _ _ @[simp] lemma mk'_mul_mk'_eq_one (x y : M) : f.mk' x y * f.mk' y x = 1 := by rw [←f.mk'_mul, mul_comm]; exact f.mk'_self _ lemma mk'_mul_mk'_eq_one' (x : R) (y : M) (h : x ∈ M) : f.mk' x y * f.mk' y ⟨x, h⟩ = 1 := f.mk'_mul_mk'_eq_one ⟨x, h⟩ _ lemma is_unit_comp (j : S →+* P) (y : M) : is_unit (j.comp f.to_map y) := f.to_localization_map.is_unit_comp j.to_monoid_hom _ /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →+* P` such that `g(M) ⊆ units P`, `f x = f y → g x = g y` for all `x y : R`. -/ lemma eq_of_eq {g : R →+* P} (hg : ∀ y : M, is_unit (g y)) {x y} (h : f.to_map x = f.to_map y) : g x = g y := @submonoid.localization_map.eq_of_eq _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom hg _ _ h lemma mk'_add (x₁ x₂ : R) (y₁ y₂ : M) : f.mk' (x₁ * y₂ + x₂ * y₁) (y₁ * y₂) = f.mk' x₁ y₁ + f.mk' x₂ y₂ := f.mk'_eq_iff_eq_mul.2 $ eq.symm begin rw [mul_comm (_ + _), mul_add, mul_mk'_eq_mk'_of_mul, ←eq_sub_iff_add_eq, mk'_eq_iff_eq_mul, mul_comm _ (f.to_map _), mul_sub, eq_sub_iff_add_eq, ←eq_sub_iff_add_eq', ←mul_assoc, ←f.to_map.map_mul, mul_mk'_eq_mk'_of_mul, mk'_eq_iff_eq_mul], simp only [f.to_map.map_add, submonoid.coe_mul, f.to_map.map_mul], ring_exp, end /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →+* P` such that `g y` is invertible for all `y : M`, the homomorphism induced from `S` to `P` sending `z : S` to `g x * (g y)⁻¹`, where `(x, y) : R × M` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def lift {g : R →+* P} (hg : ∀ y : M, is_unit (g y)) : S →+* P := ring_hom.mk' (@submonoid.localization_map.lift _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom hg) $ begin intros x y, rw [f.to_localization_map.lift_spec, mul_comm, add_mul, ←sub_eq_iff_eq_add, eq_comm, f.to_localization_map.lift_spec_mul, mul_comm _ (_ - _), sub_mul, eq_sub_iff_add_eq', ←eq_sub_iff_add_eq, mul_assoc, f.to_localization_map.lift_spec_mul], show g _ * (g _ * g _) = g _ * (g _ * g _ - g _ * g _), repeat {rw ←g.map_mul}, rw [←g.map_sub, ←g.map_mul], apply f.eq_of_eq hg, erw [f.to_map.map_mul, sec_spec', mul_sub, f.to_map.map_sub], simp only [f.to_map.map_mul, sec_spec'], ring_exp, end variables {g : R →+* P} (hg : ∀ y : M, is_unit (g y)) /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →* P` such that `g y` is invertible for all `y : M`, the homomorphism induced from `S` to `P` maps `f x * (f y)⁻¹` to `g x * (g y)⁻¹` for all `x : R, y ∈ M`. -/ lemma lift_mk' (x y) : f.lift hg (f.mk' x y) = g x * ↑(is_unit.lift_right (g.to_monoid_hom.mrestrict M) hg y)⁻¹ := f.to_localization_map.lift_mk' _ _ _ lemma lift_mk'_spec (x v) (y : M) : f.lift hg (f.mk' x y) = v ↔ g x = g y * v := f.to_localization_map.lift_mk'_spec _ _ _ _ @[simp] lemma lift_eq (x : R) : f.lift hg (f.to_map x) = g x := f.to_localization_map.lift_eq _ _ lemma lift_eq_iff {x y : R × M} : f.lift hg (f.mk' x.1 x.2) = f.lift hg (f.mk' y.1 y.2) ↔ g (x.1 * y.2) = g (y.1 * x.2) := f.to_localization_map.lift_eq_iff _ @[simp] lemma lift_comp : (f.lift hg).comp f.to_map = g := ring_hom.ext $ monoid_hom.ext_iff.1 $ f.to_localization_map.lift_comp _ @[simp] lemma lift_of_comp (j : S →+* P) : f.lift (f.is_unit_comp j) = j := ring_hom.ext $ monoid_hom.ext_iff.1 $ f.to_localization_map.lift_of_comp j.to_monoid_hom lemma epic_of_localization_map {j k : S →+* P} (h : ∀ a, j.comp f.to_map a = k.comp f.to_map a) : j = k := ring_hom.ext $ monoid_hom.ext_iff.1 $ @submonoid.localization_map.epic_of_localization_map _ _ _ _ _ _ _ f.to_localization_map j.to_monoid_hom k.to_monoid_hom h lemma lift_unique {j : S →+* P} (hj : ∀ x, j (f.to_map x) = g x) : f.lift hg = j := ring_hom.ext $ monoid_hom.ext_iff.1 $ @submonoid.localization_map.lift_unique _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom hg j.to_monoid_hom hj @[simp] lemma lift_id (x) : f.lift f.map_units x = x := f.to_localization_map.lift_id _ /-- Given two localization maps `f : R →+* S, k : R →+* P` for a submonoid `M ⊆ R`, the hom from `P` to `S` induced by `f` is left inverse to the hom from `S` to `P` induced by `k`. -/ @[simp] lemma lift_left_inverse {k : localization_map M S} (z : S) : k.lift f.map_units (f.lift k.map_units z) = z := f.to_localization_map.lift_left_inverse _ lemma lift_surjective_iff : surjective (f.lift hg) ↔ ∀ v : P, ∃ x : R × M, v * g x.2 = g x.1 := f.to_localization_map.lift_surjective_iff hg lemma lift_injective_iff : injective (f.lift hg) ↔ ∀ x y, f.to_map x = f.to_map y ↔ g x = g y := f.to_localization_map.lift_injective_iff hg variables {T : submonoid P} (hy : ∀ y : M, g y ∈ T) {Q : Type*} [comm_ring Q] (k : localization_map T Q) /-- Given a `comm_ring` homomorphism `g : R →+* P` where for submonoids `M ⊆ R, T ⊆ P` we have `g(M) ⊆ T`, the induced ring homomorphism from the localization of `R` at `M` to the localization of `P` at `T`: if `f : R →+* S` and `k : P →+* Q` are localization maps for `M` and `T` respectively, we send `z : S` to `k (g x) * (k (g y))⁻¹`, where `(x, y) : R × M` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def map : S →+* Q := @lift _ _ _ _ _ _ _ f (k.to_map.comp g) $ λ y, k.map_units ⟨g y, hy y⟩ variables {k} lemma map_eq (x) : f.map hy k (f.to_map x) = k.to_map (g x) := f.lift_eq (λ y, k.map_units ⟨g y, hy y⟩) x @[simp] lemma map_comp : (f.map hy k).comp f.to_map = k.to_map.comp g := f.lift_comp $ λ y, k.map_units ⟨g y, hy y⟩ lemma map_mk' (x) (y : M) : f.map hy k (f.mk' x y) = k.mk' (g x) ⟨g y, hy y⟩ := @submonoid.localization_map.map_mk' _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom _ hy _ _ k.to_localization_map _ _ @[simp] lemma map_id (z : S) : f.map (λ y, show ring_hom.id R y ∈ M, from y.2) f z = z := f.lift_id _ /-- If `comm_ring` homs `g : R →+* P, l : P →+* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ lemma map_comp_map {A : Type*} [comm_ring A] {U : submonoid A} {W} [comm_ring W] (j : localization_map U W) {l : P →+* A} (hl : ∀ w : T, l w ∈ U) : (k.map hl j).comp (f.map hy k) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j := ring_hom.ext $ monoid_hom.ext_iff.1 $ @submonoid.localization_map.map_comp_map _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom _ hy _ _ k.to_localization_map _ _ _ _ _ j.to_localization_map l.to_monoid_hom hl /-- If `comm_ring` homs `g : R →+* P, l : P →+* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ lemma map_map {A : Type*} [comm_ring A] {U : submonoid A} {W} [comm_ring W] (j : localization_map U W) {l : P →+* A} (hl : ∀ w : T, l w ∈ U) (x) : k.map hl j (f.map hy k x) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j x := by rw ←f.map_comp_map hy j hl; refl /-- Given localization maps `f : R →+* S, k : P →+* Q` for submonoids `M, T` respectively, an isomorphism `j : R ≃+* P` such that `j(M) = T` induces an isomorphism of localizations `S ≃+* Q`. -/ noncomputable def ring_equiv_of_ring_equiv (k : localization_map T Q) (h : R ≃+* P) (H : M.map h.to_monoid_hom = T) : S ≃+* Q := (f.to_localization_map.mul_equiv_of_mul_equiv k.to_localization_map H).to_ring_equiv $ (@lift _ _ _ _ _ _ _ f (k.to_map.comp h.to_ring_hom) (λ y, k.map_units ⟨(h y), H ▸ set.mem_image_of_mem h y.2⟩)).map_add @[simp] lemma ring_equiv_of_ring_equiv_eq_map_apply {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) (x) : f.ring_equiv_of_ring_equiv k j H x = f.map (λ y : M, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k x := rfl lemma ring_equiv_of_ring_equiv_eq_map {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) : (f.ring_equiv_of_ring_equiv k j H).to_monoid_hom = f.map (λ y : M, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k := rfl @[simp] lemma ring_equiv_of_ring_equiv_eq {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) (x) : f.ring_equiv_of_ring_equiv k j H (f.to_map x) = k.to_map (j x) := f.to_localization_map.mul_equiv_of_mul_equiv_eq H _ lemma ring_equiv_of_ring_equiv_mk' {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) (x y) : f.ring_equiv_of_ring_equiv k j H (f.mk' x y) = k.mk' (j x) ⟨j y, H ▸ set.mem_image_of_mem j y.2⟩ := f.to_localization_map.mul_equiv_of_mul_equiv_mk' H _ _ section away_map variables (x : R) /-- Given `x : R`, the type of homomorphisms `f : R →* S` such that `S` is isomorphic to the localization of `R` at the submonoid generated by `x`. -/ @[reducible] def away_map (S' : Type*) [comm_ring S'] := localization_map (submonoid.powers x) S' variables (F : away_map x S) /-- Given `x : R` and a localization map `F : R →+* S` away from `x`, `inv_self` is `(F x)⁻¹`. -/ noncomputable def away_map.inv_self : S := F.mk' 1 ⟨x, submonoid.mem_powers _⟩ /-- Given `x : R`, a localization map `F : R →+* S` away from `x`, and a map of `comm_ring`s `g : R →+* P` such that `g x` is invertible, the homomorphism induced from `S` to `P` sending `z : S` to `g y * (g x)⁻ⁿ`, where `y : R, n : ℕ` are such that `z = F y * (F x)⁻ⁿ`. -/ noncomputable def away_map.lift (hg : is_unit (g x)) : S →+* P := localization_map.lift F $ λ y, show is_unit (g y.1), begin obtain ⟨n, hn⟩ := y.2, rw [←hn, g.map_pow], exact is_unit.map (monoid_hom.of $ ((^ n) : P → P)) hg, end @[simp] lemma away_map.lift_eq (hg : is_unit (g x)) (a : R) : F.lift x hg (F.to_map a) = g a := lift_eq _ _ _ @[simp] lemma away_map.lift_comp (hg : is_unit (g x)) : (F.lift x hg).comp F.to_map = g := lift_comp _ _ /-- Given `x y : R` and localization maps `F : R →+* S, G : R →+* P` away from `x` and `x * y` respectively, the homomorphism induced from `S` to `P`. -/ noncomputable def away_to_away_right (y : R) (G : away_map (x * y) P) : S →* P := F.lift x $ show is_unit (G.to_map x), from is_unit_of_mul_eq_one (G.to_map x) (G.mk' y ⟨x * y, submonoid.mem_powers _⟩) $ by rw [mul_mk'_eq_mk'_of_mul, mk'_self] end away_map end localization_map namespace localization variables {M} instance : has_add (localization M) := ⟨λ z w, con.lift_on₂ z w (λ x y : R × M, mk ((x.2 : R) * y.1 + y.2 * x.1) (x.2 * y.2)) $ λ r1 r2 r3 r4 h1 h2, (con.eq _).2 begin rw r_eq_r' at h1 h2 ⊢, cases h1 with t₅ ht₅, cases h2 with t₆ ht₆, use t₆ * t₅, calc ((r1.2 : R) * r2.1 + r2.2 * r1.1) * (r3.2 * r4.2) * (t₆ * t₅) = (r2.1 * r4.2 * t₆) * (r1.2 * r3.2 * t₅) + (r1.1 * r3.2 * t₅) * (r2.2 * r4.2 * t₆) : by ring ... = (r3.2 * r4.1 + r4.2 * r3.1) * (r1.2 * r2.2) * (t₆ * t₅) : by rw [ht₆, ht₅]; ring end⟩ instance : has_neg (localization M) := ⟨λ z, con.lift_on z (λ x : R × M, mk (-x.1) x.2) $ λ r1 r2 h, (con.eq _).2 begin rw r_eq_r' at h ⊢, cases h with t ht, use t, rw [neg_mul_eq_neg_mul_symm, neg_mul_eq_neg_mul_symm, ht], ring, end⟩ instance : has_zero (localization M) := ⟨mk 0 1⟩ private meta def tac := `[{ intros, refine quotient.sound' (r_of_eq _), simp only [prod.snd_mul, prod.fst_mul, submonoid.coe_mul], ring }] instance : comm_ring (localization M) := { zero := 0, one := 1, add := (+), mul := (*), add_assoc := λ m n k, quotient.induction_on₃' m n k (by tac), zero_add := λ y, quotient.induction_on' y (by tac), add_zero := λ y, quotient.induction_on' y (by tac), neg := has_neg.neg, add_left_neg := λ y, quotient.induction_on' y (by tac), add_comm := λ y z, quotient.induction_on₂' z y (by tac), left_distrib := λ m n k, quotient.induction_on₃' m n k (by tac), right_distrib := λ m n k, quotient.induction_on₃' m n k (by tac), ..localization.comm_monoid M } variables (M) /-- Natural hom sending `x : R`, `R` a `comm_ring`, to the equivalence class of `(x, 1)` in the localization of `R` at a submonoid. -/ def of : localization_map M (localization M) := (localization.monoid_of M).to_ring_localization $ λ x y, (con.eq _).2 $ r_of_eq $ by simp [add_comm] variables {M} lemma monoid_of_eq_of (x) : (monoid_of M).to_map x = (of M).to_map x := rfl lemma mk_one_eq_of (x) : mk x 1 = (of M).to_map x := rfl lemma mk_eq_mk'_apply (x y) : mk x y = (of M).mk' x y := mk_eq_monoid_of_mk'_apply _ _ @[simp] lemma mk_eq_mk' : mk = (of M).mk' := mk_eq_monoid_of_mk' variables (f : localization_map M S) /-- Given a localization map `f : R →+* S` for a submonoid `M`, we get an isomorphism between the localization of `R` at `M` as a quotient type and `S`. -/ noncomputable def ring_equiv_of_quotient : localization M ≃+* S := (mul_equiv_of_quotient f.to_localization_map).to_ring_equiv $ ((of M).lift f.map_units).map_add variables {f} @[simp] lemma ring_equiv_of_quotient_apply (x) : ring_equiv_of_quotient f x = (of M).lift f.map_units x := rfl @[simp] lemma ring_equiv_of_quotient_mk' (x y) : ring_equiv_of_quotient f ((of M).mk' x y) = f.mk' x y := mul_equiv_of_quotient_mk' _ _ lemma ring_equiv_of_quotient_mk (x y) : ring_equiv_of_quotient f (mk x y) = f.mk' x y := mul_equiv_of_quotient_mk _ _ @[simp] lemma ring_equiv_of_quotient_of (x) : ring_equiv_of_quotient f ((of M).to_map x) = f.to_map x := mul_equiv_of_quotient_monoid_of _ @[simp] lemma ring_equiv_of_quotient_symm_mk' (x y) : (ring_equiv_of_quotient f).symm (f.mk' x y) = (of M).mk' x y := mul_equiv_of_quotient_symm_mk' _ _ lemma ring_equiv_of_quotient_symm_mk (x y) : (ring_equiv_of_quotient f).symm (f.mk' x y) = mk x y := mul_equiv_of_quotient_symm_mk _ _ @[simp] lemma ring_equiv_of_quotient_symm_of (x) : (ring_equiv_of_quotient f).symm (f.to_map x) = (of M).to_map x := mul_equiv_of_quotient_symm_monoid_of _ section away variables (x : R) /-- Given `x : R`, the natural hom sending `y : R`, `R` a `comm_ring`, to the equivalence class of `(y, 1)` in the localization of `R` at the submonoid generated by `x`. -/ @[reducible] def away.of : localization_map.away_map x (away x) := of (submonoid.powers x) @[simp] lemma away.mk_eq_mk' : mk = (away.of x).mk' := mk_eq_mk' /-- Given `x : R` and a localization map `f : R →+* S` away from `x`, we get an isomorphism between the localization of `R` at the submonoid generated by `x` as a quotient type and `S`. -/ noncomputable def away.ring_equiv_of_quotient (f : localization_map.away_map x S) : away x ≃+* S := ring_equiv_of_quotient f end away end localization variables {M} section at_prime variables (I : ideal R) [hp : I.is_prime] include hp namespace ideal /-- The complement of a prime ideal `I ⊆ R` is a submonoid of `R`. -/ def prime_compl : submonoid R := { carrier := (Iᶜ : set R), one_mem' := by convert I.ne_top_iff_one.1 hp.1; refl, mul_mem' := λ x y hnx hny hxy, or.cases_on (hp.2 hxy) hnx hny } end ideal namespace localization_map variables (S) /-- A localization map from `R` to `S` where the submonoid is the complement of a prime ideal of `R`. -/ @[reducible] def at_prime := localization_map I.prime_compl S end localization_map namespace localization /-- The localization of `R` at the complement of a prime ideal, as a quotient type. -/ @[reducible] def at_prime := localization I.prime_compl end localization namespace localization_map variables {I} /-- When `f` is a localization map from `R` at the complement of a prime ideal `I`, we use a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that the `local_ring` instance on `S` can 'know' the map needed to induce the instance. -/ instance at_prime.local_ring (f : at_prime S I) : local_ring f.codomain := local_of_nonunits_ideal (λ hze, begin rw [←f.to_map.map_one, ←f.to_map.map_zero] at hze, obtain ⟨t, ht⟩ := f.eq_iff_exists.1 hze, exact ((show (t : R) ∉ I, from t.2) (have htz : (t : R) = 0, by simpa using ht.symm, htz.symm ▸ I.zero_mem)) end) (begin intros x y hx hy hu, cases is_unit_iff_exists_inv.1 hu with z hxyz, have : ∀ {r s}, f.mk' r s ∈ nonunits S → r ∈ I, from λ r s, not_imp_comm.1 (λ nr, is_unit_iff_exists_inv.2 ⟨f.mk' s ⟨r, nr⟩, f.mk'_mul_mk'_eq_one' _ _ nr⟩), rcases f.mk'_surjective x with ⟨rx, sx, hrx⟩, rcases f.mk'_surjective y with ⟨ry, sy, hry⟩, rcases f.mk'_surjective z with ⟨rz, sz, hrz⟩, rw [←hrx, ←hry, ←hrz, ←f.mk'_add, ←f.mk'_mul, ←f.mk'_self I.prime_compl.one_mem] at hxyz, rw ←hrx at hx, rw ←hry at hy, cases f.eq.1 hxyz with t ht, simp only [mul_one, one_mul, submonoid.coe_mul, subtype.coe_mk] at ht, rw [←sub_eq_zero, ←sub_mul] at ht, have hr := (hp.mem_or_mem_of_mul_eq_zero ht).resolve_right t.2, have := I.neg_mem_iff.1 ((ideal.add_mem_iff_right _ _).1 hr), { exact not_or (mt hp.mem_or_mem (not_or sx.2 sy.2)) sz.2 (hp.mem_or_mem this)}, { exact I.mul_mem_right (I.add_mem (I.mul_mem_right (this hx)) (I.mul_mem_right (this hy)))} end) end localization_map namespace localization /-- The localization of `R` at the complement of a prime ideal is a local ring. -/ instance at_prime.local_ring : local_ring (localization I.prime_compl) := localization_map.at_prime.local_ring (of I.prime_compl) end localization end at_prime namespace localization_map variables (f : localization_map M S) section ideals /-- Explicit characterization of the ideal given by `ideal.map f.to_map I`. In practice, this ideal differs only in that the carrier set is defined explicitly. This definition is only meant to be used in proving `mem_map_to_map_iff`, and any proof that needs to refer to the explicit carrier set should use that theorem. -/ private def to_map_ideal (I : ideal R) : ideal S := { carrier := { z : S | ∃ x : I × M, z * (f.to_map x.2) = f.to_map x.1}, zero_mem' := ⟨⟨0, 1⟩, by simp⟩, add_mem' := begin rintros a b ⟨a', ha⟩ ⟨b', hb⟩, use ⟨a'.2 * b'.1 + b'.2 * a'.1, I.add_mem (I.smul_mem _ b'.1.2) (I.smul_mem _ a'.1.2)⟩, use a'.2 * b'.2, simp only [ring_hom.map_add, submodule.coe_mk, submonoid.coe_mul, ring_hom.map_mul], rw [add_mul, ← mul_assoc a, ha, mul_comm (f.to_map a'.2) (f.to_map b'.2), ← mul_assoc b, hb], ring end, smul_mem' := begin rintros c x ⟨x', hx⟩, obtain ⟨c', hc⟩ := localization_map.surj f c, use ⟨c'.1 * x'.1, I.smul_mem c'.1 x'.1.2⟩, use c'.2 * x'.2, simp only [←hx, ←hc, smul_eq_mul, submodule.coe_mk, submonoid.coe_mul, ring_hom.map_mul], ring end } theorem mem_map_to_map_iff {I : ideal R} {z} : z ∈ ideal.map f.to_map I ↔ ∃ x : I × M, z * (f.to_map x.2) = f.to_map x.1 := begin split, { show _ → z ∈ to_map_ideal f I, refine λ h, ideal.mem_Inf.1 h (λ z hz, _), obtain ⟨y, hy⟩ := hz, use ⟨⟨⟨y, hy.left⟩, 1⟩, by simp [hy.right]⟩ }, { rintros ⟨⟨a, s⟩, h⟩, rw [← ideal.unit_mul_mem_iff_mem _ (map_units f s), mul_comm], exact h.symm ▸ ideal.mem_map_of_mem a.2 } end theorem map_comap (J : ideal S) : ideal.map f.to_map (ideal.comap f.to_map J) = J := le_antisymm (ideal.map_le_iff_le_comap.2 (le_refl _)) $ λ x hJ, begin obtain ⟨r, s, hx⟩ := f.mk'_surjective x, rw ←hx at ⊢ hJ, exact ideal.mul_mem_right _ (ideal.mem_map_of_mem (show f.to_map r ∈ J, from f.mk'_spec r s ▸ @ideal.mul_mem_right _ _ J (f.mk' r s) (f.to_map s) hJ)), end theorem comap_map_of_is_prime_disjoint (I : ideal R) (hI : I.is_prime) (hM : disjoint (M : set R) I) : ideal.comap f.to_map (ideal.map f.to_map I) = I := begin refine le_antisymm (λ a ha, _) ideal.le_comap_map, rw [ideal.mem_comap, mem_map_to_map_iff] at ha, obtain ⟨⟨b, s⟩, h⟩ := ha, have : f.to_map (a * ↑s - b) = 0 := by simpa [sub_eq_zero] using h, rw [← f.to_map.map_zero, eq_iff_exists] at this, obtain ⟨c, hc⟩ := this, have : a * s ∈ I, { rw zero_mul at hc, let this : (a * ↑s - ↑b) * ↑c ∈ I := hc.symm ▸ I.zero_mem, cases hI.right this with h1 h2, { simpa using I.add_mem h1 b.2 }, { exfalso, refine hM ⟨c.2, h2⟩ } }, cases hI.right this with h1 h2, { exact h1 }, { exfalso, refine hM ⟨s.2, h2⟩ } end /-- If `S` is the localization of `R` at a submonoid, the ordering of ideals of `S` is embedded in the ordering of ideals of `R`. -/ def order_embedding : ideal S ↪o ideal R := { to_fun := λ J, ideal.comap f.to_map J, inj' := function.left_inverse.injective f.map_comap, map_rel_iff' := λ J₁ J₂, ⟨ideal.comap_mono, λ hJ, f.map_comap J₁ ▸ f.map_comap J₂ ▸ ideal.map_mono hJ⟩ } /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M`. This lemma gives the particular case for an ideal and its comap, see `le_rel_iso_of_prime` for the more general relation isomorphism -/ lemma is_prime_iff_is_prime_disjoint (J : ideal S) : J.is_prime ↔ (ideal.comap f.to_map J).is_prime ∧ disjoint (M : set R) ↑(ideal.comap f.to_map J) := begin split, { refine λ h, ⟨⟨_, _⟩, λ m hm, h.1 (ideal.eq_top_of_is_unit_mem _ hm.2 (map_units f ⟨m, hm.left⟩))⟩, { refine λ hJ, h.left _, rw [eq_top_iff, (order_embedding f).map_rel_iff], exact le_of_eq hJ.symm }, { intros x y hxy, rw [ideal.mem_comap, ring_hom.map_mul] at hxy, exact h.right hxy } }, { refine λ h, ⟨λ hJ, h.left.left (eq_top_iff.2 _), _⟩, { rwa [eq_top_iff, (order_embedding f).map_rel_iff] at hJ }, { intros x y hxy, obtain ⟨a, s, ha⟩ := mk'_surjective f x, obtain ⟨b, t, hb⟩ := mk'_surjective f y, have : f.mk' (a * b) (s * t) ∈ J := by rwa [mk'_mul, ha, hb], rw [mk'_mem_iff, ← ideal.mem_comap] at this, replace this := h.left.right this, rw [ideal.mem_comap, ideal.mem_comap] at this, rwa [← ha, ← hb, mk'_mem_iff, mk'_mem_iff] } } end /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M`. This lemma gives the particular case for an ideal and its map, see `le_rel_iso_of_prime` for the more general relation isomorphism, and the reverse implication -/ lemma is_prime_of_is_prime_disjoint (I : ideal R) (hp : I.is_prime) (hd : disjoint (M : set R) ↑I) : (ideal.map f.to_map I).is_prime := begin rw [is_prime_iff_is_prime_disjoint f, comap_map_of_is_prime_disjoint f I hp hd], exact ⟨hp, hd⟩ end /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M` -/ def order_iso_of_prime (f : localization_map M S) : {p : ideal S // p.is_prime} ≃o {p : ideal R // p.is_prime ∧ disjoint (M : set R) ↑p} := { to_fun := λ p, ⟨ideal.comap f.to_map p.1, (is_prime_iff_is_prime_disjoint f p.1).1 p.2⟩, inv_fun := λ p, ⟨ideal.map f.to_map p.1, is_prime_of_is_prime_disjoint f p.1 p.2.1 p.2.2⟩, left_inv := λ J, subtype.eq (map_comap f J), right_inv := λ I, subtype.eq (comap_map_of_is_prime_disjoint f I.1 I.2.1 I.2.2), map_rel_iff' := λ I I', ⟨λ h x hx, h hx, λ h, (show I.val ≤ I'.val, from (map_comap f I.val) ▸ (map_comap f I'.val) ▸ (ideal.map_mono h))⟩ } end ideals /-! ### `algebra` section Defines the `R`-algebra instance on a copy of `S` carrying the data of the localization map `f` needed to induce the `R`-algebra structure. -/ /-- We use a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that the `R`-algebra instance on `S` can 'know' the map needed to induce the `R`-algebra structure. -/ instance : algebra R f.codomain := f.to_map.to_algebra end localization_map namespace localization instance : algebra R (localization M) := localization_map.algebra (of M) end localization namespace localization_map variables (f : localization_map M S) @[simp] lemma of_id (a : R) : (algebra.of_id R f.codomain) a = f.to_map a := rfl @[simp] lemma algebra_map_eq : algebra_map R f.codomain = f.to_map := rfl variables (f) /-- Localization map `f` from `R` to `S` as an `R`-linear map. -/ def lin_coe : R →ₗ[R] f.codomain := { to_fun := f.to_map, map_add' := f.to_map.map_add, map_smul' := f.to_map.map_mul } /-- Map from ideals of `R` to submodules of `S` induced by `f`. -/ -- This was previously a `has_coe` instance, but if `f.codomain = R` then this will loop. -- It could be a `has_coe_t` instance, but we keep it explicit here to avoid slowing down -- the rest of the library. def coe_submodule (I : ideal R) : submodule R f.codomain := submodule.map f.lin_coe I variables {f} lemma mem_coe_submodule (I : ideal R) {x : S} : x ∈ f.coe_submodule I ↔ ∃ y : R, y ∈ I ∧ f.to_map y = x := iff.rfl @[simp] lemma lin_coe_apply {x} : f.lin_coe x = f.to_map x := rfl variables {g : R →+* P} variables {T : submonoid P} (hy : ∀ y : M, g y ∈ T) {Q : Type*} [comm_ring Q] (k : localization_map T Q) lemma map_smul (x : f.codomain) (z : R) : f.map hy k (z • x : f.codomain) = @has_scalar.smul P k.codomain _ (g z) (f.map hy k x) := show f.map hy k (f.to_map z * x) = k.to_map (g z) * f.map hy k x, by rw [ring_hom.map_mul, map_eq] end localization_map namespace localization variables (f : localization_map M S) /-- Given a localization map `f : R →+* S` for a submonoid `M`, we get an `R`-preserving isomorphism between the localization of `R` at `M` as a quotient type and `S`. -/ noncomputable def alg_equiv_of_quotient : localization M ≃ₐ[R] f.codomain := { commutes' := ring_equiv_of_quotient_of, ..ring_equiv_of_quotient f } lemma alg_equiv_of_quotient_apply (x : localization M) : alg_equiv_of_quotient f x = ring_equiv_of_quotient f x := rfl lemma alg_equiv_of_quotient_symm_apply (x : f.codomain) : (alg_equiv_of_quotient f).symm x = (ring_equiv_of_quotient f).symm x := rfl end localization namespace localization_map section integer_normalization variables {f : localization_map M S} open finsupp polynomial open_locale classical /-- `coeff_integer_normalization p` gives the coefficients of the polynomial `integer_normalization p` -/ noncomputable def coeff_integer_normalization (p : polynomial f.codomain) (i : ℕ) : R := if hi : i ∈ p.support then classical.some (classical.some_spec (f.exist_integer_multiples_of_finset (p.support.image p.coeff)) (p.coeff i) (finset.mem_image.mpr ⟨i, hi, rfl⟩)) else 0 lemma coeff_integer_normalization_mem_support (p : polynomial f.codomain) (i : ℕ) (h : coeff_integer_normalization p i ≠ 0) : i ∈ p.support := begin contrapose h, rw [ne.def, not_not, coeff_integer_normalization, dif_neg h] end /-- `integer_normalization g` normalizes `g` to have integer coefficients by clearing the denominators -/ noncomputable def integer_normalization : polynomial f.codomain → polynomial R := λ p, on_finset p.support (coeff_integer_normalization p) (coeff_integer_normalization_mem_support p) @[simp] lemma integer_normalization_coeff (p : polynomial f.codomain) (i : ℕ) : (integer_normalization p).coeff i = coeff_integer_normalization p i := rfl lemma integer_normalization_spec (p : polynomial f.codomain) : ∃ (b : M), ∀ i, f.to_map ((integer_normalization p).coeff i) = f.to_map b * p.coeff i := begin use classical.some (f.exist_integer_multiples_of_finset (p.support.image p.coeff)), intro i, rw [integer_normalization_coeff, coeff_integer_normalization], split_ifs with hi, { exact classical.some_spec (classical.some_spec (f.exist_integer_multiples_of_finset (p.support.image p.coeff)) (p.coeff i) (finset.mem_image.mpr ⟨i, hi, rfl⟩)) }, { convert (_root_.mul_zero (f.to_map _)).symm, { exact f.to_ring_hom.map_zero }, { exact finsupp.not_mem_support_iff.mp hi } } end lemma integer_normalization_map_to_map (p : polynomial f.codomain) : ∃ (b : M), (integer_normalization p).map f.to_map = f.to_map b • p := let ⟨b, hb⟩ := integer_normalization_spec p in ⟨b, polynomial.ext (λ i, by { rw coeff_map, exact hb i })⟩ variables {R' : Type*} [comm_ring R'] lemma integer_normalization_eval₂_eq_zero (g : f.codomain →+* R') (p : polynomial f.codomain) {x : R'} (hx : eval₂ g x p = 0) : eval₂ (g.comp f.to_map) x (integer_normalization p) = 0 := let ⟨b, hb⟩ := integer_normalization_map_to_map p in trans (eval₂_map f.to_map g x).symm (by rw [hb, eval₂_smul, hx, smul_zero]) lemma integer_normalization_aeval_eq_zero [algebra R R'] [algebra f.codomain R'] [is_scalar_tower R f.codomain R'] (p : polynomial f.codomain) {x : R'} (hx : aeval x p = 0) : aeval x (integer_normalization p) = 0 := by rw [aeval_def, is_scalar_tower.algebra_map_eq R f.codomain R', algebra_map_eq, integer_normalization_eval₂_eq_zero _ _ hx] end integer_normalization variables {R} {A K : Type*} [integral_domain A] lemma to_map_eq_zero_iff (f : localization_map M S) {x : R} (hM : M ≤ non_zero_divisors R) : f.to_map x = 0 ↔ x = 0 := begin rw ← f.to_map.map_zero, split; intro h, { cases f.eq_iff_exists.mp h with c hc, rw zero_mul at hc, exact hM c.2 x hc }, { rw h }, end lemma injective (f : localization_map M S) (hM : M ≤ non_zero_divisors R) : injective f.to_map := begin rw ring_hom.injective_iff f.to_map, intros a ha, rw [← f.to_map.map_zero, f.eq_iff_exists] at ha, cases ha with c hc, rw zero_mul at hc, exact hM c.2 a hc, end protected lemma to_map_ne_zero_of_mem_non_zero_divisors {M : submonoid A} (f : localization_map M S) (hM : M ≤ non_zero_divisors A) (x : non_zero_divisors A) : f.to_map x ≠ 0 := map_ne_zero_of_mem_non_zero_divisors (f.injective hM) /-- A `comm_ring` `S` which is the localization of an integral domain `R` at a subset of non-zero elements is an integral domain. -/ def integral_domain_of_le_non_zero_divisors {M : submonoid A} (f : localization_map M S) (hM : M ≤ non_zero_divisors A) : integral_domain S := { eq_zero_or_eq_zero_of_mul_eq_zero := begin intros z w h, cases f.surj z with x hx, cases f.surj w with y hy, have : z * w * f.to_map y.2 * f.to_map x.2 = f.to_map x.1 * f.to_map y.1, by rw [mul_assoc z, hy, ←hx]; ac_refl, rw [h, zero_mul, zero_mul, ← f.to_map.map_mul] at this, cases eq_zero_or_eq_zero_of_mul_eq_zero ((to_map_eq_zero_iff f hM).mp this.symm) with H H, { exact or.inl (f.eq_zero_of_fst_eq_zero hx H) }, { exact or.inr (f.eq_zero_of_fst_eq_zero hy H) }, end, exists_pair_ne := ⟨f.to_map 0, f.to_map 1, λ h, zero_ne_one (f.injective hM h)⟩, ..(infer_instance : comm_ring S) } /-- The localization at of an integral domain to a set of non-zero elements is an integral domain -/ def integral_domain_localization {M : submonoid A} (hM : M ≤ non_zero_divisors A) : integral_domain (localization M) := (localization.of M).integral_domain_of_le_non_zero_divisors hM /-- The localization of an integral domain at the complement of a prime ideal is an integral domain. -/ instance integral_domain_of_local_at_prime {P : ideal A} (hp : P.is_prime) : integral_domain (localization.at_prime P) := integral_domain_localization (le_non_zero_divisors_of_domain (by simpa only [] using P.zero_mem)) end localization_map section at_prime namespace localization local attribute [instance] classical.prop_decidable /-- The image of `P` in the localization at `P.prime_compl` is a maximal ideal, and in particular it is the unique maximal ideal given by the local ring structure `at_prime.local_ring` -/ lemma at_prime.map_eq_maximal_ideal {P : ideal R} [hP : ideal.is_prime P] : ideal.map (localization.of P.prime_compl).to_map P = (local_ring.maximal_ideal (localization P.prime_compl)) := begin let f := localization.of P.prime_compl, ext x, split; simp only [local_ring.mem_maximal_ideal, mem_nonunits_iff]; intro hx, { exact λ h, (localization_map.is_prime_of_is_prime_disjoint f P hP (set.disjoint_compl_left P.carrier)).1 (ideal.eq_top_of_is_unit_mem _ hx h) }, { obtain ⟨⟨a, b⟩, hab⟩ := localization_map.surj f x, contrapose! hx, rw is_unit_iff_exists_inv, rw localization_map.mem_map_to_map_iff at hx, obtain ⟨a', ha'⟩ := is_unit_iff_exists_inv.1 (localization_map.map_units f ⟨a, λ ha, hx ⟨⟨⟨a, ha⟩, b⟩, hab⟩⟩), exact ⟨f.to_map b * a', by rwa [← mul_assoc, hab]⟩ } end /-- The unique maximal ideal of the localization at `P.prime_compl` lies over the ideal `P`. -/ lemma at_prime.comap_maximal_ideal {P : ideal R} [ideal.is_prime P] : ideal.comap (localization.of P.prime_compl).to_map (local_ring.maximal_ideal (localization P.prime_compl)) = P := begin let Pₚ := local_ring.maximal_ideal (localization P.prime_compl), refine le_antisymm (λ x hx, _) (le_trans ideal.le_comap_map (ideal.comap_mono (le_of_eq at_prime.map_eq_maximal_ideal))), by_cases h0 : x = 0, { exact h0.symm ▸ P.zero_mem }, { have : Pₚ.is_prime := ideal.is_maximal.is_prime (local_ring.maximal_ideal.is_maximal _), rw localization_map.is_prime_iff_is_prime_disjoint (localization.of P.prime_compl) at this, contrapose! h0 with hx', simpa using this.2 ⟨hx', hx⟩ } end end localization end at_prime variables (R) {A : Type*} [integral_domain A] variables (K : Type*) /-- Localization map from an integral domain `R` to its field of fractions. -/ @[reducible] def fraction_map [comm_ring K] := localization_map (non_zero_divisors R) K namespace fraction_map open localization_map variables {R K} lemma to_map_eq_zero_iff [comm_ring K] (φ : fraction_map R K) {x : R} : φ.to_map x = 0 ↔ x = 0 := φ.to_map_eq_zero_iff (le_of_eq rfl) protected theorem injective [comm_ring K] (φ : fraction_map R K) : function.injective φ.to_map := φ.injective (le_of_eq rfl) protected lemma to_map_ne_zero_of_mem_non_zero_divisors [comm_ring K] (φ : fraction_map A K) (x : non_zero_divisors A) : φ.to_map x ≠ 0 := φ.to_map_ne_zero_of_mem_non_zero_divisors (le_of_eq rfl) x /-- A `comm_ring` `K` which is the localization of an integral domain `R` at `R - {0}` is an integral domain. -/ def to_integral_domain [comm_ring K] (φ : fraction_map A K) : integral_domain K := φ.integral_domain_of_le_non_zero_divisors (le_of_eq rfl) local attribute [instance] classical.dec_eq /-- The inverse of an element in the field of fractions of an integral domain. -/ protected noncomputable def inv [comm_ring K] (φ : fraction_map A K) (z : K) : K := if h : z = 0 then 0 else φ.mk' (φ.to_localization_map.sec z).2 ⟨(φ.to_localization_map.sec z).1, mem_non_zero_divisors_iff_ne_zero.2 $ λ h0, h $ φ.eq_zero_of_fst_eq_zero (sec_spec z) h0⟩ protected lemma mul_inv_cancel [comm_ring K] (φ : fraction_map A K) (x : K) (hx : x ≠ 0) : x * φ.inv x = 1 := show x * dite _ _ _ = 1, by rw [dif_neg hx, ←is_unit.mul_left_inj (φ.map_units ⟨(φ.to_localization_map.sec x).1, mem_non_zero_divisors_iff_ne_zero.2 $ λ h0, hx $ φ.eq_zero_of_fst_eq_zero (sec_spec x) h0⟩), one_mul, mul_assoc, mk'_spec, ←eq_mk'_iff_mul_eq]; exact (φ.mk'_sec x).symm /-- A `comm_ring` `K` which is the localization of an integral domain `R` at `R - {0}` is a field. -/ noncomputable def to_field [comm_ring K] (φ : fraction_map A K) : field K := { inv := φ.inv, mul_inv_cancel := φ.mul_inv_cancel, inv_zero := dif_pos rfl, ..φ.to_integral_domain } variables {B : Type*} [integral_domain B] [field K] {L : Type*} [field L] (f : fraction_map A K) {g : A →+* L} lemma mk'_eq_div {r s} : f.mk' r s = f.to_map r / f.to_map s := f.mk'_eq_iff_eq_mul.2 $ (div_mul_cancel _ (f.to_map_ne_zero_of_mem_non_zero_divisors _)).symm lemma is_unit_map_of_injective (hg : function.injective g) (y : non_zero_divisors A) : is_unit (g y) := is_unit.mk0 (g y) $ map_ne_zero_of_mem_non_zero_divisors hg /-- Given an integral domain `A`, a localization map to its fields of fractions `f : A →+* K`, and an injective ring hom `g : A →+* L` where `L` is a field, we get a field hom sending `z : K` to `g x * (g y)⁻¹`, where `(x, y) : A × (non_zero_divisors A)` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def lift (hg : injective g) : K →+* L := f.lift $ is_unit_map_of_injective hg /-- Given an integral domain `A`, a localization map to its fields of fractions `f : A →+* K`, and an injective ring hom `g : A →+* L` where `L` is a field, field hom induced from `K` to `L` maps `f x / f y` to `g x / g y` for all `x : A, y ∈ non_zero_divisors A`. -/ @[simp] lemma lift_mk' (hg : injective g) (x y) : f.lift hg (f.mk' x y) = g x / g y := begin erw f.lift_mk' (is_unit_map_of_injective hg), erw submonoid.localization_map.mul_inv_left (λ y : non_zero_divisors A, show is_unit (g.to_monoid_hom y), from is_unit_map_of_injective hg y), exact (mul_div_cancel' _ (map_ne_zero_of_mem_non_zero_divisors hg)).symm, end /-- Given integral domains `A, B` and localization maps to their fields of fractions `f : A →+* K, g : B →+* L` and an injective ring hom `j : A →+* B`, we get a field hom sending `z : K` to `g (j x) * (g (j y))⁻¹`, where `(x, y) : A × (non_zero_divisors A)` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def map (g : fraction_map B L) {j : A →+* B} (hj : injective j) : K →+* L := f.map (λ y, mem_non_zero_divisors_iff_ne_zero.2 $ map_ne_zero_of_mem_non_zero_divisors hj) g /-- Given integral domains `A, B` and localization maps to their fields of fractions `f : A →+* K, g : B →+* L`, an isomorphism `j : A ≃+* B` induces an isomorphism of fields of fractions `K ≃+* L`. -/ noncomputable def field_equiv_of_ring_equiv (g : fraction_map B L) (h : A ≃+* B) : K ≃+* L := f.ring_equiv_of_ring_equiv g h begin ext b, show b ∈ h.to_equiv '' _ ↔ _, erw [h.to_equiv.image_eq_preimage, set.preimage, set.mem_set_of_eq, mem_non_zero_divisors_iff_ne_zero, mem_non_zero_divisors_iff_ne_zero], exact h.symm.map_ne_zero_iff end /-- The cast from `int` to `rat` as a `fraction_map`. -/ def int.fraction_map : fraction_map ℤ ℚ := { to_fun := coe, map_units' := begin rintro ⟨x, hx⟩, rw [submonoid.mem_carrier, mem_non_zero_divisors_iff_ne_zero] at hx, simpa only [is_unit_iff_ne_zero, int.cast_eq_zero, ne.def, subtype.coe_mk] using hx, end, surj' := begin rintro ⟨n, d, hd, h⟩, refine ⟨⟨n, ⟨d, _⟩⟩, rat.mul_denom_eq_num⟩, rwa [submonoid.mem_carrier, mem_non_zero_divisors_iff_ne_zero, int.coe_nat_ne_zero_iff_pos] end, eq_iff_exists' := begin intros x y, rw [int.cast_inj], refine ⟨by { rintro rfl, use 1 }, _⟩, rintro ⟨⟨c, hc⟩, h⟩, apply int.eq_of_mul_eq_mul_right _ h, rwa [submonoid.mem_carrier, mem_non_zero_divisors_iff_ne_zero] at hc, end, ..int.cast_ring_hom ℚ } lemma integer_normalization_eq_zero_iff {p : polynomial f.codomain} : integer_normalization p = 0 ↔ p = 0 := begin refine (polynomial.ext_iff.trans (polynomial.ext_iff.trans _).symm), obtain ⟨⟨b, nonzero⟩, hb⟩ := integer_normalization_spec p, split; intros h i, { apply f.to_map_eq_zero_iff.mp, rw [hb i, h i], exact _root_.mul_zero _ }, { have hi := h i, rw [polynomial.coeff_zero, ← f.to_map_eq_zero_iff, hb i] at hi, apply or.resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero hi), intro h, apply mem_non_zero_divisors_iff_ne_zero.mp nonzero, exact f.to_map_eq_zero_iff.mp h } end /-- A field is algebraic over the ring `A` iff it is algebraic over the field of fractions of `A`. -/ lemma comap_is_algebraic_iff [algebra A L] [algebra f.codomain L] [is_scalar_tower A f.codomain L] : algebra.is_algebraic A L ↔ algebra.is_algebraic f.codomain L := begin split; intros h x; obtain ⟨p, hp, px⟩ := h x, { refine ⟨p.map f.to_map, λ h, hp (polynomial.ext (λ i, _)), _⟩, { have : f.to_map (p.coeff i) = 0 := trans (polynomial.coeff_map _ _).symm (by simp [h]), exact f.to_map_eq_zero_iff.mp this }, { rwa [is_scalar_tower.aeval_apply _ f.codomain, algebra_map_eq] at px } }, { exact ⟨integer_normalization p, mt f.integer_normalization_eq_zero_iff.mp hp, integer_normalization_aeval_eq_zero p px⟩ }, end section num_denom variables [unique_factorization_domain A] (φ : fraction_map A K) lemma exists_reduced_fraction (x : φ.codomain) : ∃ (a : A) (b : non_zero_divisors A), (∀ {d}, d ∣ a → d ∣ b → is_unit d) ∧ φ.mk' a b = x := begin obtain ⟨⟨b, b_nonzero⟩, a, hab⟩ := φ.exists_integer_multiple x, obtain ⟨a', b', c', no_factor, rfl, rfl⟩ := unique_factorization_domain.exists_reduced_factors' a b (mem_non_zero_divisors_iff_ne_zero.mp b_nonzero), obtain ⟨c'_nonzero, b'_nonzero⟩ := mul_mem_non_zero_divisors.mp b_nonzero, refine ⟨a', ⟨b', b'_nonzero⟩, @no_factor, _⟩, apply mul_left_cancel' (φ.to_map_ne_zero_of_mem_non_zero_divisors ⟨c' * b', b_nonzero⟩), simp only [subtype.coe_mk, φ.to_map.map_mul] at *, erw [←hab, mul_assoc, φ.mk'_spec' a' ⟨b', b'_nonzero⟩], end /-- `f.num x` is the numerator of `x : f.codomain` as a reduced fraction. -/ noncomputable def num (x : φ.codomain) : A := classical.some (φ.exists_reduced_fraction x) /-- `f.num x` is the denominator of `x : f.codomain` as a reduced fraction. -/ noncomputable def denom (x : φ.codomain) : non_zero_divisors A := classical.some (classical.some_spec (φ.exists_reduced_fraction x)) lemma num_denom_reduced (x : φ.codomain) : ∀ {d}, d ∣ φ.num x → d ∣ φ.denom x → is_unit d := (classical.some_spec (classical.some_spec (φ.exists_reduced_fraction x))).1 @[simp] lemma mk'_num_denom (x : φ.codomain) : φ.mk' (φ.num x) (φ.denom x) = x := (classical.some_spec (classical.some_spec (φ.exists_reduced_fraction x))).2 lemma num_mul_denom_eq_num_iff_eq {x y : φ.codomain} : x * φ.to_map (φ.denom y) = φ.to_map (φ.num y) ↔ x = y := ⟨ λ h, by simpa only [mk'_num_denom] using φ.eq_mk'_iff_mul_eq.mpr h, λ h, φ.eq_mk'_iff_mul_eq.mp (by rw [h, mk'_num_denom]) ⟩ lemma num_mul_denom_eq_num_iff_eq' {x y : φ.codomain} : y * φ.to_map (φ.denom x) = φ.to_map (φ.num x) ↔ x = y := ⟨ λ h, by simpa only [eq_comm, mk'_num_denom] using φ.eq_mk'_iff_mul_eq.mpr h, λ h, φ.eq_mk'_iff_mul_eq.mp (by rw [h, mk'_num_denom]) ⟩ lemma num_mul_denom_eq_num_mul_denom_iff_eq {x y : φ.codomain} : φ.num y * φ.denom x = φ.num x * φ.denom y ↔ x = y := ⟨ λ h, by simpa only [mk'_num_denom] using φ.mk'_eq_of_eq h, λ h, by rw h ⟩ lemma eq_zero_of_num_eq_zero {x : φ.codomain} (h : φ.num x = 0) : x = 0 := φ.num_mul_denom_eq_num_iff_eq'.mp (by rw [zero_mul, h, ring_hom.map_zero]) lemma is_integer_of_is_unit_denom {x : φ.codomain} (h : is_unit (φ.denom x : A)) : φ.is_integer x := begin cases h with d hd, have d_ne_zero : φ.to_map (φ.denom x) ≠ 0 := φ.to_map_ne_zero_of_mem_non_zero_divisors (φ.denom x), use ↑d⁻¹ * φ.num x, refine trans _ (φ.mk'_num_denom x), rw [φ.to_map.map_mul, φ.to_map.map_units_inv, hd], apply mul_left_cancel' d_ne_zero, rw [←mul_assoc, mul_inv_cancel d_ne_zero, one_mul, φ.mk'_spec'] end lemma is_unit_denom_of_num_eq_zero {x : φ.codomain} (h : φ.num x = 0) : is_unit (φ.denom x : A) := φ.num_denom_reduced x (h.symm ▸ dvd_zero _) (dvd_refl _) end num_denom end fraction_map section algebra section is_integral variables {R S} {Rₘ Sₘ : Type*} [comm_ring Rₘ] [comm_ring Sₘ] [algebra R S] /-- Definition of the natural algebra induced by the localization of an algebra. Given an algebra `R → S`, a submonoid `R` of `M`, and a localization `Rₘ` for `M`, let `Sₘ` be the localization of `S` to the image of `M` under `algebra_map R S`. Then this is the natural algebra structure on `Rₘ → Sₘ`, such that the entire square commutes, where `localization_map.map_comp` gives the commutativity of the underlying maps -/ noncomputable def localization_algebra (M : submonoid R) (f : localization_map M Rₘ) (g : localization_map (algebra.algebra_map_submonoid S M) Sₘ) : algebra Rₘ Sₘ := (f.map (@algebra.mem_algebra_map_submonoid_of_mem R S _ _ _ _) g).to_algebra variables (f : localization_map M Rₘ) variables (g : localization_map (algebra.algebra_map_submonoid S M) Sₘ) lemma algebra_map_mk' (r : R) (m : M) : (@algebra_map Rₘ Sₘ _ _ (localization_algebra M f g)) (f.mk' r m) = g.mk' (algebra_map R S r) ⟨algebra_map R S m, algebra.mem_algebra_map_submonoid_of_mem m⟩ := localization_map.map_mk' f _ r m /-- Injectivity of the underlying `algebra_map` descends to the algebra induced by localization -/ lemma localization_algebra_injective (hRS : function.injective (algebra_map R S)) (hM : algebra.algebra_map_submonoid S M ≤ non_zero_divisors S) : function.injective (@algebra_map Rₘ Sₘ _ _ (localization_algebra M f g)) := begin rintros x y hxy, obtain ⟨a, b, rfl⟩ := localization_map.mk'_surjective f x, obtain ⟨c, d, rfl⟩ := localization_map.mk'_surjective f y, rw [algebra_map_mk' f g a b, algebra_map_mk' f g c d, localization_map.mk'_eq_iff_eq] at hxy, refine (localization_map.mk'_eq_iff_eq f).2 (congr_arg f.to_map (hRS _)), convert g.injective hM hxy; simp, end open polynomial /-- Given a particular witness to an element being algebraic over an algebra `R → S`, We can localize to a submonoid containing the leading coefficient to make it integral -/ theorem is_integral_localization_at_leading_coeff {x : S} (p : polynomial R) (hp : aeval x p = 0) (hM' : p.leading_coeff ∈ M) : @is_integral Rₘ _ _ _ (localization_algebra M f g) (g.to_map x) := begin by_cases triv : (1 : Rₘ) = 0, { exact ⟨0, ⟨trans leading_coeff_zero triv.symm, eval₂_zero _ _⟩⟩ }, haveI : nontrivial Rₘ := nontrivial_of_ne 1 0 triv, obtain ⟨b, hb⟩ := is_unit_iff_exists_inv.mp (localization_map.map_units f ⟨p.leading_coeff, hM'⟩), refine ⟨(p.map f.to_map) * C b, ⟨_, _⟩⟩, { refine monic_mul_C_of_leading_coeff_mul_eq_one _, rwa leading_coeff_map_of_leading_coeff_ne_zero f.to_map, refine λ hfp, zero_ne_one (trans (zero_mul b).symm (hfp ▸ hb) : (0 : Rₘ) = 1) }, { refine eval₂_mul_eq_zero_of_left _ _ _ _, erw [eval₂_map, localization_map.map_comp, ← hom_eval₂ _ (algebra_map R S) g.to_map x], exact trans (congr_arg g.to_map hp) g.to_map.map_zero } end /-- If `R → S` is an integral extension, `M` is a submonoid of `R`, `Rₘ` is the localization of `R` at `M`, and `Sₘ` is the localization of `S` at the image of `M` under the extension map, then the induced map `Rₘ → Sₘ` is also an integral extension -/ theorem is_integral_localization (H : ∀ x : S, is_integral R x) (x : Sₘ) : @is_integral Rₘ _ _ _ (localization_algebra M f g) x := begin by_cases triv : (1 : R) = 0, { have : (1 : Rₘ) = 0 := by convert congr_arg f.to_map triv; simp, exact ⟨0, ⟨trans leading_coeff_zero this.symm, eval₂_zero _ _⟩⟩ }, { haveI : nontrivial R := nontrivial_of_ne 1 0 triv, obtain ⟨⟨s, ⟨u, hu⟩⟩, hx⟩ := g.surj x, obtain ⟨v, hv⟩ := hu, obtain ⟨v', hv'⟩ := is_unit_iff_exists_inv'.1 (f.map_units ⟨v, hv.1⟩), refine @is_integral_of_is_integral_mul_unit Rₘ _ _ _ (localization_algebra M f g) x (g.to_map u) v' _ _, { replace hv' := congr_arg (@algebra_map Rₘ Sₘ _ _ (localization_algebra M f g)) hv', rw [ring_hom.map_mul, ring_hom.map_one, ← ring_hom.comp_apply _ f.to_map] at hv', erw localization_map.map_comp at hv', exact hv.2 ▸ hv' }, { obtain ⟨p, hp⟩ := H s, exact hx.symm ▸ is_integral_localization_at_leading_coeff f g p hp.2 (hp.1.symm ▸ M.one_mem) } } end end is_integral namespace integral_closure variables {L : Type*} [field K] [field L] {f : fraction_map A K} open algebra /-- If the field `L` is an algebraic extension of the integral domain `A`, the integral closure of `A` in `L` has fraction field `L`. -/ def fraction_map_of_algebraic [algebra A L] (alg : is_algebraic A L) (inj : ∀ x, algebra_map A L x = 0 → x = 0) : fraction_map (integral_closure A L) L := (algebra_map (integral_closure A L) L).to_localization_map (λ ⟨⟨y, integral⟩, nonzero⟩, have y ≠ 0 := λ h, mem_non_zero_divisors_iff_ne_zero.mp nonzero (subtype.ext_iff_val.mpr h), show is_unit y, from ⟨⟨y, y⁻¹, mul_inv_cancel this, inv_mul_cancel this⟩, rfl⟩) (λ z, let ⟨x, y, hy, hxy⟩ := exists_integral_multiple (alg z) inj in ⟨⟨x, ⟨y, mem_non_zero_divisors_iff_ne_zero.mpr hy⟩⟩, hxy⟩) (λ x y, ⟨ λ (h : x.1 = y.1), ⟨1, by simpa using subtype.ext_iff_val.mpr h⟩, λ ⟨c, hc⟩, congr_arg (algebra_map _ L) (mul_right_cancel' (mem_non_zero_divisors_iff_ne_zero.mp c.2) hc) ⟩) /-- If the field `L` is a finite extension of the fraction field of the integral domain `A`, the integral closure of `A` in `L` has fraction field `L`. -/ def fraction_map_of_finite_extension [algebra A L] [algebra f.codomain L] [is_scalar_tower A f.codomain L] [finite_dimensional f.codomain L] : fraction_map (integral_closure A L) L := fraction_map_of_algebraic (f.comap_is_algebraic_iff.mpr is_algebraic_of_finite) (λ x hx, f.to_map_eq_zero_iff.mp ((algebra_map f.codomain L).map_eq_zero.mp $ (is_scalar_tower.algebra_map_apply _ _ _ _).symm.trans hx)) end integral_closure end algebra variables (A) /-- The fraction field of an integral domain as a quotient type. -/ @[reducible] def fraction_ring := localization (non_zero_divisors A) namespace fraction_ring /-- Natural hom sending `x : A`, `A` an integral domain, to the equivalence class of `(x, 1)` in the field of fractions of `A`. -/ def of : fraction_map A (localization (non_zero_divisors A)) := localization.of (non_zero_divisors A) variables {A} noncomputable instance : field (fraction_ring A) := (of A).to_field @[simp] lemma mk_eq_div {r s} : (localization.mk r s : fraction_ring A) = ((of A).to_map r / (of A).to_map s : fraction_ring A) := by erw [localization.mk_eq_mk', (of A).mk'_eq_div] /-- Given an integral domain `A` and a localization map to a field of fractions `f : A →+* K`, we get an `A`-isomorphism between the field of fractions of `A` as a quotient type and `K`. -/ noncomputable def alg_equiv_of_quotient {K : Type*} [field K] (f : fraction_map A K) : fraction_ring A ≃ₐ[A] f.codomain := localization.alg_equiv_of_quotient f end fraction_ring
880da58129e7e9156ad87bd524ccc69426952a27
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/geometry/manifold/cont_mdiff.lean
e1d310f87da4d599874dd442cca19826e6672038
[ "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
93,608
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import geometry.manifold.mfderiv import geometry.manifold.local_invariant_properties /-! # Smooth functions between smooth manifolds We define `Cⁿ` functions between smooth manifolds, as functions which are `Cⁿ` in charts, and prove basic properties of these notions. ## Main definitions and statements Let `M ` and `M'` be two smooth manifolds, with respect to model with corners `I` and `I'`. Let `f : M → M'`. * `cont_mdiff_within_at I I' n f s x` states that the function `f` is `Cⁿ` within the set `s` around the point `x`. * `cont_mdiff_at I I' n f x` states that the function `f` is `Cⁿ` around `x`. * `cont_mdiff_on I I' n f s` states that the function `f` is `Cⁿ` on the set `s` * `cont_mdiff I I' n f` states that the function `f` is `Cⁿ`. * `cont_mdiff_on.comp` gives the invariance of the `Cⁿ` property under composition * `cont_mdiff_on.cont_mdiff_on_tangent_map_within` states that the bundled derivative of a `Cⁿ` function in a domain is `Cᵐ` when `m + 1 ≤ n`. * `cont_mdiff.cont_mdiff_tangent_map` states that the bundled derivative of a `Cⁿ` function is `Cᵐ` when `m + 1 ≤ n`. * `cont_mdiff_iff_cont_diff` states that, for functions between vector spaces, manifold-smoothness is equivalent to usual smoothness. We also give many basic properties of smooth functions between manifolds, following the API of smooth functions between vector spaces. ## Implementation details Many properties follow for free from the corresponding properties of functions in vector spaces, as being `Cⁿ` is a local property invariant under the smooth groupoid. We take advantage of the general machinery developed in `local_invariant_properties.lean` to get these properties automatically. For instance, the fact that being `Cⁿ` does not depend on the chart one considers is given by `lift_prop_within_at_indep_chart`. For this to work, the definition of `cont_mdiff_within_at` and friends has to follow definitionally the setup of local invariant properties. Still, we recast the definition in terms of extended charts in `cont_mdiff_on_iff` and `cont_mdiff_iff`. -/ open set function filter charted_space smooth_manifold_with_corners open_locale topological_space manifold /-! ### Definition of smooth functions between manifolds -/ variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] -- declare a smooth manifold `M` over the pair `(E, H)`. {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] [Is : smooth_manifold_with_corners I M] -- declare a smooth manifold `M'` over the pair `(E', H')`. {E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] (I' : model_with_corners 𝕜 E' H') {M' : Type*} [topological_space M'] [charted_space H' M'] [I's : smooth_manifold_with_corners I' M'] -- declare a smooth manifold `N` over the pair `(F, G)`. {F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] {G : Type*} [topological_space G] {J : model_with_corners 𝕜 F G} {N : Type*} [topological_space N] [charted_space G N] [Js : smooth_manifold_with_corners J N] -- declare a smooth manifold `N'` over the pair `(F', G')`. {F' : Type*} [normed_add_comm_group F'] [normed_space 𝕜 F'] {G' : Type*} [topological_space G'] {J' : model_with_corners 𝕜 F' G'} {N' : Type*} [topological_space N'] [charted_space G' N'] [J's : smooth_manifold_with_corners J' N'] -- declare functions, sets, points and smoothness indices {f f₁ : M → M'} {s s₁ t : set M} {x : M} {m n : with_top ℕ} /-- Property in the model space of a model with corners of being `C^n` within at set at a point, when read in the model vector space. This property will be lifted to manifolds to define smooth functions between manifolds. -/ def cont_diff_within_at_prop (n : with_top ℕ) (f : H → H') (s : set H) (x : H) : Prop := cont_diff_within_at 𝕜 n (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) (I x) lemma cont_diff_within_at_prop_self_source {f : E → H'} {s : set E} {x : E} : cont_diff_within_at_prop 𝓘(𝕜, E) I' n f s x ↔ cont_diff_within_at 𝕜 n (I' ∘ f) s x := begin simp_rw [cont_diff_within_at_prop, model_with_corners_self_coe, range_id, inter_univ], refl end lemma cont_diff_within_at_prop_self {f : E → E'} {s : set E} {x : E} : cont_diff_within_at_prop 𝓘(𝕜, E) 𝓘(𝕜, E') n f s x ↔ cont_diff_within_at 𝕜 n f s x := cont_diff_within_at_prop_self_source 𝓘(𝕜, E') lemma cont_diff_within_at_prop_self_target {f : H → E'} {s : set H} {x : H} : cont_diff_within_at_prop I 𝓘(𝕜, E') n f s x ↔ cont_diff_within_at 𝕜 n (f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) (I x) := iff.rfl /-- Being `Cⁿ` in the model space is a local property, invariant under smooth maps. Therefore, it will lift nicely to manifolds. -/ lemma cont_diff_within_at_local_invariant_prop (n : with_top ℕ) : (cont_diff_groupoid ∞ I).local_invariant_prop (cont_diff_groupoid ∞ I') (cont_diff_within_at_prop I I' n) := { is_local := begin assume s x u f u_open xu, have : I.symm ⁻¹' (s ∩ u) ∩ range I = (I.symm ⁻¹' s ∩ range I) ∩ I.symm ⁻¹' u, by simp only [inter_right_comm, preimage_inter], rw [cont_diff_within_at_prop, cont_diff_within_at_prop, this], symmetry, apply cont_diff_within_at_inter, have : u ∈ 𝓝 (I.symm (I x)), by { rw [model_with_corners.left_inv], exact is_open.mem_nhds u_open xu }, apply continuous_at.preimage_mem_nhds I.continuous_symm.continuous_at this, end, right_invariance' := begin assume s x f e he hx h, rw cont_diff_within_at_prop at h ⊢, have : I x = (I ∘ e.symm ∘ I.symm) (I (e x)), by simp only [hx] with mfld_simps, rw this at h, have : I (e x) ∈ (I.symm) ⁻¹' e.target ∩ range I, by simp only [hx] with mfld_simps, have := ((mem_groupoid_of_pregroupoid.2 he).2.cont_diff_within_at this).of_le le_top, convert (h.comp' _ this).mono_of_mem _ using 1, { ext y, simp only with mfld_simps }, refine mem_nhds_within.mpr ⟨I.symm ⁻¹' e.target, e.open_target.preimage I.continuous_symm, by simp_rw [mem_preimage, I.left_inv, e.maps_to hx], _⟩, mfld_set_tac end, congr_of_forall := begin assume s x f g h hx hf, apply hf.congr, { assume y hy, simp only with mfld_simps at hy, simp only [h, hy] with mfld_simps }, { simp only [hx] with mfld_simps } end, left_invariance' := begin assume s x f e' he' hs hx h, rw cont_diff_within_at_prop at h ⊢, have A : (I' ∘ f ∘ I.symm) (I x) ∈ (I'.symm ⁻¹' e'.source ∩ range I'), by simp only [hx] with mfld_simps, have := ((mem_groupoid_of_pregroupoid.2 he').1.cont_diff_within_at A).of_le le_top, convert this.comp _ h _, { ext y, simp only with mfld_simps }, { assume y hy, simp only with mfld_simps at hy, simpa only [hy] with mfld_simps using hs hy.1 } end } lemma cont_diff_within_at_prop_mono (n : with_top ℕ) ⦃s x t⦄ ⦃f : H → H'⦄ (hts : t ⊆ s) (h : cont_diff_within_at_prop I I' n f s x) : cont_diff_within_at_prop I I' n f t x := begin apply h.mono (λ y hy, _), simp only with mfld_simps at hy, simp only [hy, hts _] with mfld_simps end lemma cont_diff_within_at_prop_id (x : H) : cont_diff_within_at_prop I I ∞ id univ x := begin simp [cont_diff_within_at_prop], have : cont_diff_within_at 𝕜 ∞ id (range I) (I x) := cont_diff_id.cont_diff_at.cont_diff_within_at, apply this.congr (λ y hy, _), { simp only with mfld_simps }, { simp only [model_with_corners.right_inv I hy] with mfld_simps } end /-- A function is `n` times continuously differentiable within a set at a point in a manifold if it is continuous and it is `n` times continuously differentiable in this set around this point, when read in the preferred chart at this point. -/ def cont_mdiff_within_at (n : with_top ℕ) (f : M → M') (s : set M) (x : M) := lift_prop_within_at (cont_diff_within_at_prop I I' n) f s x /-- Abbreviation for `cont_mdiff_within_at I I' ⊤ f s x`. See also documentation for `smooth`. -/ @[reducible] def smooth_within_at (f : M → M') (s : set M) (x : M) := cont_mdiff_within_at I I' ⊤ f s x /-- A function is `n` times continuously differentiable at a point in a manifold if it is continuous and it is `n` times continuously differentiable around this point, when read in the preferred chart at this point. -/ def cont_mdiff_at (n : with_top ℕ) (f : M → M') (x : M) := cont_mdiff_within_at I I' n f univ x lemma cont_mdiff_at_iff {n : with_top ℕ} {f : M → M'} {x : M} : cont_mdiff_at I I' n f x ↔ continuous_at f x ∧ cont_diff_within_at 𝕜 n (ext_chart_at I' (f x) ∘ f ∘ (ext_chart_at I x).symm) (range I) (ext_chart_at I x x) := lift_prop_at_iff.trans $ by { rw [cont_diff_within_at_prop, preimage_univ, univ_inter], refl } /-- Abbreviation for `cont_mdiff_at I I' ⊤ f x`. See also documentation for `smooth`. -/ @[reducible] def smooth_at (f : M → M') (x : M) := cont_mdiff_at I I' ⊤ f x /-- A function is `n` times continuously differentiable in a set of a manifold if it is continuous and, for any pair of points, it is `n` times continuously differentiable on this set in the charts around these points. -/ def cont_mdiff_on (n : with_top ℕ) (f : M → M') (s : set M) := ∀ x ∈ s, cont_mdiff_within_at I I' n f s x /-- Abbreviation for `cont_mdiff_on I I' ⊤ f s`. See also documentation for `smooth`. -/ @[reducible] def smooth_on (f : M → M') (s : set M) := cont_mdiff_on I I' ⊤ f s /-- A function is `n` times continuously differentiable in a manifold if it is continuous and, for any pair of points, it is `n` times continuously differentiable in the charts around these points. -/ def cont_mdiff (n : with_top ℕ) (f : M → M') := ∀ x, cont_mdiff_at I I' n f x /-- Abbreviation for `cont_mdiff I I' ⊤ f`. Short note to work with these abbreviations: a lemma of the form `cont_mdiff_foo.bar` will apply fine to an assumption `smooth_foo` using dot notation or normal notation. If the consequence `bar` of the lemma involves `cont_diff`, it is still better to restate the lemma replacing `cont_diff` with `smooth` both in the assumption and in the conclusion, to make it possible to use `smooth` consistently. This also applies to `smooth_at`, `smooth_on` and `smooth_within_at`.-/ @[reducible] def smooth (f : M → M') := cont_mdiff I I' ⊤ f /-! ### Basic properties of smooth functions between manifolds -/ variables {I I'} lemma cont_mdiff.smooth (h : cont_mdiff I I' ⊤ f) : smooth I I' f := h lemma smooth.cont_mdiff (h : smooth I I' f) : cont_mdiff I I' ⊤ f := h lemma cont_mdiff_on.smooth_on (h : cont_mdiff_on I I' ⊤ f s) : smooth_on I I' f s := h lemma smooth_on.cont_mdiff_on (h : smooth_on I I' f s) : cont_mdiff_on I I' ⊤ f s := h lemma cont_mdiff_at.smooth_at (h : cont_mdiff_at I I' ⊤ f x) : smooth_at I I' f x := h lemma smooth_at.cont_mdiff_at (h : smooth_at I I' f x) : cont_mdiff_at I I' ⊤ f x := h lemma cont_mdiff_within_at.smooth_within_at (h : cont_mdiff_within_at I I' ⊤ f s x) : smooth_within_at I I' f s x := h lemma smooth_within_at.cont_mdiff_within_at (h : smooth_within_at I I' f s x) : cont_mdiff_within_at I I' ⊤ f s x := h lemma cont_mdiff.cont_mdiff_at (h : cont_mdiff I I' n f) : cont_mdiff_at I I' n f x := h x lemma smooth.smooth_at (h : smooth I I' f) : smooth_at I I' f x := cont_mdiff.cont_mdiff_at h lemma cont_mdiff_within_at_univ : cont_mdiff_within_at I I' n f univ x ↔ cont_mdiff_at I I' n f x := iff.rfl lemma smooth_within_at_univ : smooth_within_at I I' f univ x ↔ smooth_at I I' f x := cont_mdiff_within_at_univ lemma cont_mdiff_on_univ : cont_mdiff_on I I' n f univ ↔ cont_mdiff I I' n f := by simp only [cont_mdiff_on, cont_mdiff, cont_mdiff_within_at_univ, forall_prop_of_true, mem_univ] lemma smooth_on_univ : smooth_on I I' f univ ↔ smooth I I' f := cont_mdiff_on_univ /-- One can reformulate smoothness within a set at a point as continuity within this set at this point, and smoothness in the corresponding extended chart. -/ lemma cont_mdiff_within_at_iff : cont_mdiff_within_at I I' n f s x ↔ continuous_within_at f s x ∧ cont_diff_within_at 𝕜 n ((ext_chart_at I' (f x)) ∘ f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) (ext_chart_at I x x) := iff.rfl /-- One can reformulate smoothness within a set at a point as continuity within this set at this point, and smoothness in the corresponding extended chart. This form states smoothness of `f` written in such a way that the set is restricted to lie within the domain/codomain of the corresponding charts. Even though this expression is more complicated than the one in `cont_mdiff_within_at_iff`, it is a smaller set, but their germs at `ext_chart_at I x x` are equal. It is sometimes useful to rewrite using this in the goal. -/ lemma cont_mdiff_within_at_iff' : cont_mdiff_within_at I I' n f s x ↔ continuous_within_at f s x ∧ cont_diff_within_at 𝕜 n ((ext_chart_at I' (f x)) ∘ f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' (s ∩ f ⁻¹' (ext_chart_at I' (f x)).source)) (ext_chart_at I x x) := begin rw [cont_mdiff_within_at_iff, and.congr_right_iff], set e := ext_chart_at I x, set e' := ext_chart_at I' (f x), refine λ hc, cont_diff_within_at_congr_nhds _, rw [← e.image_source_inter_eq', ← ext_chart_at_map_nhds_within_eq_image, ← ext_chart_at_map_nhds_within, inter_comm, nhds_within_inter_of_mem], exact hc (ext_chart_at_source_mem_nhds _ _) end /-- One can reformulate smoothness within a set at a point as continuity within this set at this point, and smoothness in the corresponding extended chart in the target. -/ lemma cont_mdiff_within_at_iff_target : cont_mdiff_within_at I I' n f s x ↔ continuous_within_at f s x ∧ cont_mdiff_within_at I 𝓘(𝕜, E') n (ext_chart_at I' (f x) ∘ f) s x := begin simp_rw [cont_mdiff_within_at, lift_prop_within_at, ← and_assoc], have cont : (continuous_within_at f s x ∧ continuous_within_at (ext_chart_at I' (f x) ∘ f) s x) ↔ continuous_within_at f s x, { refine ⟨λ h, h.1, λ h, ⟨h, _⟩⟩, have h₂ := (chart_at H' (f x)).continuous_to_fun.continuous_within_at (mem_chart_source _ _), refine ((I'.continuous_at.comp_continuous_within_at h₂).comp' h).mono_of_mem _, exact inter_mem self_mem_nhds_within (h.preimage_mem_nhds_within $ (chart_at _ _).open_source.mem_nhds $ mem_chart_source _ _) }, simp_rw [cont, cont_diff_within_at_prop, ext_chart_at, local_equiv.coe_trans, model_with_corners.to_local_equiv_coe, local_homeomorph.coe_coe, model_with_corners_self_coe, chart_at_self_eq, local_homeomorph.refl_apply, comp.left_id] end lemma smooth_within_at_iff : smooth_within_at I I' f s x ↔ continuous_within_at f s x ∧ cont_diff_within_at 𝕜 ∞ (ext_chart_at I' (f x) ∘ f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) (ext_chart_at I x x) := cont_mdiff_within_at_iff lemma smooth_within_at_iff_target : smooth_within_at I I' f s x ↔ continuous_within_at f s x ∧ smooth_within_at I 𝓘(𝕜, E') (ext_chart_at I' (f x) ∘ f) s x := cont_mdiff_within_at_iff_target lemma cont_mdiff_at_iff_target {x : M} : cont_mdiff_at I I' n f x ↔ continuous_at f x ∧ cont_mdiff_at I 𝓘(𝕜, E') n (ext_chart_at I' (f x) ∘ f) x := by rw [cont_mdiff_at, cont_mdiff_at, cont_mdiff_within_at_iff_target, continuous_within_at_univ] lemma smooth_at_iff_target {x : M} : smooth_at I I' f x ↔ continuous_at f x ∧ smooth_at I 𝓘(𝕜, E') (ext_chart_at I' (f x) ∘ f) x := cont_mdiff_at_iff_target include Is I's /-- One can reformulate smoothness within a set at a point as continuity within this set at this point, and smoothness in any chart containing that point. -/ lemma cont_mdiff_within_at_iff_of_mem_source {x' : M} {y : M'} (hx : x' ∈ (chart_at H x).source) (hy : f x' ∈ (chart_at H' y).source) : cont_mdiff_within_at I I' n f s x' ↔ continuous_within_at f s x' ∧ cont_diff_within_at 𝕜 n (ext_chart_at I' y ∘ f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) (ext_chart_at I x x') := (cont_diff_within_at_local_invariant_prop I I' n).lift_prop_within_at_indep_chart (structure_groupoid.chart_mem_maximal_atlas _ x) hx (structure_groupoid.chart_mem_maximal_atlas _ y) hy lemma cont_mdiff_within_at_iff_of_mem_source' {x' : M} {y : M'} (hx : x' ∈ (chart_at H x).source) (hy : f x' ∈ (chart_at H' y).source) : cont_mdiff_within_at I I' n f s x' ↔ continuous_within_at f s x' ∧ cont_diff_within_at 𝕜 n ((ext_chart_at I' y) ∘ f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' (s ∩ f ⁻¹' (ext_chart_at I' y).source)) (ext_chart_at I x x') := begin refine (cont_mdiff_within_at_iff_of_mem_source hx hy).trans _, rw [← ext_chart_at_source I] at hx, rw [← ext_chart_at_source I'] at hy, rw [and.congr_right_iff], set e := ext_chart_at I x, set e' := ext_chart_at I' (f x), refine λ hc, cont_diff_within_at_congr_nhds _, rw [← e.image_source_inter_eq', ← ext_chart_at_map_nhds_within_eq_image' I x hx, ← ext_chart_at_map_nhds_within' I x hx, inter_comm, nhds_within_inter_of_mem], exact hc ((ext_chart_at_open_source _ _).mem_nhds hy) end lemma cont_mdiff_at_iff_of_mem_source {x' : M} {y : M'} (hx : x' ∈ (chart_at H x).source) (hy : f x' ∈ (chart_at H' y).source) : cont_mdiff_at I I' n f x' ↔ continuous_at f x' ∧ cont_diff_within_at 𝕜 n (ext_chart_at I' y ∘ f ∘ (ext_chart_at I x).symm) (range I) (ext_chart_at I x x') := (cont_mdiff_within_at_iff_of_mem_source hx hy).trans $ by rw [continuous_within_at_univ, preimage_univ, univ_inter] omit Is lemma cont_mdiff_within_at_iff_target_of_mem_source {x : M} {y : M'} (hy : f x ∈ (chart_at H' y).source) : cont_mdiff_within_at I I' n f s x ↔ continuous_within_at f s x ∧ cont_mdiff_within_at I 𝓘(𝕜, E') n (ext_chart_at I' y ∘ f) s x := begin simp_rw [cont_mdiff_within_at], rw [(cont_diff_within_at_local_invariant_prop I I' n).lift_prop_within_at_indep_chart_target (chart_mem_maximal_atlas I' y) hy, and_congr_right], intro hf, simp_rw [structure_groupoid.lift_prop_within_at_self_target], simp_rw [((chart_at H' y).continuous_at hy).comp_continuous_within_at hf], rw [← ext_chart_at_source I'] at hy, simp_rw [(ext_chart_at_continuous_at' I' _ hy).comp_continuous_within_at hf], refl, end lemma cont_mdiff_at_iff_target_of_mem_source {x : M} {y : M'} (hy : f x ∈ (chart_at H' y).source) : cont_mdiff_at I I' n f x ↔ continuous_at f x ∧ cont_mdiff_at I 𝓘(𝕜, E') n (ext_chart_at I' y ∘ f) x := begin rw [cont_mdiff_at, cont_mdiff_within_at_iff_target_of_mem_source hy, continuous_within_at_univ, cont_mdiff_at], apply_instance end omit I's variable (I) lemma model_with_corners.symm_continuous_within_at_comp_right_iff {X} [topological_space X] {f : H → X} {s : set H} {x : H} : continuous_within_at (f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) (I x) ↔ continuous_within_at f s x := begin refine ⟨λ h, _, λ h, _⟩, { have := h.comp I.continuous_within_at (maps_to_preimage _ _), simp_rw [preimage_inter, preimage_preimage, I.left_inv, preimage_id', preimage_range, inter_univ] at this, rwa [function.comp.assoc, I.symm_comp_self] at this }, { rw [← I.left_inv x] at h, exact h.comp I.continuous_within_at_symm (inter_subset_left _ _) } end variable {I} lemma ext_chart_at_symm_continuous_within_at_comp_right_iff {X} [topological_space X] {f : M → X} {s : set M} {x x' : M} : continuous_within_at (f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) (ext_chart_at I x x') ↔ continuous_within_at (f ∘ (chart_at H x).symm) ((chart_at H x).symm ⁻¹' s) (chart_at H x x') := by convert I.symm_continuous_within_at_comp_right_iff; refl include Is lemma cont_mdiff_within_at_iff_source_of_mem_source {x' : M} (hx' : x' ∈ (chart_at H x).source) : cont_mdiff_within_at I I' n f s x' ↔ cont_mdiff_within_at 𝓘(𝕜, E) I' n (f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) (ext_chart_at I x x') := begin have h2x' := hx', rw [← ext_chart_at_source I] at h2x', simp_rw [cont_mdiff_within_at, (cont_diff_within_at_local_invariant_prop I I' n).lift_prop_within_at_indep_chart_source (chart_mem_maximal_atlas I x) hx', structure_groupoid.lift_prop_within_at_self_source, ext_chart_at_symm_continuous_within_at_comp_right_iff, cont_diff_within_at_prop_self_source, cont_diff_within_at_prop, function.comp, (chart_at H x).left_inv hx', (ext_chart_at I x).left_inv h2x'], refl, end lemma cont_mdiff_at_iff_source_of_mem_source {x' : M} (hx' : x' ∈ (chart_at H x).source) : cont_mdiff_at I I' n f x' ↔ cont_mdiff_within_at 𝓘(𝕜, E) I' n (f ∘ (ext_chart_at I x).symm) (range I) (ext_chart_at I x x') := by simp_rw [cont_mdiff_at, cont_mdiff_within_at_iff_source_of_mem_source hx', preimage_univ, univ_inter] lemma cont_mdiff_at_ext_chart_at' {x' : M} (h : x' ∈ (chart_at H x).source) : cont_mdiff_at I 𝓘(𝕜, E) n (ext_chart_at I x) x' := begin refine (cont_mdiff_at_iff_of_mem_source h (mem_chart_source _ _)).mpr _, rw [← ext_chart_at_source I] at h, refine ⟨ext_chart_at_continuous_at' _ _ h, _⟩, refine cont_diff_within_at_id.congr_of_eventually_eq _ _, { refine eventually_eq_of_mem (ext_chart_at_target_mem_nhds_within' I x h) (λ x₂ hx₂, _), simp_rw [function.comp_apply, (ext_chart_at I x).right_inv hx₂], refl }, simp_rw [function.comp_apply, (ext_chart_at I x).right_inv ((ext_chart_at I x).maps_to h)], refl end lemma cont_mdiff_at_ext_chart_at : cont_mdiff_at I 𝓘(𝕜, E) n (ext_chart_at I x) x := cont_mdiff_at_ext_chart_at' $ mem_chart_source H x include I's /-- If the set where you want `f` to be smooth lies entirely in a single chart, and `f` maps it into a single chart, the smoothness of `f` on that set can be expressed by purely looking in these charts. Note: this lemma uses `ext_chart_at I x '' s` instead of `(ext_chart_at I x).symm ⁻¹' s` to ensure that this set lies in `(ext_chart_at I x).target`. -/ lemma cont_mdiff_on_iff_of_subset_source {x : M} {y : M'} (hs : s ⊆ (chart_at H x).source) (h2s : maps_to f s (chart_at H' y).source) : cont_mdiff_on I I' n f s ↔ continuous_on f s ∧ cont_diff_on 𝕜 n (ext_chart_at I' y ∘ f ∘ (ext_chart_at I x).symm) (ext_chart_at I x '' s) := begin split, { refine λ H, ⟨λ x hx, (H x hx).1, _⟩, rintro _ ⟨x', hx', rfl⟩, exact ((cont_mdiff_within_at_iff_of_mem_source (hs hx') (h2s.image_subset $ mem_image_of_mem f hx')).mp (H _ hx')).2.mono (maps_to_ext_chart_at I x hs).image_subset }, { rintro ⟨h1, h2⟩ x' hx', refine (cont_mdiff_within_at_iff_of_mem_source (hs hx') (h2s.image_subset $ mem_image_of_mem f hx')).mpr ⟨h1.continuous_within_at hx', _⟩, refine (h2 _ $ mem_image_of_mem _ hx').mono_of_mem _, rw [← ext_chart_at_source I] at hs, rw [(ext_chart_at I x).image_eq_target_inter_inv_preimage hs], refine inter_mem _ (ext_chart_preimage_mem_nhds_within' I x (hs hx') self_mem_nhds_within), have := ext_chart_at_target_mem_nhds_within' I x (hs hx'), refine nhds_within_mono _ (inter_subset_right _ _) this } end /-- One can reformulate smoothness on a set as continuity on this set, and smoothness in any extended chart. -/ lemma cont_mdiff_on_iff : cont_mdiff_on I I' n f s ↔ continuous_on f s ∧ ∀ (x : M) (y : M'), cont_diff_on 𝕜 n (ext_chart_at I' y ∘ f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' (s ∩ f ⁻¹' (ext_chart_at I' y).source)) := begin split, { assume h, refine ⟨λ x hx, (h x hx).1, λ x y z hz, _⟩, simp only with mfld_simps at hz, let w := (ext_chart_at I x).symm z, have : w ∈ s, by simp only [w, hz] with mfld_simps, specialize h w this, have w1 : w ∈ (chart_at H x).source, by simp only [w, hz] with mfld_simps, have w2 : f w ∈ (chart_at H' y).source, by simp only [w, hz] with mfld_simps, convert ((cont_mdiff_within_at_iff_of_mem_source w1 w2).mp h).2.mono _, { simp only [w, hz] with mfld_simps }, { mfld_set_tac } }, { rintros ⟨hcont, hdiff⟩ x hx, refine ((cont_diff_within_at_local_invariant_prop I I' n).lift_prop_within_at_iff $ hcont x hx).mpr _, dsimp [cont_diff_within_at_prop], convert hdiff x (f x) (ext_chart_at I x x) (by simp only [hx] with mfld_simps) using 1, mfld_set_tac } end /-- One can reformulate smoothness on a set as continuity on this set, and smoothness in any extended chart in the target. -/ lemma cont_mdiff_on_iff_target : cont_mdiff_on I I' n f s ↔ continuous_on f s ∧ ∀ (y : M'), cont_mdiff_on I 𝓘(𝕜, E') n (ext_chart_at I' y ∘ f) (s ∩ f ⁻¹' (ext_chart_at I' y).source) := begin inhabit E', simp only [cont_mdiff_on_iff, model_with_corners.source_eq, chart_at_self_eq, local_homeomorph.refl_local_equiv, local_equiv.refl_trans, ext_chart_at.equations._eqn_1, set.preimage_univ, set.inter_univ, and.congr_right_iff], intros h, split, { refine λ h' y, ⟨_, λ x _, h' x y⟩, have h'' : continuous_on _ univ := (model_with_corners.continuous I').continuous_on, convert (h''.comp' (chart_at H' y).continuous_to_fun).comp' h, simp }, { exact λ h' x y, (h' y).2 x default } end lemma smooth_on_iff : smooth_on I I' f s ↔ continuous_on f s ∧ ∀ (x : M) (y : M'), cont_diff_on 𝕜 ⊤ (ext_chart_at I' y ∘ f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' (s ∩ f ⁻¹' (ext_chart_at I' y).source)) := cont_mdiff_on_iff lemma smooth_on_iff_target : smooth_on I I' f s ↔ continuous_on f s ∧ ∀ (y : M'), smooth_on I 𝓘(𝕜, E') (ext_chart_at I' y ∘ f) (s ∩ f ⁻¹' (ext_chart_at I' y).source) := cont_mdiff_on_iff_target /-- One can reformulate smoothness as continuity and smoothness in any extended chart. -/ lemma cont_mdiff_iff : cont_mdiff I I' n f ↔ continuous f ∧ ∀ (x : M) (y : M'), cont_diff_on 𝕜 n (ext_chart_at I' y ∘ f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' (f ⁻¹' (ext_chart_at I' y).source)) := by simp [← cont_mdiff_on_univ, cont_mdiff_on_iff, continuous_iff_continuous_on_univ] /-- One can reformulate smoothness as continuity and smoothness in any extended chart in the target. -/ lemma cont_mdiff_iff_target : cont_mdiff I I' n f ↔ continuous f ∧ ∀ (y : M'), cont_mdiff_on I 𝓘(𝕜, E') n (ext_chart_at I' y ∘ f) (f ⁻¹' (ext_chart_at I' y).source) := begin rw [← cont_mdiff_on_univ, cont_mdiff_on_iff_target], simp [continuous_iff_continuous_on_univ] end lemma smooth_iff : smooth I I' f ↔ continuous f ∧ ∀ (x : M) (y : M'), cont_diff_on 𝕜 ⊤ (ext_chart_at I' y ∘ f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' (f ⁻¹' (ext_chart_at I' y).source)) := cont_mdiff_iff lemma smooth_iff_target : smooth I I' f ↔ continuous f ∧ ∀ (y : M'), smooth_on I 𝓘(𝕜, E') (ext_chart_at I' y ∘ f) (f ⁻¹' (ext_chart_at I' y).source) := cont_mdiff_iff_target omit Is I's /-! ### Deducing smoothness from higher smoothness -/ lemma cont_mdiff_within_at.of_le (hf : cont_mdiff_within_at I I' n f s x) (le : m ≤ n) : cont_mdiff_within_at I I' m f s x := ⟨hf.1, hf.2.of_le le⟩ lemma cont_mdiff_at.of_le (hf : cont_mdiff_at I I' n f x) (le : m ≤ n) : cont_mdiff_at I I' m f x := cont_mdiff_within_at.of_le hf le lemma cont_mdiff_on.of_le (hf : cont_mdiff_on I I' n f s) (le : m ≤ n) : cont_mdiff_on I I' m f s := λ x hx, (hf x hx).of_le le lemma cont_mdiff.of_le (hf : cont_mdiff I I' n f) (le : m ≤ n) : cont_mdiff I I' m f := λ x, (hf x).of_le le /-! ### Deducing smoothness from smoothness one step beyond -/ lemma cont_mdiff_within_at.of_succ {n : ℕ} (h : cont_mdiff_within_at I I' n.succ f s x) : cont_mdiff_within_at I I' n f s x := h.of_le (with_top.coe_le_coe.2 (nat.le_succ n)) lemma cont_mdiff_at.of_succ {n : ℕ} (h : cont_mdiff_at I I' n.succ f x) : cont_mdiff_at I I' n f x := cont_mdiff_within_at.of_succ h lemma cont_mdiff_on.of_succ {n : ℕ} (h : cont_mdiff_on I I' n.succ f s) : cont_mdiff_on I I' n f s := λ x hx, (h x hx).of_succ lemma cont_mdiff.of_succ {n : ℕ} (h : cont_mdiff I I' n.succ f) : cont_mdiff I I' n f := λ x, (h x).of_succ /-! ### Deducing continuity from smoothness -/ lemma cont_mdiff_within_at.continuous_within_at (hf : cont_mdiff_within_at I I' n f s x) : continuous_within_at f s x := hf.1 lemma cont_mdiff_at.continuous_at (hf : cont_mdiff_at I I' n f x) : continuous_at f x := (continuous_within_at_univ _ _ ).1 $ cont_mdiff_within_at.continuous_within_at hf lemma cont_mdiff_on.continuous_on (hf : cont_mdiff_on I I' n f s) : continuous_on f s := λ x hx, (hf x hx).continuous_within_at lemma cont_mdiff.continuous (hf : cont_mdiff I I' n f) : continuous f := continuous_iff_continuous_at.2 $ λ x, (hf x).continuous_at /-! ### Deducing differentiability from smoothness -/ lemma cont_mdiff_within_at.mdifferentiable_within_at (hf : cont_mdiff_within_at I I' n f s x) (hn : 1 ≤ n) : mdifferentiable_within_at I I' f s x := begin suffices h : mdifferentiable_within_at I I' f (s ∩ (f ⁻¹' (ext_chart_at I' (f x)).source)) x, { rwa mdifferentiable_within_at_inter' at h, apply (hf.1).preimage_mem_nhds_within, exact is_open.mem_nhds (ext_chart_at_open_source I' (f x)) (mem_ext_chart_source I' (f x)) }, rw mdifferentiable_within_at_iff, exact ⟨hf.1.mono (inter_subset_left _ _), (hf.2.differentiable_within_at hn).mono (by mfld_set_tac)⟩, end lemma cont_mdiff_at.mdifferentiable_at (hf : cont_mdiff_at I I' n f x) (hn : 1 ≤ n) : mdifferentiable_at I I' f x := mdifferentiable_within_at_univ.1 $ cont_mdiff_within_at.mdifferentiable_within_at hf hn lemma cont_mdiff_on.mdifferentiable_on (hf : cont_mdiff_on I I' n f s) (hn : 1 ≤ n) : mdifferentiable_on I I' f s := λ x hx, (hf x hx).mdifferentiable_within_at hn lemma cont_mdiff.mdifferentiable (hf : cont_mdiff I I' n f) (hn : 1 ≤ n) : mdifferentiable I I' f := λ x, (hf x).mdifferentiable_at hn lemma smooth.mdifferentiable (hf : smooth I I' f) : mdifferentiable I I' f := cont_mdiff.mdifferentiable hf le_top lemma smooth.mdifferentiable_at (hf : smooth I I' f) : mdifferentiable_at I I' f x := hf.mdifferentiable x lemma smooth.mdifferentiable_within_at (hf : smooth I I' f) : mdifferentiable_within_at I I' f s x := hf.mdifferentiable_at.mdifferentiable_within_at /-! ### `C^∞` smoothness -/ lemma cont_mdiff_within_at_top : smooth_within_at I I' f s x ↔ (∀n:ℕ, cont_mdiff_within_at I I' n f s x) := ⟨λ h n, ⟨h.1, cont_diff_within_at_top.1 h.2 n⟩, λ H, ⟨(H 0).1, cont_diff_within_at_top.2 (λ n, (H n).2)⟩⟩ lemma cont_mdiff_at_top : smooth_at I I' f x ↔ (∀n:ℕ, cont_mdiff_at I I' n f x) := cont_mdiff_within_at_top lemma cont_mdiff_on_top : smooth_on I I' f s ↔ (∀n:ℕ, cont_mdiff_on I I' n f s) := ⟨λ h n, h.of_le le_top, λ h x hx, cont_mdiff_within_at_top.2 (λ n, h n x hx)⟩ lemma cont_mdiff_top : smooth I I' f ↔ (∀n:ℕ, cont_mdiff I I' n f) := ⟨λ h n, h.of_le le_top, λ h x, cont_mdiff_within_at_top.2 (λ n, h n x)⟩ lemma cont_mdiff_within_at_iff_nat : cont_mdiff_within_at I I' n f s x ↔ (∀m:ℕ, (m : with_top ℕ) ≤ n → cont_mdiff_within_at I I' m f s x) := begin refine ⟨λ h m hm, h.of_le hm, λ h, _⟩, cases n, { exact cont_mdiff_within_at_top.2 (λ n, h n le_top) }, { exact h n le_rfl } end /-! ### Restriction to a smaller set -/ lemma cont_mdiff_within_at.mono (hf : cont_mdiff_within_at I I' n f s x) (hts : t ⊆ s) : cont_mdiff_within_at I I' n f t x := structure_groupoid.local_invariant_prop.lift_prop_within_at_mono (cont_diff_within_at_prop_mono I I' n) hf hts lemma cont_mdiff_at.cont_mdiff_within_at (hf : cont_mdiff_at I I' n f x) : cont_mdiff_within_at I I' n f s x := cont_mdiff_within_at.mono hf (subset_univ _) lemma smooth_at.smooth_within_at (hf : smooth_at I I' f x) : smooth_within_at I I' f s x := cont_mdiff_at.cont_mdiff_within_at hf lemma cont_mdiff_on.mono (hf : cont_mdiff_on I I' n f s) (hts : t ⊆ s) : cont_mdiff_on I I' n f t := λ x hx, (hf x (hts hx)).mono hts lemma cont_mdiff.cont_mdiff_on (hf : cont_mdiff I I' n f) : cont_mdiff_on I I' n f s := λ x hx, (hf x).cont_mdiff_within_at lemma smooth.smooth_on (hf : smooth I I' f) : smooth_on I I' f s := cont_mdiff.cont_mdiff_on hf lemma cont_mdiff_within_at_inter' (ht : t ∈ 𝓝[s] x) : cont_mdiff_within_at I I' n f (s ∩ t) x ↔ cont_mdiff_within_at I I' n f s x := (cont_diff_within_at_local_invariant_prop I I' n).lift_prop_within_at_inter' ht lemma cont_mdiff_within_at_inter (ht : t ∈ 𝓝 x) : cont_mdiff_within_at I I' n f (s ∩ t) x ↔ cont_mdiff_within_at I I' n f s x := (cont_diff_within_at_local_invariant_prop I I' n).lift_prop_within_at_inter ht lemma cont_mdiff_within_at.cont_mdiff_at (h : cont_mdiff_within_at I I' n f s x) (ht : s ∈ 𝓝 x) : cont_mdiff_at I I' n f x := (cont_diff_within_at_local_invariant_prop I I' n).lift_prop_at_of_lift_prop_within_at h ht lemma smooth_within_at.smooth_at (h : smooth_within_at I I' f s x) (ht : s ∈ 𝓝 x) : smooth_at I I' f x := cont_mdiff_within_at.cont_mdiff_at h ht lemma cont_mdiff_on.cont_mdiff_at (h : cont_mdiff_on I I' n f s) (hx : s ∈ 𝓝 x) : cont_mdiff_at I I' n f x := (h x (mem_of_mem_nhds hx)).cont_mdiff_at hx lemma smooth_on.smooth_at (h : smooth_on I I' f s) (hx : s ∈ 𝓝 x) : smooth_at I I' f x := h.cont_mdiff_at hx include Is lemma cont_mdiff_on_ext_chart_at : cont_mdiff_on I 𝓘(𝕜, E) n (ext_chart_at I x) (chart_at H x).source := λ x' hx', (cont_mdiff_at_ext_chart_at' hx').cont_mdiff_within_at include I's /-- A function is `C^n` within a set at a point, for `n : ℕ`, if and only if it is `C^n` on a neighborhood of this point. -/ lemma cont_mdiff_within_at_iff_cont_mdiff_on_nhds {n : ℕ} : cont_mdiff_within_at I I' n f s x ↔ ∃ u ∈ 𝓝[insert x s] x, cont_mdiff_on I I' n f u := begin split, { assume h, -- the property is true in charts. We will pull such a good neighborhood in the chart to the -- manifold. For this, we need to restrict to a small enough set where everything makes sense obtain ⟨o, o_open, xo, ho, h'o⟩ : ∃ (o : set M), is_open o ∧ x ∈ o ∧ o ⊆ (chart_at H x).source ∧ o ∩ s ⊆ f ⁻¹' (chart_at H' (f x)).source, { have : (chart_at H' (f x)).source ∈ 𝓝 (f x) := is_open.mem_nhds (local_homeomorph.open_source _) (mem_chart_source H' (f x)), rcases mem_nhds_within.1 (h.1.preimage_mem_nhds_within this) with ⟨u, u_open, xu, hu⟩, refine ⟨u ∩ (chart_at H x).source, _, ⟨xu, mem_chart_source _ _⟩, _, _⟩, { exact is_open.inter u_open (local_homeomorph.open_source _) }, { assume y hy, exact hy.2 }, { assume y hy, exact hu ⟨hy.1.1, hy.2⟩ } }, have h' : cont_mdiff_within_at I I' n f (s ∩ o) x := h.mono (inter_subset_left _ _), simp only [cont_mdiff_within_at, lift_prop_within_at, cont_diff_within_at_prop] at h', -- let `u` be a good neighborhood in the chart where the function is smooth rcases h.2.cont_diff_on le_rfl with ⟨u, u_nhds, u_subset, hu⟩, -- pull it back to the manifold, and intersect with a suitable neighborhood of `x`, to get the -- desired good neighborhood `v`. let v := ((insert x s) ∩ o) ∩ (ext_chart_at I x) ⁻¹' u, have v_incl : v ⊆ (chart_at H x).source := λ y hy, ho hy.1.2, have v_incl' : ∀ y ∈ v, f y ∈ (chart_at H' (f x)).source, { assume y hy, rcases hy.1.1 with rfl|h', { simp only with mfld_simps }, { apply h'o ⟨hy.1.2, h'⟩ } }, refine ⟨v, _, _⟩, show v ∈ 𝓝[insert x s] x, { rw nhds_within_restrict _ xo o_open, refine filter.inter_mem self_mem_nhds_within _, suffices : u ∈ 𝓝[(ext_chart_at I x) '' (insert x s ∩ o)] (ext_chart_at I x x), from (ext_chart_at_continuous_at I x).continuous_within_at.preimage_mem_nhds_within' this, apply nhds_within_mono _ _ u_nhds, rw image_subset_iff, assume y hy, rcases hy.1 with rfl|h', { simp only [mem_insert_iff] with mfld_simps }, { simp only [mem_insert_iff, ho hy.2, h', h'o ⟨hy.2, h'⟩] with mfld_simps } }, show cont_mdiff_on I I' n f v, { assume y hy, have : continuous_within_at f v y, { apply (((ext_chart_at_continuous_on_symm I' (f x) _ _).comp' (hu _ hy.2).continuous_within_at).comp' (ext_chart_at_continuous_on I x _ _)).congr_mono, { assume z hz, simp only [v_incl hz, v_incl' z hz] with mfld_simps }, { assume z hz, simp only [v_incl hz, v_incl' z hz] with mfld_simps, exact hz.2 }, { simp only [v_incl hy, v_incl' y hy] with mfld_simps }, { simp only [v_incl hy, v_incl' y hy] with mfld_simps }, { simp only [v_incl hy] with mfld_simps } }, refine (cont_mdiff_within_at_iff_of_mem_source' (v_incl hy) (v_incl' y hy)).mpr ⟨this, _⟩, { apply hu.mono, { assume z hz, simp only [v] with mfld_simps at hz, have : I ((chart_at H x) (((chart_at H x).symm) (I.symm z))) ∈ u, by simp only [hz], simpa only [hz] with mfld_simps using this }, { have exty : I (chart_at H x y) ∈ u := hy.2, simp only [v_incl hy, v_incl' y hy, exty, hy.1.1, hy.1.2] with mfld_simps } } } }, { rintros ⟨u, u_nhds, hu⟩, have : cont_mdiff_within_at I I' ↑n f (insert x s ∩ u) x, { have : x ∈ insert x s := mem_insert x s, exact hu.mono (inter_subset_right _ _) _ ⟨this, mem_of_mem_nhds_within this u_nhds⟩ }, rw cont_mdiff_within_at_inter' u_nhds at this, exact this.mono (subset_insert x s) } end /-- A function is `C^n` at a point, for `n : ℕ`, if and only if it is `C^n` on a neighborhood of this point. -/ lemma cont_mdiff_at_iff_cont_mdiff_on_nhds {n : ℕ} : cont_mdiff_at I I' n f x ↔ ∃ u ∈ 𝓝 x, cont_mdiff_on I I' n f u := by simp [← cont_mdiff_within_at_univ, cont_mdiff_within_at_iff_cont_mdiff_on_nhds, nhds_within_univ] /-- Note: This does not hold for `n = ∞`. `f` being `C^∞` at `x` means that for every `n`, `f` is `C^n` on some neighborhood of `x`, but this neighborhood can depend on `n`. -/ lemma cont_mdiff_at_iff_cont_mdiff_at_nhds {n : ℕ} : cont_mdiff_at I I' n f x ↔ ∀ᶠ x' in 𝓝 x, cont_mdiff_at I I' n f x' := begin refine ⟨_, λ h, h.self_of_nhds⟩, rw [cont_mdiff_at_iff_cont_mdiff_on_nhds], rintro ⟨u, hu, h⟩, refine (eventually_mem_nhds.mpr hu).mono (λ x' hx', _), exact (h x' $ mem_of_mem_nhds hx').cont_mdiff_at hx' end omit Is I's /-! ### Congruence lemmas -/ lemma cont_mdiff_within_at.congr (h : cont_mdiff_within_at I I' n f s x) (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : f₁ x = f x) : cont_mdiff_within_at I I' n f₁ s x := (cont_diff_within_at_local_invariant_prop I I' n).lift_prop_within_at_congr h h₁ hx lemma cont_mdiff_within_at_congr (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : f₁ x = f x) : cont_mdiff_within_at I I' n f₁ s x ↔ cont_mdiff_within_at I I' n f s x := (cont_diff_within_at_local_invariant_prop I I' n).lift_prop_within_at_congr_iff h₁ hx lemma cont_mdiff_within_at.congr_of_eventually_eq (h : cont_mdiff_within_at I I' n f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : cont_mdiff_within_at I I' n f₁ s x := (cont_diff_within_at_local_invariant_prop I I' n).lift_prop_within_at_congr_of_eventually_eq h h₁ hx lemma filter.eventually_eq.cont_mdiff_within_at_iff (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : cont_mdiff_within_at I I' n f₁ s x ↔ cont_mdiff_within_at I I' n f s x := (cont_diff_within_at_local_invariant_prop I I' n) .lift_prop_within_at_congr_iff_of_eventually_eq h₁ hx lemma cont_mdiff_at.congr_of_eventually_eq (h : cont_mdiff_at I I' n f x) (h₁ : f₁ =ᶠ[𝓝 x] f) : cont_mdiff_at I I' n f₁ x := (cont_diff_within_at_local_invariant_prop I I' n).lift_prop_at_congr_of_eventually_eq h h₁ lemma filter.eventually_eq.cont_mdiff_at_iff (h₁ : f₁ =ᶠ[𝓝 x] f) : cont_mdiff_at I I' n f₁ x ↔ cont_mdiff_at I I' n f x := (cont_diff_within_at_local_invariant_prop I I' n).lift_prop_at_congr_iff_of_eventually_eq h₁ lemma cont_mdiff_on.congr (h : cont_mdiff_on I I' n f s) (h₁ : ∀ y ∈ s, f₁ y = f y) : cont_mdiff_on I I' n f₁ s := (cont_diff_within_at_local_invariant_prop I I' n).lift_prop_on_congr h h₁ lemma cont_mdiff_on_congr (h₁ : ∀ y ∈ s, f₁ y = f y) : cont_mdiff_on I I' n f₁ s ↔ cont_mdiff_on I I' n f s := (cont_diff_within_at_local_invariant_prop I I' n).lift_prop_on_congr_iff h₁ /-! ### Locality -/ /-- Being `C^n` is a local property. -/ lemma cont_mdiff_on_of_locally_cont_mdiff_on (h : ∀x∈s, ∃u, is_open u ∧ x ∈ u ∧ cont_mdiff_on I I' n f (s ∩ u)) : cont_mdiff_on I I' n f s := (cont_diff_within_at_local_invariant_prop I I' n).lift_prop_on_of_locally_lift_prop_on h lemma cont_mdiff_of_locally_cont_mdiff_on (h : ∀x, ∃u, is_open u ∧ x ∈ u ∧ cont_mdiff_on I I' n f u) : cont_mdiff I I' n f := (cont_diff_within_at_local_invariant_prop I I' n).lift_prop_of_locally_lift_prop_on h /-! ### Smoothness of the composition of smooth functions between manifolds -/ section composition variables {E'' : Type*} [normed_add_comm_group E''] [normed_space 𝕜 E''] {H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''} {M'' : Type*} [topological_space M''] [charted_space H'' M''] /-- The composition of `C^n` functions within domains at points is `C^n`. -/ lemma cont_mdiff_within_at.comp {t : set M'} {g : M' → M''} (x : M) (hg : cont_mdiff_within_at I' I'' n g t (f x)) (hf : cont_mdiff_within_at I I' n f s x) (st : maps_to f s t) : cont_mdiff_within_at I I'' n (g ∘ f) s x := begin rw cont_mdiff_within_at_iff at hg hf ⊢, refine ⟨hg.1.comp hf.1 st, _⟩, set e := ext_chart_at I x, set e' := ext_chart_at I' (f x), set e'' := ext_chart_at I'' (g (f x)), have : e' (f x) = (written_in_ext_chart_at I I' x f) (e x), by simp only [e, e'] with mfld_simps, rw this at hg, have A : ∀ᶠ y in 𝓝[e.symm ⁻¹' s ∩ range I] e x, y ∈ e.target ∧ f (e.symm y) ∈ t ∧ f (e.symm y) ∈ e'.source ∧ g (f (e.symm y)) ∈ e''.source, { simp only [← ext_chart_at_map_nhds_within, eventually_map], filter_upwards [hf.1.tendsto (ext_chart_at_source_mem_nhds I' (f x)), (hg.1.comp hf.1 st).tendsto (ext_chart_at_source_mem_nhds I'' (g (f x))), (inter_mem_nhds_within s (ext_chart_at_source_mem_nhds I x))], rintros x' (hfx' : f x' ∈ _) (hgfx' : g (f x') ∈ _) ⟨hx's, hx'⟩, simp only [e.map_source hx', true_and, e.left_inv hx', st hx's, *] }, refine ((hg.2.comp _ (hf.2.mono (inter_subset_right _ _)) (inter_subset_left _ _)).mono_of_mem (inter_mem _ self_mem_nhds_within)).congr_of_eventually_eq _ _, { filter_upwards [A], rintro x' ⟨hx', ht, hfx', hgfx'⟩, simp only [*, mem_preimage, written_in_ext_chart_at, (∘), mem_inter_eq, e'.left_inv, true_and], exact mem_range_self _ }, { filter_upwards [A], rintro x' ⟨hx', ht, hfx', hgfx'⟩, simp only [*, (∘), written_in_ext_chart_at, e'.left_inv] }, { simp only [written_in_ext_chart_at, (∘), mem_ext_chart_source, e.left_inv, e'.left_inv] } end /-- The composition of `C^n` functions on domains is `C^n`. -/ lemma cont_mdiff_on.comp {t : set M'} {g : M' → M''} (hg : cont_mdiff_on I' I'' n g t) (hf : cont_mdiff_on I I' n f s) (st : s ⊆ f ⁻¹' t) : cont_mdiff_on I I'' n (g ∘ f) s := λ x hx, (hg _ (st hx)).comp x (hf x hx) st /-- The composition of `C^n` functions on domains is `C^n`. -/ lemma cont_mdiff_on.comp' {t : set M'} {g : M' → M''} (hg : cont_mdiff_on I' I'' n g t) (hf : cont_mdiff_on I I' n f s) : cont_mdiff_on I I'' n (g ∘ f) (s ∩ f ⁻¹' t) := hg.comp (hf.mono (inter_subset_left _ _)) (inter_subset_right _ _) /-- The composition of `C^n` functions is `C^n`. -/ lemma cont_mdiff.comp {g : M' → M''} (hg : cont_mdiff I' I'' n g) (hf : cont_mdiff I I' n f) : cont_mdiff I I'' n (g ∘ f) := begin rw ← cont_mdiff_on_univ at hf hg ⊢, exact hg.comp hf subset_preimage_univ, end /-- The composition of `C^n` functions within domains at points is `C^n`. -/ lemma cont_mdiff_within_at.comp' {t : set M'} {g : M' → M''} (x : M) (hg : cont_mdiff_within_at I' I'' n g t (f x)) (hf : cont_mdiff_within_at I I' n f s x) : cont_mdiff_within_at I I'' n (g ∘ f) (s ∩ f⁻¹' t) x := hg.comp x (hf.mono (inter_subset_left _ _)) (inter_subset_right _ _) /-- `g ∘ f` is `C^n` within `s` at `x` if `g` is `C^n` at `f x` and `f` is `C^n` within `s` at `x`. -/ lemma cont_mdiff_at.comp_cont_mdiff_within_at {g : M' → M''} (x : M) (hg : cont_mdiff_at I' I'' n g (f x)) (hf : cont_mdiff_within_at I I' n f s x) : cont_mdiff_within_at I I'' 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_mdiff_at.comp {g : M' → M''} (x : M) (hg : cont_mdiff_at I' I'' n g (f x)) (hf : cont_mdiff_at I I' n f x) : cont_mdiff_at I I'' n (g ∘ f) x := hg.comp x hf (maps_to_univ _ _) lemma cont_mdiff.comp_cont_mdiff_on {f : M → M'} {g : M' → M''} {s : set M} (hg : cont_mdiff I' I'' n g) (hf : cont_mdiff_on I I' n f s) : cont_mdiff_on I I'' n (g ∘ f) s := hg.cont_mdiff_on.comp hf set.subset_preimage_univ lemma smooth.comp_smooth_on {f : M → M'} {g : M' → M''} {s : set M} (hg : smooth I' I'' g) (hf : smooth_on I I' f s) : smooth_on I I'' (g ∘ f) s := hg.smooth_on.comp hf set.subset_preimage_univ lemma cont_mdiff_on.comp_cont_mdiff {t : set M'} {g : M' → M''} (hg : cont_mdiff_on I' I'' n g t) (hf : cont_mdiff I I' n f) (ht : ∀ x, f x ∈ t) : cont_mdiff I I'' n (g ∘ f) := cont_mdiff_on_univ.mp $ hg.comp hf.cont_mdiff_on (λ x _, ht x) lemma smooth_on.comp_smooth {t : set M'} {g : M' → M''} (hg : smooth_on I' I'' g t) (hf : smooth I I' f) (ht : ∀ x, f x ∈ t) : smooth I I'' (g ∘ f) := hg.comp_cont_mdiff hf ht end composition /-! ### Atlas members are smooth -/ section atlas variables {e : local_homeomorph M H} include Is /-- An atlas member is `C^n` for any `n`. -/ lemma cont_mdiff_on_of_mem_maximal_atlas (h : e ∈ maximal_atlas I M) : cont_mdiff_on I I n e e.source := cont_mdiff_on.of_le ((cont_diff_within_at_local_invariant_prop I I ∞).lift_prop_on_of_mem_maximal_atlas (cont_diff_within_at_prop_id I) h) le_top /-- The inverse of an atlas member is `C^n` for any `n`. -/ lemma cont_mdiff_on_symm_of_mem_maximal_atlas (h : e ∈ maximal_atlas I M) : cont_mdiff_on I I n e.symm e.target := cont_mdiff_on.of_le ((cont_diff_within_at_local_invariant_prop I I ∞).lift_prop_on_symm_of_mem_maximal_atlas (cont_diff_within_at_prop_id I) h) le_top lemma cont_mdiff_on_chart : cont_mdiff_on I I n (chart_at H x) (chart_at H x).source := cont_mdiff_on_of_mem_maximal_atlas ((cont_diff_groupoid ⊤ I).chart_mem_maximal_atlas x) lemma cont_mdiff_on_chart_symm : cont_mdiff_on I I n (chart_at H x).symm (chart_at H x).target := cont_mdiff_on_symm_of_mem_maximal_atlas ((cont_diff_groupoid ⊤ I).chart_mem_maximal_atlas x) end atlas /-! ### The identity is smooth -/ section id lemma cont_mdiff_id : cont_mdiff I I n (id : M → M) := cont_mdiff.of_le ((cont_diff_within_at_local_invariant_prop I I ∞).lift_prop_id (cont_diff_within_at_prop_id I)) le_top lemma smooth_id : smooth I I (id : M → M) := cont_mdiff_id lemma cont_mdiff_on_id : cont_mdiff_on I I n (id : M → M) s := cont_mdiff_id.cont_mdiff_on lemma smooth_on_id : smooth_on I I (id : M → M) s := cont_mdiff_on_id lemma cont_mdiff_at_id : cont_mdiff_at I I n (id : M → M) x := cont_mdiff_id.cont_mdiff_at lemma smooth_at_id : smooth_at I I (id : M → M) x := cont_mdiff_at_id lemma cont_mdiff_within_at_id : cont_mdiff_within_at I I n (id : M → M) s x := cont_mdiff_at_id.cont_mdiff_within_at lemma smooth_within_at_id : smooth_within_at I I (id : M → M) s x := cont_mdiff_within_at_id end id /-! ### Constants are smooth -/ section id variable {c : M'} lemma cont_mdiff_const : cont_mdiff I I' n (λ (x : M), c) := begin assume x, refine ⟨continuous_within_at_const, _⟩, simp only [cont_diff_within_at_prop, (∘)], exact cont_diff_within_at_const, end @[to_additive] lemma cont_mdiff_one [has_one M'] : cont_mdiff I I' n (1 : M → M') := by simp only [pi.one_def, cont_mdiff_const] lemma smooth_const : smooth I I' (λ (x : M), c) := cont_mdiff_const @[to_additive] lemma smooth_one [has_one M'] : smooth I I' (1 : M → M') := by simp only [pi.one_def, smooth_const] lemma cont_mdiff_on_const : cont_mdiff_on I I' n (λ (x : M), c) s := cont_mdiff_const.cont_mdiff_on @[to_additive] lemma cont_mdiff_on_one [has_one M'] : cont_mdiff_on I I' n (1 : M → M') s := cont_mdiff_one.cont_mdiff_on lemma smooth_on_const : smooth_on I I' (λ (x : M), c) s := cont_mdiff_on_const @[to_additive] lemma smooth_on_one [has_one M'] : smooth_on I I' (1 : M → M') s := cont_mdiff_on_one lemma cont_mdiff_at_const : cont_mdiff_at I I' n (λ (x : M), c) x := cont_mdiff_const.cont_mdiff_at @[to_additive] lemma cont_mdiff_at_one [has_one M'] : cont_mdiff_at I I' n (1 : M → M') x := cont_mdiff_one.cont_mdiff_at lemma smooth_at_const : smooth_at I I' (λ (x : M), c) x := cont_mdiff_at_const @[to_additive] lemma smooth_at_one [has_one M'] : smooth_at I I' (1 : M → M') x := cont_mdiff_at_one lemma cont_mdiff_within_at_const : cont_mdiff_within_at I I' n (λ (x : M), c) s x := cont_mdiff_at_const.cont_mdiff_within_at @[to_additive] lemma cont_mdiff_within_at_one [has_one M'] : cont_mdiff_within_at I I' n (1 : M → M') s x := cont_mdiff_at_const.cont_mdiff_within_at lemma smooth_within_at_const : smooth_within_at I I' (λ (x : M), c) s x := cont_mdiff_within_at_const @[to_additive] lemma smooth_within_at_one [has_one M'] : smooth_within_at I I' (1 : M → M') s x := cont_mdiff_within_at_one end id lemma cont_mdiff_of_support {f : M → F} (hf : ∀ x ∈ tsupport f, cont_mdiff_at I 𝓘(𝕜, F) n f x) : cont_mdiff I 𝓘(𝕜, F) n f := begin intro x, by_cases hx : x ∈ tsupport f, { exact hf x hx }, { refine cont_mdiff_at.congr_of_eventually_eq _ (eventually_eq_zero_nhds.2 hx), exact cont_mdiff_at_const } end /-! ### Equivalence with the basic definition for functions between vector spaces -/ section module lemma cont_mdiff_within_at_iff_cont_diff_within_at {f : E → E'} {s : set E} {x : E} : cont_mdiff_within_at 𝓘(𝕜, E) 𝓘(𝕜, E') n f s x ↔ cont_diff_within_at 𝕜 n f s x := begin simp only [cont_mdiff_within_at, lift_prop_within_at, cont_diff_within_at_prop, iff_def] with mfld_simps {contextual := tt}, exact cont_diff_within_at.continuous_within_at end alias cont_mdiff_within_at_iff_cont_diff_within_at ↔ cont_mdiff_within_at.cont_diff_within_at cont_diff_within_at.cont_mdiff_within_at lemma cont_mdiff_at_iff_cont_diff_at {f : E → E'} {x : E} : cont_mdiff_at 𝓘(𝕜, E) 𝓘(𝕜, E') n f x ↔ cont_diff_at 𝕜 n f x := by rw [← cont_mdiff_within_at_univ, cont_mdiff_within_at_iff_cont_diff_within_at, cont_diff_within_at_univ] alias cont_mdiff_at_iff_cont_diff_at ↔ cont_mdiff_at.cont_diff_at cont_diff_at.cont_mdiff_at lemma cont_mdiff_on_iff_cont_diff_on {f : E → E'} {s : set E} : cont_mdiff_on 𝓘(𝕜, E) 𝓘(𝕜, E') n f s ↔ cont_diff_on 𝕜 n f s := forall_congr $ by simp [cont_mdiff_within_at_iff_cont_diff_within_at] alias cont_mdiff_on_iff_cont_diff_on ↔ cont_mdiff_on.cont_diff_on cont_diff_on.cont_mdiff_on lemma cont_mdiff_iff_cont_diff {f : E → E'} : cont_mdiff 𝓘(𝕜, E) 𝓘(𝕜, E') n f ↔ cont_diff 𝕜 n f := by rw [← cont_diff_on_univ, ← cont_mdiff_on_univ, cont_mdiff_on_iff_cont_diff_on] alias cont_mdiff_iff_cont_diff ↔ cont_mdiff.cont_diff cont_diff.cont_mdiff end module /-! ### The tangent map of a smooth function is smooth -/ section tangent_map /-- If a function is `C^n` with `1 ≤ n` on a domain with unique derivatives, then its bundled derivative is continuous. In this auxiliary lemma, we prove this fact when the source and target space are model spaces in models with corners. The general fact is proved in `cont_mdiff_on.continuous_on_tangent_map_within`-/ lemma cont_mdiff_on.continuous_on_tangent_map_within_aux {f : H → H'} {s : set H} (hf : cont_mdiff_on I I' n f s) (hn : 1 ≤ n) (hs : unique_mdiff_on I s) : continuous_on (tangent_map_within I I' f s) ((tangent_bundle.proj I H) ⁻¹' s) := begin suffices h : continuous_on (λ (p : H × E), (f p.fst, (fderiv_within 𝕜 (written_in_ext_chart_at I I' p.fst f) (I.symm ⁻¹' s ∩ range I) ((ext_chart_at I p.fst) p.fst) : E →L[𝕜] E') p.snd)) (prod.fst ⁻¹' s), { have A := (tangent_bundle_model_space_homeomorph H I).continuous, rw continuous_iff_continuous_on_univ at A, have B := ((tangent_bundle_model_space_homeomorph H' I').symm.continuous.comp_continuous_on h) .comp' A, have : (univ ∩ ⇑(tangent_bundle_model_space_homeomorph H I) ⁻¹' (prod.fst ⁻¹' s)) = tangent_bundle.proj I H ⁻¹' s, by { ext ⟨x, v⟩, simp only with mfld_simps }, rw this at B, apply B.congr, rintros ⟨x, v⟩ hx, dsimp [tangent_map_within], ext, { refl }, simp only with mfld_simps, apply congr_fun, apply congr_arg, rw mdifferentiable_within_at.mfderiv_within (hf.mdifferentiable_on hn x hx), refl }, suffices h : continuous_on (λ (p : H × E), (fderiv_within 𝕜 (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) (I p.fst) : E →L[𝕜] E') p.snd) (prod.fst ⁻¹' s), { dsimp [written_in_ext_chart_at, ext_chart_at], apply continuous_on.prod (continuous_on.comp hf.continuous_on continuous_fst.continuous_on (subset.refl _)), apply h.congr, assume p hp, refl }, suffices h : continuous_on (fderiv_within 𝕜 (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I)) (I '' s), { have C := continuous_on.comp h I.continuous_to_fun.continuous_on (subset.refl _), have A : continuous (λq : (E →L[𝕜] E') × E, q.1 q.2) := is_bounded_bilinear_map_apply.continuous, have B : continuous_on (λp : H × E, (fderiv_within 𝕜 (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) (I p.1), p.2)) (prod.fst ⁻¹' s), { apply continuous_on.prod _ continuous_snd.continuous_on, refine (continuous_on.comp C continuous_fst.continuous_on _ : _), exact preimage_mono (subset_preimage_image _ _) }, exact A.comp_continuous_on B }, rw cont_mdiff_on_iff at hf, let x : H := I.symm (0 : E), let y : H' := I'.symm (0 : E'), have A := hf.2 x y, simp only [I.image_eq, inter_comm] with mfld_simps at A ⊢, apply A.continuous_on_fderiv_within _ hn, convert hs.unique_diff_on_target_inter x using 1, simp only [inter_comm] with mfld_simps end /-- If a function is `C^n` on a domain with unique derivatives, then its bundled derivative is `C^m` when `m+1 ≤ n`. In this auxiliary lemma, we prove this fact when the source and target space are model spaces in models with corners. The general fact is proved in `cont_mdiff_on.cont_mdiff_on_tangent_map_within` -/ lemma cont_mdiff_on.cont_mdiff_on_tangent_map_within_aux {f : H → H'} {s : set H} (hf : cont_mdiff_on I I' n f s) (hmn : m + 1 ≤ n) (hs : unique_mdiff_on I s) : cont_mdiff_on I.tangent I'.tangent m (tangent_map_within I I' f s) ((tangent_bundle.proj I H) ⁻¹' s) := begin have m_le_n : m ≤ n, { apply le_trans _ hmn, have : m + 0 ≤ m + 1 := add_le_add_left (zero_le _) _, simpa only [add_zero] using this }, have one_le_n : 1 ≤ n, { apply le_trans _ hmn, change 0 + 1 ≤ m + 1, exact add_le_add_right (zero_le _) _ }, have U': unique_diff_on 𝕜 (range I ∩ I.symm ⁻¹' s), { assume y hy, simpa only [unique_mdiff_on, unique_mdiff_within_at, hy.1, inter_comm] with mfld_simps using hs (I.symm y) hy.2 }, rw cont_mdiff_on_iff, refine ⟨hf.continuous_on_tangent_map_within_aux one_le_n hs, λp q, _⟩, have A : range I ×ˢ univ ∩ ((equiv.sigma_equiv_prod H E).symm ∘ λ (p : E × E), ((I.symm) p.fst, p.snd)) ⁻¹' (tangent_bundle.proj I H ⁻¹' s) = (range I ∩ I.symm ⁻¹' s) ×ˢ univ, by { ext ⟨x, v⟩, simp only with mfld_simps }, suffices h : cont_diff_on 𝕜 m (((λ (p : H' × E'), (I' p.fst, p.snd)) ∘ (equiv.sigma_equiv_prod H' E')) ∘ tangent_map_within I I' f s ∘ ((equiv.sigma_equiv_prod H E).symm) ∘ λ (p : E × E), (I.symm p.fst, p.snd)) ((range ⇑I ∩ ⇑(I.symm) ⁻¹' s) ×ˢ univ), by simpa [A] using h, change cont_diff_on 𝕜 m (λ (p : E × E), ((I' (f (I.symm p.fst)), ((mfderiv_within I I' f s (I.symm p.fst)) : E → E') p.snd) : E' × E')) ((range I ∩ I.symm ⁻¹' s) ×ˢ univ), -- check that all bits in this formula are `C^n` have hf' := cont_mdiff_on_iff.1 hf, have A : cont_diff_on 𝕜 m (I' ∘ f ∘ I.symm) (range I ∩ I.symm ⁻¹' s) := by simpa only with mfld_simps using (hf'.2 (I.symm 0) (I'.symm 0)).of_le m_le_n, have B : cont_diff_on 𝕜 m ((I' ∘ f ∘ I.symm) ∘ prod.fst) ((range I ∩ I.symm ⁻¹' s) ×ˢ univ) := A.comp (cont_diff_fst.cont_diff_on) (prod_subset_preimage_fst _ _), suffices C : cont_diff_on 𝕜 m (λ (p : E × E), ((fderiv_within 𝕜 (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) p.1 : _) p.2)) ((range I ∩ I.symm ⁻¹' s) ×ˢ univ), { apply cont_diff_on.prod B _, apply C.congr (λp hp, _), simp only with mfld_simps at hp, simp only [mfderiv_within, hf.mdifferentiable_on one_le_n _ hp.2, hp.1, dif_pos] with mfld_simps }, have D : cont_diff_on 𝕜 m (λ x, (fderiv_within 𝕜 (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) x)) (range I ∩ I.symm ⁻¹' s), { have : cont_diff_on 𝕜 n (I' ∘ f ∘ I.symm) (range I ∩ I.symm ⁻¹' s) := by simpa only with mfld_simps using (hf'.2 (I.symm 0) (I'.symm 0)), simpa only [inter_comm] using this.fderiv_within U' hmn }, have := D.comp (cont_diff_fst.cont_diff_on) (prod_subset_preimage_fst _ _), have := cont_diff_on.prod this (cont_diff_snd.cont_diff_on), exact is_bounded_bilinear_map_apply.cont_diff.comp_cont_diff_on this, end include Is I's /-- If a function is `C^n` on a domain with unique derivatives, then its bundled derivative is `C^m` when `m+1 ≤ n`. -/ theorem cont_mdiff_on.cont_mdiff_on_tangent_map_within (hf : cont_mdiff_on I I' n f s) (hmn : m + 1 ≤ n) (hs : unique_mdiff_on I s) : cont_mdiff_on I.tangent I'.tangent m (tangent_map_within I I' f s) ((tangent_bundle.proj I M) ⁻¹' s) := begin /- The strategy of the proof is to avoid unfolding the definitions, and reduce by functoriality to the case of functions on the model spaces, where we have already proved the result. Let `l` and `r` be the charts to the left and to the right, so that we have ``` l^{-1} f r H --------> M ---> M' ---> H' ``` Then the tangent map `T(r ∘ f ∘ l)` is smooth by a previous result. Consider the composition ``` Tl T(r ∘ f ∘ l^{-1}) Tr^{-1} TM -----> TH -------------------> TH' ---------> TM' ``` where `Tr^{-1}` and `Tl` are the tangent maps of `r^{-1}` and `l`. Writing `Tl` and `Tr^{-1}` as composition of charts (called `Dl` and `il` for `l` and `Dr` and `ir` in the proof below), it follows that they are smooth. The composition of all these maps is `Tf`, and is therefore smooth as a composition of smooth maps. -/ have m_le_n : m ≤ n, { apply le_trans _ hmn, have : m + 0 ≤ m + 1 := add_le_add_left (zero_le _) _, simpa only [add_zero] }, have one_le_n : 1 ≤ n, { apply le_trans _ hmn, change 0 + 1 ≤ m + 1, exact add_le_add_right (zero_le _) _ }, /- First step: local reduction on the space, to a set `s'` which is contained in chart domains. -/ refine cont_mdiff_on_of_locally_cont_mdiff_on (λp hp, _), have hf' := cont_mdiff_on_iff.1 hf, simp [tangent_bundle.proj] at hp, let l := chart_at H p.1, set Dl := chart_at (model_prod H E) p with hDl, let r := chart_at H' (f p.1), let Dr := chart_at (model_prod H' E') (tangent_map_within I I' f s p), let il := chart_at (model_prod H E) (tangent_map I I l p), let ir := chart_at (model_prod H' E') (tangent_map I I' (r ∘ f) p), let s' := f ⁻¹' r.source ∩ s ∩ l.source, let s'_lift := (tangent_bundle.proj I M)⁻¹' s', let s'l := l.target ∩ l.symm ⁻¹' s', let s'l_lift := (tangent_bundle.proj I H) ⁻¹' s'l, rcases continuous_on_iff'.1 hf'.1 r.source r.open_source with ⟨o, o_open, ho⟩, suffices h : cont_mdiff_on I.tangent I'.tangent m (tangent_map_within I I' f s) s'_lift, { refine ⟨(tangent_bundle.proj I M)⁻¹' (o ∩ l.source), _, _, _⟩, show is_open ((tangent_bundle.proj I M)⁻¹' (o ∩ l.source)), from (is_open.inter o_open l.open_source).preimage (tangent_bundle_proj_continuous _ _) , show p ∈ tangent_bundle.proj I M ⁻¹' (o ∩ l.source), { simp [tangent_bundle.proj] at ⊢, have : p.1 ∈ f ⁻¹' r.source ∩ s, by simp [hp], rw ho at this, exact this.1 }, { have : tangent_bundle.proj I M ⁻¹' s ∩ tangent_bundle.proj I M ⁻¹' (o ∩ l.source) = s'_lift, { dsimp only [s'_lift, s'], rw [ho], mfld_set_tac }, rw this, exact h } }, /- Second step: check that all functions are smooth, and use the chain rule to write the bundled derivative as a composition of a function between model spaces and of charts. Convention: statements about the differentiability of `a ∘ b ∘ c` are named `diff_abc`. Statements about differentiability in the bundle have a `_lift` suffix. -/ have U' : unique_mdiff_on I s', { apply unique_mdiff_on.inter _ l.open_source, rw [ho, inter_comm], exact hs.inter o_open }, have U'l : unique_mdiff_on I s'l := U'.unique_mdiff_on_preimage (mdifferentiable_chart _ _), have diff_f : cont_mdiff_on I I' n f s' := hf.mono (by mfld_set_tac), have diff_r : cont_mdiff_on I' I' n r r.source := cont_mdiff_on_chart, have diff_rf : cont_mdiff_on I I' n (r ∘ f) s', { apply cont_mdiff_on.comp diff_r diff_f (λx hx, _), simp only [s'] with mfld_simps at hx, simp only [hx] with mfld_simps }, have diff_l : cont_mdiff_on I I n l.symm s'l, { have A : cont_mdiff_on I I n l.symm l.target := cont_mdiff_on_chart_symm, exact A.mono (by mfld_set_tac) }, have diff_rfl : cont_mdiff_on I I' n (r ∘ f ∘ l.symm) s'l, { apply cont_mdiff_on.comp diff_rf diff_l, mfld_set_tac }, have diff_rfl_lift : cont_mdiff_on I.tangent I'.tangent m (tangent_map_within I I' (r ∘ f ∘ l.symm) s'l) s'l_lift := diff_rfl.cont_mdiff_on_tangent_map_within_aux hmn U'l, have diff_irrfl_lift : cont_mdiff_on I.tangent I'.tangent m (ir ∘ (tangent_map_within I I' (r ∘ f ∘ l.symm) s'l)) s'l_lift, { have A : cont_mdiff_on I'.tangent I'.tangent m ir ir.source := cont_mdiff_on_chart, exact cont_mdiff_on.comp A diff_rfl_lift (λp hp, by simp only [ir] with mfld_simps) }, have diff_Drirrfl_lift : cont_mdiff_on I.tangent I'.tangent m (Dr.symm ∘ (ir ∘ (tangent_map_within I I' (r ∘ f ∘ l.symm) s'l))) s'l_lift, { have A : cont_mdiff_on I'.tangent I'.tangent m Dr.symm Dr.target := cont_mdiff_on_chart_symm, apply cont_mdiff_on.comp A diff_irrfl_lift (λp hp, _), simp only [s'l_lift, tangent_bundle.proj] with mfld_simps at hp, simp only [ir, @local_equiv.refl_coe (model_prod H' E'), hp] with mfld_simps }, -- conclusion of this step: the composition of all the maps above is smooth have diff_DrirrflilDl : cont_mdiff_on I.tangent I'.tangent m (Dr.symm ∘ (ir ∘ (tangent_map_within I I' (r ∘ f ∘ l.symm) s'l)) ∘ (il.symm ∘ Dl)) s'_lift, { have A : cont_mdiff_on I.tangent I.tangent m Dl Dl.source := cont_mdiff_on_chart, have A' : cont_mdiff_on I.tangent I.tangent m Dl s'_lift, { apply A.mono (λp hp, _), simp only [s'_lift, tangent_bundle.proj] with mfld_simps at hp, simp only [Dl, hp] with mfld_simps }, have B : cont_mdiff_on I.tangent I.tangent m il.symm il.target := cont_mdiff_on_chart_symm, have C : cont_mdiff_on I.tangent I.tangent m (il.symm ∘ Dl) s'_lift := cont_mdiff_on.comp B A' (λp hp, by simp only [il] with mfld_simps), apply cont_mdiff_on.comp diff_Drirrfl_lift C (λp hp, _), simp only [s'_lift, tangent_bundle.proj] with mfld_simps at hp, simp only [il, s'l_lift, hp, tangent_bundle.proj] with mfld_simps }, /- Third step: check that the composition of all the maps indeed coincides with the derivative we are looking for -/ have eq_comp : ∀q ∈ s'_lift, tangent_map_within I I' f s q = (Dr.symm ∘ ir ∘ (tangent_map_within I I' (r ∘ f ∘ l.symm) s'l) ∘ (il.symm ∘ Dl)) q, { assume q hq, simp only [s'_lift, tangent_bundle.proj] with mfld_simps at hq, have U'q : unique_mdiff_within_at I s' q.1, by { apply U', simp only [hq, s'] with mfld_simps }, have U'lq : unique_mdiff_within_at I s'l (Dl q).1, by { apply U'l, simp only [hq, s'l] with mfld_simps }, have A : tangent_map_within I I' ((r ∘ f) ∘ l.symm) s'l (il.symm (Dl q)) = tangent_map_within I I' (r ∘ f) s' (tangent_map_within I I l.symm s'l (il.symm (Dl q))), { refine tangent_map_within_comp_at (il.symm (Dl q)) _ _ (λp hp, _) U'lq, { apply diff_rf.mdifferentiable_on one_le_n, simp only [hq] with mfld_simps }, { apply diff_l.mdifferentiable_on one_le_n, simp only [s'l, hq] with mfld_simps }, { simp only with mfld_simps at hp, simp only [hp] with mfld_simps } }, have B : tangent_map_within I I l.symm s'l (il.symm (Dl q)) = q, { have : tangent_map_within I I l.symm s'l (il.symm (Dl q)) = tangent_map I I l.symm (il.symm (Dl q)), { refine tangent_map_within_eq_tangent_map U'lq _, refine mdifferentiable_at_atlas_symm _ (chart_mem_atlas _ _) _, simp only [hq] with mfld_simps }, rw [this, tangent_map_chart_symm, hDl], { simp only [hq] with mfld_simps, have : q ∈ (chart_at (model_prod H E) p).source, by simp only [hq] with mfld_simps, exact (chart_at (model_prod H E) p).left_inv this }, { simp only [hq] with mfld_simps } }, have C : tangent_map_within I I' (r ∘ f) s' q = tangent_map_within I' I' r r.source (tangent_map_within I I' f s' q), { refine tangent_map_within_comp_at q _ _ (λr hr, _) U'q, { apply diff_r.mdifferentiable_on one_le_n, simp only [hq] with mfld_simps }, { apply diff_f.mdifferentiable_on one_le_n, simp only [hq] with mfld_simps }, { simp only [s'] with mfld_simps at hr, simp only [hr] with mfld_simps } }, have D : Dr.symm (ir (tangent_map_within I' I' r r.source (tangent_map_within I I' f s' q))) = tangent_map_within I I' f s' q, { have A : tangent_map_within I' I' r r.source (tangent_map_within I I' f s' q) = tangent_map I' I' r (tangent_map_within I I' f s' q), { apply tangent_map_within_eq_tangent_map, { apply is_open.unique_mdiff_within_at _ r.open_source, simp [hq] }, { refine mdifferentiable_at_atlas _ (chart_mem_atlas _ _) _, simp only [hq] with mfld_simps } }, have : f p.1 = (tangent_map_within I I' f s p).1 := rfl, rw [A], dsimp [r, Dr], rw [this, tangent_map_chart], { simp only [hq] with mfld_simps, have : tangent_map_within I I' f s' q ∈ (chart_at (model_prod H' E') (tangent_map_within I I' f s p)).source, by simp only [hq] with mfld_simps, exact (chart_at (model_prod H' E') (tangent_map_within I I' f s p)).left_inv this }, { simp only [hq] with mfld_simps } }, have E : tangent_map_within I I' f s' q = tangent_map_within I I' f s q, { refine tangent_map_within_subset (by mfld_set_tac) U'q _, apply hf.mdifferentiable_on one_le_n, simp only [hq] with mfld_simps }, simp only [(∘), A, B, C, D, E.symm] }, exact diff_DrirrflilDl.congr eq_comp, end /-- If a function is `C^n` on a domain with unique derivatives, with `1 ≤ n`, then its bundled derivative is continuous there. -/ theorem cont_mdiff_on.continuous_on_tangent_map_within (hf : cont_mdiff_on I I' n f s) (hmn : 1 ≤ n) (hs : unique_mdiff_on I s) : continuous_on (tangent_map_within I I' f s) ((tangent_bundle.proj I M) ⁻¹' s) := begin have : cont_mdiff_on I.tangent I'.tangent 0 (tangent_map_within I I' f s) ((tangent_bundle.proj I M) ⁻¹' s) := hf.cont_mdiff_on_tangent_map_within hmn hs, exact this.continuous_on end /-- If a function is `C^n`, then its bundled derivative is `C^m` when `m+1 ≤ n`. -/ theorem cont_mdiff.cont_mdiff_tangent_map (hf : cont_mdiff I I' n f) (hmn : m + 1 ≤ n) : cont_mdiff I.tangent I'.tangent m (tangent_map I I' f) := begin rw ← cont_mdiff_on_univ at hf ⊢, convert hf.cont_mdiff_on_tangent_map_within hmn unique_mdiff_on_univ, rw tangent_map_within_univ end /-- If a function is `C^n`, with `1 ≤ n`, then its bundled derivative is continuous. -/ theorem cont_mdiff.continuous_tangent_map (hf : cont_mdiff I I' n f) (hmn : 1 ≤ n) : continuous (tangent_map I I' f) := begin rw ← cont_mdiff_on_univ at hf, rw continuous_iff_continuous_on_univ, convert hf.continuous_on_tangent_map_within hmn unique_mdiff_on_univ, rw tangent_map_within_univ end end tangent_map /-! ### Smoothness of the projection in a basic smooth bundle -/ namespace basic_smooth_vector_bundle_core variables (Z : basic_smooth_vector_bundle_core I M E') /-- A version of `cont_mdiff_at_iff_target` when the codomain is the total space of a `basic_smooth_vector_bundle_core`. The continuity condition in the RHS is weaker. -/ lemma cont_mdiff_at_iff_target {f : N → Z.to_topological_vector_bundle_core.total_space} {x : N} {n : with_top ℕ} : cont_mdiff_at J (I.prod 𝓘(𝕜, E')) n f x ↔ continuous_at (bundle.total_space.proj ∘ f) x ∧ cont_mdiff_at J 𝓘(𝕜, E × E') n (ext_chart_at (I.prod 𝓘(𝕜, E')) (f x) ∘ f) x := begin let Z' := Z.to_topological_vector_bundle_core, rw [cont_mdiff_at_iff_target, and.congr_left_iff], refine λ hf, ⟨λ h, Z'.continuous_proj.continuous_at.comp h, λ h, _⟩, exact (Z'.local_triv ⟨chart_at _ (f x).1, chart_mem_atlas _ _⟩).to_fiber_bundle_trivialization .continuous_at_of_comp_left h (mem_chart_source _ _) (h.prod hf.continuous_at.snd) end lemma cont_mdiff_proj : cont_mdiff (I.prod 𝓘(𝕜, E')) I n Z.to_topological_vector_bundle_core.proj := begin assume x, rw [cont_mdiff_at, cont_mdiff_within_at_iff'], refine ⟨Z.to_topological_vector_bundle_core.continuous_proj.continuous_within_at, _⟩, simp only [(∘), chart_at, chart] with mfld_simps, apply cont_diff_within_at_fst.congr, { rintros ⟨a, b⟩ hab, simp only with mfld_simps at hab, simp only [hab] with mfld_simps }, { simp only with mfld_simps } end lemma smooth_proj : smooth (I.prod 𝓘(𝕜, E')) I Z.to_topological_vector_bundle_core.proj := cont_mdiff_proj Z lemma cont_mdiff_on_proj {s : set (Z.to_topological_vector_bundle_core.total_space)} : cont_mdiff_on (I.prod 𝓘(𝕜, E')) I n Z.to_topological_vector_bundle_core.proj s := Z.cont_mdiff_proj.cont_mdiff_on lemma smooth_on_proj {s : set (Z.to_topological_vector_bundle_core.total_space)} : smooth_on (I.prod 𝓘(𝕜, E')) I Z.to_topological_vector_bundle_core.proj s := cont_mdiff_on_proj Z lemma cont_mdiff_at_proj {p : Z.to_topological_vector_bundle_core.total_space} : cont_mdiff_at (I.prod 𝓘(𝕜, E')) I n Z.to_topological_vector_bundle_core.proj p := Z.cont_mdiff_proj.cont_mdiff_at lemma smooth_at_proj {p : Z.to_topological_vector_bundle_core.total_space} : smooth_at (I.prod 𝓘(𝕜, E')) I Z.to_topological_vector_bundle_core.proj p := Z.cont_mdiff_at_proj lemma cont_mdiff_within_at_proj {s : set (Z.to_topological_vector_bundle_core.total_space)} {p : Z.to_topological_vector_bundle_core.total_space} : cont_mdiff_within_at (I.prod 𝓘(𝕜, E')) I n Z.to_topological_vector_bundle_core.proj s p := Z.cont_mdiff_at_proj.cont_mdiff_within_at lemma smooth_within_at_proj {s : set (Z.to_topological_vector_bundle_core.total_space)} {p : Z.to_topological_vector_bundle_core.total_space} : smooth_within_at (I.prod 𝓘(𝕜, E')) I Z.to_topological_vector_bundle_core.proj s p := Z.cont_mdiff_within_at_proj /-- If an element of `E'` is invariant under all coordinate changes, then one can define a corresponding section of the fiber bundle, which is smooth. This applies in particular to the zero section of a vector bundle. Another example (not yet defined) would be the identity section of the endomorphism bundle of a vector bundle. -/ lemma smooth_const_section (v : E') (h : ∀ (i j : atlas H M), ∀ x ∈ i.1.source ∩ j.1.source, Z.coord_change i j (i.1 x) v = v) : smooth I (I.prod 𝓘(𝕜, E')) (show M → Z.to_topological_vector_bundle_core.total_space, from λ x, ⟨x, v⟩) := begin assume x, rw [cont_mdiff_at, cont_mdiff_within_at_iff'], split, { apply continuous.continuous_within_at, apply topological_fiber_bundle_core.continuous_const_section, assume i j y hy, exact h _ _ _ hy }, { have : cont_diff 𝕜 ⊤ (λ (y : E), (y, v)) := cont_diff_id.prod cont_diff_const, apply this.cont_diff_within_at.congr, { assume y hy, simp only with mfld_simps at hy, simp only [chart, hy, chart_at, prod.mk.inj_iff, to_topological_vector_bundle_core] with mfld_simps, apply h, simp only [hy, subtype.val_eq_coe] with mfld_simps, exact mem_chart_source H (((chart_at H x).symm) ((model_with_corners.symm I) y)) }, { simp only [chart, chart_at, prod.mk.inj_iff, to_topological_vector_bundle_core] with mfld_simps, apply h, simp only [subtype.val_eq_coe] with mfld_simps, exact mem_chart_source H x, } } end end basic_smooth_vector_bundle_core /-! ### Smoothness of the tangent bundle projection -/ namespace tangent_bundle include Is lemma cont_mdiff_proj : cont_mdiff I.tangent I n (proj I M) := basic_smooth_vector_bundle_core.cont_mdiff_proj _ lemma smooth_proj : smooth I.tangent I (proj I M) := basic_smooth_vector_bundle_core.smooth_proj _ lemma cont_mdiff_on_proj {s : set (tangent_bundle I M)} : cont_mdiff_on I.tangent I n (proj I M) s := basic_smooth_vector_bundle_core.cont_mdiff_on_proj _ lemma smooth_on_proj {s : set (tangent_bundle I M)} : smooth_on I.tangent I (proj I M) s := basic_smooth_vector_bundle_core.smooth_on_proj _ lemma cont_mdiff_at_proj {p : tangent_bundle I M} : cont_mdiff_at I.tangent I n (proj I M) p := basic_smooth_vector_bundle_core.cont_mdiff_at_proj _ lemma smooth_at_proj {p : tangent_bundle I M} : smooth_at I.tangent I (proj I M) p := basic_smooth_vector_bundle_core.smooth_at_proj _ lemma cont_mdiff_within_at_proj {s : set (tangent_bundle I M)} {p : tangent_bundle I M} : cont_mdiff_within_at I.tangent I n (proj I M) s p := basic_smooth_vector_bundle_core.cont_mdiff_within_at_proj _ lemma smooth_within_at_proj {s : set (tangent_bundle I M)} {p : tangent_bundle I M} : smooth_within_at I.tangent I (proj I M) s p := basic_smooth_vector_bundle_core.smooth_within_at_proj _ variables (I M) /-- The zero section of the tangent bundle -/ def zero_section : M → tangent_bundle I M := λ x, ⟨x, 0⟩ variables {I M} lemma smooth_zero_section : smooth I I.tangent (zero_section I M) := begin apply basic_smooth_vector_bundle_core.smooth_const_section (tangent_bundle_core I M) 0, assume i j x hx, simp only [tangent_bundle_core, continuous_linear_map.map_zero, continuous_linear_map.coe_coe] with mfld_simps, end open bundle /-- The derivative of the zero section of the tangent bundle maps `⟨x, v⟩` to `⟨⟨x, 0⟩, ⟨v, 0⟩⟩`. Note that, as currently framed, this is a statement in coordinates, thus reliant on the choice of the coordinate system we use on the tangent bundle. However, the result itself is coordinate-dependent only to the extent that the coordinates determine a splitting of the tangent bundle. Moreover, there is a canonical splitting at each point of the zero section (since there is a canonical horizontal space there, the tangent space to the zero section, in addition to the canonical vertical space which is the kernel of the derivative of the projection), and this canonical splitting is also the one that comes from the coordinates on the tangent bundle in our definitions. So this statement is not as crazy as it may seem. TODO define splittings of vector bundles; state this result invariantly. -/ lemma tangent_map_tangent_bundle_pure (p : tangent_bundle I M) : tangent_map I I.tangent (tangent_bundle.zero_section I M) p = ⟨⟨p.1, 0⟩, ⟨p.2, 0⟩⟩ := begin rcases p with ⟨x, v⟩, have N : I.symm ⁻¹' (chart_at H x).target ∈ 𝓝 (I ((chart_at H x) x)), { apply is_open.mem_nhds, apply (local_homeomorph.open_target _).preimage I.continuous_inv_fun, simp only with mfld_simps }, have A : mdifferentiable_at I I.tangent (λ x, @total_space_mk M (tangent_space I) x 0) x := tangent_bundle.smooth_zero_section.mdifferentiable_at, have B : fderiv_within 𝕜 (λ (x_1 : E), (x_1, (0 : E))) (set.range ⇑I) (I ((chart_at H x) x)) v = (v, 0), { rw [fderiv_within_eq_fderiv, differentiable_at.fderiv_prod], { simp }, { exact differentiable_at_id' }, { exact differentiable_at_const _ }, { exact model_with_corners.unique_diff_at_image I }, { exact differentiable_at_id'.prod (differentiable_at_const _) } }, simp only [tangent_bundle.zero_section, tangent_map, mfderiv, A, dif_pos, chart_at, basic_smooth_vector_bundle_core.chart, basic_smooth_vector_bundle_core.to_topological_vector_bundle_core, tangent_bundle_core, function.comp, continuous_linear_map.map_zero] with mfld_simps, rw ← fderiv_within_inter N (I.unique_diff (I ((chart_at H x) x)) (set.mem_range_self _)) at B, rw [← fderiv_within_inter N (I.unique_diff (I ((chart_at H x) x)) (set.mem_range_self _)), ← B], congr' 2, apply fderiv_within_congr _ (λ y hy, _), { simp only [prod.mk.inj_iff] with mfld_simps, exact ((tangent_bundle_core I M).to_topological_vector_bundle_core.coord_change ((tangent_bundle_core I M).to_topological_vector_bundle_core.index_at (((chart_at H x).symm) (I.symm (I ((chart_at H x) x))))) ⟨chart_at H x, _⟩ (((chart_at H x).symm) (I.symm (I ((chart_at H x) x))))).map_zero, }, { apply unique_diff_within_at.inter (I.unique_diff _ _) N, simp only with mfld_simps }, { simp only with mfld_simps at hy, simp only [hy, prod.mk.inj_iff] with mfld_simps, exact ((tangent_bundle_core I M).to_topological_vector_bundle_core.coord_change ((tangent_bundle_core I M).to_topological_vector_bundle_core.index_at (((chart_at H x).symm) (I.symm y))) ⟨chart_at H x, _⟩ (((chart_at H x).symm) (I.symm y))).map_zero, }, end end tangent_bundle /-! ### Smoothness of standard maps associated to the product of manifolds -/ section prod_mk lemma cont_mdiff_within_at.prod_mk {f : M → M'} {g : M → N'} (hf : cont_mdiff_within_at I I' n f s x) (hg : cont_mdiff_within_at I J' n g s x) : cont_mdiff_within_at I (I'.prod J') n (λ x, (f x, g x)) s x := begin rw cont_mdiff_within_at_iff at *, exact ⟨hf.1.prod hg.1, hf.2.prod hg.2⟩, end lemma cont_mdiff_within_at.prod_mk_space {f : M → E'} {g : M → F'} (hf : cont_mdiff_within_at I 𝓘(𝕜, E') n f s x) (hg : cont_mdiff_within_at I 𝓘(𝕜, F') n g s x) : cont_mdiff_within_at I 𝓘(𝕜, E' × F') n (λ x, (f x, g x)) s x := begin rw cont_mdiff_within_at_iff at *, exact ⟨hf.1.prod hg.1, hf.2.prod hg.2⟩, end lemma cont_mdiff_at.prod_mk {f : M → M'} {g : M → N'} (hf : cont_mdiff_at I I' n f x) (hg : cont_mdiff_at I J' n g x) : cont_mdiff_at I (I'.prod J') n (λ x, (f x, g x)) x := hf.prod_mk hg lemma cont_mdiff_at.prod_mk_space {f : M → E'} {g : M → F'} (hf : cont_mdiff_at I 𝓘(𝕜, E') n f x) (hg : cont_mdiff_at I 𝓘(𝕜, F') n g x) : cont_mdiff_at I 𝓘(𝕜, E' × F') n (λ x, (f x, g x)) x := hf.prod_mk_space hg lemma cont_mdiff_on.prod_mk {f : M → M'} {g : M → N'} (hf : cont_mdiff_on I I' n f s) (hg : cont_mdiff_on I J' n g s) : cont_mdiff_on I (I'.prod J') n (λ x, (f x, g x)) s := λ x hx, (hf x hx).prod_mk (hg x hx) lemma cont_mdiff_on.prod_mk_space {f : M → E'} {g : M → F'} (hf : cont_mdiff_on I 𝓘(𝕜, E') n f s) (hg : cont_mdiff_on I 𝓘(𝕜, F') n g s) : cont_mdiff_on I 𝓘(𝕜, E' × F') n (λ x, (f x, g x)) s := λ x hx, (hf x hx).prod_mk_space (hg x hx) lemma cont_mdiff.prod_mk {f : M → M'} {g : M → N'} (hf : cont_mdiff I I' n f) (hg : cont_mdiff I J' n g) : cont_mdiff I (I'.prod J') n (λ x, (f x, g x)) := λ x, (hf x).prod_mk (hg x) lemma cont_mdiff.prod_mk_space {f : M → E'} {g : M → F'} (hf : cont_mdiff I 𝓘(𝕜, E') n f) (hg : cont_mdiff I 𝓘(𝕜, F') n g) : cont_mdiff I 𝓘(𝕜, E' × F') n (λ x, (f x, g x)) := λ x, (hf x).prod_mk_space (hg x) lemma smooth_within_at.prod_mk {f : M → M'} {g : M → N'} (hf : smooth_within_at I I' f s x) (hg : smooth_within_at I J' g s x) : smooth_within_at I (I'.prod J') (λ x, (f x, g x)) s x := hf.prod_mk hg lemma smooth_within_at.prod_mk_space {f : M → E'} {g : M → F'} (hf : smooth_within_at I 𝓘(𝕜, E') f s x) (hg : smooth_within_at I 𝓘(𝕜, F') g s x) : smooth_within_at I 𝓘(𝕜, E' × F') (λ x, (f x, g x)) s x := hf.prod_mk_space hg lemma smooth_at.prod_mk {f : M → M'} {g : M → N'} (hf : smooth_at I I' f x) (hg : smooth_at I J' g x) : smooth_at I (I'.prod J') (λ x, (f x, g x)) x := hf.prod_mk hg lemma smooth_at.prod_mk_space {f : M → E'} {g : M → F'} (hf : smooth_at I 𝓘(𝕜, E') f x) (hg : smooth_at I 𝓘(𝕜, F') g x) : smooth_at I 𝓘(𝕜, E' × F') (λ x, (f x, g x)) x := hf.prod_mk_space hg lemma smooth_on.prod_mk {f : M → M'} {g : M → N'} (hf : smooth_on I I' f s) (hg : smooth_on I J' g s) : smooth_on I (I'.prod J') (λ x, (f x, g x)) s := hf.prod_mk hg lemma smooth_on.prod_mk_space {f : M → E'} {g : M → F'} (hf : smooth_on I 𝓘(𝕜, E') f s) (hg : smooth_on I 𝓘(𝕜, F') g s) : smooth_on I 𝓘(𝕜, E' × F') (λ x, (f x, g x)) s := hf.prod_mk_space hg lemma smooth.prod_mk {f : M → M'} {g : M → N'} (hf : smooth I I' f) (hg : smooth I J' g) : smooth I (I'.prod J') (λ x, (f x, g x)) := hf.prod_mk hg lemma smooth.prod_mk_space {f : M → E'} {g : M → F'} (hf : smooth I 𝓘(𝕜, E') f) (hg : smooth I 𝓘(𝕜, F') g) : smooth I 𝓘(𝕜, E' × F') (λ x, (f x, g x)) := hf.prod_mk_space hg end prod_mk section projections lemma cont_mdiff_within_at_fst {s : set (M × N)} {p : M × N} : cont_mdiff_within_at (I.prod J) I n prod.fst s p := begin rw cont_mdiff_within_at_iff', refine ⟨continuous_within_at_fst, _⟩, refine cont_diff_within_at_fst.congr (λ y hy, _) _, { simp only with mfld_simps at hy, simp only [hy] with mfld_simps }, { simp only with mfld_simps } end lemma cont_mdiff_at_fst {p : M × N} : cont_mdiff_at (I.prod J) I n prod.fst p := cont_mdiff_within_at_fst lemma cont_mdiff_on_fst {s : set (M × N)} : cont_mdiff_on (I.prod J) I n prod.fst s := λ x hx, cont_mdiff_within_at_fst lemma cont_mdiff_fst : cont_mdiff (I.prod J) I n (@prod.fst M N) := λ x, cont_mdiff_at_fst lemma smooth_within_at_fst {s : set (M × N)} {p : M × N} : smooth_within_at (I.prod J) I prod.fst s p := cont_mdiff_within_at_fst lemma smooth_at_fst {p : M × N} : smooth_at (I.prod J) I prod.fst p := cont_mdiff_at_fst lemma smooth_on_fst {s : set (M × N)} : smooth_on (I.prod J) I prod.fst s := cont_mdiff_on_fst lemma smooth_fst : smooth (I.prod J) I (@prod.fst M N) := cont_mdiff_fst lemma cont_mdiff_within_at_snd {s : set (M × N)} {p : M × N} : cont_mdiff_within_at (I.prod J) J n prod.snd s p := begin rw cont_mdiff_within_at_iff', refine ⟨continuous_within_at_snd, _⟩, refine cont_diff_within_at_snd.congr (λ y hy, _) _, { simp only with mfld_simps at hy, simp only [hy] with mfld_simps }, { simp only with mfld_simps } end lemma cont_mdiff_at_snd {p : M × N} : cont_mdiff_at (I.prod J) J n prod.snd p := cont_mdiff_within_at_snd lemma cont_mdiff_on_snd {s : set (M × N)} : cont_mdiff_on (I.prod J) J n prod.snd s := λ x hx, cont_mdiff_within_at_snd lemma cont_mdiff_snd : cont_mdiff (I.prod J) J n (@prod.snd M N) := λ x, cont_mdiff_at_snd lemma smooth_within_at_snd {s : set (M × N)} {p : M × N} : smooth_within_at (I.prod J) J prod.snd s p := cont_mdiff_within_at_snd lemma smooth_at_snd {p : M × N} : smooth_at (I.prod J) J prod.snd p := cont_mdiff_at_snd lemma smooth_on_snd {s : set (M × N)} : smooth_on (I.prod J) J prod.snd s := cont_mdiff_on_snd lemma smooth_snd : smooth (I.prod J) J (@prod.snd M N) := cont_mdiff_snd lemma smooth_iff_proj_smooth {f : M → M' × N'} : (smooth I (I'.prod J') f) ↔ (smooth I I' (prod.fst ∘ f)) ∧ (smooth I J' (prod.snd ∘ f)) := begin split, { intro h, exact ⟨smooth_fst.comp h, smooth_snd.comp h⟩ }, { rintro ⟨h_fst, h_snd⟩, simpa only [prod.mk.eta] using h_fst.prod_mk h_snd, } end end projections section prod_map variables {g : N → N'} {r : set N} {y : N} /-- 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_mdiff_within_at.prod_map' {p : M × N} (hf : cont_mdiff_within_at I I' n f s p.1) (hg : cont_mdiff_within_at J J' n g r p.2) : cont_mdiff_within_at (I.prod J) (I'.prod J') n (prod.map f g) (s ×ˢ r) p := (hf.comp p cont_mdiff_within_at_fst (prod_subset_preimage_fst _ _)).prod_mk $ hg.comp p cont_mdiff_within_at_snd (prod_subset_preimage_snd _ _) lemma cont_mdiff_within_at.prod_map (hf : cont_mdiff_within_at I I' n f s x) (hg : cont_mdiff_within_at J J' n g r y) : cont_mdiff_within_at (I.prod J) (I'.prod J') n (prod.map f g) (s ×ˢ r) (x, y) := cont_mdiff_within_at.prod_map' hf hg lemma cont_mdiff_at.prod_map (hf : cont_mdiff_at I I' n f x) (hg : cont_mdiff_at J J' n g y) : cont_mdiff_at (I.prod J) (I'.prod J') n (prod.map f g) (x, y) := begin rw ← cont_mdiff_within_at_univ at *, convert hf.prod_map hg, exact univ_prod_univ.symm end lemma cont_mdiff_at.prod_map' {p : M × N} (hf : cont_mdiff_at I I' n f p.1) (hg : cont_mdiff_at J J' n g p.2) : cont_mdiff_at (I.prod J) (I'.prod J') n (prod.map f g) p := begin rcases p, exact hf.prod_map hg end lemma cont_mdiff_on.prod_map (hf : cont_mdiff_on I I' n f s) (hg : cont_mdiff_on J J' n g r) : cont_mdiff_on (I.prod J) (I'.prod J') n (prod.map f g) (s ×ˢ r) := (hf.comp cont_mdiff_on_fst (prod_subset_preimage_fst _ _)).prod_mk $ hg.comp (cont_mdiff_on_snd) (prod_subset_preimage_snd _ _) lemma cont_mdiff.prod_map (hf : cont_mdiff I I' n f) (hg : cont_mdiff J J' n g) : cont_mdiff (I.prod J) (I'.prod J') n (prod.map f g) := begin assume p, exact (hf p.1).prod_map' (hg p.2) end lemma smooth_within_at.prod_map (hf : smooth_within_at I I' f s x) (hg : smooth_within_at J J' g r y) : smooth_within_at (I.prod J) (I'.prod J') (prod.map f g) (s ×ˢ r) (x, y) := hf.prod_map hg lemma smooth_at.prod_map (hf : smooth_at I I' f x) (hg : smooth_at J J' g y) : smooth_at (I.prod J) (I'.prod J') (prod.map f g) (x, y) := hf.prod_map hg lemma smooth_on.prod_map (hf : smooth_on I I' f s) (hg : smooth_on J J' g r) : smooth_on (I.prod J) (I'.prod J') (prod.map f g) (s ×ˢ r) := hf.prod_map hg lemma smooth.prod_map (hf : smooth I I' f) (hg : smooth J J' g) : smooth (I.prod J) (I'.prod J') (prod.map f g) := hf.prod_map hg end prod_map section pi_space /-! ### Smoothness of functions with codomain `Π i, F i` We have no `model_with_corners.pi` yet, so we prove lemmas about functions `f : M → Π i, F i` and use `𝓘(𝕜, Π i, F i)` as the model space. -/ variables {ι : Type*} [fintype ι] {Fi : ι → Type*} [Π i, normed_add_comm_group (Fi i)] [Π i, normed_space 𝕜 (Fi i)] {φ : M → Π i, Fi i} lemma cont_mdiff_within_at_pi_space : cont_mdiff_within_at I (𝓘(𝕜, Π i, Fi i)) n φ s x ↔ ∀ i, cont_mdiff_within_at I (𝓘(𝕜, Fi i)) n (λ x, φ x i) s x := by simp only [cont_mdiff_within_at_iff, continuous_within_at_pi, cont_diff_within_at_pi, forall_and_distrib, written_in_ext_chart_at, ext_chart_model_space_eq_id, (∘), local_equiv.refl_coe, id] lemma cont_mdiff_on_pi_space : cont_mdiff_on I (𝓘(𝕜, Π i, Fi i)) n φ s ↔ ∀ i, cont_mdiff_on I (𝓘(𝕜, Fi i)) n (λ x, φ x i) s := ⟨λ h i x hx, cont_mdiff_within_at_pi_space.1 (h x hx) i, λ h x hx, cont_mdiff_within_at_pi_space.2 (λ i, h i x hx)⟩ lemma cont_mdiff_at_pi_space : cont_mdiff_at I (𝓘(𝕜, Π i, Fi i)) n φ x ↔ ∀ i, cont_mdiff_at I (𝓘(𝕜, Fi i)) n (λ x, φ x i) x := cont_mdiff_within_at_pi_space lemma cont_mdiff_pi_space : cont_mdiff I (𝓘(𝕜, Π i, Fi i)) n φ ↔ ∀ i, cont_mdiff I (𝓘(𝕜, Fi i)) n (λ x, φ x i) := ⟨λ h i x, cont_mdiff_at_pi_space.1 (h x) i, λ h x, cont_mdiff_at_pi_space.2 (λ i, h i x)⟩ lemma smooth_within_at_pi_space : smooth_within_at I (𝓘(𝕜, Π i, Fi i)) φ s x ↔ ∀ i, smooth_within_at I (𝓘(𝕜, Fi i)) (λ x, φ x i) s x := cont_mdiff_within_at_pi_space lemma smooth_on_pi_space : smooth_on I (𝓘(𝕜, Π i, Fi i)) φ s ↔ ∀ i, smooth_on I (𝓘(𝕜, Fi i)) (λ x, φ x i) s := cont_mdiff_on_pi_space lemma smooth_at_pi_space : smooth_at I (𝓘(𝕜, Π i, Fi i)) φ x ↔ ∀ i, smooth_at I (𝓘(𝕜, Fi i)) (λ x, φ x i) x := cont_mdiff_at_pi_space lemma smooth_pi_space : smooth I (𝓘(𝕜, Π i, Fi i)) φ ↔ ∀ i, smooth I (𝓘(𝕜, Fi i)) (λ x, φ x i) := cont_mdiff_pi_space end pi_space /-! ### Linear maps between normed spaces are smooth -/ lemma continuous_linear_map.cont_mdiff (L : E →L[𝕜] F) : cont_mdiff 𝓘(𝕜, E) 𝓘(𝕜, F) n L := L.cont_diff.cont_mdiff /-! ### Smoothness of standard operations -/ variables {V : Type*} [normed_add_comm_group V] [normed_space 𝕜 V] /-- On any vector space, multiplication by a scalar is a smooth operation. -/ lemma smooth_smul : smooth (𝓘(𝕜).prod 𝓘(𝕜, V)) 𝓘(𝕜, V) (λp : 𝕜 × V, p.1 • p.2) := smooth_iff.2 ⟨continuous_smul, λ x y, cont_diff_smul.cont_diff_on⟩ lemma cont_mdiff_within_at.smul {f : M → 𝕜} {g : M → V} (hf : cont_mdiff_within_at I 𝓘(𝕜) n f s x) (hg : cont_mdiff_within_at I 𝓘(𝕜, V) n g s x) : cont_mdiff_within_at I 𝓘(𝕜, V) n (λ p, f p • g p) s x := (smooth_smul.of_le le_top).cont_mdiff_at.comp_cont_mdiff_within_at x (hf.prod_mk hg) lemma cont_mdiff_at.smul {f : M → 𝕜} {g : M → V} (hf : cont_mdiff_at I 𝓘(𝕜) n f x) (hg : cont_mdiff_at I 𝓘(𝕜, V) n g x) : cont_mdiff_at I 𝓘(𝕜, V) n (λ p, f p • g p) x := hf.smul hg lemma cont_mdiff_on.smul {f : M → 𝕜} {g : M → V} (hf : cont_mdiff_on I 𝓘(𝕜) n f s) (hg : cont_mdiff_on I 𝓘(𝕜, V) n g s) : cont_mdiff_on I 𝓘(𝕜, V) n (λ p, f p • g p) s := λ x hx, (hf x hx).smul (hg x hx) lemma cont_mdiff.smul {f : M → 𝕜} {g : M → V} (hf : cont_mdiff I 𝓘(𝕜) n f) (hg : cont_mdiff I 𝓘(𝕜, V) n g) : cont_mdiff I 𝓘(𝕜, V) n (λ p, f p • g p) := λ x, (hf x).smul (hg x) lemma smooth_within_at.smul {f : M → 𝕜} {g : M → V} (hf : smooth_within_at I 𝓘(𝕜) f s x) (hg : smooth_within_at I 𝓘(𝕜, V) g s x) : smooth_within_at I 𝓘(𝕜, V) (λ p, f p • g p) s x := hf.smul hg lemma smooth_at.smul {f : M → 𝕜} {g : M → V} (hf : smooth_at I 𝓘(𝕜) f x) (hg : smooth_at I 𝓘(𝕜, V) g x) : smooth_at I 𝓘(𝕜, V) (λ p, f p • g p) x := hf.smul hg lemma smooth_on.smul {f : M → 𝕜} {g : M → V} (hf : smooth_on I 𝓘(𝕜) f s) (hg : smooth_on I 𝓘(𝕜, V) g s) : smooth_on I 𝓘(𝕜, V) (λ p, f p • g p) s := hf.smul hg lemma smooth.smul {f : M → 𝕜} {g : M → V} (hf : smooth I 𝓘(𝕜) f) (hg : smooth I 𝓘(𝕜, V) g) : smooth I 𝓘(𝕜, V) (λ p, f p • g p) := hf.smul hg
4a31cc32e7dc5e790ca9831473a90ea4ca2a8f92
649957717d58c43b5d8d200da34bf374293fe739
/src/data/real/ennreal.lean
b43a2f74fcc63c1998e3e8394b1801a3c5ea92c0
[ "Apache-2.0" ]
permissive
Vtec234/mathlib
b50c7b21edea438df7497e5ed6a45f61527f0370
fb1848bbbfce46152f58e219dc0712f3289d2b20
refs/heads/master
1,592,463,095,113
1,562,737,749,000
1,562,737,749,000
196,202,858
0
0
Apache-2.0
1,562,762,338,000
1,562,762,337,000
null
UTF-8
Lean
false
false
30,803
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl Extended non-negative reals -/ import data.real.nnreal order.bounds data.set.intervals tactic.norm_num noncomputable theory open classical set lattice local attribute [instance] prop_decidable variables {α : Type*} {β : Type*} /-- The extended nonnegative real numbers. This is usually denoted [0, ∞], and is relevant as the codomain of a measure. -/ def ennreal := with_top nnreal local notation `∞` := (⊤ : ennreal) namespace ennreal variables {a b c d : ennreal} {r p q : nnreal} instance : canonically_ordered_comm_semiring ennreal := by unfold ennreal; apply_instance instance : decidable_linear_order ennreal := by unfold ennreal; apply_instance instance : complete_linear_order ennreal := by unfold ennreal; apply_instance instance : inhabited ennreal := ⟨0⟩ instance : densely_ordered ennreal := with_top.densely_ordered instance : has_coe nnreal ennreal := ⟨ option.some ⟩ lemma none_eq_top : (none : ennreal) = (⊤ : ennreal) := rfl lemma some_eq_coe (a : nnreal) : (some a : ennreal) = (↑a : ennreal) := rfl /-- `to_nnreal x` returns `x` if it is real, otherwise 0. -/ protected def to_nnreal : ennreal → nnreal | (some r) := r | none := 0 /-- `to_real x` returns `x` if it is real, `0` otherwise. -/ protected def to_real (a : ennreal) : real := coe (a.to_nnreal) /-- `of_real x` returns `x` if it is nonnegative, `0` otherwise. -/ protected def of_real (r : real) : ennreal := coe (nnreal.of_real r) @[simp] lemma to_nnreal_coe : (r : ennreal).to_nnreal = r := rfl @[simp] lemma coe_to_nnreal : ∀{a:ennreal}, a ≠ ∞ → ↑(a.to_nnreal) = a | (some r) h := rfl | none h := (h rfl).elim @[simp] lemma of_real_to_real {a : ennreal} (h : a ≠ ∞) : ennreal.of_real (a.to_real) = a := by simp [ennreal.to_real, ennreal.of_real, h] @[simp] lemma to_real_of_real {r : real} (h : 0 ≤ r) : ennreal.to_real (ennreal.of_real r) = r := by simp [ennreal.to_real, ennreal.of_real, nnreal.coe_of_real _ h] lemma coe_to_nnreal_le_self : ∀{a:ennreal}, ↑(a.to_nnreal) ≤ a | (some r) := by rw [some_eq_coe, to_nnreal_coe]; exact le_refl _ | none := le_top lemma coe_nnreal_eq (r : nnreal) : (r : ennreal) = ennreal.of_real r := by { rw [ennreal.of_real, nnreal.of_real], cases r with r h, congr, dsimp, rw max_eq_left h } lemma of_real_eq_coe_nnreal {x : real} (h : x ≥ 0) : ennreal.of_real x = @coe nnreal ennreal _ (⟨x, h⟩ : nnreal) := by { rw [coe_nnreal_eq], refl } @[simp] lemma coe_zero : ↑(0 : nnreal) = (0 : ennreal) := rfl @[simp] lemma coe_one : ↑(1 : nnreal) = (1 : ennreal) := rfl @[simp] lemma to_real_nonneg {a : ennreal} : 0 ≤ a.to_real := by simp [ennreal.to_real] @[simp] lemma top_to_nnreal : ∞.to_nnreal = 0 := rfl @[simp] lemma top_to_real : ∞.to_real = 0 := rfl @[simp] lemma zero_to_nnreal : (0 : ennreal).to_nnreal = 0 := rfl @[simp] lemma zero_to_real : (0 : ennreal).to_real = 0 := rfl @[simp] lemma of_real_zero : ennreal.of_real (0 : ℝ) = 0 := by simp [ennreal.of_real]; refl @[simp] lemma of_real_one : ennreal.of_real (1 : ℝ) = (1 : ennreal) := by simp [ennreal.of_real] lemma forall_ennreal {p : ennreal → Prop} : (∀a, p a) ↔ (∀r:nnreal, p r) ∧ p ∞ := ⟨assume h, ⟨assume r, h _, h _⟩, assume ⟨h₁, h₂⟩ a, match a with some r := h₁ _ | none := h₂ end⟩ lemma to_nnreal_eq_zero_iff (x : ennreal) : x.to_nnreal = 0 ↔ x = 0 ∨ x = ⊤ := ⟨begin cases x, { simp [none_eq_top] }, { have A : some (0:nnreal) = (0:ennreal) := rfl, simp [ennreal.to_nnreal, A] {contextual := tt} } end, by intro h; cases h; simp [h]⟩ lemma to_real_eq_zero_iff (x : ennreal) : x.to_real = 0 ↔ x = 0 ∨ x = ⊤ := by simp [ennreal.to_real, to_nnreal_eq_zero_iff] @[simp] lemma coe_ne_top : (r : ennreal) ≠ ∞ := with_top.coe_ne_top @[simp] lemma top_ne_coe : ∞ ≠ (r : ennreal) := with_top.top_ne_coe @[simp] lemma of_real_ne_top {r : ℝ} : ennreal.of_real r ≠ ∞ := by simp [ennreal.of_real] @[simp] lemma top_ne_of_real {r : ℝ} : ∞ ≠ ennreal.of_real r := by simp [ennreal.of_real] @[simp] lemma zero_ne_top : 0 ≠ ∞ := coe_ne_top @[simp] lemma top_ne_zero : ∞ ≠ 0 := top_ne_coe @[simp] lemma one_ne_top : 1 ≠ ∞ := coe_ne_top @[simp] lemma top_ne_one : ∞ ≠ 1 := top_ne_coe @[simp] lemma coe_eq_coe : (↑r : ennreal) = ↑q ↔ r = q := with_top.coe_eq_coe @[simp] lemma coe_le_coe : (↑r : ennreal) ≤ ↑q ↔ r ≤ q := with_top.coe_le_coe @[simp] lemma coe_lt_coe : (↑r : ennreal) < ↑q ↔ r < q := with_top.coe_lt_coe @[simp] lemma coe_eq_zero : (↑r : ennreal) = 0 ↔ r = 0 := coe_eq_coe @[simp] lemma zero_eq_coe : 0 = (↑r : ennreal) ↔ 0 = r := coe_eq_coe @[simp] lemma coe_eq_one : (↑r : ennreal) = 1 ↔ r = 1 := coe_eq_coe @[simp] lemma one_eq_coe : 1 = (↑r : ennreal) ↔ 1 = r := coe_eq_coe @[simp] lemma coe_nonneg : 0 ≤ (↑r : ennreal) ↔ 0 ≤ r := coe_le_coe @[simp] lemma coe_pos : 0 < (↑r : ennreal) ↔ 0 < r := coe_lt_coe @[simp] lemma coe_add : ↑(r + p) = (r + p : ennreal) := with_top.coe_add @[simp] lemma coe_mul : ↑(r * p) = (r * p : ennreal) := with_top.coe_mul @[simp] lemma coe_bit0 : (↑(bit0 r) : ennreal) = bit0 r := coe_add @[simp] lemma coe_bit1 : (↑(bit1 r) : ennreal) = bit1 r := by simp [bit1] @[simp] lemma add_top : a + ∞ = ∞ := with_top.add_top @[simp] lemma top_add : ∞ + a = ∞ := with_top.top_add instance : is_semiring_hom (coe : nnreal → ennreal) := by refine_struct {..}; simp lemma add_eq_top : a + b = ∞ ↔ a = ∞ ∨ b = ∞ := with_top.add_eq_top _ _ lemma add_lt_top : a + b < ∞ ↔ a < ∞ ∧ b < ∞ := with_top.add_lt_top _ _ lemma to_nnreal_add {r₁ r₂ : ennreal} (h₁ : r₁ < ⊤) (h₂ : r₂ < ⊤) : (r₁ + r₂).to_nnreal = r₁.to_nnreal + r₂.to_nnreal := begin rw [← coe_eq_coe, coe_add, coe_to_nnreal, coe_to_nnreal, coe_to_nnreal]; apply @ne_top_of_lt ennreal _ _ ⊤, exact h₂, exact h₁, exact add_lt_top.2 ⟨h₁, h₂⟩ end /- rw has trouble with the generic lt_top_iff_ne_top and bot_lt_iff_ne_bot (contrary to erw). This is solved with the next lemmas -/ protected lemma lt_top_iff_ne_top : a < ∞ ↔ a ≠ ∞ := lt_top_iff_ne_top protected lemma bot_lt_iff_ne_bot : 0 < a ↔ a ≠ 0 := bot_lt_iff_ne_bot lemma mul_top : a * ∞ = (if a = 0 then 0 else ∞) := begin split_ifs, { simp [h] }, { exact with_top.mul_top h } end lemma top_mul : ∞ * a = (if a = 0 then 0 else ∞) := begin split_ifs, { simp [h] }, { exact with_top.top_mul h } end @[simp] lemma top_mul_top : ∞ * ∞ = ∞ := with_top.top_mul_top lemma mul_eq_top {a b : ennreal} : a * b = ⊤ ↔ (a ≠ 0 ∧ b = ⊤) ∨ (a = ⊤ ∧ b ≠ 0) := with_top.mul_eq_top_iff lemma mul_lt_top {a b : ennreal} : a < ⊤ → b < ⊤ → a * b < ⊤ := by simp [ennreal.lt_top_iff_ne_top, (≠), mul_eq_top] {contextual := tt} @[simp] lemma coe_finset_sum {s : finset α} {f : α → nnreal} : ↑(s.sum f) = (s.sum (λa, f a) : ennreal) := (finset.sum_hom coe).symm @[simp] lemma coe_finset_prod {s : finset α} {f : α → nnreal} : ↑(s.prod f) = (s.prod (λa, f a) : ennreal) := (finset.prod_hom coe).symm @[simp] lemma bot_eq_zero : (⊥ : ennreal) = 0 := rfl section order @[simp] lemma coe_lt_top : coe r < ∞ := with_top.coe_lt_top r @[simp] lemma not_top_le_coe : ¬ (⊤:ennreal) ≤ ↑r := with_top.not_top_le_coe r @[simp] lemma zero_lt_coe_iff : 0 < (↑p : ennreal) ↔ 0 < p := coe_lt_coe @[simp] lemma one_le_coe_iff : (1:ennreal) ≤ ↑r ↔ 1 ≤ r := coe_le_coe @[simp] lemma coe_le_one_iff : ↑r ≤ (1:ennreal) ↔ r ≤ 1 := coe_le_coe @[simp] lemma coe_lt_one_iff : (↑p : ennreal) < 1 ↔ p < 1 := coe_lt_coe @[simp] lemma one_lt_zero_iff : 1 < (↑p : ennreal) ↔ 1 < p := coe_lt_coe @[simp] lemma coe_nat (n : nat) : ((n : nnreal) : ennreal) = n := with_top.coe_nat n @[simp] lemma nat_ne_top (n : nat) : (n : ennreal) ≠ ⊤ := with_top.nat_ne_top n @[simp] lemma top_ne_nat (n : nat) : (⊤ : ennreal) ≠ n := with_top.top_ne_nat n lemma le_coe_iff : a ≤ ↑r ↔ (∃p:nnreal, a = p ∧ p ≤ r) := with_top.le_coe_iff r a lemma coe_le_iff : ↑r ≤ a ↔ (∀p:nnreal, a = p → r ≤ p) := with_top.coe_le_iff r a lemma lt_iff_exists_coe : a < b ↔ (∃p:nnreal, a = p ∧ ↑p < b) := with_top.lt_iff_exists_coe a b -- TODO: move to canonically ordered semiring ... protected lemma zero_lt_one : 0 < (1 : ennreal) := zero_lt_coe_iff.mpr zero_lt_one @[simp] lemma not_lt_zero : ¬ a < 0 := by simp lemma add_lt_add_iff_left : a < ⊤ → (a + c < a + b ↔ c < b) := with_top.add_lt_add_iff_left lemma add_lt_add_iff_right : a < ⊤ → (c + a < b + a ↔ c < b) := with_top.add_lt_add_iff_right lemma lt_add_right (ha : a < ⊤) (hb : 0 < b) : a < a + b := by rwa [← add_lt_add_iff_left ha, add_zero] at hb lemma le_of_forall_epsilon_le : ∀{a b : ennreal}, (∀ε:nnreal, 0 < ε → b < ∞ → a ≤ b + ε) → a ≤ b | a none h := le_top | none (some a) h := have (⊤:ennreal) ≤ ↑a + ↑(1:nnreal), from h 1 zero_lt_one coe_lt_top, by rw [← coe_add] at this; exact (not_top_le_coe this).elim | (some a) (some b) h := by simp only [none_eq_top, some_eq_coe, coe_add.symm, coe_le_coe, coe_lt_top, true_implies_iff] at *; exact nnreal.le_of_forall_epsilon_le h lemma lt_iff_exists_rat_btwn : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < nnreal.of_real q ∧ (nnreal.of_real q:ennreal) < b) := ⟨λ h, begin rcases lt_iff_exists_coe.1 h with ⟨p, rfl, _⟩, rcases dense h with ⟨c, pc, cb⟩, rcases lt_iff_exists_coe.1 cb with ⟨r, rfl, _⟩, rcases (nnreal.lt_iff_exists_rat_btwn _ _).1 (coe_lt_coe.1 pc) with ⟨q, hq0, pq, qr⟩, exact ⟨q, hq0, coe_lt_coe.2 pq, lt_trans (coe_lt_coe.2 qr) cb⟩ end, λ ⟨q, q0, qa, qb⟩, lt_trans qa qb⟩ lemma lt_iff_exists_real_btwn : a < b ↔ (∃r:ℝ, 0 ≤ r ∧ a < ennreal.of_real r ∧ (ennreal.of_real r:ennreal) < b) := ⟨λ h, let ⟨q, q0, aq, qb⟩ := ennreal.lt_iff_exists_rat_btwn.1 h in ⟨q, rat.cast_nonneg.2 q0, aq, qb⟩, λ ⟨q, q0, qa, qb⟩, lt_trans qa qb⟩ protected lemma exists_nat_gt {r : ennreal} (h : r ≠ ⊤) : ∃n:ℕ, r < n := begin rcases lt_iff_exists_coe.1 (lt_top_iff_ne_top.2 h) with ⟨r, rfl, hb⟩, rcases exists_nat_gt r with ⟨n, hn⟩, refine ⟨n, _⟩, rwa [← ennreal.coe_nat, ennreal.coe_lt_coe], end lemma add_lt_add (ac : a < c) (bd : b < d) : a + b < c + d := begin rcases dense ac with ⟨a', aa', a'c⟩, rcases lt_iff_exists_coe.1 aa' with ⟨aR, rfl, _⟩, rcases lt_iff_exists_coe.1 a'c with ⟨a'R, rfl, _⟩, rcases dense bd with ⟨b', bb', b'd⟩, rcases lt_iff_exists_coe.1 bb' with ⟨bR, rfl, _⟩, rcases lt_iff_exists_coe.1 b'd with ⟨b'R, rfl, _⟩, have I : ↑aR + ↑bR < ↑a'R + ↑b'R := begin rw [← coe_add, ← coe_add, coe_lt_coe], apply add_lt_add (coe_lt_coe.1 aa') (coe_lt_coe.1 bb') end, have J : ↑a'R + ↑b'R ≤ c + d := add_le_add' (le_of_lt a'c) (le_of_lt b'd), apply lt_of_lt_of_le I J end end order section complete_lattice lemma coe_Sup {s : set nnreal} : bdd_above s → (↑(Sup s) : ennreal) = (⨆a∈s, ↑a) := with_top.coe_Sup lemma coe_Inf {s : set nnreal} : s ≠ ∅ → (↑(Inf s) : ennreal) = (⨅a∈s, ↑a) := with_top.coe_Inf @[simp] lemma top_mem_upper_bounds {s : set ennreal} : ∞ ∈ upper_bounds s := assume x hx, le_top lemma coe_mem_upper_bounds {s : set nnreal} : ↑r ∈ upper_bounds ((coe : nnreal → ennreal) '' s) ↔ r ∈ upper_bounds s := by simp [upper_bounds, ball_image_iff, -mem_image, *] {contextual := tt} lemma infi_ennreal {α : Type*} [complete_lattice α] {f : ennreal → α} : (⨅n, f n) = (⨅n:nnreal, f n) ⊓ f ⊤ := le_antisymm (le_inf (le_infi $ assume i, infi_le _ _) (infi_le _ _)) (le_infi $ forall_ennreal.2 ⟨assume r, inf_le_left_of_le $ infi_le _ _, inf_le_right⟩) end complete_lattice section mul lemma mul_eq_mul_left : a ≠ 0 → a ≠ ⊤ → (a * b = a * c ↔ b = c) := begin cases a; cases b; cases c; simp [none_eq_top, some_eq_coe, mul_top, top_mul, -coe_mul, coe_mul.symm, nnreal.mul_eq_mul_left] {contextual := tt}, end lemma mul_le_mul_left : a ≠ 0 → a ≠ ⊤ → (a * b ≤ a * c ↔ b ≤ c) := begin cases a; cases b; cases c; simp [none_eq_top, some_eq_coe, mul_top, top_mul, -coe_mul, coe_mul.symm] {contextual := tt}, assume h, exact mul_le_mul_left (zero_lt_iff_ne_zero.2 h) end lemma mul_eq_zero {a b : ennreal} : a * b = 0 ↔ a = 0 ∨ b = 0 := canonically_ordered_comm_semiring.mul_eq_zero_iff _ _ end mul section sub instance : has_sub ennreal := ⟨λa b, Inf {d | a ≤ d + b}⟩ lemma coe_sub : ↑(p - r) = (↑p:ennreal) - r := le_antisymm (le_Inf $ assume b (hb : ↑p ≤ b + r), coe_le_iff.2 $ by rintros d rfl; rwa [← coe_add, coe_le_coe, ← nnreal.sub_le_iff_le_add] at hb) (Inf_le $ show (↑p : ennreal) ≤ ↑(p - r) + ↑r, by rw [← coe_add, coe_le_coe, ← nnreal.sub_le_iff_le_add]) @[simp] lemma top_sub_coe : ∞ - ↑r = ∞ := top_unique $ le_Inf $ by simp [add_eq_top] @[simp] lemma sub_eq_zero_of_le (h : a ≤ b) : a - b = 0 := le_antisymm (Inf_le $ le_add_left h) (zero_le _) @[simp] lemma sub_self : a - a = 0 := sub_eq_zero_of_le $ le_refl _ @[simp] lemma zero_sub : 0 - a = 0 := le_antisymm (Inf_le $ zero_le _) (zero_le _) @[simp] lemma sub_infty : a - ∞ = 0 := le_antisymm (Inf_le $ by simp) (zero_le _) lemma sub_le_sub (h₁ : a ≤ b) (h₂ : d ≤ c) : a - c ≤ b - d := Inf_le_Inf $ assume e (h : b ≤ e + d), calc a ≤ b : h₁ ... ≤ e + d : h ... ≤ e + c : add_le_add' (le_refl _) h₂ @[simp] lemma add_sub_self : ∀{a b : ennreal}, b < ∞ → (a + b) - b = a | a none := by simp [none_eq_top] | none (some b) := by simp [none_eq_top, some_eq_coe] | (some a) (some b) := by simp [some_eq_coe]; rw [← coe_add, ← coe_sub, coe_eq_coe, nnreal.add_sub_cancel] @[simp] lemma add_sub_self' (h : a < ∞) : (a + b) - a = b := by rw [add_comm, add_sub_self h] lemma add_left_inj (h : a < ∞) : a + b = a + c ↔ b = c := ⟨λ e, by simpa [h] using congr_arg (λ x, x - a) e, congr_arg _⟩ lemma add_right_inj (h : a < ∞) : b + a = c + a ↔ b = c := by rw [add_comm, add_comm c, add_left_inj h] @[simp] lemma sub_add_cancel_of_le : ∀{a b : ennreal}, b ≤ a → (a - b) + b = a := begin simp [forall_ennreal, le_coe_iff, -add_comm] {contextual := tt}, rintros r p x rfl h, rw [← coe_sub, ← coe_add, nnreal.sub_add_cancel_of_le h] end @[simp] lemma add_sub_cancel_of_le (h : b ≤ a) : b + (a - b) = a := by rwa [add_comm, sub_add_cancel_of_le] lemma sub_add_self_eq_max : (a - b) + b = max a b := match le_total a b with | or.inl h := by simp [h, max_eq_right] | or.inr h := by simp [h, max_eq_left] end @[simp] protected lemma sub_le_iff_le_add : a - b ≤ c ↔ a ≤ c + b := iff.intro (assume h : a - b ≤ c, calc a ≤ (a - b) + b : by rw [sub_add_self_eq_max]; exact le_max_left _ _ ... ≤ c + b : add_le_add' h (le_refl _)) (assume h : a ≤ c + b, calc a - b ≤ (c + b) - b : sub_le_sub h (le_refl _) ... ≤ c : Inf_le (le_refl (c + b))) protected lemma sub_le_of_sub_le (h : a - b ≤ c) : a - c ≤ b := ennreal.sub_le_iff_le_add.2 $ by { rw add_comm, exact ennreal.sub_le_iff_le_add.1 h } protected lemma sub_lt_sub_self : a ≠ ⊤ → a ≠ 0 → 0 < b → a - b < a := match a, b with | none, _ := by { have := none_eq_top, assume h, contradiction } | (some a), none := by {intros, simp only [none_eq_top, sub_infty, zero_lt_iff_ne_zero], assumption} | (some a), (some b) := begin simp only [some_eq_coe, coe_sub.symm, coe_pos, coe_eq_zero, coe_lt_coe, ne.def], assume h₁ h₂, apply nnreal.sub_lt_self, exact zero_lt_iff_ne_zero.2 h₂ end end @[simp] lemma sub_eq_zero_iff_le : a - b = 0 ↔ a ≤ b := by simpa [-ennreal.sub_le_iff_le_add] using @ennreal.sub_le_iff_le_add a b 0 @[simp] lemma zero_lt_sub_iff_lt : 0 < a - b ↔ b < a := by simpa [ennreal.bot_lt_iff_ne_bot, -sub_eq_zero_iff_le] using not_iff_not.2 (@sub_eq_zero_iff_le a b) lemma sub_le_self (a b : ennreal) : a - b ≤ a := ennreal.sub_le_iff_le_add.2 $ le_add_of_nonneg_right' $ zero_le _ @[simp] lemma sub_zero : a - 0 = a := eq.trans (add_zero (a - 0)).symm $ by simp lemma sub_sub_cancel (h : a < ∞) (h2 : b ≤ a) : a - (a - b) = b := by rw [← add_right_inj (lt_of_le_of_lt (sub_le_self _ _) h), sub_add_cancel_of_le (sub_le_self _ _), add_sub_cancel_of_le h2] lemma sub_left_inj {a b c : ennreal} (ha : a < ⊤) (hb : b ≤ a) (hc : c ≤ a) : a - b = a - c ↔ b = c := iff.intro begin assume h, have : a - (a - b) = a - (a - c), rw h, rw [sub_sub_cancel ha hb, sub_sub_cancel ha hc] at this, exact this end (λ h, by rw h) end sub section interval variables {x y z : ennreal} {ε ε₁ ε₂ : ennreal} {s : set ennreal} protected lemma Ico_eq_Iio : (Ico 0 y) = (Iio y) := ext $ assume a, iff.intro (assume ⟨_, hx⟩, hx) (assume hx, ⟨zero_le _, hx⟩) lemma mem_Iio_self_add : x ≠ ⊤ → 0 < ε → x ∈ Iio (x + ε) := assume xt ε0, lt_add_right (by rwa lt_top_iff_ne_top) ε0 lemma not_mem_Ioo_self_sub : x = 0 → x ∉ Ioo (x - ε) y := assume x0, by simp [x0] lemma mem_Ioo_self_sub_add : x ≠ ⊤ → x ≠ 0 → 0 < ε₁ → 0 < ε₂ → x ∈ Ioo (x - ε₁) (x + ε₂) := assume xt x0 ε0 ε0', ⟨ennreal.sub_lt_sub_self xt x0 ε0, lt_add_right (by rwa [lt_top_iff_ne_top]) ε0'⟩ end interval section bit @[simp] lemma bit0_inj : bit0 a = bit0 b ↔ a = b := ⟨λh, begin rcases (lt_trichotomy a b) with h₁| h₂| h₃, { exact (absurd h (ne_of_lt (add_lt_add h₁ h₁))) }, { exact h₂ }, { exact (absurd h.symm (ne_of_lt (add_lt_add h₃ h₃))) } end, λh, congr_arg _ h⟩ @[simp] lemma bit0_eq_zero_iff : bit0 a = 0 ↔ a = 0 := by simpa only [bit0_zero] using @bit0_inj a 0 @[simp] lemma bit0_eq_top_iff : bit0 a = ∞ ↔ a = ∞ := by rw [bit0, add_eq_top, or_self] @[simp] lemma bit1_inj : bit1 a = bit1 b ↔ a = b := ⟨λh, begin unfold bit1 at h, rwa [add_right_inj, bit0_inj] at h, simp [lt_top_iff_ne_top] end, λh, congr_arg _ h⟩ @[simp] lemma bit1_ne_zero : bit1 a ≠ 0 := by unfold bit1; simp @[simp] lemma bit1_eq_one_iff : bit1 a = 1 ↔ a = 0 := by simpa only [bit1_zero] using @bit1_inj a 0 @[simp] lemma bit1_eq_top_iff : bit1 a = ∞ ↔ a = ∞ := by unfold bit1; rw add_eq_top; simp end bit section inv instance : has_inv ennreal := ⟨λa, Inf {b | 1 ≤ a * b}⟩ instance : has_div ennreal := ⟨λa b, a * b⁻¹⟩ lemma div_def : a / b = a * b⁻¹ := rfl @[simp] lemma inv_zero : (0 : ennreal)⁻¹ = ∞ := show Inf {b : ennreal | 1 ≤ 0 * b} = ∞, by simp; refl @[simp] lemma inv_top : (∞ : ennreal)⁻¹ = 0 := bot_unique $ le_of_forall_le_of_dense $ λ a (h : a > 0), Inf_le $ by simp [*, ne_of_gt h, top_mul] @[simp] lemma coe_inv (hr : r ≠ 0) : (↑r⁻¹ : ennreal) = (↑r)⁻¹ := le_antisymm (le_Inf $ assume b (hb : 1 ≤ ↑r * b), coe_le_iff.2 $ by rintros b rfl; rwa [← coe_mul, ← coe_one, coe_le_coe, ← nnreal.inv_le hr] at hb) (Inf_le $ by simp; rw [← coe_mul, nnreal.mul_inv_cancel hr]; exact le_refl 1) @[simp] lemma coe_div (hr : r ≠ 0) : (↑(p / r) : ennreal) = p / r := show ↑(p * r⁻¹) = ↑p * (↑r)⁻¹, by rw [coe_mul, coe_inv hr] @[simp] lemma inv_inv : (a⁻¹)⁻¹ = a := by by_cases a = 0; cases a; simp [*, none_eq_top, some_eq_coe, -coe_inv, (coe_inv _).symm] at * @[simp] lemma inv_eq_top : a⁻¹ = ∞ ↔ a = 0 := by by_cases a = 0; cases a; simp [*, none_eq_top, some_eq_coe, -coe_inv, (coe_inv _).symm] at * lemma inv_ne_top : a⁻¹ ≠ ∞ ↔ a ≠ 0 := by simp @[simp] lemma inv_eq_zero : a⁻¹ = 0 ↔ a = ∞ := by rw [← inv_eq_top, inv_inv] lemma inv_ne_zero : a⁻¹ ≠ 0 ↔ a ≠ ∞ := by simp lemma le_div_iff_mul_le : ∀{b}, b ≠ 0 → b ≠ ⊤ → (a ≤ c / b ↔ a * b ≤ c) | none h0 ht := (ht rfl).elim | (some r) h0 ht := begin have hr : r ≠ 0, from mt coe_eq_coe.2 h0, rw [← ennreal.mul_le_mul_left h0 ht], suffices : ↑r * a ≤ (↑r * ↑r⁻¹) * c ↔ a * ↑r ≤ c, { simpa [some_eq_coe, div_def, hr, mul_left_comm, mul_comm, mul_assoc] }, rw [← coe_mul, nnreal.mul_inv_cancel hr, coe_one, one_mul, mul_comm] end lemma div_le_iff_le_mul (hb0 : b ≠ 0) (hbt : b ≠ ⊤) : a / b ≤ c ↔ a ≤ c * b := suffices a * b⁻¹ ≤ c ↔ a ≤ c / b⁻¹, by simpa [div_def], (le_div_iff_mul_le (inv_ne_zero.2 hbt) (inv_ne_top.2 hb0)).symm lemma inv_le_iff_le_mul : (b = ⊤ → a ≠ 0) → (a = ⊤ → b ≠ 0) → (a⁻¹ ≤ b ↔ 1 ≤ a * b) := begin cases a; cases b; simp [none_eq_top, some_eq_coe, mul_top, top_mul] {contextual := tt}, by_cases a = 0; simp [*, -coe_mul, coe_mul.symm, -coe_inv, (coe_inv _).symm, nnreal.inv_le] end @[simp] lemma le_inv_iff_mul_le : a ≤ b⁻¹ ↔ a * b ≤ 1 := begin cases b, { by_cases a = 0; simp [*, none_eq_top, mul_top] }, by_cases b = 0; simp [*, some_eq_coe, le_div_iff_mul_le], suffices : a ≤ 1 / b ↔ a * b ≤ 1, { simpa [div_def, h] }, exact le_div_iff_mul_le (mt coe_eq_coe.1 h) coe_ne_top end lemma mul_inv_cancel : ∀{r : ennreal}, r ≠ 0 → r ≠ ⊤ → r * r⁻¹ = 1 := begin refine forall_ennreal.2 ⟨λ r, _, _⟩; simp [-coe_inv, (coe_inv _).symm] {contextual := tt}, assume h, rw [← ennreal.coe_mul, nnreal.mul_inv_cancel h, coe_one] end lemma mul_le_if_le_inv {a b r : ennreal} (hr₀ : r ≠ 0) (hr₁ : r ≠ ⊤) : (r * a ≤ b ↔ a ≤ r⁻¹ * b) := by rw [← @ennreal.mul_le_mul_left _ a _ hr₀ hr₁, ← mul_assoc, mul_inv_cancel hr₀ hr₁, one_mul] lemma le_of_forall_lt_one_mul_lt : ∀{x y : ennreal}, (∀a<1, a * x ≤ y) → x ≤ y := forall_ennreal.2 $ and.intro (assume r, forall_ennreal.2 $ and.intro (assume q h, coe_le_coe.2 $ nnreal.le_of_forall_lt_one_mul_lt $ assume a ha, begin rw [← coe_le_coe, coe_mul], exact h _ (coe_lt_coe.2 ha) end) (assume h, le_top)) (assume r hr, have ((1 / 2 : nnreal) : ennreal) * ⊤ ≤ r := hr _ (coe_lt_coe.2 ((@nnreal.coe_lt (1/2) 1).2 one_half_lt_one)), have ne : ((1 / 2 : nnreal) : ennreal) ≠ 0, begin rw [(≠), coe_eq_zero], refine zero_lt_iff_ne_zero.1 _, show 0 < (1 / 2 : ℝ), exact div_pos zero_lt_one two_pos end, by rwa [mul_top, if_neg ne] at this) lemma div_add_div_same {a b c : ennreal} : a / c + b / c = (a + b) / c := eq.symm $ right_distrib a b (c⁻¹) lemma div_self {a : ennreal} (h0 : a ≠ 0) (hI : a ≠ ∞) : a / a = 1 := have A : 1 ≤ a / a := by simp [le_div_iff_mul_le h0 hI, le_refl], have B : a / a ≤ 1 := by simp [div_le_iff_le_mul h0 hI, le_refl], le_antisymm B A lemma add_halves (a : ennreal) : a / 2 + a / 2 = a := have ¬((2 : nnreal) : ennreal) = (0 : nnreal) := by rw [coe_eq_coe]; norm_num, have A : (2:ennreal) * 2⁻¹ = 1 := by rw [←div_def, div_self]; [assumption, apply coe_ne_top], calc a / 2 + a / 2 = (a + a) / 2 : by rw div_add_div_same ... = (a * 1 + a * 1) / 2 : by rw mul_one ... = (a * (1 + 1)) / 2 : by rw left_distrib ... = (a * 2) / 2 : by rw one_add_one_eq_two ... = (a * 2) * 2⁻¹ : by rw div_def ... = a * (2 * 2⁻¹) : by rw mul_assoc ... = a * 1 : by rw A ... = a : by rw mul_one @[simp] lemma div_zero_iff {a b : ennreal} : a / b = 0 ↔ a = 0 ∨ b = ⊤ := by simp [div_def, mul_eq_zero] @[simp] lemma div_pos_iff {a b : ennreal} : 0 < a / b ↔ a ≠ 0 ∧ b ≠ ⊤ := by simp [zero_lt_iff_ne_zero, not_or_distrib] lemma half_pos {a : ennreal} (h : 0 < a) : 0 < a / 2 := by simp [ne_of_gt h] lemma half_lt_self {a : ennreal} (hz : a ≠ 0) (ht : a ≠ ⊤) : a / 2 < a := begin cases a, { cases ht none_eq_top }, { simp [some_eq_coe] at hz, simpa [-coe_lt_coe, coe_div two_ne_zero'] using coe_lt_coe.2 (nnreal.half_lt_self hz) } end lemma exists_inv_nat_lt {a : ennreal} (h : a ≠ 0) : ∃n:ℕ, (n:ennreal)⁻¹ < a := begin rcases dense (bot_lt_iff_ne_bot.2 h) with ⟨b, bz, ba⟩, have bz' : b ≠ 0 := bot_lt_iff_ne_bot.1 bz, have : b⁻¹ ≠ ⊤ := by simp [bz'], rcases ennreal.exists_nat_gt this with ⟨n, bn⟩, have I : ((n : ℕ) : ennreal)⁻¹ ≤ b := begin rw [ennreal.inv_le_iff_le_mul, mul_comm, ← ennreal.inv_le_iff_le_mul], exact le_of_lt bn, simp only [h, ennreal.nat_ne_top, forall_prop_of_false, ne.def, not_false_iff], exact λ_, ne_bot_of_gt bn, exact λ_, ne_bot_of_gt bn, exact λ_, bz' end, exact ⟨n, lt_of_le_of_lt I ba⟩ end end inv section real lemma to_real_add (ha : a ≠ ⊤) (hb : b ≠ ⊤) : (a+b).to_real = a.to_real + b.to_real := begin cases a, { simpa [none_eq_top] using ha }, cases b, { simpa [none_eq_top] using hb }, refl end lemma of_real_add {p q : ℝ} (hp : 0 ≤ p) (hq : 0 ≤ q) : ennreal.of_real (p + q) = ennreal.of_real p + ennreal.of_real q := by rw [ennreal.of_real, ennreal.of_real, ennreal.of_real, ← coe_add, coe_eq_coe, nnreal.of_real_add hp hq] @[simp] lemma to_real_le_to_real (ha : a ≠ ⊤) (hb : b ≠ ⊤) : a.to_real ≤ b.to_real ↔ a ≤ b := begin cases a, { simpa [none_eq_top] using ha }, cases b, { simpa [none_eq_top] using hb }, simp only [ennreal.to_real, nnreal.coe_le.symm, with_top.some_le_some], refl end @[simp] lemma to_real_lt_to_real (ha : a ≠ ⊤) (hb : b ≠ ⊤) : a.to_real < b.to_real ↔ a < b := begin cases a, { simpa [none_eq_top] using ha }, cases b, { simpa [none_eq_top] using hb }, rw [with_top.some_lt_some], refl end lemma of_real_le_of_real {p q : ℝ} (h : p ≤ q) : ennreal.of_real p ≤ ennreal.of_real q := by simp [ennreal.of_real, nnreal.of_real_le_of_real h] @[simp] lemma of_real_le_of_real_iff {p q : ℝ} (h : 0 ≤ q) : ennreal.of_real p ≤ ennreal.of_real q ↔ p ≤ q := by rw [ennreal.of_real, ennreal.of_real, coe_le_coe, nnreal.of_real_le_of_real_iff h] @[simp] lemma of_real_lt_of_real_iff {p q : ℝ} (h : 0 < q) : ennreal.of_real p < ennreal.of_real q ↔ p < q := by rw [ennreal.of_real, ennreal.of_real, coe_lt_coe, nnreal.of_real_lt_of_real_iff h] @[simp] lemma of_real_pos {p : ℝ} : 0 < ennreal.of_real p ↔ 0 < p := by simp [ennreal.of_real] @[simp] lemma of_real_eq_zero {p : ℝ} : ennreal.of_real p = 0 ↔ p ≤ 0 := by simp [ennreal.of_real] lemma of_real_le_iff_le_to_real {a : ℝ} {b : ennreal} (hb : b ≠ ⊤) : ennreal.of_real a ≤ b ↔ a ≤ ennreal.to_real b := begin rcases b, { have := none_eq_top, contradiction }, { have := nnreal.of_real_le_iff_le_coe, simpa [ennreal.of_real, ennreal.to_real, some_eq_coe] } end lemma of_real_lt_iff_lt_to_real {a : ℝ} {b : ennreal} (ha : a ≥ 0) (hb : b ≠ ⊤) : ennreal.of_real a < b ↔ a < ennreal.to_real b := begin rcases b, { have := none_eq_top, contradiction }, { have := nnreal.of_real_lt_iff_lt_coe ha, simpa [ennreal.of_real, ennreal.to_real, some_eq_coe] } end lemma le_of_real_iff_to_real_le {a : ennreal} {b : ℝ} (ha : a ≠ ⊤) (hb : b ≥ 0) : a ≤ ennreal.of_real b ↔ ennreal.to_real a ≤ b := begin rcases a, { have := none_eq_top, contradiction }, { have := nnreal.le_of_real_iff_coe_le hb, simpa [ennreal.of_real, ennreal.to_real, some_eq_coe] } end lemma lt_of_real_iff_to_real_lt {a : ennreal} {b : ℝ} (ha : a ≠ ⊤) : a < ennreal.of_real b ↔ ennreal.to_real a < b := begin rcases a, { have := none_eq_top, contradiction }, { have := nnreal.lt_of_real_iff_coe_lt, simpa [ennreal.of_real, ennreal.to_real, some_eq_coe] } end lemma of_real_mul {p q : ℝ} (hp : 0 ≤ p) : ennreal.of_real (p * q) = (ennreal.of_real p) * (ennreal.of_real q) := by { simp only [ennreal.of_real, coe_mul.symm, coe_eq_coe], exact nnreal.of_real_mul hp } lemma to_real_of_real_mul (c : ℝ) (a : ennreal) (h : c ≥ 0) : ennreal.to_real ((ennreal.of_real c) * a) = c * ennreal.to_real a := begin cases a, { simp only [none_eq_top, ennreal.to_real, top_to_nnreal, nnreal.coe_zero, mul_zero, mul_top], by_cases h' : c ≤ 0, { rw [if_pos], { simp }, { convert of_real_zero, exact le_antisymm h' h } }, { rw [if_neg], refl, rw [of_real_eq_zero], assumption } }, { simp only [ennreal.to_real, ennreal.to_nnreal], simp only [some_eq_coe, ennreal.of_real, coe_mul.symm, to_nnreal_coe, nnreal.coe_mul], congr, apply nnreal.coe_of_real, exact h } end end real section infi variables {ι : Sort*} {f g : ι → ennreal} lemma infi_add : infi f + a = ⨅i, f i + a := le_antisymm (le_infi $ assume i, add_le_add' (infi_le _ _) $ le_refl _) (ennreal.sub_le_iff_le_add.1 $ le_infi $ assume i, ennreal.sub_le_iff_le_add.2 $ infi_le _ _) lemma supr_sub : (⨆i, f i) - a = (⨆i, f i - a) := le_antisymm (ennreal.sub_le_iff_le_add.2 $ supr_le $ assume i, ennreal.sub_le_iff_le_add.1 $ le_supr _ i) (supr_le $ assume i, ennreal.sub_le_sub (le_supr _ _) (le_refl a)) lemma sub_infi : a - (⨅i, f i) = (⨆i, a - f i) := begin refine (eq_of_forall_ge_iff $ λ c, _), rw [ennreal.sub_le_iff_le_add, add_comm, infi_add], simp [ennreal.sub_le_iff_le_add] end lemma Inf_add {s : set ennreal} : Inf s + a = ⨅b∈s, b + a := by simp [Inf_eq_infi, infi_add] lemma add_infi {a : ennreal} : a + infi f = ⨅b, a + f b := by rw [add_comm, infi_add]; simp lemma infi_add_infi (h : ∀i j, ∃k, f k + g k ≤ f i + g j) : infi f + infi g = (⨅a, f a + g a) := suffices (⨅a, f a + g a) ≤ infi f + infi g, from le_antisymm (le_infi $ assume a, add_le_add' (infi_le _ _) (infi_le _ _)) this, calc (⨅a, f a + g a) ≤ (⨅ a a', f a + g a') : le_infi $ assume a, le_infi $ assume a', let ⟨k, h⟩ := h a a' in infi_le_of_le k h ... ≤ infi f + infi g : by simp [add_infi, infi_add, -add_comm, -le_infi_iff]; exact le_refl _ lemma infi_sum {f : ι → α → ennreal} {s : finset α} [nonempty ι] (h : ∀(t : finset α) (i j : ι), ∃k, ∀a∈t, f k a ≤ f i a ∧ f k a ≤ f j a) : (⨅i, s.sum (f i)) = s.sum (λa, ⨅i, f i a) := finset.induction_on s (by simp) $ assume a s ha ih, have ∀ (i j : ι), ∃ (k : ι), f k a + s.sum (f k) ≤ f i a + s.sum (f j), from assume i j, let ⟨k, hk⟩ := h (insert a s) i j in ⟨k, add_le_add' (hk a (finset.mem_insert_self _ _)).left $ finset.sum_le_sum' $ assume a ha, (hk _ $ finset.mem_insert_of_mem ha).right⟩, by simp [ha, ih.symm, infi_add_infi this] end infi section supr lemma supr_coe_nat : (⨆n:ℕ, (n : ennreal)) = ⊤ := (lattice.supr_eq_top _).2 $ assume b hb, ennreal.exists_nat_gt (lt_top_iff_ne_top.1 hb) end supr end ennreal
30ad38a9166376194629bfd3a223bd58ddf6cf0a
3dd1b66af77106badae6edb1c4dea91a146ead30
/tests/lean/run/e14.lean
40bc50785685edb12379155e241c0bd740430752
[ "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
734
lean
inductive nat : Type := | zero : nat | succ : nat → nat inductive list (A : Type) : Type := | nil {} : list A | cons : A → list A → list A check nil check nil.{1} check @nil.{1} nat check @nil nat check cons zero nil inductive vector (A : Type) : nat → Type := | vnil {} : vector A zero | vcons : forall {n : nat}, A → vector A n → vector A (succ n) check vcons zero vnil variable n : nat check vcons n vnil check vector_rec definition vector_to_list {A : Type} {n : nat} (v : vector A n) : list A := vector_rec (@nil A) (fun (n : nat) (a : A) (v : vector A n) (l : list A), cons a l) v coercion vector_to_list variable f : forall {A : Type}, list A → nat check f (cons zero nil) check f (vcons zero vnil)
beaa8de4b3ca72e4acd756468f5f24c2830a35b8
556aeb81a103e9e0ac4e1fe0ce1bc6e6161c3c5e
/src/starkware/cairo/common/cairo_secp/field_spec.lean
ed82baf903beab25fa7b3d0ed20c99d897f9006c
[]
permissive
starkware-libs/formal-proofs
d6b731604461bf99e6ba820e68acca62a21709e8
f5fa4ba6a471357fd171175183203d0b437f6527
refs/heads/master
1,691,085,444,753
1,690,507,386,000
1,690,507,386,000
410,476,629
32
9
Apache-2.0
1,690,506,773,000
1,632,639,790,000
Lean
UTF-8
Lean
false
false
26,331
lean
/- Specifications file for field_spec.cairo Do not modify the constant definitions, structure definitions, or automatic specifications. Do not change the name or arguments of the user specifications and soundness theorems. You may freely move the definitions around in the file. You may add definitions and theorems wherever you wish in this file. -/ import starkware.cairo.lean.semantics.soundness.prelude import starkware.cairo.common.cairo_secp.bigint_spec import starkware.cairo.common.cairo_secp.constants_spec import starkware.cairo.common.math_spec open starkware.cairo.common.math open starkware.cairo.common.cairo_secp.bigint open starkware.cairo.common.cairo_secp.constants namespace starkware.cairo.common.cairo_secp.field variables {F : Type} [field F] [decidable_eq F] [prelude_hyps F] -- End of automatically generated prelude. /- -- Function: unreduced_mul -/ /- unreduced_mul autogenerated specification -/ -- Do not change this definition. def auto_spec_unreduced_mul (mem : F → F) (κ : ℕ) (a b : BigInt3 F) (ρ_res_low : UnreducedBigInt3 F) : Prop := 18 ≤ κ ∧ ρ_res_low = { d0 := a.d0 * b.d0 + (a.d1 * b.d2 + a.d2 * b.d1) * (4 * SECP_REM), d1 := a.d0 * b.d1 + a.d1 * b.d0 + (a.d2 * b.d2) * (4 * SECP_REM), d2 := a.d0 * b.d2 + a.d1 * b.d1 + a.d2 * b.d0 } -- You may change anything in this definition except the name and arguments. def spec_unreduced_mul (mem : F → F) (κ : ℕ) (a b : BigInt3 F) (ρ_res_low : UnreducedBigInt3 F) : Prop := ∀ x y : bigint3, a = x.toBigInt3 → b = y.toBigInt3 → ρ_res_low = (x.mul y).toUnreducedBigInt3 /- unreduced_mul soundness theorem -/ -- Do not change the statement of this theorem. You may change the proof. theorem sound_unreduced_mul {mem : F → F} (κ : ℕ) (a b : BigInt3 F) (ρ_res_low : UnreducedBigInt3 F) (h_auto : auto_spec_unreduced_mul mem κ a b ρ_res_low) : spec_unreduced_mul mem κ a b ρ_res_low := begin intros x y aeq beq, apply eq.trans h_auto.2, rw [bigint3.mul, aeq, beq], simp [bigint3.toBigInt3, bigint3.toUnreducedBigInt3] end /- -- Function: unreduced_sqr -/ /- unreduced_sqr autogenerated specification -/ -- Do not change this definition. def auto_spec_unreduced_sqr (mem : F → F) (κ : ℕ) (a : BigInt3 F) (ρ_res_low : UnreducedBigInt3 F) : Prop := ∃ twice_d0 : F, twice_d0 = a.d0 * 2 ∧ 13 ≤ κ ∧ ρ_res_low = { d0 := a.d0 * a.d0 + (a.d1 * a.d2) * (2 * 4 * SECP_REM), d1 := twice_d0 * a.d1 + (a.d2 * a.d2) * (4 * SECP_REM), d2 := twice_d0 * a.d2 + a.d1 * a.d1 } -- You may change anything in this definition except the name and arguments. def spec_unreduced_sqr (mem : F → F) (κ : ℕ) (a : BigInt3 F) (ρ_res_low : UnreducedBigInt3 F) : Prop := ∀ x : bigint3, a = x.toBigInt3 → ρ_res_low = (x.sqr).toUnreducedBigInt3 /- unreduced_sqr soundness theorem -/ -- Do not change the statement of this theorem. You may change the proof. theorem sound_unreduced_sqr {mem : F → F} (κ : ℕ) (a : BigInt3 F) (ρ_res_low : UnreducedBigInt3 F) (h_auto : auto_spec_unreduced_sqr mem κ a ρ_res_low) : spec_unreduced_sqr mem κ a ρ_res_low := begin rintros x xeq, rcases h_auto with ⟨twice_d0, rfl, _, rfl⟩, subst xeq, ext; simp [bigint3.toUnreducedBigInt3, bigint3.toBigInt3, bigint3.sqr, bigint3.mul, int.cast_mul, SECP_REM]; ring end /- -- Function: verify_zero -/ /- verify_zero autogenerated specification -/ -- Do not change this definition. def auto_spec_verify_zero (mem : F → F) (κ : ℕ) (range_check_ptr : F) (val : UnreducedBigInt3 F) (ρ_range_check_ptr : F) : Prop := ∃ q : F, ∃ q_biased : F, q_biased = q + 2 ^ 127 ∧ mem (range_check_ptr) = q_biased ∧ is_range_checked (rc_bound F) (q_biased) ∧ ∃ r1 : F, r1 = (val.d0 + q * SECP_REM) / (BASE : ℤ) ∧ mem (range_check_ptr + 1) = r1 + 2 ^ 127 ∧ is_range_checked (rc_bound F) (r1 + 2 ^ 127) ∧ ∃ r2 : F, r2 = (val.d1 + r1) / (BASE : ℤ) ∧ mem (range_check_ptr + 2) = r2 + 2 ^ 127 ∧ is_range_checked (rc_bound F) (r2 + 2 ^ 127) ∧ val.d2 = q * ((BASE : ℤ) / (4 : ℤ)) - r2 ∧ ∃ range_check_ptr₁ : F, range_check_ptr₁ = range_check_ptr + 3 ∧ 15 ≤ κ ∧ ρ_range_check_ptr = range_check_ptr₁ -- You may change anything in this definition except the name and arguments. def spec_verify_zero (mem : F → F) (κ : ℕ) (range_check_ptr : F) (val : UnreducedBigInt3 F) (ρ_range_check_ptr : F) : Prop := ∀ ix : bigint3, val = ix.toUnreducedBigInt3 → ix.bounded (2^250) → ix.val % SECP_PRIME = 0 /- verify_zero soundness theorem -/ noncomputable def rc_to_int {x : F} (h : is_range_checked (rc_bound F) (x + 2^127)) : { i : ℤ // x = i ∧ abs i ≤ 2^127 } := begin rcases classical.some_spec h with ⟨n_ltn, neq⟩, use ↑(classical.some h) - 2^127, split, { simp [neq.symm] }, rw abs_le, split, { rw le_sub, simp }, have h1 : classical.some h ≤ 2^128 := le_trans (le_of_lt n_ltn) (rc_bound_hyp F), apply le_trans (sub_le_sub_right (int.coe_nat_le.mpr h1) _), simp, norm_num end lemma BASE_calc_aux: (↑BASE : ℤ) / 4 * (↑BASE * ↑BASE) = ↑BASE^3 / 4 := begin rw [pow_succ, mul_comm _ (↑BASE^2), int.mul_div_assoc, mul_comm, pow_two], unfold BASE, norm_cast, norm_num end -- Do not change the statement of this theorem. You may change the proof. theorem sound_verify_zero {mem : F → F} (κ : ℕ) (range_check_ptr : F) (val : UnreducedBigInt3 F) (ρ_range_check_ptr : F) (h_auto : auto_spec_verify_zero mem κ range_check_ptr val ρ_range_check_ptr) : spec_verify_zero mem κ range_check_ptr val ρ_range_check_ptr := begin rintros ⟨id0, id1, id2⟩ deq abs_id_le, rcases unreduced_bigint3_eqs deq with ⟨d0eq, d1eq, d2eq⟩, rcases h_auto with ⟨q, q_biased, rfl, _, rcq, r1, r1_eq, _, rcr1, r2, r2_eq, _, rcr2, d2_eq, _, ret0_eq⟩, rcases rc_to_int rcq with ⟨iq, qeq, abs_iq_le⟩, rcases rc_to_int rcr1 with ⟨ir1, r1eq, abs_ir1_le⟩, rcases rc_to_int rcr2 with ⟨ir2, r2eq, abs_ir2_le⟩, apply int.mod_eq_zero_of_dvd, use iq, have: r1 * ↑BASE = (val.d0 + q * ↑SECP_REM), { rw [r1_eq], field_simp [BASE_ne_zero_aux'] }, have: (↑(ir1 * ↑BASE) : F) = ↑(id0 + iq * ↑SECP_REM), { simp only [qeq.symm, r1eq.symm, d0eq, this, int.cast_mul, int.cast_coe_nat, int.cast_add] }, have e1 : ir1 * ↑BASE = id0 + iq * ↑SECP_REM, { apply PRIME.int_coe_inj this, have: abs (id0 + iq * ↑SECP_REM - (ir1 * ↑BASE)) ≤ 2^250 + 2^127 * ↑SECP_REM + 2^127 * ↑BASE, { rw sub_eq_add_neg, apply le_trans (abs_add _ _), apply add_le_add, apply le_trans (abs_add _ _), rw [abs_mul, abs_of_nonneg SECP_REM_nonneg], apply add_le_add abs_id_le.1, exact mul_le_mul_of_nonneg_right abs_iq_le SECP_REM_nonneg, rw [abs_neg, abs_mul, abs_of_nonneg BASE_nonneg], exact mul_le_mul_of_nonneg_right abs_ir1_le BASE_nonneg }, apply lt_of_le_of_lt this, simp only [SECP_REM, BASE, PRIME], simp_int_casts, norm_num1 }, have: (↑(ir2 * ↑BASE) : F) = ↑(id1 + ir1), { simp [r1eq.symm, r2eq.symm, r2_eq, d1eq], field_simp [BASE_ne_zero_aux'] }, have e2 : ir2 * ↑BASE = id1 + ir1, { apply PRIME.int_coe_inj this, have: abs (id1 + ir1 - ir2 * ↑BASE) ≤ 2^250 + 2^127 + 2^127 * ↑BASE, { apply le_trans (abs_add _ _), apply add_le_add, apply le_trans (abs_add _ _), exact add_le_add abs_id_le.2.1 abs_ir1_le, rw [abs_neg, abs_mul, abs_of_nonneg BASE_nonneg], exact mul_le_mul_of_nonneg_right abs_ir2_le BASE_nonneg }, apply lt_of_le_of_lt this, simp only [BASE, PRIME], simp_int_casts, norm_num1 }, have: (4 : ℤ) ∣ ↑BASE, by { simp only [BASE], simp_int_casts, norm_num1 }, have: (↑(iq * (↑BASE / 4)) : F) = ↑(id2 + ir2), { simp [qeq.symm, r2eq.symm, d2eq.symm, d2_eq, sub_mul, mul_assoc], rw [int.cast_div], simp, norm_num1, simp [PRIME.four_ne_zero F] }, have e3 : iq * (↑BASE / 4) = id2 + ir2, { apply PRIME.int_coe_inj this, have: abs (id2 + ir2 - iq * (↑BASE / 4)) ≤ 2^250 + 2^127 + 2^127 * (↑BASE / 4), { apply le_trans (abs_add _ _), apply add_le_add, { apply le_trans (abs_add _ _), apply add_le_add abs_id_le.2.2 abs_ir2_le }, rw [abs_neg, abs_mul, abs_of_nonneg BASE_div_4_nonneg], exact mul_le_mul_of_nonneg_right abs_iq_le BASE_div_4_nonneg }, apply lt_of_le_of_lt this, simp only [BASE, PRIME], simp_int_casts, norm_num1 }, have e4 := congr_arg (λ x, x * (↑BASE * ↑BASE : int)) e3, dsimp at e4, have: (↑BASE : ℤ) / 4 * (↑BASE * ↑BASE) = ↑BASE^3 / 4, { rw [pow_succ, mul_comm _ (↑BASE^2), int.mul_div_assoc, mul_comm, pow_two], unfold BASE, norm_cast, norm_num}, rw [add_mul, ←mul_assoc ir2, e2, add_mul, e1, mul_assoc iq, this, ←sub_eq_zero] at e4, -- show bigint_val id0 id1 id2 = ((↑BASE : ℤ)^3 / 4 - ↑SECP_REM) * iq, rw [SECP_PRIME_eq, bigint3.val, ←sub_eq_zero, ←neg_eq_zero, ←e4, pow_two], ring end /- -- Function: is_zero -/ /- is_zero autogenerated specification -/ -- Do not change this definition. def auto_spec_is_zero (mem : F → F) (κ : ℕ) (range_check_ptr : F) (x : BigInt3 F) (ρ_range_check_ptr ρ_res : F) : Prop := ∃ ιχ__temp40 : F, ((ιχ__temp40 ≠ 0 ∧ ∃ (κ₁ : ℕ) (range_check_ptr₁ : F), spec_verify_zero mem κ₁ range_check_ptr { d0 := x.d0, d1 := x.d1, d2 := x.d2 } range_check_ptr₁ ∧ κ₁ + 9 ≤ κ ∧ ρ_range_check_ptr = range_check_ptr₁ ∧ ρ_res = 1) ∨ (ιχ__temp40 = 0 ∧ ∃ (κ₁ : ℕ) (range_check_ptr₁ : F) (x_inv : BigInt3 F), spec_nondet_bigint3 mem κ₁ range_check_ptr range_check_ptr₁ x_inv ∧ ∃ (κ₂ : ℕ) (x_x_inv : UnreducedBigInt3 F), spec_unreduced_mul mem κ₂ x x_inv x_x_inv ∧ ∃ (κ₃ : ℕ) (range_check_ptr₂ : F), spec_verify_zero mem κ₃ range_check_ptr₁ { d0 := x_x_inv.d0 - 1, d1 := x_x_inv.d1, d2 := x_x_inv.d2 } range_check_ptr₂ ∧ κ₁ + κ₂ + κ₃ + 19 ≤ κ ∧ ρ_range_check_ptr = range_check_ptr₂ ∧ ρ_res = 0)) -- You may change anything in this definition except the name and arguments. def spec_is_zero (mem : F → F) (κ : ℕ) (range_check_ptr : F) (x : BigInt3 F) (ρ_range_check_ptr ρ_res : F) : Prop := ∀ ix : bigint3, x = ix.toBigInt3 → ix.bounded (2^107) → ((ix.val % SECP_PRIME = 0 ∧ ρ_res = 1) ∨ (ix.val % SECP_PRIME ≠ 0 ∧ ρ_res = 0)) /- is_zero soundness theorem -/ def bigint3.one : bigint3 := ⟨1, 0, 0⟩ theorem bigint3.one_val : bigint3.one.val = 1 := by simp [bigint3.one, bigint3.val] theorem SECP_PRIME_gt_one : 1 < (SECP_PRIME : ℤ) := by { rw [SECP_PRIME_eq, BASE, SECP_REM], simp_int_casts, norm_num1 } -- Do not change the statement of this theorem. You may change the proof. theorem sound_is_zero {mem : F → F} (κ : ℕ) (range_check_ptr : F) (x : BigInt3 F) (ρ_range_check_ptr ρ_res : F) (h_auto : auto_spec_is_zero mem κ range_check_ptr x ρ_range_check_ptr ρ_res) : spec_is_zero mem κ range_check_ptr x ρ_range_check_ptr ρ_res := begin intros ix xeq ixbdd, rcases h_auto with ⟨ιχ__temp40, ⟨ιχ__temp40_nez, case_neg⟩ | ⟨ιχ__temp32_ez, case_pos⟩⟩, { rcases case_neg with ⟨_, _, h_verify_zero, _, _, ρ_res_eq⟩, have ixbdd' : ix.bounded (2^250), { apply bigint3.bounded_of_bounded_of_le ixbdd, norm_num }, have := h_verify_zero ix (by rw xeq; refl) ixbdd', left, use [this, ρ_res_eq] }, rcases case_pos with ⟨_, _, x_inv, h_x_inv, _, x_x_inv, h_x_x_inv, _, _, h_verify_zero, _, _, ρ_res_eq⟩, rcases nondet_bigint3_corr h_x_inv with ⟨ix_inv, x_inv_eq, ix_inv_bdd⟩, have := h_x_x_inv ix ix_inv xeq x_inv_eq, have : ((ix.mul ix_inv).sub bigint3.one).toUnreducedBigInt3 = {d0 := x_x_inv.d0 - 1, d1 := x_x_inv.d1, d2 := x_x_inv.d2}, { rw this, simp [bigint3.toUnreducedBigInt3, bigint3.sub, bigint3.one], }, have := h_verify_zero _ this.symm, have := this _, rw [bigint3.sub_val, bigint3.one_val, ←int.modeq_iff_sub_mod_eq_zero] at this, have h := (bigint3.mul_val _ _).symm.trans this, right, refine ⟨_, ρ_res_eq⟩, { intro ixval_mod_eq_zero, have : ix.val * ix_inv.val ≡ 0 [ZMOD ↑SECP_PRIME], { apply int.modeq.trans, apply int.modeq.mul (int.modeq_zero_iff.mpr ixval_mod_eq_zero) int.modeq.rfl, rw zero_mul }, rw [int.modeq, int.zero_mod] at this, rw [int.modeq, this, int.mod_eq_of_lt] at h, apply zero_ne_one h, norm_num, exact SECP_PRIME_gt_one }, have : ((2^107 : ℤ)^2 * (8 * SECP_REM + 1)) + 1 ≤ 2^250, { simp, norm_num }, apply bigint3.bounded_of_bounded_of_le _ this, apply bigint3.bounded_sub, swap, { simp [bigint3.bounded, bigint3.one] }, apply bigint3.bounded_mul ixbdd, apply bigint3.bounded_of_bounded_of_le ix_inv_bdd, norm_num end /- -- Function: reduce -/ /- reduce autogenerated specification -/ -- Do not change this definition. def auto_spec_reduce (mem : F → F) (κ : ℕ) (range_check_ptr : F) (x : UnreducedBigInt3 F) (ρ_range_check_ptr : F) (ρ_reduced_x : BigInt3 F) : Prop := ∃ (κ₁ : ℕ) (range_check_ptr₁ : F) (reduced_x : BigInt3 F), spec_nondet_bigint3 mem κ₁ range_check_ptr range_check_ptr₁ reduced_x ∧ ∃ (κ₂ : ℕ) (range_check_ptr₂ : F), spec_verify_zero mem κ₂ range_check_ptr₁ { d0 := x.d0 - reduced_x.d0, d1 := x.d1 - reduced_x.d1, d2 := x.d2 - reduced_x.d2 } range_check_ptr₂ ∧ κ₁ + κ₂ + 11 ≤ κ ∧ ρ_range_check_ptr = range_check_ptr₂ ∧ ρ_reduced_x = reduced_x -- You may change anything in this definition except the name and arguments. def spec_reduce (mem : F → F) (κ : ℕ) (range_check_ptr : F) (x : UnreducedBigInt3 F) (ρ_range_check_ptr : F) (ρ_reduced_x : BigInt3 F) : Prop := ∀ ix : bigint3, -- We could get by with 2^250 - 3 * (BASE - 1). ix.bounded (2^249) → x = ix.toUnreducedBigInt3 → ∃ ix' : bigint3, ix'.bounded (3 * (BASE - 1)) ∧ ix'.val ≡ ix.val [ZMOD SECP_PRIME] ∧ ρ_reduced_x = ix'.toBigInt3 /- reduce soundness theorem -/ -- Do not change the statement of this theorem. You may change the proof. theorem sound_reduce {mem : F → F} (κ : ℕ) (range_check_ptr : F) (x : UnreducedBigInt3 F) (ρ_range_check_ptr : F) (ρ_reduced_x : BigInt3 F) (h_auto : auto_spec_reduce mem κ range_check_ptr x ρ_range_check_ptr ρ_reduced_x) : spec_reduce mem κ range_check_ptr x ρ_range_check_ptr ρ_reduced_x := begin intros ix ixbdd xeq, rcases h_auto with ⟨_, _, reduced_x, h_reduced_x, _, _, h_verify_zero, _, _, ρ_reduced_x_eq⟩, rcases nondet_bigint3_corr h_reduced_x with ⟨ix', reduced_x_eq, ix'bdd⟩, have : (ix.sub ix').val % SECP_PRIME = 0, { apply h_verify_zero, { rw [bigint3.toUnreducedBigInt3_sub, ←xeq, UnreducedBigInt3.sub, reduced_x_eq, bigint3.toUnreducedBigInt3, bigint3.toBigInt3] }, have : (2^249 : ℤ) + 3 * (↑BASE - 1) ≤ 2^250, { simp, norm_num }, apply bigint3.bounded_of_bounded_of_le _ this, exact bigint3.bounded_sub ixbdd ix'bdd }, use [ix', ix'bdd], rw [bigint3.sub_val, ←int.mod_eq_mod_iff_mod_sub_eq_zero] at this, use this.symm, exact ρ_reduced_x_eq.trans reduced_x_eq end /- -- Function: validate_reduced_field_element -/ /- validate_reduced_field_element autogenerated specification -/ -- Do not change this definition. def auto_spec_validate_reduced_field_element (mem : F → F) (κ : ℕ) (range_check_ptr : F) (val : BigInt3 F) (ρ_range_check_ptr : F) : Prop := ∃ (κ₁ : ℕ) (range_check_ptr₁ : F), spec_assert_nn_le mem κ₁ range_check_ptr val.d2 P2 range_check_ptr₁ ∧ ∃ (κ₂ : ℕ) (range_check_ptr₂ : F), spec_assert_nn_le mem κ₂ range_check_ptr₁ val.d1 (BASE - 1) range_check_ptr₂ ∧ ∃ (κ₃ : ℕ) (range_check_ptr₃ : F), spec_assert_nn_le mem κ₃ range_check_ptr₂ val.d0 (BASE - 1) range_check_ptr₃ ∧ ((val.d2 = P2 ∧ ((val.d1 = P1 ∧ ∃ (κ₄ : ℕ) (range_check_ptr₄ : F), spec_assert_nn_le mem κ₄ range_check_ptr₃ val.d0 (P0 - 1) range_check_ptr₄ ∧ κ₁ + κ₂ + κ₃ + κ₄ + 19 ≤ κ ∧ ρ_range_check_ptr = range_check_ptr₄) ∨ (val.d1 ≠ P1 ∧ ∃ (κ₄ : ℕ) (range_check_ptr₄ : F), spec_assert_nn_le mem κ₄ range_check_ptr₃ val.d1 (P1 - 1) range_check_ptr₄ ∧ κ₁ + κ₂ + κ₃ + κ₄ + 19 ≤ κ ∧ ρ_range_check_ptr = range_check_ptr₄))) ∨ (val.d2 ≠ P2 ∧ κ₁ + κ₂ + κ₃ + 14 ≤ κ ∧ ρ_range_check_ptr = range_check_ptr₃)) -- You may change anything in this definition except the name and arguments. def spec_validate_reduced_field_element (mem : F → F) (κ : ℕ) (range_check_ptr : F) (val : BigInt3 F) (ρ_range_check_ptr : F) : Prop := ∃ n0 n1 n2 : ℕ, n0 ≤ BASE - 1 ∧ n1 ≤ BASE - 1 ∧ n2 ≤ P2 ∧ n2 * BASE^2 + n1 * BASE + n0 < SECP_PRIME ∧ val = ⟨n0, n1, n2⟩ /- validate_reduced_field_element soundness theorem -/ theorem P0_pos : 0 < P0 := by { rw P0, norm_num } theorem P1_pos : 0 < P1 := by { rw P1, norm_num } theorem P2_pos : 0 < P2 := by { rw P2, norm_num } theorem BASE_pos : 0 < BASE := by { rw BASE, norm_num } theorem coe_P0m1_eq : ((↑P0 : F) - 1 = ↑(P0 - 1)) := begin rw [nat.cast_sub, nat.cast_one], exact P0_pos, end theorem coe_P1m1_eq : ((↑P1 : F) - 1 = ↑(P1 - 1)) := begin rw [nat.cast_sub, nat.cast_one], exact P1_pos, end theorem coe_BASEm1_eq : ((↑BASE : F) - 1 = ↑(BASE - 1)) := begin rw [nat.cast_sub, nat.cast_one], exact BASE_pos, end theorem aux0 {m p : ℕ} (hm : m < rc_bound F) (hp : p < PRIME) (h : (m : F) = p) : m = p := begin apply @PRIME.nat_coe_field_inj F _ _ _ _ _ hp h, apply lt_of_lt_of_le hm, apply le_trans (rc_bound_hyp F), rw PRIME, norm_num end theorem aux1 {m n p : ℕ} (hm : m < rc_bound F) (hn : n < rc_bound F) (hp : p < PRIME) (h : (↑(m + n) : F) = p) : m ≤ p := begin have : m + n = p, { apply @PRIME.nat_coe_field_inj F _ _ _ _ _ hp h, apply lt_of_lt_of_le (add_lt_add hm hn), apply le_trans (add_le_add (rc_bound_hyp F) (rc_bound_hyp F)), rw PRIME, norm_num }, rw ←this, apply nat.le_add_right end -- Do not change the statement of this theorem. You may change the proof. theorem sound_validate_reduced_field_element {mem : F → F} (κ : ℕ) (range_check_ptr : F) (val : BigInt3 F) (ρ_range_check_ptr : F) (h_auto : auto_spec_validate_reduced_field_element mem κ range_check_ptr val ρ_range_check_ptr) : spec_validate_reduced_field_element mem κ range_check_ptr val ρ_range_check_ptr := begin rcases h_auto with ⟨ _, _, ⟨m2, n2, m2lt, n2lt, vald2eq, P2eq⟩, _, _, ⟨m1, n1, m1lt, n1lt, vald1eq, m1n1eq⟩, _, _, ⟨m0, n0, m0lt, n0lt, vald0eq, m0n0eq⟩, hcases⟩, have m2leP2 : m2 ≤ P2, { apply aux1 m2lt n2lt _ P2eq.symm, rw PRIME, norm_num }, have m1leBASEm1 : m1 ≤ BASE - 1, { rw coe_BASEm1_eq at m1n1eq, apply aux1 m1lt n1lt _ m1n1eq.symm, rw [PRIME, BASE], norm_num }, have m0leBASEm1 : m0 ≤ BASE - 1, { rw coe_BASEm1_eq at m0n0eq, apply aux1 m0lt n0lt _ m0n0eq.symm, rw [PRIME, BASE], norm_num }, rcases hcases with ⟨d2eq, hcases⟩ | ⟨d2ne, _, _⟩, { have m2eqP2: m2 = P2, { apply aux0 m2lt _ (vald2eq.symm.trans d2eq), rw [PRIME, P2], norm_num }, rcases hcases with ⟨d1eq, _, _, ⟨m0', n0', m0'lt, n0'lt, vald0eq', P0m1eq⟩, _, _⟩ | ⟨d1ne, _, _, ⟨m1', n1', m1'lt, n1'lt, vald1eq', P1m1eq⟩, _, _⟩, { have m1eqP1: m1 = P1, { apply aux0 m1lt _ (vald1eq.symm.trans d1eq), rw [PRIME, P1], norm_num }, have m0'leP0m1 : m0' ≤ P0 - 1, { rw coe_P0m1_eq at P0m1eq, apply aux1 m0'lt n0'lt _ P0m1eq.symm, rw [PRIME, P0], norm_num }, use [m0', m1, m2], split, { apply le_trans m0'leP0m1, rw [BASE, P0], norm_num }, split, { exact m1leBASEm1 }, split, { rw m2eqP2 }, split, { apply lt_of_le_of_lt, apply add_le_add_left m0'leP0m1, rw [m2eqP2, P2, m1eqP1, P1, P0, SECP_PRIME, BASE, SECP_REM], norm_num }, rw BigInt3.ext_iff, use [vald0eq', vald1eq, vald2eq] }, have m1'leP1m1 : m1' ≤ P1 - 1, { rw coe_P1m1_eq at P1m1eq, apply aux1 m1'lt n1'lt _ P1m1eq.symm, rw [PRIME, P1], norm_num }, use [m0, m1', m2], split, use [m0leBASEm1], split, { apply le_trans m1'leP1m1, rw [BASE, P1], norm_num }, split, { exact m2leP2 }, split, { apply lt_of_le_of_lt, apply add_le_add, apply add_le_add_left, apply nat.mul_le_mul_right _ m1'leP1m1, exact m0leBASEm1, rw [m2eqP2, SECP_PRIME, SECP_REM, BASE, P2, P1], norm_num }, rw BigInt3.ext_iff, use [vald0eq, vald1eq', vald2eq] }, have : m2 ≠ P2, { intro h, apply d2ne, rw [vald2eq, h] }, have m2leP2m1 : m2 ≤ P2 - 1, { apply nat.le_pred_of_lt, apply lt_of_le_of_ne m2leP2 this }, use [m0, m1, m2, m0leBASEm1, m1leBASEm1, m2leP2], split, { apply lt_of_le_of_lt, apply add_le_add, apply add_le_add, apply nat.mul_le_mul_right _ m2leP2m1, apply nat.mul_le_mul_right _ m1leBASEm1, apply m0leBASEm1, rw [SECP_PRIME, SECP_REM, BASE, P2], norm_num }, rw BigInt3.ext_iff, use [vald0eq, vald1eq, vald2eq] end /- def spec_validate_reduced_field_element' (mem : F → F) (κ : ℕ) (range_check_ptr : F) (val : BigInt3 F) (ρ_range_check_ptr : F) : Prop := ∃ ival : bigint3, ival.bounded ↑(BASE - 1) ∧ ival.val < SECP_PRIME ∧ val = ival.toBigInt3 theorem sound_validate_reduced_field_element' {mem : F → F} (κ : ℕ) (range_check_ptr : F) (val : BigInt3 F) (ρ_range_check_ptr : F) (h_auto : auto_spec_validate_reduced_field_element mem κ range_check_ptr val ρ_range_check_ptr) : spec_validate_reduced_field_element mem κ range_check_ptr val ρ_range_check_ptr := begin rcases h_auto with ⟨ _, _, ⟨m2, n2, m2lt, n2lt, vald2eq, P2eq⟩, _, _, ⟨m1, n1, m1lt, n1lt, vald1eq, m1n1eq⟩, _, _, ⟨m0, n0, m0lt, n0lt, vald0eq, m0n0eq⟩, hcases⟩, have m2leP2 : m2 ≤ P2, { apply aux1 m2lt n2lt _ P2eq.symm, rw PRIME, norm_num }, have m1leBASEm1 : m1 ≤ BASE - 1, { rw coe_BASEm1_eq at m1n1eq, apply aux1 m1lt n1lt _ m1n1eq.symm, rw [PRIME, BASE], norm_num }, have m0leBASEm1 : m0 ≤ BASE - 1, { rw coe_BASEm1_eq at m0n0eq, apply aux1 m0lt n0lt _ m0n0eq.symm, rw [PRIME, BASE], norm_num }, rcases hcases with ⟨d2eq, hcases⟩ | ⟨d2ne, _, _⟩, { have m2eqP2: m2 = P2, { apply aux0 m2lt _ (vald2eq.symm.trans d2eq), rw [PRIME, P2], norm_num }, rcases hcases with ⟨d1eq, _, _, ⟨m0', n0', m0'lt, n0'lt, vald0eq', P0m1eq⟩, _, _⟩ | ⟨d1ne, _, _, ⟨m1', n1', m1'lt, n1'lt, vald1eq', P1m1eq⟩, _, _⟩, { have m1eqP1: m1 = P1, { apply aux0 m1lt _ (vald1eq.symm.trans d1eq), rw [PRIME, P1], norm_num }, have m0'leP0m1 : m0' ≤ P0 - 1, { rw coe_P0m1_eq at P0m1eq, apply aux1 m0'lt n0'lt _ P0m1eq.symm, rw [PRIME, P0], norm_num }, use ⟨m0', m1, m2⟩, split, { simp only [bigint3.bounded, int.coe_nat_le, int.coe_nat_abs], split, { apply le_trans m0'leP0m1, rw [BASE, P0], norm_num }, split, { exact m1leBASEm1 }, rw [m2eqP2, BASE, P2], norm_num }, split, { have : (m0' : ℤ) ≤ ↑(P0 - 1), { rw [int.coe_nat_le], exact m0'leP0m1 }, rw [bigint3.val, m2eqP2, m1eqP1], dsimp, refine lt_of_le_of_lt (add_le_add_left this _) _, rw [P2, P1, P0, SECP_PRIME_eq, BASE, SECP_REM], simp_int_casts, norm_num }, rw [bigint3.toBigInt3, BigInt3.ext_iff], dsimp, rw [vald0eq', d2eq, d1eq, m1eqP1, m2eqP2], exact ⟨rfl, rfl, rfl⟩ }, have m1'leP1m1 : m1' ≤ P1 - 1, { rw coe_P1m1_eq at P1m1eq, apply aux1 m1'lt n1'lt _ P1m1eq.symm, rw [PRIME, P1], norm_num }, use ⟨m0, m1', m2⟩, split, { simp only [bigint3.bounded, int.coe_nat_le, int.coe_nat_abs], use [m0leBASEm1], split, { apply le_trans m1'leP1m1, rw [BASE, P1], norm_num }, rw [m2eqP2, BASE, P2], norm_num }, split, { have h1: (m1' : ℤ) * BASE ≤ ↑(P1 - 1) * BASE, { apply mul_le_mul_of_nonneg_right _ (int.coe_nat_nonneg _), rw [int.coe_nat_le], exact m1'leP1m1 }, have h2 : (m0 : ℤ) ≤ ↑(BASE - 1), { rw [int.coe_nat_le], exact m0leBASEm1 }, rw [bigint3.val, m2eqP2], dsimp, apply lt_of_le_of_lt, apply add_le_add (add_le_add_left h1 _) h2, rw [P2, P1, SECP_PRIME_eq, BASE, SECP_REM], simp_int_casts, norm_num }, rw [bigint3.toBigInt3, BigInt3.ext_iff], dsimp, rw [vald0eq, vald1eq', d2eq, m2eqP2], exact ⟨rfl, rfl, rfl⟩ }, have : m2 ≠ P2, { intro h, apply d2ne, rw [vald2eq, h] }, have m2leP2m1 : m2 ≤ P2 - 1, { apply nat.le_pred_of_lt, apply lt_of_le_of_ne m2leP2 this }, use ⟨m0, m1, m2⟩, split, { simp only [bigint3.bounded, int.coe_nat_le, int.coe_nat_abs], use [m0leBASEm1, m1leBASEm1], apply le_trans m2leP2m1, rw [BASE, P2], norm_num }, split, { rw bigint3.val, dsimp, have h0: (m2 : ℤ) * BASE^2 ≤ ↑(P2 - 1) * BASE^2, { apply mul_le_mul_of_nonneg_right _ (int.coe_nat_nonneg _), rw [int.coe_nat_le], exact m2leP2m1 }, have h1: (m1 : ℤ) * BASE ≤ ↑(BASE - 1) * BASE, { apply mul_le_mul_of_nonneg_right _ (int.coe_nat_nonneg _), rw [int.coe_nat_le], exact m1leBASEm1 }, have h2 : (m0 : ℤ) ≤ ↑(BASE - 1), { rw [int.coe_nat_le], exact m0leBASEm1 }, apply lt_of_le_of_lt, apply add_le_add (add_le_add h0 h1) h2, rw [int.coe_nat_sub, int.coe_nat_sub], rw [P2, SECP_PRIME_eq, BASE, SECP_REM], simp_int_casts, repeat { norm_num [BASE] } }, rw [bigint3.toBigInt3, BigInt3.ext_iff], dsimp, use [vald0eq, vald1eq, vald2eq] end -/ end starkware.cairo.common.cairo_secp.field
f1e3108b1f7d7398314acf2a395a5de888379af2
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/ring_theory/perfection.lean
f229901045627f90e1f400bc86f6afb9c9376a6c
[ "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
25,364
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.char_p import algebra.ring.pi import analysis.special_functions.pow import field_theory.perfect_closure import ring_theory.localization import ring_theory.subring import ring_theory.valuation.integers /-! # Ring Perfection and Tilt In this file we define the perfection of a ring of characteristic p, and the tilt of a field given a valuation to `ℝ≥0`. ## TODO Define the valuation on the tilt, and define a characteristic predicate for the tilt. -/ universes u₁ u₂ u₃ u₄ open_locale nnreal /-- The perfection of a monoid `M`, defined to be the projective limit of `M` using the `p`-th power maps `M → M` indexed by the natural numbers, implemented as `{ f : ℕ → M | ∀ n, f (n + 1) ^ p = f n }`. -/ def monoid.perfection (M : Type u₁) [comm_monoid M] (p : ℕ) : submonoid (ℕ → M) := { carrier := { f | ∀ n, f (n + 1) ^ p = f n }, one_mem' := λ n, one_pow _, mul_mem' := λ f g hf hg n, (mul_pow _ _ _).trans $ congr_arg2 _ (hf n) (hg n) } /-- The perfection of a ring `R` with characteristic `p`, as a subsemiring, defined to be the projective limit of `R` using the Frobenius maps `R → R` indexed by the natural numbers, implemented as `{ f : ℕ → R | ∀ n, f (n + 1) ^ p = f n }`. -/ def ring.perfection_subsemiring (R : Type u₁) [comm_semiring R] (p : ℕ) [hp : fact p.prime] [char_p R p] : subsemiring (ℕ → R) := { zero_mem' := λ n, zero_pow $ hp.1.pos, add_mem' := λ f g hf hg n, (frobenius_add R p _ _).trans $ congr_arg2 _ (hf n) (hg n), .. monoid.perfection R p } /-- The perfection of a ring `R` with characteristic `p`, as a subring, defined to be the projective limit of `R` using the Frobenius maps `R → R` indexed by the natural numbers, implemented as `{ f : ℕ → R | ∀ n, f (n + 1) ^ p = f n }`. -/ def ring.perfection_subring (R : Type u₁) [comm_ring R] (p : ℕ) [hp : fact p.prime] [char_p R p] : subring (ℕ → R) := (ring.perfection_subsemiring R p).to_subring $ λ n, by simp_rw [← frobenius_def, pi.neg_apply, pi.one_apply, ring_hom.map_neg, ring_hom.map_one] /-- The perfection of a ring `R` with characteristic `p`, defined to be the projective limit of `R` using the Frobenius maps `R → R` indexed by the natural numbers, implemented as `{f : ℕ → R // ∀ n, f (n + 1) ^ p = f n}`. -/ def ring.perfection (R : Type u₁) [comm_semiring R] (p : ℕ) : Type u₁ := {f // ∀ (n : ℕ), (f : ℕ → R) (n + 1) ^ p = f n} namespace perfection variables (R : Type u₁) [comm_semiring R] (p : ℕ) [hp : fact p.prime] [char_p R p] include hp instance : comm_semiring (ring.perfection R p) := (ring.perfection_subsemiring R p).to_comm_semiring instance : char_p (ring.perfection R p) p := char_p.subsemiring (ℕ → R) p (ring.perfection_subsemiring R p) instance ring (R : Type u₁) [comm_ring R] [char_p R p] : ring (ring.perfection R p) := (ring.perfection_subring R p).to_ring instance comm_ring (R : Type u₁) [comm_ring R] [char_p R p] : comm_ring (ring.perfection R p) := (ring.perfection_subring R p).to_comm_ring instance : inhabited (ring.perfection R p) := ⟨0⟩ /-- The `n`-th coefficient of an element of the perfection. -/ def coeff (n : ℕ) : ring.perfection R p →+* R := { to_fun := λ f, f.1 n, map_one' := rfl, map_mul' := λ f g, rfl, map_zero' := rfl, map_add' := λ f g, rfl } variables {R p} @[ext] lemma ext {f g : ring.perfection R p} (h : ∀ n, coeff R p n f = coeff R p n g) : f = g := subtype.eq $ funext h variables (R p) /-- The `p`-th root of an element of the perfection. -/ def pth_root : ring.perfection R p →+* ring.perfection R p := { to_fun := λ f, ⟨λ n, coeff R p (n + 1) f, λ n, f.2 _⟩, map_one' := rfl, map_mul' := λ f g, rfl, map_zero' := rfl, map_add' := λ f g, rfl } variables {R p} @[simp] lemma coeff_mk (f : ℕ → R) (hf) (n : ℕ) : coeff R p n ⟨f, hf⟩ = f n := rfl lemma coeff_pth_root (f : ring.perfection R p) (n : ℕ) : coeff R p n (pth_root R p f) = coeff R p (n + 1) f := rfl lemma coeff_pow_p (f : ring.perfection R p) (n : ℕ) : coeff R p (n + 1) (f ^ p) = coeff R p n f := by { rw ring_hom.map_pow, exact f.2 n } lemma coeff_pow_p' (f : ring.perfection R p) (n : ℕ) : coeff R p (n + 1) f ^ p = coeff R p n f := f.2 n lemma coeff_frobenius (f : ring.perfection R p) (n : ℕ) : coeff R p (n + 1) (frobenius _ p f) = coeff R p n f := by apply coeff_pow_p f n -- `coeff_pow_p f n` also works but is slow! lemma coeff_iterate_frobenius (f : ring.perfection R p) (n m : ℕ) : coeff R p (n + m) (frobenius _ p ^[m] f) = coeff R p n f := nat.rec_on m rfl $ λ m ih, by erw [function.iterate_succ_apply', coeff_frobenius, ih] lemma coeff_iterate_frobenius' (f : ring.perfection R p) (n m : ℕ) (hmn : m ≤ n) : coeff R p n (frobenius _ p ^[m] f) = coeff R p (n - m) f := eq.symm $ (coeff_iterate_frobenius _ _ m).symm.trans $ (nat.sub_add_cancel hmn).symm ▸ rfl lemma pth_root_frobenius : (pth_root R p).comp (frobenius _ p) = ring_hom.id _ := ring_hom.ext $ λ x, ext $ λ n, by rw [ring_hom.comp_apply, ring_hom.id_apply, coeff_pth_root, coeff_frobenius] lemma frobenius_pth_root : (frobenius _ p).comp (pth_root R p) = ring_hom.id _ := ring_hom.ext $ λ x, ext $ λ n, by rw [ring_hom.comp_apply, ring_hom.id_apply, ring_hom.map_frobenius, coeff_pth_root, ← ring_hom.map_frobenius, coeff_frobenius] lemma coeff_add_ne_zero {f : ring.perfection R p} {n : ℕ} (hfn : coeff R p n f ≠ 0) (k : ℕ) : coeff R p (n + k) f ≠ 0 := nat.rec_on k hfn $ λ k ih h, ih $ by erw [← coeff_pow_p, ring_hom.map_pow, h, zero_pow hp.1.pos] lemma coeff_ne_zero_of_le {f : ring.perfection R p} {m n : ℕ} (hfm : coeff R p m f ≠ 0) (hmn : m ≤ n) : coeff R p n f ≠ 0 := let ⟨k, hk⟩ := nat.exists_eq_add_of_le hmn in hk.symm ▸ coeff_add_ne_zero hfm k variables (R p) instance perfect_ring : perfect_ring (ring.perfection R p) p := { pth_root' := pth_root R p, frobenius_pth_root' := congr_fun $ congr_arg ring_hom.to_fun $ @frobenius_pth_root R _ p _ _, pth_root_frobenius' := congr_fun $ congr_arg ring_hom.to_fun $ @pth_root_frobenius R _ p _ _ } /-- Given rings `R` and `S` of characteristic `p`, with `R` being perfect, any homomorphism `R →+* S` can be lifted to a homomorphism `R →+* perfection S p`. -/ @[simps] def lift (R : Type u₁) [comm_semiring R] [char_p R p] [perfect_ring R p] (S : Type u₂) [comm_semiring S] [char_p S p] : (R →+* S) ≃ (R →+* ring.perfection S p) := { to_fun := λ f, { to_fun := λ r, ⟨λ n, f $ _root_.pth_root R p ^[n] r, λ n, by rw [← f.map_pow, function.iterate_succ_apply', pth_root_pow_p]⟩, map_one' := ext $ λ n, (congr_arg f $ ring_hom.iterate_map_one _ _).trans f.map_one, map_mul' := λ x y, ext $ λ n, (congr_arg f $ ring_hom.iterate_map_mul _ _ _ _).trans $ f.map_mul _ _, map_zero' := ext $ λ n, (congr_arg f $ ring_hom.iterate_map_zero _ _).trans f.map_zero, map_add' := λ x y, ext $ λ n, (congr_arg f $ ring_hom.iterate_map_add _ _ _ _).trans $ f.map_add _ _ }, inv_fun := ring_hom.comp $ coeff S p 0, left_inv := λ f, ring_hom.ext $ λ r, rfl, right_inv := λ f, ring_hom.ext $ λ r, ext $ λ n, show coeff S p 0 (f (_root_.pth_root R p ^[n] r)) = coeff S p n (f r), by rw [← coeff_iterate_frobenius _ 0 n, zero_add, ← ring_hom.map_iterate_frobenius, right_inverse_pth_root_frobenius.iterate] } lemma hom_ext {R : Type u₁} [comm_semiring R] [char_p R p] [perfect_ring R p] {S : Type u₂} [comm_semiring S] [char_p S p] {f g : R →+* ring.perfection S p} (hfg : ∀ x, coeff S p 0 (f x) = coeff S p 0 (g x)) : f = g := (lift p R S).symm.injective $ ring_hom.ext hfg variables {R} {S : Type u₂} [comm_semiring S] [char_p S p] /-- A ring homomorphism `R →+* S` induces `perfection R p →+* perfection S p` -/ @[simps] def map (φ : R →+* S) : ring.perfection R p →+* ring.perfection S p := { to_fun := λ f, ⟨λ n, φ (coeff R p n f), λ n, by rw [← φ.map_pow, coeff_pow_p']⟩, map_one' := subtype.eq $ funext $ λ n, φ.map_one, map_mul' := λ f g, subtype.eq $ funext $ λ n, φ.map_mul _ _, map_zero' := subtype.eq $ funext $ λ n, φ.map_zero, map_add' := λ f g, subtype.eq $ funext $ λ n, φ.map_add _ _ } lemma coeff_map (φ : R →+* S) (f : ring.perfection R p) (n : ℕ) : coeff S p n (map p φ f) = φ (coeff R p n f) := rfl end perfection /-- A perfection map to a ring of characteristic `p` is a map that is isomorphic to its perfection. -/ @[nolint has_inhabited_instance] structure perfection_map (p : ℕ) [fact p.prime] {R : Type u₁} [comm_semiring R] [char_p R p] {P : Type u₂} [comm_semiring P] [char_p P p] [perfect_ring P p] (π : P →+* R) : Prop := (injective : ∀ ⦃x y : P⦄, (∀ n, π (pth_root P p ^[n] x) = π (pth_root P p ^[n] y)) → x = y) (surjective : ∀ f : ℕ → R, (∀ n, f (n + 1) ^ p = f n) → ∃ x : P, ∀ n, π (pth_root P p ^[n] x) = f n) namespace perfection_map variables {p : ℕ} [fact p.prime] variables {R : Type u₁} [comm_semiring R] [char_p R p] variables {P : Type u₃} [comm_semiring P] [char_p P p] [perfect_ring P p] /-- Create a `perfection_map` from an isomorphism to the perfection. -/ @[simps] lemma mk' {f : P →+* R} (g : P ≃+* ring.perfection R p) (hfg : perfection.lift p P R f = g) : perfection_map p f := { injective := λ x y hxy, g.injective $ (ring_hom.ext_iff.1 hfg x).symm.trans $ eq.symm $ (ring_hom.ext_iff.1 hfg y).symm.trans $ perfection.ext $ λ n, (hxy n).symm, surjective := λ y hy, let ⟨x, hx⟩ := g.surjective ⟨y, hy⟩ in ⟨x, λ n, show perfection.coeff R p n (perfection.lift p P R f x) = perfection.coeff R p n ⟨y, hy⟩, by rw [hfg, ← coe_fn_coe_base, hx]⟩ } variables (p R P) /-- The canonical perfection map from the perfection of a ring. -/ lemma of : perfection_map p (perfection.coeff R p 0) := mk' (ring_equiv.refl _) $ (equiv.apply_eq_iff_eq_symm_apply _).2 rfl /-- For a perfect ring, it itself is the perfection. -/ lemma id [perfect_ring R p] : perfection_map p (ring_hom.id R) := { injective := λ x y hxy, hxy 0, surjective := λ f hf, ⟨f 0, λ n, show pth_root R p ^[n] (f 0) = f n, from nat.rec_on n rfl $ λ n ih, injective_pow_p p $ by rw [function.iterate_succ_apply', pth_root_pow_p _, ih, hf]⟩ } variables {p R P} /-- A perfection map induces an isomorphism to the prefection. -/ noncomputable def equiv {π : P →+* R} (m : perfection_map p π) : P ≃+* ring.perfection R p := ring_equiv.of_bijective (perfection.lift p P R π) ⟨λ x y hxy, m.injective $ λ n, (congr_arg (perfection.coeff R p n) hxy : _), λ f, let ⟨x, hx⟩ := m.surjective f.1 f.2 in ⟨x, perfection.ext $ hx⟩⟩ lemma equiv_apply {π : P →+* R} (m : perfection_map p π) (x : P) : m.equiv x = perfection.lift p P R π x := rfl lemma comp_equiv {π : P →+* R} (m : perfection_map p π) (x : P) : perfection.coeff R p 0 (m.equiv x) = π x := rfl lemma comp_equiv' {π : P →+* R} (m : perfection_map p π) : (perfection.coeff R p 0).comp ↑m.equiv = π := ring_hom.ext $ λ x, rfl lemma comp_symm_equiv {π : P →+* R} (m : perfection_map p π) (f : ring.perfection R p) : π (m.equiv.symm f) = perfection.coeff R p 0 f := (m.comp_equiv _).symm.trans $ congr_arg _ $ m.equiv.apply_symm_apply f lemma comp_symm_equiv' {π : P →+* R} (m : perfection_map p π) : π.comp ↑m.equiv.symm = perfection.coeff R p 0 := ring_hom.ext m.comp_symm_equiv variables (p R P) /-- Given rings `R` and `S` of characteristic `p`, with `R` being perfect, any homomorphism `R →+* S` can be lifted to a homomorphism `R →+* P`, where `P` is any perfection of `S`. -/ @[simps] noncomputable def lift [perfect_ring R p] (S : Type u₂) [comm_semiring S] [char_p S p] (P : Type u₃) [comm_semiring P] [char_p P p] [perfect_ring P p] (π : P →+* S) (m : perfection_map p π) : (R →+* S) ≃ (R →+* P) := { to_fun := λ f, ring_hom.comp ↑m.equiv.symm $ perfection.lift p R S f, inv_fun := λ f, π.comp f, left_inv := λ f, by { simp_rw [← ring_hom.comp_assoc, comp_symm_equiv'], exact (perfection.lift p R S).symm_apply_apply f }, right_inv := λ f, ring_hom.ext $ λ x, m.equiv.injective $ (m.equiv.apply_symm_apply _).trans $ show perfection.lift p R S (π.comp f) x = ring_hom.comp ↑m.equiv f x, from ring_hom.ext_iff.1 ((perfection.lift p R S).apply_eq_iff_eq_symm_apply.2 rfl) _ } variables {R p} lemma hom_ext [perfect_ring R p] {S : Type u₂} [comm_semiring S] [char_p S p] {P : Type u₃} [comm_semiring P] [char_p P p] [perfect_ring P p] (π : P →+* S) (m : perfection_map p π) {f g : R →+* P} (hfg : ∀ x, π (f x) = π (g x)) : f = g := (lift p R S P π m).symm.injective $ ring_hom.ext hfg variables {R P} (p) {S : Type u₂} [comm_semiring S] [char_p S p] variables {Q : Type u₄} [comm_semiring Q] [char_p Q p] [perfect_ring Q p] /-- A ring homomorphism `R →+* S` induces `P →+* Q`, a map of the respective perfections. -/ @[nolint unused_arguments] noncomputable def map {π : P →+* R} (m : perfection_map p π) {σ : Q →+* S} (n : perfection_map p σ) (φ : R →+* S) : P →+* Q := lift p P S Q σ n $ φ.comp π lemma comp_map {π : P →+* R} (m : perfection_map p π) {σ : Q →+* S} (n : perfection_map p σ) (φ : R →+* S) : σ.comp (map p m n φ) = φ.comp π := (lift p P S Q σ n).symm_apply_apply _ lemma map_map {π : P →+* R} (m : perfection_map p π) {σ : Q →+* S} (n : perfection_map p σ) (φ : R →+* S) (x : P) : σ (map p m n φ x) = φ (π x) := ring_hom.ext_iff.1 (comp_map p m n φ) x -- Why is this slow? lemma map_eq_map (φ : R →+* S) : @map p _ R _ _ _ _ _ _ S _ _ _ _ _ _ _ (of p R) _ (of p S) φ = perfection.map p φ := hom_ext _ (of p S) $ λ f, by rw [map_map, perfection.coeff_map] end perfection_map section perfectoid variables (K : Type u₁) [field K] (v : valuation K ℝ≥0) variables (O : Type u₂) [comm_ring O] [algebra O K] (hv : v.integers O) variables (p : ℕ) include hv /-- `O/(p)` for `O`, ring of integers of `K`. -/ @[nolint unused_arguments has_inhabited_instance] def mod_p := (ideal.span {p} : ideal O).quotient variables [hp : fact p.prime] [hvp : fact (v p ≠ 1)] namespace mod_p instance : comm_ring (mod_p K v O hv p) := ideal.quotient.comm_ring _ include hp hvp instance : char_p (mod_p K v O hv p) p := char_p.quotient O p $ mt hv.one_of_is_unit $ ((algebra_map O K).map_nat_cast p).symm ▸ hvp.1 instance : nontrivial (mod_p K v O hv p) := char_p.nontrivial_of_char_ne_one hp.1.ne_one section classical local attribute [instance] classical.dec omit hp hvp /-- For a field `K` with valuation `v : K → ℝ≥0` and ring of integers `O`, a function `O/(p) → ℝ≥0` that sends `0` to `0` and `x + (p)` to `v(x)` as long as `x ∉ (p)`. -/ noncomputable def pre_val (x : mod_p K v O hv p) : ℝ≥0 := if x = 0 then 0 else v (algebra_map O K x.out') variables {K v O hv p} lemma pre_val_mk {x : O} (hx : (ideal.quotient.mk _ x : mod_p K v O hv p) ≠ 0) : pre_val K v O hv p (ideal.quotient.mk _ x) = v (algebra_map O K x) := begin obtain ⟨r, hr⟩ := ideal.mem_span_singleton'.1 (ideal.quotient.eq.1 $ quotient.sound' $ @quotient.mk_out' O (ideal.span {p} : ideal O).quotient_rel x), refine (if_neg hx).trans (v.map_eq_of_sub_lt $ lt_of_not_ge' _), erw [← ring_hom.map_sub, ← hr, hv.le_iff_dvd], exact λ hprx, hx (ideal.quotient.eq_zero_iff_mem.2 $ ideal.mem_span_singleton.2 $ dvd_of_mul_left_dvd hprx), end lemma pre_val_zero : pre_val K v O hv p 0 = 0 := if_pos rfl lemma pre_val_mul {x y : mod_p K v O hv p} (hxy0 : x * y ≠ 0) : pre_val K v O hv p (x * y) = pre_val K v O hv p x * pre_val K v O hv p y := begin have hx0 : x ≠ 0 := mt (by { rintro rfl, rw zero_mul }) hxy0, have hy0 : y ≠ 0 := mt (by { rintro rfl, rw mul_zero }) hxy0, obtain ⟨r, rfl⟩ := ideal.quotient.mk_surjective x, obtain ⟨s, rfl⟩ := ideal.quotient.mk_surjective y, rw ← ring_hom.map_mul at hxy0 ⊢, rw [pre_val_mk hx0, pre_val_mk hy0, pre_val_mk hxy0, ring_hom.map_mul, v.map_mul] end lemma pre_val_add (x y : mod_p K v O hv p) : pre_val K v O hv p (x + y) ≤ max (pre_val K v O hv p x) (pre_val K v O hv p y) := begin by_cases hx0 : x = 0, { rw [hx0, zero_add], exact le_max_right _ _ }, by_cases hy0 : y = 0, { rw [hy0, add_zero], exact le_max_left _ _ }, by_cases hxy0 : x + y = 0, { rw [hxy0, pre_val_zero], exact zero_le _ }, obtain ⟨r, rfl⟩ := ideal.quotient.mk_surjective x, obtain ⟨s, rfl⟩ := ideal.quotient.mk_surjective y, rw ← ring_hom.map_add at hxy0 ⊢, rw [pre_val_mk hx0, pre_val_mk hy0, pre_val_mk hxy0, ring_hom.map_add], exact v.map_add _ _ end lemma v_p_lt_pre_val {x : mod_p K v O hv p} : v p < pre_val K v O hv p x ↔ x ≠ 0 := begin refine ⟨λ h hx, by { rw [hx, pre_val_zero] at h, exact not_lt_zero' h }, λ h, lt_of_not_ge' $ λ hp, h _⟩, obtain ⟨r, rfl⟩ := ideal.quotient.mk_surjective x, rw [pre_val_mk h, ← (algebra_map O K).map_nat_cast p, hv.le_iff_dvd] at hp, rw [ideal.quotient.eq_zero_iff_mem, ideal.mem_span_singleton], exact hp end lemma pre_val_eq_zero {x : mod_p K v O hv p} : pre_val K v O hv p x = 0 ↔ x = 0 := ⟨λ hvx, classical.by_contradiction $ λ hx0 : x ≠ 0, by { rw [← v_p_lt_pre_val, hvx] at hx0, exact not_lt_zero' hx0 }, λ hx, hx.symm ▸ pre_val_zero⟩ variables (hv hvp) lemma v_p_lt_val {x : O} : v p < v (algebra_map O K x) ↔ (ideal.quotient.mk _ x : mod_p K v O hv p) ≠ 0 := by rw [lt_iff_not_ge', not_iff_not, ← (algebra_map O K).map_nat_cast p, hv.le_iff_dvd, ideal.quotient.eq_zero_iff_mem, ideal.mem_span_singleton] open nnreal variables {hv} [hvp] include hp lemma mul_ne_zero_of_pow_p_ne_zero {x y : mod_p K v O hv p} (hx : x ^ p ≠ 0) (hy : y ^ p ≠ 0) : x * y ≠ 0 := begin obtain ⟨r, rfl⟩ := ideal.quotient.mk_surjective x, obtain ⟨s, rfl⟩ := ideal.quotient.mk_surjective y, have h1p : (0 : ℝ) < 1 / p := one_div_pos.2 (nat.cast_pos.2 hp.1.pos), rw ← ring_hom.map_mul, rw ← ring_hom.map_pow at hx hy, rw ← v_p_lt_val hv at hx hy ⊢, rw [ring_hom.map_pow, v.map_pow, ← rpow_lt_rpow_iff h1p, ← rpow_nat_cast, ← rpow_mul, mul_one_div_cancel (nat.cast_ne_zero.2 hp.1.ne_zero : (p : ℝ) ≠ 0), rpow_one] at hx hy, rw [ring_hom.map_mul, v.map_mul], refine lt_of_le_of_lt _ (mul_lt_mul'''' hx hy), by_cases hvp : v p = 0, { rw hvp, exact zero_le _ }, replace hvp := zero_lt_iff.2 hvp, conv_lhs { rw ← rpow_one (v p) }, rw ← rpow_add (ne_of_gt hvp), refine rpow_le_rpow_of_exponent_ge hvp ((algebra_map O K).map_nat_cast p ▸ hv.2 _) _, rw [← add_div, div_le_one (nat.cast_pos.2 hp.1.pos : 0 < (p : ℝ))], exact_mod_cast hp.1.two_le end end classical end mod_p /-- Perfection of `O/(p)` where `O` is the ring of integers of `K`. -/ @[nolint has_inhabited_instance] def pre_tilt := ring.perfection (mod_p K v O hv p) p include hp hvp namespace pre_tilt instance : comm_ring (pre_tilt K v O hv p) := perfection.comm_ring p _ instance : char_p (pre_tilt K v O hv p) p := perfection.char_p (mod_p K v O hv p) p section classical open_locale classical open perfection /-- The valuation `Perfection(O/(p)) → ℝ≥0` as a function. Given `f ∈ Perfection(O/(p))`, if `f = 0` then output `0`; otherwise output `pre_val(f(n))^(p^n)` for any `n` such that `f(n) ≠ 0`. -/ noncomputable def val_aux (f : pre_tilt K v O hv p) : ℝ≥0 := if h : ∃ n, coeff _ _ n f ≠ 0 then mod_p.pre_val K v O hv p (coeff _ _ (nat.find h) f) ^ (p ^ nat.find h) else 0 variables {K v O hv p} lemma coeff_nat_find_add_ne_zero {f : pre_tilt K v O hv p} {h : ∃ n, coeff _ _ n f ≠ 0} (k : ℕ) : coeff _ _ (nat.find h + k) f ≠ 0 := coeff_add_ne_zero (nat.find_spec h) k lemma val_aux_eq {f : pre_tilt K v O hv p} {n : ℕ} (hfn : coeff _ _ n f ≠ 0) : val_aux K v O hv p f = mod_p.pre_val K v O hv p (coeff _ _ n f) ^ (p ^ n) := begin have h : ∃ n, coeff _ _ n f ≠ 0 := ⟨n, hfn⟩, rw [val_aux, dif_pos h], obtain ⟨k, rfl⟩ := nat.exists_eq_add_of_le (nat.find_min' h hfn), induction k with k ih, { refl }, obtain ⟨x, hx⟩ := ideal.quotient.mk_surjective (coeff _ _ (nat.find h + k + 1) f), have h1 : (ideal.quotient.mk _ x : mod_p K v O hv p) ≠ 0 := hx.symm ▸ hfn, have h2 : (ideal.quotient.mk _ (x ^ p) : mod_p K v O hv p) ≠ 0, by { erw [ring_hom.map_pow, hx, ← ring_hom.map_pow, coeff_pow_p], exact coeff_nat_find_add_ne_zero k }, erw [ih (coeff_nat_find_add_ne_zero k), ← hx, ← coeff_pow_p, ring_hom.map_pow, ← hx, ← ring_hom.map_pow, mod_p.pre_val_mk h1, mod_p.pre_val_mk h2, ring_hom.map_pow, v.map_pow, ← pow_mul, pow_succ], refl end lemma val_aux_zero : val_aux K v O hv p 0 = 0 := dif_neg $ λ ⟨n, hn⟩, hn rfl lemma val_aux_one : val_aux K v O hv p 1 = 1 := (val_aux_eq $ show coeff (mod_p K v O hv p) p 0 1 ≠ 0, from one_ne_zero).trans $ by { rw [pow_zero, pow_one, ring_hom.map_one, ← (ideal.quotient.mk _).map_one, mod_p.pre_val_mk, ring_hom.map_one, v.map_one], exact @one_ne_zero (mod_p K v O hv p) _ _ } lemma val_aux_mul (f g : pre_tilt K v O hv p) : val_aux K v O hv p (f * g) = val_aux K v O hv p f * val_aux K v O hv p g := begin by_cases hf : f = 0, { rw [hf, zero_mul, val_aux_zero, zero_mul] }, by_cases hg : g = 0, { rw [hg, mul_zero, val_aux_zero, mul_zero] }, replace hf : ∃ n, coeff _ _ n f ≠ 0 := not_forall.1 (λ h, hf $ perfection.ext h), replace hg : ∃ n, coeff _ _ n g ≠ 0 := not_forall.1 (λ h, hg $ perfection.ext h), obtain ⟨m, hm⟩ := hf, obtain ⟨n, hn⟩ := hg, replace hm := coeff_ne_zero_of_le hm (le_max_left m n), replace hn := coeff_ne_zero_of_le hn (le_max_right m n), have hfg : coeff _ _ (max m n + 1) (f * g) ≠ 0, { rw ring_hom.map_mul, refine mod_p.mul_ne_zero_of_pow_p_ne_zero _ _; rw [← ring_hom.map_pow, coeff_pow_p]; assumption }, rw [val_aux_eq (coeff_add_ne_zero hm 1), val_aux_eq (coeff_add_ne_zero hn 1), val_aux_eq hfg], rw ring_hom.map_mul at hfg ⊢, rw [mod_p.pre_val_mul hfg, mul_pow] end lemma val_aux_add (f g : pre_tilt K v O hv p) : val_aux K v O hv p (f + g) ≤ max (val_aux K v O hv p f) (val_aux K v O hv p g) := begin by_cases hf : f = 0, { rw [hf, zero_add, val_aux_zero, max_eq_right], exact zero_le _ }, by_cases hg : g = 0, { rw [hg, add_zero, val_aux_zero, max_eq_left], exact zero_le _ }, by_cases hfg : f + g = 0, { rw [hfg, val_aux_zero], exact zero_le _ }, replace hf : ∃ n, coeff _ _ n f ≠ 0 := not_forall.1 (λ h, hf $ perfection.ext h), replace hg : ∃ n, coeff _ _ n g ≠ 0 := not_forall.1 (λ h, hg $ perfection.ext h), replace hfg : ∃ n, coeff _ _ n (f + g) ≠ 0 := not_forall.1 (λ h, hfg $ perfection.ext h), obtain ⟨m, hm⟩ := hf, obtain ⟨n, hn⟩ := hg, obtain ⟨k, hk⟩ := hfg, replace hm := coeff_ne_zero_of_le hm (le_trans (le_max_left m n) (le_max_left _ k)), replace hn := coeff_ne_zero_of_le hn (le_trans (le_max_right m n) (le_max_left _ k)), replace hk := coeff_ne_zero_of_le hk (le_max_right (max m n) k), rw [val_aux_eq hm, val_aux_eq hn, val_aux_eq hk, ring_hom.map_add], cases le_max_iff.1 (mod_p.pre_val_add (coeff _ _ (max (max m n) k) f) (coeff _ _ (max (max m n) k) g)) with h h, { exact le_max_of_le_left (canonically_ordered_semiring.pow_le_pow_of_le_left h _) }, { exact le_max_of_le_right (canonically_ordered_semiring.pow_le_pow_of_le_left h _) } end variables (K v O hv p) /-- The valuation `Perfection(O/(p)) → ℝ≥0`. Given `f ∈ Perfection(O/(p))`, if `f = 0` then output `0`; otherwise output `pre_val(f(n))^(p^n)` for any `n` such that `f(n) ≠ 0`. -/ noncomputable def val : valuation (pre_tilt K v O hv p) ℝ≥0 := { to_fun := val_aux K v O hv p, map_one' := val_aux_one, map_mul' := val_aux_mul, map_zero' := val_aux_zero, map_add' := val_aux_add } variables {K v O hv p} lemma map_eq_zero {f : pre_tilt K v O hv p} : val K v O hv p f = 0 ↔ f = 0 := begin by_cases hf0 : f = 0, { rw hf0, exact iff_of_true (valuation.map_zero _) rfl }, obtain ⟨n, hn⟩ : ∃ n, coeff _ _ n f ≠ 0 := not_forall.1 (λ h, hf0 $ perfection.ext h), show val_aux K v O hv p f = 0 ↔ f = 0, refine iff_of_false (λ hvf, hn _) hf0, rw val_aux_eq hn at hvf, replace hvf := pow_eq_zero hvf, rwa mod_p.pre_val_eq_zero at hvf end end classical instance : integral_domain (pre_tilt K v O hv p) := { exists_pair_ne := (char_p.nontrivial_of_char_ne_one hp.1.ne_one).1, eq_zero_or_eq_zero_of_mul_eq_zero := λ f g hfg, by { simp_rw ← map_eq_zero at hfg ⊢, contrapose! hfg, rw valuation.map_mul, exact mul_ne_zero hfg.1 hfg.2 }, .. (infer_instance : comm_ring (pre_tilt K v O hv p)) } end pre_tilt /-- The tilt of a field, as defined in Perfectoid Spaces by Peter Scholze, as in [scholze2011perfectoid]. Given a field `K` with valuation `K → ℝ≥0` and ring of integers `O`, this is implemented as the fraction field of the perfection of `O/(p)`. -/ @[nolint has_inhabited_instance] def tilt := fraction_ring (pre_tilt K v O hv p) namespace tilt noncomputable instance : field (tilt K v O hv p) := fraction_ring.field end tilt end perfectoid
c1e51db65bb3266041001a4f1c41a5f9a4d7e2c1
7cef822f3b952965621309e88eadf618da0c8ae9
/src/data/opposite.lean
2df85f2269351ca263d18f933961707d0b8419aa
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
3,274
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Reid Barton, Simon Hudon, Kenny Lau Opposites. -/ import data.list.defs universes v u -- declare the `v` first; see `category_theory.category` for an explanation variable (α : Sort u) /-- The type of objects of the opposite of `α`; used to defined opposite category/group/... In order to avoid confusion between `α` and its opposite type, we set up the type of objects `opposite α` using the following pattern, which will be repeated later for the morphisms. 1. Define `opposite α := α`. 2. Define the isomorphisms `op : α → opposite α`, `unop : opposite α → α`. 3. Make the definition `opposite` irreducible. This has the following consequences. * `opposite α` and `α` are distinct types in the elaborator, so you must use `op` and `unop` explicitly to convert between them. * Both `unop (op X) = X` and `op (unop X) = X` are definitional equalities. Notably, every object of the opposite category is definitionally of the form `op X`, which greatly simplifies the definition of the structure of the opposite category, for example. (If Lean supported definitional eta equality for records, we could achieve the same goals using a structure with one field.) -/ def opposite : Sort u := α -- Use a high right binding power (like that of postfix ⁻¹) so that, for example, -- `presheaf Cᵒᵖ` parses as `presheaf (Cᵒᵖ)` and not `(presheaf C)ᵒᵖ`. notation α `ᵒᵖ`:std.prec.max_plus := opposite α namespace opposite variables {α} def op : α → αᵒᵖ := id def unop : αᵒᵖ → α := id lemma op_inj : function.injective (op : α → αᵒᵖ) := λ _ _, id lemma unop_inj : function.injective (unop : αᵒᵖ → α) := λ _ _, id @[simp] lemma op_inj_iff (x y : α) : op x = op y ↔ x = y := iff.rfl @[simp] lemma unop_inj_iff (x y : αᵒᵖ) : unop x = unop y ↔ x = y := iff.rfl @[simp] lemma op_unop (x : αᵒᵖ) : op (unop x) = x := rfl @[simp] lemma unop_op (x : α) : unop (op x) = x := rfl attribute [irreducible] opposite def op_induction {F : Π (X : αᵒᵖ), Sort v} (h : Π X, F (op X)) : Π X, F X := λ X, h (unop X) end opposite namespace tactic open opposite open interactive interactive.types lean.parser tactic local postfix `?`:9001 := optional namespace op_induction meta def is_opposite (e : expr) : tactic bool := do t ← infer_type e, `(opposite _) ← whnf t | return ff, return tt meta def find_opposite_hyp : tactic name := do lc ← local_context, h :: _ ← lc.mfilter $ is_opposite | fail "No hypotheses of the form Xᵒᵖ", return h.local_pp_name end op_induction open op_induction meta def op_induction (h : option name) : tactic unit := do h ← match h with | (some h) := pure h | none := find_opposite_hyp end, h' ← tactic.get_local h, revert_lst [h'], applyc `opposite.op_induction, tactic.intro h, skip -- For use with `local attribute [tidy] op_induction` meta def op_induction' := op_induction none namespace interactive meta def op_induction (h : parse ident?) : tactic unit := tactic.op_induction h end interactive end tactic
0ed1070a82e368e5426f03ca886364f0d52b88db
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/linear_algebra/tensor_product.lean
9e8a787618ee7d3f761c55da4e3d938d8a14a0a8
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
15,480
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 Tensor product of modules over commutative rings. -/ import group_theory.free_abelian_group import linear_algebra.direct_sum_module variables {R : Type*} [comm_ring R] variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} variables [add_comm_group M] [add_comm_group N] [add_comm_group P] [add_comm_group Q] variables [module R M] [module R N] [module R P] [module R Q] include R set_option class.instance_max_depth 100 namespace linear_map variables (R) def mk₂ (f : M → N → P) (H1 : ∀ m₁ m₂ n, f (m₁ + m₂) n = f m₁ n + f m₂ n) (H2 : ∀ (c:R) m n, f (c • m) n = c • f m n) (H3 : ∀ m n₁ n₂, f m (n₁ + n₂) = f m n₁ + f m n₂) (H4 : ∀ (c:R) m n, f m (c • n) = c • f m n) : M →ₗ N →ₗ P := ⟨λ m, ⟨f m, H3 m, λ c, H4 c m⟩, λ m₁ m₂, linear_map.ext $ H1 m₁ m₂, λ c m, linear_map.ext $ H2 c m⟩ variables {R} @[simp] theorem mk₂_apply (f : M → N → P) {H1 H2 H3 H4} (m : M) (n : N) : (mk₂ R f H1 H2 H3 H4 : M →ₗ[R] N →ₗ P) m n = f m n := rfl variables (f : M →ₗ[R] N →ₗ[R] P) theorem ext₂ {f g : M →ₗ[R] N →ₗ[R] P} (H : ∀ m n, f m n = g m n) : f = g := linear_map.ext (λ m, linear_map.ext $ λ n, H m n) def flip : N →ₗ M →ₗ P := mk₂ R (λ n m, f m n) (λ n₁ n₂ m, (f m).map_add _ _) (λ c n m, (f m).map_smul _ _) (λ n m₁ m₂, by rw f.map_add; refl) (λ c n m, by rw f.map_smul; refl) @[simp] theorem flip_apply (m : M) (n : N) : flip f n m = f m n := rfl variables {R} theorem flip_inj {f g : M →ₗ[R] N →ₗ P} (H : flip f = flip g) : f = g := ext₂ $ λ m n, show flip f n m = flip g n m, by rw H variables (R M N P) def lflip : (M →ₗ[R] N →ₗ P) →ₗ[R] N →ₗ M →ₗ P := ⟨flip, λ _ _, rfl, λ _ _, rfl⟩ variables {R M N P} @[simp] theorem lflip_apply (m : M) (n : N) : lflip R M N P f n m = f m n := rfl theorem map_zero₂ (y) : f 0 y = 0 := (flip f y).map_zero theorem map_neg₂ (x y) : f (-x) y = -f x y := (flip f y).map_neg _ theorem map_add₂ (x₁ x₂ y) : f (x₁ + x₂) y = f x₁ y + f x₂ y := (flip f y).map_add _ _ theorem map_smul₂ (r:R) (x y) : f (r • x) y = r • f x y := (flip f y).map_smul _ _ variables (R P) def lcomp (f : M →ₗ[R] N) : (N →ₗ P) →ₗ M →ₗ P := flip $ (flip id).comp f variables {R P} @[simp] theorem lcomp_apply (f : M →ₗ[R] N) (g : N →ₗ P) (x : M) : lcomp R P f g x = g (f x) := rfl variables (R M N P) def llcomp : (N →ₗ[R] P) →ₗ[R] (M →ₗ[R] N) →ₗ M →ₗ P := flip ⟨lcomp R P, λ f f', ext₂ $ λ g x, g.map_add _ _, λ c f, ext₂ $ λ g x, g.map_smul _ _⟩ variables {R M N P} section @[simp] theorem llcomp_apply (f : N →ₗ[R] P) (g : M →ₗ[R] N) (x : M) : llcomp R M N P f g x = f (g x) := rfl end def compl₂ (g : Q →ₗ N) : M →ₗ Q →ₗ P := (lcomp R _ g).comp f @[simp] theorem compl₂_apply (g : Q →ₗ[R] N) (m : M) (q : Q) : f.compl₂ g m q = f m (g q) := rfl def compr₂ (g : P →ₗ Q) : M →ₗ N →ₗ Q := linear_map.comp (llcomp R N P Q g) f @[simp] theorem compr₂_apply (g : P →ₗ[R] Q) (m : M) (n : N) : f.compr₂ g m n = g (f m n) := rfl variables (R M) def lsmul : R →ₗ M →ₗ M := mk₂ R (•) add_smul (λ _ _ _, eq.symm $ smul_smul _ _ _ _) smul_add (λ r s m, by simp only [smul_smul, smul_eq_mul, mul_comm]) variables {R M} @[simp] theorem lsmul_apply (r : R) (m : M) : lsmul R M r m = r • m := rfl end linear_map variables (M N) namespace tensor_product section open free_abelian_group variables (R) def relators : set (free_abelian_group (M × N)) := add_group.closure { x : free_abelian_group (M × N) | (∃ (m₁ m₂ : M) (n : N), x = of (m₁, n) + of (m₂, n) - of (m₁ + m₂, n)) ∨ (∃ (m : M) (n₁ n₂ : N), x = of (m, n₁) + of (m, n₂) - of (m, n₁ + n₂)) ∨ (∃ (r : R) (m : M) (n : N), x = of (r • m, n) - of (m, r • n)) } end namespace relators instance : normal_add_subgroup (relators R M N) := by unfold relators; apply normal_add_subgroup_of_add_comm_group end relators end tensor_product variables (R) def tensor_product : Type* := quotient_add_group.quotient (tensor_product.relators R M N) variables {R} local infix ` ⊗ `:100 := tensor_product _ local notation M ` ⊗[`:100 R `] ` N:100 := tensor_product R M N namespace tensor_product section module local attribute [instance] quotient_add_group.left_rel normal_add_subgroup.to_is_add_subgroup instance : add_comm_group (M ⊗[R] N) := quotient_add_group.add_comm_group _ instance quotient.mk.is_add_group_hom : is_add_group_hom (quotient.mk : free_abelian_group (M × N) → M ⊗ N) := quotient_add_group.is_add_group_hom _ variables (R) {M N} def tmul (m : M) (n : N) : M ⊗[R] N := quotient_add_group.mk $ free_abelian_group.of (m, n) variables {R} infix ` ⊗ₜ `:100 := tmul _ notation x ` ⊗ₜ[`:100 R `] ` y := tmul R x y lemma add_tmul (m₁ m₂ : M) (n : N) : (m₁ + m₂) ⊗ₜ n = m₁ ⊗ₜ n + m₂ ⊗ₜ[R] n := eq.symm $ sub_eq_zero.1 $ eq.symm $ quotient.sound $ group.in_closure.basic $ or.inl $ ⟨m₁, m₂, n, rfl⟩ lemma tmul_add (m : M) (n₁ n₂ : N) : m ⊗ₜ (n₁ + n₂) = m ⊗ₜ n₁ + m ⊗ₜ[R] n₂ := eq.symm $ sub_eq_zero.1 $ eq.symm $ quotient.sound $ group.in_closure.basic $ or.inr $ or.inl $ ⟨m, n₁, n₂, rfl⟩ lemma smul_tmul (r : R) (m : M) (n : N) : (r • m) ⊗ₜ n = m ⊗ₜ[R] (r • n) := sub_eq_zero.1 $ eq.symm $ quotient.sound $ group.in_closure.basic $ or.inr $ or.inr $ ⟨r, m, n, rfl⟩ local attribute [instance] quotient_add_group.is_add_group_hom_quotient_lift def smul.aux (r : R) : free_abelian_group (M × N) → M ⊗[R] N := free_abelian_group.lift (λ (y : M × N), (r • y.1) ⊗ₜ y.2) instance (r : R) : is_add_group_hom (smul.aux r : _ → M ⊗ N) := by unfold smul.aux; apply_instance instance : has_scalar R (M ⊗ N) := ⟨λ r, quotient_add_group.lift _ (smul.aux r) $ λ x hx, begin refine (is_add_group_hom.mem_ker (smul.aux r : _ → M ⊗ N)).1 (add_group.closure_subset _ hx), clear hx x, rintro x (⟨m₁, m₂, n, rfl⟩ | ⟨m, n₁, n₂, rfl⟩ | ⟨q, m, n, rfl⟩); simp only [smul.aux, is_add_group_hom.mem_ker, -sub_eq_add_neg, sub_self, add_tmul, tmul_add, smul_tmul, smul_add, smul_smul, mul_comm, free_abelian_group.lift.of, free_abelian_group.lift.add, free_abelian_group.lift.sub] end⟩ instance smul.is_add_group_hom (r : R) : is_add_group_hom ((•) r : M ⊗[R] N → M ⊗[R] N) := by unfold has_scalar.smul; apply_instance protected theorem smul_add (r : R) (x y : M ⊗[R] N) : r • (x + y) = r • x + r • y := is_add_group_hom.map_add _ _ _ instance : module R (M ⊗ N) := module.of_core { smul := (•), smul_add := tensor_product.smul_add, add_smul := begin intros r s x, apply quotient_add_group.induction_on' x, intro z, symmetry, refine @free_abelian_group.lift.unique _ _ _ _ _ ⟨λ p q, _⟩ _ z, { simp [tensor_product.smul_add] }, rintro ⟨m, n⟩, change (r • m) ⊗ₜ n + (s • m) ⊗ₜ n = ((r + s) • m) ⊗ₜ n, rw [add_smul, add_tmul] end, mul_smul := begin intros r s x, apply quotient_add_group.induction_on' x, intro z, symmetry, refine @free_abelian_group.lift.unique _ _ _ _ _ ⟨λ p q, _⟩ _ z, { simp [tensor_product.smul_add] }, rintro ⟨m, n⟩, change r • s • (m ⊗ₜ n) = ((r * s) • m) ⊗ₜ n, rw mul_smul, refl end, one_smul := λ x, quotient.induction_on x $ λ _, eq.symm $ free_abelian_group.lift.unique _ _ $ λ ⟨p, q⟩, by rw one_smul; refl } @[simp] lemma tmul_smul (r : R) (x : M) (y : N) : x ⊗ₜ (r • y) = r • (x ⊗ₜ[R] y) := (smul_tmul _ _ _).symm variables (R M N) def mk : M →ₗ N →ₗ M ⊗ N := linear_map.mk₂ R (⊗ₜ) add_tmul (λ c m n, by rw [smul_tmul, tmul_smul]) tmul_add tmul_smul variables {R M N} @[simp] lemma mk_apply (m : M) (n : N) : mk R M N m n = m ⊗ₜ n := rfl lemma zero_tmul (n : N) : (0 ⊗ₜ[R] n : M ⊗ N) = 0 := (mk R M N).map_zero₂ _ lemma tmul_zero (m : M) : (m ⊗ₜ[R] 0 : M ⊗ N) = 0 := (mk R M N _).map_zero lemma neg_tmul (m : M) (n : N) : (-m) ⊗ₜ n = -(m ⊗ₜ[R] n) := (mk R M N).map_neg₂ _ _ lemma tmul_neg (m : M) (n : N) : m ⊗ₜ (-n) = -(m ⊗ₜ[R] n) := (mk R M N _).map_neg _ end module local attribute [instance] quotient_add_group.left_rel normal_add_subgroup.to_is_add_subgroup @[elab_as_eliminator] protected theorem induction_on {C : (M ⊗[R] N) → Prop} (z : M ⊗[R] N) (C0 : C 0) (C1 : ∀ x y, C $ x ⊗ₜ[R] y) (Cp : ∀ x y, C x → C y → C (x + y)) : C z := quotient.induction_on z $ λ x, free_abelian_group.induction_on x C0 (λ ⟨p, q⟩, C1 p q) (λ ⟨p, q⟩ _, show C (-(p ⊗ₜ q)), by rw ← neg_tmul; from C1 (-p) q) (λ _ _, Cp _ _) section UMP variables {M N P Q} variables (f : M →ₗ[R] N →ₗ[R] P) local attribute [instance] free_abelian_group.lift.is_add_group_hom def lift_aux : (M ⊗[R] N) → P := quotient_add_group.lift _ (free_abelian_group.lift $ λ z, f z.1 z.2) $ λ x hx, begin refine (is_add_group_hom.mem_ker _).1 (add_group.closure_subset _ hx), clear hx x, rintro x (⟨m₁, m₂, n, rfl⟩ | ⟨m, n₁, n₂, rfl⟩ | ⟨q, m, n, rfl⟩); simp [is_add_group_hom.mem_ker, -sub_eq_add_neg, f.map_add, f.map_add₂, f.map_smul, f.map_smul₂, sub_self], end variable {f} local attribute [instance] quotient_add_group.left_rel normal_add_subgroup.to_is_add_subgroup @[simp] lemma lift_aux.add (x y) : lift_aux f (x + y) = lift_aux f x + lift_aux f y := quotient.induction_on₂ x y $ λ m n, free_abelian_group.lift.add _ _ _ @[simp] lemma lift_aux.smul (r:R) (x) : lift_aux f (r • x) = r • lift_aux f x := tensor_product.induction_on _ _ x (smul_zero _).symm (λ p q, by rw [← tmul_smul]; simp [lift_aux, tmul]) (λ p q ih1 ih2, by simp [@smul_add _ _ _ _ _ _ p _, lift_aux.add, ih1, ih2, smul_add]) variable (f) def lift : M ⊗ N →ₗ P := { to_fun := lift_aux f, add := lift_aux.add, smul := lift_aux.smul } variable {f} @[simp] lemma lift.tmul (x y) : lift f (x ⊗ₜ y) = f x y := zero_add _ @[simp] lemma lift.tmul' (x y) : (lift f).1 (x ⊗ₜ y) = f x y := lift.tmul _ _ theorem lift.unique {g : (M ⊗[R] N) →ₗ[R] P} (H : ∀ x y, g (x ⊗ₜ y) = f x y) : g = lift f := linear_map.ext $ λ z, begin apply quotient_add_group.induction_on' z, intro z, refine @free_abelian_group.lift.unique _ _ _ _ _ ⟨λ p q, _⟩ _ z, { simp [g.2] }, exact λ ⟨m, n⟩, H m n end theorem lift_mk : lift (mk R M N) = linear_map.id := eq.symm $ lift.unique $ λ x y, rfl theorem lift_compr₂ (g : P →ₗ Q) : lift (f.compr₂ g) = g.comp (lift f) := eq.symm $ lift.unique $ λ x y, by simp theorem lift_mk_compr₂ (f : M ⊗ N →ₗ P) : lift ((mk R M N).compr₂ f) = f := by rw [lift_compr₂, lift_mk, linear_map.comp_id] theorem ext {g h : (M ⊗[R] N) →ₗ[R] P} (H : ∀ x y, g (x ⊗ₜ y) = h (x ⊗ₜ y)) : g = h := by rw ← lift_mk_compr₂ h; exact lift.unique H theorem mk_compr₂_inj {g h : M ⊗ N →ₗ P} (H : (mk R M N).compr₂ g = (mk R M N).compr₂ h) : g = h := by rw [← lift_mk_compr₂ g, H, lift_mk_compr₂] example : M → N → (M → N → P) → P := λ m, flip $ λ f, f m variables (R M N P) def uncurry : (M →ₗ N →ₗ[R] P) →ₗ M ⊗ N →ₗ P := linear_map.flip $ lift $ (linear_map.lflip _ _ _ _).comp linear_map.id.flip variables {R M N P} @[simp] theorem uncurry_apply (f : M →ₗ[R] N →ₗ[R] P) (m : M) (n : N) : uncurry R M N P f (m ⊗ₜ n) = f m n := by rw [uncurry, linear_map.flip_apply, lift.tmul]; refl variables (R M N P) def lift.equiv : (M →ₗ N →ₗ P) ≃ₗ (M ⊗ N →ₗ P) := { inv_fun := λ f, (mk R M N).compr₂ f, left_inv := λ f, linear_map.ext₂ $ λ m n, lift.tmul _ _, right_inv := λ f, ext $ λ m n, lift.tmul _ _, .. uncurry R M N P } def lcurry : (M ⊗[R] N →ₗ[R] P) →ₗ[R] M →ₗ[R] N →ₗ[R] P := (lift.equiv R M N P).symm variables {R M N P} @[simp] theorem lcurry_apply (f : M ⊗[R] N →ₗ[R] P) (m : M) (n : N) : lcurry R M N P f m n = f (m ⊗ₜ n) := rfl def curry (f : M ⊗ N →ₗ P) : M →ₗ N →ₗ P := lcurry R M N P f @[simp] theorem curry_apply (f : M ⊗ N →ₗ[R] P) (m : M) (n : N) : curry f m n = f (m ⊗ₜ n) := rfl end UMP variables {M N} protected def lid : R ⊗ M ≃ₗ M := linear_equiv.of_linear (lift $ linear_map.lsmul R M) (mk R R M 1) (linear_map.ext $ λ _, by simp) (ext $ λ r m, by simp; rw [← tmul_smul, ← smul_tmul, smul_eq_mul, mul_one]) protected def comm : M ⊗ N ≃ₗ N ⊗ M := linear_equiv.of_linear (lift (mk R N M).flip) (lift (mk R M N).flip) (ext $ λ m n, rfl) (ext $ λ m n, rfl) open linear_map protected def assoc : (M ⊗[R] N) ⊗[R] P ≃ₗ[R] M ⊗[R] (N ⊗[R] P) := begin refine linear_equiv.of_linear (lift $ lift $ comp (lcurry R _ _ _) $ mk _ _ _) (lift $ comp (uncurry R _ _ _) $ curry $ mk _ _ _) (mk_compr₂_inj $ linear_map.ext $ λ m, ext $ λ n p, _) (mk_compr₂_inj $ flip_inj $ linear_map.ext $ λ p, ext $ λ m n, _); repeat { rw lift.tmul <|> rw compr₂_apply <|> rw comp_apply <|> rw mk_apply <|> rw flip_apply <|> rw lcurry_apply <|> rw uncurry_apply <|> rw curry_apply <|> rw id_apply } end def map (f : M →ₗ[R] P) (g : N →ₗ Q) : M ⊗ N →ₗ P ⊗ Q := lift $ comp (compl₂ (mk _ _ _) g) f @[simp] theorem map_tmul (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (m : M) (n : N) : map f g (m ⊗ₜ n) = f m ⊗ₜ g n := rfl def congr (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) : M ⊗ N ≃ₗ[R] P ⊗ Q := linear_equiv.of_linear (map f g) (map f.symm g.symm) (ext $ λ m n, by simp; simp only [linear_equiv.apply_symm_apply]) (ext $ λ m n, by simp; simp only [linear_equiv.symm_apply_apply]) variables (ι₁ : Type*) (ι₂ : Type*) variables [decidable_eq ι₁] [decidable_eq ι₂] variables (β₁ : ι₁ → Type*) (β₂ : ι₂ → Type*) variables [Π i₁, add_comm_group (β₁ i₁)] [Π i₂, add_comm_group (β₂ i₂)] variables [Π i₁, module R (β₁ i₁)] [Π i₂, module R (β₂ i₂)] def direct_sum : direct_sum ι₁ β₁ ⊗[R] direct_sum ι₂ β₂ ≃ₗ[R] direct_sum (ι₁ × ι₂) (λ i, β₁ i.1 ⊗[R] β₂ i.2) := begin refine linear_equiv.of_linear (lift $ direct_sum.to_module R _ _ $ λ i₁, flip $ direct_sum.to_module R _ _ $ λ i₂, flip $ curry $ direct_sum.lof R (ι₁ × ι₂) (λ i, β₁ i.1 ⊗[R] β₂ i.2) (i₁, i₂)) (direct_sum.to_module R _ _ $ λ i, map (direct_sum.lof R _ _ _) (direct_sum.lof R _ _ _)) (linear_map.ext $ direct_sum.to_module.ext $ λ i, mk_compr₂_inj $ linear_map.ext $ λ x₁, linear_map.ext $ λ x₂, _) (mk_compr₂_inj $ linear_map.ext $ direct_sum.to_module.ext $ λ i₁, linear_map.ext $ λ x₁, linear_map.ext $ direct_sum.to_module.ext $ λ i₂, linear_map.ext $ λ x₂, _); repeat { rw compr₂_apply <|> rw comp_apply <|> rw id_apply <|> rw mk_apply <|> rw direct_sum.to_module_lof <|> rw map_tmul <|> rw lift.tmul <|> rw flip_apply <|> rw curry_apply }, cases i; refl end end tensor_product
42bf289175f1a45442dac368fc2242ba32f40ec7
9bfad7663303efa7b1cd1bf8a0b91cbb8d9a31a6
/src/deduction.lean
fd46180291f457ccd6e751a37431add289ffcc9c
[]
no_license
iehality/modallogic
9c61bdeb4ef5798c9bc80d2b39d663c24dadaf2a
b759f418259b2e1b3b7dd8a784069f0eba49907a
refs/heads/main
1,679,183,622,574
1,614,786,343,000
1,614,786,343,000
343,304,144
0
0
null
null
null
null
UTF-8
Lean
false
false
856
lean
import syntax def subst (p : ℕ) (φ : formula) : formula → formula | (q : ℕ) := if p = q then φ else q | ⊥ₘ := ⊥ₘ | (ψ →ₘ χ) := subst ψ →ₘ subst χ | □ψ := □(subst ψ) inductive prv (Γ : set formula) : set formula | us {p φ ψ} : prv φ → prv (subst p ψ φ) | ax {φ} : φ ∈ Γ → prv φ | mp {φ ψ} : prv (φ →ₘ ψ) → prv φ → prv ψ | n {φ} : prv φ → prv □φ | ak {φ ψ} : prv (φ →ₘ ψ →ₘ φ) | as {φ ψ χ} : prv ((φ →ₘ ψ →ₘ χ) →ₘ (φ →ₘ ψ) →ₘ φ →ₘ χ) | an {φ ψ} : prv ((¬ₘφ →ₘ ¬ₘψ) →ₘ ψ →ₘ φ) | k {φ ψ} : prv (□(φ →ₘ ψ) →ₘ □φ →ₘ □ψ) notation `𝐊` := prv prefix `⊢ₖ`:55 := prv ∅ infix ` ⊢ `:55 := prv def consistent (Γ : set formula) : Prop := ⊥ₘ ∉ 𝐊 Γ
fd10c1f1ae1df2dce913e8f91375c4d1b260c956
07c76fbd96ea1786cc6392fa834be62643cea420
/hott/types/sum.hlean
8ea07b4114e82da8ef4c462bc112e2b03d337462
[ "Apache-2.0" ]
permissive
fpvandoorn/lean2
5a430a153b570bf70dc8526d06f18fc000a60ad9
0889cf65b7b3cebfb8831b8731d89c2453dd1e9f
refs/heads/master
1,592,036,508,364
1,545,093,958,000
1,545,093,958,000
75,436,854
0
0
null
1,480,718,780,000
1,480,718,780,000
null
UTF-8
Lean
false
false
19,462
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Theorems about sums/coproducts/disjoint unions -/ import .pi .equiv logic open lift eq is_equiv equiv prod prod.ops is_trunc sigma bool namespace sum universe variables u v u' v' variables {A : Type.{u}} {B : Type.{v}} (z z' : A + B) {P : A → Type.{u'}} {Q : A → Type.{v'}} protected definition eta : sum.rec inl inr z = z := by induction z; all_goals reflexivity protected definition code [unfold 3 4] : A + B → A + B → Type.{max u v} | code (inl a) (inl a') := lift (a = a') | code (inr b) (inr b') := lift (b = b') | code _ _ := lift empty protected definition decode [unfold 3 4] : Π(z z' : A + B), sum.code z z' → z = z' | decode (inl a) (inl a') := λc, ap inl (down c) | decode (inl a) (inr b') := λc, empty.elim (down c) _ | decode (inr b) (inl a') := λc, empty.elim (down c) _ | decode (inr b) (inr b') := λc, ap inr (down c) protected definition mem_cases : (Σ a, z = inl a) + (Σ b, z = inr b) := by cases z with a b; exact inl ⟨a, idp⟩; exact inr ⟨b, idp⟩ protected definition eqrec {A B : Type} {C : A + B → Type} (x : A + B) (cl : Π a, x = inl a → C (inl a)) (cr : Π b, x = inr b → C (inr b)) : C x := by cases x with a b; exact cl a idp; exact cr b idp variables {z z'} protected definition encode [unfold 3 4 5] (p : z = z') : sum.code z z' := by induction p; induction z; all_goals exact up idp variables (z z') definition sum_eq_equiv [constructor] : (z = z') ≃ sum.code z z' := equiv.MK sum.encode !sum.decode abstract begin intro c, induction z with a b, all_goals induction z' with a' b', all_goals (esimp at *; induction c with c), all_goals induction c, -- c either has type empty or a path all_goals reflexivity end end abstract begin intro p, induction p, induction z, all_goals reflexivity end end section variables {a a' : A} {b b' : B} definition eq_of_inl_eq_inl [unfold 5] (p : inl a = inl a' :> A + B) : a = a' := down (sum.encode p) definition eq_of_inr_eq_inr [unfold 5] (p : inr b = inr b' :> A + B) : b = b' := down (sum.encode p) definition empty_of_inl_eq_inr (p : inl a = inr b) : empty := down (sum.encode p) definition empty_of_inr_eq_inl (p : inr b = inl a) : empty := down (sum.encode p) /- Transport -/ definition sum_transport (p : a = a') (z : P a + Q a) : p ▸ z = sum.rec (λa, inl (p ▸ a)) (λb, inr (p ▸ b)) z := by induction p; induction z; all_goals reflexivity /- Pathovers -/ definition etao (p : a = a') (z : P a + Q a) : z =[p] sum.rec (λa, inl (p ▸ a)) (λb, inr (p ▸ b)) z := by induction p; induction z; all_goals constructor protected definition codeo (p : a = a') : P a + Q a → P a' + Q a' → Type.{max u' v'} | codeo (inl x) (inl x') := lift.{u' v'} (x =[p] x') | codeo (inr y) (inr y') := lift.{v' u'} (y =[p] y') | codeo _ _ := lift empty protected definition decodeo (p : a = a') : Π(z : P a + Q a) (z' : P a' + Q a'), sum.codeo p z z' → z =[p] z' | decodeo (inl x) (inl x') := λc, apo (λa, inl) (down c) | decodeo (inl x) (inr y') := λc, empty.elim (down c) _ | decodeo (inr y) (inl x') := λc, empty.elim (down c) _ | decodeo (inr y) (inr y') := λc, apo (λa, inr) (down c) variables {z z'} protected definition encodeo {p : a = a'} {z : P a + Q a} {z' : P a' + Q a'} (q : z =[p] z') : sum.codeo p z z' := by induction q; induction z; all_goals exact up idpo variables (z z') definition sum_pathover_equiv [constructor] (p : a = a') (z : P a + Q a) (z' : P a' + Q a') : (z =[p] z') ≃ sum.codeo p z z' := equiv.MK sum.encodeo !sum.decodeo abstract begin intro c, induction z with a b, all_goals induction z' with a' b', all_goals (esimp at *; induction c with c), all_goals induction c, -- c either has type empty or a pathover all_goals reflexivity end end abstract begin intro q, induction q, induction z, all_goals reflexivity end end end /- Functorial action -/ variables {A' B' : Type} (f : A → A') (g : B → B') definition sum_functor [unfold 7] : A + B → A' + B' | sum_functor (inl a) := inl (f a) | sum_functor (inr b) := inr (g b) infix ` +→ `:62 := sum_functor /- Equivalences -/ definition is_equiv_sum_functor [constructor] [instance] [Hf : is_equiv f] [Hg : is_equiv g] : is_equiv (sum_functor f g) := adjointify (sum_functor f g) (sum_functor f⁻¹ g⁻¹) abstract begin intro z, induction z, all_goals (esimp; (apply ap inl | apply ap inr); apply right_inv) end end abstract begin intro z, induction z, all_goals (esimp; (apply ap inl | apply ap inr); apply right_inv) end end definition sum_equiv_sum_of_is_equiv [constructor] [Hf : is_equiv f] [Hg : is_equiv g] : A + B ≃ A' + B' := equiv.mk _ (is_equiv_sum_functor f g) definition sum_equiv_sum [constructor] (f : A ≃ A') (g : B ≃ B') : A + B ≃ A' + B' := equiv.mk _ (is_equiv_sum_functor f g) infix ` +≃ `:62 := sum_equiv_sum definition sum_equiv_sum_left [constructor] (g : B ≃ B') : A + B ≃ A + B' := sum_equiv_sum equiv.rfl g definition sum_equiv_sum_right [constructor] (f : A ≃ A') : A + B ≃ A' + B := sum_equiv_sum f equiv.rfl definition flip [unfold 3] : A + B → B + A | flip (inl a) := inr a | flip (inr b) := inl b definition flip_flip (x : A ⊎ B) : flip (flip x) = x := begin induction x: reflexivity end definition sum_comm_equiv [constructor] (A B : Type) : A + B ≃ B + A := equiv.MK flip flip flip_flip flip_flip definition sum_assoc_equiv [constructor] (A B C : Type) : A + (B + C) ≃ (A + B) + C := begin fapply equiv.MK, all_goals try (intro z; induction z with u v; all_goals try induction u; all_goals try induction v), exact inl (inl u), exact inl (inr a), exact inr a, exact inl a, exact inr (inl a), exact inr (inr v), all_goals reflexivity end definition sum_empty_equiv [constructor] (A : Type) : A + empty ≃ A := begin fapply equiv.MK, { intro z, induction z, assumption, contradiction}, { exact inl}, { intro a, reflexivity}, { intro z, induction z, reflexivity, contradiction} end definition empty_sum_equiv [constructor] (A : Type) : empty + A ≃ A := !sum_comm_equiv ⬝e !sum_empty_equiv definition bool_equiv_unit_sum_unit [constructor] : bool ≃ unit + unit := begin fapply equiv.MK, { intro b, cases b, exact inl unit.star, exact inr unit.star }, { intro s, cases s, exact bool.ff, exact bool.tt }, { intro s, cases s, do 2 (cases a; reflexivity) }, { intro b, cases b, do 2 reflexivity }, end definition sum_prod_right_distrib [constructor] (A B C : Type) : (A + B) × C ≃ (A × C) + (B × C) := begin fapply equiv.MK, { intro x, cases x with ab c, cases ab with a b, exact inl (a, c), exact inr (b, c) }, { intro x, cases x with ac bc, cases ac with a c, exact (inl a, c), cases bc with b c, exact (inr b, c) }, { intro x, cases x with ac bc, cases ac with a c, reflexivity, cases bc, reflexivity }, { intro x, cases x with ab c, cases ab with a b, do 2 reflexivity } end definition sum_prod_left_distrib [constructor] (A B C : Type) : A × (B + C) ≃ (A × B) + (A × C) := calc A × (B + C) ≃ (B + C) × A : prod_comm_equiv ... ≃ (B × A) + (C × A) : sum_prod_right_distrib ... ≃ (A × B) + (C × A) : sum_equiv_sum_right !prod_comm_equiv ... ≃ (A × B) + (A × C) : sum_equiv_sum_left !prod_comm_equiv section variables (H : unit + A ≃ unit + B) include H open unit sigma.ops definition unit_sum_equiv_cancel_map : A → B := begin intro a, cases sum.mem_cases (H (inr a)) with u b, rotate 1, exact b.1, cases u with u Hu, cases sum.mem_cases (H (inl ⋆)) with u' b, rotate 1, exact b.1, cases u' with u' Hu', exfalso, apply empty_of_inl_eq_inr, calc inl ⋆ = H⁻¹ (H (inl ⋆)) : (to_left_inv H (inl ⋆))⁻¹ ... = H⁻¹ (inl u') : {Hu'} ... = H⁻¹ (inl u) : is_prop.elim ... = H⁻¹ (H (inr a)) : {Hu⁻¹} ... = inr a : to_left_inv H (inr a) end definition unit_sum_equiv_cancel_inv (b : B) : unit_sum_equiv_cancel_map H (unit_sum_equiv_cancel_map H⁻¹ᵉ b) = b := begin esimp[unit_sum_equiv_cancel_map], apply sum.rec, { intro x, cases x with u Hu, esimp, apply sum.rec, { intro x, exfalso, cases x with u' Hu', apply empty_of_inl_eq_inr, calc inl ⋆ = H⁻¹ (H (inl ⋆)) : (to_left_inv H (inl ⋆))⁻¹ ... = H⁻¹ (inl u') : ap H⁻¹ Hu' ... = H⁻¹ (inl u) : {!is_prop.elim} ... = H⁻¹ (H (inr _)) : {Hu⁻¹} ... = inr _ : to_left_inv H }, { intro x, cases x with b' Hb', esimp, cases sum.mem_cases (H⁻¹ (inr b)) with x x, { cases x with u' Hu', cases u', apply eq_of_inr_eq_inr, calc inr b' = H (inl ⋆) : Hb'⁻¹ ... = H (H⁻¹ (inr b)) : (ap H Hu')⁻¹ ... = inr b : to_right_inv H (inr b)}, { exfalso, cases x with a Ha, apply empty_of_inl_eq_inr, cases u, apply concat, apply Hu⁻¹, apply concat, rotate 1, apply !(to_right_inv H), apply ap H, apply concat, rotate 1, apply Ha⁻¹, apply ap inr, esimp, apply sum.rec, intro x, exfalso, apply empty_of_inl_eq_inr, apply concat, exact x.2⁻¹, apply Ha, intro x, cases x with a' Ha', esimp, apply eq_of_inr_eq_inr, apply Ha'⁻¹ ⬝ Ha } } }, { intro x, cases x with b' Hb', esimp, apply eq_of_inr_eq_inr, refine Hb'⁻¹ ⬝ _, cases sum.mem_cases (H⁻¹ (inr b)) with x x, { cases x with u Hu, esimp, cases sum.mem_cases (H⁻¹ (inl ⋆)) with x x, { cases x with u' Hu', exfalso, apply empty_of_inl_eq_inr, calc inl ⋆ = H (H⁻¹ (inl ⋆)) : (to_right_inv H (inl ⋆))⁻¹ ... = H (inl u') : ap H Hu' ... = H (inl u) : by rewrite [is_prop.elim u' u] ... = H (H⁻¹ (inr b)) : ap H Hu⁻¹ ... = inr b : to_right_inv H (inr b) }, { cases x with a Ha, exfalso, apply empty_of_inl_eq_inr, apply concat, rotate 1, exact Hb', have Ha' : inl ⋆ = H (inr a), by apply !(to_right_inv H)⁻¹ ⬝ ap H Ha, apply concat Ha', apply ap H, apply ap inr, apply sum.rec, intro x, cases x with u' Hu', esimp, apply sum.rec, intro x, cases x with u'' Hu'', esimp, apply empty.rec, intro x, cases x with a'' Ha'', esimp, krewrite Ha' at Ha'', apply eq_of_inr_eq_inr, apply !(to_left_inv H)⁻¹ ⬝ Ha'', intro x, exfalso, cases x with a'' Ha'', apply empty_of_inl_eq_inr, apply Hu⁻¹ ⬝ Ha'', } }, { cases x with a' Ha', esimp, refine _ ⬝ !(to_right_inv H), apply ap H, apply Ha'⁻¹ } } end definition unit_sum_equiv_cancel : A ≃ B := begin fapply equiv.MK, apply unit_sum_equiv_cancel_map H, apply unit_sum_equiv_cancel_map H⁻¹ᵉ, intro b, apply unit_sum_equiv_cancel_inv, { intro a, have H = (H⁻¹ᵉ)⁻¹ᵉ, from !equiv.symm_symm⁻¹, rewrite this at {2}, apply unit_sum_equiv_cancel_inv } end end /- universal property -/ definition sum_rec_unc [unfold 5] {P : A + B → Type} (fg : (Πa, P (inl a)) × (Πb, P (inr b))) : Πz, P z := sum.rec fg.1 fg.2 definition is_equiv_sum_rec [constructor] (P : A + B → Type) : is_equiv (sum_rec_unc : (Πa, P (inl a)) × (Πb, P (inr b)) → Πz, P z) := begin apply adjointify sum_rec_unc (λf, (λa, f (inl a), λb, f (inr b))), intro f, apply eq_of_homotopy, intro z, focus (induction z; all_goals reflexivity), intro h, induction h with f g, reflexivity end definition equiv_sum_rec [constructor] (P : A + B → Type) : (Πa, P (inl a)) × (Πb, P (inr b)) ≃ Πz, P z := equiv.mk _ !is_equiv_sum_rec definition imp_prod_imp_equiv_sum_imp [constructor] (A B C : Type) : (A → C) × (B → C) ≃ (A + B → C) := !equiv_sum_rec /- truncatedness -/ theorem is_trunc_sum (n : ℕ₋₂) (A B : Type) [HA : is_trunc (n.+2) A] [HB : is_trunc (n.+2) B] : is_trunc (n.+2) (A + B) := begin apply is_trunc_succ_intro, intro z z', apply is_trunc_equiv_closed_rev, apply sum_eq_equiv, induction z with a b, all_goals induction z' with a' b', all_goals esimp, all_goals exact _, end theorem is_trunc_sum_excluded (n : ℕ₋₂) (A B : Type) [HA : is_trunc n A] [HB : is_trunc n B] (H : A → B → empty) : is_trunc n (A + B) := begin induction n with n IH, { exfalso, exact H !center !center}, { clear IH, induction n with n IH, { apply is_prop.mk, intros x y, induction x, all_goals induction y, all_goals esimp, all_goals try (exfalso;apply H;assumption;assumption), all_goals apply ap _ !is_prop.elim}, { apply is_trunc_sum}} end definition is_contr_sum_left (A : Type) {B : Type} [HA : is_contr A] (H : ¬B) : is_contr (A + B) := is_contr.mk (inl !center) (λx, sum.rec_on x (λa, ap inl !center_eq) (λb, empty.elim (H b))) /- Sums are equivalent to dependent sigmas where the first component is a bool. The current construction only works for A and B in the same universe. If we need it for A and B in different universes, we need to insert some lifts. -/ definition sum_of_sigma_bool {A B : Type.{u}} (v : Σ(b : bool), bool.rec A B b) : A + B := by induction v with b x; induction b; exact inl x; exact inr x definition sigma_bool_of_sum {A B : Type.{u}} (z : A + B) : Σ(b : bool), bool.rec A B b := by induction z with a b; exact ⟨ff, a⟩; exact ⟨tt, b⟩ definition sum_equiv_sigma_bool [constructor] (A B : Type.{u}) : A + B ≃ Σ(b : bool), bool.rec A B b := equiv.MK sigma_bool_of_sum sum_of_sigma_bool begin intro v, induction v with b x, induction b, all_goals reflexivity end begin intro z, induction z with a b, all_goals reflexivity end variables {A₀₀ A₂₀ A₀₂ A₂₂ B₀₀ B₂₀ B₀₂ B₂₂ C C' : Type} {f₁₀ : A₀₀ → A₂₀} {f₁₂ : A₀₂ → A₂₂} {f₀₁ : A₀₀ → A₀₂} {f₂₁ : A₂₀ → A₂₂} {g₁₀ : B₀₀ → B₂₀} {g₁₂ : B₀₂ → B₂₂} {g₀₁ : B₀₀ → B₀₂} {g₂₁ : B₂₀ → B₂₂} {h₀₁ : B₀₀ → A₀₂} {h₂₁ : B₂₀ → A₂₂} open function definition sum_rec_hsquare [unfold 16] (h : hsquare f₁₀ f₁₂ f₀₁ f₂₁) (k : hsquare g₁₀ f₁₂ h₀₁ h₂₁) : hsquare (f₁₀ +→ g₁₀) f₁₂ (sum.rec f₀₁ h₀₁) (sum.rec f₂₁ h₂₁) := begin intro x, induction x with a b, exact h a, exact k b end definition sum_functor_hsquare [unfold 19] (h : hsquare f₁₀ f₁₂ f₀₁ f₂₁) (k : hsquare g₁₀ g₁₂ g₀₁ g₂₁) : hsquare (f₁₀ +→ g₁₀) (f₁₂ +→ g₁₂) (f₀₁ +→ g₀₁) (f₂₁ +→ g₂₁) := sum_rec_hsquare (λa, ap inl (h a)) (λb, ap inr (k b)) definition sum_functor_compose (g : B → C) (f : A → B) (g' : B' → C') (f' : A' → B') : (g ∘ f) +→ (g' ∘ f') ~ g +→ g' ∘ f +→ f' := begin intro x, induction x with a a': reflexivity end definition sum_rec_sum_functor (g : B → C) (g' : B' → C) (f : A → B) (f' : A' → B') : sum.rec g g' ∘ sum_functor f f' ~ sum.rec (g ∘ f) (g' ∘ f') := begin intro x, induction x with a a': reflexivity end definition sum_rec_same_compose (g : B → C) (f : A → B) : sum.rec (g ∘ f) (g ∘ f) ~ g ∘ sum.rec f f := begin intro x, induction x with a a': reflexivity end definition sum_rec_same (f : A → B) : sum.rec f f ~ f ∘ sum.rec id id := by exact sum_rec_same_compose f id /- pointed sums. We arbitrarily choose (inl pt) as basepoint for the sum -/ open pointed definition psum [constructor] (A B : Type*) : Type* := pointed.MK (A ⊎ B) (inl pt) infixr ` +* `:30 := psum end sum open sum pi namespace decidable /- some properties about the inductive type `decidable` decidable A is equivalent to A + ¬A -/ definition decidable_equiv [constructor] (A : Type) : decidable A ≃ A + ¬A := begin fapply equiv.MK:intro a;induction a:try (constructor;assumption;now), all_goals reflexivity end definition is_trunc_decidable [constructor] (A : Type) (n : ℕ₋₂) [H : is_trunc n A] : is_trunc n (decidable A) := begin apply is_trunc_equiv_closed_rev, apply decidable_equiv, induction n with n IH, { apply is_contr_sum_left, exact λna, na !center}, { apply is_trunc_sum_excluded, exact λa na, na a} end definition double_neg_elim {A : Type} (H : decidable A) (p : ¬ ¬ A) : A := begin induction H, assumption, contradiction end definition dite_true {C : Type} [H : decidable C] {A : Type} {t : C → A} {e : ¬ C → A} (c : C) (H' : is_prop C) : dite C t e = t c := begin induction H with H H, exact ap t !is_prop.elim, contradiction end definition dite_false {C : Type} [H : decidable C] {A : Type} {t : C → A} {e : ¬ C → A} (c : ¬ C) : dite C t e = e c := begin induction H with H H, contradiction, exact ap e !is_prop.elim, end definition decidable_eq_of_is_prop (A : Type) [is_prop A] : decidable_eq A := λa a', decidable.inl !is_prop.elim definition decidable_eq_sigma [instance] {A : Type} (B : A → Type) [HA : decidable_eq A] [HB : Πa, decidable_eq (B a)] : decidable_eq (Σa, B a) := begin intro v v', induction v with a b, induction v' with a' b', cases HA a a' with p np, { induction p, cases HB a b b' with q nq, induction q, exact decidable.inl idp, apply decidable.inr, intro p, apply nq, apply @eq_of_pathover_idp A B, exact change_path !is_prop.elim p..2 }, { apply decidable.inr, intro p, apply np, exact p..1 } end open sum definition decidable_eq_sum [instance] (A B : Type) [HA : decidable_eq A] [HB : decidable_eq B] : decidable_eq (A ⊎ B) := begin intro v v', induction v with a b: induction v' with a' b', { cases HA a a' with p np, { exact decidable.inl (ap sum.inl p) }, { apply decidable.inr, intro p, apply np, exact down (sum.encode p) }}, { apply decidable.inr, intro p, cases p }, { apply decidable.inr, intro p, cases p }, { cases HB b b' with p np, { exact decidable.inl (ap sum.inr p) }, { apply decidable.inr, intro p, apply np, exact down (sum.encode p) }}, end end decidable attribute sum.is_trunc_sum [instance] [priority 1480] definition tsum [constructor] {n : ℕ₋₂} (A B : (n.+2)-Type) : (n.+2)-Type := trunctype.mk (A + B) _ infixr `+t`:25 := tsum
43d9fb78e6287a90c39d291c429972c6408de447
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/measure_theory/integral/set_to_l1.lean
be123172e267348b3bb2328e76e44a0b7823bfed
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
85,715
lean
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov, Sébastien Gouëzel, Rémy Degenne -/ import measure_theory.function.simple_func_dense /-! # Extension of a linear function from indicators to L1 Let `T : set α → E →L[ℝ] F` be additive for measurable sets with finite measure, in the sense that for `s, t` two such sets, `s ∩ t = ∅ → T (s ∪ t) = T s + T t`. `T` is akin to a bilinear map on `set α × E`, or a linear map on indicator functions. This file constructs an extension of `T` to integrable simple functions, which are finite sums of indicators of measurable sets with finite measure, then to integrable functions, which are limits of integrable simple functions. The main result is a continuous linear map `(α →₁[μ] E) →L[ℝ] F`. This extension process is used to define the Bochner integral in the `measure_theory.integral.bochner` file and the conditional expectation of an integrable function in `measure_theory.function.conditional_expectation`. ## Main Definitions - `fin_meas_additive μ T`: the property that `T` is additive on measurable sets with finite measure. For two such sets, `s ∩ t = ∅ → T (s ∪ t) = T s + T t`. - `dominated_fin_meas_additive μ T C`: `fin_meas_additive μ T ∧ ∀ s, ∥T s∥ ≤ C * (μ s).to_real`. This is the property needed to perform the extension from indicators to L1. - `set_to_L1 (hT : dominated_fin_meas_additive μ T C) : (α →₁[μ] E) →L[ℝ] F`: the extension of `T` from indicators to L1. - `set_to_fun μ T (hT : dominated_fin_meas_additive μ T C) (f : α → E) : F`: a version of the extension which applies to functions (with value 0 if the function is not integrable). ## Properties For most properties of `set_to_fun`, we provide two lemmas. One version uses hypotheses valid on all sets, like `T = T'`, and a second version which uses a primed name uses hypotheses on measurable sets with finite measure, like `∀ s, measurable_set s → μ s < ∞ → T s = T' s`. The lemmas listed here don't show all hypotheses. Refer to the actual lemmas for details. Linearity: - `set_to_fun_zero_left : set_to_fun μ 0 hT f = 0` - `set_to_fun_add_left : set_to_fun μ (T + T') _ f = set_to_fun μ T hT f + set_to_fun μ T' hT' f` - `set_to_fun_smul_left : set_to_fun μ (λ s, c • (T s)) (hT.smul c) f = c • set_to_fun μ T hT f` - `set_to_fun_zero : set_to_fun μ T hT (0 : α → E) = 0` - `set_to_fun_neg : set_to_fun μ T hT (-f) = - set_to_fun μ T hT f` If `f` and `g` are integrable: - `set_to_fun_add : set_to_fun μ T hT (f + g) = set_to_fun μ T hT f + set_to_fun μ T hT g` - `set_to_fun_sub : set_to_fun μ T hT (f - g) = set_to_fun μ T hT f - set_to_fun μ T hT g` If `T` is verifies `∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x`: - `set_to_fun_smul : set_to_fun μ T hT (c • f) = c • set_to_fun μ T hT f` Other: - `set_to_fun_congr_ae (h : f =ᵐ[μ] g) : set_to_fun μ T hT f = set_to_fun μ T hT g` - `set_to_fun_measure_zero (h : μ = 0) : set_to_fun μ T hT f = 0` If the space is a `normed_lattice_add_comm_group` and `T` is such that `0 ≤ T s x` for `0 ≤ x`, we also prove order-related properties: - `set_to_fun_mono_left (h : ∀ s x, T s x ≤ T' s x) : set_to_fun μ T hT f ≤ set_to_fun μ T' hT' f` - `set_to_fun_nonneg (hf : 0 ≤ᵐ[μ] f) : 0 ≤ set_to_fun μ T hT f` - `set_to_fun_mono (hfg : f ≤ᵐ[μ] g) : set_to_fun μ T hT f ≤ set_to_fun μ T hT g` ## Implementation notes The starting object `T : set α → E →L[ℝ] F` matters only through its restriction on measurable sets with finite measure. Its value on other sets is ignored. The extension step from integrable simple functions to L1 relies on a `second_countable_topology` assumption. Without it, we could only extend to `ae_fin_strongly_measurable` functions. (TODO: this might be worth doing?) -/ noncomputable theory open_locale classical topological_space big_operators nnreal ennreal measure_theory pointwise open set filter topological_space ennreal emetric namespace measure_theory variables {α E F F' G 𝕜 : Type*} {p : ℝ≥0∞} [normed_group E] [measurable_space E] [normed_space ℝ E] [normed_group F] [normed_space ℝ F] [normed_group F'] [normed_space ℝ F'] [normed_group G] [measurable_space G] {m : measurable_space α} {μ : measure α} local infixr ` →ₛ `:25 := simple_func open finset section fin_meas_additive /-- A set function is `fin_meas_additive` if its value on the union of two disjoint measurable sets with finite measure is the sum of its values on each set. -/ def fin_meas_additive {β} [add_monoid β] {m : measurable_space α} (μ : measure α) (T : set α → β) : Prop := ∀ s t, measurable_set s → measurable_set t → μ s ≠ ∞ → μ t ≠ ∞ → s ∩ t = ∅ → T (s ∪ t) = T s + T t namespace fin_meas_additive variables {β : Type*} [add_comm_monoid β] {T T' : set α → β} lemma zero : fin_meas_additive μ (0 : set α → β) := λ s t hs ht hμs hμt hst, by simp lemma add (hT : fin_meas_additive μ T) (hT' : fin_meas_additive μ T') : fin_meas_additive μ (T + T') := begin intros s t hs ht hμs hμt hst, simp only [hT s t hs ht hμs hμt hst, hT' s t hs ht hμs hμt hst, pi.add_apply], abel, end lemma smul [monoid 𝕜] [distrib_mul_action 𝕜 β] (hT : fin_meas_additive μ T) (c : 𝕜) : fin_meas_additive μ (λ s, c • (T s)) := λ s t hs ht hμs hμt hst, by simp [hT s t hs ht hμs hμt hst] lemma of_eq_top_imp_eq_top {μ' : measure α} (h : ∀ s, measurable_set s → μ s = ∞ → μ' s = ∞) (hT : fin_meas_additive μ T) : fin_meas_additive μ' T := λ s t hs ht hμ's hμ't hst, hT s t hs ht (mt (h s hs) hμ's) (mt (h t ht) hμ't) hst lemma of_smul_measure (c : ℝ≥0∞) (hc_ne_top : c ≠ ∞) (hT : fin_meas_additive (c • μ) T) : fin_meas_additive μ T := begin refine of_eq_top_imp_eq_top (λ s hs hμs, _) hT, rw [measure.smul_apply, with_top.mul_eq_top_iff] at hμs, simp only [hc_ne_top, or_false, ne.def, false_and] at hμs, exact hμs.2, end lemma smul_measure (c : ℝ≥0∞) (hc_ne_zero : c ≠ 0) (hT : fin_meas_additive μ T) : fin_meas_additive (c • μ) T := begin refine of_eq_top_imp_eq_top (λ s hs hμs, _) hT, rw [measure.smul_apply, with_top.mul_eq_top_iff], simp only [hc_ne_zero, true_and, ne.def, not_false_iff], exact or.inl hμs, end lemma smul_measure_iff (c : ℝ≥0∞) (hc_ne_zero : c ≠ 0) (hc_ne_top : c ≠ ∞) : fin_meas_additive (c • μ) T ↔ fin_meas_additive μ T := ⟨λ hT, of_smul_measure c hc_ne_top hT, λ hT, smul_measure c hc_ne_zero hT⟩ lemma map_empty_eq_zero {β} [add_cancel_monoid β] {T : set α → β} (hT : fin_meas_additive μ T) : T ∅ = 0 := begin have h_empty : μ ∅ ≠ ∞, from (measure_empty.le.trans_lt ennreal.coe_lt_top).ne, specialize hT ∅ ∅ measurable_set.empty measurable_set.empty h_empty h_empty (set.inter_empty ∅), rw set.union_empty at hT, nth_rewrite 0 ← add_zero (T ∅) at hT, exact (add_left_cancel hT).symm, end lemma map_Union_fin_meas_set_eq_sum (T : set α → β) (T_empty : T ∅ = 0) (h_add : fin_meas_additive μ T) {ι} (S : ι → set α) (sι : finset ι) (hS_meas : ∀ i, measurable_set (S i)) (hSp : ∀ i ∈ sι, μ (S i) ≠ ∞) (h_disj : ∀ i j ∈ sι, i ≠ j → disjoint (S i) (S j)) : T (⋃ i ∈ sι, S i) = ∑ i in sι, T (S i) := begin revert hSp h_disj, refine finset.induction_on sι _ _, { simp only [finset.not_mem_empty, forall_false_left, Union_false, Union_empty, sum_empty, forall_2_true_iff, implies_true_iff, forall_true_left, not_false_iff, T_empty], }, intros a s has h hps h_disj, rw [finset.sum_insert has, ← h], swap, { exact λ i hi, hps i (finset.mem_insert_of_mem hi), }, swap, { exact λ i hi j hj hij, h_disj i (finset.mem_insert_of_mem hi) j (finset.mem_insert_of_mem hj) hij, }, rw ← h_add (S a) (⋃ i ∈ s, S i) (hS_meas a) (measurable_set_bUnion _ (λ i _, hS_meas i)) (hps a (finset.mem_insert_self a s)), { congr, convert finset.supr_insert a s S, }, { exact ((measure_bUnion_finset_le _ _).trans_lt $ ennreal.sum_lt_top $ λ i hi, hps i $ finset.mem_insert_of_mem hi).ne, }, { simp_rw set.inter_Union, refine Union_eq_empty.mpr (λ i, Union_eq_empty.mpr (λ hi, _)), rw ← set.disjoint_iff_inter_eq_empty, refine h_disj a (finset.mem_insert_self a s) i (finset.mem_insert_of_mem hi) (λ hai, _), rw ← hai at hi, exact has hi, }, end end fin_meas_additive /-- A `fin_meas_additive` set function whose norm on every set is less than the measure of the set (up to a multiplicative constant). -/ def dominated_fin_meas_additive {β} [semi_normed_group β] {m : measurable_space α} (μ : measure α) (T : set α → β) (C : ℝ) : Prop := fin_meas_additive μ T ∧ ∀ s, measurable_set s → μ s < ∞ → ∥T s∥ ≤ C * (μ s).to_real namespace dominated_fin_meas_additive variables {β : Type*} [semi_normed_group β] {T T' : set α → β} {C C' : ℝ} lemma zero {m : measurable_space α} (μ : measure α) (hC : 0 ≤ C) : dominated_fin_meas_additive μ (0 : set α → β) C := begin refine ⟨fin_meas_additive.zero, λ s hs hμs, _⟩, rw [pi.zero_apply, norm_zero], exact mul_nonneg hC to_real_nonneg, end lemma eq_zero_of_measure_zero {β : Type*} [normed_group β] {T : set α → β} {C : ℝ} (hT : dominated_fin_meas_additive μ T C) {s : set α} (hs : measurable_set s) (hs_zero : μ s = 0) : T s = 0 := begin refine norm_eq_zero.mp _, refine ((hT.2 s hs (by simp [hs_zero])).trans (le_of_eq _)).antisymm (norm_nonneg _), rw [hs_zero, ennreal.zero_to_real, mul_zero], end lemma eq_zero {β : Type*} [normed_group β] {T : set α → β} {C : ℝ} {m : measurable_space α} (hT : dominated_fin_meas_additive (0 : measure α) T C) {s : set α} (hs : measurable_set s) : T s = 0 := eq_zero_of_measure_zero hT hs (by simp only [measure.coe_zero, pi.zero_apply]) lemma add (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') : dominated_fin_meas_additive μ (T + T') (C + C') := begin refine ⟨hT.1.add hT'.1, λ s hs hμs, _⟩, rw [pi.add_apply, add_mul], exact (norm_add_le _ _).trans (add_le_add (hT.2 s hs hμs) (hT'.2 s hs hμs)), end lemma smul [normed_field 𝕜] [normed_space 𝕜 β] (hT : dominated_fin_meas_additive μ T C) (c : 𝕜) : dominated_fin_meas_additive μ (λ s, c • (T s)) (∥c∥ * C) := begin refine ⟨hT.1.smul c, λ s hs hμs, _⟩, dsimp only, rw [norm_smul, mul_assoc], exact mul_le_mul le_rfl (hT.2 s hs hμs) (norm_nonneg _) (norm_nonneg _), end lemma of_measure_le {μ' : measure α} (h : μ ≤ μ') (hT : dominated_fin_meas_additive μ T C) (hC : 0 ≤ C) : dominated_fin_meas_additive μ' T C := begin have h' : ∀ s, measurable_set s → μ s = ∞ → μ' s = ∞, { intros s hs hμs, rw [eq_top_iff, ← hμs], exact h s hs, }, refine ⟨hT.1.of_eq_top_imp_eq_top h', λ s hs hμ's, _⟩, have hμs : μ s < ∞, from (h s hs).trans_lt hμ's, refine (hT.2 s hs hμs).trans (mul_le_mul le_rfl _ ennreal.to_real_nonneg hC), rw to_real_le_to_real hμs.ne hμ's.ne, exact h s hs, end lemma add_measure_right {m : measurable_space α} (μ ν : measure α) (hT : dominated_fin_meas_additive μ T C) (hC : 0 ≤ C) : dominated_fin_meas_additive (μ + ν) T C := of_measure_le (measure.le_add_right le_rfl) hT hC lemma add_measure_left {m : measurable_space α} (μ ν : measure α) (hT : dominated_fin_meas_additive ν T C) (hC : 0 ≤ C) : dominated_fin_meas_additive (μ + ν) T C := of_measure_le (measure.le_add_left le_rfl) hT hC lemma of_smul_measure (c : ℝ≥0∞) (hc_ne_top : c ≠ ∞) (hT : dominated_fin_meas_additive (c • μ) T C) : dominated_fin_meas_additive μ T (c.to_real * C) := begin have h : ∀ s, measurable_set s → c • μ s = ∞ → μ s = ∞, { intros s hs hcμs, simp only [hc_ne_top, algebra.id.smul_eq_mul, with_top.mul_eq_top_iff, or_false, ne.def, false_and] at hcμs, exact hcμs.2, }, refine ⟨hT.1.of_eq_top_imp_eq_top h, λ s hs hμs, _⟩, have hcμs : c • μ s ≠ ∞, from mt (h s hs) hμs.ne, rw smul_eq_mul at hcμs, simp_rw [dominated_fin_meas_additive, measure.smul_apply, to_real_mul] at hT, refine (hT.2 s hs hcμs.lt_top).trans (le_of_eq _), ring, end lemma of_measure_le_smul {μ' : measure α} (c : ℝ≥0∞) (hc : c ≠ ∞) (h : μ ≤ c • μ') (hT : dominated_fin_meas_additive μ T C) (hC : 0 ≤ C) : dominated_fin_meas_additive μ' T (c.to_real * C) := (hT.of_measure_le h hC).of_smul_measure c hc end dominated_fin_meas_additive end fin_meas_additive namespace simple_func /-- Extend `set α → (F →L[ℝ] F')` to `(α →ₛ F) → F'`. -/ def set_to_simple_func {m : measurable_space α} (T : set α → F →L[ℝ] F') (f : α →ₛ F) : F' := ∑ x in f.range, T (f ⁻¹' {x}) x @[simp] lemma set_to_simple_func_zero {m : measurable_space α} (f : α →ₛ F) : set_to_simple_func (0 : set α → F →L[ℝ] F') f = 0 := by simp [set_to_simple_func] lemma set_to_simple_func_zero' {T : set α → E →L[ℝ] F'} (h_zero : ∀ s, measurable_set s → μ s < ∞ → T s = 0) (f : α →ₛ E) (hf : integrable f μ) : set_to_simple_func T f = 0 := begin simp_rw set_to_simple_func, refine sum_eq_zero (λ x hx, _), by_cases hx0 : x = 0, { simp [hx0], }, rw [h_zero (f ⁻¹' ({x} : set E)) (measurable_set_fiber _ _) (measure_preimage_lt_top_of_integrable f hf hx0), continuous_linear_map.zero_apply], end @[simp] lemma set_to_simple_func_zero_apply {m : measurable_space α} (T : set α → F →L[ℝ] F') : set_to_simple_func T (0 : α →ₛ F) = 0 := by casesI is_empty_or_nonempty α; simp [set_to_simple_func] lemma set_to_simple_func_eq_sum_filter {m : measurable_space α} (T : set α → F →L[ℝ] F') (f : α →ₛ F) : set_to_simple_func T f = ∑ x in f.range.filter (λ x, x ≠ 0), (T (f ⁻¹' {x})) x := begin symmetry, refine sum_filter_of_ne (λ x hx, mt (λ hx0, _)), rw hx0, exact continuous_linear_map.map_zero _, end lemma map_set_to_simple_func (T : set α → F →L[ℝ] F') (h_add : fin_meas_additive μ T) {f : α →ₛ G} (hf : integrable f μ) {g : G → F} (hg : g 0 = 0) : (f.map g).set_to_simple_func T = ∑ x in f.range, T (f ⁻¹' {x}) (g x) := begin have T_empty : T ∅ = 0, from h_add.map_empty_eq_zero, have hfp : ∀ x ∈ f.range, x ≠ 0 → μ (f ⁻¹' {x}) ≠ ∞, from λ x hx hx0, (measure_preimage_lt_top_of_integrable f hf hx0).ne, simp only [set_to_simple_func, range_map], refine finset.sum_image' _ (assume b hb, _), rcases mem_range.1 hb with ⟨a, rfl⟩, by_cases h0 : g (f a) = 0, { simp_rw h0, rw [continuous_linear_map.map_zero, finset.sum_eq_zero (λ x hx, _)], rw mem_filter at hx, rw [hx.2, continuous_linear_map.map_zero], }, have h_left_eq : T ((map g f) ⁻¹' {g (f a)}) (g (f a)) = T (f ⁻¹' ↑(f.range.filter (λ b, g b = g (f a)))) (g (f a)), { congr, rw map_preimage_singleton, }, rw h_left_eq, have h_left_eq' : T (f ⁻¹' ↑(filter (λ (b : G), g b = g (f a)) f.range)) (g (f a)) = T (⋃ y ∈ (filter (λ (b : G), g b = g (f a)) f.range), f ⁻¹' {y}) (g (f a)), { congr, rw ← finset.set_bUnion_preimage_singleton, }, rw h_left_eq', rw h_add.map_Union_fin_meas_set_eq_sum T T_empty, { simp only [filter_congr_decidable, sum_apply, continuous_linear_map.coe_sum'], refine finset.sum_congr rfl (λ x hx, _), rw mem_filter at hx, rw hx.2, }, { exact λ i, measurable_set_fiber _ _, }, { intros i hi, rw mem_filter at hi, refine hfp i hi.1 (λ hi0, _), rw [hi0, hg] at hi, exact h0 hi.2.symm, }, { intros i j hi hj hij, rw set.disjoint_iff, intros x hx, rw [set.mem_inter_iff, set.mem_preimage, set.mem_preimage, set.mem_singleton_iff, set.mem_singleton_iff] at hx, rw [← hx.1, ← hx.2] at hij, exact absurd rfl hij, }, end lemma set_to_simple_func_congr' (T : set α → E →L[ℝ] F) (h_add : fin_meas_additive μ T) {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) (h : ∀ x y, x ≠ y → T ((f ⁻¹' {x}) ∩ (g ⁻¹' {y})) = 0) : f.set_to_simple_func T = g.set_to_simple_func T := show ((pair f g).map prod.fst).set_to_simple_func T = ((pair f g).map prod.snd).set_to_simple_func T, from begin have h_pair : integrable (f.pair g) μ, from integrable_pair hf hg, rw map_set_to_simple_func T h_add h_pair prod.fst_zero, rw map_set_to_simple_func T h_add h_pair prod.snd_zero, refine finset.sum_congr rfl (λ p hp, _), rcases mem_range.1 hp with ⟨a, rfl⟩, by_cases eq : f a = g a, { dsimp only [pair_apply], rw eq }, { have : T ((pair f g) ⁻¹' {(f a, g a)}) = 0, { have h_eq : T (⇑(f.pair g) ⁻¹' {(f a, g a)}) = T ((f ⁻¹' {f a}) ∩ (g ⁻¹' {g a})), { congr, rw pair_preimage_singleton f g, }, rw h_eq, exact h (f a) (g a) eq, }, simp only [this, continuous_linear_map.zero_apply, pair_apply], }, end lemma set_to_simple_func_congr (T : set α → (E →L[ℝ] F)) (h_zero : ∀ s, measurable_set s → μ s = 0 → T s = 0) (h_add : fin_meas_additive μ T) {f g : α →ₛ E} (hf : integrable f μ) (h : f =ᵐ[μ] g) : f.set_to_simple_func T = g.set_to_simple_func T := begin refine set_to_simple_func_congr' T h_add hf ((integrable_congr h).mp hf) _, refine λ x y hxy, h_zero _ ((measurable_set_fiber f x).inter (measurable_set_fiber g y)) _, rw [eventually_eq, ae_iff] at h, refine measure_mono_null (λ z, _) h, simp_rw [set.mem_inter_iff, set.mem_set_of_eq, set.mem_preimage, set.mem_singleton_iff], intro h, rwa [h.1, h.2], end lemma set_to_simple_func_congr_left (T T' : set α → E →L[ℝ] F) (h : ∀ s, measurable_set s → μ s < ∞ → T s = T' s) (f : α →ₛ E) (hf : integrable f μ) : set_to_simple_func T f = set_to_simple_func T' f := begin simp_rw set_to_simple_func, refine sum_congr rfl (λ x hx, _), by_cases hx0 : x = 0, { simp [hx0], }, { rw h (f ⁻¹' {x}) (simple_func.measurable_set_fiber _ _) (simple_func.measure_preimage_lt_top_of_integrable _ hf hx0), }, end lemma set_to_simple_func_add_left {m : measurable_space α} (T T' : set α → F →L[ℝ] F') {f : α →ₛ F} : set_to_simple_func (T + T') f = set_to_simple_func T f + set_to_simple_func T' f := begin simp_rw [set_to_simple_func, pi.add_apply], push_cast, simp_rw [pi.add_apply, sum_add_distrib], end lemma set_to_simple_func_add_left' (T T' T'' : set α → E →L[ℝ] F) (h_add : ∀ s, measurable_set s → μ s < ∞ → T'' s = T s + T' s) {f : α →ₛ E} (hf : integrable f μ) : set_to_simple_func (T'') f = set_to_simple_func T f + set_to_simple_func T' f := begin simp_rw [set_to_simple_func_eq_sum_filter], suffices : ∀ x ∈ filter (λ (x : E), x ≠ 0) f.range, T'' (f ⁻¹' {x}) = T (f ⁻¹' {x}) + T' (f ⁻¹' {x}), { rw ← sum_add_distrib, refine finset.sum_congr rfl (λ x hx, _), rw this x hx, push_cast, rw pi.add_apply, }, intros x hx, refine h_add (f ⁻¹' {x}) (measurable_set_preimage _ _) (measure_preimage_lt_top_of_integrable _ hf _), rw mem_filter at hx, exact hx.2, end lemma set_to_simple_func_smul_left {m : measurable_space α} (T : set α → F →L[ℝ] F') (c : ℝ) (f : α →ₛ F) : set_to_simple_func (λ s, c • (T s)) f = c • set_to_simple_func T f := by simp_rw [set_to_simple_func, continuous_linear_map.smul_apply, smul_sum] lemma set_to_simple_func_smul_left' (T T' : set α → E →L[ℝ] F') (c : ℝ) (h_smul : ∀ s, measurable_set s → μ s < ∞ → T' s = c • (T s)) {f : α →ₛ E} (hf : integrable f μ) : set_to_simple_func T' f = c • set_to_simple_func T f := begin simp_rw [set_to_simple_func_eq_sum_filter], suffices : ∀ x ∈ filter (λ (x : E), x ≠ 0) f.range, T' (f ⁻¹' {x}) = c • (T (f ⁻¹' {x})), { rw smul_sum, refine finset.sum_congr rfl (λ x hx, _), rw this x hx, refl, }, intros x hx, refine h_smul (f ⁻¹' {x}) (measurable_set_preimage _ _) (measure_preimage_lt_top_of_integrable _ hf _), rw mem_filter at hx, exact hx.2, end lemma set_to_simple_func_add (T : set α → E →L[ℝ] F) (h_add : fin_meas_additive μ T) {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) : set_to_simple_func T (f + g) = set_to_simple_func T f + set_to_simple_func T g := have hp_pair : integrable (f.pair g) μ, from integrable_pair hf hg, calc set_to_simple_func T (f + g) = ∑ x in (pair f g).range, T ((pair f g) ⁻¹' {x}) (x.fst + x.snd) : by { rw [add_eq_map₂, map_set_to_simple_func T h_add hp_pair], simp, } ... = ∑ x in (pair f g).range, (T ((pair f g) ⁻¹' {x}) x.fst + T ((pair f g) ⁻¹' {x}) x.snd) : finset.sum_congr rfl $ assume a ha, continuous_linear_map.map_add _ _ _ ... = ∑ x in (pair f g).range, T ((pair f g) ⁻¹' {x}) x.fst + ∑ x in (pair f g).range, T ((pair f g) ⁻¹' {x}) x.snd : by rw finset.sum_add_distrib ... = ((pair f g).map prod.fst).set_to_simple_func T + ((pair f g).map prod.snd).set_to_simple_func T : by rw [map_set_to_simple_func T h_add hp_pair prod.snd_zero, map_set_to_simple_func T h_add hp_pair prod.fst_zero] lemma set_to_simple_func_neg (T : set α → E →L[ℝ] F) (h_add : fin_meas_additive μ T) {f : α →ₛ E} (hf : integrable f μ) : set_to_simple_func T (-f) = - set_to_simple_func T f := calc set_to_simple_func T (-f) = set_to_simple_func T (f.map (has_neg.neg)) : rfl ... = - set_to_simple_func T f : begin rw [map_set_to_simple_func T h_add hf neg_zero, set_to_simple_func, ← sum_neg_distrib], exact finset.sum_congr rfl (λ x h, continuous_linear_map.map_neg _ _), end lemma set_to_simple_func_sub (T : set α → E →L[ℝ] F) (h_add : fin_meas_additive μ T) {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) : set_to_simple_func T (f - g) = set_to_simple_func T f - set_to_simple_func T g := begin rw [sub_eq_add_neg, set_to_simple_func_add T h_add hf, set_to_simple_func_neg T h_add hg, sub_eq_add_neg], rw integrable_iff at hg ⊢, intros x hx_ne, change μ ((has_neg.neg ∘ g) ⁻¹' {x}) < ∞, rw [preimage_comp, neg_preimage, neg_singleton], refine hg (-x) _, simp [hx_ne], end lemma set_to_simple_func_smul_real (T : set α → E →L[ℝ] F) (h_add : fin_meas_additive μ T) (c : ℝ) {f : α →ₛ E} (hf : integrable f μ) : set_to_simple_func T (c • f) = c • set_to_simple_func T f := calc set_to_simple_func T (c • f) = ∑ x in f.range, T (f ⁻¹' {x}) (c • x) : by { rw [smul_eq_map c f, map_set_to_simple_func T h_add hf], rw smul_zero, } ... = ∑ x in f.range, c • (T (f ⁻¹' {x}) x) : finset.sum_congr rfl $ λ b hb, by { rw continuous_linear_map.map_smul (T (f ⁻¹' {b})) c b, } ... = c • set_to_simple_func T f : by simp only [set_to_simple_func, smul_sum, smul_smul, mul_comm] lemma set_to_simple_func_smul {E} [measurable_space E] [normed_group E] [normed_field 𝕜] [normed_space 𝕜 E] [normed_space ℝ E] [normed_space 𝕜 F] (T : set α → E →L[ℝ] F) (h_add : fin_meas_additive μ T) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) (c : 𝕜) {f : α →ₛ E} (hf : integrable f μ) : set_to_simple_func T (c • f) = c • set_to_simple_func T f := calc set_to_simple_func T (c • f) = ∑ x in f.range, T (f ⁻¹' {x}) (c • x) : by { rw [smul_eq_map c f, map_set_to_simple_func T h_add hf], rw smul_zero, } ... = ∑ x in f.range, c • (T (f ⁻¹' {x}) x) : finset.sum_congr rfl $ λ b hb, by { rw h_smul, } ... = c • set_to_simple_func T f : by simp only [set_to_simple_func, smul_sum, smul_smul, mul_comm] section order variables {G' G'' : Type*} [normed_lattice_add_comm_group G''] [normed_space ℝ G''] [normed_lattice_add_comm_group G'] [normed_space ℝ G'] lemma set_to_simple_func_mono_left {m : measurable_space α} (T T' : set α → F →L[ℝ] G'') (hTT' : ∀ s x, T s x ≤ T' s x) (f : α →ₛ F) : set_to_simple_func T f ≤ set_to_simple_func T' f := by { simp_rw set_to_simple_func, exact sum_le_sum (λ i hi, hTT' _ i), } lemma set_to_simple_func_mono_left' (T T' : set α → E →L[ℝ] G'') (hTT' : ∀ s, measurable_set s → μ s < ∞ → ∀ x, T s x ≤ T' s x) (f : α →ₛ E) (hf : integrable f μ) : set_to_simple_func T f ≤ set_to_simple_func T' f := begin refine sum_le_sum (λ i hi, _), by_cases h0 : i = 0, { simp [h0], }, { exact hTT' _ (measurable_set_fiber _ _) (measure_preimage_lt_top_of_integrable _ hf h0) i, } end lemma set_to_simple_func_nonneg {m : measurable_space α} (T : set α → G' →L[ℝ] G'') (hT_nonneg : ∀ s x, 0 ≤ x → 0 ≤ T s x) (f : α →ₛ G') (hf : 0 ≤ f) : 0 ≤ set_to_simple_func T f := begin refine sum_nonneg (λ i hi, hT_nonneg _ i _), rw mem_range at hi, obtain ⟨y, hy⟩ := set.mem_range.mp hi, rw ← hy, refine le_trans _ (hf y), simp, end lemma set_to_simple_func_nonneg' [measurable_space G'] (T : set α → G' →L[ℝ] G'') (hT_nonneg : ∀ s, measurable_set s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) (f : α →ₛ G') (hf : 0 ≤ f) (hfi : integrable f μ) : 0 ≤ set_to_simple_func T f := begin refine sum_nonneg (λ i hi, _), by_cases h0 : i = 0, { simp [h0], }, refine hT_nonneg _ (measurable_set_fiber _ _) (measure_preimage_lt_top_of_integrable _ hfi h0) i _, rw mem_range at hi, obtain ⟨y, hy⟩ := set.mem_range.mp hi, rw ← hy, convert (hf y), end lemma set_to_simple_func_mono [measurable_space G'] [borel_space G'] [second_countable_topology G'] {T : set α → G' →L[ℝ] G''} (h_add : fin_meas_additive μ T) (hT_nonneg : ∀ s, measurable_set s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f g : α →ₛ G'} (hfi : integrable f μ) (hgi : integrable g μ) (hfg : f ≤ g) : set_to_simple_func T f ≤ set_to_simple_func T g := begin rw [← sub_nonneg, ← set_to_simple_func_sub T h_add hgi hfi], refine set_to_simple_func_nonneg' T hT_nonneg _ _ (hgi.sub hfi), intro x, simp only [coe_sub, sub_nonneg, coe_zero, pi.zero_apply, pi.sub_apply], exact hfg x, end end order lemma norm_set_to_simple_func_le_sum_op_norm {m : measurable_space α} (T : set α → F' →L[ℝ] F) (f : α →ₛ F') : ∥f.set_to_simple_func T∥ ≤ ∑ x in f.range, ∥T (f ⁻¹' {x})∥ * ∥x∥ := calc ∥∑ x in f.range, T (f ⁻¹' {x}) x∥ ≤ ∑ x in f.range, ∥T (f ⁻¹' {x}) x∥ : norm_sum_le _ _ ... ≤ ∑ x in f.range, ∥T (f ⁻¹' {x})∥ * ∥x∥ : by { refine finset.sum_le_sum (λb hb, _), simp_rw continuous_linear_map.le_op_norm, } lemma norm_set_to_simple_func_le_sum_mul_norm (T : set α → F →L[ℝ] F') {C : ℝ} (hT_norm : ∀ s, measurable_set s → ∥T s∥ ≤ C * (μ s).to_real) (f : α →ₛ F) : ∥f.set_to_simple_func T∥ ≤ C * ∑ x in f.range, (μ (f ⁻¹' {x})).to_real * ∥x∥ := calc ∥f.set_to_simple_func T∥ ≤ ∑ x in f.range, ∥T (f ⁻¹' {x})∥ * ∥x∥ : norm_set_to_simple_func_le_sum_op_norm T f ... ≤ ∑ x in f.range, C * (μ (f ⁻¹' {x})).to_real * ∥x∥ : begin refine finset.sum_le_sum (λ b hb, _), by_cases hb : ∥b∥ = 0, { rw hb, simp, }, rw _root_.mul_le_mul_right _, { exact hT_norm _ (simple_func.measurable_set_fiber _ _), }, { exact lt_of_le_of_ne (norm_nonneg _) (ne.symm hb), }, end ... ≤ C * ∑ x in f.range, (μ (f ⁻¹' {x})).to_real * ∥x∥ : by simp_rw [mul_sum, ← mul_assoc] lemma norm_set_to_simple_func_le_sum_mul_norm_of_integrable (T : set α → E →L[ℝ] F') {C : ℝ} (hT_norm : ∀ s, measurable_set s → μ s < ∞ → ∥T s∥ ≤ C * (μ s).to_real) (f : α →ₛ E) (hf : integrable f μ) : ∥f.set_to_simple_func T∥ ≤ C * ∑ x in f.range, (μ (f ⁻¹' {x})).to_real * ∥x∥ := calc ∥f.set_to_simple_func T∥ ≤ ∑ x in f.range, ∥T (f ⁻¹' {x})∥ * ∥x∥ : norm_set_to_simple_func_le_sum_op_norm T f ... ≤ ∑ x in f.range, C * (μ (f ⁻¹' {x})).to_real * ∥x∥ : begin refine finset.sum_le_sum (λ b hb, _), by_cases hb : ∥b∥ = 0, { rw hb, simp, }, rw _root_.mul_le_mul_right _, { refine hT_norm _ (simple_func.measurable_set_fiber _ _) (simple_func.measure_preimage_lt_top_of_integrable _ hf _), rwa norm_eq_zero at hb, }, { exact lt_of_le_of_ne (norm_nonneg _) (ne.symm hb), }, end ... ≤ C * ∑ x in f.range, (μ (f ⁻¹' {x})).to_real * ∥x∥ : by simp_rw [mul_sum, ← mul_assoc] lemma set_to_simple_func_indicator (T : set α → F →L[ℝ] F') (hT_empty : T ∅ = 0) {m : measurable_space α} {s : set α} (hs : measurable_set s) (x : F) : simple_func.set_to_simple_func T (simple_func.piecewise s hs (simple_func.const α x) (simple_func.const α 0)) = T s x := begin by_cases hs_empty : s = ∅, { simp only [hs_empty, hT_empty, continuous_linear_map.zero_apply, piecewise_empty, const_zero, set_to_simple_func_zero_apply], }, by_cases hs_univ : s = univ, { casesI hα : is_empty_or_nonempty α, { refine absurd _ hs_empty, haveI : subsingleton (set α), by { unfold set, apply_instance, }, exact subsingleton.elim s ∅, }, simp [hs_univ, set_to_simple_func], }, simp_rw set_to_simple_func, rw [← ne.def, set.ne_empty_iff_nonempty] at hs_empty, rw range_indicator hs hs_empty hs_univ, by_cases hx0 : x = 0, { simp_rw hx0, simp, }, rw sum_insert, swap, { rw finset.mem_singleton, exact hx0, }, rw [sum_singleton, (T _).map_zero, add_zero], congr, simp only [coe_piecewise, piecewise_eq_indicator, coe_const, pi.const_zero, piecewise_eq_indicator], rw [indicator_preimage, preimage_const_of_mem], swap, { exact set.mem_singleton x, }, rw [← pi.const_zero, preimage_const_of_not_mem], swap, { rw set.mem_singleton_iff, exact ne.symm hx0, }, simp, end lemma set_to_simple_func_const' [nonempty α] (T : set α → F →L[ℝ] F') (x : F) {m : measurable_space α} : simple_func.set_to_simple_func T (simple_func.const α x) = T univ x := by simp only [set_to_simple_func, range_const, set.mem_singleton, preimage_const_of_mem, sum_singleton, coe_const] lemma set_to_simple_func_const (T : set α → F →L[ℝ] F') (hT_empty : T ∅ = 0) (x : F) {m : measurable_space α} : simple_func.set_to_simple_func T (simple_func.const α x) = T univ x := begin casesI hα : is_empty_or_nonempty α, { have h_univ_empty : (univ : set α) = ∅, { haveI : unique (set α) := unique_empty, exact subsingleton.elim (univ : set α) (∅ : set α), }, rw [h_univ_empty, hT_empty], simp only [set_to_simple_func, continuous_linear_map.zero_apply, sum_empty, range_eq_empty_of_is_empty], }, { exact set_to_simple_func_const' T x, }, end end simple_func namespace L1 open ae_eq_fun Lp.simple_func Lp variables {α E μ} namespace simple_func lemma norm_eq_sum_mul [second_countable_topology G] [borel_space G] (f : α →₁ₛ[μ] G) : ∥f∥ = ∑ x in (to_simple_func f).range, (μ ((to_simple_func f) ⁻¹' {x})).to_real * ∥x∥ := begin rw [norm_to_simple_func, snorm_one_eq_lintegral_nnnorm], have h_eq := simple_func.map_apply (λ x, (nnnorm x : ℝ≥0∞)) (to_simple_func f), dsimp only at h_eq, simp_rw ← h_eq, rw [simple_func.lintegral_eq_lintegral, simple_func.map_lintegral, ennreal.to_real_sum], { congr, ext1 x, rw [ennreal.to_real_mul, mul_comm, ← of_real_norm_eq_coe_nnnorm, ennreal.to_real_of_real (norm_nonneg _)], }, { intros x hx, by_cases hx0 : x = 0, { rw hx0, simp, }, { exact ennreal.mul_ne_top ennreal.coe_ne_top (simple_func.measure_preimage_lt_top_of_integrable _ (simple_func.integrable f) hx0).ne } } end section set_to_L1s variables [second_countable_topology E] [borel_space E] [normed_field 𝕜] [normed_space 𝕜 E] local attribute [instance] Lp.simple_func.module local attribute [instance] Lp.simple_func.normed_space /-- Extend `set α → (E →L[ℝ] F')` to `(α →₁ₛ[μ] E) → F'`. -/ def set_to_L1s (T : set α → E →L[ℝ] F) (f : α →₁ₛ[μ] E) : F := (to_simple_func f).set_to_simple_func T lemma set_to_L1s_eq_set_to_simple_func (T : set α → E →L[ℝ] F) (f : α →₁ₛ[μ] E) : set_to_L1s T f = (to_simple_func f).set_to_simple_func T := rfl @[simp] lemma set_to_L1s_zero_left (f : α →₁ₛ[μ] E) : set_to_L1s (0 : set α → E →L[ℝ] F) f = 0 := simple_func.set_to_simple_func_zero _ lemma set_to_L1s_zero_left' {T : set α → E →L[ℝ] F} (h_zero : ∀ s, measurable_set s → μ s < ∞ → T s = 0) (f : α →₁ₛ[μ] E) : set_to_L1s T f = 0 := simple_func.set_to_simple_func_zero' h_zero _ (simple_func.integrable f) lemma set_to_L1s_congr (T : set α → E →L[ℝ] F) (h_zero : ∀ s, measurable_set s → μ s = 0 → T s = 0) (h_add : fin_meas_additive μ T) {f g : α →₁ₛ[μ] E} (h : to_simple_func f =ᵐ[μ] to_simple_func g) : set_to_L1s T f = set_to_L1s T g := simple_func.set_to_simple_func_congr T h_zero h_add (simple_func.integrable f) h lemma set_to_L1s_congr_left (T T' : set α → E →L[ℝ] F) (h : ∀ s, measurable_set s → μ s < ∞ → T s = T' s) (f : α →₁ₛ[μ] E) : set_to_L1s T f = set_to_L1s T' f := simple_func.set_to_simple_func_congr_left T T' h (simple_func.to_simple_func f) (simple_func.integrable f) /-- `set_to_L1s` does not change if we replace the measure `μ` by `μ'` with `μ ≪ μ'`. The statement uses two functions `f` and `f'` because they have to belong to different types, but morally these are the same function (we have `f =ᵐ[μ] f'`). -/ lemma set_to_L1s_congr_measure {μ' : measure α} (T : set α → E →L[ℝ] F) (h_zero : ∀ s, measurable_set s → μ s = 0 → T s = 0) (h_add : fin_meas_additive μ T) (hμ : μ ≪ μ') (f : α →₁ₛ[μ] E) (f' : α →₁ₛ[μ'] E) (h : f =ᵐ[μ] f') : set_to_L1s T f = set_to_L1s T f' := begin refine simple_func.set_to_simple_func_congr T h_zero h_add (simple_func.integrable f) _, refine (to_simple_func_eq_to_fun f).trans _, suffices : f' =ᵐ[μ] ⇑(simple_func.to_simple_func f'), from h.trans this, have goal' : f' =ᵐ[μ'] simple_func.to_simple_func f', from (to_simple_func_eq_to_fun f').symm, exact hμ.ae_eq goal', end lemma set_to_L1s_add_left (T T' : set α → E →L[ℝ] F) (f : α →₁ₛ[μ] E) : set_to_L1s (T + T') f = set_to_L1s T f + set_to_L1s T' f := simple_func.set_to_simple_func_add_left T T' lemma set_to_L1s_add_left' (T T' T'' : set α → E →L[ℝ] F) (h_add : ∀ s, measurable_set s → μ s < ∞ → T'' s = T s + T' s) (f : α →₁ₛ[μ] E) : set_to_L1s T'' f = set_to_L1s T f + set_to_L1s T' f := simple_func.set_to_simple_func_add_left' T T' T'' h_add (simple_func.integrable f) lemma set_to_L1s_smul_left (T : set α → E →L[ℝ] F) (c : ℝ) (f : α →₁ₛ[μ] E) : set_to_L1s (λ s, c • (T s)) f = c • set_to_L1s T f := simple_func.set_to_simple_func_smul_left T c _ lemma set_to_L1s_smul_left' (T T' : set α → E →L[ℝ] F) (c : ℝ) (h_smul : ∀ s, measurable_set s → μ s < ∞ → T' s = c • (T s)) (f : α →₁ₛ[μ] E) : set_to_L1s T' f = c • set_to_L1s T f := simple_func.set_to_simple_func_smul_left' T T' c h_smul (simple_func.integrable f) lemma set_to_L1s_add (T : set α → E →L[ℝ] F) (h_zero : ∀ s, measurable_set s → μ s = 0 → T s = 0) (h_add : fin_meas_additive μ T) (f g : α →₁ₛ[μ] E) : set_to_L1s T (f + g) = set_to_L1s T f + set_to_L1s T g := begin simp_rw set_to_L1s, rw ← simple_func.set_to_simple_func_add T h_add (simple_func.integrable f) (simple_func.integrable g), exact simple_func.set_to_simple_func_congr T h_zero h_add (simple_func.integrable _) (add_to_simple_func f g), end lemma set_to_L1s_neg {T : set α → E →L[ℝ] F} (h_zero : ∀ s, measurable_set s → μ s = 0 → T s = 0) (h_add : fin_meas_additive μ T) (f : α →₁ₛ[μ] E) : set_to_L1s T (-f) = - set_to_L1s T f := begin simp_rw set_to_L1s, have : simple_func.to_simple_func (-f) =ᵐ[μ] ⇑(-simple_func.to_simple_func f), from neg_to_simple_func f, rw simple_func.set_to_simple_func_congr T h_zero h_add (simple_func.integrable _) this, exact simple_func.set_to_simple_func_neg T h_add (simple_func.integrable f), end lemma set_to_L1s_sub {T : set α → E →L[ℝ] F} (h_zero : ∀ s, measurable_set s → μ s = 0 → T s = 0) (h_add : fin_meas_additive μ T) (f g : α →₁ₛ[μ] E) : set_to_L1s T (f - g) = set_to_L1s T f - set_to_L1s T g := by rw [sub_eq_add_neg, set_to_L1s_add T h_zero h_add, set_to_L1s_neg h_zero h_add, sub_eq_add_neg] lemma set_to_L1s_smul_real (T : set α → E →L[ℝ] F) (h_zero : ∀ s, measurable_set s → μ s = 0 → T s = 0) (h_add : fin_meas_additive μ T) (c : ℝ) (f : α →₁ₛ[μ] E) : set_to_L1s T (c • f) = c • set_to_L1s T f := begin simp_rw set_to_L1s, rw ← simple_func.set_to_simple_func_smul_real T h_add c (simple_func.integrable f), refine simple_func.set_to_simple_func_congr T h_zero h_add (simple_func.integrable _) _, exact smul_to_simple_func c f, end lemma set_to_L1s_smul {E} [normed_group E] [measurable_space E] [normed_space ℝ E] [normed_space 𝕜 E] [second_countable_topology E] [borel_space E] [normed_space 𝕜 F] [measurable_space 𝕜] [opens_measurable_space 𝕜] (T : set α → E →L[ℝ] F) (h_zero : ∀ s, measurable_set s → μ s = 0 → T s = 0) (h_add : fin_meas_additive μ T) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) (c : 𝕜) (f : α →₁ₛ[μ] E) : set_to_L1s T (c • f) = c • set_to_L1s T f := begin simp_rw set_to_L1s, rw ← simple_func.set_to_simple_func_smul T h_add h_smul c (simple_func.integrable f), refine simple_func.set_to_simple_func_congr T h_zero h_add (simple_func.integrable _) _, exact smul_to_simple_func c f, end lemma norm_set_to_L1s_le (T : set α → E →L[ℝ] F) {C : ℝ} (hT_norm : ∀ s, measurable_set s → μ s < ∞ → ∥T s∥ ≤ C * (μ s).to_real) (f : α →₁ₛ[μ] E) : ∥set_to_L1s T f∥ ≤ C * ∥f∥ := begin rw [set_to_L1s, norm_eq_sum_mul f], exact simple_func.norm_set_to_simple_func_le_sum_mul_norm_of_integrable T hT_norm _ (simple_func.integrable f), end lemma set_to_L1s_indicator_const {T : set α → E →L[ℝ] F} {s : set α} (h_zero : ∀ s, measurable_set s → μ s = 0 → T s = 0) (h_add : fin_meas_additive μ T) (hs : measurable_set s) (hμs : μ s < ∞) (x : E) : set_to_L1s T (simple_func.indicator_const 1 hs hμs.ne x) = T s x := begin have h_empty : T ∅ = 0, from h_zero _ measurable_set.empty measure_empty, rw set_to_L1s_eq_set_to_simple_func, refine eq.trans _ (simple_func.set_to_simple_func_indicator T h_empty hs x), refine simple_func.set_to_simple_func_congr T h_zero h_add (simple_func.integrable _) _, exact to_simple_func_indicator_const hs hμs.ne x, end lemma set_to_L1s_const [is_finite_measure μ] {T : set α → E →L[ℝ] F} (h_zero : ∀ s, measurable_set s → μ s = 0 → T s = 0) (h_add : fin_meas_additive μ T) (x : E) : set_to_L1s T (simple_func.indicator_const 1 measurable_set.univ (measure_ne_top μ _) x) = T univ x := set_to_L1s_indicator_const h_zero h_add measurable_set.univ (measure_lt_top _ _) x section order variables {G'' G' : Type*} [normed_lattice_add_comm_group G'] [normed_space ℝ G'] [normed_lattice_add_comm_group G''] [normed_space ℝ G''] {T : set α → G'' →L[ℝ] G'} lemma set_to_L1s_mono_left {T T' : set α → E →L[ℝ] G''} (hTT' : ∀ s x, T s x ≤ T' s x) (f : α →₁ₛ[μ] E) : set_to_L1s T f ≤ set_to_L1s T' f := simple_func.set_to_simple_func_mono_left T T' hTT' _ lemma set_to_L1s_mono_left' {T T' : set α → E →L[ℝ] G''} (hTT' : ∀ s, measurable_set s → μ s < ∞ → ∀ x, T s x ≤ T' s x) (f : α →₁ₛ[μ] E) : set_to_L1s T f ≤ set_to_L1s T' f := simple_func.set_to_simple_func_mono_left' T T' hTT' _ (simple_func.integrable f) variables [measurable_space G''] [borel_space G''] [second_countable_topology G''] lemma set_to_L1s_nonneg (h_zero : ∀ s, measurable_set s → μ s = 0 → T s = 0) (h_add : fin_meas_additive μ T) (hT_nonneg : ∀ s, measurable_set s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f : α →₁ₛ[μ] G''} (hf : 0 ≤ f) : 0 ≤ set_to_L1s T f := begin simp_rw set_to_L1s, obtain ⟨f', hf', hff'⟩ : ∃ f' : α →ₛ G'', 0 ≤ f' ∧ simple_func.to_simple_func f =ᵐ[μ] f', { obtain ⟨f'', hf'', hff''⟩ := exists_simple_func_nonneg_ae_eq hf, exact ⟨f'', hf'', (Lp.simple_func.to_simple_func_eq_to_fun f).trans hff''⟩, }, rw simple_func.set_to_simple_func_congr _ h_zero h_add (simple_func.integrable _) hff', exact simple_func.set_to_simple_func_nonneg' T hT_nonneg _ hf' ((simple_func.integrable f).congr hff'), end lemma set_to_L1s_mono (h_zero : ∀ s, measurable_set s → μ s = 0 → T s = 0) (h_add : fin_meas_additive μ T) (hT_nonneg : ∀ s, measurable_set s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f g : α →₁ₛ[μ] G''} (hfg : f ≤ g) : set_to_L1s T f ≤ set_to_L1s T g := begin rw ← sub_nonneg at ⊢ hfg, rw ← set_to_L1s_sub h_zero h_add, exact set_to_L1s_nonneg h_zero h_add hT_nonneg hfg, end end order variables [normed_space 𝕜 F] [measurable_space 𝕜] [opens_measurable_space 𝕜] variables (α E μ 𝕜) /-- Extend `set α → E →L[ℝ] F` to `(α →₁ₛ[μ] E) →L[𝕜] F`. -/ def set_to_L1s_clm' {T : set α → E →L[ℝ] F} {C : ℝ} (hT : dominated_fin_meas_additive μ T C) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) : (α →₁ₛ[μ] E) →L[𝕜] F := linear_map.mk_continuous ⟨set_to_L1s T, set_to_L1s_add T (λ _, hT.eq_zero_of_measure_zero) hT.1, set_to_L1s_smul T (λ _, hT.eq_zero_of_measure_zero) hT.1 h_smul⟩ C (λ f, norm_set_to_L1s_le T hT.2 f) /-- Extend `set α → E →L[ℝ] F` to `(α →₁ₛ[μ] E) →L[ℝ] F`. -/ def set_to_L1s_clm {T : set α → E →L[ℝ] F} {C : ℝ} (hT : dominated_fin_meas_additive μ T C) : (α →₁ₛ[μ] E) →L[ℝ] F := linear_map.mk_continuous ⟨set_to_L1s T, set_to_L1s_add T (λ _, hT.eq_zero_of_measure_zero) hT.1, set_to_L1s_smul_real T (λ _, hT.eq_zero_of_measure_zero) hT.1⟩ C (λ f, norm_set_to_L1s_le T hT.2 f) variables {α E μ 𝕜} variables {T T' T'' : set α → E →L[ℝ] F} {C C' C'' : ℝ} @[simp] lemma set_to_L1s_clm_zero_left (hT : dominated_fin_meas_additive μ (0 : set α → E →L[ℝ] F) C) (f : α →₁ₛ[μ] E) : set_to_L1s_clm α E μ hT f = 0 := set_to_L1s_zero_left _ lemma set_to_L1s_clm_zero_left' (hT : dominated_fin_meas_additive μ T C) (h_zero : ∀ s, measurable_set s → μ s < ∞ → T s = 0) (f : α →₁ₛ[μ] E) : set_to_L1s_clm α E μ hT f = 0 := set_to_L1s_zero_left' h_zero f lemma set_to_L1s_clm_congr_left (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (h : T = T') (f : α →₁ₛ[μ] E) : set_to_L1s_clm α E μ hT f = set_to_L1s_clm α E μ hT' f := set_to_L1s_congr_left T T' (λ _ _ _, by rw h) f lemma set_to_L1s_clm_congr_left' (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (h : ∀ s, measurable_set s → μ s < ∞ → T s = T' s) (f : α →₁ₛ[μ] E) : set_to_L1s_clm α E μ hT f = set_to_L1s_clm α E μ hT' f := set_to_L1s_congr_left T T' h f lemma set_to_L1s_clm_congr_measure {μ' : measure α} (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ' T C') (hμ : μ ≪ μ') (f : α →₁ₛ[μ] E) (f' : α →₁ₛ[μ'] E) (h : f =ᵐ[μ] f') : set_to_L1s_clm α E μ hT f = set_to_L1s_clm α E μ' hT' f' := set_to_L1s_congr_measure T (λ s, hT.eq_zero_of_measure_zero) hT.1 hμ _ _ h lemma set_to_L1s_clm_add_left (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (f : α →₁ₛ[μ] E) : set_to_L1s_clm α E μ (hT.add hT') f = set_to_L1s_clm α E μ hT f + set_to_L1s_clm α E μ hT' f := set_to_L1s_add_left T T' f lemma set_to_L1s_clm_add_left' (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (hT'' : dominated_fin_meas_additive μ T'' C'') (h_add : ∀ s, measurable_set s → μ s < ∞ → T'' s = T s + T' s) (f : α →₁ₛ[μ] E) : set_to_L1s_clm α E μ hT'' f = set_to_L1s_clm α E μ hT f + set_to_L1s_clm α E μ hT' f := set_to_L1s_add_left' T T' T'' h_add f lemma set_to_L1s_clm_smul_left (c : ℝ) (hT : dominated_fin_meas_additive μ T C) (f : α →₁ₛ[μ] E) : set_to_L1s_clm α E μ (hT.smul c) f = c • set_to_L1s_clm α E μ hT f := set_to_L1s_smul_left T c f lemma set_to_L1s_clm_smul_left' (c : ℝ) (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (h_smul : ∀ s, measurable_set s → μ s < ∞ → T' s = c • (T s)) (f : α →₁ₛ[μ] E) : set_to_L1s_clm α E μ hT' f = c • set_to_L1s_clm α E μ hT f := set_to_L1s_smul_left' T T' c h_smul f lemma norm_set_to_L1s_clm_le {T : set α → E →L[ℝ] F} {C : ℝ} (hT : dominated_fin_meas_additive μ T C) (hC : 0 ≤ C) : ∥set_to_L1s_clm α E μ hT∥ ≤ C := linear_map.mk_continuous_norm_le _ hC _ lemma norm_set_to_L1s_clm_le' {T : set α → E →L[ℝ] F} {C : ℝ} (hT : dominated_fin_meas_additive μ T C) : ∥set_to_L1s_clm α E μ hT∥ ≤ max C 0 := linear_map.mk_continuous_norm_le' _ _ lemma set_to_L1s_clm_const [is_finite_measure μ] {T : set α → E →L[ℝ] F} {C : ℝ} (hT : dominated_fin_meas_additive μ T C) (x : E) : set_to_L1s_clm α E μ hT (simple_func.indicator_const 1 measurable_set.univ (measure_ne_top μ _) x) = T univ x := set_to_L1s_const (λ s, hT.eq_zero_of_measure_zero) hT.1 x section order variables {G' G'' : Type*} [normed_lattice_add_comm_group G''] [normed_space ℝ G''] [normed_lattice_add_comm_group G'] [normed_space ℝ G'] [measurable_space G'] [borel_space G'] [second_countable_topology G'] lemma set_to_L1s_clm_mono_left {T T' : set α → E →L[ℝ] G''} {C C' : ℝ} (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (hTT' : ∀ s x, T s x ≤ T' s x) (f : α →₁ₛ[μ] E) : set_to_L1s_clm α E μ hT f ≤ set_to_L1s_clm α E μ hT' f := simple_func.set_to_simple_func_mono_left T T' hTT' _ lemma set_to_L1s_clm_mono_left' {T T' : set α → E →L[ℝ] G''} {C C' : ℝ} (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (hTT' : ∀ s, measurable_set s → μ s < ∞ → ∀ x, T s x ≤ T' s x) (f : α →₁ₛ[μ] E) : set_to_L1s_clm α E μ hT f ≤ set_to_L1s_clm α E μ hT' f := simple_func.set_to_simple_func_mono_left' T T' hTT' _ (simple_func.integrable f) lemma set_to_L1s_clm_nonneg {T : set α → G' →L[ℝ] G''} {C : ℝ} (hT : dominated_fin_meas_additive μ T C) (hT_nonneg : ∀ s, measurable_set s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f : α →₁ₛ[μ] G'} (hf : 0 ≤ f) : 0 ≤ set_to_L1s_clm α G' μ hT f := set_to_L1s_nonneg (λ s, hT.eq_zero_of_measure_zero) hT.1 hT_nonneg hf lemma set_to_L1s_clm_mono {T : set α → G' →L[ℝ] G''} {C : ℝ} (hT : dominated_fin_meas_additive μ T C) (hT_nonneg : ∀ s, measurable_set s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f g : α →₁ₛ[μ] G'} (hfg : f ≤ g) : set_to_L1s_clm α G' μ hT f ≤ set_to_L1s_clm α G' μ hT g := set_to_L1s_mono (λ s, hT.eq_zero_of_measure_zero) hT.1 hT_nonneg hfg end order end set_to_L1s end simple_func open simple_func section set_to_L1 local attribute [instance] Lp.simple_func.module local attribute [instance] Lp.simple_func.normed_space variables (𝕜) [nondiscrete_normed_field 𝕜] [measurable_space 𝕜] [opens_measurable_space 𝕜] [second_countable_topology E] [borel_space E] [normed_space 𝕜 E] [normed_space 𝕜 F] [complete_space F] {T T' T'' : set α → E →L[ℝ] F} {C C' C'' : ℝ} /-- Extend `set α → (E →L[ℝ] F)` to `(α →₁[μ] E) →L[𝕜] F`. -/ def set_to_L1' (hT : dominated_fin_meas_additive μ T C) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) : (α →₁[μ] E) →L[𝕜] F := (set_to_L1s_clm' α E 𝕜 μ hT h_smul).extend (coe_to_Lp α E 𝕜) (simple_func.dense_range one_ne_top) simple_func.uniform_inducing variables {𝕜} /-- Extend `set α → E →L[ℝ] F` to `(α →₁[μ] E) →L[ℝ] F`. -/ def set_to_L1 (hT : dominated_fin_meas_additive μ T C) : (α →₁[μ] E) →L[ℝ] F := (set_to_L1s_clm α E μ hT).extend (coe_to_Lp α E ℝ) (simple_func.dense_range one_ne_top) simple_func.uniform_inducing lemma set_to_L1_eq_set_to_L1s_clm (hT : dominated_fin_meas_additive μ T C) (f : α →₁ₛ[μ] E) : set_to_L1 hT f = set_to_L1s_clm α E μ hT f := uniformly_extend_of_ind simple_func.uniform_inducing (simple_func.dense_range one_ne_top) (set_to_L1s_clm α E μ hT).uniform_continuous _ lemma set_to_L1_eq_set_to_L1' (hT : dominated_fin_meas_additive μ T C) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) (f : α →₁[μ] E) : set_to_L1 hT f = set_to_L1' 𝕜 hT h_smul f := rfl @[simp] lemma set_to_L1_zero_left (hT : dominated_fin_meas_additive μ (0 : set α → E →L[ℝ] F) C) (f : α →₁[μ] E) : set_to_L1 hT f = 0 := begin suffices : set_to_L1 hT = 0, by { rw this, simp, }, refine continuous_linear_map.extend_unique (set_to_L1s_clm α E μ hT) _ _ _ _ _, ext1 f, rw [set_to_L1s_clm_zero_left hT f, continuous_linear_map.zero_comp, continuous_linear_map.zero_apply], end lemma set_to_L1_zero_left' (hT : dominated_fin_meas_additive μ T C) (h_zero : ∀ s, measurable_set s → μ s < ∞ → T s = 0) (f : α →₁[μ] E) : set_to_L1 hT f = 0 := begin suffices : set_to_L1 hT = 0, by { rw this, simp, }, refine continuous_linear_map.extend_unique (set_to_L1s_clm α E μ hT) _ _ _ _ _, ext1 f, rw [set_to_L1s_clm_zero_left' hT h_zero f, continuous_linear_map.zero_comp, continuous_linear_map.zero_apply], end lemma set_to_L1_congr_left (T T' : set α → E →L[ℝ] F) {C C' : ℝ} (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (h : T = T') (f : α →₁[μ] E) : set_to_L1 hT f = set_to_L1 hT' f := begin suffices : set_to_L1 hT = set_to_L1 hT', by rw this, refine continuous_linear_map.extend_unique (set_to_L1s_clm α E μ hT) _ _ _ _ _, ext1 f, suffices : set_to_L1 hT' f = set_to_L1s_clm α E μ hT f, by { rw ← this, congr' 1, }, rw set_to_L1_eq_set_to_L1s_clm, exact set_to_L1s_clm_congr_left hT' hT h.symm f, end lemma set_to_L1_congr_left' (T T' : set α → E →L[ℝ] F) {C C' : ℝ} (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (h : ∀ s, measurable_set s → μ s < ∞ → T s = T' s) (f : α →₁[μ] E) : set_to_L1 hT f = set_to_L1 hT' f := begin suffices : set_to_L1 hT = set_to_L1 hT', by rw this, refine continuous_linear_map.extend_unique (set_to_L1s_clm α E μ hT) _ _ _ _ _, ext1 f, suffices : set_to_L1 hT' f = set_to_L1s_clm α E μ hT f, by { rw ← this, congr' 1, }, rw set_to_L1_eq_set_to_L1s_clm, exact (set_to_L1s_clm_congr_left' hT hT' h f).symm, end lemma set_to_L1_add_left (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (f : α →₁[μ] E) : set_to_L1 (hT.add hT') f = set_to_L1 hT f + set_to_L1 hT' f := begin suffices : set_to_L1 (hT.add hT') = set_to_L1 hT + set_to_L1 hT', by { rw [this, continuous_linear_map.add_apply], }, refine continuous_linear_map.extend_unique (set_to_L1s_clm α E μ (hT.add hT')) _ _ _ _ _, ext1 f, simp only [continuous_linear_map.add_comp, continuous_linear_map.coe_comp', function.comp_app, continuous_linear_map.add_apply], suffices : set_to_L1 hT f + set_to_L1 hT' f = set_to_L1s_clm α E μ (hT.add hT') f, by { rw ← this, congr, }, rw [set_to_L1_eq_set_to_L1s_clm, set_to_L1_eq_set_to_L1s_clm, set_to_L1s_clm_add_left hT hT'], end lemma set_to_L1_add_left' (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (hT'' : dominated_fin_meas_additive μ T'' C'') (h_add : ∀ s, measurable_set s → μ s < ∞ → T'' s = T s + T' s) (f : α →₁[μ] E) : set_to_L1 hT'' f = set_to_L1 hT f + set_to_L1 hT' f := begin suffices : set_to_L1 hT'' = set_to_L1 hT + set_to_L1 hT', by { rw [this, continuous_linear_map.add_apply], }, refine continuous_linear_map.extend_unique (set_to_L1s_clm α E μ hT'') _ _ _ _ _, ext1 f, simp only [continuous_linear_map.add_comp, continuous_linear_map.coe_comp', function.comp_app, continuous_linear_map.add_apply], suffices : set_to_L1 hT f + set_to_L1 hT' f = set_to_L1s_clm α E μ hT'' f, by { rw ← this, congr, }, rw [set_to_L1_eq_set_to_L1s_clm, set_to_L1_eq_set_to_L1s_clm, set_to_L1s_clm_add_left' hT hT' hT'' h_add], end lemma set_to_L1_smul_left (hT : dominated_fin_meas_additive μ T C) (c : ℝ) (f : α →₁[μ] E) : set_to_L1 (hT.smul c) f = c • set_to_L1 hT f := begin suffices : set_to_L1 (hT.smul c) = c • set_to_L1 hT, by { rw [this, continuous_linear_map.smul_apply], }, refine continuous_linear_map.extend_unique (set_to_L1s_clm α E μ (hT.smul c)) _ _ _ _ _, ext1 f, simp only [continuous_linear_map.coe_comp', function.comp_app, continuous_linear_map.smul_comp, pi.smul_apply, continuous_linear_map.coe_smul'], suffices : c • set_to_L1 hT f = set_to_L1s_clm α E μ (hT.smul c) f, by { rw ← this, congr, }, rw [set_to_L1_eq_set_to_L1s_clm, set_to_L1s_clm_smul_left c hT], end lemma set_to_L1_smul_left' (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (c : ℝ) (h_smul : ∀ s, measurable_set s → μ s < ∞ → T' s = c • (T s)) (f : α →₁[μ] E) : set_to_L1 hT' f = c • set_to_L1 hT f := begin suffices : set_to_L1 hT' = c • set_to_L1 hT, by { rw [this, continuous_linear_map.smul_apply], }, refine continuous_linear_map.extend_unique (set_to_L1s_clm α E μ hT') _ _ _ _ _, ext1 f, simp only [continuous_linear_map.coe_comp', function.comp_app, continuous_linear_map.smul_comp, pi.smul_apply, continuous_linear_map.coe_smul'], suffices : c • set_to_L1 hT f = set_to_L1s_clm α E μ hT' f, by { rw ← this, congr, }, rw [set_to_L1_eq_set_to_L1s_clm, set_to_L1s_clm_smul_left' c hT hT' h_smul], end lemma set_to_L1_smul (hT : dominated_fin_meas_additive μ T C) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) (c : 𝕜) (f : α →₁[μ] E) : set_to_L1 hT (c • f) = c • set_to_L1 hT f := begin rw [set_to_L1_eq_set_to_L1' hT h_smul, set_to_L1_eq_set_to_L1' hT h_smul], exact continuous_linear_map.map_smul _ _ _, end lemma set_to_L1_simple_func_indicator_const (hT : dominated_fin_meas_additive μ T C) {s : set α} (hs : measurable_set s) (hμs : μ s < ∞) (x : E) : set_to_L1 hT (simple_func.indicator_const 1 hs hμs.ne x) = T s x := begin rw set_to_L1_eq_set_to_L1s_clm, exact set_to_L1s_indicator_const (λ s, hT.eq_zero_of_measure_zero) hT.1 hs hμs x, end lemma set_to_L1_indicator_const_Lp (hT : dominated_fin_meas_additive μ T C) {s : set α} (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : E) : set_to_L1 hT (indicator_const_Lp 1 hs hμs x) = T s x := begin rw ← Lp.simple_func.coe_indicator_const hs hμs x, exact set_to_L1_simple_func_indicator_const hT hs hμs.lt_top x, end lemma set_to_L1_const [is_finite_measure μ] (hT : dominated_fin_meas_additive μ T C) (x : E) : set_to_L1 hT (indicator_const_Lp 1 measurable_set.univ (measure_ne_top _ _) x) = T univ x := set_to_L1_indicator_const_Lp hT measurable_set.univ (measure_ne_top _ _) x section order variables {G' G'' : Type*} [normed_lattice_add_comm_group G''] [normed_space ℝ G''] [complete_space G''] [normed_lattice_add_comm_group G'] [normed_space ℝ G'] [measurable_space G'] [borel_space G'] [second_countable_topology G'] lemma set_to_L1_mono_left' {T T' : set α → E →L[ℝ] G''} {C C' : ℝ} (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (hTT' : ∀ s, measurable_set s → μ s < ∞ → ∀ x, T s x ≤ T' s x) (f : α →₁[μ] E) : set_to_L1 hT f ≤ set_to_L1 hT' f := begin refine Lp.induction one_ne_top _ _ _ _ f, { intros c s hs hμs, rw [set_to_L1_simple_func_indicator_const hT hs hμs, set_to_L1_simple_func_indicator_const hT' hs hμs], exact hTT' s hs hμs c, }, { intros f g hf hg hfg_disj hf_le hg_le, rw [(set_to_L1 hT).map_add, (set_to_L1 hT').map_add], exact add_le_add hf_le hg_le, }, { exact is_closed_le (set_to_L1 hT).continuous (set_to_L1 hT').continuous, }, end lemma set_to_L1_mono_left {T T' : set α → E →L[ℝ] G''} {C C' : ℝ} (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (hTT' : ∀ s x, T s x ≤ T' s x) (f : α →₁[μ] E) : set_to_L1 hT f ≤ set_to_L1 hT' f := set_to_L1_mono_left' hT hT' (λ s _ _ x, hTT' s x) f lemma set_to_L1_nonneg {T : set α → G' →L[ℝ] G''} {C : ℝ} (hT : dominated_fin_meas_additive μ T C) (hT_nonneg : ∀ s, measurable_set s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f : α →₁[μ] G'} (hf : 0 ≤ f) : 0 ≤ set_to_L1 hT f := begin suffices : ∀ f : {g : α →₁[μ] G' // 0 ≤ g}, 0 ≤ set_to_L1 hT f, from this (⟨f, hf⟩ : {g : α →₁[μ] G' // 0 ≤ g}), refine λ g, @is_closed_property {g : α →₁ₛ[μ] G' // 0 ≤ g} {g : α →₁[μ] G' // 0 ≤ g} _ _ _ (dense_range_coe_simple_func_nonneg_to_Lp_nonneg 1 μ G' one_ne_top) _ _ g, { exact is_closed_le continuous_zero ((set_to_L1 hT).continuous.comp continuous_induced_dom), }, { intros g, have : (coe_simple_func_nonneg_to_Lp_nonneg 1 μ G' g : α →₁[μ] G') = (g : α →₁ₛ[μ] G') := rfl, rw [this, set_to_L1_eq_set_to_L1s_clm], exact set_to_L1s_nonneg (λ s, hT.eq_zero_of_measure_zero) hT.1 hT_nonneg g.2, }, end lemma set_to_L1_mono {T : set α → G' →L[ℝ] G''} {C : ℝ} (hT : dominated_fin_meas_additive μ T C) (hT_nonneg : ∀ s, measurable_set s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f g : α →₁[μ] G'} (hfg : f ≤ g) : set_to_L1 hT f ≤ set_to_L1 hT g := begin rw ← sub_nonneg at hfg ⊢, rw ← (set_to_L1 hT).map_sub, exact set_to_L1_nonneg hT hT_nonneg hfg, end end order lemma norm_set_to_L1_le_norm_set_to_L1s_clm (hT : dominated_fin_meas_additive μ T C) : ∥set_to_L1 hT∥ ≤ ∥set_to_L1s_clm α E μ hT∥ := calc ∥set_to_L1 hT∥ ≤ (1 : ℝ≥0) * ∥set_to_L1s_clm α E μ hT∥ : begin refine continuous_linear_map.op_norm_extend_le (set_to_L1s_clm α E μ hT) (coe_to_Lp α E ℝ) (simple_func.dense_range one_ne_top) (λ x, le_of_eq _), rw [nnreal.coe_one, one_mul], refl, end ... = ∥set_to_L1s_clm α E μ hT∥ : by rw [nnreal.coe_one, one_mul] lemma norm_set_to_L1_le_mul_norm (hT : dominated_fin_meas_additive μ T C) (hC : 0 ≤ C) (f : α →₁[μ] E) : ∥set_to_L1 hT f∥ ≤ C * ∥f∥ := calc ∥set_to_L1 hT f∥ ≤ ∥set_to_L1s_clm α E μ hT∥ * ∥f∥ : continuous_linear_map.le_of_op_norm_le _ (norm_set_to_L1_le_norm_set_to_L1s_clm hT) _ ... ≤ C * ∥f∥ : mul_le_mul (norm_set_to_L1s_clm_le hT hC) le_rfl (norm_nonneg _) hC lemma norm_set_to_L1_le_mul_norm' (hT : dominated_fin_meas_additive μ T C) (f : α →₁[μ] E) : ∥set_to_L1 hT f∥ ≤ max C 0 * ∥f∥ := calc ∥set_to_L1 hT f∥ ≤ ∥set_to_L1s_clm α E μ hT∥ * ∥f∥ : continuous_linear_map.le_of_op_norm_le _ (norm_set_to_L1_le_norm_set_to_L1s_clm hT) _ ... ≤ max C 0 * ∥f∥ : mul_le_mul (norm_set_to_L1s_clm_le' hT) le_rfl (norm_nonneg _) (le_max_right _ _) lemma norm_set_to_L1_le (hT : dominated_fin_meas_additive μ T C) (hC : 0 ≤ C) : ∥set_to_L1 hT∥ ≤ C := continuous_linear_map.op_norm_le_bound _ hC (norm_set_to_L1_le_mul_norm hT hC) lemma norm_set_to_L1_le' (hT : dominated_fin_meas_additive μ T C) : ∥set_to_L1 hT∥ ≤ max C 0 := continuous_linear_map.op_norm_le_bound _ (le_max_right _ _) (norm_set_to_L1_le_mul_norm' hT) lemma set_to_L1_lipschitz (hT : dominated_fin_meas_additive μ T C) : lipschitz_with (real.to_nnreal C) (set_to_L1 hT) := (set_to_L1 hT).lipschitz.weaken (norm_set_to_L1_le' hT) /-- If `fs i → f` in `L1`, then `set_to_L1 hT (fs i) → set_to_L1 hT f`. -/ lemma tendsto_set_to_L1 (hT : dominated_fin_meas_additive μ T C) (f : α →₁[μ] E) {ι} (fs : ι → α →₁[μ] E) {l : filter ι} (hfs : tendsto fs l (𝓝 f)) : tendsto (λ i, set_to_L1 hT (fs i)) l (𝓝 $ set_to_L1 hT f) := ((set_to_L1 hT).continuous.tendsto _).comp hfs end set_to_L1 end L1 section function variables [second_countable_topology E] [borel_space E] [complete_space F] {T T' T'': set α → E →L[ℝ] F} {C C' C'' : ℝ} {f g : α → E} variables (μ T) /-- Extend `T : set α → E →L[ℝ] F` to `(α → E) → F` (for integrable functions `α → E`). We set it to 0 if the function is not integrable. -/ def set_to_fun (hT : dominated_fin_meas_additive μ T C) (f : α → E) : F := if hf : integrable f μ then L1.set_to_L1 hT (hf.to_L1 f) else 0 variables {μ T} lemma set_to_fun_eq (hT : dominated_fin_meas_additive μ T C) (hf : integrable f μ) : set_to_fun μ T hT f = L1.set_to_L1 hT (hf.to_L1 f) := dif_pos hf lemma L1.set_to_fun_eq_set_to_L1 (hT : dominated_fin_meas_additive μ T C) (f : α →₁[μ] E) : set_to_fun μ T hT f = L1.set_to_L1 hT f := by rw [set_to_fun_eq hT (L1.integrable_coe_fn f), integrable.to_L1_coe_fn] lemma set_to_fun_undef (hT : dominated_fin_meas_additive μ T C) (hf : ¬ integrable f μ) : set_to_fun μ T hT f = 0 := dif_neg hf lemma set_to_fun_non_ae_measurable (hT : dominated_fin_meas_additive μ T C) (hf : ¬ ae_measurable f μ) : set_to_fun μ T hT f = 0 := set_to_fun_undef hT (not_and_of_not_left _ hf) lemma set_to_fun_congr_left (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (h : T = T') (f : α → E) : set_to_fun μ T hT f = set_to_fun μ T' hT' f := begin by_cases hf : integrable f μ, { simp_rw [set_to_fun_eq _ hf, L1.set_to_L1_congr_left T T' hT hT' h], }, { simp_rw [set_to_fun_undef _ hf], }, end lemma set_to_fun_congr_left' (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (h : ∀ s, measurable_set s → μ s < ∞ → T s = T' s) (f : α → E) : set_to_fun μ T hT f = set_to_fun μ T' hT' f := begin by_cases hf : integrable f μ, { simp_rw [set_to_fun_eq _ hf, L1.set_to_L1_congr_left' T T' hT hT' h], }, { simp_rw [set_to_fun_undef _ hf], }, end lemma set_to_fun_add_left (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (f : α → E) : set_to_fun μ (T + T') (hT.add hT') f = set_to_fun μ T hT f + set_to_fun μ T' hT' f := begin by_cases hf : integrable f μ, { simp_rw [set_to_fun_eq _ hf, L1.set_to_L1_add_left hT hT'], }, { simp_rw [set_to_fun_undef _ hf, add_zero], }, end lemma set_to_fun_add_left' (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (hT'' : dominated_fin_meas_additive μ T'' C'') (h_add : ∀ s, measurable_set s → μ s < ∞ → T'' s = T s + T' s) (f : α → E) : set_to_fun μ T'' hT'' f = set_to_fun μ T hT f + set_to_fun μ T' hT' f := begin by_cases hf : integrable f μ, { simp_rw [set_to_fun_eq _ hf, L1.set_to_L1_add_left' hT hT' hT'' h_add], }, { simp_rw [set_to_fun_undef _ hf, add_zero], }, end lemma set_to_fun_smul_left (hT : dominated_fin_meas_additive μ T C) (c : ℝ) (f : α → E) : set_to_fun μ (λ s, c • (T s)) (hT.smul c) f = c • set_to_fun μ T hT f := begin by_cases hf : integrable f μ, { simp_rw [set_to_fun_eq _ hf, L1.set_to_L1_smul_left hT c], }, { simp_rw [set_to_fun_undef _ hf, smul_zero], }, end lemma set_to_fun_smul_left' (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (c : ℝ) (h_smul : ∀ s, measurable_set s → μ s < ∞ → T' s = c • (T s)) (f : α → E) : set_to_fun μ T' hT' f = c • set_to_fun μ T hT f := begin by_cases hf : integrable f μ, { simp_rw [set_to_fun_eq _ hf, L1.set_to_L1_smul_left' hT hT' c h_smul], }, { simp_rw [set_to_fun_undef _ hf, smul_zero], }, end @[simp] lemma set_to_fun_zero (hT : dominated_fin_meas_additive μ T C) : set_to_fun μ T hT (0 : α → E) = 0 := begin rw set_to_fun_eq hT, { simp only [integrable.to_L1_zero, continuous_linear_map.map_zero], }, { exact integrable_zero _ _ _, }, end @[simp] lemma set_to_fun_zero_left {hT : dominated_fin_meas_additive μ (0 : set α → E →L[ℝ] F) C} : set_to_fun μ 0 hT f = 0 := begin by_cases hf : integrable f μ, { rw set_to_fun_eq hT hf, exact L1.set_to_L1_zero_left hT _, }, { exact set_to_fun_undef hT hf, }, end lemma set_to_fun_zero_left' (hT : dominated_fin_meas_additive μ T C) (h_zero : ∀ s, measurable_set s → μ s < ∞ → T s = 0) : set_to_fun μ T hT f = 0 := begin by_cases hf : integrable f μ, { rw set_to_fun_eq hT hf, exact L1.set_to_L1_zero_left' hT h_zero _, }, { exact set_to_fun_undef hT hf, }, end lemma set_to_fun_add (hT : dominated_fin_meas_additive μ T C) (hf : integrable f μ) (hg : integrable g μ) : set_to_fun μ T hT (f + g) = set_to_fun μ T hT f + set_to_fun μ T hT g := by rw [set_to_fun_eq hT (hf.add hg), set_to_fun_eq hT hf, set_to_fun_eq hT hg, integrable.to_L1_add, (L1.set_to_L1 hT).map_add] lemma set_to_fun_finset_sum' (hT : dominated_fin_meas_additive μ T C) {ι} (s : finset ι) {f : ι → α → E} (hf : ∀ i ∈ s, integrable (f i) μ) : set_to_fun μ T hT (∑ i in s, f i) = ∑ i in s, set_to_fun μ T hT (f i) := begin revert hf, refine finset.induction_on s _ _, { intro h, simp only [set_to_fun_zero, finset.sum_empty] }, { assume i s his ih hf, simp only [his, finset.sum_insert, not_false_iff], rw set_to_fun_add hT (hf i (finset.mem_insert_self i s)) _, { rw ih (λ i hi, hf i (finset.mem_insert_of_mem hi)), }, { convert (integrable_finset_sum s (λ i hi, hf i (finset.mem_insert_of_mem hi))), ext1 x, simp, }, } end lemma set_to_fun_finset_sum (hT : dominated_fin_meas_additive μ T C) {ι} (s : finset ι) {f : ι → α → E} (hf : ∀ i ∈ s, integrable (f i) μ) : set_to_fun μ T hT (λ a, ∑ i in s, f i a) = ∑ i in s, set_to_fun μ T hT (f i) := by { convert set_to_fun_finset_sum' hT s hf, ext1 a, simp, } lemma set_to_fun_neg (hT : dominated_fin_meas_additive μ T C) (f : α → E) : set_to_fun μ T hT (-f) = - set_to_fun μ T hT f := begin by_cases hf : integrable f μ, { rw [set_to_fun_eq hT hf, set_to_fun_eq hT hf.neg, integrable.to_L1_neg, (L1.set_to_L1 hT).map_neg], }, { rw [set_to_fun_undef hT hf, set_to_fun_undef hT, neg_zero], rwa [← integrable_neg_iff] at hf, } end lemma set_to_fun_sub (hT : dominated_fin_meas_additive μ T C) (hf : integrable f μ) (hg : integrable g μ) : set_to_fun μ T hT (f - g) = set_to_fun μ T hT f - set_to_fun μ T hT g := by rw [sub_eq_add_neg, sub_eq_add_neg, set_to_fun_add hT hf hg.neg, set_to_fun_neg hT g] lemma set_to_fun_smul [nondiscrete_normed_field 𝕜] [measurable_space 𝕜] [opens_measurable_space 𝕜] [normed_space 𝕜 E] [normed_space 𝕜 F] (hT : dominated_fin_meas_additive μ T C) (h_smul : ∀ c : 𝕜, ∀ s x, T s (c • x) = c • T s x) (c : 𝕜) (f : α → E) : set_to_fun μ T hT (c • f) = c • set_to_fun μ T hT f := begin by_cases hf : integrable f μ, { rw [set_to_fun_eq hT hf, set_to_fun_eq hT, integrable.to_L1_smul', L1.set_to_L1_smul hT h_smul c _], }, { by_cases hr : c = 0, { rw hr, simp, }, { have hf' : ¬ integrable (c • f) μ, by rwa [integrable_smul_iff hr f], rw [set_to_fun_undef hT hf, set_to_fun_undef hT hf', smul_zero], }, }, end lemma set_to_fun_congr_ae (hT : dominated_fin_meas_additive μ T C) (h : f =ᵐ[μ] g) : set_to_fun μ T hT f = set_to_fun μ T hT g := begin by_cases hfi : integrable f μ, { have hgi : integrable g μ := hfi.congr h, rw [set_to_fun_eq hT hfi, set_to_fun_eq hT hgi, (integrable.to_L1_eq_to_L1_iff f g hfi hgi).2 h] }, { have hgi : ¬ integrable g μ, { rw integrable_congr h at hfi, exact hfi }, rw [set_to_fun_undef hT hfi, set_to_fun_undef hT hgi] }, end lemma set_to_fun_measure_zero (hT : dominated_fin_meas_additive μ T C) (h : μ = 0) : set_to_fun μ T hT f = 0 := by { have : f =ᵐ[μ] 0, by simp [h], rw [set_to_fun_congr_ae hT this, set_to_fun_zero], } lemma set_to_fun_measure_zero' (hT : dominated_fin_meas_additive μ T C) (h : ∀ s, measurable_set s → μ s < ∞ → μ s = 0) : set_to_fun μ T hT f = 0 := set_to_fun_zero_left' hT (λ s hs hμs, hT.eq_zero_of_measure_zero hs (h s hs hμs)) lemma set_to_fun_to_L1 (hT : dominated_fin_meas_additive μ T C) (hf : integrable f μ) : set_to_fun μ T hT (hf.to_L1 f) = set_to_fun μ T hT f := set_to_fun_congr_ae hT hf.coe_fn_to_L1 lemma set_to_fun_indicator_const (hT : dominated_fin_meas_additive μ T C) {s : set α} (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : E) : set_to_fun μ T hT (s.indicator (λ _, x)) = T s x := begin rw set_to_fun_congr_ae hT (@indicator_const_Lp_coe_fn _ _ _ 1 _ _ _ _ hs hμs x _ _).symm, rw L1.set_to_fun_eq_set_to_L1 hT, exact L1.set_to_L1_indicator_const_Lp hT hs hμs x, end lemma set_to_fun_const [is_finite_measure μ] (hT : dominated_fin_meas_additive μ T C) (x : E) : set_to_fun μ T hT (λ _, x) = T univ x := begin have : (λ (_ : α) , x) = set.indicator univ (λ _, x), from (indicator_univ _).symm, rw this, exact set_to_fun_indicator_const hT measurable_set.univ (measure_ne_top _ _) x, end section order variables {G' G'' : Type*} [normed_lattice_add_comm_group G''] [normed_space ℝ G''] [complete_space G''] [normed_lattice_add_comm_group G'] [normed_space ℝ G'] [measurable_space G'] [borel_space G'] [second_countable_topology G'] lemma set_to_fun_mono_left' {T T' : set α → E →L[ℝ] G''} {C C' : ℝ} (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (hTT' : ∀ s, measurable_set s → μ s < ∞ → ∀ x, T s x ≤ T' s x) (f : α → E) : set_to_fun μ T hT f ≤ set_to_fun μ T' hT' f := begin by_cases hf : integrable f μ, { simp_rw set_to_fun_eq _ hf, exact L1.set_to_L1_mono_left' hT hT' hTT' _, }, { simp_rw set_to_fun_undef _ hf, }, end lemma set_to_fun_mono_left {T T' : set α → E →L[ℝ] G''} {C C' : ℝ} (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ T' C') (hTT' : ∀ s x, T s x ≤ T' s x) (f : α →₁[μ] E) : set_to_fun μ T hT f ≤ set_to_fun μ T' hT' f := set_to_fun_mono_left' hT hT' (λ s _ _ x, hTT' s x) f lemma set_to_fun_nonneg {T : set α → G' →L[ℝ] G''} {C : ℝ} (hT : dominated_fin_meas_additive μ T C) (hT_nonneg : ∀ s, measurable_set s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f : α → G'} (hf : 0 ≤ᵐ[μ] f) : 0 ≤ set_to_fun μ T hT f := begin by_cases hfi : integrable f μ, { simp_rw set_to_fun_eq _ hfi, refine L1.set_to_L1_nonneg hT hT_nonneg _, rw ← Lp.coe_fn_le, have h0 := Lp.coe_fn_zero G' 1 μ, have h := integrable.coe_fn_to_L1 hfi, filter_upwards [h0, h, hf] with _ h0a ha hfa, rw [h0a, ha], exact hfa, }, { simp_rw set_to_fun_undef _ hfi, }, end lemma set_to_fun_mono {T : set α → G' →L[ℝ] G''} {C : ℝ} (hT : dominated_fin_meas_additive μ T C) (hT_nonneg : ∀ s, measurable_set s → μ s < ∞ → ∀ x, 0 ≤ x → 0 ≤ T s x) {f g : α → G'} (hf : integrable f μ) (hg : integrable g μ) (hfg : f ≤ᵐ[μ] g) : set_to_fun μ T hT f ≤ set_to_fun μ T hT g := begin rw [← sub_nonneg, ← set_to_fun_sub hT hg hf], refine set_to_fun_nonneg hT hT_nonneg (hfg.mono (λ a ha, _)), rw [pi.sub_apply, pi.zero_apply, sub_nonneg], exact ha, end end order @[continuity] lemma continuous_set_to_fun (hT : dominated_fin_meas_additive μ T C) : continuous (λ (f : α →₁[μ] E), set_to_fun μ T hT f) := by { simp_rw L1.set_to_fun_eq_set_to_L1 hT, exact continuous_linear_map.continuous _, } /-- Auxiliary lemma for `set_to_fun_congr_measure`: the function sending `f : α →₁[μ] G` to `f : α →₁[μ'] G` is continuous when `μ' ≤ c' • μ` for `c' ≠ ∞`. -/ lemma continuous_L1_to_L1 [borel_space G] [second_countable_topology G] {μ' : measure α} (c' : ℝ≥0∞) (hc' : c' ≠ ∞) (hμ'_le : μ' ≤ c' • μ) : continuous (λ f : α →₁[μ] G, (integrable.of_measure_le_smul c' hc' hμ'_le (L1.integrable_coe_fn f)).to_L1 f) := begin by_cases hc'0 : c' = 0, { have hμ'0 : μ' = 0, { rw ← measure.nonpos_iff_eq_zero', refine hμ'_le.trans _, simp [hc'0], }, have h_im_zero : (λ f : α →₁[μ] G, (integrable.of_measure_le_smul c' hc' hμ'_le (L1.integrable_coe_fn f)).to_L1 f) = 0, by { ext1 f, ext1, simp_rw hμ'0, simp only [ae_zero], }, rw h_im_zero, exact continuous_zero, }, rw metric.continuous_iff, intros f ε hε_pos, use (ε / 2) / c'.to_real, refine ⟨div_pos (half_pos hε_pos) (to_real_pos hc'0 hc'), _⟩, intros g hfg, rw Lp.dist_def at hfg ⊢, let h_int := λ f' : α →₁[μ] G, (L1.integrable_coe_fn f').of_measure_le_smul c' hc' hμ'_le, have : snorm (integrable.to_L1 g (h_int g) - integrable.to_L1 f (h_int f)) 1 μ' = snorm (g - f) 1 μ', from snorm_congr_ae ((integrable.coe_fn_to_L1 _).sub (integrable.coe_fn_to_L1 _)), rw this, have h_snorm_ne_top : snorm (g - f) 1 μ ≠ ∞, by { rw ← snorm_congr_ae (Lp.coe_fn_sub _ _), exact Lp.snorm_ne_top _, }, have h_snorm_ne_top' : snorm (g - f) 1 μ' ≠ ∞, { refine ((snorm_mono_measure _ hμ'_le).trans_lt _).ne, rw [snorm_smul_measure_of_ne_zero hc'0, smul_eq_mul], refine ennreal.mul_lt_top _ h_snorm_ne_top, simp [hc', hc'0], }, calc (snorm (g - f) 1 μ').to_real ≤ (c' * snorm (g - f) 1 μ).to_real : by { rw to_real_le_to_real h_snorm_ne_top' (ennreal.mul_ne_top hc' h_snorm_ne_top), refine (snorm_mono_measure (⇑g - ⇑f) hμ'_le).trans _, rw [snorm_smul_measure_of_ne_zero hc'0, smul_eq_mul], simp, } ... = c'.to_real * (snorm (⇑g - ⇑f) 1 μ).to_real : to_real_mul ... ≤ c'.to_real * ((ε / 2) / c'.to_real) : mul_le_mul le_rfl hfg.le to_real_nonneg to_real_nonneg ... = ε / 2 : by { refine mul_div_cancel' (ε / 2) _, rw [ne.def, to_real_eq_zero_iff], simp [hc', hc'0], } ... < ε : half_lt_self hε_pos, end lemma set_to_fun_congr_measure_of_integrable {μ' : measure α} (c' : ℝ≥0∞) (hc' : c' ≠ ∞) (hμ'_le : μ' ≤ c' • μ) (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ' T C') (f : α → E) (hfμ : integrable f μ) : set_to_fun μ T hT f = set_to_fun μ' T hT' f := begin /- integrability for `μ` implies integrability for `μ'`. -/ have h_int : ∀ g : α → E, integrable g μ → integrable g μ', from λ g hg, integrable.of_measure_le_smul c' hc' hμ'_le hg, /- We use `integrable.induction` -/ refine hfμ.induction _ _ _ _ _, { intros c s hs hμs, have hμ's : μ' s ≠ ∞, { refine ((hμ'_le s hs).trans_lt _).ne, rw measure.smul_apply, exact ennreal.mul_lt_top hc' hμs.ne, }, rw [set_to_fun_indicator_const hT hs hμs.ne, set_to_fun_indicator_const hT' hs hμ's], }, { intros f₂ g₂ h_dish hf₂ hg₂ h_eq_f h_eq_g, rw [set_to_fun_add hT hf₂ hg₂, set_to_fun_add hT' (h_int f₂ hf₂) (h_int g₂ hg₂), h_eq_f, h_eq_g], }, { refine is_closed_eq (continuous_set_to_fun hT) _, have : (λ f : α →₁[μ] E, set_to_fun μ' T hT' f) = (λ f : α →₁[μ] E, set_to_fun μ' T hT' ((h_int f (L1.integrable_coe_fn f)).to_L1 f)), { ext1 f, exact set_to_fun_congr_ae hT' (integrable.coe_fn_to_L1 _).symm, }, rw this, exact (continuous_set_to_fun hT').comp (continuous_L1_to_L1 c' hc' hμ'_le), }, { intros f₂ g₂ hfg hf₂ hf_eq, have hfg' : f₂ =ᵐ[μ'] g₂, from (measure.absolutely_continuous_of_le_smul hμ'_le).ae_eq hfg, rw [← set_to_fun_congr_ae hT hfg, hf_eq, set_to_fun_congr_ae hT' hfg'], }, end lemma set_to_fun_congr_measure {μ' : measure α} (c c' : ℝ≥0∞) (hc : c ≠ ∞) (hc' : c' ≠ ∞) (hμ_le : μ ≤ c • μ') (hμ'_le : μ' ≤ c' • μ) (hT : dominated_fin_meas_additive μ T C) (hT' : dominated_fin_meas_additive μ' T C') (f : α → E) : set_to_fun μ T hT f = set_to_fun μ' T hT' f := begin by_cases hf : integrable f μ, { exact set_to_fun_congr_measure_of_integrable c' hc' hμ'_le hT hT' f hf, }, { /- if `f` is not integrable, both `set_to_fun` are 0. -/ have h_int : ∀ g : α → E, ¬ integrable g μ → ¬ integrable g μ', from λ g, mt (λ h, h.of_measure_le_smul _ hc hμ_le), simp_rw [set_to_fun_undef _ hf, set_to_fun_undef _ (h_int f hf)], }, end lemma set_to_fun_congr_measure_of_add_right {μ' : measure α} (hT_add : dominated_fin_meas_additive (μ + μ') T C') (hT : dominated_fin_meas_additive μ T C) (f : α → E) (hf : integrable f (μ + μ')) : set_to_fun (μ + μ') T hT_add f = set_to_fun μ T hT f := begin refine set_to_fun_congr_measure_of_integrable 1 one_ne_top _ hT_add hT f hf, rw one_smul, nth_rewrite 0 ← add_zero μ, exact add_le_add le_rfl bot_le, end lemma set_to_fun_congr_measure_of_add_left {μ' : measure α} (hT_add : dominated_fin_meas_additive (μ + μ') T C') (hT : dominated_fin_meas_additive μ' T C) (f : α → E) (hf : integrable f (μ + μ')) : set_to_fun (μ + μ') T hT_add f = set_to_fun μ' T hT f := begin refine set_to_fun_congr_measure_of_integrable 1 one_ne_top _ hT_add hT f hf, rw one_smul, nth_rewrite 0 ← zero_add μ', exact add_le_add bot_le le_rfl, end lemma set_to_fun_top_smul_measure (hT : dominated_fin_meas_additive (∞ • μ) T C) (f : α → E) : set_to_fun (∞ • μ) T hT f = 0 := begin refine set_to_fun_measure_zero' hT (λ s hs hμs, _), rw lt_top_iff_ne_top at hμs, simp only [true_and, measure.smul_apply, with_top.mul_eq_top_iff, eq_self_iff_true, top_ne_zero, ne.def, not_false_iff, not_or_distrib, not_not] at hμs, simp only [hμs.right, measure.smul_apply, mul_zero], end lemma set_to_fun_congr_smul_measure (c : ℝ≥0∞) (hc_ne_top : c ≠ ∞) (hT : dominated_fin_meas_additive μ T C) (hT_smul : dominated_fin_meas_additive (c • μ) T C') (f : α → E) : set_to_fun μ T hT f = set_to_fun (c • μ) T hT_smul f := begin by_cases hc0 : c = 0, { simp [hc0] at hT_smul, have h : ∀ s, measurable_set s → μ s < ∞ → T s = 0, from λ s hs hμs, hT_smul.eq_zero hs, rw [set_to_fun_zero_left' _ h, set_to_fun_measure_zero], simp [hc0], }, refine set_to_fun_congr_measure c⁻¹ c _ hc_ne_top (le_of_eq _) le_rfl hT hT_smul f, { simp [hc0], }, { rw [smul_smul, ennreal.inv_mul_cancel hc0 hc_ne_top, one_smul], }, end lemma norm_set_to_fun_le_mul_norm (hT : dominated_fin_meas_additive μ T C) (f : α →₁[μ] E) (hC : 0 ≤ C) : ∥set_to_fun μ T hT f∥ ≤ C * ∥f∥ := by { rw L1.set_to_fun_eq_set_to_L1, exact L1.norm_set_to_L1_le_mul_norm hT hC f, } lemma norm_set_to_fun_le_mul_norm' (hT : dominated_fin_meas_additive μ T C) (f : α →₁[μ] E) : ∥set_to_fun μ T hT f∥ ≤ max C 0 * ∥f∥ := by { rw L1.set_to_fun_eq_set_to_L1, exact L1.norm_set_to_L1_le_mul_norm' hT f, } lemma norm_set_to_fun_le (hT : dominated_fin_meas_additive μ T C) (hf : integrable f μ) (hC : 0 ≤ C) : ∥set_to_fun μ T hT f∥ ≤ C * ∥hf.to_L1 f∥ := by { rw set_to_fun_eq hT hf, exact L1.norm_set_to_L1_le_mul_norm hT hC _, } lemma norm_set_to_fun_le' (hT : dominated_fin_meas_additive μ T C) (hf : integrable f μ) : ∥set_to_fun μ T hT f∥ ≤ max C 0 * ∥hf.to_L1 f∥ := by { rw set_to_fun_eq hT hf, exact L1.norm_set_to_L1_le_mul_norm' hT _, } /-- Lebesgue dominated convergence theorem provides sufficient conditions under which almost everywhere convergence of a sequence of functions implies the convergence of their image by `set_to_fun`. We could weaken the condition `bound_integrable` to require `has_finite_integral bound μ` instead (i.e. not requiring that `bound` is measurable), but in all applications proving integrability is easier. -/ theorem tendsto_set_to_fun_of_dominated_convergence (hT : dominated_fin_meas_additive μ T C) {fs : ℕ → α → E} {f : α → E} (bound : α → ℝ) (fs_measurable : ∀ n, ae_measurable (fs n) μ) (bound_integrable : integrable bound μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ∥fs n a∥ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, fs n a) at_top (𝓝 (f a))) : tendsto (λ n, set_to_fun μ T hT (fs n)) at_top (𝓝 $ set_to_fun μ T hT f) := begin /- `f` is a.e.-measurable, since it is the a.e.-pointwise limit of a.e.-measurable functions. -/ have f_measurable : ae_measurable f μ := ae_measurable_of_tendsto_metric_ae fs_measurable h_lim, /- all functions we consider are integrable -/ have fs_int : ∀ n, integrable (fs n) μ := λ n, bound_integrable.mono' (fs_measurable n) (h_bound _), have f_int : integrable f μ := ⟨f_measurable, has_finite_integral_of_dominated_convergence bound_integrable.has_finite_integral h_bound h_lim⟩, /- it suffices to prove the result for the corresponding L1 functions -/ suffices : tendsto (λ n, L1.set_to_L1 hT ((fs_int n).to_L1 (fs n))) at_top (𝓝 (L1.set_to_L1 hT (f_int.to_L1 f))), { convert this, { ext1 n, exact set_to_fun_eq hT (fs_int n), }, { exact set_to_fun_eq hT f_int, }, }, /- the convergence of set_to_L1 follows from the convergence of the L1 functions -/ refine L1.tendsto_set_to_L1 hT _ _ _, /- up to some rewriting, what we need to prove is `h_lim` -/ rw tendsto_iff_norm_tendsto_zero, have lintegral_norm_tendsto_zero : tendsto (λn, ennreal.to_real $ ∫⁻ a, (ennreal.of_real ∥fs n a - f a∥) ∂μ) at_top (𝓝 0) := (tendsto_to_real zero_ne_top).comp (tendsto_lintegral_norm_of_dominated_convergence fs_measurable bound_integrable.has_finite_integral h_bound h_lim), convert lintegral_norm_tendsto_zero, ext1 n, rw L1.norm_def, congr' 1, refine lintegral_congr_ae _, rw ← integrable.to_L1_sub, refine ((fs_int n).sub f_int).coe_fn_to_L1.mono (λ x hx, _), dsimp only, rw [hx, of_real_norm_eq_coe_nnnorm, pi.sub_apply], end /-- Lebesgue dominated convergence theorem for filters with a countable basis -/ lemma tendsto_set_to_fun_filter_of_dominated_convergence (hT : dominated_fin_meas_additive μ T C) {ι} {l : _root_.filter ι} [l.is_countably_generated] {fs : ι → α → E} {f : α → E} (bound : α → ℝ) (hfs_meas : ∀ᶠ n in l, ae_measurable (fs n) μ) (h_bound : ∀ᶠ n in l, ∀ᵐ a ∂μ, ∥fs n a∥ ≤ bound a) (bound_integrable : integrable bound μ) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, fs n a) l (𝓝 (f a))) : tendsto (λ n, set_to_fun μ T hT (fs n)) l (𝓝 $ set_to_fun μ T hT f) := begin rw tendsto_iff_seq_tendsto, intros x xl, have hxl : ∀ s ∈ l, ∃ a, ∀ b ≥ a, x b ∈ s, by { rwa tendsto_at_top' at xl, }, have h : {x : ι | (λ n, ae_measurable (fs n) μ) x} ∩ {x : ι | (λ n, ∀ᵐ a ∂μ, ∥fs n a∥ ≤ bound a) x} ∈ l, from inter_mem hfs_meas h_bound, obtain ⟨k, h⟩ := hxl _ h, rw ← tendsto_add_at_top_iff_nat k, refine tendsto_set_to_fun_of_dominated_convergence hT bound _ bound_integrable _ _, { exact λ n, (h _ (self_le_add_left _ _)).1, }, { exact λ n, (h _ (self_le_add_left _ _)).2, }, { filter_upwards [h_lim], refine λ a h_lin, @tendsto.comp _ _ _ (λ n, x (n + k)) (λ n, fs n a) _ _ _ h_lin _, rw tendsto_add_at_top_iff_nat, assumption } end variables {X : Type*} [topological_space X] [first_countable_topology X] lemma continuous_at_set_to_fun_of_dominated (hT : dominated_fin_meas_additive μ T C) {fs : X → α → E} {x₀ : X} {bound : α → ℝ} (hfs_meas : ∀ᶠ x in 𝓝 x₀, ae_measurable (fs x) μ) (h_bound : ∀ᶠ x in 𝓝 x₀, ∀ᵐ a ∂μ, ∥fs x a∥ ≤ bound a) (bound_integrable : integrable bound μ) (h_cont : ∀ᵐ a ∂μ, continuous_at (λ x, fs x a) x₀) : continuous_at (λ x, set_to_fun μ T hT (fs x)) x₀ := tendsto_set_to_fun_filter_of_dominated_convergence hT bound ‹_› ‹_› ‹_› ‹_› lemma continuous_set_to_fun_of_dominated (hT : dominated_fin_meas_additive μ T C) {fs : X → α → E} {bound : α → ℝ} (hfs_meas : ∀ x, ae_measurable (fs x) μ) (h_bound : ∀ x, ∀ᵐ a ∂μ, ∥fs x a∥ ≤ bound a) (bound_integrable : integrable bound μ) (h_cont : ∀ᵐ a ∂μ, continuous (λ x, fs x a)) : continuous (λ x, set_to_fun μ T hT (fs x)) := continuous_iff_continuous_at.mpr (λ x₀, continuous_at_set_to_fun_of_dominated hT (eventually_of_forall hfs_meas) (eventually_of_forall h_bound) ‹_› $ h_cont.mono $ λ _, continuous.continuous_at) end function end measure_theory
d4337fcbf8ee8eb3aed64be7cad45f9b93963178
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/linear_algebra/sesquilinear_form.lean
a3766336975ec3e2b07782b9576413549c332e8a
[ "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
8,679
lean
/- Copyright (c) 2018 Andreas Swerdlow. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Andreas Swerdlow -/ import algebra.module import ring_theory.maps /-! # Sesquilinear form This file defines a bilinear form over a module. The definition requires a ring antiautomorphism on the scalar ring, which comes from the file ring_theory.involution. Basic ideas such as orthogonality are also introduced. A sesquilinear form on an R-module M, is a function from M x M to R, that is linear in the first argument and antilinear in the second, with respect to an antiautomorphism on R (an antiisomorphism from R to R). ## Notations Given any term S of type sesq_form, due to a coercion, can use the notation S x y to refer to the function field, ie. S x y = S.bilin x y. ## References * <https://en.wikipedia.org/wiki/Sesquilinear_form#Over_arbitrary_rings> ## Tags Sesquilinear form, -/ open ring_anti_equiv universes u v /-- A sesquilinear form over a module -/ structure sesq_form (R : Type u) (M : Type v) [ring R] (I : ring_anti_equiv R R) [add_comm_group M] [module R M] := (sesq : M → M → R) (sesq_add_left : ∀ (x y z : M), sesq (x + y) z = sesq x z + sesq y z) (sesq_smul_left : ∀ (a : R) (x y : M), sesq (a • x) y = a * (sesq x y)) (sesq_add_right : ∀ (x y z : M), sesq x (y + z) = sesq x y + sesq x z) (sesq_smul_right : ∀ (a : R) (x y : M), sesq x (a • y) = (I a) * (sesq x y)) namespace sesq_form section general_ring variables {R : Type u} {M : Type v} [ring R] [add_comm_group M] [module R M] {I : ring_anti_equiv R R} {S : sesq_form R M I} instance : has_coe_to_fun (sesq_form R M I) := ⟨_, λ S, S.sesq⟩ lemma add_left (x y z : M) : S (x + y) z = S x z + S y z := sesq_add_left S x y z lemma smul_left (a : R) (x y : M) : S (a • x) y = a * (S x y) := sesq_smul_left S a x y lemma add_right (x y z : M) : S x (y + z) = S x y + S x z := sesq_add_right S x y z lemma smul_right (a : R) (x y : M) : S x (a • y) = (I a) * (S x y) := sesq_smul_right S a x y lemma zero_left (x : M) : S 0 x = 0 := by {rw [←@zero_smul R _ _ _ _ (0 : M), smul_left, zero_mul]} lemma zero_right (x : M) : S x 0 = 0 := by rw [←@zero_smul _ _ _ _ _ (0 : M), smul_right, map_zero, ring.zero_mul] lemma neg_left (x y : M) : S (-x) y = -(S x y) := by rw [←@neg_one_smul R _ _, smul_left, neg_one_mul] lemma neg_right (x y : M) : S x (-y) = -(S x y) := by rw [←@neg_one_smul R _ _, smul_right, map_neg_one, neg_one_mul] lemma sub_left (x y z : M) : S (x - y) z = S x z - S y z := by rw [sub_eq_add_neg, add_left, neg_left]; refl lemma sub_right (x y z : M) : S x (y - z) = S x y - S x z := by rw [sub_eq_add_neg, add_right, neg_right]; refl variable {D : sesq_form R M I} @[ext] lemma ext (H : ∀ (x y : M), S x y = D x y) : S = D := by {cases S, cases D, congr, funext, exact H _ _} instance : add_comm_group (sesq_form R M I) := { add := λ S D, { sesq := λ x y, S x y + D x y, sesq_add_left := λ x y z, by {rw add_left, rw add_left, ac_refl}, sesq_smul_left := λ a x y, by {rw [smul_left, smul_left, mul_add]}, sesq_add_right := λ x y z, by {rw add_right, rw add_right, ac_refl}, sesq_smul_right := λ a x y, by {rw [smul_right, smul_right, mul_add]} }, add_assoc := by {intros, ext, unfold coe_fn has_coe_to_fun.coe sesq coe_fn has_coe_to_fun.coe sesq, rw add_assoc}, zero := { sesq := λ x y, 0, sesq_add_left := λ x y z, (add_zero 0).symm, sesq_smul_left := λ a x y, (mul_zero a).symm, sesq_add_right := λ x y z, (zero_add 0).symm, sesq_smul_right := λ a x y, (mul_zero (I a)).symm }, zero_add := by {intros, ext, unfold coe_fn has_coe_to_fun.coe sesq, rw zero_add}, add_zero := by {intros, ext, unfold coe_fn has_coe_to_fun.coe sesq, rw add_zero}, neg := λ S, { sesq := λ x y, - (S.1 x y), sesq_add_left := λ x y z, by rw [sesq_add_left, neg_add], sesq_smul_left := λ a x y, by rw [sesq_smul_left, mul_neg_eq_neg_mul_symm], sesq_add_right := λ x y z, by rw [sesq_add_right, neg_add], sesq_smul_right := λ a x y, by rw [sesq_smul_right, mul_neg_eq_neg_mul_symm] }, add_left_neg := by {intros, ext, unfold coe_fn has_coe_to_fun.coe sesq, rw neg_add_self}, add_comm := by {intros, ext, unfold coe_fn has_coe_to_fun.coe sesq, rw add_comm} } instance : inhabited (sesq_form R M I) := ⟨0⟩ /-- The proposition that two elements of a sesquilinear form space are orthogonal -/ def is_ortho (S : sesq_form R M I) (x y : M) : Prop := S x y = 0 lemma ortho_zero (x : M) : is_ortho S (0 : M) x := zero_left x end general_ring section comm_ring variables {R : Type*} [comm_ring R] {M : Type v} [add_comm_group M] [module R M] {J : ring_anti_equiv R R} (F : sesq_form R M J) (f : M → M) instance to_module : module R (sesq_form R M J) := { smul := λ c S, { sesq := λ x y, c * S x y, sesq_add_left := λ x y z, by {unfold coe_fn has_coe_to_fun.coe sesq, rw [sesq_add_left, left_distrib]}, sesq_smul_left := λ a x y, by {unfold coe_fn has_coe_to_fun.coe sesq, rw [sesq_smul_left, ←mul_assoc, mul_comm c, mul_assoc]}, sesq_add_right := λ x y z, by {unfold coe_fn has_coe_to_fun.coe sesq, rw [sesq_add_right, left_distrib]}, sesq_smul_right := λ a x y, by {unfold coe_fn has_coe_to_fun.coe sesq, rw [sesq_smul_right, ←mul_assoc, mul_comm c, mul_assoc], refl} }, smul_add := λ c S D, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw left_distrib}, add_smul := λ c S D, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw right_distrib}, mul_smul := λ a c D, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw mul_assoc}, one_smul := λ S, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw one_mul}, zero_smul := λ S, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw zero_mul}, smul_zero := λ S, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw mul_zero} } end comm_ring section domain variables {R : Type*} [domain R] {M : Type v} [add_comm_group M] [module R M] {K : ring_anti_equiv R R} {G : sesq_form R M K} theorem ortho_smul_left {x y : M} {a : R} (ha : a ≠ 0) : (is_ortho G x y) ↔ (is_ortho G (a • x) y) := begin dunfold is_ortho, split; intro H, { rw [smul_left, H, ring.mul_zero] }, { rw [smul_left, mul_eq_zero] at H, cases H, { trivial }, { exact H }} end theorem ortho_smul_right {x y : M} {a : R} (ha : a ≠ 0) : (is_ortho G x y) ↔ (is_ortho G x (a • y)) := begin dunfold is_ortho, split; intro H, { rw [smul_right, H, ring.mul_zero] }, { rw [smul_right, mul_eq_zero] at H, cases H, { rw map_zero_iff at H, trivial }, { exact H }} end end domain end sesq_form namespace refl_sesq_form open refl_sesq_form sesq_form variables {R : Type*} {M : Type*} [ring R] [add_comm_group M] [module R M] {I : ring_anti_equiv R R} {S : sesq_form R M I} /-- The proposition that a sesquilinear form is reflexive -/ def is_refl (S : sesq_form R M I) : Prop := ∀ (x y : M), S x y = 0 → S y x = 0 variable (H : is_refl S) lemma eq_zero : ∀ {x y : M}, S x y = 0 → S y x = 0 := λ x y, H x y lemma ortho_sym {x y : M} : is_ortho S x y ↔ is_ortho S y x := ⟨eq_zero H, eq_zero H⟩ end refl_sesq_form namespace sym_sesq_form open sym_sesq_form sesq_form variables {R : Type*} {M : Type*} [ring R] [add_comm_group M] [module R M] {I : ring_anti_equiv R R} {S : sesq_form R M I} /-- The proposition that a sesquilinear form is symmetric -/ def is_sym (S : sesq_form R M I) : Prop := ∀ (x y : M), I (S x y) = S y x variable (H : is_sym S) include H lemma sym (x y : M) : I (S x y) = S y x := H x y lemma is_refl : refl_sesq_form.is_refl S := λ x y H1, by rw [←H, map_zero_iff, H1] lemma ortho_sym {x y : M} : is_ortho S x y ↔ is_ortho S y x := refl_sesq_form.ortho_sym (is_refl H) end sym_sesq_form namespace alt_sesq_form open alt_sesq_form sesq_form variables {R : Type*} {M : Type*} [ring R] [add_comm_group M] [module R M] {I : ring_anti_equiv R R} {S : sesq_form R M I} /-- The proposition that a sesquilinear form is alternating -/ def is_alt (S : sesq_form R M I) : Prop := ∀ (x : M), S x x = 0 variable (H : is_alt S) include H lemma self_eq_zero (x : M) : S x x = 0 := H x lemma neg (x y : M) : - S x y = S y x := begin have H1 : S (x + y) (x + y) = 0, { exact self_eq_zero H (x + y) }, rw [add_left, add_right, add_right, self_eq_zero H, self_eq_zero H, ring.zero_add, ring.add_zero, add_eq_zero_iff_neg_eq] at H1, exact H1, end end alt_sesq_form
9cbb4f1ba12da9f155784ae2b65177a26c8d2ffd
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/tacticExtOverlap.lean
ebee3f77b4416ec176bc8782f1115f06427d9a3d
[ "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
544
lean
open Lean syntax (name := myintro) "intros" sepBy(ident, ",") : tactic macro_rules (kind := myintro) | `(tactic| intros $x,*) => pure $ mkNode `Lean.Parser.Tactic.intros #[mkAtom "intros", mkNullNode x] theorem tst1 {p q : Prop} : p → q → p := by { intros h1, h2; assumption } theorem tst2 {p q : Prop} : p → q → p := by { intros h1; -- the builtin and myintro overlap here. intros h2; -- the builtin and myintro overlap here. assumption } theorem tst3 {p q : Prop} : p → q → p := by { intros h1 h2; assumption }
41e24a2957ebb60ab3c288d3b8d76fc22cb1f238
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebraic_topology/simplicial_object.lean
eac3f11996f174cd1b6837f7a88ddcdde167b8f1
[ "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
19,107
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Scott Morrison, Adam Topaz -/ import algebraic_topology.simplex_category import category_theory.category.ulift import category_theory.limits.functor_category import category_theory.opposites import category_theory.adjunction.limits import category_theory.arrow /-! # Simplicial objects in a category. A simplicial object in a category `C` is a `C`-valued presheaf on `simplex_category`. (Similarly a cosimplicial object is functor `simplex_category ⥤ C`.) Use the notation `X _[n]` in the `simplicial` locale to obtain the `n`-th term of a (co)simplicial object `X`, where `n` is a natural number. -/ open opposite open category_theory open category_theory.limits universes v u namespace category_theory variables (C : Type u) [category.{v} C] /-- The category of simplicial objects valued in a category `C`. This is the category of contravariant functors from `simplex_category` to `C`. -/ @[derive category, nolint has_inhabited_instance] def simplicial_object := simplex_category.{v}ᵒᵖ ⥤ C namespace simplicial_object localized "notation X `_[`:1000 n `]` := (X : category_theory.simplicial_object _).obj (opposite.op (simplex_category.mk n))" in simplicial instance {J : Type v} [small_category J] [has_limits_of_shape J C] : has_limits_of_shape J (simplicial_object C) := by {dsimp [simplicial_object], apply_instance} instance [has_limits C] : has_limits (simplicial_object C) := ⟨infer_instance⟩ instance {J : Type v} [small_category J] [has_colimits_of_shape J C] : has_colimits_of_shape J (simplicial_object C) := by {dsimp [simplicial_object], apply_instance} instance [has_colimits C] : has_colimits (simplicial_object C) := ⟨infer_instance⟩ variables {C} (X : simplicial_object C) /-- Face maps for a simplicial object. -/ def δ {n} (i : fin (n+2)) : X _[n+1] ⟶ X _[n] := X.map (simplex_category.δ i).op /-- Degeneracy maps for a simplicial object. -/ def σ {n} (i : fin (n+1)) : X _[n] ⟶ X _[n+1] := X.map (simplex_category.σ i).op /-- Isomorphisms from identities in ℕ. -/ def eq_to_iso {n m : ℕ} (h : n = m) : X _[n] ≅ X _[m] := X.map_iso (eq_to_iso (by rw h)) @[simp] lemma eq_to_iso_refl {n : ℕ} (h : n = n) : X.eq_to_iso h = iso.refl _ := by { ext, simp [eq_to_iso], } /-- The generic case of the first simplicial identity -/ lemma δ_comp_δ {n} {i j : fin (n+2)} (H : i ≤ j) : X.δ j.succ ≫ X.δ i = X.δ i.cast_succ ≫ X.δ j := by { dsimp [δ], simp only [←X.map_comp, ←op_comp, simplex_category.δ_comp_δ H] } /-- The special case of the first simplicial identity -/ lemma δ_comp_δ_self {n} {i : fin (n+2)} : X.δ i.cast_succ ≫ X.δ i = X.δ i.succ ≫ X.δ i := by { dsimp [δ], simp only [←X.map_comp, ←op_comp, simplex_category.δ_comp_δ_self] } /-- The second simplicial identity -/ lemma δ_comp_σ_of_le {n} {i : fin (n+2)} {j : fin (n+1)} (H : i ≤ j.cast_succ) : X.σ j.succ ≫ X.δ i.cast_succ = X.δ i ≫ X.σ j := by { dsimp [δ, σ], simp only [←X.map_comp, ←op_comp, simplex_category.δ_comp_σ_of_le H] } /-- The first part of the third simplicial identity -/ lemma δ_comp_σ_self {n} {i : fin (n+1)} : X.σ i ≫ X.δ i.cast_succ = 𝟙 _ := begin dsimp [δ, σ], simp only [←X.map_comp, ←op_comp, simplex_category.δ_comp_σ_self, op_id, X.map_id], end /-- The second part of the third simplicial identity -/ lemma δ_comp_σ_succ {n} {i : fin (n+1)} : X.σ i ≫ X.δ i.succ = 𝟙 _ := begin dsimp [δ, σ], simp only [←X.map_comp, ←op_comp, simplex_category.δ_comp_σ_succ, op_id, X.map_id], end /-- The fourth simplicial identity -/ lemma δ_comp_σ_of_gt {n} {i : fin (n+2)} {j : fin (n+1)} (H : j.cast_succ < i) : X.σ j.cast_succ ≫ X.δ i.succ = X.δ i ≫ X.σ j := by { dsimp [δ, σ], simp only [←X.map_comp, ←op_comp, simplex_category.δ_comp_σ_of_gt H] } /-- The fifth simplicial identity -/ lemma σ_comp_σ {n} {i j : fin (n+1)} (H : i ≤ j) : X.σ j ≫ X.σ i.cast_succ = X.σ i ≫ X.σ j.succ := by { dsimp [δ, σ], simp only [←X.map_comp, ←op_comp, simplex_category.σ_comp_σ H] } variable (C) /-- Functor composition induces a functor on simplicial objects. -/ @[simps] def whiskering (D : Type*) [category.{v} D] : (C ⥤ D) ⥤ simplicial_object C ⥤ simplicial_object D := whiskering_right _ _ _ /-- Truncated simplicial objects. -/ @[derive category, nolint has_inhabited_instance] def truncated (n : ℕ) := (simplex_category.truncated.{v} n)ᵒᵖ ⥤ C variable {C} namespace truncated instance {n} {J : Type v} [small_category J] [has_limits_of_shape J C] : has_limits_of_shape J (simplicial_object.truncated C n) := by {dsimp [truncated], apply_instance} instance {n} [has_limits C] : has_limits (simplicial_object.truncated C n) := ⟨infer_instance⟩ instance {n} {J : Type v} [small_category J] [has_colimits_of_shape J C] : has_colimits_of_shape J (simplicial_object.truncated C n) := by {dsimp [truncated], apply_instance} instance {n} [has_colimits C] : has_colimits (simplicial_object.truncated C n) := ⟨infer_instance⟩ variable (C) /-- Functor composition induces a functor on truncated simplicial objects. -/ @[simps] def whiskering {n} (D : Type*) [category.{v} D] : (C ⥤ D) ⥤ truncated C n ⥤ truncated D n := whiskering_right _ _ _ variable {C} end truncated section skeleton /-- The skeleton functor from simplicial objects to truncated simplicial objects. -/ def sk (n : ℕ) : simplicial_object C ⥤ simplicial_object.truncated C n := (whiskering_left _ _ _).obj simplex_category.truncated.inclusion.op end skeleton variable (C) /-- The constant simplicial object is the constant functor. -/ abbreviation const : C ⥤ simplicial_object C := category_theory.functor.const _ /-- The category of augmented simplicial objects, defined as a comma category. -/ @[derive category, nolint has_inhabited_instance] def augmented := comma (𝟭 (simplicial_object C)) (const C) variable {C} namespace augmented /-- Drop the augmentation. -/ @[simps] def drop : augmented C ⥤ simplicial_object C := comma.fst _ _ /-- The point of the augmentation. -/ @[simps] def point : augmented C ⥤ C := comma.snd _ _ /-- The functor from augmented objects to arrows. -/ @[simps] def to_arrow : augmented C ⥤ arrow C := { obj := λ X, { left := (drop.obj X) _[0], right := (point.obj X), hom := X.hom.app _ }, map := λ X Y η, { left := (drop.map η).app _, right := (point.map η), w' := begin dsimp, rw ← nat_trans.comp_app, erw η.w, refl, end } } variable (C) /-- Functor composition induces a functor on augmented simplicial objects. -/ @[simp] def whiskering_obj (D : Type*) [category.{v} D] (F : C ⥤ D) : augmented C ⥤ augmented D := { obj := λ X, { left := ((whiskering _ _).obj F).obj (drop.obj X), right := F.obj (point.obj X), hom := whisker_right X.hom F ≫ (functor.const_comp _ _ _).hom }, map := λ X Y η, { left := whisker_right η.left _, right := F.map η.right, w' := begin ext, dsimp, erw [category.comp_id, category.comp_id, ← F.map_comp, ← F.map_comp, ← nat_trans.comp_app, η.w], refl, end } } /-- Functor composition induces a functor on augmented simplicial objects. -/ @[simps] def whiskering (D : Type*) [category.{v} D] : (C ⥤ D) ⥤ augmented C ⥤ augmented D := { obj := whiskering_obj _ _, map := λ X Y η, { app := λ A, { left := whisker_left _ η, right := η.app _ } } } variable {C} end augmented open_locale simplicial /-- Aaugment a simplicial object with an object. -/ @[simps] def augment (X : simplicial_object C) (X₀ : C) (f : X _[0] ⟶ X₀) (w : ∀ (i : simplex_category) (g₁ g₂ : [0] ⟶ i), X.map g₁.op ≫ f = X.map g₂.op ≫ f) : simplicial_object.augmented C := { left := X, right := X₀, hom := { app := λ i, X.map (simplex_category.const i.unop 0).op ≫ f, naturality' := begin intros i j g, dsimp, rw ← g.op_unop, simpa only [← X.map_comp, ← category.assoc, category.comp_id, ← op_comp] using w _ _ _, end } } @[simp] lemma augment_hom_zero (X : simplicial_object C) (X₀ : C) (f : X _[0] ⟶ X₀) (w) : (X.augment X₀ f w).hom.app (op [0]) = f := by { dsimp, erw [simplex_category.hom_zero_zero ([0].const 0), X.map_id, category.id_comp] } end simplicial_object /-- Cosimplicial objects. -/ @[derive category, nolint has_inhabited_instance] def cosimplicial_object := simplex_category.{v} ⥤ C namespace cosimplicial_object localized "notation X `_[`:1000 n `]` := (X : category_theory.cosimplicial_object _).obj (simplex_category.mk n)" in simplicial instance {J : Type v} [small_category J] [has_limits_of_shape J C] : has_limits_of_shape J (cosimplicial_object C) := by {dsimp [cosimplicial_object], apply_instance} instance [has_limits C] : has_limits (cosimplicial_object C) := ⟨infer_instance⟩ instance {J : Type v} [small_category J] [has_colimits_of_shape J C] : has_colimits_of_shape J (cosimplicial_object C) := by {dsimp [cosimplicial_object], apply_instance} instance [has_colimits C] : has_colimits (cosimplicial_object C) := ⟨infer_instance⟩ variables {C} (X : cosimplicial_object C) /-- Coface maps for a cosimplicial object. -/ def δ {n} (i : fin (n+2)) : X _[n] ⟶ X _[n+1] := X.map (simplex_category.δ i) /-- Codegeneracy maps for a cosimplicial object. -/ def σ {n} (i : fin (n+1)) : X _[n+1] ⟶ X _[n] := X.map (simplex_category.σ i) /-- Isomorphisms from identities in ℕ. -/ def eq_to_iso {n m : ℕ} (h : n = m) : X _[n] ≅ X _[m] := X.map_iso (eq_to_iso (by rw h)) @[simp] lemma eq_to_iso_refl {n : ℕ} (h : n = n) : X.eq_to_iso h = iso.refl _ := by { ext, simp [eq_to_iso], } /-- The generic case of the first cosimplicial identity -/ lemma δ_comp_δ {n} {i j : fin (n+2)} (H : i ≤ j) : X.δ i ≫ X.δ j.succ = X.δ j ≫ X.δ i.cast_succ := by { dsimp [δ], simp only [←X.map_comp, simplex_category.δ_comp_δ H], } /-- The special case of the first cosimplicial identity -/ lemma δ_comp_δ_self {n} {i : fin (n+2)} : X.δ i ≫ X.δ i.cast_succ = X.δ i ≫ X.δ i.succ := by { dsimp [δ], simp only [←X.map_comp, simplex_category.δ_comp_δ_self] } /-- The second cosimplicial identity -/ lemma δ_comp_σ_of_le {n} {i : fin (n+2)} {j : fin (n+1)} (H : i ≤ j.cast_succ) : X.δ i.cast_succ ≫ X.σ j.succ = X.σ j ≫ X.δ i := by { dsimp [δ, σ], simp only [←X.map_comp, simplex_category.δ_comp_σ_of_le H] } /-- The first part of the third cosimplicial identity -/ lemma δ_comp_σ_self {n} {i : fin (n+1)} : X.δ i.cast_succ ≫ X.σ i = 𝟙 _ := begin dsimp [δ, σ], simp only [←X.map_comp, simplex_category.δ_comp_σ_self, X.map_id], end /-- The second part of the third cosimplicial identity -/ lemma δ_comp_σ_succ {n} {i : fin (n+1)} : X.δ i.succ ≫ X.σ i = 𝟙 _ := begin dsimp [δ, σ], simp only [←X.map_comp, simplex_category.δ_comp_σ_succ, X.map_id], end /-- The fourth cosimplicial identity -/ lemma δ_comp_σ_of_gt {n} {i : fin (n+2)} {j : fin (n+1)} (H : j.cast_succ < i) : X.δ i.succ ≫ X.σ j.cast_succ = X.σ j ≫ X.δ i := by { dsimp [δ, σ], simp only [←X.map_comp, simplex_category.δ_comp_σ_of_gt H] } /-- The fifth cosimplicial identity -/ lemma σ_comp_σ {n} {i j : fin (n+1)} (H : i ≤ j) : X.σ i.cast_succ ≫ X.σ j = X.σ j.succ ≫ X.σ i := by { dsimp [δ, σ], simp only [←X.map_comp, simplex_category.σ_comp_σ H] } variable (C) /-- Functor composition induces a functor on cosimplicial objects. -/ @[simps] def whiskering (D : Type*) [category.{v} D] : (C ⥤ D) ⥤ cosimplicial_object C ⥤ cosimplicial_object D := whiskering_right _ _ _ /-- Truncated cosimplicial objects. -/ @[derive category, nolint has_inhabited_instance] def truncated (n : ℕ) := simplex_category.truncated.{v} n ⥤ C variable {C} namespace truncated instance {n} {J : Type v} [small_category J] [has_limits_of_shape J C] : has_limits_of_shape J (cosimplicial_object.truncated C n) := by {dsimp [truncated], apply_instance} instance {n} [has_limits C] : has_limits (cosimplicial_object.truncated C n) := ⟨infer_instance⟩ instance {n} {J : Type v} [small_category J] [has_colimits_of_shape J C] : has_colimits_of_shape J (cosimplicial_object.truncated C n) := by {dsimp [truncated], apply_instance} instance {n} [has_colimits C] : has_colimits (cosimplicial_object.truncated C n) := ⟨infer_instance⟩ variable (C) /-- Functor composition induces a functor on truncated cosimplicial objects. -/ @[simps] def whiskering {n} (D : Type*) [category.{v} D] : (C ⥤ D) ⥤ truncated C n ⥤ truncated D n := whiskering_right _ _ _ variable {C} end truncated section skeleton /-- The skeleton functor from cosimplicial objects to truncated cosimplicial objects. -/ def sk (n : ℕ) : cosimplicial_object C ⥤ cosimplicial_object.truncated C n := (whiskering_left _ _ _).obj simplex_category.truncated.inclusion end skeleton variable (C) /-- The constant cosimplicial object. -/ abbreviation const : C ⥤ cosimplicial_object C := category_theory.functor.const _ /-- Augmented cosimplicial objects. -/ @[derive category, nolint has_inhabited_instance] def augmented := comma (const C) (𝟭 (cosimplicial_object C)) variable {C} namespace augmented /-- Drop the augmentation. -/ @[simps] def drop : augmented C ⥤ cosimplicial_object C := comma.snd _ _ /-- The point of the augmentation. -/ @[simps] def point : augmented C ⥤ C := comma.fst _ _ /-- The functor from augmented objects to arrows. -/ @[simps] def to_arrow : augmented C ⥤ arrow C := { obj := λ X, { left := (point.obj X), right := (drop.obj X) _[0], hom := X.hom.app _ }, map := λ X Y η, { left := (point.map η), right := (drop.map η).app _, w' := begin dsimp, rw ← nat_trans.comp_app, erw ← η.w, refl, end } } variable (C) /-- Functor composition induces a functor on augmented cosimplicial objects. -/ @[simp] def whiskering_obj (D : Type*) [category.{v} D] (F : C ⥤ D) : augmented C ⥤ augmented D := { obj := λ X, { left := F.obj (point.obj X), right := ((whiskering _ _).obj F).obj (drop.obj X), hom := (functor.const_comp _ _ _).inv ≫ whisker_right X.hom F }, map := λ X Y η, { left := F.map η.left, right := whisker_right η.right _, w' := begin ext, dsimp, erw [category.id_comp, category.id_comp, ← F.map_comp, ← F.map_comp, ← nat_trans.comp_app, ← η.w], refl, end } } /-- Functor composition induces a functor on augmented cosimplicial objects. -/ @[simps] def whiskering (D : Type*) [category.{v} D] : (C ⥤ D) ⥤ augmented C ⥤ augmented D := { obj := whiskering_obj _ _, map := λ X Y η, { app := λ A, { left := η.app _, right := whisker_left _ η } } } variable {C} end augmented open_locale simplicial /-- Augment a cosimplicial object with an object. -/ @[simps] def augment (X : cosimplicial_object C) (X₀ : C) (f : X₀ ⟶ X.obj [0]) (w : ∀ (i : simplex_category) (g₁ g₂ : [0] ⟶ i), f ≫ X.map g₁ = f ≫ X.map g₂) : cosimplicial_object.augmented C := { left := X₀, right := X, hom := { app := λ i, f ≫ X.map (simplex_category.const i 0), naturality' := begin intros i j g, dsimp, simpa [← X.map_comp] using w _ _ _, end } } @[simp] lemma augment_hom_zero (X : cosimplicial_object C) (X₀ : C) (f : X₀ ⟶ X.obj [0]) (w) : (X.augment X₀ f w).hom.app [0] = f := by { dsimp, rw [simplex_category.hom_zero_zero ([0].const 0), X.map_id, category.comp_id] } end cosimplicial_object /-- The anti-equivalence between simplicial objects and cosimplicial objects. -/ @[simps] def simplicial_cosimplicial_equiv : (simplicial_object C)ᵒᵖ ≌ (cosimplicial_object Cᵒᵖ) := functor.left_op_right_op_equiv _ _ variable {C} /-- Construct an augmented cosimplicial object in the opposite category from an augmented simplicial object. -/ @[simps] def simplicial_object.augmented.right_op (X : simplicial_object.augmented C) : cosimplicial_object.augmented Cᵒᵖ := { left := opposite.op X.right, right := X.left.right_op, hom := X.hom.right_op } /-- Construct an augmented simplicial object from an augmented cosimplicial object in the opposite category. -/ @[simps] def cosimplicial_object.augmented.left_op (X : cosimplicial_object.augmented Cᵒᵖ) : simplicial_object.augmented C := { left := X.right.left_op, right := X.left.unop, hom := X.hom.left_op } /-- Converting an augmented simplicial object to an augmented cosimplicial object and back is isomorphic to the given object. -/ @[simps] def simplicial_object.augmented.right_op_left_op_iso (X : simplicial_object.augmented C) : X.right_op.left_op ≅ X := comma.iso_mk X.left.right_op_left_op_iso (eq_to_iso $ by simp) (by tidy) /-- Converting an augmented cosimplicial object to an augmented simplicial object and back is isomorphic to the given object. -/ @[simps] def cosimplicial_object.augmented.left_op_right_op_iso (X : cosimplicial_object.augmented Cᵒᵖ) : X.left_op.right_op ≅ X := comma.iso_mk (eq_to_iso $ by simp) X.right.left_op_right_op_iso (by tidy) variable (C) /-- A functorial version of `simplicial_object.augmented.right_op`. -/ @[simps] def simplicial_to_cosimplicial_augmented : (simplicial_object.augmented C)ᵒᵖ ⥤ cosimplicial_object.augmented Cᵒᵖ := { obj := λ X, X.unop.right_op, map := λ X Y f, { left := f.unop.right.op, right := f.unop.left.right_op, w' := begin ext x, dsimp, simp_rw ← op_comp, congr' 1, exact (congr_app f.unop.w (op x)).symm, end } } /-- A functorial version of `cosimplicial_object.augmented.left_op`. -/ @[simps] def cosimplicial_to_simplicial_augmented : cosimplicial_object.augmented Cᵒᵖ ⥤ (simplicial_object.augmented C)ᵒᵖ := { obj := λ X, opposite.op X.left_op, map := λ X Y f, quiver.hom.op $ { left := f.right.left_op, right := f.left.unop, w' := begin ext x, dsimp, simp_rw ← unop_comp, congr' 1, exact (congr_app f.w x.unop).symm, end} } /-- The contravariant categorical equivalence between augmented simplicial objects and augmented cosimplicial objects in the opposite category. -/ @[simps] def simplicial_cosimplicial_augmented_equiv : (simplicial_object.augmented C)ᵒᵖ ≌ cosimplicial_object.augmented Cᵒᵖ := { functor := simplicial_to_cosimplicial_augmented _, inverse := cosimplicial_to_simplicial_augmented _, unit_iso := nat_iso.of_components (λ X, X.unop.right_op_left_op_iso.op) begin intros X Y f, dsimp, rw (show f = f.unop.op, by simp), simp_rw ← op_comp, congr' 1, tidy, end, counit_iso := nat_iso.of_components (λ X, X.left_op_right_op_iso) (by tidy) } end category_theory
04d9c2519107d6069734c90527c0405b510e65a6
13133fade54057ee588bc056e4eaa14a24773d23
/Definitions/mchoosek_def.lean
e5da3ea275a8da58d5c8dd83d07324668b79f9a6
[]
no_license
lkloh/lean-project-15815
444cbdca1d1a2dfa258c76c41a6ff846392e13d1
2cb657c0e41baa318193f7dce85974ff37d80883
refs/heads/master
1,611,402,038,933
1,432,020,760,000
1,432,020,760,000
33,372,120
0
0
null
1,431,932,928,000
1,428,078,840,000
Lean
UTF-8
Lean
false
false
250
lean
import data.nat open nat definition fac : nat → nat | fac 0 := 1 | fac 1 := 1 | fac (n+2) := fac (n+1) * (n+2) eval fac 3 definition mchoosen (m n : ℕ ) : ℕ := divide (fac m) (fac(m-n) * fac n) eval mchoosen 5 1 eval mchoosen 5 2
10491226a62ae6afb64e85c85ce1fac3c37762bd
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Lean/Compiler/ClosedTermCache.lean
8ebf01b778ca380d8e579e3f937d3ae6f7617ed5
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
963
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.Environment namespace Lean structure ClosedTermCache where map : Std.PHashMap Expr Name := {} constNames : NameSet := {} deriving Inhabited builtin_initialize closedTermCacheExt : EnvExtension ClosedTermCache ← registerEnvExtension (pure {}) @[export lean_cache_closed_term_name] def cacheClosedTermName (env : Environment) (e : Expr) (n : Name) : Environment := closedTermCacheExt.modifyState env fun s => { s with map := s.map.insert e n, constNames := s.constNames.insert n } @[export lean_get_closed_term_name] def getClosedTermName? (env : Environment) (e : Expr) : Option Name := (closedTermCacheExt.getState env).map.find? e def isClosedTermName (env : Environment) (n : Name) : Bool := (closedTermCacheExt.getState env).constNames.contains n end Lean
e1a4778d0eafb57e3aac3418e3ce331a8c08b77e
137c667471a40116a7afd7261f030b30180468c2
/src/data/fintype/basic.lean
429566dc769c66fd8bd7118d3777367f9c25efc9
[ "Apache-2.0" ]
permissive
bragadeesh153/mathlib
46bf814cfb1eecb34b5d1549b9117dc60f657792
b577bb2cd1f96eb47031878256856020b76f73cd
refs/heads/master
1,687,435,188,334
1,626,384,207,000
1,626,384,207,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
69,187
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Finite types. -/ import tactic.wlog import data.finset.powerset import data.finset.lattice import data.finset.pi import data.array.lemmas import order.well_founded import group_theory.perm.basic open_locale nat universes u v variables {α : Type*} {β : Type*} {γ : Type*} /-- `fintype α` means that `α` is finite, i.e. there are only finitely many distinct elements of type `α`. The evidence of this is a finset `elems` (a list up to permutation without duplicates), together with a proof that everything of type `α` is in the list. -/ class fintype (α : Type*) := (elems [] : finset α) (complete : ∀ x : α, x ∈ elems) namespace finset variable [fintype α] /-- `univ` is the universal finite set of type `finset α` implied from the assumption `fintype α`. -/ def univ : finset α := fintype.elems α @[simp] theorem mem_univ (x : α) : x ∈ (univ : finset α) := fintype.complete x @[simp] theorem mem_univ_val : ∀ x, x ∈ (univ : finset α).1 := mem_univ @[simp] lemma coe_univ : ↑(univ : finset α) = (set.univ : set α) := by ext; simp lemma univ_nonempty_iff : (univ : finset α).nonempty ↔ nonempty α := by rw [← coe_nonempty, coe_univ, set.nonempty_iff_univ_nonempty] lemma univ_nonempty [nonempty α] : (univ : finset α).nonempty := univ_nonempty_iff.2 ‹_› lemma univ_eq_empty : (univ : finset α) = ∅ ↔ ¬nonempty α := by rw [← univ_nonempty_iff, nonempty_iff_ne_empty, ne.def, not_not] lemma univ_eq_empty' : (univ : finset α) = ∅ ↔ is_empty α := univ_eq_empty.trans (not_nonempty_iff) @[simp] theorem subset_univ (s : finset α) : s ⊆ univ := λ a _, mem_univ a instance : order_top (finset α) := { top := univ, le_top := subset_univ, .. finset.partial_order } instance [decidable_eq α] : boolean_algebra (finset α) := { compl := λ s, univ \ s, inf_compl_le_bot := λ s x hx, by simpa using hx, top_le_sup_compl := λ s x hx, by simp, sdiff_eq := λ s t, by simp [ext_iff, compl], ..finset.order_top, ..finset.generalized_boolean_algebra } lemma compl_eq_univ_sdiff [decidable_eq α] (s : finset α) : sᶜ = univ \ s := rfl @[simp] lemma mem_compl [decidable_eq α] {s : finset α} {x : α} : x ∈ sᶜ ↔ x ∉ s := by simp [compl_eq_univ_sdiff] @[simp, norm_cast] lemma coe_compl [decidable_eq α] (s : finset α) : ↑(sᶜ) = (↑s : set α)ᶜ := set.ext $ λ x, mem_compl @[simp] theorem union_compl [decidable_eq α] (s : finset α) : s ∪ sᶜ = finset.univ := sup_compl_eq_top @[simp] lemma compl_filter [decidable_eq α] (p : α → Prop) [decidable_pred p] [Π x, decidable (¬p x)] : (univ.filter p)ᶜ = univ.filter (λ x, ¬p x) := (filter_not _ _).symm theorem eq_univ_iff_forall {s : finset α} : s = univ ↔ ∀ x, x ∈ s := by simp [ext_iff] lemma compl_ne_univ_iff_nonempty [decidable_eq α] (s : finset α) : sᶜ ≠ univ ↔ s.nonempty := by simp [eq_univ_iff_forall, finset.nonempty] @[simp] lemma univ_inter [decidable_eq α] (s : finset α) : univ ∩ s = s := ext $ λ a, by simp @[simp] lemma inter_univ [decidable_eq α] (s : finset α) : s ∩ univ = s := by rw [inter_comm, univ_inter] @[simp] lemma piecewise_univ [∀i : α, decidable (i ∈ (univ : finset α))] {δ : α → Sort*} (f g : Πi, δ i) : univ.piecewise f g = f := by { ext i, simp [piecewise] } lemma piecewise_compl [decidable_eq α] (s : finset α) [Π i : α, decidable (i ∈ s)] [Π i : α, decidable (i ∈ sᶜ)] {δ : α → Sort*} (f g : Π i, δ i) : sᶜ.piecewise f g = s.piecewise g f := by { ext i, simp [piecewise] } lemma univ_map_equiv_to_embedding {α β : Type*} [fintype α] [fintype β] (e : α ≃ β) : univ.map e.to_embedding = univ := begin apply eq_univ_iff_forall.mpr, intro b, rw [mem_map], use e.symm b, simp, end @[simp] lemma univ_filter_exists (f : α → β) [fintype β] [decidable_pred (λ y, ∃ x, f x = y)] [decidable_eq β] : finset.univ.filter (λ y, ∃ x, f x = y) = finset.univ.image f := by { ext, simp } /-- Note this is a special case of `(finset.image_preimage f univ _).symm`. -/ lemma univ_filter_mem_range (f : α → β) [fintype β] [decidable_pred (λ y, y ∈ set.range f)] [decidable_eq β] : finset.univ.filter (λ y, y ∈ set.range f) = finset.univ.image f := univ_filter_exists f end finset open finset function namespace fintype instance decidable_pi_fintype {α} {β : α → Type*} [∀a, decidable_eq (β a)] [fintype α] : decidable_eq (Πa, β a) := assume f g, decidable_of_iff (∀ a ∈ fintype.elems α, f a = g a) (by simp [function.funext_iff, fintype.complete]) instance decidable_forall_fintype {p : α → Prop} [decidable_pred p] [fintype α] : decidable (∀ a, p a) := decidable_of_iff (∀ a ∈ @univ α _, p a) (by simp) instance decidable_exists_fintype {p : α → Prop} [decidable_pred p] [fintype α] : decidable (∃ a, p a) := decidable_of_iff (∃ a ∈ @univ α _, p a) (by simp) instance decidable_mem_range_fintype [fintype α] [decidable_eq β] (f : α → β) : decidable_pred (∈ set.range f) := λ x, fintype.decidable_exists_fintype section bundled_homs instance decidable_eq_equiv_fintype [decidable_eq β] [fintype α] : decidable_eq (α ≃ β) := λ a b, decidable_of_iff (a.1 = b.1) equiv.coe_fn_injective.eq_iff instance decidable_eq_embedding_fintype [decidable_eq β] [fintype α] : decidable_eq (α ↪ β) := λ a b, decidable_of_iff (⇑a = b) function.embedding.coe_injective.eq_iff @[to_additive] instance decidable_eq_one_hom_fintype [decidable_eq β] [fintype α] [has_one α] [has_one β]: decidable_eq (one_hom α β) := λ a b, decidable_of_iff (⇑a = b) (injective.eq_iff one_hom.coe_inj) @[to_additive] instance decidable_eq_mul_hom_fintype [decidable_eq β] [fintype α] [has_mul α] [has_mul β]: decidable_eq (mul_hom α β) := λ a b, decidable_of_iff (⇑a = b) (injective.eq_iff mul_hom.coe_inj) @[to_additive] instance decidable_eq_monoid_hom_fintype [decidable_eq β] [fintype α] [mul_one_class α] [mul_one_class β]: decidable_eq (α →* β) := λ a b, decidable_of_iff (⇑a = b) (injective.eq_iff monoid_hom.coe_inj) instance decidable_eq_monoid_with_zero_hom_fintype [decidable_eq β] [fintype α] [mul_zero_one_class α] [mul_zero_one_class β]: decidable_eq (monoid_with_zero_hom α β) := λ a b, decidable_of_iff (⇑a = b) (injective.eq_iff monoid_with_zero_hom.coe_inj) instance decidable_eq_ring_hom_fintype [decidable_eq β] [fintype α] [semiring α] [semiring β]: decidable_eq (α →+* β) := λ a b, decidable_of_iff (⇑a = b) (injective.eq_iff ring_hom.coe_inj) end bundled_homs instance decidable_injective_fintype [decidable_eq α] [decidable_eq β] [fintype α] : decidable_pred (injective : (α → β) → Prop) := λ x, by unfold injective; apply_instance instance decidable_surjective_fintype [decidable_eq β] [fintype α] [fintype β] : decidable_pred (surjective : (α → β) → Prop) := λ x, by unfold surjective; apply_instance instance decidable_bijective_fintype [decidable_eq α] [decidable_eq β] [fintype α] [fintype β] : decidable_pred (bijective : (α → β) → Prop) := λ x, by unfold bijective; apply_instance instance decidable_right_inverse_fintype [decidable_eq α] [fintype α] (f : α → β) (g : β → α) : decidable (function.right_inverse f g) := show decidable (∀ x, g (f x) = x), by apply_instance instance decidable_left_inverse_fintype [decidable_eq β] [fintype β] (f : α → β) (g : β → α) : decidable (function.left_inverse f g) := show decidable (∀ x, f (g x) = x), by apply_instance lemma exists_max [fintype α] [nonempty α] {β : Type*} [linear_order β] (f : α → β) : ∃ x₀ : α, ∀ x, f x ≤ f x₀ := by simpa using exists_max_image univ f univ_nonempty lemma exists_min [fintype α] [nonempty α] {β : Type*} [linear_order β] (f : α → β) : ∃ x₀ : α, ∀ x, f x₀ ≤ f x := by simpa using exists_min_image univ f univ_nonempty /-- Construct a proof of `fintype α` from a universal multiset -/ def of_multiset [decidable_eq α] (s : multiset α) (H : ∀ x : α, x ∈ s) : fintype α := ⟨s.to_finset, by simpa using H⟩ /-- Construct a proof of `fintype α` from a universal list -/ def of_list [decidable_eq α] (l : list α) (H : ∀ x : α, x ∈ l) : fintype α := ⟨l.to_finset, by simpa using H⟩ theorem exists_univ_list (α) [fintype α] : ∃ l : list α, l.nodup ∧ ∀ x : α, x ∈ l := let ⟨l, e⟩ := quotient.exists_rep (@univ α _).1 in by have := and.intro univ.2 mem_univ_val; exact ⟨_, by rwa ← e at this⟩ /-- `card α` is the number of elements in `α`, defined when `α` is a fintype. -/ def card (α) [fintype α] : ℕ := (@univ α _).card /-- If `l` lists all the elements of `α` without duplicates, then `α ≃ fin (l.length)`. -/ def equiv_fin_of_forall_mem_list {α} [decidable_eq α] {l : list α} (h : ∀ x:α, x ∈ l) (nd : l.nodup) : α ≃ fin (l.length) := ⟨λ a, ⟨_, list.index_of_lt_length.2 (h a)⟩, λ i, l.nth_le i.1 i.2, λ a, by simp, λ ⟨i, h⟩, fin.eq_of_veq $ list.nodup_iff_nth_le_inj.1 nd _ _ (list.index_of_lt_length.2 (list.nth_le_mem _ _ _)) h $ by simp⟩ /-- There is (computably) a bijection between `α` and `fin (card α)`. Since it is not unique, and depends on which permutation of the universe list is used, the bijection is wrapped in `trunc` to preserve computability. See `fintype.equiv_fin` for the noncomputable version, and `fintype.trunc_equiv_fin_of_card_eq` and `fintype.equiv_fin_of_card_eq` for an equiv `α ≃ fin n` given `fintype.card α = n`. -/ def trunc_equiv_fin (α) [decidable_eq α] [fintype α] : trunc (α ≃ fin (card α)) := by unfold card finset.card; exact quot.rec_on_subsingleton (@univ α _).1 (λ l (h : ∀ x:α, x ∈ l) (nd : l.nodup), trunc.mk (equiv_fin_of_forall_mem_list h nd)) mem_univ_val univ.2 /-- There is a (noncomputable) bijection between `α` and `fin (card α)`. See `fintype.trunc_equiv_fin` for the computable version, and `fintype.trunc_equiv_fin_of_card_eq` and `fintype.equiv_fin_of_card_eq` for an equiv `α ≃ fin n` given `fintype.card α = n`. -/ noncomputable def equiv_fin (α) [fintype α] : α ≃ fin (card α) := by { letI := classical.dec_eq α, exact (trunc_equiv_fin α).out } instance (α : Type*) : subsingleton (fintype α) := ⟨λ ⟨s₁, h₁⟩ ⟨s₂, h₂⟩, by congr; simp [finset.ext_iff, h₁, h₂]⟩ /-- Given a predicate that can be represented by a finset, the subtype associated to the predicate is a fintype. -/ protected def subtype {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) : fintype {x // p x} := ⟨⟨multiset.pmap subtype.mk s.1 (λ x, (H x).1), multiset.nodup_pmap (λ a _ b _, congr_arg subtype.val) s.2⟩, λ ⟨x, px⟩, multiset.mem_pmap.2 ⟨x, (H x).2 px, rfl⟩⟩ theorem subtype_card {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) : @card {x // p x} (fintype.subtype s H) = s.card := multiset.card_pmap _ _ _ theorem card_of_subtype {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) [fintype {x // p x}] : card {x // p x} = s.card := by { rw ← subtype_card s H, congr } /-- Construct a fintype from a finset with the same elements. -/ def of_finset {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : fintype p := fintype.subtype s H @[simp] theorem card_of_finset {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : @fintype.card p (of_finset s H) = s.card := fintype.subtype_card s H theorem card_of_finset' {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) [fintype p] : fintype.card p = s.card := by rw ← card_of_finset s H; congr /-- If `f : α → β` is a bijection and `α` is a fintype, then `β` is also a fintype. -/ def of_bijective [fintype α] (f : α → β) (H : function.bijective f) : fintype β := ⟨univ.map ⟨f, H.1⟩, λ b, let ⟨a, e⟩ := H.2 b in e ▸ mem_map_of_mem _ (mem_univ _)⟩ /-- If `f : α → β` is a surjection and `α` is a fintype, then `β` is also a fintype. -/ def of_surjective [decidable_eq β] [fintype α] (f : α → β) (H : function.surjective f) : fintype β := ⟨univ.image f, λ b, let ⟨a, e⟩ := H b in e ▸ mem_image_of_mem _ (mem_univ _)⟩ end fintype section inv namespace function variables [fintype α] [decidable_eq β] namespace injective variables {f : α → β} (hf : function.injective f) /-- The inverse of an `hf : injective` function `f : α → β`, of the type `↥(set.range f) → α`. This is the computable version of `function.inv_fun` that requires `fintype α` and `decidable_eq β`, or the function version of applying `(equiv.of_injective f hf).symm`. This function should not usually be used for actual computation because for most cases, an explicit inverse can be stated that has better computational properties. This function computes by checking all terms `a : α` to find the `f a = b`, so it is O(N) where `N = fintype.card α`. -/ def inv_of_mem_range : set.range f → α := λ b, finset.choose (λ a, f a = b) finset.univ ((exists_unique_congr (by simp)).mp (hf.exists_unique_of_mem_range b.property)) lemma left_inv_of_inv_of_mem_range (b : set.range f) : f (hf.inv_of_mem_range b) = b := (finset.choose_spec (λ a, f a = b) _ _).right @[simp] lemma right_inv_of_inv_of_mem_range (a : α) : hf.inv_of_mem_range (⟨f a, set.mem_range_self a⟩) = a := hf (finset.choose_spec (λ a', f a' = f a) _ _).right lemma inv_fun_restrict [nonempty α] : (set.range f).restrict (inv_fun f) = hf.inv_of_mem_range := begin ext ⟨b, h⟩, apply hf, simp [hf.left_inv_of_inv_of_mem_range, @inv_fun_eq _ _ _ f b (set.mem_range.mp h)] end lemma inv_of_mem_range_surjective : function.surjective hf.inv_of_mem_range := λ a, ⟨⟨f a, set.mem_range_self a⟩, by simp⟩ end injective namespace embedding variables (f : α ↪ β) (b : set.range f) /-- The inverse of an embedding `f : α ↪ β`, of the type `↥(set.range f) → α`. This is the computable version of `function.inv_fun` that requires `fintype α` and `decidable_eq β`, or the function version of applying `(equiv.of_injective f f.injective).symm`. This function should not usually be used for actual computation because for most cases, an explicit inverse can be stated that has better computational properties. This function computes by checking all terms `a : α` to find the `f a = b`, so it is O(N) where `N = fintype.card α`. -/ def inv_of_mem_range : α := f.injective.inv_of_mem_range b @[simp] lemma left_inv_of_inv_of_mem_range : f (f.inv_of_mem_range b) = b := f.injective.left_inv_of_inv_of_mem_range b @[simp] lemma right_inv_of_inv_of_mem_range (a : α) : f.inv_of_mem_range ⟨f a, set.mem_range_self a⟩ = a := f.injective.right_inv_of_inv_of_mem_range a lemma inv_fun_restrict [nonempty α] : (set.range f).restrict (inv_fun f) = f.inv_of_mem_range := begin ext ⟨b, h⟩, apply f.injective, simp [f.left_inv_of_inv_of_mem_range, @inv_fun_eq _ _ _ f b (set.mem_range.mp h)] end lemma inv_of_mem_range_surjective : function.surjective f.inv_of_mem_range := λ a, ⟨⟨f a, set.mem_range_self a⟩, by simp⟩ end embedding end function end inv namespace fintype /-- Given an injective function to a fintype, the domain is also a fintype. This is noncomputable because injectivity alone cannot be used to construct preimages. -/ noncomputable def of_injective [fintype β] (f : α → β) (H : function.injective f) : fintype α := by letI := classical.dec; exact if hα : nonempty α then by letI := classical.inhabited_of_nonempty hα; exact of_surjective (inv_fun f) (inv_fun_surjective H) else ⟨∅, λ x, (hα ⟨x⟩).elim⟩ /-- If `f : α ≃ β` and `α` is a fintype, then `β` is also a fintype. -/ def of_equiv (α : Type*) [fintype α] (f : α ≃ β) : fintype β := of_bijective _ f.bijective theorem of_equiv_card [fintype α] (f : α ≃ β) : @card β (of_equiv α f) = card α := multiset.card_map _ _ theorem card_congr {α β} [fintype α] [fintype β] (f : α ≃ β) : card α = card β := by rw ← of_equiv_card f; congr section variables [fintype α] [fintype β] /-- If the cardinality of `α` is `n`, there is computably a bijection between `α` and `fin n`. See `fintype.equiv_fin_of_card_eq` for the noncomputable definition, and `fintype.trunc_equiv_fin` and `fintype.equiv_fin` for the bijection `α ≃ fin (card α)`. -/ def trunc_equiv_fin_of_card_eq [decidable_eq α] {n : ℕ} (h : fintype.card α = n) : trunc (α ≃ fin n) := (trunc_equiv_fin α).map (λ e, e.trans (fin.cast h).to_equiv) /-- If the cardinality of `α` is `n`, there is noncomputably a bijection between `α` and `fin n`. See `fintype.trunc_equiv_fin_of_card_eq` for the computable definition, and `fintype.trunc_equiv_fin` and `fintype.equiv_fin` for the bijection `α ≃ fin (card α)`. -/ noncomputable def equiv_fin_of_card_eq {n : ℕ} (h : fintype.card α = n) : α ≃ fin n := by { letI := classical.dec_eq α, exact (trunc_equiv_fin_of_card_eq h).out } /-- Two `fintype`s with the same cardinality are (computably) in bijection. See `fintype.equiv_of_card_eq` for the noncomputable version, and `fintype.trunc_equiv_fin_of_card_eq` and `fintype.equiv_fin_of_card_eq` for the specialization to `fin`. -/ def trunc_equiv_of_card_eq [decidable_eq α] [decidable_eq β] (h : card α = card β) : trunc (α ≃ β) := (trunc_equiv_fin_of_card_eq h).bind (λ e, (trunc_equiv_fin β).map (λ e', e.trans e'.symm)) /-- Two `fintype`s with the same cardinality are (noncomputably) in bijection. See `fintype.trunc_equiv_of_card_eq` for the computable version, and `fintype.trunc_equiv_fin_of_card_eq` and `fintype.equiv_fin_of_card_eq` for the specialization to `fin`. -/ noncomputable def equiv_of_card_eq (h : card α = card β) : α ≃ β := by { letI := classical.dec_eq α, letI := classical.dec_eq β, exact (trunc_equiv_of_card_eq h).out } end theorem card_eq {α β} [F : fintype α] [G : fintype β] : card α = card β ↔ nonempty (α ≃ β) := ⟨λ h, by { haveI := classical.prop_decidable, exact (trunc_equiv_of_card_eq h).nonempty }, λ ⟨f⟩, card_congr f⟩ /-- Any subsingleton type with a witness is a fintype (with one term). -/ def of_subsingleton (a : α) [subsingleton α] : fintype α := ⟨{a}, λ b, finset.mem_singleton.2 (subsingleton.elim _ _)⟩ @[simp] theorem univ_of_subsingleton (a : α) [subsingleton α] : @univ _ (of_subsingleton a) = {a} := rfl /-- Note: this lemma is specifically about `fintype.of_subsingleton`. For a statement about arbitrary `fintype` instances, use either `fintype.card_le_one_iff_subsingleton` or `fintype.card_unique`. -/ @[simp] theorem card_of_subsingleton (a : α) [subsingleton α] : @fintype.card _ (of_subsingleton a) = 1 := rfl @[simp] theorem card_unique [unique α] [h : fintype α] : fintype.card α = 1 := subsingleton.elim (of_subsingleton $ default α) h ▸ card_of_subsingleton _ @[priority 100] -- see Note [lower instance priority] instance of_is_empty [is_empty α] : fintype α := ⟨∅, is_empty_elim⟩ /-- Note: this lemma is specifically about `fintype.of_is_empty`. For a statement about arbitrary `fintype` instances, use `fintype.univ_eq_empty'`. -/ @[simp] theorem univ_of_is_empty [is_empty α] : @univ α _ = ∅ := rfl /-- Note: this lemma is specifically about `fintype.of_is_empty`. For a statement about arbitrary `fintype` instances, use `fintype.card_eq_zero_iff`. -/ @[simp] theorem card_of_is_empty [is_empty α] : fintype.card α = 0 := rfl open_locale classical variables (α) /-- Any subsingleton type is (noncomputably) a fintype (with zero or one terms). -/ @[priority 5] -- see Note [lower instance priority] noncomputable instance of_subsingleton' [subsingleton α] : fintype α := if h : nonempty α then of_subsingleton (nonempty.some h) else @fintype.of_is_empty _ $ not_nonempty_iff.mp h end fintype namespace set /-- Construct a finset enumerating a set `s`, given a `fintype` instance. -/ def to_finset (s : set α) [fintype s] : finset α := ⟨(@finset.univ s _).1.map subtype.val, multiset.nodup_map (λ a b, subtype.eq) finset.univ.2⟩ @[simp] theorem mem_to_finset {s : set α} [fintype s] {a : α} : a ∈ s.to_finset ↔ a ∈ s := by simp [to_finset] @[simp] theorem mem_to_finset_val {s : set α} [fintype s] {a : α} : a ∈ s.to_finset.1 ↔ a ∈ s := mem_to_finset -- We use an arbitrary `[fintype s]` instance here, -- not necessarily coming from a `[fintype α]`. @[simp] lemma to_finset_card {α : Type*} (s : set α) [fintype s] : s.to_finset.card = fintype.card s := multiset.card_map subtype.val finset.univ.val @[simp] theorem coe_to_finset (s : set α) [fintype s] : (↑s.to_finset : set α) = s := set.ext $ λ _, mem_to_finset @[simp] theorem to_finset_inj {s t : set α} [fintype s] [fintype t] : s.to_finset = t.to_finset ↔ s = t := ⟨λ h, by rw [← s.coe_to_finset, h, t.coe_to_finset], λ h, by simp [h]; congr⟩ @[simp, mono] theorem to_finset_mono {s t : set α} [fintype s] [fintype t] : s.to_finset ⊆ t.to_finset ↔ s ⊆ t := by simp [finset.subset_iff, set.subset_def] @[simp, mono] theorem to_finset_strict_mono {s t : set α} [fintype s] [fintype t] : s.to_finset ⊂ t.to_finset ↔ s ⊂ t := begin rw [←lt_eq_ssubset, ←finset.lt_iff_ssubset, lt_iff_le_and_ne, lt_iff_le_and_ne], simp end @[simp] theorem to_finset_disjoint_iff [decidable_eq α] {s t : set α} [fintype s] [fintype t] : disjoint s.to_finset t.to_finset ↔ disjoint s t := ⟨λ h x hx, h (by simpa using hx), λ h x hx, h (by simpa using hx)⟩ end set lemma finset.card_univ [fintype α] : (finset.univ : finset α).card = fintype.card α := rfl lemma finset.eq_univ_of_card [fintype α] (s : finset α) (hs : s.card = fintype.card α) : s = univ := eq_of_subset_of_card_le (subset_univ _) $ by rw [hs, finset.card_univ] lemma finset.card_eq_iff_eq_univ [fintype α] (s : finset α) : s.card = fintype.card α ↔ s = finset.univ := ⟨s.eq_univ_of_card, by { rintro rfl, exact finset.card_univ, }⟩ lemma finset.card_le_univ [fintype α] (s : finset α) : s.card ≤ fintype.card α := card_le_of_subset (subset_univ s) lemma finset.card_lt_univ_of_not_mem [fintype α] {s : finset α} {x : α} (hx : x ∉ s) : s.card < fintype.card α := card_lt_card ⟨subset_univ s, not_forall.2 ⟨x, λ hx', hx (hx' $ mem_univ x)⟩⟩ lemma finset.card_lt_iff_ne_univ [fintype α] (s : finset α) : s.card < fintype.card α ↔ s ≠ finset.univ := s.card_le_univ.lt_iff_ne.trans (not_iff_not_of_iff s.card_eq_iff_eq_univ) lemma finset.card_compl_lt_iff_nonempty [fintype α] [decidable_eq α] (s : finset α) : sᶜ.card < fintype.card α ↔ s.nonempty := sᶜ.card_lt_iff_ne_univ.trans s.compl_ne_univ_iff_nonempty lemma finset.card_univ_diff [decidable_eq α] [fintype α] (s : finset α) : (finset.univ \ s).card = fintype.card α - s.card := finset.card_sdiff (subset_univ s) lemma finset.card_compl [decidable_eq α] [fintype α] (s : finset α) : sᶜ.card = fintype.card α - s.card := finset.card_univ_diff s instance (n : ℕ) : fintype (fin n) := ⟨finset.fin_range n, finset.mem_fin_range⟩ lemma fin.univ_def (n : ℕ) : (univ : finset (fin n)) = finset.fin_range n := rfl @[simp] theorem fintype.card_fin (n : ℕ) : fintype.card (fin n) = n := list.length_fin_range n @[simp] lemma finset.card_fin (n : ℕ) : finset.card (finset.univ : finset (fin n)) = n := by rw [finset.card_univ, fintype.card_fin] lemma card_finset_fin_le {n : ℕ} (s : finset (fin n)) : s.card ≤ n := by simpa only [fintype.card_fin] using s.card_le_univ lemma fin.equiv_iff_eq {m n : ℕ} : nonempty (fin m ≃ fin n) ↔ m = n := ⟨λ ⟨h⟩, by simpa using fintype.card_congr h, λ h, ⟨equiv.cast $ h ▸ rfl ⟩ ⟩ /-- Embed `fin n` into `fin (n + 1)` by prepending zero to the `univ` -/ lemma fin.univ_succ (n : ℕ) : (univ : finset (fin (n + 1))) = insert 0 (univ.image fin.succ) := begin ext m, simp only [mem_univ, mem_insert, true_iff, mem_image, exists_prop], exact fin.cases (or.inl rfl) (λ i, or.inr ⟨i, trivial, rfl⟩) m end /-- Embed `fin n` into `fin (n + 1)` by appending a new `fin.last n` to the `univ` -/ lemma fin.univ_cast_succ (n : ℕ) : (univ : finset (fin (n + 1))) = insert (fin.last n) (univ.image fin.cast_succ) := begin ext m, simp only [mem_univ, mem_insert, true_iff, mem_image, exists_prop, true_and], by_cases h : m.val < n, { right, use fin.cast_lt m h, rw fin.cast_succ_cast_lt }, { left, exact fin.eq_last_of_not_lt h } end /-- Embed `fin n` into `fin (n + 1)` by inserting around a specified pivot `p : fin (n + 1)` into the `univ` -/ lemma fin.univ_succ_above (n : ℕ) (p : fin (n + 1)) : (univ : finset (fin (n + 1))) = insert p (univ.image (fin.succ_above p)) := begin rcases lt_or_eq_of_le (fin.le_last p) with hl|rfl, { ext m, simp only [finset.mem_univ, finset.mem_insert, true_iff, finset.mem_image, exists_prop], refine or_iff_not_imp_left.mpr _, { intro h, cases n, { have : m = p := by simp, exact absurd this h }, use p.cast_pred.pred_above m, { rw fin.pred_above, split_ifs with H, { simp only [fin.coe_cast_succ, true_and, fin.coe_coe_eq_self, coe_coe], rw fin.lt_last_iff_coe_cast_pred at hl, rw fin.succ_above_above, { simp }, { simp only [fin.lt_iff_coe_lt_coe, fin.coe_cast_succ] at H, simpa [fin.le_iff_coe_le_coe, ←hl] using nat.le_pred_of_lt H } }, { rw fin.succ_above_below, { simp }, { simp only [fin.cast_succ_cast_pred hl, not_lt] at H, simpa using lt_of_le_of_ne H h, } } } } }, { rw fin.succ_above_last, exact fin.univ_cast_succ n } end @[instance, priority 10] def unique.fintype {α : Type*} [unique α] : fintype α := fintype.of_subsingleton (default α) @[simp] lemma univ_unique {α : Type*} [unique α] [f : fintype α] : @finset.univ α _ = {default α} := by rw [subsingleton.elim f (@unique.fintype α _)]; refl @[simp] theorem fintype.univ_empty : @univ empty _ = ∅ := rfl @[simp] theorem fintype.card_empty : fintype.card empty = 0 := rfl @[simp] theorem fintype.univ_pempty : @univ pempty _ = ∅ := rfl @[simp] theorem fintype.card_pempty : fintype.card pempty = 0 := rfl instance : fintype unit := fintype.of_subsingleton () theorem fintype.univ_unit : @univ unit _ = {()} := rfl theorem fintype.card_unit : fintype.card unit = 1 := rfl instance : fintype punit := fintype.of_subsingleton punit.star @[simp] theorem fintype.univ_punit : @univ punit _ = {punit.star} := rfl @[simp] theorem fintype.card_punit : fintype.card punit = 1 := rfl instance : fintype bool := ⟨⟨tt ::ₘ ff ::ₘ 0, by simp⟩, λ x, by cases x; simp⟩ @[simp] theorem fintype.univ_bool : @univ bool _ = {tt, ff} := rfl instance units_int.fintype : fintype (units ℤ) := ⟨{1, -1}, λ x, by cases int.units_eq_one_or x; simp *⟩ @[simp] lemma units_int.univ : (finset.univ : finset (units ℤ)) = {1, -1} := rfl instance additive.fintype : Π [fintype α], fintype (additive α) := id instance multiplicative.fintype : Π [fintype α], fintype (multiplicative α) := id @[simp] theorem fintype.card_units_int : fintype.card (units ℤ) = 2 := rfl noncomputable instance [monoid α] [fintype α] : fintype (units α) := by classical; exact fintype.of_injective units.val units.ext @[simp] theorem fintype.card_bool : fintype.card bool = 2 := rfl /-- Given a finset on `α`, lift it to being a finset on `option α` using `option.some` and then insert `option.none`. -/ def finset.insert_none (s : finset α) : finset (option α) := ⟨none ::ₘ s.1.map some, multiset.nodup_cons.2 ⟨by simp, multiset.nodup_map (λ a b, option.some.inj) s.2⟩⟩ @[simp] theorem finset.mem_insert_none {s : finset α} : ∀ {o : option α}, o ∈ s.insert_none ↔ ∀ a ∈ o, a ∈ s | none := iff_of_true (multiset.mem_cons_self _ _) (λ a h, by cases h) | (some a) := multiset.mem_cons.trans $ by simp; refl theorem finset.some_mem_insert_none {s : finset α} {a : α} : some a ∈ s.insert_none ↔ a ∈ s := by simp instance {α : Type*} [fintype α] : fintype (option α) := ⟨univ.insert_none, λ a, by simp⟩ @[simp] theorem fintype.card_option {α : Type*} [fintype α] : fintype.card (option α) = fintype.card α + 1 := (multiset.card_cons _ _).trans (by rw multiset.card_map; refl) instance {α : Type*} (β : α → Type*) [fintype α] [∀ a, fintype (β a)] : fintype (sigma β) := ⟨univ.sigma (λ _, univ), λ ⟨a, b⟩, by simp⟩ @[simp] lemma finset.univ_sigma_univ {α : Type*} {β : α → Type*} [fintype α] [∀ a, fintype (β a)] : (univ : finset α).sigma (λ a, (univ : finset (β a))) = univ := rfl instance (α β : Type*) [fintype α] [fintype β] : fintype (α × β) := ⟨univ.product univ, λ ⟨a, b⟩, by simp⟩ @[simp] lemma finset.univ_product_univ {α β : Type*} [fintype α] [fintype β] : (univ : finset α).product (univ : finset β) = univ := rfl @[simp] theorem fintype.card_prod (α β : Type*) [fintype α] [fintype β] : fintype.card (α × β) = fintype.card α * fintype.card β := card_product _ _ /-- Given that `α × β` is a fintype, `α` is also a fintype. -/ def fintype.prod_left {α β} [decidable_eq α] [fintype (α × β)] [nonempty β] : fintype α := ⟨(fintype.elems (α × β)).image prod.fst, assume a, let ⟨b⟩ := ‹nonempty β› in by simp; exact ⟨b, fintype.complete _⟩⟩ /-- Given that `α × β` is a fintype, `β` is also a fintype. -/ def fintype.prod_right {α β} [decidable_eq β] [fintype (α × β)] [nonempty α] : fintype β := ⟨(fintype.elems (α × β)).image prod.snd, assume b, let ⟨a⟩ := ‹nonempty α› in by simp; exact ⟨a, fintype.complete _⟩⟩ instance (α : Type*) [fintype α] : fintype (ulift α) := fintype.of_equiv _ equiv.ulift.symm @[simp] theorem fintype.card_ulift (α : Type*) [fintype α] : fintype.card (ulift α) = fintype.card α := fintype.of_equiv_card _ lemma univ_sum_type {α β : Type*} [fintype α] [fintype β] [fintype (α ⊕ β)] [decidable_eq (α ⊕ β)] : (univ : finset (α ⊕ β)) = map function.embedding.inl univ ∪ map function.embedding.inr univ := begin rw [eq_comm, eq_univ_iff_forall], simp only [mem_union, mem_map, exists_prop, mem_univ, true_and], rintro (x|y), exacts [or.inl ⟨x, rfl⟩, or.inr ⟨y, rfl⟩] end instance (α : Type u) (β : Type v) [fintype α] [fintype β] : fintype (α ⊕ β) := @fintype.of_equiv _ _ (@sigma.fintype _ (λ b, cond b (ulift α) (ulift.{(max u v) v} β)) _ (λ b, by cases b; apply ulift.fintype)) ((equiv.sum_equiv_sigma_bool _ _).symm.trans (equiv.sum_congr equiv.ulift equiv.ulift)) /-- Given that `α ⊕ β` is a fintype, `α` is also a fintype. This is non-computable as it uses that `sum.inl` is an injection, but there's no clear inverse if `α` is empty. -/ noncomputable def fintype.sum_left {α β} [fintype (α ⊕ β)] : fintype α := fintype.of_injective (sum.inl : α → α ⊕ β) sum.inl_injective /-- Given that `α ⊕ β` is a fintype, `β` is also a fintype. This is non-computable as it uses that `sum.inr` is an injection, but there's no clear inverse if `β` is empty. -/ noncomputable def fintype.sum_right {α β} [fintype (α ⊕ β)] : fintype β := fintype.of_injective (sum.inr : β → α ⊕ β) sum.inr_injective section finset /-! ### `fintype (s : finset α)` -/ instance finset.fintype_coe_sort {α : Type u} (s : finset α) : fintype s := ⟨s.attach, s.mem_attach⟩ @[simp] lemma finset.univ_eq_attach {α : Type u} (s : finset α) : (univ : finset s) = s.attach := rfl end finset namespace fintype variables [fintype α] [fintype β] lemma card_le_of_injective (f : α → β) (hf : function.injective f) : card α ≤ card β := finset.card_le_card_of_inj_on f (λ _ _, finset.mem_univ _) (λ _ _ _ _ h, hf h) lemma card_le_of_embedding (f : α ↪ β) : card α ≤ card β := card_le_of_injective f f.2 lemma card_lt_of_injective_of_not_mem (f : α → β) (h : function.injective f) {b : β} (w : b ∉ set.range f) : card α < card β := calc card α = (univ.map ⟨f, h⟩).card : (card_map _).symm ... < card β : finset.card_lt_univ_of_not_mem $ by rwa [← mem_coe, coe_map, coe_univ, set.image_univ] lemma card_lt_of_injective_not_surjective (f : α → β) (h : function.injective f) (h' : ¬function.surjective f) : card α < card β := let ⟨y, hy⟩ := not_forall.1 h' in card_lt_of_injective_of_not_mem f h hy lemma card_le_of_surjective (f : α → β) (h : function.surjective f) : card β ≤ card α := card_le_of_injective _ (function.injective_surj_inv h) /-- The pigeonhole principle for finitely many pigeons and pigeonholes. This is the `fintype` version of `finset.exists_ne_map_eq_of_card_lt_of_maps_to`. -/ lemma exists_ne_map_eq_of_card_lt (f : α → β) (h : fintype.card β < fintype.card α) : ∃ x y, x ≠ y ∧ f x = f y := let ⟨x, _, y, _, h⟩ := finset.exists_ne_map_eq_of_card_lt_of_maps_to h (λ x _, mem_univ (f x)) in ⟨x, y, h⟩ lemma card_eq_one_iff : card α = 1 ↔ (∃ x : α, ∀ y, y = x) := by rw [← card_unit, card_eq]; exact ⟨λ ⟨a⟩, ⟨a.symm (), λ y, a.injective (subsingleton.elim _ _)⟩, λ ⟨x, hx⟩, ⟨⟨λ _, (), λ _, x, λ _, (hx _).trans (hx _).symm, λ _, subsingleton.elim _ _⟩⟩⟩ lemma card_eq_zero_iff : card α = 0 ↔ is_empty α := ⟨λ h, ⟨λ a, have e : α ≃ empty := classical.choice (card_eq.1 (by simp [h])), (e a).elim⟩, λ h, by { have e : α ≃ empty, exactI equiv.equiv_empty α, simp [card_congr e] }⟩ /-- A `fintype` with cardinality zero is equivalent to `empty`. -/ def card_eq_zero_equiv_equiv_empty : card α = 0 ≃ (α ≃ empty) := (equiv.of_iff card_eq_zero_iff).trans (equiv.equiv_empty_equiv α).symm lemma card_pos_iff : 0 < card α ↔ nonempty α := pos_iff_ne_zero.trans $ not_iff_comm.mp $ not_nonempty_iff.trans card_eq_zero_iff.symm lemma card_le_one_iff : card α ≤ 1 ↔ (∀ a b : α, a = b) := let n := card α in have hn : n = card α := rfl, match n, hn with | 0 := λ ha, ⟨λ h, λ a, (card_eq_zero_iff.1 ha.symm).elim a, λ _, ha ▸ nat.le_succ _⟩ | 1 := λ ha, ⟨λ h, λ a b, let ⟨x, hx⟩ := card_eq_one_iff.1 ha.symm in by rw [hx a, hx b], λ _, ha ▸ le_refl _⟩ | (n+2) := λ ha, ⟨λ h, by rw ← ha at h; exact absurd h dec_trivial, (λ h, card_unit ▸ card_le_of_injective (λ _, ()) (λ _ _ _, h _ _))⟩ end lemma card_le_one_iff_subsingleton : card α ≤ 1 ↔ subsingleton α := card_le_one_iff.trans subsingleton_iff.symm lemma one_lt_card_iff_nontrivial : 1 < card α ↔ nontrivial α := begin classical, rw ← not_iff_not, push_neg, rw [not_nontrivial_iff_subsingleton, card_le_one_iff_subsingleton] end lemma exists_ne_of_one_lt_card (h : 1 < card α) (a : α) : ∃ b : α, b ≠ a := by { haveI : nontrivial α := one_lt_card_iff_nontrivial.1 h, exact exists_ne a } lemma exists_pair_of_one_lt_card (h : 1 < card α) : ∃ (a b : α), a ≠ b := by { haveI : nontrivial α := one_lt_card_iff_nontrivial.1 h, exact exists_pair_ne α } lemma card_eq_one_of_forall_eq {i : α} (h : ∀ j, j = i) : card α = 1 := fintype.card_eq_one_iff.2 ⟨i,h⟩ lemma injective_iff_surjective {f : α → α} : injective f ↔ surjective f := by haveI := classical.prop_decidable; exact have ∀ {f : α → α}, injective f → surjective f, from λ f hinj x, have h₁ : image f univ = univ := eq_of_subset_of_card_le (subset_univ _) ((card_image_of_injective univ hinj).symm ▸ le_refl _), have h₂ : x ∈ image f univ := h₁.symm ▸ mem_univ _, exists_of_bex (mem_image.1 h₂), ⟨this, λ hsurj, has_left_inverse.injective ⟨surj_inv hsurj, left_inverse_of_surjective_of_right_inverse (this (injective_surj_inv _)) (right_inverse_surj_inv _)⟩⟩ lemma injective_iff_bijective {f : α → α} : injective f ↔ bijective f := by simp [bijective, injective_iff_surjective] lemma surjective_iff_bijective {f : α → α} : surjective f ↔ bijective f := by simp [bijective, injective_iff_surjective] lemma injective_iff_surjective_of_equiv {β : Type*} {f : α → β} (e : α ≃ β) : injective f ↔ surjective f := have injective (e.symm ∘ f) ↔ surjective (e.symm ∘ f), from injective_iff_surjective, ⟨λ hinj, by simpa [function.comp] using e.surjective.comp (this.1 (e.symm.injective.comp hinj)), λ hsurj, by simpa [function.comp] using e.injective.comp (this.2 (e.symm.surjective.comp hsurj))⟩ lemma bijective_iff_injective_and_card (f : α → β) : bijective f ↔ injective f ∧ card α = card β := begin split, { intro h, exact ⟨h.1, card_congr (equiv.of_bijective f h)⟩ }, { rintro ⟨hf, h⟩, refine ⟨hf, _⟩, rwa ← injective_iff_surjective_of_equiv (equiv_of_card_eq h) } end lemma bijective_iff_surjective_and_card (f : α → β) : bijective f ↔ surjective f ∧ card α = card β := begin split, { intro h, exact ⟨h.2, card_congr (equiv.of_bijective f h)⟩, }, { rintro ⟨hf, h⟩, refine ⟨_, hf⟩, rwa injective_iff_surjective_of_equiv (equiv_of_card_eq h) } end end fintype lemma fintype.coe_image_univ [fintype α] [decidable_eq β] {f : α → β} : ↑(finset.image f finset.univ) = set.range f := by { ext x, simp } instance list.subtype.fintype [decidable_eq α] (l : list α) : fintype {x // x ∈ l} := fintype.of_list l.attach l.mem_attach instance multiset.subtype.fintype [decidable_eq α] (s : multiset α) : fintype {x // x ∈ s} := fintype.of_multiset s.attach s.mem_attach instance finset.subtype.fintype (s : finset α) : fintype {x // x ∈ s} := ⟨s.attach, s.mem_attach⟩ instance finset_coe.fintype (s : finset α) : fintype (↑s : set α) := finset.subtype.fintype s @[simp] lemma fintype.card_coe (s : finset α) : fintype.card s = s.card := card_attach lemma finset.attach_eq_univ {s : finset α} : s.attach = finset.univ := rfl instance plift.fintype (p : Prop) [decidable p] : fintype (plift p) := ⟨if h : p then {⟨h⟩} else ∅, λ ⟨h⟩, by simp [h]⟩ instance Prop.fintype : fintype Prop := ⟨⟨true ::ₘ false ::ₘ 0, by simp [true_ne_false]⟩, classical.cases (by simp) (by simp)⟩ instance subtype.fintype (p : α → Prop) [decidable_pred p] [fintype α] : fintype {x // p x} := fintype.subtype (univ.filter p) (by simp) /-- A set on a fintype, when coerced to a type, is a fintype. -/ def set_fintype {α} [fintype α] (s : set α) [decidable_pred (∈ s)] : fintype s := subtype.fintype (λ x, x ∈ s) lemma set_fintype_card_le_univ {α : Type*} [fintype α] (s : set α) [fintype ↥s] : fintype.card ↥s ≤ fintype.card α := fintype.card_le_of_embedding (function.embedding.subtype s) namespace function.embedding /-- An embedding from a `fintype` to itself can be promoted to an equivalence. -/ noncomputable def equiv_of_fintype_self_embedding {α : Type*} [fintype α] (e : α ↪ α) : α ≃ α := equiv.of_bijective e (fintype.injective_iff_bijective.1 e.2) @[simp] lemma equiv_of_fintype_self_embedding_to_embedding {α : Type*} [fintype α] (e : α ↪ α) : e.equiv_of_fintype_self_embedding.to_embedding = e := by { ext, refl, } /-- If `‖β‖ < ‖α‖` there are no embeddings `α ↪ β`. This is a formulation of the pigeonhole principle. Note this cannot be an instance as it needs `h`. -/ @[simp] lemma is_empty_of_card_lt {α β} [fintype α] [fintype β] (h : fintype.card β < fintype.card α) : is_empty (α ↪ β) := ⟨λ f, let ⟨x, y, ne, feq⟩ := fintype.exists_ne_map_eq_of_card_lt f h in ne $ f.injective feq⟩ end function.embedding @[simp] lemma finset.univ_map_embedding {α : Type*} [fintype α] (e : α ↪ α) : univ.map e = univ := by rw [← e.equiv_of_fintype_self_embedding_to_embedding, univ_map_equiv_to_embedding] namespace fintype lemma card_lt_of_surjective_not_injective [fintype α] [fintype β] (f : α → β) (h : function.surjective f) (h' : ¬function.injective f) : card β < card α := card_lt_of_injective_not_surjective _ (function.injective_surj_inv h) $ λ hg, have w : function.bijective (function.surj_inv h) := ⟨function.injective_surj_inv h, hg⟩, h' $ (injective_iff_surjective_of_equiv (equiv.of_bijective _ w).symm).mpr h variables [decidable_eq α] [fintype α] {δ : α → Type*} /-- Given for all `a : α` a finset `t a` of `δ a`, then one can define the finset `fintype.pi_finset t` of all functions taking values in `t a` for all `a`. This is the analogue of `finset.pi` where the base finset is `univ` (but formally they are not the same, as there is an additional condition `i ∈ finset.univ` in the `finset.pi` definition). -/ def pi_finset (t : Πa, finset (δ a)) : finset (Πa, δ a) := (finset.univ.pi t).map ⟨λ f a, f a (mem_univ a), λ _ _, by simp [function.funext_iff]⟩ @[simp] lemma mem_pi_finset {t : Πa, finset (δ a)} {f : Πa, δ a} : f ∈ pi_finset t ↔ (∀a, f a ∈ t a) := begin split, { simp only [pi_finset, mem_map, and_imp, forall_prop_of_true, exists_prop, mem_univ, exists_imp_distrib, mem_pi], assume g hg hgf a, rw ← hgf, exact hg a }, { simp only [pi_finset, mem_map, forall_prop_of_true, exists_prop, mem_univ, mem_pi], assume hf, exact ⟨λ a ha, f a, hf, rfl⟩ } end lemma pi_finset_subset (t₁ t₂ : Πa, finset (δ a)) (h : ∀ a, t₁ a ⊆ t₂ a) : pi_finset t₁ ⊆ pi_finset t₂ := λ g hg, mem_pi_finset.2 $ λ a, h a $ mem_pi_finset.1 hg a lemma pi_finset_disjoint_of_disjoint [∀ a, decidable_eq (δ a)] (t₁ t₂ : Πa, finset (δ a)) {a : α} (h : disjoint (t₁ a) (t₂ a)) : disjoint (pi_finset t₁) (pi_finset t₂) := disjoint_iff_ne.2 $ λ f₁ hf₁ f₂ hf₂ eq₁₂, disjoint_iff_ne.1 h (f₁ a) (mem_pi_finset.1 hf₁ a) (f₂ a) (mem_pi_finset.1 hf₂ a) (congr_fun eq₁₂ a) end fintype /-! ### pi -/ /-- A dependent product of fintypes, indexed by a fintype, is a fintype. -/ instance pi.fintype {α : Type*} {β : α → Type*} [decidable_eq α] [fintype α] [∀a, fintype (β a)] : fintype (Πa, β a) := ⟨fintype.pi_finset (λ _, univ), by simp⟩ @[simp] lemma fintype.pi_finset_univ {α : Type*} {β : α → Type*} [decidable_eq α] [fintype α] [∀a, fintype (β a)] : fintype.pi_finset (λ a : α, (finset.univ : finset (β a))) = (finset.univ : finset (Π a, β a)) := rfl instance d_array.fintype {n : ℕ} {α : fin n → Type*} [∀n, fintype (α n)] : fintype (d_array n α) := fintype.of_equiv _ (equiv.d_array_equiv_fin _).symm instance array.fintype {n : ℕ} {α : Type*} [fintype α] : fintype (array n α) := d_array.fintype instance vector.fintype {α : Type*} [fintype α] {n : ℕ} : fintype (vector α n) := fintype.of_equiv _ (equiv.vector_equiv_fin _ _).symm instance quotient.fintype [fintype α] (s : setoid α) [decidable_rel ((≈) : α → α → Prop)] : fintype (quotient s) := fintype.of_surjective quotient.mk (λ x, quotient.induction_on x (λ x, ⟨x, rfl⟩)) instance finset.fintype [fintype α] : fintype (finset α) := ⟨univ.powerset, λ x, finset.mem_powerset.2 (finset.subset_univ _)⟩ -- irreducible due to this conversation on Zulip: -- https://leanprover.zulipchat.com/#narrow/stream/113488-general/ -- topic/.60simp.60.20ignoring.20lemmas.3F/near/241824115 @[irreducible] instance function.embedding.fintype {α β} [fintype α] [fintype β] [decidable_eq α] [decidable_eq β] : fintype (α ↪ β) := fintype.of_equiv _ (equiv.subtype_injective_equiv_embedding α β) @[simp] lemma fintype.card_finset [fintype α] : fintype.card (finset α) = 2 ^ (fintype.card α) := finset.card_powerset finset.univ @[simp] lemma finset.univ_filter_card_eq (α : Type*) [fintype α] (k : ℕ) : (finset.univ : finset (finset α)).filter (λ s, s.card = k) = finset.univ.powerset_len k := by { ext, simp [finset.mem_powerset_len] } @[simp] lemma fintype.card_finset_len [fintype α] (k : ℕ) : fintype.card {s : finset α // s.card = k} = nat.choose (fintype.card α) k := by simp [fintype.subtype_card, finset.card_univ] @[simp] lemma set.to_finset_univ [fintype α] : (set.univ : set α).to_finset = finset.univ := by { ext, simp only [set.mem_univ, mem_univ, set.mem_to_finset] } @[simp] lemma set.to_finset_eq_empty_iff {s : set α} [fintype s] : s.to_finset = ∅ ↔ s = ∅ := by simp [ext_iff, set.ext_iff] @[simp] lemma set.to_finset_empty : (∅ : set α).to_finset = ∅ := set.to_finset_eq_empty_iff.mpr rfl @[simp] lemma set.to_finset_range [decidable_eq α] [fintype β] (f : β → α) [fintype (set.range f)] : (set.range f).to_finset = finset.univ.image f := by simp [ext_iff] theorem fintype.card_subtype_le [fintype α] (p : α → Prop) [decidable_pred p] : fintype.card {x // p x} ≤ fintype.card α := fintype.card_le_of_embedding (function.embedding.subtype _) theorem fintype.card_subtype_lt [fintype α] {p : α → Prop} [decidable_pred p] {x : α} (hx : ¬ p x) : fintype.card {x // p x} < fintype.card α := fintype.card_lt_of_injective_of_not_mem coe subtype.coe_injective $ by rwa subtype.range_coe_subtype theorem fintype.card_quotient_le [fintype α] (s : setoid α) [decidable_rel ((≈) : α → α → Prop)] : fintype.card (quotient s) ≤ fintype.card α := fintype.card_le_of_surjective _ (surjective_quotient_mk _) theorem fintype.card_quotient_lt [fintype α] {s : setoid α} [decidable_rel ((≈) : α → α → Prop)] {x y : α} (h1 : x ≠ y) (h2 : x ≈ y) : fintype.card (quotient s) < fintype.card α := fintype.card_lt_of_surjective_not_injective _ (surjective_quotient_mk _) $ λ w, h1 (w $ quotient.eq.mpr h2) instance psigma.fintype {α : Type*} {β : α → Type*} [fintype α] [∀ a, fintype (β a)] : fintype (Σ' a, β a) := fintype.of_equiv _ (equiv.psigma_equiv_sigma _).symm instance psigma.fintype_prop_left {α : Prop} {β : α → Type*} [decidable α] [∀ a, fintype (β a)] : fintype (Σ' a, β a) := if h : α then fintype.of_equiv (β h) ⟨λ x, ⟨h, x⟩, psigma.snd, λ _, rfl, λ ⟨_, _⟩, rfl⟩ else ⟨∅, λ x, h x.1⟩ instance psigma.fintype_prop_right {α : Type*} {β : α → Prop} [∀ a, decidable (β a)] [fintype α] : fintype (Σ' a, β a) := fintype.of_equiv {a // β a} ⟨λ ⟨x, y⟩, ⟨x, y⟩, λ ⟨x, y⟩, ⟨x, y⟩, λ ⟨x, y⟩, rfl, λ ⟨x, y⟩, rfl⟩ instance psigma.fintype_prop_prop {α : Prop} {β : α → Prop} [decidable α] [∀ a, decidable (β a)] : fintype (Σ' a, β a) := if h : ∃ a, β a then ⟨{⟨h.fst, h.snd⟩}, λ ⟨_, _⟩, by simp⟩ else ⟨∅, λ ⟨x, y⟩, h ⟨x, y⟩⟩ instance set.fintype [fintype α] : fintype (set α) := ⟨(@finset.univ α _).powerset.map ⟨coe, coe_injective⟩, λ s, begin classical, refine mem_map.2 ⟨finset.univ.filter s, mem_powerset.2 (subset_univ _), _⟩, apply (coe_filter _ _).trans, rw [coe_univ, set.sep_univ], refl end⟩ instance pfun_fintype (p : Prop) [decidable p] (α : p → Type*) [Π hp, fintype (α hp)] : fintype (Π hp : p, α hp) := if hp : p then fintype.of_equiv (α hp) ⟨λ a _, a, λ f, f hp, λ _, rfl, λ _, rfl⟩ else ⟨singleton (λ h, (hp h).elim), by simp [hp, function.funext_iff]⟩ @[simp] lemma finset.univ_pi_univ {α : Type*} {β : α → Type*} [decidable_eq α] [fintype α] [∀a, fintype (β a)] : finset.univ.pi (λ a : α, (finset.univ : finset (β a))) = finset.univ := by { ext, simp } lemma mem_image_univ_iff_mem_range {α β : Type*} [fintype α] [decidable_eq β] {f : α → β} {b : β} : b ∈ univ.image f ↔ b ∈ set.range f := by simp /-- An auxiliary function for `quotient.fin_choice`. Given a collection of setoids indexed by a type `ι`, a (finite) list `l` of indices, and a function that for each `i ∈ l` gives a term of the corresponding quotient type, then there is a corresponding term in the quotient of the product of the setoids indexed by `l`. -/ def quotient.fin_choice_aux {ι : Type*} [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] : Π (l : list ι), (Π i ∈ l, quotient (S i)) → @quotient (Π i ∈ l, α i) (by apply_instance) | [] f := ⟦λ i, false.elim⟧ | (i::l) f := begin refine quotient.lift_on₂ (f i (list.mem_cons_self _ _)) (quotient.fin_choice_aux l (λ j h, f j (list.mem_cons_of_mem _ h))) _ _, exact λ a l, ⟦λ j h, if e : j = i then by rw e; exact a else l _ (h.resolve_left e)⟧, refine λ a₁ l₁ a₂ l₂ h₁ h₂, quotient.sound (λ j h, _), by_cases e : j = i; simp [e], { subst j, exact h₁ }, { exact h₂ _ _ } end theorem quotient.fin_choice_aux_eq {ι : Type*} [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] : ∀ (l : list ι) (f : Π i ∈ l, α i), quotient.fin_choice_aux l (λ i h, ⟦f i h⟧) = ⟦f⟧ | [] f := quotient.sound (λ i h, h.elim) | (i::l) f := begin simp [quotient.fin_choice_aux, quotient.fin_choice_aux_eq l], refine quotient.sound (λ j h, _), by_cases e : j = i; simp [e], subst j, refl end /-- Given a collection of setoids indexed by a fintype `ι` and a function that for each `i : ι` gives a term of the corresponding quotient type, then there is corresponding term in the quotient of the product of the setoids. -/ def quotient.fin_choice {ι : Type*} [decidable_eq ι] [fintype ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] (f : Π i, quotient (S i)) : @quotient (Π i, α i) (by apply_instance) := quotient.lift_on (@quotient.rec_on _ _ (λ l : multiset ι, @quotient (Π i ∈ l, α i) (by apply_instance)) finset.univ.1 (λ l, quotient.fin_choice_aux l (λ i _, f i)) (λ a b h, begin have := λ a, quotient.fin_choice_aux_eq a (λ i h, quotient.out (f i)), simp [quotient.out_eq] at this, simp [this], let g := λ a:multiset ι, ⟦λ (i : ι) (h : i ∈ a), quotient.out (f i)⟧, refine eq_of_heq ((eq_rec_heq _ _).trans (_ : g a == g b)), congr' 1, exact quotient.sound h, end)) (λ f, ⟦λ i, f i (finset.mem_univ _)⟧) (λ a b h, quotient.sound $ λ i, h _ _) theorem quotient.fin_choice_eq {ι : Type*} [decidable_eq ι] [fintype ι] {α : ι → Type*} [∀ i, setoid (α i)] (f : Π i, α i) : quotient.fin_choice (λ i, ⟦f i⟧) = ⟦f⟧ := begin let q, swap, change quotient.lift_on q _ _ = _, have : q = ⟦λ i h, f i⟧, { dsimp [q], exact quotient.induction_on (@finset.univ ι _).1 (λ l, quotient.fin_choice_aux_eq _ _) }, simp [this], exact setoid.refl _ end section equiv open list equiv equiv.perm variables [decidable_eq α] [decidable_eq β] /-- Given a list, produce a list of all permutations of its elements. -/ def perms_of_list : list α → list (perm α) | [] := [1] | (a :: l) := perms_of_list l ++ l.bind (λ b, (perms_of_list l).map (λ f, swap a b * f)) lemma length_perms_of_list : ∀ l : list α, length (perms_of_list l) = l.length! | [] := rfl | (a :: l) := begin rw [length_cons, nat.factorial_succ], simp [perms_of_list, length_bind, length_perms_of_list, function.comp, nat.succ_mul], cc end lemma mem_perms_of_list_of_mem {l : list α} {f : perm α} (h : ∀ x, f x ≠ x → x ∈ l) : f ∈ perms_of_list l := begin induction l with a l IH generalizing f h, { exact list.mem_singleton.2 (equiv.ext $ λ x, decidable.by_contradiction $ h _) }, by_cases hfa : f a = a, { refine mem_append_left _ (IH (λ x hx, mem_of_ne_of_mem _ (h x hx))), rintro rfl, exact hx hfa }, { have hfa' : f (f a) ≠ f a := mt (λ h, f.injective h) hfa, have : ∀ (x : α), (swap a (f a) * f) x ≠ x → x ∈ l, { intros x hx, have hxa : x ≠ a, { rintro rfl, apply hx, simp only [mul_apply, swap_apply_right] }, refine list.mem_of_ne_of_mem hxa (h x (λ h, _)), simp only [h, mul_apply, swap_apply_def, mul_apply, ne.def, apply_eq_iff_eq] at hx; split_ifs at hx, exacts [hxa (h.symm.trans h_1), hx h] }, suffices : f ∈ perms_of_list l ∨ ∃ (b ∈ l) (g ∈ perms_of_list l), swap a b * g = f, { simpa only [perms_of_list, exists_prop, list.mem_map, mem_append, list.mem_bind] }, refine or_iff_not_imp_left.2 (λ hfl, ⟨f a, _, swap a (f a) * f, IH this, _⟩), { by_cases hffa : f (f a) = a, { exact mem_of_ne_of_mem hfa (h _ (mt (λ h, f.injective h) hfa)) }, { apply this, simp only [mul_apply, swap_apply_def, mul_apply, ne.def, apply_eq_iff_eq], split_ifs; cc } }, { rw [← mul_assoc, mul_def (swap a (f a)) (swap a (f a)), swap_swap, ← perm.one_def, one_mul] } } end lemma mem_of_mem_perms_of_list : ∀ {l : list α} {f : perm α}, f ∈ perms_of_list l → ∀ {x}, f x ≠ x → x ∈ l | [] f h := have f = 1 := by simpa [perms_of_list] using h, by rw this; simp | (a::l) f h := (mem_append.1 h).elim (λ h x hx, mem_cons_of_mem _ (mem_of_mem_perms_of_list h hx)) (λ h x hx, let ⟨y, hy, hy'⟩ := list.mem_bind.1 h in let ⟨g, hg₁, hg₂⟩ := list.mem_map.1 hy' in if hxa : x = a then by simp [hxa] else if hxy : x = y then mem_cons_of_mem _ $ by rwa hxy else mem_cons_of_mem _ $ mem_of_mem_perms_of_list hg₁ $ by rw [eq_inv_mul_iff_mul_eq.2 hg₂, mul_apply, swap_inv, swap_apply_def]; split_ifs; cc) lemma mem_perms_of_list_iff {l : list α} {f : perm α} : f ∈ perms_of_list l ↔ ∀ {x}, f x ≠ x → x ∈ l := ⟨mem_of_mem_perms_of_list, mem_perms_of_list_of_mem⟩ lemma nodup_perms_of_list : ∀ {l : list α} (hl : l.nodup), (perms_of_list l).nodup | [] hl := by simp [perms_of_list] | (a::l) hl := have hl' : l.nodup, from nodup_of_nodup_cons hl, have hln' : (perms_of_list l).nodup, from nodup_perms_of_list hl', have hmeml : ∀ {f : perm α}, f ∈ perms_of_list l → f a = a, from λ f hf, not_not.1 (mt (mem_of_mem_perms_of_list hf) (nodup_cons.1 hl).1), by rw [perms_of_list, list.nodup_append, list.nodup_bind, pairwise_iff_nth_le]; exact ⟨hln', ⟨λ _ _, nodup_map (λ _ _, mul_left_cancel) hln', λ i j hj hij x hx₁ hx₂, let ⟨f, hf⟩ := list.mem_map.1 hx₁ in let ⟨g, hg⟩ := list.mem_map.1 hx₂ in have hix : x a = nth_le l i (lt_trans hij hj), by rw [← hf.2, mul_apply, hmeml hf.1, swap_apply_left], have hiy : x a = nth_le l j hj, by rw [← hg.2, mul_apply, hmeml hg.1, swap_apply_left], absurd (hf.2.trans (hg.2.symm)) $ λ h, ne_of_lt hij $ nodup_iff_nth_le_inj.1 hl' i j (lt_trans hij hj) hj $ by rw [← hix, hiy]⟩, λ f hf₁ hf₂, let ⟨x, hx, hx'⟩ := list.mem_bind.1 hf₂ in let ⟨g, hg⟩ := list.mem_map.1 hx' in have hgxa : g⁻¹ x = a, from f.injective $ by rw [hmeml hf₁, ← hg.2]; simp, have hxa : x ≠ a, from λ h, (list.nodup_cons.1 hl).1 (h ▸ hx), (list.nodup_cons.1 hl).1 $ hgxa ▸ mem_of_mem_perms_of_list hg.1 (by rwa [apply_inv_self, hgxa])⟩ /-- Given a finset, produce the finset of all permutations of its elements. -/ def perms_of_finset (s : finset α) : finset (perm α) := quotient.hrec_on s.1 (λ l hl, ⟨perms_of_list l, nodup_perms_of_list hl⟩) (λ a b hab, hfunext (congr_arg _ (quotient.sound hab)) (λ ha hb _, heq_of_eq $ finset.ext $ by simp [mem_perms_of_list_iff, hab.mem_iff])) s.2 lemma mem_perms_of_finset_iff : ∀ {s : finset α} {f : perm α}, f ∈ perms_of_finset s ↔ ∀ {x}, f x ≠ x → x ∈ s := by rintros ⟨⟨l⟩, hs⟩ f; exact mem_perms_of_list_iff lemma card_perms_of_finset : ∀ (s : finset α), (perms_of_finset s).card = s.card! := by rintros ⟨⟨l⟩, hs⟩; exact length_perms_of_list l /-- The collection of permutations of a fintype is a fintype. -/ def fintype_perm [fintype α] : fintype (perm α) := ⟨perms_of_finset (@finset.univ α _), by simp [mem_perms_of_finset_iff]⟩ instance [fintype α] [fintype β] : fintype (α ≃ β) := if h : fintype.card β = fintype.card α then trunc.rec_on_subsingleton (fintype.trunc_equiv_fin α) (λ eα, trunc.rec_on_subsingleton (fintype.trunc_equiv_fin β) (λ eβ, @fintype.of_equiv _ (perm α) fintype_perm (equiv_congr (equiv.refl α) (eα.trans (eq.rec_on h eβ.symm)) : (α ≃ α) ≃ (α ≃ β)))) else ⟨∅, λ x, false.elim (h (fintype.card_eq.2 ⟨x.symm⟩))⟩ lemma fintype.card_perm [fintype α] : fintype.card (perm α) = (fintype.card α)! := subsingleton.elim (@fintype_perm α _ _) (@equiv.fintype α α _ _ _ _) ▸ card_perms_of_finset _ lemma fintype.card_equiv [fintype α] [fintype β] (e : α ≃ β) : fintype.card (α ≃ β) = (fintype.card α)! := fintype.card_congr (equiv_congr (equiv.refl α) e) ▸ fintype.card_perm lemma univ_eq_singleton_of_card_one {α} [fintype α] (x : α) (h : fintype.card α = 1) : (univ : finset α) = {x} := begin symmetry, apply eq_of_subset_of_card_le (subset_univ ({x})), apply le_of_eq, simp [h, finset.card_univ] end end equiv namespace fintype section choose open fintype open equiv variables [fintype α] (p : α → Prop) [decidable_pred p] /-- Given a fintype `α` and a predicate `p`, associate to a proof that there is a unique element of `α` satisfying `p` this unique element, as an element of the corresponding subtype. -/ def choose_x (hp : ∃! a : α, p a) : {a // p a} := ⟨finset.choose p univ (by simp; exact hp), finset.choose_property _ _ _⟩ /-- Given a fintype `α` and a predicate `p`, associate to a proof that there is a unique element of `α` satisfying `p` this unique element, as an element of `α`. -/ def choose (hp : ∃! a, p a) : α := choose_x p hp lemma choose_spec (hp : ∃! a, p a) : p (choose p hp) := (choose_x p hp).property end choose section bijection_inverse open function variables [fintype α] variables [decidable_eq β] variables {f : α → β} /-- `bij_inv f` is the unique inverse to a bijection `f`. This acts as a computable alternative to `function.inv_fun`. -/ def bij_inv (f_bij : bijective f) (b : β) : α := fintype.choose (λ a, f a = b) begin rcases f_bij.right b with ⟨a', fa_eq_b⟩, rw ← fa_eq_b, exact ⟨a', ⟨rfl, (λ a h, f_bij.left h)⟩⟩ end lemma left_inverse_bij_inv (f_bij : bijective f) : left_inverse (bij_inv f_bij) f := λ a, f_bij.left (choose_spec (λ a', f a' = f a) _) lemma right_inverse_bij_inv (f_bij : bijective f) : right_inverse (bij_inv f_bij) f := λ b, choose_spec (λ a', f a' = b) _ lemma bijective_bij_inv (f_bij : bijective f) : bijective (bij_inv f_bij) := ⟨(right_inverse_bij_inv _).injective, (left_inverse_bij_inv _).surjective⟩ end bijection_inverse lemma well_founded_of_trans_of_irrefl [fintype α] (r : α → α → Prop) [is_trans α r] [is_irrefl α r] : well_founded r := by classical; exact have ∀ x y, r x y → (univ.filter (λ z, r z x)).card < (univ.filter (λ z, r z y)).card, from λ x y hxy, finset.card_lt_card $ by simp only [finset.lt_iff_ssubset.symm, lt_iff_le_not_le, finset.le_iff_subset, finset.subset_iff, mem_filter, true_and, mem_univ, hxy]; exact ⟨λ z hzx, trans hzx hxy, not_forall_of_exists_not ⟨x, not_imp.2 ⟨hxy, irrefl x⟩⟩⟩, subrelation.wf this (measure_wf _) lemma preorder.well_founded [fintype α] [preorder α] : well_founded ((<) : α → α → Prop) := well_founded_of_trans_of_irrefl _ @[instance, priority 10] lemma linear_order.is_well_order [fintype α] [linear_order α] : is_well_order α (<) := { wf := preorder.well_founded } end fintype /-- A type is said to be infinite if it has no fintype instance. Note that `infinite α` is equivalent to `is_empty (fintype α)`. -/ class infinite (α : Type*) : Prop := (not_fintype : fintype α → false) lemma not_fintype (α : Type*) [h1 : infinite α] [h2 : fintype α] : false := infinite.not_fintype h2 protected lemma fintype.false {α : Type*} [infinite α] (h : fintype α) : false := not_fintype α protected lemma infinite.false {α : Type*} [fintype α] (h : infinite α) : false := not_fintype α @[simp] lemma is_empty_fintype {α : Type*} : is_empty (fintype α) ↔ infinite α := ⟨λ ⟨x⟩, ⟨x⟩, λ ⟨x⟩, ⟨x⟩⟩ @[simp] lemma not_nonempty_fintype {α : Type*} : ¬ nonempty (fintype α) ↔ infinite α := not_nonempty_iff.trans is_empty_fintype /-- A non-infinite type is a fintype. -/ noncomputable def fintype_of_not_infinite {α : Type*} (h : ¬ infinite α) : fintype α := ((not_iff_comm.mp not_nonempty_fintype).mp h).some lemma finset.exists_minimal {α : Type*} [preorder α] (s : finset α) (h : s.nonempty) : ∃ m ∈ s, ∀ x ∈ s, ¬ (x < m) := begin obtain ⟨c, hcs : c ∈ s⟩ := h, have : well_founded (@has_lt.lt {x // x ∈ s} _) := fintype.well_founded_of_trans_of_irrefl _, obtain ⟨⟨m, hms : m ∈ s⟩, -, H⟩ := this.has_min set.univ ⟨⟨c, hcs⟩, trivial⟩, exact ⟨m, hms, λ x hx hxm, H ⟨x, hx⟩ trivial hxm⟩, end lemma finset.exists_maximal {α : Type*} [preorder α] (s : finset α) (h : s.nonempty) : ∃ m ∈ s, ∀ x ∈ s, ¬ (m < x) := @finset.exists_minimal (order_dual α) _ s h namespace infinite lemma exists_not_mem_finset [infinite α] (s : finset α) : ∃ x, x ∉ s := not_forall.1 $ λ h, fintype.false ⟨s, h⟩ @[priority 100] -- see Note [lower instance priority] instance (α : Type*) [H : infinite α] : nontrivial α := ⟨let ⟨x, hx⟩ := exists_not_mem_finset (∅ : finset α) in let ⟨y, hy⟩ := exists_not_mem_finset ({x} : finset α) in ⟨y, x, by simpa only [mem_singleton] using hy⟩⟩ lemma nonempty (α : Type*) [infinite α] : nonempty α := by apply_instance lemma of_injective [infinite β] (f : β → α) (hf : injective f) : infinite α := ⟨λ I, by exactI (fintype.of_injective f hf).false⟩ lemma of_surjective [infinite β] (f : α → β) (hf : surjective f) : infinite α := ⟨λ I, by { classical, exactI (fintype.of_surjective f hf).false }⟩ private noncomputable def nat_embedding_aux (α : Type*) [infinite α] : ℕ → α | n := by letI := classical.dec_eq α; exact classical.some (exists_not_mem_finset ((multiset.range n).pmap (λ m (hm : m < n), nat_embedding_aux m) (λ _, multiset.mem_range.1)).to_finset) private lemma nat_embedding_aux_injective (α : Type*) [infinite α] : function.injective (nat_embedding_aux α) := begin assume m n h, letI := classical.dec_eq α, wlog hmlen : m ≤ n using m n, by_contradiction hmn, have hmn : m < n, from lt_of_le_of_ne hmlen hmn, refine (classical.some_spec (exists_not_mem_finset ((multiset.range n).pmap (λ m (hm : m < n), nat_embedding_aux α m) (λ _, multiset.mem_range.1)).to_finset)) _, refine multiset.mem_to_finset.2 (multiset.mem_pmap.2 ⟨m, multiset.mem_range.2 hmn, _⟩), rw [h, nat_embedding_aux] end /-- Embedding of `ℕ` into an infinite type. -/ noncomputable def nat_embedding (α : Type*) [infinite α] : ℕ ↪ α := ⟨_, nat_embedding_aux_injective α⟩ lemma exists_subset_card_eq (α : Type*) [infinite α] (n : ℕ) : ∃ s : finset α, s.card = n := ⟨(range n).map (nat_embedding α), by rw [card_map, card_range]⟩ end infinite lemma not_injective_infinite_fintype [infinite α] [fintype β] (f : α → β) : ¬ injective f := assume (hf : injective f), have H : fintype α := fintype.of_injective f hf, H.false /-- The pigeonhole principle for infinitely many pigeons in finitely many pigeonholes. If there are infinitely many pigeons in finitely many pigeonholes, then there are at least two pigeons in the same pigeonhole. See also: `fintype.exists_ne_map_eq_of_card_lt`, `fintype.exists_infinite_fiber`. -/ lemma fintype.exists_ne_map_eq_of_infinite [infinite α] [fintype β] (f : α → β) : ∃ x y : α, x ≠ y ∧ f x = f y := begin classical, by_contra hf, push_neg at hf, apply not_injective_infinite_fintype f, intros x y, contrapose, apply hf, end -- irreducible due to this conversation on Zulip: -- https://leanprover.zulipchat.com/#narrow/stream/113488-general/ -- topic/.60simp.60.20ignoring.20lemmas.3F/near/241824115 @[irreducible] instance function.embedding.is_empty {α β} [infinite α] [fintype β] : is_empty (α ↪ β) := ⟨λ f, let ⟨x, y, ne, feq⟩ := fintype.exists_ne_map_eq_of_infinite f in ne $ f.injective feq⟩ @[priority 100] noncomputable instance function.embedding.fintype' {α β : Type*} [fintype β] : fintype (α ↪ β) := begin by_cases h : infinite α, { resetI, apply_instance }, { have := fintype_of_not_infinite h, classical, apply_instance } -- the `classical` generates `decidable_eq α/β` instances, and resets instance cache end /-- The strong pigeonhole principle for infinitely many pigeons in finitely many pigeonholes. If there are infinitely many pigeons in finitely many pigeonholes, then there is a pigeonhole with infinitely many pigeons. See also: `fintype.exists_ne_map_eq_of_infinite` -/ lemma fintype.exists_infinite_fiber [infinite α] [fintype β] (f : α → β) : ∃ y : β, infinite (f ⁻¹' {y}) := begin classical, by_contra hf, push_neg at hf, haveI := λ y, fintype_of_not_infinite $ hf y, let key : fintype α := { elems := univ.bUnion (λ (y : β), (f ⁻¹' {y}).to_finset), complete := by simp }, exact key.false, end lemma not_surjective_fintype_infinite [fintype α] [infinite β] (f : α → β) : ¬ surjective f := assume (hf : surjective f), have H : infinite α := infinite.of_surjective f hf, by exactI not_fintype α instance nat.infinite : infinite ℕ := ⟨λ ⟨s, hs⟩, finset.not_mem_range_self $ s.subset_range_sup_succ (hs _)⟩ instance int.infinite : infinite ℤ := infinite.of_injective int.of_nat (λ _ _, int.of_nat.inj) section trunc /-- For `s : multiset α`, we can lift the existential statement that `∃ x, x ∈ s` to a `trunc α`. -/ def trunc_of_multiset_exists_mem {α} (s : multiset α) : (∃ x, x ∈ s) → trunc α := quotient.rec_on_subsingleton s $ λ l h, match l, h with | [], _ := false.elim (by tauto) | (a :: _), _ := trunc.mk a end /-- A `nonempty` `fintype` constructively contains an element. -/ def trunc_of_nonempty_fintype (α) [nonempty α] [fintype α] : trunc α := trunc_of_multiset_exists_mem finset.univ.val (by simp) /-- A `fintype` with positive cardinality constructively contains an element. -/ def trunc_of_card_pos {α} [fintype α] (h : 0 < fintype.card α) : trunc α := by { letI := (fintype.card_pos_iff.mp h), exact trunc_of_nonempty_fintype α } /-- By iterating over the elements of a fintype, we can lift an existential statement `∃ a, P a` to `trunc (Σ' a, P a)`, containing data. -/ def trunc_sigma_of_exists {α} [fintype α] {P : α → Prop} [decidable_pred P] (h : ∃ a, P a) : trunc (Σ' a, P a) := @trunc_of_nonempty_fintype (Σ' a, P a) (exists.elim h $ λ a ha, ⟨⟨a, ha⟩⟩) _ end trunc namespace multiset variables [fintype α] [decidable_eq α] @[simp] lemma count_univ (a : α) : count a finset.univ.val = 1 := count_eq_one_of_mem finset.univ.nodup (finset.mem_univ _) end multiset namespace fintype /-- A recursor principle for finite types, analogous to `nat.rec`. It effectively says that every `fintype` is either `empty` or `option α`, up to an `equiv`. -/ def trunc_rec_empty_option {P : Type u → Sort v} (of_equiv : ∀ {α β}, α ≃ β → P α → P β) (h_empty : P pempty) (h_option : ∀ {α} [fintype α] [decidable_eq α], P α → P (option α)) (α : Type u) [fintype α] [decidable_eq α] : trunc (P α) := begin suffices : ∀ n : ℕ, trunc (P (ulift $ fin n)), { apply trunc.bind (this (fintype.card α)), intro h, apply trunc.map _ (fintype.trunc_equiv_fin α), intro e, exact of_equiv (equiv.ulift.trans e.symm) h }, intro n, induction n with n ih, { have : card pempty = card (ulift (fin 0)), { simp only [card_fin, card_pempty, card_ulift] }, apply trunc.bind (trunc_equiv_of_card_eq this), intro e, apply trunc.mk, refine of_equiv e h_empty, }, { have : card (option (ulift (fin n))) = card (ulift (fin n.succ)), { simp only [card_fin, card_option, card_ulift] }, apply trunc.bind (trunc_equiv_of_card_eq this), intro e, apply trunc.map _ ih, intro ih, refine of_equiv e (h_option ih), }, end /-- An induction principle for finite types, analogous to `nat.rec`. It effectively says that every `fintype` is either `empty` or `option α`, up to an `equiv`. -/ lemma induction_empty_option {P : Type u → Prop} (of_equiv : ∀ {α β}, α ≃ β → P α → P β) (h_empty : P pempty) (h_option : ∀ {α} [fintype α], P α → P (option α)) (α : Type u) [fintype α] : P α := begin haveI := classical.dec_eq α, obtain ⟨p⟩ := trunc_rec_empty_option @of_equiv h_empty (λ _ _ _, by exactI h_option) α, exact p, end end fintype
ca8359281eba9f76f146998e90c3cd8371255cfb
d1bbf1801b3dcb214451d48214589f511061da63
/src/data/nat/modeq.lean
62c4bf87e4f92a0591464e8fc77b4e80c195ba4b
[ "Apache-2.0" ]
permissive
cheraghchi/mathlib
5c366f8c4f8e66973b60c37881889da8390cab86
f29d1c3038422168fbbdb2526abf7c0ff13e86db
refs/heads/master
1,676,577,831,283
1,610,894,638,000
1,610,894,638,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,458
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.int.gcd import tactic.abel import data.list.rotate /- # Congruences modulo a natural number This file defines the equivalence relation `a ≡ b [MOD n]` on the natural numbers, and proves basic properties about it such as the Chinese Remainder Theorem `modeq_and_modeq_iff_modeq_mul`. ## Notations `a ≡ b [MOD n]` is notation for `modeq n a b`, which is defined to mean `a % n = b % n`. ## Tags modeq, congruence, mod, MOD, modulo -/ namespace nat /-- Modular equality. `modeq n a b`, or `a ≡ b [MOD n]`, means that `a - b` is a multiple of `n`. -/ @[derive decidable] def modeq (n a b : ℕ) := a % n = b % n notation a ` ≡ `:50 b ` [MOD `:50 n `]`:0 := modeq n a b namespace modeq variables {n m a b c d : ℕ} @[refl] protected theorem refl (a : ℕ) : a ≡ a [MOD n] := @rfl _ _ @[symm] protected theorem symm : a ≡ b [MOD n] → b ≡ a [MOD n] := eq.symm @[trans] protected theorem trans : a ≡ b [MOD n] → b ≡ c [MOD n] → a ≡ c [MOD n] := eq.trans protected theorem comm : a ≡ b [MOD n] ↔ b ≡ a [MOD n] := ⟨nat.modeq.symm, nat.modeq.symm⟩ theorem modeq_zero_iff : a ≡ 0 [MOD n] ↔ n ∣ a := by rw [modeq, zero_mod, dvd_iff_mod_eq_zero] theorem modeq_iff_dvd : a ≡ b [MOD n] ↔ (n:ℤ) ∣ b - a := by rw [modeq, eq_comm, ← int.coe_nat_inj', int.coe_nat_mod, int.coe_nat_mod, int.mod_eq_mod_iff_mod_sub_eq_zero, int.dvd_iff_mod_eq_zero] theorem modeq_of_dvd : (n:ℤ) ∣ b - a → a ≡ b [MOD n] := modeq_iff_dvd.2 theorem dvd_of_modeq : a ≡ b [MOD n] → (n:ℤ) ∣ b - a := modeq_iff_dvd.1 /-- A variant of `modeq_iff_dvd` with `nat` divisibility -/ theorem modeq_iff_dvd' (h : a ≤ b) : a ≡ b [MOD n] ↔ n ∣ b - a := by rw [modeq_iff_dvd, ←int.coe_nat_dvd, int.coe_nat_sub h] theorem mod_modeq (a n) : a % n ≡ a [MOD n] := nat.mod_mod _ _ theorem modeq_of_dvd_of_modeq (d : m ∣ n) (h : a ≡ b [MOD n]) : a ≡ b [MOD m] := modeq_of_dvd $ dvd_trans (int.coe_nat_dvd.2 d) (dvd_of_modeq h) theorem modeq_mul_left' (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD (c * n)] := by unfold modeq at *; rw [mul_mod_mul_left, mul_mod_mul_left, h] theorem modeq_mul_left (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD n] := modeq_of_dvd_of_modeq (dvd_mul_left _ _) $ modeq_mul_left' _ h theorem modeq_mul_right' (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD (n * c)] := by rw [mul_comm a, mul_comm b, mul_comm n]; exact modeq_mul_left' c h theorem modeq_mul_right (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD n] := by rw [mul_comm a, mul_comm b]; exact modeq_mul_left c h theorem modeq_mul (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a * c ≡ b * d [MOD n] := (modeq_mul_left _ h₂).trans (modeq_mul_right _ h₁) theorem modeq_pow (m : ℕ) (h : a ≡ b [MOD n]) : a ^ m ≡ b ^ m [MOD n] := begin induction m with d hd, {refl}, rw [pow_succ, pow_succ], exact modeq_mul h hd, end theorem modeq_add (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a + c ≡ b + d [MOD n] := modeq_of_dvd begin convert dvd_add (dvd_of_modeq h₁) (dvd_of_modeq h₂) using 1, simp [sub_eq_add_neg, add_left_comm, add_comm], end theorem modeq_add_cancel_left (h₁ : a ≡ b [MOD n]) (h₂ : a + c ≡ b + d [MOD n]) : c ≡ d [MOD n] := begin simp only [modeq_iff_dvd] at *, convert _root_.dvd_sub h₂ h₁ using 1, simp [sub_eq_add_neg], abel end theorem modeq_add_cancel_right (h₁ : c ≡ d [MOD n]) (h₂ : a + c ≡ b + d [MOD n]) : a ≡ b [MOD n] := by rw [add_comm a, add_comm b] at h₂; exact modeq_add_cancel_left h₁ h₂ theorem modeq_of_modeq_mul_left (m : ℕ) (h : a ≡ b [MOD m * n]) : a ≡ b [MOD n] := by rw [modeq_iff_dvd] at *; exact dvd.trans (dvd_mul_left (n : ℤ) (m : ℤ)) h theorem modeq_of_modeq_mul_right (m : ℕ) : a ≡ b [MOD n * m] → a ≡ b [MOD n] := mul_comm m n ▸ modeq_of_modeq_mul_left _ local attribute [semireducible] int.nonneg /-- The natural number less than `n*m` congruent to `a` mod `n` and `b` mod `m` -/ def chinese_remainder (co : coprime n m) (a b : ℕ) : {k // k ≡ a [MOD n] ∧ k ≡ b [MOD m]} := ⟨let (c, d) := xgcd n m in int.to_nat ((b * c * n + a * d * m) % (n * m)), begin rw xgcd_val, dsimp [chinese_remainder._match_1], rw [modeq_iff_dvd, modeq_iff_dvd], rw [int.to_nat_of_nonneg], swap, { by_cases h₁ : n = 0, {simp [coprime, h₁] at co, substs m n, simp}, by_cases h₂ : m = 0, {simp [coprime, h₂] at co, substs m n, simp}, exact int.mod_nonneg _ (mul_ne_zero (int.coe_nat_ne_zero.2 h₁) (int.coe_nat_ne_zero.2 h₂)) }, have := gcd_eq_gcd_ab n m, simp [co.gcd_eq_one, mul_comm] at this, rw [int.mod_def, ← sub_add, ← sub_add]; split, { refine dvd_add _ (dvd_trans (dvd_mul_right _ _) (dvd_mul_right _ _)), rw [add_comm, ← sub_sub], refine _root_.dvd_sub _ (dvd_mul_left _ _), have := congr_arg ((*) ↑a) this, exact ⟨_, by rwa [mul_add, ← mul_assoc, ← mul_assoc, mul_one, mul_comm, ← sub_eq_iff_eq_add] at this⟩ }, { refine dvd_add _ (dvd_trans (dvd_mul_left _ _) (dvd_mul_right _ _)), rw [← sub_sub], refine _root_.dvd_sub _ (dvd_mul_left _ _), have := congr_arg ((*) ↑b) this, exact ⟨_, by rwa [mul_add, ← mul_assoc, ← mul_assoc, mul_one, mul_comm _ ↑m, ← sub_eq_iff_eq_add'] at this⟩ } end⟩ lemma modeq_and_modeq_iff_modeq_mul {a b m n : ℕ} (hmn : coprime m n) : a ≡ b [MOD m] ∧ a ≡ b [MOD n] ↔ (a ≡ b [MOD m * n]) := ⟨λ h, begin rw [nat.modeq.modeq_iff_dvd, nat.modeq.modeq_iff_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd] at h, rw [nat.modeq.modeq_iff_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd], exact hmn.mul_dvd_of_dvd_of_dvd h.1 h.2 end, λ h, ⟨nat.modeq.modeq_of_modeq_mul_right _ h, nat.modeq.modeq_of_modeq_mul_left _ h⟩⟩ lemma coprime_of_mul_modeq_one (b : ℕ) {a n : ℕ} (h : a * b ≡ 1 [MOD n]) : coprime a n := nat.coprime_of_dvd' (λ k kp ⟨ka, hka⟩ ⟨kb, hkb⟩, int.coe_nat_dvd.1 begin rw [hka, hkb, modeq_iff_dvd] at h, cases h with z hz, rw [sub_eq_iff_eq_add] at hz, rw [hz, int.coe_nat_mul, mul_assoc, mul_assoc, int.coe_nat_mul, ← mul_add], exact dvd_mul_right _ _, end) end modeq @[simp] lemma mod_mul_right_mod (a b c : ℕ) : a % (b * c) % b = a % b := modeq.modeq_of_modeq_mul_right _ (modeq.mod_modeq _ _) @[simp] lemma mod_mul_left_mod (a b c : ℕ) : a % (b * c) % c = a % c := modeq.modeq_of_modeq_mul_left _ (modeq.mod_modeq _ _) lemma div_mod_eq_mod_mul_div (a b c : ℕ) : a / b % c = a % (b * c) / b := if hb0 : b = 0 then by simp [hb0] else by rw [← @add_right_cancel_iff _ _ (c * (a / b / c)), mod_add_div, nat.div_div_eq_div_mul, ← nat.mul_right_inj (nat.pos_of_ne_zero hb0),← @add_left_cancel_iff _ _ (a % b), mod_add_div, mul_add, ← @add_left_cancel_iff _ _ (a % (b * c) % b), add_left_comm, ← add_assoc (a % (b * c) % b), mod_add_div, ← mul_assoc, mod_add_div, mod_mul_right_mod] lemma add_mod_add_ite (a b c : ℕ) : (a + b) % c + (if c ≤ a % c + b % c then c else 0) = a % c + b % c := have (a + b) % c = (a % c + b % c) % c, from nat.modeq.modeq_add (nat.modeq.mod_modeq _ _).symm (nat.modeq.mod_modeq _ _).symm, if hc0 : c = 0 then by simp [hc0] else begin rw this, split_ifs, { have h2 : (a % c + b % c) / c < 2, from nat.div_lt_of_lt_mul (by rw mul_two; exact add_lt_add (nat.mod_lt _ (nat.pos_of_ne_zero hc0)) (nat.mod_lt _ (nat.pos_of_ne_zero hc0))), have h0 : 0 < (a % c + b % c) / c, from nat.div_pos h (nat.pos_of_ne_zero hc0), rw [← @add_right_cancel_iff _ _ (c * ((a % c + b % c) / c)), add_comm _ c, add_assoc, mod_add_div, le_antisymm (le_of_lt_succ h2) h0, mul_one, add_comm] }, { rw [nat.mod_eq_of_lt (lt_of_not_ge h), add_zero] } end lemma add_mod_of_add_mod_lt {a b c : ℕ} (hc : a % c + b % c < c) : (a + b) % c = a % c + b % c := by rw [← add_mod_add_ite, if_neg (not_le_of_lt hc), add_zero] lemma add_mod_add_of_le_add_mod {a b c : ℕ} (hc : c ≤ a % c + b % c) : (a + b) % c + c = a % c + b % c := by rw [← add_mod_add_ite, if_pos hc] lemma add_div {a b c : ℕ} (hc0 : 0 < c) : (a + b) / c = a / c + b / c + if c ≤ a % c + b % c then 1 else 0 := begin rw [← nat.mul_right_inj hc0, ← @add_left_cancel_iff _ _ ((a + b) % c + a % c + b % c)], suffices : (a + b) % c + c * ((a + b) / c) + a % c + b % c = a % c + c * (a / c) + (b % c + c * (b / c)) + c * (if c ≤ a % c + b % c then 1 else 0) + (a + b) % c, { simpa only [mul_add, add_comm, add_left_comm, add_assoc] }, rw [mod_add_div, mod_add_div, mod_add_div, mul_ite, add_assoc, add_assoc], conv_lhs { rw ← add_mod_add_ite }, simp, ac_refl end lemma add_div_eq_of_add_mod_lt {a b c : ℕ} (hc : a % c + b % c < c) : (a + b) / c = a / c + b / c := if hc0 : c = 0 then by simp [hc0] else by rw [add_div (nat.pos_of_ne_zero hc0), if_neg (not_le_of_lt hc), add_zero] protected lemma add_div_of_dvd_right {a b c : ℕ} (hca : c ∣ a) : (a + b) / c = a / c + b / c := if h : c = 0 then by simp [h] else add_div_eq_of_add_mod_lt begin rw [nat.mod_eq_zero_of_dvd hca, zero_add], exact nat.mod_lt _ (pos_iff_ne_zero.mpr h), end protected lemma add_div_of_dvd_left {a b c : ℕ} (hca : c ∣ b) : (a + b) / c = a / c + b / c := by rwa [add_comm, nat.add_div_of_dvd_right, add_comm] lemma add_div_eq_of_le_mod_add_mod {a b c : ℕ} (hc : c ≤ a % c + b % c) (hc0 : 0 < c) : (a + b) / c = a / c + b / c + 1 := by rw [add_div hc0, if_pos hc] lemma add_div_le_add_div (a b c : ℕ) : a / c + b / c ≤ (a + b) / c := if hc0 : c = 0 then by simp [hc0] else by rw [nat.add_div (nat.pos_of_ne_zero hc0)]; exact le_add_right _ _ lemma le_mod_add_mod_of_dvd_add_of_not_dvd {a b c : ℕ} (h : c ∣ a + b) (ha : ¬ c ∣ a) : c ≤ a % c + b % c := by_contradiction $ λ hc, have (a + b) % c = a % c + b % c, from add_mod_of_add_mod_lt (lt_of_not_ge hc), by simp [dvd_iff_mod_eq_zero, *] at * lemma odd_mul_odd {n m : ℕ} (hn1 : n % 2 = 1) (hm1 : m % 2 = 1) : (n * m) % 2 = 1 := show (n * m) % 2 = (1 * 1) % 2, from nat.modeq.modeq_mul hn1 hm1 lemma odd_mul_odd_div_two {m n : ℕ} (hm1 : m % 2 = 1) (hn1 : n % 2 = 1) : (m * n) / 2 = m * (n / 2) + m / 2 := have hm0 : 0 < m := nat.pos_of_ne_zero (λ h, by simp * at *), have hn0 : 0 < n := nat.pos_of_ne_zero (λ h, by simp * at *), (nat.mul_right_inj (show 0 < 2, from dec_trivial)).1 $ by rw [mul_add, two_mul_odd_div_two hm1, mul_left_comm, two_mul_odd_div_two hn1, two_mul_odd_div_two (nat.odd_mul_odd hm1 hn1), nat.mul_sub_left_distrib, mul_one, ← nat.add_sub_assoc hm0, nat.sub_add_cancel (le_mul_of_one_le_right (nat.zero_le _) hn0)] lemma odd_of_mod_four_eq_one {n : ℕ} (h : n % 4 = 1) : n % 2 = 1 := @modeq.modeq_of_modeq_mul_left 2 n 1 2 h lemma odd_of_mod_four_eq_three {n : ℕ} (h : n % 4 = 3) : n % 2 = 1 := @modeq.modeq_of_modeq_mul_left 2 n 3 2 h end nat namespace list variable {α : Type*} lemma nth_rotate : ∀ {l : list α} {n m : ℕ} (hml : m < l.length), (l.rotate n).nth m = l.nth ((m + n) % l.length) | [] n m hml := (nat.not_lt_zero _ hml).elim | l 0 m hml := by simp [nat.mod_eq_of_lt hml] | (a::l) (n+1) m hml := have h₃ : m < list.length (l ++ [a]), by simpa using hml, (lt_or_eq_of_le (nat.le_of_lt_succ $ nat.mod_lt (m + n) (lt_of_le_of_lt (nat.zero_le _) hml))).elim (λ hml', have h₁ : (m + (n + 1)) % ((a :: l : list α).length) = (m + n) % ((a :: l : list α).length) + 1, from calc (m + (n + 1)) % (l.length + 1) = ((m + n) % (l.length + 1) + 1) % (l.length + 1) : add_assoc m n 1 ▸ nat.modeq.modeq_add (nat.mod_mod _ _).symm rfl ... = (m + n) % (l.length + 1) + 1 : nat.mod_eq_of_lt (nat.succ_lt_succ hml'), have h₂ : (m + n) % (l ++ [a]).length < l.length, by simpa [nat.add_one] using hml', by rw [list.rotate_cons_succ, nth_rotate h₃, list.nth_append h₂, h₁, list.nth]; simp) (λ hml', have h₁ : (m + (n + 1)) % (l.length + 1) = 0, from calc (m + (n + 1)) % (l.length + 1) = (l.length + 1) % (l.length + 1) : add_assoc m n 1 ▸ nat.modeq.modeq_add (hml'.trans (nat.mod_eq_of_lt (nat.lt_succ_self _)).symm) rfl ... = 0 : by simp, by rw [list.length, list.rotate_cons_succ, nth_rotate h₃, list.length_append, list.length_cons, list.length, zero_add, hml', h₁, list.nth_concat_length]; refl) lemma rotate_eq_self_iff_eq_repeat [hα : nonempty α] : ∀ {l : list α}, (∀ n, l.rotate n = l) ↔ ∃ a, l = list.repeat a l.length | [] := ⟨λ h, nonempty.elim hα (λ a, ⟨a, by simp⟩), by simp⟩ | (a::l) := ⟨λ h, ⟨a, list.ext_le (by simp) $ λ n hn h₁, begin rw [← option.some_inj, ← list.nth_le_nth], conv {to_lhs, rw ← h ((list.length (a :: l)) - n)}, rw [nth_rotate hn, nat.add_sub_cancel' (le_of_lt hn), nat.mod_self, nth_le_repeat], refl end⟩, λ ⟨a, ha⟩ n, ha.symm ▸ list.ext_le (by simp) (λ m hm h, have hm' : (m + n) % (list.repeat a (list.length (a :: l))).length < list.length (a :: l), by rw list.length_repeat; exact nat.mod_lt _ (nat.succ_pos _), by rw [nth_le_repeat, ← option.some_inj, ← list.nth_le_nth, nth_rotate h, list.nth_le_nth, nth_le_repeat]; simp * at *)⟩ end list
bdb69b846a4bd259671f3b5f2a0f332f34db002f
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/back_chaining2.lean
4184948e14d4e2bc862e43e38ee6a3bca3b1005e
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
606
lean
-- Backward chaining with hypotheses constants {P Q R S T U : Prop} constants (Huq : U → Q) (Hur : U → R) (Hus : U → S) (Hut : U → T) attribute [intro] Huq Hur Hus Hut open tactic definition lemma1 : (P → Q) → P → Q := by (intros >> back_chaining_using_hs) definition lemma2 : (P → Q) → (Q → R) → P → R := by (intros >> back_chaining_using_hs) definition lemma3 : (P → Q) → (Q → R) → (R → S) → P → S := by (intros >> back_chaining_using_hs) definition lemma4 : (P → Q) → (Q → R) → (R → S) → (S → T) → P → T := by (intros >> back_chaining_using_hs)
c16428aa683179a615ccdccd830c601d9e61055e
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/algebra/group_with_zero_power.lean
5ab2bc08567ae1a83e0ddaab0cd8de054079af05
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
7,976
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.group_power /-! # Powers of elements of groups with an adjoined zero element In this file we define integer power functions for groups with an adjoined zero element. This generalises the integer power function on a division ring. -/ @[simp] lemma zero_pow' {M : Type*} [monoid_with_zero M] : ∀ n : ℕ, n ≠ 0 → (0 : M) ^ n = 0 | 0 h := absurd rfl h | (k+1) h := zero_mul _ @[simp] lemma zero_pow_eq_zero {M : Type*} [monoid_with_zero M] [nontrivial M] {n : ℕ} : (0 : M) ^ n = 0 ↔ 0 < n := begin split; intro h, { rw [nat.pos_iff_ne_zero], rintro rfl, simpa using h }, { exact zero_pow' n h.ne.symm } end theorem pow_eq_zero' {M : Type*} [monoid_with_zero M] [no_zero_divisors M] {a : M} {n : ℕ} (H : a ^ n = 0) : a = 0 := begin induction n with n ih, { rw pow_zero at H, rw [← mul_one a, H, mul_zero] }, exact or.cases_on (mul_eq_zero.1 H) id ih end @[field_simps] theorem pow_ne_zero' {M : Type*} [monoid_with_zero M] [no_zero_divisors M] {a : M} (n : ℕ) (h : a ≠ 0) : a ^ n ≠ 0 := mt pow_eq_zero' h section group_with_zero variables {G₀ : Type*} [group_with_zero G₀] section nat_pow @[simp, field_simps] theorem inv_pow' (a : G₀) (n : ℕ) : (a⁻¹) ^ n = (a ^ n)⁻¹ := by induction n with n ih; [exact inv_one.symm, rw [pow_succ', pow_succ, ih, mul_inv_rev']] theorem pow_sub' (a : G₀) {m n : ℕ} (ha : a ≠ 0) (h : n ≤ m) : a ^ (m - n) = a ^ m * (a ^ n)⁻¹ := have h1 : m - n + n = m, from nat.sub_add_cancel h, have h2 : a ^ (m - n) * a ^ n = a ^ m, by rw [←pow_add, h1], eq_div_of_mul_eq (pow_ne_zero' _ ha) h2 theorem pow_inv_comm' (a : G₀) (m n : ℕ) : (a⁻¹) ^ m * a ^ n = a ^ n * (a⁻¹) ^ m := (commute.refl a).inv_left'.pow_pow m n end nat_pow end group_with_zero section int_pow open int variables {G₀ : Type*} [group_with_zero G₀] /-- The power operation in a group with zero. This extends `monoid.pow` to negative integers with the definition `a ^ (-n) = (a ^ n)⁻¹`. -/ def fpow (a : G₀) : ℤ → G₀ | (of_nat n) := a ^ n | -[1+n] := (a ^ (nat.succ n))⁻¹ @[priority 10] instance : has_pow G₀ ℤ := ⟨fpow⟩ @[simp] theorem fpow_coe_nat (a : G₀) (n : ℕ) : a ^ (n:ℤ) = a ^ n := rfl theorem fpow_of_nat (a : G₀) (n : ℕ) : a ^ of_nat n = a ^ n := rfl @[simp] theorem fpow_neg_succ_of_nat (a : G₀) (n : ℕ) : a ^ -[1+n] = (a ^ n.succ)⁻¹ := rfl local attribute [ematch] le_of_lt @[simp] theorem fpow_zero (a : G₀) : a ^ (0:ℤ) = 1 := rfl @[simp] theorem fpow_one (a : G₀) : a ^ (1:ℤ) = a := mul_one _ @[simp] theorem one_fpow : ∀ (n : ℤ), (1 : G₀) ^ n = 1 | (n : ℕ) := one_pow _ | -[1+ n] := show _⁻¹=(1:G₀), by rw [one_pow, inv_one] lemma zero_fpow : ∀ z : ℤ, z ≠ 0 → (0 : G₀) ^ z = 0 | (of_nat n) h := zero_pow' _ $ by rintro rfl; exact h rfl | -[1+n] h := show (0*0 ^ n)⁻¹ = (0 : G₀), by simp @[simp] theorem fpow_neg (a : G₀) : ∀ (n : ℤ), a ^ -n = (a ^ n)⁻¹ | (n+1:ℕ) := rfl | 0 := inv_one.symm | -[1+ n] := (inv_inv' _).symm theorem fpow_neg_one (x : G₀) : x ^ (-1:ℤ) = x⁻¹ := congr_arg has_inv.inv $ pow_one x theorem inv_fpow (a : G₀) : ∀n:ℤ, a⁻¹ ^ n = (a ^ n)⁻¹ | (n : ℕ) := inv_pow' a n | -[1+ n] := congr_arg has_inv.inv $ inv_pow' a (n+1) lemma fpow_add_one {a : G₀} (ha : a ≠ 0) : ∀ n : ℤ, a ^ (n + 1) = a ^ n * a | (of_nat n) := by simp [← int.coe_nat_succ, pow_succ'] | -[1+0] := by simp [int.neg_succ_of_nat_eq, ha] | -[1+(n+1)] := by rw [int.neg_succ_of_nat_eq, fpow_neg, neg_add, neg_add_cancel_right, fpow_neg, ← int.coe_nat_succ, fpow_coe_nat, fpow_coe_nat, pow_succ _ (n + 1), mul_inv_rev', mul_assoc, inv_mul_cancel ha, mul_one] lemma fpow_sub_one {a : G₀} (ha : a ≠ 0) (n : ℤ) : a ^ (n - 1) = a ^ n * a⁻¹ := calc a ^ (n - 1) = a ^ (n - 1) * a * a⁻¹ : by rw [mul_assoc, mul_inv_cancel ha, mul_one] ... = a^n * a⁻¹ : by rw [← fpow_add_one ha, sub_add_cancel] lemma fpow_add {a : G₀} (ha : a ≠ 0) (m n : ℤ) : a ^ (m + n) = a ^ m * a ^ n := begin induction n using int.induction_on with n ihn n ihn, case hz : { simp }, { simp only [← add_assoc, fpow_add_one ha, ihn, mul_assoc] }, { rw [fpow_sub_one ha, ← mul_assoc, ← ihn, ← fpow_sub_one ha, add_sub_assoc] } end theorem fpow_one_add {a : G₀} (h : a ≠ 0) (i : ℤ) : a ^ (1 + i) = a * a ^ i := by rw [fpow_add h, fpow_one] theorem semiconj_by.fpow_right {a x y : G₀} (h : semiconj_by a x y) : ∀ m : ℤ, semiconj_by a (x^m) (y^m) | (n : ℕ) := h.pow_right n | -[1+n] := (h.pow_right (n + 1)).inv_right' theorem commute.fpow_right {a b : G₀} (h : commute a b) : ∀ m : ℤ, commute a (b^m) := h.fpow_right theorem commute.fpow_left {a b : G₀} (h : commute a b) (m : ℤ) : commute (a^m) b := (h.symm.fpow_right m).symm theorem commute.fpow_fpow {a b : G₀} (h : commute a b) (m n : ℤ) : commute (a^m) (b^n) := (h.fpow_left m).fpow_right n theorem commute.fpow_self (a : G₀) (n : ℤ) : commute (a^n) a := (commute.refl a).fpow_left n theorem commute.self_fpow (a : G₀) (n : ℤ) : commute a (a^n) := (commute.refl a).fpow_right n theorem commute.fpow_fpow_self (a : G₀) (m n : ℤ) : commute (a^m) (a^n) := (commute.refl a).fpow_fpow m n theorem fpow_mul (a : G₀) : ∀ m n : ℤ, a ^ (m * n) = (a ^ m) ^ n | (m : ℕ) (n : ℕ) := pow_mul _ _ _ | (m : ℕ) -[1+ n] := (fpow_neg _ (m * n.succ)).trans $ show (a ^ (m * n.succ))⁻¹ = _, by rw pow_mul; refl | -[1+ m] (n : ℕ) := (fpow_neg _ (m.succ * n)).trans $ show (a ^ (m.succ * n))⁻¹ = _, by rw [pow_mul, ← inv_pow']; refl | -[1+ m] -[1+ n] := (pow_mul a m.succ n.succ).trans $ show _ = (_⁻¹ ^ _)⁻¹, by rw [inv_pow', inv_inv'] theorem fpow_mul' (a : G₀) (m n : ℤ) : a ^ (m * n) = (a ^ n) ^ m := by rw [mul_comm, fpow_mul] @[simp, norm_cast] lemma units.coe_gpow' (u : units G₀) : ∀ (n : ℤ), ((u ^ n : units G₀) : G₀) = u ^ n | (n : ℕ) := u.coe_pow n | -[1+k] := by rw [gpow_neg_succ_of_nat, fpow_neg_succ_of_nat, units.coe_inv', u.coe_pow] lemma fpow_ne_zero_of_ne_zero {a : G₀} (ha : a ≠ 0) : ∀ (z : ℤ), a ^ z ≠ 0 | (of_nat n) := pow_ne_zero' _ ha | -[1+n] := inv_ne_zero $ pow_ne_zero' _ ha lemma fpow_sub {a : G₀} (ha : a ≠ 0) (z1 z2 : ℤ) : a ^ (z1 - z2) = a ^ z1 / a ^ z2 := by rw [sub_eq_add_neg, fpow_add ha, fpow_neg]; refl lemma commute.mul_fpow {a b : G₀} (h : commute a b) : ∀ (i : ℤ), (a * b) ^ i = (a ^ i) * (b ^ i) | (n : ℕ) := h.mul_pow n | -[1+n] := by simp [h.mul_pow, (h.pow_pow _ _).eq, mul_inv_rev'] lemma mul_fpow {G₀ : Type*} [comm_group_with_zero G₀] (a b : G₀) (m : ℤ): (a * b) ^ m = (a ^ m) * (b ^ m) := (commute.all a b).mul_fpow m lemma fpow_eq_zero {x : G₀} {n : ℤ} (h : x ^ n = 0) : x = 0 := classical.by_contradiction $ λ hx, fpow_ne_zero_of_ne_zero hx n h lemma fpow_ne_zero {x : G₀} (n : ℤ) : x ≠ 0 → x ^ n ≠ 0 := mt fpow_eq_zero theorem fpow_neg_mul_fpow_self (n : ℤ) {x : G₀} (h : x ≠ 0) : x ^ (-n) * x ^ n = 1 := begin rw [fpow_neg], exact inv_mul_cancel (fpow_ne_zero n h) end theorem one_div_pow {a : G₀} (n : ℕ) : (1 / a) ^ n = 1 / a ^ n := by simp only [one_div, inv_pow'] theorem one_div_fpow {a : G₀} (n : ℤ) : (1 / a) ^ n = 1 / a ^ n := by simp only [one_div, inv_fpow] end int_pow section variables {G₀ : Type*} [comm_group_with_zero G₀] @[simp] theorem div_pow (a b : G₀) (n : ℕ) : (a / b) ^ n = a ^ n / b ^ n := by simp only [div_eq_mul_inv, mul_pow, inv_pow'] @[simp] theorem div_fpow (a : G₀) {b : G₀} (n : ℤ) : (a / b) ^ n = a ^ n / b ^ n := by simp only [div_eq_mul_inv, mul_fpow, inv_fpow] lemma div_sq_cancel {a : G₀} (ha : a ≠ 0) (b : G₀) : a ^ 2 * b / a = a * b := by rw [pow_two, mul_assoc, mul_div_cancel_left _ ha] end
1dd15a620abeefcd0000a61a9b01d15ab184a153
5749d8999a76f3a8fddceca1f6941981e33aaa96
/archive/cubing_a_cube.lean
024893abcca2d591d266858791b28e199f2a2040
[ "Apache-2.0" ]
permissive
jdsalchow/mathlib
13ab43ef0d0515a17e550b16d09bd14b76125276
497e692b946d93906900bb33a51fd243e7649406
refs/heads/master
1,585,819,143,348
1,580,072,892,000
1,580,072,892,000
154,287,128
0
0
Apache-2.0
1,540,281,610,000
1,540,281,609,000
null
UTF-8
Lean
false
false
23,030
lean
/- Copyright (c) 2019 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn -/ import data.real.basic data.set.disjointed data.set.intervals set_theory.cardinal /-! Proof that a cube (in dimension n ≥ 3) cannot be cubed: There does not exist a partition of a cube into finitely many smaller cubes (at least two) of different sizes. We follow the proof described here: http://www.alaricstephen.com/main-featured/2017/9/28/cubing-a-cube-proof -/ open real set function fin noncomputable theory variable {n : ℕ} /-- Given three intervals `I, J, K` such that `J ⊂ I`, neither endpoint of `J` coincides with an endpoint of `I`, `¬ (K ⊆ J)` and `K` does not lie completely to the left nor completely to the right of `J`. Then `I ∩ K \ J` is nonempty. -/ lemma Ico_lemma {α} [decidable_linear_order α] {x₁ x₂ y₁ y₂ z₁ z₂ w : α} (h₁ : x₁ < y₁) (hy : y₁ < y₂) (h₂ : y₂ < x₂) (hz₁ : z₁ ≤ y₂) (hz₂ : y₁ ≤ z₂) (hw : w ∉ Ico y₁ y₂ ∧ w ∈ Ico z₁ z₂) : ∃w, w ∈ Ico x₁ x₂ ∧ w ∉ Ico y₁ y₂ ∧ w ∈ Ico z₁ z₂ := begin simp at hw, refine ⟨max x₁ (min w y₂), _, _, _⟩, { simp [le_refl, lt_trans h₁ (lt_trans hy h₂), h₂] }, { simp [lt_irrefl, not_le_of_lt h₁], intros, apply hw.1, assumption }, { simp [hw.2.1, hw.2.2, hz₁, lt_of_lt_of_le h₁ hz₂] at ⊢ } end /-- A (hyper)-cube (in standard orientation) is a vector `b` consisting of the bottom-left point of the cube, a width `w` and a proof that `w > 0`. We use functions from `fin n` to denote vectors. -/ structure cube (n : ℕ) : Type := (b : fin n → ℝ) -- bottom-left coordinate (w : ℝ) -- width (hw : 0 < w) namespace cube lemma hw' (c : cube n) : 0 ≤ c.w := le_of_lt c.hw /-- The j-th side of a cube is the half-open interval `[b j, b j + w)` -/ def side (c : cube n) (j : fin n) : set ℝ := Ico (c.b j) (c.b j + c.w) @[simp] lemma b_mem_side (c : cube n) (j : fin n) : c.b j ∈ c.side j := by simp [side, cube.hw, le_refl] def to_set (c : cube n) : set (fin n → ℝ) := { x | ∀j, x j ∈ side c j } def to_set_subset {c c' : cube n} : c.to_set ⊆ c'.to_set ↔ ∀j, c.side j ⊆ c'.side j := begin split, intros h j x hx, let f : fin n → ℝ := λ j', if j' = j then x else c.b j', have : f ∈ c.to_set, { intro j', by_cases hj' : j' = j; simp [f, hj', if_pos, if_neg, hx] }, convert h this j, { simp [f, if_pos] }, intros h f hf j, exact h j (hf j) end def to_set_disjoint {c c' : cube n} : disjoint c.to_set c'.to_set ↔ ∃j, disjoint (c.side j) (c'.side j) := begin split, intros h, classical, by_contra h', simp only [not_disjoint_iff, classical.skolem, not_exists] at h', cases h' with f hf, apply not_disjoint_iff.mpr ⟨f, _, _⟩ h; intro j, exact (hf j).1, exact (hf j).2, rintro ⟨j, hj⟩, rw [set.disjoint_iff], rintros f ⟨h1f, h2f⟩, apply not_disjoint_iff.mpr ⟨f j, h1f j, h2f j⟩ hj end lemma b_mem_to_set (c : cube n) : c.b ∈ c.to_set := by simp [to_set] protected def tail (c : cube (n+1)) : cube n := ⟨tail c.b, c.w, c.hw⟩ lemma side_tail (c : cube (n+1)) (j : fin n) : c.tail.side j = c.side j.succ := rfl def bottom (c : cube (n+1)) : set (fin (n+1) → ℝ) := { x | x 0 = c.b 0 ∧ tail x ∈ c.tail.to_set } lemma b_mem_bottom (c : cube (n+1)) : c.b ∈ c.bottom := by simp [bottom, to_set, side, cube.hw, le_refl, cube.tail] def xm (c : cube (n+1)) : ℝ := c.b 0 + c.w lemma b_lt_xm (c : cube (n+1)) : c.b 0 < c.xm := by simp [xm, hw] lemma b_ne_xm (c : cube (n+1)) : c.b 0 ≠ c.xm := ne_of_lt c.b_lt_xm def shift_up (c : cube (n+1)) : cube (n+1) := ⟨cons c.xm $ tail c.b, c.w, c.hw⟩ @[simp] lemma tail_shift_up (c : cube (n+1)) : c.shift_up.tail = c.tail := by simp [shift_up, cube.tail] @[simp] lemma head_shift_up (c : cube (n+1)) : c.shift_up.b 0 = c.xm := rfl def unit_cube : cube n := ⟨λ _, 0, 1, by norm_num⟩ @[simp] lemma side_unit_cube {j : fin n} : unit_cube.side j = Ico 0 1 := by norm_num [unit_cube, side] end cube open cube variables {ι : Type} [fintype ι] {cs : ι → cube (n+1)} {i i' : ι} /-- A finite family of (at least 2) cubes partitioning the unit cube with different sizes -/ def correct (cs : ι → cube n) : Prop := pairwise (disjoint on (cube.to_set ∘ cs)) ∧ (⋃(i : ι), (cs i).to_set) = unit_cube.to_set ∧ injective (cube.w ∘ cs) ∧ 2 ≤ cardinal.mk ι ∧ 3 ≤ n variable (h : correct cs) include h lemma to_set_subset_unit_cube {i} : (cs i).to_set ⊆ unit_cube.to_set := by { rw [←h.2.1], exact subset_Union _ i } lemma side_subset {i j} : (cs i).side j ⊆ Ico 0 1 := by { have := to_set_subset_unit_cube h, rw [to_set_subset] at this, convert this j, norm_num [unit_cube] } lemma zero_le_of_mem_side {i j x} (hx : x ∈ (cs i).side j) : 0 ≤ x := (side_subset h hx).1 lemma zero_le_of_mem {i p} (hp : p ∈ (cs i).to_set) (j) : 0 ≤ p j := zero_le_of_mem_side h (hp j) lemma zero_le_b {i j} : 0 ≤ (cs i).b j := zero_le_of_mem h (cs i).b_mem_to_set j lemma b_add_w_le_one {j} : (cs i).b j + (cs i).w ≤ 1 := by { have := side_subset h, rw [side, Ico_subset_Ico_iff] at this, convert this.2, simp [hw] } /-- The width of any cube in the partition cannot be 1. -/ lemma w_ne_one (i : ι) : (cs i).w ≠ 1 := begin intro hi, have := h.2.2.2.1, rw [cardinal.two_le_iff' i] at this, cases this with i' hi', let p := (cs i').b, have hp : p ∈ (cs i').to_set := (cs i').b_mem_to_set, have h2p : p ∈ (cs i).to_set, { intro j, split, transitivity (0 : ℝ), { rw [←add_le_add_iff_right (1 : ℝ)], convert b_add_w_le_one h, rw hi, rw zero_add }, apply zero_le_b h, apply lt_of_lt_of_le (side_subset h $ (cs i').b_mem_side j).2, simp [hi, zero_le_b h] }, apply not_disjoint_iff.mpr ⟨p, hp, h2p⟩, apply h.1, exact hi'.symm end /-- The top of a cube (which is the bottom of the cube shifted up by its width) must be covered by bottoms of (other) cubes in the family. -/ lemma shift_up_bottom_subset_bottoms (hc : (cs i).xm ≠ 1) : (cs i).shift_up.bottom ⊆ ⋃(i : ι), (cs i).bottom := begin intros p hp, cases hp with hp0 hps, rw [tail_shift_up] at hps, have : p ∈ (unit_cube : cube (n+1)).to_set, { simp only [to_set, forall_fin_succ, hp0, side_unit_cube, mem_set_of_eq, mem_Ico, head_shift_up], refine ⟨⟨_, _⟩, _⟩, { rw [←zero_add (0 : ℝ)], apply add_le_add, apply zero_le_b h, apply (cs i).hw' }, { exact lt_of_le_of_ne (b_add_w_le_one h) hc }, intro j, exact side_subset h (hps j) }, rw [←h.2.1] at this, rcases this with ⟨_, ⟨i', rfl⟩, hi'⟩, rw [mem_Union], use i', refine ⟨_, λ j, hi' j.succ⟩, have : i ≠ i', { rintro rfl, apply not_le_of_lt (hi' 0).2, rw [hp0], refl }, have := h.1 i i' this, rw [on_fun, to_set_disjoint, exists_fin_succ] at this, rcases this with h0|⟨j, hj⟩, rw [hp0], symmetry, apply eq_of_Ico_disjoint h0 (by simp [hw]) _, convert hi' 0, rw [hp0], refl, exfalso, apply not_disjoint_iff.mpr ⟨tail p j, hps j, hi' j.succ⟩ hj end omit h /-- A valley is a square on which cubes in the family of cubes are placed, so that the cubes completely cover the valley and none of those cubes is partially outside the square. We also require that no cube on it has the same size as the valley (so that there are at least two cubes on the valley). This is the main concept in the formalization. We prove that the smallest cube on a valley has another valley on the top of it, which gives an infinite sequence of cubes in the partition, which contradicts the finiteness. A valley is characterized by a cube `c` (which is not a cube in the family cs) by considering the bottom face of `c`. -/ def valley (cs : ι → cube (n+1)) (c : cube (n+1)) : Prop := c.bottom ⊆ (⋃(i : ι), (cs i).bottom) ∧ (∀i, (cs i).b 0 = c.b 0 → (∃x, x ∈ (cs i).tail.to_set ∩ c.tail.to_set) → (cs i).tail.to_set ⊆ c.tail.to_set) ∧ ∀(i : ι), (cs i).b 0 = c.b 0 → (cs i).w ≠ c.w variables {c : cube (n+1)} (v : valley cs c) /-- The bottom of the unit cube is a valley -/ lemma valley_unit_cube (h : correct cs) : valley cs unit_cube := begin refine ⟨_, _, _⟩, { intro v, simp [bottom], intros h0 hv, have : v ∈ (unit_cube : cube (n+1)).to_set, { dsimp [to_set], rw [forall_fin_succ, h0], split, norm_num [side, unit_cube], exact hv }, rw [←h.2.1] at this, rcases this with ⟨_, ⟨i, rfl⟩, hi⟩, use i, split, { apply le_antisymm, rw h0, exact zero_le_b h, exact (hi 0).1 }, intro j, exact hi _ }, { intros i hi h', rw to_set_subset, intro j, convert side_subset h, simp [side_tail] }, { intros i hi, exact w_ne_one h i } end /-- the cubes which lie in the valley `c` -/ def bcubes (cs : ι → cube (n+1)) (c : cube (n+1)) : set ι := { i : ι | (cs i).b 0 = c.b 0 ∧ (cs i).tail.to_set ⊆ c.tail.to_set } /-- A cube which lies on the boundary of a valley in dimension `j` -/ def on_boundary (hi : i ∈ bcubes cs c) (j : fin n) : Prop := c.b j.succ = (cs i).b j.succ ∨ (cs i).b j.succ + (cs i).w = c.b j.succ + c.w lemma tail_sub (hi : i ∈ bcubes cs c) : ∀j, (cs i).tail.side j ⊆ c.tail.side j := by { rw [←to_set_subset], exact hi.2 } lemma bottom_mem_side (hi : i ∈ bcubes cs c) : c.b 0 ∈ (cs i).side 0 := by { convert b_mem_side (cs i) _ using 1, rw hi.1 } lemma b_le_b (hi : i ∈ bcubes cs c) (j : fin n) : c.b j.succ ≤ (cs i).b j.succ := (tail_sub hi j $ b_mem_side _ _).1 lemma t_le_t (hi : i ∈ bcubes cs c) (j : fin n) : (cs i).b j.succ + (cs i).w ≤ c.b j.succ + c.w := begin have h' := tail_sub hi j, dsimp only [side] at h', rw [Ico_subset_Ico_iff] at h', exact h'.2, simp [hw] end include h v /-- Every cube in the valley must be smaller than it -/ lemma w_lt_w (hi : i ∈ bcubes cs c) : (cs i).w < c.w := begin apply lt_of_le_of_ne _ (v.2.2 i hi.1), have j : fin n := ⟨1, nat.le_of_succ_le_succ h.2.2.2.2⟩, rw [←add_le_add_iff_left ((cs i).b j.succ)], apply le_trans (t_le_t hi j), rw [add_le_add_iff_right], apply b_le_b hi, end open cardinal /-- There are at least two cubes in a valley -/ lemma two_le_mk_bcubes : 2 ≤ cardinal.mk (bcubes cs c) := begin rw [two_le_iff], rcases v.1 c.b_mem_bottom with ⟨_, ⟨i, rfl⟩, hi⟩, have h2i : i ∈ bcubes cs c := ⟨hi.1.symm, v.2.1 i hi.1.symm ⟨tail c.b, hi.2, λ j, c.b_mem_side j.succ⟩⟩, let j : fin (n+1) := ⟨2, h.2.2.2.2⟩, have hj : 0 ≠ j := by { intro h', have := congr_arg fin.val h', contradiction }, let p : fin (n+1) → ℝ := λ j', if j' = j then c.b j + (cs i).w else c.b j', have hp : p ∈ c.bottom, { split, { simp only [bottom, p, if_neg hj] }, intro j', simp [tail, side_tail], by_cases hj' : j'.succ = j, { simp [p, -add_comm, if_pos, side, hj', hw', w_lt_w h v h2i] }, { simp [p, -add_comm, if_neg hj'] }}, rcases v.1 hp with ⟨_, ⟨i', rfl⟩, hi'⟩, have h2i' : i' ∈ bcubes cs c := ⟨hi'.1.symm, v.2.1 i' hi'.1.symm ⟨tail p, hi'.2, hp.2⟩⟩, refine ⟨⟨i, h2i⟩, ⟨i', h2i'⟩, _⟩, intro hii', cases congr_arg subtype.val hii', apply not_le_of_lt (hi'.2 ⟨1, nat.le_of_succ_le_succ h.2.2.2.2⟩).2, simp only [-add_comm, tail, cube.tail, p], rw [if_pos], simp [-add_comm], exact (hi.2 _).1, refl end /-- There is a cube in the valley -/ lemma nonempty_bcubes : nonempty (bcubes cs c) := begin rw [←ne_zero_iff_nonempty], intro h', have := two_le_mk_bcubes h v, rw h' at this, apply not_lt_of_le this, rw [←nat.cast_two, ←nat.cast_zero, nat_cast_lt], norm_num end /-- There is a smallest cube in the valley -/ lemma exists_mi : ∃(i : ι), i ∈ bcubes cs c ∧ ∀(i' ∈ bcubes cs c), (cs i).w ≤ (cs i').w := (bcubes cs c).exists_min (λ i, (cs i).w) (finite.of_fintype _) (nonempty_bcubes h v) /-- We let `mi` be the (index for the) smallest cube in the valley `c` -/ def mi : ι := classical.some $ exists_mi h v variables {h v} lemma mi_mem_bcubes : mi h v ∈ bcubes cs c := (classical.some_spec $ exists_mi h v).1 lemma mi_minimal (hi : i ∈ bcubes cs c) : (cs $ mi h v).w ≤ (cs i).w := (classical.some_spec $ exists_mi h v).2 i hi lemma mi_strict_minimal (hii' : mi h v ≠ i) (hi : i ∈ bcubes cs c) : (cs $ mi h v).w < (cs i).w := by { apply lt_of_le_of_ne (mi_minimal hi), apply h.2.2.1.ne, apply hii' } /-- The top of `mi` cannot be 1, since there is a larger cube in the valley -/ lemma mi_xm_ne_one : (cs $ mi h v).xm ≠ 1 := begin apply ne_of_lt, rcases (two_le_iff' _).mp (two_le_mk_bcubes h v) with ⟨⟨i, hi⟩, h2i⟩, swap, exact ⟨mi h v, mi_mem_bcubes⟩, apply lt_of_lt_of_le _ (b_add_w_le_one h), exact i, exact 0, rw [xm, mi_mem_bcubes.1, hi.1, _root_.add_lt_add_iff_left], apply mi_strict_minimal _ hi, intro h', apply h2i, rw subtype.ext, exact h' end /-- If `mi` lies on the boundary of the valley in dimension j, then this lemma expresses that all other cubes on the same boundary extend further from the boundary. More precisely, there is a j-th coordinate `x : ℝ` in the valley, but not in `mi`, such that every cube that shares a (particular) j-th coordinate with `mi` also contains j-th coordinate `x` -/ lemma smallest_on_boundary {j} (bi : on_boundary (mi_mem_bcubes : mi h v ∈ _) j) : ∃(x : ℝ), x ∈ c.side j.succ \ (cs $ mi h v).side j.succ ∧ ∀{{i'}} (hi' : i' ∈ bcubes cs c), i' ≠ mi h v → (cs $ mi h v).b j.succ ∈ (cs i').side j.succ → x ∈ (cs i').side j.succ := begin let i := mi h v, have hi : i ∈ bcubes cs c := mi_mem_bcubes, cases bi, { refine ⟨(cs i).b j.succ + (cs i).w, ⟨_, _⟩, _⟩, { simp [side, bi, hw', w_lt_w h v hi] }, { intro h', simpa [i, lt_irrefl] using h'.2 }, intros i' hi' i'_i h2i', split, apply le_trans h2i'.1, { simp [hw'] }, apply lt_of_lt_of_le (add_lt_add_left (mi_strict_minimal i'_i.symm hi') _), simp [bi.symm, b_le_b hi'] }, let s := bcubes cs c \ { i }, have hs : nonempty s, { rcases (two_le_iff' (⟨i, hi⟩ : bcubes cs c)).mp (two_le_mk_bcubes h v) with ⟨⟨i', hi'⟩, h2i'⟩, refine ⟨⟨i', hi', _⟩⟩, simp only [mem_singleton_iff], intro h, apply h2i', simp [h] }, rcases set.exists_min s (w ∘ cs) (finite.of_fintype _) hs with ⟨i', ⟨hi', h2i'⟩, h3i'⟩, rw [mem_singleton_iff] at h2i', let x := c.b j.succ + c.w - (cs i').w, have hx : x < (cs i).b j.succ, { dsimp only [x], rw [←bi, add_sub_assoc, add_lt_iff_neg_left, sub_lt_zero], apply mi_strict_minimal (ne.symm h2i') hi' }, refine ⟨x, ⟨_, _⟩, _⟩, { simp only [side, x, -add_comm, -add_assoc, neg_lt_zero, hw, add_lt_iff_neg_left, and_true, mem_Ico, sub_eq_add_neg], rw [add_assoc, le_add_iff_nonneg_right, ←sub_eq_add_neg, sub_nonneg], apply le_of_lt (w_lt_w h v hi') }, { simp only [side, not_and_distrib, not_lt, add_comm, not_le, mem_Ico], left, exact hx }, intros i'' hi'' h2i'' h3i'', split, swap, apply lt_trans hx h3i''.2, simp only [x], rw [le_sub_iff_add_le], refine le_trans _ (t_le_t hi'' j), rw [add_le_add_iff_left], apply h3i' i'' ⟨hi'', _⟩, simp [mem_singleton, h2i''] end variables (h v) /-- `mi` cannot lie on the boundary of the valley. Otherwise, the cube adjacent to it in the `j`-th direction will intersect one of the neighbouring cubes on the same boundary as `mi`. -/ lemma mi_not_on_boundary (j : fin n) : ¬on_boundary (mi_mem_bcubes : mi h v ∈ _) j := begin let i := mi h v, have hi : i ∈ bcubes cs c := mi_mem_bcubes, rcases (two_le_iff' j).mp _ with ⟨j', hj'⟩, swap, { rw [mk_fin, ←nat.cast_two, nat_cast_le], apply nat.le_of_succ_le_succ h.2.2.2.2 }, intro hj, rcases smallest_on_boundary hj with ⟨x, ⟨hx, h2x⟩, h3x⟩, let p : fin (n+1) → ℝ := cons (c.b 0) (λ j₂, if j₂ = j then x else (cs i).b j₂.succ), have hp : p ∈ c.bottom, { simp [bottom, p, to_set, tail, side_tail], intro j₂, by_cases hj₂ : j₂ = j, simp [hj₂, hx], simp [hj₂], apply tail_sub hi, apply b_mem_side }, rcases v.1 hp with ⟨_, ⟨i', rfl⟩, hi'⟩, have h2i' : i' ∈ bcubes cs c := ⟨hi'.1.symm, v.2.1 i' hi'.1.symm ⟨tail p, hi'.2, hp.2⟩⟩, have i_i' : i ≠ i', { rintro rfl, simpa [p, side_tail, i, h2x] using hi'.2 j }, have : nonempty ↥((cs i').tail.side j' \ (cs i).tail.side j'), { apply nonempty_Ico_sdiff, apply mi_strict_minimal i_i' h2i', apply hw }, rcases this with ⟨⟨x', hx'⟩⟩, let p' : fin (n+1) → ℝ := cons (c.b 0) (λ j₂, if j₂ = j' then x' else (cs i).b j₂.succ), have hp' : p' ∈ c.bottom, { simp [bottom, p', to_set, tail, side_tail], intro j₂, by_cases hj₂ : j₂ = j', simp [hj₂], apply tail_sub h2i', apply hx'.1, simp [hj₂], apply tail_sub hi, apply b_mem_side }, rcases v.1 hp' with ⟨_, ⟨i'', rfl⟩, hi''⟩, have h2i'' : i'' ∈ bcubes cs c := ⟨hi''.1.symm, v.2.1 i'' hi''.1.symm ⟨tail p', hi''.2, hp'.2⟩⟩, have i'_i'' : i' ≠ i'', { rintro ⟨⟩, have : (cs i).b ∈ (cs i').to_set, { simp [to_set, forall_fin_succ, hi.1, bottom_mem_side h2i'], intro j₂, by_cases hj₂ : j₂ = j, simpa [side_tail, p', hj', hj₂] using hi''.2 j, simpa [hj₂] using hi'.2 j₂ }, apply not_disjoint_iff.mpr ⟨(cs i).b, (cs i).b_mem_to_set, this⟩ (h.1 i i' i_i') }, have i_i'' : i ≠ i'', { intro h, induction h, simpa [hx'.2] using hi''.2 j' }, apply not.elim _ (h.1 i' i'' i'_i''), simp only [on_fun, to_set_disjoint, not_disjoint_iff, forall_fin_succ, not_exists, comp_app], refine ⟨⟨c.b 0, bottom_mem_side h2i', bottom_mem_side h2i''⟩, _⟩, intro j₂, by_cases hj₂ : j₂ = j, { cases hj₂, refine ⟨x, _, _⟩, { convert hi'.2 j, simp [p] }, apply h3x h2i'' i_i''.symm, convert hi''.2 j, simp [p', hj'] }, by_cases h2j₂ : j₂ = j', { cases h2j₂, refine ⟨x', hx'.1, _⟩, convert hi''.2 j', simp }, refine ⟨(cs i).b j₂.succ, _, _⟩, { convert hi'.2 j₂, simp [hj₂] }, { convert hi''.2 j₂, simp [h2j₂] } end variables {h v} /-- The same result that `mi` cannot lie on the boundary of the valley written as inequalities. -/ lemma mi_not_on_boundary' (j : fin n) : c.tail.b j < (cs (mi h v)).tail.b j ∧ (cs (mi h v)).tail.b j + (cs (mi h v)).w < c.tail.b j + c.w := begin have := mi_not_on_boundary h v j, simp only [on_boundary, not_or_distrib] at this, cases this with h1 h2, split, apply lt_of_le_of_ne (b_le_b mi_mem_bcubes _) h1, apply lt_of_le_of_ne _ h2, apply ((Ico_subset_Ico_iff _).mp (tail_sub mi_mem_bcubes j)).2, simp [hw] end /-- The top of `mi` gives rise to a new valley, since the neighbouring cubes extend further upward than `mi`. -/ def valley_mi : valley cs ((cs (mi h v)).shift_up) := begin let i := mi h v, have hi : i ∈ bcubes cs c := mi_mem_bcubes, refine ⟨_, _, _⟩, { intro p, apply shift_up_bottom_subset_bottoms h mi_xm_ne_one }, { rintros i' hi' ⟨p2, hp2, h2p2⟩, simp only [head_shift_up] at hi', classical, by_contra h2i', rw [tail_shift_up] at h2p2, simp only [not_subset, tail_shift_up] at h2i', rcases h2i' with ⟨p1, hp1, h2p1⟩, have : ∃p3, p3 ∈ (cs i').tail.to_set ∧ p3 ∉ (cs i).tail.to_set ∧ p3 ∈ c.tail.to_set, { simp [to_set, not_forall] at h2p1, cases h2p1 with j hj, rcases Ico_lemma (mi_not_on_boundary' j).1 (by simp [hw]) (mi_not_on_boundary' j).2 (le_trans (hp2 j).1 $ le_of_lt (h2p2 j).2) (le_trans (h2p2 j).1 $ le_of_lt (hp2 j).2) ⟨hj, hp1 j⟩ with ⟨w, hw, h2w, h3w⟩, refine ⟨λ j', if j' = j then w else p2 j', _, _, _⟩, { intro j', by_cases h : j' = j, simp [if_pos h], convert h3w, simp [if_neg h], exact hp2 j' }, { simp [to_set, not_forall], use j, rw [if_pos rfl], convert h2w }, { intro j', by_cases h : j' = j, simp [if_pos h, side_tail], convert hw, simp [if_neg h], apply hi.2, apply h2p2 }}, rcases this with ⟨p3, h1p3, h2p3, h3p3⟩, let p := @cons n (λ_, ℝ) (c.b 0) p3, have hp : p ∈ c.bottom, { refine ⟨rfl, _⟩, rwa [tail_cons] }, rcases v.1 hp with ⟨_, ⟨i'', rfl⟩, hi''⟩, have h2i'' : i'' ∈ bcubes cs c, { use hi''.1.symm, apply v.2.1 i'' hi''.1.symm, use tail p, split, exact hi''.2, rw [tail_cons], exact h3p3 }, have h3i'' : (cs i).w < (cs i'').w, { apply mi_strict_minimal _ h2i'', rintro rfl, apply h2p3, convert hi''.2, rw [tail_cons] }, let p' := @cons n (λ_, ℝ) (cs i).xm p3, have hp' : p' ∈ (cs i').to_set, { simpa [to_set, forall_fin_succ, p', hi'.symm] using h1p3 }, have h2p' : p' ∈ (cs i'').to_set, { simp [to_set, forall_fin_succ, p'], refine ⟨_, by simpa [to_set, p] using hi''.2⟩, have : (cs i).b 0 = (cs i'').b 0, { by rw [hi.1, h2i''.1] }, simp [side, hw', xm, this, h3i''] }, apply not_disjoint_iff.mpr ⟨p', hp', h2p'⟩, apply h.1, rintro rfl, apply (cs i).b_ne_xm, rw [←hi', ←hi''.1, hi.1], refl }, { intros i' hi' h2i', dsimp [shift_up] at h2i', replace h2i' := h.2.2.1 h2i'.symm, induction h2i', exact b_ne_xm (cs i) hi' } end variables (h) omit v /-- We get a sequence of cubes whose size is decreasing -/ noncomputable def sequence_of_cubes : ℕ → { i : ι // valley cs ((cs i).shift_up) } | 0 := let v := valley_unit_cube h in ⟨mi h v, valley_mi⟩ | (k+1) := let v := (sequence_of_cubes k).2 in ⟨mi h v, valley_mi⟩ def decreasing_sequence (k : ℕ) : order_dual ℝ := (cs (sequence_of_cubes h k).1).w lemma strict_mono_sequence_of_cubes : strict_mono $ decreasing_sequence h := strict_mono.nat $ begin intro k, let v := (sequence_of_cubes h k).2, dsimp [decreasing_sequence, sequence_of_cubes], apply w_lt_w h v (mi_mem_bcubes : mi h v ∈ _), end omit h /-- The infinite sequence of cubes contradicts the finiteness of the family. -/ theorem not_correct : ¬correct cs := begin intro h, apply not_le_of_lt (lt_omega_iff_fintype.mpr ⟨_inst_1⟩), rw [omega, lift_id], fapply mk_le_of_injective, exact λ n, (sequence_of_cubes h n).1, intros n m hnm, apply strict_mono.injective (strict_mono_sequence_of_cubes h), dsimp only [decreasing_sequence], rw hnm end /-- A cube cannot be cubed. -/ theorem cannot_cube_a_cube : ∀{n : ℕ}, n ≥ 3 → -- In ℝ^n for n ≥ 3 ∀{ι : Type} [fintype ι] {cs : ι → cube n}, -- given a finite collection of (hyper)cubes 2 ≤ cardinal.mk ι → -- containing at least two elements pairwise (disjoint on (cube.to_set ∘ cs)) → -- which is pairwise disjoint (⋃(i : ι), (cs i).to_set) = unit_cube.to_set → -- whose union is the unit cube injective (cube.w ∘ cs) → -- such that the widths of all cubes are different false := -- then we can derive a contradiction begin intros n hn ι hι cs h1 h2 h3 h4, resetI, rcases n, cases hn, exact not_correct ⟨h2, h3, h4, h1, hn⟩ end
ddd707a1fa64bac94d23ab39b09e4620e1d39685
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/order/order_iso.lean
ded26fe0d0d1eade2acaf73f356fd548b9a7db7b
[ "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
13,713
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 order.basic logic.embedding data.nat.basic open function universes u v w variables {α : Type*} {β : Type*} {γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} /-- An increasing function is injective -/ lemma injective_of_increasing (r : α → α → Prop) (s : β → β → Prop) [is_trichotomous α r] [is_irrefl β s] (f : α → β) (hf : ∀{x y}, r x y → s (f x) (f y)) : injective f := begin intros x y hxy, rcases trichotomous_of r x y with h | h | h, have := hf h, rw hxy at this, exfalso, exact irrefl_of s (f y) this, exact h, have := hf h, rw hxy at this, exfalso, exact irrefl_of s (f y) this end structure order_embedding {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ↪ β := (ord : ∀ {a b}, r a b ↔ s (to_embedding a) (to_embedding b)) infix ` ≼o `:25 := order_embedding /-- the induced order on a subtype is an embedding under the natural inclusion. -/ definition subtype.order_embedding {X : Type*} (r : X → X → Prop) (p : X → Prop) : ((subtype.val : subtype p → X) ⁻¹'o r) ≼o r := ⟨⟨subtype.val,subtype.val_injective⟩,by intros;refl⟩ theorem preimage_equivalence {α β} (f : α → β) {s : β → β → Prop} (hs : equivalence s) : equivalence (f ⁻¹'o s) := ⟨λ a, hs.1 _, λ a b h, hs.2.1 h, λ a b c h₁ h₂, hs.2.2 h₁ h₂⟩ namespace order_embedding instance : has_coe_to_fun (r ≼o s) := ⟨λ _, α → β, λ o, o.to_embedding⟩ theorem ord' : ∀ (f : r ≼o s) {a b}, r a b ↔ s (f a) (f b) | ⟨f, o⟩ := @o @[simp] theorem coe_fn_mk (f : α ↪ β) (o) : (@order_embedding.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_embedding (f : r ≼o s) : (f.to_embedding : α → β) = f := rfl theorem eq_of_to_fun_eq : ∀ {e₁ e₂ : r ≼o s}, (e₁ : α → β) = e₂ → e₁ = e₂ | ⟨⟨f₁, h₁⟩, o₁⟩ ⟨⟨f₂, h₂⟩, o₂⟩ h := by congr; exact h @[refl] protected def refl (r : α → α → Prop) : r ≼o r := ⟨embedding.refl _, λ a b, iff.rfl⟩ @[trans] protected def trans (f : r ≼o s) (g : s ≼o t) : r ≼o t := ⟨f.1.trans g.1, λ a b, by rw [f.2, g.2]; simp⟩ @[simp] theorem refl_apply (x : α) : order_embedding.refl r x = x := rfl @[simp] theorem trans_apply (f : r ≼o s) (g : s ≼o t) (a : α) : (f.trans g) a = g (f a) := rfl /-- An order embedding is also an order embedding between dual orders. -/ def rsymm (f : r ≼o s) : swap r ≼o swap s := ⟨f.to_embedding, λ a b, f.ord'⟩ /-- If `f` is injective, then it is an order embedding from the preimage order of `s` to `s`. -/ def preimage (f : α ↪ β) (s : β → β → Prop) : f ⁻¹'o s ≼o s := ⟨f, λ a b, iff.rfl⟩ theorem eq_preimage (f : r ≼o s) : r = f ⁻¹'o s := by funext a b; exact propext f.ord' protected theorem is_irrefl : ∀ (f : r ≼o s) [is_irrefl β s], is_irrefl α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a h, H _ (o.1 h)⟩ protected theorem is_refl : ∀ (f : r ≼o s) [is_refl β s], is_refl α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a, o.2 (H _)⟩ protected theorem is_symm : ∀ (f : r ≼o s) [is_symm β s], is_symm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h, o.2 (H _ _ (o.1 h))⟩ protected theorem is_asymm : ∀ (f : r ≼o s) [is_asymm β s], is_asymm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, H _ _ (o.1 h₁) (o.1 h₂)⟩ protected theorem is_antisymm : ∀ (f : r ≼o s) [is_antisymm β s], is_antisymm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, f.inj' (H _ _ (o.1 h₁) (o.1 h₂))⟩ protected theorem is_trans : ∀ (f : r ≼o s) [is_trans β s], is_trans α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b c h₁ h₂, o.2 (H _ _ _ (o.1 h₁) (o.1 h₂))⟩ protected theorem is_total : ∀ (f : r ≼o s) [is_total β s], is_total α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o o).2 (H _ _)⟩ protected theorem is_preorder : ∀ (f : r ≼o s) [is_preorder β s], is_preorder α r | f H := by exactI {..f.is_refl, ..f.is_trans} protected theorem is_partial_order : ∀ (f : r ≼o s) [is_partial_order β s], is_partial_order α r | f H := by exactI {..f.is_preorder, ..f.is_antisymm} protected theorem is_linear_order : ∀ (f : r ≼o s) [is_linear_order β s], is_linear_order α r | f H := by exactI {..f.is_partial_order, ..f.is_total} protected theorem is_strict_order : ∀ (f : r ≼o s) [is_strict_order β s], is_strict_order α r | f H := by exactI {..f.is_irrefl, ..f.is_trans} protected theorem is_trichotomous : ∀ (f : r ≼o s) [is_trichotomous β s], is_trichotomous α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o (or_congr f.inj'.eq_iff.symm o)).2 (H _ _)⟩ protected theorem is_strict_total_order' : ∀ (f : r ≼o s) [is_strict_total_order' β s], is_strict_total_order' α r | f H := by exactI {..f.is_trichotomous, ..f.is_strict_order} protected theorem acc (f : r ≼o s) (a : α) : acc s (f a) → acc r a := begin generalize h : f a = b, intro ac, induction ac with _ H IH generalizing a, subst h, exact ⟨_, λ a' h, IH (f a') (f.ord'.1 h) _ rfl⟩ end protected theorem well_founded : ∀ (f : r ≼o s) (h : well_founded s), well_founded r | f ⟨H⟩ := ⟨λ a, f.acc _ (H _)⟩ protected theorem is_well_order : ∀ (f : r ≼o s) [is_well_order β s], is_well_order α r | f H := by exactI {wf := f.well_founded H.wf, ..f.is_strict_total_order'} /-- It suffices to prove `f` is monotone between strict orders to show it is an order embedding. -/ def of_monotone [is_trichotomous α r] [is_asymm β s] (f : α → β) (H : ∀ a b, r a b → s (f a) (f b)) : r ≼o s := begin haveI := @is_irrefl_of_is_asymm β s _, refine ⟨⟨f, λ a b e, _⟩, λ a b, ⟨H _ _, λ h, _⟩⟩, { refine ((@trichotomous _ r _ a b).resolve_left _).resolve_right _; exact λ h, @irrefl _ s _ _ (by simpa [e] using H _ _ h) }, { refine (@trichotomous _ r _ a b).resolve_right (or.rec (λ e, _) (λ h', _)), { subst e, exact irrefl _ h }, { exact asymm (H _ _ h') h } } end @[simp] theorem of_monotone_coe [is_trichotomous α r] [is_asymm β s] (f : α → β) (H) : (@of_monotone _ _ r s _ _ f H : α → β) = f := rfl -- If le is preserved by an order embedding of preorders, then lt is too def lt_embedding_of_le_embedding [preorder α] [preorder β] (f : (has_le.le : α → α → Prop) ≼o (has_le.le : β → β → Prop)) : (has_lt.lt : α → α → Prop) ≼o (has_lt.lt : β → β → Prop) := { to_fun := f, inj := f.inj, ord := by intros; simp [lt_iff_le_not_le,f.ord] } theorem nat_lt [is_strict_order α r] (f : ℕ → α) (H : ∀ n:ℕ, r (f n) (f (n+1))) : ((<) : ℕ → ℕ → Prop) ≼o r := of_monotone f $ λ a b h, begin induction b with b IH, {exact (nat.not_lt_zero _ h).elim}, cases nat.lt_succ_iff_lt_or_eq.1 h with h e, { exact trans (IH h) (H _) }, { subst b, apply H } end theorem nat_gt [is_strict_order α r] (f : ℕ → α) (H : ∀ n:ℕ, r (f (n+1)) (f n)) : ((>) : ℕ → ℕ → Prop) ≼o r := by haveI := is_strict_order.swap r; exact rsymm (nat_lt f H) theorem well_founded_iff_no_descending_seq [is_strict_order α r] : well_founded r ↔ ¬ nonempty (((>) : ℕ → ℕ → Prop) ≼o r) := ⟨λ ⟨h⟩ ⟨⟨f, o⟩⟩, suffices ∀ a, acc r a → ∀ n, a ≠ f n, from this (f 0) (h _) 0 rfl, λ a ac, begin induction ac with a _ IH, intros n h, subst a, exact IH (f (n+1)) (o.1 (nat.lt_succ_self _)) _ rfl end, λ N, ⟨λ a, classical.by_contradiction $ λ na, let ⟨f, h⟩ := classical.axiom_of_choice $ show ∀ x : {a // ¬ acc r a}, ∃ y : {a // ¬ acc r a}, r y.1 x.1, from λ ⟨x, h⟩, classical.by_contradiction $ λ hn, h $ ⟨_, λ y h, classical.by_contradiction $ λ na, hn ⟨⟨y, na⟩, h⟩⟩ in N ⟨nat_gt (λ n, (f^[n] ⟨a, na⟩).1) $ λ n, by rw nat.iterate_succ'; apply h⟩⟩⟩ end order_embedding /-- The inclusion map `fin n → ℕ` is an order embedding. -/ def fin.val.order_embedding (n) : @order_embedding (fin n) ℕ (<) (<) := ⟨⟨fin.val, @fin.eq_of_veq _⟩, λ a b, iff.rfl⟩ /-- The inclusion map `fin m → fin n` is an order embedding. -/ def fin_fin.order_embedding {m n} (h : m ≤ n) : @order_embedding (fin m) (fin n) (<) (<) := ⟨⟨λ ⟨x, h'⟩, ⟨x, lt_of_lt_of_le h' h⟩, λ ⟨a, _⟩ ⟨b, _⟩ h, by congr; injection h⟩, by intros; cases a; cases b; refl⟩ instance fin.lt.is_well_order (n) : is_well_order (fin n) (<) := (fin.val.order_embedding _).is_well_order /-- An order isomorphism is an equivalence that is also an order embedding. -/ structure order_iso {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ≃ β := (ord : ∀ {a b}, r a b ↔ s (to_equiv a) (to_equiv b)) infix ` ≃o `:25 := order_iso namespace order_iso def to_order_embedding (f : r ≃o s) : r ≼o s := ⟨f.to_equiv.to_embedding, f.ord⟩ instance : has_coe (r ≃o s) (r ≼o s) := ⟨to_order_embedding⟩ theorem coe_coe_fn (f : r ≃o s) : ((f : r ≼o s) : α → β) = f := rfl @[simp] lemma to_equiv_to_fun (f : r ≃o s) (x : α) : f.to_equiv.to_fun x = f x := rfl theorem ord' : ∀ (f : r ≃o s) {a b}, r a b ↔ s (f a) (f b) | ⟨f, o⟩ := @o lemma ord'' {r : α → α → Prop} {s : β → β → Prop} (f : r ≃o s) {x y : α} : r x y ↔ s ((↑f : r ≼o s) x) ((↑f : r ≼o s) y) := f.ord' @[simp] theorem coe_fn_mk (f : α ≃ β) (o) : (@order_iso.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_equiv (f : r ≃o s) : (f.to_equiv : α → β) = f := rfl theorem eq_of_to_fun_eq : ∀ {e₁ e₂ : r ≃o s}, (e₁ : α → β) = e₂ → e₁ = e₂ | ⟨e₁, o₁⟩ ⟨e₂, o₂⟩ h := by congr; exact equiv.eq_of_to_fun_eq h @[refl] protected def refl (r : α → α → Prop) : r ≃o r := ⟨equiv.refl _, λ a b, iff.rfl⟩ @[symm] protected def symm (f : r ≃o s) : s ≃o r := ⟨f.to_equiv.symm, λ a b, by cases f with f o; rw o; simp⟩ @[trans] protected def trans (f₁ : r ≃o s) (f₂ : s ≃o t) : r ≃o t := ⟨f₁.to_equiv.trans f₂.to_equiv, λ a b, by cases f₁ with f₁ o₁; cases f₂ with f₂ o₂; rw [o₁, o₂]; simp⟩ @[simp] theorem coe_fn_symm_mk (f o) : ((@order_iso.mk _ _ r s f o).symm : β → α) = f.symm := rfl @[simp] theorem refl_apply (x : α) : order_iso.refl r x = x := rfl @[simp] theorem trans_apply : ∀ (f : r ≃o s) (g : s ≃o t) (a : α), (f.trans g) a = g (f a) | ⟨f₁, o₁⟩ ⟨f₂, o₂⟩ a := equiv.trans_apply _ _ _ @[simp] theorem apply_symm_apply : ∀ (e : r ≃o s) (x : β), e (e.symm x) = x | ⟨f₁, o₁⟩ x := by simp @[simp] theorem symm_apply_apply : ∀ (e : r ≃o s) (x : α), e.symm (e x) = x | ⟨f₁, o₁⟩ x := by simp /-- Any equivalence lifts to an order isomorphism between `s` and its preimage. -/ def preimage (f : α ≃ β) (s : β → β → Prop) : f ⁻¹'o s ≃o s := ⟨f, λ a b, iff.rfl⟩ noncomputable def of_surjective (f : r ≼o s) (H : surjective f) : r ≃o s := ⟨equiv.of_bijective ⟨f.inj, H⟩, by simp [f.ord']⟩ @[simp] theorem of_surjective_coe (f : r ≼o s) (H) : (of_surjective f H : α → β) = f := by delta of_surjective; simp theorem sum_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂} (e₁ : @order_iso α₁ α₂ r₁ r₂) (e₂ : @order_iso β₁ β₂ s₁ s₂) : sum.lex r₁ s₁ ≃o sum.lex r₂ s₂ := ⟨equiv.sum_congr e₁.to_equiv e₂.to_equiv, λ a b, by cases e₁ with f hf; cases e₂ with g hg; cases a; cases b; simp [hf, hg]⟩ theorem prod_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂} (e₁ : @order_iso α₁ α₂ r₁ r₂) (e₂ : @order_iso β₁ β₂ s₁ s₂) : prod.lex r₁ s₁ ≃o prod.lex r₂ s₂ := ⟨equiv.prod_congr e₁.to_equiv e₂.to_equiv, λ a b, begin cases e₁ with f hf; cases e₂ with g hg, cases a with a₁ a₂; cases b with b₁ b₂, suffices : prod.lex r₁ s₁ (a₁, a₂) (b₁, b₂) ↔ prod.lex r₂ s₂ (f a₁, g a₂) (f b₁, g b₂), {simpa [hf, hg]}, split, { intro h, cases h with _ _ _ _ h _ _ _ h, { left, exact hf.1 h }, { right, exact hg.1 h } }, { generalize e : f b₁ = fb₁, intro h, cases h with _ _ _ _ h _ _ _ h, { subst e, left, exact hf.2 h }, { have := f.injective e, subst b₁, right, exact hg.2 h } } end⟩ end order_iso /-- A subset `p : set α` embeds into `α` -/ def set_coe_embedding {α : Type*} (p : set α) : p ↪ α := ⟨subtype.val, @subtype.eq _ _⟩ /-- `subrel r p` is the inherited relation on a subset. -/ def subrel (r : α → α → Prop) (p : set α) : p → p → Prop := @subtype.val _ p ⁻¹'o r @[simp] theorem subrel_val (r : α → α → Prop) (p : set α) {a b} : subrel r p a b ↔ r a.1 b.1 := iff.rfl namespace subrel protected def order_embedding (r : α → α → Prop) (p : set α) : subrel r p ≼o r := ⟨set_coe_embedding _, λ a b, iff.rfl⟩ @[simp] theorem order_embedding_apply (r : α → α → Prop) (p a) : subrel.order_embedding r p a = a.1 := rfl instance (r : α → α → Prop) [is_well_order α r] (p : set α) : is_well_order p (subrel r p) := order_embedding.is_well_order (subrel.order_embedding r p) end subrel /-- Restrict the codomain of an order embedding -/ def order_embedding.cod_restrict (p : set β) (f : r ≼o s) (H : ∀ a, f a ∈ p) : r ≼o subrel s p := ⟨f.to_embedding.cod_restrict p H, f.ord⟩ @[simp] theorem order_embedding.cod_restrict_apply (p) (f : r ≼o s) (H a) : order_embedding.cod_restrict p f H a = ⟨f a, H a⟩ := rfl
f136373ddd18b6eba5f9a4554e90b3052dc68a66
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/analysis/convex/caratheodory.lean
0d661ed37320449299ea72fb35f4f810f6d621b1
[ "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
8,887
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Scott Morrison -/ import analysis.convex.basic import linear_algebra.finite_dimensional /-! # Carathéodory's convexity theorem This file is devoted to proving Carathéodory's convexity theorem: The convex hull of a set `s` in ℝᵈ is the union of the convex hulls of the (d+1)-tuples in `s`. ## Main results: * `convex_hull_eq_union`: Carathéodory's convexity theorem ## Implementation details This theorem was formalized as part of the Sphere Eversion project. ## Tags convex hull, caratheodory -/ universes u open set finset finite_dimensional open_locale big_operators variables {E : Type u} [add_comm_group E] [vector_space ℝ E] [finite_dimensional ℝ E] namespace caratheodory /-- If `x` is in the convex hull of some finset `t` with strictly more than `findim + 1` elements, then it is in the union of the convex hulls of the finsets `t.erase y` for `y ∈ t`. -/ lemma mem_convex_hull_erase [decidable_eq E] {t : finset E} (h : findim ℝ E + 1 < t.card) {x : E} (m : x ∈ convex_hull (↑t : set E)) : ∃ (y : (↑t : set E)), x ∈ convex_hull (↑(t.erase y) : set E) := begin simp only [finset.convex_hull_eq, mem_set_of_eq] at m ⊢, obtain ⟨f, fpos, fsum, rfl⟩ := m, obtain ⟨g, gcombo, gsum, gpos⟩ := exists_relation_sum_zero_pos_coefficient_of_dim_succ_lt_card h, clear h, let s := t.filter (λ z : E, 0 < g z), obtain ⟨i₀, mem, w⟩ : ∃ i₀ ∈ s, ∀ i ∈ s, f i₀ / g i₀ ≤ f i / g i, { apply s.exists_min_image (λ z, f z / g z), obtain ⟨x, hx, hgx⟩ : ∃ x ∈ t, 0 < g x := gpos, exact ⟨x, mem_filter.mpr ⟨hx, hgx⟩⟩, }, have hg : 0 < g i₀ := by { rw mem_filter at mem, exact mem.2 }, have hi₀ : i₀ ∈ t := filter_subset _ _ mem, let k : E → ℝ := λ z, f z - (f i₀ / g i₀) * g z, have hk : k i₀ = 0 := by field_simp [k, ne_of_gt hg], have ksum : ∑ e in t.erase i₀, k e = 1, { calc ∑ e in t.erase i₀, k e = ∑ e in t, k e : by conv_rhs { rw [← insert_erase hi₀, sum_insert (not_mem_erase i₀ t), hk, zero_add], } ... = ∑ e in t, (f e - f i₀ / g i₀ * g e) : rfl ... = 1 : by rw [sum_sub_distrib, fsum, ← mul_sum, gsum, mul_zero, sub_zero] }, refine ⟨⟨i₀, hi₀⟩, k, _, ksum, _⟩, { simp only [and_imp, sub_nonneg, mem_erase, ne.def, subtype.coe_mk], intros e hei₀ het, by_cases hes : e ∈ s, { have hge : 0 < g e := by { rw mem_filter at hes, exact hes.2 }, rw ← le_div_iff hge, exact w _ hes, }, { calc _ ≤ 0 : mul_nonpos_of_nonneg_of_nonpos _ _ -- prove two goals below ... ≤ f e : fpos e het, { apply div_nonneg (fpos i₀ (mem_of_subset (filter_subset _ t) mem)) (le_of_lt hg) }, { simpa only [mem_filter, het, true_and, not_lt] using hes }, } }, { simp only [subtype.coe_mk, center_mass_eq_of_sum_1 _ id ksum, id], calc ∑ e in t.erase i₀, k e • e = ∑ e in t, k e • e : sum_erase _ (by rw [hk, zero_smul]) ... = ∑ e in t, (f e - f i₀ / g i₀ * g e) • e : rfl ... = t.center_mass f id : _, simp only [sub_smul, mul_smul, sum_sub_distrib, ← smul_sum, gcombo, smul_zero, sub_zero, center_mass, fsum, inv_one, one_smul, id.def], }, end /-- The convex hull of a finset `t` with `findim ℝ E + 1 < t.card` is equal to the union of the convex hulls of the finsets `t.erase x` for `x ∈ t`. -/ lemma step [decidable_eq E] (t : finset E) (h : findim ℝ E + 1 < t.card) : convex_hull (↑t : set E) = ⋃ (x : (↑t : set E)), convex_hull ↑(t.erase x) := begin apply set.subset.antisymm, { intros x m', obtain ⟨y, m⟩ := mem_convex_hull_erase h m', exact mem_Union.2 ⟨y, m⟩, }, { refine Union_subset _, intro x, apply convex_hull_mono, apply erase_subset, } end /-- The convex hull of a finset `t` with `findim ℝ E + 1 < t.card` is contained in the union of the convex hulls of the finsets `t' ⊆ t` with `t'.card ≤ findim ℝ E + 1`. -/ lemma shrink' (t : finset E) (k : ℕ) (h : t.card = findim ℝ E + 1 + k) : convex_hull (↑t : set E) ⊆ ⋃ (t' : finset E) (w : t' ⊆ t) (b : t'.card ≤ findim ℝ E + 1), convex_hull ↑t' := begin induction k with k ih generalizing t, { apply subset_subset_Union t, apply subset_subset_Union (set.subset.refl _), exact subset_subset_Union (le_of_eq h) (subset.refl _), }, { classical, rw step _ (by { rw h, simp, } : findim ℝ E + 1 < t.card), apply Union_subset, intro i, transitivity, { apply ih, rw [card_erase_of_mem, h, nat.pred_succ], exact i.2, }, { apply Union_subset_Union, intro t', apply Union_subset_Union_const, exact λ h, set.subset.trans h (erase_subset _ _), } } end /-- The convex hull of any finset `t` is contained in the union of the convex hulls of the finsets `t' ⊆ t` with `t'.card ≤ findim ℝ E + 1`. -/ lemma shrink (t : finset E) : convex_hull (↑t : set E) ⊆ ⋃ (t' : finset E) (w : t' ⊆ t) (b : t'.card ≤ findim ℝ E + 1), convex_hull ↑t' := begin by_cases h : t.card ≤ findim ℝ E + 1, { apply subset_subset_Union t, apply subset_subset_Union (set.subset.refl _), exact subset_subset_Union h (set.subset.refl _), }, push_neg at h, obtain ⟨k, w⟩ := le_iff_exists_add.mp (le_of_lt h), clear h, exact shrink' _ _ w, end end caratheodory /-- One inclusion of Carathéodory's convexity theorem. The convex hull of a set `s` in ℝᵈ is contained in the union of the convex hulls of the (d+1)-tuples in `s`. -/ lemma convex_hull_subset_union (s : set E) : convex_hull s ⊆ ⋃ (t : finset E) (w : ↑t ⊆ s) (b : t.card ≤ findim ℝ E + 1), convex_hull ↑t := begin -- First we replace `convex_hull s` with the union of the convex hulls of finite subsets, rw convex_hull_eq_union_convex_hull_finite_subsets, -- and prove the inclusion for each of those. apply Union_subset, intro r, apply Union_subset, intro h, -- Second, for each convex hull of a finite subset, we shrink it. refine subset.trans (caratheodory.shrink _) _, -- After that it's just shuffling unions around. refine Union_subset_Union (λ t, _), exact Union_subset_Union2 (λ htr, ⟨subset.trans htr h, subset.refl _⟩) end /-- Carathéodory's convexity theorem. The convex hull of a set `s` in ℝᵈ is the union of the convex hulls of the (d+1)-tuples in `s`. -/ theorem convex_hull_eq_union (s : set E) : convex_hull s = ⋃ (t : finset E) (w : ↑t ⊆ s) (b : t.card ≤ findim ℝ E + 1), convex_hull ↑t := begin apply set.subset.antisymm, { apply convex_hull_subset_union, }, iterate 3 { convert Union_subset _, intro, }, exact convex_hull_mono ‹_›, end /-- A more explicit formulation of Carathéodory's convexity theorem, writing an element of a convex hull as the center of mass of an explicit `finset` with cardinality at most `dim + 1`. -/ theorem eq_center_mass_card_le_dim_succ_of_mem_convex_hull {s : set E} {x : E} (h : x ∈ convex_hull s) : ∃ (t : finset E) (w : ↑t ⊆ s) (b : t.card ≤ findim ℝ E + 1) (f : E → ℝ), (∀ y ∈ t, 0 ≤ f y) ∧ t.sum f = 1 ∧ t.center_mass f id = x := begin rw convex_hull_eq_union at h, simp only [exists_prop, mem_Union] at h, obtain ⟨t, w, b, m⟩ := h, refine ⟨t, w, b, _⟩, rw finset.convex_hull_eq at m, simpa only [exists_prop] using m, end /-- A variation on Carathéodory's convexity theorem, writing an element of a convex hull as a center of mass of an explicit `finset` with cardinality at most `dim + 1`, where all coefficients in the center of mass formula are strictly positive. (This is proved using `eq_center_mass_card_le_dim_succ_of_mem_convex_hull`, and discarding any elements of the set with coefficient zero.) -/ theorem eq_pos_center_mass_card_le_dim_succ_of_mem_convex_hull {s : set E} {x : E} (h : x ∈ convex_hull s) : ∃ (t : finset E) (w : ↑t ⊆ s) (b : t.card ≤ findim ℝ E + 1) (f : E → ℝ), (∀ y ∈ t, 0 < f y) ∧ t.sum f = 1 ∧ t.center_mass f id = x := begin obtain ⟨t, w, b, f, ⟨pos, sum, center⟩⟩ := eq_center_mass_card_le_dim_succ_of_mem_convex_hull h, let t' := t.filter (λ z, 0 < f z), have t'sum : t'.sum f = 1, { rw ← sum, exact sum_filter_of_ne (λ x hxt hfx, (pos x hxt).lt_of_ne hfx.symm) }, refine ⟨t', _, _, f, ⟨_, t'sum, _⟩⟩, { exact subset.trans (filter_subset _ t) w, }, { exact (card_filter_le _ _).trans b, }, { exact λ y H, (mem_filter.mp H).right, }, { rw ← center, simp only [center_mass, t'sum, sum, inv_one, one_smul, id.def], refine sum_filter_of_ne (λ x hxt hfx, (pos x hxt).lt_of_ne $ λ hf₀, _), rw [← hf₀, zero_smul] at hfx, exact hfx rfl }, end
a7986abdd6602b456451de06468585851506e129
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/topology/omega_complete_partial_order.lean
c3aba31d2d291a4bcaa1dcd25d2ed0fee11d4798
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,524
lean
/- Copyright (c) 2020 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import topology.basic import order.omega_complete_partial_order /-! # Scott Topological Spaces A type of topological spaces whose notion of continuity is equivalent to continuity in ωCPOs. ## Reference * https://ncatlab.org/nlab/show/Scott+topology -/ open omega_complete_partial_order open_locale classical universes u namespace Scott /-- `x` is an `ω`-Sup of a chain `c` if it is the least upper bound of the range of `c`. -/ def is_ωSup {α : Type u} [preorder α] (c : chain α) (x : α) : Prop := (∀ i, c i ≤ x) ∧ (∀ y, (∀ i, c i ≤ y) → x ≤ y) lemma is_ωSup_iff_is_lub {α : Type u} [preorder α] {c : chain α} {x : α} : is_ωSup c x ↔ is_lub (set.range c) x := by simp [is_ωSup, is_lub, is_least, upper_bounds, lower_bounds] variables (α : Type u) [omega_complete_partial_order α] local attribute [irreducible] set /-- The characteristic function of open sets is monotone and preserves the limits of chains. -/ def is_open (s : set α) : Prop := continuous' (λ x, x ∈ s) theorem is_open_univ : is_open α set.univ := ⟨λ x y h, by simp only [set.mem_univ]; refl', by convert @complete_lattice.top_continuous α Prop _ _; ext; simp ⟩ theorem is_open.inter (s t : set α) : is_open α s → is_open α t → is_open α (s ∩ t) := begin simp only [is_open, exists_imp_distrib, continuous'], intros h₀ h₁ h₂ h₃, rw ← set.inf_eq_inter, let s' : α →ₘ Prop := ⟨λ x, x ∈ s, h₀⟩, let t' : α →ₘ Prop := ⟨λ x, x ∈ t, h₂⟩, split, { change omega_complete_partial_order.continuous (s' ⊓ t'), haveI : is_total Prop (≤) := ⟨ @le_total Prop _ ⟩, apply complete_lattice.inf_continuous; assumption }, { intros x y h, apply and_implies; solve_by_elim [h₀ h, h₂ h], } end theorem is_open_sUnion : ∀s, (∀t∈s, is_open α t) → is_open α (⋃₀ s) := begin introv h₀, suffices : is_open α ({ x | Sup (flip (∈) '' s) x }), { convert this, ext, simp only [set.sUnion, Sup, set.mem_image, set.mem_set_of_eq, supr, conditionally_complete_lattice.Sup, exists_exists_and_eq_and, complete_lattice.Sup, exists_prop, set.mem_range, set_coe.exists, eq_iff_iff, subtype.coe_mk], tauto, }, dsimp [is_open] at *, apply complete_lattice.Sup_continuous' _, introv ht, specialize h₀ { x | f x } _, { simp only [flip, set.mem_image] at *, rcases ht with ⟨x,h₀,h₁⟩, subst h₁, simpa, }, { simpa using h₀ } end end Scott /-- A Scott topological space is defined on preorders such that their open sets, seen as a function `α → Prop`, preserves the joins of ω-chains -/ @[reducible] def Scott (α : Type u) := α instance Scott.topological_space (α : Type u) [omega_complete_partial_order α] : topological_space (Scott α) := { is_open := Scott.is_open α, is_open_univ := Scott.is_open_univ α, is_open_inter := Scott.is_open.inter α, is_open_sUnion := Scott.is_open_sUnion α } section not_below variables {α : Type*} [omega_complete_partial_order α] (y : Scott α) /-- `not_below` is an open set in `Scott α` used to prove the monotonicity of continuous functions -/ def not_below := { x | ¬ x ≤ y } lemma not_below_is_open : is_open (not_below y) := begin have h : monotone (not_below y), { intros x y' h, simp only [not_below, set_of, le_Prop_eq], intros h₀ h₁, apply h₀ (le_trans h h₁) }, existsi h, rintros c, apply eq_of_forall_ge_iff, intro z, rw ωSup_le_iff, simp only [ωSup_le_iff, not_below, set.mem_set_of_eq, le_Prop_eq, preorder_hom.coe_fun_mk, chain.map_coe, function.comp_app, exists_imp_distrib, not_forall], end end not_below open Scott (hiding is_open) open omega_complete_partial_order lemma is_ωSup_ωSup {α} [omega_complete_partial_order α] (c : chain α) : is_ωSup c (ωSup c) := begin split, { apply le_ωSup, }, { apply ωSup_le, }, end lemma Scott_continuous_of_continuous {α β} [omega_complete_partial_order α] [omega_complete_partial_order β] (f : Scott α → Scott β) (hf : continuous f) : omega_complete_partial_order.continuous' f := begin simp only [continuous_def, (⁻¹')] at hf, have h : monotone f, { intros x y h, cases (hf {x | ¬ x ≤ f y} (not_below_is_open _)) with hf hf', clear hf', specialize hf h, simp only [set.preimage, set_of, (∈), set.mem, le_Prop_eq] at hf, by_contradiction H, apply hf H (le_refl (f y)) }, existsi h, intro c, apply eq_of_forall_ge_iff, intro z, specialize (hf _ (not_below_is_open z)), cases hf, specialize hf_h c, simp only [not_below, preorder_hom.coe_fun_mk, eq_iff_iff, set.mem_set_of_eq] at hf_h, rw [← not_iff_not], simp only [ωSup_le_iff, hf_h, ωSup, supr, Sup, complete_lattice.Sup, complete_semilattice_Sup.Sup, exists_prop, set.mem_range, preorder_hom.coe_fun_mk, chain.map_coe, function.comp_app, eq_iff_iff, not_forall], tauto, end lemma continuous_of_Scott_continuous {α β} [omega_complete_partial_order α] [omega_complete_partial_order β] (f : Scott α → Scott β) (hf : omega_complete_partial_order.continuous' f) : continuous f := begin rw continuous_def, intros s hs, change continuous' (s ∘ f), cases hs with hs hs', cases hf with hf hf', apply continuous.of_bundled, apply continuous_comp _ _ hf' hs', end
b0d9e8d468132c580a4df93cdabb74e239fc511f
92b50235facfbc08dfe7f334827d47281471333b
/library/logic/quantifiers.lean
a1a83be0b173c0eb11bb809f7f2db390e027a1e5
[ "Apache-2.0" ]
permissive
htzh/lean
24f6ed7510ab637379ec31af406d12584d31792c
d70c79f4e30aafecdfc4a60b5d3512199200ab6e
refs/heads/master
1,607,677,731,270
1,437,089,952,000
1,437,089,952,000
37,078,816
0
0
null
1,433,780,956,000
1,433,780,955,000
null
UTF-8
Lean
false
false
4,528
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 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} (H : ∀x, φ x ↔ ψ x) : (∀x, φ x) ↔ (∀x, ψ x) := iff.intro (assume Hl, take x, iff.elim_left (H x) (Hl x)) (assume Hr, take x, iff.elim_right (H x) (Hr x)) theorem exists_congr {A : Type} {φ ψ : A → Prop} (H : ∀x, φ x ↔ ψ x) : (∃x, φ x) ↔ (∃x, ψ x) := iff.intro (assume Hex, obtain w Pw, from Hex, exists.intro w (iff.elim_left (H w) Pw)) (assume Hex, obtain w Qw, from Hex, exists.intro w (iff.elim_right (H w) Qw)) theorem forall_true_iff_true (A : Type) : (∀x : A, true) ↔ true := iff.intro (assume H, trivial) (assume H, take x, trivial) theorem forall_p_iff_p (A : Type) [H : inhabited A] (p : Prop) : (∀x : A, p) ↔ p := iff.intro (assume Hl, inhabited.destruct H (take x, Hl x)) (assume Hr, take x, Hr) theorem exists_p_iff_p (A : Type) [H : inhabited A] (p : Prop) : (∃x : A, p) ↔ p := iff.intro (assume Hl, obtain a Hp, from Hl, Hp) (assume Hr, inhabited.destruct H (take a, exists.intro a Hr)) theorem forall_and_distribute {A : Type} (φ ψ : A → Prop) : (∀x, φ x ∧ ψ x) ↔ (∀x, φ x) ∧ (∀x, ψ x) := iff.intro (assume H, and.intro (take x, and.elim_left (H x)) (take x, and.elim_right (H x))) (assume H, take x, and.intro (and.elim_left H x) (and.elim_right H x)) theorem exists_or_distribute {A : Type} (φ ψ : A → Prop) : (∃x, φ x ∨ ψ x) ↔ (∃x, φ x) ∨ (∃x, ψ x) := iff.intro (assume H, obtain (w : A) (Hw : φ w ∨ ψ w), from H, or.elim Hw (assume Hw1 : φ w, or.inl (exists.intro w Hw1)) (assume Hw2 : ψ w, or.inr (exists.intro w Hw2))) (assume H, or.elim H (assume H1, obtain (w : A) (Hw : φ w), from H1, exists.intro w (or.inl Hw)) (assume H2, obtain (w : A) (Hw : ψ w), from H2, exists.intro w (or.inr Hw))) theorem nonempty_of_exists {A : Type} {P : A → Prop} (H : ∃x, P x) : nonempty A := obtain w Hw, from H, nonempty.intro w 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) := decidable.rec_on H (λ pa, inl (λ x heq, eq.rec_on (eq.rec_on heq rfl) pa)) (λ npa, inr (λ h, absurd (h a rfl) npa)) definition decidable_exists_eq [instance] : decidable (∃ x, x = a ∧ P x) := decidable.rec_on H (λ pa, inl (exists.intro a (and.intro rfl pa))) (λ npa, inr (λ h, obtain (w : A) (Hw : w = a ∧ P w), from h, absurd (and.rec_on Hw (λ h₁ h₂, h₁ ▸ h₂)) npa)) 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.elim_left Hw) (and.elim_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) := iff.intro (assume H', take x, iff.mp (H x) (H' x)) (assume H', take x, iff.mp' (H x) (H' x)) theorem congr_exists : (∃x, p₁ x) ↔ (∃x, p₂ x) := iff.intro (assume H', exists.elim H' (λ x H₁, exists.intro x (iff.mp (H x) H₁))) (assume H', exists.elim H' (λ x H₁, exists.intro x (iff.mp' (H x) H₁))) include H theorem congr_exists_unique : (∃!x, p₁ x) ↔ (∃!x, p₂ x) := begin apply congr_exists, intro x, apply congr_and (H x), apply congr_forall, intro y, apply congr_imp (H y) !iff.rfl end end
8fa72a706eae9359b217b3b069b195235cea385b
93366ecea09eebeeb0b320567c6f71715434c3f0
/src/bum/types.lean
34aff10ce69912dd0a5ee24a931d37ba8deb707e
[ "LicenseRef-scancode-mit-taylor-variant", "LicenseRef-scancode-warranty-disclaimer" ]
permissive
o89/bum
9c1286cedb878c1e14c618e81b33d365f62c8a8a
18ffb2b46932ba767c50758a93cc99846ef1b46a
refs/heads/master
1,670,171,693,018
1,669,086,393,000
1,669,086,393,000
205,409,352
8
6
null
null
null
null
UTF-8
Lean
false
false
5,359
lean
import bum.auxiliary open IO.Process (SpawnArgs) open System (FilePath) inductive Application | zero | n2o | nitro def Application.ofString : String → Except String Application | "zero" => Except.ok Application.zero | "n2o" => Except.ok Application.n2o | "nitro" => Except.ok Application.nitro | s => Except.error ("unknown template “" ++ s ++"”") inductive Scale | current | total instance : Inhabited Scale := ⟨Scale.current⟩ inductive Command | app : Application → Command | compile : Bool → Command | clean : Scale → Command | olean : Scale → Bool → Command | help | nope | deps | start | leanPath | gitignore protected def Command.reserved := [ "compile", "start", "dep", "clean", "olean", "app", "lean-path", "gitignore" ] protected def Command.groupAux : String × List String → List (String × List String) → List String → List (String × List String) | ("", []), [], [] => [] | (name, options), buf, hd :: tl => if Command.reserved.elem hd then Command.groupAux (hd, []) (buf ++ [ (name, options) ]) tl else Command.groupAux (name, options ++ [ hd ]) buf tl | elem, buf, [] => buf ++ [ elem ] protected def Command.group := Command.groupAux ("", []) [] protected def Command.ofString : String × List String → Except String Command | ("compile", []) => Command.compile false | ("compile", ["force"]) => Command.compile true | ("lean-path", []) => Command.leanPath | ("gitignore", []) => Command.gitignore | ("start", []) => Command.start | ("dep", []) => Command.deps | ("clean", []) => Command.clean Scale.current | ("clean", ["recur"]) => Command.clean Scale.total | ("olean", keys) => if ¬ keys.all (List.contains ["recur", "force"]) then throw s!"olean expects only “recur” and “force” flags" else let scale := if keys.contains "recur" then Scale.total else Scale.current let force := keys.contains "force" Command.olean scale force | ("app", [ template ]) => Command.app <$> Application.ofString template | ("", _) => Command.nope | (cmd, xs) => throw s!"unknown or malformed command “{cmd} {xs.space}”" protected def Command.ofList : List String → Except String (List Command) := List.mapM Command.ofString ∘ Command.group def Command.parse : List String → Except String (List Command) | [] => pure [] | lst@(hd :: _) => if Command.reserved.elem hd then Command.ofList lst else throw s!"“{hd}” is not a registered command" def Command.helpString := "BUM Lean 4 build tool invoke = bum | bum list | bum lean-path | bum gitignore list = [] | command [options] list command = app (zero|n2o|nitro) | dep | olean [recur] [force] | compile [force] | clean [recur] | start" inductive Repo | github : String → Repo | git : String → Repo | none -- TODO: remove hard-coded GitHub def Application.toRepo : Application → Repo | Application.zero => Repo.github "o89/sample-zero" | Application.n2o => Repo.github "o89/sample-n2o" | Application.nitro => Repo.github "o89/sample-nitro" def Repo.cmd (target : String) : Repo → SpawnArgs | Repo.none => { cmd := "echo", args := #["-n"] } | Repo.git url => { cmd := "git", args := #["clone", url, target] } | Repo.github repo => { cmd := "git", args := #["clone", s!"https://github.com/{repo}", target ] } structure Dep := (name : String) (source : Repo) def sep := toString FilePath.pathSeparator def Dep.cmd (depsDir : FilePath) (x : Dep) : SpawnArgs := Repo.cmd (toString depsDir ++ sep ++ x.name) x.source instance : ToString Dep := ⟨λ s => match s with | ⟨name, Repo.none⟩ => name | ⟨name, Repo.github url⟩ => name ++ " github \"" ++ url ++ "\"" | ⟨name, Repo.git url⟩ => name ++ " git \"" ++ url ++ "\""⟩ inductive BuildType | executable | library inductive Source | lean : String → Source | cpp : String → Source def Source.cpp? : Source → Bool | Source.lean _ => false | Source.cpp _ => true def file (name ext : String) : FilePath := FilePath.mk (name ++ ext) def Source.path : Source → FilePath | Source.lean path => file path ".lean" | Source.cpp path => file path ".cpp" def Source.subst (ext : String) : Source → FilePath | Source.lean path => file path ext | Source.cpp path => file path ext def Source.asOlean := Source.subst ".olean" def Source.asCpp := Source.subst ".cpp" def Source.obj := Source.subst ".o" def Source.garbage : Source → List FilePath | Source.lean path => [ file path ".olean", file path ".cpp", file path ".o" ] | Source.cpp path => [ file path ".o" ] structure Project := (build : BuildType) (name : String) (files : List Source) (deps : List Dep) (depsDir : FilePath) (srcDir : FilePath) (cppLibs : List String) (cppFlags : List String) def Project.getBinary (conf : Project) : FilePath := match conf.build with | BuildType.executable => FilePath.mk conf.name | BuildType.library => file ("lib" ++ conf.name) ".a" inductive Val | string : String → Val | list : List Val → Val | pair : Val × Val → Val def Val.getString : Val → Except String String | Val.string v => Except.ok v | _ => Except.error "expected string" structure Tools := (leanHome lean ar cpp : String)
a143d2351bd6711136b0bd10bcc58d6270534a75
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/limits/preserves/shapes/terminal.lean
d7751ce13d9f679803b1b06d594fccdc7c63534e
[ "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
4,163
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import category_theory.limits.preserves.limits import category_theory.limits.shapes /-! # Preserving terminal object Constructions to relate the notions of preserving terminal objects and reflecting terminal objects to concrete objects. In particular, we show that `terminal_comparison G` is an isomorphism iff `G` preserves terminal objects. -/ universes v u₁ u₂ noncomputable theory open category_theory category_theory.category category_theory.limits variables {C : Type u₁} [category.{v} C] variables {D : Type u₂} [category.{v} D] variables (G : C ⥤ D) namespace category_theory.limits variables (X : C) /-- The map of an empty cone is a limit iff the mapped object is terminal. -/ def is_limit_map_cone_empty_cone_equiv : is_limit (G.map_cone (as_empty_cone X)) ≃ is_terminal (G.obj X) := (is_limit.postcompose_hom_equiv (functor.empty_ext _ _) _).symm.trans (is_limit.equiv_iso_limit (cones.ext (iso.refl _) (by tidy))) /-- The property of preserving terminal objects expressed in terms of `is_terminal`. -/ def is_terminal_obj_of_is_terminal [preserves_limit (functor.empty C) G] (l : is_terminal X) : is_terminal (G.obj X) := is_limit_map_cone_empty_cone_equiv G X (preserves_limit.preserves l) /-- The property of reflecting terminal objects expressed in terms of `is_terminal`. -/ def is_terminal_of_is_terminal_obj [reflects_limit (functor.empty C) G] (l : is_terminal (G.obj X)) : is_terminal X := reflects_limit.reflects ((is_limit_map_cone_empty_cone_equiv G X).symm l) variables [has_terminal C] /-- If `G` preserves the terminal object and `C` has a terminal object, then the image of the terminal object is terminal. -/ def is_limit_of_has_terminal_of_preserves_limit [preserves_limit (functor.empty C) G] : is_terminal (G.obj (⊤_ C)) := is_terminal_obj_of_is_terminal G (⊤_ C) terminal_is_terminal /-- If `C` has a terminal object and `G` preserves terminal objects, then `D` has a terminal object also. Note this property is somewhat unique to (co)limits of the empty diagram: for general `J`, if `C` has limits of shape `J` and `G` preserves them, then `D` does not necessarily have limits of shape `J`. -/ lemma has_terminal_of_has_terminal_of_preserves_limit [preserves_limit (functor.empty C) G] : has_terminal D := ⟨λ F, begin haveI := has_limit.mk ⟨_, is_limit_of_has_terminal_of_preserves_limit G⟩, apply has_limit_of_iso F.unique_from_empty.symm, end⟩ variable [has_terminal D] /-- If the terminal comparison map for `G` is an isomorphism, then `G` preserves terminal objects. -/ def preserves_terminal.of_iso_comparison [i : is_iso (terminal_comparison G)] : preserves_limit (functor.empty C) G := begin apply preserves_limit_of_preserves_limit_cone terminal_is_terminal, apply (is_limit_map_cone_empty_cone_equiv _ _).symm _, apply is_limit.of_point_iso (limit.is_limit (functor.empty D)), apply i, end /-- If there is any isomorphism `G.obj ⊤ ⟶ ⊤`, then `G` preserves terminal objects. -/ def preserves_terminal_of_is_iso (f : G.obj (⊤_ C) ⟶ ⊤_ D) [i : is_iso f] : preserves_limit (functor.empty C) G := begin rw subsingleton.elim f (terminal_comparison G) at i, exactI preserves_terminal.of_iso_comparison G, end /-- If there is any isomorphism `G.obj ⊤ ≅ ⊤`, then `G` preserves terminal objects. -/ def preserves_terminal_of_iso (f : G.obj (⊤_ C) ≅ ⊤_ D) : preserves_limit (functor.empty C) G := preserves_terminal_of_is_iso G f.hom variables [preserves_limit (functor.empty C) G] /-- If `G` preserves terminal objects, then the terminal comparison map for `G` an isomorphism. -/ def preserves_terminal.iso : G.obj (⊤_ C) ≅ ⊤_ D := (is_limit_of_has_terminal_of_preserves_limit G).cone_point_unique_up_to_iso (limit.is_limit _) @[simp] lemma preserves_terminal.iso_hom : (preserves_terminal.iso G).hom = terminal_comparison G := rfl instance : is_iso (terminal_comparison G) := begin rw ← preserves_terminal.iso_hom, apply_instance, end end category_theory.limits
4b110cdde6f28203914ee8dd945d6b2aa8b2cbfd
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/algebra/group/hom_instances.lean
92779bafc6e600575878ff13ece7f7e8581d1fb5
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,265
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Kevin Buzzard, Scott Morrison, Johan Commelin, Chris Hughes, Johannes Hölzl, Yury Kudryashov -/ import algebra.group_power.basic /-! # Instances on spaces of monoid and group morphisms We endow the space of monoid morphisms `M →* N` with a `comm_monoid` structure when the target is commutative, through pointwise multiplication, and with a `comm_group` structure when the target is a commutative group. We also prove the same instances for additive situations. Since these structures permit morphisms of morphisms, we also provide some composition-like operations. -/ universes uM uN uP uQ variables {M : Type uM} {N : Type uN} {P : Type uP} {Q : Type uQ} /-- `(M →* N)` is a `comm_monoid` if `N` is commutative. -/ instance [mul_one_class M] [comm_monoid N] : comm_monoid (M →* N) := { mul := (*), mul_assoc := by intros; ext; apply mul_assoc, one := 1, one_mul := by intros; ext; apply one_mul, mul_one := by intros; ext; apply mul_one, mul_comm := by intros; ext; apply mul_comm, npow := λ n f, { to_fun := λ x, npow n (f x), map_one' := by simp, map_mul' := λ x y, by simp [mul_pow] }, npow_zero' := λ f, by { ext x, simp }, npow_succ' := λ n f, by { ext x, simp [pow_succ] } } /-- `(M →+ N)` is an `add_comm_monoid` if `N` is commutative. -/ instance [add_zero_class M] [add_comm_monoid N] : add_comm_monoid (M →+ N) := { add := (+), add_assoc := by intros; ext; apply add_assoc, zero := 0, zero_add := by intros; ext; apply zero_add, add_zero := by intros; ext; apply add_zero, add_comm := by intros; ext; apply add_comm, nsmul := λ n f, { to_fun := λ x, nsmul n (f x), map_zero' := by simp [nsmul_zero], map_add' := λ x y, by simp [nsmul_add] }, nsmul_zero' := λ f, by { ext x, simp [zero_nsmul], }, nsmul_succ' := λ n f, by { ext x, simp [nat.succ_eq_add_one, add_comm, add_nsmul] } } attribute [to_additive] monoid_hom.comm_monoid /-- If `G` is a commutative group, then `M →* G` is a commutative group too. -/ instance {M G} [mul_one_class M] [comm_group G] : comm_group (M →* G) := { inv := has_inv.inv, div := has_div.div, div_eq_mul_inv := by { intros, ext, apply div_eq_mul_inv }, mul_left_inv := by intros; ext; apply mul_left_inv, gpow := λ n f, { to_fun := λ x, gpow n (f x), map_one' := by simp, map_mul' := λ x y, by simp [mul_gpow] }, gpow_zero' := λ f, by { ext x, simp }, gpow_succ' := λ n f, by { ext x, simp [gpow_of_nat, pow_succ] }, gpow_neg' := λ n f, by { ext x, simp }, ..monoid_hom.comm_monoid } /-- If `G` is an additive commutative group, then `M →+ G` is an additive commutative group too. -/ instance {M G} [add_zero_class M] [add_comm_group G] : add_comm_group (M →+ G) := { neg := has_neg.neg, sub := has_sub.sub, sub_eq_add_neg := by { intros, ext, apply sub_eq_add_neg }, add_left_neg := by intros; ext; apply add_left_neg, gsmul := λ n f, { to_fun := λ x, gsmul n (f x), map_zero' := by simp, map_add' := λ x y, by simp [gsmul_add] }, gsmul_zero' := λ f, by { ext x, simp }, gsmul_succ' := λ n f, by { ext x, simp [gsmul_of_nat, nat.succ_eq_add_one, add_comm, add_nsmul] }, gsmul_neg' := λ n f, by { ext x, simp }, ..add_monoid_hom.add_comm_monoid } attribute [to_additive] monoid_hom.comm_group /-! ### Morphisms of morphisms The structures above permit morphisms that themselves produce morphisms, provided the codomain is commutative. -/ namespace monoid_hom @[to_additive] lemma ext_iff₂ {mM : mul_one_class M} {mN : mul_one_class N} {mP : comm_monoid P} {f g : M →* N →* P} : f = g ↔ (∀ x y, f x y = g x y) := monoid_hom.ext_iff.trans $ forall_congr $ λ _, monoid_hom.ext_iff /-- `flip` arguments of `f : M →* N →* P` -/ @[to_additive "`flip` arguments of `f : M →+ N →+ P`"] def flip {mM : mul_one_class M} {mN : mul_one_class N} {mP : comm_monoid P} (f : M →* N →* P) : N →* M →* P := { to_fun := λ y, ⟨λ x, f x y, by rw [f.map_one, one_apply], λ x₁ x₂, by rw [f.map_mul, mul_apply]⟩, map_one' := ext $ λ x, (f x).map_one, map_mul' := λ y₁ y₂, ext $ λ x, (f x).map_mul y₁ y₂ } @[simp, to_additive] lemma flip_apply {mM : mul_one_class M} {mN : mul_one_class N} {mP : comm_monoid P} (f : M →* N →* P) (x : M) (y : N) : f.flip y x = f x y := rfl @[to_additive] lemma map_one₂ {mM : mul_one_class M} {mN : mul_one_class N} {mP : comm_monoid P} (f : M →* N →* P) (n : N) : f 1 n = 1 := (flip f n).map_one @[to_additive] lemma map_mul₂ {mM : mul_one_class M} {mN : mul_one_class N} {mP : comm_monoid P} (f : M →* N →* P) (m₁ m₂ : M) (n : N) : f (m₁ * m₂) n = f m₁ n * f m₂ n := (flip f n).map_mul _ _ @[to_additive] lemma map_inv₂ {mM : group M} {mN : mul_one_class N} {mP : comm_group P} (f : M →* N →* P) (m : M) (n : N) : f m⁻¹ n = (f m n)⁻¹ := (flip f n).map_inv _ @[to_additive] lemma map_div₂ {mM : group M} {mN : mul_one_class N} {mP : comm_group P} (f : M →* N →* P) (m₁ m₂ : M) (n : N) : f (m₁ / m₂) n = f m₁ n / f m₂ n := (flip f n).map_div _ _ /-- Evaluation of a `monoid_hom` at a point as a monoid homomorphism. See also `monoid_hom.apply` for the evaluation of any function at a point. -/ @[to_additive "Evaluation of an `add_monoid_hom` at a point as an additive monoid homomorphism. See also `add_monoid_hom.apply` for the evaluation of any function at a point.", simps] def eval [mul_one_class M] [comm_monoid N] : M →* (M →* N) →* N := (monoid_hom.id (M →* N)).flip /-- The expression `λ g m, g (f m)` as a `monoid_hom`. Equivalently, `(λ g, monoid_hom.comp g f)` as a `monoid_hom`. -/ @[to_additive "The expression `λ g m, g (f m)` as a `add_monoid_hom`. Equivalently, `(λ g, monoid_hom.comp g f)` as a `add_monoid_hom`. This also exists in a `linear_map` version, `linear_map.lcomp`.", simps] def comp_hom' [mul_one_class M] [mul_one_class N] [comm_monoid P] (f : M →* N) : (N →* P) →* M →* P := flip $ eval.comp f /-- Composition of monoid morphisms (`monoid_hom.comp`) as a monoid morphism. Note that unlike `monoid_hom.comp_hom'` this requires commutativity of `N`. -/ @[to_additive "Composition of additive monoid morphisms (`add_monoid_hom.comp`) as an additive monoid morphism. Note that unlike `add_monoid_hom.comp_hom'` this requires commutativity of `N`. This also exists in a `linear_map` version, `linear_map.llcomp`.", simps] def comp_hom [mul_one_class M] [comm_monoid N] [comm_monoid P] : (N →* P) →* (M →* N) →* (M →* P) := { to_fun := λ g, { to_fun := g.comp, map_one' := comp_one g, map_mul' := comp_mul g }, map_one' := by { ext1 f, exact one_comp f }, map_mul' := λ g₁ g₂, by { ext1 f, exact mul_comp g₁ g₂ f } } /-- Flipping arguments of monoid morphisms (`monoid_hom.flip`) as a monoid morphism. -/ @[to_additive "Flipping arguments of additive monoid morphisms (`add_monoid_hom.flip`) as an additive monoid morphism.", simps] def flip_hom {mM : mul_one_class M} {mN : mul_one_class N} {mP : comm_monoid P} : (M →* N →* P) →* (N →* M →* P) := { to_fun := monoid_hom.flip, map_one' := rfl, map_mul' := λ f g, rfl } /-- The expression `λ m q, f m (g q)` as a `monoid_hom`. Note that the expression `λ q n, f (g q) n` is simply `monoid_hom.comp`. -/ @[to_additive "The expression `λ m q, f m (g q)` as an `add_monoid_hom`. Note that the expression `λ q n, f (g q) n` is simply `add_monoid_hom.comp`. This also exists as a `linear_map` version, `linear_map.compl₂`"] def compl₂ [mul_one_class M] [mul_one_class N] [comm_monoid P] [comm_monoid Q] (f : M →* N →* P) (g : Q →* N) : M →* Q →* P := (comp_hom' g).comp f @[simp, to_additive] lemma compl₂_apply [mul_one_class M] [mul_one_class N] [comm_monoid P] [comm_monoid Q] (f : M →* N →* P) (g : Q →* N) (m : M) (q : Q) : (compl₂ f g) m q = f m (g q) := rfl /-- The expression `λ m n, g (f m n)` as a `monoid_hom`. -/ @[to_additive "The expression `λ m n, g (f m n)` as an `add_monoid_hom`. This also exists as a linear_map version, `linear_map.compr₂`"] def compr₂ [mul_one_class M] [mul_one_class N] [comm_monoid P] [comm_monoid Q] (f : M →* N →* P) (g : P →* Q) : M →* N →* Q := (comp_hom g).comp f @[simp, to_additive] lemma compr₂_apply [mul_one_class M] [mul_one_class N] [comm_monoid P] [comm_monoid Q] (f : M →* N →* P) (g : P →* Q) (m : M) (n : N) : (compr₂ f g) m n = g (f m n) := rfl end monoid_hom /-! ### Miscellaneous definitions Due to the fact this file imports `algebra.group_power.basic`, it is not possible to import it in some of the lower-level files like `algebra.ring.basic`. The following lemmas should be rehomed if the import structure permits them to be. -/ section semiring variables {R S : Type*} [semiring R] [semiring S] /-- Multiplication of an element of a (semi)ring is an `add_monoid_hom` in both arguments. This is a more-strongly bundled version of `add_monoid_hom.mul_left` and `add_monoid_hom.mul_right`. A stronger version of this exists for algebras as `algebra.lmul`. -/ def add_monoid_hom.mul : R →+ R →+ R := { to_fun := add_monoid_hom.mul_left, map_zero' := add_monoid_hom.ext $ zero_mul, map_add' := λ a b, add_monoid_hom.ext $ add_mul a b } lemma add_monoid_hom.mul_apply (x y : R) : add_monoid_hom.mul x y = x * y := rfl @[simp] lemma add_monoid_hom.coe_mul : ⇑(add_monoid_hom.mul : R →+ R →+ R) = add_monoid_hom.mul_left := rfl @[simp] lemma add_monoid_hom.coe_flip_mul : ⇑(add_monoid_hom.mul : R →+ R →+ R).flip = add_monoid_hom.mul_right := rfl /-- An `add_monoid_hom` preserves multiplication if pre- and post- composition with `add_monoid_hom.mul` are equivalent. By converting the statement into an equality of `add_monoid_hom`s, this lemma allows various specialized `ext` lemmas about `→+` to then be applied. -/ lemma add_monoid_hom.map_mul_iff (f : R →+ S) : (∀ x y, f (x * y) = f x * f y) ↔ (add_monoid_hom.mul : R →+ R →+ R).compr₂ f = (add_monoid_hom.mul.comp f).compl₂ f := iff.symm add_monoid_hom.ext_iff₂ end semiring
f8c4181bf3acbb116e4f59854373362c6ff79c37
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/data/string/basic.lean
7dea7361841ae2288aecaf87c77b0e32dfe33f1a
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
4,111
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Supplementary theorems about the `string` type. -/ import data.list.basic import data.char namespace string def ltb : iterator → iterator → bool | s₁ s₂ := begin cases s₂.has_next, {exact ff}, cases h₁ : s₁.has_next, {exact tt}, exact if s₁.curr = s₂.curr then have s₁.next.2.length < s₁.2.length, from match s₁, h₁ with ⟨_, a::l⟩, h := nat.lt_succ_self _ end, ltb s₁.next s₂.next else s₁.curr < s₂.curr, end using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ s, s.1.2.length)⟩]} instance has_lt' : has_lt string := ⟨λ s₁ s₂, ltb s₁.mk_iterator s₂.mk_iterator⟩ instance decidable_lt : @decidable_rel string (<) := by apply_instance -- short-circuit type class inference @[simp] theorem lt_iff_to_list_lt : ∀ {s₁ s₂ : string}, s₁ < s₂ ↔ s₁.to_list < s₂.to_list | ⟨i₁⟩ ⟨i₂⟩ := suffices ∀ {p₁ p₂ s₁ s₂}, ltb ⟨p₁, s₁⟩ ⟨p₂, s₂⟩ ↔ s₁ < s₂, from this, begin intros, induction s₁ with a s₁ IH generalizing p₁ p₂ s₂; cases s₂ with b s₂; rw ltb; simp [iterator.has_next], { refl, }, { exact iff_of_true rfl list.lex.nil }, { exact iff_of_false bool.ff_ne_tt (not_lt_of_lt list.lex.nil) }, { dsimp [iterator.has_next, iterator.curr, iterator.next], split_ifs, { subst b, exact IH.trans list.lex.cons_iff.symm }, { simp, refine ⟨list.lex.rel, λ e, _⟩, cases e, {cases h rfl}, assumption } } end instance has_le : has_le string := ⟨λ s₁ s₂, ¬ s₂ < s₁⟩ instance decidable_le : @decidable_rel string (≤) := by apply_instance -- short-circuit type class inference @[simp] theorem le_iff_to_list_le {s₁ s₂ : string} : s₁ ≤ s₂ ↔ s₁.to_list ≤ s₂.to_list := (not_congr lt_iff_to_list_lt).trans not_lt theorem to_list_inj : ∀ {s₁ s₂}, to_list s₁ = to_list s₂ ↔ s₁ = s₂ | ⟨s₁⟩ ⟨s₂⟩ := ⟨congr_arg _, congr_arg _⟩ lemma nil_as_string_eq_empty : [].as_string = "" := rfl @[simp] lemma to_list_empty : "".to_list = [] := rfl lemma as_string_inv_to_list (s : string) : s.to_list.as_string = s := by { cases s, refl } @[simp] lemma to_list_singleton (c : char) : (string.singleton c).to_list = [c] := rfl lemma to_list_nonempty : ∀ {s : string}, s ≠ string.empty → s.to_list = s.head :: (s.popn 1).to_list | ⟨s⟩ h := by cases s; [cases h rfl, refl] @[simp] lemma head_empty : "".head = default _ := rfl @[simp] lemma popn_empty {n : ℕ} : "".popn n = "" := begin induction n with n hn, { refl }, { rcases hs : "" with ⟨_ | ⟨hd, tl⟩⟩, { rw hs at hn, conv_rhs { rw ←hn }, simp only [popn, mk_iterator, iterator.nextn, iterator.next] }, { simpa only [←to_list_inj] using hs } } end instance : linear_order string := by refine_struct { lt := (<), le := (≤), decidable_lt := by apply_instance, decidable_le := string.decidable_le, decidable_eq := by apply_instance, .. }; { simp only [le_iff_to_list_le, lt_iff_to_list_lt, ← to_list_inj], introv, apply_field } end string open string lemma list.to_list_inv_as_string (l : list char) : l.as_string.to_list = l := by { cases hl : l.as_string, exact string_imp.mk.inj hl.symm } @[simp] lemma list.length_as_string (l : list char) : l.as_string.length = l.length := rfl @[simp] lemma list.as_string_inj {l l' : list char} : l.as_string = l'.as_string ↔ l = l' := ⟨λ h, by rw [←list.to_list_inv_as_string l, ←list.to_list_inv_as_string l', to_list_inj, h], λ h, h ▸ rfl⟩ @[simp] lemma string.length_to_list (s : string) : s.to_list.length = s.length := by rw [←string.as_string_inv_to_list s, list.to_list_inv_as_string, list.length_as_string] lemma list.as_string_eq {l : list char} {s : string} : l.as_string = s ↔ l = s.to_list := by rw [←as_string_inv_to_list s, list.as_string_inj, as_string_inv_to_list s]
d08734a50e65bc93265b849897078192ebc248ba
618003631150032a5676f229d13a079ac875ff77
/src/data/indicator_function.lean
726bf3b52db422b07aa27ca9635c8517ccf69caa
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
7,804
lean
/- Copyright (c) 2020 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import algebra.pi_instances import data.set.disjointed import data.support /-! # Indicator function `indicator (s : set α) (f : α → β) (a : α)` is `f a` if `a ∈ s` and is `0` otherwise. ## Implementation note In mathematics, an indicator function or a characteristic function is a function used to indicate membership of an element in a set `s`, having the value `1` for all elements of `s` and the value `0` otherwise. But since it is usually used to restrict a function to a certain set `s`, we let the indicator function take the value `f x` for some function `f`, instead of `1`. If the usual indicator function is needed, just set `f` to be the constant function `λx, 1`. ## Tags indicator, characteristic -/ noncomputable theory open_locale classical namespace set universes u v variables {α : Type u} {β : Type v} section has_zero variables [has_zero β] {s t : set α} {f g : α → β} {a : α} /-- `indicator s f a` is `f a` if `a ∈ s`, `0` otherwise. -/ @[reducible] def indicator (s : set α) (f : α → β) : α → β := λ x, if x ∈ s then f x else 0 lemma indicator_apply (s : set α) (f : α → β) (a : α) : indicator s f a = if a ∈ s then f a else 0 := rfl @[simp] lemma indicator_of_mem (h : a ∈ s) (f : α → β) : indicator s f a = f a := if_pos h @[simp] lemma indicator_of_not_mem (h : a ∉ s) (f : α → β) : indicator s f a = 0 := if_neg h lemma support_indicator : function.support (s.indicator f) ⊆ s := λ x hx, hx.imp_symm (λ h, indicator_of_not_mem h f) lemma indicator_congr (h : ∀ a ∈ s, f a = g a) : indicator s f = indicator s g := funext $ λx, by { simp only [indicator], split_ifs, { exact h _ h_1 }, refl } @[simp] lemma indicator_univ (f : α → β) : indicator (univ : set α) f = f := funext $ λx, indicator_of_mem (mem_univ _) f @[simp] lemma indicator_empty (f : α → β) : indicator (∅ : set α) f = λa, 0 := funext $ λx, indicator_of_not_mem (not_mem_empty _) f variable (β) @[simp] lemma indicator_zero (s : set α) : indicator s (λx, (0:β)) = λx, (0:β) := funext $ λx, by { simp only [indicator], split_ifs, refl, refl } variable {β} lemma indicator_indicator (s t : set α) (f : α → β) : indicator s (indicator t f) = indicator (s ∩ t) f := funext $ λx, by { simp only [indicator], split_ifs, repeat {simp * at * {contextual := tt}} } lemma indicator_comp_of_zero {γ} [has_zero γ] {g : β → γ} (hg : g 0 = 0) : indicator s (g ∘ f) = λ a, indicator (f '' s) g (indicator s f a) := begin funext, simp only [indicator], split_ifs with h h', { refl }, { have := mem_image_of_mem _ h, contradiction }, { rwa eq_comm }, refl end lemma indicator_preimage (s : set α) (f : α → β) (B : set β) : (indicator s f)⁻¹' B = s ∩ f ⁻¹' B ∪ (-s) ∩ (λa:α, (0:β)) ⁻¹' B := by { rw [indicator, if_preimage] } end has_zero section add_monoid variables [add_monoid β] {s t : set α} {f g : α → β} {a : α} lemma indicator_union_of_not_mem_inter (h : a ∉ s ∩ t) (f : α → β) : indicator (s ∪ t) f a = indicator s f a + indicator t f a := by { simp only [indicator], split_ifs, repeat {simp * at * {contextual := tt}} } lemma indicator_union_of_disjoint (h : disjoint s t) (f : α → β) : indicator (s ∪ t) f = λa, indicator s f a + indicator t f a := funext $ λa, indicator_union_of_not_mem_inter (by { convert not_mem_empty a, have := disjoint.eq_bot h, assumption }) _ lemma indicator_add (s : set α) (f g : α → β) : indicator s (λa, f a + g a) = λa, indicator s f a + indicator s g a := by { funext, simp only [indicator], split_ifs, { refl }, rw add_zero } variables (β) instance is_add_monoid_hom.indicator (s : set α) : is_add_monoid_hom (λf:α → β, indicator s f) := { map_add := λ _ _, indicator_add _ _ _, map_zero := indicator_zero _ _ } variables {β} {𝕜 : Type*} [monoid 𝕜] [distrib_mul_action 𝕜 β] lemma indicator_smul (s : set α) (r : 𝕜) (f : α → β) : indicator s (λ (x : α), r • f x) = λ (x : α), r • indicator s f x := by { simp only [indicator], funext, split_ifs, refl, exact (smul_zero r).symm } end add_monoid section add_group variables [add_group β] {s t : set α} {f g : α → β} {a : α} variables (β) instance is_add_group_hom.indicator (s : set α) : is_add_group_hom (λf:α → β, indicator s f) := { .. is_add_monoid_hom.indicator β s } variables {β} lemma indicator_neg (s : set α) (f : α → β) : indicator s (λa, - f a) = λa, - indicator s f a := show indicator s (- f) = - indicator s f, from is_add_group_hom.map_neg _ _ lemma indicator_sub (s : set α) (f g : α → β) : indicator s (λa, f a - g a) = λa, indicator s f a - indicator s g a := show indicator s (f - g) = indicator s f - indicator s g, from is_add_group_hom.map_sub _ _ _ lemma indicator_compl (s : set α) (f : α → β) : indicator (-s) f = λ a, f a - indicator s f a := begin funext, simp only [indicator], split_ifs with h₁ h₂, { rw sub_zero }, { rw sub_self }, { rw ← mem_compl_iff at h₂, contradiction } end lemma indicator_finset_sum {β} [add_comm_monoid β] {ι : Type*} (I : finset ι) (s : set α) (f : ι → α → β) : indicator s (I.sum f) = I.sum (λ i, indicator s (f i)) := begin convert (finset.sum_hom _ _).symm, split, exact indicator_zero _ _ end lemma indicator_finset_bUnion {β} [add_comm_monoid β] {ι} (I : finset ι) (s : ι → set α) {f : α → β} : (∀ (i ∈ I) (j ∈ I), i ≠ j → s i ∩ s j = ∅) → indicator (⋃ i ∈ I, s i) f = λ a, I.sum (λ i, indicator (s i) f a) := begin refine finset.induction_on I _ _, assume h, { funext, simp }, assume a I haI ih hI, funext, simp only [haI, finset.sum_insert, not_false_iff], rw [finset.bUnion_insert, indicator_union_of_not_mem_inter, ih _], { assume i hi j hj hij, exact hI i (finset.mem_insert_of_mem hi) j (finset.mem_insert_of_mem hj) hij }, simp only [not_exists, exists_prop, mem_Union, mem_inter_eq, not_and], assume hx a' ha', have := hI a (finset.mem_insert_self _ _) a' (finset.mem_insert_of_mem ha') _, { assume h, have h := mem_inter hx h, rw this at h, exact not_mem_empty _ h }, { assume h, rw h at haI, contradiction } end end add_group section mul_zero_class variables [mul_zero_class β] {s t : set α} {f g : α → β} {a : α} lemma indicator_mul (s : set α) (f g : α → β) : indicator s (λa, f a * g a) = λa, indicator s f a * indicator s g a := by { funext, simp only [indicator], split_ifs, { refl }, rw mul_zero } end mul_zero_class section order variables [has_zero β] [preorder β] {s t : set α} {f g : α → β} {a : α} lemma indicator_nonneg' (h : a ∈ s → 0 ≤ f a) : 0 ≤ indicator s f a := by { rw indicator_apply, split_ifs with as, { exact h as }, refl } lemma indicator_nonneg (h : ∀ a ∈ s, 0 ≤ f a) : ∀ a, 0 ≤ indicator s f a := λ a, indicator_nonneg' (h a) lemma indicator_nonpos' (h : a ∈ s → f a ≤ 0) : indicator s f a ≤ 0 := by { rw indicator_apply, split_ifs with as, { exact h as }, refl } lemma indicator_nonpos (h : ∀ a ∈ s, f a ≤ 0) : ∀ a, indicator s f a ≤ 0 := λ a, indicator_nonpos' (h a) lemma indicator_le_indicator (h : f a ≤ g a) : indicator s f a ≤ indicator s g a := by { simp only [indicator], split_ifs with ha, { exact h }, refl } lemma indicator_le_indicator_of_subset (h : s ⊆ t) (hf : ∀a, 0 ≤ f a) (a : α) : indicator s f a ≤ indicator t f a := begin simp only [indicator], split_ifs with h₁, { refl }, { have := h h₁, contradiction }, { exact hf a }, { refl } end end order end set
f355ae40a246216288721362e0ec79f374a23434
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/analysis/calculus/fderiv_measurable.lean
8ab787fa104a492b5c966fe58416e5a5da5c5032
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
21,702
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Yury Kudryashov -/ import analysis.calculus.deriv import measure_theory.constructions.borel_space import tactic.ring_exp /-! # Derivative is measurable In this file we prove that the derivative of any function with complete codomain is a measurable function. Namely, we prove: * `measurable_set_of_differentiable_at`: the set `{x | differentiable_at 𝕜 f x}` is measurable; * `measurable_fderiv`: the function `fderiv 𝕜 f` is measurable; * `measurable_fderiv_apply_const`: for a fixed vector `y`, the function `λ x, fderiv 𝕜 f x y` is measurable; * `measurable_deriv`: the function `deriv f` is measurable (for `f : 𝕜 → F`). ## Implementation We give a proof that avoids second-countability issues, by expressing the differentiability set as a function of open sets in the following way. Define `A (L, r, ε)` to be the set of points where, on a ball of radius roughly `r` around `x`, the function is uniformly approximated by the linear map `L`, up to `ε r`. It is an open set. Let also `B (L, r, s, ε) = A (L, r, ε) ∩ A (L, s, ε)`: we require that at two possibly different scales `r` and `s`, the function is well approximated by the linear map `L`. It is also open. We claim that the differentiability set of `f` is exactly `D = ⋂ ε > 0, ⋃ δ > 0, ⋂ r, s < δ, ⋃ L, B (L, r, s, ε)`. In other words, for any `ε > 0`, we require that there is a size `δ` such that, for any two scales below this size, the function is well approximated by a linear map, common to the two scales. The set `⋃ L, B (L, r, s, ε)` is open, as a union of open sets. Converting the intersections and unions to countable ones (using real numbers of the form `2 ^ (-n)`), it follows that the differentiability set is measurable. To prove the claim, there are two inclusions. One is trivial: if the function is differentiable at `x`, then `x` belongs to `D` (just take `L` to be the derivative, and use that the differentiability exactly says that the map is well approximated by `L`). This is proved in `mem_A_of_differentiable` and `differentiable_set_subset_D`. For the other direction, the difficulty is that `L` in the union may depend on `ε, r, s`. The key point is that, in fact, it doesn't depend too much on them. First, if `x` belongs both to `A (L, r, ε)` and `A (L', r, ε)`, then `L` and `L'` have to be close on a shell, and thus `∥L - L'∥` is bounded by `ε` (see `norm_sub_le_of_mem_A`). Assume now `x ∈ D`. If one has two maps `L` and `L'` such that `x` belongs to `A (L, r, ε)` and to `A (L', r', ε')`, one deduces that `L` is close to `L'` by arguing as follows. Consider another scale `s` smaller than `r` and `r'`. Take a linear map `L₁` that approximates `f` around `x` both at scales `r` and `s` w.r.t. `ε` (it exists as `x` belongs to `D`). Take also `L₂` that approximates `f` around `x` both at scales `r'` and `s` w.r.t. `ε'`. Then `L₁` is close to `L` (as they are close on a shell of radius `r`), and `L₂` is close to `L₁` (as they are close on a shell of radius `s`), and `L'` is close to `L₂` (as they are close on a shell of radius `r'`). It follows that `L` is close to `L'`, as we claimed. It follows that the different approximating linear maps that show up form a Cauchy sequence when `ε` tends to `0`. When the target space is complete, this sequence converges, to a limit `f'`. With the same kind of arguments, one checks that `f` is differentiable with derivative `f'`. To show that the derivative itself is measurable, add in the definition of `B` and `D` a set `K` of continuous linear maps to which `L` should belong. Then, when `K` is complete, the set `D K` is exactly the set of points where `f` is differentiable with a derivative in `K`. ## Tags derivative, measurable function, Borel σ-algebra -/ noncomputable theory open set metric asymptotics filter continuous_linear_map open topological_space (second_countable_topology) open_locale topological_space namespace continuous_linear_map variables {𝕜 E F : Type*} [nondiscrete_normed_field 𝕜] [normed_group E] [normed_space 𝕜 E] [normed_group F] [normed_space 𝕜 F] lemma measurable_apply₂ [measurable_space E] [opens_measurable_space E] [second_countable_topology E] [second_countable_topology (E →L[𝕜] F)] [measurable_space F] [borel_space F] : measurable (λ p : (E →L[𝕜] F) × E, p.1 p.2) := is_bounded_bilinear_map_apply.continuous.measurable end continuous_linear_map variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] variables {F : Type*} [normed_group F] [normed_space 𝕜 F] variables {f : E → F} (K : set (E →L[𝕜] F)) namespace fderiv_measurable_aux /-- The set `A f L r ε` is the set of points `x` around which the function `f` is well approximated at scale `r` by the linear map `L`, up to an error `ε`. We tweak the definition to make sure that this is an open set.-/ def A (f : E → F) (L : E →L[𝕜] F) (r ε : ℝ) : set E := {x | ∃ r' ∈ Ioc (r/2) r, ∀ y z ∈ ball x r', ∥f z - f y - L (z-y)∥ ≤ ε * r} /-- The set `B f K r s ε` is the set of points `x` around which there exists a continuous linear map `L` belonging to `K` (a given set of continuous linear maps) that approximates well the function `f` (up to an error `ε`), simultaneously at scales `r` and `s`. -/ def B (f : E → F) (K : set (E →L[𝕜] F)) (r s ε : ℝ) : set E := ⋃ (L ∈ K), (A f L r ε) ∩ (A f L s ε) /-- The set `D f K` is a complicated set constructed using countable intersections and unions. Its main use is that, when `K` is complete, it is exactly the set of points where `f` is differentiable, with a derivative in `K`. -/ def D (f : E → F) (K : set (E →L[𝕜] F)) : set E := ⋂ (e : ℕ), ⋃ (n : ℕ), ⋂ (p ≥ n) (q ≥ n), B f K ((1/2) ^ p) ((1/2) ^ q) ((1/2) ^ e) lemma is_open_A (L : E →L[𝕜] F) (r ε : ℝ) : is_open (A f L r ε) := begin rw metric.is_open_iff, rintros x ⟨r', r'_mem, hr'⟩, obtain ⟨s, s_gt, s_lt⟩ : ∃ (s : ℝ), r / 2 < s ∧ s < r' := exists_between r'_mem.1, have : s ∈ Ioc (r/2) r := ⟨s_gt, le_of_lt (s_lt.trans_le r'_mem.2)⟩, refine ⟨r' - s, by linarith, λ x' hx', ⟨s, this, _⟩⟩, have B : ball x' s ⊆ ball x r' := ball_subset (le_of_lt hx'), assume y z hy hz, exact hr' y z (B hy) (B hz) end lemma is_open_B {K : set (E →L[𝕜] F)} {r s ε : ℝ} : is_open (B f K r s ε) := by simp [B, is_open_Union, is_open.inter, is_open_A] lemma A_mono (L : E →L[𝕜] F) (r : ℝ) {ε δ : ℝ} (h : ε ≤ δ) : A f L r ε ⊆ A f L r δ := begin rintros x ⟨r', r'r, hr'⟩, refine ⟨r', r'r, λ y z hy hz, _⟩, apply le_trans (hr' y z hy hz), apply mul_le_mul_of_nonneg_right h, linarith [mem_ball.1 hy, r'r.2, @dist_nonneg _ _ y x], end lemma le_of_mem_A {r ε : ℝ} {L : E →L[𝕜] F} {x : E} (hx : x ∈ A f L r ε) {y z : E} (hy : y ∈ closed_ball x (r/2)) (hz : z ∈ closed_ball x (r/2)) : ∥f z - f y - L (z-y)∥ ≤ ε * r := begin rcases hx with ⟨r', r'mem, hr'⟩, exact hr' _ _ (lt_of_le_of_lt (mem_closed_ball.1 hy) r'mem.1) (lt_of_le_of_lt (mem_closed_ball.1 hz) r'mem.1) end lemma mem_A_of_differentiable {ε : ℝ} (hε : 0 < ε) {x : E} (hx : differentiable_at 𝕜 f x) : ∃ R > 0, ∀ r ∈ Ioo (0 : ℝ) R, x ∈ A f (fderiv 𝕜 f x) r ε := begin have := hx.has_fderiv_at, simp only [has_fderiv_at, has_fderiv_at_filter, is_o_iff] at this, rcases eventually_nhds_iff_ball.1 (this (half_pos hε)) with ⟨R, R_pos, hR⟩, refine ⟨R, R_pos, λ r hr, _⟩, have : r ∈ Ioc (r/2) r := ⟨half_lt_self hr.1, le_refl _⟩, refine ⟨r, this, λ y z hy hz, _⟩, calc ∥f z - f y - (fderiv 𝕜 f x) (z - y)∥ = ∥(f z - f x - (fderiv 𝕜 f x) (z - x)) - (f y - f x - (fderiv 𝕜 f x) (y - x))∥ : by { congr' 1, simp only [continuous_linear_map.map_sub], abel } ... ≤ ∥(f z - f x - (fderiv 𝕜 f x) (z - x))∥ + ∥f y - f x - (fderiv 𝕜 f x) (y - x)∥ : norm_sub_le _ _ ... ≤ ε / 2 * ∥z - x∥ + ε / 2 * ∥y - x∥ : add_le_add (hR _ (lt_trans (mem_ball.1 hz) hr.2)) (hR _ (lt_trans (mem_ball.1 hy) hr.2)) ... ≤ ε / 2 * r + ε / 2 * r : add_le_add (mul_le_mul_of_nonneg_left (le_of_lt (mem_ball_iff_norm.1 hz)) (le_of_lt (half_pos hε))) (mul_le_mul_of_nonneg_left (le_of_lt (mem_ball_iff_norm.1 hy)) (le_of_lt (half_pos hε))) ... = ε * r : by ring end lemma norm_sub_le_of_mem_A {c : 𝕜} (hc : 1 < ∥c∥) {r ε : ℝ} (hε : 0 < ε) (hr : 0 < r) {x : E} {L₁ L₂ : E →L[𝕜] F} (h₁ : x ∈ A f L₁ r ε) (h₂ : x ∈ A f L₂ r ε) : ∥L₁ - L₂∥ ≤ 4 * ∥c∥ * ε := begin have : 0 ≤ 4 * ∥c∥ * ε := mul_nonneg (mul_nonneg (by norm_num : (0 : ℝ) ≤ 4) (norm_nonneg _)) hε.le, apply op_norm_le_of_shell (half_pos hr) this hc, assume y ley ylt, rw [div_div_eq_div_mul, div_le_iff' (mul_pos (by norm_num : (0 : ℝ) < 2) (zero_lt_one.trans hc))] at ley, calc ∥(L₁ - L₂) y∥ = ∥(f (x + y) - f x - L₂ ((x + y) - x)) - (f (x + y) - f x - L₁ ((x + y) - x))∥ : by simp ... ≤ ∥(f (x + y) - f x - L₂ ((x + y) - x))∥ + ∥(f (x + y) - f x - L₁ ((x + y) - x))∥ : norm_sub_le _ _ ... ≤ ε * r + ε * r : begin apply add_le_add, { apply le_of_mem_A h₂, { simp only [le_of_lt (half_pos hr), mem_closed_ball, dist_self] }, { simp only [dist_eq_norm, add_sub_cancel', mem_closed_ball, ylt.le], } }, { apply le_of_mem_A h₁, { simp only [le_of_lt (half_pos hr), mem_closed_ball, dist_self] }, { simp only [dist_eq_norm, add_sub_cancel', mem_closed_ball, ylt.le] } }, end ... = 2 * ε * r : by ring ... ≤ 2 * ε * (2 * ∥c∥ * ∥y∥) : mul_le_mul_of_nonneg_left ley (mul_nonneg (by norm_num) hε.le) ... = 4 * ∥c∥ * ε * ∥y∥ : by ring end /-- Easy inclusion: a differentiability point with derivative in `K` belongs to `D f K`. -/ lemma differentiable_set_subset_D : {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ K} ⊆ D f K := begin assume x hx, rw [D, mem_Inter], assume e, have : (0 : ℝ) < (1/2) ^ e := pow_pos (by norm_num) _, rcases mem_A_of_differentiable this hx.1 with ⟨R, R_pos, hR⟩, obtain ⟨n, hn⟩ : ∃ (n : ℕ), (1/2) ^ n < R := exists_pow_lt_of_lt_one R_pos (by norm_num : (1 : ℝ)/2 < 1), simp only [mem_Union, mem_Inter, B, mem_inter_eq], refine ⟨n, λ p hp q hq, ⟨fderiv 𝕜 f x, hx.2, ⟨_, _⟩⟩⟩; { refine hR _ ⟨pow_pos (by norm_num) _, lt_of_le_of_lt _ hn⟩, exact pow_le_pow_of_le_one (by norm_num) (by norm_num) (by assumption) } end /-- Harder inclusion: at a point in `D f K`, the function `f` has a derivative, in `K`. -/ lemma D_subset_differentiable_set {K : set (E →L[𝕜] F)} (hK : is_complete K) : D f K ⊆ {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ K} := begin have P : ∀ {n : ℕ}, (0 : ℝ) < (1/2) ^ n := pow_pos (by norm_num), rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩, have cpos : 0 < ∥c∥ := lt_trans zero_lt_one hc, assume x hx, have : ∀ (e : ℕ), ∃ (n : ℕ), ∀ p q, n ≤ p → n ≤ q → ∃ L ∈ K, x ∈ A f L ((1/2) ^ p) ((1/2) ^ e) ∩ A f L ((1/2) ^ q) ((1/2) ^ e), { assume e, have := mem_Inter.1 hx e, rcases mem_Union.1 this with ⟨n, hn⟩, refine ⟨n, λ p q hp hq, _⟩, simp only [mem_Inter, ge_iff_le] at hn, rcases mem_Union.1 (hn p hp q hq) with ⟨L, hL⟩, exact ⟨L, mem_Union.1 hL⟩, }, /- Recast the assumptions: for each `e`, there exist `n e` and linear maps `L e p q` in `K` such that, for `p, q ≥ n e`, then `f` is well approximated by `L e p q` at scale `2 ^ (-p)` and `2 ^ (-q)`, with an error `2 ^ (-e)`. -/ choose! n L hn using this, /- All the operators `L e p q` that show up are close to each other. To prove this, we argue that `L e p q` is close to `L e p r` (where `r` is large enough), as both approximate `f` at scale `2 ^(- p)`. And `L e p r` is close to `L e' p' r` as both approximate `f` at scale `2 ^ (- r)`. And `L e' p' r` is close to `L e' p' q'` as both approximate `f` at scale `2 ^ (- p')`. -/ have M : ∀ e p q e' p' q', n e ≤ p → n e ≤ q → n e' ≤ p' → n e' ≤ q' → e ≤ e' → ∥L e p q - L e' p' q'∥ ≤ 12 * ∥c∥ * (1/2) ^ e, { assume e p q e' p' q' hp hq hp' hq' he', let r := max (n e) (n e'), have I : ((1:ℝ)/2)^e' ≤ (1/2)^e := pow_le_pow_of_le_one (by norm_num) (by norm_num) he', have J1 : ∥L e p q - L e p r∥ ≤ 4 * ∥c∥ * (1/2)^e, { have I1 : x ∈ A f (L e p q) ((1 / 2) ^ p) ((1/2)^e) := (hn e p q hp hq).2.1, have I2 : x ∈ A f (L e p r) ((1 / 2) ^ p) ((1/2)^e) := (hn e p r hp (le_max_left _ _)).2.1, exact norm_sub_le_of_mem_A hc P P I1 I2 }, have J2 : ∥L e p r - L e' p' r∥ ≤ 4 * ∥c∥ * (1/2)^e, { have I1 : x ∈ A f (L e p r) ((1 / 2) ^ r) ((1/2)^e) := (hn e p r hp (le_max_left _ _)).2.2, have I2 : x ∈ A f (L e' p' r) ((1 / 2) ^ r) ((1/2)^e') := (hn e' p' r hp' (le_max_right _ _)).2.2, exact norm_sub_le_of_mem_A hc P P I1 (A_mono _ _ I I2) }, have J3 : ∥L e' p' r - L e' p' q'∥ ≤ 4 * ∥c∥ * (1/2)^e, { have I1 : x ∈ A f (L e' p' r) ((1 / 2) ^ p') ((1/2)^e') := (hn e' p' r hp' (le_max_right _ _)).2.1, have I2 : x ∈ A f (L e' p' q') ((1 / 2) ^ p') ((1/2)^e') := (hn e' p' q' hp' hq').2.1, exact norm_sub_le_of_mem_A hc P P (A_mono _ _ I I1) (A_mono _ _ I I2) }, calc ∥L e p q - L e' p' q'∥ = ∥(L e p q - L e p r) + (L e p r - L e' p' r) + (L e' p' r - L e' p' q')∥ : by { congr' 1, abel } ... ≤ ∥L e p q - L e p r∥ + ∥L e p r - L e' p' r∥ + ∥L e' p' r - L e' p' q'∥ : le_trans (norm_add_le _ _) (add_le_add_right (norm_add_le _ _) _) ... ≤ 4 * ∥c∥ * (1/2)^e + 4 * ∥c∥ * (1/2)^e + 4 * ∥c∥ * (1/2)^e : by apply_rules [add_le_add] ... = 12 * ∥c∥ * (1/2)^e : by ring }, /- For definiteness, use `L0 e = L e (n e) (n e)`, to have a single sequence. We claim that this is a Cauchy sequence. -/ let L0 : ℕ → (E →L[𝕜] F) := λ e, L e (n e) (n e), have : cauchy_seq L0, { rw metric.cauchy_seq_iff', assume ε εpos, obtain ⟨e, he⟩ : ∃ (e : ℕ), (1/2) ^ e < ε / (12 * ∥c∥) := exists_pow_lt_of_lt_one (div_pos εpos (mul_pos (by norm_num) cpos)) (by norm_num), refine ⟨e, λ e' he', _⟩, rw [dist_comm, dist_eq_norm], calc ∥L0 e - L0 e'∥ ≤ 12 * ∥c∥ * (1/2)^e : M _ _ _ _ _ _ (le_refl _) (le_refl _) (le_refl _) (le_refl _) he' ... < 12 * ∥c∥ * (ε / (12 * ∥c∥)) : mul_lt_mul' (le_refl _) he (le_of_lt P) (mul_pos (by norm_num) cpos) ... = ε : by { field_simp [(by norm_num : (12 : ℝ) ≠ 0), ne_of_gt cpos], ring } }, /- As it is Cauchy, the sequence `L0` converges, to a limit `f'` in `K`.-/ obtain ⟨f', f'K, hf'⟩ : ∃ f' ∈ K, tendsto L0 at_top (𝓝 f') := cauchy_seq_tendsto_of_is_complete hK (λ e, (hn e (n e) (n e) (le_refl _) (le_refl _)).1) this, have Lf' : ∀ e p, n e ≤ p → ∥L e (n e) p - f'∥ ≤ 12 * ∥c∥ * (1/2)^e, { assume e p hp, apply le_of_tendsto (tendsto_const_nhds.sub hf').norm, rw eventually_at_top, exact ⟨e, λ e' he', M _ _ _ _ _ _ (le_refl _) hp (le_refl _) (le_refl _) he'⟩ }, /- Let us show that `f` has derivative `f'` at `x`. -/ have : has_fderiv_at f f' x, { simp only [has_fderiv_at_iff_is_o_nhds_zero, is_o_iff], /- to get an approximation with a precision `ε`, we will replace `f` with `L e (n e) m` for some large enough `e` (yielding a small error by uniform approximation). As one can vary `m`, this makes it possible to cover all scales, and thus to obtain a good linear approximation in the whole ball of radius `(1/2)^(n e)`. -/ assume ε εpos, have pos : 0 < 4 + 12 * ∥c∥ := add_pos_of_pos_of_nonneg (by norm_num) (mul_nonneg (by norm_num) (norm_nonneg _)), obtain ⟨e, he⟩ : ∃ (e : ℕ), (1 / 2) ^ e < ε / (4 + 12 * ∥c∥) := exists_pow_lt_of_lt_one (div_pos εpos pos) (by norm_num), rw eventually_nhds_iff_ball, refine ⟨(1/2) ^ (n e + 1), P, λ y hy, _⟩, -- We need to show that `f (x + y) - f x - f' y` is small. For this, we will work at scale -- `k` where `k` is chosen with `∥y∥ ∼ 2 ^ (-k)`. by_cases y_pos : y = 0, {simp [y_pos] }, have yzero : 0 < ∥y∥ := norm_pos_iff.mpr y_pos, have y_lt : ∥y∥ < (1/2) ^ (n e + 1), by simpa using mem_ball_iff_norm.1 hy, have yone : ∥y∥ ≤ 1 := le_trans (y_lt.le) (pow_le_one _ (by norm_num) (by norm_num)), -- define the scale `k`. obtain ⟨k, hk, h'k⟩ : ∃ (k : ℕ), (1/2) ^ (k + 1) < ∥y∥ ∧ ∥y∥ ≤ (1/2) ^ k := exists_nat_pow_near_of_lt_one yzero yone (by norm_num : (0 : ℝ) < 1/2) (by norm_num : (1 : ℝ)/2 < 1), -- the scale is large enough (as `y` is small enough) have k_gt : n e < k, { have : ((1:ℝ)/2) ^ (k + 1) < (1/2) ^ (n e + 1) := lt_trans hk y_lt, rw pow_lt_pow_iff_of_lt_one (by norm_num : (0 : ℝ) < 1/2) (by norm_num) at this, linarith }, set m := k - 1 with hl, have m_ge : n e ≤ m := nat.le_pred_of_lt k_gt, have km : k = m + 1 := (nat.succ_pred_eq_of_pos (lt_of_le_of_lt (zero_le _) k_gt)).symm, rw km at hk h'k, -- `f` is well approximated by `L e (n e) k` at the relevant scale -- (in fact, we use `m = k - 1` instead of `k` because of the precise definition of `A`). have J1 : ∥f (x + y) - f x - L e (n e) m ((x + y) - x)∥ ≤ (1/2) ^ e * (1/2) ^ m, { apply le_of_mem_A (hn e (n e) m (le_refl _) m_ge).2.2, { simp only [mem_closed_ball, dist_self], exact div_nonneg (le_of_lt P) (zero_le_two) }, { simpa only [dist_eq_norm, add_sub_cancel', mem_closed_ball, pow_succ', mul_one_div] using h'k } }, have J2 : ∥f (x + y) - f x - L e (n e) m y∥ ≤ 4 * (1/2) ^ e * ∥y∥ := calc ∥f (x + y) - f x - L e (n e) m y∥ ≤ (1/2) ^ e * (1/2) ^ m : by simpa only [add_sub_cancel'] using J1 ... = 4 * (1/2) ^ e * (1/2) ^ (m + 2) : by { field_simp, ring_exp } ... ≤ 4 * (1/2) ^ e * ∥y∥ : mul_le_mul_of_nonneg_left (le_of_lt hk) (mul_nonneg (by norm_num) (le_of_lt P)), -- use the previous estimates to see that `f (x + y) - f x - f' y` is small. calc ∥f (x + y) - f x - f' y∥ = ∥(f (x + y) - f x - L e (n e) m y) + (L e (n e) m - f') y∥ : congr_arg _ (by simp) ... ≤ 4 * (1/2) ^ e * ∥y∥ + 12 * ∥c∥ * (1/2) ^ e * ∥y∥ : norm_add_le_of_le J2 ((le_op_norm _ _).trans (mul_le_mul_of_nonneg_right (Lf' _ _ m_ge) (norm_nonneg _))) ... = (4 + 12 * ∥c∥) * ∥y∥ * (1/2) ^ e : by ring ... ≤ (4 + 12 * ∥c∥) * ∥y∥ * (ε / (4 + 12 * ∥c∥)) : mul_le_mul_of_nonneg_left he.le (mul_nonneg (add_nonneg (by norm_num) (mul_nonneg (by norm_num) (norm_nonneg _))) (norm_nonneg _)) ... = ε * ∥y∥ : by { field_simp [ne_of_gt pos], ring } }, rw ← this.fderiv at f'K, exact ⟨this.differentiable_at, f'K⟩ end theorem differentiable_set_eq_D (hK : is_complete K) : {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ K} = D f K := subset.antisymm (differentiable_set_subset_D _) (D_subset_differentiable_set hK) end fderiv_measurable_aux open fderiv_measurable_aux variables [measurable_space E] [opens_measurable_space E] variables (𝕜 f) /-- The set of differentiability points of a function, with derivative in a given complete set, is Borel-measurable. -/ theorem measurable_set_of_differentiable_at_of_is_complete {K : set (E →L[𝕜] F)} (hK : is_complete K) : measurable_set {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ K} := by simp [differentiable_set_eq_D K hK, D, is_open_B.measurable_set, measurable_set.Inter_Prop, measurable_set.Inter, measurable_set.Union] variable [complete_space F] /-- The set of differentiability points of a function taking values in a complete space is Borel-measurable. -/ theorem measurable_set_of_differentiable_at : measurable_set {x | differentiable_at 𝕜 f x} := begin have : is_complete (univ : set (E →L[𝕜] F)) := complete_univ, convert measurable_set_of_differentiable_at_of_is_complete 𝕜 f this, simp end lemma measurable_fderiv : measurable (fderiv 𝕜 f) := begin refine measurable_of_is_closed (λ s hs, _), have : fderiv 𝕜 f ⁻¹' s = {x | differentiable_at 𝕜 f x ∧ fderiv 𝕜 f x ∈ s} ∪ {x | (0 : E →L[𝕜] F) ∈ s} ∩ {x | ¬differentiable_at 𝕜 f x} := set.ext (λ x, mem_preimage.trans fderiv_mem_iff), rw this, exact (measurable_set_of_differentiable_at_of_is_complete _ _ hs.is_complete).union ((measurable_set.const _).inter (measurable_set_of_differentiable_at _ _).compl) end lemma measurable_fderiv_apply_const [measurable_space F] [borel_space F] (y : E) : measurable (λ x, fderiv 𝕜 f x y) := (continuous_linear_map.measurable_apply y).comp (measurable_fderiv 𝕜 f) variable {𝕜} lemma measurable_deriv [measurable_space 𝕜] [opens_measurable_space 𝕜] [measurable_space F] [borel_space F] (f : 𝕜 → F) : measurable (deriv f) := by simpa only [fderiv_deriv] using measurable_fderiv_apply_const 𝕜 f 1
23147d8f2ccbcb4d8fe078b88030dd0fd4d62d24
df561f413cfe0a88b1056655515399c546ff32a5
/6-advanced-addition-world/l6.lean
6ec9dc5c5509453989a6964031252e9ae0cf573d
[]
no_license
nicholaspun/natural-number-game-solutions
31d5158415c6f582694680044c5c6469032c2a06
1e2aed86d2e76a3f4a275c6d99e795ad30cf6df0
refs/heads/main
1,675,123,625,012
1,607,633,548,000
1,607,633,548,000
318,933,860
3
1
null
null
null
null
UTF-8
Lean
false
false
139
lean
theorem add_left_cancel (t a b : mynat) : t + a = t + b → a = b := begin rw add_comm, rw add_comm t b, exact add_right_cancel a t b, end
93daf2c8ffd4a26122afb90bed750a9ba9911bff
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Data/Lsp/Basic.lean
5894d5d3d6b71c82af4a82e8f788d85961cd3684
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
4,662
lean
/- Copyright (c) 2020 Marc Huisinga. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Marc Huisinga, Wojciech Nawrocki -/ import Lean.Data.Json import Lean.Data.JsonRpc /-! Defines most of the 'Basic Structures' in the LSP specification (https://microsoft.github.io/language-server-protocol/specifications/specification-current/), as well as some utilities. Since LSP is Json-based, Ints/Nats are represented by Floats on the wire. -/ namespace Lean namespace Lsp open Json structure CancelParams where id : JsonRpc.RequestID deriving Inhabited, BEq, ToJson, FromJson abbrev DocumentUri := String /-- We adopt the convention that zero-based UTF-16 positions as sent by LSP clients are represented by `Lsp.Position` while internally we mostly use `String.Pos` UTF-8 offsets. For diagnostics, one-based `Lean.Position`s are used internally. `character` is accepted liberally: actual character := min(line length, character) -/ structure Position where line : Nat character : Nat deriving Inhabited, BEq, Hashable, ToJson, FromJson instance : ToString Position := ⟨fun p => "(" ++ toString p.line ++ ", " ++ toString p.character ++ ")"⟩ structure Range where start : Position «end» : Position deriving Inhabited, BEq, Hashable, ToJson, FromJson structure Location where uri : DocumentUri range : Range deriving Inhabited, BEq, ToJson, FromJson structure LocationLink where originSelectionRange? : Option Range targetUri : DocumentUri targetRange : Range targetSelectionRange : Range deriving ToJson, FromJson -- NOTE: Diagnostic defined in Diagnostics.lean /- NOTE: No specific commands are specified by LSP, hence possible commands need to be announced as capabilities. -/ structure Command where title : String command : String arguments? : Option (Array Json) := none deriving ToJson, FromJson structure TextEdit where range : Range newText : String deriving ToJson, FromJson def TextEditBatch := Array TextEdit instance : FromJson TextEditBatch := ⟨@fromJson? (Array TextEdit) _⟩ instance : ToJson TextEditBatch := ⟨@toJson (Array TextEdit) _⟩ structure TextDocumentIdentifier where uri : DocumentUri deriving ToJson, FromJson structure VersionedTextDocumentIdentifier where uri : DocumentUri version? : Option Nat := none deriving ToJson, FromJson structure TextDocumentEdit where textDocument : VersionedTextDocumentIdentifier edits : TextEditBatch deriving ToJson, FromJson -- TODO(Marc): missing: -- File Resource Changes, WorkspaceEdit -- both of these are pretty global, we can look at their -- uses when single file behaviour works. structure TextDocumentItem where uri : DocumentUri languageId : String version : Nat text : String deriving ToJson, FromJson structure TextDocumentPositionParams where textDocument : TextDocumentIdentifier position : Position deriving ToJson, FromJson structure DocumentFilter where language? : Option String := none scheme? : Option String := none pattern? : Option String := none deriving ToJson, FromJson def DocumentSelector := Array DocumentFilter instance : FromJson DocumentSelector := ⟨@fromJson? (Array DocumentFilter) _⟩ instance : ToJson DocumentSelector := ⟨@toJson (Array DocumentFilter) _⟩ structure StaticRegistrationOptions where id? : Option String := none deriving ToJson, FromJson structure TextDocumentRegistrationOptions where documentSelector? : Option DocumentSelector := none deriving ToJson, FromJson inductive MarkupKind where | plaintext | markdown instance : FromJson MarkupKind := ⟨fun | str "plaintext" => Except.ok MarkupKind.plaintext | str "markdown" => Except.ok MarkupKind.markdown | _ => throw "unknown MarkupKind"⟩ instance : ToJson MarkupKind := ⟨fun | MarkupKind.plaintext => str "plaintext" | MarkupKind.markdown => str "markdown"⟩ structure MarkupContent where kind : MarkupKind value : String deriving ToJson, FromJson structure ProgressParams (α : Type) where token : String -- do we need `integer`? value : α deriving ToJson structure WorkDoneProgressReport where kind := "report" message? : Option String := none cancellable := false percentage? : Option Nat := none deriving ToJson structure WorkDoneProgressBegin extends WorkDoneProgressReport where kind := "begin" title : String deriving ToJson structure WorkDoneProgressEnd where kind := "end" message? : Option String := none deriving ToJson -- TODO(Marc): missing: -- WorkDoneProgressOptions, PartialResultParams end Lsp end Lean
9925f63db646980f4a8e9718912ab36924fdb1de
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/ring_theory/polynomial/pochhammer.lean
44f91fc803ffff06548d3f936c11511c96bbf863
[ "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
6,216
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import tactic.abel import data.polynomial.eval /-! # The Pochhammer polynomials We define and prove some basic relations about `pochhammer S n : polynomial S := X * (X + 1) * ... * (X + n - 1)` which is also known as the rising factorial. A version of this definition that is focused on `nat` can be found in `data.nat.factorial` as `asc_factorial`. ## Implementation As with many other families of polynomials, even though the coefficients are always in `ℕ`, we define the polynomial with coefficients in any `[semiring S]`. ## TODO There is lots more in this direction: * q-factorials, q-binomials, q-Pochhammer. -/ universes u v open polynomial section semiring variables (S : Type u) [semiring S] /-- `pochhammer S n` is the polynomial `X * (X+1) * ... * (X + n - 1)`, with coefficients in the semiring `S`. -/ noncomputable def pochhammer : ℕ → polynomial S | 0 := 1 | (n+1) := X * (pochhammer n).comp (X + 1) @[simp] lemma pochhammer_zero : pochhammer S 0 = 1 := rfl @[simp] lemma pochhammer_one : pochhammer S 1 = X := by simp [pochhammer] lemma pochhammer_succ_left (n : ℕ) : pochhammer S (n+1) = X * (pochhammer S n).comp (X+1) := by rw pochhammer section variables {S} {T : Type v} [semiring T] @[simp] lemma pochhammer_map (f : S →+* T) (n : ℕ) : (pochhammer S n).map f = pochhammer T n := begin induction n with n ih, { simp, }, { simp [ih, pochhammer_succ_left, map_comp], }, end end @[simp, norm_cast] lemma pochhammer_eval_cast (n k : ℕ) : ((pochhammer ℕ n).eval k : S) = (pochhammer S n).eval k := begin rw [←pochhammer_map (algebra_map ℕ S), eval_map, ←(algebra_map ℕ S).eq_nat_cast, eval₂_at_nat_cast, nat.cast_id, ring_hom.eq_nat_cast], end lemma pochhammer_eval_zero {n : ℕ} : (pochhammer S n).eval 0 = if n = 0 then 1 else 0 := begin cases n, { simp, }, { simp [X_mul, nat.succ_ne_zero, pochhammer_succ_left], } end lemma pochhammer_zero_eval_zero : (pochhammer S 0).eval 0 = 1 := by simp @[simp] lemma pochhammer_ne_zero_eval_zero {n : ℕ} (h : n ≠ 0) : (pochhammer S n).eval 0 = 0 := by simp [pochhammer_eval_zero, h] lemma pochhammer_succ_right (n : ℕ) : pochhammer S (n+1) = pochhammer S n * (X + n) := begin suffices h : pochhammer ℕ (n+1) = pochhammer ℕ n * (X + n), { apply_fun polynomial.map (algebra_map ℕ S) at h, simpa only [pochhammer_map, map_mul, map_add, map_X, map_nat_cast] using h, }, induction n with n ih, { simp, }, { conv_lhs { rw [pochhammer_succ_left, ih, mul_comp, ←mul_assoc, ←pochhammer_succ_left, add_comp, X_comp, nat_cast_comp, add_assoc, add_comm (1 : polynomial ℕ)], }, refl, }, end lemma polynomial.mul_X_add_nat_cast_comp {p q : polynomial S} {n : ℕ} : (p * (X + n)).comp q = (p.comp q) * (q + n) := by rw [mul_add, add_comp, mul_X_comp, ←nat.cast_comm, nat_cast_mul_comp, nat.cast_comm, mul_add] lemma pochhammer_mul (n m : ℕ) : pochhammer S n * (pochhammer S m).comp (X + n) = pochhammer S (n + m) := begin induction m with m ih, { simp, }, { rw [pochhammer_succ_right, polynomial.mul_X_add_nat_cast_comp, ←mul_assoc, ih, nat.succ_eq_add_one, ←add_assoc, pochhammer_succ_right, nat.cast_add, add_assoc], } end lemma pochhammer_nat_eq_asc_factorial (n : ℕ) : ∀ k, (pochhammer ℕ k).eval (n + 1) = n.asc_factorial k | 0 := by erw [eval_one]; refl | (t + 1) := begin rw [pochhammer_succ_right, eval_mul, pochhammer_nat_eq_asc_factorial t], suffices : n.asc_factorial t * (n + 1 + t) = n.asc_factorial (t + 1), by simpa, rw [nat.asc_factorial_succ, add_right_comm, mul_comm] end lemma pochhammer_nat_eq_desc_factorial (a b : ℕ) : (pochhammer ℕ b).eval a = (a + b - 1).desc_factorial b := begin cases b, { rw [nat.desc_factorial_zero, pochhammer_zero, polynomial.eval_one] }, rw [nat.add_succ, nat.succ_sub_succ, nat.sub_zero], cases a, { rw [pochhammer_ne_zero_eval_zero _ b.succ_ne_zero, zero_add, nat.desc_factorial_of_lt b.lt_succ_self] }, { rw [nat.succ_add, ←nat.add_succ, nat.add_desc_factorial_eq_asc_factorial, pochhammer_nat_eq_asc_factorial] } end end semiring section comm_semiring variables {S : Type*} [comm_semiring S] lemma pochhammer_succ_eval (n : ℕ) (k : S) : (pochhammer S n.succ).eval k = (pochhammer S n).eval k * (k + ↑n) := by rw [pochhammer_succ_right, polynomial.eval_mul, polynomial.eval_add, polynomial.eval_X, polynomial.eval_nat_cast] end comm_semiring section ordered_semiring variables {S : Type*} [ordered_semiring S] [nontrivial S] lemma pochhammer_pos (n : ℕ) (s : S) (h : 0 < s) : 0 < (pochhammer S n).eval s := begin induction n with n ih, { simp only [nat.nat_zero_eq_zero, pochhammer_zero, eval_one], exact zero_lt_one, }, { rw [pochhammer_succ_right, mul_add, eval_add, ←nat.cast_comm, eval_nat_cast_mul, eval_mul_X, nat.cast_comm, ←mul_add], exact mul_pos ih (lt_of_lt_of_le h ((le_add_iff_nonneg_right _).mpr (nat.cast_nonneg n))), } end end ordered_semiring section factorial open_locale nat variables (S : Type*) [semiring S] (r n : ℕ) @[simp] lemma pochhammer_eval_one (S : Type*) [semiring S] (n : ℕ) : (pochhammer S n).eval (1 : S) = (n! : S) := by rw_mod_cast [pochhammer_nat_eq_asc_factorial, nat.zero_asc_factorial] lemma factorial_mul_pochhammer (S : Type*) [semiring S] (r n : ℕ) : (r! : S) * (pochhammer S n).eval (r + 1) = (r + n)! := by rw_mod_cast [pochhammer_nat_eq_asc_factorial, nat.factorial_mul_asc_factorial] lemma pochhammer_nat_eval_succ (r : ℕ) : ∀ n : ℕ, n * (pochhammer ℕ r).eval (n + 1) = (n + r) * (pochhammer ℕ r).eval n | 0 := begin by_cases h : r = 0, { simp only [h, zero_mul, zero_add], }, { simp only [pochhammer_eval_zero, zero_mul, if_neg h, mul_zero], } end | (k + 1) := by simp only [pochhammer_nat_eq_asc_factorial, nat.succ_asc_factorial, add_right_comm] lemma pochhammer_eval_succ (r n : ℕ) : (n : S) * (pochhammer S r).eval (n + 1 : S) = (n + r) * (pochhammer S r).eval n := by exact_mod_cast congr_arg nat.cast (pochhammer_nat_eval_succ r n) end factorial
b25aa0cc8b9483e95e6585b946434b2357ff7783
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/measure_theory/measure/measure_space_def.lean
1669f42d69e1770376374987809a3fe1f273e61b
[ "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
24,934
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 /-! # Measure spaces > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. 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 sum of the measures 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, an outer 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 topology 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 := nonempty_iff_ne_empty.2 $ λ 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 {ι} [countable ι] (μ : ι → 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 [countable β] (s : β → set α) : μ (⋃ i, s i) ≤ ∑' i, μ (s i) := μ.to_outer_measure.Union _ lemma measure_bUnion_le {s : set β} (hs : s.countable) (f : β → set α) : μ (⋃ b ∈ s, f b) ≤ ∑' p : s, μ (f p) := by { haveI := hs.to_subtype, rw bUnion_eq_Union, apply measure_Union_le } 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 : s.finite) (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 [countable β] {s : β → set α} : (∀ i, μ (s i) = 0) → μ (⋃ i, s i) = 0 := μ.to_outer_measure.Union_null @[simp] lemma measure_Union_null_iff [countable ι] {s : ι → set α} : μ (⋃ i, s i) = 0 ↔ ∀ i, μ (s i) = 0 := μ.to_outer_measure.Union_null_iff /-- A version of `measure_Union_null_iff` for unions indexed by Props TODO: in the long run it would be better to combine this with `measure_Union_null_iff` by generalising to `Sort`. -/ @[simp] lemma measure_Union_null_iff' {ι : Prop} {s : ι → set α} : μ (⋃ i, s i) = 0 ↔ ∀ i, μ (s i) = 0 := μ.to_outer_measure.Union_null_iff' lemma measure_bUnion_null_iff {s : set ι} (hs : s.countable) {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 : S.countable) : μ (⋃₀ 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 [countable β] {s : β → set α} (hs : μ (⋃ n, s n) ≠ 0) : ∃ n, 0 < μ (s n) := begin contrapose! hs, exact measure_Union_null (λ n, nonpos_iff_eq_zero.1 (hs n)) 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, rw [mem_ae_iff, compl_sInter, sUnion_image], exact (measure_bUnion_null_iff hSc).2 hS end⟩ lemma ae_all_iff {ι : Sort*} [countable ι] {p : α → ι → Prop} : (∀ᵐ a ∂ μ, ∀ i, p a i) ↔ ∀ i, ∀ᵐ a ∂ μ, p a i := eventually_countable_forall lemma ae_ball_iff {S : set ι} (hS : S.countable) {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' lemma ae_le_set_union {s' t' : set α} (h : s ≤ᵐ[μ] t) (h' : s' ≤ᵐ[μ] t') : (s ∪ s' : set α) ≤ᵐ[μ] (t ∪ t' : set α) := h.union h' 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] @[simp] lemma measure_symm_diff_eq_zero_iff {s t : set α} : μ (s ∆ t) = 0 ↔ s =ᵐ[μ] t := by simp [ae_eq_set, symm_diff_def] @[simp] lemma ae_eq_set_compl_compl {s t : set α} : sᶜ =ᵐ[μ] tᶜ ↔ s =ᵐ[μ] t := by simp only [← measure_symm_diff_eq_zero_iff, compl_symm_diff_compl] lemma ae_eq_set_compl {s t : set α} : sᶜ =ᵐ[μ] t ↔ s =ᵐ[μ] tᶜ := by rw [← ae_eq_set_compl_compl, compl_compl] lemma ae_eq_set_inter {s' t' : set α} (h : s =ᵐ[μ] t) (h' : s' =ᵐ[μ] t') : (s ∩ s' : set α) =ᵐ[μ] (t ∩ t' : set α) := h.inter h' lemma ae_eq_set_union {s' t' : set α} (h : s =ᵐ[μ] t) (h' : s' =ᵐ[μ] t') : (s ∪ s' : set α) =ᵐ[μ] (t ∪ t' : set α) := h.union h' lemma union_ae_eq_univ_of_ae_eq_univ_left (h : s =ᵐ[μ] univ) : (s ∪ t : set α) =ᵐ[μ] univ := by { convert ae_eq_set_union h (ae_eq_refl t), rw univ_union, } lemma union_ae_eq_univ_of_ae_eq_univ_right (h : t =ᵐ[μ] univ) : (s ∪ t : set α) =ᵐ[μ] univ := by { convert ae_eq_set_union (ae_eq_refl s) h, rw union_univ, } lemma union_ae_eq_right_of_ae_eq_empty (h : s =ᵐ[μ] (∅ : set α)) : (s ∪ t : set α) =ᵐ[μ] t := by { convert ae_eq_set_union h (ae_eq_refl t), rw empty_union, } lemma union_ae_eq_left_of_ae_eq_empty (h : t =ᵐ[μ] (∅ : set α)) : (s ∪ t : set α) =ᵐ[μ] s := by { convert ae_eq_set_union (ae_eq_refl s) h, rw union_empty, } lemma inter_ae_eq_right_of_ae_eq_univ (h : s =ᵐ[μ] univ) : (s ∩ t : set α) =ᵐ[μ] t := by { convert ae_eq_set_inter h (ae_eq_refl t), rw univ_inter, } lemma inter_ae_eq_left_of_ae_eq_univ (h : t =ᵐ[μ] univ) : (s ∩ t : set α) =ᵐ[μ] s := by { convert ae_eq_set_inter (ae_eq_refl s) h, rw inter_univ, } lemma inter_ae_eq_empty_of_ae_eq_empty_left (h : s =ᵐ[μ] (∅ : set α)) : (s ∩ t : set α) =ᵐ[μ] (∅ : set α) := by { convert ae_eq_set_inter h (ae_eq_refl t), rw empty_inter, } lemma inter_ae_eq_empty_of_ae_eq_empty_right (h : t =ᵐ[μ] (∅ : set α)) : (s ∩ t : set α) =ᵐ[μ] (∅ : set α) := by { convert ae_eq_set_inter (ae_eq_refl s) h, rw inter_empty, } @[to_additive] lemma _root_.set.mul_indicator_ae_eq_one {M : Type*} [has_one M] {f : α → M} {s : set α} : s.mul_indicator f =ᵐ[μ] 1 ↔ μ (s ∩ f.mul_support) = 0 := by simpa [eventually_eq, eventually_iff, measure.ae, compl_set_of] /-- 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 ← _root_.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 ← _root_.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
9c881a1dc380b8f4465b629abf92749551f61123
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/data/char/basic.lean
aeeaf6ca4a08148ac137b63bab5b5c92249073b4
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
2,416
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import init.data.nat.basic open nat @[reducible] def is_valid_char (n : nat) : Prop := n < 0xd800 ∨ (0xdfff < n ∧ n < 0x110000) lemma is_valid_char_range_1 (n : nat) (h : n < 0xd800) : is_valid_char n := or.inl h lemma is_valid_char_range_2 (n : nat) (h₁ : 0xdfff < n) (h₂ : n < 0x110000) : is_valid_char n := or.inr ⟨h₁, h₂⟩ /-- The `char` type represents an unicode scalar value. See http://www.unicode.org/glossary/#unicode_scalar_value). -/ structure char := (val : nat) (valid : is_valid_char val) instance : has_sizeof char := ⟨λ c, c.val⟩ namespace char protected def lt (a b : char) : Prop := a.val < b.val protected def le (a b : char) : Prop := a.val ≤ b.val instance : has_lt char := ⟨char.lt⟩ instance : has_le char := ⟨char.le⟩ instance decidable_lt (a b : char) : decidable (a < b) := nat.decidable_lt _ _ instance decidable_le (a b : char) : decidable (a ≤ b) := nat.decidable_le _ _ /- We cannot use tactics dec_trivial or comp_val here because the tactic framework has not been defined yet. We also do not use `zero_lt_succ _` as a proof term because this proof may not be trivial to check by external type checkers. See discussion at: https://github.com/leanprover/tc/issues/8 -/ lemma zero_lt_d800 : 0 < 0xd800 := nat.zero_lt_bit0 $ nat.bit0_ne_zero $ nat.bit0_ne_zero $ nat.bit0_ne_zero $ nat.bit0_ne_zero $ nat.bit0_ne_zero $ nat.bit0_ne_zero $ nat.bit0_ne_zero $ nat.bit0_ne_zero $ nat.bit0_ne_zero $ nat.bit0_ne_zero $ nat.bit1_ne_zero 13 @[pattern] def of_nat (n : nat) : char := if h : is_valid_char n then {val := n, valid := h} else {val := 0, valid := or.inl zero_lt_d800} def to_nat (c : char) : nat := c.val lemma eq_of_veq : ∀ {c d : char}, c.val = d.val → c = d | ⟨v, h⟩ ⟨_, _⟩ rfl := rfl lemma veq_of_eq : ∀ {c d : char}, c = d → c.val = d.val | _ _ rfl := rfl lemma ne_of_vne {c d : char} (h : c.val ≠ d.val) : c ≠ d := λ h', absurd (veq_of_eq h') h lemma vne_of_ne {c d : char} (h : c ≠ d) : c.val ≠ d.val := λ h', absurd (eq_of_veq h') h end char instance : decidable_eq char := λ i j, decidable_of_decidable_of_iff (nat.decidable_eq i.val j.val) ⟨char.eq_of_veq, char.veq_of_eq⟩ instance : inhabited char := ⟨'A'⟩
5d1c22e83b6e334687b67c7710acc540a213673f
137c667471a40116a7afd7261f030b30180468c2
/src/linear_algebra/matrix/determinant.lean
104b18604bd8caa74a8f8d620f5977c4f52510ed
[ "Apache-2.0" ]
permissive
bragadeesh153/mathlib
46bf814cfb1eecb34b5d1549b9117dc60f657792
b577bb2cd1f96eb47031878256856020b76f73cd
refs/heads/master
1,687,435,188,334
1,626,384,207,000
1,626,384,207,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
28,556
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes, Tim Baanen -/ import data.matrix.pequiv import data.matrix.block import data.fintype.card import group_theory.perm.fin import group_theory.perm.sign import algebra.algebra.basic import tactic.ring import linear_algebra.alternating import linear_algebra.pi /-! # Determinant of a matrix This file defines the determinant of a matrix, `matrix.det`, and its essential properties. ## Main definitions - `matrix.det`: the determinant of a square matrix, as a sum over permutations - `matrix.det_row_multilinear`: the determinant, as an `alternating_map` in the rows of the matrix ## Main results - `det_mul`: the determinant of `A ⬝ B` is the product of determinants - `det_zero_of_row_eq`: the determinant is zero if there is a repeated row - `det_block_diagonal`: the determinant of a block diagonal matrix is a product of the blocks' determinants ## Implementation notes It is possible to configure `simp` to compute determinants. See the file `test/matrix.lean` for some examples. -/ universes u v w z open equiv equiv.perm finset function namespace matrix open_locale matrix big_operators variables {m n : Type*} [decidable_eq n] [fintype n] [decidable_eq m] [fintype m] variables {R : Type v} [comm_ring R] local notation `ε` σ:max := ((sign σ : ℤ ) : R) /-- `det` is an `alternating_map` in the rows of the matrix. -/ def det_row_multilinear : alternating_map R (n → R) R n := ((multilinear_map.mk_pi_algebra R n R).comp_linear_map (linear_map.proj)).alternatization /-- The determinant of a matrix given by the Leibniz formula. -/ abbreviation det (M : matrix n n R) : R := det_row_multilinear M lemma det_apply (M : matrix n n R) : M.det = ∑ σ : perm n, σ.sign • ∏ i, M (σ i) i := multilinear_map.alternatization_apply _ M -- This is what the old definition was. We use it to avoid having to change the old proofs below lemma det_apply' (M : matrix n n R) : M.det = ∑ σ : perm n, ε σ * ∏ i, M (σ i) i := by simp [det_apply, units.smul_def] @[simp] lemma det_diagonal {d : n → R} : det (diagonal d) = ∏ i, d i := begin rw det_apply', refine (finset.sum_eq_single 1 _ _).trans _, { intros σ h1 h2, cases not_forall.1 (mt equiv.ext h2) with x h3, convert mul_zero _, apply finset.prod_eq_zero, { change x ∈ _, simp }, exact if_neg h3 }, { simp }, { simp } end @[simp] lemma det_zero (h : nonempty n) : det (0 : matrix n n R) = 0 := (det_row_multilinear : alternating_map R (n → R) R n).map_zero @[simp] lemma det_one : det (1 : matrix n n R) = 1 := by rw [← diagonal_one]; simp [-diagonal_one] lemma det_eq_one_of_card_eq_zero {A : matrix n n R} (h : fintype.card n = 0) : det A = 1 := begin have perm_eq : (univ : finset (perm n)) = {1} := univ_eq_singleton_of_card_one (1 : perm n) (by simp [card_univ, fintype.card_perm, h]), simp [det_apply, card_eq_zero.mp h, perm_eq], end /-- Specialize `det_eq_one_of_card_eq_zero` to `fin 0`. This is especially useful in combination with the `det_succ_` lemmas, for computing the determinant of a matrix given in the `![...]` notation. -/ @[simp] lemma det_fin_zero {A : matrix (fin 0) (fin 0) R}: det A = 1 := det_eq_one_of_card_eq_zero (fintype.card_fin _) /-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element. Although `unique` implies `decidable_eq` and `fintype`, the instances might not be syntactically equal. Thus, we need to fill in the args explicitly. -/ @[simp] lemma det_unique {n : Type*} [unique n] [decidable_eq n] [fintype n] (A : matrix n n R) : det A = A (default n) (default n) := by simp [det_apply, univ_unique] lemma det_eq_elem_of_card_eq_one {A : matrix n n R} (h : fintype.card n = 1) (k : n) : det A = A k k := begin have h1 : (univ : finset (perm n)) = {1}, { apply univ_eq_singleton_of_card_one (1 : perm n), simp [card_univ, fintype.card_perm, h] }, have h2 := univ_eq_singleton_of_card_one k h, simp [det_apply, h1, h2], end lemma det_mul_aux {M N : matrix n n R} {p : n → n} (H : ¬bijective p) : ∑ σ : perm n, (ε σ) * ∏ x, (M (σ x) (p x) * N (p x) x) = 0 := begin obtain ⟨i, j, hpij, hij⟩ : ∃ i j, p i = p j ∧ i ≠ j, { rw [← fintype.injective_iff_bijective, injective] at H, push_neg at H, exact H }, exact sum_involution (λ σ _, σ * swap i j) (λ σ _, have ∏ x, M (σ x) (p x) = ∏ x, M ((σ * swap i j) x) (p x), from fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij]), by simp [this, sign_swap hij, prod_mul_distrib]) (λ σ _ _, (not_congr mul_swap_eq_iff).mpr hij) (λ _ _, mem_univ _) (λ σ _, mul_swap_involutive i j σ) end @[simp] lemma det_mul (M N : matrix n n R) : det (M ⬝ N) = det M * det N := calc det (M ⬝ N) = ∑ p : n → n, ∑ σ : perm n, ε σ * ∏ i, (M (σ i) (p i) * N (p i) i) : by simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, fintype.pi_finset_univ]; rw [finset.sum_comm] ... = ∑ p in (@univ (n → n) _).filter bijective, ∑ σ : perm n, ε σ * ∏ i, (M (σ i) (p i) * N (p i) i) : eq.symm $ sum_subset (filter_subset _ _) (λ f _ hbij, det_mul_aux $ by simpa only [true_and, mem_filter, mem_univ] using hbij) ... = ∑ τ : perm n, ∑ σ : perm n, ε σ * ∏ i, (M (σ i) (τ i) * N (τ i) i) : sum_bij (λ p h, equiv.of_bijective p (mem_filter.1 h).2) (λ _ _, mem_univ _) (λ _ _, rfl) (λ _ _ _ _ h, by injection h) (λ b _, ⟨b, mem_filter.2 ⟨mem_univ _, b.bijective⟩, coe_fn_injective rfl⟩) ... = ∑ σ : perm n, ∑ τ : perm n, (∏ i, N (σ i) i) * ε τ * (∏ j, M (τ j) (σ j)) : by simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc] ... = ∑ σ : perm n, ∑ τ : perm n, (((∏ i, N (σ i) i) * (ε σ * ε τ)) * ∏ i, M (τ i) i) : sum_congr rfl (λ σ _, fintype.sum_equiv (equiv.mul_right σ⁻¹) _ _ (λ τ, have ∏ j, M (τ j) (σ j) = ∏ j, M ((τ * σ⁻¹) j) j, by { rw ← σ⁻¹.prod_comp, simp only [equiv.perm.coe_mul, apply_inv_self] }, have h : ε σ * ε (τ * σ⁻¹) = ε τ := calc ε σ * ε (τ * σ⁻¹) = ε ((τ * σ⁻¹) * σ) : by { rw [mul_comm, sign_mul (τ * σ⁻¹)], simp only [int.cast_mul, units.coe_mul] } ... = ε τ : by simp only [inv_mul_cancel_right], by { simp_rw [equiv.coe_mul_right, h], simp only [this] })) ... = det M * det N : by simp only [det_apply', finset.mul_sum, mul_comm, mul_left_comm] instance : is_monoid_hom (det : matrix n n R → R) := { map_one := det_one, map_mul := det_mul } /-- On square matrices, `mul_comm` applies under `det`. -/ lemma det_mul_comm (M N : matrix m m R) : det (M ⬝ N) = det (N ⬝ M) := by rw [det_mul, det_mul, mul_comm] /-- On square matrices, `mul_left_comm` applies under `det`. -/ lemma det_mul_left_comm (M N P : matrix m m R) : det (M ⬝ (N ⬝ P)) = det (N ⬝ (M ⬝ P)) := by rw [←matrix.mul_assoc, ←matrix.mul_assoc, det_mul, det_mul_comm M N, ←det_mul] /-- On square matrices, `mul_right_comm` applies under `det`. -/ lemma det_mul_right_comm (M N P : matrix m m R) : det (M ⬝ N ⬝ P) = det (M ⬝ P ⬝ N) := by rw [matrix.mul_assoc, matrix.mul_assoc, det_mul, det_mul_comm N P, ←det_mul] lemma det_units_conj (M : units (matrix m m R)) (N : matrix m m R) : det (↑M ⬝ N ⬝ ↑M⁻¹ : matrix m m R) = det N := by rw [det_mul_right_comm, ←mul_eq_mul, ←mul_eq_mul, units.mul_inv, one_mul] lemma det_units_conj' (M : units (matrix m m R)) (N : matrix m m R) : det (↑M⁻¹ ⬝ N ⬝ ↑M : matrix m m R) = det N := det_units_conj M⁻¹ N /-- Transposing a matrix preserves the determinant. -/ @[simp] lemma det_transpose (M : matrix n n R) : Mᵀ.det = M.det := begin rw [det_apply', det_apply'], refine fintype.sum_bijective _ inv_involutive.bijective _ _ _, intros σ, rw sign_inv, congr' 1, apply fintype.prod_equiv σ, intros, simp end /-- Permuting the columns changes the sign of the determinant. -/ lemma det_permute (σ : perm n) (M : matrix n n R) : matrix.det (λ i, M (σ i)) = σ.sign * M.det := ((det_row_multilinear : alternating_map R (n → R) R n).map_perm M σ).trans (by simp [units.smul_def]) /-- Permuting rows and columns with the same equivalence has no effect. -/ @[simp] lemma det_minor_equiv_self (e : n ≃ m) (A : matrix m m R) : det (A.minor e e) = det A := begin rw [det_apply', det_apply'], apply fintype.sum_equiv (equiv.perm_congr e), intro σ, rw equiv.perm.sign_perm_congr e σ, congr' 1, apply fintype.prod_equiv e, intro i, rw [equiv.perm_congr_apply, equiv.symm_apply_apply, minor_apply], end /-- Reindexing both indices along the same equivalence preserves the determinant. For the `simp` version of this lemma, see `det_minor_equiv_self`; this one is unsuitable because `matrix.reindex_apply` unfolds `reindex` first. -/ lemma det_reindex_self (e : m ≃ n) (A : matrix m m R) : det (reindex e e A) = det A := det_minor_equiv_self e.symm A /-- The determinant of a permutation matrix equals its sign. -/ @[simp] lemma det_permutation (σ : perm n) : matrix.det (σ.to_pequiv.to_matrix : matrix n n R) = σ.sign := by rw [←matrix.mul_one (σ.to_pequiv.to_matrix : matrix n n R), pequiv.to_pequiv_mul_matrix, det_permute, det_one, mul_one] @[simp] lemma det_smul {A : matrix n n R} {c : R} : det (c • A) = c ^ fintype.card n * det A := calc det (c • A) = det (matrix.mul (diagonal (λ _, c)) A) : by rw [smul_eq_diagonal_mul] ... = det (diagonal (λ _, c)) * det A : det_mul _ _ ... = c ^ fintype.card n * det A : by simp [card_univ] /-- Multiplying each row by a fixed `v i` multiplies the determinant by the product of the `v`s. -/ lemma det_mul_row (v : n → R) (A : matrix n n R) : det (λ i j, v j * A i j) = (∏ i, v i) * det A := calc det (λ i j, v j * A i j) = det (A ⬝ diagonal v) : congr_arg det $ by { ext, simp [mul_comm] } ... = (∏ i, v i) * det A : by rw [det_mul, det_diagonal, mul_comm] /-- Multiplying each column by a fixed `v j` multiplies the determinant by the product of the `v`s. -/ lemma det_mul_column (v : n → R) (A : matrix n n R) : det (λ i j, v i * A i j) = (∏ i, v i) * det A := multilinear_map.map_smul_univ _ v A section hom_map variables {S : Type w} [comm_ring S] lemma ring_hom.map_det {M : matrix n n R} {f : R →+* S} : f M.det = matrix.det (f.map_matrix M) := by simp [matrix.det_apply', f.map_sum, f.map_prod] lemma alg_hom.map_det [algebra R S] {T : Type z} [comm_ring T] [algebra R T] {M : matrix n n S} {f : S →ₐ[R] T} : f M.det = matrix.det ((f : S →+* T).map_matrix M) := by rw [← alg_hom.coe_to_ring_hom, ring_hom.map_det] end hom_map section det_zero /-! ### `det_zero` section Prove that a matrix with a repeated column has determinant equal to zero. -/ lemma det_eq_zero_of_row_eq_zero {A : matrix n n R} (i : n) (h : ∀ j, A i j = 0) : det A = 0 := (det_row_multilinear : alternating_map R (n → R) R n).map_coord_zero i (funext h) lemma det_eq_zero_of_column_eq_zero {A : matrix n n R} (j : n) (h : ∀ i, A i j = 0) : det A = 0 := by { rw ← det_transpose, exact det_eq_zero_of_row_eq_zero j h, } variables {M : matrix n n R} {i j : n} /-- If a matrix has a repeated row, the determinant will be zero. -/ theorem det_zero_of_row_eq (i_ne_j : i ≠ j) (hij : M i = M j) : M.det = 0 := (det_row_multilinear : alternating_map R (n → R) R n).map_eq_zero_of_eq M hij i_ne_j /-- If a matrix has a repeated column, the determinant will be zero. -/ theorem det_zero_of_column_eq (i_ne_j : i ≠ j) (hij : ∀ k, M k i = M k j) : M.det = 0 := by { rw [← det_transpose, det_zero_of_row_eq i_ne_j], exact funext hij } end det_zero lemma det_update_row_add (M : matrix n n R) (j : n) (u v : n → R) : det (update_row M j $ u + v) = det (update_row M j u) + det (update_row M j v) := (det_row_multilinear : alternating_map R (n → R) R n).map_add M j u v lemma det_update_column_add (M : matrix n n R) (j : n) (u v : n → R) : det (update_column M j $ u + v) = det (update_column M j u) + det (update_column M j v) := begin rw [← det_transpose, ← update_row_transpose, det_update_row_add], simp [update_row_transpose, det_transpose] end lemma det_update_row_smul (M : matrix n n R) (j : n) (s : R) (u : n → R) : det (update_row M j $ s • u) = s * det (update_row M j u) := (det_row_multilinear : alternating_map R (n → R) R n).map_smul M j s u lemma det_update_column_smul (M : matrix n n R) (j : n) (s : R) (u : n → R) : det (update_column M j $ s • u) = s * det (update_column M j u) := begin rw [← det_transpose, ← update_row_transpose, det_update_row_smul], simp [update_row_transpose, det_transpose] end section det_eq /-! ### `det_eq` section Lemmas showing the determinant is invariant under a variety of operations. -/ lemma det_eq_of_eq_mul_det_one {A B : matrix n n R} (C : matrix n n R) (hC : det C = 1) (hA : A = B ⬝ C) : det A = det B := calc det A = det (B ⬝ C) : congr_arg _ hA ... = det B * det C : det_mul _ _ ... = det B : by rw [hC, mul_one] lemma det_eq_of_eq_det_one_mul {A B : matrix n n R} (C : matrix n n R) (hC : det C = 1) (hA : A = C ⬝ B) : det A = det B := calc det A = det (C ⬝ B) : congr_arg _ hA ... = det C * det B : det_mul _ _ ... = det B : by rw [hC, one_mul] lemma det_update_row_add_self (A : matrix n n R) {i j : n} (hij : i ≠ j) : det (update_row A i (A i + A j)) = det A := by simp [det_update_row_add, det_zero_of_row_eq hij ((update_row_self).trans (update_row_ne hij.symm).symm)] lemma det_update_column_add_self (A : matrix n n R) {i j : n} (hij : i ≠ j) : det (update_column A i (λ k, A k i + A k j)) = det A := by { rw [← det_transpose, ← update_row_transpose, ← det_transpose A], exact det_update_row_add_self Aᵀ hij } lemma det_update_row_add_smul_self (A : matrix n n R) {i j : n} (hij : i ≠ j) (c : R) : det (update_row A i (A i + c • A j)) = det A := by simp [det_update_row_add, det_update_row_smul, det_zero_of_row_eq hij ((update_row_self).trans (update_row_ne hij.symm).symm)] lemma det_update_column_add_smul_self (A : matrix n n R) {i j : n} (hij : i ≠ j) (c : R) : det (update_column A i (λ k, A k i + c • A k j)) = det A := by { rw [← det_transpose, ← update_row_transpose, ← det_transpose A], exact det_update_row_add_smul_self Aᵀ hij c } lemma det_eq_of_forall_row_eq_smul_add_const_aux {A B : matrix n n R} {s : finset n} : ∀ (c : n → R) (hs : ∀ i, i ∉ s → c i = 0) (k : n) (hk : k ∉ s) (A_eq : ∀ i j, A i j = B i j + c i * B k j), det A = det B := begin revert B, refine s.induction_on _ _, { intros A c hs k hk A_eq, have : ∀ i, c i = 0, { intros i, specialize hs i, contrapose! hs, simp [hs] }, congr, ext i j, rw [A_eq, this, zero_mul, add_zero], }, { intros i s hi ih B c hs k hk A_eq, have hAi : A i = B i + c i • B k := funext (A_eq i), rw [@ih (update_row B i (A i)) (function.update c i 0), hAi, det_update_row_add_smul_self], { exact mt (λ h, show k ∈ insert i s, from h ▸ finset.mem_insert_self _ _) hk }, { intros i' hi', rw function.update_apply, split_ifs with hi'i, { refl }, { exact hs i' (λ h, hi' ((finset.mem_insert.mp h).resolve_left hi'i)) } }, { exact λ h, hk (finset.mem_insert_of_mem h) }, { intros i' j', rw [update_row_apply, function.update_apply], split_ifs with hi'i, { simp [hi'i] }, rw [A_eq, update_row_ne (λ (h : k = i), hk $ h ▸ finset.mem_insert_self k s)] } } end /-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/ lemma det_eq_of_forall_row_eq_smul_add_const {A B : matrix n n R} (c : n → R) (k : n) (hk : c k = 0) (A_eq : ∀ i j, A i j = B i j + c i * B k j) : det A = det B := det_eq_of_forall_row_eq_smul_add_const_aux c (λ i, not_imp_comm.mp $ λ hi, finset.mem_erase.mpr ⟨mt (λ (h : i = k), show c i = 0, from h.symm ▸ hk) hi, finset.mem_univ i⟩) k (finset.not_mem_erase k finset.univ) A_eq lemma det_eq_of_forall_row_eq_smul_add_pred_aux {n : ℕ} (k : fin (n + 1)) : ∀ (c : fin n → R) (hc : ∀ (i : fin n), k < i.succ → c i = 0) {M N : matrix (fin n.succ) (fin n.succ) R} (h0 : ∀ j, M 0 j = N 0 j) (hsucc : ∀ (i : fin n) j, M i.succ j = N i.succ j + c i * M i.cast_succ j), det M = det N := begin refine fin.induction _ (λ k ih, _) k; intros c hc M N h0 hsucc, { congr, ext i j, refine fin.cases (h0 j) (λ i, _) i, rw [hsucc, hc i (fin.succ_pos _), zero_mul, add_zero] }, set M' := update_row M k.succ (N k.succ) with hM', have hM : M = update_row M' k.succ (M' k.succ + c k • M k.cast_succ), { ext i j, by_cases hi : i = k.succ, { simp [hi, hM', hsucc, update_row_self] }, rw [update_row_ne hi, hM', update_row_ne hi] }, have k_ne_succ : k.cast_succ ≠ k.succ := (fin.cast_succ_lt_succ k).ne, have M_k : M k.cast_succ = M' k.cast_succ := (update_row_ne k_ne_succ).symm, rw [hM, M_k, det_update_row_add_smul_self M' k_ne_succ.symm, ih (function.update c k 0)], { intros i hi, rw [fin.lt_iff_coe_lt_coe, fin.coe_cast_succ, fin.coe_succ, nat.lt_succ_iff] at hi, rw function.update_apply, split_ifs with hik, { refl }, exact hc _ (fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (ne.symm hik))) }, { rwa [hM', update_row_ne (fin.succ_ne_zero _).symm] }, intros i j, rw function.update_apply, split_ifs with hik, { rw [zero_mul, add_zero, hM', hik, update_row_self] }, rw [hM', update_row_ne ((fin.succ_injective _).ne hik), hsucc], by_cases hik2 : k < i, { simp [hc i (fin.succ_lt_succ_iff.mpr hik2)] }, rw update_row_ne, apply ne_of_lt, rwa [fin.lt_iff_coe_lt_coe, fin.coe_cast_succ, fin.coe_succ, nat.lt_succ_iff, ← not_lt] end /-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/ lemma det_eq_of_forall_row_eq_smul_add_pred {n : ℕ} {A B : matrix (fin (n + 1)) (fin (n + 1)) R} (c : fin n → R) (A_zero : ∀ j, A 0 j = B 0 j) (A_succ : ∀ (i : fin n) j, A i.succ j = B i.succ j + c i * A i.cast_succ j) : det A = det B := det_eq_of_forall_row_eq_smul_add_pred_aux (fin.last _) c (λ i hi, absurd hi (not_lt_of_ge (fin.le_last _))) A_zero A_succ /-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/ lemma det_eq_of_forall_col_eq_smul_add_pred {n : ℕ} {A B : matrix (fin (n + 1)) (fin (n + 1)) R} (c : fin n → R) (A_zero : ∀ i, A i 0 = B i 0) (A_succ : ∀ i (j : fin n), A i j.succ = B i j.succ + c j * A i j.cast_succ) : det A = det B := by { rw [← det_transpose A, ← det_transpose B], exact det_eq_of_forall_row_eq_smul_add_pred c A_zero (λ i j, A_succ j i) } end det_eq @[simp] lemma det_block_diagonal {o : Type*} [fintype o] [decidable_eq o] (M : o → matrix n n R) : (block_diagonal M).det = ∏ k, (M k).det := begin -- Rewrite the determinants as a sum over permutations. simp_rw [det_apply'], -- The right hand side is a product of sums, rewrite it as a sum of products. rw finset.prod_sum, simp_rw [finset.mem_univ, finset.prod_attach_univ, finset.univ_pi_univ], -- We claim that the only permutations contributing to the sum are those that -- preserve their second component. let preserving_snd : finset (equiv.perm (n × o)) := finset.univ.filter (λ σ, ∀ x, (σ x).snd = x.snd), have mem_preserving_snd : ∀ {σ : equiv.perm (n × o)}, σ ∈ preserving_snd ↔ ∀ x, (σ x).snd = x.snd := λ σ, finset.mem_filter.trans ⟨λ h, h.2, λ h, ⟨finset.mem_univ _, h⟩⟩, rw ← finset.sum_subset (finset.subset_univ preserving_snd) _, -- And that these are in bijection with `o → equiv.perm m`. rw (finset.sum_bij (λ (σ : ∀ (k : o), k ∈ finset.univ → equiv.perm n) _, prod_congr_left (λ k, σ k (finset.mem_univ k))) _ _ _ _).symm, { intros σ _, rw mem_preserving_snd, rintros ⟨k, x⟩, simp only [prod_congr_left_apply] }, { intros σ _, rw [finset.prod_mul_distrib, ←finset.univ_product_univ, finset.prod_product, finset.prod_comm], simp only [sign_prod_congr_left, units.coe_prod, int.cast_prod, block_diagonal_apply_eq, prod_congr_left_apply] }, { intros σ σ' _ _ eq, ext x hx k, simp only at eq, have : ∀ k x, prod_congr_left (λ k, σ k (finset.mem_univ _)) (k, x) = prod_congr_left (λ k, σ' k (finset.mem_univ _)) (k, x) := λ k x, by rw eq, simp only [prod_congr_left_apply, prod.mk.inj_iff] at this, exact (this k x).1 }, { intros σ hσ, rw mem_preserving_snd at hσ, have hσ' : ∀ x, (σ⁻¹ x).snd = x.snd, { intro x, conv_rhs { rw [← perm.apply_inv_self σ x, hσ] } }, have mk_apply_eq : ∀ k x, ((σ (x, k)).fst, k) = σ (x, k), { intros k x, ext, { simp only}, { simp only [hσ] } }, have mk_inv_apply_eq : ∀ k x, ((σ⁻¹ (x, k)).fst, k) = σ⁻¹ (x, k), { intros k x, conv_lhs { rw ← perm.apply_inv_self σ (x, k) }, ext, { simp only [apply_inv_self] }, { simp only [hσ'] } }, refine ⟨λ k _, ⟨λ x, (σ (x, k)).fst, λ x, (σ⁻¹ (x, k)).fst, _, _⟩, _, _⟩, { intro x, simp only [mk_apply_eq, inv_apply_self] }, { intro x, simp only [mk_inv_apply_eq, apply_inv_self] }, { apply finset.mem_univ }, { ext ⟨k, x⟩, { simp only [coe_fn_mk, prod_congr_left_apply] }, { simp only [prod_congr_left_apply, hσ] } } }, { intros σ _ hσ, rw mem_preserving_snd at hσ, obtain ⟨⟨k, x⟩, hkx⟩ := not_forall.mp hσ, rw [finset.prod_eq_zero (finset.mem_univ (k, x)), mul_zero], rw [← @prod.mk.eta _ _ (σ (k, x)), block_diagonal_apply_ne], exact hkx } end /-- The determinant of a 2x2 block matrix with the lower-left block equal to zero is the product of the determinants of the diagonal blocks. For the generalization to any number of blocks, see `matrix.upper_block_triangular_det`. -/ lemma upper_two_block_triangular_det (A : matrix m m R) (B : matrix m n R) (D : matrix n n R) : (matrix.from_blocks A B 0 D).det = A.det * D.det := begin classical, simp_rw det_apply', convert (sum_subset (subset_univ ((sum_congr_hom m n).range : set (perm (m ⊕ n))).to_finset) _).symm, rw sum_mul_sum, simp_rw univ_product_univ, rw (sum_bij (λ (σ : perm m × perm n) _, equiv.sum_congr σ.fst σ.snd) _ _ _ _).symm, { intros σ₁₂ h, simp only [], erw [set.mem_to_finset, monoid_hom.mem_range], use σ₁₂, simp only [sum_congr_hom_apply] }, { simp only [forall_prop_of_true, prod.forall, mem_univ], intros σ₁ σ₂, rw fintype.prod_sum_type, simp_rw [equiv.sum_congr_apply, sum.map_inr, sum.map_inl, from_blocks_apply₁₁, from_blocks_apply₂₂], have hr : ∀ (a b c d : R), (a * b) * (c * d) = a * c * (b * d), { intros, ac_refl }, rw hr, congr, rw [sign_sum_congr, units.coe_mul, int.cast_mul] }, { intros σ₁ σ₂ h₁ h₂, dsimp only [], intro h, have h2 : ∀ x, perm.sum_congr σ₁.fst σ₁.snd x = perm.sum_congr σ₂.fst σ₂.snd x, { intro x, exact congr_fun (congr_arg to_fun h) x }, simp only [sum.map_inr, sum.map_inl, perm.sum_congr_apply, sum.forall] at h2, ext, { exact h2.left x }, { exact h2.right x }}, { intros σ hσ, erw [set.mem_to_finset, monoid_hom.mem_range] at hσ, obtain ⟨σ₁₂, hσ₁₂⟩ := hσ, use σ₁₂, rw ←hσ₁₂, simp }, { intros σ hσ hσn, have h1 : ¬ (∀ x, ∃ y, sum.inl y = σ (sum.inl x)), { by_contradiction, rw set.mem_to_finset at hσn, apply absurd (mem_sum_congr_hom_range_of_perm_maps_to_inl _) hσn, rintros x ⟨a, ha⟩, rw [←ha], exact h a }, obtain ⟨a, ha⟩ := not_forall.mp h1, cases hx : σ (sum.inl a) with a2 b, { have hn := (not_exists.mp ha) a2, exact absurd hx.symm hn }, { rw [finset.prod_eq_zero (finset.mem_univ (sum.inl a)), mul_zero], rw [hx, from_blocks_apply₂₁], refl }} end /-- Laplacian expansion of the determinant of an `n+1 × n+1` matrix along column 0. -/ lemma det_succ_column_zero {n : ℕ} (A : matrix (fin n.succ) (fin n.succ) R) : det A = ∑ i : fin n.succ, (-1) ^ (i : ℕ) * A i 0 * det (A.minor i.succ_above fin.succ) := begin rw [matrix.det_apply, finset.univ_perm_fin_succ, ← finset.univ_product_univ], simp only [finset.sum_map, equiv.to_embedding_apply, finset.sum_product, matrix.minor], refine finset.sum_congr rfl (λ i _, fin.cases _ (λ i, _) i), { simp only [fin.prod_univ_succ, matrix.det_apply, finset.mul_sum, equiv.perm.decompose_fin_symm_apply_zero, fin.coe_zero, one_mul, equiv.perm.decompose_fin.symm_sign, equiv.swap_self, if_true, id.def, eq_self_iff_true, equiv.perm.decompose_fin_symm_apply_succ, fin.succ_above_zero, equiv.coe_refl, pow_zero, mul_smul_comm] }, -- `univ_perm_fin_succ` gives a different embedding of `perm (fin n)` into -- `perm (fin n.succ)` than the determinant of the submatrix we want, -- permute `A` so that we get the correct one. have : (-1 : R) ^ (i : ℕ) = i.cycle_range.sign, { simp [fin.sign_cycle_range] }, rw [fin.coe_succ, pow_succ, this, mul_assoc, mul_assoc, mul_left_comm ↑(equiv.perm.sign _), ← det_permute, matrix.det_apply, finset.mul_sum, finset.mul_sum], -- now we just need to move the corresponding parts to the same place refine finset.sum_congr rfl (λ σ _, _), rw [equiv.perm.decompose_fin.symm_sign, if_neg (fin.succ_ne_zero i)], calc ((-1) * σ.sign : ℤ) • ∏ i', A (equiv.perm.decompose_fin.symm (fin.succ i, σ) i') i' = ((-1) * σ.sign : ℤ) • (A (fin.succ i) 0 * ∏ i', A (((fin.succ i).succ_above) (fin.cycle_range i (σ i'))) i'.succ) : by simp only [fin.prod_univ_succ, fin.succ_above_cycle_range, equiv.perm.decompose_fin_symm_apply_zero, equiv.perm.decompose_fin_symm_apply_succ] ... = (-1) * (A (fin.succ i) 0 * (σ.sign : ℤ) • ∏ i', A (((fin.succ i).succ_above) (fin.cycle_range i (σ i'))) i'.succ) : by simp only [mul_assoc, mul_comm, neg_mul_eq_neg_mul_symm, one_mul, gsmul_eq_mul, neg_inj, neg_smul, fin.succ_above_cycle_range], end /-- Laplacian expansion of the determinant of an `n+1 × n+1` matrix along row 0. -/ lemma det_succ_row_zero {n : ℕ} (A : matrix (fin n.succ) (fin n.succ) R) : det A = ∑ j : fin n.succ, (-1) ^ (j : ℕ) * A 0 j * det (A.minor fin.succ j.succ_above) := by { rw [← det_transpose A, det_succ_column_zero], refine finset.sum_congr rfl (λ i _, _), rw [← det_transpose], simp only [transpose_apply, transpose_minor, transpose_transpose] } /-- Laplacian expansion of the determinant of an `n+1 × n+1` matrix along row `i`. -/ lemma det_succ_row {n : ℕ} (A : matrix (fin n.succ) (fin n.succ) R) (i : fin n.succ) : det A = ∑ j : fin n.succ, (-1) ^ (i + j : ℕ) * A i j * det (A.minor i.succ_above j.succ_above) := begin simp_rw [pow_add, mul_assoc, ← mul_sum], have : det A = (-1 : R) ^ (i : ℕ) * (i.cycle_range⁻¹).sign * det A, { calc det A = ↑((-1 : units ℤ) ^ (i : ℕ) * (-1 : units ℤ) ^ (i : ℕ) : units ℤ) * det A : by simp ... = (-1 : R) ^ (i : ℕ) * (i.cycle_range⁻¹).sign * det A : by simp [-int.units_mul_self] }, rw [this, mul_assoc], congr, rw [← det_permute, det_succ_row_zero], refine finset.sum_congr rfl (λ j _, _), rw [mul_assoc, matrix.minor, matrix.minor], congr, { rw [equiv.perm.inv_def, fin.cycle_range_symm_zero] }, { ext i' j', rw [equiv.perm.inv_def, fin.cycle_range_symm_succ] }, end /-- Laplacian expansion of the determinant of an `n+1 × n+1` matrix along column `j`. -/ lemma det_succ_column {n : ℕ} (A : matrix (fin n.succ) (fin n.succ) R) (j : fin n.succ) : det A = ∑ i : fin n.succ, (-1) ^ (i + j : ℕ) * A i j * det (A.minor i.succ_above j.succ_above) := by { rw [← det_transpose, det_succ_row _ j], refine finset.sum_congr rfl (λ i _, _), rw [add_comm, ← det_transpose, transpose_apply, transpose_minor, transpose_transpose] } end matrix
93c7843e6d357c6fd653b52d506b4071e1bf0dea
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/homology/functor.lean
cee1f32ec5ddd45d9e0538c61eb84f007dbdbf0b
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
2,112
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.homology.homological_complex /-! # Complexes in functor categories > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We can view a complex valued in a functor category `T ⥤ V` as a functor from `T` to complexes valued in `V`. ## Future work In fact this is an equivalence of categories. -/ universes v u open category_theory open category_theory.limits namespace homological_complex variables {V : Type u} [category.{v} V] [has_zero_morphisms V] variables {ι : Type*} {c : complex_shape ι} /-- A complex of functors gives a functor to complexes. -/ @[simps obj map] def as_functor {T : Type*} [category T] (C : homological_complex (T ⥤ V) c) : T ⥤ homological_complex V c := { obj := λ t, { X := λ i, (C.X i).obj t, d := λ i j, (C.d i j).app t, d_comp_d' := λ i j k hij hjk, begin have := C.d_comp_d i j k, rw [nat_trans.ext_iff, function.funext_iff] at this, exact this t end, shape' := λ i j h, begin have := C.shape _ _ h, rw [nat_trans.ext_iff, function.funext_iff] at this, exact this t end }, map := λ t₁ t₂ h, { f := λ i, (C.X i).map h, comm' := λ i j hij, nat_trans.naturality _ _ }, map_id' := λ t, by { ext i, dsimp, rw (C.X i).map_id, }, map_comp' := λ t₁ t₂ t₃ h₁ h₂, by { ext i, dsimp, rw functor.map_comp, } } /-- The functorial version of `homological_complex.as_functor`. -/ -- TODO in fact, this is an equivalence of categories. @[simps] def complex_of_functors_to_functor_to_complex {T : Type*} [category T] : (homological_complex (T ⥤ V) c) ⥤ (T ⥤ homological_complex V c) := { obj := λ C, C.as_functor, map := λ C D f, { app := λ t, { f := λ i, (f.f i).app t, comm' := λ i j w, nat_trans.congr_app (f.comm i j) t, }, naturality' := λ t t' g, by { ext i, exact (f.f i).naturality g, }, } } end homological_complex
28c2d712405225b00674f36f8e8013197e5d4e61
fef48cac17c73db8662678da38fd75888db97560
/src/real_alpha.lean
4c17efecf62c2dae6638df319d616fe621e66095
[]
no_license
kbuzzard/lean-squares-in-fibonacci
6c0d924f799d6751e19798bb2530ee602ec7087e
8cea20e5ce88ab7d17b020932d84d316532a84a8
refs/heads/master
1,584,524,504,815
1,582,387,156,000
1,582,387,156,000
134,576,655
3
1
null
1,541,538,497,000
1,527,083,406,000
Lean
UTF-8
Lean
false
false
522
lean
import data.real.basic tactic.norm_num tactic.ring namespace real noncomputable theory def α := (sqrt 5 + 1) / 2 def β := 1 - α theorem root_5_squared : (sqrt 5) ^ 2 = 5 := by simp [sqr_sqrt,(by norm_num : (0:ℝ) <= 5)] lemma αβsum : α + β = 1 := begin unfold β, unfold α, norm_num, -- ;-) end lemma αβprod : α * β = -1 := begin unfold α β, ring, rw sqr_sqrt; norm_num end -- √ is \surd --#check sqrt_prop -- ∀ (x : ℝ), 0 ≤ sqrt x ∧ sqrt x * sqrt x = max 0 x end real
de8c638e339d01cd2ce0ce4c7f6149b8330b410d
4727251e0cd73359b15b664c3170e5d754078599
/archive/imo/imo2013_q5.lean
68c7ac627ae8387757c918b13a3ec50aff1c9de8
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
12,866
lean
/- Copyright (c) 2021 David Renshaw. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Renshaw -/ import algebra.geom_sum import data.rat.basic import data.real.basic /-! # IMO 2013 Q5 Let `ℚ>₀` be the positive rational numbers. Let `f : ℚ>₀ → ℝ` be a function satisfying the conditions 1. `f(x) * f(y) ≥ f(x * y)` 2. `f(x + y) ≥ f(x) + f(y)` for all `x, y ∈ ℚ>₀`. Given that `f(a) = a` for some rational `a > 1`, prove that `f(x) = x` for all `x ∈ ℚ>₀`. # Solution We provide a direct translation of the solution found in https://www.imo-official.org/problems/IMO2013SL.pdf -/ open_locale big_operators lemma le_of_all_pow_lt_succ {x y : ℝ} (hx : 1 < x) (hy : 1 < y) (h : ∀ n : ℕ, 0 < n → x^n - 1 < y^n) : x ≤ y := begin by_contra' hxy, have hxmy : 0 < x - y := sub_pos.mpr hxy, have hn : ∀ n : ℕ, 0 < n → (x - y) * (n : ℝ) ≤ x^n - y^n, { intros n hn, have hterm : ∀ i : ℕ, i ∈ finset.range n → 1 ≤ x^i * y^(n - 1 - i), { intros i hi, have hx' : 1 ≤ x ^ i := one_le_pow_of_one_le hx.le i, have hy' : 1 ≤ y ^ (n - 1 - i) := one_le_pow_of_one_le hy.le (n - 1 - i), calc 1 ≤ x^i : hx' ... = x^i * 1 : (mul_one _).symm ... ≤ x^i * y^(n-1-i) : mul_le_mul_of_nonneg_left hy' (zero_le_one.trans hx') }, calc (x - y) * (n : ℝ) = (n : ℝ) * (x - y) : mul_comm _ _ ... = (∑ (i : ℕ) in finset.range n, (1 : ℝ)) * (x - y) : by simp only [mul_one, finset.sum_const, nsmul_eq_mul, finset.card_range] ... ≤ (∑ (i : ℕ) in finset.range n, x ^ i * y ^ (n - 1 - i)) * (x-y) : (mul_le_mul_right hxmy).mpr (finset.sum_le_sum hterm) ... = x^n - y^n : geom_sum₂_mul x y n, }, -- Choose n larger than 1 / (x - y). obtain ⟨N, hN⟩ := exists_nat_gt (1 / (x - y)), have hNp : 0 < N, { exact_mod_cast (one_div_pos.mpr hxmy).trans hN }, have := calc 1 = (x - y) * (1 / (x - y)) : by field_simp [ne_of_gt hxmy] ... < (x - y) * N : (mul_lt_mul_left hxmy).mpr hN ... ≤ x^N - y^N : hn N hNp, linarith [h N hNp] end /-- Like le_of_all_pow_lt_succ, but with a weaker assumption for y. -/ lemma le_of_all_pow_lt_succ' {x y : ℝ} (hx : 1 < x) (hy : 0 < y) (h : ∀ n : ℕ, 0 < n → x^n - 1 < y^n) : x ≤ y := begin refine le_of_all_pow_lt_succ hx _ h, by_contra' hy'' : y ≤ 1, -- Then there exists y' such that 0 < y ≤ 1 < y' < x. let y' := (x + 1) / 2, have h_y'_lt_x : y' < x, { have hh : (x + 1)/2 < (x * 2) / 2, { linarith }, calc y' < (x * 2) / 2 : hh ... = x : by field_simp }, have h1_lt_y' : 1 < y', { have hh' : 1 * 2 / 2 < (x + 1) / 2, { linarith }, calc 1 = 1 * 2 / 2 : by field_simp ... < y' : hh' }, have h_y_lt_y' : y < y' := hy''.trans_lt h1_lt_y', have hh : ∀ n, 0 < n → x^n - 1 < y'^n, { intros n hn, calc x^n - 1 < y^n : h n hn ... ≤ y'^n : pow_le_pow_of_le_left hy.le h_y_lt_y'.le n }, exact h_y'_lt_x.not_le (le_of_all_pow_lt_succ hx h1_lt_y' hh) end lemma f_pos_of_pos {f : ℚ → ℝ} {q : ℚ} (hq : 0 < q) (H1 : ∀ x y, 0 < x → 0 < y → f (x * y) ≤ f x * f y) (H4 : ∀ n : ℕ, 0 < n → (n : ℝ) ≤ f n) : 0 < f q := begin have num_pos : 0 < q.num := rat.num_pos_iff_pos.mpr hq, have hmul_pos := calc (0 : ℝ) < q.num : int.cast_pos.mpr num_pos ... = ((q.num.nat_abs : ℤ) : ℝ) : congr_arg coe (int.nat_abs_of_nonneg num_pos.le).symm ... ≤ f q.num.nat_abs : H4 q.num.nat_abs (int.nat_abs_pos_of_ne_zero num_pos.ne') ... = f q.num : by { rw ←int.nat_abs_of_nonneg num_pos.le, norm_cast } ... = f (q * q.denom) : by rw ←rat.mul_denom_eq_num ... ≤ f q * f q.denom : H1 q q.denom hq (nat.cast_pos.mpr q.pos), have h_f_denom_pos := calc (0 : ℝ) < q.denom : nat.cast_pos.mpr q.pos ... ≤ f q.denom : H4 q.denom q.pos, exact pos_of_mul_pos_right hmul_pos h_f_denom_pos.le, end lemma fx_gt_xm1 {f : ℚ → ℝ} {x : ℚ} (hx : 1 ≤ x) (H1 : ∀ x y, 0 < x → 0 < y → f (x * y) ≤ f x * f y) (H2 : ∀ x y, 0 < x → 0 < y → f x + f y ≤ f (x + y)) (H4 : ∀ n : ℕ, 0 < n → (n : ℝ) ≤ f n) : (x - 1 : ℝ) < f x := begin have hx0 := calc (x - 1 : ℝ) < ⌊x⌋₊ : by exact_mod_cast nat.sub_one_lt_floor x ... ≤ f ⌊x⌋₊ : H4 _ (nat.floor_pos.2 hx), obtain h_eq | h_lt := (nat.floor_le $ zero_le_one.trans hx).eq_or_lt, { rwa h_eq at hx0 }, calc (x - 1 : ℝ) < f ⌊x⌋₊ : hx0 ... < f (x - ⌊x⌋₊) + f ⌊x⌋₊ : lt_add_of_pos_left _ (f_pos_of_pos (sub_pos.mpr h_lt) H1 H4) ... ≤ f (x - ⌊x⌋₊ + ⌊x⌋₊) : H2 _ _ (sub_pos.mpr h_lt) (nat.cast_pos.2 (nat.floor_pos.2 hx)) ... = f x : by rw sub_add_cancel end lemma pow_f_le_f_pow {f : ℚ → ℝ} {n : ℕ} (hn : 0 < n) {x : ℚ} (hx : 1 < x) (H1 : ∀ x y, 0 < x → 0 < y → f (x * y) ≤ f x * f y) (H4 : ∀ n : ℕ, 0 < n → (n : ℝ) ≤ f n) : f (x^n) ≤ (f x)^n := begin induction n with pn hpn, { exfalso, exact nat.lt_asymm hn hn }, cases pn, { simp only [pow_one] }, have hpn' := hpn pn.succ_pos, rw [pow_succ' x (pn + 1), pow_succ' (f x) (pn + 1)], have hxp : 0 < x := zero_lt_one.trans hx, calc f ((x ^ (pn+1)) * x) ≤ f (x ^ (pn+1)) * f x : H1 (x ^ (pn+1)) x (pow_pos hxp (pn+1)) hxp ... ≤ (f x) ^ (pn+1) * f x : (mul_le_mul_right (f_pos_of_pos hxp H1 H4)).mpr hpn' end lemma fixed_point_of_pos_nat_pow {f : ℚ → ℝ} {n : ℕ} (hn : 0 < n) (H1 : ∀ x y, 0 < x → 0 < y → f (x * y) ≤ f x * f y) (H4 : ∀ n : ℕ, 0 < n → (n : ℝ) ≤ f n) (H5 : ∀ x : ℚ, 1 < x → (x : ℝ) ≤ f x) {a : ℚ} (ha1 : 1 < a) (hae : f a = a) : f (a^n) = a^n := begin have hh0 : (a : ℝ) ^ n ≤ f (a ^ n), { exact_mod_cast H5 (a ^ n) (one_lt_pow ha1 hn.ne') }, have hh1 := calc f (a^n) ≤ (f a)^n : pow_f_le_f_pow hn ha1 H1 H4 ... = (a : ℝ)^n : by rw ← hae, exact hh1.antisymm hh0 end lemma fixed_point_of_gt_1 {f : ℚ → ℝ} {x : ℚ} (hx : 1 < x) (H1 : ∀ x y, 0 < x → 0 < y → f (x * y) ≤ f x * f y) (H2 : ∀ x y, 0 < x → 0 < y → f x + f y ≤ f (x + y)) (H4 : ∀ n : ℕ, 0 < n → (n : ℝ) ≤ f n) (H5 : ∀ x : ℚ, 1 < x → (x : ℝ) ≤ f x) {a : ℚ} (ha1 : 1 < a) (hae : f a = a) : f x = x := begin -- Choose n such that 1 + x < a^n. obtain ⟨N, hN⟩ := pow_unbounded_of_one_lt (1 + x) ha1, have h_big_enough : (1:ℚ) < a^N - x := lt_sub_iff_add_lt.mpr hN, have h1 := calc (x : ℝ) + ((a^N - x) : ℚ) ≤ f x + ((a^N - x) : ℚ) : add_le_add_right (H5 x hx) _ ... ≤ f x + f (a^N - x) : add_le_add_left (H5 _ h_big_enough) _, have hxp : 0 < x := zero_lt_one.trans hx, have hNp : 0 < N, { by_contra' H, rw [nat.le_zero_iff.mp H] at hN, linarith }, have h2 := calc f x + f (a^N - x) ≤ f (x + (a^N - x)) : H2 x (a^N - x) hxp (zero_lt_one.trans h_big_enough) ... = f (a^N) : by ring_nf ... = a^N : fixed_point_of_pos_nat_pow hNp H1 H4 H5 ha1 hae ... = x + (a^N - x) : by ring, have heq := h1.antisymm (by exact_mod_cast h2), linarith [H5 x hx, H5 _ h_big_enough] end theorem imo2013_q5 (f : ℚ → ℝ) (H1 : ∀ x y, 0 < x → 0 < y → f (x * y) ≤ f x * f y) (H2 : ∀ x y, 0 < x → 0 < y → f x + f y ≤ f (x + y)) (H_fixed_point : ∃ a, 1 < a ∧ f a = a) : ∀ x, 0 < x → f x = x := begin obtain ⟨a, ha1, hae⟩ := H_fixed_point, have H3 : ∀ x : ℚ, 0 < x → ∀ n : ℕ, 0 < n → ↑n * f x ≤ f (n * x), { intros x hx n hn, cases n, { exact (lt_irrefl 0 hn).elim }, induction n with pn hpn, { simp only [one_mul, nat.cast_one] }, calc (↑pn + 1 + 1) * f x = ((pn : ℝ) + 1) * f x + 1 * f x : add_mul (↑pn + 1) 1 (f x) ... = (↑pn + 1) * f x + f x : by rw one_mul ... ≤ f ((↑pn.succ) * x) + f x : by exact_mod_cast add_le_add_right (hpn pn.succ_pos) (f x) ... ≤ f ((↑pn + 1) * x + x) : by exact_mod_cast H2 _ _ (mul_pos pn.cast_add_one_pos hx) hx ... = f ((↑pn + 1) * x + 1 * x) : by rw one_mul ... = f ((↑pn + 1 + 1) * x) : congr_arg f (add_mul (↑pn + 1) 1 x).symm }, have H4 : ∀ n : ℕ, 0 < n → (n : ℝ) ≤ f n, { intros n hn, have hf1 : 1 ≤ f 1, { have a_pos : (0 : ℝ) < a := rat.cast_pos.mpr (zero_lt_one.trans ha1), suffices : ↑a * 1 ≤ ↑a * f 1, from (mul_le_mul_left a_pos).mp this, calc ↑a * 1 = ↑a : mul_one ↑a ... = f a : hae.symm ... = f (a * 1) : by rw mul_one ... ≤ f a * f 1 : (H1 a 1) (zero_lt_one.trans ha1) zero_lt_one ... = ↑a * f 1 : by rw hae }, calc (n : ℝ) = (n : ℝ) * 1 : (mul_one _).symm ... ≤ (n : ℝ) * f 1 : mul_le_mul_of_nonneg_left hf1 (nat.cast_nonneg _) ... ≤ f (n * 1) : H3 1 zero_lt_one n hn ... = f n : by rw mul_one }, have H5 : ∀ x : ℚ, 1 < x → (x : ℝ) ≤ f x, { intros x hx, have hxnm1 : ∀ n : ℕ, 0 < n → (x : ℝ)^n - 1 < (f x)^n, { intros n hn, calc (x : ℝ)^n - 1 < f (x^n) : by exact_mod_cast fx_gt_xm1 (one_le_pow_of_one_le hx.le n) H1 H2 H4 ... ≤ (f x)^n : pow_f_le_f_pow hn hx H1 H4 }, have hx' : 1 < (x : ℝ) := by exact_mod_cast hx, have hxp : 0 < x := zero_lt_one.trans hx, exact le_of_all_pow_lt_succ' hx' (f_pos_of_pos hxp H1 H4) hxnm1 }, have h_f_commutes_with_pos_nat_mul : ∀ n : ℕ, 0 < n → ∀ x : ℚ, 0 < x → f (n * x) = n * f x, { intros n hn x hx, have h2 : f (n * x) ≤ n * f x, { cases n, { exfalso, exact nat.lt_asymm hn hn }, cases n, { simp only [one_mul, nat.cast_one] }, have hfneq : f (n.succ.succ) = n.succ.succ, { have := fixed_point_of_gt_1 (nat.one_lt_cast.mpr (nat.succ_lt_succ n.succ_pos)) H1 H2 H4 H5 ha1 hae, rwa (rat.cast_coe_nat n.succ.succ) at this }, rw ← hfneq, exact H1 (n.succ.succ : ℚ) x (nat.cast_pos.mpr hn) hx }, exact h2.antisymm (H3 x hx n hn) }, -- For the final calculation, we expand x as (2*x.num) / (2*x.denom), because -- we need the top of the fraction to be strictly greater than 1 in order -- to apply fixed_point_of_gt_1. intros x hx, let x2denom := 2 * x.denom, let x2num := 2 * x.num, have hx2pos := calc 0 < x.denom : x.pos ... < x.denom + x.denom : lt_add_of_pos_left x.denom x.pos ... = 2 * x.denom : by ring, have hxcnez : (x.denom : ℚ) ≠ (0 : ℚ) := ne_of_gt (nat.cast_pos.mpr x.pos), have hx2cnezr : (x2denom : ℝ) ≠ (0 : ℝ) := nat.cast_ne_zero.mpr (ne_of_gt hx2pos), have hrat_expand2 := calc x = x.num / x.denom : by exact_mod_cast rat.num_denom.symm ... = x2num / x2denom : by { field_simp [-rat.num_div_denom], linarith }, have h_denom_times_fx := calc (x2denom : ℝ) * f x = f (x2denom * x) : (h_f_commutes_with_pos_nat_mul x2denom hx2pos x hx).symm ... = f (x2denom * (x2num / x2denom)) : by rw hrat_expand2 ... = f x2num : by { congr, field_simp, ring }, have h_fx2num_fixed : f x2num = x2num, { have hx2num_gt_one : (1 : ℚ) < (2 * x.num : ℤ), { norm_cast, linarith [rat.num_pos_iff_pos.mpr hx] }, have hh := fixed_point_of_gt_1 hx2num_gt_one H1 H2 H4 H5 ha1 hae, rwa (rat.cast_coe_int x2num) at hh }, calc f x = f x * 1 : (mul_one (f x)).symm ... = f x * (x2denom / x2denom) : by rw ←(div_self hx2cnezr) ... = (f x * x2denom) / x2denom : mul_div_assoc' (f x) _ _ ... = (x2denom * f x) / x2denom : by rw mul_comm ... = f x2num / x2denom : by rw h_denom_times_fx ... = x2num / x2denom : by rw h_fx2num_fixed ... = (((x2num : ℚ) / (x2denom : ℚ) : ℚ) : ℝ) : by norm_cast ... = x : by rw ←hrat_expand2 end
6d6965e99c67fd3adb146fd4a21028db63602729
4893106df806b4e05d3750c6657084d1643eaa0c
/hott/hit/trunc.hlean
b165799991b5c17a00ca51eed785a8c3bb72164f
[ "Apache-2.0" ]
permissive
jonas-frey/lean2
af635de01846df4064de68b2f22dfcddcb1ab054
cbfe19cb85d4f533efcca31aff62b5a3fa2a5cf0
refs/heads/master
1,632,735,988,515
1,541,608,807,000
1,541,608,807,000
118,974,364
0
0
null
1,516,918,736,000
1,516,918,736,000
null
UTF-8
Lean
false
false
6,242
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn n-truncation of types. Ported from Coq HoTT -/ /- The hit n-truncation is primitive, declared in init.hit. -/ import types.sigma types.pointed open is_trunc eq equiv is_equiv function prod sum sigma namespace trunc protected definition elim {n : trunc_index} {A : Type} {P : Type} [Pt : is_trunc n P] (H : A → P) : trunc n A → P := trunc.rec H protected definition elim_on {n : trunc_index} {A : Type} {P : Type} (aa : trunc n A) [Pt : is_trunc n P] (H : A → P) : P := trunc.elim H aa end trunc attribute trunc.elim_on [unfold 4] attribute trunc.rec [recursor 6] attribute trunc.elim [recursor 6] [unfold 6] namespace trunc variables {X Y Z : Type} {P : X → Type} (n : trunc_index) (A B : Type) local attribute is_trunc_eq [instance] variables {n A} definition untrunc_of_is_trunc [reducible] [unfold 4] [H : is_trunc n A] : trunc n A → A := trunc.rec id variables (n A) definition is_equiv_tr [instance] [constructor] [H : is_trunc n A] : is_equiv (@tr n A) := adjointify _ (untrunc_of_is_trunc) (λaa, trunc.rec_on aa (λa, idp)) (λa, idp) definition trunc_equiv [constructor] [H : is_trunc n A] : trunc n A ≃ A := (equiv.mk tr _)⁻¹ᵉ definition is_trunc_of_is_equiv_tr [H : is_equiv (@tr n A)] : is_trunc n A := is_trunc_is_equiv_closed n (@tr n _)⁻¹ᶠ _ _ /- Functoriality -/ definition trunc_functor [unfold 5] (f : X → Y) : trunc n X → trunc n Y := λxx, trunc.rec_on xx (λx, tr (f x)) definition trunc_functor_compose [unfold 7] (f : X → Y) (g : Y → Z) : trunc_functor n (g ∘ f) ~ trunc_functor n g ∘ trunc_functor n f := λxx, trunc.rec_on xx (λx, idp) definition trunc_functor_id : trunc_functor n (@id A) ~ id := λxx, trunc.rec_on xx (λx, idp) definition trunc_functor_cast {X Y : Type} (n : ℕ₋₂) (p : X = Y) : trunc_functor n (cast p) ~ cast (ap (trunc n) p) := begin intro x, induction x with x, esimp, exact fn_tr_eq_tr_fn p (λy, tr) x ⬝ !tr_compose end definition is_equiv_trunc_functor [constructor] (f : X → Y) (H : is_equiv f) : is_equiv (trunc_functor n f) := adjointify _ (trunc_functor n f⁻¹) (λyy, trunc.rec_on yy (λy, ap tr !right_inv)) (λxx, trunc.rec_on xx (λx, ap tr !left_inv)) definition trunc_homotopy {f g : X → Y} (p : f ~ g) : trunc_functor n f ~ trunc_functor n g := λxx, trunc.rec_on xx (λx, ap tr (p x)) section definition trunc_equiv_trunc [constructor] (f : X ≃ Y) : trunc n X ≃ trunc n Y := equiv.mk _ (is_equiv_trunc_functor n f _) end section open prod.ops definition trunc_prod_equiv [constructor] : trunc n (X × Y) ≃ trunc n X × trunc n Y := begin fapply equiv.MK, {exact (λpp, trunc.rec_on pp (λp, (tr p.1, tr p.2)))}, {intro p, cases p with xx yy, apply (trunc.rec_on xx), intro x, apply (trunc.rec_on yy), intro y, exact (tr (x,y))}, {intro p, cases p with xx yy, apply (trunc.rec_on xx), intro x, apply (trunc.rec_on yy), intro y, apply idp}, {intro pp, apply (trunc.rec_on pp), intro p, cases p, apply idp} end end /- Propositional truncation -/ definition ttrunc [constructor] (n : ℕ₋₂) (X : Type) : n-Type := trunctype.mk (trunc n X) _ -- should this live in Prop? definition merely [reducible] [constructor] (A : Type) : Prop := ttrunc -1 A notation `||`:max A `||`:0 := merely A notation `∥`:max A `∥`:0 := merely A definition Exists [reducible] [constructor] (P : X → Type) : Prop := ∥ sigma P ∥ definition or' [reducible] [constructor] (A B : Type) : Type := ∥ A ⊎ B ∥ definition or [reducible] [constructor] (A B : Type) : Prop := trunctype.mk (or' A B) _ notation `exists` binders `,` r:(scoped P, Exists P) := r notation `∃` binders `,` r:(scoped P, Exists P) := r notation A ` \/ ` B := or A B notation A ∨ B := or A B definition merely.intro [reducible] [constructor] (a : A) : ∥ A ∥ := tr a definition exists.intro [reducible] [constructor] (x : X) (p : P x) : ∃x, P x := tr ⟨x, p⟩ definition or.intro_left [reducible] [constructor] (x : X) : X ∨ Y := tr (inl x) definition or.intro_right [reducible] [constructor] (y : Y) : X ∨ Y := tr (inr y) definition or.rec [recursor 7] {A B : Type} {C : A ∨ B → Type} [H : ∀ x, is_prop (C x)] (l: ∀ a : A, C (or.intro_left a)) (r : ∀b, C(or.intro_right b)) (x : or' A B) : C x := begin revert x, refine @trunc.rec _ _ _ H _, intro, induction a, exact l a, exact r a end definition exists.elim {A : Type} {p : A → Type} {B : Type} [is_prop B] (H : Exists p) (H' : ∀ (a : A), p a → B) : B := trunc.elim (sigma.rec H') H definition is_contr_of_merely_prop [H : is_prop A] (aa : merely A) : is_contr A := is_contr_of_inhabited_prop (trunc.rec_on aa id) _ section open sigma.ops definition trunc_sigma_equiv [constructor] : trunc n (Σ x, P x) ≃ trunc n (Σ x, trunc n (P x)) := equiv.MK (λpp, trunc.rec_on pp (λp, tr ⟨p.1, tr p.2⟩)) (λpp, trunc.rec_on pp (λp, trunc.rec_on p.2 (λb, tr ⟨p.1, b⟩))) (λpp, trunc.rec_on pp (λp, sigma.rec_on p (λa bb, trunc.rec_on bb (λb, by esimp)))) (λpp, trunc.rec_on pp (λp, sigma.rec_on p (λa b, by esimp))) definition trunc_sigma_equiv_of_is_trunc [H : is_trunc n X] : trunc n (Σ x, P x) ≃ Σ x, trunc n (P x) := calc trunc n (Σ x, P x) ≃ trunc n (Σ x, trunc n (P x)) : trunc_sigma_equiv ... ≃ Σ x, trunc n (P x) : !trunc_equiv end /- the (non-dependent) universal property -/ definition trunc_arrow_equiv [constructor] [H : is_trunc n B] : (trunc n A → B) ≃ (A → B) := begin fapply equiv.MK, { intro g a, exact g (tr a)}, { intro f x, exact trunc.rec_on x f}, { intro f, apply eq_of_homotopy, intro a, reflexivity}, { intro g, apply eq_of_homotopy, intro x, exact trunc.rec_on x (λa, idp)}, end end trunc
4c3b9c3b083035399ba1ee68ca072608d372beb2
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/topology/uniform_space/cauchy.lean
7afd13b11511bb012f46876e92eca72d7095a14b
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
27,049
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.uniform_space.basic import topology.bases import data.set.intervals /-! # Theory of Cauchy filters in uniform spaces. Complete uniform spaces. Totally bounded subsets. -/ universes u v open filter topological_space set classical uniform_space open_locale classical uniformity topological_space filter variables {α : Type u} {β : Type v} [uniform_space α] /-- A filter `f` is Cauchy if for every entourage `r`, there exists an `s ∈ f` such that `s × s ⊆ r`. This is a generalization of Cauchy sequences, because if `a : ℕ → α` then the filter of sets containing cofinitely many of the `a n` is Cauchy iff `a` is a Cauchy sequence. -/ def cauchy (f : filter α) := ne_bot f ∧ f ×ᶠ f ≤ (𝓤 α) /-- A set `s` is called *complete*, if any Cauchy filter `f` such that `s ∈ f` has a limit in `s` (formally, it satisfies `f ≤ 𝓝 x` for some `x ∈ s`). -/ def is_complete (s : set α) := ∀f, cauchy f → f ≤ 𝓟 s → ∃x∈s, f ≤ 𝓝 x lemma filter.has_basis.cauchy_iff {p : β → Prop} {s : β → set (α × α)} (h : (𝓤 α).has_basis p s) {f : filter α} : cauchy f ↔ (ne_bot f ∧ (∀ i, p i → ∃ t ∈ f, ∀ x y ∈ t, (x, y) ∈ s i)) := and_congr iff.rfl $ (f.basis_sets.prod_self.le_basis_iff h).trans $ by simp only [subset_def, prod.forall, mem_prod_eq, and_imp, id] lemma cauchy_iff' {f : filter α} : cauchy f ↔ (ne_bot f ∧ (∀ s ∈ 𝓤 α, ∃t∈f, ∀ x y ∈ t, (x, y) ∈ s)) := (𝓤 α).basis_sets.cauchy_iff lemma cauchy_iff {f : filter α} : cauchy f ↔ (ne_bot f ∧ (∀ s ∈ 𝓤 α, ∃t∈f, (set.prod t t) ⊆ s)) := (𝓤 α).basis_sets.cauchy_iff.trans $ by simp only [subset_def, prod.forall, mem_prod_eq, and_imp, id] lemma cauchy_map_iff {l : filter β} {f : β → α} : cauchy (l.map f) ↔ (ne_bot l ∧ tendsto (λp:β×β, (f p.1, f p.2)) (l ×ᶠ l) (𝓤 α)) := by rw [cauchy, map_ne_bot_iff, prod_map_map_eq, tendsto] lemma cauchy_map_iff' {l : filter β} [hl : ne_bot l] {f : β → α} : cauchy (l.map f) ↔ tendsto (λp:β×β, (f p.1, f p.2)) (l ×ᶠ l) (𝓤 α) := cauchy_map_iff.trans $ and_iff_right hl lemma cauchy.mono {f g : filter α} [hg : ne_bot g] (h_c : cauchy f) (h_le : g ≤ f) : cauchy g := ⟨hg, le_trans (filter.prod_mono h_le h_le) h_c.right⟩ lemma cauchy.mono' {f g : filter α} (h_c : cauchy f) (hg : ne_bot g) (h_le : g ≤ f) : cauchy g := h_c.mono h_le lemma cauchy_nhds {a : α} : cauchy (𝓝 a) := ⟨nhds_ne_bot, calc 𝓝 a ×ᶠ 𝓝 a = (𝓤 α).lift (λs:set (α×α), (𝓤 α).lift' (λt:set(α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (a, y) ∈ t})) : nhds_nhds_eq_uniformity_uniformity_prod ... ≤ (𝓤 α).lift' (λs:set (α×α), comp_rel s s) : le_infi $ assume s, le_infi $ assume hs, infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le s $ infi_le_of_le hs $ principal_mono.mpr $ assume ⟨x, y⟩ ⟨(hx : (x, a) ∈ s), (hy : (a, y) ∈ s)⟩, ⟨a, hx, hy⟩ ... ≤ 𝓤 α : comp_le_uniformity⟩ lemma cauchy_pure {a : α} : cauchy (pure a) := cauchy_nhds.mono (pure_le_nhds a) lemma filter.tendsto.cauchy_map {l : filter β} [ne_bot l] {f : β → α} {a : α} (h : tendsto f l (𝓝 a)) : cauchy (map f l) := cauchy_nhds.mono h /-- The common part of the proofs of `le_nhds_of_cauchy_adhp` and `sequentially_complete.le_nhds_of_seq_tendsto_nhds`: if for any entourage `s` one can choose a set `t ∈ f` of diameter `s` such that it contains a point `y` with `(x, y) ∈ s`, then `f` converges to `x`. -/ lemma le_nhds_of_cauchy_adhp_aux {f : filter α} {x : α} (adhs : ∀ s ∈ 𝓤 α, ∃ t ∈ f, (set.prod t t ⊆ s) ∧ ∃ y, (x, y) ∈ s ∧ y ∈ t) : f ≤ 𝓝 x := begin -- Consider a neighborhood `s` of `x` assume s hs, -- Take an entourage twice smaller than `s` rcases comp_mem_uniformity_sets (mem_nhds_uniformity_iff_right.1 hs) with ⟨U, U_mem, hU⟩, -- Take a set `t ∈ f`, `t × t ⊆ U`, and a point `y ∈ t` such that `(x, y) ∈ U` rcases adhs U U_mem with ⟨t, t_mem, ht, y, hxy, hy⟩, apply mem_sets_of_superset t_mem, -- Given a point `z ∈ t`, we have `(x, y) ∈ U` and `(y, z) ∈ t × t ⊆ U`, hence `z ∈ s` exact (λ z hz, hU (prod_mk_mem_comp_rel hxy (ht $ mk_mem_prod hy hz)) rfl) end /-- If `x` is an adherent (cluster) point for a Cauchy filter `f`, then it is a limit point for `f`. -/ lemma le_nhds_of_cauchy_adhp {f : filter α} {x : α} (hf : cauchy f) (adhs : cluster_pt x f) : f ≤ 𝓝 x := le_nhds_of_cauchy_adhp_aux begin assume s hs, obtain ⟨t, t_mem, ht⟩ : ∃ t ∈ f, set.prod t t ⊆ s, from (cauchy_iff.1 hf).2 s hs, use [t, t_mem, ht], exact (forall_sets_nonempty_iff_ne_bot.2 adhs _ (inter_mem_inf_sets (mem_nhds_left x hs) t_mem )) end lemma le_nhds_iff_adhp_of_cauchy {f : filter α} {x : α} (hf : cauchy f) : f ≤ 𝓝 x ↔ cluster_pt x f := ⟨assume h, cluster_pt.of_le_nhds' h hf.1, le_nhds_of_cauchy_adhp hf⟩ lemma cauchy.map [uniform_space β] {f : filter α} {m : α → β} (hf : cauchy f) (hm : uniform_continuous m) : cauchy (map m f) := ⟨hf.1.map _, calc map m f ×ᶠ map m f = map (λp:α×α, (m p.1, m p.2)) (f ×ᶠ f) : filter.prod_map_map_eq ... ≤ map (λp:α×α, (m p.1, m p.2)) (𝓤 α) : map_mono hf.right ... ≤ 𝓤 β : hm⟩ lemma cauchy.comap [uniform_space β] {f : filter β} {m : α → β} (hf : cauchy f) (hm : comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) ≤ 𝓤 α) [ne_bot (comap m f)] : cauchy (comap m f) := ⟨‹_›, calc comap m f ×ᶠ comap m f = comap (λp:α×α, (m p.1, m p.2)) (f ×ᶠ f) : filter.prod_comap_comap_eq ... ≤ comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) : comap_mono hf.right ... ≤ 𝓤 α : hm⟩ lemma cauchy.comap' [uniform_space β] {f : filter β} {m : α → β} (hf : cauchy f) (hm : comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) ≤ 𝓤 α) (hb : ne_bot (comap m f)) : cauchy (comap m f) := hf.comap hm /-- Cauchy sequences. Usually defined on ℕ, but often it is also useful to say that a function defined on ℝ is Cauchy at +∞ to deduce convergence. Therefore, we define it in a type class that is general enough to cover both ℕ and ℝ, which are the main motivating examples. -/ def cauchy_seq [semilattice_sup β] (u : β → α) := cauchy (at_top.map u) lemma cauchy_seq.mem_entourage {ι : Type*} [nonempty ι] [linear_order ι] {u : ι → α} (h : cauchy_seq u) {V : set (α × α)} (hV : V ∈ 𝓤 α) : ∃ k₀, ∀ i j, k₀ ≤ i → k₀ ≤ j → (u i, u j) ∈ V := begin have := h.right hV, obtain ⟨⟨i₀, j₀⟩, H⟩ : ∃ a, ∀ b : ι × ι, b ≥ a → prod.map u u b ∈ V, by rwa [prod_map_at_top_eq, mem_map, mem_at_top_sets] at this, refine ⟨max i₀ j₀, _⟩, intros i j hi hj, exact H (i, j) ⟨le_of_max_le_left hi, le_of_max_le_right hj⟩, end lemma filter.tendsto.cauchy_seq [semilattice_sup β] [nonempty β] {f : β → α} {x} (hx : tendsto f at_top (𝓝 x)) : cauchy_seq f := hx.cauchy_map lemma cauchy_seq_iff_tendsto [nonempty β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ tendsto (prod.map u u) at_top (𝓤 α) := cauchy_map_iff'.trans $ by simp only [prod_at_top_at_top_eq, prod.map_def] /-- If a Cauchy sequence has a convergent subsequence, then it converges. -/ lemma tendsto_nhds_of_cauchy_seq_of_subseq [semilattice_sup β] {u : β → α} (hu : cauchy_seq u) {ι : Type*} {f : ι → β} {p : filter ι} [ne_bot p] (hf : tendsto f p at_top) {a : α} (ha : tendsto (u ∘ f) p (𝓝 a)) : tendsto u at_top (𝓝 a) := le_nhds_of_cauchy_adhp hu (map_cluster_pt_of_comp hf ha) @[nolint ge_or_gt] -- see Note [nolint_ge] lemma filter.has_basis.cauchy_seq_iff {γ} [nonempty β] [semilattice_sup β] {u : β → α} {p : γ → Prop} {s : γ → set (α × α)} (h : (𝓤 α).has_basis p s) : cauchy_seq u ↔ ∀ i, p i → ∃N, ∀m n≥N, (u m, u n) ∈ s i := begin rw [cauchy_seq_iff_tendsto, ← prod_at_top_at_top_eq], refine (at_top_basis.prod_self.tendsto_iff h).trans _, simp only [exists_prop, true_and, maps_to, preimage, subset_def, prod.forall, mem_prod_eq, mem_set_of_eq, mem_Ici, and_imp, prod.map] end lemma filter.has_basis.cauchy_seq_iff' {γ} [nonempty β] [semilattice_sup β] {u : β → α} {p : γ → Prop} {s : γ → set (α × α)} (H : (𝓤 α).has_basis p s) : cauchy_seq u ↔ ∀ i, p i → ∃N, ∀n≥N, (u n, u N) ∈ s i := begin refine H.cauchy_seq_iff.trans ⟨λ h i hi, _, λ h i hi, _⟩, { exact (h i hi).imp (λ N hN n hn, hN n N hn (le_refl N)) }, { rcases comp_symm_of_uniformity (H.mem_of_mem hi) with ⟨t, ht, ht', hts⟩, rcases H.mem_iff.1 ht with ⟨j, hj, hjt⟩, refine (h j hj).imp (λ N hN m n hm hn, hts ⟨u N, hjt _, ht' $ hjt _⟩), { exact hN m hm }, { exact hN n hn } } end lemma cauchy_seq_of_controlled [semilattice_sup β] [nonempty β] (U : β → set (α × α)) (hU : ∀ s ∈ 𝓤 α, ∃ n, U n ⊆ s) {f : β → α} (hf : ∀ {N m n : β}, N ≤ m → N ≤ n → (f m, f n) ∈ U N) : cauchy_seq f := cauchy_seq_iff_tendsto.2 begin assume s hs, rw [mem_map, mem_at_top_sets], cases hU s hs with N hN, refine ⟨(N, N), λ mn hmn, _⟩, cases mn with m n, exact hN (hf hmn.1 hmn.2) end /-- A complete space is defined here using uniformities. A uniform space is complete if every Cauchy filter converges. -/ class complete_space (α : Type u) [uniform_space α] : Prop := (complete : ∀{f:filter α}, cauchy f → ∃x, f ≤ 𝓝 x) lemma complete_univ {α : Type u} [uniform_space α] [complete_space α] : is_complete (univ : set α) := begin assume f hf _, rcases complete_space.complete hf with ⟨x, hx⟩, exact ⟨x, mem_univ x, hx⟩ end lemma cauchy_prod [uniform_space β] {f : filter α} {g : filter β} : cauchy f → cauchy g → cauchy (f ×ᶠ g) | ⟨f_proper, hf⟩ ⟨g_proper, hg⟩ := ⟨filter.prod_ne_bot.2 ⟨f_proper, g_proper⟩, let p_α := λp:(α×β)×(α×β), (p.1.1, p.2.1), p_β := λp:(α×β)×(α×β), (p.1.2, p.2.2) in suffices (f.prod f).comap p_α ⊓ (g.prod g).comap p_β ≤ (𝓤 α).comap p_α ⊓ (𝓤 β).comap p_β, by simpa [uniformity_prod, filter.prod, filter.comap_inf, filter.comap_comap, (∘), inf_assoc, inf_comm, inf_left_comm], inf_le_inf (filter.comap_mono hf) (filter.comap_mono hg)⟩ instance complete_space.prod [uniform_space β] [complete_space α] [complete_space β] : complete_space (α × β) := { complete := λ f hf, let ⟨x1, hx1⟩ := complete_space.complete $ hf.map uniform_continuous_fst in let ⟨x2, hx2⟩ := complete_space.complete $ hf.map uniform_continuous_snd in ⟨(x1, x2), by rw [nhds_prod_eq, filter.prod_def]; from filter.le_lift (λ s hs, filter.le_lift' $ λ t ht, have H1 : prod.fst ⁻¹' s ∈ f.sets := hx1 hs, have H2 : prod.snd ⁻¹' t ∈ f.sets := hx2 ht, filter.inter_mem_sets H1 H2)⟩ } /--If `univ` is complete, the space is a complete space -/ lemma complete_space_of_is_complete_univ (h : is_complete (univ : set α)) : complete_space α := ⟨λ f hf, let ⟨x, _, hx⟩ := h f hf ((@principal_univ α).symm ▸ le_top) in ⟨x, hx⟩⟩ lemma complete_space_iff_is_complete_univ : complete_space α ↔ is_complete (univ : set α) := ⟨@complete_univ α _, complete_space_of_is_complete_univ⟩ lemma cauchy_iff_exists_le_nhds [complete_space α] {l : filter α} [ne_bot l] : cauchy l ↔ (∃x, l ≤ 𝓝 x) := ⟨complete_space.complete, assume ⟨x, hx⟩, cauchy_nhds.mono hx⟩ lemma cauchy_map_iff_exists_tendsto [complete_space α] {l : filter β} {f : β → α} [ne_bot l] : cauchy (l.map f) ↔ (∃x, tendsto f l (𝓝 x)) := cauchy_iff_exists_le_nhds /-- A Cauchy sequence in a complete space converges -/ theorem cauchy_seq_tendsto_of_complete [semilattice_sup β] [complete_space α] {u : β → α} (H : cauchy_seq u) : ∃x, tendsto u at_top (𝓝 x) := complete_space.complete H /-- If `K` is a complete subset, then any cauchy sequence in `K` converges to a point in `K` -/ lemma cauchy_seq_tendsto_of_is_complete [semilattice_sup β] {K : set α} (h₁ : is_complete K) {u : β → α} (h₂ : ∀ n, u n ∈ K) (h₃ : cauchy_seq u) : ∃ v ∈ K, tendsto u at_top (𝓝 v) := h₁ _ h₃ $ le_principal_iff.2 $ mem_map_sets_iff.2 ⟨univ, univ_mem_sets, by { simp only [image_univ], rintros _ ⟨n, rfl⟩, exact h₂ n }⟩ theorem cauchy.le_nhds_Lim [complete_space α] [nonempty α] {f : filter α} (hf : cauchy f) : f ≤ 𝓝 (Lim f) := le_nhds_Lim (complete_space.complete hf) theorem cauchy_seq.tendsto_lim [semilattice_sup β] [complete_space α] [nonempty α] {u : β → α} (h : cauchy_seq u) : tendsto u at_top (𝓝 $ lim at_top u) := h.le_nhds_Lim lemma is_closed.is_complete [complete_space α] {s : set α} (h : is_closed s) : is_complete s := λ f cf fs, let ⟨x, hx⟩ := complete_space.complete cf in ⟨x, is_closed_iff_cluster_pt.mp h x (ne_bot_of_le_ne_bot cf.left (le_inf hx fs)), hx⟩ /-- A set `s` is totally bounded if for every entourage `d` there is a finite set of points `t` such that every element of `s` is `d`-near to some element of `t`. -/ def totally_bounded (s : set α) : Prop := ∀d ∈ 𝓤 α, ∃t : set α, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) theorem totally_bounded_iff_subset {s : set α} : totally_bounded s ↔ ∀d ∈ 𝓤 α, ∃t ⊆ s, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) := ⟨λ H d hd, begin rcases comp_symm_of_uniformity hd with ⟨r, hr, rs, rd⟩, rcases H r hr with ⟨k, fk, ks⟩, let u := {y ∈ k | ∃ x, x ∈ s ∧ (x, y) ∈ r}, let f : u → α := λ x, classical.some x.2.2, have : ∀ x : u, f x ∈ s ∧ (f x, x.1) ∈ r := λ x, classical.some_spec x.2.2, refine ⟨range f, _, _, _⟩, { exact range_subset_iff.2 (λ x, (this x).1) }, { have : finite u := fk.subset (λ x h, h.1), exact ⟨@set.fintype_range _ _ _ _ this.fintype⟩ }, { intros x xs, have := ks xs, simp at this, rcases this with ⟨y, hy, xy⟩, let z : coe_sort u := ⟨y, hy, x, xs, xy⟩, exact mem_bUnion_iff.2 ⟨_, ⟨z, rfl⟩, rd $ mem_comp_rel.2 ⟨_, xy, rs (this z).2⟩⟩ } end, λ H d hd, let ⟨t, _, ht⟩ := H d hd in ⟨t, ht⟩⟩ lemma totally_bounded_of_forall_symm {s : set α} (h : ∀ V ∈ 𝓤 α, symmetric_rel V → ∃ t : set α, finite t ∧ s ⊆ ⋃ y ∈ t, ball y V) : totally_bounded s := begin intros V V_in, rcases h _ (symmetrize_mem_uniformity V_in) (symmetric_symmetrize_rel V) with ⟨t, tfin, h⟩, refine ⟨t, tfin, subset.trans h _⟩, mono, intros x x_in z z_in, exact z_in.right end lemma totally_bounded_subset {s₁ s₂ : set α} (hs : s₁ ⊆ s₂) (h : totally_bounded s₂) : totally_bounded s₁ := assume d hd, let ⟨t, ht₁, ht₂⟩ := h d hd in ⟨t, ht₁, subset.trans hs ht₂⟩ lemma totally_bounded_empty : totally_bounded (∅ : set α) := λ d hd, ⟨∅, finite_empty, empty_subset _⟩ /-- The closure of a totally bounded set is totally bounded. -/ lemma totally_bounded.closure {s : set α} (h : totally_bounded s) : totally_bounded (closure s) := assume t ht, let ⟨t', ht', hct', htt'⟩ := mem_uniformity_is_closed ht, ⟨c, hcf, hc⟩ := h t' ht' in ⟨c, hcf, calc closure s ⊆ closure (⋃ (y : α) (H : y ∈ c), {x : α | (x, y) ∈ t'}) : closure_mono hc ... = _ : is_closed.closure_eq $ is_closed_bUnion hcf $ assume i hi, continuous_iff_is_closed.mp (continuous_id.prod_mk continuous_const) _ hct' ... ⊆ _ : bUnion_subset $ assume i hi, subset.trans (assume x, @htt' (x, i)) (subset_bUnion_of_mem hi)⟩ /-- The image of a totally bounded set under a unifromly continuous map is totally bounded. -/ lemma totally_bounded.image [uniform_space β] {f : α → β} {s : set α} (hs : totally_bounded s) (hf : uniform_continuous f) : totally_bounded (f '' s) := assume t ht, have {p:α×α | (f p.1, f p.2) ∈ t} ∈ 𝓤 α, from hf ht, let ⟨c, hfc, hct⟩ := hs _ this in ⟨f '' c, hfc.image f, begin simp [image_subset_iff], simp [subset_def] at hct, intros x hx, simp, exact hct x hx end⟩ lemma ultrafilter.cauchy_of_totally_bounded {s : set α} (f : ultrafilter α) (hs : totally_bounded s) (h : ↑f ≤ 𝓟 s) : cauchy (f : filter α) := ⟨f.ne_bot', assume t ht, let ⟨t', ht'₁, ht'_symm, ht'_t⟩ := comp_symm_of_uniformity ht in let ⟨i, hi, hs_union⟩ := hs t' ht'₁ in have (⋃y∈i, {x | (x,y) ∈ t'}) ∈ f, from mem_sets_of_superset (le_principal_iff.mp h) hs_union, have ∃y∈i, {x | (x,y) ∈ t'} ∈ f, from (ultrafilter.finite_bUnion_mem_iff hi).1 this, let ⟨y, hy, hif⟩ := this in have set.prod {x | (x,y) ∈ t'} {x | (x,y) ∈ t'} ⊆ comp_rel t' t', from assume ⟨x₁, x₂⟩ ⟨(h₁ : (x₁, y) ∈ t'), (h₂ : (x₂, y) ∈ t')⟩, ⟨y, h₁, ht'_symm h₂⟩, mem_sets_of_superset (prod_mem_prod hif hif) (subset.trans this ht'_t)⟩ lemma totally_bounded_iff_filter {s : set α} : totally_bounded s ↔ (∀f, ne_bot f → f ≤ 𝓟 s → ∃c ≤ f, cauchy c) := begin split, { introsI H f hf hfs, exact ⟨ultrafilter.of f, ultrafilter.of_le f, (ultrafilter.of f).cauchy_of_totally_bounded H ((ultrafilter.of_le f).trans hfs)⟩ }, { intros H d hd, contrapose! H with hd_cover, set f := ⨅ t : finset α, 𝓟 (s \ ⋃ y ∈ t, {x | (x, y) ∈ d}), have : ne_bot f, { refine infi_ne_bot_of_directed' (directed_of_sup _) _, { intros t₁ t₂ h, exact principal_mono.2 (diff_subset_diff_right $ bUnion_subset_bUnion_left h) }, { intro t, simpa [nonempty_diff] using hd_cover t t.finite_to_set } }, have : f ≤ 𝓟 s, from infi_le_of_le ∅ (by simp), refine ⟨f, ‹_›, ‹_›, λ c hcf hc, _⟩, rcases mem_prod_same_iff.1 (hc.2 hd) with ⟨m, hm, hmd⟩, have : m ∩ s ∈ c, from inter_mem_sets hm (le_principal_iff.mp (hcf.trans ‹_›)), rcases hc.1.nonempty_of_mem this with ⟨y, hym, hys⟩, set ys := ⋃ y' ∈ ({y} : finset α), {x | (x, y') ∈ d}, have : m ⊆ ys, by simpa [ys] using λ x hx, hmd (mk_mem_prod hx hym), have : c ≤ 𝓟 (s \ ys) := hcf.trans (infi_le_of_le {y} le_rfl), refine hc.1 (empty_in_sets_eq_bot.mp _), filter_upwards [le_principal_iff.1 this, hm], refine λ x hx hxm, hx.2 _, simpa [ys] using hmd (mk_mem_prod hxm hym) } end lemma totally_bounded_iff_ultrafilter {s : set α} : totally_bounded s ↔ (∀f : ultrafilter α, ↑f ≤ 𝓟 s → cauchy (f : filter α)) := begin refine ⟨λ hs f, f.cauchy_of_totally_bounded hs, λ H, totally_bounded_iff_filter.2 _⟩, introsI f hf hfs, exact ⟨ultrafilter.of f, ultrafilter.of_le f, H _ ((ultrafilter.of_le f).trans hfs)⟩ end lemma compact_iff_totally_bounded_complete {s : set α} : is_compact s ↔ totally_bounded s ∧ is_complete s := ⟨λ hs, ⟨totally_bounded_iff_ultrafilter.2 (λ f hf, let ⟨x, xs, fx⟩ := compact_iff_ultrafilter_le_nhds.1 hs f hf in cauchy_nhds.mono fx), λ f fc fs, let ⟨a, as, fa⟩ := @hs f fc.1 fs in ⟨a, as, le_nhds_of_cauchy_adhp fc fa⟩⟩, λ ⟨ht, hc⟩, compact_iff_ultrafilter_le_nhds.2 (λf hf, hc _ (totally_bounded_iff_ultrafilter.1 ht f hf) hf)⟩ @[priority 100] -- see Note [lower instance priority] instance complete_of_compact {α : Type u} [uniform_space α] [compact_space α] : complete_space α := ⟨λf hf, by simpa using (compact_iff_totally_bounded_complete.1 compact_univ).2 f hf⟩ lemma compact_of_totally_bounded_is_closed [complete_space α] {s : set α} (ht : totally_bounded s) (hc : is_closed s) : is_compact s := (@compact_iff_totally_bounded_complete α _ s).2 ⟨ht, hc.is_complete⟩ /-! ### Sequentially complete space In this section we prove that a uniform space is complete provided that it is sequentially complete (i.e., any Cauchy sequence converges) and its uniformity filter admits a countable generating set. In particular, this applies to (e)metric spaces, see the files `topology/metric_space/emetric_space` and `topology/metric_space/basic`. More precisely, we assume that there is a sequence of entourages `U_n` such that any other entourage includes one of `U_n`. Then any Cauchy filter `f` generates a decreasing sequence of sets `s_n ∈ f` such that `s_n × s_n ⊆ U_n`. Choose a sequence `x_n∈s_n`. It is easy to show that this is a Cauchy sequence. If this sequence converges to some `a`, then `f ≤ 𝓝 a`. -/ namespace sequentially_complete variables {f : filter α} (hf : cauchy f) {U : ℕ → set (α × α)} (U_mem : ∀ n, U n ∈ 𝓤 α) (U_le : ∀ s ∈ 𝓤 α, ∃ n, U n ⊆ s) open set finset noncomputable theory /-- An auxiliary sequence of sets approximating a Cauchy filter. -/ def set_seq_aux (n : ℕ) : {s : set α // ∃ (_ : s ∈ f), s.prod s ⊆ U n } := indefinite_description _ $ (cauchy_iff.1 hf).2 (U n) (U_mem n) /-- Given a Cauchy filter `f` and a sequence `U` of entourages, `set_seq` provides a sequence of monotonically decreasing sets `s n ∈ f` such that `(s n).prod (s n) ⊆ U`. -/ def set_seq (n : ℕ) : set α := ⋂ m ∈ Iic n, (set_seq_aux hf U_mem m).val lemma set_seq_mem (n : ℕ) : set_seq hf U_mem n ∈ f := (bInter_mem_sets (finite_le_nat n)).2 (λ m _, (set_seq_aux hf U_mem m).2.fst) lemma set_seq_mono ⦃m n : ℕ⦄ (h : m ≤ n) : set_seq hf U_mem n ⊆ set_seq hf U_mem m := bInter_subset_bInter_left (λ k hk, le_trans hk h) lemma set_seq_sub_aux (n : ℕ) : set_seq hf U_mem n ⊆ set_seq_aux hf U_mem n := bInter_subset_of_mem right_mem_Iic lemma set_seq_prod_subset {N m n} (hm : N ≤ m) (hn : N ≤ n) : (set_seq hf U_mem m).prod (set_seq hf U_mem n) ⊆ U N := begin assume p hp, refine (set_seq_aux hf U_mem N).2.snd ⟨_, _⟩; apply set_seq_sub_aux, exact set_seq_mono hf U_mem hm hp.1, exact set_seq_mono hf U_mem hn hp.2 end /-- A sequence of points such that `seq n ∈ set_seq n`. Here `set_seq` is a monotonically decreasing sequence of sets `set_seq n ∈ f` with diameters controlled by a given sequence of entourages. -/ def seq (n : ℕ) : α := some $ hf.1.nonempty_of_mem (set_seq_mem hf U_mem n) lemma seq_mem (n : ℕ) : seq hf U_mem n ∈ set_seq hf U_mem n := some_spec $ hf.1.nonempty_of_mem (set_seq_mem hf U_mem n) lemma seq_pair_mem ⦃N m n : ℕ⦄ (hm : N ≤ m) (hn : N ≤ n) : (seq hf U_mem m, seq hf U_mem n) ∈ U N := set_seq_prod_subset hf U_mem hm hn ⟨seq_mem hf U_mem m, seq_mem hf U_mem n⟩ include U_le theorem seq_is_cauchy_seq : cauchy_seq $ seq hf U_mem := cauchy_seq_of_controlled U U_le $ seq_pair_mem hf U_mem /-- If the sequence `sequentially_complete.seq` converges to `a`, then `f ≤ 𝓝 a`. -/ theorem le_nhds_of_seq_tendsto_nhds ⦃a : α⦄ (ha : tendsto (seq hf U_mem) at_top (𝓝 a)) : f ≤ 𝓝 a := le_nhds_of_cauchy_adhp_aux begin assume s hs, rcases U_le s hs with ⟨m, hm⟩, rcases tendsto_at_top'.1 ha _ (mem_nhds_left a (U_mem m)) with ⟨n, hn⟩, refine ⟨set_seq hf U_mem (max m n), set_seq_mem hf U_mem _, _, seq hf U_mem (max m n), _, seq_mem hf U_mem _⟩, { have := le_max_left m n, exact set.subset.trans (set_seq_prod_subset hf U_mem this this) hm }, { exact hm (hn _ $ le_max_right m n) } end end sequentially_complete namespace uniform_space open sequentially_complete variables (H : is_countably_generated (𝓤 α)) include H /-- A uniform space is complete provided that (a) its uniformity filter has a countable basis; (b) any sequence satisfying a "controlled" version of the Cauchy condition converges. -/ theorem complete_of_convergent_controlled_sequences (U : ℕ → set (α × α)) (U_mem : ∀ n, U n ∈ 𝓤 α) (HU : ∀ u : ℕ → α, (∀ N m n, N ≤ m → N ≤ n → (u m, u n) ∈ U N) → ∃ a, tendsto u at_top (𝓝 a)) : complete_space α := begin rcases H.exists_antimono_seq' with ⟨U', U'_mono, hU'⟩, have Hmem : ∀ n, U n ∩ U' n ∈ 𝓤 α, from λ n, inter_mem_sets (U_mem n) (hU'.2 ⟨n, subset.refl _⟩), refine ⟨λ f hf, (HU (seq hf Hmem) (λ N m n hm hn, _)).imp $ le_nhds_of_seq_tendsto_nhds _ _ (λ s hs, _)⟩, { rcases (hU'.1 hs) with ⟨N, hN⟩, exact ⟨N, subset.trans (inter_subset_right _ _) hN⟩ }, { exact inter_subset_left _ _ (seq_pair_mem hf Hmem hm hn) } end /-- A sequentially complete uniform space with a countable basis of the uniformity filter is complete. -/ theorem complete_of_cauchy_seq_tendsto (H' : ∀ u : ℕ → α, cauchy_seq u → ∃a, tendsto u at_top (𝓝 a)) : complete_space α := let ⟨U', U'_mono, hU'⟩ := H.exists_antimono_seq' in complete_of_convergent_controlled_sequences H U' (λ n, hU'.2 ⟨n, subset.refl _⟩) (λ u hu, H' u $ cauchy_seq_of_controlled U' (λ s hs, hU'.1 hs) hu) protected lemma first_countable_topology : first_countable_topology α := ⟨λ a, by { rw nhds_eq_comap_uniformity, exact H.comap (prod.mk a) }⟩ /-- A separable uniform space with countably generated uniformity filter is second countable: one obtains a countable basis by taking the balls centered at points in a dense subset, and with rational "radii" from a countable open symmetric antimono basis of `𝓤 α`. We do not register this as an instance, as there is already an instance going in the other direction from second countable spaces to separable spaces, and we want to avoid loops. -/ lemma second_countable_of_separable [separable_space α] : second_countable_topology α := begin rcases exists_countable_dense α with ⟨s, hsc, hsd⟩, obtain ⟨t : ℕ → set (α × α), hto : ∀ (i : ℕ), t i ∈ (𝓤 α).sets ∧ is_open (t i) ∧ symmetric_rel (t i), h_basis : (𝓤 α).has_antimono_basis (λ _, true) t⟩ := H.exists_antimono_subbasis uniformity_has_basis_open_symmetric, refine ⟨⟨⋃ (x ∈ s), range (λ k, ball x (t k)), hsc.bUnion (λ x hx, countable_range _), _⟩⟩, refine (is_topological_basis_of_open_of_nhds _ _).2.2, { simp only [mem_bUnion_iff, mem_range], rintros _ ⟨x, hxs, k, rfl⟩, exact is_open_ball x (hto k).2.1 }, { intros x V hxV hVo, simp only [mem_bUnion_iff, mem_range, exists_prop], rcases uniform_space.mem_nhds_iff.1 (mem_nhds_sets hVo hxV) with ⟨U, hU, hUV⟩, rcases comp_symm_of_uniformity hU with ⟨U', hU', hsymm, hUU'⟩, rcases h_basis.to_has_basis.mem_iff.1 hU' with ⟨k, -, hk⟩, rcases hsd.inter_open_nonempty (ball x $ t k) (uniform_space.is_open_ball x (hto k).2.1) ⟨x, uniform_space.mem_ball_self _ (hto k).1⟩ with ⟨y, hxy, hys⟩, refine ⟨_, ⟨y, hys, k, rfl⟩, (hto k).2.2.subset hxy, λ z hz, _⟩, exact hUV (ball_subset_of_comp_subset (hk hxy) hUU' (hk hz)) } end end uniform_space
4ae1f22726c4331cb904118d4650e35cfb94fbe7
57c233acf9386e610d99ed20ef139c5f97504ba3
/archive/100-theorems-list/83_friendship_graphs.lean
b5b7d6e263cf44f33d06a9bbe1cc38cfdab69f13
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
13,801
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Jalex Stark, Kyle Miller -/ import combinatorics.simple_graph.adj_matrix import linear_algebra.matrix.charpoly.coeff import data.int.modeq import data.zmod.basic import tactic.interval_cases /-! # The Friendship Theorem ## Definitions and Statement - A `friendship` graph is one in which any two distinct vertices have exactly one neighbor in common - A `politician`, at least in the context of this problem, is a vertex in a graph which is adjacent to every other vertex. - The friendship theorem (Erdős, Rényi, Sós 1966) states that every finite friendship graph has a politician. ## Proof outline The proof revolves around the theory of adjacency matrices, although some steps could equivalently be phrased in terms of counting walks. - Assume `G` is a finite friendship graph. - First we show that any two nonadjacent vertices have the same degree - Assume for contradiction that `G` does not have a politician. - Conclude from the last two points that `G` is `d`-regular for some `d : ℕ`. - Show that `G` has `d ^ 2 - d + 1` vertices. - By casework, show that if `d = 0, 1, 2`, then `G` has a politician. - If `3 ≤ d`, let `p` be a prime factor of `d - 1`. - If `A` is the adjacency matrix of `G` with entries in `ℤ/pℤ`, we show that `A ^ p` has trace `1`. - This gives a contradiction, as `A` has trace `0`, and thus `A ^ p` has trace `0`. ## References - [P. Erdős, A. Rényi, V. Sós, *On A Problem of Graph Theory*][erdosrenyisos] - [C. Huneke, *The Friendship Theorem*][huneke2002] -/ open_locale classical big_operators noncomputable theory open finset simple_graph matrix universes u v variables {V : Type u} {R : Type v} [semiring R] section friendship_def variables (G : simple_graph V) /-- This property of a graph is the hypothesis of the friendship theorem: every pair of nonadjacent vertices has exactly one common friend, a vertex to which both are adjacent. -/ def friendship [fintype V] : Prop := ∀ ⦃v w : V⦄, v ≠ w → fintype.card (G.common_neighbors v w) = 1 /-- A politician is a vertex that is adjacent to all other vertices. -/ def exists_politician : Prop := ∃ (v : V), ∀ (w : V), v ≠ w → G.adj v w end friendship_def variables [fintype V] {G : simple_graph V} {d : ℕ} (hG : friendship G) include hG namespace friendship variables (R) /-- One characterization of a friendship graph is that there is exactly one walk of length 2 between distinct vertices. These walks are counted in off-diagonal entries of the square of the adjacency matrix, so for a friendship graph, those entries are all 1. -/ theorem adj_matrix_sq_of_ne {v w : V} (hvw : v ≠ w) : ((G.adj_matrix R) ^ 2) v w = 1 := begin rw [sq, ← nat.cast_one, ← hG hvw], simp [common_neighbors, neighbor_finset_eq_filter, finset.filter_filter, finset.filter_inter, and_comm, ← neighbor_finset_def], end /-- This calculation amounts to counting the number of length 3 walks between nonadjacent vertices. We use it to show that nonadjacent vertices have equal degrees. -/ lemma adj_matrix_pow_three_of_not_adj {v w : V} (non_adj : ¬ G.adj v w) : ((G.adj_matrix R) ^ 3) v w = degree G v := begin rw [pow_succ, mul_eq_mul, adj_matrix_mul_apply, degree, card_eq_sum_ones, nat.cast_sum], apply sum_congr rfl, intros x hx, rw [adj_matrix_sq_of_ne _ hG, nat.cast_one], rintro ⟨rfl⟩, rw mem_neighbor_finset at hx, exact non_adj hx, end variable {R} /-- As `v` and `w` not being adjacent implies `degree G v = ((G.adj_matrix R) ^ 3) v w` and `degree G w = ((G.adj_matrix R) ^ 3) v w`, the degrees are equal if `((G.adj_matrix R) ^ 3) v w = ((G.adj_matrix R) ^ 3) w v` This is true as the adjacency matrix is symmetric. -/ lemma degree_eq_of_not_adj {v w : V} (hvw : ¬ G.adj v w) : degree G v = degree G w := begin rw [← nat.cast_id (G.degree v), ← nat.cast_id (G.degree w), ← adj_matrix_pow_three_of_not_adj ℕ hG hvw, ← adj_matrix_pow_three_of_not_adj ℕ hG (λ h, hvw (G.symm h))], conv_lhs {rw ← transpose_adj_matrix}, simp only [pow_succ, sq, mul_eq_mul, ← transpose_mul, transpose_apply], simp only [← mul_eq_mul, mul_assoc], end /-- Let `A` be the adjacency matrix of a graph `G`. If `G` is a friendship graph, then all of the off-diagonal entries of `A^2` are 1. If `G` is `d`-regular, then all of the diagonal entries of `A^2` are `d`. Putting these together determines `A^2` exactly for a `d`-regular friendship graph. -/ theorem adj_matrix_sq_of_regular (hd : G.is_regular_of_degree d) : ((G.adj_matrix R) ^ 2) = λ v w, if v = w then d else 1 := begin ext v w, by_cases h : v = w, { rw [h, sq, mul_eq_mul, adj_matrix_mul_self_apply_self, hd], simp, }, { rw [adj_matrix_sq_of_ne R hG h, if_neg h], }, end lemma adj_matrix_sq_mod_p_of_regular {p : ℕ} (dmod : (d : zmod p) = 1) (hd : G.is_regular_of_degree d) : (G.adj_matrix (zmod p)) ^ 2 = λ _ _, 1 := by simp [adj_matrix_sq_of_regular hG hd, dmod] section nonempty variable [nonempty V] /-- If `G` is a friendship graph without a politician (a vertex adjacent to all others), then it is regular. We have shown that nonadjacent vertices of a friendship graph have the same degree, and if there isn't a politician, we can show this for adjacent vertices by finding a vertex neither is adjacent to, and then using transitivity. -/ theorem is_regular_of_not_exists_politician (hG' : ¬exists_politician G) : ∃ (d : ℕ), G.is_regular_of_degree d := begin have v := classical.arbitrary V, use G.degree v, intro x, by_cases hvx : G.adj v x, swap, { exact (degree_eq_of_not_adj hG hvx).symm, }, dunfold exists_politician at hG', push_neg at hG', rcases hG' v with ⟨w, hvw', hvw⟩, rcases hG' x with ⟨y, hxy', hxy⟩, by_cases hxw : G.adj x w, swap, { rw degree_eq_of_not_adj hG hvw, exact degree_eq_of_not_adj hG hxw }, rw degree_eq_of_not_adj hG hxy, by_cases hvy : G.adj v y, swap, { exact (degree_eq_of_not_adj hG hvy).symm }, rw degree_eq_of_not_adj hG hvw, apply degree_eq_of_not_adj hG, intro hcontra, rcases finset.card_eq_one.mp (hG hvw') with ⟨⟨a, ha⟩, h⟩, have key : ∀ {x}, x ∈ G.common_neighbors v w → x = a, { intros x hx, have h' := mem_univ (subtype.mk x hx), rw [h, mem_singleton] at h', injection h', }, apply hxy', rw [key ((mem_common_neighbors G).mpr ⟨hvx, G.symm hxw⟩), key ((mem_common_neighbors G).mpr ⟨hvy, G.symm hcontra⟩)], end /-- Let `A` be the adjacency matrix of a `d`-regular friendship graph, and let `v` be a vector all of whose components are `1`. Then `v` is an eigenvector of `A ^ 2`, and we can compute the eigenvalue to be `d * d`, or as `d + (fintype.card V - 1)`, so those quantities must be equal. This essentially means that the graph has `d ^ 2 - d + 1` vertices. -/ lemma card_of_regular (hd : G.is_regular_of_degree d) : d + (fintype.card V - 1) = d * d := begin have v := classical.arbitrary V, transitivity ((G.adj_matrix ℕ) ^ 2).mul_vec (λ _, 1) v, { rw [adj_matrix_sq_of_regular hG hd, mul_vec, dot_product, ← insert_erase (mem_univ v)], simp only [sum_insert, mul_one, if_true, nat.cast_id, eq_self_iff_true, mem_erase, not_true, ne.def, not_false_iff, add_right_inj, false_and], rw [finset.sum_const_nat, card_erase_of_mem (mem_univ v), mul_one], { refl }, intros x hx, simp [(ne_of_mem_erase hx).symm], }, { rw [sq, mul_eq_mul, ← mul_vec_mul_vec], simp [adj_matrix_mul_vec_const_apply_of_regular hd, neighbor_finset, card_neighbor_set_eq_degree, hd v], } end /-- The size of a `d`-regular friendship graph is `1 mod (d-1)`, and thus `1 mod p` for a factor `p ∣ d-1`. -/ lemma card_mod_p_of_regular {p : ℕ} (dmod : (d : zmod p) = 1) (hd : G.is_regular_of_degree d) : (fintype.card V : zmod p) = 1 := begin have hpos : 0 < fintype.card V := fintype.card_pos_iff.mpr infer_instance, rw [← nat.succ_pred_eq_of_pos hpos, nat.succ_eq_add_one, nat.pred_eq_sub_one], simp only [add_left_eq_self, nat.cast_add, nat.cast_one], have h := congr_arg (λ n, (↑n : zmod p)) (card_of_regular hG hd), revert h, simp [dmod], end end nonempty omit hG lemma adj_matrix_sq_mul_const_one_of_regular (hd : G.is_regular_of_degree d) : (G.adj_matrix R) * (λ _ _, 1) = λ _ _, d := by { ext x, simp [← hd x, degree] } lemma adj_matrix_mul_const_one_mod_p_of_regular {p : ℕ} (dmod : (d : zmod p) = 1) (hd : G.is_regular_of_degree d) : (G.adj_matrix (zmod p)) * (λ _ _, 1) = λ _ _, 1 := by rw [adj_matrix_sq_mul_const_one_of_regular hd, dmod] include hG /-- Modulo a factor of `d-1`, the square and all higher powers of the adjacency matrix of a `d`-regular friendship graph reduce to the matrix whose entries are all 1. -/ lemma adj_matrix_pow_mod_p_of_regular {p : ℕ} (dmod : (d : zmod p) = 1) (hd : G.is_regular_of_degree d) {k : ℕ} (hk : 2 ≤ k) : (G.adj_matrix (zmod p)) ^ k = λ _ _, 1 := begin iterate 2 {cases k with k, { exfalso, linarith, }, }, induction k with k hind, { exact adj_matrix_sq_mod_p_of_regular hG dmod hd, }, rw [pow_succ, hind (nat.le_add_left 2 k)], exact adj_matrix_mul_const_one_mod_p_of_regular dmod hd, end variable [nonempty V] /-- This is the main proof. Assuming that `3 ≤ d`, we take `p` to be a prime factor of `d-1`. Then the `p`th power of the adjacency matrix of a `d`-regular friendship graph must have trace 1 mod `p`, but we can also show that the trace must be the `p`th power of the trace of the original adjacency matrix, which is 0, a contradiction. -/ lemma false_of_three_le_degree (hd : G.is_regular_of_degree d) (h : 3 ≤ d) : false := begin -- get a prime factor of d - 1 let p : ℕ := (d - 1).min_fac, have p_dvd_d_pred := (zmod.nat_coe_zmod_eq_zero_iff_dvd _ _).mpr (d - 1).min_fac_dvd, have dpos : 0 < d := by linarith, have d_cast : ↑(d - 1) = (d : ℤ) - 1 := by norm_cast, haveI : fact p.prime := ⟨nat.min_fac_prime (by linarith)⟩, have hp2 : 2 ≤ p := (fact.out p.prime).two_le, have dmod : (d : zmod p) = 1, { rw [← nat.succ_pred_eq_of_pos dpos, nat.succ_eq_add_one, nat.pred_eq_sub_one], simp only [add_left_eq_self, nat.cast_add, nat.cast_one], exact p_dvd_d_pred, }, have Vmod := card_mod_p_of_regular hG dmod hd, -- now we reduce to a trace calculation have := zmod.trace_pow_card (G.adj_matrix (zmod p)), contrapose! this, clear this, -- the trace is 0 mod p when computed one way rw [trace_adj_matrix, zero_pow (fact.out p.prime).pos], -- but the trace is 1 mod p when computed the other way rw adj_matrix_pow_mod_p_of_regular hG dmod hd hp2, dunfold fintype.card at Vmod, simp only [matrix.trace, diag_apply, mul_one, nsmul_eq_mul, linear_map.coe_mk, sum_const], rw [Vmod, ← nat.cast_one, zmod.nat_coe_zmod_eq_zero_iff_dvd, nat.dvd_one, nat.min_fac_eq_one_iff], linarith, end /-- If `d ≤ 1`, a `d`-regular friendship graph has at most one vertex, which is trivially a politician. -/ lemma exists_politician_of_degree_le_one (hd : G.is_regular_of_degree d) (hd1 : d ≤ 1) : exists_politician G := begin have sq : d * d = d := by { interval_cases d; norm_num }, have h := card_of_regular hG hd, rw sq at h, have : fintype.card V ≤ 1, { cases fintype.card V with n, { exact zero_le _, }, { have : n = 0, { rw [nat.succ_sub_succ_eq_sub, tsub_zero] at h, linarith }, subst n, } }, use classical.arbitrary V, intros w h, exfalso, apply h, apply fintype.card_le_one_iff.mp this, end /-- If `d = 2`, a `d`-regular friendship graph has 3 vertices, so it must be complete graph, and all the vertices are politicians. -/ lemma neighbor_finset_eq_of_degree_eq_two (hd : G.is_regular_of_degree 2) (v : V) : G.neighbor_finset v = finset.univ.erase v := begin apply finset.eq_of_subset_of_card_le, { rw finset.subset_iff, intro x, rw [mem_neighbor_finset, finset.mem_erase], exact λ h, ⟨(G.ne_of_adj h).symm, finset.mem_univ _⟩ }, convert_to 2 ≤ _, { convert_to _ = fintype.card V - 1, { have hfr:= card_of_regular hG hd, linarith }, { exact finset.card_erase_of_mem (finset.mem_univ _), }, }, { dsimp [is_regular_of_degree, degree] at hd, rw hd, } end lemma exists_politician_of_degree_eq_two (hd : G.is_regular_of_degree 2) : exists_politician G := begin have v := classical.arbitrary V, use v, intros w hvw, rw [← mem_neighbor_finset, neighbor_finset_eq_of_degree_eq_two hG hd v, finset.mem_erase], exact ⟨hvw.symm, finset.mem_univ _⟩, end lemma exists_politician_of_degree_le_two (hd : G.is_regular_of_degree d) (h : d ≤ 2) : exists_politician G := begin interval_cases d, iterate 2 { apply exists_politician_of_degree_le_one hG hd, norm_num }, { exact exists_politician_of_degree_eq_two hG hd }, end end friendship /-- **Friendship theorem**: We wish to show that a friendship graph has a politician (a vertex adjacent to all others). We proceed by contradiction, and assume the graph has no politician. We have already proven that a friendship graph with no politician is `d`-regular for some `d`, and now we do casework on `d`. If the degree is at most 2, we observe by casework that it has a politician anyway. If the degree is at least 3, the graph cannot exist. -/ theorem friendship_theorem [nonempty V] : exists_politician G := begin by_contradiction npG, rcases hG.is_regular_of_not_exists_politician npG with ⟨d, dreg⟩, cases lt_or_le d 3 with dle2 dge3, { exact npG (hG.exists_politician_of_degree_le_two dreg (nat.lt_succ_iff.mp dle2)) }, { exact hG.false_of_three_le_degree dreg dge3 }, end
291f6766c47defb42416e3af038fc788245d6dfc
57fdc8de88f5ea3bfde4325e6ecd13f93a274ab5
/ring_theory/ideals.lean
2f2dd8912d7484b9e8728b41b2c3b33b1751f77d
[ "Apache-2.0" ]
permissive
louisanu/mathlib
11f56f2d40dc792bc05ee2f78ea37d73e98ecbfe
2bd5e2159d20a8f20d04fc4d382e65eea775ed39
refs/heads/master
1,617,706,993,439
1,523,163,654,000
1,523,163,654,000
124,519,997
0
0
Apache-2.0
1,520,588,283,000
1,520,588,283,000
null
UTF-8
Lean
false
false
2,884
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.module universe u class is_ideal {α : Type u} [comm_ring α] (S : set α) extends is_submodule S : Prop class is_proper_ideal {α : Type u} [comm_ring α] (S : set α) extends is_ideal S : Prop := (ne_univ : S ≠ set.univ) class is_prime_ideal {α : Type u} [comm_ring α] (S : set α) extends is_proper_ideal S : Prop := (mem_or_mem_of_mul_mem : ∀ {x y : α}, x * y ∈ S → x ∈ S ∨ y ∈ S) theorem mem_or_mem_of_mul_eq_zero {α : Type u} [comm_ring α] (S : set α) [is_prime_ideal S] : ∀ {x y : α}, x * y = 0 → x ∈ S ∨ y ∈ S := λ x y hxy, have x * y ∈ S, by rw hxy; from (@is_submodule.zero α α _ _ S _ : (0:α) ∈ S), is_prime_ideal.mem_or_mem_of_mul_mem this class is_maximal_ideal {α : Type u} [comm_ring α] (S : set α) extends is_proper_ideal S : Prop := mk' :: (eq_or_univ_of_subset : ∀ (T : set α) [is_submodule T], S ⊆ T → T = S ∨ T = set.univ) theorem is_maximal_ideal.mk {α : Type u} [comm_ring α] (S : set α) [is_submodule S] (h₁ : (1:α) ∉ S) (h₂ : ∀ x (T : set α) [is_submodule T], S ⊆ T → x ∉ S → x ∈ T → (1:α) ∈ T) : is_maximal_ideal S := { ne_univ := assume hu, have (1:α) ∈ S, by rw hu; trivial, h₁ this, eq_or_univ_of_subset := assume T ht hst, classical.or_iff_not_imp_left.2 $ assume (hnst : T ≠ S), let ⟨x, hxt, hxns⟩ := set.exists_of_ssubset ⟨hst, hnst.symm⟩ in @@is_submodule.univ_of_one_mem _ T ht $ @@h₂ x T ht hst hxns hxt} theorem not_unit_of_mem_maximal_ideal {α : Type u} [comm_ring α] (S : set α) [is_maximal_ideal S] : S ⊆ nonunits α := λ x hx ⟨y, hxy⟩, is_proper_ideal.ne_univ S $ is_submodule.eq_univ_of_contains_unit S x y hx hxy class local_ring (α : Type u) [comm_ring α] := (S : set α) (max : is_maximal_ideal S) (unique : ∀ T [is_maximal_ideal T], S = T) def local_of_nonunits_ideal {α : Type u} [comm_ring α] (hnze : (0:α) ≠ 1) (h : ∀ x y ∈ nonunits α, x + y ∈ nonunits α) : local_ring α := have hi : is_submodule (nonunits α), from { zero_ := λ ⟨y, hy⟩, hnze $ by simpa using hy, add_ := h, smul := λ x y hy ⟨z, hz⟩, hy ⟨x * z, by rw [← hz]; simp [mul_left_comm, mul_assoc]⟩ }, { S := nonunits α, max := @@is_maximal_ideal.mk _ (nonunits α) hi (λ ho, ho ⟨1, mul_one 1⟩) $ λ x T ht hst hxns hxt, let ⟨y, hxy⟩ := classical.by_contradiction hxns in by rw [← hxy]; exact @@is_submodule.smul _ _ ht y hxt, unique := λ T hmt, or.cases_on (@@is_maximal_ideal.eq_or_univ_of_subset _ hmt (nonunits α) hi $ λ z hz, @@not_unit_of_mem_maximal_ideal _ T hmt hz) id (λ htu, false.elim $ ((set.set_eq_def _ _).1 htu 1).2 trivial ⟨1, mul_one 1⟩) }
797b77b57d6ce7a997190d7b25a29fa31f7fbe09
c777c32c8e484e195053731103c5e52af26a25d1
/src/algebraic_topology/dold_kan/equivalence_additive.lean
886aefc7db66b44017824c4a12a89036154dc78f
[ "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
1,983
lean
/- Copyright (c) 2022 Joël Riou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joël Riou -/ import algebraic_topology.dold_kan.n_comp_gamma /-! > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. The Dold-Kan equivalence for additive categories. This file defines `preadditive.dold_kan.equivalence` which is the equivalence of categories `karoubi (simplicial_object C) ≌ karoubi (chain_complex C ℕ)`. -/ noncomputable theory open category_theory category_theory.category category_theory.limits category_theory.idempotents algebraic_topology.dold_kan variables {C : Type*} [category C] [preadditive C] namespace category_theory namespace preadditive namespace dold_kan /-- The functor `karoubi (simplicial_object C) ⥤ karoubi (chain_complex C ℕ)` of the Dold-Kan equivalence for additive categories. -/ @[simps] def N : karoubi (simplicial_object C) ⥤ karoubi (chain_complex C ℕ) := N₂ variable [has_finite_coproducts C] /-- The inverse functor `karoubi (chain_complex C ℕ) ⥤ karoubi (simplicial_object C)` of the Dold-Kan equivalence for additive categories. -/ @[simps] def Γ : karoubi (chain_complex C ℕ) ⥤ karoubi (simplicial_object C) := Γ₂ /-- The Dold-Kan equivalence `karoubi (simplicial_object C) ≌ karoubi (chain_complex C ℕ)` for additive categories. -/ @[simps] def equivalence : karoubi (simplicial_object C) ≌ karoubi (chain_complex C ℕ) := { functor := N, inverse := Γ, unit_iso := Γ₂N₂, counit_iso := N₂Γ₂, functor_unit_iso_comp' := λ P, begin let α := N.map_iso (Γ₂N₂.app P), let β := N₂Γ₂.app (N.obj P), symmetry, change 𝟙 _ = α.hom ≫ β.hom, rw [← iso.inv_comp_eq, comp_id, ← comp_id β.hom, ← iso.inv_comp_eq], exact algebraic_topology.dold_kan.identity_N₂_objectwise P, end } end dold_kan end preadditive end category_theory
d83efb78de5a03f9d4aa9c5ede0d680f1fe5b67f
00dcf2457307a3cbdfe416bd807bc8c5565c95aa
/esp_metrique.lean
8d8c92a013d2d883ba04030a461ef3f49b0c797f
[]
no_license
PatrickMassot/LEAN_ESPACES_METRIQUES
ae7703001606a5688a9a90388f3b79acc839820b
08e6d69d10ea728a520b9d9ad2c3ed9411bc89bf
refs/heads/master
1,601,215,582,943
1,575,726,003,000
1,575,726,003,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,794
lean
import tactic import data.real.basic import data.set ------------ -- ESSAIS -- ------------ open set example (α : Type) (s : set (set α)) : ∀ t : set α, t ∈ s → t ⊆ sUnion s := begin --library_search, intro h, exact subset_sUnion_of_mem, end ----------- -- DEBUT -- ----------- /-- Une structure d'espace métrique sur un type X -/ class espace_metrique (X : Type) := (dist : X → X → ℝ) (dist_pos : ∀ x y, dist x y ≥ 0) (sep : ∀ x y, dist x y = 0 ↔ x = y) (triangle : ∀ x y z, dist x z ≤ dist x y + dist y z) open espace_metrique /-- Fonction distance avec le type en argument explicite -/ def dist' (X : Type) [espace_metrique X] : X → X → ℝ := λ x y, dist x y notation `d` := dist notation `d_[` X `]` := dist' X ---------------------------------------------------- section fondements variables {X : Type} [espace_metrique X] /-- `boule x r` est la boule ouverte de centre `x` et de rayon `r` -/ def boule (x : X) (r : ℝ) := {y | dist x y < r} lemma mem_boule (x : X) (r : ℝ) : ∀ y, y ∈ boule x r ↔ dist x y < r := assume y, iff.rfl variables (x y : X) (r : ℝ) #check boule x r #check @boule X _ x r example : d_[X] x y = d x y := begin refl, end /-- Une partie d'un espace métrique `X` est ouverte si elle contient une boule ouverte de rayon strictement positif autour de chacun de ses points. -/ def ouvert (A : set X) := ∀ x ∈ A, ∃ r > 0, boule x r ⊆ A lemma boule_est_ouverte : ∀ x : X, ∀ r > 0, ouvert (boule x r) := begin intros x r r_pos y y_in, -- on déroule les définitions, -- on se retrouve avec un point y dans la boule -- de centre x et de rayon r, et on cherche une boule autour de y qui soit incluse -- dans boule x r use r - d x y, -- le rayon candidat rw exists_prop, split, { -- La ligne suivante peut être remplacée par n'importe laquelle des trois lignes qui la suivent simp [boule] at y_in, --change d x y < r at y_in, --rw mem_boule at y_in, --unfold boule at y_in, rw set.mem_set_of_eq at y_in, linarith }, -- le rayon est bien strictement positif { -- La ligne suivante est optionnelle, elle sert à expliciter le but -- change ∀ z, z ∈ boule y (r - d x y) → z ∈ boule x r, intros z z_in, rw mem_boule at *, have clef : d x z ≤ d x y + d y z, from triangle x y z, linarith }, -- et l'inégalité triangulaire permet de montrer l'inclusion des boules end -- Lemme de théorie des ensembles - finalement non utilisé lemma inclusion_transitive {Y : Type} {A B C : set Y} : A ⊆ B → B ⊆ C → A ⊆ C := begin intros AB BC a a_app_A, exact BC (AB a_app_A), end lemma union_ouverts_est_ouvert (I : set (set X)) : (∀ O ∈ I, ouvert O) → ouvert (⋃₀ I) := begin intro O_ouverts, -- les ensembles O sont ouverts intros x x_dans_union, -- un point dans l'union cases x_dans_union with O x_app_O', -- donc dans l'un des ouverts O rw exists_prop at x_app_O', cases x_app_O' with O_app_I x_app_O, have O_ouvert : ouvert O, from (O_ouverts O) O_app_I, have existe_r, from O_ouvert x x_app_O, -- O est ouvert, ce qui fourni un rayon cases existe_r with r hr, rw exists_prop at hr, cases hr with r_positif boule_dans_O, use r, -- on utilise ce rayon pour montrer que l'union est ouverte rw exists_prop, split, assumption, -- il est bien positif have O_dans_union : O ⊆ ⋃₀ I, from by exact subset_sUnion_of_mem O_app_I, -- Les deux lignes suivantes démontrent, dans notre contexte, -- que l'inclusion est transitive, on peut les remplacer par -- la dernière ligne commentée intros y H, exact O_dans_union (boule_dans_O H), -- exact inclusion_transitive boule_dans_O O_dans_union, end -- on montre que l'intersection de deux ouvert est un ouvert, -- puis intersection finie par récurrence (ou directement) lemma intersection_deux_ouverts_est_ouvert : ∀ O₁ O₂ : set X, (ouvert O₁ ∧ ouvert O₂) → ouvert (O₁ ∩ O₂) := begin sorry end variable β : Type lemma intersection_ouverts_est_ouvert [fintype β] {O : β → set X} (h : ∀ i, ouvert (O i)) : ouvert (⋂ i, O i) := begin sorry end -- comment formuler que X et ∅ sont ouverts ? -- def topo de l'intérieur. Caractérisation métrique. -- Voisinages ? end fondements ------------------------------------------------ section continuite variables {X Y : Type} [espace_metrique X] [espace_metrique Y] -- dans la définition suivante les `d_[X]` et `d_[Y]` sont cosmétiques, `d` seul marche aussi bien def continue_en (f : X → Y) (x₀ : X) := ∀ ε > 0, ∃ δ > 0, ∀ x, d_[X] x x₀ < δ → d_[Y] (f x) (f x₀) < ε def continuite (f:X → Y) := ∀ x : X, continue_en f x -- Notations f continue, f continue_au_point x end continuite
f62e5ff027dda40dbd4b36b7bd79144182f0ce1d
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/linear_algebra/alternating.lean
5dd5aca9371e0ea2f9a1b818b6c01402deb2390e
[ "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
29,268
lean
/- Copyright (c) 2020 Zhangir Azerbayev. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser, Zhangir Azerbayev -/ import linear_algebra.multilinear import linear_algebra.linear_independent import group_theory.perm.sign import group_theory.perm.subgroup import data.equiv.fin import linear_algebra.tensor_product import group_theory.quotient_group /-! # Alternating Maps We construct the bundled function `alternating_map`, which extends `multilinear_map` with all the arguments of the same type. ## Main definitions * `alternating_map R M N ι` is the space of `R`-linear alternating maps from `ι → M` to `N`. * `f.map_eq_zero_of_eq` expresses that `f` is zero when two inputs are equal. * `f.map_swap` expresses that `f` is negated when two inputs are swapped. * `f.map_perm` expresses how `f` varies by a sign change under a permutation of its inputs. * An `add_comm_monoid`, `add_comm_group`, and `semimodule` structure over `alternating_map`s that matches the definitions over `multilinear_map`s. * `multilinear_map.alternatization`, which makes an alternating map out of a non-alternating one. * `alternating_map.dom_coprod`, which behaves as a product between two alternating maps. ## Implementation notes `alternating_map` is defined in terms of `map_eq_zero_of_eq`, as this is easier to work with than using `map_swap` as a definition, and does not require `has_neg N`. `alternating_map`s are provided with a coercion to `multilinear_map`, along with a set of `norm_cast` lemmas that act on the algebraic structure: * `alternating_map.coe_add` * `alternating_map.coe_zero` * `alternating_map.coe_sub` * `alternating_map.coe_neg` * `alternating_map.coe_smul` -/ -- semiring / add_comm_monoid variables {R : Type*} [semiring R] variables {M : Type*} [add_comm_monoid M] [semimodule R M] variables {N : Type*} [add_comm_monoid N] [semimodule R N] -- semiring / add_comm_group variables {M' : Type*} [add_comm_group M'] [semimodule R M'] variables {N' : Type*} [add_comm_group N'] [semimodule R N'] variables {ι : Type*} [decidable_eq ι] set_option old_structure_cmd true section variables (R M N ι) /-- An alternating map is a multilinear map that vanishes when two of its arguments are equal. -/ structure alternating_map extends multilinear_map R (λ i : ι, M) N := (map_eq_zero_of_eq' : ∀ (v : ι → M) (i j : ι) (h : v i = v j) (hij : i ≠ j), to_fun v = 0) end /-- The multilinear map associated to an alternating map -/ add_decl_doc alternating_map.to_multilinear_map namespace alternating_map variables (f f' : alternating_map R M N ι) variables (g g₂ : alternating_map R M N' ι) variables (g' : alternating_map R M' N' ι) variables (v : ι → M) (v' : ι → M') open function /-! Basic coercion simp lemmas, largely copied from `ring_hom` and `multilinear_map` -/ section coercions instance : has_coe_to_fun (alternating_map R M N ι) := ⟨_, λ x, x.to_fun⟩ initialize_simps_projections alternating_map (to_fun → apply) @[simp] lemma to_fun_eq_coe : f.to_fun = f := rfl @[simp] lemma coe_mk (f : (ι → M) → N) (h₁ h₂ h₃) : ⇑(⟨f, h₁, h₂, h₃⟩ : alternating_map R M N ι) = f := rfl theorem congr_fun {f g : alternating_map R M N ι} (h : f = g) (x : ι → M) : f x = g x := congr_arg (λ h : alternating_map R M N ι, h x) h theorem congr_arg (f : alternating_map R M N ι) {x y : ι → M} (h : x = y) : f x = f y := congr_arg (λ x : ι → M, f x) h theorem coe_inj ⦃f g : alternating_map R M N ι⦄ (h : ⇑f = g) : f = g := by { cases f, cases g, cases h, refl } @[ext] theorem ext {f f' : alternating_map R M N ι} (H : ∀ x, f x = f' x) : f = f' := coe_inj (funext H) theorem ext_iff {f g : alternating_map R M N ι} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ instance : has_coe (alternating_map R M N ι) (multilinear_map R (λ i : ι, M) N) := ⟨λ x, x.to_multilinear_map⟩ @[simp, norm_cast] lemma coe_multilinear_map : ⇑(f : multilinear_map R (λ i : ι, M) N) = f := rfl lemma coe_multilinear_map_injective : function.injective (coe : alternating_map R M N ι → multilinear_map R (λ i : ι, M) N) := λ x y h, ext $ multilinear_map.congr_fun h @[simp] lemma to_multilinear_map_eq_coe : f.to_multilinear_map = f := rfl @[simp] lemma coe_multilinear_map_mk (f : (ι → M) → N) (h₁ h₂ h₃) : ((⟨f, h₁, h₂, h₃⟩ : alternating_map R M N ι) : multilinear_map R (λ i : ι, M) N) = ⟨f, h₁, h₂⟩ := rfl end coercions /-! ### Simp-normal forms of the structure fields These are expressed in terms of `⇑f` instead of `f.to_fun`. -/ @[simp] lemma map_add (i : ι) (x y : M) : f (update v i (x + y)) = f (update v i x) + f (update v i y) := f.to_multilinear_map.map_add' v i x y @[simp] lemma map_sub (i : ι) (x y : M') : g' (update v' i (x - y)) = g' (update v' i x) - g' (update v' i y) := g'.to_multilinear_map.map_sub v' i x y @[simp] lemma map_neg (i : ι) (x : M') : g' (update v' i (-x)) = -g' (update v' i x) := g'.to_multilinear_map.map_neg v' i x @[simp] lemma map_smul (i : ι) (r : R) (x : M) : f (update v i (r • x)) = r • f (update v i x) := f.to_multilinear_map.map_smul' v i r x @[simp] lemma map_eq_zero_of_eq (v : ι → M) {i j : ι} (h : v i = v j) (hij : i ≠ j) : f v = 0 := f.map_eq_zero_of_eq' v i j h hij lemma map_coord_zero {m : ι → M} (i : ι) (h : m i = 0) : f m = 0 := f.to_multilinear_map.map_coord_zero i h @[simp] lemma map_update_zero (m : ι → M) (i : ι) : f (update m i 0) = 0 := f.to_multilinear_map.map_update_zero m i @[simp] lemma map_zero [nonempty ι] : f 0 = 0 := f.to_multilinear_map.map_zero /-! ### Algebraic structure inherited from `multilinear_map` `alternating_map` carries the same `add_comm_monoid`, `add_comm_group`, and `semimodule` structure as `multilinear_map` -/ instance : has_add (alternating_map R M N ι) := ⟨λ a b, { map_eq_zero_of_eq' := λ v i j h hij, by simp [a.map_eq_zero_of_eq v h hij, b.map_eq_zero_of_eq v h hij], ..(a + b : multilinear_map R (λ i : ι, M) N)}⟩ @[simp] lemma add_apply : (f + f') v = f v + f' v := rfl @[norm_cast] lemma coe_add : (↑(f + f') : multilinear_map R (λ i : ι, M) N) = f + f' := rfl instance : has_zero (alternating_map R M N ι) := ⟨{map_eq_zero_of_eq' := λ v i j h hij, by simp, ..(0 : multilinear_map R (λ i : ι, M) N)}⟩ @[simp] lemma zero_apply : (0 : alternating_map R M N ι) v = 0 := rfl @[norm_cast] lemma coe_zero : ((0 : alternating_map R M N ι) : multilinear_map R (λ i : ι, M) N) = 0 := rfl instance : inhabited (alternating_map R M N ι) := ⟨0⟩ instance : add_comm_monoid (alternating_map R M N ι) := by refine {zero := 0, add := (+), ..}; intros; ext; simp [add_comm, add_left_comm] instance : has_neg (alternating_map R M N' ι) := ⟨λ f, { map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij], ..(-(f : multilinear_map R (λ i : ι, M) N')) }⟩ @[simp] lemma neg_apply (m : ι → M) : (-g) m = -(g m) := rfl @[norm_cast] lemma coe_neg : ((-g : alternating_map R M N' ι) : multilinear_map R (λ i : ι, M) N') = -g := rfl instance : has_sub (alternating_map R M N' ι) := ⟨λ f g, { map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij, g.map_eq_zero_of_eq v h hij], ..(f - g : multilinear_map R (λ i : ι, M) N') }⟩ @[simp] lemma sub_apply (m : ι → M) : (g - g₂) m = g m - g₂ m := rfl @[norm_cast] lemma coe_sub : (↑(g - g₂) : multilinear_map R (λ i : ι, M) N') = g - g₂ := rfl instance : add_comm_group (alternating_map R M N' ι) := by refine {zero := 0, add := (+), neg := has_neg.neg, sub := has_sub.sub, sub_eq_add_neg := _, ..}; intros; ext; simp [add_comm, add_left_comm, sub_eq_add_neg] section distrib_mul_action variables {S : Type*} [monoid S] [distrib_mul_action S N] [smul_comm_class R S N] instance : has_scalar S (alternating_map R M N ι) := ⟨λ c f, { map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij], ..((c • f : multilinear_map R (λ i : ι, M) N)) }⟩ @[simp] lemma smul_apply (c : S) (m : ι → M) : (c • f) m = c • f m := rfl @[norm_cast] lemma coe_smul (c : S): ((c • f : alternating_map R M N ι) : multilinear_map R (λ i : ι, M) N) = c • f := rfl instance : distrib_mul_action S (alternating_map R M N ι) := { one_smul := λ f, ext $ λ x, one_smul _ _, mul_smul := λ c₁ c₂ f, ext $ λ x, mul_smul _ _ _, smul_zero := λ r, ext $ λ x, smul_zero _, smul_add := λ r f₁ f₂, ext $ λ x, smul_add _ _ _ } end distrib_mul_action section semimodule variables {S : Type*} [semiring S] [semimodule S N] [smul_comm_class R S N] /-- The space of multilinear maps over an algebra over `R` is a module over `R`, for the pointwise addition and scalar multiplication. -/ instance : semimodule S (alternating_map R M N ι) := { add_smul := λ r₁ r₂ f, ext $ λ x, add_smul _ _ _, zero_smul := λ f, ext $ λ x, zero_smul _ _ } end semimodule end alternating_map /-! ### Composition with linear maps -/ namespace linear_map variables {N₂ : Type*} [add_comm_monoid N₂] [semimodule R N₂] /-- Composing a alternating map with a linear map gives again a alternating map. -/ def comp_alternating_map (g : N →ₗ[R] N₂) : alternating_map R M N ι →+ alternating_map R M N₂ ι := { to_fun := λ f, { map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij], ..(g.comp_multilinear_map (f : multilinear_map R (λ _ : ι, M) N)) }, map_zero' := by { ext, simp }, map_add' := λ a b, by { ext, simp } } @[simp] lemma coe_comp_alternating_map (g : N →ₗ[R] N₂) (f : alternating_map R M N ι) : ⇑(g.comp_alternating_map f) = g ∘ f := rfl lemma comp_alternating_map_apply (g : N →ₗ[R] N₂) (f : alternating_map R M N ι) (m : ι → M) : g.comp_alternating_map f m = g (f m) := rfl end linear_map namespace alternating_map variables (f f' : alternating_map R M N ι) variables (g g₂ : alternating_map R M N' ι) variables (g' : alternating_map R M' N' ι) variables (v : ι → M) (v' : ι → M') open function /-! ### Other lemmas from `multilinear_map` -/ section open_locale big_operators lemma map_update_sum {α : Type*} (t : finset α) (i : ι) (g : α → M) (m : ι → M): f (update m i (∑ a in t, g a)) = ∑ a in t, f (update m i (g a)) := f.to_multilinear_map.map_update_sum t i g m end /-! ### Theorems specific to alternating maps Various properties of reordered and repeated inputs which follow from `alternating_map.map_eq_zero_of_eq`. -/ lemma map_update_self {i j : ι} (hij : i ≠ j) : f (function.update v i (v j)) = 0 := f.map_eq_zero_of_eq _ (by rw [function.update_same, function.update_noteq hij.symm]) hij lemma map_update_update {i j : ι} (hij : i ≠ j) (m : M) : f (function.update (function.update v i m) j m) = 0 := f.map_eq_zero_of_eq _ (by rw [function.update_same, function.update_noteq hij, function.update_same]) hij lemma map_swap_add {i j : ι} (hij : i ≠ j) : f (v ∘ equiv.swap i j) + f v = 0 := begin rw equiv.comp_swap_eq_update, convert f.map_update_update v hij (v i + v j), simp [f.map_update_self _ hij, f.map_update_self _ hij.symm, function.update_comm hij (v i + v j) (v _) v, function.update_comm hij.symm (v i) (v i) v], end lemma map_add_swap {i j : ι} (hij : i ≠ j) : f v + f (v ∘ equiv.swap i j) = 0 := by { rw add_comm, exact f.map_swap_add v hij } lemma map_swap {i j : ι} (hij : i ≠ j) : g (v ∘ equiv.swap i j) = - g v := eq_neg_of_add_eq_zero (g.map_swap_add v hij) lemma map_perm [fintype ι] (v : ι → M) (σ : equiv.perm ι) : g (v ∘ σ) = (σ.sign : ℤ) • g v := begin apply equiv.perm.swap_induction_on' σ, { simp }, { intros s x y hxy hI, simpa [g.map_swap (v ∘ s) hxy, equiv.perm.sign_swap hxy] using hI, } end lemma map_congr_perm [fintype ι] (σ : equiv.perm ι) : g v = (σ.sign : ℤ) • g (v ∘ σ) := by { rw [g.map_perm, smul_smul], simp } lemma coe_dom_dom_congr [fintype ι] (σ : equiv.perm ι) : (g : multilinear_map R (λ _ : ι, M) N').dom_dom_congr σ = (σ.sign : ℤ) • (g : multilinear_map R (λ _ : ι, M) N') := multilinear_map.ext $ λ v, g.map_perm v σ /-- If the arguments are linearly dependent then the result is `0`. -/ lemma map_linear_dependent {K : Type*} [ring K] {M : Type*} [add_comm_group M] [semimodule K M] {N : Type*} [add_comm_group N] [semimodule K N] [no_zero_smul_divisors K N] (f : alternating_map K M N ι) (v : ι → M) (h : ¬linear_independent K v) : f v = 0 := begin obtain ⟨s, g, h, i, hi, hz⟩ := linear_dependent_iff.mp h, suffices : f (update v i (g i • v i)) = 0, { rw [f.map_smul, function.update_eq_self, smul_eq_zero] at this, exact or.resolve_left this hz, }, conv at h in (g _ • v _) { rw ←if_t_t (i = x) (g _ • v _), }, rw [finset.sum_ite, finset.filter_eq, finset.filter_ne, if_pos hi, finset.sum_singleton, add_eq_zero_iff_eq_neg] at h, rw [h, f.map_neg, f.map_update_sum, neg_eq_zero, finset.sum_eq_zero], intros j hj, obtain ⟨hij, _⟩ := finset.mem_erase.mp hj, rw [f.map_smul, f.map_update_self _ hij.symm, smul_zero], end end alternating_map open_locale big_operators namespace multilinear_map open equiv variables [fintype ι] private lemma alternization_map_eq_zero_of_eq_aux (m : multilinear_map R (λ i : ι, M) N') (v : ι → M) (i j : ι) (i_ne_j : i ≠ j) (hv : v i = v j) : (∑ (σ : perm ι), (σ.sign : ℤ) • m.dom_dom_congr σ) v = 0 := begin rw sum_apply, exact finset.sum_involution (λ σ _, swap i j * σ) (λ σ _, by simp [perm.sign_swap i_ne_j, apply_swap_eq_self hv]) (λ σ _ _, (not_congr swap_mul_eq_iff).mpr i_ne_j) (λ σ _, finset.mem_univ _) (λ σ _, swap_mul_involutive i j σ) end /-- Produce an `alternating_map` out of a `multilinear_map`, by summing over all argument permutations. -/ def alternatization : multilinear_map R (λ i : ι, M) N' →+ alternating_map R M N' ι := { to_fun := λ m, { to_fun := ⇑(∑ (σ : perm ι), (σ.sign : ℤ) • m.dom_dom_congr σ), map_eq_zero_of_eq' := λ v i j hvij hij, alternization_map_eq_zero_of_eq_aux m v i j hij hvij, .. (∑ (σ : perm ι), (σ.sign : ℤ) • m.dom_dom_congr σ)}, map_add' := λ a b, begin ext, simp only [ finset.sum_add_distrib, smul_add, add_apply, dom_dom_congr_apply, alternating_map.add_apply, alternating_map.coe_mk, smul_apply, sum_apply], end, map_zero' := begin ext, simp only [ finset.sum_const_zero, smul_zero, zero_apply, dom_dom_congr_apply, alternating_map.zero_apply, alternating_map.coe_mk, smul_apply, sum_apply], end } lemma alternatization_def (m : multilinear_map R (λ i : ι, M) N') : ⇑(alternatization m) = (∑ (σ : perm ι), (σ.sign : ℤ) • m.dom_dom_congr σ : _) := rfl lemma alternatization_coe (m : multilinear_map R (λ i : ι, M) N') : ↑m.alternatization = (∑ (σ : perm ι), (σ.sign : ℤ) • m.dom_dom_congr σ : _) := coe_inj rfl lemma alternatization_apply (m : multilinear_map R (λ i : ι, M) N') (v : ι → M) : alternatization m v = ∑ (σ : perm ι), (σ.sign : ℤ) • m.dom_dom_congr σ v := by simp only [alternatization_def, smul_apply, sum_apply] end multilinear_map namespace alternating_map /-- Alternatizing a multilinear map that is already alternating results in a scale factor of `n!`, where `n` is the number of inputs. -/ lemma coe_alternatization [fintype ι] (a : alternating_map R M N' ι) : (↑a : multilinear_map R (λ ι, M) N').alternatization = nat.factorial (fintype.card ι) • a := begin apply alternating_map.coe_inj, rw multilinear_map.alternatization_def, simp_rw [coe_dom_dom_congr, smul_smul, int.units_coe_mul_self, one_smul, finset.sum_const, finset.card_univ, fintype.card_perm, nsmul_eq_smul], rw [←coe_multilinear_map, coe_smul], end end alternating_map namespace linear_map variables {N'₂ : Type*} [add_comm_group N'₂] [semimodule R N'₂] [fintype ι] /-- Composition with a linear map before and after alternatization are equivalent. -/ lemma comp_multilinear_map_alternatization (g : N' →ₗ[R] N'₂) (f : multilinear_map R (λ _ : ι, M) N') : (g.comp_multilinear_map f).alternatization = g.comp_alternating_map (f.alternatization) := by { ext, simp [multilinear_map.alternatization_def] } end linear_map section coprod open_locale big_operators open_locale tensor_product variables {ιa ιb : Type*} [decidable_eq ιa] [decidable_eq ιb] [fintype ιa] [fintype ιb] variables {R' : Type*} {Mᵢ N₁ N₂ : Type*} [comm_semiring R'] [add_comm_group N₁] [semimodule R' N₁] [add_comm_group N₂] [semimodule R' N₂] [add_comm_monoid Mᵢ] [semimodule R' Mᵢ] namespace equiv.perm /-- Elements which are considered equivalent if they differ only by swaps within α or β -/ abbreviation mod_sum_congr (α β : Type*) := quotient_group.quotient (equiv.perm.sum_congr_hom α β).range lemma mod_sum_congr.swap_smul_involutive {α β : Type*} [decidable_eq (α ⊕ β)] (i j : α ⊕ β) : function.involutive (has_scalar.smul (equiv.swap i j) : mod_sum_congr α β → mod_sum_congr α β) := λ σ, begin apply σ.induction_on' (λ σ, _), exact _root_.congr_arg quotient.mk' (equiv.swap_mul_involutive i j σ) end end equiv.perm namespace alternating_map open equiv /-- summand used in `alternating_map.dom_coprod` -/ def dom_coprod.summand (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) (σ : perm.mod_sum_congr ιa ιb) : multilinear_map R' (λ _ : ιa ⊕ ιb, Mᵢ) (N₁ ⊗[R'] N₂) := quotient.lift_on' σ (λ σ, (σ.sign : ℤ) • (multilinear_map.dom_coprod ↑a ↑b : multilinear_map R' (λ _, Mᵢ) (N₁ ⊗ N₂)).dom_dom_congr σ) (λ σ₁ σ₂ ⟨⟨sl, sr⟩, h⟩, begin ext v, simp only [multilinear_map.dom_dom_congr_apply, multilinear_map.dom_coprod_apply, coe_multilinear_map, multilinear_map.smul_apply], replace h := inv_mul_eq_iff_eq_mul.mp h.symm, have : ((σ₁ * perm.sum_congr_hom _ _ (sl, sr)).sign : ℤ) = σ₁.sign * (sl.sign * sr.sign) := by simp, rw [h, this, mul_smul, mul_smul, units.smul_left_cancel, ←tensor_product.tmul_smul, tensor_product.smul_tmul'], simp only [sum.map_inr, perm.sum_congr_hom_apply, perm.sum_congr_apply, sum.map_inl, function.comp_app, perm.coe_mul], rw [←a.map_congr_perm (λ i, v (σ₁ _)), ←b.map_congr_perm (λ i, v (σ₁ _))], end) lemma dom_coprod.summand_mk' (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) (σ : equiv.perm (ιa ⊕ ιb)) : dom_coprod.summand a b (quotient.mk' σ) = (σ.sign : ℤ) • (multilinear_map.dom_coprod ↑a ↑b : multilinear_map R' (λ _, Mᵢ) (N₁ ⊗ N₂)).dom_dom_congr σ := rfl /-- Swapping elements in `σ` with equal values in `v` results in an addition that cancels -/ lemma dom_coprod.summand_add_swap_smul_eq_zero (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) (σ : perm.mod_sum_congr ιa ιb) {v : ιa ⊕ ιb → Mᵢ} {i j : ιa ⊕ ιb} (hv : v i = v j) (hij : i ≠ j) : dom_coprod.summand a b σ v + dom_coprod.summand a b (swap i j • σ) v = 0 := begin apply σ.induction_on' (λ σ, _), dsimp only [quotient.lift_on'_mk', quotient.map'_mk', mul_action.quotient.smul_mk, dom_coprod.summand], rw [perm.sign_mul, perm.sign_swap hij], simp only [one_mul, units.neg_mul, function.comp_app, neg_smul, perm.coe_mul, units.coe_neg, multilinear_map.smul_apply, multilinear_map.neg_apply, multilinear_map.dom_dom_congr_apply, multilinear_map.dom_coprod_apply], convert add_right_neg _; { ext k, rw equiv.apply_swap_eq_self hv }, end /-- Swapping elements in `σ` with equal values in `v` result in zero if the swap has no effect on the quotient. -/ lemma dom_coprod.summand_eq_zero_of_smul_invariant (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) (σ : perm.mod_sum_congr ιa ιb) {v : ιa ⊕ ιb → Mᵢ} {i j : ιa ⊕ ιb} (hv : v i = v j) (hij : i ≠ j) : swap i j • σ = σ → dom_coprod.summand a b σ v = 0 := begin apply σ.induction_on' (λ σ, _), dsimp only [quotient.lift_on'_mk', quotient.map'_mk', multilinear_map.smul_apply, multilinear_map.dom_dom_congr_apply, multilinear_map.dom_coprod_apply, dom_coprod.summand], intro hσ, with_cases { cases hi : σ⁻¹ i; cases hj : σ⁻¹ j; rw perm.inv_eq_iff_eq at hi hj; substs hi hj, }, case [sum.inl sum.inr : i' j', sum.inr sum.inl : i' j'] { -- the term pairs with and cancels another term all_goals { obtain ⟨⟨sl, sr⟩, hσ⟩ := quotient.exact' hσ, }, work_on_goal 0 { replace hσ := equiv.congr_fun hσ (sum.inl i'), }, work_on_goal 1 { replace hσ := equiv.congr_fun hσ (sum.inr i'), }, all_goals { rw [←equiv.mul_swap_eq_swap_mul, mul_inv_rev, equiv.swap_inv, inv_mul_cancel_right] at hσ, simpa using hσ, }, }, case [sum.inr sum.inr : i' j', sum.inl sum.inl : i' j'] { -- the term does not pair but is zero all_goals { convert smul_zero _, }, work_on_goal 0 { convert tensor_product.tmul_zero _ _, }, work_on_goal 1 { convert tensor_product.zero_tmul _ _, }, all_goals { exact alternating_map.map_eq_zero_of_eq _ _ hv (λ hij', hij (hij' ▸ rfl)), }, }, end /-- Like `multilinear_map.dom_coprod`, but ensures the result is also alternating. Note that this is usually defined (for instance, as used in Proposition 22.24 in [Gallier2011Notes]) over integer indices `ιa = fin n` and `ιb = fin m`, as $$ (f \wedge g)(u_1, \ldots, u_{m+n}) = \sum_{\operatorname{shuffle}(m, n)} \operatorname{sign}(\sigma) f(u_{\sigma(1)}, \ldots, u_{\sigma(m)}) g(u_{\sigma(m+1)}, \ldots, u_{\sigma(m+n)}), $$ where $\operatorname{shuffle}(m, n)$ consists of all permutations of $[1, m+n]$ such that $\sigma(1) < \cdots < \sigma(m)$ and $\sigma(m+1) < \cdots < \sigma(m+n)$. Here, we generalize this by replacing: * the product in the sum with a tensor product * the filtering of $[1, m+n]$ to shuffles with an isomorphic quotient * the additions in the subscripts of $\sigma$ with an index of type `sum` The specialized version can be obtained by combining this definition with `fin_sum_fin_equiv` and `algebra.lmul'`. -/ @[simps] def dom_coprod (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) : alternating_map R' Mᵢ (N₁ ⊗[R'] N₂) (ιa ⊕ ιb) := { to_fun := λ v, ⇑(∑ σ : perm.mod_sum_congr ιa ιb, dom_coprod.summand a b σ) v, map_eq_zero_of_eq' := λ v i j hv hij, begin dsimp only, rw multilinear_map.sum_apply, exact finset.sum_involution (λ σ _, equiv.swap i j • σ) (λ σ _, dom_coprod.summand_add_swap_smul_eq_zero a b σ hv hij) (λ σ _, mt $ dom_coprod.summand_eq_zero_of_smul_invariant a b σ hv hij) (λ σ _, finset.mem_univ _) (λ σ _, equiv.perm.mod_sum_congr.swap_smul_involutive i j σ), end, ..(∑ σ : perm.mod_sum_congr ιa ιb, dom_coprod.summand a b σ) } lemma dom_coprod_coe (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) : (↑(a.dom_coprod b) : multilinear_map R' (λ _, Mᵢ) _) = ∑ σ : perm.mod_sum_congr ιa ιb, dom_coprod.summand a b σ := multilinear_map.ext $ λ _, rfl /-- A more bundled version of `alternating_map.dom_coprod` that maps `((ι₁ → N) → N₁) ⊗ ((ι₂ → N) → N₂)` to `(ι₁ ⊕ ι₂ → N) → N₁ ⊗ N₂`. -/ def dom_coprod' : (alternating_map R' Mᵢ N₁ ιa ⊗[R'] alternating_map R' Mᵢ N₂ ιb) →ₗ[R'] alternating_map R' Mᵢ (N₁ ⊗[R'] N₂) (ιa ⊕ ιb) := tensor_product.lift $ by refine linear_map.mk₂ R' (dom_coprod) (λ m₁ m₂ n, _) (λ c m n, _) (λ m n₁ n₂, _) (λ c m n, _); { ext, simp only [dom_coprod_apply, add_apply, smul_apply, ←finset.sum_add_distrib, finset.smul_sum, multilinear_map.sum_apply, dom_coprod.summand], congr, ext σ, apply σ.induction_on' (λ σ, _), simp only [quotient.lift_on'_mk', coe_add, coe_smul, multilinear_map.smul_apply, ←multilinear_map.dom_coprod'_apply], simp only [tensor_product.add_tmul, ←tensor_product.smul_tmul', tensor_product.tmul_add, tensor_product.tmul_smul, linear_map.map_add, linear_map.map_smul], rw ←smul_add <|> rw smul_comm, congr } @[simp] lemma dom_coprod'_apply (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) : dom_coprod' (a ⊗ₜ[R'] b) = dom_coprod a b := by simp only [dom_coprod', tensor_product.lift.tmul, linear_map.mk₂_apply] end alternating_map open equiv /-- A helper lemma for `multilinear_map.dom_coprod_alternization`. -/ lemma multilinear_map.dom_coprod_alternization_coe (a : multilinear_map R' (λ _ : ιa, Mᵢ) N₁) (b : multilinear_map R' (λ _ : ιb, Mᵢ) N₂) : multilinear_map.dom_coprod ↑a.alternatization ↑b.alternatization = ∑ (σa : perm ιa) (σb : perm ιb), (σa.sign : ℤ) • (σb.sign : ℤ) • multilinear_map.dom_coprod (a.dom_dom_congr σa) (b.dom_dom_congr σb) := begin simp_rw [←multilinear_map.dom_coprod'_apply, multilinear_map.alternatization_coe], simp_rw [tensor_product.sum_tmul, tensor_product.tmul_sum, linear_map.map_sum, ←tensor_product.smul_tmul', tensor_product.tmul_smul, linear_map.map_smul_of_tower], end open alternating_map /-- Computing the `multilinear_map.alternatization` of the `multilinear_map.dom_coprod` is the same as computing the `alternating_map.dom_coprod` of the `multilinear_map.alternatization`s. -/ lemma multilinear_map.dom_coprod_alternization (a : multilinear_map R' (λ _ : ιa, Mᵢ) N₁) (b : multilinear_map R' (λ _ : ιb, Mᵢ) N₂) : (multilinear_map.dom_coprod a b).alternatization = a.alternatization.dom_coprod b.alternatization := begin apply coe_multilinear_map_injective, rw [dom_coprod_coe, multilinear_map.alternatization_coe, finset.sum_partition (quotient_group.left_rel (perm.sum_congr_hom ιa ιb).range)], congr' 1, ext1 σ, apply σ.induction_on' (λ σ, _), -- unfold the quotient mess left by `finset.sum_partition` conv in (_ = quotient.mk' _) { change quotient.mk' _ = quotient.mk' _, rw quotient.eq', rw [quotient_group.left_rel], dsimp only [setoid.r] }, -- eliminate a multiplication have : @finset.univ (perm (ιa ⊕ ιb)) _ = finset.univ.image ((*) σ) := (finset.eq_univ_iff_forall.mpr $ λ a, let ⟨a', ha'⟩ := mul_left_surjective σ a in finset.mem_image.mpr ⟨a', finset.mem_univ _, ha'⟩).symm, rw [this, finset.image_filter], simp only [function.comp, mul_inv_rev, inv_mul_cancel_right, subgroup.inv_mem_iff], simp only [monoid_hom.mem_range], -- needs to be separate from the above `simp only` rw [finset.filter_congr_decidable, finset.univ_filter_exists (perm.sum_congr_hom ιa ιb), finset.sum_image (λ x _ y _ (h : _ = _), mul_right_injective _ h), finset.sum_image (λ x _ y _ (h : _ = _), perm.sum_congr_hom_injective h)], dsimp only, -- now we're ready to clean up the RHS, pulling out the summation rw [dom_coprod.summand_mk', multilinear_map.dom_coprod_alternization_coe, ←finset.sum_product', finset.univ_product_univ, ←multilinear_map.dom_dom_congr_equiv_apply, add_equiv.map_sum, finset.smul_sum], congr' 1, ext1 ⟨al, ar⟩, dsimp only, -- pull out the pair of smuls on the RHS, by rewriting to `add_monoid_hom` and back rw [←add_equiv.coe_to_add_monoid_hom, add_monoid_hom.map_int_module_smul, add_monoid_hom.map_int_module_smul, add_equiv.coe_to_add_monoid_hom, multilinear_map.dom_dom_congr_equiv_apply], -- pick up the pieces rw [multilinear_map.dom_dom_congr_mul, perm.sign_mul, units.coe_mul, perm.sum_congr_hom_apply, multilinear_map.dom_coprod_dom_dom_congr_sum_congr, perm.sign_sum_congr, units.coe_mul, ←mul_smul ↑al.sign ↑ar.sign, ←mul_smul], -- resolve typeclass diamonds in `has_scalar`. `congr` alone seems to make a wrong turn. congr' 3, end /-- Taking the `multilinear_map.alternatization` of the `multilinear_map.dom_coprod` of two `alternating_map`s gives a scaled version of the `alternating_map.coprod` of those maps. -/ lemma multilinear_map.dom_coprod_alternization_eq (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) : (multilinear_map.dom_coprod a b : multilinear_map R' (λ _ : ιa ⊕ ιb, Mᵢ) (N₁ ⊗ N₂)) .alternatization = ((fintype.card ιa).factorial * (fintype.card ιb).factorial) • a.dom_coprod b := begin rw [multilinear_map.dom_coprod_alternization, coe_alternatization, coe_alternatization, mul_smul, ←dom_coprod'_apply, ←dom_coprod'_apply, ←tensor_product.smul_tmul', tensor_product.tmul_smul, linear_map.map_smul_of_tower dom_coprod', linear_map.map_smul_of_tower dom_coprod'], -- typeclass resolution is a little confused here apply_instance, apply_instance, end end coprod