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
8b0b8de2cb7e6cd1542964697f83cf78b26f9450
843cffbd2a25fd01677509247a92e9e8b33bec48
/(a + b)^3.lean
870a9bf06b28a294dbed07748acd8349de9cf9ba
[]
no_license
AlexandruBosinta/MyLeanPlayground
9a78eba4a5c7a2b82edf84e1794a966f53f06c4f
5dc50a590d784bfc27e7fb37b6361a6dcc1b2790
refs/heads/master
1,586,230,010,351
1,542,487,422,000
1,542,487,422,000
158,016,626
0
0
null
null
null
null
UTF-8
Lean
false
false
3,920
lean
import data.real.basic variables (a b : ℝ) include a b example : (a + b) ^ 3 = a ^ 3 + 3 * (a ^ 2) * b + 3 * a * b ^ 2 + b ^ 3 := by calc (a + b) ^ 3 = (a + b) ^ (2 + 1) : rfl ... = (a + b) ^ 2 * (a + b) : by rw pow_succ' ... = (a + b) * (a + b) * (a + b) : by rw pow_two ... = ((a + b) * a + (a + b) * b) * (a + b) : by rw mul_add (a + b) a b ... = ((a * a + b * a) + (a * b + b * b)) * (a + b) : by rw [add_mul a b a, add_mul a b b] ... = (a * a + b * a + a * b + b * b) * (a + b) : by rw [add_assoc (a * a + b * a) (a * b) (b * b)] ... = (a ^ 2 + a * b + a * b + b ^ 2) * (a + b) : by rw [mul_comm b a, pow_two, pow_two] ... = (a ^ 2 + 1 * a * b + 1 * a * b + b ^ 2) * (a + b) : by rw one_mul ... = (a ^ 2 + (1 * a * b + 1 * a * b) + b ^ 2) * (a + b) : by rw add_assoc (a ^ 2) (1 * a * b) (1 * a * b) ... = (a ^ 2 + (1 * (a * b) + 1 * (a * b)) + b ^ 2) * (a + b) : by rw mul_assoc ... = (a ^ 2 + (1 + 1) * (a * b) + b ^ 2) * (a + b) : by rw add_mul 1 1 (a * b) ... = (a ^ 2 + 2 * (a * b) + b ^ 2) * (a + b) : rfl ... = (a ^ 2 + 2 * a * b + b ^ 2) * (a + b) : by rw mul_assoc ... = (a ^ 2 + 2 * a * b + b ^ 2) * a + (a ^ 2 + 2 * a * b + b ^ 2) * b : by rw mul_add ... = (a ^ 2 + 2 * a * b) * a + b ^ 2 * a + ((a ^ 2 + 2 * a * b) * b + b ^ 2 * b) : by rw [add_mul (a ^ 2 + 2 * a * b) (b ^ 2) a, add_mul (a ^ 2 + 2 * a * b) (b ^ 2) b] ... = a ^ 2 * a + 2 * a * b * a + a * b ^ 2 + (a ^ 2 * b + 2 * a * b * b + b ^ 3) : by rw [add_mul, add_mul, mul_comm (b ^ 2) a, pow_succ' b 2] ... = a ^ 3 + 2 * a * (b * a) + a * b ^ 2 + (a ^ 2 * b + 2 * a * (b * b) + b ^ 3) : by rw [pow_succ' a 2, mul_assoc, mul_assoc (2 * a) b b] ... = a ^ 3 + 2 * a * (a * b) + a * b ^ 2 + (a ^ 2 * b + 2 * a * b ^ 2 + b ^ 3) : by rw [mul_comm b a, pow_two] ... = a ^ 3 + 2 * a * a * b + a * b ^ 2 + (a ^ 2 * b + 2 * a * b ^ 2 + b ^ 3) : by rw [mul_assoc (2 * a) a b] ... = a ^ 3 + 2 * (a * a) * b + 1 * a * b ^ 2 + (1 * a ^ 2 * b + 2 * a * b ^ 2 + b ^ 3) : by rw [mul_assoc 2 a a, one_mul, one_mul] ... = a ^ 3 + 2 * a ^ 2 * b + 1 * a * b ^ 2 + (1 * a ^ 2 * b + 2 * a * b ^ 2 + b ^ 3) : by rw [pow_two a] ... = a ^ 3 + 2 * a ^ 2 * b + 1 * a * b ^ 2 + (1 * a ^ 2 * b + 2 * a * b ^ 2) + b ^ 3 : by rw [add_assoc (a ^ 3 + 2 * a ^ 2 * b + 1 * a * b ^ 2) (1 * a ^ 2 * b + 2 * a * b ^ 2) (b ^ 3)] ... = a ^ 3 + 2 * a ^ 2 * b + 1 * a * b ^ 2 + 1 * a ^ 2 * b + 2 * a * b ^ 2 + b ^ 3 : by rw [add_assoc (a ^ 3 + 2 * a ^ 2 * b + 1 * a * b ^ 2) (1 * a ^ 2 * b) (2 * a * b ^ 2)] ... = a ^ 3 + 2 * a ^ 2 * b + (1 * a * b ^ 2 + 1 * a ^ 2 * b) + 2 * a * b ^ 2 + b ^ 3 : by rw [add_assoc (a ^ 3 + 2 * a ^ 2 * b) (1 * a * b ^ 2) (1 * a ^ 2 * b)] ... = a ^ 3 + 2 * a ^ 2 * b + (1 * a ^ 2 * b + 1 * a * b ^ 2) + 2 * a * b ^ 2 + b ^ 3 : by rw [add_comm (1 * a ^ 2 * b) (1 * a * b ^ 2)] ... = a ^ 3 + 2 * a ^ 2 * b + 1 * a ^ 2 * b + 1 * a * b ^ 2 + 2 * a * b ^ 2 + b ^ 3 : by rw [add_assoc (a ^ 3 + 2 * a ^ 2 * b) (1 * a ^ 2 * b) (1 * a * b ^ 2)] ... = a ^ 3 + (2 * a ^ 2 * b + 1 * a ^ 2 * b) + (1 * a * b ^ 2 + 2 * a * b ^ 2) + b ^ 3 : by rw [add_assoc (a ^ 3) (2 * a ^ 2 * b) (1 * a ^ 2 * b), add_assoc (a ^ 3 + (2 * a ^ 2 * b + 1 * a ^ 2 * b)) (1 * a * b ^ 2) (2 * a * b ^ 2)] ... = a ^ 3 + (2 + 1) * (a ^ 2 * b) + (1 + 2) * (a * b ^ 2) + b ^ 3 : by rw [mul_assoc 2 (a ^ 2) b, mul_assoc 1 (a ^ 2) b, mul_assoc 1 a (b ^ 2), mul_assoc 2 a (b ^ 2), add_mul 2 1 (a ^ 2 * b), add_mul 1 2 (a * b ^ 2)] ... = a ^ 3 + 3 * (a ^ 2 * b) + 3 * (a * b ^ 2) + b ^ 3 : rfl ... = a ^ 3 + 3 * a ^ 2 * b + 3 * a * b ^ 2 + b ^ 3 : by rw [mul_assoc 3 (a ^ 2) b, mul_assoc 3 a (b ^ 2)]
935e3ba43ffff698684788b7076d5751269919ea
47c6e122a73eb61594f0426d74d9c1897ac5668e
/src/sylowtwo.lean
4e0d55ca9d83d525245c9cd44d9e8ae313854b6a
[]
no_license
ineswright/Lean-Sylow
19ec5d964be8a66e25e227621fbac79c88a83ddf
74b99544ab1ca96dc28fbe152125f565763a893b
refs/heads/main
1,688,108,643,799
1,627,664,383,000
1,627,664,383,000
382,315,172
1
0
null
null
null
null
UTF-8
Lean
false
false
6,550
lean
import group_theory.group_action import group_theory.quotient_group import data.zmod.basic import data.fintype.card import group_theory.sylow import tactic -- import algebra.group.conj open equiv fintype finset mul_action function nat sylow open subgroup quotient_group universe u variables {G : Type u} [group G] open_locale classical -- define a sylow subgroup given a prime p, and subgroup L def is_sylow_subgroup [fintype G] (L : subgroup G) {p m n : ℕ} (hp : p.prime) (hG : card G = p ^ n * m) (hndiv: ¬ p ∣ m) := card L = p ^ n lemma is_sylow_subgroup_def [fintype G] (L : subgroup G) {p m n : ℕ} (hp : p.prime) (hG : card G = p ^ n * m) (hndiv: ¬ p ∣ m) : is_sylow_subgroup L hp hG hndiv ↔ (card L = p ^ n) := iff.rfl -- TODO: think about using conjugation function -- def conjugate (x y : G) := x⁻¹ * y * x -- give the subgroup conjugate to subgroup L by g def conjugate_subgroup (L : subgroup G) (g : G) : subgroup G := { carrier := { c | ∃ h ∈ L, c = g⁻¹ * h * g }, one_mem' := begin exact ⟨1, one_mem L, by simp⟩, end, mul_mem' := begin rintros - - ⟨c, hc, rfl⟩ ⟨d, hd, rfl⟩, exact ⟨c * d, L.mul_mem hc hd, by group⟩, end, inv_mem' := begin simp only [and_imp, exists_prop, set.mem_set_of_eq, exists_imp_distrib], intros x y hy hx, refine ⟨(g * x * g⁻¹)⁻¹, _, by group⟩, rw [hx, mul_assoc, mul_assoc, mul_assoc, mul_inv_self, mul_one, ← mul_assoc, mul_inv_self, one_mul], exact inv_mem L hy, end } lemma conjugate_subgroup_def (L : subgroup G) (x g : G) : x ∈ conjugate_subgroup L g ↔ x ∈ { c | ∃ h ∈ L, c = g⁻¹ * h * g } := iff.rfl noncomputable def index_of_subgroup [fintype G] (L : subgroup G) : ℕ := card G / card L lemma index_of_subgroup_def [fintype G] (L : subgroup G) : index_of_subgroup L = card G / card L := rfl -- this must already exist in mathlib - FIND IT! lemma card_subgroup_pos [fintype G] (L : subgroup G) : 0 < card L := card_pos_iff.2 $ nonempty.intro ⟨1, L.one_mem⟩ -- lagranges theorem lemma index_of_subgroup_def' [fintype G] (L : subgroup G) : index_of_subgroup L = card (quotient L) := begin rw [index_of_subgroup_def, card_eq_card_quotient_mul_card_subgroup L], rw [nat.mul_div_assoc _ (dvd_refl (card ↥L)), nat.div_self (card_subgroup_pos L)], simp, end -- this is Eric's proof, pull request 8382 quotient_group.eq_iff_coset_eq lemma quotient_iff_set {y z : G} {L : subgroup G} : @quotient_group.mk _ _ L y = quotient_group.mk z ↔ left_coset y L = left_coset z L := begin rw [←quotient_group.eq_class_eq_left_coset, ←quotient_group.eq_class_eq_left_coset], rw set.ext_iff, dsimp, split, { intros h z, exact eq.congr_right h, }, intro h, exact (h y).mp rfl end -- think about replacing λ with a conjugation function -- this may already exist def subgroup_bijects_conjugate (L : subgroup G) (x : G) : conjugate_subgroup L x ≃ L := { to_fun := (λ y : conjugate_subgroup L x, ⟨x * y * x⁻¹, begin rcases y with ⟨_, z, hz, rfl⟩, rw [subtype.coe_mk, mul_assoc, mul_assoc, mul_inv_self, mul_one, ← mul_assoc, mul_inv_self, one_mul], exact hz, end⟩) , inv_fun := (λ y : L, ⟨x⁻¹ * y * x, ⟨y, set_like.coe_mem y, rfl⟩⟩), left_inv := begin rintro ⟨y, hy⟩, simp only [subtype.mk_eq_mk, subtype.coe_mk], group, end, right_inv := begin rintro ⟨y, hy⟩, simp only [subtype.mk_eq_mk, subtype.coe_mk], group, end } -- this index of a sylow subgroup is m lemma sylow_subgroup_index [fintype G] {L : subgroup G} {p m n : ℕ} (hp : p.prime) (hG : card G = p ^ n * m) (hndiv: ¬ p ∣ m) (h : is_sylow_subgroup L hp hG hndiv) : index_of_subgroup L = m := begin rw is_sylow_subgroup_def at h, rw [index_of_subgroup_def, hG, h, nat.mul_div_cancel_left _ (pow_pos (pos_of_gt hp.left) n)], end -- the index of a sylow subgroup is not divisible by prime p lemma subgroup_index_not_conj_zero_wrt_p [fintype G] {L : subgroup G} {p m n : ℕ} (hp : p.prime) (hG : card G = p ^ n * m) (hndiv: ¬ p ∣ m) (h : is_sylow_subgroup L hp hG hndiv) : ¬ index_of_subgroup L ≡ 0 [MOD p] := begin rw sylow_subgroup_index hp hG hndiv h, intro hn, apply hndiv, exact modeq.modeq_zero_iff.mp hn, end theorem sylow_two [fintype G] {p n m : ℕ} (L K : subgroup G) (hp : p.prime) (hG : card G = p ^ n * m) (hndiv: ¬ p ∣ m) ( h₁ : is_sylow_subgroup L hp hG hndiv) (h₂ : is_sylow_subgroup K hp hG hndiv) : ∃ g : G, conjugate_subgroup K g = L := begin haveI : fact (p.prime) := ⟨ hp ⟩, have h₄ : index_of_subgroup L ≡ card (fixed_points K (quotient L)) [MOD p], { rw is_sylow_subgroup_def at h₂, rw index_of_subgroup_def', exact card_modeq_card_fixed_points p h₂, }, have h₅ : 0 < card (fixed_points K (quotient L)), { apply lt_of_le_of_ne _ _, { exact le_of_not_gt (card ↥(fixed_points ↥K (quotient L))).not_lt_zero, }, { intro hn, apply subgroup_index_not_conj_zero_wrt_p hp hG hndiv h₁, rw hn, exact h₄, }, }, have h₆ : ∃ x : G, (conjugate_subgroup K x : set G) ⊆ L, { rw card_pos_iff at h₅, rcases h₅ with ⟨fp, hfp⟩, rw mul_action.mem_fixed_points at hfp, let x := quotient.out' fp, use x, rintro _ ⟨y, hy, rfl⟩, have h1 : y • fp = quotient_group.mk (y * x), { rw ← quotient.out_eq' fp, exact quotient.smul_mk L y x, }, have h2 : @quotient_group.mk _ _ L (x⁻¹ * y * x) = quotient_group.mk 1, { rw [mul_assoc, ← quotient.smul_mk L x⁻¹ (y * x)], rw [← smul_left_cancel_iff x, smul_smul x x⁻¹ _, mul_inv_self, quotient.smul_mk, one_mul], rw [quotient.smul_mk, mul_one, ← h1], convert hfp ⟨y, hy⟩, exact quotient.out_eq' fp, }, rw [quotient_iff_set, one_left_coset] at h2, rw ← h2, unfold left_coset, simp only [mul_inv_rev, set.mem_preimage, set.image_mul_left, set_like.mem_coe, inv_inv], rw [mul_assoc, mul_assoc, mul_assoc, ← mul_assoc x x⁻¹ (y * x)], rw [mul_inv_self, one_mul, ← mul_assoc, ← mul_assoc], simp [mul_left_inv, inv_mul_cancel_right, one_mem L], }, have h₇ : ∀ x : G, card (conjugate_subgroup K x) = card L, { rw is_sylow_subgroup_def at h₁ h₂, intro x, rw [h₁, h₂.symm], apply fintype.card_congr, exact subgroup_bijects_conjugate K x, }, apply exists.elim h₆, intros x hx, use x, rw set_like.ext'_iff, exact set.eq_of_subset_of_card_le hx (h₇ x).ge, end
7806e59ebd54393f8a34025f4d8d187ea79812a4
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/real/cau_seq.lean
d5ddf07cc5541358e4d83ad4daa6238b11d13fad
[ "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
25,905
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import algebra.big_operators.order /-! # Cauchy sequences A basic theory of Cauchy sequences, used in the construction of the reals and p-adic numbers. Where applicable, lemmas that will be reused in other contexts have been stated in extra generality. There are other "versions" of Cauchyness in the library, in particular Cauchy filters in topology. This is a concrete implementation that is useful for simplicity and computability reasons. ## Important definitions * `is_absolute_value`: a type class stating that `f : β → α` satisfies the axioms of an abs val * `is_cau_seq`: a predicate that says `f : ℕ → β` is Cauchy. * `cau_seq`: the type of Cauchy sequences valued in type `β` with respect to an absolute value function `abv`. ## Tags sequence, cauchy, abs val, absolute value -/ open_locale big_operators /-- A function `f` is an absolute value if it is nonnegative, zero only at 0, additive, and multiplicative. -/ class is_absolute_value {α} [linear_ordered_field α] {β} [ring β] (f : β → α) : Prop := (abv_nonneg [] : ∀ x, 0 ≤ f x) (abv_eq_zero [] : ∀ {x}, f x = 0 ↔ x = 0) (abv_add [] : ∀ x y, f (x + y) ≤ f x + f y) (abv_mul [] : ∀ x y, f (x * y) = f x * f y) namespace is_absolute_value variables {α : Type*} [linear_ordered_field α] {β : Type*} [ring β] (abv : β → α) [is_absolute_value abv] theorem abv_zero : abv 0 = 0 := (abv_eq_zero abv).2 rfl theorem abv_one' (h : (1:β) ≠ 0) : abv 1 = 1 := (mul_right_inj' $ mt (abv_eq_zero abv).1 h).1 $ by rw [← abv_mul abv, mul_one, mul_one] theorem abv_one {β : Type*} [domain β] (abv : β → α) [is_absolute_value abv] : abv 1 = 1 := abv_one' abv one_ne_zero theorem abv_pos {a : β} : 0 < abv a ↔ a ≠ 0 := by rw [lt_iff_le_and_ne, ne, eq_comm]; simp [abv_eq_zero abv, abv_nonneg abv] theorem abv_neg (a : β) : abv (-a) = abv a := by rw [← mul_self_inj_of_nonneg (abv_nonneg abv _) (abv_nonneg abv _), ← abv_mul abv, ← abv_mul abv]; simp theorem abv_sub (a b : β) : abv (a - b) = abv (b - a) := by rw [← neg_sub, abv_neg abv] theorem abv_inv {β : Type*} [field β] (abv : β → α) [is_absolute_value abv] (a : β) : abv a⁻¹ = (abv a)⁻¹ := classical.by_cases (λ h : a = 0, by simp [h, abv_zero abv]) (λ h, mul_right_cancel' (mt (abv_eq_zero abv).1 h) $ by rw [← abv_mul abv]; simp [h, mt (abv_eq_zero abv).1 h, abv_one abv]) theorem abv_div {β : Type*} [field β] (abv : β → α) [is_absolute_value abv] (a b : β) : abv (a / b) = abv a / abv b := by rw [division_def, abv_mul abv, abv_inv abv]; refl lemma abv_sub_le (a b c : β) : abv (a - c) ≤ abv (a - b) + abv (b - c) := by simpa [sub_eq_add_neg, add_assoc] using abv_add abv (a - b) (b - c) lemma sub_abv_le_abv_sub (a b : β) : abv a - abv b ≤ abv (a - b) := sub_le_iff_le_add.2 $ by simpa using abv_add abv (a - b) b lemma abs_abv_sub_le_abv_sub (a b : β) : abs (abv a - abv b) ≤ abv (a - b) := abs_sub_le_iff.2 ⟨sub_abv_le_abv_sub abv _ _, by rw abv_sub abv; apply sub_abv_le_abv_sub abv⟩ lemma abv_pow {β : Type*} [domain β] (abv : β → α) [is_absolute_value abv] (a : β) (n : ℕ) : abv (a ^ n) = abv a ^ n := by induction n; simp [abv_mul abv, pow_succ, abv_one abv, *] end is_absolute_value instance abs_is_absolute_value {α} [linear_ordered_field α] : is_absolute_value (abs : α → α) := { abv_nonneg := abs_nonneg, abv_eq_zero := λ _, abs_eq_zero, abv_add := abs_add, abv_mul := abs_mul } open is_absolute_value theorem exists_forall_ge_and {α} [linear_order α] {P Q : α → Prop} : (∃ i, ∀ j ≥ i, P j) → (∃ i, ∀ j ≥ i, Q j) → ∃ i, ∀ j ≥ i, P j ∧ Q j | ⟨a, h₁⟩ ⟨b, h₂⟩ := let ⟨c, ac, bc⟩ := exists_ge_of_linear a b in ⟨c, λ j hj, ⟨h₁ _ (le_trans ac hj), h₂ _ (le_trans bc hj)⟩⟩ section variables {α : Type*} [linear_ordered_field α] {β : Type*} [ring β] (abv : β → α) [is_absolute_value abv] theorem rat_add_continuous_lemma {ε : α} (ε0 : 0 < ε) : ∃ δ > 0, ∀ {a₁ a₂ b₁ b₂ : β}, abv (a₁ - b₁) < δ → abv (a₂ - b₂) < δ → abv (a₁ + a₂ - (b₁ + b₂)) < ε := ⟨ε / 2, half_pos ε0, λ a₁ a₂ b₁ b₂ h₁ h₂, by simpa [add_halves, sub_eq_add_neg, add_comm, add_left_comm, add_assoc] using lt_of_le_of_lt (abv_add abv _ _) (add_lt_add h₁ h₂)⟩ theorem rat_mul_continuous_lemma {ε K₁ K₂ : α} (ε0 : 0 < ε) : ∃ δ > 0, ∀ {a₁ a₂ b₁ b₂ : β}, abv a₁ < K₁ → abv b₂ < K₂ → abv (a₁ - b₁) < δ → abv (a₂ - b₂) < δ → abv (a₁ * a₂ - b₁ * b₂) < ε := begin have K0 : (0 : α) < max 1 (max K₁ K₂) := lt_of_lt_of_le zero_lt_one (le_max_left _ _), have εK := div_pos (half_pos ε0) K0, refine ⟨_, εK, λ a₁ a₂ b₁ b₂ ha₁ hb₂ h₁ h₂, _⟩, replace ha₁ := lt_of_lt_of_le ha₁ (le_trans (le_max_left _ K₂) (le_max_right 1 _)), replace hb₂ := lt_of_lt_of_le hb₂ (le_trans (le_max_right K₁ _) (le_max_right 1 _)), have := add_lt_add (mul_lt_mul' (le_of_lt h₁) hb₂ (abv_nonneg abv _) εK) (mul_lt_mul' (le_of_lt h₂) ha₁ (abv_nonneg abv _) εK), rw [← abv_mul abv, mul_comm, div_mul_cancel _ (ne_of_gt K0), ← abv_mul abv, add_halves] at this, simpa [mul_add, add_mul, sub_eq_add_neg, add_comm, add_left_comm] using lt_of_le_of_lt (abv_add abv _ _) this end theorem rat_inv_continuous_lemma {β : Type*} [field β] (abv : β → α) [is_absolute_value abv] {ε K : α} (ε0 : 0 < ε) (K0 : 0 < K) : ∃ δ > 0, ∀ {a b : β}, K ≤ abv a → K ≤ abv b → abv (a - b) < δ → abv (a⁻¹ - b⁻¹) < ε := begin have KK := mul_pos K0 K0, have εK := mul_pos ε0 KK, refine ⟨_, εK, λ a b ha hb h, _⟩, have a0 := lt_of_lt_of_le K0 ha, have b0 := lt_of_lt_of_le K0 hb, rw [inv_sub_inv ((abv_pos abv).1 a0) ((abv_pos abv).1 b0), abv_div abv, abv_mul abv, mul_comm, abv_sub abv, ← mul_div_cancel ε (ne_of_gt KK)], exact div_lt_div h (mul_le_mul hb ha (le_of_lt K0) (abv_nonneg abv _)) (le_of_lt $ mul_pos ε0 KK) KK end end /-- A sequence is Cauchy if the distance between its entries tends to zero. -/ def is_cau_seq {α : Type*} [linear_ordered_field α] {β : Type*} [ring β] (abv : β → α) (f : ℕ → β) : Prop := ∀ ε > 0, ∃ i, ∀ j ≥ i, abv (f j - f i) < ε namespace is_cau_seq variables {α : Type*} [linear_ordered_field α] {β : Type*} [ring β] {abv : β → α} [is_absolute_value abv] {f : ℕ → β} @[nolint ge_or_gt] -- see Note [nolint_ge] theorem cauchy₂ (hf : is_cau_seq abv f) {ε : α} (ε0 : 0 < ε) : ∃ i, ∀ j k ≥ i, abv (f j - f k) < ε := begin refine (hf _ (half_pos ε0)).imp (λ i hi j k ij ik, _), rw ← add_halves ε, refine lt_of_le_of_lt (abv_sub_le abv _ _ _) (add_lt_add (hi _ ij) _), rw abv_sub abv, exact hi _ ik end theorem cauchy₃ (hf : is_cau_seq abv f) {ε : α} (ε0 : 0 < ε) : ∃ i, ∀ j ≥ i, ∀ k ≥ j, abv (f k - f j) < ε := let ⟨i, H⟩ := hf.cauchy₂ ε0 in ⟨i, λ j ij k jk, H _ _ (le_trans ij jk) ij⟩ end is_cau_seq /-- `cau_seq β abv` is the type of `β`-valued Cauchy sequences, with respect to the absolute value function `abv`. -/ def cau_seq {α : Type*} [linear_ordered_field α] (β : Type*) [ring β] (abv : β → α) : Type* := {f : ℕ → β // is_cau_seq abv f} namespace cau_seq variables {α : Type*} [linear_ordered_field α] section ring variables {β : Type*} [ring β] {abv : β → α} instance : has_coe_to_fun (cau_seq β abv) := ⟨_, subtype.val⟩ @[simp] theorem mk_to_fun (f) (hf : is_cau_seq abv f) : @coe_fn (cau_seq β abv) _ ⟨f, hf⟩ = f := rfl theorem ext {f g : cau_seq β abv} (h : ∀ i, f i = g i) : f = g := subtype.eq (funext h) theorem is_cau (f : cau_seq β abv) : is_cau_seq abv f := f.2 theorem cauchy (f : cau_seq β abv) : ∀ {ε}, 0 < ε → ∃ i, ∀ j ≥ i, abv (f j - f i) < ε := f.2 /-- Given a Cauchy sequence `f`, create a Cauchy sequence from a sequence `g` with the same values as `f`. -/ def of_eq (f : cau_seq β abv) (g : ℕ → β) (e : ∀ i, f i = g i) : cau_seq β abv := ⟨g, λ ε, by rw [show g = f, from (funext e).symm]; exact f.cauchy⟩ variable [is_absolute_value abv] @[nolint ge_or_gt] -- see Note [nolint_ge] theorem cauchy₂ (f : cau_seq β abv) {ε} : 0 < ε → ∃ i, ∀ j k ≥ i, abv (f j - f k) < ε := f.2.cauchy₂ theorem cauchy₃ (f : cau_seq β abv) {ε} : 0 < ε → ∃ i, ∀ j ≥ i, ∀ k ≥ j, abv (f k - f j) < ε := f.2.cauchy₃ theorem bounded (f : cau_seq β abv) : ∃ r, ∀ i, abv (f i) < r := begin cases f.cauchy zero_lt_one with i h, let R := ∑ j in finset.range (i+1), abv (f j), have : ∀ j ≤ i, abv (f j) ≤ R, { intros j ij, change (λ j, abv (f j)) j ≤ R, apply finset.single_le_sum, { intros, apply abv_nonneg abv }, { rwa [finset.mem_range, nat.lt_succ_iff] } }, refine ⟨R + 1, λ j, _⟩, cases lt_or_le j i with ij ij, { exact lt_of_le_of_lt (this _ (le_of_lt ij)) (lt_add_one _) }, { have := lt_of_le_of_lt (abv_add abv _ _) (add_lt_add_of_le_of_lt (this _ (le_refl _)) (h _ ij)), rw [add_sub, add_comm] at this, simpa } end theorem bounded' (f : cau_seq β abv) (x : α) : ∃ r > x, ∀ i, abv (f i) < r := let ⟨r, h⟩ := f.bounded in ⟨max r (x+1), lt_of_lt_of_le (lt_add_one _) (le_max_right _ _), λ i, lt_of_lt_of_le (h i) (le_max_left _ _)⟩ instance : has_add (cau_seq β abv) := ⟨λ f g, ⟨λ i, (f i + g i : β), λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_add_continuous_lemma abv ε0, ⟨i, H⟩ := exists_forall_ge_and (f.cauchy₃ δ0) (g.cauchy₃ δ0) in ⟨i, λ j ij, let ⟨H₁, H₂⟩ := H _ (le_refl _) in Hδ (H₁ _ ij) (H₂ _ ij)⟩⟩⟩ @[simp] theorem add_apply (f g : cau_seq β abv) (i : ℕ) : (f + g) i = f i + g i := rfl variable (abv) /-- The constant Cauchy sequence. -/ def const (x : β) : cau_seq β abv := ⟨λ i, x, λ ε ε0, ⟨0, λ j ij, by simpa [abv_zero abv] using ε0⟩⟩ variable {abv} local notation `const` := const abv @[simp] theorem const_apply (x : β) (i : ℕ) : (const x : ℕ → β) i = x := rfl theorem const_inj {x y : β} : (const x : cau_seq β abv) = const y ↔ x = y := ⟨λ h, congr_arg (λ f:cau_seq β abv, (f:ℕ→β) 0) h, congr_arg _⟩ instance : has_zero (cau_seq β abv) := ⟨const 0⟩ instance : has_one (cau_seq β abv) := ⟨const 1⟩ instance : inhabited (cau_seq β abv) := ⟨0⟩ @[simp] theorem zero_apply (i) : (0 : cau_seq β abv) i = 0 := rfl @[simp] theorem one_apply (i) : (1 : cau_seq β abv) i = 1 := rfl theorem const_add (x y : β) : const (x + y) = const x + const y := ext $ λ i, rfl instance : has_mul (cau_seq β abv) := ⟨λ f g, ⟨λ i, (f i * g i : β), λ ε ε0, let ⟨F, F0, hF⟩ := f.bounded' 0, ⟨G, G0, hG⟩ := g.bounded' 0, ⟨δ, δ0, Hδ⟩ := rat_mul_continuous_lemma abv ε0, ⟨i, H⟩ := exists_forall_ge_and (f.cauchy₃ δ0) (g.cauchy₃ δ0) in ⟨i, λ j ij, let ⟨H₁, H₂⟩ := H _ (le_refl _) in Hδ (hF j) (hG i) (H₁ _ ij) (H₂ _ ij)⟩⟩⟩ @[simp] theorem mul_apply (f g : cau_seq β abv) (i : ℕ) : (f * g) i = f i * g i := rfl theorem const_mul (x y : β) : const (x * y) = const x * const y := ext $ λ i, rfl instance : has_neg (cau_seq β abv) := ⟨λ f, of_eq (const (-1) * f) (λ x, -f x) (λ i, by simp)⟩ @[simp] theorem neg_apply (f : cau_seq β abv) (i) : (-f) i = -f i := rfl theorem const_neg (x : β) : const (-x) = -const x := ext $ λ i, rfl instance : ring (cau_seq β abv) := by refine {neg := has_neg.neg, add := (+), zero := 0, mul := (*), one := 1, ..}; { intros, apply ext, simp [mul_add, mul_assoc, add_mul, add_comm, add_left_comm] } instance {β : Type*} [comm_ring β] {abv : β → α} [is_absolute_value abv] : comm_ring (cau_seq β abv) := { mul_comm := by intros; apply ext; simp [mul_left_comm, mul_comm], ..cau_seq.ring } theorem const_sub (x y : β) : const (x - y) = const x - const y := by rw [sub_eq_add_neg, const_add, const_neg, sub_eq_add_neg] @[simp] theorem sub_apply (f g : cau_seq β abv) (i : ℕ) : (f - g) i = f i - g i := rfl /-- `lim_zero f` holds when `f` approaches 0. -/ def lim_zero {abv : β → α} (f : cau_seq β abv) : Prop := ∀ ε > 0, ∃ i, ∀ j ≥ i, abv (f j) < ε theorem add_lim_zero {f g : cau_seq β abv} (hf : lim_zero f) (hg : lim_zero g) : lim_zero (f + g) | ε ε0 := (exists_forall_ge_and (hf _ $ half_pos ε0) (hg _ $ half_pos ε0)).imp $ λ i H j ij, let ⟨H₁, H₂⟩ := H _ ij in by simpa [add_halves ε] using lt_of_le_of_lt (abv_add abv _ _) (add_lt_add H₁ H₂) theorem mul_lim_zero_right (f : cau_seq β abv) {g} (hg : lim_zero g) : lim_zero (f * g) | ε ε0 := let ⟨F, F0, hF⟩ := f.bounded' 0 in (hg _ $ div_pos ε0 F0).imp $ λ i H j ij, by have := mul_lt_mul' (le_of_lt $ hF j) (H _ ij) (abv_nonneg abv _) F0; rwa [mul_comm F, div_mul_cancel _ (ne_of_gt F0), ← abv_mul abv] at this theorem mul_lim_zero_left {f} (g : cau_seq β abv) (hg : lim_zero f) : lim_zero (f * g) | ε ε0 := let ⟨G, G0, hG⟩ := g.bounded' 0 in (hg _ $ div_pos ε0 G0).imp $ λ i H j ij, by have := mul_lt_mul'' (H _ ij) (hG j) (abv_nonneg abv _) (abv_nonneg abv _); rwa [div_mul_cancel _ (ne_of_gt G0), ← abv_mul abv] at this theorem neg_lim_zero {f : cau_seq β abv} (hf : lim_zero f) : lim_zero (-f) := by rw ← neg_one_mul; exact mul_lim_zero_right _ hf theorem sub_lim_zero {f g : cau_seq β abv} (hf : lim_zero f) (hg : lim_zero g) : lim_zero (f - g) := add_lim_zero hf (neg_lim_zero hg) theorem lim_zero_sub_rev {f g : cau_seq β abv} (hfg : lim_zero (f - g)) : lim_zero (g - f) := by simpa using neg_lim_zero hfg theorem zero_lim_zero : lim_zero (0 : cau_seq β abv) | ε ε0 := ⟨0, λ j ij, by simpa [abv_zero abv] using ε0⟩ theorem const_lim_zero {x : β} : lim_zero (const x) ↔ x = 0 := ⟨λ H, (abv_eq_zero abv).1 $ eq_of_le_of_forall_le_of_dense (abv_nonneg abv _) $ λ ε ε0, let ⟨i, hi⟩ := H _ ε0 in le_of_lt $ hi _ (le_refl _), λ e, e.symm ▸ zero_lim_zero⟩ instance equiv : setoid (cau_seq β abv) := ⟨λ f g, lim_zero (f - g), ⟨λ f, by simp [zero_lim_zero], λ f g h, by simpa using neg_lim_zero h, λ f g h fg gh, by simpa [sub_eq_add_neg, add_assoc] using add_lim_zero fg gh⟩⟩ lemma add_equiv_add {f1 f2 g1 g2 : cau_seq β abv} (hf : f1 ≈ f2) (hg : g1 ≈ g2) : f1 + g1 ≈ f2 + g2 := begin change lim_zero ((f1 + g1) - _), convert add_lim_zero hf hg using 1, simp only [sub_eq_add_neg, add_assoc], rw add_comm (-f2), simp only [add_assoc], congr' 2, simp end lemma neg_equiv_neg {f g : cau_seq β abv} (hf : f ≈ g) : -f ≈ -g := begin have hf : lim_zero _ := neg_lim_zero hf, show lim_zero (-f - -g), convert hf using 1, simp end theorem equiv_def₃ {f g : cau_seq β abv} (h : f ≈ g) {ε : α} (ε0 : 0 < ε) : ∃ i, ∀ j ≥ i, ∀ k ≥ j, abv (f k - g j) < ε := (exists_forall_ge_and (h _ $ half_pos ε0) (f.cauchy₃ $ half_pos ε0)).imp $ λ i H j ij k jk, let ⟨h₁, h₂⟩ := H _ ij in by have := lt_of_le_of_lt (abv_add abv (f j - g j) _) (add_lt_add h₁ (h₂ _ jk)); rwa [sub_add_sub_cancel', add_halves] at this theorem lim_zero_congr {f g : cau_seq β abv} (h : f ≈ g) : lim_zero f ↔ lim_zero g := ⟨λ l, by simpa using add_lim_zero (setoid.symm h) l, λ l, by simpa using add_lim_zero h l⟩ theorem abv_pos_of_not_lim_zero {f : cau_seq β abv} (hf : ¬ lim_zero f) : ∃ K > 0, ∃ i, ∀ j ≥ i, K ≤ abv (f j) := begin haveI := classical.prop_decidable, by_contra nk, refine hf (λ ε ε0, _), simp [not_forall] at nk, cases f.cauchy₃ (half_pos ε0) with i hi, rcases nk _ (half_pos ε0) i with ⟨j, ij, hj⟩, refine ⟨j, λ k jk, _⟩, have := lt_of_le_of_lt (abv_add abv _ _) (add_lt_add (hi j ij k jk) hj), rwa [sub_add_cancel, add_halves] at this end theorem of_near (f : ℕ → β) (g : cau_seq β abv) (h : ∀ ε > 0, ∃ i, ∀ j ≥ i, abv (f j - g j) < ε) : is_cau_seq abv f | ε ε0 := let ⟨i, hi⟩ := exists_forall_ge_and (h _ (half_pos $ half_pos ε0)) (g.cauchy₃ $ half_pos ε0) in ⟨i, λ j ij, begin cases hi _ (le_refl _) with h₁ h₂, rw abv_sub abv at h₁, have := lt_of_le_of_lt (abv_add abv _ _) (add_lt_add (hi _ ij).1 h₁), have := lt_of_le_of_lt (abv_add abv _ _) (add_lt_add this (h₂ _ ij)), rwa [add_halves, add_halves, add_right_comm, sub_add_sub_cancel, sub_add_sub_cancel] at this end⟩ lemma not_lim_zero_of_not_congr_zero {f : cau_seq _ abv} (hf : ¬ f ≈ 0) : ¬ lim_zero f := assume : lim_zero f, have lim_zero (f - 0), by simpa, hf this lemma mul_equiv_zero (g : cau_seq _ abv) {f : cau_seq _ abv} (hf : f ≈ 0) : g * f ≈ 0 := have lim_zero (f - 0), from hf, have lim_zero (g*f), from mul_lim_zero_right _ $ by simpa, show lim_zero (g*f - 0), by simpa lemma mul_not_equiv_zero {f g : cau_seq _ abv} (hf : ¬ f ≈ 0) (hg : ¬ g ≈ 0) : ¬ (f * g) ≈ 0 := assume : lim_zero (f*g - 0), have hlz : lim_zero (f*g), by simpa, have hf' : ¬ lim_zero f, by simpa using (show ¬ lim_zero (f - 0), from hf), have hg' : ¬ lim_zero g, by simpa using (show ¬ lim_zero (g - 0), from hg), begin rcases abv_pos_of_not_lim_zero hf' with ⟨a1, ha1, N1, hN1⟩, rcases abv_pos_of_not_lim_zero hg' with ⟨a2, ha2, N2, hN2⟩, have : 0 < a1 * a2, from mul_pos ha1 ha2, cases hlz _ this with N hN, let i := max N (max N1 N2), have hN' := hN i (le_max_left _ _), have hN1' := hN1 i (le_trans (le_max_left _ _) (le_max_right _ _)), have hN1' := hN2 i (le_trans (le_max_right _ _) (le_max_right _ _)), apply not_le_of_lt hN', change _ ≤ abv (_ * _), rw is_absolute_value.abv_mul abv, apply mul_le_mul; try { assumption }, { apply le_of_lt ha2 }, { apply is_absolute_value.abv_nonneg abv } end theorem const_equiv {x y : β} : const x ≈ const y ↔ x = y := show lim_zero _ ↔ _, by rw [← const_sub, const_lim_zero, sub_eq_zero] end ring section comm_ring variables {β : Type*} [comm_ring β] {abv : β → α} [is_absolute_value abv] lemma mul_equiv_zero' (g : cau_seq _ abv) {f : cau_seq _ abv} (hf : f ≈ 0) : f * g ≈ 0 := by rw mul_comm; apply mul_equiv_zero _ hf end comm_ring section integral_domain variables {β : Type*} [integral_domain β] (abv : β → α) [is_absolute_value abv] lemma one_not_equiv_zero : ¬ (const abv 1) ≈ (const abv 0) := assume h, have ∀ ε > 0, ∃ i, ∀ k, i ≤ k → abv (1 - 0) < ε, from h, have h1 : abv 1 ≤ 0, from le_of_not_gt $ assume h2 : 0 < abv 1, exists.elim (this _ h2) $ λ i hi, lt_irrefl (abv 1) $ by simpa using hi _ (le_refl _), have h2 : 0 ≤ abv 1, from is_absolute_value.abv_nonneg _ _, have abv 1 = 0, from le_antisymm h1 h2, have (1 : β) = 0, from (is_absolute_value.abv_eq_zero abv).1 this, absurd this one_ne_zero end integral_domain section field variables {β : Type*} [field β] {abv : β → α} [is_absolute_value abv] theorem inv_aux {f : cau_seq β abv} (hf : ¬ lim_zero f) : ∀ ε > 0, ∃ i, ∀ j ≥ i, abv ((f j)⁻¹ - (f i)⁻¹) < ε | ε ε0 := let ⟨K, K0, HK⟩ := abv_pos_of_not_lim_zero hf, ⟨δ, δ0, Hδ⟩ := rat_inv_continuous_lemma abv ε0 K0, ⟨i, H⟩ := exists_forall_ge_and HK (f.cauchy₃ δ0) in ⟨i, λ j ij, let ⟨iK, H'⟩ := H _ (le_refl _) in Hδ (H _ ij).1 iK (H' _ ij)⟩ /-- Given a Cauchy sequence `f` with nonzero limit, create a Cauchy sequence with values equal to the inverses of the values of `f`. -/ def inv (f : cau_seq β abv) (hf : ¬ lim_zero f) : cau_seq β abv := ⟨_, inv_aux hf⟩ @[simp] theorem inv_apply {f : cau_seq β abv} (hf i) : inv f hf i = (f i)⁻¹ := rfl theorem inv_mul_cancel {f : cau_seq β abv} (hf) : inv f hf * f ≈ 1 := λ ε ε0, let ⟨K, K0, i, H⟩ := abv_pos_of_not_lim_zero hf in ⟨i, λ j ij, by simpa [(abv_pos abv).1 (lt_of_lt_of_le K0 (H _ ij)), abv_zero abv] using ε0⟩ theorem const_inv {x : β} (hx : x ≠ 0) : const abv (x⁻¹) = inv (const abv x) (by rwa const_lim_zero) := ext (assume n, by simp[inv_apply, const_apply]) end field section abs local notation `const` := const abs /-- The entries of a positive Cauchy sequence eventually have a positive lower bound. -/ def pos (f : cau_seq α abs) : Prop := ∃ K > 0, ∃ i, ∀ j ≥ i, K ≤ f j theorem not_lim_zero_of_pos {f : cau_seq α abs} : pos f → ¬ lim_zero f | ⟨F, F0, hF⟩ H := let ⟨i, h⟩ := exists_forall_ge_and hF (H _ F0), ⟨h₁, h₂⟩ := h _ (le_refl _) in not_lt_of_le h₁ (abs_lt.1 h₂).2 theorem const_pos {x : α} : pos (const x) ↔ 0 < x := ⟨λ ⟨K, K0, i, h⟩, lt_of_lt_of_le K0 (h _ (le_refl _)), λ h, ⟨x, h, 0, λ j _, le_refl _⟩⟩ theorem add_pos {f g : cau_seq α abs} : pos f → pos g → pos (f + g) | ⟨F, F0, hF⟩ ⟨G, G0, hG⟩ := let ⟨i, h⟩ := exists_forall_ge_and hF hG in ⟨_, _root_.add_pos F0 G0, i, λ j ij, let ⟨h₁, h₂⟩ := h _ ij in add_le_add h₁ h₂⟩ theorem pos_add_lim_zero {f g : cau_seq α abs} : pos f → lim_zero g → pos (f + g) | ⟨F, F0, hF⟩ H := let ⟨i, h⟩ := exists_forall_ge_and hF (H _ (half_pos F0)) in ⟨_, half_pos F0, i, λ j ij, begin cases h j ij with h₁ h₂, have := add_le_add h₁ (le_of_lt (abs_lt.1 h₂).1), rwa [← sub_eq_add_neg, sub_self_div_two] at this end⟩ protected theorem mul_pos {f g : cau_seq α abs} : pos f → pos g → pos (f * g) | ⟨F, F0, hF⟩ ⟨G, G0, hG⟩ := let ⟨i, h⟩ := exists_forall_ge_and hF hG in ⟨_, _root_.mul_pos F0 G0, i, λ j ij, let ⟨h₁, h₂⟩ := h _ ij in mul_le_mul h₁ h₂ (le_of_lt G0) (le_trans (le_of_lt F0) h₁)⟩ theorem trichotomy (f : cau_seq α abs) : pos f ∨ lim_zero f ∨ pos (-f) := begin cases classical.em (lim_zero f); simp *, rcases abv_pos_of_not_lim_zero h with ⟨K, K0, hK⟩, rcases exists_forall_ge_and hK (f.cauchy₃ K0) with ⟨i, hi⟩, refine (le_total 0 (f i)).imp _ _; refine (λ h, ⟨K, K0, i, λ j ij, _⟩); have := (hi _ ij).1; cases hi _ (le_refl _) with h₁ h₂, { rwa abs_of_nonneg at this, rw abs_of_nonneg h at h₁, exact (le_add_iff_nonneg_right _).1 (le_trans h₁ $ neg_le_sub_iff_le_add'.1 $ le_of_lt (abs_lt.1 $ h₂ _ ij).1) }, { rwa abs_of_nonpos at this, rw abs_of_nonpos h at h₁, rw [← sub_le_sub_iff_right, zero_sub], exact le_trans (le_of_lt (abs_lt.1 $ h₂ _ ij).2) h₁ } end instance : has_lt (cau_seq α abs) := ⟨λ f g, pos (g - f)⟩ instance : has_le (cau_seq α abs) := ⟨λ f g, f < g ∨ f ≈ g⟩ theorem lt_of_lt_of_eq {f g h : cau_seq α abs} (fg : f < g) (gh : g ≈ h) : f < h := by simpa [sub_eq_add_neg, add_comm, add_left_comm] using pos_add_lim_zero fg (neg_lim_zero gh) theorem lt_of_eq_of_lt {f g h : cau_seq α abs} (fg : f ≈ g) (gh : g < h) : f < h := by have := pos_add_lim_zero gh (neg_lim_zero fg); rwa [← sub_eq_add_neg, sub_sub_sub_cancel_right] at this theorem lt_trans {f g h : cau_seq α abs} (fg : f < g) (gh : g < h) : f < h := by simpa [sub_eq_add_neg, add_comm, add_left_comm] using add_pos fg gh theorem lt_irrefl {f : cau_seq α abs} : ¬ f < f | h := not_lim_zero_of_pos h (by simp [zero_lim_zero]) lemma le_of_eq_of_le {f g h : cau_seq α abs} (hfg : f ≈ g) (hgh : g ≤ h) : f ≤ h := hgh.elim (or.inl ∘ cau_seq.lt_of_eq_of_lt hfg) (or.inr ∘ setoid.trans hfg) lemma le_of_le_of_eq {f g h : cau_seq α abs} (hfg : f ≤ g) (hgh : g ≈ h) : f ≤ h := hfg.elim (λ h, or.inl (cau_seq.lt_of_lt_of_eq h hgh)) (λ h, or.inr (setoid.trans h hgh)) instance : preorder (cau_seq α abs) := { lt := (<), le := λ f g, f < g ∨ f ≈ g, le_refl := λ f, or.inr (setoid.refl _), le_trans := λ f g h fg, match fg with | or.inl fg, or.inl gh := or.inl $ lt_trans fg gh | or.inl fg, or.inr gh := or.inl $ lt_of_lt_of_eq fg gh | or.inr fg, or.inl gh := or.inl $ lt_of_eq_of_lt fg gh | or.inr fg, or.inr gh := or.inr $ setoid.trans fg gh end, lt_iff_le_not_le := λ f g, ⟨λ h, ⟨or.inl h, not_or (mt (lt_trans h) lt_irrefl) (not_lim_zero_of_pos h)⟩, λ ⟨h₁, h₂⟩, h₁.resolve_right (mt (λ h, or.inr (setoid.symm h)) h₂)⟩ } theorem le_antisymm {f g : cau_seq α abs} (fg : f ≤ g) (gf : g ≤ f) : f ≈ g := fg.resolve_left (not_lt_of_le gf) theorem lt_total (f g : cau_seq α abs) : f < g ∨ f ≈ g ∨ g < f := (trichotomy (g - f)).imp_right (λ h, h.imp (λ h, setoid.symm h) (λ h, by rwa neg_sub at h)) theorem le_total (f g : cau_seq α abs) : f ≤ g ∨ g ≤ f := (or.assoc.2 (lt_total f g)).imp_right or.inl theorem const_lt {x y : α} : const x < const y ↔ x < y := show pos _ ↔ _, by rw [← const_sub, const_pos, sub_pos] theorem const_le {x y : α} : const x ≤ const y ↔ x ≤ y := by rw le_iff_lt_or_eq; exact or_congr const_lt const_equiv lemma le_of_exists {f g : cau_seq α abs} (h : ∃ i, ∀ j ≥ i, f j ≤ g j) : f ≤ g := let ⟨i, hi⟩ := h in (or.assoc.2 (cau_seq.lt_total f g)).elim id (λ hgf, false.elim (let ⟨K, hK0, j, hKj⟩ := hgf in not_lt_of_ge (hi (max i j) (le_max_left _ _)) (sub_pos.1 (lt_of_lt_of_le hK0 (hKj _ (le_max_right _ _)))))) theorem exists_gt (f : cau_seq α abs) : ∃ a : α, f < const a := let ⟨K, H⟩ := f.bounded in ⟨K + 1, 1, zero_lt_one, 0, λ i _, begin rw [sub_apply, const_apply, le_sub_iff_add_le', add_le_add_iff_right], exact le_of_lt (abs_lt.1 (H _)).2 end⟩ theorem exists_lt (f : cau_seq α abs) : ∃ a : α, const a < f := let ⟨a, h⟩ := (-f).exists_gt in ⟨-a, show pos _, by rwa [const_neg, sub_neg_eq_add, add_comm, ← sub_neg_eq_add]⟩ end abs end cau_seq
863df27844e4e7101bd5f7a5013290b091b22527
7d5ad87afb17e514aee234fcf0a24412eed6384f
/src/reflection.lean
93ffc355068db7e6c6d2aa687cb904d4fa1a6473
[]
no_license
digama0/flypitch
764f849eaef59c045dfbeca142a0f827973e70c1
2ec14b8da6a3964f09521d17e51f363d255b030f
refs/heads/master
1,586,980,069,651
1,547,078,141,000
1,547,078,283,000
164,965,135
1
0
null
1,547,082,858,000
1,547,082,857,000
null
UTF-8
Lean
false
false
1,934
lean
import .fol tactic.tidy .abel tactic.ring .completeness .zfc open fol local notation h :: t := dvector.cons h t local notation `[]` := dvector.nil local notation `[` l:(foldr `, ` (h t, dvector.cons h t) dvector.nil `]`) := l namespace reflection section /- rephrasing of soundness -/ def by_reflection {L} (T : Theory L) {S : Structure L} [h_nonempty : nonempty S] {hS : S ⊨ T} (p : Prop) (ψ : sentence L) (h_p : realize_sentence S ψ ↔ p) : T ⊢' ψ → p := by {intro P, cases P, apply h_p.mp, apply soundness, repeat{assumption}} open abel /- every abelian group satisfies ∀ x ∀ y, x + (y + y) = y + (y + x) -/ def T_ab_proves_x_y_y : T_ab ⊢' ∀' ∀' (&1 +' (&0 +' &0) ≃ &0 +' (&0 +' &1)) := begin apply (completeness _ _).mpr, intros M h_nonempty hM, intros x y, unfold T_ab at hM, have hM' : M ⊨ a_comm ∧ M ⊨ a_inv ∧ M ⊨ a_zero_left ∧ M ⊨ a_zero_right ∧ M ⊨ a_assoc, by {simp at hM, assumption}, rcases hM' with ⟨uno,dos,tres,cuatro,cinco⟩, have this1 := cinco x y y, have this2 := uno y x, have this3 := uno (realize_bounded_term ([x,y]) (&0 +' &1) []) y, dsimp at *, conv {to_lhs, change realize_bounded_term ([y, y, x]) (&2 +' (&1 +' &0)) [], rw[<-this1]}, conv {to_lhs, change realize_bounded_term ([y, (realize_bounded_term ([x, y]) (&0 +' &1) [])]) (&1 +' &0) [], rw[this3, <-(this2)]}, refl end example : ∀ x y : ℤ, x + (y + y) = y + (y + x) := begin refine @by_reflection L_abel T_ab ℤ' (by apply_instance) (ℤ'_is_abelian_group) _ _ _ _, exact ∀' ∀' (&1 +' (&0 +' &0) ≃ &0 +' (&0 +' &1)), {refl}, exact T_ab_proves_x_y_y end example : ∀ x y : ℤ, x + (y + y) = y + (y + x) := by {intros, rw[add_comm,add_assoc]} example : ∀ x y : ℤ, x + (y + y) = y + (y + x) := by {intros, ring} example : ∀ x y : ℤ, x + (y + y) = y + (y + x) := by {simp} example : ∀ x y : ℤ, x + (y + y) = y + (y + x) := by {tidy} end end reflection
d03e11fa9b33cd8189acb1c119624a178a757b40
ef4d3feecef33d1c1b4bd3a023b85e6a58f9e708
/theorem-proving-in-lean/ch5/Exercises.lean
eef8948d01dc1da6e5f1da7f5326ad3538c8d842
[]
no_license
MikeMKH/kata
1b7da1b8d2cc115c912f2b06b583a8e675a449e1
305b054a37517dbe4d09545d41f024937f536c20
refs/heads/master
1,585,594,368,426
1,542,835,119,000
1,542,835,119,000
16,891,298
0
0
null
1,542,835,120,000
1,392,575,890,000
Racket
UTF-8
Lean
false
false
2,860
lean
-- https://leanprover.github.io/theorem_proving_in_lean/tactics.html#exercises -- 1 section three open classical variables p q r s : Prop -- commutativity of ∧ and ∨ example : p ∧ q ↔ q ∧ p := by simp example : p ∨ q ↔ q ∨ p := by simp -- associativity of ∧ and ∨ example : (p ∧ q) ∧ r ↔ p ∧ (q ∧ r) := by simp example : (p ∨ q) ∨ r ↔ p ∨ (q ∨ r) := by simp -- distributivity example : p ∧ (q ∨ r) ↔ (p ∧ q) ∨ (p ∧ r) := begin apply iff.intro, { intro h, cases h.right with hq hr, { show (p ∧ q) ∨ (p ∧ r), exact or.inl ⟨h.left, hq⟩ }, { show (p ∧ q) ∨ (p ∧ r), exact or.inr ⟨h.left, hr⟩ }, }, { intro h, cases h with hpq hpr, { show p ∧ (q ∨ r), exact ⟨hpq.left, or.inl hpq.right⟩ }, { show p ∧ (q ∨ r), exact ⟨hpr.left, or.inr hpr.right⟩ } } end example : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (p ∨ r) := sorry -- other properties example : (p → (q → r)) ↔ (p ∧ q → r) := sorry example : ((p ∨ q) → r) ↔ (p → r) ∧ (q → r) := sorry example : ¬(p ∨ q) ↔ ¬p ∧ ¬q := sorry example : ¬p ∨ ¬q → ¬(p ∧ q) := sorry example : ¬(p ∧ ¬p) := by simp example : p ∧ ¬q → ¬(p → q) := sorry example : ¬p → (p → q) := sorry example : (¬p ∨ q) → (p → q) := sorry example : p ∨ false ↔ p := by simp example : p ∧ false ↔ false := by simp example : ¬(p ↔ ¬p) := by simp example : (p → q) → (¬q → ¬p) := sorry -- these require classical reasoning example : (p → r ∨ s) → ((p → r) ∨ (p → s)) := sorry example : ¬(p ∧ q) → ¬p ∨ ¬q := sorry example : ¬(p → q) → p ∧ ¬q := sorry example : (p → q) → (¬p ∨ q) := sorry example : (¬q → ¬p) → (p → q) := sorry example : p ∨ ¬p := sorry example : (((p → q) → p) → p) := sorry end three section four variables (α : Type) (p q : α → Prop) example : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) := sorry example : (∀ x, p x → q x) → (∀ x, p x) → (∀ x, q x) := sorry example : (∀ x, p x) ∨ (∀ x, q x) → ∀ x, p x ∨ q x := sorry variable r : Prop example : α → ((∀ x : α, r) ↔ r) := sorry example : (∀ x, p x ∨ r) ↔ (∀ x, p x) ∨ r := sorry example : (∀ x, r → p x) ↔ (r → ∀ x, p x) := sorry variables (men : Type) (barber : men) variable (shaves : men → men → Prop) example (h : ∀ x : men, shaves barber x ↔ ¬ shaves x x) : false := sorry end four -- 2 example (p q r : Prop) (hp : p) : (p ∨ q ∨ r) ∧ (q ∨ p ∨ r) ∧ (q ∨ r ∨ p) := by admit
0f2373c2327d02a0ad6c15440efba6faf8aa784c
fd39fc7e47201556d5e53b9c37552ed9f5aa76cb
/data/leanlibrary/test.lean
ecda4b7c06b9d5a86b739768392e5b5a2c5a8640
[ "MIT" ]
permissive
sundararajan-s/ProvingGround
947a741d10d54a1c6d6f8a5f8664ed5aa31e4d77
f111b791369a94a5b95d24f51e12bfa309f3a50b
refs/heads/master
1,677,893,530,903
1,599,286,921,000
1,599,286,921,000
288,640,864
1
0
MIT
1,598,694,036,000
1,597,814,549,000
Jupyter Notebook
UTF-8
Lean
false
false
160
lean
prelude inductive boolean : Type | true : boolean | false : boolean definition not : boolean -> boolean := fun b, boolean.rec_on b boolean.false boolean.true
b76fce03eea8b4e0df8bbf3f3f90f8bb2de26a93
1fd908b06e3f9c1252cb2285ada1102623a67f72
/types/arrow.lean
29c8c3d53e0fbe15acea4696f5c43321e1c8ea83
[ "Apache-2.0" ]
permissive
avigad/hott3
609a002849182721e7c7ae536d9f1e2956d6d4d3
f64750cd2de7a81e87d4828246d1369d59f16f43
refs/heads/master
1,629,027,243,322
1,510,946,717,000
1,510,946,717,000
103,570,461
0
0
null
1,505,415,620,000
1,505,415,620,000
null
UTF-8
Lean
false
false
5,883
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Ported from Coq HoTT Theorems about arrow types (function spaces) -/ import types.pi universe u hott_theory namespace hott open eq equiv is_equiv funext pi is_trunc unit namespace pi variables {A : Type _} {A' : Type _} {B : Type _} {B' : Type _} {C : A → B → Type _} {D : A → Type _} {a a' a'' : A} {b b' b'' : B} {f g : A → B} {d : D a} {d' : D a'} -- all lemmas here are special cases of the ones for pi-types /- Functorial action -/ variables (f0 : A' → A) (f1 : B → B') @[hott] def arrow_functor : (A → B) → (A' → B') := pi_functor f0 (λa, f1) /- Equivalences -/ @[hott] def is_equiv_arrow_functor [H0 : is_equiv f0] [H1 : is_equiv f1] : is_equiv (arrow_functor f0 f1) := is_equiv_pi_functor f0 (λa, f1) @[hott] def arrow_equiv_arrow_rev (f0 : A' ≃ A) (f1 : B ≃ B') : (A → B) ≃ (A' → B') := equiv.mk _ (is_equiv_arrow_functor f0 f1) @[hott] def arrow_equiv_arrow (f0 : A ≃ A') (f1 : B ≃ B') : (A → B) ≃ (A' → B') := arrow_equiv_arrow_rev (equiv.symm f0) f1 variable (A) @[hott] def arrow_equiv_arrow_right (f1 : B ≃ B') : (A → B) ≃ (A → B') := arrow_equiv_arrow_rev equiv.rfl f1 variables {A} (B) @[hott] def arrow_equiv_arrow_left_rev (f0 : A' ≃ A) : (A → B) ≃ (A' → B) := arrow_equiv_arrow_rev f0 equiv.rfl @[hott] def arrow_equiv_arrow_left (f0 : A ≃ A') : (A → B) ≃ (A' → B) := arrow_equiv_arrow f0 equiv.rfl variables {B} @[hott] def arrow_equiv_arrow_right' (f1 : A → (B ≃ B')) : (A → B) ≃ (A → B') := pi_equiv_pi_right f1 /- Equivalence if one of the types is contractible -/ variables (A B) -- we prove this separately from pi_equiv_of_is_contr_left, -- because the underlying inverse function is simpler here (no transport needed) @[hott] def arrow_equiv_of_is_contr_left [H : is_contr A] : (A → B) ≃ B := begin fapply equiv.MK, { intro f, exact f (center A)}, { intros b a, exact b}, { intro b, reflexivity}, { intro f, apply eq_of_homotopy, intro a, exact ap f (is_prop.elim _ _)} end @[hott] def arrow_equiv_of_is_contr_right [H : is_contr B] : (A → B) ≃ unit := pi_equiv_of_is_contr_right _ /- Interaction with other type constructors -/ -- most of these are in the file of the other type constructor @[hott] def arrow_empty_left : (empty → B) ≃ unit := pi_empty_left _ @[hott] def arrow_unit_left : (unit → B) ≃ B := arrow_equiv_of_is_contr_left _ _ @[hott] def arrow_unit_right : (A → unit) ≃ unit := arrow_equiv_of_is_contr_right _ _ variables {A B} /- Transport -/ @[hott] def arrow_transport {B C : A → Type _} (p : a = a') (f : B a → C a) : (transport (λa, B a → C a) p f) ~ (λb, p ▸ f (p⁻¹ ▸ b)) := eq.rec_on p (λx, idp) /- Pathovers -/ @[hott] def arrow_pathover {B C : A → Type _} {f : B a → C a} {g : B a' → C a'} {p : a = a'} (r : Π(b : B a) (b' : B a') (q : b =[p] b'), f b =[p] g b') : f =[p; λa, B a → C a] g := begin induction p, apply pathover_idp_of_eq, apply eq_of_homotopy, hintro b, exact eq_of_pathover_idp (r b b idpo), end @[hott] def arrow_pathover_left {B C : A → Type _} {f : B a → C a} {g : B a' → C a'} {p : a = a'} (r : Π(b : B a), f b =[p] g (p ▸ b)) : f =[p; λa, B a → C a] g := begin induction p, apply pathover_idp_of_eq, apply eq_of_homotopy, hintro b, exact eq_of_pathover_idp (r b), end @[hott] def arrow_pathover_right {B C : A → Type _} {f : B a → C a} {g : B a' → C a'} {p : a = a'} (r : Π(b' : B a'), f (p⁻¹ ▸ b') =[p] g b') : f =[p; λa, B a → C a] g := begin induction p, apply pathover_idp_of_eq, apply eq_of_homotopy, hintro b, exact eq_of_pathover_idp (r b), end @[hott] def arrow_pathover_constant_left {B : Type _} {C : A → Type _} {f : B → C a} {g : B → C a'} {p : a = a'} (r : Π(b : B), f b =[p] g b) : f =[p; λa, B → C a] g := pi_pathover_constant r @[hott] def arrow_pathover_constant_right' {B : A → Type _} {C : Type _} {f : B a → C} {g : B a' → C} {p : a = a'} (r : Π⦃b : B a⦄ ⦃b' : B a'⦄ (q : b =[p] b'), f b = g b') : f =[p; λa, B a → C] g := arrow_pathover (λb b' q, pathover_of_eq p (r q)) @[hott] def arrow_pathover_constant_right {B : A → Type _} {C : Type _} {f : B a → C} {g : B a' → C} {p : a = a'} (r : Π(b : B a), f b = g (p ▸ b)) : f =[p; λa, B a → C] g := arrow_pathover_left (λb, pathover_of_eq p (r b)) @[hott] def arrow_pathover_constant_right_rev {A : Type _} {B : A → Type _} {C : Type _} {a a' : A} {f : B a → C} {g : B a' → C} {p : a = a'} (r : Π(b : B a'), f (p⁻¹ ▸ b) = g b) : f =[p; λa, B a → C] g := arrow_pathover_right (λb, pathover_of_eq p (r b)) /- a @[hott] lemma used for the flattening lemma, and is also used in the colimits file -/ @[hott] def apo11_arrow_pathover_constant_right {f : D a → A'} {g : D a' → A'} {p : a = a'} {q : d =[p] d'} (r : Π(d : D a), f d = g (p ▸ d)) : apo11_constant_right (arrow_pathover_constant_right r) q = r d ⬝ ap g (tr_eq_of_pathover q) := begin induction q, eapply homotopy.rec_on r, clear r, intro r, dsimp at r, induction r, dsimp [apd10, arrow_pathover_constant_right, arrow_pathover_left, pathover_of_eq, eq_of_pathover_idp, tr_eq_of_pathover], rwr [eq_of_homotopy_idp] end /- The fact that the arrow type preserves truncation level is a direct consequence of the fact that pi's preserve truncation level -/ @[hott] def is_trunc_arrow (B : Type _) (n : ℕ₋₂) [H : is_trunc n B] : is_trunc n (A → B) := by apply_instance end pi end hott
dc9ef8244bf2f61981f50af090b060dd791de5cc
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/refineOccursCheck.lean
f017c5bb2410c8b87a114c67288fa5340184d98e
[ "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
215
lean
theorem ex1 : True := by refine ?a refine ?a refine ?a exact True.intro axiom ax.{u} {α : Sort u} (a : α) : α theorem ex2 : True := by refine ?a refine ax ?a -- Error trying to assign `?a := ax ?a`
cf5c46078bae6cb8e3d306f79f5c90cf64efa51b
9dc8cecdf3c4634764a18254e94d43da07142918
/src/logic/function/conjugate.lean
a3208e9d3d108553759db67d0c97e65cd5f2aec3
[ "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
4,944
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import logic.function.basic /-! # Semiconjugate and commuting maps We define the following predicates: * `function.semiconj`: `f : α → β` semiconjugates `ga : α → α` to `gb : β → β` if `f ∘ ga = gb ∘ f`; * `function.semiconj₂: `f : α → β` semiconjugates a binary operation `ga : α → α → α` to `gb : β → β → β` if `f (ga x y) = gb (f x) (f y)`; * `f : α → α` commutes with `g : α → α` if `f ∘ g = g ∘ f`, or equivalently `semiconj f g g`. -/ namespace function variables {α : Type*} {β : Type*} {γ : Type*} /-- We say that `f : α → β` semiconjugates `ga : α → α` to `gb : β → β` if `f ∘ ga = gb ∘ f`. We use `∀ x, f (ga x) = gb (f x)` as the definition, so given `h : function.semiconj f ga gb` and `a : α`, we have `h a : f (ga a) = gb (f a)` and `h.comp_eq : f ∘ ga = gb ∘ f`. -/ def semiconj (f : α → β) (ga : α → α) (gb : β → β) : Prop := ∀ x, f (ga x) = gb (f x) namespace semiconj variables {f fab : α → β} {fbc : β → γ} {ga ga' : α → α} {gb gb' : β → β} {gc gc' : γ → γ} protected lemma comp_eq (h : semiconj f ga gb) : f ∘ ga = gb ∘ f := funext h protected lemma eq (h : semiconj f ga gb) (x : α) : f (ga x) = gb (f x) := h x lemma comp_right (h : semiconj f ga gb) (h' : semiconj f ga' gb') : semiconj f (ga ∘ ga') (gb ∘ gb') := λ x, by rw [comp_app, h.eq, h'.eq] lemma comp_left (hab : semiconj fab ga gb) (hbc : semiconj fbc gb gc) : semiconj (fbc ∘ fab) ga gc := λ x, by simp only [comp_app, hab.eq, hbc.eq] lemma id_right : semiconj f id id := λ _, rfl lemma id_left : semiconj id ga ga := λ _, rfl lemma inverses_right (h : semiconj f ga gb) (ha : right_inverse ga' ga) (hb : left_inverse gb' gb) : semiconj f ga' gb' := λ x, by rw [← hb (f (ga' x)), ← h.eq, ha x] lemma option_map {f : α → β} {ga : α → α} {gb : β → β} (h : semiconj f ga gb) : semiconj (option.map f) (option.map ga) (option.map gb) | none := rfl | (some a) := congr_arg some $ h _ end semiconj /-- Two maps `f g : α → α` commute if `f (g x) = g (f x)` for all `x : α`. Given `h : function.commute f g` and `a : α`, we have `h a : f (g a) = g (f a)` and `h.comp_eq : f ∘ g = g ∘ f`. -/ def commute (f g : α → α) : Prop := semiconj f g g lemma semiconj.commute {f g : α → α} (h : semiconj f g g) : commute f g := h namespace commute variables {f f' g g' : α → α} @[refl] lemma refl (f : α → α) : commute f f := λ _, eq.refl _ @[symm] lemma symm (h : commute f g) : commute g f := λ x, (h x).symm lemma comp_right (h : commute f g) (h' : commute f g') : commute f (g ∘ g') := h.comp_right h' lemma comp_left (h : commute f g) (h' : commute f' g) : commute (f ∘ f') g := (h.symm.comp_right h'.symm).symm lemma id_right : commute f id := semiconj.id_right lemma id_left : commute id f := semiconj.id_left lemma option_map {f g : α → α} : commute f g → commute (option.map f) (option.map g) := semiconj.option_map end commute /-- A map `f` semiconjugates a binary operation `ga` to a binary operation `gb` if for all `x`, `y` we have `f (ga x y) = gb (f x) (f y)`. E.g., a `monoid_hom` semiconjugates `(*)` to `(*)`. -/ def semiconj₂ (f : α → β) (ga : α → α → α) (gb : β → β → β) : Prop := ∀ x y, f (ga x y) = gb (f x) (f y) namespace semiconj₂ variables {f : α → β} {ga : α → α → α} {gb : β → β → β} protected lemma eq (h : semiconj₂ f ga gb) (x y : α) : f (ga x y) = gb (f x) (f y) := h x y protected lemma comp_eq (h : semiconj₂ f ga gb) : bicompr f ga = bicompl gb f f := funext $ λ x, funext $ h x lemma id_left (op : α → α → α) : semiconj₂ id op op := λ _ _, rfl lemma comp {f' : β → γ} {gc : γ → γ → γ} (hf' : semiconj₂ f' gb gc) (hf : semiconj₂ f ga gb) : semiconj₂ (f' ∘ f) ga gc := λ x y, by simp only [hf'.eq, hf.eq, comp_app] lemma is_associative_right [is_associative α ga] (h : semiconj₂ f ga gb) (h_surj : surjective f) : is_associative β gb := ⟨h_surj.forall₃.2 $ λ x₁ x₂ x₃, by simp only [← h.eq, @is_associative.assoc _ ga]⟩ lemma is_associative_left [is_associative β gb] (h : semiconj₂ f ga gb) (h_inj : injective f) : is_associative α ga := ⟨λ x₁ x₂ x₃, h_inj $ by simp only [h.eq, @is_associative.assoc _ gb]⟩ lemma is_idempotent_right [is_idempotent α ga] (h : semiconj₂ f ga gb) (h_surj : surjective f) : is_idempotent β gb := ⟨h_surj.forall.2 $ λ x, by simp only [← h.eq, @is_idempotent.idempotent _ ga]⟩ lemma is_idempotent_left [is_idempotent β gb] (h : semiconj₂ f ga gb) (h_inj : injective f) : is_idempotent α ga := ⟨λ x, h_inj $ by rw [h.eq, @is_idempotent.idempotent _ gb]⟩ end semiconj₂ end function
0dd0e89965724d51ae0c2cdcbe991fb492c9dbab
00d2363f9655e2a7618f6b94dda7e2c4e5cf8d19
/src/proof_recording_example.lean
94fb8ac7b69b3bdd3426af8e4a1daa55d7470156
[ "Apache-2.0" ]
permissive
devjuice1/lean_proof_recording
927e276e2ab8fb1288f51d9146dcfbf0d6444a87
bf7c527315deccd35363fa7ca89d97d7b9cb6ac1
refs/heads/master
1,692,914,925,585
1,633,018,872,000
1,633,018,872,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,858
lean
-- co-opting pp.colors as a flag to turn the tracing on -- can also be set from the command line with -D pp.colors=false set_option pp.colors false example : true := begin skip, exact by trivial, done end example : true := by exact true.intro example : true := by trivial example (a b c : ℕ) (H1 : a = b + 1) (H2 : b = c) : a = c + 1 := calc a = b + 1 : by exact H1 ... = c + 1 : by rw [ by exact H2 ] example (a b c : ℕ) : a * (b * c) = a * (c * b) := begin conv begin -- | a * (b * c) = a * (c * b) to_lhs, -- | a * (b * c) congr, -- 2 goals : | a and | b * c skip, -- | b * c rw nat.mul_comm -- | c * b end end example : true := begin skip <|> skip, /- Second skip not executed -/ done <|> skip, trivial end example (n : ℕ) : n = n := by { induction n, simp, simp } example (n : ℕ) : n = n := by induction n ; simp -- grouped as (((skip ; skip) ; induction n) ; skip) ; simp example (n : ℕ) : n = n := by skip ; skip ; induction n ; skip ; simp example (n : ℕ) : n = n := begin induction n, { simp }, simp end example : ∀ (n a : ℕ), n = n := begin repeat { intro }, simp end example : true := begin try { done }, try { trivial } end example (p q r : Prop) : p ∧ (q ∨ r) → (p ∧ q) ∨ (p ∧ r) := begin intro h, cases h with hp hqr, show (p ∧ q) ∨ (p ∧ r), cases hqr with hq hr, have hpq : p ∧ q, split ; assumption , left, exact hpq, have hpr : p ∧ r, split ; assumption, right, exact hpr end example (p q r : Prop) (hp : p) (hq : q) (hr : r) : p ∧ q ∧ r := begin split, all_goals { try { split } } , all_goals { assumption } end example (p q r : Prop) (hp : p) (hq : q) (hr : r) : p ∧ q ∧ r := begin repeat { all_goals { split <|> assumption } } end example :true := begin skip, skip; trivial end example (n m k : ℕ) (h1 : n = m) (h2 : m = k): (n = k) := begin rw [show n = m, by assumption, show m = k, by assumption] end theorem sub_le_sub_left {n m : ℕ} (k) (h : n ≤ m) : k - m ≤ k - n := by induction h; [refl, exact le_trans (nat.pred_le _) h_ih] example : true := by { try { trivial } } example : true := by begin try begin trivial end end -- grouped as done <|> (done <|> trivial) example : true := by done <|> done <|> trivial -- unexecuted tactic at end example : true := by { trivial ; done } -- special notation example (n : nat) : true := by induction n ; [skip, skip]; [trivial, trivial] example : true := begin skip, trivial, -- trailing comma end -- trailing comma example : true := by { skip ; trivial, } example : true := by { { skip ; [skip, skip] } <|> trivial} -- { } block with no executed children -- there is no first goal to focus on example : true := by { trivial , { skip, skip } } <|> trivial
2caa40b6627d1debdfe48cb5f740ac5a958ba520
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/char/default.lean
9a39e92636d63586a3d5a913fa41e63d17e5a851
[]
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
347
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.data.char.basic import Mathlib.Lean3Lib.init.data.char.lemmas import Mathlib.Lean3Lib.init.data.char.classes namespace Mathlib
19ad87c872bfd6a2ec616e3be41ee1839a72794a
6afa22d5eee6e9a56b6a2f1210eca8f7a1067466
/tests/lean/widget/widget2.lean
66f0437d92cc911c383df68c2eab73d21a23a7ea
[ "Apache-2.0" ]
permissive
giordano/lean
72a1fabfeb2f1ccfd38673e2719a719cd6ffbb40
56f8877f1efa22215aca0b82f1c0ce2ff975b9c3
refs/heads/master
1,663,091,511,168
1,590,688,082,000
1,590,688,082,000
268,183,678
0
0
Apache-2.0
1,590,885,425,000
1,590,885,424,000
null
UTF-8
Lean
false
false
371
lean
open widget /-- A simple counter that can be incremented or decremented with some buttons. -/ meta def counter_widget {π α : Type} : component π α := component.ignore_props $ component.mk_simple int int 0 (λ _ x y, (x + y, none)) (λ _ s, h "div" [] [ button "+" (1 : int), html.of_string $ to_string $ s, button "-" (-1) ] ) #html counter_widget
58fb3449fb7928b188e8f0f4d1844e88da4cfffb
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/idempotents/functor_extension.lean
0307fbd5ed354441f559308e94cda2d1601cfb89
[ "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
11,101
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 category_theory.idempotents.karoubi /-! # Extension of functors to the idempotent completion > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file, we construct an extension `functor_extension₁` of functors `C ⥤ karoubi D` to functors `karoubi C ⥤ karoubi D`. This results in an equivalence `karoubi_universal₁ C D : (C ⥤ karoubi D) ≌ (karoubi C ⥤ karoubi D)`. We also construct an extension `functor_extension₂` of functors `(C ⥤ D) ⥤ (karoubi C ⥤ karoubi D)`. Moreover, when `D` is idempotent complete, we get equivalences `karoubi_universal₂ C D : C ⥤ D ≌ karoubi C ⥤ karoubi D` and `karoubi_universal C D : C ⥤ D ≌ karoubi C ⥤ D`. We occasionally state and use equalities of functors because it is sometimes convenient to use rewrites when proving properties of functors obtained using the constructions in this file. Users are encouraged to use the corresponding natural isomorphism whenever possible. -/ open category_theory.category open category_theory.idempotents.karoubi namespace category_theory namespace idempotents variables {C D E : Type*} [category C] [category D] [category E] /-- A natural transformation between functors `karoubi C ⥤ D` is determined by its value on objects coming from `C`. -/ lemma nat_trans_eq {F G : karoubi C ⥤ D} (φ : F ⟶ G) (P : karoubi C) : φ.app P = F.map (decomp_id_i P) ≫ φ.app P.X ≫ G.map (decomp_id_p P) := begin rw [← φ.naturality, ← assoc, ← F.map_comp], conv { to_lhs, rw [← id_comp (φ.app P), ← F.map_id], }, congr, apply decomp_id, end namespace functor_extension₁ /-- The canonical extension of a functor `C ⥤ karoubi D` to a functor `karoubi C ⥤ karoubi D` -/ @[simps] def obj (F : C ⥤ karoubi D) : karoubi C ⥤ karoubi D := { obj := λ P, ⟨(F.obj P.X).X, (F.map P.p).f, by simpa only [F.map_comp, hom_ext] using F.congr_map P.idem⟩, map := λ P Q f, ⟨(F.map f.f).f, by simpa only [F.map_comp, hom_ext] using F.congr_map f.comm⟩, } /-- Extension of a natural transformation `φ` between functors `C ⥤ karoubi D` to a natural transformation between the extension of these functors to `karoubi C ⥤ karoubi D` -/ @[simps] def map {F G : C ⥤ karoubi D} (φ : F ⟶ G) : obj F ⟶ obj G := { app := λ P, { f := (F.map P.p).f ≫ (φ.app P.X).f, comm := begin have h := φ.naturality P.p, have h' := F.congr_map P.idem, simp only [hom_ext, karoubi.comp_f, F.map_comp] at h h', simp only [obj_obj_p, assoc, ← h], slice_rhs 1 3 { rw [h', h'], }, end, }, naturality' := λ P Q f, begin ext, dsimp [obj], have h := φ.naturality f.f, have h' := F.congr_map (comp_p f), have h'' := F.congr_map (p_comp f), simp only [hom_ext, functor.map_comp, comp_f] at ⊢ h h' h'', slice_rhs 2 3 { rw ← h, }, slice_lhs 1 2 { rw h', }, slice_rhs 1 2 { rw h'', }, end } end functor_extension₁ variables (C D E) /-- The canonical functor `(C ⥤ karoubi D) ⥤ (karoubi C ⥤ karoubi D)` -/ @[simps] def functor_extension₁ : (C ⥤ karoubi D) ⥤ (karoubi C ⥤ karoubi D) := { obj := functor_extension₁.obj, map := λ F G, functor_extension₁.map, map_id' := λ F, by { ext P, exact comp_p (F.map P.p), }, map_comp' := λ F G H φ φ', begin ext P, simp only [comp_f, functor_extension₁.map_app_f, nat_trans.comp_app, assoc], have h := φ.naturality P.p, have h' := F.congr_map P.idem, simp only [hom_ext, comp_f, F.map_comp] at h h', slice_rhs 2 3 { rw ← h, }, slice_rhs 1 2 { rw h', }, simp only [assoc], end, } lemma functor_extension₁_comp_whiskering_left_to_karoubi : functor_extension₁ C D ⋙ (whiskering_left C (karoubi C) (karoubi D)).obj (to_karoubi C) = 𝟭 _ := begin refine functor.ext _ _, { intro F, refine functor.ext _ _, { intro X, ext, { dsimp, rw [id_comp, comp_id, F.map_id, id_eq], }, { refl, }, }, { intros X Y f, ext, dsimp, simp only [comp_id, eq_to_hom_f, eq_to_hom_refl, comp_p, functor_extension₁.obj_obj_p, to_karoubi_obj_p, comp_f], dsimp, simp only [functor.map_id, id_eq, p_comp], }, }, { intros F G φ, ext X, dsimp, simp only [eq_to_hom_app, F.map_id, comp_f, eq_to_hom_f, id_eq, p_comp, eq_to_hom_refl, comp_id, comp_p, functor_extension₁.obj_obj_p, to_karoubi_obj_p, F.map_id X], }, end /-- The natural isomorphism expressing that functors `karoubi C ⥤ karoubi D` obtained using `functor_extension₁` actually extends the original functors `C ⥤ karoubi D`. -/ @[simps] def functor_extension₁_comp_whiskering_left_to_karoubi_iso : functor_extension₁ C D ⋙ (whiskering_left C (karoubi C) (karoubi D)).obj (to_karoubi C) ≅ 𝟭 _ := eq_to_iso (functor_extension₁_comp_whiskering_left_to_karoubi C D) /-- The counit isomorphism of the equivalence `(C ⥤ karoubi D) ≌ (karoubi C ⥤ karoubi D)`. -/ @[simps] def karoubi_universal₁.counit_iso : (whiskering_left C (karoubi C) (karoubi D)).obj (to_karoubi C) ⋙ functor_extension₁ C D ≅ 𝟭 _ := nat_iso.of_components (λ G, { hom := { app := λ P, { f := (G.map (decomp_id_p P)).f, comm := by simpa only [hom_ext, G.map_comp, G.map_id] using G.congr_map (show P.decomp_id_p = (to_karoubi C).map P.p ≫ P.decomp_id_p ≫ 𝟙 _, by simp), }, naturality' := λ P Q f, by simpa only [hom_ext, G.map_comp] using (G.congr_map (decomp_id_p_naturality f)).symm, }, inv := { app := λ P, { f := (G.map (decomp_id_i P)).f, comm := by simpa only [hom_ext, G.map_comp, G.map_id] using G.congr_map (show P.decomp_id_i = 𝟙 _ ≫ P.decomp_id_i ≫ (to_karoubi C).map P.p, by simp), }, naturality' := λ P Q f, by simpa only [hom_ext, G.map_comp] using G.congr_map (decomp_id_i_naturality f), }, hom_inv_id' := begin ext P, simpa only [hom_ext, G.map_comp, G.map_id] using G.congr_map P.decomp_p.symm, end, inv_hom_id' := begin ext P, simpa only [hom_ext, G.map_comp, G.map_id] using G.congr_map P.decomp_id.symm, end, }) (λ G₁ G₂ φ, begin ext P, dsimp, simpa only [nat_trans_eq φ P, comp_f, functor_extension₁.map_app_f, functor.comp_map, whisker_left_app, assoc, P.decomp_p, G₁.map_comp], end) /-- The equivalence of categories `(C ⥤ karoubi D) ≌ (karoubi C ⥤ karoubi D)`. -/ @[simps] def karoubi_universal₁ : (C ⥤ karoubi D) ≌ (karoubi C ⥤ karoubi D) := { functor := functor_extension₁ C D, inverse := (whiskering_left C (karoubi C) (karoubi D)).obj (to_karoubi C), unit_iso := (functor_extension₁_comp_whiskering_left_to_karoubi_iso C D).symm, counit_iso := karoubi_universal₁.counit_iso C D, functor_unit_iso_comp' := λ F, begin ext P, dsimp [functor_extension₁.map, karoubi_universal₁.counit_iso], simpa only [comp_f, eq_to_hom_app, eq_to_hom_f, eq_to_hom_refl, comp_id, hom_ext, F.map_comp, comp_p] using F.congr_map P.idem, end, } lemma functor_extension₁_comp (F : C ⥤ karoubi D) (G : D ⥤ karoubi E) : (functor_extension₁ C E).obj (F ⋙ (functor_extension₁ D E).obj G) = (functor_extension₁ C D).obj F ⋙ (functor_extension₁ D E).obj G := functor.ext (by tidy) (λ X Y f, by { dsimp, simpa only [id_comp, comp_id], }) /-- The canonical functor `(C ⥤ D) ⥤ (karoubi C ⥤ karoubi D)` -/ @[simps] def functor_extension₂ : (C ⥤ D) ⥤ (karoubi C ⥤ karoubi D) := (whiskering_right C D (karoubi D)).obj (to_karoubi D) ⋙ functor_extension₁ C D lemma functor_extension₂_comp_whiskering_left_to_karoubi : functor_extension₂ C D ⋙ (whiskering_left C (karoubi C) (karoubi D)).obj (to_karoubi C) = (whiskering_right C D (karoubi D)).obj (to_karoubi D) := by simp only [functor_extension₂, functor.assoc, functor_extension₁_comp_whiskering_left_to_karoubi, functor.comp_id] /-- The natural isomorphism expressing that functors `karoubi C ⥤ karoubi D` obtained using `functor_extension₂` actually extends the original functors `C ⥤ D`. -/ @[simps] def functor_extension₂_comp_whiskering_left_to_karoubi_iso : functor_extension₂ C D ⋙ (whiskering_left C (karoubi C) (karoubi D)).obj (to_karoubi C) ≅ (whiskering_right C D (karoubi D)).obj (to_karoubi D) := eq_to_iso (functor_extension₂_comp_whiskering_left_to_karoubi C D) section is_idempotent_complete variable [is_idempotent_complete D] noncomputable instance : is_equivalence (to_karoubi D) := to_karoubi_is_equivalence D /-- The equivalence of categories `(C ⥤ D) ≌ (karoubi C ⥤ karoubi D)` when `D` is idempotent complete. -/ @[simps] noncomputable def karoubi_universal₂ : (C ⥤ D) ≌ (karoubi C ⥤ karoubi D) := (equivalence.congr_right (to_karoubi D).as_equivalence).trans (karoubi_universal₁ C D) lemma karoubi_universal₂_functor_eq : (karoubi_universal₂ C D).functor = functor_extension₂ C D := rfl noncomputable instance : is_equivalence (functor_extension₂ C D) := by { rw ← karoubi_universal₂_functor_eq, apply_instance, } /-- The extension of functors functor `(C ⥤ D) ⥤ (karoubi C ⥤ D)` when `D` is idempotent compltete. -/ @[simps] noncomputable def functor_extension : (C ⥤ D) ⥤ (karoubi C ⥤ D) := functor_extension₂ C D ⋙ (whiskering_right (karoubi C) (karoubi D) D).obj (to_karoubi_is_equivalence D).inverse /-- The equivalence `(C ⥤ D) ≌ (karoubi C ⥤ D)` when `D` is idempotent complete. -/ @[simps] noncomputable def karoubi_universal : (C ⥤ D) ≌ (karoubi C ⥤ D) := (karoubi_universal₂ C D).trans (equivalence.congr_right (to_karoubi D).as_equivalence.symm) lemma karoubi_universal_functor_eq : (karoubi_universal C D).functor = functor_extension C D := rfl noncomputable instance : is_equivalence (functor_extension C D) := by { rw ← karoubi_universal_functor_eq, apply_instance, } noncomputable instance : is_equivalence ((whiskering_left C (karoubi C) D).obj (to_karoubi C)) := is_equivalence.cancel_comp_right _ ((whiskering_right C _ _).obj (to_karoubi D) ⋙ (whiskering_right C _ _).obj (to_karoubi D).inv) (is_equivalence.of_equivalence (@equivalence.congr_right _ _ _ _ C _ ((to_karoubi D).as_equivalence.trans (to_karoubi D).as_equivalence.symm))) (by { change is_equivalence (karoubi_universal C D).inverse, apply_instance, }) variables {C D} lemma whiskering_left_obj_preimage_app {F G : karoubi C ⥤ D} (τ : to_karoubi _ ⋙ F ⟶ to_karoubi _ ⋙ G) (P : karoubi C) : (((whiskering_left _ _ _).obj (to_karoubi _)).preimage τ).app P = F.map P.decomp_id_i ≫ τ.app P.X ≫ G.map P.decomp_id_p := begin rw nat_trans_eq, congr' 2, exact congr_app (((whiskering_left _ _ _).obj (to_karoubi _)).image_preimage τ) P.X, end end is_idempotent_complete end idempotents end category_theory
60ff84160cfccff830df93f25616eedceea65223
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/linarith/lemmas_auto.lean
248105fa5d490c3811f1a50f8d452bf5588f0cfe
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
3,637
lean
/- Copyright (c) 2020 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.ordered_ring import Mathlib.data.int.basic import Mathlib.tactic.norm_num import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Lemmas for `linarith` This file contains auxiliary lemmas that `linarith` uses to construct proofs. If you find yourself looking for a theorem here, you might be in the wrong place. -/ namespace linarith theorem int.coe_nat_bit0 (n : ℕ) : ↑(bit0 n) = bit0 ↑n := sorry theorem int.coe_nat_bit1 (n : ℕ) : ↑(bit1 n) = bit1 ↑n := sorry theorem int.coe_nat_bit0_mul (n : ℕ) (x : ℕ) : ↑(bit0 n * x) = ↑(bit0 n) * ↑x := sorry theorem int.coe_nat_bit1_mul (n : ℕ) (x : ℕ) : ↑(bit1 n * x) = ↑(bit1 n) * ↑x := sorry theorem int.coe_nat_one_mul (x : ℕ) : ↑(1 * x) = 1 * ↑x := sorry theorem int.coe_nat_zero_mul (x : ℕ) : ↑(0 * x) = 0 * ↑x := sorry theorem int.coe_nat_mul_bit0 (n : ℕ) (x : ℕ) : ↑(x * bit0 n) = ↑x * ↑(bit0 n) := sorry theorem int.coe_nat_mul_bit1 (n : ℕ) (x : ℕ) : ↑(x * bit1 n) = ↑x * ↑(bit1 n) := sorry theorem int.coe_nat_mul_one (x : ℕ) : ↑(x * 1) = ↑x * 1 := sorry theorem int.coe_nat_mul_zero (x : ℕ) : ↑(x * 0) = ↑x * 0 := sorry theorem nat_eq_subst {n1 : ℕ} {n2 : ℕ} {z1 : ℤ} {z2 : ℤ} (hn : n1 = n2) (h1 : ↑n1 = z1) (h2 : ↑n2 = z2) : z1 = z2 := sorry theorem nat_le_subst {n1 : ℕ} {n2 : ℕ} {z1 : ℤ} {z2 : ℤ} (hn : n1 ≤ n2) (h1 : ↑n1 = z1) (h2 : ↑n2 = z2) : z1 ≤ z2 := sorry theorem nat_lt_subst {n1 : ℕ} {n2 : ℕ} {z1 : ℤ} {z2 : ℤ} (hn : n1 < n2) (h1 : ↑n1 = z1) (h2 : ↑n2 = z2) : z1 < z2 := sorry theorem eq_of_eq_of_eq {α : Type u_1} [ordered_semiring α] {a : α} {b : α} (ha : a = 0) (hb : b = 0) : a + b = 0 := sorry theorem le_of_eq_of_le {α : Type u_1} [ordered_semiring α] {a : α} {b : α} (ha : a = 0) (hb : b ≤ 0) : a + b ≤ 0 := sorry theorem lt_of_eq_of_lt {α : Type u_1} [ordered_semiring α] {a : α} {b : α} (ha : a = 0) (hb : b < 0) : a + b < 0 := sorry theorem le_of_le_of_eq {α : Type u_1} [ordered_semiring α] {a : α} {b : α} (ha : a ≤ 0) (hb : b = 0) : a + b ≤ 0 := sorry theorem lt_of_lt_of_eq {α : Type u_1} [ordered_semiring α] {a : α} {b : α} (ha : a < 0) (hb : b = 0) : a + b < 0 := sorry theorem mul_neg {α : Type u_1} [ordered_ring α] {a : α} {b : α} (ha : a < 0) (hb : 0 < b) : b * a < 0 := sorry theorem mul_nonpos {α : Type u_1} [ordered_ring α] {a : α} {b : α} (ha : a ≤ 0) (hb : 0 < b) : b * a ≤ 0 := sorry -- used alongside `mul_neg` and `mul_nonpos`, so has the same argument pattern for uniformity theorem mul_eq {α : Type u_1} [ordered_semiring α] {a : α} {b : α} (ha : a = 0) (hb : 0 < b) : b * a = 0 := sorry theorem eq_of_not_lt_of_not_gt {α : Type u_1} [linear_order α] (a : α) (b : α) (h1 : ¬a < b) (h2 : ¬b < a) : a = b := le_antisymm (le_of_not_gt h2) (le_of_not_gt h1) -- used in the `nlinarith` normalization steps. The `_` argument is for uniformity. theorem mul_zero_eq {α : Type u_1} {R : α → α → Prop} [semiring α] {a : α} {b : α} (_x : R a 0) (h : b = 0) : a * b = 0 := sorry -- used in the `nlinarith` normalization steps. The `_` argument is for uniformity. theorem zero_mul_eq {α : Type u_1} {R : α → α → Prop} [semiring α] {a : α} {b : α} (h : a = 0) (_x : R b 0) : a * b = 0 := sorry end Mathlib
62bc19a56251affcb6326f3f1683540ebf7c02bc
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/polynomial/basic.lean
f287919dda4e2682d27d4b8ff6b49c5af40c443e
[ "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
43,980
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.char_p.basic import algebra.geom_sum import data.mv_polynomial.comm_ring import data.mv_polynomial.equiv import ring_theory.polynomial.content import ring_theory.unique_factorization_domain /-! # Ring-theoretic supplement of data.polynomial. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Main results * `mv_polynomial.is_domain`: If a ring is an integral domain, then so is its polynomial ring over finitely many variables. * `polynomial.is_noetherian_ring`: Hilbert basis theorem, that if a ring is noetherian then so is its polynomial ring. * `polynomial.wf_dvd_monoid`: If an integral domain is a `wf_dvd_monoid`, then so is its polynomial ring. * `polynomial.unique_factorization_monoid`, `mv_polynomial.unique_factorization_monoid`: If an integral domain is a `unique_factorization_monoid`, then so is its polynomial ring (of any number of variables). -/ noncomputable theory open_locale classical big_operators polynomial open finset universes u v w variables {R : Type u} {S : Type*} namespace polynomial section semiring variables [semiring R] instance (p : ℕ) [h : char_p R p] : char_p R[X] p := let ⟨h⟩ := h in ⟨λ n, by rw [← map_nat_cast C, ← C_0, C_inj, h]⟩ variables (R) /-- The `R`-submodule of `R[X]` consisting of polynomials of degree ≤ `n`. -/ def degree_le (n : with_bot ℕ) : submodule R R[X] := ⨅ k : ℕ, ⨅ h : ↑k > n, (lcoeff R k).ker /-- The `R`-submodule of `R[X]` consisting of polynomials of degree < `n`. -/ def degree_lt (n : ℕ) : submodule R R[X] := ⨅ k : ℕ, ⨅ h : k ≥ n, (lcoeff R k).ker variable {R} theorem mem_degree_le {n : with_bot ℕ} {f : R[X]} : f ∈ degree_le R n ↔ degree f ≤ n := by simp only [degree_le, submodule.mem_infi, degree_le_iff_coeff_zero, linear_map.mem_ker]; refl @[mono] theorem degree_le_mono {m n : with_bot ℕ} (H : m ≤ n) : degree_le R m ≤ degree_le R n := λ f hf, mem_degree_le.2 (le_trans (mem_degree_le.1 hf) H) theorem degree_le_eq_span_X_pow {n : ℕ} : degree_le R n = submodule.span R ↑((finset.range (n+1)).image (λ n, (X : R[X])^n)) := begin apply le_antisymm, { intros p hp, replace hp := mem_degree_le.1 hp, rw [← polynomial.sum_monomial_eq p, polynomial.sum], refine submodule.sum_mem _ (λ k hk, _), show monomial _ _ ∈ _, have := with_bot.coe_le_coe.1 (finset.sup_le_iff.1 hp k hk), rw [← C_mul_X_pow_eq_monomial, C_mul'], refine submodule.smul_mem _ _ (submodule.subset_span $ finset.mem_coe.2 $ finset.mem_image.2 ⟨_, finset.mem_range.2 (nat.lt_succ_of_le this), rfl⟩) }, rw [submodule.span_le, finset.coe_image, set.image_subset_iff], intros k hk, apply mem_degree_le.2, exact (degree_X_pow_le _).trans (with_bot.coe_le_coe.2 $ nat.le_of_lt_succ $ finset.mem_range.1 hk) end theorem mem_degree_lt {n : ℕ} {f : R[X]} : f ∈ degree_lt R n ↔ degree f < n := by { simp_rw [degree_lt, submodule.mem_infi, linear_map.mem_ker, degree, finset.max_eq_sup_coe, finset.sup_lt_iff (with_bot.bot_lt_coe n), mem_support_iff, with_bot.coe_lt_coe, lt_iff_not_le, ne, not_imp_not], refl } @[mono] theorem degree_lt_mono {m n : ℕ} (H : m ≤ n) : degree_lt R m ≤ degree_lt R n := λ f hf, mem_degree_lt.2 (lt_of_lt_of_le (mem_degree_lt.1 hf) $ with_bot.coe_le_coe.2 H) theorem degree_lt_eq_span_X_pow {n : ℕ} : degree_lt R n = submodule.span R ↑((finset.range n).image (λ n, X^n) : finset R[X]) := begin apply le_antisymm, { intros p hp, replace hp := mem_degree_lt.1 hp, rw [← polynomial.sum_monomial_eq p, polynomial.sum], refine submodule.sum_mem _ (λ k hk, _), show monomial _ _ ∈ _, have := with_bot.coe_lt_coe.1 ((finset.sup_lt_iff $ with_bot.bot_lt_coe n).1 hp k hk), rw [← C_mul_X_pow_eq_monomial, C_mul'], refine submodule.smul_mem _ _ (submodule.subset_span $ finset.mem_coe.2 $ finset.mem_image.2 ⟨_, finset.mem_range.2 this, rfl⟩) }, rw [submodule.span_le, finset.coe_image, set.image_subset_iff], intros k hk, apply mem_degree_lt.2, exact lt_of_le_of_lt (degree_X_pow_le _) (with_bot.coe_lt_coe.2 $ finset.mem_range.1 hk) end /-- The first `n` coefficients on `degree_lt n` form a linear equivalence with `fin n → R`. -/ def degree_lt_equiv (R) [semiring R] (n : ℕ) : degree_lt R n ≃ₗ[R] (fin n → R) := { to_fun := λ p n, (↑p : R[X]).coeff n, inv_fun := λ f, ⟨∑ i : fin n, monomial i (f i), (degree_lt R n).sum_mem (λ i _, mem_degree_lt.mpr (lt_of_le_of_lt (degree_monomial_le i (f i)) (with_bot.coe_lt_coe.mpr i.is_lt)))⟩, map_add' := λ p q, by { ext, rw [submodule.coe_add, coeff_add], refl }, map_smul' := λ x p, by { ext, rw [submodule.coe_smul, coeff_smul], refl }, left_inv := begin rintro ⟨p, hp⟩, ext1, simp only [submodule.coe_mk], by_cases hp0 : p = 0, { subst hp0, simp only [coeff_zero, linear_map.map_zero, finset.sum_const_zero] }, rw [mem_degree_lt, degree_eq_nat_degree hp0, with_bot.coe_lt_coe] at hp, conv_rhs { rw [p.as_sum_range' n hp, ← fin.sum_univ_eq_sum_range] }, end, right_inv := begin intro f, ext i, simp only [finset_sum_coeff, submodule.coe_mk], rw [finset.sum_eq_single i, coeff_monomial, if_pos rfl], { rintro j - hji, rw [coeff_monomial, if_neg], rwa [← fin.ext_iff] }, { intro h, exact (h (finset.mem_univ _)).elim } end } @[simp] theorem degree_lt_equiv_eq_zero_iff_eq_zero {n : ℕ} {p : R[X]} (hp : p ∈ degree_lt R n) : degree_lt_equiv _ _ ⟨p, hp⟩ = 0 ↔ p = 0 := by rw [linear_equiv.map_eq_zero_iff, submodule.mk_eq_zero] theorem eval_eq_sum_degree_lt_equiv {n : ℕ} {p : R[X]} (hp : p ∈ degree_lt R n) (x : R) : p.eval x = ∑ i, degree_lt_equiv _ _ ⟨p, hp⟩ i * (x ^ (i : ℕ)) := begin simp_rw [eval_eq_sum], exact (sum_fin _ (by simp_rw [zero_mul, forall_const]) (mem_degree_lt.mp hp)).symm end /-- The finset of nonzero coefficients of a polynomial. -/ def frange (p : R[X]) : finset R := finset.image (λ n, p.coeff n) p.support lemma frange_zero : frange (0 : R[X]) = ∅ := rfl lemma mem_frange_iff {p : R[X]} {c : R} : c ∈ p.frange ↔ ∃ n ∈ p.support, c = p.coeff n := by simp [frange, eq_comm] lemma frange_one : frange (1 : R[X]) ⊆ {1} := begin simp [frange, finset.image_subset_iff], simp only [← C_1, coeff_C], assume n hn, simp only [exists_prop, ite_eq_right_iff, not_forall] at hn, simp [hn], end lemma coeff_mem_frange (p : R[X]) (n : ℕ) (h : p.coeff n ≠ 0) : p.coeff n ∈ p.frange := begin simp only [frange, exists_prop, mem_support_iff, finset.mem_image, ne.def], exact ⟨n, h, rfl⟩, end lemma geom_sum_X_comp_X_add_one_eq_sum (n : ℕ) : (∑ i in range n, (X : R[X]) ^ i).comp (X + 1) = (finset.range n).sum (λ (i : ℕ), (n.choose (i + 1) : R[X]) * X ^ i) := begin ext i, transitivity (n.choose (i + 1) : R), swap, { simp only [finset_sum_coeff, ← C_eq_nat_cast, coeff_C_mul_X_pow], rw [finset.sum_eq_single i, if_pos rfl], { simp only [@eq_comm _ i, if_false, eq_self_iff_true, implies_true_iff] {contextual := tt}, }, { simp only [nat.lt_add_one_iff, nat.choose_eq_zero_of_lt, nat.cast_zero, finset.mem_range, not_lt, eq_self_iff_true, if_true, implies_true_iff] {contextual := tt}, } }, induction n with n ih generalizing i, { simp only [geom_sum_zero, zero_comp, coeff_zero, nat.choose_zero_succ, nat.cast_zero], }, simp only [geom_sum_succ', ih, add_comp, X_pow_comp, coeff_add, nat.choose_succ_succ, nat.cast_add, coeff_X_add_one_pow], end lemma monic.geom_sum {P : R[X]} (hP : P.monic) (hdeg : 0 < P.nat_degree) {n : ℕ} (hn : n ≠ 0) : (∑ i in range n, P ^ i).monic := begin nontriviality R, cases n, { exact (hn rfl).elim }, rw [geom_sum_succ'], refine (hP.pow _).add_of_left _, refine lt_of_le_of_lt (degree_sum_le _ _) _, rw [finset.sup_lt_iff], { simp only [finset.mem_range, degree_eq_nat_degree (hP.pow _).ne_zero, with_bot.coe_lt_coe, hP.nat_degree_pow], intro k, exact nsmul_lt_nsmul hdeg }, { rw [bot_lt_iff_ne_bot, ne.def, degree_eq_bot], exact (hP.pow _).ne_zero } end lemma monic.geom_sum' {P : R[X]} (hP : P.monic) (hdeg : 0 < P.degree) {n : ℕ} (hn : n ≠ 0) : (∑ i in range n, P ^ i).monic := hP.geom_sum (nat_degree_pos_iff_degree_pos.2 hdeg) hn lemma monic_geom_sum_X {n : ℕ} (hn : n ≠ 0) : (∑ i in range n, (X : R[X]) ^ i).monic := begin nontriviality R, apply monic_X.geom_sum _ hn, simpa only [nat_degree_X] using zero_lt_one end end semiring section ring variables [ring R] /-- Given a polynomial, return the polynomial whose coefficients are in the ring closure of the original coefficients. -/ def restriction (p : R[X]) : polynomial (subring.closure (↑p.frange : set R)) := ∑ i in p.support, monomial i (⟨p.coeff i, if H : p.coeff i = 0 then H.symm ▸ (subring.closure _).zero_mem else subring.subset_closure (p.coeff_mem_frange _ H)⟩ : (subring.closure (↑p.frange : set R))) @[simp] theorem coeff_restriction {p : R[X]} {n : ℕ} : ↑(coeff (restriction p) n) = coeff p n := begin simp only [restriction, coeff_monomial, finset_sum_coeff, mem_support_iff, finset.sum_ite_eq', ne.def, ite_not], split_ifs, { rw h, refl }, { refl } end @[simp] theorem coeff_restriction' {p : R[X]} {n : ℕ} : (coeff (restriction p) n).1 = coeff p n := coeff_restriction @[simp] lemma support_restriction (p : R[X]) : support (restriction p) = support p := begin ext i, simp only [mem_support_iff, not_iff_not, ne.def], conv_rhs { rw [← coeff_restriction] }, exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩ end @[simp] theorem map_restriction {R : Type u} [comm_ring R] (p : R[X]) : p.restriction.map (algebra_map _ _) = p := ext $ λ n, by rw [coeff_map, algebra.algebra_map_of_subring_apply, coeff_restriction] @[simp] theorem degree_restriction {p : R[X]} : (restriction p).degree = p.degree := by simp [degree] @[simp] theorem nat_degree_restriction {p : R[X]} : (restriction p).nat_degree = p.nat_degree := by simp [nat_degree] @[simp] theorem monic_restriction {p : R[X]} : monic (restriction p) ↔ monic p := begin simp only [monic, leading_coeff, nat_degree_restriction], rw [←@coeff_restriction _ _ p], exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩ end @[simp] theorem restriction_zero : restriction (0 : R[X]) = 0 := by simp only [restriction, finset.sum_empty, support_zero] @[simp] theorem restriction_one : restriction (1 : R[X]) = 1 := ext $ λ i, subtype.eq $ by rw [coeff_restriction', coeff_one, coeff_one]; split_ifs; refl variables [semiring S] {f : R →+* S} {x : S} theorem eval₂_restriction {p : R[X]} : eval₂ f x p = eval₂ (f.comp (subring.subtype (subring.closure (p.frange : set R)))) x p.restriction := begin simp only [eval₂_eq_sum, sum, support_restriction, ←@coeff_restriction _ _ p], refl, end section to_subring variables (p : R[X]) (T : subring R) /-- Given a polynomial `p` and a subring `T` that contains the coefficients of `p`, return the corresponding polynomial whose coefficients are in `T`. -/ def to_subring (hp : (↑p.frange : set R) ⊆ T) : T[X] := ∑ i in p.support, monomial i (⟨p.coeff i, if H : p.coeff i = 0 then H.symm ▸ T.zero_mem else hp (p.coeff_mem_frange _ H)⟩ : T) variables (hp : (↑p.frange : set R) ⊆ T) include hp @[simp] theorem coeff_to_subring {n : ℕ} : ↑(coeff (to_subring p T hp) n) = coeff p n := begin simp only [to_subring, coeff_monomial, finset_sum_coeff, mem_support_iff, finset.sum_ite_eq', ne.def, ite_not], split_ifs, { rw h, refl }, { refl } end @[simp] theorem coeff_to_subring' {n : ℕ} : (coeff (to_subring p T hp) n).1 = coeff p n := coeff_to_subring _ _ hp @[simp] lemma support_to_subring : support (to_subring p T hp) = support p := begin ext i, simp only [mem_support_iff, not_iff_not, ne.def], conv_rhs { rw [← coeff_to_subring p T hp] }, exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩ end @[simp] theorem degree_to_subring : (to_subring p T hp).degree = p.degree := by simp [degree] @[simp] theorem nat_degree_to_subring : (to_subring p T hp).nat_degree = p.nat_degree := by simp [nat_degree] @[simp] theorem monic_to_subring : monic (to_subring p T hp) ↔ monic p := begin simp_rw [monic, leading_coeff, nat_degree_to_subring, ← coeff_to_subring p T hp], exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩ end omit hp @[simp] theorem to_subring_zero : to_subring (0 : R[X]) T (by simp [frange_zero]) = 0 := by { ext i, simp } @[simp] theorem to_subring_one : to_subring (1 : R[X]) T (set.subset.trans frange_one $finset.singleton_subset_set_iff.2 T.one_mem) = 1 := ext $ λ i, subtype.eq $ by rw [coeff_to_subring', coeff_one, coeff_one]; split_ifs; refl @[simp] theorem map_to_subring : (p.to_subring T hp).map (subring.subtype T) = p := by { ext n, simp [coeff_map] } end to_subring variables (T : subring R) /-- Given a polynomial whose coefficients are in some subring, return the corresponding polynomial whose coefficients are in the ambient ring. -/ def of_subring (p : T[X]) : R[X] := ∑ i in p.support, monomial i (p.coeff i : R) lemma coeff_of_subring (p : T[X]) (n : ℕ) : coeff (of_subring T p) n = (coeff p n : T) := begin simp only [of_subring, coeff_monomial, finset_sum_coeff, mem_support_iff, finset.sum_ite_eq', ite_eq_right_iff, ne.def, ite_not, not_not, ite_eq_left_iff], assume h, rw h, refl end @[simp] theorem frange_of_subring {p : T[X]} : (↑(p.of_subring T).frange : set R) ⊆ T := begin assume i hi, simp only [frange, set.mem_image, mem_support_iff, ne.def, finset.mem_coe, finset.coe_image] at hi, rcases hi with ⟨n, hn, h'n⟩, rw [← h'n, coeff_of_subring], exact subtype.mem (coeff p n : T) end end ring section comm_ring variables [comm_ring R] section mod_by_monic variables {q : R[X]} lemma mem_ker_mod_by_monic (hq : q.monic) {p : R[X]} : p ∈ (mod_by_monic_hom q).ker ↔ q ∣ p := linear_map.mem_ker.trans (dvd_iff_mod_by_monic_eq_zero hq) @[simp] lemma ker_mod_by_monic_hom (hq : q.monic) : (polynomial.mod_by_monic_hom q).ker = (ideal.span {q}).restrict_scalars R := submodule.ext (λ f, (mem_ker_mod_by_monic hq).trans ideal.mem_span_singleton.symm) end mod_by_monic end comm_ring end polynomial namespace ideal open polynomial section semiring variables [semiring R] /-- Transport an ideal of `R[X]` to an `R`-submodule of `R[X]`. -/ def of_polynomial (I : ideal R[X]) : submodule R R[X] := { carrier := I.carrier, zero_mem' := I.zero_mem, add_mem' := λ _ _, I.add_mem, smul_mem' := λ c x H, by { rw [← C_mul'], exact I.mul_mem_left _ H } } variables {I : ideal R[X]} theorem mem_of_polynomial (x) : x ∈ I.of_polynomial ↔ x ∈ I := iff.rfl variables (I) /-- Given an ideal `I` of `R[X]`, make the `R`-submodule of `I` consisting of polynomials of degree ≤ `n`. -/ def degree_le (n : with_bot ℕ) : submodule R R[X] := degree_le R n ⊓ I.of_polynomial /-- Given an ideal `I` of `R[X]`, make the ideal in `R` of leading coefficients of polynomials in `I` with degree ≤ `n`. -/ def leading_coeff_nth (n : ℕ) : ideal R := (I.degree_le n).map $ lcoeff R n /-- Given an ideal `I` in `R[X]`, make the ideal in `R` of the leading coefficients in `I`. -/ def leading_coeff : ideal R := ⨆ n : ℕ, I.leading_coeff_nth n end semiring section comm_semiring variables [comm_semiring R] [semiring S] /-- If every coefficient of a polynomial is in an ideal `I`, then so is the polynomial itself -/ lemma polynomial_mem_ideal_of_coeff_mem_ideal (I : ideal R[X]) (p : R[X]) (hp : ∀ (n : ℕ), (p.coeff n) ∈ I.comap (C : R →+* R[X])) : p ∈ I := sum_C_mul_X_pow_eq p ▸ submodule.sum_mem I (λ n hn, I.mul_mem_right _ (hp n)) /-- The push-forward of an ideal `I` of `R` to `R[X]` via inclusion is exactly the set of polynomials whose coefficients are in `I` -/ theorem mem_map_C_iff {I : ideal R} {f : R[X]} : f ∈ (ideal.map (C : R →+* R[X]) I : ideal R[X]) ↔ ∀ n : ℕ, f.coeff n ∈ I := begin split, { intros hf, apply submodule.span_induction hf, { intros f hf n, cases (set.mem_image _ _ _).mp hf with x hx, rw [← hx.right, coeff_C], by_cases (n = 0), { simpa [h] using hx.left }, { simp [h] } }, { simp }, { exact λ f g hf hg n, by simp [I.add_mem (hf n) (hg n)] }, { refine λ f g hg n, _, rw [smul_eq_mul, coeff_mul], exact I.sum_mem (λ c hc, I.mul_mem_left (f.coeff c.fst) (hg c.snd)) } }, { intros hf, rw ← sum_monomial_eq f, refine (I.map C : ideal R[X]).sum_mem (λ n hn, _), simp [← C_mul_X_pow_eq_monomial], rw mul_comm, exact (I.map C : ideal R[X]).mul_mem_left _ (mem_map_of_mem _ (hf n)) } end lemma _root_.polynomial.ker_map_ring_hom (f : R →+* S) : (polynomial.map_ring_hom f).ker = f.ker.map (C : R →+* R[X]) := begin ext, rw [mem_map_C_iff, ring_hom.mem_ker, polynomial.ext_iff], simp_rw [coe_map_ring_hom, coeff_map, coeff_zero, ring_hom.mem_ker], end variable (I : ideal R[X]) theorem mem_leading_coeff_nth (n : ℕ) (x) : x ∈ I.leading_coeff_nth n ↔ ∃ p ∈ I, degree p ≤ n ∧ p.leading_coeff = x := begin simp only [leading_coeff_nth, degree_le, submodule.mem_map, lcoeff_apply, submodule.mem_inf, mem_degree_le], split, { rintro ⟨p, ⟨hpdeg, hpI⟩, rfl⟩, cases lt_or_eq_of_le hpdeg with hpdeg hpdeg, { refine ⟨0, I.zero_mem, bot_le, _⟩, rw [leading_coeff_zero, eq_comm], exact coeff_eq_zero_of_degree_lt hpdeg }, { refine ⟨p, hpI, le_of_eq hpdeg, _⟩, rw [polynomial.leading_coeff, nat_degree, hpdeg], refl } }, { rintro ⟨p, hpI, hpdeg, rfl⟩, have : nat_degree p + (n - nat_degree p) = n, { exact add_tsub_cancel_of_le (nat_degree_le_of_degree_le hpdeg) }, refine ⟨p * X ^ (n - nat_degree p), ⟨_, I.mul_mem_right _ hpI⟩, _⟩, { apply le_trans (degree_mul_le _ _) _, apply le_trans (add_le_add (degree_le_nat_degree) (degree_X_pow_le _)) _, rw [← with_bot.coe_add, this], exact le_rfl }, { rw [polynomial.leading_coeff, ← coeff_mul_X_pow p (n - nat_degree p), this] } } end theorem mem_leading_coeff_nth_zero (x) : x ∈ I.leading_coeff_nth 0 ↔ C x ∈ I := (mem_leading_coeff_nth _ _ _).trans ⟨λ ⟨p, hpI, hpdeg, hpx⟩, by rwa [← hpx, polynomial.leading_coeff, nat.eq_zero_of_le_zero (nat_degree_le_of_degree_le hpdeg), ← eq_C_of_degree_le_zero hpdeg], λ hx, ⟨C x, hx, degree_C_le, leading_coeff_C x⟩⟩ theorem leading_coeff_nth_mono {m n : ℕ} (H : m ≤ n) : I.leading_coeff_nth m ≤ I.leading_coeff_nth n := begin intros r hr, simp only [set_like.mem_coe, mem_leading_coeff_nth] at hr ⊢, rcases hr with ⟨p, hpI, hpdeg, rfl⟩, refine ⟨p * X ^ (n - m), I.mul_mem_right _ hpI, _, leading_coeff_mul_X_pow⟩, refine le_trans (degree_mul_le _ _) _, refine le_trans (add_le_add hpdeg (degree_X_pow_le _)) _, rw [← with_bot.coe_add, add_tsub_cancel_of_le H], exact le_rfl end theorem mem_leading_coeff (x) : x ∈ I.leading_coeff ↔ ∃ p ∈ I, polynomial.leading_coeff p = x := begin rw [leading_coeff, submodule.mem_supr_of_directed], simp only [mem_leading_coeff_nth], { split, { rintro ⟨i, p, hpI, hpdeg, rfl⟩, exact ⟨p, hpI, rfl⟩ }, rintro ⟨p, hpI, rfl⟩, exact ⟨nat_degree p, p, hpI, degree_le_nat_degree, rfl⟩ }, intros i j, exact ⟨i + j, I.leading_coeff_nth_mono (nat.le_add_right _ _), I.leading_coeff_nth_mono (nat.le_add_left _ _)⟩ end /-- If `I` is an ideal, and `pᵢ` is a finite family of polynomials each satisfying `∀ k, (pᵢ)ₖ ∈ Iⁿⁱ⁻ᵏ` for some `nᵢ`, then `p = ∏ pᵢ` also satisfies `∀ k, pₖ ∈ Iⁿ⁻ᵏ` with `n = ∑ nᵢ`. -/ lemma _root_.polynomial.coeff_prod_mem_ideal_pow_tsub {ι : Type*} (s : finset ι) (f : ι → R[X]) (I : ideal R) (n : ι → ℕ) (h : ∀ (i ∈ s) k, (f i).coeff k ∈ I ^ (n i - k)) (k : ℕ) : (s.prod f).coeff k ∈ I ^ (s.sum n - k) := begin classical, induction s using finset.induction with a s ha hs generalizing k, { rw [sum_empty, prod_empty, coeff_one, zero_tsub, pow_zero, ideal.one_eq_top], exact submodule.mem_top }, { rw [sum_insert ha, prod_insert ha, coeff_mul], apply sum_mem, rintro ⟨i, j⟩ e, obtain rfl : i + j = k := nat.mem_antidiagonal.mp e, apply ideal.pow_le_pow add_tsub_add_le_tsub_add_tsub, rw pow_add, exact ideal.mul_mem_mul (h _ (finset.mem_insert.mpr $ or.inl rfl) _) (hs (λ i hi k, h _ (finset.mem_insert.mpr $ or.inr hi) _) j) } end end comm_semiring section ring variables [ring R] /-- `R[X]` is never a field for any ring `R`. -/ lemma polynomial_not_is_field : ¬ is_field R[X] := begin nontriviality R, intro hR, obtain ⟨p, hp⟩ := hR.mul_inv_cancel X_ne_zero, have hp0 : p ≠ 0, { rintro rfl, rw [mul_zero] at hp, exact zero_ne_one hp }, have := degree_lt_degree_mul_X hp0, rw [←X_mul, congr_arg degree hp, degree_one, nat.with_bot.lt_zero_iff, degree_eq_bot] at this, exact hp0 this, end /-- The only constant in a maximal ideal over a field is `0`. -/ lemma eq_zero_of_constant_mem_of_maximal (hR : is_field R) (I : ideal R[X]) [hI : I.is_maximal] (x : R) (hx : C x ∈ I) : x = 0 := begin refine classical.by_contradiction (λ hx0, hI.ne_top ((eq_top_iff_one I).2 _)), obtain ⟨y, hy⟩ := hR.mul_inv_cancel hx0, convert I.mul_mem_left (C y) hx, rw [← C.map_mul, hR.mul_comm y x, hy, ring_hom.map_one], end end ring section comm_ring variables [comm_ring R] /-- If `P` is a prime ideal of `R`, then `P.R[x]` is a prime ideal of `R[x]`. -/ lemma is_prime_map_C_iff_is_prime (P : ideal R) : is_prime (map (C : R →+* R[X]) P : ideal R[X]) ↔ is_prime P := begin -- Porting note: the following proof avoids quotient rings -- It can be golfed substantially by using something like -- `(quotient.is_domain_iff_prime (map C P : ideal R[X]))` split, { intro H, have := @comap_is_prime R R[X] (R →+* R[X]) _ _ _ C (map C P) H, convert this using 1, ext x, simp only [mem_comap, mem_map_C_iff], split, { rintro h (-|n), { simpa only [coeff_C_zero] using h }, { simp only [coeff_C_ne_zero (nat.succ_ne_zero _), submodule.zero_mem] } }, { intro h, simpa only [coeff_C_zero] using h 0 } }, { intro h, constructor, { rw [ne.def, eq_top_iff_one, mem_map_C_iff, not_forall], use 0, rw [coeff_one_zero, ← eq_top_iff_one], exact h.1 }, { intros f g, simp only [mem_map_C_iff], contrapose!, rintro ⟨hf, hg⟩, classical, let m := nat.find hf, let n := nat.find hg, refine ⟨m + n, _⟩, rw [coeff_mul, ← finset.insert_erase ((@finset.nat.mem_antidiagonal _ (m,n)).mpr rfl), finset.sum_insert (finset.not_mem_erase _ _), (P.add_mem_iff_left _).not], { apply mt h.2, rw [not_or_distrib], exact ⟨nat.find_spec hf, nat.find_spec hg⟩ }, apply P.sum_mem, rintro ⟨i, j⟩ hij, rw [finset.mem_erase, finset.nat.mem_antidiagonal] at hij, simp only [ne.def, prod.mk.inj_iff, not_and_distrib] at hij, obtain (hi|hj) : i < m ∨ j < n, { rw [or_iff_not_imp_left, not_lt, le_iff_lt_or_eq], rintro (hmi|rfl), { rw [← not_le], intro hnj, exact (add_lt_add_of_lt_of_le hmi hnj).ne hij.2.symm, }, { simpa only [eq_self_iff_true, not_true, false_or, add_right_inj, not_and_self] using hij, } }, { rw [mul_comm], apply P.mul_mem_left, exact not_not.1 (nat.find_min hf hi) }, { apply P.mul_mem_left, exact not_not.1 (nat.find_min hg hj) } } } end /-- If `P` is a prime ideal of `R`, then `P.R[x]` is a prime ideal of `R[x]`. -/ lemma is_prime_map_C_of_is_prime {P : ideal R} (H : is_prime P) : is_prime (map (C : R →+* R[X]) P : ideal R[X]) := (is_prime_map_C_iff_is_prime P).mpr H theorem is_fg_degree_le [is_noetherian_ring R] (I : ideal R[X]) (n : ℕ) : submodule.fg (I.degree_le n) := is_noetherian_submodule_left.1 (is_noetherian_of_fg_of_noetherian _ ⟨_, degree_le_eq_span_X_pow.symm⟩) _ end comm_ring end ideal variables {σ : Type v} {M : Type w} variables [comm_ring R] [comm_ring S] [add_comm_group M] [module R M] section prime variables (σ) {r : R} namespace polynomial lemma prime_C_iff : prime (C r) ↔ prime r := ⟨ comap_prime C (eval_ring_hom (0 : R)) (λ r, eval_C), λ hr, by { have := hr.1, rw ← ideal.span_singleton_prime at hr ⊢, { convert ideal.is_prime_map_C_of_is_prime hr using 1, rw [ideal.map_span, set.image_singleton] }, exacts [λ h, this (C_eq_zero.1 h), this] } ⟩ end polynomial namespace mv_polynomial private lemma prime_C_iff_of_fintype [fintype σ] : prime (C r : mv_polynomial σ R) ↔ prime r := begin rw (rename_equiv R (fintype.equiv_fin σ)).to_mul_equiv.prime_iff, convert_to prime (C r) ↔ _, { congr, apply rename_C }, { symmetry, induction fintype.card σ with d hd, { exact (is_empty_alg_equiv R (fin 0)).to_mul_equiv.symm.prime_iff }, { rw [hd, ← polynomial.prime_C_iff], convert (fin_succ_equiv R d).to_mul_equiv.symm.prime_iff, rw ← fin_succ_equiv_comp_C_eq_C, refl } }, end lemma prime_C_iff : prime (C r : mv_polynomial σ R) ↔ prime r := ⟨ comap_prime C constant_coeff (constant_coeff_C _), λ hr, ⟨ λ h, hr.1 $ by { rw [← C_inj, h], simp }, λ h, hr.2.1 $ by { rw ← constant_coeff_C _ r, exact h.map _ }, λ a b hd, begin obtain ⟨s,a',b',rfl,rfl⟩ := exists_finset_rename₂ a b, rw ← algebra_map_eq at hd, have : algebra_map R _ r ∣ a' * b', { convert (kill_compl subtype.coe_injective).to_ring_hom.map_dvd hd, simpa, simp }, rw ← rename_C (coe : s → σ), let f := (rename (coe : s → σ)).to_ring_hom, exact (((prime_C_iff_of_fintype s).2 hr).2.2 a' b' this).imp f.map_dvd f.map_dvd, end ⟩ ⟩ variable {σ} lemma prime_rename_iff (s : set σ) {p : mv_polynomial s R} : prime (rename (coe : s → σ) p) ↔ prime p := begin classical, symmetry, let eqv := (sum_alg_equiv R _ _).symm.trans (rename_equiv R $ (equiv.sum_comm ↥sᶜ s).trans $ equiv.set.sum_compl s), rw [← prime_C_iff ↥sᶜ, eqv.to_mul_equiv.prime_iff], convert iff.rfl, suffices : (rename coe).to_ring_hom = eqv.to_alg_hom.to_ring_hom.comp C, { apply ring_hom.congr_fun this }, { apply ring_hom_ext, { intro, dsimp [eqv], erw [iter_to_sum_C_C, rename_C, rename_C] }, { intro, dsimp [eqv], erw [iter_to_sum_C_X, rename_X, rename_X], refl } }, end end mv_polynomial end prime namespace polynomial @[priority 100] instance {R : Type*} [comm_ring R] [is_domain R] [wf_dvd_monoid R] : wf_dvd_monoid R[X] := { well_founded_dvd_not_unit := begin classical, refine rel_hom_class.well_founded (⟨λ (p : R[X]), ((if p = 0 then ⊤ else ↑p.degree : with_top (with_bot ℕ)), p.leading_coeff), _⟩ : dvd_not_unit →r prod.lex (<) dvd_not_unit) (prod.lex_wf (with_top.well_founded_lt $ with_bot.well_founded_lt nat.lt_wf) ‹wf_dvd_monoid R›.well_founded_dvd_not_unit), rintros a b ⟨ane0, ⟨c, ⟨not_unit_c, rfl⟩⟩⟩, rw [polynomial.degree_mul, if_neg ane0], split_ifs with hac, { rw [hac, polynomial.leading_coeff_zero], apply prod.lex.left, exact lt_of_le_of_ne le_top with_top.coe_ne_top }, have cne0 : c ≠ 0 := right_ne_zero_of_mul hac, simp only [cne0, ane0, polynomial.leading_coeff_mul], by_cases hdeg : c.degree = 0, { simp only [hdeg, add_zero], refine prod.lex.right _ ⟨_, ⟨c.leading_coeff, (λ unit_c, not_unit_c _), rfl⟩⟩, { rwa [ne, polynomial.leading_coeff_eq_zero] }, rw [polynomial.is_unit_iff, polynomial.eq_C_of_degree_eq_zero hdeg], use [c.leading_coeff, unit_c], rw [polynomial.leading_coeff, polynomial.nat_degree_eq_of_degree_eq_some hdeg] }, { apply prod.lex.left, rw polynomial.degree_eq_nat_degree cne0 at *, rw [with_top.coe_lt_coe, polynomial.degree_eq_nat_degree ane0, ← with_bot.coe_add, with_bot.coe_lt_coe], exact lt_add_of_pos_right _ (nat.pos_of_ne_zero (λ h, hdeg (h.symm ▸ with_bot.coe_zero))) }, end } end polynomial /-- Hilbert basis theorem: a polynomial ring over a noetherian ring is a noetherian ring. -/ protected theorem polynomial.is_noetherian_ring [is_noetherian_ring R] : is_noetherian_ring R[X] := is_noetherian_ring_iff.2 ⟨assume I : ideal R[X], let M := well_founded.min (is_noetherian_iff_well_founded.1 (by apply_instance)) (set.range I.leading_coeff_nth) ⟨_, ⟨0, rfl⟩⟩ in have hm : M ∈ set.range I.leading_coeff_nth := well_founded.min_mem _ _ _, let ⟨N, HN⟩ := hm, ⟨s, hs⟩ := I.is_fg_degree_le N in have hm2 : ∀ k, I.leading_coeff_nth k ≤ M := λ k, or.cases_on (le_or_lt k N) (λ h, HN ▸ I.leading_coeff_nth_mono h) (λ h x hx, classical.by_contradiction $ λ hxm, have ¬M < I.leading_coeff_nth k, by refine well_founded.not_lt_min (well_founded_submodule_gt _ _) _ _ _; exact ⟨k, rfl⟩, this ⟨HN ▸ I.leading_coeff_nth_mono (le_of_lt h), λ H, hxm (H hx)⟩), have hs2 : ∀ {x}, x ∈ I.degree_le N → x ∈ ideal.span (↑s : set R[X]), from hs ▸ λ x hx, submodule.span_induction hx (λ _ hx, ideal.subset_span hx) (ideal.zero_mem _) (λ _ _, ideal.add_mem _) (λ c f hf, f.C_mul' c ▸ ideal.mul_mem_left _ _ hf), ⟨s, le_antisymm (ideal.span_le.2 $ λ x hx, have x ∈ I.degree_le N, from hs ▸ submodule.subset_span hx, this.2) $ begin have : submodule.span R[X] ↑s = ideal.span ↑s, by refl, rw this, intros p hp, generalize hn : p.nat_degree = k, induction k using nat.strong_induction_on with k ih generalizing p, cases le_or_lt k N, { subst k, refine hs2 ⟨polynomial.mem_degree_le.2 (le_trans polynomial.degree_le_nat_degree $ with_bot.coe_le_coe.2 h), hp⟩ }, { have hp0 : p ≠ 0, { rintro rfl, cases hn, exact nat.not_lt_zero _ h }, have : (0 : R) ≠ 1, { intro h, apply hp0, ext i, refine (mul_one _).symm.trans _, rw [← h, mul_zero], refl }, haveI : nontrivial R := ⟨⟨0, 1, this⟩⟩, have : p.leading_coeff ∈ I.leading_coeff_nth N, { rw HN, exact hm2 k ((I.mem_leading_coeff_nth _ _).2 ⟨_, hp, hn ▸ polynomial.degree_le_nat_degree, rfl⟩) }, rw I.mem_leading_coeff_nth at this, rcases this with ⟨q, hq, hdq, hlqp⟩, have hq0 : q ≠ 0, { intro H, rw [← polynomial.leading_coeff_eq_zero] at H, rw [hlqp, polynomial.leading_coeff_eq_zero] at H, exact hp0 H }, have h1 : p.degree = (q * polynomial.X ^ (k - q.nat_degree)).degree, { rw [polynomial.degree_mul', polynomial.degree_X_pow], rw [polynomial.degree_eq_nat_degree hp0, polynomial.degree_eq_nat_degree hq0], rw [← with_bot.coe_add, add_tsub_cancel_of_le, hn], { refine le_trans (polynomial.nat_degree_le_of_degree_le hdq) (le_of_lt h) }, rw [polynomial.leading_coeff_X_pow, mul_one], exact mt polynomial.leading_coeff_eq_zero.1 hq0 }, have h2 : p.leading_coeff = (q * polynomial.X ^ (k - q.nat_degree)).leading_coeff, { rw [← hlqp, polynomial.leading_coeff_mul_X_pow] }, have := polynomial.degree_sub_lt h1 hp0 h2, rw [polynomial.degree_eq_nat_degree hp0] at this, rw ← sub_add_cancel p (q * polynomial.X ^ (k - q.nat_degree)), refine (ideal.span ↑s).add_mem _ ((ideal.span ↑s).mul_mem_right _ _), { by_cases hpq : p - q * polynomial.X ^ (k - q.nat_degree) = 0, { rw hpq, exact ideal.zero_mem _ }, refine ih _ _ (I.sub_mem hp (I.mul_mem_right _ hq)) rfl, rwa [polynomial.degree_eq_nat_degree hpq, with_bot.coe_lt_coe, hn] at this }, exact hs2 ⟨polynomial.mem_degree_le.2 hdq, hq⟩ } end⟩⟩ attribute [instance] polynomial.is_noetherian_ring namespace polynomial theorem exists_irreducible_of_degree_pos {R : Type u} [comm_ring R] [is_domain R] [wf_dvd_monoid R] {f : R[X]} (hf : 0 < f.degree) : ∃ g, irreducible g ∧ g ∣ f := wf_dvd_monoid.exists_irreducible_factor (λ huf, ne_of_gt hf $ degree_eq_zero_of_is_unit huf) (λ hf0, not_lt_of_lt hf $ hf0.symm ▸ (@degree_zero R _).symm ▸ with_bot.bot_lt_coe _) theorem exists_irreducible_of_nat_degree_pos {R : Type u} [comm_ring R] [is_domain R] [wf_dvd_monoid R] {f : R[X]} (hf : 0 < f.nat_degree) : ∃ g, irreducible g ∧ g ∣ f := exists_irreducible_of_degree_pos $ by { contrapose! hf, exact nat_degree_le_of_degree_le hf } theorem exists_irreducible_of_nat_degree_ne_zero {R : Type u} [comm_ring R] [is_domain R] [wf_dvd_monoid R] {f : R[X]} (hf : f.nat_degree ≠ 0) : ∃ g, irreducible g ∧ g ∣ f := exists_irreducible_of_nat_degree_pos $ nat.pos_of_ne_zero hf lemma linear_independent_powers_iff_aeval (f : M →ₗ[R] M) (v : M) : linear_independent R (λ n : ℕ, (f ^ n) v) ↔ ∀ (p : R[X]), aeval f p v = 0 → p = 0 := begin rw linear_independent_iff, simp only [finsupp.total_apply, aeval_endomorphism, forall_iff_forall_finsupp, sum, support, coeff, of_finsupp_eq_zero], exact iff.rfl, end lemma disjoint_ker_aeval_of_coprime (f : M →ₗ[R] M) {p q : R[X]} (hpq : is_coprime p q) : disjoint (aeval f p).ker (aeval f q).ker := begin rw disjoint_iff_inf_le, intros v hv, rcases hpq with ⟨p', q', hpq'⟩, simpa [linear_map.mem_ker.1 (submodule.mem_inf.1 hv).1, linear_map.mem_ker.1 (submodule.mem_inf.1 hv).2] using congr_arg (λ p : R[X], aeval f p v) hpq'.symm, end lemma sup_aeval_range_eq_top_of_coprime (f : M →ₗ[R] M) {p q : R[X]} (hpq : is_coprime p q) : (aeval f p).range ⊔ (aeval f q).range = ⊤ := begin rw eq_top_iff, intros v hv, rw submodule.mem_sup, rcases hpq with ⟨p', q', hpq'⟩, use aeval f (p * p') v, use linear_map.mem_range.2 ⟨aeval f p' v, by simp only [linear_map.mul_apply, aeval_mul]⟩, use aeval f (q * q') v, use linear_map.mem_range.2 ⟨aeval f q' v, by simp only [linear_map.mul_apply, aeval_mul]⟩, simpa only [mul_comm p p', mul_comm q q', aeval_one, aeval_add] using congr_arg (λ p : R[X], aeval f p v) hpq' end lemma sup_ker_aeval_le_ker_aeval_mul {f : M →ₗ[R] M} {p q : R[X]} : (aeval f p).ker ⊔ (aeval f q).ker ≤ (aeval f (p * q)).ker := begin intros v hv, rcases submodule.mem_sup.1 hv with ⟨x, hx, y, hy, hxy⟩, have h_eval_x : aeval f (p * q) x = 0, { rw [mul_comm, aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hx, linear_map.map_zero] }, have h_eval_y : aeval f (p * q) y = 0, { rw [aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hy, linear_map.map_zero] }, rw [linear_map.mem_ker, ←hxy, linear_map.map_add, h_eval_x, h_eval_y, add_zero], end lemma sup_ker_aeval_eq_ker_aeval_mul_of_coprime (f : M →ₗ[R] M) {p q : R[X]} (hpq : is_coprime p q) : (aeval f p).ker ⊔ (aeval f q).ker = (aeval f (p * q)).ker := begin apply le_antisymm sup_ker_aeval_le_ker_aeval_mul, intros v hv, rw submodule.mem_sup, rcases hpq with ⟨p', q', hpq'⟩, have h_eval₂_qpp' := calc aeval f (q * (p * p')) v = aeval f (p' * (p * q)) v : by rw [mul_comm, mul_assoc, mul_comm, mul_assoc, mul_comm q p] ... = 0 : by rw [aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hv, linear_map.map_zero], have h_eval₂_pqq' := calc aeval f (p * (q * q')) v = aeval f (q' * (p * q)) v : by rw [←mul_assoc, mul_comm] ... = 0 : by rw [aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hv, linear_map.map_zero], rw aeval_mul at h_eval₂_qpp' h_eval₂_pqq', refine ⟨aeval f (q * q') v, linear_map.mem_ker.1 h_eval₂_pqq', aeval f (p * p') v, linear_map.mem_ker.1 h_eval₂_qpp', _⟩, rw [add_comm, mul_comm p p', mul_comm q q'], simpa only [map_add, map_mul, aeval_one] using congr_arg (λ p : R[X], aeval f p v) hpq' end end polynomial namespace mv_polynomial lemma is_noetherian_ring_fin_0 [is_noetherian_ring R] : is_noetherian_ring (mv_polynomial (fin 0) R) := is_noetherian_ring_of_ring_equiv R ((mv_polynomial.is_empty_ring_equiv R pempty).symm.trans (rename_equiv R fin_zero_equiv'.symm).to_ring_equiv) theorem is_noetherian_ring_fin [is_noetherian_ring R] : ∀ {n : ℕ}, is_noetherian_ring (mv_polynomial (fin n) R) | 0 := is_noetherian_ring_fin_0 | (n+1) := @is_noetherian_ring_of_ring_equiv (polynomial (mv_polynomial (fin n) R)) _ _ _ (mv_polynomial.fin_succ_equiv _ n).to_ring_equiv.symm (@polynomial.is_noetherian_ring (mv_polynomial (fin n) R) _ (is_noetherian_ring_fin)) /-- The multivariate polynomial ring in finitely many variables over a noetherian ring is itself a noetherian ring. -/ instance is_noetherian_ring [finite σ] [is_noetherian_ring R] : is_noetherian_ring (mv_polynomial σ R) := by casesI nonempty_fintype σ; exact @is_noetherian_ring_of_ring_equiv (mv_polynomial (fin (fintype.card σ)) R) _ _ _ (rename_equiv R (fintype.equiv_fin σ).symm).to_ring_equiv is_noetherian_ring_fin /-- Auxiliary lemma: Multivariate polynomials over an integral domain with variables indexed by `fin n` form an integral domain. This fact is proven inductively, and then used to prove the general case without any finiteness hypotheses. See `mv_polynomial.no_zero_divisors` for the general case. -/ lemma no_zero_divisors_fin (R : Type u) [comm_semiring R] [no_zero_divisors R] : ∀ (n : ℕ), no_zero_divisors (mv_polynomial (fin n) R) | 0 := (mv_polynomial.is_empty_alg_equiv R _).injective.no_zero_divisors _ (map_zero _) (map_mul _) | (n+1) := begin haveI := no_zero_divisors_fin n, exact (mv_polynomial.fin_succ_equiv R n).injective.no_zero_divisors _ (map_zero _) (map_mul _) end /-- Auxiliary definition: Multivariate polynomials in finitely many variables over an integral domain form an integral domain. This fact is proven by transport of structure from the `mv_polynomial.no_zero_divisors_fin`, and then used to prove the general case without finiteness hypotheses. See `mv_polynomial.no_zero_divisors` for the general case. -/ lemma no_zero_divisors_of_finite (R : Type u) (σ : Type v) [comm_semiring R] [finite σ] [no_zero_divisors R] : no_zero_divisors (mv_polynomial σ R) := begin casesI nonempty_fintype σ, haveI := no_zero_divisors_fin R (fintype.card σ), exact (rename_equiv R (fintype.equiv_fin σ)).injective.no_zero_divisors _ (map_zero _) (map_mul _) end instance {R : Type u} [comm_semiring R] [no_zero_divisors R] {σ : Type v} : no_zero_divisors (mv_polynomial σ R) := ⟨λ p q h, begin obtain ⟨s, p, rfl⟩ := exists_finset_rename p, obtain ⟨t, q, rfl⟩ := exists_finset_rename q, have : rename (subtype.map id (finset.subset_union_left s t) : {x // x ∈ s} → {x // x ∈ s ∪ t}) p * rename (subtype.map id (finset.subset_union_right s t) : {x // x ∈ t} → {x // x ∈ s ∪ t}) q = 0, { apply rename_injective _ subtype.val_injective, simpa using h }, letI := mv_polynomial.no_zero_divisors_of_finite R {x // x ∈ (s ∪ t)}, rw mul_eq_zero at this, cases this; [left, right], all_goals { simpa using congr_arg (rename subtype.val) this } end⟩ /-- The multivariate polynomial ring over an integral domain is an integral domain. -/ instance {R : Type u} {σ : Type v} [comm_ring R] [is_domain R] : is_domain (mv_polynomial σ R) := begin apply no_zero_divisors.to_is_domain _, exact add_monoid_algebra.nontrivial, exact mv_polynomial.no_zero_divisors end lemma map_mv_polynomial_eq_eval₂ {S : Type*} [comm_ring S] [finite σ] (ϕ : mv_polynomial σ R →+* S) (p : mv_polynomial σ R) : ϕ p = mv_polynomial.eval₂ (ϕ.comp mv_polynomial.C) (λ s, ϕ (mv_polynomial.X s)) p := begin casesI nonempty_fintype σ, refine trans (congr_arg ϕ (mv_polynomial.as_sum p)) _, rw [mv_polynomial.eval₂_eq', ϕ.map_sum], congr, ext, simp only [monomial_eq, ϕ.map_pow, ϕ.map_prod, ϕ.comp_apply, ϕ.map_mul, finsupp.prod_pow], end /-- If every coefficient of a polynomial is in an ideal `I`, then so is the polynomial itself, multivariate version. -/ lemma mem_ideal_of_coeff_mem_ideal (I : ideal (mv_polynomial σ R)) (p : mv_polynomial σ R) (hcoe : ∀ (m : σ →₀ ℕ), p.coeff m ∈ I.comap (C : R →+* mv_polynomial σ R)) : p ∈ I := begin rw as_sum p, suffices : ∀ m ∈ p.support, monomial m (mv_polynomial.coeff m p) ∈ I, { exact submodule.sum_mem I this }, intros m hm, rw [← mul_one (coeff m p), ← C_mul_monomial], suffices : C (coeff m p) ∈ I, { exact I.mul_mem_right (monomial m 1) this }, simpa [ideal.mem_comap] using hcoe m end /-- The push-forward of an ideal `I` of `R` to `mv_polynomial σ R` via inclusion is exactly the set of polynomials whose coefficients are in `I` -/ theorem mem_map_C_iff {I : ideal R} {f : mv_polynomial σ R} : f ∈ (ideal.map (C : R →+* mv_polynomial σ R) I : ideal (mv_polynomial σ R)) ↔ ∀ (m : σ →₀ ℕ), f.coeff m ∈ I := begin split, { intros hf, apply submodule.span_induction hf, { intros f hf n, cases (set.mem_image _ _ _).mp hf with x hx, rw [← hx.right, coeff_C], by_cases (n = 0), { simpa [h] using hx.left }, { simp [ne.symm h] } }, { simp }, { exact λ f g hf hg n, by simp [I.add_mem (hf n) (hg n)] }, { refine λ f g hg n, _, rw [smul_eq_mul, coeff_mul], exact I.sum_mem (λ c hc, I.mul_mem_left (f.coeff c.fst) (hg c.snd)) } }, { intros hf, rw as_sum f, suffices : ∀ m ∈ f.support, monomial m (coeff m f) ∈ (ideal.map C I : ideal (mv_polynomial σ R)), { exact submodule.sum_mem _ this }, intros m hm, rw [← mul_one (coeff m f), ← C_mul_monomial], suffices : C (coeff m f) ∈ (ideal.map C I : ideal (mv_polynomial σ R)), { exact ideal.mul_mem_right _ _ this }, apply ideal.mem_map_of_mem _, exact hf m } end lemma ker_map (f : R →+* S) : (map f : mv_polynomial σ R →+* mv_polynomial σ S).ker = f.ker.map (C : R →+* mv_polynomial σ R) := begin ext, rw [mv_polynomial.mem_map_C_iff, ring_hom.mem_ker, mv_polynomial.ext_iff], simp_rw [coeff_map, coeff_zero, ring_hom.mem_ker], end end mv_polynomial section unique_factorization_domain variables {D : Type u} [comm_ring D] [is_domain D] [unique_factorization_monoid D] (σ) open unique_factorization_monoid namespace polynomial @[priority 100] instance unique_factorization_monoid : unique_factorization_monoid D[X] := begin haveI := arbitrary (normalization_monoid D), haveI := to_normalized_gcd_monoid D, exact ufm_of_gcd_of_wf_dvd_monoid end end polynomial namespace mv_polynomial private lemma unique_factorization_monoid_of_fintype [fintype σ] : unique_factorization_monoid (mv_polynomial σ D) := (rename_equiv D (fintype.equiv_fin σ)).to_mul_equiv.symm.unique_factorization_monoid $ begin induction fintype.card σ with d hd, { apply (is_empty_alg_equiv D (fin 0)).to_mul_equiv.symm.unique_factorization_monoid, apply_instance }, { apply (fin_succ_equiv D d).to_mul_equiv.symm.unique_factorization_monoid, exactI polynomial.unique_factorization_monoid }, end @[priority 100] instance : unique_factorization_monoid (mv_polynomial σ D) := begin rw iff_exists_prime_factors, intros a ha, obtain ⟨s,a',rfl⟩ := exists_finset_rename a, obtain ⟨w,h,u,hw⟩ := iff_exists_prime_factors.1 (unique_factorization_monoid_of_fintype s) a' (λ h, ha $ by simp [h]), exact ⟨ w.map (rename coe), λ b hb, let ⟨b',hb',he⟩ := multiset.mem_map.1 hb in he ▸ (prime_rename_iff ↑s).2 (h b' hb'), units.map (@rename s σ D _ coe).to_ring_hom.to_monoid_hom u, by erw [multiset.prod_hom, ← map_mul, hw] ⟩, end end mv_polynomial end unique_factorization_domain
69e59635ee729a349a0c3f8423d20eb8d8c854c9
aa5a655c05e5359a70646b7154e7cac59f0b4132
/tests/lean/PPRoundtrip.lean
94ad4fefb727bc1d68fe5fecfb32ac212d796247
[ "Apache-2.0" ]
permissive
lambdaxymox/lean4
ae943c960a42247e06eff25c35338268d07454cb
278d47c77270664ef29715faab467feac8a0f446
refs/heads/master
1,677,891,867,340
1,612,500,005,000
1,612,500,005,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,036
lean
import Lean open Lean open Lean.Elab open Lean.Elab.Term open Lean.Elab.Command open Std.Format open Std open Lean.PrettyPrinter open Lean.Meta def checkM (stx : TermElabM Syntax) (optionsPerPos : OptionsPerPos := {}) : TermElabM Unit := do let opts ← getOptions let stx ← stx let e ← elabTermAndSynthesize stx none <* throwErrorIfErrors let stx' ← delab Name.anonymous [] e optionsPerPos let f' ← PrettyPrinter.ppTerm stx' let s := f'.pretty' opts IO.println s let env ← getEnv match Parser.runParserCategory env `term s "<input>" with | Except.error e => throwErrorAt stx e | Except.ok stx'' => do let e' ← elabTermAndSynthesize stx'' none <* throwErrorIfErrors unless (← isDefEq e e') do throwErrorAt stx (m!"failed to round-trip" ++ line ++ fmt e ++ line ++ fmt e') -- set_option trace.PrettyPrinter.parenthesize true set_option format.width 20 -- #eval checkM `(?m) -- fails round-trip #eval checkM `(Sort) #eval checkM `(Type) #eval checkM `(Type 0) #eval checkM `(Type 1) -- TODO: we need support for parsing `?u` to roundtrip the terms containing universe metavariables. Just pretty printing them as `_` is bad for error and trace messages -- #eval checkM `(Type _) -- #eval checkM `(Type (_ + 2)) #eval checkM `(Nat) #eval checkM `(List Nat) #eval checkM `(id Nat) #eval checkM `(id (id (id Nat))) section set_option pp.explicit true #eval checkM `(List Nat) #eval checkM `(id Nat) end section set_option pp.universes true #eval checkM `(List Nat) #eval checkM `(id Nat) #eval checkM `(Sum Nat Nat) end #eval checkM `(id (id Nat)) (Std.RBMap.empty.insert 4 $ KVMap.empty.insert `pp.explicit true) -- specify the expected type of `a` in a way that is not erased by the delaborator def typeAs.{u} (α : Type u) (a : α) := () #eval checkM `(fun (a : Nat) => a) #eval checkM `(fun (a b : Nat) => a) #eval checkM `(fun (a : Nat) (b : Bool) => a) #eval checkM `(fun {a b : Nat} => a) -- implicit lambdas work as long as the expected type is preserved #eval checkM `(typeAs ({α : Type} → (a : α) → α) fun a => a) section set_option pp.explicit true #eval checkM `(fun {α : Type} [ToString α] (a : α) => toString a) end #eval checkM `((α : Type) → α) #eval checkM `((α β : Type) → α) -- group #eval checkM `((α β : Type) → Type) -- don't group #eval checkM `((α : Type) → (a : α) → α) #eval checkM `({α : Type} → α) #eval checkM `({α : Type} → [ToString α] → α) #eval checkM `(∀ x : Nat, x = x) #eval checkM `(∀ {x : Nat} [ToString Nat], x = x) set_option pp.binder_types false in #eval checkM `(∀ x : Nat, x = x) -- TODO: hide `ofNat` #eval checkM `(0) #eval checkM `(1) #eval checkM `(42) #eval checkM `("hi") set_option pp.structure_instance_type true in #eval checkM `({ type := Nat, val := 0 : PointedType }) #eval checkM `((1,2,3)) #eval checkM `((1,2).fst) #eval checkM `(1 < 2 || true) #eval checkM `(id (fun a => a) 0) #eval checkM `(typeAs Nat (do let x ← pure 1 discard <| pure 2 let y := 3 return x + y))
1eb0ea1c31b4cdeb66d309908acff70c47acde88
e5c11e5a7d990ce404047c2bd848eeafac3c0a85
/src/dedekind_domain.lean
6a9e1a3cbf909680f75610cf513dcb02c46d214b
[ "LPPL-1.3c" ]
permissive
lean-forward/class-number
9ec63c24845e46efc8fa8b15324d0815918292c7
4fccf36d5e0e16accae84c16df77a3839ad964e4
refs/heads/main
1,686,927,014,542
1,624,886,724,000
1,624,886,724,000
327,319,245
2
0
null
null
null
null
UTF-8
Lean
false
false
41,587
lean
/- Copyright (c) 2020 Kenji Nakagawa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenji Nakagawa, Anne Baanen, Filippo A. E. Nuccio, Ashvni Narayanan -/ import algebra.group_with_zero.basic import field_theory.minpoly import linear_algebra.finite_dimensional import logic.function.basic import order.zorn import ring_theory.adjoin_root import ring_theory.discrete_valuation_ring import ring_theory.fractional_ideal import ring_theory.ideal.over import ring_theory.polynomial.rational_root import ring_theory.power_basis import ring_theory.trace import set_theory.cardinal import tactic /-! # Dedekind domains This file defines the notion of a Dedekind domain (or Dedekind ring), giving three equivalent definitions (TODO: and shows that they are equivalent). We have now shown one side of the equivalence two of these definitions. ## Main definitions - `is_dedekind_domain` defines a Dedekind domain as an integral domain that is Noetherian, integrally closed in its field of fractions and has Krull dimension exactly one. `is_dedekind_domain_iff` shows that this does not depend on the choice of field of fractions. - `is_dedekind_domain_dvr` alternatively defines a Dedekind domain as an integral domain that is Noetherian, and the localization at every nonzero prime ideal is a discrete valuation ring. - `is_dedekind_domain_inv` alternatively defines a Dedekind domain as an integral domain that is and every nonzero fractional ideal is invertible. - `is_dedekind_domain_inv_iff` shows that this does not depend on the choice of field of fractions. ## Main results - `ideal.unique_factorization_monoid`: we have unique factorization of ideals into prime ideals ## Implementation notes The definitions that involve a field of fractions choose a canonical field of fractions, but are independent of that choice. The `..._iff` lemmas express this independence. ## References * [D. Marcus, *Number Fields*][marcus1977number] * [J.W.S. Cassels, A. Frölich, *Algebraic Number Theory*][cassels1967algebraic] * [P. Samuel, *Algebraic Theory of Numbers*][samuel1970algebraic] ## Tags dedekind domain, dedekind ring -/ variables (A K : Type*) [integral_domain A] [field K] /-- A ring `R` has Krull dimension at most one if all nonzero prime ideals are maximal. -/ def ring.dimension_le_one (R : Type*) [comm_ring R] : Prop := ∀ p ≠ (⊥ : ideal R), p.is_prime → p.is_maximal open ideal ring namespace ring lemma dimension_le_one.principal_ideal_ring [is_principal_ideal_ring A] : dimension_le_one A := λ p nonzero prime, by { haveI := prime, exact is_prime.to_maximal_ideal nonzero } lemma dimension_le_one.integral_closure (R : Type*) [comm_ring R] [nontrivial R] [algebra R A] (h : dimension_le_one R) : dimension_le_one (integral_closure R A) := begin intros p ne_bot prime, haveI := prime, refine integral_closure.is_maximal_of_is_maximal_comap p (h _ (integral_closure.comap_ne_bot ne_bot) _), apply is_prime.comap end end ring section variables {A K} lemma fraction_map.is_algebraic_iff {R L : Type*} [integral_domain R] [field L] (f : fraction_map R K) [algebra f.codomain L] [algebra R L] [is_scalar_tower R f.codomain L] {x : L} : is_algebraic f.codomain x ↔ is_algebraic R x := begin split, { rintro ⟨p, p_ne, p_eq⟩, exact ⟨f.integer_normalization p, mt f.integer_normalization_eq_zero_iff.mp p_ne, localization_map.integer_normalization_aeval_eq_zero p p_eq⟩ }, { rintro ⟨p, p_ne, p_eq⟩, refine ⟨p.map f.to_map, _, _⟩, { simpa only [ne.def, polynomial.ext_iff, polynomial.coeff_zero, polynomial.coeff_map, f.to_map_eq_zero_iff] using p_ne }, { simpa only [polynomial.aeval_def, polynomial.eval₂_map, ← f.algebra_map_eq, is_scalar_tower.algebra_map_eq R f.codomain L] using p_eq } }, end end open ring open ring.fractional_ideal /-- A Dedekind domain is an integral domain that is Noetherian, integrally closed, and has Krull dimension at most one. The integral closure condition is independent of the choice of field of fractions: use `is_dedekind_domain_iff` to prove `is_dedekind_domain` for a given `fraction_map`. This is the default implementation, but there are equivalent definitions, `is_dedekind_domain_dvr` and `is_dedekind_domain_inv`. TODO: Prove that these are actually equivalent definitions. -/ class is_dedekind_domain : Prop := (to_is_noetherian_ring : is_noetherian_ring A) (dimension_le_one : dimension_le_one A) (is_integrally_closed : integral_closure A (fraction_ring A) = ⊥) attribute [instance, priority 100] is_dedekind_domain.to_is_noetherian_ring -- see Note [lower instance priority] /-- An integral domain is a Dedekind domain iff and only if it is Noetherian, has dimension ≤ 1, and is integrally closed in a given fraction field. In particular, this definition does not depend on the choice of this fraction field. -/ lemma is_dedekind_domain_iff (f : fraction_map A K) : is_dedekind_domain A ↔ is_noetherian_ring A ∧ dimension_le_one A ∧ integral_closure A f.codomain = ⊥ := ⟨λ ⟨hr, hd, hi⟩, ⟨hr, hd, by rw [←integral_closure_map_alg_equiv (fraction_ring.alg_equiv_of_quotient f), hi, algebra.map_bot]⟩, λ ⟨hr, hd, hi⟩, ⟨hr, hd, by rw [←integral_closure_map_alg_equiv (fraction_ring.alg_equiv_of_quotient f).symm, hi, algebra.map_bot]⟩⟩ section principal_ideal_ring lemma integrally_closed_iff_integral_implies_integer {R K : Type*} [comm_ring R] [comm_ring K] {f : fraction_map R K} : integral_closure R f.codomain = ⊥ ↔ ∀ x : f.codomain, is_integral R x → f.is_integer x := subalgebra.ext_iff.trans ⟨λ h x hx, algebra.mem_bot.mp ((h x).mp hx), λ h x, iff.trans ⟨λ hx, h x hx, λ ⟨y, hy⟩, hy ▸ is_integral_algebra_map⟩ (@algebra.mem_bot R f.codomain _ _ _ _).symm⟩ @[priority 100] -- see Note [lower instance priority] instance principal_ideal_ring.is_dedekind_domain [is_principal_ideal_ring A] : is_dedekind_domain A := ⟨principal_ideal_ring.is_noetherian_ring, dimension_le_one.principal_ideal_ring _, unique_factorization_monoid.integrally_closed (fraction_ring.of A)⟩ end principal_ideal_ring /-- A Dedekind domain is an integral domain that is Noetherian, and the localization at every nonzero prime is a discrete valuation ring. This is equivalent to `is_dedekind_domain`. TODO: prove the equivalence. -/ structure is_dedekind_domain_dvr : Prop := (to_is_noetherian_ring : is_noetherian_ring A) (is_dvr_at_nonzero_prime : ∀ P ≠ (⊥ : ideal A), P.is_prime → discrete_valuation_ring (localization.at_prime P)) section inverse /-! ### `inverse` section This section deals with the multiplicative inverse of fractional ideals. We define a `has_inv (fractional_ideal g)` instance for Dedekind domains, and show this inverse satisfies the axioms of a `comm_group_with_zero`. The structure `is_dedekind_domain_inv` is an equivalent condition for being a Dedekind domain: all fractional ideals (except `0`) have an inverse. We prove the equivalence in `is_dedekind_domain_iff_inv` -/ namespace ring.fractional_ideal open_locale classical variables {R₁ : Type*} [integral_domain R₁] {g : fraction_map R₁ K} variables {I J : fractional_ideal g} open submodule submodule.is_principal lemma mul_generator_self_inv (I : fractional_ideal g) [is_principal (I : submodule R₁ g.codomain)] (h : I ≠ 0) : I * fractional_ideal.span_singleton (generator (I : submodule R₁ g.codomain))⁻¹ = 1 := begin -- Rewrite only the `I` that appears alone. conv_lhs { congr, rw fractional_ideal.eq_span_singleton_of_principal I }, rw [fractional_ideal.span_singleton_mul_span_singleton, mul_inv_cancel, fractional_ideal.span_singleton_one], intro generator_I_eq_zero, apply h, rw [fractional_ideal.eq_span_singleton_of_principal I, generator_I_eq_zero, fractional_ideal.span_singleton_zero] end variables [is_dedekind_domain R₁] @[nolint unused_arguments] noncomputable instance : has_inv (fractional_ideal g) := ⟨λ I, 1 / I⟩ lemma inv_eq : I⁻¹ = 1 / I := rfl lemma inv_zero' : (0 : fractional_ideal g)⁻¹ = 0 := div_zero lemma inv_nonzero {J : fractional_ideal g} (h : J ≠ 0) : J⁻¹ = ⟨(1 : fractional_ideal g) / J, fractional_div_of_nonzero h⟩ := div_nonzero _ lemma coe_inv_of_nonzero {J : fractional_ideal g} (h : J ≠ 0) : (↑J⁻¹ : submodule R₁ g.codomain) = g.coe_submodule 1 / J := by { rwa inv_nonzero _, refl, assumption} /-- `I⁻¹` is the inverse of `I` if `I` has an inverse. -/ theorem right_inverse_eq (I J : fractional_ideal g) (h : I * J = 1) : J = I⁻¹ := begin have hI : I ≠ 0 := ne_zero_of_mul_eq_one I J h, suffices h' : I * (1 / I) = 1, { exact (congr_arg units.inv $ @units.ext _ _ (units.mk_of_mul_eq_one _ _ h) (units.mk_of_mul_eq_one _ _ h') rfl) }, apply le_antisymm, { apply fractional_ideal.mul_le.mpr _, intros x hx y hy, rw mul_comm, exact (mem_div_iff_of_nonzero hI).mp hy x hx }, rw ← h, apply mul_left_mono I, apply (le_div_iff_of_nonzero hI).mpr _, intros y hy x hx, rw mul_comm, exact mul_mem_mul hx hy end theorem mul_inv_cancel_iff {I : fractional_ideal g} : I * I⁻¹ = 1 ↔ ∃ J, I * J = 1 := ⟨λ h, ⟨I⁻¹, h⟩, λ ⟨J, hJ⟩, by rwa [← @right_inverse_eq _ _ _ _ _ _ I J hJ]⟩ variables {K' : Type*} [field K'] {g' : fraction_map R₁ K'} @[simp] lemma map_inv (I : fractional_ideal g) (h : g.codomain ≃ₐ[R₁] g'.codomain) : (I⁻¹).map (h : g.codomain →ₐ[R₁] g'.codomain) = (I.map h)⁻¹ := by rw [inv_eq, fractional_ideal.map_div, fractional_ideal.map_one, inv_eq] open submodule submodule.is_principal @[simp] lemma span_singleton_inv (x : g.codomain) : (fractional_ideal.span_singleton x)⁻¹ = fractional_ideal.span_singleton (x⁻¹) := fractional_ideal.one_div_span_singleton x local attribute [semireducible] fractional_ideal.span_singleton lemma mul_inv_cancel_of_is_principal (I : fractional_ideal g) [submodule.is_principal (I : submodule R₁ g.codomain)] (h : I ≠ 0) : I * I⁻¹ = 1 := (fractional_ideal.mul_div_self_cancel_iff).mpr ⟨fractional_ideal.span_singleton (generator (I : submodule R₁ g.codomain))⁻¹, @mul_generator_self_inv _ _ _ _ _ I _ h⟩ lemma mul_inv_cancel_iff_generator (I : fractional_ideal g) [submodule.is_principal (I : submodule R₁ g.codomain)] : I * I⁻¹ = 1 ↔ generator (I : submodule R₁ g.codomain) ≠ 0 := begin split, { intros hI hg, apply fractional_ideal.ne_zero_of_mul_eq_one _ _ hI, rw [fractional_ideal.eq_span_singleton_of_principal I, hg, fractional_ideal.span_singleton_zero] }, { intro hg, apply mul_inv_cancel_of_is_principal, rw [fractional_ideal.eq_span_singleton_of_principal I], intro hI, have := fractional_ideal.mem_span_singleton_self (generator (I : submodule R₁ g.codomain)), rw [hI, fractional_ideal.mem_zero_iff] at this, contradiction } end lemma is_principal_inv (I : fractional_ideal g) [submodule.is_principal (I : submodule R₁ g.codomain)] (h : I ≠ 0) : submodule.is_principal (I⁻¹).1 := begin rw [fractional_ideal.val_eq_coe, fractional_ideal.is_principal_iff], use (generator (I : submodule R₁ g.codomain))⁻¹, have hI : I * span_singleton ((generator ↑I)⁻¹) = 1, apply mul_generator_self_inv _ I h, exact (right_inverse_eq _ I (span_singleton ((generator ↑I)⁻¹)) hI).symm end end ring.fractional_ideal /-- A Dedekind domain is an integral domain such that every fractional ideal has an inverse. This is equivalent to `is_dedekind_domain`. TODO: prove the equivalence. -/ def is_dedekind_domain_inv : Prop := ∀ I ≠ (⊥ : fractional_ideal (fraction_ring.of A)), I * (1 / I) = 1 open ring.fractional_ideal lemma is_dedekind_domain_inv_iff (f : fraction_map A K) : is_dedekind_domain_inv A ↔ (∀ I ≠ (⊥ : fractional_ideal f), I * (1 / I) = 1) := begin set h : (fraction_ring.of A).codomain ≃ₐ[A] f.codomain := fraction_ring.alg_equiv_of_quotient f, split; intro hi; intros I hI, { have := hi (map ↑h.symm I) (map_ne_zero _ hI), convert congr_arg (map (h : (fraction_ring.of A).codomain →ₐ[A] f.codomain)) this; simp only [map_symm_map, map_one, fractional_ideal.map_mul, fractional_ideal.map_div, inv_eq] }, { have := hi (map ↑h I) (map_ne_zero _ hI), convert congr_arg (map (h.symm : f.codomain →ₐ[A] (fraction_ring.of A).codomain)) this; simp only [map_map_symm, map_one, fractional_ideal.map_mul, fractional_ideal.map_div, inv_eq] }, end end inverse section equivalence section open ring.fractional_ideal variables {A} open_locale classical variables {B : Type*} [semiring B] variables {M : Type*} [add_comm_monoid M] [semimodule B M] open submodule variables {K} {f : fraction_map A K} lemma fg_of_one_mem_span_mul (s : ideal A) (h2 : (s * (1 / s) : fractional_ideal f) = 1) (T T' : finset f.codomain) (hT : (T : set f.codomain) ⊆ (s : fractional_ideal f)) (hT' : (T' : set f.codomain) ⊆ ↑(1 / (s : fractional_ideal f))) (one_mem : (1 : f.codomain) ∈ span A (T * T' : set f.codomain)) : s.fg := begin apply fg_of_fg_map f.lin_coe (linear_map.ker_eq_bot.mpr f.injective), refine ⟨T, _⟩, apply le_antisymm, { intros x gx, simp only [localization_map.lin_coe_apply, submodule.mem_map], exact submodule.span_le.mpr hT gx }, intros x gx, suffices f2 : span A ({x} * T' : set f.codomain) ≤ 1, { convert submodule.mul_le_mul_left f2 _, { exact (one_mul _).symm }, rw [submodule.span_mul_span, mul_assoc, ← mul_one x, ← submodule.span_mul_span, mul_comm (T' : set f.codomain)] { occs := occurrences.pos [1] }, exact submodule.mul_mem_mul (mem_span_singleton_self x) one_mem, }, rw [← fractional_ideal.coe_one, ← h2, fractional_ideal.coe_mul, ← submodule.span_mul_span], apply submodule.mul_le_mul, { rwa [submodule.span_le, set.singleton_subset_iff] }, { rwa submodule.span_le }, end lemma is_noetherian_of_is_dedekind_domain_inv : is_dedekind_domain_inv A → is_noetherian_ring A := begin intro h2, rw is_noetherian_ring_iff, refine ⟨λ s, _⟩, by_cases h : s = ⊥, { rw h, apply submodule.fg_bot }, have : (1 : fraction_ring A) ∈ (1 : fractional_ideal (fraction_ring.of A)) := one_mem_one, have h := (coe_to_fractional_ideal_ne_zero (le_refl (non_zero_divisors A))).mpr h, rw [← h2 _ h, ← fractional_ideal.mem_coe, fractional_ideal.coe_mul] at this, obtain ⟨T, T', hT, hT', one_mem⟩ := submodule.mem_span_mul_finite_of_mem_mul this, exact fg_of_one_mem_span_mul s (h2 _ h) T T' hT hT' one_mem, end /-- `A[x]` is a fractional ideal for every `x` in the codomain of the fraction map `f`. -/ lemma is_fractional_adjoin_integral (x : f.codomain) (hx : is_integral A x) : is_fractional f (↑(algebra.adjoin A ({x} : set f.codomain)) : submodule A f.codomain) := is_fractional_of_fg (fg_adjoin_singleton_of_integral x hx) lemma mem_adjoin_self (x : f.codomain) : x ∈ ((algebra.adjoin A {x}) : subalgebra A f.codomain) := algebra.subset_adjoin (set.mem_singleton x) lemma int_closed_of_is_dedekind_domain_inv : is_dedekind_domain_inv A → integral_closure A (fraction_ring A) = ⊥ := begin intro h2, rw eq_bot_iff, rintros x hx, set M : fractional_ideal (fraction_ring.of A) := ⟨_, is_fractional_adjoin_integral _ hx⟩ with h1M, have fx : x ∈ M := mem_adjoin_self x, by_cases h : x = 0, { rw h, apply subalgebra.zero_mem _ }, have mul_self : M * M = M, { rw subtype.ext_iff_val, simp }, have eq_one : M = 1, { have g : M ≠ ⊥, { intro a, rw [fractional_ideal.bot_eq_zero, ← fractional_ideal.ext_iff] at a, exact h (mem_zero_iff.mp ((a x).mp fx)) }, have h2 : M * (1 / M) = 1 := h2 _ g, convert congr_arg (* (1 / M)) mul_self; simp only [mul_assoc, h2, mul_one] }, show x ∈ ((⊥ : subalgebra A (localization_map.codomain (fraction_ring.of A))) : submodule A (localization_map.codomain (fraction_ring.of A))), rwa [algebra.to_submodule_bot, ← coe_span_singleton 1, fractional_ideal.span_singleton_one, ← eq_one], end lemma is_field.is_principal_ideal_ring (h : is_field A) : is_principal_ideal_ring A := @euclidean_domain.to_principal_ideal_domain A (@field.to_euclidean_domain A (h.to_field A)) lemma dim_le_one_of_is_dedekind_domain_inv : is_dedekind_domain_inv A → dimension_le_one A := begin have coe_ne_bot : ∀ {I : ideal A}, I ≠ ⊥ → (I : fractional_ideal (fraction_ring.of A)) ≠ 0 := λ I, (coe_to_fractional_ideal_ne_zero (le_refl (non_zero_divisors A))).mpr, rintros h2, -- If A is a field, we're done. by_cases h1 : is_field A, { haveI : is_principal_ideal_ring A := is_field.is_principal_ideal_ring h1, apply dimension_le_one.principal_ideal_ring }, rintros p hpz hp, set p' : fractional_ideal (fraction_ring.of A) := p with p'_eq, have hpinv := h2 p' (coe_ne_bot hpz), -- We're going to show that `p` is maximal because any maximal ideal `M` -- that is strictly larger would be `⊤`. obtain ⟨M, hM1, hM2⟩ := exists_le_maximal p hp.1, set M' : fractional_ideal (fraction_ring.of A) := M with M'_eq, have M'_ne := coe_ne_bot (ne_bot_of_is_maximal_of_not_is_field hM1 h1), have hMinv := h2 M' M'_ne, convert hM1, by_contra h, apply hM1.ne_top, rw [eq_top_iff, ← @coe_ideal_le_coe_ideal _ _ _ _ (fraction_ring.of A), ← ideal.one_eq_top], show 1 ≤ M', suffices g : (1 / M') * p' ≤ p', { have : M' * (((1 / M') * p') * (1 / p')) ≤ M' * (p' * (1 / p')) := mul_left_mono M' (mul_right_mono (1 / p') g), rwa [mul_assoc, hpinv, mul_one, hMinv, mul_one] at this }, -- Suppose we have `x ∈ M'⁻¹ * p'`, then in fact `x = fraction_ring.of A y` for some `y`. rintros x hx, have le_one : (1 / M') * p ≤ 1, { have g'' := fractional_ideal.mul_right_mono (1 / M') (coe_ideal_le_coe_ideal.mpr hM2), simpa only [val_eq_coe, ← coe_mul, hMinv, mul_comm (1 / M') p'] using g'' }, obtain ⟨y, hy, rfl⟩ := mem_coe_ideal.mp (le_one hx), -- Since `M` is maximal and not equal to `p`, let `z ∈ M \ p`. obtain ⟨z, hzM, hzp⟩ := exists_of_lt (lt_of_le_of_ne hM2 h), -- If `z * y ∈ p` (or `fraction_ring.of A (z * y) ∈ p'`) we are done, -- since `p` is prime and `z ∉ p`. suffices zy_mem : (fraction_ring.of A).to_map (z * y) ∈ p', { obtain ⟨zy, hzy, zy_eq⟩ := mem_coe_ideal.mp zy_mem, rw (fraction_ring.of A).injective zy_eq at hzy, exact mem_coe_ideal.mpr ⟨_, or.resolve_left (hp.mem_or_mem hzy) hzp, rfl⟩ }, -- But `p' = M * M⁻¹ * p`, so `z ∈ M` and `y ∈ M⁻¹ * p` and we get our conclusion. rw [ring_hom.map_mul], convert fractional_ideal.mul_mem_mul (show (fraction_ring.of A).to_map z ∈ M', from mem_coe_ideal.mpr ⟨_, hzM, rfl⟩) hx, rw [← mul_assoc, hMinv, one_mul] end /-- Showing one side of the equivalence between the definitions `is_dedekind_domain_inv` and `is_dedekind_domain` of Dedekind domains. -/ theorem is_dedekind_domain_of_is_dedekind_domain_inv : is_dedekind_domain_inv A → is_dedekind_domain A := λ h, ⟨is_noetherian_of_is_dedekind_domain_inv h, dim_le_one_of_is_dedekind_domain_inv h, int_closed_of_is_dedekind_domain_inv h⟩ end namespace is_dedekind_domain section iff_inv variables {R S : Type*} [integral_domain R] [integral_domain S] [algebra R S] variables {L : Type*} [field L] {f : fraction_map R K} open finsupp polynomial ring.fractional_ideal variables {M : ideal R} [is_maximal M] local attribute [instance] classical.prop_decidable lemma exists_not_mem_one_of_ne_bot [hR : is_dedekind_domain R] (hNF : ¬ is_field R) {I : ideal R} (hnbot : I ≠ ⊥) (hntop : I ≠ ⊤) : ∃ x : f.codomain, x ∈ (1 / ↑I : fractional_ideal f) ∧ x ∉ (1 : fractional_ideal f) := begin obtain ⟨M, hM⟩ : ∃ (M : ideal R), is_maximal M ∧ I ≤ M := ideal.exists_le_maximal I hntop, obtain ⟨a, h_nza⟩ : ∃ a : I, a ≠ 0 := submodule.nonzero_mem_of_bot_lt (bot_lt_iff_ne_bot.mpr hnbot), let A : (ideal R) := ideal.span {a}, have hA : A ≠ ⊥ ∧ A ≤ M, { rwa [ne.def, span_singleton_eq_bot, submodule.coe_eq_zero, span_le, set.singleton_subset_iff], split, { exact h_nza }, { apply submodule.le_def'.mp hM.2, apply submodule.coe_mem } }, obtain ⟨Z₀, h_Z₀⟩ := exists_prime_spectrum_prod_le_and_ne_bot_of_domain hNF hA.1, obtain ⟨Z, -, hZ, h_eraseZ⟩ := multiset.can_assume_min Z₀ h_Z₀, have hZ_M : multiset.prod (Z.map (coe : subtype _ → ideal R)) ≤ M := le_trans hZ.1 hA.2, have hZ_nz : Z ≠ 0, { by_contra, rw [ne.def, not_not] at h, have : multiset.prod (Z.map (coe : subtype _ → ideal R)) = ⊤, { rw [h, multiset.map_zero, ← one_eq_top], exact multiset.prod_zero }, rw [this, top_le_iff] at hZ_M, exact hM.1.ne_top hZ_M }, obtain ⟨P, h_PZ, h_PM⟩ := is_prime.multiset.prod_le (ideal.is_maximal.is_prime hM.1) hZ_M, have hZP_nz : P.1 ≠ ⊥ ∧ multiset.prod ((Z.erase P).map (coe : subtype _ → ideal R)) ≠ ⊥, { suffices this : multiset.prod (Z.map (coe : subtype _ → ideal R)) ≠ ⊥, rw [← (multiset.cons_erase h_PZ), multiset.map_cons, multiset.prod_cons, ne.def, ideal.mul_eq_bot, not_or_distrib] at this, exacts [this, hZ.2] }, replace h_PM : P.val = M := is_maximal.eq_of_le _ hM.1.ne_top h_PM, swap, { apply hR.2, exacts [hZP_nz.1, P.2] }, obtain ⟨b, hb⟩ : ∃ (b : R) (H : b ∈ multiset.prod ((Z.erase P).map (coe : subtype _ → ideal R))), b ∉ A, { specialize h_eraseZ P h_PZ, dsimp at h_eraseZ, rw [not_and, not_not] at h_eraseZ, replace h_eraseZ : ¬ multiset.prod ((Z.erase P).map (coe : subtype _ → ideal R)) ≤ A := mt h_eraseZ hZP_nz.2, rwa ← submodule.not_le_iff_exists }, have hnz_fa : (f.to_map a) ≠ 0 := mt (f.to_map.injective_iff.mp f.injective a) _, swap, simp only [h_nza, not_false_iff, submodule.coe_eq_zero], use (f.to_map b) * (f.to_map a)⁻¹, split, { rw fractional_ideal.mem_div_iff_of_nonzero, { rintro y₀ hy₀, obtain ⟨y, h_Iy, hy⟩ := fractional_ideal.mem_coe_ideal.mp hy₀, rw [mul_comm, ← mul_assoc, ← hy, ← ring_hom.map_mul], have h_yb : y * b ∈ A, { suffices hM_yb : y * b ∈ multiset.prod (Z.map (coe : subtype _ → ideal R)), { apply submodule.le_def'.mpr hZ.1 hM_yb }, { rw [← (multiset.cons_erase h_PZ), multiset.map_cons, multiset.prod_cons], apply submodule.smul_mem_smul, rw [← subtype.val_eq_coe, h_PM], { apply submodule.le_def'.mp hM.2 y h_Iy }, { rcases hb with ⟨H, -⟩, assumption } } }, rw ideal.mem_span_singleton' at h_yb, rcases h_yb with ⟨c, hc⟩, rw [← hc, ring_hom.map_mul, mul_assoc, mul_inv_cancel hnz_fa, mul_one], apply fractional_ideal.mem_one_iff.mpr, use c }, { apply (fractional_ideal.coe_to_fractional_ideal_ne_zero _).mpr hnbot, tauto } }, { rw not_iff_not.mpr fractional_ideal.mem_one_iff, rintros ⟨x', h₂_abs⟩, rw [← div_eq_mul_inv, eq_div_iff_mul_eq hnz_fa, ← ring_hom.map_mul] at h₂_abs, replace h₂_abs : x' * ↑a = b := f.injective h₂_abs, replace h₂_abs : b ∈ A := ideal.mem_span_singleton'.mpr ⟨x', h₂_abs⟩, tauto }, end lemma coe_ideal_mul_one_div [hR : is_dedekind_domain R] (I : ideal R) (hne : I ≠ ⊥) : ↑I * ((1 : fractional_ideal f) / ↑I) = (1 : fractional_ideal f) := begin by_cases hNF : is_field R, { rw [← not_iff_comm.mp not_is_field_iff_exists_ideal_bot_lt_and_lt_top, not_exists] at hNF, specialize hNF I, rw [not_and_distrib, or_eq_of_eq_false_left, lt_top_iff_ne_top, ne.def, not_not, ← ideal.one_eq_top] at hNF, rw hNF, show (1 * (1 / 1) : fractional_ideal f) = 1, rw [one_mul, ring.fractional_ideal.div_one], simp only [bot_lt_iff_ne_bot, hne, not_true, ne.def, not_false_iff], apply is_integral_domain.to_nontrivial, apply integral_domain.to_is_integral_domain, }, { let h_RalgK := ring_hom.to_algebra f.to_map, by_cases hntop : I = ⊤, { rw [hntop, ← ideal.one_eq_top], show (1 * (1 / 1) : fractional_ideal f) = 1, simp only [mul_one, ring.fractional_ideal.div_one] }, { by_contradiction h_abs, obtain ⟨J, hJ⟩ : ∃ (J : ideal R), ↑J = ↑I * (1 / ↑I : fractional_ideal f) := fractional_ideal.le_one_iff_exists_coe_ideal.mp fractional_ideal.mul_one_div_le_one, by_cases hJ_b : J = ⊥, { rw hJ_b at hJ, apply hne, rw [eq_bot_iff, ← @coe_ideal_le_coe_ideal _ _ _ _ f, hJ], apply fractional_ideal.le_self_mul_one_div, exact fractional_ideal.coe_ideal_le_one }, have hJ_t : J ≠ ⊤, { intro hJ_t, rw [← hJ, hJ_t, ← ideal.one_eq_top] at h_abs, exact h_abs rfl }, obtain ⟨x, hx, h_xnotint⟩ : ∃ (x : f.codomain), x ∈ (1 / ↑J : fractional_ideal f) ∧ x ∉ (1 : fractional_ideal f) := exists_not_mem_one_of_ne_bot _ hNF hJ_b hJ_t, have h₁ : (submodule.span R {x} * (1 / ↑I : fractional_ideal f).val) ≤ (1 / ↑I : fractional_ideal f).val, { apply submodule.mul_le.mpr, intros z hz b hb, rw fractional_ideal.val_eq_coe at hb, obtain ⟨a, ha⟩ := submodule.mem_span_singleton.mp hz, rw [← ha, algebra.smul_def, mul_assoc, mul_comm, localization_map.algebra_map_eq, val_eq_coe, coe_div, coe_coe_ideal, submodule.mem_div_iff_forall_mul_mem], intros y hy, have h_by : y * b ∈ (↑J : fractional_ideal f).val, { rw [hJ, val_eq_coe, fractional_ideal.coe_mul, coe_coe_ideal], apply submodule.mul_mem_mul hy hb }, replace h_by : (f.to_map a) * y * b ∈ (↑J : fractional_ideal f).val, { rw mul_assoc, exact submodule.smul_mem _ _ h_by }, rw [← fractional_ideal.mem_coe, fractional_ideal.coe_div, fractional_ideal.coe_coe_ideal] at hx, rw [mul_assoc, mul_assoc], apply submodule.mem_div_iff_forall_mul_mem.mp hx, rw mul_comm, exact h_by, repeat { rwa fractional_ideal.coe_to_fractional_ideal_ne_zero, exact le_refl _ } }, have h_pow : ∀ n : ℕ, x ^ n ∈ (1 / ↑I : fractional_ideal f), { intro n, induction n with n hn, { rw fractional_ideal.mem_div_iff_of_nonzero, simp only [pow_zero, one_mul], intros y' hy', rw fractional_ideal.mem_one_iff, rw fractional_ideal.mem_coe_ideal at hy', rcases hy' with ⟨y, -, hy⟩, exact ⟨y, hy⟩, apply mt (coe_to_fractional_ideal_eq_zero (le_refl (non_zero_divisors R))).mp hne }, { rw pow_succ, apply submodule.mul_le.mp h₁, exacts [submodule.mem_span_singleton_self x, hn] }}, let φ := @aeval R K _ _ h_RalgK x, let A := @alg_hom.range R (polynomial R) f.codomain _ _ _ _ h_RalgK φ, have h_xA : x ∈ A, { suffices hp : ∃ (p : polynomial R), φ p = x, { simpa only [alg_hom.mem_range] }, { use X, apply aeval_X } }, have h_Afrac : (↑A : submodule R f.codomain) ≤ (1 / ↑I : fractional_ideal f).val, { rw submodule.le_def', intros a ha, dsimp [A] at ha, rw [subalgebra.mem_to_submodule, alg_hom.mem_range] at ha, cases ha with p hp, rw aeval_eq_sum_range at hp, rw ← hp, apply submodule.sum_mem, intros i hi, exact submodule.smul_mem _ _ (h_pow i) }, have h_xint : x ∈ integral_closure R f.codomain, { have h_noeth : is_noetherian R (1 / ↑I : fractional_ideal f).val := by apply fractional_ideal.is_noetherian, rw mem_integral_closure_iff_mem_fg, use A, split, apply is_noetherian_submodule.mp, exacts [h_noeth, h_Afrac, h_xA] }, replace h_xint : x ∈ ((⊥ : subalgebra R f.codomain) : submodule R f.codomain), { rw ← ((is_dedekind_domain_iff _ _ f).mp hR).right.right, exact h_xint }, rw [algebra.to_submodule_bot, ← fractional_ideal.coe_span_singleton 1, fractional_ideal.span_singleton_one] at h_xint, tauto } }, end theorem fractional_ideal.mul_inv_cancel [hR : is_dedekind_domain R] {I : fractional_ideal f} (hne : I ≠ 0) : I * (1 / I) = 1 := begin by_cases hNF : is_field R, { obtain rfl : I = 1 := (I.eq_zero_or_one_of_is_field hNF).resolve_left hne, simp }, obtain ⟨a, J, ha, hJ⟩ : ∃ (a : R) (aI : ideal R), a ≠ 0 ∧ I = span_singleton (f.to_map a)⁻¹ * aI := exists_eq_span_singleton_mul I, have hne_J : (↑J : fractional_ideal f) ≠ 0, { rw hJ at hne, apply right_ne_zero_of_mul hne }, have h₁ : fractional_ideal.span_singleton (f.to_map a) * I = ↑J, { rw hJ, rw [← mul_assoc, fractional_ideal.span_singleton_mul_span_singleton, mul_inv_cancel, fractional_ideal.span_singleton_one, one_mul], apply mt (f.to_map.injective_iff.mp f.injective a) ha }, suffices h₂ : I * (fractional_ideal.span_singleton (f.to_map a) * (1 / J)) = 1, { rw fractional_ideal.mul_div_self_cancel_iff, exact ⟨fractional_ideal.span_singleton (f.to_map a) * (1 / J), h₂⟩ }, { rw mul_comm at h₁, rw [← mul_assoc, h₁], exact coe_ideal_mul_one_div _ _ ((coe_to_fractional_ideal_ne_zero (le_refl (non_zero_divisors R))).mp hne_J) }, end lemma fractional_ideal.is_unit {hR : is_dedekind_domain R} (I : fractional_ideal f) (hne : I ≠ ⊥) : is_unit I := begin apply is_unit_of_mul_eq_one I ((1 : fractional_ideal f) / I), exact fractional_ideal.mul_inv_cancel _ hne end lemma mul_one_div [is_dedekind_domain R] {I J : fractional_ideal f} : I * (1 / J) = I / J := le_antisymm fractional_ideal.mul_one_div_le_div $ if hJ : J = 0 then by simp [hJ] else have (I / J) * J ≤ I := (fractional_ideal.le_div_iff_mul_le hJ).mp (le_refl _), by simpa [mul_assoc, fractional_ideal.mul_inv_cancel _ hJ] using fractional_ideal.mul_right_mono (1 / J) this noncomputable instance [hR : is_dedekind_domain R] : comm_group_with_zero (fractional_ideal f) := { inv := λ I, 1 / I, div := λ I J, I / J, div_eq_mul_inv := λ I J, by rw [inv_eq, mul_one_div], inv_zero := fractional_ideal.div_zero, mul_inv_cancel := λ I hI, by rw [inv_eq, fractional_ideal.mul_inv_cancel _ hI], .. fractional_ideal.nontrivial, .. fractional_ideal.comm_semiring } theorem is_dedekind_domain_iff_inv : is_dedekind_domain R ↔ is_dedekind_domain_inv R := ⟨λ hR I hI, @fractional_ideal.mul_inv_cancel _ _ _ _ _ hR _ hI, is_dedekind_domain_of_is_dedekind_domain_inv⟩ end iff_inv open_locale big_operators variables {K} section integral_closure /-! ### `integral_closure` section We show that the integral closure of a Dedekind domain in a finite separable field extension is again a Dedekind domain. This implies the ring of integers of a number field is a Dedekind domain. -/ variables {R L : Type*} [integral_domain R] [field L] variables {f : fraction_map R K} variables [algebra f.codomain L] [algebra R L] [is_scalar_tower R f.codomain L] lemma integral_closure_le_span [is_separable (localization_map.codomain f) L] {ι : Type*} [fintype ι] [decidable_eq ι] {b : ι → L} (hb : is_basis f.codomain b) (hb_int : ∀ i, is_integral R (b i)) (int_cl : integral_closure R f.codomain = ⊥) : (integral_closure R L : submodule R L) ≤ submodule.span R (set.range (dual_basis hb)) := begin rintros x (hx : is_integral R x), suffices : ∃ (c : ι → R), x = ∑ i, c i • dual_basis hb i, { obtain ⟨c, rfl⟩ := this, refine submodule.sum_mem _ (λ i _, submodule.smul_mem _ _ (submodule.subset_span _)), rw set.mem_range, exact ⟨i, rfl⟩ }, suffices : ∃ (c : ι → f.codomain), ((∀ i, is_integral R (c i)) ∧ x = ∑ i, c i • dual_basis hb i), { obtain ⟨c, hc, hx⟩ := this, have hc' := λ i, (integrally_closed_iff_integral_implies_integer.mp int_cl (c i) (hc i)), use λ i, classical.some (hc' i), refine hx.trans (finset.sum_congr rfl (λ i _, _)), conv_lhs { rw [← classical.some_spec (hc' i)] }, rw [← is_scalar_tower.algebra_map_smul f.codomain (classical.some (hc' i)) (dual_basis hb i), f.algebra_map_eq] }, refine ⟨λ i, (is_basis_dual_basis hb).repr x i, (λ i, _), (sum_repr _ _).symm⟩, rw ← trace_gen_pow_mul, haveI : finite_dimensional f.codomain L := finite_dimensional.of_fintype_basis hb, exact is_integral_trace (is_integral_mul (hb_int i) hx) end lemma is_noetherian_of_le {s t : submodule R L} (ht : is_noetherian R t) (h : s ≤ t): is_noetherian R s := is_noetherian_submodule.mpr (λ s' hs', is_noetherian_submodule.mp ht _ (le_trans hs' h)) lemma is_noetherian_adjoin_finset [is_noetherian_ring R] (s : finset L) (hs : ∀ x ∈ s, is_integral R x) : is_noetherian R (algebra.adjoin R (↑s : set L)) := is_noetherian_of_fg_of_noetherian _ (fg_adjoin_of_finite s.finite_to_set hs) section variables (f) /-- Send a set of `x`'es in a finite extension `L` of the fraction field of `R` to `(y : R) • x ∈ integral_closure R L`. -/ lemma exists_integral_multiples [finite_dimensional f.codomain L] (s : finset L) : ∃ (y ≠ (0 : R)), ∀ x ∈ s, is_integral R (y • x) := begin haveI := classical.dec_eq L, refine s.induction _ _, { use [1, one_ne_zero], rintros x ⟨⟩ }, { rintros x s hx ⟨y, hy, hs⟩, obtain ⟨x', y', hy', hx'⟩ := exists_integral_multiple (f.is_algebraic_iff.mp (algebra.is_algebraic_of_finite x)) _, use [y * y', mul_ne_zero hy hy'], intros x'' hx'', rcases finset.mem_insert.mp hx'' with (rfl | hx''), { rw [mul_smul, hx', algebra.smul_def], exact is_integral_mul is_integral_algebra_map x'.2 }, { rw [mul_comm, mul_smul, algebra.smul_def], exact is_integral_mul is_integral_algebra_map (hs _ hx'') }, { rw is_scalar_tower.algebra_map_eq R f.codomain L, apply (algebra_map f.codomain L).injective.comp, rw f.algebra_map_eq, exact f.injective } } end end /-- If `x` in a field `L` is not zero, then multiplying in `L` by `x` is a linear equivalence. -/ def lsmul_equiv {x : R} (hx : algebra_map R L x ≠ 0) : L ≃ₗ[R] L := { inv_fun := λ y, (algebra_map R L x)⁻¹ * y, left_inv := λ y, by simp only [linear_map.to_fun_eq_coe, algebra.lmul_apply, ← mul_assoc, inv_mul_cancel hx, one_mul], right_inv := λ y, by simp only [linear_map.to_fun_eq_coe, algebra.lmul_apply, ← mul_assoc, mul_inv_cancel hx, one_mul], .. algebra.lmul R L (algebra_map R L x) } @[simp] lemma lsmul_equiv_apply {x : R} (hx : algebra_map R L x ≠ 0) (y : L) : lsmul_equiv hx y = x • y := (algebra.smul_def x y).symm section variables {K} (f L) lemma exists_is_basis_integral [finite_dimensional f.codomain L] : ∃ (s : finset L) (b : (↑s : set L) → L), is_basis f.codomain b ∧ (∀ x, is_integral R (b x)) := let ⟨s', hbs'⟩ := finite_dimensional.exists_is_basis_finset f.codomain L, ⟨y, hy, his'⟩ := exists_integral_multiples f s' in have hy' : algebra_map f.codomain L (algebra_map R f.codomain y) ≠ 0 := by { apply mt (λ h, _) hy, apply f.to_map.injective_iff.mp f.injective, apply (algebra_map f.codomain L).injective_iff.mp (algebra_map f.codomain L).injective, exact h }, ⟨s', _, (lsmul_equiv hy').is_basis hbs', by { rintros ⟨x', hx'⟩, simp only [function.comp, lsmul_equiv_apply, is_scalar_tower.algebra_map_smul], exact his' x' hx' }⟩ end lemma integral_closure.is_noetherian_ring [is_noetherian_ring R] [finite_dimensional f.codomain L] [is_separable (localization_map.codomain f) L] (int_cl : integral_closure R f.codomain = ⊥) : is_noetherian_ring (integral_closure R L) := begin haveI := classical.dec_eq L, obtain ⟨s, b, hb, hb_int⟩ := exists_is_basis_integral L f, rw is_noetherian_ring_iff, exact is_noetherian_of_is_scalar_tower _ (is_noetherian_of_le (is_noetherian_span_of_finite _ (set.finite_range _)) (integral_closure_le_span hb (λ x, hb_int x) int_cl)) end variables (f) /- If L is a finite extension of R's fraction field, the integral closure of R in L is a Dedekind domain. -/ protected lemma integral_closure [finite_dimensional f.codomain L] [is_separable f.codomain L] (h : is_dedekind_domain R) : is_dedekind_domain (integral_closure R L) := (is_dedekind_domain_iff _ _ (integral_closure.fraction_map_of_finite_extension L f)).mpr ⟨integral_closure.is_noetherian_ring ((is_dedekind_domain_iff _ _ f).mp h).2.2, h.dimension_le_one.integral_closure _ _, integral_closure_idem⟩ instance integral_closure.is_dedekind_domain [algebra (fraction_ring.of R).codomain L] [is_scalar_tower R (fraction_ring.of R).codomain L] [finite_dimensional (fraction_ring.of R).codomain L] [is_separable (fraction_ring.of R).codomain L] [h : is_dedekind_domain R] : is_dedekind_domain (integral_closure R L) := is_dedekind_domain.integral_closure (fraction_ring.of R) h end integral_closure end is_dedekind_domain end equivalence section ideal variables {R : Type*} [integral_domain R] [is_dedekind_domain R] open ring.fractional_ideal /-! ### `ideal` section This section covers basic properties of (non-fractional) ideals in a Dedekind domain. -/ /-- For ideals in a dedekind domain, to contain is to divide. -/ lemma ideal.dvd_iff_le {I J : ideal R} : (I ∣ J) ↔ J ≤ I := ⟨ideal.le_of_dvd, λ h, begin by_cases hI : I = ⊥, { have hJ : J = ⊥, { rw hI at h, exact eq_bot_iff.mpr h }, rw [hI, hJ] }, set f := fraction_ring.of R, have hI' : (I : fractional_ideal f) ≠ 0 := (fractional_ideal.coe_to_fractional_ideal_ne_zero (le_refl (non_zero_divisors R))).mpr hI, have : (I : fractional_ideal f)⁻¹ * J ≤ 1 := le_trans (fractional_ideal.mul_left_mono _ (coe_ideal_le_coe_ideal.mpr h)) (le_of_eq (inv_mul_cancel hI')), obtain ⟨H, hH⟩ := fractional_ideal.le_one_iff_exists_coe_ideal.mp this, use H, refine coe_to_fractional_ideal_injective (le_refl (non_zero_divisors R)) (show (J : fractional_ideal f) = _, from _), rw [fractional_ideal.coe_ideal_mul, hH, ← mul_assoc, mul_inv_cancel hI', one_mul] end⟩ lemma ideal.mul_left_cancel' {H I J : ideal R} (hH : H ≠ 0) (hIJ : H * I = H * J) : I = J := coe_to_fractional_ideal_injective (le_refl (non_zero_divisors R)) (show (I : fractional_ideal (fraction_ring.of R)) = J, from mul_left_cancel' ((coe_to_fractional_ideal_ne_zero (le_refl (non_zero_divisors R))).mpr hH) (by simpa only [← fractional_ideal.coe_ideal_mul] using congr_arg coe hIJ)) instance : comm_cancel_monoid_with_zero (ideal R) := { mul_left_cancel_of_ne_zero := λ H I J hH hIJ, ideal.mul_left_cancel' hH hIJ, mul_right_cancel_of_ne_zero := λ H I J hI hHJ, ideal.mul_left_cancel' hI (by rwa [mul_comm I H, mul_comm I J]), .. ideal.comm_semiring } lemma ideal.is_unit_iff {I : ideal R} : is_unit I ↔ I = ⊤ := by rw [is_unit_iff_dvd_one, ideal.one_eq_top, ideal.dvd_iff_le, eq_top_iff] lemma ideal.dvd_not_unit_iff_lt {I J : ideal R} : dvd_not_unit I J ↔ J < I := ⟨λ ⟨hI, H, hunit, hmul⟩, lt_of_le_of_ne (ideal.dvd_iff_le.mp ⟨H, hmul⟩) (mt (λ h, have H = 1, from mul_left_cancel' hI (by rw [← hmul, h, mul_one]), show is_unit H, from this.symm ▸ is_unit_one) hunit), λ h, dvd_not_unit_of_dvd_of_not_dvd (ideal.dvd_iff_le.mpr (le_of_lt h)) (mt ideal.dvd_iff_le.mp (not_le_of_lt h))⟩ lemma ideal.dvd_not_unit_eq_gt : (dvd_not_unit : ideal R → ideal R → Prop) = (>) := by { ext, exact ideal.dvd_not_unit_iff_lt } instance : wf_dvd_monoid (ideal R) := { well_founded_dvd_not_unit := have well_founded ((>) : ideal R → ideal R → Prop) := is_noetherian_iff_well_founded.mp (is_noetherian_ring_iff.mp is_dedekind_domain.to_is_noetherian_ring), by rwa ideal.dvd_not_unit_eq_gt } instance ideal.unique_factorization_monoid : unique_factorization_monoid (ideal R) := { irreducible_iff_prime := λ P, ⟨λ hirr, ⟨hirr.ne_zero, hirr.not_unit, λ I J, begin have : P.is_maximal, { use mt ideal.is_unit_iff.mpr hirr.not_unit, intros J hJ, obtain ⟨J_ne, H, hunit, P_eq⟩ := ideal.dvd_not_unit_iff_lt.mpr hJ, exact ideal.is_unit_iff.mp ((hirr.is_unit_or_is_unit P_eq).resolve_right hunit) }, simp only [ideal.dvd_iff_le, has_le.le, preorder.le, partial_order.le], contrapose!, rintros ⟨⟨x, x_mem, x_not_mem⟩, ⟨y, y_mem, y_not_mem⟩⟩, exact ⟨x * y, ideal.mul_mem_mul x_mem y_mem, mt this.is_prime.mem_or_mem (not_or x_not_mem y_not_mem)⟩, end⟩, λ h, irreducible_of_prime h⟩, .. ideal.wf_dvd_monoid } /-- In a Dedekind domain, each ideal has finitely many divisors. -/ noncomputable def ideal.finite_divisors (I : ideal R) (hI : I ≠ ⊥) : fintype {J // J ∣ I} := begin apply @fintype.of_equiv _ _ (unique_factorization_monoid.finite_divisors hI), refine equiv.symm (equiv.subtype_equiv associates_ideal_equiv.to_equiv _), intro J, simp [associates_ideal_equiv, associates.mk_dvd_mk], end end ideal
946babad6f07688fad9628c97c686ae8420b0447
9dc8cecdf3c4634764a18254e94d43da07142918
/src/ring_theory/unique_factorization_domain.lean
268c6a5c17543a636f083aeb120a91d2a2c36d68
[ "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
70,282
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jens Wagemaker, Aaron Anderson -/ import algebra.big_operators.associated import algebra.gcd_monoid.basic import data.finsupp.multiset import ring_theory.noetherian import ring_theory.multiplicity /-! # Unique factorization ## Main Definitions * `wf_dvd_monoid` holds for `monoid`s for which a strict divisibility relation is well-founded. * `unique_factorization_monoid` holds for `wf_dvd_monoid`s where `irreducible` is equivalent to `prime` ## To do * set up the complete lattice structure on `factor_set`. -/ variables {α : Type*} local infix ` ~ᵤ ` : 50 := associated /-- Well-foundedness of the strict version of |, which is equivalent to the descending chain condition on divisibility and to the ascending chain condition on principal ideals in an integral domain. -/ class wf_dvd_monoid (α : Type*) [comm_monoid_with_zero α] : Prop := (well_founded_dvd_not_unit : well_founded (@dvd_not_unit α _)) export wf_dvd_monoid (well_founded_dvd_not_unit) @[priority 100] -- see Note [lower instance priority] instance is_noetherian_ring.wf_dvd_monoid [comm_ring α] [is_domain α] [is_noetherian_ring α] : wf_dvd_monoid α := ⟨by { convert inv_image.wf (λ a, ideal.span ({a} : set α)) (well_founded_submodule_gt _ _), ext, exact ideal.span_singleton_lt_span_singleton.symm }⟩ namespace wf_dvd_monoid variables [comm_monoid_with_zero α] open associates nat theorem of_wf_dvd_monoid_associates (h : wf_dvd_monoid (associates α)): wf_dvd_monoid α := ⟨begin haveI := h, refine (surjective.well_founded_iff mk_surjective _).2 well_founded_dvd_not_unit, intros, rw mk_dvd_not_unit_mk_iff end⟩ variables [wf_dvd_monoid α] instance wf_dvd_monoid_associates : wf_dvd_monoid (associates α) := ⟨begin refine (surjective.well_founded_iff mk_surjective _).1 well_founded_dvd_not_unit, intros, rw mk_dvd_not_unit_mk_iff end⟩ theorem well_founded_associates : well_founded ((<) : associates α → associates α → Prop) := subrelation.wf (λ x y, dvd_not_unit_of_lt) well_founded_dvd_not_unit local attribute [elab_as_eliminator] well_founded.fix lemma exists_irreducible_factor {a : α} (ha : ¬ is_unit a) (ha0 : a ≠ 0) : ∃ i, irreducible i ∧ i ∣ a := let ⟨b, hs, hr⟩ := well_founded_dvd_not_unit.has_min {b | b ∣ a ∧ ¬ is_unit b} ⟨a, dvd_rfl, ha⟩ in ⟨b, ⟨hs.2, λ c d he, let h := dvd_trans ⟨d, he⟩ hs.1 in or_iff_not_imp_left.2 $ λ hc, of_not_not $ λ hd, hr c ⟨h, hc⟩ ⟨ne_zero_of_dvd_ne_zero ha0 h, d, hd, he⟩⟩, hs.1⟩ @[elab_as_eliminator] lemma induction_on_irreducible {P : α → Prop} (a : α) (h0 : P 0) (hu : ∀ u : α, is_unit u → P u) (hi : ∀ a i : α, a ≠ 0 → irreducible i → P a → P (i * a)) : P a := by haveI := classical.dec; exact well_founded_dvd_not_unit.fix (λ a ih, if ha0 : a = 0 then ha0.substr h0 else if hau : is_unit a then hu a hau else let ⟨i, hii, b, hb⟩ := exists_irreducible_factor hau ha0, hb0 : b ≠ 0 := ne_zero_of_dvd_ne_zero ha0 ⟨i, mul_comm i b ▸ hb⟩ in hb.symm ▸ hi b i hb0 hii $ ih b ⟨hb0, i, hii.1, mul_comm i b ▸ hb⟩) a lemma exists_factors (a : α) : a ≠ 0 → ∃ f : multiset α, (∀ b ∈ f, irreducible b) ∧ associated f.prod a := induction_on_irreducible a (λ h, (h rfl).elim) (λ u hu _, ⟨0, λ _ h, h.elim, hu.unit, one_mul _⟩) (λ a i ha0 hi ih _, let ⟨s, hs⟩ := ih ha0 in ⟨i ::ₘ s, λ b H, (multiset.mem_cons.1 H).elim (λ h, h.symm ▸ hi) (hs.1 b), by { rw s.prod_cons i, exact hs.2.mul_left i }⟩) lemma not_unit_iff_exists_factors_eq (a : α) (hn0 : a ≠ 0) : ¬ is_unit a ↔ ∃ f : multiset α, (∀ b ∈ f, irreducible b) ∧ f.prod = a ∧ f ≠ ∅ := ⟨λ hnu, begin obtain ⟨f, hi, u, rfl⟩ := exists_factors a hn0, obtain ⟨b, h⟩ := multiset.exists_mem_of_ne_zero (λ h : f = 0, hnu $ by simp [h]), classical, refine ⟨(f.erase b).cons (b * u), λ a ha, _, _, multiset.cons_ne_zero⟩, { obtain (rfl|ha) := multiset.mem_cons.1 ha, exacts [associated.irreducible ⟨u,rfl⟩ (hi b h), hi a (multiset.mem_of_mem_erase ha)] }, { rw [multiset.prod_cons, mul_comm b, mul_assoc, multiset.prod_erase h, mul_comm] }, end, λ ⟨f, hi, he, hne⟩, let ⟨b, h⟩ := multiset.exists_mem_of_ne_zero hne in not_is_unit_of_not_is_unit_dvd (hi b h).not_unit $ he ▸ multiset.dvd_prod h⟩ end wf_dvd_monoid theorem wf_dvd_monoid.of_well_founded_associates [cancel_comm_monoid_with_zero α] (h : well_founded ((<) : associates α → associates α → Prop)) : wf_dvd_monoid α := wf_dvd_monoid.of_wf_dvd_monoid_associates ⟨by { convert h, ext, exact associates.dvd_not_unit_iff_lt }⟩ theorem wf_dvd_monoid.iff_well_founded_associates [cancel_comm_monoid_with_zero α] : wf_dvd_monoid α ↔ well_founded ((<) : associates α → associates α → Prop) := ⟨by apply wf_dvd_monoid.well_founded_associates, wf_dvd_monoid.of_well_founded_associates⟩ section prio set_option default_priority 100 -- see Note [default priority] /-- unique factorization monoids. These are defined as `cancel_comm_monoid_with_zero`s with well-founded strict divisibility relations, but this is equivalent to more familiar definitions: Each element (except zero) is uniquely represented as a multiset of irreducible factors. Uniqueness is only up to associated elements. Each element (except zero) is non-uniquely represented as a multiset of prime factors. To define a UFD using the definition in terms of multisets of irreducible factors, use the definition `of_exists_unique_irreducible_factors` To define a UFD using the definition in terms of multisets of prime factors, use the definition `of_exists_prime_factors` -/ class unique_factorization_monoid (α : Type*) [cancel_comm_monoid_with_zero α] extends wf_dvd_monoid α : Prop := (irreducible_iff_prime : ∀ {a : α}, irreducible a ↔ prime a) /-- Can't be an instance because it would cause a loop `ufm → wf_dvd_monoid → ufm → ...`. -/ @[reducible] lemma ufm_of_gcd_of_wf_dvd_monoid [cancel_comm_monoid_with_zero α] [wf_dvd_monoid α] [gcd_monoid α] : unique_factorization_monoid α := { irreducible_iff_prime := λ _, gcd_monoid.irreducible_iff_prime .. ‹wf_dvd_monoid α› } instance associates.ufm [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α] : unique_factorization_monoid (associates α) := { irreducible_iff_prime := by { rw ← associates.irreducible_iff_prime_iff, apply unique_factorization_monoid.irreducible_iff_prime, } .. (wf_dvd_monoid.wf_dvd_monoid_associates : wf_dvd_monoid (associates α)) } end prio namespace unique_factorization_monoid variables [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α] theorem exists_prime_factors (a : α) : a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a := by { simp_rw ← unique_factorization_monoid.irreducible_iff_prime, apply wf_dvd_monoid.exists_factors a } @[elab_as_eliminator] lemma induction_on_prime {P : α → Prop} (a : α) (h₁ : P 0) (h₂ : ∀ x : α, is_unit x → P x) (h₃ : ∀ a p : α, a ≠ 0 → prime p → P a → P (p * a)) : P a := begin simp_rw ← unique_factorization_monoid.irreducible_iff_prime at h₃, exact wf_dvd_monoid.induction_on_irreducible a h₁ h₂ h₃, end end unique_factorization_monoid lemma prime_factors_unique [cancel_comm_monoid_with_zero α] : ∀ {f g : multiset α}, (∀ x ∈ f, prime x) → (∀ x ∈ g, prime x) → f.prod ~ᵤ g.prod → multiset.rel associated f g := by haveI := classical.dec_eq α; exact λ f, multiset.induction_on f (λ g _ hg h, multiset.rel_zero_left.2 $ multiset.eq_zero_of_forall_not_mem $ λ x hx, have is_unit g.prod, by simpa [associated_one_iff_is_unit] using h.symm, (hg x hx).not_unit $ is_unit_iff_dvd_one.2 $ (multiset.dvd_prod hx).trans (is_unit_iff_dvd_one.1 this)) (λ p f ih g hf hg hfg, let ⟨b, hbg, hb⟩ := exists_associated_mem_of_dvd_prod (hf p (by simp)) (λ q hq, hg _ hq) $ hfg.dvd_iff_dvd_right.1 (show p ∣ (p ::ₘ f).prod, by simp) in begin rw ← multiset.cons_erase hbg, exact multiset.rel.cons hb (ih (λ q hq, hf _ (by simp [hq])) (λ q (hq : q ∈ g.erase b), hg q (multiset.mem_of_mem_erase hq)) (associated.of_mul_left (by rwa [← multiset.prod_cons, ← multiset.prod_cons, multiset.cons_erase hbg]) hb (hf p (by simp)).ne_zero)), end) namespace unique_factorization_monoid variables [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α] lemma factors_unique {f g : multiset α} (hf : ∀ x ∈ f, irreducible x) (hg : ∀ x ∈ g, irreducible x) (h : f.prod ~ᵤ g.prod) : multiset.rel associated f g := prime_factors_unique (λ x hx, irreducible_iff_prime.mp (hf x hx)) (λ x hx, irreducible_iff_prime.mp (hg x hx)) h end unique_factorization_monoid /-- If an irreducible has a prime factorization, then it is an associate of one of its prime factors. -/ lemma prime_factors_irreducible [cancel_comm_monoid_with_zero α] {a : α} {f : multiset α} (ha : irreducible a) (pfa : (∀ b ∈ f, prime b) ∧ f.prod ~ᵤ a) : ∃ p, a ~ᵤ p ∧ f = {p} := begin haveI := classical.dec_eq α, refine multiset.induction_on f (λ h, (ha.not_unit (associated_one_iff_is_unit.1 (associated.symm h))).elim) _ pfa.2 pfa.1, rintros p s _ ⟨u, hu⟩ hs, use p, have hs0 : s = 0, { by_contra hs0, obtain ⟨q, hq⟩ := multiset.exists_mem_of_ne_zero hs0, apply (hs q (by simp [hq])).2.1, refine (ha.is_unit_or_is_unit (_ : _ = ((p * ↑u) * (s.erase q).prod) * _)).resolve_left _, { rw [mul_right_comm _ _ q, mul_assoc, ← multiset.prod_cons, multiset.cons_erase hq, ← hu, mul_comm, mul_comm p _, mul_assoc], simp, }, apply mt is_unit_of_mul_is_unit_left (mt is_unit_of_mul_is_unit_left _), apply (hs p (multiset.mem_cons_self _ _)).2.1 }, simp only [mul_one, multiset.prod_cons, multiset.prod_zero, hs0] at *, exact ⟨associated.symm ⟨u, hu⟩, rfl⟩, end section exists_prime_factors variables [cancel_comm_monoid_with_zero α] variables (pf : ∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a) include pf lemma wf_dvd_monoid.of_exists_prime_factors : wf_dvd_monoid α := ⟨begin classical, refine rel_hom_class.well_founded (rel_hom.mk _ _ : (dvd_not_unit : α → α → Prop) →r ((<) : ℕ∞ → ℕ∞ → Prop)) (with_top.well_founded_lt nat.lt_wf), { intro a, by_cases h : a = 0, { exact ⊤ }, exact (classical.some (pf a h)).card }, rintros a b ⟨ane0, ⟨c, hc, b_eq⟩⟩, rw dif_neg ane0, by_cases h : b = 0, { simp [h, lt_top_iff_ne_top] }, rw [dif_neg h, with_top.coe_lt_coe], have cne0 : c ≠ 0, { refine mt (λ con, _) h, rw [b_eq, con, mul_zero] }, calc multiset.card (classical.some (pf a ane0)) < _ + multiset.card (classical.some (pf c cne0)) : lt_add_of_pos_right _ (multiset.card_pos.mpr (λ con, hc (associated_one_iff_is_unit.mp _))) ... = multiset.card (classical.some (pf a ane0) + classical.some (pf c cne0)) : (multiset.card_add _ _).symm ... = multiset.card (classical.some (pf b h)) : multiset.card_eq_card_of_rel (prime_factors_unique _ (classical.some_spec (pf _ h)).1 _), { convert (classical.some_spec (pf c cne0)).2.symm, rw [con, multiset.prod_zero] }, { intros x hadd, rw multiset.mem_add at hadd, cases hadd; apply (classical.some_spec (pf _ _)).1 _ hadd }, { rw multiset.prod_add, transitivity a * c, { apply associated.mul_mul; apply (classical.some_spec (pf _ _)).2 }, { rw ← b_eq, apply (classical.some_spec (pf _ _)).2.symm, } } end⟩ lemma irreducible_iff_prime_of_exists_prime_factors {p : α} : irreducible p ↔ prime p := begin by_cases hp0 : p = 0, { simp [hp0] }, refine ⟨λ h, _, prime.irreducible⟩, obtain ⟨f, hf⟩ := pf p hp0, obtain ⟨q, hq, rfl⟩ := prime_factors_irreducible h hf, rw hq.prime_iff, exact hf.1 q (multiset.mem_singleton_self _) end theorem unique_factorization_monoid.of_exists_prime_factors : unique_factorization_monoid α := { irreducible_iff_prime := λ _, irreducible_iff_prime_of_exists_prime_factors pf, .. wf_dvd_monoid.of_exists_prime_factors pf } end exists_prime_factors theorem unique_factorization_monoid.iff_exists_prime_factors [cancel_comm_monoid_with_zero α] : unique_factorization_monoid α ↔ (∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, prime b) ∧ f.prod ~ᵤ a) := ⟨λ h, @unique_factorization_monoid.exists_prime_factors _ _ h, unique_factorization_monoid.of_exists_prime_factors⟩ section variables {β : Type*} [cancel_comm_monoid_with_zero α] [cancel_comm_monoid_with_zero β] lemma mul_equiv.unique_factorization_monoid (e : α ≃* β) (hα : unique_factorization_monoid α) : unique_factorization_monoid β := begin rw unique_factorization_monoid.iff_exists_prime_factors at hα ⊢, intros a ha, obtain ⟨w,hp,u,h⟩ := hα (e.symm a) (λ h, ha $ by { convert ← map_zero e, simp [← h] }), exact ⟨ w.map e, λ b hb, let ⟨c,hc,he⟩ := multiset.mem_map.1 hb in he ▸ e.prime_iff.1 (hp c hc), units.map e.to_monoid_hom u, by { erw [multiset.prod_hom, ← e.map_mul, h], simp } ⟩, end lemma mul_equiv.unique_factorization_monoid_iff (e : α ≃* β) : unique_factorization_monoid α ↔ unique_factorization_monoid β := ⟨ e.unique_factorization_monoid, e.symm.unique_factorization_monoid ⟩ end theorem irreducible_iff_prime_of_exists_unique_irreducible_factors [cancel_comm_monoid_with_zero α] (eif : ∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, irreducible b) ∧ f.prod ~ᵤ a) (uif : ∀ (f g : multiset α), (∀ x ∈ f, irreducible x) → (∀ x ∈ g, irreducible x) → f.prod ~ᵤ g.prod → multiset.rel associated f g) (p : α) : irreducible p ↔ prime p := ⟨by letI := classical.dec_eq α; exact λ hpi, ⟨hpi.ne_zero, hpi.1, λ a b ⟨x, hx⟩, if hab0 : a * b = 0 then (eq_zero_or_eq_zero_of_mul_eq_zero hab0).elim (λ ha0, by simp [ha0]) (λ hb0, by simp [hb0]) else have hx0 : x ≠ 0, from λ hx0, by simp * at *, have ha0 : a ≠ 0, from left_ne_zero_of_mul hab0, have hb0 : b ≠ 0, from right_ne_zero_of_mul hab0, begin cases eif x hx0 with fx hfx, cases eif a ha0 with fa hfa, cases eif b hb0 with fb hfb, have h : multiset.rel associated (p ::ₘ fx) (fa + fb), { apply uif, { exact λ i hi, (multiset.mem_cons.1 hi).elim (λ hip, hip.symm ▸ hpi) (hfx.1 _), }, { exact λ i hi, (multiset.mem_add.1 hi).elim (hfa.1 _) (hfb.1 _), }, calc multiset.prod (p ::ₘ fx) ~ᵤ a * b : by rw [hx, multiset.prod_cons]; exact hfx.2.mul_left _ ... ~ᵤ (fa).prod * (fb).prod : hfa.2.symm.mul_mul hfb.2.symm ... = _ : by rw multiset.prod_add, }, exact let ⟨q, hqf, hq⟩ := multiset.exists_mem_of_rel_of_mem h (multiset.mem_cons_self p _) in (multiset.mem_add.1 hqf).elim (λ hqa, or.inl $ hq.dvd_iff_dvd_left.2 $ hfa.2.dvd_iff_dvd_right.1 (multiset.dvd_prod hqa)) (λ hqb, or.inr $ hq.dvd_iff_dvd_left.2 $ hfb.2.dvd_iff_dvd_right.1 (multiset.dvd_prod hqb)) end⟩, prime.irreducible⟩ theorem unique_factorization_monoid.of_exists_unique_irreducible_factors [cancel_comm_monoid_with_zero α] (eif : ∀ (a : α), a ≠ 0 → ∃ f : multiset α, (∀b ∈ f, irreducible b) ∧ f.prod ~ᵤ a) (uif : ∀ (f g : multiset α), (∀ x ∈ f, irreducible x) → (∀ x ∈ g, irreducible x) → f.prod ~ᵤ g.prod → multiset.rel associated f g) : unique_factorization_monoid α := unique_factorization_monoid.of_exists_prime_factors (by { convert eif, simp_rw irreducible_iff_prime_of_exists_unique_irreducible_factors eif uif }) namespace unique_factorization_monoid variables [cancel_comm_monoid_with_zero α] [decidable_eq α] variables [unique_factorization_monoid α] /-- Noncomputably determines the multiset of prime factors. -/ noncomputable def factors (a : α) : multiset α := if h : a = 0 then 0 else classical.some (unique_factorization_monoid.exists_prime_factors a h) theorem factors_prod {a : α} (ane0 : a ≠ 0) : associated (factors a).prod a := begin rw [factors, dif_neg ane0], exact (classical.some_spec (exists_prime_factors a ane0)).2 end lemma ne_zero_of_mem_factors {p a : α} (h : p ∈ factors a) : a ≠ 0 := begin intro ha, rw [factors, dif_pos ha] at h, exact multiset.not_mem_zero _ h end lemma dvd_of_mem_factors {p a : α} (h : p ∈ factors a) : p ∣ a := dvd_trans (multiset.dvd_prod h) (associated.dvd (factors_prod (ne_zero_of_mem_factors h))) theorem prime_of_factor {a : α} (x : α) (hx : x ∈ factors a) : prime x := begin have ane0 := ne_zero_of_mem_factors hx, rw [factors, dif_neg ane0] at hx, exact (classical.some_spec (unique_factorization_monoid.exists_prime_factors a ane0)).1 x hx, end theorem irreducible_of_factor {a : α} : ∀ (x : α), x ∈ factors a → irreducible x := λ x h, (prime_of_factor x h).irreducible @[simp] lemma factors_zero : factors (0 : α) = 0 := by simp [factors] @[simp] lemma factors_one : factors (1 : α) = 0 := begin nontriviality α using [factors], rw ← multiset.rel_zero_right, refine factors_unique irreducible_of_factor (λ x hx, (multiset.not_mem_zero x hx).elim) _, rw multiset.prod_zero, exact factors_prod one_ne_zero, end lemma exists_mem_factors_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) : p ∣ a → ∃ q ∈ factors a, p ~ᵤ q := λ ⟨b, hb⟩, have hb0 : b ≠ 0, from λ hb0, by simp * at *, have multiset.rel associated (p ::ₘ factors b) (factors a), from factors_unique (λ x hx, (multiset.mem_cons.1 hx).elim (λ h, h.symm ▸ hp) (irreducible_of_factor _)) irreducible_of_factor (associated.symm $ calc multiset.prod (factors a) ~ᵤ a : factors_prod ha0 ... = p * b : hb ... ~ᵤ multiset.prod (p ::ₘ factors b) : by rw multiset.prod_cons; exact (factors_prod hb0).symm.mul_left _), multiset.exists_mem_of_rel_of_mem this (by simp) lemma factors_mul {x y : α} (hx : x ≠ 0) (hy : y ≠ 0) : multiset.rel associated (factors (x * y)) (factors x + factors y) := begin refine factors_unique irreducible_of_factor (λ a ha, (multiset.mem_add.mp ha).by_cases (irreducible_of_factor _) (irreducible_of_factor _)) ((factors_prod (mul_ne_zero hx hy)).trans _), rw multiset.prod_add, exact (associated.mul_mul (factors_prod hx) (factors_prod hy)).symm, end lemma factors_pow {x : α} (n : ℕ) : multiset.rel associated (factors (x ^ n)) (n • factors x) := begin induction n with n ih, { simp }, by_cases h0 : x = 0, { simp [h0, zero_pow n.succ_pos, smul_zero] }, rw [pow_succ, succ_nsmul], refine multiset.rel.trans _ (factors_mul h0 (pow_ne_zero n h0)) _, refine multiset.rel.add _ ih, exact multiset.rel_refl_of_refl_on (λ y hy, associated.refl _), end end unique_factorization_monoid namespace unique_factorization_monoid variables [cancel_comm_monoid_with_zero α] [decidable_eq α] [normalization_monoid α] variables [unique_factorization_monoid α] /-- Noncomputably determines the multiset of prime factors. -/ noncomputable def normalized_factors (a : α) : multiset α := multiset.map normalize $ factors a /-- An arbitrary choice of factors of `x : M` is exactly the (unique) normalized set of factors, if `M` has a trivial group of units. -/ @[simp] lemma factors_eq_normalized_factors {M : Type*} [cancel_comm_monoid_with_zero M] [decidable_eq M] [unique_factorization_monoid M] [unique (Mˣ)] (x : M) : factors x = normalized_factors x := begin unfold normalized_factors, convert (multiset.map_id (factors x)).symm, ext p, exact normalize_eq p end theorem normalized_factors_prod {a : α} (ane0 : a ≠ 0) : associated (normalized_factors a).prod a := begin rw [normalized_factors, factors, dif_neg ane0], refine associated.trans _ (classical.some_spec (exists_prime_factors a ane0)).2, rw [← associates.mk_eq_mk_iff_associated, ← associates.prod_mk, ← associates.prod_mk, multiset.map_map], congr' 2, ext, rw [function.comp_apply, associates.mk_normalize], end theorem prime_of_normalized_factor {a : α} : ∀ (x : α), x ∈ normalized_factors a → prime x := begin rw [normalized_factors, factors], split_ifs with ane0, { simp }, intros x hx, rcases multiset.mem_map.1 hx with ⟨y, ⟨hy, rfl⟩⟩, rw (normalize_associated _).prime_iff, exact (classical.some_spec (unique_factorization_monoid.exists_prime_factors a ane0)).1 y hy, end theorem irreducible_of_normalized_factor {a : α} : ∀ (x : α), x ∈ normalized_factors a → irreducible x := λ x h, (prime_of_normalized_factor x h).irreducible theorem normalize_normalized_factor {a : α} : ∀ (x : α), x ∈ normalized_factors a → normalize x = x := begin rw [normalized_factors, factors], split_ifs with h, { simp }, intros x hx, obtain ⟨y, hy, rfl⟩ := multiset.mem_map.1 hx, apply normalize_idem end lemma normalized_factors_irreducible {a : α} (ha : irreducible a) : normalized_factors a = {normalize a} := begin obtain ⟨p, a_assoc, hp⟩ := prime_factors_irreducible ha ⟨prime_of_normalized_factor, normalized_factors_prod ha.ne_zero⟩, have p_mem : p ∈ normalized_factors a, { rw hp, exact multiset.mem_singleton_self _ }, convert hp, rwa [← normalize_normalized_factor p p_mem, normalize_eq_normalize_iff, dvd_dvd_iff_associated] end lemma exists_mem_normalized_factors_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) : p ∣ a → ∃ q ∈ normalized_factors a, p ~ᵤ q := λ ⟨b, hb⟩, have hb0 : b ≠ 0, from λ hb0, by simp * at *, have multiset.rel associated (p ::ₘ normalized_factors b) (normalized_factors a), from factors_unique (λ x hx, (multiset.mem_cons.1 hx).elim (λ h, h.symm ▸ hp) (irreducible_of_normalized_factor _)) irreducible_of_normalized_factor (associated.symm $ calc multiset.prod (normalized_factors a) ~ᵤ a : normalized_factors_prod ha0 ... = p * b : hb ... ~ᵤ multiset.prod (p ::ₘ normalized_factors b) : by rw multiset.prod_cons; exact (normalized_factors_prod hb0).symm.mul_left _), multiset.exists_mem_of_rel_of_mem this (by simp) @[simp] lemma normalized_factors_zero : normalized_factors (0 : α) = 0 := by simp [normalized_factors, factors] @[simp] lemma normalized_factors_one : normalized_factors (1 : α) = 0 := begin nontriviality α using [normalized_factors, factors], rw ← multiset.rel_zero_right, apply factors_unique irreducible_of_normalized_factor, { intros x hx, exfalso, apply multiset.not_mem_zero x hx }, { simp [normalized_factors_prod (@one_ne_zero α _ _)] }, apply_instance end @[simp] lemma normalized_factors_mul {x y : α} (hx : x ≠ 0) (hy : y ≠ 0) : normalized_factors (x * y) = normalized_factors x + normalized_factors y := begin have h : (normalize : α → α) = associates.out ∘ associates.mk, { ext, rw [function.comp_apply, associates.out_mk], }, rw [← multiset.map_id' (normalized_factors (x * y)), ← multiset.map_id' (normalized_factors x), ← multiset.map_id' (normalized_factors y), ← multiset.map_congr rfl normalize_normalized_factor, ← multiset.map_congr rfl normalize_normalized_factor, ← multiset.map_congr rfl normalize_normalized_factor, ← multiset.map_add, h, ← multiset.map_map associates.out, eq_comm, ← multiset.map_map associates.out], refine congr rfl _, apply multiset.map_mk_eq_map_mk_of_rel, apply factors_unique, { intros x hx, rcases multiset.mem_add.1 hx with hx | hx; exact irreducible_of_normalized_factor x hx }, { exact irreducible_of_normalized_factor }, { rw multiset.prod_add, exact ((normalized_factors_prod hx).mul_mul (normalized_factors_prod hy)).trans (normalized_factors_prod (mul_ne_zero hx hy)).symm } end @[simp] lemma normalized_factors_pow {x : α} (n : ℕ) : normalized_factors (x ^ n) = n • normalized_factors x := begin induction n with n ih, { simp }, by_cases h0 : x = 0, { simp [h0, zero_pow n.succ_pos, smul_zero] }, rw [pow_succ, succ_nsmul, normalized_factors_mul h0 (pow_ne_zero _ h0), ih], end theorem _root_.irreducible.normalized_factors_pow {p : α} (hp : irreducible p) (k : ℕ) : normalized_factors (p ^ k) = multiset.repeat (normalize p) k := by rw [normalized_factors_pow, normalized_factors_irreducible hp, multiset.nsmul_singleton] lemma dvd_iff_normalized_factors_le_normalized_factors {x y : α} (hx : x ≠ 0) (hy : y ≠ 0) : x ∣ y ↔ normalized_factors x ≤ normalized_factors y := begin split, { rintro ⟨c, rfl⟩, simp [hx, right_ne_zero_of_mul hy] }, { rw [← (normalized_factors_prod hx).dvd_iff_dvd_left, ← (normalized_factors_prod hy).dvd_iff_dvd_right], apply multiset.prod_dvd_prod_of_le } end lemma associated_iff_normalized_factors_eq_normalized_factors {x y : α} (hx : x ≠ 0) (hy : y ≠ 0) : x ~ᵤ y ↔ normalized_factors x = normalized_factors y := begin refine ⟨λ h, _, λ h, (normalized_factors_prod hx).symm.trans (trans (by rw h) (normalized_factors_prod hy))⟩, apply le_antisymm; rw [← dvd_iff_normalized_factors_le_normalized_factors], all_goals { simp [*, h.dvd, h.symm.dvd], }, end theorem normalized_factors_of_irreducible_pow {p : α} (hp : irreducible p) (k : ℕ) : normalized_factors (p ^ k) = multiset.repeat (normalize p) k := by rw [normalized_factors_pow, normalized_factors_irreducible hp, multiset.nsmul_singleton] lemma zero_not_mem_normalized_factors (x : α) : (0 : α) ∉ normalized_factors x := λ h, prime.ne_zero (prime_of_normalized_factor _ h) rfl lemma dvd_of_mem_normalized_factors {a p : α} (H : p ∈ normalized_factors a) : p ∣ a := begin by_cases hcases : a = 0, { rw hcases, exact dvd_zero p }, { exact dvd_trans (multiset.dvd_prod H) (associated.dvd (normalized_factors_prod hcases)) }, end lemma exists_associated_prime_pow_of_unique_normalized_factor {p r : α} (h : ∀ {m}, m ∈ normalized_factors r → m = p) (hr : r ≠ 0) : ∃ (i : ℕ), associated (p ^ i) r := begin use (normalized_factors r).card, have := unique_factorization_monoid.normalized_factors_prod hr, rwa [multiset.eq_repeat_of_mem (λ b, h), multiset.prod_repeat] at this end lemma normalized_factors_prod_of_prime [nontrivial α] [unique αˣ] {m : multiset α} (h : ∀ p ∈ m, prime p) : (normalized_factors m.prod) = m := by simpa only [←multiset.rel_eq, ←associated_eq_eq] using prime_factors_unique (prime_of_normalized_factor) h (normalized_factors_prod (m.prod_ne_zero_of_prime h)) lemma mem_normalized_factors_eq_of_associated {a b c : α} (ha : a ∈ normalized_factors c) (hb : b ∈ normalized_factors c) (h : associated a b) : a = b := begin rw [← normalize_normalized_factor a ha, ← normalize_normalized_factor b hb, normalize_eq_normalize_iff], apply associated.dvd_dvd h, end end unique_factorization_monoid namespace unique_factorization_monoid open_locale classical open multiset associates noncomputable theory variables [cancel_comm_monoid_with_zero α] [nontrivial α] [unique_factorization_monoid α] /-- Noncomputably defines a `normalization_monoid` structure on a `unique_factorization_monoid`. -/ protected def normalization_monoid : normalization_monoid α := normalization_monoid_of_monoid_hom_right_inverse { to_fun := λ a : associates α, if a = 0 then 0 else ((normalized_factors a).map (classical.some mk_surjective.has_right_inverse : associates α → α)).prod, map_one' := by simp, map_mul' := λ x y, by { by_cases hx : x = 0, { simp [hx] }, by_cases hy : y = 0, { simp [hy] }, simp [hx, hy] } } begin intro x, dsimp, by_cases hx : x = 0, { simp [hx] }, have h : associates.mk_monoid_hom ∘ (classical.some mk_surjective.has_right_inverse) = (id : associates α → associates α), { ext x, rw [function.comp_apply, mk_monoid_hom_apply, classical.some_spec mk_surjective.has_right_inverse x], refl }, rw [if_neg hx, ← mk_monoid_hom_apply, monoid_hom.map_multiset_prod, map_map, h, map_id, ← associated_iff_eq], apply normalized_factors_prod hx end instance : inhabited (normalization_monoid α) := ⟨unique_factorization_monoid.normalization_monoid⟩ end unique_factorization_monoid namespace unique_factorization_monoid variables {R : Type*} [cancel_comm_monoid_with_zero R] [unique_factorization_monoid R] lemma no_factors_of_no_prime_factors {a b : R} (ha : a ≠ 0) (h : (∀ {d}, d ∣ a → d ∣ b → ¬ prime d)) : ∀ {d}, d ∣ a → d ∣ b → is_unit d := λ d, induction_on_prime d (by { simp only [zero_dvd_iff], intros, contradiction }) (λ x hx _ _, hx) (λ d q hp hq ih dvd_a dvd_b, absurd hq (h (dvd_of_mul_right_dvd dvd_a) (dvd_of_mul_right_dvd dvd_b))) /-- Euclid's lemma: if `a ∣ b * c` and `a` and `c` have no common prime factors, `a ∣ b`. Compare `is_coprime.dvd_of_dvd_mul_left`. -/ lemma dvd_of_dvd_mul_left_of_no_prime_factors {a b c : R} (ha : a ≠ 0) : (∀ {d}, d ∣ a → d ∣ c → ¬ prime d) → a ∣ b * c → a ∣ b := begin refine induction_on_prime c _ _ _, { intro no_factors, simp only [dvd_zero, mul_zero, forall_prop_of_true], haveI := classical.prop_decidable, exact is_unit_iff_forall_dvd.mp (no_factors_of_no_prime_factors ha @no_factors (dvd_refl a) (dvd_zero a)) _ }, { rintros _ ⟨x, rfl⟩ _ a_dvd_bx, apply units.dvd_mul_right.mp a_dvd_bx }, { intros c p hc hp ih no_factors a_dvd_bpc, apply ih (λ q dvd_a dvd_c hq, no_factors dvd_a (dvd_c.mul_left _) hq), rw mul_left_comm at a_dvd_bpc, refine or.resolve_left (hp.left_dvd_or_dvd_right_of_dvd_mul a_dvd_bpc) (λ h, _), exact no_factors h (dvd_mul_right p c) hp } end /-- Euclid's lemma: if `a ∣ b * c` and `a` and `b` have no common prime factors, `a ∣ c`. Compare `is_coprime.dvd_of_dvd_mul_right`. -/ lemma dvd_of_dvd_mul_right_of_no_prime_factors {a b c : R} (ha : a ≠ 0) (no_factors : ∀ {d}, d ∣ a → d ∣ b → ¬ prime d) : a ∣ b * c → a ∣ c := by simpa [mul_comm b c] using dvd_of_dvd_mul_left_of_no_prime_factors ha @no_factors /-- If `a ≠ 0, b` are elements of a unique factorization domain, then dividing out their common factor `c'` gives `a'` and `b'` with no factors in common. -/ lemma exists_reduced_factors : ∀ (a ≠ (0 : R)) b, ∃ a' b' c', (∀ {d}, d ∣ a' → d ∣ b' → is_unit d) ∧ c' * a' = a ∧ c' * b' = b := begin haveI := classical.prop_decidable, intros a, refine induction_on_prime a _ _ _, { intros, contradiction }, { intros a a_unit a_ne_zero b, use [a, b, 1], split, { intros p p_dvd_a _, exact is_unit_of_dvd_unit p_dvd_a a_unit }, { simp } }, { intros a p a_ne_zero p_prime ih_a pa_ne_zero b, by_cases p ∣ b, { rcases h with ⟨b, rfl⟩, obtain ⟨a', b', c', no_factor, ha', hb'⟩ := ih_a a_ne_zero b, refine ⟨a', b', p * c', @no_factor, _, _⟩, { rw [mul_assoc, ha'] }, { rw [mul_assoc, hb'] } }, { obtain ⟨a', b', c', coprime, rfl, rfl⟩ := ih_a a_ne_zero b, refine ⟨p * a', b', c', _, mul_left_comm _ _ _, rfl⟩, intros q q_dvd_pa' q_dvd_b', cases p_prime.left_dvd_or_dvd_right_of_dvd_mul q_dvd_pa' with p_dvd_q q_dvd_a', { have : p ∣ c' * b' := dvd_mul_of_dvd_right (p_dvd_q.trans q_dvd_b') _, contradiction }, exact coprime q_dvd_a' q_dvd_b' } } end lemma exists_reduced_factors' (a b : R) (hb : b ≠ 0) : ∃ a' b' c', (∀ {d}, d ∣ a' → d ∣ b' → is_unit d) ∧ c' * a' = a ∧ c' * b' = b := let ⟨b', a', c', no_factor, hb, ha⟩ := exists_reduced_factors b hb a in ⟨a', b', c', λ _ hpb hpa, no_factor hpa hpb, ha, hb⟩ section multiplicity variables [nontrivial R] [normalization_monoid R] [decidable_eq R] variables [dec_dvd : decidable_rel (has_dvd.dvd : R → R → Prop)] open multiplicity multiset include dec_dvd lemma le_multiplicity_iff_repeat_le_normalized_factors {a b : R} {n : ℕ} (ha : irreducible a) (hb : b ≠ 0) : ↑n ≤ multiplicity a b ↔ repeat (normalize a) n ≤ normalized_factors b := begin rw ← pow_dvd_iff_le_multiplicity, revert b, induction n with n ih, { simp }, intros b hb, split, { rintro ⟨c, rfl⟩, rw [ne.def, pow_succ, mul_assoc, mul_eq_zero, decidable.not_or_iff_and_not] at hb, rw [pow_succ, mul_assoc, normalized_factors_mul hb.1 hb.2, repeat_succ, normalized_factors_irreducible ha, singleton_add, cons_le_cons_iff, ← ih hb.2], apply dvd.intro _ rfl }, { rw [multiset.le_iff_exists_add], rintro ⟨u, hu⟩, rw [← (normalized_factors_prod hb).dvd_iff_dvd_right, hu, prod_add, prod_repeat], exact (associated.pow_pow $ associated_normalize a).dvd.trans (dvd.intro u.prod rfl) } end /-- The multiplicity of an irreducible factor of a nonzero element is exactly the number of times the normalized factor occurs in the `normalized_factors`. See also `count_normalized_factors_eq` which expands the definition of `multiplicity` to produce a specification for `count (normalized_factors _) _`.. -/ lemma multiplicity_eq_count_normalized_factors {a b : R} (ha : irreducible a) (hb : b ≠ 0) : multiplicity a b = (normalized_factors b).count (normalize a) := begin apply le_antisymm, { apply part_enat.le_of_lt_add_one, rw [← nat.cast_one, ← nat.cast_add, lt_iff_not_ge, ge_iff_le, le_multiplicity_iff_repeat_le_normalized_factors ha hb, ← le_count_iff_repeat_le], simp }, rw [le_multiplicity_iff_repeat_le_normalized_factors ha hb, ← le_count_iff_repeat_le], end omit dec_dvd /-- The number of times an irreducible factor `p` appears in `normalized_factors x` is defined by the number of times it divides `x`. See also `multiplicity_eq_count_normalized_factors` if `n` is given by `multiplicity p x`. -/ lemma count_normalized_factors_eq {p x : R} (hp : irreducible p) (hnorm : normalize p = p) {n : ℕ} (hle : p^n ∣ x) (hlt : ¬ (p^(n+1) ∣ x)) : (normalized_factors x).count p = n := begin letI : decidable_rel ((∣) : R → R → Prop) := λ _ _, classical.prop_decidable _, by_cases hx0 : x = 0, { simp [hx0] at hlt, contradiction }, rw [← part_enat.coe_inj], convert (multiplicity_eq_count_normalized_factors hp hx0).symm, { exact hnorm.symm }, exact (multiplicity.eq_coe_iff.mpr ⟨hle, hlt⟩).symm end /-- The number of times an irreducible factor `p` appears in `normalized_factors x` is defined by the number of times it divides `x`. This is a slightly more general version of `unique_factorization_monoid.count_normalized_factors_eq` that allows `p = 0`. See also `multiplicity_eq_count_normalized_factors` if `n` is given by `multiplicity p x`. -/ lemma count_normalized_factors_eq' {p x : R} (hp : p = 0 ∨ irreducible p) (hnorm : normalize p = p) {n : ℕ} (hle : p^n ∣ x) (hlt : ¬ (p^(n+1) ∣ x)) : (normalized_factors x).count p = n := begin rcases hp with rfl|hp, { cases n, { exact count_eq_zero.2 (zero_not_mem_normalized_factors _) }, { rw [zero_pow (nat.succ_pos _)] at hle hlt, exact absurd hle hlt } }, { exact count_normalized_factors_eq hp hnorm hle hlt } end end multiplicity end unique_factorization_monoid namespace associates open unique_factorization_monoid associated multiset variables [cancel_comm_monoid_with_zero α] /-- `factor_set α` representation elements of unique factorization domain as multisets. `multiset α` produced by `normalized_factors` are only unique up to associated elements, while the multisets in `factor_set α` are unique by equality and restricted to irreducible elements. This gives us a representation of each element as a unique multisets (or the added ⊤ for 0), which has a complete lattice struture. Infimum is the greatest common divisor and supremum is the least common multiple. -/ @[reducible] def {u} factor_set (α : Type u) [cancel_comm_monoid_with_zero α] : Type u := with_top (multiset { a : associates α // irreducible a }) local attribute [instance] associated.setoid theorem factor_set.coe_add {a b : multiset { a : associates α // irreducible a }} : (↑(a + b) : factor_set α) = a + b := by norm_cast lemma factor_set.sup_add_inf_eq_add [decidable_eq (associates α)] : ∀(a b : factor_set α), a ⊔ b + a ⊓ b = a + b | none b := show ⊤ ⊔ b + ⊤ ⊓ b = ⊤ + b, by simp | a none := show a ⊔ ⊤ + a ⊓ ⊤ = a + ⊤, by simp | (some a) (some b) := show (a : factor_set α) ⊔ b + a ⊓ b = a + b, from begin rw [← with_top.coe_sup, ← with_top.coe_inf, ← with_top.coe_add, ← with_top.coe_add, with_top.coe_eq_coe], exact multiset.union_add_inter _ _ end /-- Evaluates the product of a `factor_set` to be the product of the corresponding multiset, or `0` if there is none. -/ def factor_set.prod : factor_set α → associates α | none := 0 | (some s) := (s.map coe).prod @[simp] theorem prod_top : (⊤ : factor_set α).prod = 0 := rfl @[simp] theorem prod_coe {s : multiset { a : associates α // irreducible a }} : (s : factor_set α).prod = (s.map coe).prod := rfl @[simp] theorem prod_add : ∀(a b : factor_set α), (a + b).prod = a.prod * b.prod | none b := show (⊤ + b).prod = (⊤:factor_set α).prod * b.prod, by simp | a none := show (a + ⊤).prod = a.prod * (⊤:factor_set α).prod, by simp | (some a) (some b) := show (↑a + ↑b:factor_set α).prod = (↑a:factor_set α).prod * (↑b:factor_set α).prod, by rw [← factor_set.coe_add, prod_coe, prod_coe, prod_coe, multiset.map_add, multiset.prod_add] theorem prod_mono : ∀{a b : factor_set α}, a ≤ b → a.prod ≤ b.prod | none b h := have b = ⊤, from top_unique h, by rw [this, prod_top]; exact le_rfl | a none h := show a.prod ≤ (⊤ : factor_set α).prod, by simp; exact le_top | (some a) (some b) h := prod_le_prod $ multiset.map_le_map $ with_top.coe_le_coe.1 $ h theorem factor_set.prod_eq_zero_iff [nontrivial α] (p : factor_set α) : p.prod = 0 ↔ p = ⊤ := begin induction p using with_top.rec_top_coe, { simp only [iff_self, eq_self_iff_true, associates.prod_top] }, simp only [prod_coe, with_top.coe_ne_top, iff_false, prod_eq_zero_iff, multiset.mem_map], rintro ⟨⟨a, ha⟩, -, eq⟩, rw [subtype.coe_mk] at eq, exact ha.ne_zero eq, end /-- `bcount p s` is the multiplicity of `p` in the factor_set `s` (with bundled `p`)-/ def bcount [decidable_eq (associates α)] (p : {a : associates α // irreducible a}) : factor_set α → ℕ | none := 0 | (some s) := s.count p variables [dec_irr : Π (p : associates α), decidable (irreducible p)] include dec_irr /-- `count p s` is the multiplicity of the irreducible `p` in the factor_set `s`. If `p` is not irreducible, `count p s` is defined to be `0`. -/ def count [decidable_eq (associates α)] (p : associates α) : factor_set α → ℕ := if hp : irreducible p then bcount ⟨p, hp⟩ else 0 @[simp] lemma count_some [decidable_eq (associates α)] {p : associates α} (hp : irreducible p) (s : multiset _) : count p (some s) = s.count ⟨p, hp⟩:= by { dunfold count, split_ifs, refl } @[simp] lemma count_zero [decidable_eq (associates α)] {p : associates α} (hp : irreducible p) : count p (0 : factor_set α) = 0 := by { dunfold count, split_ifs, refl } lemma count_reducible [decidable_eq (associates α)] {p : associates α} (hp : ¬ irreducible p) : count p = 0 := dif_neg hp omit dec_irr /-- membership in a factor_set (bundled version) -/ def bfactor_set_mem : {a : associates α // irreducible a} → (factor_set α) → Prop | _ ⊤ := true | p (some l) := p ∈ l include dec_irr /-- `factor_set_mem p s` is the predicate that the irreducible `p` is a member of `s : factor_set α`. If `p` is not irreducible, `p` is not a member of any `factor_set`. -/ def factor_set_mem (p : associates α) (s : factor_set α) : Prop := if hp : irreducible p then bfactor_set_mem ⟨p, hp⟩ s else false instance : has_mem (associates α) (factor_set α) := ⟨factor_set_mem⟩ @[simp] lemma factor_set_mem_eq_mem (p : associates α) (s : factor_set α) : factor_set_mem p s = (p ∈ s) := rfl lemma mem_factor_set_top {p : associates α} {hp : irreducible p} : p ∈ (⊤ : factor_set α) := begin dunfold has_mem.mem, dunfold factor_set_mem, split_ifs, exact trivial end lemma mem_factor_set_some {p : associates α} {hp : irreducible p} {l : multiset {a : associates α // irreducible a }} : p ∈ (l : factor_set α) ↔ subtype.mk p hp ∈ l := begin dunfold has_mem.mem, dunfold factor_set_mem, split_ifs, refl end lemma reducible_not_mem_factor_set {p : associates α} (hp : ¬ irreducible p) (s : factor_set α) : ¬ p ∈ s := λ (h : if hp : irreducible p then bfactor_set_mem ⟨p, hp⟩ s else false), by rwa [dif_neg hp] at h omit dec_irr variable [unique_factorization_monoid α] theorem unique' {p q : multiset (associates α)} : (∀a∈p, irreducible a) → (∀a∈q, irreducible a) → p.prod = q.prod → p = q := begin apply multiset.induction_on_multiset_quot p, apply multiset.induction_on_multiset_quot q, assume s t hs ht eq, refine multiset.map_mk_eq_map_mk_of_rel (unique_factorization_monoid.factors_unique _ _ _), { exact assume a ha, ((irreducible_mk _).1 $ hs _ $ multiset.mem_map_of_mem _ ha) }, { exact assume a ha, ((irreducible_mk _).1 $ ht _ $ multiset.mem_map_of_mem _ ha) }, simpa [quot_mk_eq_mk, prod_mk, mk_eq_mk_iff_associated] using eq end theorem factor_set.unique [nontrivial α] {p q : factor_set α} (h : p.prod = q.prod) : p = q := begin induction p using with_top.rec_top_coe; induction q using with_top.rec_top_coe, { refl }, { rw [eq_comm, ←factor_set.prod_eq_zero_iff, ←h, associates.prod_top] }, { rw [←factor_set.prod_eq_zero_iff, h, associates.prod_top] }, { congr' 1, rw ←multiset.map_eq_map subtype.coe_injective, apply unique' _ _ h; { intros a ha, obtain ⟨⟨a', irred⟩, -, rfl⟩ := multiset.mem_map.mp ha, rwa [subtype.coe_mk] } }, end theorem prod_le_prod_iff_le [nontrivial α] {p q : multiset (associates α)} (hp : ∀a∈p, irreducible a) (hq : ∀a∈q, irreducible a) : p.prod ≤ q.prod ↔ p ≤ q := iff.intro begin classical, rintros ⟨c, eqc⟩, refine multiset.le_iff_exists_add.2 ⟨factors c, unique' hq (λ x hx, _) _⟩, { obtain h|h := multiset.mem_add.1 hx, { exact hp x h }, { exact irreducible_of_factor _ h } }, { rw [eqc, multiset.prod_add], congr, refine associated_iff_eq.mp (factors_prod (λ hc, _)).symm, refine not_irreducible_zero (hq _ _), rw [←prod_eq_zero_iff, eqc, hc, mul_zero] } end prod_le_prod variables [dec : decidable_eq α] [dec' : decidable_eq (associates α)] include dec /-- This returns the multiset of irreducible factors as a `factor_set`, a multiset of irreducible associates `with_top`. -/ noncomputable def factors' (a : α) : multiset { a : associates α // irreducible a } := (factors a).pmap (λa ha, ⟨associates.mk a, (irreducible_mk _).2 ha⟩) (irreducible_of_factor) @[simp] theorem map_subtype_coe_factors' {a : α} : (factors' a).map coe = (factors a).map associates.mk := by simp [factors', multiset.map_pmap, multiset.pmap_eq_map] theorem factors'_cong {a b : α} (h : a ~ᵤ b) : factors' a = factors' b := begin obtain rfl|hb := eq_or_ne b 0, { rw associated_zero_iff_eq_zero at h, rw h }, have ha : a ≠ 0, { contrapose! hb with ha, rw [←associated_zero_iff_eq_zero, ←ha], exact h.symm }, rw [←multiset.map_eq_map subtype.coe_injective, map_subtype_coe_factors', map_subtype_coe_factors', ←rel_associated_iff_map_eq_map], exact factors_unique irreducible_of_factor irreducible_of_factor ((factors_prod ha).trans $ h.trans $ (factors_prod hb).symm), end include dec' /-- This returns the multiset of irreducible factors of an associate as a `factor_set`, a multiset of irreducible associates `with_top`. -/ noncomputable def factors (a : associates α) : factor_set α := begin refine (if h : a = 0 then ⊤ else quotient.hrec_on a (λx h, some $ factors' x) _ h), assume a b hab, apply function.hfunext, { have : a ~ᵤ 0 ↔ b ~ᵤ 0, from iff.intro (assume ha0, hab.symm.trans ha0) (assume hb0, hab.trans hb0), simp only [associated_zero_iff_eq_zero] at this, simp only [quotient_mk_eq_mk, this, mk_eq_zero] }, exact (assume ha hb eq, heq_of_eq $ congr_arg some $ factors'_cong hab) end @[simp] theorem factors_0 : (0 : associates α).factors = ⊤ := dif_pos rfl @[simp] theorem factors_mk (a : α) (h : a ≠ 0) : (associates.mk a).factors = factors' a := by { classical, apply dif_neg, apply (mt mk_eq_zero.1 h) } @[simp] theorem factors_prod (a : associates α) : a.factors.prod = a := quotient.induction_on a $ assume a, decidable.by_cases (assume : associates.mk a = 0, by simp [quotient_mk_eq_mk, this]) (assume : associates.mk a ≠ 0, have a ≠ 0, by simp * at *, by simp [this, quotient_mk_eq_mk, prod_mk, mk_eq_mk_iff_associated.2 (factors_prod this)]) theorem prod_factors [nontrivial α] (s : factor_set α) : s.prod.factors = s := factor_set.unique $ factors_prod _ @[nontriviality] lemma factors_subsingleton [subsingleton α] {a : associates α} : a.factors = option.none := by { convert factors_0; apply_instance } lemma factors_eq_none_iff_zero {a : associates α} : a.factors = option.none ↔ a = 0 := begin nontriviality α, exact ⟨λ h, by rwa [← factors_prod a, factor_set.prod_eq_zero_iff], λ h, h.symm ▸ factors_0⟩ end lemma factors_eq_some_iff_ne_zero {a : associates α} : (∃ (s : multiset {p : associates α // irreducible p}), a.factors = some s) ↔ a ≠ 0 := by rw [← option.is_some_iff_exists, ← option.ne_none_iff_is_some, ne.def, ne.def, factors_eq_none_iff_zero] theorem eq_of_factors_eq_factors {a b : associates α} (h : a.factors = b.factors) : a = b := have a.factors.prod = b.factors.prod, by rw h, by rwa [factors_prod, factors_prod] at this omit dec dec' theorem eq_of_prod_eq_prod [nontrivial α] {a b : factor_set α} (h : a.prod = b.prod) : a = b := begin classical, have : a.prod.factors = b.prod.factors, by rw h, rwa [prod_factors, prod_factors] at this end include dec dec' dec_irr theorem eq_factors_of_eq_counts {a b : associates α} (ha : a ≠ 0) (hb : b ≠ 0) (h : ∀ (p : associates α) (hp : irreducible p), p.count a.factors = p.count b.factors) : a.factors = b.factors := begin obtain ⟨sa, h_sa⟩ := factors_eq_some_iff_ne_zero.mpr ha, obtain ⟨sb, h_sb⟩ := factors_eq_some_iff_ne_zero.mpr hb, rw [h_sa, h_sb] at h ⊢, rw option.some_inj, have h_count : ∀ (p : associates α) (hp : irreducible p), sa.count ⟨p, hp⟩ = sb.count ⟨p, hp⟩, { intros p hp, rw [← count_some, ← count_some, h p hp] }, apply multiset.to_finsupp.injective, ext ⟨p, hp⟩, rw [multiset.to_finsupp_apply, multiset.to_finsupp_apply, h_count p hp] end theorem eq_of_eq_counts {a b : associates α} (ha : a ≠ 0) (hb : b ≠ 0) (h : ∀ (p : associates α), irreducible p → p.count a.factors = p.count b.factors) : a = b := eq_of_factors_eq_factors (eq_factors_of_eq_counts ha hb h) lemma count_le_count_of_factors_le {a b p : associates α} (hb : b ≠ 0) (hp : irreducible p) (h : a.factors ≤ b.factors) : p.count a.factors ≤ p.count b.factors := begin by_cases ha : a = 0, { simp [*] at *, }, obtain ⟨sa, h_sa⟩ := factors_eq_some_iff_ne_zero.mpr ha, obtain ⟨sb, h_sb⟩ := factors_eq_some_iff_ne_zero.mpr hb, rw [h_sa, h_sb] at h ⊢, rw [count_some hp, count_some hp], rw with_top.some_le_some at h, exact multiset.count_le_of_le _ h end omit dec_irr @[simp] theorem factors_mul (a b : associates α) : (a * b).factors = a.factors + b.factors := begin casesI subsingleton_or_nontrivial α, { simp [subsingleton.elim a 0], }, refine (eq_of_prod_eq_prod (eq_of_factors_eq_factors _)), rw [prod_add, factors_prod, factors_prod, factors_prod], end theorem factors_mono : ∀{a b : associates α}, a ≤ b → a.factors ≤ b.factors | s t ⟨d, rfl⟩ := by rw [factors_mul] ; exact le_add_of_nonneg_right bot_le theorem factors_le {a b : associates α} : a.factors ≤ b.factors ↔ a ≤ b := iff.intro (assume h, have a.factors.prod ≤ b.factors.prod, from prod_mono h, by rwa [factors_prod, factors_prod] at this) factors_mono include dec_irr lemma count_le_count_of_le {a b p : associates α} (hb : b ≠ 0) (hp : irreducible p) (h : a ≤ b) : p.count a.factors ≤ p.count b.factors := count_le_count_of_factors_le hb hp $ factors_mono h omit dec dec' dec_irr theorem prod_le [nontrivial α] {a b : factor_set α} : a.prod ≤ b.prod ↔ a ≤ b := begin classical, exact iff.intro (assume h, have a.prod.factors ≤ b.prod.factors, from factors_mono h, by rwa [prod_factors, prod_factors] at this) prod_mono end include dec dec' noncomputable instance : has_sup (associates α) := ⟨λa b, (a.factors ⊔ b.factors).prod⟩ noncomputable instance : has_inf (associates α) := ⟨λa b, (a.factors ⊓ b.factors).prod⟩ noncomputable instance : lattice (associates α) := { sup := (⊔), inf := (⊓), sup_le := assume a b c hac hbc, factors_prod c ▸ prod_mono (sup_le (factors_mono hac) (factors_mono hbc)), le_sup_left := assume a b, le_trans (le_of_eq (factors_prod a).symm) $ prod_mono $ le_sup_left, le_sup_right := assume a b, le_trans (le_of_eq (factors_prod b).symm) $ prod_mono $ le_sup_right, le_inf := assume a b c hac hbc, factors_prod a ▸ prod_mono (le_inf (factors_mono hac) (factors_mono hbc)), inf_le_left := assume a b, le_trans (prod_mono inf_le_left) (le_of_eq (factors_prod a)), inf_le_right := assume a b, le_trans (prod_mono inf_le_right) (le_of_eq (factors_prod b)), .. associates.partial_order } lemma sup_mul_inf (a b : associates α) : (a ⊔ b) * (a ⊓ b) = a * b := show (a.factors ⊔ b.factors).prod * (a.factors ⊓ b.factors).prod = a * b, begin nontriviality α, refine eq_of_factors_eq_factors _, rw [← prod_add, prod_factors, factors_mul, factor_set.sup_add_inf_eq_add] end include dec_irr lemma dvd_of_mem_factors {a p : associates α} {hp : irreducible p} (hm : p ∈ factors a) : p ∣ a := begin by_cases ha0 : a = 0, { rw ha0, exact dvd_zero p }, obtain ⟨a0, nza, ha'⟩ := exists_non_zero_rep ha0, rw [← associates.factors_prod a], rw [← ha', factors_mk a0 nza] at hm ⊢, erw prod_coe, apply multiset.dvd_prod, apply multiset.mem_map.mpr, exact ⟨⟨p, hp⟩, mem_factor_set_some.mp hm, rfl⟩ end omit dec' lemma dvd_of_mem_factors' {a : α} {p : associates α} {hp : irreducible p} {hz : a ≠ 0} (h_mem : subtype.mk p hp ∈ factors' a) : p ∣ associates.mk a := by { haveI := classical.dec_eq (associates α), apply @dvd_of_mem_factors _ _ _ _ _ _ _ _ hp, rw factors_mk _ hz, apply mem_factor_set_some.2 h_mem } omit dec_irr lemma mem_factors'_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) (hd : p ∣ a) : subtype.mk (associates.mk p) ((irreducible_mk _).2 hp) ∈ factors' a := begin obtain ⟨q, hq, hpq⟩ := exists_mem_factors_of_dvd ha0 hp hd, apply multiset.mem_pmap.mpr, use q, use hq, exact subtype.eq (eq.symm (mk_eq_mk_iff_associated.mpr hpq)) end include dec_irr lemma mem_factors'_iff_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) : subtype.mk (associates.mk p) ((irreducible_mk _).2 hp) ∈ factors' a ↔ p ∣ a := begin split, { rw ← mk_dvd_mk, apply dvd_of_mem_factors', apply ha0 }, { apply mem_factors'_of_dvd ha0 } end include dec' lemma mem_factors_of_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) (hd : p ∣ a) : (associates.mk p) ∈ factors (associates.mk a) := begin rw factors_mk _ ha0, exact mem_factor_set_some.mpr (mem_factors'_of_dvd ha0 hp hd) end lemma mem_factors_iff_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) : (associates.mk p) ∈ factors (associates.mk a) ↔ p ∣ a := begin split, { rw ← mk_dvd_mk, apply dvd_of_mem_factors, exact (irreducible_mk p).mpr hp }, { apply mem_factors_of_dvd ha0 hp } end lemma exists_prime_dvd_of_not_inf_one {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) (h : (associates.mk a) ⊓ (associates.mk b) ≠ 1) : ∃ (p : α), prime p ∧ p ∣ a ∧ p ∣ b := begin have hz : (factors (associates.mk a)) ⊓ (factors (associates.mk b)) ≠ 0, { contrapose! h with hf, change ((factors (associates.mk a)) ⊓ (factors (associates.mk b))).prod = 1, rw hf, exact multiset.prod_zero }, rw [factors_mk a ha, factors_mk b hb, ← with_top.coe_inf] at hz, obtain ⟨⟨p0, p0_irr⟩, p0_mem⟩ := multiset.exists_mem_of_ne_zero ((mt with_top.coe_eq_coe.mpr) hz), rw multiset.inf_eq_inter at p0_mem, obtain ⟨p, rfl⟩ : ∃ p, associates.mk p = p0 := quot.exists_rep p0, refine ⟨p, _, _, _⟩, { rw [← irreducible_iff_prime, ← irreducible_mk], exact p0_irr }, { apply dvd_of_mk_le_mk, apply dvd_of_mem_factors' (multiset.mem_inter.mp p0_mem).left, apply ha, }, { apply dvd_of_mk_le_mk, apply dvd_of_mem_factors' (multiset.mem_inter.mp p0_mem).right, apply hb } end theorem coprime_iff_inf_one {a b : α} (ha0 : a ≠ 0) (hb0 : b ≠ 0) : (associates.mk a) ⊓ (associates.mk b) = 1 ↔ ∀ {d : α}, d ∣ a → d ∣ b → ¬ prime d := begin split, { intros hg p ha hb hp, refine ((associates.prime_mk _).mpr hp).not_unit (is_unit_of_dvd_one _ _), rw ← hg, exact le_inf (mk_le_mk_of_dvd ha) (mk_le_mk_of_dvd hb) }, { contrapose, intros hg hc, obtain ⟨p, hp, hpa, hpb⟩ := exists_prime_dvd_of_not_inf_one ha0 hb0 hg, exact hc hpa hpb hp } end omit dec_irr theorem factors_self [nontrivial α] {p : associates α} (hp : irreducible p) : p.factors = some ({⟨p, hp⟩}) := eq_of_prod_eq_prod (by rw [factors_prod, factor_set.prod, map_singleton, prod_singleton, subtype.coe_mk]) theorem factors_prime_pow [nontrivial α] {p : associates α} (hp : irreducible p) (k : ℕ) : factors (p ^ k) = some (multiset.repeat ⟨p, hp⟩ k) := eq_of_prod_eq_prod (by rw [associates.factors_prod, factor_set.prod, multiset.map_repeat, multiset.prod_repeat, subtype.coe_mk]) include dec_irr theorem prime_pow_dvd_iff_le [nontrivial α] {m p : associates α} (h₁ : m ≠ 0) (h₂ : irreducible p) {k : ℕ} : p ^ k ≤ m ↔ k ≤ count p m.factors := begin obtain ⟨a, nz, rfl⟩ := associates.exists_non_zero_rep h₁, rw [factors_mk _ nz, ← with_top.some_eq_coe, count_some, multiset.le_count_iff_repeat_le, ← factors_le, factors_prime_pow h₂, factors_mk _ nz], exact with_top.coe_le_coe end theorem le_of_count_ne_zero {m p : associates α} (h0 : m ≠ 0) (hp : irreducible p) : count p m.factors ≠ 0 → p ≤ m := begin nontriviality α, rw [← pos_iff_ne_zero], intro h, rw [← pow_one p], apply (prime_pow_dvd_iff_le h0 hp).2, simpa only end theorem count_ne_zero_iff_dvd {a p : α} (ha0 : a ≠ 0) (hp : irreducible p) : (associates.mk p).count (associates.mk a).factors ≠ 0 ↔ p ∣ a := begin nontriviality α, rw ← associates.mk_le_mk_iff_dvd_iff, refine ⟨λ h, associates.le_of_count_ne_zero (associates.mk_ne_zero.mpr ha0) ((associates.irreducible_mk p).mpr hp) h, λ h, _⟩, { rw [← pow_one (associates.mk p), associates.prime_pow_dvd_iff_le (associates.mk_ne_zero.mpr ha0) ((associates.irreducible_mk p).mpr hp)] at h, exact (zero_lt_one.trans_le h).ne' } end theorem count_self [nontrivial α] {p : associates α} (hp : irreducible p) : p.count p.factors = 1 := by simp [factors_self hp, associates.count_some hp] lemma count_eq_zero_of_ne {p q : associates α} (hp : irreducible p) (hq : irreducible q) (h : p ≠ q) : p.count q.factors = 0 := not_ne_iff.mp $ λ h', h $ associated_iff_eq.mp $ hp.associated_of_dvd hq $ by { nontriviality α, exact le_of_count_ne_zero hq.ne_zero hp h' } theorem count_mul {a : associates α} (ha : a ≠ 0) {b : associates α} (hb : b ≠ 0) {p : associates α} (hp : irreducible p) : count p (factors (a * b)) = count p a.factors + count p b.factors := begin obtain ⟨a0, nza, ha'⟩ := exists_non_zero_rep ha, obtain ⟨b0, nzb, hb'⟩ := exists_non_zero_rep hb, rw [factors_mul, ← ha', ← hb', factors_mk a0 nza, factors_mk b0 nzb, ← factor_set.coe_add, ← with_top.some_eq_coe, ← with_top.some_eq_coe, ← with_top.some_eq_coe, count_some hp, multiset.count_add, count_some hp, count_some hp] end theorem count_of_coprime {a : associates α} (ha : a ≠ 0) {b : associates α} (hb : b ≠ 0) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) {p : associates α} (hp : irreducible p) : count p a.factors = 0 ∨ count p b.factors = 0 := begin rw [or_iff_not_imp_left, ← ne.def], intro hca, contrapose! hab with hcb, exact ⟨p, le_of_count_ne_zero ha hp hca, le_of_count_ne_zero hb hp hcb, (irreducible_iff_prime.mp hp)⟩, end theorem count_mul_of_coprime {a : associates α} {b : associates α} (hb : b ≠ 0) {p : associates α} (hp : irreducible p) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) : count p a.factors = 0 ∨ count p a.factors = count p (a * b).factors := begin by_cases ha : a = 0, { simp [ha], }, cases count_of_coprime ha hb hab hp with hz hb0, { tauto }, apply or.intro_right, rw [count_mul ha hb hp, hb0, add_zero] end theorem count_mul_of_coprime' {a b : associates α} {p : associates α} (hp : irreducible p) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) : count p (a * b).factors = count p a.factors ∨ count p (a * b).factors = count p b.factors := begin by_cases ha : a = 0, { simp [ha], }, by_cases hb : b = 0, { simp [hb], }, rw [count_mul ha hb hp], cases count_of_coprime ha hb hab hp with ha0 hb0, { apply or.intro_right, rw [ha0, zero_add] }, { apply or.intro_left, rw [hb0, add_zero] } end theorem dvd_count_of_dvd_count_mul {a b : associates α} (hb : b ≠ 0) {p : associates α} (hp : irreducible p) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) {k : ℕ} (habk : k ∣ count p (a * b).factors) : k ∣ count p a.factors := begin by_cases ha : a = 0, { simpa [*] using habk, }, cases count_of_coprime ha hb hab hp with hz h, { rw hz, exact dvd_zero k }, { rw [count_mul ha hb hp, h] at habk, exact habk } end omit dec_irr @[simp] lemma factors_one [nontrivial α] : factors (1 : associates α) = 0 := begin apply eq_of_prod_eq_prod, rw associates.factors_prod, exact multiset.prod_zero, end @[simp] theorem pow_factors [nontrivial α] {a : associates α} {k : ℕ} : (a ^ k).factors = k • a.factors := begin induction k with n h, { rw [zero_nsmul, pow_zero], exact factors_one }, { rw [pow_succ, succ_nsmul, factors_mul, h] } end include dec_irr lemma count_pow [nontrivial α] {a : associates α} (ha : a ≠ 0) {p : associates α} (hp : irreducible p) (k : ℕ) : count p (a ^ k).factors = k * count p a.factors := begin induction k with n h, { rw [pow_zero, factors_one, zero_mul, count_zero hp] }, { rw [pow_succ, count_mul ha (pow_ne_zero _ ha) hp, h, nat.succ_eq_add_one], ring } end theorem dvd_count_pow [nontrivial α] {a : associates α} (ha : a ≠ 0) {p : associates α} (hp : irreducible p) (k : ℕ) : k ∣ count p (a ^ k).factors := by { rw count_pow ha hp, apply dvd_mul_right } theorem is_pow_of_dvd_count [nontrivial α] {a : associates α} (ha : a ≠ 0) {k : ℕ} (hk : ∀ (p : associates α) (hp : irreducible p), k ∣ count p a.factors) : ∃ (b : associates α), a = b ^ k := begin obtain ⟨a0, hz, rfl⟩ := exists_non_zero_rep ha, rw [factors_mk a0 hz] at hk, have hk' : ∀ p, p ∈ (factors' a0) → k ∣ (factors' a0).count p, { rintros p -, have pp : p = ⟨p.val, p.2⟩, { simp only [subtype.coe_eta, subtype.val_eq_coe] }, rw [pp, ← count_some p.2], exact hk p.val p.2 }, obtain ⟨u, hu⟩ := multiset.exists_smul_of_dvd_count _ hk', use (u : factor_set α).prod, apply eq_of_factors_eq_factors, rw [pow_factors, prod_factors, factors_mk a0 hz, ← with_top.some_eq_coe, hu], exact with_bot.coe_nsmul u k end /-- The only divisors of prime powers are prime powers. See `eq_pow_find_of_dvd_irreducible_pow` for an explicit expression as a p-power (without using `count`). -/ theorem eq_pow_count_factors_of_dvd_pow {p a : associates α} (hp : irreducible p) {n : ℕ} (h : a ∣ p ^ n) : a = p ^ p.count a.factors := begin nontriviality α, have hph := pow_ne_zero n hp.ne_zero, have ha := ne_zero_of_dvd_ne_zero hph h, apply eq_of_eq_counts ha (pow_ne_zero _ hp.ne_zero), have eq_zero_of_ne : ∀ (q : associates α), irreducible q → q ≠ p → _ = 0 := λ q hq h', nat.eq_zero_of_le_zero $ by { convert count_le_count_of_le hph hq h, symmetry, rw [count_pow hp.ne_zero hq, count_eq_zero_of_ne hq hp h', mul_zero] }, intros q hq, rw count_pow hp.ne_zero hq, by_cases h : q = p, { rw [h, count_self hp, mul_one] }, { rw [count_eq_zero_of_ne hq hp h, mul_zero, eq_zero_of_ne q hq h] } end lemma count_factors_eq_find_of_dvd_pow {a p : associates α} (hp : irreducible p) [∀ n : ℕ, decidable (a ∣ p ^ n)] {n : ℕ} (h : a ∣ p ^ n) : nat.find ⟨n, h⟩ = p.count a.factors := begin apply le_antisymm, { refine nat.find_le ⟨1, _⟩, rw mul_one, symmetry, exact eq_pow_count_factors_of_dvd_pow hp h }, { have hph := pow_ne_zero (nat.find ⟨n, h⟩) hp.ne_zero, casesI (subsingleton_or_nontrivial α) with hα hα, { simpa using hph, }, convert count_le_count_of_le hph hp (nat.find_spec ⟨n, h⟩), rw [count_pow hp.ne_zero hp, count_self hp, mul_one] } end omit dec omit dec_irr omit dec' theorem eq_pow_of_mul_eq_pow [nontrivial α] {a b c : associates α} (ha : a ≠ 0) (hb : b ≠ 0) (hab : ∀ d, d ∣ a → d ∣ b → ¬ prime d) {k : ℕ} (h : a * b = c ^ k) : ∃ (d : associates α), a = d ^ k := begin classical, by_cases hk0 : k = 0, { use 1, rw [hk0, pow_zero] at h ⊢, apply (mul_eq_one_iff.1 h).1 }, { refine is_pow_of_dvd_count ha _, intros p hp, apply dvd_count_of_dvd_count_mul hb hp hab, rw h, apply dvd_count_pow _ hp, rintros rfl, rw zero_pow' _ hk0 at h, cases mul_eq_zero.mp h; contradiction } end /-- The only divisors of prime powers are prime powers. -/ theorem eq_pow_find_of_dvd_irreducible_pow {a p : associates α} (hp : irreducible p) [∀ n : ℕ, decidable (a ∣ p ^ n)] {n : ℕ} (h : a ∣ p ^ n) : a = p ^ nat.find ⟨n, h⟩ := by { classical, rw [count_factors_eq_find_of_dvd_pow hp, ← eq_pow_count_factors_of_dvd_pow hp h] } end associates section open associates unique_factorization_monoid lemma associates.quot_out {α : Type*} [comm_monoid α] (a : associates α): associates.mk (quot.out (a)) = a := by rw [←quot_mk_eq_mk, quot.out_eq] /-- `to_gcd_monoid` constructs a GCD monoid out of a unique factorization domain. -/ noncomputable def unique_factorization_monoid.to_gcd_monoid (α : Type*) [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α] [decidable_eq (associates α)] [decidable_eq α] : gcd_monoid α := { gcd := λa b, quot.out (associates.mk a ⊓ associates.mk b : associates α), lcm := λa b, quot.out (associates.mk a ⊔ associates.mk b : associates α), gcd_dvd_left := λ a b, by { rw [←mk_dvd_mk, (associates.mk a ⊓ associates.mk b).quot_out, dvd_eq_le], exact inf_le_left }, gcd_dvd_right := λ a b, by { rw [←mk_dvd_mk, (associates.mk a ⊓ associates.mk b).quot_out, dvd_eq_le], exact inf_le_right }, dvd_gcd := λ a b c hac hab, by { rw [←mk_dvd_mk, (associates.mk c ⊓ associates.mk b).quot_out, dvd_eq_le, le_inf_iff, mk_le_mk_iff_dvd_iff, mk_le_mk_iff_dvd_iff], exact ⟨hac, hab⟩ }, lcm_zero_left := λ a, by { have : associates.mk (0 : α) = ⊤ := rfl, rw [this, top_sup_eq, ←this, ←associated_zero_iff_eq_zero, ←mk_eq_mk_iff_associated, ←associated_iff_eq, associates.quot_out] }, lcm_zero_right := λ a, by { have : associates.mk (0 : α) = ⊤ := rfl, rw [this, sup_top_eq, ←this, ←associated_zero_iff_eq_zero, ←mk_eq_mk_iff_associated, ←associated_iff_eq, associates.quot_out] }, gcd_mul_lcm := λ a b, by { rw [←mk_eq_mk_iff_associated, ←associates.mk_mul_mk, ←associated_iff_eq, associates.quot_out, associates.quot_out, mul_comm, sup_mul_inf, associates.mk_mul_mk] } } /-- `to_normalized_gcd_monoid` constructs a GCD monoid out of a normalization on a unique factorization domain. -/ noncomputable def unique_factorization_monoid.to_normalized_gcd_monoid (α : Type*) [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α] [normalization_monoid α] [decidable_eq (associates α)] [decidable_eq α] : normalized_gcd_monoid α := { gcd := λa b, (associates.mk a ⊓ associates.mk b).out, lcm := λa b, (associates.mk a ⊔ associates.mk b).out, gcd_dvd_left := assume a b, (out_dvd_iff a (associates.mk a ⊓ associates.mk b)).2 $ inf_le_left, gcd_dvd_right := assume a b, (out_dvd_iff b (associates.mk a ⊓ associates.mk b)).2 $ inf_le_right, dvd_gcd := assume a b c hac hab, show a ∣ (associates.mk c ⊓ associates.mk b).out, by rw [dvd_out_iff, le_inf_iff, mk_le_mk_iff_dvd_iff, mk_le_mk_iff_dvd_iff]; exact ⟨hac, hab⟩, lcm_zero_left := assume a, show (⊤ ⊔ associates.mk a).out = 0, by simp, lcm_zero_right := assume a, show (associates.mk a ⊔ ⊤).out = 0, by simp, gcd_mul_lcm := assume a b, by { rw [← out_mul, mul_comm, sup_mul_inf, mk_mul_mk, out_mk], exact normalize_associated (a * b) }, normalize_gcd := assume a b, by convert normalize_out _, normalize_lcm := assume a b, by convert normalize_out _, .. ‹normalization_monoid α› } end namespace unique_factorization_monoid /-- If `y` is a nonzero element of a unique factorization monoid with finitely many units (e.g. `ℤ`, `ideal (ring_of_integers K)`), it has finitely many divisors. -/ noncomputable def fintype_subtype_dvd {M : Type*} [cancel_comm_monoid_with_zero M] [unique_factorization_monoid M] [fintype Mˣ] (y : M) (hy : y ≠ 0) : fintype {x // x ∣ y} := begin haveI : nontrivial M := ⟨⟨y, 0, hy⟩⟩, haveI : normalization_monoid M := unique_factorization_monoid.normalization_monoid, haveI := classical.dec_eq M, haveI := classical.dec_eq (associates M), -- We'll show `λ (u : Mˣ) (f ⊆ factors y) → u * Π f` is injective -- and has image exactly the divisors of `y`. refine fintype.of_finset (((normalized_factors y).powerset.to_finset.product (finset.univ : finset Mˣ)).image (λ s, (s.snd : M) * s.fst.prod)) (λ x, _), simp only [exists_prop, finset.mem_image, finset.mem_product, finset.mem_univ, and_true, multiset.mem_to_finset, multiset.mem_powerset, exists_eq_right, multiset.mem_map], split, { rintros ⟨s, hs, rfl⟩, have prod_s_ne : s.fst.prod ≠ 0, { intro hz, apply hy (eq_zero_of_zero_dvd _), have hz := (@multiset.prod_eq_zero_iff M _ _ _ s.fst).mp hz, rw ← (normalized_factors_prod hy).dvd_iff_dvd_right, exact multiset.dvd_prod (multiset.mem_of_le hs hz) }, show (s.snd : M) * s.fst.prod ∣ y, rw [(unit_associated_one.mul_right s.fst.prod).dvd_iff_dvd_left, one_mul, ← (normalized_factors_prod hy).dvd_iff_dvd_right], exact multiset.prod_dvd_prod_of_le hs }, { rintro (h : x ∣ y), have hx : x ≠ 0, { refine mt (λ hx, _) hy, rwa [hx, zero_dvd_iff] at h }, obtain ⟨u, hu⟩ := normalized_factors_prod hx, refine ⟨⟨normalized_factors x, u⟩, _, (mul_comm _ _).trans hu⟩, exact (dvd_iff_normalized_factors_le_normalized_factors hx hy).mp h } end end unique_factorization_monoid section finsupp variables [cancel_comm_monoid_with_zero α] [unique_factorization_monoid α] variables [normalization_monoid α] [decidable_eq α] open unique_factorization_monoid /-- This returns the multiset of irreducible factors as a `finsupp` -/ noncomputable def factorization (n : α) : α →₀ ℕ := (normalized_factors n).to_finsupp lemma factorization_eq_count {n p : α} : factorization n p = multiset.count p (normalized_factors n) := by simp [factorization] @[simp] lemma factorization_zero : factorization (0 : α) = 0 := by simp [factorization] @[simp] lemma factorization_one : factorization (1 : α) = 0 := by simp [factorization] /-- The support of `factorization n` is exactly the finset of normalized factors -/ @[simp] lemma support_factorization {n : α} : (factorization n).support = (normalized_factors n).to_finset := by simp [factorization, multiset.to_finsupp_support] /-- For nonzero `a` and `b`, the power of `p` in `a * b` is the sum of the powers in `a` and `b` -/ @[simp] lemma factorization_mul {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : factorization (a * b) = factorization a + factorization b := by simp [factorization, normalized_factors_mul ha hb] /-- For any `p`, the power of `p` in `x^n` is `n` times the power in `x` -/ lemma factorization_pow {x : α} {n : ℕ} : factorization (x^n) = n • factorization x := by { ext, simp [factorization] } lemma associated_of_factorization_eq (a b: α) (ha: a ≠ 0) (hb: b ≠ 0) (h: factorization a = factorization b) : associated a b := begin simp_rw [factorization, add_equiv.apply_eq_iff_eq] at h, rwa [associated_iff_normalized_factors_eq_normalized_factors ha hb], end end finsupp
a755844a0b92a5cf2f9798d85b793e4ee93d2ccf
9cb9db9d79fad57d80ca53543dc07efb7c4f3838
/src/breen_deligne/universal.lean
438b5d4d3b65017f4e4fc7881dfda02dccb81772
[]
no_license
mr-infty/lean-liquid
3ff89d1f66244b434654c59bdbd6b77cb7de0109
a8db559073d2101173775ccbd85729d3a4f1ed4d
refs/heads/master
1,678,465,145,334
1,614,565,310,000
1,614,565,310,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,498
lean
-- n© import breen_deligne.basic /-! # Universality of universal maps A `universal_map m n` in this repo is a finite formal `ℤ`-linear combination of `m × n` matrices (so-called `basic_universal_map`s). In this file we show that to give a term of this type is to give a term of type `functorial_map m n`, which is defined to be a collection of additive group homomorphisms `ℤ[A^m] →+ ℤ[A^n]` for each abelian group `A`, functorial in `A`. The dictionary: given an `m × n` matrix with integer coefficients, it induces a map `A^m → A^n` in the obvious way and hence a group homomorphism `ℤ[A^m] →+ ℤ[A^n]`; hence finite formal integer combinations of these also give group homomorphisms. It is not hard to check that homomorphisms obtained in this way are functorial in `A`. Conversely, we start by identifying `(ℤ^p)^q` with `q × p` matrices in the obvious way. Now given a functorial collection of homomorphisms `ℤ[A^m] → ℤ[A^n]`, evaluating at collection at the group `A=ℤ^m` and the element of `(ℤ^m)^m` corresponding to the `m × m` identity matrix gives an element of `ℤ[(ℤ^m)^n]` which can be thought of in a natural way as a finite ℤ-linear combination of `m × n` matrices. The proofs that these constructions are inverse to one another is relatively straightforward, and is of course given in full below. To show that the construction `functorial_map m n → universal_map m n → functorial_map m n` is the identity, we observe that the value of a `functorial_map m n` at an element `x : A^m` can be computed by using functoriality applied to the map `ℤ^m → A` sending the i'th basis vector to `x i`. -/ noncomputable theory -- get some notation working: open_locale big_operators direct_sum local attribute [instance] type_pow local notation `ℤ[` A `]` := free_abelian_group A universes u namespace breen_deligne section functorial_map_section open free_abelian_group /-- A term `F : functorial_map m n` is a collection of group homomorphisms `F.f A : ℤ[A^m] →+ ℤ[A^n]`, functorial in the abelian group `A`. -/ @[ext] structure functorial_map (m n : ℕ) := (f (A : Type u) [add_comm_group A] : ℤ[A^m] →+ ℤ[A^n]) (functorial {A : Type u} [add_comm_group A] {B : Type u} [add_comm_group B] (φ : A →+ B) : (free_abelian_group.map (φ.pow n)).comp (f A) = (f B).comp (map (φ.pow m))) variables {m n : ℕ} namespace functorial_map open add_monoid_hom def add (F G : functorial_map m n) : functorial_map m n := { f := λ A _, by exactI F.f A + G.f A, functorial := λ A _ B _ φ, by rw [comp_add, add_comp, F.functorial, G.functorial] } instance : has_add (functorial_map m n) := ⟨add⟩ def zero : functorial_map m n := { f := λ A _, 0, functorial := by intros; simp } instance : has_zero (functorial_map m n) := ⟨zero⟩ def neg (F : functorial_map m n) : functorial_map m n := { f := λ A _, by exactI -F.f A, functorial := λ A _ B _ φ, by rw [comp_neg, neg_comp, F.functorial]} instance : has_neg (functorial_map m n) := ⟨neg⟩ instance : add_comm_group (functorial_map m n) := { add := (+), add_assoc := λ _ _ _, ext _ _ $ funext (λ A, funext (λ _, by exactI add_assoc _ _ _)), zero := 0, zero_add := λ _, ext _ _ $ funext (λ A, funext (λ _, by exactI zero_add _)), add_zero := λ _, ext _ _ $ funext (λ A, funext (λ _, by exactI add_zero _)), neg := has_neg.neg, add_left_neg := λ _, ext _ _ $ funext (λ A, funext (λ _, by exactI add_left_neg _)), add_comm := λ F G, ext _ _ $ funext (λ A, funext (λ _, by exactI add_comm _ _)) } end functorial_map end functorial_map_section section punit_stuff open free_abelian_group -- this is now on `free-abelian-group-equivs` branch def punit_equiv : ℤ[punit] ≃+ ℤ := { inv_fun := λ n, n • of (punit.star), to_fun := free_abelian_group.lift (λ _, (1 : ℤ)), left_inv := λ z, free_abelian_group.induction_on z (by {dsimp only, rw [add_monoid_hom.map_zero, zero_smul]}) (λ x, punit.cases_on x (by simp)) (λ x, punit.cases_on x (by simp)) (λ x y hx hy, by { simp only [add_monoid_hom.map_add, add_smul] at *, rw [hx, hy]}), right_inv := λ n, by { rw [add_monoid_hom.map_int_module_smul, lift.of], exact gsmul_int_one n}, map_add' := add_monoid_hom.map_add _ } -- this is now on `free-abelian-group-equivs` branch def equiv_of_equiv {α β : Type*} (f : α ≃ β) : free_abelian_group α ≃+ free_abelian_group β := { to_fun := free_abelian_group.lift $ free_abelian_group.of ∘ f, inv_fun := free_abelian_group.lift $ free_abelian_group.of ∘ f.symm, left_inv := begin intros x, refine free_abelian_group.induction_on x (by simp only [add_monoid_hom.map_zero]) (by simp only [equiv.symm_apply_apply, eq_self_iff_true, function.comp_app, lift.of, forall_true_iff]) (by simp only [equiv.symm_apply_apply, eq_self_iff_true, function.comp_app, add_monoid_hom.map_neg, lift.of, forall_true_iff]) _, intros x y h, simp only [h, imp_self, add_right_inj, add_monoid_hom.map_add], end, right_inv := begin intros x, refine free_abelian_group.induction_on x (by simp only [add_monoid_hom.map_zero]) (by simp only [eq_self_iff_true, function.comp_app, equiv.apply_symm_apply, lift.of, forall_true_iff]) (by simp only [eq_self_iff_true, function.comp_app, equiv.apply_symm_apply, add_monoid_hom.map_neg, lift.of, forall_true_iff]) _, intros x y h, simp only [h, imp_self, add_right_inj, add_monoid_hom.map_add], end, map_add' := begin intros x y, simp only [add_monoid_hom.map_add], end } end punit_stuff lemma lift_smul_eq_lift {A : Type*} [add_comm_group A] (a : A) (c : ℤ[punit]) : (free_abelian_group.lift (λ _, (1 : ℤ))) c • a = free_abelian_group.lift (λ _, a) c := begin rw ← punit_equiv.left_inv c, generalize : punit_equiv.to_fun c = d, delta punit_equiv, dsimp only, apply int.induction_on d, { simp }, { intros i hi, simp [add_smul, ←hi], }, { intros i hi, simp at *, simp [sub_smul, ←hi] } end open universal_map open add_monoid_hom /-- universal maps are equivalent to functorial maps. -/ def universal_map_equiv_functorial_map (m n : ℕ) : universal_map m n ≃+ functorial_map.{u} m n := { -- The construction of a functorial map from a universal map. to_fun := λ U, { f := λ A _, by exactI eval A U, functorial := λ A _ B _ φ, begin -- The proof that evaluation of universal maps is functorial for group homomorphisms. -- We start by unravelling what the question is. resetI, ext as, rw [comp_apply, comp_apply], -- free_abelian_group.map_of should be redefined now map is an add_group_hom not a map? -- Bhavik says make a dsimp lemma, I say make `map_of'` or `map_apply` change _ = ((eval B) U) ((φ.pow m) <$> (free_abelian_group.of as)), rw free_abelian_group.map_of, -- We need to prove that for all `as : A^m`, evaluating `U` then mapping with `φ` -- is the same as applying `φ` and then evaluating `U` on the corresponding element of -- `ℤ[B^m]`. -- By linearity, we can assume that `U` is a basic universal map `f`. apply free_abelian_group.induction_on U, { simp only [map_zero, zero_apply]}, { intro f, -- Here is the proof for basic universal maps. simp only [basic_universal_map.eval_of, eval_of], -- We use the universal property convert free_abelian_group.map_of _ _, -- which boils the question down to checking that φ : A^n → B^n and φ : A^m → B^m -- commutes with the matrix action A^m → A^n ext i, -- and this just boils down to trivialities rw [pow_eval, add_monoid_hom.map_sum], apply finset.sum_congr rfl, rintros j -, rw [pow_eval, map_int_module_smul] }, -- the rest is just checking that the question about universal maps was linear -- so the reduction to the basic case was OK. { intros F hF, simp only [add_monoid_hom.map_neg, neg_inj, neg_apply, hF] }, { intros F G hF hG, simp only [hF, hG, add_monoid_hom.map_add, add_apply]} end }, -- The construction of a universal map from a functorial map inv_fun := λ F, free_abelian_group.lift (λ x, free_abelian_group.of (λ i j, (punit_equiv ((x : (ℤ[punit] ^ m) ^ n) i j)) : basic_universal_map m n)) (F.f -- We evaluate on ℤ^m (in universe `u`) (free_abelian_group (punit)^m) -- and on the identity matrix. (free_abelian_group.of (λ i j, if i = j then free_abelian_group.of punit.star else 0))), -- We check that given a universal map, constructing a functorial map and then -- another universal map gives us the universal map we started with. left_inv := begin intro u, -- By linearity it suffices to check for basic universal maps. apply free_abelian_group.induction_on u, { simp only [map_zero, zero_apply]}, { intro b, -- so let b be a basic universal map. Now unravel everything simp only [free_abelian_group.lift.of, basic_universal_map.eval_of, eval_of], congr', ext i j', -- and it boils down to checking that a sum over j of `b i j` times `delta j j'` -- is b i j'. Because of universe issues and typeclass issues this is a bit -- bumpier than expected. convert punit_equiv.right_inv (b i j'), apply _root_.congr_arg, change _ = (b i j') • _, -- next 10 lines = hack to get around pi.has_scalar != mul_action.to_has_scalar have h2 := fintype.sum_apply j' (λ (j : fin m), @has_scalar.smul _ _ (mul_action.to_has_scalar) (b i j) (λ (j_1 : fin m), ite (j = j_1) (free_abelian_group.of punit.star) 0 : (ℤ[punit]^m))), dsimp only at h2, convert h2; clear h2, { ext j, apply _root_.congr_fun, congr' }, change b i j' • free_abelian_group.of punit.star = ∑ (c : fin m), b i c • ite (c = j') (free_abelian_group.of punit.star) 0, -- Now we're over that hump it is as easy as it sounded 10 lines ago. simp_rw [smul_ite, smul_zero], rw finset.sum_ite_eq', rw if_pos (finset.mem_univ _) }, { simp }, { intros u v hu hv, simp * at * } end, -- Finally we check that starting with a functorial map and then constructing the -- universal map and then the functorial map gets us back to where we started. right_inv := begin intro F, -- We check the two functorial maps agree on the abelian group `A` and on input `x : A^m` ext A inst x, simp only, resetI, -- Let φ be the map from ℤ^m to A sending the i'th basis vector to `x i`. let φ : ℤ[punit.{u+1}]^m →+ A := pow_hom (λ i, free_abelian_group.lift (λ _, x i)), -- Use the fact that the original functorial map is functorial for `phi`, and -- evaluate the corresponding commutative diagram at the identity matrix in `(ℤ^m)^m`. have h := F.functorial φ, let id_mat : (ℤ[punit]^m)^m := λ i j, if i = j then free_abelian_group.of punit.star else 0, replace h := congr_fun h (free_abelian_group.of id_mat), -- The corresponding equation is almost what we have to prove. convert h, { clear h, rw add_monoid_hom.comp_apply, -- Of the two things left to do, the first follows from the following assertion -- (letting t be the image of the identity map under φ) suffices : ∀ (t : ℤ[(ℤ[punit] ^ m) ^ n]), breen_deligne.universal_map.eval A (free_abelian_group.lift (λ (y : fin n → fin m → ℤ[punit]), free_abelian_group.of (λ (i : fin n) (j : fin m), punit_equiv (y i j))) t) (free_abelian_group.of x) = (free_abelian_group.map ⇑(φ.pow n)) t, rw ← this, refl, -- and by linearity this assertion only needs to be verified for `t` an `m × n` -- matrix (rather than a ℤ-linear combination of them) intro t, apply free_abelian_group.induction_on t, { simp only [map_zero, zero_apply]}, { clear t, intro t, -- for which it follows by unravelling the definitions. rw free_abelian_group.lift.of, rw eval_of, convert (free_abelian_group.map_of _ _).symm, rw free_abelian_group.map_of, convert free_abelian_group.map_of _ _, ext i, simp only [φ, pow_hom, add_monoid_hom.pow], apply finset.sum_congr rfl, rintro j -, delta punit_equiv, simp only [add_equiv.coe_mk], apply lift_smul_eq_lift }, { simp only [imp_self, forall_const, neg_inj, map_neg, neg_apply]}, { intros x y hx hy, simp * at * } }, { -- The second and final thing we need to check is that evaluating `φ` on -- the identity matrix gives us `x`, and this follows essentially immediately -- from the definitions. ext i, simp only [id_mat, add_monoid_hom.pow, φ, pow_hom, coe_mk, function.comp_app], symmetry, convert finset.sum_ite_eq (finset.univ : finset (fin m)) i x, swap, rw if_pos (finset.mem_univ _), ext j, split_ifs; simp }, end, -- Note finally that this functorial constuction is an isomorphism of -- abelian groups, as can be easily checked. map_add' := begin intros u v, ext, resetI, simp, refl, end, } end breen_deligne
9398aee13811158fc55d9d3399149b542cba94b5
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/matrix2.lean
a6ffae2e72243f76dc1ec9208f198601d55498fd
[ "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
815
lean
constant {u} matrix : Type u → Type u constant same_dim {A : Type} : matrix A → matrix A → Prop constant add1 {A : Type} (m1 m2 : matrix A) {H : same_dim m1 m2} : matrix A open eq theorem same_dim_eq_args {A : Type} {m1 m2 m1' m2' : matrix A} (H1 : m1 = m1') (H2 : m2 = m2') (H : same_dim m1 m2) : same_dim m1' m2' := subst H1 (subst H2 H) theorem add1_congr {A : Type} (m1 m2 m1' m2' : matrix A) (H1 : m1 = m1') (H2 : m2 = m2') (H : same_dim m1 m2) : @add1 A m1 m2 H = @add1 A m1' m2' (same_dim_eq_args H1 H2 H) := have base : ∀ (H1 : m1 = m1) (H2 : m2 = m2), @add1 A m1 m2 H = @add1 A m1 m2 (eq.rec (eq.rec H H1) H2), from assume H1 H2, rfl, have general : ∀ (H1 : m1 = m1') (H2 : m2 = m2'), @add1 A m1 m2 H = @add1 A m1' m2' (eq.rec (eq.rec H H1) H2), from subst H1 (subst H2 base), general H1 H2
4d2829e43867b066790712f2aa9ad3256879a4e5
649957717d58c43b5d8d200da34bf374293fe739
/src/algebra/group/basic.lean
c65b4215f5571e6d571cec18f300ca73f84d0f08
[ "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
7,867
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Simon Hudon, Mario Carneiro Various multiplicative and additive structures. -/ import algebra.group.to_additive universe u variable {α : Type u} instance monoid_to_is_left_id {α : Type*} [monoid α] : is_left_id α (*) 1 := ⟨ monoid.one_mul ⟩ instance monoid_to_is_right_id {α : Type*} [monoid α] : is_right_id α (*) 1 := ⟨ monoid.mul_one ⟩ instance add_monoid_to_is_left_id {α : Type*} [add_monoid α] : is_left_id α (+) 0 := ⟨ add_monoid.zero_add ⟩ instance add_monoid_to_is_right_id {α : Type*} [add_monoid α] : is_right_id α (+) 0 := ⟨ add_monoid.add_zero ⟩ @[simp, to_additive add_left_inj] theorem mul_left_inj [left_cancel_semigroup α] (a : α) {b c : α} : a * b = a * c ↔ b = c := ⟨mul_left_cancel, congr_arg _⟩ @[simp, to_additive add_right_inj] theorem mul_right_inj [right_cancel_semigroup α] (a : α) {b c : α} : b * a = c * a ↔ b = c := ⟨mul_right_cancel, congr_arg _⟩ section comm_semigroup variables [comm_semigroup α] {a b c d : α} @[to_additive add_add_add_comm] theorem mul_mul_mul_comm : (a * b) * (c * d) = (a * c) * (b * d) := by simp [mul_left_comm, mul_assoc] end comm_semigroup section group variables [group α] {a b c : α} @[simp, to_additive neg_inj'] theorem inv_inj' : a⁻¹ = b⁻¹ ↔ a = b := ⟨λ h, by rw [← inv_inv a, h, inv_inv], congr_arg _⟩ @[to_additive eq_of_neg_eq_neg] theorem eq_of_inv_eq_inv : a⁻¹ = b⁻¹ → a = b := inv_inj'.1 @[simp, to_additive add_self_iff_eq_zero] theorem mul_self_iff_eq_one : a * a = a ↔ a = 1 := by have := @mul_left_inj _ _ a a 1; rwa mul_one at this @[simp, to_additive neg_eq_zero] theorem inv_eq_one : a⁻¹ = 1 ↔ a = 1 := by rw [← @inv_inj' _ _ a 1, one_inv] @[simp, to_additive neg_ne_zero] theorem inv_ne_one : a⁻¹ ≠ 1 ↔ a ≠ 1 := not_congr inv_eq_one @[to_additive left_inverse_neg] theorem left_inverse_inv (α) [group α] : function.left_inverse (λ a : α, a⁻¹) (λ a, a⁻¹) := assume a, inv_inv a attribute [simp] mul_inv_cancel_left add_neg_cancel_left mul_inv_cancel_right add_neg_cancel_right @[to_additive eq_neg_iff_eq_neg] theorem eq_inv_iff_eq_inv : a = b⁻¹ ↔ b = a⁻¹ := ⟨eq_inv_of_eq_inv, eq_inv_of_eq_inv⟩ @[to_additive neg_eq_iff_neg_eq] theorem inv_eq_iff_inv_eq : a⁻¹ = b ↔ b⁻¹ = a := by rw [eq_comm, @eq_comm _ _ a, eq_inv_iff_eq_inv] @[to_additive add_eq_zero_iff_eq_neg] theorem mul_eq_one_iff_eq_inv : a * b = 1 ↔ a = b⁻¹ := by simpa [mul_left_inv, -mul_right_inj] using @mul_right_inj _ _ b a (b⁻¹) @[to_additive add_eq_zero_iff_neg_eq] theorem mul_eq_one_iff_inv_eq : a * b = 1 ↔ a⁻¹ = b := by rw [mul_eq_one_iff_eq_inv, eq_inv_iff_eq_inv, eq_comm] @[to_additive eq_neg_iff_add_eq_zero] theorem eq_inv_iff_mul_eq_one : a = b⁻¹ ↔ a * b = 1 := mul_eq_one_iff_eq_inv.symm @[to_additive neg_eq_iff_add_eq_zero] theorem inv_eq_iff_mul_eq_one : a⁻¹ = b ↔ a * b = 1 := mul_eq_one_iff_inv_eq.symm @[to_additive eq_add_neg_iff_add_eq] theorem eq_mul_inv_iff_mul_eq : a = b * c⁻¹ ↔ a * c = b := ⟨λ h, by rw [h, inv_mul_cancel_right], λ h, by rw [← h, mul_inv_cancel_right]⟩ @[to_additive eq_neg_add_iff_add_eq] theorem eq_inv_mul_iff_mul_eq : a = b⁻¹ * c ↔ b * a = c := ⟨λ h, by rw [h, mul_inv_cancel_left], λ h, by rw [← h, inv_mul_cancel_left]⟩ @[to_additive neg_add_eq_iff_eq_add] theorem inv_mul_eq_iff_eq_mul : a⁻¹ * b = c ↔ b = a * c := ⟨λ h, by rw [← h, mul_inv_cancel_left], λ h, by rw [h, inv_mul_cancel_left]⟩ @[to_additive add_neg_eq_iff_eq_add] theorem mul_inv_eq_iff_eq_mul : a * b⁻¹ = c ↔ a = c * b := ⟨λ h, by rw [← h, inv_mul_cancel_right], λ h, by rw [h, mul_inv_cancel_right]⟩ @[to_additive add_neg_eq_zero] theorem mul_inv_eq_one {a b : α} : a * b⁻¹ = 1 ↔ a = b := by rw [mul_eq_one_iff_eq_inv, inv_inv] @[to_additive neg_comm_of_comm] theorem inv_comm_of_comm {a b : α} (H : a * b = b * a) : a⁻¹ * b = b * a⁻¹ := begin have : a⁻¹ * (b * a) * a⁻¹ = a⁻¹ * (a * b) * a⁻¹ := congr_arg (λ x:α, a⁻¹ * x * a⁻¹) H.symm, rwa [inv_mul_cancel_left, mul_assoc, mul_inv_cancel_right] at this end end group section add_group variables [add_group α] {a b c : α} local attribute [simp] sub_eq_add_neg def sub_sub_cancel := @sub_sub_self @[simp] lemma sub_left_inj : a - b = a - c ↔ b = c := (add_left_inj _).trans neg_inj' @[simp] lemma sub_right_inj : b - a = c - a ↔ b = c := add_right_inj _ lemma sub_add_sub_cancel (a b c : α) : (a - b) + (b - c) = a - c := by rw [← add_sub_assoc, sub_add_cancel] lemma sub_sub_sub_cancel_right (a b c : α) : (a - c) - (b - c) = a - b := by rw [← neg_sub c b, sub_neg_eq_add, sub_add_sub_cancel] theorem sub_eq_zero : a - b = 0 ↔ a = b := ⟨eq_of_sub_eq_zero, λ h, by rw [h, sub_self]⟩ theorem sub_ne_zero : a - b ≠ 0 ↔ a ≠ b := not_congr sub_eq_zero theorem eq_sub_iff_add_eq : a = b - c ↔ a + c = b := eq_add_neg_iff_add_eq theorem sub_eq_iff_eq_add : a - b = c ↔ a = c + b := add_neg_eq_iff_eq_add theorem eq_iff_eq_of_sub_eq_sub {a b c d : α} (H : a - b = c - d) : a = b ↔ c = d := by rw [← sub_eq_zero, H, sub_eq_zero] theorem left_inverse_sub_add_left (c : α) : function.left_inverse (λ x, x - c) (λ x, x + c) := assume x, add_sub_cancel x c theorem left_inverse_add_left_sub (c : α) : function.left_inverse (λ x, x + c) (λ x, x - c) := assume x, sub_add_cancel x c theorem left_inverse_add_right_neg_add (c : α) : function.left_inverse (λ x, c + x) (λ x, - c + x) := assume x, add_neg_cancel_left c x theorem left_inverse_neg_add_add_right (c : α) : function.left_inverse (λ x, - c + x) (λ x, c + x) := assume x, neg_add_cancel_left c x end add_group section add_comm_group variables [add_comm_group α] {a b c : α} lemma sub_eq_neg_add (a b : α) : a - b = -b + a := add_comm _ _ theorem neg_add' (a b : α) : -(a + b) = -a - b := neg_add a b lemma neg_sub_neg (a b : α) : -a - -b = b - a := by simp lemma eq_sub_iff_add_eq' : a = b - c ↔ c + a = b := by rw [eq_sub_iff_add_eq, add_comm] lemma sub_eq_iff_eq_add' : a - b = c ↔ a = b + c := by rw [sub_eq_iff_eq_add, add_comm] lemma add_sub_cancel' (a b : α) : a + b - a = b := by rw [sub_eq_neg_add, neg_add_cancel_left] lemma add_sub_cancel'_right (a b : α) : a + (b - a) = b := by rw [← add_sub_assoc, add_sub_cancel'] lemma sub_right_comm (a b c : α) : a - b - c = a - c - b := add_right_comm _ _ _ lemma add_add_sub_cancel (a b c : α) : (a + c) + (b - c) = a + b := by rw [add_assoc, add_sub_cancel'_right] lemma sub_add_add_cancel (a b c : α) : (a - c) + (b + c) = a + b := by rw [add_left_comm, sub_add_cancel, add_comm] lemma sub_add_sub_cancel' (a b c : α) : (a - b) + (c - a) = c - b := by rw add_comm; apply sub_add_sub_cancel lemma add_sub_sub_cancel (a b c : α) : (a + b) - (a - c) = b + c := by rw [← sub_add, add_sub_cancel'] lemma sub_sub_sub_cancel_left (a b c : α) : (c - a) - (c - b) = b - a := by rw [← neg_sub b c, sub_neg_eq_add, add_comm, sub_add_sub_cancel] lemma sub_eq_sub_iff_sub_eq_sub {d : α} : a - b = c - d ↔ a - c = b - d := ⟨λ h, by rw eq_add_of_sub_eq h; simp, λ h, by rw eq_add_of_sub_eq h; simp⟩ end add_comm_group section add_monoid variables [add_monoid α] {a b c : α} @[simp] lemma bit0_zero : bit0 (0 : α) = 0 := add_zero _ @[simp] lemma bit1_zero [has_one α] : bit1 (0 : α) = 1 := show 0+0+1=(1:α), by rw [zero_add, zero_add] end add_monoid
d32830b8af0a3933a4d5709edeb1a9f2f91c46cb
27a31d06bcfc7c5d379fd04a08a9f5ed3f5302d4
/stage0/src/Lean/Data/Format.lean
d459a0edc525d098c018c993b0b2cc6c0fa692e2
[ "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
joehendrix/lean4
0d1486945f7ca9fe225070374338f4f7e74bab03
1221bdd3c7d5395baa451ce8fdd2c2f8a00cbc8f
refs/heads/master
1,640,573,727,861
1,639,662,710,000
1,639,665,515,000
198,893,504
0
0
Apache-2.0
1,564,084,645,000
1,564,084,644,000
null
UTF-8
Lean
false
false
1,705
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ import Lean.Data.Options universe u v namespace Std namespace Format open Lean def getWidth (o : Options) : Nat := o.get `format.width defWidth def getIndent (o : Options) : Nat := o.get `format.indent defIndent def getUnicode (o : Options) : Bool := o.get `format.unicode defUnicode register_builtin_option format.width : Nat := { defValue := defWidth descr := "indentation" } register_builtin_option format.unicode : Bool := { defValue := defUnicode descr := "unicode characters" } register_builtin_option format.indent : Nat := { defValue := defIndent descr := "indentation" } def pretty' (f : Format) (o : Options := {}) : String := pretty f (format.width.get o) end Format end Std namespace Lean open Std export Std (Format ToFormat Format.nest Format.nil Format.joinSep Format.line Format.sbracket Format.bracket Format.group Format.tag Format.pretty Format.fill Format.paren Format.join) export Std.ToFormat (format) instance : ToFormat Name where format n := n.toString instance : ToFormat DataValue where format | DataValue.ofString v => format (repr v) | DataValue.ofBool v => format v | DataValue.ofName v => "`" ++ format v | DataValue.ofNat v => format v | DataValue.ofInt v => format v instance : ToFormat (Name × DataValue) where format | (n, v) => format n ++ " := " ++ format v open Std.Format def formatKVMap (m : KVMap) : Format := sbracket (Format.joinSep m.entries ", ") instance : ToFormat KVMap := ⟨formatKVMap⟩ end Lean
7725ae7c8874973ae703b8292b4dd4d9ffcd3c29
649957717d58c43b5d8d200da34bf374293fe739
/src/topology/instances/real.lean
ea3e63a924d489e00a27b3f64f56edcc31fe4227
[ "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
18,905
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 The real numbers ℝ. They are constructed as the topological completion of ℚ. With the following steps: (1) prove that ℚ forms a uniform space. (2) subtraction and addition are uniform continuous functions in this space (3) for multiplication and inverse this only holds on bounded subsets (4) ℝ is defined as separated Cauchy filters over ℚ (the separation requires a quotient construction) (5) extend the uniform continuous functions along the completion (6) proof field properties using the principle of extension of identities TODO generalizations: * topological groups & rings * order topologies * Archimedean fields -/ import topology.metric_space.basic topology.algebra.uniform_group topology.algebra.ring tactic.linarith noncomputable theory open classical set lattice filter topological_space metric local attribute [instance] prop_decidable universes u v w variables {α : Type u} {β : Type v} {γ : Type w} instance : metric_space ℚ := metric_space.induced coe rat.cast_injective real.metric_space theorem rat.dist_eq (x y : ℚ) : dist x y = abs (x - y) := rfl instance : metric_space ℤ := begin letI M := metric_space.induced coe int.cast_injective real.metric_space, refine @metric_space.replace_uniformity _ int.uniform_space M (le_antisymm refl_le_uniformity $ λ r ru, mem_uniformity_dist.2 ⟨1, zero_lt_one, λ a b h, mem_principal_sets.1 ru $ dist_le_zero.1 (_ : (abs (a - b) : ℝ) ≤ 0)⟩), simpa using (@int.cast_le ℝ _ _ 0).2 (int.lt_add_one_iff.1 $ (@int.cast_lt ℝ _ (abs (a - b)) 1).1 $ by simpa using h) end theorem uniform_continuous_of_rat : uniform_continuous (coe : ℚ → ℝ) := uniform_continuous_comap theorem uniform_embedding_of_rat : uniform_embedding (coe : ℚ → ℝ) := uniform_embedding_comap rat.cast_injective theorem dense_embedding_of_rat : dense_embedding (coe : ℚ → ℝ) := uniform_embedding_of_rat.dense_embedding $ λ x, mem_closure_iff_nhds.2 $ λ t ht, let ⟨ε,ε0, hε⟩ := mem_nhds_iff.1 ht in let ⟨q, h⟩ := exists_rat_near x ε0 in ne_empty_iff_exists_mem.2 ⟨_, hε (mem_ball'.2 h), q, rfl⟩ theorem embedding_of_rat : embedding (coe : ℚ → ℝ) := dense_embedding_of_rat.to_embedding theorem continuous_of_rat : continuous (coe : ℚ → ℝ) := uniform_continuous_of_rat.continuous theorem real.uniform_continuous_add : uniform_continuous (λp : ℝ × ℝ, p.1 + p.2) := metric.uniform_continuous_iff.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_add_continuous_lemma abs ε0 in ⟨δ, δ0, λ a b h, let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ h₁ h₂⟩ -- TODO(Mario): Find a way to use rat_add_continuous_lemma theorem rat.uniform_continuous_add : uniform_continuous (λp : ℚ × ℚ, p.1 + p.2) := uniform_embedding_of_rat.to_uniform_inducing.uniform_continuous_iff.2 $ by simp [(∘)]; exact real.uniform_continuous_add.comp ((uniform_continuous_of_rat.comp uniform_continuous_fst).prod_mk (uniform_continuous_of_rat.comp uniform_continuous_snd)) theorem real.uniform_continuous_neg : uniform_continuous (@has_neg.neg ℝ _) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h, by rw dist_comm at h; simpa [real.dist_eq] using h⟩ theorem rat.uniform_continuous_neg : uniform_continuous (@has_neg.neg ℚ _) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h, by rw dist_comm at h; simpa [rat.dist_eq] using h⟩ instance : uniform_add_group ℝ := uniform_add_group.mk' real.uniform_continuous_add real.uniform_continuous_neg instance : uniform_add_group ℚ := uniform_add_group.mk' rat.uniform_continuous_add rat.uniform_continuous_neg instance : topological_add_group ℝ := by apply_instance instance : topological_add_group ℚ := by apply_instance instance : orderable_topology ℚ := induced_orderable_topology _ (λ x y, rat.cast_lt) (@exists_rat_btwn _ _ _) lemma real.is_topological_basis_Ioo_rat : @is_topological_basis ℝ _ (⋃(a b : ℚ) (h : a < b), {Ioo a b}) := is_topological_basis_of_open_of_nhds (by simp [is_open_Ioo] {contextual:=tt}) (assume a v hav hv, let ⟨l, u, hl, hu, h⟩ := (mem_nhds_unbounded (no_top _) (no_bot _)).mp (mem_nhds_sets hv hav), ⟨q, hlq, hqa⟩ := exists_rat_btwn hl, ⟨p, hap, hpu⟩ := exists_rat_btwn hu in ⟨Ioo q p, by simp; exact ⟨q, p, rat.cast_lt.1 $ lt_trans hqa hap, rfl⟩, ⟨hqa, hap⟩, assume a' ⟨hqa', ha'p⟩, h _ (lt_trans hlq hqa') (lt_trans ha'p hpu)⟩) instance : second_countable_topology ℝ := ⟨⟨(⋃(a b : ℚ) (h : a < b), {Ioo a b}), by simp [countable_Union, countable_Union_Prop], real.is_topological_basis_Ioo_rat.2.2⟩⟩ /- TODO(Mario): Prove that these are uniform isomorphisms instead of uniform embeddings lemma uniform_embedding_add_rat {r : ℚ} : uniform_embedding (λp:ℚ, p + r) := _ lemma uniform_embedding_mul_rat {q : ℚ} (hq : q ≠ 0) : uniform_embedding ((*) q) := _ -/ lemma real.uniform_continuous_inv (s : set ℝ) {r : ℝ} (r0 : 0 < r) (H : ∀ x ∈ s, r ≤ abs x) : uniform_continuous (λp:s, p.1⁻¹) := metric.uniform_continuous_iff.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_inv_continuous_lemma abs ε0 r0 in ⟨δ, δ0, λ a b h, Hδ (H _ a.2) (H _ b.2) h⟩ lemma real.uniform_continuous_abs : uniform_continuous (abs : ℝ → ℝ) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨ε, ε0, λ a b, lt_of_le_of_lt (abs_abs_sub_abs_le_abs_sub _ _)⟩ lemma real.continuous_abs : continuous (abs : ℝ → ℝ) := real.uniform_continuous_abs.continuous lemma rat.uniform_continuous_abs : uniform_continuous (abs : ℚ → ℚ) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨ε, ε0, λ a b h, lt_of_le_of_lt (by simpa [rat.dist_eq] using abs_abs_sub_abs_le_abs_sub _ _) h⟩ lemma rat.continuous_abs : continuous (abs : ℚ → ℚ) := rat.uniform_continuous_abs.continuous lemma real.tendsto_inv {r : ℝ} (r0 : r ≠ 0) : tendsto (λq, q⁻¹) (nhds r) (nhds r⁻¹) := by rw ← abs_pos_iff at r0; exact tendsto_of_uniform_continuous_subtype (real.uniform_continuous_inv {x | abs r / 2 < abs x} (half_pos r0) (λ x h, le_of_lt h)) (mem_nhds_sets (real.continuous_abs _ $ is_open_lt' (abs r / 2)) (half_lt_self r0)) lemma real.continuous_inv' : continuous (λa:{r:ℝ // r ≠ 0}, a.val⁻¹) := continuous_iff_continuous_at.mpr $ assume ⟨r, hr⟩, tendsto.comp (real.tendsto_inv hr) (continuous_iff_continuous_at.mp continuous_subtype_val _) lemma real.continuous_inv [topological_space α] {f : α → ℝ} (h : ∀a, f a ≠ 0) (hf : continuous f) : continuous (λa, (f a)⁻¹) := show continuous ((has_inv.inv ∘ @subtype.val ℝ (λr, r ≠ 0)) ∘ λa, ⟨f a, h a⟩), from real.continuous_inv'.comp (continuous_subtype_mk _ hf) lemma real.uniform_continuous_mul_const {x : ℝ} : uniform_continuous ((*) x) := metric.uniform_continuous_iff.2 $ λ ε ε0, begin cases no_top (abs x) with y xy, have y0 := lt_of_le_of_lt (abs_nonneg _) xy, refine ⟨_, div_pos ε0 y0, λ a b h, _⟩, rw [real.dist_eq, ← mul_sub, abs_mul, ← mul_div_cancel' ε (ne_of_gt y0)], exact mul_lt_mul' (le_of_lt xy) h (abs_nonneg _) y0 end lemma real.uniform_continuous_mul (s : set (ℝ × ℝ)) {r₁ r₂ : ℝ} (r₁0 : 0 < r₁) (r₂0 : 0 < r₂) (H : ∀ x ∈ s, abs (x : ℝ × ℝ).1 < r₁ ∧ abs x.2 < r₂) : uniform_continuous (λp:s, p.1.1 * p.1.2) := metric.uniform_continuous_iff.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_mul_continuous_lemma abs ε0 r₁0 r₂0 in ⟨δ, δ0, λ a b h, let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ (H _ a.2).1 (H _ b.2).2 h₁ h₂⟩ protected lemma real.continuous_mul : continuous (λp : ℝ × ℝ, p.1 * p.2) := continuous_iff_continuous_at.2 $ λ ⟨a₁, a₂⟩, tendsto_of_uniform_continuous_subtype (real.uniform_continuous_mul ({x | abs x < abs a₁ + 1}.prod {x | abs x < abs a₂ + 1}) (lt_of_le_of_lt (abs_nonneg _) (lt_add_one _)) (lt_of_le_of_lt (abs_nonneg _) (lt_add_one _)) (λ x, id)) (mem_nhds_sets (is_open_prod (real.continuous_abs _ $ is_open_gt' (abs a₁ + 1)) (real.continuous_abs _ $ is_open_gt' (abs a₂ + 1))) ⟨lt_add_one (abs a₁), lt_add_one (abs a₂)⟩) instance : topological_ring ℝ := { continuous_mul := real.continuous_mul, ..real.topological_add_group } instance : topological_semiring ℝ := by apply_instance lemma rat.continuous_mul : continuous (λp : ℚ × ℚ, p.1 * p.2) := embedding_of_rat.continuous_iff.2 $ by simp [(∘)]; exact real.continuous_mul.comp ((continuous_of_rat.comp continuous_fst).prod_mk (continuous_of_rat.comp continuous_snd)) instance : topological_ring ℚ := { continuous_mul := rat.continuous_mul, ..rat.topological_add_group } theorem real.ball_eq_Ioo (x ε : ℝ) : ball x ε = Ioo (x - ε) (x + ε) := set.ext $ λ y, by rw [mem_ball, real.dist_eq, abs_sub_lt_iff, sub_lt_iff_lt_add', and_comm, sub_lt]; refl theorem real.Ioo_eq_ball (x y : ℝ) : Ioo x y = ball ((x + y) / 2) ((y - x) / 2) := by rw [real.ball_eq_Ioo, ← sub_div, add_comm, ← sub_add, add_sub_cancel', add_self_div_two, ← add_div, add_assoc, add_sub_cancel'_right, add_self_div_two] lemma real.totally_bounded_Ioo (a b : ℝ) : totally_bounded (Ioo a b) := metric.totally_bounded_iff.2 $ λ ε ε0, begin rcases exists_nat_gt ((b - a) / ε) with ⟨n, ba⟩, rw [div_lt_iff' ε0, sub_lt_iff_lt_add'] at ba, let s := (λ i:ℕ, a + ε * i) '' {i:ℕ | i < n}, refine ⟨s, finite_image _ ⟨set.fintype_lt_nat _⟩, λ x h, _⟩, rcases h with ⟨ax, xb⟩, let i : ℕ := ⌊(x - a) / ε⌋.to_nat, have : (i : ℤ) = ⌊(x - a) / ε⌋ := int.to_nat_of_nonneg (floor_nonneg.2 $ le_of_lt (div_pos (sub_pos.2 ax) ε0)), simp, refine ⟨_, ⟨i, _, rfl⟩, _⟩, { rw [← int.coe_nat_lt, this], refine int.cast_lt.1 (lt_of_le_of_lt (floor_le _) _), rw [int.cast_coe_nat, div_lt_iff' ε0, sub_lt_iff_lt_add'], exact lt_trans xb ba }, { rw [real.dist_eq, ← int.cast_coe_nat, this, abs_of_nonneg, ← sub_sub, sub_lt_iff_lt_add'], { have := lt_floor_add_one ((x - a) / ε), rwa [div_lt_iff' ε0, mul_add, mul_one] at this }, { have := floor_le ((x - a) / ε), rwa [ge, sub_nonneg, ← le_sub_iff_add_le', ← le_div_iff' ε0] } } end lemma real.totally_bounded_ball (x ε : ℝ) : totally_bounded (ball x ε) := by rw real.ball_eq_Ioo; apply real.totally_bounded_Ioo lemma real.totally_bounded_Ico (a b : ℝ) : totally_bounded (Ico a b) := let ⟨c, ac⟩ := no_bot a in totally_bounded_subset (by exact λ x ⟨h₁, h₂⟩, ⟨lt_of_lt_of_le ac h₁, h₂⟩) (real.totally_bounded_Ioo c b) lemma real.totally_bounded_Icc (a b : ℝ) : totally_bounded (Icc a b) := let ⟨c, bc⟩ := no_top b in totally_bounded_subset (by exact λ x ⟨h₁, h₂⟩, ⟨h₁, lt_of_le_of_lt h₂ bc⟩) (real.totally_bounded_Ico a c) lemma rat.totally_bounded_Icc (a b : ℚ) : totally_bounded (Icc a b) := begin have := totally_bounded_preimage uniform_embedding_of_rat (real.totally_bounded_Icc a b), rwa (set.ext (λ q, _) : Icc _ _ = _), simp end -- TODO(Mario): Generalize to first-countable uniform spaces? instance : complete_space ℝ := ⟨λ f cf, begin let g : ℕ → {ε:ℝ//ε>0} := λ n, ⟨n.to_pnat'⁻¹, inv_pos (nat.cast_pos.2 n.to_pnat'.pos)⟩, choose S hS hS_dist using show ∀n:ℕ, ∃t ∈ f.sets, ∀ x y ∈ t, dist x y < g n, from assume n, let ⟨t, tf, h⟩ := (metric.cauchy_iff.1 cf).2 (g n).1 (g n).2 in ⟨t, tf, h⟩, let F : ℕ → set ℝ := λn, ⋂i≤n, S i, have hF : ∀n, F n ∈ f.sets := assume n, Inter_mem_sets (finite_le_nat n) (λ i _, hS i), have hF_dist : ∀n, ∀ x y ∈ F n, dist x y < g n := assume n x y hx hy, have F n ⊆ S n := bInter_subset_of_mem (le_refl n), (hS_dist n) _ _ (this hx) (this hy), choose G hG using assume n:ℕ, inhabited_of_mem_sets cf.1 (hF n), have hg : ∀ ε > 0, ∃ n, ∀ j ≥ n, (g j : ℝ) < ε, { intros ε ε0, cases exists_nat_gt ε⁻¹ with n hn, refine ⟨n, λ j nj, _⟩, have hj := lt_of_lt_of_le hn (nat.cast_le.2 nj), have j0 := lt_trans (inv_pos ε0) hj, have jε := (inv_lt j0 ε0).2 hj, rwa ← pnat.to_pnat'_coe (nat.cast_pos.1 j0) at jε }, let c : cau_seq ℝ abs, { refine ⟨λ n, G n, λ ε ε0, _⟩, cases hg _ ε0 with n hn, refine ⟨n, λ j jn, _⟩, have : F j ⊆ F n := bInter_subset_bInter_left (λ i h, @le_trans _ _ i n j h jn), exact lt_trans (hF_dist n _ _ (this (hG j)) (hG n)) (hn _ $ le_refl _) }, refine ⟨cau_seq.lim c, λ s h, _⟩, rcases metric.mem_nhds_iff.1 h with ⟨ε, ε0, hε⟩, cases exists_forall_ge_and (hg _ $ half_pos ε0) (cau_seq.equiv_lim c _ $ half_pos ε0) with n hn, cases hn _ (le_refl _) with h₁ h₂, refine sets_of_superset _ (hF n) (subset.trans _ $ subset.trans (ball_half_subset (G n) h₂) hε), exact λ x h, lt_trans ((hF_dist n) x (G n) h (hG n)) h₁ end⟩ lemma tendsto_coe_nat_real_at_top_iff {f : α → ℕ} {l : filter α} : tendsto (λ n, (f n : ℝ)) l at_top ↔ tendsto f l at_top := tendsto_at_top_embedding (assume a₁ a₂, nat.cast_le) $ assume r, let ⟨n, hn⟩ := exists_nat_gt r in ⟨n, le_of_lt hn⟩ lemma tendsto_coe_nat_real_at_top_at_top : tendsto (coe : ℕ → ℝ) at_top at_top := tendsto_coe_nat_real_at_top_iff.2 tendsto_id lemma tendsto_coe_int_real_at_top_iff {f : α → ℤ} {l : filter α} : tendsto (λ n, (f n : ℝ)) l at_top ↔ tendsto f l at_top := tendsto_at_top_embedding (assume a₁ a₂, int.cast_le) $ assume r, let ⟨n, hn⟩ := exists_nat_gt r in ⟨(n:ℤ), le_of_lt $ by rwa [int.cast_coe_nat]⟩ lemma tendsto_coe_int_real_at_top_at_top : tendsto (coe : ℤ → ℝ) at_top at_top := tendsto_coe_int_real_at_top_iff.2 tendsto_id section lemma closure_of_rat_image_lt {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q < x}) = {r | ↑q ≤ r} := subset.antisymm ((closure_subset_iff_subset_of_is_closed (is_closed_ge' _)).2 (image_subset_iff.2 $ λ p h, le_of_lt $ (@rat.cast_lt ℝ _ _ _).2 h)) $ λ x hx, mem_closure_iff_nhds.2 $ λ t ht, let ⟨ε, ε0, hε⟩ := metric.mem_nhds_iff.1 ht in let ⟨p, h₁, h₂⟩ := exists_rat_btwn ((lt_add_iff_pos_right x).2 ε0) in ne_empty_iff_exists_mem.2 ⟨_, hε (show abs _ < _, by rwa [abs_of_nonneg (le_of_lt $ sub_pos.2 h₁), sub_lt_iff_lt_add']), p, rat.cast_lt.1 (@lt_of_le_of_lt ℝ _ _ _ _ hx h₁), rfl⟩ /- TODO(Mario): Put these back only if needed later lemma closure_of_rat_image_le_eq {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q ≤ x}) = {r | ↑q ≤ r} := _ lemma closure_of_rat_image_le_le_eq {a b : ℚ} (hab : a ≤ b) : closure (of_rat '' {q:ℚ | a ≤ q ∧ q ≤ b}) = {r:ℝ | of_rat a ≤ r ∧ r ≤ of_rat b} := _-/ lemma compact_Icc {a b : ℝ} : compact (Icc a b) := compact_of_totally_bounded_is_closed (real.totally_bounded_Icc a b) (is_closed_inter (is_closed_ge' a) (is_closed_le' b)) instance : proper_space ℝ := { compact_ball := λx r, by rw closed_ball_Icc; apply compact_Icc } open real lemma real.intermediate_value {f : ℝ → ℝ} {a b t : ℝ} (hf : ∀ x, a ≤ x → x ≤ b → tendsto f (nhds x) (nhds (f x))) (ha : f a ≤ t) (hb : t ≤ f b) (hab : a ≤ b) : ∃ x : ℝ, a ≤ x ∧ x ≤ b ∧ f x = t := let x := real.Sup {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} in have hx₁ : ∃ y, ∀ g ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b}, g ≤ y := ⟨b, λ _ h, h.2.2⟩, have hx₂ : ∃ y, y ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} := ⟨a, ha, le_refl _, hab⟩, have hax : a ≤ x, from le_Sup _ hx₁ ⟨ha, le_refl _, hab⟩, have hxb : x ≤ b, from (Sup_le _ hx₂ hx₁).2 (λ _ h, h.2.2), ⟨x, hax, hxb, eq_of_forall_dist_le $ λ ε ε0, let ⟨δ, hδ0, hδ⟩ := metric.tendsto_nhds_nhds.1 (hf _ hax hxb) ε ε0 in (le_total t (f x)).elim (λ h, le_of_not_gt $ λ hfε, begin rw [dist_eq, abs_of_nonneg (sub_nonneg.2 h)] at hfε, refine mt (Sup_le {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} hx₂ hx₁).2 (not_le_of_gt (sub_lt_self x (half_pos hδ0))) (λ g hg, le_of_not_gt (λ hgδ, not_lt_of_ge hg.1 (lt_trans (lt_sub.1 hfε) (sub_lt_of_sub_lt (lt_of_le_of_lt (le_abs_self _) _))))), rw abs_sub, exact hδ (abs_sub_lt_iff.2 ⟨lt_of_le_of_lt (sub_nonpos.2 (le_Sup _ hx₁ hg)) hδ0, by simp only [x] at *; linarith⟩) end) (λ h, le_of_not_gt $ λ hfε, begin rw [dist_eq, abs_of_nonpos (sub_nonpos.2 h)] at hfε, exact mt (le_Sup {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b}) (λ h : ∀ k, k ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} → k ≤ x, not_le_of_gt ((lt_add_iff_pos_left x).2 (half_pos hδ0)) (h _ ⟨le_trans (le_sub_iff_add_le.2 (le_trans (le_abs_self _) (le_of_lt (hδ $ by rw [dist_eq, add_sub_cancel, abs_of_nonneg (le_of_lt (half_pos hδ0))]; exact half_lt_self hδ0)))) (by linarith), le_trans hax (le_of_lt ((lt_add_iff_pos_left _).2 (half_pos hδ0))), le_of_not_gt (λ hδy, not_lt_of_ge hb (lt_of_le_of_lt (show f b ≤ f b - f x - ε + t, by linarith) (add_lt_of_neg_of_le (sub_neg_of_lt (lt_of_le_of_lt (le_abs_self _) (@hδ b (abs_sub_lt_iff.2 ⟨by simp only [x] at *; linarith, by linarith⟩)))) (le_refl _))))⟩)) hx₁ end)⟩ lemma real.intermediate_value' {f : ℝ → ℝ} {a b t : ℝ} (hf : ∀ x, a ≤ x → x ≤ b → tendsto f (nhds x) (nhds (f x))) (ha : t ≤ f a) (hb : f b ≤ t) (hab : a ≤ b) : ∃ x : ℝ, a ≤ x ∧ x ≤ b ∧ f x = t := let ⟨x, hx₁, hx₂, hx₃⟩ := @real.intermediate_value (λ x, - f x) a b (-t) (λ x hax hxb, tendsto_neg (hf x hax hxb)) (neg_le_neg ha) (neg_le_neg hb) hab in ⟨x, hx₁, hx₂, neg_inj hx₃⟩ lemma real.bounded_iff_bdd_below_bdd_above {s : set ℝ} : bounded s ↔ bdd_below s ∧ bdd_above s := ⟨begin assume bdd, rcases (bounded_iff_subset_ball 0).1 bdd with ⟨r, hr⟩, -- hr : s ⊆ closed_ball 0 r rw closed_ball_Icc at hr, -- hr : s ⊆ Icc (0 - r) (0 + r) exact ⟨⟨-r, λy hy, by simpa using (hr hy).1⟩, ⟨r, λy hy, by simpa using (hr hy).2⟩⟩ end, begin rintros ⟨⟨m, hm⟩, ⟨M, hM⟩⟩, have I : s ⊆ Icc m M := λx hx, ⟨hm x hx, hM x hx⟩, have : Icc m M = closed_ball ((m+M)/2) ((M-m)/2) := by rw closed_ball_Icc; congr; ring, rw this at I, exact bounded.subset I bounded_closed_ball end⟩ end
e0b2c30d1424689ed83a7520f5dece66d902f0c5
958488bc7f3c2044206e0358e56d7690b6ae696c
/lean/first_order.lean
294a9d70d6e328330a1a9185624123ede51e6a28
[]
no_license
possientis/Prog
a08eec1c1b121c2fd6c70a8ae89e2fbef952adb4
d4b3debc37610a88e0dac3ac5914903604fd1d1f
refs/heads/master
1,692,263,717,723
1,691,757,179,000
1,691,757,179,000
40,361,602
3
0
null
1,679,896,438,000
1,438,953,859,000
Coq
UTF-8
Lean
false
false
320
lean
lemma L1 : ∀ (α : Type) (p q : α → Prop), (∀ (x:α), p x ∧ q x) → ∀ (y:α), p y := λ α p q H y, show p y, from (H y).left --#check L1 lemma L2 : ∀ (α : Type) (p q : α → Prop), (∀ (x:α), p x ∧ q x) → ∀ (y:α), p y := assume α p q H y, show p y, from (H y).left --#check L2
c73f001b886ca9d82cf48b698f73fccfb706eeed
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/uniform_space/uniform_convergence_topology.lean
b7309d6fb990ef7629ae5309e6901d4f21693edb
[ "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
46,950
lean
/- Copyright (c) 2022 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker -/ import topology.uniform_space.uniform_convergence import topology.uniform_space.pi import topology.uniform_space.equiv /-! # Topology and uniform structure of uniform convergence > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This files endows `α → β` with the topologies / uniform structures of - uniform convergence on `α` - uniform convergence on a specified family `𝔖` of sets of `α`, also called `𝔖`-convergence Since `α → β` is already endowed with the topologies and uniform structures of pointwise convergence, we introduce type aliases `uniform_fun α β` (denoted `α →ᵤ β`) and `uniform_on_fun α β 𝔖` (denoted `α →ᵤ[𝔖] β`) and we actually endow *these* with the structures of uniform and `𝔖`-convergence respectively. Usual examples of the second construction include : - the topology of compact convergence, when `𝔖` is the set of compacts of `α` - the strong topology on the dual of a topological vector space (TVS) `E`, when `𝔖` is the set of Von Neuman bounded subsets of `E` - the weak-* topology on the dual of a TVS `E`, when `𝔖` is the set of singletons of `E`. This file contains a lot of technical facts, so it is heavily commented, proofs included! ## Main definitions * `uniform_fun.gen`: basis sets for the uniformity of uniform convergence. These are sets of the form `S(V) := {(f, g) | ∀ x : α, (f x, g x) ∈ V}` for some `V : set (β × β)` * `uniform_fun.uniform_space`: uniform structure of uniform convergence. This is the `uniform_space` on `α →ᵤ β` whose uniformity is generated by the sets `S(V)` for `V ∈ 𝓤 β`. We will denote this uniform space as `𝒰(α, β, uβ)`, both in the comments and as a local notation in the Lean code, where `uβ` is the uniform space structure on `β`. This is declared as an instance on `α →ᵤ β`. * `uniform_on_fun.uniform_space`: uniform structure of `𝔖`-convergence, where `𝔖 : set (set α)`. This is the infimum, for `S ∈ 𝔖`, of the pullback of `𝒰 S β` by the map of restriction to `S`. We will denote it `𝒱(α, β, 𝔖, uβ)`, where `uβ` is the uniform space structure on `β`. This is declared as an instance on `α →ᵤ[𝔖] β`. ## Main statements ### Basic properties * `uniform_fun.uniform_continuous_eval`: evaluation is uniformly continuous on `α →ᵤ β`. * `uniform_fun.t2_space`: the topology of uniform convergence on `α →ᵤ β` is T₂ if `β` is T₂. * `uniform_fun.tendsto_iff_tendsto_uniformly`: `𝒰(α, β, uβ)` is indeed the uniform structure of uniform convergence * `uniform_on_fun.uniform_continuous_eval_of_mem`: evaluation at a point contained in a set of `𝔖` is uniformly continuous on `α →ᵤ[𝔖] β` * `uniform_on_fun.t2_space_of_covering`: the topology of `𝔖`-convergence on `α →ᵤ[𝔖] β` is T₂ if `β` is T₂ and `𝔖` covers `α` * `uniform_on_fun.tendsto_iff_tendsto_uniformly_on`: `𝒱(α, β, 𝔖 uβ)` is indeed the uniform structure of `𝔖`-convergence ### Functoriality and compatibility with product of uniform spaces In order to avoid the need for filter bases as much as possible when using these definitions, we develop an extensive API for manipulating these structures abstractly. As usual in the topology section of mathlib, we first state results about the complete lattices of `uniform_space`s on fixed types, and then we use these to deduce categorical-like results about maps between two uniform spaces. We only describe these in the harder case of `𝔖`-convergence, as the names of the corresponding results for uniform convergence can easily be guessed. #### Order statements * `uniform_on_fun.mono`: let `u₁`, `u₂` be two uniform structures on `γ` and `𝔖₁ 𝔖₂ : set (set α)`. If `u₁ ≤ u₂` and `𝔖₂ ⊆ 𝔖₁` then `𝒱(α, γ, 𝔖₁, u₁) ≤ 𝒱(α, γ, 𝔖₂, u₂)`. * `uniform_on_fun.infi_eq`: if `u` is a family of uniform structures on `γ`, then `𝒱(α, γ, 𝔖, (⨅ i, u i)) = ⨅ i, 𝒱(α, γ, 𝔖, u i)`. * `uniform_on_fun.comap_eq`: if `u` is a uniform structures on `β` and `f : γ → β`, then `𝒱(α, γ, 𝔖, comap f u) = comap (λ g, f ∘ g) 𝒱(α, γ, 𝔖, u₁)`. An interesting note about these statements is that they are proved without ever unfolding the basis definition of the uniform structure of uniform convergence! Instead, we build a (not very interesting) Galois connection `uniform_convergence.gc` and then rely on the Galois connection API to do most of the work. #### Morphism statements (unbundled) * `uniform_on_fun.postcomp_uniform_continuous`: if `f : γ → β` is uniformly continuous, then `(λ g, f ∘ g) : (α →ᵤ[𝔖] γ) → (α →ᵤ[𝔖] β)` is uniformly continuous. * `uniform_on_fun.postcomp_uniform_inducing`: if `f : γ → β` is a uniform inducing, then `(λ g, f ∘ g) : (α →ᵤ[𝔖] γ) → (α →ᵤ[𝔖] β)` is a uniform inducing. * `uniform_on_fun.precomp_uniform_continuous`: let `f : γ → α`, `𝔖 : set (set α)`, `𝔗 : set (set γ)`, and assume that `∀ T ∈ 𝔗, f '' T ∈ 𝔖`. Then, the function `(λ g, g ∘ f) : (α →ᵤ[𝔖] β) → (γ →ᵤ[𝔗] β)` is uniformly continuous. #### Isomorphism statements (bundled) * `uniform_on_fun.congr_right`: turn a uniform isomorphism `γ ≃ᵤ β` into a uniform isomorphism `(α →ᵤ[𝔖] γ) ≃ᵤ (α →ᵤ[𝔖] β)` by post-composing. * `uniform_on_fun.congr_left`: turn a bijection `e : γ ≃ α` such that we have both `∀ T ∈ 𝔗, e '' T ∈ 𝔖` and `∀ S ∈ 𝔖, e ⁻¹' S ∈ 𝔗` into a uniform isomorphism `(γ →ᵤ[𝔗] β) ≃ᵤ (α →ᵤ[𝔖] β)` by pre-composing. * `uniform_on_fun.uniform_equiv_Pi_comm`: the natural bijection between `α → Π i, δ i` and `Π i, α → δ i`, upgraded to a uniform isomorphism between `α →ᵤ[𝔖] (Π i, δ i)` and `Π i, α →ᵤ[𝔖] δ i`. #### Important use cases * If `G` is a uniform group, then `α →ᵤ[𝔖] G` is a uniform group: since `(/) : G × G → G` is uniformly continuous, `uniform_convergence_on.postcomp_uniform_continuous` tells us that `((/) ∘ —) : (α →ᵤ[𝔖] G × G) → (α →ᵤ[𝔖] G)` is uniformly continuous. By precomposing with `uniform_convergence_on.uniform_equiv_prod_arrow`, this gives that `(/) : (α →ᵤ[𝔖] G) × (α →ᵤ[𝔖] G) → (α →ᵤ[𝔖] G)` is also uniformly continuous * The transpose of a continuous linear map is continuous for the strong topologies: since continuous linear maps are uniformly continuous and map bounded sets to bounded sets, this is just a special case of `uniform_convergence_on.precomp_uniform_continuous`. ## TODO * Show that the uniform structure of `𝔖`-convergence is exactly the structure of `𝔖'`-convergence, where `𝔖'` is the ***noncovering*** bornology (i.e ***not*** what `bornology` currently refers to in mathlib) generated by `𝔖`. ## References * [N. Bourbaki, *General Topology, Chapter X*][bourbaki1966] ## Tags uniform convergence -/ noncomputable theory open_locale topology classical uniformity filter open set filter section type_alias /-- The type of functions from `α` to `β` equipped with the uniform structure and topology of uniform convergence. We denote it `α →ᵤ β`. -/ def uniform_fun (α β : Type*) := α → β /-- The type of functions from `α` to `β` equipped with the uniform structure and topology of uniform convergence on some family `𝔖` of subsets of `α`. We denote it `α →ᵤ[𝔖] β`. -/ @[nolint unused_arguments] def uniform_on_fun (α β : Type*) (𝔖 : set (set α)) := α → β localized "notation α ` →ᵤ `:25 β:0 := uniform_fun α β" in uniform_convergence localized "notation α ` →ᵤ[`:25 𝔖 `] `:0 β:0 := uniform_on_fun α β 𝔖" in uniform_convergence localized "notation `λᵘ` binders `, ` r:(scoped p, uniform_fun.of_fun p) := r" in uniform_convergence localized "notation `λᵘ[` 𝔖 `] ` binders `, ` r:(scoped p, uniform_fun.of_fun p) := r" in uniform_convergence instance {α β} [nonempty β] : nonempty (α →ᵤ β) := pi.nonempty instance {α β 𝔖} [nonempty β] : nonempty (α →ᵤ[𝔖] β) := pi.nonempty /-- Reinterpret `f : α → β` as an element of `α →ᵤ β`. -/ def uniform_fun.of_fun {α β} : (α → β) ≃ (α →ᵤ β) := ⟨λ x, x, λ x, x, λ x, rfl, λ x, rfl⟩ /-- Reinterpret `f : α → β` as an element of `α →ᵤ[𝔖] β`. -/ def uniform_on_fun.of_fun {α β} (𝔖) : (α → β) ≃ (α →ᵤ[𝔖] β) := ⟨λ x, x, λ x, x, λ x, rfl, λ x, rfl⟩ /-- Reinterpret `f : α →ᵤ β` as an element of `α → β`. -/ def uniform_fun.to_fun {α β} : (α →ᵤ β) ≃ (α → β) := uniform_fun.of_fun.symm /-- Reinterpret `f : α →ᵤ[𝔖] β` as an element of `α → β`. -/ def uniform_on_fun.to_fun {α β} (𝔖) : (α →ᵤ[𝔖] β) ≃ (α → β) := (uniform_on_fun.of_fun 𝔖).symm -- Note: we don't declare a `has_coe_to_fun` instance because Lean wouldn't insert it when writing -- `f x` (because of definitional equality with `α → β`). end type_alias open_locale uniform_convergence namespace uniform_fun variables (α β : Type*) {γ ι : Type*} variables {s s' : set α} {x : α} {p : filter ι} {g : ι → α} /-- Basis sets for the uniformity of uniform convergence: `gen α β V` is the set of pairs `(f, g)` of functions `α →ᵤ β` such that `∀ x, (f x, g x) ∈ V`. -/ protected def gen (V : set (β × β)) : set ((α →ᵤ β) × (α →ᵤ β)) := {uv : (α →ᵤ β) × (α →ᵤ β) | ∀ x, (uv.1 x, uv.2 x) ∈ V} /-- If `𝓕` is a filter on `β × β`, then the set of all `uniform_convergence.gen α β V` for `V ∈ 𝓕` is a filter basis on `(α →ᵤ β) × (α →ᵤ β)`. This will only be applied to `𝓕 = 𝓤 β` when `β` is equipped with a `uniform_space` structure, but it is useful to define it for any filter in order to be able to state that it has a lower adjoint (see `uniform_convergence.gc`). -/ protected lemma is_basis_gen (𝓑 : filter $ β × β) : is_basis (λ V : set (β × β), V ∈ 𝓑) (uniform_fun.gen α β) := ⟨⟨univ, univ_mem⟩, λ U V hU hV, ⟨U ∩ V, inter_mem hU hV, λ uv huv, ⟨λ x, (huv x).left, λ x, (huv x).right⟩⟩⟩ /-- For `𝓕 : filter (β × β)`, this is the set of all `uniform_convergence.gen α β V` for `V ∈ 𝓕` as a bundled `filter_basis` over `(α →ᵤ β) × (α →ᵤ β)`. This will only be applied to `𝓕 = 𝓤 β` when `β` is equipped with a `uniform_space` structure, but it is useful to define it for any filter in order to be able to state that it has a lower adjoint (see `uniform_convergence.gc`). -/ protected def basis (𝓕 : filter $ β × β) : filter_basis ((α →ᵤ β) × (α →ᵤ β)) := (uniform_fun.is_basis_gen α β 𝓕).filter_basis /-- For `𝓕 : filter (β × β)`, this is the filter generated by the filter basis `uniform_convergence.basis α β 𝓕`. For `𝓕 = 𝓤 β`, this will be the uniformity of uniform convergence on `α`. -/ protected def filter (𝓕 : filter $ β × β) : filter ((α →ᵤ β) × (α →ᵤ β)) := (uniform_fun.basis α β 𝓕).filter local notation `Φ` := λ (α β : Type*) (uvx : ((α →ᵤ β) × (α →ᵤ β)) × α), (uvx.1.1 uvx.2, uvx.1.2 uvx.2) /- This is a lower adjoint to `uniform_convergence.filter` (see `uniform_convergence.gc`). The exact definition of the lower adjoint `l` is not interesting; we will only use that it exists (in `uniform_convergence.mono` and `uniform_convergence.infi_eq`) and that `l (filter.map (prod.map f f) 𝓕) = filter.map (prod.map ((∘) f) ((∘) f)) (l 𝓕)` for each `𝓕 : filter (γ × γ)` and `f : γ → α` (in `uniform_convergence.comap_eq`). -/ local notation `lower_adjoint` := λ 𝓐, map (Φ α β) (𝓐 ×ᶠ ⊤) /-- The function `uniform_convergence.filter α β : filter (β × β) → filter ((α →ᵤ β) × (α →ᵤ β))` has a lower adjoint `l` (in the sense of `galois_connection`). The exact definition of `l` is not interesting; we will only use that it exists (in `uniform_convergence.mono` and `uniform_convergence.infi_eq`) and that `l (filter.map (prod.map f f) 𝓕) = filter.map (prod.map ((∘) f) ((∘) f)) (l 𝓕)` for each `𝓕 : filter (γ × γ)` and `f : γ → α` (in `uniform_convergence.comap_eq`). -/ protected lemma gc : galois_connection lower_adjoint (λ 𝓕, uniform_fun.filter α β 𝓕) := begin intros 𝓐 𝓕, symmetry, calc 𝓐 ≤ uniform_fun.filter α β 𝓕 ↔ (uniform_fun.basis α β 𝓕).sets ⊆ 𝓐.sets : by rw [uniform_fun.filter, ← filter_basis.generate, sets_iff_generate] ... ↔ ∀ U ∈ 𝓕, uniform_fun.gen α β U ∈ 𝓐 : image_subset_iff ... ↔ ∀ U ∈ 𝓕, {uv | ∀ x, (uv, x) ∈ {t : ((α →ᵤ β) × (α →ᵤ β)) × α | (t.1.1 t.2, t.1.2 t.2) ∈ U}} ∈ 𝓐 : iff.rfl ... ↔ ∀ U ∈ 𝓕, {uvx : ((α →ᵤ β) × (α →ᵤ β)) × α | (uvx.1.1 uvx.2, uvx.1.2 uvx.2) ∈ U} ∈ 𝓐 ×ᶠ (⊤ : filter α) : forall₂_congr (λ U hU, mem_prod_top.symm) ... ↔ lower_adjoint 𝓐 ≤ 𝓕 : iff.rfl, end variables [uniform_space β] /-- Core of the uniform structure of uniform convergence. -/ protected def uniform_core : uniform_space.core (α →ᵤ β) := uniform_space.core.mk_of_basis (uniform_fun.basis α β (𝓤 β)) (λ U ⟨V, hV, hVU⟩ f, hVU ▸ λ x, refl_mem_uniformity hV) (λ U ⟨V, hV, hVU⟩, hVU ▸ ⟨uniform_fun.gen α β (prod.swap ⁻¹' V), ⟨prod.swap ⁻¹' V, tendsto_swap_uniformity hV, rfl⟩, λ uv huv x, huv x⟩) (λ U ⟨V, hV, hVU⟩, hVU ▸ let ⟨W, hW, hWV⟩ := comp_mem_uniformity_sets hV in ⟨uniform_fun.gen α β W, ⟨W, hW, rfl⟩, λ uv ⟨w, huw, hwv⟩ x, hWV ⟨w x, by exact ⟨huw x, hwv x⟩⟩⟩) /-- Uniform structure of uniform convergence, declared as an instance on `α →ᵤ β`. We will denote it `𝒰(α, β, uβ)` in the rest of this file. -/ instance : uniform_space (α →ᵤ β) := uniform_space.of_core (uniform_fun.uniform_core α β) /-- Topology of uniform convergence, declared as an instance on `α →ᵤ β`. -/ instance : topological_space (α →ᵤ β) := infer_instance local notation `𝒰(`α`, `β`, `u`)` := @uniform_fun.uniform_space α β u /-- By definition, the uniformity of `α →ᵤ β` admits the family `{(f, g) | ∀ x, (f x, g x) ∈ V}` for `V ∈ 𝓤 β` as a filter basis. -/ protected lemma has_basis_uniformity : (𝓤 (α →ᵤ β)).has_basis (λ V, V ∈ 𝓤 β) (uniform_fun.gen α β) := (uniform_fun.is_basis_gen α β (𝓤 β)).has_basis /-- The uniformity of `α →ᵤ β` admits the family `{(f, g) | ∀ x, (f x, g x) ∈ V}` for `V ∈ 𝓑` as a filter basis, for any basis `𝓑` of `𝓤 β` (in the case `𝓑 = (𝓤 β).as_basis` this is true by definition). -/ protected lemma has_basis_uniformity_of_basis {ι : Sort*} {p : ι → Prop} {s : ι → set (β × β)} (h : (𝓤 β).has_basis p s) : (𝓤 (α →ᵤ β)).has_basis p (uniform_fun.gen α β ∘ s) := (uniform_fun.has_basis_uniformity α β).to_has_basis (λ U hU, let ⟨i, hi, hiU⟩ := h.mem_iff.mp hU in ⟨i, hi, λ uv huv x, hiU (huv x)⟩) (λ i hi, ⟨s i, h.mem_of_mem hi, subset_refl _⟩) /-- For `f : α →ᵤ β`, `𝓝 f` admits the family `{g | ∀ x, (f x, g x) ∈ V}` for `V ∈ 𝓑` as a filter basis, for any basis `𝓑` of `𝓤 β`. -/ protected lemma has_basis_nhds_of_basis (f) {p : ι → Prop} {s : ι → set (β × β)} (h : has_basis (𝓤 β) p s) : (𝓝 f).has_basis p (λ i, {g | (f, g) ∈ uniform_fun.gen α β (s i)}) := nhds_basis_uniformity' (uniform_fun.has_basis_uniformity_of_basis α β h) /-- For `f : α →ᵤ β`, `𝓝 f` admits the family `{g | ∀ x, (f x, g x) ∈ V}` for `V ∈ 𝓤 β` as a filter basis. -/ protected lemma has_basis_nhds (f) : (𝓝 f).has_basis (λ V, V ∈ 𝓤 β) (λ V, {g | (f, g) ∈ uniform_fun.gen α β V}) := uniform_fun.has_basis_nhds_of_basis α β f (filter.basis_sets _) variables {α} /-- Evaluation at a fixed point is uniformly continuous on `α →ᵤ β`. -/ lemma uniform_continuous_eval (x : α) : uniform_continuous (function.eval x ∘ to_fun : (α →ᵤ β) → β) := begin change _ ≤ _, rw [map_le_iff_le_comap, (uniform_fun.has_basis_uniformity α β).le_basis_iff ((𝓤 _).basis_sets.comap _)], exact λ U hU, ⟨U, hU, λ uv huv, huv x⟩ end variables {β} /-- If `u₁` and `u₂` are two uniform structures on `γ` and `u₁ ≤ u₂`, then `𝒰(α, γ, u₁) ≤ 𝒰(α, γ, u₂)`. -/ protected lemma mono : monotone (@uniform_fun.uniform_space α γ) := λ u₁ u₂ hu, (uniform_fun.gc α γ).monotone_u hu /-- If `u` is a family of uniform structures on `γ`, then `𝒰(α, γ, (⨅ i, u i)) = ⨅ i, 𝒰(α, γ, u i)`. -/ protected lemma infi_eq {u : ι → uniform_space γ} : (𝒰(α, γ, ⨅ i, u i)) = ⨅ i, 𝒰(α, γ, u i) := begin -- This follows directly from the fact that the upper adjoint in a Galois connection maps -- infimas to infimas. ext : 1, change uniform_fun.filter α γ (𝓤[⨅ i, u i]) = 𝓤[⨅ i, 𝒰(α, γ, u i)], rw [infi_uniformity, infi_uniformity], exact (uniform_fun.gc α γ).u_infi end /-- If `u₁` and `u₂` are two uniform structures on `γ`, then `𝒰(α, γ, u₁ ⊓ u₂) = 𝒰(α, γ, u₁) ⊓ 𝒰(α, γ, u₂)`. -/ protected lemma inf_eq {u₁ u₂ : uniform_space γ} : (𝒰(α, γ, u₁ ⊓ u₂)) = (𝒰(α, γ, u₁)) ⊓ (𝒰(α, γ, u₂)) := begin -- This follows directly from the fact that the upper adjoint in a Galois connection maps -- infimas to infimas. rw [inf_eq_infi, inf_eq_infi, uniform_fun.infi_eq], refine infi_congr (λ i, _), cases i; refl end /-- If `u` is a uniform structures on `β` and `f : γ → β`, then `𝒰(α, γ, comap f u) = comap (λ g, f ∘ g) 𝒰(α, γ, u₁)`. -/ protected lemma comap_eq {f : γ → β} : (𝒰(α, γ, ‹uniform_space β›.comap f)) = (𝒰(α, β, _)).comap ((∘) f) := begin letI : uniform_space γ := ‹uniform_space β›.comap f, ext : 1, change (uniform_fun.filter α γ ((𝓤 β).comap _)) = (uniform_fun.filter α β ((𝓤 β))).comap _, -- We have the following four Galois connection which form a square diagram, and we want -- to show that the square of upper adjoints is commutative. The trick then is to use -- `galois_connection.u_comm_of_l_comm` to reduce it to commutativity of the lower adjoints, -- which is way easier to prove. have h₁ := filter.gc_map_comap (prod.map ((∘) f) ((∘) f)), have h₂ := filter.gc_map_comap (prod.map f f), have h₃ := uniform_fun.gc α β, have h₄ := uniform_fun.gc α γ, refine galois_connection.u_comm_of_l_comm h₁ h₂ h₃ h₄ (λ 𝓐, _), have : prod.map f f ∘ (Φ α γ) = (Φ α β) ∘ prod.map (prod.map ((∘) f) ((∘) f)) id, { ext; refl }, rw [map_comm this, ← prod_map_map_eq'], refl end /-- Post-composition by a uniformly continuous function is uniformly continuous on `α →ᵤ β`. More precisely, if `f : γ → β` is uniformly continuous, then `(λ g, f ∘ g) : (α →ᵤ γ) → (α →ᵤ β)` is uniformly continuous. -/ protected lemma postcomp_uniform_continuous [uniform_space γ] {f : γ → β} (hf : uniform_continuous f): uniform_continuous (of_fun ∘ ((∘) f) ∘ to_fun : (α →ᵤ γ) → (α →ᵤ β)) := -- This is a direct consequence of `uniform_convergence.comap_eq` uniform_continuous_iff.mpr $ calc 𝒰(α, γ, _) ≤ 𝒰(α, γ, ‹uniform_space β›.comap f) : uniform_fun.mono (uniform_continuous_iff.mp hf) ... = (𝒰(α, β, _)).comap ((∘) f) : uniform_fun.comap_eq /-- Post-composition by a uniform inducing is a uniform inducing for the uniform structures of uniform convergence. More precisely, if `f : γ → β` is a uniform inducing, then `(λ g, f ∘ g) : (α →ᵤ γ) → (α →ᵤ β)` is a uniform inducing. -/ protected lemma postcomp_uniform_inducing [uniform_space γ] {f : γ → β} (hf : uniform_inducing f): uniform_inducing (of_fun ∘ ((∘) f) ∘ to_fun : (α →ᵤ γ) → (α →ᵤ β)) := -- This is a direct consequence of `uniform_convergence.comap_eq` begin split, replace hf : (𝓤 β).comap (prod.map f f) = _ := hf.comap_uniformity, change comap (prod.map (of_fun ∘ (∘) f ∘ to_fun) (of_fun ∘ (∘) f ∘ to_fun)) _ = _, rw [← uniformity_comap] at ⊢ hf, congr, rw [← uniform_space_eq hf, uniform_fun.comap_eq], refl end /-- Turn a uniform isomorphism `γ ≃ᵤ β` into a uniform isomorphism `(α →ᵤ γ) ≃ᵤ (α →ᵤ β)` by post-composing. -/ protected def congr_right [uniform_space γ] (e : γ ≃ᵤ β) : (α →ᵤ γ) ≃ᵤ (α →ᵤ β) := { uniform_continuous_to_fun := uniform_fun.postcomp_uniform_continuous e.uniform_continuous, uniform_continuous_inv_fun := uniform_fun.postcomp_uniform_continuous e.symm.uniform_continuous, .. equiv.Pi_congr_right (λ a, e.to_equiv) } /-- Pre-composition by a any function is uniformly continuous for the uniform structures of uniform convergence. More precisely, for any `f : γ → α`, the function `(λ g, g ∘ f) : (α →ᵤ β) → (γ →ᵤ β)` is uniformly continuous. -/ protected lemma precomp_uniform_continuous {f : γ → α} : uniform_continuous (λ g : α →ᵤ β, of_fun (g ∘ f)) := begin -- Here we simply go back to filter bases. rw uniform_continuous_iff, change 𝓤 (α →ᵤ β) ≤ (𝓤 (γ →ᵤ β)).comap (prod.map (λ g : α →ᵤ β, g ∘ f) (λ g : α →ᵤ β, g ∘ f)), rw (uniform_fun.has_basis_uniformity α β).le_basis_iff ((uniform_fun.has_basis_uniformity γ β).comap _), exact λ U hU, ⟨U, hU, λ uv huv x, huv (f x)⟩ end /-- Turn a bijection `γ ≃ α` into a uniform isomorphism `(γ →ᵤ β) ≃ᵤ (α →ᵤ β)` by pre-composing. -/ protected def congr_left (e : γ ≃ α) : (γ →ᵤ β) ≃ᵤ (α →ᵤ β) := { uniform_continuous_to_fun := uniform_fun.precomp_uniform_continuous, uniform_continuous_inv_fun := uniform_fun.precomp_uniform_continuous, .. equiv.arrow_congr e (equiv.refl _) } /-- The topology of uniform convergence is T₂. -/ instance [t2_space β] : t2_space (α →ᵤ β) := { t2 := begin intros f g h, obtain ⟨x, hx⟩ := not_forall.mp (mt funext h), exact separated_by_continuous (uniform_continuous_eval β x).continuous hx end } /-- The natural map `uniform_fun.to_fun` from `α →ᵤ β` to `α → β` is uniformly continuous. In other words, the uniform structure of uniform convergence is finer than that of pointwise convergence, aka the product uniform structure. -/ protected lemma uniform_continuous_to_fun : uniform_continuous (to_fun : (α →ᵤ β) → α → β) := begin -- By definition of the product uniform structure, this is just `uniform_continuous_eval`. rw uniform_continuous_pi, intros x, exact uniform_continuous_eval β x end /-- The topology of uniform convergence indeed gives the same notion of convergence as `tendsto_uniformly`. -/ protected lemma tendsto_iff_tendsto_uniformly {F : ι → α →ᵤ β} {f : α →ᵤ β} : tendsto F p (𝓝 f) ↔ tendsto_uniformly F f p := begin rw [(uniform_fun.has_basis_nhds α β f).tendsto_right_iff, tendsto_uniformly], exact iff.rfl, end /-- The natural bijection between `α → β × γ` and `(α → β) × (α → γ)`, upgraded to a uniform isomorphism between `α →ᵤ β × γ` and `(α →ᵤ β) × (α →ᵤ γ)`. -/ protected def uniform_equiv_prod_arrow [uniform_space γ] : (α →ᵤ β × γ) ≃ᵤ ((α →ᵤ β) × (α →ᵤ γ)) := -- Denote `φ` this bijection. We want to show that -- `comap φ (𝒰(α, β, uβ) × 𝒰(α, γ, uγ)) = 𝒰(α, β × γ, uβ × uγ)`. -- But `uβ × uγ` is defined as `comap fst uβ ⊓ comap snd uγ`, so we just have to apply -- `uniform_convergence.inf_eq` and `uniform_convergence.comap_eq`, which leaves us to check -- that some square commutes. (equiv.arrow_prod_equiv_prod_arrow _ _ _).to_uniform_equiv_of_uniform_inducing begin split, change comap (prod.map (equiv.arrow_prod_equiv_prod_arrow _ _ _) (equiv.arrow_prod_equiv_prod_arrow _ _ _)) _ = _, rw ← uniformity_comap, congr, rw [prod.uniform_space, prod.uniform_space, uniform_space.comap_inf, uniform_fun.inf_eq], congr; rw [← uniform_space.comap_comap, uniform_fun.comap_eq]; refl -- the relevant diagram commutes by definition end variables (α) (δ : ι → Type*) [Π i, uniform_space (δ i)] /-- The natural bijection between `α → Π i, δ i` and `Π i, α → δ i`, upgraded to a uniform isomorphism between `α →ᵤ (Π i, δ i)` and `Π i, α →ᵤ δ i`. -/ protected def uniform_equiv_Pi_comm : uniform_equiv (α →ᵤ Π i, δ i) (Π i, α →ᵤ δ i) := -- Denote `φ` this bijection. We want to show that -- `comap φ (Π i, 𝒰(α, δ i, uδ i)) = 𝒰(α, (Π i, δ i), (Π i, uδ i))`. -- But `Π i, uδ i` is defined as `⨅ i, comap (eval i) (uδ i)`, so we just have to apply -- `uniform_convergence.infi_eq` and `uniform_convergence.comap_eq`, which leaves us to check -- that some square commutes. @equiv.to_uniform_equiv_of_uniform_inducing _ _ (𝒰(α, Π i, δ i, Pi.uniform_space δ)) (@Pi.uniform_space ι (λ i, α → δ i) (λ i, 𝒰(α, δ i, _))) (equiv.Pi_comm _) begin split, change comap (prod.map function.swap function.swap) _ = _, rw ← uniformity_comap, congr, rw [Pi.uniform_space, uniform_space.of_core_eq_to_core, Pi.uniform_space, uniform_space.of_core_eq_to_core, uniform_space.comap_infi, uniform_fun.infi_eq], refine infi_congr (λ i, _), rw [← uniform_space.comap_comap, uniform_fun.comap_eq] -- Like in the previous lemma, the diagram actually commutes by definition end end uniform_fun namespace uniform_on_fun variables {α β : Type*} {γ ι : Type*} variables {s s' : set α} {x : α} {p : filter ι} {g : ι → α} local notation `𝒰(`α`, `β`, `u`)` := @uniform_fun.uniform_space α β u /-- Basis sets for the uniformity of `𝔖`-convergence: for `S : set α` and `V : set (β × β)`, `gen 𝔖 S V` is the set of pairs `(f, g)` of functions `α →ᵤ[𝔖] β` such that `∀ x ∈ S, (f x, g x) ∈ V`. Note that the family `𝔖 : set (set α)` is only used to specify which type alias of `α → β` to use here. -/ protected def gen (𝔖) (S : set α) (V : set (β × β)) : set ((α →ᵤ[𝔖] β) × (α →ᵤ[𝔖] β)) := {uv : (α →ᵤ[𝔖] β) × (α →ᵤ[𝔖] β) | ∀ x ∈ S, (uv.1 x, uv.2 x) ∈ V} /-- For `S : set α` and `V : set (β × β)`, we have `uniform_on_fun.gen 𝔖 S V = (S.restrict × S.restrict) ⁻¹' (uniform_fun.gen S β V)`. This is the crucial fact for proving that the family `uniform_on_fun.gen S V` for `S ∈ 𝔖` and `V ∈ 𝓤 β` is indeed a basis for the uniformity `α →ᵤ[𝔖] β` endowed with `𝒱(α, β, 𝔖, uβ)` the uniform structure of `𝔖`-convergence, as defined in `uniform_on_fun.uniform_space`. -/ protected lemma gen_eq_preimage_restrict {𝔖} (S : set α) (V : set (β × β)) : uniform_on_fun.gen 𝔖 S V = (prod.map S.restrict S.restrict) ⁻¹' (uniform_fun.gen S β V) := begin ext uv, exact ⟨λ h ⟨x, hx⟩, h x hx, λ h x hx, h ⟨x, hx⟩⟩ end /-- `uniform_on_fun.gen` is antitone in the first argument and monotone in the second. -/ protected lemma gen_mono {𝔖} {S S' : set α} {V V' : set (β × β)} (hS : S' ⊆ S) (hV : V ⊆ V') : uniform_on_fun.gen 𝔖 S V ⊆ uniform_on_fun.gen 𝔖 S' V' := λ uv h x hx, hV (h x $ hS hx) /-- If `𝔖 : set (set α)` is nonempty and directed and `𝓑` is a filter basis on `β × β`, then the family `uniform_on_fun.gen 𝔖 S V` for `S ∈ 𝔖` and `V ∈ 𝓑` is a filter basis on `(α →ᵤ[𝔖] β) × (α →ᵤ[𝔖] β)`. We will show in `has_basis_uniformity_of_basis` that, if `𝓑` is a basis for `𝓤 β`, then the corresponding filter is the uniformity of `α →ᵤ[𝔖] β`. -/ protected lemma is_basis_gen (𝔖 : set (set α)) (h : 𝔖.nonempty) (h' : directed_on (⊆) 𝔖) (𝓑 : filter_basis $ β × β) : is_basis (λ SV : set α × set (β × β), SV.1 ∈ 𝔖 ∧ SV.2 ∈ 𝓑) (λ SV, uniform_on_fun.gen 𝔖 SV.1 SV.2) := ⟨h.prod 𝓑.nonempty, λ U₁V₁ U₂V₂ h₁ h₂, let ⟨U₃, hU₃, hU₁₃, hU₂₃⟩ := h' U₁V₁.1 h₁.1 U₂V₂.1 h₂.1 in let ⟨V₃, hV₃, hV₁₂₃⟩ := 𝓑.inter_sets h₁.2 h₂.2 in ⟨⟨U₃, V₃⟩, ⟨⟨hU₃, hV₃⟩, λ uv huv, ⟨(λ x hx, (hV₁₂₃ $ huv x $ hU₁₃ hx).1), (λ x hx, (hV₁₂₃ $ huv x $ hU₂₃ hx).2)⟩⟩⟩⟩ variables (α β) [uniform_space β] (𝔖 : set (set α)) /-- Uniform structure of `𝔖`-convergence, i.e uniform convergence on the elements of `𝔖`, declared as an instance on `α →ᵤ[𝔖] β`. It is defined as the infimum, for `S ∈ 𝔖`, of the pullback by `S.restrict`, the map of restriction to `S`, of the uniform structure `𝒰(s, β, uβ)` on `↥S →ᵤ β`. We will denote it `𝒱(α, β, 𝔖, uβ)`, where `uβ` is the uniform structure on `β`. -/ instance : uniform_space (α →ᵤ[𝔖] β) := ⨅ (s : set α) (hs : s ∈ 𝔖), uniform_space.comap s.restrict (𝒰(s, β, _)) local notation `𝒱(`α`, `β`, `𝔖`, `u`)` := @uniform_on_fun.uniform_space α β u 𝔖 /-- Topology of `𝔖`-convergence, i.e uniform convergence on the elements of `𝔖`, declared as an instance on `α →ᵤ[𝔖] β`. -/ instance : topological_space (α →ᵤ[𝔖] β) := (𝒱(α, β, 𝔖, _)).to_topological_space /-- The topology of `𝔖`-convergence is the infimum, for `S ∈ 𝔖`, of topology induced by the map of `S.restrict : (α →ᵤ[𝔖] β) → (↥S →ᵤ β)` of restriction to `S`, where `↥S →ᵤ β` is endowed with the topology of uniform convergence. -/ protected lemma topological_space_eq : uniform_on_fun.topological_space α β 𝔖 = ⨅ (s : set α) (hs : s ∈ 𝔖), topological_space.induced s.restrict (uniform_fun.topological_space s β) := begin simp only [uniform_on_fun.topological_space, to_topological_space_infi, to_topological_space_infi, to_topological_space_comap], refl end protected lemma has_basis_uniformity_of_basis_aux₁ {p : ι → Prop} {s : ι → set (β × β)} (hb : has_basis (𝓤 β) p s) (S : set α) : (@uniformity (α →ᵤ[𝔖] β) ((uniform_fun.uniform_space S β).comap S.restrict)).has_basis p (λ i, uniform_on_fun.gen 𝔖 S (s i)) := begin simp_rw [uniform_on_fun.gen_eq_preimage_restrict, uniformity_comap], exact (uniform_fun.has_basis_uniformity_of_basis S β hb).comap _ end protected lemma has_basis_uniformity_of_basis_aux₂ (h : directed_on (⊆) 𝔖) {p : ι → Prop} {s : ι → set (β × β)} (hb : has_basis (𝓤 β) p s) : directed_on ((λ s : set α, (uniform_fun.uniform_space s β).comap (s.restrict : (α →ᵤ β) → s →ᵤ β)) ⁻¹'o ge) 𝔖 := h.mono $ λ s t hst, ((uniform_on_fun.has_basis_uniformity_of_basis_aux₁ α β 𝔖 hb _).le_basis_iff (uniform_on_fun.has_basis_uniformity_of_basis_aux₁ α β 𝔖 hb _)).mpr (λ V hV, ⟨V, hV, uniform_on_fun.gen_mono hst subset_rfl⟩) /-- If `𝔖 : set (set α)` is nonempty and directed and `𝓑` is a filter basis of `𝓤 β`, then the uniformity of `α →ᵤ[𝔖] β` admits the family `{(f, g) | ∀ x ∈ S, (f x, g x) ∈ V}` for `S ∈ 𝔖` and `V ∈ 𝓑` as a filter basis. -/ protected lemma has_basis_uniformity_of_basis (h : 𝔖.nonempty) (h' : directed_on (⊆) 𝔖) {p : ι → Prop} {s : ι → set (β × β)} (hb : has_basis (𝓤 β) p s) : (𝓤 (α →ᵤ[𝔖] β)).has_basis (λ Si : set α × ι, Si.1 ∈ 𝔖 ∧ p Si.2) (λ Si, uniform_on_fun.gen 𝔖 Si.1 (s Si.2)) := begin simp only [infi_uniformity], exact has_basis_binfi_of_directed h (λ S, (uniform_on_fun.gen 𝔖 S) ∘ s) _ (λ S hS, uniform_on_fun.has_basis_uniformity_of_basis_aux₁ α β 𝔖 hb S) (uniform_on_fun.has_basis_uniformity_of_basis_aux₂ α β 𝔖 h' hb) end /-- If `𝔖 : set (set α)` is nonempty and directed, then the uniformity of `α →ᵤ[𝔖] β` admits the family `{(f, g) | ∀ x ∈ S, (f x, g x) ∈ V}` for `S ∈ 𝔖` and `V ∈ 𝓤 β` as a filter basis. -/ protected lemma has_basis_uniformity (h : 𝔖.nonempty) (h' : directed_on (⊆) 𝔖) : (𝓤 (α →ᵤ[𝔖] β)).has_basis (λ SV : set α × set (β × β), SV.1 ∈ 𝔖 ∧ SV.2 ∈ 𝓤 β) (λ SV, uniform_on_fun.gen 𝔖 SV.1 SV.2) := uniform_on_fun.has_basis_uniformity_of_basis α β 𝔖 h h' (𝓤 β).basis_sets /-- For `f : α →ᵤ[𝔖] β`, where `𝔖 : set (set α)` is nonempty and directed, `𝓝 f` admits the family `{g | ∀ x ∈ S, (f x, g x) ∈ V}` for `S ∈ 𝔖` and `V ∈ 𝓑` as a filter basis, for any basis `𝓑` of `𝓤 β`. -/ protected lemma has_basis_nhds_of_basis (f : α →ᵤ[𝔖] β) (h : 𝔖.nonempty) (h' : directed_on (⊆) 𝔖) {p : ι → Prop} {s : ι → set (β × β)} (hb : has_basis (𝓤 β) p s) : (𝓝 f).has_basis (λ Si : set α × ι, Si.1 ∈ 𝔖 ∧ p Si.2) (λ Si, {g | (g, f) ∈ uniform_on_fun.gen 𝔖 Si.1 (s Si.2)}) := begin letI : uniform_space (α → β) := uniform_on_fun.uniform_space α β 𝔖, exact nhds_basis_uniformity (uniform_on_fun.has_basis_uniformity_of_basis α β 𝔖 h h' hb) end /-- For `f : α →ᵤ[𝔖] β`, where `𝔖 : set (set α)` is nonempty and directed, `𝓝 f` admits the family `{g | ∀ x ∈ S, (f x, g x) ∈ V}` for `S ∈ 𝔖` and `V ∈ 𝓤 β` as a filter basis. -/ protected lemma has_basis_nhds (f : α →ᵤ[𝔖] β) (h : 𝔖.nonempty) (h' : directed_on (⊆) 𝔖) : (𝓝 f).has_basis (λ SV : set α × set (β × β), SV.1 ∈ 𝔖 ∧ SV.2 ∈ 𝓤 β) (λ SV, {g | (g, f) ∈ uniform_on_fun.gen 𝔖 SV.1 SV.2}) := uniform_on_fun.has_basis_nhds_of_basis α β 𝔖 f h h' (filter.basis_sets _) /-- If `S ∈ 𝔖`, then the restriction to `S` is a uniformly continuous map from `α →ᵤ[𝔖] β` to `↥S →ᵤ β`. -/ protected lemma uniform_continuous_restrict (h : s ∈ 𝔖) : uniform_continuous (uniform_fun.of_fun ∘ (s.restrict : (α → β) → (s → β)) ∘ (to_fun 𝔖)) := begin change _ ≤ _, simp only [uniform_on_fun.uniform_space, map_le_iff_le_comap, infi_uniformity], exact infi₂_le s h end variables {α} /-- Let `u₁`, `u₂` be two uniform structures on `γ` and `𝔖₁ 𝔖₂ : set (set α)`. If `u₁ ≤ u₂` and `𝔖₂ ⊆ 𝔖₁` then `𝒱(α, γ, 𝔖₁, u₁) ≤ 𝒱(α, γ, 𝔖₂, u₂)`. -/ protected lemma mono ⦃u₁ u₂ : uniform_space γ⦄ (hu : u₁ ≤ u₂) ⦃𝔖₁ 𝔖₂ : set (set α)⦄ (h𝔖 : 𝔖₂ ⊆ 𝔖₁) : 𝒱(α, γ, 𝔖₁, u₁) ≤ 𝒱(α, γ, 𝔖₂, u₂) := calc 𝒱(α, γ, 𝔖₁, u₁) ≤ 𝒱(α, γ, 𝔖₂, u₁) : infi_le_infi_of_subset h𝔖 ... ≤ 𝒱(α, γ, 𝔖₂, u₂) : infi₂_mono (λ i hi, uniform_space.comap_mono $ uniform_fun.mono hu) /-- If `x : α` is in some `S ∈ 𝔖`, then evaluation at `x` is uniformly continuous on `α →ᵤ[𝔖] β`. -/ lemma uniform_continuous_eval_of_mem {x : α} (hxs : x ∈ s) (hs : s ∈ 𝔖) : uniform_continuous ((function.eval x : (α → β) → β) ∘ to_fun 𝔖) := (uniform_fun.uniform_continuous_eval β (⟨x, hxs⟩ : s)).comp (uniform_on_fun.uniform_continuous_restrict α β 𝔖 hs) variables {β} {𝔖} /-- If `u` is a family of uniform structures on `γ`, then `𝒱(α, γ, 𝔖, (⨅ i, u i)) = ⨅ i, 𝒱(α, γ, 𝔖, u i)`. -/ protected lemma infi_eq {u : ι → uniform_space γ} : 𝒱(α, γ, 𝔖, ⨅ i, u i) = ⨅ i, 𝒱(α, γ, 𝔖, u i) := begin simp_rw [uniform_on_fun.uniform_space, uniform_fun.infi_eq, uniform_space.comap_infi], rw infi_comm, exact infi_congr (λ s, infi_comm) end /-- If `u₁` and `u₂` are two uniform structures on `γ`, then `𝒱(α, γ, 𝔖, u₁ ⊓ u₂) = 𝒱(α, γ, 𝔖, u₁) ⊓ 𝒱(α, γ, 𝔖, u₂)`. -/ protected lemma inf_eq {u₁ u₂ : uniform_space γ} : 𝒱(α, γ, 𝔖, u₁ ⊓ u₂) = 𝒱(α, γ, 𝔖, u₁) ⊓ 𝒱(α, γ, 𝔖, u₂) := begin rw [inf_eq_infi, inf_eq_infi, uniform_on_fun.infi_eq], refine infi_congr (λ i, _), cases i; refl end /-- If `u` is a uniform structures on `β` and `f : γ → β`, then `𝒱(α, γ, 𝔖, comap f u) = comap (λ g, f ∘ g) 𝒱(α, γ, 𝔖, u₁)`. -/ protected lemma comap_eq {f : γ → β} : 𝒱(α, γ, 𝔖, ‹uniform_space β›.comap f) = 𝒱(α, β, 𝔖, _).comap ((∘) f) := begin -- We reduce this to `uniform_convergence.comap_eq` using the fact that `comap` distributes -- on `infi`. simp_rw [uniform_on_fun.uniform_space, uniform_space.comap_infi, uniform_fun.comap_eq, ← uniform_space.comap_comap], refl -- by definition, `∀ S ∈ 𝔖, (f ∘ —) ∘ S.restrict = S.restrict ∘ (f ∘ —)`. end /-- Post-composition by a uniformly continuous function is uniformly continuous for the uniform structures of `𝔖`-convergence. More precisely, if `f : γ → β` is uniformly continuous, then `(λ g, f ∘ g) : (α →ᵤ[𝔖] γ) → (α →ᵤ[𝔖] β)` is uniformly continuous. -/ protected lemma postcomp_uniform_continuous [uniform_space γ] {f : γ → β} (hf : uniform_continuous f): uniform_continuous (of_fun 𝔖 ∘ (∘) f ∘ to_fun 𝔖) := begin -- This is a direct consequence of `uniform_convergence.comap_eq` rw uniform_continuous_iff, calc 𝒱(α, γ, 𝔖, _) ≤ 𝒱(α, γ, 𝔖, ‹uniform_space β›.comap f) : uniform_on_fun.mono (uniform_continuous_iff.mp hf) (subset_rfl) ... = 𝒱(α, β, 𝔖, _).comap ((∘) f) : uniform_on_fun.comap_eq end /-- Post-composition by a uniform inducing is a uniform inducing for the uniform structures of `𝔖`-convergence. More precisely, if `f : γ → β` is a uniform inducing, then `(λ g, f ∘ g) : (α →ᵤ[𝔖] γ) → (α →ᵤ[𝔖] β)` is a uniform inducing. -/ protected lemma postcomp_uniform_inducing [uniform_space γ] {f : γ → β} (hf : uniform_inducing f): uniform_inducing (of_fun 𝔖 ∘ (∘) f ∘ to_fun 𝔖) := -- This is a direct consequence of `uniform_convergence.comap_eq` begin split, replace hf : (𝓤 β).comap (prod.map f f) = _ := hf.comap_uniformity, change comap (prod.map (of_fun 𝔖 ∘ (∘) f ∘ to_fun 𝔖) (of_fun 𝔖 ∘ (∘) f ∘ to_fun 𝔖)) _ = _, rw [← uniformity_comap] at ⊢ hf, congr, rw [← uniform_space_eq hf, uniform_on_fun.comap_eq], refl end /-- Turn a uniform isomorphism `γ ≃ᵤ β` into a uniform isomorphism `(α →ᵤ[𝔖] γ) ≃ᵤ (α →ᵤ[𝔖] β)` by post-composing. -/ protected def congr_right [uniform_space γ] (e : γ ≃ᵤ β) : (α →ᵤ[𝔖] γ) ≃ᵤ (α →ᵤ[𝔖] β) := { uniform_continuous_to_fun := uniform_on_fun.postcomp_uniform_continuous e.uniform_continuous, uniform_continuous_inv_fun := uniform_on_fun.postcomp_uniform_continuous e.symm.uniform_continuous, .. equiv.Pi_congr_right (λ a, e.to_equiv) } /-- Let `f : γ → α`, `𝔖 : set (set α)`, `𝔗 : set (set γ)`, and assume that `∀ T ∈ 𝔗, f '' T ∈ 𝔖`. Then, the function `(λ g, g ∘ f) : (α →ᵤ[𝔖] β) → (γ →ᵤ[𝔗] β)` is uniformly continuous. Note that one can easily see that assuming `∀ T ∈ 𝔗, ∃ S ∈ 𝔖, f '' T ⊆ S` would work too, but we will get this for free when we prove that `𝒱(α, β, 𝔖, uβ) = 𝒱(α, β, 𝔖', uβ)` where `𝔖'` is the ***noncovering*** bornology generated by `𝔖`. -/ protected lemma precomp_uniform_continuous {𝔗 : set (set γ)} {f : γ → α} (hf : 𝔗 ⊆ (image f) ⁻¹' 𝔖) : uniform_continuous (λ g : α →ᵤ[𝔖] β, of_fun 𝔗 (g ∘ f)) := begin -- Since `comap` distributes on `infi`, it suffices to prove that -- `⨅ s ∈ 𝔖, comap s.restrict 𝒰(↥s, β, uβ) ≤ ⨅ t ∈ 𝔗, comap (t.restrict ∘ (— ∘ f)) 𝒰(↥t, β, uβ)`. simp_rw [uniform_continuous_iff, uniform_on_fun.uniform_space, uniform_space.comap_infi, ← uniform_space.comap_comap], -- For any `t ∈ 𝔗`, note `s := f '' t ∈ 𝔖`. -- We will show that `comap s.restrict 𝒰(↥s, β, uβ) ≤ comap (t.restrict ∘ (— ∘ f)) 𝒰(↥t, β, uβ)`. refine le_infi₂ (λ t ht, infi_le_of_le (f '' t) $ infi_le_of_le (hf ht) _), -- Let `f'` be the map from `t` to `f '' t` induced by `f`. let f' : t → f '' t := (maps_to_image f t).restrict f t (f '' t), -- By definition `t.restrict ∘ (— ∘ f) = (— ∘ f') ∘ (f '' t).restrict`. have : t.restrict ∘ (λ g : α →ᵤ[𝔖] β, of_fun 𝔗 (g ∘ f)) = (λ g : (f '' t) → β, g ∘ f') ∘ (f '' t).restrict := rfl, -- Thus, we have to show `comap (f '' t).restrict 𝒰(↥(f '' t), β, uβ) ≤` -- `comap (f '' t).restrict (comap (— ∘ f') 𝒰(↥t, β, uβ))`. rw [this, @uniform_space.comap_comap (α →ᵤ[𝔖] β) ((f '' t) →ᵤ β)], -- But this is exactly monotonicity of `comap` applied to -- `uniform_convergence.precomp_continuous`. refine uniform_space.comap_mono _, rw ← uniform_continuous_iff, exact uniform_fun.precomp_uniform_continuous end /-- Turn a bijection `e : γ ≃ α` such that we have both `∀ T ∈ 𝔗, e '' T ∈ 𝔖` and `∀ S ∈ 𝔖, e ⁻¹' S ∈ 𝔗` into a uniform isomorphism `(γ →ᵤ[𝔗] β) ≃ᵤ (α →ᵤ[𝔖] β)` by pre-composing. -/ protected def congr_left {𝔗 : set (set γ)} (e : γ ≃ α) (he : 𝔗 ⊆ (image e) ⁻¹' 𝔖) (he' : 𝔖 ⊆ (preimage e) ⁻¹' 𝔗) : (γ →ᵤ[𝔗] β) ≃ᵤ (α →ᵤ[𝔖] β) := { uniform_continuous_to_fun := uniform_on_fun.precomp_uniform_continuous begin intros s hs, change e.symm '' s ∈ 𝔗, rw ← preimage_equiv_eq_image_symm, exact he' hs end, uniform_continuous_inv_fun := uniform_on_fun.precomp_uniform_continuous he, .. equiv.arrow_congr e (equiv.refl _) } /-- If `𝔖` covers `α`, then the topology of `𝔖`-convergence is T₂. -/ lemma t2_space_of_covering [t2_space β] (h : ⋃₀ 𝔖 = univ) : t2_space (α →ᵤ[𝔖] β) := { t2 := begin intros f g hfg, obtain ⟨x, hx⟩ := not_forall.mp (mt funext hfg), obtain ⟨s, hs, hxs⟩ : ∃ s ∈ 𝔖, x ∈ s := mem_sUnion.mp (h.symm ▸ true.intro), exact separated_by_continuous (uniform_continuous_eval_of_mem β 𝔖 hxs hs).continuous hx end } /-- If `𝔖` covers `α`, the natural map `uniform_on_fun.to_fun` from `α →ᵤ[𝔖] β` to `α → β` is uniformly continuous. In other words, if `𝔖` covers `α`, then the uniform structure of `𝔖`-convergence is finer than that of pointwise convergence. -/ protected lemma uniform_continuous_to_fun (h : ⋃₀ 𝔖 = univ) : uniform_continuous (to_fun 𝔖 : (α →ᵤ[𝔖] β) → α → β) := begin rw uniform_continuous_pi, intros x, obtain ⟨s : set α, hs : s ∈ 𝔖, hxs : x ∈ s⟩ := sUnion_eq_univ_iff.mp h x, exact uniform_continuous_eval_of_mem β 𝔖 hxs hs end /-- Convergence in the topology of `𝔖`-convergence means uniform convergence on `S` (in the sense of `tendsto_uniformly_on`) for all `S ∈ 𝔖`. -/ protected lemma tendsto_iff_tendsto_uniformly_on {F : ι → α →ᵤ[𝔖] β} {f : α →ᵤ[𝔖] β} : tendsto F p (𝓝 f) ↔ ∀ s ∈ 𝔖, tendsto_uniformly_on F f p s := begin rw [uniform_on_fun.topological_space_eq, nhds_infi, tendsto_infi], refine forall_congr (λ s, _), rw [nhds_infi, tendsto_infi], refine forall_congr (λ hs, _), rw [nhds_induced, tendsto_comap_iff, tendsto_uniformly_on_iff_tendsto_uniformly_comp_coe, uniform_fun.tendsto_iff_tendsto_uniformly], refl end /-- The natural bijection between `α → β × γ` and `(α → β) × (α → γ)`, upgraded to a uniform isomorphism between `α →ᵤ[𝔖] β × γ` and `(α →ᵤ[𝔖] β) × (α →ᵤ[𝔖] γ)`. -/ protected def uniform_equiv_prod_arrow [uniform_space γ] : (α →ᵤ[𝔖] β × γ) ≃ᵤ ((α →ᵤ[𝔖] β) × (α →ᵤ[𝔖] γ)) := -- Denote `φ` this bijection. We want to show that -- `comap φ (𝒱(α, β, 𝔖, uβ) × 𝒱(α, γ, 𝔖, uγ)) = 𝒱(α, β × γ, 𝔖, uβ × uγ)`. -- But `uβ × uγ` is defined as `comap fst uβ ⊓ comap snd uγ`, so we just have to apply -- `uniform_convergence_on.inf_eq` and `uniform_convergence_on.comap_eq`, which leaves us to check -- that some square commutes. -- We could also deduce this from `uniform_convergence.uniform_equiv_prod_arrow`, but it turns out -- to be more annoying. ((uniform_on_fun.of_fun 𝔖).symm.trans $ (equiv.arrow_prod_equiv_prod_arrow _ _ _).trans $ (uniform_on_fun.of_fun 𝔖).prod_congr (uniform_on_fun.of_fun 𝔖)) .to_uniform_equiv_of_uniform_inducing begin split, rw [uniformity_prod, comap_inf, comap_comap, comap_comap, uniform_on_fun.inf_eq, inf_uniformity, uniform_on_fun.comap_eq, uniform_on_fun.comap_eq, uniformity_comap, uniformity_comap], refl -- the relevant diagram commutes by definition end variables (𝔖) (δ : ι → Type*) [Π i, uniform_space (δ i)] /-- The natural bijection between `α → Π i, δ i` and `Π i, α → δ i`, upgraded to a uniform isomorphism between `α →ᵤ[𝔖] (Π i, δ i)` and `Π i, α →ᵤ[𝔖] δ i`. -/ protected def uniform_equiv_Pi_comm : (α →ᵤ[𝔖] Π i, δ i) ≃ᵤ (Π i, α →ᵤ[𝔖] δ i) := -- Denote `φ` this bijection. We want to show that -- `comap φ (Π i, 𝒱(α, δ i, 𝔖, uδ i)) = 𝒱(α, (Π i, δ i), 𝔖, (Π i, uδ i))`. -- But `Π i, uδ i` is defined as `⨅ i, comap (eval i) (uδ i)`, so we just have to apply -- `uniform_convergence_on.infi_eq` and `uniform_convergence_on.comap_eq`, which leaves us to check -- that some square commutes. -- We could also deduce this from `uniform_convergence.uniform_equiv_Pi_comm`, but it turns out -- to be more annoying. (equiv.Pi_comm _).to_uniform_equiv_of_uniform_inducing begin split, change comap (prod.map function.swap function.swap) _ = _, rw ← uniformity_comap, congr, rw [Pi.uniform_space, uniform_space.of_core_eq_to_core, Pi.uniform_space, uniform_space.of_core_eq_to_core, uniform_space.comap_infi, uniform_on_fun.infi_eq], refine infi_congr (λ i, _), rw [← uniform_space.comap_comap, uniform_on_fun.comap_eq] -- Like in the previous lemma, the diagram actually commutes by definition end end uniform_on_fun
6299180969517c1fb31daccf59c4c6cad7812cd9
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/category/Mon/colimits.lean
3fd2f27ef898099d6660bb512ba11fea5fb37840
[]
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
8,307
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.category.Mon.basic import Mathlib.category_theory.limits.limits import Mathlib.category_theory.limits.concrete_category import Mathlib.PostPort universes v l u_1 namespace Mathlib /-! # The category of monoids has all colimits. We do this construction knowing nothing about monoids. In particular, I want to claim that this file could be produced by a python script that just looks at the output of `#print monoid`: -- structure monoid : Type u → Type u -- fields: -- monoid.mul : Π {α : Type u} [c : monoid α], α → α → α -- monoid.mul_assoc : ∀ {α : Type u} [c : monoid α] (a b c_1 : α), a * b * c_1 = a * (b * c_1) -- monoid.one : Π (α : Type u) [c : monoid α], α -- monoid.one_mul : ∀ {α : Type u} [c : monoid α] (a : α), 1 * a = a -- monoid.mul_one : ∀ {α : Type u} [c : monoid α] (a : α), a * 1 = a and if we'd fed it the output of `#print comm_ring`, this file would instead build colimits of commutative rings. A slightly bolder claim is that we could do this with tactics, as well. -/ namespace Mon.colimits /-! We build the colimit of a diagram in `Mon` by constructing the free monoid on the disjoint union of all the monoids in the diagram, then taking the quotient by the monoid laws within each monoid, and the identifications given by the morphisms in the diagram. -/ /-- An inductive type representing all monoid expressions (without relations) on a collection of types indexed by the objects of `J`. -/ -- There's always `of` inductive prequotient {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) where | of : (j : J) → ↥(category_theory.functor.obj F j) → prequotient F | one : prequotient F | mul : prequotient F → prequotient F → prequotient F -- Then one generator for each operation protected instance prequotient.inhabited {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) : Inhabited (prequotient F) := { default := prequotient.one } /-- The relation on `prequotient` saying when two expressions are equal because of the monoid laws, or because one element is mapped to another by a morphism in the diagram. -/ -- Make it an equivalence relation: inductive relation {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) : prequotient F → prequotient F → Prop where | refl : ∀ (x : prequotient F), relation F x x | symm : ∀ (x y : prequotient F), relation F x y → relation F y x | trans : ∀ (x y z : prequotient F), relation F x y → relation F y z → relation F x z | map : ∀ (j j' : J) (f : j ⟶ j') (x : ↥(category_theory.functor.obj F j)), relation F (prequotient.of j' (coe_fn (category_theory.functor.map F f) x)) (prequotient.of j x) | mul : ∀ (j : J) (x y : ↥(category_theory.functor.obj F j)), relation F (prequotient.of j (x * y)) (prequotient.mul (prequotient.of j x) (prequotient.of j y)) | one : ∀ (j : J), relation F (prequotient.of j 1) prequotient.one | mul_1 : ∀ (x x' y : prequotient F), relation F x x' → relation F (prequotient.mul x y) (prequotient.mul x' y) | mul_2 : ∀ (x y y' : prequotient F), relation F y y' → relation F (prequotient.mul x y) (prequotient.mul x y') | mul_assoc : ∀ (x y z : prequotient F), relation F (prequotient.mul (prequotient.mul x y) z) (prequotient.mul x (prequotient.mul y z)) | one_mul : ∀ (x : prequotient F), relation F (prequotient.mul prequotient.one x) x | mul_one : ∀ (x : prequotient F), relation F (prequotient.mul x prequotient.one) x -- There's always a `map` relation -- Then one relation per operation, describing the interaction with `of` -- Then one relation per argument of each operation -- And one relation per axiom /-- The setoid corresponding to monoid expressions modulo monoid relations and identifications. -/ instance colimit_setoid {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) : setoid (prequotient F) := setoid.mk (relation F) sorry /-- The underlying type of the colimit of a diagram in `Mon`. -/ def colimit_type {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) := quotient (colimit_setoid F) protected instance monoid_colimit_type {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) : monoid (colimit_type F) := monoid.mk (Quot.lift (fun (x : prequotient F) => Quot.lift (fun (y : prequotient F) => Quot.mk setoid.r (prequotient.mul x y)) sorry) sorry) sorry (Quot.mk setoid.r prequotient.one) sorry sorry @[simp] theorem quot_one {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) : Quot.mk setoid.r prequotient.one = 1 := rfl @[simp] theorem quot_mul {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) (x : prequotient F) (y : prequotient F) : Quot.mk setoid.r (prequotient.mul x y) = Quot.mk setoid.r x * Quot.mk setoid.r y := rfl /-- The bundled monoid giving the colimit of a diagram. -/ def colimit {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) : Mon := category_theory.bundled.mk (colimit_type F) /-- The function from a given monoid in the diagram to the colimit monoid. -/ def cocone_fun {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) (j : J) (x : ↥(category_theory.functor.obj F j)) : colimit_type F := Quot.mk setoid.r (prequotient.of j x) /-- The monoid homomorphism from a given monoid in the diagram to the colimit monoid. -/ def cocone_morphism {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) (j : J) : category_theory.functor.obj F j ⟶ colimit F := monoid_hom.mk (cocone_fun F j) sorry sorry @[simp] theorem cocone_naturality {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) {j : J} {j' : J} (f : j ⟶ j') : category_theory.functor.map F f ≫ cocone_morphism F j' = cocone_morphism F j := monoid_hom.ext fun (x : ↥(category_theory.functor.obj F j)) => quot.sound (relation.map j j' f x) @[simp] theorem cocone_naturality_components {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) (j : J) (j' : J) (f : j ⟶ j') (x : ↥(category_theory.functor.obj F j)) : coe_fn (cocone_morphism F j') (coe_fn (category_theory.functor.map F f) x) = coe_fn (cocone_morphism F j) x := sorry /-- The cocone over the proposed colimit monoid. -/ def colimit_cocone {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) : category_theory.limits.cocone F := category_theory.limits.cocone.mk (colimit F) (category_theory.nat_trans.mk (cocone_morphism F)) /-- The function from the free monoid on the diagram to the cone point of any other cocone. -/ @[simp] def desc_fun_lift {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) (s : category_theory.limits.cocone F) : prequotient F → ↥(category_theory.limits.cocone.X s) := sorry /-- The function from the colimit monoid to the cone point of any other cocone. -/ def desc_fun {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) (s : category_theory.limits.cocone F) : colimit_type F → ↥(category_theory.limits.cocone.X s) := Quot.lift (desc_fun_lift F s) sorry /-- The monoid homomorphism from the colimit monoid to the cone point of any other cocone. -/ def desc_morphism {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) (s : category_theory.limits.cocone F) : colimit F ⟶ category_theory.limits.cocone.X s := monoid_hom.mk (desc_fun F s) sorry sorry /-- Evidence that the proposed colimit is the colimit. -/ def colimit_is_colimit {J : Type v} [category_theory.small_category J] (F : J ⥤ Mon) : category_theory.limits.is_colimit (colimit_cocone F) := category_theory.limits.is_colimit.mk fun (s : category_theory.limits.cocone F) => desc_morphism F s protected instance has_colimits_Mon : category_theory.limits.has_colimits Mon := category_theory.limits.has_colimits.mk fun (J : Type u_1) (𝒥 : category_theory.small_category J) => category_theory.limits.has_colimits_of_shape.mk fun (F : J ⥤ Mon) => category_theory.limits.has_colimit.mk (category_theory.limits.colimit_cocone.mk (colimit_cocone F) (colimit_is_colimit F))
e1f8bf347f228c30c516197d35d8d6095d1d8a79
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/nat/dist_auto.lean
4b6b3e8aaf794e9dfd17ef8aac431b7f8b30f701
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
3,911
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Jeremy Avigad Distance function on the natural numbers. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.nat.basic import Mathlib.PostPort namespace Mathlib namespace nat /- distance -/ /-- Distance (absolute value of difference) between natural numbers. -/ def dist (n : ℕ) (m : ℕ) : ℕ := n - m + (m - n) theorem dist.def (n : ℕ) (m : ℕ) : dist n m = n - m + (m - n) := rfl theorem dist_comm (n : ℕ) (m : ℕ) : dist n m = dist m n := sorry @[simp] theorem dist_self (n : ℕ) : dist n n = 0 := sorry theorem eq_of_dist_eq_zero {n : ℕ} {m : ℕ} (h : dist n m = 0) : n = m := sorry theorem dist_eq_zero {n : ℕ} {m : ℕ} (h : n = m) : dist n m = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (dist n m = 0)) h)) (eq.mpr (id (Eq._oldrec (Eq.refl (dist m m = 0)) (dist_self m))) (Eq.refl 0)) theorem dist_eq_sub_of_le {n : ℕ} {m : ℕ} (h : n ≤ m) : dist n m = m - n := eq.mpr (id (Eq._oldrec (Eq.refl (dist n m = m - n)) (dist.def n m))) (eq.mpr (id (Eq._oldrec (Eq.refl (n - m + (m - n) = m - n)) (sub_eq_zero_of_le h))) (eq.mpr (id (Eq._oldrec (Eq.refl (0 + (m - n) = m - n)) (zero_add (m - n)))) (Eq.refl (m - n)))) theorem dist_eq_sub_of_le_right {n : ℕ} {m : ℕ} (h : m ≤ n) : dist n m = n - m := eq.mpr (id (Eq._oldrec (Eq.refl (dist n m = n - m)) (dist_comm n m))) (dist_eq_sub_of_le h) theorem dist_tri_left (n : ℕ) (m : ℕ) : m ≤ dist n m + n := le_trans (nat.le_sub_add m n) (add_le_add_right (le_add_left (m - n) (n - m)) n) theorem dist_tri_right (n : ℕ) (m : ℕ) : m ≤ n + dist n m := eq.mpr (id (Eq._oldrec (Eq.refl (m ≤ n + dist n m)) (add_comm n (dist n m)))) (dist_tri_left n m) theorem dist_tri_left' (n : ℕ) (m : ℕ) : n ≤ dist n m + m := eq.mpr (id (Eq._oldrec (Eq.refl (n ≤ dist n m + m)) (dist_comm n m))) (dist_tri_left m n) theorem dist_tri_right' (n : ℕ) (m : ℕ) : n ≤ m + dist n m := eq.mpr (id (Eq._oldrec (Eq.refl (n ≤ m + dist n m)) (dist_comm n m))) (dist_tri_right m n) theorem dist_zero_right (n : ℕ) : dist n 0 = n := Eq.trans (dist_eq_sub_of_le_right (zero_le n)) (nat.sub_zero n) theorem dist_zero_left (n : ℕ) : dist 0 n = n := Eq.trans (dist_eq_sub_of_le (zero_le n)) (nat.sub_zero n) theorem dist_add_add_right (n : ℕ) (k : ℕ) (m : ℕ) : dist (n + k) (m + k) = dist n m := sorry theorem dist_add_add_left (k : ℕ) (n : ℕ) (m : ℕ) : dist (k + n) (k + m) = dist n m := eq.mpr (id (Eq._oldrec (Eq.refl (dist (k + n) (k + m) = dist n m)) (add_comm k n))) (eq.mpr (id (Eq._oldrec (Eq.refl (dist (n + k) (k + m) = dist n m)) (add_comm k m))) (dist_add_add_right n k m)) theorem dist_eq_intro {n : ℕ} {m : ℕ} {k : ℕ} {l : ℕ} (h : n + m = k + l) : dist n k = dist l m := sorry protected theorem sub_lt_sub_add_sub (n : ℕ) (m : ℕ) (k : ℕ) : n - k ≤ n - m + (m - k) := sorry theorem dist.triangle_inequality (n : ℕ) (m : ℕ) (k : ℕ) : dist n k ≤ dist n m + dist m k := sorry theorem dist_mul_right (n : ℕ) (k : ℕ) (m : ℕ) : dist (n * k) (m * k) = dist n m * k := sorry theorem dist_mul_left (k : ℕ) (n : ℕ) (m : ℕ) : dist (k * n) (k * m) = k * dist n m := sorry -- TODO(Jeremy): do when we have max and minx --theorem dist_eq_max_sub_min {i j : nat} : dist i j = (max i j) - min i j := --sorry /- or.elim (lt_or_ge i j) (assume : i < j, by rw [max_eq_right_of_lt this, min_eq_left_of_lt this, dist_eq_sub_of_lt this]) (assume : i ≥ j, by rw [max_eq_left this , min_eq_right this, dist_eq_sub_of_le_right this]) -/ theorem dist_succ_succ {i : ℕ} {j : ℕ} : dist (Nat.succ i) (Nat.succ j) = dist i j := sorry theorem dist_pos_of_ne {i : ℕ} {j : ℕ} : i ≠ j → 0 < dist i j := sorry end Mathlib
d82dfa265ae72b246bd4c6c503b51153a3aafe3e
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Data/RBMap.lean
6b235c260ec7ca0d86b09538d4c25b5c1c60ac53
[ "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
15,405
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ namespace Lean universe u v w w' inductive RBColor where | red | black inductive RBNode (α : Type u) (β : α → Type v) where | leaf : RBNode α β | node (color : RBColor) (lchild : RBNode α β) (key : α) (val : β key) (rchild : RBNode α β) : RBNode α β namespace RBNode variable {α : Type u} {β : α → Type v} {σ : Type w} open RBColor Nat def depth (f : Nat → Nat → Nat) : RBNode α β → Nat | leaf => 0 | node _ l _ _ r => succ (f (depth f l) (depth f r)) protected def min : RBNode α β → Option (Sigma (fun k => β k)) | leaf => none | node _ leaf k v _ => some ⟨k, v⟩ | node _ l _ _ _ => RBNode.min l protected def max : RBNode α β → Option (Sigma (fun k => β k)) | leaf => none | node _ _ k v leaf => some ⟨k, v⟩ | node _ _ _ _ r => RBNode.max r @[specialize] def fold (f : σ → (k : α) → β k → σ) : (init : σ) → RBNode α β → σ | b, leaf => b | b, node _ l k v r => fold f (f (fold f b l) k v) r @[specialize] def forM [Monad m] (f : (k : α) → β k → m Unit) : RBNode α β → m Unit | leaf => pure () | node _ l k v r => do forM f l; f k v; forM f r @[specialize] def foldM [Monad m] (f : σ → (k : α) → β k → m σ) : (init : σ) → RBNode α β → m σ | b, leaf => pure b | b, node _ l k v r => do let b ← foldM f b l let b ← f b k v foldM f b r @[inline] protected def forIn [Monad m] (as : RBNode α β) (init : σ) (f : (k : α) → β k → σ → m (ForInStep σ)) : m σ := do let rec @[specialize] visit : RBNode α β → σ → m (ForInStep σ) | leaf, b => return ForInStep.yield b | node _ l k v r, b => do match (← visit l b) with | r@(ForInStep.done _) => return r | ForInStep.yield b => match (← f k v b) with | r@(ForInStep.done _) => return r | ForInStep.yield b => visit r b match (← visit as init) with | ForInStep.done b => pure b | ForInStep.yield b => pure b @[specialize] def revFold (f : σ → (k : α) → β k → σ) : (init : σ) → RBNode α β → σ | b, leaf => b | b, node _ l k v r => revFold f (f (revFold f b r) k v) l @[specialize] def all (p : (k : α) → β k → Bool) : RBNode α β → Bool | leaf => true | node _ l k v r => p k v && all p l && all p r @[specialize] def any (p : (k : α) → β k → Bool) : RBNode α β → Bool | leaf => false | node _ l k v r => p k v || any p l || any p r def singleton (k : α) (v : β k) : RBNode α β := node red leaf k v leaf -- the first half of Okasaki's `balance`, concerning red-red sequences in the left child @[inline] def balance1 : RBNode α β → (a : α) → β a → RBNode α β → RBNode α β | node red (node red a kx vx b) ky vy c, kz, vz, d | node red a kx vx (node red b ky vy c), kz, vz, d => node red (node black a kx vx b) ky vy (node black c kz vz d) | a, kx, vx, b => node black a kx vx b -- the second half, concerning red-red sequences in the right child @[inline] def balance2 : RBNode α β → (a : α) → β a → RBNode α β → RBNode α β | a, kx, vx, node red (node red b ky vy c) kz vz d | a, kx, vx, node red b ky vy (node red c kz vz d) => node red (node black a kx vx b) ky vy (node black c kz vz d) | a, kx, vx, b => node black a kx vx b def isRed : RBNode α β → Bool | node red .. => true | _ => false def isBlack : RBNode α β → Bool | node black .. => true | _ => false section Insert variable (cmp : α → α → Ordering) @[specialize] def ins : RBNode α β → (k : α) → β k → RBNode α β | leaf, kx, vx => node red leaf kx vx leaf | node red a ky vy b, kx, vx => match cmp kx ky with | Ordering.lt => node red (ins a kx vx) ky vy b | Ordering.gt => node red a ky vy (ins b kx vx) | Ordering.eq => node red a kx vx b | node black a ky vy b, kx, vx => match cmp kx ky with | Ordering.lt => balance1 (ins a kx vx) ky vy b | Ordering.gt => balance2 a ky vy (ins b kx vx) | Ordering.eq => node black a kx vx b def setBlack : RBNode α β → RBNode α β | node _ l k v r => node black l k v r | e => e @[specialize] def insert (t : RBNode α β) (k : α) (v : β k) : RBNode α β := if isRed t then setBlack (ins cmp t k v) else ins cmp t k v end Insert def setRed : RBNode α β → RBNode α β | node _ a k v b => node red a k v b | e => e def balLeft : RBNode α β → (k : α) → β k → RBNode α β → RBNode α β | node red a kx vx b, k, v, r => node red (node black a kx vx b) k v r | l, k, v, node black a ky vy b => balance2 l k v (node red a ky vy b) | l, k, v, node red (node black a ky vy b) kz vz c => node red (node black l k v a) ky vy (balance2 b kz vz (setRed c)) | l, k, v, r => node red l k v r -- unreachable def balRight (l : RBNode α β) (k : α) (v : β k) (r : RBNode α β) : RBNode α β := match r with | (node red b ky vy c) => node red l k v (node black b ky vy c) | _ => match l with | node black a kx vx b => balance1 (node red a kx vx b) k v r | node red a kx vx (node black b ky vy c) => node red (balance1 (setRed a) kx vx b) ky vy (node black c k v r) | _ => node red l k v r -- unreachable /-- The number of nodes in the tree. -/ @[local simp] def size : RBNode α β → Nat | leaf => 0 | node _ x _ _ y => x.size + y.size + 1 def appendTrees : RBNode α β → RBNode α β → RBNode α β | leaf, x => x | x, leaf => x | node red a kx vx b, node red c ky vy d => match appendTrees b c with | node red b' kz vz c' => node red (node red a kx vx b') kz vz (node red c' ky vy d) | bc => node red a kx vx (node red bc ky vy d) | node black a kx vx b, node black c ky vy d => match appendTrees b c with | node red b' kz vz c' => node red (node black a kx vx b') kz vz (node black c' ky vy d) | bc => balLeft a kx vx (node black bc ky vy d) | a, node red b kx vx c => node red (appendTrees a b) kx vx c | node red a kx vx b, c => node red a kx vx (appendTrees b c) termination_by _ x y => x.size + y.size section Erase variable (cmp : α → α → Ordering) @[specialize] def del (x : α) : RBNode α β → RBNode α β | leaf => leaf | node _ a y v b => match cmp x y with | Ordering.lt => if a.isBlack then balLeft (del x a) y v b else node red (del x a) y v b | Ordering.gt => if b.isBlack then balRight a y v (del x b) else node red a y v (del x b) | Ordering.eq => appendTrees a b @[specialize] def erase (x : α) (t : RBNode α β) : RBNode α β := let t := del cmp x t; t.setBlack end Erase section Membership variable (cmp : α → α → Ordering) @[specialize] def findCore : RBNode α β → (k : α) → Option (Sigma (fun k => β k)) | leaf, _ => none | node _ a ky vy b, x => match cmp x ky with | Ordering.lt => findCore a x | Ordering.gt => findCore b x | Ordering.eq => some ⟨ky, vy⟩ @[specialize] def find {β : Type v} : RBNode α (fun _ => β) → α → Option β | leaf, _ => none | node _ a ky vy b, x => match cmp x ky with | Ordering.lt => find a x | Ordering.gt => find b x | Ordering.eq => some vy @[specialize] def lowerBound : RBNode α β → α → Option (Sigma β) → Option (Sigma β) | leaf, _, lb => lb | node _ a ky vy b, x, lb => match cmp x ky with | Ordering.lt => lowerBound a x lb | Ordering.gt => lowerBound b x (some ⟨ky, vy⟩) | Ordering.eq => some ⟨ky, vy⟩ end Membership inductive WellFormed (cmp : α → α → Ordering) : RBNode α β → Prop where | leafWff : WellFormed cmp leaf | insertWff {n n' : RBNode α β} {k : α} {v : β k} : WellFormed cmp n → n' = insert cmp n k v → WellFormed cmp n' | eraseWff {n n' : RBNode α β} {k : α} : WellFormed cmp n → n' = erase cmp k n → WellFormed cmp n' section Map @[specialize] def mapM {α : Type v} {β γ : α → Type v} {M : Type v → Type v} [Applicative M] (f : (a : α) → β a → M (γ a)) : RBNode α β → M (RBNode α γ) | leaf => pure leaf | node color lchild key val rchild => pure (node color · key · ·) <*> lchild.mapM f <*> f _ val <*> rchild.mapM f @[specialize] def map {α : Type u} {β γ : α → Type v} (f : (a : α) → β a → γ a) : RBNode α β → RBNode α γ | leaf => leaf | node color lchild key val rchild => node color (lchild.map f) key (f key val) (rchild.map f) end Map def toArray (n : RBNode α β) : Array (Sigma β) := n.fold (init := ∅) fun acc k v => acc.push ⟨k,v⟩ instance : EmptyCollection (RBNode α β) := ⟨leaf⟩ end RBNode open Lean.RBNode /- TODO(Leo): define dRBMap -/ def RBMap (α : Type u) (β : Type v) (cmp : α → α → Ordering) : Type (max u v) := {t : RBNode α (fun _ => β) // t.WellFormed cmp } @[inline] def mkRBMap (α : Type u) (β : Type v) (cmp : α → α → Ordering) : RBMap α β cmp := ⟨leaf, WellFormed.leafWff⟩ @[inline] def RBMap.empty {α : Type u} {β : Type v} {cmp : α → α → Ordering} : RBMap α β cmp := mkRBMap .. instance (α : Type u) (β : Type v) (cmp : α → α → Ordering) : EmptyCollection (RBMap α β cmp) := ⟨RBMap.empty⟩ instance (α : Type u) (β : Type v) (cmp : α → α → Ordering) : Inhabited (RBMap α β cmp) := ⟨∅⟩ namespace RBMap variable {α : Type u} {β : Type v} {σ : Type w} {cmp : α → α → Ordering} def depth (f : Nat → Nat → Nat) (t : RBMap α β cmp) : Nat := t.val.depth f @[inline] def fold (f : σ → α → β → σ) : (init : σ) → RBMap α β cmp → σ | b, ⟨t, _⟩ => t.fold f b @[inline] def revFold (f : σ → α → β → σ) : (init : σ) → RBMap α β cmp → σ | b, ⟨t, _⟩ => t.revFold f b @[inline] def foldM [Monad m] (f : σ → α → β → m σ) : (init : σ) → RBMap α β cmp → m σ | b, ⟨t, _⟩ => t.foldM f b @[inline] def forM [Monad m] (f : α → β → m PUnit) (t : RBMap α β cmp) : m PUnit := t.foldM (fun _ k v => f k v) ⟨⟩ @[inline] protected def forIn [Monad m] (t : RBMap α β cmp) (init : σ) (f : (α × β) → σ → m (ForInStep σ)) : m σ := t.val.forIn init (fun a b acc => f (a, b) acc) instance : ForIn m (RBMap α β cmp) (α × β) where forIn := RBMap.forIn @[inline] def isEmpty : RBMap α β cmp → Bool | ⟨leaf, _⟩ => true | _ => false @[specialize] def toList : RBMap α β cmp → List (α × β) | ⟨t, _⟩ => t.revFold (fun ps k v => (k, v)::ps) [] /-- Returns the kv pair `(a,b)` such that `a ≤ k` for all keys in the RBMap. -/ @[inline] protected def min : RBMap α β cmp → Option (α × β) | ⟨t, _⟩ => match t.min with | some ⟨k, v⟩ => some (k, v) | none => none /-- Returns the kv pair `(a,b)` such that `a ≥ k` for all keys in the RBMap. -/ @[inline] protected def max : RBMap α β cmp → Option (α × β) | ⟨t, _⟩ => match t.max with | some ⟨k, v⟩ => some (k, v) | none => none instance [Repr α] [Repr β] : Repr (RBMap α β cmp) where reprPrec m prec := Repr.addAppParen ("Lean.rbmapOf " ++ repr m.toList) prec @[inline] def insert : RBMap α β cmp → α → β → RBMap α β cmp | ⟨t, w⟩, k, v => ⟨t.insert cmp k v, WellFormed.insertWff w rfl⟩ @[inline] def erase : RBMap α β cmp → α → RBMap α β cmp | ⟨t, w⟩, k => ⟨t.erase cmp k, WellFormed.eraseWff w rfl⟩ @[specialize] def ofList : List (α × β) → RBMap α β cmp | [] => mkRBMap .. | ⟨k,v⟩::xs => (ofList xs).insert k v @[inline] def findCore? : RBMap α β cmp → α → Option (Sigma (fun (_ : α) => β)) | ⟨t, _⟩, x => t.findCore cmp x @[inline] def find? : RBMap α β cmp → α → Option β | ⟨t, _⟩, x => t.find cmp x @[inline] def findD (t : RBMap α β cmp) (k : α) (v₀ : β) : β := (t.find? k).getD v₀ /-- (lowerBound k) retrieves the kv pair of the largest key smaller than or equal to `k`, if it exists. -/ @[inline] def lowerBound : RBMap α β cmp → α → Option (Sigma (fun (_ : α) => β)) | ⟨t, _⟩, x => t.lowerBound cmp x none /-- Returns true if the given key `a` is in the RBMap. -/ @[inline] def contains (t : RBMap α β cmp) (a : α) : Bool := (t.find? a).isSome @[inline] def fromList (l : List (α × β)) (cmp : α → α → Ordering) : RBMap α β cmp := l.foldl (fun r p => r.insert p.1 p.2) (mkRBMap α β cmp) @[inline] def fromArray (l : Array (α × β)) (cmp : α → α → Ordering) : RBMap α β cmp := l.foldl (fun r p => r.insert p.1 p.2) (mkRBMap α β cmp) /-- Returns true if the given predicate is true for all items in the RBMap. -/ @[inline] def all : RBMap α β cmp → (α → β → Bool) → Bool | ⟨t, _⟩, p => t.all p /-- Returns true if the given predicate is true for any item in the RBMap. -/ @[inline] def any : RBMap α β cmp → (α → β → Bool) → Bool | ⟨t, _⟩, p => t.any p /-- The number of items in the RBMap. -/ def size (m : RBMap α β cmp) : Nat := m.fold (fun sz _ _ => sz+1) 0 def maxDepth (t : RBMap α β cmp) : Nat := t.val.depth Nat.max @[inline] def min! [Inhabited α] [Inhabited β] (t : RBMap α β cmp) : α × β := match t.min with | some p => p | none => panic! "map is empty" @[inline] def max! [Inhabited α] [Inhabited β] (t : RBMap α β cmp) : α × β := match t.max with | some p => p | none => panic! "map is empty" /-- Attempts to find the value with key `k : α` in `t` and panics if there is no such key. -/ @[inline] def find! [Inhabited β] (t : RBMap α β cmp) (k : α) : β := match t.find? k with | some b => b | none => panic! "key is not in the map" /-- Merges the maps `t₁` and `t₂`, if a key `a : α` exists in both, then use `mergeFn a b₁ b₂` to produce the new merged value. -/ def mergeBy (mergeFn : α → β → β → β) (t₁ t₂ : RBMap α β cmp) : RBMap α β cmp := t₂.fold (init := t₁) fun t₁ a b₂ => t₁.insert a <| match t₁.find? a with | some b₁ => mergeFn a b₁ b₂ | none => b₂ /-- Intersects the maps `t₁` and `t₂` using `mergeFn a b₁ b₂` to produce the new value. -/ def intersectBy {γ : Type v₁} {δ : Type v₂} (mergeFn : α → β → γ → δ) (t₁ : RBMap α β cmp) (t₂ : RBMap α γ cmp) : RBMap α δ cmp := t₁.fold (init := ∅) fun acc a b₁ => match t₂.find? a with | some b₂ => acc.insert a <| mergeFn a b₁ b₂ | none => acc end RBMap def rbmapOf {α : Type u} {β : Type v} (l : List (α × β)) (cmp : α → α → Ordering) : RBMap α β cmp := RBMap.fromList l cmp
9cb99582ed1867616a82af56d108df6f1385b36e
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/check.lean
451c69de5bc9ebb01d00ee258e7b209cb3bf2621
[ "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
97
lean
import logic set_option pp.metavar_args true check and.intro check or.elim check eq check eq.rec
5d4c0c282571a007ca8c37cbdc2d8552cb9a5a53
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/category/Mon/filtered_colimits.lean
93d263d8b09419f80ddbff3ec8c9f443a40ac943
[ "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
14,515
lean
/- Copyright (c) 2021 Justus Springer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Justus Springer -/ import algebra.category.Mon.basic import category_theory.limits.preserves.filtered import category_theory.concrete_category.elementwise import category_theory.limits.types /-! # The forgetful functor from (commutative) (additive) monoids preserves filtered colimits. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Forgetful functors from algebraic categories usually don't preserve colimits. However, they tend to preserve _filtered_ colimits. In this file, we start with a small filtered category `J` and a functor `F : J ⥤ Mon`. We then construct a monoid structure on the colimit of `F ⋙ forget Mon` (in `Type`), thereby showing that the forgetful functor `forget Mon` preserves filtered colimits. Similarly for `AddMon`, `CommMon` and `AddCommMon`. -/ universes v u noncomputable theory open_locale classical open category_theory open category_theory.limits open category_theory.is_filtered (renaming max → max') -- avoid name collision with `_root_.max`. namespace Mon.filtered_colimits section -- We use parameters here, mainly so we can have the abbreviations `M` and `M.mk` below, without -- passing around `F` all the time. parameters {J : Type v} [small_category J] (F : J ⥤ Mon.{max v u}) /-- The colimit of `F ⋙ forget Mon` in the category of types. In the following, we will construct a monoid structure on `M`. -/ @[to_additive "The colimit of `F ⋙ forget AddMon` in the category of types. In the following, we will construct an additive monoid structure on `M`."] abbreviation M : Type (max v u) := types.quot (F ⋙ forget Mon) /-- The canonical projection into the colimit, as a quotient type. -/ @[to_additive "The canonical projection into the colimit, as a quotient type."] abbreviation M.mk : (Σ j, F.obj j) → M := quot.mk (types.quot.rel (F ⋙ forget Mon)) @[to_additive] lemma M.mk_eq (x y : Σ j, F.obj j) (h : ∃ (k : J) (f : x.1 ⟶ k) (g : y.1 ⟶ k), F.map f x.2 = F.map g y.2) : M.mk x = M.mk y := quot.eqv_gen_sound (types.filtered_colimit.eqv_gen_quot_rel_of_rel (F ⋙ forget Mon) x y h) variables [is_filtered J] /-- As `J` is nonempty, we can pick an arbitrary object `j₀ : J`. We use this object to define the "one" in the colimit as the equivalence class of `⟨j₀, 1 : F.obj j₀⟩`. -/ @[to_additive "As `J` is nonempty, we can pick an arbitrary object `j₀ : J`. We use this object to define the \"zero\" in the colimit as the equivalence class of `⟨j₀, 0 : F.obj j₀⟩`."] instance colimit_has_one : has_one M := { one := M.mk ⟨is_filtered.nonempty.some, 1⟩ } /-- The definition of the "one" in the colimit is independent of the chosen object of `J`. In particular, this lemma allows us to "unfold" the definition of `colimit_one` at a custom chosen object `j`. -/ @[to_additive "The definition of the \"zero\" in the colimit is independent of the chosen object of `J`. In particular, this lemma allows us to \"unfold\" the definition of `colimit_zero` at a custom chosen object `j`."] lemma colimit_one_eq (j : J) : (1 : M) = M.mk ⟨j, 1⟩ := begin apply M.mk_eq, refine ⟨max' _ j, left_to_max _ j, right_to_max _ j, _⟩, simp, end /-- The "unlifted" version of multiplication in the colimit. To multiply two dependent pairs `⟨j₁, x⟩` and `⟨j₂, y⟩`, we pass to a common successor of `j₁` and `j₂` (given by `is_filtered.max`) and multiply them there. -/ @[to_additive "The \"unlifted\" version of addition in the colimit. To add two dependent pairs `⟨j₁, x⟩` and `⟨j₂, y⟩`, we pass to a common successor of `j₁` and `j₂` (given by `is_filtered.max`) and add them there."] def colimit_mul_aux (x y : Σ j, F.obj j) : M := M.mk ⟨max' x.1 y.1, F.map (left_to_max x.1 y.1) x.2 * F.map (right_to_max x.1 y.1) y.2⟩ /-- Multiplication in the colimit is well-defined in the left argument. -/ @[to_additive "Addition in the colimit is well-defined in the left argument."] lemma colimit_mul_aux_eq_of_rel_left {x x' y : Σ j, F.obj j} (hxx' : types.filtered_colimit.rel (F ⋙ forget Mon) x x') : colimit_mul_aux x y = colimit_mul_aux x' y := begin cases x with j₁ x, cases y with j₂ y, cases x' with j₃ x', obtain ⟨l, f, g, hfg⟩ := hxx', simp at hfg, obtain ⟨s, α, β, γ, h₁, h₂, h₃⟩ := tulip (left_to_max j₁ j₂) (right_to_max j₁ j₂) (right_to_max j₃ j₂) (left_to_max j₃ j₂) f g, apply M.mk_eq, use [s, α, γ], dsimp, simp_rw [monoid_hom.map_mul, ← comp_apply, ← F.map_comp, h₁, h₂, h₃, F.map_comp, comp_apply, hfg] end /-- Multiplication in the colimit is well-defined in the right argument. -/ @[to_additive "Addition in the colimit is well-defined in the right argument."] lemma colimit_mul_aux_eq_of_rel_right {x y y' : Σ j, F.obj j} (hyy' : types.filtered_colimit.rel (F ⋙ forget Mon) y y') : colimit_mul_aux x y = colimit_mul_aux x y' := begin cases y with j₁ y, cases x with j₂ x, cases y' with j₃ y', obtain ⟨l, f, g, hfg⟩ := hyy', simp at hfg, obtain ⟨s, α, β, γ, h₁, h₂, h₃⟩ := tulip (right_to_max j₂ j₁) (left_to_max j₂ j₁) (left_to_max j₂ j₃) (right_to_max j₂ j₃) f g, apply M.mk_eq, use [s, α, γ], dsimp, simp_rw [monoid_hom.map_mul, ← comp_apply, ← F.map_comp, h₁, h₂, h₃, F.map_comp, comp_apply, hfg] end /-- Multiplication in the colimit. See also `colimit_mul_aux`. -/ @[to_additive "Addition in the colimit. See also `colimit_add_aux`."] instance colimit_has_mul : has_mul M := { mul := λ x y, begin refine quot.lift₂ (colimit_mul_aux F) _ _ x y, { intros x y y' h, apply colimit_mul_aux_eq_of_rel_right, apply types.filtered_colimit.rel_of_quot_rel, exact h }, { intros x x' y h, apply colimit_mul_aux_eq_of_rel_left, apply types.filtered_colimit.rel_of_quot_rel, exact h }, end } /-- Multiplication in the colimit is independent of the chosen "maximum" in the filtered category. In particular, this lemma allows us to "unfold" the definition of the multiplication of `x` and `y`, using a custom object `k` and morphisms `f : x.1 ⟶ k` and `g : y.1 ⟶ k`. -/ @[to_additive "Addition in the colimit is independent of the chosen \"maximum\" in the filtered category. In particular, this lemma allows us to \"unfold\" the definition of the addition of `x` and `y`, using a custom object `k` and morphisms `f : x.1 ⟶ k` and `g : y.1 ⟶ k`."] lemma colimit_mul_mk_eq (x y : Σ j, F.obj j) (k : J) (f : x.1 ⟶ k) (g : y.1 ⟶ k) : (M.mk x) * (M.mk y) = M.mk ⟨k, F.map f x.2 * F.map g y.2⟩ := begin cases x with j₁ x, cases y with j₂ y, obtain ⟨s, α, β, h₁, h₂⟩ := bowtie (left_to_max j₁ j₂) f (right_to_max j₁ j₂) g, apply M.mk_eq, use [s, α, β], dsimp, simp_rw [monoid_hom.map_mul, ← comp_apply, ← F.map_comp, h₁, h₂], end @[to_additive] instance colimit_monoid : monoid M := { one_mul := λ x, begin apply quot.induction_on x, clear x, intro x, cases x with j x, rw [colimit_one_eq F j, colimit_mul_mk_eq F ⟨j, 1⟩ ⟨j, x⟩ j (𝟙 j) (𝟙 j), monoid_hom.map_one, one_mul, F.map_id, id_apply], end, mul_one := λ x, begin apply quot.induction_on x, clear x, intro x, cases x with j x, rw [colimit_one_eq F j, colimit_mul_mk_eq F ⟨j, x⟩ ⟨j, 1⟩ j (𝟙 j) (𝟙 j), monoid_hom.map_one, mul_one, F.map_id, id_apply], end, mul_assoc := λ x y z, begin apply quot.induction_on₃ x y z, clear x y z, intros x y z, cases x with j₁ x, cases y with j₂ y, cases z with j₃ z, rw [colimit_mul_mk_eq F ⟨j₁, x⟩ ⟨j₂, y⟩ _ (first_to_max₃ j₁ j₂ j₃) (second_to_max₃ j₁ j₂ j₃), colimit_mul_mk_eq F ⟨max₃ j₁ j₂ j₃, _⟩ ⟨j₃, z⟩ _ (𝟙 _) (third_to_max₃ j₁ j₂ j₃), colimit_mul_mk_eq F ⟨j₂, y⟩ ⟨j₃, z⟩ _ (second_to_max₃ j₁ j₂ j₃) (third_to_max₃ j₁ j₂ j₃), colimit_mul_mk_eq F ⟨j₁, x⟩ ⟨max₃ j₁ j₂ j₃, _⟩ _ (first_to_max₃ j₁ j₂ j₃) (𝟙 _)], simp only [F.map_id, id_apply, mul_assoc], end, ..colimit_has_one, ..colimit_has_mul } /-- The bundled monoid giving the filtered colimit of a diagram. -/ @[to_additive "The bundled additive monoid giving the filtered colimit of a diagram."] def colimit : Mon := Mon.of M /-- The monoid homomorphism from a given monoid in the diagram to the colimit monoid. -/ @[to_additive "The additive monoid homomorphism from a given additive monoid in the diagram to the colimit additive monoid."] def cocone_morphism (j : J) : F.obj j ⟶ colimit := { to_fun := (types.colimit_cocone (F ⋙ forget Mon)).ι.app j, map_one' := (colimit_one_eq j).symm, map_mul' := λ x y, begin convert (colimit_mul_mk_eq F ⟨j, x⟩ ⟨j, y⟩ j (𝟙 j) (𝟙 j)).symm, rw [F.map_id, id_apply, id_apply], refl, end } @[simp, to_additive] lemma cocone_naturality {j j' : J} (f : j ⟶ j') : F.map f ≫ (cocone_morphism j') = cocone_morphism j := monoid_hom.coe_inj ((types.colimit_cocone (F ⋙ forget Mon)).ι.naturality f) /-- The cocone over the proposed colimit monoid. -/ @[to_additive "The cocone over the proposed colimit additive monoid."] def colimit_cocone : cocone F := { X := colimit, ι := { app := cocone_morphism } }. /-- Given a cocone `t` of `F`, the induced monoid homomorphism from the colimit to the cocone point. As a function, this is simply given by the induced map of the corresponding cocone in `Type`. The only thing left to see is that it is a monoid homomorphism. -/ @[to_additive "Given a cocone `t` of `F`, the induced additive monoid homomorphism from the colimit to the cocone point. As a function, this is simply given by the induced map of the corresponding cocone in `Type`. The only thing left to see is that it is an additive monoid homomorphism."] def colimit_desc (t : cocone F) : colimit ⟶ t.X := { to_fun := (types.colimit_cocone_is_colimit (F ⋙ forget Mon)).desc ((forget Mon).map_cocone t), map_one' := begin rw colimit_one_eq F is_filtered.nonempty.some, exact monoid_hom.map_one _, end, map_mul' := λ x y, begin apply quot.induction_on₂ x y, clear x y, intros x y, cases x with i x, cases y with j y, rw colimit_mul_mk_eq F ⟨i, x⟩ ⟨j, y⟩ (max' i j) (left_to_max i j) (right_to_max i j), dsimp [types.colimit_cocone_is_colimit], rw [monoid_hom.map_mul, t.w_apply, t.w_apply], end } /-- The proposed colimit cocone is a colimit in `Mon`. -/ @[to_additive "The proposed colimit cocone is a colimit in `AddMon`."] def colimit_cocone_is_colimit : is_colimit colimit_cocone := { desc := colimit_desc, fac' := λ t j, monoid_hom.coe_inj ((types.colimit_cocone_is_colimit (F ⋙ forget Mon)).fac ((forget Mon).map_cocone t) j), uniq' := λ t m h, monoid_hom.coe_inj $ (types.colimit_cocone_is_colimit (F ⋙ forget Mon)).uniq ((forget Mon).map_cocone t) m (λ j, funext $ λ x, monoid_hom.congr_fun (h j) x) } @[to_additive] instance forget_preserves_filtered_colimits : preserves_filtered_colimits (forget Mon.{u}) := { preserves_filtered_colimits := λ J _ _, by exactI { preserves_colimit := λ F, preserves_colimit_of_preserves_colimit_cocone (colimit_cocone_is_colimit.{u u} F) (types.colimit_cocone_is_colimit (F ⋙ forget Mon.{u})) } } end end Mon.filtered_colimits namespace CommMon.filtered_colimits open Mon.filtered_colimits (colimit_mul_mk_eq) section -- We use parameters here, mainly so we can have the abbreviation `M` below, without -- passing around `F` all the time. parameters {J : Type v} [small_category J] [is_filtered J] (F : J ⥤ CommMon.{max v u}) /-- The colimit of `F ⋙ forget₂ CommMon Mon` in the category `Mon`. In the following, we will show that this has the structure of a _commutative_ monoid. -/ @[to_additive "The colimit of `F ⋙ forget₂ AddCommMon AddMon` in the category `AddMon`. In the following, we will show that this has the structure of a _commutative_ additive monoid."] abbreviation M : Mon := Mon.filtered_colimits.colimit (F ⋙ forget₂ CommMon Mon.{max v u}) @[to_additive] instance colimit_comm_monoid : comm_monoid M := { mul_comm := λ x y, begin apply quot.induction_on₂ x y, clear x y, intros x y, let k := max' x.1 y.1, let f := left_to_max x.1 y.1, let g := right_to_max x.1 y.1, rw [colimit_mul_mk_eq _ x y k f g, colimit_mul_mk_eq _ y x k g f], dsimp, rw mul_comm, end ..M.monoid } /-- The bundled commutative monoid giving the filtered colimit of a diagram. -/ @[to_additive "The bundled additive commutative monoid giving the filtered colimit of a diagram."] def colimit : CommMon := CommMon.of M /-- The cocone over the proposed colimit commutative monoid. -/ @[to_additive "The cocone over the proposed colimit additive commutative monoid."] def colimit_cocone : cocone F := { X := colimit, ι := { ..(Mon.filtered_colimits.colimit_cocone (F ⋙ forget₂ CommMon Mon.{max v u})).ι } } /-- The proposed colimit cocone is a colimit in `CommMon`. -/ @[to_additive "The proposed colimit cocone is a colimit in `AddCommMon`."] def colimit_cocone_is_colimit : is_colimit colimit_cocone := { desc := λ t, Mon.filtered_colimits.colimit_desc (F ⋙ forget₂ CommMon Mon.{max v u}) ((forget₂ CommMon Mon.{max v u}).map_cocone t), fac' := λ t j, monoid_hom.coe_inj $ (types.colimit_cocone_is_colimit (F ⋙ forget CommMon)).fac ((forget CommMon).map_cocone t) j, uniq' := λ t m h, monoid_hom.coe_inj $ (types.colimit_cocone_is_colimit (F ⋙ forget CommMon)).uniq ((forget CommMon).map_cocone t) m ((λ j, funext $ λ x, monoid_hom.congr_fun (h j) x)) } @[to_additive forget₂_AddMon_preserves_filtered_colimits] instance forget₂_Mon_preserves_filtered_colimits : preserves_filtered_colimits (forget₂ CommMon Mon.{u}) := { preserves_filtered_colimits := λ J _ _, by exactI { preserves_colimit := λ F, preserves_colimit_of_preserves_colimit_cocone (colimit_cocone_is_colimit.{u u} F) (Mon.filtered_colimits.colimit_cocone_is_colimit (F ⋙ forget₂ CommMon Mon.{u})) } } @[to_additive] instance forget_preserves_filtered_colimits : preserves_filtered_colimits (forget CommMon.{u}) := limits.comp_preserves_filtered_colimits (forget₂ CommMon Mon) (forget Mon) end end CommMon.filtered_colimits
34c1bd69d1d8108e487f59b587effb798c0ba15a
0ed3609caf1962115b28aeb010d2bda5f67ddc4c
/src/set_theory/ordinal.lean
2af3b90bf513a5db906f145e49c7f6c2ad4c17f0
[ "Apache-2.0" ]
permissive
jonaslippert/mathlib
82dba29632969e3ed1c153a6454306f6bc9d9037
1435a196db69a7886a11e310e8923f3dcf249b81
refs/heads/master
1,609,938,673,069
1,582,018,388,000
1,582,018,388,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
138,225
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Ordinal arithmetic. Ordinals are defined as equivalences of well-ordered sets by order isomorphism. -/ import order.order_iso set_theory.cardinal data.sum noncomputable theory open function cardinal set equiv open_locale classical cardinal universes u v w variables {α : Type*} {β : Type*} {γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} /-- If `r` is a relation on `α` and `s` in a relation on `β`, then `f : r ≼i s` is an order embedding whose range is an initial segment. That is, whenever `b < f a` in `β` then `b` is in the range of `f`. -/ structure initial_seg {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends r ≼o s := (init : ∀ a b, s b (to_order_embedding a) → ∃ a', to_order_embedding a' = b) local infix ` ≼i `:25 := initial_seg namespace initial_seg instance : has_coe (r ≼i s) (r ≼o s) := ⟨initial_seg.to_order_embedding⟩ @[simp] theorem coe_fn_mk (f : r ≼o s) (o) : (@initial_seg.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_order_embedding (f : r ≼i s) : (f.to_order_embedding : α → β) = f := rfl theorem coe_coe_fn (f : r ≼i s) : ((f : r ≼o s) : α → β) = f := rfl theorem init' (f : r ≼i s) {a : α} {b : β} : s b (f a) → ∃ a', f a' = b := f.init _ _ theorem init_iff (f : r ≼i s) {a : α} {b : β} : s b (f a) ↔ ∃ a', f a' = b ∧ r a' a := ⟨λ h, let ⟨a', e⟩ := f.init' h in ⟨a', e, (f : r ≼o s).ord'.2 (e.symm ▸ h)⟩, λ ⟨a', e, h⟩, e ▸ (f : r ≼o s).ord'.1 h⟩ /-- An order isomorphism is an initial segment -/ def of_iso (f : r ≃o s) : r ≼i s := ⟨f, λ a b h, ⟨f.symm b, order_iso.apply_symm_apply f _⟩⟩ /-- The identity function shows that `≼i` is reflexive -/ @[refl] protected def refl (r : α → α → Prop) : r ≼i r := ⟨order_embedding.refl _, λ a b h, ⟨_, rfl⟩⟩ /-- Composition of functions shows that `≼i` is transitive -/ @[trans] protected def trans (f : r ≼i s) (g : s ≼i t) : r ≼i t := ⟨f.1.trans g.1, λ a c h, begin simp at h ⊢, rcases g.2 _ _ h with ⟨b, rfl⟩, have h := g.1.ord'.2 h, rcases f.2 _ _ h with ⟨a', rfl⟩, exact ⟨a', rfl⟩ end⟩ @[simp] theorem of_iso_apply (f : r ≃o s) (x : α) : of_iso f x = f x := rfl @[simp] theorem refl_apply (x : α) : initial_seg.refl r x = x := rfl @[simp] theorem trans_apply (f : r ≼i s) (g : s ≼i t) (a : α) : (f.trans g) a = g (f a) := rfl theorem unique_of_extensional [is_extensional β s] : well_founded r → subsingleton (r ≼i s) | ⟨h⟩ := ⟨λ f g, begin suffices : (f : α → β) = g, { cases f, cases g, congr, exact order_embedding.eq_of_to_fun_eq this }, funext a, have := h a, induction this with a H IH, refine @is_extensional.ext _ s _ _ _ (λ x, ⟨λ h, _, λ h, _⟩), { rcases f.init_iff.1 h with ⟨y, rfl, h'⟩, rw IH _ h', exact (g : r ≼o s).ord'.1 h' }, { rcases g.init_iff.1 h with ⟨y, rfl, h'⟩, rw ← IH _ h', exact (f : r ≼o s).ord'.1 h' } end⟩ instance [is_well_order β s] : subsingleton (r ≼i s) := ⟨λ a, @subsingleton.elim _ (unique_of_extensional (@order_embedding.well_founded _ _ r s a (is_well_order.wf s))) a⟩ protected theorem eq [is_well_order β s] (f g : r ≼i s) (a) : f a = g a := by rw subsingleton.elim f g theorem antisymm.aux [is_well_order α r] (f : r ≼i s) (g : s ≼i r) : left_inverse g f := initial_seg.eq (f.trans g) (initial_seg.refl _) /-- If we have order embeddings between `α` and `β` whose images are initial segments, and β is a well-order then `α` and `β` are order-isomorphic. -/ def antisymm [is_well_order β s] (f : r ≼i s) (g : s ≼i r) : r ≃o s := by haveI := f.to_order_embedding.is_well_order; exact ⟨⟨f, g, antisymm.aux f g, antisymm.aux g f⟩, f.ord⟩ @[simp] theorem antisymm_to_fun [is_well_order β s] (f : r ≼i s) (g : s ≼i r) : (antisymm f g : α → β) = f := rfl @[simp] theorem antisymm_symm [is_well_order α r] [is_well_order β s] (f : r ≼i s) (g : s ≼i r) : (antisymm f g).symm = antisymm g f := order_iso.eq_of_to_fun_eq rfl theorem eq_or_principal [is_well_order β s] (f : r ≼i s) : surjective f ∨ ∃ b, ∀ x, s x b ↔ ∃ y, f y = x := or_iff_not_imp_right.2 $ λ h b, acc.rec_on ((is_well_order.wf s).apply b) $ λ x H IH, not_forall_not.1 $ λ hn, h ⟨x, λ y, ⟨(IH _), λ ⟨a, e⟩, by rw ← e; exact (trichotomous _ _).resolve_right (not_or (hn a) (λ hl, not_exists.2 hn (f.init' hl)))⟩⟩ /-- Restrict the codomain of an initial segment -/ def cod_restrict (p : set β) (f : r ≼i s) (H : ∀ a, f a ∈ p) : r ≼i subrel s p := ⟨order_embedding.cod_restrict p f H, λ a ⟨b, m⟩ (h : s b (f a)), let ⟨a', e⟩ := f.init' h in ⟨a', by clear _let_match; subst e; refl⟩⟩ @[simp] theorem cod_restrict_apply (p) (f : r ≼i s) (H a) : cod_restrict p f H a = ⟨f a, H a⟩ := rfl def le_add (r : α → α → Prop) (s : β → β → Prop) : r ≼i sum.lex r s := ⟨⟨⟨sum.inl, λ _ _, sum.inl.inj⟩, λ a b, sum.lex_inl_inl.symm⟩, λ a b, by cases b; [exact λ _, ⟨_, rfl⟩, exact false.elim ∘ sum.lex_inr_inl]⟩ @[simp] theorem le_add_apply (r : α → α → Prop) (s : β → β → Prop) (a) : le_add r s a = sum.inl a := rfl end initial_seg structure principal_seg {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends r ≼o s := (top : β) (down : ∀ b, s b top ↔ ∃ a, to_order_embedding a = b) local infix ` ≺i `:25 := principal_seg namespace principal_seg instance : has_coe (r ≺i s) (r ≼o s) := ⟨principal_seg.to_order_embedding⟩ @[simp] theorem coe_fn_mk (f : r ≼o s) (t o) : (@principal_seg.mk _ _ r s f t o : α → β) = f := rfl @[simp] theorem coe_fn_to_order_embedding (f : r ≺i s) : (f.to_order_embedding : α → β) = f := rfl theorem coe_coe_fn (f : r ≺i s) : ((f : r ≼o s) : α → β) = f := rfl theorem down' (f : r ≺i s) {b : β} : s b f.top ↔ ∃ a, f a = b := f.down _ theorem lt_top (f : r ≺i s) (a : α) : s (f a) f.top := f.down'.2 ⟨_, rfl⟩ theorem init [is_trans β s] (f : r ≺i s) {a : α} {b : β} (h : s b (f a)) : ∃ a', f a' = b := f.down'.1 $ trans h $ f.lt_top _ instance has_coe_initial_seg [is_trans β s] : has_coe (r ≺i s) (r ≼i s) := ⟨λ f, ⟨f.to_order_embedding, λ a b, f.init⟩⟩ theorem coe_coe_fn' [is_trans β s] (f : r ≺i s) : ((f : r ≼i s) : α → β) = f := rfl theorem init_iff [is_trans β s] (f : r ≺i s) {a : α} {b : β} : s b (f a) ↔ ∃ a', f a' = b ∧ r a' a := initial_seg.init_iff f theorem irrefl (r : α → α → Prop) [is_well_order α r] (f : r ≺i r) : false := begin have := f.lt_top f.top, rw [show f f.top = f.top, from initial_seg.eq ↑f (initial_seg.refl r) f.top] at this, exact irrefl _ this end def lt_le (f : r ≺i s) (g : s ≼i t) : r ≺i t := ⟨@order_embedding.trans _ _ _ r s t f g, g f.top, λ a, by simp only [g.init_iff, f.down', exists_and_distrib_left.symm, exists_swap, order_embedding.trans_apply, exists_eq_right']; refl⟩ @[simp] theorem lt_le_apply [is_trans β s] [is_trans γ t] (f : r ≺i s) (g : s ≼i t) (a : α) : (f.lt_le g) a = g (f a) := order_embedding.trans_apply _ _ _ @[simp] theorem lt_le_top (f : r ≺i s) (g : s ≼i t) : (f.lt_le g).top = g f.top := rfl @[trans] protected def trans [is_trans γ t] (f : r ≺i s) (g : s ≺i t) : r ≺i t := lt_le f g @[simp] theorem trans_apply [is_trans β s] [is_trans γ t] (f : r ≺i s) (g : s ≺i t) (a : α) : (f.trans g) a = g (f a) := lt_le_apply _ _ _ @[simp] theorem trans_top [is_trans β s] [is_trans γ t] (f : r ≺i s) (g : s ≺i t) : (f.trans g).top = g f.top := rfl def equiv_lt (f : r ≃o s) (g : s ≺i t) : r ≺i t := ⟨@order_embedding.trans _ _ _ r s t f g, g.top, λ c, by simp only [g.down', coe_fn_coe_base, order_embedding.trans_apply]; exact ⟨λ ⟨b, h⟩, ⟨f.symm b, by simp only [h, order_iso.apply_symm_apply, order_iso.coe_coe_fn]⟩, λ ⟨a, h⟩, ⟨f a, h⟩⟩⟩ def lt_equiv {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} (f : principal_seg r s) (g : s ≃o t) : principal_seg r t := ⟨@order_embedding.trans _ _ _ r s t f g, g f.top, begin intro x, rw [←g.right_inv x], simp only [order_iso.to_equiv_to_fun, coe_fn_coe_base, order_embedding.trans_apply], rw [←order_iso.ord'' g, f.down', exists_congr], intro y, exact ⟨congr_arg g, λ h, g.to_equiv.bijective.1 h⟩ end⟩ @[simp] theorem equiv_lt_apply [is_trans γ t] (f : r ≃o s) (g : s ≺i t) (a : α) : (equiv_lt f g) a = g (f a) := order_embedding.trans_apply _ _ _ @[simp] theorem equiv_lt_top (f : r ≃o s) (g : s ≺i t) : (equiv_lt f g).top = g.top := rfl instance [is_well_order β s] : subsingleton (r ≺i s) := ⟨λ f g, begin have ef : (f : α → β) = g, { show ((f : r ≼i s) : α → β) = g, rw @subsingleton.elim _ _ (f : r ≼i s) g, refl }, have et : f.top = g.top, { refine @is_extensional.ext _ s _ _ _ (λ x, _), simp only [f.down, g.down, ef, coe_fn_to_order_embedding] }, cases f, cases g, have := order_embedding.eq_of_to_fun_eq ef; congr' end⟩ theorem top_eq [is_well_order γ t] (e : r ≃o s) (f : r ≺i t) (g : s ≺i t) : f.top = g.top := by rw subsingleton.elim f (principal_seg.equiv_lt e g); refl lemma top_lt_top {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} [is_trans β s] [is_well_order γ t] (f : principal_seg r s) (g : principal_seg s t) (h : principal_seg r t) : t h.top g.top := by { rw [subsingleton.elim h (f.trans g)], apply principal_seg.lt_top } /-- Any element of a well order yields a principal segment -/ def of_element {α : Type*} (r : α → α → Prop) (a : α) : subrel r {b | r b a} ≺i r := ⟨subrel.order_embedding _ _, a, λ b, ⟨λ h, ⟨⟨_, h⟩, rfl⟩, λ ⟨⟨_, h⟩, rfl⟩, h⟩⟩ @[simp] theorem of_element_apply {α : Type*} (r : α → α → Prop) [is_well_order α r] (a : α) (b) : of_element r a b = b.1 := rfl @[simp] theorem of_element_top {α : Type*} (r : α → α → Prop) (a : α) : (of_element r a).top = a := rfl /-- Restrict the codomain of a principal segment -/ def cod_restrict (p : set β) (f : r ≺i s) (H : ∀ a, f a ∈ p) (H₂ : f.top ∈ p) : r ≺i subrel s p := ⟨order_embedding.cod_restrict p f H, ⟨f.top, H₂⟩, λ ⟨b, h⟩, f.down'.trans $ exists_congr $ λ a, show (⟨f a, H a⟩ : p).1 = _ ↔ _, from ⟨subtype.eq, congr_arg _⟩⟩ @[simp] theorem cod_restrict_apply (p) (f : r ≺i s) (H H₂ a) : cod_restrict p f H H₂ a = ⟨f a, H a⟩ := rfl @[simp] theorem cod_restrict_top (p) (f : r ≺i s) (H H₂) : (cod_restrict p f H H₂).top = ⟨f.top, H₂⟩ := rfl end principal_seg def initial_seg.lt_or_eq [is_well_order β s] (f : r ≼i s) : (r ≺i s) ⊕ (r ≃o s) := if h : surjective f then sum.inr (order_iso.of_surjective f h) else have h' : _, from (initial_seg.eq_or_principal f).resolve_left h, sum.inl ⟨f, classical.some h', classical.some_spec h'⟩ theorem initial_seg.lt_or_eq_apply_left [is_well_order β s] (f : r ≼i s) (g : r ≺i s) (a : α) : g a = f a := initial_seg.eq g f a theorem initial_seg.lt_or_eq_apply_right [is_well_order β s] (f : r ≼i s) (g : r ≃o s) (a : α) : g a = f a := initial_seg.eq (initial_seg.of_iso g) f a def initial_seg.le_lt [is_well_order β s] [is_trans γ t] (f : r ≼i s) (g : s ≺i t) : r ≺i t := match f.lt_or_eq with | sum.inl f' := f'.trans g | sum.inr f' := principal_seg.equiv_lt f' g end @[simp] theorem initial_seg.le_lt_apply [is_well_order β s] [is_trans γ t] (f : r ≼i s) (g : s ≺i t) (a : α) : (f.le_lt g) a = g (f a) := begin delta initial_seg.le_lt, cases h : f.lt_or_eq with f' f', { simp only [principal_seg.trans_apply, f.lt_or_eq_apply_left] }, { simp only [principal_seg.equiv_lt_apply, f.lt_or_eq_apply_right] } end namespace order_embedding def collapse_F [is_well_order β s] (f : r ≼o s) : Π a, {b // ¬ s (f a) b} := (order_embedding.well_founded f $ is_well_order.wf s).fix $ λ a IH, begin let S := {b | ∀ a h, s (IH a h).1 b}, have : f a ∈ S, from λ a' h, ((trichotomous _ _) .resolve_left $ λ h', (IH a' h).2 $ trans (f.ord'.1 h) h') .resolve_left $ λ h', (IH a' h).2 $ h' ▸ f.ord'.1 h, exact ⟨(is_well_order.wf s).min S ⟨_, this⟩, (is_well_order.wf s).not_lt_min _ _ this⟩ end theorem collapse_F.lt [is_well_order β s] (f : r ≼o s) {a : α} : ∀ {a'}, r a' a → s (collapse_F f a').1 (collapse_F f a).1 := show (collapse_F f a).1 ∈ {b | ∀ a' (h : r a' a), s (collapse_F f a').1 b}, begin unfold collapse_F, rw well_founded.fix_eq, apply well_founded.min_mem _ _ end theorem collapse_F.not_lt [is_well_order β s] (f : r ≼o s) (a : α) {b} (h : ∀ a' (h : r a' a), s (collapse_F f a').1 b) : ¬ s b (collapse_F f a).1 := begin unfold collapse_F, rw well_founded.fix_eq, exact well_founded.not_lt_min _ _ _ (show b ∈ {b | ∀ a' (h : r a' a), s (collapse_F f a').1 b}, from h) end /-- Construct an initial segment from an order embedding. -/ def collapse [is_well_order β s] (f : r ≼o s) : r ≼i s := by haveI := order_embedding.is_well_order f; exact ⟨order_embedding.of_monotone (λ a, (collapse_F f a).1) (λ a b, collapse_F.lt f), λ a b, acc.rec_on ((is_well_order.wf s).apply b) (λ b H IH a h, begin let S := {a | ¬ s (collapse_F f a).1 b}, have : S.nonempty := ⟨_, asymm h⟩, existsi (is_well_order.wf r).min S this, refine ((@trichotomous _ s _ _ _).resolve_left _).resolve_right _, { exact (is_well_order.wf r).min_mem S this }, { refine collapse_F.not_lt f _ (λ a' h', _), by_contradiction hn, exact (is_well_order.wf r).not_lt_min S this hn h' } end) a⟩ theorem collapse_apply [is_well_order β s] (f : r ≼o s) (a) : collapse f a = (collapse_F f a).1 := rfl end order_embedding section well_ordering_thm parameter {σ : Type u} open function theorem nonempty_embedding_to_cardinal : nonempty (σ ↪ cardinal.{u}) := embedding.total.resolve_left $ λ ⟨⟨f, hf⟩⟩, let g : σ → cardinal.{u} := inv_fun f in let ⟨x, (hx : g x = 2 ^ sum g)⟩ := inv_fun_surjective hf (2 ^ sum g) in have g x ≤ sum g, from le_sum.{u u} g x, not_le_of_gt (by rw hx; exact cantor _) this /-- An embedding of any type to the set of cardinals. -/ def embedding_to_cardinal : σ ↪ cardinal.{u} := classical.choice nonempty_embedding_to_cardinal /-- The relation whose existence is given by the well-ordering theorem -/ def well_ordering_rel : σ → σ → Prop := embedding_to_cardinal ⁻¹'o (<) instance well_ordering_rel.is_well_order : is_well_order σ well_ordering_rel := (order_embedding.preimage _ _).is_well_order end well_ordering_thm structure Well_order : Type (u+1) := (α : Type u) (r : α → α → Prop) (wo : is_well_order α r) attribute [instance] Well_order.wo namespace Well_order instance : inhabited Well_order := ⟨⟨pempty, _, empty_relation.is_well_order⟩⟩ end Well_order instance ordinal.is_equivalent : setoid Well_order := { r := λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, nonempty (r ≃o s), iseqv := ⟨λ⟨α, r, _⟩, ⟨order_iso.refl _⟩, λ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨e⟩, ⟨e.symm⟩, λ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨e₁⟩ ⟨e₂⟩, ⟨e₁.trans e₂⟩⟩ } /-- `ordinal.{u}` is the type of well orders in `Type u`, quotient by order isomorphism. -/ def ordinal : Type (u + 1) := quotient ordinal.is_equivalent namespace ordinal /-- The order type of a well order is an ordinal. -/ def type (r : α → α → Prop) [wo : is_well_order α r] : ordinal := ⟦⟨α, r, wo⟩⟧ /-- The order type of an element inside a well order. -/ def typein (r : α → α → Prop) [is_well_order α r] (a : α) : ordinal := type (subrel r {b | r b a}) theorem type_def (r : α → α → Prop) [wo : is_well_order α r] : @eq ordinal ⟦⟨α, r, wo⟩⟧ (type r) := rfl @[simp] theorem type_def' (r : α → α → Prop) [is_well_order α r] {wo} : @eq ordinal ⟦⟨α, r, wo⟩⟧ (type r) := rfl theorem type_eq {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] : type r = type s ↔ nonempty (r ≃o s) := quotient.eq @[simp] lemma type_out (o : ordinal) : type o.out.r = o := by { refine eq.trans _ (by rw [←quotient.out_eq o]), cases quotient.out o, refl } @[elab_as_eliminator] theorem induction_on {C : ordinal → Prop} (o : ordinal) (H : ∀ α r [is_well_order α r], C (type r)) : C o := quot.induction_on o $ λ ⟨α, r, wo⟩, @H α r wo /-- Ordinal less-equal is defined such that well orders `r` and `s` satisfy `type r ≤ type s` if there exists a function embedding `r` as an initial segment of `s`. -/ protected def le (a b : ordinal) : Prop := quotient.lift_on₂ a b (λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, nonempty (r ≼i s)) $ λ ⟨α₁, r₁, o₁⟩ ⟨α₂, r₂, o₂⟩ ⟨β₁, s₁, p₁⟩ ⟨β₂, s₂, p₂⟩ ⟨f⟩ ⟨g⟩, propext ⟨ λ ⟨h⟩, ⟨(initial_seg.of_iso f.symm).trans $ h.trans (initial_seg.of_iso g)⟩, λ ⟨h⟩, ⟨(initial_seg.of_iso f).trans $ h.trans (initial_seg.of_iso g.symm)⟩⟩ instance : has_le ordinal := ⟨ordinal.le⟩ theorem type_le {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] : type r ≤ type s ↔ nonempty (r ≼i s) := iff.rfl theorem type_le' {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] : type r ≤ type s ↔ nonempty (r ≼o s) := ⟨λ ⟨f⟩, ⟨f⟩, λ ⟨f⟩, ⟨f.collapse⟩⟩ /-- Ordinal less-than is defined such that well orders `r` and `s` satisfy `type r < type s` if there exists a function embedding `r` as a principal segment of `s`. -/ def lt (a b : ordinal) : Prop := quotient.lift_on₂ a b (λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, nonempty (r ≺i s)) $ λ ⟨α₁, r₁, o₁⟩ ⟨α₂, r₂, o₂⟩ ⟨β₁, s₁, p₁⟩ ⟨β₂, s₂, p₂⟩ ⟨f⟩ ⟨g⟩, by exactI propext ⟨ λ ⟨h⟩, ⟨principal_seg.equiv_lt f.symm $ h.lt_le (initial_seg.of_iso g)⟩, λ ⟨h⟩, ⟨principal_seg.equiv_lt f $ h.lt_le (initial_seg.of_iso g.symm)⟩⟩ instance : has_lt ordinal := ⟨ordinal.lt⟩ @[simp] theorem type_lt {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] : type r < type s ↔ nonempty (r ≺i s) := iff.rfl instance : partial_order ordinal := { le := (≤), lt := (<), le_refl := quot.ind $ by exact λ ⟨α, r, wo⟩, ⟨initial_seg.refl _⟩, le_trans := λ a b c, quotient.induction_on₃ a b c $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨f⟩ ⟨g⟩, ⟨f.trans g⟩, lt_iff_le_not_le := λ a b, quotient.induction_on₂ a b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩, by exactI ⟨λ ⟨f⟩, ⟨⟨f⟩, λ ⟨g⟩, (f.lt_le g).irrefl _⟩, λ ⟨⟨f⟩, h⟩, sum.rec_on f.lt_or_eq (λ g, ⟨g⟩) (λ g, (h ⟨initial_seg.of_iso g.symm⟩).elim)⟩, le_antisymm := λ x b, show x ≤ b → b ≤ x → x = b, from quotient.induction_on₂ x b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨h₁⟩ ⟨h₂⟩, by exactI quot.sound ⟨initial_seg.antisymm h₁ h₂⟩ } def initial_seg_out {α β : ordinal} (h : α ≤ β) : initial_seg α.out.r β.out.r := begin rw [←quotient.out_eq α, ←quotient.out_eq β] at h, revert h, cases quotient.out α, cases quotient.out β, exact classical.choice end def principal_seg_out {α β : ordinal} (h : α < β) : principal_seg α.out.r β.out.r := begin rw [←quotient.out_eq α, ←quotient.out_eq β] at h, revert h, cases quotient.out α, cases quotient.out β, exact classical.choice end def order_iso_out {α β : ordinal} (h : α = β) : order_iso α.out.r β.out.r := begin rw [←quotient.out_eq α, ←quotient.out_eq β] at h, revert h, cases quotient.out α, cases quotient.out β, exact classical.choice ∘ quotient.exact end theorem typein_lt_type (r : α → α → Prop) [is_well_order α r] (a : α) : typein r a < type r := ⟨principal_seg.of_element _ _⟩ @[simp] theorem typein_top {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : r ≺i s) : typein s f.top = type r := eq.symm $ quot.sound ⟨order_iso.of_surjective (order_embedding.cod_restrict _ f f.lt_top) (λ ⟨a, h⟩, by rcases f.down'.1 h with ⟨b, rfl⟩; exact ⟨b, rfl⟩)⟩ @[simp] theorem typein_apply {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : r ≼i s) (a : α) : ordinal.typein s (f a) = ordinal.typein r a := eq.symm $ quotient.sound ⟨order_iso.of_surjective (order_embedding.cod_restrict _ ((subrel.order_embedding _ _).trans f) (λ ⟨x, h⟩, by rw [order_embedding.trans_apply]; exact f.to_order_embedding.ord'.1 h)) (λ ⟨y, h⟩, by rcases f.init' h with ⟨a, rfl⟩; exact ⟨⟨a, f.to_order_embedding.ord'.2 h⟩, subtype.eq $ order_embedding.trans_apply _ _ _⟩)⟩ @[simp] theorem typein_lt_typein (r : α → α → Prop) [is_well_order α r] {a b : α} : typein r a < typein r b ↔ r a b := ⟨λ ⟨f⟩, begin have : f.top.1 = a, { let f' := principal_seg.of_element r a, let g' := f.trans (principal_seg.of_element r b), have : g'.top = f'.top, {rw subsingleton.elim f' g'}, exact this }, rw ← this, exact f.top.2 end, λ h, ⟨principal_seg.cod_restrict _ (principal_seg.of_element r a) (λ x, @trans _ r _ _ _ _ x.2 h) h⟩⟩ theorem typein_surj (r : α → α → Prop) [is_well_order α r] {o} (h : o < type r) : ∃ a, typein r a = o := induction_on o (λ β s _ ⟨f⟩, by exactI ⟨f.top, typein_top _⟩) h lemma injective_typein (r : α → α → Prop) [is_well_order α r] : injective (typein r) := injective_of_increasing r (<) (typein r) (λ x y, (typein_lt_typein r).2) theorem typein_inj (r : α → α → Prop) [is_well_order α r] {a b} : typein r a = typein r b ↔ a = b := injective.eq_iff (injective_typein r) /-- `enum r o h` is the `o`-th element of `α` ordered by `r`. That is, `enum` maps an initial segment of the ordinals, those less than the order type of `r`, to the elements of `α`. -/ def enum (r : α → α → Prop) [is_well_order α r] (o) : o < type r → α := quot.rec_on o (λ ⟨β, s, _⟩ h, (classical.choice h).top) $ λ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨h⟩, begin resetI, refine funext (λ (H₂ : type t < type r), _), have H₁ : type s < type r, {rwa type_eq.2 ⟨h⟩}, have : ∀ {o e} (H : o < type r), @@eq.rec (λ (o : ordinal), o < type r → α) (λ (h : type s < type r), (classical.choice h).top) e H = (classical.choice H₁).top, {intros, subst e}, exact (this H₂).trans (principal_seg.top_eq h (classical.choice H₁) (classical.choice H₂)) end theorem enum_type {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : s ≺i r) {h : type s < type r} : enum r (type s) h = f.top := principal_seg.top_eq (order_iso.refl _) _ _ @[simp] theorem enum_typein (r : α → α → Prop) [is_well_order α r] (a : α) {h : typein r a < type r} : enum r (typein r a) h = a := enum_type (principal_seg.of_element r a) @[simp] theorem typein_enum (r : α → α → Prop) [is_well_order α r] {o} (h : o < type r) : typein r (enum r o h) = o := let ⟨a, e⟩ := typein_surj r h in by clear _let_match; subst e; rw enum_typein def typein_iso (r : α → α → Prop) [is_well_order α r] : r ≃o subrel (<) (< type r) := ⟨⟨λ x, ⟨typein r x, typein_lt_type r x⟩, λ x, enum r x.1 x.2, λ y, enum_typein r y, λ ⟨y, hy⟩, subtype.eq (typein_enum r hy)⟩, λ a b, (typein_lt_typein r).symm⟩ theorem enum_lt {r : α → α → Prop} [is_well_order α r] {o₁ o₂ : ordinal} (h₁ : o₁ < type r) (h₂ : o₂ < type r) : r (enum r o₁ h₁) (enum r o₂ h₂) ↔ o₁ < o₂ := by rw [← typein_lt_typein r, typein_enum, typein_enum] lemma order_iso_enum' {α β : Type u} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : order_iso r s) (o : ordinal) : ∀(hr : o < type r) (hs : o < type s), f (enum r o hr) = enum s o hs := begin refine induction_on o _, rintros γ t wo ⟨g⟩ ⟨h⟩, resetI, rw [enum_type g, enum_type (principal_seg.lt_equiv g f)], refl end lemma order_iso_enum {α β : Type u} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : order_iso r s) (o : ordinal) (hr : o < type r) : f (enum r o hr) = enum s o (by {convert hr using 1, apply quotient.sound, exact ⟨f.symm⟩ }) := order_iso_enum' _ _ _ _ theorem wf : @well_founded ordinal (<) := ⟨λ a, induction_on a $ λ α r wo, by exactI suffices ∀ a, acc (<) (typein r a), from ⟨_, λ o h, let ⟨a, e⟩ := typein_surj r h in e ▸ this a⟩, λ a, acc.rec_on (wo.wf.apply a) $ λ x H IH, ⟨_, λ o h, begin rcases typein_surj r (lt_trans h (typein_lt_type r _)) with ⟨b, rfl⟩, exact IH _ ((typein_lt_typein r).1 h) end⟩⟩ instance : has_well_founded ordinal := ⟨(<), wf⟩ /-- The cardinal of an ordinal is the cardinal of any set with that order type. -/ def card (o : ordinal) : cardinal := quot.lift_on o (λ ⟨α, r, _⟩, mk α) $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨e⟩, quotient.sound ⟨e.to_equiv⟩ @[simp] theorem card_type (r : α → α → Prop) [is_well_order α r] : card (type r) = mk α := rfl lemma card_typein {r : α → α → Prop} [wo : is_well_order α r] (x : α) : mk {y // r y x} = (typein r x).card := rfl theorem card_le_card {o₁ o₂ : ordinal} : o₁ ≤ o₂ → card o₁ ≤ card o₂ := induction_on o₁ $ λ α r _, induction_on o₂ $ λ β s _ ⟨⟨⟨f, _⟩, _⟩⟩, ⟨f⟩ instance : has_zero ordinal := ⟨⟦⟨pempty, empty_relation, by apply_instance⟩⟧⟩ instance : inhabited ordinal := ⟨0⟩ theorem zero_eq_type_empty : 0 = @type empty empty_relation _ := quotient.sound ⟨⟨empty_equiv_pempty.symm, λ _ _, iff.rfl⟩⟩ @[simp] theorem card_zero : card 0 = 0 := rfl theorem zero_le (o : ordinal) : 0 ≤ o := induction_on o $ λ α r _, ⟨⟨⟨embedding.of_not_nonempty $ λ ⟨a⟩, a.elim, λ a, a.elim⟩, λ a, a.elim⟩⟩ @[simp] theorem le_zero {o : ordinal} : o ≤ 0 ↔ o = 0 := by simp only [le_antisymm_iff, zero_le, and_true] theorem pos_iff_ne_zero {o : ordinal} : 0 < o ↔ o ≠ 0 := by simp only [lt_iff_le_and_ne, zero_le, true_and, ne.def, eq_comm] instance : has_one ordinal := ⟨⟦⟨punit, empty_relation, by apply_instance⟩⟧⟩ theorem one_eq_type_unit : 1 = @type unit empty_relation _ := quotient.sound ⟨⟨punit_equiv_punit, λ _ _, iff.rfl⟩⟩ @[simp] theorem card_one : card 1 = 1 := rfl instance : has_add ordinal.{u} := ⟨λo₁ o₂, quotient.lift_on₂ o₁ o₂ (λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, ⟦⟨α ⊕ β, sum.lex r s, by exactI sum.lex.is_well_order⟩⟧ : Well_order → Well_order → ordinal) $ λ ⟨α₁, r₁, o₁⟩ ⟨α₂, r₂, o₂⟩ ⟨β₁, s₁, p₁⟩ ⟨β₂, s₂, p₂⟩ ⟨f⟩ ⟨g⟩, quot.sound ⟨order_iso.sum_lex_congr f g⟩⟩ @[simp] theorem type_add {α β : Type u} (r : α → α → Prop) (s : β → β → Prop) [is_well_order α r] [is_well_order β s] : type r + type s = type (sum.lex r s) := rfl /-- The ordinal successor is the smallest ordinal larger than `o`. It is defined as `o + 1`. -/ def succ (o : ordinal) : ordinal := o + 1 theorem succ_eq_add_one (o) : succ o = o + 1 := rfl theorem lt_succ_self (o : ordinal.{u}) : o < succ o := induction_on o $ λ α r _, ⟨⟨⟨⟨λ x, sum.inl x, λ _ _, sum.inl.inj⟩, λ _ _, sum.lex_inl_inl.symm⟩, sum.inr punit.star, λ b, sum.rec_on b (λ x, ⟨λ _, ⟨x, rfl⟩, λ _, sum.lex.sep _ _ _ _⟩) (λ x, sum.lex_inr_inr.trans ⟨false.elim, λ ⟨x, H⟩, sum.inl_ne_inr H⟩)⟩⟩ theorem succ_pos (o : ordinal) : 0 < succ o := lt_of_le_of_lt (zero_le _) (lt_succ_self _) theorem succ_ne_zero (o : ordinal) : succ o ≠ 0 := ne_of_gt $ succ_pos o theorem succ_le {a b : ordinal} : succ a ≤ b ↔ a < b := ⟨lt_of_lt_of_le (lt_succ_self _), induction_on a $ λ α r hr, induction_on b $ λ β s hs ⟨⟨f, t, hf⟩⟩, begin refine ⟨⟨@order_embedding.of_monotone (α ⊕ punit) β _ _ (@sum.lex.is_well_order _ _ _ _ hr _).1.1 (@is_asymm_of_is_trans_of_is_irrefl _ _ hs.1.2.2 hs.1.2.1) (sum.rec _ _) (λ a b, _), λ a b, _⟩⟩, { exact f }, { exact λ _, t }, { rcases a with a|_; rcases b with b|_, { simpa only [sum.lex_inl_inl] using f.ord'.1 }, { intro _, rw hf, exact ⟨_, rfl⟩ }, { exact false.elim ∘ sum.lex_inr_inl }, { exact false.elim ∘ sum.lex_inr_inr.1 } }, { rcases a with a|_, { intro h, have := @principal_seg.init _ _ _ _ hs.1.2.2 ⟨f, t, hf⟩ _ _ h, cases this with w h, exact ⟨sum.inl w, h⟩ }, { intro h, cases (hf b).1 h with w h, exact ⟨sum.inl w, h⟩ } } end⟩ @[simp] theorem card_add (o₁ o₂ : ordinal) : card (o₁ + o₂) = card o₁ + card o₂ := induction_on o₁ $ λ α r _, induction_on o₂ $ λ β s _, rfl @[simp] theorem card_succ (o : ordinal) : card (succ o) = card o + 1 := by simp only [succ, card_add, card_one] @[simp] theorem card_nat (n : ℕ) : card.{u} n = n := by induction n; [refl, simp only [card_add, card_one, nat.cast_succ, *]] theorem nat_cast_succ (n : ℕ) : (succ n : ordinal) = n.succ := rfl instance : add_monoid ordinal.{u} := { add := (+), zero := 0, zero_add := λ o, induction_on o $ λ α r _, eq.symm $ quotient.sound ⟨⟨(pempty_sum α).symm, λ a b, sum.lex_inr_inr.symm⟩⟩, add_zero := λ o, induction_on o $ λ α r _, eq.symm $ quotient.sound ⟨⟨(sum_pempty α).symm, λ a b, sum.lex_inl_inl.symm⟩⟩, add_assoc := λ o₁ o₂ o₃, quotient.induction_on₃ o₁ o₂ o₃ $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩, quot.sound ⟨⟨sum_assoc _ _ _, λ a b, begin rcases a with ⟨a|a⟩|a; rcases b with ⟨b|b⟩|b; simp only [sum_assoc_apply_in1, sum_assoc_apply_in2, sum_assoc_apply_in3, sum.lex_inl_inl, sum.lex_inr_inr, sum.lex.sep, sum.lex_inr_inl] end⟩⟩ } theorem add_succ (o₁ o₂ : ordinal) : o₁ + succ o₂ = succ (o₁ + o₂) := (add_assoc _ _ _).symm @[simp] theorem succ_zero : succ 0 = 1 := zero_add _ theorem one_le_iff_pos {o : ordinal} : 1 ≤ o ↔ 0 < o := by rw [← succ_zero, succ_le] theorem one_le_iff_ne_zero {o : ordinal} : 1 ≤ o ↔ o ≠ 0 := by rw [one_le_iff_pos, pos_iff_ne_zero] theorem add_le_add_left {a b : ordinal} : a ≤ b → ∀ c, c + a ≤ c + b := induction_on a $ λ α₁ r₁ _, induction_on b $ λ α₂ r₂ _ ⟨⟨⟨f, fo⟩, fi⟩⟩ c, induction_on c $ λ β s _, ⟨⟨⟨(embedding.refl _).sum_congr f, λ a b, match a, b with | sum.inl a, sum.inl b := sum.lex_inl_inl.trans sum.lex_inl_inl.symm | sum.inl a, sum.inr b := by apply iff_of_true; apply sum.lex.sep | sum.inr a, sum.inl b := by apply iff_of_false; exact sum.lex_inr_inl | sum.inr a, sum.inr b := sum.lex_inr_inr.trans $ fo.trans sum.lex_inr_inr.symm end⟩, λ a b H, match a, b, H with | _, sum.inl b, _ := ⟨sum.inl b, rfl⟩ | sum.inl a, sum.inr b, H := (sum.lex_inr_inl H).elim | sum.inr a, sum.inr b, H := let ⟨w, h⟩ := fi _ _ (sum.lex_inr_inr.1 H) in ⟨sum.inr w, congr_arg sum.inr h⟩ end⟩⟩ theorem le_add_right (a b : ordinal) : a ≤ a + b := by simpa only [add_zero] using add_le_add_left (zero_le b) a theorem add_le_add_iff_left (a) {b c : ordinal} : a + b ≤ a + c ↔ b ≤ c := ⟨induction_on a $ λ α r hr, induction_on b $ λ β₁ s₁ hs₁, induction_on c $ λ β₂ s₂ hs₂ ⟨f⟩, ⟨ have fl : ∀ a, f (sum.inl a) = sum.inl a := λ a, by simpa only [initial_seg.trans_apply, initial_seg.le_add_apply] using @initial_seg.eq _ _ _ _ (@sum.lex.is_well_order _ _ _ _ hr hs₂) ((initial_seg.le_add r s₁).trans f) (initial_seg.le_add r s₂) a, have ∀ b, {b' // f (sum.inr b) = sum.inr b'}, begin intro b, cases e : f (sum.inr b), { rw ← fl at e, have := f.inj e, contradiction }, { exact ⟨_, rfl⟩ } end, let g (b) := (this b).1 in have fr : ∀ b, f (sum.inr b) = sum.inr (g b), from λ b, (this b).2, ⟨⟨⟨g, λ x y h, by injection f.inj (by rw [fr, fr, h] : f (sum.inr x) = f (sum.inr y))⟩, λ a b, by simpa only [sum.lex_inr_inr, fr, order_embedding.coe_fn_to_embedding, initial_seg.coe_fn_to_order_embedding, function.embedding.coe_fn_mk] using @order_embedding.ord _ _ _ _ f.to_order_embedding (sum.inr a) (sum.inr b)⟩, λ a b H, begin rcases f.init' (by rw fr; exact sum.lex_inr_inr.2 H) with ⟨a'|a', h⟩, { rw fl at h, cases h }, { rw fr at h, exact ⟨a', sum.inr.inj h⟩ } end⟩⟩, λ h, add_le_add_left h _⟩ theorem add_left_cancel (a) {b c : ordinal} : a + b = a + c ↔ b = c := by simp only [le_antisymm_iff, add_le_add_iff_left] /-- The universe lift operation for ordinals, which embeds `ordinal.{u}` as a proper initial segment of `ordinal.{v}` for `v > u`. -/ def lift (o : ordinal.{u}) : ordinal.{max u v} := quotient.lift_on o (λ ⟨α, r, wo⟩, @type _ _ (@order_embedding.is_well_order _ _ (@equiv.ulift.{u v} α ⁻¹'o r) r (order_iso.preimage equiv.ulift.{u v} r) wo)) $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨f⟩, quot.sound ⟨(order_iso.preimage equiv.ulift r).trans $ f.trans (order_iso.preimage equiv.ulift s).symm⟩ theorem lift_type {α} (r : α → α → Prop) [is_well_order α r] : ∃ wo', lift (type r) = @type _ (@equiv.ulift.{u v} α ⁻¹'o r) wo' := ⟨_, rfl⟩ theorem lift_umax : lift.{u (max u v)} = lift.{u v} := funext $ λ a, induction_on a $ λ α r _, quotient.sound ⟨(order_iso.preimage equiv.ulift r).trans (order_iso.preimage equiv.ulift r).symm⟩ theorem lift_id' (a : ordinal) : lift a = a := induction_on a $ λ α r _, quotient.sound ⟨order_iso.preimage equiv.ulift r⟩ @[simp] theorem lift_id : ∀ a, lift.{u u} a = a := lift_id'.{u u} @[simp] theorem lift_lift (a : ordinal) : lift.{(max u v) w} (lift.{u v} a) = lift.{u (max v w)} a := induction_on a $ λ α r _, quotient.sound ⟨(order_iso.preimage equiv.ulift _).trans $ (order_iso.preimage equiv.ulift _).trans (order_iso.preimage equiv.ulift _).symm⟩ theorem lift_type_le {α : Type u} {β : Type v} {r s} [is_well_order α r] [is_well_order β s] : lift.{u (max v w)} (type r) ≤ lift.{v (max u w)} (type s) ↔ nonempty (r ≼i s) := ⟨λ ⟨f⟩, ⟨(initial_seg.of_iso (order_iso.preimage equiv.ulift r).symm).trans $ f.trans (initial_seg.of_iso (order_iso.preimage equiv.ulift s))⟩, λ ⟨f⟩, ⟨(initial_seg.of_iso (order_iso.preimage equiv.ulift r)).trans $ f.trans (initial_seg.of_iso (order_iso.preimage equiv.ulift s).symm)⟩⟩ theorem lift_type_eq {α : Type u} {β : Type v} {r s} [is_well_order α r] [is_well_order β s] : lift.{u (max v w)} (type r) = lift.{v (max u w)} (type s) ↔ nonempty (r ≃o s) := quotient.eq.trans ⟨λ ⟨f⟩, ⟨(order_iso.preimage equiv.ulift r).symm.trans $ f.trans (order_iso.preimage equiv.ulift s)⟩, λ ⟨f⟩, ⟨(order_iso.preimage equiv.ulift r).trans $ f.trans (order_iso.preimage equiv.ulift s).symm⟩⟩ theorem lift_type_lt {α : Type u} {β : Type v} {r s} [is_well_order α r] [is_well_order β s] : lift.{u (max v w)} (type r) < lift.{v (max u w)} (type s) ↔ nonempty (r ≺i s) := by haveI := @order_embedding.is_well_order _ _ (@equiv.ulift.{u (max v w)} α ⁻¹'o r) r (order_iso.preimage equiv.ulift.{u (max v w)} r) _; haveI := @order_embedding.is_well_order _ _ (@equiv.ulift.{v (max u w)} β ⁻¹'o s) s (order_iso.preimage equiv.ulift.{v (max u w)} s) _; exact ⟨λ ⟨f⟩, ⟨(f.equiv_lt (order_iso.preimage equiv.ulift r).symm).lt_le (initial_seg.of_iso (order_iso.preimage equiv.ulift s))⟩, λ ⟨f⟩, ⟨(f.equiv_lt (order_iso.preimage equiv.ulift r)).lt_le (initial_seg.of_iso (order_iso.preimage equiv.ulift s).symm)⟩⟩ @[simp] theorem lift_le {a b : ordinal} : lift.{u v} a ≤ lift b ↔ a ≤ b := induction_on a $ λ α r _, induction_on b $ λ β s _, by rw ← lift_umax; exactI lift_type_le @[simp] theorem lift_inj {a b : ordinal} : lift a = lift b ↔ a = b := by simp only [le_antisymm_iff, lift_le] @[simp] theorem lift_lt {a b : ordinal} : lift a < lift b ↔ a < b := by simp only [lt_iff_le_not_le, lift_le] @[simp] theorem lift_zero : lift 0 = 0 := quotient.sound ⟨(order_iso.preimage equiv.ulift _).trans ⟨pempty_equiv_pempty, λ a b, iff.rfl⟩⟩ theorem zero_eq_lift_type_empty : 0 = lift.{0 u} (@type empty empty_relation _) := by rw [← zero_eq_type_empty, lift_zero] @[simp] theorem lift_one : lift 1 = 1 := quotient.sound ⟨(order_iso.preimage equiv.ulift _).trans ⟨punit_equiv_punit, λ a b, iff.rfl⟩⟩ theorem one_eq_lift_type_unit : 1 = lift.{0 u} (@type unit empty_relation _) := by rw [← one_eq_type_unit, lift_one] @[simp] theorem lift_add (a b) : lift (a + b) = lift a + lift b := quotient.induction_on₂ a b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩, quotient.sound ⟨(order_iso.preimage equiv.ulift _).trans (order_iso.sum_lex_congr (order_iso.preimage equiv.ulift _) (order_iso.preimage equiv.ulift _)).symm⟩ @[simp] theorem lift_succ (a) : lift (succ a) = succ (lift a) := by unfold succ; simp only [lift_add, lift_one] @[simp] theorem lift_card (a) : (card a).lift = card (lift a) := induction_on a $ λ α r _, rfl theorem lift_down' {a : cardinal.{u}} {b : ordinal.{max u v}} (h : card b ≤ a.lift) : ∃ a', lift a' = b := let ⟨c, e⟩ := cardinal.lift_down h in quotient.induction_on c (λ α, induction_on b $ λ β s _ e', begin resetI, rw [mk_def, card_type, ← cardinal.lift_id'.{(max u v) u} (mk β), ← cardinal.lift_umax.{u v}, lift_mk_eq.{u (max u v) (max u v)}] at e', cases e' with f, have g := order_iso.preimage f s, haveI := g.to_order_embedding.is_well_order, have := lift_type_eq.{u (max u v) (max u v)}.2 ⟨g⟩, rw [lift_id, lift_umax.{u v}] at this, exact ⟨_, this⟩ end) e theorem lift_down {a : ordinal.{u}} {b : ordinal.{max u v}} (h : b ≤ lift a) : ∃ a', lift a' = b := @lift_down' (card a) _ (by rw lift_card; exact card_le_card h) theorem le_lift_iff {a : ordinal.{u}} {b : ordinal.{max u v}} : b ≤ lift a ↔ ∃ a', lift a' = b ∧ a' ≤ a := ⟨λ h, let ⟨a', e⟩ := lift_down h in ⟨a', e, lift_le.1 $ e.symm ▸ h⟩, λ ⟨a', e, h⟩, e ▸ lift_le.2 h⟩ theorem lt_lift_iff {a : ordinal.{u}} {b : ordinal.{max u v}} : b < lift a ↔ ∃ a', lift a' = b ∧ a' < a := ⟨λ h, let ⟨a', e⟩ := lift_down (le_of_lt h) in ⟨a', e, lift_lt.1 $ e.symm ▸ h⟩, λ ⟨a', e, h⟩, e ▸ lift_lt.2 h⟩ /-- `ω` is the first infinite ordinal, defined as the order type of `ℕ`. -/ def omega : ordinal.{u} := lift $ @type ℕ (<) _ localized "notation `ω` := ordinal.omega.{0}" in ordinal theorem card_omega : card omega = cardinal.omega := rfl @[simp] theorem lift_omega : lift omega = omega := lift_lift _ theorem add_le_add_right {a b : ordinal} : a ≤ b → ∀ c, a + c ≤ b + c := induction_on a $ λ α₁ r₁ hr₁, induction_on b $ λ α₂ r₂ hr₂ ⟨⟨⟨f, fo⟩, fi⟩⟩ c, induction_on c $ λ β s hs, (@type_le' _ _ _ _ (@sum.lex.is_well_order _ _ _ _ hr₁ hs) (@sum.lex.is_well_order _ _ _ _ hr₂ hs)).2 ⟨⟨embedding.sum_congr f (embedding.refl _), λ a b, begin split; intro H, { cases H; constructor; [rwa ← fo, assumption] }, { cases a with a a; cases b with b b; cases H; constructor; [rwa fo, assumption] } end⟩⟩ theorem le_add_left (a b : ordinal) : a ≤ b + a := by simpa only [zero_add] using add_le_add_right (zero_le b) a theorem le_total (a b : ordinal) : a ≤ b ∨ b ≤ a := match lt_or_eq_of_le (le_add_left b a), lt_or_eq_of_le (le_add_right a b) with | or.inr h, _ := by rw h; exact or.inl (le_add_right _ _) | _, or.inr h := by rw h; exact or.inr (le_add_left _ _) | or.inl h₁, or.inl h₂ := induction_on a (λ α₁ r₁ _, induction_on b $ λ α₂ r₂ _ ⟨f⟩ ⟨g⟩, begin resetI, rw [← typein_top f, ← typein_top g, le_iff_lt_or_eq, le_iff_lt_or_eq, typein_lt_typein, typein_lt_typein], rcases trichotomous_of (sum.lex r₁ r₂) g.top f.top with h|h|h; [exact or.inl (or.inl h), {left, right, rw h}, exact or.inr (or.inl h)] end) h₁ h₂ end instance : decidable_linear_order ordinal := { le_total := le_total, decidable_le := classical.dec_rel _, ..ordinal.partial_order } @[simp] lemma typein_le_typein (r : α → α → Prop) [is_well_order α r] {x x' : α} : typein r x ≤ typein r x' ↔ ¬r x' x := by rw [←not_lt, typein_lt_typein] lemma enum_le_enum (r : α → α → Prop) [is_well_order α r] {o o' : ordinal} (ho : o < type r) (ho' : o' < type r) : ¬r (enum r o' ho') (enum r o ho) ↔ o ≤ o' := by rw [←@not_lt _ _ o' o, enum_lt ho'] theorem lt_succ {a b : ordinal} : a < succ b ↔ a ≤ b := by rw [← not_le, succ_le, not_lt] theorem add_lt_add_iff_left (a) {b c : ordinal} : a + b < a + c ↔ b < c := by rw [← not_le, ← not_le, add_le_add_iff_left] theorem lt_of_add_lt_add_right {a b c : ordinal} : a + b < c + b → a < c := lt_imp_lt_of_le_imp_le (λ h, add_le_add_right h _) @[simp] theorem succ_lt_succ {a b : ordinal} : succ a < succ b ↔ a < b := by rw [lt_succ, succ_le] @[simp] theorem succ_le_succ {a b : ordinal} : succ a ≤ succ b ↔ a ≤ b := le_iff_le_iff_lt_iff_lt.2 succ_lt_succ theorem succ_inj {a b : ordinal} : succ a = succ b ↔ a = b := by simp only [le_antisymm_iff, succ_le_succ] theorem add_le_add_iff_right {a b : ordinal} (n : ℕ) : a + n ≤ b + n ↔ a ≤ b := by induction n with n ih; [rw [nat.cast_zero, add_zero, add_zero], rw [← nat_cast_succ, add_succ, add_succ, succ_le_succ, ih]] theorem add_right_cancel {a b : ordinal} (n : ℕ) : a + n = b + n ↔ a = b := by simp only [le_antisymm_iff, add_le_add_iff_right] @[simp] theorem card_eq_zero {o} : card o = 0 ↔ o = 0 := ⟨induction_on o $ λ α r _ h, begin refine le_antisymm (le_of_not_lt $ λ hn, ne_zero_iff_nonempty.2 _ h) (zero_le _), rw [← succ_le, succ_zero] at hn, cases hn with f, exact ⟨f punit.star⟩ end, λ e, by simp only [e, card_zero]⟩ @[simp] theorem type_ne_zero_iff_nonempty [is_well_order α r] : type r ≠ 0 ↔ nonempty α := (not_congr (@card_eq_zero (type r))).symm.trans ne_zero_iff_nonempty @[simp] theorem type_eq_zero_iff_empty [is_well_order α r] : type r = 0 ↔ ¬ nonempty α := (not_iff_comm.1 type_ne_zero_iff_nonempty).symm instance : zero_ne_one_class ordinal.{u} := { zero := 0, one := 1, zero_ne_one := ne.symm $ type_ne_zero_iff_nonempty.2 ⟨punit.star⟩ } theorem zero_lt_one : (0 : ordinal) < 1 := lt_iff_le_and_ne.2 ⟨zero_le _, zero_ne_one⟩ /-- The ordinal predecessor of `o` is `o'` if `o = succ o'`, and `o` otherwise. -/ def pred (o : ordinal.{u}) : ordinal.{u} := if h : ∃ a, o = succ a then classical.some h else o @[simp] theorem pred_succ (o) : pred (succ o) = o := by have h : ∃ a, succ o = succ a := ⟨_, rfl⟩; simpa only [pred, dif_pos h] using (succ_inj.1 $ classical.some_spec h).symm theorem pred_le_self (o) : pred o ≤ o := if h : ∃ a, o = succ a then let ⟨a, e⟩ := h in by rw [e, pred_succ]; exact le_of_lt (lt_succ_self _) else by rw [pred, dif_neg h] theorem pred_eq_iff_not_succ {o} : pred o = o ↔ ¬ ∃ a, o = succ a := ⟨λ e ⟨a, e'⟩, by rw [e', pred_succ] at e; exact ne_of_lt (lt_succ_self _) e, λ h, dif_neg h⟩ theorem pred_lt_iff_is_succ {o} : pred o < o ↔ ∃ a, o = succ a := iff.trans (by simp only [le_antisymm_iff, pred_le_self, true_and, not_le]) (iff_not_comm.1 pred_eq_iff_not_succ).symm theorem succ_pred_iff_is_succ {o} : succ (pred o) = o ↔ ∃ a, o = succ a := ⟨λ e, ⟨_, e.symm⟩, λ ⟨a, e⟩, by simp only [e, pred_succ]⟩ theorem succ_lt_of_not_succ {o} (h : ¬ ∃ a, o = succ a) {b} : succ b < o ↔ b < o := ⟨lt_trans (lt_succ_self _), λ l, lt_of_le_of_ne (succ_le.2 l) (λ e, h ⟨_, e.symm⟩)⟩ theorem lt_pred {a b} : a < pred b ↔ succ a < b := if h : ∃ a, b = succ a then let ⟨c, e⟩ := h in by rw [e, pred_succ, succ_lt_succ] else by simp only [pred, dif_neg h, succ_lt_of_not_succ h] theorem pred_le {a b} : pred a ≤ b ↔ a ≤ succ b := le_iff_le_iff_lt_iff_lt.2 lt_pred @[simp] theorem lift_is_succ {o} : (∃ a, lift o = succ a) ↔ (∃ a, o = succ a) := ⟨λ ⟨a, h⟩, let ⟨b, e⟩ := lift_down $ show a ≤ lift o, from le_of_lt $ h.symm ▸ lt_succ_self _ in ⟨b, lift_inj.1 $ by rw [h, ← e, lift_succ]⟩, λ ⟨a, h⟩, ⟨lift a, by simp only [h, lift_succ]⟩⟩ @[simp] theorem lift_pred (o) : lift (pred o) = pred (lift o) := if h : ∃ a, o = succ a then by cases h with a e; simp only [e, pred_succ, lift_succ] else by rw [pred_eq_iff_not_succ.2 h, pred_eq_iff_not_succ.2 (mt lift_is_succ.1 h)] /-- A limit ordinal is an ordinal which is not zero and not a successor. -/ def is_limit (o : ordinal) : Prop := o ≠ 0 ∧ ∀ a < o, succ a < o theorem not_zero_is_limit : ¬ is_limit 0 | ⟨h, _⟩ := h rfl theorem not_succ_is_limit (o) : ¬ is_limit (succ o) | ⟨_, h⟩ := lt_irrefl _ (h _ (lt_succ_self _)) theorem not_succ_of_is_limit {o} (h : is_limit o) : ¬ ∃ a, o = succ a | ⟨a, e⟩ := not_succ_is_limit a (e ▸ h) theorem succ_lt_of_is_limit {o} (h : is_limit o) {a} : succ a < o ↔ a < o := ⟨lt_trans (lt_succ_self _), h.2 _⟩ theorem le_succ_of_is_limit {o} (h : is_limit o) {a} : o ≤ succ a ↔ o ≤ a := le_iff_le_iff_lt_iff_lt.2 $ succ_lt_of_is_limit h theorem limit_le {o} (h : is_limit o) {a} : o ≤ a ↔ ∀ x < o, x ≤ a := ⟨λ h x l, le_trans (le_of_lt l) h, λ H, (le_succ_of_is_limit h).1 $ le_of_not_lt $ λ hn, not_lt_of_le (H _ hn) (lt_succ_self _)⟩ theorem lt_limit {o} (h : is_limit o) {a} : a < o ↔ ∃ x < o, a < x := by simpa only [not_ball, not_le] using not_congr (@limit_le _ h a) @[simp] theorem lift_is_limit (o) : is_limit (lift o) ↔ is_limit o := and_congr (not_congr $ by simpa only [lift_zero] using @lift_inj o 0) ⟨λ H a h, lift_lt.1 $ by simpa only [lift_succ] using H _ (lift_lt.2 h), λ H a h, let ⟨a', e⟩ := lift_down (le_of_lt h) in by rw [← e, ← lift_succ, lift_lt]; rw [← e, lift_lt] at h; exact H a' h⟩ theorem is_limit.pos {o : ordinal} (h : is_limit o) : 0 < o := lt_of_le_of_ne (zero_le _) h.1.symm theorem is_limit.one_lt {o : ordinal} (h : is_limit o) : 1 < o := by simpa only [succ_zero] using h.2 _ h.pos theorem is_limit.nat_lt {o : ordinal} (h : is_limit o) : ∀ n : ℕ, (n : ordinal) < o | 0 := h.pos | (n+1) := h.2 _ (is_limit.nat_lt n) theorem zero_or_succ_or_limit (o : ordinal) : o = 0 ∨ (∃ a, o = succ a) ∨ is_limit o := if o0 : o = 0 then or.inl o0 else if h : ∃ a, o = succ a then or.inr (or.inl h) else or.inr $ or.inr ⟨o0, λ a, (succ_lt_of_not_succ h).2⟩ instance : is_well_order ordinal (<) := ⟨wf⟩ @[elab_as_eliminator] def limit_rec_on {C : ordinal → Sort*} (o : ordinal) (H₁ : C 0) (H₂ : ∀ o, C o → C (succ o)) (H₃ : ∀ o, is_limit o → (∀ o' < o, C o') → C o) : C o := wf.fix (λ o IH, if o0 : o = 0 then by rw o0; exact H₁ else if h : ∃ a, o = succ a then by rw ← succ_pred_iff_is_succ.2 h; exact H₂ _ (IH _ $ pred_lt_iff_is_succ.2 h) else H₃ _ ⟨o0, λ a, (succ_lt_of_not_succ h).2⟩ IH) o @[simp] theorem limit_rec_on_zero {C} (H₁ H₂ H₃) : @limit_rec_on C 0 H₁ H₂ H₃ = H₁ := by rw [limit_rec_on, well_founded.fix_eq, dif_pos rfl]; refl @[simp] theorem limit_rec_on_succ {C} (o H₁ H₂ H₃) : @limit_rec_on C (succ o) H₁ H₂ H₃ = H₂ o (@limit_rec_on C o H₁ H₂ H₃) := begin have h : ∃ a, succ o = succ a := ⟨_, rfl⟩, rw [limit_rec_on, well_founded.fix_eq, dif_neg (succ_ne_zero o), dif_pos h], generalize : limit_rec_on._proof_2 (succ o) h = h₂, generalize : limit_rec_on._proof_3 (succ o) h = h₃, revert h₂ h₃, generalize e : pred (succ o) = o', intros, rw pred_succ at e, subst o', refl end @[simp] theorem limit_rec_on_limit {C} (o H₁ H₂ H₃ h) : @limit_rec_on C o H₁ H₂ H₃ = H₃ o h (λ x h, @limit_rec_on C x H₁ H₂ H₃) := by rw [limit_rec_on, well_founded.fix_eq, dif_neg h.1, dif_neg (not_succ_of_is_limit h)]; refl lemma has_succ_of_is_limit {α} {r : α → α → Prop} [wo : is_well_order α r] (h : (type r).is_limit) (x : α) : ∃y, r x y := begin use enum r (typein r x).succ (h.2 _ (typein_lt_type r x)), convert (enum_lt (typein_lt_type r x) _).mpr (lt_succ_self _), rw [enum_typein] end lemma type_subrel_lt (o : ordinal.{u}) : type (subrel (<) {o' : ordinal | o' < o}) = ordinal.lift.{u u+1} o := begin refine quotient.induction_on o _, rintro ⟨α, r, wo⟩, resetI, apply quotient.sound, constructor, symmetry, refine (order_iso.preimage equiv.ulift r).trans (typein_iso r) end lemma mk_initial_seg (o : ordinal.{u}) : #{o' : ordinal | o' < o} = cardinal.lift.{u u+1} o.card := by rw [lift_card, ←type_subrel_lt, card_type] /-- A normal ordinal function is a strictly increasing function which is order-continuous. -/ def is_normal (f : ordinal → ordinal) : Prop := (∀ o, f o < f (succ o)) ∧ ∀ o, is_limit o → ∀ a, f o ≤ a ↔ ∀ b < o, f b ≤ a theorem is_normal.limit_le {f} (H : is_normal f) : ∀ {o}, is_limit o → ∀ {a}, f o ≤ a ↔ ∀ b < o, f b ≤ a := H.2 theorem is_normal.limit_lt {f} (H : is_normal f) {o} (h : is_limit o) {a} : a < f o ↔ ∃ b < o, a < f b := not_iff_not.1 $ by simpa only [exists_prop, not_exists, not_and, not_lt] using H.2 _ h a theorem is_normal.lt_iff {f} (H : is_normal f) {a b} : f a < f b ↔ a < b := strict_mono.lt_iff_lt $ λ a b, limit_rec_on b (not.elim (not_lt_of_le $ zero_le _)) (λ b IH h, (lt_or_eq_of_le (lt_succ.1 h)).elim (λ h, lt_trans (IH h) (H.1 _)) (λ e, e ▸ H.1 _)) (λ b l IH h, lt_of_lt_of_le (H.1 a) ((H.2 _ l _).1 (le_refl _) _ (l.2 _ h))) theorem is_normal.le_iff {f} (H : is_normal f) {a b} : f a ≤ f b ↔ a ≤ b := le_iff_le_iff_lt_iff_lt.2 H.lt_iff theorem is_normal.inj {f} (H : is_normal f) {a b} : f a = f b ↔ a = b := by simp only [le_antisymm_iff, H.le_iff] theorem is_normal.le_self {f} (H : is_normal f) (a) : a ≤ f a := limit_rec_on a (zero_le _) (λ a IH, succ_le.2 $ lt_of_le_of_lt IH (H.1 _)) (λ a l IH, (limit_le l).2 $ λ b h, le_trans (IH b h) $ H.le_iff.2 $ le_of_lt h) theorem is_normal.le_set {f} (H : is_normal f) (p : ordinal → Prop) (p0 : ∃ x, p x) (S) (H₂ : ∀ o, S ≤ o ↔ ∀ a, p a → a ≤ o) {o} : f S ≤ o ↔ ∀ a, p a → f a ≤ o := ⟨λ h a pa, le_trans (H.le_iff.2 ((H₂ _).1 (le_refl _) _ pa)) h, λ h, begin revert H₂, apply limit_rec_on S, { intro H₂, cases p0 with x px, have := le_zero.1 ((H₂ _).1 (zero_le _) _ px), rw this at px, exact h _ px }, { intros S _ H₂, rcases not_ball.1 (mt (H₂ S).2 $ not_le_of_lt $ lt_succ_self _) with ⟨a, h₁, h₂⟩, exact le_trans (H.le_iff.2 $ succ_le.2 $ not_le.1 h₂) (h _ h₁) }, { intros S L _ H₂, apply (H.2 _ L _).2, intros a h', rcases not_ball.1 (mt (H₂ a).2 (not_le.2 h')) with ⟨b, h₁, h₂⟩, exact le_trans (H.le_iff.2 $ le_of_lt $ not_le.1 h₂) (h _ h₁) } end⟩ theorem is_normal.le_set' {f} (H : is_normal f) (p : α → Prop) (g : α → ordinal) (p0 : ∃ x, p x) (S) (H₂ : ∀ o, S ≤ o ↔ ∀ a, p a → g a ≤ o) {o} : f S ≤ o ↔ ∀ a, p a → f (g a) ≤ o := (H.le_set (λ x, ∃ y, p y ∧ x = g y) (let ⟨x, px⟩ := p0 in ⟨_, _, px, rfl⟩) _ (λ o, (H₂ o).trans ⟨λ H a ⟨y, h1, h2⟩, h2.symm ▸ H y h1, λ H a h1, H (g a) ⟨a, h1, rfl⟩⟩)).trans ⟨λ H a h, H (g a) ⟨a, h, rfl⟩, λ H a ⟨y, h1, h2⟩, h2.symm ▸ H y h1⟩ theorem is_normal.refl : is_normal id := ⟨λ x, lt_succ_self _, λ o l a, limit_le l⟩ theorem is_normal.trans {f g} (H₁ : is_normal f) (H₂ : is_normal g) : is_normal (λ x, f (g x)) := ⟨λ x, H₁.lt_iff.2 (H₂.1 _), λ o l a, H₁.le_set' (< o) g ⟨_, l.pos⟩ _ (λ c, H₂.2 _ l _)⟩ theorem is_normal.is_limit {f} (H : is_normal f) {o} (l : is_limit o) : is_limit (f o) := ⟨ne_of_gt $ lt_of_le_of_lt (zero_le _) $ H.lt_iff.2 l.pos, λ a h, let ⟨b, h₁, h₂⟩ := (H.limit_lt l).1 h in lt_of_le_of_lt (succ_le.2 h₂) (H.lt_iff.2 h₁)⟩ theorem add_le_of_limit {a b c : ordinal.{u}} (h : is_limit b) : a + b ≤ c ↔ ∀ b' < b, a + b' ≤ c := ⟨λ h b' l, le_trans (add_le_add_left (le_of_lt l) _) h, λ H, le_of_not_lt $ induction_on a (λ α r _, induction_on b $ λ β s _ h H l, begin resetI, suffices : ∀ x : β, sum.lex r s (sum.inr x) (enum _ _ l), { cases enum _ _ l with x x, { cases this (enum s 0 h.pos) }, { exact irrefl _ (this _) } }, intros x, rw [← typein_lt_typein (sum.lex r s), typein_enum], have := H _ (h.2 _ (typein_lt_type s x)), rw [add_succ, succ_le] at this, refine lt_of_le_of_lt (type_le'.2 ⟨order_embedding.of_monotone (λ a, _) (λ a b, _)⟩) this, { rcases a with ⟨a | b, h⟩, { exact sum.inl a }, { exact sum.inr ⟨b, by cases h; assumption⟩ } }, { rcases a with ⟨a | a, h₁⟩; rcases b with ⟨b | b, h₂⟩; cases h₁; cases h₂; rintro ⟨⟩; constructor; assumption } end) h H⟩ theorem add_is_normal (a : ordinal) : is_normal ((+) a) := ⟨λ b, (add_lt_add_iff_left a).2 (lt_succ_self _), λ b l c, add_le_of_limit l⟩ theorem add_is_limit (a) {b} : is_limit b → is_limit (a + b) := (add_is_normal a).is_limit def typein.principal_seg {α : Type u} (r : α → α → Prop) [is_well_order α r] : @principal_seg α ordinal.{u} r (<) := ⟨order_embedding.of_monotone (typein r) (λ a b, (typein_lt_typein r).2), type r, λ b, ⟨λ h, ⟨enum r _ h, typein_enum r h⟩, λ ⟨a, e⟩, e ▸ typein_lt_type _ _⟩⟩ @[simp] theorem typein.principal_seg_coe (r : α → α → Prop) [is_well_order α r] : (typein.principal_seg r : α → ordinal) = typein r := rfl /-- The minimal element of a nonempty family of ordinals -/ def min {ι} (I : nonempty ι) (f : ι → ordinal) : ordinal := wf.min (set.range f) (let ⟨i⟩ := I in ⟨_, set.mem_range_self i⟩) theorem min_eq {ι} (I) (f : ι → ordinal) : ∃ i, min I f = f i := let ⟨i, e⟩ := wf.min_mem (set.range f) _ in ⟨i, e.symm⟩ theorem min_le {ι I} (f : ι → ordinal) (i) : min I f ≤ f i := le_of_not_gt $ wf.not_lt_min (set.range f) _ (set.mem_range_self i) theorem le_min {ι I} {f : ι → ordinal} {a} : a ≤ min I f ↔ ∀ i, a ≤ f i := ⟨λ h i, le_trans h (min_le _ _), λ h, let ⟨i, e⟩ := min_eq I f in e.symm ▸ h i⟩ /-- The minimal element of a nonempty set of ordinals -/ def omin (S : set ordinal.{u}) (H : ∃ x, x ∈ S) : ordinal.{u} := @min.{(u+2) u} S (let ⟨x, px⟩ := H in ⟨⟨x, px⟩⟩) subtype.val theorem omin_mem (S H) : omin S H ∈ S := let ⟨⟨i, h⟩, e⟩ := @min_eq S _ _ in (show omin S H = i, from e).symm ▸ h theorem le_omin {S H a} : a ≤ omin S H ↔ ∀ i ∈ S, a ≤ i := le_min.trans set_coe.forall theorem omin_le {S H i} (h : i ∈ S) : omin S H ≤ i := le_omin.1 (le_refl _) _ h @[simp] theorem lift_min {ι} (I) (f : ι → ordinal) : lift (min I f) = min I (lift ∘ f) := le_antisymm (le_min.2 $ λ a, lift_le.2 $ min_le _ a) $ let ⟨i, e⟩ := min_eq I (lift ∘ f) in by rw e; exact lift_le.2 (le_min.2 $ λ j, lift_le.1 $ by have := min_le (lift ∘ f) j; rwa e at this) def lift.initial_seg : @initial_seg ordinal.{u} ordinal.{max u v} (<) (<) := ⟨⟨⟨lift.{u v}, λ a b, lift_inj.1⟩, λ a b, lift_lt.symm⟩, λ a b h, lift_down (le_of_lt h)⟩ @[simp] theorem lift.initial_seg_coe : (lift.initial_seg : ordinal → ordinal) = lift := rfl /-- `univ.{u v}` is the order type of the ordinals of `Type u` as a member of `ordinal.{v}` (when `u < v`). It is an inaccessible cardinal. -/ def univ := lift.{(u+1) v} (@type ordinal.{u} (<) _) theorem univ_id : univ.{u (u+1)} = @type ordinal.{u} (<) _ := lift_id _ @[simp] theorem lift_univ : lift.{_ w} univ.{u v} = univ.{u (max v w)} := lift_lift _ theorem univ_umax : univ.{u (max (u+1) v)} = univ.{u v} := congr_fun lift_umax _ def lift.principal_seg : @principal_seg ordinal.{u} ordinal.{max (u+1) v} (<) (<) := ⟨↑lift.initial_seg.{u (max (u+1) v)}, univ.{u v}, begin refine λ b, induction_on b _, introsI β s _, rw [univ, ← lift_umax], split; intro h, { rw ← lift_id (type s) at h ⊢, cases lift_type_lt.1 h with f, cases f with f a hf, existsi a, revert hf, apply induction_on a, intros α r _ hf, refine lift_type_eq.{u (max (u+1) v) (max (u+1) v)}.2 ⟨(order_iso.of_surjective (order_embedding.of_monotone _ _) _).symm⟩, { exact λ b, enum r (f b) ((hf _).2 ⟨_, rfl⟩) }, { refine λ a b h, (typein_lt_typein r).1 _, rw [typein_enum, typein_enum], exact f.ord'.1 h }, { intro a', cases (hf _).1 (typein_lt_type _ a') with b e, existsi b, simp, simp [e] } }, { cases h with a e, rw [← e], apply induction_on a, intros α r _, exact lift_type_lt.{u (u+1) (max (u+1) v)}.2 ⟨typein.principal_seg r⟩ } end⟩ @[simp] theorem lift.principal_seg_coe : (lift.principal_seg.{u v} : ordinal → ordinal) = lift.{u (max (u+1) v)} := rfl @[simp] theorem lift.principal_seg_top : lift.principal_seg.top = univ := rfl theorem lift.principal_seg_top' : lift.principal_seg.{u (u+1)}.top = @type ordinal.{u} (<) _ := by simp only [lift.principal_seg_top, univ_id] /-- `a - b` is the unique ordinal satisfying `b + (a - b) = a` when `b ≤ a`. -/ def sub (a b : ordinal.{u}) : ordinal.{u} := omin {o | a ≤ b+o} ⟨a, le_add_left _ _⟩ instance : has_sub ordinal := ⟨sub⟩ theorem le_add_sub (a b : ordinal) : a ≤ b + (a - b) := omin_mem {o | a ≤ b+o} _ theorem sub_le {a b c : ordinal} : a - b ≤ c ↔ a ≤ b + c := ⟨λ h, le_trans (le_add_sub a b) (add_le_add_left h _), λ h, omin_le h⟩ theorem lt_sub {a b c : ordinal} : a < b - c ↔ c + a < b := lt_iff_lt_of_le_iff_le sub_le theorem add_sub_cancel (a b : ordinal) : a + b - a = b := le_antisymm (sub_le.2 $ le_refl _) ((add_le_add_iff_left a).1 $ le_add_sub _ _) theorem sub_eq_of_add_eq {a b c : ordinal} (h : a + b = c) : c - a = b := h ▸ add_sub_cancel _ _ theorem sub_le_self (a b : ordinal) : a - b ≤ a := sub_le.2 $ le_add_left _ _ theorem add_sub_cancel_of_le {a b : ordinal} (h : b ≤ a) : b + (a - b) = a := le_antisymm begin rcases zero_or_succ_or_limit (a-b) with e|⟨c,e⟩|l, { simp only [e, add_zero, h] }, { rw [e, add_succ, succ_le, ← lt_sub, e], apply lt_succ_self }, { exact (add_le_of_limit l).2 (λ c l, le_of_lt (lt_sub.1 l)) } end (le_add_sub _ _) @[simp] theorem sub_zero (a : ordinal) : a - 0 = a := by simpa only [zero_add] using add_sub_cancel 0 a @[simp] theorem zero_sub (a : ordinal) : 0 - a = 0 := by rw ← le_zero; apply sub_le_self @[simp] theorem sub_self (a : ordinal) : a - a = 0 := by simpa only [add_zero] using add_sub_cancel a 0 theorem sub_eq_zero_iff_le {a b : ordinal} : a - b = 0 ↔ a ≤ b := ⟨λ h, by simpa only [h, add_zero] using le_add_sub a b, λ h, by rwa [← le_zero, sub_le, add_zero]⟩ theorem sub_sub (a b c : ordinal) : a - b - c = a - (b + c) := eq_of_forall_ge_iff $ λ d, by rw [sub_le, sub_le, sub_le, add_assoc] theorem add_sub_add_cancel (a b c : ordinal) : a + b - (a + c) = b - c := by rw [← sub_sub, add_sub_cancel] theorem sub_is_limit {a b} (l : is_limit a) (h : b < a) : is_limit (a - b) := ⟨ne_of_gt $ lt_sub.2 $ by rwa add_zero, λ c h, by rw [lt_sub, add_succ]; exact l.2 _ (lt_sub.1 h)⟩ @[simp] theorem one_add_omega : 1 + omega.{u} = omega := begin refine le_antisymm _ (le_add_left _ _), rw [omega, one_eq_lift_type_unit, ← lift_add, lift_le, type_add], have : is_well_order unit empty_relation := by apply_instance, refine ⟨order_embedding.collapse (order_embedding.of_monotone _ _)⟩, { apply sum.rec, exact λ _, 0, exact nat.succ }, { intros a b, cases a; cases b; intro H; cases H with _ _ H _ _ H; [cases H, exact nat.succ_pos _, exact nat.succ_lt_succ H] } end @[simp] theorem one_add_of_omega_le {o} (h : omega ≤ o) : 1 + o = o := by rw [← add_sub_cancel_of_le h, ← add_assoc, one_add_omega] instance : monoid ordinal.{u} := { mul := λ a b, quotient.lift_on₂ a b (λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, ⟦⟨β × α, prod.lex s r, by exactI prod.lex.is_well_order⟩⟧ : Well_order → Well_order → ordinal) $ λ ⟨α₁, r₁, o₁⟩ ⟨α₂, r₂, o₂⟩ ⟨β₁, s₁, p₁⟩ ⟨β₂, s₂, p₂⟩ ⟨f⟩ ⟨g⟩, quot.sound ⟨order_iso.prod_lex_congr g f⟩, one := 1, mul_assoc := λ a b c, quotient.induction_on₃ a b c $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩, eq.symm $ quotient.sound ⟨⟨prod_assoc _ _ _, λ a b, begin rcases a with ⟨⟨a₁, a₂⟩, a₃⟩, rcases b with ⟨⟨b₁, b₂⟩, b₃⟩, simp [prod.lex_def, and_or_distrib_left, or_assoc, and_assoc] end⟩⟩, mul_one := λ a, induction_on a $ λ α r _, quotient.sound ⟨⟨punit_prod _, λ a b, by rcases a with ⟨⟨⟨⟩⟩, a⟩; rcases b with ⟨⟨⟨⟩⟩, b⟩; simp only [prod.lex_def, empty_relation, false_or]; dsimp only; simp only [eq_self_iff_true, true_and]; refl⟩⟩, one_mul := λ a, induction_on a $ λ α r _, quotient.sound ⟨⟨prod_punit _, λ a b, by rcases a with ⟨a, ⟨⟨⟩⟩⟩; rcases b with ⟨b, ⟨⟨⟩⟩⟩; simp only [prod.lex_def, empty_relation, and_false, or_false]; refl⟩⟩ } @[simp] theorem type_mul {α β : Type u} (r : α → α → Prop) (s : β → β → Prop) [is_well_order α r] [is_well_order β s] : type r * type s = type (prod.lex s r) := rfl @[simp] theorem lift_mul (a b) : lift (a * b) = lift a * lift b := quotient.induction_on₂ a b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩, quotient.sound ⟨(order_iso.preimage equiv.ulift _).trans (order_iso.prod_lex_congr (order_iso.preimage equiv.ulift _) (order_iso.preimage equiv.ulift _)).symm⟩ @[simp] theorem card_mul (a b) : card (a * b) = card a * card b := quotient.induction_on₂ a b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩, mul_comm (mk β) (mk α) @[simp] theorem mul_zero (a : ordinal) : a * 0 = 0 := induction_on a $ λ α _ _, by exactI type_eq_zero_iff_empty.2 (λ ⟨⟨e, _⟩⟩, e.elim) @[simp] theorem zero_mul (a : ordinal) : 0 * a = 0 := induction_on a $ λ α _ _, by exactI type_eq_zero_iff_empty.2 (λ ⟨⟨_, e⟩⟩, e.elim) theorem mul_add (a b c : ordinal) : a * (b + c) = a * b + a * c := quotient.induction_on₃ a b c $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩, quotient.sound ⟨⟨sum_prod_distrib _ _ _, begin rintro ⟨a₁|a₁, a₂⟩ ⟨b₁|b₁, b₂⟩; simp only [prod.lex_def, sum.lex_inl_inl, sum.lex.sep, sum.lex_inr_inl, sum.lex_inr_inr, sum_prod_distrib_apply_left, sum_prod_distrib_apply_right]; simp only [sum.inl.inj_iff, true_or, false_and, false_or] end⟩⟩ @[simp] theorem mul_add_one (a b : ordinal) : a * (b + 1) = a * b + a := by simp only [mul_add, mul_one] @[simp] theorem mul_succ (a b : ordinal) : a * succ b = a * b + a := mul_add_one _ _ theorem mul_le_mul_left {a b} (c : ordinal) : a ≤ b → c * a ≤ c * b := quotient.induction_on₃ a b c $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨f⟩, begin resetI, refine type_le'.2 ⟨order_embedding.of_monotone (λ a, (f a.1, a.2)) (λ a b h, _)⟩, clear_, cases h with a₁ b₁ a₂ b₂ h' a b₁ b₂ h', { exact prod.lex.left _ _ _ (f.to_order_embedding.ord'.1 h') }, { exact prod.lex.right _ _ h' } end theorem mul_le_mul_right {a b} (c : ordinal) : a ≤ b → a * c ≤ b * c := quotient.induction_on₃ a b c $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨f⟩, begin resetI, refine type_le'.2 ⟨order_embedding.of_monotone (λ a, (a.1, f a.2)) (λ a b h, _)⟩, cases h with a₁ b₁ a₂ b₂ h' a b₁ b₂ h', { exact prod.lex.left _ _ _ h' }, { exact prod.lex.right _ _ (f.to_order_embedding.ord'.1 h') } end theorem mul_le_mul {a b c d : ordinal} (h₁ : a ≤ c) (h₂ : b ≤ d) : a * b ≤ c * d := le_trans (mul_le_mul_left _ h₂) (mul_le_mul_right _ h₁) private lemma mul_le_of_limit_aux {α β r s} [is_well_order α r] [is_well_order β s] {c} (h : is_limit (type s)) (H : ∀ b' < type s, type r * b' ≤ c) (l : c < type r * type s) : false := begin suffices : ∀ a b, prod.lex s r (b, a) (enum _ _ l), { cases enum _ _ l with b a, exact irrefl _ (this _ _) }, intros a b, rw [← typein_lt_typein (prod.lex s r), typein_enum], have := H _ (h.2 _ (typein_lt_type s b)), rw [mul_succ] at this, have := lt_of_lt_of_le ((add_lt_add_iff_left _).2 (typein_lt_type _ a)) this, refine lt_of_le_of_lt _ this, refine (type_le'.2 _), constructor, refine order_embedding.of_monotone (λ a, _) (λ a b, _), { rcases a with ⟨⟨b', a'⟩, h⟩, by_cases e : b = b', { refine sum.inr ⟨a', _⟩, subst e, cases h with _ _ _ _ h _ _ _ h, { exact (irrefl _ h).elim }, { exact h } }, { refine sum.inl (⟨b', _⟩, a'), cases h with _ _ _ _ h _ _ _ h, { exact h }, { exact (e rfl).elim } } }, { rcases a with ⟨⟨b₁, a₁⟩, h₁⟩, rcases b with ⟨⟨b₂, a₂⟩, h₂⟩, intro h, by_cases e₁ : b = b₁; by_cases e₂ : b = b₂, { substs b₁ b₂, simpa only [subrel_val, prod.lex_def, @irrefl _ s _ b, true_and, false_or, eq_self_iff_true, dif_pos, sum.lex_inr_inr] using h }, { subst b₁, simp only [subrel_val, prod.lex_def, e₂, prod.lex_def, dif_pos, subrel_val, eq_self_iff_true, or_false, dif_neg, not_false_iff, sum.lex_inr_inl, false_and] at h ⊢, cases h₂; [exact asymm h h₂_h, exact e₂ rfl] }, { simp only [e₂, dif_pos, eq_self_iff_true, dif_neg e₁, not_false_iff, sum.lex.sep] }, { simpa only [dif_neg e₁, dif_neg e₂, prod.lex_def, subrel_val, subtype.mk_eq_mk, sum.lex_inl_inl] using h } } end theorem mul_le_of_limit {a b c : ordinal.{u}} (h : is_limit b) : a * b ≤ c ↔ ∀ b' < b, a * b' ≤ c := ⟨λ h b' l, le_trans (mul_le_mul_left _ (le_of_lt l)) h, λ H, le_of_not_lt $ induction_on a (λ α r _, induction_on b $ λ β s _, by exactI mul_le_of_limit_aux) h H⟩ theorem mul_is_normal {a : ordinal} (h : 0 < a) : is_normal ((*) a) := ⟨λ b, by rw mul_succ; simpa only [add_zero] using (add_lt_add_iff_left (a*b)).2 h, λ b l c, mul_le_of_limit l⟩ theorem lt_mul_of_limit {a b c : ordinal.{u}} (h : is_limit c) : a < b * c ↔ ∃ c' < c, a < b * c' := by simpa only [not_ball, not_le] using not_congr (@mul_le_of_limit b c a h) theorem mul_lt_mul_iff_left {a b c : ordinal} (a0 : 0 < a) : a * b < a * c ↔ b < c := (mul_is_normal a0).lt_iff theorem mul_le_mul_iff_left {a b c : ordinal} (a0 : 0 < a) : a * b ≤ a * c ↔ b ≤ c := (mul_is_normal a0).le_iff theorem mul_lt_mul_of_pos_left {a b c : ordinal} (h : a < b) (c0 : 0 < c) : c * a < c * b := (mul_lt_mul_iff_left c0).2 h theorem mul_pos {a b : ordinal} (h₁ : 0 < a) (h₂ : 0 < b) : 0 < a * b := by simpa only [mul_zero] using mul_lt_mul_of_pos_left h₂ h₁ theorem mul_ne_zero {a b : ordinal} : a ≠ 0 → b ≠ 0 → a * b ≠ 0 := by simpa only [pos_iff_ne_zero] using mul_pos theorem le_of_mul_le_mul_left {a b c : ordinal} (h : c * a ≤ c * b) (h0 : 0 < c) : a ≤ b := le_imp_le_of_lt_imp_lt (λ h', mul_lt_mul_of_pos_left h' h0) h theorem mul_left_inj {a b c : ordinal} (a0 : 0 < a) : a * b = a * c ↔ b = c := (mul_is_normal a0).inj theorem mul_is_limit {a b : ordinal} (a0 : 0 < a) : is_limit b → is_limit (a * b) := (mul_is_normal a0).is_limit theorem mul_is_limit_left {a b : ordinal} (l : is_limit a) (b0 : 0 < b) : is_limit (a * b) := begin rcases zero_or_succ_or_limit b with rfl|⟨b,rfl⟩|lb, { exact (lt_irrefl _).elim b0 }, { rw mul_succ, exact add_is_limit _ l }, { exact mul_is_limit l.pos lb } end /-- `a / b` is the unique ordinal `o` satisfying `a = b * o + o'` with `o' < b`. -/ protected def div (a b : ordinal.{u}) : ordinal.{u} := if h : b = 0 then 0 else omin {o | a < b * succ o} ⟨a, succ_le.1 $ by simpa only [succ_zero, one_mul] using mul_le_mul_right (succ a) (succ_le.2 (pos_iff_ne_zero.2 h))⟩ instance : has_div ordinal := ⟨ordinal.div⟩ @[simp] theorem div_zero (a : ordinal) : a / 0 = 0 := dif_pos rfl -- TODO(lint): This should be a theorem but Lean fails to synthesize the placeholder @[nolint] def div_def (a) {b : ordinal} (h : b ≠ 0) : a / b = omin {o | a < b * succ o} _ := dif_neg h theorem lt_mul_succ_div (a) {b : ordinal} (h : b ≠ 0) : a < b * succ (a / b) := by rw div_def a h; exact omin_mem {o | a < b * succ o} _ theorem lt_mul_div_add (a) {b : ordinal} (h : b ≠ 0) : a < b * (a / b) + b := by simpa only [mul_succ] using lt_mul_succ_div a h theorem div_le {a b c : ordinal} (b0 : b ≠ 0) : a / b ≤ c ↔ a < b * succ c := ⟨λ h, lt_of_lt_of_le (lt_mul_succ_div a b0) (mul_le_mul_left _ $ succ_le_succ.2 h), λ h, by rw div_def a b0; exact omin_le h⟩ theorem lt_div {a b c : ordinal} (c0 : c ≠ 0) : a < b / c ↔ c * succ a ≤ b := by rw [← not_le, div_le c0, not_lt] theorem le_div {a b c : ordinal} (c0 : c ≠ 0) : a ≤ b / c ↔ c * a ≤ b := begin apply limit_rec_on a, { simp only [mul_zero, zero_le] }, { intros, rw [succ_le, lt_div c0] }, { simp only [mul_le_of_limit, limit_le, iff_self, forall_true_iff] {contextual := tt} } end theorem div_lt {a b c : ordinal} (b0 : b ≠ 0) : a / b < c ↔ a < b * c := lt_iff_lt_of_le_iff_le $ le_div b0 theorem div_le_of_le_mul {a b c : ordinal} (h : a ≤ b * c) : a / b ≤ c := if b0 : b = 0 then by simp only [b0, div_zero, zero_le] else (div_le b0).2 $ lt_of_le_of_lt h $ mul_lt_mul_of_pos_left (lt_succ_self _) (pos_iff_ne_zero.2 b0) theorem mul_lt_of_lt_div {a b c : ordinal} : a < b / c → c * a < b := lt_imp_lt_of_le_imp_le div_le_of_le_mul @[simp] theorem zero_div (a : ordinal) : 0 / a = 0 := le_zero.1 $ div_le_of_le_mul $ zero_le _ theorem mul_div_le (a b : ordinal) : b * (a / b) ≤ a := if b0 : b = 0 then by simp only [b0, zero_mul, zero_le] else (le_div b0).1 (le_refl _) theorem mul_add_div (a) {b : ordinal} (b0 : b ≠ 0) (c) : (b * a + c) / b = a + c / b := begin apply le_antisymm, { apply (div_le b0).2, rw [mul_succ, mul_add, add_assoc, add_lt_add_iff_left], apply lt_mul_div_add _ b0 }, { rw [le_div b0, mul_add, add_le_add_iff_left], apply mul_div_le } end theorem div_eq_zero_of_lt {a b : ordinal} (h : a < b) : a / b = 0 := by rw [← le_zero, div_le $ pos_iff_ne_zero.1 $ lt_of_le_of_lt (zero_le _) h]; simpa only [succ_zero, mul_one] using h @[simp] theorem mul_div_cancel (a) {b : ordinal} (b0 : b ≠ 0) : b * a / b = a := by simpa only [add_zero, zero_div] using mul_add_div a b0 0 @[simp] theorem div_one (a : ordinal) : a / 1 = a := by simpa only [one_mul] using mul_div_cancel a one_ne_zero @[simp] theorem div_self {a : ordinal} (h : a ≠ 0) : a / a = 1 := by simpa only [mul_one] using mul_div_cancel 1 h theorem mul_sub (a b c : ordinal) : a * (b - c) = a * b - a * c := if a0 : a = 0 then by simp only [a0, zero_mul, sub_self] else eq_of_forall_ge_iff $ λ d, by rw [sub_le, ← le_div a0, sub_le, ← le_div a0, mul_add_div _ a0] theorem is_limit_add_iff {a b} : is_limit (a + b) ↔ is_limit b ∨ (b = 0 ∧ is_limit a) := begin split; intro h, { by_cases h' : b = 0, { rw [h', add_zero] at h, right, exact ⟨h', h⟩ }, left, rw [←add_sub_cancel a b], apply sub_is_limit h, suffices : a + 0 < a + b, simpa only [add_zero], rwa [add_lt_add_iff_left, pos_iff_ne_zero] }, rcases h with h|⟨rfl, h⟩, exact add_is_limit a h, simpa only [add_zero] end /-- Divisibility is defined by right multiplication: `a ∣ b` if there exists `c` such that `b = a * c`. -/ instance : has_dvd ordinal := ⟨λ a b, ∃ c, b = a * c⟩ theorem dvd_def {a b : ordinal} : a ∣ b ↔ ∃ c, b = a * c := iff.rfl theorem dvd_mul (a b : ordinal) : a ∣ a * b := ⟨_, rfl⟩ theorem dvd_trans : ∀ {a b c : ordinal}, a ∣ b → b ∣ c → a ∣ c | a _ _ ⟨b, rfl⟩ ⟨c, rfl⟩ := ⟨b * c, mul_assoc _ _ _⟩ theorem dvd_mul_of_dvd {a b : ordinal} (c) (h : a ∣ b) : a ∣ b * c := dvd_trans h (dvd_mul _ _) theorem dvd_add_iff : ∀ {a b c : ordinal}, a ∣ b → (a ∣ b + c ↔ a ∣ c) | a _ c ⟨b, rfl⟩ := ⟨λ ⟨d, e⟩, ⟨d - b, by rw [mul_sub, ← e, add_sub_cancel]⟩, λ ⟨d, e⟩, by rw [e, ← mul_add]; apply dvd_mul⟩ theorem dvd_add {a b c : ordinal} (h₁ : a ∣ b) : a ∣ c → a ∣ b + c := (dvd_add_iff h₁).2 theorem dvd_zero (a : ordinal) : a ∣ 0 := ⟨_, (mul_zero _).symm⟩ theorem zero_dvd {a : ordinal} : 0 ∣ a ↔ a = 0 := ⟨λ ⟨h, e⟩, by simp only [e, zero_mul], λ e, e.symm ▸ dvd_zero _⟩ theorem one_dvd (a : ordinal) : 1 ∣ a := ⟨a, (one_mul _).symm⟩ theorem div_mul_cancel : ∀ {a b : ordinal}, a ≠ 0 → a ∣ b → a * (b / a) = b | a _ a0 ⟨b, rfl⟩ := by rw [mul_div_cancel _ a0] theorem le_of_dvd : ∀ {a b : ordinal}, b ≠ 0 → a ∣ b → a ≤ b | a _ b0 ⟨b, rfl⟩ := by simpa only [mul_one] using mul_le_mul_left a (one_le_iff_ne_zero.2 (λ h : b = 0, by simpa only [h, mul_zero] using b0)) theorem dvd_antisymm {a b : ordinal} (h₁ : a ∣ b) (h₂ : b ∣ a) : a = b := if a0 : a = 0 then by subst a; exact (zero_dvd.1 h₁).symm else if b0 : b = 0 then by subst b; exact zero_dvd.1 h₂ else le_antisymm (le_of_dvd b0 h₁) (le_of_dvd a0 h₂) /-- `a % b` is the unique ordinal `o'` satisfying `a = b * o + o'` with `o' < b`. -/ instance : has_mod ordinal := ⟨λ a b, a - b * (a / b)⟩ theorem mod_def (a b : ordinal) : a % b = a - b * (a / b) := rfl @[simp] theorem mod_zero (a : ordinal) : a % 0 = a := by simp only [mod_def, div_zero, zero_mul, sub_zero] theorem mod_eq_of_lt {a b : ordinal} (h : a < b) : a % b = a := by simp only [mod_def, div_eq_zero_of_lt h, mul_zero, sub_zero] @[simp] theorem zero_mod (b : ordinal) : 0 % b = 0 := by simp only [mod_def, zero_div, mul_zero, sub_self] theorem div_add_mod (a b : ordinal) : b * (a / b) + a % b = a := add_sub_cancel_of_le $ mul_div_le _ _ theorem mod_lt (a) {b : ordinal} (h : b ≠ 0) : a % b < b := (add_lt_add_iff_left (b * (a / b))).1 $ by rw div_add_mod; exact lt_mul_div_add a h @[simp] theorem mod_self (a : ordinal) : a % a = 0 := if a0 : a = 0 then by simp only [a0, zero_mod] else by simp only [mod_def, div_self a0, mul_one, sub_self] @[simp] theorem mod_one (a : ordinal) : a % 1 = 0 := by simp only [mod_def, div_one, one_mul, sub_self] end ordinal namespace cardinal open ordinal /-- The ordinal corresponding to a cardinal `c` is the least ordinal whose cardinal is `c`. -/ def ord (c : cardinal) : ordinal := begin let ι := λ α, {r // is_well_order α r}, have : Π α, ι α := λ α, ⟨well_ordering_rel, by apply_instance⟩, let F := λ α, ordinal.min ⟨this _⟩ (λ i:ι α, ⟦⟨α, i.1, i.2⟩⟧), refine quot.lift_on c F _, suffices : ∀ {α β}, α ≈ β → F α ≤ F β, from λ α β h, le_antisymm (this h) (this (setoid.symm h)), intros α β h, cases h with f, refine ordinal.le_min.2 (λ i, _), haveI := @order_embedding.is_well_order _ _ (f ⁻¹'o i.1) _ ↑(order_iso.preimage f i.1) i.2, rw ← show type (f ⁻¹'o i.1) = ⟦⟨β, i.1, i.2⟩⟧, from quot.sound ⟨order_iso.preimage f i.1⟩, exact ordinal.min_le (λ i:ι α, ⟦⟨α, i.1, i.2⟩⟧) ⟨_, _⟩ end -- TODO(lint): This should be a theorem but Lean fails to synthesize the placeholders @[nolint] def ord_eq_min (α : Type u) : ord (mk α) = @ordinal.min _ _ (λ i:{r // is_well_order α r}, ⟦⟨α, i.1, i.2⟩⟧) := rfl theorem ord_eq (α) : ∃ (r : α → α → Prop) [wo : is_well_order α r], ord (mk α) = @type α r wo := let ⟨⟨r, wo⟩, h⟩ := @ordinal.min_eq {r // is_well_order α r} ⟨⟨well_ordering_rel, by apply_instance⟩⟩ (λ i:{r // is_well_order α r}, ⟦⟨α, i.1, i.2⟩⟧) in ⟨r, wo, h⟩ theorem ord_le_type (r : α → α → Prop) [is_well_order α r] : ord (mk α) ≤ ordinal.type r := @ordinal.min_le {r // is_well_order α r} ⟨⟨well_ordering_rel, by apply_instance⟩⟩ (λ i:{r // is_well_order α r}, ⟦⟨α, i.1, i.2⟩⟧) ⟨r, _⟩ theorem ord_le {c o} : ord c ≤ o ↔ c ≤ o.card := quotient.induction_on c $ λ α, induction_on o $ λ β s _, let ⟨r, _, e⟩ := ord_eq α in begin resetI, simp only [mk_def, card_type], split; intro h, { rw e at h, exact let ⟨f⟩ := h in ⟨f.to_embedding⟩ }, { cases h with f, have g := order_embedding.preimage f s, haveI := order_embedding.is_well_order g, exact le_trans (ord_le_type _) (type_le'.2 ⟨g⟩) } end theorem lt_ord {c o} : o < ord c ↔ o.card < c := by rw [← not_le, ← not_le, ord_le] @[simp] theorem card_ord (c) : (ord c).card = c := quotient.induction_on c $ λ α, let ⟨r, _, e⟩ := ord_eq α in by simp only [mk_def, e, card_type] theorem ord_card_le (o : ordinal) : o.card.ord ≤ o := ord_le.2 (le_refl _) lemma lt_ord_succ_card (o : ordinal) : o < o.card.succ.ord := by { rw [lt_ord], apply cardinal.lt_succ_self } @[simp] theorem ord_le_ord {c₁ c₂} : ord c₁ ≤ ord c₂ ↔ c₁ ≤ c₂ := by simp only [ord_le, card_ord] @[simp] theorem ord_lt_ord {c₁ c₂} : ord c₁ < ord c₂ ↔ c₁ < c₂ := by simp only [lt_ord, card_ord] @[simp] theorem ord_zero : ord 0 = 0 := le_antisymm (ord_le.2 $ zero_le _) (ordinal.zero_le _) @[simp] theorem ord_nat (n : ℕ) : ord n = n := le_antisymm (ord_le.2 $ by simp only [card_nat]) $ begin induction n with n IH, { apply ordinal.zero_le }, { exact (@ordinal.succ_le n _).2 (lt_of_le_of_lt IH $ ord_lt_ord.2 $ nat_cast_lt.2 (nat.lt_succ_self n)) } end @[simp] theorem lift_ord (c) : (ord c).lift = ord (lift c) := eq_of_forall_ge_iff $ λ o, le_iff_le_iff_lt_iff_lt.2 $ begin split; intro h, { rcases ordinal.lt_lift_iff.1 h with ⟨a, e, h⟩, rwa [← e, lt_ord, ← lift_card, lift_lt, ← lt_ord] }, { rw lt_ord at h, rcases lift_down' (le_of_lt h) with ⟨o, rfl⟩, rw [← lift_card, lift_lt] at h, rwa [ordinal.lift_lt, lt_ord] } end lemma mk_ord_out (c : cardinal) : mk c.ord.out.α = c := by rw [←card_type c.ord.out.r, type_out, card_ord] lemma card_typein_lt (r : α → α → Prop) [is_well_order α r] (x : α) (h : ord (mk α) = type r) : card (typein r x) < mk α := by { rw [←ord_lt_ord, h], refine lt_of_le_of_lt (ord_card_le _) (typein_lt_type r x) } lemma card_typein_out_lt (c : cardinal) (x : c.ord.out.α) : card (typein c.ord.out.r x) < c := by { convert card_typein_lt c.ord.out.r x _, rw [mk_ord_out], rw [type_out, mk_ord_out] } lemma ord_injective : injective ord := by { intros c c' h, rw [←card_ord c, ←card_ord c', h] } def ord.order_embedding : @order_embedding cardinal ordinal (<) (<) := order_embedding.of_monotone cardinal.ord $ λ a b, cardinal.ord_lt_ord.2 @[simp] theorem ord.order_embedding_coe : (ord.order_embedding : cardinal → ordinal) = ord := rfl /-- The cardinal `univ` is the cardinality of ordinal `univ`, or equivalently the cardinal of `ordinal.{u}`, or `cardinal.{u}`, as an element of `cardinal.{v}` (when `u < v`). -/ def univ := lift.{(u+1) v} (mk ordinal) theorem univ_id : univ.{u (u+1)} = mk ordinal := lift_id _ @[simp] theorem lift_univ : lift.{_ w} univ.{u v} = univ.{u (max v w)} := lift_lift _ theorem univ_umax : univ.{u (max (u+1) v)} = univ.{u v} := congr_fun lift_umax _ theorem lift_lt_univ (c : cardinal) : lift.{u (u+1)} c < univ.{u (u+1)} := by simpa only [lift.principal_seg_coe, lift_ord, lift_succ, ord_le, succ_le] using le_of_lt (lift.principal_seg.{u (u+1)}.lt_top (succ c).ord) theorem lift_lt_univ' (c : cardinal) : lift.{u (max (u+1) v)} c < univ.{u v} := by simpa only [lift_lift, lift_univ, univ_umax] using lift_lt.{_ (max (u+1) v)}.2 (lift_lt_univ c) @[simp] theorem ord_univ : ord univ.{u v} = ordinal.univ.{u v} := le_antisymm (ord_card_le _) $ le_of_forall_lt $ λ o h, lt_ord.2 begin rcases lift.principal_seg.{u v}.down'.1 (by simpa only [lift.principal_seg_coe] using h) with ⟨o', rfl⟩, simp only [lift.principal_seg_coe], rw [← lift_card], apply lift_lt_univ' end theorem lt_univ {c} : c < univ.{u (u+1)} ↔ ∃ c', c = lift.{u (u+1)} c' := ⟨λ h, begin have := ord_lt_ord.2 h, rw ord_univ at this, cases lift.principal_seg.{u (u+1)}.down'.1 (by simpa only [lift.principal_seg_top]) with o e, have := card_ord c, rw [← e, lift.principal_seg_coe, ← lift_card] at this, exact ⟨_, this.symm⟩ end, λ ⟨c', e⟩, e.symm ▸ lift_lt_univ _⟩ theorem lt_univ' {c} : c < univ.{u v} ↔ ∃ c', c = lift.{u (max (u+1) v)} c' := ⟨λ h, let ⟨a, e, h'⟩ := lt_lift_iff.1 h in begin rw [← univ_id] at h', rcases lt_univ.{u}.1 h' with ⟨c', rfl⟩, exact ⟨c', by simp only [e.symm, lift_lift]⟩ end, λ ⟨c', e⟩, e.symm ▸ lift_lt_univ' _⟩ end cardinal namespace ordinal @[simp] theorem card_univ : card univ = cardinal.univ := rfl /-- The supremum of a family of ordinals -/ def sup {ι} (f : ι → ordinal) : ordinal := omin {c | ∀ i, f i ≤ c} ⟨(sup (cardinal.succ ∘ card ∘ f)).ord, λ i, le_of_lt $ cardinal.lt_ord.2 (lt_of_lt_of_le (cardinal.lt_succ_self _) (le_sup _ _))⟩ theorem le_sup {ι} (f : ι → ordinal) : ∀ i, f i ≤ sup f := omin_mem {c | ∀ i, f i ≤ c} _ theorem sup_le {ι} {f : ι → ordinal} {a} : sup f ≤ a ↔ ∀ i, f i ≤ a := ⟨λ h i, le_trans (le_sup _ _) h, λ h, omin_le h⟩ theorem lt_sup {ι} {f : ι → ordinal} {a} : a < sup f ↔ ∃ i, a < f i := by simpa only [not_forall, not_le] using not_congr (@sup_le _ f a) theorem is_normal.sup {f} (H : is_normal f) {ι} {g : ι → ordinal} (h : nonempty ι) : f (sup g) = sup (f ∘ g) := eq_of_forall_ge_iff $ λ a, by rw [sup_le, comp, H.le_set' (λ_:ι, true) g (let ⟨i⟩ := h in ⟨i, ⟨⟩⟩)]; intros; simp only [sup_le, true_implies_iff] theorem sup_ord {ι} (f : ι → cardinal) : sup (λ i, (f i).ord) = (cardinal.sup f).ord := eq_of_forall_ge_iff $ λ a, by simp only [sup_le, cardinal.ord_le, cardinal.sup_le] lemma sup_succ {ι} (f : ι → ordinal) : sup (λ i, succ (f i)) ≤ succ (sup f) := by { rw [ordinal.sup_le], intro i, rw ordinal.succ_le_succ, apply ordinal.le_sup } lemma unbounded_range_of_sup_ge {α β : Type u} (r : α → α → Prop) [is_well_order α r] (f : β → α) (h : sup.{u u} (typein r ∘ f) ≥ type r) : unbounded r (range f) := begin apply (not_bounded_iff _).mp, rintro ⟨x, hx⟩, apply not_lt_of_ge h, refine lt_of_le_of_lt _ (typein_lt_type r x), rw [sup_le], intro y, apply le_of_lt, rw typein_lt_typein, apply hx, apply mem_range_self end /-- The supremum of a family of ordinals indexed by the set of ordinals less than some `o : ordinal.{u}`. (This is not a special case of `sup` over the subtype, because `{a // a < o} : Type (u+1)` and `sup` only works over families in `Type u`.) -/ def bsup (o : ordinal.{u}) : (Π a < o, ordinal.{max u v}) → ordinal.{max u v} := match o, o.out, o.out_eq with | _, ⟨α, r, _⟩, rfl, f := by exactI sup (λ a, f (typein r a) (typein_lt_type _ _)) end theorem bsup_le {o f a} : bsup.{u v} o f ≤ a ↔ ∀ i h, f i h ≤ a := match o, o.out, o.out_eq, f : ∀ o w (e : ⟦w⟧ = o) (f : Π (a : ordinal.{u}), a < o → ordinal.{(max u v)}), bsup._match_1 o w e f ≤ a ↔ ∀ i h, f i h ≤ a with | _, ⟨α, r, _⟩, rfl, f := by rw [bsup._match_1, sup_le]; exactI ⟨λ H i h, by simpa only [typein_enum] using H (enum r i h), λ H b, H _ _⟩ end theorem bsup_type (r : α → α → Prop) [is_well_order α r] (f) : bsup (type r) f = sup (λ a, f (typein r a) (typein_lt_type _ _)) := eq_of_forall_ge_iff $ λ o, by rw [bsup_le, sup_le]; exact ⟨λ H b, H _ _, λ H i h, by simpa only [typein_enum] using H (enum r i h)⟩ theorem le_bsup {o} (f : Π a < o, ordinal) (i h) : f i h ≤ bsup o f := bsup_le.1 (le_refl _) _ _ theorem lt_bsup {o : ordinal} {f : Π a < o, ordinal} (hf : ∀{a a'} (ha : a < o) (ha' : a' < o), a < a' → f a ha < f a' ha') (ho : o.is_limit) (i h) : f i h < bsup o f := lt_of_lt_of_le (hf _ _ $ lt_succ_self i) (le_bsup f i.succ $ ho.2 _ h) theorem bsup_id {o} (ho : is_limit o) : bsup.{u u} o (λ x _, x) = o := begin apply le_antisymm, rw [bsup_le], intro i, apply le_of_lt, rw [←not_lt], intro h, apply lt_irrefl (bsup.{u u} o (λ x _, x)), apply lt_of_le_of_lt _ (lt_bsup _ ho _ h), refl, intros, assumption end theorem is_normal.bsup {f} (H : is_normal f) {o : ordinal} : ∀ (g : Π a < o, ordinal) (h : o ≠ 0), f (bsup o g) = bsup o (λ a h, f (g a h)) := induction_on o $ λ α r _ g h, by resetI; rw [bsup_type, H.sup (type_ne_zero_iff_nonempty.1 h), bsup_type] theorem is_normal.bsup_eq {f} (H : is_normal f) {o : ordinal} (h : is_limit o) : bsup.{u} o (λx _, f x) = f o := by { rw [←is_normal.bsup.{u u} H (λ x _, x) h.1, bsup_id h] } /-- The ordinal exponential, defined by transfinite recursion. -/ def power (a b : ordinal) : ordinal := if a = 0 then 1 - b else limit_rec_on b 1 (λ _ IH, IH * a) (λ b _, bsup.{u u} b) instance : has_pow ordinal ordinal := ⟨power⟩ local infixr ^ := @pow ordinal ordinal ordinal.has_pow theorem zero_power' (a : ordinal) : 0 ^ a = 1 - a := by simp only [pow, power, if_pos rfl] @[simp] theorem zero_power {a : ordinal} (a0 : a ≠ 0) : 0 ^ a = 0 := by rwa [zero_power', sub_eq_zero_iff_le, one_le_iff_ne_zero] @[simp] theorem power_zero (a : ordinal) : a ^ 0 = 1 := by by_cases a = 0; [simp only [pow, power, if_pos h, sub_zero], simp only [pow, power, if_neg h, limit_rec_on_zero]] @[simp] theorem power_succ (a b : ordinal) : a ^ succ b = a ^ b * a := if h : a = 0 then by subst a; simp only [zero_power (succ_ne_zero _), mul_zero] else by simp only [pow, power, limit_rec_on_succ, if_neg h] theorem power_limit {a b : ordinal} (a0 : a ≠ 0) (h : is_limit b) : a ^ b = bsup.{u u} b (λ c _, a ^ c) := by simp only [pow, power, if_neg a0]; rw limit_rec_on_limit _ _ _ _ h; refl theorem power_le_of_limit {a b c : ordinal} (a0 : a ≠ 0) (h : is_limit b) : a ^ b ≤ c ↔ ∀ b' < b, a ^ b' ≤ c := by rw [power_limit a0 h, bsup_le] theorem lt_power_of_limit {a b c : ordinal} (b0 : b ≠ 0) (h : is_limit c) : a < b ^ c ↔ ∃ c' < c, a < b ^ c' := by rw [← not_iff_not, not_exists]; simp only [not_lt, power_le_of_limit b0 h, exists_prop, not_and] @[simp] theorem power_one (a : ordinal) : a ^ 1 = a := by rw [← succ_zero, power_succ]; simp only [power_zero, one_mul] @[simp] theorem one_power (a : ordinal) : 1 ^ a = 1 := begin apply limit_rec_on a, { simp only [power_zero] }, { intros _ ih, simp only [power_succ, ih, mul_one] }, refine λ b l IH, eq_of_forall_ge_iff (λ c, _), rw [power_le_of_limit one_ne_zero l], exact ⟨λ H, by simpa only [power_zero] using H 0 l.pos, λ H b' h, by rwa IH _ h⟩, end theorem power_pos {a : ordinal} (b) (a0 : 0 < a) : 0 < a ^ b := begin have h0 : 0 < a ^ 0, {simp only [power_zero, zero_lt_one]}, apply limit_rec_on b, { exact h0 }, { intros b IH, rw [power_succ], exact mul_pos IH a0 }, { exact λ b l _, (lt_power_of_limit (pos_iff_ne_zero.1 a0) l).2 ⟨0, l.pos, h0⟩ }, end theorem power_ne_zero {a : ordinal} (b) (a0 : a ≠ 0) : a ^ b ≠ 0 := pos_iff_ne_zero.1 $ power_pos b $ pos_iff_ne_zero.2 a0 theorem power_is_normal {a : ordinal} (h : 1 < a) : is_normal ((^) a) := have a0 : 0 < a, from lt_trans zero_lt_one h, ⟨λ b, by simpa only [mul_one, power_succ] using (mul_lt_mul_iff_left (power_pos b a0)).2 h, λ b l c, power_le_of_limit (ne_of_gt a0) l⟩ theorem power_lt_power_iff_right {a b c : ordinal} (a1 : 1 < a) : a ^ b < a ^ c ↔ b < c := (power_is_normal a1).lt_iff theorem power_le_power_iff_right {a b c : ordinal} (a1 : 1 < a) : a ^ b ≤ a ^ c ↔ b ≤ c := (power_is_normal a1).le_iff theorem power_right_inj {a b c : ordinal} (a1 : 1 < a) : a ^ b = a ^ c ↔ b = c := (power_is_normal a1).inj theorem power_is_limit {a b : ordinal} (a1 : 1 < a) : is_limit b → is_limit (a ^ b) := (power_is_normal a1).is_limit theorem power_is_limit_left {a b : ordinal} (l : is_limit a) (hb : b ≠ 0) : is_limit (a ^ b) := begin rcases zero_or_succ_or_limit b with e|⟨b,rfl⟩|l', { exact absurd e hb }, { rw power_succ, exact mul_is_limit (power_pos _ l.pos) l }, { exact power_is_limit l.one_lt l' } end theorem power_le_power_right {a b c : ordinal} (h₁ : 0 < a) (h₂ : b ≤ c) : a ^ b ≤ a ^ c := begin cases lt_or_eq_of_le (one_le_iff_pos.2 h₁) with h₁ h₁, { exact (power_le_power_iff_right h₁).2 h₂ }, { subst a, simp only [one_power] } end theorem power_le_power_left {a b : ordinal} (c) (ab : a ≤ b) : a ^ c ≤ b ^ c := begin by_cases a0 : a = 0, { subst a, by_cases c0 : c = 0, { subst c, simp only [power_zero] }, { simp only [zero_power c0, zero_le] } }, { apply limit_rec_on c, { simp only [power_zero] }, { intros c IH, simpa only [power_succ] using mul_le_mul IH ab }, { exact λ c l IH, (power_le_of_limit a0 l).2 (λ b' h, le_trans (IH _ h) (power_le_power_right (lt_of_lt_of_le (pos_iff_ne_zero.2 a0) ab) (le_of_lt h))) } } end theorem le_power_self {a : ordinal} (b) (a1 : 1 < a) : b ≤ a ^ b := (power_is_normal a1).le_self _ theorem power_lt_power_left_of_succ {a b c : ordinal} (ab : a < b) : a ^ succ c < b ^ succ c := by rw [power_succ, power_succ]; exact lt_of_le_of_lt (mul_le_mul_right _ $ power_le_power_left _ $ le_of_lt ab) (mul_lt_mul_of_pos_left ab (power_pos _ (lt_of_le_of_lt (zero_le _) ab))) theorem power_add (a b c : ordinal) : a ^ (b + c) = a ^ b * a ^ c := begin by_cases a0 : a = 0, { subst a, by_cases c0 : c = 0, {simp only [c0, add_zero, power_zero, mul_one]}, have : b+c ≠ 0 := ne_of_gt (lt_of_lt_of_le (pos_iff_ne_zero.2 c0) (le_add_left _ _)), simp only [zero_power c0, zero_power this, mul_zero] }, cases eq_or_lt_of_le (one_le_iff_ne_zero.2 a0) with a1 a1, { subst a1, simp only [one_power, mul_one] }, apply limit_rec_on c, { simp only [add_zero, power_zero, mul_one] }, { intros c IH, rw [add_succ, power_succ, IH, power_succ, mul_assoc] }, { intros c l IH, refine eq_of_forall_ge_iff (λ d, (((power_is_normal a1).trans (add_is_normal b)).limit_le l).trans _), simp only [IH] {contextual := tt}, exact (((mul_is_normal $ power_pos b (pos_iff_ne_zero.2 a0)).trans (power_is_normal a1)).limit_le l).symm } end theorem power_dvd_power (a) {b c : ordinal} (h : b ≤ c) : a ^ b ∣ a ^ c := by rw [← add_sub_cancel_of_le h, power_add]; apply dvd_mul theorem power_dvd_power_iff {a b c : ordinal} (a1 : 1 < a) : a ^ b ∣ a ^ c ↔ b ≤ c := ⟨λ h, le_of_not_lt $ λ hn, not_le_of_lt ((power_lt_power_iff_right a1).2 hn) $ le_of_dvd (power_ne_zero _ $ one_le_iff_ne_zero.1 $ le_of_lt a1) h, power_dvd_power _⟩ theorem power_mul (a b c : ordinal) : a ^ (b * c) = (a ^ b) ^ c := begin by_cases b0 : b = 0, {simp only [b0, zero_mul, power_zero, one_power]}, by_cases a0 : a = 0, { subst a, by_cases c0 : c = 0, {simp only [c0, mul_zero, power_zero]}, simp only [zero_power b0, zero_power c0, zero_power (mul_ne_zero b0 c0)] }, cases eq_or_lt_of_le (one_le_iff_ne_zero.2 a0) with a1 a1, { subst a1, simp only [one_power] }, apply limit_rec_on c, { simp only [mul_zero, power_zero] }, { intros c IH, rw [mul_succ, power_add, IH, power_succ] }, { intros c l IH, refine eq_of_forall_ge_iff (λ d, (((power_is_normal a1).trans (mul_is_normal (pos_iff_ne_zero.2 b0))).limit_le l).trans _), simp only [IH] {contextual := tt}, exact (power_le_of_limit (power_ne_zero _ a0) l).symm } end /-- The ordinal logarithm is the solution `u` to the equation `x = b ^ u * v + w` where `v < b` and `w < b`. -/ def log (b : ordinal) (x : ordinal) : ordinal := if h : 1 < b then pred $ omin {o | x < b^o} ⟨succ x, succ_le.1 (le_power_self _ h)⟩ else 0 @[simp] theorem log_not_one_lt {b : ordinal} (b1 : ¬ 1 < b) (x : ordinal) : log b x = 0 := by simp only [log, dif_neg b1] theorem log_def {b : ordinal} (b1 : 1 < b) (x : ordinal) : log b x = pred (omin {o | x < b^o} (log._proof_1 b x b1)) := by simp only [log, dif_pos b1] @[simp] theorem log_zero (b : ordinal) : log b 0 = 0 := if b1 : 1 < b then by rw [log_def b1, ← le_zero, pred_le]; apply omin_le; change 0<b^succ 0; rw [succ_zero, power_one]; exact lt_trans zero_lt_one b1 else by simp only [log_not_one_lt b1] theorem succ_log_def {b x : ordinal} (b1 : 1 < b) (x0 : 0 < x) : succ (log b x) = omin {o | x < b^o} (log._proof_1 b x b1) := begin let t := omin {o | x < b^o} (log._proof_1 b x b1), have : x < b ^ t := omin_mem {o | x < b^o} _, rcases zero_or_succ_or_limit t with h|h|h, { refine (not_lt_of_le (one_le_iff_pos.2 x0) _).elim, simpa only [h, power_zero] }, { rw [show log b x = pred t, from log_def b1 x, succ_pred_iff_is_succ.2 h] }, { rcases (lt_power_of_limit (ne_of_gt $ lt_trans zero_lt_one b1) h).1 this with ⟨a, h₁, h₂⟩, exact (not_le_of_lt h₁).elim (le_omin.1 (le_refl t) a h₂) } end theorem lt_power_succ_log {b : ordinal} (b1 : 1 < b) (x : ordinal) : x < b ^ succ (log b x) := begin cases lt_or_eq_of_le (zero_le x) with x0 x0, { rw [succ_log_def b1 x0], exact omin_mem {o | x < b^o} _ }, { subst x, apply power_pos _ (lt_trans zero_lt_one b1) } end theorem power_log_le (b) {x : ordinal} (x0 : 0 < x) : b ^ log b x ≤ x := begin by_cases b0 : b = 0, { rw [b0, zero_power'], refine le_trans (sub_le_self _ _) (one_le_iff_pos.2 x0) }, cases lt_or_eq_of_le (one_le_iff_ne_zero.2 b0) with b1 b1, { refine le_of_not_lt (λ h, not_le_of_lt (lt_succ_self (log b x)) _), have := @omin_le {o | x < b^o} _ _ h, rwa ← succ_log_def b1 x0 at this }, { rw [← b1, one_power], exact one_le_iff_pos.2 x0 } end theorem le_log {b x c : ordinal} (b1 : 1 < b) (x0 : 0 < x) : c ≤ log b x ↔ b ^ c ≤ x := ⟨λ h, le_trans ((power_le_power_iff_right b1).2 h) (power_log_le b x0), λ h, le_of_not_lt $ λ hn, not_le_of_lt (lt_power_succ_log b1 x) $ le_trans ((power_le_power_iff_right b1).2 (succ_le.2 hn)) h⟩ theorem log_lt {b x c : ordinal} (b1 : 1 < b) (x0 : 0 < x) : log b x < c ↔ x < b ^ c := lt_iff_lt_of_le_iff_le (le_log b1 x0) theorem log_le_log (b) {x y : ordinal} (xy : x ≤ y) : log b x ≤ log b y := if x0 : x = 0 then by simp only [x0, log_zero, zero_le] else have x0 : 0 < x, from pos_iff_ne_zero.2 x0, if b1 : 1 < b then (le_log b1 (lt_of_lt_of_le x0 xy)).2 $ le_trans (power_log_le _ x0) xy else by simp only [log_not_one_lt b1, zero_le] theorem log_le_self (b x : ordinal) : log b x ≤ x := if x0 : x = 0 then by simp only [x0, log_zero, zero_le] else if b1 : 1 < b then le_trans (le_power_self _ b1) (power_log_le b (pos_iff_ne_zero.2 x0)) else by simp only [log_not_one_lt b1, zero_le] @[simp] theorem nat_cast_mul {m n : ℕ} : ((m * n : ℕ) : ordinal) = m * n := by induction n with n IH; [simp only [nat.cast_zero, nat.mul_zero, mul_zero], rw [nat.mul_succ, nat.cast_add, IH, nat.cast_succ, mul_add_one]] @[simp] theorem nat_cast_power {m n : ℕ} : ((pow m n : ℕ) : ordinal) = m ^ n := by induction n with n IH; [simp only [nat.pow_zero, nat.cast_zero, power_zero, nat.cast_one], rw [nat.pow_succ, nat_cast_mul, IH, nat.cast_succ, ← succ_eq_add_one, power_succ]] @[simp] theorem nat_cast_le {m n : ℕ} : (m : ordinal) ≤ n ↔ m ≤ n := by rw [← cardinal.ord_nat, ← cardinal.ord_nat, cardinal.ord_le_ord, cardinal.nat_cast_le] @[simp] theorem nat_cast_lt {m n : ℕ} : (m : ordinal) < n ↔ m < n := by simp only [lt_iff_le_not_le, nat_cast_le] @[simp] theorem nat_cast_inj {m n : ℕ} : (m : ordinal) = n ↔ m = n := by simp only [le_antisymm_iff, nat_cast_le] @[simp] theorem nat_cast_eq_zero {n : ℕ} : (n : ordinal) = 0 ↔ n = 0 := @nat_cast_inj n 0 @[simp] theorem nat_cast_ne_zero {n : ℕ} : (n : ordinal) ≠ 0 ↔ n ≠ 0 := not_congr nat_cast_eq_zero @[simp] theorem nat_cast_pos {n : ℕ} : (0 : ordinal) < n ↔ 0 < n := @nat_cast_lt 0 n @[simp] theorem nat_cast_sub {m n : ℕ} : ((m - n : ℕ) : ordinal) = m - n := (_root_.le_total m n).elim (λ h, by rw [nat.sub_eq_zero_iff_le.2 h, sub_eq_zero_iff_le.2 (nat_cast_le.2 h)]; refl) (λ h, (add_left_cancel n).1 $ by rw [← nat.cast_add, nat.add_sub_cancel' h, add_sub_cancel_of_le (nat_cast_le.2 h)]) @[simp] theorem nat_cast_div {m n : ℕ} : ((m / n : ℕ) : ordinal) = m / n := if n0 : n = 0 then by simp only [n0, nat.div_zero, nat.cast_zero, div_zero] else have n0':_, from nat_cast_ne_zero.2 n0, le_antisymm (by rw [le_div n0', ← nat_cast_mul, nat_cast_le, mul_comm]; apply nat.div_mul_le_self) (by rw [div_le n0', succ, ← nat.cast_succ, ← nat_cast_mul, nat_cast_lt, mul_comm, ← nat.div_lt_iff_lt_mul _ _ (nat.pos_of_ne_zero n0)]; apply nat.lt_succ_self) @[simp] theorem nat_cast_mod {m n : ℕ} : ((m % n : ℕ) : ordinal) = m % n := by rw [← add_left_cancel (n*(m/n)), div_add_mod, ← nat_cast_div, ← nat_cast_mul, ← nat.cast_add, add_comm, nat.mod_add_div] @[simp] theorem nat_le_card {o} {n : ℕ} : (n : cardinal) ≤ card o ↔ (n : ordinal) ≤ o := ⟨λ h, by rwa [← cardinal.ord_le, cardinal.ord_nat] at h, λ h, card_nat n ▸ card_le_card h⟩ @[simp] theorem nat_lt_card {o} {n : ℕ} : (n : cardinal) < card o ↔ (n : ordinal) < o := by rw [← succ_le, ← cardinal.succ_le, cardinal.nat_succ, nat_le_card]; refl @[simp] theorem card_lt_nat {o} {n : ℕ} : card o < n ↔ o < n := lt_iff_lt_of_le_iff_le nat_le_card @[simp] theorem card_le_nat {o} {n : ℕ} : card o ≤ n ↔ o ≤ n := le_iff_le_iff_lt_iff_lt.2 nat_lt_card @[simp] theorem card_eq_nat {o} {n : ℕ} : card o = n ↔ o = n := by simp only [le_antisymm_iff, card_le_nat, nat_le_card] @[simp] theorem type_fin (n : ℕ) : @type (fin n) (<) _ = n := by rw [← card_eq_nat, card_type, mk_fin] @[simp] theorem lift_nat_cast (n : ℕ) : lift n = n := by induction n with n ih; [simp only [nat.cast_zero, lift_zero], simp only [nat.cast_succ, lift_add, ih, lift_one]] theorem lift_type_fin (n : ℕ) : lift (@type (fin n) (<) _) = n := by simp only [type_fin, lift_nat_cast] theorem fintype_card (r : α → α → Prop) [is_well_order α r] [fintype α] : type r = fintype.card α := by rw [← card_eq_nat, card_type, fintype_card] end ordinal namespace cardinal open ordinal @[simp] theorem ord_omega : ord.{u} omega = ordinal.omega := le_antisymm (ord_le.2 $ le_refl _) $ le_of_forall_lt $ λ o h, begin rcases ordinal.lt_lift_iff.1 h with ⟨o, rfl, h'⟩, rw [lt_ord, ← lift_card, ← lift_omega.{0 u}, lift_lt, ← typein_enum (<) h'], exact lt_omega_iff_fintype.2 ⟨set.fintype_lt_nat _⟩ end @[simp] theorem add_one_of_omega_le {c} (h : omega ≤ c) : c + 1 = c := by rw [add_comm, ← card_ord c, ← card_one, ← card_add, one_add_of_omega_le]; rwa [← ord_omega, ord_le_ord] end cardinal namespace ordinal theorem lt_omega {o : ordinal.{u}} : o < omega ↔ ∃ n : ℕ, o = n := by rw [← cardinal.ord_omega, cardinal.lt_ord, lt_omega]; simp only [card_eq_nat] theorem nat_lt_omega (n : ℕ) : (n : ordinal) < omega := lt_omega.2 ⟨_, rfl⟩ theorem omega_pos : 0 < omega := nat_lt_omega 0 theorem omega_ne_zero : omega ≠ 0 := ne_of_gt omega_pos theorem one_lt_omega : 1 < omega := by simpa only [nat.cast_one] using nat_lt_omega 1 theorem omega_is_limit : is_limit omega := ⟨omega_ne_zero, λ o h, let ⟨n, e⟩ := lt_omega.1 h in by rw [e]; exact nat_lt_omega (n+1)⟩ theorem omega_le {o : ordinal.{u}} : omega ≤ o ↔ ∀ n : ℕ, (n : ordinal) ≤ o := ⟨λ h n, le_trans (le_of_lt (nat_lt_omega _)) h, λ H, le_of_forall_lt $ λ a h, let ⟨n, e⟩ := lt_omega.1 h in by rw [e, ← succ_le]; exact H (n+1)⟩ theorem nat_lt_limit {o} (h : is_limit o) : ∀ n : ℕ, (n : ordinal) < o | 0 := lt_of_le_of_ne (zero_le o) h.1.symm | (n+1) := h.2 _ (nat_lt_limit n) theorem omega_le_of_is_limit {o} (h : is_limit o) : omega ≤ o := omega_le.2 $ λ n, le_of_lt $ nat_lt_limit h n theorem add_omega {a : ordinal} (h : a < omega) : a + omega = omega := begin rcases lt_omega.1 h with ⟨n, rfl⟩, clear h, induction n with n IH, { rw [nat.cast_zero, zero_add] }, { rw [nat.cast_succ, add_assoc, one_add_of_omega_le (le_refl _), IH] } end theorem add_lt_omega {a b : ordinal} (ha : a < omega) (hb : b < omega) : a + b < omega := match a, b, lt_omega.1 ha, lt_omega.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat.cast_add]; apply nat_lt_omega end theorem mul_lt_omega {a b : ordinal} (ha : a < omega) (hb : b < omega) : a * b < omega := match a, b, lt_omega.1 ha, lt_omega.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat_cast_mul]; apply nat_lt_omega end theorem is_limit_iff_omega_dvd {a : ordinal} : is_limit a ↔ a ≠ 0 ∧ omega ∣ a := begin refine ⟨λ l, ⟨l.1, ⟨a / omega, le_antisymm _ (mul_div_le _ _)⟩⟩, λ h, _⟩, { refine (limit_le l).2 (λ x hx, le_of_lt _), rw [← div_lt omega_ne_zero, ← succ_le, le_div omega_ne_zero, mul_succ, add_le_of_limit omega_is_limit], intros b hb, rcases lt_omega.1 hb with ⟨n, rfl⟩, exact le_trans (add_le_add_right (mul_div_le _ _) _) (le_of_lt $ lt_sub.1 $ nat_lt_limit (sub_is_limit l hx) _) }, { rcases h with ⟨a0, b, rfl⟩, refine mul_is_limit_left omega_is_limit (pos_iff_ne_zero.2 $ mt _ a0), intro e, simp only [e, mul_zero] } end local infixr ^ := @pow ordinal ordinal ordinal.has_pow theorem power_lt_omega {a b : ordinal} (ha : a < omega) (hb : b < omega) : a ^ b < omega := match a, b, lt_omega.1 ha, lt_omega.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat_cast_power]; apply nat_lt_omega end theorem add_omega_power {a b : ordinal} (h : a < omega ^ b) : a + omega ^ b = omega ^ b := begin refine le_antisymm _ (le_add_left _ _), revert h, apply limit_rec_on b, { intro h, rw [power_zero, ← succ_zero, lt_succ, le_zero] at h, rw [h, zero_add] }, { intros b _ h, rw [power_succ] at h, rcases (lt_mul_of_limit omega_is_limit).1 h with ⟨x, xo, ax⟩, refine le_trans (add_le_add_right (le_of_lt ax) _) _, rw [power_succ, ← mul_add, add_omega xo] }, { intros b l IH h, rcases (lt_power_of_limit omega_ne_zero l).1 h with ⟨x, xb, ax⟩, refine (((add_is_normal a).trans (power_is_normal one_lt_omega)) .limit_le l).2 (λ y yb, _), let z := max x y, have := IH z (max_lt xb yb) (lt_of_lt_of_le ax $ power_le_power_right omega_pos (le_max_left _ _)), exact le_trans (add_le_add_left (power_le_power_right omega_pos (le_max_right _ _)) _) (le_trans this (power_le_power_right omega_pos $ le_of_lt $ max_lt xb yb)) } end theorem add_lt_omega_power {a b c : ordinal} (h₁ : a < omega ^ c) (h₂ : b < omega ^ c) : a + b < omega ^ c := by rwa [← add_omega_power h₁, add_lt_add_iff_left] theorem add_absorp {a b c : ordinal} (h₁ : a < omega ^ b) (h₂ : omega ^ b ≤ c) : a + c = c := by rw [← add_sub_cancel_of_le h₂, ← add_assoc, add_omega_power h₁] theorem add_absorp_iff {o : ordinal} (o0 : o > 0) : (∀ a < o, a + o = o) ↔ ∃ a, o = omega ^ a := ⟨λ H, ⟨log omega o, begin refine ((lt_or_eq_of_le (power_log_le _ o0)) .resolve_left $ λ h, _).symm, have := H _ h, have := lt_power_succ_log one_lt_omega o, rw [power_succ, lt_mul_of_limit omega_is_limit] at this, rcases this with ⟨a, ao, h'⟩, rcases lt_omega.1 ao with ⟨n, rfl⟩, clear ao, revert h', apply not_lt_of_le, suffices e : omega ^ log omega o * ↑n + o = o, { simpa only [e] using le_add_right (omega ^ log omega o * ↑n) o }, induction n with n IH, {simp only [nat.cast_zero, mul_zero, zero_add]}, simp only [nat.cast_succ, mul_add_one, add_assoc, this, IH] end⟩, λ ⟨b, e⟩, e.symm ▸ λ a, add_omega_power⟩ theorem add_mul_limit_aux {a b c : ordinal} (ba : b + a = a) (l : is_limit c) (IH : ∀ c' < c, (a + b) * succ c' = a * succ c' + b) : (a + b) * c = a * c := le_antisymm ((mul_le_of_limit l).2 $ λ c' h, begin apply le_trans (mul_le_mul_left _ (le_of_lt $ lt_succ_self _)), rw IH _ h, apply le_trans (add_le_add_left _ _), { rw ← mul_succ, exact mul_le_mul_left _ (succ_le.2 $ l.2 _ h) }, { rw ← ba, exact le_add_right _ _ } end) (mul_le_mul_right _ (le_add_right _ _)) theorem add_mul_succ {a b : ordinal} (c) (ba : b + a = a) : (a + b) * succ c = a * succ c + b := begin apply limit_rec_on c, { simp only [succ_zero, mul_one] }, { intros c IH, rw [mul_succ, IH, ← add_assoc, add_assoc _ b, ba, ← mul_succ] }, { intros c l IH, have := add_mul_limit_aux ba l IH, rw [mul_succ, add_mul_limit_aux ba l IH, mul_succ, add_assoc] } end theorem add_mul_limit {a b c : ordinal} (ba : b + a = a) (l : is_limit c) : (a + b) * c = a * c := add_mul_limit_aux ba l (λ c' _, add_mul_succ c' ba) theorem mul_omega {a : ordinal} (a0 : 0 < a) (ha : a < omega) : a * omega = omega := le_antisymm ((mul_le_of_limit omega_is_limit).2 $ λ b hb, le_of_lt (mul_lt_omega ha hb)) (by simpa only [one_mul] using mul_le_mul_right omega (one_le_iff_pos.2 a0)) theorem mul_lt_omega_power {a b c : ordinal} (c0 : 0 < c) (ha : a < omega ^ c) (hb : b < omega) : a * b < omega ^ c := if b0 : b = 0 then by simp only [b0, mul_zero, power_pos _ omega_pos] else begin rcases zero_or_succ_or_limit c with rfl|⟨c,rfl⟩|l, { exact (lt_irrefl _).elim c0 }, { rw power_succ at ha, rcases ((mul_is_normal $ power_pos _ omega_pos).limit_lt omega_is_limit).1 ha with ⟨n, hn, an⟩, refine lt_of_le_of_lt (mul_le_mul_right _ (le_of_lt an)) _, rw [power_succ, mul_assoc, mul_lt_mul_iff_left (power_pos _ omega_pos)], exact mul_lt_omega hn hb }, { rcases ((power_is_normal one_lt_omega).limit_lt l).1 ha with ⟨x, hx, ax⟩, refine lt_of_le_of_lt (mul_le_mul (le_of_lt ax) (le_of_lt hb)) _, rw [← power_succ, power_lt_power_iff_right one_lt_omega], exact l.2 _ hx } end theorem mul_omega_dvd {a : ordinal} (a0 : 0 < a) (ha : a < omega) : ∀ {b}, omega ∣ b → a * b = b | _ ⟨b, rfl⟩ := by rw [← mul_assoc, mul_omega a0 ha] theorem mul_omega_power_power {a b : ordinal} (a0 : 0 < a) (h : a < omega ^ omega ^ b) : a * omega ^ omega ^ b = omega ^ omega ^ b := begin by_cases b0 : b = 0, {rw [b0, power_zero, power_one] at h ⊢, exact mul_omega a0 h}, refine le_antisymm _ (by simpa only [one_mul] using mul_le_mul_right (omega^omega^b) (one_le_iff_pos.2 a0)), rcases (lt_power_of_limit omega_ne_zero (power_is_limit_left omega_is_limit b0)).1 h with ⟨x, xb, ax⟩, refine le_trans (mul_le_mul_right _ (le_of_lt ax)) _, rw [← power_add, add_omega_power xb] end theorem power_omega {a : ordinal} (a1 : 1 < a) (h : a < omega) : a ^ omega = omega := le_antisymm ((power_le_of_limit (one_le_iff_ne_zero.1 $ le_of_lt a1) omega_is_limit).2 (λ b hb, le_of_lt (power_lt_omega h hb))) (le_power_self _ a1) theorem CNF_aux {b o : ordinal} (b0 : b ≠ 0) (o0 : o ≠ 0) : o % b ^ log b o < o := lt_of_lt_of_le (mod_lt _ $ power_ne_zero _ b0) (power_log_le _ $ pos_iff_ne_zero.2 o0) @[elab_as_eliminator] noncomputable def CNF_rec {b : ordinal} (b0 : b ≠ 0) {C : ordinal → Sort*} (H0 : C 0) (H : ∀ o, o ≠ 0 → o % b ^ log b o < o → C (o % b ^ log b o) → C o) : ∀ o, C o | o := if o0 : o = 0 then by rw o0; exact H0 else have _, from CNF_aux b0 o0, H o o0 this (CNF_rec (o % b ^ log b o)) using_well_founded {dec_tac := `[assumption]} @[simp] theorem CNF_rec_zero {b} (b0) {C H0 H} : @CNF_rec b b0 C H0 H 0 = H0 := by rw [CNF_rec, dif_pos rfl]; refl @[simp] theorem CNF_rec_ne_zero {b} (b0) {C H0 H o} (o0) : @CNF_rec b b0 C H0 H o = H o o0 (CNF_aux b0 o0) (@CNF_rec b b0 C H0 H _) := by rw [CNF_rec, dif_neg o0] /-- The Cantor normal form of an ordinal is the list of coefficients in the base-`b` expansion of `o`. CNF b (b ^ u₁ * v₁ + b ^ u₂ * v₂) = [(u₁, v₁), (u₂, v₂)] -/ def CNF (b := omega) (o : ordinal) : list (ordinal × ordinal) := if b0 : b = 0 then [] else CNF_rec b0 [] (λ o o0 h IH, (log b o, o / b ^ log b o) :: IH) o @[simp] theorem zero_CNF (o) : CNF 0 o = [] := dif_pos rfl @[simp] theorem CNF_zero (b) : CNF b 0 = [] := if b0 : b = 0 then dif_pos b0 else (dif_neg b0).trans $ CNF_rec_zero _ theorem CNF_ne_zero {b o : ordinal} (b0 : b ≠ 0) (o0 : o ≠ 0) : CNF b o = (log b o, o / b ^ log b o) :: CNF b (o % b ^ log b o) := by unfold CNF; rw [dif_neg b0, dif_neg b0, CNF_rec_ne_zero b0 o0] theorem one_CNF {o : ordinal} (o0 : o ≠ 0) : CNF 1 o = [(0, o)] := by rw [CNF_ne_zero one_ne_zero o0, log_not_one_lt (lt_irrefl _), power_zero, mod_one, CNF_zero, div_one] theorem CNF_foldr {b : ordinal} (b0 : b ≠ 0) (o) : (CNF b o).foldr (λ p r, b ^ p.1 * p.2 + r) 0 = o := CNF_rec b0 (by rw CNF_zero; refl) (λ o o0 h IH, by rw [CNF_ne_zero b0 o0, list.foldr_cons, IH, div_add_mod]) o theorem CNF_pairwise_aux (b := omega) (o) : (∀ p ∈ CNF b o, prod.fst p ≤ log b o) ∧ (CNF b o).pairwise (λ p q, q.1 < p.1) := begin by_cases b0 : b = 0, { simp only [b0, zero_CNF, list.pairwise.nil, and_true], exact λ _, false.elim }, cases lt_or_eq_of_le (one_le_iff_ne_zero.2 b0) with b1 b1, { refine CNF_rec b0 _ _ o, { simp only [CNF_zero, list.pairwise.nil, and_true], exact λ _, false.elim }, intros o o0 H IH, cases IH with IH₁ IH₂, simp only [CNF_ne_zero b0 o0, list.forall_mem_cons, list.pairwise_cons, IH₂, and_true], refine ⟨⟨le_refl _, λ p m, _⟩, λ p m, _⟩, { exact le_trans (IH₁ p m) (log_le_log _ $ le_of_lt H) }, { refine lt_of_le_of_lt (IH₁ p m) ((log_lt b1 _).2 _), { rw pos_iff_ne_zero, intro e, rw e at m, simpa only [CNF_zero] using m }, { exact mod_lt _ (power_ne_zero _ b0) } } }, { by_cases o0 : o = 0, { simp only [o0, CNF_zero, list.pairwise.nil, and_true], exact λ _, false.elim }, rw [← b1, one_CNF o0], simp only [list.mem_singleton, log_not_one_lt (lt_irrefl _), forall_eq, le_refl, true_and, list.pairwise_singleton] } end theorem CNF_pairwise (b := omega) (o) : (CNF b o).pairwise (λ p q, prod.fst q < p.1) := (CNF_pairwise_aux _ _).2 theorem CNF_fst_le_log (b := omega) (o) : ∀ p ∈ CNF b o, prod.fst p ≤ log b o := (CNF_pairwise_aux _ _).1 theorem CNF_fst_le (b := omega) (o) (p ∈ CNF b o) : prod.fst p ≤ o := le_trans (CNF_fst_le_log _ _ p H) (log_le_self _ _) theorem CNF_snd_lt {b : ordinal} (b1 : 1 < b) (o) : ∀ p ∈ CNF b o, prod.snd p < b := begin have b0 := ne_of_gt (lt_trans zero_lt_one b1), refine CNF_rec b0 (λ _, by rw [CNF_zero]; exact false.elim) _ o, intros o o0 H IH, simp only [CNF_ne_zero b0 o0, list.mem_cons_iff, list.forall_mem_cons', iff_true_intro IH, and_true], rw [div_lt (power_ne_zero _ b0), ← power_succ], exact lt_power_succ_log b1 _, end theorem CNF_sorted (b := omega) (o) : ((CNF b o).map prod.fst).sorted (>) := by rw [list.sorted, list.pairwise_map]; exact CNF_pairwise b o /-- The next fixed point function, the least fixed point of the normal function `f` above `a`. -/ def nfp (f : ordinal → ordinal) (a : ordinal) := sup (λ n : ℕ, f^[n] a) theorem iterate_le_nfp (f a n) : f^[n] a ≤ nfp f a := le_sup _ n theorem le_nfp_self (f a) : a ≤ nfp f a := iterate_le_nfp f a 0 theorem is_normal.lt_nfp {f} (H : is_normal f) {a b} : f b < nfp f a ↔ b < nfp f a := lt_sup.trans $ iff.trans (by exact ⟨λ ⟨n, h⟩, ⟨n, lt_of_le_of_lt (H.le_self _) h⟩, λ ⟨n, h⟩, ⟨n+1, by rw nat.iterate_succ'; exact H.lt_iff.2 h⟩⟩) lt_sup.symm theorem is_normal.nfp_le {f} (H : is_normal f) {a b} : nfp f a ≤ f b ↔ nfp f a ≤ b := le_iff_le_iff_lt_iff_lt.2 H.lt_nfp theorem is_normal.nfp_le_fp {f} (H : is_normal f) {a b} (ab : a ≤ b) (h : f b ≤ b) : nfp f a ≤ b := sup_le.2 $ λ i, begin induction i with i IH generalizing a, {exact ab}, exact IH (le_trans (H.le_iff.2 ab) h), end theorem is_normal.nfp_fp {f} (H : is_normal f) (a) : f (nfp f a) = nfp f a := begin refine le_antisymm _ (H.le_self _), cases le_or_lt (f a) a with aa aa, { rwa le_antisymm (H.nfp_le_fp (le_refl _) aa) (le_nfp_self _ _) }, rcases zero_or_succ_or_limit (nfp f a) with e|⟨b, e⟩|l, { refine @le_trans _ _ _ (f a) _ (H.le_iff.2 _) (iterate_le_nfp f a 1), simp only [e, zero_le] }, { have : f b < nfp f a := H.lt_nfp.2 (by simp only [e, lt_succ_self]), rw [e, lt_succ] at this, have ab : a ≤ b, { rw [← lt_succ, ← e], exact lt_of_lt_of_le aa (iterate_le_nfp f a 1) }, refine le_trans (H.le_iff.2 (H.nfp_le_fp ab this)) (le_trans this (le_of_lt _)), simp only [e, lt_succ_self] }, { exact (H.2 _ l _).2 (λ b h, le_of_lt (H.lt_nfp.2 h)) } end theorem is_normal.le_nfp {f} (H : is_normal f) {a b} : f b ≤ nfp f a ↔ b ≤ nfp f a := ⟨le_trans (H.le_self _), λ h, by simpa only [H.nfp_fp] using H.le_iff.2 h⟩ theorem nfp_eq_self {f : ordinal → ordinal} {a} (h : f a = a) : nfp f a = a := le_antisymm (sup_le.mpr $ λ i, by rw [nat.iterate₀ h]) (le_nfp_self f a) /-- The derivative of a normal function `f` is the sequence of fixed points of `f`. -/ def deriv (f : ordinal → ordinal) (o : ordinal) : ordinal := limit_rec_on o (nfp f 0) (λ a IH, nfp f (succ IH)) (λ a l, bsup.{u u} a) @[simp] theorem deriv_zero (f) : deriv f 0 = nfp f 0 := limit_rec_on_zero _ _ _ @[simp] theorem deriv_succ (f o) : deriv f (succ o) = nfp f (succ (deriv f o)) := limit_rec_on_succ _ _ _ _ theorem deriv_limit (f) {o} : is_limit o → deriv f o = bsup.{u u} o (λ a _, deriv f a) := limit_rec_on_limit _ _ _ _ theorem deriv_is_normal (f) : is_normal (deriv f) := ⟨λ o, by rw [deriv_succ, ← succ_le]; apply le_nfp_self, λ o l a, by rw [deriv_limit _ l, bsup_le]⟩ theorem is_normal.deriv_fp {f} (H : is_normal f) (o) : f (deriv.{u} f o) = deriv f o := begin apply limit_rec_on o, { rw [deriv_zero, H.nfp_fp] }, { intros o ih, rw [deriv_succ, H.nfp_fp] }, intros o l IH, rw [deriv_limit _ l, is_normal.bsup.{u u u} H _ l.1], refine eq_of_forall_ge_iff (λ c, _), simp only [bsup_le, IH] {contextual:=tt} end theorem is_normal.fp_iff_deriv {f} (H : is_normal f) {a} : f a ≤ a ↔ ∃ o, a = deriv f o := ⟨λ ha, begin suffices : ∀ o (_:a ≤ deriv f o), ∃ o, a = deriv f o, from this a ((deriv_is_normal _).le_self _), intro o, apply limit_rec_on o, { intros h₁, refine ⟨0, le_antisymm h₁ _⟩, rw deriv_zero, exact H.nfp_le_fp (zero_le _) ha }, { intros o IH h₁, cases le_or_lt a (deriv f o), {exact IH h}, refine ⟨succ o, le_antisymm h₁ _⟩, rw deriv_succ, exact H.nfp_le_fp (succ_le.2 h) ha }, { intros o l IH h₁, cases eq_or_lt_of_le h₁, {exact ⟨_, h⟩}, rw [deriv_limit _ l, ← not_le, bsup_le, not_ball] at h, exact let ⟨o', h, hl⟩ := h in IH o' h (le_of_not_le hl) } end, λ ⟨o, e⟩, e.symm ▸ le_of_eq (H.deriv_fp _)⟩ end ordinal namespace cardinal section using_ordinals open ordinal theorem ord_is_limit {c} (co : omega ≤ c) : (ord c).is_limit := begin refine ⟨λ h, omega_ne_zero _, λ a, lt_imp_lt_of_le_imp_le _⟩, { rw [← ordinal.le_zero, ord_le] at h, simpa only [card_zero, le_zero] using le_trans co h }, { intro h, rw [ord_le] at h ⊢, rwa [← @add_one_of_omega_le (card a), ← card_succ], rw [← ord_le, ← le_succ_of_is_limit, ord_le], { exact le_trans co h }, { rw ord_omega, exact omega_is_limit } } end def aleph_idx.initial_seg : @initial_seg cardinal ordinal (<) (<) := @order_embedding.collapse cardinal ordinal (<) (<) _ cardinal.ord.order_embedding /-- The `aleph'` index function, which gives the ordinal index of a cardinal. (The `aleph'` part is because unlike `aleph` this counts also the finite stages. So `aleph_idx n = n`, `aleph_idx ω = ω`, `aleph_idx ℵ₁ = ω + 1` and so on.) -/ def aleph_idx : cardinal → ordinal := aleph_idx.initial_seg @[simp] theorem aleph_idx.initial_seg_coe : (aleph_idx.initial_seg : cardinal → ordinal) = aleph_idx := rfl @[simp] theorem aleph_idx_lt {a b} : aleph_idx a < aleph_idx b ↔ a < b := aleph_idx.initial_seg.to_order_embedding.ord'.symm @[simp] theorem aleph_idx_le {a b} : aleph_idx a ≤ aleph_idx b ↔ a ≤ b := by rw [← not_lt, ← not_lt, aleph_idx_lt] theorem aleph_idx.init {a b} : b < aleph_idx a → ∃ c, aleph_idx c = b := aleph_idx.initial_seg.init _ _ def aleph_idx.order_iso : @order_iso cardinal.{u} ordinal.{u} (<) (<) := @order_iso.of_surjective cardinal.{u} ordinal.{u} (<) (<) aleph_idx.initial_seg.{u} $ (initial_seg.eq_or_principal aleph_idx.initial_seg.{u}).resolve_right $ λ ⟨o, e⟩, begin have : ∀ c, aleph_idx c < o := λ c, (e _).2 ⟨_, rfl⟩, refine ordinal.induction_on o _ this, introsI α r _ h, let s := sup.{u u} (λ a:α, inv_fun aleph_idx (ordinal.typein r a)), apply not_le_of_gt (lt_succ_self s), have I : injective aleph_idx := aleph_idx.initial_seg.to_embedding.inj, simpa only [typein_enum, left_inverse_inv_fun I (succ s)] using le_sup.{u u} (λ a, inv_fun aleph_idx (ordinal.typein r a)) (ordinal.enum r _ (h (succ s))), end @[simp] theorem aleph_idx.order_iso_coe : (aleph_idx.order_iso : cardinal → ordinal) = aleph_idx := rfl @[simp] theorem type_cardinal : @ordinal.type cardinal (<) _ = ordinal.univ.{u (u+1)} := by rw ordinal.univ_id; exact quotient.sound ⟨aleph_idx.order_iso⟩ @[simp] theorem mk_cardinal : mk cardinal = univ.{u (u+1)} := by simpa only [card_type, card_univ] using congr_arg card type_cardinal def aleph'.order_iso := cardinal.aleph_idx.order_iso.symm /-- The `aleph'` function gives the cardinals listed by their ordinal index, and is the inverse of `aleph_idx`. `aleph' n = n`, `aleph' ω = ω`, `aleph' (ω + 1) = ℵ₁, etc. -/ def aleph' : ordinal → cardinal := aleph'.order_iso @[simp] theorem aleph'.order_iso_coe : (aleph'.order_iso : ordinal → cardinal) = aleph' := rfl @[simp] theorem aleph'_lt {o₁ o₂ : ordinal.{u}} : aleph' o₁ < aleph' o₂ ↔ o₁ < o₂ := aleph'.order_iso.ord'.symm @[simp] theorem aleph'_le {o₁ o₂ : ordinal.{u}} : aleph' o₁ ≤ aleph' o₂ ↔ o₁ ≤ o₂ := le_iff_le_iff_lt_iff_lt.2 aleph'_lt @[simp] theorem aleph'_aleph_idx (c : cardinal.{u}) : aleph' c.aleph_idx = c := cardinal.aleph_idx.order_iso.to_equiv.symm_apply_apply c @[simp] theorem aleph_idx_aleph' (o : ordinal.{u}) : (aleph' o).aleph_idx = o := cardinal.aleph_idx.order_iso.to_equiv.apply_symm_apply o @[simp] theorem aleph'_zero : aleph' 0 = 0 := by rw [← le_zero, ← aleph'_aleph_idx 0, aleph'_le]; apply ordinal.zero_le @[simp] theorem aleph'_succ {o : ordinal.{u}} : aleph' o.succ = (aleph' o).succ := le_antisymm (cardinal.aleph_idx_le.1 $ by rw [aleph_idx_aleph', ordinal.succ_le, ← aleph'_lt, aleph'_aleph_idx]; apply cardinal.lt_succ_self) (cardinal.succ_le.2 $ aleph'_lt.2 $ ordinal.lt_succ_self _) @[simp] theorem aleph'_nat : ∀ n : ℕ, aleph' n = n | 0 := aleph'_zero | (n+1) := show aleph' (ordinal.succ n) = n.succ, by rw [aleph'_succ, aleph'_nat, nat_succ] theorem aleph'_le_of_limit {o : ordinal.{u}} (l : o.is_limit) {c} : aleph' o ≤ c ↔ ∀ o' < o, aleph' o' ≤ c := ⟨λ h o' h', le_trans (aleph'_le.2 $ le_of_lt h') h, λ h, begin rw [← aleph'_aleph_idx c, aleph'_le, ordinal.limit_le l], intros x h', rw [← aleph'_le, aleph'_aleph_idx], exact h _ h' end⟩ @[simp] theorem aleph'_omega : aleph' ordinal.omega = omega := eq_of_forall_ge_iff $ λ c, begin simp only [aleph'_le_of_limit omega_is_limit, ordinal.lt_omega, exists_imp_distrib, omega_le], exact forall_swap.trans (forall_congr $ λ n, by simp only [forall_eq, aleph'_nat]), end /-- aleph' and aleph_idx form an equivalence between `ordinal` and `cardinal` -/ @[simp] def aleph'_equiv : ordinal ≃ cardinal := ⟨aleph', aleph_idx, aleph_idx_aleph', aleph'_aleph_idx⟩ /-- The `aleph` function gives the infinite cardinals listed by their ordinal index. `aleph 0 = ω`, `aleph 1 = succ ω` is the first uncountable cardinal, and so on. -/ def aleph (o : ordinal) : cardinal := aleph' (ordinal.omega + o) @[simp] theorem aleph_lt {o₁ o₂ : ordinal.{u}} : aleph o₁ < aleph o₂ ↔ o₁ < o₂ := aleph'_lt.trans (ordinal.add_lt_add_iff_left _) @[simp] theorem aleph_le {o₁ o₂ : ordinal.{u}} : aleph o₁ ≤ aleph o₂ ↔ o₁ ≤ o₂ := le_iff_le_iff_lt_iff_lt.2 aleph_lt @[simp] theorem aleph_succ {o : ordinal.{u}} : aleph o.succ = (aleph o).succ := by rw [aleph, ordinal.add_succ, aleph'_succ]; refl @[simp] theorem aleph_zero : aleph 0 = omega := by simp only [aleph, add_zero, aleph'_omega] theorem omega_le_aleph' {o : ordinal} : omega ≤ aleph' o ↔ ordinal.omega ≤ o := by rw [← aleph'_omega, aleph'_le] theorem omega_le_aleph (o : ordinal) : omega ≤ aleph o := by rw [aleph, omega_le_aleph']; apply ordinal.le_add_right theorem ord_aleph_is_limit (o : ordinal) : is_limit (aleph o).ord := ord_is_limit $ omega_le_aleph _ theorem exists_aleph {c : cardinal} : omega ≤ c ↔ ∃ o, c = aleph o := ⟨λ h, ⟨aleph_idx c - ordinal.omega, by rw [aleph, ordinal.add_sub_cancel_of_le, aleph'_aleph_idx]; rwa [← omega_le_aleph', aleph'_aleph_idx]⟩, λ ⟨o, e⟩, e.symm ▸ omega_le_aleph _⟩ theorem aleph'_is_normal : is_normal (ord ∘ aleph') := ⟨λ o, ord_lt_ord.2 $ aleph'_lt.2 $ ordinal.lt_succ_self _, λ o l a, by simp only [ord_le, aleph'_le_of_limit l]⟩ theorem aleph_is_normal : is_normal (ord ∘ aleph) := aleph'_is_normal.trans $ add_is_normal ordinal.omega /- properties of mul -/ theorem mul_eq_self {c : cardinal} (h : omega ≤ c) : c * c = c := begin refine le_antisymm _ (by simpa only [mul_one] using mul_le_mul_left c (le_trans (le_of_lt one_lt_omega) h)), refine acc.rec_on (cardinal.wf.apply c) (λ c _, quotient.induction_on c $ λ α IH ol, _) h, rcases ord_eq α with ⟨r, wo, e⟩, resetI, let := decidable_linear_order_of_STO' r, have : is_well_order α (<) := wo, let g : α × α → α := λ p, max p.1 p.2, let f : α × α ↪ ordinal × (α × α) := ⟨λ p:α×α, (typein (<) (g p), p), λ p q, congr_arg prod.snd⟩, let s := f ⁻¹'o (prod.lex (<) (prod.lex (<) (<))), have : is_well_order _ s := (order_embedding.preimage _ _).is_well_order, suffices : type s ≤ type r, {exact card_le_card this}, refine le_of_forall_lt (λ o h, _), rcases typein_surj s h with ⟨p, rfl⟩, rw [← e, lt_ord], refine lt_of_le_of_lt (_ : _ ≤ card (typein (<) (g p)).succ * card (typein (<) (g p)).succ) _, { have : {q|s q p} ⊆ (insert (g p) {x | x < (g p)}).prod (insert (g p) {x | x < (g p)}), { intros q h, simp only [s, embedding.coe_fn_mk, order.preimage, typein_lt_typein, prod.lex_def, typein_inj] at h, exact max_le_iff.1 (le_iff_lt_or_eq.2 $ h.imp_right and.left) }, suffices H : (insert (g p) {x | r x (g p)} : set α) ≃ ({x | r x (g p)} ⊕ punit), { exact ⟨(set.embedding_of_subset this).trans ((equiv.set.prod _ _).trans (H.prod_congr H)).to_embedding⟩ }, refine (equiv.set.insert _).trans ((equiv.refl _).sum_congr punit_equiv_punit), apply @irrefl _ r }, cases lt_or_ge (card (typein (<) (g p)).succ) omega with qo qo, { exact lt_of_lt_of_le (mul_lt_omega qo qo) ol }, { suffices, {exact lt_of_le_of_lt (IH _ this qo) this}, rw ← lt_ord, apply (ord_is_limit ol).2, rw [mk_def, e], apply typein_lt_type } end end using_ordinals theorem mul_eq_max {a b : cardinal} (ha : omega ≤ a) (hb : omega ≤ b) : a * b = max a b := le_antisymm (mul_eq_self (le_trans ha (le_max_left a b)) ▸ mul_le_mul (le_max_left _ _) (le_max_right _ _)) $ max_le (by simpa only [mul_one] using mul_le_mul_left a (le_trans (le_of_lt one_lt_omega) hb)) (by simpa only [one_mul] using mul_le_mul_right b (le_trans (le_of_lt one_lt_omega) ha)) theorem mul_lt_of_lt {a b c : cardinal} (hc : omega ≤ c) (h1 : a < c) (h2 : b < c) : a * b < c := lt_of_le_of_lt (mul_le_mul (le_max_left a b) (le_max_right a b)) $ (lt_or_le (max a b) omega).elim (λ h, lt_of_lt_of_le (mul_lt_omega h h) hc) (λ h, by rw mul_eq_self h; exact max_lt h1 h2) lemma mul_le_max_of_omega_le_left {a b : cardinal} (h : omega ≤ a) : a * b ≤ max a b := begin convert mul_le_mul (le_max_left a b) (le_max_right a b), rw [mul_eq_self], refine le_trans h (le_max_left a b) end lemma mul_eq_max_of_omega_le_left {a b : cardinal} (h : omega ≤ a) (h' : b ≠ 0) : a * b = max a b := begin apply le_antisymm, apply mul_le_max_of_omega_le_left h, cases le_or_gt omega b with hb hb, rw [mul_eq_max h hb], have : b ≤ a, exact le_trans (le_of_lt hb) h, rw [max_eq_left this], convert mul_le_mul_left _ (one_le_iff_ne_zero.mpr h'), rw [mul_one], end lemma mul_eq_left {a b : cardinal} (ha : omega ≤ a) (hb : b ≤ a) (hb' : b ≠ 0) : a * b = a := by { rw [mul_eq_max_of_omega_le_left ha hb', max_eq_left hb] } lemma mul_eq_right {a b : cardinal} (hb : omega ≤ b) (ha : a ≤ b) (ha' : a ≠ 0) : a * b = b := by { rw [mul_comm, mul_eq_left hb ha ha'] } lemma le_mul_left {a b : cardinal} (h : b ≠ 0) : a ≤ b * a := by { convert mul_le_mul_right _ (one_le_iff_ne_zero.mpr h), rw [one_mul] } lemma le_mul_right {a b : cardinal} (h : b ≠ 0) : a ≤ a * b := by { rw [mul_comm], exact le_mul_left h } lemma mul_eq_left_iff {a b : cardinal} : a * b = a ↔ ((max omega b ≤ a ∧ b ≠ 0) ∨ b = 1 ∨ a = 0) := begin rw [max_le_iff], split, { intro h, cases (le_or_lt omega a) with ha ha, { have : a ≠ 0, { rintro rfl, exact not_lt_of_le ha omega_pos }, left, use ha, { rw [← not_lt], intro hb, apply ne_of_gt _ h, refine lt_of_lt_of_le hb (le_mul_left this) }, { rintro rfl, apply this, rw [_root_.mul_zero] at h, subst h }}, right, by_cases h2a : a = 0, { right, exact h2a }, have hb : b ≠ 0, { rintro rfl, apply h2a, rw [mul_zero] at h, subst h }, left, rw [← h, mul_lt_omega_iff, lt_omega, lt_omega] at ha, rcases ha with rfl|rfl|⟨⟨n, rfl⟩, ⟨m, rfl⟩⟩, contradiction, contradiction, rw [← ne] at h2a, rw [← one_le_iff_ne_zero] at h2a hb, norm_cast at h2a hb h ⊢, apply le_antisymm _ hb, rw [← not_lt], intro h2b, apply ne_of_gt _ h, rw [gt], conv_lhs { rw [← mul_one n] }, rwa [mul_lt_mul_left], apply nat.lt_of_succ_le h2a }, { rintro (⟨⟨ha, hab⟩, hb⟩|rfl|rfl), { rw [mul_eq_max_of_omega_le_left ha hb, max_eq_left hab] }, all_goals {simp}} end /- properties of add -/ theorem add_eq_self {c : cardinal} (h : omega ≤ c) : c + c = c := le_antisymm (by simpa only [nat.cast_bit0, nat.cast_one, mul_eq_self h, two_mul] using mul_le_mul_right c (le_trans (le_of_lt $ nat_lt_omega 2) h)) (le_add_left c c) theorem add_eq_max {a b : cardinal} (ha : omega ≤ a) : a + b = max a b := le_antisymm (add_eq_self (le_trans ha (le_max_left a b)) ▸ add_le_add (le_max_left _ _) (le_max_right _ _)) $ max_le (le_add_right _ _) (le_add_left _ _) theorem add_lt_of_lt {a b c : cardinal} (hc : omega ≤ c) (h1 : a < c) (h2 : b < c) : a + b < c := lt_of_le_of_lt (add_le_add (le_max_left a b) (le_max_right a b)) $ (lt_or_le (max a b) omega).elim (λ h, lt_of_lt_of_le (add_lt_omega h h) hc) (λ h, by rw add_eq_self h; exact max_lt h1 h2) lemma eq_of_add_eq_of_omega_le {a b c : cardinal} (h : a + b = c) (ha : a < c) (hc : omega ≤ c) : b = c := begin apply le_antisymm, { rw [← h], apply cardinal.le_add_left }, rw[← not_lt], intro hb, have : a + b < c := add_lt_of_lt hc ha hb, simpa [h, lt_irrefl] using this end lemma add_eq_left {a b : cardinal} (ha : omega ≤ a) (hb : b ≤ a) : a + b = a := by { rw [add_eq_max ha, max_eq_left hb] } lemma add_eq_right {a b : cardinal} (hb : omega ≤ b) (ha : a ≤ b) : a + b = b := by { rw [add_comm, add_eq_left hb ha] } lemma add_eq_left_iff {a b : cardinal} : a + b = a ↔ (max omega b ≤ a ∨ b = 0) := begin rw [max_le_iff], split, { intro h, cases (le_or_lt omega a) with ha ha, { left, use ha, rw [← not_lt], intro hb, apply ne_of_gt _ h, exact lt_of_lt_of_le hb (le_add_left b a) }, right, rw [← h, add_lt_omega_iff, lt_omega, lt_omega] at ha, rcases ha with ⟨⟨n, rfl⟩, ⟨m, rfl⟩⟩, norm_cast at h ⊢, rw [← add_left_inj, h, add_zero] }, { rintro (⟨h1, h2⟩|h3), rw [add_eq_max h1, max_eq_left h2], rw [h3, add_zero] } end lemma add_eq_right_iff {a b : cardinal} : a + b = b ↔ (max omega a ≤ b ∨ a = 0) := by { rw [add_comm, add_eq_left_iff] } lemma add_one_eq {a : cardinal} (ha : omega ≤ a) : a + 1 = a := have 1 ≤ a, from le_trans (le_of_lt one_lt_omega) ha, add_eq_left ha this protected lemma eq_of_add_eq_add_left {a b c : cardinal} (h : a + b = a + c) (ha : a < omega) : b = c := begin cases le_or_lt omega b with hb hb, { have : a < b := lt_of_lt_of_le ha hb, rw [add_eq_right hb (le_of_lt this), eq_comm] at h, rw [eq_of_add_eq_of_omega_le h this hb] }, { have hc : c < omega, { rw [← not_le], intro hc, apply lt_irrefl omega, apply lt_of_le_of_lt (le_trans hc (le_add_left _ a)), rw [← h], apply add_lt_omega ha hb }, rw [lt_omega] at *, rcases ha with ⟨n, rfl⟩, rcases hb with ⟨m, rfl⟩, rcases hc with ⟨k, rfl⟩, norm_cast at h ⊢, apply eq_of_add_eq_add_left h } end protected lemma eq_of_add_eq_add_right {a b c : cardinal} (h : a + b = c + b) (hb : b < omega) : a = c := by { rw [add_comm a b, add_comm c b] at h, exact cardinal.eq_of_add_eq_add_left h hb } /- properties about power -/ theorem pow_le {κ μ : cardinal.{u}} (H1 : omega ≤ κ) (H2 : μ < omega) : κ ^ μ ≤ κ := let ⟨n, H3⟩ := lt_omega.1 H2 in H3.symm ▸ (quotient.induction_on κ (λ α H1, nat.rec_on n (le_of_lt $ lt_of_lt_of_le (by rw [nat.cast_zero, power_zero]; from one_lt_omega) H1) (λ n ih, trans_rel_left _ (by rw [nat.cast_succ, power_add, power_one]; from mul_le_mul_right _ ih) (mul_eq_self H1))) H1) lemma power_self_eq {c : cardinal} (h : omega ≤ c) : c ^ c = 2 ^ c := begin apply le_antisymm, { apply le_trans (power_le_power_right $ le_of_lt $ cantor c), rw [power_mul, mul_eq_self h] }, { convert power_le_power_right (le_trans (le_of_lt $ nat_lt_omega 2) h), apply nat.cast_two.symm } end lemma power_nat_le {c : cardinal.{u}} {n : ℕ} (h : omega ≤ c) : c ^ (n : cardinal.{u}) ≤ c := pow_le h (nat_lt_omega n) lemma powerlt_omega {c : cardinal} (h : omega ≤ c) : c ^< omega = c := begin apply le_antisymm, { rw [powerlt_le], intro c', rw [lt_omega], rintro ⟨n, rfl⟩, apply power_nat_le h }, convert le_powerlt one_lt_omega, rw [power_one] end lemma powerlt_omega_le (c : cardinal) : c ^< omega ≤ max c omega := begin cases le_or_gt omega c, { rw [powerlt_omega h], apply le_max_left }, rw [powerlt_le], intros c' hc', refine le_trans (le_of_lt $ power_lt_omega h hc') (le_max_right _ _) end /- compute cardinality of various types -/ theorem mk_list_eq_mk {α : Type u} (H1 : omega ≤ mk α) : mk (list α) = mk α := eq.symm $ le_antisymm ⟨⟨λ x, [x], λ x y H, (list.cons.inj H).1⟩⟩ $ calc mk (list α) = sum (λ n : ℕ, mk α ^ (n : cardinal.{u})) : mk_list_eq_sum_pow α ... ≤ sum (λ n : ℕ, mk α) : sum_le_sum _ _ $ λ n, pow_le H1 $ nat_lt_omega n ... = sum (λ n : ulift.{u} ℕ, mk α) : quotient.sound ⟨@sigma_congr_left _ _ (λ _, quotient.out (mk α)) equiv.ulift.symm⟩ ... = omega * mk α : sum_const _ _ ... = max (omega) (mk α) : mul_eq_max (le_refl _) H1 ... = mk α : max_eq_right H1 lemma mk_bounded_set_le_of_omega_le (α : Type u) (c : cardinal) (hα : omega ≤ mk α) : mk {t : set α // mk t ≤ c} ≤ mk α ^ c := begin refine le_trans _ (by rw [←add_one_eq hα]), refine quotient.induction_on c _, clear c, intro β, fapply mk_le_of_surjective, { intro f, use sum.inl ⁻¹' range f, refine le_trans (mk_preimage_of_injective _ _ (λ x y, sum.inl.inj)) _, apply mk_range_le }, rintro ⟨s, ⟨g⟩⟩, use λ y, if h : ∃(x : s), g x = y then sum.inl (classical.some h).val else sum.inr ⟨⟩, apply subtype.eq, ext, split, { rintro ⟨y, h⟩, dsimp only at h, by_cases h' : ∃ (z : s), g z = y, { rw [dif_pos h'] at h, cases sum.inl.inj h, exact (classical.some h').2 }, { rw [dif_neg h'] at h, cases h }}, { intro h, have : ∃(z : s), g z = g ⟨x, h⟩, exact ⟨⟨x, h⟩, rfl⟩, use g ⟨x, h⟩, dsimp only, rw [dif_pos this], congr', suffices : classical.some this = ⟨x, h⟩, exact congr_arg subtype.val this, apply g.2, exact classical.some_spec this } end lemma mk_bounded_set_le (α : Type u) (c : cardinal) : mk {t : set α // mk t ≤ c} ≤ max (mk α) omega ^ c := begin transitivity mk {t : set (ulift.{u} nat ⊕ α) // mk t ≤ c}, { refine ⟨embedding.subtype_map _ _⟩, apply embedding.image, use sum.inr, apply sum.inr.inj, intros s hs, exact le_trans mk_image_le hs }, refine le_trans (mk_bounded_set_le_of_omega_le (ulift.{u} nat ⊕ α) c (le_add_right omega (mk α))) _, rw [max_comm, ←add_eq_max]; refl end lemma mk_bounded_subset_le {α : Type u} (s : set α) (c : cardinal.{u}) : mk {t : set α // t ⊆ s ∧ mk t ≤ c} ≤ max (mk s) omega ^ c := begin refine le_trans _ (mk_bounded_set_le s c), refine ⟨embedding.cod_restrict _ _ _⟩, use λ t, subtype.val ⁻¹' t.1, { rintros ⟨t, ht1, ht2⟩ ⟨t', h1t', h2t'⟩ h, apply subtype.eq, dsimp only at h ⊢, refine (preimage_eq_preimage' _ _).1 h; rw [subtype.range_val]; assumption }, rintro ⟨t, h1t, h2t⟩, exact le_trans (mk_preimage_of_injective _ _ subtype.val_injective) h2t end /- compl -/ lemma mk_compl_of_omega_le {α : Type*} (s : set α) (h : omega ≤ #α) (h2 : #s < #α) : #(-s : set α) = #α := by { refine eq_of_add_eq_of_omega_le _ h2 h, exact mk_sum_compl s } lemma mk_compl_finset_of_omega_le {α : Type*} (s : finset α) (h : omega ≤ #α) : #(-s.to_set : set α) = #α := by { apply mk_compl_of_omega_le _ h, exact lt_of_lt_of_le (finset_card_lt_omega s) h } lemma mk_compl_eq_mk_compl_infinite {α : Type*} {s t : set α} (h : omega ≤ #α) (hs : #s < #α) (ht : #t < #α) : #(-s : set α) = #(-t : set α) := by { rw [mk_compl_of_omega_le s h hs, mk_compl_of_omega_le t h ht] } lemma mk_compl_eq_mk_compl_finite_lift {α : Type u} {β : Type v} {s : set α} {t : set β} (hα : #α < omega) (h1 : lift.{u (max v w)} (#α) = lift.{v (max u w)} (#β)) (h2 : lift.{u (max v w)} (#s) = lift.{v (max u w)} (#t)) : lift.{u (max v w)} (#(-s : set α)) = lift.{v (max u w)} (#(-t : set β)) := begin have hα' := hα, have h1' := h1, rw [← mk_sum_compl s, ← mk_sum_compl t] at h1, rw [← mk_sum_compl s, add_lt_omega_iff] at hα, lift #s to ℕ using hα.1 with n hn, lift #(- s : set α) to ℕ using hα.2 with m hm, have : #(- t : set β) < omega, { refine lt_of_le_of_lt (mk_subtype_le _) _, rw [← lift_lt, lift_omega, ← h1', ← lift_omega.{u (max v w)}, lift_lt], exact hα' }, lift #(- t : set β) to ℕ using this with k hk, simp [nat_eq_lift_eq_iff] at h2, rw [nat_eq_lift_eq_iff.{v (max u w)}] at h2, simp [h2.symm] at h1 ⊢, norm_cast at h1, simp at h1, exact h1 end lemma mk_compl_eq_mk_compl_finite {α β : Type u} {s : set α} {t : set β} (hα : #α < omega) (h1 : #α = #β) (h : #s = #t) : #(-s : set α) = #(-t : set β) := by { rw [← lift_inj], apply mk_compl_eq_mk_compl_finite_lift hα; rw [lift_inj]; assumption } lemma mk_compl_eq_mk_compl_finite_same {α : Type*} {s t : set α} (hα : #α < omega) (h : #s = #t) : #(-s : set α) = #(-t : set α) := mk_compl_eq_mk_compl_finite hα rfl h /- extend an injection to an equiv -/ theorem extend_function {α β : Type*} {s : set α} (f : s ↪ β) (h : nonempty ((-s : set α) ≃ (- range f : set β))) : ∃ (g : α ≃ β), ∀ x : s, g x = f x := begin intros, have := h, cases this with g, let h : α ≃ β := (set.sum_compl (s : set α)).symm.trans ((sum_congr (equiv.set.range f f.2) g).trans (set.sum_compl (range f))), refine ⟨h, _⟩, rintro ⟨x, hx⟩, simp [set.sum_compl_symm_apply_of_mem, hx, equiv.symm] end theorem extend_function_finite {α β : Type*} {s : set α} (f : s ↪ β) (hs : #α < omega) (h : nonempty (α ≃ β)) : ∃ (g : α ≃ β), ∀ x : s, g x = f x := begin apply extend_function f, have := h, cases this with g, rw [← lift_mk_eq] at h, rw [←lift_mk_eq, mk_compl_eq_mk_compl_finite_lift hs h], rw [mk_range_eq_lift], exact f.2 end theorem extend_function_of_lt {α β : Type*} {s : set α} (f : s ↪ β) (hs : #s < #α) (h : nonempty (α ≃ β)) : ∃ (g : α ≃ β), ∀ x : s, g x = f x := begin cases (le_or_lt omega (#α)) with hα hα, { apply extend_function f, have := h, cases this with g, rw [← lift_mk_eq] at h, cases cardinal.eq.mp (mk_compl_of_omega_le s hα hs) with g2, cases cardinal.eq.mp (mk_compl_of_omega_le (range f) _ _) with g3, { constructor, exact g2.trans (g.trans g3.symm) }, { rw [← lift_le, ← h], refine le_trans _ (lift_le.mpr hα), simp }, rwa [← lift_lt, ← h, mk_range_eq_lift, lift_lt], exact f.2 }, { exact extend_function_finite f hα h } end end cardinal
9d561a7a2271420ffa2a4f75f7af46cd5fc7e344
ac49064e8a9a038e07cf5574b4fccd8a70d115c8
/hott/algebra/default.hlean
c12af809b60b6561954e69ac318c9064eed5ffd8
[ "Apache-2.0" ]
permissive
Bolt64/lean2
7c75016729569e04a3f403c7a4fc7c1de4377c9d
75fd8162488214a959dbe3303a185cbbb83f60f9
refs/heads/master
1,611,290,445,156
1,493,763,922,000
1,493,763,922,000
81,566,307
0
0
null
1,486,732,167,000
1,486,732,167,000
null
UTF-8
Lean
false
false
206
hlean
/- Copyright (c) 2016 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import .homotopy_group .ordered_field .lattice
27942cc1fea4e9b2fec33e3326b67cab609ffc99
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/matrix/notation.lean
a5b8ce8843f08475771ce299fdab25caf078f0b2
[]
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
20,260
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen Notation for vectors and matrices -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.fintype.card import Mathlib.data.matrix.basic import Mathlib.tactic.fin_cases import Mathlib.PostPort universes u u_1 u_2 u_3 namespace Mathlib /-! # Matrix and vector notation This file defines notation for vectors and matrices. Given `a b c d : α`, the notation allows us to write `![a, b, c, d] : fin 4 → α`. Nesting vectors gives a matrix, so `![![a, b], ![c, d]] : matrix (fin 2) (fin 2) α`. This file includes `simp` lemmas for applying operations in `data.matrix.basic` to values built out of this notation. ## Main definitions * `vec_empty` is the empty vector (or `0` by `n` matrix) `![]` * `vec_cons` prepends an entry to a vector, so `![a, b]` is `vec_cons a (vec_cons b vec_empty)` ## Implementation notes The `simp` lemmas require that one of the arguments is of the form `vec_cons _ _`. This ensures `simp` works with entries only when (some) entries are already given. In other words, this notation will only appear in the output of `simp` if it already appears in the input. ## Notations The main new notation is `![a, b]`, which gets expanded to `vec_cons a (vec_cons b vec_empty)`. -/ namespace matrix /-- `![]` is the vector with no entries. -/ def vec_empty {α : Type u} : fin 0 → α := fin_zero_elim /-- `vec_cons h t` prepends an entry `h` to a vector `t`. The inverse functions are `vec_head` and `vec_tail`. The notation `![a, b, ...]` expands to `vec_cons a (vec_cons b ...)`. -/ def vec_cons {α : Type u} {n : ℕ} (h : α) (t : fin n → α) : fin (Nat.succ n) → α := fin.cons h t /-- `vec_head v` gives the first entry of the vector `v` -/ def vec_head {α : Type u} {n : ℕ} (v : fin (Nat.succ n) → α) : α := v 0 /-- `vec_tail v` gives a vector consisting of all entries of `v` except the first -/ def vec_tail {α : Type u} {n : ℕ} (v : fin (Nat.succ n) → α) : fin n → α := v ∘ fin.succ theorem empty_eq {α : Type u} (v : fin 0 → α) : v = vec_empty := funext fun (i : fin 0) => false.dcases_on (fun (h : i ∈ fintype.elems (fin 0)) => v i = vec_empty i) (fintype.complete i) @[simp] theorem head_fin_const {α : Type u} {n : ℕ} (a : α) : (vec_head fun (i : fin (n + 1)) => a) = a := rfl @[simp] theorem cons_val_zero {α : Type u} {m : ℕ} (x : α) (u : fin m → α) : vec_cons x u 0 = x := rfl theorem cons_val_zero' {α : Type u} {m : ℕ} (h : 0 < Nat.succ m) (x : α) (u : fin m → α) : vec_cons x u { val := 0, property := h } = x := rfl @[simp] theorem cons_val_succ {α : Type u} {m : ℕ} (x : α) (u : fin m → α) (i : fin m) : vec_cons x u (fin.succ i) = u i := sorry @[simp] theorem cons_val_succ' {α : Type u} {m : ℕ} {i : ℕ} (h : Nat.succ i < Nat.succ m) (x : α) (u : fin m → α) : vec_cons x u { val := Nat.succ i, property := h } = u { val := i, property := nat.lt_of_succ_lt_succ h } := sorry @[simp] theorem head_cons {α : Type u} {m : ℕ} (x : α) (u : fin m → α) : vec_head (vec_cons x u) = x := rfl @[simp] theorem tail_cons {α : Type u} {m : ℕ} (x : α) (u : fin m → α) : vec_tail (vec_cons x u) = u := sorry @[simp] theorem empty_val' {α : Type u} {n' : Type u_1} (j : n') : (fun (i : fin 0) => vec_empty i j) = vec_empty := empty_eq fun (i : fin 0) => vec_empty i j @[simp] theorem cons_val' {α : Type u} {m : ℕ} {n' : Type u_2} [fintype n'] (v : n' → α) (B : matrix (fin m) n' α) (i : fin (Nat.succ m)) (j : n') : vec_cons v B i j = vec_cons (v j) (fun (i : fin m) => B i j) i := sorry @[simp] theorem head_val' {α : Type u} {m : ℕ} {n' : Type u_2} [fintype n'] (B : matrix (fin (Nat.succ m)) n' α) (j : n') : (vec_head fun (i : fin (Nat.succ m)) => B i j) = vec_head B j := rfl @[simp] theorem tail_val' {α : Type u} {m : ℕ} {n' : Type u_2} [fintype n'] (B : matrix (fin (Nat.succ m)) n' α) (j : n') : (vec_tail fun (i : fin (Nat.succ m)) => B i j) = fun (i : fin m) => vec_tail B i j := sorry @[simp] theorem cons_head_tail {α : Type u} {m : ℕ} (u : fin (Nat.succ m) → α) : vec_cons (vec_head u) (vec_tail u) = u := fin.cons_self_tail u @[simp] theorem range_cons {α : Type u} {n : ℕ} (x : α) (u : fin n → α) : set.range (vec_cons x u) = singleton x ∪ set.range u := sorry @[simp] theorem range_empty {α : Type u} (u : fin 0 → α) : set.range u = ∅ := sorry /-- `![a, b, ...] 1` is equal to `b`. The simplifier needs a special lemma for length `≥ 2`, in addition to `cons_val_succ`, because `1 : fin 1 = 0 : fin 1`. -/ @[simp] theorem cons_val_one {α : Type u} {m : ℕ} (x : α) (u : fin (Nat.succ m) → α) : vec_cons x u 1 = vec_head u := cons_val_succ x u 0 @[simp] theorem cons_val_fin_one {α : Type u} (x : α) (u : fin 0 → α) (i : fin 1) : vec_cons x u i = x := sorry /-! ### Numeral (`bit0` and `bit1`) indices The following definitions and `simp` lemmas are to allow any numeral-indexed element of a vector given with matrix notation to be extracted by `simp` (even when the numeral is larger than the number of elements in the vector, which is taken modulo that number of elements by virtue of the semantics of `bit0` and `bit1` and of addition on `fin n`). -/ @[simp] theorem empty_append {α : Type u} {n : ℕ} (v : fin n → α) : fin.append (Eq.symm (zero_add n)) vec_empty v = v := sorry @[simp] theorem cons_append {α : Type u} {m : ℕ} {n : ℕ} {o : ℕ} (ho : o + 1 = m + 1 + n) (x : α) (u : fin m → α) (v : fin n → α) : fin.append ho (vec_cons x u) v = vec_cons x (fin.append (eq.mp (Eq._oldrec (Eq.refl (o + 1 = m + n + 1)) (propext add_right_cancel_iff)) (eq.mp (Eq._oldrec (Eq.refl (o + 1 = m + (n + 1))) (Eq.symm (add_assoc m n 1))) (eq.mp (Eq._oldrec (Eq.refl (o + 1 = m + (1 + n))) (add_comm 1 n)) (eq.mp (Eq._oldrec (Eq.refl (o + 1 = m + 1 + n)) (add_assoc m 1 n)) ho)))) u v) := sorry /-- `vec_alt0 v` gives a vector with half the length of `v`, with only alternate elements (even-numbered). -/ def vec_alt0 {α : Type u} {m : ℕ} {n : ℕ} (hm : m = n + n) (v : fin m → α) (k : fin n) : α := v { val := ↑k + ↑k, property := sorry } /-- `vec_alt1 v` gives a vector with half the length of `v`, with only alternate elements (odd-numbered). -/ def vec_alt1 {α : Type u} {m : ℕ} {n : ℕ} (hm : m = n + n) (v : fin m → α) (k : fin n) : α := v { val := ↑k + ↑k + 1, property := sorry } theorem vec_alt0_append {α : Type u} {n : ℕ} (v : fin n → α) : vec_alt0 rfl (fin.append rfl v v) = v ∘ bit0 := sorry theorem vec_alt1_append {α : Type u} {n : ℕ} (v : fin (n + 1) → α) : vec_alt1 rfl (fin.append rfl v v) = v ∘ bit1 := sorry @[simp] theorem vec_head_vec_alt0 {α : Type u} {m : ℕ} {n : ℕ} (hm : m + bit0 1 = n + 1 + (n + 1)) (v : fin (m + bit0 1) → α) : vec_head (vec_alt0 hm v) = v 0 := rfl @[simp] theorem vec_head_vec_alt1 {α : Type u} {m : ℕ} {n : ℕ} (hm : m + bit0 1 = n + 1 + (n + 1)) (v : fin (m + bit0 1) → α) : vec_head (vec_alt1 hm v) = v 1 := rfl @[simp] theorem cons_vec_bit0_eq_alt0 {α : Type u} {n : ℕ} (x : α) (u : fin n → α) (i : fin (n + 1)) : vec_cons x u (bit0 i) = vec_alt0 rfl (fin.append rfl (vec_cons x u) (vec_cons x u)) i := sorry @[simp] theorem cons_vec_bit1_eq_alt1 {α : Type u} {n : ℕ} (x : α) (u : fin n → α) (i : fin (n + 1)) : vec_cons x u (bit1 i) = vec_alt1 rfl (fin.append rfl (vec_cons x u) (vec_cons x u)) i := sorry @[simp] theorem cons_vec_alt0 {α : Type u} {m : ℕ} {n : ℕ} (h : m + 1 + 1 = n + 1 + (n + 1)) (x : α) (y : α) (u : fin m → α) : vec_alt0 h (vec_cons x (vec_cons y u)) = vec_cons x (vec_alt0 (eq.mp (Eq._oldrec (Eq.refl (m + 1 = n + n + 1)) (propext add_right_cancel_iff)) (eq.mp (Eq._oldrec (Eq.refl (m + 1 + 1 = n + n + 1 + 1)) (propext add_right_cancel_iff)) (eq.mp (Eq._oldrec (Eq.refl (m + 1 + 1 = n + (n + 1) + 1)) (Eq.symm (add_assoc n n 1))) (eq.mp (Eq._oldrec (Eq.refl (m + 1 + 1 = n + (n + 1 + 1))) (Eq.symm (add_assoc n (n + 1) 1))) (eq.mp (Eq._oldrec (Eq.refl (m + 1 + 1 = n + (1 + (n + 1)))) (add_comm 1 (n + 1))) (eq.mp (Eq._oldrec (Eq.refl (m + 1 + 1 = n + 1 + (n + 1))) (add_assoc n 1 (n + 1))) h)))))) u) := sorry -- Although proved by simp, extracting element 8 of a five-element -- vector does not work by simp unless this lemma is present. @[simp] theorem empty_vec_alt0 (α : Type u_1) {h : 0 = 0 + 0} : vec_alt0 h vec_empty = vec_empty := eq.mpr (id (propext (eq_iff_true_of_subsingleton (vec_alt0 h vec_empty) vec_empty))) trivial @[simp] theorem cons_vec_alt1 {α : Type u} {m : ℕ} {n : ℕ} (h : m + 1 + 1 = n + 1 + (n + 1)) (x : α) (y : α) (u : fin m → α) : vec_alt1 h (vec_cons x (vec_cons y u)) = vec_cons y (vec_alt1 (eq.mp (Eq._oldrec (Eq.refl (m + 1 = n + n + 1)) (propext add_right_cancel_iff)) (eq.mp (Eq._oldrec (Eq.refl (m + 1 + 1 = n + n + 1 + 1)) (propext add_right_cancel_iff)) (eq.mp (Eq._oldrec (Eq.refl (m + 1 + 1 = n + (n + 1) + 1)) (Eq.symm (add_assoc n n 1))) (eq.mp (Eq._oldrec (Eq.refl (m + 1 + 1 = n + (n + 1 + 1))) (Eq.symm (add_assoc n (n + 1) 1))) (eq.mp (Eq._oldrec (Eq.refl (m + 1 + 1 = n + (1 + (n + 1)))) (add_comm 1 (n + 1))) (eq.mp (Eq._oldrec (Eq.refl (m + 1 + 1 = n + 1 + (n + 1))) (add_assoc n 1 (n + 1))) h)))))) u) := sorry -- Although proved by simp, extracting element 9 of a five-element -- vector does not work by simp unless this lemma is present. @[simp] theorem empty_vec_alt1 (α : Type u_1) {h : 0 = 0 + 0} : vec_alt1 h vec_empty = vec_empty := eq.mpr (id (propext (eq_iff_true_of_subsingleton (vec_alt1 h vec_empty) vec_empty))) trivial @[simp] theorem dot_product_empty {α : Type u} [add_comm_monoid α] [Mul α] (v : fin 0 → α) (w : fin 0 → α) : dot_product v w = 0 := finset.sum_empty @[simp] theorem cons_dot_product {α : Type u} {n : ℕ} [add_comm_monoid α] [Mul α] (x : α) (v : fin n → α) (w : fin (Nat.succ n) → α) : dot_product (vec_cons x v) w = x * vec_head w + dot_product v (vec_tail w) := sorry @[simp] theorem dot_product_cons {α : Type u} {n : ℕ} [add_comm_monoid α] [Mul α] (v : fin (Nat.succ n) → α) (x : α) (w : fin n → α) : dot_product v (vec_cons x w) = vec_head v * x + dot_product (vec_tail v) w := sorry @[simp] theorem col_empty {α : Type u} (v : fin 0 → α) : col v = vec_empty := empty_eq (col v) @[simp] theorem col_cons {α : Type u} {m : ℕ} (x : α) (u : fin m → α) : col (vec_cons x u) = vec_cons (fun (_x : Unit) => x) (col u) := sorry @[simp] theorem row_empty {α : Type u} : row vec_empty = fun (_x : Unit) => vec_empty := funext fun (x : Unit) => funext fun (x_1 : fin 0) => Eq.refl (row vec_empty x x_1) @[simp] theorem row_cons {α : Type u} {m : ℕ} (x : α) (u : fin m → α) : row (vec_cons x u) = fun (_x : Unit) => vec_cons x u := funext fun (x_1 : Unit) => funext fun (x_2 : fin (Nat.succ m)) => Eq.refl (row (vec_cons x u) x_1 x_2) @[simp] theorem transpose_empty_rows {α : Type u} {m' : Type u_1} [fintype m'] (A : matrix m' (fin 0) α) : transpose A = vec_empty := empty_eq (transpose A) @[simp] theorem transpose_empty_cols {α : Type u} {m' : Type u_1} [fintype m'] : transpose vec_empty = fun (i : m') => vec_empty := funext fun (i : m') => empty_eq (transpose vec_empty i) @[simp] theorem cons_transpose {α : Type u} {m : ℕ} {n' : Type u_2} [fintype n'] (v : n' → α) (A : matrix (fin m) n' α) : transpose (vec_cons v A) = fun (i : n') => vec_cons (v i) (transpose A i) := sorry @[simp] theorem head_transpose {α : Type u} {n : ℕ} {m' : Type u_1} [fintype m'] (A : matrix m' (fin (Nat.succ n)) α) : vec_head (transpose A) = vec_head ∘ A := rfl @[simp] theorem tail_transpose {α : Type u} {n : ℕ} {m' : Type u_1} [fintype m'] (A : matrix m' (fin (Nat.succ n)) α) : vec_tail (transpose A) = transpose (vec_tail ∘ A) := ext fun (i : fin n) (j : m') => Eq.refl (vec_tail (transpose A) i j) @[simp] theorem empty_mul {α : Type u} {n' : Type u_2} {o' : Type u_3} [fintype n'] [fintype o'] [semiring α] (A : matrix (fin 0) n' α) (B : matrix n' o' α) : matrix.mul A B = vec_empty := empty_eq (matrix.mul A B) @[simp] theorem empty_mul_empty {α : Type u} {m' : Type u_1} {o' : Type u_3} [fintype m'] [fintype o'] [semiring α] (A : matrix m' (fin 0) α) (B : matrix (fin 0) o' α) : matrix.mul A B = 0 := rfl @[simp] theorem mul_empty {α : Type u} {m' : Type u_1} {n' : Type u_2} [fintype m'] [fintype n'] [semiring α] (A : matrix m' n' α) (B : matrix n' (fin 0) α) : matrix.mul A B = fun (_x : m') => vec_empty := funext fun (_x : m') => empty_eq (matrix.mul A B _x) theorem mul_val_succ {α : Type u} {m : ℕ} {n' : Type u_2} {o' : Type u_3} [fintype n'] [fintype o'] [semiring α] (A : matrix (fin (Nat.succ m)) n' α) (B : matrix n' o' α) (i : fin m) (j : o') : matrix.mul A B (fin.succ i) j = matrix.mul (vec_tail A) B i j := rfl @[simp] theorem cons_mul {α : Type u} {m : ℕ} {n' : Type u_2} {o' : Type u_3} [fintype n'] [fintype o'] [semiring α] (v : n' → α) (A : matrix (fin m) n' α) (B : matrix n' o' α) : matrix.mul (vec_cons v A) B = vec_cons (vec_mul v B) (matrix.mul A B) := sorry @[simp] theorem empty_vec_mul {α : Type u} {o' : Type u_3} [fintype o'] [semiring α] (v : fin 0 → α) (B : matrix (fin 0) o' α) : vec_mul v B = 0 := rfl @[simp] theorem vec_mul_empty {α : Type u} {n' : Type u_2} [fintype n'] [semiring α] (v : n' → α) (B : matrix n' (fin 0) α) : vec_mul v B = vec_empty := empty_eq (vec_mul v B) @[simp] theorem cons_vec_mul {α : Type u} {n : ℕ} {o' : Type u_3} [fintype o'] [semiring α] (x : α) (v : fin n → α) (B : matrix (fin (Nat.succ n)) o' α) : vec_mul (vec_cons x v) B = x • vec_head B + vec_mul v (vec_tail B) := sorry @[simp] theorem vec_mul_cons {α : Type u} {n : ℕ} {o' : Type u_3} [fintype o'] [semiring α] (v : fin (Nat.succ n) → α) (w : o' → α) (B : matrix (fin n) o' α) : vec_mul v (vec_cons w B) = vec_head v • w + vec_mul (vec_tail v) B := sorry @[simp] theorem empty_mul_vec {α : Type u} {n' : Type u_2} [fintype n'] [semiring α] (A : matrix (fin 0) n' α) (v : n' → α) : mul_vec A v = vec_empty := empty_eq (mul_vec A v) @[simp] theorem mul_vec_empty {α : Type u} {m' : Type u_1} [fintype m'] [semiring α] (A : matrix m' (fin 0) α) (v : fin 0 → α) : mul_vec A v = 0 := rfl @[simp] theorem cons_mul_vec {α : Type u} {m : ℕ} {n' : Type u_2} [fintype n'] [semiring α] (v : n' → α) (A : fin m → n' → α) (w : n' → α) : mul_vec (vec_cons v A) w = vec_cons (dot_product v w) (mul_vec A w) := sorry @[simp] theorem mul_vec_cons {n : ℕ} {m' : Type u_1} [fintype m'] {α : Type u_2} [comm_semiring α] (A : m' → fin (Nat.succ n) → α) (x : α) (v : fin n → α) : mul_vec A (vec_cons x v) = x • vec_head ∘ A + mul_vec (vec_tail ∘ A) v := sorry @[simp] theorem empty_vec_mul_vec {α : Type u} {n' : Type u_2} [fintype n'] [semiring α] (v : fin 0 → α) (w : n' → α) : vec_mul_vec v w = vec_empty := empty_eq (vec_mul_vec v w) @[simp] theorem vec_mul_vec_empty {α : Type u} {m' : Type u_1} [fintype m'] [semiring α] (v : m' → α) (w : fin 0 → α) : vec_mul_vec v w = fun (_x : m') => vec_empty := funext fun (i : m') => empty_eq (vec_mul_vec v w i) @[simp] theorem cons_vec_mul_vec {α : Type u} {m : ℕ} {n' : Type u_2} [fintype n'] [semiring α] (x : α) (v : fin m → α) (w : n' → α) : vec_mul_vec (vec_cons x v) w = vec_cons (x • w) (vec_mul_vec v w) := sorry @[simp] theorem vec_mul_vec_cons {α : Type u} {n : ℕ} {m' : Type u_1} [fintype m'] [semiring α] (v : m' → α) (x : α) (w : fin n → α) : vec_mul_vec v (vec_cons x w) = fun (i : m') => v i • vec_cons x w := sorry @[simp] theorem smul_empty {α : Type u} [semiring α] (x : α) (v : fin 0 → α) : x • v = vec_empty := empty_eq (x • v) @[simp] theorem smul_mat_empty {α : Type u} [semiring α] {m' : Type u_1} (x : α) (A : fin 0 → m' → α) : x • A = vec_empty := empty_eq (x • A) @[simp] theorem smul_cons {α : Type u} {n : ℕ} [semiring α] (x : α) (y : α) (v : fin n → α) : x • vec_cons y v = vec_cons (x * y) (x • v) := sorry @[simp] theorem smul_mat_cons {α : Type u} {m : ℕ} {n' : Type u_2} [fintype n'] [semiring α] (x : α) (v : n' → α) (A : matrix (fin m) n' α) : x • vec_cons v A = vec_cons (x • v) (x • A) := sorry @[simp] theorem empty_add_empty {α : Type u} [Add α] (v : fin 0 → α) (w : fin 0 → α) : v + w = vec_empty := empty_eq (v + w) @[simp] theorem cons_add {α : Type u} {n : ℕ} [Add α] (x : α) (v : fin n → α) (w : fin (Nat.succ n) → α) : vec_cons x v + w = vec_cons (x + vec_head w) (v + vec_tail w) := sorry @[simp] theorem add_cons {α : Type u} {n : ℕ} [Add α] (v : fin (Nat.succ n) → α) (y : α) (w : fin n → α) : v + vec_cons y w = vec_cons (vec_head v + y) (vec_tail v + w) := sorry @[simp] theorem head_add {α : Type u} {n : ℕ} [Add α] (a : fin (Nat.succ n) → α) (b : fin (Nat.succ n) → α) : vec_head (a + b) = vec_head a + vec_head b := rfl @[simp] theorem tail_add {α : Type u} {n : ℕ} [Add α] (a : fin (Nat.succ n) → α) (b : fin (Nat.succ n) → α) : vec_tail (a + b) = vec_tail a + vec_tail b := rfl @[simp] theorem empty_sub_empty {α : Type u} [Sub α] (v : fin 0 → α) (w : fin 0 → α) : v - w = vec_empty := empty_eq (v - w) @[simp] theorem cons_sub {α : Type u} {n : ℕ} [Sub α] (x : α) (v : fin n → α) (w : fin (Nat.succ n) → α) : vec_cons x v - w = vec_cons (x - vec_head w) (v - vec_tail w) := sorry @[simp] theorem sub_cons {α : Type u} {n : ℕ} [Sub α] (v : fin (Nat.succ n) → α) (y : α) (w : fin n → α) : v - vec_cons y w = vec_cons (vec_head v - y) (vec_tail v - w) := sorry @[simp] theorem head_sub {α : Type u} {n : ℕ} [Sub α] (a : fin (Nat.succ n) → α) (b : fin (Nat.succ n) → α) : vec_head (a - b) = vec_head a - vec_head b := rfl @[simp] theorem tail_sub {α : Type u} {n : ℕ} [Sub α] (a : fin (Nat.succ n) → α) (b : fin (Nat.succ n) → α) : vec_tail (a - b) = vec_tail a - vec_tail b := rfl @[simp] theorem zero_empty {α : Type u} [HasZero α] : 0 = vec_empty := empty_eq 0 @[simp] theorem cons_zero_zero {α : Type u} {n : ℕ} [HasZero α] : vec_cons 0 0 = 0 := sorry @[simp] theorem head_zero {α : Type u} {n : ℕ} [HasZero α] : vec_head 0 = 0 := rfl @[simp] theorem tail_zero {α : Type u} {n : ℕ} [HasZero α] : vec_tail 0 = 0 := rfl @[simp] theorem cons_eq_zero_iff {α : Type u} {n : ℕ} [HasZero α] {v : fin n → α} {x : α} : vec_cons x v = 0 ↔ x = 0 ∧ v = 0 := sorry theorem cons_nonzero_iff {α : Type u} {n : ℕ} [HasZero α] {v : fin n → α} {x : α} : vec_cons x v ≠ 0 ↔ x ≠ 0 ∨ v ≠ 0 := { mp := fun (h : vec_cons x v ≠ 0) => iff.mp not_and_distrib (h ∘ iff.mpr cons_eq_zero_iff), mpr := fun (h : x ≠ 0 ∨ v ≠ 0) => mt (iff.mp cons_eq_zero_iff) (iff.mpr not_and_distrib h) } @[simp] theorem neg_empty {α : Type u} [Neg α] (v : fin 0 → α) : -v = vec_empty := empty_eq (-v) @[simp] theorem neg_cons {α : Type u} {n : ℕ} [Neg α] (x : α) (v : fin n → α) : -vec_cons x v = vec_cons (-x) (-v) := sorry @[simp] theorem head_neg {α : Type u} {n : ℕ} [Neg α] (a : fin (Nat.succ n) → α) : vec_head (-a) = -vec_head a := rfl @[simp] theorem tail_neg {α : Type u} {n : ℕ} [Neg α] (a : fin (Nat.succ n) → α) : vec_tail (-a) = -vec_tail a := rfl @[simp] theorem minor_empty {α : Type u} {m' : Type u_1} {n' : Type u_2} {o' : Type u_3} [fintype m'] [fintype n'] [fintype o'] (A : matrix m' n' α) (row : fin 0 → m') (col : o' → n') : minor A row col = vec_empty := empty_eq (minor A row col) @[simp] theorem minor_cons_row {α : Type u} {m : ℕ} {m' : Type u_1} {n' : Type u_2} {o' : Type u_3} [fintype m'] [fintype n'] [fintype o'] (A : matrix m' n' α) (i : m') (row : fin m → m') (col : o' → n') : minor A (vec_cons i row) col = vec_cons (fun (j : o') => A i (col j)) (minor A row col) := sorry
d5f2e69d6ce46e7d712f21fc4bda89e82329f38a
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/archive/100-theorems-list/82_cubing_a_cube.lean
cbfd9936ff2aec2d9badf251b7efc828414329b9
[ "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
23,055
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 import data.set.disjointed import data.set.intervals import 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 : (bcubes cs c).nonempty := begin rw [←set.ne_empty_iff_nonempty], intro h', have := two_le_mk_bcubes h v, rw h' at this, apply not_lt_of_le this, rw mk_emptyc, norm_cast, 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 := by simpa using (bcubes cs c).exists_min_image (λ 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_iff_val, 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 : s.nonempty, { 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_image 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
9907166387e0cc15f62415c2816b2abf90493760
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1132.lean
cc8c16ccccaf31691f2564d88a30d27df06529ee
[ "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
512
lean
class FooClass (α : Type) where mkFoo : {m : Type → Type} → [Monad m] → α → m Nat inductive Baz where | a | b instance : FooClass Baz where mkFoo := fun | .a => return 0 | .b => return 1 instance : FooClass Baz where mkFoo {m} _ := fun | .a => return 0 | .b => return 1 instance : FooClass Baz where mkFoo := fun a => match a with | .a => return 0 | .b => return 1 instance : FooClass Baz where mkFoo := @fun | _, _, .a => return 1 | _, _, .b => return 2
f3182f89b87066b460884d0e00ace8df506b3bf3
90bd8c2a52dbaaba588bdab57b155a7ec1532de0
/src/simply_connected/basic.lean
7c216ca553c53f6b7a44609356b7ca30151dbe01
[ "Apache-2.0" ]
permissive
shingtaklam1324/alg-top
fd434f1478925a232af45f18f370ee3a22811c54
4c88e28df6f0a329f26eab32bae023789193990e
refs/heads/master
1,689,447,024,947
1,630,073,400,000
1,630,073,400,000
381,528,689
2
0
null
null
null
null
UTF-8
Lean
false
false
815
lean
import homotopy_group.basic /-! # Simply Connected Spaces In this file, we define what it means for a path connected space to be simply connected. -/ variables {X : Type _} [topological_space X] [path_connected_space X] /-- A path connected space `X` is simply connected if there is a `x₀` such that `π₁ x₀` is trivial. -/ class simply_connected (X : Type _) [topological_space X] [path_connected_space X] : Prop := (hequiv_unit : ∃ x₀ : X, nonempty (π₁ x₀ ≃* unit)) lemma simply_connected_iff_forall : simply_connected X ↔ ∀ x₀ : X, nonempty (π₁ x₀ ≃* unit) := begin split, { rintros ⟨x, ⟨h⟩⟩ y, refine ⟨(π₁.mul_equiv_of_path_connected y x).trans h⟩ }, { intro h, constructor, use nonempty.some path_connected_space.nonempty, apply h } end
f5f2f437fa72c64102c619db442c30e5b0e55443
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Meta/Tactic/Apply.lean
81acd807e7a83b3dc082d1b74265e4f4bb84b010
[ "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
6,889
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.FindMVar import Lean.Meta.SynthInstance import Lean.Meta.CollectMVars import Lean.Meta.Tactic.Util namespace Lean.Meta /-- Compute the number of expected arguments and whether the result type is of the form (?m ...) where ?m is an unassigned metavariable. -/ def getExpectedNumArgsAux (e : Expr) : MetaM (Nat × Bool) := withDefault <| forallTelescopeReducing e fun xs body => pure (xs.size, body.getAppFn.isMVar) def getExpectedNumArgs (e : Expr) : MetaM Nat := do let (numArgs, _) ← getExpectedNumArgsAux e pure numArgs private def throwApplyError {α} (mvarId : MVarId) (eType : Expr) (targetType : Expr) : MetaM α := throwTacticEx `apply mvarId m!"failed to unify{indentExpr eType}\nwith{indentExpr targetType}" def synthAppInstances (tacticName : Name) (mvarId : MVarId) (newMVars : Array Expr) (binderInfos : Array BinderInfo) : MetaM Unit := newMVars.size.forM fun i => do if binderInfos[i]!.isInstImplicit then let mvar := newMVars[i]! let mvarType ← inferType mvar let mvarVal ← synthInstance mvarType unless (← isDefEq mvar mvarVal) do throwTacticEx tacticName mvarId "failed to assign synthesized instance" def appendParentTag (mvarId : MVarId) (newMVars : Array Expr) (binderInfos : Array BinderInfo) : MetaM Unit := do let parentTag ← mvarId.getTag if newMVars.size == 1 then -- if there is only one subgoal, we inherit the parent tag newMVars[0]!.mvarId!.setTag parentTag else unless parentTag.isAnonymous do newMVars.size.forM fun i => do let mvarIdNew := newMVars[i]!.mvarId! unless (← mvarIdNew.isAssigned) do unless binderInfos[i]!.isInstImplicit do let currTag ← mvarIdNew.getTag mvarIdNew.setTag (appendTag parentTag currTag) def postprocessAppMVars (tacticName : Name) (mvarId : MVarId) (newMVars : Array Expr) (binderInfos : Array BinderInfo) : MetaM Unit := do synthAppInstances tacticName mvarId newMVars binderInfos -- TODO: default and auto params appendParentTag mvarId newMVars binderInfos private def dependsOnOthers (mvar : Expr) (otherMVars : Array Expr) : MetaM Bool := otherMVars.anyM fun otherMVar => do if mvar == otherMVar then return false else let otherMVarType ← inferType otherMVar return (otherMVarType.findMVar? fun mvarId => mvarId == mvar.mvarId!).isSome /-- Partitions the given mvars in to two arrays (non-deps, deps) according to whether the given mvar depends on other mvars in the array.-/ private def partitionDependentMVars (mvars : Array Expr) : MetaM (Array MVarId × Array MVarId) := mvars.foldlM (init := (#[], #[])) fun (nonDeps, deps) mvar => do let currMVarId := mvar.mvarId! if (← dependsOnOthers mvar mvars) then return (nonDeps, deps.push currMVarId) else return (nonDeps.push currMVarId, deps) /-- Controls which new mvars are turned in to goals by the `apply` tactic. - `nonDependentFirst` mvars that don't depend on other goals appear first in the goal list. - `nonDependentOnly` only mvars that don't depend on other goals are added to goal list. - `all` all unassigned mvars are added to the goal list. -/ inductive ApplyNewGoals where | nonDependentFirst | nonDependentOnly | all private def reorderGoals (mvars : Array Expr) : ApplyNewGoals → MetaM (List MVarId) | ApplyNewGoals.nonDependentFirst => do let (nonDeps, deps) ← partitionDependentMVars mvars return nonDeps.toList ++ deps.toList | ApplyNewGoals.nonDependentOnly => do let (nonDeps, _) ← partitionDependentMVars mvars return nonDeps.toList | ApplyNewGoals.all => return mvars.toList.map Lean.Expr.mvarId! /-- Configures the behaviour of the `apply` tactic. -/ structure ApplyConfig where newGoals := ApplyNewGoals.nonDependentFirst /-- Close the give goal using `apply e`. -/ def _root_.Lean.MVarId.apply (mvarId : MVarId) (e : Expr) (cfg : ApplyConfig := {}) : MetaM (List MVarId) := mvarId.withContext do mvarId.checkNotAssigned `apply let targetType ← mvarId.getType let eType ← inferType e let mut (numArgs, hasMVarHead) ← getExpectedNumArgsAux eType unless hasMVarHead do let targetTypeNumArgs ← getExpectedNumArgs targetType numArgs := numArgs - targetTypeNumArgs let (newMVars, binderInfos, eType) ← forallMetaTelescopeReducing eType (some numArgs) unless (← isDefEq eType targetType) do throwApplyError mvarId eType targetType postprocessAppMVars `apply mvarId newMVars binderInfos let e ← instantiateMVars e mvarId.assign (mkAppN e newMVars) let newMVars ← newMVars.filterM fun mvar => not <$> mvar.mvarId!.isAssigned let otherMVarIds ← getMVarsNoDelayed e let newMVarIds ← reorderGoals newMVars cfg.newGoals let otherMVarIds := otherMVarIds.filter fun mvarId => !newMVarIds.contains mvarId let result := newMVarIds ++ otherMVarIds.toList result.forM (·.headBetaType) return result @[deprecated MVarId.apply] def apply (mvarId : MVarId) (e : Expr) (cfg : ApplyConfig := {}) : MetaM (List MVarId) := mvarId.apply e cfg partial def splitAndCore (mvarId : MVarId) : MetaM (List MVarId) := mvarId.withContext do mvarId.checkNotAssigned `splitAnd let type ← mvarId.getType' if !type.isAppOfArity ``And 2 then return [mvarId] else let tag ← mvarId.getTag let rec go (type : Expr) : StateRefT (Array MVarId) MetaM Expr := do let type ← whnf type if type.isAppOfArity ``And 2 then let p₁ := type.appFn!.appArg! let p₂ := type.appArg! return mkApp4 (mkConst ``And.intro) p₁ p₂ (← go p₁) (← go p₂) else let idx := (← get).size + 1 let mvar ← mkFreshExprSyntheticOpaqueMVar type (tag ++ (`h).appendIndexAfter idx) modify fun s => s.push mvar.mvarId! return mvar let (val, s) ← go type |>.run #[] mvarId.assign val return s.toList /-- Apply `And.intro` as much as possible to goal `mvarId`. -/ abbrev _root_.Lean.MVarId.splitAnd (mvarId : MVarId) : MetaM (List MVarId) := splitAndCore mvarId @[deprecated MVarId.splitAnd] def splitAnd (mvarId : MVarId) : MetaM (List MVarId) := mvarId.splitAnd def _root_.Lean.MVarId.exfalso (mvarId : MVarId) : MetaM MVarId := mvarId.withContext do mvarId.checkNotAssigned `exfalso let target ← instantiateMVars (← mvarId.getType) let u ← getLevel target let mvarIdNew ← mkFreshExprSyntheticOpaqueMVar (mkConst ``False) (tag := (← mvarId.getTag)) mvarId.assign (mkApp2 (mkConst ``False.elim [u]) target mvarIdNew) return mvarIdNew.mvarId! end Lean.Meta
a66c87d367a6b9549d1756ec1d6a0d31030c86e7
64874bd1010548c7f5a6e3e8902efa63baaff785
/tests/lean/run/match4.lean
4aa55c10da67be655c533fd7f41cd9f64afe6b1a
[ "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
518
lean
open nat bool inhabited prod definition diag (a b c : bool) : nat := match (a, b, c) with (_, tt, ff) := 1, (ff, _, tt) := 2, (tt, ff, _) := 3, (_, _, _) := arbitrary nat end theorem diag1 (a : bool) : diag a tt ff = 1 := bool.cases_on a rfl rfl theorem diag2 (a : bool) : diag ff a tt = 2 := bool.cases_on a rfl rfl theorem diag3 (a : bool) : diag tt ff a = 3 := bool.cases_on a rfl rfl theorem diag4_1 : diag ff ff ff = arbitrary nat := rfl theorem diag4_2 : diag tt tt tt = arbitrary nat := rfl
170ec621279f6fb0f38c275fd79c0e4b8cffc996
54c9ed381c63410c9b6af3b0a1722c41152f037f
/Binport/ParseExport.lean
56d91d1ab9f9f50812789fd6ca48d499a4dd611c
[ "Apache-2.0" ]
permissive
dselsam/binport
0233f1aa961a77c4fc96f0dccc780d958c5efc6c
aef374df0e169e2c3f1dc911de240c076315805c
refs/heads/master
1,687,453,448,108
1,627,483,296,000
1,627,483,296,000
333,825,622
0
0
null
null
null
null
UTF-8
Lean
false
false
10,756
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Daniel Selsam, Gabriel Ebner -/ import Binport.Util import Binport.Basic import Binport.ActionItem import Lean import Std.Data.HashSet import Std.Data.HashMap namespace Binport open Lean private def nat2expr (i : Nat) : PortM Expr := do let s ← get if i < s.exprs.size then return s.exprs[i] throwError s!"[nat2expr] {i} > {s.exprs.size}" private def nat2level (i : Nat) : PortM Level := do let s ← get if i < s.levels.size then return s.levels[i] throwError s!"[nat2level] {i} > {s.levels.size}" private def nat2name (i : Nat) : PortM Name := do let s ← get if i < s.names.size then return s.names[i] throwError s!"[nat2name] {i} > {s.names.size}" private def parseNat (s : String) : PortM Nat := match s.toNat? with | some k => pure k | none => throwError s!"String '{s}' cannot be converted to Nat" private def parseBool (s : String) : PortM Bool := match s.toNat? with | some k => if k == 1 then true else if k == 0 then false else throwError s!"String '{s}' cannot be converted to Bool" | none => throwError s!"String '{s}' cannot be converted to Bool" private def parseHints (s : String) : PortM ReducibilityHints := do match s with | "A" => ReducibilityHints.abbrev | "O" => ReducibilityHints.opaque | _ => match s.splitOn "." with | [height, selfOpt] => let height ← parseNat height let selfOpt ← parseBool selfOpt if h : height < UInt32.size then ReducibilityHints.regular ⟨⟨height, h⟩⟩ else throwError s!"Reducibility hint too large {height}" | _ => throwError s!"failed to parse reducibility hint: {s}" private def parseMixfixKind (kind : String) : PortM MixfixKind := match kind with | "prefix" => pure MixfixKind.prefix | "postfix" => pure MixfixKind.postfix | "infixl" => pure MixfixKind.infixl | "infixr" => pure MixfixKind.infixr | "singleton" => pure MixfixKind.singleton | _ => throwError s!"invalid mixfix kind {kind}" private def str2expr (s : String) : PortM Expr := parseNat s >>= nat2expr private def str2level (s : String) : PortM Level := parseNat s >>= nat2level private def str2name (s : String) : PortM Name := parseNat s >>= nat2name private def writeName (i : String) (n : Name) : PortM Unit := do let i ← parseNat i modify $ λ s => { s with names := s.names.write i n } private def writeLevel (i : String) (l : Level) : PortM Unit := do let i ← parseNat i modify $ λ s => { s with levels := s.levels.write i l } private def writeExpr (i : String) (e : Expr) : PortM Unit := do let i ← parseNat i modify $ λ s => { s with exprs := s.exprs.write i e } private def parseReducibilityStatus : String → PortM ReducibilityStatus | "Reducible" => ReducibilityStatus.reducible | "Semireducible" => ReducibilityStatus.semireducible | "Irreducible" => ReducibilityStatus.irreducible | s => throwError s!"unknown reducibility status {s}" def processLine (line : String) : PortM (List ActionItem) := do let tokens := line.splitOn " " match tokens with | [] => throwError "[processLine] line has no tokens" | (t::_) => if t.isNat then processTerm tokens *> pure [] else processMisc tokens where processTerm (tokens : List String) : PortM Unit := do match tokens with | (i :: "#NS" :: j :: rest) => writeName i $ (← str2name j).mkStr (" ".intercalate rest) | [i, "#NI", j, k] => writeName i $ (← str2name j).mkNum (← parseNat k) | [i, "#US", j] => writeLevel i $ mkLevelSucc (← str2level j) | [i, "#UM", j₁, j₂] => writeLevel i $ mkLevelMax (← str2level j₁) (← str2level j₂) | [i, "#UIM", j₁, j₂] => writeLevel i $ mkLevelIMax (← str2level j₁) (← str2level j₂) | [i, "#UP", j] => writeLevel i $ mkLevelParam (← str2name j) | [i, "#EV", j] => writeExpr i $ mkBVar (← parseNat j) | [i, "#ES", j] => writeExpr i $ mkSort (← str2level j) | (i :: "#EC" :: j :: us) => writeExpr i $ mkConst (← str2name j) (← us.mapM str2level) | [i, "#EA", j₁, j₂] => writeExpr i $ mkApp (← str2expr j₁) (← str2expr j₂) | [i, "#EL", bi, j₁, j₂, j₃] => writeExpr i $ mkLambda (← str2name j₁) (← parseBinderInfo bi) (← str2expr j₂) (← str2expr j₃) | [i, "#EP", bi, j₁, j₂, j₃] => writeExpr i $ mkForall (← str2name j₁) (← parseBinderInfo bi) (← str2expr j₂) (← str2expr j₃) | [i, "#EZ", j₁, j₂, j₃, j₄] => writeExpr i $ mkLet (← str2name j₁) (← str2expr j₂) (← str2expr j₃) (← str2expr j₄) | [i, "#PROJ_MACRO", iName, cName, pName, idx, arg] => let (iName, idx, arg) := (← str2name iName, ← parseNat idx, ← str2expr arg) let some numParams ← (← get).ind2params.find? iName | throwError "projection type {iName} not found" println! "[PROJ_MACRO] {iName} {idx - numParams} {arg}" writeExpr i $ mkProj iName (idx - numParams) arg | _ => throwError s!"[processTerm] unexpected '{tokens}'" processMisc (tokens : List String) : PortM (List ActionItem) := do match tokens with | ("#AX" :: n :: t :: ups) => let (n, t, ups) ← ((← str2name n), (← str2expr t), (← ups.mapM str2name)) pure [ActionItem.decl $ Declaration.axiomDecl { name := n, levelParams := ups, type := t, isUnsafe := false, }] | ("#DEF" :: n :: thm :: h :: t :: v :: ups) => let (n, h, t, v, ups) ← ((← str2name n), (← parseHints h), (← str2expr t), (← str2expr v), (← ups.mapM str2name)) let thm ← parseNat thm -- TODO: why can't I synthesize `thm > 0` any more? if Nat.ble 1 thm then pure [ActionItem.decl $ Declaration.thmDecl { name := n, levelParams := ups, type := t, value := v }] else pure [ActionItem.decl $ Declaration.defnDecl { name := n, levelParams := ups, type := t, value := v, safety := DefinitionSafety.safe, -- TODO: confirm only safe things are being exported hints := h, }] | ("#IND" :: nps :: n :: t :: nis :: rest) => let (nps, n, t, nis) ← ((← parseNat nps), (← str2name n), (← str2expr t), (← parseNat nis)) let (is, ups) := rest.splitAt (2 * nis) let lparams ← ups.mapM str2name let ctors ← parseIntros is modify fun s => { s with ind2params := s.ind2params.insert n nps } pure [ActionItem.decl $ Declaration.inductDecl lparams nps [{ name := n, type := t, ctors := ctors }] false] | ["#QUOT"] => pure [] | ("#MIXFIX" :: kind :: n :: prec :: tok) => pure [ActionItem.mixfix (← parseMixfixKind kind) (← str2name n) (← parseNat prec) (" ".intercalate tok)] | ["#PRIVATE", pretty, real] => pure [ActionItem.private (← str2name pretty) (← str2name real)] | ["#PROTECTED", n] => pure [ActionItem.protected (← str2name n)] | ("#POS_INFO" :: _) => pure [] -- TODO: look at the 'deleted' bit | ("#ATTR" :: a :: p :: n :: _ :: rest) => do let attrName ← str2name a if attrName == "simp" then pure [ActionItem.simp (← str2name n) (← parseNat p)] else if attrName == "reducibility" then match rest with | [status] => pure [ActionItem.reducibility (← str2name n) (← parseReducibilityStatus status)] | _ => throwError s!"[reducibility] expected name" else pure [] | ["#CLASS", c] => pure [ActionItem.class (← str2name c)] | ["#CLASS_INSTANCE", c, i, p] => pure [ActionItem.instance (← str2name c) (← str2name i) (← parseNat p)] | ("#CLASS_TRACK_ATTR" :: _) => pure [] | ("#AUXREC" :: _) => pure [] | ("#NEW_NAMESPACE" :: _) => pure [] | ("#NONCOMPUTABLE" :: _) => pure [] | ("#NOCONF" :: _) => pure [] | ("#TOKEN" :: _) => pure [] | ("#USER_ATTR" :: _) => pure [] | ["#PROJECTION", proj, mk, nParams, i, ii] => do pure [ActionItem.projection { projName := ← str2name proj, ctorName := ← str2name mk, nParams := ← parseNat nParams, index := ← parseNat i, fromClass := ← parseBool ii }] | ("#EXPORT_DECL" :: currNs :: ns :: nsAs :: hadExplicit :: nRenames :: rest) => do let rest := rest.toArray let nRenames ← parseNat nRenames let mut renames := #[] for i in [:nRenames] do let n1 ← str2name rest[2*i] let n2 ← str2name rest[2*i+1] renames := renames.push (n1, n2) let nExcepts ← parseNat rest[2*nRenames] let offset := (2 * nRenames + 1) let mut exceptNames := #[] for i in [:nExcepts] do exceptNames := exceptNames.push $ ← str2name rest[offset + i] let exportDecl : ExportDecl := { currNs := (← str2name currNs), ns := (← str2name ns), nsAs := (← str2name nsAs), hadExplicit := (← parseNat hadExplicit) > 0, renames := renames, exceptNames := exceptNames } pure [ActionItem.export exportDecl] | _ => println! "[processLine] unexpected case: '{line}'\n{tokens}" pure [] parseIntros : List String → PortM (List Constructor) | (n :: t :: is) => do let rest ← parseIntros is pure $ { name := (← str2name n), type := ← str2expr t } :: rest | _ => [] parseBinderInfo : String → PortM BinderInfo | "#BD" => BinderInfo.default | "#BI" => BinderInfo.implicit | "#BS" => -- Lean4 is missing support for strictImplicit, so we convert here BinderInfo.implicit -- BinderInfo.strictImplicit | "#BC" => BinderInfo.instImplicit | s => throwError s!"[parseBinderInfo] unexpected: {s}" end Binport
bd4485b1298bac92de8a55f55f7c5818d8350c92
efce24474b28579aba3272fdb77177dc2b11d7aa
/src/homotopy_theory/topological_spaces/closed_t1_inclusion.lean
87a2d4aff6bfaa49670ef8ac87c029bae69a2e72
[ "Apache-2.0" ]
permissive
rwbarton/lean-homotopy-theory
cff499f24268d60e1c546e7c86c33f58c62888ed
39e1b4ea1ed1b0eca2f68bc64162dde6a6396dee
refs/heads/lean-3.4.2
1,622,711,883,224
1,598,550,958,000
1,598,550,958,000
136,023,667
12
6
Apache-2.0
1,573,187,573,000
1,528,116,262,000
Lean
UTF-8
Lean
false
false
3,037
lean
import .pushout_lemmas local attribute [instance] classical.prop_decidable universe u open category_theory set namespace homotopy_theory.topological_spaces open homotopy_theory.topological_spaces.Top local notation `Top` := Top.{u} def closed_t1_inclusion {A X : Top} (i : A ⟶ X) : Prop := embedding i ∧ is_closed (set.range i) ∧ ∀ x, x ∉ set.range i → is_closed ({x} : set X) lemma closed_t1_inclusion_of_closed_embedding_t1 {A X : Top} [t1_space X] (i : A ⟶ X) (h₁ : embedding i) (h₂ : is_closed (set.range i)) : closed_t1_inclusion i := ⟨h₁, h₂, λ x _, is_closed_singleton⟩ lemma closed_t1_inclusion_id {X : Top} : closed_t1_inclusion (𝟙 X) := ⟨embedding_id, by convert is_closed_univ; exact range_id, λ x hx, false.elim (hx ⟨x, rfl⟩)⟩ lemma closed_t1_inclusion_comp {X Y Z : Top} {f : X ⟶ Y} {g : Y ⟶ Z} (hf : closed_t1_inclusion f) (hg : closed_t1_inclusion g) : closed_t1_inclusion (f ≫ g) := ⟨hg.1.comp hf.1, by convert embedding_is_closed hg.1 hg.2.1 hf.2.1; apply range_comp, λ z hz, if hz' : z ∈ range g then let ⟨y, hy⟩ := hz' in have y ∉ range f, from λ hy', hz (by erw range_comp; exact ⟨y, hy', hy⟩), by convert embedding_is_closed hg.1 hg.2.1 (hf.2.2 y this); rw ←hy; simp else hg.2.2 z hz'⟩ section parameters {A B X Y : Top} {i : A ⟶ X} {f : A ⟶ B} {g : X ⟶ Y} {j : B ⟶ Y} parameter (po : Is_pushout i f g j) include po lemma closed_t1_inclusion_of_pushout (h : closed_t1_inclusion i) : closed_t1_inclusion j := ⟨embedding_j_of_embedding_i po h.1, (range_i_closed_iff_range_j_closed po).mp h.2.1, λ y hy, begin rw is_closed_in_pushout po, split, { let g' := complement_homeomorphism po (or.inl h.2.1), let x := g'.inv ⟨y, hy⟩, suffices : g ⁻¹' {y} = {x.val}, { rw this, apply h.2.2 x.val x.property }, have g'x : g'.hom x = ⟨y, hy⟩, { ext, change (g'.inv ≫ g'.hom ≫ incl _) ⟨y, hy⟩ = y, simp, refl }, suffices : ∀ x', g x' = y → x' = x.val, { ext x', rw [mem_preimage, mem_singleton_iff, mem_singleton_iff], refine ⟨this x', _⟩, rintro rfl, convert Top.hom_congr (complement_homeomorphism_eq po (or.inl h.2.1)).symm x, exact congr_arg subtype.val g'x.symm }, intros x' hx', have : x' ∉ range i, { rintro ⟨a, rfl⟩, change (i ≫ g) a = y at hx', rw po.commutes at hx', exact hy ⟨f a, hx'⟩ }, have : g'.hom ⟨x', this⟩ = ⟨y, hy⟩, { ext, convert ←Top.hom_congr (complement_homeomorphism_eq po (or.inl h.2.1)).symm ⟨x', this⟩ }, have : g'.hom ⟨x', _⟩ = g'.hom x, from this.trans g'x.symm, exact congr_arg subtype.val ((homeomorphism.embedding g').inj this) }, { convert is_closed_empty, rw ←preimage_inter_range, convert preimage_empty, -- FIXME: Used to be `rwa singleton_inter_eq_empty` exact singleton_inter_eq_empty.mpr hy } end⟩ end end homotopy_theory.topological_spaces
3603e58b26cf12b15325101fe679cab29fc7a81e
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/group_with_zero/basic.lean
9d7c48dd57d3a5770b620cfd1fe2445cd492284f
[ "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
46,586
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 logic.nontrivial import algebra.group.units_hom import algebra.group.inj_surj import algebra.group_with_zero.defs /-! # Groups with an adjoined zero element This file describes structures that are not usually studied on their own right in mathematics, namely a special sort of monoid: apart from a distinguished “zero element” they form a group, or in other words, they are groups with an adjoined zero element. Examples are: * division rings; * the value monoid of a multiplicative valuation; * in particular, the non-negative real numbers. ## Main definitions Various lemmas about `group_with_zero` and `comm_group_with_zero`. To reduce import dependencies, the type-classes themselves are in `algebra.group_with_zero.defs`. ## Implementation details As is usual in mathlib, we extend the inverse function to the zero element, and require `0⁻¹ = 0`. -/ set_option old_structure_cmd true open_locale classical open function variables {M₀ G₀ M₀' G₀' : Type*} mk_simp_attribute field_simps "The simpset `field_simps` is used by the tactic `field_simp` to reduce an expression in a field to an expression of the form `n / d` where `n` and `d` are division-free." attribute [field_simps] mul_div_assoc' section section mul_zero_class variables [mul_zero_class M₀] {a b : M₀} /-- Pullback a `mul_zero_class` instance along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.mul_zero_class [has_mul M₀'] [has_zero M₀'] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (mul : ∀ a b, f (a * b) = f a * f b) : mul_zero_class M₀' := { mul := (*), zero := 0, zero_mul := λ a, hf $ by simp only [mul, zero, zero_mul], mul_zero := λ a, hf $ by simp only [mul, zero, mul_zero] } /-- Pushforward a `mul_zero_class` instance along an surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.mul_zero_class [has_mul M₀'] [has_zero M₀'] (f : M₀ → M₀') (hf : surjective f) (zero : f 0 = 0) (mul : ∀ a b, f (a * b) = f a * f b) : mul_zero_class M₀' := { mul := (*), zero := 0, mul_zero := hf.forall.2 $ λ x, by simp only [← zero, ← mul, mul_zero], zero_mul := hf.forall.2 $ λ x, by simp only [← zero, ← mul, zero_mul] } lemma mul_eq_zero_of_left (h : a = 0) (b : M₀) : a * b = 0 := h.symm ▸ zero_mul b lemma mul_eq_zero_of_right (a : M₀) (h : b = 0) : a * b = 0 := h.symm ▸ mul_zero a lemma left_ne_zero_of_mul : a * b ≠ 0 → a ≠ 0 := mt (λ h, mul_eq_zero_of_left h b) lemma right_ne_zero_of_mul : a * b ≠ 0 → b ≠ 0 := mt (mul_eq_zero_of_right a) lemma ne_zero_and_ne_zero_of_mul (h : a * b ≠ 0) : a ≠ 0 ∧ b ≠ 0 := ⟨left_ne_zero_of_mul h, right_ne_zero_of_mul h⟩ lemma mul_eq_zero_of_ne_zero_imp_eq_zero {a b : M₀} (h : a ≠ 0 → b = 0) : a * b = 0 := if ha : a = 0 then by rw [ha, zero_mul] else by rw [h ha, mul_zero] /-- To match `one_mul_eq_id`. -/ lemma zero_mul_eq_const : ((*) (0 : M₀)) = function.const _ 0 := funext zero_mul /-- To match `mul_one_eq_id`. -/ lemma mul_zero_eq_const : (* (0 : M₀)) = function.const _ 0 := funext mul_zero end mul_zero_class /-- Pushforward a `no_zero_divisors` instance along an injective function. -/ protected lemma function.injective.no_zero_divisors [has_mul M₀] [has_zero M₀] [has_mul M₀'] [has_zero M₀'] [no_zero_divisors M₀'] (f : M₀ → M₀') (hf : injective f) (zero : f 0 = 0) (mul : ∀ x y, f (x * y) = f x * f y) : no_zero_divisors M₀ := { eq_zero_or_eq_zero_of_mul_eq_zero := λ x y H, have f x * f y = 0, by rw [← mul, H, zero], (eq_zero_or_eq_zero_of_mul_eq_zero this).imp (λ H, hf $ by rwa zero) (λ H, hf $ by rwa zero) } lemma eq_zero_of_mul_self_eq_zero [has_mul M₀] [has_zero M₀] [no_zero_divisors M₀] {a : M₀} (h : a * a = 0) : a = 0 := (eq_zero_or_eq_zero_of_mul_eq_zero h).elim id id section variables [mul_zero_class M₀] [no_zero_divisors M₀] {a b : M₀} /-- If `α` has no zero divisors, then the product of two elements equals zero iff one of them equals zero. -/ @[simp] theorem mul_eq_zero : a * b = 0 ↔ a = 0 ∨ b = 0 := ⟨eq_zero_or_eq_zero_of_mul_eq_zero, λo, o.elim (λ h, mul_eq_zero_of_left h b) (mul_eq_zero_of_right a)⟩ /-- If `α` has no zero divisors, then the product of two elements equals zero iff one of them equals zero. -/ @[simp] theorem zero_eq_mul : 0 = a * b ↔ a = 0 ∨ b = 0 := by rw [eq_comm, mul_eq_zero] /-- If `α` has no zero divisors, then the product of two elements is nonzero iff both of them are nonzero. -/ theorem mul_ne_zero_iff : a * b ≠ 0 ↔ a ≠ 0 ∧ b ≠ 0 := (not_congr mul_eq_zero).trans not_or_distrib @[field_simps] theorem mul_ne_zero (ha : a ≠ 0) (hb : b ≠ 0) : a * b ≠ 0 := mul_ne_zero_iff.2 ⟨ha, hb⟩ /-- If `α` has no zero divisors, then for elements `a, b : α`, `a * b` equals zero iff so is `b * a`. -/ theorem mul_eq_zero_comm : a * b = 0 ↔ b * a = 0 := mul_eq_zero.trans $ (or_comm _ _).trans mul_eq_zero.symm /-- If `α` has no zero divisors, then for elements `a, b : α`, `a * b` is nonzero iff so is `b * a`. -/ theorem mul_ne_zero_comm : a * b ≠ 0 ↔ b * a ≠ 0 := not_congr mul_eq_zero_comm lemma mul_self_eq_zero : a * a = 0 ↔ a = 0 := by simp lemma zero_eq_mul_self : 0 = a * a ↔ a = 0 := by simp end end section variables [mul_zero_one_class M₀] /-- Pullback a `mul_zero_one_class` instance along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.mul_zero_one_class [has_mul M₀'] [has_zero M₀'] [has_one M₀'] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ a b, f (a * b) = f a * f b) : mul_zero_one_class M₀' := { ..hf.mul_zero_class f zero mul, ..hf.mul_one_class f one mul } /-- Pushforward a `mul_zero_one_class` instance along an surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.mul_zero_one_class [has_mul M₀'] [has_zero M₀'] [has_one M₀'] (f : M₀ → M₀') (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ a b, f (a * b) = f a * f b) : mul_zero_one_class M₀' := { ..hf.mul_zero_class f zero mul, ..hf.mul_one_class f one mul } /-- In a monoid with zero, if zero equals one, then zero is the only element. -/ lemma eq_zero_of_zero_eq_one (h : (0 : M₀) = 1) (a : M₀) : a = 0 := by rw [← mul_one a, ← h, mul_zero] /-- In a monoid with zero, if zero equals one, then zero is the unique element. Somewhat arbitrarily, we define the default element to be `0`. All other elements will be provably equal to it, but not necessarily definitionally equal. -/ def unique_of_zero_eq_one (h : (0 : M₀) = 1) : unique M₀ := { default := 0, uniq := eq_zero_of_zero_eq_one h } /-- In a monoid with zero, zero equals one if and only if all elements of that semiring are equal. -/ theorem subsingleton_iff_zero_eq_one : (0 : M₀) = 1 ↔ subsingleton M₀ := ⟨λ h, @unique.subsingleton _ (unique_of_zero_eq_one h), λ h, @subsingleton.elim _ h _ _⟩ alias subsingleton_iff_zero_eq_one ↔ subsingleton_of_zero_eq_one _ lemma eq_of_zero_eq_one (h : (0 : M₀) = 1) (a b : M₀) : a = b := @subsingleton.elim _ (subsingleton_of_zero_eq_one h) a b /-- In a monoid with zero, either zero and one are nonequal, or zero is the only element. -/ lemma zero_ne_one_or_forall_eq_0 : (0 : M₀) ≠ 1 ∨ (∀a:M₀, a = 0) := not_or_of_imp eq_zero_of_zero_eq_one end section variables [mul_zero_one_class M₀] [nontrivial M₀] {a b : M₀} /-- In a nontrivial monoid with zero, zero and one are different. -/ @[simp] lemma zero_ne_one : 0 ≠ (1:M₀) := begin assume h, rcases exists_pair_ne M₀ with ⟨x, y, hx⟩, apply hx, calc x = 1 * x : by rw [one_mul] ... = 0 : by rw [← h, zero_mul] ... = 1 * y : by rw [← h, zero_mul] ... = y : by rw [one_mul] end @[simp] lemma one_ne_zero : (1:M₀) ≠ 0 := zero_ne_one.symm lemma ne_zero_of_eq_one {a : M₀} (h : a = 1) : a ≠ 0 := calc a = 1 : h ... ≠ 0 : one_ne_zero lemma left_ne_zero_of_mul_eq_one (h : a * b = 1) : a ≠ 0 := left_ne_zero_of_mul $ ne_zero_of_eq_one h lemma right_ne_zero_of_mul_eq_one (h : a * b = 1) : b ≠ 0 := right_ne_zero_of_mul $ ne_zero_of_eq_one h /-- Pullback a `nontrivial` instance along a function sending `0` to `0` and `1` to `1`. -/ protected lemma pullback_nonzero [has_zero M₀'] [has_one M₀'] (f : M₀' → M₀) (zero : f 0 = 0) (one : f 1 = 1) : nontrivial M₀' := ⟨⟨0, 1, mt (congr_arg f) $ by { rw [zero, one], exact zero_ne_one }⟩⟩ end section semigroup_with_zero /-- Pullback a `semigroup_with_zero` class along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.semigroup_with_zero [has_zero M₀'] [has_mul M₀'] [semigroup_with_zero M₀] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (mul : ∀ x y, f (x * y) = f x * f y) : semigroup_with_zero M₀' := { .. hf.mul_zero_class f zero mul, .. ‹has_zero M₀'›, .. hf.semigroup f mul } /-- Pushforward a `semigroup_with_zero` class along an surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.semigroup_with_zero [semigroup_with_zero M₀] [has_zero M₀'] [has_mul M₀'] (f : M₀ → M₀') (hf : surjective f) (zero : f 0 = 0) (mul : ∀ x y, f (x * y) = f x * f y) : semigroup_with_zero M₀' := { .. hf.mul_zero_class f zero mul, .. ‹has_zero M₀'›, .. hf.semigroup f mul } end semigroup_with_zero section monoid_with_zero /-- Pullback a `monoid_with_zero` class along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] [monoid_with_zero M₀] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : monoid_with_zero M₀' := { .. hf.monoid f one mul, .. hf.mul_zero_class f zero mul } /-- Pushforward a `monoid_with_zero` class along a surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] [monoid_with_zero M₀] (f : M₀ → M₀') (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : monoid_with_zero M₀' := { .. hf.monoid f one mul, .. hf.mul_zero_class f zero mul } /-- Pullback a `monoid_with_zero` class along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.comm_monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] [comm_monoid_with_zero M₀] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : comm_monoid_with_zero M₀' := { .. hf.comm_monoid f one mul, .. hf.mul_zero_class f zero mul } /-- Pushforward a `monoid_with_zero` class along a surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.comm_monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] [comm_monoid_with_zero M₀] (f : M₀ → M₀') (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : comm_monoid_with_zero M₀' := { .. hf.comm_monoid f one mul, .. hf.mul_zero_class f zero mul } variables [monoid_with_zero M₀] namespace units /-- An element of the unit group of a nonzero monoid with zero represented as an element of the monoid is nonzero. -/ @[simp] lemma ne_zero [nontrivial M₀] (u : M₀ˣ) : (u : M₀) ≠ 0 := left_ne_zero_of_mul_eq_one u.mul_inv -- We can't use `mul_eq_zero` + `units.ne_zero` in the next two lemmas because we don't assume -- `nonzero M₀`. @[simp] lemma mul_left_eq_zero (u : M₀ˣ) {a : M₀} : a * u = 0 ↔ a = 0 := ⟨λ h, by simpa using mul_eq_zero_of_left h ↑u⁻¹, λ h, mul_eq_zero_of_left h u⟩ @[simp] lemma mul_right_eq_zero (u : M₀ˣ) {a : M₀} : ↑u * a = 0 ↔ a = 0 := ⟨λ h, by simpa using mul_eq_zero_of_right ↑u⁻¹ h, mul_eq_zero_of_right u⟩ end units namespace is_unit lemma ne_zero [nontrivial M₀] {a : M₀} (ha : is_unit a) : a ≠ 0 := let ⟨u, hu⟩ := ha in hu ▸ u.ne_zero lemma mul_right_eq_zero {a b : M₀} (ha : is_unit a) : a * b = 0 ↔ b = 0 := let ⟨u, hu⟩ := ha in hu ▸ u.mul_right_eq_zero lemma mul_left_eq_zero {a b : M₀} (hb : is_unit b) : a * b = 0 ↔ a = 0 := let ⟨u, hu⟩ := hb in hu ▸ u.mul_left_eq_zero end is_unit @[simp] theorem is_unit_zero_iff : is_unit (0 : M₀) ↔ (0:M₀) = 1 := ⟨λ ⟨⟨_, a, (a0 : 0 * a = 1), _⟩, rfl⟩, by rwa zero_mul at a0, λ h, @is_unit_of_subsingleton _ _ (subsingleton_of_zero_eq_one h) 0⟩ @[simp] theorem not_is_unit_zero [nontrivial M₀] : ¬ is_unit (0 : M₀) := mt is_unit_zero_iff.1 zero_ne_one namespace ring open_locale classical /-- Introduce a function `inverse` on a monoid with zero `M₀`, which sends `x` to `x⁻¹` if `x` is invertible and to `0` otherwise. This definition is somewhat ad hoc, but one needs a fully (rather than partially) defined inverse function for some purposes, including for calculus. Note that while this is in the `ring` namespace for brevity, it requires the weaker assumption `monoid_with_zero M₀` instead of `ring M₀`. -/ noncomputable def inverse : M₀ → M₀ := λ x, if h : is_unit x then ((h.unit⁻¹ : M₀ˣ) : M₀) else 0 /-- By definition, if `x` is invertible then `inverse x = x⁻¹`. -/ @[simp] lemma inverse_unit (u : M₀ˣ) : inverse (u : M₀) = (u⁻¹ : M₀ˣ) := begin simp only [units.is_unit, inverse, dif_pos], exact units.inv_unique rfl end /-- By definition, if `x` is not invertible then `inverse x = 0`. -/ @[simp] lemma inverse_non_unit (x : M₀) (h : ¬(is_unit x)) : inverse x = 0 := dif_neg h lemma mul_inverse_cancel (x : M₀) (h : is_unit x) : x * inverse x = 1 := by { rcases h with ⟨u, rfl⟩, rw [inverse_unit, units.mul_inv], } lemma inverse_mul_cancel (x : M₀) (h : is_unit x) : inverse x * x = 1 := by { rcases h with ⟨u, rfl⟩, rw [inverse_unit, units.inv_mul], } lemma mul_inverse_cancel_right (x y : M₀) (h : is_unit x) : y * x * inverse x = y := by rw [mul_assoc, mul_inverse_cancel x h, mul_one] lemma inverse_mul_cancel_right (x y : M₀) (h : is_unit x) : y * inverse x * x = y := by rw [mul_assoc, inverse_mul_cancel x h, mul_one] lemma mul_inverse_cancel_left (x y : M₀) (h : is_unit x) : x * (inverse x * y) = y := by rw [← mul_assoc, mul_inverse_cancel x h, one_mul] lemma inverse_mul_cancel_left (x y : M₀) (h : is_unit x) : inverse x * (x * y) = y := by rw [← mul_assoc, inverse_mul_cancel x h, one_mul] variables (M₀) @[simp] lemma inverse_one : inverse (1 : M₀) = 1 := inverse_unit 1 @[simp] lemma inverse_zero : inverse (0 : M₀) = 0 := by { nontriviality, exact inverse_non_unit _ not_is_unit_zero } variables {M₀} lemma mul_inverse_rev' {a b : M₀} (h : commute a b) : inverse (a * b) = inverse b * inverse a := begin by_cases hab : is_unit (a * b), { obtain ⟨⟨a, rfl⟩, b, rfl⟩ := h.is_unit_mul_iff.mp hab, rw [←units.coe_mul, inverse_unit, inverse_unit, inverse_unit, ←units.coe_mul, mul_inv_rev], }, obtain ha | hb := not_and_distrib.mp (mt h.is_unit_mul_iff.mpr hab), { rw [inverse_non_unit _ hab, inverse_non_unit _ ha, mul_zero]}, { rw [inverse_non_unit _ hab, inverse_non_unit _ hb, zero_mul]}, end lemma mul_inverse_rev {M₀} [comm_monoid_with_zero M₀] (a b : M₀) : ring.inverse (a * b) = inverse b * inverse a := mul_inverse_rev' (commute.all _ _) end ring lemma is_unit.ring_inverse {a : M₀} : is_unit a → is_unit (ring.inverse a) | ⟨u, hu⟩ := hu ▸ ⟨u⁻¹, (ring.inverse_unit u).symm⟩ @[simp] lemma is_unit_ring_inverse {a : M₀} : is_unit (ring.inverse a) ↔ is_unit a := ⟨λ h, begin casesI subsingleton_or_nontrivial M₀, { convert h }, { contrapose h, rw ring.inverse_non_unit _ h, exact not_is_unit_zero, }, end, is_unit.ring_inverse⟩ lemma commute.ring_inverse_ring_inverse {a b : M₀} (h : commute a b) : commute (ring.inverse a) (ring.inverse b) := (ring.mul_inverse_rev' h.symm).symm.trans $ (congr_arg _ h.symm.eq).trans $ ring.mul_inverse_rev' h variable (M₀) end monoid_with_zero section cancel_monoid_with_zero variables [cancel_monoid_with_zero M₀] {a b c : M₀} @[priority 10] -- see Note [lower instance priority] instance cancel_monoid_with_zero.to_no_zero_divisors : no_zero_divisors M₀ := ⟨λ a b ab0, by { by_cases a = 0, { left, exact h }, right, apply cancel_monoid_with_zero.mul_left_cancel_of_ne_zero h, rw [ab0, mul_zero], }⟩ lemma mul_left_inj' (hc : c ≠ 0) : a * c = b * c ↔ a = b := ⟨mul_right_cancel₀ hc, λ h, h ▸ rfl⟩ lemma mul_right_inj' (ha : a ≠ 0) : a * b = a * c ↔ b = c := ⟨mul_left_cancel₀ ha, λ h, h ▸ rfl⟩ @[simp] lemma mul_eq_mul_right_iff : a * c = b * c ↔ a = b ∨ c = 0 := by by_cases hc : c = 0; [simp [hc], simp [mul_left_inj', hc]] @[simp] lemma mul_eq_mul_left_iff : a * b = a * c ↔ b = c ∨ a = 0 := by by_cases ha : a = 0; [simp [ha], simp [mul_right_inj', ha]] lemma mul_right_eq_self₀ : a * b = a ↔ b = 1 ∨ a = 0 := calc a * b = a ↔ a * b = a * 1 : by rw mul_one ... ↔ b = 1 ∨ a = 0 : mul_eq_mul_left_iff lemma mul_left_eq_self₀ : a * b = b ↔ a = 1 ∨ b = 0 := calc a * b = b ↔ a * b = 1 * b : by rw one_mul ... ↔ a = 1 ∨ b = 0 : mul_eq_mul_right_iff /-- Pullback a `monoid_with_zero` class along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.cancel_monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : cancel_monoid_with_zero M₀' := { mul_left_cancel_of_ne_zero := λ x y z hx H, hf $ mul_left_cancel₀ ((hf.ne_iff' zero).2 hx) $ by erw [← mul, ← mul, H]; refl, mul_right_cancel_of_ne_zero := λ x y z hx H, hf $ mul_right_cancel₀ ((hf.ne_iff' zero).2 hx) $ by erw [← mul, ← mul, H]; refl, .. hf.monoid f one mul, .. hf.mul_zero_class f zero mul } /-- An element of a `cancel_monoid_with_zero` fixed by right multiplication by an element other than one must be zero. -/ theorem eq_zero_of_mul_eq_self_right (h₁ : b ≠ 1) (h₂ : a * b = a) : a = 0 := classical.by_contradiction $ λ ha, h₁ $ mul_left_cancel₀ ha $ h₂.symm ▸ (mul_one a).symm /-- An element of a `cancel_monoid_with_zero` fixed by left multiplication by an element other than one must be zero. -/ theorem eq_zero_of_mul_eq_self_left (h₁ : b ≠ 1) (h₂ : b * a = a) : a = 0 := classical.by_contradiction $ λ ha, h₁ $ mul_right_cancel₀ ha $ h₂.symm ▸ (one_mul a).symm end cancel_monoid_with_zero section cancel_comm_monoid_with_zero variables [cancel_comm_monoid_with_zero M₀] {a b c : M₀} /-- Pullback a `cancel_comm_monoid_with_zero` class along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.cancel_comm_monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : cancel_comm_monoid_with_zero M₀' := { .. hf.comm_monoid_with_zero f zero one mul, .. hf.cancel_monoid_with_zero f zero one mul } end cancel_comm_monoid_with_zero section group_with_zero variables [group_with_zero G₀] {a b c g h x : G₀} alias div_eq_mul_inv ← division_def /-- Pullback a `group_with_zero` class along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.group_with_zero [has_zero G₀'] [has_mul G₀'] [has_one G₀'] [has_inv G₀'] [has_div G₀'] (f : G₀' → G₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) : group_with_zero G₀' := { inv_zero := hf $ by erw [inv, zero, inv_zero], mul_inv_cancel := λ x hx, hf $ by erw [one, mul, inv, mul_inv_cancel ((hf.ne_iff' zero).2 hx)], .. hf.monoid_with_zero f zero one mul, .. hf.div_inv_monoid f one mul inv div, .. pullback_nonzero f zero one, } /-- Pushforward a `group_with_zero` class along an surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.group_with_zero [has_zero G₀'] [has_mul G₀'] [has_one G₀'] [has_inv G₀'] [has_div G₀'] (h01 : (0:G₀') ≠ 1) (f : G₀ → G₀') (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) : group_with_zero G₀' := { inv_zero := by erw [← zero, ← inv, inv_zero], mul_inv_cancel := hf.forall.2 $ λ x hx, by erw [← inv, ← mul, mul_inv_cancel (mt (congr_arg f) $ trans_rel_left ne hx zero.symm)]; exact one, exists_pair_ne := ⟨0, 1, h01⟩, .. hf.monoid_with_zero f zero one mul, .. hf.div_inv_monoid f one mul inv div } @[simp] lemma mul_inv_cancel_right₀ (h : b ≠ 0) (a : G₀) : (a * b) * b⁻¹ = a := calc (a * b) * b⁻¹ = a * (b * b⁻¹) : mul_assoc _ _ _ ... = a : by simp [h] @[simp] lemma mul_inv_cancel_left₀ (h : a ≠ 0) (b : G₀) : a * (a⁻¹ * b) = b := calc a * (a⁻¹ * b) = (a * a⁻¹) * b : (mul_assoc _ _ _).symm ... = b : by simp [h] lemma inv_ne_zero (h : a ≠ 0) : a⁻¹ ≠ 0 := assume a_eq_0, by simpa [a_eq_0] using mul_inv_cancel h @[simp] lemma inv_mul_cancel (h : a ≠ 0) : a⁻¹ * a = 1 := calc a⁻¹ * a = (a⁻¹ * a) * a⁻¹ * a⁻¹⁻¹ : by simp [inv_ne_zero h] ... = a⁻¹ * a⁻¹⁻¹ : by simp [h] ... = 1 : by simp [inv_ne_zero h] lemma group_with_zero.mul_left_injective (h : x ≠ 0) : function.injective (λ y, x * y) := λ y y' w, by simpa only [←mul_assoc, inv_mul_cancel h, one_mul] using congr_arg (λ y, x⁻¹ * y) w lemma group_with_zero.mul_right_injective (h : x ≠ 0) : function.injective (λ y, y * x) := λ y y' w, by simpa only [mul_assoc, mul_inv_cancel h, mul_one] using congr_arg (λ y, y * x⁻¹) w @[simp] lemma inv_mul_cancel_right₀ (h : b ≠ 0) (a : G₀) : (a * b⁻¹) * b = a := calc (a * b⁻¹) * b = a * (b⁻¹ * b) : mul_assoc _ _ _ ... = a : by simp [h] @[simp] lemma inv_mul_cancel_left₀ (h : a ≠ 0) (b : G₀) : a⁻¹ * (a * b) = b := calc a⁻¹ * (a * b) = (a⁻¹ * a) * b : (mul_assoc _ _ _).symm ... = b : by simp [h] @[simp] lemma inv_one : 1⁻¹ = (1:G₀) := calc 1⁻¹ = 1 * 1⁻¹ : by rw [one_mul] ... = (1:G₀) : by simp @[simp] lemma inv_inv₀ (a : G₀) : a⁻¹⁻¹ = a := begin by_cases h : a = 0, { simp [h] }, calc a⁻¹⁻¹ = a * (a⁻¹ * a⁻¹⁻¹) : by simp [h] ... = a : by simp [inv_ne_zero h] end /-- Multiplying `a` by itself and then by its inverse results in `a` (whether or not `a` is zero). -/ @[simp] lemma mul_self_mul_inv (a : G₀) : a * a * a⁻¹ = a := begin by_cases h : a = 0, { rw [h, inv_zero, mul_zero] }, { rw [mul_assoc, mul_inv_cancel h, mul_one] } end /-- Multiplying `a` by its inverse and then by itself results in `a` (whether or not `a` is zero). -/ @[simp] lemma mul_inv_mul_self (a : G₀) : a * a⁻¹ * a = a := begin by_cases h : a = 0, { rw [h, inv_zero, mul_zero] }, { rw [mul_inv_cancel h, one_mul] } end /-- Multiplying `a⁻¹` by `a` twice results in `a` (whether or not `a` is zero). -/ @[simp] lemma inv_mul_mul_self (a : G₀) : a⁻¹ * a * a = a := begin by_cases h : a = 0, { rw [h, inv_zero, mul_zero] }, { rw [inv_mul_cancel h, one_mul] } end /-- Multiplying `a` by itself and then dividing by itself results in `a` (whether or not `a` is zero). -/ @[simp] lemma mul_self_div_self (a : G₀) : a * a / a = a := by rw [div_eq_mul_inv, mul_self_mul_inv a] /-- Dividing `a` by itself and then multiplying by itself results in `a` (whether or not `a` is zero). -/ @[simp] lemma div_self_mul_self (a : G₀) : a / a * a = a := by rw [div_eq_mul_inv, mul_inv_mul_self a] lemma inv_involutive₀ : function.involutive (has_inv.inv : G₀ → G₀) := inv_inv₀ lemma eq_inv_of_mul_right_eq_one (h : a * b = 1) : b = a⁻¹ := by rw [← inv_mul_cancel_left₀ (left_ne_zero_of_mul_eq_one h) b, h, mul_one] lemma eq_inv_of_mul_left_eq_one (h : a * b = 1) : a = b⁻¹ := by rw [← mul_inv_cancel_right₀ (right_ne_zero_of_mul_eq_one h) a, h, one_mul] lemma inv_injective₀ : function.injective (@has_inv.inv G₀ _) := inv_involutive₀.injective @[simp] lemma inv_inj₀ : g⁻¹ = h⁻¹ ↔ g = h := inv_injective₀.eq_iff /-- This is the analogue of `inv_eq_iff_inv_eq` for `group_with_zero`. It could also be named `inv_eq_iff_inv_eq'`. -/ lemma inv_eq_iff : g⁻¹ = h ↔ h⁻¹ = g := by rw [← inv_inj₀, eq_comm, inv_inv₀] /-- This is the analogue of `eq_inv_iff_eq_inv` for `group_with_zero`. It could also be named `eq_inv_iff_eq_inv'`. -/ lemma eq_inv_iff : a = b⁻¹ ↔ b = a⁻¹ := by rw [eq_comm, inv_eq_iff, eq_comm] @[simp] lemma inv_eq_one₀ : g⁻¹ = 1 ↔ g = 1 := by rw [inv_eq_iff, inv_one, eq_comm] lemma eq_mul_inv_iff_mul_eq₀ (hc : c ≠ 0) : a = b * c⁻¹ ↔ a * c = b := by split; rintro rfl; [rw inv_mul_cancel_right₀ hc, rw mul_inv_cancel_right₀ hc] lemma eq_inv_mul_iff_mul_eq₀ (hb : b ≠ 0) : a = b⁻¹ * c ↔ b * a = c := by split; rintro rfl; [rw mul_inv_cancel_left₀ hb, rw inv_mul_cancel_left₀ hb] lemma inv_mul_eq_iff_eq_mul₀ (ha : a ≠ 0) : a⁻¹ * b = c ↔ b = a * c := by rw [eq_comm, eq_inv_mul_iff_mul_eq₀ ha, eq_comm] lemma mul_inv_eq_iff_eq_mul₀ (hb : b ≠ 0) : a * b⁻¹ = c ↔ a = c * b := by rw [eq_comm, eq_mul_inv_iff_mul_eq₀ hb, eq_comm] lemma mul_inv_eq_one₀ (hb : b ≠ 0) : a * b⁻¹ = 1 ↔ a = b := by rw [mul_inv_eq_iff_eq_mul₀ hb, one_mul] lemma inv_mul_eq_one₀ (ha : a ≠ 0) : a⁻¹ * b = 1 ↔ a = b := by rw [inv_mul_eq_iff_eq_mul₀ ha, mul_one, eq_comm] lemma mul_eq_one_iff_eq_inv₀ (hb : b ≠ 0) : a * b = 1 ↔ a = b⁻¹ := by { convert mul_inv_eq_one₀ (inv_ne_zero hb), rw [inv_inv₀] } lemma mul_eq_one_iff_inv_eq₀ (ha : a ≠ 0) : a * b = 1 ↔ a⁻¹ = b := by { convert inv_mul_eq_one₀ (inv_ne_zero ha), rw [inv_inv₀] } end group_with_zero namespace units variables [group_with_zero G₀] variables {a b : G₀} /-- Embed a non-zero element of a `group_with_zero` into the unit group. By combining this function with the operations on units, or the `/ₚ` operation, it is possible to write a division as a partial function with three arguments. -/ def mk0 (a : G₀) (ha : a ≠ 0) : G₀ˣ := ⟨a, a⁻¹, mul_inv_cancel ha, inv_mul_cancel ha⟩ @[simp] lemma mk0_one (h := one_ne_zero) : mk0 (1 : G₀) h = 1 := by { ext, refl } @[simp] lemma coe_mk0 {a : G₀} (h : a ≠ 0) : (mk0 a h : G₀) = a := rfl @[simp] lemma mk0_coe (u : G₀ˣ) (h : (u : G₀) ≠ 0) : mk0 (u : G₀) h = u := units.ext rfl @[simp, norm_cast] lemma coe_inv' (u : G₀ˣ) : ((u⁻¹ : G₀ˣ) : G₀) = u⁻¹ := eq_inv_of_mul_left_eq_one u.inv_mul @[simp] lemma mul_inv' (u : G₀ˣ) : (u : G₀) * u⁻¹ = 1 := mul_inv_cancel u.ne_zero @[simp] lemma inv_mul' (u : G₀ˣ) : (u⁻¹ : G₀) * u = 1 := inv_mul_cancel u.ne_zero @[simp] lemma mk0_inj {a b : G₀} (ha : a ≠ 0) (hb : b ≠ 0) : units.mk0 a ha = units.mk0 b hb ↔ a = b := ⟨λ h, by injection h, λ h, units.ext h⟩ @[simp] lemma exists_iff_ne_zero {x : G₀} : (∃ u : G₀ˣ, ↑u = x) ↔ x ≠ 0 := ⟨λ ⟨u, hu⟩, hu ▸ u.ne_zero, assume hx, ⟨mk0 x hx, rfl⟩⟩ lemma _root_.group_with_zero.eq_zero_or_unit (a : G₀) : a = 0 ∨ ∃ u : G₀ˣ, a = u := begin by_cases h : a = 0, { left, exact h }, { right, simpa only [eq_comm] using units.exists_iff_ne_zero.mpr h } end end units section group_with_zero variables [group_with_zero G₀] lemma is_unit.mk0 (x : G₀) (hx : x ≠ 0) : is_unit x := (units.mk0 x hx).is_unit lemma is_unit_iff_ne_zero {x : G₀} : is_unit x ↔ x ≠ 0 := units.exists_iff_ne_zero @[priority 10] -- see Note [lower instance priority] instance group_with_zero.no_zero_divisors : no_zero_divisors G₀ := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h, begin contrapose! h, exact ((units.mk0 a h.1) * (units.mk0 b h.2)).ne_zero end, .. (‹_› : group_with_zero G₀) } @[priority 10] -- see Note [lower instance priority] instance group_with_zero.cancel_monoid_with_zero : cancel_monoid_with_zero G₀ := { mul_left_cancel_of_ne_zero := λ x y z hx h, by rw [← inv_mul_cancel_left₀ hx y, h, inv_mul_cancel_left₀ hx z], mul_right_cancel_of_ne_zero := λ x y z hy h, by rw [← mul_inv_cancel_right₀ hy x, h, mul_inv_cancel_right₀ hy z], .. (‹_› : group_with_zero G₀) } -- Can't be put next to the other `mk0` lemmas becuase it depends on the -- `no_zero_divisors` instance, which depends on `mk0`. @[simp] lemma units.mk0_mul (x y : G₀) (hxy) : units.mk0 (x * y) hxy = units.mk0 x (mul_ne_zero_iff.mp hxy).1 * units.mk0 y (mul_ne_zero_iff.mp hxy).2 := by { ext, refl } lemma mul_inv_rev₀ (x y : G₀) : (x * y)⁻¹ = y⁻¹ * x⁻¹ := begin by_cases hx : x = 0, { simp [hx] }, by_cases hy : y = 0, { simp [hy] }, symmetry, apply eq_inv_of_mul_left_eq_one, simp [mul_assoc, hx, hy] end @[simp] lemma div_self {a : G₀} (h : a ≠ 0) : a / a = 1 := by rw [div_eq_mul_inv, mul_inv_cancel h] @[simp] lemma div_one (a : G₀) : a / 1 = a := by simp [div_eq_mul_inv a 1] @[simp] lemma zero_div (a : G₀) : 0 / a = 0 := by rw [div_eq_mul_inv, zero_mul] @[simp] lemma div_zero (a : G₀) : a / 0 = 0 := by rw [div_eq_mul_inv, inv_zero, mul_zero] @[simp] lemma div_mul_cancel (a : G₀) {b : G₀} (h : b ≠ 0) : a / b * b = a := by rw [div_eq_mul_inv, inv_mul_cancel_right₀ h a] lemma div_mul_cancel_of_imp {a b : G₀} (h : b = 0 → a = 0) : a / b * b = a := classical.by_cases (λ hb : b = 0, by simp [*]) (div_mul_cancel a) @[simp] lemma mul_div_cancel (a : G₀) {b : G₀} (h : b ≠ 0) : a * b / b = a := by rw [div_eq_mul_inv, mul_inv_cancel_right₀ h a] lemma mul_div_cancel_of_imp {a b : G₀} (h : b = 0 → a = 0) : a * b / b = a := classical.by_cases (λ hb : b = 0, by simp [*]) (mul_div_cancel a) local attribute [simp] div_eq_mul_inv mul_comm mul_assoc mul_left_comm @[simp] lemma div_self_mul_self' (a : G₀) : a / (a * a) = a⁻¹ := calc a / (a * a) = a⁻¹⁻¹ * a⁻¹ * a⁻¹ : by simp [mul_inv_rev₀] ... = a⁻¹ : inv_mul_mul_self _ lemma div_eq_mul_one_div (a b : G₀) : a / b = a * (1 / b) := by simp lemma mul_one_div_cancel {a : G₀} (h : a ≠ 0) : a * (1 / a) = 1 := by simp [h] lemma one_div_mul_cancel {a : G₀} (h : a ≠ 0) : (1 / a) * a = 1 := by simp [h] lemma one_div_one : 1 / 1 = (1:G₀) := div_self (ne.symm zero_ne_one) lemma one_div_ne_zero {a : G₀} (h : a ≠ 0) : 1 / a ≠ 0 := by simpa only [one_div] using inv_ne_zero h lemma eq_one_div_of_mul_eq_one {a b : G₀} (h : a * b = 1) : b = 1 / a := by simpa only [one_div] using eq_inv_of_mul_right_eq_one h lemma eq_one_div_of_mul_eq_one_left {a b : G₀} (h : b * a = 1) : b = 1 / a := by simpa only [one_div] using eq_inv_of_mul_left_eq_one h @[simp] lemma one_div_div (a b : G₀) : 1 / (a / b) = b / a := by rw [one_div, div_eq_mul_inv, mul_inv_rev₀, inv_inv₀, div_eq_mul_inv] lemma one_div_one_div (a : G₀) : 1 / (1 / a) = a := by simp lemma eq_of_one_div_eq_one_div {a b : G₀} (h : 1 / a = 1 / b) : a = b := by rw [← one_div_one_div a, h, one_div_one_div] variables {a b c : G₀} @[simp] lemma inv_eq_zero {a : G₀} : a⁻¹ = 0 ↔ a = 0 := by rw [inv_eq_iff, inv_zero, eq_comm] @[simp] lemma zero_eq_inv {a : G₀} : 0 = a⁻¹ ↔ 0 = a := eq_comm.trans $ inv_eq_zero.trans eq_comm lemma one_div_mul_one_div_rev (a b : G₀) : (1 / a) * (1 / b) = 1 / (b * a) := by simp only [div_eq_mul_inv, one_mul, mul_inv_rev₀] theorem divp_eq_div (a : G₀) (u : G₀ˣ) : a /ₚ u = a / u := by simpa only [div_eq_mul_inv] using congr_arg ((*) a) u.coe_inv' @[simp] theorem divp_mk0 (a : G₀) {b : G₀} (hb : b ≠ 0) : a /ₚ units.mk0 b hb = a / b := divp_eq_div _ _ lemma inv_div : (a / b)⁻¹ = b / a := by rw [div_eq_mul_inv, mul_inv_rev₀, div_eq_mul_inv, inv_inv₀] lemma inv_div_left : a⁻¹ / b = (b * a)⁻¹ := by rw [mul_inv_rev₀, div_eq_mul_inv] lemma div_ne_zero (ha : a ≠ 0) (hb : b ≠ 0) : a / b ≠ 0 := by { rw div_eq_mul_inv, exact mul_ne_zero ha (inv_ne_zero hb) } @[simp] lemma div_eq_zero_iff : a / b = 0 ↔ a = 0 ∨ b = 0:= by simp [div_eq_mul_inv] lemma div_ne_zero_iff : a / b ≠ 0 ↔ a ≠ 0 ∧ b ≠ 0 := (not_congr div_eq_zero_iff).trans not_or_distrib lemma div_left_inj' (hc : c ≠ 0) : a / c = b / c ↔ a = b := by rw [← divp_mk0 _ hc, ← divp_mk0 _ hc, divp_left_inj] lemma div_eq_iff_mul_eq (hb : b ≠ 0) : a / b = c ↔ c * b = a := ⟨λ h, by rw [← h, div_mul_cancel _ hb], λ h, by rw [← h, mul_div_cancel _ hb]⟩ lemma eq_div_iff_mul_eq (hc : c ≠ 0) : a = b / c ↔ a * c = b := by rw [eq_comm, div_eq_iff_mul_eq hc] lemma div_eq_of_eq_mul {x : G₀} (hx : x ≠ 0) {y z : G₀} (h : y = z * x) : y / x = z := (div_eq_iff_mul_eq hx).2 h.symm lemma eq_div_of_mul_eq {x : G₀} (hx : x ≠ 0) {y z : G₀} (h : z * x = y) : z = y / x := eq.symm $ div_eq_of_eq_mul hx h.symm lemma eq_of_div_eq_one (h : a / b = 1) : a = b := begin by_cases hb : b = 0, { rw [hb, div_zero] at h, exact eq_of_zero_eq_one h a b }, { rwa [div_eq_iff_mul_eq hb, one_mul, eq_comm] at h } end lemma div_eq_one_iff_eq (hb : b ≠ 0) : a / b = 1 ↔ a = b := ⟨eq_of_div_eq_one, λ h, h.symm ▸ div_self hb⟩ lemma div_mul_left {a b : G₀} (hb : b ≠ 0) : b / (a * b) = 1 / a := by simp only [div_eq_mul_inv, mul_inv_rev₀, mul_inv_cancel_left₀ hb, one_mul] lemma mul_div_mul_right (a b : G₀) {c : G₀} (hc : c ≠ 0) : (a * c) / (b * c) = a / b := by simp only [div_eq_mul_inv, mul_inv_rev₀, mul_assoc, mul_inv_cancel_left₀ hc] lemma mul_mul_div (a : G₀) {b : G₀} (hb : b ≠ 0) : a = a * b * (1 / b) := by simp [hb] lemma ring.inverse_eq_inv (a : G₀) : ring.inverse a = a⁻¹ := begin obtain rfl | ha := eq_or_ne a 0, { simp }, { exact ring.inverse_unit (units.mk0 a ha) } end @[simp] lemma ring.inverse_eq_inv' : (ring.inverse : G₀ → G₀) = has_inv.inv := funext ring.inverse_eq_inv @[field_simps] lemma div_div_eq_mul_div (a b c : G₀) : a / (b / c) = (a * c) / b := by rw [div_eq_mul_one_div, one_div_div, ← mul_div_assoc] /-- Dividing `a` by the result of dividing `a` by itself results in `a` (whether or not `a` is zero). -/ @[simp] lemma div_div_self (a : G₀) : a / (a / a) = a := begin rw div_div_eq_mul_div, exact mul_self_div_self a end lemma ne_zero_of_one_div_ne_zero {a : G₀} (h : 1 / a ≠ 0) : a ≠ 0 := assume ha : a = 0, begin rw [ha, div_zero] at h, contradiction end lemma eq_zero_of_one_div_eq_zero {a : G₀} (h : 1 / a = 0) : a = 0 := classical.by_cases (assume ha, ha) (assume ha, ((one_div_ne_zero ha) h).elim) lemma div_div_div_cancel_right (a : G₀) (hc : c ≠ 0) : (a / c) / (b / c) = a / b := by rw [div_div_eq_mul_div, div_mul_cancel _ hc] lemma div_mul_div_cancel (a : G₀) (hc : c ≠ 0) : (a / c) * (c / b) = a / b := by rw [← mul_div_assoc, div_mul_cancel _ hc] @[field_simps] lemma eq_div_iff (hb : b ≠ 0) : c = a / b ↔ c * b = a := eq_div_iff_mul_eq hb @[field_simps] lemma div_eq_iff (hb : b ≠ 0) : a / b = c ↔ a = c * b := (div_eq_iff_mul_eq hb).trans eq_comm end group_with_zero section comm_group_with_zero -- comm variables [comm_group_with_zero G₀] {a b c : G₀} @[priority 10] -- see Note [lower instance priority] instance comm_group_with_zero.cancel_comm_monoid_with_zero : cancel_comm_monoid_with_zero G₀ := { ..group_with_zero.cancel_monoid_with_zero, ..comm_group_with_zero.to_comm_monoid_with_zero G₀ } /-- Pullback a `comm_group_with_zero` class along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.comm_group_with_zero [has_zero G₀'] [has_mul G₀'] [has_one G₀'] [has_inv G₀'] [has_div G₀'] (f : G₀' → G₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) : comm_group_with_zero G₀' := { .. hf.group_with_zero f zero one mul inv div, .. hf.comm_semigroup f mul } /-- Pushforward a `comm_group_with_zero` class along a surjective function. -/ protected def function.surjective.comm_group_with_zero [has_zero G₀'] [has_mul G₀'] [has_one G₀'] [has_inv G₀'] [has_div G₀'] (h01 : (0:G₀') ≠ 1) (f : G₀ → G₀') (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) : comm_group_with_zero G₀' := { .. hf.group_with_zero h01 f zero one mul inv div, .. hf.comm_semigroup f mul } lemma mul_inv₀ : (a * b)⁻¹ = a⁻¹ * b⁻¹ := by rw [mul_inv_rev₀, mul_comm] lemma one_div_mul_one_div (a b : G₀) : (1 / a) * (1 / b) = 1 / (a * b) := by rw [one_div_mul_one_div_rev, mul_comm b] lemma div_mul_right {a : G₀} (b : G₀) (ha : a ≠ 0) : a / (a * b) = 1 / b := by rw [mul_comm, div_mul_left ha] lemma mul_div_cancel_left_of_imp {a b : G₀} (h : a = 0 → b = 0) : a * b / a = b := by rw [mul_comm, mul_div_cancel_of_imp h] lemma mul_div_cancel_left {a : G₀} (b : G₀) (ha : a ≠ 0) : a * b / a = b := mul_div_cancel_left_of_imp $ λ h, (ha h).elim lemma mul_div_cancel_of_imp' {a b : G₀} (h : b = 0 → a = 0) : b * (a / b) = a := by rw [mul_comm, div_mul_cancel_of_imp h] lemma mul_div_cancel' (a : G₀) {b : G₀} (hb : b ≠ 0) : b * (a / b) = a := by rw [mul_comm, (div_mul_cancel _ hb)] local attribute [simp] mul_assoc mul_comm mul_left_comm lemma div_mul_div (a b c d : G₀) : (a / b) * (c / d) = (a * c) / (b * d) := by simp [div_eq_mul_inv, mul_inv₀] lemma mul_div_mul_left (a b : G₀) {c : G₀} (hc : c ≠ 0) : (c * a) / (c * b) = a / b := by rw [mul_comm c, mul_comm c, mul_div_mul_right _ _ hc] @[field_simps] lemma div_mul_eq_mul_div (a b c : G₀) : (b / c) * a = (b * a) / c := by simp [div_eq_mul_inv] lemma div_mul_eq_mul_div_comm (a b c : G₀) : (b / c) * a = b * (a / c) := by rw [div_mul_eq_mul_div, ← one_mul c, ← div_mul_div, div_one, one_mul] lemma mul_eq_mul_of_div_eq_div (a : G₀) {b : G₀} (c : G₀) {d : G₀} (hb : b ≠ 0) (hd : d ≠ 0) (h : a / b = c / d) : a * d = c * b := by rw [← mul_one (a*d), mul_assoc, mul_comm d, ← mul_assoc, ← div_self hb, ← div_mul_eq_mul_div_comm, h, div_mul_eq_mul_div, div_mul_cancel _ hd] @[field_simps] lemma div_div_eq_div_mul (a b c : G₀) : (a / b) / c = a / (b * c) := by rw [div_eq_mul_one_div, div_mul_div, mul_one] lemma div_div_div_div_eq (a : G₀) {b c d : G₀} : (a / b) / (c / d) = (a * d) / (b * c) := by rw [div_div_eq_mul_div, div_mul_eq_mul_div, div_div_eq_div_mul] lemma div_mul_eq_div_mul_one_div (a b c : G₀) : a / (b * c) = (a / b) * (1 / c) := by rw [← div_div_eq_div_mul, ← div_eq_mul_one_div] lemma div_helper {a : G₀} (b : G₀) (h : a ≠ 0) : (1 / (a * b)) * a = 1 / b := by rw [div_mul_eq_mul_div, one_mul, div_mul_right _ h] end comm_group_with_zero section comm_group_with_zero variables [comm_group_with_zero G₀] {a b c d : G₀} lemma div_eq_inv_mul : a / b = b⁻¹ * a := by rw [div_eq_mul_inv, mul_comm] lemma mul_div_right_comm (a b c : G₀) : (a * b) / c = (a / c) * b := by rw [div_eq_mul_inv, mul_assoc, mul_comm b, ← mul_assoc, div_eq_mul_inv] lemma mul_comm_div' (a b c : G₀) : (a / b) * c = a * (c / b) := by rw [← mul_div_assoc, mul_div_right_comm] lemma div_mul_comm' (a b c : G₀) : (a / b) * c = (c / b) * a := by rw [div_mul_eq_mul_div, mul_comm, mul_div_right_comm] lemma mul_div_comm (a b c : G₀) : a * (b / c) = b * (a / c) := by rw [← mul_div_assoc, mul_comm, mul_div_assoc] lemma div_right_comm (a : G₀) : (a / b) / c = (a / c) / b := by rw [div_div_eq_div_mul, div_div_eq_div_mul, mul_comm] @[field_simps] lemma div_eq_div_iff (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d ↔ a * d = c * b := calc a / b = c / d ↔ a / b * (b * d) = c / d * (b * d) : by rw [mul_left_inj' (mul_ne_zero hb hd)] ... ↔ a * d = c * b : by rw [← mul_assoc, div_mul_cancel _ hb, ← mul_assoc, mul_right_comm, div_mul_cancel _ hd] lemma div_div_cancel' (ha : a ≠ 0) : a / (a / b) = b := by rw [div_eq_mul_inv, inv_div, mul_div_cancel' _ ha] end comm_group_with_zero namespace semiconj_by @[simp] lemma zero_right [mul_zero_class G₀] (a : G₀) : semiconj_by a 0 0 := by simp only [semiconj_by, mul_zero, zero_mul] @[simp] lemma zero_left [mul_zero_class G₀] (x y : G₀) : semiconj_by 0 x y := by simp only [semiconj_by, mul_zero, zero_mul] variables [group_with_zero G₀] {a x y x' y' : G₀} @[simp] lemma inv_symm_left_iff₀ : semiconj_by a⁻¹ x y ↔ semiconj_by a y x := classical.by_cases (λ ha : a = 0, by simp only [ha, inv_zero, semiconj_by.zero_left]) (λ ha, @units_inv_symm_left_iff _ _ (units.mk0 a ha) _ _) lemma inv_symm_left₀ (h : semiconj_by a x y) : semiconj_by a⁻¹ y x := semiconj_by.inv_symm_left_iff₀.2 h lemma inv_right₀ (h : semiconj_by a x y) : semiconj_by a x⁻¹ y⁻¹ := begin by_cases ha : a = 0, { simp only [ha, zero_left] }, by_cases hx : x = 0, { subst x, simp only [semiconj_by, mul_zero, @eq_comm _ _ (y * a), mul_eq_zero] at h, simp [h.resolve_right ha] }, { have := mul_ne_zero ha hx, rw [h.eq, mul_ne_zero_iff] at this, exact @units_inv_right _ _ _ (units.mk0 x hx) (units.mk0 y this.1) h }, end @[simp] lemma inv_right_iff₀ : semiconj_by a x⁻¹ y⁻¹ ↔ semiconj_by a x y := ⟨λ h, inv_inv₀ x ▸ inv_inv₀ y ▸ h.inv_right₀, inv_right₀⟩ lemma div_right (h : semiconj_by a x y) (h' : semiconj_by a x' y') : semiconj_by a (x / x') (y / y') := by { rw [div_eq_mul_inv, div_eq_mul_inv], exact h.mul_right h'.inv_right₀ } end semiconj_by namespace commute @[simp] theorem zero_right [mul_zero_class G₀] (a : G₀) :commute a 0 := semiconj_by.zero_right a @[simp] theorem zero_left [mul_zero_class G₀] (a : G₀) : commute 0 a := semiconj_by.zero_left a a variables [group_with_zero G₀] {a b c : G₀} @[simp] theorem inv_left_iff₀ : commute a⁻¹ b ↔ commute a b := semiconj_by.inv_symm_left_iff₀ theorem inv_left₀ (h : commute a b) : commute a⁻¹ b := inv_left_iff₀.2 h @[simp] theorem inv_right_iff₀ : commute a b⁻¹ ↔ commute a b := semiconj_by.inv_right_iff₀ theorem inv_right₀ (h : commute a b) : commute a b⁻¹ := inv_right_iff₀.2 h theorem inv_inv₀ (h : commute a b) : commute a⁻¹ b⁻¹ := h.inv_left₀.inv_right₀ @[simp] theorem div_right (hab : commute a b) (hac : commute a c) : commute a (b / c) := hab.div_right hac @[simp] theorem div_left (hac : commute a c) (hbc : commute b c) : commute (a / b) c := by { rw div_eq_mul_inv, exact hac.mul_left hbc.inv_left₀ } end commute namespace monoid_with_zero_hom variables [group_with_zero G₀] [group_with_zero G₀'] [monoid_with_zero M₀] [nontrivial M₀] section monoid_with_zero variables (f : G₀ →*₀ M₀) {a : G₀} lemma map_ne_zero : f a ≠ 0 ↔ a ≠ 0 := ⟨λ hfa ha, hfa $ ha.symm ▸ f.map_zero, λ ha, ((is_unit.mk0 a ha).map f.to_monoid_hom).ne_zero⟩ @[simp] lemma map_eq_zero : f a = 0 ↔ a = 0 := not_iff_not.1 f.map_ne_zero end monoid_with_zero section group_with_zero variables (f : G₀ →*₀ G₀') (a b : G₀) /-- A monoid homomorphism between groups with zeros sending `0` to `0` sends `a⁻¹` to `(f a)⁻¹`. -/ @[simp] lemma map_inv : f a⁻¹ = (f a)⁻¹ := begin by_cases h : a = 0, by simp [h], apply eq_inv_of_mul_left_eq_one, rw [← f.map_mul, inv_mul_cancel h, f.map_one] end @[simp] lemma map_div : f (a / b) = f a / f b := by simpa only [div_eq_mul_inv] using ((f.map_mul _ _).trans $ _root_.congr_arg _ $ f.map_inv b) end group_with_zero end monoid_with_zero_hom /-- Inversion on a commutative group with zero, considered as a monoid with zero homomorphism. -/ def inv_monoid_with_zero_hom {G₀ : Type*} [comm_group_with_zero G₀] : G₀ →*₀ G₀ := { to_fun := has_inv.inv, map_zero' := inv_zero, map_one' := inv_one, map_mul' := λ _ _, mul_inv₀ } @[simp] lemma monoid_hom.map_units_inv {M G₀ : Type*} [monoid M] [group_with_zero G₀] (f : M →* G₀) (u : Mˣ) : f ↑u⁻¹ = (f u)⁻¹ := by rw [← units.coe_map, ← units.coe_map, ← units.coe_inv', monoid_hom.map_inv] @[simp] lemma monoid_with_zero_hom.map_units_inv {M G₀ : Type*} [monoid_with_zero M] [group_with_zero G₀] (f : M →*₀ G₀) (u : Mˣ) : f ↑u⁻¹ = (f u)⁻¹ := f.to_monoid_hom.map_units_inv u section noncomputable_defs variables {M : Type*} [nontrivial M] /-- Constructs a `group_with_zero` structure on a `monoid_with_zero` consisting only of units and 0. -/ noncomputable def group_with_zero_of_is_unit_or_eq_zero [hM : monoid_with_zero M] (h : ∀ (a : M), is_unit a ∨ a = 0) : group_with_zero M := { inv := λ a, if h0 : a = 0 then 0 else ↑((h a).resolve_right h0).unit⁻¹, inv_zero := dif_pos rfl, mul_inv_cancel := λ a h0, by { change a * (if h0 : a = 0 then 0 else ↑((h a).resolve_right h0).unit⁻¹) = 1, rw [dif_neg h0, units.mul_inv_eq_iff_eq_mul, one_mul, is_unit.unit_spec] }, exists_pair_ne := nontrivial.exists_pair_ne, .. hM } /-- Constructs a `comm_group_with_zero` structure on a `comm_monoid_with_zero` consisting only of units and 0. -/ noncomputable def comm_group_with_zero_of_is_unit_or_eq_zero [hM : comm_monoid_with_zero M] (h : ∀ (a : M), is_unit a ∨ a = 0) : comm_group_with_zero M := { .. (group_with_zero_of_is_unit_or_eq_zero h), .. hM } end noncomputable_defs
b1890fe41be7cdbb2fbabc6d3fd2bd347b02cd65
94e33a31faa76775069b071adea97e86e218a8ee
/src/group_theory/transfer.lean
db47f440cf7487ad4148310e439cf619613e80b7
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
2,994
lean
/- Copyright (c) 2022 Thomas Browning. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning -/ import group_theory.complement import group_theory.group_action.basic import group_theory.index /-! # The Transfer Homomorphism In this file we construct the transfer homomorphism. ## Main definitions - `diff ϕ S T` : The difference of two left transversals `S` and `T` under the homomorphism `ϕ`. - `transfer ϕ` : The transfer homomorphism induced by `ϕ`. -/ open_locale big_operators variables {G : Type*} [group G] {H : subgroup G} {A : Type*} [comm_group A] (ϕ : H →* A) namespace subgroup namespace left_transversals open finset mul_action open_locale pointwise variables (R S T : left_transversals (H : set G)) [fintype (G ⧸ H)] /-- The difference of two left transversals -/ @[to_additive "The difference of two left transversals"] noncomputable def diff : A := let α := mem_left_transversals.to_equiv S.2, β := mem_left_transversals.to_equiv T.2 in ∏ q, ϕ ⟨(α q)⁻¹ * β q, quotient_group.left_rel_apply.mp $ quotient.exact' ((α.symm_apply_apply q).trans (β.symm_apply_apply q).symm)⟩ @[to_additive] lemma diff_mul_diff : diff ϕ R S * diff ϕ S T = diff ϕ R T := prod_mul_distrib.symm.trans (prod_congr rfl (λ q hq, (ϕ.map_mul _ _).symm.trans (congr_arg ϕ (by simp_rw [subtype.ext_iff, coe_mul, coe_mk, mul_assoc, mul_inv_cancel_left])))) @[to_additive] lemma diff_self : diff ϕ T T = 1 := mul_right_eq_self.mp (diff_mul_diff ϕ T T T) @[to_additive] lemma diff_inv : (diff ϕ S T)⁻¹ = diff ϕ T S := inv_eq_of_mul_eq_one_right $ (diff_mul_diff ϕ S T S).trans $ diff_self ϕ S @[to_additive] lemma smul_diff_smul (g : G) : diff ϕ (g • S) (g • T) = diff ϕ S T := prod_bij' (λ q _, g⁻¹ • q) (λ _ _, mem_univ _) (λ _ _, congr_arg ϕ (by simp_rw [coe_mk, smul_apply_eq_smul_apply_inv_smul, smul_eq_mul, mul_inv_rev, mul_assoc, inv_mul_cancel_left])) (λ q _, g • q) (λ _ _, mem_univ _) (λ q _, smul_inv_smul g q) (λ q _, inv_smul_smul g q) end left_transversals end subgroup namespace monoid_hom variables [fintype (G ⧸ H)] open subgroup subgroup.left_transversals /-- Given `ϕ : H →* A` from `H : subgroup G` to a commutative group `A`, the transfer homomorphism is `transfer ϕ : G →* A`. -/ @[to_additive "Given `ϕ : H →+ A` from `H : add_subgroup G` to an additive commutative group `A`, the transfer homomorphism is `transfer ϕ : G →+ A`."] noncomputable def transfer : G →* A := let T : left_transversals (H : set G) := inhabited.default in { to_fun := λ g, diff ϕ T (g • T), map_one' := by rw [one_smul, diff_self], map_mul' := λ g h, by rw [mul_smul, ←diff_mul_diff, smul_diff_smul] } variables (T : left_transversals (H : set G)) @[to_additive] lemma transfer_def (g : G) : transfer ϕ g = diff ϕ T (g • T) := by rw [transfer, ←diff_mul_diff, ←smul_diff_smul, mul_comm, diff_mul_diff]; refl end monoid_hom
05e3fc3a359872acafda839f590db14f3fa7ea5a
5d166a16ae129621cb54ca9dde86c275d7d2b483
/tests/lean/run/and_rec_code_gen_issue.lean
fa0f9b99a6b2e3df1f837c8d991e8f5d4e025afe
[ "Apache-2.0" ]
permissive
jcarlson23/lean
b00098763291397e0ac76b37a2dd96bc013bd247
8de88701247f54d325edd46c0eed57aeacb64baf
refs/heads/master
1,611,571,813,719
1,497,020,963,000
1,497,021,515,000
93,882,536
1
0
null
1,497,029,896,000
1,497,029,896,000
null
UTF-8
Lean
false
false
3,219
lean
import data.buffer structure hoare_state (σ : Type) (α : Type) := (pre : σ → Prop) (post : σ → α → σ → Prop) (action : ∀ s : {s // pre s}, {as' : α × σ // post s.val as'.1 as'.2}) namespace hoare_state protected def return (σ : Type) {α : Type} (a : α) : hoare_state σ α := {pre := λ s, true, post := λ s a' s', s' = s ∧ a' = a, action := λ ⟨s, h⟩, ⟨(a, s), by simp⟩} protected def bind {σ α β : Type} (m₁ : hoare_state σ α) (m₂ : α → hoare_state σ β) : hoare_state σ β := {pre := λ s₁, m₁.pre s₁ ∧ ∀ a s₂, m₁.post s₁ a s₂ → (m₂ a).pre s₂, post := λ s₁ b s₃, ∃ a s₂, m₁.post s₁ a s₂ ∧ (m₂ a).post s₂ b s₃, action := λ ⟨s₁, h₁, h₂⟩, match m₁.action ⟨s₁, h₁⟩ with | ⟨(a, s₂), h₃⟩ := match (m₂ a).action ⟨s₂, h₂ a s₂ h₃⟩ with | ⟨(b, s₃), s₄⟩ := ⟨(b, s₃), ⟨a, s₂, h₃, s₄⟩⟩ end end } protected def assert {σ : Type} (p : σ → Prop) : hoare_state σ unit := {pre := λ s, p s, post := λ s _ s', s' = s ∧ p s', action := λ ⟨s, h⟩, ⟨((), s), ⟨rfl, h⟩⟩} protected def write {σ : Type} (new_s : σ) : hoare_state σ unit := {pre := λ s, true, post := λ s _ s', s' = new_s, action := λ ⟨s, h⟩, ⟨((), new_s), rfl⟩} protected def read {σ : Type} : hoare_state σ σ := {pre := λ s, true, post := λ s a s', s' = s ∧ a = s, action := λ ⟨s, h⟩, ⟨(s, s), by simp⟩} protected def assign {α : Type} (p : nat) (v : α) : hoare_state (buffer α) unit := {pre := λ s, p < s.size, post := λ s _ s', ∀ h : p < s.size, s' = s.write ⟨p, h⟩ v, action := λ ⟨s, h⟩, ⟨((), s.write ⟨p, h⟩ v), by intros; simp⟩} local infix ` ::= `:60 := hoare_state.assign instance (σ : Type) : has_bind (hoare_state σ) := ⟨@hoare_state.bind σ⟩ protected def run {σ α : Type} (a : hoare_state σ α) (s : σ) (h : a.pre s) : {as' : α × σ // a.post s as'.1 as'.2} := a.action ⟨s, h⟩ def test : hoare_state (buffer nat) unit := do 0 ::= 1, 10 ::= 2, 2 ::= 4 lemma size_write (b : buffer nat) (i : nat) (h : i < b.size) (v : nat) : (b.write ⟨i, h⟩ v).size = b.size := begin cases b, unfold buffer.write buffer.size, simp end open nat def init_mem : nat → hoare_state (buffer nat) unit | 0 := 0 ::= 0 | (succ p) := succ p ::= 0 >> init_mem p lemma init_mem_inv : ∀ n (b : buffer nat), n < b.size → (init_mem n).pre b | 0 b h := h | (nat.succ n) b h := have n < b.size, from nat.lt_of_succ_lt h, begin unfold init_mem has_bind.and_then bind has_bind.bind hoare_state.bind, simp, split, {unfold hoare_state.assign, simp, exact h}, {intros _ _, unfold hoare_state.assign, simp, intro h₁, rewrite h₁ h, apply init_mem_inv n, rewrite size_write, assumption} end def run_init_mem (n : nat) (b : buffer nat) (h : n < b.size) : buffer nat := (hoare_state.run (init_mem n) b (init_mem_inv n b h)).1.2 #eval run_init_mem 10 [0,1,2,3,4,5,6,7,8,9,10,11,12,13].to_buffer dec_trivial end hoare_state
1c4875165275e644056fd7e1e1d3ea4eefde1e52
969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb
/src/linear_algebra/quadratic_form.lean
0d78495eb220c77b1884a52c23274d5d2cb809b4
[ "Apache-2.0" ]
permissive
SAAluthwela/mathlib
62044349d72dd63983a8500214736aa7779634d3
83a4b8b990907291421de54a78988c024dc8a552
refs/heads/master
1,679,433,873,417
1,615,998,031,000
1,615,998,031,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
30,397
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Anne Baanen -/ import algebra.invertible import linear_algebra.bilinear_form import linear_algebra.determinant import linear_algebra.special_linear_group /-! # Quadratic forms This file defines quadratic forms over a `R`-module `M`. A quadratic form is a map `Q : M → R` such that (`to_fun_smul`) `Q (a • x) = a * a * Q x` (`polar_...`) The map `polar Q := λ x y, Q (x + y) - Q x - Q y` is bilinear. They come with a scalar multiplication, `(a • Q) x = Q (a • x) = a * a * Q x`, and composition with linear maps `f`, `Q.comp f x = Q (f x)`. ## Main definitions * `quadratic_form.associated`: associated bilinear form * `quadratic_form.pos_def`: positive definite quadratic forms * `quadratic_form.anisotropic`: anisotropic quadratic forms * `quadratic_form.discr`: discriminant of a quadratic form ## Main statements * `quadratic_form.associated_left_inverse`, * `quadratic_form.associated_right_inverse`: in a commutative ring where 2 has an inverse, there is a correspondence between quadratic forms and symmetric bilinear forms * `bilin_form.exists_orthogonal_basis`: There exists an orthogonal basis with respect to any nondegenerate, symmetric bilinear form `B`. ## Notation In this file, the variable `R` is used when a `ring` structure is sufficient and `R₁` is used when specifically a `comm_ring` is required. This allows us to keep `[module R M]` and `[module R₁ M]` assumptions in the variables without confusion between `*` from `ring` and `*` from `comm_ring`. The variable `S` is used when `R` itself has a `•` action. ## References * https://en.wikipedia.org/wiki/Quadratic_form * https://en.wikipedia.org/wiki/Discriminant#Quadratic_forms ## Tags quadratic form, homogeneous polynomial, quadratic polynomial -/ universes u v w variables {S : Type*} variables {R : Type*} {M : Type*} [add_comm_group M] [ring R] variables {R₁ : Type*} [comm_ring R₁] namespace quadratic_form /-- Up to a factor 2, `Q.polar` is the associated bilinear form for a quadratic form `Q`.d Source of this name: https://en.wikipedia.org/wiki/Quadratic_form#Generalization -/ def polar (f : M → R) (x y : M) := f (x + y) - f x - f y lemma polar_add (f g : M → R) (x y : M) : polar (f + g) x y = polar f x y + polar g x y := by { simp only [polar, pi.add_apply], abel } lemma polar_neg (f : M → R) (x y : M) : polar (-f) x y = - polar f x y := by { simp only [polar, pi.neg_apply, sub_eq_add_neg, neg_add] } lemma polar_smul [monoid S] [distrib_mul_action S R] (f : M → R) (s : S) (x y : M) : polar (s • f) x y = s • polar f x y := by { simp only [polar, pi.smul_apply, smul_sub] } lemma polar_comm (f : M → R) (x y : M) : polar f x y = polar f y x := by rw [polar, polar, add_comm, sub_sub, sub_sub, add_comm (f x) (f y)] end quadratic_form variables [module R M] [module R₁ M] open quadratic_form /-- A quadratic form over a module. -/ structure quadratic_form (R : Type u) (M : Type v) [ring R] [add_comm_group M] [module R M] := (to_fun : M → R) (to_fun_smul : ∀ (a : R) (x : M), to_fun (a • x) = a * a * to_fun x) (polar_add_left' : ∀ (x x' y : M), polar to_fun (x + x') y = polar to_fun x y + polar to_fun x' y) (polar_smul_left' : ∀ (a : R) (x y : M), polar to_fun (a • x) y = a • polar to_fun x y) (polar_add_right' : ∀ (x y y' : M), polar to_fun x (y + y') = polar to_fun x y + polar to_fun x y') (polar_smul_right' : ∀ (a : R) (x y : M), polar to_fun x (a • y) = a • polar to_fun x y) namespace quadratic_form variables {Q : quadratic_form R M} instance : has_coe_to_fun (quadratic_form R M) := ⟨_, λ B, B.to_fun⟩ /-- The `simp` normal form for a quadratic form is `coe_fn`, not `to_fun`. -/ @[simp] lemma to_fun_eq_apply : Q.to_fun = ⇑ Q := rfl lemma map_smul (a : R) (x : M) : Q (a • x) = a * a * Q x := Q.to_fun_smul a x lemma map_add_self (x : M) : Q (x + x) = 4 * Q x := by { rw [←one_smul R x, ←add_smul, map_smul], norm_num } @[simp] lemma map_zero : Q 0 = 0 := by rw [←@zero_smul R _ _ _ _ (0 : M), map_smul, zero_mul, zero_mul] @[simp] lemma map_neg (x : M) : Q (-x) = Q x := by rw [←@neg_one_smul R _ _ _ _ x, map_smul, neg_one_mul, neg_neg, one_mul] lemma map_sub (x y : M) : Q (x - y) = Q (y - x) := by rw [←neg_sub, map_neg] @[simp] lemma polar_zero_left (y : M) : polar Q 0 y = 0 := by simp [polar] @[simp] lemma polar_add_left (x x' y : M) : polar Q (x + x') y = polar Q x y + polar Q x' y := Q.polar_add_left' x x' y @[simp] lemma polar_smul_left (a : R) (x y : M) : polar Q (a • x) y = a * polar Q x y := Q.polar_smul_left' a x y @[simp] lemma polar_neg_left (x y : M) : polar Q (-x) y = -polar Q x y := by rw [←neg_one_smul R x, polar_smul_left, neg_one_mul] @[simp] lemma polar_sub_left (x x' y : M) : polar Q (x - x') y = polar Q x y - polar Q x' y := by rw [sub_eq_add_neg, sub_eq_add_neg, polar_add_left, polar_neg_left] @[simp] lemma polar_zero_right (y : M) : polar Q y 0 = 0 := by simp [polar] @[simp] lemma polar_add_right (x y y' : M) : polar Q x (y + y') = polar Q x y + polar Q x y' := Q.polar_add_right' x y y' @[simp] lemma polar_smul_right (a : R) (x y : M) : polar Q x (a • y) = a * polar Q x y := Q.polar_smul_right' a x y @[simp] lemma polar_neg_right (x y : M) : polar Q x (-y) = -polar Q x y := by rw [←neg_one_smul R y, polar_smul_right, neg_one_mul] @[simp] lemma polar_sub_right (x y y' : M) : polar Q x (y - y') = polar Q x y - polar Q x y' := by rw [sub_eq_add_neg, sub_eq_add_neg, polar_add_right, polar_neg_right] @[simp] lemma polar_self (x : M) : polar Q x x = 2 * Q x := begin rw [polar, map_add_self, sub_sub, sub_eq_iff_eq_add, ←two_mul, ←two_mul, ←mul_assoc], norm_num end section of_tower variables [comm_semiring S] [algebra S R] [semimodule S M] [is_scalar_tower S R M] @[simp] lemma polar_smul_left_of_tower (a : S) (x y : M) : polar Q (a • x) y = a • polar Q x y := by rw [←is_scalar_tower.algebra_map_smul R a x, polar_smul_left, algebra.smul_def] @[simp] lemma polar_smul_right_of_tower (a : S) (x y : M) : polar Q x (a • y) = a • polar Q x y := by rw [←is_scalar_tower.algebra_map_smul R a y, polar_smul_right, algebra.smul_def] end of_tower variable {Q' : quadratic_form R M} @[ext] lemma ext (H : ∀ (x : M), Q x = Q' x) : Q = Q' := by { cases Q, cases Q', congr, funext, apply H } instance : has_zero (quadratic_form R M) := ⟨ { to_fun := λ x, 0, to_fun_smul := λ a x, by simp, polar_add_left' := λ x x' y, by simp [polar], polar_smul_left' := λ a x y, by simp [polar], polar_add_right' := λ x y y', by simp [polar], polar_smul_right' := λ a x y, by simp [polar] } ⟩ @[simp] lemma zero_apply (x : M) : (0 : quadratic_form R M) x = 0 := rfl instance : inhabited (quadratic_form R M) := ⟨0⟩ instance : has_add (quadratic_form R M) := ⟨ λ Q Q', { to_fun := Q + Q', to_fun_smul := λ a x, by simp only [pi.add_apply, map_smul, mul_add], polar_add_left' := λ x x' y, by simp only [polar_add, polar_add_left, add_assoc, add_left_comm], polar_smul_left' := λ a x y, by simp only [polar_add, smul_eq_mul, mul_add, polar_smul_left], polar_add_right' := λ x y y', by simp only [polar_add, polar_add_right, add_assoc, add_left_comm], polar_smul_right' := λ a x y, by simp only [polar_add, smul_eq_mul, mul_add, polar_smul_right] } ⟩ @[simp] lemma coe_fn_add (Q Q' : quadratic_form R M) : ⇑(Q + Q') = Q + Q' := rfl @[simp] lemma add_apply (Q Q' : quadratic_form R M) (x : M) : (Q + Q') x = Q x + Q' x := rfl instance : has_neg (quadratic_form R M) := ⟨ λ Q, { to_fun := -Q, to_fun_smul := λ a x, by simp only [pi.neg_apply, map_smul, mul_neg_eq_neg_mul_symm], polar_add_left' := λ x x' y, by simp only [polar_neg, polar_add_left, neg_add], polar_smul_left' := λ a x y, by simp only [polar_neg, polar_smul_left, mul_neg_eq_neg_mul_symm, smul_eq_mul], polar_add_right' := λ x y y', by simp only [polar_neg, polar_add_right, neg_add], polar_smul_right' := λ a x y, by simp only [polar_neg, polar_smul_right, mul_neg_eq_neg_mul_symm, smul_eq_mul] } ⟩ @[simp] lemma coe_fn_neg (Q : quadratic_form R M) : ⇑(-Q) = -Q := rfl @[simp] lemma neg_apply (Q : quadratic_form R M) (x : M) : (-Q) x = -Q x := rfl instance : add_comm_group (quadratic_form R M) := { add := (+), zero := 0, neg := has_neg.neg, add_comm := λ Q Q', by { ext, simp only [add_apply, add_comm] }, add_assoc := λ Q Q' Q'', by { ext, simp only [add_apply, add_assoc] }, add_left_neg := λ Q, by { ext, simp only [add_apply, neg_apply, zero_apply, add_left_neg] }, add_zero := λ Q, by { ext, simp only [zero_apply, add_apply, add_zero] }, zero_add := λ Q, by { ext, simp only [zero_apply, add_apply, zero_add] } } @[simp] lemma coe_fn_sub (Q Q' : quadratic_form R M) : ⇑(Q - Q') = Q - Q' := by simp [sub_eq_add_neg] @[simp] lemma sub_apply (Q Q' : quadratic_form R M) (x : M) : (Q - Q') x = Q x - Q' x := by simp [sub_eq_add_neg] section has_scalar variables [comm_semiring S] [algebra S R] /-- `quadratic_form R M` inherits the scalar action from any algebra over `R`. When `R` is commutative, this provides an `R`-action via `algebra.id`. -/ instance : has_scalar S (quadratic_form R M) := ⟨ λ a Q, { to_fun := a • Q, to_fun_smul := λ b x, by rw [pi.smul_apply, map_smul, pi.smul_apply, algebra.mul_smul_comm], polar_add_left' := λ x x' y, by simp only [polar_smul, polar_add_left, smul_add], polar_smul_left' := λ b x y, begin simp only [polar_smul, polar_smul_left, ←algebra.mul_smul_comm, smul_eq_mul], end, polar_add_right' := λ x y y', by simp only [polar_smul, polar_add_right, smul_add], polar_smul_right' := λ b x y, begin simp only [polar_smul, polar_smul_right, ←algebra.mul_smul_comm, smul_eq_mul], end } ⟩ @[simp] lemma coe_fn_smul (a : S) (Q : quadratic_form R M) : ⇑(a • Q) = a • Q := rfl @[simp] lemma smul_apply (a : S) (Q : quadratic_form R M) (x : M) : (a • Q) x = a • Q x := rfl instance : semimodule S (quadratic_form R M) := { mul_smul := λ a b Q, ext (λ x, by simp only [smul_apply, mul_left_comm, ←smul_eq_mul, smul_assoc]), one_smul := λ Q, ext (λ x, by simp), smul_add := λ a Q Q', by { ext, simp only [add_apply, smul_apply, smul_add] }, smul_zero := λ a, by { ext, simp only [zero_apply, smul_apply, smul_zero] }, zero_smul := λ Q, by { ext, simp only [zero_apply, smul_apply, zero_smul] }, add_smul := λ a b Q, by { ext, simp only [add_apply, smul_apply, add_smul] } } end has_scalar section comp variables {N : Type v} [add_comm_group N] [module R N] /-- Compose the quadratic form with a linear function. -/ def comp (Q : quadratic_form R N) (f : M →ₗ[R] N) : quadratic_form R M := { to_fun := λ x, Q (f x), to_fun_smul := λ a x, by simp only [map_smul, f.map_smul], polar_add_left' := λ x x' y, by convert polar_add_left (f x) (f x') (f y) using 1; simp only [polar, f.map_add], polar_smul_left' := λ a x y, by convert polar_smul_left a (f x) (f y) using 1; simp only [polar, f.map_smul, f.map_add, smul_eq_mul], polar_add_right' := λ x y y', by convert polar_add_right (f x) (f y) (f y') using 1; simp only [polar, f.map_add], polar_smul_right' := λ a x y, by convert polar_smul_right a (f x) (f y) using 1; simp only [polar, f.map_smul, f.map_add, smul_eq_mul] } @[simp] lemma comp_apply (Q : quadratic_form R N) (f : M →ₗ[R] N) (x : M) : (Q.comp f) x = Q (f x) := rfl end comp section comm_ring /-- Create a quadratic form in a commutative ring by proving only one side of the bilinearity. -/ def mk_left (f : M → R₁) (to_fun_smul : ∀ a x, f (a • x) = a * a * f x) (polar_add_left : ∀ x x' y, polar f (x + x') y = polar f x y + polar f x' y) (polar_smul_left : ∀ a x y, polar f (a • x) y = a * polar f x y) : quadratic_form R₁ M := { to_fun := f, to_fun_smul := to_fun_smul, polar_add_left' := polar_add_left, polar_smul_left' := polar_smul_left, polar_add_right' := λ x y y', by rw [polar_comm, polar_add_left, polar_comm f y x, polar_comm f y' x], polar_smul_right' := λ a x y, by rw [polar_comm, polar_smul_left, polar_comm f y x, smul_eq_mul] } /-- The product of linear forms is a quadratic form. -/ def lin_mul_lin (f g : M →ₗ[R₁] R₁) : quadratic_form R₁ M := mk_left (f * g) (λ a x, by { simp, ring }) (λ x x' y, by { simp [polar], ring }) (λ a x y, by { simp [polar], ring }) @[simp] lemma lin_mul_lin_apply (f g : M →ₗ[R₁] R₁) (x) : lin_mul_lin f g x = f x * g x := rfl @[simp] lemma add_lin_mul_lin (f g h : M →ₗ[R₁] R₁) : lin_mul_lin (f + g) h = lin_mul_lin f h + lin_mul_lin g h := ext (λ x, add_mul _ _ _) @[simp] lemma lin_mul_lin_add (f g h : M →ₗ[R₁] R₁) : lin_mul_lin f (g + h) = lin_mul_lin f g + lin_mul_lin f h := ext (λ x, mul_add _ _ _) variables {N : Type v} [add_comm_group N] [module R₁ N] @[simp] lemma lin_mul_lin_comp (f g : M →ₗ[R₁] R₁) (h : N →ₗ[R₁] M) : (lin_mul_lin f g).comp h = lin_mul_lin (f.comp h) (g.comp h) := rfl variables {n : Type*} /-- `proj i j` is the quadratic form mapping the vector `x : n → R₁` to `x i * x j` -/ def proj (i j : n) : quadratic_form R₁ (n → R₁) := lin_mul_lin (@linear_map.proj _ _ _ (λ _, R₁) _ _ i) (@linear_map.proj _ _ _ (λ _, R₁) _ _ j) @[simp] lemma proj_apply (i j : n) (x : n → R₁) : proj i j x = x i * x j := rfl end comm_ring end quadratic_form /-! ### Associated bilinear forms Over a commutative ring with an inverse of 2, the theory of quadratic forms is basically identical to that of symmetric bilinear forms. The map from quadratic forms to bilinear forms giving this identification is called the `associated` quadratic form. -/ variables {B : bilin_form R M} namespace bilin_form open quadratic_form lemma polar_to_quadratic_form (x y : M) : polar (λ x, B x x) x y = B x y + B y x := by simp [polar, add_left, add_right, sub_eq_add_neg _ (B y y), add_comm (B y x) _, add_assoc] /-- A bilinear form gives a quadratic form by applying the argument twice. -/ def to_quadratic_form (B : bilin_form R M) : quadratic_form R M := ⟨ λ x, B x x, λ a x, by simp [smul_left, smul_right, mul_assoc], λ x x' y, by simp [polar_to_quadratic_form, add_left, add_right, add_left_comm, add_assoc], λ a x y, by simp [polar_to_quadratic_form, smul_left, smul_right, mul_add], λ x y y', by simp [polar_to_quadratic_form, add_left, add_right, add_left_comm, add_assoc], λ a x y, by simp [polar_to_quadratic_form, smul_left, smul_right, mul_add] ⟩ @[simp] lemma to_quadratic_form_apply (B : bilin_form R M) (x : M) : B.to_quadratic_form x = B x x := rfl end bilin_form namespace quadratic_form open bilin_form sym_bilin_form section associated_hom variables (S) [comm_semiring S] [algebra S R] variables [invertible (2 : R)] {B₁ : bilin_form R M} /-- `associated_hom` is the map that sends a quadratic form on a module `M` over `R` to its associated symmetric bilinear form. As provided here, this has the structure of an `S`-linear map where `S` is a commutative subring of `R`. Over a commutative ring, use `associated`, which gives an `R`-linear map. Over a general ring with no nontrivial distinguished commutative subring, use `associated'`, which gives an additive homomorphism (or more precisely a `ℤ`-linear map.) -/ def associated_hom : quadratic_form R M →ₗ[S] bilin_form R M := { to_fun := λ Q, { bilin := λ x y, ⅟2 * polar Q x y, bilin_add_left := λ x y z, by rw [← mul_add, polar_add_left], bilin_smul_left := λ x y z, begin have htwo : x * ⅟2 = ⅟2 * x := (commute.one_right x).bit0_right.inv_of_right, simp [polar_smul_left, ← mul_assoc, htwo] end, bilin_add_right := λ x y z, by rw [← mul_add, polar_add_right], bilin_smul_right := λ x y z, begin have htwo : x * ⅟2 = ⅟2 * x := (commute.one_right x).bit0_right.inv_of_right, simp [polar_smul_left, ← mul_assoc, htwo] end }, map_add' := λ Q Q', by { ext, simp [bilin_form.add_apply, polar_add, mul_add] }, map_smul' := λ s Q, by { ext, simp [polar_smul, algebra.mul_smul_comm] } } variables {Q : quadratic_form R M} {S} @[simp] lemma associated_apply (x y : M) : associated_hom S Q x y = ⅟2 * (Q (x + y) - Q x - Q y) := rfl lemma associated_is_sym : is_sym (associated_hom S Q) := λ x y, by simp only [associated_apply, add_comm, add_left_comm, sub_eq_add_neg] @[simp] lemma associated_comp {N : Type v} [add_comm_group N] [module R N] (f : N →ₗ[R] M) : associated_hom S (Q.comp f) = (associated_hom S Q).comp f f := by { ext, simp } lemma associated_to_quadratic_form (B : bilin_form R M) (x y : M) : associated_hom S B.to_quadratic_form x y = ⅟2 * (B x y + B y x) := by simp [associated_apply, ←polar_to_quadratic_form, polar] lemma associated_left_inverse (h : is_sym B₁) : associated_hom S (B₁.to_quadratic_form) = B₁ := bilin_form.ext $ λ x y, by rw [associated_to_quadratic_form, sym h x y, ←two_mul, ←mul_assoc, inv_of_mul_self, one_mul] lemma associated_right_inverse : (associated_hom S Q).to_quadratic_form = Q := quadratic_form.ext $ λ x, calc (associated_hom S Q).to_quadratic_form x = ⅟2 * (Q x + Q x) : by simp [map_add_self, bit0, add_mul, add_assoc] ... = Q x : by rw [← two_mul (Q x), ←mul_assoc, inv_of_mul_self, one_mul] lemma associated_eq_self_apply (x : M) : associated_hom S Q x x = Q x := begin rw [associated_apply, map_add_self], suffices : (⅟2) * (2 * Q x) = Q x, { convert this, simp only [bit0, add_mul, one_mul], abel }, simp [← mul_assoc], end /-- `associated'` is the `ℤ`-linear map that sends a quadratic form on a module `M` over `R` to its associated symmetric bilinear form. -/ abbreviation associated' : quadratic_form R M →ₗ[ℤ] bilin_form R M := associated_hom ℤ /-- There exists a non-null vector with respect to any quadratic form `Q` whose associated bilinear form is non-degenerate, i.e. there exists `x` such that `Q x ≠ 0`. -/ lemma exists_quadratic_form_neq_zero [nontrivial M] {Q : quadratic_form R M} (hB₁ : Q.associated'.nondegenerate) : ∃ x, Q x ≠ 0 := begin rw nondegenerate at hB₁, contrapose! hB₁, obtain ⟨x, hx⟩ := exists_ne (0 : M), refine ⟨x, λ y, _, hx⟩, have : Q = 0 := quadratic_form.ext hB₁, simp [this] end end associated_hom section associated variables [invertible (2 : R₁)] -- Note: When possible, rather than writing lemmas about `associated`, write a lemma applying to -- the more general `associated_hom` and place it in the previous section. /-- `associated` is the linear map that sends a quadratic form over a commutative ring to its associated symmetric bilinear form. -/ abbreviation associated : quadratic_form R₁ M →ₗ[R₁] bilin_form R₁ M := associated_hom R₁ @[simp] lemma associated_lin_mul_lin (f g : M →ₗ[R₁] R₁) : (lin_mul_lin f g).associated = ⅟(2 : R₁) • (bilin_form.lin_mul_lin f g + bilin_form.lin_mul_lin g f) := by { ext, simp [bilin_form.add_apply, bilin_form.smul_apply], ring } end associated section anisotropic /-- An anisotropic quadratic form is zero only on zero vectors. -/ def anisotropic (Q : quadratic_form R M) : Prop := ∀ x, Q x = 0 → x = 0 lemma not_anisotropic_iff_exists (Q : quadratic_form R M) : ¬anisotropic Q ↔ ∃ x ≠ 0, Q x = 0 := by simp only [anisotropic, not_forall, exists_prop, and_comm] /-- The associated bilinear form of an anisotropic quadratic form is nondegenerate. -/ lemma nondegenerate_of_anisotropic [invertible (2 : R)] (Q : quadratic_form R M) (hB : Q.anisotropic) : Q.associated'.nondegenerate := begin intros x hx, refine hB _ _, rw ← hx x, exact (associated_eq_self_apply x).symm, end end anisotropic section pos_def variables {R₂ : Type u} [ordered_ring R₂] [module R₂ M] {Q₂ : quadratic_form R₂ M} /-- A positive definite quadratic form is positive on nonzero vectors. -/ def pos_def (Q₂ : quadratic_form R₂ M) : Prop := ∀ x ≠ 0, 0 < Q₂ x lemma pos_def.smul {R} [linear_ordered_comm_ring R] [module R M] {Q : quadratic_form R M} (h : pos_def Q) {a : R} (a_pos : 0 < a) : pos_def (a • Q) := λ x hx, mul_pos a_pos (h x hx) variables {n : Type*} lemma pos_def.add (Q Q' : quadratic_form R₂ M) (hQ : pos_def Q) (hQ' : pos_def Q') : pos_def (Q + Q') := λ x hx, add_pos (hQ x hx) (hQ' x hx) lemma lin_mul_lin_self_pos_def {R} [linear_ordered_comm_ring R] [module R M] (f : M →ₗ[R] R) (hf : linear_map.ker f = ⊥) : pos_def (lin_mul_lin f f) := λ x hx, mul_self_pos (λ h, hx (linear_map.ker_eq_bot.mp hf (by rw [h, linear_map.map_zero]))) end pos_def end quadratic_form section /-! ### Quadratic forms and matrices Connect quadratic forms and matrices, in order to explicitly compute with them. The convention is twos out, so there might be a factor 2⁻¹ in the entries of the matrix. The determinant of the matrix is the discriminant of the quadratic form. -/ variables {n : Type w} [fintype n] [decidable_eq n] /-- `M.to_quadratic_form` is the map `λ x, col x ⬝ M ⬝ row x` as a quadratic form. -/ def matrix.to_quadratic_form' (M : matrix n n R₁) : quadratic_form R₁ (n → R₁) := M.to_bilin'.to_quadratic_form variables [invertible (2 : R₁)] /-- A matrix representation of the quadratic form. -/ def quadratic_form.to_matrix' (Q : quadratic_form R₁ (n → R₁)) : matrix n n R₁ := Q.associated.to_matrix' open quadratic_form lemma quadratic_form.to_matrix'_smul (a : R₁) (Q : quadratic_form R₁ (n → R₁)) : (a • Q).to_matrix' = a • Q.to_matrix' := by simp only [to_matrix', linear_equiv.map_smul, linear_map.map_smul] end namespace quadratic_form variables {n : Type w} [fintype n] variables [decidable_eq n] [invertible (2 : R₁)] variables {m : Type w} [decidable_eq m] [fintype m] open_locale matrix @[simp] lemma to_matrix'_comp (Q : quadratic_form R₁ (m → R₁)) (f : (n → R₁) →ₗ[R₁] (m → R₁)) : (Q.comp f).to_matrix' = f.to_matrix'ᵀ ⬝ Q.to_matrix' ⬝ f.to_matrix' := by { ext, simp [to_matrix', bilin_form.to_matrix'_comp] } section discriminant variables {Q : quadratic_form R₁ (n → R₁)} /-- The discriminant of a quadratic form generalizes the discriminant of a quadratic polynomial. -/ def discr (Q : quadratic_form R₁ (n → R₁)) : R₁ := Q.to_matrix'.det lemma discr_smul (a : R₁) : (a • Q).discr = a ^ fintype.card n * Q.discr := by simp only [discr, to_matrix'_smul, matrix.det_smul] lemma discr_comp (f : (n → R₁) →ₗ[R₁] (n → R₁)) : (Q.comp f).discr = f.to_matrix'.det * f.to_matrix'.det * Q.discr := by simp [discr, mul_left_comm, mul_comm] end discriminant end quadratic_form namespace quadratic_form variables {M₁ : Type*} {M₂ : Type*} {M₃ : Type*} variables [add_comm_group M₁] [add_comm_group M₂] [add_comm_group M₃] variables [module R M₁] [module R M₂] [module R M₃] /-- An isometry between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`, is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/ @[nolint has_inhabited_instance] structure isometry (Q₁ : quadratic_form R M₁) (Q₂ : quadratic_form R M₂) extends M₁ ≃ₗ[R] M₂ := (map_app' : ∀ m, Q₂ (to_fun m) = Q₁ m) /-- Two quadratic forms over a ring `R` are equivalent if there exists an isometry between them: a linear equivalence that transforms one quadratic form into the other. -/ def equivalent (Q₁ : quadratic_form R M₁) (Q₂ : quadratic_form R M₂) := nonempty (Q₁.isometry Q₂) namespace isometry variables {Q₁ : quadratic_form R M₁} {Q₂ : quadratic_form R M₂} {Q₃ : quadratic_form R M₃} instance : has_coe (Q₁.isometry Q₂) (M₁ ≃ₗ[R] M₂) := ⟨isometry.to_linear_equiv⟩ instance : has_coe_to_fun (Q₁.isometry Q₂) := { F := λ _, M₁ → M₂, coe := λ f, ⇑(f : M₁ ≃ₗ[R] M₂) } @[simp] lemma map_app (f : Q₁.isometry Q₂) (m : M₁) : Q₂ (f m) = Q₁ m := f.map_app' m /-- The identity isometry from a quadratic form to itself. -/ @[refl] def refl (Q : quadratic_form R M) : Q.isometry Q := { map_app' := λ m, rfl, .. linear_equiv.refl R M } /-- The inverse isometry of an isometry between two quadratic forms. -/ @[symm] def symm (f : Q₁.isometry Q₂) : Q₂.isometry Q₁ := { map_app' := by { intro m, rw ← f.map_app, congr, exact f.to_linear_equiv.apply_symm_apply m }, .. (f : M₁ ≃ₗ[R] M₂).symm } /-- The composition of two isometries between quadratic forms. -/ @[trans] def trans (f : Q₁.isometry Q₂) (g : Q₂.isometry Q₃) : Q₁.isometry Q₃ := { map_app' := by { intro m, rw [← f.map_app, ← g.map_app], refl }, .. (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) } end isometry namespace equivalent variables {Q₁ : quadratic_form R M₁} {Q₂ : quadratic_form R M₂} {Q₃ : quadratic_form R M₃} @[refl] lemma refl (Q : quadratic_form R M) : Q.equivalent Q := ⟨isometry.refl Q⟩ @[symm] lemma symm (h : Q₁.equivalent Q₂) : Q₂.equivalent Q₁ := h.elim $ λ f, ⟨f.symm⟩ @[trans] lemma trans (h : Q₁.equivalent Q₂) (h' : Q₂.equivalent Q₃) : Q₁.equivalent Q₃ := h'.elim $ h.elim $ λ f g, ⟨f.trans g⟩ end equivalent end quadratic_form namespace bilin_form /-- A bilinear form is nondegenerate if the quadratic form it is associated with is anisotropic. -/ lemma nondegenerate_of_anisotropic {B : bilin_form R M} (hB : B.to_quadratic_form.anisotropic) : B.nondegenerate := λ x hx, hB _ (hx x) /-- There exists a non-null vector with respect to any symmetric, nondegenerate bilinear form `B` on a nontrivial module `M` over a ring `R` with invertible `2`, i.e. there exists some `x : M` such that `B x x ≠ 0`. -/ lemma exists_bilin_form_self_neq_zero [htwo : invertible (2 : R)] [nontrivial M] {B : bilin_form R M} (hB₁ : B.nondegenerate) (hB₂ : sym_bilin_form.is_sym B) : ∃ x, ¬ B.is_ortho x x := begin have : B.to_quadratic_form.associated'.nondegenerate, { simpa [quadratic_form.associated_left_inverse hB₂] using hB₁ }, obtain ⟨x, hx⟩ := quadratic_form.exists_quadratic_form_neq_zero this, refine ⟨x, λ h, hx (B.to_quadratic_form_apply x ▸ h)⟩, end open finite_dimensional variables {V : Type u} {K : Type v} [field K] [add_comm_group V] [vector_space K V] variable [finite_dimensional K V] -- We start proving that symmetric nondegenerate bilinear forms are diagonalisable, or equivalently -- there exists a orthogonal basis with respect to any symmetric nondegenerate bilinear form. lemma exists_orthogonal_basis' [hK : invertible (2 : K)] {B : bilin_form K V} (hB₁ : B.nondegenerate) (hB₂ : sym_bilin_form.is_sym B) : ∃ v : fin (findim K V) → V, B.is_Ortho v ∧ is_basis K v ∧ ∀ i, B (v i) (v i) ≠ 0 := begin tactic.unfreeze_local_instances, induction hd : findim K V with d ih generalizing V, { exact ⟨λ _, 0, λ _ _ _, zero_left _, is_basis_of_findim_zero' hd, fin.elim0⟩ }, { haveI := findim_pos_iff.1 (hd.symm ▸ nat.succ_pos d : 0 < findim K V), cases exists_bilin_form_self_neq_zero hB₁ hB₂ with x hx, { have hd' := hd, rw [← submodule.findim_add_eq_of_is_compl (is_compl_span_singleton_orthogonal hx).symm, findim_span_singleton (ne_zero_of_not_is_ortho_self x hx)] at hd, rcases @ih (B.orthogonal $ K ∙ x) _ _ _ (B.restrict _) (B.restrict_orthogonal_span_singleton_nondegenerate hB₁ hB₂ hx) (B.restrict_sym hB₂ _) (nat.succ.inj hd) with ⟨v', hv₁, hv₂, hv₃⟩, refine ⟨λ i, if h : i ≠ 0 then coe (v' (i.pred h)) else x, λ i j hij, _, _, _⟩, { by_cases hi : i = 0, { subst i, simp only [eq_self_iff_true, not_true, ne.def, dif_neg, not_false_iff, dite_not], rw [dif_neg hij.symm, is_ortho, hB₂], exact (v' (j.pred hij.symm)).2 _ (submodule.mem_span_singleton_self x) }, by_cases hj : j = 0, { subst j, simp only [eq_self_iff_true, not_true, ne.def, dif_neg, not_false_iff, dite_not], rw dif_neg hi, exact (v' (i.pred hi)).2 _ (submodule.mem_span_singleton_self x) }, { simp_rw [dif_pos hi, dif_pos hj], rw [is_ortho, hB₂], exact hv₁ (j.pred hj) (i.pred hi) (by simpa using hij.symm) } }, { refine is_basis_of_linear_independent_of_card_eq_findim (@linear_independent_of_is_Ortho _ _ _ _ _ _ B _ _ _) (by rw [hd', fintype.card_fin]), { intros i j hij, by_cases hi : i = 0, { subst hi, simp only [eq_self_iff_true, not_true, ne.def, dif_neg, not_false_iff, dite_not], rw [dif_neg hij.symm, is_ortho, hB₂], exact (v' (j.pred hij.symm)).2 _ (submodule.mem_span_singleton_self x) }, by_cases hj : j = 0, { subst j, simp only [eq_self_iff_true, not_true, ne.def, dif_neg, not_false_iff, dite_not], rw dif_neg hi, exact (v' (i.pred hi)).2 _ (submodule.mem_span_singleton_self x) }, { simp_rw [dif_pos hi, dif_pos hj], rw [is_ortho, hB₂], exact hv₁ (j.pred hj) (i.pred hi) (by simpa using hij.symm) } }, { intro i, by_cases hi : i ≠ 0, { rw dif_pos hi, exact hv₃ (i.pred hi) }, { rw dif_neg hi, exact hx } } }, { intro i, by_cases hi : i ≠ 0, { rw dif_pos hi, exact hv₃ (i.pred hi) }, { rw dif_neg hi, exact hx } } } } end . /-- Given a nondegenerate symmetric bilinear form `B` on some vector space `V` over the field `K` with invertible `2`, there exists an orthogonal basis with respect to `B`. -/ theorem exists_orthogonal_basis [hK : invertible (2 : K)] {B : bilin_form K V} (hB₁ : B.nondegenerate) (hB₂ : sym_bilin_form.is_sym B) : ∃ v : fin (findim K V) → V, B.is_Ortho v ∧ is_basis K v := let ⟨v, hv₁, hv₂, _⟩ := exists_orthogonal_basis' hB₁ hB₂ in ⟨v, hv₁, hv₂⟩ end bilin_form
a13037897ee20784851f32d36760669a9a6fc568
367134ba5a65885e863bdc4507601606690974c1
/src/algebra/ordered_monoid.lean
42d29f05fa292eeb852ac1c5aa890ba81ee45f49
[ "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
47,585
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import algebra.group.with_one import algebra.group.type_tags import algebra.group.prod import algebra.order_functions import order.bounded_lattice /-! # Ordered monoids This file develops the basics of ordered monoids. ## Implementation details Unfortunately, the number of `'` appended to lemmas in this file may differ between the multiplicative and the additive version of a lemma. The reason is that we did not want to change existing names in the library. -/ set_option old_structure_cmd true universe u variable {α : Type u} /-- An ordered commutative monoid is a commutative monoid with a partial order such that * `a ≤ b → c * a ≤ c * b` (multiplication is monotone) * `a * b < a * c → b < c`. -/ @[protect_proj, ancestor comm_monoid partial_order] class ordered_comm_monoid (α : Type*) extends comm_monoid α, partial_order α := (mul_le_mul_left : ∀ a b : α, a ≤ b → ∀ c : α, c * a ≤ c * b) (lt_of_mul_lt_mul_left : ∀ a b c : α, a * b < a * c → b < c) /-- An ordered (additive) commutative monoid is a commutative monoid with a partial order such that * `a ≤ b → c + a ≤ c + b` (addition is monotone) * `a + b < a + c → b < c`. -/ @[protect_proj, ancestor add_comm_monoid partial_order] class ordered_add_comm_monoid (α : Type*) extends add_comm_monoid α, partial_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) (lt_of_add_lt_add_left : ∀ a b c : α, a + b < a + c → b < c) attribute [to_additive] ordered_comm_monoid /-- A linearly ordered additive commutative monoid. -/ @[protect_proj, ancestor linear_order ordered_add_comm_monoid] class linear_ordered_add_comm_monoid (α : Type*) extends linear_order α, ordered_add_comm_monoid α := (lt_of_add_lt_add_left := λ x y z, by { apply imp_of_not_imp_not, intro h, apply not_lt_of_le, apply add_le_add_left, -- type-class inference uses `a : linear_order α` which it can't unfold, unless we provide this! -- `lt_iff_le_not_le` gets filled incorrectly with `autoparam` if we don't provide that field. letI : linear_order α := by refine { le := le, lt := lt, lt_iff_le_not_le := _, .. }; assumption, exact le_of_not_lt h }) /-- A linearly ordered commutative monoid. -/ @[protect_proj, ancestor linear_order ordered_comm_monoid, to_additive] class linear_ordered_comm_monoid (α : Type*) extends linear_order α, ordered_comm_monoid α := (lt_of_mul_lt_mul_left := λ x y z, by { apply imp_of_not_imp_not, intro h, apply not_lt_of_le, apply mul_le_mul_left, -- type-class inference uses `a : linear_order α` which it can't unfold, unless we provide this! -- `lt_iff_le_not_le` gets filled incorrectly with `autoparam` if we don't provide that field. letI : linear_order α := by refine { le := le, lt := lt, lt_iff_le_not_le := _, .. }; assumption, exact le_of_not_lt h }) /-- A linearly ordered commutative monoid with a zero element. -/ class linear_ordered_comm_monoid_with_zero (α : Type*) extends linear_ordered_comm_monoid α, comm_monoid_with_zero α := (zero_le_one : (0 : α) ≤ 1) /-- A linearly ordered commutative monoid with an additively absorbing `⊤` element. Instances should include number systems with an infinite element adjoined.` -/ @[protect_proj, ancestor linear_ordered_add_comm_monoid order_top] class linear_ordered_add_comm_monoid_with_top (α : Type*) extends linear_ordered_add_comm_monoid α, order_top α := (top_add' : ∀ x : α, ⊤ + x = ⊤) section linear_ordered_add_comm_monoid_with_top variables [linear_ordered_add_comm_monoid_with_top α] {a b : α} @[simp] lemma top_add (a : α) : ⊤ + a = ⊤ := linear_ordered_add_comm_monoid_with_top.top_add' a @[simp] lemma add_top (a : α) : a + ⊤ = ⊤ := by rw [add_comm, top_add] end linear_ordered_add_comm_monoid_with_top section ordered_comm_monoid variables [ordered_comm_monoid α] {a b c d : α} @[to_additive add_le_add_left] lemma mul_le_mul_left' (h : a ≤ b) (c) : c * a ≤ c * b := ordered_comm_monoid.mul_le_mul_left a b h c @[to_additive add_le_add_right] lemma mul_le_mul_right' (h : a ≤ b) (c) : a * c ≤ b * c := by { convert mul_le_mul_left' h c using 1; rw mul_comm } @[to_additive] lemma mul_lt_of_mul_lt_left (h : a * b < c) (hle : d ≤ b) : a * d < c := (mul_le_mul_left' hle a).trans_lt h @[to_additive] lemma mul_lt_of_mul_lt_right (h : a * b < c) (hle : d ≤ a) : d * b < c := (mul_le_mul_right' hle b).trans_lt h @[to_additive] lemma mul_le_of_mul_le_left (h : a * b ≤ c) (hle : d ≤ b) : a * d ≤ c := (mul_le_mul_left' hle a).trans h @[to_additive] lemma mul_le_of_mul_le_right (h : a * b ≤ c) (hle : d ≤ a) : d * b ≤ c := (mul_le_mul_right' hle b).trans h @[to_additive] lemma lt_mul_of_lt_mul_left (h : a < b * c) (hle : c ≤ d) : a < b * d := h.trans_le (mul_le_mul_left' hle b) @[to_additive] lemma lt_mul_of_lt_mul_right (h : a < b * c) (hle : b ≤ d) : a < d * c := h.trans_le (mul_le_mul_right' hle c) @[to_additive] lemma le_mul_of_le_mul_left (h : a ≤ b * c) (hle : c ≤ d) : a ≤ b * d := h.trans (mul_le_mul_left' hle b) @[to_additive] lemma le_mul_of_le_mul_right (h : a ≤ b * c) (hle : b ≤ d) : a ≤ d * c := h.trans (mul_le_mul_right' hle c) @[to_additive lt_of_add_lt_add_left] lemma lt_of_mul_lt_mul_left' : a * b < a * c → b < c := ordered_comm_monoid.lt_of_mul_lt_mul_left a b c @[to_additive lt_of_add_lt_add_right] lemma lt_of_mul_lt_mul_right' (h : a * b < c * b) : a < c := lt_of_mul_lt_mul_left' (show b * a < b * c, begin rw [mul_comm b a, mul_comm b c], assumption end) @[to_additive add_le_add] lemma mul_le_mul' (h₁ : a ≤ b) (h₂ : c ≤ d) : a * c ≤ b * d := (mul_le_mul_right' h₁ _).trans $ mul_le_mul_left' h₂ _ @[to_additive] lemma mul_le_mul_three {e f : α} (h₁ : a ≤ d) (h₂ : b ≤ e) (h₃ : c ≤ f) : a * b * c ≤ d * e * f := mul_le_mul' (mul_le_mul' h₁ h₂) h₃ -- here we start using properties of one. @[to_additive le_add_of_nonneg_right] lemma le_mul_of_one_le_right' (h : 1 ≤ b) : a ≤ a * b := by simpa only [mul_one] using mul_le_mul_left' h a @[to_additive le_add_of_nonneg_left] lemma le_mul_of_one_le_left' (h : 1 ≤ b) : a ≤ b * a := by simpa only [one_mul] using mul_le_mul_right' h a @[to_additive add_le_of_nonpos_right] lemma mul_le_of_le_one_right' (h : b ≤ 1) : a * b ≤ a := by simpa only [mul_one] using mul_le_mul_left' h a @[to_additive add_le_of_nonpos_left] lemma mul_le_of_le_one_left' (h : b ≤ 1) : b * a ≤ a := by simpa only [one_mul] using mul_le_mul_right' h a @[to_additive] lemma lt_of_mul_lt_of_one_le_left (h : a * b < c) (hle : 1 ≤ b) : a < c := (le_mul_of_one_le_right' hle).trans_lt h @[to_additive] lemma lt_of_mul_lt_of_one_le_right (h : a * b < c) (hle : 1 ≤ a) : b < c := (le_mul_of_one_le_left' hle).trans_lt h @[to_additive] lemma le_of_mul_le_of_one_le_left (h : a * b ≤ c) (hle : 1 ≤ b) : a ≤ c := (le_mul_of_one_le_right' hle).trans h @[to_additive] lemma le_of_mul_le_of_one_le_right (h : a * b ≤ c) (hle : 1 ≤ a) : b ≤ c := (le_mul_of_one_le_left' hle).trans h @[to_additive] lemma lt_of_lt_mul_of_le_one_left (h : a < b * c) (hle : c ≤ 1) : a < b := h.trans_le (mul_le_of_le_one_right' hle) @[to_additive] lemma lt_of_lt_mul_of_le_one_right (h : a < b * c) (hle : b ≤ 1) : a < c := h.trans_le (mul_le_of_le_one_left' hle) @[to_additive] lemma le_of_le_mul_of_le_one_left (h : a ≤ b * c) (hle : c ≤ 1) : a ≤ b := h.trans (mul_le_of_le_one_right' hle) @[to_additive] lemma le_of_le_mul_of_le_one_right (h : a ≤ b * c) (hle : b ≤ 1) : a ≤ c := h.trans (mul_le_of_le_one_left' hle) @[to_additive] lemma le_mul_of_one_le_of_le (ha : 1 ≤ a) (hbc : b ≤ c) : b ≤ a * c := one_mul b ▸ mul_le_mul' ha hbc @[to_additive] lemma le_mul_of_le_of_one_le (hbc : b ≤ c) (ha : 1 ≤ a) : b ≤ c * a := mul_one b ▸ mul_le_mul' hbc ha @[to_additive add_nonneg] lemma one_le_mul (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b := le_mul_of_one_le_of_le ha hb @[to_additive add_pos_of_pos_of_nonneg] lemma one_lt_mul_of_lt_of_le' (ha : 1 < a) (hb : 1 ≤ b) : 1 < a * b := lt_of_lt_of_le ha $ le_mul_of_one_le_right' hb @[to_additive add_pos_of_nonneg_of_pos] lemma one_lt_mul_of_le_of_lt' (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := lt_of_lt_of_le hb $ le_mul_of_one_le_left' ha @[to_additive add_pos] lemma one_lt_mul' (ha : 1 < a) (hb : 1 < b) : 1 < a * b := one_lt_mul_of_lt_of_le' ha hb.le @[to_additive add_nonpos] lemma mul_le_one' (ha : a ≤ 1) (hb : b ≤ 1) : a * b ≤ 1 := one_mul (1:α) ▸ (mul_le_mul' ha hb) @[to_additive] lemma mul_le_of_le_one_of_le' (ha : a ≤ 1) (hbc : b ≤ c) : a * b ≤ c := one_mul c ▸ mul_le_mul' ha hbc @[to_additive] lemma mul_le_of_le_of_le_one' (hbc : b ≤ c) (ha : a ≤ 1) : b * a ≤ c := mul_one c ▸ mul_le_mul' hbc ha @[to_additive] lemma mul_lt_one_of_lt_one_of_le_one' (ha : a < 1) (hb : b ≤ 1) : a * b < 1 := (mul_le_of_le_of_le_one' le_rfl hb).trans_lt ha @[to_additive] lemma mul_lt_one_of_le_one_of_lt_one' (ha : a ≤ 1) (hb : b < 1) : a * b < 1 := (mul_le_of_le_one_of_le' ha le_rfl).trans_lt hb @[to_additive] lemma mul_lt_one' (ha : a < 1) (hb : b < 1) : a * b < 1 := mul_lt_one_of_le_one_of_lt_one' ha.le hb @[to_additive] lemma lt_mul_of_one_le_of_lt' (ha : 1 ≤ a) (hbc : b < c) : b < a * c := hbc.trans_le $ le_mul_of_one_le_left' ha @[to_additive] lemma lt_mul_of_lt_of_one_le' (hbc : b < c) (ha : 1 ≤ a) : b < c * a := hbc.trans_le $ le_mul_of_one_le_right' ha @[to_additive] lemma lt_mul_of_one_lt_of_lt' (ha : 1 < a) (hbc : b < c) : b < a * c := lt_mul_of_one_le_of_lt' ha.le hbc @[to_additive] lemma lt_mul_of_lt_of_one_lt' (hbc : b < c) (ha : 1 < a) : b < c * a := lt_mul_of_lt_of_one_le' hbc ha.le @[to_additive] lemma mul_lt_of_le_one_of_lt' (ha : a ≤ 1) (hbc : b < c) : a * b < c := lt_of_le_of_lt (mul_le_of_le_one_of_le' ha le_rfl) hbc @[to_additive] lemma mul_lt_of_lt_of_le_one' (hbc : b < c) (ha : a ≤ 1) : b * a < c := lt_of_le_of_lt (mul_le_of_le_of_le_one' le_rfl ha) hbc @[to_additive] lemma mul_lt_of_lt_one_of_lt' (ha : a < 1) (hbc : b < c) : a * b < c := mul_lt_of_le_one_of_lt' ha.le hbc @[to_additive] lemma mul_lt_of_lt_of_lt_one' (hbc : b < c) (ha : a < 1) : b * a < c := mul_lt_of_lt_of_le_one' hbc ha.le @[to_additive] lemma mul_eq_one_iff' (ha : 1 ≤ a) (hb : 1 ≤ b) : a * b = 1 ↔ a = 1 ∧ b = 1 := iff.intro (assume hab : a * b = 1, have a ≤ 1, from hab ▸ le_mul_of_le_of_one_le le_rfl hb, have a = 1, from le_antisymm this ha, have b ≤ 1, from hab ▸ le_mul_of_one_le_of_le ha le_rfl, have b = 1, from le_antisymm this hb, and.intro ‹a = 1› ‹b = 1›) (assume ⟨ha', hb'⟩, by rw [ha', hb', mul_one]) /-- Pullback an `ordered_comm_monoid` under an injective map. -/ @[to_additive function.injective.ordered_add_comm_monoid "Pullback an `ordered_add_comm_monoid` under an injective map."] def function.injective.ordered_comm_monoid {β : Type*} [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : ordered_comm_monoid β := { mul_le_mul_left := λ a b ab c, show f (c * a) ≤ f (c * b), by simp [mul, mul_le_mul_left' ab], lt_of_mul_lt_mul_left := λ a b c bc, @lt_of_mul_lt_mul_left' _ _ (f a) _ _ (by rwa [← mul, ← mul]), ..partial_order.lift f hf, ..hf.comm_monoid f one mul } section mono variables {β : Type*} [preorder β] {f g : β → α} @[to_additive monotone.add] lemma monotone.mul' (hf : monotone f) (hg : monotone g) : monotone (λ x, f x * g x) := λ x y h, mul_le_mul' (hf h) (hg h) @[to_additive monotone.add_const] lemma monotone.mul_const' (hf : monotone f) (a : α) : monotone (λ x, f x * a) := hf.mul' monotone_const @[to_additive monotone.const_add] lemma monotone.const_mul' (hf : monotone f) (a : α) : monotone (λ x, a * f x) := monotone_const.mul' hf end mono end ordered_comm_monoid /-- Pullback a `linear_ordered_comm_monoid` under an injective map. -/ @[to_additive function.injective.linear_ordered_add_comm_monoid "Pullback an `ordered_add_comm_monoid` under an injective map."] def function.injective.linear_ordered_comm_monoid [linear_ordered_comm_monoid α] {β : Type*} [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : linear_ordered_comm_monoid β := { .. hf.ordered_comm_monoid f one mul, .. linear_order.lift f hf } lemma bit0_pos [ordered_add_comm_monoid α] {a : α} (h : 0 < a) : 0 < bit0 a := add_pos h h namespace units @[to_additive] instance [monoid α] [preorder α] : preorder (units α) := preorder.lift (coe : units α → α) @[simp, norm_cast, to_additive] theorem coe_le_coe [monoid α] [preorder α] {a b : units α} : (a : α) ≤ b ↔ a ≤ b := iff.rfl -- should `to_additive` do this? attribute [norm_cast] add_units.coe_le_coe @[simp, norm_cast, to_additive] theorem coe_lt_coe [monoid α] [preorder α] {a b : units α} : (a : α) < b ↔ a < b := iff.rfl attribute [norm_cast] add_units.coe_lt_coe @[to_additive] instance [monoid α] [partial_order α] : partial_order (units α) := partial_order.lift coe units.ext @[to_additive] instance [monoid α] [linear_order α] : linear_order (units α) := linear_order.lift coe units.ext @[simp, norm_cast, to_additive] theorem max_coe [monoid α] [linear_order α] {a b : units α} : (↑(max a b) : α) = max a b := by by_cases b ≤ a; simp [max, h] attribute [norm_cast] add_units.max_coe @[simp, norm_cast, to_additive] theorem min_coe [monoid α] [linear_order α] {a b : units α} : (↑(min a b) : α) = min a b := by by_cases a ≤ b; simp [min, h] attribute [norm_cast] add_units.min_coe end units namespace with_zero local attribute [semireducible] with_zero instance [preorder α] : preorder (with_zero α) := with_bot.preorder instance [partial_order α] : partial_order (with_zero α) := with_bot.partial_order instance [partial_order α] : order_bot (with_zero α) := with_bot.order_bot lemma zero_le [partial_order α] (a : with_zero α) : 0 ≤ a := order_bot.bot_le a lemma zero_lt_coe [partial_order α] (a : α) : (0 : with_zero α) < a := with_bot.bot_lt_coe a @[simp, norm_cast] lemma coe_lt_coe [partial_order α] {a b : α} : (a : with_zero α) < b ↔ a < b := with_bot.coe_lt_coe @[simp, norm_cast] lemma coe_le_coe [partial_order α] {a b : α} : (a : with_zero α) ≤ b ↔ a ≤ b := with_bot.coe_le_coe instance [lattice α] : lattice (with_zero α) := with_bot.lattice instance [linear_order α] : linear_order (with_zero α) := with_bot.linear_order lemma mul_le_mul_left {α : Type u} [ordered_comm_monoid α] : ∀ (a b : with_zero α), a ≤ b → ∀ (c : with_zero α), c * a ≤ c * b := begin rintro (_ | a) (_ | b) h (_ | c), { apply with_zero.zero_le }, { apply with_zero.zero_le }, { apply with_zero.zero_le }, { apply with_zero.zero_le }, { apply with_zero.zero_le }, { exact false.elim (not_lt_of_le h (with_zero.zero_lt_coe a))}, { apply with_zero.zero_le }, { simp_rw [some_eq_coe] at h ⊢, norm_cast at h ⊢, exact mul_le_mul_left' h c } end lemma lt_of_mul_lt_mul_left {α : Type u} [ordered_comm_monoid α] : ∀ (a b c : with_zero α), a * b < a * c → b < c := begin rintro (_ | a) (_ | b) (_ | c) h, { exact false.elim (lt_irrefl none h) }, { exact false.elim (lt_irrefl none h) }, { exact false.elim (lt_irrefl none h) }, { exact false.elim (lt_irrefl none h) }, { exact false.elim (lt_irrefl none h) }, { exact with_zero.zero_lt_coe c }, { exact false.elim (not_le_of_lt h (with_zero.zero_le _)) }, { simp_rw [some_eq_coe] at h ⊢, norm_cast at h ⊢, apply lt_of_mul_lt_mul_left' h } end instance [ordered_comm_monoid α] : ordered_comm_monoid (with_zero α) := { mul_le_mul_left := with_zero.mul_le_mul_left, lt_of_mul_lt_mul_left := with_zero.lt_of_mul_lt_mul_left, ..with_zero.comm_monoid_with_zero, ..with_zero.partial_order } /- Note 1 : the below is not an instance because it requires `zero_le`. It seems like a rather pathological definition because α already has a zero. Note 2 : there is no multiplicative analogue because it does not seem necessary. Mathematicians might be more likely to use the order-dual version, where all elements are ≤ 1 and then 1 is the top element. -/ /-- If `0` is the least element in `α`, then `with_zero α` is an `ordered_add_comm_monoid`. -/ def ordered_add_comm_monoid [ordered_add_comm_monoid α] (zero_le : ∀ a : α, 0 ≤ a) : ordered_add_comm_monoid (with_zero α) := begin suffices, refine { add_le_add_left := this, ..with_zero.partial_order, ..with_zero.add_comm_monoid, .. }, { intros a b c h, have h' := lt_iff_le_not_le.1 h, rw lt_iff_le_not_le at ⊢, refine ⟨λ b h₂, _, λ h₂, h'.2 $ this _ _ h₂ _⟩, cases h₂, cases c with c, { cases h'.2 (this _ _ bot_le a) }, { refine ⟨_, rfl, _⟩, cases a with a, { exact with_bot.some_le_some.1 h'.1 }, { exact le_of_lt (lt_of_add_lt_add_left $ with_bot.some_lt_some.1 h), } } }, { intros a b h c ca h₂, cases b with b, { rw le_antisymm h bot_le at h₂, exact ⟨_, h₂, le_refl _⟩ }, cases a with a, { change c + 0 = some ca at h₂, simp at h₂, simp [h₂], exact ⟨_, rfl, by simpa using add_le_add_left (zero_le b) _⟩ }, { simp at h, cases c with c; change some _ = _ at h₂; simp [-add_comm] at h₂; subst ca; refine ⟨_, rfl, _⟩, { exact h }, { exact add_le_add_left h _ } } } end end with_zero namespace with_top section has_one variables [has_one α] @[to_additive] instance : has_one (with_top α) := ⟨(1 : α)⟩ @[simp, to_additive] lemma coe_one : ((1 : α) : with_top α) = 1 := rfl @[simp, to_additive] lemma coe_eq_one {a : α} : (a : with_top α) = 1 ↔ a = 1 := coe_eq_coe @[simp, to_additive] theorem one_eq_coe {a : α} : 1 = (a : with_top α) ↔ a = 1 := by rw [eq_comm, coe_eq_one] attribute [norm_cast] coe_one coe_eq_one coe_zero coe_eq_zero one_eq_coe zero_eq_coe @[simp, to_additive] theorem top_ne_one : ⊤ ≠ (1 : with_top α) . @[simp, to_additive] theorem one_ne_top : (1 : with_top α) ≠ ⊤ . end has_one instance [has_add α] : has_add (with_top α) := ⟨λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a + b))⟩ local attribute [reducible] with_zero instance [add_semigroup α] : add_semigroup (with_top α) := { add := (+), ..@additive.add_semigroup _ $ @with_zero.semigroup (multiplicative α) _ } @[norm_cast] lemma coe_add [has_add α] {a b : α} : ((a + b : α) : with_top α) = a + b := rfl @[norm_cast] lemma coe_bit0 [has_add α] {a : α} : ((bit0 a : α) : with_top α) = bit0 a := rfl @[norm_cast] lemma coe_bit1 [has_add α] [has_one α] {a : α} : ((bit1 a : α) : with_top α) = bit1 a := rfl @[simp] lemma add_top [has_add α] : ∀{a : with_top α}, a + ⊤ = ⊤ | none := rfl | (some a) := rfl @[simp] lemma top_add [has_add α] {a : with_top α} : ⊤ + a = ⊤ := rfl lemma add_eq_top [has_add α] {a b : with_top α} : a + b = ⊤ ↔ a = ⊤ ∨ b = ⊤ := by {cases a; cases b; simp [none_eq_top, some_eq_coe, ←with_top.coe_add, ←with_zero.coe_add]} lemma add_lt_top [has_add α] [partial_order α] {a b : with_top α} : a + b < ⊤ ↔ a < ⊤ ∧ b < ⊤ := by simp [lt_top_iff_ne_top, add_eq_top, not_or_distrib] lemma add_eq_coe [has_add α] : ∀ {a b : with_top α} {c : α}, a + b = c ↔ ∃ (a' b' : α), ↑a' = a ∧ ↑b' = b ∧ a' + b' = c | none b c := by simp [none_eq_top] | (some a) none c := by simp [none_eq_top] | (some a) (some b) c := by simp only [some_eq_coe, ← coe_add, coe_eq_coe, exists_and_distrib_left, exists_eq_left] instance [add_comm_semigroup α] : add_comm_semigroup (with_top α) := { ..@additive.add_comm_semigroup _ $ @with_zero.comm_semigroup (multiplicative α) _ } instance [add_monoid α] : add_monoid (with_top α) := { zero := some 0, add := (+), ..@additive.add_monoid _ $ @monoid_with_zero.to_monoid _ $ @with_zero.monoid_with_zero (multiplicative α) _ } instance [add_comm_monoid α] : add_comm_monoid (with_top α) := { zero := 0, add := (+), ..@additive.add_comm_monoid _ $ @comm_monoid_with_zero.to_comm_monoid _ $ @with_zero.comm_monoid_with_zero (multiplicative α) _ } instance [ordered_add_comm_monoid α] : ordered_add_comm_monoid (with_top α) := { add_le_add_left := begin rintros a b h (_|c), { simp [none_eq_top] }, rcases b with (_|b), { simp [none_eq_top] }, rcases le_coe_iff.1 h with ⟨a, rfl, h⟩, simp only [some_eq_coe, ← coe_add, coe_le_coe] at h ⊢, exact add_le_add_left h c end, lt_of_add_lt_add_left := begin intros a b c h, rcases lt_iff_exists_coe.1 h with ⟨ab, hab, hlt⟩, rcases add_eq_coe.1 hab with ⟨a, b, rfl, rfl, rfl⟩, rw coe_lt_iff, rintro c rfl, exact lt_of_add_lt_add_left (coe_lt_coe.1 hlt) end, ..with_top.partial_order, ..with_top.add_comm_monoid } instance [linear_ordered_add_comm_monoid α] : linear_ordered_add_comm_monoid_with_top (with_top α) := { top_add' := λ x, with_top.top_add, ..with_top.order_top, ..with_top.linear_order, ..with_top.ordered_add_comm_monoid, ..option.nontrivial } /-- Coercion from `α` to `with_top α` as an `add_monoid_hom`. -/ def coe_add_hom [add_monoid α] : α →+ with_top α := ⟨coe, rfl, λ _ _, rfl⟩ @[simp] lemma coe_coe_add_hom [add_monoid α] : ⇑(coe_add_hom : α →+ with_top α) = coe := rfl @[simp] lemma zero_lt_top [ordered_add_comm_monoid α] : (0 : with_top α) < ⊤ := coe_lt_top 0 @[simp, norm_cast] lemma zero_lt_coe [ordered_add_comm_monoid α] (a : α) : (0 : with_top α) < a ↔ 0 < a := coe_lt_coe end with_top namespace with_bot instance [has_zero α] : has_zero (with_bot α) := with_top.has_zero instance [has_one α] : has_one (with_bot α) := with_top.has_one instance [add_semigroup α] : add_semigroup (with_bot α) := with_top.add_semigroup instance [add_comm_semigroup α] : add_comm_semigroup (with_bot α) := with_top.add_comm_semigroup instance [add_monoid α] : add_monoid (with_bot α) := with_top.add_monoid instance [add_comm_monoid α] : add_comm_monoid (with_bot α) := with_top.add_comm_monoid instance [ordered_add_comm_monoid α] : ordered_add_comm_monoid (with_bot α) := begin suffices, refine { add_le_add_left := this, ..with_bot.partial_order, ..with_bot.add_comm_monoid, ..}, { intros a b c h, have h' := h, rw lt_iff_le_not_le at h' ⊢, refine ⟨λ b h₂, _, λ h₂, h'.2 $ this _ _ h₂ _⟩, cases h₂, cases a with a, { exact (not_le_of_lt h).elim bot_le }, cases c with c, { exact (not_le_of_lt h).elim bot_le }, { exact ⟨_, rfl, le_of_lt (lt_of_add_lt_add_left $ with_bot.some_lt_some.1 h)⟩ } }, { intros a b h c ca h₂, cases c with c, {cases h₂}, cases a with a; cases h₂, cases b with b, {cases le_antisymm h bot_le}, simp at h, exact ⟨_, rfl, add_le_add_left h _⟩, } end -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_zero [has_zero α] : ((0 : α) : with_bot α) = 0 := rfl -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_one [has_one α] : ((1 : α) : with_bot α) = 1 := rfl -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_eq_zero {α : Type*} [add_monoid α] {a : α} : (a : with_bot α) = 0 ↔ a = 0 := by norm_cast -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_add [add_semigroup α] (a b : α) : ((a + b : α) : with_bot α) = a + b := by norm_cast -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_bit0 [add_semigroup α] {a : α} : ((bit0 a : α) : with_bot α) = bit0 a := by norm_cast -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_bit1 [add_semigroup α] [has_one α] {a : α} : ((bit1 a : α) : with_bot α) = bit1 a := by norm_cast @[simp] lemma bot_add [ordered_add_comm_monoid α] (a : with_bot α) : ⊥ + a = ⊥ := rfl @[simp] lemma add_bot [ordered_add_comm_monoid α] (a : with_bot α) : a + ⊥ = ⊥ := by cases a; refl end with_bot /-- A canonically ordered additive monoid is an ordered commutative additive monoid in which the ordering coincides with the subtractibility relation, which is to say, `a ≤ b` iff there exists `c` with `b = a + c`. This is satisfied by the natural numbers, for example, but not the integers or other nontrivial `ordered_add_comm_group`s. -/ @[protect_proj, ancestor ordered_add_comm_monoid order_bot] class canonically_ordered_add_monoid (α : Type*) extends ordered_add_comm_monoid α, order_bot α := (le_iff_exists_add : ∀ a b : α, a ≤ b ↔ ∃ c, b = a + c) /-- A canonically ordered monoid is an ordered commutative monoid in which the ordering coincides with the divisibility relation, which is to say, `a ≤ b` iff there exists `c` with `b = a * c`. Example seem rare; it seems more likely that the `order_dual` of a naturally-occurring lattice satisfies this than the lattice itself (for example, dual of the lattice of ideals of a PID or Dedekind domain satisfy this; collections of all things ≤ 1 seem to be more natural that collections of all things ≥ 1). -/ @[protect_proj, ancestor ordered_comm_monoid order_bot, to_additive] class canonically_ordered_monoid (α : Type*) extends ordered_comm_monoid α, order_bot α := (le_iff_exists_mul : ∀ a b : α, a ≤ b ↔ ∃ c, b = a * c) section canonically_ordered_monoid variables [canonically_ordered_monoid α] {a b c d : α} @[to_additive] lemma le_iff_exists_mul : a ≤ b ↔ ∃c, b = a * c := canonically_ordered_monoid.le_iff_exists_mul a b @[to_additive] lemma self_le_mul_right (a b : α) : a ≤ a * b := le_iff_exists_mul.mpr ⟨b, rfl⟩ @[to_additive] lemma self_le_mul_left (a b : α) : a ≤ b * a := by { rw [mul_comm], exact self_le_mul_right a b } @[simp, to_additive zero_le] lemma one_le (a : α) : 1 ≤ a := le_iff_exists_mul.mpr ⟨a, by simp⟩ @[simp, to_additive] lemma bot_eq_one : (⊥ : α) = 1 := le_antisymm bot_le (one_le ⊥) @[simp, to_additive] lemma mul_eq_one_iff : a * b = 1 ↔ a = 1 ∧ b = 1 := mul_eq_one_iff' (one_le _) (one_le _) @[simp, to_additive] lemma le_one_iff_eq_one : a ≤ 1 ↔ a = 1 := iff.intro (assume h, le_antisymm h (one_le a)) (assume h, h ▸ le_refl a) @[to_additive] lemma one_lt_iff_ne_one : 1 < a ↔ a ≠ 1 := iff.intro ne_of_gt $ assume hne, lt_of_le_of_ne (one_le _) hne.symm @[to_additive] lemma exists_pos_mul_of_lt (h : a < b) : ∃ c > 1, a * c = b := begin obtain ⟨c, hc⟩ := le_iff_exists_mul.1 h.le, refine ⟨c, one_lt_iff_ne_one.2 _, hc.symm⟩, rintro rfl, simpa [hc, lt_irrefl] using h end @[to_additive] lemma le_mul_left (h : a ≤ c) : a ≤ b * c := calc a = 1 * a : by simp ... ≤ b * c : mul_le_mul' (one_le _) h @[to_additive] lemma le_mul_right (h : a ≤ b) : a ≤ b * c := calc a = a * 1 : by simp ... ≤ b * c : mul_le_mul' h (one_le _) local attribute [semireducible] with_zero -- This instance looks absurd: a monoid already has a zero /-- Adding a new zero to a canonically ordered additive monoid produces another one. -/ instance with_zero.canonically_ordered_add_monoid {α : Type u} [canonically_ordered_add_monoid α] : canonically_ordered_add_monoid (with_zero α) := { le_iff_exists_add := λ a b, begin cases a with a, { exact iff_of_true bot_le ⟨b, (zero_add b).symm⟩ }, cases b with b, { exact iff_of_false (mt (le_antisymm bot_le) (by simp)) (λ ⟨c, h⟩, by cases c; cases h) }, { simp [le_iff_exists_add, -add_comm], split; intro h; rcases h with ⟨c, h⟩, { exact ⟨some c, congr_arg some h⟩ }, { cases c; cases h, { exact ⟨_, (add_zero _).symm⟩ }, { exact ⟨_, rfl⟩ } } } end, bot := 0, bot_le := assume a a' h, option.no_confusion h, .. with_zero.ordered_add_comm_monoid zero_le } instance with_top.canonically_ordered_add_monoid {α : Type u} [canonically_ordered_add_monoid α] : canonically_ordered_add_monoid (with_top α) := { le_iff_exists_add := assume a b, match a, b with | a, none := show a ≤ ⊤ ↔ ∃c, ⊤ = a + c, by simp; refine ⟨⊤, _⟩; cases a; refl | (some a), (some b) := show (a:with_top α) ≤ ↑b ↔ ∃c:with_top α, ↑b = ↑a + c, begin simp [canonically_ordered_add_monoid.le_iff_exists_add, -add_comm], split, { rintro ⟨c, rfl⟩, refine ⟨c, _⟩, norm_cast }, { exact assume h, match b, h with _, ⟨some c, rfl⟩ := ⟨_, rfl⟩ end } end | none, some b := show (⊤ : with_top α) ≤ b ↔ ∃c:with_top α, ↑b = ⊤ + c, by simp end, .. with_top.order_bot, .. with_top.ordered_add_comm_monoid } end canonically_ordered_monoid /-- A canonically linear-ordered additive monoid is a canonically ordered additive monoid whose ordering is a linear order. -/ @[protect_proj, ancestor canonically_ordered_add_monoid linear_order] class canonically_linear_ordered_add_monoid (α : Type*) extends canonically_ordered_add_monoid α, linear_order α /-- A canonically linear-ordered monoid is a canonically ordered monoid whose ordering is a linear order. -/ @[protect_proj, ancestor canonically_ordered_monoid linear_order, to_additive] class canonically_linear_ordered_monoid (α : Type*) extends canonically_ordered_monoid α, linear_order α section canonically_linear_ordered_monoid variables @[priority 100, to_additive] -- see Note [lower instance priority] instance canonically_linear_ordered_monoid.semilattice_sup_bot [canonically_linear_ordered_monoid α] : semilattice_sup_bot α := { ..lattice_of_linear_order, ..canonically_ordered_monoid.to_order_bot α } instance with_top.canonically_linear_ordered_add_monoid (α : Type*) [canonically_linear_ordered_add_monoid α] : canonically_linear_ordered_add_monoid (with_top α) := { .. (infer_instance : canonically_ordered_add_monoid (with_top α)), .. (infer_instance : linear_order (with_top α)) } @[to_additive] lemma min_mul_distrib [canonically_linear_ordered_monoid α] (a b c : α) : min a (b * c) = min a (min a b * min a c) := begin cases le_total a b with hb hb, { simp [hb, le_mul_right] }, { cases le_total a c with hc hc, { simp [hc, le_mul_left] }, { simp [hb, hc] } } end @[to_additive] lemma min_mul_distrib' [canonically_linear_ordered_monoid α] (a b c : α) : min (a * b) c = min (min a c * min b c) c := by simpa [min_comm _ c] using min_mul_distrib c a b end canonically_linear_ordered_monoid /-- An ordered cancellative additive commutative monoid is an additive commutative monoid with a partial order, in which addition is cancellative and monotone. -/ @[protect_proj, ancestor add_cancel_comm_monoid partial_order] class ordered_cancel_add_comm_monoid (α : Type u) extends add_cancel_comm_monoid α, partial_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) (le_of_add_le_add_left : ∀ a b c : α, a + b ≤ a + c → b ≤ c) /-- An ordered cancellative commutative monoid is a commutative monoid with a partial order, in which multiplication is cancellative and monotone. -/ @[protect_proj, ancestor cancel_comm_monoid partial_order, to_additive] class ordered_cancel_comm_monoid (α : Type u) extends cancel_comm_monoid α, partial_order α := (mul_le_mul_left : ∀ a b : α, a ≤ b → ∀ c : α, c * a ≤ c * b) (le_of_mul_le_mul_left : ∀ a b c : α, a * b ≤ a * c → b ≤ c) section ordered_cancel_comm_monoid variables [ordered_cancel_comm_monoid α] {a b c d : α} @[to_additive le_of_add_le_add_left] lemma le_of_mul_le_mul_left' : ∀ {a b c : α}, a * b ≤ a * c → b ≤ c := ordered_cancel_comm_monoid.le_of_mul_le_mul_left @[priority 100, to_additive] -- see Note [lower instance priority] instance ordered_cancel_comm_monoid.to_ordered_comm_monoid : ordered_comm_monoid α := { lt_of_mul_lt_mul_left := λ a b c h, lt_of_le_not_le (le_of_mul_le_mul_left' h.le) $ mt (λ h, ordered_cancel_comm_monoid.mul_le_mul_left _ _ h _) (not_le_of_gt h), ..‹ordered_cancel_comm_monoid α› } @[to_additive add_lt_add_left] lemma mul_lt_mul_left' (h : a < b) (c : α) : c * a < c * b := lt_of_le_not_le (mul_le_mul_left' h.le _) $ mt le_of_mul_le_mul_left' (not_le_of_gt h) @[to_additive add_lt_add_right] lemma mul_lt_mul_right' (h : a < b) (c : α) : a * c < b * c := begin rw [mul_comm a c, mul_comm b c], exact (mul_lt_mul_left' h c) end @[to_additive add_lt_add] lemma mul_lt_mul''' (h₁ : a < b) (h₂ : c < d) : a * c < b * d := lt_trans (mul_lt_mul_right' h₁ c) (mul_lt_mul_left' h₂ b) @[to_additive] lemma mul_lt_mul_of_le_of_lt (h₁ : a ≤ b) (h₂ : c < d) : a * c < b * d := lt_of_le_of_lt (mul_le_mul_right' h₁ _) (mul_lt_mul_left' h₂ b) @[to_additive] lemma mul_lt_mul_of_lt_of_le (h₁ : a < b) (h₂ : c ≤ d) : a * c < b * d := lt_of_lt_of_le (mul_lt_mul_right' h₁ c) (mul_le_mul_left' h₂ _) @[to_additive lt_add_of_pos_right] lemma lt_mul_of_one_lt_right' (a : α) {b : α} (h : 1 < b) : a < a * b := have a * 1 < a * b, from mul_lt_mul_left' h a, by rwa [mul_one] at this @[to_additive lt_add_of_pos_left] lemma lt_mul_of_one_lt_left' (a : α) {b : α} (h : 1 < b) : a < b * a := have 1 * a < b * a, from mul_lt_mul_right' h a, by rwa [one_mul] at this @[to_additive le_of_add_le_add_right] lemma le_of_mul_le_mul_right' (h : a * b ≤ c * b) : a ≤ c := le_of_mul_le_mul_left' (show b * a ≤ b * c, begin rw [mul_comm b a, mul_comm b c], assumption end) @[to_additive] lemma mul_lt_one (ha : a < 1) (hb : b < 1) : a * b < 1 := one_mul (1:α) ▸ (mul_lt_mul''' ha hb) @[to_additive] lemma mul_lt_one_of_lt_one_of_le_one (ha : a < 1) (hb : b ≤ 1) : a * b < 1 := one_mul (1:α) ▸ (mul_lt_mul_of_lt_of_le ha hb) @[to_additive] lemma mul_lt_one_of_le_one_of_lt_one (ha : a ≤ 1) (hb : b < 1) : a * b < 1 := one_mul (1:α) ▸ (mul_lt_mul_of_le_of_lt ha hb) @[to_additive] lemma lt_mul_of_one_lt_of_le (ha : 1 < a) (hbc : b ≤ c) : b < a * c := one_mul b ▸ mul_lt_mul_of_lt_of_le ha hbc @[to_additive] lemma lt_mul_of_le_of_one_lt (hbc : b ≤ c) (ha : 1 < a) : b < c * a := mul_one b ▸ mul_lt_mul_of_le_of_lt hbc ha @[to_additive] lemma mul_le_of_le_one_of_le (ha : a ≤ 1) (hbc : b ≤ c) : a * b ≤ c := one_mul c ▸ mul_le_mul' ha hbc @[to_additive] lemma mul_le_of_le_of_le_one (hbc : b ≤ c) (ha : a ≤ 1) : b * a ≤ c := mul_one c ▸ mul_le_mul' hbc ha @[to_additive] lemma mul_lt_of_lt_one_of_le (ha : a < 1) (hbc : b ≤ c) : a * b < c := one_mul c ▸ mul_lt_mul_of_lt_of_le ha hbc @[to_additive] lemma mul_lt_of_le_of_lt_one (hbc : b ≤ c) (ha : a < 1) : b * a < c := mul_one c ▸ mul_lt_mul_of_le_of_lt hbc ha @[to_additive] lemma lt_mul_of_one_le_of_lt (ha : 1 ≤ a) (hbc : b < c) : b < a * c := one_mul b ▸ mul_lt_mul_of_le_of_lt ha hbc @[to_additive] lemma lt_mul_of_lt_of_one_le (hbc : b < c) (ha : 1 ≤ a) : b < c * a := mul_one b ▸ mul_lt_mul_of_lt_of_le hbc ha @[to_additive] lemma lt_mul_of_one_lt_of_lt (ha : 1 < a) (hbc : b < c) : b < a * c := one_mul b ▸ mul_lt_mul''' ha hbc @[to_additive] lemma lt_mul_of_lt_of_one_lt (hbc : b < c) (ha : 1 < a) : b < c * a := mul_one b ▸ mul_lt_mul''' hbc ha @[to_additive] lemma mul_lt_of_le_one_of_lt (ha : a ≤ 1) (hbc : b < c) : a * b < c := one_mul c ▸ mul_lt_mul_of_le_of_lt ha hbc @[to_additive] lemma mul_lt_of_lt_of_le_one (hbc : b < c) (ha : a ≤ 1) : b * a < c := mul_one c ▸ mul_lt_mul_of_lt_of_le hbc ha @[to_additive] lemma mul_lt_of_lt_one_of_lt (ha : a < 1) (hbc : b < c) : a * b < c := one_mul c ▸ mul_lt_mul''' ha hbc @[to_additive] lemma mul_lt_of_lt_of_lt_one (hbc : b < c) (ha : a < 1) : b * a < c := mul_one c ▸ mul_lt_mul''' hbc ha @[simp, to_additive] lemma mul_le_mul_iff_left (a : α) {b c : α} : a * b ≤ a * c ↔ b ≤ c := ⟨le_of_mul_le_mul_left', λ h, mul_le_mul_left' h _⟩ @[simp, to_additive] lemma mul_le_mul_iff_right (c : α) : a * c ≤ b * c ↔ a ≤ b := mul_comm c a ▸ mul_comm c b ▸ mul_le_mul_iff_left c @[simp, to_additive] lemma mul_lt_mul_iff_left (a : α) {b c : α} : a * b < a * c ↔ b < c := ⟨lt_of_mul_lt_mul_left', λ h, mul_lt_mul_left' h _⟩ @[simp, to_additive] lemma mul_lt_mul_iff_right (c : α) : a * c < b * c ↔ a < b := mul_comm c a ▸ mul_comm c b ▸ mul_lt_mul_iff_left c @[simp, to_additive le_add_iff_nonneg_right] lemma le_mul_iff_one_le_right' (a : α) {b : α} : a ≤ a * b ↔ 1 ≤ b := have a * 1 ≤ a * b ↔ 1 ≤ b, from mul_le_mul_iff_left a, by rwa mul_one at this @[simp, to_additive le_add_iff_nonneg_left] lemma le_mul_iff_one_le_left' (a : α) {b : α} : a ≤ b * a ↔ 1 ≤ b := by rw [mul_comm, le_mul_iff_one_le_right'] @[simp, to_additive lt_add_iff_pos_right] lemma lt_mul_iff_one_lt_right' (a : α) {b : α} : a < a * b ↔ 1 < b := have a * 1 < a * b ↔ 1 < b, from mul_lt_mul_iff_left a, by rwa mul_one at this @[simp, to_additive lt_add_iff_pos_left] lemma lt_mul_iff_one_lt_left' (a : α) {b : α} : a < b * a ↔ 1 < b := by rw [mul_comm, lt_mul_iff_one_lt_right'] @[simp, to_additive add_le_iff_nonpos_left] lemma mul_le_iff_le_one_left' : a * b ≤ b ↔ a ≤ 1 := by { convert mul_le_mul_iff_right b, rw [one_mul] } @[simp, to_additive add_le_iff_nonpos_right] lemma mul_le_iff_le_one_right' : a * b ≤ a ↔ b ≤ 1 := by { convert mul_le_mul_iff_left a, rw [mul_one] } @[simp, to_additive add_lt_iff_neg_right] lemma mul_lt_iff_lt_one_right' : a * b < b ↔ a < 1 := by { convert mul_lt_mul_iff_right b, rw [one_mul] } @[simp, to_additive add_lt_iff_neg_left] lemma mul_lt_iff_lt_one_left' : a * b < a ↔ b < 1 := by { convert mul_lt_mul_iff_left a, rw [mul_one] } @[to_additive] lemma mul_eq_one_iff_eq_one_of_one_le (ha : 1 ≤ a) (hb : 1 ≤ b) : a * b = 1 ↔ a = 1 ∧ b = 1 := ⟨λ hab : a * b = 1, by split; apply le_antisymm; try {assumption}; rw ← hab; simp [ha, hb], λ ⟨ha', hb'⟩, by rw [ha', hb', mul_one]⟩ /-- Pullback an `ordered_cancel_comm_monoid` under an injective map. -/ @[to_additive function.injective.ordered_cancel_add_comm_monoid "Pullback an `ordered_cancel_add_comm_monoid` under an injective map."] def function.injective.ordered_cancel_comm_monoid {β : Type*} [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : ordered_cancel_comm_monoid β := { le_of_mul_le_mul_left := λ a b c (ab : f (a * b) ≤ f (a * c)), (by { rw [mul, mul] at ab, exact le_of_mul_le_mul_left' ab }), ..hf.left_cancel_semigroup f mul, ..hf.right_cancel_semigroup f mul, ..hf.ordered_comm_monoid f one mul } section mono variables {β : Type*} [preorder β] {f g : β → α} @[to_additive monotone.add_strict_mono] lemma monotone.mul_strict_mono' (hf : monotone f) (hg : strict_mono g) : strict_mono (λ x, f x * g x) := λ x y h, mul_lt_mul_of_le_of_lt (hf $ le_of_lt h) (hg h) @[to_additive strict_mono.add_monotone] lemma strict_mono.mul_monotone' (hf : strict_mono f) (hg : monotone g) : strict_mono (λ x, f x * g x) := λ x y h, mul_lt_mul_of_lt_of_le (hf h) (hg $ le_of_lt h) @[to_additive strict_mono.add_const] lemma strict_mono.mul_const' (hf : strict_mono f) (c : α) : strict_mono (λ x, f x * c) := hf.mul_monotone' monotone_const @[to_additive strict_mono.const_add] lemma strict_mono.const_mul' (hf : strict_mono f) (c : α) : strict_mono (λ x, c * f x) := monotone_const.mul_strict_mono' hf end mono end ordered_cancel_comm_monoid section ordered_cancel_add_comm_monoid variable [ordered_cancel_add_comm_monoid α] lemma with_top.add_lt_add_iff_left : ∀{a b c : with_top α}, a < ⊤ → (a + c < a + b ↔ c < b) | none := assume b c h, (lt_irrefl ⊤ h).elim | (some a) := begin assume b c h, cases b; cases c; simp [with_top.none_eq_top, with_top.some_eq_coe, with_top.coe_lt_top, with_top.coe_lt_coe], { norm_cast, exact with_top.coe_lt_top _ }, { norm_cast, exact add_lt_add_iff_left _ } end lemma with_bot.add_lt_add_iff_left : ∀{a b c : with_bot α}, ⊥ < a → (a + c < a + b ↔ c < b) | none := assume b c h, (lt_irrefl ⊥ h).elim | (some a) := begin assume b c h, cases b; cases c; simp [with_bot.none_eq_bot, with_bot.some_eq_coe, with_bot.bot_lt_coe, with_bot.coe_lt_coe], { norm_cast, exact with_bot.bot_lt_coe _ }, { norm_cast, exact add_lt_add_iff_left _ } end local attribute [reducible] with_zero lemma with_top.add_lt_add_iff_right {a b c : with_top α} : a < ⊤ → (c + a < b + a ↔ c < b) := by simpa [add_comm] using @with_top.add_lt_add_iff_left _ _ a b c lemma with_bot.add_lt_add_iff_right {a b c : with_bot α} : ⊥ < a → (c + a < b + a ↔ c < b) := by simpa [add_comm] using @with_bot.add_lt_add_iff_left _ _ a b c end ordered_cancel_add_comm_monoid /-! Some lemmas about types that have an ordering and a binary operation, with no rules relating them. -/ @[to_additive] lemma fn_min_mul_fn_max {β} [linear_order α] [comm_semigroup β] (f : α → β) (n m : α) : f (min n m) * f (max n m) = f n * f m := by { cases le_total n m with h h; simp [h, mul_comm] } @[to_additive] lemma min_mul_max [linear_order α] [comm_semigroup α] (n m : α) : min n m * max n m = n * m := fn_min_mul_fn_max id n m /-- A linearly ordered cancellative additive commutative monoid is an additive commutative monoid with a decidable linear order in which addition is cancellative and monotone. -/ @[protect_proj, ancestor ordered_cancel_add_comm_monoid linear_ordered_add_comm_monoid] class linear_ordered_cancel_add_comm_monoid (α : Type u) extends ordered_cancel_add_comm_monoid α, linear_ordered_add_comm_monoid α /-- A linearly ordered cancellative commutative monoid is a commutative monoid with a linear order in which multiplication is cancellative and monotone. -/ @[protect_proj, ancestor ordered_cancel_comm_monoid linear_ordered_comm_monoid, to_additive] class linear_ordered_cancel_comm_monoid (α : Type u) extends ordered_cancel_comm_monoid α, linear_ordered_comm_monoid α section linear_ordered_cancel_comm_monoid variables [linear_ordered_cancel_comm_monoid α] @[to_additive] lemma min_mul_mul_left (a b c : α) : min (a * b) (a * c) = a * min b c := (monotone_id.const_mul' a).map_min.symm @[to_additive] lemma min_mul_mul_right (a b c : α) : min (a * c) (b * c) = min a b * c := (monotone_id.mul_const' c).map_min.symm @[to_additive] lemma max_mul_mul_left (a b c : α) : max (a * b) (a * c) = a * max b c := (monotone_id.const_mul' a).map_max.symm @[to_additive] lemma max_mul_mul_right (a b c : α) : max (a * c) (b * c) = max a b * c := (monotone_id.mul_const' c).map_max.symm @[to_additive] lemma min_le_mul_of_one_le_right {a b : α} (hb : 1 ≤ b) : min a b ≤ a * b := min_le_iff.2 $ or.inl $ le_mul_of_one_le_right' hb @[to_additive] lemma min_le_mul_of_one_le_left {a b : α} (ha : 1 ≤ a) : min a b ≤ a * b := min_le_iff.2 $ or.inr $ le_mul_of_one_le_left' ha @[to_additive] lemma max_le_mul_of_one_le {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : max a b ≤ a * b := max_le_iff.2 ⟨le_mul_of_one_le_right' hb, le_mul_of_one_le_left' ha⟩ /-- Pullback a `linear_ordered_cancel_comm_monoid` under an injective map. -/ @[to_additive function.injective.linear_ordered_cancel_add_comm_monoid "Pullback a `linear_ordered_cancel_add_comm_monoid` under an injective map."] def function.injective.linear_ordered_cancel_comm_monoid {β : Type*} [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : linear_ordered_cancel_comm_monoid β := { ..hf.linear_ordered_comm_monoid f one mul, ..hf.ordered_cancel_comm_monoid f one mul } end linear_ordered_cancel_comm_monoid namespace order_dual @[to_additive] instance [ordered_comm_monoid α] : ordered_comm_monoid (order_dual α) := { mul_le_mul_left := λ a b h c, @mul_le_mul_left' α _ b a h _, lt_of_mul_lt_mul_left := λ a b c h, @lt_of_mul_lt_mul_left' α _ a c b h, ..order_dual.partial_order α, ..show comm_monoid α, by apply_instance } @[to_additive] instance [ordered_cancel_comm_monoid α] : ordered_cancel_comm_monoid (order_dual α) := { le_of_mul_le_mul_left := λ a b c : α, le_of_mul_le_mul_left', mul_left_cancel := @mul_left_cancel α _, mul_right_cancel := @mul_right_cancel α _, ..order_dual.ordered_comm_monoid } @[to_additive] instance [linear_ordered_cancel_comm_monoid α] : linear_ordered_cancel_comm_monoid (order_dual α) := { .. order_dual.linear_order α, .. order_dual.ordered_cancel_comm_monoid } end order_dual namespace prod variables {M N : Type*} @[to_additive] instance [ordered_cancel_comm_monoid M] [ordered_cancel_comm_monoid N] : ordered_cancel_comm_monoid (M × N) := { mul_le_mul_left := λ a b h c, ⟨mul_le_mul_left' h.1 _, mul_le_mul_left' h.2 _⟩, le_of_mul_le_mul_left := λ a b c h, ⟨le_of_mul_le_mul_left' h.1, le_of_mul_le_mul_left' h.2⟩, .. prod.comm_monoid, .. prod.left_cancel_semigroup, .. prod.right_cancel_semigroup, .. prod.partial_order M N } end prod section type_tags instance : Π [preorder α], preorder (multiplicative α) := id instance : Π [preorder α], preorder (additive α) := id instance : Π [partial_order α], partial_order (multiplicative α) := id instance : Π [partial_order α], partial_order (additive α) := id instance : Π [linear_order α], linear_order (multiplicative α) := id instance : Π [linear_order α], linear_order (additive α) := id instance [ordered_add_comm_monoid α] : ordered_comm_monoid (multiplicative α) := { mul_le_mul_left := @ordered_add_comm_monoid.add_le_add_left α _, lt_of_mul_lt_mul_left := @ordered_add_comm_monoid.lt_of_add_lt_add_left α _, ..multiplicative.partial_order, ..multiplicative.comm_monoid } instance [ordered_comm_monoid α] : ordered_add_comm_monoid (additive α) := { add_le_add_left := @ordered_comm_monoid.mul_le_mul_left α _, lt_of_add_lt_add_left := @ordered_comm_monoid.lt_of_mul_lt_mul_left α _, ..additive.partial_order, ..additive.add_comm_monoid } instance [ordered_cancel_add_comm_monoid α] : ordered_cancel_comm_monoid (multiplicative α) := { le_of_mul_le_mul_left := @ordered_cancel_add_comm_monoid.le_of_add_le_add_left α _, ..multiplicative.right_cancel_semigroup, ..multiplicative.left_cancel_semigroup, ..multiplicative.ordered_comm_monoid } instance [ordered_cancel_comm_monoid α] : ordered_cancel_add_comm_monoid (additive α) := { le_of_add_le_add_left := @ordered_cancel_comm_monoid.le_of_mul_le_mul_left α _, ..additive.add_right_cancel_semigroup, ..additive.add_left_cancel_semigroup, ..additive.ordered_add_comm_monoid } instance [linear_ordered_add_comm_monoid α] : linear_ordered_comm_monoid (multiplicative α) := { ..multiplicative.linear_order, ..multiplicative.ordered_comm_monoid } instance [linear_ordered_comm_monoid α] : linear_ordered_add_comm_monoid (additive α) := { ..additive.linear_order, ..additive.ordered_add_comm_monoid } end type_tags
3122b8638223bf42e70a801f217c095c276f4555
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/cpdt.lean
38f55ace1a482589562fef63941095af8ebad1d8
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
1,081
lean
/- "Proving in the Large" chapter of CPDT -/ inductive exp : Type | Const (n : nat) : exp | Plus (e1 e2 : exp) : exp | Mult (e1 e2 : exp) : exp open exp def eeval : exp → nat | (Const n) := n | (Plus e1 e2) := eeval e1 + eeval e2 | (Mult e1 e2) := eeval e1 * eeval e2 def times (k : nat) : exp → exp | (Const n) := Const (k * n) | (Plus e1 e2) := Plus (times e1) (times e2) | (Mult e1 e2) := Mult (times e1) e2 def reassoc : exp → exp | (Const n) := (Const n) | (Plus e1 e2) := let e1' := reassoc e1 in let e2' := reassoc e2 in match e2' with | (Plus e21 e22) := Plus (Plus e1' e21) e22 | _ := Plus e1' e2' end | (Mult e1 e2) := let e1' := reassoc e1 in let e2' := reassoc e2 in match e2' with | (Mult e21 e22) := Mult (Mult e1' e21) e22 | _ := Mult e1' e2' end attribute [simp] mul_add times reassoc eeval theorem eeval_times (k e) : eeval (times k e) = k * eeval e := by induction e; simp [*] theorem reassoc_correct (e) : eeval (reassoc e) = eeval e := by induction e; simp [*]; cases (reassoc e2); rsimp
9b9847fdd6786eb6ea66b1421cbde6469f2cbf67
f3a5af2927397cf346ec0e24312bfff077f00425
/src/game/world10/level11.lean
359ddc842be9db7a23637d1b1c21faddbc60330a
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/natural_number_game
05c39e1586408cfb563d1a12e1085a90726ab655
f29b6c2884299fc63fdfc81ae5d7daaa3219f9fd
refs/heads/master
1,688,570,964,990
1,636,908,242,000
1,636,908,242,000
195,403,790
277
84
Apache-2.0
1,694,547,955,000
1,562,328,792,000
Lean
UTF-8
Lean
false
false
571
lean
import game.world10.level10 -- hide namespace mynat -- hide /- # Inequality world. ## Level 11: `add_le_add_right` If you're faced with a goal of the form `forall t, ...`, then the next line is "so let $t$ be arbitrary". The way to do this in Lean is `intro t`. -/ /- Lemma For all naturals $a$ and $b$, $a\le b$ implies that for all naturals $t$, $a+t\le b+t$. -/ theorem add_le_add_right {a b : mynat} : a ≤ b → ∀ t, (a + t) ≤ (b + t) := begin [nat_num_game] intro h, cases h with c hc, intro t, use c, rw hc, ring, end end mynat -- hide
ebdf08f4b60b09038269304ea1cd3baeb6d0e12b
4727251e0cd73359b15b664c3170e5d754078599
/src/data/nat/dist.lean
c6eacfc7255103a3dc91bcaf2bd58a52c616f7fe
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
4,135
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Jeremy Avigad -/ import data.nat.basic /-! # Distance function on ℕ This file defines a simple distance function on naturals from truncated substraction. -/ namespace nat /-- Distance (absolute value of difference) between natural numbers. -/ def dist (n m : ℕ) := (n - m) + (m - n) theorem dist.def (n m : ℕ) : dist n m = (n - m) + (m - n) := rfl theorem dist_comm (n m : ℕ) : dist n m = dist m n := by simp [dist.def, add_comm] @[simp] theorem dist_self (n : ℕ) : dist n n = 0 := by simp [dist.def, tsub_self] theorem eq_of_dist_eq_zero {n m : ℕ} (h : dist n m = 0) : n = m := have n - m = 0, from nat.eq_zero_of_add_eq_zero_right h, have n ≤ m, from tsub_eq_zero_iff_le.mp this, have m - n = 0, from nat.eq_zero_of_add_eq_zero_left h, have m ≤ n, from tsub_eq_zero_iff_le.mp this, le_antisymm ‹n ≤ m› ‹m ≤ n› theorem dist_eq_zero {n m : ℕ} (h : n = m) : dist n m = 0 := begin rw [h, dist_self] end theorem dist_eq_sub_of_le {n m : ℕ} (h : n ≤ m) : dist n m = m - n := begin rw [dist.def, tsub_eq_zero_iff_le.mpr h, zero_add] end theorem dist_eq_sub_of_le_right {n m : ℕ} (h : m ≤ n) : dist n m = n - m := begin rw [dist_comm], apply dist_eq_sub_of_le h end theorem dist_tri_left (n m : ℕ) : m ≤ dist n m + n := le_trans le_tsub_add (add_le_add_right (nat.le_add_left _ _) _) theorem dist_tri_right (n m : ℕ) : m ≤ n + dist n m := by rw add_comm; apply dist_tri_left theorem dist_tri_left' (n m : ℕ) : n ≤ dist n m + m := by rw dist_comm; apply dist_tri_left theorem dist_tri_right' (n m : ℕ) : n ≤ m + dist n m := by rw dist_comm; apply dist_tri_right theorem dist_zero_right (n : ℕ) : dist n 0 = n := eq.trans (dist_eq_sub_of_le_right (zero_le n)) (tsub_zero n) theorem dist_zero_left (n : ℕ) : dist 0 n = n := eq.trans (dist_eq_sub_of_le (zero_le n)) (tsub_zero n) theorem dist_add_add_right (n k m : ℕ) : dist (n + k) (m + k) = dist n m := calc dist (n + k) (m + k) = ((n + k) - (m + k)) + ((m + k)-(n + k)) : rfl ... = (n - m) + ((m + k) - (n + k)) : by rw add_tsub_add_eq_tsub_right ... = (n - m) + (m - n) : by rw add_tsub_add_eq_tsub_right theorem dist_add_add_left (k n m : ℕ) : dist (k + n) (k + m) = dist n m := begin rw [add_comm k n, add_comm k m], apply dist_add_add_right end theorem dist_eq_intro {n m k l : ℕ} (h : n + m = k + l) : dist n k = dist l m := calc dist n k = dist (n + m) (k + m) : by rw dist_add_add_right ... = dist (k + l) (k + m) : by rw h ... = dist l m : by rw dist_add_add_left theorem dist.triangle_inequality (n m k : ℕ) : dist n k ≤ dist n m + dist m k := have dist n m + dist m k = (n - m) + (m - k) + ((k - m) + (m - n)), by simp [dist.def, add_comm, add_left_comm], by { rw [this, dist.def], exact add_le_add tsub_le_tsub_add_tsub tsub_le_tsub_add_tsub } theorem dist_mul_right (n k m : ℕ) : dist (n * k) (m * k) = dist n m * k := by rw [dist.def, dist.def, right_distrib, tsub_mul, tsub_mul] theorem dist_mul_left (k n m : ℕ) : dist (k * n) (k * m) = k * dist n m := by rw [mul_comm k n, mul_comm k m, dist_mul_right, mul_comm] -- TODO(Jeremy): do when we have max and minx --theorem dist_eq_max_sub_min {i j : nat} : dist i j = (max i j) - min i j := --sorry /- or.elim (lt_or_ge i j) (assume : i < j, by rw [max_eq_right_of_lt this, min_eq_left_of_lt this, dist_eq_sub_of_lt this]) (assume : i ≥ j, by rw [max_eq_left this , min_eq_right this, dist_eq_sub_of_le_right this]) -/ theorem dist_succ_succ {i j : nat} : dist (succ i) (succ j) = dist i j := by simp [dist.def, succ_sub_succ] theorem dist_pos_of_ne {i j : nat} : i ≠ j → 0 < dist i j := assume hne, nat.lt_by_cases (assume : i < j, begin rw [dist_eq_sub_of_le (le_of_lt this)], apply tsub_pos_of_lt this end) (assume : i = j, by contradiction) (assume : i > j, begin rw [dist_eq_sub_of_le_right (le_of_lt this)], apply tsub_pos_of_lt this end) end nat
fefdd194dd07f730bb26870e86575f98bbe4ef24
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/triv_sq_zero_ext.lean
eba81416ae1c79019bd8cc8e895b04336162f1a6
[ "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
16,678
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.algebra.basic import linear_algebra.prod /-! # Trivial Square-Zero Extension Given a module `M` over a ring `R`, the trivial square-zero extension of `M` over `R` is defined to be the `R`-algebra `R ⊕ M` with multiplication given by `(r₁ + m₁) * (r₂ + m₂) = r₁ r₂ + r₁ m₂ + r₂ m₁`. It is a square-zero extension because `M^2 = 0`. ## Main definitions * `triv_sq_zero_ext.inl`, `triv_sq_zero_ext.inr`: the canonical inclusions into `triv_sq_zero_ext R M`. * `triv_sq_zero_ext.fst`, `triv_sq_zero_ext.snd`: the canonical projections from `triv_sq_zero_ext R M`. * `triv_sq_zero_ext.algebra`: the associated `R`-algebra structure. * `triv_sq_zero_ext.lift`: the universal property of the trivial square-zero extension; algebra morphisms `triv_sq_zero_ext R M →ₐ[R] A` are uniquely defined by linear maps `M →ₗ[R] A` for which the product of any two elements in the range is zero. -/ universes u v w /-- "Trivial Square-Zero Extension". Given a module `M` over a ring `R`, the trivial square-zero extension of `M` over `R` is defined to be the `R`-algebra `R × M` with multiplication given by `(r₁ + m₁) * (r₂ + m₂) = r₁ r₂ + r₁ m₂ + r₂ m₁`. It is a square-zero extension because `M^2 = 0`. -/ def triv_sq_zero_ext (R : Type u) (M : Type v) := R × M local notation `tsze` := triv_sq_zero_ext namespace triv_sq_zero_ext section basic variables {R : Type u} {M : Type v} /-- The canonical inclusion `R → triv_sq_zero_ext R M`. -/ def inl [has_zero M] (r : R) : tsze R M := (r, 0) /-- The canonical inclusion `M → triv_sq_zero_ext R M`. -/ def inr [has_zero R] (m : M) : tsze R M := (0, m) /-- The canonical projection `triv_sq_zero_ext R M → R`. -/ def fst (x : tsze R M) : R := x.1 /-- The canonical projection `triv_sq_zero_ext R M → M`. -/ def snd (x : tsze R M) : M := x.2 @[ext] lemma ext {x y : tsze R M} (h1 : x.fst = y.fst) (h2 : x.snd = y.snd) : x = y := prod.ext h1 h2 section variables (M) @[simp] lemma fst_inl [has_zero M] (r : R) : (inl r : tsze R M).fst = r := rfl @[simp] lemma snd_inl [has_zero M] (r : R) : (inl r : tsze R M).snd = 0 := rfl end section variables (R) @[simp] lemma fst_inr [has_zero R] (m : M) : (inr m : tsze R M).fst = 0 := rfl @[simp] lemma snd_inr [has_zero R] (m : M) : (inr m : tsze R M).snd = m := rfl end lemma inl_injective [has_zero M] : function.injective (inl : R → tsze R M) := function.left_inverse.injective $ fst_inl _ lemma inr_injective [has_zero R] : function.injective (inr : M → tsze R M) := function.left_inverse.injective $ snd_inr _ end basic /-! ### Structures inherited from `prod` Additive operators and scalar multiplication operate elementwise. -/ section additive variables {T : Type*} {S : Type*} {R : Type u} {M : Type v} instance [inhabited R] [inhabited M] : inhabited (tsze R M) := prod.inhabited instance [has_zero R] [has_zero M] : has_zero (tsze R M) := prod.has_zero instance [has_add R] [has_add M] : has_add (tsze R M) := prod.has_add instance [has_neg R] [has_neg M] : has_neg (tsze R M) := prod.has_neg instance [add_semigroup R] [add_semigroup M] : add_semigroup (tsze R M) := prod.add_semigroup instance [add_zero_class R] [add_zero_class M] : add_zero_class (tsze R M) := prod.add_zero_class instance [add_monoid R] [add_monoid M] : add_monoid (tsze R M) := prod.add_monoid instance [add_group R] [add_group M] : add_group (tsze R M) := prod.add_group instance [add_comm_semigroup R] [add_comm_semigroup M] : add_comm_semigroup (tsze R M) := prod.add_comm_semigroup instance [add_comm_monoid R] [add_comm_monoid M] : add_comm_monoid (tsze R M) := prod.add_comm_monoid instance [add_comm_group R] [add_comm_group M] : add_comm_group (tsze R M) := prod.add_comm_group instance [has_scalar S R] [has_scalar S M] : has_scalar S (tsze R M) := prod.has_scalar instance [has_scalar T R] [has_scalar T M] [has_scalar S R] [has_scalar S M] [has_scalar T S] [is_scalar_tower T S R] [is_scalar_tower T S M] : is_scalar_tower T S (tsze R M) := prod.is_scalar_tower instance [has_scalar T R] [has_scalar T M] [has_scalar S R] [has_scalar S M] [smul_comm_class T S R] [smul_comm_class T S M] : smul_comm_class T S (tsze R M) := prod.smul_comm_class instance [has_scalar S R] [has_scalar S M] [has_scalar Sᵐᵒᵖ R] [has_scalar Sᵐᵒᵖ M] [is_central_scalar S R] [is_central_scalar S M] : is_central_scalar S (tsze R M) := prod.is_central_scalar instance [monoid S] [mul_action S R] [mul_action S M] : mul_action S (tsze R M) := prod.mul_action instance [monoid S] [add_monoid R] [add_monoid M] [distrib_mul_action S R] [distrib_mul_action S M] : distrib_mul_action S (tsze R M) := prod.distrib_mul_action instance [semiring S] [add_comm_monoid R] [add_comm_monoid M] [module S R] [module S M] : module S (tsze R M) := prod.module @[simp] lemma fst_zero [has_zero R] [has_zero M] : (0 : tsze R M).fst = 0 := rfl @[simp] lemma snd_zero [has_zero R] [has_zero M] : (0 : tsze R M).snd = 0 := rfl @[simp] lemma fst_add [has_add R] [has_add M] (x₁ x₂ : tsze R M) : (x₁ + x₂).fst = x₁.fst + x₂.fst := rfl @[simp] lemma snd_add [has_add R] [has_add M] (x₁ x₂ : tsze R M) : (x₁ + x₂).snd = x₁.snd + x₂.snd := rfl @[simp] lemma fst_neg [has_neg R] [has_neg M] (x : tsze R M) : (-x).fst = -x.fst := rfl @[simp] lemma snd_neg [has_neg R] [has_neg M] (x : tsze R M) : (-x).snd = -x.snd := rfl @[simp] lemma fst_smul [has_scalar S R] [has_scalar S M] (s : S) (x : tsze R M) : (s • x).fst = s • x.fst := rfl @[simp] lemma snd_smul [has_scalar S R] [has_scalar S M] (s : S) (x : tsze R M) : (s • x).snd = s • x.snd := rfl section variables (M) @[simp] lemma inl_zero [has_zero R] [has_zero M] : (inl 0 : tsze R M) = 0 := rfl @[simp] lemma inl_add [has_add R] [add_zero_class M] (r₁ r₂ : R) : (inl (r₁ + r₂) : tsze R M) = inl r₁ + inl r₂ := ext rfl (add_zero 0).symm @[simp] lemma inl_neg [has_neg R] [add_group M] (r : R) : (inl (-r) : tsze R M) = -inl r := ext rfl neg_zero.symm @[simp] lemma inl_smul [monoid S] [add_monoid M] [has_scalar S R] [distrib_mul_action S M] (s : S) (r : R) : (inl (s • r) : tsze R M) = s • inl r := ext rfl (smul_zero s).symm end section variables (R) @[simp] lemma inr_zero [has_zero R] [has_zero M] : (inr 0 : tsze R M) = 0 := rfl @[simp] lemma inr_add [add_zero_class R] [add_zero_class M] (m₁ m₂ : M) : (inr (m₁ + m₂) : tsze R M) = inr m₁ + inr m₂ := ext (add_zero 0).symm rfl @[simp] lemma inr_neg [add_group R] [has_neg M] (m : M) : (inr (-m) : tsze R M) = -inr m := ext neg_zero.symm rfl @[simp] lemma inr_smul [has_zero R] [has_zero S] [smul_with_zero S R] [has_scalar S M] (r : S) (m : M) : (inr (r • m) : tsze R M) = r • inr m := ext (smul_zero' _ _).symm rfl end lemma inl_fst_add_inr_snd_eq [add_zero_class R] [add_zero_class M] (x : tsze R M) : inl x.fst + inr x.snd = x := ext (add_zero x.1) (zero_add x.2) /-- To show a property hold on all `triv_sq_zero_ext R M` it suffices to show it holds on terms of the form `inl r + inr m`. This can be used as `induction x using triv_sq_zero_ext.ind`. -/ lemma ind {R M} [add_zero_class R] [add_zero_class M] {P : triv_sq_zero_ext R M → Prop} (h : ∀ r m, P (inl r + inr m)) (x) : P x := inl_fst_add_inr_snd_eq x ▸ h x.1 x.2 /-- This cannot be marked `@[ext]` as it ends up being used instead of `linear_map.prod_ext` when working with `R × M`. -/ lemma linear_map_ext {N} [semiring S] [add_comm_monoid R] [add_comm_monoid M] [add_comm_monoid N] [module S R] [module S M] [module S N] ⦃f g : tsze R M →ₗ[S] N⦄ (hl : ∀ r, f (inl r) = g (inl r)) (hr : ∀ m, f (inr m) = g (inr m)) : f = g := linear_map.prod_ext (linear_map.ext hl) (linear_map.ext hr) variables (R M) /-- The canonical `R`-linear inclusion `M → triv_sq_zero_ext R M`. -/ @[simps apply] def inr_hom [semiring R] [add_comm_monoid M] [module R M] : M →ₗ[R] tsze R M := { to_fun := inr, ..linear_map.inr _ _ _ } /-- The canonical `R`-linear projection `triv_sq_zero_ext R M → M`. -/ @[simps apply] def snd_hom [semiring R] [add_comm_monoid M] [module R M] : tsze R M →ₗ[R] M := { to_fun := snd, ..linear_map.snd _ _ _ } end additive /-! ### Multiplicative structure -/ section mul variables {R : Type u} {M : Type v} instance [has_one R] [has_zero M] : has_one (tsze R M) := ⟨(1, 0)⟩ instance [has_mul R] [has_add M] [has_scalar R M] : has_mul (tsze R M) := ⟨λ x y, (x.1 * y.1, x.1 • y.2 + y.1 • x.2)⟩ @[simp] lemma fst_one [has_one R] [has_zero M] : (1 : tsze R M).fst = 1 := rfl @[simp] lemma snd_one [has_one R] [has_zero M] : (1 : tsze R M).snd = 0 := rfl @[simp] lemma fst_mul [has_mul R] [has_add M] [has_scalar R M] (x₁ x₂ : tsze R M) : (x₁ * x₂).fst = x₁.fst * x₂.fst := rfl @[simp] lemma snd_mul [has_mul R] [has_add M] [has_scalar R M] (x₁ x₂ : tsze R M) : (x₁ * x₂).snd = x₁.fst • x₂.snd + x₂.fst • x₁.snd := rfl section variables (M) @[simp] lemma inl_one [has_one R] [has_zero M] : (inl 1 : tsze R M) = 1 := rfl @[simp] lemma inl_mul [monoid R] [add_monoid M] [distrib_mul_action R M] (r₁ r₂ : R) : (inl (r₁ * r₂) : tsze R M) = inl r₁ * inl r₂ := ext rfl $ show (0 : M) = r₁ • 0 + r₂ • 0, by rw [smul_zero, zero_add, smul_zero] lemma inl_mul_inl [monoid R] [add_monoid M] [distrib_mul_action R M] (r₁ r₂ : R) : (inl r₁ * inl r₂ : tsze R M) = inl (r₁ * r₂) := (inl_mul M r₁ r₂).symm end section variables (R) @[simp] lemma inr_mul_inr [semiring R] [add_comm_monoid M] [module R M] (m₁ m₂ : M) : (inr m₁ * inr m₂ : tsze R M) = 0 := ext (mul_zero _) $ show (0 : R) • m₂ + (0 : R) • m₁ = 0, by rw [zero_smul, zero_add, zero_smul] end lemma inl_mul_inr [semiring R] [add_comm_monoid M] [module R M] (r : R) (m : M) : (inl r * inr m : tsze R M) = inr (r • m) := ext (mul_zero r) $ show r • m + (0 : R) • 0 = r • m, by rw [smul_zero, add_zero] lemma inr_mul_inl [semiring R] [add_comm_monoid M] [module R M] (r : R) (m : M) : (inr m * inl r : tsze R M) = inr (r • m) := ext (zero_mul r) $ show (0 : R) • 0 + r • m = r • m, by rw [smul_zero, zero_add] instance [monoid R] [add_monoid M] [distrib_mul_action R M] : mul_one_class (tsze R M) := { one_mul := λ x, ext (one_mul x.1) $ show (1 : R) • x.2 + x.1 • 0 = x.2, by rw [one_smul, smul_zero, add_zero], mul_one := λ x, ext (mul_one x.1) $ show (x.1 • 0 : M) + (1 : R) • x.2 = x.2, by rw [smul_zero, zero_add, one_smul], .. triv_sq_zero_ext.has_one, .. triv_sq_zero_ext.has_mul } instance [semiring R] [add_comm_monoid M] [module R M] : non_assoc_semiring (tsze R M) := { zero_mul := λ x, ext (zero_mul x.1) $ show (0 : R) • x.2 + x.1 • 0 = 0, by rw [zero_smul, zero_add, smul_zero], mul_zero := λ x, ext (mul_zero x.1) $ show (x.1 • 0 : M) + (0 : R) • x.2 = 0, by rw [smul_zero, zero_add, zero_smul], left_distrib := λ x₁ x₂ x₃, ext (mul_add x₁.1 x₂.1 x₃.1) $ show x₁.1 • (x₂.2 + x₃.2) + (x₂.1 + x₃.1) • x₁.2 = x₁.1 • x₂.2 + x₂.1 • x₁.2 + (x₁.1 • x₃.2 + x₃.1 • x₁.2), by simp_rw [smul_add, add_smul, add_add_add_comm], right_distrib := λ x₁ x₂ x₃, ext (add_mul x₁.1 x₂.1 x₃.1) $ show (x₁.1 + x₂.1) • x₃.2 + x₃.1 • (x₁.2 + x₂.2) = x₁.1 • x₃.2 + x₃.1 • x₁.2 + (x₂.1 • x₃.2 + x₃.1 • x₂.2), by simp_rw [add_smul, smul_add, add_add_add_comm], .. triv_sq_zero_ext.mul_one_class, .. triv_sq_zero_ext.add_comm_monoid } instance [comm_monoid R] [add_monoid M] [distrib_mul_action R M] : monoid (tsze R M) := { mul_assoc := λ x y z, ext (mul_assoc x.1 y.1 z.1) $ show (x.1 * y.1) • z.2 + z.1 • (x.1 • y.2 + y.1 • x.2) = x.1 • (y.1 • z.2 + z.1 • y.2) + (y.1 * z.1) • x.2, by simp_rw [smul_add, ← mul_smul, add_assoc, mul_comm], .. triv_sq_zero_ext.mul_one_class } instance [comm_monoid R] [add_comm_monoid M] [distrib_mul_action R M] : comm_monoid (tsze R M) := { mul_comm := λ x₁ x₂, ext (mul_comm x₁.1 x₂.1) $ show x₁.1 • x₂.2 + x₂.1 • x₁.2 = x₂.1 • x₁.2 + x₁.1 • x₂.2, from add_comm _ _, .. triv_sq_zero_ext.monoid } instance [comm_semiring R] [add_comm_monoid M] [module R M] : comm_semiring (tsze R M) := { .. triv_sq_zero_ext.comm_monoid, .. triv_sq_zero_ext.non_assoc_semiring } variables (R M) /-- The canonical inclusion of rings `R → triv_sq_zero_ext R M`. -/ @[simps apply] def inl_hom [semiring R] [add_comm_monoid M] [module R M] : R →+* tsze R M := { to_fun := inl, map_one' := inl_one M, map_mul' := inl_mul M, map_zero' := inl_zero M, map_add' := inl_add M } end mul section algebra variables (S : Type*) (R : Type u) (M : Type v) variables [comm_semiring S] [comm_semiring R] [add_comm_monoid M] variables [algebra S R] [module S M] [module R M] [is_scalar_tower S R M] instance algebra' : algebra S (tsze R M) := { commutes' := λ r x, mul_comm _ _, smul_def' := λ r x, ext (algebra.smul_def _ _) $ show r • x.2 = algebra_map S R r • x.2 + x.1 • 0, by rw [smul_zero, add_zero, algebra_map_smul], .. (triv_sq_zero_ext.inl_hom R M).comp (algebra_map S R) } -- shortcut instance for the common case instance : algebra R (tsze R M) := triv_sq_zero_ext.algebra' _ _ _ lemma algebra_map_eq_inl : ⇑(algebra_map R (tsze R M)) = inl := rfl lemma algebra_map_eq_inl_hom : algebra_map R (tsze R M) = inl_hom R M := rfl lemma algebra_map_eq_inl' (s : S) : algebra_map S (tsze R M) s = inl (algebra_map S R s) := rfl /-- The canonical `R`-algebra projection `triv_sq_zero_ext R M → R`. -/ @[simps] def fst_hom : tsze R M →ₐ[R] R := { to_fun := fst, map_one' := fst_one, map_mul' := fst_mul, map_zero' := fst_zero, map_add' := fst_add, commutes' := fst_inl M } variables {R S M} lemma alg_hom_ext {A} [semiring A] [algebra R A] ⦃f g : tsze R M →ₐ[R] A⦄ (h : ∀ m, f (inr m) = g (inr m)) : f = g := alg_hom.to_linear_map_injective $ linear_map_ext (λ r, (f.commutes _).trans (g.commutes _).symm) h @[ext] lemma alg_hom_ext' {A} [semiring A] [algebra R A] ⦃f g : tsze R M →ₐ[R] A⦄ (h : f.to_linear_map.comp (inr_hom R M) = g.to_linear_map.comp (inr_hom R M)) : f = g := alg_hom_ext $ linear_map.congr_fun h variables {A : Type*} [semiring A] [algebra R A] /-- There is an alg_hom from the trivial square zero extension to any `R`-algebra with a submodule whose products are all zero. See `triv_sq_zero_ext.lift` for this as an equiv. -/ def lift_aux (f : M →ₗ[R] A) (hf : ∀ x y, f x * f y = 0) : tsze R M →ₐ[R] A := alg_hom.of_linear_map ((algebra.linear_map _ _).comp (fst_hom R M).to_linear_map + f.comp (snd_hom R M)) (show algebra_map R _ 1 + f (0 : M) = 1, by rw [map_zero, map_one, add_zero]) (triv_sq_zero_ext.ind $ λ r₁ m₁, triv_sq_zero_ext.ind $ λ r₂ m₂, begin dsimp, simp only [add_zero, zero_add, add_mul, mul_add, smul_mul_smul, hf, smul_zero], rw [←ring_hom.map_mul, linear_map.map_add, ←algebra.commutes _ (f _), ←algebra.smul_def, ←algebra.smul_def, add_right_comm, add_assoc, linear_map.map_smul, linear_map.map_smul], end) @[simp] lemma lift_aux_apply_inr (f : M →ₗ[R] A) (hf : ∀ x y, f x * f y = 0) (m : M) : lift_aux f hf (inr m) = f m := show algebra_map R A 0 + f m = f m, by rw [ring_hom.map_zero, zero_add] @[simp] lemma lift_aux_comp_inr_hom (f : M →ₗ[R] A) (hf : ∀ x y, f x * f y = 0) : (lift_aux f hf).to_linear_map.comp (inr_hom R M) = f := linear_map.ext $ lift_aux_apply_inr f hf /- When applied to `inr` itself, `lift_aux` is the identity. -/ @[simp] lemma lift_aux_inr_hom : lift_aux (inr_hom R M) (inr_mul_inr R) = alg_hom.id R (tsze R M) := alg_hom_ext' $ lift_aux_comp_inr_hom _ _ /-- A universal property of the trivial square-zero extension, providing a unique `triv_sq_zero_ext R M →ₐ[R] A` for every linear map `M →ₗ[R] A` whose range has no non-zero products. This isomorphism is named to match the very similar `complex.lift`. -/ @[simps] def lift : {f : M →ₗ[R] A // ∀ x y, f x * f y = 0} ≃ (tsze R M →ₐ[R] A) := { to_fun := λ f, lift_aux f f.prop, inv_fun := λ F, ⟨F.to_linear_map.comp (inr_hom R M), λ x y, (F.map_mul _ _).symm.trans $ (F.congr_arg $ inr_mul_inr _ _ _).trans F.map_zero⟩, left_inv := λ f, subtype.ext $ lift_aux_comp_inr_hom _ _, right_inv := λ F, alg_hom_ext' $ lift_aux_comp_inr_hom _ _, } end algebra end triv_sq_zero_ext
c515e30dd622c05893a62494a01e0096e408d0af
5749d8999a76f3a8fddceca1f6941981e33aaa96
/src/data/zsqrtd/basic.lean
bb72887cc9e98fa0aadd0fa8ad0826f2b27736ab
[ "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
26,811
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.basic algebra.associated data.nat.gcd /-- The ring of integers adjoined with a square root of `d`. These have the form `a + b √d` where `a b : ℤ`. The components are called `re` and `im` by analogy to the negative `d` case, but of course both parts are real here since `d` is nonnegative. -/ structure zsqrtd (d : ℤ) := mk {} :: (re : ℤ) (im : ℤ) prefix `ℤ√`:100 := zsqrtd namespace zsqrtd section parameters {d : ℤ} instance : decidable_eq ℤ√d := by tactic.mk_dec_eq_instance theorem ext : ∀ {z w : ℤ√d}, z = w ↔ z.re = w.re ∧ z.im = w.im | ⟨x, y⟩ ⟨x', y'⟩ := ⟨λ h, by injection h; split; assumption, λ ⟨h₁, h₂⟩, by congr; assumption⟩ /-- Convert an integer to a `ℤ√d` -/ def of_int (n : ℤ) : ℤ√d := ⟨n, 0⟩ @[simp] theorem of_int_re (n : ℤ) : (of_int n).re = n := rfl @[simp] theorem of_int_im (n : ℤ) : (of_int n).im = 0 := rfl /-- The zero of the ring -/ def zero : ℤ√d := of_int 0 instance : has_zero ℤ√d := ⟨zsqrtd.zero⟩ @[simp] theorem zero_re : (0 : ℤ√d).re = 0 := rfl @[simp] theorem zero_im : (0 : ℤ√d).im = 0 := rfl /-- The one of the ring -/ def one : ℤ√d := of_int 1 instance : has_one ℤ√d := ⟨zsqrtd.one⟩ @[simp] theorem one_re : (1 : ℤ√d).re = 1 := rfl @[simp] theorem one_im : (1 : ℤ√d).im = 0 := rfl /-- The representative of `√d` in the ring -/ def sqrtd : ℤ√d := ⟨0, 1⟩ @[simp] theorem sqrtd_re : (sqrtd : ℤ√d).re = 0 := rfl @[simp] theorem sqrtd_im : (sqrtd : ℤ√d).im = 1 := rfl /-- Addition of elements of `ℤ√d` -/ def add : ℤ√d → ℤ√d → ℤ√d | ⟨x, y⟩ ⟨x', y'⟩ := ⟨x + x', y + y'⟩ instance : has_add ℤ√d := ⟨zsqrtd.add⟩ @[simp] theorem add_def (x y x' y' : ℤ) : (⟨x, y⟩ + ⟨x', y'⟩ : ℤ√d) = ⟨x + x', y + y'⟩ := rfl @[simp] theorem add_re : ∀ z w : ℤ√d, (z + w).re = z.re + w.re | ⟨x, y⟩ ⟨x', y'⟩ := rfl @[simp] theorem add_im : ∀ z w : ℤ√d, (z + w).im = z.im + w.im | ⟨x, y⟩ ⟨x', y'⟩ := rfl @[simp] theorem bit0_re (z) : (bit0 z : ℤ√d).re = bit0 z.re := add_re _ _ @[simp] theorem bit0_im (z) : (bit0 z : ℤ√d).im = bit0 z.im := add_im _ _ @[simp] theorem bit1_re (z) : (bit1 z : ℤ√d).re = bit1 z.re := by simp [bit1] @[simp] theorem bit1_im (z) : (bit1 z : ℤ√d).im = bit0 z.im := by simp [bit1] /-- Negation in `ℤ√d` -/ def neg : ℤ√d → ℤ√d | ⟨x, y⟩ := ⟨-x, -y⟩ instance : has_neg ℤ√d := ⟨zsqrtd.neg⟩ @[simp] theorem neg_re : ∀ z : ℤ√d, (-z).re = -z.re | ⟨x, y⟩ := rfl @[simp] theorem neg_im : ∀ z : ℤ√d, (-z).im = -z.im | ⟨x, y⟩ := rfl /-- Conjugation in `ℤ√d`. The conjugate of `a + b √d` is `a - b √d`. -/ def conj : ℤ√d → ℤ√d | ⟨x, y⟩ := ⟨x, -y⟩ @[simp] theorem conj_re : ∀ z : ℤ√d, (conj z).re = z.re | ⟨x, y⟩ := rfl @[simp] theorem conj_im : ∀ z : ℤ√d, (conj z).im = -z.im | ⟨x, y⟩ := rfl /-- Multiplication in `ℤ√d` -/ def mul : ℤ√d → ℤ√d → ℤ√d | ⟨x, y⟩ ⟨x', y'⟩ := ⟨x * x' + d * y * y', x * y' + y * x'⟩ instance : has_mul ℤ√d := ⟨zsqrtd.mul⟩ @[simp] theorem mul_re : ∀ z w : ℤ√d, (z * w).re = z.re * w.re + d * z.im * w.im | ⟨x, y⟩ ⟨x', y'⟩ := rfl @[simp] theorem mul_im : ∀ z w : ℤ√d, (z * w).im = z.re * w.im + z.im * w.re | ⟨x, y⟩ ⟨x', y'⟩ := rfl instance : comm_ring ℤ√d := by refine { add := (+), zero := 0, neg := has_neg.neg, mul := (*), one := 1, ..}; { intros, simp [ext, add_mul, mul_add, mul_comm, mul_left_comm] } instance : add_comm_monoid ℤ√d := by apply_instance instance : add_monoid ℤ√d := by apply_instance instance : monoid ℤ√d := by apply_instance instance : comm_monoid ℤ√d := by apply_instance instance : comm_semigroup ℤ√d := by apply_instance instance : semigroup ℤ√d := by apply_instance instance : add_comm_semigroup ℤ√d := by apply_instance instance : add_semigroup ℤ√d := by apply_instance instance : comm_semiring ℤ√d := by apply_instance instance : semiring ℤ√d := by apply_instance instance : ring ℤ√d := by apply_instance instance : distrib ℤ√d := by apply_instance instance : zero_ne_one_class ℤ√d := { zero := 0, one := 1, zero_ne_one := dec_trivial } instance : nonzero_comm_ring ℤ√d := { ..zsqrtd.comm_ring, ..zsqrtd.zero_ne_one_class } @[simp] theorem coe_nat_re (n : ℕ) : (n : ℤ√d).re = n := by induction n; simp * @[simp] theorem coe_nat_im (n : ℕ) : (n : ℤ√d).im = 0 := by induction n; simp * theorem coe_nat_val (n : ℕ) : (n : ℤ√d) = ⟨n, 0⟩ := by simp [ext] @[simp] theorem coe_int_re (n : ℤ) : (n : ℤ√d).re = n := by cases n; simp [*, int.of_nat_eq_coe, int.neg_succ_of_nat_eq] @[simp] theorem coe_int_im (n : ℤ) : (n : ℤ√d).im = 0 := by cases n; simp * theorem coe_int_val (n : ℤ) : (n : ℤ√d) = ⟨n, 0⟩ := by simp [ext] instance : char_zero ℤ√d := { cast_injective := λ m n, by simp [ext] } @[simp] theorem of_int_eq_coe (n : ℤ) : (of_int n : ℤ√d) = n := by simp [ext] @[simp] theorem smul_val (n x y : ℤ) : (n : ℤ√d) * ⟨x, y⟩ = ⟨n * x, n * y⟩ := by simp [ext] @[simp] theorem muld_val (x y : ℤ) : sqrtd * ⟨x, y⟩ = ⟨d * y, x⟩ := by simp [ext] @[simp] theorem smuld_val (n x y : ℤ) : sqrtd * (n : ℤ√d) * ⟨x, y⟩ = ⟨d * n * y, n * x⟩ := by simp [ext] theorem decompose {x y : ℤ} : (⟨x, y⟩ : ℤ√d) = x + sqrtd * y := by simp [ext] theorem mul_conj {x y : ℤ} : (⟨x, y⟩ * conj ⟨x, y⟩ : ℤ√d) = x * x - d * y * y := by simp [ext, mul_comm] theorem conj_mul : Π {a b : ℤ√d}, conj (a * b) = conj a * conj b := by simp [ext] protected lemma coe_int_add (m n : ℤ) : (↑(m + n) : ℤ√d) = ↑m + ↑n := by simp [ext] protected lemma coe_int_sub (m n : ℤ) : (↑(m - n) : ℤ√d) = ↑m - ↑n := by simp [ext] protected lemma coe_int_mul (m n : ℤ) : (↑(m * n) : ℤ√d) = ↑m * ↑n := by simp [ext] protected lemma coe_int_inj {m n : ℤ} (h : (↑m : ℤ√d) = ↑n) : m = n := by simpa using congr_arg re h /-- Read `sq_le a c b d` as `a √c ≤ b √d` -/ def sq_le (a c b d : ℕ) : Prop := c*a*a ≤ d*b*b theorem sq_le_of_le {c d x y z w : ℕ} (xz : z ≤ x) (yw : y ≤ w) (xy : sq_le x c y d) : sq_le z c w d := le_trans (mul_le_mul (nat.mul_le_mul_left _ xz) xz (nat.zero_le _) (nat.zero_le _)) $ le_trans xy (mul_le_mul (nat.mul_le_mul_left _ yw) yw (nat.zero_le _) (nat.zero_le _)) theorem sq_le_add_mixed {c d x y z w : ℕ} (xy : sq_le x c y d) (zw : sq_le z c w d) : c * (x * z) ≤ d * (y * w) := nat.mul_self_le_mul_self_iff.2 $ by simpa [mul_comm, mul_left_comm] using mul_le_mul xy zw (nat.zero_le _) (nat.zero_le _) theorem sq_le_add {c d x y z w : ℕ} (xy : sq_le x c y d) (zw : sq_le z c w d) : sq_le (x + z) c (y + w) d := begin have xz := sq_le_add_mixed xy zw, simp [sq_le, mul_assoc] at xy zw, simp [sq_le, mul_add, mul_comm, mul_left_comm, add_le_add, *] end theorem sq_le_cancel {c d x y z w : ℕ} (zw : sq_le y d x c) (h : sq_le (x + z) c (y + w) d) : sq_le z c w d := begin apply le_of_not_gt, intro l, refine not_le_of_gt _ h, simp [sq_le, mul_add, mul_comm, mul_left_comm], have hm := sq_le_add_mixed zw (le_of_lt l), simp [sq_le, mul_assoc] at l zw, exact lt_of_le_of_lt (add_le_add_right zw _) (add_lt_add_left (add_lt_add_of_le_of_lt hm (add_lt_add_of_le_of_lt hm l)) _) end theorem sq_le_smul {c d x y : ℕ} (n : ℕ) (xy : sq_le x c y d) : sq_le (n * x) c (n * y) d := by simpa [sq_le, mul_left_comm, mul_assoc] using nat.mul_le_mul_left (n * n) xy theorem sq_le_mul {d x y z w : ℕ} : (sq_le x 1 y d → sq_le z 1 w d → sq_le (x * w + y * z) d (x * z + d * y * w) 1) ∧ (sq_le x 1 y d → sq_le w d z 1 → sq_le (x * z + d * y * w) 1 (x * w + y * z) d) ∧ (sq_le y d x 1 → sq_le z 1 w d → sq_le (x * z + d * y * w) 1 (x * w + y * z) d) ∧ (sq_le y d x 1 → sq_le w d z 1 → sq_le (x * w + y * z) d (x * z + d * y * w) 1) := by refine ⟨_, _, _, _⟩; { intros xy zw, have := int.mul_nonneg (sub_nonneg_of_le (int.coe_nat_le_coe_nat_of_le xy)) (sub_nonneg_of_le (int.coe_nat_le_coe_nat_of_le zw)), refine int.le_of_coe_nat_le_coe_nat (le_of_sub_nonneg _), simpa [mul_add, mul_left_comm, mul_comm] } /-- "Generalized" `nonneg`. `nonnegg c d x y` means `a √c + b √d ≥ 0`; we are interested in the case `c = 1` but this is more symmetric -/ def nonnegg (c d : ℕ) : ℤ → ℤ → Prop | (a : ℕ) (b : ℕ) := true | (a : ℕ) -[1+ b] := sq_le (b+1) c a d | -[1+ a] (b : ℕ) := sq_le (a+1) d b c | -[1+ a] -[1+ b] := false theorem nonnegg_comm {c d : ℕ} {x y : ℤ} : nonnegg c d x y = nonnegg d c y x := by induction x; induction y; refl theorem nonnegg_neg_pos {c d} : Π {a b : ℕ}, nonnegg c d (-a) b ↔ sq_le a d b c | 0 b := ⟨by simp [sq_le, nat.zero_le], λa, trivial⟩ | (a+1) b := by rw ← int.neg_succ_of_nat_coe; refl theorem nonnegg_pos_neg {c d} {a b : ℕ} : nonnegg c d a (-b) ↔ sq_le b c a d := by rw nonnegg_comm; exact nonnegg_neg_pos theorem nonnegg_cases_right {c d} {a : ℕ} : Π {b : ℤ}, (Π x : ℕ, b = -x → sq_le x c a d) → nonnegg c d a b | (b:nat) h := trivial | -[1+ b] h := h (b+1) rfl theorem nonnegg_cases_left {c d} {b : ℕ} {a : ℤ} (h : Π x : ℕ, a = -x → sq_le x d b c) : nonnegg c d a b := cast nonnegg_comm (nonnegg_cases_right h) section norm def norm (n : ℤ√d) : ℤ := n.re * n.re - d * n.im * n.im @[simp] lemma norm_zero : norm 0 = 0 := by simp [norm] @[simp] lemma norm_one : norm 1 = 1 := by simp [norm] @[simp] lemma norm_int_cast (n : ℤ) : norm n = n * n := by simp [norm] @[simp] lemma norm_nat_cast (n : ℕ) : norm n = n * n := norm_int_cast n @[simp] lemma norm_mul (n m : ℤ√d) : norm (n * m) = norm n * norm m := by simp [norm, mul_add, add_mul, mul_comm, mul_assoc, mul_left_comm] lemma norm_eq_mul_conj (n : ℤ√d) : (norm n : ℤ√d) = n * n.conj := by cases n; simp [norm, conj, zsqrtd.ext, mul_comm] instance : is_monoid_hom norm := { map_one := norm_one, map_mul := norm_mul } lemma norm_nonneg (hd : d ≤ 0) (n : ℤ√d) : 0 ≤ n.norm := add_nonneg (mul_self_nonneg _) (by rw [mul_assoc, neg_mul_eq_neg_mul]; exact (mul_nonneg (neg_nonneg.2 hd) (mul_self_nonneg _))) lemma norm_eq_one_iff {x : ℤ√d} : x.norm.nat_abs = 1 ↔ is_unit x := ⟨λ h, is_unit_iff_dvd_one.2 $ (le_total 0 (norm x)).cases_on (λ hx, show x ∣ 1, from ⟨x.conj, by rwa [← int.coe_nat_inj', int.nat_abs_of_nonneg hx, ← @int.cast_inj (ℤ√d) _ _, norm_eq_mul_conj, eq_comm] at h⟩) (λ hx, show x ∣ 1, from ⟨- x.conj, by rwa [← int.coe_nat_inj', int.of_nat_nat_abs_of_nonpos hx, ← @int.cast_inj (ℤ√d) _ _, int.cast_neg, norm_eq_mul_conj, neg_mul_eq_mul_neg, eq_comm] at h⟩), λ h, let ⟨y, hy⟩ := is_unit_iff_dvd_one.1 h in begin have := congr_arg (int.nat_abs ∘ norm) hy, rw [function.comp_app, function.comp_app, norm_mul, int.nat_abs_mul, norm_one, int.nat_abs_one, eq_comm, nat.mul_eq_one_iff] at this, exact this.1 end⟩ end norm end section parameter {d : ℕ} /-- Nonnegativity of an element of `ℤ√d`. -/ def nonneg : ℤ√d → Prop | ⟨a, b⟩ := nonnegg d 1 a b protected def le (a b : ℤ√d) : Prop := nonneg (b - a) instance : has_le ℤ√d := ⟨zsqrtd.le⟩ protected def lt (a b : ℤ√d) : Prop := ¬(b ≤ a) instance : has_lt ℤ√d := ⟨zsqrtd.lt⟩ instance decidable_nonnegg (c d a b) : decidable (nonnegg c d a b) := by cases a; cases b; repeat {rw int.of_nat_eq_coe}; unfold nonnegg sq_le; apply_instance instance decidable_nonneg : Π (a : ℤ√d), decidable (nonneg a) | ⟨a, b⟩ := zsqrtd.decidable_nonnegg _ _ _ _ instance decidable_le (a b : ℤ√d) : decidable (a ≤ b) := decidable_nonneg _ theorem nonneg_cases : Π {a : ℤ√d}, nonneg a → ∃ x y : ℕ, a = ⟨x, y⟩ ∨ a = ⟨x, -y⟩ ∨ a = ⟨-x, y⟩ | ⟨(x : ℕ), (y : ℕ)⟩ h := ⟨x, y, or.inl rfl⟩ | ⟨(x : ℕ), -[1+ y]⟩ h := ⟨x, y+1, or.inr $ or.inl rfl⟩ | ⟨-[1+ x], (y : ℕ)⟩ h := ⟨x+1, y, or.inr $ or.inr rfl⟩ | ⟨-[1+ x], -[1+ y]⟩ h := false.elim h lemma nonneg_add_lem {x y z w : ℕ} (xy : nonneg ⟨x, -y⟩) (zw : nonneg ⟨-z, w⟩) : nonneg (⟨x, -y⟩ + ⟨-z, w⟩) := have nonneg ⟨int.sub_nat_nat x z, int.sub_nat_nat w y⟩, from int.sub_nat_nat_elim x z (λm n i, sq_le y d m 1 → sq_le n 1 w d → nonneg ⟨i, int.sub_nat_nat w y⟩) (λj k, int.sub_nat_nat_elim w y (λm n i, sq_le n d (k + j) 1 → sq_le k 1 m d → nonneg ⟨int.of_nat j, i⟩) (λm n xy zw, trivial) (λm n xy zw, sq_le_cancel zw xy)) (λj k, int.sub_nat_nat_elim w y (λm n i, sq_le n d k 1 → sq_le (k + j + 1) 1 m d → nonneg ⟨-[1+ j], i⟩) (λm n xy zw, sq_le_cancel xy zw) (λm n xy zw, let t := nat.le_trans zw (sq_le_of_le (nat.le_add_right n (m+1)) (le_refl _) xy) in have k + j + 1 ≤ k, from nat.mul_self_le_mul_self_iff.2 (by repeat{rw one_mul at t}; exact t), absurd this (not_le_of_gt $ nat.succ_le_succ $ nat.le_add_right _ _))) (nonnegg_pos_neg.1 xy) (nonnegg_neg_pos.1 zw), show nonneg ⟨_, _⟩, by rw [neg_add_eq_sub]; rwa [int.sub_nat_nat_eq_coe,int.sub_nat_nat_eq_coe] at this theorem nonneg_add {a b : ℤ√d} (ha : nonneg a) (hb : nonneg b) : nonneg (a + b) := begin rcases nonneg_cases ha with ⟨x, y, rfl|rfl|rfl⟩; rcases nonneg_cases hb with ⟨z, w, rfl|rfl|rfl⟩; dsimp [add, nonneg] at ha hb ⊢, { trivial }, { refine nonnegg_cases_right (λi h, sq_le_of_le _ _ (nonnegg_pos_neg.1 hb)), { exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ y (by simp *))) }, { apply nat.le_add_left } }, { refine nonnegg_cases_left (λi h, sq_le_of_le _ _ (nonnegg_neg_pos.1 hb)), { exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ x (by simp *))) }, { apply nat.le_add_left } }, { refine nonnegg_cases_right (λi h, sq_le_of_le _ _ (nonnegg_pos_neg.1 ha)), { exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ w (by simp *))) }, { apply nat.le_add_right } }, { simpa using nonnegg_pos_neg.2 (sq_le_add (nonnegg_pos_neg.1 ha) (nonnegg_pos_neg.1 hb)) }, { exact nonneg_add_lem ha hb }, { refine nonnegg_cases_left (λi h, sq_le_of_le _ _ (nonnegg_neg_pos.1 ha)), { exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ z (by simp *))) }, { apply nat.le_add_right } }, { rw [add_comm, add_comm ↑y], exact nonneg_add_lem hb ha }, { simpa using nonnegg_neg_pos.2 (sq_le_add (nonnegg_neg_pos.1 ha) (nonnegg_neg_pos.1 hb)) }, end theorem le_refl (a : ℤ√d) : a ≤ a := show nonneg (a - a), by simp protected theorem le_trans {a b c : ℤ√d} (ab : a ≤ b) (bc : b ≤ c) : a ≤ c := have nonneg (b - a + (c - b)), from nonneg_add ab bc, by simpa theorem nonneg_iff_zero_le {a : ℤ√d} : nonneg a ↔ 0 ≤ a := show _ ↔ nonneg _, by simp theorem le_of_le_le {x y z w : ℤ} (xz : x ≤ z) (yw : y ≤ w) : (⟨x, y⟩ : ℤ√d) ≤ ⟨z, w⟩ := show nonneg ⟨z - x, w - y⟩, from match z - x, w - y, int.le.dest_sub xz, int.le.dest_sub yw with ._, ._, ⟨a, rfl⟩, ⟨b, rfl⟩ := trivial end theorem le_arch (a : ℤ√d) : ∃n : ℕ, a ≤ n := let ⟨x, y, (h : a ≤ ⟨x, y⟩)⟩ := show ∃x y : ℕ, nonneg (⟨x, y⟩ + -a), from match -a with | ⟨int.of_nat x, int.of_nat y⟩ := ⟨0, 0, trivial⟩ | ⟨int.of_nat x, -[1+ y]⟩ := ⟨0, y+1, by simp [int.neg_succ_of_nat_coe]⟩ | ⟨-[1+ x], int.of_nat y⟩ := ⟨x+1, 0, by simp [int.neg_succ_of_nat_coe]⟩ | ⟨-[1+ x], -[1+ y]⟩ := ⟨x+1, y+1, by simp [int.neg_succ_of_nat_coe]⟩ end in begin refine ⟨x + d*y, zsqrtd.le_trans h _⟩, rw [← int.cast_coe_nat, ← of_int_eq_coe], change nonneg ⟨(↑x + d*y) - ↑x, 0-↑y⟩, cases y with y, { simp }, have h : ∀y, sq_le y d (d * y) 1 := λ y, by simpa [sq_le, mul_comm, mul_left_comm] using nat.mul_le_mul_right (y * y) (nat.le_mul_self d), rw [show (x:ℤ) + d * nat.succ y - x = d * nat.succ y, by simp], exact h (y+1) end protected theorem nonneg_total : Π (a : ℤ√d), nonneg a ∨ nonneg (-a) | ⟨(x : ℕ), (y : ℕ)⟩ := or.inl trivial | ⟨-[1+ x], -[1+ y]⟩ := or.inr trivial | ⟨0, -[1+ y]⟩ := or.inr trivial | ⟨-[1+ x], 0⟩ := or.inr trivial | ⟨(x+1:ℕ), -[1+ y]⟩ := nat.le_total | ⟨-[1+ x], (y+1:ℕ)⟩ := nat.le_total protected theorem le_total (a b : ℤ√d) : a ≤ b ∨ b ≤ a := let t := nonneg_total (b - a) in by rw [show -(b-a) = a-b, from neg_sub b a] at t; exact t instance : preorder ℤ√d := { le := zsqrtd.le, le_refl := zsqrtd.le_refl, le_trans := @zsqrtd.le_trans, lt := zsqrtd.lt, lt_iff_le_not_le := λ a b, (and_iff_right_of_imp (zsqrtd.le_total _ _).resolve_left).symm } protected theorem add_le_add_left (a b : ℤ√d) (ab : a ≤ b) (c : ℤ√d) : c + a ≤ c + b := show nonneg _, by rw add_sub_add_left_eq_sub; exact ab protected theorem le_of_add_le_add_left (a b c : ℤ√d) (h : c + a ≤ c + b) : a ≤ b := by simpa using zsqrtd.add_le_add_left _ _ h (-c) protected theorem add_lt_add_left (a b : ℤ√d) (h : a < b) (c) : c + a < c + b := λ h', h (zsqrtd.le_of_add_le_add_left _ _ _ h') theorem nonneg_smul {a : ℤ√d} {n : ℕ} (ha : nonneg a) : nonneg (n * a) := by rw ← int.cast_coe_nat; exact match a, nonneg_cases ha, ha with | ._, ⟨x, y, or.inl rfl⟩, ha := by rw smul_val; trivial | ._, ⟨x, y, or.inr $ or.inl rfl⟩, ha := by rw smul_val; simpa using nonnegg_pos_neg.2 (sq_le_smul n $ nonnegg_pos_neg.1 ha) | ._, ⟨x, y, or.inr $ or.inr rfl⟩, ha := by rw smul_val; simpa using nonnegg_neg_pos.2 (sq_le_smul n $ nonnegg_neg_pos.1 ha) end theorem nonneg_muld {a : ℤ√d} (ha : nonneg a) : nonneg (sqrtd * a) := by refine match a, nonneg_cases ha, ha with | ._, ⟨x, y, or.inl rfl⟩, ha := trivial | ._, ⟨x, y, or.inr $ or.inl rfl⟩, ha := by simp; apply nonnegg_neg_pos.2; simpa [sq_le, mul_comm, mul_left_comm] using nat.mul_le_mul_left d (nonnegg_pos_neg.1 ha) | ._, ⟨x, y, or.inr $ or.inr rfl⟩, ha := by simp; apply nonnegg_pos_neg.2; simpa [sq_le, mul_comm, mul_left_comm] using nat.mul_le_mul_left d (nonnegg_neg_pos.1 ha) end theorem nonneg_mul_lem {x y : ℕ} {a : ℤ√d} (ha : nonneg a) : nonneg (⟨x, y⟩ * a) := have (⟨x, y⟩ * a : ℤ√d) = x * a + sqrtd * (y * a), by rw [decompose, right_distrib, mul_assoc]; refl, by rw this; exact nonneg_add (nonneg_smul ha) (nonneg_muld $ nonneg_smul ha) theorem nonneg_mul {a b : ℤ√d} (ha : nonneg a) (hb : nonneg b) : nonneg (a * b) := match a, b, nonneg_cases ha, nonneg_cases hb, ha, hb with | ._, ._, ⟨x, y, or.inl rfl⟩, ⟨z, w, or.inl rfl⟩, ha, hb := trivial | ._, ._, ⟨x, y, or.inl rfl⟩, ⟨z, w, or.inr $ or.inr rfl⟩, ha, hb := nonneg_mul_lem hb | ._, ._, ⟨x, y, or.inl rfl⟩, ⟨z, w, or.inr $ or.inl rfl⟩, ha, hb := nonneg_mul_lem hb | ._, ._, ⟨x, y, or.inr $ or.inr rfl⟩, ⟨z, w, or.inl rfl⟩, ha, hb := by rw mul_comm; exact nonneg_mul_lem ha | ._, ._, ⟨x, y, or.inr $ or.inl rfl⟩, ⟨z, w, or.inl rfl⟩, ha, hb := by rw mul_comm; exact nonneg_mul_lem ha | ._, ._, ⟨x, y, or.inr $ or.inr rfl⟩, ⟨z, w, or.inr $ or.inr rfl⟩, ha, hb := by rw [calc (⟨-x, y⟩ * ⟨-z, w⟩ : ℤ√d) = ⟨_, _⟩ : rfl ... = ⟨x * z + d * y * w, -(x * w + y * z)⟩ : by simp]; exact nonnegg_pos_neg.2 (sq_le_mul.left (nonnegg_neg_pos.1 ha) (nonnegg_neg_pos.1 hb)) | ._, ._, ⟨x, y, or.inr $ or.inr rfl⟩, ⟨z, w, or.inr $ or.inl rfl⟩, ha, hb := by rw [calc (⟨-x, y⟩ * ⟨z, -w⟩ : ℤ√d) = ⟨_, _⟩ : rfl ... = ⟨-(x * z + d * y * w), x * w + y * z⟩ : by simp]; exact nonnegg_neg_pos.2 (sq_le_mul.right.left (nonnegg_neg_pos.1 ha) (nonnegg_pos_neg.1 hb)) | ._, ._, ⟨x, y, or.inr $ or.inl rfl⟩, ⟨z, w, or.inr $ or.inr rfl⟩, ha, hb := by rw [calc (⟨x, -y⟩ * ⟨-z, w⟩ : ℤ√d) = ⟨_, _⟩ : rfl ... = ⟨-(x * z + d * y * w), x * w + y * z⟩ : by simp]; exact nonnegg_neg_pos.2 (sq_le_mul.right.right.left (nonnegg_pos_neg.1 ha) (nonnegg_neg_pos.1 hb)) | ._, ._, ⟨x, y, or.inr $ or.inl rfl⟩, ⟨z, w, or.inr $ or.inl rfl⟩, ha, hb := by rw [calc (⟨x, -y⟩ * ⟨z, -w⟩ : ℤ√d) = ⟨_, _⟩ : rfl ... = ⟨x * z + d * y * w, -(x * w + y * z)⟩ : by simp]; exact nonnegg_pos_neg.2 (sq_le_mul.right.right.right (nonnegg_pos_neg.1 ha) (nonnegg_pos_neg.1 hb)) end protected theorem mul_nonneg (a b : ℤ√d) : 0 ≤ a → 0 ≤ b → 0 ≤ a * b := by repeat {rw ← nonneg_iff_zero_le}; exact nonneg_mul theorem not_sq_le_succ (c d y) (h : 0 < c) : ¬sq_le (y + 1) c 0 d := not_le_of_gt $ mul_pos (mul_pos h $ nat.succ_pos _) $ nat.succ_pos _ /-- A nonsquare is a natural number that is not equal to the square of an integer. This is implemented as a typeclass because it's a necessary condition for much of the Pell equation theory. -/ class nonsquare (x : ℕ) : Prop := (ns : ∀n : ℕ, x ≠ n*n) parameter [dnsq : nonsquare d] include dnsq theorem d_pos : 0 < d := lt_of_le_of_ne (nat.zero_le _) $ ne.symm $ (nonsquare.ns d 0) theorem divides_sq_eq_zero {x y} (h : x * x = d * y * y) : x = 0 ∧ y = 0 := let g := x.gcd y in or.elim g.eq_zero_or_pos (λH, ⟨nat.eq_zero_of_gcd_eq_zero_left H, nat.eq_zero_of_gcd_eq_zero_right H⟩) (λgpos, false.elim $ let ⟨m, n, co, (hx : x = m * g), (hy : y = n * g)⟩ := nat.exists_coprime gpos in begin rw [hx, hy] at h, have : m * m = d * (n * n) := nat.eq_of_mul_eq_mul_left (mul_pos gpos gpos) (by simpa [mul_comm, mul_left_comm] using h), have co2 := let co1 := co.mul_right co in co1.mul co1, exact nonsquare.ns d m (nat.dvd_antisymm (by rw this; apply dvd_mul_right) $ co2.dvd_of_dvd_mul_right $ by simp [this]) end) theorem divides_sq_eq_zero_z {x y : ℤ} (h : x * x = d * y * y) : x = 0 ∧ y = 0 := by rw [mul_assoc, ← int.nat_abs_mul_self, ← int.nat_abs_mul_self, ← int.coe_nat_mul, ← mul_assoc] at h; exact let ⟨h1, h2⟩ := divides_sq_eq_zero (int.coe_nat_inj h) in ⟨int.eq_zero_of_nat_abs_eq_zero h1, int.eq_zero_of_nat_abs_eq_zero h2⟩ theorem not_divides_square (x y) : (x + 1) * (x + 1) ≠ d * (y + 1) * (y + 1) := λe, by have t := (divides_sq_eq_zero e).left; contradiction theorem nonneg_antisymm : Π {a : ℤ√d}, nonneg a → nonneg (-a) → a = 0 | ⟨0, 0⟩ xy yx := rfl | ⟨-[1+ x], -[1+ y]⟩ xy yx := false.elim xy | ⟨(x+1:nat), (y+1:nat)⟩ xy yx := false.elim yx | ⟨-[1+ x], 0⟩ xy yx := absurd xy (not_sq_le_succ _ _ _ dec_trivial) | ⟨(x+1:nat), 0⟩ xy yx := absurd yx (not_sq_le_succ _ _ _ dec_trivial) | ⟨0, -[1+ y]⟩ xy yx := absurd xy (not_sq_le_succ _ _ _ d_pos) | ⟨0, (y+1:nat)⟩ _ yx := absurd yx (not_sq_le_succ _ _ _ d_pos) | ⟨(x+1:nat), -[1+ y]⟩ (xy : sq_le _ _ _ _) (yx : sq_le _ _ _ _) := let t := le_antisymm yx xy in by rw[one_mul] at t; exact absurd t (not_divides_square _ _) | ⟨-[1+ x], (y+1:nat)⟩ (xy : sq_le _ _ _ _) (yx : sq_le _ _ _ _) := let t := le_antisymm xy yx in by rw[one_mul] at t; exact absurd t (not_divides_square _ _) theorem le_antisymm {a b : ℤ√d} (ab : a ≤ b) (ba : b ≤ a) : a = b := eq_of_sub_eq_zero $ nonneg_antisymm ba (by rw neg_sub; exact ab) instance : decidable_linear_order ℤ√d := { le_antisymm := @zsqrtd.le_antisymm, le_total := zsqrtd.le_total, decidable_le := zsqrtd.decidable_le, ..zsqrtd.preorder } protected theorem eq_zero_or_eq_zero_of_mul_eq_zero : Π {a b : ℤ√d}, a * b = 0 → a = 0 ∨ b = 0 | ⟨x, y⟩ ⟨z, w⟩ h := by injection h with h1 h2; exact have h1 : x*z = -(d*y*w), from eq_neg_of_add_eq_zero h1, have h2 : x*w = -(y*z), from eq_neg_of_add_eq_zero h2, have fin : x*x = d*y*y → (⟨x, y⟩:ℤ√d) = 0, from λe, match x, y, divides_sq_eq_zero_z e with ._, ._, ⟨rfl, rfl⟩ := rfl end, if z0 : z = 0 then if w0 : w = 0 then or.inr (match z, w, z0, w0 with ._, ._, rfl, rfl := rfl end) else or.inl $ fin $ eq_of_mul_eq_mul_right w0 $ calc x * x * w = -y * (x * z) : by simp [h2, mul_assoc, mul_left_comm] ... = d * y * y * w : by simp [h1, mul_assoc, mul_left_comm] else or.inl $ fin $ eq_of_mul_eq_mul_right z0 $ calc x * x * z = d * -y * (x * w) : by simp [h1, mul_assoc, mul_left_comm] ... = d * y * y * z : by simp [h2, mul_assoc, mul_left_comm] instance : integral_domain ℤ√d := { zero_ne_one := zero_ne_one, eq_zero_or_eq_zero_of_mul_eq_zero := @zsqrtd.eq_zero_or_eq_zero_of_mul_eq_zero, ..zsqrtd.comm_ring } protected theorem mul_pos (a b : ℤ√d) (a0 : 0 < a) (b0 : 0 < b) : 0 < a * b := λab, or.elim (eq_zero_or_eq_zero_of_mul_eq_zero (le_antisymm ab (mul_nonneg _ _ (le_of_lt a0) (le_of_lt b0)))) (λe, ne_of_gt a0 e) (λe, ne_of_gt b0 e) instance : decidable_linear_ordered_comm_ring ℤ√d := { add_le_add_left := @zsqrtd.add_le_add_left, add_lt_add_left := @zsqrtd.add_lt_add_left, zero_ne_one := zero_ne_one, mul_nonneg := @zsqrtd.mul_nonneg, mul_pos := @zsqrtd.mul_pos, zero_lt_one := dec_trivial, ..zsqrtd.comm_ring, ..zsqrtd.decidable_linear_order } instance : decidable_linear_ordered_semiring ℤ√d := by apply_instance instance : linear_ordered_semiring ℤ√d := by apply_instance instance : ordered_semiring ℤ√d := by apply_instance end end zsqrtd
ac664e7cbe674fcc4c9bedf685880c7aa1f03152
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/10_Structures_and_Records.org.6.lean
e94c6fb62deb999a0a80d6ef0a6ad768eaf2fdcb
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
65
lean
import standard record point (A : Type) := mk :: (x : A) (y : A)
800a5222014475a2bcae33658d54d57e60937840
367134ba5a65885e863bdc4507601606690974c1
/src/linear_algebra/finsupp_vector_space.lean
15e0e9b36c5e90f0e83025d1d3132c720c9fe48f
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
7,091
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl -/ import data.mv_polynomial import linear_algebra.dimension import linear_algebra.direct_sum.finsupp import linear_algebra.finite_dimensional /-! # Linear structures on function with finite support `ι →₀ M` This file contains results on the `R`-module structure on functions of finite support from a type `ι` to an `R`-module `M`, in particular in the case that `R` is a field. Furthermore, it contains some facts about isomorphisms of vector spaces from equality of dimension as well as the cardinality of finite dimensional vector spaces. ## TODO Move the second half of this file to more appropriate other files. -/ noncomputable theory local attribute [instance, priority 100] classical.prop_decidable open set linear_map submodule namespace finsupp section ring variables {R : Type*} {M : Type*} {ι : Type*} variables [ring R] [add_comm_group M] [module R M] lemma linear_independent_single {φ : ι → Type*} {f : Π ι, φ ι → M} (hf : ∀i, linear_independent R (f i)) : linear_independent R (λ ix : Σ i, φ i, single ix.1 (f ix.1 ix.2)) := begin apply @linear_independent_Union_finite R _ _ _ _ ι φ (λ i x, single i (f i x)), { assume i, have h_disjoint : disjoint (span R (range (f i))) (ker (lsingle i)), { rw ker_lsingle, exact disjoint_bot_right }, apply (hf i).map h_disjoint }, { intros i t ht hit, refine (disjoint_lsingle_lsingle {i} t (disjoint_singleton_left.2 hit)).mono _ _, { rw span_le, simp only [supr_singleton], rw range_coe, apply range_comp_subset_range }, { refine supr_le_supr (λ i, supr_le_supr _), intros hi, rw span_le, rw range_coe, apply range_comp_subset_range } } end open linear_map submodule lemma is_basis_single {φ : ι → Type*} (f : Π ι, φ ι → M) (hf : ∀i, is_basis R (f i)) : is_basis R (λ ix : Σ i, φ i, single ix.1 (f ix.1 ix.2)) := begin split, { apply linear_independent_single, exact λ i, (hf i).1 }, { rw [range_sigma_eq_Union_range, span_Union], simp only [image_univ.symm, λ i, image_comp (single i) (f i), span_single_image], simp only [image_univ, (hf _).2, map_top, supr_lsingle_range] } end lemma is_basis_single_one : is_basis R (λ i : ι, single i (1 : R)) := by convert (is_basis_single (λ (i : ι) (x : unit), (1 : R)) (λ i, is_basis_singleton_one R)).comp (λ i : ι, ⟨i, ()⟩) ⟨λ _ _, and.left ∘ sigma.mk.inj, λ ⟨i, ⟨⟩⟩, ⟨i, rfl⟩⟩ end ring section comm_ring variables {R : Type*} {M : Type*} {N : Type*} {ι : Type*} {κ : Type*} variables [comm_ring R] [add_comm_group M] [module R M] [add_comm_group N] [module R N] /-- If b : ι → M and c : κ → N are bases then so is λ i, b i.1 ⊗ₜ c i.2 : ι × κ → M ⊗ N. -/ lemma is_basis.tensor_product {b : ι → M} (hb : is_basis R b) {c : κ → N} (hc : is_basis R c) : is_basis R (λ i : ι × κ, b i.1 ⊗ₜ[R] c i.2) := by { convert linear_equiv.is_basis is_basis_single_one ((tensor_product.congr (module_equiv_finsupp hb) (module_equiv_finsupp hc)).trans $ (finsupp_tensor_finsupp _ _ _ _ _).trans $ lcongr (equiv.refl _) (tensor_product.lid R R)).symm, ext ⟨i, k⟩, rw [function.comp_apply, linear_equiv.eq_symm_apply], simp } end comm_ring section dim universes u v variables {K : Type u} {V : Type v} {ι : Type v} variables [field K] [add_comm_group V] [vector_space K V] lemma dim_eq : vector_space.dim K (ι →₀ V) = cardinal.mk ι * vector_space.dim K V := begin rcases exists_is_basis K V with ⟨bs, hbs⟩, rw [← cardinal.lift_inj, cardinal.lift_mul, ← hbs.mk_eq_dim, ← (is_basis_single _ (λa:ι, hbs)).mk_eq_dim, ← cardinal.sum_mk, ← cardinal.lift_mul, cardinal.lift_inj], { simp only [cardinal.mk_image_eq (single_injective.{u u} _), cardinal.sum_const] } end end dim end finsupp section vector_space /- We use `universe variables` instead of `universes` here because universes introduced by the `universes` keyword do not get replaced by metavariables once a lemma has been proven. So if you prove a lemma using universe `u`, you can only apply it to universe `u` in other lemmas of the same section. -/ universe variables u v w variables {K : Type u} {V V₁ V₂ : Type v} {V' : Type w} variables [field K] variables [add_comm_group V] [vector_space K V] variables [add_comm_group V₁] [vector_space K V₁] variables [add_comm_group V₂] [vector_space K V₂] variables [add_comm_group V'] [vector_space K V'] open vector_space lemma equiv_of_dim_eq_lift_dim (h : cardinal.lift.{v w} (dim K V) = cardinal.lift.{w v} (dim K V')) : nonempty (V ≃ₗ[K] V') := begin haveI := classical.dec_eq V, haveI := classical.dec_eq V', rcases exists_is_basis K V with ⟨m, hm⟩, rcases exists_is_basis K V' with ⟨m', hm'⟩, rw [←cardinal.lift_inj.1 hm.mk_eq_dim, ←cardinal.lift_inj.1 hm'.mk_eq_dim] at h, rcases quotient.exact h with ⟨e⟩, let e := (equiv.ulift.symm.trans e).trans equiv.ulift, exact ⟨((module_equiv_finsupp hm).trans (finsupp.dom_lcongr e)).trans (module_equiv_finsupp hm').symm⟩, end /-- Two `K`-vector spaces are equivalent if their dimension is the same. -/ def equiv_of_dim_eq_dim (h : dim K V₁ = dim K V₂) : V₁ ≃ₗ[K] V₂ := begin classical, exact classical.choice (equiv_of_dim_eq_lift_dim (cardinal.lift_inj.2 h)) end /-- An `n`-dimensional `K`-vector space is equivalent to `fin n → K`. -/ def fin_dim_vectorspace_equiv (n : ℕ) (hn : (dim K V) = n) : V ≃ₗ[K] (fin n → K) := begin have : cardinal.lift.{v u} (n : cardinal.{v}) = cardinal.lift.{u v} (n : cardinal.{u}), by simp, have hn := cardinal.lift_inj.{v u}.2 hn, rw this at hn, rw ←@dim_fin_fun K _ n at hn, exact classical.choice (equiv_of_dim_eq_lift_dim hn), end end vector_space section vector_space universes u open vector_space variables (K V : Type u) [field K] [add_comm_group V] [vector_space K V] lemma cardinal_mk_eq_cardinal_mk_field_pow_dim [finite_dimensional K V] : cardinal.mk V = cardinal.mk K ^ dim K V := begin rcases exists_is_basis K V with ⟨s, hs⟩, have : nonempty (fintype s), { rw [← cardinal.lt_omega_iff_fintype, cardinal.lift_inj.1 hs.mk_eq_dim], exact finite_dimensional.dim_lt_omega K V }, cases this with hsf, letI := hsf, calc cardinal.mk V = cardinal.mk (s →₀ K) : quotient.sound ⟨(module_equiv_finsupp hs).to_equiv⟩ ... = cardinal.mk (s → K) : quotient.sound ⟨finsupp.equiv_fun_on_fintype⟩ ... = _ : by rw [← cardinal.lift_inj.1 hs.mk_eq_dim, cardinal.power_def] end lemma cardinal_lt_omega_of_finite_dimensional [fintype K] [finite_dimensional K V] : cardinal.mk V < cardinal.omega := begin rw cardinal_mk_eq_cardinal_mk_field_pow_dim K V, exact cardinal.power_lt_omega (cardinal.lt_omega_iff_fintype.2 ⟨infer_instance⟩) (finite_dimensional.dim_lt_omega K V), end end vector_space
c2b9d9b9836e721ab2c4983f2fccfd42e8c857d2
08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4
/src/Lean/MetavarContext.lean
e1040c1af21e9a266483761f4bb76f267563cbc9
[ "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "Apache-2.0", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
gebner/lean4
d51c4922640a52a6f7426536ea669ef18a1d9af5
8cd9ce06843c9d42d6d6dc43d3e81e3b49dfc20f
refs/heads/master
1,685,732,780,391
1,672,962,627,000
1,673,459,398,000
373,307,283
0
0
Apache-2.0
1,691,316,730,000
1,622,669,271,000
Lean
UTF-8
Lean
false
false
64,085
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.MonadCache import Lean.LocalContext namespace Lean /-! The metavariable context stores metavariable declarations and their assignments. It is used in the elaborator, tactic framework, unifier (aka `isDefEq`), and type class resolution (TC). First, we list all the requirements imposed by these modules. - We may invoke TC while executing `isDefEq`. We need this feature to be able to solve unification problems such as: ``` f ?a (ringAdd ?s) ?x ?y =?= f Int intAdd n m ``` where `(?a : Type) (?s : Ring ?a) (?x ?y : ?a)` During `isDefEq` (i.e., unification), it will need to solve the constrain ``` ringAdd ?s =?= intAdd ``` We say `ringAdd ?s` is stuck because it cannot be reduced until we synthesize the term `?s : Ring ?a` using TC. This can be done since we have assigned `?a := Int` when solving `?a =?= Int`. - TC uses `isDefEq`, and `isDefEq` may create TC problems as shown above. Thus, we may have nested TC problems. - `isDefEq` extends the local context when going inside binders. Thus, the local context for nested TC may be an extension of the local context for outer TC. - TC should not assign metavariables created by the elaborator, simp, tactic framework, and outer TC problems. Reason: TC commits to the first solution it finds. Consider the TC problem `Coe Nat ?x`, where `?x` is a metavariable created by the caller. There are many solutions to this problem (e.g., `?x := Int`, `?x := Real`, ...), and it doesn’t make sense to commit to the first one since TC does not know the constraints the caller may impose on `?x` after the TC problem is solved. Remark: we claim it is not feasible to make the whole system backtrackable, and allow the caller to backtrack back to TC and ask it for another solution if the first one found did not work. We claim it would be too inefficient. - TC metavariables should not leak outside of TC. Reason: we want to get rid of them after we synthesize the instance. - `simp` invokes `isDefEq` for matching the left-hand-side of equations to terms in our goal. Thus, it may invoke TC indirectly. - In Lean3, we didn’t have to create a fresh pattern for trying to match the left-hand-side of equations when executing `simp`. We had a mechanism called "tmp" metavariables. It avoided this overhead, but it created many problems since `simp` may indirectly call TC which may recursively call TC. Moreover, we may want to allow TC to invoke tactics in the future. Thus, when `simp` invokes `isDefEq`, it may indirectly invoke a tactic and `simp` itself. The Lean3 approach assumed that metavariables were short-lived, this is not true in Lean4, and to some extent was also not true in Lean3 since `simp`, in principle, could trigger an arbitrary number of nested TC problems. - Here are some possible call stack traces we could have in Lean3 (and Lean4). ``` Elaborator (-> TC -> isDefEq)+ Elaborator -> isDefEq (-> TC -> isDefEq)* Elaborator -> simp -> isDefEq (-> TC -> isDefEq)* ``` In Lean4, TC may also invoke tactics in the future. - In Lean3 and Lean4, TC metavariables are not really short-lived. We solve an arbitrary number of unification problems, and we may have nested TC invocations. - TC metavariables do not share the same local context even in the same invocation. In the C++ and Lean implementations we use a trick to ensure they do: https://github.com/leanprover/lean/blob/92826917a252a6092cffaf5fc5f1acb1f8cef379/src/library/type_context.cpp#L3583-L3594 - Metavariables may be natural, synthetic or syntheticOpaque. a) Natural metavariables may be assigned by unification (i.e., `isDefEq`). b) Synthetic metavariables may still be assigned by unification, but whenever possible `isDefEq` will avoid the assignment. For example, if we have the unification constraint `?m =?= ?n`, where `?m` is synthetic, but `?n` is not, `isDefEq` solves it by using the assignment `?n := ?m`. We use synthetic metavariables for type class resolution. Any module that creates synthetic metavariables, must also check whether they have been assigned by `isDefEq`, and then still synthesize them, and check whether the synthesized result is compatible with the one assigned by `isDefEq`. c) SyntheticOpaque metavariables are never assigned by `isDefEq`. That is, the constraint `?n =?= Nat.succ Nat.zero` always fail if `?n` is a syntheticOpaque metavariable. This kind of metavariable is created by tactics such as `intro`. Reason: in the tactic framework, subgoals as represented as metavariables, and a subgoal `?n` is considered as solved whenever the metavariable is assigned. This distinction was not precise in Lean3 and produced counterintuitive behavior. For example, the following hack was added in Lean3 to work around one of these issues: https://github.com/leanprover/lean/blob/92826917a252a6092cffaf5fc5f1acb1f8cef379/src/library/type_context.cpp#L2751 - When creating lambda/forall expressions, we need to convert/abstract free variables and convert them to bound variables. Now, suppose we a trying to create a lambda/forall expression by abstracting free variable `xs` and a term `t[?m]` which contains a metavariable `?m`, and the local context of `?m` contains `xs`. The term ``` fun xs => t[?m] ``` will be ill-formed if we later assign a term `s` to `?m`, and `s` contains free variables in `xs`. We address this issue by changing the free variable abstraction procedure. We consider two cases: `?m` is natural, `?m` is synthetic. Assume the type of `?m` is `A[xs]`. Then, in both cases we create an auxiliary metavariable `?n` with type `forall xs => A[xs]`, and local context := local context of `?m` - `xs`. In both cases, we produce the term `fun xs => t[?n xs]` 1- If `?m` is natural or synthetic, then we assign `?m := ?n xs`, and we produce the term `fun xs => t[?n xs]` 2- If `?m` is syntheticOpaque, then we mark `?n` as a syntheticOpaque variable. However, `?n` is managed by the metavariable context itself. We say we have a "delayed assignment" `?n xs := ?m`. That is, after a term `s` is assigned to `?m`, and `s` does not contain metavariables, we replace any occurrence `?n ts` with `s[xs := ts]`. Gruesome details: - When we create the type `forall xs => A` for `?n`, we may encounter the same issue if `A` contains metavariables. So, the process above is recursive. We claim it terminates because we keep creating new metavariables with smaller local contexts. - Suppose, we have `t[?m]` and we want to create a let-expression by abstracting a let-decl free variable `x`, and the local context of `?m` contains `x`. Similarly to the previous case ``` let x : T := v; t[?m] ``` will be ill-formed if we later assign a term `s` to `?m`, and `s` contains free variable `x`. Again, assume the type of `?m` is `A[x]`. 1- If `?m` is natural or synthetic, then we create `?n : (let x : T := v; A[x])` with and local context := local context of `?m` - `x`, we assign `?m := ?n`, and produce the term `let x : T := v; t[?n]`. That is, we are just making sure `?n` must never be assigned to a term containing `x`. 2- If `?m` is syntheticOpaque, we create a fresh syntheticOpaque `?n` with type `?n : T -> (let x : T := v; A[x])` and local context := local context of `?m` - `x`, create the delayed assignment `?n #[x] := ?m`, and produce the term `let x : T := v; t[?n x]`. Now suppose we assign `s` to `?m`. We do not assign the term `fun (x : T) => s` to `?n`, since `fun (x : T) => s` may not even be type correct. Instead, we just replace applications `?n r` with `s[x/r]`. The term `r` may not necessarily be a bound variable. For example, a tactic may have reduced `let x : T := v; t[?n x]` into `t[?n v]`. We are essentially using the pair "delayed assignment + application" to implement a delayed substitution. - We use TC for implementing coercions. Both Joe Hendrix and Reid Barton reported a nasty limitation. In Lean3, TC will not be used if there are metavariables in the TC problem. For example, the elaborator will not try to synthesize `Coe Nat ?x`. This is good, but this constraint is too strict for problems such as `Coe (Vector Bool ?n) (BV ?n)`. The coercion exists independently of `?n`. Thus, during TC, we want `isDefEq` to throw an exception instead of return `false` whenever it tries to assign a metavariable owned by its caller. The idea is to sign to the caller that it cannot solve the TC problem at this point, and more information is needed. That is, the caller must make progress an assign its metavariables before trying to invoke TC again. In Lean4, we are using a simpler design for the `MetavarContext`. - No distinction between temporary and regular metavariables. - Metavariables have a `depth` Nat field. - MetavarContext also has a `depth` field. - We bump the `MetavarContext` depth when we create a nested problem. Example: Elaborator (depth = 0) -> Simplifier matcher (depth = 1) -> TC (level = 2) -> TC (level = 3) -> ... - When `MetavarContext` is at depth N, `isDefEq` does not assign variables from `depth < N`. - Metavariables from depth N+1 must be fully assigned before we return to level N. - New design even allows us to invoke tactics from TC. * Main concern We don't have tmp metavariables anymore in Lean4. Thus, before trying to match the left-hand-side of an equation in `simp`. We first must bump the level of the `MetavarContext`, create fresh metavariables, then create a new pattern by replacing the free variable on the left-hand-side with these metavariables. We are hoping to minimize this overhead by - Using better indexing data structures in `simp`. They should reduce the number of time `simp` must invoke `isDefEq`. - Implementing `isDefEqApprox` which ignores metavariables and returns only `false` or `undef`. It is a quick filter that allows us to fail quickly and avoid the creation of new fresh metavariables, and a new pattern. - Adding built-in support for arithmetic, Logical connectives, etc. Thus, we avoid a bunch of lemmas in the simp set. - Adding support for AC-rewriting. In Lean3, users use AC lemmas as rewriting rules for "sorting" terms. This is inefficient, requires a quadratic number of rewrite steps, and does not preserve the structure of the goal. The temporary metavariables were also used in the "app builder" module used in Lean3. The app builder uses `isDefEq`. So, it could, in principle, invoke an arbitrary number of nested TC problems. However, in Lean3, all app builder uses are controlled. That is, it is mainly used to synthesize implicit arguments using very simple unification and/or non-nested TC. So, if the "app builder" becomes a bottleneck without tmp metavars, we may solve the issue by implementing `isDefEqCheap` that never invokes TC and uses tmp metavars. -/ /-- `LocalInstance` represents a local typeclass instance registered by and for the elaborator. It stores the name of the typeclass in `className`, and the concrete typeclass instance in `fvar`. Note that the kernel does not care about this information, since typeclasses are entirely eliminated during elaboration. -/ structure LocalInstance where className : Name fvar : Expr deriving Inhabited abbrev LocalInstances := Array LocalInstance instance : BEq LocalInstance where beq i₁ i₂ := i₁.fvar == i₂.fvar /-- Remove local instance with the given `fvarId`. Do nothing if `localInsts` does not contain any free variable with id `fvarId`. -/ def LocalInstances.erase (localInsts : LocalInstances) (fvarId : FVarId) : LocalInstances := match localInsts.findIdx? (fun inst => inst.fvar.fvarId! == fvarId) with | some idx => localInsts.eraseIdx idx | _ => localInsts /-- A kind for the metavariable that determines its unification behaviour. For more information see the large comment at the beginning of this file. -/ inductive MetavarKind where /-- Normal unification behaviour -/ | natural /-- `isDefEq` avoids assignment -/ | synthetic /-- Never assigned by isDefEq -/ | syntheticOpaque deriving Inhabited, Repr def MetavarKind.isSyntheticOpaque : MetavarKind → Bool | MetavarKind.syntheticOpaque => true | _ => false def MetavarKind.isNatural : MetavarKind → Bool | MetavarKind.natural => true | _ => false /-- Information about a metavariable. -/ structure MetavarDecl where /-- A user-friendly name for the metavariable. If anonymous then there is no such name. -/ userName : Name := Name.anonymous /-- The local context containing the free variables that the mvar is permitted to depend upon. -/ lctx : LocalContext /-- The type of the metavarible, in the given `lctx`. -/ type : Expr /-- The nesting depth of this metavariable. We do not want unification subproblems to influence the results of parent problems. The depth keeps track of this information and ensures that unification subproblems cannot leak information out, by unifying based on depth. -/ depth : Nat localInstances : LocalInstances kind : MetavarKind /-- See comment at `CheckAssignment` `Meta/ExprDefEq.lean` -/ numScopeArgs : Nat := 0 /-- We use this field to track how old a metavariable is. It is set using a counter at `MetavarContext` -/ index : Nat deriving Inhabited /-- A delayed assignment for a metavariable `?m`. It represents an assignment of the form `?m := (fun fvars => (mkMVar mvarIdPending))`. `mvarIdPending` is a `syntheticOpaque` metavariable that has not been synthesized yet. The delayed assignment becomes a real one as soon as `mvarIdPending` has been fully synthesized. `fvars` are variables in the `mvarIdPending` local context. See the comment below `assignDelayedMVar ` for the rationale of delayed assignments. Recall that we use a locally nameless approach when dealing with binders. Suppose we are trying to synthesize `?n` in the expression `e`, in the context of `(fun x => e)`. The metavariable `?n` might depend on the bound variable `x`. However, since we are locally nameless, the bound variable `x` is in fact represented by some free variable `fvar_x`. Thus, when we exit the scope, we must rebind the value of `fvar_x` in `?n` to the de-bruijn index of the bound variable `x`. -/ structure DelayedMetavarAssignment where fvars : Array Expr mvarIdPending : MVarId /-- The metavariable context is a set of metavariable declarations and their assignments. For more information on specifics see the comment in the file that `MetavarContext` is defined in. -/ structure MetavarContext where /-- Depth is used to control whether an mvar can be assigned in unification. -/ depth : Nat := 0 /-- At what depth level mvars can be assigned. -/ levelAssignDepth : Nat := 0 /-- Counter for setting the field `index` at `MetavarDecl` -/ mvarCounter : Nat := 0 lDepth : PersistentHashMap LMVarId Nat := {} /-- Metavariable declarations. -/ decls : PersistentHashMap MVarId MetavarDecl := {} /-- Index mapping user-friendly names to ids. -/ userNames : PersistentHashMap Name MVarId := {} /-- Assignment table for universe level metavariables.-/ lAssignment : PersistentHashMap LMVarId Level := {} /-- Assignment table for expression metavariables.-/ eAssignment : PersistentHashMap MVarId Expr := {} /-- Assignment table for delayed abstraction metavariables. For more information about delayed abstraction, see the docstring for `DelayedMetavarAssignment`. -/ dAssignment : PersistentHashMap MVarId DelayedMetavarAssignment := {} /-- A monad with a stateful metavariable context, defining `getMCtx` and `modifyMCtx`. -/ class MonadMCtx (m : Type → Type) where getMCtx : m MetavarContext modifyMCtx : (MetavarContext → MetavarContext) → m Unit export MonadMCtx (getMCtx modifyMCtx) @[always_inline] instance (m n) [MonadLift m n] [MonadMCtx m] : MonadMCtx n where getMCtx := liftM (getMCtx : m _) modifyMCtx := fun f => liftM (modifyMCtx f : m _) abbrev setMCtx [MonadMCtx m] (mctx : MetavarContext) : m Unit := modifyMCtx fun _ => mctx abbrev getLevelMVarAssignment? [Monad m] [MonadMCtx m] (mvarId : LMVarId) : m (Option Level) := return (← getMCtx).lAssignment.find? mvarId def MetavarContext.getExprAssignmentCore? (m : MetavarContext) (mvarId : MVarId) : Option Expr := m.eAssignment.find? mvarId def getExprMVarAssignment? [Monad m] [MonadMCtx m] (mvarId : MVarId) : m (Option Expr) := return (← getMCtx).getExprAssignmentCore? mvarId def getDelayedMVarAssignment? [Monad m] [MonadMCtx m] (mvarId : MVarId) : m (Option DelayedMetavarAssignment) := return (← getMCtx).dAssignment.find? mvarId /-- Given a sequence of delayed assignments ``` mvarId₁ := mvarId₂ ...; ... mvarIdₙ := mvarId_root ... -- where `mvarId_root` is not delayed assigned ``` in `mctx`, `getDelayedRoot mctx mvarId₁` return `mvarId_root`. If `mvarId₁` is not delayed assigned then return `mvarId₁` -/ partial def getDelayedMVarRoot [Monad m] [MonadMCtx m] (mvarId : MVarId) : m MVarId := do match (← getDelayedMVarAssignment? mvarId) with | some d => getDelayedMVarRoot d.mvarIdPending | none => return mvarId def isLevelMVarAssigned [Monad m] [MonadMCtx m] (mvarId : LMVarId) : m Bool := return (← getMCtx).lAssignment.contains mvarId /-- Return `true` if the give metavariable is already assigned. -/ def _root_.Lean.MVarId.isAssigned [Monad m] [MonadMCtx m] (mvarId : MVarId) : m Bool := return (← getMCtx).eAssignment.contains mvarId @[deprecated MVarId.isAssigned] def isExprMVarAssigned [Monad m] [MonadMCtx m] (mvarId : MVarId) : m Bool := do mvarId.isAssigned def _root_.Lean.MVarId.isDelayedAssigned [Monad m] [MonadMCtx m] (mvarId : MVarId) : m Bool := return (← getMCtx).dAssignment.contains mvarId @[deprecated MVarId.isDelayedAssigned] def isMVarDelayedAssigned [Monad m] [MonadMCtx m] (mvarId : MVarId) : m Bool := do mvarId.isDelayedAssigned def isLevelMVarAssignable [Monad m] [MonadMCtx m] (mvarId : LMVarId) : m Bool := do let mctx ← getMCtx match mctx.lDepth.find? mvarId with | some d => return d >= mctx.levelAssignDepth | _ => panic! "unknown universe metavariable" def MetavarContext.getDecl (mctx : MetavarContext) (mvarId : MVarId) : MetavarDecl := match mctx.decls.find? mvarId with | some decl => decl | none => panic! "unknown metavariable" def _root_.Lean.MVarId.isAssignable [Monad m] [MonadMCtx m] (mvarId : MVarId) : m Bool := do let mctx ← getMCtx let decl := mctx.getDecl mvarId return decl.depth == mctx.depth @[deprecated MVarId.isAssignable] def isExprMVarAssignable [Monad m] [MonadMCtx m] (mvarId : MVarId) : m Bool := do mvarId.isAssignable /-- Return true iff the given level contains an assigned metavariable. -/ def hasAssignedLevelMVar [Monad m] [MonadMCtx m] : Level → m Bool | .succ lvl => pure lvl.hasMVar <&&> hasAssignedLevelMVar lvl | .max lvl₁ lvl₂ => (pure lvl₁.hasMVar <&&> hasAssignedLevelMVar lvl₁) <||> (pure lvl₂.hasMVar <&&> hasAssignedLevelMVar lvl₂) | .imax lvl₁ lvl₂ => (pure lvl₁.hasMVar <&&> hasAssignedLevelMVar lvl₁) <||> (pure lvl₂.hasMVar <&&> hasAssignedLevelMVar lvl₂) | .mvar mvarId => isLevelMVarAssigned mvarId | .zero => pure false | .param _ => pure false /-- Return `true` iff expression contains assigned (level/expr) metavariables or delayed assigned mvars -/ def hasAssignedMVar [Monad m] [MonadMCtx m] : Expr → m Bool | .const _ lvls => lvls.anyM hasAssignedLevelMVar | .sort lvl => hasAssignedLevelMVar lvl | .app f a => (pure f.hasMVar <&&> hasAssignedMVar f) <||> (pure a.hasMVar <&&> hasAssignedMVar a) | .letE _ t v b _ => (pure t.hasMVar <&&> hasAssignedMVar t) <||> (pure v.hasMVar <&&> hasAssignedMVar v) <||> (pure b.hasMVar <&&> hasAssignedMVar b) | .forallE _ d b _ => (pure d.hasMVar <&&> hasAssignedMVar d) <||> (pure b.hasMVar <&&> hasAssignedMVar b) | .lam _ d b _ => (pure d.hasMVar <&&> hasAssignedMVar d) <||> (pure b.hasMVar <&&> hasAssignedMVar b) | .fvar _ => return false | .bvar _ => return false | .lit _ => return false | .mdata _ e => pure e.hasMVar <&&> hasAssignedMVar e | .proj _ _ e => pure e.hasMVar <&&> hasAssignedMVar e | .mvar mvarId => mvarId.isAssigned <||> mvarId.isDelayedAssigned /-- Return true iff the given level contains a metavariable that can be assigned. -/ def hasAssignableLevelMVar [Monad m] [MonadMCtx m] : Level → m Bool | .succ lvl => pure lvl.hasMVar <&&> hasAssignableLevelMVar lvl | .max lvl₁ lvl₂ => (pure lvl₁.hasMVar <&&> hasAssignableLevelMVar lvl₁) <||> (pure lvl₂.hasMVar <&&> hasAssignableLevelMVar lvl₂) | .imax lvl₁ lvl₂ => (pure lvl₁.hasMVar <&&> hasAssignableLevelMVar lvl₁) <||> (pure lvl₂.hasMVar <&&> hasAssignableLevelMVar lvl₂) | .mvar mvarId => isLevelMVarAssignable mvarId | .zero => return false | .param _ => return false /-- Return `true` iff expression contains a metavariable that can be assigned. -/ def hasAssignableMVar [Monad m] [MonadMCtx m] : Expr → m Bool | .const _ lvls => lvls.anyM hasAssignableLevelMVar | .sort lvl => hasAssignableLevelMVar lvl | .app f a => (pure f.hasMVar <&&> hasAssignableMVar f) <||> (pure a.hasMVar <&&> hasAssignableMVar a) | .letE _ t v b _ => (pure t.hasMVar <&&> hasAssignableMVar t) <||> (pure v.hasMVar <&&> hasAssignableMVar v) <||> (pure b.hasMVar <&&> hasAssignableMVar b) | .forallE _ d b _ => (pure d.hasMVar <&&> hasAssignableMVar d) <||> (pure b.hasMVar <&&> hasAssignableMVar b) | .lam _ d b _ => (pure d.hasMVar <&&> hasAssignableMVar d) <||> (pure b.hasMVar <&&> hasAssignableMVar b) | .fvar _ => return false | .bvar _ => return false | .lit _ => return false | .mdata _ e => pure e.hasMVar <&&> hasAssignableMVar e | .proj _ _ e => pure e.hasMVar <&&> hasAssignableMVar e | .mvar mvarId => mvarId.isAssignable /-- Add `mvarId := u` to the universe metavariable assignment. This method does not check whether `mvarId` is already assigned, nor it checks whether a cycle is being introduced. This is a low-level API, and it is safer to use `isLevelDefEq (mkLevelMVar mvarId) u`. -/ def assignLevelMVar [MonadMCtx m] (mvarId : LMVarId) (val : Level) : m Unit := modifyMCtx fun m => { m with lAssignment := m.lAssignment.insert mvarId val } /-- Add `mvarId := x` to the metavariable assignment. This method does not check whether `mvarId` is already assigned, nor it checks whether a cycle is being introduced, or whether the expression has the right type. This is a low-level API, and it is safer to use `isDefEq (mkMVar mvarId) x`. -/ def _root_.Lean.MVarId.assign [MonadMCtx m] (mvarId : MVarId) (val : Expr) : m Unit := modifyMCtx fun m => { m with eAssignment := m.eAssignment.insert mvarId val } @[deprecated MVarId.assign] def assignExprMVar [MonadMCtx m] (mvarId : MVarId) (val : Expr) : m Unit := mvarId.assign val def assignDelayedMVar [MonadMCtx m] (mvarId : MVarId) (fvars : Array Expr) (mvarIdPending : MVarId) : m Unit := modifyMCtx fun m => { m with dAssignment := m.dAssignment.insert mvarId { fvars, mvarIdPending } } /-! Notes on artificial eta-expanded terms due to metavariables. We try avoid synthetic terms such as `((fun x y => t) a b)` in the output produced by the elaborator. This kind of term may be generated when instantiating metavariable assignments. This module tries to avoid their generation because they often introduce unnecessary dependencies and may affect automation. When elaborating terms, we use metavariables to represent "holes". Each hole has a context which includes all free variables that may be used to "fill" the hole. Suppose, we create a metavariable (hole) `?m : Nat` in a context containing `(x : Nat) (y : Nat) (b : Bool)`, then we can assign terms such as `x + y` to `?m` since `x` and `y` are in the context used to create `?m`. Now, suppose we have the term `?m + 1` and we want to create the lambda expression `fun x => ?m + 1`. This term is not correct since we may assign to `?m` a term containing `x`. We address this issue by create a synthetic metavariable `?n : Nat → Nat` and adding the delayed assignment `?n #[x] := ?m`, and the term `fun x => ?n x + 1`. When we later assign a term `t[x]` to `?m`, `fun x => t[x]` is assigned to `?n`, and if we substitute it at `fun x => ?n x + 1`, we produce `fun x => ((fun x => t[x]) x) + 1`. To avoid this term eta-expanded term, we apply beta-reduction when instantiating metavariable assignments in this module. This operation is performed at `instantiateExprMVars`, `elimMVarDeps`, and `levelMVarToParam`. -/ partial def instantiateLevelMVars [Monad m] [MonadMCtx m] : Level → m Level | lvl@(Level.succ lvl₁) => return Level.updateSucc! lvl (← instantiateLevelMVars lvl₁) | lvl@(Level.max lvl₁ lvl₂) => return Level.updateMax! lvl (← instantiateLevelMVars lvl₁) (← instantiateLevelMVars lvl₂) | lvl@(Level.imax lvl₁ lvl₂) => return Level.updateIMax! lvl (← instantiateLevelMVars lvl₁) (← instantiateLevelMVars lvl₂) | lvl@(Level.mvar mvarId) => do match (← getLevelMVarAssignment? mvarId) with | some newLvl => if !newLvl.hasMVar then pure newLvl else do let newLvl' ← instantiateLevelMVars newLvl assignLevelMVar mvarId newLvl' pure newLvl' | none => pure lvl | lvl => pure lvl /-- instantiateExprMVars main function -/ partial def instantiateExprMVars [Monad m] [MonadMCtx m] [STWorld ω m] [MonadLiftT (ST ω) m] (e : Expr) : MonadCacheT ExprStructEq Expr m Expr := if !e.hasMVar then pure e else checkCache { val := e : ExprStructEq } fun _ => do match e with | .proj _ _ s => return e.updateProj! (← instantiateExprMVars s) | .forallE _ d b _ => return e.updateForallE! (← instantiateExprMVars d) (← instantiateExprMVars b) | .lam _ d b _ => return e.updateLambdaE! (← instantiateExprMVars d) (← instantiateExprMVars b) | .letE _ t v b _ => return e.updateLet! (← instantiateExprMVars t) (← instantiateExprMVars v) (← instantiateExprMVars b) | .const _ lvls => return e.updateConst! (← lvls.mapM instantiateLevelMVars) | .sort lvl => return e.updateSort! (← instantiateLevelMVars lvl) | .mdata _ b => return e.updateMData! (← instantiateExprMVars b) | .app .. => e.withApp fun f args => do let instArgs (f : Expr) : MonadCacheT ExprStructEq Expr m Expr := do let args ← args.mapM instantiateExprMVars pure (mkAppN f args) let instApp : MonadCacheT ExprStructEq Expr m Expr := do let wasMVar := f.isMVar let f ← instantiateExprMVars f if wasMVar && f.isLambda then /- Some of the arguments in args are irrelevant after we beta reduce. -/ instantiateExprMVars (f.betaRev args.reverse) else instArgs f match f with | .mvar mvarId => match (← getDelayedMVarAssignment? mvarId) with | none => instApp | some { fvars, mvarIdPending } => /- Apply "delayed substitution" (i.e., delayed assignment + application). That is, `f` is some metavariable `?m`, that is delayed assigned to `val`. If after instantiating `val`, we obtain `newVal`, and `newVal` does not contain metavariables, we replace the free variables `fvars` in `newVal` with the first `fvars.size` elements of `args`. -/ if fvars.size > args.size then /- We don't have sufficient arguments for instantiating the free variables `fvars`. This can only happy if a tactic or elaboration function is not implemented correctly. We decided to not use `panic!` here and report it as an error in the frontend when we are checking for unassigned metavariables in an elaborated term. -/ instArgs f else let newVal ← instantiateExprMVars (mkMVar mvarIdPending) if newVal.hasExprMVar then instArgs f else do let args ← args.mapM instantiateExprMVars /- Example: suppose we have `?m t1 t2 t3` That is, `f := ?m` and `args := #[t1, t2, t3]` Morever, `?m` is delayed assigned `?m #[x, y] := f x y` where, `fvars := #[x, y]` and `newVal := f x y`. After abstracting `newVal`, we have `f (Expr.bvar 0) (Expr.bvar 1)`. After `instantiaterRevRange 0 2 args`, we have `f t1 t2`. After `mkAppRange 2 3`, we have `f t1 t2 t3` -/ let newVal := newVal.abstract fvars let result := newVal.instantiateRevRange 0 fvars.size args let result := mkAppRange result fvars.size args.size args pure result | _ => instApp | e@(.mvar mvarId) => checkCache { val := e : ExprStructEq } fun _ => do match (← getExprMVarAssignment? mvarId) with | some newE => do let newE' ← instantiateExprMVars newE mvarId.assign newE' pure newE' | none => pure e | e => pure e instance : MonadMCtx (StateRefT MetavarContext (ST ω)) where getMCtx := get modifyMCtx := modify def instantiateMVarsCore (mctx : MetavarContext) (e : Expr) : Expr × MetavarContext := let instantiate {ω} (e : Expr) : (MonadCacheT ExprStructEq Expr <| StateRefT MetavarContext (ST ω)) Expr := instantiateExprMVars e runST fun _ => instantiate e |>.run |>.run mctx def instantiateMVars [Monad m] [MonadMCtx m] (e : Expr) : m Expr := do if !e.hasMVar then return e else let (r, mctx) := instantiateMVarsCore (← getMCtx) e modifyMCtx fun _ => mctx return r def instantiateLCtxMVars [Monad m] [MonadMCtx m] (lctx : LocalContext) : m LocalContext := lctx.foldlM (init := {}) fun lctx ldecl => do match ldecl with | .cdecl _ fvarId userName type bi k => let type ← instantiateMVars type return lctx.mkLocalDecl fvarId userName type bi k | .ldecl _ fvarId userName type value nonDep k => let type ← instantiateMVars type let value ← instantiateMVars value return lctx.mkLetDecl fvarId userName type value nonDep k def instantiateMVarDeclMVars [Monad m] [MonadMCtx m] (mvarId : MVarId) : m Unit := do let mvarDecl := (← getMCtx).getDecl mvarId let lctx ← instantiateLCtxMVars mvarDecl.lctx let type ← instantiateMVars mvarDecl.type modifyMCtx fun mctx => { mctx with decls := mctx.decls.insert mvarId { mvarDecl with lctx, type } } def instantiateLocalDeclMVars [Monad m] [MonadMCtx m] (localDecl : LocalDecl) : m LocalDecl := do match localDecl with | .cdecl idx id n type bi k => return .cdecl idx id n (← instantiateMVars type) bi k | .ldecl idx id n type val nonDep k => return .ldecl idx id n (← instantiateMVars type) (← instantiateMVars val) nonDep k namespace DependsOn structure State where visited : ExprSet := {} mctx : MetavarContext private abbrev M := StateM State instance : MonadMCtx M where getMCtx := return (← get).mctx modifyMCtx f := modify fun s => { s with mctx := f s.mctx } private def shouldVisit (e : Expr) : M Bool := do if !e.hasMVar && !e.hasFVar then return false else if (← get).visited.contains e then return false else modify fun s => { s with visited := s.visited.insert e } return true @[specialize] private partial def dep (pf : FVarId → Bool) (pm : MVarId → Bool) (e : Expr) : M Bool := let rec visit (e : Expr) : M Bool := do if !(← shouldVisit e) then pure false else visitMain e, visitApp : Expr → M Bool | .app f a .. => visitApp f <||> visit a | e => visit e, visitMain : Expr → M Bool | .proj _ _ s => visit s | .forallE _ d b _ => visit d <||> visit b | .lam _ d b _ => visit d <||> visit b | .letE _ t v b _ => visit t <||> visit v <||> visit b | .mdata _ b => visit b | e@(.app ..) => do let f := e.getAppFn if f.isMVar then let e' ← instantiateMVars e if e'.getAppFn != f then visitMain e' else if pm f.mvarId! then return true else visitApp e else visitApp e | .mvar mvarId => do match (← getExprMVarAssignment? mvarId) with | some a => visit a | none => if pm mvarId then return true else let lctx := (← getMCtx).getDecl mvarId |>.lctx return lctx.any fun decl => pf decl.fvarId | .fvar fvarId => return pf fvarId | _ => pure false visit e @[inline] partial def main (pf : FVarId → Bool) (pm : MVarId → Bool) (e : Expr) : M Bool := if !e.hasFVar && !e.hasMVar then pure false else dep pf pm e end DependsOn /-- Return `true` iff `e` depends on a free variable `x` s.t. `pf x` is `true`, or an unassigned metavariable `?m` s.t. `pm ?m` is true. For each metavariable `?m` (that does not satisfy `pm` occurring in `x` 1- If `?m := t`, then we visit `t` looking for `x` 2- If `?m` is unassigned, then we consider the worst case and check whether `x` is in the local context of `?m`. This case is a "may dependency". That is, we may assign a term `t` to `?m` s.t. `t` contains `x`. -/ @[inline] def findExprDependsOn [Monad m] [MonadMCtx m] (e : Expr) (pf : FVarId → Bool := fun _ => false) (pm : MVarId → Bool := fun _ => false) : m Bool := do let (result, { mctx, .. }) := DependsOn.main pf pm e |>.run { mctx := (← getMCtx) } setMCtx mctx return result /-- Similar to `findExprDependsOn`, but checks the expressions in the given local declaration depends on a free variable `x` s.t. `pf x` is `true` or an unassigned metavariable `?m` s.t. `pm ?m` is true. -/ @[inline] def findLocalDeclDependsOn [Monad m] [MonadMCtx m] (localDecl : LocalDecl) (pf : FVarId → Bool := fun _ => false) (pm : MVarId → Bool := fun _ => false) : m Bool := do match localDecl with | .cdecl (type := t) .. => findExprDependsOn t pf pm | .ldecl (type := t) (value := v) .. => let (result, { mctx, .. }) := (DependsOn.main pf pm t <||> DependsOn.main pf pm v).run { mctx := (← getMCtx) } setMCtx mctx return result def exprDependsOn [Monad m] [MonadMCtx m] (e : Expr) (fvarId : FVarId) : m Bool := findExprDependsOn e (fvarId == ·) /-- Return true iff `e` depends on the free variable `fvarId` -/ def dependsOn [Monad m] [MonadMCtx m] (e : Expr) (fvarId : FVarId) : m Bool := exprDependsOn e fvarId /-- Return true iff `e` depends on the free variable `fvarId` -/ def localDeclDependsOn [Monad m] [MonadMCtx m] (localDecl : LocalDecl) (fvarId : FVarId) : m Bool := findLocalDeclDependsOn localDecl (fvarId == ·) /-- Similar to `exprDependsOn`, but `x` can be a free variable or an unassigned metavariable. -/ def exprDependsOn' [Monad m] [MonadMCtx m] (e : Expr) (x : Expr) : m Bool := if x.isFVar then findExprDependsOn e (x.fvarId! == ·) else if x.isMVar then findExprDependsOn e (pm := (x.mvarId! == ·)) else return false /-- Similar to `localDeclDependsOn`, but `x` can be a free variable or an unassigned metavariable. -/ def localDeclDependsOn' [Monad m] [MonadMCtx m] (localDecl : LocalDecl) (x : Expr) : m Bool := if x.isFVar then findLocalDeclDependsOn localDecl (x.fvarId! == ·) else if x.isMVar then findLocalDeclDependsOn localDecl (pm := (x.mvarId! == ·)) else return false /-- Return true iff `e` depends on a free variable `x` s.t. `pf x`, or an unassigned metavariable `?m` s.t. `pm ?m` is true. -/ def dependsOnPred [Monad m] [MonadMCtx m] (e : Expr) (pf : FVarId → Bool := fun _ => false) (pm : MVarId → Bool := fun _ => false) : m Bool := findExprDependsOn e pf pm /-- Return true iff the local declaration `localDecl` depends on a free variable `x` s.t. `pf x`, an unassigned metavariable `?m` s.t. `pm ?m` is true. -/ def localDeclDependsOnPred [Monad m] [MonadMCtx m] (localDecl : LocalDecl) (pf : FVarId → Bool := fun _ => false) (pm : MVarId → Bool := fun _ => false) : m Bool := do findLocalDeclDependsOn localDecl pf pm namespace MetavarContext instance : Inhabited MetavarContext := ⟨{}⟩ @[export lean_mk_metavar_ctx] def mkMetavarContext : Unit → MetavarContext := fun _ => {} /-- Low level API for adding/declaring metavariable declarations. It is used to implement actions in the monads `MetaM`, `ElabM` and `TacticM`. It should not be used directly since the argument `(mvarId : MVarId)` is assumed to be "unique". -/ def addExprMVarDecl (mctx : MetavarContext) (mvarId : MVarId) (userName : Name) (lctx : LocalContext) (localInstances : LocalInstances) (type : Expr) (kind : MetavarKind := MetavarKind.natural) (numScopeArgs : Nat := 0) : MetavarContext := { mctx with mvarCounter := mctx.mvarCounter + 1 decls := mctx.decls.insert mvarId { depth := mctx.depth index := mctx.mvarCounter userName lctx localInstances type kind numScopeArgs } userNames := if userName.isAnonymous then mctx.userNames else mctx.userNames.insert userName mvarId } def addExprMVarDeclExp (mctx : MetavarContext) (mvarId : MVarId) (userName : Name) (lctx : LocalContext) (localInstances : LocalInstances) (type : Expr) (kind : MetavarKind) : MetavarContext := addExprMVarDecl mctx mvarId userName lctx localInstances type kind /-- Low level API for adding/declaring universe level metavariable declarations. It is used to implement actions in the monads `MetaM`, `ElabM` and `TacticM`. It should not be used directly since the argument `(mvarId : MVarId)` is assumed to be "unique". -/ def addLevelMVarDecl (mctx : MetavarContext) (mvarId : LMVarId) : MetavarContext := { mctx with lDepth := mctx.lDepth.insert mvarId mctx.depth } def findDecl? (mctx : MetavarContext) (mvarId : MVarId) : Option MetavarDecl := mctx.decls.find? mvarId def findUserName? (mctx : MetavarContext) (userName : Name) : Option MVarId := mctx.userNames.find? userName def setMVarKind (mctx : MetavarContext) (mvarId : MVarId) (kind : MetavarKind) : MetavarContext := let decl := mctx.getDecl mvarId { mctx with decls := mctx.decls.insert mvarId { decl with kind := kind } } /-- Set the metavariable user facing name. -/ def setMVarUserName (mctx : MetavarContext) (mvarId : MVarId) (userName : Name) : MetavarContext := let decl := mctx.getDecl mvarId { mctx with decls := mctx.decls.insert mvarId { decl with userName := userName } userNames := let userNames := mctx.userNames.erase decl.userName if userName.isAnonymous then userNames else userNames.insert userName mvarId } /-- Low-level version of `setMVarUserName`. It does not update the table `userNames`. Thus, `findUserName?` cannot see the modification. It is meant for `mkForallFVars'` where we temporarily set the user facing name of metavariables to get more meaningful binder names. -/ def setMVarUserNameTemporarily (mctx : MetavarContext) (mvarId : MVarId) (userName : Name) : MetavarContext := let decl := mctx.getDecl mvarId { mctx with decls := mctx.decls.insert mvarId { decl with userName := userName } } /-- Update the type of the given metavariable. This function assumes the new type is definitionally equal to the current one -/ def setMVarType (mctx : MetavarContext) (mvarId : MVarId) (type : Expr) : MetavarContext := let decl := mctx.getDecl mvarId { mctx with decls := mctx.decls.insert mvarId { decl with type := type } } def findLevelDepth? (mctx : MetavarContext) (mvarId : LMVarId) : Option Nat := mctx.lDepth.find? mvarId def getLevelDepth (mctx : MetavarContext) (mvarId : LMVarId) : Nat := match mctx.findLevelDepth? mvarId with | some d => d | none => panic! "unknown metavariable" def isAnonymousMVar (mctx : MetavarContext) (mvarId : MVarId) : Bool := match mctx.findDecl? mvarId with | none => false | some mvarDecl => mvarDecl.userName.isAnonymous def incDepth (mctx : MetavarContext) (allowLevelAssignments := false) : MetavarContext := let depth := mctx.depth + 1 let levelAssignDepth := if allowLevelAssignments then mctx.levelAssignDepth else depth { mctx with depth, levelAssignDepth } instance : MonadMCtx (StateRefT MetavarContext (ST ω)) where getMCtx := get modifyMCtx := modify namespace MkBinding inductive Exception where | revertFailure (mctx : MetavarContext) (lctx : LocalContext) (toRevert : Array Expr) (varName : String) instance : ToString Exception where toString | Exception.revertFailure _ lctx toRevert varName => "failed to revert " ++ toString (toRevert.map (fun x => "'" ++ toString (lctx.getFVar! x).userName ++ "'")) ++ ", '" ++ toString varName ++ "' depends on them, and it is an auxiliary declaration created by the elaborator" ++ " (possible solution: use tactic 'clear' to remove '" ++ toString varName ++ "' from local context)" /-- `MkBinding` and `elimMVarDepsAux` are mutually recursive, but `cache` is only used at `elimMVarDepsAux`. We use a single state object for convenience. We have a `NameGenerator` because we need to generate fresh auxiliary metavariables. -/ structure State where mctx : MetavarContext nextMacroScope : MacroScope ngen : NameGenerator cache : HashMap ExprStructEq Expr := {} structure Context where mainModule : Name preserveOrder : Bool /-- When creating binders for abstracted metavariables, we use the following `BinderInfo`. -/ binderInfoForMVars : BinderInfo := BinderInfo.implicit /-- Set of unassigned metavariables being abstracted. -/ mvarIdsToAbstract : MVarIdSet := {} abbrev MCore := EStateM Exception State abbrev M := ReaderT Context MCore instance : MonadMCtx M where getMCtx := return (← get).mctx modifyMCtx f := modify fun s => { s with mctx := f s.mctx } private def mkFreshBinderName (n : Name := `x) : M Name := do let fresh ← modifyGet fun s => (s.nextMacroScope, { s with nextMacroScope := s.nextMacroScope + 1 }) return addMacroScope (← read).mainModule n fresh def preserveOrder : M Bool := return (← read).preserveOrder instance : MonadHashMapCacheAdapter ExprStructEq Expr M where getCache := do let s ← get; pure s.cache modifyCache := fun f => modify fun s => { s with cache := f s.cache } /-- Return the local declaration of the free variable `x` in `xs` with the smallest index -/ private def getLocalDeclWithSmallestIdx (lctx : LocalContext) (xs : Array Expr) : LocalDecl := Id.run do let mut d : LocalDecl := lctx.getFVar! xs[0]! for x in xs[1:] do if x.isFVar then let curr := lctx.getFVar! x if curr.index < d.index then d := curr return d /-- Given `toRevert` an array of free variables s.t. `lctx` contains their declarations, return a new array of free variables that contains `toRevert` and all free variables in `lctx` that may depend on `toRevert`. Remark: the result is sorted by `LocalDecl` indices. Remark: We used to throw an `Exception.revertFailure` exception when an auxiliary declaration had to be reversed. Recall that auxiliary declarations are created when compiling (mutually) recursive definitions. The `revertFailure` due to auxiliary declaration dependency was originally introduced in Lean3 to address issue https://github.com/leanprover/lean/issues/1258. In Lean4, this solution is not satisfactory because all definitions/theorems are potentially recursive. So, even an simple (incomplete) definition such as ``` variables {α : Type} in def f (a : α) : List α := _ ``` would trigger the `Exception.revertFailure` exception. In the definition above, the elaborator creates the auxiliary definition `f : {α : Type} → List α`. The `_` is elaborated as a new fresh variable `?m` that contains `α : Type`, `a : α`, and `f : α → List α` in its context, When we try to create the lambda `fun {α : Type} (a : α) => ?m`, we first need to create an auxiliary `?n` which do not contain `α` and `a` in its context. That is, we create the metavariable `?n : {α : Type} → (a : α) → (f : α → List α) → List α`, add the delayed assignment `?n #[α, a, f] := ?m α a f`, and create the lambda `fun {α : Type} (a : α) => ?n α a f`. See `elimMVarDeps` for more information. If we kept using the Lean3 approach, we would get the `Exception.revertFailure` exception because we are reverting the auxiliary definition `f`. Note that https://github.com/leanprover/lean/issues/1258 is not an issue in Lean4 because we have changed how we compile recursive definitions. -/ def collectForwardDeps (lctx : LocalContext) (toRevert : Array Expr) : M (Array Expr) := do if toRevert.size == 0 then pure toRevert else if (← preserveOrder) then -- Make sure toRevert[j] does not depend on toRevert[i] for j > i toRevert.size.forM fun i => do let fvar := toRevert[i]! i.forM fun j => do let prevFVar := toRevert[j]! let prevDecl := lctx.getFVar! prevFVar if (← localDeclDependsOn prevDecl fvar.fvarId!) then throw (Exception.revertFailure (← getMCtx) lctx toRevert prevDecl.userName.toString) let newToRevert := if (← preserveOrder) then toRevert else Array.mkEmpty toRevert.size let firstDeclToVisit := getLocalDeclWithSmallestIdx lctx toRevert let initSize := newToRevert.size lctx.foldlM (init := newToRevert) (start := firstDeclToVisit.index) fun (newToRevert : Array Expr) decl => do if initSize.any fun i => decl.fvarId == newToRevert[i]!.fvarId! then return newToRevert else if toRevert.any fun x => decl.fvarId == x.fvarId! then return newToRevert.push decl.toExpr else if (← findLocalDeclDependsOn decl (newToRevert.any fun x => x.fvarId! == ·)) then return newToRevert.push decl.toExpr else return newToRevert /-- Create a new `LocalContext` by removing the free variables in `toRevert` from `lctx`. We use this function when we create auxiliary metavariables at `elimMVarDepsAux`. -/ def reduceLocalContext (lctx : LocalContext) (toRevert : Array Expr) : LocalContext := toRevert.foldr (init := lctx) fun x lctx => if x.isFVar then lctx.erase x.fvarId! else lctx /-- Return free variables in `xs` that are in the local context `lctx` -/ private def getInScope (lctx : LocalContext) (xs : Array Expr) : Array Expr := xs.foldl (init := #[]) fun scope x => if !x.isFVar then scope else if lctx.contains x.fvarId! then scope.push x else scope /-- Execute `x` with an empty cache, and then restore the original cache. -/ @[inline] private def withFreshCache (x : M α) : M α := do let cache ← modifyGet fun s => (s.cache, { s with cache := {} }) let a ← x modify fun s => { s with cache := cache } pure a /-- Create an application `mvar ys` where `ys` are the free variables. See "Gruesome details" in the beginning of the file for understanding how let-decl free variables are handled. -/ private def mkMVarApp (lctx : LocalContext) (mvar : Expr) (xs : Array Expr) (kind : MetavarKind) : Expr := xs.foldl (init := mvar) fun e x => if !x.isFVar then e else match kind with | MetavarKind.syntheticOpaque => mkApp e x | _ => if (lctx.getFVar! x).isLet then e else mkApp e x mutual private partial def visit (xs : Array Expr) (e : Expr) : M Expr := if !e.hasMVar then pure e else checkCache { val := e : ExprStructEq } fun _ => elim xs e private partial def elim (xs : Array Expr) (e : Expr) : M Expr := match e with | .proj _ _ s => return e.updateProj! (← visit xs s) | .forallE _ d b _ => return e.updateForallE! (← visit xs d) (← visit xs b) | .lam _ d b _ => return e.updateLambdaE! (← visit xs d) (← visit xs b) | .letE _ t v b _ => return e.updateLet! (← visit xs t) (← visit xs v) (← visit xs b) | .mdata _ b => return e.updateMData! (← visit xs b) | .app .. => e.withApp fun f args => elimApp xs f args | .mvar _ => elimApp xs e #[] | e => return e private partial def mkAuxMVarType (lctx : LocalContext) (xs : Array Expr) (kind : MetavarKind) (e : Expr) : M Expr := do let e ← abstractRangeAux xs xs.size e xs.size.foldRevM (init := e) fun i e => do let x := xs[i]! if x.isFVar then match lctx.getFVar! x with | LocalDecl.cdecl _ _ n type bi _ => let type := type.headBeta let type ← abstractRangeAux xs i type return Lean.mkForall n bi type e | LocalDecl.ldecl _ _ n type value nonDep _ => let type := type.headBeta let type ← abstractRangeAux xs i type let value ← abstractRangeAux xs i value let e := mkLet n type value e nonDep match kind with | MetavarKind.syntheticOpaque => -- See "Gruesome details" section in the beginning of the file let e := e.liftLooseBVars 0 1 return mkForall n BinderInfo.default type e | _ => pure e else let mvarDecl := (← get).mctx.getDecl x.mvarId! let type := mvarDecl.type.headBeta let type ← abstractRangeAux xs i type let id ← if mvarDecl.userName.isAnonymous then mkFreshBinderName else pure mvarDecl.userName return Lean.mkForall id (← read).binderInfoForMVars type e where abstractRangeAux (xs : Array Expr) (i : Nat) (e : Expr) : M Expr := do let e ← elim xs e pure (e.abstractRange i xs) private partial def elimMVar (xs : Array Expr) (mvarId : MVarId) (args : Array Expr) : M (Expr × Array Expr) := do let mvarDecl := (← getMCtx).getDecl mvarId let mvarLCtx := mvarDecl.lctx let toRevert := getInScope mvarLCtx xs if toRevert.size == 0 then let args ← args.mapM (visit xs) return (mkAppN (mkMVar mvarId) args, #[]) else /- `newMVarKind` is the kind for the new auxiliary metavariable. There is an alternative approach where we use ``` let newMVarKind := if !mctx.isExprAssignable mvarId || mvarDecl.isSyntheticOpaque then MetavarKind.syntheticOpaque else MetavarKind.natural ``` In this approach, we use the natural kind for the new auxiliary metavariable if the original metavariable is synthetic and assignable. Since we mainly use synthetic metavariables for pending type class (TC) resolution problems, this approach may minimize the number of TC resolution problems that may need to be resolved. A potential disadvantage is that `isDefEq` will not eagerly use `synthPending` for natural metavariables. That being said, we should try this approach as soon as we have an extensive test suite. -/ let newMVarKind := if !(← mvarId.isAssignable) then MetavarKind.syntheticOpaque else mvarDecl.kind let args ← args.mapM (visit xs) let toRevert ← collectForwardDeps mvarLCtx toRevert let newMVarLCtx := reduceLocalContext mvarLCtx toRevert -- Note that `toRevert` only contains free variables since it is the result of `getInScope` let newLocalInsts := mvarDecl.localInstances.filter fun inst => toRevert.all fun x => inst.fvar != x -- Remark: we must reset the before processing `mkAuxMVarType` because `toRevert` may not be equal to `xs` let newMVarType ← withFreshCache do mkAuxMVarType mvarLCtx toRevert newMVarKind mvarDecl.type let newMVarId := { name := (← get).ngen.curr } let newMVar := mkMVar newMVarId let result := mkMVarApp mvarLCtx newMVar toRevert newMVarKind let numScopeArgs := mvarDecl.numScopeArgs + result.getAppNumArgs modify fun s => { s with mctx := s.mctx.addExprMVarDecl newMVarId Name.anonymous newMVarLCtx newLocalInsts newMVarType newMVarKind numScopeArgs, ngen := s.ngen.next } if !mvarDecl.kind.isSyntheticOpaque then mvarId.assign result else /- If `mvarId` is the lhs of a delayed assignment `?m #[x_1, ... x_n] := ?mvarPending`, then `nestedFVars` is `#[x_1, ..., x_n]`. In this case, `newMVarId` is also `syntheticOpaque` and we add the delayed assignment delayed assignment ``` ?newMVar #[y_1, ..., y_m, x_1, ... x_n] := ?m ``` where `#[y_1, ..., y_m]` is `toRevert` after `collectForwardDeps`. -/ let (mvarIdPending, nestedFVars) ← match (← getDelayedMVarAssignment? mvarId) with | none => pure (mvarId, #[]) | some { fvars, mvarIdPending } => pure (mvarIdPending, fvars) assignDelayedMVar newMVarId (toRevert ++ nestedFVars) mvarIdPending return (mkAppN result args, toRevert) private partial def elimApp (xs : Array Expr) (f : Expr) (args : Array Expr) : M Expr := do match f with | Expr.mvar mvarId => match (← getExprMVarAssignment? mvarId) with | some newF => if newF.isLambda then let args ← args.mapM (visit xs) elim xs <| newF.betaRev args.reverse else elimApp xs newF args | none => if (← read).mvarIdsToAbstract.contains mvarId then return mkAppN f (← args.mapM (visit xs)) else return (← elimMVar xs mvarId args).1 | _ => return mkAppN (← visit xs f) (← args.mapM (visit xs)) end partial def elimMVarDeps (xs : Array Expr) (e : Expr) : M Expr := if !e.hasMVar then return e else withFreshCache do elim xs e partial def revert (xs : Array Expr) (mvarId : MVarId) : M (Expr × Array Expr) := withFreshCache do elimMVar xs mvarId #[] /-- Similar to `Expr.abstractRange`, but handles metavariables correctly. It uses `elimMVarDeps` to ensure `e` and the type of the free variables `xs` do not contain a metavariable `?m` s.t. local context of `?m` contains a free variable in `xs`. `elimMVarDeps` is defined later in this file. -/ @[inline] def abstractRange (xs : Array Expr) (i : Nat) (e : Expr) : M Expr := do let e ← elimMVarDeps xs e pure (e.abstractRange i xs) /-- Similar to `LocalContext.mkBinding`, but handles metavariables correctly. If `usedOnly == false` then `forall` and `lambda` expressions are created only for used variables. If `usedLetOnly == false` then `let` expressions are created only for used (let-) variables. -/ @[specialize] def mkBinding (isLambda : Bool) (lctx : LocalContext) (xs : Array Expr) (e : Expr) (usedOnly : Bool) (usedLetOnly : Bool) : M (Expr × Nat) := do let e ← abstractRange xs xs.size e xs.size.foldRevM (init := (e, 0)) fun i (e, num) => do let x := xs[i]! if x.isFVar then match lctx.getFVar! x with | LocalDecl.cdecl _ _ n type bi _ => if !usedOnly || e.hasLooseBVar 0 then let type := type.headBeta; let type ← abstractRange xs i type if isLambda then return (Lean.mkLambda n bi type e, num + 1) else return (Lean.mkForall n bi type e, num + 1) else return (e.lowerLooseBVars 1 1, num) | LocalDecl.ldecl _ _ n type value nonDep _ => if !usedLetOnly || e.hasLooseBVar 0 then let type ← abstractRange xs i type let value ← abstractRange xs i value return (mkLet n type value e nonDep, num + 1) else return (e.lowerLooseBVars 1 1, num) else let mvarDecl := (← get).mctx.getDecl x.mvarId! let type := mvarDecl.type.headBeta let type ← abstractRange xs i type let id ← if mvarDecl.userName.isAnonymous then mkFreshBinderName else pure mvarDecl.userName if isLambda then return (Lean.mkLambda id (← read).binderInfoForMVars type e, num + 1) else return (Lean.mkForall id (← read).binderInfoForMVars type e, num + 1) end MkBinding structure MkBindingM.Context where mainModule : Name lctx : LocalContext abbrev MkBindingM := ReaderT MkBindingM.Context MkBinding.MCore def elimMVarDeps (xs : Array Expr) (e : Expr) (preserveOrder : Bool) : MkBindingM Expr := fun ctx => MkBinding.elimMVarDeps xs e { preserveOrder, mainModule := ctx.mainModule } def revert (xs : Array Expr) (mvarId : MVarId) (preserveOrder : Bool) : MkBindingM (Expr × Array Expr) := fun ctx => MkBinding.revert xs mvarId { preserveOrder, mainModule := ctx.mainModule } def mkBinding (isLambda : Bool) (xs : Array Expr) (e : Expr) (usedOnly : Bool := false) (usedLetOnly : Bool := true) (binderInfoForMVars := BinderInfo.implicit) : MkBindingM (Expr × Nat) := fun ctx => let mvarIdsToAbstract := xs.foldl (init := {}) fun s x => if x.isMVar then s.insert x.mvarId! else s MkBinding.mkBinding isLambda ctx.lctx xs e usedOnly usedLetOnly { preserveOrder := false, binderInfoForMVars, mvarIdsToAbstract, mainModule := ctx.mainModule } @[inline] def mkLambda (xs : Array Expr) (e : Expr) (usedOnly : Bool := false) (usedLetOnly : Bool := true) (binderInfoForMVars := BinderInfo.implicit) : MkBindingM Expr := return (← mkBinding (isLambda := true) xs e usedOnly usedLetOnly binderInfoForMVars).1 @[inline] def mkForall (xs : Array Expr) (e : Expr) (usedOnly : Bool := false) (usedLetOnly : Bool := true) (binderInfoForMVars := BinderInfo.implicit) : MkBindingM Expr := return (← mkBinding (isLambda := false) xs e usedOnly usedLetOnly binderInfoForMVars).1 @[inline] def abstractRange (e : Expr) (n : Nat) (xs : Array Expr) : MkBindingM Expr := fun ctx => MkBinding.abstractRange xs n e { preserveOrder := false, mainModule := ctx.mainModule } @[inline] def collectForwardDeps (toRevert : Array Expr) (preserveOrder : Bool) : MkBindingM (Array Expr) := fun ctx => MkBinding.collectForwardDeps ctx.lctx toRevert { preserveOrder, mainModule := ctx.mainModule } /-- `isWellFormed mctx lctx e` return true if - All locals in `e` are declared in `lctx` - All metavariables `?m` in `e` have a local context which is a subprefix of `lctx` or are assigned, and the assignment is well-formed. -/ partial def isWellFormed [Monad m] [MonadMCtx m] (lctx : LocalContext) : Expr → m Bool | .mdata _ e => isWellFormed lctx e | .proj _ _ e => isWellFormed lctx e | e@(.app f a) => pure (!e.hasExprMVar && !e.hasFVar) <||> (isWellFormed lctx f <&&> isWellFormed lctx a) | e@(.lam _ d b _) => pure (!e.hasExprMVar && !e.hasFVar) <||> (isWellFormed lctx d <&&> isWellFormed lctx b) | e@(.forallE _ d b _) => pure (!e.hasExprMVar && !e.hasFVar) <||> (isWellFormed lctx d <&&> isWellFormed lctx b) | e@(.letE _ t v b _) => pure (!e.hasExprMVar && !e.hasFVar) <||> (isWellFormed lctx t <&&> isWellFormed lctx v <&&> isWellFormed lctx b) | .const .. => return true | .bvar .. => return true | .sort .. => return true | .lit .. => return true | .mvar mvarId => do let mvarDecl := (← getMCtx).getDecl mvarId; if mvarDecl.lctx.isSubPrefixOf lctx then return true else match (← getExprMVarAssignment? mvarId) with | none => return false | some v => isWellFormed lctx v | .fvar fvarId => return lctx.contains fvarId namespace LevelMVarToParam structure Context where paramNamePrefix : Name alreadyUsedPred : Name → Bool except : LMVarId → Bool structure State where mctx : MetavarContext paramNames : Array Name := #[] nextParamIdx : Nat cache : HashMap ExprStructEq Expr := {} abbrev M := ReaderT Context <| StateM State instance : MonadMCtx M where getMCtx := return (← get).mctx modifyMCtx f := modify fun s => { s with mctx := f s.mctx } instance : MonadCache ExprStructEq Expr M where findCached? e := return (← get).cache.find? e cache e v := modify fun s => { s with cache := s.cache.insert e v } partial def mkParamName : M Name := do let ctx ← read let s ← get let newParamName := ctx.paramNamePrefix.appendIndexAfter s.nextParamIdx if ctx.alreadyUsedPred newParamName then modify fun s => { s with nextParamIdx := s.nextParamIdx + 1 } mkParamName else do modify fun s => { s with nextParamIdx := s.nextParamIdx + 1, paramNames := s.paramNames.push newParamName } pure newParamName partial def visitLevel (u : Level) : M Level := do match u with | .succ v => return u.updateSucc! (← visitLevel v) | .max v₁ v₂ => return u.updateMax! (← visitLevel v₁) (← visitLevel v₂) | .imax v₁ v₂ => return u.updateIMax! (← visitLevel v₁) (← visitLevel v₂) | .zero => return u | .param .. => return u | .mvar mvarId => match (← getLevelMVarAssignment? mvarId) with | some v => visitLevel v | none => if (← read).except mvarId then return u else let p ← mkParamName let p := mkLevelParam p assignLevelMVar mvarId p return p partial def main (e : Expr) : M Expr := if !e.hasMVar then return e else checkCache { val := e : ExprStructEq } fun _ => do match e with | .proj _ _ s => return e.updateProj! (← main s) | .forallE _ d b _ => return e.updateForallE! (← main d) (← main b) | .lam _ d b _ => return e.updateLambdaE! (← main d) (← main b) | .letE _ t v b _ => return e.updateLet! (← main t) (← main v) (← main b) | .app .. => e.withApp fun f args => visitApp f args | .mdata _ b => return e.updateMData! (← main b) | .const _ us => return e.updateConst! (← us.mapM visitLevel) | .sort u => return e.updateSort! (← visitLevel u) | .mvar .. => visitApp e #[] | e => return e where visitApp (f : Expr) (args : Array Expr) : M Expr := do match f with | .mvar mvarId .. => match (← getExprMVarAssignment? mvarId) with | some v => return (← visitApp v args).headBeta | none => return mkAppN f (← args.mapM main) | _ => return mkAppN (← main f) (← args.mapM main) end LevelMVarToParam structure UnivMVarParamResult where mctx : MetavarContext newParamNames : Array Name nextParamIdx : Nat expr : Expr def levelMVarToParam (mctx : MetavarContext) (alreadyUsedPred : Name → Bool) (except : LMVarId → Bool) (e : Expr) (paramNamePrefix : Name := `u) (nextParamIdx : Nat := 1) : UnivMVarParamResult := let (e, s) := LevelMVarToParam.main e { except, paramNamePrefix, alreadyUsedPred } { mctx, nextParamIdx } { mctx := s.mctx newParamNames := s.paramNames nextParamIdx := s.nextParamIdx expr := e } def getExprAssignmentDomain (mctx : MetavarContext) : Array MVarId := mctx.eAssignment.foldl (init := #[]) fun a mvarId _ => Array.push a mvarId end MetavarContext end Lean
5306a6fb268fac3a6482f89dbfd79aceffe5989e
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/tests/lean/run/500_lean3.lean
39de9ca088c99e041d39f40a4a56c3a59efaed6a
[ "Apache-2.0" ]
permissive
collares/lean4
861a9269c4592bce49b71059e232ff0bfe4594cc
52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee
refs/heads/master
1,691,419,031,324
1,618,678,138,000
1,618,678,138,000
358,989,750
0
0
Apache-2.0
1,618,696,333,000
1,618,696,333,000
null
UTF-8
Lean
false
false
124
lean
example (foo bar : OptionM Nat) : False := by have do { let x ← bar; foo } = bar >>= fun x => foo := rfl admit done
7d762ba3fb4eef58684d538f11809d6e5c4284ff
abd85493667895c57a7507870867b28124b3998f
/src/group_theory/order_of_element.lean
186fa205ab6bf0432aca30c7febc02cf8e14f8c6
[ "Apache-2.0" ]
permissive
pechersky/mathlib
d56eef16bddb0bfc8bc552b05b7270aff5944393
f1df14c2214ee114c9738e733efd5de174deb95d
refs/heads/master
1,666,714,392,571
1,591,747,567,000
1,591,747,567,000
270,557,274
0
0
Apache-2.0
1,591,597,975,000
1,591,597,974,000
null
UTF-8
Lean
false
false
24,259
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import group_theory.coset import data.nat.totient import data.set.finite open function variables {α : Type*} {s : set α} {a a₁ a₂ b c: α} -- TODO this lemma isn't used anywhere in this file, and should be moved elsewhere. namespace finset open finset lemma mem_range_iff_mem_finset_range_of_mod_eq [decidable_eq α] {f : ℤ → α} {a : α} {n : ℕ} (hn : 0 < n) (h : ∀i, f (i % n) = f i) : a ∈ set.range f ↔ a ∈ (finset.range n).image (λi, f i) := suffices (∃i, f (i % n) = a) ↔ ∃i, i < n ∧ f ↑i = a, by simpa [h], have hn' : 0 < (n : ℤ), from int.coe_nat_lt.mpr hn, iff.intro (assume ⟨i, hi⟩, have 0 ≤ i % ↑n, from int.mod_nonneg _ (ne_of_gt hn'), ⟨int.to_nat (i % n), by rw [←int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos i hn', hi⟩⟩) (assume ⟨i, hi, ha⟩, ⟨i, by rw [int.mod_eq_of_lt (int.coe_zero_le _) (int.coe_nat_lt_coe_nat_of_lt hi), ha]⟩) end finset lemma conj_inj [group α] {x : α} : function.injective (λ (g : α), x * g * x⁻¹) := λ a b h, by simpa [mul_left_inj, mul_right_inj] using h lemma mem_normalizer_fintype [group α] {s : set α} [fintype s] {x : α} (h : ∀ n, n ∈ s → x * n * x⁻¹ ∈ s) : x ∈ is_subgroup.normalizer s := by haveI := classical.prop_decidable; haveI := set.fintype_image s (λ n, x * n * x⁻¹); exact λ n, ⟨h n, λ h₁, have heq : (λ n, x * n * x⁻¹) '' s = s := set.eq_of_subset_of_card_le (λ n ⟨y, hy⟩, hy.2 ▸ h y hy.1) (by rw set.card_image_of_injective s conj_inj), have x * n * x⁻¹ ∈ (λ n, x * n * x⁻¹) '' s := heq.symm ▸ h₁, let ⟨y, hy⟩ := this in conj_inj hy.2 ▸ hy.1⟩ section order_of variable [group α] open quotient_group set @[simp] lemma card_trivial [fintype (is_subgroup.trivial α)] : fintype.card (is_subgroup.trivial α) = 1 := fintype.card_eq_one_iff.2 ⟨⟨(1 : α), by simp⟩, λ ⟨y, hy⟩, subtype.eq $ is_subgroup.mem_trivial.1 hy⟩ variables [fintype α] [dec : decidable_eq α] instance quotient_group.fintype (s : set α) [is_subgroup s] [d : decidable_pred s] : fintype (quotient s) := @quotient.fintype _ _ (left_rel s) (λ _ _, d _) lemma card_eq_card_quotient_mul_card_subgroup (s : set α) [hs : is_subgroup s] [fintype s] [decidable_pred s] : fintype.card α = fintype.card (quotient s) * fintype.card s := by rw ← fintype.card_prod; exact fintype.card_congr (is_subgroup.group_equiv_quotient_times_subgroup hs) lemma card_subgroup_dvd_card (s : set α) [is_subgroup s] [fintype s] : fintype.card s ∣ fintype.card α := by haveI := classical.prop_decidable; simp [card_eq_card_quotient_mul_card_subgroup s] lemma card_quotient_dvd_card (s : set α) [is_subgroup s] [decidable_pred s] [fintype s] : fintype.card (quotient s) ∣ fintype.card α := by simp [card_eq_card_quotient_mul_card_subgroup s] lemma exists_gpow_eq_one (a : α) : ∃i≠0, a ^ (i:ℤ) = 1 := have ¬ injective (λi:ℤ, a ^ i), from not_injective_infinite_fintype _, let ⟨i, j, a_eq, ne⟩ := show ∃(i j : ℤ), a ^ i = a ^ j ∧ i ≠ j, by rw [injective] at this; simpa [classical.not_forall] in have a ^ (i - j) = 1, by simp [sub_eq_add_neg, gpow_add, gpow_neg, a_eq], ⟨i - j, sub_ne_zero.mpr ne, this⟩ @[nolint ge_or_gt] -- see Note [nolint_ge] lemma exists_pow_eq_one (a : α) : ∃i > 0, a ^ i = 1 := let ⟨i, hi, eq⟩ := exists_gpow_eq_one a in begin cases i, { exact ⟨i, nat.pos_of_ne_zero (by simp [int.of_nat_eq_coe, *] at *), eq⟩ }, { exact ⟨i + 1, dec_trivial, inv_eq_one.1 eq⟩ } end include dec /-- `order_of a` is the order of the element `a`, i.e. the `n ≥ 1`, s.t. `a ^ n = 1` -/ def order_of (a : α) : ℕ := nat.find (exists_pow_eq_one a) lemma pow_order_of_eq_one (a : α) : a ^ order_of a = 1 := let ⟨h₁, h₂⟩ := nat.find_spec (exists_pow_eq_one a) in h₂ lemma order_of_pos (a : α) : 0 < order_of a := let ⟨h₁, h₂⟩ := nat.find_spec (exists_pow_eq_one a) in h₁ private lemma pow_injective_aux {n m : ℕ} (a : α) (h : n ≤ m) (hn : n < order_of a) (hm : m < order_of a) (eq : a ^ n = a ^ m) : n = m := decidable.by_contradiction $ assume ne : n ≠ m, have h₁ : m - n > 0, from nat.pos_of_ne_zero (by simp [nat.sub_eq_iff_eq_add h, ne.symm]), have h₂ : a ^ (m - n) = 1, by simp [pow_sub _ h, eq], have le : order_of a ≤ m - n, from nat.find_min' (exists_pow_eq_one a) ⟨h₁, h₂⟩, have lt : m - n < order_of a, from (nat.sub_lt_left_iff_lt_add h).mpr $ nat.lt_add_left _ _ _ hm, lt_irrefl _ (lt_of_le_of_lt le lt) lemma pow_injective_of_lt_order_of {n m : ℕ} (a : α) (hn : n < order_of a) (hm : m < order_of a) (eq : a ^ n = a ^ m) : n = m := (le_total n m).elim (assume h, pow_injective_aux a h hn hm eq) (assume h, (pow_injective_aux a h hm hn eq.symm).symm) lemma order_of_le_card_univ : order_of a ≤ fintype.card α := finset.card_le_of_inj_on ((^) a) (assume n _, fintype.complete _) (assume i j, pow_injective_of_lt_order_of a) lemma pow_eq_mod_order_of {n : ℕ} : a ^ n = a ^ (n % order_of a) := calc a ^ n = a ^ (n % order_of a + order_of a * (n / order_of a)) : by rw [nat.mod_add_div] ... = a ^ (n % order_of a) : by simp [pow_add, pow_mul, pow_order_of_eq_one] lemma gpow_eq_mod_order_of {i : ℤ} : a ^ i = a ^ (i % order_of a) := calc a ^ i = a ^ (i % order_of a + order_of a * (i / order_of a)) : by rw [int.mod_add_div] ... = a ^ (i % order_of a) : by simp [gpow_add, gpow_mul, pow_order_of_eq_one] lemma mem_gpowers_iff_mem_range_order_of {a a' : α} : a' ∈ gpowers a ↔ a' ∈ (finset.range (order_of a)).image ((^) a : ℕ → α) := finset.mem_range_iff_mem_finset_range_of_mod_eq (order_of_pos a) (assume i, gpow_eq_mod_order_of.symm) instance decidable_gpowers : decidable_pred (gpowers a) := assume a', decidable_of_iff' (a' ∈ (finset.range (order_of a)).image ((^) a)) mem_gpowers_iff_mem_range_order_of lemma order_of_dvd_of_pow_eq_one {n : ℕ} (h : a ^ n = 1) : order_of a ∣ n := by_contradiction (λ h₁, nat.find_min _ (show n % order_of a < order_of a, from nat.mod_lt _ (order_of_pos _)) ⟨nat.pos_of_ne_zero (mt nat.dvd_of_mod_eq_zero h₁), by rwa ← pow_eq_mod_order_of⟩) lemma order_of_dvd_iff_pow_eq_one {n : ℕ} : order_of a ∣ n ↔ a ^ n = 1 := ⟨λ h, by rw [pow_eq_mod_order_of, nat.mod_eq_zero_of_dvd h, pow_zero], order_of_dvd_of_pow_eq_one⟩ lemma order_of_le_of_pow_eq_one {n : ℕ} (hn : 0 < n) (h : a ^ n = 1) : order_of a ≤ n := nat.find_min' (exists_pow_eq_one a) ⟨hn, h⟩ lemma sum_card_order_of_eq_card_pow_eq_one {n : ℕ} (hn : 0 < n) : ((finset.range n.succ).filter (∣ n)).sum (λ m, (finset.univ.filter (λ a : α, order_of a = m)).card) = (finset.univ.filter (λ a : α, a ^ n = 1)).card := calc ((finset.range n.succ).filter (∣ n)).sum (λ m, (finset.univ.filter (λ a : α, order_of a = m)).card) = _ : (finset.card_bind (by { intros, apply finset.disjoint_filter.2, cc })).symm ... = _ : congr_arg finset.card (finset.ext.2 (begin assume a, suffices : order_of a ≤ n ∧ order_of a ∣ n ↔ a ^ n = 1, { simpa [nat.lt_succ_iff], }, exact ⟨λ h, let ⟨m, hm⟩ := h.2 in by rw [hm, pow_mul, pow_order_of_eq_one, _root_.one_pow], λ h, ⟨order_of_le_of_pow_eq_one hn h, order_of_dvd_of_pow_eq_one h⟩⟩ end)) section local attribute [instance] set_fintype lemma order_eq_card_gpowers : order_of a = fintype.card (gpowers a) := begin refine (finset.card_eq_of_bijective _ _ _ _).symm, { exact λn hn, ⟨gpow a n, ⟨n, rfl⟩⟩ }, { exact assume ⟨_, i, rfl⟩ _, have pos: (0:int) < order_of a, from int.coe_nat_lt.mpr $ order_of_pos a, have 0 ≤ i % (order_of a), from int.mod_nonneg _ $ ne_of_gt pos, ⟨int.to_nat (i % order_of a), by rw [← int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos _ pos, subtype.eq gpow_eq_mod_order_of.symm⟩⟩ }, { intros, exact finset.mem_univ _ }, { exact assume i j hi hj eq, pow_injective_of_lt_order_of a hi hj $ by simpa using eq } end @[simp] lemma order_of_one : order_of (1 : α) = 1 := by rw [order_eq_card_gpowers, fintype.card_eq_one_iff]; exact ⟨⟨1, 0, rfl⟩, λ ⟨a, i, ha⟩, by simp [ha.symm]⟩ @[simp] lemma order_of_eq_one_iff : order_of a = 1 ↔ a = 1 := ⟨λ h, by conv { to_lhs, rw [← pow_one a, ← h, pow_order_of_eq_one] }, λ h, by simp [h]⟩ lemma order_of_eq_prime {p : ℕ} [hp : fact p.prime] (hg : a^p = 1) (hg1 : a ≠ 1) : order_of a = p := (hp.2 _ (order_of_dvd_of_pow_eq_one hg)).resolve_left (mt order_of_eq_one_iff.1 hg1) section classical open_locale classical open quotient_group /- TODO: use cardinal theory, introduce `card : set α → ℕ`, or setup decidability for cosets -/ lemma order_of_dvd_card_univ : order_of a ∣ fintype.card α := have ft_prod : fintype (quotient (gpowers a) × (gpowers a)), from fintype.of_equiv α (gpowers.is_subgroup a).group_equiv_quotient_times_subgroup, have ft_s : fintype (gpowers a), from @fintype.fintype_prod_right _ _ _ ft_prod _, have ft_cosets : fintype (quotient (gpowers a)), from @fintype.fintype_prod_left _ _ _ ft_prod ⟨⟨1, is_submonoid.one_mem⟩⟩, have ft : fintype (quotient (gpowers a) × (gpowers a)), from @prod.fintype _ _ ft_cosets ft_s, have eq₁ : fintype.card α = @fintype.card _ ft_cosets * @fintype.card _ ft_s, from calc fintype.card α = @fintype.card _ ft_prod : @fintype.card_congr _ _ _ ft_prod (gpowers.is_subgroup a).group_equiv_quotient_times_subgroup ... = @fintype.card _ (@prod.fintype _ _ ft_cosets ft_s) : congr_arg (@fintype.card _) $ subsingleton.elim _ _ ... = @fintype.card _ ft_cosets * @fintype.card _ ft_s : @fintype.card_prod _ _ ft_cosets ft_s, have eq₂ : order_of a = @fintype.card _ ft_s, from calc order_of a = _ : order_eq_card_gpowers ... = _ : congr_arg (@fintype.card _) $ subsingleton.elim _ _, dvd.intro (@fintype.card (quotient (gpowers a)) ft_cosets) $ by rw [eq₁, eq₂, mul_comm] omit dec @[simp] lemma pow_card_eq_one (a : α) : a ^ fintype.card α = 1 := let ⟨m, hm⟩ := @order_of_dvd_card_univ _ a _ _ _ in by simp [hm, pow_mul, pow_order_of_eq_one] lemma powers_eq_gpowers (a : α) : powers a = gpowers a := set.ext (λ x, ⟨λ ⟨n, hn⟩, ⟨n, by simp * at *⟩, λ ⟨i, hi⟩, ⟨(i % order_of a).nat_abs, by rwa [← gpow_coe_nat, int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))), ← gpow_eq_mod_order_of]⟩⟩) end classical open nat lemma order_of_pow (a : α) (n : ℕ) : order_of (a ^ n) = order_of a / gcd (order_of a) n := dvd_antisymm (order_of_dvd_of_pow_eq_one (by rw [← pow_mul, ← nat.mul_div_assoc _ (gcd_dvd_left _ _), mul_comm, nat.mul_div_assoc _ (gcd_dvd_right _ _), pow_mul, pow_order_of_eq_one, _root_.one_pow])) (have gcd_pos : 0 < gcd (order_of a) n, from gcd_pos_of_pos_left n (order_of_pos a), have hdvd : order_of a ∣ n * order_of (a ^ n), from order_of_dvd_of_pow_eq_one (by rw [pow_mul, pow_order_of_eq_one]), coprime.dvd_of_dvd_mul_right (coprime_div_gcd_div_gcd gcd_pos) (dvd_of_mul_dvd_mul_right gcd_pos (by rwa [nat.div_mul_cancel (gcd_dvd_left _ _), mul_assoc, nat.div_mul_cancel (gcd_dvd_right _ _), mul_comm]))) lemma image_range_order_of (a : α) : finset.image (λ i, a ^ i) (finset.range (order_of a)) = (gpowers a).to_finset := by { ext x, rw [set.mem_to_finset, mem_gpowers_iff_mem_range_order_of] } omit dec open_locale classical lemma pow_gcd_card_eq_one_iff {n : ℕ} {a : α} : a ^ n = 1 ↔ a ^ (gcd n (fintype.card α)) = 1 := ⟨λ h, have hn : order_of a ∣ n, from dvd_of_mod_eq_zero $ by_contradiction (λ ha, by rw pow_eq_mod_order_of at h; exact (not_le_of_gt (nat.mod_lt n (order_of_pos a))) (order_of_le_of_pow_eq_one (nat.pos_of_ne_zero ha) h)), let ⟨m, hm⟩ := dvd_gcd hn order_of_dvd_card_univ in by rw [hm, pow_mul, pow_order_of_eq_one, _root_.one_pow], λ h, let ⟨m, hm⟩ := gcd_dvd_left n (fintype.card α) in by rw [hm, pow_mul, h, _root_.one_pow]⟩ end end order_of section cyclic local attribute [instance] set_fintype /-- A group is called *cyclic* if it is generated by a single element. -/ class is_cyclic (α : Type*) [group α] : Prop := (exists_generator [] : ∃ g : α, ∀ x, x ∈ gpowers g) /-- A cyclic group is always commutative. This is not an `instance` because often we have a better proof of `comm_group`. -/ def is_cyclic.comm_group [hg : group α] [is_cyclic α] : comm_group α := { mul_comm := λ x y, show x * y = y * x, from let ⟨g, hg⟩ := is_cyclic.exists_generator α in let ⟨n, hn⟩ := hg x in let ⟨m, hm⟩ := hg y in hm ▸ hn ▸ gpow_mul_comm _ _ _, ..hg } lemma is_cyclic_of_order_of_eq_card [group α] [fintype α] [decidable_eq α] (x : α) (hx : order_of x = fintype.card α) : is_cyclic α := ⟨⟨x, set.eq_univ_iff_forall.1 $ set.eq_of_subset_of_card_le (set.subset_univ _) (by rw [fintype.card_congr (equiv.set.univ α), ← hx, order_eq_card_gpowers])⟩⟩ lemma order_of_eq_card_of_forall_mem_gpowers [group α] [fintype α] [decidable_eq α] {g : α} (hx : ∀ x, x ∈ gpowers g) : order_of g = fintype.card α := by rw [← fintype.card_congr (equiv.set.univ α), order_eq_card_gpowers]; simp [hx]; congr instance [group α] : is_cyclic (is_subgroup.trivial α) := ⟨⟨(1 : is_subgroup.trivial α), λ x, ⟨0, subtype.eq $ eq.symm (is_subgroup.mem_trivial.1 x.2)⟩⟩⟩ instance is_subgroup.is_cyclic [group α] [is_cyclic α] (H : set α) [is_subgroup H] : is_cyclic H := by haveI := classical.prop_decidable; exact let ⟨g, hg⟩ := is_cyclic.exists_generator α in if hx : ∃ (x : α), x ∈ H ∧ x ≠ (1 : α) then let ⟨x, hx₁, hx₂⟩ := hx in let ⟨k, hk⟩ := hg x in have hex : ∃ n : ℕ, 0 < n ∧ g ^ n ∈ H, from ⟨k.nat_abs, nat.pos_of_ne_zero (λ h, hx₂ $ by rw [← hk, int.eq_zero_of_nat_abs_eq_zero h, gpow_zero]), match k, hk with | (k : ℕ), hk := by rw [int.nat_abs_of_nat, ← gpow_coe_nat, hk]; exact hx₁ | -[1+ k], hk := by rw [int.nat_abs_of_neg_succ_of_nat, ← is_subgroup.inv_mem_iff H]; simp * at * end⟩, ⟨⟨⟨g ^ nat.find hex, (nat.find_spec hex).2⟩, λ ⟨x, hx⟩, let ⟨k, hk⟩ := hg x in have hk₁ : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) ∈ gpowers (g ^ nat.find hex), from ⟨k / nat.find hex, eq.symm $ gpow_mul _ _ _⟩, have hk₂ : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) ∈ H, by rw gpow_mul; exact is_subgroup.gpow_mem (nat.find_spec hex).2, have hk₃ : g ^ (k % nat.find hex) ∈ H, from (is_subgroup.mul_mem_cancel_right H hk₂).1 $ by rw [← gpow_add, int.mod_add_div, hk]; exact hx, have hk₄ : k % nat.find hex = (k % nat.find hex).nat_abs, by rw int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (nat.find_spec hex).1)), have hk₅ : g ^ (k % nat.find hex ).nat_abs ∈ H, by rwa [← gpow_coe_nat, ← hk₄], have hk₆ : (k % (nat.find hex : ℤ)).nat_abs = 0, from by_contradiction (λ h, nat.find_min hex (int.coe_nat_lt.1 $ by rw [← hk₄]; exact int.mod_lt_of_pos _ (int.coe_nat_pos.2 (nat.find_spec hex).1)) ⟨nat.pos_of_ne_zero h, hk₅⟩), ⟨k / (nat.find hex : ℤ), subtype.coe_ext.2 begin suffices : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) = x, { simpa [gpow_mul] }, rw [int.mul_div_cancel' (int.dvd_of_mod_eq_zero (int.eq_zero_of_nat_abs_eq_zero hk₆)), hk] end⟩⟩⟩ else have H = is_subgroup.trivial α, from set.ext $ λ x, ⟨λ h, by simp at *; tauto, λ h, by rw [is_subgroup.mem_trivial.1 h]; exact is_submonoid.one_mem⟩, by clear _let_match; subst this; apply_instance open finset nat lemma is_cyclic.card_pow_eq_one_le [group α] [fintype α] [decidable_eq α] [is_cyclic α] {n : ℕ} (hn0 : 0 < n) : (univ.filter (λ a : α, a ^ n = 1)).card ≤ n := let ⟨g, hg⟩ := is_cyclic.exists_generator α in calc (univ.filter (λ a : α, a ^ n = 1)).card ≤ (gpowers (g ^ (fintype.card α / (gcd n (fintype.card α))))).to_finset.card : card_le_of_subset (λ x hx, let ⟨m, hm⟩ := show x ∈ powers g, from (powers_eq_gpowers g).symm ▸ hg x in set.mem_to_finset.2 ⟨(m / (fintype.card α / (gcd n (fintype.card α))) : ℕ), have hgmn : g ^ (m * gcd n (fintype.card α)) = 1, by rw [pow_mul, hm, ← pow_gcd_card_eq_one_iff]; exact (mem_filter.1 hx).2, begin rw [gpow_coe_nat, ← pow_mul, nat.mul_div_cancel_left', hm], refine dvd_of_mul_dvd_mul_right (gcd_pos_of_pos_left (fintype.card α) hn0) _, conv {to_lhs, rw [nat.div_mul_cancel (gcd_dvd_right _ _), ← order_of_eq_card_of_forall_mem_gpowers hg]}, exact order_of_dvd_of_pow_eq_one hgmn end⟩) ... ≤ n : let ⟨m, hm⟩ := gcd_dvd_right n (fintype.card α) in have hm0 : 0 < m, from nat.pos_of_ne_zero (λ hm0, (by rw [hm0, mul_zero, fintype.card_eq_zero_iff] at hm; exact hm 1)), begin rw [← fintype.card_of_finset' _ (λ _, set.mem_to_finset), ← order_eq_card_gpowers, order_of_pow, order_of_eq_card_of_forall_mem_gpowers hg], rw [hm] {occs := occurrences.pos [2,3]}, rw [nat.mul_div_cancel_left _ (gcd_pos_of_pos_left _ hn0), gcd_mul_left_left, hm, nat.mul_div_cancel _ hm0], exact le_of_dvd hn0 (gcd_dvd_left _ _) end lemma is_cyclic.exists_monoid_generator (α : Type*) [group α] [fintype α] [is_cyclic α] : ∃ x : α, ∀ y : α, y ∈ powers x := by simp only [powers_eq_gpowers]; exact is_cyclic.exists_generator α section variables [group α] [fintype α] [decidable_eq α] lemma is_cyclic.image_range_order_of (ha : ∀ x : α, x ∈ gpowers a) : finset.image (λ i, a ^ i) (range (order_of a)) = univ := begin simp only [image_range_order_of, set.eq_univ_iff_forall.mpr ha], convert set.to_finset_univ end lemma is_cyclic.image_range_card (ha : ∀ x : α, x ∈ gpowers a) : finset.image (λ i, a ^ i) (range (fintype.card α)) = univ := by rw [← order_of_eq_card_of_forall_mem_gpowers ha, is_cyclic.image_range_order_of ha] end section totient variables [group α] [fintype α] [decidable_eq α] (hn : ∀ n : ℕ, 0 < n → (univ.filter (λ a : α, a ^ n = 1)).card ≤ n) include hn lemma card_pow_eq_one_eq_order_of_aux (a : α) : (finset.univ.filter (λ b : α, b ^ order_of a = 1)).card = order_of a := le_antisymm (hn _ (order_of_pos _)) (calc order_of a = @fintype.card (gpowers a) (id _) : order_eq_card_gpowers ... ≤ @fintype.card (↑(univ.filter (λ b : α, b ^ order_of a = 1)) : set α) (fintype.of_finset _ (λ _, iff.rfl)) : @fintype.card_le_of_injective (gpowers a) (↑(univ.filter (λ b : α, b ^ order_of a = 1)) : set α) (id _) (id _) (λ b, ⟨b.1, mem_filter.2 ⟨mem_univ _, let ⟨i, hi⟩ := b.2 in by rw [← hi, ← gpow_coe_nat, ← gpow_mul, mul_comm, gpow_mul, gpow_coe_nat, pow_order_of_eq_one, one_gpow]⟩⟩) (λ _ _ h, subtype.eq (subtype.mk.inj h)) ... = (univ.filter (λ b : α, b ^ order_of a = 1)).card : fintype.card_of_finset _ _) open_locale nat -- use φ for nat.totient private lemma card_order_of_eq_totient_aux₁ : ∀ {d : ℕ}, d ∣ fintype.card α → 0 < (univ.filter (λ a : α, order_of a = d)).card → (univ.filter (λ a : α, order_of a = d)).card = φ d | 0 := λ hd hd0, let ⟨a, ha⟩ := card_pos.1 hd0 in absurd (mem_filter.1 ha).2 $ ne_of_gt $ order_of_pos a | (d+1) := λ hd hd0, let ⟨a, ha⟩ := card_pos.1 hd0 in have ha : order_of a = d.succ, from (mem_filter.1 ha).2, have h : ((range d.succ).filter (∣ d.succ)).sum (λ m, (univ.filter (λ a : α, order_of a = m)).card) = ((range d.succ).filter (∣ d.succ)).sum φ, from finset.sum_congr rfl (λ m hm, have hmd : m < d.succ, from mem_range.1 (mem_filter.1 hm).1, have hm : m ∣ d.succ, from (mem_filter.1 hm).2, card_order_of_eq_totient_aux₁ (dvd.trans hm hd) (finset.card_pos.2 ⟨a ^ (d.succ / m), mem_filter.2 ⟨mem_univ _, by rw [order_of_pow, ha, gcd_eq_right (div_dvd_of_dvd hm), nat.div_div_self hm (succ_pos _)]⟩⟩)), have hinsert : insert d.succ ((range d.succ).filter (∣ d.succ)) = (range d.succ.succ).filter (∣ d.succ), from (finset.ext.2 $ λ x, ⟨λ h, (mem_insert.1 h).elim (λ h, by simp [h, range_succ]) (by clear _let_match; simp [range_succ]; tauto), by clear _let_match; simp [range_succ] {contextual := tt}; tauto⟩), have hinsert₁ : d.succ ∉ (range d.succ).filter (∣ d.succ), by simp [mem_range, zero_le_one, le_succ], (add_left_inj (((range d.succ).filter (∣ d.succ)).sum (λ m, (univ.filter (λ a : α, order_of a = m)).card))).1 (calc _ = (insert d.succ (filter (∣ d.succ) (range d.succ))).sum (λ m, (univ.filter (λ a : α, order_of a = m)).card) : eq.symm (finset.sum_insert (by simp [mem_range, zero_le_one, le_succ])) ... = ((range d.succ.succ).filter (∣ d.succ)).sum (λ m, (univ.filter (λ a : α, order_of a = m)).card) : sum_congr hinsert (λ _ _, rfl) ... = (univ.filter (λ a : α, a ^ d.succ = 1)).card : sum_card_order_of_eq_card_pow_eq_one (succ_pos d) ... = ((range d.succ.succ).filter (∣ d.succ)).sum φ : ha ▸ (card_pow_eq_one_eq_order_of_aux hn a).symm ▸ (sum_totient _).symm ... = _ : by rw [h, ← sum_insert hinsert₁]; exact finset.sum_congr hinsert.symm (λ _ _, rfl)) lemma card_order_of_eq_totient_aux₂ {d : ℕ} (hd : d ∣ fintype.card α) : (univ.filter (λ a : α, order_of a = d)).card = φ d := by_contradiction $ λ h, have h0 : (univ.filter (λ a : α , order_of a = d)).card = 0 := not_not.1 (mt nat.pos_iff_ne_zero.2 (mt (card_order_of_eq_totient_aux₁ hn hd) h)), let c := fintype.card α in have hc0 : 0 < c, from fintype.card_pos_iff.2 ⟨1⟩, lt_irrefl c $ calc c = (univ.filter (λ a : α, a ^ c = 1)).card : congr_arg card $ by simp [finset.ext, c] ... = ((range c.succ).filter (∣ c)).sum (λ m, (univ.filter (λ a : α, order_of a = m)).card) : (sum_card_order_of_eq_card_pow_eq_one hc0).symm ... = (((range c.succ).filter (∣ c)).erase d).sum (λ m, (univ.filter (λ a : α, order_of a = m)).card) : eq.symm (sum_subset (erase_subset _ _) (λ m hm₁ hm₂, have m = d, by simp at *; cc, by simp [*, finset.ext] at *; exact h0)) ... ≤ (((range c.succ).filter (∣ c)).erase d).sum φ : sum_le_sum (λ m hm, have hmc : m ∣ c, by simp at hm; tauto, (imp_iff_not_or.1 (card_order_of_eq_totient_aux₁ hn hmc)).elim (λ h, by simp [nat.le_zero_iff.1 (le_of_not_gt h), nat.zero_le]) (by { intro h, rw h, apply le_refl })) ... < φ d + (((range c.succ).filter (∣ c)).erase d).sum φ : lt_add_of_pos_left _ (totient_pos (nat.pos_of_ne_zero (λ h, nat.pos_iff_ne_zero.1 hc0 (eq_zero_of_zero_dvd $ h ▸ hd)))) ... = (insert d (((range c.succ).filter (∣ c)).erase d)).sum φ : eq.symm (sum_insert (by simp)) ... = ((range c.succ).filter (∣ c)).sum φ : finset.sum_congr (finset.insert_erase (mem_filter.2 ⟨mem_range.2 (lt_succ_of_le (le_of_dvd hc0 hd)), hd⟩)) (λ _ _, rfl) ... = c : sum_totient _ lemma is_cyclic_of_card_pow_eq_one_le : is_cyclic α := have (univ.filter (λ a : α, order_of a = fintype.card α)).nonempty, from (card_pos.1 $ by rw [card_order_of_eq_totient_aux₂ hn (dvd_refl _)]; exact totient_pos (fintype.card_pos_iff.2 ⟨1⟩)), let ⟨x, hx⟩ := this in is_cyclic_of_order_of_eq_card x (finset.mem_filter.1 hx).2 end totient lemma is_cyclic.card_order_of_eq_totient [group α] [is_cyclic α] [fintype α] [decidable_eq α] {d : ℕ} (hd : d ∣ fintype.card α) : (univ.filter (λ a : α, order_of a = d)).card = totient d := card_order_of_eq_totient_aux₂ (λ n, is_cyclic.card_pow_eq_one_le) hd end cyclic
e4828122c75a5c1d6be64c94bc6e432da880e104
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/data/pnat/basic.lean
89f01acb723e1fdc7d3c9c56fdbf316f746cd8bd
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,067
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Neil Strickland -/ import algebra.group_power.basic /-! # The positive natural numbers This file defines the type `ℕ+` or `pnat`, the subtype of natural numbers that are positive. -/ /-- `ℕ+` is the type of positive natural numbers. It is defined as a subtype, and the VM representation of `ℕ+` is the same as `ℕ` because the proof is not stored. -/ def pnat := {n : ℕ // 0 < n} notation `ℕ+` := pnat instance coe_pnat_nat : has_coe ℕ+ ℕ := ⟨subtype.val⟩ instance : has_repr ℕ+ := ⟨λ n, repr n.1⟩ /-- Predecessor of a `ℕ+`, as a `ℕ`. -/ def pnat.nat_pred (i : ℕ+) : ℕ := i - 1 namespace nat /-- Convert a natural number to a positive natural number. The positivity assumption is inferred by `dec_trivial`. -/ def to_pnat (n : ℕ) (h : 0 < n . tactic.exact_dec_trivial) : ℕ+ := ⟨n, h⟩ /-- Write a successor as an element of `ℕ+`. -/ def succ_pnat (n : ℕ) : ℕ+ := ⟨succ n, succ_pos n⟩ @[simp] theorem succ_pnat_coe (n : ℕ) : (succ_pnat n : ℕ) = succ n := rfl theorem succ_pnat_inj {n m : ℕ} : succ_pnat n = succ_pnat m → n = m := λ h, by { let h' := congr_arg (coe : ℕ+ → ℕ) h, exact nat.succ.inj h' } /-- Convert a natural number to a pnat. `n+1` is mapped to itself, and `0` becomes `1`. -/ def to_pnat' (n : ℕ) : ℕ+ := succ_pnat (pred n) @[simp] theorem to_pnat'_coe : ∀ (n : ℕ), ((to_pnat' n) : ℕ) = ite (0 < n) n 1 | 0 := rfl | (m + 1) := by {rw [if_pos (succ_pos m)], refl} end nat namespace pnat open nat /-- We now define a long list of structures on ℕ+ induced by similar structures on ℕ. Most of these behave in a completely obvious way, but there are a few things to be said about subtraction, division and powers. -/ instance : decidable_eq ℕ+ := λ (a b : ℕ+), by apply_instance instance : linear_order ℕ+ := subtype.linear_order _ @[simp] lemma mk_le_mk (n k : ℕ) (hn : 0 < n) (hk : 0 < k) : (⟨n, hn⟩ : ℕ+) ≤ ⟨k, hk⟩ ↔ n ≤ k := iff.rfl @[simp] lemma mk_lt_mk (n k : ℕ) (hn : 0 < n) (hk : 0 < k) : (⟨n, hn⟩ : ℕ+) < ⟨k, hk⟩ ↔ n < k := iff.rfl @[simp, norm_cast] lemma coe_le_coe (n k : ℕ+) : (n : ℕ) ≤ k ↔ n ≤ k := iff.rfl @[simp, norm_cast] lemma coe_lt_coe (n k : ℕ+) : (n : ℕ) < k ↔ n < k := iff.rfl @[simp] theorem pos (n : ℕ+) : 0 < (n : ℕ) := n.2 theorem eq {m n : ℕ+} : (m : ℕ) = n → m = n := subtype.eq @[simp] lemma coe_inj {m n : ℕ+} : (m : ℕ) = n ↔ m = n := set_coe.ext_iff lemma coe_injective : function.injective (coe : ℕ+ → ℕ) := subtype.coe_injective @[simp] theorem mk_coe (n h) : ((⟨n, h⟩ : ℕ+) : ℕ) = n := rfl instance : has_add ℕ+ := ⟨λ a b, ⟨(a + b : ℕ), add_pos a.pos b.pos⟩⟩ instance : add_comm_semigroup ℕ+ := coe_injective.add_comm_semigroup coe (λ _ _, rfl) @[simp] theorem add_coe (m n : ℕ+) : ((m + n : ℕ+) : ℕ) = m + n := rfl /-- `pnat.coe` promoted to an `add_hom`, that is, a morphism which preserves addition. -/ def coe_add_hom : add_hom ℕ+ ℕ := { to_fun := coe, map_add' := add_coe } instance : add_left_cancel_semigroup ℕ+ := coe_injective.add_left_cancel_semigroup coe (λ _ _, rfl) instance : add_right_cancel_semigroup ℕ+ := coe_injective.add_right_cancel_semigroup coe (λ _ _, rfl) @[simp] theorem ne_zero (n : ℕ+) : (n : ℕ) ≠ 0 := n.2.ne' theorem to_pnat'_coe {n : ℕ} : 0 < n → (n.to_pnat' : ℕ) = n := succ_pred_eq_of_pos @[simp] theorem coe_to_pnat' (n : ℕ+) : (n : ℕ).to_pnat' = n := eq (to_pnat'_coe n.pos) instance : has_mul ℕ+ := ⟨λ m n, ⟨m.1 * n.1, mul_pos m.2 n.2⟩⟩ instance : has_one ℕ+ := ⟨succ_pnat 0⟩ instance : comm_monoid ℕ+ := coe_injective.comm_monoid coe rfl (λ _ _, rfl) theorem lt_add_one_iff : ∀ {a b : ℕ+}, a < b + 1 ↔ a ≤ b := λ a b, nat.lt_add_one_iff theorem add_one_le_iff : ∀ {a b : ℕ+}, a + 1 ≤ b ↔ a < b := λ a b, nat.add_one_le_iff @[simp] lemma one_le (n : ℕ+) : (1 : ℕ+) ≤ n := n.2 instance : order_bot ℕ+ := { bot := 1, bot_le := λ a, a.property, .. pnat.linear_order } @[simp] lemma bot_eq_one : (⊥ : ℕ+) = 1 := rfl instance : inhabited ℕ+ := ⟨1⟩ -- Some lemmas that rewrite `pnat.mk n h`, for `n` an explicit numeral, into explicit numerals. @[simp] lemma mk_one {h} : (⟨1, h⟩ : ℕ+) = (1 : ℕ+) := rfl @[simp] lemma mk_bit0 (n) {h} : (⟨bit0 n, h⟩ : ℕ+) = (bit0 ⟨n, pos_of_bit0_pos h⟩ : ℕ+) := rfl @[simp] lemma mk_bit1 (n) {h} {k} : (⟨bit1 n, h⟩ : ℕ+) = (bit1 ⟨n, k⟩ : ℕ+) := rfl -- Some lemmas that rewrite inequalities between explicit numerals in `ℕ+` -- into the corresponding inequalities in `ℕ`. -- TODO: perhaps this should not be attempted by `simp`, -- and instead we should expect `norm_num` to take care of these directly? -- TODO: these lemmas are perhaps incomplete: -- * 1 is not represented as a bit0 or bit1 -- * strict inequalities? @[simp] lemma bit0_le_bit0 (n m : ℕ+) : (bit0 n) ≤ (bit0 m) ↔ (bit0 (n : ℕ)) ≤ (bit0 (m : ℕ)) := iff.rfl @[simp] lemma bit0_le_bit1 (n m : ℕ+) : (bit0 n) ≤ (bit1 m) ↔ (bit0 (n : ℕ)) ≤ (bit1 (m : ℕ)) := iff.rfl @[simp] lemma bit1_le_bit0 (n m : ℕ+) : (bit1 n) ≤ (bit0 m) ↔ (bit1 (n : ℕ)) ≤ (bit0 (m : ℕ)) := iff.rfl @[simp] lemma bit1_le_bit1 (n m : ℕ+) : (bit1 n) ≤ (bit1 m) ↔ (bit1 (n : ℕ)) ≤ (bit1 (m : ℕ)) := iff.rfl @[simp] theorem one_coe : ((1 : ℕ+) : ℕ) = 1 := rfl @[simp] theorem mul_coe (m n : ℕ+) : ((m * n : ℕ+) : ℕ) = m * n := rfl /-- `pnat.coe` promoted to a `monoid_hom`. -/ def coe_monoid_hom : ℕ+ →* ℕ := { to_fun := coe, map_one' := one_coe, map_mul' := mul_coe } @[simp] lemma coe_coe_monoid_hom : (coe_monoid_hom : ℕ+ → ℕ) = coe := rfl @[simp] lemma coe_eq_one_iff {m : ℕ+} : (m : ℕ) = 1 ↔ m = 1 := by { split; intro h; try { apply pnat.eq}; rw h; simp } @[simp] lemma coe_bit0 (a : ℕ+) : ((bit0 a : ℕ+) : ℕ) = bit0 (a : ℕ) := rfl @[simp] lemma coe_bit1 (a : ℕ+) : ((bit1 a : ℕ+) : ℕ) = bit1 (a : ℕ) := rfl @[simp] theorem pow_coe (m : ℕ+) (n : ℕ) : ((m ^ n : ℕ+) : ℕ) = (m : ℕ) ^ n := by induction n with n ih; [refl, rw [pow_succ', pow_succ, mul_coe, mul_comm, ih]] instance : ordered_cancel_comm_monoid ℕ+ := { mul_le_mul_left := by { intros, apply nat.mul_le_mul_left, assumption }, le_of_mul_le_mul_left := by { intros a b c h, apply nat.le_of_mul_le_mul_left h a.property, }, mul_left_cancel := λ a b c h, by { replace h := congr_arg (coe : ℕ+ → ℕ) h, exact eq ((nat.mul_right_inj a.pos).mp h)}, .. pnat.comm_monoid, .. pnat.linear_order } instance : distrib ℕ+ := coe_injective.distrib coe (λ _ _, rfl) (λ _ _, rfl) /-- Subtraction a - b is defined in the obvious way when a > b, and by a - b = 1 if a ≤ b. -/ instance : has_sub ℕ+ := ⟨λ a b, to_pnat' (a - b : ℕ)⟩ theorem sub_coe (a b : ℕ+) : ((a - b : ℕ+) : ℕ) = ite (b < a) (a - b : ℕ) 1 := begin change ((to_pnat' ((a : ℕ) - (b : ℕ)) : ℕ)) = ite ((a : ℕ) > (b : ℕ)) ((a : ℕ) - (b : ℕ)) 1, split_ifs with h, { exact to_pnat'_coe (tsub_pos_of_lt h) }, { rw [tsub_eq_zero_iff_le.mpr (le_of_not_gt h)], refl } end theorem add_sub_of_lt {a b : ℕ+} : a < b → a + (b - a) = b := λ h, eq $ by { rw [add_coe, sub_coe, if_pos h], exact add_tsub_cancel_of_le h.le } instance : has_well_founded ℕ+ := ⟨(<), measure_wf coe⟩ /-- Strong induction on `ℕ+`. -/ def strong_induction_on {p : ℕ+ → Sort*} : ∀ (n : ℕ+) (h : ∀ k, (∀ m, m < k → p m) → p k), p n | n := λ IH, IH _ (λ a h, strong_induction_on a IH) using_well_founded { dec_tac := `[assumption] } /-- If `n : ℕ+` is different from `1`, then it is the successor of some `k : ℕ+`. -/ lemma exists_eq_succ_of_ne_one : ∀ {n : ℕ+} (h1 : n ≠ 1), ∃ (k : ℕ+), n = k + 1 | ⟨1, _⟩ h1 := false.elim $ h1 rfl | ⟨n+2, _⟩ _ := ⟨⟨n+1, by simp⟩, rfl⟩ /-- Strong induction on `ℕ+`, with `n = 1` treated separately. -/ def case_strong_induction_on {p : ℕ+ → Sort*} (a : ℕ+) (hz : p 1) (hi : ∀ n, (∀ m, m ≤ n → p m) → p (n + 1)) : p a := begin apply strong_induction_on a, rintro ⟨k, kprop⟩ hk, cases k with k, { exact (lt_irrefl 0 kprop).elim }, cases k with k, { exact hz }, exact hi ⟨k.succ, nat.succ_pos _⟩ (λ m hm, hk _ (lt_succ_iff.2 hm)), end /-- An induction principle for `ℕ+`: it takes values in `Sort*`, so it applies also to Types, not only to `Prop`. -/ @[elab_as_eliminator] def rec_on (n : ℕ+) {p : ℕ+ → Sort*} (p1 : p 1) (hp : ∀ n, p n → p (n + 1)) : p n := begin rcases n with ⟨n, h⟩, induction n with n IH, { exact absurd h dec_trivial }, { cases n with n, { exact p1 }, { exact hp _ (IH n.succ_pos) } } end @[simp] theorem rec_on_one {p} (p1 hp) : @pnat.rec_on 1 p p1 hp = p1 := rfl @[simp] theorem rec_on_succ (n : ℕ+) {p : ℕ+ → Sort*} (p1 hp) : @pnat.rec_on (n + 1) p p1 hp = hp n (@pnat.rec_on n p p1 hp) := by { cases n with n h, cases n; [exact absurd h dec_trivial, refl] } /-- We define `m % k` and `m / k` in the same way as for `ℕ` except that when `m = n * k` we take `m % k = k` and `m / k = n - 1`. This ensures that `m % k` is always positive and `m = (m % k) + k * (m / k)` in all cases. Later we define a function `div_exact` which gives the usual `m / k` in the case where `k` divides `m`. -/ def mod_div_aux : ℕ+ → ℕ → ℕ → ℕ+ × ℕ | k 0 q := ⟨k, q.pred⟩ | k (r + 1) q := ⟨⟨r + 1, nat.succ_pos r⟩, q⟩ lemma mod_div_aux_spec : ∀ (k : ℕ+) (r q : ℕ) (h : ¬ (r = 0 ∧ q = 0)), (((mod_div_aux k r q).1 : ℕ) + k * (mod_div_aux k r q).2 = (r + k * q)) | k 0 0 h := (h ⟨rfl, rfl⟩).elim | k 0 (q + 1) h := by { change (k : ℕ) + (k : ℕ) * (q + 1).pred = 0 + (k : ℕ) * (q + 1), rw [nat.pred_succ, nat.mul_succ, zero_add, add_comm]} | k (r + 1) q h := rfl /-- `mod_div m k = (m % k, m / k)`. We define `m % k` and `m / k` in the same way as for `ℕ` except that when `m = n * k` we take `m % k = k` and `m / k = n - 1`. This ensures that `m % k` is always positive and `m = (m % k) + k * (m / k)` in all cases. Later we define a function `div_exact` which gives the usual `m / k` in the case where `k` divides `m`. -/ def mod_div (m k : ℕ+) : ℕ+ × ℕ := mod_div_aux k ((m : ℕ) % (k : ℕ)) ((m : ℕ) / (k : ℕ)) /-- We define `m % k` in the same way as for `ℕ` except that when `m = n * k` we take `m % k = k` This ensures that `m % k` is always positive. -/ def mod (m k : ℕ+) : ℕ+ := (mod_div m k).1 /-- We define `m / k` in the same way as for `ℕ` except that when `m = n * k` we take `m / k = n - 1`. This ensures that `m = (m % k) + k * (m / k)` in all cases. Later we define a function `div_exact` which gives the usual `m / k` in the case where `k` divides `m`. -/ def div (m k : ℕ+) : ℕ := (mod_div m k).2 theorem mod_add_div (m k : ℕ+) : ((mod m k) + k * (div m k) : ℕ) = m := begin let h₀ := nat.mod_add_div (m : ℕ) (k : ℕ), have : ¬ ((m : ℕ) % (k : ℕ) = 0 ∧ (m : ℕ) / (k : ℕ) = 0), by { rintro ⟨hr, hq⟩, rw [hr, hq, mul_zero, zero_add] at h₀, exact (m.ne_zero h₀.symm).elim }, have := mod_div_aux_spec k ((m : ℕ) % (k : ℕ)) ((m : ℕ) / (k : ℕ)) this, exact (this.trans h₀), end theorem div_add_mod (m k : ℕ+) : (k * (div m k) + mod m k : ℕ) = m := (add_comm _ _).trans (mod_add_div _ _) lemma mod_add_div' (m k : ℕ+) : ((mod m k) + (div m k) * k : ℕ) = m := by { rw mul_comm, exact mod_add_div _ _ } lemma div_add_mod' (m k : ℕ+) : ((div m k) * k + mod m k : ℕ) = m := by { rw mul_comm, exact div_add_mod _ _ } theorem mod_coe (m k : ℕ+) : ((mod m k) : ℕ) = ite ((m : ℕ) % (k : ℕ) = 0) (k : ℕ) ((m : ℕ) % (k : ℕ)) := begin dsimp [mod, mod_div], cases (m : ℕ) % (k : ℕ), { rw [if_pos rfl], refl }, { rw [if_neg n.succ_ne_zero], refl } end theorem div_coe (m k : ℕ+) : ((div m k) : ℕ) = ite ((m : ℕ) % (k : ℕ) = 0) ((m : ℕ) / (k : ℕ)).pred ((m : ℕ) / (k : ℕ)) := begin dsimp [div, mod_div], cases (m : ℕ) % (k : ℕ), { rw [if_pos rfl], refl }, { rw [if_neg n.succ_ne_zero], refl } end theorem mod_le (m k : ℕ+) : mod m k ≤ m ∧ mod m k ≤ k := begin change ((mod m k) : ℕ) ≤ (m : ℕ) ∧ ((mod m k) : ℕ) ≤ (k : ℕ), rw [mod_coe], split_ifs, { have hm : (m : ℕ) > 0 := m.pos, rw [← nat.mod_add_div (m : ℕ) (k : ℕ), h, zero_add] at hm ⊢, by_cases h' : ((m : ℕ) / (k : ℕ)) = 0, { rw [h', mul_zero] at hm, exact (lt_irrefl _ hm).elim}, { let h' := nat.mul_le_mul_left (k : ℕ) (nat.succ_le_of_lt (nat.pos_of_ne_zero h')), rw [mul_one] at h', exact ⟨h', le_refl (k : ℕ)⟩ } }, { exact ⟨nat.mod_le (m : ℕ) (k : ℕ), (nat.mod_lt (m : ℕ) k.pos).le⟩ } end theorem dvd_iff {k m : ℕ+} : k ∣ m ↔ (k : ℕ) ∣ (m : ℕ) := begin split; intro h, rcases h with ⟨_, rfl⟩, apply dvd_mul_right, rcases h with ⟨a, h⟩, cases a, { contrapose h, apply ne_zero, }, use a.succ, apply nat.succ_pos, rw [← coe_inj, h, mul_coe, mk_coe], end theorem dvd_iff' {k m : ℕ+} : k ∣ m ↔ mod m k = k := begin rw dvd_iff, rw [nat.dvd_iff_mod_eq_zero], split, { intro h, apply eq, rw [mod_coe, if_pos h] }, { intro h, by_cases h' : (m : ℕ) % (k : ℕ) = 0, { exact h'}, { replace h : ((mod m k) : ℕ) = (k : ℕ) := congr_arg _ h, rw [mod_coe, if_neg h'] at h, exact ((nat.mod_lt (m : ℕ) k.pos).ne h).elim } } end lemma le_of_dvd {m n : ℕ+} : m ∣ n → m ≤ n := by { rw dvd_iff', intro h, rw ← h, apply (mod_le n m).left } /-- If `h : k | m`, then `k * (div_exact m k) = m`. Note that this is not equal to `m / k`. -/ def div_exact (m k : ℕ+) : ℕ+ := ⟨(div m k).succ, nat.succ_pos _⟩ theorem mul_div_exact {m k : ℕ+} (h : k ∣ m) : k * (div_exact m k) = m := begin apply eq, rw [mul_coe], change (k : ℕ) * (div m k).succ = m, rw [← div_add_mod m k, dvd_iff'.mp h, nat.mul_succ] end theorem dvd_antisymm {m n : ℕ+} : m ∣ n → n ∣ m → m = n := λ hmn hnm, (le_of_dvd hmn).antisymm (le_of_dvd hnm) theorem dvd_one_iff (n : ℕ+) : n ∣ 1 ↔ n = 1 := ⟨λ h, dvd_antisymm h (one_dvd n), λ h, h.symm ▸ (dvd_refl 1)⟩ lemma pos_of_div_pos {n : ℕ+} {a : ℕ} (h : a ∣ n) : 0 < a := begin apply pos_iff_ne_zero.2, intro hzero, rw hzero at h, exact pnat.ne_zero n (eq_zero_of_zero_dvd h) end end pnat section can_lift instance nat.can_lift_pnat : can_lift ℕ ℕ+ := ⟨coe, λ n, 0 < n, λ n hn, ⟨nat.to_pnat' n, pnat.to_pnat'_coe hn⟩⟩ instance int.can_lift_pnat : can_lift ℤ ℕ+ := ⟨coe, λ n, 0 < n, λ n hn, ⟨nat.to_pnat' (int.nat_abs n), by rw [coe_coe, nat.to_pnat'_coe, if_pos (int.nat_abs_pos_of_ne_zero hn.ne'), int.nat_abs_of_nonneg hn.le]⟩⟩ end can_lift
0ad672fec854258c65d099192173d0afce5cf59c
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Lean/Elab/Tactic/Conv/Rewrite.lean
39861871c2d22503c6fd099cad9467a21a939134
[ "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
781
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Tactic.Rewrite import Lean.Elab.Tactic.Rewrite import Lean.Elab.Tactic.Conv.Basic namespace Lean.Elab.Tactic.Conv open Meta @[builtinTactic Lean.Parser.Tactic.Conv.rewrite] def evalRewrite : Tactic := fun stx => do let config ← Tactic.elabRewriteConfig stx[1] withRWRulesSeq stx[0] stx[2] fun symm term => do Term.withSynthesize <| withMainContext do let e ← elabTerm term none true let r ← (← getMainGoal).rewrite (← getLhs) e symm (config := config) updateLhs r.eNew r.eqProof replaceMainGoal ((← getMainGoal) :: r.mvarIds) end Lean.Elab.Tactic.Conv
bca5213117119db568e355c1cbac1eb96e654c59
acc85b4be2c618b11fc7cb3005521ae6858a8d07
/logic/basic.lean
ca0415ed48c7c3d47a20665f9d8235c085b5ddff
[ "Apache-2.0" ]
permissive
linpingchuan/mathlib
d49990b236574df2a45d9919ba43c923f693d341
5ad8020f67eb13896a41cc7691d072c9331b1f76
refs/heads/master
1,626,019,377,808
1,508,048,784,000
1,508,048,784,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
18,343
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura Theorems that require decidability hypotheses are in the namespace "decidable". Classical versions are in the namespace "classical". Note: in the presence of automation, this whole file may be unnecessary. On the other hand, maybe it is useful for writing automation. -/ /- miscellany TODO: move elsewhere -/ section miscellany variables {α : Type*} {β : Type*} theorem eq_iff_le_and_le [partial_order α] {a b : α} : a = b ↔ (a ≤ b ∧ b ≤ a) := ⟨assume eq, eq ▸ ⟨le_refl a, le_refl a⟩, assume ⟨ab, ba⟩, le_antisymm ab ba⟩ @[simp] theorem prod.mk.inj_iff {a₁ a₂ : α} {b₁ b₂ : β} : (a₁, b₁) = (a₂, b₂) ↔ (a₁ = a₂ ∧ b₁ = b₂) := ⟨prod.mk.inj, by cc⟩ @[simp] theorem prod.forall {p : α × β → Prop} : (∀ x, p x) ↔ (∀ a b, p (a, b)) := ⟨assume h a b, h (a, b), assume h ⟨a, b⟩, h a b⟩ @[simp] theorem prod.exists {p : α × β → Prop} : (∃ x, p x) ↔ (∃ a b, p (a, b)) := ⟨assume ⟨⟨a, b⟩, h⟩, ⟨a, b, h⟩, assume ⟨a, b, h⟩, ⟨⟨a, b⟩, h⟩⟩ @[simp] theorem set_of_subset_set_of {p q : α → Prop} : {a | p a} ⊆ {a | q a} = (∀a, p a → q a) := rfl @[simp] theorem sigma.mk.inj_iff {β : α → Type*} {a₁ a₂ : α} {b₁ : β a₁} {b₂ : β a₂} : sigma.mk a₁ b₁ = ⟨a₂, b₂⟩ ↔ (a₁ = a₂ ∧ b₁ == b₂) := ⟨sigma.mk.inj, λ ⟨h₁, h₂⟩, by congr; assumption⟩ @[simp] theorem sigma.forall {β : α → Type*} {p : (Σ a, β a) → Prop} : (∀ x, p x) ↔ (∀ a b, p ⟨a, b⟩) := ⟨assume h a b, h ⟨a, b⟩, assume h ⟨a, b⟩, h a b⟩ @[simp] theorem sigma.exists {β : α → Type*} {p : (Σ a, β a) → Prop} : (∃ x, p x) ↔ (∃ a b, p ⟨a, b⟩) := ⟨assume ⟨⟨a, b⟩, h⟩, ⟨a, b, h⟩, assume ⟨a, b, h⟩, ⟨⟨a, b⟩, h⟩⟩ @[simp] theorem subtype.forall {β : α → Prop} {p : {a // β a} → Prop} : (∀ x, p x) ↔ (∀ a b, p ⟨a, b⟩) := ⟨assume h a b, h ⟨a, b⟩, assume h ⟨a, b⟩, h a b⟩ @[simp] theorem subtype.exists {β : α → Prop} {p : {a // β a} → Prop} : (∃ x, p x) ↔ (∃ a b, p ⟨a, b⟩) := ⟨assume ⟨⟨a, b⟩, h⟩, ⟨a, b, h⟩, assume ⟨a, b, h⟩, ⟨⟨a, b⟩, h⟩⟩ end miscellany /- propositional connectives -/ @[simp] lemma false_neq_true : false ≠ true := begin intro h, rw [h], trivial end section propositional variables {a b c d : Prop} /- implies -/ @[simp] theorem imp_self : (a → a) ↔ true := iff_true_intro id theorem imp_intro {α β} (h : α) (h₂ : β) : α := h theorem imp_false : (a → false) ↔ ¬ a := iff.rfl theorem imp_and_distrib {α} : (α → b ∧ c) ↔ (α → b) ∧ (α → c) := ⟨λ h, ⟨λ ha, (h ha).left, λ ha, (h ha).right⟩, λ h ha, ⟨h.left ha, h.right ha⟩⟩ @[simp] theorem and_imp : (a ∧ b → c) ↔ (a → b → c) := iff.intro (λ h ha hb, h ⟨ha, hb⟩) (λ h ⟨ha, hb⟩, h ha hb) theorem iff_def : (a ↔ b) ↔ (a → b) ∧ (b → a) := iff_iff_implies_and_implies _ _ theorem iff_def' : (a ↔ b) ↔ (b → a) ∧ (a → b) := iff_def.trans and.comm @[simp] theorem imp_true_iff {α : Sort*} : (α → true) ↔ true := iff_true_intro $ λ_, trivial @[simp] theorem imp_iff_right (ha : a) : (a → b) ↔ b := ⟨λf, f ha, imp_intro⟩ /- not -/ theorem not.elim {α : Sort*} (H1 : ¬a) (H2 : a) : α := absurd H2 H1 @[reducible] theorem not.imp {a b : Prop} (H2 : ¬b) (H1 : a → b) : ¬a := mt H1 H2 theorem not_not_of_not_imp : ¬(a → b) → ¬¬a := mt not.elim theorem not_of_not_imp {α} : ¬(α → b) → ¬b := mt imp_intro theorem dec_em (p : Prop) [decidable p] : p ∨ ¬p := decidable.em p theorem by_contradiction {p} [decidable p] : (¬p → false) → p := decidable.by_contradiction @[simp] theorem not_not [decidable a] : ¬¬a ↔ a := iff.intro by_contradiction not_not_intro theorem of_not_not [decidable a] : ¬¬a → a := by_contradiction theorem of_not_imp [decidable a] (h : ¬ (a → b)) : a := by_contradiction (not_not_of_not_imp h) theorem not.imp_symm [decidable a] (h : ¬a → b) (hb : ¬b) : a := by_contradiction $ hb ∘ h theorem not_imp_comm [decidable a] [decidable b] : (¬a → b) ↔ (¬b → a) := ⟨not.imp_symm, not.imp_symm⟩ theorem imp.swap : (a → b → c) ↔ (b → a → c) := ⟨function.swap, function.swap⟩ theorem imp_not_comm : (a → ¬b) ↔ (b → ¬a) := imp.swap /- and -/ theorem not_and_of_not_left (b : Prop) : ¬a → ¬(a ∧ b) := mt and.left theorem not_and_of_not_right (a : Prop) {b : Prop} : ¬b → ¬(a ∧ b) := mt and.right theorem and.imp_left (h : a → b) : a ∧ c → b ∧ c := and.imp h id theorem and.imp_right (h : a → b) : c ∧ a → c ∧ b := and.imp id h theorem and_not_self_iff (a : Prop) : a ∧ ¬ a ↔ false := iff.intro (assume h, (h.right) (h.left)) (assume h, h.elim) theorem not_and_self_iff (a : Prop) : ¬ a ∧ a ↔ false := iff.intro (assume ⟨hna, ha⟩, hna ha) false.elim lemma and_iff_left_of_imp {a b : Prop} (h : a → b) : (a ∧ b) ↔ a := iff.intro and.left (λ ha, ⟨ha, h ha⟩) lemma and_iff_right_of_imp {a b : Prop} (h : b → a) : (a ∧ b) ↔ b := iff.intro and.right (λ hb, ⟨h hb, hb⟩) /- or -/ theorem or_of_or_of_imp_of_imp (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → d) : c ∨ d := or.imp h₂ h₃ h₁ theorem or_of_or_of_imp_left (h₁ : a ∨ c) (h : a → b) : b ∨ c := or.imp_left h h₁ theorem or_of_or_of_imp_right (h₁ : c ∨ a) (h : a → b) : c ∨ b := or.imp_right h h₁ theorem or.elim3 (h : a ∨ b ∨ c) (ha : a → d) (hb : b → d) (hc : c → d) : d := or.elim h ha (assume h₂, or.elim h₂ hb hc) theorem or_imp_distrib : (a ∨ b → c) ↔ (a → c) ∧ (b → c) := ⟨assume h, ⟨assume ha, h (or.inl ha), assume hb, h (or.inr hb)⟩, assume ⟨ha, hb⟩, or.rec ha hb⟩ theorem or_iff_not_imp_left [decidable a] : a ∨ b ↔ (¬ a → b) := ⟨or.resolve_left, λ h, dite _ or.inl (or.inr ∘ h)⟩ theorem or_iff_not_imp_right [decidable b] : a ∨ b ↔ (¬ b → a) := or.comm.trans or_iff_not_imp_left theorem not_imp_not [decidable a] : (¬ a → ¬ b) ↔ (b → a) := ⟨assume h hb, by_contradiction $ assume na, h na hb, mt⟩ /- distributivity -/ theorem and_or_distrib_left : a ∧ (b ∨ c) ↔ (a ∧ b) ∨ (a ∧ c) := ⟨λ ⟨ha, hbc⟩, hbc.imp (and.intro ha) (and.intro ha), or.rec (and.imp_right or.inl) (and.imp_right or.inr)⟩ theorem or_and_distrib_right : (a ∨ b) ∧ c ↔ (a ∧ c) ∨ (b ∧ c) := (and.comm.trans and_or_distrib_left).trans (or_congr and.comm and.comm) theorem or_and_distrib_left : a ∨ (b ∧ c) ↔ (a ∨ b) ∧ (a ∨ c) := ⟨or.rec (λha, and.intro (or.inl ha) (or.inl ha)) (and.imp or.inr or.inr), and.rec $ or.rec (imp_intro ∘ or.inl) (or.imp_right ∘ and.intro)⟩ theorem and_or_distrib_right : (a ∧ b) ∨ c ↔ (a ∨ c) ∧ (b ∨ c) := (or.comm.trans or_and_distrib_left).trans (and_congr or.comm or.comm) /- iff -/ theorem iff_of_true (ha : a) (hb : b) : a ↔ b := ⟨λ_, hb, λ _, ha⟩ theorem iff_of_false (ha : ¬a) (hb : ¬b) : a ↔ b := ⟨ha.elim, hb.elim⟩ theorem iff_true_left (ha : a) : (a ↔ b) ↔ b := ⟨λ h, h.1 ha, iff_of_true ha⟩ theorem iff_true_right (ha : a) : (b ↔ a) ↔ b := iff.comm.trans (iff_true_left ha) theorem iff_false_left (ha : ¬a) : (a ↔ b) ↔ ¬b := ⟨λ h, mt h.2 ha, iff_of_false ha⟩ theorem iff_false_right (ha : ¬a) : (b ↔ a) ↔ ¬b := iff.comm.trans (iff_false_left ha) theorem not_or_of_imp [decidable a] (h : a → b) : ¬ a ∨ b := if ha : a then or.inr (h ha) else or.inl ha theorem imp_iff_not_or [decidable a] : (a → b) ↔ (¬ a ∨ b) := ⟨not_or_of_imp, or.neg_resolve_left⟩ theorem not_imp_of_and_not (h : a ∧ ¬ b) : ¬ (a → b) := assume h₁, and.right h (h₁ (and.left h)) @[simp] theorem not_imp [decidable a] : ¬(a → b) ↔ a ∧ ¬b := ⟨λ h, ⟨of_not_imp h, not_of_not_imp h⟩, not_imp_of_and_not⟩ theorem peirce (a b : Prop) [decidable a] : ((a → b) → a) → a := if ha : a then λ h, ha else λ h, h (λ h', absurd h' ha) theorem peirce' {a : Prop} (H : ∀ b : Prop, (a → b) → a) : a := H _ id theorem not_iff_not [decidable a] [decidable b] : (¬ a ↔ ¬ b) ↔ (a ↔ b) := by rw [@iff_def (¬ a), @iff_def' a]; exact and_congr not_imp_not not_imp_not theorem not_iff_comm [decidable a] [decidable b] : (¬ a ↔ b) ↔ (¬ b ↔ a) := by rw [@iff_def (¬ a), @iff_def (¬ b)]; exact and_congr not_imp_comm imp_not_comm theorem iff_not_comm [decidable a] [decidable b] : (a ↔ ¬ b) ↔ (b ↔ ¬ a) := by rw [@iff_def a, @iff_def b]; exact and_congr imp_not_comm not_imp_comm @[simp] theorem not_and_not_right [decidable a] [decidable b] : ¬(a ∧ ¬b) ↔ (a → b) := not_iff_comm.1 not_imp def decidable_of_iff (a : Prop) (h : a ↔ b) [D : decidable a] : decidable b := decidable_of_decidable_of_iff D h def decidable_of_iff' (b : Prop) (h : a ↔ b) [D : decidable b] : decidable a := decidable_of_decidable_of_iff D h.symm /- de morgan's laws -/ theorem not_and_of_not_or_not (h : ¬ a ∨ ¬ b) : ¬ (a ∧ b) := λ ⟨ha, hb⟩, or.elim h (absurd ha) (absurd hb) theorem not_and_distrib [decidable a] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if ha : a then or.inr (λ hb, h ⟨ha, hb⟩) else or.inl ha, not_and_of_not_or_not⟩ theorem not_and_distrib' [decidable b] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if hb : b then or.inl (λ ha, h ⟨ha, hb⟩) else or.inr hb, not_and_of_not_or_not⟩ @[simp] theorem not_and : ¬ (a ∧ b) ↔ (a → ¬ b) := and_imp theorem not_and' : ¬ (a ∧ b) ↔ b → ¬a := not_and.trans imp_not_comm theorem not_or_distrib : ¬ (a ∨ b) ↔ ¬ a ∧ ¬ b := ⟨λ h, ⟨λ ha, h (or.inl ha), λ hb, h (or.inr hb)⟩, λ ⟨h₁, h₂⟩ h, or.elim h h₁ h₂⟩ theorem or_iff_not_and_not [decidable a] [decidable b] : a ∨ b ↔ ¬ (¬a ∧ ¬b) := by rw [← not_or_distrib, not_not] theorem and_iff_not_or_not [decidable a] [decidable b] : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := by rw [← not_and_distrib, not_not] end propositional /- equality -/ section equality variables {α : Sort*} {a b : α} @[simp] lemma heq_iff_eq : a == b ↔ a = b := ⟨eq_of_heq, heq_of_eq⟩ end equality /- quantifiers -/ section quantifiers variables {α : Sort*} {p q : α → Prop} {b : Prop} theorem forall_swap {α β} {p : α → β → Prop} : (∀ x y, p x y) ↔ ∀ y x, p x y := ⟨function.swap, function.swap⟩ theorem exists_swap {α β} {p : α → β → Prop} : (∃ x y, p x y) ↔ ∃ y x, p x y := ⟨λ ⟨x, y, h⟩, ⟨y, x, h⟩, λ ⟨y, x, h⟩, ⟨x, y, h⟩⟩ theorem forall_of_forall (h : ∀ x, p x → q x) (h₁ : ∀ x, p x) : ∀ x, q x := assume x, h x (h₁ x) theorem exists_of_exists (h : ∀ x, p x → q x) (h₁ : ∃ x, p x) : ∃ x, q x := match h₁ with ⟨x, hpx⟩ := ⟨x, h x hpx⟩ end @[simp] theorem exists_imp_distrib : ((∃ x, p x) → b) ↔ ∀ x, p x → b := ⟨λ h x hpx, h ⟨x, hpx⟩, λ h ⟨x, hpx⟩, h x hpx⟩ --theorem forall_not_of_not_exists (h : ¬ ∃ x, p x) : ∀ x, ¬ p x := --forall_imp_of_exists_imp h theorem not_exists_of_forall_not (h : ∀ x, ¬ p x) : ¬ ∃ x, p x := exists_imp_distrib.2 h @[simp] theorem not_exists : (¬ ∃ x, p x) ↔ ∀ x, ¬ p x := exists_imp_distrib theorem not_forall_of_exists_not : (∃ x, ¬ p x) → ¬ ∀ x, p x | ⟨x, hn⟩ h := hn (h x) theorem not_forall {p : α → Prop} [decidable (∃ x, ¬ p x)] [∀ x, decidable (p x)] : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x := ⟨not.imp_symm $ λ nx x, nx.imp_symm $ λ h, ⟨x, h⟩, not_forall_of_exists_not⟩ @[simp] theorem not_forall_not [decidable (∃ x, p x)] : (¬ ∀ x, ¬ p x) ↔ ∃ x, p x := by have := decidable_of_iff (¬ ∃ x, p x) not_exists; exact not_iff_comm.1 not_exists @[simp] theorem not_exists_not [∀ x, decidable (p x)] : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := by simp @[simp] theorem forall_true_iff : (α → true) ↔ true := iff_true_intro (λ _, trivial) -- Unfortunately this causes simp to loop sometimes, so we -- add the 2 and 3 cases as simp lemmas instead theorem forall_true_iff' (h : b ↔ true) : (α → b) ↔ true := iff_true_intro (λ _, of_iff_true h) @[simp] theorem forall_2_true_iff {β} : (α → β → true) ↔ true := forall_true_iff' forall_true_iff @[simp] theorem forall_3_true_iff {β γ} : (α → β → γ → true) ↔ true := forall_true_iff' forall_2_true_iff @[simp] theorem forall_const (α : Sort*) [inhabited α] : (α → b) ↔ b := ⟨λ h, h (arbitrary α), λ hb x, hb⟩ @[simp] theorem exists_const (α : Sort*) [inhabited α] : (∃ x : α, b) ↔ b := ⟨λ ⟨x, h⟩, h, λ h, ⟨arbitrary α, h⟩⟩ theorem forall_and_distrib : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) := ⟨λ h, ⟨λ x, (h x).left, λ x, (h x).right⟩, λ ⟨h₁, h₂⟩ x, ⟨h₁ x, h₂ x⟩⟩ theorem exists_or_distrib : (∃ x, p x ∨ q x) ↔ (∃ x, p x) ∨ (∃ x, q x) := ⟨λ ⟨x, hpq⟩, hpq.elim (λ hpx, or.inl ⟨x, hpx⟩) (λ hqx, or.inr ⟨x, hqx⟩), λ hepq, hepq.elim (λ ⟨x, hpx⟩, ⟨x, or.inl hpx⟩) (λ ⟨x, hqx⟩, ⟨x, or.inr hqx⟩)⟩ @[simp] theorem exists_and_distrib_left {q : Prop} {p : α → Prop} : (∃x, q ∧ p x) ↔ q ∧ (∃x, p x) := ⟨λ ⟨x, hq, hp⟩, ⟨hq, x, hp⟩, λ ⟨hq, x, hp⟩, ⟨x, hq, hp⟩⟩ @[simp] theorem exists_and_distrib_right {q : Prop} {p : α → Prop} : (∃x, p x ∧ q) ↔ (∃x, p x) ∧ q := by simp @[simp] theorem forall_eq {a' : α} : (∀a, a = a' → p a) ↔ p a' := ⟨λ h, h a' rfl, λ h a e, e.symm ▸ h⟩ @[simp] theorem exists_eq {a' : α} : (∃ a, a = a' ∧ p a) ↔ p a' := ⟨λ ⟨a, e, h⟩, e ▸ h, λ h, ⟨_, rfl, h⟩⟩ @[simp] theorem exists_eq_right {a' : α} : (∃ a, p a ∧ a = a') ↔ p a' := (exists_congr $ by exact λ a, and.comm).trans exists_eq @[simp] theorem forall_eq' {a' : α} : (∀a, a' = a → p a) ↔ p a' := by simp [@eq_comm _ a'] @[simp] theorem exists_eq' {a' : α} : (∃ a, a' = a ∧ p a) ↔ p a' := by simp [@eq_comm _ a'] @[simp] theorem exists_eq_right' {a' : α} : (∃ a, p a ∧ a' = a) ↔ p a' := by simp [@eq_comm _ a'] theorem forall_or_of_or_forall (h : b ∨ ∀x, p x) (x) : b ∨ p x := h.imp_right $ λ h₂, h₂ x theorem forall_or_distrib_left {q : Prop} {p : α → Prop} [decidable q] : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := ⟨λ h, if hq : q then or.inl hq else or.inr $ λ x, (h x).resolve_left hq, forall_or_of_or_forall⟩ @[simp] theorem exists_prop {p q : Prop} : (∃ h : p, q) ↔ p ∧ q := ⟨λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩⟩ @[simp] theorem exists_false : ¬ (∃a:α, false) := assume ⟨a, h⟩, h end quantifiers /- classical versions -/ namespace classical variables {α : Sort*} {p : α → Prop} local attribute [instance] prop_decidable theorem not_forall : (¬ ∀ x, p x) ↔ (∃ x, ¬ p x) := not_forall theorem forall_or_distrib_left {q : Prop} {p : α → Prop} : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := forall_or_distrib_left lemma cases {p : Prop → Prop} (h1 : p true) (h2 : p false) : ∀a, p a := assume a, cases_on a h1 h2 lemma or_not {p : Prop} : p ∨ ¬ p := by_cases or.inl or.inr end classical /- bounded quantifiers -/ section bounded_quantifiers variables {α : Sort*} {r p q : α → Prop} {P Q : ∀ x, p x → Prop} {b : Prop} theorem bex_def : (∃ x (h : p x), q x) ↔ ∃ x, p x ∧ q x := ⟨λ ⟨x, px, qx⟩, ⟨x, px, qx⟩, λ ⟨x, px, qx⟩, ⟨x, px, qx⟩⟩ theorem bex.elim {b : Prop} : (∃ x h, P x h) → (∀ a h, P a h → b) → b | ⟨a, h₁, h₂⟩ h' := h' a h₁ h₂ theorem bex.intro (a : α) (h₁ : p a) (h₂ : P a h₁) : ∃ x (h : p x), P x h := ⟨a, h₁, h₂⟩ theorem ball_congr (H : ∀ x h, P x h ↔ Q x h) : (∀ x h, P x h) ↔ (∀ x h, Q x h) := forall_congr $ λ x, forall_congr (H x) theorem bex_congr (H : ∀ x h, P x h ↔ Q x h) : (∃ x h, P x h) ↔ (∃ x h, Q x h) := exists_congr $ λ x, exists_congr (H x) theorem ball.imp_right (H : ∀ x h, (P x h → Q x h)) (h₁ : ∀ x h, P x h) (x h) : Q x h := H _ _ $ h₁ _ _ theorem bex.imp_right (H : ∀ x h, (P x h → Q x h)) : (∃ x h, P x h) → ∃ x h, Q x h | ⟨x, h, h'⟩ := ⟨_, _, H _ _ h'⟩ theorem ball.imp_left (H : ∀ x, p x → q x) (h₁ : ∀ x, q x → r x) (x) (h : p x) : r x := h₁ _ $ H _ h theorem bex.imp_left (H : ∀ x, p x → q x) : (∃ x (_ : p x), r x) → ∃ x (_ : q x), r x | ⟨x, hp, hr⟩ := ⟨x, H _ hp, hr⟩ theorem ball_of_forall (h : ∀ x, p x) (x) (_ : q x) : p x := h x theorem forall_of_ball (H : ∀ x, p x) (h : ∀ x, p x → q x) (x) : q x := h x $ H x theorem bex_of_exists (H : ∀ x, p x) : (∃ x, q x) → ∃ x (_ : p x), q x | ⟨x, hq⟩ := ⟨x, H x, hq⟩ theorem exists_of_bex : (∃ x (_ : p x), q x) → ∃ x, q x | ⟨x, _, hq⟩ := ⟨x, hq⟩ @[simp] theorem bex_imp_distrib : ((∃ x h, P x h) → b) ↔ (∀ x h, P x h → b) := by simp theorem not_bex : (¬ ∃ x h, P x h) ↔ ∀ x h, ¬ P x h := bex_imp_distrib theorem not_ball_of_bex_not : (∃ x h, ¬ P x h) → ¬ ∀ x h, P x h | ⟨x, h, hp⟩ al := hp $ al x h theorem not_ball [decidable (∃ x h, ¬ P x h)] [∀ x h, decidable (P x h)] : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := ⟨not.imp_symm $ λ nx x h, nx.imp_symm $ λ h', ⟨x, h, h'⟩, not_ball_of_bex_not⟩ theorem ball_true_iff (p : α → Prop) : (∀ x, p x → true) ↔ true := iff_true_intro (λ h hrx, trivial) theorem ball_and_distrib : (∀ x h, P x h ∧ Q x h) ↔ (∀ x h, P x h) ∧ (∀ x h, Q x h) := iff.trans (forall_congr $ λ x, forall_and_distrib) forall_and_distrib theorem bex_or_distrib : (∃ x h, P x h ∨ Q x h) ↔ (∃ x h, P x h) ∨ (∃ x h, Q x h) := iff.trans (exists_congr $ λ x, exists_or_distrib) exists_or_distrib end bounded_quantifiers namespace classical local attribute [instance] prop_decidable theorem not_ball {α : Sort*} {p : α → Prop} {P : Π (x : α), p x → Prop} : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := _root_.not_ball end classical
f476a1008a2d2b57d11ec5d32301c745fa274116
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/order/filter/filter_product.lean
97249ae6faafea570b95430dcad6dca13c44721b
[ "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,897
lean
/- Copyright (c) 2019 Abhimanyu Pallavi Sudhir. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Abhimanyu Pallavi Sudhir, Yury Kudryashov -/ import order.filter.ultrafilter import order.filter.germ /-! # Ultraproducts If `φ` is an ultrafilter, then the space of germs of functions `f : α → β` at `φ` is called the *ultraproduct*. In this file we prove properties of ultraproducts that rely on `φ` being an ultrafilter. Definitions and properties that work for any filter should go to `order.filter.germ`. ## Tags ultrafilter, ultraproduct -/ universes u v variables {α : Type u} {β : Type v} {φ : ultrafilter α} open_locale classical namespace filter local notation `∀*` binders `, ` r:(scoped p, filter.eventually p φ) := r namespace germ open ultrafilter local notation `β*` := germ (φ : filter α) β /-- If `φ` is an ultrafilter then the ultraproduct is a division ring. -/ instance [division_ring β] : division_ring β* := { mul_inv_cancel := λ f, induction_on f $ λ f hf, coe_eq.2 $ (φ.em (λ y, f y = 0)).elim (λ H, (hf $ coe_eq.2 H).elim) (λ H, H.mono $ λ x, mul_inv_cancel), inv_zero := coe_eq.2 $ by simp only [(∘), inv_zero], .. germ.ring, .. germ.div_inv_monoid, .. germ.nontrivial } /-- If `φ` is an ultrafilter then the ultraproduct is a field. -/ instance [field β] : field β* := { .. germ.comm_ring, .. germ.division_ring } /-- If `φ` is an ultrafilter then the ultraproduct is a linear order. -/ noncomputable instance [linear_order β] : linear_order β* := { le_total := λ f g, induction_on₂ f g $ λ f g, eventually_or.1 $ eventually_of_forall $ λ x, le_total _ _, decidable_le := by apply_instance, .. germ.partial_order } @[simp, norm_cast] lemma const_div [division_ring β] (x y : β) : (↑(x / y) : β*) = ↑x / ↑y := rfl lemma coe_lt [preorder β] {f g : α → β} : (f : β*) < g ↔ ∀* x, f x < g x := by simp only [lt_iff_le_not_le, eventually_and, coe_le, eventually_not, eventually_le] lemma coe_pos [preorder β] [has_zero β] {f : α → β} : 0 < (f : β*) ↔ ∀* x, 0 < f x := coe_lt lemma const_lt [preorder β] {x y : β} : (↑x : β*) < ↑y ↔ x < y := coe_lt.trans lift_rel_const_iff lemma lt_def [preorder β] : ((<) : β* → β* → Prop) = lift_rel (<) := by { ext ⟨f⟩ ⟨g⟩, exact coe_lt } /-- If `φ` is an ultrafilter then the ultraproduct is an ordered ring. -/ instance [ordered_ring β] : ordered_ring β* := { zero_le_one := const_le zero_le_one, mul_pos := λ x y, induction_on₂ x y $ λ f g hf hg, coe_pos.2 $ (coe_pos.1 hg).mp $ (coe_pos.1 hf).mono $ λ x, mul_pos, .. germ.ring, .. germ.ordered_add_comm_group, .. germ.nontrivial } /-- If `φ` is an ultrafilter then the ultraproduct is a linear ordered ring. -/ noncomputable instance [linear_ordered_ring β] : linear_ordered_ring β* := { .. germ.ordered_ring, .. germ.linear_order, .. germ.nontrivial } /-- If `φ` is an ultrafilter then the ultraproduct is a linear ordered field. -/ noncomputable instance [linear_ordered_field β] : linear_ordered_field β* := { .. germ.linear_ordered_ring, .. germ.field } /-- If `φ` is an ultrafilter then the ultraproduct is a linear ordered commutative ring. -/ noncomputable instance [linear_ordered_comm_ring β] : linear_ordered_comm_ring β* := { .. germ.linear_ordered_ring, .. germ.comm_monoid } /-- If `φ` is an ultrafilter then the ultraproduct is a decidable linear ordered commutative group. -/ noncomputable instance [linear_ordered_add_comm_group β] : linear_ordered_add_comm_group β* := { .. germ.ordered_add_comm_group, .. germ.linear_order } lemma max_def [linear_order β] (x y : β*) : max x y = map₂ max x y := induction_on₂ x y $ λ a b, begin cases le_total (a : β*) b, { rw [max_eq_right h, map₂_coe, coe_eq], exact h.mono (λ i hi, (max_eq_right hi).symm) }, { rw [max_eq_left h, map₂_coe, coe_eq], exact h.mono (λ i hi, (max_eq_left hi).symm) } end lemma min_def [K : linear_order β] (x y : β*) : min x y = map₂ min x y := induction_on₂ x y $ λ a b, begin cases le_total (a : β*) b, { rw [min_eq_left h, map₂_coe, coe_eq], exact h.mono (λ i hi, (min_eq_left hi).symm) }, { rw [min_eq_right h, map₂_coe, coe_eq], exact h.mono (λ i hi, (min_eq_right hi).symm) } end lemma abs_def [linear_ordered_add_comm_group β] (x : β*) : abs x = map abs x := induction_on x $ λ a, by rw [abs, ← coe_neg, max_def, map₂_coe]; refl @[simp] lemma const_max [linear_order β] (x y : β) : (↑(max x y : β) : β*) = max ↑x ↑y := by rw [max_def, map₂_const] @[simp] lemma const_min [linear_order β] (x y : β) : (↑(min x y : β) : β*) = min ↑x ↑y := by rw [min_def, map₂_const] @[simp] lemma const_abs [linear_ordered_add_comm_group β] (x : β) : (↑(abs x) : β*) = abs ↑x := const_max x (-x) end germ end filter
aa35af3c00c991b7cd2a00a3dc6e4287c6d32000
94e33a31faa76775069b071adea97e86e218a8ee
/src/linear_algebra/alternating.lean
14d0b96d0024b48bc6d57bac38fc7002dc2a84d4
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
42,986
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 group_theory.group_action.quotient import group_theory.perm.sign import group_theory.perm.subgroup import linear_algebra.linear_independent import linear_algebra.multilinear.basis import linear_algebra.multilinear.tensor_product import logic.equiv.fin /-! # 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 `module` structure over `alternating_map`s that matches the definitions over `multilinear_map`s. * `multilinear_map.dom_dom_congr`, for permutating the elements within a family. * `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. * `alternating_map.curry_left`, for binding the leftmost argument of an alternating map indexed by `fin n.succ`. ## 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] [module R M] variables {N : Type*} [add_comm_monoid N] [module R N] -- semiring / add_comm_group variables {M' : Type*} [add_comm_group M'] [module R M'] variables {N' : Type*} [add_comm_group N'] [module R N'] variables {ι ι' ι'' : Type*} [decidable_eq ι] [decidable_eq ι'] [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 ι) (λ _, (ι → 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_injective : injective (coe_fn : alternating_map R M N ι → ((ι → M) → N)) := λ f g h, by { cases f, cases g, cases h, refl } @[simp, norm_cast] theorem coe_inj {f g : alternating_map R M N ι} : (f : (ι → M) → N) = g ↔ f = g := coe_injective.eq_iff @[ext] theorem ext {f f' : alternating_map R M N ι} (H : ∀ x, f x = f' x) : f = f' := coe_injective (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 lemma map_eq_zero_of_not_injective (v : ι → M) (hv : ¬function.injective v) : f v = 0 := begin rw function.injective at hv, push_neg at hv, rcases hv with ⟨i₁, i₂, heq, hne⟩, exact f.map_eq_zero_of_eq v heq hne end /-! ### Algebraic structure inherited from `multilinear_map` `alternating_map` carries the same `add_comm_monoid`, `add_comm_group`, and `module` structure as `multilinear_map` -/ section has_smul variables {S : Type*} [monoid S] [distrib_mul_action S N] [smul_comm_class R S N] instance : has_smul 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 lemma coe_fn_smul (c : S) (f : alternating_map R M N ι) : ⇑(c • f) = c • f := rfl instance [distrib_mul_action Sᵐᵒᵖ N] [is_central_scalar S N] : is_central_scalar S (alternating_map R M N ι) := ⟨λ c f, ext $ λ x, op_smul_eq_smul _ _⟩ end has_smul 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 ι) := coe_injective.add_comm_monoid _ rfl (λ _ _, rfl) (λ _ _, coe_fn_smul _ _) 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' ι) := coe_injective.add_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, coe_fn_smul _ _) (λ _ _, coe_fn_smul _ _) section distrib_mul_action variables {S : Type*} [monoid S] [distrib_mul_action S N] [smul_comm_class R S N] 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 module variables {S : Type*} [semiring S] [module 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 : module S (alternating_map R M N ι) := { add_smul := λ r₁ r₂ f, ext $ λ x, add_smul _ _ _, zero_smul := λ f, ext $ λ x, zero_smul _ _ } instance [no_zero_smul_divisors S N] : no_zero_smul_divisors S (alternating_map R M N ι) := coe_injective.no_zero_smul_divisors _ rfl coe_fn_smul end module section variables (R M) /-- The evaluation map from `ι → M` to `M` at a given `i` is alternating when `ι` is subsingleton. -/ @[simps] def of_subsingleton [subsingleton ι] (i : ι) : alternating_map R M M ι := { to_fun := function.eval i, map_eq_zero_of_eq' := λ v i j hv hij, (hij $ subsingleton.elim _ _).elim, ..multilinear_map.of_subsingleton R M i } /-- The constant map is alternating when `ι` is empty. -/ @[simps {fully_applied := ff}] def const_of_is_empty [is_empty ι] (m : N) : alternating_map R M N ι := { to_fun := function.const _ m, map_eq_zero_of_eq' := λ v, is_empty_elim, ..multilinear_map.const_of_is_empty R m } end /-- Restrict the codomain of an alternating map to a submodule. -/ @[simps] def cod_restrict (f : alternating_map R M N ι) (p : submodule R N) (h : ∀ v, f v ∈ p) : alternating_map R M p ι := { to_fun := λ v, ⟨f v, h v⟩, map_eq_zero_of_eq' := λ v i j hv hij, subtype.ext $ map_eq_zero_of_eq _ _ hv hij, ..f.to_multilinear_map.cod_restrict p h } end alternating_map /-! ### Composition with linear maps -/ namespace linear_map variables {N₂ : Type*} [add_comm_monoid N₂] [module R N₂] /-- Composing a alternating map with a linear map on the left gives again an 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 @[simp] 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 @[simp] lemma subtype_comp_alternating_map_cod_restrict (f : alternating_map R M N ι) (p : submodule R N) (h) : p.subtype.comp_alternating_map (f.cod_restrict p h) = f := alternating_map.ext $ λ v, rfl @[simp] lemma comp_alternating_map_cod_restrict (g : N →ₗ[R] N₂) (f : alternating_map R M N ι) (p : submodule R N₂) (h) : (g.cod_restrict p h).comp_alternating_map f = (g.comp_alternating_map f).cod_restrict p (λ v, h (f v)):= alternating_map.ext $ λ v, rfl end linear_map namespace alternating_map variables {M₂ : Type*} [add_comm_monoid M₂] [module R M₂] variables {M₃ : Type*} [add_comm_monoid M₃] [module R M₃] /-- Composing a alternating map with the same linear map on each argument gives again an alternating map. -/ def comp_linear_map (f : alternating_map R M N ι) (g : M₂ →ₗ[R] M) : alternating_map R M₂ N ι := { map_eq_zero_of_eq' := λ v i j h hij, f.map_eq_zero_of_eq _ (linear_map.congr_arg h) hij, .. (f : multilinear_map R (λ _ : ι, M) N).comp_linear_map (λ _, g) } lemma coe_comp_linear_map (f : alternating_map R M N ι) (g : M₂ →ₗ[R] M) : ⇑(f.comp_linear_map g) = f ∘ ((∘) g) := rfl @[simp] lemma comp_linear_map_apply (f : alternating_map R M N ι) (g : M₂ →ₗ[R] M) (v : ι → M₂) : f.comp_linear_map g v = f (λ i, g (v i)) := rfl /-- Composing an alternating map twice with the same linear map in each argument is the same as composing with their composition. -/ lemma comp_linear_map_assoc (f : alternating_map R M N ι) (g₁ : M₂ →ₗ[R] M) (g₂ : M₃ →ₗ[R] M₂) : (f.comp_linear_map g₁).comp_linear_map g₂ = f.comp_linear_map (g₁ ∘ₗ g₂) := rfl @[simp] lemma zero_comp_linear_map (g : M₂ →ₗ[R] M) : (0 : alternating_map R M N ι).comp_linear_map g = 0 := by { ext, simp only [comp_linear_map_apply, zero_apply] } @[simp] lemma add_comp_linear_map (f₁ f₂ : alternating_map R M N ι) (g : M₂ →ₗ[R] M) : (f₁ + f₂).comp_linear_map g = f₁.comp_linear_map g + f₂.comp_linear_map g := by { ext, simp only [comp_linear_map_apply, add_apply] } @[simp] lemma comp_linear_map_zero [nonempty ι] (f : alternating_map R M N ι) : f.comp_linear_map (0 : M₂ →ₗ[R] M) = 0 := begin ext, simp_rw [comp_linear_map_apply, linear_map.zero_apply, ←pi.zero_def, map_zero, zero_apply], end /-- Composing an alternating map with the identity linear map in each argument. -/ @[simp] lemma comp_linear_map_id (f : alternating_map R M N ι) : f.comp_linear_map linear_map.id = f := ext $ λ _, rfl /-- Composing with a surjective linear map is injective. -/ lemma comp_linear_map_injective (f : M₂ →ₗ[R] M) (hf : function.surjective f) : function.injective (λ g : alternating_map R M N ι, g.comp_linear_map f) := λ g₁ g₂ h, ext $ λ x, by simpa [function.surj_inv_eq hf] using ext_iff.mp h (function.surj_inv hf ∘ x) lemma comp_linear_map_inj (f : M₂ →ₗ[R] M) (hf : function.surjective f) (g₁ g₂ : alternating_map R M N ι) : g₁.comp_linear_map f = g₂.comp_linear_map f ↔ g₁ = g₂ := (comp_linear_map_injective _ hf).eq_iff section dom_lcongr variables (ι R N) (S : Type*) [semiring S] [module S N] [smul_comm_class R S N] /-- Construct a linear equivalence between maps from a linear equivalence between domains. -/ @[simps apply] def dom_lcongr (e : M ≃ₗ[R] M₂) : alternating_map R M N ι ≃ₗ[S] alternating_map R M₂ N ι := { to_fun := λ f, f.comp_linear_map e.symm, inv_fun := λ g, g.comp_linear_map e, map_add' := λ _ _, rfl, map_smul' := λ _ _, rfl, left_inv := λ f, alternating_map.ext $ λ v, f.congr_arg $ funext $ λ i, e.symm_apply_apply _, right_inv := λ f, alternating_map.ext $ λ v, f.congr_arg $ funext $ λ i, e.apply_symm_apply _ } @[simp] lemma dom_lcongr_refl : dom_lcongr R N ι S (linear_equiv.refl R M) = linear_equiv.refl S _ := linear_equiv.ext $ λ _, alternating_map.ext $ λ v, rfl @[simp] lemma dom_lcongr_symm (e : M ≃ₗ[R] M₂) : (dom_lcongr R N ι S e).symm = dom_lcongr R N ι S e.symm := rfl lemma dom_lcongr_trans (e : M ≃ₗ[R] M₂) (f : M₂ ≃ₗ[R] M₃): (dom_lcongr R N ι S e).trans (dom_lcongr R N ι S f) = dom_lcongr R N ι S (e.trans f) := rfl end dom_lcongr /-- Composing an alternating map with the same linear equiv on each argument gives the zero map if and only if the alternating map is the zero map. -/ @[simp] lemma comp_linear_equiv_eq_zero_iff (f : alternating_map R M N ι) (g : M₂ ≃ₗ[R] M) : f.comp_linear_map (g : M₂ →ₗ[R] M) = 0 ↔ f = 0 := (dom_lcongr R N ι ℕ g.symm).map_eq_zero_iff 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_left $ 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 } section dom_dom_congr /-- Transfer the arguments to a map along an equivalence between argument indices. This is the alternating version of `multilinear_map.dom_dom_congr`. -/ @[simps] def dom_dom_congr (σ : ι ≃ ι') (f : alternating_map R M N ι) : alternating_map R M N ι' := { to_fun := λ v, f (v ∘ σ), map_eq_zero_of_eq' := λ v i j hv hij, f.map_eq_zero_of_eq (v ∘ σ) (by simpa using hv) (σ.symm.injective.ne hij), .. f.to_multilinear_map.dom_dom_congr σ } @[simp] lemma dom_dom_congr_refl (f : alternating_map R M N ι) : f.dom_dom_congr (equiv.refl ι) = f := ext $ λ v, rfl lemma dom_dom_congr_trans (σ₁ : ι ≃ ι') (σ₂ : ι' ≃ ι'') (f : alternating_map R M N ι) : f.dom_dom_congr (σ₁.trans σ₂) = (f.dom_dom_congr σ₁).dom_dom_congr σ₂ := rfl @[simp] lemma dom_dom_congr_zero (σ : ι ≃ ι') : (0 : alternating_map R M N ι).dom_dom_congr σ = 0 := rfl @[simp] lemma dom_dom_congr_add (σ : ι ≃ ι') (f g : alternating_map R M N ι) : (f + g).dom_dom_congr σ = f.dom_dom_congr σ + g.dom_dom_congr σ := rfl /-- `alternating_map.dom_dom_congr` as an equivalence. This is declared separately because it does not work with dot notation. -/ @[simps apply symm_apply] def dom_dom_congr_equiv (σ : ι ≃ ι') : alternating_map R M N ι ≃+ alternating_map R M N ι' := { to_fun := dom_dom_congr σ, inv_fun := dom_dom_congr σ.symm, left_inv := λ f, by { ext, simp [function.comp] }, right_inv := λ m, by { ext, simp [function.comp] }, map_add' := dom_dom_congr_add σ } /-- The results of applying `dom_dom_congr` to two maps are equal if and only if those maps are. -/ @[simp] lemma dom_dom_congr_eq_iff (σ : ι ≃ ι') (f g : alternating_map R M N ι) : f.dom_dom_congr σ = g.dom_dom_congr σ ↔ f = g := (dom_dom_congr_equiv σ : _ ≃+ alternating_map R M N ι').apply_eq_iff_eq @[simp] lemma dom_dom_congr_eq_zero_iff (σ : ι ≃ ι') (f : alternating_map R M N ι) : f.dom_dom_congr σ = 0 ↔ f = 0 := (dom_dom_congr_equiv σ : alternating_map R M N ι ≃+ alternating_map R M N ι').map_eq_zero_iff lemma dom_dom_congr_perm [fintype ι] (σ : equiv.perm ι) : g.dom_dom_congr σ = σ.sign • g := alternating_map.ext $ λ v, g.map_perm v σ @[norm_cast] lemma coe_dom_dom_congr (σ : ι ≃ ι') : ↑(f.dom_dom_congr σ) = (f : multilinear_map R (λ _ : ι, M) N).dom_dom_congr σ := multilinear_map.ext $ λ v, rfl end dom_dom_congr /-- 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] [module K M] {N : Type*} [add_comm_group N] [module 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⟩ := not_linear_independent_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 section fin open fin /-- A version of `multilinear_map.cons_add` for `alternating_map`. -/ lemma map_vec_cons_add {n : ℕ} (f : alternating_map R M N (fin n.succ)) (m : fin n → M) (x y : M) : f (matrix.vec_cons (x+y) m) = f (matrix.vec_cons x m) + f (matrix.vec_cons y m) := f.to_multilinear_map.cons_add _ _ _ /-- A version of `multilinear_map.cons_smul` for `alternating_map`. -/ lemma map_vec_cons_smul {n : ℕ} (f : alternating_map R M N (fin n.succ)) (m : fin n → M) (c : R) (x : M) : f (matrix.vec_cons (c • x) m) = c • f (matrix.vec_cons x m) := f.to_multilinear_map.cons_smul _ _ _ end fin 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_injective 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_injective, simp_rw [multilinear_map.alternatization_def, ←coe_dom_dom_congr, dom_dom_congr_perm, coe_smul, smul_smul, int.units_mul_self, one_smul, finset.sum_const, finset.card_univ, fintype.card_perm, ←coe_multilinear_map, coe_smul], end end alternating_map namespace linear_map variables {N'₂ : Type*} [add_comm_group N'₂] [module 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₁] [module R' N₁] [add_comm_group N₂] [module R' N₂] [add_comm_monoid Mᵢ] [module R' Mᵢ] namespace equiv.perm /-- Elements which are considered equivalent if they differ only by swaps within α or β -/ abbreviation mod_sum_congr (α β : Type*) := _ ⧸ (equiv.perm.sum_congr_hom α β).range lemma mod_sum_congr.swap_smul_involutive {α β : Type*} [decidable_eq (α ⊕ β)] (i j : α ⊕ β) : function.involutive (has_smul.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 σ) (λ σ₁ σ₂ H, begin rw quotient_group.left_rel_apply at H, obtain ⟨⟨sl, sr⟩, h⟩ := H, 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, smul_left_cancel_iff, ←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 [smul_eq_mul, perm.sign_mul, perm.sign_swap hij], simp only [one_mul, neg_mul, function.comp_app, units.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_group.left_rel_apply.mp (quotient.exact' hσ), }, work_on_goal 1 { replace hσ := equiv.congr_fun hσ (sum.inl i'), }, work_on_goal 2 { replace hσ := equiv.congr_fun hσ (sum.inr i'), }, all_goals { rw [smul_eq_mul, ←mul_swap_eq_swap_mul, mul_inv_rev, 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 1 { convert tensor_product.tmul_zero _ _, }, work_on_goal 2 { 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_group.eq' }, -- 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 `_ →ₗ[ℤ] _` and back rw [←add_equiv.coe_to_add_monoid_hom, ←add_monoid_hom.coe_to_int_linear_map, linear_map.map_smul_of_tower, linear_map.map_smul_of_tower, add_monoid_hom.coe_to_int_linear_map, 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, perm.sum_congr_hom_apply, multilinear_map.dom_coprod_dom_dom_congr_sum_congr, perm.sign_sum_congr, mul_smul, mul_smul], 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 section basis open alternating_map variables {ι₁ : Type*} [fintype ι] variables {R' : Type*} {N₁ N₂ : Type*} [comm_semiring R'] [add_comm_monoid N₁] [add_comm_monoid N₂] variables [module R' N₁] [module R' N₂] /-- Two alternating maps indexed by a `fintype` are equal if they are equal when all arguments are distinct basis vectors. -/ lemma basis.ext_alternating {f g : alternating_map R' N₁ N₂ ι} (e : basis ι₁ R' N₁) (h : ∀ v : ι → ι₁, function.injective v → f (λ i, e (v i)) = g (λ i, e (v i))) : f = g := begin refine alternating_map.coe_multilinear_map_injective (basis.ext_multilinear e $ λ v, _), by_cases hi : function.injective v, { exact h v hi }, { have : ¬function.injective (λ i, e (v i)) := hi.imp function.injective.of_comp, rw [coe_multilinear_map, coe_multilinear_map, f.map_eq_zero_of_not_injective _ this, g.map_eq_zero_of_not_injective _ this], } end end basis /-! ### Currying -/ section currying variables {R' : Type*} {M'' M₂'' N'' N₂'': Type*} [comm_semiring R'] [add_comm_monoid M''] [add_comm_monoid M₂''] [add_comm_monoid N''] [add_comm_monoid N₂''] [module R' M''] [module R' M₂''] [module R' N''] [module R' N₂''] namespace alternating_map /-- Given an alternating map `f` in `n+1` variables, split the first variable to obtain a linear map into alternating maps in `n` variables, given by `x ↦ (m ↦ f (matrix.vec_cons x m))`. It can be thought of as a map $Hom(\bigwedge^{n+1} M, N) \to Hom(M, Hom(\bigwedge^n M, N))$. This is `multilinear_map.curry_left` for `alternating_map`. See also `alternating_map.curry_left_linear_map`. -/ @[simps] def curry_left {n : ℕ} (f : alternating_map R' M'' N'' (fin n.succ)) : M'' →ₗ[R'] alternating_map R' M'' N'' (fin n) := { to_fun := λ m, { to_fun := λ v, f (matrix.vec_cons m v), map_eq_zero_of_eq' := λ v i j hv hij, f.map_eq_zero_of_eq _ (by rwa [matrix.cons_val_succ, matrix.cons_val_succ]) ((fin.succ_injective _).ne hij), .. f.to_multilinear_map.curry_left m }, map_add' := λ m₁ m₂, ext $ λ v, f.map_vec_cons_add _ _ _, map_smul' := λ r m, ext $ λ v, f.map_vec_cons_smul _ _ _ } @[simp] lemma curry_left_zero {n : ℕ} : curry_left (0 : alternating_map R' M'' N'' (fin n.succ)) = 0 := rfl @[simp] lemma curry_left_add {n : ℕ} (f g : alternating_map R' M'' N'' (fin n.succ)) : curry_left (f + g) = curry_left f + curry_left g := rfl @[simp] lemma curry_left_smul {n : ℕ} (r : R') (f : alternating_map R' M'' N'' (fin n.succ)) : curry_left (r • f) = r • curry_left f := rfl /-- `alternating_map.curry_left` as a `linear_map`. This is a separate definition as dot notation does not work for this version. -/ @[simps] def curry_left_linear_map {n : ℕ} : alternating_map R' M'' N'' (fin n.succ) →ₗ[R'] M'' →ₗ[R'] alternating_map R' M'' N'' (fin n) := { to_fun := λ f, f.curry_left, map_add' := curry_left_add, map_smul' := curry_left_smul } /-- Currying with the same element twice gives the zero map. -/ @[simp] lemma curry_left_same {n : ℕ} (f : alternating_map R' M'' N'' (fin n.succ.succ)) (m : M'') : (f.curry_left m).curry_left m = 0 := ext $ λ x, f.map_eq_zero_of_eq _ (by simp) fin.zero_ne_one @[simp] lemma curry_left_comp_alternating_map {n : ℕ} (g : N'' →ₗ[R'] N₂'') (f : alternating_map R' M'' N'' (fin n.succ)) (m : M'') : (g.comp_alternating_map f).curry_left m = g.comp_alternating_map (f.curry_left m) := rfl @[simp] lemma curry_left_comp_linear_map {n : ℕ} (g : M₂'' →ₗ[R'] M'') (f : alternating_map R' M'' N'' (fin n.succ)) (m : M₂'') : (f.comp_linear_map g).curry_left m = (f.curry_left (g m)).comp_linear_map g := ext $ λ v, congr_arg f $ funext $ begin refine fin.cases _ _, { refl }, { simp } end /-- The space of constant maps is equivalent to the space of maps that are alternating with respect to an empty family. -/ @[simps] def const_linear_equiv_of_is_empty [is_empty ι] : N'' ≃ₗ[R'] alternating_map R' M'' N'' ι := { to_fun := alternating_map.const_of_is_empty R' M'', map_add' := λ x y, rfl, map_smul' := λ t x, rfl, inv_fun := λ f, f 0, left_inv := λ _, rfl, right_inv := λ f, ext $ λ x, alternating_map.congr_arg f $ subsingleton.elim _ _ } end alternating_map end currying
b427a944ffc01b343c18257783252eb717e0dffc
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/continued_fractions/continuants_recurrence.lean
e3a6f7bf026d53a63f18560583dc8275933c1db5
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
3,761
lean
/- Copyright (c) 2019 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import algebra.continued_fractions.translations /-! # Recurrence Lemmas for the `continuants` Function of Continued Fractions. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Summary Given a generalized continued fraction `g`, for all `n ≥ 1`, we prove that the `continuants` function indeed satisfies the following recurrences: - `Aₙ = bₙ * Aₙ₋₁ + aₙ * Aₙ₋₂`, and - `Bₙ = bₙ * Bₙ₋₁ + aₙ * Bₙ₋₂`. -/ namespace generalized_continued_fraction variables {K : Type*} {g : generalized_continued_fraction K} {n : ℕ} [division_ring K] lemma continuants_aux_recurrence {gp ppred pred : pair K} (nth_s_eq : g.s.nth n = some gp) (nth_conts_aux_eq : g.continuants_aux n = ppred) (succ_nth_conts_aux_eq : g.continuants_aux (n + 1) = pred) : g.continuants_aux (n + 2) = ⟨gp.b * pred.a + gp.a * ppred.a, gp.b * pred.b + gp.a * ppred.b⟩ := by simp [*, continuants_aux, next_continuants, next_denominator, next_numerator] lemma continuants_recurrence_aux {gp ppred pred : pair K} (nth_s_eq : g.s.nth n = some gp) (nth_conts_aux_eq : g.continuants_aux n = ppred) (succ_nth_conts_aux_eq : g.continuants_aux (n + 1) = pred) : g.continuants (n + 1) = ⟨gp.b * pred.a + gp.a * ppred.a, gp.b * pred.b + gp.a * ppred.b⟩ := by simp [nth_cont_eq_succ_nth_cont_aux, (continuants_aux_recurrence nth_s_eq nth_conts_aux_eq succ_nth_conts_aux_eq)] /-- Shows that `Aₙ = bₙ * Aₙ₋₁ + aₙ * Aₙ₋₂` and `Bₙ = bₙ * Bₙ₋₁ + aₙ * Bₙ₋₂`. -/ theorem continuants_recurrence {gp ppred pred : pair K} (succ_nth_s_eq : g.s.nth (n + 1) = some gp) (nth_conts_eq : g.continuants n = ppred) (succ_nth_conts_eq : g.continuants (n + 1) = pred) : g.continuants (n + 2) = ⟨gp.b * pred.a + gp.a * ppred.a, gp.b * pred.b + gp.a * ppred.b⟩ := begin rw [nth_cont_eq_succ_nth_cont_aux] at nth_conts_eq succ_nth_conts_eq, exact (continuants_recurrence_aux succ_nth_s_eq nth_conts_eq succ_nth_conts_eq) end /-- Shows that `Aₙ = bₙ * Aₙ₋₁ + aₙ * Aₙ₋₂`. -/ lemma numerators_recurrence {gp : pair K} {ppredA predA : K} (succ_nth_s_eq : g.s.nth (n + 1) = some gp) (nth_num_eq : g.numerators n = ppredA) (succ_nth_num_eq : g.numerators (n + 1) = predA) : g.numerators (n + 2) = gp.b * predA + gp.a * ppredA := begin obtain ⟨ppredConts, nth_conts_eq, ⟨rfl⟩⟩ : ∃ conts, g.continuants n = conts ∧ conts.a = ppredA, from exists_conts_a_of_num nth_num_eq, obtain ⟨predConts, succ_nth_conts_eq, ⟨rfl⟩⟩ : ∃ conts, g.continuants (n + 1) = conts ∧ conts.a = predA, from exists_conts_a_of_num succ_nth_num_eq, rw [num_eq_conts_a, (continuants_recurrence succ_nth_s_eq nth_conts_eq succ_nth_conts_eq)] end /-- Shows that `Bₙ = bₙ * Bₙ₋₁ + aₙ * Bₙ₋₂`. -/ lemma denominators_recurrence {gp : pair K} {ppredB predB : K} (succ_nth_s_eq : g.s.nth (n + 1) = some gp) (nth_denom_eq : g.denominators n = ppredB) (succ_nth_denom_eq : g.denominators (n + 1) = predB) : g.denominators (n + 2) = gp.b * predB + gp.a * ppredB := begin obtain ⟨ppredConts, nth_conts_eq, ⟨rfl⟩⟩ : ∃ conts, g.continuants n = conts ∧ conts.b = ppredB, from exists_conts_b_of_denom nth_denom_eq, obtain ⟨predConts, succ_nth_conts_eq, ⟨rfl⟩⟩ : ∃ conts, g.continuants (n + 1) = conts ∧ conts.b = predB, from exists_conts_b_of_denom succ_nth_denom_eq, rw [denom_eq_conts_b, (continuants_recurrence succ_nth_s_eq nth_conts_eq succ_nth_conts_eq)] end end generalized_continued_fraction
9e8bc2860416d415c47e2f11ecd73dfb52ebbc2c
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/implicitApplyIssue.lean
f6a03865900b1fd930b083e05dda7cd2bb9268ba
[ "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
848
lean
def Set α := α → Prop class HasMem (α : outParam $ Type u) (β : Type v) where mem : α → β → Prop infix:50 " ∈ " => HasMem.mem instance {α : Type u} : HasMem α (Set α) := ⟨λ a s => s a⟩ instance {α : Type u} : LE (Set α) := ⟨λ s t => ∀ {x : α}, x ∈ s → x ∈ t⟩ class HasInf (P : Type u) where inf : P → P → P infix:70 " ⊓ " => HasInf.inf instance {α : Type u} : HasInf (Set α) := ⟨λ s t x => x ∈ s ∧ x ∈ t⟩ theorem infLeLeft {s t : Set α} : s ⊓ t ≤ s := And.left theorem infLeRight {s t : Set α} : s ⊓ t ≤ t := And.right theorem inter_mem_sets_iff {α : Type u} (f : Set (Set α)) (hf : ∀ {s t}, s ∈ f → s ≤ t → t ∈ f) {x y : Set α} : x ⊓ y ∈ f → x ∈ f ∧ y ∈ f := by intro h refine ⟨hf h infLeLeft, hf h ?_⟩ apply infLeRight
29e69d48d988c057ac3415fd668e89a5c715e571
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/field_theory/polynomial_galois_group.lean
ae71bd2348ebe5da46b1ae78086e878b626af98e
[ "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
21,463
lean
/- Copyright (c) 2020 Thomas Browning, Patrick Lutz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning, Patrick Lutz -/ import analysis.complex.polynomial import field_theory.galois import group_theory.perm.cycle_type import ring_theory.eisenstein_criterion /-! # Galois Groups of Polynomials In this file, we introduce the Galois group of a polynomial `p` over a field `F`, defined as the automorphism group of its splitting field. We also provide some results about some extension `E` above `p.splitting_field`, and some specific results about the Galois groups of ℚ-polynomials with specific numbers of non-real roots. ## Main definitions - `polynomial.gal p`: the Galois group of a polynomial p. - `polynomial.gal.restrict p E`: the restriction homomorphism `(E ≃ₐ[F] E) → gal p`. - `polynomial.gal.gal_action p E`: the action of `gal p` on the roots of `p` in `E`. ## Main results - `polynomial.gal.restrict_smul`: `restrict p E` is compatible with `gal_action p E`. - `polynomial.gal.gal_action_hom_injective`: `gal p` acting on the roots of `p` in `E` is faithful. - `polynomial.gal.restrict_prod_injective`: `gal (p * q)` embeds as a subgroup of `gal p × gal q`. - `polynomial.gal.card_of_separable`: For a separable polynomial, its Galois group has cardinality equal to the dimension of its splitting field over `F`. - `polynomial.gal.gal_action_hom_bijective_of_prime_degree`: An irreducible polynomial of prime degree with two non-real roots has full Galois group. ## Other results - `polynomial.gal.card_complex_roots_eq_card_real_add_card_not_gal_inv`: The number of complex roots equals the number of real roots plus the number of roots not fixed by complex conjugation (i.e. with some imaginary component). -/ noncomputable theory open_locale classical open finite_dimensional namespace polynomial variables {F : Type*} [field F] (p q : polynomial F) (E : Type*) [field E] [algebra F E] /-- The Galois group of a polynomial. -/ @[derive [has_coe_to_fun, group, fintype]] def gal := p.splitting_field ≃ₐ[F] p.splitting_field namespace gal @[ext] lemma ext {σ τ : p.gal} (h : ∀ x ∈ p.root_set p.splitting_field, σ x = τ x) : σ = τ := begin refine alg_equiv.ext (λ x, (alg_hom.mem_equalizer σ.to_alg_hom τ.to_alg_hom x).mp ((set_like.ext_iff.mp _ x).mpr algebra.mem_top)), rwa [eq_top_iff, ←splitting_field.adjoin_roots, algebra.adjoin_le_iff], end /-- If `p` splits in `F` then the `p.gal` is trivial. -/ def unique_gal_of_splits (h : p.splits (ring_hom.id F)) : unique p.gal := { default := 1, uniq := λ f, alg_equiv.ext (λ x, by { obtain ⟨y, rfl⟩ := algebra.mem_bot.mp ((set_like.ext_iff.mp ((is_splitting_field.splits_iff _ p).mp h) x).mp algebra.mem_top), rw [alg_equiv.commutes, alg_equiv.commutes] }) } instance [h : fact (p.splits (ring_hom.id F))] : unique p.gal := unique_gal_of_splits _ (h.1) instance unique_gal_zero : unique (0 : polynomial F).gal := unique_gal_of_splits _ (splits_zero _) instance unique_gal_one : unique (1 : polynomial F).gal := unique_gal_of_splits _ (splits_one _) instance unique_gal_C (x : F) : unique (C x).gal := unique_gal_of_splits _ (splits_C _ _) instance unique_gal_X : unique (X : polynomial F).gal := unique_gal_of_splits _ (splits_X _) instance unique_gal_X_sub_C (x : F) : unique (X - C x).gal := unique_gal_of_splits _ (splits_X_sub_C _) instance unique_gal_X_pow (n : ℕ) : unique (X ^ n : polynomial F).gal := unique_gal_of_splits _ (splits_X_pow _ _) instance [h : fact (p.splits (algebra_map F E))] : algebra p.splitting_field E := (is_splitting_field.lift p.splitting_field p h.1).to_ring_hom.to_algebra instance [h : fact (p.splits (algebra_map F E))] : is_scalar_tower F p.splitting_field E := is_scalar_tower.of_algebra_map_eq (λ x, ((is_splitting_field.lift p.splitting_field p h.1).commutes x).symm) /-- Restrict from a superfield automorphism into a member of `gal p`. -/ def restrict [fact (p.splits (algebra_map F E))] : (E ≃ₐ[F] E) →* p.gal := alg_equiv.restrict_normal_hom p.splitting_field lemma restrict_surjective [fact (p.splits (algebra_map F E))] [normal F E] : function.surjective (restrict p E) := alg_equiv.restrict_normal_hom_surjective E section roots_action /-- The function taking `roots p p.splitting_field` to `roots p E`. This is actually a bijection, see `polynomial.gal.map_roots_bijective`. -/ def map_roots [fact (p.splits (algebra_map F E))] : root_set p p.splitting_field → root_set p E := λ x, ⟨is_scalar_tower.to_alg_hom F p.splitting_field E x, begin have key := subtype.mem x, by_cases p = 0, { simp only [h, root_set_zero] at key, exact false.rec _ key }, { rw [mem_root_set h, aeval_alg_hom_apply, (mem_root_set h).mp key, alg_hom.map_zero] } end⟩ lemma map_roots_bijective [h : fact (p.splits (algebra_map F E))] : function.bijective (map_roots p E) := begin split, { exact λ _ _ h, subtype.ext (ring_hom.injective _ (subtype.ext_iff.mp h)) }, { intro y, -- this is just an equality of two different ways to write the roots of `p` as an `E`-polynomial have key := roots_map (is_scalar_tower.to_alg_hom F p.splitting_field E : p.splitting_field →+* E) ((splits_id_iff_splits _).mpr (is_splitting_field.splits p.splitting_field p)), rw [map_map, alg_hom.comp_algebra_map] at key, have hy := subtype.mem y, simp only [root_set, finset.mem_coe, multiset.mem_to_finset, key, multiset.mem_map] at hy, rcases hy with ⟨x, hx1, hx2⟩, exact ⟨⟨x, multiset.mem_to_finset.mpr hx1⟩, subtype.ext hx2⟩ } end /-- The bijection between `root_set p p.splitting_field` and `root_set p E`. -/ def roots_equiv_roots [fact (p.splits (algebra_map F E))] : (root_set p p.splitting_field) ≃ (root_set p E) := equiv.of_bijective (map_roots p E) (map_roots_bijective p E) instance gal_action_aux : mul_action p.gal (root_set p p.splitting_field) := { smul := λ ϕ x, ⟨ϕ x, begin have key := subtype.mem x, --simp only [root_set, finset.mem_coe, multiset.mem_to_finset] at *, by_cases p = 0, { simp only [h, root_set_zero] at key, exact false.rec _ key }, { rw mem_root_set h, change aeval (ϕ.to_alg_hom x) p = 0, rw [aeval_alg_hom_apply, (mem_root_set h).mp key, alg_hom.map_zero] } end⟩, one_smul := λ _, by { ext, refl }, mul_smul := λ _ _ _, by { ext, refl } } /-- The action of `gal p` on the roots of `p` in `E`. -/ instance gal_action [fact (p.splits (algebra_map F E))] : mul_action p.gal (root_set p E) := { smul := λ ϕ x, roots_equiv_roots p E (ϕ • ((roots_equiv_roots p E).symm x)), one_smul := λ _, by simp only [equiv.apply_symm_apply, one_smul], mul_smul := λ _ _ _, by simp only [equiv.apply_symm_apply, equiv.symm_apply_apply, mul_smul] } variables {p E} /-- `polynomial.gal.restrict p E` is compatible with `polynomial.gal.gal_action p E`. -/ @[simp] lemma restrict_smul [fact (p.splits (algebra_map F E))] (ϕ : E ≃ₐ[F] E) (x : root_set p E) : ↑((restrict p E ϕ) • x) = ϕ x := begin let ψ := alg_equiv.of_injective_field (is_scalar_tower.to_alg_hom F p.splitting_field E), change ↑(ψ (ψ.symm _)) = ϕ x, rw alg_equiv.apply_symm_apply ψ, change ϕ (roots_equiv_roots p E ((roots_equiv_roots p E).symm x)) = ϕ x, rw equiv.apply_symm_apply (roots_equiv_roots p E), end variables (p E) /-- `polynomial.gal.gal_action` as a permutation representation -/ def gal_action_hom [fact (p.splits (algebra_map F E))] : p.gal →* equiv.perm (root_set p E) := { to_fun := λ ϕ, equiv.mk (λ x, ϕ • x) (λ x, ϕ⁻¹ • x) (λ x, inv_smul_smul ϕ x) (λ x, smul_inv_smul ϕ x), map_one' := by { ext1 x, exact mul_action.one_smul x }, map_mul' := λ x y, by { ext1 z, exact mul_action.mul_smul x y z } } lemma gal_action_hom_restrict [fact (p.splits (algebra_map F E))] (ϕ : E ≃ₐ[F] E) (x : root_set p E) : ↑(gal_action_hom p E (restrict p E ϕ) x) = ϕ x := restrict_smul ϕ x /-- `gal p` embeds as a subgroup of permutations of the roots of `p` in `E`. -/ lemma gal_action_hom_injective [fact (p.splits (algebra_map F E))] : function.injective (gal_action_hom p E) := begin rw monoid_hom.injective_iff, intros ϕ hϕ, ext x hx, have key := equiv.perm.ext_iff.mp hϕ (roots_equiv_roots p E ⟨x, hx⟩), change roots_equiv_roots p E (ϕ • (roots_equiv_roots p E).symm (roots_equiv_roots p E ⟨x, hx⟩)) = roots_equiv_roots p E ⟨x, hx⟩ at key, rw equiv.symm_apply_apply at key, exact subtype.ext_iff.mp (equiv.injective (roots_equiv_roots p E) key), end end roots_action variables {p q} /-- `polynomial.gal.restrict`, when both fields are splitting fields of polynomials. -/ def restrict_dvd (hpq : p ∣ q) : q.gal →* p.gal := if hq : q = 0 then 1 else @restrict F _ p _ _ _ ⟨splits_of_splits_of_dvd (algebra_map F q.splitting_field) hq (splitting_field.splits q) hpq⟩ lemma restrict_dvd_surjective (hpq : p ∣ q) (hq : q ≠ 0) : function.surjective (restrict_dvd hpq) := by simp only [restrict_dvd, dif_neg hq, restrict_surjective] variables (p q) /-- The Galois group of a product maps into the product of the Galois groups. -/ def restrict_prod : (p * q).gal →* p.gal × q.gal := monoid_hom.prod (restrict_dvd (dvd_mul_right p q)) (restrict_dvd (dvd_mul_left q p)) /-- `polynomial.gal.restrict_prod` is actually a subgroup embedding. -/ lemma restrict_prod_injective : function.injective (restrict_prod p q) := begin by_cases hpq : (p * q) = 0, { haveI : unique (p * q).gal := by { rw hpq, apply_instance }, exact λ f g h, eq.trans (unique.eq_default f) (unique.eq_default g).symm }, intros f g hfg, dsimp only [restrict_prod, restrict_dvd] at hfg, simp only [dif_neg hpq, monoid_hom.prod_apply, prod.mk.inj_iff] at hfg, ext x hx, rw [root_set, map_mul, polynomial.roots_mul] at hx, cases multiset.mem_add.mp (multiset.mem_to_finset.mp hx) with h h, { haveI : fact (p.splits (algebra_map F (p * q).splitting_field)) := ⟨splits_of_splits_of_dvd _ hpq (splitting_field.splits (p * q)) (dvd_mul_right p q)⟩, have key : x = algebra_map (p.splitting_field) (p * q).splitting_field ((roots_equiv_roots p _).inv_fun ⟨x, multiset.mem_to_finset.mpr h⟩) := subtype.ext_iff.mp (equiv.apply_symm_apply (roots_equiv_roots p _) ⟨x, _⟩).symm, rw [key, ←alg_equiv.restrict_normal_commutes, ←alg_equiv.restrict_normal_commutes], exact congr_arg _ (alg_equiv.ext_iff.mp hfg.1 _) }, { haveI : fact (q.splits (algebra_map F (p * q).splitting_field)) := ⟨splits_of_splits_of_dvd _ hpq (splitting_field.splits (p * q)) (dvd_mul_left q p)⟩, have key : x = algebra_map (q.splitting_field) (p * q).splitting_field ((roots_equiv_roots q _).inv_fun ⟨x, multiset.mem_to_finset.mpr h⟩) := subtype.ext_iff.mp (equiv.apply_symm_apply (roots_equiv_roots q _) ⟨x, _⟩).symm, rw [key, ←alg_equiv.restrict_normal_commutes, ←alg_equiv.restrict_normal_commutes], exact congr_arg _ (alg_equiv.ext_iff.mp hfg.2 _) }, { rwa [ne.def, mul_eq_zero, map_eq_zero, map_eq_zero, ←mul_eq_zero] } end lemma mul_splits_in_splitting_field_of_mul {p₁ q₁ p₂ q₂ : polynomial F} (hq₁ : q₁ ≠ 0) (hq₂ : q₂ ≠ 0) (h₁ : p₁.splits (algebra_map F q₁.splitting_field)) (h₂ : p₂.splits (algebra_map F q₂.splitting_field)) : (p₁ * p₂).splits (algebra_map F (q₁ * q₂).splitting_field) := begin apply splits_mul, { rw ← (splitting_field.lift q₁ (splits_of_splits_of_dvd _ (mul_ne_zero hq₁ hq₂) (splitting_field.splits _) (dvd_mul_right q₁ q₂))).comp_algebra_map, exact splits_comp_of_splits _ _ h₁, }, { rw ← (splitting_field.lift q₂ (splits_of_splits_of_dvd _ (mul_ne_zero hq₁ hq₂) (splitting_field.splits _) (dvd_mul_left q₂ q₁))).comp_algebra_map, exact splits_comp_of_splits _ _ h₂, }, end /-- `p` splits in the splitting field of `p ∘ q`, for `q` non-constant. -/ lemma splits_in_splitting_field_of_comp (hq : q.nat_degree ≠ 0) : p.splits (algebra_map F (p.comp q).splitting_field) := begin let P : polynomial F → Prop := λ r, r.splits (algebra_map F (r.comp q).splitting_field), have key1 : ∀ {r : polynomial F}, irreducible r → P r, { intros r hr, by_cases hr' : nat_degree r = 0, { exact splits_of_nat_degree_le_one _ (le_trans (le_of_eq hr') zero_le_one) }, obtain ⟨x, hx⟩ := exists_root_of_splits _ (splitting_field.splits (r.comp q)) (λ h, hr' ((mul_eq_zero.mp (nat_degree_comp.symm.trans (nat_degree_eq_of_degree_eq_some h))).resolve_right hq)), rw [←aeval_def, aeval_comp] at hx, have h_normal : normal F (r.comp q).splitting_field := splitting_field.normal (r.comp q), have qx_int := normal.is_integral h_normal (aeval x q), exact splits_of_splits_of_dvd _ (minpoly.ne_zero qx_int) (normal.splits h_normal _) (dvd_symm_of_irreducible (minpoly.irreducible qx_int) hr (minpoly.dvd F _ hx)) }, have key2 : ∀ {p₁ p₂ : polynomial F}, P p₁ → P p₂ → P (p₁ * p₂), { intros p₁ p₂ hp₁ hp₂, by_cases h₁ : p₁.comp q = 0, { cases comp_eq_zero_iff.mp h₁ with h h, { rw [h, zero_mul], exact splits_zero _ }, { exact false.rec _ (hq (by rw [h.2, nat_degree_C])) } }, by_cases h₂ : p₂.comp q = 0, { cases comp_eq_zero_iff.mp h₂ with h h, { rw [h, mul_zero], exact splits_zero _ }, { exact false.rec _ (hq (by rw [h.2, nat_degree_C])) } }, have key := mul_splits_in_splitting_field_of_mul h₁ h₂ hp₁ hp₂, rwa ← mul_comp at key }, exact wf_dvd_monoid.induction_on_irreducible p (splits_zero _) (λ _, splits_of_is_unit _) (λ _ _ _ h, key2 (key1 h)), end /-- `polynomial.gal.restrict` for the composition of polynomials. -/ def restrict_comp (hq : q.nat_degree ≠ 0) : (p.comp q).gal →* p.gal := @restrict F _ p _ _ _ ⟨splits_in_splitting_field_of_comp p q hq⟩ lemma restrict_comp_surjective (hq : q.nat_degree ≠ 0) : function.surjective (restrict_comp p q hq) := by simp only [restrict_comp, restrict_surjective] variables {p q} /-- For a separable polynomial, its Galois group has cardinality equal to the dimension of its splitting field over `F`. -/ lemma card_of_separable (hp : p.separable) : fintype.card p.gal = finrank F p.splitting_field := begin haveI : is_galois F p.splitting_field := is_galois.of_separable_splitting_field hp, exact is_galois.card_aut_eq_finrank F p.splitting_field, end lemma prime_degree_dvd_card [char_zero F] (p_irr : irreducible p) (p_deg : p.nat_degree.prime) : p.nat_degree ∣ fintype.card p.gal := begin rw gal.card_of_separable p_irr.separable, have hp : p.degree ≠ 0 := λ h, nat.prime.ne_zero p_deg (nat_degree_eq_zero_iff_degree_le_zero.mpr (le_of_eq h)), let α : p.splitting_field := root_of_splits (algebra_map F p.splitting_field) (splitting_field.splits p) hp, have hα : is_integral F α := (is_algebraic_iff_is_integral F).mp (algebra.is_algebraic_of_finite α), use finite_dimensional.finrank F⟮α⟯ p.splitting_field, suffices : (minpoly F α).nat_degree = p.nat_degree, { rw [←finite_dimensional.finrank_mul_finrank F F⟮α⟯ p.splitting_field, intermediate_field.adjoin.finrank hα, this] }, suffices : minpoly F α ∣ p, { have key := dvd_symm_of_irreducible (minpoly.irreducible hα) p_irr this, apply le_antisymm, { exact nat_degree_le_of_dvd this p_irr.ne_zero }, { exact nat_degree_le_of_dvd key (minpoly.ne_zero hα) } }, apply minpoly.dvd F α, rw [aeval_def, map_root_of_splits _ (splitting_field.splits p) hp], end section rationals lemma splits_ℚ_ℂ {p : polynomial ℚ} : fact (p.splits (algebra_map ℚ ℂ)) := ⟨is_alg_closed.splits_codomain p⟩ local attribute [instance] splits_ℚ_ℂ /-- The number of complex roots equals the number of real roots plus the number of roots not fixed by complex conjugation (i.e. with some imaginary component). -/ lemma card_complex_roots_eq_card_real_add_card_not_gal_inv (p : polynomial ℚ) : (p.root_set ℂ).to_finset.card = (p.root_set ℝ).to_finset.card + (gal_action_hom p ℂ (restrict p ℂ (complex.conj_ae.restrict_scalars ℚ))).support.card := begin by_cases hp : p = 0, { simp_rw [hp, root_set_zero, set.to_finset_eq_empty_iff.mpr rfl, finset.card_empty, zero_add], refine eq.symm (nat.le_zero_iff.mp ((finset.card_le_univ _).trans (le_of_eq _))), simp_rw [hp, root_set_zero, fintype.card_eq_zero_iff], apply_instance }, have inj : function.injective (is_scalar_tower.to_alg_hom ℚ ℝ ℂ) := (algebra_map ℝ ℂ).injective, rw [←finset.card_image_of_injective _ subtype.coe_injective, ←finset.card_image_of_injective _ inj], let a : finset ℂ := _, let b : finset ℂ := _, let c : finset ℂ := _, change a.card = b.card + c.card, have ha : ∀ z : ℂ, z ∈ a ↔ aeval z p = 0 := λ z, by rw [set.mem_to_finset, mem_root_set hp], have hb : ∀ z : ℂ, z ∈ b ↔ aeval z p = 0 ∧ z.im = 0, { intro z, simp_rw [finset.mem_image, exists_prop, set.mem_to_finset, mem_root_set hp], split, { rintros ⟨w, hw, rfl⟩, exact ⟨by rw [aeval_alg_hom_apply, hw, alg_hom.map_zero], rfl⟩ }, { rintros ⟨hz1, hz2⟩, have key : is_scalar_tower.to_alg_hom ℚ ℝ ℂ z.re = z := by { ext, refl, rw hz2, refl }, exact ⟨z.re, inj (by rwa [←aeval_alg_hom_apply, key, alg_hom.map_zero]), key⟩ } }, have hc0 : ∀ w : p.root_set ℂ, gal_action_hom p ℂ (restrict p ℂ (complex.conj_ae.restrict_scalars ℚ)) w = w ↔ w.val.im = 0, { intro w, rw [subtype.ext_iff, gal_action_hom_restrict], exact complex.eq_conj_iff_im }, have hc : ∀ z : ℂ, z ∈ c ↔ aeval z p = 0 ∧ z.im ≠ 0, { intro z, simp_rw [finset.mem_image, exists_prop], split, { rintros ⟨w, hw, rfl⟩, exact ⟨(mem_root_set hp).mp w.2, mt (hc0 w).mpr (equiv.perm.mem_support.mp hw)⟩ }, { rintros ⟨hz1, hz2⟩, exact ⟨⟨z, (mem_root_set hp).mpr hz1⟩, equiv.perm.mem_support.mpr (mt (hc0 _).mp hz2), rfl⟩ } }, rw ← finset.card_disjoint_union, { apply congr_arg finset.card, simp_rw [finset.ext_iff, finset.mem_union, ha, hb, hc], tauto }, { intro z, rw [finset.inf_eq_inter, finset.mem_inter, hb, hc], tauto }, { apply_instance }, end /-- An irreducible polynomial of prime degree with two non-real roots has full Galois group. -/ lemma gal_action_hom_bijective_of_prime_degree {p : polynomial ℚ} (p_irr : irreducible p) (p_deg : p.nat_degree.prime) (p_roots : fintype.card (p.root_set ℂ) = fintype.card (p.root_set ℝ) + 2) : function.bijective (gal_action_hom p ℂ) := begin have h1 : fintype.card (p.root_set ℂ) = p.nat_degree, { simp_rw [root_set_def, finset.coe_sort_coe, fintype.card_coe], rw [multiset.to_finset_card_of_nodup, ←nat_degree_eq_card_roots], { exact is_alg_closed.splits_codomain p }, { exact nodup_roots ((separable_map (algebra_map ℚ ℂ)).mpr p_irr.separable) } }, have h2 : fintype.card p.gal = fintype.card (gal_action_hom p ℂ).range := fintype.card_congr (monoid_hom.of_injective (gal_action_hom_injective p ℂ)).to_equiv, let conj := restrict p ℂ (complex.conj_ae.restrict_scalars ℚ), refine ⟨gal_action_hom_injective p ℂ, λ x, (congr_arg (has_mem.mem x) (show (gal_action_hom p ℂ).range = ⊤, from _)).mpr (subgroup.mem_top x)⟩, apply equiv.perm.subgroup_eq_top_of_swap_mem, { rwa h1 }, { rw [h1, ←h2], exact prime_degree_dvd_card p_irr p_deg }, { exact ⟨conj, rfl⟩ }, { rw ← equiv.perm.card_support_eq_two, apply nat.add_left_cancel, rw [←p_roots, ←set.to_finset_card (root_set p ℝ), ←set.to_finset_card (root_set p ℂ)], exact (card_complex_roots_eq_card_real_add_card_not_gal_inv p).symm }, end /-- An irreducible polynomial of prime degree with 1-3 non-real roots has full Galois group. -/ lemma gal_action_hom_bijective_of_prime_degree' {p : polynomial ℚ} (p_irr : irreducible p) (p_deg : p.nat_degree.prime) (p_roots1 : fintype.card (p.root_set ℝ) + 1 ≤ fintype.card (p.root_set ℂ)) (p_roots2 : fintype.card (p.root_set ℂ) ≤ fintype.card (p.root_set ℝ) + 3) : function.bijective (gal_action_hom p ℂ) := begin apply gal_action_hom_bijective_of_prime_degree p_irr p_deg, let n := (gal_action_hom p ℂ (restrict p ℂ (complex.conj_ae.restrict_scalars ℚ))).support.card, have hn : 2 ∣ n := equiv.perm.two_dvd_card_support (by rw [←monoid_hom.map_pow, ←monoid_hom.map_pow, show alg_equiv.restrict_scalars ℚ complex.conj_ae ^ 2 = 1, from alg_equiv.ext complex.conj_conj, monoid_hom.map_one, monoid_hom.map_one]), have key := card_complex_roots_eq_card_real_add_card_not_gal_inv p, simp_rw [set.to_finset_card] at key, rw [key, add_le_add_iff_left] at p_roots1 p_roots2, rw [key, add_right_inj], suffices : ∀ m : ℕ, 2 ∣ m → 1 ≤ m → m ≤ 3 → m = 2, { exact this n hn p_roots1 p_roots2 }, rintros m ⟨k, rfl⟩ h2 h3, exact le_antisymm (nat.lt_succ_iff.mp (lt_of_le_of_ne h3 (show 2 * k ≠ 2 * 1 + 1, from nat.two_mul_ne_two_mul_add_one))) (nat.succ_le_iff.mpr (lt_of_le_of_ne h2 (show 2 * 0 + 1 ≠ 2 * k, from nat.two_mul_ne_two_mul_add_one.symm))), end end rationals end gal end polynomial
2f5cb89428fd02c2f654f2e38119dd41536bb789
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/int/basic_auto.lean
f7957cacdcb629f9f058c53529db357ec2b191fa
[]
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
13,527
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad The integers, with addition, multiplication, and subtraction. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.data.nat.lemmas import Mathlib.Lean3Lib.init.data.nat.gcd universes l namespace Mathlib /- the type, coercions, and notation -/ not found notation:1024 "ℤ" => Mathlib.int protected instance int.has_coe : has_coe ℕ ℤ := has_coe.mk Int.ofNat protected def int.repr : ℤ → string := sorry protected instance int.has_repr : has_repr ℤ := has_repr.mk int.repr protected instance int.has_to_string : has_to_string ℤ := has_to_string.mk int.repr namespace int protected theorem coe_nat_eq (n : ℕ) : ↑n = Int.ofNat n := rfl protected def zero : ℤ := Int.ofNat 0 protected def one : ℤ := Int.ofNat 1 protected instance has_zero : HasZero ℤ := { zero := int.zero } protected instance has_one : HasOne ℤ := { one := int.one } theorem of_nat_zero : Int.ofNat 0 = 0 := rfl theorem of_nat_one : Int.ofNat 1 = 1 := rfl /- definitions of basic functions -/ def neg_of_nat : ℕ → ℤ := sorry def sub_nat_nat (m : ℕ) (n : ℕ) : ℤ := sorry theorem sub_nat_nat_of_sub_eq_zero {m : ℕ} {n : ℕ} (h : n - m = 0) : sub_nat_nat m n = Int.ofNat (m - n) := sorry theorem sub_nat_nat_of_sub_eq_succ {m : ℕ} {n : ℕ} {k : ℕ} (h : n - m = Nat.succ k) : sub_nat_nat m n = Int.negSucc k := sorry protected def neg : ℤ → ℤ := sorry protected def add : ℤ → ℤ → ℤ := sorry protected def mul : ℤ → ℤ → ℤ := sorry protected instance has_neg : Neg ℤ := { neg := int.neg } protected instance has_add : Add ℤ := { add := int.add } protected instance has_mul : Mul ℤ := { mul := int.mul } -- defeq to algebra.sub which gives subtraction for arbitrary `add_group`s protected def sub : ℤ → ℤ → ℤ := fun (m n : ℤ) => m + -n protected instance has_sub : Sub ℤ := { sub := int.sub } protected theorem neg_zero : -0 = 0 := rfl theorem of_nat_add (n : ℕ) (m : ℕ) : Int.ofNat (n + m) = Int.ofNat n + Int.ofNat m := rfl theorem of_nat_mul (n : ℕ) (m : ℕ) : Int.ofNat (n * m) = Int.ofNat n * Int.ofNat m := rfl theorem of_nat_succ (n : ℕ) : Int.ofNat (Nat.succ n) = Int.ofNat n + 1 := rfl theorem neg_of_nat_zero : -Int.ofNat 0 = 0 := rfl theorem neg_of_nat_of_succ (n : ℕ) : -Int.ofNat (Nat.succ n) = Int.negSucc n := rfl theorem neg_neg_of_nat_succ (n : ℕ) : -Int.negSucc n = Int.ofNat (Nat.succ n) := rfl theorem of_nat_eq_coe (n : ℕ) : Int.ofNat n = ↑n := rfl theorem neg_succ_of_nat_coe (n : ℕ) : Int.negSucc n = -↑(n + 1) := rfl protected theorem coe_nat_add (m : ℕ) (n : ℕ) : ↑(m + n) = ↑m + ↑n := rfl protected theorem coe_nat_mul (m : ℕ) (n : ℕ) : ↑(m * n) = ↑m * ↑n := rfl protected theorem coe_nat_zero : ↑0 = 0 := rfl protected theorem coe_nat_one : ↑1 = 1 := rfl protected theorem coe_nat_succ (n : ℕ) : ↑(Nat.succ n) = ↑n + 1 := rfl protected theorem coe_nat_add_out (m : ℕ) (n : ℕ) : ↑m + ↑n = ↑m + ↑n := rfl protected theorem coe_nat_mul_out (m : ℕ) (n : ℕ) : ↑m * ↑n = ↑(m * n) := rfl protected theorem coe_nat_add_one_out (n : ℕ) : ↑n + 1 = ↑(Nat.succ n) := rfl /- these are only for internal use -/ theorem of_nat_add_of_nat (m : ℕ) (n : ℕ) : Int.ofNat m + Int.ofNat n = Int.ofNat (m + n) := rfl theorem of_nat_add_neg_succ_of_nat (m : ℕ) (n : ℕ) : Int.ofNat m + Int.negSucc n = sub_nat_nat m (Nat.succ n) := rfl theorem neg_succ_of_nat_add_of_nat (m : ℕ) (n : ℕ) : Int.negSucc m + Int.ofNat n = sub_nat_nat n (Nat.succ m) := rfl theorem neg_succ_of_nat_add_neg_succ_of_nat (m : ℕ) (n : ℕ) : Int.negSucc m + Int.negSucc n = Int.negSucc (Nat.succ (m + n)) := rfl theorem of_nat_mul_of_nat (m : ℕ) (n : ℕ) : Int.ofNat m * Int.ofNat n = Int.ofNat (m * n) := rfl theorem of_nat_mul_neg_succ_of_nat (m : ℕ) (n : ℕ) : Int.ofNat m * Int.negSucc n = neg_of_nat (m * Nat.succ n) := rfl theorem neg_succ_of_nat_of_nat (m : ℕ) (n : ℕ) : Int.negSucc m * Int.ofNat n = neg_of_nat (Nat.succ m * n) := rfl theorem mul_neg_succ_of_nat_neg_succ_of_nat (m : ℕ) (n : ℕ) : Int.negSucc m * Int.negSucc n = Int.ofNat (Nat.succ m * Nat.succ n) := rfl /- some basic functions and properties -/ protected theorem coe_nat_inj {m : ℕ} {n : ℕ} (h : ↑m = ↑n) : m = n := of_nat.inj h theorem of_nat_eq_of_nat_iff (m : ℕ) (n : ℕ) : Int.ofNat m = Int.ofNat n ↔ m = n := { mp := of_nat.inj, mpr := congr_arg fun (m : ℕ) => Int.ofNat m } protected theorem coe_nat_eq_coe_nat_iff (m : ℕ) (n : ℕ) : ↑m = ↑n ↔ m = n := of_nat_eq_of_nat_iff m n theorem neg_succ_of_nat_inj_iff {m : ℕ} {n : ℕ} : Int.negSucc m = Int.negSucc n ↔ m = n := sorry theorem neg_succ_of_nat_eq (n : ℕ) : Int.negSucc n = -(↑n + 1) := rfl /- neg -/ protected theorem neg_neg (a : ℤ) : --a = a := sorry protected theorem neg_inj {a : ℤ} {b : ℤ} (h : -a = -b) : a = b := eq.mpr (id (Eq._oldrec (Eq.refl (a = b)) (Eq.symm (int.neg_neg a)))) (eq.mpr (id (Eq._oldrec (Eq.refl ( --a = b)) (Eq.symm (int.neg_neg b)))) (eq.mpr (id (Eq._oldrec (Eq.refl ( --a = --b)) h)) (Eq.refl ( --b)))) protected theorem sub_eq_add_neg {a : ℤ} {b : ℤ} : a - b = a + -b := rfl /- basic properties of sub_nat_nat -/ theorem sub_nat_nat_elim (m : ℕ) (n : ℕ) (P : ℕ → ℕ → ℤ → Prop) (hp : ∀ (i n : ℕ), P (n + i) n (Int.ofNat i)) (hn : ∀ (i m : ℕ), P m (m + i + 1) (Int.negSucc i)) : P m n (sub_nat_nat m n) := sorry theorem sub_nat_nat_add_left {m : ℕ} {n : ℕ} : sub_nat_nat (m + n) m = Int.ofNat n := sorry theorem sub_nat_nat_add_right {m : ℕ} {n : ℕ} : sub_nat_nat m (m + n + 1) = Int.negSucc n := sorry theorem sub_nat_nat_add_add (m : ℕ) (n : ℕ) (k : ℕ) : sub_nat_nat (m + k) (n + k) = sub_nat_nat m n := sorry theorem sub_nat_nat_of_ge {m : ℕ} {n : ℕ} (h : m ≥ n) : sub_nat_nat m n = Int.ofNat (m - n) := sub_nat_nat_of_sub_eq_zero (nat.sub_eq_zero_of_le h) theorem sub_nat_nat_of_lt {m : ℕ} {n : ℕ} (h : m < n) : sub_nat_nat m n = Int.negSucc (Nat.pred (n - m)) := sorry /- nat_abs -/ @[simp] def nat_abs : ℤ → ℕ := sorry theorem nat_abs_of_nat (n : ℕ) : nat_abs ↑n = n := rfl theorem eq_zero_of_nat_abs_eq_zero {a : ℤ} : nat_abs a = 0 → a = 0 := sorry theorem nat_abs_pos_of_ne_zero {a : ℤ} (h : a ≠ 0) : nat_abs a > 0 := or.resolve_left (nat.eq_zero_or_pos (nat_abs a)) (mt eq_zero_of_nat_abs_eq_zero h) theorem nat_abs_zero : nat_abs 0 = 0 := rfl theorem nat_abs_one : nat_abs 1 = 1 := rfl theorem nat_abs_mul_self {a : ℤ} : ↑(nat_abs a * nat_abs a) = a * a := sorry @[simp] theorem nat_abs_neg (a : ℤ) : nat_abs (-a) = nat_abs a := sorry theorem nat_abs_eq (a : ℤ) : a = ↑(nat_abs a) ∨ a = -↑(nat_abs a) := sorry theorem eq_coe_or_neg (a : ℤ) : ∃ (n : ℕ), a = ↑n ∨ a = -↑n := Exists.intro (nat_abs a) (nat_abs_eq a) /- sign -/ def sign : ℤ → ℤ := sorry @[simp] theorem sign_zero : sign 0 = 0 := rfl @[simp] theorem sign_one : sign 1 = 1 := rfl @[simp] theorem sign_neg_one : sign (-1) = -1 := rfl /- Quotient and remainder -/ -- There are three main conventions for integer division, -- referred here as the E, F, T rounding conventions. -- All three pairs satisfy the identity x % y + (x / y) * y = x -- unconditionally. -- E-rounding: This pair satisfies 0 ≤ mod x y < nat_abs y for y ≠ 0 protected def div : ℤ → ℤ → ℤ := sorry protected def mod : ℤ → ℤ → ℤ := sorry -- F-rounding: This pair satisfies fdiv x y = floor (x / y) def fdiv : ℤ → ℤ → ℤ := sorry def fmod : ℤ → ℤ → ℤ := sorry -- T-rounding: This pair satisfies quot x y = round_to_zero (x / y) def quot : ℤ → ℤ → ℤ := sorry def rem : ℤ → ℤ → ℤ := sorry protected instance has_div : Div ℤ := { div := int.div } protected instance has_mod : Mod ℤ := { mod := int.mod } /- gcd -/ def gcd (m : ℤ) (n : ℤ) : ℕ := nat.gcd (nat_abs m) (nat_abs n) /- int is a ring -/ /- addition -/ protected theorem add_comm (a : ℤ) (b : ℤ) : a + b = b + a := sorry protected theorem add_zero (a : ℤ) : a + 0 = a := int.cases_on a (fun (a : ℕ) => idRhs (Int.ofNat a + 0 = Int.ofNat a + 0) rfl) fun (a : ℕ) => idRhs (Int.negSucc a + 0 = Int.negSucc a + 0) rfl protected theorem zero_add (a : ℤ) : 0 + a = a := int.add_comm a 0 ▸ int.add_zero a theorem sub_nat_nat_sub {m : ℕ} {n : ℕ} (h : m ≥ n) (k : ℕ) : sub_nat_nat (m - n) k = sub_nat_nat m (k + n) := sorry theorem sub_nat_nat_add (m : ℕ) (n : ℕ) (k : ℕ) : sub_nat_nat (m + n) k = Int.ofNat m + sub_nat_nat n k := sorry theorem sub_nat_nat_add_neg_succ_of_nat (m : ℕ) (n : ℕ) (k : ℕ) : sub_nat_nat m n + Int.negSucc k = sub_nat_nat m (n + Nat.succ k) := sorry theorem add_assoc_aux1 (m : ℕ) (n : ℕ) (c : ℤ) : Int.ofNat m + Int.ofNat n + c = Int.ofNat m + (Int.ofNat n + c) := sorry theorem add_assoc_aux2 (m : ℕ) (n : ℕ) (k : ℕ) : Int.negSucc m + Int.negSucc n + Int.ofNat k = Int.negSucc m + (Int.negSucc n + Int.ofNat k) := sorry protected theorem add_assoc (a : ℤ) (b : ℤ) (c : ℤ) : a + b + c = a + (b + c) := sorry /- negation -/ theorem sub_nat_self (n : ℕ) : sub_nat_nat n n = 0 := sorry protected theorem add_left_neg (a : ℤ) : -a + a = 0 := sorry protected theorem add_right_neg (a : ℤ) : a + -a = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (a + -a = 0)) (int.add_comm a (-a)))) (eq.mpr (id (Eq._oldrec (Eq.refl (-a + a = 0)) (int.add_left_neg a))) (Eq.refl 0)) /- multiplication -/ protected theorem mul_comm (a : ℤ) (b : ℤ) : a * b = b * a := sorry theorem of_nat_mul_neg_of_nat (m : ℕ) (n : ℕ) : Int.ofNat m * neg_of_nat n = neg_of_nat (m * n) := sorry theorem neg_of_nat_mul_of_nat (m : ℕ) (n : ℕ) : neg_of_nat m * Int.ofNat n = neg_of_nat (m * n) := sorry theorem neg_succ_of_nat_mul_neg_of_nat (m : ℕ) (n : ℕ) : Int.negSucc m * neg_of_nat n = Int.ofNat (Nat.succ m * n) := sorry theorem neg_of_nat_mul_neg_succ_of_nat (m : ℕ) (n : ℕ) : neg_of_nat n * Int.negSucc m = Int.ofNat (n * Nat.succ m) := sorry protected theorem mul_assoc (a : ℤ) (b : ℤ) (c : ℤ) : a * b * c = a * (b * c) := sorry protected theorem mul_zero (a : ℤ) : a * 0 = 0 := int.cases_on a (fun (a : ℕ) => idRhs (Int.ofNat a * 0 = Int.ofNat a * 0) rfl) fun (a : ℕ) => idRhs (Int.negSucc a * 0 = Int.negSucc a * 0) rfl protected theorem zero_mul (a : ℤ) : 0 * a = 0 := int.mul_comm a 0 ▸ int.mul_zero a theorem neg_of_nat_eq_sub_nat_nat_zero (n : ℕ) : neg_of_nat n = sub_nat_nat 0 n := nat.cases_on n (idRhs (neg_of_nat 0 = neg_of_nat 0) rfl) fun (n : ℕ) => idRhs (neg_of_nat (Nat.succ n) = neg_of_nat (Nat.succ n)) rfl theorem of_nat_mul_sub_nat_nat (m : ℕ) (n : ℕ) (k : ℕ) : Int.ofNat m * sub_nat_nat n k = sub_nat_nat (m * n) (m * k) := sorry theorem neg_of_nat_add (m : ℕ) (n : ℕ) : neg_of_nat m + neg_of_nat n = neg_of_nat (m + n) := sorry theorem neg_succ_of_nat_mul_sub_nat_nat (m : ℕ) (n : ℕ) (k : ℕ) : Int.negSucc m * sub_nat_nat n k = sub_nat_nat (Nat.succ m * k) (Nat.succ m * n) := sorry protected theorem distrib_left (a : ℤ) (b : ℤ) (c : ℤ) : a * (b + c) = a * b + a * c := sorry protected theorem distrib_right (a : ℤ) (b : ℤ) (c : ℤ) : (a + b) * c = a * c + b * c := sorry protected theorem zero_ne_one : 0 ≠ 1 := fun (h : 0 = 1) => nat.succ_ne_zero 0 (Eq.symm (of_nat.inj h)) theorem of_nat_sub {n : ℕ} {m : ℕ} (h : m ≤ n) : Int.ofNat (n - m) = Int.ofNat n - Int.ofNat m := sorry protected theorem add_left_comm (a : ℤ) (b : ℤ) (c : ℤ) : a + (b + c) = b + (a + c) := eq.mpr (id (Eq._oldrec (Eq.refl (a + (b + c) = b + (a + c))) (Eq.symm (int.add_assoc a b c)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + b + c = b + (a + c))) (int.add_comm a b))) (eq.mpr (id (Eq._oldrec (Eq.refl (b + a + c = b + (a + c))) (int.add_assoc b a c))) (Eq.refl (b + (a + c))))) protected theorem add_left_cancel {a : ℤ} {b : ℤ} {c : ℤ} (h : a + b = a + c) : b = c := sorry protected theorem neg_add {a : ℤ} {b : ℤ} : -(a + b) = -a + -b := sorry theorem neg_succ_of_nat_coe' (n : ℕ) : Int.negSucc n = -↑n - 1 := eq.mpr (id (Eq._oldrec (Eq.refl (Int.negSucc n = -↑n - 1)) int.sub_eq_add_neg)) (eq.mpr (id (Eq._oldrec (Eq.refl (Int.negSucc n = -↑n + -1)) (Eq.symm int.neg_add))) (Eq.refl (Int.negSucc n))) protected theorem coe_nat_sub {n : ℕ} {m : ℕ} : n ≤ m → ↑(m - n) = ↑m - ↑n := of_nat_sub protected theorem sub_nat_nat_eq_coe {m : ℕ} {n : ℕ} : sub_nat_nat m n = ↑m - ↑n := sorry def to_nat : ℤ → ℕ := sorry theorem to_nat_sub (m : ℕ) (n : ℕ) : to_nat (↑m - ↑n) = m - n := sorry -- Since mod x y is always nonnegative when y ≠ 0, we can make a nat version of it def nat_mod (m : ℤ) (n : ℤ) : ℕ := to_nat (m % n) protected theorem one_mul (a : ℤ) : 1 * a = a := sorry protected theorem mul_one (a : ℤ) : a * 1 = a := eq.mpr (id (Eq._oldrec (Eq.refl (a * 1 = a)) (int.mul_comm a 1))) (eq.mpr (id (Eq._oldrec (Eq.refl (1 * a = a)) (int.one_mul a))) (Eq.refl a)) protected theorem neg_eq_neg_one_mul (a : ℤ) : -a = -1 * a := sorry theorem sign_mul_nat_abs (a : ℤ) : sign a * ↑(nat_abs a) = a := sorry end Mathlib
d212b81f892ba12387f10410aa70d2677c794e05
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/assoc_flat.lean
c10c5aa4c7ec877f9dfd854697481f3d0598cabb
[ "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
2,369
lean
open tactic expr constant nat.add_assoc (a b c : nat) : (a + b) + c = a + (b + c) 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₃ : expr ← return (app (app (app assoc a1) a2) rhs), -- H₃ is a proof for a1 + (a2 + rhs) = a1 + rhs1 H₄ : expr ← mk_app `congr_arg [app op a1, H₁], H₅ ← mk_app `eq.trans [H₃, H₄], H ← mk_app `eq.trans [H₅, H₂], return (new_app, H) | none := do new_app ← return $ app (app op e) rhs, H ← mk_app `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 ← mk_app `congr_arg [app op a1, H₁], H ← mk_app `eq.trans [H₃, H₂], return (new_app, H) | none := do pr ← mk_app `eq.refl [e], return (e, pr) end local infix `+` := 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, trace (prod.pr2 r), exact (prod.pr2 r) | none := failed end
6ddb23e8710cdc1c4ab2cb6d0664da7575cdf3e0
90bd8c2a52dbaaba588bdab57b155a7ec1532de0
/src/path/defs.lean
bd53b57b9a7b33846a274f1408d45175f0cbc641
[ "Apache-2.0" ]
permissive
shingtaklam1324/alg-top
fd434f1478925a232af45f18f370ee3a22811c54
4c88e28df6f0a329f26eab32bae023789193990e
refs/heads/master
1,689,447,024,947
1,630,073,400,000
1,630,073,400,000
381,528,689
2
0
null
null
null
null
UTF-8
Lean
false
false
3,874
lean
import topology.continuous_function.basic import topology.instances.real import topology.path_connected /-! # Paths Given a topological space `X`, and points `x₀ x₁ : X`, we define a `path' x₀ x₁` to be a continuous function `to_fun : C(ℝ, X)` where `to_fun 0 = x₀` and `to_fun 1 = x₁`. We define an API for a different version of paths as in mathlib (although it is possible to convert between the two fairly easily), since the mathlib version has `I → ℝ`, which can sometimes be unwieldy in our applications. -/ noncomputable theory variables {X Y : Type _} [topological_space X] [topological_space Y] /-- `path' x₀ x₁` is the type of all paths joining `x₀` and `x₁`. -/ @[ext] structure path' (x₀ x₁ : X) := (to_fun : C(ℝ, X)) (to_fun_zero' : to_fun 0 = x₀) (to_fun_one' : to_fun 1 = x₁) namespace path' variables {x₀ x₁ x₂ : X} (p : path' x₀ x₁) instance : has_coe_t (path' x₀ x₁) (C(ℝ, X)) := ⟨path'.to_fun⟩ instance : has_coe_to_fun (path' x₀ x₁) := ⟨_, λ p, p.to_fun.to_fun⟩ @[continuity] lemma continuous {f : path' x₀ x₁} : continuous f := f.to_fun.continuous_to_fun /-- Converting a `path` to a `path'`. -/ def of_path (q : path x₀ x₁) : path' x₀ x₁ := { to_fun := { to_fun := set.Icc_extend zero_le_one q }, to_fun_zero' := by simp, to_fun_one' := by simp } @[simp] lemma coe_apply (i : ℝ) : (p : C(ℝ, X)) i = p i := rfl @[simp] lemma to_fun_zero : p 0 = x₀ := p.to_fun_zero' @[simp] lemma to_fun_one : p 1 = x₁ := p.to_fun_one' /-- Converting a `path'` to a `path`. Note that this is a left inverse to `of_path`, but it is *not* a right inverse. -/ def to_path : path x₀ x₁ := { to_fun := λ t, p t, continuous' := by continuity, source' := by simp, target' := by simp } /-- The image of a path under a continuous function is a path. -/ def map (p : path' x₀ x₁) (f : C(X, Y)) : path' (f x₀) (f x₁) := { to_fun := f.comp p, to_fun_zero' := by simp, to_fun_one' := by simp } @[simp] lemma map_apply (p : path' x₀ x₁) (f : C(X, Y)) (t : ℝ) : (p.map f) t = (f.comp (p : C(ℝ, X))) t := rfl @[simp] lemma mk_apply (f : C(ℝ, X)) {h₁} {h₂} (t : ℝ) : (⟨f, h₁, h₂⟩ : path' x₀ x₁) t = f t := rfl /-- If `p₀` is a path from `x₀` to `x₁`, and `p₁` is a path from `x₁` to `x₂`, we can define a path from `x₀` to `x₂` by joining them together. -/ def trans (p₀ : path' x₀ x₁) (p₁ : path' x₁ x₂) : path' x₀ x₂ := { to_fun := { to_fun := λ s, if s ≤ 1/2 then p₀ (2 * s) else p₁ (2 * s - 1), continuous_to_fun := begin apply continuous.if; [rintros a (ha : a ∈ frontier (set.Iic (1/2 : ℝ))), continuity, continuity], simp * at * end }, to_fun_zero' := by simp, to_fun_one' := by norm_num } . @[simp] lemma map_trans (p₀ : path' x₀ x₁) (p₁ : path' x₁ x₂) (f : C(X, Y)) : (p₀.trans p₁).map f = (p₀.map f).trans (p₁.map f) := begin ext, simp only [trans, map, one_div, mk_apply, continuous_map.comp_coe, continuous_map.coe_mk, function.comp_app, coe_apply], split_ifs; refl end @[simp] lemma map_comp {Z : Type _} [topological_space Z] (p : path' x₀ x₁) (f : C(X, Y)) (g : C(Y, Z)) : (p.map f).map g = p.map (g.comp f) := rfl /-- The constant path in `x`. -/ def const (x : X) : path' x x := { to_fun := { to_fun := λ t, x }, to_fun_zero' := by simp, to_fun_one' := by simp } @[simp] lemma const_map (x : X) (f : C(X, Y)) : (const x).map f = const (f x) := rfl @[simp] lemma const_to_fun (x : X) (t : ℝ) : (const x) t = x := rfl /-- The inverse of a path is given by traversing the path in the opposite direction. -/ def inv (p : path' x₀ x₁) : path' x₁ x₀ := { to_fun := { to_fun := λ s, p (1 - s) }, to_fun_zero' := by norm_num, to_fun_one' := by norm_num } end path'
7fa13a6a926c0254fabfb73f162b2018fc27e2e1
37683ecbb27d7c2037bfd9ad7e06d662f460a005
/pointed_pi.hlean
446f3247b678666416b1ecec6884b1d687494ddf
[ "Apache-2.0" ]
permissive
GRSEB9S/Spectral-1
b2443b09cae7aac1247b1f88c846c532ac802b8e
dd14277e0bfc6270a488eb3b9ec231484065b9d8
refs/heads/master
1,631,315,269,407
1,522,048,315,000
1,522,799,803,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
45,544
hlean
/- Copyright (c) 2016 Ulrik Buchholtz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ulrik Buchholtz, Floris van Doorn -/ import homotopy.connectedness types.pointed2 .move_to_lib .pointed open eq pointed equiv sigma is_equiv trunc option pi function fiber /- In this file we define dependent pointed maps and properties of them. Using this, we give the truncation level of the type of pointed maps, giving the connectivity of the domain and the truncation level of the codomain. This is is_trunc_pmap_of_is_conn at the end. We also prove other properties about pointed (dependent maps), like the fact that (Π*a, F a) → (Π*a, X a) → (Π*a, B a) is a fibration sequence if (F a) → (X a) → B a) is. -/ namespace pointed /- the pointed type of unpointed (nondependent) maps -/ definition pumap [constructor] (A : Type) (B : Type*) : Type* := pointed.MK (A → B) (const A pt) /- the pointed type of unpointed dependent maps -/ definition pupi [constructor] {A : Type} (B : A → Type*) : Type* := pointed.MK (Πa, B a) (λa, pt) notation `Πᵘ*` binders `, ` r:(scoped P, pupi P) := r infix ` →ᵘ* `:30 := pumap /- stuff about the pointed type of unpointed maps (dependent and non-dependent) -/ definition sigma_pumap {A : Type} (B : A → Type) (C : Type*) : ((Σa, B a) →ᵘ* C) ≃* Πᵘ*a, B a →ᵘ* C := pequiv_of_equiv (equiv_sigma_rec _)⁻¹ᵉ idp definition phomotopy_mk_pupi [constructor] {A : Type*} {B : Type} {C : B → Type*} {f g : A →* (Πᵘ*b, C b)} (p : f ~2 g) (q : p pt ⬝hty apd10 (respect_pt g) ~ apd10 (respect_pt f)) : f ~* g := begin apply phomotopy.mk (λa, eq_of_homotopy (p a)), apply eq_of_fn_eq_fn eq_equiv_homotopy, apply eq_of_homotopy, intro b, refine !apd10_con ⬝ _, refine whisker_right _ !pi.apd10_eq_of_homotopy ⬝ q b end definition pupi_functor [constructor] {A A' : Type} {B : A → Type*} {B' : A' → Type*} (f : A' → A) (g : Πa, B (f a) →* B' a) : (Πᵘ*a, B a) →* (Πᵘ*a', B' a') := pmap.mk (pi_functor f g) (eq_of_homotopy (λa, respect_pt (g a))) definition pupi_functor_right [constructor] {A : Type} {B B' : A → Type*} (g : Πa, B a →* B' a) : (Πᵘ*a, B a) →* (Πᵘ*a, B' a) := pupi_functor id g definition pupi_functor_compose {A A' A'' : Type} {B : A → Type*} {B' : A' → Type*} {B'' : A'' → Type*} (f : A'' → A') (f' : A' → A) (g' : Πa, B' (f a) →* B'' a) (g : Πa, B (f' a) →* B' a) : pupi_functor (f' ∘ f) (λa, g' a ∘* g (f a)) ~* pupi_functor f g' ∘* pupi_functor f' g := begin fapply phomotopy_mk_pupi, { intro h a, reflexivity }, { intro a, refine !idp_con ⬝ _, refine !apd10_con ⬝ _ ⬝ !pi.apd10_eq_of_homotopy⁻¹, esimp, refine (!apd10_prepostcompose ⬝ ap02 (g' a) !pi.apd10_eq_of_homotopy) ◾ !pi.apd10_eq_of_homotopy } end definition pupi_functor_pid (A : Type) (B : A → Type*) : pupi_functor id (λa, pid (B a)) ~* pid (Πᵘ*a, B a) := begin fapply phomotopy_mk_pupi, { intro h a, reflexivity }, { intro a, refine !idp_con ⬝ !pi.apd10_eq_of_homotopy⁻¹ } end definition pupi_functor_phomotopy {A A' : Type} {B : A → Type*} {B' : A' → Type*} {f f' : A' → A} {g : Πa, B (f a) →* B' a} {g' : Πa, B (f' a) →* B' a} (p : f ~ f') (q : Πa, g a ~* g' a ∘* ptransport B (p a)) : pupi_functor f g ~* pupi_functor f' g' := begin fapply phomotopy_mk_pupi, { intro h a, exact q a (h (f a)) ⬝ ap (g' a) (apdt h (p a)) }, { intro a, esimp, exact whisker_left _ !pi.apd10_eq_of_homotopy ⬝ !con.assoc ⬝ to_homotopy_pt (q a) ⬝ !pi.apd10_eq_of_homotopy⁻¹ } end definition pupi_pequiv [constructor] {A A' : Type} {B : A → Type*} {B' : A' → Type*} (e : A' ≃ A) (f : Πa, B (e a) ≃* B' a) : (Πᵘ*a, B a) ≃* (Πᵘ*a', B' a') := pequiv.MK (pupi_functor e f) (pupi_functor e⁻¹ᵉ (λa, ptransport B (right_inv e a) ∘* (f (e⁻¹ᵉ a))⁻¹ᵉ*)) abstract begin refine !pupi_functor_compose⁻¹* ⬝* pupi_functor_phomotopy (to_right_inv e) _ ⬝* !pupi_functor_pid, intro a, exact !pinv_pcompose_cancel_right ⬝* !pid_pcompose⁻¹* end end abstract begin refine !pupi_functor_compose⁻¹* ⬝* pupi_functor_phomotopy (to_left_inv e) _ ⬝* !pupi_functor_pid, intro a, refine !passoc⁻¹* ⬝* pinv_right_phomotopy_of_phomotopy _ ⬝* !pid_pcompose⁻¹*, refine pwhisker_left _ _ ⬝* !ptransport_natural, exact ptransport_change_eq _ (adj e a) ⬝* ptransport_ap B e (to_left_inv e a) end end definition pupi_pequiv_right [constructor] {A : Type} {B B' : A → Type*} (f : Πa, B a ≃* B' a) : (Πᵘ*a, B a) ≃* (Πᵘ*a, B' a) := pupi_pequiv erfl f definition loop_pupi [constructor] {A : Type} (B : A → Type*) : Ω (Πᵘ*a, B a) ≃* Πᵘ*a, Ω (B a) := pequiv_of_equiv eq_equiv_homotopy idp -- definition loop_pupi_natural [constructor] {A : Type} {B B' : A → Type*} (f : Πa, B a →* B' a) : -- psquare (Ω→ (pupi_functor_right f)) (pupi_functor_right (λa, Ω→ (f a))) -- (loop_pupi B) (loop_pupi B') := definition ap1_gen_pi {A A' : Type} {B : A → Type} {B' : A' → Type} {h₀ h₁ : Πa, B a} {h₀' h₁' : Πa', B' a'} (f : A' → A) (g : Πa, B (f a) → B' a) (p₀ : pi_functor f g h₀ ~ h₀') (p₁ : pi_functor f g h₁ ~ h₁') (r : h₀ = h₁) (a' : A') : apd10 (ap1_gen (pi_functor f g) (eq_of_homotopy p₀) (eq_of_homotopy p₁) r) a' = ap1_gen (g a') (p₀ a') (p₁ a') (apd10 r (f a')) := begin induction r, induction p₀ using homotopy.rec_idp, induction p₁ using homotopy.rec_idp, esimp, exact apd10 (ap apd10 !ap1_gen_idp) a', -- exact ap (λx, apd10 (ap1_gen _ x x _) _) !eq_of_homotopy_idp end definition ap1_gen_pi_idp {A A' : Type} {B : A → Type} {B' : A' → Type} {h₀ : Πa, B a} (f : A' → A) (g : Πa, B (f a) → B' a) (a' : A') : ap1_gen_pi f g (homotopy.refl (pi_functor f g h₀)) (homotopy.refl (pi_functor f g h₀)) idp a' = apd10 (ap apd10 !ap1_gen_idp) a' := -- apd10 (ap apd10 (ap1_gen_idp (pi_functor id f) (eq_of_homotopy (λ a, idp)))) a' := -- ap (λp, apd10 p a') (ap1_gen_idp (pi_functor f g) (eq_of_homotopy homotopy.rfl)) := begin esimp [ap1_gen_pi], refine !homotopy_rec_idp_refl ⬝ !homotopy_rec_idp_refl, end -- print homotopy.rec_ -- print apd10_ap_postcompose -- print pi_functor -- print ap1_gen_idp -- print ap1_gen_pi definition loop_pupi_natural [constructor] {A : Type} {B B' : A → Type*} (f : Πa, B a →* B' a) : psquare (Ω→ (pupi_functor_right f)) (pupi_functor_right (λa, Ω→ (f a))) (loop_pupi B) (loop_pupi B') := begin fapply phomotopy_mk_pupi, { intro p a, exact ap1_gen_pi id f (λa, respect_pt (f a)) (λa, respect_pt (f a)) p a }, { intro a, revert B' f, refine fiberwise_pointed_map_rec _ _, intro B' f, refine !ap1_gen_pi_idp ◾ (ap (λx, apd10 x _) !idp_con ⬝ !apd10_eq_of_homotopy) } end definition phomotopy_mk_pumap [constructor] {A C : Type*} {B : Type} {f g : A →* (B →ᵘ* C)} (p : f ~2 g) (q : p pt ⬝hty apd10 (respect_pt g) ~ apd10 (respect_pt f)) : f ~* g := phomotopy_mk_pupi p q definition pumap_functor [constructor] {A A' : Type} {B B' : Type*} (f : A' → A) (g : B →* B') : (A →ᵘ* B) →* (A' →ᵘ* B') := pupi_functor f (λa, g) definition pumap_functor_compose {A A' A'' : Type} {B B' B'' : Type*} (f : A'' → A') (f' : A' → A) (g' : B' →* B'') (g : B →* B') : pumap_functor (f' ∘ f) (g' ∘* g) ~* pumap_functor f g' ∘* pumap_functor f' g := pupi_functor_compose f f' (λa, g') (λa, g) definition pumap_functor_pid (A : Type) (B : Type*) : pumap_functor id (pid B) ~* pid (A →ᵘ* B) := pupi_functor_pid A (λa, B) definition pumap_functor_phomotopy {A A' : Type} {B B' : Type*} {f f' : A' → A} {g g' : B →* B'} (p : f ~ f') (q : g ~* g') : pumap_functor f g ~* pumap_functor f' g' := pupi_functor_phomotopy p (λa, q ⬝* !pcompose_pid⁻¹* ⬝* pwhisker_left _ !ptransport_constant⁻¹*) definition pumap_pequiv [constructor] {A A' : Type} {B B' : Type*} (e : A ≃ A') (f : B ≃* B') : (A →ᵘ* B) ≃* (A' →ᵘ* B') := pupi_pequiv e⁻¹ᵉ (λa, f) definition pumap_pequiv_right [constructor] (A : Type) {B B' : Type*} (f : B ≃* B') : (A →ᵘ* B) ≃* (A →ᵘ* B') := pumap_pequiv erfl f definition pumap_pequiv_left [constructor] {A A' : Type} (B : Type*) (f : A ≃ A') : (A →ᵘ* B) ≃* (A' →ᵘ* B) := pumap_pequiv f pequiv.rfl definition loop_pumap [constructor] (A : Type) (B : Type*) : Ω (A →ᵘ* B) ≃* A →ᵘ* Ω B := loop_pupi (λa, B) /- the pointed sigma type -/ definition psigma_gen [constructor] {A : Type*} (P : A → Type) (x : P pt) : Type* := pointed.MK (Σa, P a) ⟨pt, x⟩ definition psigma_gen_functor [constructor] {A A' : Type*} {B : A → Type} {B' : A' → Type} {b : B pt} {b' : B' pt} (f : A →* A') (g : Πa, B a → B' (f a)) (p : g pt b =[respect_pt f] b') : psigma_gen B b →* psigma_gen B' b' := pmap.mk (sigma_functor f g) (sigma_eq (respect_pt f) p) definition psigma_gen_functor_right [constructor] {A : Type*} {B B' : A → Type} {b : B pt} {b' : B' pt} (f : Πa, B a → B' a) (p : f pt b = b') : psigma_gen B b →* psigma_gen B' b' := proof pmap.mk (sigma_functor id f) (sigma_eq_right p) qed definition psigma_gen_pequiv_psigma_gen [constructor] {A A' : Type*} {B : A → Type} {B' : A' → Type} {b : B pt} {b' : B' pt} (f : A ≃* A') (g : Πa, B a ≃ B' (f a)) (p : g pt b =[respect_pt f] b') : psigma_gen B b ≃* psigma_gen B' b' := pequiv_of_equiv (sigma_equiv_sigma f g) (sigma_eq (respect_pt f) p) definition psigma_gen_pequiv_psigma_gen_left [constructor] {A A' : Type*} {B : A' → Type} {b : B pt} (f : A ≃* A') {b' : B (f pt)} (q : b' =[respect_pt f] b) : psigma_gen (B ∘ f) b' ≃* psigma_gen B b := psigma_gen_pequiv_psigma_gen f (λa, erfl) q definition psigma_gen_pequiv_psigma_gen_right [constructor] {A : Type*} {B B' : A → Type} {b : B pt} {b' : B' pt} (f : Πa, B a ≃ B' a) (p : f pt b = b') : psigma_gen B b ≃* psigma_gen B' b' := psigma_gen_pequiv_psigma_gen pequiv.rfl f (pathover_idp_of_eq p) definition psigma_gen_pequiv_psigma_gen_basepoint [constructor] {A : Type*} {B : A → Type} {b b' : B pt} (p : b = b') : psigma_gen B b ≃* psigma_gen B b' := psigma_gen_pequiv_psigma_gen_right (λa, erfl) p definition loop_psigma_gen [constructor] {A : Type*} (B : A → Type) (b₀ : B pt) : Ω (psigma_gen B b₀) ≃* @psigma_gen (Ω A) (λp, pathover B b₀ p b₀) idpo := pequiv_of_equiv (sigma_eq_equiv pt pt) idp open sigma.ops definition ap1_gen_sigma {A A' : Type} {B : A → Type} {B' : A' → Type} {x₀ x₁ : Σa, B a} {a₀' a₁' : A'} {b₀' : B' a₀'} {b₁' : B' a₁'} (f : A → A') (p₀ : f x₀.1 = a₀') (p₁ : f x₁.1 = a₁') (g : Πa, B a → B' (f a)) (q₀ : g x₀.1 x₀.2 =[p₀] b₀') (q₁ : g x₁.1 x₁.2 =[p₁] b₁') (r : x₀ = x₁) : (λx, ⟨x..1, x..2⟩) (ap1_gen (sigma_functor f g) (sigma_eq p₀ q₀) (sigma_eq p₁ q₁) r) = ⟨ap1_gen f p₀ p₁ r..1, q₀⁻¹ᵒ ⬝o pathover_ap B' f (apo g r..2) ⬝o q₁⟩ := begin induction r, induction q₀, induction q₁, reflexivity end definition loop_psigma_gen_natural {A A' : Type*} {B : A → Type} {B' : A' → Type} {b : B pt} {b' : B' pt} (f : A →* A') (g : Πa, B a → B' (f a)) (p : g pt b =[respect_pt f] b') : psquare (Ω→ (psigma_gen_functor f g p)) (psigma_gen_functor (Ω→ f) (λq r, p⁻¹ᵒ ⬝o pathover_ap _ _ (apo g r) ⬝o p) !cono.left_inv) (loop_psigma_gen B b) (loop_psigma_gen B' b') := begin fapply phomotopy.mk, { exact ap1_gen_sigma f (respect_pt f) (respect_pt f) g p p }, { induction f with f f₀, induction A' with A' a₀', esimp at * ⊢, induction p, reflexivity } end definition psigma_gen_functor_pcompose [constructor] {A₁ A₂ A₃ : Type*} {B₁ : A₁ → Type} {B₂ : A₂ → Type} {B₃ : A₃ → Type} {b₁ : B₁ pt} {b₂ : B₂ pt} {b₃ : B₃ pt} {f₁ : A₁ →* A₂} {f₂ : A₂ →* A₃} (g₁ : Π⦃a⦄, B₁ a → B₂ (f₁ a)) (g₂ : Π⦃a⦄, B₂ a → B₃ (f₂ a)) (p₁ : pathover B₂ (g₁ b₁) (respect_pt f₁) b₂) (p₂ : pathover B₃ (g₂ b₂) (respect_pt f₂) b₃) : psigma_gen_functor (f₂ ∘* f₁) (λa, @g₂ (f₁ a) ∘ @g₁ a) (pathover_ap B₃ f₂ (apo g₂ p₁) ⬝o p₂) ~* psigma_gen_functor f₂ g₂ p₂ ∘* psigma_gen_functor f₁ g₁ p₁ := begin fapply phomotopy.mk, { intro x, reflexivity }, { refine !idp_con ⬝ _, esimp, refine whisker_right _ !ap_sigma_functor_eq_dpair ⬝ _, induction f₁ with f₁ f₁₀, induction f₂ with f₂ f₂₀, induction A₂ with A₂ a₂₀, induction A₃ with A₃ a₃₀, esimp at * ⊢, induction p₁, induction p₂, reflexivity } end definition psigma_gen_functor_phomotopy [constructor] {A₁ A₂ : Type*} {B₁ : A₁ → Type} {B₂ : A₂ → Type} {b₁ : B₁ pt} {b₂ : B₂ pt} {f₁ f₂ : A₁ →* A₂} {g₁ : Π⦃a⦄, B₁ a → B₂ (f₁ a)} {g₂ : Π⦃a⦄, B₁ a → B₂ (f₂ a)} {p₁ : pathover B₂ (g₁ b₁) (respect_pt f₁) b₂} {p₂ : pathover B₂ (g₂ b₁) (respect_pt f₂) b₂} (h₁ : f₁ ~* f₂) (h₂ : Π⦃a⦄ (b : B₁ a), pathover B₂ (g₁ b) (h₁ a) (g₂ b)) (h₃ : squareover B₂ (square_of_eq (to_homotopy_pt h₁)⁻¹) p₁ p₂ (h₂ b₁) idpo) : psigma_gen_functor f₁ g₁ p₁ ~* psigma_gen_functor f₂ g₂ p₂ := begin induction h₁ using phomotopy_rec_idp, fapply phomotopy.mk, { intro x, induction x with a b, exact ap (dpair (f₁ a)) (eq_of_pathover_idp (h₂ b)) }, { induction f₁ with f f₀, induction A₂ with A₂ a₂₀, esimp at * ⊢, induction f₀, esimp, induction p₂ using idp_rec_on, rewrite [to_right_inv !eq_con_inv_equiv_con_eq at h₃], have h₂ b₁ = p₁, from (eq_top_of_squareover h₃)⁻¹, induction this, refine !ap_dpair ⬝ ap (sigma_eq _) _, exact to_left_inv !pathover_idp (h₂ b₁) } end definition psigma_gen_functor_psquare [constructor] {A₀₀ A₀₂ A₂₀ A₂₂ : Type*} {B₀₀ : A₀₀ → Type} {B₀₂ : A₀₂ → Type} {B₂₀ : A₂₀ → Type} {B₂₂ : A₂₂ → Type} {b₀₀ : B₀₀ pt} {b₀₂ : B₀₂ pt} {b₂₀ : B₂₀ pt} {b₂₂ : B₂₂ pt} {f₀₁ : A₀₀ →* A₀₂} {f₁₀ : A₀₀ →* A₂₀} {f₂₁ : A₂₀ →* A₂₂} {f₁₂ : A₀₂ →* A₂₂} {g₀₁ : Π⦃a⦄, B₀₀ a → B₀₂ (f₀₁ a)} {g₁₀ : Π⦃a⦄, B₀₀ a → B₂₀ (f₁₀ a)} {g₂₁ : Π⦃a⦄, B₂₀ a → B₂₂ (f₂₁ a)} {g₁₂ : Π⦃a⦄, B₀₂ a → B₂₂ (f₁₂ a)} {p₀₁ : pathover B₀₂ (g₀₁ b₀₀) (respect_pt f₀₁) b₀₂} {p₁₀ : pathover B₂₀ (g₁₀ b₀₀) (respect_pt f₁₀) b₂₀} {p₂₁ : pathover B₂₂ (g₂₁ b₂₀) (respect_pt f₂₁) b₂₂} {p₁₂ : pathover B₂₂ (g₁₂ b₀₂) (respect_pt f₁₂) b₂₂} (h₁ : psquare f₁₀ f₁₂ f₀₁ f₂₁) (h₂ : Π⦃a⦄ (b : B₀₀ a), pathover B₂₂ (g₂₁ (g₁₀ b)) (h₁ a) (g₁₂ (g₀₁ b))) (h₃ : squareover B₂₂ (square_of_eq (to_homotopy_pt h₁)⁻¹) (pathover_ap B₂₂ f₂₁ (apo g₂₁ p₁₀) ⬝o p₂₁) (pathover_ap B₂₂ f₁₂ (apo g₁₂ p₀₁) ⬝o p₁₂) (h₂ b₀₀) idpo) : psquare (psigma_gen_functor f₁₀ g₁₀ p₁₀) (psigma_gen_functor f₁₂ g₁₂ p₁₂) (psigma_gen_functor f₀₁ g₀₁ p₀₁) (psigma_gen_functor f₂₁ g₂₁ p₂₁) := proof !psigma_gen_functor_pcompose⁻¹* ⬝* psigma_gen_functor_phomotopy h₁ h₂ h₃ ⬝* !psigma_gen_functor_pcompose qed end pointed open pointed namespace pointed definition pointed_respect_pt [instance] [constructor] {A B : Type*} (f : A →* B) : pointed (f pt = pt) := pointed.mk (respect_pt f) definition ppi_of_phomotopy [constructor] {A B : Type*} {f g : A →* B} (h : f ~* g) : ppi (λx, f x = g x) (respect_pt f ⬝ (respect_pt g)⁻¹) := h definition phomotopy {A : Type*} {P : A → Type} {x : P pt} (f g : ppi P x) : Type := ppi (λa, f a = g a) (respect_pt f ⬝ (respect_pt g)⁻¹) variables {A : Type*} {P Q R : A → Type*} {f g h : Π*a, P a} {B C D : A → Type} {b₀ : B pt} {c₀ : C pt} {d₀ : D pt} {k k' l m : ppi B b₀} definition pppi_equiv_pmap [constructor] (A B : Type*) : (Π*(a : A), B) ≃ (A →* B) := by reflexivity definition pppi_pequiv_ppmap [constructor] (A B : Type*) : (Π*(a : A), B) ≃* ppmap A B := by reflexivity definition apd10_to_fun_eq_of_phomotopy (h : f ~* g) : apd10 (ap (λ k, pppi.to_fun k) (eq_of_phomotopy h)) = h := begin induction h using phomotopy_rec_idp, xrewrite [eq_of_phomotopy_refl f] end -- definition phomotopy_of_eq_of_phomotopy definition phomotopy_mk_ppi [constructor] {A : Type*} {B : Type*} {C : B → Type*} {f g : A →* (Π*b, C b)} (p : Πa, f a ~* g a) (q : p pt ⬝* phomotopy_of_eq (respect_pt g) = phomotopy_of_eq (respect_pt f)) : f ~* g := begin apply phomotopy.mk (λa, eq_of_phomotopy (p a)), apply eq_of_fn_eq_fn !ppi_eq_equiv, refine !phomotopy_of_eq_con ⬝ _, esimp, refine ap (λx, x ⬝* _) !phomotopy_of_eq_of_phomotopy ⬝ q end -- definition phomotopy_mk_ppmap [constructor] -- {A : Type*} {X : A → Type*} {Y : Π (a : A), X a → Type*} -- {f g : Π* (a : A), Π*(x : (X a)), (Y a x)} -- (p : Πa, f a ~* g a) -- (q : p pt ⬝* phomotopy_of_eq (ppi_resp_pt g) = phomotopy_of_eq (ppi_resp_pt f)) -- : f ~* g := -- begin -- apply phomotopy.mk (λa, eq_of_phomotopy (p a)), -- apply eq_of_fn_eq_fn (ppi_eq_equiv _ _), -- refine !phomotopy_of_eq_con ⬝ _, -- -- refine !phomotopy_of_eq_of_phomotopy ◾** idp ⬝ q, -- end variable {k} variables (k l) definition ppi_loop_equiv : (k = k) ≃ Π*(a : A), Ω (pType.mk (B a) (k a)) := begin induction k with k p, induction p, exact ppi_eq_equiv (ppi.mk k idp) (ppi.mk k idp) end variables {k l} -- definition eq_of_phomotopy (h : k ~* l) : k = l := -- (ppi_eq_equiv k l)⁻¹ᵉ h definition ppi_functor_right [constructor] {A : Type*} {B B' : A → Type} {b : B pt} {b' : B' pt} (f : Πa, B a → B' a) (p : f pt b = b') (g : ppi B b) : ppi B' b' := ppi.mk (λa, f a (g a)) (ap (f pt) (respect_pt g) ⬝ p) definition ppi_functor_right_compose [constructor] {A : Type*} {B₁ B₂ B₃ : A → Type} {b₁ : B₁ pt} {b₂ : B₂ pt} {b₃ : B₃ pt} (f₂ : Πa, B₂ a → B₃ a) (p₂ : f₂ pt b₂ = b₃) (f₁ : Πa, B₁ a → B₂ a) (p₁ : f₁ pt b₁ = b₂) (g : ppi B₁ b₁) : ppi_functor_right (λa, f₂ a ∘ f₁ a) (ap (f₂ pt) p₁ ⬝ p₂) g ~* ppi_functor_right f₂ p₂ (ppi_functor_right f₁ p₁ g) := begin fapply phomotopy.mk, { reflexivity }, { induction p₁, induction p₂, exact !idp_con ⬝ !ap_compose⁻¹ } end definition ppi_functor_right_id [constructor] {A : Type*} {B : A → Type} {b : B pt} (g : ppi B b) : ppi_functor_right (λa, id) idp g ~* g := begin fapply phomotopy.mk, { reflexivity }, { reflexivity } end definition ppi_functor_right_phomotopy [constructor] {g g' : Π(a : A), B a → C a} {g₀ : g pt b₀ = c₀} {g₀' : g' pt b₀ = c₀} {f f' : ppi B b₀} (p : g ~2 g') (q : f ~* f') (r : p pt b₀ ⬝ g₀' = g₀) : ppi_functor_right g g₀ f ~* ppi_functor_right g' g₀' f' := phomotopy.mk (λa, p a (f a) ⬝ ap (g' a) (q a)) abstract begin induction q using phomotopy_rec_idp, induction r, revert g p, refine rec_idp_of_equiv _ homotopy2.rfl _ _ _, { intro h h', exact !eq_equiv_eq_symm ⬝e !eq_equiv_homotopy2 }, { reflexivity }, induction g₀', induction f with f f₀, induction f₀, reflexivity end end definition ppi_functor_right_phomotopy_refl [constructor] (g : Π(a : A), B a → C a) (g₀ : g pt b₀ = c₀) (f : ppi B b₀) : ppi_functor_right_phomotopy (homotopy2.refl g) (phomotopy.refl f) !idp_con = phomotopy.refl (ppi_functor_right g g₀ f) := begin induction g₀, apply ap (phomotopy.mk homotopy.rfl), refine !phomotopy_rec_idp_refl ⬝ _, esimp, refine !rec_idp_of_equiv_idp ⬝ _, induction f with f f₀, induction f₀, reflexivity end definition ppi_equiv_ppi_right [constructor] {A : Type*} {B B' : A → Type} {b : B pt} {b' : B' pt} (f : Πa, B a ≃ B' a) (p : f pt b = b') : ppi B b ≃ ppi B' b' := equiv.MK (ppi_functor_right f p) (ppi_functor_right (λa, (f a)⁻¹ᵉ) (inv_eq_of_eq p⁻¹)) abstract begin intro g, apply eq_of_phomotopy, refine !ppi_functor_right_compose⁻¹* ⬝* _, refine ppi_functor_right_phomotopy (λa, to_right_inv (f a)) (phomotopy.refl g) _ ⬝* !ppi_functor_right_id, induction p, exact adj (f pt) b ⬝ ap02 (f pt) !idp_con⁻¹ end end abstract begin intro g, apply eq_of_phomotopy, refine !ppi_functor_right_compose⁻¹* ⬝* _, refine ppi_functor_right_phomotopy (λa, to_left_inv (f a)) (phomotopy.refl g) _ ⬝* !ppi_functor_right_id, induction p, exact (!idp_con ⬝ !idp_con)⁻¹, end end definition ppi_equiv_ppi_basepoint [constructor] {A : Type*} {B : A → Type} {b b' : B pt} (p : b = b') : ppi B b ≃ ppi B b' := ppi_equiv_ppi_right (λa, erfl) p definition pmap_compose_ppi [constructor] (g : Π(a : A), ppmap (P a) (Q a)) (f : Π*(a : A), P a) : Π*(a : A), Q a := ppi_functor_right g (respect_pt (g pt)) f definition pmap_compose_ppi_ppi_const [constructor] (g : Π(a : A), ppmap (P a) (Q a)) : pmap_compose_ppi g (ppi_const P) ~* ppi_const Q := proof phomotopy.mk (λa, respect_pt (g a)) !idp_con⁻¹ qed definition pmap_compose_ppi_pconst [constructor] (f : Π*(a : A), P a) : pmap_compose_ppi (λa, pconst (P a) (Q a)) f ~* ppi_const Q := phomotopy.mk homotopy.rfl !ap_constant⁻¹ definition pmap_compose_ppi2 [constructor] {g g' : Π(a : A), ppmap (P a) (Q a)} {f f' : Π*(a : A), P a} (p : Πa, g a ~* g' a) (q : f ~* f') : pmap_compose_ppi g f ~* pmap_compose_ppi g' f' := ppi_functor_right_phomotopy p q (to_homotopy_pt (p pt)) definition pmap_compose_ppi2_refl [constructor] (g : Π(a : A), P a →* Q a) (f : Π*(a : A), P a) : pmap_compose_ppi2 (λa, phomotopy.refl (g a)) (phomotopy.refl f) = phomotopy.rfl := begin refine _ ⬝ ppi_functor_right_phomotopy_refl g (respect_pt (g pt)) f, exact ap (ppi_functor_right_phomotopy _ _) (to_right_inv !eq_con_inv_equiv_con_eq _) end definition pmap_compose_ppi_phomotopy_left [constructor] {g g' : Π(a : A), ppmap (P a) (Q a)} (f : Π*(a : A), P a) (p : Πa, g a ~* g' a) : pmap_compose_ppi g f ~* pmap_compose_ppi g' f := pmap_compose_ppi2 p phomotopy.rfl definition pmap_compose_ppi_phomotopy_right [constructor] (g : Π(a : A), ppmap (P a) (Q a)) {f f' : Π*(a : A), P a} (p : f ~* f') : pmap_compose_ppi g f ~* pmap_compose_ppi g f' := pmap_compose_ppi2 (λa, phomotopy.rfl) p definition pmap_compose_ppi_pid_left [constructor] (f : Π*(a : A), P a) : pmap_compose_ppi (λa, pid (P a)) f ~* f := phomotopy.mk homotopy.rfl idp definition pmap_compose_ppi_pcompose [constructor] (h : Π(a : A), ppmap (Q a) (R a)) (g : Π(a : A), ppmap (P a) (Q a)) : pmap_compose_ppi (λa, h a ∘* g a) f ~* pmap_compose_ppi h (pmap_compose_ppi g f) := phomotopy.mk homotopy.rfl abstract !idp_con ⬝ whisker_right _ (!ap_con ⬝ whisker_right _ !ap_compose'⁻¹) ⬝ !con.assoc end definition ppi_assoc [constructor] (h : Π (a : A), Q a →* R a) (g : Π (a : A), P a →* Q a) (f : Π*a, P a) : pmap_compose_ppi (λa, h a ∘* g a) f ~* pmap_compose_ppi h (pmap_compose_ppi g f) := begin fapply phomotopy.mk, { intro a, reflexivity }, exact !idp_con ⬝ whisker_right _ (!ap_con ⬝ whisker_right _ !ap_compose⁻¹) ⬝ !con.assoc end definition pmap_compose_ppi_eq_of_phomotopy (g : Πa, P a →* Q a) {f f' : Π*a, P a} (p : f ~* f') : ap (pmap_compose_ppi g) (eq_of_phomotopy p) = eq_of_phomotopy (pmap_compose_ppi_phomotopy_right g p) := begin induction p using phomotopy_rec_idp, refine ap02 _ !eq_of_phomotopy_refl ⬝ !eq_of_phomotopy_refl⁻¹ ⬝ ap eq_of_phomotopy _, exact !pmap_compose_ppi2_refl⁻¹ end definition ppi_assoc_ppi_const_right (g : Πa, Q a →* R a) (f : Πa, P a →* Q a) : ppi_assoc g f (ppi_const P) ⬝* (pmap_compose_ppi_phomotopy_right _ (pmap_compose_ppi_ppi_const f) ⬝* pmap_compose_ppi_ppi_const g) = pmap_compose_ppi_ppi_const (λa, g a ∘* f a) := begin revert R g, refine fiberwise_pointed_map_rec _ _, revert Q f, refine fiberwise_pointed_map_rec _ _, intro Q f R g, refine ap (λx, _ ⬝* (x ⬝* _)) !pmap_compose_ppi2_refl ⬝ _, reflexivity end definition pppi_compose_left [constructor] (g : Π(a : A), ppmap (P a) (Q a)) : (Π*(a : A), P a) →* Π*(a : A), Q a := pmap.mk (pmap_compose_ppi g) (eq_of_phomotopy (pmap_compose_ppi_ppi_const g)) -- pppi_compose_left is a functor in the following sense definition pppi_compose_left_pcompose (g : Π (a : A), Q a →* R a) (f : Π (a : A), P a →* Q a) : pppi_compose_left (λ a, g a ∘* f a) ~* (pppi_compose_left g ∘* pppi_compose_left f) := begin fapply phomotopy_mk_ppi, { exact ppi_assoc g f }, { refine idp ◾** (!phomotopy_of_eq_con ⬝ (ap phomotopy_of_eq !pmap_compose_ppi_eq_of_phomotopy ⬝ !phomotopy_of_eq_of_phomotopy) ◾** !phomotopy_of_eq_of_phomotopy) ⬝ _ ⬝ !phomotopy_of_eq_of_phomotopy⁻¹, apply ppi_assoc_ppi_const_right }, end definition pppi_compose_left_phomotopy [constructor] {g g' : Π(a : A), ppmap (P a) (Q a)} (p : Πa, g a ~* g' a) : pppi_compose_left g ~* pppi_compose_left g' := begin apply phomotopy_of_eq, apply ap pppi_compose_left, apply eq_of_homotopy, intro a, apply eq_of_phomotopy, exact p a end definition psquare_pppi_compose_left {A : Type*} {B C D E : A → Type*} {ftop : Π (a : A), B a →* C a} {fbot : Π (a : A), D a →* E a} {fleft : Π (a : A), B a →* D a} {fright : Π (a : A), C a →* E a} (psq : Π (a :A), psquare (ftop a) (fbot a) (fleft a) (fright a)) : psquare (pppi_compose_left ftop) (pppi_compose_left fbot) (pppi_compose_left fleft) (pppi_compose_left fright) := begin refine (pppi_compose_left_pcompose fright ftop)⁻¹* ⬝* _ ⬝* (pppi_compose_left_pcompose fbot fleft), exact pppi_compose_left_phomotopy psq end definition ppi_pequiv_right [constructor] (g : Π(a : A), P a ≃* Q a) : (Π*(a : A), P a) ≃* Π*(a : A), Q a := begin apply pequiv_of_pmap (pppi_compose_left g), apply adjointify _ (pppi_compose_left (λa, (g a)⁻¹ᵉ*)), { intro f, apply eq_of_phomotopy, refine !pmap_compose_ppi_pcompose⁻¹* ⬝* _, refine pmap_compose_ppi_phomotopy_left _ (λa, !pright_inv) ⬝* _, apply pmap_compose_ppi_pid_left }, { intro f, apply eq_of_phomotopy, refine !pmap_compose_ppi_pcompose⁻¹* ⬝* _, refine pmap_compose_ppi_phomotopy_left _ (λa, !pleft_inv) ⬝* _, apply pmap_compose_ppi_pid_left } end end pointed namespace pointed variables {A B C : Type*} -- TODO: replace in types.fiber definition pfiber.sigma_char' (f : A →* B) : pfiber f ≃* psigma_gen (λa, f a = pt) (respect_pt f) := pequiv_of_equiv (fiber.sigma_char f pt) idp definition fiberpt [constructor] {A B : Type*} {f : A →* B} : fiber f pt := fiber.mk pt (respect_pt f) definition psigma_fiber_pequiv [constructor] {A B : Type*} (f : A →* B) : psigma_gen (fiber f) fiberpt ≃* A := pequiv_of_equiv (sigma_fiber_equiv f) idp definition ppmap.sigma_char [constructor] (A B : Type*) : ppmap A B ≃* @psigma_gen (A →ᵘ* B) (λf, f pt = pt) idp := pequiv_of_equiv pmap.sigma_char idp definition pppi.sigma_char [constructor] (B : A → Type*) : (Π*(a : A), B a) ≃* @psigma_gen (Πᵘ*a, B a) (λf, f pt = pt) idp := proof pequiv_of_equiv !ppi.sigma_char idp qed definition pppi_sigma_char_natural_bottom [constructor] {B B' : A → Type*} (f : Πa, B a →* B' a) : @psigma_gen (Πᵘ*a, B a) (λg, g pt = pt) idp →* @psigma_gen (Πᵘ*a, B' a) (λg, g pt = pt) idp := psigma_gen_functor (pupi_functor_right f) (λg p, ap (f pt) p ⬝ respect_pt (f pt)) begin apply eq_pathover_constant_right, apply square_of_eq, esimp, exact !idp_con ⬝ !apd10_eq_of_homotopy⁻¹ ⬝ !ap_eq_apd10⁻¹, end definition pppi_sigma_char_natural {B B' : A → Type*} (f : Πa, B a →* B' a) : psquare (pppi_compose_left f) (pppi_sigma_char_natural_bottom f) (pppi.sigma_char B) (pppi.sigma_char B') := begin fapply phomotopy.mk, { intro g, reflexivity }, { refine !idp_con ⬝ !idp_con ⬝ _, fapply sigma_eq2, { refine !sigma_eq_pr1 ⬝ _ ⬝ !ap_sigma_pr1⁻¹, apply eq_of_fn_eq_fn eq_equiv_homotopy, refine !apd10_eq_of_homotopy ⬝ _ ⬝ !apd10_to_fun_eq_of_phomotopy⁻¹, apply eq_of_homotopy, intro a, reflexivity }, { exact sorry } } end open sigma.ops definition psigma_gen_pi_pequiv_pupi_psigma_gen [constructor] {A : Type*} {B : A → Type*} (C : Πa, B a → Type) (c : Πa, C a pt) : @psigma_gen (Πᵘ*a, B a) (λf, Πa, C a (f a)) c ≃* Πᵘ*a, psigma_gen (C a) (c a) := pequiv_of_equiv sigma_pi_equiv_pi_sigma idp definition pupi_psigma_gen_pequiv_psigma_gen_pi [constructor] {A : Type*} {B : A → Type*} (C : Πa, B a → Type) (c : Πa, C a pt) : (Πᵘ*a, psigma_gen (C a) (c a)) ≃* @psigma_gen (Πᵘ*a, B a) (λf, Πa, C a (f a)) c := pequiv_of_equiv sigma_pi_equiv_pi_sigma⁻¹ᵉ idp definition psigma_gen_assoc [constructor] {A : Type*} {B : A → Type} (C : Πa, B a → Type) (b₀ : B pt) (c₀ : C pt b₀) : psigma_gen (λa, Σb, C a b) ⟨b₀, c₀⟩ ≃* @psigma_gen (psigma_gen B b₀) (λv, C v.1 v.2) c₀ := pequiv_of_equiv !sigma_assoc_equiv idp definition psigma_gen_swap [constructor] {A : Type*} {B B' : A → Type} (C : Π⦃a⦄, B a → B' a → Type) (b₀ : B pt) (b₀' : B' pt) (c₀ : C b₀ b₀') : @psigma_gen (psigma_gen B b₀ ) (λv, Σb', C v.2 b') ⟨b₀', c₀⟩ ≃* @psigma_gen (psigma_gen B' b₀') (λv, Σb , C b v.2) ⟨b₀ , c₀⟩ := !psigma_gen_assoc⁻¹ᵉ* ⬝e* psigma_gen_pequiv_psigma_gen_right (λa, !sigma_comm_equiv) idp ⬝e* !psigma_gen_assoc definition ppi_psigma.{u v w} {A : pType.{u}} {B : A → pType.{v}} (C : Πa, B a → Type.{w}) (c : Πa, C a pt) : (Π*(a : A), (psigma_gen (C a) (c a))) ≃* psigma_gen (λ(f : Π*(a : A), B a), ppi (λa, C a (f a)) (transport (C pt) (respect_pt f)⁻¹ (c pt))) (ppi_const _) := proof calc (Π*(a : A), psigma_gen (C a) (c a)) ≃* @psigma_gen (Πᵘ*a, psigma_gen (C a) (c a)) (λf, f pt = pt) idp : pppi.sigma_char ... ≃* @psigma_gen (@psigma_gen (Πᵘ*a, B a) (λf, Πa, C a (f a)) c) (λv, Σ(p : v.1 pt = pt), v.2 pt =[p] c pt) ⟨idp, idpo⟩ : by exact psigma_gen_pequiv_psigma_gen (pupi_psigma_gen_pequiv_psigma_gen_pi C c) (λf, sigma_eq_equiv _ _) idpo ... ≃* @psigma_gen (@psigma_gen (Πᵘ*a, B a) (λf, f pt = pt) idp) (λv, Σ(g : Πa, C a (v.1 a)), g pt =[v.2] c pt) ⟨c, idpo⟩ : by apply psigma_gen_swap ... ≃* psigma_gen (λ(f : Π*(a : A), B a), ppi (λa, C a (f a)) (transport (C pt) (respect_pt f)⁻¹ (c pt))) (ppi_const _) : by exact (psigma_gen_pequiv_psigma_gen (pppi.sigma_char B) (λf, !ppi.sigma_char ⬝e sigma_equiv_sigma_right (λg, !pathover_equiv_eq_tr⁻¹ᵉ)) idpo)⁻¹ᵉ* qed definition ppmap_psigma {A B : Type*} (C : B → Type) (c : C pt) : ppmap A (psigma_gen C c) ≃* psigma_gen (λ(f : ppmap A B), ppi (C ∘ f) (transport C (respect_pt f)⁻¹ c)) (ppi_const _) := !pppi_pequiv_ppmap⁻¹ᵉ* ⬝e* !ppi_psigma ⬝e* sorry -- psigma_gen_pequiv_psigma_gen (pppi_pequiv_ppmap A B) (λf, begin esimp, exact ppi_equiv_ppi_right (λa, _) _ end) _ definition pfiber_pppi_compose_left {B C : A → Type*} (f : Πa, B a →* C a) : pfiber (pppi_compose_left f) ≃* Π*(a : A), pfiber (f a) := calc pfiber (pppi_compose_left f) ≃* psigma_gen (λ(g : Π*(a : A), B a), pmap_compose_ppi f g = ppi_const C) proof (eq_of_phomotopy (pmap_compose_ppi_ppi_const f)) qed : by exact !pfiber.sigma_char' ... ≃* psigma_gen (λ(g : Π*(a : A), B a), pmap_compose_ppi f g ~* ppi_const C) proof (pmap_compose_ppi_ppi_const f) qed : by exact psigma_gen_pequiv_psigma_gen_right (λa, !ppi_eq_equiv) !phomotopy_of_eq_of_phomotopy ... ≃* @psigma_gen (Π*(a : A), B a) (λ(g : Π*(a : A), B a), ppi (λa, f a (g a) = pt) (transport (λb, f pt b = pt) (respect_pt g)⁻¹ (respect_pt (f pt)))) (ppi_const _) : begin refine psigma_gen_pequiv_psigma_gen_right (λg, ppi_equiv_ppi_basepoint (_ ⬝ !eq_transport_Fl⁻¹)) _, intro g, refine !con_idp ⬝ _, apply whisker_right, exact ap02 (f pt) !inv_inv⁻¹ ⬝ !ap_inv, apply eq_of_phomotopy, fapply phomotopy.mk, intro x, reflexivity, refine !idp_con ⬝ _, symmetry, refine !ap_id ◾ !idp_con ⬝ _, apply con.right_inv end ... ≃* Π*(a : A), (psigma_gen (λb, f a b = pt) (respect_pt (f a))) : by exact (ppi_psigma _ _)⁻¹ᵉ* ... ≃* Π*(a : A), pfiber (f a) : by exact ppi_pequiv_right (λa, !pfiber.sigma_char'⁻¹ᵉ*) /- TODO: proof the following as a special case of pfiber_pppi_compose_left -/ definition pfiber_ppcompose_left (f : B →* C) : pfiber (@ppcompose_left A B C f) ≃* ppmap A (pfiber f) := calc pfiber (@ppcompose_left A B C f) ≃* psigma_gen (λ(g : ppmap A B), f ∘* g = pconst A C) proof (eq_of_phomotopy (pcompose_pconst f)) qed : by exact !pfiber.sigma_char' ... ≃* psigma_gen (λ(g : ppmap A B), f ∘* g ~* pconst A C) proof (pcompose_pconst f) qed : by exact psigma_gen_pequiv_psigma_gen_right (λa, !pmap_eq_equiv) !phomotopy_of_eq_of_phomotopy ... ≃* psigma_gen (λ(g : ppmap A B), ppi (λa, f (g a) = pt) (transport (λb, f b = pt) (respect_pt g)⁻¹ (respect_pt f))) (ppi_const _) : begin refine psigma_gen_pequiv_psigma_gen_right (λg, ppi_equiv_ppi_basepoint (_ ⬝ !eq_transport_Fl⁻¹)) _, intro g, refine !con_idp ⬝ _, apply whisker_right, exact ap02 f !inv_inv⁻¹ ⬝ !ap_inv, apply eq_of_phomotopy, fapply phomotopy.mk, intro x, reflexivity, refine !idp_con ⬝ _, symmetry, refine !ap_id ◾ !idp_con ⬝ _, apply con.right_inv end ... ≃* ppmap A (psigma_gen (λb, f b = pt) (respect_pt f)) : by exact (ppmap_psigma _ _)⁻¹ᵉ* ... ≃* ppmap A (pfiber f) : by exact pequiv_ppcompose_left !pfiber.sigma_char'⁻¹ᵉ* -- definition pppi_ppmap {A C : Type*} {B : A → Type*} : -- ppmap (/- dependent smash of B -/) C ≃* Π*(a : A), ppmap (B a) C := definition ppi_add_point_over {A : Type} (B : A → Type*) : (Π*a, add_point_over B a) ≃ Πa, B a := begin fapply equiv.MK, { intro f a, exact f (some a) }, { intro f, fconstructor, intro a, cases a, exact pt, exact f a, reflexivity }, { intro f, reflexivity }, { intro f, cases f with f p, apply eq_of_phomotopy, fapply phomotopy.mk, { intro a, cases a, exact p⁻¹, reflexivity }, { exact con.left_inv p }}, end definition pppi_add_point_over {A : Type} (B : A → Type*) : (Π*a, add_point_over B a) ≃* Πᵘ*a, B a := pequiv_of_equiv (ppi_add_point_over B) idp definition ppmap_add_point {A : Type} (B : Type*) : ppmap A₊ B ≃* A →ᵘ* B := pequiv_of_equiv (pmap_equiv_left A B) idp /- There are some lemma's needed to prove the naturality of the equivalence Ω (Π*a, B a) ≃* Π*(a : A), Ω (B a) -/ definition ppi_eq_equiv_natural_gen_lem {B C : A → Type} {b₀ : B pt} {c₀ : C pt} {f : Π(a : A), B a → C a} {f₀ : f pt b₀ = c₀} {k : ppi B b₀} {k' : ppi C c₀} (p : ppi_functor_right f f₀ k ~* k') : ap1_gen (f pt) (p pt) f₀ (respect_pt k) = respect_pt k' := begin symmetry, refine _ ⬝ !con.assoc⁻¹, exact eq_inv_con_of_con_eq (to_homotopy_pt p), end definition ppi_eq_equiv_natural_gen_lem2 {B C : A → Type} {b₀ : B pt} {c₀ : C pt} {f : Π(a : A), B a → C a} {f₀ : f pt b₀ = c₀} {k l : ppi B b₀} {k' l' : ppi C c₀} (p : ppi_functor_right f f₀ k ~* k') (q : ppi_functor_right f f₀ l ~* l') : ap1_gen (f pt) (p pt) (q pt) (respect_pt k ⬝ (respect_pt l)⁻¹) = respect_pt k' ⬝ (respect_pt l')⁻¹ := (ap1_gen_con (f pt) _ f₀ _ _ _ ⬝ (ppi_eq_equiv_natural_gen_lem p) ◾ (!ap1_gen_inv ⬝ (ppi_eq_equiv_natural_gen_lem q)⁻²)) definition ppi_eq_equiv_natural_gen {B C : A → Type} {b₀ : B pt} {c₀ : C pt} {f : Π(a : A), B a → C a} {f₀ : f pt b₀ = c₀} {k l : ppi B b₀} {k' l' : ppi C c₀} (p : ppi_functor_right f f₀ k ~* k') (q : ppi_functor_right f f₀ l ~* l') : hsquare (ap1_gen (ppi_functor_right f f₀) (eq_of_phomotopy p) (eq_of_phomotopy q)) (ppi_functor_right (λa, ap1_gen (f a) (p a) (q a)) (ppi_eq_equiv_natural_gen_lem2 p q)) phomotopy_of_eq phomotopy_of_eq := begin intro r, induction r, induction f₀, induction k with k k₀, induction k₀, refine idp ⬝ _, revert l' q, refine phomotopy_rec_idp' _ _, revert k' p, refine phomotopy_rec_idp' _ _, reflexivity end definition ppi_eq_equiv_natural_gen_refl {B C : A → Type} {f : Π(a : A), B a → C a} {k : Πa, B a} : ppi_eq_equiv_natural_gen (phomotopy.refl (ppi_functor_right f idp (ppi.mk k idp))) (phomotopy.refl (ppi_functor_right f idp (ppi.mk k idp))) idp = ap phomotopy_of_eq !ap1_gen_idp := begin refine !idp_con ⬝ _, refine !phomotopy_rec_idp'_refl ⬝ _, refine ap (transport _ _) !phomotopy_rec_idp'_refl ⬝ _, refine !tr_diag_eq_tr_tr⁻¹ ⬝ _, refine !eq_transport_Fl ⬝ _, refine !ap_inv⁻² ⬝ !inv_inv ⬝ !ap_compose ⬝ ap02 _ _, exact !ap1_gen_idp_eq⁻¹ end definition loop_pppi_pequiv [constructor] {A : Type*} (B : A → Type*) : Ω (Π*a, B a) ≃* Π*(a : A), Ω (B a) := pequiv_of_equiv !ppi_eq_equiv idp definition loop_pppi_pequiv_natural {A : Type*} {X Y : A → Type*} (f : Π (a : A), X a →* Y a) : psquare (Ω→ (pppi_compose_left f)) (pppi_compose_left (λ a, Ω→ (f a))) (loop_pppi_pequiv X) (loop_pppi_pequiv Y) := begin revert Y f, refine fiberwise_pointed_map_rec _ _, intro Y f, fapply phomotopy.mk, { exact ppi_eq_equiv_natural_gen (pmap_compose_ppi_ppi_const (λa, pmap_of_map (f a) pt)) (pmap_compose_ppi_ppi_const (λa, pmap_of_map (f a) pt)) }, { exact !ppi_eq_equiv_natural_gen_refl ◾ (!idp_con ⬝ !eq_of_phomotopy_refl) } end /- below is an alternate proof strategy for the naturality of loop_pppi_pequiv_natural, where we define loop_pppi_pequiv as composite of pointed equivalences, and proved the naturality individually. That turned out to be harder. -/ /- definition loop_pppi_pequiv2 {A : Type*} (B : A → Type*) : Ω (Π*a, B a) ≃* Π*(a : A), Ω (B a) := begin refine loop_pequiv_loop (pppi.sigma_char B) ⬝e* _, refine !loop_psigma_gen ⬝e* _, transitivity @psigma_gen (Πᵘ*a, Ω (B a)) (λf, f pt = idp) idp, exact psigma_gen_pequiv_psigma_gen (loop_pupi B) (λp, eq_pathover_equiv_Fl p idp idp ⬝e equiv_eq_closed_right _ (whisker_right _ (ap_eq_apd10 p _)) ⬝e !eq_equiv_eq_symm) idpo, exact (pppi.sigma_char (Ω ∘ B))⁻¹ᵉ* end definition loop_pppi_pequiv_natural2 {A : Type*} {X Y : A → Type*} (f : Π (a : A), X a →* Y a) : psquare (Ω→ (pppi_compose_left f)) (pppi_compose_left (λ a, Ω→ (f a))) (loop_pppi_pequiv2 X) (loop_pppi_pequiv2 Y) := begin refine ap1_psquare (pppi_sigma_char_natural f) ⬝v* _, refine !loop_psigma_gen_natural ⬝v* _, refine _ ⬝v* (pppi_sigma_char_natural (λ a, Ω→ (f a)))⁻¹ᵛ*, fapply psigma_gen_functor_psquare, { apply loop_pupi_natural }, { intro p q, exact sorry }, { exact sorry } end-/ end pointed open pointed open is_trunc is_conn namespace is_conn section variables (A : Type*) (n : ℕ₋₂) [H : is_conn (n.+1) A] include H definition is_contr_ppi_match (P : A → Type*) (H : Πa, is_trunc (n.+1) (P a)) : is_contr (Π*(a : A), P a) := begin apply is_contr.mk pt, intro f, induction f with f p, apply eq_of_phomotopy, fapply phomotopy.mk, { apply is_conn.elim n, exact p⁻¹ }, { krewrite (is_conn.elim_β n), apply con.left_inv } end -- definition is_trunc_ppi_of_is_conn (k : ℕ₋₂) (P : A → Type*) -- : is_trunc k.+1 (Π*(a : A), P a) := definition is_trunc_ppi_of_is_conn (k l : ℕ₋₂) (H2 : l ≤ n.+1+2+k) (P : A → Type*) (H3 : Πa, is_trunc l (P a)) : is_trunc k.+1 (Π*(a : A), P a) := begin have H4 : Πa, is_trunc (n.+1+2+k) (P a), from λa, is_trunc_of_le (P a) H2, clear H2 H3, revert P H4, induction k with k IH: intro P H4, { apply is_prop_of_imp_is_contr, intro f, apply is_contr_ppi_match A n P H4 }, { apply is_trunc_succ_of_is_trunc_loop (trunc_index.succ_le_succ (trunc_index.minus_two_le k)), intro f, apply @is_trunc_equiv_closed_rev _ _ k.+1 (ppi_loop_equiv f), apply IH, intro a, apply is_trunc_loop, apply H4 } end definition is_trunc_pmap_of_is_conn (k l : ℕ₋₂) (B : Type*) (H2 : l ≤ n.+1+2+k) (H3 : is_trunc l B) : is_trunc k.+1 (A →* B) := @is_trunc_equiv_closed _ _ k.+1 (pppi_equiv_pmap A B) (is_trunc_ppi_of_is_conn A n k l H2 (λ a, B) _) end -- this is probably much easier to prove directly definition is_trunc_ppi (A : Type*) (n k : ℕ₋₂) (H : n ≤ k) (P : A → Type*) (H2 : Πa, is_trunc n (P a)) : is_trunc k (Π*(a : A), P a) := begin cases k with k, { apply is_contr_of_merely_prop, { exact @is_trunc_ppi_of_is_conn A -2 (is_conn_minus_one A (tr pt)) -2 _ (trunc_index.le.step H) P H2 }, { exact tr pt } }, { assert K : n ≤ -1 +2+ k, { rewrite (trunc_index.add_plus_two_comm -1 k), exact H }, { exact @is_trunc_ppi_of_is_conn A -2 (is_conn_minus_one A (tr pt)) k _ K P H2 } } end end is_conn /- TODO: move, these facts use some of these pointed properties -/ namespace trunc lemma pmap_ptrunc_pequiv_natural [constructor] (n : ℕ₋₂) {A A' B B' : Type*} [H : is_trunc n B] [H : is_trunc n B'] (f : A' →* A) (g : B →* B') : psquare (pmap_ptrunc_pequiv n A B) (pmap_ptrunc_pequiv n A' B') (ppcompose_left g ∘* ppcompose_right (ptrunc_functor n f)) (ppcompose_left g ∘* ppcompose_right f) := begin refine _ ⬝v* _, exact pmap_ptrunc_pequiv n A' B, { fapply phomotopy.mk, { intro h, apply eq_of_phomotopy, exact !passoc ⬝* pwhisker_left h (ptr_natural n f)⁻¹* ⬝* !passoc⁻¹* }, { xrewrite [▸*, +pcompose_right_eq_of_phomotopy, -+eq_of_phomotopy_trans], apply ap eq_of_phomotopy, refine !trans_assoc ⬝ idp ◾** (!trans_assoc⁻¹ ⬝ (eq_bot_of_phsquare (phtranspose (passoc_pconst_left (ptrunc_functor n f) (ptr n A'))))⁻¹) ⬝ _, refine !trans_assoc ⬝ idp ◾** !pconst_pcompose_phomotopy ⬝ _, apply passoc_pconst_left }}, { fapply phomotopy.mk, { intro h, apply eq_of_phomotopy, exact !passoc⁻¹* }, { xrewrite [▸*, pcompose_right_eq_of_phomotopy, pcompose_left_eq_of_phomotopy, -+eq_of_phomotopy_trans], apply ap eq_of_phomotopy, apply symm_trans_eq_of_eq_trans, symmetry, apply passoc_pconst_middle }} end end trunc
748254a05617e1792cec35a4196eeca5de811ce4
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/measure_theory/function/lp_space.lean
c4ecc2d2109e2c30b608fff95d648a05b5fd8980
[ "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
122,273
lean
/- Copyright (c) 2020 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne, Sébastien Gouëzel -/ import analysis.normed_space.indicator_function import analysis.normed.group.hom import measure_theory.function.ess_sup import measure_theory.function.ae_eq_fun import measure_theory.integral.mean_inequalities import topology.continuous_function.compact /-! # ℒp space and Lp space This file describes properties of almost everywhere strongly measurable functions with finite seminorm, denoted by `snorm f p μ` and defined for `p:ℝ≥0∞` as `0` if `p=0`, `(∫ ∥f a∥^p ∂μ) ^ (1/p)` for `0 < p < ∞` and `ess_sup ∥f∥ μ` for `p=∞`. The Prop-valued `mem_ℒp f p μ` states that a function `f : α → E` has finite seminorm. The space `Lp E p μ` is the subtype of elements of `α →ₘ[μ] E` (see ae_eq_fun) such that `snorm f p μ` is finite. For `1 ≤ p`, `snorm` defines a norm and `Lp` is a complete metric space. ## Main definitions * `snorm' f p μ` : `(∫ ∥f a∥^p ∂μ) ^ (1/p)` for `f : α → F` and `p : ℝ`, where `α` is a measurable space and `F` is a normed group. * `snorm_ess_sup f μ` : seminorm in `ℒ∞`, equal to the essential supremum `ess_sup ∥f∥ μ`. * `snorm f p μ` : for `p : ℝ≥0∞`, seminorm in `ℒp`, equal to `0` for `p=0`, to `snorm' f p μ` for `0 < p < ∞` and to `snorm_ess_sup f μ` for `p = ∞`. * `mem_ℒp f p μ` : property that the function `f` is almost everywhere strongly measurable and has finite `p`-seminorm for the measure `μ` (`snorm f p μ < ∞`) * `Lp E p μ` : elements of `α →ₘ[μ] E` (see ae_eq_fun) such that `snorm f p μ` is finite. Defined as an `add_subgroup` of `α →ₘ[μ] E`. Lipschitz functions vanishing at zero act by composition on `Lp`. We define this action, and prove that it is continuous. In particular, * `continuous_linear_map.comp_Lp` defines the action on `Lp` of a continuous linear map. * `Lp.pos_part` is the positive part of an `Lp` function. * `Lp.neg_part` is the negative part of an `Lp` function. When `α` is a topological space equipped with a finite Borel measure, there is a bounded linear map from the normed space of bounded continuous functions (`α →ᵇ E`) to `Lp E p μ`. We construct this as `bounded_continuous_function.to_Lp`. ## Notations * `α →₁[μ] E` : the type `Lp E 1 μ`. * `α →₂[μ] E` : the type `Lp E 2 μ`. ## Implementation Since `Lp` is defined as an `add_subgroup`, dot notation does not work. Use `Lp.measurable f` to say that the coercion of `f` to a genuine function is measurable, instead of the non-working `f.measurable`. To prove that two `Lp` elements are equal, it suffices to show that their coercions to functions coincide almost everywhere (this is registered as an `ext` rule). This can often be done using `filter_upwards`. For instance, a proof from first principles that `f + (g + h) = (f + g) + h` could read (in the `Lp` namespace) ``` example (f g h : Lp E p μ) : (f + g) + h = f + (g + h) := begin ext1, filter_upwards [coe_fn_add (f + g) h, coe_fn_add f g, coe_fn_add f (g + h), coe_fn_add g h] with _ ha1 ha2 ha3 ha4, simp only [ha1, ha2, ha3, ha4, add_assoc], end ``` The lemma `coe_fn_add` states that the coercion of `f + g` coincides almost everywhere with the sum of the coercions of `f` and `g`. All such lemmas use `coe_fn` in their name, to distinguish the function coercion from the coercion to almost everywhere defined functions. -/ noncomputable theory open topological_space measure_theory filter open_locale nnreal ennreal big_operators topological_space measure_theory variables {α E F G : Type*} {m m0 : measurable_space α} {p : ℝ≥0∞} {q : ℝ} {μ ν : measure α} [normed_add_comm_group E] [normed_add_comm_group F] [normed_add_comm_group G] namespace measure_theory section ℒp /-! ### ℒp seminorm We define the ℒp seminorm, denoted by `snorm f p μ`. For real `p`, it is given by an integral formula (for which we use the notation `snorm' f p μ`), and for `p = ∞` it is the essential supremum (for which we use the notation `snorm_ess_sup f μ`). We also define a predicate `mem_ℒp f p μ`, requesting that a function is almost everywhere measurable and has finite `snorm f p μ`. This paragraph is devoted to the basic properties of these definitions. It is constructed as follows: for a given property, we prove it for `snorm'` and `snorm_ess_sup` when it makes sense, deduce it for `snorm`, and translate it in terms of `mem_ℒp`. -/ section ℒp_space_definition /-- `(∫ ∥f a∥^q ∂μ) ^ (1/q)`, which is a seminorm on the space of measurable functions for which this quantity is finite -/ def snorm' {m : measurable_space α} (f : α → F) (q : ℝ) (μ : measure α) : ℝ≥0∞ := (∫⁻ a, ∥f a∥₊^q ∂μ) ^ (1/q) /-- seminorm for `ℒ∞`, equal to the essential supremum of `∥f∥`. -/ def snorm_ess_sup {m : measurable_space α} (f : α → F) (μ : measure α) := ess_sup (λ x, (∥f x∥₊ : ℝ≥0∞)) μ /-- `ℒp` seminorm, equal to `0` for `p=0`, to `(∫ ∥f a∥^p ∂μ) ^ (1/p)` for `0 < p < ∞` and to `ess_sup ∥f∥ μ` for `p = ∞`. -/ def snorm {m : measurable_space α} (f : α → F) (p : ℝ≥0∞) (μ : measure α) : ℝ≥0∞ := if p = 0 then 0 else (if p = ∞ then snorm_ess_sup f μ else snorm' f (ennreal.to_real p) μ) lemma snorm_eq_snorm' (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) {f : α → F} : snorm f p μ = snorm' f (ennreal.to_real p) μ := by simp [snorm, hp_ne_zero, hp_ne_top] lemma snorm_eq_lintegral_rpow_nnnorm (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) {f : α → F} : snorm f p μ = (∫⁻ x, ∥f x∥₊ ^ p.to_real ∂μ) ^ (1 / p.to_real) := by rw [snorm_eq_snorm' hp_ne_zero hp_ne_top, snorm'] lemma snorm_one_eq_lintegral_nnnorm {f : α → F} : snorm f 1 μ = ∫⁻ x, ∥f x∥₊ ∂μ := by simp_rw [snorm_eq_lintegral_rpow_nnnorm one_ne_zero ennreal.coe_ne_top, ennreal.one_to_real, one_div_one, ennreal.rpow_one] @[simp] lemma snorm_exponent_top {f : α → F} : snorm f ∞ μ = snorm_ess_sup f μ := by simp [snorm] /-- The property that `f:α→E` is ae strongly measurable and `(∫ ∥f a∥^p ∂μ)^(1/p)` is finite if `p < ∞`, or `ess_sup f < ∞` if `p = ∞`. -/ def mem_ℒp {α} {m : measurable_space α} (f : α → E) (p : ℝ≥0∞) (μ : measure α . volume_tac) : Prop := ae_strongly_measurable f μ ∧ snorm f p μ < ∞ lemma mem_ℒp.ae_strongly_measurable {f : α → E} {p : ℝ≥0∞} (h : mem_ℒp f p μ) : ae_strongly_measurable f μ := h.1 lemma lintegral_rpow_nnnorm_eq_rpow_snorm' {f : α → F} (hq0_lt : 0 < q) : ∫⁻ a, ∥f a∥₊ ^ q ∂μ = (snorm' f q μ) ^ q := begin rw [snorm', ←ennreal.rpow_mul, one_div, inv_mul_cancel, ennreal.rpow_one], exact (ne_of_lt hq0_lt).symm, end end ℒp_space_definition section top lemma mem_ℒp.snorm_lt_top {f : α → E} (hfp : mem_ℒp f p μ) : snorm f p μ < ∞ := hfp.2 lemma mem_ℒp.snorm_ne_top {f : α → E} (hfp : mem_ℒp f p μ) : snorm f p μ ≠ ∞ := ne_of_lt (hfp.2) lemma lintegral_rpow_nnnorm_lt_top_of_snorm'_lt_top {f : α → F} (hq0_lt : 0 < q) (hfq : snorm' f q μ < ∞) : ∫⁻ a, ∥f a∥₊ ^ q ∂μ < ∞ := begin rw lintegral_rpow_nnnorm_eq_rpow_snorm' hq0_lt, exact ennreal.rpow_lt_top_of_nonneg (le_of_lt hq0_lt) (ne_of_lt hfq), end lemma lintegral_rpow_nnnorm_lt_top_of_snorm_lt_top {f : α → F} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hfp : snorm f p μ < ∞) : ∫⁻ a, ∥f a∥₊ ^ p.to_real ∂μ < ∞ := begin apply lintegral_rpow_nnnorm_lt_top_of_snorm'_lt_top, { exact ennreal.to_real_pos hp_ne_zero hp_ne_top }, { simpa [snorm_eq_snorm' hp_ne_zero hp_ne_top] using hfp } end lemma snorm_lt_top_iff_lintegral_rpow_nnnorm_lt_top {f : α → F} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : snorm f p μ < ∞ ↔ ∫⁻ a, ∥f a∥₊ ^ p.to_real ∂μ < ∞ := ⟨lintegral_rpow_nnnorm_lt_top_of_snorm_lt_top hp_ne_zero hp_ne_top, begin intros h, have hp' := ennreal.to_real_pos hp_ne_zero hp_ne_top, have : 0 < 1 / p.to_real := div_pos zero_lt_one hp', simpa [snorm_eq_lintegral_rpow_nnnorm hp_ne_zero hp_ne_top] using ennreal.rpow_lt_top_of_nonneg (le_of_lt this) (ne_of_lt h) end⟩ end top section zero @[simp] lemma snorm'_exponent_zero {f : α → F} : snorm' f 0 μ = 1 := by rw [snorm', div_zero, ennreal.rpow_zero] @[simp] lemma snorm_exponent_zero {f : α → F} : snorm f 0 μ = 0 := by simp [snorm] lemma mem_ℒp_zero_iff_ae_strongly_measurable {f : α → E} : mem_ℒp f 0 μ ↔ ae_strongly_measurable f μ := by simp [mem_ℒp, snorm_exponent_zero] @[simp] lemma snorm'_zero (hp0_lt : 0 < q) : snorm' (0 : α → F) q μ = 0 := by simp [snorm', hp0_lt] @[simp] lemma snorm'_zero' (hq0_ne : q ≠ 0) (hμ : μ ≠ 0) : snorm' (0 : α → F) q μ = 0 := begin cases le_or_lt 0 q with hq0 hq_neg, { exact snorm'_zero (lt_of_le_of_ne hq0 hq0_ne.symm), }, { simp [snorm', ennreal.rpow_eq_zero_iff, hμ, hq_neg], }, end @[simp] lemma snorm_ess_sup_zero : snorm_ess_sup (0 : α → F) μ = 0 := begin simp_rw [snorm_ess_sup, pi.zero_apply, nnnorm_zero, ennreal.coe_zero, ←ennreal.bot_eq_zero], exact ess_sup_const_bot, end @[simp] lemma snorm_zero : snorm (0 : α → F) p μ = 0 := begin by_cases h0 : p = 0, { simp [h0], }, by_cases h_top : p = ∞, { simp only [h_top, snorm_exponent_top, snorm_ess_sup_zero], }, rw ←ne.def at h0, simp [snorm_eq_snorm' h0 h_top, ennreal.to_real_pos h0 h_top], end @[simp] lemma snorm_zero' : snorm (λ x : α, (0 : F)) p μ = 0 := by convert snorm_zero lemma zero_mem_ℒp : mem_ℒp (0 : α → E) p μ := ⟨ae_strongly_measurable_zero, by { rw snorm_zero, exact ennreal.coe_lt_top, } ⟩ lemma zero_mem_ℒp' : mem_ℒp (λ x : α, (0 : E)) p μ := by convert zero_mem_ℒp variables [measurable_space α] lemma snorm'_measure_zero_of_pos {f : α → F} (hq_pos : 0 < q) : snorm' f q (0 : measure α) = 0 := by simp [snorm', hq_pos] lemma snorm'_measure_zero_of_exponent_zero {f : α → F} : snorm' f 0 (0 : measure α) = 1 := by simp [snorm'] lemma snorm'_measure_zero_of_neg {f : α → F} (hq_neg : q < 0) : snorm' f q (0 : measure α) = ∞ := by simp [snorm', hq_neg] @[simp] lemma snorm_ess_sup_measure_zero {f : α → F} : snorm_ess_sup f (0 : measure α) = 0 := by simp [snorm_ess_sup] @[simp] lemma snorm_measure_zero {f : α → F} : snorm f p (0 : measure α) = 0 := begin by_cases h0 : p = 0, { simp [h0], }, by_cases h_top : p = ∞, { simp [h_top], }, rw ←ne.def at h0, simp [snorm_eq_snorm' h0 h_top, snorm', ennreal.to_real_pos h0 h_top], end end zero section const lemma snorm'_const (c : F) (hq_pos : 0 < q) : snorm' (λ x : α , c) q μ = (∥c∥₊ : ℝ≥0∞) * (μ set.univ) ^ (1/q) := begin rw [snorm', lintegral_const, ennreal.mul_rpow_of_nonneg _ _ (by simp [hq_pos.le] : 0 ≤ 1 / q)], congr, rw ←ennreal.rpow_mul, suffices hq_cancel : q * (1/q) = 1, by rw [hq_cancel, ennreal.rpow_one], rw [one_div, mul_inv_cancel (ne_of_lt hq_pos).symm], end lemma snorm'_const' [is_finite_measure μ] (c : F) (hc_ne_zero : c ≠ 0) (hq_ne_zero : q ≠ 0) : snorm' (λ x : α , c) q μ = (∥c∥₊ : ℝ≥0∞) * (μ set.univ) ^ (1/q) := begin rw [snorm', lintegral_const, ennreal.mul_rpow_of_ne_top _ (measure_ne_top μ set.univ)], { congr, rw ←ennreal.rpow_mul, suffices hp_cancel : q * (1/q) = 1, by rw [hp_cancel, ennreal.rpow_one], rw [one_div, mul_inv_cancel hq_ne_zero], }, { rw [ne.def, ennreal.rpow_eq_top_iff, not_or_distrib, not_and_distrib, not_and_distrib], split, { left, rwa [ennreal.coe_eq_zero, nnnorm_eq_zero], }, { exact or.inl ennreal.coe_ne_top, }, }, end lemma snorm_ess_sup_const (c : F) (hμ : μ ≠ 0) : snorm_ess_sup (λ x : α, c) μ = (∥c∥₊ : ℝ≥0∞) := by rw [snorm_ess_sup, ess_sup_const _ hμ] lemma snorm'_const_of_is_probability_measure (c : F) (hq_pos : 0 < q) [is_probability_measure μ] : snorm' (λ x : α , c) q μ = (∥c∥₊ : ℝ≥0∞) := by simp [snorm'_const c hq_pos, measure_univ] lemma snorm_const (c : F) (h0 : p ≠ 0) (hμ : μ ≠ 0) : snorm (λ x : α , c) p μ = (∥c∥₊ : ℝ≥0∞) * (μ set.univ) ^ (1/(ennreal.to_real p)) := begin by_cases h_top : p = ∞, { simp [h_top, snorm_ess_sup_const c hμ], }, simp [snorm_eq_snorm' h0 h_top, snorm'_const, ennreal.to_real_pos h0 h_top], end lemma snorm_const' (c : F) (h0 : p ≠ 0) (h_top: p ≠ ∞) : snorm (λ x : α , c) p μ = (∥c∥₊ : ℝ≥0∞) * (μ set.univ) ^ (1/(ennreal.to_real p)) := begin simp [snorm_eq_snorm' h0 h_top, snorm'_const, ennreal.to_real_pos h0 h_top], end lemma snorm_const_lt_top_iff {p : ℝ≥0∞} {c : F} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : snorm (λ x : α, c) p μ < ∞ ↔ c = 0 ∨ μ set.univ < ∞ := begin have hp : 0 < p.to_real, from ennreal.to_real_pos hp_ne_zero hp_ne_top, by_cases hμ : μ = 0, { simp only [hμ, measure.coe_zero, pi.zero_apply, or_true, with_top.zero_lt_top, snorm_measure_zero], }, by_cases hc : c = 0, { simp only [hc, true_or, eq_self_iff_true, with_top.zero_lt_top, snorm_zero'], }, rw snorm_const' c hp_ne_zero hp_ne_top, by_cases hμ_top : μ set.univ = ∞, { simp [hc, hμ_top, hp], }, rw ennreal.mul_lt_top_iff, simp only [true_and, one_div, ennreal.rpow_eq_zero_iff, hμ, false_or, or_false, ennreal.coe_lt_top, nnnorm_eq_zero, ennreal.coe_eq_zero, measure_theory.measure.measure_univ_eq_zero, hp, inv_lt_zero, hc, and_false, false_and, _root_.inv_pos, or_self, hμ_top, ne.lt_top hμ_top, iff_true], exact ennreal.rpow_lt_top_of_nonneg (inv_nonneg.mpr hp.le) hμ_top, end lemma mem_ℒp_const (c : E) [is_finite_measure μ] : mem_ℒp (λ a:α, c) p μ := begin refine ⟨ae_strongly_measurable_const, _⟩, by_cases h0 : p = 0, { simp [h0], }, by_cases hμ : μ = 0, { simp [hμ], }, rw snorm_const c h0 hμ, refine ennreal.mul_lt_top ennreal.coe_ne_top _, refine (ennreal.rpow_lt_top_of_nonneg _ (measure_ne_top μ set.univ)).ne, simp, end lemma mem_ℒp_top_const (c : E) : mem_ℒp (λ a:α, c) ∞ μ := begin refine ⟨ae_strongly_measurable_const, _⟩, by_cases h : μ = 0, { simp only [h, snorm_measure_zero, with_top.zero_lt_top] }, { rw snorm_const _ ennreal.top_ne_zero h, simp only [ennreal.top_to_real, div_zero, ennreal.rpow_zero, mul_one, ennreal.coe_lt_top] } end lemma mem_ℒp_const_iff {p : ℝ≥0∞} {c : E} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : mem_ℒp (λ x : α, c) p μ ↔ c = 0 ∨ μ set.univ < ∞ := begin rw ← snorm_const_lt_top_iff hp_ne_zero hp_ne_top, exact ⟨λ h, h.2, λ h, ⟨ae_strongly_measurable_const, h⟩⟩, end end const lemma snorm'_mono_ae {f : α → F} {g : α → G} (hq : 0 ≤ q) (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ ∥g x∥) : snorm' f q μ ≤ snorm' g q μ := begin rw [snorm'], refine ennreal.rpow_le_rpow _ (one_div_nonneg.2 hq), refine lintegral_mono_ae (h.mono $ λ x hx, _), exact ennreal.rpow_le_rpow (ennreal.coe_le_coe.2 hx) hq end lemma snorm'_congr_norm_ae {f g : α → F} (hfg : ∀ᵐ x ∂μ, ∥f x∥ = ∥g x∥) : snorm' f q μ = snorm' g q μ := begin have : (λ x, (∥f x∥₊ ^ q : ℝ≥0∞)) =ᵐ[μ] (λ x, ∥g x∥₊ ^ q), from hfg.mono (λ x hx, by { simp only [← coe_nnnorm, nnreal.coe_eq] at hx, simp [hx] }), simp only [snorm', lintegral_congr_ae this] end lemma snorm'_congr_ae {f g : α → F} (hfg : f =ᵐ[μ] g) : snorm' f q μ = snorm' g q μ := snorm'_congr_norm_ae (hfg.fun_comp _) lemma snorm_ess_sup_congr_ae {f g : α → F} (hfg : f =ᵐ[μ] g) : snorm_ess_sup f μ = snorm_ess_sup g μ := ess_sup_congr_ae (hfg.fun_comp (coe ∘ nnnorm)) lemma snorm_mono_ae {f : α → F} {g : α → G} (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ ∥g x∥) : snorm f p μ ≤ snorm g p μ := begin simp only [snorm], split_ifs, { exact le_rfl }, { refine ess_sup_mono_ae (h.mono $ λ x hx, _), exact_mod_cast hx }, { exact snorm'_mono_ae ennreal.to_real_nonneg h } end lemma snorm_mono_ae_real {f : α → F} {g : α → ℝ} (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ g x) : snorm f p μ ≤ snorm g p μ := snorm_mono_ae $ h.mono (λ x hx, hx.trans ((le_abs_self _).trans (real.norm_eq_abs _).symm.le)) lemma snorm_mono {f : α → F} {g : α → G} (h : ∀ x, ∥f x∥ ≤ ∥g x∥) : snorm f p μ ≤ snorm g p μ := snorm_mono_ae (eventually_of_forall (λ x, h x)) lemma snorm_mono_real {f : α → F} {g : α → ℝ} (h : ∀ x, ∥f x∥ ≤ g x) : snorm f p μ ≤ snorm g p μ := snorm_mono_ae_real (eventually_of_forall (λ x, h x)) lemma snorm_ess_sup_le_of_ae_bound {f : α → F} {C : ℝ} (hfC : ∀ᵐ x ∂μ, ∥f x∥ ≤ C) : snorm_ess_sup f μ ≤ ennreal.of_real C:= begin simp_rw [snorm_ess_sup, ← of_real_norm_eq_coe_nnnorm], refine ess_sup_le_of_ae_le (ennreal.of_real C) (hfC.mono (λ x hx, _)), exact ennreal.of_real_le_of_real hx, end lemma snorm_ess_sup_lt_top_of_ae_bound {f : α → F} {C : ℝ} (hfC : ∀ᵐ x ∂μ, ∥f x∥ ≤ C) : snorm_ess_sup f μ < ∞ := (snorm_ess_sup_le_of_ae_bound hfC).trans_lt ennreal.of_real_lt_top lemma snorm_le_of_ae_bound {f : α → F} {C : ℝ} (hfC : ∀ᵐ x ∂μ, ∥f x∥ ≤ C) : snorm f p μ ≤ ((μ set.univ) ^ p.to_real⁻¹) * (ennreal.of_real C) := begin by_cases hμ : μ = 0, { simp [hμ] }, haveI : μ.ae.ne_bot := ae_ne_bot.mpr hμ, by_cases hp : p = 0, { simp [hp] }, have hC : 0 ≤ C, from le_trans (norm_nonneg _) hfC.exists.some_spec, have hC' : ∥C∥ = C := by rw [real.norm_eq_abs, abs_eq_self.mpr hC], have : ∀ᵐ x ∂μ, ∥f x∥ ≤ ∥(λ _, C) x∥, from hfC.mono (λ x hx, hx.trans (le_of_eq hC'.symm)), convert snorm_mono_ae this, rw [snorm_const _ hp hμ, mul_comm, ← of_real_norm_eq_coe_nnnorm, hC', one_div] end lemma snorm_congr_norm_ae {f : α → F} {g : α → G} (hfg : ∀ᵐ x ∂μ, ∥f x∥ = ∥g x∥) : snorm f p μ = snorm g p μ := le_antisymm (snorm_mono_ae $ eventually_eq.le hfg) (snorm_mono_ae $ (eventually_eq.symm hfg).le) @[simp] lemma snorm'_norm {f : α → F} : snorm' (λ a, ∥f a∥) q μ = snorm' f q μ := by simp [snorm'] @[simp] lemma snorm_norm (f : α → F) : snorm (λ x, ∥f x∥) p μ = snorm f p μ := snorm_congr_norm_ae $ eventually_of_forall $ λ x, norm_norm _ lemma snorm'_norm_rpow (f : α → F) (p q : ℝ) (hq_pos : 0 < q) : snorm' (λ x, ∥f x∥ ^ q) p μ = (snorm' f (p * q) μ) ^ q := begin simp_rw snorm', rw [← ennreal.rpow_mul, ←one_div_mul_one_div], simp_rw one_div, rw [mul_assoc, inv_mul_cancel hq_pos.ne.symm, mul_one], congr, ext1 x, simp_rw ← of_real_norm_eq_coe_nnnorm, rw [real.norm_eq_abs, abs_eq_self.mpr (real.rpow_nonneg_of_nonneg (norm_nonneg _) _), mul_comm, ← ennreal.of_real_rpow_of_nonneg (norm_nonneg _) hq_pos.le, ennreal.rpow_mul], end lemma snorm_norm_rpow (f : α → F) (hq_pos : 0 < q) : snorm (λ x, ∥f x∥ ^ q) p μ = (snorm f (p * ennreal.of_real q) μ) ^ q := begin by_cases h0 : p = 0, { simp [h0, ennreal.zero_rpow_of_pos hq_pos], }, by_cases hp_top : p = ∞, { simp only [hp_top, snorm_exponent_top, ennreal.top_mul, hq_pos.not_le, ennreal.of_real_eq_zero, if_false, snorm_exponent_top, snorm_ess_sup], have h_rpow : ess_sup (λ (x : α), (∥(∥f x∥ ^ q)∥₊ : ℝ≥0∞)) μ = ess_sup (λ (x : α), (↑∥f x∥₊) ^ q) μ, { congr, ext1 x, nth_rewrite 1 ← nnnorm_norm, rw [ennreal.coe_rpow_of_nonneg _ hq_pos.le, ennreal.coe_eq_coe], ext, push_cast, rw real.norm_rpow_of_nonneg (norm_nonneg _), }, rw h_rpow, have h_rpow_mono := ennreal.strict_mono_rpow_of_pos hq_pos, have h_rpow_surj := (ennreal.rpow_left_bijective hq_pos.ne.symm).2, let iso := h_rpow_mono.order_iso_of_surjective _ h_rpow_surj, exact (iso.ess_sup_apply (λ x, (∥f x∥₊ : ℝ≥0∞)) μ).symm, }, rw [snorm_eq_snorm' h0 hp_top, snorm_eq_snorm' _ _], swap, { refine mul_ne_zero h0 _, rwa [ne.def, ennreal.of_real_eq_zero, not_le], }, swap, { exact ennreal.mul_ne_top hp_top ennreal.of_real_ne_top, }, rw [ennreal.to_real_mul, ennreal.to_real_of_real hq_pos.le], exact snorm'_norm_rpow f p.to_real q hq_pos, end lemma snorm_congr_ae {f g : α → F} (hfg : f =ᵐ[μ] g) : snorm f p μ = snorm g p μ := snorm_congr_norm_ae $ hfg.mono (λ x hx, hx ▸ rfl) lemma mem_ℒp_congr_ae {f g : α → E} (hfg : f =ᵐ[μ] g) : mem_ℒp f p μ ↔ mem_ℒp g p μ := by simp only [mem_ℒp, snorm_congr_ae hfg, ae_strongly_measurable_congr hfg] lemma mem_ℒp.ae_eq {f g : α → E} (hfg : f =ᵐ[μ] g) (hf_Lp : mem_ℒp f p μ) : mem_ℒp g p μ := (mem_ℒp_congr_ae hfg).1 hf_Lp lemma mem_ℒp.of_le {f : α → E} {g : α → F} (hg : mem_ℒp g p μ) (hf : ae_strongly_measurable f μ) (hfg : ∀ᵐ x ∂μ, ∥f x∥ ≤ ∥g x∥) : mem_ℒp f p μ := ⟨hf, (snorm_mono_ae hfg).trans_lt hg.snorm_lt_top⟩ alias mem_ℒp.of_le ← mem_ℒp.mono lemma mem_ℒp.mono' {f : α → E} {g : α → ℝ} (hg : mem_ℒp g p μ) (hf : ae_strongly_measurable f μ) (h : ∀ᵐ a ∂μ, ∥f a∥ ≤ g a) : mem_ℒp f p μ := hg.mono hf $ h.mono $ λ x hx, le_trans hx (le_abs_self _) lemma mem_ℒp.congr_norm {f : α → E} {g : α → F} (hf : mem_ℒp f p μ) (hg : ae_strongly_measurable g μ) (h : ∀ᵐ a ∂μ, ∥f a∥ = ∥g a∥) : mem_ℒp g p μ := hf.mono hg $ eventually_eq.le $ eventually_eq.symm h lemma mem_ℒp_congr_norm {f : α → E} {g : α → F} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) (h : ∀ᵐ a ∂μ, ∥f a∥ = ∥g a∥) : mem_ℒp f p μ ↔ mem_ℒp g p μ := ⟨λ h2f, h2f.congr_norm hg h, λ h2g, h2g.congr_norm hf $ eventually_eq.symm h⟩ lemma mem_ℒp_top_of_bound {f : α → E} (hf : ae_strongly_measurable f μ) (C : ℝ) (hfC : ∀ᵐ x ∂μ, ∥f x∥ ≤ C) : mem_ℒp f ∞ μ := ⟨hf, by { rw snorm_exponent_top, exact snorm_ess_sup_lt_top_of_ae_bound hfC, }⟩ lemma mem_ℒp.of_bound [is_finite_measure μ] {f : α → E} (hf : ae_strongly_measurable f μ) (C : ℝ) (hfC : ∀ᵐ x ∂μ, ∥f x∥ ≤ C) : mem_ℒp f p μ := (mem_ℒp_const C).of_le hf (hfC.mono (λ x hx, le_trans hx (le_abs_self _))) @[mono] lemma snorm'_mono_measure (f : α → F) (hμν : ν ≤ μ) (hq : 0 ≤ q) : snorm' f q ν ≤ snorm' f q μ := begin simp_rw snorm', suffices h_integral_mono : (∫⁻ a, (∥f a∥₊ : ℝ≥0∞) ^ q ∂ν) ≤ ∫⁻ a, ∥f a∥₊ ^ q ∂μ, from ennreal.rpow_le_rpow h_integral_mono (by simp [hq]), exact lintegral_mono' hμν le_rfl, end @[mono] lemma snorm_ess_sup_mono_measure (f : α → F) (hμν : ν ≪ μ) : snorm_ess_sup f ν ≤ snorm_ess_sup f μ := by { simp_rw snorm_ess_sup, exact ess_sup_mono_measure hμν, } @[mono] lemma snorm_mono_measure (f : α → F) (hμν : ν ≤ μ) : snorm f p ν ≤ snorm f p μ := begin by_cases hp0 : p = 0, { simp [hp0], }, by_cases hp_top : p = ∞, { simp [hp_top, snorm_ess_sup_mono_measure f (measure.absolutely_continuous_of_le hμν)], }, simp_rw snorm_eq_snorm' hp0 hp_top, exact snorm'_mono_measure f hμν ennreal.to_real_nonneg, end lemma mem_ℒp.mono_measure {f : α → E} (hμν : ν ≤ μ) (hf : mem_ℒp f p μ) : mem_ℒp f p ν := ⟨hf.1.mono_measure hμν, (snorm_mono_measure f hμν).trans_lt hf.2⟩ lemma mem_ℒp.restrict (s : set α) {f : α → E} (hf : mem_ℒp f p μ) : mem_ℒp f p (μ.restrict s) := hf.mono_measure measure.restrict_le_self lemma snorm'_smul_measure {p : ℝ} (hp : 0 ≤ p) {f : α → F} (c : ℝ≥0∞) : snorm' f p (c • μ) = c ^ (1 / p) * snorm' f p μ := by { rw [snorm', lintegral_smul_measure, ennreal.mul_rpow_of_nonneg, snorm'], simp [hp], } lemma snorm_ess_sup_smul_measure {f : α → F} {c : ℝ≥0∞} (hc : c ≠ 0) : snorm_ess_sup f (c • μ) = snorm_ess_sup f μ := by { simp_rw [snorm_ess_sup], exact ess_sup_smul_measure hc, } /-- Use `snorm_smul_measure_of_ne_top` instead. -/ private lemma snorm_smul_measure_of_ne_zero_of_ne_top {p : ℝ≥0∞} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) {f : α → F} (c : ℝ≥0∞) : snorm f p (c • μ) = c ^ (1 / p).to_real • snorm f p μ := begin simp_rw snorm_eq_snorm' hp_ne_zero hp_ne_top, rw snorm'_smul_measure ennreal.to_real_nonneg, congr, simp_rw one_div, rw ennreal.to_real_inv, end lemma snorm_smul_measure_of_ne_zero {p : ℝ≥0∞} {f : α → F} {c : ℝ≥0∞} (hc : c ≠ 0) : snorm f p (c • μ) = c ^ (1 / p).to_real • snorm f p μ := begin by_cases hp0 : p = 0, { simp [hp0], }, by_cases hp_top : p = ∞, { simp [hp_top, snorm_ess_sup_smul_measure hc], }, exact snorm_smul_measure_of_ne_zero_of_ne_top hp0 hp_top c, end lemma snorm_smul_measure_of_ne_top {p : ℝ≥0∞} (hp_ne_top : p ≠ ∞) {f : α → F} (c : ℝ≥0∞) : snorm f p (c • μ) = c ^ (1 / p).to_real • snorm f p μ := begin by_cases hp0 : p = 0, { simp [hp0], }, { exact snorm_smul_measure_of_ne_zero_of_ne_top hp0 hp_ne_top c, }, end lemma snorm_one_smul_measure {f : α → F} (c : ℝ≥0∞) : snorm f 1 (c • μ) = c * snorm f 1 μ := by { rw @snorm_smul_measure_of_ne_top _ _ _ μ _ 1 (@ennreal.coe_ne_top 1) f c, simp, } lemma mem_ℒp.of_measure_le_smul {μ' : measure α} (c : ℝ≥0∞) (hc : c ≠ ∞) (hμ'_le : μ' ≤ c • μ) {f : α → E} (hf : mem_ℒp f p μ) : mem_ℒp f p μ' := begin refine ⟨hf.1.mono' (measure.absolutely_continuous_of_le_smul hμ'_le), _⟩, refine (snorm_mono_measure f hμ'_le).trans_lt _, by_cases hc0 : c = 0, { simp [hc0], }, rw [snorm_smul_measure_of_ne_zero hc0, smul_eq_mul], refine ennreal.mul_lt_top _ hf.2.ne, simp [hc, hc0], end lemma mem_ℒp.smul_measure {f : α → E} {c : ℝ≥0∞} (hf : mem_ℒp f p μ) (hc : c ≠ ∞) : mem_ℒp f p (c • μ) := hf.of_measure_le_smul c hc le_rfl include m lemma snorm_one_add_measure (f : α → F) (μ ν : measure α) : snorm f 1 (μ + ν) = snorm f 1 μ + snorm f 1 ν := by { simp_rw snorm_one_eq_lintegral_nnnorm, rw lintegral_add_measure _ μ ν, } lemma snorm_le_add_measure_right (f : α → F) (μ ν : measure α) {p : ℝ≥0∞} : snorm f p μ ≤ snorm f p (μ + ν) := snorm_mono_measure f $ measure.le_add_right $ le_refl _ lemma snorm_le_add_measure_left (f : α → F) (μ ν : measure α) {p : ℝ≥0∞} : snorm f p ν ≤ snorm f p (μ + ν) := snorm_mono_measure f $ measure.le_add_left $ le_refl _ omit m lemma mem_ℒp.left_of_add_measure {f : α → E} (h : mem_ℒp f p (μ + ν)) : mem_ℒp f p μ := h.mono_measure $ measure.le_add_right $ le_refl _ lemma mem_ℒp.right_of_add_measure {f : α → E} (h : mem_ℒp f p (μ + ν)) : mem_ℒp f p ν := h.mono_measure $ measure.le_add_left $ le_refl _ lemma mem_ℒp.norm {f : α → E} (h : mem_ℒp f p μ) : mem_ℒp (λ x, ∥f x∥) p μ := h.of_le h.ae_strongly_measurable.norm (eventually_of_forall (λ x, by simp)) lemma mem_ℒp_norm_iff {f : α → E} (hf : ae_strongly_measurable f μ) : mem_ℒp (λ x, ∥f x∥) p μ ↔ mem_ℒp f p μ := ⟨λ h, ⟨hf, by { rw ← snorm_norm, exact h.2, }⟩, λ h, h.norm⟩ lemma snorm'_eq_zero_of_ae_zero {f : α → F} (hq0_lt : 0 < q) (hf_zero : f =ᵐ[μ] 0) : snorm' f q μ = 0 := by rw [snorm'_congr_ae hf_zero, snorm'_zero hq0_lt] lemma snorm'_eq_zero_of_ae_zero' (hq0_ne : q ≠ 0) (hμ : μ ≠ 0) {f : α → F} (hf_zero : f =ᵐ[μ] 0) : snorm' f q μ = 0 := by rw [snorm'_congr_ae hf_zero, snorm'_zero' hq0_ne hμ] lemma ae_eq_zero_of_snorm'_eq_zero {f : α → E} (hq0 : 0 ≤ q) (hf : ae_strongly_measurable f μ) (h : snorm' f q μ = 0) : f =ᵐ[μ] 0 := begin rw [snorm', ennreal.rpow_eq_zero_iff] at h, cases h, { rw lintegral_eq_zero_iff' (hf.ennnorm.pow_const q) at h, refine h.left.mono (λ x hx, _), rw [pi.zero_apply, ennreal.rpow_eq_zero_iff] at hx, cases hx, { cases hx with hx _, rwa [←ennreal.coe_zero, ennreal.coe_eq_coe, nnnorm_eq_zero] at hx, }, { exact absurd hx.left ennreal.coe_ne_top, }, }, { exfalso, rw [one_div, inv_lt_zero] at h, exact hq0.not_lt h.right }, end lemma snorm'_eq_zero_iff (hq0_lt : 0 < q) {f : α → E} (hf : ae_strongly_measurable f μ) : snorm' f q μ = 0 ↔ f =ᵐ[μ] 0 := ⟨ae_eq_zero_of_snorm'_eq_zero (le_of_lt hq0_lt) hf, snorm'_eq_zero_of_ae_zero hq0_lt⟩ lemma coe_nnnorm_ae_le_snorm_ess_sup {m : measurable_space α} (f : α → F) (μ : measure α) : ∀ᵐ x ∂μ, (∥f x∥₊ : ℝ≥0∞) ≤ snorm_ess_sup f μ := ennreal.ae_le_ess_sup (λ x, (∥f x∥₊ : ℝ≥0∞)) @[simp] lemma snorm_ess_sup_eq_zero_iff {f : α → F} : snorm_ess_sup f μ = 0 ↔ f =ᵐ[μ] 0 := by simp [eventually_eq, snorm_ess_sup] lemma snorm_eq_zero_iff {f : α → E} (hf : ae_strongly_measurable f μ) (h0 : p ≠ 0) : snorm f p μ = 0 ↔ f =ᵐ[μ] 0 := begin by_cases h_top : p = ∞, { rw [h_top, snorm_exponent_top, snorm_ess_sup_eq_zero_iff], }, rw snorm_eq_snorm' h0 h_top, exact snorm'_eq_zero_iff (ennreal.to_real_pos h0 h_top) hf, end lemma snorm'_add_le {f g : α → E} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) (hq1 : 1 ≤ q) : snorm' (f + g) q μ ≤ snorm' f q μ + snorm' g q μ := calc (∫⁻ a, ↑∥(f + g) a∥₊ ^ q ∂μ) ^ (1 / q) ≤ (∫⁻ a, (((λ a, (∥f a∥₊ : ℝ≥0∞)) + (λ a, (∥g a∥₊ : ℝ≥0∞))) a) ^ q ∂μ) ^ (1 / q) : begin refine ennreal.rpow_le_rpow _ (by simp [le_trans zero_le_one hq1] : 0 ≤ 1 / q), refine lintegral_mono (λ a, ennreal.rpow_le_rpow _ (le_trans zero_le_one hq1)), simp [←ennreal.coe_add, nnnorm_add_le], end ... ≤ snorm' f q μ + snorm' g q μ : ennreal.lintegral_Lp_add_le hf.ennnorm hg.ennnorm hq1 lemma snorm_ess_sup_add_le {f g : α → F} : snorm_ess_sup (f + g) μ ≤ snorm_ess_sup f μ + snorm_ess_sup g μ := begin refine le_trans (ess_sup_mono_ae (eventually_of_forall (λ x, _))) (ennreal.ess_sup_add_le _ _), simp_rw [pi.add_apply, ←ennreal.coe_add, ennreal.coe_le_coe], exact nnnorm_add_le _ _, end lemma snorm_add_le {f g : α → E} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) (hp1 : 1 ≤ p) : snorm (f + g) p μ ≤ snorm f p μ + snorm g p μ := begin by_cases hp0 : p = 0, { simp [hp0], }, by_cases hp_top : p = ∞, { simp [hp_top, snorm_ess_sup_add_le], }, have hp1_real : 1 ≤ p.to_real, by rwa [← ennreal.one_to_real, ennreal.to_real_le_to_real ennreal.one_ne_top hp_top], repeat { rw snorm_eq_snorm' hp0 hp_top, }, exact snorm'_add_le hf hg hp1_real, end lemma snorm_sub_le {f g : α → E} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) (hp1 : 1 ≤ p) : snorm (f - g) p μ ≤ snorm f p μ + snorm g p μ := calc snorm (f - g) p μ = snorm (f + - g) p μ : by rw sub_eq_add_neg -- We cannot use snorm_add_le on f and (-g) because we don't have `ae_measurable (-g) μ`, since -- we don't suppose `[borel_space E]`. ... = snorm (λ x, ∥f x + - g x∥) p μ : (snorm_norm (f + - g)).symm ... ≤ snorm (λ x, ∥f x∥ + ∥- g x∥) p μ : by { refine snorm_mono_real (λ x, _), rw norm_norm, exact norm_add_le _ _, } ... = snorm (λ x, ∥f x∥ + ∥g x∥) p μ : by simp_rw norm_neg ... ≤ snorm (λ x, ∥f x∥) p μ + snorm (λ x, ∥g x∥) p μ : snorm_add_le hf.norm hg.norm hp1 ... = snorm f p μ + snorm g p μ : by rw [← snorm_norm f, ← snorm_norm g] lemma snorm_add_lt_top_of_one_le {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) (hq1 : 1 ≤ p) : snorm (f + g) p μ < ∞ := lt_of_le_of_lt (snorm_add_le hf.1 hg.1 hq1) (ennreal.add_lt_top.mpr ⟨hf.2, hg.2⟩) lemma snorm'_add_lt_top_of_le_one {f g : α → E} (hf : ae_strongly_measurable f μ) (hf_snorm : snorm' f q μ < ∞) (hg_snorm : snorm' g q μ < ∞) (hq_pos : 0 < q) (hq1 : q ≤ 1) : snorm' (f + g) q μ < ∞ := calc (∫⁻ a, ↑∥(f + g) a∥₊ ^ q ∂μ) ^ (1 / q) ≤ (∫⁻ a, (((λ a, (∥f a∥₊ : ℝ≥0∞)) + (λ a, (∥g a∥₊ : ℝ≥0∞))) a) ^ q ∂μ) ^ (1 / q) : begin refine ennreal.rpow_le_rpow _ (by simp [hq_pos.le] : 0 ≤ 1 / q), refine lintegral_mono (λ a, ennreal.rpow_le_rpow _ hq_pos.le), simp [←ennreal.coe_add, nnnorm_add_le], end ... ≤ (∫⁻ a, (∥f a∥₊ : ℝ≥0∞) ^ q + (∥g a∥₊ : ℝ≥0∞) ^ q ∂μ) ^ (1 / q) : begin refine ennreal.rpow_le_rpow (lintegral_mono (λ a, _)) (by simp [hq_pos.le] : 0 ≤ 1 / q), exact ennreal.rpow_add_le_add_rpow _ _ hq_pos.le hq1, end ... < ∞ : begin refine ennreal.rpow_lt_top_of_nonneg (by simp [hq_pos.le] : 0 ≤ 1 / q) _, rw [lintegral_add_left' (hf.ennnorm.pow_const q), ennreal.add_ne_top], exact ⟨(lintegral_rpow_nnnorm_lt_top_of_snorm'_lt_top hq_pos hf_snorm).ne, (lintegral_rpow_nnnorm_lt_top_of_snorm'_lt_top hq_pos hg_snorm).ne⟩, end lemma snorm_add_lt_top {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : snorm (f + g) p μ < ∞ := begin by_cases h0 : p = 0, { simp [h0], }, rw ←ne.def at h0, cases le_total 1 p with hp1 hp1, { exact snorm_add_lt_top_of_one_le hf hg hp1, }, have hp_top : p ≠ ∞, from (lt_of_le_of_lt hp1 ennreal.coe_lt_top).ne, have hp_pos : 0 < p.to_real, { rw [← ennreal.zero_to_real, @ennreal.to_real_lt_to_real 0 p ennreal.coe_ne_top hp_top], exact ((zero_le p).lt_of_ne h0.symm), }, have hp1_real : p.to_real ≤ 1, { rwa [← ennreal.one_to_real, @ennreal.to_real_le_to_real p 1 hp_top ennreal.coe_ne_top], }, rw snorm_eq_snorm' h0 hp_top, rw [mem_ℒp, snorm_eq_snorm' h0 hp_top] at hf hg, exact snorm'_add_lt_top_of_le_one hf.1 hf.2 hg.2 hp_pos hp1_real, end section map_measure variables {β : Type*} {mβ : measurable_space β} {f : α → β} {g : β → E} include mβ lemma snorm_ess_sup_map_measure (hg : ae_strongly_measurable g (measure.map f μ)) (hf : ae_measurable f μ) : snorm_ess_sup g (measure.map f μ) = snorm_ess_sup (g ∘ f) μ := ess_sup_map_measure hg.ennnorm hf lemma snorm_map_measure (hg : ae_strongly_measurable g (measure.map f μ)) (hf : ae_measurable f μ) : snorm g p (measure.map f μ) = snorm (g ∘ f) p μ := begin by_cases hp_zero : p = 0, { simp only [hp_zero, snorm_exponent_zero], }, by_cases hp_top : p = ∞, { simp_rw [hp_top, snorm_exponent_top], exact snorm_ess_sup_map_measure hg hf, }, simp_rw snorm_eq_lintegral_rpow_nnnorm hp_zero hp_top, rw lintegral_map' (hg.ennnorm.pow_const p.to_real) hf, end lemma mem_ℒp_map_measure_iff (hg : ae_strongly_measurable g (measure.map f μ)) (hf : ae_measurable f μ) : mem_ℒp g p (measure.map f μ) ↔ mem_ℒp (g ∘ f) p μ := by simp [mem_ℒp, snorm_map_measure hg hf, hg.comp_ae_measurable hf, hg] lemma _root_.measurable_embedding.snorm_ess_sup_map_measure {g : β → F} (hf : measurable_embedding f) : snorm_ess_sup g (measure.map f μ) = snorm_ess_sup (g ∘ f) μ := hf.ess_sup_map_measure lemma _root_.measurable_embedding.snorm_map_measure {g : β → F} (hf : measurable_embedding f) : snorm g p (measure.map f μ) = snorm (g ∘ f) p μ := begin by_cases hp_zero : p = 0, { simp only [hp_zero, snorm_exponent_zero], }, by_cases hp : p = ∞, { simp_rw [hp, snorm_exponent_top], exact hf.ess_sup_map_measure, }, { simp_rw snorm_eq_lintegral_rpow_nnnorm hp_zero hp, rw hf.lintegral_map, }, end lemma _root_.measurable_embedding.mem_ℒp_map_measure_iff {g : β → F} (hf : measurable_embedding f) : mem_ℒp g p (measure.map f μ) ↔ mem_ℒp (g ∘ f) p μ := by simp_rw [mem_ℒp, hf.ae_strongly_measurable_map_iff, hf.snorm_map_measure] lemma _root_.measurable_equiv.mem_ℒp_map_measure_iff (f : α ≃ᵐ β) {g : β → F} : mem_ℒp g p (measure.map f μ) ↔ mem_ℒp (g ∘ f) p μ := f.measurable_embedding.mem_ℒp_map_measure_iff omit mβ end map_measure section trim lemma snorm'_trim (hm : m ≤ m0) {f : α → E} (hf : strongly_measurable[m] f) : snorm' f q (ν.trim hm) = snorm' f q ν := begin simp_rw snorm', congr' 1, refine lintegral_trim hm _, refine @measurable.pow_const _ _ _ _ _ _ _ m _ (@measurable.coe_nnreal_ennreal _ m _ _) _, apply @strongly_measurable.measurable, exact (@strongly_measurable.nnnorm α m _ _ _ hf), end lemma limsup_trim (hm : m ≤ m0) {f : α → ℝ≥0∞} (hf : measurable[m] f) : (ν.trim hm).ae.limsup f = ν.ae.limsup f := begin simp_rw limsup_eq, suffices h_set_eq : {a : ℝ≥0∞ | ∀ᵐ n ∂(ν.trim hm), f n ≤ a} = {a : ℝ≥0∞ | ∀ᵐ n ∂ν, f n ≤ a}, by rw h_set_eq, ext1 a, suffices h_meas_eq : ν {x | ¬ f x ≤ a} = ν.trim hm {x | ¬ f x ≤ a}, by simp_rw [set.mem_set_of_eq, ae_iff, h_meas_eq], refine (trim_measurable_set_eq hm _).symm, refine @measurable_set.compl _ _ m (@measurable_set_le ℝ≥0∞ _ _ _ _ m _ _ _ _ _ hf _), exact @measurable_const _ _ _ m _, end lemma ess_sup_trim (hm : m ≤ m0) {f : α → ℝ≥0∞} (hf : measurable[m] f) : ess_sup f (ν.trim hm) = ess_sup f ν := by { simp_rw ess_sup, exact limsup_trim hm hf, } lemma snorm_ess_sup_trim (hm : m ≤ m0) {f : α → E} (hf : strongly_measurable[m] f) : snorm_ess_sup f (ν.trim hm) = snorm_ess_sup f ν := ess_sup_trim _ (@strongly_measurable.ennnorm _ m _ _ _ hf) lemma snorm_trim (hm : m ≤ m0) {f : α → E} (hf : strongly_measurable[m] f) : snorm f p (ν.trim hm) = snorm f p ν := begin by_cases h0 : p = 0, { simp [h0], }, by_cases h_top : p = ∞, { simpa only [h_top, snorm_exponent_top] using snorm_ess_sup_trim hm hf, }, simpa only [snorm_eq_snorm' h0 h_top] using snorm'_trim hm hf, end lemma snorm_trim_ae (hm : m ≤ m0) {f : α → E} (hf : ae_strongly_measurable f (ν.trim hm)) : snorm f p (ν.trim hm) = snorm f p ν := begin rw [snorm_congr_ae hf.ae_eq_mk, snorm_congr_ae (ae_eq_of_ae_eq_trim hf.ae_eq_mk)], exact snorm_trim hm hf.strongly_measurable_mk, end lemma mem_ℒp_of_mem_ℒp_trim (hm : m ≤ m0) {f : α → E} (hf : mem_ℒp f p (ν.trim hm)) : mem_ℒp f p ν := ⟨ae_strongly_measurable_of_ae_strongly_measurable_trim hm hf.1, (le_of_eq (snorm_trim_ae hm hf.1).symm).trans_lt hf.2⟩ end trim @[simp] lemma snorm'_neg {f : α → F} : snorm' (-f) q μ = snorm' f q μ := by simp [snorm'] @[simp] lemma snorm_neg {f : α → F} : snorm (-f) p μ = snorm f p μ := begin by_cases h0 : p = 0, { simp [h0], }, by_cases h_top : p = ∞, { simp [h_top, snorm_ess_sup], }, simp [snorm_eq_snorm' h0 h_top], end section borel_space -- variable [borel_space E] lemma mem_ℒp.neg {f : α → E} (hf : mem_ℒp f p μ) : mem_ℒp (-f) p μ := ⟨ae_strongly_measurable.neg hf.1, by simp [hf.right]⟩ lemma mem_ℒp_neg_iff {f : α → E} : mem_ℒp (-f) p μ ↔ mem_ℒp f p μ := ⟨λ h, neg_neg f ▸ h.neg, mem_ℒp.neg⟩ lemma snorm'_le_snorm'_mul_rpow_measure_univ {p q : ℝ} (hp0_lt : 0 < p) (hpq : p ≤ q) {f : α → E} (hf : ae_strongly_measurable f μ) : snorm' f p μ ≤ snorm' f q μ * (μ set.univ) ^ (1/p - 1/q) := begin have hq0_lt : 0 < q, from lt_of_lt_of_le hp0_lt hpq, by_cases hpq_eq : p = q, { rw [hpq_eq, sub_self, ennreal.rpow_zero, mul_one], exact le_rfl, }, have hpq : p < q, from lt_of_le_of_ne hpq hpq_eq, let g := λ a : α, (1 : ℝ≥0∞), have h_rw : ∫⁻ a, ↑∥f a∥₊^p ∂ μ = ∫⁻ a, (∥f a∥₊ * (g a))^p ∂ μ, from lintegral_congr (λ a, by simp), repeat {rw snorm'}, rw h_rw, let r := p * q / (q - p), have hpqr : 1/p = 1/q + 1/r, { field_simp [(ne_of_lt hp0_lt).symm, (ne_of_lt hq0_lt).symm], ring, }, calc (∫⁻ (a : α), (↑∥f a∥₊ * g a) ^ p ∂μ) ^ (1/p) ≤ (∫⁻ (a : α), ↑∥f a∥₊ ^ q ∂μ) ^ (1/q) * (∫⁻ (a : α), (g a) ^ r ∂μ) ^ (1/r) : ennreal.lintegral_Lp_mul_le_Lq_mul_Lr hp0_lt hpq hpqr μ hf.ennnorm ae_measurable_const ... = (∫⁻ (a : α), ↑∥f a∥₊ ^ q ∂μ) ^ (1/q) * μ set.univ ^ (1/p - 1/q) : by simp [hpqr], end lemma snorm'_le_snorm_ess_sup_mul_rpow_measure_univ (hq_pos : 0 < q) {f : α → F} : snorm' f q μ ≤ snorm_ess_sup f μ * (μ set.univ) ^ (1/q) := begin have h_le : ∫⁻ (a : α), ↑∥f a∥₊ ^ q ∂μ ≤ ∫⁻ (a : α), (snorm_ess_sup f μ) ^ q ∂μ, { refine lintegral_mono_ae _, have h_nnnorm_le_snorm_ess_sup := coe_nnnorm_ae_le_snorm_ess_sup f μ, refine h_nnnorm_le_snorm_ess_sup.mono (λ x hx, ennreal.rpow_le_rpow hx (le_of_lt hq_pos)), }, rw [snorm', ←ennreal.rpow_one (snorm_ess_sup f μ)], nth_rewrite 1 ←mul_inv_cancel (ne_of_lt hq_pos).symm, rw [ennreal.rpow_mul, one_div, ←ennreal.mul_rpow_of_nonneg _ _ (by simp [hq_pos.le] : 0 ≤ q⁻¹)], refine ennreal.rpow_le_rpow _ (by simp [hq_pos.le]), rwa lintegral_const at h_le, end lemma snorm_le_snorm_mul_rpow_measure_univ {p q : ℝ≥0∞} (hpq : p ≤ q) {f : α → E} (hf : ae_strongly_measurable f μ) : snorm f p μ ≤ snorm f q μ * (μ set.univ) ^ (1/p.to_real - 1/q.to_real) := begin by_cases hp0 : p = 0, { simp [hp0, zero_le], }, rw ← ne.def at hp0, have hp0_lt : 0 < p, from lt_of_le_of_ne (zero_le _) hp0.symm, have hq0_lt : 0 < q, from lt_of_lt_of_le hp0_lt hpq, by_cases hq_top : q = ∞, { simp only [hq_top, div_zero, one_div, ennreal.top_to_real, sub_zero, snorm_exponent_top, inv_zero], by_cases hp_top : p = ∞, { simp only [hp_top, ennreal.rpow_zero, mul_one, ennreal.top_to_real, sub_zero, inv_zero, snorm_exponent_top], exact le_rfl, }, rw snorm_eq_snorm' hp0 hp_top, have hp_pos : 0 < p.to_real, from ennreal.to_real_pos hp0_lt.ne' hp_top, refine (snorm'_le_snorm_ess_sup_mul_rpow_measure_univ hp_pos).trans (le_of_eq _), congr, exact one_div _, }, have hp_lt_top : p < ∞, from hpq.trans_lt (lt_top_iff_ne_top.mpr hq_top), have hp_pos : 0 < p.to_real, from ennreal.to_real_pos hp0_lt.ne' hp_lt_top.ne, rw [snorm_eq_snorm' hp0_lt.ne.symm hp_lt_top.ne, snorm_eq_snorm' hq0_lt.ne.symm hq_top], have hpq_real : p.to_real ≤ q.to_real, by rwa ennreal.to_real_le_to_real hp_lt_top.ne hq_top, exact snorm'_le_snorm'_mul_rpow_measure_univ hp_pos hpq_real hf, end lemma snorm'_le_snorm'_of_exponent_le {m : measurable_space α} {p q : ℝ} (hp0_lt : 0 < p) (hpq : p ≤ q) (μ : measure α) [is_probability_measure μ] {f : α → E} (hf : ae_strongly_measurable f μ) : snorm' f p μ ≤ snorm' f q μ := begin have h_le_μ := snorm'_le_snorm'_mul_rpow_measure_univ hp0_lt hpq hf, rwa [measure_univ, ennreal.one_rpow, mul_one] at h_le_μ, end lemma snorm'_le_snorm_ess_sup (hq_pos : 0 < q) {f : α → F} [is_probability_measure μ] : snorm' f q μ ≤ snorm_ess_sup f μ := le_trans (snorm'_le_snorm_ess_sup_mul_rpow_measure_univ hq_pos) (le_of_eq (by simp [measure_univ])) lemma snorm_le_snorm_of_exponent_le {p q : ℝ≥0∞} (hpq : p ≤ q) [is_probability_measure μ] {f : α → E} (hf : ae_strongly_measurable f μ) : snorm f p μ ≤ snorm f q μ := (snorm_le_snorm_mul_rpow_measure_univ hpq hf).trans (le_of_eq (by simp [measure_univ])) lemma snorm'_lt_top_of_snorm'_lt_top_of_exponent_le {p q : ℝ} [is_finite_measure μ] {f : α → E} (hf : ae_strongly_measurable f μ) (hfq_lt_top : snorm' f q μ < ∞) (hp_nonneg : 0 ≤ p) (hpq : p ≤ q) : snorm' f p μ < ∞ := begin cases le_or_lt p 0 with hp_nonpos hp_pos, { rw le_antisymm hp_nonpos hp_nonneg, simp, }, have hq_pos : 0 < q, from lt_of_lt_of_le hp_pos hpq, calc snorm' f p μ ≤ snorm' f q μ * (μ set.univ) ^ (1/p - 1/q) : snorm'_le_snorm'_mul_rpow_measure_univ hp_pos hpq hf ... < ∞ : begin rw ennreal.mul_lt_top_iff, refine or.inl ⟨hfq_lt_top, ennreal.rpow_lt_top_of_nonneg _ (measure_ne_top μ set.univ)⟩, rwa [le_sub, sub_zero, one_div, one_div, inv_le_inv hq_pos hp_pos], end end variables (μ) lemma pow_mul_meas_ge_le_snorm {f : α → E} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf : ae_strongly_measurable f μ) (ε : ℝ≥0∞) : (ε * μ {x | ε ≤ ∥f x∥₊ ^ p.to_real}) ^ (1 / p.to_real) ≤ snorm f p μ := begin rw snorm_eq_lintegral_rpow_nnnorm hp_ne_zero hp_ne_top, exact ennreal.rpow_le_rpow (mul_meas_ge_le_lintegral₀ (hf.ennnorm.pow_const _) ε) (one_div_nonneg.2 ennreal.to_real_nonneg), end lemma mul_meas_ge_le_pow_snorm {f : α → E} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf : ae_strongly_measurable f μ) (ε : ℝ≥0∞) : ε * μ {x | ε ≤ ∥f x∥₊ ^ p.to_real} ≤ snorm f p μ ^ p.to_real := begin have : 1 / p.to_real * p.to_real = 1, { refine one_div_mul_cancel _, rw [ne, ennreal.to_real_eq_zero_iff], exact not_or hp_ne_zero hp_ne_top }, rw [← ennreal.rpow_one (ε * μ {x | ε ≤ ∥f x∥₊ ^ p.to_real}), ← this, ennreal.rpow_mul], exact ennreal.rpow_le_rpow (pow_mul_meas_ge_le_snorm μ hp_ne_zero hp_ne_top hf ε) ennreal.to_real_nonneg, end /-- A version of Markov's inequality using Lp-norms. -/ lemma mul_meas_ge_le_pow_snorm' {f : α → E} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf : ae_strongly_measurable f μ) (ε : ℝ≥0∞) : ε ^ p.to_real * μ {x | ε ≤ ∥f x∥₊} ≤ snorm f p μ ^ p.to_real := begin convert mul_meas_ge_le_pow_snorm μ hp_ne_zero hp_ne_top hf (ε ^ p.to_real), ext x, rw ennreal.rpow_le_rpow_iff (ennreal.to_real_pos hp_ne_zero hp_ne_top), end lemma meas_ge_le_mul_pow_snorm {f : α → E} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf : ae_strongly_measurable f μ) {ε : ℝ≥0∞} (hε : ε ≠ 0) : μ {x | ε ≤ ∥f x∥₊} ≤ ε⁻¹ ^ p.to_real * snorm f p μ ^ p.to_real := begin by_cases ε = ∞, { simp [h] }, have hεpow : ε ^ p.to_real ≠ 0 := (ennreal.rpow_pos (pos_iff_ne_zero.2 hε) h).ne.symm, have hεpow' : ε ^ p.to_real ≠ ∞ := (ennreal.rpow_ne_top_of_nonneg ennreal.to_real_nonneg h), rw [ennreal.inv_rpow, ← ennreal.mul_le_mul_left hεpow hεpow', ← mul_assoc, ennreal.mul_inv_cancel hεpow hεpow', one_mul], exact mul_meas_ge_le_pow_snorm' μ hp_ne_zero hp_ne_top hf ε, end variables {μ} lemma mem_ℒp.mem_ℒp_of_exponent_le {p q : ℝ≥0∞} [is_finite_measure μ] {f : α → E} (hfq : mem_ℒp f q μ) (hpq : p ≤ q) : mem_ℒp f p μ := begin cases hfq with hfq_m hfq_lt_top, by_cases hp0 : p = 0, { rwa [hp0, mem_ℒp_zero_iff_ae_strongly_measurable], }, rw ←ne.def at hp0, refine ⟨hfq_m, _⟩, by_cases hp_top : p = ∞, { have hq_top : q = ∞, by rwa [hp_top, top_le_iff] at hpq, rw [hp_top], rwa hq_top at hfq_lt_top, }, have hp_pos : 0 < p.to_real, from ennreal.to_real_pos hp0 hp_top, by_cases hq_top : q = ∞, { rw snorm_eq_snorm' hp0 hp_top, rw [hq_top, snorm_exponent_top] at hfq_lt_top, refine lt_of_le_of_lt (snorm'_le_snorm_ess_sup_mul_rpow_measure_univ hp_pos) _, refine ennreal.mul_lt_top hfq_lt_top.ne _, exact (ennreal.rpow_lt_top_of_nonneg (by simp [hp_pos.le]) (measure_ne_top μ set.univ)).ne }, have hq0 : q ≠ 0, { by_contra hq_eq_zero, have hp_eq_zero : p = 0, from le_antisymm (by rwa hq_eq_zero at hpq) (zero_le _), rw [hp_eq_zero, ennreal.zero_to_real] at hp_pos, exact (lt_irrefl _) hp_pos, }, have hpq_real : p.to_real ≤ q.to_real, by rwa ennreal.to_real_le_to_real hp_top hq_top, rw snorm_eq_snorm' hp0 hp_top, rw snorm_eq_snorm' hq0 hq_top at hfq_lt_top, exact snorm'_lt_top_of_snorm'_lt_top_of_exponent_le hfq_m hfq_lt_top (le_of_lt hp_pos) hpq_real, end section has_measurable_add -- variable [has_measurable_add₂ E] lemma snorm'_sum_le {ι} {f : ι → α → E} {s : finset ι} (hfs : ∀ i, i ∈ s → ae_strongly_measurable (f i) μ) (hq1 : 1 ≤ q) : snorm' (∑ i in s, f i) q μ ≤ ∑ i in s, snorm' (f i) q μ := finset.le_sum_of_subadditive_on_pred (λ (f : α → E), snorm' f q μ) (λ f, ae_strongly_measurable f μ) (snorm'_zero (zero_lt_one.trans_le hq1)) (λ f g hf hg, snorm'_add_le hf hg hq1) (λ f g hf hg, hf.add hg) _ hfs lemma snorm_sum_le {ι} {f : ι → α → E} {s : finset ι} (hfs : ∀ i, i ∈ s → ae_strongly_measurable (f i) μ) (hp1 : 1 ≤ p) : snorm (∑ i in s, f i) p μ ≤ ∑ i in s, snorm (f i) p μ := finset.le_sum_of_subadditive_on_pred (λ (f : α → E), snorm f p μ) (λ f, ae_strongly_measurable f μ) snorm_zero (λ f g hf hg, snorm_add_le hf hg hp1) (λ f g hf hg, hf.add hg) _ hfs lemma mem_ℒp.add {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : mem_ℒp (f + g) p μ := ⟨ae_strongly_measurable.add hf.1 hg.1, snorm_add_lt_top hf hg⟩ lemma mem_ℒp.sub {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : mem_ℒp (f - g) p μ := by { rw sub_eq_add_neg, exact hf.add hg.neg } lemma mem_ℒp_finset_sum {ι} (s : finset ι) {f : ι → α → E} (hf : ∀ i ∈ s, mem_ℒp (f i) p μ) : mem_ℒp (λ a, ∑ i in s, f i a) p μ := begin haveI : decidable_eq ι := classical.dec_eq _, revert hf, refine finset.induction_on s _ _, { simp only [zero_mem_ℒp', finset.sum_empty, implies_true_iff], }, { intros i s his ih hf, simp only [his, finset.sum_insert, not_false_iff], exact (hf i (s.mem_insert_self i)).add (ih (λ j hj, hf j (finset.mem_insert_of_mem hj))), }, end lemma mem_ℒp_finset_sum' {ι} (s : finset ι) {f : ι → α → E} (hf : ∀ i ∈ s, mem_ℒp (f i) p μ) : mem_ℒp (∑ i in s, f i) p μ := begin convert mem_ℒp_finset_sum s hf, ext x, simp, end end has_measurable_add end borel_space section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E] [normed_space 𝕜 F] lemma snorm'_const_smul {f : α → F} (c : 𝕜) (hq_pos : 0 < q) : snorm' (c • f) q μ = (∥c∥₊ : ℝ≥0∞) * snorm' f q μ := begin rw snorm', simp_rw [pi.smul_apply, nnnorm_smul, ennreal.coe_mul, ennreal.mul_rpow_of_nonneg _ _ hq_pos.le], suffices h_integral : ∫⁻ a, ↑(∥c∥₊) ^ q * ↑∥f a∥₊ ^ q ∂μ = (∥c∥₊ : ℝ≥0∞)^q * ∫⁻ a, ∥f a∥₊ ^ q ∂μ, { apply_fun (λ x, x ^ (1/q)) at h_integral, rw [h_integral, ennreal.mul_rpow_of_nonneg _ _ (by simp [hq_pos.le] : 0 ≤ 1 / q)], congr, simp_rw [←ennreal.rpow_mul, one_div, mul_inv_cancel hq_pos.ne.symm, ennreal.rpow_one], }, rw lintegral_const_mul', rw ennreal.coe_rpow_of_nonneg _ hq_pos.le, exact ennreal.coe_ne_top, end lemma snorm_ess_sup_const_smul {f : α → F} (c : 𝕜) : snorm_ess_sup (c • f) μ = (∥c∥₊ : ℝ≥0∞) * snorm_ess_sup f μ := by simp_rw [snorm_ess_sup, pi.smul_apply, nnnorm_smul, ennreal.coe_mul, ennreal.ess_sup_const_mul] lemma snorm_const_smul {f : α → F} (c : 𝕜) : snorm (c • f) p μ = (∥c∥₊ : ℝ≥0∞) * snorm f p μ := begin by_cases h0 : p = 0, { simp [h0], }, by_cases h_top : p = ∞, { simp [h_top, snorm_ess_sup_const_smul], }, repeat { rw snorm_eq_snorm' h0 h_top, }, rw ←ne.def at h0, exact snorm'_const_smul c (ennreal.to_real_pos h0 h_top), end lemma mem_ℒp.const_smul {f : α → E} (hf : mem_ℒp f p μ) (c : 𝕜) : mem_ℒp (c • f) p μ := ⟨ae_strongly_measurable.const_smul hf.1 c, (snorm_const_smul c).le.trans_lt (ennreal.mul_lt_top ennreal.coe_ne_top hf.2.ne)⟩ lemma mem_ℒp.const_mul {f : α → 𝕜} (hf : mem_ℒp f p μ) (c : 𝕜) : mem_ℒp (λ x, c * f x) p μ := hf.const_smul c lemma snorm'_smul_le_mul_snorm' {p q r : ℝ} {f : α → E} (hf : ae_strongly_measurable f μ) {φ : α → 𝕜} (hφ : ae_strongly_measurable φ μ) (hp0_lt : 0 < p) (hpq : p < q) (hpqr : 1/p = 1/q + 1/r) : snorm' (φ • f) p μ ≤ snorm' φ q μ * snorm' f r μ := begin simp_rw [snorm', pi.smul_apply', nnnorm_smul, ennreal.coe_mul], exact ennreal.lintegral_Lp_mul_le_Lq_mul_Lr hp0_lt hpq hpqr μ hφ.ennnorm hf.ennnorm, end end normed_space section monotonicity lemma snorm_le_mul_snorm_aux_of_nonneg {f : α → F} {g : α → G} {c : ℝ} (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ c * ∥g x∥) (hc : 0 ≤ c) (p : ℝ≥0∞) : snorm f p μ ≤ (ennreal.of_real c) * snorm g p μ := begin lift c to ℝ≥0 using hc, rw [ennreal.of_real_coe_nnreal, ← c.nnnorm_eq, ← snorm_norm g, ← snorm_const_smul (c : ℝ)], swap, apply_instance, refine snorm_mono_ae _, simpa end lemma snorm_le_mul_snorm_aux_of_neg {f : α → F} {g : α → G} {c : ℝ} (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ c * ∥g x∥) (hc : c < 0) (p : ℝ≥0∞) : snorm f p μ = 0 ∧ snorm g p μ = 0 := begin suffices : f =ᵐ[μ] 0 ∧ g =ᵐ[μ] 0, by simp [snorm_congr_ae this.1, snorm_congr_ae this.2], refine ⟨h.mono $ λ x hx, _, h.mono $ λ x hx, _⟩, { refine norm_le_zero_iff.1 (hx.trans _), exact mul_nonpos_of_nonpos_of_nonneg hc.le (norm_nonneg _) }, { refine norm_le_zero_iff.1 (nonpos_of_mul_nonneg_right _ hc), exact (norm_nonneg _).trans hx } end lemma snorm_le_mul_snorm_of_ae_le_mul {f : α → F} {g : α → G} {c : ℝ} (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ c * ∥g x∥) (p : ℝ≥0∞) : snorm f p μ ≤ (ennreal.of_real c) * snorm g p μ := begin cases le_or_lt 0 c with hc hc, { exact snorm_le_mul_snorm_aux_of_nonneg h hc p }, { simp [snorm_le_mul_snorm_aux_of_neg h hc p] } end lemma mem_ℒp.of_le_mul {f : α → E} {g : α → F} {c : ℝ} (hg : mem_ℒp g p μ) (hf : ae_strongly_measurable f μ) (hfg : ∀ᵐ x ∂μ, ∥f x∥ ≤ c * ∥g x∥) : mem_ℒp f p μ := begin simp only [mem_ℒp, hf, true_and], apply lt_of_le_of_lt (snorm_le_mul_snorm_of_ae_le_mul hfg p), simp [lt_top_iff_ne_top, hg.snorm_ne_top], end end monotonicity lemma snorm_indicator_ge_of_bdd_below (hp : p ≠ 0) (hp' : p ≠ ∞) {f : α → F} (C : ℝ≥0) {s : set α} (hs : measurable_set s) (hf : ∀ᵐ x ∂μ, x ∈ s → C ≤ ∥s.indicator f x∥₊) : C • μ s ^ (1 / p.to_real) ≤ snorm (s.indicator f) p μ := begin rw [ennreal.smul_def, smul_eq_mul, snorm_eq_lintegral_rpow_nnnorm hp hp', ennreal.le_rpow_one_div_iff (ennreal.to_real_pos hp hp'), ennreal.mul_rpow_of_nonneg _ _ ennreal.to_real_nonneg, ← ennreal.rpow_mul, one_div_mul_cancel (ennreal.to_real_pos hp hp').ne.symm, ennreal.rpow_one, ← set_lintegral_const, ← lintegral_indicator _ hs], refine lintegral_mono_ae _, filter_upwards [hf] with x hx, rw nnnorm_indicator_eq_indicator_nnnorm, by_cases hxs : x ∈ s, { simp only [set.indicator_of_mem hxs] at ⊢ hx, exact ennreal.rpow_le_rpow (ennreal.coe_le_coe.2 (hx hxs)) ennreal.to_real_nonneg }, { simp [set.indicator_of_not_mem hxs] }, end section is_R_or_C variables {𝕜 : Type*} [is_R_or_C 𝕜] {f : α → 𝕜} lemma mem_ℒp.re (hf : mem_ℒp f p μ) : mem_ℒp (λ x, is_R_or_C.re (f x)) p μ := begin have : ∀ x, ∥is_R_or_C.re (f x)∥ ≤ 1 * ∥f x∥, by { intro x, rw one_mul, exact is_R_or_C.norm_re_le_norm (f x), }, exact hf.of_le_mul hf.1.re (eventually_of_forall this), end lemma mem_ℒp.im (hf : mem_ℒp f p μ) : mem_ℒp (λ x, is_R_or_C.im (f x)) p μ := begin have : ∀ x, ∥is_R_or_C.im (f x)∥ ≤ 1 * ∥f x∥, by { intro x, rw one_mul, exact is_R_or_C.norm_im_le_norm (f x), }, exact hf.of_le_mul hf.1.im (eventually_of_forall this), end end is_R_or_C section inner_product variables {E' 𝕜 : Type*} [is_R_or_C 𝕜] [inner_product_space 𝕜 E'] local notation `⟪`x`, `y`⟫` := @inner 𝕜 E' _ x y lemma mem_ℒp.const_inner (c : E') {f : α → E'} (hf : mem_ℒp f p μ) : mem_ℒp (λ a, ⟪c, f a⟫) p μ := hf.of_le_mul (ae_strongly_measurable.inner ae_strongly_measurable_const hf.1) (eventually_of_forall (λ x, norm_inner_le_norm _ _)) lemma mem_ℒp.inner_const {f : α → E'} (hf : mem_ℒp f p μ) (c : E') : mem_ℒp (λ a, ⟪f a, c⟫) p μ := hf.of_le_mul (ae_strongly_measurable.inner hf.1 ae_strongly_measurable_const) (eventually_of_forall (λ x, by { rw mul_comm, exact norm_inner_le_norm _ _, })) end inner_product section liminf variables [measurable_space E] [opens_measurable_space E] {R : ℝ≥0} lemma ae_bdd_liminf_at_top_rpow_of_snorm_bdd {p : ℝ≥0∞} {f : ℕ → α → E} (hfmeas : ∀ n, measurable (f n)) (hbdd : ∀ n, snorm (f n) p μ ≤ R) : ∀ᵐ x ∂μ, liminf at_top (λ n, (∥f n x∥₊ ^ p.to_real : ℝ≥0∞)) < ∞ := begin by_cases hp0 : p.to_real = 0, { simp only [hp0, ennreal.rpow_zero], refine eventually_of_forall (λ x, _), rw liminf_const (1 : ℝ≥0∞), exacts [ennreal.one_lt_top, at_top_ne_bot] }, have hp : p ≠ 0 := λ h, by simpa [h] using hp0, have hp' : p ≠ ∞ := λ h, by simpa [h] using hp0, refine ae_lt_top (measurable_liminf (λ n, (hfmeas n).nnnorm.coe_nnreal_ennreal.pow_const p.to_real)) (lt_of_le_of_lt (lintegral_liminf_le (λ n, (hfmeas n).nnnorm.coe_nnreal_ennreal.pow_const p.to_real)) (lt_of_le_of_lt _ (ennreal.rpow_lt_top_of_nonneg ennreal.to_real_nonneg ennreal.coe_ne_top : ↑R ^ p.to_real < ∞))).ne, simp_rw snorm_eq_lintegral_rpow_nnnorm hp hp' at hbdd, simp_rw [liminf_eq, eventually_at_top], exact Sup_le (λ b ⟨a, ha⟩, (ha a le_rfl).trans ((ennreal.rpow_one_div_le_iff (ennreal.to_real_pos hp hp')).1 (hbdd _))), end lemma ae_bdd_liminf_at_top_of_snorm_bdd {p : ℝ≥0∞} (hp : p ≠ 0) {f : ℕ → α → E} (hfmeas : ∀ n, measurable (f n)) (hbdd : ∀ n, snorm (f n) p μ ≤ R) : ∀ᵐ x ∂μ, liminf at_top (λ n, (∥f n x∥₊ : ℝ≥0∞)) < ∞ := begin by_cases hp' : p = ∞, { subst hp', simp_rw snorm_exponent_top at hbdd, have : ∀ n, ∀ᵐ x ∂μ, (∥f n x∥₊ : ℝ≥0∞) < R + 1 := λ n, ae_lt_of_ess_sup_lt (lt_of_le_of_lt (hbdd n) $ ennreal.lt_add_right ennreal.coe_ne_top one_ne_zero), rw ← ae_all_iff at this, filter_upwards [this] with x hx using lt_of_le_of_lt (liminf_le_of_frequently_le' $ frequently_of_forall $ λ n, (hx n).le) (ennreal.add_lt_top.2 ⟨ennreal.coe_lt_top, ennreal.one_lt_top⟩) }, filter_upwards [ae_bdd_liminf_at_top_rpow_of_snorm_bdd hfmeas hbdd] with x hx, have hppos : 0 < p.to_real := ennreal.to_real_pos hp hp', have : liminf at_top (λ n, (∥f n x∥₊ ^ p.to_real : ℝ≥0∞)) = liminf at_top (λ n, (∥f n x∥₊ : ℝ≥0∞)) ^ p.to_real, { change liminf at_top (λ n, ennreal.order_iso_rpow p.to_real hppos (∥f n x∥₊ : ℝ≥0∞)) = ennreal.order_iso_rpow p.to_real hppos (liminf at_top (λ n, (∥f n x∥₊ : ℝ≥0∞))), refine (order_iso.liminf_apply (ennreal.order_iso_rpow p.to_real _) _ _ _ _).symm; is_bounded_default }, rw this at hx, rw [← ennreal.rpow_one (liminf at_top (λ n, ∥f n x∥₊)), ← mul_inv_cancel hppos.ne.symm, ennreal.rpow_mul], exact ennreal.rpow_lt_top_of_nonneg (inv_nonneg.2 hppos.le) hx.ne, end end liminf end ℒp /-! ### Lp space The space of equivalence classes of measurable functions for which `snorm f p μ < ∞`. -/ @[simp] lemma snorm_ae_eq_fun {α E : Type*} [measurable_space α] {μ : measure α} [normed_add_comm_group E] {p : ℝ≥0∞} {f : α → E} (hf : ae_strongly_measurable f μ) : snorm (ae_eq_fun.mk f hf) p μ = snorm f p μ := snorm_congr_ae (ae_eq_fun.coe_fn_mk _ _) lemma mem_ℒp.snorm_mk_lt_top {α E : Type*} [measurable_space α] {μ : measure α} [normed_add_comm_group E] {p : ℝ≥0∞} {f : α → E} (hfp : mem_ℒp f p μ) : snorm (ae_eq_fun.mk f hfp.1) p μ < ∞ := by simp [hfp.2] /-- Lp space -/ def Lp {α} (E : Type*) {m : measurable_space α} [normed_add_comm_group E] (p : ℝ≥0∞) (μ : measure α . volume_tac) : add_subgroup (α →ₘ[μ] E) := { carrier := {f | snorm f p μ < ∞}, zero_mem' := by simp [snorm_congr_ae ae_eq_fun.coe_fn_zero, snorm_zero], add_mem' := λ f g hf hg, by simp [snorm_congr_ae (ae_eq_fun.coe_fn_add _ _), snorm_add_lt_top ⟨f.ae_strongly_measurable, hf⟩ ⟨g.ae_strongly_measurable, hg⟩], neg_mem' := λ f hf, by rwa [set.mem_set_of_eq, snorm_congr_ae (ae_eq_fun.coe_fn_neg _), snorm_neg] } localized "notation α ` →₁[`:25 μ `] ` E := measure_theory.Lp E 1 μ" in measure_theory localized "notation α ` →₂[`:25 μ `] ` E := measure_theory.Lp E 2 μ" in measure_theory namespace mem_ℒp /-- make an element of Lp from a function verifying `mem_ℒp` -/ def to_Lp (f : α → E) (h_mem_ℒp : mem_ℒp f p μ) : Lp E p μ := ⟨ae_eq_fun.mk f h_mem_ℒp.1, h_mem_ℒp.snorm_mk_lt_top⟩ lemma coe_fn_to_Lp {f : α → E} (hf : mem_ℒp f p μ) : hf.to_Lp f =ᵐ[μ] f := ae_eq_fun.coe_fn_mk _ _ lemma to_Lp_congr {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) (hfg : f =ᵐ[μ] g) : hf.to_Lp f = hg.to_Lp g := by simp [to_Lp, hfg] @[simp] lemma to_Lp_eq_to_Lp_iff {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : hf.to_Lp f = hg.to_Lp g ↔ f =ᵐ[μ] g := by simp [to_Lp] @[simp] lemma to_Lp_zero (h : mem_ℒp (0 : α → E) p μ) : h.to_Lp 0 = 0 := rfl lemma to_Lp_add {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : (hf.add hg).to_Lp (f + g) = hf.to_Lp f + hg.to_Lp g := rfl lemma to_Lp_neg {f : α → E} (hf : mem_ℒp f p μ) : hf.neg.to_Lp (-f) = - hf.to_Lp f := rfl lemma to_Lp_sub {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : (hf.sub hg).to_Lp (f - g) = hf.to_Lp f - hg.to_Lp g := rfl end mem_ℒp namespace Lp instance : has_coe_to_fun (Lp E p μ) (λ _, α → E) := ⟨λ f, ((f : α →ₘ[μ] E) : α → E)⟩ @[ext] lemma ext {f g : Lp E p μ} (h : f =ᵐ[μ] g) : f = g := begin cases f, cases g, simp only [subtype.mk_eq_mk], exact ae_eq_fun.ext h end lemma ext_iff {f g : Lp E p μ} : f = g ↔ f =ᵐ[μ] g := ⟨λ h, by rw h, λ h, ext h⟩ lemma mem_Lp_iff_snorm_lt_top {f : α →ₘ[μ] E} : f ∈ Lp E p μ ↔ snorm f p μ < ∞ := iff.refl _ lemma mem_Lp_iff_mem_ℒp {f : α →ₘ[μ] E} : f ∈ Lp E p μ ↔ mem_ℒp f p μ := by simp [mem_Lp_iff_snorm_lt_top, mem_ℒp, f.strongly_measurable.ae_strongly_measurable] protected lemma antitone [is_finite_measure μ] {p q : ℝ≥0∞} (hpq : p ≤ q) : Lp E q μ ≤ Lp E p μ := λ f hf, (mem_ℒp.mem_ℒp_of_exponent_le ⟨f.ae_strongly_measurable, hf⟩ hpq).2 @[simp] lemma coe_fn_mk {f : α →ₘ[μ] E} (hf : snorm f p μ < ∞) : ((⟨f, hf⟩ : Lp E p μ) : α → E) = f := rfl @[simp] lemma coe_mk {f : α →ₘ[μ] E} (hf : snorm f p μ < ∞) : ((⟨f, hf⟩ : Lp E p μ) : α →ₘ[μ] E) = f := rfl @[simp] lemma to_Lp_coe_fn (f : Lp E p μ) (hf : mem_ℒp f p μ) : hf.to_Lp f = f := by { cases f, simp [mem_ℒp.to_Lp] } lemma snorm_lt_top (f : Lp E p μ) : snorm f p μ < ∞ := f.prop lemma snorm_ne_top (f : Lp E p μ) : snorm f p μ ≠ ∞ := (snorm_lt_top f).ne @[measurability] protected lemma strongly_measurable (f : Lp E p μ) : strongly_measurable f := f.val.strongly_measurable @[measurability] protected lemma ae_strongly_measurable (f : Lp E p μ) : ae_strongly_measurable f μ := f.val.ae_strongly_measurable protected lemma mem_ℒp (f : Lp E p μ) : mem_ℒp f p μ := ⟨Lp.ae_strongly_measurable f, f.prop⟩ variables (E p μ) lemma coe_fn_zero : ⇑(0 : Lp E p μ) =ᵐ[μ] 0 := ae_eq_fun.coe_fn_zero variables {E p μ} lemma coe_fn_neg (f : Lp E p μ) : ⇑(-f) =ᵐ[μ] -f := ae_eq_fun.coe_fn_neg _ lemma coe_fn_add (f g : Lp E p μ) : ⇑(f + g) =ᵐ[μ] f + g := ae_eq_fun.coe_fn_add _ _ lemma coe_fn_sub (f g : Lp E p μ) : ⇑(f - g) =ᵐ[μ] f - g := ae_eq_fun.coe_fn_sub _ _ lemma mem_Lp_const (α) {m : measurable_space α} (μ : measure α) (c : E) [is_finite_measure μ] : @ae_eq_fun.const α _ _ μ _ c ∈ Lp E p μ := (mem_ℒp_const c).snorm_mk_lt_top instance : has_norm (Lp E p μ) := { norm := λ f, ennreal.to_real (snorm f p μ) } instance : has_dist (Lp E p μ) := { dist := λ f g, ∥f - g∥} instance : has_edist (Lp E p μ) := { edist := λ f g, snorm (f - g) p μ } lemma norm_def (f : Lp E p μ) : ∥f∥ = ennreal.to_real (snorm f p μ) := rfl @[simp] lemma norm_to_Lp (f : α → E) (hf : mem_ℒp f p μ) : ∥hf.to_Lp f∥ = ennreal.to_real (snorm f p μ) := by rw [norm_def, snorm_congr_ae (mem_ℒp.coe_fn_to_Lp hf)] lemma dist_def (f g : Lp E p μ) : dist f g = (snorm (f - g) p μ).to_real := begin simp_rw [dist, norm_def], congr' 1, apply snorm_congr_ae (coe_fn_sub _ _), end lemma edist_def (f g : Lp E p μ) : edist f g = snorm (f - g) p μ := rfl @[simp] lemma edist_to_Lp_to_Lp (f g : α → E) (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : edist (hf.to_Lp f) (hg.to_Lp g) = snorm (f - g) p μ := by { rw edist_def, exact snorm_congr_ae (hf.coe_fn_to_Lp.sub hg.coe_fn_to_Lp) } @[simp] lemma edist_to_Lp_zero (f : α → E) (hf : mem_ℒp f p μ) : edist (hf.to_Lp f) 0 = snorm f p μ := by { convert edist_to_Lp_to_Lp f 0 hf zero_mem_ℒp, simp } @[simp] lemma norm_zero : ∥(0 : Lp E p μ)∥ = 0 := begin change (snorm ⇑(0 : α →ₘ[μ] E) p μ).to_real = 0, simp [snorm_congr_ae ae_eq_fun.coe_fn_zero, snorm_zero] end lemma norm_eq_zero_iff {f : Lp E p μ} (hp : 0 < p) : ∥f∥ = 0 ↔ f = 0 := begin refine ⟨λ hf, _, λ hf, by simp [hf]⟩, rw [norm_def, ennreal.to_real_eq_zero_iff] at hf, cases hf, { rw snorm_eq_zero_iff (Lp.ae_strongly_measurable f) hp.ne.symm at hf, exact subtype.eq (ae_eq_fun.ext (hf.trans ae_eq_fun.coe_fn_zero.symm)), }, { exact absurd hf (snorm_ne_top f), }, end lemma eq_zero_iff_ae_eq_zero {f : Lp E p μ} : f = 0 ↔ f =ᵐ[μ] 0 := begin split, { assume h, rw h, exact ae_eq_fun.coe_fn_const _ _ }, { assume h, ext1, filter_upwards [h, ae_eq_fun.coe_fn_const α (0 : E)] with _ ha h'a, rw ha, exact h'a.symm, }, end @[simp] lemma norm_neg {f : Lp E p μ} : ∥-f∥ = ∥f∥ := by rw [norm_def, norm_def, snorm_congr_ae (coe_fn_neg _), snorm_neg] lemma norm_le_mul_norm_of_ae_le_mul {c : ℝ} {f : Lp E p μ} {g : Lp F p μ} (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ c * ∥g x∥) : ∥f∥ ≤ c * ∥g∥ := begin by_cases pzero : p = 0, { simp [pzero, norm_def] }, cases le_or_lt 0 c with hc hc, { have := snorm_le_mul_snorm_aux_of_nonneg h hc p, rw [← ennreal.to_real_le_to_real, ennreal.to_real_mul, ennreal.to_real_of_real hc] at this, { exact this }, { exact (Lp.mem_ℒp _).snorm_ne_top }, { simp [(Lp.mem_ℒp _).snorm_ne_top] } }, { have := snorm_le_mul_snorm_aux_of_neg h hc p, simp only [snorm_eq_zero_iff (Lp.ae_strongly_measurable _) pzero, ← eq_zero_iff_ae_eq_zero] at this, simp [this] } end lemma norm_le_norm_of_ae_le {f : Lp E p μ} {g : Lp F p μ} (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ ∥g x∥) : ∥f∥ ≤ ∥g∥ := begin rw [norm_def, norm_def, ennreal.to_real_le_to_real (snorm_ne_top _) (snorm_ne_top _)], exact snorm_mono_ae h end lemma mem_Lp_of_ae_le_mul {c : ℝ} {f : α →ₘ[μ] E} {g : Lp F p μ} (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ c * ∥g x∥) : f ∈ Lp E p μ := mem_Lp_iff_mem_ℒp.2 $ mem_ℒp.of_le_mul (Lp.mem_ℒp g) f.ae_strongly_measurable h lemma mem_Lp_of_ae_le {f : α →ₘ[μ] E} {g : Lp F p μ} (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ ∥g x∥) : f ∈ Lp E p μ := mem_Lp_iff_mem_ℒp.2 $ mem_ℒp.of_le (Lp.mem_ℒp g) f.ae_strongly_measurable h lemma mem_Lp_of_ae_bound [is_finite_measure μ] {f : α →ₘ[μ] E} (C : ℝ) (hfC : ∀ᵐ x ∂μ, ∥f x∥ ≤ C) : f ∈ Lp E p μ := mem_Lp_iff_mem_ℒp.2 $ mem_ℒp.of_bound f.ae_strongly_measurable _ hfC lemma norm_le_of_ae_bound [is_finite_measure μ] {f : Lp E p μ} {C : ℝ} (hC : 0 ≤ C) (hfC : ∀ᵐ x ∂μ, ∥f x∥ ≤ C) : ∥f∥ ≤ (measure_univ_nnreal μ) ^ (p.to_real)⁻¹ * C := begin by_cases hμ : μ = 0, { by_cases hp : p.to_real⁻¹ = 0, { simpa [hp, hμ, norm_def] using hC }, { simp [hμ, norm_def, real.zero_rpow hp] } }, let A : ℝ≥0 := (measure_univ_nnreal μ) ^ (p.to_real)⁻¹ * ⟨C, hC⟩, suffices : snorm f p μ ≤ A, { exact ennreal.to_real_le_coe_of_le_coe this }, convert snorm_le_of_ae_bound hfC, rw [← coe_measure_univ_nnreal μ, ennreal.coe_rpow_of_ne_zero (measure_univ_nnreal_pos hμ).ne', ennreal.coe_mul], congr, rw max_eq_left hC end instance [hp : fact (1 ≤ p)] : normed_add_comm_group (Lp E p μ) := { edist := edist, edist_dist := λ f g, by rw [edist_def, dist_def, ←snorm_congr_ae (coe_fn_sub _ _), ennreal.of_real_to_real (snorm_ne_top (f - g))], .. normed_add_comm_group.of_core (Lp E p μ) { norm_eq_zero_iff := λ f, norm_eq_zero_iff (ennreal.zero_lt_one.trans_le hp.1), triangle := begin assume f g, simp only [norm_def], rw ← ennreal.to_real_add (snorm_ne_top f) (snorm_ne_top g), suffices h_snorm : snorm ⇑(f + g) p μ ≤ snorm ⇑f p μ + snorm ⇑g p μ, { rwa ennreal.to_real_le_to_real (snorm_ne_top (f + g)), exact ennreal.add_ne_top.mpr ⟨snorm_ne_top f, snorm_ne_top g⟩, }, rw [snorm_congr_ae (coe_fn_add _ _)], exact snorm_add_le (Lp.ae_strongly_measurable f) (Lp.ae_strongly_measurable g) hp.1, end, norm_neg := by simp } } -- check no diamond is created example [fact (1 ≤ p)] : pseudo_emetric_space.to_has_edist = (Lp.has_edist : has_edist (Lp E p μ)) := rfl section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E] lemma mem_Lp_const_smul (c : 𝕜) (f : Lp E p μ) : c • ↑f ∈ Lp E p μ := begin rw [mem_Lp_iff_snorm_lt_top, snorm_congr_ae (ae_eq_fun.coe_fn_smul _ _), snorm_const_smul, ennreal.mul_lt_top_iff], exact or.inl ⟨ennreal.coe_lt_top, f.prop⟩, end variables (E p μ 𝕜) /-- The `𝕜`-submodule of elements of `α →ₘ[μ] E` whose `Lp` norm is finite. This is `Lp E p μ`, with extra structure. -/ def Lp_submodule : submodule 𝕜 (α →ₘ[μ] E) := { smul_mem' := λ c f hf, by simpa using mem_Lp_const_smul c ⟨f, hf⟩, .. Lp E p μ } variables {E p μ 𝕜} lemma coe_Lp_submodule : (Lp_submodule E p μ 𝕜).to_add_subgroup = Lp E p μ := rfl instance : module 𝕜 (Lp E p μ) := { .. (Lp_submodule E p μ 𝕜).module } lemma coe_fn_smul (c : 𝕜) (f : Lp E p μ) : ⇑(c • f) =ᵐ[μ] c • f := ae_eq_fun.coe_fn_smul _ _ lemma norm_const_smul (c : 𝕜) (f : Lp E p μ) : ∥c • f∥ = ∥c∥ * ∥f∥ := by rw [norm_def, snorm_congr_ae (coe_fn_smul _ _), snorm_const_smul c, ennreal.to_real_mul, ennreal.coe_to_real, coe_nnnorm, norm_def] instance [fact (1 ≤ p)] : normed_space 𝕜 (Lp E p μ) := { norm_smul_le := λ _ _, by simp [norm_const_smul] } end normed_space end Lp namespace mem_ℒp variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E] lemma to_Lp_const_smul {f : α → E} (c : 𝕜) (hf : mem_ℒp f p μ) : (hf.const_smul c).to_Lp (c • f) = c • hf.to_Lp f := rfl end mem_ℒp /-! ### Indicator of a set as an element of Lᵖ For a set `s` with `(hs : measurable_set s)` and `(hμs : μ s < ∞)`, we build `indicator_const_Lp p hs hμs c`, the element of `Lp` corresponding to `s.indicator (λ x, c)`. -/ section indicator variables {s : set α} {hs : measurable_set s} {c : E} {f : α → E} {hf : ae_strongly_measurable f μ} lemma snorm_ess_sup_indicator_le (s : set α) (f : α → G) : snorm_ess_sup (s.indicator f) μ ≤ snorm_ess_sup f μ := begin refine ess_sup_mono_ae (eventually_of_forall (λ x, _)), rw [ennreal.coe_le_coe, nnnorm_indicator_eq_indicator_nnnorm], exact set.indicator_le_self s _ x, end lemma snorm_ess_sup_indicator_const_le (s : set α) (c : G) : snorm_ess_sup (s.indicator (λ x : α , c)) μ ≤ ∥c∥₊ := begin by_cases hμ0 : μ = 0, { rw [hμ0, snorm_ess_sup_measure_zero, ennreal.coe_nonneg], exact zero_le', }, { exact (snorm_ess_sup_indicator_le s (λ x, c)).trans (snorm_ess_sup_const c hμ0).le, }, end lemma snorm_ess_sup_indicator_const_eq (s : set α) (c : G) (hμs : μ s ≠ 0) : snorm_ess_sup (s.indicator (λ x : α , c)) μ = ∥c∥₊ := begin refine le_antisymm (snorm_ess_sup_indicator_const_le s c) _, by_contra' h, have h' := ae_iff.mp (ae_lt_of_ess_sup_lt h), push_neg at h', refine hμs (measure_mono_null (λ x hx_mem, _) h'), rw [set.mem_set_of_eq, set.indicator_of_mem hx_mem], exact le_rfl, end variables (hs) lemma snorm_indicator_le {E : Type*} [normed_add_comm_group E] (f : α → E) : snorm (s.indicator f) p μ ≤ snorm f p μ := begin refine snorm_mono_ae (eventually_of_forall (λ x, _)), suffices : ∥s.indicator f x∥₊ ≤ ∥f x∥₊, { exact nnreal.coe_mono this }, rw nnnorm_indicator_eq_indicator_nnnorm, exact s.indicator_le_self _ x, end variables {hs} lemma snorm_indicator_const {c : G} (hs : measurable_set s) (hp : p ≠ 0) (hp_top : p ≠ ∞) : snorm (s.indicator (λ x, c)) p μ = ∥c∥₊ * (μ s) ^ (1 / p.to_real) := begin have hp_pos : 0 < p.to_real, from ennreal.to_real_pos hp hp_top, rw snorm_eq_lintegral_rpow_nnnorm hp hp_top, simp_rw [nnnorm_indicator_eq_indicator_nnnorm, ennreal.coe_indicator], have h_indicator_pow : (λ a : α, s.indicator (λ (x : α), (∥c∥₊ : ℝ≥0∞)) a ^ p.to_real) = s.indicator (λ (x : α), ↑∥c∥₊ ^ p.to_real), { rw set.comp_indicator_const (∥c∥₊ : ℝ≥0∞) (λ x, x ^ p.to_real) _, simp [hp_pos], }, rw [h_indicator_pow, lintegral_indicator _ hs, set_lintegral_const, ennreal.mul_rpow_of_nonneg], { rw [← ennreal.rpow_mul, mul_one_div_cancel hp_pos.ne.symm, ennreal.rpow_one], }, { simp [hp_pos.le], }, end lemma snorm_indicator_const' {c : G} (hs : measurable_set s) (hμs : μ s ≠ 0) (hp : p ≠ 0) : snorm (s.indicator (λ _, c)) p μ = ∥c∥₊ * (μ s) ^ (1 / p.to_real) := begin by_cases hp_top : p = ∞, { simp [hp_top, snorm_ess_sup_indicator_const_eq s c hμs], }, { exact snorm_indicator_const hs hp hp_top, }, end lemma mem_ℒp.indicator (hs : measurable_set s) (hf : mem_ℒp f p μ) : mem_ℒp (s.indicator f) p μ := ⟨hf.ae_strongly_measurable.indicator hs, lt_of_le_of_lt (snorm_indicator_le f) hf.snorm_lt_top⟩ lemma snorm_ess_sup_indicator_eq_snorm_ess_sup_restrict {f : α → F} (hs : measurable_set s) : snorm_ess_sup (s.indicator f) μ = snorm_ess_sup f (μ.restrict s) := begin simp_rw [snorm_ess_sup, nnnorm_indicator_eq_indicator_nnnorm, ennreal.coe_indicator], by_cases hs_null : μ s = 0, { rw measure.restrict_zero_set hs_null, simp only [ess_sup_measure_zero, ennreal.ess_sup_eq_zero_iff, ennreal.bot_eq_zero], have hs_empty : s =ᵐ[μ] (∅ : set α), by { rw ae_eq_set, simpa using hs_null, }, refine (indicator_ae_eq_of_ae_eq_set hs_empty).trans _, rw set.indicator_empty, refl, }, rw ess_sup_indicator_eq_ess_sup_restrict (eventually_of_forall (λ x, _)) hs hs_null, rw pi.zero_apply, exact zero_le _, end lemma snorm_indicator_eq_snorm_restrict {f : α → F} (hs : measurable_set s) : snorm (s.indicator f) p μ = snorm f p (μ.restrict s) := begin by_cases hp_zero : p = 0, { simp only [hp_zero, snorm_exponent_zero], }, by_cases hp_top : p = ∞, { simp_rw [hp_top, snorm_exponent_top], exact snorm_ess_sup_indicator_eq_snorm_ess_sup_restrict hs, }, simp_rw snorm_eq_lintegral_rpow_nnnorm hp_zero hp_top, suffices : ∫⁻ x, ∥s.indicator f x∥₊ ^ p.to_real ∂μ = ∫⁻ x in s, ∥f x∥₊ ^ p.to_real ∂μ, by rw this, rw ← lintegral_indicator _ hs, congr, simp_rw [nnnorm_indicator_eq_indicator_nnnorm, ennreal.coe_indicator], have h_zero : (λ x, x ^ p.to_real) (0 : ℝ≥0∞) = 0, by simp [ennreal.to_real_pos hp_zero hp_top], exact (set.indicator_comp_of_zero h_zero).symm, end lemma mem_ℒp_indicator_iff_restrict (hs : measurable_set s) : mem_ℒp (s.indicator f) p μ ↔ mem_ℒp f p (μ.restrict s) := by simp [mem_ℒp, ae_strongly_measurable_indicator_iff hs, snorm_indicator_eq_snorm_restrict hs] lemma mem_ℒp_indicator_const (p : ℝ≥0∞) (hs : measurable_set s) (c : E) (hμsc : c = 0 ∨ μ s ≠ ∞) : mem_ℒp (s.indicator (λ _, c)) p μ := begin rw mem_ℒp_indicator_iff_restrict hs, by_cases hp_zero : p = 0, { rw hp_zero, exact mem_ℒp_zero_iff_ae_strongly_measurable.mpr ae_strongly_measurable_const, }, by_cases hp_top : p = ∞, { rw hp_top, exact mem_ℒp_top_of_bound ae_strongly_measurable_const (∥c∥) (eventually_of_forall (λ x, le_rfl)), }, rw [mem_ℒp_const_iff hp_zero hp_top, measure.restrict_apply_univ], cases hμsc, { exact or.inl hμsc, }, { exact or.inr hμsc.lt_top, }, end end indicator section indicator_const_Lp open set function variables {s : set α} {hs : measurable_set s} {hμs : μ s ≠ ∞} {c : E} /-- Indicator of a set as an element of `Lp`. -/ def indicator_const_Lp (p : ℝ≥0∞) (hs : measurable_set s) (hμs : μ s ≠ ∞) (c : E) : Lp E p μ := mem_ℒp.to_Lp (s.indicator (λ _, c)) (mem_ℒp_indicator_const p hs c (or.inr hμs)) lemma indicator_const_Lp_coe_fn : ⇑(indicator_const_Lp p hs hμs c) =ᵐ[μ] s.indicator (λ _, c) := mem_ℒp.coe_fn_to_Lp (mem_ℒp_indicator_const p hs c (or.inr hμs)) lemma indicator_const_Lp_coe_fn_mem : ∀ᵐ (x : α) ∂μ, x ∈ s → indicator_const_Lp p hs hμs c x = c := indicator_const_Lp_coe_fn.mono (λ x hx hxs, hx.trans (set.indicator_of_mem hxs _)) lemma indicator_const_Lp_coe_fn_nmem : ∀ᵐ (x : α) ∂μ, x ∉ s → indicator_const_Lp p hs hμs c x = 0 := indicator_const_Lp_coe_fn.mono (λ x hx hxs, hx.trans (set.indicator_of_not_mem hxs _)) lemma norm_indicator_const_Lp (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : ∥indicator_const_Lp p hs hμs c∥ = ∥c∥ * (μ s).to_real ^ (1 / p.to_real) := by rw [Lp.norm_def, snorm_congr_ae indicator_const_Lp_coe_fn, snorm_indicator_const hs hp_ne_zero hp_ne_top, ennreal.to_real_mul, ennreal.to_real_rpow, ennreal.coe_to_real, coe_nnnorm] lemma norm_indicator_const_Lp_top (hμs_ne_zero : μ s ≠ 0) : ∥indicator_const_Lp ∞ hs hμs c∥ = ∥c∥ := by rw [Lp.norm_def, snorm_congr_ae indicator_const_Lp_coe_fn, snorm_indicator_const' hs hμs_ne_zero ennreal.top_ne_zero, ennreal.top_to_real, div_zero, ennreal.rpow_zero, mul_one, ennreal.coe_to_real, coe_nnnorm] lemma norm_indicator_const_Lp' (hp_pos : p ≠ 0) (hμs_pos : μ s ≠ 0) : ∥indicator_const_Lp p hs hμs c∥ = ∥c∥ * (μ s).to_real ^ (1 / p.to_real) := begin by_cases hp_top : p = ∞, { rw [hp_top, ennreal.top_to_real, div_zero, real.rpow_zero, mul_one], exact norm_indicator_const_Lp_top hμs_pos, }, { exact norm_indicator_const_Lp hp_pos hp_top, }, end @[simp] lemma indicator_const_empty : indicator_const_Lp p measurable_set.empty (by simp : μ ∅ ≠ ∞) c = 0 := begin rw Lp.eq_zero_iff_ae_eq_zero, convert indicator_const_Lp_coe_fn, simp [set.indicator_empty'], end lemma mem_ℒp_add_of_disjoint {f g : α → E} (h : disjoint (support f) (support g)) (hf : strongly_measurable f) (hg : strongly_measurable g) : mem_ℒp (f + g) p μ ↔ mem_ℒp f p μ ∧ mem_ℒp g p μ := begin borelize E, refine ⟨λ hfg, ⟨_, _⟩, λ h, h.1.add h.2⟩, { rw ← indicator_add_eq_left h, exact hfg.indicator (measurable_set_support hf.measurable) }, { rw ← indicator_add_eq_right h, exact hfg.indicator (measurable_set_support hg.measurable) } end /-- The indicator of a disjoint union of two sets is the sum of the indicators of the sets. -/ lemma indicator_const_Lp_disjoint_union {s t : set α} (hs : measurable_set s) (ht : measurable_set t) (hμs : μ s ≠ ∞) (hμt : μ t ≠ ∞) (hst : s ∩ t = ∅) (c : E) : (indicator_const_Lp p (hs.union ht) ((measure_union_le s t).trans_lt (lt_top_iff_ne_top.mpr (ennreal.add_ne_top.mpr ⟨hμs, hμt⟩))).ne c) = indicator_const_Lp p hs hμs c + indicator_const_Lp p ht hμt c := begin ext1, refine indicator_const_Lp_coe_fn.trans (eventually_eq.trans _ (Lp.coe_fn_add _ _).symm), refine eventually_eq.trans _ (eventually_eq.add indicator_const_Lp_coe_fn.symm indicator_const_Lp_coe_fn.symm), rw set.indicator_union_of_disjoint (set.disjoint_iff_inter_eq_empty.mpr hst) _, end end indicator_const_Lp lemma mem_ℒp.norm_rpow_div {f : α → E} (hf : mem_ℒp f p μ) (q : ℝ≥0∞) : mem_ℒp (λ (x : α), ∥f x∥ ^ q.to_real) (p/q) μ := begin refine ⟨(hf.1.norm.ae_measurable.pow_const q.to_real).ae_strongly_measurable, _⟩, by_cases q_top : q = ∞, { simp [q_top] }, by_cases q_zero : q = 0, { simp [q_zero], by_cases p_zero : p = 0, { simp [p_zero] }, rw ennreal.div_zero p_zero, exact (mem_ℒp_top_const (1 : ℝ)).2 }, rw snorm_norm_rpow _ (ennreal.to_real_pos q_zero q_top), apply ennreal.rpow_lt_top_of_nonneg ennreal.to_real_nonneg, rw [ennreal.of_real_to_real q_top, div_eq_mul_inv, mul_assoc, ennreal.inv_mul_cancel q_zero q_top, mul_one], exact hf.2.ne end lemma mem_ℒp_norm_rpow_iff {q : ℝ≥0∞} {f : α → E} (hf : ae_strongly_measurable f μ) (q_zero : q ≠ 0) (q_top : q ≠ ∞) : mem_ℒp (λ (x : α), ∥f x∥ ^ q.to_real) (p/q) μ ↔ mem_ℒp f p μ := begin refine ⟨λ h, _, λ h, h.norm_rpow_div q⟩, apply (mem_ℒp_norm_iff hf).1, convert h.norm_rpow_div (q⁻¹), { ext x, rw [real.norm_eq_abs, real.abs_rpow_of_nonneg (norm_nonneg _), ← real.rpow_mul (abs_nonneg _), ennreal.to_real_inv, mul_inv_cancel, abs_of_nonneg (norm_nonneg _), real.rpow_one], simp [ennreal.to_real_eq_zero_iff, not_or_distrib, q_zero, q_top] }, { rw [div_eq_mul_inv, inv_inv, div_eq_mul_inv, mul_assoc, ennreal.inv_mul_cancel q_zero q_top, mul_one] } end lemma mem_ℒp.norm_rpow {f : α → E} (hf : mem_ℒp f p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : mem_ℒp (λ (x : α), ∥f x∥ ^ p.to_real) 1 μ := begin convert hf.norm_rpow_div p, rw [div_eq_mul_inv, ennreal.mul_inv_cancel hp_ne_zero hp_ne_top], end end measure_theory open measure_theory /-! ### Composition on `L^p` We show that Lipschitz functions vanishing at zero act by composition on `L^p`, and specialize this to the composition with continuous linear maps, and to the definition of the positive part of an `L^p` function. -/ section composition variables {g : E → F} {c : ℝ≥0} lemma lipschitz_with.comp_mem_ℒp {α E F} {K} [measurable_space α] {μ : measure α} [normed_add_comm_group E] [normed_add_comm_group F] {f : α → E} {g : E → F} (hg : lipschitz_with K g) (g0 : g 0 = 0) (hL : mem_ℒp f p μ) : mem_ℒp (g ∘ f) p μ := begin have : ∀ᵐ x ∂μ, ∥g (f x)∥ ≤ K * ∥f x∥, { apply filter.eventually_of_forall (λ x, _), rw [← dist_zero_right, ← dist_zero_right, ← g0], apply hg.dist_le_mul }, exact hL.of_le_mul (hg.continuous.comp_ae_strongly_measurable hL.1) this, end lemma measure_theory.mem_ℒp.of_comp_antilipschitz_with {α E F} {K'} [measurable_space α] {μ : measure α} [normed_add_comm_group E] [normed_add_comm_group F] {f : α → E} {g : E → F} (hL : mem_ℒp (g ∘ f) p μ) (hg : uniform_continuous g) (hg' : antilipschitz_with K' g) (g0 : g 0 = 0) : mem_ℒp f p μ := begin have A : ∀ᵐ x ∂μ, ∥f x∥ ≤ K' * ∥g (f x)∥, { apply filter.eventually_of_forall (λ x, _), rw [← dist_zero_right, ← dist_zero_right, ← g0], apply hg'.le_mul_dist }, have B : ae_strongly_measurable f μ := ((hg'.uniform_embedding hg).embedding.ae_strongly_measurable_comp_iff.1 hL.1), exact hL.of_le_mul B A, end namespace lipschitz_with lemma mem_ℒp_comp_iff_of_antilipschitz {α E F} {K K'} [measurable_space α] {μ : measure α} [normed_add_comm_group E] [normed_add_comm_group F] {f : α → E} {g : E → F} (hg : lipschitz_with K g) (hg' : antilipschitz_with K' g) (g0 : g 0 = 0) : mem_ℒp (g ∘ f) p μ ↔ mem_ℒp f p μ := ⟨λ h, h.of_comp_antilipschitz_with hg.uniform_continuous hg' g0, λ h, hg.comp_mem_ℒp g0 h⟩ /-- When `g` is a Lipschitz function sending `0` to `0` and `f` is in `Lp`, then `g ∘ f` is well defined as an element of `Lp`. -/ def comp_Lp (hg : lipschitz_with c g) (g0 : g 0 = 0) (f : Lp E p μ) : Lp F p μ := ⟨ae_eq_fun.comp g hg.continuous (f : α →ₘ[μ] E), begin suffices : ∀ᵐ x ∂μ, ∥ae_eq_fun.comp g hg.continuous (f : α →ₘ[μ] E) x∥ ≤ c * ∥f x∥, { exact Lp.mem_Lp_of_ae_le_mul this }, filter_upwards [ae_eq_fun.coe_fn_comp g hg.continuous (f : α →ₘ[μ] E)] with a ha, simp only [ha], rw [← dist_zero_right, ← dist_zero_right, ← g0], exact hg.dist_le_mul (f a) 0, end⟩ lemma coe_fn_comp_Lp (hg : lipschitz_with c g) (g0 : g 0 = 0) (f : Lp E p μ) : hg.comp_Lp g0 f =ᵐ[μ] g ∘ f := ae_eq_fun.coe_fn_comp _ _ _ @[simp] lemma comp_Lp_zero (hg : lipschitz_with c g) (g0 : g 0 = 0) : hg.comp_Lp g0 (0 : Lp E p μ) = 0 := begin rw Lp.eq_zero_iff_ae_eq_zero, apply (coe_fn_comp_Lp _ _ _).trans, filter_upwards [Lp.coe_fn_zero E p μ] with _ ha, simp [ha, g0], end lemma norm_comp_Lp_sub_le (hg : lipschitz_with c g) (g0 : g 0 = 0) (f f' : Lp E p μ) : ∥hg.comp_Lp g0 f - hg.comp_Lp g0 f'∥ ≤ c * ∥f - f'∥ := begin apply Lp.norm_le_mul_norm_of_ae_le_mul, filter_upwards [hg.coe_fn_comp_Lp g0 f, hg.coe_fn_comp_Lp g0 f', Lp.coe_fn_sub (hg.comp_Lp g0 f) (hg.comp_Lp g0 f'), Lp.coe_fn_sub f f'] with a ha1 ha2 ha3 ha4, simp [ha1, ha2, ha3, ha4, ← dist_eq_norm], exact hg.dist_le_mul (f a) (f' a) end lemma norm_comp_Lp_le (hg : lipschitz_with c g) (g0 : g 0 = 0) (f : Lp E p μ) : ∥hg.comp_Lp g0 f∥ ≤ c * ∥f∥ := by simpa using hg.norm_comp_Lp_sub_le g0 f 0 lemma lipschitz_with_comp_Lp [fact (1 ≤ p)] (hg : lipschitz_with c g) (g0 : g 0 = 0) : lipschitz_with c (hg.comp_Lp g0 : Lp E p μ → Lp F p μ) := lipschitz_with.of_dist_le_mul $ λ f g, by simp [dist_eq_norm, norm_comp_Lp_sub_le] lemma continuous_comp_Lp [fact (1 ≤ p)] (hg : lipschitz_with c g) (g0 : g 0 = 0) : continuous (hg.comp_Lp g0 : Lp E p μ → Lp F p μ) := (lipschitz_with_comp_Lp hg g0).continuous end lipschitz_with namespace continuous_linear_map variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] [normed_space 𝕜 E] [normed_space 𝕜 F] /-- Composing `f : Lp ` with `L : E →L[𝕜] F`. -/ def comp_Lp (L : E →L[𝕜] F) (f : Lp E p μ) : Lp F p μ := L.lipschitz.comp_Lp (map_zero L) f lemma coe_fn_comp_Lp (L : E →L[𝕜] F) (f : Lp E p μ) : ∀ᵐ a ∂μ, (L.comp_Lp f) a = L (f a) := lipschitz_with.coe_fn_comp_Lp _ _ _ lemma coe_fn_comp_Lp' (L : E →L[𝕜] F) (f : Lp E p μ) : L.comp_Lp f =ᵐ[μ] λ a, L (f a) := L.coe_fn_comp_Lp f lemma comp_mem_ℒp (L : E →L[𝕜] F) (f : Lp E p μ) : mem_ℒp (L ∘ f) p μ := (Lp.mem_ℒp (L.comp_Lp f)).ae_eq (L.coe_fn_comp_Lp' f) lemma comp_mem_ℒp' (L : E →L[𝕜] F) {f : α → E} (hf : mem_ℒp f p μ) : mem_ℒp (L ∘ f) p μ := (L.comp_mem_ℒp (hf.to_Lp f)).ae_eq (eventually_eq.fun_comp (hf.coe_fn_to_Lp) _) section is_R_or_C variables {K : Type*} [is_R_or_C K] lemma _root_.measure_theory.mem_ℒp.of_real {f : α → ℝ} (hf : mem_ℒp f p μ) : mem_ℒp (λ x, (f x : K)) p μ := (@is_R_or_C.of_real_clm K _).comp_mem_ℒp' hf lemma _root_.measure_theory.mem_ℒp_re_im_iff {f : α → K} : mem_ℒp (λ x, is_R_or_C.re (f x)) p μ ∧ mem_ℒp (λ x, is_R_or_C.im (f x)) p μ ↔ mem_ℒp f p μ := begin refine ⟨_, λ hf, ⟨hf.re, hf.im⟩⟩, rintro ⟨hre, him⟩, convert hre.of_real.add (him.of_real.const_mul is_R_or_C.I), { ext1 x, rw [pi.add_apply, mul_comm, is_R_or_C.re_add_im] }, all_goals { apply_instance } end end is_R_or_C lemma add_comp_Lp (L L' : E →L[𝕜] F) (f : Lp E p μ) : (L + L').comp_Lp f = L.comp_Lp f + L'.comp_Lp f := begin ext1, refine (coe_fn_comp_Lp' (L + L') f).trans _, refine eventually_eq.trans _ (Lp.coe_fn_add _ _).symm, refine eventually_eq.trans _ (eventually_eq.add (L.coe_fn_comp_Lp' f).symm (L'.coe_fn_comp_Lp' f).symm), refine eventually_of_forall (λ x, _), refl, end lemma smul_comp_Lp {𝕜'} [normed_field 𝕜'] [normed_space 𝕜' F] [smul_comm_class 𝕜 𝕜' F] (c : 𝕜') (L : E →L[𝕜] F) (f : Lp E p μ) : (c • L).comp_Lp f = c • L.comp_Lp f := begin ext1, refine (coe_fn_comp_Lp' (c • L) f).trans _, refine eventually_eq.trans _ (Lp.coe_fn_smul _ _).symm, refine (L.coe_fn_comp_Lp' f).mono (λ x hx, _), rw [pi.smul_apply, hx], refl, end lemma norm_comp_Lp_le (L : E →L[𝕜] F) (f : Lp E p μ) : ∥L.comp_Lp f∥ ≤ ∥L∥ * ∥f∥ := lipschitz_with.norm_comp_Lp_le _ _ _ variables (μ p) /-- Composing `f : Lp E p μ` with `L : E →L[𝕜] F`, seen as a `𝕜`-linear map on `Lp E p μ`. -/ def comp_Lpₗ (L : E →L[𝕜] F) : (Lp E p μ) →ₗ[𝕜] (Lp F p μ) := { to_fun := λ f, L.comp_Lp f, map_add' := begin intros f g, ext1, filter_upwards [Lp.coe_fn_add f g, coe_fn_comp_Lp L (f + g), coe_fn_comp_Lp L f, coe_fn_comp_Lp L g, Lp.coe_fn_add (L.comp_Lp f) (L.comp_Lp g)], assume a ha1 ha2 ha3 ha4 ha5, simp only [ha1, ha2, ha3, ha4, ha5, map_add, pi.add_apply], end, map_smul' := begin intros c f, dsimp, ext1, filter_upwards [Lp.coe_fn_smul c f, coe_fn_comp_Lp L (c • f), Lp.coe_fn_smul c (L.comp_Lp f), coe_fn_comp_Lp L f] with _ ha1 ha2 ha3 ha4, simp only [ha1, ha2, ha3, ha4, smul_hom_class.map_smul, pi.smul_apply], end } /-- Composing `f : Lp E p μ` with `L : E →L[𝕜] F`, seen as a continuous `𝕜`-linear map on `Lp E p μ`. See also the similar * `linear_map.comp_left` for functions, * `continuous_linear_map.comp_left_continuous` for continuous functions, * `continuous_linear_map.comp_left_continuous_bounded` for bounded continuous functions, * `continuous_linear_map.comp_left_continuous_compact` for continuous functions on compact spaces. -/ def comp_LpL [fact (1 ≤ p)] (L : E →L[𝕜] F) : (Lp E p μ) →L[𝕜] (Lp F p μ) := linear_map.mk_continuous (L.comp_Lpₗ p μ) ∥L∥ L.norm_comp_Lp_le variables {μ p} lemma coe_fn_comp_LpL [fact (1 ≤ p)] (L : E →L[𝕜] F) (f : Lp E p μ) : L.comp_LpL p μ f =ᵐ[μ] λ a, L (f a) := L.coe_fn_comp_Lp f lemma add_comp_LpL [fact (1 ≤ p)] (L L' : E →L[𝕜] F) : (L + L').comp_LpL p μ = L.comp_LpL p μ + L'.comp_LpL p μ := by { ext1 f, exact add_comp_Lp L L' f } lemma smul_comp_LpL [fact (1 ≤ p)] (c : 𝕜) (L : E →L[𝕜] F) : (c • L).comp_LpL p μ = c • (L.comp_LpL p μ) := by { ext1 f, exact smul_comp_Lp c L f } /-- TODO: written in an "apply" way because of a missing `has_smul` instance. -/ lemma smul_comp_LpL_apply [fact (1 ≤ p)] {𝕜'} [normed_field 𝕜'] [normed_space 𝕜' F] [smul_comm_class 𝕜 𝕜' F] (c : 𝕜') (L : E →L[𝕜] F) (f : Lp E p μ) : (c • L).comp_LpL p μ f = c • (L.comp_LpL p μ f) := smul_comp_Lp c L f lemma norm_compLpL_le [fact (1 ≤ p)] (L : E →L[𝕜] F) : ∥L.comp_LpL p μ∥ ≤ ∥L∥ := linear_map.mk_continuous_norm_le _ (norm_nonneg _) _ end continuous_linear_map namespace measure_theory lemma indicator_const_Lp_eq_to_span_singleton_comp_Lp {s : set α} [normed_space ℝ F] (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : F) : indicator_const_Lp 2 hs hμs x = (continuous_linear_map.to_span_singleton ℝ x).comp_Lp (indicator_const_Lp 2 hs hμs (1 : ℝ)) := begin ext1, refine indicator_const_Lp_coe_fn.trans _, have h_comp_Lp := (continuous_linear_map.to_span_singleton ℝ x).coe_fn_comp_Lp (indicator_const_Lp 2 hs hμs (1 : ℝ)), rw ← eventually_eq at h_comp_Lp, refine eventually_eq.trans _ h_comp_Lp.symm, refine (@indicator_const_Lp_coe_fn _ _ _ 2 μ _ s hs hμs (1 : ℝ)).mono (λ y hy, _), dsimp only, rw hy, simp_rw [continuous_linear_map.to_span_singleton_apply], by_cases hy_mem : y ∈ s; simp [hy_mem, continuous_linear_map.lsmul_apply], end namespace Lp section pos_part lemma lipschitz_with_pos_part : lipschitz_with 1 (λ (x : ℝ), max x 0) := lipschitz_with.of_dist_le_mul $ λ x y, by simp [real.dist_eq, abs_max_sub_max_le_abs] lemma _root_.measure_theory.mem_ℒp.pos_part {f : α → ℝ} (hf : mem_ℒp f p μ) : mem_ℒp (λ x, max (f x) 0) p μ := lipschitz_with_pos_part.comp_mem_ℒp (max_eq_right le_rfl) hf lemma _root_.measure_theory.mem_ℒp.neg_part {f : α → ℝ} (hf : mem_ℒp f p μ) : mem_ℒp (λ x, max (-f x) 0) p μ := lipschitz_with_pos_part.comp_mem_ℒp (max_eq_right le_rfl) hf.neg /-- Positive part of a function in `L^p`. -/ def pos_part (f : Lp ℝ p μ) : Lp ℝ p μ := lipschitz_with_pos_part.comp_Lp (max_eq_right le_rfl) f /-- Negative part of a function in `L^p`. -/ def neg_part (f : Lp ℝ p μ) : Lp ℝ p μ := pos_part (-f) @[norm_cast] lemma coe_pos_part (f : Lp ℝ p μ) : (pos_part f : α →ₘ[μ] ℝ) = (f : α →ₘ[μ] ℝ).pos_part := rfl lemma coe_fn_pos_part (f : Lp ℝ p μ) : ⇑(pos_part f) =ᵐ[μ] λ a, max (f a) 0 := ae_eq_fun.coe_fn_pos_part _ lemma coe_fn_neg_part_eq_max (f : Lp ℝ p μ) : ∀ᵐ a ∂μ, neg_part f a = max (- f a) 0 := begin rw neg_part, filter_upwards [coe_fn_pos_part (-f), coe_fn_neg f] with _ h₁ h₂, rw [h₁, h₂, pi.neg_apply], end lemma coe_fn_neg_part (f : Lp ℝ p μ) : ∀ᵐ a ∂μ, neg_part f a = - min (f a) 0 := (coe_fn_neg_part_eq_max f).mono $ assume a h, by rw [h, ← max_neg_neg, neg_zero] lemma continuous_pos_part [fact (1 ≤ p)] : continuous (λf : Lp ℝ p μ, pos_part f) := lipschitz_with.continuous_comp_Lp _ _ lemma continuous_neg_part [fact (1 ≤ p)] : continuous (λf : Lp ℝ p μ, neg_part f) := have eq : (λf : Lp ℝ p μ, neg_part f) = (λf : Lp ℝ p μ, pos_part (-f)) := rfl, by { rw eq, exact continuous_pos_part.comp continuous_neg } end pos_part end Lp end measure_theory end composition /-! ## `L^p` is a complete space We show that `L^p` is a complete space for `1 ≤ p`. -/ section complete_space namespace measure_theory namespace Lp lemma snorm'_lim_eq_lintegral_liminf {ι} [nonempty ι] [linear_order ι] {f : ι → α → G} {p : ℝ} (hp_nonneg : 0 ≤ p) {f_lim : α → G} (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : snorm' f_lim p μ = (∫⁻ a, at_top.liminf (λ m, (∥f m a∥₊ : ℝ≥0∞)^p) ∂μ) ^ (1/p) := begin suffices h_no_pow : (∫⁻ a, ∥f_lim a∥₊ ^ p ∂μ) = (∫⁻ a, at_top.liminf (λ m, (∥f m a∥₊ : ℝ≥0∞)^p) ∂μ), { rw [snorm', h_no_pow], }, refine lintegral_congr_ae (h_lim.mono (λ a ha, _)), rw tendsto.liminf_eq, simp_rw [ennreal.coe_rpow_of_nonneg _ hp_nonneg, ennreal.tendsto_coe], refine ((nnreal.continuous_rpow_const hp_nonneg).tendsto (∥f_lim a∥₊)).comp _, exact (continuous_nnnorm.tendsto (f_lim a)).comp ha, end lemma snorm'_lim_le_liminf_snorm' {E} [normed_add_comm_group E] {f : ℕ → α → E} {p : ℝ} (hp_pos : 0 < p) (hf : ∀ n, ae_strongly_measurable (f n) μ) {f_lim : α → E} (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : snorm' f_lim p μ ≤ at_top.liminf (λ n, snorm' (f n) p μ) := begin rw snorm'_lim_eq_lintegral_liminf hp_pos.le h_lim, rw [←ennreal.le_rpow_one_div_iff (by simp [hp_pos] : 0 < 1 / p), one_div_one_div], refine (lintegral_liminf_le' (λ m, ((hf m).ennnorm.pow_const _))).trans_eq _, have h_pow_liminf : at_top.liminf (λ n, snorm' (f n) p μ) ^ p = at_top.liminf (λ n, (snorm' (f n) p μ) ^ p), { have h_rpow_mono := ennreal.strict_mono_rpow_of_pos hp_pos, have h_rpow_surj := (ennreal.rpow_left_bijective hp_pos.ne.symm).2, refine (h_rpow_mono.order_iso_of_surjective _ h_rpow_surj).liminf_apply _ _ _ _, all_goals { is_bounded_default }, }, rw h_pow_liminf, simp_rw [snorm', ← ennreal.rpow_mul, one_div, inv_mul_cancel hp_pos.ne.symm, ennreal.rpow_one], end lemma snorm_exponent_top_lim_eq_ess_sup_liminf {ι} [nonempty ι] [linear_order ι] {f : ι → α → G} {f_lim : α → G} (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : snorm f_lim ∞ μ = ess_sup (λ x, at_top.liminf (λ m, (∥f m x∥₊ : ℝ≥0∞))) μ := begin rw [snorm_exponent_top, snorm_ess_sup], refine ess_sup_congr_ae (h_lim.mono (λ x hx, _)), rw tendsto.liminf_eq, rw ennreal.tendsto_coe, exact (continuous_nnnorm.tendsto (f_lim x)).comp hx, end lemma snorm_exponent_top_lim_le_liminf_snorm_exponent_top {ι} [nonempty ι] [encodable ι] [linear_order ι] {f : ι → α → F} {f_lim : α → F} (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : snorm f_lim ∞ μ ≤ at_top.liminf (λ n, snorm (f n) ∞ μ) := begin rw snorm_exponent_top_lim_eq_ess_sup_liminf h_lim, simp_rw [snorm_exponent_top, snorm_ess_sup], exact ennreal.ess_sup_liminf_le (λ n, (λ x, (∥f n x∥₊ : ℝ≥0∞))), end lemma snorm_lim_le_liminf_snorm {E} [normed_add_comm_group E] {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) (f_lim : α → E) (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : snorm f_lim p μ ≤ at_top.liminf (λ n, snorm (f n) p μ) := begin by_cases hp0 : p = 0, { simp [hp0], }, rw ← ne.def at hp0, by_cases hp_top : p = ∞, { simp_rw [hp_top], exact snorm_exponent_top_lim_le_liminf_snorm_exponent_top h_lim, }, simp_rw snorm_eq_snorm' hp0 hp_top, have hp_pos : 0 < p.to_real, from ennreal.to_real_pos hp0 hp_top, exact snorm'_lim_le_liminf_snorm' hp_pos hf h_lim, end /-! ### `Lp` is complete iff Cauchy sequences of `ℒp` have limits in `ℒp` -/ lemma tendsto_Lp_iff_tendsto_ℒp' {ι} {fi : filter ι} [fact (1 ≤ p)] (f : ι → Lp E p μ) (f_lim : Lp E p μ) : fi.tendsto f (𝓝 f_lim) ↔ fi.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0) := begin rw tendsto_iff_dist_tendsto_zero, simp_rw dist_def, rw [← ennreal.zero_to_real, ennreal.tendsto_to_real_iff (λ n, _) ennreal.zero_ne_top], rw snorm_congr_ae (Lp.coe_fn_sub _ _).symm, exact Lp.snorm_ne_top _, end lemma tendsto_Lp_iff_tendsto_ℒp {ι} {fi : filter ι} [fact (1 ≤ p)] (f : ι → Lp E p μ) (f_lim : α → E) (f_lim_ℒp : mem_ℒp f_lim p μ) : fi.tendsto f (𝓝 (f_lim_ℒp.to_Lp f_lim)) ↔ fi.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0) := begin rw tendsto_Lp_iff_tendsto_ℒp', suffices h_eq : (λ n, snorm (f n - mem_ℒp.to_Lp f_lim f_lim_ℒp) p μ) = (λ n, snorm (f n - f_lim) p μ), by rw h_eq, exact funext (λ n, snorm_congr_ae (eventually_eq.rfl.sub (mem_ℒp.coe_fn_to_Lp f_lim_ℒp))), end lemma tendsto_Lp_iff_tendsto_ℒp'' {ι} {fi : filter ι} [fact (1 ≤ p)] (f : ι → α → E) (f_ℒp : ∀ n, mem_ℒp (f n) p μ) (f_lim : α → E) (f_lim_ℒp : mem_ℒp f_lim p μ) : fi.tendsto (λ n, (f_ℒp n).to_Lp (f n)) (𝓝 (f_lim_ℒp.to_Lp f_lim)) ↔ fi.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0) := begin convert Lp.tendsto_Lp_iff_tendsto_ℒp' _ _, ext1 n, apply snorm_congr_ae, filter_upwards [((f_ℒp n).sub f_lim_ℒp).coe_fn_to_Lp, Lp.coe_fn_sub ((f_ℒp n).to_Lp (f n)) (f_lim_ℒp.to_Lp f_lim)] with _ hx₁ hx₂, rw ← hx₂, exact hx₁.symm, end lemma tendsto_Lp_of_tendsto_ℒp {ι} {fi : filter ι} [hp : fact (1 ≤ p)] {f : ι → Lp E p μ} (f_lim : α → E) (f_lim_ℒp : mem_ℒp f_lim p μ) (h_tendsto : fi.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0)) : fi.tendsto f (𝓝 (f_lim_ℒp.to_Lp f_lim)) := (tendsto_Lp_iff_tendsto_ℒp f f_lim f_lim_ℒp).mpr h_tendsto lemma cauchy_seq_Lp_iff_cauchy_seq_ℒp {ι} [nonempty ι] [semilattice_sup ι] [hp : fact (1 ≤ p)] (f : ι → Lp E p μ) : cauchy_seq f ↔ tendsto (λ (n : ι × ι), snorm (f n.fst - f n.snd) p μ) at_top (𝓝 0) := begin simp_rw [cauchy_seq_iff_tendsto_dist_at_top_0, dist_def], rw [← ennreal.zero_to_real, ennreal.tendsto_to_real_iff (λ n, _) ennreal.zero_ne_top], rw snorm_congr_ae (Lp.coe_fn_sub _ _).symm, exact snorm_ne_top _, end lemma complete_space_Lp_of_cauchy_complete_ℒp [hp : fact (1 ≤ p)] (H : ∀ (f : ℕ → α → E) (hf : ∀ n, mem_ℒp (f n) p μ) (B : ℕ → ℝ≥0∞) (hB : ∑' i, B i < ∞) (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm (f n - f m) p μ < B N), ∃ (f_lim : α → E) (hf_lim_meas : mem_ℒp f_lim p μ), at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0)) : complete_space (Lp E p μ) := begin let B := λ n : ℕ, ((1:ℝ) / 2) ^ n, have hB_pos : ∀ n, 0 < B n, from λ n, pow_pos (div_pos zero_lt_one zero_lt_two) n, refine metric.complete_of_convergent_controlled_sequences B hB_pos (λ f hf, _), suffices h_limit : ∃ (f_lim : α → E) (hf_lim_meas : mem_ℒp f_lim p μ), at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0), { rcases h_limit with ⟨f_lim, hf_lim_meas, h_tendsto⟩, exact ⟨hf_lim_meas.to_Lp f_lim, tendsto_Lp_of_tendsto_ℒp f_lim hf_lim_meas h_tendsto⟩, }, have hB : summable B, from summable_geometric_two, cases hB with M hB, let B1 := λ n, ennreal.of_real (B n), have hB1_has : has_sum B1 (ennreal.of_real M), { have h_tsum_B1 : ∑' i, B1 i = (ennreal.of_real M), { change (∑' (n : ℕ), ennreal.of_real (B n)) = ennreal.of_real M, rw ←hB.tsum_eq, exact (ennreal.of_real_tsum_of_nonneg (λ n, le_of_lt (hB_pos n)) hB.summable).symm, }, have h_sum := (@ennreal.summable _ B1).has_sum, rwa h_tsum_B1 at h_sum, }, have hB1 : ∑' i, B1 i < ∞, by {rw hB1_has.tsum_eq, exact ennreal.of_real_lt_top, }, let f1 : ℕ → α → E := λ n, f n, refine H f1 (λ n, Lp.mem_ℒp (f n)) B1 hB1 (λ N n m hn hm, _), specialize hf N n m hn hm, rw dist_def at hf, simp_rw [f1, B1], rwa ennreal.lt_of_real_iff_to_real_lt, rw snorm_congr_ae (Lp.coe_fn_sub _ _).symm, exact Lp.snorm_ne_top _, end /-! ### Prove that controlled Cauchy sequences of `ℒp` have limits in `ℒp` -/ private lemma snorm'_sum_norm_sub_le_tsum_of_cauchy_snorm' {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) {p : ℝ} (hp1 : 1 ≤ p) {B : ℕ → ℝ≥0∞} (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm' (f n - f m) p μ < B N) (n : ℕ) : snorm' (λ x, ∑ i in finset.range (n + 1), ∥f (i + 1) x - f i x∥) p μ ≤ ∑' i, B i := begin let f_norm_diff := λ i x, ∥f (i + 1) x - f i x∥, have hgf_norm_diff : ∀ n, (λ x, ∑ i in finset.range (n + 1), ∥f (i + 1) x - f i x∥) = ∑ i in finset.range (n + 1), f_norm_diff i, from λ n, funext (λ x, by simp [f_norm_diff]), rw hgf_norm_diff, refine (snorm'_sum_le (λ i _, ((hf (i+1)).sub (hf i)).norm) hp1).trans _, simp_rw [←pi.sub_apply, snorm'_norm], refine (finset.sum_le_sum _).trans (sum_le_tsum _ (λ m _, zero_le _) ennreal.summable), exact λ m _, (h_cau m (m + 1) m (nat.le_succ m) (le_refl m)).le, end private lemma lintegral_rpow_sum_coe_nnnorm_sub_le_rpow_tsum {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) {p : ℝ} (hp1 : 1 ≤ p) {B : ℕ → ℝ≥0∞} (n : ℕ) (hn : snorm' (λ x, ∑ i in finset.range (n + 1), ∥f (i + 1) x - f i x∥) p μ ≤ ∑' i, B i) : ∫⁻ a, (∑ i in finset.range (n + 1), ∥f (i + 1) a - f i a∥₊ : ℝ≥0∞)^p ∂μ ≤ (∑' i, B i) ^ p := begin have hp_pos : 0 < p := zero_lt_one.trans_le hp1, rw [←one_div_one_div p, @ennreal.le_rpow_one_div_iff _ _ (1/p) (by simp [hp_pos]), one_div_one_div p], simp_rw snorm' at hn, have h_nnnorm_nonneg : (λ a, (∥∑ i in finset.range (n + 1), ∥f (i + 1) a - f i a∥∥₊ : ℝ≥0∞) ^ p) = λ a, (∑ i in finset.range (n + 1), (∥f (i + 1) a - f i a∥₊ : ℝ≥0∞)) ^ p, { ext1 a, congr, simp_rw ←of_real_norm_eq_coe_nnnorm, rw ←ennreal.of_real_sum_of_nonneg, { rw real.norm_of_nonneg _, exact finset.sum_nonneg (λ x hx, norm_nonneg _), }, { exact λ x hx, norm_nonneg _, }, }, change (∫⁻ a, (λ x, ↑∥∑ i in finset.range (n + 1), ∥f (i+1) x - f i x∥∥₊^p) a ∂μ)^(1/p) ≤ ∑' i, B i at hn, rwa h_nnnorm_nonneg at hn, end private lemma lintegral_rpow_tsum_coe_nnnorm_sub_le_tsum {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) {p : ℝ} (hp1 : 1 ≤ p) {B : ℕ → ℝ≥0∞} (h : ∀ n, ∫⁻ a, (∑ i in finset.range (n + 1), ∥f (i + 1) a - f i a∥₊ : ℝ≥0∞)^p ∂μ ≤ (∑' i, B i) ^ p) : (∫⁻ a, (∑' i, ∥f (i + 1) a - f i a∥₊ : ℝ≥0∞)^p ∂μ) ^ (1/p) ≤ ∑' i, B i := begin have hp_pos : 0 < p := zero_lt_one.trans_le hp1, suffices h_pow : ∫⁻ a, (∑' i, ∥f (i + 1) a - f i a∥₊ : ℝ≥0∞)^p ∂μ ≤ (∑' i, B i) ^ p, by rwa [←ennreal.le_rpow_one_div_iff (by simp [hp_pos] : 0 < 1 / p), one_div_one_div], have h_tsum_1 : ∀ g : ℕ → ℝ≥0∞, ∑' i, g i = at_top.liminf (λ n, ∑ i in finset.range (n + 1), g i), by { intro g, rw [ennreal.tsum_eq_liminf_sum_nat, ← liminf_nat_add _ 1], }, simp_rw h_tsum_1 _, rw ← h_tsum_1, have h_liminf_pow : ∫⁻ a, at_top.liminf (λ n, ∑ i in finset.range (n + 1), (∥f (i + 1) a - f i a∥₊))^p ∂μ = ∫⁻ a, at_top.liminf (λ n, (∑ i in finset.range (n + 1), (∥f (i + 1) a - f i a∥₊))^p) ∂μ, { refine lintegral_congr (λ x, _), have h_rpow_mono := ennreal.strict_mono_rpow_of_pos (zero_lt_one.trans_le hp1), have h_rpow_surj := (ennreal.rpow_left_bijective hp_pos.ne.symm).2, refine (h_rpow_mono.order_iso_of_surjective _ h_rpow_surj).liminf_apply _ _ _ _, all_goals { is_bounded_default }, }, rw h_liminf_pow, refine (lintegral_liminf_le' _).trans _, { exact λ n, (finset.ae_measurable_sum (finset.range (n+1)) (λ i _, ((hf (i+1)).sub (hf i)).ennnorm)).pow_const _, }, { exact liminf_le_of_frequently_le' (frequently_of_forall h), }, end private lemma tsum_nnnorm_sub_ae_lt_top {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) {p : ℝ} (hp1 : 1 ≤ p) {B : ℕ → ℝ≥0∞} (hB : ∑' i, B i ≠ ∞) (h : (∫⁻ a, (∑' i, ∥f (i + 1) a - f i a∥₊ : ℝ≥0∞)^p ∂μ) ^ (1/p) ≤ ∑' i, B i) : ∀ᵐ x ∂μ, (∑' i, ∥f (i + 1) x - f i x∥₊ : ℝ≥0∞) < ∞ := begin have hp_pos : 0 < p := zero_lt_one.trans_le hp1, have h_integral : ∫⁻ a, (∑' i, ∥f (i + 1) a - f i a∥₊ : ℝ≥0∞)^p ∂μ < ∞, { have h_tsum_lt_top : (∑' i, B i) ^ p < ∞, from ennreal.rpow_lt_top_of_nonneg hp_pos.le hB, refine lt_of_le_of_lt _ h_tsum_lt_top, rwa [←ennreal.le_rpow_one_div_iff (by simp [hp_pos] : 0 < 1 / p), one_div_one_div] at h, }, have rpow_ae_lt_top : ∀ᵐ x ∂μ, (∑' i, ∥f (i + 1) x - f i x∥₊ : ℝ≥0∞)^p < ∞, { refine ae_lt_top' (ae_measurable.pow_const _ _) h_integral.ne, exact ae_measurable.ennreal_tsum (λ n, ((hf (n+1)).sub (hf n)).ennnorm), }, refine rpow_ae_lt_top.mono (λ x hx, _), rwa [←ennreal.lt_rpow_one_div_iff hp_pos, ennreal.top_rpow_of_pos (by simp [hp_pos] : 0 < 1 / p)] at hx, end lemma ae_tendsto_of_cauchy_snorm' [complete_space E] {f : ℕ → α → E} {p : ℝ} (hf : ∀ n, ae_strongly_measurable (f n) μ) (hp1 : 1 ≤ p) {B : ℕ → ℝ≥0∞} (hB : ∑' i, B i ≠ ∞) (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm' (f n - f m) p μ < B N) : ∀ᵐ x ∂μ, ∃ l : E, at_top.tendsto (λ n, f n x) (𝓝 l) := begin have h_summable : ∀ᵐ x ∂μ, summable (λ (i : ℕ), f (i + 1) x - f i x), { have h1 : ∀ n, snorm' (λ x, ∑ i in finset.range (n + 1), ∥f (i + 1) x - f i x∥) p μ ≤ ∑' i, B i, from snorm'_sum_norm_sub_le_tsum_of_cauchy_snorm' hf hp1 h_cau, have h2 : ∀ n, ∫⁻ a, (∑ i in finset.range (n + 1), ∥f (i + 1) a - f i a∥₊ : ℝ≥0∞)^p ∂μ ≤ (∑' i, B i) ^ p, from λ n, lintegral_rpow_sum_coe_nnnorm_sub_le_rpow_tsum hf hp1 n (h1 n), have h3 : (∫⁻ a, (∑' i, ∥f (i + 1) a - f i a∥₊ : ℝ≥0∞)^p ∂μ) ^ (1/p) ≤ ∑' i, B i, from lintegral_rpow_tsum_coe_nnnorm_sub_le_tsum hf hp1 h2, have h4 : ∀ᵐ x ∂μ, (∑' i, ∥f (i + 1) x - f i x∥₊ : ℝ≥0∞) < ∞, from tsum_nnnorm_sub_ae_lt_top hf hp1 hB h3, exact h4.mono (λ x hx, summable_of_summable_nnnorm (ennreal.tsum_coe_ne_top_iff_summable.mp (lt_top_iff_ne_top.mp hx))), }, have h : ∀ᵐ x ∂μ, ∃ l : E, at_top.tendsto (λ n, ∑ i in finset.range n, (f (i + 1) x - f i x)) (𝓝 l), { refine h_summable.mono (λ x hx, _), let hx_sum := hx.has_sum.tendsto_sum_nat, exact ⟨∑' i, (f (i + 1) x - f i x), hx_sum⟩, }, refine h.mono (λ x hx, _), cases hx with l hx, have h_rw_sum : (λ n, ∑ i in finset.range n, (f (i + 1) x - f i x)) = λ n, f n x - f 0 x, { ext1 n, change ∑ (i : ℕ) in finset.range n, ((λ m, f m x) (i + 1) - (λ m, f m x) i) = f n x - f 0 x, rw finset.sum_range_sub, }, rw h_rw_sum at hx, have hf_rw : (λ n, f n x) = λ n, f n x - f 0 x + f 0 x, by { ext1 n, abel, }, rw hf_rw, exact ⟨l + f 0 x, tendsto.add_const _ hx⟩, end lemma ae_tendsto_of_cauchy_snorm [complete_space E] {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) (hp : 1 ≤ p) {B : ℕ → ℝ≥0∞} (hB : ∑' i, B i ≠ ∞) (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm (f n - f m) p μ < B N) : ∀ᵐ x ∂μ, ∃ l : E, at_top.tendsto (λ n, f n x) (𝓝 l) := begin by_cases hp_top : p = ∞, { simp_rw [hp_top] at *, have h_cau_ae : ∀ᵐ x ∂μ, ∀ N n m, N ≤ n → N ≤ m → (∥(f n - f m) x∥₊ : ℝ≥0∞) < B N, { simp_rw [ae_all_iff, ae_imp_iff], exact λ N n m hnN hmN, ae_lt_of_ess_sup_lt (h_cau N n m hnN hmN), }, simp_rw [snorm_exponent_top, snorm_ess_sup] at h_cau, refine h_cau_ae.mono (λ x hx, cauchy_seq_tendsto_of_complete _), refine cauchy_seq_of_le_tendsto_0 (λ n, (B n).to_real) _ _, { intros n m N hnN hmN, specialize hx N n m hnN hmN, rw [dist_eq_norm, ←ennreal.to_real_of_real (norm_nonneg _), ennreal.to_real_le_to_real ennreal.of_real_ne_top (ennreal.ne_top_of_tsum_ne_top hB N)], rw ←of_real_norm_eq_coe_nnnorm at hx, exact hx.le, }, { rw ← ennreal.zero_to_real, exact tendsto.comp (ennreal.tendsto_to_real ennreal.zero_ne_top) (ennreal.tendsto_at_top_zero_of_tsum_ne_top hB), }, }, have hp1 : 1 ≤ p.to_real, { rw [← ennreal.of_real_le_iff_le_to_real hp_top, ennreal.of_real_one], exact hp, }, have h_cau' : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm' (f n - f m) (p.to_real) μ < B N, { intros N n m hn hm, specialize h_cau N n m hn hm, rwa snorm_eq_snorm' (ennreal.zero_lt_one.trans_le hp).ne.symm hp_top at h_cau, }, exact ae_tendsto_of_cauchy_snorm' hf hp1 hB h_cau', end lemma cauchy_tendsto_of_tendsto {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) (f_lim : α → E) {B : ℕ → ℝ≥0∞} (hB : ∑' i, B i ≠ ∞) (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm (f n - f m) p μ < B N) (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0) := begin rw ennreal.tendsto_at_top_zero, intros ε hε, have h_B : ∃ (N : ℕ), B N ≤ ε, { suffices h_tendsto_zero : ∃ (N : ℕ), ∀ n : ℕ, N ≤ n → B n ≤ ε, from ⟨h_tendsto_zero.some, h_tendsto_zero.some_spec _ le_rfl⟩, exact (ennreal.tendsto_at_top_zero.mp (ennreal.tendsto_at_top_zero_of_tsum_ne_top hB)) ε hε, }, cases h_B with N h_B, refine ⟨N, λ n hn, _⟩, have h_sub : snorm (f n - f_lim) p μ ≤ at_top.liminf (λ m, snorm (f n - f m) p μ), { refine snorm_lim_le_liminf_snorm (λ m, (hf n).sub (hf m)) (f n - f_lim) _, refine h_lim.mono (λ x hx, _), simp_rw sub_eq_add_neg, exact tendsto.add tendsto_const_nhds (tendsto.neg hx), }, refine h_sub.trans _, refine liminf_le_of_frequently_le' (frequently_at_top.mpr _), refine λ N1, ⟨max N N1, le_max_right _ _, _⟩, exact (h_cau N n (max N N1) hn (le_max_left _ _)).le.trans h_B, end lemma mem_ℒp_of_cauchy_tendsto (hp : 1 ≤ p) {f : ℕ → α → E} (hf : ∀ n, mem_ℒp (f n) p μ) (f_lim : α → E) (h_lim_meas : ae_strongly_measurable f_lim μ) (h_tendsto : at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0)) : mem_ℒp f_lim p μ := begin refine ⟨h_lim_meas, _⟩, rw ennreal.tendsto_at_top_zero at h_tendsto, cases (h_tendsto 1 ennreal.zero_lt_one) with N h_tendsto_1, specialize h_tendsto_1 N (le_refl N), have h_add : f_lim = f_lim - f N + f N, by abel, rw h_add, refine lt_of_le_of_lt (snorm_add_le (h_lim_meas.sub (hf N).1) (hf N).1 hp) _, rw ennreal.add_lt_top, split, { refine lt_of_le_of_lt _ ennreal.one_lt_top, have h_neg : f_lim - f N = -(f N - f_lim), by simp, rwa [h_neg, snorm_neg], }, { exact (hf N).2, }, end lemma cauchy_complete_ℒp [complete_space E] (hp : 1 ≤ p) {f : ℕ → α → E} (hf : ∀ n, mem_ℒp (f n) p μ) {B : ℕ → ℝ≥0∞} (hB : ∑' i, B i ≠ ∞) (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm (f n - f m) p μ < B N) : ∃ (f_lim : α → E) (hf_lim_meas : mem_ℒp f_lim p μ), at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0) := begin obtain ⟨f_lim, h_f_lim_meas, h_lim⟩ : ∃ (f_lim : α → E) (hf_lim_meas : strongly_measurable f_lim), ∀ᵐ x ∂μ, tendsto (λ n, f n x) at_top (nhds (f_lim x)), from exists_strongly_measurable_limit_of_tendsto_ae (λ n, (hf n).1) (ae_tendsto_of_cauchy_snorm (λ n, (hf n).1) hp hB h_cau), have h_tendsto' : at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0), from cauchy_tendsto_of_tendsto (λ m, (hf m).1) f_lim hB h_cau h_lim, have h_ℒp_lim : mem_ℒp f_lim p μ, from mem_ℒp_of_cauchy_tendsto hp hf f_lim h_f_lim_meas.ae_strongly_measurable h_tendsto', exact ⟨f_lim, h_ℒp_lim, h_tendsto'⟩, end /-! ### `Lp` is complete for `1 ≤ p` -/ instance [complete_space E] [hp : fact (1 ≤ p)] : complete_space (Lp E p μ) := complete_space_Lp_of_cauchy_complete_ℒp $ λ f hf B hB h_cau, cauchy_complete_ℒp hp.elim hf hB.ne h_cau end Lp end measure_theory end complete_space /-! ### Continuous functions in `Lp` -/ open_locale bounded_continuous_function open bounded_continuous_function section variables [topological_space α] [borel_space α] [second_countable_topology_either α E] variables (E p μ) /-- An additive subgroup of `Lp E p μ`, consisting of the equivalence classes which contain a bounded continuous representative. -/ def measure_theory.Lp.bounded_continuous_function : add_subgroup (Lp E p μ) := add_subgroup.add_subgroup_of ((continuous_map.to_ae_eq_fun_add_hom μ).comp (to_continuous_map_add_hom α E)).range (Lp E p μ) variables {E p μ} /-- By definition, the elements of `Lp.bounded_continuous_function E p μ` are the elements of `Lp E p μ` which contain a bounded continuous representative. -/ lemma measure_theory.Lp.mem_bounded_continuous_function_iff {f : (Lp E p μ)} : f ∈ measure_theory.Lp.bounded_continuous_function E p μ ↔ ∃ f₀ : (α →ᵇ E), f₀.to_continuous_map.to_ae_eq_fun μ = (f : α →ₘ[μ] E) := add_subgroup.mem_add_subgroup_of namespace bounded_continuous_function variables [is_finite_measure μ] /-- A bounded continuous function on a finite-measure space is in `Lp`. -/ lemma mem_Lp (f : α →ᵇ E) : f.to_continuous_map.to_ae_eq_fun μ ∈ Lp E p μ := begin refine Lp.mem_Lp_of_ae_bound (∥f∥) _, filter_upwards [f.to_continuous_map.coe_fn_to_ae_eq_fun μ] with x _, convert f.norm_coe_le_norm x end /-- The `Lp`-norm of a bounded continuous function is at most a constant (depending on the measure of the whole space) times its sup-norm. -/ lemma Lp_norm_le (f : α →ᵇ E) : ∥(⟨f.to_continuous_map.to_ae_eq_fun μ, mem_Lp f⟩ : Lp E p μ)∥ ≤ (measure_univ_nnreal μ) ^ (p.to_real)⁻¹ * ∥f∥ := begin apply Lp.norm_le_of_ae_bound (norm_nonneg f), { refine (f.to_continuous_map.coe_fn_to_ae_eq_fun μ).mono _, intros x hx, convert f.norm_coe_le_norm x }, { apply_instance } end variables (p μ) /-- The normed group homomorphism of considering a bounded continuous function on a finite-measure space as an element of `Lp`. -/ def to_Lp_hom [fact (1 ≤ p)] : normed_add_group_hom (α →ᵇ E) (Lp E p μ) := { bound' := ⟨_, Lp_norm_le⟩, .. add_monoid_hom.cod_restrict ((continuous_map.to_ae_eq_fun_add_hom μ).comp (to_continuous_map_add_hom α E)) (Lp E p μ) mem_Lp } lemma range_to_Lp_hom [fact (1 ≤ p)] : ((to_Lp_hom p μ).range : add_subgroup (Lp E p μ)) = measure_theory.Lp.bounded_continuous_function E p μ := begin symmetry, convert add_monoid_hom.add_subgroup_of_range_eq_of_le ((continuous_map.to_ae_eq_fun_add_hom μ).comp (to_continuous_map_add_hom α E)) (by { rintros - ⟨f, rfl⟩, exact mem_Lp f } : _ ≤ Lp E p μ), end variables (𝕜 : Type*) /-- The bounded linear map of considering a bounded continuous function on a finite-measure space as an element of `Lp`. -/ def to_Lp [normed_field 𝕜] [normed_space 𝕜 E] [fact (1 ≤ p)] : (α →ᵇ E) →L[𝕜] (Lp E p μ) := linear_map.mk_continuous (linear_map.cod_restrict (Lp.Lp_submodule E p μ 𝕜) ((continuous_map.to_ae_eq_fun_linear_map μ).comp (to_continuous_map_linear_map α E 𝕜)) mem_Lp) _ Lp_norm_le variables {𝕜} lemma range_to_Lp [normed_field 𝕜] [normed_space 𝕜 E] [fact (1 ≤ p)] : (((to_Lp p μ 𝕜).range : submodule 𝕜 (Lp E p μ)).to_add_subgroup) = measure_theory.Lp.bounded_continuous_function E p μ := range_to_Lp_hom p μ variables {p} lemma coe_fn_to_Lp [normed_field 𝕜] [normed_space 𝕜 E] [fact (1 ≤ p)] (f : α →ᵇ E) : to_Lp p μ 𝕜 f =ᵐ[μ] f := ae_eq_fun.coe_fn_mk f _ lemma to_Lp_norm_le [nontrivially_normed_field 𝕜] [normed_space 𝕜 E] [fact (1 ≤ p)] : ∥(to_Lp p μ 𝕜 : (α →ᵇ E) →L[𝕜] (Lp E p μ))∥ ≤ (measure_univ_nnreal μ) ^ (p.to_real)⁻¹ := linear_map.mk_continuous_norm_le _ ((measure_univ_nnreal μ) ^ (p.to_real)⁻¹).coe_nonneg _ end bounded_continuous_function namespace continuous_map variables [compact_space α] [is_finite_measure μ] variables (𝕜 : Type*) (p μ) [fact (1 ≤ p)] /-- The bounded linear map of considering a continuous function on a compact finite-measure space `α` as an element of `Lp`. By definition, the norm on `C(α, E)` is the sup-norm, transferred from the space `α →ᵇ E` of bounded continuous functions, so this construction is just a matter of transferring the structure from `bounded_continuous_function.to_Lp` along the isometry. -/ def to_Lp [normed_field 𝕜] [normed_space 𝕜 E] : C(α, E) →L[𝕜] (Lp E p μ) := (bounded_continuous_function.to_Lp p μ 𝕜).comp (linear_isometry_bounded_of_compact α E 𝕜).to_linear_isometry.to_continuous_linear_map variables {𝕜} lemma range_to_Lp [normed_field 𝕜] [normed_space 𝕜 E] : ((to_Lp p μ 𝕜).range : submodule 𝕜 (Lp E p μ)).to_add_subgroup = measure_theory.Lp.bounded_continuous_function E p μ := begin refine set_like.ext' _, have := (linear_isometry_bounded_of_compact α E 𝕜).surjective, convert function.surjective.range_comp this (bounded_continuous_function.to_Lp p μ 𝕜), rw ← bounded_continuous_function.range_to_Lp p μ, refl, end variables {p} lemma coe_fn_to_Lp [normed_field 𝕜] [normed_space 𝕜 E] (f : C(α, E)) : to_Lp p μ 𝕜 f =ᵐ[μ] f := ae_eq_fun.coe_fn_mk f _ lemma to_Lp_def [normed_field 𝕜] [normed_space 𝕜 E] (f : C(α, E)) : to_Lp p μ 𝕜 f = bounded_continuous_function.to_Lp p μ 𝕜 (linear_isometry_bounded_of_compact α E 𝕜 f) := rfl @[simp] lemma to_Lp_comp_to_continuous_map [normed_field 𝕜] [normed_space 𝕜 E] (f : α →ᵇ E) : to_Lp p μ 𝕜 f.to_continuous_map = bounded_continuous_function.to_Lp p μ 𝕜 f := rfl @[simp] lemma coe_to_Lp [normed_field 𝕜] [normed_space 𝕜 E] (f : C(α, E)) : (to_Lp p μ 𝕜 f : α →ₘ[μ] E) = f.to_ae_eq_fun μ := rfl variables [nontrivially_normed_field 𝕜] [normed_space 𝕜 E] lemma to_Lp_norm_eq_to_Lp_norm_coe : ∥(to_Lp p μ 𝕜 : C(α, E) →L[𝕜] (Lp E p μ))∥ = ∥(bounded_continuous_function.to_Lp p μ 𝕜 : (α →ᵇ E) →L[𝕜] (Lp E p μ))∥ := continuous_linear_map.op_norm_comp_linear_isometry_equiv _ _ /-- Bound for the operator norm of `continuous_map.to_Lp`. -/ lemma to_Lp_norm_le : ∥(to_Lp p μ 𝕜 : C(α, E) →L[𝕜] (Lp E p μ))∥ ≤ (measure_univ_nnreal μ) ^ (p.to_real)⁻¹ := by { rw to_Lp_norm_eq_to_Lp_norm_coe, exact bounded_continuous_function.to_Lp_norm_le μ } end continuous_map end namespace measure_theory namespace Lp lemma pow_mul_meas_ge_le_norm (f : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (ε : ℝ≥0∞) : (ε * μ {x | ε ≤ ∥f x∥₊ ^ p.to_real}) ^ (1 / p.to_real) ≤ (ennreal.of_real ∥f∥) := (ennreal.of_real_to_real (snorm_ne_top f)).symm ▸ pow_mul_meas_ge_le_snorm μ hp_ne_zero hp_ne_top (Lp.ae_strongly_measurable f) ε lemma mul_meas_ge_le_pow_norm (f : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (ε : ℝ≥0∞) : ε * μ {x | ε ≤ ∥f x∥₊ ^ p.to_real} ≤ (ennreal.of_real ∥f∥) ^ p.to_real := (ennreal.of_real_to_real (snorm_ne_top f)).symm ▸ mul_meas_ge_le_pow_snorm μ hp_ne_zero hp_ne_top (Lp.ae_strongly_measurable f) ε /-- A version of Markov's inequality with elements of Lp. -/ lemma mul_meas_ge_le_pow_norm' (f : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (ε : ℝ≥0∞) : ε ^ p.to_real * μ {x | ε ≤ ∥f x∥₊} ≤ (ennreal.of_real ∥f∥) ^ p.to_real := (ennreal.of_real_to_real (snorm_ne_top f)).symm ▸ mul_meas_ge_le_pow_snorm' μ hp_ne_zero hp_ne_top (Lp.ae_strongly_measurable f) ε lemma meas_ge_le_mul_pow_norm (f : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) : μ {x | ε ≤ ∥f x∥₊} ≤ ε⁻¹ ^ p.to_real * (ennreal.of_real ∥f∥) ^ p.to_real := (ennreal.of_real_to_real (snorm_ne_top f)).symm ▸ meas_ge_le_mul_pow_snorm μ hp_ne_zero hp_ne_top (Lp.ae_strongly_measurable f) hε end Lp end measure_theory
9b323fa06bde3ff6c4b4a88bd085f08b6fde4f50
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/library/data/real/order.lean
837f0ff6b5de7888bf1a0e52966bb0de5035a3be
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
38,741
lean
/- Copyright (c) 2015 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Robert Y. Lewis The real numbers, constructed as equivalence classes of Cauchy sequences of rationals. This construction follows Bishop and Bridges (1985). To do: o Rename things and possibly make theorems private -/ import data.real.basic data.rat data.nat open rat nat eq pnat local postfix `⁻¹` := pnat.inv namespace rat_seq definition pos (s : seq) := ∃ n : ℕ+, n⁻¹ < (s n) definition nonneg (s : seq) := ∀ n : ℕ+, -(n⁻¹) ≤ s n theorem sub_sub_comm (a b c : ℚ) : a - b - c = a - c - b := by rewrite [+sub_eq_add_neg, add.assoc, {-b+_}add.comm, -add.assoc] theorem bdd_away_of_pos {s : seq} (Hs : regular s) (H : pos s) : ∃ N : ℕ+, ∀ n : ℕ+, n ≥ N → (s n) ≥ N⁻¹ := begin cases H with [n, Hn], cases sep_by_inv Hn with [N, HN], existsi N, intro m Hm, have Habs : abs (s m - s n) ≥ s n - s m, by rewrite abs_sub; apply le_abs_self, have Habs' : s m + abs (s m - s n) ≥ s n, from (iff.mpr (le_add_iff_sub_left_le _ _ _)) Habs, have HN' : N⁻¹ + N⁻¹ ≤ s n - n⁻¹, begin rewrite sub_eq_add_neg, apply iff.mpr (le_add_iff_sub_right_le _ _ _), rewrite [sub_neg_eq_add, add.comm, -add.assoc], apply le_of_lt HN end, rewrite add.comm at Habs', have Hin : s m ≥ N⁻¹, from calc s m ≥ s n - abs (s m - s n) : (iff.mp (le_add_iff_sub_left_le _ _ _)) Habs' ... ≥ s n - (m⁻¹ + n⁻¹) : sub_le_sub_left !Hs ... = s n - m⁻¹ - n⁻¹ : by rewrite sub_add_eq_sub_sub ... = s n - n⁻¹ - m⁻¹ : by rewrite sub_sub_comm ... ≥ s n - n⁻¹ - N⁻¹ : sub_le_sub_left (inv_ge_of_le Hm) ... ≥ N⁻¹ + N⁻¹ - N⁻¹ : sub_le_sub_right HN' ... = N⁻¹ : by rewrite add_sub_cancel, apply Hin end theorem pos_of_bdd_away {s : seq} (H : ∃ N : ℕ+, ∀ n : ℕ+, n ≥ N → (s n) ≥ N⁻¹) : pos s := begin cases H with [N, HN], existsi (N + pone), apply lt_of_lt_of_le, apply inv_add_lt_left, apply HN, apply pnat.le_of_lt, apply lt_add_left end theorem bdd_within_of_nonneg {s : seq} (Hs : regular s) (H : nonneg s) : ∀ n : ℕ+, ∃ N : ℕ+, ∀ m : ℕ+, m ≥ N → s m ≥ -n⁻¹ := begin intros, existsi n, intro m Hm, apply le.trans, apply neg_le_neg, apply inv_ge_of_le, apply Hm, apply H end theorem nonneg_of_bdd_within {s : seq} (Hs : regular s) (H : ∀n : ℕ+, ∃ N : ℕ+, ∀ m : ℕ+, m ≥ N → s m ≥ -n⁻¹) : nonneg s := begin rewrite ↑nonneg, intro k, apply ge_of_forall_ge_sub, intro ε Hε, cases H (pceil ((2) / ε)) with [N, HN], apply le.trans, rotate 1, apply sub_le_of_abs_sub_le_left, apply Hs, apply (max (pceil ((2)/ε)) N), rewrite [+sub_eq_add_neg, neg_add, {_ + (-k⁻¹ + _)}add.comm, *add.assoc], apply rat.add_le_add_left, apply le.trans, rotate 1, apply add_le_add, rotate 1, apply HN (max (pceil ((2)/ε)) N) !pnat.max_right, rotate_right 1, apply neg_le_neg, apply inv_ge_of_le, apply pnat.max_left, rewrite -neg_add, apply neg_le_neg, apply le.trans, apply add_le_add, repeat (apply inv_pceil_div; apply add_pos; repeat apply zero_lt_one; exact Hε), rewrite [add_halves], apply rat.le_refl end theorem pos_of_pos_equiv {s t : seq} (Hs : regular s) (Heq : s ≡ t) (Hp : pos s) : pos t := begin cases (bdd_away_of_pos Hs Hp) with [N, HN], existsi 2 * 2 * N, apply lt_of_lt_of_le, rotate 1, apply sub_le_of_abs_sub_le_right, apply Heq, have Hs4 : N⁻¹ ≤ s (2 * 2 * N), from HN _ (!pnat.mul_le_mul_left), apply lt_of_lt_of_le, rotate 1, rewrite sub_eq_add_neg, apply iff.mpr !add_le_add_right_iff, apply Hs4, rewrite [*pnat.mul_assoc, pnat.add_halves, -(pnat.add_halves N), -sub_eq_add_neg, add_sub_cancel], apply inv_two_mul_lt_inv end theorem nonneg_of_nonneg_equiv {s t : seq} (Hs : regular s) (Ht : regular t) (Heq : s ≡ t) (Hp : nonneg s) : nonneg t := begin apply nonneg_of_bdd_within, apply Ht, intros, cases bdd_within_of_nonneg Hs Hp (2 * 2 * n) with [Ns, HNs], existsi max Ns (2 * 2 * n), intro m Hm, apply le.trans, rotate 1, apply sub_le_of_abs_sub_le_right, apply Heq, apply le.trans, rotate 1, apply sub_le_sub_right, apply HNs, apply pnat.le_trans, rotate 1, apply Hm, rotate_right 1, apply pnat.max_left, have Hms : m⁻¹ ≤ (2 * 2 * n)⁻¹, begin apply inv_ge_of_le, apply pnat.le_trans, rotate 1, apply Hm; apply pnat.max_right end, have Hms' : m⁻¹ + m⁻¹ ≤ (2 * 2 * n)⁻¹ + (2 * 2 * n)⁻¹, from add_le_add Hms Hms, apply le.trans, rotate 1, apply sub_le_sub_left, apply Hms', rewrite [*pnat.mul_assoc, pnat.add_halves, -neg_sub, -pnat.add_halves n, sub_neg_eq_add], apply neg_le_neg, apply add_le_add_left, apply inv_two_mul_le_inv end definition s_le (a b : seq) := nonneg (sadd b (sneg a)) definition s_lt (a b : seq) := pos (sadd b (sneg a)) theorem zero_nonneg : nonneg zero := begin intros, apply neg_nonpos_of_nonneg, apply rat.le_of_lt, apply pnat.inv_pos end theorem s_zero_lt_one : s_lt zero one := begin rewrite [↑s_lt, ↑zero, ↑sadd, ↑sneg, ↑one, neg_zero, add_zero, ↑pos], existsi 2, apply inv_lt_one_of_gt, apply one_lt_two end protected theorem le_refl {s : seq} (Hs : regular s) : s_le s s := begin apply nonneg_of_nonneg_equiv, rotate 2, apply equiv.symm, apply neg_s_cancel s Hs, apply zero_nonneg, apply zero_is_reg, apply reg_add_reg Hs (reg_neg_reg Hs) end theorem s_nonneg_of_pos {s : seq} (Hs : regular s) (H : pos s) : nonneg s := begin apply nonneg_of_bdd_within, apply Hs, intros, cases bdd_away_of_pos Hs H with [N, HN], existsi N, intro m Hm, apply le.trans, rotate 1, apply HN, apply Hm, apply le.trans, rotate 1, apply rat.le_of_lt, apply pnat.inv_pos, rewrite -neg_zero, apply neg_le_neg, apply rat.le_of_lt, apply pnat.inv_pos end theorem s_le_of_s_lt {s t : seq} (Hs : regular s) (Ht : regular t) (H : s_lt s t) : s_le s t := begin rewrite [↑s_le, ↑s_lt at *], apply s_nonneg_of_pos, repeat (apply reg_add_reg | apply reg_neg_reg | assumption) end theorem s_neg_add_eq_s_add_neg (s t : seq) : sneg (sadd s t) ≡ sadd (sneg s) (sneg t) := begin rewrite [↑equiv, ↑sadd, ↑sneg], intros, rewrite [neg_add, sub_self, abs_zero], apply add_invs_nonneg end theorem equiv_cancel_middle {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u) : sadd (sadd u t) (sneg (sadd u s)) ≡ sadd t (sneg s) := begin note Hz := zero_is_reg, apply equiv.trans, rotate 3, apply add_well_defined, rotate 4, apply s_add_comm, apply s_neg_add_eq_s_add_neg, apply equiv.trans, rotate 3, apply s_add_assoc, rotate 2, apply add_well_defined, rotate 4, apply equiv.refl, apply equiv.trans, rotate 4, apply equiv.refl, rotate_right 1, apply equiv.trans, rotate 3, apply equiv.symm, apply s_add_assoc, rotate 2, apply equiv.trans, rotate 4, apply s_zero_add, rotate_right 1, apply add_well_defined, rotate 4, apply neg_s_cancel, rotate 1, apply equiv.refl, repeat (apply reg_add_reg | apply reg_neg_reg | assumption) end protected theorem add_le_add_of_le_right {s t : seq} (Hs : regular s) (Ht : regular t) (Lst : s_le s t) : ∀ u : seq, regular u → s_le (sadd u s) (sadd u t) := begin intro u Hu, rewrite [↑s_le at *], apply nonneg_of_nonneg_equiv, rotate 2, apply equiv.symm, apply equiv_cancel_middle, repeat (apply reg_add_reg | apply reg_neg_reg | assumption) end theorem s_add_lt_add_left {s t : seq} (Hs : regular s) (Ht : regular t) (Hst : s_lt s t) {u : seq} (Hu : regular u) : s_lt (sadd u s) (sadd u t) := begin rewrite ↑s_lt at *, apply pos_of_pos_equiv, rotate 1, apply equiv.symm, apply equiv_cancel_middle, repeat (apply reg_add_reg | apply reg_neg_reg | assumption) end protected theorem add_nonneg_of_nonneg {s t : seq} (Hs : nonneg s) (Ht : nonneg t) : nonneg (sadd s t) := begin intros, rewrite [-pnat.add_halves, neg_add], apply add_le_add, apply Hs, apply Ht end protected theorem le_trans {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u) (Lst : s_le s t) (Ltu : s_le t u) : s_le s u := begin rewrite ↑s_le at *, note Rz := zero_is_reg, have Hsum : nonneg (sadd (sadd u (sneg t)) (sadd t (sneg s))), from rat_seq.add_nonneg_of_nonneg Ltu Lst, have H' : nonneg (sadd (sadd u (sadd (sneg t) t)) (sneg s)), begin apply nonneg_of_nonneg_equiv, rotate 2, apply add_well_defined, rotate 4, apply s_add_assoc, repeat (apply reg_add_reg | apply reg_neg_reg | assumption), apply equiv.refl, apply nonneg_of_nonneg_equiv, rotate 2, apply equiv.symm, apply s_add_assoc, rotate 2, repeat (apply reg_add_reg | apply reg_neg_reg | assumption) end, have H'' : sadd (sadd u (sadd (sneg t) t)) (sneg s) ≡ sadd u (sneg s), begin apply add_well_defined, rotate 4, apply equiv.trans, rotate 3, apply add_well_defined, rotate 4, apply equiv.refl, apply s_neg_cancel, rotate 1, apply s_add_zero, rotate 1, apply equiv.refl, repeat (apply reg_add_reg | apply reg_neg_reg | assumption) end, apply nonneg_of_nonneg_equiv, rotate 2, apply H'', apply H', repeat (apply reg_add_reg | apply reg_neg_reg | assumption) end theorem equiv_of_le_of_ge {s t : seq} (Hs : regular s) (Ht : regular t) (Lst : s_le s t) (Lts : s_le t s) : s ≡ t := begin apply equiv_of_diff_equiv_zero, rotate 2, rewrite [↑s_le at *, ↑nonneg at *, ↑equiv, ↑sadd at *, ↑sneg at *], intros, rewrite [↑zero, sub_zero], apply abs_le_of_le_of_neg_le, apply le_of_neg_le_neg, rewrite [2 neg_add, neg_neg], apply rat.le_trans, apply neg_add_neg_le_neg_of_pos, apply pnat.inv_pos, rewrite add.comm, apply Lst, apply le_of_neg_le_neg, rewrite [neg_add, neg_neg], apply rat.le_trans, apply neg_add_neg_le_neg_of_pos, apply pnat.inv_pos, apply Lts, repeat assumption end definition sep (s t : seq) := s_lt s t ∨ s_lt t s local infix `≢` : 50 := sep theorem le_and_sep_of_lt {s t : seq} (Hs : regular s) (Ht : regular t) (Lst : s_lt s t) : s_le s t ∧ sep s t := begin apply and.intro, intros, cases Lst with [N, HN], let Rns := reg_neg_reg Hs, let Rtns := reg_add_reg Ht Rns, note Habs := sub_le_of_abs_sub_le_right (Rtns N n), rewrite [sub_add_eq_sub_sub at Habs], exact (calc sadd t (sneg s) n ≥ sadd t (sneg s) N - N⁻¹ - n⁻¹ : Habs ... ≥ 0 - n⁻¹: begin apply sub_le_sub_right, apply rat.le_of_lt, apply (iff.mpr (sub_pos_iff_lt _ _)), apply HN end ... = -n⁻¹ : by rewrite zero_sub), exact or.inl Lst end theorem lt_of_le_and_sep {s t : seq} (Hs : regular s) (Ht : regular t) (H : s_le s t ∧ sep s t) : s_lt s t := begin note Le := and.left H, cases and.right H with [P, Hlt], exact P, rewrite [↑s_le at Le, ↑nonneg at Le, ↑s_lt at Hlt, ↑pos at Hlt], apply exists.elim Hlt, intro N HN, let LeN := Le N, note HN' := (iff.mpr !neg_lt_neg_iff_lt) HN, rewrite [↑sadd at HN', ↑sneg at HN', neg_add at HN', neg_neg at HN', add.comm at HN'], let HN'' := not_le_of_gt HN', apply absurd LeN HN'' end theorem lt_iff_le_and_sep {s t : seq} (Hs : regular s) (Ht : regular t) : s_lt s t ↔ s_le s t ∧ sep s t := iff.intro (le_and_sep_of_lt Hs Ht) (lt_of_le_and_sep Hs Ht) theorem s_neg_zero : sneg zero ≡ zero := begin rewrite ↑[sneg, zero, equiv], intros, rewrite [sub_zero, abs_neg, abs_zero], apply add_invs_nonneg end theorem s_sub_zero {s : seq} (Hs : regular s) : sadd s (sneg zero) ≡ s := begin apply equiv.trans, rotate 3, apply add_well_defined, rotate 4, apply equiv.refl, apply s_neg_zero, apply s_add_zero, repeat (assumption | apply reg_add_reg | apply reg_neg_reg | apply zero_is_reg) end theorem s_pos_of_gt_zero {s : seq} (Hs : regular s) (Hgz : s_lt zero s) : pos s := begin rewrite [↑s_lt at *], apply pos_of_pos_equiv, rotate 1, apply s_sub_zero, repeat (assumption | apply reg_add_reg | apply reg_neg_reg), apply zero_is_reg end theorem s_gt_zero_of_pos {s : seq} (Hs : regular s) (Hp : pos s) : s_lt zero s := begin rewrite ↑s_lt, apply pos_of_pos_equiv, rotate 1, apply equiv.symm, apply s_sub_zero, repeat assumption end theorem s_nonneg_of_ge_zero {s : seq} (Hs : regular s) (Hgz : s_le zero s) : nonneg s := begin rewrite ↑s_le at *, apply nonneg_of_nonneg_equiv, rotate 2, apply s_sub_zero, repeat (assumption | apply reg_add_reg | apply reg_neg_reg | apply zero_is_reg) end theorem s_ge_zero_of_nonneg {s : seq} (Hs : regular s) (Hn : nonneg s) : s_le zero s := begin rewrite ↑s_le, apply nonneg_of_nonneg_equiv, rotate 2, apply equiv.symm, apply s_sub_zero, repeat (assumption | apply reg_add_reg | apply reg_neg_reg | apply zero_is_reg) end theorem s_mul_pos_of_pos {s t : seq} (Hs : regular s) (Ht : regular t) (Hps : pos s) (Hpt : pos t) : pos (smul s t) := begin rewrite [↑pos at *], cases bdd_away_of_pos Hs Hps with [Ns, HNs], cases bdd_away_of_pos Ht Hpt with [Nt, HNt], existsi 2 * max Ns Nt * max Ns Nt, rewrite ↑smul, apply lt_of_lt_of_le, rotate 1, apply mul_le_mul, apply HNs, apply pnat.le_trans, apply pnat.max_left Ns Nt, rewrite -pnat.mul_assoc, apply pnat.mul_le_mul_left, apply HNt, apply pnat.le_trans, apply pnat.max_right Ns Nt, rewrite -pnat.mul_assoc, apply pnat.mul_le_mul_left, apply rat.le_of_lt, apply pnat.inv_pos, apply rat.le_trans, rotate 1, apply HNs, apply pnat.le_trans, apply pnat.max_left Ns Nt, rewrite -pnat.mul_assoc, apply pnat.mul_le_mul_left, rewrite pnat.inv_mul_eq_mul_inv, apply mul_lt_mul, rewrite [pnat.inv_mul_eq_mul_inv, -one_mul Ns⁻¹], apply mul_lt_mul, apply inv_lt_one_of_gt, apply dec_trivial, apply inv_ge_of_le, apply pnat.max_left, apply pnat.inv_pos, apply rat.le_of_lt zero_lt_one, apply inv_ge_of_le, apply pnat.max_right, apply pnat.inv_pos, repeat (apply le_of_lt; apply pnat.inv_pos) end theorem s_mul_gt_zero_of_gt_zero {s t : seq} (Hs : regular s) (Ht : regular t) (Hzs : s_lt zero s) (Hzt : s_lt zero t) : s_lt zero (smul s t) := s_gt_zero_of_pos (reg_mul_reg Hs Ht) (s_mul_pos_of_pos Hs Ht (s_pos_of_gt_zero Hs Hzs) (s_pos_of_gt_zero Ht Hzt)) theorem le_of_lt_or_equiv {s t : seq} (Hs : regular s) (Ht : regular t) (Hor : (s_lt s t) ∨ (s ≡ t)) : s_le s t := or.elim Hor (begin intro Hlt, apply s_le_of_s_lt Hs Ht Hlt end) (begin intro Heq, rewrite ↑s_le, apply nonneg_of_nonneg_equiv, rotate 3, apply zero_nonneg, apply zero_is_reg, apply reg_add_reg Ht (reg_neg_reg Hs), apply equiv.symm, apply diff_equiv_zero_of_equiv, rotate 2, apply equiv.symm, apply Heq, repeat assumption end) theorem s_zero_mul {s : seq} : smul s zero ≡ zero := begin rewrite [↑equiv, ↑smul, ↑zero], intros, rewrite [mul_zero, sub_zero, abs_zero], apply add_invs_nonneg end theorem s_mul_nonneg_of_pos_of_zero {s t : seq} (Hs : regular s) (Ht : regular t) (Hps : pos s) (Hpt : zero ≡ t) : nonneg (smul s t) := begin apply nonneg_of_nonneg_equiv, rotate 2, apply mul_well_defined, rotate 4, apply equiv.refl, apply Hpt, apply nonneg_of_nonneg_equiv, rotate 2, apply equiv.symm, apply s_zero_mul, apply zero_nonneg, repeat (assumption | apply reg_mul_reg | apply zero_is_reg) end theorem s_mul_nonneg_of_nonneg {s t : seq} (Hs : regular s) (Ht : regular t) (Hps : nonneg s) (Hpt : nonneg t) : nonneg (smul s t) := begin intro n, rewrite ↑smul, apply rat.le_by_cases 0 (s (((K₂ s t) * 2) * n)), intro Hsp, apply rat.le_by_cases 0 (t (((K₂ s t) * 2) * n)), intro Htp, apply rat.le_trans, rotate 1, apply rat.mul_nonneg Hsp Htp, rotate_right 1, apply le_of_lt, apply neg_neg_of_pos, apply pnat.inv_pos, intro Htn, apply rat.le_trans, rotate 1, apply mul_le_mul_of_nonpos_right, apply rat.le_trans, apply le_abs_self, apply canon_2_bound_left s t Hs, apply Htn, rotate_right 1, apply rat.le_trans, rotate 1, apply mul_le_mul_of_nonneg_left, apply Hpt, apply le_of_lt, apply rat_of_pnat_is_pos, rotate 1, rewrite -neg_mul_eq_mul_neg, apply neg_le_neg, rewrite [*pnat.mul_assoc, pnat.inv_mul_eq_mul_inv, -mul.assoc, pnat.inv_cancel_left, one_mul], apply inv_ge_of_le, apply pnat.mul_le_mul_left, intro Hsn, apply rat.le_by_cases 0 (t (((K₂ s t) * 2) * n)), intro Htp, apply rat.le_trans, rotate 1, apply mul_le_mul_of_nonpos_left, apply rat.le_trans, apply le_abs_self, apply canon_2_bound_right s t Ht, apply Hsn, rotate_right 1, apply rat.le_trans, rotate 1, apply mul_le_mul_of_nonneg_right, apply Hps, apply le_of_lt, apply rat_of_pnat_is_pos, rotate 1, rewrite -neg_mul_eq_neg_mul, apply neg_le_neg, rewrite [+pnat.mul_assoc, pnat.inv_mul_eq_mul_inv, mul.comm, -mul.assoc, pnat.inv_cancel_left, one_mul], apply inv_ge_of_le, apply pnat.mul_le_mul_left, intro Htn, apply le.trans, rotate 1, apply mul_nonneg_of_nonpos_of_nonpos, apply Hsn, apply Htn, apply le_of_lt, apply neg_neg_of_pos, apply pnat.inv_pos end theorem s_mul_ge_zero_of_ge_zero {s t : seq} (Hs : regular s) (Ht : regular t) (Hzs : s_le zero s) (Hzt : s_le zero t) : s_le zero (smul s t) := begin note Hzs' := s_nonneg_of_ge_zero Hs Hzs, note Htz' := s_nonneg_of_ge_zero Ht Hzt, apply s_ge_zero_of_nonneg, rotate 1, apply s_mul_nonneg_of_nonneg, repeat assumption, apply reg_mul_reg Hs Ht end protected theorem not_lt_self (s : seq) : ¬ s_lt s s := begin intro Hlt, rewrite [↑s_lt at Hlt, ↑pos at Hlt], apply exists.elim Hlt, intro n Hn, esimp at Hn, rewrite [↑sadd at Hn,↑sneg at Hn, -sub_eq_add_neg at Hn, sub_self at Hn], apply absurd Hn (not_lt_of_ge (rat.le_of_lt !pnat.inv_pos)) end theorem not_sep_self (s : seq) : ¬ s ≢ s := begin intro Hsep, rewrite ↑sep at Hsep, let Hsep' := (iff.mp !or_self) Hsep, apply absurd Hsep' (!rat_seq.not_lt_self) end theorem le_well_defined {s t u v : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u) (Hv : regular v) (Hsu : s ≡ u) (Htv : t ≡ v) : s_le s t ↔ s_le u v := iff.intro (begin intro Hle, rewrite [↑s_le at *], apply nonneg_of_nonneg_equiv, rotate 2, apply add_well_defined, rotate 4, apply Htv, apply neg_well_defined, apply Hsu, apply Hle, repeat (apply reg_add_reg | apply reg_neg_reg | assumption) end) (begin intro Hle, rewrite [↑s_le at *], apply nonneg_of_nonneg_equiv, rotate 2, apply add_well_defined, rotate 4, apply equiv.symm, apply Htv, apply neg_well_defined, apply equiv.symm, apply Hsu, apply Hle, repeat (apply reg_add_reg | apply reg_neg_reg | assumption) end) theorem lt_well_defined {s t u v : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u) (Hv : regular v) (Hsu : s ≡ u) (Htv : t ≡ v) : s_lt s t ↔ s_lt u v := iff.intro (begin intro Hle, rewrite [↑s_lt at *], apply pos_of_pos_equiv, rotate 1, apply add_well_defined, rotate 4, apply Htv, apply neg_well_defined, apply Hsu, apply Hle, repeat (apply reg_add_reg | apply reg_neg_reg | assumption) end) (begin intro Hle, rewrite [↑s_lt at *], apply pos_of_pos_equiv, rotate 1, apply add_well_defined, rotate 4, apply equiv.symm, apply Htv, apply neg_well_defined, apply equiv.symm, apply Hsu, apply Hle, repeat (apply reg_add_reg | apply reg_neg_reg | assumption) end) theorem sep_well_defined {s t u v : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u) (Hv : regular v) (Hsu : s ≡ u) (Htv : t ≡ v) : s ≢ t ↔ u ≢ v := begin rewrite ↑sep, apply iff.intro, intro Hor, apply or.elim Hor, intro Hlt, apply or.inl, apply iff.mp (lt_well_defined Hs Ht Hu Hv Hsu Htv), assumption, intro Hlt, apply or.inr, apply iff.mp (lt_well_defined Ht Hs Hv Hu Htv Hsu), assumption, intro Hor, apply or.elim Hor, intro Hlt, apply or.inl, apply iff.mpr (lt_well_defined Hs Ht Hu Hv Hsu Htv), assumption, intro Hlt, apply or.inr, apply iff.mpr (lt_well_defined Ht Hs Hv Hu Htv Hsu), assumption end theorem s_lt_of_lt_of_le {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u) (Hst : s_lt s t) (Htu : s_le t u) : s_lt s u := begin let Rtns := reg_add_reg Ht (reg_neg_reg Hs), let Runt := reg_add_reg Hu (reg_neg_reg Ht), have Hcan : ∀ m, sadd u (sneg s) m = (sadd t (sneg s)) m + (sadd u (sneg t)) m, begin intro m, rewrite [↑sadd, ↑sneg, -*sub_eq_add_neg, -sub_eq_sub_add_sub] end, rewrite [↑s_lt at *, ↑s_le at *], cases bdd_away_of_pos Rtns Hst with [Nt, HNt], cases bdd_within_of_nonneg Runt Htu (2 * Nt) with [Nu, HNu], apply pos_of_bdd_away, existsi max (2 * Nt) Nu, intro n Hn, rewrite Hcan, apply rat.le_trans, rotate 1, apply add_le_add, apply HNt, apply pnat.le_trans, apply pnat.mul_le_mul_left 2, apply pnat.le_trans, rotate 1, apply Hn, rotate_right 1, apply pnat.max_left, apply HNu, apply pnat.le_trans, rotate 1, apply Hn, rotate_right 1, apply pnat.max_right, rewrite [-pnat.add_halves Nt, -sub_eq_add_neg, add_sub_cancel], apply inv_ge_of_le, apply pnat.max_left end theorem s_lt_of_le_of_lt {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u) (Hst : s_le s t) (Htu : s_lt t u) : s_lt s u := begin let Rtns := reg_add_reg Ht (reg_neg_reg Hs), let Runt := reg_add_reg Hu (reg_neg_reg Ht), have Hcan : ∀ m, sadd u (sneg s) m = (sadd t (sneg s)) m + (sadd u (sneg t)) m, begin intro m, rewrite [↑sadd, ↑sneg, -*sub_eq_add_neg, -sub_eq_sub_add_sub] end, rewrite [↑s_lt at *, ↑s_le at *], cases bdd_away_of_pos Runt Htu with [Nu, HNu], cases bdd_within_of_nonneg Rtns Hst (2 * Nu) with [Nt, HNt], apply pos_of_bdd_away, existsi max (2 * Nu) Nt, intro n Hn, rewrite Hcan, apply rat.le_trans, rotate 1, apply add_le_add, apply HNt, apply pnat.le_trans, rotate 1, apply Hn, rotate_right 1, apply pnat.max_right, apply HNu, apply pnat.le_trans, apply pnat.mul_le_mul_left 2, apply pnat.le_trans, rotate 1, apply Hn, rotate_right 1, apply pnat.max_left, rewrite [-pnat.add_halves Nu, neg_add_cancel_left], apply inv_ge_of_le, apply pnat.max_left end theorem le_of_le_reprs {s t : seq} (Hs : regular s) (Ht : regular t) (Hle : ∀ n : ℕ+, s_le s (const (t n))) : s_le s t := by intro m; apply Hle (2 * m) m theorem le_of_reprs_le {s t : seq} (Hs : regular s) (Ht : regular t) (Hle : ∀ n : ℕ+, s_le (const (t n)) s) : s_le t s := by intro m; apply Hle (2 * m) m ----------------------------- -- of_rat theorems theorem const_le_const_of_le {a b : ℚ} (H : a ≤ b) : s_le (const a) (const b) := begin rewrite [↑s_le, ↑nonneg], intro n, rewrite [↑sadd, ↑sneg, ↑const], apply le.trans, apply neg_nonpos_of_nonneg, apply rat.le_of_lt, apply pnat.inv_pos, apply iff.mpr !sub_nonneg_iff_le, apply H end theorem le_of_const_le_const {a b : ℚ} (H : s_le (const a) (const b)) : a ≤ b := begin rewrite [↑s_le at H, ↑nonneg at H, ↑sadd at H, ↑sneg at H, ↑const at H], apply iff.mp !sub_nonneg_iff_le, apply nonneg_of_ge_neg_invs _ H end theorem nat_inv_lt_rat {a : ℚ} (H : a > 0) : ∃ n : ℕ+, n⁻¹ < a := begin existsi (pceil (1 / (a / (2)))), apply lt_of_le_of_lt, rotate 1, apply div_two_lt_of_pos H, rewrite -(one_div_one_div (a / (1 + 1))), apply pceil_helper, apply pnat.le_refl, apply one_div_pos_of_pos, apply div_pos_of_pos_of_pos H dec_trivial end theorem const_lt_const_of_lt {a b : ℚ} (H : a < b) : s_lt (const a) (const b) := begin rewrite [↑s_lt, ↑pos, ↑sadd, ↑sneg, ↑const], apply nat_inv_lt_rat, apply (iff.mpr !sub_pos_iff_lt H) end theorem lt_of_const_lt_const {a b : ℚ} (H : s_lt (const a) (const b)) : a < b := begin rewrite [↑s_lt at H, ↑pos at H, ↑const at H, ↑sadd at H, ↑sneg at H], cases H with [n, Hn], apply (iff.mp !sub_pos_iff_lt), apply lt.trans, rotate 1, exact Hn, apply pnat.inv_pos end theorem s_le_of_le_pointwise {s t : seq} (Hs : regular s) (Ht : regular t) (H : ∀ n : ℕ+, s n ≤ t n) : s_le s t := begin rewrite [↑s_le, ↑nonneg, ↑sadd, ↑sneg], intros, apply le.trans, apply iff.mpr !neg_nonpos_iff_nonneg, apply le_of_lt, apply pnat.inv_pos, apply iff.mpr !sub_nonneg_iff_le, apply H end -------- lift to reg_seqs definition r_lt (s t : reg_seq) := s_lt (reg_seq.sq s) (reg_seq.sq t) definition r_le (s t : reg_seq) := s_le (reg_seq.sq s) (reg_seq.sq t) definition r_sep (s t : reg_seq) := sep (reg_seq.sq s) (reg_seq.sq t) theorem r_le_well_defined (s t u v : reg_seq) (Hsu : requiv s u) (Htv : requiv t v) : r_le s t = r_le u v := propext (le_well_defined (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) (reg_seq.is_reg v) Hsu Htv) theorem r_lt_well_defined (s t u v : reg_seq) (Hsu : requiv s u) (Htv : requiv t v) : r_lt s t = r_lt u v := propext (lt_well_defined (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) (reg_seq.is_reg v) Hsu Htv) theorem r_sep_well_defined (s t u v : reg_seq) (Hsu : requiv s u) (Htv : requiv t v) : r_sep s t = r_sep u v := propext (sep_well_defined (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) (reg_seq.is_reg v) Hsu Htv) theorem r_le.refl (s : reg_seq) : r_le s s := rat_seq.le_refl (reg_seq.is_reg s) theorem r_le.trans {s t u : reg_seq} (Hst : r_le s t) (Htu : r_le t u) : r_le s u := rat_seq.le_trans (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) Hst Htu theorem r_equiv_of_le_of_ge {s t : reg_seq} (Hs : r_le s t) (Hu : r_le t s) : requiv s t := equiv_of_le_of_ge (reg_seq.is_reg s) (reg_seq.is_reg t) Hs Hu theorem r_lt_iff_le_and_sep (s t : reg_seq) : r_lt s t ↔ r_le s t ∧ r_sep s t := lt_iff_le_and_sep (reg_seq.is_reg s) (reg_seq.is_reg t) theorem r_add_le_add_of_le_right {s t : reg_seq} (H : r_le s t) (u : reg_seq) : r_le (u + s) (u + t) := rat_seq.add_le_add_of_le_right (reg_seq.is_reg s) (reg_seq.is_reg t) H (reg_seq.sq u) (reg_seq.is_reg u) theorem r_add_le_add_of_le_right_var (s t u : reg_seq) (H : r_le s t) : r_le (u + s) (u + t) := r_add_le_add_of_le_right H u theorem r_mul_pos_of_pos {s t : reg_seq} (Hs : r_lt r_zero s) (Ht : r_lt r_zero t) : r_lt r_zero (s * t) := s_mul_gt_zero_of_gt_zero (reg_seq.is_reg s) (reg_seq.is_reg t) Hs Ht theorem r_mul_nonneg_of_nonneg {s t : reg_seq} (Hs : r_le r_zero s) (Ht : r_le r_zero t) : r_le r_zero (s * t) := s_mul_ge_zero_of_ge_zero (reg_seq.is_reg s) (reg_seq.is_reg t) Hs Ht theorem r_not_lt_self (s : reg_seq) : ¬ r_lt s s := rat_seq.not_lt_self (reg_seq.sq s) theorem r_not_sep_self (s : reg_seq) : ¬ r_sep s s := not_sep_self (reg_seq.sq s) theorem r_le_of_lt {s t : reg_seq} (H : r_lt s t) : r_le s t := s_le_of_s_lt (reg_seq.is_reg s) (reg_seq.is_reg t) H theorem r_lt_of_le_of_lt {s t u : reg_seq} (Hst : r_le s t) (Htu : r_lt t u) : r_lt s u := s_lt_of_le_of_lt (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) Hst Htu theorem r_lt_of_lt_of_le {s t u : reg_seq} (Hst : r_lt s t) (Htu : r_le t u) : r_lt s u := s_lt_of_lt_of_le (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) Hst Htu theorem r_add_lt_add_left (s t : reg_seq) (H : r_lt s t) (u : reg_seq) : r_lt (u + s) (u + t) := s_add_lt_add_left (reg_seq.is_reg s) (reg_seq.is_reg t) H (reg_seq.is_reg u) theorem r_add_lt_add_left_var (s t u : reg_seq) (H : r_lt s t) : r_lt (u + s) (u + t) := r_add_lt_add_left s t H u theorem r_zero_lt_one : r_lt r_zero r_one := s_zero_lt_one theorem r_le_of_lt_or_eq (s t : reg_seq) (H : r_lt s t ∨ requiv s t) : r_le s t := le_of_lt_or_equiv (reg_seq.is_reg s) (reg_seq.is_reg t) H theorem r_const_le_const_of_le {a b : ℚ} (H : a ≤ b) : r_le (r_const a) (r_const b) := const_le_const_of_le H theorem r_le_of_const_le_const {a b : ℚ} (H : r_le (r_const a) (r_const b)) : a ≤ b := le_of_const_le_const H theorem r_const_lt_const_of_lt {a b : ℚ} (H : a < b) : r_lt (r_const a) (r_const b) := const_lt_const_of_lt H theorem r_lt_of_const_lt_const {a b : ℚ} (H : r_lt (r_const a) (r_const b)) : a < b := lt_of_const_lt_const H theorem r_le_of_le_reprs (s t : reg_seq) (Hle : ∀ n : ℕ+, r_le s (r_const (reg_seq.sq t n))) : r_le s t := le_of_le_reprs (reg_seq.is_reg s) (reg_seq.is_reg t) Hle theorem r_le_of_reprs_le (s t : reg_seq) (Hle : ∀ n : ℕ+, r_le (r_const (reg_seq.sq t n)) s) : r_le t s := le_of_reprs_le (reg_seq.is_reg s) (reg_seq.is_reg t) Hle end rat_seq open real open [class] rat_seq namespace real protected definition lt (x y : ℝ) := quot.lift_on₂ x y (λ a b, rat_seq.r_lt a b) rat_seq.r_lt_well_defined protected definition le (x y : ℝ) := quot.lift_on₂ x y (λ a b, rat_seq.r_le a b) rat_seq.r_le_well_defined definition real_has_lt [reducible] [instance] [priority real.prio] : has_lt ℝ := has_lt.mk real.lt definition real_has_le [reducible] [instance] [priority real.prio] : has_le ℝ := has_le.mk real.le definition sep (x y : ℝ) := quot.lift_on₂ x y (λ a b, rat_seq.r_sep a b) rat_seq.r_sep_well_defined infix `≢` : 50 := sep protected theorem le_refl (x : ℝ) : x ≤ x := quot.induction_on x (λ t, rat_seq.r_le.refl t) protected theorem le_trans {x y z : ℝ} : x ≤ y → y ≤ z → x ≤ z := quot.induction_on₃ x y z (λ s t u, rat_seq.r_le.trans) protected theorem eq_of_le_of_ge {x y : ℝ} : x ≤ y → y ≤ x → x = y := quot.induction_on₂ x y (λ s t Hst Hts, quot.sound (rat_seq.r_equiv_of_le_of_ge Hst Hts)) theorem lt_iff_le_and_sep (x y : ℝ) : x < y ↔ x ≤ y ∧ x ≢ y := quot.induction_on₂ x y (λ s t, rat_seq.r_lt_iff_le_and_sep s t) protected theorem add_le_add_left' (x y z : ℝ) : x ≤ y → z + x ≤ z + y := quot.induction_on₃ x y z (λ s t u, rat_seq.r_add_le_add_of_le_right_var s t u) protected theorem add_le_add_left (x y : ℝ) : x ≤ y → ∀ z : ℝ, z + x ≤ z + y := take H z, real.add_le_add_left' x y z H protected theorem mul_pos (x y : ℝ) : 0 < x → 0 < y → 0 < x * y := quot.induction_on₂ x y (λ s t, rat_seq.r_mul_pos_of_pos) protected theorem mul_nonneg (x y : ℝ) : 0 ≤ x → 0 ≤ y → 0 ≤ x * y := quot.induction_on₂ x y (λ s t, rat_seq.r_mul_nonneg_of_nonneg) theorem not_sep_self (x : ℝ) : ¬ x ≢ x := quot.induction_on x (λ s, rat_seq.r_not_sep_self s) protected theorem lt_irrefl (x : ℝ) : ¬ x < x := quot.induction_on x (λ s, rat_seq.r_not_lt_self s) protected theorem le_of_lt {x y : ℝ} : x < y → x ≤ y := quot.induction_on₂ x y (λ s t H', rat_seq.r_le_of_lt H') protected theorem lt_of_le_of_lt {x y z : ℝ} : x ≤ y → y < z → x < z := quot.induction_on₃ x y z (λ s t u H H', rat_seq.r_lt_of_le_of_lt H H') protected theorem lt_of_lt_of_le {x y z : ℝ} : x < y → y ≤ z → x < z := quot.induction_on₃ x y z (λ s t u H H', rat_seq.r_lt_of_lt_of_le H H') protected theorem add_lt_add_left' (x y z : ℝ) : x < y → z + x < z + y := quot.induction_on₃ x y z (λ s t u, rat_seq.r_add_lt_add_left_var s t u) protected theorem add_lt_add_left (x y : ℝ) : x < y → ∀ z : ℝ, z + x < z + y := take H z, real.add_lt_add_left' x y z H protected theorem zero_lt_one : (0 : ℝ) < (1 : ℝ) := rat_seq.r_zero_lt_one protected theorem le_of_lt_or_eq (x y : ℝ) : x < y ∨ x = y → x ≤ y := (quot.induction_on₂ x y (λ s t H, or.elim H (take H', begin apply rat_seq.r_le_of_lt_or_eq, apply or.inl H' end) (take H', begin apply rat_seq.r_le_of_lt_or_eq, apply (or.inr (quot.exact H')) end))) definition ordered_ring [reducible] [instance] : ordered_ring ℝ := ⦃ ordered_ring, real.comm_ring, le_refl := real.le_refl, le_trans := @real.le_trans, mul_pos := real.mul_pos, mul_nonneg := real.mul_nonneg, zero_ne_one := real.zero_ne_one, add_le_add_left := real.add_le_add_left, le_antisymm := @real.eq_of_le_of_ge, lt_irrefl := real.lt_irrefl, lt_of_le_of_lt := @real.lt_of_le_of_lt, lt_of_lt_of_le := @real.lt_of_lt_of_le, le_of_lt := @real.le_of_lt, add_lt_add_left := real.add_lt_add_left ⦄ open int theorem of_rat_sub (a b : ℚ) : of_rat (a - b) = of_rat a - of_rat b := rfl theorem of_int_sub (a b : ℤ) : of_int (a - b) = of_int a - of_int b := by rewrite [of_int_eq, rat.of_int_sub, of_rat_sub] theorem of_rat_le_of_rat_of_le {a b : ℚ} : a ≤ b → of_rat a ≤ of_rat b := rat_seq.r_const_le_const_of_le theorem le_of_of_rat_le_of_rat {a b : ℚ} : of_rat a ≤ of_rat b → a ≤ b := rat_seq.r_le_of_const_le_const theorem of_rat_le_of_rat_iff (a b : ℚ) : of_rat a ≤ of_rat b ↔ a ≤ b := iff.intro le_of_of_rat_le_of_rat of_rat_le_of_rat_of_le theorem of_rat_lt_of_rat_of_lt {a b : ℚ} : a < b → of_rat a < of_rat b := rat_seq.r_const_lt_const_of_lt theorem lt_of_of_rat_lt_of_rat {a b : ℚ} : of_rat a < of_rat b → a < b := rat_seq.r_lt_of_const_lt_const theorem of_rat_lt_of_rat_iff (a b : ℚ) : of_rat a < of_rat b ↔ a < b := iff.intro lt_of_of_rat_lt_of_rat of_rat_lt_of_rat_of_lt theorem of_int_le_of_int_iff (a b : ℤ) : of_int a ≤ of_int b ↔ (a ≤ b) := begin rewrite [+of_int_eq, of_rat_le_of_rat_iff], apply rat.of_int_le_of_int_iff end theorem of_int_le_of_int_of_le {a b : ℤ} : (a ≤ b) → of_int a ≤ of_int b := iff.mpr !of_int_le_of_int_iff theorem le_of_of_int_le_of_int {a b : ℤ} : of_int a ≤ of_int b → (a ≤ b) := iff.mp !of_int_le_of_int_iff theorem of_int_lt_of_int_iff (a b : ℤ) : of_int a < of_int b ↔ (a < b) := by rewrite [*of_int_eq, of_rat_lt_of_rat_iff]; apply rat.of_int_lt_of_int_iff theorem of_int_lt_of_int_of_lt {a b : ℤ} : (a < b) → of_int a < of_int b := iff.mpr !of_int_lt_of_int_iff theorem lt_of_of_int_lt_of_int {a b : ℤ} : of_int a < of_int b → (a < b) := iff.mp !of_int_lt_of_int_iff theorem of_nat_le_of_nat_iff (a b : ℕ) : of_nat a ≤ of_nat b ↔ (a ≤ b) := by rewrite [*of_nat_eq, of_rat_le_of_rat_iff]; apply rat.of_nat_le_of_nat_iff theorem of_nat_le_of_nat_of_le {a b : ℕ} : (a ≤ b) → of_nat a ≤ of_nat b := iff.mpr !of_nat_le_of_nat_iff theorem le_of_of_nat_le_of_nat {a b : ℕ} : of_nat a ≤ of_nat b → (a ≤ b) := iff.mp !of_nat_le_of_nat_iff theorem of_nat_lt_of_nat_iff (a b : ℕ) : of_nat a < of_nat b ↔ (a < b) := by rewrite [*of_nat_eq, of_rat_lt_of_rat_iff]; apply rat.of_nat_lt_of_nat_iff theorem of_nat_lt_of_nat_of_lt {a b : ℕ} : (a < b) → of_nat a < of_nat b := iff.mpr !of_nat_lt_of_nat_iff theorem lt_of_of_nat_lt_of_nat {a b : ℕ} : of_nat a < of_nat b → (a < b) := iff.mp !of_nat_lt_of_nat_iff theorem of_rat_pos_of_pos {q : ℚ} (Hq : q > 0) : of_rat q > 0 := of_rat_lt_of_rat_of_lt Hq theorem of_rat_nonneg_of_nonneg {q : ℚ} (Hq : q ≥ 0) : of_rat q ≥ 0 := of_rat_le_of_rat_of_le Hq theorem of_rat_neg_of_neg {q : ℚ} (Hq : q < 0) : of_rat q < 0 := of_rat_lt_of_rat_of_lt Hq theorem of_rat_nonpos_of_nonpos {q : ℚ} (Hq : q ≤ 0) : of_rat q ≤ 0 := of_rat_le_of_rat_of_le Hq theorem of_nat_nonneg (a : ℕ) : of_nat a ≥ 0 := of_rat_le_of_rat_of_le !rat.of_nat_nonneg theorem of_nat_succ_pos (k : ℕ) : 0 < of_nat k + 1 := add_pos_of_nonneg_of_pos (of_nat_nonneg k) real.zero_lt_one theorem of_rat_pow (a : ℚ) (n : ℕ) : of_rat (a^n) = (of_rat a)^n := begin induction n with n ih, apply eq.refl, rewrite [2 pow_succ, of_rat_mul, ih] end theorem of_int_pow (a : ℤ) (n : ℕ) : of_int (#int a^n) = (of_int a)^n := by rewrite [of_int_eq, rat.of_int_pow, of_rat_pow] theorem of_nat_pow (a : ℕ) (n : ℕ) : of_nat (#nat a^n) = (of_nat a)^n := by rewrite [of_nat_eq, rat.of_nat_pow, of_rat_pow] open rat_seq theorem le_of_le_reprs (x : ℝ) (t : seq) (Ht : regular t) : (∀ n : ℕ+, x ≤ t n) → x ≤ quot.mk (reg_seq.mk t Ht) := quot.induction_on x (take s Hs, show r_le s (reg_seq.mk t Ht), from have H' : ∀ n : ℕ+, r_le s (r_const (t n)), from Hs, by apply r_le_of_le_reprs; apply Hs) theorem le_of_reprs_le (x : ℝ) (t : seq) (Ht : regular t) : (∀ n : ℕ+, t n ≤ x) → x ≥ ((quot.mk (reg_seq.mk t Ht)) : ℝ) := quot.induction_on x (take s Hs, show r_le (reg_seq.mk t Ht) s, from have H' : ∀ n : ℕ+, r_le (r_const (t n)) s, from Hs, by apply r_le_of_reprs_le; apply Hs) end real
007382cd553501f93ffae61389f8af47bc977e1d
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/eval_constant.lean
f5dd5f289e3bb9b7efbb691e79455640e2447897
[ "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
244
lean
open tactic run_cmd do e ← to_expr ``(nat.add), fn ← eval_expr (nat → nat → nat) e, trace (fn 10 20) run_cmd do e ← to_expr ``(λ x y : nat, x + x + y), fn ← eval_expr (nat → nat → nat) e, trace (fn 10 20)
07e58140197e844f0a9d9e6f86151b911a8e4cc9
3dd1b66af77106badae6edb1c4dea91a146ead30
/tests/lean/run/group2.lean
5d1b2b6f23dd6775d1a4a7b20590dce9be1b38ec
[ "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
1,725
lean
import standard using num section parameter {A : Type} parameter f : A → A → A parameter one : A parameter inv : A → A infixl `*`:75 := f postfix `^-1`:100 := inv definition is_assoc := ∀ a b c, (a*b)*c = a*b*c definition is_id := ∀ a, a*one = a definition is_inv := ∀ a, a*a^-1 = one end inductive group_struct (A : Type) : Type := | mk_group_struct : Π (mul : A → A → A) (one : A) (inv : A → A), is_assoc mul → is_id mul one → is_inv mul one inv → group_struct A inductive group : Type := | mk_group : Π (A : Type), group_struct A → group definition carrier (g : group) : Type := group_rec (λ c s, c) g coercion carrier definition group_to_struct [instance] (g : group) : group_struct (carrier g) := group_rec (λ (A : Type) (s : group_struct A), s) g check group_struct definition mul [inline] {A : Type} {s : group_struct A} (a b : A) : A := group_struct_rec (λ mul one inv h1 h2 h3, mul) s a b infixl `*`:75 := mul section variable G1 : group variable G2 : group variables a b c : G2 variables d e : G1 check a * b * b check d * e end variable G : group.{1} variables a b : G definition val : G := a*b check val variable pos_real : Type.{1} variable rmul : pos_real → pos_real → pos_real variable rone : pos_real variable rinv : pos_real → pos_real axiom H1 : is_assoc rmul axiom H2 : is_id rmul rone axiom H3 : is_inv rmul rone rinv definition real_group_struct [inline] [instance] : group_struct pos_real := mk_group_struct rmul rone rinv H1 H2 H3 variables x y : pos_real check x * y set_option pp.implicit true print "---------------" theorem T (a b : pos_real): (rmul a b) = a*b := refl (rmul a b)
e246f9faa31cdc189d77168b01d7f1cd943c2bd4
36c7a18fd72e5b57229bd8ba36493daf536a19ce
/library/theories/group_theory/finsubg.lean
67dff7079268eb7d8c041c39bb6e401b542e6271
[ "Apache-2.0" ]
permissive
YHVHvx/lean
732bf0fb7a298cd7fe0f15d82f8e248c11db49e9
038369533e0136dd395dc252084d3c1853accbf2
refs/heads/master
1,610,701,080,210
1,449,128,595,000
1,449,128,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
20,694
lean
/- Copyright (c) 2015 Haitao Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Haitao Zhang -/ -- develop the concept of finite subgroups based on finsets so that the properties -- can be used directly without translating from the set based theory first import data algebra.group .subgroup open function algebra finset -- ⁻¹ in eq.ops conflicts with group ⁻¹ open eq.ops namespace group open ops section subg -- we should be able to prove properties using finsets directly variable {G : Type} variable [ambientG : group G] include ambientG definition finset_mul_closed_on [reducible] (H : finset G) : Prop := ∀ x y : G, x ∈ H → y ∈ H → x * y ∈ H definition finset_has_inv (H : finset G) : Prop := ∀ a : G, a ∈ H → a⁻¹ ∈ H structure is_finsubg [class] (H : finset G) : Type := (has_one : 1 ∈ H) (mul_closed : finset_mul_closed_on H) (has_inv : finset_has_inv H) definition univ_is_finsubg [instance] [finG : fintype G] : is_finsubg (@finset.univ G _) := is_finsubg.mk !mem_univ (λ x y Px Py, !mem_univ) (λ a Pa, !mem_univ) definition one_is_finsubg [instance] : is_finsubg (singleton (1:G)) := is_finsubg.mk !mem_singleton (λ x y Px Py, by rewrite [eq_of_mem_singleton Px, eq_of_mem_singleton Py, one_mul]; apply mem_singleton) (λ x Px, by rewrite [eq_of_mem_singleton Px, one_inv]; apply mem_singleton) lemma finsubg_has_one (H : finset G) [h : is_finsubg H] : 1 ∈ H := @is_finsubg.has_one G _ H h lemma finsubg_mul_closed (H : finset G) [h : is_finsubg H] {x y : G} : x ∈ H → y ∈ H → x * y ∈ H := @is_finsubg.mul_closed G _ H h x y lemma finsubg_has_inv (H : finset G) [h : is_finsubg H] {a : G} : a ∈ H → a⁻¹ ∈ H := @is_finsubg.has_inv G _ H h a variable [deceqG : decidable_eq G] include deceqG definition finsubg_to_subg [instance] {H : finset G} [h : is_finsubg H] : is_subgroup (ts H) := is_subgroup.mk (mem_eq_mem_to_set H 1 ▸ finsubg_has_one H) (take x y, begin repeat rewrite -mem_eq_mem_to_set, apply finsubg_mul_closed H end) (take a, begin repeat rewrite -mem_eq_mem_to_set, apply finsubg_has_inv H end) open nat lemma finsubg_eq_singleton_one_of_card_one {H : finset G} [h : is_finsubg H] : card H = 1 → H = singleton 1 := assume Pcard, eq.symm (eq_of_card_eq_of_subset (by rewrite [Pcard]) (subset_of_forall take g, by rewrite [mem_singleton_eq]; intro Pg; rewrite Pg; exact finsubg_has_one H)) end subg section fin_lcoset open set variable {A : Type} variable [deceq : decidable_eq A] include deceq variable [s : group A] include s definition fin_lcoset (H : finset A) (a : A) := finset.image (lmul_by a) H definition fin_rcoset (H : finset A) (a : A) : finset A := image (rmul_by a) H definition fin_lcosets (H G : finset A) := image (fin_lcoset H) G definition fin_inv : finset A → finset A := image inv variable {H : finset A} lemma lmul_rmul {a b : A} : (lmul_by a) ∘ (rmul_by b) = (rmul_by b) ∘ (lmul_by a) := funext take c, calc a*(c*b) = (a*c)*b : mul.assoc lemma fin_lrcoset_comm {a b : A} : fin_lcoset (fin_rcoset H b) a = fin_rcoset (fin_lcoset H a) b := by esimp [fin_lcoset, fin_rcoset]; rewrite [-*image_compose, lmul_rmul] lemma inv_mem_fin_inv {a : A} : a ∈ H → a⁻¹ ∈ fin_inv H := assume Pin, mem_image Pin rfl lemma fin_lcoset_eq (a : A) : ts (fin_lcoset H a) = a ∘> (ts H) := calc ts (fin_lcoset H a) = coset.l a (ts H) : to_set_image ... = a ∘> (ts H) : glcoset_eq_lcoset lemma fin_lcoset_id : fin_lcoset H 1 = H := by rewrite [eq_eq_to_set_eq, fin_lcoset_eq, glcoset_id] lemma fin_lcoset_compose (a b : A) : fin_lcoset (fin_lcoset H b) a = fin_lcoset H (a*b) := to_set.inj (by rewrite [*fin_lcoset_eq, glcoset_compose]) lemma fin_lcoset_inv (a : A) : fin_lcoset (fin_lcoset H a) a⁻¹ = H := to_set.inj (by rewrite [*fin_lcoset_eq, glcoset_inv]) lemma fin_lcoset_card (a : A) : card (fin_lcoset H a) = card H := card_image_eq_of_inj_on (lmul_inj_on a (ts H)) lemma fin_lcosets_card_eq {G : finset A} : ∀ gH, gH ∈ fin_lcosets H G → card gH = card H := take gH, assume Pcosets, obtain g Pg, from exists_of_mem_image Pcosets, and.right Pg ▸ fin_lcoset_card g variable [is_finsubgH : is_finsubg H] include is_finsubgH lemma fin_lcoset_same (x a : A) : x ∈ (fin_lcoset H a) = (fin_lcoset H x = fin_lcoset H a) := begin rewrite mem_eq_mem_to_set, rewrite [eq_eq_to_set_eq, *(fin_lcoset_eq x), fin_lcoset_eq a], exact (subg_lcoset_same x a) end lemma fin_mem_lcoset (g : A) : g ∈ fin_lcoset H g := have P : g ∈ g ∘> ts H, from and.left (subg_in_coset_refl g), assert P1 : g ∈ ts (fin_lcoset H g), from eq.symm (fin_lcoset_eq g) ▸ P, eq.symm (mem_eq_mem_to_set _ g) ▸ P1 lemma fin_lcoset_subset {S : finset A} (Psub : S ⊆ H) : ∀ x, x ∈ H → fin_lcoset S x ⊆ H := assert Psubs : set.subset (ts S) (ts H), from subset_eq_to_set_subset S H ▸ Psub, take x, assume Pxs : x ∈ ts H, assert Pcoset : set.subset (x ∘> ts S) (ts H), from subg_lcoset_subset_subg Psubs x Pxs, by rewrite [subset_eq_to_set_subset, fin_lcoset_eq x]; exact Pcoset lemma finsubg_lcoset_id {a : A} : a ∈ H → fin_lcoset H a = H := by rewrite [eq_eq_to_set_eq, fin_lcoset_eq, mem_eq_mem_to_set]; apply subgroup_lcoset_id lemma finsubg_inv_lcoset_eq_rcoset {a : A} : fin_inv (fin_lcoset H a) = fin_rcoset H a⁻¹ := begin esimp [fin_inv, fin_lcoset, fin_rcoset], rewrite [-image_compose], apply ext, intro b, rewrite [*mem_image_iff, ↑compose, ↑lmul_by, ↑rmul_by], apply iff.intro, intro Pl, cases Pl with h Ph, cases Ph with Pin Peq, existsi h⁻¹, apply and.intro, exact finsubg_has_inv H Pin, rewrite [-mul_inv, Peq], intro Pr, cases Pr with h Ph, cases Ph with Pin Peq, existsi h⁻¹, apply and.intro, exact finsubg_has_inv H Pin, rewrite [mul_inv, inv_inv, Peq], end lemma finsubg_conj_closed {g h : A} : g ∈ H → h ∈ H → g ∘c h ∈ H := assume Pgin Phin, finsubg_mul_closed H (finsubg_mul_closed H Pgin Phin) (finsubg_has_inv H Pgin) variable {G : finset A} variable [is_finsubgG : is_finsubg G] include is_finsubgG open finset.partition definition fin_lcoset_partition_subg (Psub : H ⊆ G) := partition.mk G (fin_lcoset H) fin_lcoset_same (restriction_imp_union (fin_lcoset H) fin_lcoset_same (fin_lcoset_subset Psub)) open nat theorem lagrange_theorem (Psub : H ⊆ G) : card G = card (fin_lcosets H G) * card H := calc card G = nat.finset.Sum (fin_lcosets H G) card : class_equation (fin_lcoset_partition_subg Psub) ... = nat.finset.Sum (fin_lcosets H G) (λ x, card H) : nat.finset.Sum_ext (take g P, fin_lcosets_card_eq g P) ... = card (fin_lcosets H G) * card H : Sum_const_eq_card_mul end fin_lcoset section open fintype list subtype lemma dinj_tag {A : Type} (P : A → Prop) : dinj P tag := take a₁ a₂ Pa₁ Pa₂ Pteq, subtype.no_confusion Pteq (λ Pe Pqe, Pe) open nat lemma card_pos {A : Type} [ambientA : group A] [finA : fintype A] : 0 < card A := length_pos_of_mem (mem_univ 1) end section lcoset_fintype open fintype list subtype variables {A : Type} [ambientA : group A] [finA : fintype A] [deceqA : decidable_eq A] include ambientA deceqA finA variables G H : finset A definition is_fin_lcoset [reducible] (S : finset A) : Prop := ∃ g, g ∈ G ∧ fin_lcoset H g = S definition to_list : list A := list.filter (λ g, g ∈ G) (elems A) definition list_lcosets : list (finset A) := erase_dup (map (fin_lcoset H) (to_list G)) definition lcoset_type [reducible] : Type := {S : finset A | is_fin_lcoset G H S} definition all_lcosets : list (lcoset_type G H) := dmap (is_fin_lcoset G H) tag (list_lcosets G H) variables {G H} [finsubgG : is_finsubg G] include finsubgG lemma self_is_lcoset : is_fin_lcoset G H H := exists.intro 1 (and.intro !finsubg_has_one fin_lcoset_id) lemma lcoset_subset_of_subset (J : lcoset_type G H) : H ⊆ G → elt_of J ⊆ G := assume Psub, obtain j Pjin Pj, from has_property J, by rewrite [-Pj]; apply fin_lcoset_subset Psub; exact Pjin variables (G H) definition lcoset_one : lcoset_type G H := tag H self_is_lcoset variables {G H} definition lcoset_lmul {g : A} (Pgin : g ∈ G) (S : lcoset_type G H) : lcoset_type G H := tag (fin_lcoset (elt_of S) g) (obtain f Pfin Pf, from has_property S, exists.intro (g*f) (by apply and.intro; exact finsubg_mul_closed G Pgin Pfin; rewrite [-Pf, -fin_lcoset_compose])) definition lcoset_mul (S₁ S₂ : lcoset_type G H): finset A := Union (elt_of S₁) (fin_lcoset (elt_of S₂)) lemma mul_mem_lcoset_mul (J K : lcoset_type G H) {g h} : g ∈ elt_of J → h ∈ elt_of K → g*h ∈ lcoset_mul J K := assume Pg, begin rewrite [↑lcoset_mul, mem_Union_iff, ↑fin_lcoset], intro Ph, existsi g, apply and.intro, exact Pg, rewrite [mem_image_iff, ↑lmul_by], existsi h, exact and.intro Ph rfl end lemma is_lcoset_of_mem_list_lcosets {S : finset A} : S ∈ list_lcosets G H → is_fin_lcoset G H S := assume Pin, obtain g Pgin Pg, from exists_of_mem_map (mem_of_mem_erase_dup Pin), exists.intro g (and.intro (of_mem_filter Pgin) Pg) lemma mem_list_lcosets_of_is_lcoset {S : finset A} : is_fin_lcoset G H S → S ∈ list_lcosets G H := assume Plcoset, obtain g Pgin Pg, from Plcoset, Pg ▸ mem_erase_dup (mem_map _ (mem_filter_of_mem (complete g) Pgin)) lemma fin_lcosets_eq : fin_lcosets H G = to_finset_of_nodup (list_lcosets G H) !nodup_erase_dup := ext (take S, iff.intro (λ Pimg, mem_list_lcosets_of_is_lcoset (exists_of_mem_image Pimg)) (λ Pl, obtain g Pg, from is_lcoset_of_mem_list_lcosets Pl, iff.elim_right !mem_image_iff (is_lcoset_of_mem_list_lcosets Pl))) lemma length_all_lcosets : length (all_lcosets G H) = card (fin_lcosets H G) := eq.trans (show length (all_lcosets G H) = length (list_lcosets G H), from assert Pmap : map elt_of (all_lcosets G H) = list_lcosets G H, from map_dmap_of_inv_of_pos (λ S P, rfl) (λ S, is_lcoset_of_mem_list_lcosets), by rewrite[-Pmap, length_map]) (by rewrite fin_lcosets_eq) lemma lcoset_lmul_compose {f g : A} (Pf : f ∈ G) (Pg : g ∈ G) (S : lcoset_type G H) : lcoset_lmul Pf (lcoset_lmul Pg S) = lcoset_lmul (finsubg_mul_closed G Pf Pg) S := subtype.eq !fin_lcoset_compose lemma lcoset_lmul_one (S : lcoset_type G H) : lcoset_lmul !finsubg_has_one S = S := subtype.eq fin_lcoset_id lemma lcoset_lmul_inv {g : A} {Pg : g ∈ G} (S : lcoset_type G H) : lcoset_lmul (finsubg_has_inv G Pg) (lcoset_lmul Pg S) = S := subtype.eq (to_set.inj begin esimp [lcoset_lmul], rewrite [fin_lcoset_compose, mul.left_inv, fin_lcoset_eq, glcoset_id] end) lemma lcoset_lmul_inj {g : A} {Pg : g ∈ G}: @injective (lcoset_type G H) _ (lcoset_lmul Pg) := injective_of_has_left_inverse (exists.intro (lcoset_lmul (finsubg_has_inv G Pg)) lcoset_lmul_inv) lemma card_elt_of_lcoset_type (S : lcoset_type G H) : card (elt_of S) = card H := obtain f Pfin Pf, from has_property S, Pf ▸ fin_lcoset_card f definition lcoset_fintype [instance] : fintype (lcoset_type G H) := fintype.mk (all_lcosets G H) (dmap_nodup_of_dinj (dinj_tag (is_fin_lcoset G H)) !nodup_erase_dup) (take s, subtype.destruct s (take S, assume PS, mem_dmap PS (mem_list_lcosets_of_is_lcoset PS))) lemma card_lcoset_type : card (lcoset_type G H) = card (fin_lcosets H G) := length_all_lcosets open nat nat.finset variable [finsubgH : is_finsubg H] include finsubgH theorem lagrange_theorem' (Psub : H ⊆ G) : card G = card (lcoset_type G H) * card H := calc card G = card (fin_lcosets H G) * card H : lagrange_theorem Psub ... = card (lcoset_type G H) * card H : card_lcoset_type lemma lcoset_disjoint {S₁ S₂ : lcoset_type G H} : S₁ ≠ S₂ → elt_of S₁ ∩ elt_of S₂ = ∅ := obtain f₁ Pfin₁ Pf₁, from has_property S₁, obtain f₂ Pfin₂ Pf₂, from has_property S₂, assume Pne, inter_eq_empty_of_disjoint (disjoint.intro take g, begin rewrite [-Pf₁, -Pf₂, *fin_lcoset_same], intro Pgf₁, rewrite [Pgf₁, Pf₁, Pf₂], intro Peq, exact absurd (subtype.eq Peq) Pne end ) lemma card_Union_lcosets (lcs : finset (lcoset_type G H)) : card (Union lcs elt_of) = card lcs * card H := calc card (Union lcs elt_of) = ∑ lc ∈ lcs, card (elt_of lc) : card_Union_of_disjoint lcs elt_of (λ (S₁ S₂ : lcoset_type G H) P₁ P₂ Pne, lcoset_disjoint Pne) ... = ∑ lc ∈ lcs, card H : Sum_ext (take lc P, card_elt_of_lcoset_type _) ... = card lcs * card H : Sum_const_eq_card_mul lemma exists_of_lcoset_type (J : lcoset_type G H) : ∃ j, j ∈ elt_of J ∧ fin_lcoset H j = elt_of J := obtain j Pjin Pj, from has_property J, exists.intro j (and.intro (Pj ▸ !fin_mem_lcoset) Pj) lemma lcoset_not_empty (J : lcoset_type G H) : elt_of J ≠ ∅ := obtain j Pjin Pj, from has_property J, assume Pempty, absurd (by rewrite [-Pempty, -Pj]; apply fin_mem_lcoset) (not_mem_empty j) end lcoset_fintype section normalizer open subtype variables {G : Type} [ambientG : group G] [finG : fintype G] [deceqG : decidable_eq G] include ambientG deceqG finG variable H : finset G definition normalizer : finset G := {g ∈ univ | ∀ h, h ∈ H → g ∘c h ∈ H} variable {H} variable [finsubgH : is_finsubg H] include finsubgH lemma subset_normalizer : H ⊆ normalizer H := subset_of_forall take g, assume PginH, mem_sep_of_mem !mem_univ (take h, assume PhinH, finsubg_conj_closed PginH PhinH) lemma normalizer_has_one : 1 ∈ normalizer H := mem_of_subset_of_mem subset_normalizer (finsubg_has_one H) lemma normalizer_mul_closed : finset_mul_closed_on (normalizer H) := take f g, assume Pfin Pgin, mem_sep_of_mem !mem_univ take h, assume Phin, begin rewrite [-conj_compose], apply of_mem_sep Pfin, apply of_mem_sep Pgin, exact Phin end lemma conj_eq_of_mem_normalizer {g : G} : g ∈ normalizer H → image (conj_by g) H = H := assume Pgin, eq_of_card_eq_of_subset (card_image_eq_of_inj_on (take h j, assume P1 P2, !conj_inj)) (subset_of_forall take h, assume Phin, obtain j Pjin Pj, from exists_of_mem_image Phin, begin substvars, apply of_mem_sep Pgin, exact Pjin end) lemma normalizer_has_inv : finset_has_inv (normalizer H) := take g, assume Pgin, mem_sep_of_mem !mem_univ take h, begin rewrite [-(conj_eq_of_mem_normalizer Pgin) at {1}, mem_image_iff], intro Pex, cases Pex with k Pk, rewrite [-(and.right Pk), conj_compose, mul.left_inv, conj_id], exact and.left Pk end definition normalizer_is_finsubg [instance] : is_finsubg (normalizer H) := is_finsubg.mk normalizer_has_one normalizer_mul_closed normalizer_has_inv lemma lcoset_subset_normalizer (J : lcoset_type (normalizer H) H) : elt_of J ⊆ normalizer H := lcoset_subset_of_subset J subset_normalizer lemma lcoset_subset_normalizer_of_mem {g : G} : g ∈ normalizer H → fin_lcoset H g ⊆ normalizer H := assume Pgin, fin_lcoset_subset subset_normalizer g Pgin lemma lrcoset_same_of_mem_normalizer {g : G} : g ∈ normalizer H → fin_lcoset H g = fin_rcoset H g := assume Pg, ext take h, iff.intro (assume Pl, obtain j Pjin Pj, from exists_of_mem_image Pl, mem_image (of_mem_sep Pg j Pjin) (calc g*j*g⁻¹*g = g*j : inv_mul_cancel_right ... = h : Pj)) (assume Pr, obtain j Pjin Pj, from exists_of_mem_image Pr, mem_image (of_mem_sep (finsubg_has_inv (normalizer H) Pg) j Pjin) (calc g*(g⁻¹*j*g⁻¹⁻¹) = g*(g⁻¹*j*g) : inv_inv ... = g*(g⁻¹*(j*g)) : mul.assoc ... = j*g : mul_inv_cancel_left ... = h : Pj)) lemma lcoset_mul_eq_lcoset (J K : lcoset_type (normalizer H) H) {g : G} : g ∈ elt_of J → (lcoset_mul J K) = fin_lcoset (elt_of K) g := assume Pgin, obtain j Pjin Pj, from has_property J, obtain k Pkin Pk, from has_property K, Union_const (lcoset_not_empty J) begin rewrite [-Pk], intro h Phin, assert Phinn : h ∈ normalizer H, apply mem_of_subset_of_mem (lcoset_subset_normalizer_of_mem Pjin), rewrite Pj, assumption, revert Phin Pgin, rewrite [-Pj, *fin_lcoset_same], intro Pheq Pgeq, rewrite [*(lrcoset_same_of_mem_normalizer Pkin), *fin_lrcoset_comm, Pheq, Pgeq] end lemma lcoset_mul_is_lcoset (J K : lcoset_type (normalizer H) H) : is_fin_lcoset (normalizer H) H (lcoset_mul J K) := obtain j Pjin Pj, from has_property J, obtain k Pkin Pk, from has_property K, exists.intro (j*k) (and.intro (finsubg_mul_closed _ Pjin Pkin) begin rewrite [lcoset_mul_eq_lcoset J K (Pj ▸ fin_mem_lcoset j), -fin_lcoset_compose, Pk] end) lemma lcoset_inv_is_lcoset (J : lcoset_type (normalizer H) H) : is_fin_lcoset (normalizer H) H (fin_inv (elt_of J)) := obtain j Pjin Pj, from has_property J, exists.intro j⁻¹ begin rewrite [-Pj, finsubg_inv_lcoset_eq_rcoset], apply and.intro, apply normalizer_has_inv, assumption, apply lrcoset_same_of_mem_normalizer, apply normalizer_has_inv, assumption end definition fin_coset_mul (J K : lcoset_type (normalizer H) H) : lcoset_type (normalizer H) H := tag (lcoset_mul J K) (lcoset_mul_is_lcoset J K) definition fin_coset_inv (J : lcoset_type (normalizer H) H) : lcoset_type (normalizer H) H := tag (fin_inv (elt_of J)) (lcoset_inv_is_lcoset J) definition fin_coset_one : lcoset_type (normalizer H) H := tag H self_is_lcoset local infix `^` := fin_coset_mul lemma fin_coset_mul_eq_lcoset (J K : lcoset_type (normalizer H) H) {g : G} : g ∈ (elt_of J) → elt_of (J ^ K) = fin_lcoset (elt_of K) g := assume Pgin, lcoset_mul_eq_lcoset J K Pgin lemma fin_coset_mul_assoc (J K L : lcoset_type (normalizer H) H) : J ^ K ^ L = J ^ (K ^ L) := obtain j Pjin Pj, from exists_of_lcoset_type J, obtain k Pkin Pk, from exists_of_lcoset_type K, assert Pjk : j*k ∈ elt_of (J ^ K), from mul_mem_lcoset_mul J K Pjin Pkin, obtain l Plin Pl, from has_property L, subtype.eq (begin rewrite [fin_coset_mul_eq_lcoset (J ^ K) _ Pjk, fin_coset_mul_eq_lcoset J _ Pjin, fin_coset_mul_eq_lcoset K _ Pkin, -Pl, *fin_lcoset_compose] end) lemma fin_coset_mul_one (J : lcoset_type (normalizer H) H) : J ^ fin_coset_one = J := obtain j Pjin Pj, from exists_of_lcoset_type J, subtype.eq begin rewrite [↑fin_coset_one, fin_coset_mul_eq_lcoset _ _ Pjin, -Pj] end lemma fin_coset_one_mul (J : lcoset_type (normalizer H) H) : fin_coset_one ^ J = J := subtype.eq begin rewrite [↑fin_coset_one, fin_coset_mul_eq_lcoset _ _ (finsubg_has_one H), fin_lcoset_id] end lemma fin_coset_left_inv (J : lcoset_type (normalizer H) H) : (fin_coset_inv J) ^ J = fin_coset_one := obtain j Pjin Pj, from exists_of_lcoset_type J, assert Pjinv : j⁻¹ ∈ elt_of (fin_coset_inv J), from inv_mem_fin_inv Pjin, subtype.eq begin rewrite [↑fin_coset_one, fin_coset_mul_eq_lcoset _ _ Pjinv, -Pj, fin_lcoset_inv] end variable (H) definition fin_coset_group [instance] : group (lcoset_type (normalizer H) H) := group.mk fin_coset_mul fin_coset_mul_assoc fin_coset_one fin_coset_one_mul fin_coset_mul_one fin_coset_inv fin_coset_left_inv variables {H} (Hc : finset (lcoset_type (normalizer H) H)) definition fin_coset_Union : finset G := Union Hc elt_of variables {Hc} [finsubgHc : is_finsubg Hc] include finsubgHc lemma mem_normalizer_of_mem_fcU {j : G} : j ∈ fin_coset_Union Hc → j ∈ normalizer H := assume Pjin, obtain J PJ PjJ, from iff.elim_left !mem_Union_iff Pjin, mem_of_subset_of_mem !lcoset_subset_normalizer PjJ lemma fcU_has_one : (1:G) ∈ fin_coset_Union Hc := iff.elim_right (mem_Union_iff Hc elt_of (1:G)) (exists.intro 1 (and.intro (finsubg_has_one Hc) (finsubg_has_one H))) lemma fcU_has_inv : finset_has_inv (fin_coset_Union Hc) := take j, assume Pjin, obtain J PJ PjJ, from iff.elim_left !mem_Union_iff Pjin, have PJinv : J⁻¹ ∈ Hc, from finsubg_has_inv Hc PJ, have Pjinv : j⁻¹ ∈ elt_of J⁻¹, from inv_mem_fin_inv PjJ, iff.elim_right !mem_Union_iff (exists.intro J⁻¹ (and.intro PJinv Pjinv)) lemma fcU_mul_closed : finset_mul_closed_on (fin_coset_Union Hc) := take j k, assume Pjin Pkin, obtain J PJ PjJ, from iff.elim_left !mem_Union_iff Pjin, obtain K PK PkK, from iff.elim_left !mem_Union_iff Pkin, assert Pjk : j*k ∈ elt_of (J*K), from mul_mem_lcoset_mul J K PjJ PkK, iff.elim_right !mem_Union_iff (exists.intro (J*K) (and.intro (finsubg_mul_closed Hc PJ PK) Pjk)) definition fcU_is_finsubg [instance] : is_finsubg (fin_coset_Union Hc) := is_finsubg.mk fcU_has_one fcU_mul_closed fcU_has_inv end normalizer end group
d7aab8964464cccf6342abd06fdb7eba40a4b6a9
7c216aab58e7d519f010545575b6a843da4aa0ac
/metamath/lean/main.lean
e1b2bdec5d3a90656de7306d76c319f7ac0359aa
[]
no_license
digama0/twitchcoq
82a301baf97eb5e0ec855ee7f45d5b2735c12601
2f3d63289b93456040491e8b50140a49f34d8b61
refs/heads/master
1,606,509,387,393
1,577,030,121,000
1,577,030,121,000
229,592,726
2
0
null
1,577,030,158,000
1,577,030,157,000
null
UTF-8
Lean
false
false
3,542
lean
import system.io import init.data.string.ops inductive stmt : Type | constant_stmt : list string -> stmt | variable_stmt : list string -> stmt | disjoint_stmt : list string -> stmt | floating_stmt : string -> string -> string -> stmt | essential_stmt : string -> string -> (list string) -> stmt | axiom_stmt : string -> string -> (list string) -> stmt | provable_stmt : string -> string -> (list string) -> (list string) -> stmt | scope_stmt : (list stmt) -> stmt | parse_error : (list string) -> stmt def stmt_to_string : stmt -> string | (stmt.constant_stmt l) := "constant_stmt: " ++ (list.to_string l) | (stmt.variable_stmt l) := "variable_stmt: " ++ (list.to_string l) | (stmt.disjoint_stmt l) := "disjoint_stmt: " ++ (list.to_string l) | (stmt.floating_stmt name typecode var) := "floating_stmt: " ++ name ++ " " ++ typecode ++ " " ++ var | (stmt.essential_stmt name typecode ms) := "essential_stmt: " ++ name ++ " " ++ typecode ++ " " ++ (list.to_string ms) | (stmt.axiom_stmt name typecode ms) := "axiom_stmt: " ++ name ++ " " ++ typecode ++ " " ++ (list.to_string ms) | (stmt.provable_stmt name typecode ms proof) := "provable_stmt: " ++ name ++ " " ++ typecode ++ " " ++ (list.to_string ms) ++ " " ++ (list.to_string proof) | (stmt.scope_stmt (a::lst)) := (stmt_to_string a) ++ "\n" ++ (stmt_to_string (stmt.scope_stmt lst)) | (stmt.scope_stmt []) := "}" | (stmt.parse_error lst) := "PARSE ERROR: " ++ (lst.take 10).to_string instance : has_to_string stmt := ⟨stmt_to_string⟩ def consume_until (s : string) : list string -> (list string) × (list string) | [] := ([], []) | (a :: l) := if a ≠ s then let (aa, bb) := consume_until l in (a :: aa, bb) else ([], l) def parser_core : nat -> list string -> list stmt | 0 := λ x, [(stmt.parse_error x)] | (n+1) := let pc := (parser_core n) in λ x, match x with | ("$c" :: l) := let (x, rest) := consume_until "$." l in (stmt.constant_stmt x) :: (pc rest) | ("$v" :: l) := let (x, rest) := consume_until "$." l in (stmt.variable_stmt x) :: (pc rest) | ("$d" :: l) := let (x, rest) := consume_until "$." l in (stmt.disjoint_stmt x) :: (pc rest) | (name :: "$f" :: typecode :: var :: "$." :: l) := (stmt.floating_stmt name typecode var) :: (pc l) | (name :: "$e" :: typecode :: l) := let (x, rest) := consume_until "$." l in (stmt.essential_stmt name typecode x) :: (pc rest) | (name :: "$a" :: typecode :: l) := let (x, rest) := consume_until "$." l in (stmt.axiom_stmt name typecode x) :: (pc rest) | (name :: "$p" :: typecode :: l) := let (x, rest) := consume_until "$=" l in let (y, rest2) := consume_until "$." rest in (stmt.provable_stmt name typecode x y) :: (pc rest2) | ("${" :: l) := let (x, rest) := consume_until "$}" l in (stmt.scope_stmt ((parser_core n) x)) :: (pc rest) -- comments go nowhere | ("$(" :: l) := let (x, rest) := consume_until "$)" l in (pc rest) | [] := [] | l := [(stmt.parse_error l)] end def parser (s : list string) : list stmt := ((parser_core s.length) s) def whitespace : char -> bool | ' ' := true | '\n' := true | default := false def lexer (s : string) : list string := ((s.split) whitespace).filter (λ r, r ≠ "") -- IO crap below this line open io def get_file : io char_buffer := fs.read_file "/Users/smooth/build/twitchcoq/metamath/miu2.mm" def main : io unit := do a <- get_file, let s := a.to_string, let l := lexer s, let p := stmt.scope_stmt (parser l), put_str ((stmt_to_string p) ++ "\n") #eval main
657f1baf3ffd03048af1c0c6c39b433d102efb41
f7315930643edc12e76c229a742d5446dad77097
/tests/lean/run/tactic22.lean
9400e037838896393678bd10921fe53180e74582
[ "Apache-2.0" ]
permissive
bmalehorn/lean
8f77b762a76c59afff7b7403f9eb5fc2c3ce70c1
53653c352643751c4b62ff63ec5e555f11dae8eb
refs/heads/master
1,610,945,684,489
1,429,681,220,000
1,429,681,449,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
177
lean
import logic open tactic theorem T (a b c d : Prop) (Ha : a) (Hb : b) (Hc : c) (Hd : d) : a ∧ b ∧ c ∧ d := by fixpoint (λ f, (apply @and.intro; f | assumption; f | id))
7b978c084df301bb042991006e5ef81fe6fd8428
b522075d31b564daeb3347a10eb9bb777ee93943
/equiv.lean
24ab6e143943bdfe3e21fd383b900ea91a89446a
[]
no_license
truonghoangle/manifolds
e6c2534dd46579f56ba99a48e2eb7ce51640e7c0
9c0d731a480e88758180b31ce7c3b371771d426b
refs/heads/master
1,638,501,090,139
1,557,991,948,000
1,557,991,948,000
185,779,631
0
0
null
null
null
null
UTF-8
Lean
false
false
4,076
lean
/- Copyright (c) 2019 Joe Cool. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Hoang Le Truong. -/ import data.equiv.algebra algebra.module data.pfun universes u v w variables {α : Type u} {β : Type v} {γ : Type w} local attribute [instance] classical.prop_decidable namespace roption noncomputable theory instance [has_add α]: has_add (roption α) := ⟨ λ x y, ⟨x.dom ∧ y.dom, λ h, x.get (h.1)+ y.get (h.2) ⟩ ⟩ def add_def [has_add α] (x y: roption α) : x+y =⟨x.dom ∧ y.dom, λ h, x.get (h.1)+ y.get (h.2) ⟩ :=rfl instance [has_zero α ] : has_zero (roption (α )) :=⟨some (0:α)⟩ def zero_def [has_zero α] : (0:roption α ) =⟨true, λ _, (0:α) ⟩ :=rfl instance [has_one α ]: has_one (roption (α )) :=⟨some (1:α)⟩ def one_def [has_one α] : (1:roption α ) =⟨true, λ _, (1:α) ⟩ :=rfl instance [has_mul α ]: has_mul (roption α) := ⟨ λ x y, ⟨x.dom ∧ y.dom, λ h, x.get (h.1) * y.get (h.2) ⟩ ⟩ def mul_def [has_mul α] (x y: roption α) : x * y =⟨x.dom ∧ y.dom , λ h, x.get (h.1) * y.get (h.2) ⟩ :=rfl instance [has_scalar α β ]: has_scalar α (roption β ) := ⟨λ a f, ⟨f.dom, λ h, a • (f.get h) ⟩ ⟩ def smul_def [has_scalar α β ] (a:α) (x: roption β ) : a • x =⟨x.dom , λ h, a • x.get h ⟩ :=rfl instance [has_neg α ]: has_neg (roption α) := ⟨ λ x, ⟨x.dom , λ h, -x.get h ⟩ ⟩ def neg_def [has_neg α ](x: roption α) : - x =⟨x.dom , λ h, - x.get h ⟩ :=rfl instance [has_inv α ]: has_inv (roption α) := ⟨ λ x, ⟨x.dom , λ h, (x.get h)⁻¹ ⟩ ⟩ def inv_def [has_inv α ](x: roption α) : x⁻¹ =⟨x.dom , λ h, (x.get h)⁻¹ ⟩ :=rfl instance [semigroup α ] : semigroup (roption α) := { mul_assoc := λ x y z, roption.ext' and.assoc (λ _ _, mul_assoc _ _ _), ..roption.has_mul } instance [comm_semigroup α ] : comm_semigroup (roption α) := { mul_comm := λ x y, roption.ext' and.comm (λ _ _, mul_comm _ _ ) ..roption.semigroup } instance [monoid α ] : monoid (roption α) := { monoid. mul := roption.has_mul.mul, mul_assoc := λ x y z, roption.ext' and.assoc (λ _ _, mul_assoc _ _ _), one := some(1:α), one_mul := λ x, roption.ext' (true_and _) (λ _ _, one_mul _), mul_one := λ x, roption.ext' (and_true _) (λ _ _, mul_one _), } instance [comm_monoid α] : comm_monoid (roption α) := { ..roption.comm_semigroup, ..roption.monoid } instance [add_semigroup α ] : add_semigroup (roption α) := { add_assoc:= λ x y z, roption.ext' and.assoc (λ _ _, add_assoc _ _ _) ..roption.has_add} instance [add_comm_semigroup α ] : add_comm_semigroup (roption α) := { add_comm := λ x y, roption.ext' and.comm (λ _ _, add_comm _ _ ) ..roption.add_semigroup } instance [add_monoid α ] : add_monoid (roption α ) := { add_monoid. add := roption.has_add.add, add_assoc := λ x y z, roption.ext' and.assoc (λ _ _, add_assoc _ _ _), zero := roption.has_zero.zero, zero_add := λ x, roption.ext' (true_and _) (λ _ _, zero_add _), add_zero := λ x, roption.ext' (and_true _) (λ _ _,add_zero _), } instance [add_comm_monoid α] : add_comm_monoid ( roption α) := { add_comm := λ x y, roption.ext' and.comm (λ _ _, add_comm _ _ ), ..roption.add_monoid } instance [monoid β][mul_action β α ] :mul_action β (roption α):= { one_smul := λ x, roption.ext' (by{simp[one_def,smul_def]}) (by{ repeat {intro}, simp[smul_def,one_def,one_smul]}), mul_smul := λ a b x, roption.ext' (by{simp[smul_def]}) (by{repeat {intro},simp[smul_def,mul_smul]}) , ..roption.has_scalar } instance [monoid β ] [add_monoid α] [distrib_mul_action β α ] : distrib_mul_action β (roption α):= { smul_add := λ a x y, roption.ext' (by{simp[add_def,smul_def]}) (by{ repeat {intro}, simp[smul_def,add_def,smul_add]}), smul_zero := λ x, roption.ext' (by{simp[zero_def,smul_def]}) (by{ repeat {intro}, simp[smul_def,zero_def,smul_zero]}), ..roption.mul_action } end roption
24604991a081868e07abcf4646ad010bd537c40f
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/analysis/normed_space/bounded_linear_maps.lean
243e941a68b29e594cad2c98ec16a02d1494e40f
[ "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,374
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl Continuous linear functions -- functions between normed vector spaces which are bounded and linear. -/ import algebra.field import analysis.normed_space.operator_norm noncomputable theory local attribute [instance] classical.prop_decidable local notation f ` →_{`:50 a `} `:0 b := filter.tendsto f (nhds a) (nhds b) open filter (tendsto) open metric variables {k : Type*} [nondiscrete_normed_field k] variables {E : Type*} [normed_group E] [normed_space k E] variables {F : Type*} [normed_group F] [normed_space k F] variables {G : Type*} [normed_group G] [normed_space k G] set_option class.instance_max_depth 70 structure is_bounded_linear_map (k : Type*) [normed_field k] {E : Type*} [normed_group E] [normed_space k E] {F : Type*} [normed_group F] [normed_space k F] (f : E → F) extends is_linear_map k f : Prop := (bound : ∃ M > 0, ∀ x : E, ∥ f x ∥ ≤ M * ∥ x ∥) lemma is_linear_map.with_bound {f : E → F} (hf : is_linear_map k f) (M : ℝ) (h : ∀ x : E, ∥ f x ∥ ≤ M * ∥ x ∥) : is_bounded_linear_map k f := ⟨ hf, classical.by_cases (assume : M ≤ 0, ⟨1, zero_lt_one, assume x, le_trans (h x) $ mul_le_mul_of_nonneg_right (le_trans this zero_le_one) (norm_nonneg x)⟩) (assume : ¬ M ≤ 0, ⟨M, lt_of_not_ge this, h⟩)⟩ /-- A continuous linear map satisfies `is_bounded_linear_map` -/ lemma continuous_linear_map.is_bounded_linear_map (f : E →L[k] F) : is_bounded_linear_map k f := { bound := f.bound, ..f.to_linear_map } namespace is_bounded_linear_map def to_linear_map (f : E → F) (h : is_bounded_linear_map k f) : E →ₗ[k] F := (is_linear_map.mk' _ h.to_is_linear_map) /-- Construct a continuous linear map from is_bounded_linear_map -/ def to_continuous_linear_map {f : E → F} (hf : is_bounded_linear_map k f) : E →L[k] F := { cont := let ⟨C, Cpos, hC⟩ := hf.bound in linear_map.continuous_of_bound _ C hC, ..to_linear_map f hf} lemma zero : is_bounded_linear_map k (λ (x:E), (0:F)) := (0 : E →ₗ F).is_linear.with_bound 0 $ by simp [le_refl] lemma id : is_bounded_linear_map k (λ (x:E), x) := linear_map.id.is_linear.with_bound 1 $ by simp [le_refl] lemma fst : is_bounded_linear_map k (λ x : E × F, x.1) := begin refine (linear_map.fst k E F).is_linear.with_bound 1 (λx, _), rw one_mul, exact le_max_left _ _ end lemma snd : is_bounded_linear_map k (λ x : E × F, x.2) := begin refine (linear_map.snd k E F).is_linear.with_bound 1 (λx, _), rw one_mul, exact le_max_right _ _ end variables { f g : E → F } lemma smul (c : k) (hf : is_bounded_linear_map k f) : is_bounded_linear_map k (λ e, c • f e) := let ⟨hlf, M, hMp, hM⟩ := hf in (c • hlf.mk' f).is_linear.with_bound (∥c∥ * M) $ assume x, calc ∥c • f x∥ = ∥c∥ * ∥f x∥ : norm_smul c (f x) ... ≤ ∥c∥ * (M * ∥x∥) : mul_le_mul_of_nonneg_left (hM _) (norm_nonneg _) ... = (∥c∥ * M) * ∥x∥ : (mul_assoc _ _ _).symm lemma neg (hf : is_bounded_linear_map k f) : is_bounded_linear_map k (λ e, -f e) := begin rw show (λ e, -f e) = (λ e, (-1 : k) • f e), { funext, simp }, exact smul (-1) hf end lemma add (hf : is_bounded_linear_map k f) (hg : is_bounded_linear_map k g) : is_bounded_linear_map k (λ e, f e + g e) := let ⟨hlf, Mf, hMfp, hMf⟩ := hf in let ⟨hlg, Mg, hMgp, hMg⟩ := hg in (hlf.mk' _ + hlg.mk' _).is_linear.with_bound (Mf + Mg) $ assume x, calc ∥f x + g x∥ ≤ ∥f x∥ + ∥g x∥ : norm_triangle _ _ ... ≤ Mf * ∥x∥ + Mg * ∥x∥ : add_le_add (hMf x) (hMg x) ... ≤ (Mf + Mg) * ∥x∥ : by rw add_mul lemma sub (hf : is_bounded_linear_map k f) (hg : is_bounded_linear_map k g) : is_bounded_linear_map k (λ e, f e - g e) := add hf (neg hg) lemma comp {g : F → G} (hg : is_bounded_linear_map k g) (hf : is_bounded_linear_map k f) : is_bounded_linear_map k (g ∘ f) := let ⟨hlg, Mg, hMgp, hMg⟩ := hg in let ⟨hlf, Mf, hMfp, hMf⟩ := hf in ((hlg.mk' _).comp (hlf.mk' _)).is_linear.with_bound (Mg * Mf) $ assume x, calc ∥g (f x)∥ ≤ Mg * ∥f x∥ : hMg _ ... ≤ Mg * (Mf * ∥x∥) : mul_le_mul_of_nonneg_left (hMf _) (le_of_lt hMgp) ... = Mg * Mf * ∥x∥ : (mul_assoc _ _ _).symm lemma tendsto (x : E) (hf : is_bounded_linear_map k f) : f →_{x} (f x) := let ⟨hf, M, hMp, hM⟩ := hf in tendsto_iff_norm_tendsto_zero.2 $ squeeze_zero (assume e, norm_nonneg _) (assume e, calc ∥f e - f x∥ = ∥hf.mk' f (e - x)∥ : by rw (hf.mk' _).map_sub e x; refl ... ≤ M * ∥e - x∥ : hM (e - x)) (suffices (λ (e : E), M * ∥e - x∥) →_{x} (M * 0), by simpa, tendsto_mul tendsto_const_nhds (lim_norm _)) lemma continuous (hf : is_bounded_linear_map k f) : continuous f := continuous_iff_continuous_at.2 $ λ _, hf.tendsto _ lemma lim_zero_bounded_linear_map (hf : is_bounded_linear_map k f) : (f →_{0} 0) := (hf.1.mk' _).map_zero ▸ continuous_iff_continuous_at.1 hf.continuous 0 section open asymptotics filter theorem is_O_id {f : E → F} (h : is_bounded_linear_map k f) (l : filter E) : is_O f (λ x, x) l := let ⟨M, hMp, hM⟩ := h.bound in ⟨M, hMp, mem_sets_of_superset univ_mem_sets (λ x _, hM x)⟩ theorem is_O_comp {g : F → G} (hg : is_bounded_linear_map k g) {f : E → F} (l : filter E) : is_O (λ x', g (f x')) f l := ((hg.is_O_id ⊤).comp _).mono (map_le_iff_le_comap.mp lattice.le_top) theorem is_O_sub {f : E → F} (h : is_bounded_linear_map k f) (l : filter E) (x : E) : is_O (λ x', f (x' - x)) (λ x', x' - x) l := is_O_comp h l end end is_bounded_linear_map section set_option class.instance_max_depth 180 lemma is_bounded_linear_map_prod_iso : is_bounded_linear_map k (λ(p : (E →L[k] F) × (E →L[k] G)), (continuous_linear_map.prod p.1 p.2 : (E →L[k] (F × G)))) := begin refine is_linear_map.with_bound ⟨λu v, rfl, λc u, rfl⟩ 1 (λp, _), simp only [norm, one_mul], refine continuous_linear_map.op_norm_le_bound _ (le_trans (norm_nonneg _) (le_max_left _ _)) (λu, _), simp only [norm, continuous_linear_map.prod, max_le_iff], split, { calc ∥p.1 u∥ ≤ ∥p.1∥ * ∥u∥ : continuous_linear_map.le_op_norm _ _ ... ≤ max (∥p.1∥) (∥p.2∥) * ∥u∥ : mul_le_mul_of_nonneg_right (le_max_left _ _) (norm_nonneg _) }, { calc ∥p.2 u∥ ≤ ∥p.2∥ * ∥u∥ : continuous_linear_map.le_op_norm _ _ ... ≤ max (∥p.1∥) (∥p.2∥) * ∥u∥ : mul_le_mul_of_nonneg_right (le_max_right _ _) (norm_nonneg _) } end lemma continuous_linear_map.is_bounded_linear_map_comp_left (g : continuous_linear_map k F G) : is_bounded_linear_map k (λ(f : E →L[k] F), continuous_linear_map.comp g f) := begin refine is_linear_map.with_bound ⟨λu v, _, λc u, _⟩ (∥g∥) (λu, continuous_linear_map.op_norm_comp_le _ _), { ext x, change g ((u+v) x) = g (u x) + g (v x), have : (u+v) x = u x + v x := rfl, rw [this, g.map_add] }, { ext x, change g ((c • u) x) = c • g (u x), have : (c • u) x = c • u x := rfl, rw [this, continuous_linear_map.map_smul] } end lemma continuous_linear_map.is_bounded_linear_map_comp_right (f : continuous_linear_map k E F) : is_bounded_linear_map k (λ(g : F →L[k] G), continuous_linear_map.comp g f) := begin refine is_linear_map.with_bound ⟨λu v, rfl, λc u, rfl⟩ (∥f∥) (λg, _), rw mul_comm, exact continuous_linear_map.op_norm_comp_le _ _ end end section bilinear_map variable (k) structure is_bounded_bilinear_map (f : E × F → G) : Prop := (add_left : ∀(x₁ x₂ : E) (y : F), f (x₁ + x₂, y) = f (x₁, y) + f (x₂, y)) (smul_left : ∀(c : k) (x : E) (y : F), f (c • x, y) = c • f (x,y)) (add_right : ∀(x : E) (y₁ y₂ : F), f (x, y₁ + y₂) = f (x, y₁) + f (x, y₂)) (smul_right : ∀(c : k) (x : E) (y : F), f (x, c • y) = c • f (x,y)) (bound : ∃C>0, ∀(x : E) (y : F), ∥f (x, y)∥ ≤ C * ∥x∥ * ∥y∥) variable {k} variable {f : E × F → G} lemma is_bounded_bilinear_map.map_sub_left (h : is_bounded_bilinear_map k f) {x y : E} {z : F} : f (x - y, z) = f (x, z) - f(y, z) := calc f (x - y, z) = f (x + (-1 : k) • y, z) : by simp ... = f (x, z) + (-1 : k) • f (y, z) : by simp only [h.add_left, h.smul_left] ... = f (x, z) - f (y, z) : by simp lemma is_bounded_bilinear_map.map_sub_right (h : is_bounded_bilinear_map k f) {x : E} {y z : F} : f (x, y - z) = f (x, y) - f (x, z) := calc f (x, y - z) = f (x, y + (-1 : k) • z) : by simp ... = f (x, y) + (-1 : k) • f (x, z) : by simp only [h.add_right, h.smul_right] ... = f (x, y) - f (x, z) : by simp lemma is_bounded_bilinear_map_smul : is_bounded_bilinear_map k (λ (p : k × E), p.1 • p.2) := { add_left := add_smul, smul_left := λc x y, by simp [smul_smul], add_right := smul_add, smul_right := λc x y, by simp [smul_smul, mul_comm], bound := ⟨1, zero_lt_one, λx y, by simp [norm_smul]⟩ } lemma is_bounded_bilinear_map_mul : is_bounded_bilinear_map k (λ (p : k × k), p.1 * p.2) := is_bounded_bilinear_map_smul lemma is_bounded_bilinear_map_comp : is_bounded_bilinear_map k (λ(p : (E →L[k] F) × (F →L[k] G)), p.2.comp p.1) := { add_left := λx₁ x₂ y, begin ext z, change y (x₁ z + x₂ z) = y (x₁ z) + y (x₂ z), rw y.map_add end, smul_left := λc x y, begin ext z, change y (c • (x z)) = c • y (x z), rw continuous_linear_map.map_smul end, add_right := λx y₁ y₂, rfl, smul_right := λc x y, rfl, bound := ⟨1, zero_lt_one, λx y, calc ∥continuous_linear_map.comp ((x, y).snd) ((x, y).fst)∥ ≤ ∥y∥ * ∥x∥ : continuous_linear_map.op_norm_comp_le _ _ ... = 1 * ∥x∥ * ∥ y∥ : by ring ⟩ } lemma is_bounded_bilinear_map_apply : is_bounded_bilinear_map k (λp : (E →L[k] F) × E, p.1 p.2) := { add_left := by simp, smul_left := by simp, add_right := by simp, smul_right := by simp, bound := ⟨1, zero_lt_one, by simp [continuous_linear_map.le_op_norm]⟩ } /-- Definition of the derivative of a bilinear map `f`, given at a point `p` by `q ↦ f(p.1, q.2) + f(q.1, p.2)` as in the standard formula for the derivative of a product. We define this function here a bounded linear map from `E × F` to `G`. The fact that this is indeed the derivative of `f` is proved in `is_bounded_bilinear_map.has_fderiv_at` in `deriv.lean`-/ def is_bounded_bilinear_map.linear_deriv (h : is_bounded_bilinear_map k f) (p : E × F) : (E × F) →ₗ[k] G := { to_fun := λq, f (p.1, q.2) + f (q.1, p.2), add := λq₁ q₂, begin change f (p.1, q₁.2 + q₂.2) + f (q₁.1 + q₂.1, p.2) = f (p.1, q₁.2) + f (q₁.1, p.2) + (f (p.1, q₂.2) + f (q₂.1, p.2)), simp [h.add_left, h.add_right] end, smul := λc q, begin change f (p.1, c • q.2) + f (c • q.1, p.2) = c • (f (p.1, q.2) + f (q.1, p.2)), simp [h.smul_left, h.smul_right, smul_add] end } def is_bounded_bilinear_map.deriv (h : is_bounded_bilinear_map k f) (p : E × F) : (E × F) →L[k] G := (h.linear_deriv p).with_bound $ begin rcases h.bound with ⟨C, Cpos, hC⟩, refine ⟨C * ∥p.1∥ + C * ∥p.2∥, λq, _⟩, calc ∥f (p.1, q.2) + f (q.1, p.2)∥ ≤ ∥f (p.1, q.2)∥ + ∥f (q.1, p.2)∥ : norm_triangle _ _ ... ≤ C * ∥p.1∥ * ∥q.2∥ + C * ∥q.1∥ * ∥p.2∥ : add_le_add (hC _ _) (hC _ _) ... ≤ C * ∥p.1∥ * ∥q∥ + C * ∥q∥ * ∥p.2∥ : begin apply add_le_add, exact mul_le_mul_of_nonneg_left (le_max_right _ _) (mul_nonneg (le_of_lt Cpos) (norm_nonneg _)), apply mul_le_mul_of_nonneg_right _ (norm_nonneg _), exact mul_le_mul_of_nonneg_left (le_max_left _ _) (le_of_lt Cpos), end ... = (C * ∥p.1∥ + C * ∥p.2∥) * ∥q∥ : by ring end @[simp] lemma is_bounded_bilinear_map_deriv_coe (h : is_bounded_bilinear_map k f) (p q : E × F) : h.deriv p q = f (p.1, q.2) + f (q.1, p.2) := rfl set_option class.instance_max_depth 95 /-- Given a bounded bilinear map `f`, the map associating to a point `p` the derivative of `f` at `p` is itself a bounded linear map. -/ lemma is_bounded_bilinear_map.is_bounded_linear_map_deriv (h : is_bounded_bilinear_map k f) : is_bounded_linear_map k (λp : E × F, h.deriv p) := begin rcases h.bound with ⟨C, Cpos, hC⟩, refine is_linear_map.with_bound ⟨λp₁ p₂, _, λc p, _⟩ (C + C) (λp, _), { ext q, simp [h.add_left, h.add_right] }, { ext q, simp [h.smul_left, h.smul_right, smul_add] }, { refine continuous_linear_map.op_norm_le_bound _ (mul_nonneg (add_nonneg (le_of_lt Cpos) (le_of_lt Cpos)) (norm_nonneg _)) (λq, _), calc ∥f (p.1, q.2) + f (q.1, p.2)∥ ≤ ∥f (p.1, q.2)∥ + ∥f (q.1, p.2)∥ : norm_triangle _ _ ... ≤ C * ∥p.1∥ * ∥q.2∥ + C * ∥q.1∥ * ∥p.2∥ : add_le_add (hC _ _) (hC _ _) ... ≤ C * ∥p∥ * ∥q∥ + C * ∥q∥ * ∥p∥ : by apply_rules [add_le_add, mul_le_mul, norm_nonneg, le_of_lt Cpos, le_refl, le_max_left, le_max_right, mul_nonneg, norm_nonneg, norm_nonneg, norm_nonneg] ... = (C + C) * ∥p∥ * ∥q∥ : by ring }, end end bilinear_map
0d3f4e2e4b20d43fb07cc11983c5325ca296697f
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/06_Inductive_Types.org.35.lean
8a17305c0d296d098d1800fafdfb3308ff2261d9
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
322
lean
/- page 89 -/ import standard namespace hide -- BEGIN inductive list (A : Type) : Type := | nil {} : list A | cons : A → list A → list A namespace list notation `[` l:(foldr `,` (h t, cons h t) nil) `]` := l section open nat check [1, 2, 3, 4, 5] check ([1, 2, 3, 4, 5] : list ℕ) end end list -- END end hide
e6de58ff331db089a3adafb358bc919fa3590b98
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Meta/SizeOf.lean
be337143d0c62591d10636a241a45fbbe745023f
[ "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
23,640
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.AppBuilder import Lean.Meta.Instances namespace Lean.Meta /-- Create `SizeOf` local instances for applicable parameters, and execute `k` using them. -/ private partial def mkLocalInstances (params : Array Expr) (k : Array Expr → MetaM α) : MetaM α := loop 0 #[] where loop (i : Nat) (insts : Array Expr) : MetaM α := do if i < params.size then let param := params[i]! let paramType ← inferType param let instType? ← forallTelescopeReducing paramType fun xs _ => do let type := mkAppN param xs try let sizeOf ← mkAppM `SizeOf #[type] let instType ← mkForallFVars xs sizeOf return some instType catch _ => return none match instType? with | none => loop (i+1) insts | some instType => let instName ← mkFreshUserName `inst withLocalDecl instName BinderInfo.instImplicit instType fun inst => loop (i+1) (insts.push inst) else k insts /-- Return `some x` if `fvar` has type of the form `... -> motive ... fvar` where `motive` in `motiveFVars`. That is, `x` "produces" one of the recursor motives. -/ private def isInductiveHypothesis? (motiveFVars : Array Expr) (fvar : Expr) : MetaM (Option Expr) := do forallTelescopeReducing (← inferType fvar) fun _ type => if type.isApp && motiveFVars.contains type.getAppFn then return some type.appArg! else return none private def isInductiveHypothesis (motiveFVars : Array Expr) (fvar : Expr) : MetaM Bool := return (← isInductiveHypothesis? motiveFVars fvar).isSome /-- Let `motiveFVars` be free variables for each motive in a kernel recursor, and `minorFVars` the free variables for a minor premise. Then, return `some idx` if `minorFVars[idx]` has a type of the form `... -> motive ... fvar` for some `motive` in `motiveFVars`. -/ private def isRecField? (motiveFVars : Array Expr) (minorFVars : Array Expr) (fvar : Expr) : MetaM (Option Nat) := do let mut idx := 0 for minorFVar in minorFVars do if let some fvar' ← isInductiveHypothesis? motiveFVars minorFVar then if fvar == fvar'.getAppFn then return some idx idx := idx + 1 return none private partial def mkSizeOfMotives (motiveFVars : Array Expr) (k : Array Expr → MetaM α) : MetaM α := loop 0 #[] where loop (i : Nat) (motives : Array Expr) : MetaM α := do if i < motiveFVars.size then let type ← inferType motiveFVars[i]! let motive ← forallTelescopeReducing type fun xs _ => do mkLambdaFVars xs <| mkConst ``Nat trace[Meta.sizeOf] "motive: {motive}" loop (i+1) (motives.push motive) else k motives private partial def ignoreFieldType (type : Expr) : MetaM Bool := do let type ← whnf type if type.isForall then -- TODO: add support for finite domains if type.isArrow && type.bindingDomain!.isConstOf ``Unit then ignoreFieldType type.bindingBody! else return true else return false private def ignoreField (x : Expr) : MetaM Bool := do let type ← inferType x ignoreFieldType type /-- See `ignoreField`. We have support for functions of the form `Unit → ...` -/ private partial def mkSizeOfRecFieldFormIH (ih : Expr) : MetaM Expr := do if (← whnf (← inferType ih)).isForall then mkSizeOfRecFieldFormIH (mkApp ih (mkConst ``Unit.unit)) else return ih private partial def mkSizeOfMinors (motiveFVars : Array Expr) (minorFVars : Array Expr) (minorFVars' : Array Expr) (k : Array Expr → MetaM α) : MetaM α := assert! minorFVars.size == minorFVars'.size loop 0 #[] where loop (i : Nat) (minors : Array Expr) : MetaM α := do if i < minorFVars.size then forallTelescopeReducing (← inferType minorFVars[i]!) fun xs _ => do forallBoundedTelescope (← inferType minorFVars'[i]!) xs.size fun xs' _ => do let mut minor ← mkNumeral (mkConst ``Nat) 1 for x in xs, x' in xs' do unless (← isInductiveHypothesis motiveFVars x) do unless (← ignoreField x) do -- we suppress higher-order fields match (← isRecField? motiveFVars xs x) with | some idx => minor ← mkAdd minor (← mkSizeOfRecFieldFormIH xs'[idx]!) | none => minor ← mkAdd minor (← mkAppM ``SizeOf.sizeOf #[x']) minor ← mkLambdaFVars xs' minor trace[Meta.sizeOf] "minor: {minor}" loop (i+1) (minors.push minor) else k minors /-- Create a "sizeOf" function with name `declName` using the recursor `recName`. -/ partial def mkSizeOfFn (recName : Name) (declName : Name): MetaM Unit := do trace[Meta.sizeOf] "recName: {recName}" let recInfo : RecursorVal ← getConstInfoRec recName forallTelescopeReducing recInfo.type fun xs _ => let levelParams := recInfo.levelParams.tail! -- universe parameters for declaration being defined let params := xs[:recInfo.numParams] let motiveFVars := xs[recInfo.numParams : recInfo.numParams + recInfo.numMotives] let minorFVars := xs[recInfo.getFirstMinorIdx : recInfo.getFirstMinorIdx + recInfo.numMinors] let indices := xs[recInfo.getFirstIndexIdx : recInfo.getFirstIndexIdx + recInfo.numIndices] let major := xs[recInfo.getMajorIdx]! let nat := mkConst ``Nat mkLocalInstances params fun localInsts => mkSizeOfMotives motiveFVars fun motives => do let us := levelOne :: levelParams.map mkLevelParam -- universe level parameters for `rec`-application let recFn := mkConst recName us let val := mkAppN recFn (params ++ motives) forallBoundedTelescope (← inferType val) recInfo.numMinors fun minorFVars' _ => mkSizeOfMinors motiveFVars minorFVars minorFVars' fun minors => do withInstImplicitAsImplict params do let sizeOfParams := params ++ localInsts ++ indices ++ #[major] let sizeOfType ← mkForallFVars sizeOfParams nat let val := mkAppN val (minors ++ indices ++ #[major]) let sizeOfValue ← mkLambdaFVars sizeOfParams val trace[Meta.sizeOf] "declName: {declName}" trace[Meta.sizeOf] "type: {sizeOfType}" trace[Meta.sizeOf] "val: {sizeOfValue}" addDecl <| Declaration.defnDecl { name := declName levelParams := levelParams type := sizeOfType value := sizeOfValue safety := DefinitionSafety.safe hints := ReducibilityHints.abbrev } /-- Create `sizeOf` functions for all inductive datatypes in the mutual inductive declaration containing `typeName` The resulting array contains the generated functions names. The `NameMap` maps recursor names into the generated function names. There is a function for each element of the mutual inductive declaration, and for auxiliary recursors for nested inductive types. -/ def mkSizeOfFns (typeName : Name) : MetaM (Array Name × NameMap Name) := do let indInfo ← getConstInfoInduct typeName let recInfo ← getConstInfoRec (mkRecName typeName) let numExtra := recInfo.numMotives - indInfo.all.length -- numExtra > 0 for nested inductive types let mut result := #[] let baseName := indInfo.all.head! ++ `_sizeOf -- we use the first inductive type as the base name for `sizeOf` functions let mut i := 1 let mut recMap : NameMap Name := {} for indTypeName in indInfo.all do let sizeOfName := baseName.appendIndexAfter i let recName := mkRecName indTypeName mkSizeOfFn recName sizeOfName recMap := recMap.insert recName sizeOfName result := result.push sizeOfName i := i + 1 for j in [:numExtra] do let recName := (mkRecName indInfo.all.head!).appendIndexAfter (j+1) let sizeOfName := baseName.appendIndexAfter i mkSizeOfFn recName sizeOfName recMap := recMap.insert recName sizeOfName result := result.push sizeOfName i := i + 1 return (result, recMap) def mkSizeOfSpecLemmaName (ctorName : Name) : Name := ctorName ++ `sizeOf_spec def mkSizeOfSpecLemmaInstance (ctorApp : Expr) : MetaM Expr := matchConstCtor ctorApp.getAppFn (fun _ => throwError "failed to apply 'sizeOf' spec, constructor expected{indentExpr ctorApp}") fun ctorInfo _ => do let ctorArgs := ctorApp.getAppArgs let ctorParams := ctorArgs[:ctorInfo.numParams] let ctorFields := ctorArgs[ctorInfo.numParams:] let lemmaName := mkSizeOfSpecLemmaName ctorInfo.name let lemmaInfo ← getConstInfo lemmaName let lemmaArity ← forallTelescopeReducing lemmaInfo.type fun xs _ => return xs.size let lemmaArgMask := ctorParams.toArray.map some let lemmaArgMask := lemmaArgMask ++ mkArray (lemmaArity - ctorInfo.numParams - ctorInfo.numFields) (none (α := Expr)) let lemmaArgMask := lemmaArgMask ++ ctorFields.toArray.map some mkAppOptM lemmaName lemmaArgMask /-! # SizeOf spec theorem for nested inductive types -/ namespace SizeOfSpecNested structure Context where indInfo : InductiveVal sizeOfFns : Array Name ctorName : Name params : Array Expr localInsts : Array Expr recMap : NameMap Name -- mapping from recursor name into `_sizeOf_<idx>` function name (see `mkSizeOfFns`) abbrev M := ReaderT Context MetaM def throwUnexpected {α} (msg : MessageData) : M α := do throwError "failed to generate sizeOf theorem for {(← read).ctorName} (use `set_option genSizeOfSpec false` to disable theorem generation), {msg}" def throwFailed {α} : M α := do throwError "failed to generate sizeOf theorem for {(← read).ctorName}, (use `set_option genSizeOfSpec false` to disable theorem generation)" /-- Convert a recursor application into a `_sizeOf_<idx>` application. -/ private def recToSizeOf (e : Expr) : M Expr := do matchConstRec e.getAppFn (fun _ => throwFailed) fun info us => do match (← read).recMap.find? info.name with | none => throwUnexpected m!"expected recursor application {indentExpr e}" | some sizeOfName => let args := e.getAppArgs let indices := args[info.getFirstIndexIdx : info.getFirstIndexIdx + info.numIndices] let major := args[info.getMajorIdx]! return mkAppN (mkConst sizeOfName us.tail!) ((← read).params ++ (← read).localInsts ++ indices ++ #[major]) mutual /-- Construct minor premise proof for `mkSizeOfAuxLemmaProof`. `ys` contains fields and inductive hypotheses for the minor premise. -/ private partial def mkMinorProof (ys : Array Expr) (lhs rhs : Expr) : M Expr := do trace[Meta.sizeOf.minor] "{lhs} =?= {rhs}" if (← isDefEq lhs rhs) then mkEqRefl rhs else match (← whnfI lhs).natAdd?, (← whnfI rhs).natAdd? with | some (a₁, b₁), some (a₂, b₂) => let p₁ ← mkMinorProof ys a₁ a₂ let p₂ ← mkMinorProofStep ys b₁ b₂ mkCongr (← mkCongrArg (mkConst ``Nat.add) p₁) p₂ | _, _ => throwUnexpected m!"expected 'Nat.add' application, lhs is {indentExpr lhs}\nrhs is{indentExpr rhs}" /-- Helper method for `mkMinorProof`. The proof step is one of the following - Reflexivity - Assumption (i.e., using an inductive hypotheses from `ys`) - `mkSizeOfAuxLemma` application. This case happens when we have multiple levels of nesting -/ private partial def mkMinorProofStep (ys : Array Expr) (lhs rhs : Expr) : M Expr := do if (← isDefEq lhs rhs) then mkEqRefl rhs else let lhs ← recToSizeOf lhs trace[Meta.sizeOf.minor.step] "{lhs} =?= {rhs}" let target ← mkEq lhs rhs for y in ys do if (← isDefEq (← inferType y) target) then return y mkSizeOfAuxLemma lhs rhs /-- Construct proof of auxiliary lemma. See `mkSizeOfAuxLemma` -/ private partial def mkSizeOfAuxLemmaProof (info : InductiveVal) (lhs : Expr) : M Expr := do let lhsArgs := lhs.getAppArgs let sizeOfBaseArgs := lhsArgs[:lhsArgs.size - info.numIndices - 1] let indicesMajor := lhsArgs[lhsArgs.size - info.numIndices - 1:] let sizeOfLevels := lhs.getAppFn.constLevels! let rec /-- Auxiliary function for constructing an `_sizeOf_<idx>` for `ys`, where `ys` are the indices + major. Recall that if `info.name` is part of a mutually inductive declaration, then the resulting application is not necessarily a `lhs.getAppFn` application. The result is an application of one of the `(← read),sizeOfFns` functions. We use this auxiliary function to builtin the motive of the recursor. -/ mkSizeOf (ys : Array Expr) : M Expr := do for sizeOfFn in (← read).sizeOfFns do let candidate := mkAppN (mkAppN (mkConst sizeOfFn sizeOfLevels) sizeOfBaseArgs) ys if (← isTypeCorrect candidate) then return candidate throwFailed let major := lhs.appArg! let majorType ← whnf (← inferType major) let majorTypeArgs := majorType.getAppArgs match majorType.getAppFn.const? with | none => throwFailed | some (_, us) => let recName := mkRecName info.name let recInfo ← getConstInfoRec recName let r := mkConst recName (levelZero :: us) let r := mkAppN r majorTypeArgs[:info.numParams] forallBoundedTelescope (← inferType r) recInfo.numMotives fun motiveFVars _ => do let mut r := r -- Add motives for motiveFVar in motiveFVars do let motive ← forallTelescopeReducing (← inferType motiveFVar) fun ys _ => do let lhs ← mkSizeOf ys let rhs ← mkAppM ``SizeOf.sizeOf #[ys.back] mkLambdaFVars ys (← mkEq lhs rhs) r := mkApp r motive forallBoundedTelescope (← inferType r) recInfo.numMinors fun minorFVars _ => do let mut r := r -- Add minors for minorFVar in minorFVars do let minor ← forallTelescopeReducing (← inferType minorFVar) fun ys target => do let target ← whnf target match target.eq? with | none => throwFailed | some (_, lhs, rhs) => if (← isDefEq lhs rhs) then mkLambdaFVars ys (← mkEqRefl rhs) else let lhs ← unfoldDefinition lhs -- Unfold `_sizeOf_<idx>` -- rhs is of the form `sizeOf (ctor ...)` let ctorApp := rhs.appArg! let specLemma ← mkSizeOfSpecLemmaInstance ctorApp let specEq ← whnf (← inferType specLemma) match specEq.eq? with | none => throwFailed | some (_, _, rhsExpanded) => let lhs_eq_rhsExpanded ← mkMinorProof ys lhs rhsExpanded let rhsExpanded_eq_rhs ← mkEqSymm specLemma mkLambdaFVars ys (← mkEqTrans lhs_eq_rhsExpanded rhsExpanded_eq_rhs) r := mkApp r minor -- Add indices and major return mkAppN r indicesMajor /-- Generate proof for `C._sizeOf_<idx> t = sizeOf t` where `C._sizeOf_<idx>` is a auxiliary function generated for a nested inductive type in `C`. For example, given ```lean inductive Expr where | app (f : String) (args : List Expr) ``` We generate the auxiliary function `Expr._sizeOf_1 : List Expr → Nat`. To generate the `sizeOf` spec lemma ``` sizeOf (Expr.app f args) = 1 + sizeOf f + sizeOf args ``` we need an auxiliary lemma for showing `Expr._sizeOf_1 args = sizeOf args`. Recall that `sizeOf (Expr.app f args)` is definitionally equal to `1 + sizeOf f + Expr._sizeOf_1 args`, but `Expr._sizeOf_1 args` is **not** definitionally equal to `sizeOf args`. We need a proof by induction. -/ private partial def mkSizeOfAuxLemma (lhs rhs : Expr) : M Expr := do trace[Meta.sizeOf.aux] "{lhs} =?= {rhs}" match lhs.getAppFn.const? with | none => throwFailed | some (fName, us) => let thmLevelParams ← us.mapM fun | Level.param n => return n | _ => throwFailed let thmName := fName.appendAfter "_eq" if (← getEnv).contains thmName then -- Auxiliary lemma has already been defined return mkAppN (mkConst thmName us) lhs.getAppArgs else -- Define auxiliary lemma -- First, generalize indices let x := lhs.appArg! let xType ← whnf (← inferType x) matchConstInduct xType.getAppFn (fun _ => throwFailed) fun info _ => do let params := xType.getAppArgs[:info.numParams] forallTelescopeReducing (← inferType (mkAppN xType.getAppFn params)) fun indices _ => do let majorType := mkAppN (mkAppN xType.getAppFn params) indices withLocalDeclD `x majorType fun major => do let lhsArgs := lhs.getAppArgs let lhsArgsNew := lhsArgs[:lhsArgs.size - 1 - indices.size] ++ indices ++ #[major] let lhsNew := mkAppN lhs.getAppFn lhsArgsNew let rhsNew ← mkAppM ``SizeOf.sizeOf #[major] let eq ← mkEq lhsNew rhsNew let thmParams := lhsArgsNew let thmType ← mkForallFVars thmParams eq let thmValue ← mkSizeOfAuxLemmaProof info lhsNew let thmValue ← mkLambdaFVars thmParams thmValue trace[Meta.sizeOf] "thmValue: {thmValue}" addDecl <| Declaration.thmDecl { name := thmName levelParams := thmLevelParams type := thmType value := thmValue } return mkAppN (mkConst thmName us) lhs.getAppArgs end /- Prove SizeOf spec lemma of the form `sizeOf <ctor-application> = 1 + sizeOf <field_1> + ... + sizeOf <field_n> -/ partial def main (lhs rhs : Expr) : M Expr := do if (← isDefEq lhs rhs) then mkEqRefl rhs else /- Expand lhs and rhs to obtain `Nat.add` applications -/ let lhs ← whnfI lhs -- Expand `sizeOf (ctor ...)` into `_sizeOf_<idx>` application let lhs ← unfoldDefinition lhs -- Unfold `_sizeOf_<idx>` application into `HAdd.hAdd` application loop lhs rhs where loop (lhs rhs : Expr) : M Expr := do trace[Meta.sizeOf.loop] "{lhs} =?= {rhs}" if (← isDefEq lhs rhs) then mkEqRefl rhs else match (← whnfI lhs).natAdd?, (← whnfI rhs).natAdd? with | some (a₁, b₁), some (a₂, b₂) => let p₁ ← loop a₁ a₂ let p₂ ← step b₁ b₂ mkCongr (← mkCongrArg (mkConst ``Nat.add) p₁) p₂ | _, _ => throwUnexpected m!"expected 'Nat.add' application, lhs is {indentExpr lhs}\nrhs is{indentExpr rhs}" step (lhs rhs : Expr) : M Expr := do if (← isDefEq lhs rhs) then mkEqRefl rhs else let lhs ← recToSizeOf lhs mkSizeOfAuxLemma lhs rhs end SizeOfSpecNested private def mkSizeOfSpecTheorem (indInfo : InductiveVal) (sizeOfFns : Array Name) (recMap : NameMap Name) (ctorName : Name) : MetaM Unit := do let ctorInfo ← getConstInfoCtor ctorName let us := ctorInfo.levelParams.map mkLevelParam let simpAttr ← ofExcept <| getAttributeImpl (← getEnv) `simp forallTelescopeReducing ctorInfo.type fun xs _ => do let params := xs[:ctorInfo.numParams] let fields := xs[ctorInfo.numParams:] let ctorApp := mkAppN (mkConst ctorName us) xs mkLocalInstances params fun localInsts => do let lhs ← mkAppM ``SizeOf.sizeOf #[ctorApp] let mut rhs ← mkNumeral (mkConst ``Nat) 1 for field in fields do unless (← ignoreField field) do rhs ← mkAdd rhs (← mkAppM ``SizeOf.sizeOf #[field]) let target ← mkEq lhs rhs trace[Meta.sizeOf] "ctor: {ctorInfo.name}, target: {target}" let thmName := mkSizeOfSpecLemmaName ctorName let thmParams := params ++ localInsts ++ fields let thmType ← mkForallFVars thmParams target let thmValue ← if indInfo.isNested then SizeOfSpecNested.main lhs rhs |>.run { indInfo, sizeOfFns, ctorName, params, localInsts, recMap } else mkEqRefl rhs let thmValue ← mkLambdaFVars thmParams thmValue trace[Meta.sizeOf] "sizeOf spec theorem name: {thmName}" trace[Meta.sizeOf] "sizeOf spec theorem type: {thmType}" trace[Meta.sizeOf] "sizeOf spec theorem value: {thmValue}" unless (← isDefEq (← inferType thmValue) thmType) do throwError "type mismatch" addDecl <| Declaration.thmDecl { name := thmName levelParams := ctorInfo.levelParams type := thmType value := thmValue } simpAttr.add thmName default AttributeKind.global private def mkSizeOfSpecTheorems (indTypeNames : Array Name) (sizeOfFns : Array Name) (recMap : NameMap Name) : MetaM Unit := do for indTypeName in indTypeNames do let indInfo ← getConstInfoInduct indTypeName for ctorName in indInfo.ctors do mkSizeOfSpecTheorem indInfo sizeOfFns recMap ctorName return () register_builtin_option genSizeOf : Bool := { defValue := true descr := "generate `SizeOf` instance for inductive types and structures" } register_builtin_option genSizeOfSpec : Bool := { defValue := true descr := "generate `SizeOf` specification theorems for automatically generated instances" } def mkSizeOfInstances (typeName : Name) : MetaM Unit := do if (← getEnv).contains ``SizeOf && genSizeOf.get (← getOptions) && !(← isInductivePredicate typeName) then withTraceNode `Meta.sizeOf (fun _ => return m!"{typeName}") do let indInfo ← getConstInfoInduct typeName unless indInfo.isUnsafe do let (fns, recMap) ← mkSizeOfFns typeName for indTypeName in indInfo.all, fn in fns do let indInfo ← getConstInfoInduct indTypeName forallTelescopeReducing indInfo.type fun xs _ => let params := xs[:indInfo.numParams] withInstImplicitAsImplict params do let indices := xs[indInfo.numParams:] mkLocalInstances params fun localInsts => do let us := indInfo.levelParams.map mkLevelParam let indType := mkAppN (mkConst indTypeName us) xs let sizeOfIndType ← mkAppM ``SizeOf #[indType] withLocalDeclD `m indType fun m => do let v ← mkLambdaFVars #[m] <| mkAppN (mkConst fn us) (params ++ localInsts ++ indices ++ #[m]) let sizeOfMk ← mkAppM ``SizeOf.mk #[v] let instDeclName := indTypeName ++ `_sizeOf_inst let instDeclType ← mkForallFVars (xs ++ localInsts) sizeOfIndType let instDeclValue ← mkLambdaFVars (xs ++ localInsts) sizeOfMk trace[Meta.sizeOf] ">> {instDeclName} : {instDeclType}" addDecl <| Declaration.defnDecl { name := instDeclName levelParams := indInfo.levelParams type := instDeclType value := instDeclValue safety := .safe hints := .abbrev } addInstance instDeclName AttributeKind.global (eval_prio default) if genSizeOfSpec.get (← getOptions) then mkSizeOfSpecTheorems indInfo.all.toArray fns recMap builtin_initialize registerTraceClass `Meta.sizeOf end Lean.Meta
f40f1df70efa79a53ef6408cda3cb8b94162c022
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/measure_theory/integration.lean
5282b30bd407989c941b4a957a90938fe3218f95
[]
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
70,067
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johannes Hölzl -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.measure_theory.measure_space import Mathlib.measure_theory.borel_space import Mathlib.data.indicator_function import Mathlib.data.support import Mathlib.PostPort universes u v l u_1 u_2 u_3 u_4 u_5 namespace Mathlib /-! # Lebesgue integral for `ennreal`-valued functions We define simple functions and show that each Borel measurable function on `ennreal` can be approximated by a sequence of simple functions. To prove something for an arbitrary measurable function into `ennreal`, the theorem `measurable.ennreal_induction` shows that is it sufficient to show that the property holds for (multiples of) characteristic functions and is closed under addition and supremum of increasing sequences of functions. ## Notation We introduce the following notation for the lower Lebesgue integral of a function `f : α → ennreal`. * `∫⁻ x, f x ∂μ`: integral of a function `f : α → ennreal` with respect to a measure `μ`; * `∫⁻ x, f x`: integral of a function `f : α → ennreal` with respect to the canonical measure `volume` on `α`; * `∫⁻ x in s, f x ∂μ`: integral of a function `f : α → ennreal` over a set `s` with respect to a measure `μ`, defined as `∫⁻ x, f x ∂(μ.restrict s)`; * `∫⁻ x in s, f x`: integral of a function `f : α → ennreal` over a set `s` with respect to the canonical measure `volume`, defined as `∫⁻ x, f x ∂(volume.restrict s)`. -/ namespace measure_theory /-- A function `f` from a measurable space to any type is called *simple*, if every preimage `f ⁻¹' {x}` is measurable, and the range is finite. This structure bundles a function with these properties. -/ structure simple_func (α : Type u) [measurable_space α] (β : Type v) where to_fun : α → β is_measurable_fiber' : ∀ (x : β), is_measurable (to_fun ⁻¹' singleton x) finite_range' : set.finite (set.range to_fun) namespace simple_func protected instance has_coe_to_fun {α : Type u_1} {β : Type u_2} [measurable_space α] : has_coe_to_fun (simple_func α β) := has_coe_to_fun.mk (fun (x : simple_func α β) => α → β) to_fun theorem coe_injective {α : Type u_1} {β : Type u_2} [measurable_space α] {f : simple_func α β} {g : simple_func α β} (H : ⇑f = ⇑g) : f = g := sorry theorem ext {α : Type u_1} {β : Type u_2} [measurable_space α] {f : simple_func α β} {g : simple_func α β} (H : ∀ (a : α), coe_fn f a = coe_fn g a) : f = g := coe_injective (funext H) theorem finite_range {α : Type u_1} {β : Type u_2} [measurable_space α] (f : simple_func α β) : set.finite (set.range ⇑f) := finite_range' f theorem is_measurable_fiber {α : Type u_1} {β : Type u_2} [measurable_space α] (f : simple_func α β) (x : β) : is_measurable (⇑f ⁻¹' singleton x) := is_measurable_fiber' f x /-- Range of a simple function `α →ₛ β` as a `finset β`. -/ protected def range {α : Type u_1} {β : Type u_2} [measurable_space α] (f : simple_func α β) : finset β := set.finite.to_finset (finite_range f) @[simp] theorem mem_range {α : Type u_1} {β : Type u_2} [measurable_space α] {f : simple_func α β} {b : β} : b ∈ simple_func.range f ↔ b ∈ set.range ⇑f := set.finite.mem_to_finset theorem mem_range_self {α : Type u_1} {β : Type u_2} [measurable_space α] (f : simple_func α β) (x : α) : coe_fn f x ∈ simple_func.range f := iff.mpr mem_range (Exists.intro x rfl) @[simp] theorem coe_range {α : Type u_1} {β : Type u_2} [measurable_space α] (f : simple_func α β) : ↑(simple_func.range f) = set.range ⇑f := set.finite.coe_to_finset (finite_range f) theorem mem_range_of_measure_ne_zero {α : Type u_1} {β : Type u_2} [measurable_space α] {f : simple_func α β} {x : β} {μ : measure α} (H : coe_fn μ (⇑f ⁻¹' singleton x) ≠ 0) : x ∈ simple_func.range f := sorry theorem forall_range_iff {α : Type u_1} {β : Type u_2} [measurable_space α] {f : simple_func α β} {p : β → Prop} : (∀ (y : β), y ∈ simple_func.range f → p y) ↔ ∀ (x : α), p (coe_fn f x) := sorry theorem exists_range_iff {α : Type u_1} {β : Type u_2} [measurable_space α] {f : simple_func α β} {p : β → Prop} : (∃ (y : β), ∃ (H : y ∈ simple_func.range f), p y) ↔ ∃ (x : α), p (coe_fn f x) := sorry theorem preimage_eq_empty_iff {α : Type u_1} {β : Type u_2} [measurable_space α] (f : simple_func α β) (b : β) : ⇑f ⁻¹' singleton b = ∅ ↔ ¬b ∈ simple_func.range f := iff.trans set.preimage_singleton_eq_empty (not_congr (iff.symm mem_range)) theorem exists_forall_le {α : Type u_1} {β : Type u_2} [measurable_space α] [Nonempty β] [directed_order β] (f : simple_func α β) : ∃ (C : β), ∀ (x : α), coe_fn f x ≤ C := Exists.imp (fun (C : β) => iff.mp forall_range_iff) (finset.exists_le (simple_func.range f)) /-- Constant function as a `simple_func`. -/ def const (α : Type u_1) {β : Type u_2} [measurable_space α] (b : β) : simple_func α β := mk (fun (a : α) => b) sorry set.finite_range_const protected instance inhabited {α : Type u_1} {β : Type u_2} [measurable_space α] [Inhabited β] : Inhabited (simple_func α β) := { default := const α Inhabited.default } theorem const_apply {α : Type u_1} {β : Type u_2} [measurable_space α] (a : α) (b : β) : coe_fn (const α b) a = b := rfl @[simp] theorem coe_const {α : Type u_1} {β : Type u_2} [measurable_space α] (b : β) : ⇑(const α b) = function.const α b := rfl @[simp] theorem range_const {β : Type u_2} (α : Type u_1) [measurable_space α] [Nonempty α] (b : β) : simple_func.range (const α b) = singleton b := sorry theorem is_measurable_cut {α : Type u_1} {β : Type u_2} [measurable_space α] (r : α → β → Prop) (f : simple_func α β) (h : ∀ (b : β), is_measurable (set_of fun (a : α) => r a b)) : is_measurable (set_of fun (a : α) => r a (coe_fn f a)) := sorry theorem is_measurable_preimage {α : Type u_1} {β : Type u_2} [measurable_space α] (f : simple_func α β) (s : set β) : is_measurable (⇑f ⁻¹' s) := is_measurable_cut (fun (_x : α) (b : β) => b ∈ s) f fun (b : β) => is_measurable.const (b ∈ s) /-- A simple function is measurable -/ protected theorem measurable {α : Type u_1} {β : Type u_2} [measurable_space α] [measurable_space β] (f : simple_func α β) : measurable ⇑f := fun (s : set β) (_x : is_measurable s) => is_measurable_preimage f s protected theorem ae_measurable {α : Type u_1} {β : Type u_2} [measurable_space α] [measurable_space β] {μ : measure α} (f : simple_func α β) : ae_measurable ⇑f := measurable.ae_measurable (simple_func.measurable f) protected theorem sum_measure_preimage_singleton {α : Type u_1} {β : Type u_2} [measurable_space α] (f : simple_func α β) {μ : measure α} (s : finset β) : (finset.sum s fun (y : β) => coe_fn μ (⇑f ⁻¹' singleton y)) = coe_fn μ (⇑f ⁻¹' ↑s) := sum_measure_preimage_singleton s fun (_x : β) (_x_1 : _x ∈ s) => is_measurable_fiber f _x theorem sum_range_measure_preimage_singleton {α : Type u_1} {β : Type u_2} [measurable_space α] (f : simple_func α β) (μ : measure α) : (finset.sum (simple_func.range f) fun (y : β) => coe_fn μ (⇑f ⁻¹' singleton y)) = coe_fn μ set.univ := sorry /-- If-then-else as a `simple_func`. -/ def piecewise {α : Type u_1} {β : Type u_2} [measurable_space α] (s : set α) (hs : is_measurable s) (f : simple_func α β) (g : simple_func α β) : simple_func α β := mk (set.piecewise s ⇑f ⇑g) sorry sorry @[simp] theorem coe_piecewise {α : Type u_1} {β : Type u_2} [measurable_space α] {s : set α} (hs : is_measurable s) (f : simple_func α β) (g : simple_func α β) : ⇑(piecewise s hs f g) = set.piecewise s ⇑f ⇑g := rfl theorem piecewise_apply {α : Type u_1} {β : Type u_2} [measurable_space α] {s : set α} (hs : is_measurable s) (f : simple_func α β) (g : simple_func α β) (a : α) : coe_fn (piecewise s hs f g) a = ite (a ∈ s) (coe_fn f a) (coe_fn g a) := rfl @[simp] theorem piecewise_compl {α : Type u_1} {β : Type u_2} [measurable_space α] {s : set α} (hs : is_measurable (sᶜ)) (f : simple_func α β) (g : simple_func α β) : piecewise (sᶜ) hs f g = piecewise s (is_measurable.of_compl hs) g f := sorry @[simp] theorem piecewise_univ {α : Type u_1} {β : Type u_2} [measurable_space α] (f : simple_func α β) (g : simple_func α β) : piecewise set.univ is_measurable.univ f g = f := sorry @[simp] theorem piecewise_empty {α : Type u_1} {β : Type u_2} [measurable_space α] (f : simple_func α β) (g : simple_func α β) : piecewise ∅ is_measurable.empty f g = g := sorry theorem measurable_bind {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] [measurable_space γ] (f : simple_func α β) (g : β → α → γ) (hg : ∀ (b : β), measurable (g b)) : measurable fun (a : α) => g (coe_fn f a) a := fun (s : set γ) (hs : is_measurable s) => is_measurable_cut (fun (a : α) (b : β) => g b a ∈ s) f fun (b : β) => hg b hs /-- If `f : α →ₛ β` is a simple function and `g : β → α →ₛ γ` is a family of simple functions, then `f.bind g` binds the first argument of `g` to `f`. In other words, `f.bind g a = g (f a) a`. -/ def bind {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] (f : simple_func α β) (g : β → simple_func α γ) : simple_func α γ := mk (fun (a : α) => coe_fn (g (coe_fn f a)) a) sorry sorry @[simp] theorem bind_apply {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] (f : simple_func α β) (g : β → simple_func α γ) (a : α) : coe_fn (bind f g) a = coe_fn (g (coe_fn f a)) a := rfl /-- Given a function `g : β → γ` and a simple function `f : α →ₛ β`, `f.map g` return the simple function `g ∘ f : α →ₛ γ` -/ def map {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] (g : β → γ) (f : simple_func α β) : simple_func α γ := bind f (const α ∘ g) theorem map_apply {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] (g : β → γ) (f : simple_func α β) (a : α) : coe_fn (map g f) a = g (coe_fn f a) := rfl theorem map_map {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} [measurable_space α] (g : β → γ) (h : γ → δ) (f : simple_func α β) : map h (map g f) = map (h ∘ g) f := rfl @[simp] theorem coe_map {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] (g : β → γ) (f : simple_func α β) : ⇑(map g f) = g ∘ ⇑f := rfl @[simp] theorem range_map {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] [DecidableEq γ] (g : β → γ) (f : simple_func α β) : simple_func.range (map g f) = finset.image g (simple_func.range f) := sorry @[simp] theorem map_const {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] (g : β → γ) (b : β) : map g (const α b) = const α (g b) := rfl theorem map_preimage {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] (f : simple_func α β) (g : β → γ) (s : set γ) : ⇑(map g f) ⁻¹' s = ⇑f ⁻¹' ↑(finset.filter (fun (b : β) => g b ∈ s) (simple_func.range f)) := sorry theorem map_preimage_singleton {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] (f : simple_func α β) (g : β → γ) (c : γ) : ⇑(map g f) ⁻¹' singleton c = ⇑f ⁻¹' ↑(finset.filter (fun (b : β) => g b = c) (simple_func.range f)) := map_preimage f g (singleton c) /-- Composition of a `simple_fun` and a measurable function is a `simple_func`. -/ def comp {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] [measurable_space β] (f : simple_func β γ) (g : α → β) (hgm : measurable g) : simple_func α γ := mk (⇑f ∘ g) sorry sorry @[simp] theorem coe_comp {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] [measurable_space β] (f : simple_func β γ) {g : α → β} (hgm : measurable g) : ⇑(comp f g hgm) = ⇑f ∘ g := rfl theorem range_comp_subset_range {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] [measurable_space β] (f : simple_func β γ) {g : α → β} (hgm : measurable g) : simple_func.range (comp f g hgm) ⊆ simple_func.range f := sorry /-- If `f` is a simple function taking values in `β → γ` and `g` is another simple function with the same domain and codomain `β`, then `f.seq g = f a (g a)`. -/ def seq {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] (f : simple_func α (β → γ)) (g : simple_func α β) : simple_func α γ := bind f fun (f : β → γ) => map f g @[simp] theorem seq_apply {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] (f : simple_func α (β → γ)) (g : simple_func α β) (a : α) : coe_fn (seq f g) a = coe_fn f a (coe_fn g a) := rfl /-- Combine two simple functions `f : α →ₛ β` and `g : α →ₛ β` into `λ a, (f a, g a)`. -/ def pair {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] (f : simple_func α β) (g : simple_func α γ) : simple_func α (β × γ) := seq (map Prod.mk f) g @[simp] theorem pair_apply {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] (f : simple_func α β) (g : simple_func α γ) (a : α) : coe_fn (pair f g) a = (coe_fn f a, coe_fn g a) := rfl theorem pair_preimage {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] (f : simple_func α β) (g : simple_func α γ) (s : set β) (t : set γ) : ⇑(pair f g) ⁻¹' set.prod s t = ⇑f ⁻¹' s ∩ ⇑g ⁻¹' t := rfl /- A special form of `pair_preimage` -/ theorem pair_preimage_singleton {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] (f : simple_func α β) (g : simple_func α γ) (b : β) (c : γ) : ⇑(pair f g) ⁻¹' singleton (b, c) = ⇑f ⁻¹' singleton b ∩ ⇑g ⁻¹' singleton c := sorry theorem bind_const {α : Type u_1} {β : Type u_2} [measurable_space α] (f : simple_func α β) : bind f (const α) = f := sorry protected instance has_zero {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] : HasZero (simple_func α β) := { zero := const α 0 } protected instance has_add {α : Type u_1} {β : Type u_2} [measurable_space α] [Add β] : Add (simple_func α β) := { add := fun (f g : simple_func α β) => seq (map Add.add f) g } protected instance has_mul {α : Type u_1} {β : Type u_2} [measurable_space α] [Mul β] : Mul (simple_func α β) := { mul := fun (f g : simple_func α β) => seq (map Mul.mul f) g } protected instance has_sup {α : Type u_1} {β : Type u_2} [measurable_space α] [has_sup β] : has_sup (simple_func α β) := has_sup.mk fun (f g : simple_func α β) => seq (map has_sup.sup f) g protected instance has_inf {α : Type u_1} {β : Type u_2} [measurable_space α] [has_inf β] : has_inf (simple_func α β) := has_inf.mk fun (f g : simple_func α β) => seq (map has_inf.inf f) g protected instance has_le {α : Type u_1} {β : Type u_2} [measurable_space α] [HasLessEq β] : HasLessEq (simple_func α β) := { LessEq := fun (f g : simple_func α β) => ∀ (a : α), coe_fn f a ≤ coe_fn g a } @[simp] theorem coe_zero {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] : ⇑0 = 0 := rfl @[simp] theorem const_zero {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] : const α 0 = 0 := rfl @[simp] theorem coe_add {α : Type u_1} {β : Type u_2} [measurable_space α] [Add β] (f : simple_func α β) (g : simple_func α β) : ⇑(f + g) = ⇑f + ⇑g := rfl @[simp] theorem coe_mul {α : Type u_1} {β : Type u_2} [measurable_space α] [Mul β] (f : simple_func α β) (g : simple_func α β) : ⇑(f * g) = ⇑f * ⇑g := rfl @[simp] theorem coe_le {α : Type u_1} {β : Type u_2} [measurable_space α] [preorder β] {f : simple_func α β} {g : simple_func α β} : ⇑f ≤ ⇑g ↔ f ≤ g := iff.rfl @[simp] theorem range_zero {α : Type u_1} {β : Type u_2} [measurable_space α] [Nonempty α] [HasZero β] : simple_func.range 0 = singleton 0 := sorry theorem eq_zero_of_mem_range_zero {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] {y : β} : y ∈ simple_func.range 0 → y = 0 := iff.mpr forall_range_iff fun (x : α) => rfl theorem sup_apply {α : Type u_1} {β : Type u_2} [measurable_space α] [has_sup β] (f : simple_func α β) (g : simple_func α β) (a : α) : coe_fn (f ⊔ g) a = coe_fn f a ⊔ coe_fn g a := rfl theorem mul_apply {α : Type u_1} {β : Type u_2} [measurable_space α] [Mul β] (f : simple_func α β) (g : simple_func α β) (a : α) : coe_fn (f * g) a = coe_fn f a * coe_fn g a := rfl theorem add_apply {α : Type u_1} {β : Type u_2} [measurable_space α] [Add β] (f : simple_func α β) (g : simple_func α β) (a : α) : coe_fn (f + g) a = coe_fn f a + coe_fn g a := rfl theorem add_eq_map₂ {α : Type u_1} {β : Type u_2} [measurable_space α] [Add β] (f : simple_func α β) (g : simple_func α β) : f + g = map (fun (p : β × β) => prod.fst p + prod.snd p) (pair f g) := rfl theorem mul_eq_map₂ {α : Type u_1} {β : Type u_2} [measurable_space α] [Mul β] (f : simple_func α β) (g : simple_func α β) : f * g = map (fun (p : β × β) => prod.fst p * prod.snd p) (pair f g) := rfl theorem sup_eq_map₂ {α : Type u_1} {β : Type u_2} [measurable_space α] [has_sup β] (f : simple_func α β) (g : simple_func α β) : f ⊔ g = map (fun (p : β × β) => prod.fst p ⊔ prod.snd p) (pair f g) := rfl theorem const_mul_eq_map {α : Type u_1} {β : Type u_2} [measurable_space α] [Mul β] (f : simple_func α β) (b : β) : const α b * f = map (fun (a : β) => b * a) f := rfl theorem map_add {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] [Add β] [Add γ] {g : β → γ} (hg : ∀ (x y : β), g (x + y) = g x + g y) (f₁ : simple_func α β) (f₂ : simple_func α β) : map g (f₁ + f₂) = map g f₁ + map g f₂ := ext fun (x : α) => hg (coe_fn f₁ x) (coe_fn f₂ x) protected instance add_monoid {α : Type u_1} {β : Type u_2} [measurable_space α] [add_monoid β] : add_monoid (simple_func α β) := function.injective.add_monoid (fun (f : simple_func α β) => (fun (this : α → β) => this) ⇑f) coe_injective sorry sorry protected instance add_comm_monoid {α : Type u_1} {β : Type u_2} [measurable_space α] [add_comm_monoid β] : add_comm_monoid (simple_func α β) := function.injective.add_comm_monoid (fun (f : simple_func α β) => (fun (this : α → β) => this) ⇑f) coe_injective sorry sorry protected instance has_neg {α : Type u_1} {β : Type u_2} [measurable_space α] [Neg β] : Neg (simple_func α β) := { neg := fun (f : simple_func α β) => map Neg.neg f } @[simp] theorem coe_neg {α : Type u_1} {β : Type u_2} [measurable_space α] [Neg β] (f : simple_func α β) : ⇑(-f) = -⇑f := rfl protected instance has_sub {α : Type u_1} {β : Type u_2} [measurable_space α] [Sub β] : Sub (simple_func α β) := { sub := fun (f g : simple_func α β) => seq (map Sub.sub f) g } @[simp] theorem coe_sub {α : Type u_1} {β : Type u_2} [measurable_space α] [Sub β] (f : simple_func α β) (g : simple_func α β) : ⇑(f - g) = ⇑f - ⇑g := rfl theorem sub_apply {α : Type u_1} {β : Type u_2} [measurable_space α] [Sub β] (f : simple_func α β) (g : simple_func α β) (x : α) : coe_fn (f - g) x = coe_fn f x - coe_fn g x := rfl protected instance add_group {α : Type u_1} {β : Type u_2} [measurable_space α] [add_group β] : add_group (simple_func α β) := function.injective.add_group_sub (fun (f : simple_func α β) => (fun (this : α → β) => this) ⇑f) coe_injective sorry sorry sorry sorry protected instance add_comm_group {α : Type u_1} {β : Type u_2} [measurable_space α] [add_comm_group β] : add_comm_group (simple_func α β) := function.injective.add_comm_group_sub (fun (f : simple_func α β) => (fun (this : α → β) => this) ⇑f) coe_injective sorry sorry sorry sorry protected instance has_scalar {α : Type u_1} {β : Type u_2} [measurable_space α] {K : Type u_5} [has_scalar K β] : has_scalar K (simple_func α β) := has_scalar.mk fun (k : K) (f : simple_func α β) => map (has_scalar.smul k) f @[simp] theorem coe_smul {α : Type u_1} {β : Type u_2} [measurable_space α] {K : Type u_5} [has_scalar K β] (c : K) (f : simple_func α β) : ⇑(c • f) = c • ⇑f := rfl theorem smul_apply {α : Type u_1} {β : Type u_2} [measurable_space α] {K : Type u_5} [has_scalar K β] (k : K) (f : simple_func α β) (a : α) : coe_fn (k • f) a = k • coe_fn f a := rfl protected instance semimodule {α : Type u_1} {β : Type u_2} [measurable_space α] {K : Type u_5} [semiring K] [add_comm_monoid β] [semimodule K β] : semimodule K (simple_func α β) := function.injective.semimodule K (add_monoid_hom.mk (fun (f : simple_func α β) => (fun (this : α → β) => this) ⇑f) sorry sorry) coe_injective sorry theorem smul_eq_map {α : Type u_1} {β : Type u_2} [measurable_space α] {K : Type u_5} [has_scalar K β] (k : K) (f : simple_func α β) : k • f = map (has_scalar.smul k) f := rfl protected instance preorder {α : Type u_1} {β : Type u_2} [measurable_space α] [preorder β] : preorder (simple_func α β) := preorder.mk LessEq (fun (a b : simple_func α β) => a ≤ b ∧ ¬b ≤ a) sorry sorry protected instance partial_order {α : Type u_1} {β : Type u_2} [measurable_space α] [partial_order β] : partial_order (simple_func α β) := partial_order.mk preorder.le preorder.lt sorry sorry sorry protected instance order_bot {α : Type u_1} {β : Type u_2} [measurable_space α] [order_bot β] : order_bot (simple_func α β) := order_bot.mk (const α ⊥) partial_order.le partial_order.lt sorry sorry sorry sorry protected instance order_top {α : Type u_1} {β : Type u_2} [measurable_space α] [order_top β] : order_top (simple_func α β) := order_top.mk (const α ⊤) partial_order.le partial_order.lt sorry sorry sorry sorry protected instance semilattice_inf {α : Type u_1} {β : Type u_2} [measurable_space α] [semilattice_inf β] : semilattice_inf (simple_func α β) := semilattice_inf.mk has_inf.inf partial_order.le partial_order.lt sorry sorry sorry sorry sorry sorry protected instance semilattice_sup {α : Type u_1} {β : Type u_2} [measurable_space α] [semilattice_sup β] : semilattice_sup (simple_func α β) := semilattice_sup.mk has_sup.sup partial_order.le partial_order.lt sorry sorry sorry sorry sorry sorry protected instance semilattice_sup_bot {α : Type u_1} {β : Type u_2} [measurable_space α] [semilattice_sup_bot β] : semilattice_sup_bot (simple_func α β) := semilattice_sup_bot.mk order_bot.bot semilattice_sup.le semilattice_sup.lt sorry sorry sorry sorry semilattice_sup.sup sorry sorry sorry protected instance lattice {α : Type u_1} {β : Type u_2} [measurable_space α] [lattice β] : lattice (simple_func α β) := lattice.mk semilattice_sup.sup semilattice_sup.le semilattice_sup.lt sorry sorry sorry sorry sorry sorry semilattice_inf.inf sorry sorry sorry protected instance bounded_lattice {α : Type u_1} {β : Type u_2} [measurable_space α] [bounded_lattice β] : bounded_lattice (simple_func α β) := bounded_lattice.mk lattice.sup lattice.le lattice.lt sorry sorry sorry sorry sorry sorry lattice.inf sorry sorry sorry order_top.top sorry order_bot.bot sorry theorem finset_sup_apply {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] [semilattice_sup_bot β] {f : γ → simple_func α β} (s : finset γ) (a : α) : coe_fn (finset.sup s f) a = finset.sup s fun (c : γ) => coe_fn (f c) a := sorry /-- Restrict a simple function `f : α →ₛ β` to a set `s`. If `s` is measurable, then `f.restrict s a = if a ∈ s then f a else 0`, otherwise `f.restrict s = const α 0`. -/ def restrict {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] (f : simple_func α β) (s : set α) : simple_func α β := dite (is_measurable s) (fun (hs : is_measurable s) => piecewise s hs f 0) fun (hs : ¬is_measurable s) => 0 theorem restrict_of_not_measurable {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] {f : simple_func α β} {s : set α} (hs : ¬is_measurable s) : restrict f s = 0 := dif_neg hs @[simp] theorem coe_restrict {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] (f : simple_func α β) {s : set α} (hs : is_measurable s) : ⇑(restrict f s) = set.indicator s ⇑f := sorry @[simp] theorem restrict_univ {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] (f : simple_func α β) : restrict f set.univ = f := sorry @[simp] theorem restrict_empty {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] (f : simple_func α β) : restrict f ∅ = 0 := sorry theorem map_restrict_of_zero {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] [HasZero β] [HasZero γ] {g : β → γ} (hg : g 0 = 0) (f : simple_func α β) (s : set α) : map g (restrict f s) = restrict (map g f) s := sorry theorem map_coe_ennreal_restrict {α : Type u_1} [measurable_space α] (f : simple_func α nnreal) (s : set α) : map coe (restrict f s) = restrict (map coe f) s := map_restrict_of_zero ennreal.coe_zero f s theorem map_coe_nnreal_restrict {α : Type u_1} [measurable_space α] (f : simple_func α nnreal) (s : set α) : map coe (restrict f s) = restrict (map coe f) s := map_restrict_of_zero nnreal.coe_zero f s theorem restrict_apply {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] (f : simple_func α β) {s : set α} (hs : is_measurable s) (a : α) : coe_fn (restrict f s) a = ite (a ∈ s) (coe_fn f a) 0 := sorry theorem restrict_preimage {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] (f : simple_func α β) {s : set α} (hs : is_measurable s) {t : set β} (ht : ¬0 ∈ t) : ⇑(restrict f s) ⁻¹' t = s ∩ ⇑f ⁻¹' t := sorry theorem restrict_preimage_singleton {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] (f : simple_func α β) {s : set α} (hs : is_measurable s) {r : β} (hr : r ≠ 0) : ⇑(restrict f s) ⁻¹' singleton r = s ∩ ⇑f ⁻¹' singleton r := restrict_preimage f hs (ne.symm hr) theorem mem_restrict_range {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] {r : β} {s : set α} {f : simple_func α β} (hs : is_measurable s) : r ∈ simple_func.range (restrict f s) ↔ r = 0 ∧ s ≠ set.univ ∨ r ∈ ⇑f '' s := sorry theorem mem_image_of_mem_range_restrict {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] {r : β} {s : set α} {f : simple_func α β} (hr : r ∈ simple_func.range (restrict f s)) (h0 : r ≠ 0) : r ∈ ⇑f '' s := sorry theorem restrict_mono {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] [preorder β] (s : set α) {f : simple_func α β} {g : simple_func α β} (H : f ≤ g) : restrict f s ≤ restrict g s := sorry /-- Fix a sequence `i : ℕ → β`. Given a function `α → β`, its `n`-th approximation by simple functions is defined so that in case `β = ennreal` it sends each `a` to the supremum of the set `{i k | k ≤ n ∧ i k ≤ f a}`, see `approx_apply` and `supr_approx_apply` for details. -/ def approx {α : Type u_1} {β : Type u_2} [measurable_space α] [semilattice_sup_bot β] [HasZero β] (i : ℕ → β) (f : α → β) (n : ℕ) : simple_func α β := finset.sup (finset.range n) fun (k : ℕ) => restrict (const α (i k)) (set_of fun (a : α) => i k ≤ f a) theorem approx_apply {α : Type u_1} {β : Type u_2} [measurable_space α] [semilattice_sup_bot β] [HasZero β] [topological_space β] [order_closed_topology β] [measurable_space β] [opens_measurable_space β] {i : ℕ → β} {f : α → β} {n : ℕ} (a : α) (hf : measurable f) : coe_fn (approx i f n) a = finset.sup (finset.range n) fun (k : ℕ) => ite (i k ≤ f a) (i k) 0 := sorry theorem monotone_approx {α : Type u_1} {β : Type u_2} [measurable_space α] [semilattice_sup_bot β] [HasZero β] (i : ℕ → β) (f : α → β) : monotone (approx i f) := fun (n m : ℕ) (h : n ≤ m) => finset.sup_mono (iff.mpr finset.range_subset h) theorem approx_comp {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] [semilattice_sup_bot β] [HasZero β] [topological_space β] [order_closed_topology β] [measurable_space β] [opens_measurable_space β] [measurable_space γ] {i : ℕ → β} {f : γ → β} {g : α → γ} {n : ℕ} (a : α) (hf : measurable f) (hg : measurable g) : coe_fn (approx i (f ∘ g) n) a = coe_fn (approx i f n) (g a) := sorry theorem supr_approx_apply {α : Type u_1} {β : Type u_2} [measurable_space α] [topological_space β] [complete_lattice β] [order_closed_topology β] [HasZero β] [measurable_space β] [opens_measurable_space β] (i : ℕ → β) (f : α → β) (a : α) (hf : measurable f) (h_zero : 0 = ⊥) : (supr fun (n : ℕ) => coe_fn (approx i f n) a) = supr fun (k : ℕ) => supr fun (h : i k ≤ f a) => i k := sorry /-- A sequence of `ennreal`s such that its range is the set of non-negative rational numbers. -/ def ennreal_rat_embed (n : ℕ) : ennreal := ennreal.of_real ↑(option.get_or_else (encodable.decode ℚ n) 0) theorem ennreal_rat_embed_encode (q : ℚ) : ennreal_rat_embed (encodable.encode q) = ↑(nnreal.of_real ↑q) := sorry /-- Approximate a function `α → ennreal` by a sequence of simple functions. -/ def eapprox {α : Type u_1} [measurable_space α] : (α → ennreal) → ℕ → simple_func α ennreal := approx ennreal_rat_embed theorem monotone_eapprox {α : Type u_1} [measurable_space α] (f : α → ennreal) : monotone (eapprox f) := monotone_approx ennreal_rat_embed f theorem supr_eapprox_apply {α : Type u_1} [measurable_space α] (f : α → ennreal) (hf : measurable f) (a : α) : (supr fun (n : ℕ) => coe_fn (eapprox f n) a) = f a := sorry theorem eapprox_comp {α : Type u_1} {γ : Type u_3} [measurable_space α] [measurable_space γ] {f : γ → ennreal} {g : α → γ} {n : ℕ} (hf : measurable f) (hg : measurable g) : ⇑(eapprox (f ∘ g) n) = ⇑(eapprox f n) ∘ g := funext fun (a : α) => approx_comp a hf hg /-- Integral of a simple function whose codomain is `ennreal`. -/ def lintegral {α : Type u_1} [measurable_space α] (f : simple_func α ennreal) (μ : measure α) : ennreal := finset.sum (simple_func.range f) fun (x : ennreal) => x * coe_fn μ (⇑f ⁻¹' singleton x) theorem lintegral_eq_of_subset {α : Type u_1} [measurable_space α] {μ : measure α} (f : simple_func α ennreal) {s : finset ennreal} (hs : ∀ (x : α), coe_fn f x ≠ 0 → coe_fn μ (⇑f ⁻¹' singleton (coe_fn f x)) ≠ 0 → coe_fn f x ∈ s) : lintegral f μ = finset.sum s fun (x : ennreal) => x * coe_fn μ (⇑f ⁻¹' singleton x) := sorry /-- Calculate the integral of `(g ∘ f)`, where `g : β → ennreal` and `f : α →ₛ β`. -/ theorem map_lintegral {α : Type u_1} {β : Type u_2} [measurable_space α] {μ : measure α} (g : β → ennreal) (f : simple_func α β) : lintegral (map g f) μ = finset.sum (simple_func.range f) fun (x : β) => g x * coe_fn μ (⇑f ⁻¹' singleton x) := sorry theorem add_lintegral {α : Type u_1} [measurable_space α] {μ : measure α} (f : simple_func α ennreal) (g : simple_func α ennreal) : lintegral (f + g) μ = lintegral f μ + lintegral g μ := sorry theorem const_mul_lintegral {α : Type u_1} [measurable_space α] {μ : measure α} (f : simple_func α ennreal) (x : ennreal) : lintegral (const α x * f) μ = x * lintegral f μ := sorry /-- Integral of a simple function `α →ₛ ennreal` as a bilinear map. -/ def lintegralₗ {α : Type u_1} [measurable_space α] : linear_map ennreal (simple_func α ennreal) (linear_map ennreal (measure α) ennreal) := linear_map.mk (fun (f : simple_func α ennreal) => linear_map.mk (lintegral f) sorry sorry) sorry sorry @[simp] theorem zero_lintegral {α : Type u_1} [measurable_space α] {μ : measure α} : lintegral 0 μ = 0 := iff.mp linear_map.ext_iff (linear_map.map_zero lintegralₗ) μ theorem lintegral_add {α : Type u_1} [measurable_space α] {μ : measure α} {ν : measure α} (f : simple_func α ennreal) : lintegral f (μ + ν) = lintegral f μ + lintegral f ν := linear_map.map_add (coe_fn lintegralₗ f) μ ν theorem lintegral_smul {α : Type u_1} [measurable_space α] {μ : measure α} (f : simple_func α ennreal) (c : ennreal) : lintegral f (c • μ) = c • lintegral f μ := linear_map.map_smul (coe_fn lintegralₗ f) c μ @[simp] theorem lintegral_zero {α : Type u_1} [measurable_space α] (f : simple_func α ennreal) : lintegral f 0 = 0 := linear_map.map_zero (coe_fn lintegralₗ f) theorem lintegral_sum {α : Type u_1} [measurable_space α] {ι : Type u_2} (f : simple_func α ennreal) (μ : ι → measure α) : lintegral f (measure.sum μ) = tsum fun (i : ι) => lintegral f (μ i) := sorry theorem restrict_lintegral {α : Type u_1} [measurable_space α] {μ : measure α} (f : simple_func α ennreal) {s : set α} (hs : is_measurable s) : lintegral (restrict f s) μ = finset.sum (simple_func.range f) fun (r : ennreal) => r * coe_fn μ (⇑f ⁻¹' singleton r ∩ s) := sorry theorem lintegral_restrict {α : Type u_1} [measurable_space α] (f : simple_func α ennreal) (s : set α) (μ : measure α) : lintegral f (measure.restrict μ s) = finset.sum (simple_func.range f) fun (y : ennreal) => y * coe_fn μ (⇑f ⁻¹' singleton y ∩ s) := sorry theorem restrict_lintegral_eq_lintegral_restrict {α : Type u_1} [measurable_space α] {μ : measure α} (f : simple_func α ennreal) {s : set α} (hs : is_measurable s) : lintegral (restrict f s) μ = lintegral f (measure.restrict μ s) := sorry theorem const_lintegral {α : Type u_1} [measurable_space α] {μ : measure α} (c : ennreal) : lintegral (const α c) μ = c * coe_fn μ set.univ := sorry theorem const_lintegral_restrict {α : Type u_1} [measurable_space α] {μ : measure α} (c : ennreal) (s : set α) : lintegral (const α c) (measure.restrict μ s) = c * coe_fn μ s := sorry theorem restrict_const_lintegral {α : Type u_1} [measurable_space α] {μ : measure α} (c : ennreal) {s : set α} (hs : is_measurable s) : lintegral (restrict (const α c) s) μ = c * coe_fn μ s := sorry theorem le_sup_lintegral {α : Type u_1} [measurable_space α] {μ : measure α} (f : simple_func α ennreal) (g : simple_func α ennreal) : lintegral f μ ⊔ lintegral g μ ≤ lintegral (f ⊔ g) μ := sorry /-- `simple_func.lintegral` is monotone both in function and in measure. -/ theorem lintegral_mono {α : Type u_1} [measurable_space α] {f : simple_func α ennreal} {g : simple_func α ennreal} (hfg : f ≤ g) {μ : measure α} {ν : measure α} (hμν : μ ≤ ν) : lintegral f μ ≤ lintegral g ν := sorry /-- `simple_func.lintegral` depends only on the measures of `f ⁻¹' {y}`. -/ theorem lintegral_eq_of_measure_preimage {α : Type u_1} {β : Type u_2} [measurable_space α] {μ : measure α} [measurable_space β] {f : simple_func α ennreal} {g : simple_func β ennreal} {ν : measure β} (H : ∀ (y : ennreal), coe_fn μ (⇑f ⁻¹' singleton y) = coe_fn ν (⇑g ⁻¹' singleton y)) : lintegral f μ = lintegral g ν := sorry /-- If two simple functions are equal a.e., then their `lintegral`s are equal. -/ theorem lintegral_congr {α : Type u_1} [measurable_space α] {μ : measure α} {f : simple_func α ennreal} {g : simple_func α ennreal} (h : filter.eventually_eq (measure.ae μ) ⇑f ⇑g) : lintegral f μ = lintegral g μ := sorry theorem lintegral_map {α : Type u_1} [measurable_space α] {μ : measure α} {β : Type u_2} [measurable_space β] {μ' : measure β} (f : simple_func α ennreal) (g : simple_func β ennreal) (m : α → β) (eq : ∀ (a : α), coe_fn f a = coe_fn g (m a)) (h : ∀ (s : set β), is_measurable s → coe_fn μ' s = coe_fn μ (m ⁻¹' s)) : lintegral f μ = lintegral g μ' := sorry theorem support_eq {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] (f : simple_func α β) : function.support ⇑f = set.Union fun (y : β) => set.Union fun (H : y ∈ finset.filter (fun (y : β) => y ≠ 0) (simple_func.range f)) => ⇑f ⁻¹' singleton y := sorry /-- A `simple_func` has finite measure support if it is equal to `0` outside of a set of finite measure. -/ protected def fin_meas_supp {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] (f : simple_func α β) (μ : measure α) := filter.eventually_eq (measure.cofinite μ) (⇑f) 0 theorem fin_meas_supp_iff_support {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] {f : simple_func α β} {μ : measure α} : simple_func.fin_meas_supp f μ ↔ coe_fn μ (function.support ⇑f) < ⊤ := iff.rfl theorem fin_meas_supp_iff {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] {f : simple_func α β} {μ : measure α} : simple_func.fin_meas_supp f μ ↔ ∀ (y : β), y ≠ 0 → coe_fn μ (⇑f ⁻¹' singleton y) < ⊤ := sorry namespace fin_meas_supp theorem meas_preimage_singleton_ne_zero {α : Type u_1} {β : Type u_2} [measurable_space α] [HasZero β] {μ : measure α} {f : simple_func α β} (h : simple_func.fin_meas_supp f μ) {y : β} (hy : y ≠ 0) : coe_fn μ (⇑f ⁻¹' singleton y) < ⊤ := iff.mp fin_meas_supp_iff h y hy protected theorem map {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] [HasZero β] [HasZero γ] {μ : measure α} {f : simple_func α β} {g : β → γ} (hf : simple_func.fin_meas_supp f μ) (hg : g 0 = 0) : simple_func.fin_meas_supp (map g f) μ := flip lt_of_le_of_lt hf (measure_mono (function.support_comp_subset hg ⇑f)) theorem of_map {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] [HasZero β] [HasZero γ] {μ : measure α} {f : simple_func α β} {g : β → γ} (h : simple_func.fin_meas_supp (map g f) μ) (hg : ∀ (b : β), g b = 0 → b = 0) : simple_func.fin_meas_supp f μ := flip lt_of_le_of_lt h (measure_mono (function.support_subset_comp hg fun (x : α) => coe_fn f x)) theorem map_iff {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] [HasZero β] [HasZero γ] {μ : measure α} {f : simple_func α β} {g : β → γ} (hg : ∀ {b : β}, g b = 0 ↔ b = 0) : simple_func.fin_meas_supp (map g f) μ ↔ simple_func.fin_meas_supp f μ := { mp := fun (h : simple_func.fin_meas_supp (map g f) μ) => of_map h fun (b : β) => iff.mp hg, mpr := fun (h : simple_func.fin_meas_supp f μ) => fin_meas_supp.map h (iff.mpr hg rfl) } protected theorem pair {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] [HasZero β] [HasZero γ] {μ : measure α} {f : simple_func α β} {g : simple_func α γ} (hf : simple_func.fin_meas_supp f μ) (hg : simple_func.fin_meas_supp g μ) : simple_func.fin_meas_supp (pair f g) μ := sorry protected theorem map₂ {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} [measurable_space α] [HasZero β] [HasZero γ] [HasZero δ] {μ : measure α} {f : simple_func α β} (hf : simple_func.fin_meas_supp f μ) {g : simple_func α γ} (hg : simple_func.fin_meas_supp g μ) {op : β → γ → δ} (H : op 0 0 = 0) : simple_func.fin_meas_supp (map (function.uncurry op) (pair f g)) μ := fin_meas_supp.map (fin_meas_supp.pair hf hg) H protected theorem add {α : Type u_1} [measurable_space α] {μ : measure α} {β : Type u_2} [add_monoid β] {f : simple_func α β} {g : simple_func α β} (hf : simple_func.fin_meas_supp f μ) (hg : simple_func.fin_meas_supp g μ) : simple_func.fin_meas_supp (f + g) μ := eq.mpr (id (Eq._oldrec (Eq.refl (simple_func.fin_meas_supp (f + g) μ)) (add_eq_map₂ f g))) (fin_meas_supp.map₂ hf hg (zero_add 0)) protected theorem mul {α : Type u_1} [measurable_space α] {μ : measure α} {β : Type u_2} [monoid_with_zero β] {f : simple_func α β} {g : simple_func α β} (hf : simple_func.fin_meas_supp f μ) (hg : simple_func.fin_meas_supp g μ) : simple_func.fin_meas_supp (f * g) μ := eq.mpr (id (Eq._oldrec (Eq.refl (simple_func.fin_meas_supp (f * g) μ)) (mul_eq_map₂ f g))) (fin_meas_supp.map₂ hf hg (zero_mul 0)) theorem lintegral_lt_top {α : Type u_1} [measurable_space α] {μ : measure α} {f : simple_func α ennreal} (hm : simple_func.fin_meas_supp f μ) (hf : filter.eventually (fun (a : α) => coe_fn f a < ⊤) (measure.ae μ)) : lintegral f μ < ⊤ := sorry theorem of_lintegral_lt_top {α : Type u_1} [measurable_space α] {μ : measure α} {f : simple_func α ennreal} (h : lintegral f μ < ⊤) : simple_func.fin_meas_supp f μ := sorry theorem iff_lintegral_lt_top {α : Type u_1} [measurable_space α] {μ : measure α} {f : simple_func α ennreal} (hf : filter.eventually (fun (a : α) => coe_fn f a < ⊤) (measure.ae μ)) : simple_func.fin_meas_supp f μ ↔ lintegral f μ < ⊤ := { mp := fun (h : simple_func.fin_meas_supp f μ) => lintegral_lt_top h hf, mpr := fun (h : lintegral f μ < ⊤) => of_lintegral_lt_top h } end fin_meas_supp /-- To prove something for an arbitrary simple function, it suffices to show that the property holds for (multiples of) characteristic functions and is closed under addition (of functions with disjoint support). It is possible to make the hypotheses in `h_sum` a bit stronger, and such conditions can be added once we need them (for example it is only necessary to consider the case where `g` is a multiple of a characteristic function, and that this multiple doesn't appear in the image of `f`) -/ protected theorem induction {α : Type u_1} {γ : Type u_2} [measurable_space α] [add_monoid γ] {P : simple_func α γ → Prop} (h_ind : ∀ (c : γ) {s : set α} (hs : is_measurable s), P (piecewise s hs (const α c) (const α 0))) (h_sum : ∀ {f g : simple_func α γ}, set.univ ⊆ ⇑f ⁻¹' singleton 0 ∪ ⇑g ⁻¹' singleton 0 → P f → P g → P (f + g)) (f : simple_func α γ) : P f := sorry end simple_func /-- The lower Lebesgue integral of a function `f` with respect to a measure `μ`. -/ def lintegral {α : Type u_1} [measurable_space α] (μ : measure α) (f : α → ennreal) : ennreal := supr fun (g : simple_func α ennreal) => supr fun (hf : ⇑g ≤ f) => simple_func.lintegral g μ /-! In the notation for integrals, an expression like `∫⁻ x, g ∥x∥ ∂μ` will not be parsed correctly, and needs parentheses. We do not set the binding power of `r` to `0`, because then `∫⁻ x, f x = 0` will be parsed incorrectly. -/ theorem simple_func.lintegral_eq_lintegral {α : Type u_1} [measurable_space α] (f : simple_func α ennreal) (μ : measure α) : (lintegral μ fun (a : α) => coe_fn f a) = simple_func.lintegral f μ := sorry theorem lintegral_mono' {α : Type u_1} [measurable_space α] {μ : measure α} {ν : measure α} (hμν : μ ≤ ν) {f : α → ennreal} {g : α → ennreal} (hfg : f ≤ g) : (lintegral μ fun (a : α) => f a) ≤ lintegral ν fun (a : α) => g a := sorry theorem lintegral_mono {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} {g : α → ennreal} (hfg : f ≤ g) : (lintegral μ fun (a : α) => f a) ≤ lintegral μ fun (a : α) => g a := lintegral_mono' (le_refl μ) hfg theorem lintegral_mono_nnreal {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → nnreal} {g : α → nnreal} (h : f ≤ g) : (lintegral μ fun (a : α) => ↑(f a)) ≤ lintegral μ fun (a : α) => ↑(g a) := sorry theorem monotone_lintegral {α : Type u_1} [measurable_space α] (μ : measure α) : monotone (lintegral μ) := lintegral_mono @[simp] theorem lintegral_const {α : Type u_1} [measurable_space α] {μ : measure α} (c : ennreal) : (lintegral μ fun (a : α) => c) = c * coe_fn μ set.univ := sorry @[simp] theorem lintegral_one {α : Type u_1} [measurable_space α] {μ : measure α} : (lintegral μ fun (a : α) => 1) = coe_fn μ set.univ := eq.mpr (id (Eq._oldrec (Eq.refl ((lintegral μ fun (a : α) => 1) = coe_fn μ set.univ)) (lintegral_const 1))) (eq.mpr (id (Eq._oldrec (Eq.refl (1 * coe_fn μ set.univ = coe_fn μ set.univ)) (one_mul (coe_fn μ set.univ)))) (Eq.refl (coe_fn μ set.univ))) theorem set_lintegral_const {α : Type u_1} [measurable_space α] {μ : measure α} (s : set α) (c : ennreal) : (lintegral (measure.restrict μ s) fun (a : α) => c) = c * coe_fn μ s := sorry theorem set_lintegral_one {α : Type u_1} [measurable_space α] {μ : measure α} (s : set α) : (lintegral (measure.restrict μ s) fun (a : α) => 1) = coe_fn μ s := sorry /-- `∫⁻ a in s, f a ∂μ` is defined as the supremum of integrals of simple functions `φ : α →ₛ ennreal` such that `φ ≤ f`. This lemma says that it suffices to take functions `φ : α →ₛ ℝ≥0`. -/ theorem lintegral_eq_nnreal {α : Type u_1} [measurable_space α] (f : α → ennreal) (μ : measure α) : (lintegral μ fun (a : α) => f a) = supr fun (φ : simple_func α nnreal) => supr fun (hf : ∀ (x : α), ↑(coe_fn φ x) ≤ f x) => simple_func.lintegral (simple_func.map coe φ) μ := sorry theorem exists_simple_func_forall_lintegral_sub_lt_of_pos {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} (h : (lintegral μ fun (x : α) => f x) < ⊤) {ε : ennreal} (hε : 0 < ε) : ∃ (φ : simple_func α nnreal), (∀ (x : α), ↑(coe_fn φ x) ≤ f x) ∧ ∀ (ψ : simple_func α nnreal), (∀ (x : α), ↑(coe_fn ψ x) ≤ f x) → simple_func.lintegral (simple_func.map coe (ψ - φ)) μ < ε := sorry theorem supr_lintegral_le {α : Type u_1} [measurable_space α] {μ : measure α} {ι : Sort u_2} (f : ι → α → ennreal) : (supr fun (i : ι) => lintegral μ fun (a : α) => f i a) ≤ lintegral μ fun (a : α) => supr fun (i : ι) => f i a := sorry theorem supr2_lintegral_le {α : Type u_1} [measurable_space α] {μ : measure α} {ι : Sort u_2} {ι' : ι → Sort u_3} (f : (i : ι) → ι' i → α → ennreal) : (supr fun (i : ι) => supr fun (h : ι' i) => lintegral μ fun (a : α) => f i h a) ≤ lintegral μ fun (a : α) => supr fun (i : ι) => supr fun (h : ι' i) => f i h a := sorry theorem le_infi_lintegral {α : Type u_1} [measurable_space α] {μ : measure α} {ι : Sort u_2} (f : ι → α → ennreal) : (lintegral μ fun (a : α) => infi fun (i : ι) => f i a) ≤ infi fun (i : ι) => lintegral μ fun (a : α) => f i a := sorry theorem le_infi2_lintegral {α : Type u_1} [measurable_space α] {μ : measure α} {ι : Sort u_2} {ι' : ι → Sort u_3} (f : (i : ι) → ι' i → α → ennreal) : (lintegral μ fun (a : α) => infi fun (i : ι) => infi fun (h : ι' i) => f i h a) ≤ infi fun (i : ι) => infi fun (h : ι' i) => lintegral μ fun (a : α) => f i h a := sorry theorem lintegral_mono_ae {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} {g : α → ennreal} (h : filter.eventually (fun (a : α) => f a ≤ g a) (measure.ae μ)) : (lintegral μ fun (a : α) => f a) ≤ lintegral μ fun (a : α) => g a := sorry theorem lintegral_congr_ae {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} {g : α → ennreal} (h : filter.eventually_eq (measure.ae μ) f g) : (lintegral μ fun (a : α) => f a) = lintegral μ fun (a : α) => g a := le_antisymm (lintegral_mono_ae (filter.eventually_eq.le h)) (lintegral_mono_ae (filter.eventually_eq.le (filter.eventually_eq.symm h))) theorem lintegral_congr {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} {g : α → ennreal} (h : ∀ (a : α), f a = g a) : (lintegral μ fun (a : α) => f a) = lintegral μ fun (a : α) => g a := sorry theorem set_lintegral_congr {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} {s : set α} {t : set α} (h : filter.eventually_eq (measure.ae μ) s t) : (lintegral (measure.restrict μ s) fun (x : α) => f x) = lintegral (measure.restrict μ t) fun (x : α) => f x := sorry /-- Monotone convergence theorem -- sometimes called Beppo-Levi convergence. See `lintegral_supr_directed` for a more general form. -/ theorem lintegral_supr {α : Type u_1} [measurable_space α] {μ : measure α} {f : ℕ → α → ennreal} (hf : ∀ (n : ℕ), measurable (f n)) (h_mono : monotone f) : (lintegral μ fun (a : α) => supr fun (n : ℕ) => f n a) = supr fun (n : ℕ) => lintegral μ fun (a : α) => f n a := sorry /-- Monotone convergence theorem -- sometimes called Beppo-Levi convergence. Version with ae_measurable functions. -/ theorem lintegral_supr' {α : Type u_1} [measurable_space α] {μ : measure α} {f : ℕ → α → ennreal} (hf : ∀ (n : ℕ), ae_measurable (f n)) (h_mono : filter.eventually (fun (x : α) => monotone fun (n : ℕ) => f n x) (measure.ae μ)) : (lintegral μ fun (a : α) => supr fun (n : ℕ) => f n a) = supr fun (n : ℕ) => lintegral μ fun (a : α) => f n a := sorry theorem lintegral_eq_supr_eapprox_lintegral {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} (hf : measurable f) : (lintegral μ fun (a : α) => f a) = supr fun (n : ℕ) => simple_func.lintegral (simple_func.eapprox f n) μ := sorry /-- If `f` has finite integral, then `∫⁻ x in s, f x ∂μ` is absolutely continuous in `s`: it tends to zero as `μ s` tends to zero. This lemma states states this fact in terms of `ε` and `δ`. -/ theorem exists_pos_set_lintegral_lt_of_measure_lt {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} (h : (lintegral μ fun (x : α) => f x) < ⊤) {ε : ennreal} (hε : 0 < ε) : ∃ (δ : ennreal), ∃ (H : δ > 0), ∀ (s : set α), coe_fn μ s < δ → (lintegral (measure.restrict μ s) fun (x : α) => f x) < ε := sorry /-- If `f` has finite integral, then `∫⁻ x in s, f x ∂μ` is absolutely continuous in `s`: it tends to zero as `μ s` tends to zero. -/ theorem tendsto_set_lintegral_zero {α : Type u_1} [measurable_space α] {μ : measure α} {ι : Type u_2} {f : α → ennreal} (h : (lintegral μ fun (x : α) => f x) < ⊤) {l : filter ι} {s : ι → set α} (hl : filter.tendsto (⇑μ ∘ s) l (nhds 0)) : filter.tendsto (fun (i : ι) => lintegral (measure.restrict μ (s i)) fun (x : α) => f x) l (nhds 0) := sorry @[simp] theorem lintegral_add {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} {g : α → ennreal} (hf : measurable f) (hg : measurable g) : (lintegral μ fun (a : α) => f a + g a) = (lintegral μ fun (a : α) => f a) + lintegral μ fun (a : α) => g a := sorry theorem lintegral_add' {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} {g : α → ennreal} (hf : ae_measurable f) (hg : ae_measurable g) : (lintegral μ fun (a : α) => f a + g a) = (lintegral μ fun (a : α) => f a) + lintegral μ fun (a : α) => g a := sorry theorem lintegral_zero {α : Type u_1} [measurable_space α] {μ : measure α} : (lintegral μ fun (a : α) => 0) = 0 := sorry theorem lintegral_zero_fun {α : Type u_1} [measurable_space α] {μ : measure α} : (lintegral μ fun (a : α) => HasZero.zero a) = 0 := sorry @[simp] theorem lintegral_smul_measure {α : Type u_1} [measurable_space α] {μ : measure α} (c : ennreal) (f : α → ennreal) : (lintegral (c • μ) fun (a : α) => f a) = c * lintegral μ fun (a : α) => f a := sorry @[simp] theorem lintegral_sum_measure {α : Type u_1} [measurable_space α] {ι : Type u_2} (f : α → ennreal) (μ : ι → measure α) : (lintegral (measure.sum μ) fun (a : α) => f a) = tsum fun (i : ι) => lintegral (μ i) fun (a : α) => f a := sorry @[simp] theorem lintegral_add_measure {α : Type u_1} [measurable_space α] (f : α → ennreal) (μ : measure α) (ν : measure α) : (lintegral (μ + ν) fun (a : α) => f a) = (lintegral μ fun (a : α) => f a) + lintegral ν fun (a : α) => f a := sorry @[simp] theorem lintegral_zero_measure {α : Type u_1} [measurable_space α] (f : α → ennreal) : (lintegral 0 fun (a : α) => f a) = 0 := sorry theorem lintegral_finset_sum {α : Type u_1} {β : Type u_2} [measurable_space α] {μ : measure α} (s : finset β) {f : β → α → ennreal} (hf : ∀ (b : β), measurable (f b)) : (lintegral μ fun (a : α) => finset.sum s fun (b : β) => f b a) = finset.sum s fun (b : β) => lintegral μ fun (a : α) => f b a := sorry @[simp] theorem lintegral_const_mul {α : Type u_1} [measurable_space α] {μ : measure α} (r : ennreal) {f : α → ennreal} (hf : measurable f) : (lintegral μ fun (a : α) => r * f a) = r * lintegral μ fun (a : α) => f a := sorry theorem lintegral_const_mul'' {α : Type u_1} [measurable_space α] {μ : measure α} (r : ennreal) {f : α → ennreal} (hf : ae_measurable f) : (lintegral μ fun (a : α) => r * f a) = r * lintegral μ fun (a : α) => f a := sorry theorem lintegral_const_mul_le {α : Type u_1} [measurable_space α] {μ : measure α} (r : ennreal) (f : α → ennreal) : (r * lintegral μ fun (a : α) => f a) ≤ lintegral μ fun (a : α) => r * f a := sorry theorem lintegral_const_mul' {α : Type u_1} [measurable_space α] {μ : measure α} (r : ennreal) (f : α → ennreal) (hr : r ≠ ⊤) : (lintegral μ fun (a : α) => r * f a) = r * lintegral μ fun (a : α) => f a := sorry theorem lintegral_mul_const {α : Type u_1} [measurable_space α] {μ : measure α} (r : ennreal) {f : α → ennreal} (hf : measurable f) : (lintegral μ fun (a : α) => f a * r) = (lintegral μ fun (a : α) => f a) * r := sorry theorem lintegral_mul_const'' {α : Type u_1} [measurable_space α] {μ : measure α} (r : ennreal) {f : α → ennreal} (hf : ae_measurable f) : (lintegral μ fun (a : α) => f a * r) = (lintegral μ fun (a : α) => f a) * r := sorry theorem lintegral_mul_const_le {α : Type u_1} [measurable_space α] {μ : measure α} (r : ennreal) (f : α → ennreal) : (lintegral μ fun (a : α) => f a) * r ≤ lintegral μ fun (a : α) => f a * r := sorry theorem lintegral_mul_const' {α : Type u_1} [measurable_space α] {μ : measure α} (r : ennreal) (f : α → ennreal) (hr : r ≠ ⊤) : (lintegral μ fun (a : α) => f a * r) = (lintegral μ fun (a : α) => f a) * r := sorry /- A double integral of a product where each factor contains only one variable is a product of integrals -/ theorem lintegral_lintegral_mul {α : Type u_1} [measurable_space α] {μ : measure α} {β : Type u_2} [measurable_space β] {ν : measure β} {f : α → ennreal} {g : β → ennreal} (hf : measurable f) (hg : measurable g) : (lintegral μ fun (x : α) => lintegral ν fun (y : β) => f x * g y) = (lintegral μ fun (x : α) => f x) * lintegral ν fun (y : β) => g y := sorry -- TODO: Need a better way of rewriting inside of a integral theorem lintegral_rw₁ {α : Type u_1} {β : Type u_2} [measurable_space α] {μ : measure α} {f : α → β} {f' : α → β} (h : filter.eventually_eq (measure.ae μ) f f') (g : β → ennreal) : (lintegral μ fun (a : α) => g (f a)) = lintegral μ fun (a : α) => g (f' a) := lintegral_congr_ae (filter.eventually.mono h fun (a : α) (h : f a = f' a) => eq.mpr (id (Eq._oldrec (Eq.refl (g (f a) = g (f' a))) h)) (Eq.refl (g (f' a)))) -- TODO: Need a better way of rewriting inside of a integral theorem lintegral_rw₂ {α : Type u_1} {β : Type u_2} {γ : Type u_3} [measurable_space α] {μ : measure α} {f₁ : α → β} {f₁' : α → β} {f₂ : α → γ} {f₂' : α → γ} (h₁ : filter.eventually_eq (measure.ae μ) f₁ f₁') (h₂ : filter.eventually_eq (measure.ae μ) f₂ f₂') (g : β → γ → ennreal) : (lintegral μ fun (a : α) => g (f₁ a) (f₂ a)) = lintegral μ fun (a : α) => g (f₁' a) (f₂' a) := sorry @[simp] theorem lintegral_indicator {α : Type u_1} [measurable_space α] {μ : measure α} (f : α → ennreal) {s : set α} (hs : is_measurable s) : (lintegral μ fun (a : α) => set.indicator s f a) = lintegral (measure.restrict μ s) fun (a : α) => f a := sorry /-- Chebyshev's inequality -/ theorem mul_meas_ge_le_lintegral {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} (hf : measurable f) (ε : ennreal) : ε * coe_fn μ (set_of fun (x : α) => ε ≤ f x) ≤ lintegral μ fun (a : α) => f a := sorry theorem meas_ge_le_lintegral_div {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} (hf : measurable f) {ε : ennreal} (hε : ε ≠ 0) (hε' : ε ≠ ⊤) : coe_fn μ (set_of fun (x : α) => ε ≤ f x) ≤ (lintegral μ fun (a : α) => f a) / ε := sorry @[simp] theorem lintegral_eq_zero_iff {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} (hf : measurable f) : (lintegral μ fun (a : α) => f a) = 0 ↔ filter.eventually_eq (measure.ae μ) f 0 := sorry @[simp] theorem lintegral_eq_zero_iff' {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} (hf : ae_measurable f) : (lintegral μ fun (a : α) => f a) = 0 ↔ filter.eventually_eq (measure.ae μ) f 0 := sorry theorem lintegral_pos_iff_support {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} (hf : measurable f) : (0 < lintegral μ fun (a : α) => f a) ↔ 0 < coe_fn μ (function.support f) := sorry /-- Weaker version of the monotone convergence theorem-/ theorem lintegral_supr_ae {α : Type u_1} [measurable_space α] {μ : measure α} {f : ℕ → α → ennreal} (hf : ∀ (n : ℕ), measurable (f n)) (h_mono : ∀ (n : ℕ), filter.eventually (fun (a : α) => f n a ≤ f (Nat.succ n) a) (measure.ae μ)) : (lintegral μ fun (a : α) => supr fun (n : ℕ) => f n a) = supr fun (n : ℕ) => lintegral μ fun (a : α) => f n a := sorry theorem lintegral_sub {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} {g : α → ennreal} (hf : measurable f) (hg : measurable g) (hg_fin : (lintegral μ fun (a : α) => g a) < ⊤) (h_le : filter.eventually_le (measure.ae μ) g f) : (lintegral μ fun (a : α) => f a - g a) = (lintegral μ fun (a : α) => f a) - lintegral μ fun (a : α) => g a := sorry /-- Monotone convergence theorem for nonincreasing sequences of functions -/ theorem lintegral_infi_ae {α : Type u_1} [measurable_space α] {μ : measure α} {f : ℕ → α → ennreal} (h_meas : ∀ (n : ℕ), measurable (f n)) (h_mono : ∀ (n : ℕ), filter.eventually_le (measure.ae μ) (f (Nat.succ n)) (f n)) (h_fin : (lintegral μ fun (a : α) => f 0 a) < ⊤) : (lintegral μ fun (a : α) => infi fun (n : ℕ) => f n a) = infi fun (n : ℕ) => lintegral μ fun (a : α) => f n a := sorry /-- Monotone convergence theorem for nonincreasing sequences of functions -/ theorem lintegral_infi {α : Type u_1} [measurable_space α] {μ : measure α} {f : ℕ → α → ennreal} (h_meas : ∀ (n : ℕ), measurable (f n)) (h_mono : ∀ {m n : ℕ}, m ≤ n → f n ≤ f m) (h_fin : (lintegral μ fun (a : α) => f 0 a) < ⊤) : (lintegral μ fun (a : α) => infi fun (n : ℕ) => f n a) = infi fun (n : ℕ) => lintegral μ fun (a : α) => f n a := lintegral_infi_ae h_meas (fun (n : ℕ) => ae_of_all μ (h_mono (le_of_lt (nat.lt_succ_self n)))) h_fin /-- Known as Fatou's lemma, version with `ae_measurable` functions -/ theorem lintegral_liminf_le' {α : Type u_1} [measurable_space α] {μ : measure α} {f : ℕ → α → ennreal} (h_meas : ∀ (n : ℕ), ae_measurable (f n)) : (lintegral μ fun (a : α) => filter.liminf filter.at_top fun (n : ℕ) => f n a) ≤ filter.liminf filter.at_top fun (n : ℕ) => lintegral μ fun (a : α) => f n a := sorry /-- Known as Fatou's lemma -/ theorem lintegral_liminf_le {α : Type u_1} [measurable_space α] {μ : measure α} {f : ℕ → α → ennreal} (h_meas : ∀ (n : ℕ), measurable (f n)) : (lintegral μ fun (a : α) => filter.liminf filter.at_top fun (n : ℕ) => f n a) ≤ filter.liminf filter.at_top fun (n : ℕ) => lintegral μ fun (a : α) => f n a := lintegral_liminf_le' fun (n : ℕ) => measurable.ae_measurable (h_meas n) theorem limsup_lintegral_le {α : Type u_1} [measurable_space α] {μ : measure α} {f : ℕ → α → ennreal} {g : α → ennreal} (hf_meas : ∀ (n : ℕ), measurable (f n)) (h_bound : ∀ (n : ℕ), filter.eventually_le (measure.ae μ) (f n) g) (h_fin : (lintegral μ fun (a : α) => g a) < ⊤) : (filter.limsup filter.at_top fun (n : ℕ) => lintegral μ fun (a : α) => f n a) ≤ lintegral μ fun (a : α) => filter.limsup filter.at_top fun (n : ℕ) => f n a := sorry /-- Dominated convergence theorem for nonnegative functions -/ theorem tendsto_lintegral_of_dominated_convergence {α : Type u_1} [measurable_space α] {μ : measure α} {F : ℕ → α → ennreal} {f : α → ennreal} (bound : α → ennreal) (hF_meas : ∀ (n : ℕ), measurable (F n)) (h_bound : ∀ (n : ℕ), filter.eventually_le (measure.ae μ) (F n) bound) (h_fin : (lintegral μ fun (a : α) => bound a) < ⊤) (h_lim : filter.eventually (fun (a : α) => filter.tendsto (fun (n : ℕ) => F n a) filter.at_top (nhds (f a))) (measure.ae μ)) : filter.tendsto (fun (n : ℕ) => lintegral μ fun (a : α) => F n a) filter.at_top (nhds (lintegral μ fun (a : α) => f a)) := sorry /-- Dominated convergence theorem for nonnegative functions which are just almost everywhere measurable. -/ theorem tendsto_lintegral_of_dominated_convergence' {α : Type u_1} [measurable_space α] {μ : measure α} {F : ℕ → α → ennreal} {f : α → ennreal} (bound : α → ennreal) (hF_meas : ∀ (n : ℕ), ae_measurable (F n)) (h_bound : ∀ (n : ℕ), filter.eventually_le (measure.ae μ) (F n) bound) (h_fin : (lintegral μ fun (a : α) => bound a) < ⊤) (h_lim : filter.eventually (fun (a : α) => filter.tendsto (fun (n : ℕ) => F n a) filter.at_top (nhds (f a))) (measure.ae μ)) : filter.tendsto (fun (n : ℕ) => lintegral μ fun (a : α) => F n a) filter.at_top (nhds (lintegral μ fun (a : α) => f a)) := sorry /-- Dominated convergence theorem for filters with a countable basis -/ theorem tendsto_lintegral_filter_of_dominated_convergence {α : Type u_1} [measurable_space α] {μ : measure α} {ι : Type u_2} {l : filter ι} {F : ι → α → ennreal} {f : α → ennreal} (bound : α → ennreal) (hl_cb : filter.is_countably_generated l) (hF_meas : filter.eventually (fun (n : ι) => measurable (F n)) l) (h_bound : filter.eventually (fun (n : ι) => filter.eventually (fun (a : α) => F n a ≤ bound a) (measure.ae μ)) l) (h_fin : (lintegral μ fun (a : α) => bound a) < ⊤) (h_lim : filter.eventually (fun (a : α) => filter.tendsto (fun (n : ι) => F n a) l (nhds (f a))) (measure.ae μ)) : filter.tendsto (fun (n : ι) => lintegral μ fun (a : α) => F n a) l (nhds (lintegral μ fun (a : α) => f a)) := sorry /-- Monotone convergence for a suprema over a directed family and indexed by an encodable type -/ theorem lintegral_supr_directed {α : Type u_1} {β : Type u_2} [measurable_space α] {μ : measure α} [encodable β] {f : β → α → ennreal} (hf : ∀ (b : β), measurable (f b)) (h_directed : directed LessEq f) : (lintegral μ fun (a : α) => supr fun (b : β) => f b a) = supr fun (b : β) => lintegral μ fun (a : α) => f b a := sorry theorem lintegral_tsum {α : Type u_1} {β : Type u_2} [measurable_space α] {μ : measure α} [encodable β] {f : β → α → ennreal} (hf : ∀ (i : β), measurable (f i)) : (lintegral μ fun (a : α) => tsum fun (i : β) => f i a) = tsum fun (i : β) => lintegral μ fun (a : α) => f i a := sorry theorem lintegral_Union {α : Type u_1} {β : Type u_2} [measurable_space α] {μ : measure α} [encodable β] {s : β → set α} (hm : ∀ (i : β), is_measurable (s i)) (hd : pairwise (disjoint on s)) (f : α → ennreal) : (lintegral (measure.restrict μ (set.Union fun (i : β) => s i)) fun (a : α) => f a) = tsum fun (i : β) => lintegral (measure.restrict μ (s i)) fun (a : α) => f a := sorry theorem lintegral_Union_le {α : Type u_1} {β : Type u_2} [measurable_space α] {μ : measure α} [encodable β] (s : β → set α) (f : α → ennreal) : (lintegral (measure.restrict μ (set.Union fun (i : β) => s i)) fun (a : α) => f a) ≤ tsum fun (i : β) => lintegral (measure.restrict μ (s i)) fun (a : α) => f a := sorry theorem lintegral_map {α : Type u_1} {β : Type u_2} [measurable_space α] {μ : measure α} [measurable_space β] {f : β → ennreal} {g : α → β} (hf : measurable f) (hg : measurable g) : (lintegral (coe_fn (measure.map g) μ) fun (a : β) => f a) = lintegral μ fun (a : α) => f (g a) := sorry theorem lintegral_map' {α : Type u_1} {β : Type u_2} [measurable_space α] {μ : measure α} [measurable_space β] {f : β → ennreal} {g : α → β} (hf : ae_measurable f) (hg : measurable g) : (lintegral (coe_fn (measure.map g) μ) fun (a : β) => f a) = lintegral μ fun (a : α) => f (g a) := Eq.trans (Eq.trans (lintegral_congr_ae (ae_measurable.ae_eq_mk hf)) (lintegral_map (ae_measurable.measurable_mk hf) hg)) (lintegral_congr_ae (ae_eq_comp hg (filter.eventually_eq.symm (ae_measurable.ae_eq_mk hf)))) theorem lintegral_comp {α : Type u_1} {β : Type u_2} [measurable_space α] {μ : measure α} [measurable_space β] {f : β → ennreal} {g : α → β} (hf : measurable f) (hg : measurable g) : lintegral μ (f ∘ g) = lintegral (coe_fn (measure.map g) μ) fun (a : β) => f a := Eq.symm (lintegral_map hf hg) theorem set_lintegral_map {α : Type u_1} {β : Type u_2} [measurable_space α] {μ : measure α} [measurable_space β] {f : β → ennreal} {g : α → β} {s : set β} (hs : is_measurable s) (hf : measurable f) (hg : measurable g) : (lintegral (measure.restrict (coe_fn (measure.map g) μ) s) fun (y : β) => f y) = lintegral (measure.restrict μ (g ⁻¹' s)) fun (x : α) => f (g x) := sorry theorem lintegral_dirac' {α : Type u_1} [measurable_space α] (a : α) {f : α → ennreal} (hf : measurable f) : (lintegral (measure.dirac a) fun (a : α) => f a) = f a := sorry theorem lintegral_dirac {α : Type u_1} [measurable_space α] [measurable_singleton_class α] (a : α) (f : α → ennreal) : (lintegral (measure.dirac a) fun (a : α) => f a) = f a := sorry theorem lintegral_count' {α : Type u_1} [measurable_space α] {f : α → ennreal} (hf : measurable f) : (lintegral measure.count fun (a : α) => f a) = tsum fun (a : α) => f a := sorry theorem lintegral_count {α : Type u_1} [measurable_space α] [measurable_singleton_class α] (f : α → ennreal) : (lintegral measure.count fun (a : α) => f a) = tsum fun (a : α) => f a := sorry theorem ae_lt_top {α : Type u_1} [measurable_space α] {μ : measure α} {f : α → ennreal} (hf : measurable f) (h2f : (lintegral μ fun (x : α) => f x) < ⊤) : filter.eventually (fun (x : α) => f x < ⊤) (measure.ae μ) := sorry /-- Given a measure `μ : measure α` and a function `f : α → ennreal`, `μ.with_density f` is the measure such that for a measurable set `s` we have `μ.with_density f s = ∫⁻ a in s, f a ∂μ`. -/ def measure.with_density {α : Type u_1} [measurable_space α] (μ : measure α) (f : α → ennreal) : measure α := measure.of_measurable (fun (s : set α) (hs : is_measurable s) => lintegral (measure.restrict μ s) fun (a : α) => f a) sorry sorry @[simp] theorem with_density_apply {α : Type u_1} [measurable_space α] {μ : measure α} (f : α → ennreal) {s : set α} (hs : is_measurable s) : coe_fn (measure.with_density μ f) s = lintegral (measure.restrict μ s) fun (a : α) => f a := measure.of_measurable_apply s hs end measure_theory /-- To prove something for an arbitrary measurable function into `ennreal`, it suffices to show that the property holds for (multiples of) characteristic functions and is closed under addition and supremum of increasing sequences of functions. It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions can be added once we need them (for example in `h_sum` it is only necessary to consider the sum of a simple function with a multiple of a characteristic function and that the intersection of their images is a subset of `{0}`. -/ theorem measurable.ennreal_induction {α : Type u_1} [measurable_space α] {P : (α → ennreal) → Prop} (h_ind : ∀ (c : ennreal) {s : set α}, is_measurable s → P (set.indicator s fun (_x : α) => c)) (h_sum : ∀ {f g : α → ennreal}, set.univ ⊆ f ⁻¹' singleton 0 ∪ g ⁻¹' singleton 0 → measurable f → measurable g → P f → P g → P (f + g)) (h_supr : ∀ {f : ℕ → α → ennreal}, (∀ (n : ℕ), measurable (f n)) → monotone f → (∀ (n : ℕ), P (f n)) → P fun (x : α) => supr fun (n : ℕ) => f n x) {f : α → ennreal} (hf : measurable f) : P f := sorry namespace measure_theory /-- This is Exercise 1.2.1 from [tao2010]. It allows you to express integration of a measurable function with respect to `(μ.with_density f)` as an integral with respect to `μ`, called the base measure. `μ` is often the Lebesgue measure, and in this circumstance `f` is the probability density function, and `(μ.with_density f)` represents any continuous random variable as a probability measure, such as the uniform distribution between 0 and 1, the Gaussian distribution, the exponential distribution, the Beta distribution, or the Cauchy distribution (see Section 2.4 of [wasserman2004]). Thus, this method shows how to one can calculate expectations, variances, and other moments as a function of the probability density function. -/ theorem lintegral_with_density_eq_lintegral_mul {α : Type u_1} [measurable_space α] (μ : measure α) {f : α → ennreal} (h_mf : measurable f) {g : α → ennreal} : measurable g → (lintegral (measure.with_density μ f) fun (a : α) => g a) = lintegral μ fun (a : α) => Mul.mul f g a := sorry
0b031e4d708ea638eda19aa6fdfc115f93df59dc
35b83be3126daae10419b573c55e1fed009d3ae8
/_target/deps/mathlib/algebra/group_power.lean
f266e95c9c0e771a97be6f3026678108c6820482
[]
no_license
AHassan1024/Lean_Playground
ccb25b72029d199c0d23d002db2d32a9f2689ebc
a00b004c3a2eb9e3e863c361aa2b115260472414
refs/heads/master
1,586,221,905,125
1,544,951,310,000
1,544,951,310,000
157,934,290
0
0
null
null
null
null
UTF-8
Lean
false
false
23,082
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis The power operation on monoids and groups. We separate this from group, because it depends on nat, which in turn depends on other parts of algebra. We have "pow a n" for natural number powers, and "gpow a i" for integer powers. The notation a^n is used for the first, but users can locally redefine it to gpow when needed. Note: power adopts the convention that 0^0=1. -/ import algebra.char_zero algebra.group algebra.ordered_field import data.int.basic data.list.basic universes u v variable {α : Type u} @[simp] theorem inv_one [division_ring α] : (1⁻¹ : α) = 1 := by rw [inv_eq_one_div, one_div_one] @[simp] theorem inv_inv' [discrete_field α] {a:α} : a⁻¹⁻¹ = a := by rw [inv_eq_one_div, inv_eq_one_div, div_div_eq_mul_div, one_mul, div_one] /-- The power operation in a monoid. `a^n = a*a*...*a` n times. -/ def monoid.pow [monoid α] (a : α) : ℕ → α | 0 := 1 | (n+1) := a * monoid.pow n def add_monoid.smul [add_monoid α] (n : ℕ) (a : α) : α := @monoid.pow (multiplicative α) _ a n precedence `•`:70 local infix ` • ` := add_monoid.smul @[priority 5] instance monoid.has_pow [monoid α] : has_pow α ℕ := ⟨monoid.pow⟩ /- monoid -/ section monoid variables [monoid α] {β : Type u} [add_monoid β] @[simp] theorem pow_zero (a : α) : a^0 = 1 := rfl @[simp] theorem add_monoid.zero_smul (a : β) : 0 • a = 0 := rfl attribute [to_additive add_monoid.zero_smul] pow_zero theorem pow_succ (a : α) (n : ℕ) : a^(n+1) = a * a^n := rfl theorem succ_smul (a : β) (n : ℕ) : (n+1)•a = a + n•a := rfl attribute [to_additive succ_smul] pow_succ @[simp] theorem pow_one (a : α) : a^1 = a := mul_one _ @[simp] theorem add_monoid.one_smul (a : β) : 1•a = a := add_zero _ attribute [to_additive add_monoid.one_smul] pow_one theorem pow_mul_comm' (a : α) (n : ℕ) : a^n * a = a * a^n := by induction n with n ih; [rw [pow_zero, one_mul, mul_one], rw [pow_succ, mul_assoc, ih]] theorem smul_add_comm' : ∀ (a : β) (n : ℕ), n•a + a = a + n•a := @pow_mul_comm' (multiplicative β) _ theorem pow_succ' (a : α) (n : ℕ) : a^(n+1) = a^n * a := by rw [pow_succ, pow_mul_comm'] theorem succ_smul' (a : β) (n : ℕ) : (n+1)•a = n•a + a := by rw [succ_smul, smul_add_comm'] attribute [to_additive succ_smul'] pow_succ' theorem pow_two (a : α) : a^2 = a * a := show a*(a*1)=a*a, by rw mul_one theorem two_smul (a : β) : 2•a = a + a := show a+(a+0)=a+a, by rw add_zero attribute [to_additive two_smul] pow_two theorem pow_add (a : α) (m n : ℕ) : a^(m + n) = a^m * a^n := by induction n with n ih; [rw [add_zero, pow_zero, mul_one], rw [pow_succ, ← pow_mul_comm', ← mul_assoc, ← ih, ← pow_succ']]; refl theorem add_monoid.add_smul : ∀ (a : β) (m n : ℕ), (m + n)•a = m•a + n•a := @pow_add (multiplicative β) _ attribute [to_additive add_monoid.add_smul] pow_add @[simp] theorem one_pow (n : ℕ) : (1 : α)^n = (1:α) := by induction n with n ih; [refl, rw [pow_succ, ih, one_mul]] @[simp] theorem add_monoid.smul_zero (n : ℕ) : n•(0 : β) = (0:β) := by induction n with n ih; [refl, rw [succ_smul, ih, zero_add]] attribute [to_additive add_monoid.smul_zero] one_pow theorem pow_mul (a : α) (m n : ℕ) : a^(m * n) = (a^m)^n := by induction n with n ih; [rw mul_zero, rw [nat.mul_succ, pow_add, pow_succ', ih]]; refl theorem add_monoid.mul_smul' : ∀ (a : β) (m n : ℕ), m * n • a = n•(m•a) := @pow_mul (multiplicative β) _ attribute [to_additive add_monoid.mul_smul'] pow_mul theorem pow_mul' (a : α) (m n : ℕ) : a^(m * n) = (a^n)^m := by rw [mul_comm, pow_mul] theorem add_monoid.mul_smul (a : β) (m n : ℕ) : m * n • a = m•(n•a) := by rw [mul_comm, add_monoid.mul_smul'] attribute [to_additive add_monoid.mul_smul] pow_mul' @[simp] theorem add_monoid.smul_one [has_one β] : ∀ n : ℕ, n • (1 : β) = n := nat.eq_cast _ (add_monoid.zero_smul _) (add_monoid.one_smul _) (add_monoid.add_smul _) theorem pow_bit0 (a : α) (n : ℕ) : a ^ bit0 n = a^n * a^n := pow_add _ _ _ theorem bit0_smul (a : β) (n : ℕ) : bit0 n • a = n•a + n•a := add_monoid.add_smul _ _ _ attribute [to_additive bit0_smul] pow_bit0 theorem pow_bit1 (a : α) (n : ℕ) : a ^ bit1 n = a^n * a^n * a := by rw [bit1, pow_succ', pow_bit0] theorem bit1_smul : ∀ (a : β) (n : ℕ), bit1 n • a = n•a + n•a + a := @pow_bit1 (multiplicative β) _ attribute [to_additive bit1_smul] pow_bit1 theorem pow_mul_comm (a : α) (m n : ℕ) : a^m * a^n = a^n * a^m := by rw [←pow_add, ←pow_add, add_comm] theorem smul_add_comm : ∀ (a : β) (m n : ℕ), m•a + n•a = n•a + m•a := @pow_mul_comm (multiplicative β) _ attribute [to_additive smul_add_comm] pow_mul_comm @[simp] theorem list.prod_repeat (a : α) (n : ℕ) : (list.repeat a n).prod = a ^ n := by induction n with n ih; [refl, rw [list.repeat_succ, list.prod_cons, ih]]; refl @[simp] theorem list.sum_repeat : ∀ (a : β) (n : ℕ), (list.repeat a n).sum = n • a := @list.prod_repeat (multiplicative β) _ attribute [to_additive list.sum_repeat] list.prod_repeat @[simp] lemma units.coe_pow (u : units α) (n : ℕ) : ((u ^ n : units α) : α) = u ^ n := by induction n; simp [*, pow_succ] end monoid @[simp] theorem nat.pow_eq_pow (p q : ℕ) : @has_pow.pow _ _ monoid.has_pow p q = p ^ q := by induction q with q ih; [refl, rw [nat.pow_succ, pow_succ, mul_comm, ih]] @[simp] theorem nat.smul_eq_mul (m n : ℕ) : m • n = m * n := by induction m with m ih; [rw [add_monoid.zero_smul, zero_mul], rw [succ_smul', ih, nat.succ_mul]] /- commutative monoid -/ section comm_monoid variables [comm_monoid α] {β : Type*} [add_comm_monoid β] theorem mul_pow (a b : α) (n : ℕ) : (a * b)^n = a^n * b^n := by induction n with n ih; [exact (mul_one _).symm, simp only [pow_succ, ih, mul_assoc, mul_left_comm]] theorem add_monoid.smul_add : ∀ (a b : β) (n : ℕ), n•(a + b) = n•a + n•b := @mul_pow (multiplicative β) _ attribute [to_additive add_monoid.add_smul] mul_pow end comm_monoid section group variables [group α] {β : Type*} [add_group β] section nat @[simp] theorem inv_pow (a : α) (n : ℕ) : (a⁻¹)^n = (a^n)⁻¹ := by induction n with n ih; [exact one_inv.symm, rw [pow_succ', pow_succ, ih, mul_inv_rev]] @[simp] theorem add_monoid.neg_smul : ∀ (a : β) (n : ℕ), n•(-a) = -(n•a) := @inv_pow (multiplicative β) _ attribute [to_additive add_monoid.neg_smul] inv_pow theorem pow_sub (a : α) {m n : ℕ} (h : m ≥ n) : a^(m - n) = a^m * (a^n)⁻¹ := have h1 : m - n + n = m, from nat.sub_add_cancel h, have h2 : a^(m - n) * a^n = a^m, by rw [←pow_add, h1], eq_mul_inv_of_mul_eq h2 theorem add_monoid.smul_sub : ∀ (a : β) {m n : ℕ}, m ≥ n → (m - n)•a = m•a - n•a := @pow_sub (multiplicative β) _ attribute [to_additive add_monoid.smul_sub] inv_pow theorem pow_inv_comm (a : α) (m n : ℕ) : (a⁻¹)^m * a^n = a^n * (a⁻¹)^m := by rw inv_pow; exact inv_comm_of_comm (pow_mul_comm _ _ _) theorem add_monoid.smul_neg_comm : ∀ (a : β) (m n : ℕ), m•(-a) + n•a = n•a + m•(-a) := @pow_inv_comm (multiplicative β) _ attribute [to_additive add_monoid.smul_neg_comm] pow_inv_comm end nat open int /-- The power operation in a group. This extends `monoid.pow` to negative integers with the definition `a^(-n) = (a^n)⁻¹`. -/ def gpow (a : α) : ℤ → α | (of_nat n) := a^n | -[1+n] := (a^(nat.succ n))⁻¹ def gsmul (n : ℤ) (a : β) : β := @gpow (multiplicative β) _ a n @[priority 10] instance group.has_pow : has_pow α ℤ := ⟨gpow⟩ local infix ` • `:70 := gsmul local infix ` •ℕ `:70 := add_monoid.smul @[simp] theorem gpow_coe_nat (a : α) (n : ℕ) : a ^ (n:ℤ) = a ^ n := rfl @[simp] theorem gsmul_coe_nat (a : β) (n : ℕ) : (n:ℤ) • a = n •ℕ a := rfl attribute [to_additive gsmul_coe_nat] gpow_coe_nat @[simp] theorem gpow_of_nat (a : α) (n : ℕ) : a ^ of_nat n = a ^ n := rfl @[simp] theorem gsmul_of_nat (a : β) (n : ℕ) : of_nat n • a = n •ℕ a := rfl attribute [to_additive gsmul_of_nat] gpow_of_nat @[simp] theorem gpow_neg_succ (a : α) (n : ℕ) : a ^ -[1+n] = (a ^ n.succ)⁻¹ := rfl @[simp] theorem gsmul_neg_succ (a : β) (n : ℕ) : -[1+n] • a = - (n.succ •ℕ a) := rfl attribute [to_additive gsmul_neg_succ] gpow_neg_succ local attribute [ematch] le_of_lt open nat @[simp] theorem gpow_zero (a : α) : a ^ (0:ℤ) = 1 := rfl @[simp] theorem zero_gsmul (a : β) : (0:ℤ) • a = 0 := rfl attribute [to_additive zero_gsmul] gpow_zero @[simp] theorem gpow_one (a : α) : a ^ (1:ℤ) = a := mul_one _ @[simp] theorem one_gsmul (a : β) : (1:ℤ) • a = a := add_zero _ attribute [to_additive one_gsmul] gpow_one @[simp] theorem one_gpow : ∀ (n : ℤ), (1 : α) ^ n = 1 | (n : ℕ) := one_pow _ | -[1+ n] := show _⁻¹=(1:α), by rw [_root_.one_pow, one_inv] @[simp] theorem gsmul_zero : ∀ (n : ℤ), n • (0 : β) = 0 := @one_gpow (multiplicative β) _ attribute [to_additive gsmul_zero] one_gpow @[simp] theorem gpow_neg (a : α) : ∀ (n : ℤ), a ^ -n = (a ^ n)⁻¹ | (n+1:ℕ) := rfl | 0 := one_inv.symm | -[1+ n] := (inv_inv _).symm @[simp] theorem neg_gsmul : ∀ (a : β) (n : ℤ), -n • a = -(n • a) := @gpow_neg (multiplicative β) _ attribute [to_additive neg_gsmul] gpow_neg theorem gpow_neg_one (x : α) : x ^ (-1:ℤ) = x⁻¹ := congr_arg has_inv.inv $ pow_one x theorem neg_one_gsmul (x : β) : (-1:ℤ) • x = -x := congr_arg has_neg.neg $ add_monoid.one_smul x attribute [to_additive neg_one_gsmul] gpow_neg_one theorem inv_gpow (a : α) : ∀n:ℤ, a⁻¹ ^ n = (a ^ n)⁻¹ | (n : ℕ) := inv_pow a n | -[1+ n] := congr_arg has_inv.inv $ inv_pow a (n+1) private lemma gpow_add_aux (a : α) (m n : nat) : a ^ ((of_nat m) + -[1+n]) = a ^ of_nat m * a ^ -[1+n] := or.elim (nat.lt_or_ge m (nat.succ n)) (assume h1 : m < succ n, have h2 : m ≤ n, from le_of_lt_succ h1, suffices a ^ -[1+ n-m] = a ^ of_nat m * a ^ -[1+n], by rwa [of_nat_add_neg_succ_of_nat_of_lt h1], show (a ^ nat.succ (n - m))⁻¹ = a ^ of_nat m * a ^ -[1+n], by rw [← succ_sub h2, pow_sub _ (le_of_lt h1), mul_inv_rev, inv_inv]; refl) (assume : m ≥ succ n, suffices a ^ (of_nat (m - succ n)) = (a ^ (of_nat m)) * (a ^ -[1+ n]), by rw [of_nat_add_neg_succ_of_nat_of_ge]; assumption, suffices a ^ (m - succ n) = a ^ m * (a ^ n.succ)⁻¹, from this, by rw pow_sub; assumption) theorem gpow_add (a : α) : ∀ (i j : ℤ), a ^ (i + j) = a ^ i * a ^ j | (of_nat m) (of_nat n) := pow_add _ _ _ | (of_nat m) -[1+n] := gpow_add_aux _ _ _ | -[1+m] (of_nat n) := by rw [add_comm, gpow_add_aux, gpow_neg_succ, gpow_of_nat, ← inv_pow, ← pow_inv_comm] | -[1+m] -[1+n] := suffices (a ^ (m + succ (succ n)))⁻¹ = (a ^ m.succ)⁻¹ * (a ^ n.succ)⁻¹, from this, by rw [← succ_add_eq_succ_add, add_comm, _root_.pow_add, mul_inv_rev] theorem add_gsmul : ∀ (a : β) (i j : ℤ), (i + j) • a = i • a + j • a := @gpow_add (multiplicative β) _ theorem gpow_mul_comm (a : α) (i j : ℤ) : a ^ i * a ^ j = a ^ j * a ^ i := by rw [← gpow_add, ← gpow_add, add_comm] theorem gsmul_add_comm : ∀ (a : β) (i j), i • a + j • a = j • a + i • a := @gpow_mul_comm (multiplicative β) _ attribute [to_additive gsmul_add_comm] gpow_mul_comm theorem gpow_mul (a : α) : ∀ m n : ℤ, a ^ (m * n) = (a ^ m) ^ n | (m : ℕ) (n : ℕ) := pow_mul _ _ _ | (m : ℕ) -[1+ n] := (gpow_neg _ (m * succ n)).trans $ show (a ^ (m * succ n))⁻¹ = _, by rw pow_mul; refl | -[1+ m] (n : ℕ) := (gpow_neg _ (succ m * n)).trans $ show (a ^ (m.succ * n))⁻¹ = _, by rw [pow_mul, ← inv_pow]; refl | -[1+ m] -[1+ n] := (pow_mul a (succ m) (succ n)).trans $ show _ = (_⁻¹^_)⁻¹, by rw [inv_pow, inv_inv] theorem gsmul_mul' : ∀ (a : β) (m n : ℤ), m * n • a = n • (m • a) := @gpow_mul (multiplicative β) _ attribute [to_additive gsmul_mul'] gpow_mul theorem gpow_mul' (a : α) (m n : ℤ) : a ^ (m * n) = (a ^ n) ^ m := by rw [mul_comm, gpow_mul] theorem gsmul_mul (a : β) (m n : ℤ) : m * n • a = m • (n • a) := by rw [mul_comm, gsmul_mul'] attribute [to_additive gsmul_mul] gpow_mul' theorem gpow_bit0 (a : α) (n : ℤ) : a ^ bit0 n = a ^ n * a ^ n := gpow_add _ _ _ theorem bit0_gsmul (a : β) (n : ℤ) : bit0 n • a = n • a + n • a := gpow_add _ _ _ attribute [to_additive bit0_gsmul] gpow_bit0 theorem gpow_bit1 (a : α) (n : ℤ) : a ^ bit1 n = a ^ n * a ^ n * a := by rw [bit1, gpow_add]; simp [gpow_bit0] theorem bit1_gsmul : ∀ (a : β) (n : ℤ), bit1 n • a = n • a + n • a + a := @gpow_bit1 (multiplicative β) _ attribute [to_additive bit1_gsmul] gpow_bit1 theorem gsmul_neg (a : β) (n : ℤ) : gsmul n (- a) = - gsmul n a := begin induction n using int.induction_on with z ih z ih, { simp }, { rw [add_comm] {occs := occurrences.pos [1]}, simp [add_gsmul, ih, -add_comm] }, { rw [sub_eq_add_neg, add_comm] {occs := occurrences.pos [1]}, simp [ih, add_gsmul, neg_gsmul, -add_comm] } end attribute [to_additive gsmul_neg] gpow_neg end group namespace is_group_hom variables {β : Type v} [group α] [group β] (f : α → β) [is_group_hom f] theorem pow (a : α) (n : ℕ) : f (a ^ n) = f a ^ n := by induction n with n ih; [exact is_group_hom.one f, rw [pow_succ, is_group_hom.mul f, ih]]; refl theorem gpow (a : α) (n : ℤ) : f (a ^ n) = f a ^ n := by cases n; [exact is_group_hom.pow f _ _, exact (is_group_hom.inv f _).trans (congr_arg _ $ is_group_hom.pow f _ _)] end is_group_hom namespace is_add_group_hom variables {β : Type v} [add_group α] [add_group β] (f : α → β) [is_add_group_hom f] theorem smul (a : α) (n : ℕ) : f (n • a) = n • f a := by induction n with n ih; [exact is_add_group_hom.zero f, rw [succ_smul, is_add_group_hom.add f, ih]]; refl theorem gsmul (a : α) (n : ℤ) : f (gsmul n a) = gsmul n (f a) := begin induction n using int.induction_on with z ih z ih, { simp [is_add_group_hom.zero f] }, { simp [is_add_group_hom.add f, add_gsmul, ih] }, { simp [is_add_group_hom.add f, is_add_group_hom.neg f, add_gsmul, ih] } end end is_add_group_hom local infix ` •ℤ `:70 := gsmul section comm_monoid variables [comm_group α] {β : Type*} [add_comm_group β] theorem mul_gpow (a b : α) : ∀ n:ℤ, (a * b)^n = a^n * b^n | (n : ℕ) := mul_pow a b n | -[1+ n] := show _⁻¹=_⁻¹*_⁻¹, by rw [mul_pow, mul_inv_rev, mul_comm] theorem gsmul_add : ∀ (a b : β) (n : ℤ), n •ℤ (a + b) = n •ℤ a + n •ℤ b := @mul_gpow (multiplicative β) _ attribute [to_additive gsmul_add] mul_gpow theorem gsmul_sub : ∀ (a b : β) (n : ℤ), gsmul n (a - b) = gsmul n a - gsmul n b := by simp [gsmul_add, gsmul_neg] end comm_monoid section group @[instance] theorem is_add_group_hom_gsmul {α β} [add_group α] [add_comm_group β] (f : α → β) [is_add_group_hom f] (z : ℤ) : is_add_group_hom (λa, gsmul z (f a)) := ⟨assume a b, by rw [is_add_group_hom.add f, gsmul_add]⟩ end group theorem add_monoid.smul_eq_mul' [semiring α] (a : α) (n : ℕ) : n • a = a * n := by induction n with n ih; [rw [add_monoid.zero_smul, nat.cast_zero, mul_zero], rw [succ_smul', ih, nat.cast_succ, mul_add, mul_one]] theorem add_monoid.smul_eq_mul [semiring α] (n : ℕ) (a : α) : n • a = n * a := by rw [add_monoid.smul_eq_mul', nat.mul_cast_comm] theorem add_monoid.mul_smul_left [semiring α] (a b : α) (n : ℕ) : n • (a * b) = a * (n • b) := by rw [add_monoid.smul_eq_mul', add_monoid.smul_eq_mul', mul_assoc] theorem add_monoid.mul_smul_assoc [semiring α] (a b : α) (n : ℕ) : n • (a * b) = n • a * b := by rw [add_monoid.smul_eq_mul, add_monoid.smul_eq_mul, mul_assoc] lemma zero_pow [semiring α] : ∀ {n : ℕ}, 0 < n → (0 : α) ^ n = 0 | (n+1) _ := zero_mul _ @[simp] theorem nat.cast_pow [semiring α] (n m : ℕ) : (↑(n ^ m) : α) = ↑n ^ m := by induction m with m ih; [exact nat.cast_one, rw [nat.pow_succ, pow_succ', nat.cast_mul, ih]] @[simp] theorem int.coe_nat_pow (n m : ℕ) : ((n ^ m : ℕ) : ℤ) = n ^ m := by induction m with m ih; [exact int.coe_nat_one, rw [nat.pow_succ, pow_succ', int.coe_nat_mul, ih]] theorem is_semiring_hom.map_pow {β} [semiring α] [semiring β] (f : α → β) [is_semiring_hom f] (x : α) (n : ℕ) : f (x ^ n) = f x ^ n := by induction n with n ih; [exact is_semiring_hom.map_one f, rw [pow_succ, pow_succ, is_semiring_hom.map_mul f, ih]] theorem neg_one_pow_eq_or {R} [ring R] : ∀ n : ℕ, (-1 : R)^n = 1 ∨ (-1 : R)^n = -1 | 0 := or.inl rfl | (n+1) := (neg_one_pow_eq_or n).swap.imp (λ h, by rw [pow_succ, h, neg_one_mul, neg_neg]) (λ h, by rw [pow_succ, h, mul_one]) theorem gsmul_eq_mul [ring α] (a : α) : ∀ n, n •ℤ a = n * a | (n : ℕ) := add_monoid.smul_eq_mul _ _ | -[1+ n] := show -(_•_)=-_*_, by rw [neg_mul_eq_neg_mul_symm, add_monoid.smul_eq_mul, nat.cast_succ] theorem gsmul_eq_mul' [ring α] (a : α) (n : ℤ) : n •ℤ a = a * n := by rw [gsmul_eq_mul, int.mul_cast_comm] theorem mul_gsmul_left [ring α] (a b : α) (n : ℤ) : n •ℤ (a * b) = a * (n •ℤ b) := by rw [gsmul_eq_mul', gsmul_eq_mul', mul_assoc] theorem mul_gsmul_assoc [ring α] (a b : α) (n : ℤ) : n •ℤ (a * b) = n •ℤ a * b := by rw [gsmul_eq_mul, gsmul_eq_mul, mul_assoc] @[simp] theorem int.cast_pow [ring α] (n : ℤ) (m : ℕ) : (↑(n ^ m) : α) = ↑n ^ m := by induction m with m ih; [exact int.cast_one, rw [pow_succ, pow_succ, int.cast_mul, ih]] lemma neg_one_pow_eq_pow_mod_two [ring α] {n : ℕ} : (-1 : α) ^ n = -1 ^ (n % 2) := by rw [← nat.mod_add_div n 2, pow_add, pow_mul]; simp [pow_two] theorem pow_ne_zero [domain α] {a : α} (n : ℕ) (h : a ≠ 0) : a ^ n ≠ 0 := begin induction n with n ih, {exact one_ne_zero}, intro H, cases mul_eq_zero.1 H with h1 h1, exacts [h h1, ih h1] end @[simp] theorem one_div_pow [division_ring α] {a : α} (ha : a ≠ 0) (n : ℕ) : (1 / a) ^ n = 1 / a ^ n := by induction n with n ih; [exact (div_one _).symm, rw [pow_succ', ih, division_ring.one_div_mul_one_div (pow_ne_zero _ ha) ha]]; refl @[simp] theorem division_ring.inv_pow [division_ring α] {a : α} (ha : a ≠ 0) (n : ℕ) : a⁻¹ ^ n = (a ^ n)⁻¹ := by simpa only [inv_eq_one_div] using one_div_pow ha n @[simp] theorem div_pow [field α] (a : α) {b : α} (hb : b ≠ 0) (n : ℕ) : (a / b) ^ n = a ^ n / b ^ n := by rw [div_eq_mul_one_div, mul_pow, one_div_pow hb, ← div_eq_mul_one_div] theorem add_monoid.smul_nonneg [ordered_comm_monoid α] {a : α} (H : 0 ≤ a) : ∀ n : ℕ, 0 ≤ n • a | 0 := le_refl _ | (n+1) := add_nonneg' H (add_monoid.smul_nonneg n) lemma pow_abs [decidable_linear_ordered_comm_ring α] (a : α) (n : ℕ) : (abs a)^n = abs (a^n) := by induction n with n ih; [exact (abs_one).symm, rw [pow_succ, pow_succ, ih, abs_mul]] lemma inv_pow' [discrete_field α] (a : α) (n : ℕ) : (a ^ n)⁻¹ = a⁻¹ ^ n := by induction n; simp [*, pow_succ, mul_inv', mul_comm] lemma pow_inv [division_ring α] (a : α) : ∀ n : ℕ, a ≠ 0 → (a^n)⁻¹ = (a⁻¹)^n | 0 ha := inv_one | (n+1) ha := by rw [pow_succ, pow_succ', mul_inv_eq (pow_ne_zero _ ha) ha, pow_inv _ ha] section linear_ordered_semiring variable [linear_ordered_semiring α] theorem pow_pos {a : α} (H : 0 < a) : ∀ (n : ℕ), 0 < a ^ n | 0 := zero_lt_one | (n+1) := mul_pos H (pow_pos _) theorem pow_nonneg {a : α} (H : 0 ≤ a) : ∀ (n : ℕ), 0 ≤ a ^ n | 0 := zero_le_one | (n+1) := mul_nonneg H (pow_nonneg _) theorem one_le_pow_of_one_le {a : α} (H : 1 ≤ a) : ∀ (n : ℕ), 1 ≤ a ^ n | 0 := le_refl _ | (n+1) := by simpa only [mul_one] using mul_le_mul H (one_le_pow_of_one_le n) zero_le_one (le_trans zero_le_one H) theorem pow_ge_one_add_mul {a : α} (H : a ≥ 0) : ∀ (n : ℕ), 1 + n • a ≤ (1 + a) ^ n | 0 := le_of_eq $ add_zero _ | (n+1) := begin rw [pow_succ', succ_smul'], refine le_trans _ (mul_le_mul_of_nonneg_right (pow_ge_one_add_mul n) (add_nonneg zero_le_one H)), rw [mul_add, mul_one, ← add_assoc, add_le_add_iff_left], simpa only [one_mul] using mul_le_mul_of_nonneg_right ((le_add_iff_nonneg_right 1).2 (add_monoid.smul_nonneg H n)) H end theorem pow_le_pow {a : α} {n m : ℕ} (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m := let ⟨k, hk⟩ := nat.le.dest h in calc a ^ n = a ^ n * 1 : (mul_one _).symm ... ≤ a ^ n * a ^ k : mul_le_mul_of_nonneg_left (one_le_pow_of_one_le ha _) (pow_nonneg (le_trans zero_le_one ha) _) ... = a ^ m : by rw [←hk, pow_add] lemma pow_le_pow_of_le_left {a b : α} (ha : 0 ≤ a) (hab : a ≤ b) : ∀ i : ℕ, a^i ≤ b^i | 0 := by simp | (k+1) := mul_le_mul hab (pow_le_pow_of_le_left _) (pow_nonneg ha _) (le_trans ha hab) private lemma pow_lt_pow_of_lt_one_aux {a : α} (h : 0 < a) (ha : a < 1) (i : ℕ) : ∀ k : ℕ, a ^ (i + k + 1) < a ^ i | 0 := begin simp, rw ←one_mul (a^i), exact mul_lt_mul ha (le_refl _) (pow_pos h _) zero_le_one end | (k+1) := begin rw ←one_mul (a^i), apply mul_lt_mul ha _ _ zero_le_one, { apply le_of_lt, apply pow_lt_pow_of_lt_one_aux }, { show 0 < a ^ (i + (k + 1) + 0), apply pow_pos h } end private lemma pow_le_pow_of_le_one_aux {a : α} (h : 0 ≤ a) (ha : a ≤ 1) (i : ℕ) : ∀ k : ℕ, a ^ (i + k) ≤ a ^ i | 0 := by simp | (k+1) := by rw [←add_assoc, ←one_mul (a^i)]; exact mul_le_mul ha (pow_le_pow_of_le_one_aux _) (pow_nonneg h _) zero_le_one lemma pow_lt_pow_of_lt_one {a : α} (h : 0 < a) (ha : a < 1) {i j : ℕ} (hij : i < j) : a ^ j < a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_lt hij in by rw hk; exact pow_lt_pow_of_lt_one_aux h ha _ _ lemma pow_le_pow_of_le_one {a : α} (h : 0 ≤ a) (ha : a ≤ 1) {i j : ℕ} (hij : i ≤ j) : a ^ j ≤ a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_le hij in by rw hk; exact pow_le_pow_of_le_one_aux h ha _ _ lemma pow_le_one {x : α} : ∀ (n : ℕ) (h0 : 0 ≤ x) (h1 : x ≤ 1), x ^ n ≤ 1 | 0 h0 h1 := le_refl (1 : α) | (n+1) h0 h1 := mul_le_one h1 (pow_nonneg h0 _) (pow_le_one n h0 h1) end linear_ordered_semiring theorem pow_two_nonneg [linear_ordered_ring α] (a : α) : 0 ≤ a ^ 2 := by rw pow_two; exact mul_self_nonneg _ theorem pow_ge_one_add_sub_mul [linear_ordered_ring α] {a : α} (H : a ≥ 1) (n : ℕ) : 1 + n • (a - 1) ≤ a ^ n := by simpa only [add_sub_cancel'_right] using pow_ge_one_add_mul (sub_nonneg.2 H) n namespace int lemma units_pow_two (u : units ℤ) : u ^ 2 = 1 := (units_eq_one_or u).elim (λ h, h.symm ▸ rfl) (λ h, h.symm ▸ rfl) lemma units_pow_eq_pow_mod_two (u : units ℤ) (n : ℕ) : u ^ n = u ^ (n % 2) := by conv {to_lhs, rw ← nat.mod_add_div n 2}; rw [pow_add, pow_mul, units_pow_two, one_pow, mul_one] end int @[simp] lemma neg_square {α} [ring α] (z : α) : (-z)^2 = z^2 := by simp [pow, monoid.pow] lemma div_sq_cancel {α} [field α] {a : α} (ha : a ≠ 0) (b : α) : a^2 * b / a = a * b := by rw [pow_two, mul_assoc, mul_div_cancel_left _ ha]
646aa2c4b047b871d261d63d098c0acf76ab5713
4950bf76e5ae40ba9f8491647d0b6f228ddce173
/src/logic/basic.lean
73ba7bca130d208b2a6bf63293495e07efe2ccbc
[ "Apache-2.0" ]
permissive
ntzwq/mathlib
ca50b21079b0a7c6781c34b62199a396dd00cee2
36eec1a98f22df82eaccd354a758ef8576af2a7f
refs/heads/master
1,675,193,391,478
1,607,822,996,000
1,607,822,996,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
51,272
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ import tactic.doc_commands /-! # Basic logic properties This file is one of the earliest imports in mathlib. ## Implementation notes Theorems that require decidability hypotheses are in the namespace "decidable". Classical versions are in the namespace "classical". In the presence of automation, this whole file may be unnecessary. On the other hand, maybe it is useful for writing automation. -/ local attribute [instance, priority 10] classical.prop_decidable section miscellany /- We add the `inline` attribute to optimize VM computation using these declarations. For example, `if p ∧ q then ... else ...` will not evaluate the decidability of `q` if `p` is false. -/ attribute [inline] and.decidable or.decidable decidable.false xor.decidable iff.decidable decidable.true implies.decidable not.decidable ne.decidable bool.decidable_eq decidable.to_bool attribute [simp] cast_eq variables {α : Type*} {β : Type*} /-- An identity function with its main argument implicit. This will be printed as `hidden` even if it is applied to a large term, so it can be used for elision, as done in the `elide` and `unelide` tactics. -/ @[reducible] def hidden {α : Sort*} {a : α} := a /-- Ex falso, the nondependent eliminator for the `empty` type. -/ def empty.elim {C : Sort*} : empty → C. instance : subsingleton empty := ⟨λa, a.elim⟩ instance subsingleton.prod {α β : Type*} [subsingleton α] [subsingleton β] : subsingleton (α × β) := ⟨by { intros a b, cases a, cases b, congr, }⟩ instance : decidable_eq empty := λa, a.elim instance sort.inhabited : inhabited (Sort*) := ⟨punit⟩ instance sort.inhabited' : inhabited (default (Sort*)) := ⟨punit.star⟩ instance psum.inhabited_left {α β} [inhabited α] : inhabited (psum α β) := ⟨psum.inl (default _)⟩ instance psum.inhabited_right {α β} [inhabited β] : inhabited (psum α β) := ⟨psum.inr (default _)⟩ @[priority 10] instance decidable_eq_of_subsingleton {α} [subsingleton α] : decidable_eq α | a b := is_true (subsingleton.elim a b) @[simp] lemma eq_iff_true_of_subsingleton [subsingleton α] (x y : α) : x = y ↔ true := by cc /-- Add an instance to "undo" coercion transitivity into a chain of coercions, because most simp lemmas are stated with respect to simple coercions and will not match when part of a chain. -/ @[simp] theorem coe_coe {α β γ} [has_coe α β] [has_coe_t β γ] (a : α) : (a : γ) = (a : β) := rfl theorem coe_fn_coe_trans {α β γ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_fun γ] (x : α) : @coe_fn α _ x = @coe_fn β _ x := rfl @[simp] theorem coe_fn_coe_base {α β} [has_coe α β] [has_coe_to_fun β] (x : α) : @coe_fn α _ x = @coe_fn β _ x := rfl theorem coe_sort_coe_trans {α β γ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_sort γ] (x : α) : @coe_sort α _ x = @coe_sort β _ x := rfl /-- Many structures such as bundled morphisms coerce to functions so that you can transparently apply them to arguments. For example, if `e : α ≃ β` and `a : α` then you can write `e a` and this is elaborated as `⇑e a`. This type of coercion is implemented using the `has_coe_to_fun`type class. There is one important consideration: If a type coerces to another type which in turn coerces to a function, then it **must** implement `has_coe_to_fun` directly: ```lean structure sparkling_equiv (α β) extends α ≃ β -- if we add a `has_coe` instance, instance {α β} : has_coe (sparkling_equiv α β) (α ≃ β) := ⟨sparkling_equiv.to_equiv⟩ -- then a `has_coe_to_fun` instance **must** be added as well: instance {α β} : has_coe_to_fun (sparkling_equiv α β) := ⟨λ _, α → β, λ f, f.to_equiv.to_fun⟩ ``` (Rationale: if we do not declare the direct coercion, then `⇑e a` is not in simp-normal form. The lemma `coe_fn_coe_base` will unfold it to `⇑↑e a`. This often causes loops in the simplifier.) -/ library_note "function coercion" @[simp] theorem coe_sort_coe_base {α β} [has_coe α β] [has_coe_to_sort β] (x : α) : @coe_sort α _ x = @coe_sort β _ x := rfl /-- `pempty` is the universe-polymorphic analogue of `empty`. -/ @[derive decidable_eq] inductive {u} pempty : Sort u /-- Ex falso, the nondependent eliminator for the `pempty` type. -/ def pempty.elim {C : Sort*} : pempty → C. instance subsingleton_pempty : subsingleton pempty := ⟨λa, a.elim⟩ @[simp] lemma not_nonempty_pempty : ¬ nonempty pempty := assume ⟨h⟩, h.elim @[simp] theorem forall_pempty {P : pempty → Prop} : (∀ x : pempty, P x) ↔ true := ⟨λ h, trivial, λ h x, by cases x⟩ @[simp] theorem exists_pempty {P : pempty → Prop} : (∃ x : pempty, P x) ↔ false := ⟨λ h, by { cases h with w, cases w }, false.elim⟩ lemma congr_arg_heq {α} {β : α → Sort*} (f : ∀ a, β a) : ∀ {a₁ a₂ : α}, a₁ = a₂ → f a₁ == f a₂ | a _ rfl := heq.rfl lemma plift.down_inj {α : Sort*} : ∀ (a b : plift α), a.down = b.down → a = b | ⟨a⟩ ⟨b⟩ rfl := rfl -- missing [symm] attribute for ne in core. attribute [symm] ne.symm lemma ne_comm {α} {a b : α} : a ≠ b ↔ b ≠ a := ⟨ne.symm, ne.symm⟩ @[simp] lemma eq_iff_eq_cancel_left {b c : α} : (∀ {a}, a = b ↔ a = c) ↔ (b = c) := ⟨λ h, by rw [← h], λ h a, by rw h⟩ @[simp] lemma eq_iff_eq_cancel_right {a b : α} : (∀ {c}, a = c ↔ b = c) ↔ (a = b) := ⟨λ h, by rw h, λ h a, by rw h⟩ /-- Wrapper for adding elementary propositions to the type class systems. Warning: this can easily be abused. See the rest of this docstring for details. Certain propositions should not be treated as a class globally, but sometimes it is very convenient to be able to use the type class system in specific circumstances. For example, `zmod p` is a field if and only if `p` is a prime number. In order to be able to find this field instance automatically by type class search, we have to turn `p.prime` into an instance implicit assumption. On the other hand, making `nat.prime` a class would require a major refactoring of the library, and it is questionable whether making `nat.prime` a class is desirable at all. The compromise is to add the assumption `[fact p.prime]` to `zmod.field`. In particular, this class is not intended for turning the type class system into an automated theorem prover for first order logic. -/ @[class] def fact (p : Prop) := p lemma fact.elim {p : Prop} (h : fact p) : p := h end miscellany /-! ### Declarations about propositional connectives -/ theorem false_ne_true : false ≠ true | h := h.symm ▸ trivial section propositional variables {a b c d : Prop} /-! ### Declarations about `implies` -/ theorem iff_of_eq (e : a = b) : a ↔ b := e ▸ iff.rfl theorem iff_iff_eq : (a ↔ b) ↔ a = b := ⟨propext, iff_of_eq⟩ @[simp] lemma eq_iff_iff {p q : Prop} : (p = q) ↔ (p ↔ q) := iff_iff_eq.symm @[simp] theorem imp_self : (a → a) ↔ true := iff_true_intro id theorem imp_intro {α β : Prop} (h : α) : β → α := λ _, h theorem imp_false : (a → false) ↔ ¬ a := iff.rfl theorem imp_and_distrib {α} : (α → b ∧ c) ↔ (α → b) ∧ (α → c) := ⟨λ h, ⟨λ ha, (h ha).left, λ ha, (h ha).right⟩, λ h ha, ⟨h.left ha, h.right ha⟩⟩ @[simp] theorem and_imp : (a ∧ b → c) ↔ (a → b → c) := iff.intro (λ h ha hb, h ⟨ha, hb⟩) (λ h ⟨ha, hb⟩, h ha hb) theorem iff_def : (a ↔ b) ↔ (a → b) ∧ (b → a) := iff_iff_implies_and_implies _ _ theorem iff_def' : (a ↔ b) ↔ (b → a) ∧ (a → b) := iff_def.trans and.comm theorem imp_true_iff {α : Sort*} : (α → true) ↔ true := iff_true_intro $ λ_, trivial @[simp] theorem imp_iff_right (ha : a) : (a → b) ↔ b := ⟨λf, f ha, imp_intro⟩ /-! ### Declarations about `not` -/ /-- Ex falso for negation. From `¬ a` and `a` anything follows. This is the same as `absurd` with the arguments flipped, but it is in the `not` namespace so that projection notation can be used. -/ def not.elim {α : Sort*} (H1 : ¬a) (H2 : a) : α := absurd H2 H1 @[reducible] theorem not.imp {a b : Prop} (H2 : ¬b) (H1 : a → b) : ¬a := mt H1 H2 theorem not_not_of_not_imp : ¬(a → b) → ¬¬a := mt not.elim theorem not_of_not_imp {a : Prop} : ¬(a → b) → ¬b := mt imp_intro theorem dec_em (p : Prop) [decidable p] : p ∨ ¬p := decidable.em p theorem em (p : Prop) : p ∨ ¬ p := classical.em _ theorem or_not {p : Prop} : p ∨ ¬ p := em _ theorem by_contradiction {p} : (¬p → false) → p := decidable.by_contradiction -- alias by_contradiction ← by_contra theorem by_contra {p} : (¬p → false) → p := decidable.by_contradiction /-- In most of mathlib, we use the law of excluded middle (LEM) and the axiom of choice (AC) freely. The `decidable` namespace contains versions of lemmas from the root namespace that explicitly attempt to avoid the axiom of choice, usually by adding decidability assumptions on the inputs. You can check if a lemma uses the axiom of choice by using `#print axioms foo` and seeing if `classical.choice` appears in the list. -/ library_note "decidable namespace" -- See Note [decidable namespace] protected theorem decidable.not_not [decidable a] : ¬¬a ↔ a := iff.intro decidable.by_contradiction not_not_intro /-- The Double Negation Theorem: `¬ ¬ P` is equivalent to `P`. The left-to-right direction, double negation elimination (DNE), is classically true but not constructively. -/ @[simp] theorem not_not : ¬¬a ↔ a := decidable.not_not theorem of_not_not : ¬¬a → a := by_contra -- See Note [decidable namespace] protected theorem decidable.of_not_imp [decidable a] (h : ¬ (a → b)) : a := decidable.by_contradiction (not_not_of_not_imp h) theorem of_not_imp : ¬ (a → b) → a := decidable.of_not_imp -- See Note [decidable namespace] protected theorem decidable.not_imp_symm [decidable a] (h : ¬a → b) (hb : ¬b) : a := decidable.by_contradiction $ hb ∘ h theorem not.decidable_imp_symm [decidable a] : (¬a → b) → ¬b → a := decidable.not_imp_symm theorem not.imp_symm : (¬a → b) → ¬b → a := not.decidable_imp_symm -- See Note [decidable namespace] protected theorem decidable.not_imp_comm [decidable a] [decidable b] : (¬a → b) ↔ (¬b → a) := ⟨not.decidable_imp_symm, not.decidable_imp_symm⟩ theorem not_imp_comm : (¬a → b) ↔ (¬b → a) := decidable.not_imp_comm @[simp] theorem imp_not_self : (a → ¬a) ↔ ¬a := ⟨λ h ha, h ha ha, λ h _, h⟩ theorem decidable.not_imp_self [decidable a] : (¬a → a) ↔ a := by { have := @imp_not_self (¬a), rwa decidable.not_not at this } @[simp] theorem not_imp_self : (¬a → a) ↔ a := decidable.not_imp_self theorem imp.swap : (a → b → c) ↔ (b → a → c) := ⟨function.swap, function.swap⟩ theorem imp_not_comm : (a → ¬b) ↔ (b → ¬a) := imp.swap /-! ### Declarations about `and` -/ theorem not_and_of_not_left (b : Prop) : ¬a → ¬(a ∧ b) := mt and.left theorem not_and_of_not_right (a : Prop) {b : Prop} : ¬b → ¬(a ∧ b) := mt and.right theorem and.imp_left (h : a → b) : a ∧ c → b ∧ c := and.imp h id theorem and.imp_right (h : a → b) : c ∧ a → c ∧ b := and.imp id h lemma and.right_comm : (a ∧ b) ∧ c ↔ (a ∧ c) ∧ b := by simp only [and.left_comm, and.comm] lemma and.rotate : a ∧ b ∧ c ↔ b ∧ c ∧ a := by simp only [and.left_comm, and.comm] theorem and_not_self_iff (a : Prop) : a ∧ ¬ a ↔ false := iff.intro (assume h, (h.right) (h.left)) (assume h, h.elim) theorem not_and_self_iff (a : Prop) : ¬ a ∧ a ↔ false := iff.intro (assume ⟨hna, ha⟩, hna ha) false.elim theorem and_iff_left_of_imp {a b : Prop} (h : a → b) : (a ∧ b) ↔ a := iff.intro and.left (λ ha, ⟨ha, h ha⟩) theorem and_iff_right_of_imp {a b : Prop} (h : b → a) : (a ∧ b) ↔ b := iff.intro and.right (λ hb, ⟨h hb, hb⟩) @[simp] theorem and_iff_left_iff_imp {a b : Prop} : ((a ∧ b) ↔ a) ↔ (a → b) := ⟨λ h ha, (h.2 ha).2, and_iff_left_of_imp⟩ @[simp] theorem and_iff_right_iff_imp {a b : Prop} : ((a ∧ b) ↔ b) ↔ (b → a) := ⟨λ h ha, (h.2 ha).1, and_iff_right_of_imp⟩ @[simp] lemma and.congr_right_iff : (a ∧ b ↔ a ∧ c) ↔ (a → (b ↔ c)) := ⟨λ h ha, by simp [ha] at h; exact h, and_congr_right⟩ @[simp] lemma and.congr_left_iff : (a ∧ c ↔ b ∧ c) ↔ c → (a ↔ b) := by simp only [and.comm, ← and.congr_right_iff] @[simp] lemma and_self_left : a ∧ a ∧ b ↔ a ∧ b := ⟨λ h, ⟨h.1, h.2.2⟩, λ h, ⟨h.1, h.1, h.2⟩⟩ @[simp] lemma and_self_right : (a ∧ b) ∧ b ↔ a ∧ b := ⟨λ h, ⟨h.1.1, h.2⟩, λ h, ⟨⟨h.1, h.2⟩, h.2⟩⟩ /-! ### Declarations about `or` -/ theorem or.right_comm : (a ∨ b) ∨ c ↔ (a ∨ c) ∨ b := by rw [or_assoc, or_assoc, or_comm b] theorem or_of_or_of_imp_of_imp (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → d) : c ∨ d := or.imp h₂ h₃ h₁ theorem or_of_or_of_imp_left (h₁ : a ∨ c) (h : a → b) : b ∨ c := or.imp_left h h₁ theorem or_of_or_of_imp_right (h₁ : c ∨ a) (h : a → b) : c ∨ b := or.imp_right h h₁ theorem or.elim3 (h : a ∨ b ∨ c) (ha : a → d) (hb : b → d) (hc : c → d) : d := or.elim h ha (assume h₂, or.elim h₂ hb hc) theorem or_imp_distrib : (a ∨ b → c) ↔ (a → c) ∧ (b → c) := ⟨assume h, ⟨assume ha, h (or.inl ha), assume hb, h (or.inr hb)⟩, assume ⟨ha, hb⟩, or.rec ha hb⟩ -- See Note [decidable namespace] protected theorem decidable.or_iff_not_imp_left [decidable a] : a ∨ b ↔ (¬ a → b) := ⟨or.resolve_left, λ h, dite _ or.inl (or.inr ∘ h)⟩ theorem or_iff_not_imp_left : a ∨ b ↔ (¬ a → b) := decidable.or_iff_not_imp_left -- See Note [decidable namespace] protected theorem decidable.or_iff_not_imp_right [decidable b] : a ∨ b ↔ (¬ b → a) := or.comm.trans decidable.or_iff_not_imp_left theorem or_iff_not_imp_right : a ∨ b ↔ (¬ b → a) := decidable.or_iff_not_imp_right -- See Note [decidable namespace] protected theorem decidable.not_imp_not [decidable a] : (¬ a → ¬ b) ↔ (b → a) := ⟨assume h hb, decidable.by_contradiction $ assume na, h na hb, mt⟩ theorem not_imp_not : (¬ a → ¬ b) ↔ (b → a) := decidable.not_imp_not /-! ### Declarations about distributivity -/ /-- `∧` distributes over `∨` (on the left). -/ theorem and_or_distrib_left : a ∧ (b ∨ c) ↔ (a ∧ b) ∨ (a ∧ c) := ⟨λ ⟨ha, hbc⟩, hbc.imp (and.intro ha) (and.intro ha), or.rec (and.imp_right or.inl) (and.imp_right or.inr)⟩ /-- `∧` distributes over `∨` (on the right). -/ theorem or_and_distrib_right : (a ∨ b) ∧ c ↔ (a ∧ c) ∨ (b ∧ c) := (and.comm.trans and_or_distrib_left).trans (or_congr and.comm and.comm) /-- `∨` distributes over `∧` (on the left). -/ theorem or_and_distrib_left : a ∨ (b ∧ c) ↔ (a ∨ b) ∧ (a ∨ c) := ⟨or.rec (λha, and.intro (or.inl ha) (or.inl ha)) (and.imp or.inr or.inr), and.rec $ or.rec (imp_intro ∘ or.inl) (or.imp_right ∘ and.intro)⟩ /-- `∨` distributes over `∧` (on the right). -/ theorem and_or_distrib_right : (a ∧ b) ∨ c ↔ (a ∨ c) ∧ (b ∨ c) := (or.comm.trans or_and_distrib_left).trans (and_congr or.comm or.comm) @[simp] lemma or_self_left : a ∨ a ∨ b ↔ a ∨ b := ⟨λ h, h.elim or.inl id, λ h, h.elim or.inl (or.inr ∘ or.inr)⟩ @[simp] lemma or_self_right : (a ∨ b) ∨ b ↔ a ∨ b := ⟨λ h, h.elim id or.inr, λ h, h.elim (or.inl ∘ or.inl) or.inr⟩ /-! Declarations about `iff` -/ theorem iff_of_true (ha : a) (hb : b) : a ↔ b := ⟨λ_, hb, λ _, ha⟩ theorem iff_of_false (ha : ¬a) (hb : ¬b) : a ↔ b := ⟨ha.elim, hb.elim⟩ theorem iff_true_left (ha : a) : (a ↔ b) ↔ b := ⟨λ h, h.1 ha, iff_of_true ha⟩ theorem iff_true_right (ha : a) : (b ↔ a) ↔ b := iff.comm.trans (iff_true_left ha) theorem iff_false_left (ha : ¬a) : (a ↔ b) ↔ ¬b := ⟨λ h, mt h.2 ha, iff_of_false ha⟩ theorem iff_false_right (ha : ¬a) : (b ↔ a) ↔ ¬b := iff.comm.trans (iff_false_left ha) -- See Note [decidable namespace] protected theorem decidable.not_or_of_imp [decidable a] (h : a → b) : ¬ a ∨ b := if ha : a then or.inr (h ha) else or.inl ha theorem not_or_of_imp : (a → b) → ¬ a ∨ b := decidable.not_or_of_imp -- See Note [decidable namespace] protected theorem decidable.imp_iff_not_or [decidable a] : (a → b) ↔ (¬ a ∨ b) := ⟨decidable.not_or_of_imp, or.neg_resolve_left⟩ theorem imp_iff_not_or : (a → b) ↔ (¬ a ∨ b) := decidable.imp_iff_not_or -- See Note [decidable namespace] protected theorem decidable.imp_or_distrib [decidable a] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by simp [decidable.imp_iff_not_or, or.comm, or.left_comm] theorem imp_or_distrib : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := decidable.imp_or_distrib -- See Note [decidable namespace] protected theorem decidable.imp_or_distrib' [decidable b] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by by_cases b; simp [h, or_iff_right_of_imp ((∘) false.elim)] theorem imp_or_distrib' : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := decidable.imp_or_distrib' theorem not_imp_of_and_not : a ∧ ¬ b → ¬ (a → b) | ⟨ha, hb⟩ h := hb $ h ha -- See Note [decidable namespace] protected theorem decidable.not_imp [decidable a] : ¬(a → b) ↔ a ∧ ¬b := ⟨λ h, ⟨decidable.of_not_imp h, not_of_not_imp h⟩, not_imp_of_and_not⟩ theorem not_imp : ¬(a → b) ↔ a ∧ ¬b := decidable.not_imp -- for monotonicity lemma imp_imp_imp (h₀ : c → a) (h₁ : b → d) : (a → b) → (c → d) := assume (h₂ : a → b), h₁ ∘ h₂ ∘ h₀ -- See Note [decidable namespace] protected theorem decidable.peirce (a b : Prop) [decidable a] : ((a → b) → a) → a := if ha : a then λ h, ha else λ h, h ha.elim theorem peirce (a b : Prop) : ((a → b) → a) → a := decidable.peirce _ _ theorem peirce' {a : Prop} (H : ∀ b : Prop, (a → b) → a) : a := H _ id -- See Note [decidable namespace] protected theorem decidable.not_iff_not [decidable a] [decidable b] : (¬ a ↔ ¬ b) ↔ (a ↔ b) := by rw [@iff_def (¬ a), @iff_def' a]; exact and_congr decidable.not_imp_not decidable.not_imp_not theorem not_iff_not : (¬ a ↔ ¬ b) ↔ (a ↔ b) := decidable.not_iff_not -- See Note [decidable namespace] protected theorem decidable.not_iff_comm [decidable a] [decidable b] : (¬ a ↔ b) ↔ (¬ b ↔ a) := by rw [@iff_def (¬ a), @iff_def (¬ b)]; exact and_congr decidable.not_imp_comm imp_not_comm theorem not_iff_comm : (¬ a ↔ b) ↔ (¬ b ↔ a) := decidable.not_iff_comm -- See Note [decidable namespace] protected theorem decidable.not_iff : ∀ [decidable b], ¬ (a ↔ b) ↔ (¬ a ↔ b) := by intro h; cases h; simp only [h, iff_true, iff_false] theorem not_iff : ¬ (a ↔ b) ↔ (¬ a ↔ b) := decidable.not_iff -- See Note [decidable namespace] protected theorem decidable.iff_not_comm [decidable a] [decidable b] : (a ↔ ¬ b) ↔ (b ↔ ¬ a) := by rw [@iff_def a, @iff_def b]; exact and_congr imp_not_comm decidable.not_imp_comm theorem iff_not_comm : (a ↔ ¬ b) ↔ (b ↔ ¬ a) := decidable.iff_not_comm -- See Note [decidable namespace] protected theorem decidable.iff_iff_and_or_not_and_not [decidable b] : (a ↔ b) ↔ (a ∧ b) ∨ (¬ a ∧ ¬ b) := by { split; intro h, { rw h; by_cases b; [left,right]; split; assumption }, { cases h with h h; cases h; split; intro; { contradiction <|> assumption } } } theorem iff_iff_and_or_not_and_not : (a ↔ b) ↔ (a ∧ b) ∨ (¬ a ∧ ¬ b) := decidable.iff_iff_and_or_not_and_not lemma decidable.iff_iff_not_or_and_or_not [decidable a] [decidable b] : (a ↔ b) ↔ ((¬a ∨ b) ∧ (a ∨ ¬b)) := begin rw [iff_iff_implies_and_implies a b], simp only [decidable.imp_iff_not_or, or.comm] end lemma iff_iff_not_or_and_or_not : (a ↔ b) ↔ ((¬a ∨ b) ∧ (a ∨ ¬b)) := decidable.iff_iff_not_or_and_or_not -- See Note [decidable namespace] protected theorem decidable.not_and_not_right [decidable b] : ¬(a ∧ ¬b) ↔ (a → b) := ⟨λ h ha, h.decidable_imp_symm $ and.intro ha, λ h ⟨ha, hb⟩, hb $ h ha⟩ theorem not_and_not_right : ¬(a ∧ ¬b) ↔ (a → b) := decidable.not_and_not_right /-- Transfer decidability of `a` to decidability of `b`, if the propositions are equivalent. **Important**: this function should be used instead of `rw` on `decidable b`, because the kernel will get stuck reducing the usage of `propext` otherwise, and `dec_trivial` will not work. -/ @[inline] def decidable_of_iff (a : Prop) (h : a ↔ b) [D : decidable a] : decidable b := decidable_of_decidable_of_iff D h /-- Transfer decidability of `b` to decidability of `a`, if the propositions are equivalent. This is the same as `decidable_of_iff` but the iff is flipped. -/ @[inline] def decidable_of_iff' (b : Prop) (h : a ↔ b) [D : decidable b] : decidable a := decidable_of_decidable_of_iff D h.symm /-- Prove that `a` is decidable by constructing a boolean `b` and a proof that `b ↔ a`. (This is sometimes taken as an alternate definition of decidability.) -/ def decidable_of_bool : ∀ (b : bool) (h : b ↔ a), decidable a | tt h := is_true (h.1 rfl) | ff h := is_false (mt h.2 bool.ff_ne_tt) /-! ### De Morgan's laws -/ theorem not_and_of_not_or_not (h : ¬ a ∨ ¬ b) : ¬ (a ∧ b) | ⟨ha, hb⟩ := or.elim h (absurd ha) (absurd hb) -- See Note [decidable namespace] protected theorem decidable.not_and_distrib [decidable a] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if ha : a then or.inr (λ hb, h ⟨ha, hb⟩) else or.inl ha, not_and_of_not_or_not⟩ -- See Note [decidable namespace] protected theorem decidable.not_and_distrib' [decidable b] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if hb : b then or.inl (λ ha, h ⟨ha, hb⟩) else or.inr hb, not_and_of_not_or_not⟩ /-- One of de Morgan's laws: the negation of a conjunction is logically equivalent to the disjunction of the negations. -/ theorem not_and_distrib : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := decidable.not_and_distrib @[simp] theorem not_and : ¬ (a ∧ b) ↔ (a → ¬ b) := and_imp theorem not_and' : ¬ (a ∧ b) ↔ b → ¬a := not_and.trans imp_not_comm /-- One of de Morgan's laws: the negation of a disjunction is logically equivalent to the conjunction of the negations. -/ theorem not_or_distrib : ¬ (a ∨ b) ↔ ¬ a ∧ ¬ b := ⟨λ h, ⟨λ ha, h (or.inl ha), λ hb, h (or.inr hb)⟩, λ ⟨h₁, h₂⟩ h, or.elim h h₁ h₂⟩ -- See Note [decidable namespace] protected theorem decidable.or_iff_not_and_not [decidable a] [decidable b] : a ∨ b ↔ ¬ (¬a ∧ ¬b) := by rw [← not_or_distrib, decidable.not_not] theorem or_iff_not_and_not : a ∨ b ↔ ¬ (¬a ∧ ¬b) := decidable.or_iff_not_and_not -- See Note [decidable namespace] protected theorem decidable.and_iff_not_or_not [decidable a] [decidable b] : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := by rw [← decidable.not_and_distrib, decidable.not_not] theorem and_iff_not_or_not : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := decidable.and_iff_not_or_not end propositional /-! ### Declarations about equality -/ section equality variables {α : Sort*} {a b : α} @[simp] theorem heq_iff_eq : a == b ↔ a = b := ⟨eq_of_heq, heq_of_eq⟩ theorem proof_irrel_heq {p q : Prop} (hp : p) (hq : q) : hp == hq := have p = q, from propext ⟨λ _, hq, λ _, hp⟩, by subst q; refl theorem ne_of_mem_of_not_mem {α β} [has_mem α β] {s : β} {a b : α} (h : a ∈ s) : b ∉ s → a ≠ b := mt $ λ e, e ▸ h theorem eq_equivalence : equivalence (@eq α) := ⟨eq.refl, @eq.symm _, @eq.trans _⟩ /-- Transport through trivial families is the identity. -/ @[simp] lemma eq_rec_constant {α : Sort*} {a a' : α} {β : Sort*} (y : β) (h : a = a') : (@eq.rec α a (λ a, β) y a' h) = y := by { cases h, refl, } @[simp] lemma eq_mp_rfl {α : Sort*} {a : α} : eq.mp (eq.refl α) a = a := rfl @[simp] lemma eq_mpr_rfl {α : Sort*} {a : α} : eq.mpr (eq.refl α) a = a := rfl lemma heq_of_eq_mp : ∀ {α β : Sort*} {a : α} {a' : β} (e : α = β) (h₂ : (eq.mp e a) = a'), a == a' | α ._ a a' rfl h := eq.rec_on h (heq.refl _) lemma rec_heq_of_heq {β} {C : α → Sort*} {x : C a} {y : β} (eq : a = b) (h : x == y) : @eq.rec α a C x b eq == y := by subst eq; exact h @[simp] lemma {u} eq_mpr_heq {α β : Sort u} (h : β = α) (x : α) : eq.mpr h x == x := by subst h; refl protected lemma eq.congr {x₁ x₂ y₁ y₂ : α} (h₁ : x₁ = y₁) (h₂ : x₂ = y₂) : (x₁ = x₂) ↔ (y₁ = y₂) := by { subst h₁, subst h₂ } lemma eq.congr_left {x y z : α} (h : x = y) : x = z ↔ y = z := by rw [h] lemma eq.congr_right {x y z : α} (h : x = y) : z = x ↔ z = y := by rw [h] lemma congr_arg2 {α β γ : Type*} (f : α → β → γ) {x x' : α} {y y' : β} (hx : x = x') (hy : y = y') : f x y = f x' y' := by { subst hx, subst hy } end equality /-! ### Declarations about quantifiers -/ section quantifiers variables {α : Sort*} {β : Sort*} {p q : α → Prop} {b : Prop} lemma forall_imp (h : ∀ a, p a → q a) : (∀ a, p a) → ∀ a, q a := λ h' a, h a (h' a) lemma forall₂_congr {p q : α → β → Prop} (h : ∀ a b, p a b ↔ q a b) : (∀ a b, p a b) ↔ (∀ a b, q a b) := forall_congr (λ a, forall_congr (h a)) lemma forall₃_congr {γ : Sort*} {p q : α → β → γ → Prop} (h : ∀ a b c, p a b c ↔ q a b c) : (∀ a b c, p a b c) ↔ (∀ a b c, q a b c) := forall_congr (λ a, forall₂_congr (h a)) lemma forall₄_congr {γ δ : Sort*} {p q : α → β → γ → δ → Prop} (h : ∀ a b c d, p a b c d ↔ q a b c d) : (∀ a b c d, p a b c d) ↔ (∀ a b c d, q a b c d) := forall_congr (λ a, forall₃_congr (h a)) lemma Exists.imp (h : ∀ a, (p a → q a)) (p : ∃ a, p a) : ∃ a, q a := exists_imp_exists h p lemma exists_imp_exists' {p : α → Prop} {q : β → Prop} (f : α → β) (hpq : ∀ a, p a → q (f a)) (hp : ∃ a, p a) : ∃ b, q b := exists.elim hp (λ a hp', ⟨_, hpq _ hp'⟩) lemma exists₂_congr {p q : α → β → Prop} (h : ∀ a b, p a b ↔ q a b) : (∃ a b, p a b) ↔ (∃ a b, q a b) := exists_congr (λ a, exists_congr (h a)) lemma exists₃_congr {γ : Sort*} {p q : α → β → γ → Prop} (h : ∀ a b c, p a b c ↔ q a b c) : (∃ a b c, p a b c) ↔ (∃ a b c, q a b c) := exists_congr (λ a, exists₂_congr (h a)) lemma exists₄_congr {γ δ : Sort*} {p q : α → β → γ → δ → Prop} (h : ∀ a b c d, p a b c d ↔ q a b c d) : (∃ a b c d, p a b c d) ↔ (∃ a b c d, q a b c d) := exists_congr (λ a, exists₃_congr (h a)) theorem forall_swap {p : α → β → Prop} : (∀ x y, p x y) ↔ ∀ y x, p x y := ⟨function.swap, function.swap⟩ theorem exists_swap {p : α → β → Prop} : (∃ x y, p x y) ↔ ∃ y x, p x y := ⟨λ ⟨x, y, h⟩, ⟨y, x, h⟩, λ ⟨y, x, h⟩, ⟨x, y, h⟩⟩ @[simp] theorem exists_imp_distrib : ((∃ x, p x) → b) ↔ ∀ x, p x → b := ⟨λ h x hpx, h ⟨x, hpx⟩, λ h ⟨x, hpx⟩, h x hpx⟩ /-- Extract an element from a existential statement, using `classical.some`. -/ -- This enables projection notation. @[reducible] noncomputable def Exists.some {p : α → Prop} (P : ∃ a, p a) : α := classical.some P /-- Show that an element extracted from `P : ∃ a, p a` using `P.some` satisfies `p`. -/ lemma Exists.some_spec {p : α → Prop} (P : ∃ a, p a) : p (P.some) := classical.some_spec P --theorem forall_not_of_not_exists (h : ¬ ∃ x, p x) : ∀ x, ¬ p x := --forall_imp_of_exists_imp h theorem not_exists_of_forall_not (h : ∀ x, ¬ p x) : ¬ ∃ x, p x := exists_imp_distrib.2 h @[simp] theorem not_exists : (¬ ∃ x, p x) ↔ ∀ x, ¬ p x := exists_imp_distrib theorem not_forall_of_exists_not : (∃ x, ¬ p x) → ¬ ∀ x, p x | ⟨x, hn⟩ h := hn (h x) -- See Note [decidable namespace] protected theorem decidable.not_forall {p : α → Prop} [decidable (∃ x, ¬ p x)] [∀ x, decidable (p x)] : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x := ⟨not.decidable_imp_symm $ λ nx x, nx.decidable_imp_symm $ λ h, ⟨x, h⟩, not_forall_of_exists_not⟩ @[simp] theorem not_forall {p : α → Prop} : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x := decidable.not_forall -- See Note [decidable namespace] protected theorem decidable.not_forall_not [decidable (∃ x, p x)] : (¬ ∀ x, ¬ p x) ↔ ∃ x, p x := (@decidable.not_iff_comm _ _ _ (decidable_of_iff (¬ ∃ x, p x) not_exists)).1 not_exists theorem not_forall_not : (¬ ∀ x, ¬ p x) ↔ ∃ x, p x := decidable.not_forall_not -- See Note [decidable namespace] protected theorem decidable.not_exists_not [∀ x, decidable (p x)] : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := by simp [decidable.not_not] @[simp] theorem not_exists_not : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := decidable.not_exists_not @[simp] theorem forall_true_iff : (α → true) ↔ true := iff_true_intro (λ _, trivial) -- Unfortunately this causes simp to loop sometimes, so we -- add the 2 and 3 cases as simp lemmas instead theorem forall_true_iff' (h : ∀ a, p a ↔ true) : (∀ a, p a) ↔ true := iff_true_intro (λ _, of_iff_true (h _)) @[simp] theorem forall_2_true_iff {β : α → Sort*} : (∀ a, β a → true) ↔ true := forall_true_iff' $ λ _, forall_true_iff @[simp] theorem forall_3_true_iff {β : α → Sort*} {γ : Π a, β a → Sort*} : (∀ a (b : β a), γ a b → true) ↔ true := forall_true_iff' $ λ _, forall_2_true_iff @[simp] theorem forall_const (α : Sort*) [i : nonempty α] : (α → b) ↔ b := ⟨i.elim, λ hb x, hb⟩ @[simp] theorem exists_const (α : Sort*) [i : nonempty α] : (∃ x : α, b) ↔ b := ⟨λ ⟨x, h⟩, h, i.elim exists.intro⟩ theorem forall_and_distrib : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) := ⟨λ h, ⟨λ x, (h x).left, λ x, (h x).right⟩, λ ⟨h₁, h₂⟩ x, ⟨h₁ x, h₂ x⟩⟩ theorem exists_or_distrib : (∃ x, p x ∨ q x) ↔ (∃ x, p x) ∨ (∃ x, q x) := ⟨λ ⟨x, hpq⟩, hpq.elim (λ hpx, or.inl ⟨x, hpx⟩) (λ hqx, or.inr ⟨x, hqx⟩), λ hepq, hepq.elim (λ ⟨x, hpx⟩, ⟨x, or.inl hpx⟩) (λ ⟨x, hqx⟩, ⟨x, or.inr hqx⟩)⟩ @[simp] theorem exists_and_distrib_left {q : Prop} {p : α → Prop} : (∃x, q ∧ p x) ↔ q ∧ (∃x, p x) := ⟨λ ⟨x, hq, hp⟩, ⟨hq, x, hp⟩, λ ⟨hq, x, hp⟩, ⟨x, hq, hp⟩⟩ @[simp] theorem exists_and_distrib_right {q : Prop} {p : α → Prop} : (∃x, p x ∧ q) ↔ (∃x, p x) ∧ q := by simp [and_comm] @[simp] theorem forall_eq {a' : α} : (∀a, a = a' → p a) ↔ p a' := ⟨λ h, h a' rfl, λ h a e, e.symm ▸ h⟩ @[simp] theorem forall_eq' {a' : α} : (∀a, a' = a → p a) ↔ p a' := by simp [@eq_comm _ a'] -- this lemma is needed to simplify the output of `list.mem_cons_iff` @[simp] theorem forall_eq_or_imp {a' : α} : (∀ a, a = a' ∨ q a → p a) ↔ p a' ∧ ∀ a, q a → p a := by simp only [or_imp_distrib, forall_and_distrib, forall_eq] @[simp] theorem exists_eq {a' : α} : ∃ a, a = a' := ⟨_, rfl⟩ @[simp] theorem exists_eq' {a' : α} : ∃ a, a' = a := ⟨_, rfl⟩ @[simp] theorem exists_eq_left {a' : α} : (∃ a, a = a' ∧ p a) ↔ p a' := ⟨λ ⟨a, e, h⟩, e ▸ h, λ h, ⟨_, rfl, h⟩⟩ @[simp] theorem exists_eq_right {a' : α} : (∃ a, p a ∧ a = a') ↔ p a' := (exists_congr $ by exact λ a, and.comm).trans exists_eq_left @[simp] theorem exists_apply_eq_apply {α β : Type*} (f : α → β) (a' : α) : ∃ a, f a = f a' := ⟨a', rfl⟩ @[simp] theorem exists_apply_eq_apply' {α β : Type*} (f : α → β) (a' : α) : ∃ a, f a' = f a := ⟨a', rfl⟩ @[simp] theorem exists_exists_and_eq_and {f : α → β} {p : α → Prop} {q : β → Prop} : (∃ b, (∃ a, p a ∧ f a = b) ∧ q b) ↔ ∃ a, p a ∧ q (f a) := ⟨λ ⟨b, ⟨a, ha, hab⟩, hb⟩, ⟨a, ha, hab.symm ▸ hb⟩, λ ⟨a, hp, hq⟩, ⟨f a, ⟨a, hp, rfl⟩, hq⟩⟩ @[simp] theorem exists_exists_eq_and {f : α → β} {p : β → Prop} : (∃ b, (∃ a, f a = b) ∧ p b) ↔ ∃ a, p (f a) := ⟨λ ⟨b, ⟨a, ha⟩, hb⟩, ⟨a, ha.symm ▸ hb⟩, λ ⟨a, ha⟩, ⟨f a, ⟨a, rfl⟩, ha⟩⟩ @[simp] theorem forall_apply_eq_imp_iff {f : α → β} {p : β → Prop} : (∀ a, ∀ b, f a = b → p b) ↔ (∀ a, p (f a)) := ⟨λ h a, h a (f a) rfl, λ h a b hab, hab ▸ h a⟩ @[simp] theorem forall_apply_eq_imp_iff' {f : α → β} {p : β → Prop} : (∀ b, ∀ a, f a = b → p b) ↔ (∀ a, p (f a)) := by { rw forall_swap, simp } @[simp] theorem forall_eq_apply_imp_iff {f : α → β} {p : β → Prop} : (∀ a, ∀ b, b = f a → p b) ↔ (∀ a, p (f a)) := by simp [@eq_comm _ _ (f _)] @[simp] theorem forall_eq_apply_imp_iff' {f : α → β} {p : β → Prop} : (∀ b, ∀ a, b = f a → p b) ↔ (∀ a, p (f a)) := by { rw forall_swap, simp } @[simp] theorem forall_apply_eq_imp_iff₂ {f : α → β} {p : α → Prop} {q : β → Prop} : (∀ b, ∀ a, p a → f a = b → q b) ↔ ∀ a, p a → q (f a) := ⟨λ h a ha, h (f a) a ha rfl, λ h b a ha hb, hb ▸ h a ha⟩ @[simp] theorem exists_eq_left' {a' : α} : (∃ a, a' = a ∧ p a) ↔ p a' := by simp [@eq_comm _ a'] @[simp] theorem exists_eq_right' {a' : α} : (∃ a, p a ∧ a' = a) ↔ p a' := by simp [@eq_comm _ a'] theorem exists_comm {p : α → β → Prop} : (∃ a b, p a b) ↔ ∃ b a, p a b := ⟨λ ⟨a, b, h⟩, ⟨b, a, h⟩, λ ⟨b, a, h⟩, ⟨a, b, h⟩⟩ theorem forall_or_of_or_forall (h : b ∨ ∀x, p x) (x) : b ∨ p x := h.imp_right $ λ h₂, h₂ x -- See Note [decidable namespace] protected theorem decidable.forall_or_distrib_left {q : Prop} {p : α → Prop} [decidable q] : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := ⟨λ h, if hq : q then or.inl hq else or.inr $ λ x, (h x).resolve_left hq, forall_or_of_or_forall⟩ theorem forall_or_distrib_left {q : Prop} {p : α → Prop} : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := decidable.forall_or_distrib_left -- See Note [decidable namespace] protected theorem decidable.forall_or_distrib_right {q : Prop} {p : α → Prop} [decidable q] : (∀x, p x ∨ q) ↔ (∀x, p x) ∨ q := by simp [or_comm, decidable.forall_or_distrib_left] theorem forall_or_distrib_right {q : Prop} {p : α → Prop} : (∀x, p x ∨ q) ↔ (∀x, p x) ∨ q := decidable.forall_or_distrib_right /-- A predicate holds everywhere on the image of a surjective functions iff it holds everywhere. -/ theorem forall_iff_forall_surj {α β : Type*} {f : α → β} (h : function.surjective f) {P : β → Prop} : (∀ a, P (f a)) ↔ ∀ b, P b := ⟨λ ha b, by cases h b with a hab; rw ←hab; exact ha a, λ hb a, hb $ f a⟩ @[simp] theorem exists_prop {p q : Prop} : (∃ h : p, q) ↔ p ∧ q := ⟨λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩⟩ @[simp] theorem exists_false : ¬ (∃a:α, false) := assume ⟨a, h⟩, h theorem Exists.fst {p : b → Prop} : Exists p → b | ⟨h, _⟩ := h theorem Exists.snd {p : b → Prop} : ∀ h : Exists p, p h.fst | ⟨_, h⟩ := h @[simp] theorem forall_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∀ h' : p, q h') ↔ q h := @forall_const (q h) p ⟨h⟩ @[simp] theorem exists_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∃ h' : p, q h') ↔ q h := @exists_const (q h) p ⟨h⟩ @[simp] theorem forall_prop_of_false {p : Prop} {q : p → Prop} (hn : ¬ p) : (∀ h' : p, q h') ↔ true := iff_true_intro $ λ h, hn.elim h @[simp] theorem exists_prop_of_false {p : Prop} {q : p → Prop} : ¬ p → ¬ (∃ h' : p, q h') := mt Exists.fst lemma exists_unique.exists {α : Sort*} {p : α → Prop} (h : ∃! x, p x) : ∃ x, p x := exists.elim h (λ x hx, ⟨x, and.left hx⟩) lemma exists_unique.unique {α : Sort*} {p : α → Prop} (h : ∃! x, p x) {y₁ y₂ : α} (py₁ : p y₁) (py₂ : p y₂) : y₁ = y₂ := unique_of_exists_unique h py₁ py₂ @[simp] lemma exists_unique_iff_exists {α : Sort*} [subsingleton α] {p : α → Prop} : (∃! x, p x) ↔ ∃ x, p x := ⟨λ h, h.exists, Exists.imp $ λ x hx, ⟨hx, λ y _, subsingleton.elim y x⟩⟩ lemma exists_unique.elim2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)] {q : Π x (h : p x), Prop} {b : Prop} (h₂ : ∃! x (h : p x), q x h) (h₁ : ∀ x (h : p x), q x h → (∀ y (hy : p y), q y hy → y = x) → b) : b := begin simp only [exists_unique_iff_exists] at h₂, apply h₂.elim, exact λ x ⟨hxp, hxq⟩ H, h₁ x hxp hxq (λ y hyp hyq, H y ⟨hyp, hyq⟩) end lemma exists_unique.intro2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)] {q : Π (x : α) (h : p x), Prop} (w : α) (hp : p w) (hq : q w hp) (H : ∀ y (hy : p y), q y hy → y = w) : ∃! x (hx : p x), q x hx := begin simp only [exists_unique_iff_exists], exact exists_unique.intro w ⟨hp, hq⟩ (λ y ⟨hyp, hyq⟩, H y hyp hyq) end lemma exists_unique.exists2 {α : Sort*} {p : α → Sort*} {q : Π (x : α) (h : p x), Prop} (h : ∃! x (hx : p x), q x hx) : ∃ x (hx : p x), q x hx := h.exists.imp (λ x hx, hx.exists) lemma exists_unique.unique2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)] {q : Π (x : α) (hx : p x), Prop} (h : ∃! x (hx : p x), q x hx) {y₁ y₂ : α} (hpy₁ : p y₁) (hqy₁ : q y₁ hpy₁) (hpy₂ : p y₂) (hqy₂ : q y₂ hpy₂) : y₁ = y₂ := begin simp only [exists_unique_iff_exists] at h, exact h.unique ⟨hpy₁, hqy₁⟩ ⟨hpy₂, hqy₂⟩ end end quantifiers /-! ### Classical lemmas -/ namespace classical variables {α : Sort*} {p : α → Prop} theorem cases {p : Prop → Prop} (h1 : p true) (h2 : p false) : ∀a, p a := assume a, cases_on a h1 h2 /- use shortened names to avoid conflict when classical namespace is open. -/ noncomputable lemma dec (p : Prop) : decidable p := -- see Note [classical lemma] by apply_instance noncomputable lemma dec_pred (p : α → Prop) : decidable_pred p := -- see Note [classical lemma] by apply_instance noncomputable lemma dec_rel (p : α → α → Prop) : decidable_rel p := -- see Note [classical lemma] by apply_instance noncomputable lemma dec_eq (α : Sort*) : decidable_eq α := -- see Note [classical lemma] by apply_instance /-- We make decidability results that depends on `classical.choice` noncomputable lemmas. * We have to mark them as noncomputable, because otherwise Lean will try to generate bytecode for them, and fail because it depends on `classical.choice`. * We make them lemmas, and not definitions, because otherwise later definitions will raise \"failed to generate bytecode\" errors when writing something like `letI := classical.dec_eq _`. Cf. <https://leanprover-community.github.io/archive/113488general/08268noncomputabletheorem.html> -/ library_note "classical lemma" /-- Construct a function from a default value `H0`, and a function to use if there exists a value satisfying the predicate. -/ @[elab_as_eliminator] noncomputable def {u} exists_cases {C : Sort u} (H0 : C) (H : ∀ a, p a → C) : C := if h : ∃ a, p a then H (classical.some h) (classical.some_spec h) else H0 lemma some_spec2 {α : Sort*} {p : α → Prop} {h : ∃a, p a} (q : α → Prop) (hpq : ∀a, p a → q a) : q (some h) := hpq _ $ some_spec _ /-- A version of classical.indefinite_description which is definitionally equal to a pair -/ noncomputable def subtype_of_exists {α : Type*} {P : α → Prop} (h : ∃ x, P x) : {x // P x} := ⟨classical.some h, classical.some_spec h⟩ end classical /-- This function has the same type as `exists.rec_on`, and can be used to case on an equality, but `exists.rec_on` can only eliminate into Prop, while this version eliminates into any universe using the axiom of choice. -/ @[elab_as_eliminator] noncomputable def {u} exists.classical_rec_on {α} {p : α → Prop} (h : ∃ a, p a) {C : Sort u} (H : ∀ a, p a → C) : C := H (classical.some h) (classical.some_spec h) /-! ### Declarations about bounded quantifiers -/ section bounded_quantifiers variables {α : Sort*} {r p q : α → Prop} {P Q : ∀ x, p x → Prop} {b : Prop} theorem bex_def : (∃ x (h : p x), q x) ↔ ∃ x, p x ∧ q x := ⟨λ ⟨x, px, qx⟩, ⟨x, px, qx⟩, λ ⟨x, px, qx⟩, ⟨x, px, qx⟩⟩ theorem bex.elim {b : Prop} : (∃ x h, P x h) → (∀ a h, P a h → b) → b | ⟨a, h₁, h₂⟩ h' := h' a h₁ h₂ theorem bex.intro (a : α) (h₁ : p a) (h₂ : P a h₁) : ∃ x (h : p x), P x h := ⟨a, h₁, h₂⟩ theorem ball_congr (H : ∀ x h, P x h ↔ Q x h) : (∀ x h, P x h) ↔ (∀ x h, Q x h) := forall_congr $ λ x, forall_congr (H x) theorem bex_congr (H : ∀ x h, P x h ↔ Q x h) : (∃ x h, P x h) ↔ (∃ x h, Q x h) := exists_congr $ λ x, exists_congr (H x) theorem ball.imp_right (H : ∀ x h, (P x h → Q x h)) (h₁ : ∀ x h, P x h) (x h) : Q x h := H _ _ $ h₁ _ _ theorem bex.imp_right (H : ∀ x h, (P x h → Q x h)) : (∃ x h, P x h) → ∃ x h, Q x h | ⟨x, h, h'⟩ := ⟨_, _, H _ _ h'⟩ theorem ball.imp_left (H : ∀ x, p x → q x) (h₁ : ∀ x, q x → r x) (x) (h : p x) : r x := h₁ _ $ H _ h theorem bex.imp_left (H : ∀ x, p x → q x) : (∃ x (_ : p x), r x) → ∃ x (_ : q x), r x | ⟨x, hp, hr⟩ := ⟨x, H _ hp, hr⟩ theorem ball_of_forall (h : ∀ x, p x) (x) : p x := h x theorem forall_of_ball (H : ∀ x, p x) (h : ∀ x, p x → q x) (x) : q x := h x $ H x theorem bex_of_exists (H : ∀ x, p x) : (∃ x, q x) → ∃ x (_ : p x), q x | ⟨x, hq⟩ := ⟨x, H x, hq⟩ theorem exists_of_bex : (∃ x (_ : p x), q x) → ∃ x, q x | ⟨x, _, hq⟩ := ⟨x, hq⟩ @[simp] theorem bex_imp_distrib : ((∃ x h, P x h) → b) ↔ (∀ x h, P x h → b) := by simp theorem not_bex : (¬ ∃ x h, P x h) ↔ ∀ x h, ¬ P x h := bex_imp_distrib theorem not_ball_of_bex_not : (∃ x h, ¬ P x h) → ¬ ∀ x h, P x h | ⟨x, h, hp⟩ al := hp $ al x h -- See Note [decidable namespace] protected theorem decidable.not_ball [decidable (∃ x h, ¬ P x h)] [∀ x h, decidable (P x h)] : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := ⟨not.decidable_imp_symm $ λ nx x h, nx.decidable_imp_symm $ λ h', ⟨x, h, h'⟩, not_ball_of_bex_not⟩ theorem not_ball : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := decidable.not_ball theorem ball_true_iff (p : α → Prop) : (∀ x, p x → true) ↔ true := iff_true_intro (λ h hrx, trivial) theorem ball_and_distrib : (∀ x h, P x h ∧ Q x h) ↔ (∀ x h, P x h) ∧ (∀ x h, Q x h) := iff.trans (forall_congr $ λ x, forall_and_distrib) forall_and_distrib theorem bex_or_distrib : (∃ x h, P x h ∨ Q x h) ↔ (∃ x h, P x h) ∨ (∃ x h, Q x h) := iff.trans (exists_congr $ λ x, exists_or_distrib) exists_or_distrib end bounded_quantifiers namespace classical local attribute [instance] prop_decidable theorem not_ball {α : Sort*} {p : α → Prop} {P : Π (x : α), p x → Prop} : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := _root_.not_ball end classical lemma ite_eq_iff {α} {p : Prop} [decidable p] {a b c : α} : (if p then a else b) = c ↔ p ∧ a = c ∨ ¬p ∧ b = c := by by_cases p; simp * @[simp] lemma ite_eq_left_iff {α} {p : Prop} [decidable p] {a b : α} : (if p then a else b) = a ↔ (¬p → b = a) := by by_cases p; simp * @[simp] lemma ite_eq_right_iff {α} {p : Prop} [decidable p] {a b : α} : (if p then a else b) = b ↔ (p → a = b) := by by_cases p; simp * /-! ### Declarations about `nonempty` -/ section nonempty universe variables u v w variables {α : Type u} {β : Type v} {γ : α → Type w} attribute [simp] nonempty_of_inhabited @[priority 20] instance has_zero.nonempty [has_zero α] : nonempty α := ⟨0⟩ @[priority 20] instance has_one.nonempty [has_one α] : nonempty α := ⟨1⟩ lemma exists_true_iff_nonempty {α : Sort*} : (∃a:α, true) ↔ nonempty α := iff.intro (λ⟨a, _⟩, ⟨a⟩) (λ⟨a⟩, ⟨a, trivial⟩) @[simp] lemma nonempty_Prop {p : Prop} : nonempty p ↔ p := iff.intro (assume ⟨h⟩, h) (assume h, ⟨h⟩) lemma not_nonempty_iff_imp_false : ¬ nonempty α ↔ α → false := ⟨λ h a, h ⟨a⟩, λ h ⟨a⟩, h a⟩ @[simp] lemma nonempty_sigma : nonempty (Σa:α, γ a) ↔ (∃a:α, nonempty (γ a)) := iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩) @[simp] lemma nonempty_subtype {α : Sort u} {p : α → Prop} : nonempty (subtype p) ↔ (∃a:α, p a) := iff.intro (assume ⟨⟨a, h⟩⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a, h⟩⟩) @[simp] lemma nonempty_prod : nonempty (α × β) ↔ (nonempty α ∧ nonempty β) := iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩) @[simp] lemma nonempty_pprod {α : Sort u} {β : Sort v} : nonempty (pprod α β) ↔ (nonempty α ∧ nonempty β) := iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩) @[simp] lemma nonempty_sum : nonempty (α ⊕ β) ↔ (nonempty α ∨ nonempty β) := iff.intro (assume ⟨h⟩, match h with sum.inl a := or.inl ⟨a⟩ | sum.inr b := or.inr ⟨b⟩ end) (assume h, match h with or.inl ⟨a⟩ := ⟨sum.inl a⟩ | or.inr ⟨b⟩ := ⟨sum.inr b⟩ end) @[simp] lemma nonempty_psum {α : Sort u} {β : Sort v} : nonempty (psum α β) ↔ (nonempty α ∨ nonempty β) := iff.intro (assume ⟨h⟩, match h with psum.inl a := or.inl ⟨a⟩ | psum.inr b := or.inr ⟨b⟩ end) (assume h, match h with or.inl ⟨a⟩ := ⟨psum.inl a⟩ | or.inr ⟨b⟩ := ⟨psum.inr b⟩ end) @[simp] lemma nonempty_psigma {α : Sort u} {β : α → Sort v} : nonempty (psigma β) ↔ (∃a:α, nonempty (β a)) := iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩) @[simp] lemma nonempty_empty : ¬ nonempty empty := assume ⟨h⟩, h.elim @[simp] lemma nonempty_ulift : nonempty (ulift α) ↔ nonempty α := iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩) @[simp] lemma nonempty_plift {α : Sort u} : nonempty (plift α) ↔ nonempty α := iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩) @[simp] lemma nonempty.forall {α : Sort u} {p : nonempty α → Prop} : (∀h:nonempty α, p h) ↔ (∀a, p ⟨a⟩) := iff.intro (assume h a, h _) (assume h ⟨a⟩, h _) @[simp] lemma nonempty.exists {α : Sort u} {p : nonempty α → Prop} : (∃h:nonempty α, p h) ↔ (∃a, p ⟨a⟩) := iff.intro (assume ⟨⟨a⟩, h⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a⟩, h⟩) lemma classical.nonempty_pi {α : Sort u} {β : α → Sort v} : nonempty (Πa:α, β a) ↔ (∀a:α, nonempty (β a)) := iff.intro (assume ⟨f⟩ a, ⟨f a⟩) (assume f, ⟨assume a, classical.choice $ f a⟩) /-- Using `classical.choice`, lifts a (`Prop`-valued) `nonempty` instance to a (`Type`-valued) `inhabited` instance. `classical.inhabited_of_nonempty` already exists, in `core/init/classical.lean`, but the assumption is not a type class argument, which makes it unsuitable for some applications. -/ noncomputable def classical.inhabited_of_nonempty' {α : Sort u} [h : nonempty α] : inhabited α := ⟨classical.choice h⟩ /-- Using `classical.choice`, extracts a term from a `nonempty` type. -/ @[reducible] protected noncomputable def nonempty.some {α : Sort u} (h : nonempty α) : α := classical.choice h /-- Using `classical.choice`, extracts a term from a `nonempty` type. -/ @[reducible] protected noncomputable def classical.arbitrary (α : Sort u) [h : nonempty α] : α := classical.choice h /-- Given `f : α → β`, if `α` is nonempty then `β` is also nonempty. `nonempty` cannot be a `functor`, because `functor` is restricted to `Type`. -/ lemma nonempty.map {α : Sort u} {β : Sort v} (f : α → β) : nonempty α → nonempty β | ⟨h⟩ := ⟨f h⟩ protected lemma nonempty.map2 {α β γ : Sort*} (f : α → β → γ) : nonempty α → nonempty β → nonempty γ | ⟨x⟩ ⟨y⟩ := ⟨f x y⟩ protected lemma nonempty.congr {α : Sort u} {β : Sort v} (f : α → β) (g : β → α) : nonempty α ↔ nonempty β := ⟨nonempty.map f, nonempty.map g⟩ lemma nonempty.elim_to_inhabited {α : Sort*} [h : nonempty α] {p : Prop} (f : inhabited α → p) : p := h.elim $ f ∘ inhabited.mk instance {α β} [h : nonempty α] [h2 : nonempty β] : nonempty (α × β) := h.elim $ λ g, h2.elim $ λ g2, ⟨⟨g, g2⟩⟩ end nonempty section ite /-- A function applied to a `dite` is a `dite` of that function applied to each of the branches. -/ lemma apply_dite {α β : Sort*} (f : α → β) (P : Prop) [decidable P] (x : P → α) (y : ¬P → α) : f (dite P x y) = dite P (λ h, f (x h)) (λ h, f (y h)) := by { by_cases h : P; simp [h] } /-- A function applied to a `ite` is a `ite` of that function applied to each of the branches. -/ lemma apply_ite {α β : Sort*} (f : α → β) (P : Prop) [decidable P] (x y : α) : f (ite P x y) = ite P (f x) (f y) := apply_dite f P (λ _, x) (λ _, y) /-- A two-argument function applied to two `dite`s is a `dite` of that two-argument function applied to each of the branches. -/ lemma apply_dite2 {α β γ : Sort*} (f : α → β → γ) (P : Prop) [decidable P] (a : P → α) (b : ¬P → α) (c : P → β) (d : ¬P → β) : f (dite P a b) (dite P c d) = dite P (λ h, f (a h) (c h)) (λ h, f (b h) (d h)) := by { by_cases h : P; simp [h] } /-- A two-argument function applied to two `ite`s is a `ite` of that two-argument function applied to each of the branches. -/ lemma apply_ite2 {α β γ : Sort*} (f : α → β → γ) (P : Prop) [decidable P] (a b : α) (c d : β) : f (ite P a b) (ite P c d) = ite P (f a c) (f b d) := apply_dite2 f P (λ _, a) (λ _, b) (λ _, c) (λ _, d) /-- A 'dite' producing a `Pi` type `Π a, β a`, applied to a value `x : α` is a `dite` that applies either branch to `x`. -/ lemma dite_apply {α : Sort*} {β : α → Sort*} (P : Prop) [decidable P] (f : P → Π a, β a) (g : ¬ P → Π a, β a) (x : α) : (dite P f g) x = dite P (λ h, f h x) (λ h, g h x) := by { by_cases h : P; simp [h] } /-- A 'ite' producing a `Pi` type `Π a, β a`, applied to a value `x : α` is a `ite` that applies either branch to `x` -/ lemma ite_apply {α : Sort*} {β : α → Sort*} (P : Prop) [decidable P] (f g : Π a, β a) (x : α) : (ite P f g) x = ite P (f x) (g x) := dite_apply P (λ _, f) (λ _, g) x /-- Negation of the condition `P : Prop` in a `dite` is the same as swapping the branches. -/ @[simp] lemma dite_not {α : Sort*} (P : Prop) [decidable P] (x : ¬ P → α) (y : ¬¬ P → α) : dite (¬ P) x y = dite P (λ h, y (not_not_intro h)) x := by { by_cases h : P; simp [h] } /-- Negation of the condition `P : Prop` in a `ite` is the same as swapping the branches. -/ @[simp] lemma ite_not {α : Sort*} (P : Prop) [decidable P] (x y : α) : ite (¬ P) x y = ite P y x := dite_not P (λ _, x) (λ _, y) lemma ite_and {α} {p q : Prop} [decidable p] [decidable q] {x y : α} : ite (p ∧ q) x y = ite p (ite q x y) y := by { by_cases hp : p; by_cases hq : q; simp [hp, hq] } end ite
12a1dba013476a6f3b2ab758fcf5d0ca47abbb9e
b66feea4001b6bd591938498243cbb311bef715f
/test.lean
94ac7ebe706341749cb16354415abdf6de83e76a
[ "CC-BY-4.0" ]
permissive
vutuananh1210/formalabstracts
e19c27eded6272515db2293f5670d93c3f6cb2ab
58400178f7b1996afc0d34a141ebaf1db6744eec
refs/heads/master
1,584,645,359,178
1,529,492,486,000
1,529,492,486,000
136,415,832
0
0
null
1,528,341,384,000
1,528,341,384,000
null
UTF-8
Lean
false
false
580
lean
import analysis.real data.set.finite analysis.topology.topological_structures data.nat.prime structure homeo (α β : Type* )[topological_space α ][topological_space β ] extends α ≃ β := (to_fun_cont: continuous to_fun) (inv_fun_cont: continuous inv_fun ) variable (n:ℕ ) #check fin n #check (by apply_instance: topological_space (fin n → ℝ )) class manifold (α :Type*)(n:ℕ ) extends topological_space α, t2_space α := (locally_ℝn: ∀ x :α , ∃ U, is_open U ∧ x ∈ U ∧ nonempty(homeo U (fin n → ℝ )) ) #print nat.prime
8fa9b2dbddd3e73cead2efadd55dc779d4d123a1
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/order/category/HeytAlg.lean
5e3c7af5b84b4090a150b7ad78a9afb5c2a66569
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
1,789
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import order.category.BoundedDistribLattice import order.heyting.hom /-! # The category of Heyting algebras This file defines `HeytAlg`, the category of Heyting algebras. -/ universes u open category_theory opposite order /-- The category of Heyting algebras. -/ def HeytAlg := bundled heyting_algebra namespace HeytAlg instance : has_coe_to_sort HeytAlg Type* := bundled.has_coe_to_sort instance (X : HeytAlg) : heyting_algebra X := X.str /-- Construct a bundled `HeytAlg` from a `heyting_algebra`. -/ def of (α : Type*) [heyting_algebra α] : HeytAlg := bundled.of α @[simp] lemma coe_of (α : Type*) [heyting_algebra α] : ↥(of α) = α := rfl instance : inhabited HeytAlg := ⟨of punit⟩ instance bundled_hom : bundled_hom heyting_hom := { to_fun := λ α β [heyting_algebra α] [heyting_algebra β], by exactI (coe_fn : heyting_hom α β → α → β), id := heyting_hom.id, comp := @heyting_hom.comp, hom_ext := λ α β [heyting_algebra α] [heyting_algebra β], by exactI fun_like.coe_injective } attribute [derive [large_category, concrete_category]] HeytAlg @[simps] instance has_forget_to_Lattice : has_forget₂ HeytAlg BoundedDistribLattice := { forget₂ := { obj := λ X, BoundedDistribLattice.of X, map := λ X Y f, (f : bounded_lattice_hom X Y) } } /-- Constructs an isomorphism of Heyting algebras from an order isomorphism between them. -/ @[simps] def iso.mk {α β : HeytAlg.{u}} (e : α ≃o β) : α ≅ β := { hom := e, inv := e.symm, hom_inv_id' := by { ext, exact e.symm_apply_apply _ }, inv_hom_id' := by { ext, exact e.apply_symm_apply _ } } end HeytAlg
a8eb9339ac4ed722fa3bd062ee8802d480199bef
618003631150032a5676f229d13a079ac875ff77
/test/push_neg.lean
a0a36ae2aede160d3be94558ef418863e862ebb4
[ "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
2,070
lean
import tactic.push_neg import init_.data.int.order import data.nat.basic example (h : ∃ p: ℕ, ¬ ∀ n : ℕ, n > p) (h' : ∃ p: ℕ, ¬ ∃ n : ℕ, n < p) : ¬ ∀ n : ℕ, n = 0 := begin push_neg at *, guard_target_strict ∃ (n : ℕ), n ≠ 0, guard_hyp_strict h := ∃ (p n : ℕ), n ≤ p, guard_hyp_strict h' := ∃ (p : ℕ), ∀ (n : ℕ), p ≤ n, use 1, end -- In the next example, ℤ should be ℝ in maths, but I don't want to import real numbers -- for testing only local notation `|` x `|` := abs x example (a : ℕ → ℤ) (l : ℤ) (h : ¬ ∀ ε > 0, ∃ N, ∀ n ≥ N, | a n - l | < ε) : true := begin push_neg at h, guard_hyp_strict h := ∃ (ε : ℤ), ε > 0 ∧ ∀ (N : ℕ), ∃ (n : ℕ), n ≥ N ∧ ε ≤ |a n - l|, trivial end example (f : ℤ → ℤ) (x₀ y₀) (h : ¬ ∀ ε > 0, ∃ δ > 0, ∀ x, |x - x₀| ≤ δ → |f x - y₀| ≤ ε) : true := begin push_neg at h, guard_hyp_strict h := ∃ (ε : ℤ), ε > 0 ∧ ∀ δ > 0, (∃ (x : ℤ), |x - x₀| ≤ δ ∧ ε < |f x - y₀| ), trivial end example (n) : n*n ≠ 1 → n ≠ 1 := begin contrapose, rw [not_not, not_not], intro h, rw [h, one_mul] end example (n) : n*n ≠ 1 → n ≠ 1 := begin contrapose!, intro h, rw [h, one_mul] end example (n) (h : n*n ≠ 1) : n ≠ 1 := begin contrapose h, rw not_not at *, rw [h, one_mul] end example (n) (h : n*n ≠ 1) : n ≠ 1 := begin contrapose! h, rw [h, one_mul] end example (n) (h : n*n ≠ 1) : n ≠ 1 := begin contrapose! h with newh, rw [newh, one_mul] end example : 0 = 0 := begin success_if_fail_with_msg { contrapose } "The goal is not an implication, and you didn't specify an assumption", refl end -- Remember that ∀ is the same as Π which is a generalization of → so we need to make sure -- `contrapose` fails with a helpful error message in the next example. example : ∀ x : ℕ, x = x := begin success_if_fail_with_msg { contrapose } "contrapose only applies to nondependent arrows between props", intro, refl end
897099c73835b30e32a25eed4def314cec0a0cc6
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/geom_sum.lean
d129841e1be0e4ac23cdfa1616357c485e0a991c
[ "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
21,695
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Neil Strickland -/ import algebra.big_operators.order import algebra.big_operators.ring import algebra.big_operators.intervals import tactic.abel import data.nat.parity /-! # Partial sums of geometric series This file determines the values of the geometric series $\sum_{i=0}^{n-1} x^i$ and $\sum_{i=0}^{n-1} x^i y^{n-1-i}$ and variants thereof. We also provide some bounds on the "geometric" sum of `a/b^i` where `a b : ℕ`. ## Main statements * `geom_sum_Ico` proves that $\sum_{i=m}^{n-1} x^i=\frac{x^n-x^m}{x-1}$ in a division ring. * `geom_sum₂_Ico` proves that $\sum_{i=m}^{n-1} x^i=\frac{x^n-y^{n-m}x^m}{x-y}$ in a field. Several variants are recorded, generalising in particular to the case of a noncommutative ring in which `x` and `y` commute. Even versions not using division or subtraction, valid in each semiring, are recorded. -/ universe u variable {α : Type u} open finset mul_opposite open_locale big_operators section semiring variable [semiring α] lemma geom_sum_succ {x : α} {n : ℕ} : ∑ i in range (n + 1), x ^ i = x * ∑ i in range n, x ^ i + 1 := by simp only [mul_sum, ←pow_succ, sum_range_succ', pow_zero] lemma geom_sum_succ' {x : α} {n : ℕ} : ∑ i in range (n + 1), x ^ i = x ^ n + ∑ i in range n, x ^ i := (sum_range_succ _ _).trans (add_comm _ _) theorem geom_sum_zero (x : α) : ∑ i in range 0, x ^ i = 0 := rfl theorem geom_sum_one (x : α) : ∑ i in range 1, x ^ i = 1 := by simp [geom_sum_succ'] @[simp] lemma geom_sum_two {x : α} : ∑ i in range 2, x ^ i = x + 1 := by simp [geom_sum_succ'] @[simp] lemma zero_geom_sum : ∀ {n}, ∑ i in range n, (0 : α) ^ i = if n = 0 then 0 else 1 | 0 := by simp | 1 := by simp | (n+2) := by { rw geom_sum_succ', simp [zero_geom_sum] } lemma one_geom_sum (n : ℕ) : ∑ i in range n, (1 : α) ^ i = n := by simp @[simp] lemma op_geom_sum (x : α) (n : ℕ) : op (∑ i in range n, x ^ i) = ∑ i in range n, (op x) ^ i := by simp @[simp] lemma op_geom_sum₂ (x y : α) (n : ℕ) : op (∑ i in range n, x ^ i * (y ^ (n - 1 - i))) = ∑ i in range n, (op y) ^ i * ((op x) ^ (n - 1 - i)) := begin simp only [op_sum, op_mul, op_pow], rw ← sum_range_reflect, refine sum_congr rfl (λ j j_in, _), rw [mem_range, nat.lt_iff_add_one_le] at j_in, congr, apply tsub_tsub_cancel_of_le, exact le_tsub_of_add_le_right j_in end theorem geom_sum₂_with_one (x : α) (n : ℕ) : ∑ i in range n, x ^ i * (1 ^ (n - 1 - i)) = ∑ i in range n, x ^ i := sum_congr rfl (λ i _, by { rw [one_pow, mul_one] }) /-- $x^n-y^n = (x-y) \sum x^ky^{n-1-k}$ reformulated without `-` signs. -/ protected theorem commute.geom_sum₂_mul_add {x y : α} (h : commute x y) (n : ℕ) : (∑ i in range n, (x + y) ^ i * (y ^ (n - 1 - i))) * x + y ^ n = (x + y) ^ n := begin let f := λ (m i : ℕ), (x + y) ^ i * y ^ (m - 1 - i), change (∑ i in range n, (f n) i) * x + y ^ n = (x + y) ^ n, induction n with n ih, { rw [range_zero, sum_empty, zero_mul, zero_add, pow_zero, pow_zero] }, { have f_last : f (n + 1) n = (x + y) ^ n := by { dsimp [f], rw [← tsub_add_eq_tsub_tsub, nat.add_comm, tsub_self, pow_zero, mul_one] }, have f_succ : ∀ i, i ∈ range n → f (n + 1) i = y * f n i := λ i hi, by { dsimp [f], have : commute y ((x + y) ^ i) := (h.symm.add_right (commute.refl y)).pow_right i, rw [← mul_assoc, this.eq, mul_assoc, ← pow_succ y (n - 1 - i)], congr' 2, rw [add_tsub_cancel_right, ← tsub_add_eq_tsub_tsub, add_comm 1 i], have : i + 1 + (n - (i + 1)) = n := add_tsub_cancel_of_le (mem_range.mp hi), rw [add_comm (i + 1)] at this, rw [← this, add_tsub_cancel_right, add_comm i 1, ← add_assoc, add_tsub_cancel_right] }, rw [pow_succ (x + y), add_mul, sum_range_succ_comm, add_mul, f_last, add_assoc], rw (((commute.refl x).add_right h).pow_right n).eq, congr' 1, rw [sum_congr rfl f_succ, ← mul_sum, pow_succ y, mul_assoc, ← mul_add y, ih] } end end semiring @[simp] lemma neg_one_geom_sum [ring α] {n : ℕ} : ∑ i in range n, (-1 : α) ^ i = if even n then 0 else 1 := begin induction n with k hk, { simp }, { simp only [geom_sum_succ', nat.even_add_one, hk], split_ifs, { rw [h.neg_one_pow, add_zero] }, { rw [(nat.odd_iff_not_even.2 h).neg_one_pow, neg_add_self] } } end theorem geom_sum₂_self {α : Type*} [comm_ring α] (x : α) (n : ℕ) : ∑ i in range n, x ^ i * (x ^ (n - 1 - i)) = n * x ^ (n-1) := calc ∑ i in finset.range n, x ^ i * x ^ (n - 1 - i) = ∑ i in finset.range n, x ^ (i + (n - 1 - i)) : by simp_rw [← pow_add] ... = ∑ i in finset.range n, x ^ (n - 1) : finset.sum_congr rfl (λ i hi, congr_arg _ $ add_tsub_cancel_of_le $ nat.le_pred_of_lt $ finset.mem_range.1 hi) ... = (finset.range n).card • (x ^ (n - 1)) : finset.sum_const _ ... = n * x ^ (n - 1) : by rw [finset.card_range, nsmul_eq_mul] /-- $x^n-y^n = (x-y) \sum x^ky^{n-1-k}$ reformulated without `-` signs. -/ theorem geom_sum₂_mul_add [comm_semiring α] (x y : α) (n : ℕ) : (∑ i in range n, (x + y) ^ i * (y ^ (n - 1 - i))) * x + y ^ n = (x + y) ^ n := (commute.all x y).geom_sum₂_mul_add n theorem geom_sum_mul_add [semiring α] (x : α) (n : ℕ) : (∑ i in range n, (x + 1) ^ i) * x + 1 = (x + 1) ^ n := begin have := (commute.one_right x).geom_sum₂_mul_add n, rw [one_pow, geom_sum₂_with_one] at this, exact this end protected theorem commute.geom_sum₂_mul [ring α] {x y : α} (h : commute x y) (n : ℕ) : (∑ i in range n, x ^ i * (y ^ (n - 1 - i))) * (x - y) = x ^ n - y ^ n := begin have := (h.sub_left (commute.refl y)).geom_sum₂_mul_add n, rw [sub_add_cancel] at this, rw [← this, add_sub_cancel] end lemma commute.mul_neg_geom_sum₂ [ring α] {x y : α} (h : commute x y) (n : ℕ) : (y - x) * (∑ i in range n, x ^ i * (y ^ (n - 1 - i))) = y ^ n - x ^ n := begin apply op_injective, simp only [op_mul, op_sub, op_geom_sum₂, op_pow], exact (commute.op h.symm).geom_sum₂_mul n end lemma commute.mul_geom_sum₂ [ring α] {x y : α} (h : commute x y) (n : ℕ) : (x - y) * (∑ i in range n, x ^ i * (y ^ (n - 1 - i))) = x ^ n - y ^ n := by rw [← neg_sub (y ^ n), ← h.mul_neg_geom_sum₂, ← neg_mul, neg_sub] theorem geom_sum₂_mul [comm_ring α] (x y : α) (n : ℕ) : (∑ i in range n, x ^ i * (y ^ (n - 1 - i))) * (x - y) = x ^ n - y ^ n := (commute.all x y).geom_sum₂_mul n theorem geom_sum_mul [ring α] (x : α) (n : ℕ) : (∑ i in range n, x ^ i) * (x - 1) = x ^ n - 1 := begin have := (commute.one_right x).geom_sum₂_mul n, rw [one_pow, geom_sum₂_with_one] at this, exact this end lemma mul_geom_sum [ring α] (x : α) (n : ℕ) : (x - 1) * (∑ i in range n, x ^ i) = x ^ n - 1 := op_injective $ by simpa using geom_sum_mul (op x) n theorem geom_sum_mul_neg [ring α] (x : α) (n : ℕ) : (∑ i in range n, x ^ i) * (1 - x) = 1 - x ^ n := begin have := congr_arg has_neg.neg (geom_sum_mul x n), rw [neg_sub, ← mul_neg, neg_sub] at this, exact this end lemma mul_neg_geom_sum [ring α] (x : α) (n : ℕ) : (1 - x) * (∑ i in range n, x ^ i) = 1 - x ^ n := op_injective $ by simpa using geom_sum_mul_neg (op x) n protected lemma commute.geom_sum₂_comm {α : Type u} [semiring α] {x y : α} (n : ℕ) (h : commute x y) : ∑ i in range n, x ^ i * y ^ (n - 1 - i) = ∑ i in range n, y ^ i * x ^ (n - 1 - i) := begin cases n, { simp }, simp only [nat.succ_eq_add_one, nat.add_sub_cancel], rw ← finset.sum_flip, refine finset.sum_congr rfl (λ i hi, _), simpa [nat.sub_sub_self (nat.succ_le_succ_iff.mp (finset.mem_range.mp hi))] using h.pow_pow _ _ end lemma geom_sum₂_comm {α : Type u} [comm_semiring α] (x y : α) (n : ℕ) : ∑ i in range n, x ^ i * y ^ (n - 1 - i) = ∑ i in range n, y ^ i * x ^ (n - 1 - i) := (commute.all x y).geom_sum₂_comm n protected theorem commute.geom_sum₂ [division_ring α] {x y : α} (h' : commute x y) (h : x ≠ y) (n : ℕ) : (∑ i in range n, x ^ i * (y ^ (n - 1 - i))) = (x ^ n - y ^ n) / (x - y) := have x - y ≠ 0, by simp [*, -sub_eq_add_neg, sub_eq_iff_eq_add] at *, by rw [← h'.geom_sum₂_mul, mul_div_cancel _ this] theorem geom₂_sum [field α] {x y : α} (h : x ≠ y) (n : ℕ) : (∑ i in range n, x ^ i * (y ^ (n - 1 - i))) = (x ^ n - y ^ n) / (x - y) := (commute.all x y).geom_sum₂ h n theorem geom_sum_eq [division_ring α] {x : α} (h : x ≠ 1) (n : ℕ) : (∑ i in range n, x ^ i) = (x ^ n - 1) / (x - 1) := have x - 1 ≠ 0, by simp [*, -sub_eq_add_neg, sub_eq_iff_eq_add] at *, by rw [← geom_sum_mul, mul_div_cancel _ this] protected theorem commute.mul_geom_sum₂_Ico [ring α] {x y : α} (h : commute x y) {m n : ℕ} (hmn : m ≤ n) : (x - y) * (∑ i in finset.Ico m n, x ^ i * y ^ (n - 1 - i)) = x ^ n - x ^ m * y ^ (n - m) := begin rw [sum_Ico_eq_sub _ hmn], have : ∑ k in range m, x ^ k * y ^ (n - 1 - k) = ∑ k in range m, x ^ k * (y ^ (n - m) * y ^ (m - 1 - k)), { refine sum_congr rfl (λ j j_in, _), rw ← pow_add, congr, rw [mem_range, nat.lt_iff_add_one_le, add_comm] at j_in, have h' : n - m + (m - (1 + j)) = n - (1 + j) := tsub_add_tsub_cancel hmn j_in, rw [← tsub_add_eq_tsub_tsub m, h', ← tsub_add_eq_tsub_tsub] }, rw this, simp_rw pow_mul_comm y (n-m) _, simp_rw ← mul_assoc, rw [← sum_mul, mul_sub, h.mul_geom_sum₂, ← mul_assoc, h.mul_geom_sum₂, sub_mul, ← pow_add, add_tsub_cancel_of_le hmn, sub_sub_sub_cancel_right (x ^ n) (x ^ m * y ^ (n - m)) (y ^ n)], end protected theorem commute.geom_sum₂_succ_eq {α : Type u} [ring α] {x y : α} (h : commute x y) {n : ℕ} : ∑ i in range (n + 1), x ^ i * (y ^ (n - i)) = x ^ n + y * (∑ i in range n, x ^ i * (y ^ (n - 1 - i))) := begin simp_rw [mul_sum, sum_range_succ_comm, tsub_self, pow_zero, mul_one, add_right_inj, ←mul_assoc, (h.symm.pow_right _).eq, mul_assoc, ←pow_succ], refine sum_congr rfl (λ i hi, _), suffices : n - 1 - i + 1 = n - i, { rw this }, cases n, { exact absurd (list.mem_range.mp hi) i.not_lt_zero }, { rw [tsub_add_eq_add_tsub (nat.le_pred_of_lt (list.mem_range.mp hi)), tsub_add_cancel_of_le (nat.succ_le_iff.mpr n.succ_pos)] }, end theorem geom_sum₂_succ_eq {α : Type u} [comm_ring α] (x y : α) {n : ℕ} : ∑ i in range (n + 1), x ^ i * (y ^ (n - i)) = x ^ n + y * (∑ i in range n, x ^ i * (y ^ (n - 1 - i))) := (commute.all x y).geom_sum₂_succ_eq theorem mul_geom_sum₂_Ico [comm_ring α] (x y : α) {m n : ℕ} (hmn : m ≤ n) : (x - y) * (∑ i in finset.Ico m n, x ^ i * y ^ (n - 1 - i)) = x ^ n - x ^ m * y ^ (n - m) := (commute.all x y).mul_geom_sum₂_Ico hmn protected theorem commute.geom_sum₂_Ico_mul [ring α] {x y : α} (h : commute x y) {m n : ℕ} (hmn : m ≤ n) : (∑ i in finset.Ico m n, x ^ i * y ^ (n - 1 - i)) * (x - y) = x ^ n - y ^ (n - m) * x ^ m := begin apply op_injective, simp only [op_sub, op_mul, op_pow, op_sum], have : ∑ k in Ico m n, op y ^ (n - 1 - k) * op x ^ k = ∑ k in Ico m n, op x ^ k * op y ^ (n - 1 - k), { refine sum_congr rfl (λ k k_in, _), apply commute.pow_pow (commute.op h.symm) }, rw this, exact (commute.op h).mul_geom_sum₂_Ico hmn end theorem geom_sum_Ico_mul [ring α] (x : α) {m n : ℕ} (hmn : m ≤ n) : (∑ i in finset.Ico m n, x ^ i) * (x - 1) = x^n - x^m := by rw [sum_Ico_eq_sub _ hmn, sub_mul, geom_sum_mul, geom_sum_mul, sub_sub_sub_cancel_right] theorem geom_sum_Ico_mul_neg [ring α] (x : α) {m n : ℕ} (hmn : m ≤ n) : (∑ i in finset.Ico m n, x ^ i) * (1 - x) = x^m - x^n := by rw [sum_Ico_eq_sub _ hmn, sub_mul, geom_sum_mul_neg, geom_sum_mul_neg, sub_sub_sub_cancel_left] protected theorem commute.geom_sum₂_Ico [division_ring α] {x y : α} (h : commute x y) (hxy : x ≠ y) {m n : ℕ} (hmn : m ≤ n) : ∑ i in finset.Ico m n, x ^ i * y ^ (n - 1 - i) = (x ^ n - y ^ (n - m) * x ^ m ) / (x - y) := have x - y ≠ 0, by simp [*, -sub_eq_add_neg, sub_eq_iff_eq_add] at *, by rw [← h.geom_sum₂_Ico_mul hmn, mul_div_cancel _ this] theorem geom_sum₂_Ico [field α] {x y : α} (hxy : x ≠ y) {m n : ℕ} (hmn : m ≤ n) : ∑ i in finset.Ico m n, x ^ i * y ^ (n - 1 - i) = (x ^ n - y ^ (n - m) * x ^ m ) / (x - y) := (commute.all x y).geom_sum₂_Ico hxy hmn theorem geom_sum_Ico [division_ring α] {x : α} (hx : x ≠ 1) {m n : ℕ} (hmn : m ≤ n) : ∑ i in finset.Ico m n, x ^ i = (x ^ n - x ^ m) / (x - 1) := by simp only [sum_Ico_eq_sub _ hmn, geom_sum_eq hx, div_sub_div_same, sub_sub_sub_cancel_right] theorem geom_sum_Ico' [division_ring α] {x : α} (hx : x ≠ 1) {m n : ℕ} (hmn : m ≤ n) : ∑ i in finset.Ico m n, x ^ i = (x ^ m - x ^ n) / (1 - x) := by { simp only [geom_sum_Ico hx hmn], convert neg_div_neg_eq (x^m - x^n) (1-x); abel } lemma geom_sum_Ico_le_of_lt_one [linear_ordered_field α] {x : α} (hx : 0 ≤ x) (h'x : x < 1) {m n : ℕ} : ∑ i in Ico m n, x ^ i ≤ x ^ m / (1 - x) := begin rcases le_or_lt m n with hmn | hmn, { rw geom_sum_Ico' h'x.ne hmn, apply div_le_div (pow_nonneg hx _) _ (sub_pos.2 h'x) le_rfl, simpa using pow_nonneg hx _ }, { rw [Ico_eq_empty, sum_empty], { apply div_nonneg (pow_nonneg hx _), simpa using h'x.le }, { simpa using hmn.le } }, end lemma geom_sum_inv [division_ring α] {x : α} (hx1 : x ≠ 1) (hx0 : x ≠ 0) (n : ℕ) : (∑ i in range n, x⁻¹ ^ i) = (x - 1)⁻¹ * (x - x⁻¹ ^ n * x) := have h₁ : x⁻¹ ≠ 1, by rwa [inv_eq_one_div, ne.def, div_eq_iff_mul_eq hx0, one_mul], have h₂ : x⁻¹ - 1 ≠ 0, from mt sub_eq_zero.1 h₁, have h₃ : x - 1 ≠ 0, from mt sub_eq_zero.1 hx1, have h₄ : x * (x ^ n)⁻¹ = (x ^ n)⁻¹ * x := nat.rec_on n (by simp) (λ n h, by rw [pow_succ, mul_inv_rev, ←mul_assoc, h, mul_assoc, mul_inv_cancel hx0, mul_assoc, inv_mul_cancel hx0]), begin rw [geom_sum_eq h₁, div_eq_iff_mul_eq h₂, ← mul_right_inj' h₃, ← mul_assoc, ← mul_assoc, mul_inv_cancel h₃], simp [mul_add, add_mul, mul_inv_cancel hx0, mul_assoc, h₄, sub_eq_add_neg, add_comm, add_left_comm], end variables {β : Type*} theorem ring_hom.map_geom_sum [semiring α] [semiring β] (x : α) (n : ℕ) (f : α →+* β) : f (∑ i in range n, x ^ i) = ∑ i in range n, (f x) ^ i := by simp [f.map_sum] theorem ring_hom.map_geom_sum₂ [semiring α] [semiring β] (x y : α) (n : ℕ) (f : α →+* β) : f (∑ i in range n, x ^ i * (y ^ (n - 1 - i))) = ∑ i in range n, (f x) ^ i * ((f y) ^ (n - 1 - i)) := by simp [f.map_sum] /-! ### Geometric sum with `ℕ`-division -/ lemma nat.pred_mul_geom_sum_le (a b n : ℕ) : (b - 1) * ∑ i in range n.succ, a/b^i ≤ a * b - a/b^n := calc (b - 1) * (∑ i in range n.succ, a/b^i) = ∑ i in range n, a/b^(i + 1) * b + a * b - (∑ i in range n, a/b^i + a/b^n) : by rw [tsub_mul, mul_comm, sum_mul, one_mul, sum_range_succ', sum_range_succ, pow_zero, nat.div_one] ... ≤ ∑ i in range n, a/b^i + a * b - (∑ i in range n, a/b^i + a/b^n) : begin refine tsub_le_tsub_right (add_le_add_right (sum_le_sum $ λ i _, _) _) _, rw [pow_succ', ←nat.div_div_eq_div_mul], exact nat.div_mul_le_self _ _, end ... = a * b - a/b^n : add_tsub_add_eq_tsub_left _ _ _ lemma nat.geom_sum_le {b : ℕ} (hb : 2 ≤ b) (a n : ℕ) : ∑ i in range n, a/b^i ≤ a * b/(b - 1) := begin refine (nat.le_div_iff_mul_le $ tsub_pos_of_lt hb).2 _, cases n, { rw [sum_range_zero, zero_mul], exact nat.zero_le _ }, rw mul_comm, exact (nat.pred_mul_geom_sum_le a b n).trans tsub_le_self, end lemma nat.geom_sum_Ico_le {b : ℕ} (hb : 2 ≤ b) (a n : ℕ) : ∑ i in Ico 1 n, a/b^i ≤ a/(b - 1) := begin cases n, { rw [Ico_eq_empty_of_le (zero_le_one' ℕ), sum_empty], exact nat.zero_le _ }, rw ←add_le_add_iff_left a, calc a + ∑ (i : ℕ) in Ico 1 n.succ, a/b^i = a/b^0 + ∑ (i : ℕ) in Ico 1 n.succ, a/b^i : by rw [pow_zero, nat.div_one] ... = ∑ i in range n.succ, a/b^i : begin rw [range_eq_Ico, ←nat.Ico_insert_succ_left (nat.succ_pos _), sum_insert], exact λ h, zero_lt_one.not_le (mem_Ico.1 h).1, end ... ≤ a * b/(b - 1) : nat.geom_sum_le hb a _ ... = (a * 1 + a * (b - 1))/(b - 1) : by rw [←mul_add, add_tsub_cancel_of_le (one_le_two.trans hb)] ... = a + a/(b - 1) : by rw [mul_one, nat.add_mul_div_right _ _ (tsub_pos_of_lt hb), add_comm] end section order variables {n : ℕ} {x : α} lemma geom_sum_pos [strict_ordered_semiring α] (hx : 0 ≤ x) (hn : n ≠ 0) : 0 < ∑ i in range n, x ^ i := sum_pos' (λ k hk, pow_nonneg hx _) ⟨0, mem_range.2 hn.bot_lt, by simp⟩ lemma geom_sum_pos_and_lt_one [strict_ordered_ring α] (hx : x < 0) (hx' : 0 < x + 1) (hn : 1 < n) : 0 < ∑ i in range n, x ^ i ∧ ∑ i in range n, x ^ i < 1 := begin refine nat.le_induction _ _ n (show 2 ≤ n, from hn), { rw geom_sum_two, exact ⟨hx', (add_lt_iff_neg_right _).2 hx⟩ }, clear hn n, intros n hn ihn, rw [geom_sum_succ, add_lt_iff_neg_right, ← neg_lt_iff_pos_add', neg_mul_eq_neg_mul], exact ⟨mul_lt_one_of_nonneg_of_lt_one_left (neg_nonneg.2 hx.le) (neg_lt_iff_pos_add'.2 hx') ihn.2.le, mul_neg_of_neg_of_pos hx ihn.1⟩ end lemma geom_sum_alternating_of_le_neg_one [strict_ordered_ring α] (hx : x + 1 ≤ 0) (n : ℕ) : if even n then ∑ i in range n, x ^ i ≤ 0 else 1 ≤ ∑ i in range n, x ^ i := begin have hx0 : x ≤ 0 := (le_add_of_nonneg_right zero_le_one).trans hx, induction n with n ih, { simp only [even_zero, geom_sum_zero, le_refl] }, simp only [nat.even_add_one, geom_sum_succ], split_ifs at ih, { rw [if_neg (not_not_intro h), le_add_iff_nonneg_left], exact mul_nonneg_of_nonpos_of_nonpos hx0 ih }, { rw if_pos h, refine (add_le_add_right _ _).trans hx, simpa only [mul_one] using mul_le_mul_of_nonpos_left ih hx0 } end lemma geom_sum_alternating_of_lt_neg_one [strict_ordered_ring α] (hx : x + 1 < 0) (hn : 1 < n) : if even n then ∑ i in range n, x ^ i < 0 else 1 < ∑ i in range n, x ^ i := begin have hx0 : x < 0, from ((le_add_iff_nonneg_right _).2 zero_le_one).trans_lt hx, refine nat.le_induction _ _ n (show 2 ≤ n, from hn), { simp only [geom_sum_two, hx, true_or, even_bit0, if_true_left_eq_or] }, clear hn n, intros n hn ihn, simp only [nat.even_add_one, geom_sum_succ], by_cases hn' : even n, { rw [if_pos hn'] at ihn, rw [if_neg, lt_add_iff_pos_left], exact mul_pos_of_neg_of_neg hx0 ihn, exact not_not_intro hn', }, { rw [if_neg hn'] at ihn, rw [if_pos], swap, { exact hn' }, have := add_lt_add_right (mul_lt_mul_of_neg_left ihn hx0) 1, rw mul_one at this, exact this.trans hx } end lemma geom_sum_pos' [linear_ordered_ring α] (hx : 0 < x + 1) (hn : n ≠ 0) : 0 < ∑ i in range n, x ^ i := begin obtain _ | _ | n := n, { cases hn rfl }, { simp }, obtain hx' | hx' := lt_or_le x 0, { exact (geom_sum_pos_and_lt_one hx' hx n.one_lt_succ_succ).1 }, { exact geom_sum_pos hx' (by simp only [nat.succ_ne_zero, ne.def, not_false_iff]) } end lemma odd.geom_sum_pos [linear_ordered_ring α] (h : odd n) : 0 < ∑ i in range n, x ^ i := begin rcases n with (_ | _ | k), { exact ((show ¬ odd 0, from dec_trivial) h).elim }, { simp only [geom_sum_one, zero_lt_one] }, rw nat.odd_iff_not_even at h, rcases lt_trichotomy (x + 1) 0 with hx | hx | hx, { have := geom_sum_alternating_of_lt_neg_one hx k.one_lt_succ_succ, simp only [h, if_false] at this, exact zero_lt_one.trans this }, { simp only [eq_neg_of_add_eq_zero_left hx, h, neg_one_geom_sum, if_false, zero_lt_one] }, { exact geom_sum_pos' hx k.succ.succ_ne_zero } end lemma geom_sum_pos_iff [linear_ordered_ring α] (hn : n ≠ 0) : 0 < ∑ i in range n, x ^ i ↔ odd n ∨ 0 < x + 1 := begin refine ⟨λ h, _, _⟩, { rw [or_iff_not_imp_left, ←not_le, ←nat.even_iff_not_odd], refine λ hn hx, h.not_le _, simpa [if_pos hn] using geom_sum_alternating_of_le_neg_one hx n }, { rintro (hn | hx'), { exact hn.geom_sum_pos }, { exact geom_sum_pos' hx' hn } } end lemma geom_sum_ne_zero [linear_ordered_ring α] (hx : x ≠ -1) (hn : n ≠ 0) : ∑ i in range n, x ^ i ≠ 0 := begin obtain _ | _ | n := n, { cases hn rfl }, { simp }, rw [ne.def, eq_neg_iff_add_eq_zero, ←ne.def] at hx, obtain h | h := hx.lt_or_lt, { have := geom_sum_alternating_of_lt_neg_one h n.one_lt_succ_succ, split_ifs at this, { exact this.ne }, { exact (zero_lt_one.trans this).ne' } }, { exact (geom_sum_pos' h n.succ.succ_ne_zero).ne' } end lemma geom_sum_eq_zero_iff_neg_one [linear_ordered_ring α] (hn : n ≠ 0) : ∑ i in range n, x ^ i = 0 ↔ x = -1 ∧ even n := begin refine ⟨λ h, _, λ ⟨h, hn⟩, by simp only [h, hn, neg_one_geom_sum, if_true]⟩, contrapose! h, obtain rfl | hx := eq_or_ne x (-1), { simp only [h rfl, neg_one_geom_sum, if_false, ne.def, not_false_iff, one_ne_zero] }, { exact geom_sum_ne_zero hx hn } end lemma geom_sum_neg_iff [linear_ordered_ring α] (hn : n ≠ 0) : ∑ i in range n, x ^ i < 0 ↔ even n ∧ x + 1 < 0 := by rw [← not_iff_not, not_lt, le_iff_lt_or_eq, eq_comm, or_congr (geom_sum_pos_iff hn) (geom_sum_eq_zero_iff_neg_one hn), nat.odd_iff_not_even, ← add_eq_zero_iff_eq_neg, not_and, not_lt, le_iff_lt_or_eq, eq_comm, ← imp_iff_not_or, or_comm, and_comm, decidable.and_or_imp, or_comm] end order
7159b0657b1f0081a9f56c581cea0b343d2ce6de
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/finsupp/lex.lean
09fdd2c4fc9ef030b0535de8e5f297d12540c24f
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
4,782
lean
/- Copyright (c) 2022 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import data.finsupp.order import data.dfinsupp.lex import data.finsupp.to_dfinsupp /-! # Lexicographic order on finitely supported functions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the lexicographic order on `finsupp`. -/ variables {α N : Type*} namespace finsupp section N_has_zero variables [has_zero N] /-- `finsupp.lex r s` is the lexicographic relation on `α →₀ N`, where `α` is ordered by `r`, and `N` is ordered by `s`. The type synonym `lex (α →₀ N)` has an order given by `finsupp.lex (<) (<)`. -/ protected def lex (r : α → α → Prop) (s : N → N → Prop) (x y : α →₀ N) : Prop := pi.lex r (λ _, s) x y lemma _root_.pi.lex_eq_finsupp_lex {r : α → α → Prop} {s : N → N → Prop} (a b : α →₀ N) : pi.lex r (λ _, s) (a : α → N) (b : α → N) = finsupp.lex r s a b := rfl lemma lex_def {r : α → α → Prop} {s : N → N → Prop} {a b : α →₀ N} : finsupp.lex r s a b ↔ ∃ j, (∀ d, r d j → a d = b d) ∧ s (a j) (b j) := iff.rfl lemma lex_eq_inv_image_dfinsupp_lex (r : α → α → Prop) (s : N → N → Prop) : finsupp.lex r s = inv_image (dfinsupp.lex r $ λ a, s) to_dfinsupp := rfl instance [has_lt α] [has_lt N] : has_lt (lex (α →₀ N)) := ⟨λ f g, finsupp.lex (<) (<) (of_lex f) (of_lex g)⟩ lemma lex_lt_of_lt_of_preorder [preorder N] (r) [is_strict_order α r] {x y : α →₀ N} (hlt : x < y) : ∃ i, (∀ j, r j i → x j ≤ y j ∧ y j ≤ x j) ∧ x i < y i := dfinsupp.lex_lt_of_lt_of_preorder r (id hlt : x.to_dfinsupp < y.to_dfinsupp) lemma lex_lt_of_lt [partial_order N] (r) [is_strict_order α r] {x y : α →₀ N} (hlt : x < y) : pi.lex r (λ i, (<)) x y := dfinsupp.lex_lt_of_lt r (id hlt : x.to_dfinsupp < y.to_dfinsupp) instance lex.is_strict_order [linear_order α] [partial_order N] : is_strict_order (lex (α →₀ N)) (<) := let i : is_strict_order (lex (α → N)) (<) := pi.lex.is_strict_order in { irrefl := to_lex.surjective.forall.2 $ λ a, @irrefl _ _ i.to_is_irrefl a, trans := to_lex.surjective.forall₃.2 $ λ a b c, @trans _ _ i.to_is_trans a b c } variables [linear_order α] /-- The partial order on `finsupp`s obtained by the lexicographic ordering. See `finsupp.lex.linear_order` for a proof that this partial order is in fact linear. -/ instance lex.partial_order [partial_order N] : partial_order (lex (α →₀ N)) := partial_order.lift (λ x, to_lex ⇑(of_lex x)) finsupp.coe_fn_injective--fun_like.coe_injective /-- The linear order on `finsupp`s obtained by the lexicographic ordering. -/ instance lex.linear_order [linear_order N] : linear_order (lex (α →₀ N)) := { ..lex.partial_order, ..linear_order.lift' (to_lex ∘ to_dfinsupp ∘ of_lex) finsupp_equiv_dfinsupp.injective } variable [partial_order N] lemma to_lex_monotone : monotone (@to_lex (α →₀ N)) := λ a b h, dfinsupp.to_lex_monotone (id h : ∀ i, of_lex (to_dfinsupp a) i ≤ of_lex (to_dfinsupp b) i) lemma lt_of_forall_lt_of_lt (a b : lex (α →₀ N)) (i : α) : (∀ j < i, of_lex a j = of_lex b j) → of_lex a i < of_lex b i → a < b := λ h1 h2, ⟨i, h1, h2⟩ end N_has_zero section covariants variables [linear_order α] [add_monoid N] [linear_order N] /-! We are about to sneak in a hypothesis that might appear to be too strong. We assume `covariant_class` with *strict* inequality `<` also when proving the one with the *weak* inequality `≤`. This is actually necessary: addition on `lex (α →₀ N)` may fail to be monotone, when it is "just" monotone on `N`. See `counterexamples.zero_divisors_in_add_monoid_algebras` for a counterexample. -/ section left variables [covariant_class N N (+) (<)] instance lex.covariant_class_lt_left : covariant_class (lex (α →₀ N)) (lex (α →₀ N)) (+) (<) := ⟨λ f g h ⟨a, lta, ha⟩, ⟨a, λ j ja, congr_arg ((+) _) (lta j ja), add_lt_add_left ha _⟩⟩ instance lex.covariant_class_le_left : covariant_class (lex (α →₀ N)) (lex (α →₀ N)) (+) (≤) := has_add.to_covariant_class_left _ end left section right variables [covariant_class N N (function.swap (+)) (<)] instance lex.covariant_class_lt_right : covariant_class (lex (α →₀ N)) (lex (α →₀ N)) (function.swap (+)) (<) := ⟨λ f g h ⟨a, lta, ha⟩, ⟨a, λ j ja, congr_arg (+ (of_lex f j)) (lta j ja), add_lt_add_right ha _⟩⟩ instance lex.covariant_class_le_right : covariant_class (lex (α →₀ N)) (lex (α →₀ N)) (function.swap (+)) (≤) := has_add.to_covariant_class_right _ end right end covariants end finsupp
c879cc7676841f26167c4aefeb75659d091cb56f
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/equiv/encodable/small.lean
0f645241bad783faba9a95a5c0af30768a934c04
[ "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
473
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import data.equiv.encodable.basic import logic.small /-! # All encodable types are small. That is, any encodable type is equivalent to a type in any universe. -/ universes w v @[priority 100] instance small_of_encodable (α : Type v) [encodable α] : small.{w} α := small_of_injective _ encodable.encode_injective
9abc499744f63ee66dba000d027976ff54117f80
491068d2ad28831e7dade8d6dff871c3e49d9431
/library/data/finset/equiv.lean
2de7a0bf6c5d51dd964a9a680d29667bd28f7aef
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,880
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ import data.finset.card open nat nat.finset decidable namespace finset variable {A : Type} protected definition to_nat (s : finset nat) : nat := nat.finset.Sum s (λ n, 2^n) open finset (to_nat) lemma to_nat_empty : to_nat ∅ = 0 := rfl lemma to_nat_insert {n : nat} {s : finset nat} : n ∉ s → to_nat (insert n s) = 2^n + to_nat s := assume h, Sum_insert_of_not_mem _ h protected definition of_nat (s : nat) : finset nat := { n ∈ upto (succ s) | odd (s div 2^n) } open finset (of_nat) private lemma of_nat_zero : of_nat 0 = ∅ := rfl private lemma odd_of_mem_of_nat {n : nat} {s : nat} : n ∈ of_nat s → odd (s div 2^n) := assume h, of_mem_sep h private lemma mem_of_nat_of_odd {n : nat} {s : nat} : odd (s div 2^n) → n ∈ of_nat s := assume h, have 2^n < succ s, from by_contradiction (suppose ¬(2^n < succ s), assert 2^n > s, from lt_of_succ_le (le_of_not_gt this), assert s div 2^n = 0, from div_eq_zero_of_lt this, by rewrite this at h; exact absurd h dec_trivial), have n < succ s, from calc n ≤ 2^n : le_pow_self dec_trivial n ... < succ s : this, have n ∈ upto (succ s), from mem_upto_of_lt this, mem_sep_of_mem this h private lemma succ_mem_of_nat (n : nat) (s : nat) : succ n ∈ of_nat s ↔ n ∈ of_nat (s div 2) := iff.intro (suppose succ n ∈ of_nat s, assert odd (s div 2^(succ n)), from odd_of_mem_of_nat this, have odd ((s div 2) div (2 ^ n)), by rewrite [pow_succ' at this, div_div_eq_div_mul, mul.comm]; assumption, show n ∈ of_nat (s div 2), from mem_of_nat_of_odd this) (suppose n ∈ of_nat (s div 2), assert odd ((s div 2) div (2 ^ n)), from odd_of_mem_of_nat this, assert odd (s div 2^(succ n)), by rewrite [pow_succ', mul.comm, -div_div_eq_div_mul]; assumption, show succ n ∈ of_nat s, from mem_of_nat_of_odd this) private lemma odd_of_zero_mem (s : nat) : 0 ∈ of_nat s ↔ odd s := begin unfold of_nat, rewrite [mem_sep_eq, pow_zero, div_one, mem_upto_eq], show 0 < succ s ∧ odd s ↔ odd s, from iff.intro (assume h, and.right h) (assume h, and.intro (zero_lt_succ s) h) end private lemma even_of_not_zero_mem (s : nat) : 0 ∉ of_nat s ↔ even s := have aux : 0 ∉ of_nat s ↔ ¬odd s, from not_iff_not_of_iff (odd_of_zero_mem s), iff.intro (suppose 0 ∉ of_nat s, even_of_not_odd (iff.mp aux this)) (suppose even s, iff.mpr aux (not_odd_of_even this)) private lemma even_to_nat (s : finset nat) : even (to_nat s) ↔ 0 ∉ s := finset.induction_on s dec_trivial (λ a s nains ih, begin rewrite [to_nat_insert nains], apply iff.intro, suppose even (2^a + to_nat s), by_cases (suppose e : even (2^a), by_cases (suppose even (to_nat s), assert 0 ∉ s, from iff.mp ih this, suppose 0 ∈ insert a s, or.elim (eq_or_mem_of_mem_insert this) (suppose 0 = a, begin rewrite [-this at e], exact absurd e not_even_one end) (by contradiction)) (suppose odd (to_nat s), absurd `even (2^a + to_nat s)` (odd_add_of_even_of_odd `even (2^a)` this))) (suppose o : odd (2^a), by_cases (suppose even (to_nat s), absurd `even (2^a + to_nat s)` (odd_add_of_odd_of_even `odd (2^a)` this)) (suppose odd (to_nat s), suppose 0 ∈ insert a s, or.elim (eq_or_mem_of_mem_insert this) (suppose 0 = a, have even (to_nat s), from iff.mpr ih (by rewrite -this at nains; exact nains), absurd this `odd (to_nat s)`) (suppose 0 ∈ s, assert a ≠ 0, from suppose a = 0, by subst a; contradiction, begin cases a with a, contradiction, have odd (2*2^a), by rewrite [pow_succ' at o, mul.comm]; exact o, have even (2*2^a), from !even_two_mul, exact absurd `even (2*2^a)` `odd (2*2^a)` end))), suppose 0 ∉ insert a s, have a ≠ 0, from suppose a = 0, absurd (by rewrite this; apply mem_insert) `0 ∉ insert a s`, have 0 ∉ s, from suppose 0 ∈ s, absurd (mem_insert_of_mem _ this) `0 ∉ insert a s`, have even (to_nat s), from iff.mpr ih this, match a with | 0 := suppose a = 0, absurd this `a ≠ 0` | (succ a') := suppose a = succ a', have even (2^(succ a')), by rewrite [pow_succ', mul.comm]; apply even_two_mul, even_add_of_even_of_even this `even (to_nat s)` end rfl end) private lemma of_nat_eq_insert_zero {s : nat} : 0 ∉ of_nat s → of_nat (2^0 + s) = insert 0 (of_nat s) := assume h : 0 ∉ of_nat s, assert even s, from iff.mp (even_of_not_zero_mem s) h, have odd (s+1), from odd_succ_of_even this, assert zmem : 0 ∈ of_nat (s+1), from iff.mpr (odd_of_zero_mem (s+1)) this, obtain w (hw : s = 2*w), from exists_of_even `even s`, begin rewrite [pow_zero, add.comm, hw], show of_nat (2*w+1) = insert 0 (of_nat (2*w)), from finset.ext (λ n, match n with | 0 := iff.intro (λ h, !mem_insert) (λ h, by rewrite [hw at zmem]; exact zmem) | succ m := assert d₁ : 1 div 2 = 0, from dec_trivial, assert aux : _, from calc succ m ∈ of_nat (2 * w + 1) ↔ m ∈ of_nat ((2*w+1) div 2) : succ_mem_of_nat ... ↔ m ∈ of_nat w : by rewrite [add.comm, add_mul_div_self_left _ _ (dec_trivial : 2 > 0), d₁, zero_add] ... ↔ m ∈ of_nat (2*w div 2) : by rewrite [mul.comm, mul_div_cancel _ (dec_trivial : 2 > 0)] ... ↔ succ m ∈ of_nat (2*w) : succ_mem_of_nat, iff.intro (λ hl, finset.mem_insert_of_mem _ (iff.mp aux hl)) (λ hr, or.elim (eq_or_mem_of_mem_insert hr) (by contradiction) (iff.mpr aux)) end) end private lemma of_nat_eq_insert : ∀ {n s : nat}, n ∉ of_nat s → of_nat (2^n + s) = insert n (of_nat s) | 0 s h := of_nat_eq_insert_zero h | (succ n) s h := have n ∉ of_nat (s div 2), from iff.mp (not_iff_not_of_iff !succ_mem_of_nat) h, assert ih : of_nat (2^n + s div 2) = insert n (of_nat (s div 2)), from of_nat_eq_insert this, finset.ext (λ x, have gen : ∀ m, m ∈ of_nat (2^(succ n) + s) ↔ m ∈ insert (succ n) (of_nat s) | zero := have even (2^(succ n)), by rewrite [pow_succ', mul.comm]; apply even_two_mul, have aux₁ : odd (2^(succ n) + s) ↔ odd s, from iff.intro (suppose odd (2^(succ n) + s), by_contradiction (suppose ¬ odd s, have even s, from even_of_not_odd this, have even (2^(succ n) + s), from even_add_of_even_of_even `even (2^(succ n))` this, absurd `odd (2^(succ n) + s)` (not_odd_of_even this))) (suppose odd s, odd_add_of_even_of_odd `even (2^(succ n))` this), have aux₂ : odd s ↔ 0 ∈ insert (succ n) (of_nat s), from iff.intro (suppose odd s, finset.mem_insert_of_mem _ (iff.mpr !odd_of_zero_mem this)) (suppose 0 ∈ insert (succ n) (of_nat s), or.elim (eq_or_mem_of_mem_insert this) (by contradiction) (suppose 0 ∈ of_nat s, iff.mp !odd_of_zero_mem this)), calc 0 ∈ of_nat (2^(succ n) + s) ↔ odd (2^(succ n) + s) : odd_of_zero_mem ... ↔ odd s : aux₁ ... ↔ 0 ∈ insert (succ n) (of_nat s) : aux₂ | (succ m) := assert aux : m ∈ insert n (of_nat (s div 2)) ↔ succ m ∈ insert (succ n) (of_nat s), from iff.intro (assume hl, or.elim (eq_or_mem_of_mem_insert hl) (suppose m = n, by subst m; apply mem_insert) (suppose m ∈ of_nat (s div 2), finset.mem_insert_of_mem _ (iff.mpr !succ_mem_of_nat this))) (assume hr, or.elim (eq_or_mem_of_mem_insert hr) (suppose succ m = succ n, assert m = n, by injection this; assumption, by subst m; apply mem_insert) (suppose succ m ∈ of_nat s, finset.mem_insert_of_mem _ (iff.mp !succ_mem_of_nat this))), calc succ m ∈ of_nat (2^(succ n) + s) ↔ succ m ∈ of_nat (2^n * 2 + s) : by rewrite pow_succ' ... ↔ m ∈ of_nat ((2^n * 2 + s) div 2) : succ_mem_of_nat ... ↔ m ∈ of_nat (2^n + s div 2) : by rewrite [add.comm, add_mul_div_self (dec_trivial : 2 > 0), add.comm] ... ↔ m ∈ insert n (of_nat (s div 2)) : by rewrite ih ... ↔ succ m ∈ insert (succ n) (of_nat s) : aux, gen x) lemma of_nat_to_nat (s : finset nat) : of_nat (to_nat s) = s := finset.induction_on s rfl (λ a s nains ih, by rewrite [to_nat_insert nains, -ih at nains, of_nat_eq_insert nains, ih]) private definition predimage (s : finset nat) : finset nat := { n ∈ image pred s | succ n ∈ s } private lemma mem_image_pred_of_succ_mem {n : nat} {s : finset nat} : succ n ∈ s → n ∈ image pred s := assume h, assert pred (succ n) ∈ image pred s, from mem_image_of_mem _ h, begin rewrite [pred_succ at this], assumption end private lemma mem_predimage_of_succ_mem {n : nat} {s : finset nat} : succ n ∈ s → n ∈ predimage s := assume h, begin unfold predimage, rewrite [mem_sep_eq], exact and.intro (mem_image_pred_of_succ_mem h) h end private lemma succ_mem_of_mem_predimage {n : nat} {s : finset nat} : n ∈ predimage s → succ n ∈ s := begin unfold predimage, rewrite [mem_sep_eq], suppose n ∈ image pred s ∧ succ n ∈ s, and.right this end private lemma predimage_insert_zero (s : finset nat) : predimage (insert 0 s) = predimage s := finset.ext (λ n, begin unfold predimage, rewrite [*mem_sep_eq, image_insert, pred_zero], apply iff.intro, suppose n ∈ insert 0 (image pred s) ∧ succ n ∈ insert 0 s, have succ n ∈ s, from or.elim (eq_or_mem_of_mem_insert (and.right this)) (by contradiction) (λ h, h), and.intro (mem_image_pred_of_succ_mem this) this, suppose n ∈ image pred s ∧ succ n ∈ s, obtain h₁ h₂, from this, and.intro (mem_insert_of_mem 0 h₁) (mem_insert_of_mem 0 h₂) end) private lemma predimage_insert_succ (n : nat) (s : finset nat) : predimage (insert (succ n) s) = insert n (predimage s) := finset.ext (λ m, begin unfold predimage, rewrite [*mem_sep_eq, *image_insert, pred_succ, *mem_insert_eq, *mem_sep_eq], apply iff.intro, suppose (m = n ∨ m ∈ image pred s) ∧ (succ m = succ n ∨ succ m ∈ s), obtain h₁ h₂, from this, or.elim h₁ (suppose m = n, or.inl this) (suppose m ∈ image pred s, or.elim h₂ (suppose succ m = succ n, by injection this; left; assumption) (suppose succ m ∈ s, by right; split; repeat assumption)), suppose m = n ∨ m ∈ image pred s ∧ succ m ∈ s, or.elim this (suppose m = n, and.intro (or.inl this) (or.inl (by subst m))) (suppose m ∈ image pred s ∧ succ m ∈ s, obtain h₁ h₂, from this, and.intro (or.inr h₁) (or.inr h₂)) end) private lemma of_nat_div2 (s : nat) : of_nat (s div 2) = predimage (of_nat s) := finset.ext (λ n, iff.intro (suppose n ∈ of_nat (s div 2), have succ n ∈ of_nat s, from iff.mpr !succ_mem_of_nat this, mem_predimage_of_succ_mem this) (suppose n ∈ predimage (of_nat s), have succ n ∈ of_nat s, from succ_mem_of_mem_predimage this, iff.mp !succ_mem_of_nat this)) private lemma to_nat_predimage (s : finset nat) : to_nat (predimage s) = (to_nat s) div 2 := begin induction s with a s nains ih, reflexivity, cases a with a, { rewrite [predimage_insert_zero, ih, to_nat_insert nains, pow_zero], have 0 ∉ of_nat (to_nat s), by rewrite of_nat_to_nat; assumption, have even (to_nat s), from iff.mp !even_of_not_zero_mem this, obtain w (hw : to_nat s = 2*w), from exists_of_even this, begin rewrite hw, have d₁ : 1 div 2 = 0, from dec_trivial, show 2 * w div 2 = (1 + 2 * w) div 2, by rewrite [add_mul_div_self_left _ _ (dec_trivial : 2 > 0), mul.comm, mul_div_cancel _ (dec_trivial : 2 > 0), d₁, zero_add] end }, { have a ∉ predimage s, from suppose a ∈ predimage s, absurd (succ_mem_of_mem_predimage this) nains, rewrite [predimage_insert_succ, to_nat_insert nains, pow_succ', add.comm, add_mul_div_self (dec_trivial : 2 > 0), -ih, to_nat_insert this, add.comm] } end lemma to_nat_of_nat (s : nat) : to_nat (of_nat s) = s := nat.strong_induction_on s (λ n ih, by_cases (suppose n = 0, by rewrite this) (suppose n ≠ 0, have n div 2 < n, from div_lt_of_ne_zero this, have to_nat (of_nat (n div 2)) = n div 2, from ih _ this, have e₁ : to_nat (of_nat n) div 2 = n div 2, from calc to_nat (of_nat n) div 2 = to_nat (predimage (of_nat n)) : by rewrite to_nat_predimage ... = to_nat (of_nat (n div 2)) : by rewrite of_nat_div2 ... = n div 2 : this, have e₂ : even (to_nat (of_nat n)) ↔ even n, from calc even (to_nat (of_nat n)) ↔ 0 ∉ of_nat n : even_to_nat ... ↔ even n : even_of_not_zero_mem, eq_of_div2_of_even e₁ e₂)) open equiv definition finset_nat_equiv_nat : finset nat ≃ nat := mk to_nat of_nat of_nat_to_nat to_nat_of_nat end finset
d8a8ded43eece2c8724519c639772028f6722652
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/nat/dist.lean
256b6f41b482c153feb6e09f167982a5c3fa18e1
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
4,141
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Jeremy Avigad -/ import data.nat.order.basic /-! # Distance function on ℕ This file defines a simple distance function on naturals from truncated substraction. -/ namespace nat /-- Distance (absolute value of difference) between natural numbers. -/ def dist (n m : ℕ) := (n - m) + (m - n) theorem dist.def (n m : ℕ) : dist n m = (n - m) + (m - n) := rfl theorem dist_comm (n m : ℕ) : dist n m = dist m n := by simp [dist.def, add_comm] @[simp] theorem dist_self (n : ℕ) : dist n n = 0 := by simp [dist.def, tsub_self] theorem eq_of_dist_eq_zero {n m : ℕ} (h : dist n m = 0) : n = m := have n - m = 0, from nat.eq_zero_of_add_eq_zero_right h, have n ≤ m, from tsub_eq_zero_iff_le.mp this, have m - n = 0, from nat.eq_zero_of_add_eq_zero_left h, have m ≤ n, from tsub_eq_zero_iff_le.mp this, le_antisymm ‹n ≤ m› ‹m ≤ n› theorem dist_eq_zero {n m : ℕ} (h : n = m) : dist n m = 0 := begin rw [h, dist_self] end theorem dist_eq_sub_of_le {n m : ℕ} (h : n ≤ m) : dist n m = m - n := begin rw [dist.def, tsub_eq_zero_iff_le.mpr h, zero_add] end theorem dist_eq_sub_of_le_right {n m : ℕ} (h : m ≤ n) : dist n m = n - m := begin rw [dist_comm], apply dist_eq_sub_of_le h end theorem dist_tri_left (n m : ℕ) : m ≤ dist n m + n := le_trans le_tsub_add (add_le_add_right (nat.le_add_left _ _) _) theorem dist_tri_right (n m : ℕ) : m ≤ n + dist n m := by rw add_comm; apply dist_tri_left theorem dist_tri_left' (n m : ℕ) : n ≤ dist n m + m := by rw dist_comm; apply dist_tri_left theorem dist_tri_right' (n m : ℕ) : n ≤ m + dist n m := by rw dist_comm; apply dist_tri_right theorem dist_zero_right (n : ℕ) : dist n 0 = n := eq.trans (dist_eq_sub_of_le_right (zero_le n)) (tsub_zero n) theorem dist_zero_left (n : ℕ) : dist 0 n = n := eq.trans (dist_eq_sub_of_le (zero_le n)) (tsub_zero n) theorem dist_add_add_right (n k m : ℕ) : dist (n + k) (m + k) = dist n m := calc dist (n + k) (m + k) = ((n + k) - (m + k)) + ((m + k)-(n + k)) : rfl ... = (n - m) + ((m + k) - (n + k)) : by rw add_tsub_add_eq_tsub_right ... = (n - m) + (m - n) : by rw add_tsub_add_eq_tsub_right theorem dist_add_add_left (k n m : ℕ) : dist (k + n) (k + m) = dist n m := begin rw [add_comm k n, add_comm k m], apply dist_add_add_right end theorem dist_eq_intro {n m k l : ℕ} (h : n + m = k + l) : dist n k = dist l m := calc dist n k = dist (n + m) (k + m) : by rw dist_add_add_right ... = dist (k + l) (k + m) : by rw h ... = dist l m : by rw dist_add_add_left theorem dist.triangle_inequality (n m k : ℕ) : dist n k ≤ dist n m + dist m k := have dist n m + dist m k = (n - m) + (m - k) + ((k - m) + (m - n)), by simp [dist.def, add_comm, add_left_comm], by { rw [this, dist.def], exact add_le_add tsub_le_tsub_add_tsub tsub_le_tsub_add_tsub } theorem dist_mul_right (n k m : ℕ) : dist (n * k) (m * k) = dist n m * k := by rw [dist.def, dist.def, right_distrib, tsub_mul, tsub_mul] theorem dist_mul_left (k n m : ℕ) : dist (k * n) (k * m) = k * dist n m := by rw [mul_comm k n, mul_comm k m, dist_mul_right, mul_comm] -- TODO(Jeremy): do when we have max and minx --theorem dist_eq_max_sub_min {i j : nat} : dist i j = (max i j) - min i j := --sorry /- or.elim (lt_or_ge i j) (assume : i < j, by rw [max_eq_right_of_lt this, min_eq_left_of_lt this, dist_eq_sub_of_lt this]) (assume : i ≥ j, by rw [max_eq_left this , min_eq_right this, dist_eq_sub_of_le_right this]) -/ theorem dist_succ_succ {i j : nat} : dist (succ i) (succ j) = dist i j := by simp [dist.def, succ_sub_succ] theorem dist_pos_of_ne {i j : nat} : i ≠ j → 0 < dist i j := assume hne, nat.lt_by_cases (assume : i < j, begin rw [dist_eq_sub_of_le (le_of_lt this)], apply tsub_pos_of_lt this end) (assume : i = j, by contradiction) (assume : i > j, begin rw [dist_eq_sub_of_le_right (le_of_lt this)], apply tsub_pos_of_lt this end) end nat
d04665a02bbc2aa4f5256481935dac19f6a3db1e
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/category_theory/single_obj.lean
9275e9546200eb02eb6e4aac38bcc201c46bfa9d
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
5,389
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import category_theory.endomorphism import category_theory.category.Cat import algebra.category.Mon.basic /-! # Single-object category Single object category with a given monoid of endomorphisms. It is defined to facilitate transfering some definitions and lemmas (e.g., conjugacy etc.) from category theory to monoids and groups. ## Main definitions Given a type `α` with a monoid structure, `single_obj α` is `unit` type with `category` structure such that `End (single_obj α).star` is the monoid `α`. This can be extended to a functor `Mon ⥤ Cat`. If `α` is a group, then `single_obj α` is a groupoid. An element `x : α` can be reinterpreted as an element of `End (single_obj.star α)` using `single_obj.to_End`. ## Implementation notes - `category_struct.comp` on `End (single_obj.star α)` is `flip (*)`, not `(*)`. This way multiplication on `End` agrees with the multiplication on `α`. - By default, Lean puts instances into `category_theory` namespace instead of `category_theory.single_obj`, so we give all names explicitly. -/ universes u v w namespace category_theory /-- Type tag on `unit` used to define single-object categories and groupoids. -/ @[nolint unused_arguments has_inhabited_instance] def single_obj (α : Type u) : Type := unit namespace single_obj variables (α : Type u) /-- One and `flip (*)` become `id` and `comp` for morphisms of the single object category. -/ instance category_struct [has_one α] [has_mul α] : category_struct (single_obj α) := { hom := λ _ _, α, comp := λ _ _ _ x y, y * x, id := λ _, 1 } /-- Monoid laws become category laws for the single object category. -/ instance category [monoid α] : category (single_obj α) := { comp_id' := λ _ _, one_mul, id_comp' := λ _ _, mul_one, assoc' := λ _ _ _ _ x y z, (mul_assoc z y x).symm } /-- Groupoid structure on `single_obj α` -/ instance groupoid [group α] : groupoid (single_obj α) := { inv := λ _ _ x, x⁻¹, inv_comp' := λ _ _, mul_right_inv, comp_inv' := λ _ _, mul_left_inv } /-- The single object in `single_obj α`. -/ protected def star : single_obj α := unit.star /-- The endomorphisms monoid of the only object in `single_obj α` is equivalent to the original monoid α. -/ def to_End [monoid α] : α ≃* End (single_obj.star α) := { map_mul' := λ x y, rfl, .. equiv.refl α } lemma to_End_def [monoid α] (x : α) : to_End α x = x := rfl /-- There is a 1-1 correspondence between monoid homomorphisms `α → β` and functors between the corresponding single-object categories. It means that `single_obj` is a fully faithful functor. -/ def map_hom (α : Type u) (β : Type v) [monoid α] [monoid β] : (α →* β) ≃ (single_obj α) ⥤ (single_obj β) := { to_fun := λ f, { obj := id, map := λ _ _, ⇑f, map_id' := λ _, f.map_one, map_comp' := λ _ _ _ x y, f.map_mul y x }, inv_fun := λ f, { to_fun := @functor.map _ _ _ _ f (single_obj.star α) (single_obj.star α), map_one' := f.map_id _, map_mul' := λ x y, f.map_comp y x }, left_inv := λ ⟨f, h₁, h₂⟩, rfl, right_inv := λ f, by cases f; obviously } lemma map_hom_id (α : Type u) [monoid α] : map_hom α α (monoid_hom.id α) = 𝟭 _ := rfl lemma map_hom_comp {α : Type u} {β : Type v} [monoid α] [monoid β] (f : α →* β) {γ : Type w} [monoid γ] (g : β →* γ) : map_hom α γ (g.comp f) = map_hom α β f ⋙ map_hom β γ g := rfl end single_obj end category_theory open category_theory namespace monoid_hom /-- Reinterpret a monoid homomorphism `f : α → β` as a functor `(single_obj α) ⥤ (single_obj β)`. See also `category_theory.single_obj.map_hom` for an equivalence between these types. -/ @[reducible] def to_functor {α : Type u} {β : Type v} [monoid α] [monoid β] (f : α →* β) : (single_obj α) ⥤ (single_obj β) := single_obj.map_hom α β f @[simp] lemma id_to_functor (α : Type u) [monoid α] : (id α).to_functor = 𝟭 _ := rfl @[simp] lemma comp_to_functor {α : Type u} {β : Type v} [monoid α] [monoid β] (f : α →* β) {γ : Type w} [monoid γ] (g : β →* γ) : (g.comp f).to_functor = f.to_functor ⋙ g.to_functor := rfl end monoid_hom namespace units variables (α : Type u) [monoid α] /-- The units in a monoid are (multiplicatively) equivalent to the automorphisms of `star` when we think of the monoid as a single-object category. -/ def to_Aut : units α ≃* Aut (single_obj.star α) := (units.map_equiv (single_obj.to_End α)).trans $ Aut.units_End_equiv_Aut _ @[simp] lemma to_Aut_hom (x : units α) : (to_Aut α x).hom = single_obj.to_End α x := rfl @[simp] lemma to_Aut_inv (x : units α) : (to_Aut α x).inv = single_obj.to_End α (x⁻¹ : units α) := rfl end units namespace Mon open category_theory /-- The fully faithful functor from `Mon` to `Cat`. -/ def to_Cat : Mon ⥤ Cat := { obj := λ x, Cat.of (single_obj x), map := λ x y f, single_obj.map_hom x y f } instance to_Cat_full : full to_Cat := { preimage := λ x y, (single_obj.map_hom x y).inv_fun, witness' := λ x y, by apply equiv.right_inv } instance to_Cat_faithful : faithful to_Cat := { map_injective' := λ x y, by apply equiv.injective } end Mon
f6cb7a05ca51d4a4ed33253691a1c576bafd7eb9
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/probability/probability_mass_function/basic.lean
b849e214b150b9dbe5f6774bf6a11927ac7685e5
[ "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
14,532
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, Devon Tuma -/ import topology.instances.ennreal import measure_theory.measure.measure_space /-! # Probability mass functions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file is about probability mass functions or discrete probability measures: a function `α → ℝ≥0∞` such that the values have (infinite) sum `1`. Construction of monadic `pure` and `bind` is found in `probability_mass_function/monad.lean`, other constructions of `pmf`s are found in `probability_mass_function/constructions.lean`. Given `p : pmf α`, `pmf.to_outer_measure` constructs an `outer_measure` on `α`, by assigning each set the sum of the probabilities of each of its elements. Under this outer measure, every set is Carathéodory-measurable, so we can further extend this to a `measure` on `α`, see `pmf.to_measure`. `pmf.to_measure.is_probability_measure` shows this associated measure is a probability measure. Conversely, given a probability measure `μ` on a measurable space `α` with all singleton sets measurable, `μ.to_pmf` constructs a `pmf` on `α`, setting the probability mass of a point `x` to be the measure of the singleton set `{x}`. ## Tags probability mass function, discrete probability measure -/ noncomputable theory variables {α β γ : Type*} open_locale classical big_operators nnreal ennreal measure_theory /-- A probability mass function, or discrete probability measures is a function `α → ℝ≥0∞` such that the values have (infinite) sum `1`. -/ def {u} pmf (α : Type u) : Type u := { f : α → ℝ≥0∞ // has_sum f 1 } namespace pmf instance fun_like : fun_like (pmf α) α (λ p, ℝ≥0∞) := { coe := λ p a, p.1 a, coe_injective' := λ p q h, subtype.eq h } @[ext] protected lemma ext {p q : pmf α} (h : ∀ x, p x = q x) : p = q := fun_like.ext p q h lemma ext_iff {p q : pmf α} : p = q ↔ ∀ x, p x = q x := fun_like.ext_iff lemma has_sum_coe_one (p : pmf α) : has_sum p 1 := p.2 @[simp] lemma tsum_coe (p : pmf α) : ∑' a, p a = 1 := p.has_sum_coe_one.tsum_eq lemma tsum_coe_ne_top (p : pmf α) : ∑' a, p a ≠ ∞ := p.tsum_coe.symm ▸ ennreal.one_ne_top lemma tsum_coe_indicator_ne_top (p : pmf α) (s : set α) : ∑' a, s.indicator p a ≠ ∞ := ne_of_lt (lt_of_le_of_lt (tsum_le_tsum (λ a, set.indicator_apply_le (λ _, le_rfl)) ennreal.summable ennreal.summable) (lt_of_le_of_ne le_top p.tsum_coe_ne_top)) @[simp] lemma coe_ne_zero (p : pmf α) : ⇑p ≠ 0 := λ hp, zero_ne_one ((tsum_zero.symm.trans (tsum_congr $ λ x, symm (congr_fun hp x))).trans p.tsum_coe) /-- The support of a `pmf` is the set where it is nonzero. -/ def support (p : pmf α) : set α := function.support p @[simp] lemma mem_support_iff (p : pmf α) (a : α) : a ∈ p.support ↔ p a ≠ 0 := iff.rfl @[simp] lemma support_nonempty (p : pmf α) : p.support.nonempty := function.support_nonempty_iff.2 p.coe_ne_zero lemma apply_eq_zero_iff (p : pmf α) (a : α) : p a = 0 ↔ a ∉ p.support := by rw [mem_support_iff, not_not] lemma apply_pos_iff (p : pmf α) (a : α) : 0 < p a ↔ a ∈ p.support := pos_iff_ne_zero.trans (p.mem_support_iff a).symm lemma apply_eq_one_iff (p : pmf α) (a : α) : p a = 1 ↔ p.support = {a} := begin refine ⟨λ h, set.subset.antisymm (λ a' ha', by_contra $ λ ha, _) (λ a' ha', ha'.symm ▸ (p.mem_support_iff a).2 (λ ha, zero_ne_one $ ha.symm.trans h)), λ h, trans (symm $ tsum_eq_single a (λ a' ha', (p.apply_eq_zero_iff a').2 (h.symm ▸ ha'))) p.tsum_coe⟩, suffices : 1 < ∑' a, p a, from ne_of_lt this p.tsum_coe.symm, have : 0 < ∑' b, ite (b = a) 0 (p b), from lt_of_le_of_ne' zero_le' ((tsum_ne_zero_iff ennreal.summable).2 ⟨a', ite_ne_left_iff.2 ⟨ha, ne.symm $ (p.mem_support_iff a').2 ha'⟩⟩), calc 1 = 1 + 0 : (add_zero 1).symm ... < p a + ∑' b, ite (b = a) 0 (p b) : ennreal.add_lt_add_of_le_of_lt ennreal.one_ne_top (le_of_eq h.symm) this ... = ite (a = a) (p a) 0 + ∑' b, ite (b = a) 0 (p b) : by rw [eq_self_iff_true, if_true] ... = ∑' b, ite (b = a) (p b) 0 + ∑' b, ite (b = a) 0 (p b) : by { congr, exact symm (tsum_eq_single a $ λ b hb, if_neg hb) } ... = ∑' b, (ite (b = a) (p b) 0 + ite (b = a) 0 (p b)) : ennreal.tsum_add.symm ... = ∑' b, p b : tsum_congr (λ b, by split_ifs; simp only [zero_add, add_zero, le_rfl]) end lemma coe_le_one (p : pmf α) (a : α) : p a ≤ 1 := has_sum_le (by { intro b, split_ifs; simp only [h, zero_le', le_rfl] }) (has_sum_ite_eq a (p a)) (has_sum_coe_one p) lemma apply_ne_top (p : pmf α) (a : α) : p a ≠ ∞ := ne_of_lt (lt_of_le_of_lt (p.coe_le_one a) ennreal.one_lt_top) lemma apply_lt_top (p : pmf α) (a : α) : p a < ∞ := lt_of_le_of_ne le_top (p.apply_ne_top a) section outer_measure open measure_theory measure_theory.outer_measure /-- Construct an `outer_measure` from a `pmf`, by assigning measure to each set `s : set α` equal to the sum of `p x` for for each `x ∈ α` -/ def to_outer_measure (p : pmf α) : outer_measure α := outer_measure.sum (λ (x : α), p x • dirac x) variables (p : pmf α) (s t : set α) lemma to_outer_measure_apply : p.to_outer_measure s = ∑' x, s.indicator p x := tsum_congr (λ x, smul_dirac_apply (p x) x s) @[simp] lemma to_outer_measure_caratheodory : p.to_outer_measure.caratheodory = ⊤ := begin refine (eq_top_iff.2 $ le_trans (le_Inf $ λ x hx, _) (le_sum_caratheodory _)), exact let ⟨y, hy⟩ := hx in ((le_of_eq (dirac_caratheodory y).symm).trans (le_smul_caratheodory _ _)).trans (le_of_eq hy), end @[simp] lemma to_outer_measure_apply_finset (s : finset α) : p.to_outer_measure s = ∑ x in s, p x := begin refine (to_outer_measure_apply p s).trans ((@tsum_eq_sum _ _ _ _ _ _ s _).trans _), { exact λ x hx, set.indicator_of_not_mem hx _ }, { exact finset.sum_congr rfl (λ x hx, set.indicator_of_mem hx _) } end lemma to_outer_measure_apply_singleton (a : α) : p.to_outer_measure {a} = p a := begin refine (p.to_outer_measure_apply {a}).trans ((tsum_eq_single a $ λ b hb, _).trans _), { exact ite_eq_right_iff.2 (λ hb', false.elim $ hb hb') }, { exact ite_eq_left_iff.2 (λ ha', false.elim $ ha' rfl) } end lemma to_outer_measure_injective : (to_outer_measure : pmf α → outer_measure α).injective := λ p q h, pmf.ext (λ x, (p.to_outer_measure_apply_singleton x).symm.trans ((congr_fun (congr_arg _ h) _).trans $ q.to_outer_measure_apply_singleton x)) @[simp] lemma to_outer_measure_inj {p q : pmf α} : p.to_outer_measure = q.to_outer_measure ↔ p = q := to_outer_measure_injective.eq_iff lemma to_outer_measure_apply_eq_zero_iff : p.to_outer_measure s = 0 ↔ disjoint p.support s := begin rw [to_outer_measure_apply, ennreal.tsum_eq_zero], exact function.funext_iff.symm.trans set.indicator_eq_zero', end lemma to_outer_measure_apply_eq_one_iff : p.to_outer_measure s = 1 ↔ p.support ⊆ s := begin refine (p.to_outer_measure_apply s).symm ▸ ⟨λ h a hap, _, λ h, _⟩, { refine by_contra (λ hs, ne_of_lt _ (h.trans p.tsum_coe.symm)), have hs' : s.indicator p a = 0 := set.indicator_apply_eq_zero.2 (λ hs', false.elim $ hs hs'), have hsa : s.indicator p a < p a := hs'.symm ▸ (p.apply_pos_iff a).2 hap, exact ennreal.tsum_lt_tsum (p.tsum_coe_indicator_ne_top s) (λ x, set.indicator_apply_le $ λ _, le_rfl) hsa }, { suffices : ∀ x ∉ s, p x = 0, from trans (tsum_congr $ λ a, (set.indicator_apply s p a).trans (ite_eq_left_iff.2 $ symm ∘ (this a))) p.tsum_coe, exact λ a ha, (p.apply_eq_zero_iff a).2 $ set.not_mem_subset h ha } end @[simp] lemma to_outer_measure_apply_inter_support : p.to_outer_measure (s ∩ p.support) = p.to_outer_measure s := by simp only [to_outer_measure_apply, pmf.support, set.indicator_inter_support] /-- Slightly stronger than `outer_measure.mono` having an intersection with `p.support` -/ lemma to_outer_measure_mono {s t : set α} (h : s ∩ p.support ⊆ t) : p.to_outer_measure s ≤ p.to_outer_measure t := le_trans (le_of_eq (to_outer_measure_apply_inter_support p s).symm) (p.to_outer_measure.mono h) lemma to_outer_measure_apply_eq_of_inter_support_eq {s t : set α} (h : s ∩ p.support = t ∩ p.support) : p.to_outer_measure s = p.to_outer_measure t := le_antisymm (p.to_outer_measure_mono (h.symm ▸ (set.inter_subset_left t p.support))) (p.to_outer_measure_mono (h ▸ (set.inter_subset_left s p.support))) @[simp] lemma to_outer_measure_apply_fintype [fintype α] : p.to_outer_measure s = ∑ x, s.indicator p x := (p.to_outer_measure_apply s).trans (tsum_eq_sum (λ x h, absurd (finset.mem_univ x) h)) end outer_measure section measure open measure_theory /-- Since every set is Carathéodory-measurable under `pmf.to_outer_measure`, we can further extend this `outer_measure` to a `measure` on `α` -/ def to_measure [measurable_space α] (p : pmf α) : measure α := p.to_outer_measure.to_measure ((to_outer_measure_caratheodory p).symm ▸ le_top) variables [measurable_space α] (p : pmf α) (s t : set α) lemma to_outer_measure_apply_le_to_measure_apply : p.to_outer_measure s ≤ p.to_measure s := le_to_measure_apply p.to_outer_measure _ s lemma to_measure_apply_eq_to_outer_measure_apply (hs : measurable_set s) : p.to_measure s = p.to_outer_measure s := to_measure_apply p.to_outer_measure _ hs lemma to_measure_apply (hs : measurable_set s) : p.to_measure s = ∑' x, s.indicator p x := (p.to_measure_apply_eq_to_outer_measure_apply s hs).trans (p.to_outer_measure_apply s) lemma to_measure_apply_singleton (a : α) (h : measurable_set ({a} : set α)) : p.to_measure {a} = p a := by simp [to_measure_apply_eq_to_outer_measure_apply _ _ h, to_outer_measure_apply_singleton] lemma to_measure_apply_eq_zero_iff (hs : measurable_set s) : p.to_measure s = 0 ↔ disjoint p.support s := by rw [to_measure_apply_eq_to_outer_measure_apply p s hs, to_outer_measure_apply_eq_zero_iff] lemma to_measure_apply_eq_one_iff (hs : measurable_set s) : p.to_measure s = 1 ↔ p.support ⊆ s := (p.to_measure_apply_eq_to_outer_measure_apply s hs : p.to_measure s = p.to_outer_measure s).symm ▸ (p.to_outer_measure_apply_eq_one_iff s) @[simp] lemma to_measure_apply_inter_support (hs : measurable_set s) (hp : measurable_set p.support) : p.to_measure (s ∩ p.support) = p.to_measure s := by simp [p.to_measure_apply_eq_to_outer_measure_apply s hs, p.to_measure_apply_eq_to_outer_measure_apply _ (hs.inter hp)] lemma to_measure_mono {s t : set α} (hs : measurable_set s) (ht : measurable_set t) (h : s ∩ p.support ⊆ t) : p.to_measure s ≤ p.to_measure t := by simpa only [p.to_measure_apply_eq_to_outer_measure_apply, hs, ht] using to_outer_measure_mono p h lemma to_measure_apply_eq_of_inter_support_eq {s t : set α} (hs : measurable_set s) (ht : measurable_set t) (h : s ∩ p.support = t ∩ p.support) : p.to_measure s = p.to_measure t := by simpa only [p.to_measure_apply_eq_to_outer_measure_apply, hs, ht] using to_outer_measure_apply_eq_of_inter_support_eq p h section measurable_singleton_class variables [measurable_singleton_class α] lemma to_measure_injective : (to_measure : pmf α → measure α).injective := λ p q h, pmf.ext (λ x, (p.to_measure_apply_singleton x $ measurable_set_singleton x).symm.trans ((congr_fun (congr_arg _ h) _).trans $ q.to_measure_apply_singleton x $ measurable_set_singleton x)) @[simp] lemma to_measure_inj {p q : pmf α} : p.to_measure = q.to_measure ↔ p = q := to_measure_injective.eq_iff @[simp] lemma to_measure_apply_finset (s : finset α) : p.to_measure s = ∑ x in s, p x := (p.to_measure_apply_eq_to_outer_measure_apply s s.measurable_set).trans (p.to_outer_measure_apply_finset s) lemma to_measure_apply_of_finite (hs : s.finite) : p.to_measure s = ∑' x, s.indicator p x := (p.to_measure_apply_eq_to_outer_measure_apply s hs.measurable_set).trans (p.to_outer_measure_apply s) @[simp] lemma to_measure_apply_fintype [fintype α] : p.to_measure s = ∑ x, s.indicator p x := (p.to_measure_apply_eq_to_outer_measure_apply s s.to_finite.measurable_set).trans (p.to_outer_measure_apply_fintype s) end measurable_singleton_class end measure end pmf namespace measure_theory open pmf namespace measure /-- Given that `α` is a countable, measurable space with all singleton sets measurable, we can convert any probability measure into a `pmf`, where the mass of a point is the measure of the singleton set under the original measure. -/ def to_pmf [countable α] [measurable_space α] [measurable_singleton_class α] (μ : measure α) [h : is_probability_measure μ] : pmf α := ⟨λ x, μ ({x} : set α), ennreal.summable.has_sum_iff.2 (trans (symm $ (tsum_indicator_apply_singleton μ set.univ measurable_set.univ).symm.trans (tsum_congr (λ x, congr_fun (set.indicator_univ _) x))) (h.measure_univ))⟩ variables [countable α] [measurable_space α] [measurable_singleton_class α] (μ : measure α) [is_probability_measure μ] lemma to_pmf_apply (x : α) : μ.to_pmf x = μ {x} := rfl @[simp] lemma to_pmf_to_measure : μ.to_pmf.to_measure = μ := measure.ext (λ s hs, by simpa only [μ.to_pmf.to_measure_apply s hs, ← μ.tsum_indicator_apply_singleton s hs]) end measure end measure_theory namespace pmf open measure_theory /-- The measure associated to a `pmf` by `to_measure` is a probability measure -/ instance to_measure.is_probability_measure [measurable_space α] (p : pmf α) : is_probability_measure (p.to_measure) := ⟨by simpa only [measurable_set.univ, to_measure_apply_eq_to_outer_measure_apply, set.indicator_univ, to_outer_measure_apply, ennreal.coe_eq_one] using tsum_coe p⟩ variables [countable α] [measurable_space α] [measurable_singleton_class α] (p : pmf α) (μ : measure α) [is_probability_measure μ] @[simp] lemma to_measure_to_pmf : p.to_measure.to_pmf = p := pmf.ext (λ x, by rw [← p.to_measure_apply_singleton x (measurable_set_singleton x), p.to_measure.to_pmf_apply]) lemma to_measure_eq_iff_eq_to_pmf (μ : measure α) [is_probability_measure μ] : p.to_measure = μ ↔ p = μ.to_pmf := by rw [← to_measure_inj, measure.to_pmf_to_measure] lemma to_pmf_eq_iff_to_measure_eq (μ : measure α) [is_probability_measure μ] : μ.to_pmf = p ↔ μ = p.to_measure := by rw [← to_measure_inj, measure.to_pmf_to_measure] end pmf
be73c1eb783b4405cb9351d0ebbdb8e80e3b89d9
491068d2ad28831e7dade8d6dff871c3e49d9431
/library/theories/group_theory/cyclic.lean
6b80cc44c3baf7f5a9a4e2583ae24796e65776c9
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,737
lean
/- Copyright (c) 2015 Haitao Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Haitao Zhang -/ import data algebra.group algebra.group_power .finsubg .hom .perm open function algebra finset open eq.ops namespace group section cyclic open nat fin list local attribute madd [reducible] variable {A : Type} variable [ambG : group A] include ambG lemma pow_mod {a : A} {n m : nat} : a ^ m = 1 → a ^ n = a ^ (n mod m) := assume Pid, assert a ^ (n div m * m) = 1, from calc a ^ (n div m * m) = a ^ (m * (n div m)) : by rewrite (mul.comm (n div m) m) ... = (a ^ m) ^ (n div m) : by rewrite pow_mul ... = 1 ^ (n div m) : by rewrite Pid ... = 1 : one_pow (n div m), calc a ^ n = a ^ (n div m * m + n mod m) : by rewrite -(eq_div_mul_add_mod n m) ... = a ^ (n div m * m) * a ^ (n mod m) : by rewrite pow_add ... = 1 * a ^ (n mod m) : by rewrite this ... = a ^ (n mod m) : by rewrite one_mul lemma pow_sub_eq_one_of_pow_eq {a : A} {i j : nat} : a^i = a^j → a^(i - j) = 1 := assume Pe, or.elim (lt_or_ge i j) (assume Piltj, begin rewrite [sub_eq_zero_of_le (nat.le_of_lt Piltj)] end) (assume Pigej, begin rewrite [pow_sub a Pigej, Pe, mul.right_inv] end) lemma pow_dist_eq_one_of_pow_eq {a : A} {i j : nat} : a^i = a^j → a^(dist i j) = 1 := assume Pe, or.elim (lt_or_ge i j) (suppose i < j, by rewrite [dist_eq_sub_of_lt this]; exact pow_sub_eq_one_of_pow_eq (eq.symm Pe)) (suppose i ≥ j, by rewrite [dist_eq_sub_of_ge this]; exact pow_sub_eq_one_of_pow_eq Pe) lemma pow_madd {a : A} {n : nat} {i j : fin (succ n)} : a^(succ n) = 1 → a^(val (i + j)) = a^i * a^j := assume Pe, calc a^(val (i + j)) = a^((i + j) mod (succ n)) : rfl ... = a^(i + j) : by rewrite [-pow_mod Pe] ... = a^i * a^j : by rewrite pow_add lemma mk_pow_mod {a : A} {n m : nat} : a ^ (succ m) = 1 → a ^ n = a ^ (mk_mod m n) := assume Pe, pow_mod Pe variable [finA : fintype A] include finA open fintype variable [deceqA : decidable_eq A] include deceqA lemma exists_pow_eq_one (a : A) : ∃ n, n < card A ∧ a ^ (succ n) = 1 := let f := (λ i : fin (succ (card A)), a ^ i) in assert Pninj : ¬(injective f), from assume Pinj, absurd (card_le_of_inj _ _ (exists.intro f Pinj)) (begin rewrite [card_fin], apply not_succ_le_self end), obtain i₁ P₁, from exists_not_of_not_forall Pninj, obtain i₂ P₂, from exists_not_of_not_forall P₁, obtain Pfe Pne, from iff.elim_left not_implies_iff_and_not P₂, assert Pvne : val i₁ ≠ val i₂, from assume Pveq, absurd (eq_of_veq Pveq) Pne, exists.intro (pred (dist i₁ i₂)) (begin rewrite [succ_pred_of_pos (dist_pos_of_ne Pvne)], apply and.intro, apply lt_of_succ_lt_succ, rewrite [succ_pred_of_pos (dist_pos_of_ne Pvne)], apply nat.lt_of_le_of_lt dist_le_max (max_lt i₁ i₂), apply pow_dist_eq_one_of_pow_eq Pfe end) -- Another possibility is to generate a list of powers and use find to get the first -- unity. -- The bound on bex is arbitrary as long as it is large enough (at least card A). Making -- it larger simplifies some proofs, such as a ∈ cyc a. definition cyc (a : A) : finset A := {x ∈ univ | bex (succ (card A)) (λ n, a ^ n = x)} definition order (a : A) := card (cyc a) definition pow_fin (a : A) (n : nat) (i : fin (order a)) := pow a (i + n) definition cyc_pow_fin (a : A) (n : nat) : finset A := image (pow_fin a n) univ lemma order_le_group_order {a : A} : order a ≤ card A := card_le_card_of_subset !subset_univ lemma cyc_has_one (a : A) : 1 ∈ cyc a := begin apply mem_sep_of_mem !mem_univ, existsi 0, apply and.intro, apply zero_lt_succ, apply pow_zero end lemma order_pos (a : A) : 0 < order a := length_pos_of_mem (cyc_has_one a) lemma cyc_mul_closed (a : A) : finset_mul_closed_on (cyc a) := take g h, assume Pgin Phin, obtain n Plt Pe, from exists_pow_eq_one a, obtain i Pilt Pig, from of_mem_sep Pgin, obtain j Pjlt Pjh, from of_mem_sep Phin, begin rewrite [-Pig, -Pjh, -pow_add, pow_mod Pe], apply mem_sep_of_mem !mem_univ, existsi ((i + j) mod (succ n)), apply and.intro, apply nat.lt.trans (mod_lt (i+j) !zero_lt_succ) (succ_lt_succ Plt), apply rfl end lemma cyc_has_inv (a : A) : finset_has_inv (cyc a) := take g, assume Pgin, obtain n Plt Pe, from exists_pow_eq_one a, obtain i Pilt Pig, from of_mem_sep Pgin, let ni := -(mk_mod n i) in assert Pinv : g*a^ni = 1, by rewrite [-Pig, mk_pow_mod Pe, -(pow_madd Pe), add.right_inv], begin rewrite [inv_eq_of_mul_eq_one Pinv], apply mem_sep_of_mem !mem_univ, existsi ni, apply and.intro, apply nat.lt.trans (is_lt ni) (succ_lt_succ Plt), apply rfl end lemma self_mem_cyc (a : A) : a ∈ cyc a := mem_sep_of_mem !mem_univ (exists.intro (1 : nat) (and.intro (succ_lt_succ card_pos) !pow_one)) lemma mem_cyc (a : A) : ∀ {n : nat}, a^n ∈ cyc a | 0 := cyc_has_one a | (succ n) := begin rewrite pow_succ', apply cyc_mul_closed a, exact mem_cyc, apply self_mem_cyc end lemma order_le {a : A} {n : nat} : a^(succ n) = 1 → order a ≤ succ n := assume Pe, let s := image (pow a) (upto (succ n)) in assert Psub: cyc a ⊆ s, from subset_of_forall (take g, assume Pgin, obtain i Pilt Pig, from of_mem_sep Pgin, begin rewrite [-Pig, pow_mod Pe], apply mem_image, apply mem_upto_of_lt (mod_lt i !zero_lt_succ), exact rfl end), #nat calc order a ≤ card s : card_le_card_of_subset Psub ... ≤ card (upto (succ n)) : !card_image_le ... = succ n : card_upto (succ n) lemma pow_ne_of_lt_order {a : A} {n : nat} : succ n < order a → a^(succ n) ≠ 1 := assume Plt, not_imp_not_of_imp order_le (nat.not_le_of_gt Plt) lemma eq_zero_of_pow_eq_one {a : A} : ∀ {n : nat}, a^n = 1 → n < order a → n = 0 | 0 := assume Pe Plt, rfl | (succ n) := assume Pe Plt, absurd Pe (pow_ne_of_lt_order Plt) lemma pow_fin_inj (a : A) (n : nat) : injective (pow_fin a n) := take i j, suppose a^(i + n) = a^(j + n), have a^(dist i j) = 1, begin apply !dist_add_add_right ▸ (pow_dist_eq_one_of_pow_eq this) end, have dist i j = 0, from eq_zero_of_pow_eq_one this (nat.lt_of_le_of_lt dist_le_max (max_lt i j)), eq_of_veq (eq_of_dist_eq_zero this) lemma cyc_eq_cyc (a : A) (n : nat) : cyc_pow_fin a n = cyc a := assert Psub : cyc_pow_fin a n ⊆ cyc a, from subset_of_forall (take g, assume Pgin, obtain i Pin Pig, from exists_of_mem_image Pgin, by rewrite [-Pig]; apply mem_cyc), eq_of_card_eq_of_subset (begin apply eq.trans, apply card_image_eq_of_inj_on, rewrite [to_set_univ, -set.injective_iff_inj_on_univ], exact pow_fin_inj a n, rewrite [card_fin] end) Psub lemma pow_order (a : A) : a^(order a) = 1 := obtain i Pin Pone, from exists_of_mem_image (eq.symm (cyc_eq_cyc a 1) ▸ cyc_has_one a), or.elim (eq_or_lt_of_le (succ_le_of_lt (is_lt i))) (assume P, P ▸ Pone) (assume P, absurd Pone (pow_ne_of_lt_order P)) lemma eq_one_of_order_eq_one {a : A} : order a = 1 → a = 1 := assume Porder, calc a = a^1 : by rewrite (pow_one a) ... = a^(order a) : by rewrite Porder ... = 1 : by rewrite pow_order lemma order_of_min_pow {a : A} {n : nat} (Pone : a^(succ n) = 1) (Pmin : ∀ i, i < n → a^(succ i) ≠ 1) : order a = succ n := or.elim (eq_or_lt_of_le (order_le Pone)) (λ P, P) (λ P : order a < succ n, begin assert Pn : a^(order a) ≠ 1, rewrite [-(succ_pred_of_pos (order_pos a))], apply Pmin, apply nat.lt_of_succ_lt_succ, rewrite [succ_pred_of_pos !order_pos], assumption, exact absurd (pow_order a) Pn end) lemma order_dvd_of_pow_eq_one {a : A} {n : nat} (Pone : a^n = 1) : order a ∣ n := assert Pe : a^(n mod order a) = 1, from begin revert Pone, rewrite [eq_div_mul_add_mod n (order a) at {1}, pow_add, mul.comm _ (order a), pow_mul, pow_order, one_pow, one_mul], intros, assumption end, dvd_of_mod_eq_zero (eq_zero_of_pow_eq_one Pe (mod_lt n !order_pos)) definition cyc_is_finsubg [instance] (a : A) : is_finsubg (cyc a) := is_finsubg.mk (cyc_has_one a) (cyc_mul_closed a) (cyc_has_inv a) lemma order_dvd_group_order (a : A) : order a ∣ card A := dvd.intro (eq.symm (!mul.comm ▸ lagrange_theorem (subset_univ (cyc a)))) definition pow_fin' (a : A) (i : fin (succ (pred (order a)))) := pow a i local attribute group_of_add_group [instance] lemma pow_fin_hom (a : A) : homomorphic (pow_fin' a) := take i j, begin rewrite [↑pow_fin'], apply pow_madd, rewrite [succ_pred_of_pos !order_pos], exact pow_order a end definition pow_fin_is_iso (a : A) : is_iso_class (pow_fin' a) := is_iso_class.mk (pow_fin_hom a) (begin rewrite [↑pow_fin', succ_pred_of_pos !order_pos], exact pow_fin_inj a 0 end) end cyclic section rot open nat list open fin fintype list section local attribute group_of_add_group [instance] local infix ^ := algebra.pow lemma pow_eq_mul {n : nat} {i : fin (succ n)} : ∀ {k : nat}, i^k = mk_mod n (i*k) | 0 := by rewrite [pow_zero] | (succ k) := begin assert Psucc : i^(succ k) = madd (i^k) i, apply pow_succ', rewrite [Psucc, pow_eq_mul], apply eq_of_veq, rewrite [mul_succ, val_madd, ↑mk_mod, mod_add_mod] end end definition rotl : ∀ {n : nat} m : nat, fin n → fin n | 0 := take m i, elim0 i | (succ n) := take m, madd (mk_mod n (n*m)) definition rotr : ∀ {n : nat} m : nat, fin n → fin n | (0:nat) := take m i, elim0 i | (nat.succ n) := take m, madd (-(mk_mod n (n*m))) lemma rotl_succ' {n m : nat} : rotl m = madd (mk_mod n (n*m)) := rfl lemma rotl_zero : ∀ {n : nat}, @rotl n 0 = id | 0 := funext take i, elim0 i | (succ n) := funext take i, zero_add i lemma rotl_id : ∀ {n : nat}, @rotl n n = id | 0 := funext take i, elim0 i | (succ n) := assert P : mk_mod n (n * succ n) = mk_mod n 0, from eq_of_veq !mul_mod_left, begin rewrite [rotl_succ', P], apply rotl_zero end lemma rotl_to_zero {n i : nat} : rotl i (mk_mod n i) = zero n := eq_of_veq begin rewrite [↑rotl, val_madd], esimp [mk_mod], rewrite [ mod_add_mod, add_mod_mod, -succ_mul, mul_mod_right] end lemma rotl_compose : ∀ {n : nat} {j k : nat}, (@rotl n j) ∘ (rotl k) = rotl (j + k) | 0 := take j k, funext take i, elim0 i | (succ n) := take j k, funext take i, eq.symm begin rewrite [*rotl_succ', mul.left_distrib, -(@madd_mk_mod n (n*j)), madd_assoc], end lemma rotr_rotl : ∀ {n : nat} (m : nat) {i : fin n}, rotr m (rotl m i) = i | 0 := take m i, elim0 i | (nat.succ n) := take m i, calc (-(mk_mod n (n*m))) + ((mk_mod n (n*m)) + i) = i : by rewrite neg_add_cancel_left lemma rotl_rotr : ∀ {n : nat} (m : nat), (@rotl n m) ∘ (rotr m) = id | 0 := take m, funext take i, elim0 i | (nat.succ n) := take m, funext take i, calc (mk_mod n (n*m)) + (-(mk_mod n (n*m)) + i) = i : add_neg_cancel_left lemma rotl_succ {n : nat} : (rotl 1) ∘ (@succ n) = lift_succ := funext (take i, eq_of_veq (begin rewrite [↑compose, ↑rotl, ↑madd, mul_one n, ↑mk_mod, mod_add_mod, ↑lift_succ, val_succ, -succ_add_eq_succ_add, add_mod_self_left, mod_eq_of_lt (lt.trans (is_lt i) !lt_succ_self), -val_lift] end)) definition list.rotl {A : Type} : ∀ l : list A, list A | [] := [] | (a::l) := l++[a] lemma rotl_cons {A : Type} {a : A} {l} : list.rotl (a::l) = l++[a] := rfl lemma rotl_map {A B : Type} {f : A → B} : ∀ {l : list A}, list.rotl (map f l) = map f (list.rotl l) | [] := rfl | (a::l) := begin rewrite [map_cons, *rotl_cons, map_append] end lemma rotl_eq_rotl : ∀ {n : nat}, map (rotl 1) (upto n) = list.rotl (upto n) | 0 := rfl | (succ n) := begin rewrite [upto_step at {1}, upto_succ, rotl_cons, map_append], congruence, rewrite [map_map], congruence, exact rotl_succ, rewrite [map_singleton], congruence, rewrite [↑rotl, mul_one n, ↑mk_mod, ↑zero, ↑maxi, ↑madd], congruence, rewrite [ mod_add_mod, nat.add_zero, mod_eq_of_lt !lt_succ_self ] end definition seq [reducible] (A : Type) (n : nat) := fin n → A variable {A : Type} definition rotl_fun {n : nat} (m : nat) (f : seq A n) : seq A n := f ∘ (rotl m) definition rotr_fun {n : nat} (m : nat) (f : seq A n) : seq A n := f ∘ (rotr m) lemma rotl_seq_zero {n : nat} : rotl_fun 0 = @id (seq A n) := funext take f, begin rewrite [↑rotl_fun, rotl_zero] end lemma rotl_seq_ne_id : ∀ {n : nat}, (∃ a b : A, a ≠ b) → ∀ i, i < n → rotl_fun (succ i) ≠ (@id (seq A (succ n))) | 0 := assume Pex, take i, assume Piltn, absurd Piltn !not_lt_zero | (succ n) := assume Pex, obtain a b Pne, from Pex, take i, assume Pilt, let f := (λ j : fin (succ (succ n)), if j = zero (succ n) then a else b), fi := mk_mod (succ n) (succ i) in have Pfne : rotl_fun (succ i) f fi ≠ f fi, from begin rewrite [↑rotl_fun, rotl_to_zero, mk_mod_of_lt (succ_lt_succ Pilt), if_pos rfl, if_neg mk_succ_ne_zero], assumption end, have P : rotl_fun (succ i) f ≠ f, from assume Peq, absurd (congr_fun Peq fi) Pfne, assume Peq, absurd (congr_fun Peq f) P lemma rotr_rotl_fun {n : nat} (m : nat) (f : seq A n) : rotr_fun m (rotl_fun m f) = f := calc f ∘ (rotl m) ∘ (rotr m) = f ∘ ((rotl m) ∘ (rotr m)) : by rewrite -compose.assoc ... = f ∘ id : by rewrite (rotl_rotr m) lemma rotl_fun_inj {n : nat} {m : nat} : @injective (seq A n) (seq A n) (rotl_fun m) := injective_of_has_left_inverse (exists.intro (rotr_fun m) (rotr_rotl_fun m)) lemma seq_rotl_eq_list_rotl {n : nat} (f : seq A n) : fun_to_list (rotl_fun 1 f) = list.rotl (fun_to_list f) := begin rewrite [↑fun_to_list, ↑rotl_fun, -map_map, rotl_map], congruence, exact rotl_eq_rotl end end rot section rotg open nat fin fintype definition rotl_perm [reducible] (A : Type) [finA : fintype A] [deceqA : decidable_eq A] (n : nat) (m : nat) : perm (seq A n) := perm.mk (rotl_fun m) rotl_fun_inj variable {A : Type} variable [finA : fintype A] variable [deceqA : decidable_eq A] variable {n : nat} include finA deceqA lemma rotl_perm_mul {i j : nat} : (rotl_perm A n i) * (rotl_perm A n j) = rotl_perm A n (j+i) := eq_of_feq (funext take f, calc f ∘ (rotl j) ∘ (rotl i) = f ∘ ((rotl j) ∘ (rotl i)) : by rewrite -compose.assoc ... = f ∘ (rotl (j+i)) : by rewrite rotl_compose) lemma rotl_perm_pow_eq : ∀ {i : nat}, (rotl_perm A n 1) ^ i = rotl_perm A n i | 0 := begin rewrite [pow_zero, ↑rotl_perm, perm_one, -eq_iff_feq], esimp, rewrite rotl_seq_zero end | (succ i) := begin rewrite [pow_succ', rotl_perm_pow_eq, rotl_perm_mul, one_add] end lemma rotl_perm_pow_eq_one : (rotl_perm A n 1) ^ n = 1 := eq.trans rotl_perm_pow_eq (eq_of_feq begin esimp [rotl_perm], rewrite [↑rotl_fun, rotl_id] end) lemma rotl_perm_mod {i : nat} : rotl_perm A n i = rotl_perm A n (i mod n) := calc rotl_perm A n i = (rotl_perm A n 1) ^ i : by rewrite rotl_perm_pow_eq ... = (rotl_perm A n 1) ^ (i mod n) : by rewrite (pow_mod rotl_perm_pow_eq_one) ... = rotl_perm A n (i mod n) : by rewrite rotl_perm_pow_eq -- needs A to have at least two elements! lemma rotl_perm_pow_ne_one (Pex : ∃ a b : A, a ≠ b) : ∀ i, i < n → (rotl_perm A (succ n) 1)^(succ i) ≠ 1 := take i, assume Piltn, begin intro P, revert P, rewrite [rotl_perm_pow_eq, -eq_iff_feq, perm_one, *perm.f_mk], intro P, exact absurd P (rotl_seq_ne_id Pex i Piltn) end lemma rotl_perm_order (Pex : ∃ a b : A, a ≠ b) : order (rotl_perm A (succ n) 1) = (succ n) := order_of_min_pow rotl_perm_pow_eq_one (rotl_perm_pow_ne_one Pex) end rotg end group
fddbf538f8b42dd238650df35f4a1af9aa6242aa
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/582.hlean
f540a022ff48d8ebc63aa994e58b47a285217d12
[ "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
173
hlean
open prod variables {A B C : Type} (f : A → B → C) (a : A) (b b' : B) example (p : b = b') : prod.cases_on (a, b) f = f a b' := begin esimp, state, rewrite p end
53bdfbd42c7a16e45722c5dd3e74608f53b10a90
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1389.lean
e93994dc6eb3284c6636f6fd9c180247e6bce4dd
[ "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
181
lean
inductive Bit where | zero | one instance inst0 : OfNat Bit 0 where ofNat := Bit.zero instance : OfNat Bit 1 where ofNat := Bit.one example : Bit := 0 example : Bit := 1
115dd89234925bf6f3361743460deb93f2665004
626e312b5c1cb2d88fca108f5933076012633192
/src/ring_theory/fractional_ideal.lean
34f7fdb119dc378eca6a8df0adafec9911fe382b
[ "Apache-2.0" ]
permissive
Bioye97/mathlib
9db2f9ee54418d29dd06996279ba9dc874fd6beb
782a20a27ee83b523f801ff34efb1a9557085019
refs/heads/master
1,690,305,956,488
1,631,067,774,000
1,631,067,774,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
49,394
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen, Filippo A. E. Nuccio -/ import ring_theory.localization import ring_theory.noetherian import ring_theory.principal_ideal_domain import tactic.field_simp /-! # Fractional ideals This file defines fractional ideals of an integral domain and proves basic facts about them. ## Main definitions Let `S` be a submonoid of an integral domain `R`, `P` the localization of `R` at `S`, and `f` the natural ring hom from `R` to `P`. * `is_fractional` defines which `R`-submodules of `P` are fractional ideals * `fractional_ideal S P` is the type of fractional ideals in `P` * `has_coe_t (ideal R) (fractional_ideal S P)` instance * `comm_semiring (fractional_ideal S P)` instance: the typical ideal operations generalized to fractional ideals * `lattice (fractional_ideal S P)` instance * `map` is the pushforward of a fractional ideal along an algebra morphism Let `K` be the localization of `R` at `R⁰ = R \ {0}` (i.e. the field of fractions). * `fractional_ideal R⁰ K` is the type of fractional ideals in the field of fractions * `has_div (fractional_ideal R⁰ K)` instance: the ideal quotient `I / J` (typically written $I : J$, but a `:` operator cannot be defined) ## Main statements * `mul_left_mono` and `mul_right_mono` state that ideal multiplication is monotone * `prod_one_self_div_eq` states that `1 / I` is the inverse of `I` if one exists * `is_noetherian` states that very fractional ideal of a noetherian integral domain is noetherian ## Implementation notes Fractional ideals are considered equal when they contain the same elements, independent of the denominator `a : R` such that `a I ⊆ R`. Thus, we define `fractional_ideal` to be the subtype of the predicate `is_fractional`, instead of having `fractional_ideal` be a structure of which `a` is a field. Most definitions in this file specialize operations from submodules to fractional ideals, proving that the result of this operation is fractional if the input is fractional. Exceptions to this rule are defining `(+) := (⊔)` and `⊥ := 0`, in order to re-use their respective proof terms. We can still use `simp` to show `↑I + ↑J = ↑(I + J)` and `↑⊥ = ↑0`. Many results in fact do not need that `P` is a localization, only that `P` is an `R`-algebra. We omit the `is_localization` parameter whenever this is practical. Similarly, we don't assume that the localization is a field until we need it to define ideal quotients. When this assumption is needed, we replace `S` with `R⁰`, making the localization a field. ## References * https://en.wikipedia.org/wiki/Fractional_ideal ## Tags fractional ideal, fractional ideals, invertible ideal -/ open is_localization open_locale pointwise open_locale non_zero_divisors section defs variables {R : Type*} [comm_ring R] {S : submonoid R} {P : Type*} [comm_ring P] variables [algebra R P] variables (S) /-- A submodule `I` is a fractional ideal if `a I ⊆ R` for some `a ≠ 0`. -/ def is_fractional (I : submodule R P) := ∃ a ∈ S, ∀ b ∈ I, is_integer R (a • b) variables (S P) /-- The fractional ideals of a domain `R` are ideals of `R` divided by some `a ∈ R`. More precisely, let `P` be a localization of `R` at some submonoid `S`, then a fractional ideal `I ⊆ P` is an `R`-submodule of `P`, such that there is a nonzero `a : R` with `a I ⊆ R`. -/ def fractional_ideal := {I : submodule R P // is_fractional S I} end defs namespace fractional_ideal open set open submodule variables {R : Type*} [comm_ring R] {S : submonoid R} {P : Type*} [comm_ring P] variables [algebra R P] [loc : is_localization S P] /-- Map a fractional ideal `I` to a submodule by forgetting that `∃ a, a I ⊆ R`. This coercion is typically called `coe_to_submodule` in lemma names (or `coe` when the coercion is clear from the context), not to be confused with `is_localization.coe_submodule : ideal R → submodule R P` (which we use to define `coe : ideal R → fractional_ideal S P`, referred to as `coe_ideal` in theorem names). -/ instance : has_coe (fractional_ideal S P) (submodule R P) := ⟨λ I, I.val⟩ protected lemma is_fractional (I : fractional_ideal S P) : is_fractional S (I : submodule R P) := I.prop section set_like instance : set_like (fractional_ideal S P) P := { coe := λ I, ↑(I : submodule R P), coe_injective' := set_like.coe_injective.comp subtype.coe_injective } @[simp] lemma mem_coe {I : fractional_ideal S P} {x : P} : x ∈ (I : submodule R P) ↔ x ∈ I := iff.rfl @[ext] lemma ext {I J : fractional_ideal S P} : (∀ x, x ∈ I ↔ x ∈ J) → I = J := set_like.ext /-- Copy of a `fractional_ideal` with a new underlying set equal to the old one. Useful to fix definitional equalities. -/ protected def copy (p : fractional_ideal S P) (s : set P) (hs : s = ↑p) : fractional_ideal S P := ⟨submodule.copy p s hs, by { convert p.is_fractional, ext, simp only [hs], refl }⟩ end set_like @[simp] lemma val_eq_coe (I : fractional_ideal S P) : I.val = I := rfl @[simp, norm_cast] lemma coe_mk (I : submodule R P) (hI : is_fractional S I) : (subtype.mk I hI : submodule R P) = I := rfl lemma coe_to_submodule_injective : function.injective (coe : fractional_ideal S P → submodule R P) := subtype.coe_injective lemma is_fractional_of_le_one (I : submodule R P) (h : I ≤ 1) : is_fractional S I := begin use [1, S.one_mem], intros b hb, rw one_smul, obtain ⟨b', b'_mem, rfl⟩ := h hb, exact set.mem_range_self b', end lemma is_fractional_of_le {I : submodule R P} {J : fractional_ideal S P} (hIJ : I ≤ J) : is_fractional S I := begin obtain ⟨a, a_mem, ha⟩ := J.is_fractional, use [a, a_mem], intros b b_mem, exact ha b (hIJ b_mem) end /-- Map an ideal `I` to a fractional ideal by forgetting `I` is integral. This is a bundled version of `is_localization.coe_submodule : ideal R → submodule R P`, which is not to be confused with the `coe : fractional_ideal S P → submodule R P`, also called `coe_to_submodule` in theorem names. This map is available as a ring hom, called `fractional_ideal.coe_ideal_hom`. -/ -- Is a `coe_t` rather than `coe` to speed up failing inference, see library note [use has_coe_t] instance coe_to_fractional_ideal : has_coe_t (ideal R) (fractional_ideal S P) := ⟨λ I, ⟨coe_submodule P I, is_fractional_of_le_one _ (by simpa using coe_submodule_mono P (le_top : I ≤ ⊤))⟩⟩ @[simp, norm_cast] lemma coe_coe_ideal (I : ideal R) : ((I : fractional_ideal S P) : submodule R P) = coe_submodule P I := rfl variables (S) @[simp] lemma mem_coe_ideal {x : P} {I : ideal R} : x ∈ (I : fractional_ideal S P) ↔ ∃ x', x' ∈ I ∧ algebra_map R P x' = x := mem_coe_submodule _ _ lemma mem_coe_ideal_of_mem {x : R} {I : ideal R} (hx : x ∈ I) : algebra_map R P x ∈ (I : fractional_ideal S P) := (mem_coe_ideal S).mpr ⟨x, hx, rfl⟩ lemma coe_ideal_le_coe_ideal' [is_localization S P] (h : S ≤ non_zero_divisors R) {I J : ideal R} : (I : fractional_ideal S P) ≤ J ↔ I ≤ J := coe_submodule_le_coe_submodule h @[simp] lemma coe_ideal_le_coe_ideal (K : Type*) [comm_ring K] [algebra R K] [is_fraction_ring R K] {I J : ideal R} : (I : fractional_ideal R⁰ K) ≤ J ↔ I ≤ J := is_fraction_ring.coe_submodule_le_coe_submodule instance : has_zero (fractional_ideal S P) := ⟨(0 : ideal R)⟩ @[simp] lemma mem_zero_iff {x : P} : x ∈ (0 : fractional_ideal S P) ↔ x = 0 := ⟨(λ ⟨x', x'_mem_zero, x'_eq_x⟩, have x'_eq_zero : x' = 0 := x'_mem_zero, by simp [x'_eq_x.symm, x'_eq_zero]), (λ hx, ⟨0, rfl, by simp [hx]⟩)⟩ variables {S} @[simp, norm_cast] lemma coe_zero : ↑(0 : fractional_ideal S P) = (⊥ : submodule R P) := submodule.ext $ λ _, mem_zero_iff S @[simp, norm_cast] lemma coe_to_fractional_ideal_bot : ((⊥ : ideal R) : fractional_ideal S P) = 0 := rfl variables (P) include loc @[simp] lemma exists_mem_to_map_eq {x : R} {I : ideal R} (h : S ≤ non_zero_divisors R) : (∃ x', x' ∈ I ∧ algebra_map R P x' = algebra_map R P x) ↔ x ∈ I := ⟨λ ⟨x', hx', eq⟩, is_localization.injective _ h eq ▸ hx', λ h, ⟨x, h, rfl⟩⟩ variables {P} lemma coe_to_fractional_ideal_injective (h : S ≤ non_zero_divisors R) : function.injective (coe : ideal R → fractional_ideal S P) := λ I J heq, have ∀ (x : R), algebra_map R P x ∈ (I : fractional_ideal S P) ↔ algebra_map R P x ∈ (J : fractional_ideal S P) := λ x, heq ▸ iff.rfl, ideal.ext (by simpa only [mem_coe_ideal, exists_prop, exists_mem_to_map_eq P h] using this) lemma coe_to_fractional_ideal_eq_zero {I : ideal R} (hS : S ≤ non_zero_divisors R) : (I : fractional_ideal S P) = 0 ↔ I = (⊥ : ideal R) := ⟨λ h, coe_to_fractional_ideal_injective hS h, λ h, by rw [h, coe_to_fractional_ideal_bot]⟩ lemma coe_to_fractional_ideal_ne_zero {I : ideal R} (hS : S ≤ non_zero_divisors R) : (I : fractional_ideal S P) ≠ 0 ↔ I ≠ (⊥ : ideal R) := not_iff_not.mpr (coe_to_fractional_ideal_eq_zero hS) omit loc lemma coe_to_submodule_eq_bot {I : fractional_ideal S P} : (I : submodule R P) = ⊥ ↔ I = 0 := ⟨λ h, coe_to_submodule_injective (by simp [h]), λ h, by simp [h]⟩ lemma coe_to_submodule_ne_bot {I : fractional_ideal S P} : ↑I ≠ (⊥ : submodule R P) ↔ I ≠ 0 := not_iff_not.mpr coe_to_submodule_eq_bot instance : inhabited (fractional_ideal S P) := ⟨0⟩ instance : has_one (fractional_ideal S P) := ⟨(⊤ : ideal R)⟩ variables (S) @[simp, norm_cast] lemma coe_ideal_top : ((⊤ : ideal R) : fractional_ideal S P) = 1 := rfl lemma mem_one_iff {x : P} : x ∈ (1 : fractional_ideal S P) ↔ ∃ x' : R, algebra_map R P x' = x := iff.intro (λ ⟨x', _, h⟩, ⟨x', h⟩) (λ ⟨x', h⟩, ⟨x', ⟨⟩, h⟩) lemma coe_mem_one (x : R) : algebra_map R P x ∈ (1 : fractional_ideal S P) := (mem_one_iff S).mpr ⟨x, rfl⟩ lemma one_mem_one : (1 : P) ∈ (1 : fractional_ideal S P) := (mem_one_iff S).mpr ⟨1, ring_hom.map_one _⟩ variables {S} /-- `(1 : fractional_ideal S P)` is defined as the R-submodule `f(R) ≤ P`. However, this is not definitionally equal to `1 : submodule R P`, which is proved in the actual `simp` lemma `coe_one`. -/ lemma coe_one_eq_coe_submodule_top : ↑(1 : fractional_ideal S P) = coe_submodule P (⊤ : ideal R) := rfl @[simp, norm_cast] lemma coe_one : (↑(1 : fractional_ideal S P) : submodule R P) = 1 := by rw [coe_one_eq_coe_submodule_top, coe_submodule_top] section lattice /-! ### `lattice` section Defines the order on fractional ideals as inclusion of their underlying sets, and ports the lattice structure on submodules to fractional ideals. -/ @[simp] lemma coe_le_coe {I J : fractional_ideal S P} : (I : submodule R P) ≤ (J : submodule R P) ↔ I ≤ J := iff.rfl lemma zero_le (I : fractional_ideal S P) : 0 ≤ I := begin intros x hx, convert submodule.zero_mem _, simpa using hx end instance order_bot : order_bot (fractional_ideal S P) := { bot := 0, bot_le := zero_le, ..set_like.partial_order } @[simp] lemma bot_eq_zero : (⊥ : fractional_ideal S P) = 0 := rfl @[simp] lemma le_zero_iff {I : fractional_ideal S P} : I ≤ 0 ↔ I = 0 := le_bot_iff lemma eq_zero_iff {I : fractional_ideal S P} : I = 0 ↔ (∀ x ∈ I, x = (0 : P)) := ⟨ (λ h x hx, by simpa [h, mem_zero_iff] using hx), (λ h, le_bot_iff.mp (λ x hx, (mem_zero_iff S).mpr (h x hx))) ⟩ lemma fractional_sup (I J : fractional_ideal S P) : is_fractional S (I ⊔ J : submodule R P) := begin rcases I.is_fractional with ⟨aI, haI, hI⟩, rcases J.is_fractional with ⟨aJ, haJ, hJ⟩, use aI * aJ, use S.mul_mem haI haJ, intros b hb, rcases mem_sup.mp hb with ⟨bI, hbI, bJ, hbJ, rfl⟩, rw smul_add, apply is_integer_add, { rw [mul_smul, smul_comm], exact is_integer_smul (hI bI hbI), }, { rw mul_smul, exact is_integer_smul (hJ bJ hbJ) } end lemma fractional_inf (I J : fractional_ideal S P) : is_fractional S (I ⊓ J : submodule R P) := begin rcases I.is_fractional with ⟨aI, haI, hI⟩, use aI, use haI, intros b hb, rcases mem_inf.mp hb with ⟨hbI, hbJ⟩, exact hI b hbI end instance lattice : lattice (fractional_ideal S P) := { inf := λ I J, ⟨I ⊓ J, fractional_inf I J⟩, sup := λ I J, ⟨I ⊔ J, fractional_sup I J⟩, inf_le_left := λ I J, show (I ⊓ J : submodule R P) ≤ I, from inf_le_left, inf_le_right := λ I J, show (I ⊓ J : submodule R P) ≤ J, from inf_le_right, le_inf := λ I J K hIJ hIK, show (I : submodule R P) ≤ J ⊓ K, from le_inf hIJ hIK, le_sup_left := λ I J, show (I : submodule R P) ≤ I ⊔ J, from le_sup_left, le_sup_right := λ I J, show (J : submodule R P) ≤ I ⊔ J, from le_sup_right, sup_le := λ I J K hIK hJK, show (I ⊔ J : submodule R P) ≤ K, from sup_le hIK hJK, ..set_like.partial_order } instance : semilattice_sup_bot (fractional_ideal S P) := { ..fractional_ideal.order_bot, ..fractional_ideal.lattice } end lattice section semiring instance : has_add (fractional_ideal S P) := ⟨(⊔)⟩ @[simp] lemma sup_eq_add (I J : fractional_ideal S P) : I ⊔ J = I + J := rfl @[simp, norm_cast] lemma coe_add (I J : fractional_ideal S P) : (↑(I + J) : submodule R P) = I + J := rfl @[simp, norm_cast] lemma coe_ideal_sup (I J : ideal R) : ↑(I ⊔ J) = (I + J : fractional_ideal S P) := coe_to_submodule_injective $ coe_submodule_sup _ _ _ lemma fractional_mul (I J : fractional_ideal S P) : is_fractional S (I * J : submodule R P) := begin rcases I with ⟨I, aI, haI, hI⟩, rcases J with ⟨J, aJ, haJ, hJ⟩, use aI * aJ, use S.mul_mem haI haJ, intros b hb, apply submodule.mul_induction_on hb, { intros m hm n hn, obtain ⟨n', hn'⟩ := hJ n hn, rw [mul_smul, mul_comm m, ← smul_mul_assoc, ← hn', ← algebra.smul_def], apply hI, exact submodule.smul_mem _ _ hm }, { rw smul_zero, exact ⟨0, ring_hom.map_zero _⟩ }, { intros x y hx hy, rw smul_add, apply is_integer_add hx hy }, { intros r x hx, rw smul_comm, exact is_integer_smul hx }, end /-- `fractional_ideal.mul` is the product of two fractional ideals, used to define the `has_mul` instance. This is only an auxiliary definition: the preferred way of writing `I.mul J` is `I * J`. Elaborated terms involving `fractional_ideal` tend to grow quite large, so by making definitions irreducible, we hope to avoid deep unfolds. -/ @[irreducible] def mul (I J : fractional_ideal S P) : fractional_ideal S P := ⟨I * J, fractional_mul I J⟩ local attribute [semireducible] mul instance : has_mul (fractional_ideal S P) := ⟨λ I J, mul I J⟩ @[simp] lemma mul_eq_mul (I J : fractional_ideal S P) : mul I J = I * J := rfl @[simp, norm_cast] lemma coe_mul (I J : fractional_ideal S P) : (↑(I * J) : submodule R P) = I * J := rfl @[simp, norm_cast] lemma coe_ideal_mul (I J : ideal R) : (↑(I * J) : fractional_ideal S P) = I * J := coe_to_submodule_injective $ coe_submodule_mul _ _ _ lemma mul_left_mono (I : fractional_ideal S P) : monotone ((*) I) := λ J J' h, mul_le.mpr (λ x hx y hy, mul_mem_mul hx (h hy)) lemma mul_right_mono (I : fractional_ideal S P) : monotone (λ J, J * I) := λ J J' h, mul_le.mpr (λ x hx y hy, mul_mem_mul (h hx) hy) lemma mul_mem_mul {I J : fractional_ideal S P} {i j : P} (hi : i ∈ I) (hj : j ∈ J) : i * j ∈ I * J := submodule.mul_mem_mul hi hj lemma mul_le {I J K : fractional_ideal S P} : I * J ≤ K ↔ (∀ (i ∈ I) (j ∈ J), i * j ∈ K) := submodule.mul_le @[elab_as_eliminator] protected theorem mul_induction_on {I J : fractional_ideal S P} {C : P → Prop} {r : P} (hr : r ∈ I * J) (hm : ∀ (i ∈ I) (j ∈ J), C (i * j)) (h0 : C 0) (ha : ∀ x y, C x → C y → C (x + y)) (hs : ∀ (r : R) x, C x → C (r • x)) : C r := submodule.mul_induction_on hr hm h0 ha hs instance comm_semiring : comm_semiring (fractional_ideal S P) := { add_assoc := λ I J K, sup_assoc, add_comm := λ I J, sup_comm, add_zero := λ I, sup_bot_eq, zero_add := λ I, bot_sup_eq, mul_assoc := λ I J K, coe_to_submodule_injective (submodule.mul_assoc _ _ _), mul_comm := λ I J, coe_to_submodule_injective (submodule.mul_comm _ _), mul_one := λ I, begin ext, split; intro h, { apply mul_le.mpr _ h, rintros x hx y ⟨y', y'_mem_R, rfl⟩, convert submodule.smul_mem _ y' hx, rw [mul_comm, eq_comm], exact algebra.smul_def y' x }, { have : x * 1 ∈ (I * 1) := mul_mem_mul h (one_mem_one _), rwa [mul_one] at this } end, one_mul := λ I, begin ext, split; intro h, { apply mul_le.mpr _ h, rintros x ⟨x', x'_mem_R, rfl⟩ y hy, convert submodule.smul_mem _ x' hy, rw eq_comm, exact algebra.smul_def x' y }, { have : 1 * x ∈ (1 * I) := mul_mem_mul (one_mem_one _) h, rwa one_mul at this } end, mul_zero := λ I, eq_zero_iff.mpr (λ x hx, submodule.mul_induction_on hx (λ x hx y hy, by simp [(mem_zero_iff S).mp hy]) rfl (λ x y hx hy, by simp [hx, hy]) (λ r x hx, by simp [hx])), zero_mul := λ I, eq_zero_iff.mpr (λ x hx, submodule.mul_induction_on hx (λ x hx y hy, by simp [(mem_zero_iff S).mp hx]) rfl (λ x y hx hy, by simp [hx, hy]) (λ r x hx, by simp [hx])), left_distrib := λ I J K, coe_to_submodule_injective (mul_add _ _ _), right_distrib := λ I J K, coe_to_submodule_injective (add_mul _ _ _), ..fractional_ideal.has_zero S, ..fractional_ideal.has_add, ..fractional_ideal.has_one, ..fractional_ideal.has_mul } section order lemma add_le_add_left {I J : fractional_ideal S P} (hIJ : I ≤ J) (J' : fractional_ideal S P) : J' + I ≤ J' + J := sup_le_sup_left hIJ J' lemma mul_le_mul_left {I J : fractional_ideal S P} (hIJ : I ≤ J) (J' : fractional_ideal S P) : J' * I ≤ J' * J := mul_le.mpr (λ k hk j hj, mul_mem_mul hk (hIJ hj)) lemma le_self_mul_self {I : fractional_ideal S P} (hI: 1 ≤ I) : I ≤ I * I := begin convert mul_left_mono I hI, exact (mul_one I).symm end lemma mul_self_le_self {I : fractional_ideal S P} (hI: I ≤ 1) : I * I ≤ I := begin convert mul_left_mono I hI, exact (mul_one I).symm end lemma coe_ideal_le_one {I : ideal R} : (I : fractional_ideal S P) ≤ 1 := λ x hx, let ⟨y, _, hy⟩ := (fractional_ideal.mem_coe_ideal S).mp hx in (fractional_ideal.mem_one_iff S).mpr ⟨y, hy⟩ lemma le_one_iff_exists_coe_ideal {J : fractional_ideal S P} : J ≤ (1 : fractional_ideal S P) ↔ ∃ (I : ideal R), ↑I = J := begin split, { intro hJ, refine ⟨⟨{x : R | algebra_map R P x ∈ J}, _, _, _⟩, _⟩, { rw [mem_set_of_eq, ring_hom.map_zero], exact J.val.zero_mem }, { intros a b ha hb, rw [mem_set_of_eq, ring_hom.map_add], exact J.val.add_mem ha hb }, { intros c x hx, rw [smul_eq_mul, mem_set_of_eq, ring_hom.map_mul, ← algebra.smul_def], exact J.val.smul_mem c hx }, { ext x, split, { rintros ⟨y, hy, eq_y⟩, rwa ← eq_y }, { intro hx, obtain ⟨y, eq_x⟩ := (fractional_ideal.mem_one_iff S).mp (hJ hx), rw ← eq_x at *, exact ⟨y, hx, rfl⟩ } } }, { rintro ⟨I, hI⟩, rw ← hI, apply coe_ideal_le_one }, end variables (S P) /-- `coe_ideal_hom (S : submonoid R) P` is `coe : ideal R → fractional_ideal S P` as a ring hom -/ @[simps] def coe_ideal_hom : ideal R →+* fractional_ideal S P := { to_fun := coe, map_add' := coe_ideal_sup, map_mul' := coe_ideal_mul, map_one' := by rw [ideal.one_eq_top, coe_ideal_top], map_zero' := coe_to_fractional_ideal_bot } end order variables {P' : Type*} [comm_ring P'] [algebra R P'] [loc' : is_localization S P'] variables {P'' : Type*} [comm_ring P''] [algebra R P''] [loc'' : is_localization S P''] lemma fractional_map (g : P →ₐ[R] P') (I : fractional_ideal S P) : is_fractional S (submodule.map g.to_linear_map I) := begin rcases I with ⟨I, a, a_nonzero, hI⟩, use [a, a_nonzero], intros b hb, obtain ⟨b', b'_mem, hb'⟩ := submodule.mem_map.mp hb, obtain ⟨x, hx⟩ := hI b' b'_mem, use x, erw [←g.commutes, hx, g.map_smul, hb'] end /-- `I.map g` is the pushforward of the fractional ideal `I` along the algebra morphism `g` -/ def map (g : P →ₐ[R] P') : fractional_ideal S P → fractional_ideal S P' := λ I, ⟨submodule.map g.to_linear_map I, fractional_map g I⟩ @[simp, norm_cast] lemma coe_map (g : P →ₐ[R] P') (I : fractional_ideal S P) : ↑(map g I) = submodule.map g.to_linear_map I := rfl @[simp] lemma mem_map {I : fractional_ideal S P} {g : P →ₐ[R] P'} {y : P'} : y ∈ I.map g ↔ ∃ x, x ∈ I ∧ g x = y := submodule.mem_map variables (I J : fractional_ideal S P) (g : P →ₐ[R] P') @[simp] lemma map_id : I.map (alg_hom.id _ _) = I := coe_to_submodule_injective (submodule.map_id I) @[simp] lemma map_comp (g' : P' →ₐ[R] P'') : I.map (g'.comp g) = (I.map g).map g' := coe_to_submodule_injective (submodule.map_comp g.to_linear_map g'.to_linear_map I) @[simp, norm_cast] lemma map_coe_ideal (I : ideal R) : (I : fractional_ideal S P).map g = I := begin ext x, simp only [mem_coe_ideal], split, { rintro ⟨_, ⟨y, hy, rfl⟩, rfl⟩, exact ⟨y, hy, (g.commutes y).symm⟩ }, { rintro ⟨y, hy, rfl⟩, exact ⟨_, ⟨y, hy, rfl⟩, g.commutes y⟩ }, end @[simp] lemma map_one : (1 : fractional_ideal S P).map g = 1 := map_coe_ideal g ⊤ @[simp] lemma map_zero : (0 : fractional_ideal S P).map g = 0 := map_coe_ideal g 0 @[simp] lemma map_add : (I + J).map g = I.map g + J.map g := coe_to_submodule_injective (submodule.map_sup _ _ _) @[simp] lemma map_mul : (I * J).map g = I.map g * J.map g := coe_to_submodule_injective (submodule.map_mul _ _ _) @[simp] lemma map_map_symm (g : P ≃ₐ[R] P') : (I.map (g : P →ₐ[R] P')).map (g.symm : P' →ₐ[R] P) = I := by rw [←map_comp, g.symm_comp, map_id] @[simp] lemma map_symm_map (I : fractional_ideal S P') (g : P ≃ₐ[R] P') : (I.map (g.symm : P' →ₐ[R] P)).map (g : P →ₐ[R] P') = I := by rw [←map_comp, g.comp_symm, map_id] /-- If `g` is an equivalence, `map g` is an isomorphism -/ def map_equiv (g : P ≃ₐ[R] P') : fractional_ideal S P ≃+* fractional_ideal S P' := { to_fun := map g, inv_fun := map g.symm, map_add' := λ I J, map_add I J _, map_mul' := λ I J, map_mul I J _, left_inv := λ I, by { rw [←map_comp, alg_equiv.symm_comp, map_id] }, right_inv := λ I, by { rw [←map_comp, alg_equiv.comp_symm, map_id] } } @[simp] lemma coe_fun_map_equiv (g : P ≃ₐ[R] P') : (map_equiv g : fractional_ideal S P → fractional_ideal S P') = map g := rfl @[simp] lemma map_equiv_apply (g : P ≃ₐ[R] P') (I : fractional_ideal S P) : map_equiv g I = map ↑g I := rfl @[simp] lemma map_equiv_symm (g : P ≃ₐ[R] P') : ((map_equiv g).symm : fractional_ideal S P' ≃+* _) = map_equiv g.symm := rfl @[simp] lemma map_equiv_refl : map_equiv alg_equiv.refl = ring_equiv.refl (fractional_ideal S P) := ring_equiv.ext (λ x, by simp) lemma is_fractional_span_iff {s : set P} : is_fractional S (span R s) ↔ ∃ a ∈ S, ∀ (b : P), b ∈ s → is_integer R (a • b) := ⟨λ ⟨a, a_mem, h⟩, ⟨a, a_mem, λ b hb, h b (subset_span hb)⟩, λ ⟨a, a_mem, h⟩, ⟨a, a_mem, λ b hb, span_induction hb h (by { rw smul_zero, exact is_integer_zero }) (λ x y hx hy, by { rw smul_add, exact is_integer_add hx hy }) (λ s x hx, by { rw smul_comm, exact is_integer_smul hx })⟩⟩ include loc lemma is_fractional_of_fg {I : submodule R P} (hI : I.fg) : is_fractional S I := begin rcases hI with ⟨I, rfl⟩, rcases exist_integer_multiples_of_finset S I with ⟨⟨s, hs1⟩, hs⟩, rw is_fractional_span_iff, exact ⟨s, hs1, hs⟩, end omit loc lemma mem_span_mul_finite_of_mem_mul {I J : fractional_ideal S P} {x : P} (hx : x ∈ I * J) : ∃ (T T' : finset P), (T : set P) ⊆ I ∧ (T' : set P) ⊆ J ∧ x ∈ span R (T * T' : set P) := submodule.mem_span_mul_finite_of_mem_mul (by simpa using mem_coe.mpr hx) variables (S) lemma coe_ideal_fg (inj : function.injective (algebra_map R P)) (I : ideal R) : fg ((I : fractional_ideal S P) : submodule R P) ↔ fg I := coe_submodule_fg _ inj _ variables {S} lemma fg_unit (I : units (fractional_ideal S P)) : fg (I : submodule R P) := begin have : (1 : P) ∈ (I * ↑I⁻¹ : fractional_ideal S P), { rw units.mul_inv, exact one_mem_one _ }, obtain ⟨T, T', hT, hT', one_mem⟩ := mem_span_mul_finite_of_mem_mul this, refine ⟨T, submodule.span_eq_of_le _ hT _⟩, rw [← one_mul ↑I, ← mul_one (span R ↑T)], conv_rhs { rw [← fractional_ideal.coe_one, ← units.mul_inv I, fractional_ideal.coe_mul, mul_comm ↑↑I, ← mul_assoc] }, refine submodule.mul_le_mul_left (le_trans _ (submodule.mul_le_mul_right (submodule.span_le.mpr hT'))), rwa [submodule.one_le, submodule.span_mul_span] end lemma fg_of_is_unit (I : fractional_ideal S P) (h : is_unit I) : fg (I : submodule R P) := by { rcases h with ⟨I, rfl⟩, exact fg_unit I } lemma _root_.ideal.fg_of_is_unit (inj : function.injective (algebra_map R P)) (I : ideal R) (h : is_unit (I : fractional_ideal S P)) : I.fg := by { rw ← coe_ideal_fg S inj I, exact fg_of_is_unit I h } variables (S P P') include loc loc' /-- `canonical_equiv f f'` is the canonical equivalence between the fractional ideals in `P` and in `P'` -/ @[irreducible] noncomputable def canonical_equiv : fractional_ideal S P ≃+* fractional_ideal S P' := map_equiv { commutes' := λ r, ring_equiv_of_ring_equiv_eq _ _, ..ring_equiv_of_ring_equiv P P' (ring_equiv.refl R) (show S.map _ = S, by rw [ring_equiv.to_monoid_hom_refl, submonoid.map_id]) } @[simp] lemma mem_canonical_equiv_apply {I : fractional_ideal S P} {x : P'} : x ∈ canonical_equiv S P P' I ↔ ∃ y ∈ I, is_localization.map P' (ring_hom.id R) (λ y (hy : y ∈ S), show ring_hom.id R y ∈ S, from hy) (y : P) = x := begin rw [canonical_equiv, map_equiv_apply, mem_map], exact ⟨λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩, λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩⟩ end @[simp] lemma canonical_equiv_symm : (canonical_equiv S P P').symm = canonical_equiv S P' P := ring_equiv.ext $ λ I, set_like.ext_iff.mpr $ λ x, by { rw [mem_canonical_equiv_apply, canonical_equiv, map_equiv_symm, map_equiv, ring_equiv.coe_mk, mem_map], exact ⟨λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩, λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩⟩ } @[simp] lemma canonical_equiv_flip (I) : canonical_equiv S P P' (canonical_equiv S P' P I) = I := by rw [←canonical_equiv_symm, ring_equiv.symm_apply_apply] end semiring section is_fraction_ring /-! ### `is_fraction_ring` section This section concerns fractional ideals in the field of fractions, i.e. the type `fractional_ideal R⁰ K` where `is_fraction_ring R K`. -/ variables {K K' : Type*} [field K] [field K'] variables [algebra R K] [is_fraction_ring R K] [algebra R K'] [is_fraction_ring R K'] variables {I J : fractional_ideal R⁰ K} (h : K →ₐ[R] K') /-- Nonzero fractional ideals contain a nonzero integer. -/ lemma exists_ne_zero_mem_is_integer [nontrivial R] (hI : I ≠ 0) : ∃ x ≠ (0 : R), algebra_map R K x ∈ I := begin obtain ⟨y, y_mem, y_not_mem⟩ := set_like.exists_of_lt (bot_lt_iff_ne_bot.mpr hI), have y_ne_zero : y ≠ 0 := by simpa using y_not_mem, obtain ⟨z, ⟨x, hx⟩⟩ := exists_integer_multiple R⁰ y, refine ⟨x, _, _⟩, { rw [ne.def, ← @is_fraction_ring.to_map_eq_zero_iff R _ K, hx, algebra.smul_def], exact mul_ne_zero (is_fraction_ring.to_map_ne_zero_of_mem_non_zero_divisors z.2) y_ne_zero }, { rw hx, exact smul_mem _ _ y_mem } end lemma map_ne_zero [nontrivial R] (hI : I ≠ 0) : I.map h ≠ 0 := begin obtain ⟨x, x_ne_zero, hx⟩ := exists_ne_zero_mem_is_integer hI, contrapose! x_ne_zero with map_eq_zero, refine is_fraction_ring.to_map_eq_zero_iff.mp (eq_zero_iff.mp map_eq_zero _ (mem_map.mpr _)), exact ⟨algebra_map R K x, hx, h.commutes x⟩, end @[simp] lemma map_eq_zero_iff [nontrivial R] : I.map h = 0 ↔ I = 0 := ⟨imp_of_not_imp_not _ _ (map_ne_zero _), λ hI, hI.symm ▸ map_zero h⟩ lemma coe_ideal_injective : function.injective (coe : ideal R → fractional_ideal R⁰ K) := injective_of_le_imp_le _ (λ _ _, (coe_ideal_le_coe_ideal _).mp) @[simp] lemma coe_ideal_eq_zero_iff {I : ideal R} : (I : fractional_ideal R⁰ K) = 0 ↔ I = ⊥ := by { rw ← coe_to_fractional_ideal_bot, exact coe_ideal_injective.eq_iff } lemma coe_ideal_ne_zero_iff {I : ideal R} : (I : fractional_ideal R⁰ K) ≠ 0 ↔ I ≠ ⊥ := not_iff_not.mpr coe_ideal_eq_zero_iff lemma coe_ideal_ne_zero {I : ideal R} (hI : I ≠ ⊥) : (I : fractional_ideal R⁰ K) ≠ 0 := coe_ideal_ne_zero_iff.mpr hI end is_fraction_ring section quotient /-! ### `quotient` section This section defines the ideal quotient of fractional ideals. In this section we need that each non-zero `y : R` has an inverse in the localization, i.e. that the localization is a field. We satisfy this assumption by taking `S = non_zero_divisors R`, `R`'s localization at which is a field because `R` is a domain. -/ open_locale classical variables {R₁ : Type*} [integral_domain R₁] {K : Type*} [field K] variables [algebra R₁ K] [frac : is_fraction_ring R₁ K] instance : nontrivial (fractional_ideal R₁⁰ K) := ⟨⟨0, 1, λ h, have this : (1 : K) ∈ (0 : fractional_ideal R₁⁰ K) := by { rw ← (algebra_map R₁ K).map_one, simpa only [h] using coe_mem_one R₁⁰ 1 }, one_ne_zero ((mem_zero_iff _).mp this)⟩⟩ include frac lemma fractional_div_of_nonzero {I J : fractional_ideal R₁⁰ K} (h : J ≠ 0) : is_fractional R₁⁰ (I / J : submodule R₁ K) := begin rcases I with ⟨I, aI, haI, hI⟩, rcases J with ⟨J, aJ, haJ, hJ⟩, obtain ⟨y, mem_J, not_mem_zero⟩ := set_like.exists_of_lt (bot_lt_iff_ne_bot.mpr h), obtain ⟨y', hy'⟩ := hJ y mem_J, use (aI * y'), split, { apply (non_zero_divisors R₁).mul_mem haI (mem_non_zero_divisors_iff_ne_zero.mpr _), intro y'_eq_zero, have : algebra_map R₁ K aJ * y = 0, { rw [← algebra.smul_def, ←hy', y'_eq_zero, ring_hom.map_zero] }, have y_zero := (mul_eq_zero.mp this).resolve_left (mt ((algebra_map R₁ K).injective_iff.1 (is_fraction_ring.injective _ _) _) (mem_non_zero_divisors_iff_ne_zero.mp haJ)), exact not_mem_zero ((mem_zero_iff R₁⁰).mpr y_zero) }, intros b hb, convert hI _ (hb _ (submodule.smul_mem _ aJ mem_J)) using 1, rw [← hy', mul_comm b, ← algebra.smul_def, mul_smul] end noncomputable instance fractional_ideal_has_div : has_div (fractional_ideal R₁⁰ K) := ⟨ λ I J, if h : J = 0 then 0 else ⟨I / J, fractional_div_of_nonzero h⟩ ⟩ variables {I J : fractional_ideal R₁⁰ K} [ J ≠ 0 ] @[simp] lemma div_zero {I : fractional_ideal R₁⁰ K} : I / 0 = 0 := dif_pos rfl lemma div_nonzero {I J : fractional_ideal R₁⁰ K} (h : J ≠ 0) : (I / J) = ⟨I / J, fractional_div_of_nonzero h⟩ := dif_neg h @[simp] lemma coe_div {I J : fractional_ideal R₁⁰ K} (hJ : J ≠ 0) : (↑(I / J) : submodule R₁ K) = ↑I / (↑J : submodule R₁ K) := begin unfold has_div.div, simp only [dif_neg hJ, coe_mk, val_eq_coe], end lemma mem_div_iff_of_nonzero {I J : fractional_ideal R₁⁰ K} (h : J ≠ 0) {x} : x ∈ I / J ↔ ∀ y ∈ J, x * y ∈ I := by { rw div_nonzero h, exact submodule.mem_div_iff_forall_mul_mem } lemma mul_one_div_le_one {I : fractional_ideal R₁⁰ K} : I * (1 / I) ≤ 1 := begin by_cases hI : I = 0, { rw [hI, div_zero, mul_zero], exact zero_le 1 }, { rw [← coe_le_coe, coe_mul, coe_div hI, coe_one], apply submodule.mul_one_div_le_one }, end lemma le_self_mul_one_div {I : fractional_ideal R₁⁰ K} (hI : I ≤ (1 : fractional_ideal R₁⁰ K)) : I ≤ I * (1 / I) := begin by_cases hI_nz : I = 0, { rw [hI_nz, div_zero, mul_zero], exact zero_le 0 }, { rw [← coe_le_coe, coe_mul, coe_div hI_nz, coe_one], rw [← coe_le_coe, coe_one] at hI, exact submodule.le_self_mul_one_div hI }, end lemma le_div_iff_of_nonzero {I J J' : fractional_ideal R₁⁰ K} (hJ' : J' ≠ 0) : I ≤ J / J' ↔ ∀ (x ∈ I) (y ∈ J'), x * y ∈ J := ⟨ λ h x hx, (mem_div_iff_of_nonzero hJ').mp (h hx), λ h x hx, (mem_div_iff_of_nonzero hJ').mpr (h x hx) ⟩ lemma le_div_iff_mul_le {I J J' : fractional_ideal R₁⁰ K} (hJ' : J' ≠ 0) : I ≤ J / J' ↔ I * J' ≤ J := begin rw div_nonzero hJ', convert submodule.le_div_iff_mul_le using 1, rw [← coe_mul, coe_le_coe] end @[simp] lemma div_one {I : fractional_ideal R₁⁰ K} : I / 1 = I := begin rw [div_nonzero (@one_ne_zero (fractional_ideal R₁⁰ K) _ _)], ext, split; intro h, { simpa using mem_div_iff_forall_mul_mem.mp h 1 ((algebra_map R₁ K).map_one ▸ coe_mem_one R₁⁰ 1) }, { apply mem_div_iff_forall_mul_mem.mpr, rintros y ⟨y', _, rfl⟩, rw mul_comm, convert submodule.smul_mem _ y' h, exact (algebra.smul_def _ _).symm } end omit frac lemma ne_zero_of_mul_eq_one (I J : fractional_ideal R₁⁰ K) (h : I * J = 1) : I ≠ 0 := λ hI, @zero_ne_one (fractional_ideal R₁⁰ K) _ _ (by { convert h, simp [hI], }) include frac theorem eq_one_div_of_mul_eq_one (I J : fractional_ideal R₁⁰ K) (h : I * J = 1) : J = 1 / I := begin have hI : I ≠ 0 := ne_zero_of_mul_eq_one I J h, suffices h' : I * (1 / I) = 1, { exact (congr_arg units.inv $ @units.ext _ _ (units.mk_of_mul_eq_one _ _ h) (units.mk_of_mul_eq_one _ _ h') rfl) }, apply le_antisymm, { apply mul_le.mpr _, intros x hx y hy, rw mul_comm, exact (mem_div_iff_of_nonzero hI).mp hy x hx }, rw ← h, apply mul_left_mono I, apply (le_div_iff_of_nonzero hI).mpr _, intros y hy x hx, rw mul_comm, exact mul_mem_mul hx hy, end theorem mul_div_self_cancel_iff {I : fractional_ideal R₁⁰ K} : I * (1 / I) = 1 ↔ ∃ J, I * J = 1 := ⟨λ h, ⟨(1 / I), h⟩, λ ⟨J, hJ⟩, by rwa [← eq_one_div_of_mul_eq_one I J hJ]⟩ variables {K' : Type*} [field K'] [algebra R₁ K'] [is_fraction_ring R₁ K'] @[simp] lemma map_div (I J : fractional_ideal R₁⁰ K) (h : K ≃ₐ[R₁] K') : (I / J).map (h : K →ₐ[R₁] K') = I.map h / J.map h := begin by_cases H : J = 0, { rw [H, div_zero, map_zero, div_zero] }, { apply coe_to_submodule_injective, simp [div_nonzero H, div_nonzero (map_ne_zero _ H), submodule.map_div] } end @[simp] lemma map_one_div (I : fractional_ideal R₁⁰ K) (h : K ≃ₐ[R₁] K') : (1 / I).map (h : K →ₐ[R₁] K') = 1 / I.map h := by rw [map_div, map_one] end quotient section field variables {R₁ K L : Type*} [integral_domain R₁] [field K] [field L] variables [algebra R₁ K] [is_fraction_ring R₁ K] [algebra K L] [is_fraction_ring K L] lemma eq_zero_or_one (I : fractional_ideal K⁰ L) : I = 0 ∨ I = 1 := begin rw or_iff_not_imp_left, intro hI, simp_rw [@set_like.ext_iff _ _ _ I 1, fractional_ideal.mem_one_iff], intro x, split, { intro x_mem, obtain ⟨n, d, rfl⟩ := is_localization.mk'_surjective K⁰ x, refine ⟨n / d, _⟩, rw [ring_hom.map_div, is_fraction_ring.mk'_eq_div] }, { rintro ⟨x, rfl⟩, obtain ⟨y, y_ne, y_mem⟩ := fractional_ideal.exists_ne_zero_mem_is_integer hI, rw [← div_mul_cancel x y_ne, ring_hom.map_mul, ← algebra.smul_def], exact submodule.smul_mem I _ y_mem } end lemma eq_zero_or_one_of_is_field (hF : is_field R₁) (I : fractional_ideal R₁⁰ K) : I = 0 ∨ I = 1 := by { letI : field R₁ := hF.to_field R₁, exact eq_zero_or_one I } end field section principal_ideal_ring variables {R₁ : Type*} [integral_domain R₁] {K : Type*} [field K] variables [algebra R₁ K] [is_fraction_ring R₁ K] open_locale classical open submodule submodule.is_principal include loc lemma is_fractional_span_singleton (x : P) : is_fractional S (span R {x} : submodule R P) := let ⟨a, ha⟩ := exists_integer_multiple S x in is_fractional_span_iff.mpr ⟨a, a.2, λ x' hx', (set.mem_singleton_iff.mp hx').symm ▸ ha⟩ variables (S) /-- `span_singleton x` is the fractional ideal generated by `x` if `0 ∉ S` -/ @[irreducible] def span_singleton (x : P) : fractional_ideal S P := ⟨span R {x}, is_fractional_span_singleton x⟩ local attribute [semireducible] span_singleton @[simp] lemma coe_span_singleton (x : P) : (span_singleton S x : submodule R P) = span R {x} := rfl @[simp] lemma mem_span_singleton {x y : P} : x ∈ span_singleton S y ↔ ∃ (z : R), z • y = x := submodule.mem_span_singleton lemma mem_span_singleton_self (x : P) : x ∈ span_singleton S x := (mem_span_singleton S).mpr ⟨1, one_smul _ _⟩ variables {S} lemma eq_span_singleton_of_principal (I : fractional_ideal S P) [is_principal (I : submodule R P)] : I = span_singleton S (generator (I : submodule R P)) := coe_to_submodule_injective (span_singleton_generator ↑I).symm lemma is_principal_iff (I : fractional_ideal S P) : is_principal (I : submodule R P) ↔ ∃ x, I = span_singleton S x := ⟨λ h, ⟨@generator _ _ _ _ _ ↑I h, @eq_span_singleton_of_principal _ _ _ _ _ _ _ I h⟩, λ ⟨x, hx⟩, { principal := ⟨x, trans (congr_arg _ hx) (coe_span_singleton _ x)⟩ } ⟩ @[simp] lemma span_singleton_zero : span_singleton S (0 : P) = 0 := by { ext, simp [submodule.mem_span_singleton, eq_comm] } lemma span_singleton_eq_zero_iff {y : P} : span_singleton S y = 0 ↔ y = 0 := ⟨λ h, span_eq_bot.mp (by simpa using congr_arg subtype.val h : span R {y} = ⊥) y (mem_singleton y), λ h, by simp [h] ⟩ lemma span_singleton_ne_zero_iff {y : P} : span_singleton S y ≠ 0 ↔ y ≠ 0 := not_congr span_singleton_eq_zero_iff @[simp] lemma span_singleton_one : span_singleton S (1 : P) = 1 := begin ext, refine (mem_span_singleton S).trans ((exists_congr _).trans (mem_one_iff S).symm), intro x', rw [algebra.smul_def, mul_one] end @[simp] lemma span_singleton_mul_span_singleton (x y : P) : span_singleton S x * span_singleton S y = span_singleton S (x * y) := begin apply coe_to_submodule_injective, simp only [coe_mul, coe_span_singleton, span_mul_span, singleton_mul_singleton], end @[simp] lemma coe_ideal_span_singleton (x : R) : (↑(span R {x} : ideal R) : fractional_ideal S P) = span_singleton S (algebra_map R P x) := begin ext y, refine (mem_coe_ideal S).trans (iff.trans _ (mem_span_singleton S).symm), split, { rintros ⟨y', hy', rfl⟩, obtain ⟨x', rfl⟩ := submodule.mem_span_singleton.mp hy', use x', rw [smul_eq_mul, ring_hom.map_mul, algebra.smul_def] }, { rintros ⟨y', rfl⟩, refine ⟨y' * x, submodule.mem_span_singleton.mpr ⟨y', rfl⟩, _⟩, rw [ring_hom.map_mul, algebra.smul_def] } end @[simp] lemma canonical_equiv_span_singleton {P'} [comm_ring P'] [algebra R P'] [is_localization S P'] (x : P) : canonical_equiv S P P' (span_singleton S x) = span_singleton S (is_localization.map P' (ring_hom.id R) (λ y (hy : y ∈ S), show ring_hom.id R y ∈ S, from hy) x) := begin apply set_like.ext_iff.mpr, intro y, split; intro h, { rw mem_span_singleton, obtain ⟨x', hx', rfl⟩ := (mem_canonical_equiv_apply _ _ _).mp h, obtain ⟨z, rfl⟩ := (mem_span_singleton _).mp hx', use z, rw is_localization.map_smul, refl }, { rw mem_canonical_equiv_apply, obtain ⟨z, rfl⟩ := (mem_span_singleton _).mp h, use z • x, use (mem_span_singleton _).mpr ⟨z, rfl⟩, simp [is_localization.map_smul] } end lemma mem_singleton_mul {x y : P} {I : fractional_ideal S P} : y ∈ span_singleton S x * I ↔ ∃ y' ∈ I, y = x * y' := begin split, { intro h, apply fractional_ideal.mul_induction_on h, { intros x' hx' y' hy', obtain ⟨a, ha⟩ := (mem_span_singleton S).mp hx', use [a • y', submodule.smul_mem I a hy'], rw [←ha, algebra.mul_smul_comm, algebra.smul_mul_assoc] }, { exact ⟨0, submodule.zero_mem I, (mul_zero x).symm⟩ }, { rintros _ _ ⟨y, hy, rfl⟩ ⟨y', hy', rfl⟩, exact ⟨y + y', submodule.add_mem I hy hy', (mul_add _ _ _).symm⟩ }, { rintros r _ ⟨y', hy', rfl⟩, exact ⟨r • y', submodule.smul_mem I r hy', (algebra.mul_smul_comm _ _ _).symm ⟩ } }, { rintros ⟨y', hy', rfl⟩, exact mul_mem_mul ((mem_span_singleton S).mpr ⟨1, one_smul _ _⟩) hy' } end omit loc lemma one_div_span_singleton (x : K) : 1 / span_singleton R₁⁰ x = span_singleton R₁⁰ (x⁻¹) := if h : x = 0 then by simp [h] else (eq_one_div_of_mul_eq_one _ _ (by simp [h])).symm @[simp] lemma div_span_singleton (J : fractional_ideal R₁⁰ K) (d : K) : J / span_singleton R₁⁰ d = span_singleton R₁⁰ (d⁻¹) * J := begin rw ← one_div_span_singleton, by_cases hd : d = 0, { simp only [hd, span_singleton_zero, div_zero, zero_mul] }, have h_spand : span_singleton R₁⁰ d ≠ 0 := mt span_singleton_eq_zero_iff.mp hd, apply le_antisymm, { intros x hx, rw [← mem_coe, coe_div h_spand, submodule.mem_div_iff_forall_mul_mem] at hx, specialize hx d (mem_span_singleton_self R₁⁰ d), have h_xd : x = d⁻¹ * (x * d), { field_simp }, rw [← mem_coe, coe_mul, one_div_span_singleton, h_xd], exact submodule.mul_mem_mul (mem_span_singleton_self R₁⁰ _) hx }, { rw [le_div_iff_mul_le h_spand, mul_assoc, mul_left_comm, one_div_span_singleton, span_singleton_mul_span_singleton, inv_mul_cancel hd, span_singleton_one, mul_one], exact le_refl J }, end lemma exists_eq_span_singleton_mul (I : fractional_ideal R₁⁰ K) : ∃ (a : R₁) (aI : ideal R₁), a ≠ 0 ∧ I = span_singleton R₁⁰ (algebra_map R₁ K a)⁻¹ * aI := begin obtain ⟨a_inv, nonzero, ha⟩ := I.is_fractional, have nonzero := mem_non_zero_divisors_iff_ne_zero.mp nonzero, have map_a_nonzero : algebra_map R₁ K a_inv ≠ 0 := mt is_fraction_ring.to_map_eq_zero_iff.mp nonzero, refine ⟨a_inv, submodule.comap (algebra.linear_map R₁ K) ↑(span_singleton R₁⁰ (algebra_map R₁ K a_inv) * I), nonzero, ext (λ x, iff.trans ⟨_, _⟩ mem_singleton_mul.symm)⟩, { intro hx, obtain ⟨x', hx'⟩ := ha x hx, rw algebra.smul_def at hx', refine ⟨algebra_map R₁ K x', (mem_coe_ideal _).mpr ⟨x', mem_singleton_mul.mpr _, rfl⟩, _⟩, { exact ⟨x, hx, hx'⟩ }, { rw [hx', ← mul_assoc, inv_mul_cancel map_a_nonzero, one_mul] } }, { rintros ⟨y, hy, rfl⟩, obtain ⟨x', hx', rfl⟩ := (mem_coe_ideal _).mp hy, obtain ⟨y', hy', hx'⟩ := mem_singleton_mul.mp hx', rw algebra.linear_map_apply at hx', rwa [hx', ←mul_assoc, inv_mul_cancel map_a_nonzero, one_mul] } end instance is_principal {R} [integral_domain R] [is_principal_ideal_ring R] [algebra R K] [is_fraction_ring R K] (I : fractional_ideal R⁰ K) : (I : submodule R K).is_principal := begin obtain ⟨a, aI, -, ha⟩ := exists_eq_span_singleton_mul I, use (algebra_map R K a)⁻¹ * algebra_map R K (generator aI), suffices : I = span_singleton R⁰ ((algebra_map R K a)⁻¹ * algebra_map R K (generator aI)), { exact congr_arg subtype.val this }, conv_lhs { rw [ha, ←span_singleton_generator aI] }, rw [coe_ideal_span_singleton (generator aI), span_singleton_mul_span_singleton] end include loc lemma le_span_singleton_mul_iff {x : P} {I J : fractional_ideal S P} : I ≤ span_singleton S x * J ↔ ∀ zI ∈ I, ∃ zJ ∈ J, x * zJ = zI := show (∀ {zI} (hzI : zI ∈ I), zI ∈ span_singleton _ x * J) ↔ ∀ zI ∈ I, ∃ zJ ∈ J, x * zJ = zI, by simp only [fractional_ideal.mem_singleton_mul, eq_comm] lemma span_singleton_mul_le_iff {x : P} {I J : fractional_ideal S P} : span_singleton _ x * I ≤ J ↔ ∀ z ∈ I, x * z ∈ J := begin simp only [fractional_ideal.mul_le, fractional_ideal.mem_singleton_mul, fractional_ideal.mem_span_singleton], split, { intros h zI hzI, exact h x ⟨1, one_smul _ _⟩ zI hzI }, { rintros h _ ⟨z, rfl⟩ zI hzI, rw [algebra.smul_mul_assoc], exact submodule.smul_mem J.1 _ (h zI hzI) }, end lemma eq_span_singleton_mul {x : P} {I J : fractional_ideal S P} : I = span_singleton _ x * J ↔ (∀ zI ∈ I, ∃ zJ ∈ J, x * zJ = zI) ∧ ∀ z ∈ J, x * z ∈ I := by simp only [le_antisymm_iff, fractional_ideal.le_span_singleton_mul_iff, fractional_ideal.span_singleton_mul_le_iff] omit loc variables (K) lemma mk'_mul_coe_ideal_eq_coe_ideal {I J : ideal R₁} {x y : R₁} (hy : y ∈ R₁⁰) : span_singleton R₁⁰ (is_localization.mk' K x ⟨y, hy⟩) * I = (J : fractional_ideal R₁⁰ K) ↔ ideal.span {x} * I = ideal.span {y} * J := begin have inj : function.injective (coe : ideal R₁ → fractional_ideal R₁⁰ K) := fractional_ideal.coe_ideal_injective, have : span_singleton R₁⁰ (is_localization.mk' _ 1 ⟨y, hy⟩) * span_singleton R₁⁰ (algebra_map R₁ K y) = 1, { rw [span_singleton_mul_span_singleton, mul_comm, ← is_localization.mk'_eq_mul_mk'_one, is_localization.mk'_self, span_singleton_one] }, let y' : units (fractional_ideal R₁⁰ K) := units.mk_of_mul_eq_one _ _ this, have coe_y' : ↑y' = span_singleton R₁⁰ (is_localization.mk' K 1 ⟨y, hy⟩) := rfl, refine iff.trans _ (y'.mul_right_inj.trans inj.eq_iff), simp only [coe_ideal_mul, ideal.span, coe_ideal_span_singleton, ← mul_assoc, coe_y', span_singleton_mul_span_singleton, mul_comm, ← is_localization.mk'_eq_mul_mk'_one, is_localization.mk'_self, span_singleton_one, one_mul], end variables {K} lemma span_singleton_mul_coe_ideal_eq_coe_ideal {I J : ideal R₁} {z : K} : span_singleton R₁⁰ z * (I : fractional_ideal R₁⁰ K) = J ↔ ideal.span {((is_localization.sec R₁⁰ z).1 : R₁)} * I = ideal.span {(is_localization.sec R₁⁰ z).2} * J := -- `erw` to deal with the distinction between `y` and `⟨y.1, y.2⟩` by erw [← mk'_mul_coe_ideal_eq_coe_ideal K (is_localization.sec R₁⁰ z).2.prop, is_localization.mk'_sec K z] end principal_ideal_ring variables {R₁ : Type*} [integral_domain R₁] variables {K : Type*} [field K] [algebra R₁ K] [frac : is_fraction_ring R₁ K] local attribute [instance] classical.prop_decidable lemma is_noetherian_zero : is_noetherian R₁ (0 : fractional_ideal R₁⁰ K) := is_noetherian_submodule.mpr (λ I (hI : I ≤ (0 : fractional_ideal R₁⁰ K)), by { rw coe_zero at hI, rw le_bot_iff.mp hI, exact fg_bot }) lemma is_noetherian_iff {I : fractional_ideal R₁⁰ K} : is_noetherian R₁ I ↔ ∀ J ≤ I, (J : submodule R₁ K).fg := is_noetherian_submodule.trans ⟨λ h J hJ, h _ hJ, λ h J hJ, h ⟨J, is_fractional_of_le hJ⟩ hJ⟩ lemma is_noetherian_coe_to_fractional_ideal [_root_.is_noetherian_ring R₁] (I : ideal R₁) : is_noetherian R₁ (I : fractional_ideal R₁⁰ K) := begin rw is_noetherian_iff, intros J hJ, obtain ⟨J, rfl⟩ := le_one_iff_exists_coe_ideal.mp (le_trans hJ coe_ideal_le_one), exact fg_map (is_noetherian.noetherian J), end include frac lemma is_noetherian_span_singleton_inv_to_map_mul (x : R₁) {I : fractional_ideal R₁⁰ K} (hI : is_noetherian R₁ I) : is_noetherian R₁ (span_singleton R₁⁰ (algebra_map R₁ K x)⁻¹ * I : fractional_ideal R₁⁰ K) := begin by_cases hx : x = 0, { rw [hx, ring_hom.map_zero, _root_.inv_zero, span_singleton_zero, zero_mul], exact is_noetherian_zero }, have h_gx : algebra_map R₁ K x ≠ 0, from mt ((algebra_map R₁ K).injective_iff.mp (is_fraction_ring.injective _ _) x) hx, have h_spanx : span_singleton R₁⁰ (algebra_map R₁ K x) ≠ 0, from span_singleton_ne_zero_iff.mpr h_gx, rw is_noetherian_iff at ⊢ hI, intros J hJ, rw [← div_span_singleton, le_div_iff_mul_le h_spanx] at hJ, obtain ⟨s, hs⟩ := hI _ hJ, use s * {(algebra_map R₁ K x)⁻¹}, rw [finset.coe_mul, finset.coe_singleton, ← span_mul_span, hs, ← coe_span_singleton R₁⁰, ← coe_mul, mul_assoc, span_singleton_mul_span_singleton, mul_inv_cancel h_gx, span_singleton_one, mul_one], end /-- Every fractional ideal of a noetherian integral domain is noetherian. -/ theorem is_noetherian [_root_.is_noetherian_ring R₁] (I : fractional_ideal R₁⁰ K) : is_noetherian R₁ I := begin obtain ⟨d, J, h_nzd, rfl⟩ := exists_eq_span_singleton_mul I, apply is_noetherian_span_singleton_inv_to_map_mul, apply is_noetherian_coe_to_fractional_ideal, end section adjoin include loc omit frac variables {R P} (S) (x : P) (hx : is_integral R x) /-- `A[x]` is a fractional ideal for every integral `x`. -/ lemma is_fractional_adjoin_integral : is_fractional S (algebra.adjoin R ({x} : set P)).to_submodule := is_fractional_of_fg (fg_adjoin_singleton_of_integral x hx) /-- `fractional_ideal.adjoin_integral (S : submonoid R) x hx` is `R[x]` as a fractional ideal, where `hx` is a proof that `x : P` is integral over `R`. -/ @[simps] def adjoin_integral : fractional_ideal S P := ⟨_, is_fractional_adjoin_integral S x hx⟩ lemma mem_adjoin_integral_self : x ∈ adjoin_integral S x hx := algebra.subset_adjoin (set.mem_singleton x) end adjoin end fractional_ideal
19aae68208446bc719ffc200970e1ed0565a60af
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/ring_theory/algebra_tower.lean
a1934918f7194fca9b5604304928d86d07183a46
[ "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
12,623
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.algebra.tower import algebra.invertible import linear_algebra.basis import ring_theory.adjoin.basic import ring_theory.polynomial.tower /-! # Towers of algebras We set up the basic theory of algebra towers. An algebra tower A/S/R is expressed by having instances of `algebra A S`, `algebra R S`, `algebra R A` and `is_scalar_tower R S A`, the later asserting the compatibility condition `(r • s) • a = r • (s • a)`. In `field_theory/tower.lean` we use this to prove the tower law for finite extensions, that if `R` and `S` are both fields, then `[A:R] = [A:S] [S:A]`. In this file we prepare the main lemma: if `{bi | i ∈ I}` is an `R`-basis of `S` and `{cj | j ∈ J}` is a `S`-basis of `A`, then `{bi cj | i ∈ I, j ∈ J}` is an `R`-basis of `A`. This statement does not require the base rings to be a field, so we also generalize the lemma to rings in this file. -/ universes u v w u₁ variables (R : Type u) (S : Type v) (A : Type w) (B : Type u₁) namespace is_scalar_tower section semiring variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B] variables [algebra R S] [algebra S A] [algebra S B] [algebra R A] [algebra R B] variables [is_scalar_tower R S A] [is_scalar_tower R S B] variables (R S A B) /-- Suppose that `R -> S -> A` is a tower of algebras. If an element `r : R` is invertible in `S`, then it is invertible in `A`. -/ def invertible.algebra_tower (r : R) [invertible (algebra_map R S r)] : invertible (algebra_map R A r) := invertible.copy (invertible.map (algebra_map S A : S →* A) (algebra_map R S r)) (algebra_map R A r) (by rw [ring_hom.coe_monoid_hom, is_scalar_tower.algebra_map_apply R S A]) /-- A natural number that is invertible when coerced to `R` is also invertible when coerced to any `R`-algebra. -/ def invertible_algebra_coe_nat (n : ℕ) [inv : invertible (n : R)] : invertible (n : A) := by { haveI : invertible (algebra_map ℕ R n) := inv, exact invertible.algebra_tower ℕ R A n } end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A] [comm_semiring B] variables [algebra R A] [algebra A B] [algebra R B] [is_scalar_tower R A B] end comm_semiring end is_scalar_tower namespace algebra theorem adjoin_algebra_map' {R : Type u} {S : Type v} {A : Type w} [comm_ring R] [comm_ring S] [comm_ring A] [algebra R S] [algebra S A] (s : set S) : adjoin R (algebra_map S (restrict_scalars R S A) '' s) = (adjoin R s).map ((algebra.of_id S (restrict_scalars R S A)).restrict_scalars R) := le_antisymm (adjoin_le $ set.image_subset_iff.2 $ λ y hy, ⟨y, subset_adjoin hy, rfl⟩) (subalgebra.map_le.2 $ adjoin_le $ λ y hy, subset_adjoin ⟨y, hy, rfl⟩) theorem adjoin_algebra_map (R : Type u) (S : Type v) (A : Type w) [comm_ring R] [comm_ring S] [comm_ring A] [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A] (s : set S) : adjoin R (algebra_map S A '' s) = subalgebra.map (adjoin R s) (is_scalar_tower.to_alg_hom R S A) := le_antisymm (adjoin_le $ set.image_subset_iff.2 $ λ y hy, ⟨y, subset_adjoin hy, rfl⟩) (subalgebra.map_le.2 $ adjoin_le $ λ y hy, subset_adjoin ⟨y, hy, rfl⟩) lemma adjoin_res (C D E : Type*) [comm_semiring C] [comm_semiring D] [comm_semiring E] [algebra C D] [algebra C E] [algebra D E] [is_scalar_tower C D E] (S : set E) : (algebra.adjoin D S).res C = ((⊤ : subalgebra C D).map (is_scalar_tower.to_alg_hom C D E)).under (algebra.adjoin ((⊤ : subalgebra C D).map (is_scalar_tower.to_alg_hom C D E)) S) := begin suffices : set.range (algebra_map D E) = set.range (algebra_map ((⊤ : subalgebra C D).map (is_scalar_tower.to_alg_hom C D E)) E), { ext x, change x ∈ subsemiring.closure (_ ∪ S) ↔ x ∈ subsemiring.closure (_ ∪ S), rw this }, ext x, split, { rintros ⟨y, hy⟩, exact ⟨⟨algebra_map D E y, ⟨y, ⟨algebra.mem_top, rfl⟩⟩⟩, hy⟩ }, { rintros ⟨⟨y, ⟨z, ⟨h0, h1⟩⟩⟩, h2⟩, exact ⟨z, eq.trans h1 h2⟩ }, end lemma adjoin_res_eq_adjoin_res (C D E F : Type*) [comm_semiring C] [comm_semiring D] [comm_semiring E] [comm_semiring F] [algebra C D] [algebra C E] [algebra C F] [algebra D F] [algebra E F] [is_scalar_tower C D F] [is_scalar_tower C E F] {S : set D} {T : set E} (hS : algebra.adjoin C S = ⊤) (hT : algebra.adjoin C T = ⊤) : (algebra.adjoin E (algebra_map D F '' S)).res C = (algebra.adjoin D (algebra_map E F '' T)).res C := by { rw [adjoin_res, adjoin_res, ←hS, ←hT, ←algebra.adjoin_image, ←algebra.adjoin_image, ←alg_hom.coe_to_ring_hom, ←alg_hom.coe_to_ring_hom, is_scalar_tower.coe_to_alg_hom, is_scalar_tower.coe_to_alg_hom, ←adjoin_union_eq_under, ←adjoin_union_eq_under, set.union_comm] } end algebra section open_locale classical lemma algebra.fg_trans' {R S A : Type*} [comm_ring R] [comm_ring S] [comm_ring A] [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A] (hRS : (⊤ : subalgebra R S).fg) (hSA : (⊤ : subalgebra S A).fg) : (⊤ : subalgebra R A).fg := let ⟨s, hs⟩ := hRS, ⟨t, ht⟩ := hSA in ⟨s.image (algebra_map S A) ∪ t, by rw [finset.coe_union, finset.coe_image, algebra.adjoin_union_eq_under, algebra.adjoin_algebra_map, hs, algebra.map_top, is_scalar_tower.range_under_adjoin, ht, subalgebra.res_top]⟩ end section ring open finsupp open_locale big_operators classical universes v₁ w₁ variables {R S A} variables [comm_ring R] [ring S] [add_comm_group A] variables [algebra R S] [module S A] [module R A] [is_scalar_tower R S A] theorem linear_independent_smul {ι : Type v₁} {b : ι → S} {ι' : Type w₁} {c : ι' → A} (hb : linear_independent R b) (hc : linear_independent S c) : linear_independent R (λ p : ι × ι', b p.1 • c p.2) := begin rw linear_independent_iff' at hb hc, rw linear_independent_iff'', rintros s g hg hsg ⟨i, k⟩, by_cases hik : (i, k) ∈ s, { have h1 : ∑ i in (s.image prod.fst).product (s.image prod.snd), g i • b i.1 • c i.2 = 0, { rw ← hsg, exact (finset.sum_subset finset.subset_product $ λ p _ hp, show g p • b p.1 • c p.2 = 0, by rw [hg p hp, zero_smul]).symm }, rw [finset.sum_product, finset.sum_comm] at h1, simp_rw [← smul_assoc, ← finset.sum_smul] at h1, exact hb _ _ (hc _ _ h1 k (finset.mem_image_of_mem _ hik)) i (finset.mem_image_of_mem _ hik) }, exact hg _ hik end /-- `basis.smul (b : basis ι R S) (c : basis ι S A)` is the `R`-basis on `A` where the `(i, j)`th basis vector is `b i • c j`. -/ noncomputable def basis.smul {ι : Type v₁} {ι' : Type w₁} (b : basis ι R S) (c : basis ι' S A) : basis (ι × ι') R A := basis.of_repr ((c.repr.restrict_scalars R).trans $ (finsupp.lcongr (equiv.refl _) b.repr).trans $ (finsupp_prod_lequiv R).symm.trans $ (finsupp.lcongr (equiv.prod_comm ι' ι) (linear_equiv.refl _ _))) @[simp] theorem basis.smul_repr {ι : Type v₁} {ι' : Type w₁} (b : basis ι R S) (c : basis ι' S A) (x ij): (b.smul c).repr x ij = b.repr (c.repr x ij.2) ij.1 := by simp [basis.smul] theorem basis.smul_repr_mk {ι : Type v₁} {ι' : Type w₁} (b : basis ι R S) (c : basis ι' S A) (x i j): (b.smul c).repr x (i, j) = b.repr (c.repr x j) i := b.smul_repr c x (i, j) @[simp] theorem basis.smul_apply {ι : Type v₁} {ι' : Type w₁} (b : basis ι R S) (c : basis ι' S A) (ij) : (b.smul c) ij = b ij.1 • c ij.2 := begin obtain ⟨i, j⟩ := ij, rw basis.apply_eq_iff, ext ⟨i', j'⟩, rw [basis.smul_repr, linear_equiv.map_smul, basis.repr_self, finsupp.smul_apply, finsupp.single_apply], dsimp only, split_ifs with hi, { simp [hi, finsupp.single_apply] }, { simp [hi] }, end end ring section artin_tate variables (C : Type*) variables [comm_ring A] [comm_ring B] [comm_ring C] variables [algebra A B] [algebra B C] [algebra A C] [is_scalar_tower A B C] open finset submodule open_locale classical lemma exists_subalgebra_of_fg (hAC : (⊤ : subalgebra A C).fg) (hBC : (⊤ : submodule B C).fg) : ∃ B₀ : subalgebra A B, B₀.fg ∧ (⊤ : submodule B₀ C).fg := begin cases hAC with x hx, cases hBC with y hy, have := hy, simp_rw [eq_top_iff', mem_span_finset] at this, choose f hf, let s : finset B := (finset.product (x ∪ (y * y)) y).image (function.uncurry f), have hsx : ∀ (xi ∈ x) (yj ∈ y), f xi yj ∈ s := λ xi hxi yj hyj, show function.uncurry f (xi, yj) ∈ s, from mem_image_of_mem _ $ mem_product.2 ⟨mem_union_left _ hxi, hyj⟩, have hsy : ∀ (yi yj yk ∈ y), f (yi * yj) yk ∈ s := λ yi yj yk hyi hyj hyk, show function.uncurry f (yi * yj, yk) ∈ s, from mem_image_of_mem _ $ mem_product.2 ⟨mem_union_right _ $ finset.mul_mem_mul hyi hyj, hyk⟩, have hxy : ∀ xi ∈ x, xi ∈ span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C) := λ xi hxi, hf xi ▸ sum_mem _ (λ yj hyj, smul_mem (span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C)) ⟨f xi yj, algebra.subset_adjoin $ hsx xi hxi yj hyj⟩ (subset_span $ mem_insert_of_mem hyj)), have hyy : span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C) * span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C) ≤ span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C), { rw [span_mul_span, span_le, coe_insert], rintros _ ⟨yi, yj, rfl | hyi, rfl | hyj, rfl⟩, { rw mul_one, exact subset_span (set.mem_insert _ _) }, { rw one_mul, exact subset_span (set.mem_insert_of_mem _ hyj) }, { rw mul_one, exact subset_span (set.mem_insert_of_mem _ hyi) }, { rw ← hf (yi * yj), exact set_like.mem_coe.2 (sum_mem _ $ λ yk hyk, smul_mem (span (algebra.adjoin A (↑s : set B)) (insert 1 ↑y : set C)) ⟨f (yi * yj) yk, algebra.subset_adjoin $ hsy yi yj yk hyi hyj hyk⟩ (subset_span $ set.mem_insert_of_mem _ hyk : yk ∈ _)) } }, refine ⟨algebra.adjoin A (↑s : set B), subalgebra.fg_adjoin_finset _, insert 1 y, _⟩, refine restrict_scalars_injective A _ _ _, rw [restrict_scalars_top, eq_top_iff, ← algebra.top_to_submodule, ← hx, algebra.adjoin_eq_span, span_le], refine λ r hr, submonoid.closure_induction hr (λ c hc, hxy c hc) (subset_span $ mem_insert_self _ _) (λ p q hp hq, hyy $ submodule.mul_mem_mul hp hq) end /-- Artin--Tate lemma: if A ⊆ B ⊆ C is a chain of subrings of commutative rings, and A is noetherian, and C is algebra-finite over A, and C is module-finite over B, then B is algebra-finite over A. References: Atiyah--Macdonald Proposition 7.8; Stacks 00IS; Altman--Kleiman 16.17. -/ theorem fg_of_fg_of_fg [is_noetherian_ring A] (hAC : (⊤ : subalgebra A C).fg) (hBC : (⊤ : submodule B C).fg) (hBCi : function.injective (algebra_map B C)) : (⊤ : subalgebra A B).fg := let ⟨B₀, hAB₀, hB₀C⟩ := exists_subalgebra_of_fg A B C hAC hBC in algebra.fg_trans' (B₀.fg_top.2 hAB₀) $ subalgebra.fg_of_submodule_fg $ have is_noetherian_ring B₀, from is_noetherian_ring_of_fg hAB₀, have is_noetherian B₀ C, by exactI is_noetherian_of_fg_of_noetherian' hB₀C, by exactI fg_of_injective (is_scalar_tower.to_alg_hom B₀ B C).to_linear_map (linear_map.ker_eq_bot.2 hBCi) end artin_tate section alg_hom_tower variables {A} {C D : Type*} [comm_semiring A] [comm_semiring C] [comm_semiring D] [algebra A C] [algebra A D] variables (f : C →ₐ[A] D) (B) [comm_semiring B] [algebra A B] [algebra B C] [is_scalar_tower A B C] /-- Restrict the domain of an `alg_hom`. -/ def alg_hom.restrict_domain : B →ₐ[A] D := f.comp (is_scalar_tower.to_alg_hom A B C) /-- Extend the scalars of an `alg_hom`. -/ def alg_hom.extend_scalars : @alg_hom B C D _ _ _ _ (f.restrict_domain B).to_ring_hom.to_algebra := { commutes' := λ _, rfl .. f } variables {B} /-- `alg_hom`s from the top of a tower are equivalent to a pair of `alg_hom`s. -/ def alg_hom_equiv_sigma : (C →ₐ[A] D) ≃ Σ (f : B →ₐ[A] D), @alg_hom B C D _ _ _ _ f.to_ring_hom.to_algebra := { to_fun := λ f, ⟨f.restrict_domain B, f.extend_scalars B⟩, inv_fun := λ fg, let alg := fg.1.to_ring_hom.to_algebra in by exactI fg.2.restrict_scalars A, left_inv := λ f, by { dsimp only, ext, refl }, right_inv := begin rintros ⟨⟨f, _, _, _, _, _⟩, g, _, _, _, _, hg⟩, have : f = λ x, g (algebra_map B C x) := by { ext, exact (hg x).symm }, subst this, refl, end } end alg_hom_tower
77972c32e28e4e7bbd6dd1fe245e191adbf63b3a
63abd62053d479eae5abf4951554e1064a4c45b4
/src/category_theory/limits/pi.lean
780e1b0e3c5f785c52bf4b6ae8e38d2ad3dbfa5f
[ "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
4,499
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 category_theory.pi.basic import category_theory.limits.limits /-! # Limits in the category of indexed families of objects. Given a functor `F : J ⥤ Π i, C i` into a category of indexed families, 1. we can assemble a collection of cones over `F ⋙ pi.eval C i` into a cone over `F` 2. if all those cones are limit cones, the assembled cone is a limit cone, and 3. if we have limits for each of `F ⋙ pi.eval C i`, we can produce a `has_limit F` instance -/ open category_theory open category_theory.limits namespace category_theory.pi universes v₁ v₂ u₁ u₂ variables {I : Type v₁} {C : I → Type u₁} [Π i, category.{v₁} (C i)] variables {J : Type v₁} [small_category J] variables {F : J ⥤ Π i, C i} /-- A cone over `F : J ⥤ Π i, C i` has as its components cones over each of the `F ⋙ pi.eval C i`. -/ def cone_comp_eval (c : cone F) (i : I) : cone (F ⋙ pi.eval C i) := { X := c.X i, π := { app := λ j, c.π.app j i, naturality' := λ j j' f, congr_fun (c.π.naturality f) i, } } /-- A cocone over `F : J ⥤ Π i, C i` has as its components cocones over each of the `F ⋙ pi.eval C i`. -/ def cocone_comp_eval (c : cocone F) (i : I) : cocone (F ⋙ pi.eval C i) := { X := c.X i, ι := { app := λ j, c.ι.app j i, naturality' := λ j j' f, congr_fun (c.ι.naturality f) i, } } /-- Given a family of cones over the `F ⋙ pi.eval C i`, we can assemble these together as a `cone F`. -/ def cone_of_cone_comp_eval (c : Π i, cone (F ⋙ pi.eval C i)) : cone F := { X := λ i, (c i).X, π := { app := λ j i, (c i).π.app j, naturality' := λ j j' f, by { ext i, exact (c i).π.naturality f, } } } /-- Given a family of cocones over the `F ⋙ pi.eval C i`, we can assemble these together as a `cocone F`. -/ def cocone_of_cocone_comp_eval (c : Π i, cocone (F ⋙ pi.eval C i)) : cocone F := { X := λ i, (c i).X, ι := { app := λ j i, (c i).ι.app j, naturality' := λ j j' f, by { ext i, exact (c i).ι.naturality f, } } } /-- Given a family of limit cones over the `F ⋙ pi.eval C i`, assembling them together as a `cone F` produces a limit cone. -/ def cone_of_cone_eval_is_limit {c : Π i, cone (F ⋙ pi.eval C i)} (P : Π i, is_limit (c i)) : is_limit (cone_of_cone_comp_eval c) := { lift := λ s i, (P i).lift (cone_comp_eval s i), fac' := λ s j, begin ext i, exact (P i).fac (cone_comp_eval s i) j, end, uniq' := λ s m w, begin ext i, exact (P i).uniq (cone_comp_eval s i) (m i) (λ j, congr_fun (w j) i) end } /-- Given a family of colimit cocones over the `F ⋙ pi.eval C i`, assembling them together as a `cocone F` produces a colimit cocone. -/ def cocone_of_cocone_eval_is_colimit {c : Π i, cocone (F ⋙ pi.eval C i)} (P : Π i, is_colimit (c i)) : is_colimit (cocone_of_cocone_comp_eval c) := { desc := λ s i, (P i).desc (cocone_comp_eval s i), fac' := λ s j, begin ext i, exact (P i).fac (cocone_comp_eval s i) j, end, uniq' := λ s m w, begin ext i, exact (P i).uniq (cocone_comp_eval s i) (m i) (λ j, congr_fun (w j) i) end } section variables [∀ i, has_limit (F ⋙ pi.eval C i)] /-- If we have a functor `F : J ⥤ Π i, C i` into a category of indexed families, and we have limits for each of the `F ⋙ pi.eval C i`, then `F` has a limit. -/ lemma has_limit_of_has_limit_comp_eval : has_limit F := has_limit.mk { cone := cone_of_cone_comp_eval (λ i, limit.cone _), is_limit := cone_of_cone_eval_is_limit (λ i, limit.is_limit _), } end section variables [∀ i, has_colimit (F ⋙ pi.eval C i)] /-- If we have a functor `F : J ⥤ Π i, C i` into a category of indexed families, and colimits exist for each of the `F ⋙ pi.eval C i`, there is a colimit for `F`. -/ lemma has_colimit_of_has_colimit_comp_eval : has_colimit F := has_colimit.mk { cocone := cocone_of_cocone_comp_eval (λ i, colimit.cocone _), is_colimit := cocone_of_cocone_eval_is_colimit (λ i, colimit.is_colimit _), } end /-! As an example, we can use this to construct particular shapes of limits in a category of indexed families. With the addition of `import category_theory.limits.shapes.types` we can use: ``` local attribute [instance] has_limit_of_has_limit_comp_eval example : has_binary_products (I → Type v₁) := ⟨by apply_instance⟩ ``` -/ end category_theory.pi
73b6da58bde25f3a39f64739b30a252323854d2d
32da3d0f92cab08875472ef6cacc1931c2b3eafa
/src/category_theory/limits/shapes/binary_products.lean
30ba1f56242c3721f14aaa300190d223c12eaa5f
[ "Apache-2.0" ]
permissive
karthiknadig/mathlib
b6073c3748860bfc9a3e55da86afcddba62dc913
33a86cfff12d7f200d0010cd03b95e9b69a6c1a5
refs/heads/master
1,676,389,371,851
1,610,061,127,000
1,610,061,127,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
34,014
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Bhavik Mehta -/ import category_theory.limits.limits import category_theory.limits.shapes.terminal import category_theory.discrete_category import category_theory.epi_mono /-! # Binary (co)products We define a category `walking_pair`, which is the index category for a binary (co)product diagram. A convenience method `pair X Y` constructs the functor from the walking pair, hitting the given objects. We define `prod X Y` and `coprod X Y` as limits and colimits of such functors. Typeclasses `has_binary_products` and `has_binary_coproducts` assert the existence of (co)limits shaped as walking pairs. We include lemmas for simplifying equations involving projections and coprojections, and define braiding and associating isomorphisms, and the product comparison morphism. ## References * [Stacks: Products of pairs](https://stacks.math.columbia.edu/tag/001R) * [Stacks: coproducts of pairs](https://stacks.math.columbia.edu/tag/04AN) -/ noncomputable theory universes v u u₂ open category_theory namespace category_theory.limits /-- The type of objects for the diagram indexing a binary (co)product. -/ @[derive decidable_eq, derive inhabited] inductive walking_pair : Type v | left | right open walking_pair /-- The equivalence swapping left and right. -/ def walking_pair.swap : walking_pair ≃ walking_pair := { to_fun := λ j, walking_pair.rec_on j right left, inv_fun := λ j, walking_pair.rec_on j right left, left_inv := λ j, by { cases j; refl, }, right_inv := λ j, by { cases j; refl, }, } @[simp] lemma walking_pair.swap_apply_left : walking_pair.swap left = right := rfl @[simp] lemma walking_pair.swap_apply_right : walking_pair.swap right = left := rfl @[simp] lemma walking_pair.swap_symm_apply_tt : walking_pair.swap.symm left = right := rfl @[simp] lemma walking_pair.swap_symm_apply_ff : walking_pair.swap.symm right = left := rfl /-- An equivalence from `walking_pair` to `bool`, sometimes useful when reindexing limits. -/ def walking_pair.equiv_bool : walking_pair ≃ bool := { to_fun := λ j, walking_pair.rec_on j tt ff, -- to match equiv.sum_equiv_sigma_bool inv_fun := λ b, bool.rec_on b right left, left_inv := λ j, by { cases j; refl, }, right_inv := λ b, by { cases b; refl, }, } @[simp] lemma walking_pair.equiv_bool_apply_left : walking_pair.equiv_bool left = tt := rfl @[simp] lemma walking_pair.equiv_bool_apply_right : walking_pair.equiv_bool right = ff := rfl @[simp] lemma walking_pair.equiv_bool_symm_apply_tt : walking_pair.equiv_bool.symm tt = left := rfl @[simp] lemma walking_pair.equiv_bool_symm_apply_ff : walking_pair.equiv_bool.symm ff = right := rfl variables {C : Type u} [category.{v} C] /-- The diagram on the walking pair, sending the two points to `X` and `Y`. -/ def pair (X Y : C) : discrete walking_pair ⥤ C := discrete.functor (λ j, walking_pair.cases_on j X Y) @[simp] lemma pair_obj_left (X Y : C) : (pair X Y).obj left = X := rfl @[simp] lemma pair_obj_right (X Y : C) : (pair X Y).obj right = Y := rfl section variables {F G : discrete walking_pair.{v} ⥤ C} (f : F.obj left ⟶ G.obj left) (g : F.obj right ⟶ G.obj right) /-- The natural transformation between two functors out of the walking pair, specified by its components. -/ def map_pair : F ⟶ G := { app := λ j, walking_pair.cases_on j f g } @[simp] lemma map_pair_left : (map_pair f g).app left = f := rfl @[simp] lemma map_pair_right : (map_pair f g).app right = g := rfl /-- The natural isomorphism between two functors out of the walking pair, specified by its components. -/ @[simps] def map_pair_iso (f : F.obj left ≅ G.obj left) (g : F.obj right ≅ G.obj right) : F ≅ G := nat_iso.of_components (λ j, walking_pair.cases_on j f g) (by tidy) end /-- Every functor out of the walking pair is naturally isomorphic (actually, equal) to a `pair` -/ @[simps] def diagram_iso_pair (F : discrete walking_pair ⥤ C) : F ≅ pair (F.obj walking_pair.left) (F.obj walking_pair.right) := map_pair_iso (iso.refl _) (iso.refl _) section variables {D : Type u} [category.{v} D] /-- The natural isomorphism between `pair X Y ⋙ F` and `pair (F.obj X) (F.obj Y)`. -/ def pair_comp (X Y : C) (F : C ⥤ D) : pair X Y ⋙ F ≅ pair (F.obj X) (F.obj Y) := diagram_iso_pair _ end /-- A binary fan is just a cone on a diagram indexing a product. -/ abbreviation binary_fan (X Y : C) := cone (pair X Y) /-- The first projection of a binary fan. -/ abbreviation binary_fan.fst {X Y : C} (s : binary_fan X Y) := s.π.app walking_pair.left /-- The second projection of a binary fan. -/ abbreviation binary_fan.snd {X Y : C} (s : binary_fan X Y) := s.π.app walking_pair.right @[simp] lemma binary_fan.π_app_left {X Y : C} (s : binary_fan X Y) : s.π.app walking_pair.left = s.fst := rfl @[simp] lemma binary_fan.π_app_right {X Y : C} (s : binary_fan X Y) : s.π.app walking_pair.right = s.snd := rfl lemma binary_fan.is_limit.hom_ext {W X Y : C} {s : binary_fan X Y} (h : is_limit s) {f g : W ⟶ s.X} (h₁ : f ≫ s.fst = g ≫ s.fst) (h₂ : f ≫ s.snd = g ≫ s.snd) : f = g := h.hom_ext $ λ j, walking_pair.cases_on j h₁ h₂ /-- A binary cofan is just a cocone on a diagram indexing a coproduct. -/ abbreviation binary_cofan (X Y : C) := cocone (pair X Y) /-- The first inclusion of a binary cofan. -/ abbreviation binary_cofan.inl {X Y : C} (s : binary_cofan X Y) := s.ι.app walking_pair.left /-- The second inclusion of a binary cofan. -/ abbreviation binary_cofan.inr {X Y : C} (s : binary_cofan X Y) := s.ι.app walking_pair.right @[simp] lemma binary_cofan.ι_app_left {X Y : C} (s : binary_cofan X Y) : s.ι.app walking_pair.left = s.inl := rfl @[simp] lemma binary_cofan.ι_app_right {X Y : C} (s : binary_cofan X Y) : s.ι.app walking_pair.right = s.inr := rfl lemma binary_cofan.is_colimit.hom_ext {W X Y : C} {s : binary_cofan X Y} (h : is_colimit s) {f g : s.X ⟶ W} (h₁ : s.inl ≫ f = s.inl ≫ g) (h₂ : s.inr ≫ f = s.inr ≫ g) : f = g := h.hom_ext $ λ j, walking_pair.cases_on j h₁ h₂ variables {X Y : C} /-- A binary fan with vertex `P` consists of the two projections `π₁ : P ⟶ X` and `π₂ : P ⟶ Y`. -/ @[simps X] def binary_fan.mk {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) : binary_fan X Y := { X := P, π := { app := λ j, walking_pair.cases_on j π₁ π₂ }} /-- A binary cofan with vertex `P` consists of the two inclusions `ι₁ : X ⟶ P` and `ι₂ : Y ⟶ P`. -/ @[simps X] def binary_cofan.mk {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) : binary_cofan X Y := { X := P, ι := { app := λ j, walking_pair.cases_on j ι₁ ι₂ }} @[simp] lemma binary_fan.mk_π_app_left {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) : (binary_fan.mk π₁ π₂).π.app walking_pair.left = π₁ := rfl @[simp] lemma binary_fan.mk_π_app_right {P : C} (π₁ : P ⟶ X) (π₂ : P ⟶ Y) : (binary_fan.mk π₁ π₂).π.app walking_pair.right = π₂ := rfl @[simp] lemma binary_cofan.mk_ι_app_left {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) : (binary_cofan.mk ι₁ ι₂).ι.app walking_pair.left = ι₁ := rfl @[simp] lemma binary_cofan.mk_ι_app_right {P : C} (ι₁ : X ⟶ P) (ι₂ : Y ⟶ P) : (binary_cofan.mk ι₁ ι₂).ι.app walking_pair.right = ι₂ := rfl /-- If `s` is a limit binary fan over `X` and `Y`, then every pair of morphisms `f : W ⟶ X` and `g : W ⟶ Y` induces a morphism `l : W ⟶ s.X` satisfying `l ≫ s.fst = f` and `l ≫ s.snd = g`. -/ @[simps] def binary_fan.is_limit.lift' {W X Y : C} {s : binary_fan X Y} (h : is_limit s) (f : W ⟶ X) (g : W ⟶ Y) : {l : W ⟶ s.X // l ≫ s.fst = f ∧ l ≫ s.snd = g} := ⟨h.lift $ binary_fan.mk f g, h.fac _ _, h.fac _ _⟩ /-- If `s` is a colimit binary cofan over `X` and `Y`,, then every pair of morphisms `f : X ⟶ W` and `g : Y ⟶ W` induces a morphism `l : s.X ⟶ W` satisfying `s.inl ≫ l = f` and `s.inr ≫ l = g`. -/ @[simps] def binary_cofan.is_colimit.desc' {W X Y : C} {s : binary_cofan X Y} (h : is_colimit s) (f : X ⟶ W) (g : Y ⟶ W) : {l : s.X ⟶ W // s.inl ≫ l = f ∧ s.inr ≫ l = g} := ⟨h.desc $ binary_cofan.mk f g, h.fac _ _, h.fac _ _⟩ /-- An abbreviation for `has_limit (pair X Y)`. -/ abbreviation has_binary_product (X Y : C) := has_limit (pair X Y) /-- An abbreviation for `has_colimit (pair X Y)`. -/ abbreviation has_binary_coproduct (X Y : C) := has_colimit (pair X Y) /-- If we have a product of `X` and `Y`, we can access it using `prod X Y` or `X ⨯ Y`. -/ abbreviation prod (X Y : C) [has_binary_product X Y] := limit (pair X Y) /-- If we have a coproduct of `X` and `Y`, we can access it using `coprod X Y ` or `X ⨿ Y`. -/ abbreviation coprod (X Y : C) [has_binary_coproduct X Y] := colimit (pair X Y) notation X ` ⨯ `:20 Y:20 := prod X Y notation X ` ⨿ `:20 Y:20 := coprod X Y /-- The projection map to the first component of the product. -/ abbreviation prod.fst {X Y : C} [has_binary_product X Y] : X ⨯ Y ⟶ X := limit.π (pair X Y) walking_pair.left /-- The projecton map to the second component of the product. -/ abbreviation prod.snd {X Y : C} [has_binary_product X Y] : X ⨯ Y ⟶ Y := limit.π (pair X Y) walking_pair.right /-- The inclusion map from the first component of the coproduct. -/ abbreviation coprod.inl {X Y : C} [has_binary_coproduct X Y] : X ⟶ X ⨿ Y := colimit.ι (pair X Y) walking_pair.left /-- The inclusion map from the second component of the coproduct. -/ abbreviation coprod.inr {X Y : C} [has_binary_coproduct X Y] : Y ⟶ X ⨿ Y := colimit.ι (pair X Y) walking_pair.right /-- The binary fan constructed from the projection maps is a limit. -/ def prod_is_prod (X Y : C) [has_binary_product X Y] : is_limit (binary_fan.mk (prod.fst : X ⨯ Y ⟶ X) prod.snd) := (limit.is_limit _).of_iso_limit (cones.ext (iso.refl _) (by { rintro (_ | _), tidy })) /-- The binary cofan constructed from the coprojection maps is a colimit. -/ def coprod_is_coprod {X Y : C} [has_binary_coproduct X Y] : is_colimit (binary_cofan.mk (coprod.inl : X ⟶ X ⨿ Y) coprod.inr) := (colimit.is_colimit _).of_iso_colimit (cocones.ext (iso.refl _) (by { rintro (_ | _), tidy })) @[ext] lemma prod.hom_ext {W X Y : C} [has_binary_product X Y] {f g : W ⟶ X ⨯ Y} (h₁ : f ≫ prod.fst = g ≫ prod.fst) (h₂ : f ≫ prod.snd = g ≫ prod.snd) : f = g := binary_fan.is_limit.hom_ext (limit.is_limit _) h₁ h₂ @[ext] lemma coprod.hom_ext {W X Y : C} [has_binary_coproduct X Y] {f g : X ⨿ Y ⟶ W} (h₁ : coprod.inl ≫ f = coprod.inl ≫ g) (h₂ : coprod.inr ≫ f = coprod.inr ≫ g) : f = g := binary_cofan.is_colimit.hom_ext (colimit.is_colimit _) h₁ h₂ /-- If the product of `X` and `Y` exists, then every pair of morphisms `f : W ⟶ X` and `g : W ⟶ Y` induces a morphism `prod.lift f g : W ⟶ X ⨯ Y`. -/ abbreviation prod.lift {W X Y : C} [has_binary_product X Y] (f : W ⟶ X) (g : W ⟶ Y) : W ⟶ X ⨯ Y := limit.lift _ (binary_fan.mk f g) /-- diagonal arrow of the binary product in the category `fam I` -/ abbreviation diag (X : C) [has_binary_product X X] : X ⟶ X ⨯ X := prod.lift (𝟙 _) (𝟙 _) /-- If the coproduct of `X` and `Y` exists, then every pair of morphisms `f : X ⟶ W` and `g : Y ⟶ W` induces a morphism `coprod.desc f g : X ⨿ Y ⟶ W`. -/ abbreviation coprod.desc {W X Y : C} [has_binary_coproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) : X ⨿ Y ⟶ W := colimit.desc _ (binary_cofan.mk f g) /-- codiagonal arrow of the binary coproduct -/ abbreviation codiag (X : C) [has_binary_coproduct X X] : X ⨿ X ⟶ X := coprod.desc (𝟙 _) (𝟙 _) @[simp, reassoc] lemma prod.lift_fst {W X Y : C} [has_binary_product X Y] (f : W ⟶ X) (g : W ⟶ Y) : prod.lift f g ≫ prod.fst = f := limit.lift_π _ _ @[simp, reassoc] lemma prod.lift_snd {W X Y : C} [has_binary_product X Y] (f : W ⟶ X) (g : W ⟶ Y) : prod.lift f g ≫ prod.snd = g := limit.lift_π _ _ -- The simp linter says simp can prove the reassoc version of this lemma. @[reassoc, simp] lemma coprod.inl_desc {W X Y : C} [has_binary_coproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) : coprod.inl ≫ coprod.desc f g = f := colimit.ι_desc _ _ -- The simp linter says simp can prove the reassoc version of this lemma. @[reassoc, simp] lemma coprod.inr_desc {W X Y : C} [has_binary_coproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) : coprod.inr ≫ coprod.desc f g = g := colimit.ι_desc _ _ instance prod.mono_lift_of_mono_left {W X Y : C} [has_binary_product X Y] (f : W ⟶ X) (g : W ⟶ Y) [mono f] : mono (prod.lift f g) := mono_of_mono_fac $ prod.lift_fst _ _ instance prod.mono_lift_of_mono_right {W X Y : C} [has_binary_product X Y] (f : W ⟶ X) (g : W ⟶ Y) [mono g] : mono (prod.lift f g) := mono_of_mono_fac $ prod.lift_snd _ _ instance coprod.epi_desc_of_epi_left {W X Y : C} [has_binary_coproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) [epi f] : epi (coprod.desc f g) := epi_of_epi_fac $ coprod.inl_desc _ _ instance coprod.epi_desc_of_epi_right {W X Y : C} [has_binary_coproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) [epi g] : epi (coprod.desc f g) := epi_of_epi_fac $ coprod.inr_desc _ _ /-- If the product of `X` and `Y` exists, then every pair of morphisms `f : W ⟶ X` and `g : W ⟶ Y` induces a morphism `l : W ⟶ X ⨯ Y` satisfying `l ≫ prod.fst = f` and `l ≫ prod.snd = g`. -/ def prod.lift' {W X Y : C} [has_binary_product X Y] (f : W ⟶ X) (g : W ⟶ Y) : {l : W ⟶ X ⨯ Y // l ≫ prod.fst = f ∧ l ≫ prod.snd = g} := ⟨prod.lift f g, prod.lift_fst _ _, prod.lift_snd _ _⟩ /-- If the coproduct of `X` and `Y` exists, then every pair of morphisms `f : X ⟶ W` and `g : Y ⟶ W` induces a morphism `l : X ⨿ Y ⟶ W` satisfying `coprod.inl ≫ l = f` and `coprod.inr ≫ l = g`. -/ def coprod.desc' {W X Y : C} [has_binary_coproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) : {l : X ⨿ Y ⟶ W // coprod.inl ≫ l = f ∧ coprod.inr ≫ l = g} := ⟨coprod.desc f g, coprod.inl_desc _ _, coprod.inr_desc _ _⟩ /-- If the products `W ⨯ X` and `Y ⨯ Z` exist, then every pair of morphisms `f : W ⟶ Y` and `g : X ⟶ Z` induces a morphism `prod.map f g : W ⨯ X ⟶ Y ⨯ Z`. -/ def prod.map {W X Y Z : C} [has_binary_product W X] [has_binary_product Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : W ⨯ X ⟶ Y ⨯ Z := lim_map (map_pair f g) /-- If the coproducts `W ⨿ X` and `Y ⨿ Z` exist, then every pair of morphisms `f : W ⟶ Y` and `g : W ⟶ Z` induces a morphism `coprod.map f g : W ⨿ X ⟶ Y ⨿ Z`. -/ def coprod.map {W X Y Z : C} [has_binary_coproduct W X] [has_binary_coproduct Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : W ⨿ X ⟶ Y ⨿ Z := colim_map (map_pair f g) section prod_lemmas -- Making the reassoc version of this a simp lemma seems to be more harmful than helpful. @[reassoc, simp] lemma prod.comp_lift {V W X Y : C} [has_binary_product X Y] (f : V ⟶ W) (g : W ⟶ X) (h : W ⟶ Y) : f ≫ prod.lift g h = prod.lift (f ≫ g) (f ≫ h) := by { ext; simp } lemma prod.comp_diag {X Y : C} [has_binary_product Y Y] (f : X ⟶ Y) : f ≫ diag Y = prod.lift f f := by simp @[simp, reassoc] lemma prod.map_fst {W X Y Z : C} [has_binary_product W X] [has_binary_product Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : prod.map f g ≫ prod.fst = prod.fst ≫ f := lim_map_π _ _ @[simp, reassoc] lemma prod.map_snd {W X Y Z : C} [has_binary_product W X] [has_binary_product Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : prod.map f g ≫ prod.snd = prod.snd ≫ g := lim_map_π _ _ @[simp] lemma prod.map_id_id {X Y : C} [has_binary_product X Y] : prod.map (𝟙 X) (𝟙 Y) = 𝟙 _ := by { ext; simp } @[simp] lemma prod.lift_fst_snd {X Y : C} [has_binary_product X Y] : prod.lift prod.fst prod.snd = 𝟙 (X ⨯ Y) := by { ext; simp } @[simp, reassoc] lemma prod.lift_map {V W X Y Z : C} [has_binary_product W X] [has_binary_product Y Z] (f : V ⟶ W) (g : V ⟶ X) (h : W ⟶ Y) (k : X ⟶ Z) : prod.lift f g ≫ prod.map h k = prod.lift (f ≫ h) (g ≫ k) := by { ext; simp } @[simp] lemma prod.lift_fst_comp_snd_comp {W X Y Z : C} [has_binary_product W Y] [has_binary_product X Z] (g : W ⟶ X) (g' : Y ⟶ Z) : prod.lift (prod.fst ≫ g) (prod.snd ≫ g') = prod.map g g' := by { rw ← prod.lift_map, simp } -- We take the right hand side here to be simp normal form, as this way composition lemmas for -- `f ≫ h` and `g ≫ k` can fire (eg `id_comp`) , while `map_fst` and `map_snd` can still work just -- as well. @[simp, reassoc] lemma prod.map_map {A₁ A₂ A₃ B₁ B₂ B₃ : C} [has_binary_product A₁ B₁] [has_binary_product A₂ B₂] [has_binary_product A₃ B₃] (f : A₁ ⟶ A₂) (g : B₁ ⟶ B₂) (h : A₂ ⟶ A₃) (k : B₂ ⟶ B₃) : prod.map f g ≫ prod.map h k = prod.map (f ≫ h) (g ≫ k) := by { ext; simp } -- TODO: is it necessary to weaken the assumption here? @[reassoc] lemma prod.map_swap {A B X Y : C} (f : A ⟶ B) (g : X ⟶ Y) [has_limits_of_shape (discrete walking_pair) C] : prod.map (𝟙 X) f ≫ prod.map g (𝟙 B) = prod.map g (𝟙 A) ≫ prod.map (𝟙 Y) f := by simp @[reassoc] lemma prod.map_comp_id {X Y Z W : C} (f : X ⟶ Y) (g : Y ⟶ Z) [has_binary_product X W] [has_binary_product Z W] [has_binary_product Y W] : prod.map (f ≫ g) (𝟙 W) = prod.map f (𝟙 W) ≫ prod.map g (𝟙 W) := by simp @[reassoc] lemma prod.map_id_comp {X Y Z W : C} (f : X ⟶ Y) (g : Y ⟶ Z) [has_binary_product W X] [has_binary_product W Y] [has_binary_product W Z] : prod.map (𝟙 W) (f ≫ g) = prod.map (𝟙 W) f ≫ prod.map (𝟙 W) g := by simp /-- If the products `W ⨯ X` and `Y ⨯ Z` exist, then every pair of isomorphisms `f : W ≅ Y` and `g : X ≅ Z` induces an isomorphism `prod.map_iso f g : W ⨯ X ≅ Y ⨯ Z`. -/ @[simps] def prod.map_iso {W X Y Z : C} [has_binary_product W X] [has_binary_product Y Z] (f : W ≅ Y) (g : X ≅ Z) : W ⨯ X ≅ Y ⨯ Z := { hom := prod.map f.hom g.hom, inv := prod.map f.inv g.inv } instance is_iso_prod {W X Y Z : C} [has_binary_product W X] [has_binary_product Y Z] (f : W ⟶ Y) (g : X ⟶ Z) [is_iso f] [is_iso g] : is_iso (prod.map f g) := is_iso.of_iso (prod.map_iso (as_iso f) (as_iso g)) @[simp, reassoc] lemma prod.diag_map {X Y : C} (f : X ⟶ Y) [has_binary_product X X] [has_binary_product Y Y] : diag X ≫ prod.map f f = f ≫ diag Y := by simp @[simp, reassoc] lemma prod.diag_map_fst_snd {X Y : C} [has_binary_product X Y] [has_binary_product (X ⨯ Y) (X ⨯ Y)] : diag (X ⨯ Y) ≫ prod.map prod.fst prod.snd = 𝟙 (X ⨯ Y) := by simp @[simp, reassoc] lemma prod.diag_map_fst_snd_comp [has_limits_of_shape (discrete walking_pair) C] {X X' Y Y' : C} (g : X ⟶ Y) (g' : X' ⟶ Y') : diag (X ⨯ X') ≫ prod.map (prod.fst ≫ g) (prod.snd ≫ g') = prod.map g g' := by simp instance {X : C} [has_binary_product X X] : split_mono (diag X) := { retraction := prod.fst } end prod_lemmas section coprod_lemmas @[simp, reassoc] lemma coprod.desc_comp {V W X Y : C} [has_binary_coproduct X Y] (f : V ⟶ W) (g : X ⟶ V) (h : Y ⟶ V) : coprod.desc g h ≫ f = coprod.desc (g ≫ f) (h ≫ f) := by { ext; simp } lemma coprod.diag_comp {X Y : C} [has_binary_coproduct X X] (f : X ⟶ Y) : codiag X ≫ f = coprod.desc f f := by simp @[simp, reassoc] lemma coprod.inl_map {W X Y Z : C} [has_binary_coproduct W X] [has_binary_coproduct Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : coprod.inl ≫ coprod.map f g = f ≫ coprod.inl := ι_colim_map _ _ @[simp, reassoc] lemma coprod.inr_map {W X Y Z : C} [has_binary_coproduct W X] [has_binary_coproduct Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : coprod.inr ≫ coprod.map f g = g ≫ coprod.inr := ι_colim_map _ _ @[simp] lemma coprod.map_id_id {X Y : C} [has_binary_coproduct X Y] : coprod.map (𝟙 X) (𝟙 Y) = 𝟙 _ := by { ext; simp } @[simp] lemma coprod.desc_inl_inr {X Y : C} [has_binary_coproduct X Y] : coprod.desc coprod.inl coprod.inr = 𝟙 (X ⨿ Y) := by { ext; simp } -- The simp linter says simp can prove the reassoc version of this lemma. @[reassoc, simp] lemma coprod.map_desc {S T U V W : C} [has_binary_coproduct U W] [has_binary_coproduct T V] (f : U ⟶ S) (g : W ⟶ S) (h : T ⟶ U) (k : V ⟶ W) : coprod.map h k ≫ coprod.desc f g = coprod.desc (h ≫ f) (k ≫ g) := by { ext; simp } @[simp] lemma coprod.desc_comp_inl_comp_inr {W X Y Z : C} [has_binary_coproduct W Y] [has_binary_coproduct X Z] (g : W ⟶ X) (g' : Y ⟶ Z) : coprod.desc (g ≫ coprod.inl) (g' ≫ coprod.inr) = coprod.map g g' := by { rw ← coprod.map_desc, simp } -- We take the right hand side here to be simp normal form, as this way composition lemmas for -- `f ≫ h` and `g ≫ k` can fire (eg `id_comp`) , while `inl_map` and `inr_map` can still work just -- as well. @[simp, reassoc] lemma coprod.map_map {A₁ A₂ A₃ B₁ B₂ B₃ : C} [has_binary_coproduct A₁ B₁] [has_binary_coproduct A₂ B₂] [has_binary_coproduct A₃ B₃] (f : A₁ ⟶ A₂) (g : B₁ ⟶ B₂) (h : A₂ ⟶ A₃) (k : B₂ ⟶ B₃) : coprod.map f g ≫ coprod.map h k = coprod.map (f ≫ h) (g ≫ k) := by { ext; simp } -- I don't think it's a good idea to make any of the following three simp lemmas. @[reassoc] lemma coprod.map_swap {A B X Y : C} (f : A ⟶ B) (g : X ⟶ Y) [has_colimits_of_shape (discrete walking_pair) C] : coprod.map (𝟙 X) f ≫ coprod.map g (𝟙 B) = coprod.map g (𝟙 A) ≫ coprod.map (𝟙 Y) f := by simp @[reassoc] lemma coprod.map_comp_id {X Y Z W : C} (f : X ⟶ Y) (g : Y ⟶ Z) [has_binary_coproduct Z W] [has_binary_coproduct Y W] [has_binary_coproduct X W] : coprod.map (f ≫ g) (𝟙 W) = coprod.map f (𝟙 W) ≫ coprod.map g (𝟙 W) := by simp @[reassoc] lemma coprod.map_id_comp {X Y Z W : C} (f : X ⟶ Y) (g : Y ⟶ Z) [has_binary_coproduct W X] [has_binary_coproduct W Y] [has_binary_coproduct W Z] : coprod.map (𝟙 W) (f ≫ g) = coprod.map (𝟙 W) f ≫ coprod.map (𝟙 W) g := by simp /-- If the coproducts `W ⨿ X` and `Y ⨿ Z` exist, then every pair of isomorphisms `f : W ≅ Y` and `g : W ≅ Z` induces a isomorphism `coprod.map_iso f g : W ⨿ X ≅ Y ⨿ Z`. -/ @[simps] def coprod.map_iso {W X Y Z : C} [has_binary_coproduct W X] [has_binary_coproduct Y Z] (f : W ≅ Y) (g : X ≅ Z) : W ⨿ X ≅ Y ⨿ Z := { hom := coprod.map f.hom g.hom, inv := coprod.map f.inv g.inv } instance is_iso_coprod {W X Y Z : C} [has_binary_coproduct W X] [has_binary_coproduct Y Z] (f : W ⟶ Y) (g : X ⟶ Z) [is_iso f] [is_iso g] : is_iso (coprod.map f g) := is_iso.of_iso (coprod.map_iso (as_iso f) (as_iso g)) -- The simp linter says simp can prove the reassoc version of this lemma. @[reassoc, simp] lemma coprod.map_codiag {X Y : C} (f : X ⟶ Y) [has_binary_coproduct X X] [has_binary_coproduct Y Y] : coprod.map f f ≫ codiag Y = codiag X ≫ f := by simp -- The simp linter says simp can prove the reassoc version of this lemma. @[reassoc, simp] lemma coprod.map_inl_inr_codiag {X Y : C} [has_binary_coproduct X Y] [has_binary_coproduct (X ⨿ Y) (X ⨿ Y)] : coprod.map coprod.inl coprod.inr ≫ codiag (X ⨿ Y) = 𝟙 (X ⨿ Y) := by simp -- The simp linter says simp can prove the reassoc version of this lemma. @[reassoc, simp] lemma coprod.map_comp_inl_inr_codiag [has_colimits_of_shape (discrete walking_pair) C] {X X' Y Y' : C} (g : X ⟶ Y) (g' : X' ⟶ Y') : coprod.map (g ≫ coprod.inl) (g' ≫ coprod.inr) ≫ codiag (Y ⨿ Y') = coprod.map g g' := by simp end coprod_lemmas variables (C) /-- `has_binary_products` represents a choice of product for every pair of objects. See https://stacks.math.columbia.edu/tag/001T. -/ abbreviation has_binary_products := has_limits_of_shape (discrete walking_pair) C /-- `has_binary_coproducts` represents a choice of coproduct for every pair of objects. See https://stacks.math.columbia.edu/tag/04AP. -/ abbreviation has_binary_coproducts := has_colimits_of_shape (discrete walking_pair) C /-- If `C` has all limits of diagrams `pair X Y`, then it has all binary products -/ lemma has_binary_products_of_has_limit_pair [Π {X Y : C}, has_limit (pair X Y)] : has_binary_products C := { has_limit := λ F, has_limit_of_iso (diagram_iso_pair F).symm } /-- If `C` has all colimits of diagrams `pair X Y`, then it has all binary coproducts -/ lemma has_binary_coproducts_of_has_colimit_pair [Π {X Y : C}, has_colimit (pair X Y)] : has_binary_coproducts C := { has_colimit := λ F, has_colimit_of_iso (diagram_iso_pair F) } section variables {C} /-- The braiding isomorphism which swaps a binary product. -/ @[simps] def prod.braiding (P Q : C) [has_binary_product P Q] [has_binary_product Q P] : P ⨯ Q ≅ Q ⨯ P := { hom := prod.lift prod.snd prod.fst, inv := prod.lift prod.snd prod.fst } /-- The braiding isomorphism can be passed through a map by swapping the order. -/ @[reassoc] lemma braid_natural [has_binary_products C] {W X Y Z : C} (f : X ⟶ Y) (g : Z ⟶ W) : prod.map f g ≫ (prod.braiding _ _).hom = (prod.braiding _ _).hom ≫ prod.map g f := by simp @[reassoc] lemma prod.symmetry' (P Q : C) [has_binary_product P Q] [has_binary_product Q P] : prod.lift prod.snd prod.fst ≫ prod.lift prod.snd prod.fst = 𝟙 (P ⨯ Q) := (prod.braiding _ _).hom_inv_id /-- The braiding isomorphism is symmetric. -/ @[reassoc] lemma prod.symmetry (P Q : C) [has_binary_product P Q] [has_binary_product Q P] : (prod.braiding P Q).hom ≫ (prod.braiding Q P).hom = 𝟙 _ := (prod.braiding _ _).hom_inv_id /-- The associator isomorphism for binary products. -/ @[simps] def prod.associator [has_binary_products C] (P Q R : C) : (P ⨯ Q) ⨯ R ≅ P ⨯ (Q ⨯ R) := { hom := prod.lift (prod.fst ≫ prod.fst) (prod.lift (prod.fst ≫ prod.snd) prod.snd), inv := prod.lift (prod.lift prod.fst (prod.snd ≫ prod.fst)) (prod.snd ≫ prod.snd) } @[reassoc] lemma prod.pentagon [has_binary_products C] (W X Y Z : C) : prod.map ((prod.associator W X Y).hom) (𝟙 Z) ≫ (prod.associator W (X ⨯ Y) Z).hom ≫ prod.map (𝟙 W) ((prod.associator X Y Z).hom) = (prod.associator (W ⨯ X) Y Z).hom ≫ (prod.associator W X (Y ⨯ Z)).hom := by simp @[reassoc] lemma prod.associator_naturality [has_binary_products C] {X₁ X₂ X₃ Y₁ Y₂ Y₃ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (f₃ : X₃ ⟶ Y₃) : prod.map (prod.map f₁ f₂) f₃ ≫ (prod.associator Y₁ Y₂ Y₃).hom = (prod.associator X₁ X₂ X₃).hom ≫ prod.map f₁ (prod.map f₂ f₃) := by simp variables [has_terminal C] /-- The left unitor isomorphism for binary products with the terminal object. -/ @[simps] def prod.left_unitor (P : C) [has_binary_product (⊤_ C) P] : ⊤_ C ⨯ P ≅ P := { hom := prod.snd, inv := prod.lift (terminal.from P) (𝟙 _) } /-- The right unitor isomorphism for binary products with the terminal object. -/ @[simps] def prod.right_unitor (P : C) [has_binary_product P (⊤_ C)] : P ⨯ ⊤_ C ≅ P := { hom := prod.fst, inv := prod.lift (𝟙 _) (terminal.from P) } @[reassoc] lemma prod.left_unitor_hom_naturality [has_binary_products C] (f : X ⟶ Y) : prod.map (𝟙 _) f ≫ (prod.left_unitor Y).hom = (prod.left_unitor X).hom ≫ f := prod.map_snd _ _ @[reassoc] lemma prod.left_unitor_inv_naturality [has_binary_products C] (f : X ⟶ Y) : (prod.left_unitor X).inv ≫ prod.map (𝟙 _) f = f ≫ (prod.left_unitor Y).inv := by rw [iso.inv_comp_eq, ← category.assoc, iso.eq_comp_inv, prod.left_unitor_hom_naturality] @[reassoc] lemma prod.right_unitor_hom_naturality [has_binary_products C] (f : X ⟶ Y) : prod.map f (𝟙 _) ≫ (prod.right_unitor Y).hom = (prod.right_unitor X).hom ≫ f := prod.map_fst _ _ @[reassoc] lemma prod_right_unitor_inv_naturality [has_binary_products C] (f : X ⟶ Y) : (prod.right_unitor X).inv ≫ prod.map f (𝟙 _) = f ≫ (prod.right_unitor Y).inv := by rw [iso.inv_comp_eq, ← category.assoc, iso.eq_comp_inv, prod.right_unitor_hom_naturality] lemma prod.triangle [has_binary_products C] (X Y : C) : (prod.associator X (⊤_ C) Y).hom ≫ prod.map (𝟙 X) ((prod.left_unitor Y).hom) = prod.map ((prod.right_unitor X).hom) (𝟙 Y) := by tidy end section variables {C} [has_binary_coproducts C] /-- The braiding isomorphism which swaps a binary coproduct. -/ @[simps] def coprod.braiding (P Q : C) : P ⨿ Q ≅ Q ⨿ P := { hom := coprod.desc coprod.inr coprod.inl, inv := coprod.desc coprod.inr coprod.inl } @[reassoc] lemma coprod.symmetry' (P Q : C) : coprod.desc coprod.inr coprod.inl ≫ coprod.desc coprod.inr coprod.inl = 𝟙 (P ⨿ Q) := (coprod.braiding _ _).hom_inv_id /-- The braiding isomorphism is symmetric. -/ lemma coprod.symmetry (P Q : C) : (coprod.braiding P Q).hom ≫ (coprod.braiding Q P).hom = 𝟙 _ := coprod.symmetry' _ _ /-- The associator isomorphism for binary coproducts. -/ @[simps] def coprod.associator (P Q R : C) : (P ⨿ Q) ⨿ R ≅ P ⨿ (Q ⨿ R) := { hom := coprod.desc (coprod.desc coprod.inl (coprod.inl ≫ coprod.inr)) (coprod.inr ≫ coprod.inr), inv := coprod.desc (coprod.inl ≫ coprod.inl) (coprod.desc (coprod.inr ≫ coprod.inl) coprod.inr) } lemma coprod.pentagon (W X Y Z : C) : coprod.map ((coprod.associator W X Y).hom) (𝟙 Z) ≫ (coprod.associator W (X ⨿ Y) Z).hom ≫ coprod.map (𝟙 W) ((coprod.associator X Y Z).hom) = (coprod.associator (W ⨿ X) Y Z).hom ≫ (coprod.associator W X (Y ⨿ Z)).hom := by simp lemma coprod.associator_naturality {X₁ X₂ X₃ Y₁ Y₂ Y₃ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (f₃ : X₃ ⟶ Y₃) : coprod.map (coprod.map f₁ f₂) f₃ ≫ (coprod.associator Y₁ Y₂ Y₃).hom = (coprod.associator X₁ X₂ X₃).hom ≫ coprod.map f₁ (coprod.map f₂ f₃) := by simp variables [has_initial C] /-- The left unitor isomorphism for binary coproducts with the initial object. -/ @[simps] def coprod.left_unitor (P : C) : ⊥_ C ⨿ P ≅ P := { hom := coprod.desc (initial.to P) (𝟙 _), inv := coprod.inr } /-- The right unitor isomorphism for binary coproducts with the initial object. -/ @[simps] def coprod.right_unitor (P : C) : P ⨿ ⊥_ C ≅ P := { hom := coprod.desc (𝟙 _) (initial.to P), inv := coprod.inl } lemma coprod.triangle (X Y : C) : (coprod.associator X (⊥_ C) Y).hom ≫ coprod.map (𝟙 X) ((coprod.left_unitor Y).hom) = coprod.map ((coprod.right_unitor X).hom) (𝟙 Y) := by tidy end section prod_functor variables {C} [has_binary_products C] -- FIXME deterministic timeout with `-T50000` /-- The binary product functor. -/ @[simps] def prod.functor : C ⥤ C ⥤ C := { obj := λ X, { obj := λ Y, X ⨯ Y, map := λ Y Z, prod.map (𝟙 X) }, map := λ Y Z f, { app := λ T, prod.map f (𝟙 T) }} /-- The product functor can be decomposed. -/ def prod.functor_left_comp (X Y : C) : prod.functor.obj (X ⨯ Y) ≅ prod.functor.obj Y ⋙ prod.functor.obj X := nat_iso.of_components (prod.associator _ _) (by tidy) end prod_functor section prod_comparison variables {C} {D : Type u₂} [category.{v} D] variables (F : C ⥤ D) {A A' B B' : C} variables [has_binary_product A B] [has_binary_product A' B'] variables [has_binary_product (F.obj A) (F.obj B)] [has_binary_product (F.obj A') (F.obj B')] /-- The product comparison morphism. In `category_theory/limits/preserves` we show this is always an iso iff F preserves binary products. -/ def prod_comparison (F : C ⥤ D) (A B : C) [has_binary_product A B] [has_binary_product (F.obj A) (F.obj B)] : F.obj (A ⨯ B) ⟶ F.obj A ⨯ F.obj B := prod.lift (F.map prod.fst) (F.map prod.snd) @[simp] lemma prod_comparison_fst : prod_comparison F A B ≫ prod.fst = F.map prod.fst := prod.lift_fst _ _ @[simp] lemma prod_comparison_snd : prod_comparison F A B ≫ prod.snd = F.map prod.snd := prod.lift_snd _ _ /-- Naturality of the prod_comparison morphism in both arguments. -/ @[reassoc] lemma prod_comparison_natural (f : A ⟶ A') (g : B ⟶ B') : F.map (prod.map f g) ≫ prod_comparison F A' B' = prod_comparison F A B ≫ prod.map (F.map f) (F.map g) := begin rw [prod_comparison, prod_comparison, prod.lift_map, ← F.map_comp, ← F.map_comp, prod.comp_lift, ← F.map_comp, prod.map_fst, ← F.map_comp, prod.map_snd] end /-- The product comparison morphism from `F(A ⨯ -)` to `FA ⨯ F-`, whose components are given by `prod_comparison`. -/ @[simps] def prod_comparison_nat_trans [has_binary_products C] [has_binary_products D] (F : C ⥤ D) (A : C) : prod.functor.obj A ⋙ F ⟶ F ⋙ prod.functor.obj (F.obj A) := { app := λ B, prod_comparison F A B, naturality' := λ B B' f, by simp [prod_comparison_natural] } @[reassoc] lemma inv_prod_comparison_map_fst [is_iso (prod_comparison F A B)] : inv (prod_comparison F A B) ≫ F.map prod.fst = prod.fst := by simp [is_iso.inv_comp_eq] @[reassoc] lemma inv_prod_comparison_map_snd [is_iso (prod_comparison F A B)] : inv (prod_comparison F A B) ≫ F.map prod.snd = prod.snd := by simp [is_iso.inv_comp_eq] /-- If the product comparison morphism is an iso, its inverse is natural. -/ @[reassoc] lemma prod_comparison_inv_natural (f : A ⟶ A') (g : B ⟶ B') [is_iso (prod_comparison F A B)] [is_iso (prod_comparison F A' B')] : inv (prod_comparison F A B) ≫ F.map (prod.map f g) = prod.map (F.map f) (F.map g) ≫ inv (prod_comparison F A' B') := by rw [is_iso.eq_comp_inv, category.assoc, is_iso.inv_comp_eq, prod_comparison_natural] /-- The natural isomorphism `F(A ⨯ -) ≅ FA ⨯ F-`, provided each `prod_comparison F A B` is an isomorphism (as `B` changes). -/ @[simps {rhs_md := semireducible}] def prod_comparison_nat_iso [has_binary_products C] [has_binary_products D] (A : C) [∀ B, is_iso (prod_comparison F A B)] : prod.functor.obj A ⋙ F ≅ F ⋙ prod.functor.obj (F.obj A) := { hom := prod_comparison_nat_trans F A ..nat_iso.is_iso_of_is_iso_app ⟨_, _⟩ } end prod_comparison end category_theory.limits
f62c47d06e0f49942f743a78fedd2ffb27eadf02
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/topology/metric_space/antilipschitz.lean
a3ae855ab0025a7e64090f346d82861a680c5402
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
5,184
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import topology.metric_space.lipschitz /-! # Antilipschitz functions We say that a map `f : α → β` between two (extended) metric spaces is `antilipschitz_with K`, `K ≥ 0`, if for all `x, y` we have `edist x y ≤ K * edist (f x) (f y)`. For a metric space, the latter inequality is equivalent to `dist x y ≤ K * dist (f x) (f y)`. ## Implementation notes The parameter `K` has type `nnreal`. This way we avoid conjuction in the definition and have coercions both to `ℝ` and `ennreal`. We do not require `0 < K` in the definition, mostly because we do not have a `posreal` type. -/ variables {α : Type*} {β : Type*} {γ : Type*} open_locale nnreal open set /-- We say that `f : α → β` is `antilipschitz_with K` if for any two points `x`, `y` we have `K * edist x y ≤ edist (f x) (f y)`. -/ def antilipschitz_with [emetric_space α] [emetric_space β] (K : ℝ≥0) (f : α → β) := ∀ x y, edist x y ≤ K * edist (f x) (f y) lemma antilipschitz_with_iff_le_mul_dist [metric_space α] [metric_space β] {K : ℝ≥0} {f : α → β} : antilipschitz_with K f ↔ ∀ x y, dist x y ≤ K * dist (f x) (f y) := by { simp only [antilipschitz_with, edist_nndist, dist_nndist], norm_cast } alias antilipschitz_with_iff_le_mul_dist ↔ antilipschitz_with.le_mul_dist antilipschitz_with.of_le_mul_dist lemma antilipschitz_with.mul_le_dist [metric_space α] [metric_space β] {K : ℝ≥0} {f : α → β} (hf : antilipschitz_with K f) (x y : α) : ↑K⁻¹ * dist x y ≤ dist (f x) (f y) := begin by_cases hK : K = 0, by simp [hK, dist_nonneg], rw [nnreal.coe_inv, ← div_eq_inv_mul], apply div_le_of_le_mul (nnreal.coe_pos.2 $ zero_lt_iff_ne_zero.2 hK), exact hf.le_mul_dist x y end namespace antilipschitz_with variables [emetric_space α] [emetric_space β] [emetric_space γ] {K : ℝ≥0} {f : α → β} protected lemma injective (hf : antilipschitz_with K f) : function.injective f := λ x y h, by simpa only [h, edist_self, mul_zero, edist_le_zero] using hf x y lemma mul_le_edist (hf : antilipschitz_with K f) (x y : α) : ↑K⁻¹ * edist x y ≤ edist (f x) (f y) := begin by_cases hK : K = 0, by simp [hK], rw [ennreal.coe_inv hK, mul_comm, ← ennreal.div_def], apply ennreal.div_le_of_le_mul, rw mul_comm, exact hf x y end protected lemma id : antilipschitz_with 1 (id : α → α) := λ x y, by simp only [ennreal.coe_one, one_mul, id, le_refl] lemma comp {Kg : ℝ≥0} {g : β → γ} (hg : antilipschitz_with Kg g) {Kf : ℝ≥0} {f : α → β} (hf : antilipschitz_with Kf f) : antilipschitz_with (Kf * Kg) (g ∘ f) := λ x y, calc edist x y ≤ Kf * edist (f x) (f y) : hf x y ... ≤ Kf * (Kg * edist (g (f x)) (g (f y))) : ennreal.mul_left_mono (hg _ _) ... = _ : by rw [ennreal.coe_mul, mul_assoc] lemma restrict (hf : antilipschitz_with K f) (s : set α) : antilipschitz_with K (s.restrict f) := λ x y, hf x y lemma cod_restrict (hf : antilipschitz_with K f) {s : set β} (hs : ∀ x, f x ∈ s) : antilipschitz_with K (s.cod_restrict f hs) := λ x y, hf x y lemma to_right_inv_on' {s : set α} (hf : antilipschitz_with K (s.restrict f)) {g : β → α} {t : set β} (g_maps : maps_to g t s) (g_inv : right_inv_on g f t) : lipschitz_with K (t.restrict g) := λ x y, by simpa only [restrict_apply, g_inv x.mem, g_inv y.mem, subtype.edist_eq, subtype.coe_mk] using hf ⟨g x, g_maps x.mem⟩ ⟨g y, g_maps y.mem⟩ lemma to_right_inv_on (hf : antilipschitz_with K f) {g : β → α} {t : set β} (h : right_inv_on g f t) : lipschitz_with K (t.restrict g) := (hf.restrict univ).to_right_inv_on' (maps_to_univ g t) h lemma to_right_inverse (hf : antilipschitz_with K f) {g : β → α} (hg : function.right_inverse g f) : lipschitz_with K g := begin intros x y, have := hf (g x) (g y), rwa [hg x, hg y] at this end lemma uniform_embedding (hf : antilipschitz_with K f) (hfc : uniform_continuous f) : uniform_embedding f := begin refine emetric.uniform_embedding_iff.2 ⟨hf.injective, hfc, λ δ δ0, _⟩, by_cases hK : K = 0, { refine ⟨1, ennreal.zero_lt_one, λ x y _, lt_of_le_of_lt _ δ0⟩, simpa only [hK, ennreal.coe_zero, zero_mul] using hf x y }, { refine ⟨K⁻¹ * δ, _, λ x y hxy, lt_of_le_of_lt (hf x y) _⟩, { exact canonically_ordered_semiring.mul_pos.2 ⟨ennreal.inv_pos.2 ennreal.coe_ne_top, δ0⟩ }, { rw [mul_comm, ← ennreal.div_def] at hxy, have := ennreal.mul_lt_of_lt_div hxy, rwa mul_comm } } end lemma subtype_coe (s : set α) : antilipschitz_with 1 (coe : s → α) := antilipschitz_with.id.restrict s lemma of_subsingleton [subsingleton α] {K : ℝ≥0} : antilipschitz_with K f := λ x y, by simp only [subsingleton.elim x y, edist_self, zero_le] end antilipschitz_with lemma lipschitz_with.to_right_inverse [emetric_space α] [emetric_space β] {K : ℝ≥0} {f : α → β} (hf : lipschitz_with K f) {g : β → α} (hg : function.right_inverse g f) : antilipschitz_with K g := λ x y, by simpa only [hg _] using hf (g x) (g y)
2c1b1d854a0ec966d10c0f93d4a24582510f857b
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/diamond1.lean
54d93ea796c725b3ad7cd2cfa774bf20f6dc523d
[ "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
635
lean
structure Bar (α : Type) where a : α b : Nat → α structure Baz (α : Type) where a : α → α c : Bool → α d : Nat set_option structureDiamondWarning false in structure Foo (α : Type) extends Bar α, Baz α -- Error: parent field type mismatch set_option structureDiamondWarning false in structure Foo (α : Type) extends Bar (α → α), Baz α #print Foo def f (x : Nat) : Foo Nat := { a := fun y => x + y b := (. + .) c := fun _ => x d := x } #print f set_option structureDiamondWarning true in structure Foo' (α : Type) extends Bar (α → α), Baz α -- Warning: parent field duplication