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
3375a7af539d92bb6ec23773e68a23fd7149cf18
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/order/filter/lift.lean
56a3f85869b3114b6632b9737622c4ca21e7a1f4
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
18,035
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Lift filters along filter and set functions. -/ import order.filter.basic open lattice set local attribute [instance] classical.prop_decidable namespace filter variables {α : Type*} {β : Type*} {γ : Type*} {ι : Sort*} section lift /-- A variant on `bind` using a function `g` taking a set instead of a member of `α`. This is essentially a push-forward along a function mapping each set to a filter. -/ protected def lift (f : filter α) (g : set α → filter β) := ⨅s ∈ f.sets, g s variables {f f₁ f₂ : filter α} {g g₁ g₂ : set α → filter β} lemma lift_sets_eq (hg : monotone g) : (f.lift g).sets = (⋃t∈f.sets, (g t).sets) := infi_sets_eq' (assume s hs t ht, ⟨s ∩ t, inter_mem_sets hs ht, hg $ inter_subset_left s t, hg $ inter_subset_right s t⟩) ⟨univ, univ_mem_sets⟩ lemma mem_lift {s : set β} {t : set α} (ht : t ∈ f.sets) (hs : s ∈ (g t).sets) : s ∈ (f.lift g).sets := le_principal_iff.mp $ show f.lift g ≤ principal s, from infi_le_of_le t $ infi_le_of_le ht $ le_principal_iff.mpr hs lemma mem_lift_sets (hg : monotone g) {s : set β} : s ∈ (f.lift g).sets ↔ (∃t∈f.sets, s ∈ (g t).sets) := by rw [lift_sets_eq hg]; simp only [mem_Union] lemma lift_le {f : filter α} {g : set α → filter β} {h : filter β} {s : set α} (hs : s ∈ f.sets) (hg : g s ≤ h) : f.lift g ≤ h := infi_le_of_le s $ infi_le_of_le hs $ hg lemma le_lift {f : filter α} {g : set α → filter β} {h : filter β} (hh : ∀s∈f.sets, h ≤ g s) : h ≤ f.lift g := le_infi $ assume s, le_infi $ assume hs, hh s hs lemma lift_mono (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.lift g₁ ≤ f₂.lift g₂ := infi_le_infi $ assume s, infi_le_infi2 $ assume hs, ⟨hf hs, hg s⟩ lemma lift_mono' (hg : ∀s∈f.sets, g₁ s ≤ g₂ s) : f.lift g₁ ≤ f.lift g₂ := infi_le_infi $ assume s, infi_le_infi $ assume hs, hg s hs lemma map_lift_eq {m : β → γ} (hg : monotone g) : map m (f.lift g) = f.lift (map m ∘ g) := have monotone (map m ∘ g), from monotone_comp hg monotone_map, filter_eq $ set.ext $ by simp only [mem_lift_sets, hg, @mem_lift_sets _ _ f _ this, exists_prop, forall_const, mem_map, iff_self, function.comp_app] lemma comap_lift_eq {m : γ → β} (hg : monotone g) : comap m (f.lift g) = f.lift (comap m ∘ g) := have monotone (comap m ∘ g), from monotone_comp hg monotone_comap, filter_eq $ set.ext begin simp only [hg, @mem_lift_sets _ _ f _ this, comap, mem_lift_sets, mem_set_of_eq, exists_prop, function.comp_apply], exact λ s, ⟨λ ⟨b, ⟨a, ha, hb⟩, hs⟩, ⟨a, ha, b, hb, hs⟩, λ ⟨a, ha, b, hb, hs⟩, ⟨b, ⟨a, ha, hb⟩, hs⟩⟩ end theorem comap_lift_eq2 {m : β → α} {g : set β → filter γ} (hg : monotone g) : (comap m f).lift g = f.lift (g ∘ preimage m) := le_antisymm (le_infi $ assume s, le_infi $ assume hs, infi_le_of_le (preimage m s) $ infi_le _ ⟨s, hs, subset.refl _⟩) (le_infi $ assume s, le_infi $ assume ⟨s', hs', (h_sub : preimage m s' ⊆ s)⟩, infi_le_of_le s' $ infi_le_of_le hs' $ hg h_sub) lemma map_lift_eq2 {g : set β → filter γ} {m : α → β} (hg : monotone g) : (map m f).lift g = f.lift (g ∘ image m) := le_antisymm (infi_le_infi2 $ assume s, ⟨image m s, infi_le_infi2 $ assume hs, ⟨ f.sets_of_superset hs $ assume a h, mem_image_of_mem _ h, le_refl _⟩⟩) (infi_le_infi2 $ assume t, ⟨preimage m t, infi_le_infi2 $ assume ht, ⟨ht, hg $ assume x, assume h : x ∈ m '' preimage m t, let ⟨y, hy, h_eq⟩ := h in show x ∈ t, from h_eq ▸ hy⟩⟩) lemma lift_comm {g : filter β} {h : set α → set β → filter γ} : f.lift (λs, g.lift (h s)) = g.lift (λt, f.lift (λs, h s t)) := le_antisymm (le_infi $ assume i, le_infi $ assume hi, le_infi $ assume j, le_infi $ assume hj, infi_le_of_le j $ infi_le_of_le hj $ infi_le_of_le i $ infi_le _ hi) (le_infi $ assume i, le_infi $ assume hi, le_infi $ assume j, le_infi $ assume hj, infi_le_of_le j $ infi_le_of_le hj $ infi_le_of_le i $ infi_le _ hi) lemma lift_assoc {h : set β → filter γ} (hg : monotone g) : (f.lift g).lift h = f.lift (λs, (g s).lift h) := le_antisymm (le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht, infi_le_of_le t $ infi_le _ $ (mem_lift_sets hg).mpr ⟨_, hs, ht⟩) (le_infi $ assume t, le_infi $ assume ht, let ⟨s, hs, h'⟩ := (mem_lift_sets hg).mp ht in infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le t $ infi_le _ h') lemma lift_lift_same_le_lift {g : set α → set α → filter β} : f.lift (λs, f.lift (g s)) ≤ f.lift (λs, g s s) := le_infi $ assume s, le_infi $ assume hs, infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le s $ infi_le _ hs lemma lift_lift_same_eq_lift {g : set α → set α → filter β} (hg₁ : ∀s, monotone (λt, g s t)) (hg₂ : ∀t, monotone (λs, g s t)): f.lift (λs, f.lift (g s)) = f.lift (λs, g s s) := le_antisymm lift_lift_same_le_lift (le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht, infi_le_of_le (s ∩ t) $ infi_le_of_le (inter_mem_sets hs ht) $ calc g (s ∩ t) (s ∩ t) ≤ g s (s ∩ t) : hg₂ (s ∩ t) (inter_subset_left _ _) ... ≤ g s t : hg₁ s (inter_subset_right _ _)) lemma lift_principal {s : set α} (hg : monotone g) : (principal s).lift g = g s := le_antisymm (infi_le_of_le s $ infi_le _ $ subset.refl _) (le_infi $ assume t, le_infi $ assume hi, hg hi) theorem monotone_lift [preorder γ] {f : γ → filter α} {g : γ → set α → filter β} (hf : monotone f) (hg : monotone g) : monotone (λc, (f c).lift (g c)) := assume a b h, lift_mono (hf h) (hg h) lemma lift_neq_bot_iff (hm : monotone g) : (f.lift g ≠ ⊥) ↔ (∀s∈f.sets, g s ≠ ⊥) := classical.by_cases (assume hn : nonempty β, calc f.lift g ≠ ⊥ ↔ (⨅s : { s // s ∈ f.sets}, g s.val) ≠ ⊥ : by simp only [filter.lift, infi_subtype, iff_self, ne.def] ... ↔ (∀s:{ s // s ∈ f.sets}, g s.val ≠ ⊥) : infi_neq_bot_iff_of_directed hn (assume ⟨a, ha⟩ ⟨b, hb⟩, ⟨⟨a ∩ b, inter_mem_sets ha hb⟩, hm $ inter_subset_left _ _, hm $ inter_subset_right _ _⟩) ... ↔ (∀s∈f.sets, g s ≠ ⊥) : ⟨assume h s hs, h ⟨s, hs⟩, assume h ⟨s, hs⟩, h s hs⟩) (assume hn : ¬ nonempty β, have h₁ : f.lift g = ⊥, from filter_eq_bot_of_not_nonempty hn, have h₂ : ∀s, g s = ⊥, from assume s, filter_eq_bot_of_not_nonempty hn, calc (f.lift g ≠ ⊥) ↔ false : by simp only [h₁, iff_self, eq_self_iff_true, not_true, ne.def] ... ↔ (∀s∈f.sets, false) : ⟨false.elim, assume h, h univ univ_mem_sets⟩ ... ↔ (∀s∈f.sets, g s ≠ ⊥) : by simp only [h₂, iff_self, eq_self_iff_true, not_true, ne.def]) @[simp] lemma lift_const {f : filter α} {g : filter β} : f.lift (λx, g) = g := le_antisymm (lift_le univ_mem_sets $ le_refl g) (le_lift $ assume s hs, le_refl g) @[simp] lemma lift_inf {f : filter α} {g h : set α → filter β} : f.lift (λx, g x ⊓ h x) = f.lift g ⊓ f.lift h := by simp only [filter.lift, infi_inf_eq, eq_self_iff_true] @[simp] lemma lift_principal2 {f : filter α} : f.lift principal = f := le_antisymm (assume s hs, mem_lift hs (mem_principal_self s)) (le_infi $ assume s, le_infi $ assume hs, by simp only [hs, le_principal_iff]) lemma lift_infi {f : ι → filter α} {g : set α → filter β} (hι : nonempty ι) (hg : ∀{s t}, g s ⊓ g t = g (s ∩ t)) : (infi f).lift g = (⨅i, (f i).lift g) := le_antisymm (le_infi $ assume i, lift_mono (infi_le _ _) (le_refl _)) (assume s, have g_mono : monotone g, from assume s t h, le_of_inf_eq $ eq.trans hg $ congr_arg g $ inter_eq_self_of_subset_left h, have ∀t∈(infi f).sets, (⨅ (i : ι), filter.lift (f i) g) ≤ g t, from assume t ht, infi_sets_induct ht (let ⟨i⟩ := hι in infi_le_of_le i $ infi_le_of_le univ $ infi_le _ univ_mem_sets) (assume i s₁ s₂ hs₁ hs₂, @hg s₁ s₂ ▸ le_inf (infi_le_of_le i $ infi_le_of_le s₁ $ infi_le _ hs₁) hs₂) (assume s₁ s₂ hs₁ hs₂, le_trans hs₂ $ g_mono hs₁), begin rw [lift_sets_eq g_mono], simp only [mem_Union, exists_imp_distrib], exact assume t ht hs, this t ht hs end) end lift section lift' /-- Specialize `lift` to functions `set α → set β`. This can be viewed as a generalization of `map`. This is essentially a push-forward along a function mapping each set to a set. -/ protected def lift' (f : filter α) (h : set α → set β) := f.lift (principal ∘ h) variables {f f₁ f₂ : filter α} {h h₁ h₂ : set α → set β} lemma mem_lift' {t : set α} (ht : t ∈ f.sets) : h t ∈ (f.lift' h).sets := le_principal_iff.mp $ show f.lift' h ≤ principal (h t), from infi_le_of_le t $ infi_le_of_le ht $ le_refl _ lemma mem_lift'_sets (hh : monotone h) {s : set β} : s ∈ (f.lift' h).sets ↔ (∃t∈f.sets, h t ⊆ s) := have monotone (principal ∘ h), from assume a b h, principal_mono.mpr $ hh h, by simp only [filter.lift', @mem_lift_sets α β f _ this, exists_prop, iff_self, mem_principal_sets, function.comp_app] lemma lift'_le {f : filter α} {g : set α → set β} {h : filter β} {s : set α} (hs : s ∈ f.sets) (hg : principal (g s) ≤ h) : f.lift' g ≤ h := lift_le hs hg lemma lift'_mono (hf : f₁ ≤ f₂) (hh : h₁ ≤ h₂) : f₁.lift' h₁ ≤ f₂.lift' h₂ := lift_mono hf $ assume s, principal_mono.mpr $ hh s lemma lift'_mono' (hh : ∀s∈f.sets, h₁ s ⊆ h₂ s) : f.lift' h₁ ≤ f.lift' h₂ := infi_le_infi $ assume s, infi_le_infi $ assume hs, principal_mono.mpr $ hh s hs lemma lift'_cong (hh : ∀s∈f.sets, h₁ s = h₂ s) : f.lift' h₁ = f.lift' h₂ := le_antisymm (lift'_mono' $ assume s hs, le_of_eq $ hh s hs) (lift'_mono' $ assume s hs, le_of_eq $ (hh s hs).symm) lemma map_lift'_eq {m : β → γ} (hh : monotone h) : map m (f.lift' h) = f.lift' (image m ∘ h) := calc map m (f.lift' h) = f.lift (map m ∘ principal ∘ h) : map_lift_eq $ monotone_comp hh monotone_principal ... = f.lift' (image m ∘ h) : by simp only [(∘), filter.lift', map_principal, eq_self_iff_true] lemma map_lift'_eq2 {g : set β → set γ} {m : α → β} (hg : monotone g) : (map m f).lift' g = f.lift' (g ∘ image m) := map_lift_eq2 $ monotone_comp hg monotone_principal theorem comap_lift'_eq {m : γ → β} (hh : monotone h) : comap m (f.lift' h) = f.lift' (preimage m ∘ h) := calc comap m (f.lift' h) = f.lift (comap m ∘ principal ∘ h) : comap_lift_eq $ monotone_comp hh monotone_principal ... = f.lift' (preimage m ∘ h) : by simp only [(∘), filter.lift', comap_principal, eq_self_iff_true] theorem comap_lift'_eq2 {m : β → α} {g : set β → set γ} (hg : monotone g) : (comap m f).lift' g = f.lift' (g ∘ preimage m) := comap_lift_eq2 $ monotone_comp hg monotone_principal lemma lift'_principal {s : set α} (hh : monotone h) : (principal s).lift' h = principal (h s) := lift_principal $ monotone_comp hh monotone_principal lemma principal_le_lift' {t : set β} (hh : ∀s∈f.sets, t ⊆ h s) : principal t ≤ f.lift' h := le_infi $ assume s, le_infi $ assume hs, principal_mono.mpr (hh s hs) theorem monotone_lift' [preorder γ] {f : γ → filter α} {g : γ → set α → set β} (hf : monotone f) (hg : monotone g) : monotone (λc, (f c).lift' (g c)) := assume a b h, lift'_mono (hf h) (hg h) lemma lift_lift'_assoc {g : set α → set β} {h : set β → filter γ} (hg : monotone g) (hh : monotone h) : (f.lift' g).lift h = f.lift (λs, h (g s)) := calc (f.lift' g).lift h = f.lift (λs, (principal (g s)).lift h) : lift_assoc (monotone_comp hg monotone_principal) ... = f.lift (λs, h (g s)) : by simp only [lift_principal, hh, eq_self_iff_true] lemma lift'_lift'_assoc {g : set α → set β} {h : set β → set γ} (hg : monotone g) (hh : monotone h) : (f.lift' g).lift' h = f.lift' (λs, h (g s)) := lift_lift'_assoc hg (monotone_comp hh monotone_principal) lemma lift'_lift_assoc {g : set α → filter β} {h : set β → set γ} (hg : monotone g) : (f.lift g).lift' h = f.lift (λs, (g s).lift' h) := lift_assoc hg lemma lift_lift'_same_le_lift' {g : set α → set α → set β} : f.lift (λs, f.lift' (g s)) ≤ f.lift' (λs, g s s) := lift_lift_same_le_lift lemma lift_lift'_same_eq_lift' {g : set α → set α → set β} (hg₁ : ∀s, monotone (λt, g s t)) (hg₂ : ∀t, monotone (λs, g s t)): f.lift (λs, f.lift' (g s)) = f.lift' (λs, g s s) := lift_lift_same_eq_lift (assume s, monotone_comp monotone_id $ monotone_comp (hg₁ s) monotone_principal) (assume t, monotone_comp (hg₂ t) monotone_principal) lemma lift'_inf_principal_eq {h : set α → set β} {s : set β} : f.lift' h ⊓ principal s = f.lift' (λt, h t ∩ s) := le_antisymm (le_infi $ assume t, le_infi $ assume ht, calc filter.lift' f h ⊓ principal s ≤ principal (h t) ⊓ principal s : inf_le_inf (infi_le_of_le t $ infi_le _ ht) (le_refl _) ... = _ : by simp only [principal_eq_iff_eq, inf_principal, eq_self_iff_true, function.comp_app]) (le_inf (le_infi $ assume t, le_infi $ assume ht, infi_le_of_le t $ infi_le_of_le ht $ by simp only [le_principal_iff, inter_subset_left, mem_principal_sets, function.comp_app]; exact inter_subset_right _ _) (infi_le_of_le univ $ infi_le_of_le univ_mem_sets $ by simp only [le_principal_iff, inter_subset_right, mem_principal_sets, function.comp_app]; exact inter_subset_left _ _)) lemma lift'_neq_bot_iff (hh : monotone h) : (f.lift' h ≠ ⊥) ↔ (∀s∈f.sets, h s ≠ ∅) := calc (f.lift' h ≠ ⊥) ↔ (∀s∈f.sets, principal (h s) ≠ ⊥) : lift_neq_bot_iff (monotone_comp hh monotone_principal) ... ↔ (∀s∈f.sets, h s ≠ ∅) : by simp only [principal_eq_bot_iff, iff_self, ne.def, principal_eq_bot_iff] @[simp] lemma lift'_id {f : filter α} : f.lift' id = f := lift_principal2 lemma le_lift' {f : filter α} {h : set α → set β} {g : filter β} (h_le : ∀s∈f.sets, h s ∈ g.sets) : g ≤ f.lift' h := le_infi $ assume s, le_infi $ assume hs, by simp only [h_le, le_principal_iff, function.comp_app]; exact h_le s hs lemma lift_infi' {f : ι → filter α} {g : set α → filter β} (hι : nonempty ι) (hf : directed (≥) f) (hg : monotone g) : (infi f).lift g = (⨅i, (f i).lift g) := le_antisymm (le_infi $ assume i, lift_mono (infi_le _ _) (le_refl _)) (assume s, begin rw [lift_sets_eq hg], simp only [mem_Union, exists_imp_distrib, infi_sets_eq hf hι], exact assume t i ht hs, mem_infi_sets i $ mem_lift ht hs end) lemma lift'_infi {f : ι → filter α} {g : set α → set β} (hι : nonempty ι) (hg : ∀{s t}, g s ∩ g t = g (s ∩ t)) : (infi f).lift' g = (⨅i, (f i).lift' g) := lift_infi hι $ by simp only [principal_eq_iff_eq, inf_principal, function.comp_app]; apply assume s t, hg theorem comap_eq_lift' {f : filter β} {m : α → β} : comap m f = f.lift' (preimage m) := filter_eq $ set.ext $ by simp only [mem_lift'_sets, monotone_preimage, comap, exists_prop, forall_const, iff_self, mem_set_of_eq] end lift' section prod variables {f : filter α} lemma prod_def {f : filter α} {g : filter β} : f.prod g = (f.lift $ λs, g.lift' $ set.prod s) := have ∀(s:set α) (t : set β), principal (set.prod s t) = (principal s).comap prod.fst ⊓ (principal t).comap prod.snd, by simp only [principal_eq_iff_eq, comap_principal, inf_principal]; intros; refl, begin simp only [filter.lift', function.comp, this, -comap_principal, lift_inf, lift_const, lift_inf], rw [← comap_lift_eq monotone_principal, ← comap_lift_eq monotone_principal], simp only [filter.prod, lift_principal2, eq_self_iff_true] end lemma prod_same_eq : filter.prod f f = f.lift' (λt, set.prod t t) := by rw [prod_def]; from lift_lift'_same_eq_lift' (assume s, set.monotone_prod monotone_const monotone_id) (assume t, set.monotone_prod monotone_id monotone_const) lemma mem_prod_same_iff {s : set (α×α)} : s ∈ (filter.prod f f).sets ↔ (∃t∈f.sets, set.prod t t ⊆ s) := by rw [prod_same_eq, mem_lift'_sets]; exact set.monotone_prod monotone_id monotone_id lemma tendsto_prod_self_iff {f : α × α → β} {x : filter α} {y : filter β} : filter.tendsto f (filter.prod x x) y ↔ ∀ W ∈ y.sets, ∃ U ∈ x.sets, ∀ (x x' : α), x ∈ U → x' ∈ U → f (x, x') ∈ W := by simp only [tendsto_def, mem_prod_same_iff, prod_sub_preimage_iff, exists_prop, iff_self] variables {α₁ : Type*} {α₂ : Type*} {β₁ : Type*} {β₂ : Type*} lemma prod_lift_lift {f₁ : filter α₁} {f₂ : filter α₂} {g₁ : set α₁ → filter β₁} {g₂ : set α₂ → filter β₂} (hg₁ : monotone g₁) (hg₂ : monotone g₂) : filter.prod (f₁.lift g₁) (f₂.lift g₂) = f₁.lift (λs, f₂.lift (λt, filter.prod (g₁ s) (g₂ t))) := begin simp only [prod_def], rw [lift_assoc], apply congr_arg, funext x, rw [lift_comm], apply congr_arg, funext y, rw [lift'_lift_assoc], exact hg₂, exact hg₁ end lemma prod_lift'_lift' {f₁ : filter α₁} {f₂ : filter α₂} {g₁ : set α₁ → set β₁} {g₂ : set α₂ → set β₂} (hg₁ : monotone g₁) (hg₂ : monotone g₂) : filter.prod (f₁.lift' g₁) (f₂.lift' g₂) = f₁.lift (λs, f₂.lift' (λt, set.prod (g₁ s) (g₂ t))) := begin rw [prod_def, lift_lift'_assoc], apply congr_arg, funext x, rw [lift'_lift'_assoc], exact hg₂, exact set.monotone_prod monotone_const monotone_id, exact hg₁, exact (monotone_lift' monotone_const $ monotone_lam $ assume x, set.monotone_prod monotone_id monotone_const) end end prod end filter
04f01b4d6df013d95ccaf4b3fa4834e33113218d
54f4ad05b219d444b709f56c2f619dd87d14ec29
/test.lean
4a3d07c6678176bfa43a4c34f99482345fb7a4ef
[]
no_license
yizhou7/learning-lean
8efcf838c7276e235a81bd291f467fa43ce56e0a
91fb366c624df6e56e19555b2e482ce767cd8224
refs/heads/master
1,675,649,087,737
1,609,022,281,000
1,609,022,281,000
272,072,779
0
0
null
null
null
null
UTF-8
Lean
false
false
1,912
lean
example (p q r : Prop) : p ∧ (q ∨ r) ↔ (p ∧ q) ∨ (p ∧ r) := begin apply iff.intro, intro h, apply or.elim (and.right h), intro hq, apply or.inl, apply and.intro, apply and.left h, exact hq, intro hr, apply or.inr, apply and.intro, apply and.left h, exact hr, intro h, apply or.elim h, intro a, apply and.intro, apply and.elim_left a, apply or.intro_left, apply and.elim_right a, intro a, apply and.intro, apply and.elim_left a, apply or.intro_right, apply and.elim_right a, end example : ∀ a b c : ℕ, a = b → a = c → c = b := begin intros, transitivity a, symmetry, assumption, assumption, end example (p q r : Prop) : p ∧ (q ∨ r) ↔ (p ∧ q) ∨ (p ∧ r) := begin apply iff.intro, intro h, cases h with hp hqr, cases hqr with hq hr, left, constructor, assumption, assumption, right, constructor, assumption, assumption, intro h, cases h with hpq hpr, cases hpq with hp hq, constructor, assumption, left, assumption, cases hpr with hp hr, constructor,assumption, right, assumption, end example (p q : ℕ → Prop) : (∃ x, p x) → ∃ x, p x ∨ q x := begin intro h, cases h with x px, constructor, left, exact px, end example (p q : ℕ → Prop) : (∃ x, p x ∧ q x) → ∃ x, q x ∧ p x := begin intro, cases a with x hx, cases hx, existsi x, constructor, assumption, assumption, end universes u v def swap_pair {α : Type u} {β : Type v} : α × β → β × α := begin intro p, cases p, constructor, assumption, assumption, end example (x y : nat) (h : y = x + 7) : 2 * y = 2 * (x + 7) := begin rewrite h, end
f7c4e5abb59c79cb7936cf2ea050913c3be8b7ef
3f48345ac9bbaa421714efc9872a0409379bb4ae
/src/examples/kripke_structure/Kripke_Structure.lean
2e2cb0570297624f4d860c5284a88948c45fa949
[]
no_license
QaisHamarneh/Coalgebra-in-Lean
b4318ee6d83780e5c734eb78fed98b1fe8016f7e
bd0452df98bc64b608e5dfd7babc42c301bb6a46
refs/heads/master
1,663,371,200,241
1,661,004,695,000
1,661,004,695,000
209,798,828
0
0
null
null
null
null
UTF-8
Lean
false
false
2,861
lean
import coalgebra.Coalgebra namespace Kripke_Structure universes u v w variable (Properties : Type u) -- Elementry properties structure Kripke (Φ : Type u): Type (max u (v+1)) := (State : Type v) (T : State → set State) (v : State → set Φ) instance Kripke_to_State : has_coe_to_sort (Kripke Properties):= ⟨Type v, λ K, K.State⟩ variables {K K1 K2 : Kripke.{u v} Properties} --notation s ` ⟹ ` t := (s , t) ∈ Kripke.T inductive φ | prop : Properties → φ | TRUE {} : φ | FALSE {} : φ | box {} : φ → φ | diamond {} : φ → φ | and {} : φ → φ → φ | or {} : φ → φ → φ | not {} : φ → φ prefix ` # ` :100 := φ.prop prefix ` □ ` :10 := φ.box prefix ` ◇ ` :20 := φ.diamond prefix ` ¬ ` := φ.not notation p1 ` ∧ ` p2 := φ.and p1 p2 notation p1 ` ∨ ` p2 := φ.or p1 p2 open φ def satisfy (s : K.State): K.State → φ Properties → Prop | s (#p) := p ∈ K.v s | s TRUE := true | s FALSE := false | s □ phi := ∀ t :K.State, t ∈ K.T s → satisfy t phi | s ◇ phi := ∃ t :K.State, t ∈ K.T s → satisfy t phi | s (p1 ∧ p2) := satisfy s p1 ∧ satisfy s p2 | s (p1 ∨ p2) := satisfy s p1 ∨ satisfy s p2 | s ¬ phi := ¬ satisfy s phi notation s ` ⊨ ` phi := satisfy s phi def is_homomorphism {A B : Kripke Properties} (σ : A → B) : Prop := (∀ a₁ a₂ :A , a₂ ∈ A.T a₁ → (σ a₂) ∈ B.T (σ a₁)) ∧ (∀ (a : A) (b : B) , b ∈ B.T (σ a) → ∃ a': A , a' ∈ A.T a ∧ σ a' = b) ∧ (∀ a : A , A.v a = B.v (σ a)) def is_bisimulation (R : set (K1 × K2)) : Prop := ∀ (s1 : K1) (s2 : K2), (s1 , s2) ∈ R → ( K1.v s1 = K2.v s2 ∧ ∀ t1 : K1, K1.T s1 t1 → ∃ t2 :K2, K2.T s2 t2 ∧ (t1 , t2) ∈ R ∧ ∀ t2 : K2, K2.T s2 t2 → ∃ t1 :K1, K1.T s1 t1 ∧ (t1 , t2) ∈ R ) def is_bisimilar {K : Kripke Properties} (B :set (K × K)) : Prop := ∀ R : set (K × K), @is_bisimulation Properties K K R → R ⊆ B notation s1 ` ~ ` s2 := ∃ B : set (Kripke × Kripke), is_bisimilar B ∧ (s1 , s2) ∈ B end Kripke_Structure open Kripke_Structure open Kripke_Structure.Kripke open Kripke_Structure.φ def Kr : Kripke char := { State := nat, T := λ a b, b = a + 1, v := (λ c , {'a'}) }
0b7f1a685973f1d92838dd0a312a4010a9f564d6
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/reserve.lean
143a2b57a09dca2136f25a5258220b98fe080d77
[ "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
177
lean
reserve infix `=?=`:50 reserve infixr `&&&`:25 notation a `=?=` b := eq a b notation a `&&&` b := and a b set_option pp.notation false check λ a b : num, a =?= b &&& b =?= a
a05bbc52641fe181b59dee405e8665aaed6282dd
9cb9db9d79fad57d80ca53543dc07efb7c4f3838
/src/facts/nnreal.lean
f34b2997d21177ffea69ff9b4174ea2b3c0d9508
[]
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
2,752
lean
import data.real.nnreal open_locale nnreal namespace nnreal variables (r' k c c₁ c₂ c₃ : ℝ≥0) instance fact_le_mul_of_one_le_left [hk : fact (1 ≤ k)] [hc : fact (c₁ ≤ c₂)] : fact (c₁ ≤ k * c₂) := calc c₁ = 1 * c₁ : (one_mul _).symm ... ≤ k * c₂ : mul_le_mul' hk hc instance fact_mul_le_of_le_one_left [hk : fact (k ≤ 1)] [hc : fact (c₁ ≤ c₂)] : fact (k * c₁ ≤ c₂) := calc k * c₁ ≤ 1 * c₂ : mul_le_mul' hk hc ... = c₂ : one_mul _ instance fact_le_refl : fact (c ≤ c) := le_rfl instance fact_le_subst_right [fact (c₁ ≤ c₂)] [h : fact (c₂ = c₃)]: fact (c₁ ≤ c₃) := by rwa ← show c₂ = c₃, from h instance fact_le_subst_right' [fact (c₁ ≤ c₂)] [h : fact (c₃ = c₂)]: fact (c₁ ≤ c₃) := by rwa ← show c₂ = c₃, from h.symm instance fact_le_subst_left [fact (c₁ ≤ c₂)] [h : fact (c₁ = c₃)]: fact (c₃ ≤ c₂) := by rwa ← show c₁ = c₃, from h instance fact_le_subst_left' [fact (c₁ ≤ c₂)] [h : fact (c₃ = c₁)]: fact (c₃ ≤ c₂) := by rwa ← show c₁ = c₃, from h.symm instance fact_inv_mul_le [h : fact (0 < r')] : fact (r'⁻¹ * (r' * c) ≤ c) := le_of_eq $ inv_mul_cancel_left' (ne_of_gt h) _ instance fact_mul_le_mul_left [fact (c₁ ≤ c₂)] : fact (r' * c₁ ≤ r' * c₂) := mul_le_mul' le_rfl ‹_› instance fact_mul_le_mul_right [fact (c₁ ≤ c₂)] : fact (c₁ * r' ≤ c₂ * r') := mul_le_mul' ‹_› le_rfl instance fact_le_inv_mul_self [h1 : fact (0 < r')] [h2 : fact (r' ≤ 1)] : fact (c ≤ r'⁻¹ * c) := begin rw mul_comm, apply le_mul_inv_of_mul_le (ne_of_gt h1), nth_rewrite 1 ← mul_one c, exact mul_le_mul (le_of_eq rfl) h2 (le_of_lt h1) zero_le', end instance fact_le_of_add_one_le (q : ℤ) (m : ℕ) [h : fact (q + 1 ≤ m)] : fact (q ≤ m) := (int.le_add_one le_rfl).trans h instance fact_le_max_left (a b c : ℝ≥0) [h : fact (a ≤ b)] : fact (a ≤ max b c) := le_trans h $ le_max_left _ _ instance fact_one_le_mul_self (a : ℝ≥0) [h : fact (1 ≤ a)] : fact (1 ≤ a * a) := calc (1 : ℝ≥0) = 1 * 1 : (mul_one 1).symm ... ≤ a * a : mul_le_mul' h h instance one_le_add {a b : ℝ≥0} [ha : fact (1 ≤ a)] : fact (1 ≤ a + b) := le_trans ha $ by simp instance one_le_add' {a b : ℝ≥0} [hb : fact (1 ≤ b)] : fact (1 ≤ a + b) := le_trans hb $ by simp instance one_le_pow {n : ℕ} {a : ℝ≥0} [h : fact (1 ≤ a)] : fact (1 ≤ a^n) := begin cases n, { simp [le_refl] }, { rwa one_le_pow_iff, apply nat.succ_ne_zero } end instance fact_le_mul_add : fact (c * c₁ + c * c₂ ≤ c * (c₁ + c₂)) := by { rw mul_add, exact le_rfl } end nnreal #lint- only unused_arguments def_lemma doc_blame
fc47887dbe543796ff4fee1e854052e68880d649
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/stage0/src/Lean/Data/Format.lean
d990bf2800f4fb9e8f361faf3e634a7d04aa3f02
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,832
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 universes u v namespace Lean inductive Format | nil : Format | line : Format | text : String → Format | nest (indent : Nat) : Format → Format | compose (flattened : Bool) : Format → Format → Format | choice : Format → Format → Format namespace Format @[export lean_format_append] protected def append (a b : Format) : Format := compose false a b instance : HasAppend Format := ⟨Format.append⟩ instance : HasCoe String Format := ⟨text⟩ instance : Inhabited Format := ⟨nil⟩ def join (xs : List Format) : Format := xs.foldl HasAppend.append "" def isNil : Format → Bool | nil => true | _ => false def flatten : Format → Format | nil => nil | line => text " " | f@(text _) => f | nest _ f => flatten f | choice f _ => flatten f | f@(compose true _ _) => f | f@(compose false f₁ f₂) => compose true (flatten f₁) (flatten f₂) @[export lean_format_group] def group : Format → Format | nil => nil | f@(text _) => f | f@(compose true _ _) => f | f => choice (flatten f) f structure SpaceResult := (found := false) (exceeded := false) (space := 0) @[inline] private def merge (w : Nat) (r₁ : SpaceResult) (r₂ : Thunk SpaceResult) : SpaceResult := if r₁.exceeded || r₁.found then r₁ else let y := r₂.get; if y.exceeded || y.found then y else let newSpace := r₁.space + y.space; { space := newSpace, exceeded := newSpace > w } def spaceUptoLine : Format → Nat → SpaceResult | nil, w => {} | line, w => { found := true } | text s, w => { space := s.length, exceeded := s.length > w } | compose _ f₁ f₂, w => merge w (spaceUptoLine f₁ w) (spaceUptoLine f₂ w) | nest _ f, w => spaceUptoLine f w | choice f₁ f₂, w => spaceUptoLine f₂ w def spaceUptoLine' : List (Nat × Format) → Nat → SpaceResult | [], w => {} | p::ps, w => merge w (spaceUptoLine p.2 w) (spaceUptoLine' ps w) partial def be : Nat → Nat → String → List (Nat × Format) → String | w, k, out, [] => out | w, k, out, (i, nil)::z => be w k out z | w, k, out, (i, (compose _ f₁ f₂))::z => be w k out ((i, f₁)::(i, f₂)::z) | w, k, out, (i, (nest n f))::z => be w k out ((i+n, f)::z) | w, k, out, (i, text s)::z => be w (k + s.length) (out ++ s) z | w, k, out, (i, line)::z => be w i ((out ++ "\n").pushn ' ' i) z | w, k, out, (i, choice f₁ f₂)::z => let r := merge w (spaceUptoLine f₁ w) (spaceUptoLine' z w); if r.exceeded then be w k out ((i, f₂)::z) else be w k out ((i, f₁)::z) @[inline] def bracket (l : String) (f : Format) (r : String) : Format := group (nest l.length $ l ++ f ++ r) @[inline] def paren (f : Format) : Format := bracket "(" f ")" @[inline] def sbracket (f : Format) : Format := bracket "[" f "]" def defIndent := 4 def defUnicode := true def defWidth := 120 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 @[init] def indentOption : IO Unit := registerOption `format.indent { defValue := defIndent, group := "format", descr := "indentation" } @[init] def unicodeOption : IO Unit := registerOption `format.unicode { defValue := defUnicode, group := "format", descr := "unicode characters" } @[init] def widthOption : IO Unit := registerOption `format.width { defValue := defWidth, group := "format", descr := "line width" } @[export lean_format_pretty] def prettyAux (f : Format) (w : Nat := defWidth) : String := be w 0 "" [(0, f)] def pretty (f : Format) (o : Options := {}) : String := prettyAux f (getWidth o) end Format open Lean.Format class HasFormat (α : Type u) := (format : α → Format) export Lean.HasFormat (format) def fmt {α : Type u} [HasFormat α] : α → Format := format instance toStringToFormat {α : Type u} [HasToString α] : HasFormat α := ⟨text ∘ toString⟩ -- note: must take precendence over the above instance to avoid premature formatting instance formatHasFormat : HasFormat Format := ⟨id⟩ instance stringHasFormat : HasFormat String := ⟨Format.text⟩ def Format.joinSep {α : Type u} [HasFormat α] : List α → Format → Format | [], sep => nil | [a], sep => format a | a::as, sep => format a ++ sep ++ Format.joinSep as sep def Format.prefixJoin {α : Type u} [HasFormat α] (pre : Format) : List α → Format | [] => nil | a::as => pre ++ format a ++ Format.prefixJoin as def Format.joinSuffix {α : Type u} [HasFormat α] : List α → Format → Format | [], suffix => nil | a::as, suffix => format a ++ suffix ++ Format.joinSuffix as suffix def List.format {α : Type u} [HasFormat α] : List α → Format | [] => "[]" | xs => sbracket $ Format.joinSep xs ("," ++ line) instance listHasFormat {α : Type u} [HasFormat α] : HasFormat (List α) := ⟨List.format⟩ instance arrayHasFormat {α : Type u} [HasFormat α] : HasFormat (Array α) := ⟨fun a => "#" ++ fmt a.toList⟩ def Option.format {α : Type u} [HasFormat α] : Option α → Format | none => "none" | some a => "some " ++ fmt a instance optionHasFormat {α : Type u} [HasFormat α] : HasFormat (Option α) := ⟨Option.format⟩ instance prodHasFormat {α : Type u} {β : Type v} [HasFormat α] [HasFormat β] : HasFormat (Prod α β) := ⟨fun ⟨a, b⟩ => paren $ format a ++ "," ++ line ++ format b⟩ def Format.joinArraySep {α : Type u} [HasFormat α] (a : Array α) (sep : Format) : Format := a.iterate nil (fun i a r => if i.val > 0 then r ++ sep ++ format a else r ++ format a) instance natHasFormat : HasFormat Nat := ⟨fun n => toString n⟩ instance uint16HasFormat : HasFormat UInt16 := ⟨fun n => toString n⟩ instance uint32HasFormat : HasFormat UInt32 := ⟨fun n => toString n⟩ instance uint64HasFormat : HasFormat UInt64 := ⟨fun n => toString n⟩ instance usizeHasFormat : HasFormat USize := ⟨fun n => toString n⟩ instance nameHasFormat : HasFormat Name := ⟨fun n => n.toString⟩ protected def Format.repr : Format → Format | nil => "Format.nil" | line => "Format.line" | text s => paren $ "Format.text" ++ line ++ repr s | nest n f => paren $ "Format.nest" ++ line ++ repr n ++ line ++ Format.repr f | compose b f₁ f₂ => paren $ "Format.compose " ++ repr b ++ line ++ Format.repr f₁ ++ line ++ Format.repr f₂ | choice f₁ f₂ => paren $ "Format.choice" ++ line ++ Format.repr f₁ ++ line ++ Format.repr f₂ instance formatHasToString : HasToString Format := ⟨Format.pretty⟩ instance : HasRepr Format := ⟨Format.pretty ∘ Format.repr⟩ def formatDataValue : DataValue → 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 dataValueHasFormat : HasFormat DataValue := ⟨formatDataValue⟩ def formatEntry : Name × DataValue → Format | (n, v) => format n ++ " := " ++ format v instance entryHasFormat : HasFormat (Name × DataValue) := ⟨formatEntry⟩ def formatKVMap (m : KVMap) : Format := sbracket (Format.joinSep m.entries ", ") instance kvMapHasFormat : HasFormat KVMap := ⟨formatKVMap⟩ end Lean def String.toFormat (s : String) : Lean.Format := Lean.Format.joinSep (s.splitOn "\n") Lean.Format.line
27a89f78ceece03e7444cc8c49f0cbff3eb2168e
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/implicit_after_auto_param_bug.lean
58462b60a463fe1abdf6ca703e40c94729aef189
[ "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
670
lean
meta def default_f_lt := `[apply has_lt.lt] def f (α : Type) (lt : α → α → Prop . default_f_lt) [decidable_rel lt] : Type := α example : id (f nat) = nat := rfl example : f nat = nat := rfl def mk {α : Type} (a : α) (lt : α → α → Prop . default_f_lt) [decidable_rel lt] : f α lt := a def f.to_val {α : Type} {lt : α → α → Prop} {h : decidable_rel lt} (v : @f α lt h) : α := v instance repr_f (α : Type) (lt : α → α → Prop) (d : decidable_rel lt) [has_repr α] : has_repr (@f α lt d) := ⟨λ a : α, repr a⟩ #check f nat #check id (f nat) #check mk 1 #check (mk 1).to_val #eval f.to_val (mk 1) #eval mk 1 #eval id (mk 1)
e7df166474bd87349408955ba819a892d2620870
aa5a655c05e5359a70646b7154e7cac59f0b4132
/stage0/src/Init.lean
8e850e3eb096668175009fb36ac6c12173c58be5
[ "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
423
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Prelude import Init.Notation import Init.Core import Init.Control import Init.Data.Basic import Init.WF import Init.Data import Init.System import Init.Util import Init.Fix import Init.Meta import Init.NotationExtra import Init.SimpLemmas
2005388b0deb1eac1e6882da25fc2a198dd79df2
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/set_theory/surreal/basic.lean
fe4a17e045c3c2712f9f12d91ab1463543a7569e
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,149
lean
/- Copyright (c) 2019 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Scott Morrison -/ import set_theory.game /-! # Surreal numbers The basic theory of surreal numbers, built on top of the theory of combinatorial (pre-)games. A pregame is `numeric` if all the Left options are strictly smaller than all the Right options, and all those options are themselves numeric. In terms of combinatorial games, the numeric games have "frozen"; you can only make your position worse by playing, and Left is some definite "number" of moves ahead (or behind) Right. A surreal number is an equivalence class of numeric pregames. In fact, the surreals form a complete ordered field, containing a copy of the reals (and much else besides!) but we do not yet have a complete development. ## Order properties Surreal numbers inherit the relations `≤` and `<` from games, and these relations satisfy the axioms of a partial order (recall that `x < y ↔ x ≤ y ∧ ¬ y ≤ x` did not hold for games). ## Algebraic operations We show that the surreals form a linear ordered commutative group. One can also map all the ordinals into the surreals! ### Multiplication of surreal numbers The definition of multiplication for surreal numbers is surprisingly difficult and is currently missing in the library. A sample proof can be found in Theorem 3.8 in the second reference below. The difficulty lies in the length of the proof and the number of theorems that need to proven simultaneously. This will make for a fun and challenging project. ## References * [Conway, *On numbers and games*][conway2001] * [Schleicher, Stoll, *An introduction to Conway's games and numbers*][schleicher_stoll] -/ universes u local infix ` ≈ ` := pgame.equiv namespace pgame /-- A pre-game is numeric if everything in the L set is less than everything in the R set, and all the elements of L and R are also numeric. -/ def numeric : pgame → Prop | ⟨l, r, L, R⟩ := (∀ i j, L i < R j) ∧ (∀ i, numeric (L i)) ∧ (∀ i, numeric (R i)) lemma numeric.move_left {x : pgame} (o : numeric x) (i : x.left_moves) : numeric (x.move_left i) := begin cases x with xl xr xL xR, exact o.2.1 i, end lemma numeric.move_right {x : pgame} (o : numeric x) (j : x.right_moves) : numeric (x.move_right j) := begin cases x with xl xr xL xR, exact o.2.2 j, end @[elab_as_eliminator] theorem numeric_rec {C : pgame → Prop} (H : ∀ l r (L : l → pgame) (R : r → pgame), (∀ i j, L i < R j) → (∀ i, numeric (L i)) → (∀ i, numeric (R i)) → (∀ i, C (L i)) → (∀ i, C (R i)) → C ⟨l, r, L, R⟩) : ∀ x, numeric x → C x | ⟨l, r, L, R⟩ ⟨h, hl, hr⟩ := H _ _ _ _ h hl hr (λ i, numeric_rec _ (hl i)) (λ i, numeric_rec _ (hr i)) theorem lt_asymm {x y : pgame} (ox : numeric x) (oy : numeric y) : x < y → ¬ y < x := begin refine numeric_rec (λ xl xr xL xR hx oxl oxr IHxl IHxr, _) x ox y oy, refine numeric_rec (λ yl yr yL yR hy oyl oyr IHyl IHyr, _), rw [mk_lt_mk, mk_lt_mk], rintro (⟨i, h₁⟩ | ⟨j, h₁⟩) (⟨i, h₂⟩ | ⟨j, h₂⟩), { exact IHxl _ _ (oyl _) (lt_of_le_mk h₁) (lt_of_le_mk h₂) }, { exact not_lt.2 (le_trans h₂ h₁) (hy _ _) }, { exact not_lt.2 (le_trans h₁ h₂) (hx _ _) }, { exact IHxr _ _ (oyr _) (lt_of_mk_le h₁) (lt_of_mk_le h₂) }, end theorem le_of_lt {x y : pgame} (ox : numeric x) (oy : numeric y) (h : x < y) : x ≤ y := not_lt.1 (lt_asymm ox oy h) /-- On numeric pre-games, `<` and `≤` satisfy the axioms of a partial order (even though they don't on all pre-games). -/ theorem lt_iff_le_not_le {x y : pgame} (ox : numeric x) (oy : numeric y) : x < y ↔ x ≤ y ∧ ¬ y ≤ x := ⟨λ h, ⟨le_of_lt ox oy h, not_le.2 h⟩, λ h, not_le.1 h.2⟩ theorem numeric_zero : numeric 0 := ⟨by rintros ⟨⟩ ⟨⟩, ⟨by rintros ⟨⟩, by rintros ⟨⟩⟩⟩ theorem numeric_one : numeric 1 := ⟨by rintros ⟨⟩ ⟨⟩, ⟨λ x, numeric_zero, by rintros ⟨⟩⟩⟩ theorem numeric_neg : Π {x : pgame} (o : numeric x), numeric (-x) | ⟨l, r, L, R⟩ o := ⟨λ j i, lt_iff_neg_gt.1 (o.1 i j), ⟨λ j, numeric_neg (o.2.2 j), λ i, numeric_neg (o.2.1 i)⟩⟩ theorem numeric.move_left_lt {x : pgame.{u}} (o : numeric x) (i : x.left_moves) : x.move_left i < x := begin rw lt_def_le, left, use i, end theorem numeric.move_left_le {x : pgame} (o : numeric x) (i : x.left_moves) : x.move_left i ≤ x := le_of_lt (o.move_left i) o (o.move_left_lt i) theorem numeric.lt_move_right {x : pgame} (o : numeric x) (j : x.right_moves) : x < x.move_right j := begin rw lt_def_le, right, use j, end theorem numeric.le_move_right {x : pgame} (o : numeric x) (j : x.right_moves) : x ≤ x.move_right j := le_of_lt o (o.move_right j) (o.lt_move_right j) theorem add_lt_add {w x y z : pgame.{u}} (ow : numeric w) (ox : numeric x) (oy : numeric y) (oz : numeric z) (hwx : w < x) (hyz : y < z) : w + y < x + z := begin rw lt_def_le at *, rcases hwx with ⟨ix, hix⟩|⟨jw, hjw⟩; rcases hyz with ⟨iz, hiz⟩|⟨jy, hjy⟩, { left, use (left_moves_add x z).symm (sum.inl ix), simp only [add_move_left_inl], calc w + y ≤ move_left x ix + y : add_le_add_right hix ... ≤ move_left x ix + move_left z iz : add_le_add_left hiz ... ≤ move_left x ix + z : add_le_add_left (oz.move_left_le iz) }, { left, use (left_moves_add x z).symm (sum.inl ix), simp only [add_move_left_inl], calc w + y ≤ move_left x ix + y : add_le_add_right hix ... ≤ move_left x ix + move_right y jy : add_le_add_left (oy.le_move_right jy) ... ≤ move_left x ix + z : add_le_add_left hjy }, { right, use (right_moves_add w y).symm (sum.inl jw), simp only [add_move_right_inl], calc move_right w jw + y ≤ x + y : add_le_add_right hjw ... ≤ x + move_left z iz : add_le_add_left hiz ... ≤ x + z : add_le_add_left (oz.move_left_le iz), }, { right, use (right_moves_add w y).symm (sum.inl jw), simp only [add_move_right_inl], calc move_right w jw + y ≤ x + y : add_le_add_right hjw ... ≤ x + move_right y jy : add_le_add_left (oy.le_move_right jy) ... ≤ x + z : add_le_add_left hjy, }, end theorem numeric_add : Π {x y : pgame} (ox : numeric x) (oy : numeric y), numeric (x + y) | ⟨xl, xr, xL, xR⟩ ⟨yl, yr, yL, yR⟩ ox oy := ⟨begin rintros (ix|iy) (jx|jy), { show xL ix + ⟨yl, yr, yL, yR⟩ < xR jx + ⟨yl, yr, yL, yR⟩, exact add_lt_add_right (ox.1 ix jx), }, { show xL ix + ⟨yl, yr, yL, yR⟩ < ⟨xl, xr, xL, xR⟩ + yR jy, apply add_lt_add (ox.move_left ix) ox oy (oy.move_right jy), apply ox.move_left_lt, apply oy.lt_move_right }, { -- show ⟨xl, xr, xL, xR⟩ + yL iy < xR jx + ⟨yl, yr, yL, yR⟩, -- fails? apply add_lt_add ox (ox.move_right jx) (oy.move_left iy) oy, apply ox.lt_move_right, apply oy.move_left_lt, }, { -- show ⟨xl, xr, xL, xR⟩ + yL iy < ⟨xl, xr, xL, xR⟩ + yR jy, -- fails? exact @add_lt_add_left ⟨xl, xr, xL, xR⟩ _ _ (oy.1 iy jy), } end, begin split, { rintros (ix|iy), { apply numeric_add (ox.move_left ix) oy, }, { apply numeric_add ox (oy.move_left iy), }, }, { rintros (jx|jy), { apply numeric_add (ox.move_right jx) oy, }, { apply numeric_add ox (oy.move_right jy), }, }, end⟩ using_well_founded { dec_tac := pgame_wf_tac } /-- Pre-games defined by natural numbers are numeric. -/ theorem numeric_nat : Π (n : ℕ), numeric n | 0 := numeric_zero | (n + 1) := numeric_add (numeric_nat n) numeric_one /-- The pre-game omega is numeric. -/ theorem numeric_omega : numeric omega := ⟨by rintros ⟨⟩ ⟨⟩, λ i, numeric_nat i.down, by rintros ⟨⟩⟩ /-- The pre-game `half` is numeric. -/ theorem numeric_half : numeric half := begin split, { rintros ⟨ ⟩ ⟨ ⟩, exact zero_lt_one }, split; rintro ⟨ ⟩, { exact numeric_zero }, { exact numeric_one } end theorem half_add_half_equiv_one : half + half ≈ 1 := begin split; rw le_def; split, { rintro (⟨⟨ ⟩⟩ | ⟨⟨ ⟩⟩), { right, use (sum.inr punit.star), calc ((half + half).move_left (sum.inl punit.star)).move_right (sum.inr punit.star) = (half.move_left punit.star + half).move_right (sum.inr punit.star) : by fsplit ... = (0 + half).move_right (sum.inr punit.star) : by fsplit ... ≈ 1 : zero_add_equiv 1 ... ≤ 1 : pgame.le_refl 1 }, { right, use (sum.inl punit.star), calc ((half + half).move_left (sum.inr punit.star)).move_right (sum.inl punit.star) = (half + half.move_left punit.star).move_right (sum.inl punit.star) : by fsplit ... = (half + 0).move_right (sum.inl punit.star) : by fsplit ... ≈ 1 : add_zero_equiv 1 ... ≤ 1 : pgame.le_refl 1 } }, { rintro ⟨ ⟩ }, { rintro ⟨ ⟩, left, use (sum.inl punit.star), calc 0 ≤ half : le_of_lt numeric_zero numeric_half zero_lt_half ... ≈ 0 + half : (zero_add_equiv half).symm ... = (half + half).move_left (sum.inl punit.star) : by fsplit }, { rintro (⟨⟨ ⟩⟩ | ⟨⟨ ⟩⟩); left, { exact ⟨sum.inr punit.star, le_of_le_of_equiv (pgame.le_refl _) (add_zero_equiv _).symm⟩ }, { exact ⟨sum.inl punit.star, le_of_le_of_equiv (pgame.le_refl _) (zero_add_equiv _).symm⟩ } } end end pgame /-- The equivalence on numeric pre-games. -/ def surreal.equiv (x y : {x // pgame.numeric x}) : Prop := x.1.equiv y.1 instance surreal.setoid : setoid {x // pgame.numeric x} := ⟨λ x y, x.1.equiv y.1, λ x, pgame.equiv_refl _, λ x y, pgame.equiv_symm, λ x y z, pgame.equiv_trans⟩ /-- The type of surreal numbers. These are the numeric pre-games quotiented by the equivalence relation `x ≈ y ↔ x ≤ y ∧ y ≤ x`. In the quotient, the order becomes a total order. -/ def surreal := quotient surreal.setoid namespace surreal open pgame /-- Construct a surreal number from a numeric pre-game. -/ def mk (x : pgame) (h : x.numeric) : surreal := quotient.mk ⟨x, h⟩ instance : has_zero surreal := { zero := ⟦⟨0, numeric_zero⟩⟧ } instance : has_one surreal := { one := ⟦⟨1, numeric_one⟩⟧ } instance : inhabited surreal := ⟨0⟩ /-- Lift an equivalence-respecting function on pre-games to surreals. -/ def lift {α} (f : ∀ x, numeric x → α) (H : ∀ {x y} (hx : numeric x) (hy : numeric y), x.equiv y → f x hx = f y hy) : surreal → α := quotient.lift (λ x : {x // numeric x}, f x.1 x.2) (λ x y, H x.2 y.2) /-- Lift a binary equivalence-respecting function on pre-games to surreals. -/ def lift₂ {α} (f : ∀ x y, numeric x → numeric y → α) (H : ∀ {x₁ y₁ x₂ y₂} (ox₁ : numeric x₁) (oy₁ : numeric y₁) (ox₂ : numeric x₂) (oy₂ : numeric y₂), x₁.equiv x₂ → y₁.equiv y₂ → f x₁ y₁ ox₁ oy₁ = f x₂ y₂ ox₂ oy₂) : surreal → surreal → α := lift (λ x ox, lift (λ y oy, f x y ox oy) (λ y₁ y₂ oy₁ oy₂ h, H _ _ _ _ (equiv_refl _) h)) (λ x₁ x₂ ox₁ ox₂ h, funext $ quotient.ind $ by exact λ ⟨y, oy⟩, H _ _ _ _ h (equiv_refl _)) /-- The relation `x ≤ y` on surreals. -/ def le : surreal → surreal → Prop := lift₂ (λ x y _ _, x ≤ y) (λ x₁ y₁ x₂ y₂ _ _ _ _ hx hy, propext (le_congr hx hy)) /-- The relation `x < y` on surreals. -/ def lt : surreal → surreal → Prop := lift₂ (λ x y _ _, x < y) (λ x₁ y₁ x₂ y₂ _ _ _ _ hx hy, propext (lt_congr hx hy)) theorem not_le : ∀ {x y : surreal}, ¬ le x y ↔ lt y x := by rintro ⟨⟨x, ox⟩⟩ ⟨⟨y, oy⟩⟩; exact not_le /-- Addition on surreals is inherited from pre-game addition: the sum of `x = {xL | xR}` and `y = {yL | yR}` is `{xL + y, x + yL | xR + y, x + yR}`. -/ def add : surreal → surreal → surreal := surreal.lift₂ (λ (x y : pgame) (ox) (oy), ⟦⟨x + y, numeric_add ox oy⟩⟧) (λ x₁ y₁ x₂ y₂ _ _ _ _ hx hy, quotient.sound (pgame.add_congr hx hy)) /-- Negation for surreal numbers is inherited from pre-game negation: the negation of `{L | R}` is `{-R | -L}`. -/ def neg : surreal → surreal := surreal.lift (λ x ox, ⟦⟨-x, pgame.numeric_neg ox⟩⟧) (λ _ _ _ _ a, quotient.sound (pgame.neg_congr a)) instance : has_le surreal := ⟨le⟩ instance : has_lt surreal := ⟨lt⟩ instance : has_add surreal := ⟨add⟩ instance : has_neg surreal := ⟨neg⟩ instance : ordered_add_comm_group surreal := { add := (+), add_assoc := by { rintros ⟨_⟩ ⟨_⟩ ⟨_⟩, exact quotient.sound add_assoc_equiv }, zero := 0, zero_add := by { rintros ⟨_⟩, exact quotient.sound (pgame.zero_add_equiv _) }, add_zero := by { rintros ⟨_⟩, exact quotient.sound (pgame.add_zero_equiv _) }, neg := has_neg.neg, add_left_neg := by { rintros ⟨_⟩, exact quotient.sound pgame.add_left_neg_equiv }, add_comm := by { rintros ⟨_⟩ ⟨_⟩, exact quotient.sound pgame.add_comm_equiv }, le := (≤), lt := (<), le_refl := by { rintros ⟨_⟩, refl }, le_trans := by { rintros ⟨_⟩ ⟨_⟩ ⟨_⟩, exact pgame.le_trans }, lt_iff_le_not_le := by { rintros ⟨_, ox⟩ ⟨_, oy⟩, exact pgame.lt_iff_le_not_le ox oy }, le_antisymm := by { rintros ⟨_⟩ ⟨_⟩ h₁ h₂, exact quotient.sound ⟨h₁, h₂⟩ }, add_le_add_left := by { rintros ⟨_⟩ ⟨_⟩ hx ⟨_⟩, exact pgame.add_le_add_left hx } } noncomputable instance : linear_ordered_add_comm_group surreal := { le_total := by rintro ⟨⟨x, ox⟩⟩ ⟨⟨y, oy⟩⟩; classical; exact or_iff_not_imp_left.2 (λ h, le_of_lt oy ox (pgame.not_le.1 h)), decidable_le := classical.dec_rel _, ..surreal.ordered_add_comm_group } -- We conclude with some ideas for further work on surreals; these would make fun projects. -- TODO define the inclusion of groups `surreal → game` -- TODO define the field structure on the surreals end surreal
bbcb4dc331cdea7e578aab42cad9166578acf984
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/data/real/ennreal.lean
d4a304c5d4c440e68f7c836c86f779ddcf90eba3
[ "Apache-2.0" ]
permissive
DanielFabian/mathlib
efc3a50b5dde303c59eeb6353ef4c35a345d7112
f520d07eba0c852e96fe26da71d85bf6d40fcc2a
refs/heads/master
1,668,739,922,971
1,595,201,756,000
1,595,201,756,000
279,469,476
0
0
null
1,594,696,604,000
1,594,696,604,000
null
UTF-8
Lean
false
false
46,614
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl, Yury Kudryashov Extended non-negative reals -/ import data.real.nnreal import data.set.intervals noncomputable theory open classical set open_locale classical big_operators variables {α : Type*} {β : Type*} /-- The extended nonnegative real numbers. This is usually denoted [0, ∞], and is relevant as the codomain of a measure. -/ @[derive canonically_ordered_comm_semiring, derive complete_linear_order, derive densely_ordered] def ennreal := with_top nnreal localized "notation `∞` := (⊤ : ennreal)" in ennreal namespace ennreal variables {a b c d : ennreal} {r p q : nnreal} instance : inhabited ennreal := ⟨0⟩ instance : has_coe nnreal ennreal := ⟨ option.some ⟩ instance : can_lift ennreal nnreal := { coe := coe, cond := λ r, r ≠ ∞, prf := λ x hx, ⟨option.get $ option.ne_none_iff_is_some.1 hx, option.some_get _⟩ } @[simp] lemma none_eq_top : (none : ennreal) = (⊤ : ennreal) := rfl @[simp] lemma some_eq_coe (a : nnreal) : (some a : ennreal) = (↑a : ennreal) := rfl /-- `to_nnreal x` returns `x` if it is real, otherwise 0. -/ protected def to_nnreal : ennreal → nnreal | (some r) := r | none := 0 /-- `to_real x` returns `x` if it is real, `0` otherwise. -/ protected def to_real (a : ennreal) : real := coe (a.to_nnreal) /-- `of_real x` returns `x` if it is nonnegative, `0` otherwise. -/ protected def of_real (r : real) : ennreal := coe (nnreal.of_real r) @[simp, norm_cast] lemma to_nnreal_coe : (r : ennreal).to_nnreal = r := rfl @[simp] lemma coe_to_nnreal : ∀{a:ennreal}, a ≠ ∞ → ↑(a.to_nnreal) = a | (some r) h := rfl | none h := (h rfl).elim @[simp] lemma of_real_to_real {a : ennreal} (h : a ≠ ∞) : ennreal.of_real (a.to_real) = a := by simp [ennreal.to_real, ennreal.of_real, h] @[simp] lemma to_real_of_real {r : real} (h : 0 ≤ r) : ennreal.to_real (ennreal.of_real r) = r := by simp [ennreal.to_real, ennreal.of_real, nnreal.coe_of_real _ h] lemma coe_to_nnreal_le_self : ∀{a:ennreal}, ↑(a.to_nnreal) ≤ a | (some r) := by rw [some_eq_coe, to_nnreal_coe]; exact le_refl _ | none := le_top lemma coe_nnreal_eq (r : nnreal) : (r : ennreal) = ennreal.of_real r := by { rw [ennreal.of_real, nnreal.of_real], cases r with r h, congr, dsimp, rw max_eq_left h } lemma of_real_eq_coe_nnreal {x : real} (h : 0 ≤ x) : ennreal.of_real x = @coe nnreal ennreal _ (⟨x, h⟩ : nnreal) := by { rw [coe_nnreal_eq], refl } @[simp, norm_cast] lemma coe_zero : ↑(0 : nnreal) = (0 : ennreal) := rfl @[simp, norm_cast] lemma coe_one : ↑(1 : nnreal) = (1 : ennreal) := rfl @[simp] lemma to_real_nonneg {a : ennreal} : 0 ≤ a.to_real := by simp [ennreal.to_real] @[simp] lemma top_to_nnreal : ∞.to_nnreal = 0 := rfl @[simp] lemma top_to_real : ∞.to_real = 0 := rfl @[simp] lemma one_to_real : (1 : ennreal).to_real = 1 := rfl @[simp] lemma one_to_nnreal : (1 : ennreal).to_nnreal = 1 := rfl @[simp] lemma coe_to_real (r : nnreal) : (r : ennreal).to_real = r := rfl @[simp] lemma zero_to_nnreal : (0 : ennreal).to_nnreal = 0 := rfl @[simp] lemma zero_to_real : (0 : ennreal).to_real = 0 := rfl @[simp] lemma of_real_zero : ennreal.of_real (0 : ℝ) = 0 := by simp [ennreal.of_real]; refl @[simp] lemma of_real_one : ennreal.of_real (1 : ℝ) = (1 : ennreal) := by simp [ennreal.of_real] lemma of_real_to_real_le {a : ennreal} : ennreal.of_real (a.to_real) ≤ a := if ha : a = ∞ then ha.symm ▸ le_top else le_of_eq (of_real_to_real ha) lemma forall_ennreal {p : ennreal → Prop} : (∀a, p a) ↔ (∀r:nnreal, p r) ∧ p ∞ := ⟨assume h, ⟨assume r, h _, h _⟩, assume ⟨h₁, h₂⟩ a, match a with some r := h₁ _ | none := h₂ end⟩ lemma to_nnreal_eq_zero_iff (x : ennreal) : x.to_nnreal = 0 ↔ x = 0 ∨ x = ⊤ := ⟨begin cases x, { simp [none_eq_top] }, { have A : some (0:nnreal) = (0:ennreal) := rfl, simp [ennreal.to_nnreal, A] {contextual := tt} } end, by intro h; cases h; simp [h]⟩ lemma to_real_eq_zero_iff (x : ennreal) : x.to_real = 0 ↔ x = 0 ∨ x = ⊤ := by simp [ennreal.to_real, to_nnreal_eq_zero_iff] @[simp] lemma coe_ne_top : (r : ennreal) ≠ ∞ := with_top.coe_ne_top @[simp] lemma top_ne_coe : ∞ ≠ (r : ennreal) := with_top.top_ne_coe @[simp] lemma of_real_ne_top {r : ℝ} : ennreal.of_real r ≠ ∞ := by simp [ennreal.of_real] @[simp] lemma top_ne_of_real {r : ℝ} : ∞ ≠ ennreal.of_real r := by simp [ennreal.of_real] @[simp] lemma zero_ne_top : 0 ≠ ∞ := coe_ne_top @[simp] lemma top_ne_zero : ∞ ≠ 0 := top_ne_coe @[simp] lemma one_ne_top : 1 ≠ ∞ := coe_ne_top @[simp] lemma top_ne_one : ∞ ≠ 1 := top_ne_coe @[simp, norm_cast] lemma coe_eq_coe : (↑r : ennreal) = ↑q ↔ r = q := with_top.coe_eq_coe @[simp, norm_cast] lemma coe_le_coe : (↑r : ennreal) ≤ ↑q ↔ r ≤ q := with_top.coe_le_coe @[simp, norm_cast] lemma coe_lt_coe : (↑r : ennreal) < ↑q ↔ r < q := with_top.coe_lt_coe lemma coe_mono : monotone (coe : nnreal → ennreal) := λ _ _, coe_le_coe.2 @[simp, norm_cast] lemma coe_eq_zero : (↑r : ennreal) = 0 ↔ r = 0 := coe_eq_coe @[simp, norm_cast] lemma zero_eq_coe : 0 = (↑r : ennreal) ↔ 0 = r := coe_eq_coe @[simp, norm_cast] lemma coe_eq_one : (↑r : ennreal) = 1 ↔ r = 1 := coe_eq_coe @[simp, norm_cast] lemma one_eq_coe : 1 = (↑r : ennreal) ↔ 1 = r := coe_eq_coe @[simp, norm_cast] lemma coe_nonneg : 0 ≤ (↑r : ennreal) ↔ 0 ≤ r := coe_le_coe @[simp, norm_cast] lemma coe_pos : 0 < (↑r : ennreal) ↔ 0 < r := coe_lt_coe @[simp, norm_cast] lemma coe_add : ↑(r + p) = (r + p : ennreal) := with_top.coe_add @[simp, norm_cast] lemma coe_mul : ↑(r * p) = (r * p : ennreal) := with_top.coe_mul @[simp, norm_cast] lemma coe_bit0 : (↑(bit0 r) : ennreal) = bit0 r := coe_add @[simp, norm_cast] lemma coe_bit1 : (↑(bit1 r) : ennreal) = bit1 r := by simp [bit1] lemma coe_two : ((2:nnreal) : ennreal) = 2 := by norm_cast protected lemma zero_lt_one : 0 < (1 : ennreal) := canonically_ordered_semiring.zero_lt_one @[simp] lemma one_lt_two : (1:ennreal) < 2 := coe_one ▸ coe_two ▸ by exact_mod_cast one_lt_two @[simp] lemma two_pos : (0:ennreal) < 2 := lt_trans ennreal.zero_lt_one one_lt_two lemma two_ne_zero : (2:ennreal) ≠ 0 := ne_of_gt two_pos lemma two_ne_top : (2:ennreal) ≠ ∞ := coe_two ▸ coe_ne_top @[simp] lemma add_top : a + ∞ = ∞ := with_top.add_top @[simp] lemma top_add : ∞ + a = ∞ := with_top.top_add /-- Coercion `ℝ≥0 → ennreal` as a `ring_hom`. -/ def of_nnreal_hom : nnreal →+* ennreal := ⟨coe, coe_one, λ _ _, coe_mul, coe_zero, λ _ _, coe_add⟩ @[simp] lemma coe_of_nnreal_hom : ⇑of_nnreal_hom = coe := rfl @[simp, norm_cast] lemma coe_pow (n : ℕ) : (↑(r^n) : ennreal) = r^n := of_nnreal_hom.map_pow r n lemma add_eq_top : a + b = ∞ ↔ a = ∞ ∨ b = ∞ := with_top.add_eq_top _ _ lemma add_lt_top : a + b < ∞ ↔ a < ∞ ∧ b < ∞ := with_top.add_lt_top _ _ lemma to_nnreal_add {r₁ r₂ : ennreal} (h₁ : r₁ < ⊤) (h₂ : r₂ < ⊤) : (r₁ + r₂).to_nnreal = r₁.to_nnreal + r₂.to_nnreal := begin rw [← coe_eq_coe, coe_add, coe_to_nnreal, coe_to_nnreal, coe_to_nnreal]; apply @ne_top_of_lt ennreal _ _ ⊤, exact h₂, exact h₁, exact add_lt_top.2 ⟨h₁, h₂⟩ end /- rw has trouble with the generic lt_top_iff_ne_top and bot_lt_iff_ne_bot (contrary to erw). This is solved with the next lemmas -/ protected lemma lt_top_iff_ne_top : a < ∞ ↔ a ≠ ∞ := lt_top_iff_ne_top protected lemma bot_lt_iff_ne_bot : 0 < a ↔ a ≠ 0 := bot_lt_iff_ne_bot lemma add_ne_top : a + b ≠ ∞ ↔ a ≠ ∞ ∧ b ≠ ∞ := by simpa only [lt_top_iff_ne_top] using add_lt_top lemma mul_top : a * ∞ = (if a = 0 then 0 else ∞) := begin split_ifs, { simp [h] }, { exact with_top.mul_top h } end lemma top_mul : ∞ * a = (if a = 0 then 0 else ∞) := begin split_ifs, { simp [h] }, { exact with_top.top_mul h } end @[simp] lemma top_mul_top : ∞ * ∞ = ∞ := with_top.top_mul_top lemma top_pow {n:ℕ} (h : 0 < n) : ∞^n = ∞ := nat.le_induction (pow_one _) (λ m hm hm', by rw [pow_succ, hm', top_mul_top]) _ (nat.succ_le_of_lt h) lemma mul_eq_top {a b : ennreal} : a * b = ⊤ ↔ (a ≠ 0 ∧ b = ⊤) ∨ (a = ⊤ ∧ b ≠ 0) := with_top.mul_eq_top_iff lemma mul_ne_top {a b : ennreal} : a ≠ ∞ → b ≠ ∞ → a * b ≠ ∞ := by simp [(≠), mul_eq_top] {contextual := tt} lemma mul_lt_top {a b : ennreal} : a < ⊤ → b < ⊤ → a * b < ⊤ := by simpa only [ennreal.lt_top_iff_ne_top] using mul_ne_top @[simp] lemma mul_pos {a b : ennreal} : 0 < a * b ↔ 0 < a ∧ 0 < b := by simp only [zero_lt_iff_ne_zero, ne.def, mul_eq_zero, not_or_distrib] lemma pow_eq_top : ∀ n:ℕ, a^n=∞ → a=∞ | 0 := by simp | (n+1) := λ o, (mul_eq_top.1 o).elim (λ h, pow_eq_top n h.2) and.left lemma pow_ne_top (h : a ≠ ∞) {n:ℕ} : a^n ≠ ∞ := mt (pow_eq_top n) h lemma pow_lt_top : a < ∞ → ∀ n:ℕ, a^n < ∞ := by simpa only [lt_top_iff_ne_top] using pow_ne_top @[simp, norm_cast] lemma coe_finset_sum {s : finset α} {f : α → nnreal} : ↑(∑ a in s, f a) = (∑ a in s, f a : ennreal) := of_nnreal_hom.map_sum f s @[simp, norm_cast] lemma coe_finset_prod {s : finset α} {f : α → nnreal} : ↑(∏ a in s, f a) = ((∏ a in s, f a) : ennreal) := of_nnreal_hom.map_prod f s section order @[simp] lemma bot_eq_zero : (⊥ : ennreal) = 0 := rfl @[simp] lemma coe_lt_top : coe r < ∞ := with_top.coe_lt_top r @[simp] lemma not_top_le_coe : ¬ (⊤:ennreal) ≤ ↑r := with_top.not_top_le_coe r lemma zero_lt_coe_iff : 0 < (↑p : ennreal) ↔ 0 < p := coe_lt_coe @[simp, norm_cast] lemma one_le_coe_iff : (1:ennreal) ≤ ↑r ↔ 1 ≤ r := coe_le_coe @[simp, norm_cast] lemma coe_le_one_iff : ↑r ≤ (1:ennreal) ↔ r ≤ 1 := coe_le_coe @[simp, norm_cast] lemma coe_lt_one_iff : (↑p : ennreal) < 1 ↔ p < 1 := coe_lt_coe @[simp, norm_cast] lemma one_lt_coe_iff : 1 < (↑p : ennreal) ↔ 1 < p := coe_lt_coe @[simp, norm_cast] lemma coe_nat (n : nat) : ((n : nnreal) : ennreal) = n := with_top.coe_nat n @[simp] lemma nat_ne_top (n : nat) : (n : ennreal) ≠ ⊤ := with_top.nat_ne_top n @[simp] lemma top_ne_nat (n : nat) : (⊤ : ennreal) ≠ n := with_top.top_ne_nat n lemma le_coe_iff : a ≤ ↑r ↔ (∃p:nnreal, a = p ∧ p ≤ r) := with_top.le_coe_iff r a lemma coe_le_iff : ↑r ≤ a ↔ (∀p:nnreal, a = p → r ≤ p) := with_top.coe_le_iff r a lemma lt_iff_exists_coe : a < b ↔ (∃p:nnreal, a = p ∧ ↑p < b) := with_top.lt_iff_exists_coe a b lemma pow_le_pow {n m : ℕ} (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m := begin cases a, { cases m, { rw eq_bot_iff.mpr h, exact le_refl _ }, { rw [none_eq_top, top_pow (nat.succ_pos m)], exact le_top } }, { rw [some_eq_coe, ← coe_pow, ← coe_pow, coe_le_coe], exact pow_le_pow (by simpa using ha) h } end @[simp] lemma max_eq_zero_iff : max a b = 0 ↔ a = 0 ∧ b = 0 := by simp only [le_zero_iff_eq.symm, max_le_iff] @[simp] lemma max_zero_left : max 0 a = a := max_eq_right (zero_le a) @[simp] lemma max_zero_right : max a 0 = a := max_eq_left (zero_le a) -- TODO: why this is not a `rfl`? There is some hidden diamond here. @[simp] lemma sup_eq_max : a ⊔ b = max a b := eq_of_forall_ge_iff $ λ c, sup_le_iff.trans max_le_iff.symm protected lemma pow_pos : 0 < a → ∀ n : ℕ, 0 < a^n := canonically_ordered_semiring.pow_pos protected lemma pow_ne_zero : a ≠ 0 → ∀ n : ℕ, a^n ≠ 0 := by simpa only [zero_lt_iff_ne_zero] using ennreal.pow_pos @[simp] lemma not_lt_zero : ¬ a < 0 := by simp lemma add_lt_add_iff_left : a < ⊤ → (a + c < a + b ↔ c < b) := with_top.add_lt_add_iff_left lemma add_lt_add_iff_right : a < ⊤ → (c + a < b + a ↔ c < b) := with_top.add_lt_add_iff_right lemma lt_add_right (ha : a < ⊤) (hb : 0 < b) : a < a + b := by rwa [← add_lt_add_iff_left ha, add_zero] at hb lemma le_of_forall_epsilon_le : ∀{a b : ennreal}, (∀ε:nnreal, 0 < ε → b < ∞ → a ≤ b + ε) → a ≤ b | a none h := le_top | none (some a) h := have (⊤:ennreal) ≤ ↑a + ↑(1:nnreal), from h 1 zero_lt_one coe_lt_top, by rw [← coe_add] at this; exact (not_top_le_coe this).elim | (some a) (some b) h := by simp only [none_eq_top, some_eq_coe, coe_add.symm, coe_le_coe, coe_lt_top, true_implies_iff] at *; exact nnreal.le_of_forall_epsilon_le h lemma lt_iff_exists_rat_btwn : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < nnreal.of_real q ∧ (nnreal.of_real q:ennreal) < b) := ⟨λ h, begin rcases lt_iff_exists_coe.1 h with ⟨p, rfl, _⟩, rcases dense h with ⟨c, pc, cb⟩, rcases lt_iff_exists_coe.1 cb with ⟨r, rfl, _⟩, rcases (nnreal.lt_iff_exists_rat_btwn _ _).1 (coe_lt_coe.1 pc) with ⟨q, hq0, pq, qr⟩, exact ⟨q, hq0, coe_lt_coe.2 pq, lt_trans (coe_lt_coe.2 qr) cb⟩ end, λ ⟨q, q0, qa, qb⟩, lt_trans qa qb⟩ lemma lt_iff_exists_real_btwn : a < b ↔ (∃r:ℝ, 0 ≤ r ∧ a < ennreal.of_real r ∧ (ennreal.of_real r:ennreal) < b) := ⟨λ h, let ⟨q, q0, aq, qb⟩ := ennreal.lt_iff_exists_rat_btwn.1 h in ⟨q, rat.cast_nonneg.2 q0, aq, qb⟩, λ ⟨q, q0, qa, qb⟩, lt_trans qa qb⟩ lemma lt_iff_exists_nnreal_btwn : a < b ↔ (∃r:nnreal, a < r ∧ (r : ennreal) < b) := with_top.lt_iff_exists_coe_btwn lemma lt_iff_exists_add_pos_lt : a < b ↔ (∃ r : nnreal, 0 < r ∧ a + r < b) := begin refine ⟨λ hab, _, λ ⟨r, rpos, hr⟩, lt_of_le_of_lt (le_add_right (le_refl _)) hr⟩, cases a, { simpa using hab }, rcases lt_iff_exists_real_btwn.1 hab with ⟨c, c_nonneg, ac, cb⟩, let d : nnreal := ⟨c, c_nonneg⟩, have ad : a < d, { rw of_real_eq_coe_nnreal c_nonneg at ac, exact coe_lt_coe.1 ac }, refine ⟨d-a, nnreal.sub_pos.2 ad, _⟩, rw [some_eq_coe, ← coe_add], convert cb, have : nnreal.of_real c = d, by { rw [← nnreal.coe_eq, nnreal.coe_of_real _ c_nonneg], refl }, rw [add_comm, this], exact nnreal.sub_add_cancel_of_le (le_of_lt ad) end lemma coe_nat_lt_coe {n : ℕ} : (n : ennreal) < r ↔ ↑n < r := ennreal.coe_nat n ▸ coe_lt_coe lemma coe_lt_coe_nat {n : ℕ} : (r : ennreal) < n ↔ r < n := ennreal.coe_nat n ▸ coe_lt_coe @[norm_cast] lemma coe_nat_lt_coe_nat {m n : ℕ} : (m : ennreal) < n ↔ m < n := ennreal.coe_nat n ▸ coe_nat_lt_coe.trans nat.cast_lt lemma coe_nat_ne_top {n : ℕ} : (n : ennreal) ≠ ∞ := ennreal.coe_nat n ▸ coe_ne_top lemma coe_nat_mono : strict_mono (coe : ℕ → ennreal) := λ _ _, coe_nat_lt_coe_nat.2 @[norm_cast] lemma coe_nat_le_coe_nat {m n : ℕ} : (m : ennreal) ≤ n ↔ m ≤ n := coe_nat_mono.le_iff_le instance : char_zero ennreal := ⟨coe_nat_mono.injective⟩ protected lemma exists_nat_gt {r : ennreal} (h : r ≠ ⊤) : ∃n:ℕ, r < n := begin rcases lt_iff_exists_coe.1 (lt_top_iff_ne_top.2 h) with ⟨r, rfl, hb⟩, rcases exists_nat_gt r with ⟨n, hn⟩, exact ⟨n, coe_lt_coe_nat.2 hn⟩, end lemma add_lt_add (ac : a < c) (bd : b < d) : a + b < c + d := begin rcases dense ac with ⟨a', aa', a'c⟩, rcases lt_iff_exists_coe.1 aa' with ⟨aR, rfl, _⟩, rcases lt_iff_exists_coe.1 a'c with ⟨a'R, rfl, _⟩, rcases dense bd with ⟨b', bb', b'd⟩, rcases lt_iff_exists_coe.1 bb' with ⟨bR, rfl, _⟩, rcases lt_iff_exists_coe.1 b'd with ⟨b'R, rfl, _⟩, have I : ↑aR + ↑bR < ↑a'R + ↑b'R := begin rw [← coe_add, ← coe_add, coe_lt_coe], apply add_lt_add (coe_lt_coe.1 aa') (coe_lt_coe.1 bb') end, have J : ↑a'R + ↑b'R ≤ c + d := add_le_add (le_of_lt a'c) (le_of_lt b'd), apply lt_of_lt_of_le I J end @[norm_cast] lemma coe_min : ((min r p:nnreal):ennreal) = min r p := coe_mono.map_min @[norm_cast] lemma coe_max : ((max r p:nnreal):ennreal) = max r p := coe_mono.map_max end order section complete_lattice lemma coe_Sup {s : set nnreal} : bdd_above s → (↑(Sup s) : ennreal) = (⨆a∈s, ↑a) := with_top.coe_Sup lemma coe_Inf {s : set nnreal} : s.nonempty → (↑(Inf s) : ennreal) = (⨅a∈s, ↑a) := with_top.coe_Inf @[simp] lemma top_mem_upper_bounds {s : set ennreal} : ∞ ∈ upper_bounds s := assume x hx, le_top lemma coe_mem_upper_bounds {s : set nnreal} : ↑r ∈ upper_bounds ((coe : nnreal → ennreal) '' s) ↔ r ∈ upper_bounds s := by simp [upper_bounds, ball_image_iff, -mem_image, *] {contextual := tt} lemma infi_ennreal {α : Type*} [complete_lattice α] {f : ennreal → α} : (⨅n, f n) = (⨅n:nnreal, f n) ⊓ f ⊤ := le_antisymm (le_inf (le_infi $ assume i, infi_le _ _) (infi_le _ _)) (le_infi $ forall_ennreal.2 ⟨assume r, inf_le_left_of_le $ infi_le _ _, inf_le_right⟩) end complete_lattice section mul lemma mul_le_mul : a ≤ b → c ≤ d → a * c ≤ b * d := canonically_ordered_semiring.mul_le_mul lemma mul_left_mono : monotone ((*) a) := λ b c, mul_le_mul (le_refl a) lemma mul_right_mono : monotone (λ x, x * a) := λ b c h, mul_le_mul h (le_refl a) lemma max_mul : max a b * c = max (a * c) (b * c) := mul_right_mono.map_max lemma mul_max : a * max b c = max (a * b) (a * c) := mul_left_mono.map_max lemma mul_eq_mul_left : a ≠ 0 → a ≠ ⊤ → (a * b = a * c ↔ b = c) := begin cases a; cases b; cases c; simp [none_eq_top, some_eq_coe, mul_top, top_mul, -coe_mul, coe_mul.symm, nnreal.mul_eq_mul_left] {contextual := tt}, end lemma mul_eq_mul_right : c ≠ 0 → c ≠ ∞ → (a * c = b * c ↔ a = b) := mul_comm c a ▸ mul_comm c b ▸ mul_eq_mul_left lemma mul_le_mul_left : a ≠ 0 → a ≠ ⊤ → (a * b ≤ a * c ↔ b ≤ c) := begin cases a; cases b; cases c; simp [none_eq_top, some_eq_coe, mul_top, top_mul, -coe_mul, coe_mul.symm] {contextual := tt}, assume h, exact mul_le_mul_left (zero_lt_iff_ne_zero.2 h) end lemma mul_le_mul_right : c ≠ 0 → c ≠ ∞ → (a * c ≤ b * c ↔ a ≤ b) := mul_comm c a ▸ mul_comm c b ▸ mul_le_mul_left lemma mul_lt_mul_left : a ≠ 0 → a ≠ ⊤ → (a * b < a * c ↔ b < c) := λ h0 ht, by simp only [mul_le_mul_left h0 ht, lt_iff_le_not_le] lemma mul_lt_mul_right : c ≠ 0 → c ≠ ∞ → (a * c < b * c ↔ a < b) := mul_comm c a ▸ mul_comm c b ▸ mul_lt_mul_left end mul section sub instance : has_sub ennreal := ⟨λa b, Inf {d | a ≤ d + b}⟩ @[norm_cast] lemma coe_sub : ↑(p - r) = (↑p:ennreal) - r := le_antisymm (le_Inf $ assume b (hb : ↑p ≤ b + r), coe_le_iff.2 $ by rintros d rfl; rwa [← coe_add, coe_le_coe, ← nnreal.sub_le_iff_le_add] at hb) (Inf_le $ show (↑p : ennreal) ≤ ↑(p - r) + ↑r, by rw [← coe_add, coe_le_coe, ← nnreal.sub_le_iff_le_add]) @[simp] lemma top_sub_coe : ∞ - ↑r = ∞ := top_unique $ le_Inf $ by simp [add_eq_top] @[simp] lemma sub_eq_zero_of_le (h : a ≤ b) : a - b = 0 := le_antisymm (Inf_le $ le_add_left h) (zero_le _) @[simp] lemma sub_self : a - a = 0 := sub_eq_zero_of_le $ le_refl _ @[simp] lemma zero_sub : 0 - a = 0 := le_antisymm (Inf_le $ zero_le _) (zero_le _) @[simp] lemma sub_infty : a - ∞ = 0 := le_antisymm (Inf_le $ by simp) (zero_le _) lemma sub_le_sub (h₁ : a ≤ b) (h₂ : d ≤ c) : a - c ≤ b - d := Inf_le_Inf $ assume e (h : b ≤ e + d), calc a ≤ b : h₁ ... ≤ e + d : h ... ≤ e + c : add_le_add (le_refl _) h₂ @[simp] lemma add_sub_self : ∀{a b : ennreal}, b < ∞ → (a + b) - b = a | a none := by simp [none_eq_top] | none (some b) := by simp [none_eq_top, some_eq_coe] | (some a) (some b) := by simp [some_eq_coe]; rw [← coe_add, ← coe_sub, coe_eq_coe, nnreal.add_sub_cancel] @[simp] lemma add_sub_self' (h : a < ∞) : (a + b) - a = b := by rw [add_comm, add_sub_self h] lemma add_right_inj (h : a < ∞) : a + b = a + c ↔ b = c := ⟨λ e, by simpa [h] using congr_arg (λ x, x - a) e, congr_arg _⟩ lemma add_left_inj (h : a < ∞) : b + a = c + a ↔ b = c := by rw [add_comm, add_comm c, add_right_inj h] @[simp] lemma sub_add_cancel_of_le : ∀{a b : ennreal}, b ≤ a → (a - b) + b = a := begin simp [forall_ennreal, le_coe_iff, -add_comm] {contextual := tt}, rintros r p x rfl h, rw [← coe_sub, ← coe_add, nnreal.sub_add_cancel_of_le h] end @[simp] lemma add_sub_cancel_of_le (h : b ≤ a) : b + (a - b) = a := by rwa [add_comm, sub_add_cancel_of_le] lemma sub_add_self_eq_max : (a - b) + b = max a b := match le_total a b with | or.inl h := by simp [h, max_eq_right] | or.inr h := by simp [h, max_eq_left] end lemma le_sub_add_self : a ≤ (a - b) + b := by { rw sub_add_self_eq_max, exact le_max_left a b } @[simp] protected lemma sub_le_iff_le_add : a - b ≤ c ↔ a ≤ c + b := iff.intro (assume h : a - b ≤ c, calc a ≤ (a - b) + b : le_sub_add_self ... ≤ c + b : add_le_add_right h _) (assume h : a ≤ c + b, calc a - b ≤ (c + b) - b : sub_le_sub h (le_refl _) ... ≤ c : Inf_le (le_refl (c + b))) protected lemma sub_le_iff_le_add' : a - b ≤ c ↔ a ≤ b + c := add_comm c b ▸ ennreal.sub_le_iff_le_add lemma sub_eq_of_add_eq : b ≠ ∞ → a + b = c → c - b = a := λ hb hc, hc ▸ add_sub_self (lt_top_iff_ne_top.2 hb) protected lemma sub_le_of_sub_le (h : a - b ≤ c) : a - c ≤ b := ennreal.sub_le_iff_le_add.2 $ by { rw add_comm, exact ennreal.sub_le_iff_le_add.1 h } protected lemma sub_lt_sub_self : a ≠ ⊤ → a ≠ 0 → 0 < b → a - b < a := match a, b with | none, _ := by { have := none_eq_top, assume h, contradiction } | (some a), none := by {intros, simp only [none_eq_top, sub_infty, zero_lt_iff_ne_zero], assumption} | (some a), (some b) := begin simp only [some_eq_coe, coe_sub.symm, coe_pos, coe_eq_zero, coe_lt_coe, ne.def], assume h₁ h₂, apply nnreal.sub_lt_self, exact zero_lt_iff_ne_zero.2 h₂ end end @[simp] lemma sub_eq_zero_iff_le : a - b = 0 ↔ a ≤ b := by simpa [-ennreal.sub_le_iff_le_add] using @ennreal.sub_le_iff_le_add a b 0 @[simp] lemma zero_lt_sub_iff_lt : 0 < a - b ↔ b < a := by simpa [ennreal.bot_lt_iff_ne_bot, -sub_eq_zero_iff_le] using not_iff_not.2 (@sub_eq_zero_iff_le a b) lemma lt_sub_iff_add_lt : a < b - c ↔ a + c < b := begin cases a, { simp }, cases c, { simp }, cases b, { simp only [true_iff, coe_lt_top, some_eq_coe, top_sub_coe, none_eq_top, ← coe_add] }, simp only [some_eq_coe], rw [← coe_add, ← coe_sub, coe_lt_coe, coe_lt_coe, nnreal.lt_sub_iff_add_lt], end lemma sub_le_self (a b : ennreal) : a - b ≤ a := ennreal.sub_le_iff_le_add.2 $ le_add_right (le_refl a) @[simp] lemma sub_zero : a - 0 = a := eq.trans (add_zero (a - 0)).symm $ by simp /-- A version of triangle inequality for difference as a "distance". -/ lemma sub_le_sub_add_sub : a - c ≤ a - b + (b - c) := ennreal.sub_le_iff_le_add.2 $ calc a ≤ a - b + b : le_sub_add_self ... ≤ a - b + ((b - c) + c) : add_le_add_left le_sub_add_self _ ... = a - b + (b - c) + c : (add_assoc _ _ _).symm lemma sub_sub_cancel (h : a < ∞) (h2 : b ≤ a) : a - (a - b) = b := by rw [← add_left_inj (lt_of_le_of_lt (sub_le_self _ _) h), sub_add_cancel_of_le (sub_le_self _ _), add_sub_cancel_of_le h2] lemma sub_right_inj {a b c : ennreal} (ha : a < ⊤) (hb : b ≤ a) (hc : c ≤ a) : a - b = a - c ↔ b = c := iff.intro begin assume h, have : a - (a - b) = a - (a - c), rw h, rw [sub_sub_cancel ha hb, sub_sub_cancel ha hc] at this, exact this end (λ h, by rw h) lemma sub_mul (h : 0 < b → b < a → c ≠ ∞) : (a - b) * c = a * c - b * c := begin cases le_or_lt a b with hab hab, { simp [hab, mul_right_mono hab] }, symmetry, cases eq_or_lt_of_le (zero_le b) with hb hb, { subst b, simp }, apply sub_eq_of_add_eq, { exact mul_ne_top (ne_top_of_lt hab) (h hb hab) }, rw [← add_mul, sub_add_cancel_of_le (le_of_lt hab)] end lemma mul_sub (h : 0 < c → c < b → a ≠ ∞) : a * (b - c) = a * b - a * c := by { simp only [mul_comm a], exact sub_mul h } lemma sub_mul_ge : a * c - b * c ≤ (a - b) * c := begin -- with `0 < b → b < a → c ≠ ∞` Lean names the first variable `a` by_cases h : ∀ (hb : 0 < b), b < a → c ≠ ∞, { rw [sub_mul h], exact le_refl _ }, { push_neg at h, rcases h with ⟨hb, hba, hc⟩, subst c, simp only [mul_top, if_neg (ne_of_gt hb), if_neg (ne_of_gt $ lt_trans hb hba), sub_self, zero_le] } end end sub section sum open finset /-- A sum of finite numbers is still finite -/ lemma sum_lt_top {s : finset α} {f : α → ennreal} : (∀a∈s, f a < ⊤) → ∑ a in s, f a < ⊤ := with_top.sum_lt_top /-- A sum of finite numbers is still finite -/ lemma sum_lt_top_iff {s : finset α} {f : α → ennreal} : ∑ a in s, f a < ⊤ ↔ (∀a∈s, f a < ⊤) := with_top.sum_lt_top_iff /-- A sum of numbers is infinite iff one of them is infinite -/ lemma sum_eq_top_iff {s : finset α} {f : α → ennreal} : (∑ x in s, f x) = ⊤ ↔ (∃a∈s, f a = ⊤) := with_top.sum_eq_top_iff /-- seeing `ennreal` as `nnreal` does not change their sum, unless one of the `ennreal` is infinity -/ lemma to_nnreal_sum {s : finset α} {f : α → ennreal} (hf : ∀a∈s, f a < ⊤) : ennreal.to_nnreal (∑ a in s, f a) = ∑ a in s, ennreal.to_nnreal (f a) := begin rw [← coe_eq_coe, coe_to_nnreal, coe_finset_sum, sum_congr], { refl }, { intros x hx, rw coe_to_nnreal, rw ← ennreal.lt_top_iff_ne_top, exact hf x hx }, { rw ← ennreal.lt_top_iff_ne_top, exact sum_lt_top hf } end /-- seeing `ennreal` as `real` does not change their sum, unless one of the `ennreal` is infinity -/ lemma to_real_sum {s : finset α} {f : α → ennreal} (hf : ∀a∈s, f a < ⊤) : ennreal.to_real (∑ a in s, f a) = ∑ a in s, ennreal.to_real (f a) := by { rw [ennreal.to_real, to_nnreal_sum hf, nnreal.coe_sum], refl } end sum section interval variables {x y z : ennreal} {ε ε₁ ε₂ : ennreal} {s : set ennreal} protected lemma Ico_eq_Iio : (Ico 0 y) = (Iio y) := ext $ assume a, iff.intro (assume ⟨_, hx⟩, hx) (assume hx, ⟨zero_le _, hx⟩) lemma mem_Iio_self_add : x ≠ ⊤ → 0 < ε → x ∈ Iio (x + ε) := assume xt ε0, lt_add_right (by rwa lt_top_iff_ne_top) ε0 lemma not_mem_Ioo_self_sub : x = 0 → x ∉ Ioo (x - ε) y := assume x0, by simp [x0] lemma mem_Ioo_self_sub_add : x ≠ ⊤ → x ≠ 0 → 0 < ε₁ → 0 < ε₂ → x ∈ Ioo (x - ε₁) (x + ε₂) := assume xt x0 ε0 ε0', ⟨ennreal.sub_lt_sub_self xt x0 ε0, lt_add_right (by rwa [lt_top_iff_ne_top]) ε0'⟩ end interval section bit @[simp] lemma bit0_inj : bit0 a = bit0 b ↔ a = b := ⟨λh, begin rcases (lt_trichotomy a b) with h₁| h₂| h₃, { exact (absurd h (ne_of_lt (add_lt_add h₁ h₁))) }, { exact h₂ }, { exact (absurd h.symm (ne_of_lt (add_lt_add h₃ h₃))) } end, λh, congr_arg _ h⟩ @[simp] lemma bit0_eq_zero_iff : bit0 a = 0 ↔ a = 0 := by simpa only [bit0_zero] using @bit0_inj a 0 @[simp] lemma bit0_eq_top_iff : bit0 a = ∞ ↔ a = ∞ := by rw [bit0, add_eq_top, or_self] @[simp] lemma bit1_inj : bit1 a = bit1 b ↔ a = b := ⟨λh, begin unfold bit1 at h, rwa [add_left_inj, bit0_inj] at h, simp [lt_top_iff_ne_top] end, λh, congr_arg _ h⟩ @[simp] lemma bit1_ne_zero : bit1 a ≠ 0 := by unfold bit1; simp @[simp] lemma bit1_eq_one_iff : bit1 a = 1 ↔ a = 0 := by simpa only [bit1_zero] using @bit1_inj a 0 @[simp] lemma bit1_eq_top_iff : bit1 a = ∞ ↔ a = ∞ := by unfold bit1; rw add_eq_top; simp end bit section inv instance : has_inv ennreal := ⟨λa, Inf {b | 1 ≤ a * b}⟩ instance : has_div ennreal := ⟨λa b, a * b⁻¹⟩ lemma div_def : a / b = a * b⁻¹ := rfl lemma mul_div_assoc : (a * b) / c = a * (b / c) := mul_assoc _ _ _ @[simp] lemma inv_zero : (0 : ennreal)⁻¹ = ∞ := show Inf {b : ennreal | 1 ≤ 0 * b} = ∞, by simp; refl @[simp] lemma inv_top : (∞ : ennreal)⁻¹ = 0 := bot_unique $ le_of_forall_le_of_dense $ λ a (h : a > 0), Inf_le $ by simp [*, ne_of_gt h, top_mul] @[simp, norm_cast] lemma coe_inv (hr : r ≠ 0) : (↑r⁻¹ : ennreal) = (↑r)⁻¹ := le_antisymm (le_Inf $ assume b (hb : 1 ≤ ↑r * b), coe_le_iff.2 $ by rintros b rfl; rwa [← coe_mul, ← coe_one, coe_le_coe, ← nnreal.inv_le hr] at hb) (Inf_le $ by simp; rw [← coe_mul, nnreal.mul_inv_cancel hr]; exact le_refl 1) lemma coe_inv_le : (↑r⁻¹ : ennreal) ≤ (↑r)⁻¹ := if hr : r = 0 then by simp only [hr, nnreal.inv_zero, inv_zero, coe_zero, zero_le] else by simp only [coe_inv hr, le_refl] @[norm_cast] lemma coe_inv_two : ((2⁻¹:nnreal):ennreal) = 2⁻¹ := by rw [coe_inv (ne_of_gt zero_lt_two), coe_two] @[simp, norm_cast] lemma coe_div (hr : r ≠ 0) : (↑(p / r) : ennreal) = p / r := show ↑(p * r⁻¹) = ↑p * (↑r)⁻¹, by rw [coe_mul, coe_inv hr] @[simp] lemma inv_one : (1:ennreal)⁻¹ = 1 := by simpa only [coe_inv one_ne_zero, coe_one] using coe_eq_coe.2 nnreal.inv_one @[simp] lemma div_one {a : ennreal} : a / 1 = a := by simp [ennreal.div_def] protected lemma inv_pow {n : ℕ} : (a^n)⁻¹ = (a⁻¹)^n := begin by_cases a = 0; cases a; cases n; simp [*, none_eq_top, some_eq_coe, zero_pow, top_pow, nat.zero_lt_succ] at *, rw [← coe_inv h, ← coe_pow, ← coe_inv, nnreal.inv_pow, coe_pow], rw [← ne.def] at h, rw [← zero_lt_iff_ne_zero] at *, apply pow_pos h end @[simp] lemma inv_inv : (a⁻¹)⁻¹ = a := by by_cases a = 0; cases a; simp [*, none_eq_top, some_eq_coe, -coe_inv, (coe_inv _).symm] at * lemma inv_involutive : function.involutive (λ a:ennreal, a⁻¹) := λ a, ennreal.inv_inv lemma inv_bijective : function.bijective (λ a:ennreal, a⁻¹) := ennreal.inv_involutive.bijective @[simp] lemma inv_eq_inv : a⁻¹ = b⁻¹ ↔ a = b := inv_bijective.1.eq_iff @[simp] lemma inv_eq_top : a⁻¹ = ∞ ↔ a = 0 := inv_zero ▸ inv_eq_inv lemma inv_ne_top : a⁻¹ ≠ ∞ ↔ a ≠ 0 := by simp @[simp] lemma inv_lt_top {x : ennreal} : x⁻¹ < ⊤ ↔ 0 < x := by { simp only [lt_top_iff_ne_top, inv_ne_top, zero_lt_iff_ne_zero] } lemma div_lt_top {x y : ennreal} (h1 : x < ⊤) (h2 : 0 < y) : x / y < ⊤ := mul_lt_top h1 (inv_lt_top.mpr h2) @[simp] lemma inv_eq_zero : a⁻¹ = 0 ↔ a = ∞ := inv_top ▸ inv_eq_inv lemma inv_ne_zero : a⁻¹ ≠ 0 ↔ a ≠ ∞ := by simp @[simp] lemma inv_pos : 0 < a⁻¹ ↔ a ≠ ∞ := zero_lt_iff_ne_zero.trans inv_ne_zero @[simp] lemma inv_lt_inv : a⁻¹ < b⁻¹ ↔ b < a := begin cases a; cases b; simp only [some_eq_coe, none_eq_top, inv_top], { simp only [lt_irrefl] }, { exact inv_pos.trans lt_top_iff_ne_top.symm }, { simp only [not_lt_zero, not_top_lt] }, { cases eq_or_lt_of_le (zero_le a) with ha ha; cases eq_or_lt_of_le (zero_le b) with hb hb, { subst a, subst b, simp }, { subst a, simp }, { subst b, simp [zero_lt_iff_ne_zero, lt_top_iff_ne_top, inv_ne_top] }, { rw [← coe_inv (ne_of_gt ha), ← coe_inv (ne_of_gt hb), coe_lt_coe, coe_lt_coe], simp only [nnreal.coe_lt_coe.symm] at *, exact inv_lt_inv ha hb } } end lemma inv_lt_iff_inv_lt : a⁻¹ < b ↔ b⁻¹ < a := by simpa only [inv_inv] using @inv_lt_inv a b⁻¹ lemma lt_inv_iff_lt_inv : a < b⁻¹ ↔ b < a⁻¹ := by simpa only [inv_inv] using @inv_lt_inv a⁻¹ b @[simp, priority 1100] -- higher than le_inv_iff_mul_le lemma inv_le_inv : a⁻¹ ≤ b⁻¹ ↔ b ≤ a := by simp only [le_iff_lt_or_eq, inv_lt_inv, inv_eq_inv, eq_comm] lemma inv_le_iff_inv_le : a⁻¹ ≤ b ↔ b⁻¹ ≤ a := by simpa only [inv_inv] using @inv_le_inv a b⁻¹ lemma le_inv_iff_le_inv : a ≤ b⁻¹ ↔ b ≤ a⁻¹ := by simpa only [inv_inv] using @inv_le_inv a⁻¹ b @[simp] lemma inv_lt_one : a⁻¹ < 1 ↔ 1 < a := inv_lt_iff_inv_lt.trans $ by rw [inv_one] lemma top_div : ∞ / a = if a = ∞ then 0 else ∞ := by by_cases a = ∞; simp [div_def, top_mul, *] @[simp] lemma div_top : a / ∞ = 0 := by simp only [div_def, inv_top, mul_zero] @[simp] lemma zero_div : 0 / a = 0 := zero_mul a⁻¹ lemma div_eq_top : a / b = ⊤ ↔ (a ≠ 0 ∧ b = 0) ∨ (a = ⊤ ∧ b ≠ ⊤) := by simp [ennreal.div_def, ennreal.mul_eq_top] lemma le_div_iff_mul_le (h0 : b ≠ 0 ∨ c ≠ 0) (ht : b ≠ ⊤ ∨ c ≠ ⊤) : a ≤ c / b ↔ a * b ≤ c := begin cases b, { simp at ht, split, { assume ha, simp at ha, simp [ha] }, { contrapose, assume ha, simp at ha, have : a * ⊤ = ⊤, by simp [ennreal.mul_eq_top, ha], simp [this, ht] } }, by_cases hb : b ≠ 0, { have : (b : ennreal) ≠ 0, by simp [hb], rw [← ennreal.mul_le_mul_left this coe_ne_top], suffices : ↑b * a ≤ (↑b * ↑b⁻¹) * c ↔ a * ↑b ≤ c, { simpa [some_eq_coe, div_def, hb, mul_left_comm, mul_comm, mul_assoc] }, rw [← coe_mul, nnreal.mul_inv_cancel hb, coe_one, one_mul, mul_comm] }, { simp at hb, simp [hb] at h0, have : c / 0 = ⊤, by simp [div_eq_top, h0], simp [hb, this] } end lemma div_le_iff_le_mul (hb0 : b ≠ 0 ∨ c ≠ ⊤) (hbt : b ≠ ⊤ ∨ c ≠ 0) : a / b ≤ c ↔ a ≤ c * b := begin suffices : a * b⁻¹ ≤ c ↔ a ≤ c / b⁻¹, by simpa [div_def], apply (le_div_iff_mul_le _ _).symm, simpa [inv_ne_zero] using hbt, simpa [inv_ne_zero] using hb0 end lemma div_le_of_le_mul (h : a ≤ b * c) : a / c ≤ b := begin by_cases h0 : c = 0, { have : a = 0, by simpa [h0] using h, simp [*] }, by_cases hinf : c = ⊤, by simp [hinf], exact (div_le_iff_le_mul (or.inl h0) (or.inl hinf)).2 h end protected lemma div_lt_iff (h0 : b ≠ 0 ∨ c ≠ 0) (ht : b ≠ ⊤ ∨ c ≠ ⊤) : c / b < a ↔ c < a * b := lt_iff_lt_of_le_iff_le $ le_div_iff_mul_le h0 ht lemma mul_lt_of_lt_div (h : a < b / c) : a * c < b := by { contrapose! h, exact ennreal.div_le_of_le_mul h } lemma inv_le_iff_le_mul : (b = ⊤ → a ≠ 0) → (a = ⊤ → b ≠ 0) → (a⁻¹ ≤ b ↔ 1 ≤ a * b) := begin cases a; cases b; simp [none_eq_top, some_eq_coe, mul_top, top_mul] {contextual := tt}, by_cases a = 0; simp [*, -coe_mul, coe_mul.symm, -coe_inv, (coe_inv _).symm, nnreal.inv_le] end @[simp] lemma le_inv_iff_mul_le : a ≤ b⁻¹ ↔ a * b ≤ 1 := begin cases b, { by_cases a = 0; simp [*, none_eq_top, mul_top] }, by_cases b = 0; simp [*, some_eq_coe, le_div_iff_mul_le], suffices : a ≤ 1 / b ↔ a * b ≤ 1, { simpa [div_def, h] }, exact le_div_iff_mul_le (or.inl (mt coe_eq_coe.1 h)) (or.inl coe_ne_top) end lemma mul_inv_cancel (h0 : a ≠ 0) (ht : a ≠ ⊤) : a * a⁻¹ = 1 := begin lift a to nnreal using ht, norm_cast at h0, norm_cast, exact nnreal.mul_inv_cancel h0 end lemma inv_mul_cancel (h0 : a ≠ 0) (ht : a ≠ ∞) : a⁻¹ * a = 1 := mul_comm a a⁻¹ ▸ mul_inv_cancel h0 ht lemma mul_le_iff_le_inv {a b r : ennreal} (hr₀ : r ≠ 0) (hr₁ : r ≠ ⊤) : (r * a ≤ b ↔ a ≤ r⁻¹ * b) := by rw [← @ennreal.mul_le_mul_left _ a _ hr₀ hr₁, ← mul_assoc, mul_inv_cancel hr₀ hr₁, one_mul] lemma le_of_forall_lt_one_mul_lt : ∀{x y : ennreal}, (∀a<1, a * x ≤ y) → x ≤ y := forall_ennreal.2 $ and.intro (assume r, forall_ennreal.2 $ and.intro (assume q h, coe_le_coe.2 $ nnreal.le_of_forall_lt_one_mul_lt $ assume a ha, begin rw [← coe_le_coe, coe_mul], exact h _ (coe_lt_coe.2 ha) end) (assume h, le_top)) (assume r hr, have ((1 / 2 : nnreal) : ennreal) * ⊤ ≤ r := hr _ (coe_lt_coe.2 ((@nnreal.coe_lt_coe (1/2) 1).1 one_half_lt_one)), have ne : ((1 / 2 : nnreal) : ennreal) ≠ 0, begin rw [(≠), coe_eq_zero], refine zero_lt_iff_ne_zero.1 _, show 0 < (1 / 2 : ℝ), exact div_pos zero_lt_one _root_.two_pos end, by rwa [mul_top, if_neg ne] at this) lemma div_add_div_same {a b c : ennreal} : a / c + b / c = (a + b) / c := eq.symm $ right_distrib a b (c⁻¹) lemma div_self (h0 : a ≠ 0) (hI : a ≠ ∞) : a / a = 1 := mul_inv_cancel h0 hI lemma mul_div_cancel (h0 : a ≠ 0) (hI : a ≠ ∞) : (b / a) * a = b := by rw [div_def, mul_assoc, inv_mul_cancel h0 hI, mul_one] lemma mul_div_cancel' (h0 : a ≠ 0) (hI : a ≠ ∞) : a * (b / a) = b := by rw [mul_comm, mul_div_cancel h0 hI] lemma inv_two_add_inv_two : (2:ennreal)⁻¹ + 2⁻¹ = 1 := by rw [← two_mul, ← div_def, div_self two_ne_zero two_ne_top] lemma add_halves (a : ennreal) : a / 2 + a / 2 = a := by rw [div_def, ← mul_add, inv_two_add_inv_two, mul_one] @[simp] lemma div_zero_iff {a b : ennreal} : a / b = 0 ↔ a = 0 ∨ b = ⊤ := by simp [div_def, mul_eq_zero] @[simp] lemma div_pos_iff {a b : ennreal} : 0 < a / b ↔ a ≠ 0 ∧ b ≠ ⊤ := by simp [zero_lt_iff_ne_zero, not_or_distrib] lemma half_pos {a : ennreal} (h : 0 < a) : 0 < a / 2 := by simp [ne_of_gt h] lemma one_half_lt_one : (2⁻¹:ennreal) < 1 := inv_lt_one.2 $ one_lt_two lemma half_lt_self {a : ennreal} (hz : a ≠ 0) (ht : a ≠ ⊤) : a / 2 < a := begin lift a to nnreal using ht, have h : (2 : ennreal) = ((2 : nnreal) : ennreal), from rfl, have h' : (2 : nnreal) ≠ 0, from _root_.two_ne_zero', rw [h, ← coe_div h', coe_lt_coe], -- `norm_cast` fails to apply `coe_div` norm_cast at hz, exact nnreal.half_lt_self hz end lemma sub_half (h : a ≠ ∞) : a - a / 2 = a / 2 := begin lift a to nnreal using h, exact sub_eq_of_add_eq (mul_ne_top coe_ne_top $ by simp) (add_halves a) end lemma one_sub_inv_two : (1:ennreal) - 2⁻¹ = 2⁻¹ := by simpa only [div_def, one_mul] using sub_half one_ne_top lemma exists_inv_nat_lt {a : ennreal} (h : a ≠ 0) : ∃n:ℕ, (n:ennreal)⁻¹ < a := @inv_inv a ▸ by simp only [inv_lt_inv, ennreal.exists_nat_gt (inv_ne_top.2 h)] lemma exists_nat_mul_gt (ha : a ≠ 0) (hb : b ≠ ⊤) : ∃ n : ℕ, b < n * a := begin have : b / a ≠ ⊤, from mul_ne_top hb (inv_ne_top.2 ha), refine (ennreal.exists_nat_gt this).imp (λ n hn, _), rwa [← ennreal.div_lt_iff (or.inl ha) (or.inr hb)] end end inv section real lemma to_real_add (ha : a ≠ ⊤) (hb : b ≠ ⊤) : (a+b).to_real = a.to_real + b.to_real := begin lift a to nnreal using ha, lift b to nnreal using hb, refl end lemma to_real_add_le : (a+b).to_real ≤ a.to_real + b.to_real := if ha : a = ⊤ then by simp only [ha, top_add, top_to_real, zero_add, to_real_nonneg] else if hb : b = ⊤ then by simp only [hb, add_top, top_to_real, add_zero, to_real_nonneg] else le_of_eq (to_real_add ha hb) lemma of_real_add {p q : ℝ} (hp : 0 ≤ p) (hq : 0 ≤ q) : ennreal.of_real (p + q) = ennreal.of_real p + ennreal.of_real q := by rw [ennreal.of_real, ennreal.of_real, ennreal.of_real, ← coe_add, coe_eq_coe, nnreal.of_real_add hp hq] @[simp] lemma to_real_le_to_real (ha : a ≠ ⊤) (hb : b ≠ ⊤) : a.to_real ≤ b.to_real ↔ a ≤ b := begin lift a to nnreal using ha, lift b to nnreal using hb, norm_cast end @[simp] lemma to_real_lt_to_real (ha : a ≠ ⊤) (hb : b ≠ ⊤) : a.to_real < b.to_real ↔ a < b := begin lift a to nnreal using ha, lift b to nnreal using hb, norm_cast end lemma to_real_max (hr : a ≠ ⊤) (hp : b ≠ ⊤) : ennreal.to_real (max a b) = max (ennreal.to_real a) (ennreal.to_real b) := (le_total a b).elim (λ h, by simp only [h, (ennreal.to_real_le_to_real hr hp).2 h, max_eq_right]) (λ h, by simp only [h, (ennreal.to_real_le_to_real hp hr).2 h, max_eq_left]) lemma to_nnreal_pos_iff : 0 < a.to_nnreal ↔ (0 < a ∧ a ≠ ∞) := begin cases a, { simp [none_eq_top] }, { simp [some_eq_coe] } end lemma to_real_pos_iff : 0 < a.to_real ↔ (0 < a ∧ a ≠ ∞):= (nnreal.coe_pos).trans to_nnreal_pos_iff lemma of_real_le_of_real {p q : ℝ} (h : p ≤ q) : ennreal.of_real p ≤ ennreal.of_real q := by simp [ennreal.of_real, nnreal.of_real_le_of_real h] @[simp] lemma of_real_le_of_real_iff {p q : ℝ} (h : 0 ≤ q) : ennreal.of_real p ≤ ennreal.of_real q ↔ p ≤ q := by rw [ennreal.of_real, ennreal.of_real, coe_le_coe, nnreal.of_real_le_of_real_iff h] @[simp] lemma of_real_lt_of_real_iff {p q : ℝ} (h : 0 < q) : ennreal.of_real p < ennreal.of_real q ↔ p < q := by rw [ennreal.of_real, ennreal.of_real, coe_lt_coe, nnreal.of_real_lt_of_real_iff h] lemma of_real_lt_of_real_iff_of_nonneg {p q : ℝ} (hp : 0 ≤ p) : ennreal.of_real p < ennreal.of_real q ↔ p < q := by rw [ennreal.of_real, ennreal.of_real, coe_lt_coe, nnreal.of_real_lt_of_real_iff_of_nonneg hp] @[simp] lemma of_real_pos {p : ℝ} : 0 < ennreal.of_real p ↔ 0 < p := by simp [ennreal.of_real] @[simp] lemma of_real_eq_zero {p : ℝ} : ennreal.of_real p = 0 ↔ p ≤ 0 := by simp [ennreal.of_real] lemma of_real_le_iff_le_to_real {a : ℝ} {b : ennreal} (hb : b ≠ ⊤) : ennreal.of_real a ≤ b ↔ a ≤ ennreal.to_real b := begin lift b to nnreal using hb, simpa [ennreal.of_real, ennreal.to_real] using nnreal.of_real_le_iff_le_coe end lemma of_real_lt_iff_lt_to_real {a : ℝ} {b : ennreal} (ha : 0 ≤ a) (hb : b ≠ ⊤) : ennreal.of_real a < b ↔ a < ennreal.to_real b := begin lift b to nnreal using hb, simpa [ennreal.of_real, ennreal.to_real] using nnreal.of_real_lt_iff_lt_coe ha end lemma le_of_real_iff_to_real_le {a : ennreal} {b : ℝ} (ha : a ≠ ⊤) (hb : 0 ≤ b) : a ≤ ennreal.of_real b ↔ ennreal.to_real a ≤ b := begin lift a to nnreal using ha, simpa [ennreal.of_real, ennreal.to_real] using nnreal.le_of_real_iff_coe_le hb end lemma to_real_le_of_le_of_real {a : ennreal} {b : ℝ} (hb : 0 ≤ b) (h : a ≤ ennreal.of_real b) : ennreal.to_real a ≤ b := have ha : a ≠ ⊤, from ne_top_of_le_ne_top of_real_ne_top h, (le_of_real_iff_to_real_le ha hb).1 h lemma lt_of_real_iff_to_real_lt {a : ennreal} {b : ℝ} (ha : a ≠ ⊤) : a < ennreal.of_real b ↔ ennreal.to_real a < b := begin lift a to nnreal using ha, simpa [ennreal.of_real, ennreal.to_real] using nnreal.lt_of_real_iff_coe_lt end lemma of_real_mul {p q : ℝ} (hp : 0 ≤ p) : ennreal.of_real (p * q) = (ennreal.of_real p) * (ennreal.of_real q) := by { simp only [ennreal.of_real, coe_mul.symm, coe_eq_coe], exact nnreal.of_real_mul hp } lemma to_real_of_real_mul (c : ℝ) (a : ennreal) (h : 0 ≤ c) : ennreal.to_real ((ennreal.of_real c) * a) = c * ennreal.to_real a := begin cases a, { simp only [none_eq_top, ennreal.to_real, top_to_nnreal, nnreal.coe_zero, mul_zero, mul_top], by_cases h' : c ≤ 0, { rw [if_pos], { simp }, { convert of_real_zero, exact le_antisymm h' h } }, { rw [if_neg], refl, rw [of_real_eq_zero], assumption } }, { simp only [ennreal.to_real, ennreal.to_nnreal], simp only [some_eq_coe, ennreal.of_real, coe_mul.symm, to_nnreal_coe, nnreal.coe_mul], congr, apply nnreal.coe_of_real, exact h } end @[simp] lemma to_real_mul_top (a : ennreal) : ennreal.to_real (a * ⊤) = 0 := begin by_cases h : a = 0, { rw [h, zero_mul, zero_to_real] }, { rw [mul_top, if_neg h, top_to_real] } end @[simp] lemma to_real_top_mul (a : ennreal) : ennreal.to_real (⊤ * a) = 0 := by { rw mul_comm, exact to_real_mul_top _ } lemma to_real_eq_to_real {a b : ennreal} (ha : a < ⊤) (hb : b < ⊤) : ennreal.to_real a = ennreal.to_real b ↔ a = b := begin rw ennreal.lt_top_iff_ne_top at *, split, { assume h, apply le_antisymm, rw ← to_real_le_to_real ha hb, exact le_of_eq h, rw ← to_real_le_to_real hb ha, exact le_of_eq h.symm }, { assume h, rw h } end lemma to_real_mul_to_real {a b : ennreal} : (ennreal.to_real a) * (ennreal.to_real b) = ennreal.to_real (a * b) := begin by_cases ha : a = ⊤, { rw ha, simp }, by_cases hb : b = ⊤, { rw hb, simp }, have ha : ennreal.of_real (ennreal.to_real a) = a := of_real_to_real ha, have hb : ennreal.of_real (ennreal.to_real b) = b := of_real_to_real hb, conv_rhs { rw [← ha, ← hb, ← of_real_mul to_real_nonneg] }, rw [to_real_of_real (mul_nonneg to_real_nonneg to_real_nonneg)] end end real section infi variables {ι : Sort*} {f g : ι → ennreal} lemma infi_add : infi f + a = ⨅i, f i + a := le_antisymm (le_infi $ assume i, add_le_add (infi_le _ _) $ le_refl _) (ennreal.sub_le_iff_le_add.1 $ le_infi $ assume i, ennreal.sub_le_iff_le_add.2 $ infi_le _ _) lemma supr_sub : (⨆i, f i) - a = (⨆i, f i - a) := le_antisymm (ennreal.sub_le_iff_le_add.2 $ supr_le $ assume i, ennreal.sub_le_iff_le_add.1 $ le_supr _ i) (supr_le $ assume i, ennreal.sub_le_sub (le_supr _ _) (le_refl a)) lemma sub_infi : a - (⨅i, f i) = (⨆i, a - f i) := begin refine (eq_of_forall_ge_iff $ λ c, _), rw [ennreal.sub_le_iff_le_add, add_comm, infi_add], simp [ennreal.sub_le_iff_le_add, sub_eq_add_neg, add_comm], end lemma Inf_add {s : set ennreal} : Inf s + a = ⨅b∈s, b + a := by simp [Inf_eq_infi, infi_add] lemma add_infi {a : ennreal} : a + infi f = ⨅b, a + f b := by rw [add_comm, infi_add]; simp [add_comm] lemma infi_add_infi (h : ∀i j, ∃k, f k + g k ≤ f i + g j) : infi f + infi g = (⨅a, f a + g a) := suffices (⨅a, f a + g a) ≤ infi f + infi g, from le_antisymm (le_infi $ assume a, add_le_add (infi_le _ _) (infi_le _ _)) this, calc (⨅a, f a + g a) ≤ (⨅ a a', f a + g a') : le_infi $ assume a, le_infi $ assume a', let ⟨k, h⟩ := h a a' in infi_le_of_le k h ... ≤ infi f + infi g : by simp [add_infi, infi_add, -add_comm, -le_infi_iff]; exact le_refl _ lemma infi_sum {f : ι → α → ennreal} {s : finset α} [nonempty ι] (h : ∀(t : finset α) (i j : ι), ∃k, ∀a∈t, f k a ≤ f i a ∧ f k a ≤ f j a) : (⨅i, ∑ a in s, f i a) = ∑ a in s, ⨅i, f i a := finset.induction_on s (by simp) $ assume a s ha ih, have ∀ (i j : ι), ∃ (k : ι), f k a + ∑ b in s, f k b ≤ f i a + ∑ b in s, f j b, from assume i j, let ⟨k, hk⟩ := h (insert a s) i j in ⟨k, add_le_add (hk a (finset.mem_insert_self _ _)).left $ finset.sum_le_sum $ assume a ha, (hk _ $ finset.mem_insert_of_mem ha).right⟩, by simp [ha, ih.symm, infi_add_infi this] lemma infi_mul {ι} [nonempty ι] {f : ι → ennreal} {x : ennreal} (h : x ≠ ⊤) : infi f * x = ⨅i, f i * x := begin by_cases h2 : x = 0, simp only [h2, mul_zero, infi_const], refine le_antisymm (le_infi $ λ i, mul_right_mono $ infi_le _ _) ((div_le_iff_le_mul (or.inl h2) $ or.inl h).mp $ le_infi $ λ i, (div_le_iff_le_mul (or.inl h2) $ or.inl h).mpr $ infi_le _ _) end lemma mul_infi {ι} [nonempty ι] {f : ι → ennreal} {x : ennreal} (h : x ≠ ⊤) : x * infi f = ⨅i, x * f i := by { rw [mul_comm, infi_mul h], simp only [mul_comm], assumption } /-! `supr_mul`, `mul_supr` and variants are in `topology.instances.ennreal`. -/ end infi section supr lemma supr_coe_nat : (⨆n:ℕ, (n : ennreal)) = ⊤ := (supr_eq_top _).2 $ assume b hb, ennreal.exists_nat_gt (lt_top_iff_ne_top.1 hb) end supr end ennreal
01d621ff8a40a4b6369c3340dd1389085d89f7c6
bb31430994044506fa42fd667e2d556327e18dfe
/src/data/real/cau_seq_completion.lean
0769af1d4a5a53721f72c2e4c3b719e9f7251b03
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
12,776
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Robert Y. Lewis -/ import data.real.cau_seq /-! # Cauchy completion This file generalizes the Cauchy completion of `(ℚ, abs)` to the completion of a ring with absolute value. -/ namespace cau_seq.completion open cau_seq section variables {α : Type*} [linear_ordered_field α] variables {β : Type*} [ring β] (abv : β → α) [is_absolute_value abv] /-- The Cauchy completion of a ring with absolute value. -/ def Cauchy := @quotient (cau_seq _ abv) cau_seq.equiv variables {abv} /-- The map from Cauchy sequences into the Cauchy completion. -/ def mk : cau_seq _ abv → Cauchy abv := quotient.mk @[simp] theorem mk_eq_mk (f) : @eq (Cauchy abv) ⟦f⟧ (mk f) := rfl theorem mk_eq {f g : cau_seq _ abv} : mk f = mk g ↔ f ≈ g := quotient.eq /-- The map from the original ring into the Cauchy completion. -/ def of_rat (x : β) : Cauchy abv := mk (const abv x) instance : has_zero (Cauchy abv) := ⟨of_rat 0⟩ instance : has_one (Cauchy abv) := ⟨of_rat 1⟩ instance : inhabited (Cauchy abv) := ⟨0⟩ theorem of_rat_zero : (of_rat 0 : Cauchy abv) = 0 := rfl theorem of_rat_one : (of_rat 1 : Cauchy abv) = 1 := rfl @[simp] theorem mk_eq_zero {f : cau_seq _ abv} : mk f = 0 ↔ lim_zero f := by have : mk f = 0 ↔ lim_zero (f - 0) := quotient.eq; rwa sub_zero at this instance : has_add (Cauchy abv) := ⟨quotient.map₂ (+) $ λ f₁ g₁ hf f₂ g₂ hg, add_equiv_add hf hg⟩ @[simp] theorem mk_add (f g : cau_seq β abv) : mk f + mk g = mk (f + g) := rfl instance : has_neg (Cauchy abv) := ⟨quotient.map has_neg.neg $ λ f₁ f₂ hf, neg_equiv_neg hf⟩ @[simp] theorem mk_neg (f : cau_seq β abv) : -mk f = mk (-f) := rfl instance : has_mul (Cauchy abv) := ⟨quotient.map₂ (*) $ λ f₁ g₁ hf f₂ g₂ hg, mul_equiv_mul hf hg⟩ @[simp] theorem mk_mul (f g : cau_seq β abv) : mk f * mk g = mk (f * g) := rfl instance : has_sub (Cauchy abv) := ⟨quotient.map₂ has_sub.sub $ λ f₁ g₁ hf f₂ g₂ hg, sub_equiv_sub hf hg⟩ @[simp] theorem mk_sub (f g : cau_seq β abv) : mk f - mk g = mk (f - g) := rfl instance {γ : Type*} [has_smul γ β] [is_scalar_tower γ β β] : has_smul γ (Cauchy abv) := ⟨λ c, quotient.map ((•) c) $ λ f₁ g₁ hf, smul_equiv_smul _ hf⟩ @[simp] theorem mk_smul {γ : Type*} [has_smul γ β] [is_scalar_tower γ β β] (c : γ) (f : cau_seq β abv) : c • mk f = mk (c • f) := rfl instance : has_pow (Cauchy abv) ℕ := ⟨λ x n, quotient.map (^ n) (λ f₁ g₁ hf, pow_equiv_pow hf _) x⟩ @[simp] theorem mk_pow (n : ℕ) (f : cau_seq β abv) : mk f ^ n = mk (f ^ n) := rfl instance : has_nat_cast (Cauchy abv) := ⟨λ n, mk n⟩ instance : has_int_cast (Cauchy abv) := ⟨λ n, mk n⟩ @[simp] theorem of_rat_nat_cast (n : ℕ) : (of_rat n : Cauchy abv) = n := rfl @[simp] theorem of_rat_int_cast (z : ℤ) : (of_rat z : Cauchy abv) = z := rfl theorem of_rat_add (x y : β) : of_rat (x + y) = (of_rat x + of_rat y : Cauchy abv) := congr_arg mk (const_add _ _) theorem of_rat_neg (x : β) : of_rat (-x) = (-of_rat x : Cauchy abv) := congr_arg mk (const_neg _) theorem of_rat_mul (x y : β) : of_rat (x * y) = (of_rat x * of_rat y : Cauchy abv) := congr_arg mk (const_mul _ _) private lemma zero_def : 0 = (mk 0 : Cauchy abv) := rfl private lemma one_def : 1 = (mk 1 : Cauchy abv) := rfl instance : ring (Cauchy abv) := function.surjective.ring mk (surjective_quotient_mk _) zero_def.symm one_def.symm (λ _ _, (mk_add _ _).symm) (λ _ _, (mk_mul _ _).symm) (λ _, (mk_neg _).symm) (λ _ _, (mk_sub _ _).symm) (λ _ _, (mk_smul _ _).symm) (λ _ _, (mk_smul _ _).symm) (λ _ _, (mk_pow _ _).symm) (λ _, rfl) (λ _, rfl) /-- `cau_seq.completion.of_rat` as a `ring_hom` -/ @[simps] def of_rat_ring_hom : β →+* Cauchy abv := { to_fun := of_rat, map_zero' := of_rat_zero, map_one' := of_rat_one, map_add' := of_rat_add, map_mul' := of_rat_mul, } theorem of_rat_sub (x y : β) : of_rat (x - y) = (of_rat x - of_rat y : Cauchy abv) := congr_arg mk (const_sub _ _) end section variables {α : Type*} [linear_ordered_field α] variables {β : Type*} [comm_ring β] {abv : β → α} [is_absolute_value abv] instance : comm_ring (Cauchy abv) := function.surjective.comm_ring mk (surjective_quotient_mk _) zero_def.symm one_def.symm (λ _ _, (mk_add _ _).symm) (λ _ _, (mk_mul _ _).symm) (λ _, (mk_neg _).symm) (λ _ _, (mk_sub _ _).symm) (λ _ _, (mk_smul _ _).symm) (λ _ _, (mk_smul _ _).symm) (λ _ _, (mk_pow _ _).symm) (λ _, rfl) (λ _, rfl) end open_locale classical section variables {α : Type*} [linear_ordered_field α] variables {β : Type*} [division_ring β] {abv : β → α} [is_absolute_value abv] instance : has_rat_cast (Cauchy abv) := ⟨λ q, of_rat q⟩ @[simp] theorem of_rat_rat_cast (q : ℚ) : of_rat (↑q : β) = (q : Cauchy abv) := rfl noncomputable instance : has_inv (Cauchy abv) := ⟨λ x, quotient.lift_on x (λ f, mk $ if h : lim_zero f then 0 else inv f h) $ λ f g fg, begin have := lim_zero_congr fg, by_cases hf : lim_zero f, { simp [hf, this.1 hf, setoid.refl] }, { have hg := mt this.2 hf, simp [hf, hg], have If : mk (inv f hf) * mk f = 1 := mk_eq.2 (inv_mul_cancel hf), have Ig : mk (inv g hg) * mk g = 1 := mk_eq.2 (inv_mul_cancel hg), have Ig' : mk g * mk (inv g hg) = 1 := mk_eq.2 (mul_inv_cancel hg), rw [mk_eq.2 fg, ← Ig] at If, rw [← mul_one (mk (inv f hf)), ← Ig', ← mul_assoc, If, mul_assoc, Ig', mul_one] } end⟩ @[simp] theorem inv_zero : (0 : Cauchy abv)⁻¹ = 0 := congr_arg mk $ by rw dif_pos; [refl, exact zero_lim_zero] @[simp] theorem inv_mk {f} (hf) : (@mk α _ β _ abv _ f)⁻¹ = mk (inv f hf) := congr_arg mk $ by rw dif_neg lemma cau_seq_zero_ne_one : ¬ (0 : cau_seq _ abv) ≈ 1 := λ h, have lim_zero (1 - 0), from setoid.symm h, have lim_zero 1, by simpa, one_ne_zero $ const_lim_zero.1 this lemma zero_ne_one : (0 : Cauchy abv) ≠ 1 := λ h, cau_seq_zero_ne_one $ mk_eq.1 h protected theorem inv_mul_cancel {x : Cauchy abv} : x ≠ 0 → x⁻¹ * x = 1 := quotient.induction_on x $ λ f hf, begin simp at hf, simp [hf], exact quotient.sound (cau_seq.inv_mul_cancel hf) end protected theorem mul_inv_cancel {x : Cauchy abv} : x ≠ 0 → x * x⁻¹ = 1 := quotient.induction_on x $ λ f hf, begin simp at hf, simp [hf], exact quotient.sound (cau_seq.mul_inv_cancel hf) end theorem of_rat_inv (x : β) : of_rat (x⁻¹) = ((of_rat x)⁻¹ : Cauchy abv) := congr_arg mk $ by split_ifs with h; [simp [const_lim_zero.1 h], refl] /-- The Cauchy completion forms a division ring. -/ noncomputable instance : division_ring (Cauchy abv) := { inv := has_inv.inv, mul_inv_cancel := λ x, cau_seq.completion.mul_inv_cancel, exists_pair_ne := ⟨0, 1, zero_ne_one⟩, inv_zero := inv_zero, rat_cast := λ q, of_rat q, rat_cast_mk := λ n d hd hnd, by rw [rat.cast_mk', of_rat_mul, of_rat_int_cast, of_rat_inv, of_rat_nat_cast], .. Cauchy.ring } theorem of_rat_div (x y : β) : of_rat (x / y) = (of_rat x / of_rat y : Cauchy abv) := by simp only [div_eq_mul_inv, of_rat_inv, of_rat_mul] /-- Show the first 10 items of a representative of this equivalence class of cauchy sequences. The representative chosen is the one passed in the VM to `quot.mk`, so two cauchy sequences converging to the same number may be printed differently. -/ meta instance [has_repr β] : has_repr (Cauchy abv) := { repr := λ r, let N := 10, seq := r.unquot in "(sorry /- " ++ (", ".intercalate $ (list.range N).map $ repr ∘ seq) ++ ", ... -/)" } end section variables {α : Type*} [linear_ordered_field α] variables {β : Type*} [field β] {abv : β → α} [is_absolute_value abv] /-- The Cauchy completion forms a field. -/ noncomputable instance : field (Cauchy abv) := { .. Cauchy.division_ring, .. Cauchy.comm_ring } end end cau_seq.completion variables {α : Type*} [linear_ordered_field α] namespace cau_seq section variables (β : Type*) [ring β] (abv : β → α) [is_absolute_value abv] /-- A class stating that a ring with an absolute value is complete, i.e. every Cauchy sequence has a limit. -/ class is_complete : Prop := (is_complete : ∀ s : cau_seq β abv, ∃ b : β, s ≈ const abv b) end section variables {β : Type*} [ring β] {abv : β → α} [is_absolute_value abv] variable [is_complete β abv] lemma complete : ∀ s : cau_seq β abv, ∃ b : β, s ≈ const abv b := is_complete.is_complete /-- The limit of a Cauchy sequence in a complete ring. Chosen non-computably. -/ noncomputable def lim (s : cau_seq β abv) : β := classical.some (complete s) lemma equiv_lim (s : cau_seq β abv) : s ≈ const abv (lim s) := classical.some_spec (complete s) lemma eq_lim_of_const_equiv {f : cau_seq β abv} {x : β} (h : cau_seq.const abv x ≈ f) : x = lim f := const_equiv.mp $ setoid.trans h $ equiv_lim f lemma lim_eq_of_equiv_const {f : cau_seq β abv} {x : β} (h : f ≈ cau_seq.const abv x) : lim f = x := (eq_lim_of_const_equiv $ setoid.symm h).symm lemma lim_eq_lim_of_equiv {f g : cau_seq β abv} (h : f ≈ g) : lim f = lim g := lim_eq_of_equiv_const $ setoid.trans h $ equiv_lim g @[simp] lemma lim_const (x : β) : lim (const abv x) = x := lim_eq_of_equiv_const $ setoid.refl _ lemma lim_add (f g : cau_seq β abv) : lim f + lim g = lim (f + g) := eq_lim_of_const_equiv $ show lim_zero (const abv (lim f + lim g) - (f + g)), by rw [const_add, add_sub_add_comm]; exact add_lim_zero (setoid.symm (equiv_lim f)) (setoid.symm (equiv_lim g)) lemma lim_mul_lim (f g : cau_seq β abv) : lim f * lim g = lim (f * g) := eq_lim_of_const_equiv $ show lim_zero (const abv (lim f * lim g) - f * g), from have h : const abv (lim f * lim g) - f * g = (const abv (lim f) - f) * g + const abv (lim f) * (const abv (lim g) - g) := by simp [const_mul (lim f), mul_add, add_mul, sub_eq_add_neg, add_comm, add_left_comm], by rw h; exact add_lim_zero (mul_lim_zero_left _ (setoid.symm (equiv_lim _))) (mul_lim_zero_right _ (setoid.symm (equiv_lim _))) lemma lim_mul (f : cau_seq β abv) (x : β) : lim f * x = lim (f * const abv x) := by rw [← lim_mul_lim, lim_const] lemma lim_neg (f : cau_seq β abv) : lim (-f) = -lim f := lim_eq_of_equiv_const (show lim_zero (-f - const abv (-lim f)), by rw [const_neg, sub_neg_eq_add, add_comm, ← sub_eq_add_neg]; exact setoid.symm (equiv_lim f)) lemma lim_eq_zero_iff (f : cau_seq β abv) : lim f = 0 ↔ lim_zero f := ⟨assume h, by have hf := equiv_lim f; rw h at hf; exact (lim_zero_congr hf).mpr (const_lim_zero.mpr rfl), assume h, have h₁ : f = (f - const abv 0) := ext (λ n, by simp [sub_apply, const_apply]), by rw h₁ at h; exact lim_eq_of_equiv_const h ⟩ end section variables {β : Type*} [field β] {abv : β → α} [is_absolute_value abv] [is_complete β abv] lemma lim_inv {f : cau_seq β abv} (hf : ¬ lim_zero f) : lim (inv f hf) = (lim f)⁻¹ := have hl : lim f ≠ 0 := by rwa ← lim_eq_zero_iff at hf, lim_eq_of_equiv_const $ show lim_zero (inv f hf - const abv (lim f)⁻¹), from have h₁ : ∀ (g f : cau_seq β abv) (hf : ¬ lim_zero f), lim_zero (g - f * inv f hf * g) := λ g f hf, by rw [← one_mul g, ← mul_assoc, ← sub_mul, mul_one, mul_comm, mul_comm f]; exact mul_lim_zero_right _ (setoid.symm (cau_seq.inv_mul_cancel _)), have h₂ : lim_zero ((inv f hf - const abv (lim f)⁻¹) - (const abv (lim f) - f) * (inv f hf * const abv (lim f)⁻¹)) := by rw [sub_mul, ← sub_add, sub_sub, sub_add_eq_sub_sub, sub_right_comm, sub_add]; exact show lim_zero (inv f hf - const abv (lim f) * (inv f hf * const abv (lim f)⁻¹) - (const abv (lim f)⁻¹ - f * (inv f hf * const abv (lim f)⁻¹))), from sub_lim_zero (by rw [← mul_assoc, mul_right_comm, const_inv hl]; exact h₁ _ _ _) (by rw [← mul_assoc]; exact h₁ _ _ _), (lim_zero_congr h₂).mpr $ mul_lim_zero_left _ (setoid.symm (equiv_lim f)) end section variables [is_complete α abs] lemma lim_le {f : cau_seq α abs} {x : α} (h : f ≤ cau_seq.const abs x) : lim f ≤ x := cau_seq.const_le.1 $ cau_seq.le_of_eq_of_le (setoid.symm (equiv_lim f)) h lemma le_lim {f : cau_seq α abs} {x : α} (h : cau_seq.const abs x ≤ f) : x ≤ lim f := cau_seq.const_le.1 $ cau_seq.le_of_le_of_eq h (equiv_lim f) lemma lt_lim {f : cau_seq α abs} {x : α} (h : cau_seq.const abs x < f) : x < lim f := cau_seq.const_lt.1 $ cau_seq.lt_of_lt_of_eq h (equiv_lim f) lemma lim_lt {f : cau_seq α abs} {x : α} (h : f < cau_seq.const abs x) : lim f < x := cau_seq.const_lt.1 $ cau_seq.lt_of_eq_of_lt (setoid.symm (equiv_lim f)) h end end cau_seq
a009c05764e292f0a5232165e583e23c639f715a
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/group_theory/index.lean
ce2d7fe11c1514dd225488ff4fe3c67ab117bfa9
[ "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
18,694
lean
/- Copyright (c) 2021 Thomas Browning. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning -/ import data.finite.card import group_theory.finiteness import group_theory.group_action.quotient /-! # Index of a Subgroup > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define the index of a subgroup, and prove several divisibility properties. Several theorems proved in this file are known as Lagrange's theorem. ## Main definitions - `H.index` : the index of `H : subgroup G` as a natural number, and returns 0 if the index is infinite. - `H.relindex K` : the relative index of `H : subgroup G` in `K : subgroup G` as a natural number, and returns 0 if the relative index is infinite. # Main results - `card_mul_index` : `nat.card H * H.index = nat.card G` - `index_mul_card` : `H.index * fintype.card H = fintype.card G` - `index_dvd_card` : `H.index ∣ fintype.card G` - `index_eq_mul_of_le` : If `H ≤ K`, then `H.index = K.index * (H.subgroup_of K).index` - `index_dvd_of_le` : If `H ≤ K`, then `K.index ∣ H.index` - `relindex_mul_relindex` : `relindex` is multiplicative in towers -/ namespace subgroup open_locale big_operators cardinal variables {G : Type*} [group G] (H K L : subgroup G) /-- The index of a subgroup as a natural number, and returns 0 if the index is infinite. -/ @[to_additive "The index of a subgroup as a natural number, and returns 0 if the index is infinite."] noncomputable def index : ℕ := nat.card (G ⧸ H) /-- The relative index of a subgroup as a natural number, and returns 0 if the relative index is infinite. -/ @[to_additive "The relative index of a subgroup as a natural number, and returns 0 if the relative index is infinite."] noncomputable def relindex : ℕ := (H.subgroup_of K).index @[to_additive] lemma index_comap_of_surjective {G' : Type*} [group G'] {f : G' →* G} (hf : function.surjective f) : (H.comap f).index = H.index := begin letI := quotient_group.left_rel H, letI := quotient_group.left_rel (H.comap f), have key : ∀ x y : G', setoid.r x y ↔ setoid.r (f x) (f y), { simp only [quotient_group.left_rel_apply], exact λ x y, iff_of_eq (congr_arg (∈ H) (by rw [f.map_mul, f.map_inv])) }, refine cardinal.to_nat_congr (equiv.of_bijective (quotient.map' f (λ x y, (key x y).mp)) ⟨_, _⟩), { simp_rw [←quotient.eq'] at key, refine quotient.ind' (λ x, _), refine quotient.ind' (λ y, _), exact (key x y).mpr }, { refine quotient.ind' (λ x, _), obtain ⟨y, hy⟩ := hf x, exact ⟨y, (quotient.map'_mk' f _ y).trans (congr_arg quotient.mk' hy)⟩ }, end @[to_additive] lemma index_comap {G' : Type*} [group G'] (f : G' →* G) : (H.comap f).index = H.relindex f.range := eq.trans (congr_arg index (by refl)) ((H.subgroup_of f.range).index_comap_of_surjective f.range_restrict_surjective) @[to_additive] lemma relindex_comap {G' : Type*} [group G'] (f : G' →* G) (K : subgroup G') : relindex (comap f H) K = relindex H (map f K) := by rw [relindex, subgroup_of, comap_comap, index_comap, ← f.map_range, K.subtype_range] variables {H K L} @[to_additive relindex_mul_index] lemma relindex_mul_index (h : H ≤ K) : H.relindex K * K.index = H.index := ((mul_comm _ _).trans (cardinal.to_nat_mul _ _).symm).trans (congr_arg cardinal.to_nat (equiv.cardinal_eq (quotient_equiv_prod_of_le h))).symm @[to_additive] lemma index_dvd_of_le (h : H ≤ K) : K.index ∣ H.index := dvd_of_mul_left_eq (H.relindex K) (relindex_mul_index h) @[to_additive] lemma relindex_dvd_index_of_le (h : H ≤ K) : H.relindex K ∣ H.index := dvd_of_mul_right_eq K.index (relindex_mul_index h) @[to_additive] lemma relindex_subgroup_of (hKL : K ≤ L) : (H.subgroup_of L).relindex (K.subgroup_of L) = H.relindex K := ((index_comap (H.subgroup_of L) (inclusion hKL)).trans (congr_arg _ (inclusion_range hKL))).symm variables (H K L) @[to_additive relindex_mul_relindex] lemma relindex_mul_relindex (hHK : H ≤ K) (hKL : K ≤ L) : H.relindex K * K.relindex L = H.relindex L := begin rw [←relindex_subgroup_of hKL], exact relindex_mul_index (λ x hx, hHK hx), end @[to_additive] lemma inf_relindex_right : (H ⊓ K).relindex K = H.relindex K := by rw [relindex, relindex, inf_subgroup_of_right] @[to_additive] lemma inf_relindex_left : (H ⊓ K).relindex H = K.relindex H := by rw [inf_comm, inf_relindex_right] @[to_additive relindex_inf_mul_relindex] lemma relindex_inf_mul_relindex : H.relindex (K ⊓ L) * K.relindex L = (H ⊓ K).relindex L := by rw [←inf_relindex_right H (K ⊓ L), ←inf_relindex_right K L, ←inf_relindex_right (H ⊓ K) L, inf_assoc, relindex_mul_relindex (H ⊓ (K ⊓ L)) (K ⊓ L) L inf_le_right inf_le_right] @[simp, to_additive] lemma relindex_sup_right [K.normal] : K.relindex (H ⊔ K) = K.relindex H := nat.card_congr (quotient_group.quotient_inf_equiv_prod_normal_quotient H K).to_equiv.symm @[simp, to_additive] lemma relindex_sup_left [K.normal] : K.relindex (K ⊔ H) = K.relindex H := by rw [sup_comm, relindex_sup_right] @[to_additive] lemma relindex_dvd_index_of_normal [H.normal] : H.relindex K ∣ H.index := relindex_sup_right K H ▸ relindex_dvd_index_of_le le_sup_right variables {H K} @[to_additive] lemma relindex_dvd_of_le_left (hHK : H ≤ K) : K.relindex L ∣ H.relindex L := inf_of_le_left hHK ▸ dvd_of_mul_left_eq _ (relindex_inf_mul_relindex _ _ _) /-- A subgroup has index two if and only if there exists `a` such that for all `b`, exactly one of `b * a` and `b` belong to `H`. -/ @[to_additive "/-- An additive subgroup has index two if and only if there exists `a` such that for all `b`, exactly one of `b + a` and `b` belong to `H`. -/"] lemma index_eq_two_iff : H.index = 2 ↔ ∃ a, ∀ b, xor (b * a ∈ H) (b ∈ H) := begin simp only [index, nat.card_eq_two_iff' ((1 : G) : G ⧸ H), exists_unique, inv_mem_iff, quotient_group.exists_coe, quotient_group.forall_coe, ne.def, quotient_group.eq, mul_one, xor_iff_iff_not], refine exists_congr (λ a, ⟨λ ha b, ⟨λ hba hb, _, λ hb, _⟩, λ ha, ⟨_, λ b hb, _⟩⟩), { exact ha.1 ((mul_mem_cancel_left hb).1 hba) }, { exact inv_inv b ▸ ha.2 _ (mt inv_mem_iff.1 hb) }, { rw [← inv_mem_iff, ← ha, inv_mul_self], exact one_mem _ }, { rwa [ha, inv_mem_iff] } end @[to_additive] lemma mul_mem_iff_of_index_two (h : H.index = 2) {a b : G} : a * b ∈ H ↔ (a ∈ H ↔ b ∈ H) := begin by_cases ha : a ∈ H, { simp only [ha, true_iff, mul_mem_cancel_left ha] }, by_cases hb : b ∈ H, { simp only [hb, iff_true, mul_mem_cancel_right hb] }, simp only [ha, hb, iff_self, iff_true], rcases index_eq_two_iff.1 h with ⟨c, hc⟩, refine (hc _).or.resolve_left _, rwa [mul_assoc, mul_mem_cancel_right ((hc _).or.resolve_right hb)] end @[to_additive] lemma mul_self_mem_of_index_two (h : H.index = 2) (a : G) : a * a ∈ H := by rw [mul_mem_iff_of_index_two h] @[to_additive two_smul_mem_of_index_two] lemma sq_mem_of_index_two (h : H.index = 2) (a : G) : a ^ 2 ∈ H := (pow_two a).symm ▸ mul_self_mem_of_index_two h a variables (H K) @[simp, to_additive] lemma index_top : (⊤ : subgroup G).index = 1 := cardinal.to_nat_eq_one_iff_unique.mpr ⟨quotient_group.subsingleton_quotient_top, ⟨1⟩⟩ @[simp, to_additive] lemma index_bot : (⊥ : subgroup G).index = nat.card G := cardinal.to_nat_congr (quotient_group.quotient_bot.to_equiv) @[to_additive] lemma index_bot_eq_card [fintype G] : (⊥ : subgroup G).index = fintype.card G := index_bot.trans nat.card_eq_fintype_card @[simp, to_additive] lemma relindex_top_left : (⊤ : subgroup G).relindex H = 1 := index_top @[simp, to_additive] lemma relindex_top_right : H.relindex ⊤ = H.index := by rw [←relindex_mul_index (show H ≤ ⊤, from le_top), index_top, mul_one] @[simp, to_additive] lemma relindex_bot_left : (⊥ : subgroup G).relindex H = nat.card H := by rw [relindex, bot_subgroup_of, index_bot] @[to_additive] lemma relindex_bot_left_eq_card [fintype H] : (⊥ : subgroup G).relindex H = fintype.card H := H.relindex_bot_left.trans nat.card_eq_fintype_card @[simp, to_additive] lemma relindex_bot_right : H.relindex ⊥ = 1 := by rw [relindex, subgroup_of_bot_eq_top, index_top] @[simp, to_additive] lemma relindex_self : H.relindex H = 1 := by rw [relindex, subgroup_of_self, index_top] @[to_additive] lemma index_ker {H} [group H] (f : G →* H) : f.ker.index = nat.card (set.range f) := by { rw [← monoid_hom.comap_bot, index_comap, relindex_bot_left], refl } @[to_additive] lemma relindex_ker {H} [group H] (f : G →* H) (K : subgroup G) : f.ker.relindex K = nat.card (f '' K) := by { rw [← monoid_hom.comap_bot, relindex_comap, relindex_bot_left], refl } @[simp, to_additive card_mul_index] lemma card_mul_index : nat.card H * H.index = nat.card G := by { rw [←relindex_bot_left, ←index_bot], exact relindex_mul_index bot_le } @[to_additive] lemma nat_card_dvd_of_injective {G H : Type*} [group G] [group H] (f : G →* H) (hf : function.injective f) : nat.card G ∣ nat.card H := begin rw nat.card_congr (monoid_hom.of_injective hf).to_equiv, exact dvd.intro f.range.index f.range.card_mul_index, end @[to_additive] lemma nat_card_dvd_of_le (hHK : H ≤ K) : nat.card H ∣ nat.card K := nat_card_dvd_of_injective (inclusion hHK) (inclusion_injective hHK) @[to_additive] lemma nat_card_dvd_of_surjective {G H : Type*} [group G] [group H] (f : G →* H) (hf : function.surjective f) : nat.card H ∣ nat.card G := begin rw ← nat.card_congr (quotient_group.quotient_ker_equiv_of_surjective f hf).to_equiv, exact dvd.intro_left (nat.card f.ker) f.ker.card_mul_index, end @[to_additive] lemma card_dvd_of_surjective {G H : Type*} [group G] [group H] [fintype G] [fintype H] (f : G →* H) (hf : function.surjective f) : fintype.card H ∣ fintype.card G := by simp only [←nat.card_eq_fintype_card, nat_card_dvd_of_surjective f hf] @[to_additive] lemma index_map {G' : Type*} [group G'] (f : G →* G') : (H.map f).index = (H ⊔ f.ker).index * f.range.index := by rw [←comap_map_eq, index_comap, relindex_mul_index (H.map_le_range f)] @[to_additive] lemma index_map_dvd {G' : Type*} [group G'] {f : G →* G'} (hf : function.surjective f) : (H.map f).index ∣ H.index := begin rw [index_map, f.range_top_of_surjective hf, index_top, mul_one], exact index_dvd_of_le le_sup_left, end @[to_additive] lemma dvd_index_map {G' : Type*} [group G'] {f : G →* G'} (hf : f.ker ≤ H) : H.index ∣ (H.map f).index := begin rw [index_map, sup_of_le_left hf], apply dvd_mul_right, end @[to_additive] lemma index_map_eq {G' : Type*} [group G'] {f : G →* G'} (hf1 : function.surjective f) (hf2 : f.ker ≤ H) : (H.map f).index = H.index := nat.dvd_antisymm (H.index_map_dvd hf1) (H.dvd_index_map hf2) @[to_additive] lemma index_eq_card [fintype (G ⧸ H)] : H.index = fintype.card (G ⧸ H) := nat.card_eq_fintype_card @[to_additive index_mul_card] lemma index_mul_card [fintype G] [hH : fintype H] : H.index * fintype.card H = fintype.card G := by rw [←relindex_bot_left_eq_card, ←index_bot_eq_card, mul_comm]; exact relindex_mul_index bot_le @[to_additive] lemma index_dvd_card [fintype G] : H.index ∣ fintype.card G := begin classical, exact ⟨fintype.card H, H.index_mul_card.symm⟩, end variables {H K L} @[to_additive] lemma relindex_eq_zero_of_le_left (hHK : H ≤ K) (hKL : K.relindex L = 0) : H.relindex L = 0 := eq_zero_of_zero_dvd (hKL ▸ (relindex_dvd_of_le_left L hHK)) @[to_additive] lemma relindex_eq_zero_of_le_right (hKL : K ≤ L) (hHK : H.relindex K = 0) : H.relindex L = 0 := finite.card_eq_zero_of_embedding (quotient_subgroup_of_embedding_of_le H hKL) hHK @[to_additive] lemma index_eq_zero_of_relindex_eq_zero (h : H.relindex K = 0) : H.index = 0 := H.relindex_top_right.symm.trans (relindex_eq_zero_of_le_right le_top h) @[to_additive] lemma relindex_le_of_le_left (hHK : H ≤ K) (hHL : H.relindex L ≠ 0) : K.relindex L ≤ H.relindex L := nat.le_of_dvd (nat.pos_of_ne_zero hHL) (relindex_dvd_of_le_left L hHK) @[to_additive] lemma relindex_le_of_le_right (hKL : K ≤ L) (hHL : H.relindex L ≠ 0) : H.relindex K ≤ H.relindex L := finite.card_le_of_embedding' (quotient_subgroup_of_embedding_of_le H hKL) (λ h, (hHL h).elim) @[to_additive] lemma relindex_ne_zero_trans (hHK : H.relindex K ≠ 0) (hKL : K.relindex L ≠ 0) : H.relindex L ≠ 0 := λ h, mul_ne_zero (mt (relindex_eq_zero_of_le_right (show K ⊓ L ≤ K, from inf_le_left)) hHK) hKL ((relindex_inf_mul_relindex H K L).trans (relindex_eq_zero_of_le_left inf_le_left h)) @[to_additive] lemma relindex_inf_ne_zero (hH : H.relindex L ≠ 0) (hK : K.relindex L ≠ 0) : (H ⊓ K).relindex L ≠ 0 := begin replace hH : H.relindex (K ⊓ L) ≠ 0 := mt (relindex_eq_zero_of_le_right inf_le_right) hH, rw ← inf_relindex_right at hH hK ⊢, rw inf_assoc, exact relindex_ne_zero_trans hH hK, end @[to_additive] lemma index_inf_ne_zero (hH : H.index ≠ 0) (hK : K.index ≠ 0) : (H ⊓ K).index ≠ 0 := begin rw ← relindex_top_right at hH hK ⊢, exact relindex_inf_ne_zero hH hK, end @[to_additive] lemma relindex_inf_le : (H ⊓ K).relindex L ≤ H.relindex L * K.relindex L := begin by_cases h : H.relindex L = 0, { exact (le_of_eq (relindex_eq_zero_of_le_left (by exact inf_le_left) h)).trans (zero_le _) }, rw [←inf_relindex_right, inf_assoc, ←relindex_mul_relindex _ _ L inf_le_right inf_le_right, inf_relindex_right, inf_relindex_right], exact mul_le_mul_right' (relindex_le_of_le_right inf_le_right h) (K.relindex L), end @[to_additive] lemma index_inf_le : (H ⊓ K).index ≤ H.index * K.index := by simp_rw [←relindex_top_right, relindex_inf_le] @[to_additive] lemma relindex_infi_ne_zero {ι : Type*} [hι : finite ι] {f : ι → subgroup G} (hf : ∀ i, (f i).relindex L ≠ 0) : (⨅ i, f i).relindex L ≠ 0 := begin haveI := fintype.of_finite ι, exact finset.prod_ne_zero_iff.mpr (λ i hi, hf i) ∘ nat.card_pi.symm.trans ∘ finite.card_eq_zero_of_embedding (quotient_infi_subgroup_of_embedding f L), end @[to_additive] lemma relindex_infi_le {ι : Type*} [fintype ι] (f : ι → subgroup G) : (⨅ i, f i).relindex L ≤ ∏ i, (f i).relindex L := le_of_le_of_eq (finite.card_le_of_embedding' (quotient_infi_subgroup_of_embedding f L) (λ h, let ⟨i, hi, h⟩ := finset.prod_eq_zero_iff.mp (nat.card_pi.symm.trans h) in relindex_eq_zero_of_le_left (infi_le f i) h)) nat.card_pi @[to_additive] lemma index_infi_ne_zero {ι : Type*} [finite ι] {f : ι → subgroup G} (hf : ∀ i, (f i).index ≠ 0) : (⨅ i, f i).index ≠ 0 := begin simp_rw ← relindex_top_right at hf ⊢, exact relindex_infi_ne_zero hf, end @[to_additive] lemma index_infi_le {ι : Type*} [fintype ι] (f : ι → subgroup G) : (⨅ i, f i).index ≤ ∏ i, (f i).index := by simp_rw [←relindex_top_right, relindex_infi_le] @[simp, to_additive index_eq_one] lemma index_eq_one : H.index = 1 ↔ H = ⊤ := ⟨λ h, quotient_group.subgroup_eq_top_of_subsingleton H (cardinal.to_nat_eq_one_iff_unique.mp h).1, λ h, (congr_arg index h).trans index_top⟩ @[simp, to_additive relindex_eq_one] lemma relindex_eq_one : H.relindex K = 1 ↔ K ≤ H := index_eq_one.trans subgroup_of_eq_top @[simp, to_additive card_eq_one] lemma card_eq_one : nat.card H = 1 ↔ H = ⊥ := H.relindex_bot_left ▸ (relindex_eq_one.trans le_bot_iff) @[to_additive] lemma index_ne_zero_of_finite [hH : finite (G ⧸ H)] : H.index ≠ 0 := by { casesI nonempty_fintype (G ⧸ H), rw index_eq_card, exact fintype.card_ne_zero } /-- Finite index implies finite quotient. -/ @[to_additive "Finite index implies finite quotient."] noncomputable def fintype_of_index_ne_zero (hH : H.index ≠ 0) : fintype (G ⧸ H) := (cardinal.lt_aleph_0_iff_fintype.mp (lt_of_not_ge (mt cardinal.to_nat_apply_of_aleph_0_le hH))).some @[to_additive one_lt_index_of_ne_top] lemma one_lt_index_of_ne_top [finite (G ⧸ H)] (hH : H ≠ ⊤) : 1 < H.index := nat.one_lt_iff_ne_zero_and_ne_one.mpr ⟨index_ne_zero_of_finite, mt index_eq_one.mp hH⟩ section finite_index variables (H K) /-- Typeclass for finite index subgroups. -/ class finite_index : Prop := (finite_index : H.index ≠ 0) /-- Typeclass for finite index subgroups. -/ class _root_.add_subgroup.finite_index {G : Type*} [add_group G] (H : add_subgroup G) : Prop := (finite_index : H.index ≠ 0) /-- A finite index subgroup has finite quotient. -/ @[to_additive "A finite index subgroup has finite quotient"] noncomputable def fintype_quotient_of_finite_index [finite_index H] : fintype (G ⧸ H) := fintype_of_index_ne_zero finite_index.finite_index @[to_additive] instance finite_quotient_of_finite_index [finite_index H] : finite (G ⧸ H) := H.fintype_quotient_of_finite_index.finite @[to_additive] lemma finite_index_of_finite_quotient [finite (G ⧸ H)] : finite_index H := ⟨index_ne_zero_of_finite⟩ @[priority 100, to_additive] instance finite_index_of_finite [finite G] : finite_index H := finite_index_of_finite_quotient H @[to_additive] instance : finite_index (⊤ : subgroup G) := ⟨ne_of_eq_of_ne index_top one_ne_zero⟩ @[to_additive] instance [finite_index H] [finite_index K] : finite_index (H ⊓ K) := ⟨index_inf_ne_zero finite_index.finite_index finite_index.finite_index⟩ variables {H K} @[to_additive] lemma finite_index_of_le [finite_index H] (h : H ≤ K) : finite_index K := ⟨ne_zero_of_dvd_ne_zero finite_index.finite_index (index_dvd_of_le h)⟩ variables (H K) @[to_additive] instance finite_index_ker {G' : Type*} [group G'] (f : G →* G') [finite f.range] : f.ker.finite_index := @finite_index_of_finite_quotient G _ f.ker (finite.of_equiv f.range (quotient_group.quotient_ker_equiv_range f).symm) instance finite_index_normal_core [H.finite_index] : H.normal_core.finite_index := begin rw normal_core_eq_ker, apply_instance, end variables (G) instance finite_index_center [finite (commutator_set G)] [group.fg G] : finite_index (center G) := begin obtain ⟨S, -, hS⟩ := group.rank_spec G, exact ⟨mt (finite.card_eq_zero_of_embedding (quotient_center_embedding hS)) finite.card_pos.ne'⟩, end lemma index_center_le_pow [finite (commutator_set G)] [group.fg G] : (center G).index ≤ (nat.card (commutator_set G)) ^ group.rank G := begin obtain ⟨S, hS1, hS2⟩ := group.rank_spec G, rw [←hS1, ←fintype.card_coe, ←nat.card_eq_fintype_card, ←finset.coe_sort_coe, ←nat.card_fun], exact finite.card_le_of_embedding (quotient_center_embedding hS2), end end finite_index end subgroup
3c5f84e955bb135563faf274331adba43aa42443
07c6143268cfb72beccd1cc35735d424ebcb187b
/src/category_theory/differential_object.lean
a0bd5f53fa83f2cdec4bf25d1157a0a57beb268d
[ "Apache-2.0" ]
permissive
khoek/mathlib
bc49a842910af13a3c372748310e86467d1dc766
aa55f8b50354b3e11ba64792dcb06cccb2d8ee28
refs/heads/master
1,588,232,063,837
1,587,304,803,000
1,587,304,803,000
176,688,517
0
0
Apache-2.0
1,553,070,585,000
1,553,070,585,000
null
UTF-8
Lean
false
false
3,773
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.limits.shapes.zero import category_theory.shift import category_theory.concrete_category /-! # Differential objects in a category. A differential object in a category with zero morphisms and a shift is an object `X` equipped with a morphism `d : X ⟶ X⟦1⟧`, such that `d^2 = 0`. We build the category of differential objects, and some basic constructions such as the forgetful functor, and zero morphisms and zero objects. -/ open category_theory.limits universes v u namespace category_theory variables (C : Type u) [𝒞 : category.{v} C] include 𝒞 variables [has_zero_morphisms.{v} C] [has_shift.{v} C] /-- A differential object in a category with zero morphisms and a shift is an object `X` equipped with a morphism `d : X ⟶ X⟦1⟧`, such that `d^2 = 0`. -/ @[nolint has_inhabited_instance] structure differential_object := (X : C) (d : X ⟶ X⟦1⟧) (d_squared' : d ≫ d⟦1⟧' = 0 . obviously) restate_axiom differential_object.d_squared' attribute [simp] differential_object.d_squared variables {C} namespace differential_object /-- A morphism of differential objects is a morphism commuting with the differentials. -/ @[ext, nolint has_inhabited_instance] structure hom (X Y : differential_object.{v} C) := (f : X.X ⟶ Y.X) (comm' : X.d ≫ f⟦1⟧' = f ≫ Y.d . obviously) restate_axiom hom.comm' attribute [simp, reassoc] hom.comm namespace hom /-- The identity morphism of a differential object. -/ @[simps] def id (X : differential_object.{v} C) : hom X X := { f := 𝟙 X.X } /-- The composition of morphisms of differential objects. -/ @[simps] def comp {X Y Z : differential_object.{v} C} (f : hom X Y) (g : hom Y Z) : hom X Z := { f := f.f ≫ g.f, } end hom instance category_of_differential_objects : category.{v} (differential_object.{v} C) := { hom := hom, id := hom.id, comp := λ X Y Z f g, hom.comp f g, } @[simp] lemma id_f (X : differential_object.{v} C) : ((𝟙 X) : X ⟶ X).f = 𝟙 (X.X) := rfl @[simp] lemma comp_f {X Y Z : differential_object.{v} C} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g).f = f.f ≫ g.f := rfl variables (C) /-- The forgetful functor taking a differential object to its underlying object. -/ def forget : (differential_object.{v} C) ⥤ C := { obj := λ X, X.X, map := λ X Y f, f.f, } instance forget_faithful : faithful (forget C) := { } instance has_zero_morphisms : has_zero_morphisms.{v} (differential_object.{v} C) := { has_zero := λ X Y, ⟨{ f := 0, }⟩} variables {C} @[simp] lemma zero_f (P Q : differential_object.{v} C) : (0 : P ⟶ Q).f = 0 := rfl end differential_object end category_theory namespace category_theory namespace differential_object variables (C : Type u) [𝒞 : category.{v} C] include 𝒞 variables [has_zero_object.{v} C] [has_zero_morphisms.{v} C] [has_shift.{v} C] local attribute [instance] has_zero_object.has_zero instance has_zero_object : has_zero_object.{v} (differential_object.{v} C) := { zero := { X := (0 : C), d := 0, }, unique_to := λ X, ⟨⟨{ f := 0 }⟩, λ f, (by ext)⟩, unique_from := λ X, ⟨⟨{ f := 0 }⟩, λ f, (by ext)⟩, } end differential_object namespace differential_object variables (C : Type (u+1)) [large_category C] [𝒞 : concrete_category C] [has_zero_morphisms.{u} C] [has_shift.{u} C] include 𝒞 instance concrete_category_of_differential_objects : concrete_category (differential_object.{u} C) := { forget := forget C ⋙ category_theory.forget C } instance : has_forget₂ (differential_object.{u} C) C := { forget₂ := forget C } end differential_object end category_theory
de2bf278698c1185569030e251838483a2686053
3ed5a65c1ab3ce5d1a094edce8fa3287980f197b
/src/herstein/ex2_5/Q_13.lean
6baf96c84b69a1082d06656dc5664e4cc0e5841e
[]
no_license
group-study-group/herstein
35d32e77158efa2cc303c84e1ee5e3bc80831137
f5a1a72eb56fa19c19ece0cb3ab6cf7ffd161f66
refs/heads/master
1,586,202,191,519
1,548,969,759,000
1,548,969,759,000
157,746,953
0
0
null
1,542,412,901,000
1,542,302,366,000
Lean
UTF-8
Lean
false
false
962
lean
import algebra.group group_theory.subgroup variables {G: Type*} def normaliser (a: G) [group G]: set G := { x: G | a * x = x * a } theorem Q_13 (a: G) [group G]: is_subgroup (normaliser a) := { one_mem := calc a * 1 = 1 * a: by rw [mul_one, one_mul], mul_mem := λ g h hg hh, calc a * (g * h) = (a * g) * h : (mul_assoc _ _ _).symm ... = (g * a) * h : hg ▸ rfl ... = g * (a * h) : mul_assoc _ _ _ ... = g * (h * a) : hh ▸ rfl ... = (g * h) * a : (mul_assoc _ _ _).symm, inv_mem := λ g hg, calc a * g⁻¹ = (g⁻¹ * g) * (a * g⁻¹) : (mul_left_inv g).symm ▸ (one_mul _).symm ... = g⁻¹ * (g * a) * g⁻¹ : by rw [ ←mul_assoc, mul_assoc g⁻¹ ] ... = g⁻¹ * (a * g) * g⁻¹ : hg ▸ rfl ... = (g⁻¹ * a) * g * g⁻¹ : mul_assoc g⁻¹ a g ▸ rfl ... = g⁻¹ * a : mul_inv_cancel_right (g⁻¹ * a) g }
0e6c3a09bc23ed86efdd123955cfbba46b937475
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/bitvec/core.lean
3053d766e754ebd176ec9531c8678e6fb24edb71
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,794
lean
/- Copyright (c) 2015 Joe Hendrix. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joe Hendrix, Sebastian Ullrich -/ import data.vector.basic import data.nat.basic /-! # Basic operations on bitvectors This is a work-in-progress, and contains additions to other theories. This file was moved to mathlib from core Lean in the switch to Lean 3.20.0c. It is not fully in compliance with mathlib style standards. -/ /-- `bitvec n` is a `vector` of `bool` with length `n`. -/ @[reducible] def bitvec (n : ℕ) := vector bool n namespace bitvec open nat open vector local infix `++ₜ`:65 := vector.append /-- Create a zero bitvector -/ @[reducible] protected def zero (n : ℕ) : bitvec n := repeat ff n /-- Create a bitvector of length `n` whose `n-1`st entry is 1 and other entries are 0. -/ @[reducible] protected def one : Π (n : ℕ), bitvec n | 0 := nil | (succ n) := repeat ff n ++ₜ tt::ᵥnil /-- Create a bitvector from another with a provably equal length. -/ protected def cong {a b : ℕ} (h : a = b) : bitvec a → bitvec b | ⟨x, p⟩ := ⟨x, h ▸ p⟩ /-- `bitvec` specific version of `vector.append` -/ def append {m n} : bitvec m → bitvec n → bitvec (m + n) := vector.append /-! ### Shift operations -/ section shift variable {n : ℕ} /-- `shl x i` is the bitvector obtained by left-shifting `x` `i` times and padding with `ff`. If `x.length < i` then this will return the all-`ff`s bitvector. -/ def shl (x : bitvec n) (i : ℕ) : bitvec n := bitvec.cong (by simp) $ drop i x ++ₜ repeat ff (min n i) /-- `fill_shr x i fill` is the bitvector obtained by right-shifting `x` `i` times and then padding with `fill : bool`. If `x.length < i` then this will return the constant `fill` bitvector. -/ def fill_shr (x : bitvec n) (i : ℕ) (fill : bool) : bitvec n := bitvec.cong begin by_cases (i ≤ n), { have h₁ := nat.sub_le n i, rw [min_eq_right h], rw [min_eq_left h₁, ← add_tsub_assoc_of_le h, nat.add_comm, add_tsub_cancel_right] }, { have h₁ := le_of_not_ge h, rw [min_eq_left h₁, tsub_eq_zero_iff_le.mpr h₁, zero_min, nat.add_zero] } end $ repeat fill (min n i) ++ₜ take (n-i) x /-- unsigned shift right -/ def ushr (x : bitvec n) (i : ℕ) : bitvec n := fill_shr x i ff /-- signed shift right -/ def sshr : Π {m : ℕ}, bitvec m → ℕ → bitvec m | 0 _ _ := nil | (succ m) x i := head x ::ᵥ fill_shr (tail x) i (head x) end shift /-! ### Bitwise operations -/ section bitwise variable {n : ℕ} /-- bitwise not -/ def not : bitvec n → bitvec n := map bnot /-- bitwise and -/ def and : bitvec n → bitvec n → bitvec n := map₂ band /-- bitwise or -/ def or : bitvec n → bitvec n → bitvec n := map₂ bor /-- bitwise xor -/ def xor : bitvec n → bitvec n → bitvec n := map₂ bxor end bitwise /-! ### Arithmetic operators -/ section arith variable {n : ℕ} /-- `xor3 x y c` is `((x XOR y) XOR c)`. -/ protected def xor3 (x y c : bool) := bxor (bxor x y) c /-- `carry x y c` is `x && y || x && c || y && c`. -/ protected def carry (x y c : bool) := x && y || x && c || y && c /-- `neg x` is the two's complement of `x`. -/ protected def neg (x : bitvec n) : bitvec n := let f := λ y c, (y || c, bxor y c) in prod.snd (map_accumr f x ff) /-- Add with carry (no overflow) -/ def adc (x y : bitvec n) (c : bool) : bitvec (n+1) := let f := λ x y c, (bitvec.carry x y c, bitvec.xor3 x y c) in let ⟨c, z⟩ := vector.map_accumr₂ f x y c in c ::ᵥ z /-- The sum of two bitvectors -/ protected def add (x y : bitvec n) : bitvec n := tail (adc x y ff) /-- Subtract with borrow -/ def sbb (x y : bitvec n) (b : bool) : bool × bitvec n := let f := λ x y c, (bitvec.carry (bnot x) y c, bitvec.xor3 x y c) in vector.map_accumr₂ f x y b /-- The difference of two bitvectors -/ protected def sub (x y : bitvec n) : bitvec n := prod.snd (sbb x y ff) instance : has_zero (bitvec n) := ⟨bitvec.zero n⟩ instance : has_one (bitvec n) := ⟨bitvec.one n⟩ instance : has_add (bitvec n) := ⟨bitvec.add⟩ instance : has_sub (bitvec n) := ⟨bitvec.sub⟩ instance : has_neg (bitvec n) := ⟨bitvec.neg⟩ /-- The product of two bitvectors -/ protected def mul (x y : bitvec n) : bitvec n := let f := λ r b, cond b (r + r + y) (r + r) in (to_list x).foldl f 0 instance : has_mul (bitvec n) := ⟨bitvec.mul⟩ end arith /-! ### Comparison operators -/ section comparison variable {n : ℕ} /-- `uborrow x y` returns `tt` iff the "subtract with borrow" operation on `x`, `y` and `ff` required a borrow. -/ def uborrow (x y : bitvec n) : bool := prod.fst (sbb x y ff) /-- unsigned less-than proposition -/ def ult (x y : bitvec n) : Prop := uborrow x y /-- unsigned greater-than proposition -/ def ugt (x y : bitvec n) : Prop := ult y x /-- unsigned less-than-or-equal-to proposition -/ def ule (x y : bitvec n) : Prop := ¬ (ult y x) /-- unsigned greater-than-or-equal-to proposition -/ def uge (x y : bitvec n) : Prop := ule y x /-- `sborrow x y` returns `tt` iff `x < y` as two's complement integers -/ def sborrow : Π {n : ℕ}, bitvec n → bitvec n → bool | 0 _ _ := ff | (succ n) x y := match (head x, head y) with | (tt, ff) := tt | (ff, tt) := ff | _ := uborrow (tail x) (tail y) end /-- signed less-than proposition -/ def slt (x y : bitvec n) : Prop := sborrow x y /-- signed greater-than proposition -/ def sgt (x y : bitvec n) : Prop := slt y x /-- signed less-than-or-equal-to proposition -/ def sle (x y : bitvec n) : Prop := ¬ (slt y x) /-- signed greater-than-or-equal-to proposition -/ def sge (x y : bitvec n) : Prop := sle y x end comparison /-! ### Conversion to `nat` and `int` -/ section conversion variable {α : Type} /-- Create a bitvector from a `nat` -/ protected def of_nat : Π (n : ℕ), nat → bitvec n | 0 x := nil | (succ n) x := of_nat n (x / 2) ++ₜ to_bool (x % 2 = 1) ::ᵥ nil /-- Create a bitvector in the two's complement representation from an `int` -/ protected def of_int : Π (n : ℕ), int → bitvec (succ n) | n (int.of_nat m) := ff ::ᵥ bitvec.of_nat n m | n (int.neg_succ_of_nat m) := tt ::ᵥ not (bitvec.of_nat n m) /-- `add_lsb r b` is `r + r + 1` if `b` is `tt` and `r + r` otherwise. -/ def add_lsb (r : ℕ) (b : bool) := r + r + cond b 1 0 /-- Given a `list` of `bool`s, return the `nat` they represent as a list of binary digits. -/ def bits_to_nat (v : list bool) : nat := v.foldl add_lsb 0 /-- Return the natural number encoded by the input bitvector -/ protected def to_nat {n : nat} (v : bitvec n) : nat := bits_to_nat (to_list v) theorem bits_to_nat_to_list {n : ℕ} (x : bitvec n) : bitvec.to_nat x = bits_to_nat (vector.to_list x) := rfl local attribute [simp] nat.add_comm nat.add_assoc nat.add_left_comm nat.mul_comm nat.mul_assoc local attribute [simp] nat.zero_add nat.add_zero nat.one_mul nat.mul_one nat.zero_mul nat.mul_zero -- mul_left_comm theorem to_nat_append {m : ℕ} (xs : bitvec m) (b : bool) : bitvec.to_nat (xs ++ₜ b::ᵥnil) = bitvec.to_nat xs * 2 + bitvec.to_nat (b::ᵥnil) := begin cases xs with xs P, simp [bits_to_nat_to_list], clear P, unfold bits_to_nat list.foldl, -- generalize the accumulator of foldl generalize h : 0 = x, conv in (add_lsb x b) { rw ←h }, clear h, simp, induction xs with x xs generalizing x, { simp, unfold list.foldl add_lsb, simp [nat.mul_succ] }, { simp, apply xs_ih } end theorem bits_to_nat_to_bool (n : ℕ) : bitvec.to_nat (to_bool (n % 2 = 1) ::ᵥ nil) = n % 2 := begin simp [bits_to_nat_to_list], unfold bits_to_nat add_lsb list.foldl cond, simp [cond_to_bool_mod_two], end theorem of_nat_succ {k n : ℕ} : bitvec.of_nat (succ k) n = bitvec.of_nat k (n / 2) ++ₜ to_bool (n % 2 = 1) ::ᵥ nil := rfl theorem to_nat_of_nat {k n : ℕ} : bitvec.to_nat (bitvec.of_nat k n) = n % 2 ^ k := begin induction k with k ih generalizing n, { simp [nat.mod_one], refl }, { have h : 0 < 2, { apply le_succ }, rw [of_nat_succ, to_nat_append, ih, bits_to_nat_to_bool, mod_pow_succ h, nat.mul_comm] } end /-- Return the integer encoded by the input bitvector -/ protected def to_int : Π {n : nat}, bitvec n → int | 0 _ := 0 | (succ n) v := cond (head v) (int.neg_succ_of_nat $ bitvec.to_nat $ not $ tail v) (int.of_nat $ bitvec.to_nat $ tail v) end conversion /-! ### Miscellaneous instances -/ private def repr {n : nat} : bitvec n → string | ⟨bs, p⟩ := "0b" ++ (bs.map (λ b : bool, if b then '1' else '0')).as_string instance (n : nat) : has_repr (bitvec n) := ⟨repr⟩ end bitvec instance {n} {x y : bitvec n} : decidable (bitvec.ult x y) := bool.decidable_eq _ _ instance {n} {x y : bitvec n} : decidable (bitvec.ugt x y) := bool.decidable_eq _ _
3ba0b3ea3b87171688fe58ee0586d299d6c13c19
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/model_theory/syntax.lean
d93d18ac26b9a2f68cbab8623e21ed28f533052e
[ "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
38,809
lean
/- Copyright (c) 2021 Aaron Anderson, Jesse Michael Han, Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Jesse Michael Han, Floris van Doorn -/ import data.set.prod import logic.equiv.fin import model_theory.language_map /-! # Basics on First-Order Syntax This file defines first-order terms, formulas, sentences, and theories in a style inspired by the [Flypitch project](https://flypitch.github.io/). ## Main Definitions * A `first_order.language.term` is defined so that `L.term α` is the type of `L`-terms with free variables indexed by `α`. * A `first_order.language.formula` is defined so that `L.formula α` is the type of `L`-formulas with free variables indexed by `α`. * A `first_order.language.sentence` is a formula with no free variables. * A `first_order.language.Theory` is a set of sentences. * The variables of terms and formulas can be relabelled with `first_order.language.term.relabel`, `first_order.language.bounded_formula.relabel`, and `first_order.language.formula.relabel`. * Given an operation on terms and an operation on relations, `first_order.language.bounded_formula.map_term_rel` gives an operation on formulas. * `first_order.language.bounded_formula.cast_le` adds more `fin`-indexed variables. * `first_order.language.bounded_formula.lift_at` raises the indexes of the `fin`-indexed variables above a particular index. * `first_order.language.term.subst` and `first_order.language.bounded_formula.subst` substitute variables with given terms. * Language maps can act on syntactic objects with functions such as `first_order.language.Lhom.on_formula`. * `first_order.language.term.constants_vars_equiv` and `first_order.language.bounded_formula.constants_vars_equiv` switch terms and formulas between having constants in the language and having extra variables indexed by the same type. ## Implementation Notes * Formulas use a modified version of de Bruijn variables. Specifically, a `L.bounded_formula α n` is a formula with some variables indexed by a type `α`, which cannot be quantified over, and some indexed by `fin n`, which can. For any `φ : L.bounded_formula α (n + 1)`, we define the formula `∀' φ : L.bounded_formula α n` by universally quantifying over the variable indexed by `n : fin (n + 1)`. ## References For the Flypitch project: - [J. Han, F. van Doorn, *A formal proof of the independence of the continuum hypothesis*] [flypitch_cpp] - [J. Han, F. van Doorn, *A formalization of forcing and the unprovability of the continuum hypothesis*][flypitch_itp] -/ universes u v w u' v' namespace first_order namespace language variables (L : language.{u v}) {L' : language} variables {M : Type w} {N P : Type*} [L.Structure M] [L.Structure N] [L.Structure P] variables {α : Type u'} {β : Type v'} {γ : Type*} open_locale first_order open Structure fin /-- A term on `α` is either a variable indexed by an element of `α` or a function symbol applied to simpler terms. -/ inductive term (α : Type u') : Type (max u u') | var {} : ∀ (a : α), term | func {} : ∀ {l : ℕ} (f : L.functions l) (ts : fin l → term), term export term variable {L} namespace term open finset /-- The `finset` of variables used in a given term. -/ @[simp] def var_finset [decidable_eq α] : L.term α → finset α | (var i) := {i} | (func f ts) := univ.bUnion (λ i, (ts i).var_finset) /-- The `finset` of variables from the left side of a sum used in a given term. -/ @[simp] def var_finset_left [decidable_eq α] : L.term (α ⊕ β) → finset α | (var (sum.inl i)) := {i} | (var (sum.inr i)) := ∅ | (func f ts) := univ.bUnion (λ i, (ts i).var_finset_left) /-- Relabels a term's variables along a particular function. -/ @[simp] def relabel (g : α → β) : L.term α → L.term β | (var i) := var (g i) | (func f ts) := func f (λ i, (ts i).relabel) lemma relabel_id (t : L.term α) : t.relabel id = t := begin induction t with _ _ _ _ ih, { refl, }, { simp [ih] }, end @[simp] lemma relabel_id_eq_id : (term.relabel id : L.term α → L.term α) = id := funext relabel_id @[simp] lemma relabel_relabel (f : α → β) (g : β → γ) (t : L.term α) : (t.relabel f).relabel g = t.relabel (g ∘ f) := begin induction t with _ _ _ _ ih, { refl, }, { simp [ih] }, end @[simp] lemma relabel_comp_relabel (f : α → β) (g : β → γ) : (term.relabel g ∘ term.relabel f : L.term α → L.term γ) = term.relabel (g ∘ f) := funext (relabel_relabel f g) /-- Relabels a term's variables along a bijection. -/ @[simps] def relabel_equiv (g : α ≃ β) : L.term α ≃ L.term β := ⟨relabel g, relabel g.symm, λ t, by simp, λ t, by simp⟩ /-- Restricts a term to use only a set of the given variables. -/ def restrict_var [decidable_eq α] : Π (t : L.term α) (f : t.var_finset → β), L.term β | (var a) f := var (f ⟨a, mem_singleton_self a⟩) | (func F ts) f := func F (λ i, (ts i).restrict_var (f ∘ set.inclusion (subset_bUnion_of_mem _ (mem_univ i)))) /-- Restricts a term to use only a set of the given variables on the left side of a sum. -/ def restrict_var_left [decidable_eq α] {γ : Type*} : Π (t : L.term (α ⊕ γ)) (f : t.var_finset_left → β), L.term (β ⊕ γ) | (var (sum.inl a)) f := var (sum.inl (f ⟨a, mem_singleton_self a⟩)) | (var (sum.inr a)) f := var (sum.inr a) | (func F ts) f := func F (λ i, (ts i).restrict_var_left (f ∘ set.inclusion (subset_bUnion_of_mem _ (mem_univ i)))) end term /-- The representation of a constant symbol as a term. -/ def constants.term (c : L.constants) : (L.term α) := func c default /-- Applies a unary function to a term. -/ def functions.apply₁ (f : L.functions 1) (t : L.term α) : L.term α := func f ![t] /-- Applies a binary function to two terms. -/ def functions.apply₂ (f : L.functions 2) (t₁ t₂ : L.term α) : L.term α := func f ![t₁, t₂] namespace term /-- Sends a term with constants to a term with extra variables. -/ @[simp] def constants_to_vars : L[[γ]].term α → L.term (γ ⊕ α) | (var a) := var (sum.inr a) | (@func _ _ 0 f ts) := sum.cases_on f (λ f, func f (λ i, (ts i).constants_to_vars)) (λ c, var (sum.inl c)) | (@func _ _ (n + 1) f ts) := sum.cases_on f (λ f, func f (λ i, (ts i).constants_to_vars)) (λ c, is_empty_elim c) /-- Sends a term with extra variables to a term with constants. -/ @[simp] def vars_to_constants : L.term (γ ⊕ α) → L[[γ]].term α | (var (sum.inr a)) := var a | (var (sum.inl c)) := constants.term (sum.inr c) | (func f ts) := func (sum.inl f) (λ i, (ts i).vars_to_constants) /-- A bijection between terms with constants and terms with extra variables. -/ @[simps] def constants_vars_equiv : L[[γ]].term α ≃ L.term (γ ⊕ α) := ⟨constants_to_vars, vars_to_constants, begin intro t, induction t with _ n f _ ih, { refl }, { cases n, { cases f, { simp [constants_to_vars, vars_to_constants, ih] }, { simp [constants_to_vars, vars_to_constants, constants.term] } }, { cases f, { simp [constants_to_vars, vars_to_constants, ih] }, { exact is_empty_elim f } } } end, begin intro t, induction t with x n f _ ih, { cases x; refl }, { cases n; { simp [vars_to_constants, constants_to_vars, ih] } } end⟩ /-- A bijection between terms with constants and terms with extra variables. -/ def constants_vars_equiv_left : L[[γ]].term (α ⊕ β) ≃ L.term ((γ ⊕ α) ⊕ β) := constants_vars_equiv.trans (relabel_equiv (equiv.sum_assoc _ _ _)).symm @[simp] lemma constants_vars_equiv_left_apply (t : L[[γ]].term (α ⊕ β)) : constants_vars_equiv_left t = (constants_to_vars t).relabel (equiv.sum_assoc _ _ _).symm := rfl @[simp] lemma constants_vars_equiv_left_symm_apply (t : L.term ((γ ⊕ α) ⊕ β)) : constants_vars_equiv_left.symm t = vars_to_constants (t.relabel (equiv.sum_assoc _ _ _)) := rfl instance inhabited_of_var [inhabited α] : inhabited (L.term α) := ⟨var default⟩ instance inhabited_of_constant [inhabited L.constants] : inhabited (L.term α) := ⟨(default : L.constants).term⟩ /-- Raises all of the `fin`-indexed variables of a term greater than or equal to `m` by `n'`. -/ def lift_at {n : ℕ} (n' m : ℕ) : L.term (α ⊕ fin n) → L.term (α ⊕ fin (n + n')) := relabel (sum.map id (λ i, if ↑i < m then fin.cast_add n' i else fin.add_nat n' i)) /-- Substitutes the variables in a given term with terms. -/ @[simp] def subst : L.term α → (α → L.term β) → L.term β | (var a) tf := tf a | (func f ts) tf := (func f (λ i, (ts i).subst tf)) end term localized "prefix (name := language.term.var) `&`:max := first_order.language.term.var ∘ sum.inr" in first_order namespace Lhom /-- Maps a term's symbols along a language map. -/ @[simp] def on_term (φ : L →ᴸ L') : L.term α → L'.term α | (var i) := var i | (func f ts) := func (φ.on_function f) (λ i, on_term (ts i)) @[simp] lemma id_on_term : ((Lhom.id L).on_term : L.term α → L.term α) = id := begin ext t, induction t with _ _ _ _ ih, { refl }, { simp_rw [on_term, ih], refl, }, end @[simp] lemma comp_on_term {L'' : language} (φ : L' →ᴸ L'') (ψ : L →ᴸ L') : ((φ.comp ψ).on_term : L.term α → L''.term α) = φ.on_term ∘ ψ.on_term := begin ext t, induction t with _ _ _ _ ih, { refl }, { simp_rw [on_term, ih], refl, }, end end Lhom /-- Maps a term's symbols along a language equivalence. -/ @[simps] def Lequiv.on_term (φ : L ≃ᴸ L') : L.term α ≃ L'.term α := { to_fun := φ.to_Lhom.on_term, inv_fun := φ.inv_Lhom.on_term, left_inv := by rw [function.left_inverse_iff_comp, ← Lhom.comp_on_term, φ.left_inv, Lhom.id_on_term], right_inv := by rw [function.right_inverse_iff_comp, ← Lhom.comp_on_term, φ.right_inv, Lhom.id_on_term] } variables (L) (α) /-- `bounded_formula α n` is the type of formulas with free variables indexed by `α` and up to `n` additional free variables. -/ inductive bounded_formula : ℕ → Type (max u v u') | falsum {} {n} : bounded_formula n | equal {n} (t₁ t₂ : L.term (α ⊕ fin n)) : bounded_formula n | rel {n l : ℕ} (R : L.relations l) (ts : fin l → L.term (α ⊕ fin n)) : bounded_formula n | imp {n} (f₁ f₂ : bounded_formula n) : bounded_formula n | all {n} (f : bounded_formula (n+1)) : bounded_formula n /-- `formula α` is the type of formulas with all free variables indexed by `α`. -/ @[reducible] def formula := L.bounded_formula α 0 /-- A sentence is a formula with no free variables. -/ @[reducible] def sentence := L.formula empty /-- A theory is a set of sentences. -/ @[reducible] def Theory := set L.sentence variables {L} {α} {n : ℕ} /-- Applies a relation to terms as a bounded formula. -/ def relations.bounded_formula {l : ℕ} (R : L.relations n) (ts : fin n → L.term (α ⊕ fin l)) : L.bounded_formula α l := bounded_formula.rel R ts /-- Applies a unary relation to a term as a bounded formula. -/ def relations.bounded_formula₁ (r : L.relations 1) (t : L.term (α ⊕ fin n)) : L.bounded_formula α n := r.bounded_formula ![t] /-- Applies a binary relation to two terms as a bounded formula. -/ def relations.bounded_formula₂ (r : L.relations 2) (t₁ t₂ : L.term (α ⊕ fin n)) : L.bounded_formula α n := r.bounded_formula ![t₁, t₂] /-- The equality of two terms as a bounded formula. -/ def term.bd_equal (t₁ t₂ : L.term (α ⊕ fin n)) : (L.bounded_formula α n) := bounded_formula.equal t₁ t₂ /-- Applies a relation to terms as a bounded formula. -/ def relations.formula (R : L.relations n) (ts : fin n → L.term α) : L.formula α := R.bounded_formula (λ i, (ts i).relabel sum.inl) /-- Applies a unary relation to a term as a formula. -/ def relations.formula₁ (r : L.relations 1) (t : L.term α) : L.formula α := r.formula ![t] /-- Applies a binary relation to two terms as a formula. -/ def relations.formula₂ (r : L.relations 2) (t₁ t₂ : L.term α) : L.formula α := r.formula ![t₁, t₂] /-- The equality of two terms as a first-order formula. -/ def term.equal (t₁ t₂ : L.term α) : (L.formula α) := (t₁.relabel sum.inl).bd_equal (t₂.relabel sum.inl) namespace bounded_formula instance : inhabited (L.bounded_formula α n) := ⟨falsum⟩ instance : has_bot (L.bounded_formula α n) := ⟨falsum⟩ /-- The negation of a bounded formula is also a bounded formula. -/ @[pattern] protected def not (φ : L.bounded_formula α n) : L.bounded_formula α n := φ.imp ⊥ /-- Puts an `∃` quantifier on a bounded formula. -/ @[pattern] protected def ex (φ : L.bounded_formula α (n + 1)) : L.bounded_formula α n := φ.not.all.not instance : has_top (L.bounded_formula α n) := ⟨bounded_formula.not ⊥⟩ instance : has_inf (L.bounded_formula α n) := ⟨λ f g, (f.imp g.not).not⟩ instance : has_sup (L.bounded_formula α n) := ⟨λ f g, f.not.imp g⟩ /-- The biimplication between two bounded formulas. -/ protected def iff (φ ψ : L.bounded_formula α n) := φ.imp ψ ⊓ ψ.imp φ open finset /-- The `finset` of variables used in a given formula. -/ @[simp] def free_var_finset [decidable_eq α] : ∀ {n}, L.bounded_formula α n → finset α | n falsum := ∅ | n (equal t₁ t₂) := t₁.var_finset_left ∪ t₂.var_finset_left | n (rel R ts) := univ.bUnion (λ i, (ts i).var_finset_left) | n (imp f₁ f₂) := f₁.free_var_finset ∪ f₂.free_var_finset | n (all f) := f.free_var_finset /-- Casts `L.bounded_formula α m` as `L.bounded_formula α n`, where `m ≤ n`. -/ @[simp] def cast_le : ∀ {m n : ℕ} (h : m ≤ n), L.bounded_formula α m → L.bounded_formula α n | m n h falsum := falsum | m n h (equal t₁ t₂) := equal (t₁.relabel (sum.map id (fin.cast_le h))) (t₂.relabel (sum.map id (fin.cast_le h))) | m n h (rel R ts) := rel R (term.relabel (sum.map id (fin.cast_le h)) ∘ ts) | m n h (imp f₁ f₂) := (f₁.cast_le h).imp (f₂.cast_le h) | m n h (all f) := (f.cast_le (add_le_add_right h 1)).all @[simp] lemma cast_le_rfl {n} (h : n ≤ n) (φ : L.bounded_formula α n) : φ.cast_le h = φ := begin induction φ with _ _ _ _ _ _ _ _ _ _ _ ih1 ih2 _ _ ih3, { refl }, { simp [fin.cast_le_of_eq], }, { simp [fin.cast_le_of_eq], }, { simp [fin.cast_le_of_eq, ih1, ih2], }, { simp [fin.cast_le_of_eq, ih3], }, end @[simp] lemma cast_le_cast_le {k m n} (km : k ≤ m) (mn : m ≤ n) (φ : L.bounded_formula α k) : (φ.cast_le km).cast_le mn = φ.cast_le (km.trans mn) := begin revert m n, induction φ with _ _ _ _ _ _ _ _ _ _ _ ih1 ih2 _ _ ih3; intros m n km mn, { refl }, { simp }, { simp only [cast_le, eq_self_iff_true, heq_iff_eq, true_and], rw [← function.comp.assoc, relabel_comp_relabel], simp }, { simp [ih1, ih2] }, { simp only [cast_le, ih3] } end @[simp] lemma cast_le_comp_cast_le {k m n} (km : k ≤ m) (mn : m ≤ n) : (bounded_formula.cast_le mn ∘ bounded_formula.cast_le km : L.bounded_formula α k → L.bounded_formula α n) = bounded_formula.cast_le (km.trans mn) := funext (cast_le_cast_le km mn) /-- Restricts a bounded formula to only use a particular set of free variables. -/ def restrict_free_var [decidable_eq α] : Π {n : ℕ} (φ : L.bounded_formula α n) (f : φ.free_var_finset → β), L.bounded_formula β n | n falsum f := falsum | n (equal t₁ t₂) f := equal (t₁.restrict_var_left (f ∘ set.inclusion (subset_union_left _ _))) (t₂.restrict_var_left (f ∘ set.inclusion (subset_union_right _ _))) | n (rel R ts) f := rel R (λ i, (ts i).restrict_var_left (f ∘ set.inclusion (subset_bUnion_of_mem _ (mem_univ i)))) | n (imp φ₁ φ₂) f := (φ₁.restrict_free_var (f ∘ set.inclusion (subset_union_left _ _))).imp (φ₂.restrict_free_var (f ∘ set.inclusion (subset_union_right _ _))) | n (all φ) f := (φ.restrict_free_var f).all /-- Places universal quantifiers on all extra variables of a bounded formula. -/ def alls : ∀ {n}, L.bounded_formula α n → L.formula α | 0 φ := φ | (n + 1) φ := φ.all.alls /-- Places existential quantifiers on all extra variables of a bounded formula. -/ def exs : ∀ {n}, L.bounded_formula α n → L.formula α | 0 φ := φ | (n + 1) φ := φ.ex.exs /-- Maps bounded formulas along a map of terms and a map of relations. -/ def map_term_rel {g : ℕ → ℕ} (ft : ∀ n, L.term (α ⊕ fin n) → L'.term (β ⊕ fin (g n))) (fr : ∀ n, L.relations n → L'.relations n) (h : ∀ n, L'.bounded_formula β (g (n + 1)) → L'.bounded_formula β (g n + 1)) : ∀ {n}, L.bounded_formula α n → L'.bounded_formula β (g n) | n falsum := falsum | n (equal t₁ t₂) := equal (ft _ t₁) (ft _ t₂) | n (rel R ts) := rel (fr _ R) (λ i, ft _ (ts i)) | n (imp φ₁ φ₂) := φ₁.map_term_rel.imp φ₂.map_term_rel | n (all φ) := (h n φ.map_term_rel).all /-- Raises all of the `fin`-indexed variables of a formula greater than or equal to `m` by `n'`. -/ def lift_at : ∀ {n : ℕ} (n' m : ℕ), L.bounded_formula α n → L.bounded_formula α (n + n') := λ n n' m φ, φ.map_term_rel (λ k t, t.lift_at n' m) (λ _, id) (λ _, cast_le (by rw [add_assoc, add_comm 1, add_assoc])) @[simp] lemma map_term_rel_map_term_rel {L'' : language} (ft : ∀ n, L.term (α ⊕ fin n) → L'.term (β ⊕ fin n)) (fr : ∀ n, L.relations n → L'.relations n) (ft' : ∀ n, L'.term (β ⊕ fin n) → L''.term (γ ⊕ fin n)) (fr' : ∀ n, L'.relations n → L''.relations n) {n} (φ : L.bounded_formula α n) : (φ.map_term_rel ft fr (λ _, id)).map_term_rel ft' fr' (λ _, id) = φ.map_term_rel (λ _, (ft' _) ∘ (ft _)) (λ _, (fr' _) ∘ (fr _)) (λ _, id) := begin induction φ with _ _ _ _ _ _ _ _ _ _ _ ih1 ih2 _ _ ih3, { refl }, { simp [map_term_rel] }, { simp [map_term_rel] }, { simp [map_term_rel, ih1, ih2] }, { simp [map_term_rel, ih3], } end @[simp] lemma map_term_rel_id_id_id {n} (φ : L.bounded_formula α n) : φ.map_term_rel (λ _, id) (λ _, id) (λ _, id) = φ := begin induction φ with _ _ _ _ _ _ _ _ _ _ _ ih1 ih2 _ _ ih3, { refl }, { simp [map_term_rel] }, { simp [map_term_rel] }, { simp [map_term_rel, ih1, ih2] }, { simp [map_term_rel, ih3], } end /-- An equivalence of bounded formulas given by an equivalence of terms and an equivalence of relations. -/ @[simps] def map_term_rel_equiv (ft : ∀ n, L.term (α ⊕ fin n) ≃ L'.term (β ⊕ fin n)) (fr : ∀ n, L.relations n ≃ L'.relations n) {n} : L.bounded_formula α n ≃ L'.bounded_formula β n := ⟨map_term_rel (λ n, ft n) (λ n, fr n) (λ _, id), map_term_rel (λ n, (ft n).symm) (λ n, (fr n).symm) (λ _, id), λ φ, by simp, λ φ, by simp⟩ /-- A function to help relabel the variables in bounded formulas. -/ def relabel_aux (g : α → β ⊕ fin n) (k : ℕ) : α ⊕ fin k → β ⊕ fin (n + k) := sum.map id fin_sum_fin_equiv ∘ equiv.sum_assoc _ _ _ ∘ sum.map g id @[simp] lemma sum_elim_comp_relabel_aux {m : ℕ} {g : α → (β ⊕ fin n)} {v : β → M} {xs : fin (n + m) → M} : sum.elim v xs ∘ relabel_aux g m = sum.elim (sum.elim v (xs ∘ cast_add m) ∘ g) (xs ∘ nat_add n) := begin ext x, cases x, { simp only [bounded_formula.relabel_aux, function.comp_app, sum.map_inl, sum.elim_inl], cases g x with l r; simp }, { simp [bounded_formula.relabel_aux] } end @[simp] lemma relabel_aux_sum_inl (k : ℕ) : relabel_aux (sum.inl : α → α ⊕ fin n) k = sum.map id (nat_add n) := begin ext x, cases x; { simp [relabel_aux] }, end /-- Relabels a bounded formula's variables along a particular function. -/ def relabel (g : α → (β ⊕ fin n)) {k} (φ : L.bounded_formula α k) : L.bounded_formula β (n + k) := φ.map_term_rel (λ _ t, t.relabel (relabel_aux g _)) (λ _, id) (λ _, cast_le (ge_of_eq (add_assoc _ _ _))) @[simp] lemma relabel_falsum (g : α → (β ⊕ fin n)) {k} : (falsum : L.bounded_formula α k).relabel g = falsum := rfl @[simp] lemma relabel_bot (g : α → (β ⊕ fin n)) {k} : (⊥ : L.bounded_formula α k).relabel g = ⊥ := rfl @[simp] lemma relabel_imp (g : α → (β ⊕ fin n)) {k} (φ ψ : L.bounded_formula α k) : (φ.imp ψ).relabel g = (φ.relabel g).imp (ψ.relabel g) := rfl @[simp] lemma relabel_not (g : α → (β ⊕ fin n)) {k} (φ : L.bounded_formula α k) : φ.not.relabel g = (φ.relabel g).not := by simp [bounded_formula.not] @[simp] lemma relabel_all (g : α → (β ⊕ fin n)) {k} (φ : L.bounded_formula α (k + 1)) : φ.all.relabel g = (φ.relabel g).all := begin rw [relabel, map_term_rel, relabel], simp, end @[simp] lemma relabel_ex (g : α → (β ⊕ fin n)) {k} (φ : L.bounded_formula α (k + 1)) : φ.ex.relabel g = (φ.relabel g).ex := by simp [bounded_formula.ex] @[simp] lemma relabel_sum_inl (φ : L.bounded_formula α n) : (φ.relabel sum.inl : L.bounded_formula α (0 + n)) = φ.cast_le (ge_of_eq (zero_add n)) := begin simp only [relabel, relabel_aux_sum_inl], induction φ with _ _ _ _ _ _ _ _ _ _ _ ih1 ih2 _ _ ih3, { refl }, { simp [fin.nat_add_zero, cast_le_of_eq, map_term_rel] }, { simp [fin.nat_add_zero, cast_le_of_eq, map_term_rel] }, { simp [map_term_rel, ih1, ih2], }, { simp [map_term_rel, ih3, cast_le], }, end /-- Substitutes the variables in a given formula with terms. -/ @[simp] def subst {n : ℕ} (φ : L.bounded_formula α n) (f : α → L.term β) : L.bounded_formula β n := φ.map_term_rel (λ _ t, t.subst (sum.elim (term.relabel sum.inl ∘ f) (var ∘ sum.inr))) (λ _, id) (λ _, id) /-- A bijection sending formulas with constants to formulas with extra variables. -/ def constants_vars_equiv : L[[γ]].bounded_formula α n ≃ L.bounded_formula (γ ⊕ α) n := map_term_rel_equiv (λ _, term.constants_vars_equiv_left) (λ _, equiv.sum_empty _ _) /-- Turns the extra variables of a bounded formula into free variables. -/ @[simp] def to_formula : ∀ {n : ℕ}, L.bounded_formula α n → L.formula (α ⊕ fin n) | n falsum := falsum | n (equal t₁ t₂) := t₁.equal t₂ | n (rel R ts) := R.formula ts | n (imp φ₁ φ₂) := φ₁.to_formula.imp φ₂.to_formula | n (all φ) := (φ.to_formula.relabel (sum.elim (sum.inl ∘ sum.inl) (sum.map sum.inr id ∘ fin_sum_fin_equiv.symm))).all variables {l : ℕ} {φ ψ : L.bounded_formula α l} {θ : L.bounded_formula α l.succ} variables {v : α → M} {xs : fin l → M} /-- An atomic formula is either equality or a relation symbol applied to terms. Note that `⊥` and `⊤` are not considered atomic in this convention. -/ inductive is_atomic : L.bounded_formula α n → Prop | equal (t₁ t₂ : L.term (α ⊕ fin n)) : is_atomic (bd_equal t₁ t₂) | rel {l : ℕ} (R : L.relations l) (ts : fin l → L.term (α ⊕ fin n)) : is_atomic (R.bounded_formula ts) lemma not_all_is_atomic (φ : L.bounded_formula α (n + 1)) : ¬ φ.all.is_atomic := λ con, by cases con lemma not_ex_is_atomic (φ : L.bounded_formula α (n + 1)) : ¬ φ.ex.is_atomic := λ con, by cases con lemma is_atomic.relabel {m : ℕ} {φ : L.bounded_formula α m} (h : φ.is_atomic) (f : α → β ⊕ (fin n)) : (φ.relabel f).is_atomic := is_atomic.rec_on h (λ _ _, is_atomic.equal _ _) (λ _ _ _, is_atomic.rel _ _) lemma is_atomic.lift_at {k m : ℕ} (h : is_atomic φ) : (φ.lift_at k m).is_atomic := is_atomic.rec_on h (λ _ _, is_atomic.equal _ _) (λ _ _ _, is_atomic.rel _ _) lemma is_atomic.cast_le {h : l ≤ n} (hφ : is_atomic φ) : (φ.cast_le h).is_atomic := is_atomic.rec_on hφ (λ _ _, is_atomic.equal _ _) (λ _ _ _, is_atomic.rel _ _) /-- A quantifier-free formula is a formula defined without quantifiers. These are all equivalent to boolean combinations of atomic formulas. -/ inductive is_qf : L.bounded_formula α n → Prop | falsum : is_qf falsum | of_is_atomic {φ} (h : is_atomic φ) : is_qf φ | imp {φ₁ φ₂} (h₁ : is_qf φ₁) (h₂ : is_qf φ₂) : is_qf (φ₁.imp φ₂) lemma is_atomic.is_qf {φ : L.bounded_formula α n} : is_atomic φ → is_qf φ := is_qf.of_is_atomic lemma is_qf_bot : is_qf (⊥ : L.bounded_formula α n) := is_qf.falsum lemma is_qf.not {φ : L.bounded_formula α n} (h : is_qf φ) : is_qf φ.not := h.imp is_qf_bot lemma is_qf.relabel {m : ℕ} {φ : L.bounded_formula α m} (h : φ.is_qf) (f : α → β ⊕ (fin n)) : (φ.relabel f).is_qf := is_qf.rec_on h is_qf_bot (λ _ h, (h.relabel f).is_qf) (λ _ _ _ _ h1 h2, h1.imp h2) lemma is_qf.lift_at {k m : ℕ} (h : is_qf φ) : (φ.lift_at k m).is_qf := is_qf.rec_on h is_qf_bot (λ _ ih, ih.lift_at.is_qf) (λ _ _ _ _ ih1 ih2, ih1.imp ih2) lemma is_qf.cast_le {h : l ≤ n} (hφ : is_qf φ) : (φ.cast_le h).is_qf := is_qf.rec_on hφ is_qf_bot (λ _ ih, ih.cast_le.is_qf) (λ _ _ _ _ ih1 ih2, ih1.imp ih2) lemma not_all_is_qf (φ : L.bounded_formula α (n + 1)) : ¬ φ.all.is_qf := λ con, begin cases con with _ con, exact (φ.not_all_is_atomic con), end lemma not_ex_is_qf (φ : L.bounded_formula α (n + 1)) : ¬ φ.ex.is_qf := λ con, begin cases con with _ con _ _ con, { exact (φ.not_ex_is_atomic con) }, { exact not_all_is_qf _ con } end /-- Indicates that a bounded formula is in prenex normal form - that is, it consists of quantifiers applied to a quantifier-free formula. -/ inductive is_prenex : ∀ {n}, L.bounded_formula α n → Prop | of_is_qf {n} {φ : L.bounded_formula α n} (h : is_qf φ) : is_prenex φ | all {n} {φ : L.bounded_formula α (n + 1)} (h : is_prenex φ) : is_prenex φ.all | ex {n} {φ : L.bounded_formula α (n + 1)} (h : is_prenex φ) : is_prenex φ.ex lemma is_qf.is_prenex {φ : L.bounded_formula α n} : is_qf φ → is_prenex φ := is_prenex.of_is_qf lemma is_atomic.is_prenex {φ : L.bounded_formula α n} (h : is_atomic φ) : is_prenex φ := h.is_qf.is_prenex lemma is_prenex.induction_on_all_not {P : ∀ {n}, L.bounded_formula α n → Prop} {φ : L.bounded_formula α n} (h : is_prenex φ) (hq : ∀ {m} {ψ : L.bounded_formula α m}, ψ.is_qf → P ψ) (ha : ∀ {m} {ψ : L.bounded_formula α (m + 1)}, P ψ → P ψ.all) (hn : ∀ {m} {ψ : L.bounded_formula α m}, P ψ → P ψ.not) : P φ := is_prenex.rec_on h (λ _ _, hq) (λ _ _ _, ha) (λ _ _ _ ih, hn (ha (hn ih))) lemma is_prenex.relabel {m : ℕ} {φ : L.bounded_formula α m} (h : φ.is_prenex) (f : α → β ⊕ (fin n)) : (φ.relabel f).is_prenex := is_prenex.rec_on h (λ _ _ h, (h.relabel f).is_prenex) (λ _ _ _ h, by simp [h.all]) (λ _ _ _ h, by simp [h.ex]) lemma is_prenex.cast_le (hφ : is_prenex φ) : ∀ {n} {h : l ≤ n}, (φ.cast_le h).is_prenex := is_prenex.rec_on hφ (λ _ _ ih _ _, ih.cast_le.is_prenex) (λ _ _ _ ih _ _, ih.all) (λ _ _ _ ih _ _, ih.ex) lemma is_prenex.lift_at {k m : ℕ} (h : is_prenex φ) : (φ.lift_at k m).is_prenex := is_prenex.rec_on h (λ _ _ ih, ih.lift_at.is_prenex) (λ _ _ _ ih, ih.cast_le.all) (λ _ _ _ ih, ih.cast_le.ex) /-- An auxiliary operation to `first_order.language.bounded_formula.to_prenex`. If `φ` is quantifier-free and `ψ` is in prenex normal form, then `φ.to_prenex_imp_right ψ` is a prenex normal form for `φ.imp ψ`. -/ def to_prenex_imp_right : ∀ {n}, L.bounded_formula α n → L.bounded_formula α n → L.bounded_formula α n | n φ (bounded_formula.ex ψ) := ((φ.lift_at 1 n).to_prenex_imp_right ψ).ex | n φ (all ψ) := ((φ.lift_at 1 n).to_prenex_imp_right ψ).all | n φ ψ := φ.imp ψ lemma is_qf.to_prenex_imp_right {φ : L.bounded_formula α n} : Π {ψ : L.bounded_formula α n}, is_qf ψ → (φ.to_prenex_imp_right ψ = φ.imp ψ) | _ is_qf.falsum := rfl | _ (is_qf.of_is_atomic (is_atomic.equal _ _)) := rfl | _ (is_qf.of_is_atomic (is_atomic.rel _ _)) := rfl | _ (is_qf.imp is_qf.falsum _) := rfl | _ (is_qf.imp (is_qf.of_is_atomic (is_atomic.equal _ _)) _) := rfl | _ (is_qf.imp (is_qf.of_is_atomic (is_atomic.rel _ _)) _) := rfl | _ (is_qf.imp (is_qf.imp _ _) _) := rfl lemma is_prenex_to_prenex_imp_right {φ ψ : L.bounded_formula α n} (hφ : is_qf φ) (hψ : is_prenex ψ) : is_prenex (φ.to_prenex_imp_right ψ) := begin induction hψ with _ _ hψ _ _ _ ih1 _ _ _ ih2, { rw hψ.to_prenex_imp_right, exact (hφ.imp hψ).is_prenex }, { exact (ih1 hφ.lift_at).all }, { exact (ih2 hφ.lift_at).ex } end /-- An auxiliary operation to `first_order.language.bounded_formula.to_prenex`. If `φ` and `ψ` are in prenex normal form, then `φ.to_prenex_imp ψ` is a prenex normal form for `φ.imp ψ`. -/ def to_prenex_imp : ∀ {n}, L.bounded_formula α n → L.bounded_formula α n → L.bounded_formula α n | n (bounded_formula.ex φ) ψ := (φ.to_prenex_imp (ψ.lift_at 1 n)).all | n (all φ) ψ := (φ.to_prenex_imp (ψ.lift_at 1 n)).ex | _ φ ψ := φ.to_prenex_imp_right ψ lemma is_qf.to_prenex_imp : Π {φ ψ : L.bounded_formula α n}, φ.is_qf → φ.to_prenex_imp ψ = φ.to_prenex_imp_right ψ | _ _ is_qf.falsum := rfl | _ _ (is_qf.of_is_atomic (is_atomic.equal _ _)) := rfl | _ _ (is_qf.of_is_atomic (is_atomic.rel _ _)) := rfl | _ _ (is_qf.imp is_qf.falsum _) := rfl | _ _ (is_qf.imp (is_qf.of_is_atomic (is_atomic.equal _ _)) _) := rfl | _ _ (is_qf.imp (is_qf.of_is_atomic (is_atomic.rel _ _)) _) := rfl | _ _ (is_qf.imp (is_qf.imp _ _) _) := rfl lemma is_prenex_to_prenex_imp {φ ψ : L.bounded_formula α n} (hφ : is_prenex φ) (hψ : is_prenex ψ) : is_prenex (φ.to_prenex_imp ψ) := begin induction hφ with _ _ hφ _ _ _ ih1 _ _ _ ih2, { rw hφ.to_prenex_imp, exact is_prenex_to_prenex_imp_right hφ hψ }, { exact (ih1 hψ.lift_at).ex }, { exact (ih2 hψ.lift_at).all } end /-- For any bounded formula `φ`, `φ.to_prenex` is a semantically-equivalent formula in prenex normal form. -/ def to_prenex : ∀ {n}, L.bounded_formula α n → L.bounded_formula α n | _ falsum := ⊥ | _ (equal t₁ t₂) := t₁.bd_equal t₂ | _ (rel R ts) := rel R ts | _ (imp f₁ f₂) := f₁.to_prenex.to_prenex_imp f₂.to_prenex | _ (all f) := f.to_prenex.all lemma to_prenex_is_prenex (φ : L.bounded_formula α n) : φ.to_prenex.is_prenex := bounded_formula.rec_on φ (λ _, is_qf_bot.is_prenex) (λ _ _ _, (is_atomic.equal _ _).is_prenex) (λ _ _ _ _, (is_atomic.rel _ _).is_prenex) (λ _ _ _ h1 h2, is_prenex_to_prenex_imp h1 h2) (λ _ _, is_prenex.all) end bounded_formula namespace Lhom open bounded_formula /-- Maps a bounded formula's symbols along a language map. -/ @[simp] def on_bounded_formula (g : L →ᴸ L') : ∀ {k : ℕ}, L.bounded_formula α k → L'.bounded_formula α k | k falsum := falsum | k (equal t₁ t₂) := (g.on_term t₁).bd_equal (g.on_term t₂) | k (rel R ts) := (g.on_relation R).bounded_formula (g.on_term ∘ ts) | k (imp f₁ f₂) := (on_bounded_formula f₁).imp (on_bounded_formula f₂) | k (all f) := (on_bounded_formula f).all @[simp] lemma id_on_bounded_formula : ((Lhom.id L).on_bounded_formula : L.bounded_formula α n → L.bounded_formula α n) = id := begin ext f, induction f with _ _ _ _ _ _ _ _ _ _ _ ih1 ih2 _ _ ih3, { refl }, { rw [on_bounded_formula, Lhom.id_on_term, id.def, id.def, id.def, bd_equal] }, { rw [on_bounded_formula, Lhom.id_on_term], refl, }, { rw [on_bounded_formula, ih1, ih2, id.def, id.def, id.def] }, { rw [on_bounded_formula, ih3, id.def, id.def] } end @[simp] lemma comp_on_bounded_formula {L'' : language} (φ : L' →ᴸ L'') (ψ : L →ᴸ L') : ((φ.comp ψ).on_bounded_formula : L.bounded_formula α n → L''.bounded_formula α n) = φ.on_bounded_formula ∘ ψ.on_bounded_formula := begin ext f, induction f with _ _ _ _ _ _ _ _ _ _ _ ih1 ih2 _ _ ih3, { refl }, { simp only [on_bounded_formula, comp_on_term, function.comp_app], refl, }, { simp only [on_bounded_formula, comp_on_relation, comp_on_term, function.comp_app], refl }, { simp only [on_bounded_formula, function.comp_app, ih1, ih2, eq_self_iff_true, and_self], }, { simp only [ih3, on_bounded_formula, function.comp_app] } end /-- Maps a formula's symbols along a language map. -/ def on_formula (g : L →ᴸ L') : L.formula α → L'.formula α := g.on_bounded_formula /-- Maps a sentence's symbols along a language map. -/ def on_sentence (g : L →ᴸ L') : L.sentence → L'.sentence := g.on_formula /-- Maps a theory's symbols along a language map. -/ def on_Theory (g : L →ᴸ L') (T : L.Theory) : L'.Theory := g.on_sentence '' T @[simp] lemma mem_on_Theory {g : L →ᴸ L'} {T : L.Theory} {φ : L'.sentence} : φ ∈ g.on_Theory T ↔ ∃ φ₀, φ₀ ∈ T ∧ g.on_sentence φ₀ = φ := set.mem_image _ _ _ end Lhom namespace Lequiv /-- Maps a bounded formula's symbols along a language equivalence. -/ @[simps] def on_bounded_formula (φ : L ≃ᴸ L') : L.bounded_formula α n ≃ L'.bounded_formula α n := { to_fun := φ.to_Lhom.on_bounded_formula, inv_fun := φ.inv_Lhom.on_bounded_formula, left_inv := by rw [function.left_inverse_iff_comp, ← Lhom.comp_on_bounded_formula, φ.left_inv, Lhom.id_on_bounded_formula], right_inv := by rw [function.right_inverse_iff_comp, ← Lhom.comp_on_bounded_formula, φ.right_inv, Lhom.id_on_bounded_formula] } lemma on_bounded_formula_symm (φ : L ≃ᴸ L') : (φ.on_bounded_formula.symm : L'.bounded_formula α n ≃ L.bounded_formula α n) = φ.symm.on_bounded_formula := rfl /-- Maps a formula's symbols along a language equivalence. -/ def on_formula (φ : L ≃ᴸ L') : L.formula α ≃ L'.formula α := φ.on_bounded_formula @[simp] lemma on_formula_apply (φ : L ≃ᴸ L') : (φ.on_formula : L.formula α → L'.formula α) = φ.to_Lhom.on_formula := rfl @[simp] lemma on_formula_symm (φ : L ≃ᴸ L') : (φ.on_formula.symm : L'.formula α ≃ L.formula α) = φ.symm.on_formula := rfl /-- Maps a sentence's symbols along a language equivalence. -/ @[simps] def on_sentence (φ : L ≃ᴸ L') : L.sentence ≃ L'.sentence := φ.on_formula end Lequiv localized "infix (name := term.bd_equal) ` =' `:88 := first_order.language.term.bd_equal" in first_order -- input \~- or \simeq localized "infixr (name := bounded_formula.imp) ` ⟹ `:62 := first_order.language.bounded_formula.imp" in first_order -- input \==> localized "prefix (name := bounded_formula.all) `∀'`:110 := first_order.language.bounded_formula.all" in first_order localized "prefix (name := bounded_formula.not) `∼`:max := first_order.language.bounded_formula.not" in first_order -- input \~, the ASCII character ~ has too low precedence localized "infix (name := bounded_formula.iff) ` ⇔ `:61 := first_order.language.bounded_formula.iff" in first_order -- input \<=> localized "prefix (name := bounded_formula.ex) `∃'`:110 := first_order.language.bounded_formula.ex" in first_order -- input \ex namespace formula /-- Relabels a formula's variables along a particular function. -/ def relabel (g : α → β) : L.formula α → L.formula β := @bounded_formula.relabel _ _ _ 0 (sum.inl ∘ g) 0 /-- The graph of a function as a first-order formula. -/ def graph (f : L.functions n) : L.formula (fin (n + 1)) := equal (var 0) (func f (λ i, var i.succ)) /-- The negation of a formula. -/ protected def not (φ : L.formula α) : L.formula α := φ.not /-- The implication between formulas, as a formula. -/ protected def imp : L.formula α → L.formula α → L.formula α := bounded_formula.imp /-- The biimplication between formulas, as a formula. -/ protected def iff (φ ψ : L.formula α) : L.formula α := φ.iff ψ lemma is_atomic_graph (f : L.functions n) : (graph f).is_atomic := bounded_formula.is_atomic.equal _ _ end formula namespace relations variable (r : L.relations 2) /-- The sentence indicating that a basic relation symbol is reflexive. -/ protected def reflexive : L.sentence := ∀' r.bounded_formula₂ &0 &0 /-- The sentence indicating that a basic relation symbol is irreflexive. -/ protected def irreflexive : L.sentence := ∀' ∼ (r.bounded_formula₂ &0 &0) /-- The sentence indicating that a basic relation symbol is symmetric. -/ protected def symmetric : L.sentence := ∀' ∀' (r.bounded_formula₂ &0 &1 ⟹ r.bounded_formula₂ &1 &0) /-- The sentence indicating that a basic relation symbol is antisymmetric. -/ protected def antisymmetric : L.sentence := ∀' ∀' (r.bounded_formula₂ &0 &1 ⟹ (r.bounded_formula₂ &1 &0 ⟹ term.bd_equal &0 &1)) /-- The sentence indicating that a basic relation symbol is transitive. -/ protected def transitive : L.sentence := ∀' ∀' ∀' (r.bounded_formula₂ &0 &1 ⟹ r.bounded_formula₂ &1 &2 ⟹ r.bounded_formula₂ &0 &2) /-- The sentence indicating that a basic relation symbol is total. -/ protected def total : L.sentence := ∀' ∀' (r.bounded_formula₂ &0 &1 ⊔ r.bounded_formula₂ &1 &0) end relations section cardinality variable (L) /-- A sentence indicating that a structure has `n` distinct elements. -/ protected def sentence.card_ge (n) : L.sentence := (((((list.fin_range n).product (list.fin_range n)).filter (λ ij : _ × _, ij.1 ≠ ij.2)).map (λ (ij : _ × _), ∼ ((& ij.1).bd_equal (& ij.2)))).foldr (⊓) ⊤).exs /-- A theory indicating that a structure is infinite. -/ def infinite_theory : L.Theory := set.range (sentence.card_ge L) /-- A theory that indicates a structure is nonempty. -/ def nonempty_theory : L.Theory := {sentence.card_ge L 1} /-- A theory indicating that each of a set of constants is distinct. -/ def distinct_constants_theory (s : set α) : L[[α]].Theory := (λ ab : α × α, (((L.con ab.1).term.equal (L.con ab.2).term).not)) '' (s ×ˢ s ∩ (set.diagonal α)ᶜ) variables {L} {α} open set lemma monotone_distinct_constants_theory : monotone (L.distinct_constants_theory : set α → L[[α]].Theory) := λ s t st, (image_subset _ (inter_subset_inter_left _ (prod_mono st st))) lemma directed_distinct_constants_theory : directed (⊆) (L.distinct_constants_theory : set α → L[[α]].Theory) := monotone.directed_le monotone_distinct_constants_theory lemma distinct_constants_theory_eq_Union (s : set α) : L.distinct_constants_theory s = ⋃ (t : finset s), L.distinct_constants_theory (t.map (function.embedding.subtype (λ x, x ∈ s))) := begin classical, simp only [distinct_constants_theory], rw [← image_Union, ← Union_inter], refine congr rfl (congr (congr rfl _) rfl), ext ⟨i, j⟩, simp only [prod_mk_mem_set_prod_eq, finset.coe_map, function.embedding.coe_subtype, mem_Union, mem_image, finset.mem_coe, subtype.exists, subtype.coe_mk, exists_and_distrib_right, exists_eq_right], refine ⟨λ h, ⟨{⟨i, h.1⟩, ⟨j, h.2⟩}, ⟨h.1, _⟩, ⟨h.2, _⟩⟩, _⟩, { simp }, { simp }, { rintros ⟨t, ⟨is, _⟩, ⟨js, _⟩⟩, exact ⟨is, js⟩ } end end cardinality end language end first_order
5214a4cccab376e34fe0d6365d13c3b5f98ff3cc
9dc8cecdf3c4634764a18254e94d43da07142918
/src/ring_theory/is_tensor_product.lean
e2d58cab89499ba6b69c063ec6fe2ad1dcb08911
[ "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
14,674
lean
/- Copyright (c) 2022 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import ring_theory.tensor_product import algebra.module.ulift import logic.equiv.transfer_instance /-! # The characteristice predicate of tensor product ## Main definitions - `is_tensor_product`: A predicate on `f : M₁ →ₗ[R] M₂ →ₗ[R] M` expressing that `f` realizes `M` as the tensor product of `M₁ ⊗[R] M₂`. This is defined by requiring the lift `M₁ ⊗[R] M₂ → M` to be bijective. - `is_base_change`: A predicate on an `R`-algebra `S` and a map `f : M →ₗ[R] N` with `N` being a `S`-module, expressing that `f` realizes `N` as the base change of `M` along `R → S`. ## Main results - `tensor_product.is_base_change`: `S ⊗[R] M` is the base change of `M` along `R → S`. -/ universes u v₁ v₂ v₃ v₄ open_locale tensor_product open tensor_product section is_tensor_product variables {R : Type*} [comm_ring R] variables {M₁ M₂ M M' : Type*} variables [add_comm_monoid M₁] [add_comm_monoid M₂] [add_comm_monoid M] [add_comm_monoid M'] variables [module R M₁] [module R M₂] [module R M] [module R M'] variable (f : M₁ →ₗ[R] M₂ →ₗ[R] M) variables {N₁ N₂ N : Type*} [add_comm_monoid N₁] [add_comm_monoid N₂] [add_comm_monoid N] variables [module R N₁] [module R N₂] [module R N] variable {g : N₁ →ₗ[R] N₂ →ₗ[R] N} /-- Given a bilinear map `f : M₁ →ₗ[R] M₂ →ₗ[R] M`, `is_tensor_product f` means that `M` is the tensor product of `M₁` and `M₂` via `f`. This is defined by requiring the lift `M₁ ⊗[R] M₂ → M` to be bijective. -/ def is_tensor_product : Prop := function.bijective (tensor_product.lift f) variables (R M N) {f} lemma tensor_product.is_tensor_product : is_tensor_product (tensor_product.mk R M N) := begin delta is_tensor_product, convert_to function.bijective linear_map.id using 2, { apply tensor_product.ext', simp }, { exact function.bijective_id } end variables {R M N} /-- If `M` is the tensor product of `M₁` and `M₂`, it is linearly equivalent to `M₁ ⊗[R] M₂`. -/ @[simps apply] noncomputable def is_tensor_product.equiv (h : is_tensor_product f) : M₁ ⊗[R] M₂ ≃ₗ[R] M := linear_equiv.of_bijective _ h.1 h.2 @[simp] lemma is_tensor_product.equiv_to_linear_map (h : is_tensor_product f) : h.equiv.to_linear_map = tensor_product.lift f := rfl @[simp] lemma is_tensor_product.equiv_symm_apply (h : is_tensor_product f) (x₁ : M₁) (x₂ : M₂) : h.equiv.symm (f x₁ x₂) = x₁ ⊗ₜ x₂ := begin apply h.equiv.injective, refine (h.equiv.apply_symm_apply _).trans _, simp end /-- If `M` is the tensor product of `M₁` and `M₂`, we may lift a bilinear map `M₁ →ₗ[R] M₂ →ₗ[R] M'` to a `M →ₗ[R] M'`. -/ noncomputable def is_tensor_product.lift (h : is_tensor_product f) (f' : M₁ →ₗ[R] M₂ →ₗ[R] M') : M →ₗ[R] M' := (tensor_product.lift f').comp h.equiv.symm.to_linear_map lemma is_tensor_product.lift_eq (h : is_tensor_product f) (f' : M₁ →ₗ[R] M₂ →ₗ[R] M') (x₁ : M₁) (x₂ : M₂) : h.lift f' (f x₁ x₂) = f' x₁ x₂ := begin delta is_tensor_product.lift, simp, end /-- The tensor product of a pair of linear maps between modules. -/ noncomputable def is_tensor_product.map (hf : is_tensor_product f) (hg : is_tensor_product g) (i₁ : M₁ →ₗ[R] N₁) (i₂ : M₂ →ₗ[R] N₂) : M →ₗ[R] N := hg.equiv.to_linear_map.comp ((tensor_product.map i₁ i₂).comp hf.equiv.symm.to_linear_map) lemma is_tensor_product.map_eq (hf : is_tensor_product f) (hg : is_tensor_product g) (i₁ : M₁ →ₗ[R] N₁) (i₂ : M₂ →ₗ[R] N₂) (x₁ : M₁) (x₂ : M₂) : hf.map hg i₁ i₂ (f x₁ x₂) = g (i₁ x₁) (i₂ x₂) := begin delta is_tensor_product.map, simp end lemma is_tensor_product.induction_on (h : is_tensor_product f) {C : M → Prop} (m : M) (h0 : C 0) (htmul : ∀ x y, C (f x y)) (hadd : ∀ x y, C x → C y → C (x + y)) : C m := begin rw ← h.equiv.right_inv m, generalize : h.equiv.inv_fun m = y, change C (tensor_product.lift f y), induction y using tensor_product.induction_on, { rwa map_zero }, { rw tensor_product.lift.tmul, apply htmul }, { rw map_add, apply hadd; assumption } end end is_tensor_product section is_base_change variables {R : Type*} {M : Type v₁} {N : Type v₂} (S : Type v₃) variables [add_comm_monoid M] [add_comm_monoid N] [comm_ring R] variables [comm_ring S] [algebra R S] [module R M] [module R N] [module S N] [is_scalar_tower R S N] variables (f : M →ₗ[R] N) include f /-- Given an `R`-algebra `S` and an `R`-module `M`, an `S`-module `N` together with a map `f : M →ₗ[R] N` is the base change of `M` to `S` if the map `S × M → N, (s, m) ↦ s • f m` is the tensor product. -/ def is_base_change : Prop := is_tensor_product (((algebra.of_id S $ module.End S (M →ₗ[R] N)).to_linear_map.flip f).restrict_scalars R) variables {S f} (h : is_base_change S f) variables {P Q : Type*} [add_comm_monoid P] [module R P] variables [add_comm_monoid Q] [module S Q] section variables [module R Q] [is_scalar_tower R S Q] /-- Suppose `f : M →ₗ[R] N` is the base change of `M` along `R → S`. Then any `R`-linear map from `M` to an `S`-module factors thorugh `f`. -/ noncomputable def is_base_change.lift (g : M →ₗ[R] Q) : N →ₗ[S] Q := { map_smul' := λ r x, begin let F := ((algebra.of_id S $ module.End S (M →ₗ[R] Q)) .to_linear_map.flip g).restrict_scalars R, have hF : ∀ (s : S) (m : M), h.lift F (s • f m) = s • g m := h.lift_eq F, change h.lift F (r • x) = r • h.lift F x, apply h.induction_on x, { rw [smul_zero, map_zero, smul_zero] }, { intros s m, change h.lift F (r • s • f m) = r • h.lift F (s • f m), rw [← mul_smul, hF, hF, mul_smul] }, { intros x₁ x₂ e₁ e₂, rw [map_add, smul_add, map_add, smul_add, e₁, e₂] } end, ..(h.lift (((algebra.of_id S $ module.End S (M →ₗ[R] Q)) .to_linear_map.flip g).restrict_scalars R)) } lemma is_base_change.lift_eq (g : M →ₗ[R] Q) (x : M) : h.lift g (f x) = g x := begin have hF : ∀ (s : S) (m : M), h.lift g (s • f m) = s • g m := h.lift_eq _, convert hF 1 x; rw one_smul, end lemma is_base_change.lift_comp (g : M →ₗ[R] Q) : ((h.lift g).restrict_scalars R).comp f = g := linear_map.ext (h.lift_eq g) end include h @[elab_as_eliminator] lemma is_base_change.induction_on (x : N) (P : N → Prop) (h₁ : P 0) (h₂ : ∀ m : M, P (f m)) (h₃ : ∀ (s : S) n, P n → P (s • n)) (h₄ : ∀ n₁ n₂, P n₁ → P n₂ → P (n₁ + n₂)) : P x := h.induction_on x h₁ (λ s y, h₃ _ _ (h₂ _)) h₄ lemma is_base_change.alg_hom_ext (g₁ g₂ : N →ₗ[S] Q) (e : ∀ x, g₁ (f x) = g₂ (f x)) : g₁ = g₂ := begin ext x, apply h.induction_on x, { rw [map_zero, map_zero] }, { assumption }, { intros s n e', rw [g₁.map_smul, g₂.map_smul, e'] }, { intros x y e₁ e₂, rw [map_add, map_add, e₁, e₂] } end lemma is_base_change.alg_hom_ext' [module R Q] [is_scalar_tower R S Q] (g₁ g₂ : N →ₗ[S] Q) (e : (g₁.restrict_scalars R).comp f = (g₂.restrict_scalars R).comp f) : g₁ = g₂ := h.alg_hom_ext g₁ g₂ (linear_map.congr_fun e) variables (R M N S) omit h f lemma tensor_product.is_base_change : is_base_change S (tensor_product.mk R S M 1) := begin delta is_base_change, convert tensor_product.is_tensor_product R S M using 1, ext s x, change s • 1 ⊗ₜ x = s ⊗ₜ x, rw tensor_product.smul_tmul', congr' 1, exact mul_one _, end variables {R M N S} /-- The base change of `M` along `R → S` is linearly equivalent to `S ⊗[R] M`. -/ noncomputable def is_base_change.equiv : S ⊗[R] M ≃ₗ[R] N := h.equiv lemma is_base_change.equiv_tmul (s : S) (m : M) : h.equiv (s ⊗ₜ m) = s • (f m) := tensor_product.lift.tmul s m lemma is_base_change.equiv_symm_apply (m : M) : h.equiv.symm (f m) = 1 ⊗ₜ m := by rw [h.equiv.symm_apply_eq, h.equiv_tmul, one_smul] variable (f) lemma is_base_change.of_lift_unique (h : ∀ (Q : Type (max v₁ v₂ v₃)) [add_comm_monoid Q], by exactI ∀ [module R Q] [module S Q], by exactI ∀ [is_scalar_tower R S Q], by exactI ∀ (g : M →ₗ[R] Q), ∃! (g' : N →ₗ[S] Q), (g'.restrict_scalars R).comp f = g) : is_base_change S f := begin delta is_base_change is_tensor_product, obtain ⟨g, hg, hg'⟩ := h (ulift.{v₂} $ S ⊗[R] M) (ulift.module_equiv.symm.to_linear_map.comp $ tensor_product.mk R S M 1), let f' : S ⊗[R] M →ₗ[R] N := _, change function.bijective f', let f'' : S ⊗[R] M →ₗ[S] N, { refine { map_smul' := λ r x, _, ..f' }, apply tensor_product.induction_on x, { simp only [map_zero, smul_zero, linear_map.to_fun_eq_coe] }, { intros x y, simp only [algebra.of_id_apply, algebra.id.smul_eq_mul, alg_hom.to_linear_map_apply, linear_map.mul_apply, tensor_product.lift.tmul', linear_map.smul_apply, ring_hom.id_apply, module.algebra_map_End_apply, f', _root_.map_mul, tensor_product.smul_tmul', linear_map.coe_restrict_scalars_eq_coe, linear_map.flip_apply] }, { intros x y hx hy, dsimp at hx hy ⊢, simp only [hx, hy, smul_add, map_add] } }, change function.bijective f'', split, { apply function.has_left_inverse.injective, refine ⟨ulift.module_equiv.to_linear_map.comp g, λ x, _⟩, apply tensor_product.induction_on x, { simp only [map_zero] }, { intros x y, have := (congr_arg (λ a, x • a) (linear_map.congr_fun hg y)).trans (ulift.module_equiv.symm.map_smul x _).symm, apply (ulift.module_equiv : ulift.{v₂} (S ⊗ M) ≃ₗ[S] S ⊗ M) .to_equiv.apply_eq_iff_eq_symm_apply.mpr, any_goals { apply_instance }, simpa only [algebra.of_id_apply, smul_tmul', algebra.id.smul_eq_mul, lift.tmul', linear_map.coe_restrict_scalars_eq_coe, linear_map.flip_apply, alg_hom.to_linear_map_apply, module.algebra_map_End_apply, linear_map.smul_apply, linear_map.coe_mk, linear_map.map_smulₛₗ, mk_apply, mul_one] using this }, { intros x y hx hy, simp only [map_add, hx, hy] } }, { apply function.has_right_inverse.surjective, refine ⟨ulift.module_equiv.to_linear_map.comp g, λ x, _⟩, obtain ⟨g', hg₁, hg₂⟩ := h (ulift.{max v₁ v₃} N) (ulift.module_equiv.symm.to_linear_map.comp f), have : g' = ulift.module_equiv.symm.to_linear_map := by { refine (hg₂ _ _).symm, refl }, subst this, apply (ulift.module_equiv : ulift.{max v₁ v₃} N ≃ₗ[S] N).symm.injective, simp_rw [← linear_equiv.coe_to_linear_map, ← linear_map.comp_apply], congr' 1, apply hg₂, ext y, have := linear_map.congr_fun hg y, dsimp [ulift.module_equiv] at this ⊢, rw this, simp only [lift.tmul, linear_map.coe_restrict_scalars_eq_coe, linear_map.flip_apply, alg_hom.to_linear_map_apply, _root_.map_one, linear_map.one_apply] } end variable {f} lemma is_base_change.iff_lift_unique : is_base_change S f ↔ ∀ (Q : Type (max v₁ v₂ v₃)) [add_comm_monoid Q], by exactI ∀ [module R Q] [module S Q], by exactI ∀ [is_scalar_tower R S Q], by exactI ∀ (g : M →ₗ[R] Q), ∃! (g' : N →ₗ[S] Q), (g'.restrict_scalars R).comp f = g := ⟨λ h, by { introsI, exact ⟨h.lift g, h.lift_comp g, λ g' e, h.alg_hom_ext' _ _ (e.trans (h.lift_comp g).symm)⟩ }, is_base_change.of_lift_unique f⟩ lemma is_base_change.of_equiv (e : M ≃ₗ[R] N) : is_base_change R e.to_linear_map := begin apply is_base_change.of_lift_unique, introsI Q I₁ I₂ I₃ I₄ g, have : I₂ = I₃, { ext r q, rw [← one_smul R q, smul_smul, ← smul_assoc, smul_eq_mul, mul_one] }, unfreezingI { cases this }, refine ⟨g.comp e.symm.to_linear_map, by { ext, simp }, _⟩, rintros y (rfl : _ = _), ext, simp, end variables {T O : Type*} [comm_ring T] [algebra R T] [algebra S T] [is_scalar_tower R S T] variables [add_comm_monoid O] [module R O] [module S O] [module T O] [is_scalar_tower S T O] variables [is_scalar_tower R S O] [is_scalar_tower R T O] lemma is_base_change.comp {f : M →ₗ[R] N} (hf : is_base_change S f) {g : N →ₗ[S] O} (hg : is_base_change T g) : is_base_change T ((g.restrict_scalars R).comp f) := begin apply is_base_change.of_lift_unique, introsI Q _ _ _ _ i, letI := module.comp_hom Q (algebra_map S T), haveI : is_scalar_tower S T Q := ⟨λ x y z, by { rw [algebra.smul_def, mul_smul], refl }⟩, haveI : is_scalar_tower R S Q, { refine ⟨λ x y z, _⟩, change (is_scalar_tower.to_alg_hom R S T) (x • y) • z = x • (algebra_map S T y • z), rw [alg_hom.map_smul, smul_assoc], refl }, refine ⟨hg.lift (hf.lift i), by { ext, simp [is_base_change.lift_eq] }, _⟩, rintros g' (e : _ = _), refine hg.alg_hom_ext' _ _ (hf.alg_hom_ext' _ _ _), rw [is_base_change.lift_comp, is_base_change.lift_comp, ← e], ext, refl end variables {R' S' : Type*} [comm_ring R'] [comm_ring S'] variables [algebra R R'] [algebra S S'] [algebra R' S'] [algebra R S'] variables [is_scalar_tower R R' S'] [is_scalar_tower R S S'] lemma is_base_change.symm (h : is_base_change S (is_scalar_tower.to_alg_hom R R' S').to_linear_map) : is_base_change R' (is_scalar_tower.to_alg_hom R S S').to_linear_map := begin letI := (algebra.tensor_product.include_right : R' →ₐ[R] S ⊗ R').to_ring_hom.to_algebra, let e : R' ⊗[R] S ≃ₗ[R'] S', { refine { map_smul' := _, ..((tensor_product.comm R R' S).trans $ h.equiv.restrict_scalars R) }, intros r x, change h.equiv (tensor_product.comm R R' S (r • x)) = r • h.equiv (tensor_product.comm R R' S x), apply tensor_product.induction_on x, { simp only [smul_zero, map_zero] }, { intros x y, simp [smul_tmul', algebra.smul_def, ring_hom.algebra_map_to_algebra, h.equiv_tmul], ring }, { intros x y hx hy, simp only [map_add, smul_add, hx, hy] } }, have : (is_scalar_tower.to_alg_hom R S S').to_linear_map = (e.to_linear_map.restrict_scalars R).comp (tensor_product.mk R R' S 1), { ext, simp [e, h.equiv_tmul, algebra.smul_def] }, rw this, exact (tensor_product.is_base_change R S R').comp (is_base_change.of_equiv e), end variables (R S R' S') lemma is_base_change.comm : is_base_change S (is_scalar_tower.to_alg_hom R R' S').to_linear_map ↔ is_base_change R' (is_scalar_tower.to_alg_hom R S S').to_linear_map := ⟨is_base_change.symm, is_base_change.symm⟩ end is_base_change
727146b8679ae171b6df108087ca6b1358c0376f
5c7fe6c4a9d4079b5457ffa5f061797d42a1cd65
/src/exercises/src_25_intersection_and_union.lean
0a5145ae9418ddd53c1453b4dbbe463bb032dce6
[]
no_license
gihanmarasingha/mth1001_tutorial
8e0817feeb96e7c1bb3bac49b63e3c9a3a329061
bb277eebd5013766e1418365b91416b406275130
refs/heads/master
1,675,008,746,310
1,607,993,443,000
1,607,993,443,000
321,511,270
3
0
null
null
null
null
UTF-8
Lean
false
false
4,159
lean
import tactic import data.set import logic.basic open set namespace mth1001 section intersection_and_union -- We'll deal with sets on a type `U`. variable U : Type* variables A B C : set U /- Typing set notation in Lean: `∪` is `\cup` `∩` is `\cap` `∈` is `\in` `∉` is `\notin` `∅` is `\empty` -/ /- Throughout this file, we use `mem_inter_iff` and `mem_union_eq` to rewrite intersections and unions as conjunctions and disjunctions. -/ example (x : U) : x ∈ A ∩ B ↔ x ∈ A ∧ x ∈ B := by rw mem_inter_iff example (x : U) : x ∈ A ∪ B ↔ x ∈ A ∨ x ∈ B := by rw mem_union_eq /- We prove right distributivity of intersection over union. -/ example : (A ∪ B) ∩ C = (A ∩ C) ∪ (B ∩ C) := begin ext, -- Assume `x : U`. It suffices to show `x ∈ (A ∪ B) ∩ C ↔ x ∈ (A ∩ C) ∪ (B ∩ C)`. -- Repeatedly apply the basic theorems on membership of intersections and unions. -- `or_and_distrib_right` is the distributive law, `(P ∨ Q) ∧ R ↔ (P ∧ Q) ∨ (Q ∧ R)`. calc x ∈ (A ∪ B) ∩ C ↔ x ∈ (A ∪ B) ∧ x ∈ C : by rw mem_inter_iff ... ↔ (x ∈ A ∨ x ∈ B) ∧ x ∈ C : by rw mem_union_eq ... ↔ (x ∈ A ∧ x ∈ C) ∨ (x ∈ B ∧ x ∈ C) : by rw or_and_distrib_right ... ↔ x ∈ A ∩ C ∨ x ∈ B ∩ C : by rw [mem_inter_iff, mem_inter_iff] ... ↔ x ∈ (A ∩ C) ∪ (B ∩ C) : by rw mem_union_eq, end -- Alternatively, we can just rewrite the goal many times. example : (A ∪ B) ∩ C = (A ∩ C) ∪ (B ∩ C) := begin ext, -- Assume `x : U`. It suffices to show `x ∈ (A ∪ B) ∩ C ↔ x ∈ (A ∩ C) ∪ (B ∩ C)`. repeat { rw mem_inter_iff }, repeat { rw mem_union_eq, }, repeat { rw mem_inter_iff }, rw or_and_distrib_right, end /- *************************** Laws of propositional logic *************************** -/ -- Feel free to use the following laws in the proofs below. variables p q r : Prop example : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (p ∨ r) := by rw or_and_distrib_left example : p ∧ (q ∨ r) ↔ (p ∧ q) ∨ (p ∧ r) := by rw and_or_distrib_left example : (p ∨ q) ∧ r ↔ (p ∧ r) ∨ (q ∧ r) := by rw or_and_distrib_right example : (p ∧ q) ∨ r ↔ (p ∨ r) ∧ (q ∨ r) := by rw and_or_distrib_right example : (p ∧ q) ∧ r ↔ p ∧ (q ∧ r) := by rw and_assoc example : (p ∨ q) ∨ r ↔ p ∨ (q ∨ r) := by rw or_assoc example : p ∨ false ↔ p := by rw or_false example : p ∧ false ↔ false := by rw and_false example : p ∧ p ↔ p := by rw and_self example : p ∨ p ↔ p := by rw or_self -- Exercise 131: -- Adapt either of the proofs above to give the following distributive law. example : A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) := begin sorry end -- Exercise 132: example : A ∩ B = B ∩ A := begin sorry end -- Exercise 133: example : A ∪ B = B ∪ A := begin sorry end -- Exercise 134: example : (A ∩ B) ∩ C = A ∩ (B ∩ C) := begin sorry end -- Exercise 135: example : (A ∪ B) ∪ C = A ∪ (B ∪ C) := begin sorry end -- Exercise 136: example : A ∩ A = A := begin sorry end /- For the next result, we use `empty_def` and `mem_set_of_eq`, statements that are equivalent to our mathematical definitions of empty set and set membership, respectively. -/ example : ∅ = {x : U | false} := by rw empty_def example (P : U → Prop) (z : U) : z ∈ {x : U | P x} = P z := by rw mem_set_of_eq example : A ∩ ∅ = ∅ := begin ext x, -- Assume `x : U`. It remains to prove `x ∈ A ∩ ∅ ↔ x ∈ ∅`. rw mem_inter_iff, -- Rewrite `x ∈ A ∩ ∅` as `x ∈ A ∧ x ∈ ∅`. rw empty_def, -- Rewrite `∅` as `{x : U | ⊥}`. rw mem_set_of_eq, -- Rewrite `x ∈ {x : U | ⊥}` as `⊥`. rw and_false, -- Complete the goal using the result `P ∧ ⊥ ↔ ⊥` and reflexivity of `↔`. end -- Exercise 137: example : A ∪ ∅ = A := begin sorry end end intersection_and_union end mth1001
ae2207a4acb831f7da62d5908c8025d6d910c49d
b73bd2854495d87ad5ce4f247cfcd6faa7e71c7e
/src/game/world1/level1.lean
4a4045c63074fec4f68743d45b5b979fb9706264
[]
no_license
agusakov/category-theory-game
20db0b26270e0c95a3d5605498570273d72f731d
652dd7e90ae706643b2a597e2c938403653e167d
refs/heads/master
1,669,201,216,310
1,595,740,057,000
1,595,740,057,000
280,895,295
12
0
null
null
null
null
UTF-8
Lean
false
false
1,547
lean
import category_theory.category.default universes v u -- The order in this declaration matters: v often needs to be explicitly specified while u often can be omitted namespace category_theory variables (C : Type u) [category.{v} C] /- # Category world ## Level 1: Definition of category A category `C` consists of * a collection of objects `X, Y, Z, ...` * a collection of morphisms `f, g, h, ...` so that: * each morphism has specified domain and codomain objects; `f : X ⟶ Y` signifies that `f` is a morphism with domain `X` and codomain `Y` * each object has a designated identity morphism `𝟙 X : X ⟶ X` * for any pair of morphisms `f, g` with the codomain of `f` equal to the domain of `g`,the exists a specified composite morphism `f ≫ g` whose domain is that of `f` and codomain that of `g`, i.e. `f : X ⟶ Y, g : Y ⟶ Z` then `f ≫ g : X ⟶ Z` This data is subject to the following axioms: * For any `f : X ⟶ Y`, -/ /- Axiom : f ≫ 𝟙 Y = f-/ /- Axiom: 𝟙 X ≫ f = f-/ /-* For any composable triple of morphisms `f, g, h`, we have associativity `f ≫ (g ≫ h) = (f ≫ g) ≫ h`-/ /- Axiom: f ≫ (g ≫ h) = (f ≫ g) ≫ h-/ /-First we start out with some easy lemmas to get us warmed up.-/ /- Lemma If $$f : X ⟶ Y$$ and $$g : X ⟶ Y$$ are morphisms such that $$f = g$$, then $$f ≫ h = g ≫ h$$. -/ lemma eq_precomp_eq {X Y Z : C} {f g : X ⟶ Y} (w : f = g) (h : Y ⟶ Z) : f ≫ h = g ≫ h := begin rw w, end end category_theory
722fb7e52e66e2716d38b727ee062cc754579540
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/tests/lean/mvar1.lean
85b43409187ede997f0c9254d73e7a30b111ae60
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,097
lean
import Lean.MetavarContext open Lean def check (b : Bool) : IO Unit := «unless» b (throw $ IO.userError "error") def f := mkConst `f [] def g := mkConst `g [] def a := mkConst `a [] def b := mkConst `b [] def c := mkConst `c [] def b0 := mkBVar 0 def b1 := mkBVar 1 def b2 := mkBVar 2 def u := mkLevelParam `u def typeE := mkSort levelOne def natE := mkConst `Nat [] def boolE := mkConst `Bool [] def vecE := mkConst `Vec [levelZero] def α := mkFVar `α def x := mkFVar `x def y := mkFVar `y def z := mkFVar `z def w := mkFVar `w def m1 := mkMVar `m1 def m2 := mkMVar `m2 def m3 := mkMVar `m3 def m4 := mkMVar `m4 def m5 := mkMVar `m5 def m6 := mkMVar `m6 def bi := BinderInfo.default def arrow (d b : Expr) := mkForall `_ bi d b def lctx1 : LocalContext := {} def lctx2 := lctx1.mkLocalDecl `α `α typeE def lctx3 := lctx2.mkLocalDecl `x `x natE def lctx4 := lctx3.mkLocalDecl `y `y α def lctx5 := lctx4.mkLocalDecl `z `z (mkAppN vecE #[x]) def lctx6 := lctx5.mkLocalDecl `w `w (arrow natE (mkAppN m6 #[α, x, y])) def t1 := lctx5.mkLambda #[α, x, y] $ mkAppN f #[α, mkAppN g #[x, y], lctx5.mkLambda #[z] (mkApp f z)] #eval check (!t1.hasFVar) #eval t1 def mctx1 : MetavarContext := {} def mctx2 := mctx1.addExprMVarDecl `m1 `m1 lctx3 #[] natE def mctx3 := mctx2.addExprMVarDecl `m2 `m2 lctx4 #[] α def mctx4 := mctx3.addExprMVarDecl `m3 `m3 lctx4 #[] natE def mctx5 := mctx4.addExprMVarDecl `m4 `m4 lctx1 #[] (arrow typeE (arrow natE (arrow α natE))) def mctx6 := mctx5.addExprMVarDecl `m5 `m5 lctx5 #[] typeE def mctx7 := mctx5.addExprMVarDecl `m6 `m6 lctx5 #[] (arrow typeE (arrow natE (arrow α natE))) def mctx8 := mctx7.assignDelayed `m4 lctx4 #[α, x, y] m3 def mctx9 := mctx8.assignExpr `m3 (mkAppN g #[x, y]) def mctx10 := mctx9.assignExpr `m1 a def t2 := lctx5.mkLambda #[α, x, y] $ mkAppN f #[mkAppN m4 #[α, x, y], x] #eval check (!t2.hasFVar) #eval t2 #eval (mctx6.instantiateMVars t2).1 #eval check (!(mctx9.instantiateMVars t2).1.hasMVar) #eval check (!(mctx9.instantiateMVars t2).1.hasFVar) #eval (mctx9.instantiateMVars t2).1 -- t3 is ill-formed since m3's localcontext contains ‵α`, `x` and `y` def t3 := lctx5.mkLambda #[α, x, y] $ mkAppN f #[m3, x] #eval check (mctx10.instantiateMVars t3).1.hasFVar #eval (mctx7.instantiateMVars t3).1 partial def mkDiamond : Nat → Expr | 0 => m1 | (n+1) => mkAppN f #[mkDiamond n, mkDiamond n] #eval (mctx7.instantiateMVars (mkDiamond 3)).1 #eval (mctx10.instantiateMVars (mkDiamond 3)).1 #eval (mctx7.instantiateMVars (mkDiamond 100)).1.getAppFn #eval (mctx10.instantiateMVars (mkDiamond 100)).1.getAppFn def mctx11 := mctx10.assignDelayed `m6 lctx5 #[α, x, y] m5 def mctx12 := mctx11.assignExpr `m5 (arrow α α) def t4 := lctx6.mkLambda #[α, x, y, w] $ mkAppN f #[mkAppN m4 #[α, x, y], x] #eval t4 #eval (mctx9.instantiateMVars t4).1 #eval (mctx12.instantiateMVars t4).1 #eval check (mctx9.instantiateMVars t4).1.hasMVar #eval check (!((mctx9.instantiateMVars t4).1.hasFVar)) #eval check (!(mctx12.instantiateMVars t4).1.hasMVar) #eval check (!((mctx12.instantiateMVars t4).1.hasFVar))
8247c2fdc47eccc3e0d8e11dfed1f2d93751a104
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/RightSpindle_ShelfSig.lean
4f32d6b3a2d475dce777d7a4facceb1b4cc435f2
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
9,620
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section RightSpindle_ShelfSig structure RightSpindle_ShelfSig (A : Type) : Type := (rinv : (A → (A → A))) (rightDistributive : (∀ {x y z : A} , (rinv (rinv y z) x) = (rinv (rinv y x) (rinv z x)))) (idempotent_rinv : (∀ {x : A} , (rinv x x) = x)) (linv : (A → (A → A))) open RightSpindle_ShelfSig structure Sig (AS : Type) : Type := (rinvS : (AS → (AS → AS))) (linvS : (AS → (AS → AS))) structure Product (A : Type) : Type := (rinvP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (linvP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (rightDistributiveP : (∀ {xP yP zP : (Prod A A)} , (rinvP (rinvP yP zP) xP) = (rinvP (rinvP yP xP) (rinvP zP xP)))) (idempotent_rinvP : (∀ {xP : (Prod A A)} , (rinvP xP xP) = xP)) structure Hom {A1 : Type} {A2 : Type} (Ri1 : (RightSpindle_ShelfSig A1)) (Ri2 : (RightSpindle_ShelfSig A2)) : Type := (hom : (A1 → A2)) (pres_rinv : (∀ {x1 x2 : A1} , (hom ((rinv Ri1) x1 x2)) = ((rinv Ri2) (hom x1) (hom x2)))) (pres_linv : (∀ {x1 x2 : A1} , (hom ((linv Ri1) x1 x2)) = ((linv Ri2) (hom x1) (hom x2)))) structure RelInterp {A1 : Type} {A2 : Type} (Ri1 : (RightSpindle_ShelfSig A1)) (Ri2 : (RightSpindle_ShelfSig A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_rinv : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((rinv Ri1) x1 x2) ((rinv Ri2) y1 y2)))))) (interp_linv : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((linv Ri1) x1 x2) ((linv Ri2) y1 y2)))))) inductive RightSpindle_ShelfSigTerm : Type | rinvL : (RightSpindle_ShelfSigTerm → (RightSpindle_ShelfSigTerm → RightSpindle_ShelfSigTerm)) | linvL : (RightSpindle_ShelfSigTerm → (RightSpindle_ShelfSigTerm → RightSpindle_ShelfSigTerm)) open RightSpindle_ShelfSigTerm inductive ClRightSpindle_ShelfSigTerm (A : Type) : Type | sing : (A → ClRightSpindle_ShelfSigTerm) | rinvCl : (ClRightSpindle_ShelfSigTerm → (ClRightSpindle_ShelfSigTerm → ClRightSpindle_ShelfSigTerm)) | linvCl : (ClRightSpindle_ShelfSigTerm → (ClRightSpindle_ShelfSigTerm → ClRightSpindle_ShelfSigTerm)) open ClRightSpindle_ShelfSigTerm inductive OpRightSpindle_ShelfSigTerm (n : ℕ) : Type | v : ((fin n) → OpRightSpindle_ShelfSigTerm) | rinvOL : (OpRightSpindle_ShelfSigTerm → (OpRightSpindle_ShelfSigTerm → OpRightSpindle_ShelfSigTerm)) | linvOL : (OpRightSpindle_ShelfSigTerm → (OpRightSpindle_ShelfSigTerm → OpRightSpindle_ShelfSigTerm)) open OpRightSpindle_ShelfSigTerm inductive OpRightSpindle_ShelfSigTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpRightSpindle_ShelfSigTerm2) | sing2 : (A → OpRightSpindle_ShelfSigTerm2) | rinvOL2 : (OpRightSpindle_ShelfSigTerm2 → (OpRightSpindle_ShelfSigTerm2 → OpRightSpindle_ShelfSigTerm2)) | linvOL2 : (OpRightSpindle_ShelfSigTerm2 → (OpRightSpindle_ShelfSigTerm2 → OpRightSpindle_ShelfSigTerm2)) open OpRightSpindle_ShelfSigTerm2 def simplifyCl {A : Type} : ((ClRightSpindle_ShelfSigTerm A) → (ClRightSpindle_ShelfSigTerm A)) | (rinvCl x1 x2) := (rinvCl (simplifyCl x1) (simplifyCl x2)) | (linvCl x1 x2) := (linvCl (simplifyCl x1) (simplifyCl x2)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpRightSpindle_ShelfSigTerm n) → (OpRightSpindle_ShelfSigTerm n)) | (rinvOL x1 x2) := (rinvOL (simplifyOpB x1) (simplifyOpB x2)) | (linvOL x1 x2) := (linvOL (simplifyOpB x1) (simplifyOpB x2)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpRightSpindle_ShelfSigTerm2 n A) → (OpRightSpindle_ShelfSigTerm2 n A)) | (rinvOL2 x1 x2) := (rinvOL2 (simplifyOp x1) (simplifyOp x2)) | (linvOL2 x1 x2) := (linvOL2 (simplifyOp x1) (simplifyOp x2)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((RightSpindle_ShelfSig A) → (RightSpindle_ShelfSigTerm → A)) | Ri (rinvL x1 x2) := ((rinv Ri) (evalB Ri x1) (evalB Ri x2)) | Ri (linvL x1 x2) := ((linv Ri) (evalB Ri x1) (evalB Ri x2)) def evalCl {A : Type} : ((RightSpindle_ShelfSig A) → ((ClRightSpindle_ShelfSigTerm A) → A)) | Ri (sing x1) := x1 | Ri (rinvCl x1 x2) := ((rinv Ri) (evalCl Ri x1) (evalCl Ri x2)) | Ri (linvCl x1 x2) := ((linv Ri) (evalCl Ri x1) (evalCl Ri x2)) def evalOpB {A : Type} {n : ℕ} : ((RightSpindle_ShelfSig A) → ((vector A n) → ((OpRightSpindle_ShelfSigTerm n) → A))) | Ri vars (v x1) := (nth vars x1) | Ri vars (rinvOL x1 x2) := ((rinv Ri) (evalOpB Ri vars x1) (evalOpB Ri vars x2)) | Ri vars (linvOL x1 x2) := ((linv Ri) (evalOpB Ri vars x1) (evalOpB Ri vars x2)) def evalOp {A : Type} {n : ℕ} : ((RightSpindle_ShelfSig A) → ((vector A n) → ((OpRightSpindle_ShelfSigTerm2 n A) → A))) | Ri vars (v2 x1) := (nth vars x1) | Ri vars (sing2 x1) := x1 | Ri vars (rinvOL2 x1 x2) := ((rinv Ri) (evalOp Ri vars x1) (evalOp Ri vars x2)) | Ri vars (linvOL2 x1 x2) := ((linv Ri) (evalOp Ri vars x1) (evalOp Ri vars x2)) def inductionB {P : (RightSpindle_ShelfSigTerm → Type)} : ((∀ (x1 x2 : RightSpindle_ShelfSigTerm) , ((P x1) → ((P x2) → (P (rinvL x1 x2))))) → ((∀ (x1 x2 : RightSpindle_ShelfSigTerm) , ((P x1) → ((P x2) → (P (linvL x1 x2))))) → (∀ (x : RightSpindle_ShelfSigTerm) , (P x)))) | prinvl plinvl (rinvL x1 x2) := (prinvl _ _ (inductionB prinvl plinvl x1) (inductionB prinvl plinvl x2)) | prinvl plinvl (linvL x1 x2) := (plinvl _ _ (inductionB prinvl plinvl x1) (inductionB prinvl plinvl x2)) def inductionCl {A : Type} {P : ((ClRightSpindle_ShelfSigTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 x2 : (ClRightSpindle_ShelfSigTerm A)) , ((P x1) → ((P x2) → (P (rinvCl x1 x2))))) → ((∀ (x1 x2 : (ClRightSpindle_ShelfSigTerm A)) , ((P x1) → ((P x2) → (P (linvCl x1 x2))))) → (∀ (x : (ClRightSpindle_ShelfSigTerm A)) , (P x))))) | psing prinvcl plinvcl (sing x1) := (psing x1) | psing prinvcl plinvcl (rinvCl x1 x2) := (prinvcl _ _ (inductionCl psing prinvcl plinvcl x1) (inductionCl psing prinvcl plinvcl x2)) | psing prinvcl plinvcl (linvCl x1 x2) := (plinvcl _ _ (inductionCl psing prinvcl plinvcl x1) (inductionCl psing prinvcl plinvcl x2)) def inductionOpB {n : ℕ} {P : ((OpRightSpindle_ShelfSigTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 x2 : (OpRightSpindle_ShelfSigTerm n)) , ((P x1) → ((P x2) → (P (rinvOL x1 x2))))) → ((∀ (x1 x2 : (OpRightSpindle_ShelfSigTerm n)) , ((P x1) → ((P x2) → (P (linvOL x1 x2))))) → (∀ (x : (OpRightSpindle_ShelfSigTerm n)) , (P x))))) | pv prinvol plinvol (v x1) := (pv x1) | pv prinvol plinvol (rinvOL x1 x2) := (prinvol _ _ (inductionOpB pv prinvol plinvol x1) (inductionOpB pv prinvol plinvol x2)) | pv prinvol plinvol (linvOL x1 x2) := (plinvol _ _ (inductionOpB pv prinvol plinvol x1) (inductionOpB pv prinvol plinvol x2)) def inductionOp {n : ℕ} {A : Type} {P : ((OpRightSpindle_ShelfSigTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 x2 : (OpRightSpindle_ShelfSigTerm2 n A)) , ((P x1) → ((P x2) → (P (rinvOL2 x1 x2))))) → ((∀ (x1 x2 : (OpRightSpindle_ShelfSigTerm2 n A)) , ((P x1) → ((P x2) → (P (linvOL2 x1 x2))))) → (∀ (x : (OpRightSpindle_ShelfSigTerm2 n A)) , (P x)))))) | pv2 psing2 prinvol2 plinvol2 (v2 x1) := (pv2 x1) | pv2 psing2 prinvol2 plinvol2 (sing2 x1) := (psing2 x1) | pv2 psing2 prinvol2 plinvol2 (rinvOL2 x1 x2) := (prinvol2 _ _ (inductionOp pv2 psing2 prinvol2 plinvol2 x1) (inductionOp pv2 psing2 prinvol2 plinvol2 x2)) | pv2 psing2 prinvol2 plinvol2 (linvOL2 x1 x2) := (plinvol2 _ _ (inductionOp pv2 psing2 prinvol2 plinvol2 x1) (inductionOp pv2 psing2 prinvol2 plinvol2 x2)) def stageB : (RightSpindle_ShelfSigTerm → (Staged RightSpindle_ShelfSigTerm)) | (rinvL x1 x2) := (stage2 rinvL (codeLift2 rinvL) (stageB x1) (stageB x2)) | (linvL x1 x2) := (stage2 linvL (codeLift2 linvL) (stageB x1) (stageB x2)) def stageCl {A : Type} : ((ClRightSpindle_ShelfSigTerm A) → (Staged (ClRightSpindle_ShelfSigTerm A))) | (sing x1) := (Now (sing x1)) | (rinvCl x1 x2) := (stage2 rinvCl (codeLift2 rinvCl) (stageCl x1) (stageCl x2)) | (linvCl x1 x2) := (stage2 linvCl (codeLift2 linvCl) (stageCl x1) (stageCl x2)) def stageOpB {n : ℕ} : ((OpRightSpindle_ShelfSigTerm n) → (Staged (OpRightSpindle_ShelfSigTerm n))) | (v x1) := (const (code (v x1))) | (rinvOL x1 x2) := (stage2 rinvOL (codeLift2 rinvOL) (stageOpB x1) (stageOpB x2)) | (linvOL x1 x2) := (stage2 linvOL (codeLift2 linvOL) (stageOpB x1) (stageOpB x2)) def stageOp {n : ℕ} {A : Type} : ((OpRightSpindle_ShelfSigTerm2 n A) → (Staged (OpRightSpindle_ShelfSigTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | (rinvOL2 x1 x2) := (stage2 rinvOL2 (codeLift2 rinvOL2) (stageOp x1) (stageOp x2)) | (linvOL2 x1 x2) := (stage2 linvOL2 (codeLift2 linvOL2) (stageOp x1) (stageOp x2)) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (rinvT : ((Repr A) → ((Repr A) → (Repr A)))) (linvT : ((Repr A) → ((Repr A) → (Repr A)))) end RightSpindle_ShelfSig
823b1d4d2b50e4fcc159cd4985bf475077ea6ae7
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/char_zero/infinite.lean
e204f5bbb4c8445b0d55a9934d10cea37a865027
[ "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
587
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.char_zero.defs import data.fintype.card /-! # A characteristic-zero semiring is infinite > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4.-/ open set variables (M : Type*) [add_monoid_with_one M] [char_zero M] @[priority 100] -- see Note [lower instance priority] instance char_zero.infinite : infinite M := infinite.of_injective coe nat.cast_injective
0ba8255d55e331495c9947aaf767974629c08bd4
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/order/succ_pred/relation.lean
5f1e0fc2b37a179ac40d2ebb47f66d3dde590d06
[ "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
6,740
lean
/- Copyright (c) 2022 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import order.succ_pred.basic /-! # Relations on types with a `succ_order` This file contains properties about relations on types with a `succ_order` and their closure operations (like the transitive closure). -/ open set relation succ_order pred_order function section partial_succ variables {α : Type*} [partial_order α] [succ_order α] [is_succ_archimedean α] /-- For `n ≤ m`, `(n, m)` is in the reflexive-transitive closure of `~` if `i ~ succ i` for all `i` between `n` and `m`. -/ lemma refl_trans_gen_of_succ_of_le (r : α → α → Prop) {n m : α} (h : ∀ i ∈ Ico n m, r i (succ i)) (hnm : n ≤ m) : refl_trans_gen r n m := begin revert h, refine succ.rec _ _ hnm, { intros h, exact refl_trans_gen.refl }, { intros m hnm ih h, have : refl_trans_gen r n m := ih (λ i hi, h i ⟨hi.1, hi.2.trans_le $ le_succ m⟩), cases (le_succ m).eq_or_lt with hm hm, { rwa [← hm] }, exact this.tail (h m ⟨hnm, hm⟩) } end /-- For `m ≤ n`, `(n, m)` is in the reflexive-transitive closure of `~` if `succ i ~ i` for all `i` between `n` and `m`. -/ lemma refl_trans_gen_of_succ_of_ge (r : α → α → Prop) {n m : α} (h : ∀ i ∈ Ico m n, r (succ i) i) (hmn : m ≤ n) : refl_trans_gen r n m := by { rw [← refl_trans_gen_swap], exact refl_trans_gen_of_succ_of_le (swap r) h hmn } /-- For `n < m`, `(n, m)` is in the transitive closure of a relation `~` if `i ~ succ i` for all `i` between `n` and `m`. -/ lemma trans_gen_of_succ_of_lt (r : α → α → Prop) {n m : α} (h : ∀ i ∈ Ico n m, r i (succ i)) (hnm : n < m) : trans_gen r n m := (refl_trans_gen_iff_eq_or_trans_gen.mp $ refl_trans_gen_of_succ_of_le r h hnm.le).resolve_left hnm.ne' /-- For `m < n`, `(n, m)` is in the transitive closure of a relation `~` if `succ i ~ i` for all `i` between `n` and `m`. -/ lemma trans_gen_of_succ_of_gt (r : α → α → Prop) {n m : α} (h : ∀ i ∈ Ico m n, r (succ i) i) (hmn : m < n) : trans_gen r n m := (refl_trans_gen_iff_eq_or_trans_gen.mp $ refl_trans_gen_of_succ_of_ge r h hmn.le).resolve_left hmn.ne end partial_succ section linear_succ variables {α : Type*} [linear_order α] [succ_order α] [is_succ_archimedean α] /-- `(n, m)` is in the reflexive-transitive closure of `~` if `i ~ succ i` and `succ i ~ i` for all `i` between `n` and `m`. -/ lemma refl_trans_gen_of_succ (r : α → α → Prop) {n m : α} (h1 : ∀ i ∈ Ico n m, r i (succ i)) (h2 : ∀ i ∈ Ico m n, r (succ i) i) : refl_trans_gen r n m := (le_total n m).elim (refl_trans_gen_of_succ_of_le r h1) $ refl_trans_gen_of_succ_of_ge r h2 /-- For `n ≠ m`,`(n, m)` is in the transitive closure of a relation `~` if `i ~ succ i` and `succ i ~ i` for all `i` between `n` and `m`. -/ lemma trans_gen_of_succ_of_ne (r : α → α → Prop) {n m : α} (h1 : ∀ i ∈ Ico n m, r i (succ i)) (h2 : ∀ i ∈ Ico m n, r (succ i) i) (hnm : n ≠ m) : trans_gen r n m := (refl_trans_gen_iff_eq_or_trans_gen.mp (refl_trans_gen_of_succ r h1 h2)).resolve_left hnm.symm /-- `(n, m)` is in the transitive closure of a reflexive relation `~` if `i ~ succ i` and `succ i ~ i` for all `i` between `n` and `m`. -/ lemma trans_gen_of_succ_of_reflexive (r : α → α → Prop) {n m : α} (hr : reflexive r) (h1 : ∀ i ∈ Ico n m, r i (succ i)) (h2 : ∀ i ∈ Ico m n, r (succ i) i) : trans_gen r n m := begin rcases eq_or_ne m n with rfl|hmn, { exact trans_gen.single (hr m) }, exact trans_gen_of_succ_of_ne r h1 h2 hmn.symm end end linear_succ section partial_pred variables {α : Type*} [partial_order α] [pred_order α] [is_pred_archimedean α] /-- For `m ≤ n`, `(n, m)` is in the reflexive-transitive closure of `~` if `i ~ pred i` for all `i` between `n` and `m`. -/ lemma refl_trans_gen_of_pred_of_ge (r : α → α → Prop) {n m : α} (h : ∀ i ∈ Ioc m n, r i (pred i)) (hnm : m ≤ n) : refl_trans_gen r n m := @refl_trans_gen_of_succ_of_le (order_dual α) _ _ _ r n m (λ x hx, h x ⟨hx.2, hx.1⟩) hnm /-- For `n ≤ m`, `(n, m)` is in the reflexive-transitive closure of `~` if `pred i ~ i` for all `i` between `n` and `m`. -/ lemma refl_trans_gen_of_pred_of_le (r : α → α → Prop) {n m : α} (h : ∀ i ∈ Ioc n m, r (pred i) i) (hmn : n ≤ m) : refl_trans_gen r n m := @refl_trans_gen_of_succ_of_ge (order_dual α) _ _ _ r n m (λ x hx, h x ⟨hx.2, hx.1⟩) hmn /-- For `m < n`, `(n, m)` is in the transitive closure of a relation `~` for `n ≠ m` if `i ~ pred i` for all `i` between `n` and `m`. -/ lemma trans_gen_of_pred_of_gt (r : α → α → Prop) {n m : α} (h : ∀ i ∈ Ioc m n, r i (pred i)) (hnm : m < n) : trans_gen r n m := @trans_gen_of_succ_of_lt (order_dual α) _ _ _ r _ _ (λ x hx, h x ⟨hx.2, hx.1⟩) hnm /-- For `n < m`, `(n, m)` is in the transitive closure of a relation `~` for `n ≠ m` if `pred i ~ i` for all `i` between `n` and `m`. -/ lemma trans_gen_of_pred_of_lt (r : α → α → Prop) {n m : α} (h : ∀ i ∈ Ioc n m, r (pred i) i) (hmn : n < m) : trans_gen r n m := @trans_gen_of_succ_of_gt (order_dual α) _ _ _ r _ _ (λ x hx, h x ⟨hx.2, hx.1⟩) hmn end partial_pred section linear_pred variables {α : Type*} [linear_order α] [pred_order α] [is_pred_archimedean α] /-- `(n, m)` is in the reflexive-transitive closure of `~` if `i ~ pred i` and `pred i ~ i` for all `i` between `n` and `m`. -/ lemma refl_trans_gen_of_pred (r : α → α → Prop) {n m : α} (h1 : ∀ i ∈ Ioc m n, r i (pred i)) (h2 : ∀ i ∈ Ioc n m, r (pred i) i) : refl_trans_gen r n m := @refl_trans_gen_of_succ (order_dual α) _ _ _ r n m (λ x hx, h1 x ⟨hx.2, hx.1⟩) (λ x hx, h2 x ⟨hx.2, hx.1⟩) /-- For `n ≠ m`, `(n, m)` is in the transitive closure of a relation `~` if `i ~ pred i` and `pred i ~ i` for all `i` between `n` and `m`. -/ lemma trans_gen_of_pred_of_ne (r : α → α → Prop) {n m : α} (h1 : ∀ i ∈ Ioc m n, r i (pred i)) (h2 : ∀ i ∈ Ioc n m, r (pred i) i) (hnm : n ≠ m) : trans_gen r n m := @trans_gen_of_succ_of_ne (order_dual α) _ _ _ r n m (λ x hx, h1 x ⟨hx.2, hx.1⟩) (λ x hx, h2 x ⟨hx.2, hx.1⟩) hnm /-- `(n, m)` is in the transitive closure of a reflexive relation `~` if `i ~ pred i` and `pred i ~ i` for all `i` between `n` and `m`. -/ lemma trans_gen_of_pred_of_reflexive (r : α → α → Prop) {n m : α} (hr : reflexive r) (h1 : ∀ i ∈ Ioc m n, r i (pred i)) (h2 : ∀ i ∈ Ioc n m, r (pred i) i) : trans_gen r n m := @trans_gen_of_succ_of_reflexive (order_dual α) _ _ _ r n m hr (λ x hx, h1 x ⟨hx.2, hx.1⟩) (λ x hx, h2 x ⟨hx.2, hx.1⟩) end linear_pred
d53ebb47741dbacd7b44a2c3c247f25c2837e7e3
76ce87faa6bc3c2aa9af5962009e01e04f2a074a
/HW/06_properties.lean
af81e15c7b00203e1a8a879ad134476fcc40b463
[]
no_license
Mnormansell/Discrete-Notes
db423dd9206bbe7080aecb84b4c2d275b758af97
61f13b98be590269fc4822be7b47924a6ddc1261
refs/heads/master
1,585,412,435,424
1,540,919,483,000
1,540,919,483,000
148,684,638
0
0
null
null
null
null
UTF-8
Lean
false
false
5,090
lean
/- HOMEWORK: Read, complete, submit. We've seen that equality is reflexive. That is, everything is equal to itself. It is also symmetric in the sense that if any value, a, is equal to some other value, b, i.e., if a = b, then b is also equal to a, i.e., b = a. What this means is we have an inference rule that both expresses the symmetric property of equality and allows us to compute a proof of b = a from any proof of a = b, no matter what a and b are (no matter what type, T, a and b have, and no matter what values they have of this type). -/ /- (T: Type) (a b: T) (ab: a = b) ------------------------------ (eq.symm) ba: b = a -/ -- Let's see it in action in five lines def a := 1 def b := 2 - 1 lemma ab : a = b := rfl #check ab -- a proof of a = b #check (eq.symm ab) -- a proof of b = a! /- This is a big success. We understand not only the property of being symmetric but that we can use symmetry to derive new proofs from proofs that we already have. In fact, eq.symm is a program that does just this derivation, as we see here! -/ /- Finally we come to the notion that equality is also transitive. That means that for any values, a, b, and c, if a = b, and if b = c, then it must be that consequently a = c as well. {T : Type}, { a b c: T }, ab: a = b, bc: b = c ------------ (eq.trans) ac: a = c That is, if given proofs of a = b and b = c, eq.symm constructs and returns a proof of a = c. Let's see it in action. We've already got variables a and b to work with. We need one more, c. -/ def c := 1 /- We've also already got a proof of a = b. It's easy to generate one of b = c. -/ lemma bc : b = c := rfl /- And now we can apply eq.trans to these two premise-proofs and it will construct and return a proof of the conclusion. The expression that applies eq.trans to these two proofs is (eq.trans ab bc). Now for the fun part! -/ #check eq.trans ab bc /- EXERCISE: First write a textual inference rule, let's call it eq_snart. It says that if T is any type; if a, b, and c are values of this type, T; and you are given proofs of a = b and c = b then you can derive a proof of a = c. -/ -- {T : Type}, -- { a b c: T }, -- ab: a = b, -- cb: c = b ------------ (eq.snart) -- ac: a = c /- EXERCISE: Now "prove" that this rule is valid by implementing it as a program that, when given any argument values of the specified types, returns a proof of the specified type (of the conclusion). Hint: Use eq.trans to construct the proof of a = c. It's first argument will be ab, the proof that a = b. It's second argument has to be a proof of b = c for it to work to derive a proof of a = c; but all we've got is a proof of c = b (in the reverse order). How can we pass a second argument of type b = c to eq.trans, so that it can do its job, when we have at hand is a proof of c = b. Now a major hint: we already have a way to construct a proof of b = c from a proof of c = b. Just use it. Ignore the error message in the following incomplete code. The problem is simply that the definition is incomplete, due to the underscore placeholder. Replace the underscore with your answer. Leave parenthesis around your expression so that it gets evaluated as its own term. -/ def eq_snart { T : Type} { a b c: T } (ab: a = b) (cb: c = b) := eq.trans ab (eq.symm cb) /- EXERCISE: Use lean to implement a new rule that that, from a proof of c = b and a proof of b = a, derives a proof of a = c. Call the proof eq_snart' (why not, it sounds funny). -/ def eq_snart2 { T : Type} { a b c: T } (cb: c = b) (ba: b = a) := eq.trans (eq.symm ba) (eq.symm cb) /- EXERCISE: Use eq_snart rather than eq.trans directly to prove a = c, given proofs of a = b and c = b. -/ lemma cb : c = b := rfl #check cb -- ab has already been declared theorem aeqc : a = c := eq_snart ab cb /- In general, there are many ways to prove a given theorem. Each distinct proof is nevertheless an inhabitant of the type of the proposition that it proves, and each suffices as evidence to justify a truth judgment for the proposition. In cases where one's aim is simply to prove a proposition, the particular proof object that is used doesn't matter. We say that the particular proof is "irrelevant." -/ /- SUMMARY: In this section (1) you first recalled that the equality relation is reflexive, symmetric, and transitive. (2) You saw that in Lean, these are not just abstract ideas; there are also inference rules that you can apply to to arguments of the right types to build proofs of new propositions. (3) You also saw that you can prove your own inference rules by writing programs that implement them! Such programs can use already accepted inference rules (such as eq.refl, eq.symm, eq.trans) in their implementations. Thereafter, the new rules are as good as the old, and can then also be used to construct proofs that might be needed. -/
73eef7507d0b3daa462e12fdb6f53973546b923a
01ae0d022f2e2fefdaaa898938c1ac1fbce3b3ab
/categories/universal/types/default.lean
7bab95315f85e2ffdec10505ad0f8c51b2b1ab8b
[]
no_license
PatrickMassot/lean-category-theory
0f56a83464396a253c28a42dece16c93baf8ad74
ef239978e91f2e1c3b8e88b6e9c64c155dc56c99
refs/heads/master
1,629,739,187,316
1,512,422,659,000
1,512,422,659,000
113,098,786
0
0
null
1,512,424,022,000
1,512,424,022,000
null
UTF-8
Lean
false
false
1,640
lean
import ...types import ..instances open categories.universal open categories.isomorphism namespace categories.types definition {u} Types_has_Products : has_Products CategoryOfTypes.{u} := { product := λ I φ, { product := Π i : I, φ i, projection := λ i x, x i, map := λ Z f z i, f i z, factorisation := ♯, uniqueness := begin tidy, have p := witness x_1, tidy, end } } definition {u} Types_has_Coproducts : has_Coproducts CategoryOfTypes.{u} := { coproduct := λ I φ, { coproduct := Σ i : I, φ i, inclusion := λ i x, ⟨ i, x ⟩ , map := λ Z f p, f p.1 p.2, factorisation := ♯, uniqueness := begin tidy, have p := witness fst, tidy end } } -- PROJECT better automation. definition {u} Types_has_Equalizers : has_Equalizers CategoryOfTypes.{u} := { equalizer := λ α _ f g, { equalizer := { x : α // f x = g x }, inclusion := λ x, x.val, witness := ♯, map := begin tidy, exact k a, tidy, end, factorisation := ♯, uniqueness := ♯ } } attribute [instance] Types_has_Equalizers -- Does Types have coequalizers? Quotients are hard. -- definition {u} relation_from_functions { α β : Type u } ( f g : α → β ) : β → β → Prop := -- λ b0 bn : β, ∃ l : list α, end categories.types
7905a58910d100bc443196b5c36a634a7b3a2d7d
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/measure_theory/constructions/pi.lean
0495faa795381def0baf8ce421dad3adf158b0c1
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
32,886
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import measure_theory.constructions.prod /-! # Product measures In this file we define and prove properties about finite products of measures (and at some point, countable products of measures). ## Main definition * `measure_theory.measure.pi`: The product of finitely many σ-finite measures. Given `μ : Π i : ι, measure (α i)` for `[fintype ι]` it has type `measure (Π i : ι, α i)`. To apply Fubini along some subset of the variables, use `measure_theory.measure.map_pi_equiv_pi_subtype_prod` to reduce to the situation of a product of two measures: this lemma states that the bijection `equiv.pi_equiv_pi_subtype_prod p α` between `(Π i : ι, α i)` and `(Π i : {i // p i}, α i) × (Π i : {i // ¬ p i}, α i)` maps a product measure to a direct product of product measures, to which one can apply the usual Fubini for direct product of measures. ## Implementation Notes We define `measure_theory.outer_measure.pi`, the product of finitely many outer measures, as the maximal outer measure `n` with the property that `n (pi univ s) ≤ ∏ i, m i (s i)`, where `pi univ s` is the product of the sets `{s i | i : ι}`. We then show that this induces a product of measures, called `measure_theory.measure.pi`. For a collection of σ-finite measures `μ` and a collection of measurable sets `s` we show that `measure.pi μ (pi univ s) = ∏ i, m i (s i)`. To do this, we follow the following steps: * We know that there is some ordering on `ι`, given by an element of `[encodable ι]`. * Using this, we have an equivalence `measurable_equiv.pi_measurable_equiv_tprod` between `Π ι, α i` and an iterated product of `α i`, called `list.tprod α l` for some list `l`. * On this iterated product we can easily define a product measure `measure_theory.measure.tprod` by iterating `measure_theory.measure.prod` * Using the previous two steps we construct `measure_theory.measure.pi'` on `Π ι, α i` for encodable `ι`. * We know that `measure_theory.measure.pi'` sends products of sets to products of measures, and since `measure_theory.measure.pi` is the maximal such measure (or at least, it comes from an outer measure which is the maximal such outer measure), we get the same rule for `measure_theory.measure.pi`. ## Tags finitary product measure -/ noncomputable theory open function set measure_theory.outer_measure filter measurable_space encodable open_locale classical big_operators topological_space ennreal variables {ι ι' : Type*} {α : ι → Type*} /-! We start with some measurability properties -/ /-- Boxes formed by π-systems form a π-system. -/ lemma is_pi_system.pi {C : Π i, set (set (α i))} (hC : ∀ i, is_pi_system (C i)) : is_pi_system (pi univ '' pi univ C) := begin rintro _ _ ⟨s₁, hs₁, rfl⟩ ⟨s₂, hs₂, rfl⟩ hst, rw [← pi_inter_distrib] at hst ⊢, rw [univ_pi_nonempty_iff] at hst, exact mem_image_of_mem _ (λ i _, hC i _ _ (hs₁ i (mem_univ i)) (hs₂ i (mem_univ i)) (hst i)) end /-- Boxes form a π-system. -/ lemma is_pi_system_pi [Π i, measurable_space (α i)] : is_pi_system (pi univ '' pi univ (λ i, {s : set (α i) | measurable_set s})) := is_pi_system.pi (λ i, is_pi_system_measurable_set) variables [fintype ι] [fintype ι'] /-- Boxes of countably spanning sets are countably spanning. -/ lemma is_countably_spanning.pi {C : Π i, set (set (α i))} (hC : ∀ i, is_countably_spanning (C i)) : is_countably_spanning (pi univ '' pi univ C) := begin choose s h1s h2s using hC, haveI := fintype.encodable ι, let e : ℕ → (ι → ℕ) := λ n, (decode (ι → ℕ) n).iget, refine ⟨λ n, pi univ (λ i, s i (e n i)), λ n, mem_image_of_mem _ (λ i _, h1s i _), _⟩, simp_rw [(surjective_decode_iget (ι → ℕ)).Union_comp (λ x, pi univ (λ i, s i (x i))), Union_univ_pi s, h2s, pi_univ] end /-- The product of generated σ-algebras is the one generated by boxes, if both generating sets are countably spanning. -/ lemma generate_from_pi_eq {C : Π i, set (set (α i))} (hC : ∀ i, is_countably_spanning (C i)) : @measurable_space.pi _ _ (λ i, generate_from (C i)) = generate_from (pi univ '' pi univ C) := begin haveI := fintype.encodable ι, apply le_antisymm, { refine supr_le _, intro i, rw [comap_generate_from], apply generate_from_le, rintro _ ⟨s, hs, rfl⟩, dsimp, choose t h1t h2t using hC, simp_rw [eval_preimage, ← h2t], rw [← @Union_const _ ℕ _ s], have : (pi univ (update (λ (i' : ι), Union (t i')) i (⋃ (i' : ℕ), s))) = (pi univ (λ k, ⋃ j : ℕ, @update ι (λ i', set (α i')) _ (λ i', t i' j) i s k)), { ext, simp_rw [mem_univ_pi], apply forall_congr, intro i', by_cases (i' = i), { subst h, simp }, { rw [← ne.def] at h, simp [h] }}, rw [this, ← Union_univ_pi], apply measurable_set.Union, intro n, apply measurable_set_generate_from, apply mem_image_of_mem, intros j _, dsimp only, by_cases h: j = i, subst h, rwa [update_same], rw [update_noteq h], apply h1t }, { apply generate_from_le, rintro _ ⟨s, hs, rfl⟩, rw [univ_pi_eq_Inter], apply measurable_set.Inter, intro i, apply measurable_pi_apply, exact measurable_set_generate_from (hs i (mem_univ i)) } end /-- If `C` and `D` generate the σ-algebras on `α` resp. `β`, then rectangles formed by `C` and `D` generate the σ-algebra on `α × β`. -/ lemma generate_from_eq_pi [h : Π i, measurable_space (α i)] {C : Π i, set (set (α i))} (hC : ∀ i, generate_from (C i) = h i) (h2C : ∀ i, is_countably_spanning (C i)) : generate_from (pi univ '' pi univ C) = measurable_space.pi := by rw [← funext hC, generate_from_pi_eq h2C] /-- The product σ-algebra is generated from boxes, i.e. `s.prod t` for sets `s : set α` and `t : set β`. -/ lemma generate_from_pi [Π i, measurable_space (α i)] : generate_from (pi univ '' pi univ (λ i, { s : set (α i) | measurable_set s})) = measurable_space.pi := generate_from_eq_pi (λ i, generate_from_measurable_set) (λ i, is_countably_spanning_measurable_set) namespace measure_theory variables {m : Π i, outer_measure (α i)} /-- An upper bound for the measure in a finite product space. It is defined to by taking the image of the set under all projections, and taking the product of the measures of these images. For measurable boxes it is equal to the correct measure. -/ @[simp] def pi_premeasure (m : Π i, outer_measure (α i)) (s : set (Π i, α i)) : ℝ≥0∞ := ∏ i, m i (eval i '' s) lemma pi_premeasure_pi {s : Π i, set (α i)} (hs : (pi univ s).nonempty) : pi_premeasure m (pi univ s) = ∏ i, m i (s i) := by simp [hs] lemma pi_premeasure_pi' [nonempty ι] {s : Π i, set (α i)} : pi_premeasure m (pi univ s) = ∏ i, m i (s i) := begin cases (pi univ s).eq_empty_or_nonempty with h h, { rcases univ_pi_eq_empty_iff.mp h with ⟨i, hi⟩, have : ∃ i, m i (s i) = 0 := ⟨i, by simp [hi]⟩, simpa [h, finset.card_univ, zero_pow (fintype.card_pos_iff.mpr ‹_›), @eq_comm _ (0 : ℝ≥0∞), finset.prod_eq_zero_iff] }, { simp [h] } end lemma pi_premeasure_pi_mono {s t : set (Π i, α i)} (h : s ⊆ t) : pi_premeasure m s ≤ pi_premeasure m t := finset.prod_le_prod' (λ i _, (m i).mono' (image_subset _ h)) lemma pi_premeasure_pi_eval [nonempty ι] {s : set (Π i, α i)} : pi_premeasure m (pi univ (λ i, eval i '' s)) = pi_premeasure m s := by simp [pi_premeasure_pi'] namespace outer_measure /-- `outer_measure.pi m` is the finite product of the outer measures `{m i | i : ι}`. It is defined to be the maximal outer measure `n` with the property that `n (pi univ s) ≤ ∏ i, m i (s i)`, where `pi univ s` is the product of the sets `{s i | i : ι}`. -/ protected def pi (m : Π i, outer_measure (α i)) : outer_measure (Π i, α i) := bounded_by (pi_premeasure m) lemma pi_pi_le (m : Π i, outer_measure (α i)) (s : Π i, set (α i)) : outer_measure.pi m (pi univ s) ≤ ∏ i, m i (s i) := by { cases (pi univ s).eq_empty_or_nonempty with h h, simp [h], exact (bounded_by_le _).trans_eq (pi_premeasure_pi h) } lemma le_pi {m : Π i, outer_measure (α i)} {n : outer_measure (Π i, α i)} : n ≤ outer_measure.pi m ↔ ∀ (s : Π i, set (α i)), (pi univ s).nonempty → n (pi univ s) ≤ ∏ i, m i (s i) := begin rw [outer_measure.pi, le_bounded_by'], split, { intros h s hs, refine (h _ hs).trans_eq (pi_premeasure_pi hs) }, { intros h s hs, refine le_trans (n.mono $ subset_pi_eval_image univ s) (h _ _), simp [univ_pi_nonempty_iff, hs] } end end outer_measure namespace measure variables [Π i, measurable_space (α i)] (μ : Π i, measure (α i)) section tprod open list variables {δ : Type*} {π : δ → Type*} [∀ x, measurable_space (π x)] /-- A product of measures in `tprod α l`. -/ -- for some reason the equation compiler doesn't like this definition protected def tprod (l : list δ) (μ : Π i, measure (π i)) : measure (tprod π l) := by { induction l with i l ih, exact dirac punit.star, exact (μ i).prod ih } @[simp] lemma tprod_nil (μ : Π i, measure (π i)) : measure.tprod [] μ = dirac punit.star := rfl @[simp] lemma tprod_cons (i : δ) (l : list δ) (μ : Π i, measure (π i)) : measure.tprod (i :: l) μ = (μ i).prod (measure.tprod l μ) := rfl instance sigma_finite_tprod (l : list δ) (μ : Π i, measure (π i)) [∀ i, sigma_finite (μ i)] : sigma_finite (measure.tprod l μ) := begin induction l with i l ih, { rw [tprod_nil], apply_instance }, { rw [tprod_cons], resetI, apply_instance } end lemma tprod_tprod (l : list δ) (μ : Π i, measure (π i)) [∀ i, sigma_finite (μ i)] {s : Π i, set (π i)} (hs : ∀ i, measurable_set (s i)) : measure.tprod l μ (set.tprod l s) = (l.map (λ i, (μ i) (s i))).prod := begin induction l with i l ih, { simp }, simp_rw [tprod_cons, set.tprod, prod_prod (hs i) (measurable_set.tprod l hs), map_cons, prod_cons, ih] end lemma tprod_tprod_le (l : list δ) (μ : Π i, measure (π i)) [∀ i, sigma_finite (μ i)] (s : Π i, set (π i)) : measure.tprod l μ (set.tprod l s) ≤ (l.map (λ i, (μ i) (s i))).prod := begin induction l with i l ih, { simp [le_refl] }, simp_rw [tprod_cons, set.tprod, map_cons, prod_cons], refine (prod_prod_le _ _).trans _, exact ennreal.mul_left_mono ih end end tprod section encodable open list measurable_equiv variables [encodable ι] /-- The product measure on an encodable finite type, defined by mapping `measure.tprod` along the equivalence `measurable_equiv.pi_measurable_equiv_tprod`. The definition `measure_theory.measure.pi` should be used instead of this one. -/ def pi' : measure (Π i, α i) := measure.map (tprod.elim' mem_sorted_univ) (measure.tprod (sorted_univ ι) μ) lemma pi'_pi [∀ i, sigma_finite (μ i)] {s : Π i, set (α i)} (hs : ∀ i, measurable_set (s i)) : pi' μ (pi univ s) = ∏ i, μ i (s i) := begin have hl := λ i : ι, mem_sorted_univ i, have hnd := @sorted_univ_nodup ι _ _, rw [pi', map_apply (measurable_tprod_elim' hl) (measurable_set.pi_fintype (λ i _, hs i)), elim_preimage_pi hnd, tprod_tprod _ μ hs, ← list.prod_to_finset _ hnd], congr' with i, simp [hl] end lemma pi'_pi_le [∀ i, sigma_finite (μ i)] {s : Π i, set (α i)} : pi' μ (pi univ s) ≤ ∏ i, μ i (s i) := begin have hl := λ i : ι, mem_sorted_univ i, have hnd := @sorted_univ_nodup ι _ _, apply ((pi_measurable_equiv_tprod hnd hl).symm.map_apply (pi univ s)).trans_le, dsimp only [pi_measurable_equiv_tprod_symm_apply], rw [elim_preimage_pi hnd], refine (tprod_tprod_le _ _ _).trans_eq _, rw [← list.prod_to_finset _ hnd], congr' with i, simp [hl] end end encodable lemma pi_caratheodory : measurable_space.pi ≤ (outer_measure.pi (λ i, (μ i).to_outer_measure)).caratheodory := begin refine supr_le _, intros i s hs, rw [measurable_space.comap] at hs, rcases hs with ⟨s, hs, rfl⟩, apply bounded_by_caratheodory, intro t, simp_rw [pi_premeasure], refine finset.prod_add_prod_le' (finset.mem_univ i) _ _ _, { simp [image_inter_preimage, image_diff_preimage, (μ i).caratheodory hs, le_refl] }, { rintro j - hj, apply mono', apply image_subset, apply inter_subset_left }, { rintro j - hj, apply mono', apply image_subset, apply diff_subset } end /-- `measure.pi μ` is the finite product of the measures `{μ i | i : ι}`. It is defined to be measure corresponding to `measure_theory.outer_measure.pi`. -/ @[irreducible] protected def pi : measure (Π i, α i) := to_measure (outer_measure.pi (λ i, (μ i).to_outer_measure)) (pi_caratheodory μ) lemma pi_pi [∀ i, sigma_finite (μ i)] (s : Π i, set (α i)) (hs : ∀ i, measurable_set (s i)) : measure.pi μ (pi univ s) = ∏ i, μ i (s i) := begin refine le_antisymm _ _, { rw [measure.pi, to_measure_apply _ _ (measurable_set.pi_fintype (λ i _, hs i))], apply outer_measure.pi_pi_le }, { haveI : encodable ι := fintype.encodable ι, rw [← pi'_pi μ hs], simp_rw [← pi'_pi μ hs, measure.pi, to_measure_apply _ _ (measurable_set.pi_fintype (λ i _, hs i)), ← to_outer_measure_apply], suffices : (pi' μ).to_outer_measure ≤ outer_measure.pi (λ i, (μ i).to_outer_measure), { exact this _ }, clear hs s, rw [outer_measure.le_pi], intros s hs, simp_rw [to_outer_measure_apply], exact pi'_pi_le μ } end lemma pi_ball [∀ i, sigma_finite (μ i)] [∀ i, metric_space (α i)] [∀ i, borel_space (α i)] (x : Π i, α i) {r : ℝ} (hr : 0 < r) : measure.pi μ (metric.ball x r) = ∏ i, μ i (metric.ball (x i) r) := begin rw [ball_pi _ hr, pi_pi], exact λ i, measurable_set_ball end lemma pi_closed_ball [∀ i, sigma_finite (μ i)] [∀ i, metric_space (α i)] [∀ i, borel_space (α i)] (x : Π i, α i) {r : ℝ} (hr : 0 ≤ r) : measure.pi μ (metric.closed_ball x r) = ∏ i, μ i (metric.closed_ball (x i) r) := begin rw [closed_ball_pi _ hr, pi_pi], exact λ i, measurable_set_closed_ball end lemma pi_unique_eq_map {β : Type*} {m : measurable_space β} (μ : measure β) (α : Type*) [unique α] : measure.pi (λ a : α, μ) = map (measurable_equiv.fun_unique α β).symm μ := begin set e := measurable_equiv.fun_unique α β, have : pi_premeasure (λ _ : α, μ.to_outer_measure) = map e.symm μ, { ext1 s, rw [pi_premeasure, fintype.prod_unique, to_outer_measure_apply, e.symm.map_apply], congr' 1, exact e.to_equiv.image_eq_preimage s }, simp only [measure.pi, outer_measure.pi, this, bounded_by_measure, to_outer_measure_to_measure], end lemma map_fun_unique {α β : Type*} [unique α] {m : measurable_space β} (μ : measure β) : map (measurable_equiv.fun_unique α β) (measure.pi $ λ _, μ) = μ := (measurable_equiv.fun_unique α β).map_apply_eq_iff_map_symm_apply_eq.2 (pi_unique_eq_map μ _).symm variable {μ} /-- `measure.pi μ` has finite spanning sets in rectangles of finite spanning sets. -/ def finite_spanning_sets_in.pi {C : Π i, set (set (α i))} (hμ : ∀ i, (μ i).finite_spanning_sets_in (C i)) (hC : ∀ i (s ∈ C i), measurable_set s) : (measure.pi μ).finite_spanning_sets_in (pi univ '' pi univ C) := begin haveI := λ i, (hμ i).sigma_finite, haveI := fintype.encodable ι, let e : ℕ → (ι → ℕ) := λ n, (decode (ι → ℕ) n).iget, refine ⟨λ n, pi univ (λ i, (hμ i).set (e n i)), λ n, _, λ n, _, _⟩, { refine mem_image_of_mem _ (λ i _, (hμ i).set_mem _) }, { simp_rw [pi_pi μ (λ i, (hμ i).set (e n i)) (λ i, hC i _ ((hμ i).set_mem _))], exact ennreal.prod_lt_top (λ i _, ((hμ i).finite _).ne) }, { simp_rw [(surjective_decode_iget (ι → ℕ)).Union_comp (λ x, pi univ (λ i, (hμ i).set (x i))), Union_univ_pi (λ i, (hμ i).set), (hμ _).spanning, pi_univ] } end /-- A measure on a finite product space equals the product measure if they are equal on rectangles with as sides sets that generate the corresponding σ-algebras. -/ lemma pi_eq_generate_from {C : Π i, set (set (α i))} (hC : ∀ i, generate_from (C i) = _inst_3 i) (h2C : ∀ i, is_pi_system (C i)) (h3C : ∀ i, (μ i).finite_spanning_sets_in (C i)) {μν : measure (Π i, α i)} (h₁ : ∀ s : Π i, set (α i), (∀ i, s i ∈ C i) → μν (pi univ s) = ∏ i, μ i (s i)) : measure.pi μ = μν := begin have h4C : ∀ i (s : set (α i)), s ∈ C i → measurable_set s, { intros i s hs, rw [← hC], exact measurable_set_generate_from hs }, refine (finite_spanning_sets_in.pi h3C h4C).ext (generate_from_eq_pi hC (λ i, (h3C i).is_countably_spanning)).symm (is_pi_system.pi h2C) _, rintro _ ⟨s, hs, rfl⟩, rw [mem_univ_pi] at hs, haveI := λ i, (h3C i).sigma_finite, simp_rw [h₁ s hs, pi_pi μ s (λ i, h4C i _ (hs i))] end variables [∀ i, sigma_finite (μ i)] /-- A measure on a finite product space equals the product measure if they are equal on rectangles. -/ lemma pi_eq {μ' : measure (Π i, α i)} (h : ∀ s : Π i, set (α i), (∀ i, measurable_set (s i)) → μ' (pi univ s) = ∏ i, μ i (s i)) : measure.pi μ = μ' := pi_eq_generate_from (λ i, generate_from_measurable_set) (λ i, is_pi_system_measurable_set) (λ i, (μ i).to_finite_spanning_sets_in) h variable (μ) instance pi.sigma_finite : sigma_finite (measure.pi μ) := (finite_spanning_sets_in.pi (λ i, (μ i).to_finite_spanning_sets_in) (λ _ _, id)).sigma_finite lemma {u} pi_fin_two_eq_map {α : fin 2 → Type u} {m : Π i, measurable_space (α i)} (μ : Π i, measure (α i)) [∀ i, sigma_finite (μ i)] : measure.pi μ = map (measurable_equiv.pi_fin_two α).symm ((μ 0).prod (μ 1)) := begin refine pi_eq (λ s hs, _), rw [measurable_equiv.map_apply, fin.prod_univ_succ, fin.prod_univ_succ, fin.prod_univ_zero, mul_one, ← measure.prod_prod (hs _) (hs _)]; [skip, apply_instance], congr' 1, ext ⟨a, b⟩, simp [fin.forall_fin_succ, is_empty.forall_iff] end lemma {u} map_pi_fin_two {α : fin 2 → Type u} {m : Π i, measurable_space (α i)} (μ : Π i, measure (α i)) [∀ i, sigma_finite (μ i)] : map (measurable_equiv.pi_fin_two α) (measure.pi μ) = ((μ 0).prod (μ 1)) := (measurable_equiv.pi_fin_two α).map_apply_eq_iff_map_symm_apply_eq.2 (pi_fin_two_eq_map μ).symm lemma prod_eq_map_fin_two_arrow {α : Type*} {m : measurable_space α} (μ ν : measure α) [sigma_finite μ] [sigma_finite ν] : μ.prod ν = map measurable_equiv.fin_two_arrow (measure.pi ![μ, ν]) := begin haveI : ∀ i, sigma_finite (![μ, ν] i) := fin.forall_fin_two.2 ⟨‹_›, ‹_›⟩, exact (map_pi_fin_two ![μ, ν]).symm end lemma prod_eq_map_fin_two_arrow_same {α : Type*} {m : measurable_space α} (μ : measure α) [sigma_finite μ] : μ.prod μ = map measurable_equiv.fin_two_arrow (measure.pi $ λ _, μ) := by rw [prod_eq_map_fin_two_arrow, matrix.vec_single_eq_const, matrix.vec_cons_const] lemma pi_eval_preimage_null {i : ι} {s : set (α i)} (hs : μ i s = 0) : measure.pi μ (eval i ⁻¹' s) = 0 := begin /- WLOG, `s` is measurable -/ rcases exists_measurable_superset_of_null hs with ⟨t, hst, htm, hμt⟩, suffices : measure.pi μ (eval i ⁻¹' t) = 0, from measure_mono_null (preimage_mono hst) this, clear_dependent s, /- Now rewrite it as `set.pi`, and apply `pi_pi` -/ rw [← univ_pi_update_univ, pi_pi], { apply finset.prod_eq_zero (finset.mem_univ i), simp [hμt] }, { intro j, rcases em (j = i) with rfl | hj; simp * } end lemma pi_hyperplane (i : ι) [has_no_atoms (μ i)] (x : α i) : measure.pi μ {f : Π i, α i | f i = x} = 0 := show measure.pi μ (eval i ⁻¹' {x}) = 0, from pi_eval_preimage_null _ (measure_singleton x) lemma ae_eval_ne (i : ι) [has_no_atoms (μ i)] (x : α i) : ∀ᵐ y : Π i, α i ∂measure.pi μ, y i ≠ x := compl_mem_ae_iff.2 (pi_hyperplane μ i x) variable {μ} lemma tendsto_eval_ae_ae {i : ι} : tendsto (eval i) (measure.pi μ).ae (μ i).ae := λ s hs, pi_eval_preimage_null μ hs -- TODO: should we introduce `filter.pi` and prove some basic facts about it? -- The same combinator appears here and in `nhds_pi` lemma ae_pi_le_infi_comap : (measure.pi μ).ae ≤ ⨅ i, filter.comap (eval i) (μ i).ae := le_infi $ λ i, tendsto_eval_ae_ae.le_comap lemma ae_eq_pi {β : ι → Type*} {f f' : Π i, α i → β i} (h : ∀ i, f i =ᵐ[μ i] f' i) : (λ (x : Π i, α i) i, f i (x i)) =ᵐ[measure.pi μ] (λ x i, f' i (x i)) := (eventually_all.2 (λ i, tendsto_eval_ae_ae.eventually (h i))).mono $ λ x hx, funext hx lemma ae_le_pi {β : ι → Type*} [Π i, preorder (β i)] {f f' : Π i, α i → β i} (h : ∀ i, f i ≤ᵐ[μ i] f' i) : (λ (x : Π i, α i) i, f i (x i)) ≤ᵐ[measure.pi μ] (λ x i, f' i (x i)) := (eventually_all.2 (λ i, tendsto_eval_ae_ae.eventually (h i))).mono $ λ x hx, hx lemma ae_le_set_pi {I : set ι} {s t : Π i, set (α i)} (h : ∀ i ∈ I, s i ≤ᵐ[μ i] t i) : (set.pi I s) ≤ᵐ[measure.pi μ] (set.pi I t) := ((eventually_all_finite (finite.of_fintype I)).2 (λ i hi, tendsto_eval_ae_ae.eventually (h i hi))).mono $ λ x hst hx i hi, hst i hi $ hx i hi lemma ae_eq_set_pi {I : set ι} {s t : Π i, set (α i)} (h : ∀ i ∈ I, s i =ᵐ[μ i] t i) : (set.pi I s) =ᵐ[measure.pi μ] (set.pi I t) := (ae_le_set_pi (λ i hi, (h i hi).le)).antisymm (ae_le_set_pi (λ i hi, (h i hi).symm.le)) section intervals variables {μ} [Π i, partial_order (α i)] [∀ i, has_no_atoms (μ i)] lemma pi_Iio_ae_eq_pi_Iic {s : set ι} {f : Π i, α i} : pi s (λ i, Iio (f i)) =ᵐ[measure.pi μ] pi s (λ i, Iic (f i)) := ae_eq_set_pi $ λ i hi, Iio_ae_eq_Iic lemma pi_Ioi_ae_eq_pi_Ici {s : set ι} {f : Π i, α i} : pi s (λ i, Ioi (f i)) =ᵐ[measure.pi μ] pi s (λ i, Ici (f i)) := ae_eq_set_pi $ λ i hi, Ioi_ae_eq_Ici lemma univ_pi_Iio_ae_eq_Iic {f : Π i, α i} : pi univ (λ i, Iio (f i)) =ᵐ[measure.pi μ] Iic f := by { rw ← pi_univ_Iic, exact pi_Iio_ae_eq_pi_Iic } lemma univ_pi_Ioi_ae_eq_Ici {f : Π i, α i} : pi univ (λ i, Ioi (f i)) =ᵐ[measure.pi μ] Ici f := by { rw ← pi_univ_Ici, exact pi_Ioi_ae_eq_pi_Ici } lemma pi_Ioo_ae_eq_pi_Icc {s : set ι} {f g : Π i, α i} : pi s (λ i, Ioo (f i) (g i)) =ᵐ[measure.pi μ] pi s (λ i, Icc (f i) (g i)) := ae_eq_set_pi $ λ i hi, Ioo_ae_eq_Icc lemma univ_pi_Ioo_ae_eq_Icc {f g : Π i, α i} : pi univ (λ i, Ioo (f i) (g i)) =ᵐ[measure.pi μ] Icc f g := by { rw ← pi_univ_Icc, exact pi_Ioo_ae_eq_pi_Icc } lemma pi_Ioc_ae_eq_pi_Icc {s : set ι} {f g : Π i, α i} : pi s (λ i, Ioc (f i) (g i)) =ᵐ[measure.pi μ] pi s (λ i, Icc (f i) (g i)) := ae_eq_set_pi $ λ i hi, Ioc_ae_eq_Icc lemma univ_pi_Ioc_ae_eq_Icc {f g : Π i, α i} : pi univ (λ i, Ioc (f i) (g i)) =ᵐ[measure.pi μ] Icc f g := by { rw ← pi_univ_Icc, exact pi_Ioc_ae_eq_pi_Icc } lemma pi_Ico_ae_eq_pi_Icc {s : set ι} {f g : Π i, α i} : pi s (λ i, Ico (f i) (g i)) =ᵐ[measure.pi μ] pi s (λ i, Icc (f i) (g i)) := ae_eq_set_pi $ λ i hi, Ico_ae_eq_Icc lemma univ_pi_Ico_ae_eq_Icc {f g : Π i, α i} : pi univ (λ i, Ico (f i) (g i)) =ᵐ[measure.pi μ] Icc f g := by { rw ← pi_univ_Icc, exact pi_Ico_ae_eq_pi_Icc } end intervals /-- If one of the measures `μ i` has no atoms, them `measure.pi µ` has no atoms. The instance below assumes that all `μ i` have no atoms. -/ lemma pi_has_no_atoms (i : ι) [has_no_atoms (μ i)] : has_no_atoms (measure.pi μ) := ⟨λ x, flip measure_mono_null (pi_hyperplane μ i (x i)) (singleton_subset_iff.2 rfl)⟩ instance [h : nonempty ι] [∀ i, has_no_atoms (μ i)] : has_no_atoms (measure.pi μ) := h.elim $ λ i, pi_has_no_atoms i instance [Π i, topological_space (α i)] [∀ i, opens_measurable_space (α i)] [∀ i, is_locally_finite_measure (μ i)] : is_locally_finite_measure (measure.pi μ) := begin refine ⟨λ x, _⟩, choose s hxs ho hμ using λ i, (μ i).exists_is_open_measure_lt_top (x i), refine ⟨pi univ s, set_pi_mem_nhds finite_univ (λ i hi, is_open.mem_nhds (ho i) (hxs i)), _⟩, rw [pi_pi], exacts [ennreal.prod_lt_top (λ i _, (hμ i).ne), λ i, (ho i).measurable_set] end variable (μ) /-- Separating the indices into those that satisfy a predicate `p` and those that don't maps a product measure to a product of product measures. This is useful to apply Fubini to some subset of the variables. The converse is `measure_theory.measure.map_pi_equiv_pi_subtype_prod`. -/ lemma map_pi_equiv_pi_subtype_prod_symm (p : ι → Prop) [decidable_pred p] : map (equiv.pi_equiv_pi_subtype_prod p α).symm (measure.prod (measure.pi (λ i, μ i)) (measure.pi (λ i, μ i))) = measure.pi μ := begin refine (measure.pi_eq (λ s hs, _)).symm, have A : (equiv.pi_equiv_pi_subtype_prod p α).symm ⁻¹' (set.pi set.univ (λ (i : ι), s i)) = set.prod (set.pi set.univ (λ i, s i)) (set.pi set.univ (λ i, s i)), { ext x, simp only [equiv.pi_equiv_pi_subtype_prod_symm_apply, mem_prod, mem_univ_pi, mem_preimage, subtype.forall], split, { exact λ h, ⟨λ i hi, by simpa [dif_pos hi] using h i, λ i hi, by simpa [dif_neg hi] using h i⟩ }, { assume h i, by_cases hi : p i, { simpa only [dif_pos hi] using h.1 i hi }, {simpa only [dif_neg hi] using h.2 i hi } } }, rw [measure.map_apply (measurable_pi_equiv_pi_subtype_prod_symm _ p) (measurable_set.univ_pi_fintype hs), A, measure.prod_prod, pi_pi, pi_pi, ← fintype.prod_subtype_mul_prod_subtype p (λ i, μ i (s i))], { exact λ i, hs i.1 }, { exact λ i, hs i.1 }, { exact measurable_set.univ_pi_fintype (λ i, hs i.1) }, { exact measurable_set.univ_pi_fintype (λ i, hs i.1) }, end lemma map_pi_equiv_pi_subtype_prod (p : ι → Prop) [decidable_pred p] : map (equiv.pi_equiv_pi_subtype_prod p α) (measure.pi μ) = measure.prod (measure.pi (λ i, μ i)) (measure.pi (λ i, μ i)) := begin rw [← map_pi_equiv_pi_subtype_prod_symm μ p, measure.map_map (measurable_pi_equiv_pi_subtype_prod _ p) (measurable_pi_equiv_pi_subtype_prod_symm _ p)], simp only [equiv.self_comp_symm, map_id] end end measure instance measure_space.pi [Π i, measure_space (α i)] : measure_space (Π i, α i) := ⟨measure.pi (λ i, volume)⟩ lemma volume_pi [Π i, measure_space (α i)] : (volume : measure (Π i, α i)) = measure.pi (λ i, volume) := rfl lemma volume_pi_pi [Π i, measure_space (α i)] [∀ i, sigma_finite (volume : measure (α i))] (s : Π i, set (α i)) (hs : ∀ i, measurable_set (s i)) : volume (pi univ s) = ∏ i, volume (s i) := measure.pi_pi (λ i, volume) s hs lemma volume_pi_ball [Π i, measure_space (α i)] [∀ i, sigma_finite (volume : measure (α i))] [∀ i, metric_space (α i)] [∀ i, borel_space (α i)] (x : Π i, α i) {r : ℝ} (hr : 0 < r) : volume (metric.ball x r) = ∏ i, volume (metric.ball (x i) r) := measure.pi_ball _ _ hr lemma volume_pi_closed_ball [Π i, measure_space (α i)] [∀ i, sigma_finite (volume : measure (α i))] [∀ i, metric_space (α i)] [∀ i, borel_space (α i)] (x : Π i, α i) {r : ℝ} (hr : 0 ≤ r) : volume (metric.closed_ball x r) = ∏ i, volume (metric.closed_ball (x i) r) := measure.pi_closed_ball _ _ hr section fun_unique /-! ### Integral over `ι → α` with `[unique ι]` In this section we prove some lemmas that relate integrals over `ι → β`, where `ι` is a type with unique element (e.g., `unit` or `fin 1`) and integrals over `β`. -/ variables {β E : Type*} [normed_group E] [normed_space ℝ E] [measurable_space E] [topological_space.second_countable_topology E] [borel_space E] [complete_space E] lemma integral_fun_unique_pi (ι) [unique ι] {m : measurable_space β} (μ : measure β) (f : (ι → β) → E) : ∫ y, f y ∂(measure.pi (λ _, μ)) = ∫ x, f (λ _, x) ∂μ := by rw [measure.pi_unique_eq_map μ ι, integral_map_equiv]; refl lemma integral_fun_unique_pi' (ι : Type*) [unique ι] {m : measurable_space β} (μ : measure β) (f : β → E) : ∫ y : ι → β, f (y (default ι)) ∂(measure.pi (λ _, μ)) = ∫ x, f x ∂μ := integral_fun_unique_pi ι μ _ lemma integral_fun_unique (ι : Type*) [unique ι] [measure_space β] (f : (ι → β) → E) : ∫ y, f y = ∫ x, f (λ _, x) := integral_fun_unique_pi ι volume f lemma integral_fun_unique' (ι : Type*) [unique ι] [measure_space β] (f : β → E) : ∫ y : ι → β, f (y (default ι)) = ∫ x, f x := integral_fun_unique_pi' ι volume f lemma set_integral_fun_unique_pi (ι : Type*) [unique ι] {m : measurable_space β} (μ : measure β) (f : (ι → β) → E) (s : set (ι → β)) : ∫ y in s, f y ∂(measure.pi (λ _, μ)) = ∫ x in const ι ⁻¹' s, f (λ _, x) ∂μ := by rw [measure.pi_unique_eq_map μ ι, set_integral_map_equiv]; refl lemma set_integral_fun_unique_pi' (ι : Type*) [unique ι] {m : measurable_space β} (μ : measure β) (f : β → E) (s : set β) : ∫ y : ι → β in function.eval (default ι) ⁻¹' s, f (y (default ι)) ∂(measure.pi (λ _, μ)) = ∫ x in s, f x ∂μ := by erw [set_integral_fun_unique_pi, (equiv.fun_unique ι β).symm_preimage_preimage] lemma set_integral_fun_unique (ι : Type*) [unique ι] [measure_space β] (f : (ι → β) → E) (s : set (ι → β)) : ∫ y in s, f y = ∫ x in const ι ⁻¹' s, f (λ _, x) := by convert set_integral_fun_unique_pi ι volume f s lemma set_integral_fun_unique' (ι : Type*) [unique ι] [measure_space β] (f : β → E) (s : set β) : ∫ y : ι → β in @function.eval ι (λ _, β) (default ι) ⁻¹' s, f (y (default ι)) = ∫ x in s, f x := by convert set_integral_fun_unique_pi' ι volume f s end fun_unique section fin_two_arrow variables {β E : Type*} [normed_group E] [normed_space ℝ E] [measurable_space E] [topological_space.second_countable_topology E] [borel_space E] [complete_space E] lemma integral_fin_two_arrow_pi {m : measurable_space β} (μ ν : measure β) [sigma_finite μ] [sigma_finite ν] (f : (fin 2 → β) → E) : ∫ y, f y ∂(measure.pi ![μ, ν]) = ∫ x, f ![x.1, x.2] ∂(μ.prod ν) := begin haveI : ∀ i, sigma_finite (![μ, ν] i) := fin.forall_fin_two.2 ⟨‹_›, ‹_›⟩, rw [measure.pi_fin_two_eq_map, integral_map_equiv], refl end lemma integral_fin_two_arrow_pi' {m : measurable_space β} (μ ν : measure β) [sigma_finite μ] [sigma_finite ν] (f : β × β → E) : ∫ y : fin 2 → β, f (y 0, y 1) ∂(measure.pi ![μ, ν]) = ∫ x, f x ∂(μ.prod ν) := by { rw [measure.prod_eq_map_fin_two_arrow, integral_map_equiv], refl } lemma integral_fin_two_arrow [measure_space β] [sigma_finite (volume : measure β)] (f : (fin 2 → β) → E) : ∫ y, f y = ∫ x : β × β, f ![x.1, x.2] := by rw [volume_pi, measure.volume_eq_prod, ← integral_fin_two_arrow_pi, matrix.vec_single_eq_const, matrix.vec_cons_const] lemma integral_fin_two_arrow' [measure_space β] [sigma_finite (volume : measure β)] (f : β × β → E) : ∫ y : fin 2 → β, f (y 0, y 1) = ∫ x, f x := by rw [volume_pi, measure.volume_eq_prod, ← integral_fin_two_arrow_pi', matrix.vec_single_eq_const, matrix.vec_cons_const] lemma set_integral_fin_two_arrow_pi {m : measurable_space β} (μ ν : measure β) [sigma_finite μ] [sigma_finite ν] (f : (fin 2 → β) → E) (s : set (fin 2 → β)) : ∫ y in s, f y ∂(measure.pi ![μ, ν]) = ∫ x : β × β in (fin_two_arrow_equiv β).symm ⁻¹' s, f ![x.1, x.2] ∂(μ.prod ν) := begin haveI : ∀ i, sigma_finite (![μ, ν] i) := fin.forall_fin_two.2 ⟨‹_›, ‹_›⟩, rw [measure.pi_fin_two_eq_map, set_integral_map_equiv], refl end lemma set_integral_fin_two_arrow_pi' {m : measurable_space β} (μ ν : measure β) [sigma_finite μ] [sigma_finite ν] (f : β × β → E) (s : set (β × β)) : ∫ y : fin 2 → β in fin_two_arrow_equiv β ⁻¹' s, f (y 0, y 1) ∂(measure.pi ![μ, ν]) = ∫ x in s, f x ∂(μ.prod ν) := by { rw [set_integral_fin_two_arrow_pi, equiv.symm_preimage_preimage], simp } lemma set_integral_fin_two_arrow [measure_space β] [sigma_finite (volume : measure β)] (f : (fin 2 → β) → E) (s : set (fin 2 → β)) : ∫ y in s, f y = ∫ x in (fin_two_arrow_equiv β).symm ⁻¹' s, f ![x.1, x.2] := by rw [measure.volume_eq_prod, ← set_integral_fin_two_arrow_pi, volume_pi, matrix.vec_single_eq_const, matrix.vec_cons_const] lemma set_integral_fin_two_arrow' [measure_space β] [sigma_finite (volume : measure β)] (f : β × β → E) (s : set (β × β)) : ∫ y : fin 2 → β in fin_two_arrow_equiv β ⁻¹' s, f (y 0, y 1) = ∫ x in s, f x := by rw [measure.volume_eq_prod, ← set_integral_fin_two_arrow_pi', volume_pi, matrix.vec_single_eq_const, matrix.vec_cons_const] end fin_two_arrow end measure_theory
3ee0cf79bb62bb58b0650d2e46489ed07ae1d2ed
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/run/meta2.lean
50dca8fb081be60f2987b39f953362fe91141984
[ "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
18,281
lean
import Lean.Meta open Lean open Lean.Meta -- set_option trace.Meta true --set_option trace.Meta.isDefEq.step false -- set_option trace.Meta.isDefEq.delta false set_option trace.Meta.debug true def print (msg : MessageData) : MetaM Unit := do trace[Meta.debug] msg def checkM (x : MetaM Bool) : MetaM Unit := unless (← x) do throwError "check failed" def getAssignment (m : Expr) : MetaM Expr := do let v? ← getExprMVarAssignment? m.mvarId!; (match v? with | some v => pure v | none => throwError "metavariable is not assigned") def nat := mkConst `Nat def boolE := mkConst `Bool def succ := mkConst `Nat.succ def zero := mkConst `Nat.zero def add := mkConst `Nat.add def io := mkConst `IO def type := mkSort levelOne def boolFalse := mkConst `Bool.false def boolTrue := mkConst `Bool.true def tst1 : MetaM Unit := do print "----- tst1 -----"; let mvar <- mkFreshExprMVar nat; checkM $ isExprDefEq mvar (mkNatLit 10); checkM $ isExprDefEq mvar (mkNatLit 10); pure () #eval tst1 def tst2 : MetaM Unit := do print "----- tst2 -----"; let mvar <- mkFreshExprMVar nat; checkM $ isExprDefEq (mkApp succ mvar) (mkApp succ (mkNatLit 10)); checkM $ isExprDefEq mvar (mkNatLit 10); pure () #eval tst2 def tst3 : MetaM Unit := do print "----- tst3 -----"; let t := mkLambda `x BinderInfo.default nat $ mkBVar 0; let mvar ← mkFreshExprMVar (mkForall `x BinderInfo.default nat nat); lambdaTelescope t fun xs _ => do let x := xs[0]; checkM $ isExprDefEq (mkApp mvar x) (mkAppN add #[x, mkAppN add #[mkNatLit 10, x]]); pure (); let v ← getAssignment mvar; print v; pure () #eval tst3 def tst4 : MetaM Unit := do print "----- tst4 -----"; let t := mkLambda `x BinderInfo.default nat $ mkBVar 0; lambdaTelescope t fun xs _ => do let x := xs[0]; let mvar ← mkFreshExprMVar (mkForall `x BinderInfo.default nat nat); -- the following `isExprDefEq` fails because `x` is also in the context of `mvar` checkM $ not <$> isExprDefEq (mkApp mvar x) (mkAppN add #[x, mkAppN add #[mkNatLit 10, x]]); checkM $ approxDefEq $ isExprDefEq (mkApp mvar x) (mkAppN add #[x, mkAppN add #[mkNatLit 10, x]]); let v ← getAssignment mvar; print v; pure (); pure () #eval tst4 def mkAppC (c : Name) (xs : Array Expr) : MetaM Expr := do let r ← mkAppM c xs; check r; pure r def mkProd (a b : Expr) : MetaM Expr := mkAppC `Prod #[a, b] def mkPair (a b : Expr) : MetaM Expr := mkAppC `Prod.mk #[a, b] def mkFst (s : Expr) : MetaM Expr := mkAppC `Prod.fst #[s] def mkSnd (s : Expr) : MetaM Expr := mkAppC `Prod.snd #[s] def tst5 : MetaM Unit := do print "----- tst5 -----"; let p₁ ← mkPair (mkNatLit 1) (mkNatLit 2); let x ← mkFst p₁; print x; let v ← whnf x; print v; let v ← withTransparency TransparencyMode.reducible $ whnf x; print v; let x ← mkId x; print x; let prod ← mkProd nat nat; let m ← mkFreshExprMVar prod; let y ← mkFst m; checkM $ isExprDefEq y x; print y; let x ← mkProjection p₁ `fst; print x; let y ← mkProjection p₁ `snd; print y #eval tst5 def tst6 : MetaM Unit := do print "----- tst6 -----"; withLocalDeclD `x nat $ fun x => do let m ← mkFreshExprMVar nat; let t := mkAppN add #[m, mkNatLit 4]; let s := mkAppN add #[x, mkNatLit 3]; checkM $ not <$> isExprDefEq t s; let s := mkAppN add #[x, mkNatLit 6]; checkM $ isExprDefEq t s; let v ← getAssignment m; checkM $ pure $ v == (← mkAdd x (mkNatLit 2)) print v; let m ← mkFreshExprMVar nat; let t := mkAppN add #[m, mkNatLit 4]; let s := mkNatLit 3; checkM $ not <$> isExprDefEq t s; let s := mkNatLit 10; checkM $ isExprDefEq t s; let v ← getAssignment m; checkM $ pure $ v == mkNatLit 6; print v; pure () #eval tst6 def tst7 : MetaM Unit := do print "----- tst7 -----"; withLocalDeclD `x type $ fun x => do let m1 ← mkFreshExprMVar (← mkArrow type type); let m2 ← mkFreshExprMVar type; let t := mkApp io x; -- we need to use foApprox to solve `?m1 ?m2 =?= IO x` checkM $ not <$> isDefEq (mkApp m1 m2) t; checkM $ approxDefEq $ isDefEq (mkApp m1 m2) t; let v ← getAssignment m1; checkM $ pure $ v == io; let v ← getAssignment m2; checkM $ pure $ v == x; pure () #eval tst7 def tst9 : MetaM Unit := do print "----- tst9 -----"; let env ← getEnv; print (toString (← isReducible `Prod.fst)) print (toString (← isReducible `Add.add)) pure () #eval tst9 def tst10 : MetaM Unit := do print "----- tst10 -----"; let t ← withLocalDeclD `x nat $ fun x => do { let b := mkAppN add #[x, mkAppN add #[mkNatLit 2, mkNatLit 3]]; mkLambdaFVars #[x] b }; print t; let t ← reduce t; print t; pure () #eval tst10 def tst11 : MetaM Unit := do print "----- tst11 -----"; checkM $ isType nat; checkM $ isType (← mkArrow nat nat); checkM $ not <$> isType add; checkM $ not <$> isType (mkNatLit 1); withLocalDeclD `x nat fun x => do checkM $ not <$> isType x; checkM $ not <$> (mkLambdaFVars #[x] x >>= isType); checkM $ not <$> (mkLambdaFVars #[x] nat >>= isType); let t ← mkEq x (mkNatLit 0); let t ← mkForallFVars #[x] t (usedOnly := true); print t; checkM $ isType t; pure (); pure () #eval tst11 def tst12 : MetaM Unit := do print "----- tst12 -----"; withLocalDeclD `x nat $ fun x => do let t ← mkEqRefl x >>= mkLambdaFVars #[x]; print t; let type ← inferType t; print type; isProofQuick t >>= fun b => print (toString b); isProofQuick nat >>= fun b => print (toString b); isProofQuick type >>= fun b => print (toString b); pure (); pure () #eval tst12 def tst13 : MetaM Unit := do print "----- tst13 -----"; let m₁ ← mkFreshExprMVar (← mkArrow type type); let m₂ ← mkFreshExprMVar (mkApp m₁ nat); let t ← mkId m₂; print t; let r ← abstractMVars t; print r.expr; let (_, _, e) ← openAbstractMVarsResult r; print e; pure () def mkDecEq (type : Expr) : MetaM Expr := mkAppC `DecidableEq #[type] def mkStateM (σ : Expr) : MetaM Expr := mkAppC `StateM #[σ] def mkMonad (m : Expr) : MetaM Expr := mkAppC `Monad #[m] def mkMonadState (σ m : Expr) : MetaM Expr := mkAppC `MonadState #[σ, m] def mkAdd (a : Expr) : MetaM Expr := mkAppC `Add #[a] def mkToString (a : Expr) : MetaM Expr := mkAppC `ToString #[a] def tst14 : MetaM Unit := do print "----- tst14 -----"; let stateM ← mkStateM nat; print stateM; let monad ← mkMonad stateM; let globalInsts ← getGlobalInstancesIndex; let insts ← globalInsts.getUnify monad; print (insts.map (·.val)); pure () #eval tst14 def tst15 : MetaM Unit := do print "----- tst15 -----"; let inst ← _root_.mkAdd nat; let r ← synthInstance inst; print r; pure () #eval tst15 def tst16 : MetaM Unit := do print "----- tst16 -----"; let prod ← mkProd nat nat; let inst ← mkToString prod; print inst; let r ← synthInstance inst; print r; pure () #eval tst16 def tst17 : MetaM Unit := do print "----- tst17 -----"; let prod ← mkProd nat nat; let prod ← mkProd boolE prod; let inst ← mkToString prod; print inst; let r ← synthInstance inst; print r; pure () #eval tst17 def tst18 : MetaM Unit := do print "----- tst18 -----"; let decEqNat ← mkDecEq nat; let r ← synthInstance decEqNat; print r; pure () #eval tst18 def tst19 : MetaM Unit := do print "----- tst19 -----"; let stateM ← mkStateM nat; print stateM; let monad ← mkMonad stateM; print monad; let r ← synthInstance monad; print r; pure () #eval tst19 def tst20 : MetaM Unit := do print "----- tst20 -----"; let stateM ← mkStateM nat; print stateM; let monadState ← mkMonadState nat stateM; print monadState; let r ← synthInstance monadState; print r; pure () #eval tst20 def tst21 : MetaM Unit := do print "----- tst21 -----"; withLocalDeclD `x nat $ fun x => do withLocalDeclD `y nat $ fun y => do withLocalDeclD `z nat $ fun z => do let eq₁ ← mkEq x y; let eq₂ ← mkEq y z; withLocalDeclD `h₁ eq₁ $ fun h₁ => do withLocalDeclD `h₂ eq₂ $ fun h₂ => do let h ← mkEqTrans h₁ h₂; let h ← mkEqSymm h; let h ← mkCongrArg succ h; let h₂ ← mkEqRefl succ; let h ← mkCongr h₂ h; let t ← inferType h; check h; print h; print t; let h ← mkCongrFun h₂ x; let t ← inferType h; check h; print t; pure () #eval tst21 def tst22 : MetaM Unit := do print "----- tst22 -----"; withLocalDeclD `x nat $ fun x => do withLocalDeclD `y nat $ fun y => do let t ← mkAppC `Add.add #[x, y]; print t; let t ← mkAppC `Add.add #[y, x]; print t; let t ← mkAppC `ToString.toString #[x]; print t; pure () #eval tst22 def test1 : Nat := (fun x y => x + y) 0 1 def tst23 : MetaM Unit := do print "----- tst23 -----"; let cinfo ← getConstInfo `test1; let v := cinfo.value?.get!; print v; print v.headBeta #eval tst23 def tst26 : MetaM Unit := do print "----- tst26 -----"; let m1 ← mkFreshExprMVar (← mkArrow nat nat); let m2 ← mkFreshExprMVar nat; let m3 ← mkFreshExprMVar nat; checkM $ approxDefEq $ isDefEq (mkApp m1 m2) m3; checkM $ do { let b ← isExprMVarAssigned $ m1.mvarId!; pure (!b) }; checkM $ isExprMVarAssigned $ m3.mvarId!; pure () #eval tst26 section set_option trace.Meta.isDefEq.step true set_option trace.Meta.isDefEq.delta true set_option trace.Meta.isDefEq.assign true def tst27 : MetaM Unit := do print "----- tst27 -----"; let m ← mkFreshExprMVar nat; checkM $ isDefEq (mkNatLit 1) (mkApp (mkConst `Nat.succ) m); pure () #eval tst27 end def tst28 : MetaM Unit := do print "----- tst28 -----"; withLocalDeclD `x nat $ fun x => withLocalDeclD `y nat $ fun y => withLocalDeclD `z nat $ fun z => do let t1 ← mkAppM `Add.add #[x, y]; let t1 ← mkAppM `Add.add #[x, t1]; let t1 ← mkAppM `Add.add #[t1, t1]; let t2 ← mkAppM `Add.add #[z, y]; let t3 ← mkAppM `Eq #[t2, t1]; let t3 ← mkForallFVars #[z] t3; let m ← mkFreshExprMVar nat; let p ← mkAppM `Add.add #[x, m]; print t3; let r ← kabstract t3 p; print r; let p ← mkAppM `Add.add #[x, y]; let r ← kabstract t3 p; print r; pure () #eval tst28 def norm : Level → Level := @Lean.Level.normalize def tst29 : MetaM Unit := do print "----- tst29 -----"; let u := mkLevelParam `u; let v := mkLevelParam `v; let u1 := mkLevelSucc u; let m := mkLevelMax levelOne u1; print (norm m); checkM $ pure $ norm m == u1; let m := mkLevelMax u1 levelOne; print (norm m); checkM $ pure $ norm m == u1; let m := mkLevelMax (mkLevelMax levelOne (mkLevelSucc u1)) (mkLevelSucc levelOne); checkM $ pure $ norm m == mkLevelSucc u1; print m; print (norm m); let m := mkLevelMax (mkLevelMax (mkLevelSucc (mkLevelSucc u1)) (mkLevelSucc u1)) (mkLevelSucc levelOne); print m; print (norm m); checkM $ pure $ norm m == mkLevelSucc (mkLevelSucc u1); let m := mkLevelMax (mkLevelMax (mkLevelSucc v) (mkLevelSucc u1)) (mkLevelSucc levelOne); print m; print (norm m); pure () #eval tst29 def tst30 : MetaM Unit := do print "----- tst30 -----"; let m1 ← mkFreshExprMVar nat; let m2 ← mkFreshExprMVar (← mkArrow nat nat); withLocalDeclD `x nat $ fun x => do let t := mkApp succ $ mkApp m2 x; print t; checkM $ approxDefEq $ isDefEq m1 t; let r ← instantiateMVars m1; print r; let r ← instantiateMVars m2; print r; pure () #eval tst30 def tst31 : MetaM Unit := do print "----- tst31 -----"; let m ← mkFreshExprMVar nat; let t := mkLet `x nat zero m; print t; checkM $ isDefEq t m; pure () def tst32 : MetaM Unit := do print "----- tst32 -----"; withLocalDeclD `a nat $ fun a => do withLocalDeclD `b nat $ fun b => do let aeqb ← mkEq a b; withLocalDeclD `h2 aeqb $ fun h2 => do let t ← mkEq (mkApp2 add a a) a; print t; let motive := mkLambda `x BinderInfo.default nat (mkApp3 (mkConst `Eq [levelOne]) nat (mkApp2 add a (mkBVar 0)) a); withLocalDeclD `h1 t $ fun h1 => do let r ← mkEqNDRec motive h1 h2; print r; let rType ← inferType r >>= whnf; print rType; check r; pure () #eval tst32 def tst33 : MetaM Unit := do print "----- tst33 -----"; withLocalDeclD `a nat $ fun a => do withLocalDeclD `b nat $ fun b => do let aeqb ← mkEq a b; withLocalDeclD `h2 aeqb $ fun h2 => do let t ← mkEq (mkApp2 add a a) a; let motive := mkLambda `x BinderInfo.default nat $ mkLambda `h BinderInfo.default (mkApp3 (mkConst `Eq [levelOne]) nat a (mkBVar 0)) $ (mkApp3 (mkConst `Eq [levelOne]) nat (mkApp2 add a (mkBVar 1)) a); withLocalDeclD `h1 t $ fun h1 => do let r ← mkEqRec motive h1 h2; print r; let rType ← inferType r >>= whnf; print rType; check r; pure () #eval tst33 def tst34 : MetaM Unit := do print "----- tst34 -----"; let type := mkSort levelOne; withLocalDeclD `α type $ fun α => do let m ← mkFreshExprMVar type; let t ← mkLambdaFVars #[α] (← mkArrow m m); print t; pure () #eval tst34 def tst35 : MetaM Unit := do print "----- tst35 -----"; let type := mkSort levelOne; withLocalDeclD `α type $ fun α => do let m1 ← mkFreshExprMVar type; let m2 ← mkFreshExprMVar (← mkArrow nat type); let v := mkLambda `x BinderInfo.default nat m1; assignExprMVar m2.mvarId! v; let w := mkApp m2 zero; let t1 ← mkLambdaFVars #[α] (← mkArrow w w); print t1; let m3 ← mkFreshExprMVar type; let t2 ← mkLambdaFVars #[α] (← mkArrow (mkBVar 0) (mkBVar 1)); print t2; checkM $ isDefEq t1 t2; pure () #eval tst35 #check @Id def tst36 : MetaM Unit := do print "----- tst36 -----"; let type := mkSort levelOne; let m1 ← mkFreshExprMVar (← mkArrow type type); withLocalDeclD `α type $ fun α => do let m2 ← mkFreshExprMVar type; let t ← mkAppM `Id #[m2]; checkM $ approxDefEq $ isDefEq (mkApp m1 α) t; checkM $ approxDefEq $ isDefEq m1 (mkConst `Id [levelZero]); pure () #eval tst36 def tst37 : MetaM Unit := do print "----- tst37 -----"; let m1 ← mkFreshExprMVar (←mkArrow nat (←mkArrow type type)); let m2 ← mkFreshExprMVar (←mkArrow nat type); withLocalDeclD `v nat $ fun v => do let lhs := mkApp2 m1 v (mkApp m2 v); let rhs ← mkAppM `StateM #[nat, nat]; print lhs; print rhs; checkM $ approxDefEq $ isDefEq lhs rhs; pure () #eval tst37 def tst38 : MetaM Unit := do print "----- tst38 -----"; let m1 ← mkFreshExprMVar nat; withLocalDeclD `x nat $ fun x => do let m2 ← mkFreshExprMVar type; withLocalDeclD `y m2 $ fun y => do let m3 ← mkFreshExprMVar (←mkArrow m2 nat); let rhs := mkApp m3 y; checkM $ approxDefEq $ isDefEq m2 nat; print m2; checkM $ getAssignment m2 >>= fun v => pure $ v == nat; checkM $ approxDefEq $ isDefEq m1 rhs; print m2; checkM $ getAssignment m2 >>= fun v => pure $ v == nat; pure () set_option pp.all true set_option trace.Meta.isDefEq.step true set_option trace.Meta.isDefEq.delta true set_option trace.Meta.isDefEq.assign true #eval tst38 def tst39 : MetaM Unit := do print "----- tst39 -----"; withLocalDeclD `α type $ fun α => withLocalDeclD `β type $ fun β => do let p ← mkProd α β; let t ← mkForallFVars #[α, β] p; print t; let e ← instantiateForall t #[nat, boolE]; print e; pure () #eval tst39 def tst40 : MetaM Unit := do print "----- tst40 -----"; withLocalDeclD `α type $ fun α => withLocalDeclD `β type $ fun β => withLocalDeclD `a α $ fun a => withLocalDeclD `b β $ fun b => do let p ← mkProd α β; let t1 ← mkForallFVars #[α, β] p; let t2 ← mkForallFVars #[α, β, a, b] p; print t1; print $ toString $ t1.bindingBody!.hasLooseBVarInExplicitDomain 0 false; print $ toString $ t1.bindingBody!.hasLooseBVarInExplicitDomain 0 true; print $ toString $ t2.bindingBody!.hasLooseBVarInExplicitDomain 0 false; print $ t1.inferImplicit 2 false; checkM $ pure $ ((t1.inferImplicit 2 false).bindingInfo! == BinderInfo.default); checkM $ pure $ ((t1.inferImplicit 2 false).bindingBody!.bindingInfo! == BinderInfo.default); print $ t1.inferImplicit 2 true; checkM $ pure $ ((t1.inferImplicit 2 true).bindingInfo! == BinderInfo.implicit); checkM $ pure $ ((t1.inferImplicit 2 true).bindingBody!.bindingInfo! == BinderInfo.implicit); print t2; print $ t2.inferImplicit 2 false; checkM $ pure $ ((t2.inferImplicit 2 false).bindingInfo! == BinderInfo.implicit); checkM $ pure $ ((t2.inferImplicit 2 false).bindingBody!.bindingInfo! == BinderInfo.implicit); print $ t2.inferImplicit 1 false; checkM $ pure $ ((t2.inferImplicit 1 false).bindingInfo! == BinderInfo.implicit); checkM $ pure $ ((t2.inferImplicit 1 false).bindingBody!.bindingInfo! == BinderInfo.default); pure () #eval tst40 universe u structure A (α : Type u) := (x y : α) structure B (α : Type u) := (z : α) structure C (α : Type u) extends A α, B α := (w : Bool) def mkA (x y : Expr) : MetaM Expr := mkAppC `A.mk #[x, y] def mkB (z : Expr) : MetaM Expr := mkAppC `B.mk #[z] def mkC (x y z w : Expr) : MetaM Expr := do let a ← mkA x y; let b ← mkB z; mkAppC `C.mk #[a, b, w] def tst41 : MetaM Unit := do print "----- tst41 -----"; let c ← mkC (mkNatLit 1) (mkNatLit 2) (mkNatLit 3) boolTrue; print c; let x ← mkProjection c `x; check x; print x; let y ← mkProjection c `y; check y; print y; let z ← mkProjection c `z; check z; print z; let w ← mkProjection c `w; check w; print w; pure () set_option trace.Meta.isDefEq.step false set_option trace.Meta.isDefEq.delta false set_option trace.Meta.isDefEq.assign false #eval tst41 set_option pp.all false def tst42 : MetaM Unit := do print "----- tst42 -----"; let t ← mkListLit nat [mkRawNatLit 1, mkRawNatLit 2]; print t; check t; let t ← mkArrayLit nat [mkRawNatLit 1, mkRawNatLit 2]; print t; check t; (match t.arrayLit? with | some (_, xs) => do checkM $ pure $ xs.length == 2; (match (xs.get! 0).natLit?, (xs.get! 1).natLit? with | some 1, some 2 => pure () | _, _ => throwError "nat lits expected") | none => throwError "array lit expected") #eval tst42
8230e5563170e353ee87b90293c5db9f55a26a0d
9dc8cecdf3c4634764a18254e94d43da07142918
/src/measure_theory/decomposition/jordan.lean
e8d762e73b939b404f54db5b50d1a6dda0a9ceaa
[ "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
25,172
lean
/- Copyright (c) 2021 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import measure_theory.decomposition.signed_hahn import measure_theory.measure.mutually_singular /-! # Jordan decomposition This file proves the existence and uniqueness of the Jordan decomposition for signed measures. The Jordan decomposition theorem states that, given a signed measure `s`, there exists a unique pair of mutually singular measures `μ` and `ν`, such that `s = μ - ν`. The Jordan decomposition theorem for measures is a corollary of the Hahn decomposition theorem and is useful for the Lebesgue decomposition theorem. ## Main definitions * `measure_theory.jordan_decomposition`: a Jordan decomposition of a measurable space is a pair of mutually singular finite measures. We say `j` is a Jordan decomposition of a signed measure `s` if `s = j.pos_part - j.neg_part`. * `measure_theory.signed_measure.to_jordan_decomposition`: the Jordan decomposition of a signed measure. * `measure_theory.signed_measure.to_jordan_decomposition_equiv`: is the `equiv` between `measure_theory.signed_measure` and `measure_theory.jordan_decomposition` formed by `measure_theory.signed_measure.to_jordan_decomposition`. ## Main results * `measure_theory.signed_measure.to_signed_measure_to_jordan_decomposition` : the Jordan decomposition theorem. * `measure_theory.jordan_decomposition.to_signed_measure_injective` : the Jordan decomposition of a signed measure is unique. ## Tags Jordan decomposition theorem -/ noncomputable theory open_locale classical measure_theory ennreal nnreal variables {α β : Type*} [measurable_space α] namespace measure_theory /-- A Jordan decomposition of a measurable space is a pair of mutually singular, finite measures. -/ @[ext] structure jordan_decomposition (α : Type*) [measurable_space α] := (pos_part neg_part : measure α) [pos_part_finite : is_finite_measure pos_part] [neg_part_finite : is_finite_measure neg_part] (mutually_singular : pos_part ⊥ₘ neg_part) attribute [instance] jordan_decomposition.pos_part_finite attribute [instance] jordan_decomposition.neg_part_finite namespace jordan_decomposition open measure vector_measure variable (j : jordan_decomposition α) instance : has_zero (jordan_decomposition α) := { zero := ⟨0, 0, mutually_singular.zero_right⟩ } instance : inhabited (jordan_decomposition α) := { default := 0 } instance : has_involutive_neg (jordan_decomposition α) := { neg := λ j, ⟨j.neg_part, j.pos_part, j.mutually_singular.symm⟩, neg_neg := λ j, jordan_decomposition.ext _ _ rfl rfl } instance : has_smul ℝ≥0 (jordan_decomposition α) := { smul := λ r j, ⟨r • j.pos_part, r • j.neg_part, mutually_singular.smul _ (mutually_singular.smul _ j.mutually_singular.symm).symm⟩ } instance has_smul_real : has_smul ℝ (jordan_decomposition α) := { smul := λ r j, if hr : 0 ≤ r then r.to_nnreal • j else - ((-r).to_nnreal • j) } @[simp] lemma zero_pos_part : (0 : jordan_decomposition α).pos_part = 0 := rfl @[simp] lemma zero_neg_part : (0 : jordan_decomposition α).neg_part = 0 := rfl @[simp] lemma neg_pos_part : (-j).pos_part = j.neg_part := rfl @[simp] lemma neg_neg_part : (-j).neg_part = j.pos_part := rfl @[simp] lemma smul_pos_part (r : ℝ≥0) : (r • j).pos_part = r • j.pos_part := rfl @[simp] lemma smul_neg_part (r : ℝ≥0) : (r • j).neg_part = r • j.neg_part := rfl lemma real_smul_def (r : ℝ) (j : jordan_decomposition α) : r • j = if hr : 0 ≤ r then r.to_nnreal • j else - ((-r).to_nnreal • j) := rfl @[simp] lemma coe_smul (r : ℝ≥0) : (r : ℝ) • j = r • j := show dite _ _ _ = _, by rw [dif_pos (nnreal.coe_nonneg r), real.to_nnreal_coe] lemma real_smul_nonneg (r : ℝ) (hr : 0 ≤ r) : r • j = r.to_nnreal • j := dif_pos hr lemma real_smul_neg (r : ℝ) (hr : r < 0) : r • j = - ((-r).to_nnreal • j) := dif_neg (not_le.2 hr) lemma real_smul_pos_part_nonneg (r : ℝ) (hr : 0 ≤ r) : (r • j).pos_part = r.to_nnreal • j.pos_part := by { rw [real_smul_def, ← smul_pos_part, dif_pos hr] } lemma real_smul_neg_part_nonneg (r : ℝ) (hr : 0 ≤ r) : (r • j).neg_part = r.to_nnreal • j.neg_part := by { rw [real_smul_def, ← smul_neg_part, dif_pos hr] } lemma real_smul_pos_part_neg (r : ℝ) (hr : r < 0) : (r • j).pos_part = (-r).to_nnreal • j.neg_part := by { rw [real_smul_def, ← smul_neg_part, dif_neg (not_le.2 hr), neg_pos_part] } lemma real_smul_neg_part_neg (r : ℝ) (hr : r < 0) : (r • j).neg_part = (-r).to_nnreal • j.pos_part := by { rw [real_smul_def, ← smul_pos_part, dif_neg (not_le.2 hr), neg_neg_part] } /-- The signed measure associated with a Jordan decomposition. -/ def to_signed_measure : signed_measure α := j.pos_part.to_signed_measure - j.neg_part.to_signed_measure lemma to_signed_measure_zero : (0 : jordan_decomposition α).to_signed_measure = 0 := begin ext1 i hi, erw [to_signed_measure, to_signed_measure_sub_apply hi, sub_self, zero_apply], end lemma to_signed_measure_neg : (-j).to_signed_measure = -j.to_signed_measure := begin ext1 i hi, rw [neg_apply, to_signed_measure, to_signed_measure, to_signed_measure_sub_apply hi, to_signed_measure_sub_apply hi, neg_sub], refl, end lemma to_signed_measure_smul (r : ℝ≥0) : (r • j).to_signed_measure = r • j.to_signed_measure := begin ext1 i hi, rw [vector_measure.smul_apply, to_signed_measure, to_signed_measure, to_signed_measure_sub_apply hi, to_signed_measure_sub_apply hi, smul_sub, smul_pos_part, smul_neg_part, ← ennreal.to_real_smul, ← ennreal.to_real_smul], refl end /-- A Jordan decomposition provides a Hahn decomposition. -/ lemma exists_compl_positive_negative : ∃ S : set α, measurable_set S ∧ j.to_signed_measure ≤[S] 0 ∧ 0 ≤[Sᶜ] j.to_signed_measure ∧ j.pos_part S = 0 ∧ j.neg_part Sᶜ = 0 := begin obtain ⟨S, hS₁, hS₂, hS₃⟩ := j.mutually_singular, refine ⟨S, hS₁, _, _, hS₂, hS₃⟩, { refine restrict_le_restrict_of_subset_le _ _ (λ A hA hA₁, _), rw [to_signed_measure, to_signed_measure_sub_apply hA, show j.pos_part A = 0, by exact nonpos_iff_eq_zero.1 (hS₂ ▸ measure_mono hA₁), ennreal.zero_to_real, zero_sub, neg_le, zero_apply, neg_zero], exact ennreal.to_real_nonneg }, { refine restrict_le_restrict_of_subset_le _ _ (λ A hA hA₁, _), rw [to_signed_measure, to_signed_measure_sub_apply hA, show j.neg_part A = 0, by exact nonpos_iff_eq_zero.1 (hS₃ ▸ measure_mono hA₁), ennreal.zero_to_real, sub_zero], exact ennreal.to_real_nonneg }, end end jordan_decomposition namespace signed_measure open classical jordan_decomposition measure set vector_measure variables {s : signed_measure α} {μ ν : measure α} [is_finite_measure μ] [is_finite_measure ν] /-- Given a signed measure `s`, `s.to_jordan_decomposition` is the Jordan decomposition `j`, such that `s = j.to_signed_measure`. This property is known as the Jordan decomposition theorem, and is shown by `measure_theory.signed_measure.to_signed_measure_to_jordan_decomposition`. -/ def to_jordan_decomposition (s : signed_measure α) : jordan_decomposition α := let i := some s.exists_compl_positive_negative in let hi := some_spec s.exists_compl_positive_negative in { pos_part := s.to_measure_of_zero_le i hi.1 hi.2.1, neg_part := s.to_measure_of_le_zero iᶜ hi.1.compl hi.2.2, pos_part_finite := infer_instance, neg_part_finite := infer_instance, mutually_singular := begin refine ⟨iᶜ, hi.1.compl, _, _⟩, { rw [to_measure_of_zero_le_apply _ _ hi.1 hi.1.compl], simp }, { rw [to_measure_of_le_zero_apply _ _ hi.1.compl hi.1.compl.compl], simp } end } lemma to_jordan_decomposition_spec (s : signed_measure α) : ∃ (i : set α) (hi₁ : measurable_set i) (hi₂ : 0 ≤[i] s) (hi₃ : s ≤[iᶜ] 0), s.to_jordan_decomposition.pos_part = s.to_measure_of_zero_le i hi₁ hi₂ ∧ s.to_jordan_decomposition.neg_part = s.to_measure_of_le_zero iᶜ hi₁.compl hi₃ := begin set i := some s.exists_compl_positive_negative, obtain ⟨hi₁, hi₂, hi₃⟩ := some_spec s.exists_compl_positive_negative, exact ⟨i, hi₁, hi₂, hi₃, rfl, rfl⟩, end /-- **The Jordan decomposition theorem**: Given a signed measure `s`, there exists a pair of mutually singular measures `μ` and `ν` such that `s = μ - ν`. In this case, the measures `μ` and `ν` are given by `s.to_jordan_decomposition.pos_part` and `s.to_jordan_decomposition.neg_part` respectively. Note that we use `measure_theory.jordan_decomposition.to_signed_measure` to represent the signed measure corresponding to `s.to_jordan_decomposition.pos_part - s.to_jordan_decomposition.neg_part`. -/ @[simp] lemma to_signed_measure_to_jordan_decomposition (s : signed_measure α) : s.to_jordan_decomposition.to_signed_measure = s := begin obtain ⟨i, hi₁, hi₂, hi₃, hμ, hν⟩ := s.to_jordan_decomposition_spec, simp only [jordan_decomposition.to_signed_measure, hμ, hν], ext k hk, rw [to_signed_measure_sub_apply hk, to_measure_of_zero_le_apply _ hi₂ hi₁ hk, to_measure_of_le_zero_apply _ hi₃ hi₁.compl hk], simp only [ennreal.coe_to_real, subtype.coe_mk, ennreal.some_eq_coe, sub_neg_eq_add], rw [← of_union _ (measurable_set.inter hi₁ hk) (measurable_set.inter hi₁.compl hk), set.inter_comm i, set.inter_comm iᶜ, set.inter_union_compl _ _], { apply_instance }, { rintro x ⟨⟨hx₁, _⟩, hx₂, _⟩, exact false.elim (hx₂ hx₁) } end section variables {u v w : set α} /-- A subset `v` of a null-set `w` has zero measure if `w` is a subset of a positive set `u`. -/ lemma subset_positive_null_set (hu : measurable_set u) (hv : measurable_set v) (hw : measurable_set w) (hsu : 0 ≤[u] s) (hw₁ : s w = 0) (hw₂ : w ⊆ u) (hwt : v ⊆ w) : s v = 0 := begin have : s v + s (w \ v) = 0, { rw [← hw₁, ← of_union set.disjoint_diff hv (hw.diff hv), set.union_diff_self, set.union_eq_self_of_subset_left hwt], apply_instance }, have h₁ := nonneg_of_zero_le_restrict _ (restrict_le_restrict_subset _ _ hu hsu (hwt.trans hw₂)), have h₂ := nonneg_of_zero_le_restrict _ (restrict_le_restrict_subset _ _ hu hsu ((w.diff_subset v).trans hw₂)), linarith, end /-- A subset `v` of a null-set `w` has zero measure if `w` is a subset of a negative set `u`. -/ lemma subset_negative_null_set (hu : measurable_set u) (hv : measurable_set v) (hw : measurable_set w) (hsu : s ≤[u] 0) (hw₁ : s w = 0) (hw₂ : w ⊆ u) (hwt : v ⊆ w) : s v = 0 := begin rw [← s.neg_le_neg_iff _ hu, neg_zero] at hsu, have := subset_positive_null_set hu hv hw hsu, simp only [pi.neg_apply, neg_eq_zero, coe_neg] at this, exact this hw₁ hw₂ hwt, end /-- If the symmetric difference of two positive sets is a null-set, then so are the differences between the two sets. -/ lemma of_diff_eq_zero_of_symm_diff_eq_zero_positive (hu : measurable_set u) (hv : measurable_set v) (hsu : 0 ≤[u] s) (hsv : 0 ≤[v] s) (hs : s (u ∆ v) = 0) : s (u \ v) = 0 ∧ s (v \ u) = 0 := begin rw restrict_le_restrict_iff at hsu hsv, have a := hsu (hu.diff hv) (u.diff_subset v), have b := hsv (hv.diff hu) (v.diff_subset u), erw [of_union (set.disjoint_of_subset_left (u.diff_subset v) set.disjoint_diff) (hu.diff hv) (hv.diff hu)] at hs, rw zero_apply at a b, split, all_goals { linarith <|> apply_instance <|> assumption }, end /-- If the symmetric difference of two negative sets is a null-set, then so are the differences between the two sets. -/ lemma of_diff_eq_zero_of_symm_diff_eq_zero_negative (hu : measurable_set u) (hv : measurable_set v) (hsu : s ≤[u] 0) (hsv : s ≤[v] 0) (hs : s (u ∆ v) = 0) : s (u \ v) = 0 ∧ s (v \ u) = 0 := begin rw [← s.neg_le_neg_iff _ hu, neg_zero] at hsu, rw [← s.neg_le_neg_iff _ hv, neg_zero] at hsv, have := of_diff_eq_zero_of_symm_diff_eq_zero_positive hu hv hsu hsv, simp only [pi.neg_apply, neg_eq_zero, coe_neg] at this, exact this hs, end lemma of_inter_eq_of_symm_diff_eq_zero_positive (hu : measurable_set u) (hv : measurable_set v) (hw : measurable_set w) (hsu : 0 ≤[u] s) (hsv : 0 ≤[v] s) (hs : s (u ∆ v) = 0) : s (w ∩ u) = s (w ∩ v) := begin have hwuv : s ((w ∩ u) ∆ (w ∩ v)) = 0, { refine subset_positive_null_set (hu.union hv) ((hw.inter hu).symm_diff (hw.inter hv)) (hu.symm_diff hv) (restrict_le_restrict_union _ _ hu hsu hv hsv) hs symm_diff_subset_union _, rw ←inter_symm_diff_distrib_left, exact inter_subset_right _ _ }, obtain ⟨huv, hvu⟩ := of_diff_eq_zero_of_symm_diff_eq_zero_positive (hw.inter hu) (hw.inter hv) (restrict_le_restrict_subset _ _ hu hsu (w.inter_subset_right u)) (restrict_le_restrict_subset _ _ hv hsv (w.inter_subset_right v)) hwuv, rw [← of_diff_of_diff_eq_zero (hw.inter hu) (hw.inter hv) hvu, huv, zero_add] end lemma of_inter_eq_of_symm_diff_eq_zero_negative (hu : measurable_set u) (hv : measurable_set v) (hw : measurable_set w) (hsu : s ≤[u] 0) (hsv : s ≤[v] 0) (hs : s (u ∆ v) = 0) : s (w ∩ u) = s (w ∩ v) := begin rw [← s.neg_le_neg_iff _ hu, neg_zero] at hsu, rw [← s.neg_le_neg_iff _ hv, neg_zero] at hsv, have := of_inter_eq_of_symm_diff_eq_zero_positive hu hv hw hsu hsv, simp only [pi.neg_apply, neg_inj, neg_eq_zero, coe_neg] at this, exact this hs, end end end signed_measure namespace jordan_decomposition open measure vector_measure signed_measure function private lemma eq_of_pos_part_eq_pos_part {j₁ j₂ : jordan_decomposition α} (hj : j₁.pos_part = j₂.pos_part) (hj' : j₁.to_signed_measure = j₂.to_signed_measure) : j₁ = j₂ := begin ext1, { exact hj }, { rw ← to_signed_measure_eq_to_signed_measure_iff, suffices : j₁.pos_part.to_signed_measure - j₁.neg_part.to_signed_measure = j₁.pos_part.to_signed_measure - j₂.neg_part.to_signed_measure, { exact sub_right_inj.mp this }, convert hj' } end /-- The Jordan decomposition of a signed measure is unique. -/ theorem to_signed_measure_injective : injective $ @jordan_decomposition.to_signed_measure α _ := begin /- The main idea is that two Jordan decompositions of a signed measure provide two Hahn decompositions for that measure. Then, from `of_symm_diff_compl_positive_negative`, the symmetric difference of the two Hahn decompositions has measure zero, thus, allowing us to show the equality of the underlying measures of the Jordan decompositions. -/ intros j₁ j₂ hj, -- obtain the two Hahn decompositions from the Jordan decompositions obtain ⟨S, hS₁, hS₂, hS₃, hS₄, hS₅⟩ := j₁.exists_compl_positive_negative, obtain ⟨T, hT₁, hT₂, hT₃, hT₄, hT₅⟩ := j₂.exists_compl_positive_negative, rw ← hj at hT₂ hT₃, -- the symmetric differences of the two Hahn decompositions have measure zero obtain ⟨hST₁, -⟩ := of_symm_diff_compl_positive_negative hS₁.compl hT₁.compl ⟨hS₃, (compl_compl S).symm ▸ hS₂⟩ ⟨hT₃, (compl_compl T).symm ▸ hT₂⟩, -- it suffices to show the Jordan decompositions have the same positive parts refine eq_of_pos_part_eq_pos_part _ hj, ext1 i hi, -- we see that the positive parts of the two Jordan decompositions are equal to their -- associated signed measures restricted on their associated Hahn decompositions have hμ₁ : (j₁.pos_part i).to_real = j₁.to_signed_measure (i ∩ Sᶜ), { rw [to_signed_measure, to_signed_measure_sub_apply (hi.inter hS₁.compl), show j₁.neg_part (i ∩ Sᶜ) = 0, by exact nonpos_iff_eq_zero.1 (hS₅ ▸ measure_mono (set.inter_subset_right _ _)), ennreal.zero_to_real, sub_zero], conv_lhs { rw ← set.inter_union_compl i S }, rw [measure_union, show j₁.pos_part (i ∩ S) = 0, by exact nonpos_iff_eq_zero.1 (hS₄ ▸ measure_mono (set.inter_subset_right _ _)), zero_add], { refine set.disjoint_of_subset_left (set.inter_subset_right _ _) (set.disjoint_of_subset_right (set.inter_subset_right _ _) disjoint_compl_right) }, { exact hi.inter hS₁.compl } }, have hμ₂ : (j₂.pos_part i).to_real = j₂.to_signed_measure (i ∩ Tᶜ), { rw [to_signed_measure, to_signed_measure_sub_apply (hi.inter hT₁.compl), show j₂.neg_part (i ∩ Tᶜ) = 0, by exact nonpos_iff_eq_zero.1 (hT₅ ▸ measure_mono (set.inter_subset_right _ _)), ennreal.zero_to_real, sub_zero], conv_lhs { rw ← set.inter_union_compl i T }, rw [measure_union, show j₂.pos_part (i ∩ T) = 0, by exact nonpos_iff_eq_zero.1 (hT₄ ▸ measure_mono (set.inter_subset_right _ _)), zero_add], { exact set.disjoint_of_subset_left (set.inter_subset_right _ _) (set.disjoint_of_subset_right (set.inter_subset_right _ _) disjoint_compl_right) }, { exact hi.inter hT₁.compl } }, -- since the two signed measures associated with the Jordan decompositions are the same, -- and the symmetric difference of the Hahn decompositions have measure zero, the result follows rw [← ennreal.to_real_eq_to_real (measure_ne_top _ _) (measure_ne_top _ _), hμ₁, hμ₂, ← hj], exact of_inter_eq_of_symm_diff_eq_zero_positive hS₁.compl hT₁.compl hi hS₃ hT₃ hST₁, all_goals { apply_instance }, end @[simp] lemma to_jordan_decomposition_to_signed_measure (j : jordan_decomposition α) : (j.to_signed_measure).to_jordan_decomposition = j := (@to_signed_measure_injective _ _ j (j.to_signed_measure).to_jordan_decomposition (by simp)).symm end jordan_decomposition namespace signed_measure open jordan_decomposition /-- `measure_theory.signed_measure.to_jordan_decomposition` and `measure_theory.jordan_decomposition.to_signed_measure` form a `equiv`. -/ @[simps apply symm_apply] def to_jordan_decomposition_equiv (α : Type*) [measurable_space α] : signed_measure α ≃ jordan_decomposition α := { to_fun := to_jordan_decomposition, inv_fun := to_signed_measure, left_inv := to_signed_measure_to_jordan_decomposition, right_inv := to_jordan_decomposition_to_signed_measure } lemma to_jordan_decomposition_zero : (0 : signed_measure α).to_jordan_decomposition = 0 := begin apply to_signed_measure_injective, simp [to_signed_measure_zero], end lemma to_jordan_decomposition_neg (s : signed_measure α) : (-s).to_jordan_decomposition = -s.to_jordan_decomposition := begin apply to_signed_measure_injective, simp [to_signed_measure_neg], end lemma to_jordan_decomposition_smul (s : signed_measure α) (r : ℝ≥0) : (r • s).to_jordan_decomposition = r • s.to_jordan_decomposition := begin apply to_signed_measure_injective, simp [to_signed_measure_smul], end private lemma to_jordan_decomposition_smul_real_nonneg (s : signed_measure α) (r : ℝ) (hr : 0 ≤ r): (r • s).to_jordan_decomposition = r • s.to_jordan_decomposition := begin lift r to ℝ≥0 using hr, rw [jordan_decomposition.coe_smul, ← to_jordan_decomposition_smul], refl end lemma to_jordan_decomposition_smul_real (s : signed_measure α) (r : ℝ) : (r • s).to_jordan_decomposition = r • s.to_jordan_decomposition := begin by_cases hr : 0 ≤ r, { exact to_jordan_decomposition_smul_real_nonneg s r hr }, { ext1, { rw [real_smul_pos_part_neg _ _ (not_le.1 hr), show r • s = -(-r • s), by rw [neg_smul, neg_neg], to_jordan_decomposition_neg, neg_pos_part, to_jordan_decomposition_smul_real_nonneg, ← smul_neg_part, real_smul_nonneg], all_goals { exact left.nonneg_neg_iff.2 (le_of_lt (not_le.1 hr)) } }, { rw [real_smul_neg_part_neg _ _ (not_le.1 hr), show r • s = -(-r • s), by rw [neg_smul, neg_neg], to_jordan_decomposition_neg, neg_neg_part, to_jordan_decomposition_smul_real_nonneg, ← smul_pos_part, real_smul_nonneg], all_goals { exact left.nonneg_neg_iff.2 (le_of_lt (not_le.1 hr)) } } } end lemma to_jordan_decomposition_eq {s : signed_measure α} {j : jordan_decomposition α} (h : s = j.to_signed_measure) : s.to_jordan_decomposition = j := by rw [h, to_jordan_decomposition_to_signed_measure] /-- The total variation of a signed measure. -/ def total_variation (s : signed_measure α) : measure α := s.to_jordan_decomposition.pos_part + s.to_jordan_decomposition.neg_part lemma total_variation_zero : (0 : signed_measure α).total_variation = 0 := by simp [total_variation, to_jordan_decomposition_zero] lemma total_variation_neg (s : signed_measure α) : (-s).total_variation = s.total_variation := by simp [total_variation, to_jordan_decomposition_neg, add_comm] lemma null_of_total_variation_zero (s : signed_measure α) {i : set α} (hs : s.total_variation i = 0) : s i = 0 := begin rw [total_variation, measure.coe_add, pi.add_apply, add_eq_zero_iff] at hs, rw [← to_signed_measure_to_jordan_decomposition s, to_signed_measure, vector_measure.coe_sub, pi.sub_apply, measure.to_signed_measure_apply, measure.to_signed_measure_apply], by_cases hi : measurable_set i, { rw [if_pos hi, if_pos hi], simp [hs.1, hs.2] }, { simp [if_neg hi] } end lemma absolutely_continuous_ennreal_iff (s : signed_measure α) (μ : vector_measure α ℝ≥0∞) : s ≪ᵥ μ ↔ s.total_variation ≪ μ.ennreal_to_measure := begin split; intro h, { refine measure.absolutely_continuous.mk (λ S hS₁ hS₂, _), obtain ⟨i, hi₁, hi₂, hi₃, hpos, hneg⟩ := s.to_jordan_decomposition_spec, rw [total_variation, measure.add_apply, hpos, hneg, to_measure_of_zero_le_apply _ _ _ hS₁, to_measure_of_le_zero_apply _ _ _ hS₁], rw ← vector_measure.absolutely_continuous.ennreal_to_measure at h, simp [h (measure_mono_null (i.inter_subset_right S) hS₂), h (measure_mono_null (iᶜ.inter_subset_right S) hS₂)] }, { refine vector_measure.absolutely_continuous.mk (λ S hS₁ hS₂, _), rw ← vector_measure.ennreal_to_measure_apply hS₁ at hS₂, exact null_of_total_variation_zero s (h hS₂) } end lemma total_variation_absolutely_continuous_iff (s : signed_measure α) (μ : measure α) : s.total_variation ≪ μ ↔ s.to_jordan_decomposition.pos_part ≪ μ ∧ s.to_jordan_decomposition.neg_part ≪ μ := begin split; intro h, { split, all_goals { refine measure.absolutely_continuous.mk (λ S hS₁ hS₂, _), have := h hS₂, rw [total_variation, measure.add_apply, add_eq_zero_iff] at this }, exacts [this.1, this.2] }, { refine measure.absolutely_continuous.mk (λ S hS₁ hS₂, _), rw [total_variation, measure.add_apply, h.1 hS₂, h.2 hS₂, add_zero] } end -- TODO: Generalize to vector measures once total variation on vector measures is defined lemma mutually_singular_iff (s t : signed_measure α) : s ⊥ᵥ t ↔ s.total_variation ⊥ₘ t.total_variation := begin split, { rintro ⟨u, hmeas, hu₁, hu₂⟩, obtain ⟨i, hi₁, hi₂, hi₃, hipos, hineg⟩ := s.to_jordan_decomposition_spec, obtain ⟨j, hj₁, hj₂, hj₃, hjpos, hjneg⟩ := t.to_jordan_decomposition_spec, refine ⟨u, hmeas, _, _⟩, { rw [total_variation, measure.add_apply, hipos, hineg, to_measure_of_zero_le_apply _ _ _ hmeas, to_measure_of_le_zero_apply _ _ _ hmeas], simp [hu₁ _ (set.inter_subset_right _ _)] }, { rw [total_variation, measure.add_apply, hjpos, hjneg, to_measure_of_zero_le_apply _ _ _ hmeas.compl, to_measure_of_le_zero_apply _ _ _ hmeas.compl], simp [hu₂ _ (set.inter_subset_right _ _)] } }, { rintro ⟨u, hmeas, hu₁, hu₂⟩, exact ⟨u, hmeas, (λ t htu, null_of_total_variation_zero _ (measure_mono_null htu hu₁)), (λ t htv, null_of_total_variation_zero _ (measure_mono_null htv hu₂))⟩ } end lemma mutually_singular_ennreal_iff (s : signed_measure α) (μ : vector_measure α ℝ≥0∞) : s ⊥ᵥ μ ↔ s.total_variation ⊥ₘ μ.ennreal_to_measure := begin split, { rintro ⟨u, hmeas, hu₁, hu₂⟩, obtain ⟨i, hi₁, hi₂, hi₃, hpos, hneg⟩ := s.to_jordan_decomposition_spec, refine ⟨u, hmeas, _, _⟩, { rw [total_variation, measure.add_apply, hpos, hneg, to_measure_of_zero_le_apply _ _ _ hmeas, to_measure_of_le_zero_apply _ _ _ hmeas], simp [hu₁ _ (set.inter_subset_right _ _)] }, { rw vector_measure.ennreal_to_measure_apply hmeas.compl, exact hu₂ _ (set.subset.refl _) } }, { rintro ⟨u, hmeas, hu₁, hu₂⟩, refine vector_measure.mutually_singular.mk u hmeas (λ t htu _, null_of_total_variation_zero _ (measure_mono_null htu hu₁)) (λ t htv hmt, _), rw ← vector_measure.ennreal_to_measure_apply hmt, exact measure_mono_null htv hu₂ } end lemma total_variation_mutually_singular_iff (s : signed_measure α) (μ : measure α) : s.total_variation ⊥ₘ μ ↔ s.to_jordan_decomposition.pos_part ⊥ₘ μ ∧ s.to_jordan_decomposition.neg_part ⊥ₘ μ := measure.mutually_singular.add_left_iff end signed_measure end measure_theory
bc1270fa3dc7db3e9ae51c634762d6eac4a736d1
2f8bf12144551bc7d8087a6320990c4621741f3d
/tests/playground/binarytrees.lean
d5bbabbdeedb968efd7208dee50a966c8e55f25f
[ "Apache-2.0" ]
permissive
jesse-michael-han/lean4
eb63a12960e69823749edceb4f23fd33fa2253ce
fa16920a6a7700cabc567aa629ce4ae2478a2f40
refs/heads/master
1,589,935,810,594
1,557,177,860,000
1,557,177,860,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,626
lean
inductive Tree | Nil | Node (l r : Tree) : Tree open Tree -- This Function has an extra argument to suppress the -- Common Sub-expression Elimination optimization def make' : UInt32 -> UInt32 -> Tree | n d := if d = 0 then Node Nil Nil else Node (make' n (d - 1)) (make' (n + 1) (d - 1)) -- build a tree def make (d : UInt32) := make' d d def check : Tree → UInt32 | Nil := 0 | (Node l r) := 1 + check l + check r def minN := 4 def out (s) (n : Nat) (t : UInt32) := IO.println (s ++ " of depth " ++ toString n ++ "\t check: " ++ toString t) -- allocate and check lots of trees def sumT : UInt32 -> UInt32 -> UInt32 -> UInt32 | d i t := if i = 0 then t else let a := check (make d) in sumT d (i-1) (t + a) -- generate many trees meta def depth : Nat -> Nat -> List (Nat × Nat × Task UInt32) | d m := if d ≤ m then let n := 2 ^ (m - d + minN) in (n, d, Task.mk (λ _, sumT (UInt32.ofNat d) (UInt32.ofNat n) 0)) :: depth (d+2) m else [] meta def main : List String → IO UInt32 | [s] := do let n := s.toNat, let maxN := Nat.max (minN + 2) n, let stretchN := maxN + 1, -- stretch memory tree let c := check (make $ UInt32.ofNat stretchN), out "stretch tree" stretchN c, -- allocate a long lived tree let long := make $ UInt32.ofNat maxN, -- allocate, walk, and deallocate many bottom-up binary trees let vs := (depth minN maxN), -- `using` (parList $ evalTuple3 r0 r0 rseq) vs.mmap (λ ⟨m,d,i⟩, out (toString m ++ "\t trees") d i.get), -- confirm the the long-lived binary tree still exists out "long lived tree" maxN (check long), pure 0 | _ := pure 1
221df3475fa2f8c8f50cf388ea486200aa4255cc
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/star/module.lean
3bc5edfa0e127c1e73a54f5a3de2512d2cf2bd16
[ "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
5,962
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser, Frédéric Dupuis -/ import algebra.star.self_adjoint import algebra.module.equiv import linear_algebra.prod /-! # The star operation, bundled as a star-linear equiv > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We define `star_linear_equiv`, which is the star operation bundled as a star-linear map. It is defined on a star algebra `A` over the base ring `R`. This file also provides some lemmas that need `algebra.module.basic` imported to prove. ## TODO - Define `star_linear_equiv` for noncommutative `R`. We only the commutative case for now since, in the noncommutative case, the ring hom needs to reverse the order of multiplication. This requires a ring hom of type `R →+* Rᵐᵒᵖ`, which is very undesirable in the commutative case. One way out would be to define a new typeclass `is_op R S` and have an instance `is_op R R` for commutative `R`. - Also note that such a definition involving `Rᵐᵒᵖ` or `is_op R S` would require adding the appropriate `ring_hom_inv_pair` instances to be able to define the semilinear equivalence. -/ section smul_lemmas variables {R M : Type*} @[simp] lemma star_nat_cast_smul [semiring R] [add_comm_monoid M] [module R M] [star_add_monoid M] (n : ℕ) (x : M) : star ((n : R) • x) = (n : R) • star x := map_nat_cast_smul (star_add_equiv : M ≃+ M) R R n x @[simp] lemma star_int_cast_smul [ring R] [add_comm_group M] [module R M] [star_add_monoid M] (n : ℤ) (x : M) : star ((n : R) • x) = (n : R) • star x := map_int_cast_smul (star_add_equiv : M ≃+ M) R R n x @[simp] lemma star_inv_nat_cast_smul [division_semiring R] [add_comm_monoid M] [module R M] [star_add_monoid M] (n : ℕ) (x : M) : star ((n⁻¹ : R) • x) = (n⁻¹ : R) • star x := map_inv_nat_cast_smul (star_add_equiv : M ≃+ M) R R n x @[simp] lemma star_inv_int_cast_smul [division_ring R] [add_comm_group M] [module R M] [star_add_monoid M] (n : ℤ) (x : M) : star ((n⁻¹ : R) • x) = (n⁻¹ : R) • star x := map_inv_int_cast_smul (star_add_equiv : M ≃+ M) R R n x @[simp] lemma star_rat_cast_smul [division_ring R] [add_comm_group M] [module R M] [star_add_monoid M] (n : ℚ) (x : M) : star ((n : R) • x) = (n : R) • star x := map_rat_cast_smul (star_add_equiv : M ≃+ M) _ _ _ x @[simp] lemma star_rat_smul {R : Type*} [add_comm_group R] [star_add_monoid R] [module ℚ R] (x : R) (n : ℚ) : star (n • x) = n • star x := map_rat_smul (star_add_equiv : R ≃+ R) _ _ end smul_lemmas /-- If `A` is a module over a commutative `R` with compatible actions, then `star` is a semilinear equivalence. -/ @[simps] def star_linear_equiv (R : Type*) {A : Type*} [comm_semiring R] [star_ring R] [add_comm_monoid A] [star_add_monoid A] [module R A] [star_module R A] : A ≃ₗ⋆[R] A := { to_fun := star, map_smul' := star_smul, .. star_add_equiv } variables (R : Type*) (A : Type*) [semiring R] [star_semigroup R] [has_trivial_star R] [add_comm_group A] [module R A] [star_add_monoid A] [star_module R A] /-- The self-adjoint elements of a star module, as a submodule. -/ def self_adjoint.submodule : submodule R A := { smul_mem' := λ r x, (is_self_adjoint.all _).smul, ..self_adjoint A } /-- The skew-adjoint elements of a star module, as a submodule. -/ def skew_adjoint.submodule : submodule R A := { smul_mem' := skew_adjoint.smul_mem, ..skew_adjoint A } variables {A} [invertible (2 : R)] /-- The self-adjoint part of an element of a star module, as a linear map. -/ @[simps] def self_adjoint_part : A →ₗ[R] self_adjoint A := { to_fun := λ x, ⟨(⅟2 : R) • (x + star x), by simp only [self_adjoint.mem_iff, star_smul, add_comm, star_add_monoid.star_add, star_inv', star_bit0, star_one, star_star, star_inv_of (2 : R), star_trivial]⟩, map_add' := λ x y, by { ext, simp [add_add_add_comm] }, map_smul' := λ r x, by { ext, simp [←mul_smul, show ⅟ 2 * r = r * ⅟ 2, from commute.inv_of_left (commute.one_left r).bit0_left] } } /-- The skew-adjoint part of an element of a star module, as a linear map. -/ @[simps] def skew_adjoint_part : A →ₗ[R] skew_adjoint A := { to_fun := λ x, ⟨(⅟2 : R) • (x - star x), by simp only [skew_adjoint.mem_iff, star_smul, star_sub, star_star, star_trivial, ←smul_neg, neg_sub]⟩, map_add' := λ x y, by { ext, simp only [sub_add, ←smul_add, sub_sub_eq_add_sub, star_add, add_subgroup.coe_mk, add_subgroup.coe_add] }, map_smul' := λ r x, by { ext, simp [←mul_smul, ←smul_sub, show r * ⅟ 2 = ⅟ 2 * r, from commute.inv_of_right (commute.one_right r).bit0_right] } } lemma star_module.self_adjoint_part_add_skew_adjoint_part (x : A) : (self_adjoint_part R x : A) + skew_adjoint_part R x = x := by simp only [smul_sub, self_adjoint_part_apply_coe, smul_add, skew_adjoint_part_apply_coe, add_add_sub_cancel, inv_of_two_smul_add_inv_of_two_smul] variables (A) /-- The decomposition of elements of a star module into their self- and skew-adjoint parts, as a linear equivalence. -/ @[simps] def star_module.decompose_prod_adjoint : A ≃ₗ[R] self_adjoint A × skew_adjoint A := linear_equiv.of_linear ((self_adjoint_part R).prod (skew_adjoint_part R)) ((self_adjoint.submodule R A).subtype.coprod (skew_adjoint.submodule R A).subtype) (by ext; simp) (linear_map.ext $ star_module.self_adjoint_part_add_skew_adjoint_part R) @[simp] lemma algebra_map_star_comm {R A : Type*} [comm_semiring R] [star_ring R] [semiring A] [star_semigroup A] [algebra R A] [star_module R A] (r : R) : algebra_map R A (star r) = star (algebra_map R A r) := by simp only [algebra.algebra_map_eq_smul_one, star_smul, star_one]
3a06f4f62b35cdf762e2e2b9ad5b2e7ce0919446
7cef822f3b952965621309e88eadf618da0c8ae9
/src/tactic/omega/nat/neg_elim.lean
8cc42923a421fe0fbca3e7bd2b3d8fbb1219c1ab
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
3,818
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek Negation elimination. -/ import tactic.omega.nat.form namespace omega namespace nat open_locale omega.nat @[simp] def push_neg : form → form | (p ∨* q) := (push_neg p) ∧* (push_neg q) | (p ∧* q) := (push_neg p) ∨* (push_neg q) | (¬*p) := p | p := ¬* p lemma push_neg_equiv : ∀ {p : form}, form.equiv (push_neg p) (¬* p) := begin form.induce `[intros v; try {refl}], { simp only [classical.not_not, form.holds, push_neg] }, { simp only [form.holds, push_neg, not_or_distrib, ihp v, ihq v] }, { simp only [form.holds, push_neg, classical.not_and_distrib, ihp v, ihq v] } end def nnf : form → form | (¬* p) := push_neg (nnf p) | (p ∨* q) := (nnf p) ∨* (nnf q) | (p ∧* q) := (nnf p) ∧* (nnf q) | a := a def is_nnf : form → Prop | (t =* s) := true | (t ≤* s) := true | ¬*(t =* s) := true | ¬*(t ≤* s) := true | (p ∨* q) := is_nnf p ∧ is_nnf q | (p ∧* q) := is_nnf p ∧ is_nnf q | _ := false lemma is_nnf_push_neg : ∀ p : form, is_nnf p → is_nnf (push_neg p) := begin form.induce `[intro h1; try {trivial}], { cases p; try {cases h1}; trivial }, { cases h1, constructor; [{apply ihp}, {apply ihq}]; assumption }, { cases h1, constructor; [{apply ihp}, {apply ihq}]; assumption } end lemma is_nnf_nnf : ∀ p : form, is_nnf (nnf p) := begin form.induce `[try {trivial}], { apply is_nnf_push_neg _ ih }, { constructor; assumption }, { constructor; assumption } end lemma nnf_equiv : ∀ {p : form}, form.equiv (nnf p) p := begin form.induce `[intros v; try {refl}; simp only [nnf]], { rw push_neg_equiv, apply not_iff_not_of_iff, apply ih }, { apply pred_mono_2' (ihp v) (ihq v) }, { apply pred_mono_2' (ihp v) (ihq v) } end @[simp] def neg_elim_core : form → form | (¬* (t =* s)) := (t.add_one ≤* s) ∨* (s.add_one ≤* t) | (¬* (t ≤* s)) := s.add_one ≤* t | (p ∨* q) := (neg_elim_core p) ∨* (neg_elim_core q) | (p ∧* q) := (neg_elim_core p) ∧* (neg_elim_core q) | p := p lemma neg_free_neg_elim_core : ∀ p, is_nnf p → (neg_elim_core p).neg_free := begin form.induce `[intro h1, try {simp only [neg_free, neg_elim_core]}, try {trivial}], { cases p; try {cases h1}; try {trivial}, constructor; trivial }, { cases h1, constructor; [{apply ihp}, {apply ihq}]; assumption }, { cases h1, constructor; [{apply ihp}, {apply ihq}]; assumption } end lemma le_and_le_iff_eq {α : Type} [partial_order α] {a b : α} : (a ≤ b ∧ b ≤ a) ↔ a = b := begin constructor; intro h1, { cases h1, apply le_antisymm; assumption }, { constructor; apply le_of_eq; rw h1 } end lemma implies_neg_elim_core : ∀ {p : form}, form.implies p (neg_elim_core p) := begin form.induce `[intros v h, try {apply h}], { cases p with t s t s; try {apply h}, { have : preterm.val v (preterm.add_one t) ≤ preterm.val v s ∨ preterm.val v (preterm.add_one s) ≤ preterm.val v t, { rw or.comm, simpa only [form.holds, le_and_le_iff_eq.symm, classical.not_and_distrib, not_le] using h }, simpa only [form.holds, neg_elim_core, int.add_one_le_iff] }, simpa only [form.holds, not_le, int.add_one_le_iff] using h }, { simp only [neg_elim_core], cases h; [{left, apply ihp}, {right, apply ihq}]; assumption }, apply and.imp (ihp _) (ihq _) h end def neg_elim : form → form := neg_elim_core ∘ nnf lemma neg_free_neg_elim {p : form} : (neg_elim p).neg_free := neg_free_neg_elim_core _ (is_nnf_nnf _) lemma implies_neg_elim {p : form} : form.implies p (neg_elim p) := begin intros v h1, apply implies_neg_elim_core, apply (nnf_equiv v).elim_right h1 end end nat end omega
7ac67768f01a5bf10c2d62073f40012cf361abbf
b147e1312077cdcfea8e6756207b3fa538982e12
/tests/tactics.lean
9e613b31e4b9f8534e113a367bee09303956318f
[ "Apache-2.0" ]
permissive
SzJS/mathlib
07836ee708ca27cd18347e1e11ce7dd5afb3e926
23a5591fca0d43ee5d49d89f6f0ee07a24a6ca29
refs/heads/master
1,584,980,332,064
1,532,063,841,000
1,532,063,841,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,753
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import tactic data.set.lattice data.prod section solve_by_elim example {a b : Prop} (h₀ : a → b) (h₁ : a) : b := begin apply_assumption, apply_assumption, end example {a b : Prop} (h₀ : a → b) (h₁ : a) : b := by solve_by_elim example {α : Type} {a b : α → Prop} (h₀ : ∀ x : α, b x = a x) (y : α) : a y = b y := by solve_by_elim example {α : Type} {p : α → Prop} (h₀ : ∀ x, p x) (y : α) : p y := begin apply_assumption, end end solve_by_elim section tauto₀ variables p q r : Prop variables h : p ∧ q ∨ p ∧ r include h example : p ∧ p := by tauto end tauto₀ section tauto₁ variables α : Type variables p q r : α → Prop variables h : (∃ x, p x ∧ q x) ∨ (∃ x, p x ∧ r x) include h example : ∃ x, p x := by tauto end tauto₁ section tauto₂ variables α : Type variables x : α variables p q r : α → Prop variables h₀ : (∀ x, p x → q x → r x) ∨ r x variables h₁ : p x variables h₂ : q x include h₀ h₁ h₂ example : ∃ x, r x := by tauto end tauto₂ section tauto₃ example (p : Prop) : p ∧ true ↔ p := by tauto example (p : Prop) : p ∨ false ↔ p := by tauto example (p q r : Prop) [decidable p] [decidable r] : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (r ∨ p ∨ r) := by tauto example (p q r : Prop) [decidable q] [decidable r] : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (r ∨ p ∨ r) := by tauto example (p q : Prop) [decidable q] [decidable p] (h : ¬ (p ↔ q)) (h' : ¬ p) : q := by tauto example (p q : Prop) [decidable q] [decidable p] (h : ¬ (p ↔ q)) (h' : p) : ¬ q := by tauto example (p q : Prop) [decidable q] [decidable p] (h : ¬ (p ↔ q)) (h' : q) : ¬ p := by tauto example (p q : Prop) [decidable q] [decidable p] (h : ¬ (p ↔ q)) (h' : ¬ q) : p := by tauto example (p q : Prop) [decidable q] [decidable p] (h : ¬ (p ↔ q)) (h' : ¬ q) (h'' : ¬ p) : false := by tauto example (p q r : Prop) [decidable q] [decidable p] (h : p ↔ q) (h' : r ↔ q) (h'' : ¬ r) : ¬ p := by tauto example (p q r : Prop) [decidable q] [decidable p] (h : p ↔ q) (h' : r ↔ q) : p ↔ r := by tauto example (p q r : Prop) [decidable p] [decidable q] [decidable r] (h : ¬ p = q) (h' : r = q) : p ↔ ¬ r := by tauto section modulo_symmetry variables {p q r : Prop} {α : Type} {x y : α} [decidable_eq α] variables [decidable p] [decidable q] [decidable r] variables (h : x = y) variables (h'' : (p ∧ q ↔ q ∨ r) ↔ (r ∧ p ↔ r ∨ q)) include h include h'' example (h' : ¬ y = x) : p ∧ q := by tauto example (h' : p ∧ ¬ y = x) : p ∧ q := by tauto example : y = x := by tauto example (h' : ¬ x = y) : p ∧ q := by tauto example : x = y := by tauto end modulo_symmetry end tauto₃ section wlog example {x y : ℕ} (a : x = 1) : true := begin suffices : false, trivial, wlog h : x = y, { guard_target x = y ∨ y = x, admit }, { guard_hyp h := x = y, guard_hyp a := x = 1, admit } end example {x y : ℕ} : true := begin suffices : false, trivial, wlog h : x ≤ y, { guard_hyp h := x ≤ y, guard_target false, admit } end example {x y z : ℕ} : true := begin suffices : false, trivial, wlog h : x ≤ y + z, { guard_target x ≤ y + z ∨ x ≤ z + y, admit }, { guard_hyp h := x ≤ y + z, guard_target false, admit } end example {x y z : ℕ} : true := begin suffices : false, trivial, wlog : x ≤ y + z using x y, { guard_target x ≤ y + z ∨ y ≤ x + z, admit }, { guard_hyp case := x ≤ y + z, guard_target false, admit }, end example {x : ℕ} (S₀ S₁ : set ℕ) (P : ℕ → Prop) (h : x ∈ S₀ ∪ S₁) : true := begin suffices : false, trivial, wlog h' : x ∈ S₀ using S₀ S₁, { guard_target x ∈ S₀ ∨ x ∈ S₁, admit }, { guard_hyp h := x ∈ S₀ ∪ S₁, guard_hyp h' := x ∈ S₀, admit } end example {n m i : ℕ} {p : ℕ → ℕ → ℕ → Prop} : true := begin suffices : false, trivial, wlog : p n m i using [n m i, n i m, i n m], { guard_target p n m i ∨ p n i m ∨ p i n m, admit }, { guard_hyp case := p n m i, admit } end example {n m i : ℕ} {p : ℕ → Prop} : true := begin suffices : false, trivial, wlog : p n using [n m i, m n i, i n m], { guard_target p n ∨ p m ∨ p i, admit }, { guard_hyp case := p n, admit } end example {n m i : ℕ} {p : ℕ → ℕ → Prop} {q : ℕ → ℕ → ℕ → Prop} : true := begin suffices : q n m i, trivial, have h : p n i ∨ p i m ∨ p m i, from sorry, wlog : p n i := h using n m i, { guard_hyp h := p n i, guard_target q n m i, admit }, { guard_hyp h := p i m, guard_hyp this := q i m n, guard_target q n m i, admit }, { guard_hyp h := p m i, guard_hyp this := q m i n, guard_target q n m i, admit }, end example (X : Type) (A B C : set X) : A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) := begin ext x, split, { intro hyp, cases hyp, wlog x_in : x ∈ B using B C, { assumption }, { exact or.inl ⟨hyp_left, x_in⟩ } }, { intro hyp, wlog x_in : x ∈ A ∩ B using B C, { assumption }, { exact ⟨x_in.left, or.inl x_in.right⟩ } } end example (X : Type) (A B C : set X) : A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) := begin ext x, split, { intro hyp, wlog x_in : x ∈ B := hyp.2 using B C, { exact or.inl ⟨hyp.1, x_in⟩ } }, { intro hyp, wlog x_in : x ∈ A ∩ B := hyp using B C, { exact ⟨x_in.left, or.inl x_in.right⟩ } } end example (X : Type) (A B C : set X) : A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) := begin ext x, split, { intro hyp, cases hyp, wlog x_in : x ∈ B := hyp_right using B C, { exact or.inl ⟨hyp_left, x_in⟩ }, }, { intro hyp, wlog x_in : x ∈ A ∩ B := hyp using B C, { exact ⟨x_in.left, or.inl x_in.right⟩ } } end end wlog example (m n p q : nat) (h : m + n = p) : true := begin have : m + n = q, { generalize_hyp h' : m + n = x at h, guard_hyp h' := m + n = x, guard_hyp h := x = p, guard_target m + n = q, admit }, have : m + n = q, { generalize_hyp h' : m + n = x at h ⊢, guard_hyp h' := m + n = x, guard_hyp h := x = p, guard_target x = q, admit }, trivial end example (α : Sort*) (L₁ L₂ L₃ : list α) (H : L₁ ++ L₂ = L₃) : true := begin have : L₁ ++ L₂ = L₂, { generalize_hyp h : L₁ ++ L₂ = L at H, induction L with hd tl ih, case list.nil { tactic.cleanup, change list.nil = L₃ at H, admit }, case list.cons { change hd :: tl = L₃ at H, admit } }, trivial end section convert open set variables {α β : Type} local attribute [simp] private lemma singleton_inter_singleton_eq_empty {x y : α} : ({x} ∩ {y} = (∅ : set α)) ↔ x ≠ y := by simp [singleton_inter_eq_empty] example {f : β → α} {x y : α} (h : x ≠ y) : f ⁻¹' {x} ∩ f ⁻¹' {y} = ∅ := begin have : {x} ∩ {y} = (∅ : set α) := by simpa using h, convert preimage_empty, rw [←preimage_inter,this], end end convert section rcases universe u variables {α β γ : Type u} example (x : α × β × γ) : true := begin rcases x with ⟨a, b, c⟩, { guard_hyp a := α, guard_hyp b := β, guard_hyp c := γ, trivial } end example (x : α × β × γ) : true := begin rcases x with ⟨a, ⟨b, c⟩⟩, { guard_hyp a := α, guard_hyp b := β, guard_hyp c := γ, trivial } end example (x : (α × β) × γ) : true := begin rcases x with ⟨⟨a, b⟩, c⟩, { guard_hyp a := α, guard_hyp b := β, guard_hyp c := γ, trivial } end example (x : inhabited α × option β ⊕ γ) : true := begin rcases x with ⟨⟨a⟩, _ | b⟩ | c, { guard_hyp a := α, trivial }, { guard_hyp a := α, guard_hyp b := β, trivial }, { guard_hyp c := γ, trivial } end example (x y : ℕ) (h : x = y) : true := begin rcases x with _|⟨⟩|z, { guard_hyp h := nat.zero = y, trivial }, { guard_hyp h := nat.succ nat.zero = y, trivial }, { guard_hyp z := ℕ, guard_hyp h := z.succ.succ = y, trivial }, end -- from equiv.sum_empty example (s : α ⊕ empty) : true := begin rcases s with _ | ⟨⟨⟩⟩, { guard_hyp s := α, trivial } end end rcases section ext example (x y : ℕ) : true := begin have : x = y, { ext <|> admit }, have : x = y, { ext i <|> admit }, have : x = y, { ext 1 <|> admit }, trivial end example (X Y : ℕ × ℕ) (h : X.1 = Y.1) (h : X.2 = Y.2) : X = Y := begin ext ; assumption end example (X Y : (ℕ → ℕ) × ℕ) (h : ∀ i, X.1 i = Y.1 i) (h : X.2 = Y.2) : X = Y := begin ext x ; solve_by_elim, end example (X Y : ℕ → ℕ × ℕ) (h : ∀ i, X i = Y i) : true := begin have : X = Y, { ext 1 with i, guard_target X i = Y i, admit }, have : X = Y, { ext i, guard_target (X i).fst = (Y i).fst, admit, guard_target (X i).snd = (Y i).snd, admit, }, have : X = Y, { ext 1, guard_target X x = Y x, admit }, trivial, end def my_foo {α} (x : semigroup α) (y : group α) : true := trivial example {α : Type} : true := begin have : true, { refine_struct (@my_foo α { .. } { .. } ), -- 9 goals guard_tags _field mul semigroup, admit, -- case semigroup, mul -- α : Type -- ⊢ α → α → α guard_tags _field mul_assoc semigroup, admit, -- case semigroup, mul_assoc -- α : Type -- ⊢ ∀ (a b c : α), a * b * c = a * (b * c) guard_tags _field mul group, admit, -- case group, mul -- α : Type -- ⊢ α → α → α guard_tags _field mul_assoc group, admit, -- case group, mul_assoc -- α : Type -- ⊢ ∀ (a b c : α), a * b * c = a * (b * c) guard_tags _field one group, admit, -- case group, one -- α : Type -- ⊢ α guard_tags _field one_mul group, admit, -- case group, one_mul -- α : Type -- ⊢ ∀ (a : α), 1 * a = a guard_tags _field mul_one group, admit, -- case group, mul_one -- α : Type -- ⊢ ∀ (a : α), a * 1 = a guard_tags _field inv group, admit, -- case group, inv -- α : Type -- ⊢ α → α guard_tags _field mul_left_inv group, admit, -- case group, mul_left_inv -- α : Type -- ⊢ ∀ (a : α), a⁻¹ * a = 1 }, trivial end def my_bar {α} (x : semigroup α) (y : group α) (i j : α) : α := i example {α : Type} : true := begin have : monoid α, { refine_struct { mul := my_bar { .. } { .. } }, guard_tags _field mul semigroup, admit, guard_tags _field mul_assoc semigroup, admit, guard_tags _field mul group, admit, guard_tags _field mul_assoc group, admit, guard_tags _field one group, admit, guard_tags _field one_mul group, admit, guard_tags _field mul_one group, admit, guard_tags _field inv group, admit, guard_tags _field mul_left_inv group, admit, guard_tags _field mul_assoc monoid, admit, guard_tags _field one monoid, admit, guard_tags _field one_mul monoid, admit, guard_tags _field mul_one monoid, admit, }, trivial end end ext section apply_rules example {a b c d e : nat} (h1 : a ≤ b) (h2 : c ≤ d) (h3 : 0 ≤ e) : a + c * e + a + c + 0 ≤ b + d * e + b + d + e := add_le_add (add_le_add (add_le_add (add_le_add h1 (mul_le_mul_of_nonneg_right h2 h3)) h1 ) h2) h3 example {a b c d e : nat} (h1 : a ≤ b) (h2 : c ≤ d) (h3 : 0 ≤ e) : a + c * e + a + c + 0 ≤ b + d * e + b + d + e := by apply_rules [add_le_add, mul_le_mul_of_nonneg_right] @[user_attribute] meta def mono_rules : user_attribute := { name := `mono_rules, descr := "lemmas usable to prove monotonicity" } attribute [mono_rules] add_le_add mul_le_mul_of_nonneg_right example {a b c d e : nat} (h1 : a ≤ b) (h2 : c ≤ d) (h3 : 0 ≤ e) : a + c * e + a + c + 0 ≤ b + d * e + b + d + e := by apply_rules [mono_rules] example {a b c d e : nat} (h1 : a ≤ b) (h2 : c ≤ d) (h3 : 0 ≤ e) : a + c * e + a + c + 0 ≤ b + d * e + b + d + e := by apply_rules mono_rules end apply_rules section h_generalize variables {α β γ φ ψ : Type} (f : α → α → α → φ → γ) (x y : α) (a b : β) (z : φ) (h₀ : β = α) (h₁ : β = α) (h₂ : φ = β) (hx : x == a) (hy : y == b) (hz : z == a) include f x y z a b hx hy hz example : f x y x z = f (eq.rec_on h₀ a) (cast h₀ b) (eq.mpr h₁.symm a) (eq.mpr h₂ a) := begin guard_hyp_nums 16, h_generalize hp : a == p with hh, guard_hyp_nums 19, guard_hyp' hh := β = α, guard_target f x y x z = f p (cast h₀ b) p (eq.mpr h₂ a), h_generalize hq : _ == q, guard_hyp_nums 21, guard_target f x y x z = f p q p (eq.mpr h₂ a), h_generalize _ : _ == r, guard_hyp_nums 23, guard_target f x y x z = f p q p r, casesm* [_ == _, _ = _], refl end end h_generalize section h_generalize variables {α β γ φ ψ : Type} (f : list α → list α → γ) (x : list α) (a : list β) (z : φ) (h₀ : β = α) (h₁ : list β = list α) (hx : x == a) include f x z a hx h₀ h₁ example : true := begin have : f x x = f (eq.rec_on h₀ a) (cast h₁ a), { guard_hyp_nums 11, h_generalize : a == p with _, guard_hyp_nums 13, guard_hyp' h := β = α, guard_target f x x = f p (cast h₁ a), h_generalize! : a == q , guard_hyp_nums 13, guard_target ∀ q, f x x = f p q, casesm* [_ == _, _ = _], success_if_fail { refl }, admit }, trivial end end h_generalize
85fc8bc73fd0146dc041ef951da9d31faf5850bb
4fa118f6209450d4e8d058790e2967337811b2b5
/src/examples/empty.lean
95858f3eca515415761ae5c5dbfd7f7d2336a617
[ "Apache-2.0" ]
permissive
leanprover-community/lean-perfectoid-spaces
16ab697a220ed3669bf76311daa8c466382207f7
95a6520ce578b30a80b4c36e36ab2d559a842690
refs/heads/master
1,639,557,829,139
1,638,797,866,000
1,638,797,866,000
135,769,296
96
10
Apache-2.0
1,638,797,866,000
1,527,892,754,000
Lean
UTF-8
Lean
false
false
1,991
lean
import data.padics.padic_numbers import for_mathlib.punit_instances import perfectoid_space /-! # An example of a perfectoid space In this file we show that the empty space is perfectoid. Every nonempty example requires a non-trivial amount of mathematical effort. -/ /-- The structure presheaf on the empty space. -/ def CLVRS.empty_presheaf : presheaf_of_topological_rings empty := { F := λ _, unit, res := λ _ _ _ _, (), Hid := λ U, by {funext x, cases x, refl}, Hcomp := λ U V W _ _, rfl, Fring := λ x, punit.comm_ring, res_is_ring_hom := λ U V _, { map_one := rfl, map_mul := λ _ _, rfl, map_add := λ _ _, rfl }, Ftop := λ U, by apply_instance, Ftop_ring := λ U, by apply_instance, res_continuous := λ U V _, continuous_of_discrete_topology } /-- The structure sheaf on the empty space. -/ def CLVRS.empty_sheaf : sheaf_of_topological_rings empty := { F := CLVRS.empty_presheaf, locality := by {rintro _ _ ⟨s⟩ ⟨t⟩ _, refl}, gluing := by {intros _ _ c _, use (), intro i, cases c i, refl}, homeo := begin rintros ⟨U, HU⟩ ⟨γ, Uis, _⟩ c d, dsimp at *, change set unit at c, rcases subset_subsingleton c with rfl|rfl, { convert is_open_empty, exact set.image_empty _ }, { convert is_open_univ, apply set.image_univ_of_surjective, rintro ⟨s, hs⟩, use (), apply subtype.eq, funext i, show () = s i, apply subsingleton.elim, }, end } /--The empty CLVRS-/ def CLVRS.empty : CLVRS := { space := empty, sheaf' := CLVRS.empty_sheaf, complete := λ U, { complete := λ f hf, begin use (), rintro V HV, convert f.univ_sets, funext x, cases x, show _ = true, rw eq_true, exact mem_of_nhds HV, end }, valuation := by rintro ⟨⟩, local_stalks := by rintro ⟨⟩, supp_maximal := by rintro ⟨⟩ } example : PerfectoidSpace ⟨37, by norm_num⟩ := ⟨CLVRS.empty, by rintro ⟨⟩⟩
7079a02b6de964b52fa3e1516f8b41bc4b03150a
ee8cdbabf07f77e7be63a449b8483ce308d37218
/lean/src/test/mathd-algebra-156.lean
ebc531cd6cee50db145a9de6c158c53089d31da9
[ "MIT", "Apache-2.0" ]
permissive
zeta1999/miniF2F
6d66c75d1c18152e224d07d5eed57624f731d4b7
c1ba9629559c5273c92ec226894baa0c1ce27861
refs/heads/main
1,681,897,460,642
1,620,646,361,000
1,620,646,361,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
360
lean
/- Copyright (c) 2021 OpenAI. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kunhao Zheng -/ import data.real.basic example (x y : ℝ) (f g : ℝ → ℝ) (h₀ : ∀t, f t = t^4) (h₁ : ∀t, g t = 5 * t^2 - 6) (h₂ : f x = g x) (h₃ : f y = g y) (h₄ : x^2 < y^2) : y^2 - x^2 = 1 := begin sorry end
9610ef05f3005a38a4b10f39a120fcb65602c043
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/analysis/normed/group/add_torsor.lean
0e1f3e2a565d97de69c42bf6dd7323af5897b0b3
[ "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
10,793
lean
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers, Yury Kudryashov -/ import analysis.normed.group.basic import linear_algebra.affine_space.affine_subspace import linear_algebra.affine_space.midpoint /-! # Torsors of additive normed group actions. This file defines torsors of additive normed group actions, with a metric space structure. The motivating case is Euclidean affine spaces. -/ noncomputable theory open_locale nnreal topological_space open filter /-- A `normed_add_torsor V P` is a torsor of an additive seminormed group action by a `seminormed_add_comm_group V` on points `P`. We bundle the pseudometric space structure and require the distance to be the same as results from the norm (which in fact implies the distance yields a pseudometric space, but bundling just the distance and using an instance for the pseudometric space results in type class problems). -/ class normed_add_torsor (V : out_param $ Type*) (P : Type*) [out_param $ seminormed_add_comm_group V] [pseudo_metric_space P] extends add_torsor V P := (dist_eq_norm' : ∀ (x y : P), dist x y = ‖(x -ᵥ y : V)‖) /-- Shortcut instance to help typeclass inference out. -/ @[priority 100] instance normed_add_torsor.to_add_torsor' {V P : Type*} [normed_add_comm_group V] [metric_space P] [normed_add_torsor V P] : add_torsor V P := normed_add_torsor.to_add_torsor variables {α V P W Q : Type*} [seminormed_add_comm_group V] [pseudo_metric_space P] [normed_add_torsor V P] [normed_add_comm_group W] [metric_space Q] [normed_add_torsor W Q] /-- A `seminormed_add_comm_group` is a `normed_add_torsor` over itself. -/ @[priority 100] instance seminormed_add_comm_group.to_normed_add_torsor : normed_add_torsor V V := { dist_eq_norm' := dist_eq_norm } /-- A nonempty affine subspace of a `normed_add_torsor` is itself a `normed_add_torsor`. -/ @[nolint fails_quickly] -- Because of the add_torsor.nonempty instance. instance affine_subspace.to_normed_add_torsor {R : Type*} [ring R] [module R V] (s : affine_subspace R P) [nonempty s] : normed_add_torsor s.direction s := { dist_eq_norm' := λ x y, normed_add_torsor.dist_eq_norm' ↑x ↑y, ..affine_subspace.to_add_torsor s } include V section variables (V W) /-- The distance equals the norm of subtracting two points. In this lemma, it is necessary to have `V` as an explicit argument; otherwise `rw dist_eq_norm_vsub` sometimes doesn't work. -/ lemma dist_eq_norm_vsub (x y : P) : dist x y = ‖x -ᵥ y‖ := normed_add_torsor.dist_eq_norm' x y /-- The distance equals the norm of subtracting two points. In this lemma, it is necessary to have `V` as an explicit argument; otherwise `rw dist_eq_norm_vsub'` sometimes doesn't work. -/ lemma dist_eq_norm_vsub' (x y : P) : dist x y = ‖y -ᵥ x‖ := (dist_comm _ _).trans (dist_eq_norm_vsub _ _ _) end @[simp] lemma dist_vadd_cancel_left (v : V) (x y : P) : dist (v +ᵥ x) (v +ᵥ y) = dist x y := by rw [dist_eq_norm_vsub V, dist_eq_norm_vsub V, vadd_vsub_vadd_cancel_left] @[simp] lemma dist_vadd_cancel_right (v₁ v₂ : V) (x : P) : dist (v₁ +ᵥ x) (v₂ +ᵥ x) = dist v₁ v₂ := by rw [dist_eq_norm_vsub V, dist_eq_norm, vadd_vsub_vadd_cancel_right] @[simp] lemma dist_vadd_left (v : V) (x : P) : dist (v +ᵥ x) x = ‖v‖ := by simp [dist_eq_norm_vsub V _ x] @[simp] lemma dist_vadd_right (v : V) (x : P) : dist x (v +ᵥ x) = ‖v‖ := by rw [dist_comm, dist_vadd_left] /-- Isometry between the tangent space `V` of a (semi)normed add torsor `P` and `P` given by addition/subtraction of `x : P`. -/ @[simps] def isometric.vadd_const (x : P) : V ≃ᵢ P := { to_equiv := equiv.vadd_const x, isometry_to_fun := isometry.of_dist_eq $ λ _ _, dist_vadd_cancel_right _ _ _ } section variable (P) /-- Self-isometry of a (semi)normed add torsor given by addition of a constant vector `x`. -/ @[simps] def isometric.const_vadd (x : V) : P ≃ᵢ P := { to_equiv := equiv.const_vadd P x, isometry_to_fun := isometry.of_dist_eq $ λ _ _, dist_vadd_cancel_left _ _ _ } end @[simp] lemma dist_vsub_cancel_left (x y z : P) : dist (x -ᵥ y) (x -ᵥ z) = dist y z := by rw [dist_eq_norm, vsub_sub_vsub_cancel_left, dist_comm, dist_eq_norm_vsub V] /-- Isometry between the tangent space `V` of a (semi)normed add torsor `P` and `P` given by subtraction from `x : P`. -/ @[simps] def isometric.const_vsub (x : P) : P ≃ᵢ V := { to_equiv := equiv.const_vsub x, isometry_to_fun := isometry.of_dist_eq $ λ y z, dist_vsub_cancel_left _ _ _ } @[simp] lemma dist_vsub_cancel_right (x y z : P) : dist (x -ᵥ z) (y -ᵥ z) = dist x y := (isometric.vadd_const z).symm.dist_eq x y section pointwise open_locale pointwise @[simp] lemma vadd_ball (x : V) (y : P) (r : ℝ) : x +ᵥ metric.ball y r = metric.ball (x +ᵥ y) r := (isometric.const_vadd P x).image_ball y r @[simp] lemma vadd_closed_ball (x : V) (y : P) (r : ℝ) : x +ᵥ metric.closed_ball y r = metric.closed_ball (x +ᵥ y) r := (isometric.const_vadd P x).image_closed_ball y r @[simp] lemma vadd_sphere (x : V) (y : P) (r : ℝ) : x +ᵥ metric.sphere y r = metric.sphere (x +ᵥ y) r := (isometric.const_vadd P x).image_sphere y r end pointwise lemma dist_vadd_vadd_le (v v' : V) (p p' : P) : dist (v +ᵥ p) (v' +ᵥ p') ≤ dist v v' + dist p p' := by simpa using dist_triangle (v +ᵥ p) (v' +ᵥ p) (v' +ᵥ p') lemma dist_vsub_vsub_le (p₁ p₂ p₃ p₄ : P) : dist (p₁ -ᵥ p₂) (p₃ -ᵥ p₄) ≤ dist p₁ p₃ + dist p₂ p₄ := by { rw [dist_eq_norm, vsub_sub_vsub_comm, dist_eq_norm_vsub V, dist_eq_norm_vsub V], exact norm_sub_le _ _ } lemma nndist_vadd_vadd_le (v v' : V) (p p' : P) : nndist (v +ᵥ p) (v' +ᵥ p') ≤ nndist v v' + nndist p p' := by simp only [← nnreal.coe_le_coe, nnreal.coe_add, ← dist_nndist, dist_vadd_vadd_le] lemma nndist_vsub_vsub_le (p₁ p₂ p₃ p₄ : P) : nndist (p₁ -ᵥ p₂) (p₃ -ᵥ p₄) ≤ nndist p₁ p₃ + nndist p₂ p₄ := by simp only [← nnreal.coe_le_coe, nnreal.coe_add, ← dist_nndist, dist_vsub_vsub_le] lemma edist_vadd_vadd_le (v v' : V) (p p' : P) : edist (v +ᵥ p) (v' +ᵥ p') ≤ edist v v' + edist p p' := by { simp only [edist_nndist], apply_mod_cast nndist_vadd_vadd_le } lemma edist_vsub_vsub_le (p₁ p₂ p₃ p₄ : P) : edist (p₁ -ᵥ p₂) (p₃ -ᵥ p₄) ≤ edist p₁ p₃ + edist p₂ p₄ := by { simp only [edist_nndist], apply_mod_cast nndist_vsub_vsub_le } omit V /-- The pseudodistance defines a pseudometric space structure on the torsor. This is not an instance because it depends on `V` to define a `metric_space P`. -/ def pseudo_metric_space_of_normed_add_comm_group_of_add_torsor (V P : Type*) [seminormed_add_comm_group V] [add_torsor V P] : pseudo_metric_space P := { dist := λ x y, ‖(x -ᵥ y : V)‖, dist_self := λ x, by simp, dist_comm := λ x y, by simp only [←neg_vsub_eq_vsub_rev y x, norm_neg], dist_triangle := begin intros x y z, change ‖x -ᵥ z‖ ≤ ‖x -ᵥ y‖ + ‖y -ᵥ z‖, rw ←vsub_add_vsub_cancel, apply norm_add_le end } /-- The distance defines a metric space structure on the torsor. This is not an instance because it depends on `V` to define a `metric_space P`. -/ def metric_space_of_normed_add_comm_group_of_add_torsor (V P : Type*) [normed_add_comm_group V] [add_torsor V P] : metric_space P := { dist := λ x y, ‖(x -ᵥ y : V)‖, dist_self := λ x, by simp, eq_of_dist_eq_zero := λ x y h, by simpa using h, dist_comm := λ x y, by simp only [←neg_vsub_eq_vsub_rev y x, norm_neg], dist_triangle := begin intros x y z, change ‖x -ᵥ z‖ ≤ ‖x -ᵥ y‖ + ‖y -ᵥ z‖, rw ←vsub_add_vsub_cancel, apply norm_add_le end } include V lemma lipschitz_with.vadd [pseudo_emetric_space α] {f : α → V} {g : α → P} {Kf Kg : ℝ≥0} (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf + Kg) (f +ᵥ g) := λ x y, calc edist (f x +ᵥ g x) (f y +ᵥ g y) ≤ edist (f x) (f y) + edist (g x) (g y) : edist_vadd_vadd_le _ _ _ _ ... ≤ Kf * edist x y + Kg * edist x y : add_le_add (hf x y) (hg x y) ... = (Kf + Kg) * edist x y : (add_mul _ _ _).symm lemma lipschitz_with.vsub [pseudo_emetric_space α] {f g : α → P} {Kf Kg : ℝ≥0} (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf + Kg) (f -ᵥ g) := λ x y, calc edist (f x -ᵥ g x) (f y -ᵥ g y) ≤ edist (f x) (f y) + edist (g x) (g y) : edist_vsub_vsub_le _ _ _ _ ... ≤ Kf * edist x y + Kg * edist x y : add_le_add (hf x y) (hg x y) ... = (Kf + Kg) * edist x y : (add_mul _ _ _).symm lemma uniform_continuous_vadd : uniform_continuous (λ x : V × P, x.1 +ᵥ x.2) := (lipschitz_with.prod_fst.vadd lipschitz_with.prod_snd).uniform_continuous lemma uniform_continuous_vsub : uniform_continuous (λ x : P × P, x.1 -ᵥ x.2) := (lipschitz_with.prod_fst.vsub lipschitz_with.prod_snd).uniform_continuous @[priority 100] instance normed_add_torsor.to_has_continuous_vadd : has_continuous_vadd V P := { continuous_vadd := uniform_continuous_vadd.continuous } lemma continuous_vsub : continuous (λ x : P × P, x.1 -ᵥ x.2) := uniform_continuous_vsub.continuous lemma filter.tendsto.vsub {l : filter α} {f g : α → P} {x y : P} (hf : tendsto f l (𝓝 x)) (hg : tendsto g l (𝓝 y)) : tendsto (f -ᵥ g) l (𝓝 (x -ᵥ y)) := (continuous_vsub.tendsto (x, y)).comp (hf.prod_mk_nhds hg) section variables [topological_space α] lemma continuous.vsub {f g : α → P} (hf : continuous f) (hg : continuous g) : continuous (f -ᵥ g) := continuous_vsub.comp (hf.prod_mk hg : _) lemma continuous_at.vsub {f g : α → P} {x : α} (hf : continuous_at f x) (hg : continuous_at g x) : continuous_at (f -ᵥ g) x := hf.vsub hg lemma continuous_within_at.vsub {f g : α → P} {x : α} {s : set α} (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) : continuous_within_at (f -ᵥ g) s x := hf.vsub hg end section variables {R : Type*} [ring R] [topological_space R] [module R V] [has_continuous_smul R V] lemma filter.tendsto.line_map {l : filter α} {f₁ f₂ : α → P} {g : α → R} {p₁ p₂ : P} {c : R} (h₁ : tendsto f₁ l (𝓝 p₁)) (h₂ : tendsto f₂ l (𝓝 p₂)) (hg : tendsto g l (𝓝 c)) : tendsto (λ x, affine_map.line_map (f₁ x) (f₂ x) (g x)) l (𝓝 $ affine_map.line_map p₁ p₂ c) := (hg.smul (h₂.vsub h₁)).vadd h₁ lemma filter.tendsto.midpoint [invertible (2:R)] {l : filter α} {f₁ f₂ : α → P} {p₁ p₂ : P} (h₁ : tendsto f₁ l (𝓝 p₁)) (h₂ : tendsto f₂ l (𝓝 p₂)) : tendsto (λ x, midpoint R (f₁ x) (f₂ x)) l (𝓝 $ midpoint R p₁ p₂) := h₁.line_map h₂ tendsto_const_nhds end
9cf1139df4bb7af0dd2b71370fa965c17d38e7f4
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/category_theory/category/preorder.lean
3055687f8ece27f7b9c7d58d17767fdd9b34359f
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,903
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stephen Morgan, Scott Morrison, Johannes Hölzl, Reid Barton -/ import category_theory.category.Cat import category_theory.category.default import category_theory.opposites import order.category.Preorder /-! # Preorders as categories We install a category instance on any preorder. This is not to be confused with the category _of_ preorders, defined in `order/category/Preorder`. We show that monotone functions between preorders correspond to functors of the associated categories. Furthermore, galois connections correspond to adjoint functors. ## Main definitions * `hom_of_le` and `le_of_hom` provide translations between inequalities in the preorder, and morphisms in the associated category. * `monotone.functor` is the functor associated to a monotone function. * `galois_connection.adjunction` is the adjunction associated to a galois connection. * `Preorder_to_Cat` is the functor embedding the category of preorders into `Cat`. -/ universes u v namespace preorder open category_theory /-- The category structure coming from a preorder. There is a morphism `X ⟶ Y` if and only if `X ≤ Y`. Because we don't allow morphisms to live in `Prop`, we have to define `X ⟶ Y` as `ulift (plift (X ≤ Y))`. See `category_theory.hom_of_le` and `category_theory.le_of_hom`. See https://stacks.math.columbia.edu/tag/00D3. -/ @[priority 100] -- see Note [lower instance priority] instance small_category (α : Type u) [preorder α] : small_category α := { hom := λ U V, ulift (plift (U ≤ V)), id := λ X, ⟨ ⟨ le_refl X ⟩ ⟩, comp := λ X Y Z f g, ⟨ ⟨ le_trans _ _ _ f.down.down g.down.down ⟩ ⟩ } end preorder namespace category_theory open opposite variables {X : Type u} [preorder X] /-- Express an inequality as a morphism in the corresponding preorder category. -/ def hom_of_le {x y : X} (h : x ≤ y) : x ⟶ y := ulift.up (plift.up h) alias hom_of_le ← has_le.le.hom @[simp] lemma hom_of_le_refl {x : X} : (le_refl x).hom = 𝟙 x := rfl @[simp] lemma hom_of_le_comp {x y z : X} (h : x ≤ y) (k : y ≤ z) : h.hom ≫ k.hom = (h.trans k).hom := rfl /-- Extract the underlying inequality from a morphism in a preorder category. -/ lemma le_of_hom {x y : X} (h : x ⟶ y) : x ≤ y := h.down.down alias le_of_hom ← quiver.hom.le @[simp] lemma le_of_hom_hom_of_le {x y : X} (h : x ≤ y) : h.hom.le = h := rfl @[simp] lemma hom_of_le_le_of_hom {x y : X} (h : x ⟶ y) : h.le.hom = h := by { cases h, cases h, refl, } /-- Construct a morphism in the opposite of a preorder category from an inequality. -/ def op_hom_of_le {x y : Xᵒᵖ} (h : unop x ≤ unop y) : y ⟶ x := h.hom.op lemma le_of_op_hom {x y : Xᵒᵖ} (h : x ⟶ y) : unop y ≤ unop x := h.unop.le end category_theory section variables {X : Type u} {Y : Type v} [preorder X] [preorder Y] /-- A monotone function between preorders induces a functor between the associated categories. -/ def monotone.functor {f : X → Y} (h : monotone f) : X ⥤ Y := { obj := f, map := λ x₁ x₂ g, (h g.le).hom } @[simp] lemma monotone.functor_obj {f : X → Y} (h : monotone f) : h.functor.obj = f := rfl /-- A galois connection between preorders induces an adjunction between the associated categories. -/ def galois_connection.adjunction {l : X → Y} {u : Y → X} (gc : galois_connection l u) : gc.monotone_l.functor ⊣ gc.monotone_u.functor := category_theory.adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, ⟨λ f, (gc.le_u f.le).hom, λ f, (gc.l_le f.le).hom, by tidy, by tidy⟩ } end namespace category_theory section preorder variables {X : Type u} {Y : Type v} [preorder X] [preorder Y] /-- A functor between preorder categories is monotone. -/ @[mono] lemma functor.monotone (f : X ⥤ Y) : monotone f.obj := λ x y hxy, (f.map hxy.hom).le /-- An adjunction between preorder categories induces a galois connection. -/ lemma adjunction.gc {L : X ⥤ Y} {R : Y ⥤ X} (adj : L ⊣ R) : galois_connection L.obj R.obj := λ x y, ⟨λ h, ((adj.hom_equiv x y).to_fun h.hom).le, λ h, ((adj.hom_equiv x y).inv_fun h.hom).le⟩ /-- The embedding of `Preorder` into `Cat`. -/ @[simps] def Preorder_to_Cat : Preorder.{u} ⥤ Cat := { obj := λ X, Cat.of X.1, map := λ X Y f, f.monotone.functor, map_id' := λ X, begin apply category_theory.functor.ext, tidy end, map_comp' := λ X Y Z f g, begin apply category_theory.functor.ext, tidy end } instance : faithful Preorder_to_Cat.{u} := { map_injective' := λ X Y f g h, begin ext x, exact functor.congr_obj h x end } instance : full Preorder_to_Cat.{u} := { preimage := λ X Y f, ⟨f.obj, f.monotone⟩, witness' := λ X Y f, begin apply category_theory.functor.ext, tidy end } end preorder section partial_order variables {X : Type u} {Y : Type v} [partial_order X] [partial_order Y] lemma iso.to_eq {x y : X} (f : x ≅ y) : x = y := le_antisymm f.hom.le f.inv.le /-- A categorical equivalence between partial orders is just an order isomorphism. -/ def equivalence.to_order_iso (e : X ≌ Y) : X ≃o Y := { to_fun := e.functor.obj, inv_fun := e.inverse.obj, left_inv := λ a, (e.unit_iso.app a).to_eq.symm, right_inv := λ b, (e.counit_iso.app b).to_eq, map_rel_iff' := λ a a', ⟨λ h, ((equivalence.unit e).app a ≫ e.inverse.map h.hom ≫ (equivalence.unit_inv e).app a').le, λ (h : a ≤ a'), (e.functor.map h.hom).le⟩, } -- `@[simps]` on `equivalence.to_order_iso` produces lemmas that fail the `simp_nf` linter, -- so we provide them by hand: @[simp] lemma equivalence.to_order_iso_apply (e : X ≌ Y) (x : X) : e.to_order_iso x = e.functor.obj x := rfl @[simp] lemma equivalence.to_order_iso_symm_apply (e : X ≌ Y) (y : Y) : e.to_order_iso.symm y = e.inverse.obj y := rfl end partial_order end category_theory
e2ff6718c254f3b4690aaedad6a56caeeafd583e
1a61aba1b67cddccce19532a9596efe44be4285f
/hott/core.hlean
dfaa9b191823cd23bf08048f0dc4aa4b66c2be63
[ "Apache-2.0" ]
permissive
eigengrau/lean
07986a0f2548688c13ba36231f6cdbee82abf4c6
f8a773be1112015e2d232661ce616d23f12874d0
refs/heads/master
1,610,939,198,566
1,441,352,386,000
1,441,352,494,000
41,903,576
0
0
null
1,441,352,210,000
1,441,352,210,000
null
UTF-8
Lean
false
false
255
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn The core of the HoTT library -/ import types import cubical import hit.circle import algebra.hott
63b13f086c277ee7b07cafb3dd74dbc625df1532
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/normed_space/affine_isometry.lean
859a7d258b3b726fba8cbe863935e5bc65611b66
[ "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
21,954
lean
/- Copyright (c) 2021 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import analysis.normed_space.add_torsor import analysis.normed_space.linear_isometry /-! # Affine isometries In this file we define `affine_isometry 𝕜 P P₂` to be an affine isometric embedding of normed add-torsors `P` into `P₂` over normed `𝕜`-spaces and `affine_isometry_equiv` to be an affine isometric equivalence between `P` and `P₂`. We also prove basic lemmas and provide convenience constructors. The choice of these lemmas and constructors is closely modelled on those for the `linear_isometry` and `affine_map` theories. Since many elementary properties don't require `∥x∥ = 0 → x = 0` we initially set up the theory for `semi_normed_group` and specialize to `normed_group` only when needed. ## Notation We introduce the notation `P →ᵃⁱ[𝕜] P₂` for `affine_isometry 𝕜 P P₂`, and `P ≃ᵃⁱ[𝕜] P₂` for `affine_isometry_equiv 𝕜 P P₂`. In contrast with the notation `→ₗᵢ` for linear isometries, `≃ᵢ` for isometric equivalences, etc., the "i" here is a superscript. This is for aesthetic reasons to match the superscript "a" (note that in mathlib `→ᵃ` is an affine map, since `→ₐ` has been taken by algebra-homomorphisms.) -/ open function set variables (𝕜 : Type*) {V V₁ V₂ V₃ V₄ : Type*} {P₁ : Type*} (P P₂ : Type*) {P₃ P₄ : Type*} [normed_field 𝕜] [semi_normed_group V] [semi_normed_group V₁] [semi_normed_group V₂] [semi_normed_group V₃] [semi_normed_group V₄] [normed_space 𝕜 V] [normed_space 𝕜 V₁] [normed_space 𝕜 V₂] [normed_space 𝕜 V₃] [normed_space 𝕜 V₄] [pseudo_metric_space P] [metric_space P₁] [pseudo_metric_space P₂] [pseudo_metric_space P₃] [pseudo_metric_space P₄] [normed_add_torsor V P] [normed_add_torsor V₁ P₁] [normed_add_torsor V₂ P₂] [normed_add_torsor V₃ P₃] [normed_add_torsor V₄ P₄] include V V₂ /-- An `𝕜`-affine isometric embedding of one normed add-torsor over a normed `𝕜`-space into another. -/ structure affine_isometry extends P →ᵃ[𝕜] P₂ := (norm_map : ∀ x : V, ∥linear x∥ = ∥x∥) omit V V₂ variables {𝕜 P P₂} -- `→ᵃᵢ` would be more consistent with the linear isometry notation, but it is uglier notation P ` →ᵃⁱ[`:25 𝕜:25 `] `:0 P₂:0 := affine_isometry 𝕜 P P₂ namespace affine_isometry variables (f : P →ᵃⁱ[𝕜] P₂) /-- The underlying linear map of an affine isometry is in fact a linear isometry. -/ protected def linear_isometry : V →ₗᵢ[𝕜] V₂ := { norm_map' := f.norm_map, .. f.linear } @[simp] lemma linear_eq_linear_isometry : f.linear = f.linear_isometry.to_linear_map := by { ext, refl } include V V₂ instance : has_coe_to_fun (P →ᵃⁱ[𝕜] P₂) (λ _, P → P₂) := ⟨λ f, f.to_fun⟩ omit V V₂ @[simp] lemma coe_to_affine_map : ⇑f.to_affine_map = f := rfl include V V₂ lemma to_affine_map_injective : injective (to_affine_map : (P →ᵃⁱ[𝕜] P₂) → (P →ᵃ[𝕜] P₂)) | ⟨f, _⟩ ⟨g, _⟩ rfl := rfl lemma coe_fn_injective : @injective (P →ᵃⁱ[𝕜] P₂) (P → P₂) coe_fn := affine_map.coe_fn_injective.comp to_affine_map_injective @[ext] lemma ext {f g : P →ᵃⁱ[𝕜] P₂} (h : ∀ x, f x = g x) : f = g := coe_fn_injective $ funext h omit V V₂ end affine_isometry namespace linear_isometry variables (f : V →ₗᵢ[𝕜] V₂) /-- Reinterpret a linear isometry as an affine isometry. -/ def to_affine_isometry : V →ᵃⁱ[𝕜] V₂ := { norm_map := f.norm_map, .. f.to_linear_map.to_affine_map } @[simp] lemma coe_to_affine_isometry : ⇑(f.to_affine_isometry : V →ᵃⁱ[𝕜] V₂) = f := rfl @[simp] lemma to_affine_isometry_linear_isometry : f.to_affine_isometry.linear_isometry = f := by { ext, refl } -- somewhat arbitrary choice of simp direction @[simp] lemma to_affine_isometry_to_affine_map : f.to_affine_isometry.to_affine_map = f.to_linear_map.to_affine_map := rfl end linear_isometry namespace affine_isometry /-- We use `f₁` when we need the domain to be a `normed_space`. -/ variables (f : P →ᵃⁱ[𝕜] P₂) (f₁ : P₁ →ᵃⁱ[𝕜] P₂) @[simp] lemma map_vadd (p : P) (v : V) : f (v +ᵥ p) = f.linear_isometry v +ᵥ f p := f.to_affine_map.map_vadd p v @[simp] lemma map_vsub (p1 p2 : P) : f.linear_isometry (p1 -ᵥ p2) = f p1 -ᵥ f p2 := f.to_affine_map.linear_map_vsub p1 p2 @[simp] lemma dist_map (x y : P) : dist (f x) (f y) = dist x y := by rw [dist_eq_norm_vsub V₂, dist_eq_norm_vsub V, ← map_vsub, f.linear_isometry.norm_map] @[simp] lemma nndist_map (x y : P) : nndist (f x) (f y) = nndist x y := by simp [nndist_dist] @[simp] lemma edist_map (x y : P) : edist (f x) (f y) = edist x y := by simp [edist_dist] protected lemma isometry : isometry f := f.edist_map protected lemma injective : injective f₁ := f₁.isometry.injective @[simp] lemma map_eq_iff {x y : P₁} : f₁ x = f₁ y ↔ x = y := f₁.injective.eq_iff lemma map_ne {x y : P₁} (h : x ≠ y) : f₁ x ≠ f₁ y := f₁.injective.ne h protected lemma lipschitz : lipschitz_with 1 f := f.isometry.lipschitz protected lemma antilipschitz : antilipschitz_with 1 f := f.isometry.antilipschitz @[continuity] protected lemma continuous : continuous f := f.isometry.continuous lemma ediam_image (s : set P) : emetric.diam (f '' s) = emetric.diam s := f.isometry.ediam_image s lemma ediam_range : emetric.diam (range f) = emetric.diam (univ : set P) := f.isometry.ediam_range lemma diam_image (s : set P) : metric.diam (f '' s) = metric.diam s := f.isometry.diam_image s lemma diam_range : metric.diam (range f) = metric.diam (univ : set P) := f.isometry.diam_range @[simp] lemma comp_continuous_iff {α : Type*} [topological_space α] {g : α → P} : continuous (f ∘ g) ↔ continuous g := f.isometry.comp_continuous_iff include V /-- The identity affine isometry. -/ def id : P →ᵃⁱ[𝕜] P := ⟨affine_map.id 𝕜 P, λ x, rfl⟩ @[simp] lemma coe_id : ⇑(id : P →ᵃⁱ[𝕜] P) = _root_.id := rfl @[simp] lemma id_apply (x : P) : (affine_isometry.id : P →ᵃⁱ[𝕜] P) x = x := rfl @[simp] lemma id_to_affine_map : (id.to_affine_map : P →ᵃ[𝕜] P) = affine_map.id 𝕜 P := rfl instance : inhabited (P →ᵃⁱ[𝕜] P) := ⟨id⟩ include V₂ V₃ /-- Composition of affine isometries. -/ def comp (g : P₂ →ᵃⁱ[𝕜] P₃) (f : P →ᵃⁱ[𝕜] P₂) : P →ᵃⁱ[𝕜] P₃ := ⟨g.to_affine_map.comp f.to_affine_map, λ x, (g.norm_map _).trans (f.norm_map _)⟩ @[simp] lemma coe_comp (g : P₂ →ᵃⁱ[𝕜] P₃) (f : P →ᵃⁱ[𝕜] P₂) : ⇑(g.comp f) = g ∘ f := rfl omit V V₂ V₃ @[simp] lemma id_comp : (id : P₂ →ᵃⁱ[𝕜] P₂).comp f = f := ext $ λ x, rfl @[simp] lemma comp_id : f.comp id = f := ext $ λ x, rfl include V V₂ V₃ V₄ lemma comp_assoc (f : P₃ →ᵃⁱ[𝕜] P₄) (g : P₂ →ᵃⁱ[𝕜] P₃) (h : P →ᵃⁱ[𝕜] P₂) : (f.comp g).comp h = f.comp (g.comp h) := rfl omit V₂ V₃ V₄ instance : monoid (P →ᵃⁱ[𝕜] P) := { one := id, mul := comp, mul_assoc := comp_assoc, one_mul := id_comp, mul_one := comp_id } @[simp] lemma coe_one : ⇑(1 : P →ᵃⁱ[𝕜] P) = _root_.id := rfl @[simp] lemma coe_mul (f g : P →ᵃⁱ[𝕜] P) : ⇑(f * g) = f ∘ g := rfl end affine_isometry -- remark: by analogy with the `linear_isometry` file from which this is adapted, there should -- follow here a section defining an "inclusion" affine isometry from `p : affine_subspace 𝕜 P` -- into `P`; we omit this for now variables (𝕜 P P₂) include V V₂ /-- A affine isometric equivalence between two normed vector spaces. -/ structure affine_isometry_equiv extends P ≃ᵃ[𝕜] P₂ := (norm_map : ∀ x, ∥linear x∥ = ∥x∥) variables {𝕜 P P₂} omit V V₂ -- `≃ᵃᵢ` would be more consistent with the linear isometry equiv notation, but it is uglier notation P ` ≃ᵃⁱ[`:25 𝕜:25 `] `:0 P₂:0 := affine_isometry_equiv 𝕜 P P₂ namespace affine_isometry_equiv variables (e : P ≃ᵃⁱ[𝕜] P₂) /-- The underlying linear equiv of an affine isometry equiv is in fact a linear isometry equiv. -/ protected def linear_isometry_equiv : V ≃ₗᵢ[𝕜] V₂ := { norm_map' := e.norm_map, .. e.linear } @[simp] lemma linear_eq_linear_isometry : e.linear = e.linear_isometry_equiv.to_linear_equiv := by { ext, refl } include V V₂ instance : has_coe_to_fun (P ≃ᵃⁱ[𝕜] P₂) (λ _, P → P₂) := ⟨λ f, f.to_fun⟩ @[simp] lemma coe_mk (e : P ≃ᵃ[𝕜] P₂) (he : ∀ x, ∥e.linear x∥ = ∥x∥) : ⇑(mk e he) = e := rfl @[simp] lemma coe_to_affine_equiv (e : P ≃ᵃⁱ[𝕜] P₂) : ⇑e.to_affine_equiv = e := rfl lemma to_affine_equiv_injective : injective (to_affine_equiv : (P ≃ᵃⁱ[𝕜] P₂) → (P ≃ᵃ[𝕜] P₂)) | ⟨e, _⟩ ⟨_, _⟩ rfl := rfl @[ext] lemma ext {e e' : P ≃ᵃⁱ[𝕜] P₂} (h : ∀ x, e x = e' x) : e = e' := to_affine_equiv_injective $ affine_equiv.ext h omit V V₂ /-- Reinterpret a `affine_isometry_equiv` as a `affine_isometry`. -/ def to_affine_isometry : P →ᵃⁱ[𝕜] P₂ := ⟨e.1.to_affine_map, e.2⟩ @[simp] lemma coe_to_affine_isometry : ⇑e.to_affine_isometry = e := rfl /-- Construct an affine isometry equivalence by verifying the relation between the map and its linear part at one base point. Namely, this function takes a map `e : P₁ → P₂`, a linear isometry equivalence `e' : V₁ ≃ᵢₗ[k] V₂`, and a point `p` such that for any other point `p'` we have `e p' = e' (p' -ᵥ p) +ᵥ e p`. -/ def mk' (e : P₁ → P₂) (e' : V₁ ≃ₗᵢ[𝕜] V₂) (p : P₁) (h : ∀ p' : P₁, e p' = e' (p' -ᵥ p) +ᵥ e p) : P₁ ≃ᵃⁱ[𝕜] P₂ := { norm_map := e'.norm_map, .. affine_equiv.mk' e e'.to_linear_equiv p h } @[simp] lemma coe_mk' (e : P₁ → P₂) (e' : V₁ ≃ₗᵢ[𝕜] V₂) (p h) : ⇑(mk' e e' p h) = e := rfl @[simp] lemma linear_isometry_equiv_mk' (e : P₁ → P₂) (e' : V₁ ≃ₗᵢ[𝕜] V₂) (p h) : (mk' e e' p h).linear_isometry_equiv = e' := by { ext, refl } end affine_isometry_equiv namespace linear_isometry_equiv variables (e : V ≃ₗᵢ[𝕜] V₂) /-- Reinterpret a linear isometry equiv as an affine isometry equiv. -/ def to_affine_isometry_equiv : V ≃ᵃⁱ[𝕜] V₂ := { norm_map := e.norm_map, .. e.to_linear_equiv.to_affine_equiv } @[simp] lemma coe_to_affine_isometry_equiv : ⇑(e.to_affine_isometry_equiv : V ≃ᵃⁱ[𝕜] V₂) = e := rfl @[simp] lemma to_affine_isometry_equiv_linear_isometry_equiv : e.to_affine_isometry_equiv.linear_isometry_equiv = e := by { ext, refl } -- somewhat arbitrary choice of simp direction @[simp] lemma to_affine_isometry_equiv_to_affine_equiv : e.to_affine_isometry_equiv.to_affine_equiv = e.to_linear_equiv.to_affine_equiv := rfl -- somewhat arbitrary choice of simp direction @[simp] lemma to_affine_isometry_equiv_to_affine_isometry : e.to_affine_isometry_equiv.to_affine_isometry = e.to_linear_isometry.to_affine_isometry := rfl end linear_isometry_equiv namespace affine_isometry_equiv variables (e : P ≃ᵃⁱ[𝕜] P₂) protected lemma isometry : isometry e := e.to_affine_isometry.isometry /-- Reinterpret a `affine_isometry_equiv` as an `isometric`. -/ def to_isometric : P ≃ᵢ P₂ := ⟨e.to_affine_equiv.to_equiv, e.isometry⟩ @[simp] lemma coe_to_isometric : ⇑e.to_isometric = e := rfl include V V₂ lemma range_eq_univ (e : P ≃ᵃⁱ[𝕜] P₂) : set.range e = set.univ := by { rw ← coe_to_isometric, exact isometric.range_eq_univ _, } omit V V₂ /-- Reinterpret a `affine_isometry_equiv` as an `homeomorph`. -/ def to_homeomorph : P ≃ₜ P₂ := e.to_isometric.to_homeomorph @[simp] lemma coe_to_homeomorph : ⇑e.to_homeomorph = e := rfl protected lemma continuous : continuous e := e.isometry.continuous protected lemma continuous_at {x} : continuous_at e x := e.continuous.continuous_at protected lemma continuous_on {s} : continuous_on e s := e.continuous.continuous_on protected lemma continuous_within_at {s x} : continuous_within_at e s x := e.continuous.continuous_within_at variables (𝕜 P) include V /-- Identity map as a `affine_isometry_equiv`. -/ def refl : P ≃ᵃⁱ[𝕜] P := ⟨affine_equiv.refl 𝕜 P, λ x, rfl⟩ variables {𝕜 P} instance : inhabited (P ≃ᵃⁱ[𝕜] P) := ⟨refl 𝕜 P⟩ @[simp] lemma coe_refl : ⇑(refl 𝕜 P) = id := rfl @[simp] lemma to_affine_equiv_refl : (refl 𝕜 P).to_affine_equiv = affine_equiv.refl 𝕜 P := rfl @[simp] lemma to_isometric_refl : (refl 𝕜 P).to_isometric = isometric.refl P := rfl @[simp] lemma to_homeomorph_refl : (refl 𝕜 P).to_homeomorph = homeomorph.refl P := rfl omit V /-- The inverse `affine_isometry_equiv`. -/ def symm : P₂ ≃ᵃⁱ[𝕜] P := { norm_map := e.linear_isometry_equiv.symm.norm_map, .. e.to_affine_equiv.symm } @[simp] lemma apply_symm_apply (x : P₂) : e (e.symm x) = x := e.to_affine_equiv.apply_symm_apply x @[simp] lemma symm_apply_apply (x : P) : e.symm (e x) = x := e.to_affine_equiv.symm_apply_apply x @[simp] lemma symm_symm : e.symm.symm = e := ext $ λ x, rfl @[simp] lemma to_affine_equiv_symm : e.to_affine_equiv.symm = e.symm.to_affine_equiv := rfl @[simp] lemma to_isometric_symm : e.to_isometric.symm = e.symm.to_isometric := rfl @[simp] lemma to_homeomorph_symm : e.to_homeomorph.symm = e.symm.to_homeomorph := rfl include V₃ /-- Composition of `affine_isometry_equiv`s as a `affine_isometry_equiv`. -/ def trans (e' : P₂ ≃ᵃⁱ[𝕜] P₃) : P ≃ᵃⁱ[𝕜] P₃ := ⟨e.to_affine_equiv.trans e'.to_affine_equiv, λ x, (e'.norm_map _).trans (e.norm_map _)⟩ include V V₂ @[simp] lemma coe_trans (e₁ : P ≃ᵃⁱ[𝕜] P₂) (e₂ : P₂ ≃ᵃⁱ[𝕜] P₃) : ⇑(e₁.trans e₂) = e₂ ∘ e₁ := rfl omit V V₂ V₃ @[simp] lemma trans_refl : e.trans (refl 𝕜 P₂) = e := ext $ λ x, rfl @[simp] lemma refl_trans : (refl 𝕜 P).trans e = e := ext $ λ x, rfl @[simp] lemma self_trans_symm : e.trans e.symm = refl 𝕜 P := ext e.symm_apply_apply @[simp] lemma symm_trans_self : e.symm.trans e = refl 𝕜 P₂ := ext e.apply_symm_apply include V V₂ V₃ @[simp] lemma coe_symm_trans (e₁ : P ≃ᵃⁱ[𝕜] P₂) (e₂ : P₂ ≃ᵃⁱ[𝕜] P₃) : ⇑(e₁.trans e₂).symm = e₁.symm ∘ e₂.symm := rfl include V₄ lemma trans_assoc (ePP₂ : P ≃ᵃⁱ[𝕜] P₂) (eP₂G : P₂ ≃ᵃⁱ[𝕜] P₃) (eGG' : P₃ ≃ᵃⁱ[𝕜] P₄) : ePP₂.trans (eP₂G.trans eGG') = (ePP₂.trans eP₂G).trans eGG' := rfl omit V₂ V₃ V₄ /-- The group of affine isometries of a `normed_add_torsor`, `P`. -/ instance : group (P ≃ᵃⁱ[𝕜] P) := { mul := λ e₁ e₂, e₂.trans e₁, one := refl _ _, inv := symm, one_mul := trans_refl, mul_one := refl_trans, mul_assoc := λ _ _ _, trans_assoc _ _ _, mul_left_inv := self_trans_symm } @[simp] lemma coe_one : ⇑(1 : P ≃ᵃⁱ[𝕜] P) = id := rfl @[simp] lemma coe_mul (e e' : P ≃ᵃⁱ[𝕜] P) : ⇑(e * e') = e ∘ e' := rfl @[simp] lemma coe_inv (e : P ≃ᵃⁱ[𝕜] P) : ⇑(e⁻¹) = e.symm := rfl omit V @[simp] lemma map_vadd (p : P) (v : V) : e (v +ᵥ p) = e.linear_isometry_equiv v +ᵥ e p := e.to_affine_isometry.map_vadd p v @[simp] lemma map_vsub (p1 p2 : P) : e.linear_isometry_equiv (p1 -ᵥ p2) = e p1 -ᵥ e p2 := e.to_affine_isometry.map_vsub p1 p2 @[simp] lemma dist_map (x y : P) : dist (e x) (e y) = dist x y := e.to_affine_isometry.dist_map x y @[simp] lemma edist_map (x y : P) : edist (e x) (e y) = edist x y := e.to_affine_isometry.edist_map x y protected lemma bijective : bijective e := e.1.bijective protected lemma injective : injective e := e.1.injective protected lemma surjective : surjective e := e.1.surjective @[simp] lemma map_eq_iff {x y : P} : e x = e y ↔ x = y := e.injective.eq_iff lemma map_ne {x y : P} (h : x ≠ y) : e x ≠ e y := e.injective.ne h protected lemma lipschitz : lipschitz_with 1 e := e.isometry.lipschitz protected lemma antilipschitz : antilipschitz_with 1 e := e.isometry.antilipschitz @[simp] lemma ediam_image (s : set P) : emetric.diam (e '' s) = emetric.diam s := e.isometry.ediam_image s @[simp] lemma diam_image (s : set P) : metric.diam (e '' s) = metric.diam s := e.isometry.diam_image s variables {α : Type*} [topological_space α] @[simp] lemma comp_continuous_on_iff {f : α → P} {s : set α} : continuous_on (e ∘ f) s ↔ continuous_on f s := e.isometry.comp_continuous_on_iff @[simp] lemma comp_continuous_iff {f : α → P} : continuous (e ∘ f) ↔ continuous f := e.isometry.comp_continuous_iff section constructions variables (𝕜) /-- The map `v ↦ v +ᵥ p` as an affine isometric equivalence between `V` and `P`. -/ def vadd_const (p : P) : V ≃ᵃⁱ[𝕜] P := { norm_map := λ x, rfl, .. affine_equiv.vadd_const 𝕜 p } variables {𝕜} include V @[simp] lemma coe_vadd_const (p : P) : ⇑(vadd_const 𝕜 p) = λ v, v +ᵥ p := rfl @[simp] lemma coe_vadd_const_symm (p : P) : ⇑(vadd_const 𝕜 p).symm = λ p', p' -ᵥ p := rfl @[simp] lemma vadd_const_to_affine_equiv (p : P) : (vadd_const 𝕜 p).to_affine_equiv = affine_equiv.vadd_const 𝕜 p := rfl omit V variables (𝕜) /-- `p' ↦ p -ᵥ p'` as an affine isometric equivalence. -/ def const_vsub (p : P) : P ≃ᵃⁱ[𝕜] V := { norm_map := norm_neg, .. affine_equiv.const_vsub 𝕜 p } variables {𝕜} include V @[simp] lemma coe_const_vsub (p : P) : ⇑(const_vsub 𝕜 p) = (-ᵥ) p := rfl @[simp] lemma symm_const_vsub (p : P) : (const_vsub 𝕜 p).symm = (linear_isometry_equiv.neg 𝕜).to_affine_isometry_equiv.trans (vadd_const 𝕜 p) := by { ext, refl } omit V variables (𝕜 P) /-- Translation by `v` (that is, the map `p ↦ v +ᵥ p`) as an affine isometric automorphism of `P`. -/ def const_vadd (v : V) : P ≃ᵃⁱ[𝕜] P := { norm_map := λ x, rfl, .. affine_equiv.const_vadd 𝕜 P v } variables {𝕜 P} @[simp] lemma coe_const_vadd (v : V) : ⇑(const_vadd 𝕜 P v : P ≃ᵃⁱ[𝕜] P) = (+ᵥ) v := rfl @[simp] lemma const_vadd_zero : const_vadd 𝕜 P (0:V) = refl 𝕜 P := ext $ zero_vadd V include 𝕜 V /-- The map `g` from `V` to `V₂` corresponding to a map `f` from `P` to `P₂`, at a base point `p`, is an isometry if `f` is one. -/ lemma vadd_vsub {f : P → P₂} (hf : isometry f) {p : P} {g : V → V₂} (hg : ∀ v, g v = f (v +ᵥ p) -ᵥ f p) : isometry g := begin convert (vadd_const 𝕜 (f p)).symm.isometry.comp (hf.comp (vadd_const 𝕜 p).isometry), exact funext hg end omit 𝕜 variables (𝕜) /-- Point reflection in `x` as an affine isometric automorphism. -/ def point_reflection (x : P) : P ≃ᵃⁱ[𝕜] P := (const_vsub 𝕜 x).trans (vadd_const 𝕜 x) variables {𝕜} lemma point_reflection_apply (x y : P) : (point_reflection 𝕜 x) y = x -ᵥ y +ᵥ x := rfl @[simp] lemma point_reflection_to_affine_equiv (x : P) : (point_reflection 𝕜 x).to_affine_equiv = affine_equiv.point_reflection 𝕜 x := rfl @[simp] lemma point_reflection_self (x : P) : point_reflection 𝕜 x x = x := affine_equiv.point_reflection_self 𝕜 x lemma point_reflection_involutive (x : P) : function.involutive (point_reflection 𝕜 x) := equiv.point_reflection_involutive x @[simp] lemma point_reflection_symm (x : P) : (point_reflection 𝕜 x).symm = point_reflection 𝕜 x := to_affine_equiv_injective $ affine_equiv.point_reflection_symm 𝕜 x @[simp] lemma dist_point_reflection_fixed (x y : P) : dist (point_reflection 𝕜 x y) x = dist y x := by rw [← (point_reflection 𝕜 x).dist_map y x, point_reflection_self] lemma dist_point_reflection_self' (x y : P) : dist (point_reflection 𝕜 x y) y = ∥bit0 (x -ᵥ y)∥ := by rw [point_reflection_apply, dist_eq_norm_vsub V, vadd_vsub_assoc, bit0] lemma dist_point_reflection_self (x y : P) : dist (point_reflection 𝕜 x y) y = ∥(2:𝕜)∥ * dist x y := by rw [dist_point_reflection_self', ← two_smul' 𝕜 (x -ᵥ y), norm_smul, ← dist_eq_norm_vsub V] lemma point_reflection_fixed_iff [invertible (2:𝕜)] {x y : P} : point_reflection 𝕜 x y = y ↔ y = x := affine_equiv.point_reflection_fixed_iff_of_module 𝕜 variables [normed_space ℝ V] lemma dist_point_reflection_self_real (x y : P) : dist (point_reflection ℝ x y) y = 2 * dist x y := by { rw [dist_point_reflection_self, real.norm_two] } @[simp] lemma point_reflection_midpoint_left (x y : P) : point_reflection ℝ (midpoint ℝ x y) x = y := affine_equiv.point_reflection_midpoint_left x y @[simp] lemma point_reflection_midpoint_right (x y : P) : point_reflection ℝ (midpoint ℝ x y) y = x := affine_equiv.point_reflection_midpoint_right x y end constructions end affine_isometry_equiv include V V₂ /-- If `f` is an affine map, then its linear part is continuous iff `f` is continuous. -/ lemma affine_map.continuous_linear_iff {f : P →ᵃ[𝕜] P₂} : continuous f.linear ↔ continuous f := begin inhabit P, have : (f.linear : V → V₂) = (affine_isometry_equiv.vadd_const 𝕜 $ f default).to_homeomorph.symm ∘ f ∘ (affine_isometry_equiv.vadd_const 𝕜 default).to_homeomorph, { ext v, simp }, rw this, simp only [homeomorph.comp_continuous_iff, homeomorph.comp_continuous_iff'], end /-- If `f` is an affine map, then its linear part is an open map iff `f` is an open map. -/ lemma affine_map.is_open_map_linear_iff {f : P →ᵃ[𝕜] P₂} : is_open_map f.linear ↔ is_open_map f := begin inhabit P, have : (f.linear : V → V₂) = (affine_isometry_equiv.vadd_const 𝕜 $ f default).to_homeomorph.symm ∘ f ∘ (affine_isometry_equiv.vadd_const 𝕜 default).to_homeomorph, { ext v, simp }, rw this, simp only [homeomorph.comp_is_open_map_iff, homeomorph.comp_is_open_map_iff'], end
ec701e94e3e2cd3fcc117c9c27dd4ef9fbbc0483
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Compiler/ImplementedByAttr.lean
cc017e309d35eca9d8da3ed32243019745bc6fb0
[ "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
2,052
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.Attributes import Lean.Declaration import Lean.MonadEnv import Lean.Elab.InfoTree namespace Lean.Compiler builtin_initialize implementedByAttr : ParametricAttribute Name ← registerParametricAttribute { name := `implementedBy descr := "name of the Lean (probably unsafe) function that implements opaque constant" getParam := fun declName stx => do let decl ← getConstInfo declName let fnNameStx ← Attribute.Builtin.getIdent stx let fnName ← Elab.resolveGlobalConstNoOverloadWithInfo fnNameStx let fnDecl ← getConstInfo fnName unless decl.levelParams.length == fnDecl.levelParams.length do throwError "invalid 'implementedBy' argument '{fnName}', '{fnName}' has {fnDecl.levelParams.length} universe level parameter(s), but '{declName}' has {decl.levelParams.length}" let declType := decl.type let fnType := fnDecl.instantiateTypeLevelParams (decl.levelParams.map mkLevelParam) unless declType == fnType do throwError "invalid 'implementedBy' argument '{fnName}', '{fnName}' has type{indentExpr fnType}\nbut '{declName}' has type{indentExpr declType}" if decl.name == fnDecl.name then throwError "invalid 'implementedBy' argument '{fnName}', function cannot be implemented by itself" return fnName } @[export lean_get_implemented_by] def getImplementedBy? (env : Environment) (declName : Name) : Option Name := implementedByAttr.getParam? env declName def setImplementedBy (env : Environment) (declName : Name) (impName : Name) : Except String Environment := implementedByAttr.setParam env declName impName end Compiler def setImplementedBy {m} [Monad m] [MonadEnv m] [MonadError m] (declName : Name) (impName : Name) : m Unit := do let env ← getEnv match Compiler.setImplementedBy env declName impName with | Except.ok env => setEnv env | Except.error ex => throwError ex end Lean
23a728ed85725f2e6e14eb842fedf78405b17bd6
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/topology/Top/opens.lean
57e8869b2fc47edf47ca9c57998f2e84ffde6fcc
[ "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
3,944
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 topology.Top.basic import category_theory.natural_isomorphism import category_theory.opposites import category_theory.eq_to_hom import topology.opens open category_theory open topological_space open opposite universe u namespace topological_space.opens variables {X Y Z : Top.{u}} instance opens_category : category.{u+1} (opens X) := { hom := λ U V, ulift (plift (U ≤ V)), id := λ X, ⟨ ⟨ le_refl X ⟩ ⟩, comp := λ X Y Z f g, ⟨ ⟨ le_trans f.down.down g.down.down ⟩ ⟩ } def to_Top (X : Top.{u}) : opens X ⥤ Top := { obj := λ U, ⟨U.val, infer_instance⟩, map := λ U V i, ⟨λ x, ⟨x.1, i.down.down x.2⟩, (embedding.continuous_iff embedding_subtype_val).2 continuous_induced_dom⟩ } /-- `opens.map f` gives the functor from open sets in Y to open set in X, given by taking preimages under f. -/ def map (f : X ⟶ Y) : opens Y ⥤ opens X := { obj := λ U, ⟨ f.val ⁻¹' U.val, f.property _ U.property ⟩, map := λ U V i, ⟨ ⟨ λ a b, i.down.down b ⟩ ⟩ }. @[simp] lemma map_obj (f : X ⟶ Y) (U) (p) : (map f).obj ⟨U, p⟩ = ⟨ f.val ⁻¹' U, f.property _ p ⟩ := rfl @[simp] lemma map_id_obj' (U) (p) : (map (𝟙 X)).obj ⟨U, p⟩ = ⟨U, p⟩ := rfl @[simp] lemma map_id_obj (U : opens X) : (map (𝟙 X)).obj U = U := by { ext, refl } -- not quite `rfl`, since we don't have eta for records @[simp] lemma map_id_obj_unop (U : (opens X)ᵒᵖ) : (map (𝟙 X)).obj (unop U) = unop U := by simp @[simp] lemma op_map_id_obj (U : (opens X)ᵒᵖ) : (map (𝟙 X)).op.obj U = U := by simp section variable (X) def map_id : map (𝟙 X) ≅ functor.id (opens X) := { hom := { app := λ U, eq_to_hom (map_id_obj U) }, inv := { app := λ U, eq_to_hom (map_id_obj U).symm } } @[simp] lemma map_id_hom_app (U) : (map_id X).hom.app U = eq_to_hom (map_id_obj U) := rfl @[simp] lemma map_id_inv_app (U) : (map_id X).inv.app U = eq_to_hom (map_id_obj U).symm := rfl end @[simp] lemma map_comp_obj' (f : X ⟶ Y) (g : Y ⟶ Z) (U) (p) : (map (f ≫ g)).obj ⟨U, p⟩ = (map f).obj ((map g).obj ⟨U, p⟩) := rfl @[simp] lemma map_comp_obj (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).obj U = (map f).obj ((map g).obj U) := by { ext, refl } -- not quite `rfl`, since we don't have eta for records @[simp] lemma map_comp_obj_unop (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).obj (unop U) = (map f).obj ((map g).obj (unop U)) := by simp @[simp] lemma op_map_comp_obj (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).op.obj U = (map f).op.obj ((map g).op.obj U) := by simp def map_comp (f : X ⟶ Y) (g : Y ⟶ Z) : map (f ≫ g) ≅ map g ⋙ map f := { hom := { app := λ U, eq_to_hom (map_comp_obj f g U) }, inv := { app := λ U, eq_to_hom (map_comp_obj f g U).symm } } @[simp] lemma map_comp_hom_app (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map_comp f g).hom.app U = eq_to_hom (map_comp_obj f g U) := rfl @[simp] lemma map_comp_inv_app (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map_comp f g).inv.app U = eq_to_hom (map_comp_obj f g U).symm := rfl -- We could make f g implicit here, but it's nice to be able to see when -- they are the identity (often!) def map_iso (f g : X ⟶ Y) (h : f = g) : map f ≅ map g := nat_iso.of_components (λ U, eq_to_iso (congr_fun (congr_arg functor.obj (congr_arg map h)) U) ) (by obviously) @[simp] lemma map_iso_refl (f : X ⟶ Y) (h) : map_iso f f h = iso.refl (map _) := rfl @[simp] lemma map_iso_hom_app (f g : X ⟶ Y) (h : f = g) (U : opens Y) : (map_iso f g h).hom.app U = eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h)) U) := rfl @[simp] lemma map_iso_inv_app (f g : X ⟶ Y) (h : f = g) (U : opens Y) : (map_iso f g h).inv.app U = eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h.symm)) U) := rfl end topological_space.opens
96726a4d7b7816ef1eabb505c5a27e8c1bc01325
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/data/quot.lean
374ab2920a20e8c384d8f5f277dfe1d171f74e88
[ "Apache-2.0" ]
permissive
DanielFabian/mathlib
efc3a50b5dde303c59eeb6353ef4c35a345d7112
f520d07eba0c852e96fe26da71d85bf6d40fcc2a
refs/heads/master
1,668,739,922,971
1,595,201,756,000
1,595,201,756,000
279,469,476
0
0
null
1,594,696,604,000
1,594,696,604,000
null
UTF-8
Lean
false
false
16,160
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import logic.relator /-! # Quotients -- extends the core library -/ variables {α : Sort*} {β : Sort*} namespace setoid lemma ext {α : Sort*} : ∀{s t : setoid α}, (∀a b, @setoid.r α s a b ↔ @setoid.r α t a b) → s = t | ⟨r, _⟩ ⟨p, _⟩ eq := have r = p, from funext $ assume a, funext $ assume b, propext $ eq a b, by subst this end setoid namespace quot variables {ra : α → α → Prop} {rb : β → β → Prop} {φ : quot ra → quot rb → Sort*} local notation `⟦`:max a `⟧` := quot.mk _ a instance [inhabited α] : inhabited (quot ra) := ⟨⟦default _⟧⟩ /-- Recursion on two `quotient` arguments `a` and `b`, result type depends on `⟦a⟧` and `⟦b⟧`. -/ protected def hrec_on₂ (qa : quot ra) (qb : quot rb) (f : Π a b, φ ⟦a⟧ ⟦b⟧) (ca : ∀ {b a₁ a₂}, ra a₁ a₂ → f a₁ b == f a₂ b) (cb : ∀ {a b₁ b₂}, rb b₁ b₂ → f a b₁ == f a b₂) : φ qa qb := quot.hrec_on qa (λ a, quot.hrec_on qb (f a) (λ b₁ b₂ pb, cb pb)) $ λ a₁ a₂ pa, quot.induction_on qb $ λ b, calc @quot.hrec_on _ _ (φ _) ⟦b⟧ (f a₁) (@cb _) == f a₁ b : by simp ... == f a₂ b : ca pa ... == @quot.hrec_on _ _ (φ _) ⟦b⟧ (f a₂) (@cb _) : by simp /-- Map a function `f : α → β` such that `ra x y` implies `rb (f x) (f y)` to a map `quot ra → quot rb`. -/ protected def map (f : α → β) (h : (ra ⇒ rb) f f) : quot ra → quot rb := quot.lift (λ x, ⟦f x⟧) $ assume x y (h₁ : ra x y), quot.sound $ h h₁ /-- If `ra` is a subrelation of `ra'`, then we have a natural map `quot ra → quot ra'`. -/ protected def map_right {ra' : α → α → Prop} (h : ∀a₁ a₂, ra a₁ a₂ → ra' a₁ a₂) : quot ra → quot ra' := quot.map id h /-- weaken the relation of a quotient -/ def factor {α : Type*} (r s : α → α → Prop) (h : ∀ x y, r x y → s x y) : quot r → quot s := quot.lift (quot.mk s) (λ x y rxy, quot.sound (h x y rxy)) lemma factor_mk_eq {α : Type*} (r s : α → α → Prop) (h : ∀ x y, r x y → s x y) : factor r s h ∘ quot.mk _ = quot.mk _ := rfl end quot namespace quotient variables [sa : setoid α] [sb : setoid β] variables {φ : quotient sa → quotient sb → Sort*} instance [inhabited α] : inhabited (quotient sa) := ⟨⟦default _⟧⟩ /-- Induction on two `quotient` arguments `a` and `b`, result type depends on `⟦a⟧` and `⟦b⟧`. -/ protected def hrec_on₂ (qa : quotient sa) (qb : quotient sb) (f : Π a b, φ ⟦a⟧ ⟦b⟧) (c : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ == f a₂ b₂) : φ qa qb := quot.hrec_on₂ qa qb f (λ _ _ _ p, c _ _ _ _ p (setoid.refl _)) (λ _ _ _ p, c _ _ _ _ (setoid.refl _) p) /-- Map a function `f : α → β` that sends equivalent elements to equivalent elements to a function `quotient sa → quotient sb`. Useful to define unary operations on quotients. -/ protected def map (f : α → β) (h : ((≈) ⇒ (≈)) f f) : quotient sa → quotient sb := quot.map f h @[simp] lemma map_mk (f : α → β) (h : ((≈) ⇒ (≈)) f f) (x : α) : quotient.map f h (⟦x⟧ : quotient sa) = (⟦f x⟧ : quotient sb) := rfl variables {γ : Sort*} [sc : setoid γ] /-- Map a function `f : α → β → γ` that sends equivalent elements to equivalent elements to a function `f : quotient sa → quotient sb → quotient sc`. Useful to define binary operations on quotients. -/ protected def map₂ (f : α → β → γ) (h : ((≈) ⇒ (≈) ⇒ (≈)) f f) : quotient sa → quotient sb → quotient sc := quotient.lift₂ (λ x y, ⟦f x y⟧) (λ x₁ y₁ x₂ y₂ h₁ h₂, quot.sound $ h h₁ h₂) end quotient @[simp] theorem quotient.eq [r : setoid α] {x y : α} : ⟦x⟧ = ⟦y⟧ ↔ x ≈ y := ⟨quotient.exact, quotient.sound⟩ theorem forall_quotient_iff {α : Type*} [r : setoid α] {p : quotient r → Prop} : (∀a:quotient r, p a) ↔ (∀a:α, p ⟦a⟧) := ⟨assume h x, h _, assume h a, a.induction_on h⟩ @[simp] lemma quotient.lift_beta [s : setoid α] (f : α → β) (h : ∀ (a b : α), a ≈ b → f a = f b) (x : α) : quotient.lift f h (quotient.mk x) = f x := rfl @[simp] lemma quotient.lift_on_beta [s : setoid α] (f : α → β) (h : ∀ (a b : α), a ≈ b → f a = f b) (x : α) : quotient.lift_on (quotient.mk x) f h = f x := rfl /-- Choose an element of the equivalence class using the axiom of choice. Sound but noncomputable. -/ noncomputable def quot.out {r : α → α → Prop} (q : quot r) : α := classical.some (quot.exists_rep q) /-- Unwrap the VM representation of a quotient to obtain an element of the equivalence class. Computable but unsound. -/ meta def quot.unquot {r : α → α → Prop} : quot r → α := unchecked_cast @[simp] theorem quot.out_eq {r : α → α → Prop} (q : quot r) : quot.mk r q.out = q := classical.some_spec (quot.exists_rep q) /-- Choose an element of the equivalence class using the axiom of choice. Sound but noncomputable. -/ noncomputable def quotient.out [s : setoid α] : quotient s → α := quot.out @[simp] theorem quotient.out_eq [s : setoid α] (q : quotient s) : ⟦q.out⟧ = q := q.out_eq theorem quotient.mk_out [s : setoid α] (a : α) : ⟦a⟧.out ≈ a := quotient.exact (quotient.out_eq _) instance pi_setoid {ι : Sort*} {α : ι → Sort*} [∀ i, setoid (α i)] : setoid (Π i, α i) := { r := λ a b, ∀ i, a i ≈ b i, iseqv := ⟨ λ a i, setoid.refl _, λ a b h i, setoid.symm (h _), λ a b c h₁ h₂ i, setoid.trans (h₁ _) (h₂ _)⟩ } /-- Given a function `f : Π i, quotient (S i)`, returns the class of functions `Π i, α i` sending each `i` to an element of the class `f i`. -/ noncomputable def quotient.choice {ι : Type*} {α : ι → Type*} [S : Π i, setoid (α i)] (f : Π i, quotient (S i)) : @quotient (Π i, α i) (by apply_instance) := ⟦λ i, (f i).out⟧ theorem quotient.choice_eq {ι : Type*} {α : ι → Type*} [Π i, setoid (α i)] (f : Π i, α i) : quotient.choice (λ i, ⟦f i⟧) = ⟦f⟧ := quotient.sound $ λ i, quotient.mk_out _ lemma nonempty_quotient_iff (s : setoid α) : nonempty (quotient s) ↔ nonempty α := ⟨assume ⟨a⟩, quotient.induction_on a nonempty.intro, assume ⟨a⟩, ⟨⟦a⟧⟩⟩ /-- `trunc α` is the quotient of `α` by the always-true relation. This is related to the propositional truncation in HoTT, and is similar in effect to `nonempty α`, but unlike `nonempty α`, `trunc α` is data, so the VM representation is the same as `α`, and so this can be used to maintain computability. -/ def {u} trunc (α : Sort u) : Sort u := @quot α (λ _ _, true) theorem true_equivalence : @equivalence α (λ _ _, true) := ⟨λ _, trivial, λ _ _ _, trivial, λ _ _ _ _ _, trivial⟩ namespace trunc /-- Constructor for `trunc α` -/ def mk (a : α) : trunc α := quot.mk _ a instance [inhabited α] : inhabited (trunc α) := ⟨mk (default _)⟩ /-- Any constant function lifts to a function out of the truncation -/ def lift (f : α → β) (c : ∀ a b : α, f a = f b) : trunc α → β := quot.lift f (λ a b _, c a b) theorem ind {β : trunc α → Prop} : (∀ a : α, β (mk a)) → ∀ q : trunc α, β q := quot.ind protected theorem lift_beta (f : α → β) (c) (a : α) : lift f c (mk a) = f a := rfl /-- Lift a constant function on `q : trunc α`. -/ @[reducible, elab_as_eliminator] protected def lift_on (q : trunc α) (f : α → β) (c : ∀ a b : α, f a = f b) : β := lift f c q @[elab_as_eliminator] protected theorem induction_on {β : trunc α → Prop} (q : trunc α) (h : ∀ a, β (mk a)) : β q := ind h q theorem exists_rep (q : trunc α) : ∃ a : α, mk a = q := quot.exists_rep q attribute [elab_as_eliminator] protected theorem induction_on₂ {C : trunc α → trunc β → Prop} (q₁ : trunc α) (q₂ : trunc β) (h : ∀ a b, C (mk a) (mk b)) : C q₁ q₂ := trunc.induction_on q₁ $ λ a₁, trunc.induction_on q₂ (h a₁) protected theorem eq (a b : trunc α) : a = b := trunc.induction_on₂ a b (λ x y, quot.sound trivial) instance : subsingleton (trunc α) := ⟨trunc.eq⟩ def bind (q : trunc α) (f : α → trunc β) : trunc β := trunc.lift_on q f (λ a b, trunc.eq _ _) /-- A function `f : α → β` defines a function `map f : trunc α → trunc β`. -/ def map (f : α → β) (q : trunc α) : trunc β := bind q (trunc.mk ∘ f) instance : monad trunc := { pure := @trunc.mk, bind := @trunc.bind } instance : is_lawful_monad trunc := { id_map := λ α q, trunc.eq _ _, pure_bind := λ α β q f, rfl, bind_assoc := λ α β γ x f g, trunc.eq _ _ } variable {C : trunc α → Sort*} /-- Recursion/induction principle for `trunc`. -/ @[reducible, elab_as_eliminator] protected def rec (f : Π a, C (mk a)) (h : ∀ (a b : α), (eq.rec (f a) (trunc.eq (mk a) (mk b)) : C (mk b)) = f b) (q : trunc α) : C q := quot.rec f (λ a b _, h a b) q /-- A version of `trunc.rec` taking `q : trunc α` as the first argument. -/ @[reducible, elab_as_eliminator] protected def rec_on (q : trunc α) (f : Π a, C (mk a)) (h : ∀ (a b : α), (eq.rec (f a) (trunc.eq (mk a) (mk b)) : C (mk b)) = f b) : C q := trunc.rec f h q /-- A version of `trunc.rec_on` assuming the codomain is a `subsingleton`. -/ @[reducible, elab_as_eliminator] protected def rec_on_subsingleton [∀ a, subsingleton (C (mk a))] (q : trunc α) (f : Π a, C (mk a)) : C q := trunc.rec f (λ a b, subsingleton.elim _ (f b)) q /-- Noncomputably extract a representative of `trunc α` (using the axiom of choice). -/ noncomputable def out : trunc α → α := quot.out @[simp] theorem out_eq (q : trunc α) : mk q.out = q := trunc.eq _ _ end trunc theorem nonempty_of_trunc (q : trunc α) : nonempty α := let ⟨a, _⟩ := q.exists_rep in ⟨a⟩ namespace quotient variables {γ : Sort*} {φ : Sort*} {s₁ : setoid α} {s₂ : setoid β} {s₃ : setoid γ} /- Versions of quotient definitions and lemmas ending in `'` use unification instead of typeclass inference for inferring the `setoid` argument. This is useful when there are several different quotient relations on a type, for example quotient groups, rings and modules -/ /-- A version of `quotient.mk` taking `{s : setoid α}` as an implicit argument instead of an instance argument. -/ protected def mk' (a : α) : quotient s₁ := quot.mk s₁.1 a /-- A version of `quotient.lift_on` taking `{s : setoid α}` as an implicit argument instead of an instance argument. -/ @[elab_as_eliminator, reducible] protected def lift_on' (q : quotient s₁) (f : α → φ) (h : ∀ a b, @setoid.r α s₁ a b → f a = f b) : φ := quotient.lift_on q f h /-- A version of `quotient.lift_on₂` taking `{s₁ : setoid α} {s₂ : setoid β}` as implicit arguments instead of instance arguments. -/ @[elab_as_eliminator, reducible] protected def lift_on₂' (q₁ : quotient s₁) (q₂ : quotient s₂) (f : α → β → γ) (h : ∀ a₁ a₂ b₁ b₂, @setoid.r α s₁ a₁ b₁ → @setoid.r β s₂ a₂ b₂ → f a₁ a₂ = f b₁ b₂) : γ := quotient.lift_on₂ q₁ q₂ f h /-- A version of `quotient.ind` taking `{s : setoid α}` as an implicit argument instead of an instance argument. -/ @[elab_as_eliminator] protected lemma ind' {p : quotient s₁ → Prop} (h : ∀ a, p (quotient.mk' a)) (q : quotient s₁) : p q := quotient.ind h q /-- A version of `quotient.ind₂` taking `{s₁ : setoid α} {s₂ : setoid β}` as implicit arguments instead of instance arguments. -/ @[elab_as_eliminator] protected lemma ind₂' {p : quotient s₁ → quotient s₂ → Prop} (h : ∀ a₁ a₂, p (quotient.mk' a₁) (quotient.mk' a₂)) (q₁ : quotient s₁) (q₂ : quotient s₂) : p q₁ q₂ := quotient.ind₂ h q₁ q₂ /-- A version of `quotient.induction_on` taking `{s : setoid α}` as an implicit argument instead of an instance argument. -/ @[elab_as_eliminator] protected lemma induction_on' {p : quotient s₁ → Prop} (q : quotient s₁) (h : ∀ a, p (quotient.mk' a)) : p q := quotient.induction_on q h /-- A version of `quotient.induction_on₂` taking `{s₁ : setoid α} {s₂ : setoid β}` as implicit arguments instead of instance arguments. -/ @[elab_as_eliminator] protected lemma induction_on₂' {p : quotient s₁ → quotient s₂ → Prop} (q₁ : quotient s₁) (q₂ : quotient s₂) (h : ∀ a₁ a₂, p (quotient.mk' a₁) (quotient.mk' a₂)) : p q₁ q₂ := quotient.induction_on₂ q₁ q₂ h /-- A version of `quotient.induction_on₃` taking `{s₁ : setoid α} {s₂ : setoid β} {s₃ : setoid γ}` as implicit arguments instead of instance arguments. -/ @[elab_as_eliminator] protected lemma induction_on₃' {p : quotient s₁ → quotient s₂ → quotient s₃ → Prop} (q₁ : quotient s₁) (q₂ : quotient s₂) (q₃ : quotient s₃) (h : ∀ a₁ a₂ a₃, p (quotient.mk' a₁) (quotient.mk' a₂) (quotient.mk' a₃)) : p q₁ q₂ q₃ := quotient.induction_on₃ q₁ q₂ q₃ h /-- Recursion on a `quotient` argument `a`, result type depends on `⟦a⟧`. -/ protected def hrec_on' {φ : quotient s₁ → Sort*} (qa : quotient s₁) (f : Π a, φ (quotient.mk' a)) (c : ∀ a₁ a₂, a₁ ≈ a₂ → f a₁ == f a₂) : φ qa := quot.hrec_on qa f c @[simp] lemma hrec_on'_mk' {φ : quotient s₁ → Sort*} (f : Π a, φ (quotient.mk' a)) (c : ∀ a₁ a₂, a₁ ≈ a₂ → f a₁ == f a₂) (x : α) : (quotient.mk' x).hrec_on' f c = f x := rfl /-- Recursion on two `quotient` arguments `a` and `b`, result type depends on `⟦a⟧` and `⟦b⟧`. -/ protected def hrec_on₂' {φ : quotient s₁ → quotient s₂ → Sort*} (qa : quotient s₁) (qb : quotient s₂) (f : ∀ a b, φ (quotient.mk' a) (quotient.mk' b)) (c : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ == f a₂ b₂) : φ qa qb := quotient.hrec_on₂ qa qb f c @[simp] lemma hrec_on₂'_mk' {φ : quotient s₁ → quotient s₂ → Sort*} (f : ∀ a b, φ (quotient.mk' a) (quotient.mk' b)) (c : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ == f a₂ b₂) (x : α) (qb : quotient s₂) : (quotient.mk' x).hrec_on₂' qb f c = qb.hrec_on' (f x) (λ b₁ b₂, c _ _ _ _ (setoid.refl _)) := rfl /-- Map a function `f : α → β` that sends equivalent elements to equivalent elements to a function `quotient sa → quotient sb`. Useful to define unary operations on quotients. -/ protected def map' (f : α → β) (h : ((≈) ⇒ (≈)) f f) : quotient s₁ → quotient s₂ := quot.map f h @[simp] lemma map'_mk' (f : α → β) (h) (x : α) : (quotient.mk' x : quotient s₁).map' f h = (quotient.mk' (f x) : quotient s₂) := rfl /-- A version of `quotient.map₂` using curly braces and unification. -/ protected def map₂' (f : α → β → γ) (h : ((≈) ⇒ (≈) ⇒ (≈)) f f) : quotient s₁ → quotient s₂ → quotient s₃ := quotient.map₂ f h @[simp] lemma map₂'_mk' (f : α → β → γ) (h) (x : α) : (quotient.mk' x : quotient s₁).map₂' f h = (quotient.map' (f x) (h (setoid.refl x)) : quotient s₂ → quotient s₃) := rfl lemma exact' {a b : α} : (quotient.mk' a : quotient s₁) = quotient.mk' b → @setoid.r _ s₁ a b := quotient.exact lemma sound' {a b : α} : @setoid.r _ s₁ a b → @quotient.mk' α s₁ a = quotient.mk' b := quotient.sound @[simp] protected lemma eq' {a b : α} : @quotient.mk' α s₁ a = quotient.mk' b ↔ @setoid.r _ s₁ a b := quotient.eq /-- A version of `quotient.out` taking `{s₁ : setoid α}` as an implicit argument instead of an instance argument. -/ noncomputable def out' (a : quotient s₁) : α := quotient.out a @[simp] theorem out_eq' (q : quotient s₁) : quotient.mk' q.out' = q := q.out_eq theorem mk_out' (a : α) : @setoid.r α s₁ (quotient.mk' a : quotient s₁).out' a := quotient.exact (quotient.out_eq _) end quotient
9c5c204dd18f7c0e35bcf9acf574296b66571623
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/order/conditionally_complete_lattice.lean
254f3e7ce670c856a9ccedc9de3ac66edd3bfa02
[ "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
50,064
lean
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import data.set.intervals.ord_connected /-! # Theory of conditionally complete lattices. A conditionally complete lattice is a lattice in which every non-empty bounded subset s has a least upper bound and a greatest lower bound, denoted below by Sup s and Inf s. Typical examples are real, nat, int with their usual orders. The theory is very comparable to the theory of complete lattices, except that suitable boundedness and nonemptiness assumptions have to be added to most statements. We introduce two predicates bdd_above and bdd_below to express this boundedness, prove their basic properties, and then go on to prove most useful properties of Sup and Inf in conditionally complete lattices. To differentiate the statements between complete lattices and conditionally complete lattices, we prefix Inf and Sup in the statements by c, giving cInf and cSup. For instance, Inf_le is a statement in complete lattices ensuring Inf s ≤ x, while cInf_le is the same statement in conditionally complete lattices with an additional assumption that s is bounded below. -/ set_option old_structure_cmd true open set variables {α β : Type*} {ι : Sort*} section /-! Extension of Sup and Inf from a preorder `α` to `with_top α` and `with_bot α` -/ open_locale classical noncomputable instance {α : Type*} [preorder α] [has_Sup α] : has_Sup (with_top α) := ⟨λ S, if ⊤ ∈ S then ⊤ else if bdd_above (coe ⁻¹' S : set α) then ↑(Sup (coe ⁻¹' S : set α)) else ⊤⟩ noncomputable instance {α : Type*} [has_Inf α] : has_Inf (with_top α) := ⟨λ S, if S ⊆ {⊤} then ⊤ else ↑(Inf (coe ⁻¹' S : set α))⟩ noncomputable instance {α : Type*} [has_Sup α] : has_Sup (with_bot α) := ⟨(@with_top.has_Inf (order_dual α) _).Inf⟩ noncomputable instance {α : Type*} [preorder α] [has_Inf α] : has_Inf (with_bot α) := ⟨(@with_top.has_Sup (order_dual α) _ _).Sup⟩ @[simp] theorem with_top.cInf_empty {α : Type*} [has_Inf α] : Inf (∅ : set (with_top α)) = ⊤ := if_pos $ set.empty_subset _ @[simp] theorem with_bot.cSup_empty {α : Type*} [has_Sup α] : Sup (∅ : set (with_bot α)) = ⊥ := if_pos $ set.empty_subset _ end -- section /-- A conditionally complete lattice is a lattice in which every nonempty subset which is bounded above has a supremum, and every nonempty subset which is bounded below has an infimum. Typical examples are real numbers or natural numbers. To differentiate the statements from the corresponding statements in (unconditional) complete lattices, we prefix Inf and Sup by a c everywhere. The same statements should hold in both worlds, sometimes with additional assumptions of nonemptiness or boundedness.-/ class conditionally_complete_lattice (α : Type*) extends lattice α, has_Sup α, has_Inf α := (le_cSup : ∀s a, bdd_above s → a ∈ s → a ≤ Sup s) (cSup_le : ∀ s a, set.nonempty s → a ∈ upper_bounds s → Sup s ≤ a) (cInf_le : ∀s a, bdd_below s → a ∈ s → Inf s ≤ a) (le_cInf : ∀s a, set.nonempty s → a ∈ lower_bounds s → a ≤ Inf s) /-- A conditionally complete linear order is a linear order in which every nonempty subset which is bounded above has a supremum, and every nonempty subset which is bounded below has an infimum. Typical examples are real numbers or natural numbers. To differentiate the statements from the corresponding statements in (unconditional) complete linear orders, we prefix Inf and Sup by a c everywhere. The same statements should hold in both worlds, sometimes with additional assumptions of nonemptiness or boundedness.-/ class conditionally_complete_linear_order (α : Type*) extends conditionally_complete_lattice α, linear_order α /-- A conditionally complete linear order with `bot` is a linear order with least element, in which every nonempty subset which is bounded above has a supremum, and every nonempty subset (necessarily bounded below) has an infimum. A typical example is the natural numbers. To differentiate the statements from the corresponding statements in (unconditional) complete linear orders, we prefix Inf and Sup by a c everywhere. The same statements should hold in both worlds, sometimes with additional assumptions of nonemptiness or boundedness.-/ @[ancestor conditionally_complete_linear_order has_bot] class conditionally_complete_linear_order_bot (α : Type*) extends conditionally_complete_linear_order α, has_bot α := (bot_le : ∀ x : α, ⊥ ≤ x) (cSup_empty : Sup ∅ = ⊥) @[priority 100] -- see Note [lower instance priority] instance conditionally_complete_linear_order_bot.to_order_bot (α : Type*) [h : conditionally_complete_linear_order_bot α] : order_bot α := { ..h } /- A complete lattice is a conditionally complete lattice, as there are no restrictions on the properties of Inf and Sup in a complete lattice.-/ @[priority 100] -- see Note [lower instance priority] instance conditionally_complete_lattice_of_complete_lattice [complete_lattice α]: conditionally_complete_lattice α := { le_cSup := by intros; apply le_Sup; assumption, cSup_le := by intros; apply Sup_le; assumption, cInf_le := by intros; apply Inf_le; assumption, le_cInf := by intros; apply le_Inf; assumption, ..‹complete_lattice α› } @[priority 100] -- see Note [lower instance priority] instance conditionally_complete_linear_order_of_complete_linear_order [complete_linear_order α]: conditionally_complete_linear_order α := { ..conditionally_complete_lattice_of_complete_lattice, .. ‹complete_linear_order α› } section open_locale classical /-- A well founded linear order is conditionally complete, with a bottom element. -/ @[reducible] noncomputable def well_founded.conditionally_complete_linear_order_with_bot {α : Type*} [i : linear_order α] (h : well_founded ((<) : α → α → Prop)) (c : α) (hc : c = h.min set.univ ⟨c, mem_univ c⟩) : conditionally_complete_linear_order_bot α := { sup := max, le_sup_left := le_max_left, le_sup_right := le_max_right, sup_le := λ a b c, max_le, inf := min, inf_le_left := min_le_left, inf_le_right := min_le_right, le_inf := λ a b c, le_min, Inf := λ s, if hs : s.nonempty then h.min s hs else c, cInf_le := begin assume s a hs has, have s_ne : s.nonempty := ⟨a, has⟩, simpa [s_ne] using not_lt.1 (h.not_lt_min s s_ne has), end, le_cInf := begin assume s a hs has, simp only [hs, dif_pos], exact has (h.min_mem s hs), end, Sup := λ s, if hs : (upper_bounds s).nonempty then h.min _ hs else c, le_cSup := begin assume s a hs has, have h's : (upper_bounds s).nonempty := hs, simp only [h's, dif_pos], exact h.min_mem _ h's has, end, cSup_le := begin assume s a hs has, have h's : (upper_bounds s).nonempty := ⟨a, has⟩, simp only [h's, dif_pos], simpa using h.not_lt_min _ h's has, end, bot := c, bot_le := λ x, by convert not_lt.1 (h.not_lt_min set.univ ⟨c, mem_univ c⟩ (mem_univ x)), cSup_empty := begin have : (set.univ : set α).nonempty := ⟨c, mem_univ c⟩, simp only [this, dif_pos, upper_bounds_empty], exact hc.symm end, .. i } end section order_dual instance (α : Type*) [conditionally_complete_lattice α] : conditionally_complete_lattice (order_dual α) := { le_cSup := @conditionally_complete_lattice.cInf_le α _, cSup_le := @conditionally_complete_lattice.le_cInf α _, le_cInf := @conditionally_complete_lattice.cSup_le α _, cInf_le := @conditionally_complete_lattice.le_cSup α _, ..order_dual.has_Inf α, ..order_dual.has_Sup α, ..order_dual.lattice α } instance (α : Type*) [conditionally_complete_linear_order α] : conditionally_complete_linear_order (order_dual α) := { ..order_dual.conditionally_complete_lattice α, ..order_dual.linear_order α } end order_dual section conditionally_complete_lattice variables [conditionally_complete_lattice α] {s t : set α} {a b : α} theorem le_cSup (h₁ : bdd_above s) (h₂ : a ∈ s) : a ≤ Sup s := conditionally_complete_lattice.le_cSup s a h₁ h₂ theorem cSup_le (h₁ : s.nonempty) (h₂ : ∀b∈s, b ≤ a) : Sup s ≤ a := conditionally_complete_lattice.cSup_le s a h₁ h₂ theorem cInf_le (h₁ : bdd_below s) (h₂ : a ∈ s) : Inf s ≤ a := conditionally_complete_lattice.cInf_le s a h₁ h₂ theorem le_cInf (h₁ : s.nonempty) (h₂ : ∀b∈s, a ≤ b) : a ≤ Inf s := conditionally_complete_lattice.le_cInf s a h₁ h₂ theorem le_cSup_of_le (_ : bdd_above s) (hb : b ∈ s) (h : a ≤ b) : a ≤ Sup s := le_trans h (le_cSup ‹bdd_above s› hb) theorem cInf_le_of_le (_ : bdd_below s) (hb : b ∈ s) (h : b ≤ a) : Inf s ≤ a := le_trans (cInf_le ‹bdd_below s› hb) h theorem cSup_le_cSup (_ : bdd_above t) (_ : s.nonempty) (h : s ⊆ t) : Sup s ≤ Sup t := cSup_le ‹_› (assume (a) (ha : a ∈ s), le_cSup ‹bdd_above t› (h ha)) theorem cInf_le_cInf (_ : bdd_below t) (_ : s.nonempty) (h : s ⊆ t) : Inf t ≤ Inf s := le_cInf ‹_› (assume (a) (ha : a ∈ s), cInf_le ‹bdd_below t› (h ha)) lemma is_lub_cSup (ne : s.nonempty) (H : bdd_above s) : is_lub s (Sup s) := ⟨assume x, le_cSup H, assume x, cSup_le ne⟩ lemma is_lub_csupr [nonempty ι] {f : ι → α} (H : bdd_above (range f)) : is_lub (range f) (⨆ i, f i) := is_lub_cSup (range_nonempty f) H lemma is_lub_csupr_set {f : β → α} {s : set β} (H : bdd_above (f '' s)) (Hne : s.nonempty) : is_lub (f '' s) (⨆ i : s, f i) := by { rw ← Sup_image', exact is_lub_cSup (Hne.image _) H } lemma is_glb_cInf (ne : s.nonempty) (H : bdd_below s) : is_glb s (Inf s) := ⟨assume x, cInf_le H, assume x, le_cInf ne⟩ lemma is_glb_cinfi [nonempty ι] {f : ι → α} (H : bdd_below (range f)) : is_glb (range f) (⨅ i, f i) := is_glb_cInf (range_nonempty f) H lemma is_glb_cinfi_set {f : β → α} {s : set β} (H : bdd_below (f '' s)) (Hne : s.nonempty) : is_glb (f '' s) (⨅ i : s, f i) := @is_lub_csupr_set (order_dual α) _ _ _ _ H Hne lemma is_lub.cSup_eq (H : is_lub s a) (ne : s.nonempty) : Sup s = a := (is_lub_cSup ne ⟨a, H.1⟩).unique H lemma is_lub.csupr_eq [nonempty ι] {f : ι → α} (H : is_lub (range f) a) : (⨆ i, f i) = a := H.cSup_eq (range_nonempty f) lemma is_lub.csupr_set_eq {s : set β} {f : β → α} (H : is_lub (f '' s) a) (Hne : s.nonempty) : (⨆ i : s, f i) = a := is_lub.cSup_eq (image_eq_range f s ▸ H) (image_eq_range f s ▸ Hne.image f) /-- A greatest element of a set is the supremum of this set. -/ lemma is_greatest.cSup_eq (H : is_greatest s a) : Sup s = a := H.is_lub.cSup_eq H.nonempty lemma is_greatest.Sup_mem (H : is_greatest s a) : Sup s ∈ s := H.cSup_eq.symm ▸ H.1 lemma is_glb.cInf_eq (H : is_glb s a) (ne : s.nonempty) : Inf s = a := (is_glb_cInf ne ⟨a, H.1⟩).unique H lemma is_glb.cinfi_eq [nonempty ι] {f : ι → α} (H : is_glb (range f) a) : (⨅ i, f i) = a := H.cInf_eq (range_nonempty f) lemma is_glb.cinfi_set_eq {s : set β} {f : β → α} (H : is_glb (f '' s) a) (Hne : s.nonempty) : (⨅ i : s, f i) = a := is_glb.cInf_eq (image_eq_range f s ▸ H) (image_eq_range f s ▸ Hne.image f) /-- A least element of a set is the infimum of this set. -/ lemma is_least.cInf_eq (H : is_least s a) : Inf s = a := H.is_glb.cInf_eq H.nonempty lemma is_least.Inf_mem (H : is_least s a) : Inf s ∈ s := H.cInf_eq.symm ▸ H.1 lemma subset_Icc_cInf_cSup (hb : bdd_below s) (ha : bdd_above s) : s ⊆ Icc (Inf s) (Sup s) := λ x hx, ⟨cInf_le hb hx, le_cSup ha hx⟩ theorem cSup_le_iff (hb : bdd_above s) (ne : s.nonempty) : Sup s ≤ a ↔ (∀b ∈ s, b ≤ a) := is_lub_le_iff (is_lub_cSup ne hb) theorem le_cInf_iff (hb : bdd_below s) (ne : s.nonempty) : a ≤ Inf s ↔ (∀b ∈ s, a ≤ b) := le_is_glb_iff (is_glb_cInf ne hb) lemma cSup_lower_bounds_eq_cInf {s : set α} (h : bdd_below s) (hs : s.nonempty) : Sup (lower_bounds s) = Inf s := (is_lub_cSup h $ hs.mono $ λ x hx y hy, hy hx).unique (is_glb_cInf hs h).is_lub lemma cInf_upper_bounds_eq_cSup {s : set α} (h : bdd_above s) (hs : s.nonempty) : Inf (upper_bounds s) = Sup s := (is_glb_cInf h $ hs.mono $ λ x hx y hy, hy hx).unique (is_lub_cSup hs h).is_glb lemma not_mem_of_lt_cInf {x : α} {s : set α} (h : x < Inf s) (hs : bdd_below s) : x ∉ s := λ hx, lt_irrefl _ (h.trans_le (cInf_le hs hx)) lemma not_mem_of_cSup_lt {x : α} {s : set α} (h : Sup s < x) (hs : bdd_above s) : x ∉ s := @not_mem_of_lt_cInf (order_dual α) _ x s h hs /--Introduction rule to prove that `b` is the supremum of `s`: it suffices to check that `b` is larger than all elements of `s`, and that this is not the case of any `w<b`. See `Sup_eq_of_forall_le_of_forall_lt_exists_gt` for a version in complete lattices. -/ theorem cSup_eq_of_forall_le_of_forall_lt_exists_gt (_ : s.nonempty) (_ : ∀a∈s, a ≤ b) (H : ∀w, w < b → (∃a∈s, w < a)) : Sup s = b := have bdd_above s := ⟨b, by assumption⟩, have (Sup s < b) ∨ (Sup s = b) := lt_or_eq_of_le (cSup_le ‹_› ‹∀a∈s, a ≤ b›), have ¬(Sup s < b) := assume: Sup s < b, let ⟨a, _, _⟩ := (H (Sup s) ‹Sup s < b›) in /- a ∈ s, Sup s < a-/ have Sup s < Sup s := lt_of_lt_of_le ‹Sup s < a› (le_cSup ‹bdd_above s› ‹a ∈ s›), show false, by finish [lt_irrefl (Sup s)], show Sup s = b, by finish /--Introduction rule to prove that `b` is the infimum of `s`: it suffices to check that `b` is smaller than all elements of `s`, and that this is not the case of any `w>b`. See `Inf_eq_of_forall_ge_of_forall_gt_exists_lt` for a version in complete lattices. -/ theorem cInf_eq_of_forall_ge_of_forall_gt_exists_lt (_ : s.nonempty) (_ : ∀a∈s, b ≤ a) (H : ∀w, b < w → (∃a∈s, a < w)) : Inf s = b := @cSup_eq_of_forall_le_of_forall_lt_exists_gt (order_dual α) _ _ _ ‹_› ‹_› ‹_› /--b < Sup s when there is an element a in s with b < a, when s is bounded above. This is essentially an iff, except that the assumptions for the two implications are slightly different (one needs boundedness above for one direction, nonemptiness and linear order for the other one), so we formulate separately the two implications, contrary to the complete_lattice case.-/ lemma lt_cSup_of_lt (_ : bdd_above s) (_ : a ∈ s) (_ : b < a) : b < Sup s := lt_of_lt_of_le ‹b < a› (le_cSup ‹bdd_above s› ‹a ∈ s›) /--Inf s < b when there is an element a in s with a < b, when s is bounded below. This is essentially an iff, except that the assumptions for the two implications are slightly different (one needs boundedness below for one direction, nonemptiness and linear order for the other one), so we formulate separately the two implications, contrary to the complete_lattice case.-/ lemma cInf_lt_of_lt (_ : bdd_below s) (_ : a ∈ s) (_ : a < b) : Inf s < b := @lt_cSup_of_lt (order_dual α) _ _ _ _ ‹_› ‹_› ‹_› /-- If all elements of a nonempty set `s` are less than or equal to all elements of a nonempty set `t`, then there exists an element between these sets. -/ lemma exists_between_of_forall_le (sne : s.nonempty) (tne : t.nonempty) (hst : ∀ (x ∈ s) (y ∈ t), x ≤ y) : (upper_bounds s ∩ lower_bounds t).nonempty := ⟨Inf t, λ x hx, le_cInf tne $ hst x hx, λ y hy, cInf_le (sne.mono hst) hy⟩ /--The supremum of a singleton is the element of the singleton-/ @[simp] theorem cSup_singleton (a : α) : Sup {a} = a := is_greatest_singleton.cSup_eq /--The infimum of a singleton is the element of the singleton-/ @[simp] theorem cInf_singleton (a : α) : Inf {a} = a := is_least_singleton.cInf_eq /--If a set is bounded below and above, and nonempty, its infimum is less than or equal to its supremum.-/ theorem cInf_le_cSup (hb : bdd_below s) (ha : bdd_above s) (ne : s.nonempty) : Inf s ≤ Sup s := is_glb_le_is_lub (is_glb_cInf ne hb) (is_lub_cSup ne ha) ne /--The sup of a union of two sets is the max of the suprema of each subset, under the assumptions that all sets are bounded above and nonempty.-/ theorem cSup_union (hs : bdd_above s) (sne : s.nonempty) (ht : bdd_above t) (tne : t.nonempty) : Sup (s ∪ t) = Sup s ⊔ Sup t := ((is_lub_cSup sne hs).union (is_lub_cSup tne ht)).cSup_eq sne.inl /--The inf of a union of two sets is the min of the infima of each subset, under the assumptions that all sets are bounded below and nonempty.-/ theorem cInf_union (hs : bdd_below s) (sne : s.nonempty) (ht : bdd_below t) (tne : t.nonempty) : Inf (s ∪ t) = Inf s ⊓ Inf t := @cSup_union (order_dual α) _ _ _ hs sne ht tne /--The supremum of an intersection of two sets is bounded by the minimum of the suprema of each set, if all sets are bounded above and nonempty.-/ theorem cSup_inter_le (_ : bdd_above s) (_ : bdd_above t) (hst : (s ∩ t).nonempty) : Sup (s ∩ t) ≤ Sup s ⊓ Sup t := begin apply cSup_le hst, simp only [le_inf_iff, and_imp, set.mem_inter_eq], intros b _ _, split, apply le_cSup ‹bdd_above s› ‹b ∈ s›, apply le_cSup ‹bdd_above t› ‹b ∈ t› end /--The infimum of an intersection of two sets is bounded below by the maximum of the infima of each set, if all sets are bounded below and nonempty.-/ theorem le_cInf_inter (_ : bdd_below s) (_ : bdd_below t) (hst : (s ∩ t).nonempty) : Inf s ⊔ Inf t ≤ Inf (s ∩ t) := @cSup_inter_le (order_dual α) _ _ _ ‹_› ‹_› hst /-- The supremum of insert a s is the maximum of a and the supremum of s, if s is nonempty and bounded above.-/ theorem cSup_insert (hs : bdd_above s) (sne : s.nonempty) : Sup (insert a s) = a ⊔ Sup s := ((is_lub_cSup sne hs).insert a).cSup_eq (insert_nonempty a s) /-- The infimum of insert a s is the minimum of a and the infimum of s, if s is nonempty and bounded below.-/ theorem cInf_insert (hs : bdd_below s) (sne : s.nonempty) : Inf (insert a s) = a ⊓ Inf s := @cSup_insert (order_dual α) _ _ _ hs sne @[simp] lemma cInf_Icc (h : a ≤ b) : Inf (Icc a b) = a := (is_glb_Icc h).cInf_eq (nonempty_Icc.2 h) @[simp] lemma cInf_Ici : Inf (Ici a) = a := is_least_Ici.cInf_eq @[simp] lemma cInf_Ico (h : a < b) : Inf (Ico a b) = a := (is_glb_Ico h).cInf_eq (nonempty_Ico.2 h) @[simp] lemma cInf_Ioc [densely_ordered α] (h : a < b) : Inf (Ioc a b) = a := (is_glb_Ioc h).cInf_eq (nonempty_Ioc.2 h) @[simp] lemma cInf_Ioi [no_top_order α] [densely_ordered α] : Inf (Ioi a) = a := cInf_eq_of_forall_ge_of_forall_gt_exists_lt nonempty_Ioi (λ _, le_of_lt) (λ w hw, by simpa using exists_between hw) @[simp] lemma cInf_Ioo [densely_ordered α] (h : a < b) : Inf (Ioo a b) = a := (is_glb_Ioo h).cInf_eq (nonempty_Ioo.2 h) @[simp] lemma cSup_Icc (h : a ≤ b) : Sup (Icc a b) = b := (is_lub_Icc h).cSup_eq (nonempty_Icc.2 h) @[simp] lemma cSup_Ico [densely_ordered α] (h : a < b) : Sup (Ico a b) = b := (is_lub_Ico h).cSup_eq (nonempty_Ico.2 h) @[simp] lemma cSup_Iic : Sup (Iic a) = a := is_greatest_Iic.cSup_eq @[simp] lemma cSup_Iio [no_bot_order α] [densely_ordered α] : Sup (Iio a) = a := cSup_eq_of_forall_le_of_forall_lt_exists_gt nonempty_Iio (λ _, le_of_lt) (λ w hw, by simpa [and_comm] using exists_between hw) @[simp] lemma cSup_Ioc (h : a < b) : Sup (Ioc a b) = b := (is_lub_Ioc h).cSup_eq (nonempty_Ioc.2 h) @[simp] lemma cSup_Ioo [densely_ordered α] (h : a < b) : Sup (Ioo a b) = b := (is_lub_Ioo h).cSup_eq (nonempty_Ioo.2 h) /--The indexed supremum of a function is bounded above by a uniform bound-/ lemma csupr_le [nonempty ι] {f : ι → α} {c : α} (H : ∀x, f x ≤ c) : supr f ≤ c := cSup_le (range_nonempty f) (by rwa forall_range_iff) /--The indexed supremum of a function is bounded below by the value taken at one point-/ lemma le_csupr {f : ι → α} (H : bdd_above (range f)) (c : ι) : f c ≤ supr f := le_cSup H (mem_range_self _) lemma le_csupr_of_le {f : ι → α} (H : bdd_above (range f)) (c : ι) (h : a ≤ f c) : a ≤ supr f := le_trans h (le_csupr H c) /--The indexed supremum of two functions are comparable if the functions are pointwise comparable-/ lemma csupr_le_csupr {f g : ι → α} (B : bdd_above (range g)) (H : ∀x, f x ≤ g x) : supr f ≤ supr g := begin casesI is_empty_or_nonempty ι, { rw [supr_of_empty', supr_of_empty'] }, { exact csupr_le (λ x, le_csupr_of_le B x (H x)) }, end /--The indexed infimum of two functions are comparable if the functions are pointwise comparable-/ lemma cinfi_le_cinfi {f g : ι → α} (B : bdd_below (range f)) (H : ∀x, f x ≤ g x) : infi f ≤ infi g := @csupr_le_csupr (order_dual α) _ _ _ _ B H /--The indexed minimum of a function is bounded below by a uniform lower bound-/ lemma le_cinfi [nonempty ι] {f : ι → α} {c : α} (H : ∀x, c ≤ f x) : c ≤ infi f := @csupr_le (order_dual α) _ _ _ _ _ H /--The indexed infimum of a function is bounded above by the value taken at one point-/ lemma cinfi_le {f : ι → α} (H : bdd_below (range f)) (c : ι) : infi f ≤ f c := @le_csupr (order_dual α) _ _ _ H c lemma cinfi_le_of_le {f : ι → α} (H : bdd_below (range f)) (c : ι) (h : f c ≤ a) : infi f ≤ a := @le_csupr_of_le (order_dual α) _ _ _ _ H c h @[simp] theorem csupr_const [hι : nonempty ι] {a : α} : (⨆ b:ι, a) = a := by rw [supr, range_const, cSup_singleton] @[simp] theorem cinfi_const [hι : nonempty ι] {a : α} : (⨅ b:ι, a) = a := @csupr_const (order_dual α) _ _ _ _ theorem supr_unique [unique ι] {s : ι → α} : (⨆ i, s i) = s (default ι) := have ∀ i, s i = s (default ι) := λ i, congr_arg s (unique.eq_default i), by simp only [this, csupr_const] theorem infi_unique [unique ι] {s : ι → α} : (⨅ i, s i) = s (default ι) := @supr_unique (order_dual α) _ _ _ _ @[simp] theorem supr_unit {f : unit → α} : (⨆ x, f x) = f () := by { convert supr_unique, apply_instance } @[simp] theorem infi_unit {f : unit → α} : (⨅ x, f x) = f () := @supr_unit (order_dual α) _ _ @[simp] lemma csupr_pos {p : Prop} {f : p → α} (hp : p) : (⨆ h : p, f h) = f hp := by haveI := unique_prop hp; exact supr_unique @[simp] lemma cinfi_pos {p : Prop} {f : p → α} (hp : p) : (⨅ h : p, f h) = f hp := @csupr_pos (order_dual α) _ _ _ hp lemma csupr_set {s : set β} {f : β → α} : (⨆ x : s, f x) = Sup (f '' s) := begin rw supr, congr, ext, rw [mem_image, mem_range, set_coe.exists], simp_rw [subtype.coe_mk, exists_prop], end lemma cinfi_set {s : set β} {f : β → α} : (⨅ x : s, f x) = Inf (f '' s) := @csupr_set (order_dual α) _ _ _ _ /--Introduction rule to prove that `b` is the supremum of `f`: it suffices to check that `b` is larger than `f i` for all `i`, and that this is not the case of any `w<b`. See `supr_eq_of_forall_le_of_forall_lt_exists_gt` for a version in complete lattices. -/ theorem csupr_eq_of_forall_le_of_forall_lt_exists_gt [nonempty ι] {f : ι → α} (h₁ : ∀ i, f i ≤ b) (h₂ : ∀ w, w < b → (∃ i, w < f i)) : (⨆ (i : ι), f i) = b := cSup_eq_of_forall_le_of_forall_lt_exists_gt (range_nonempty f) (forall_range_iff.mpr h₁) (λ w hw, exists_range_iff.mpr $ h₂ w hw) /--Introduction rule to prove that `b` is the infimum of `f`: it suffices to check that `b` is smaller than `f i` for all `i`, and that this is not the case of any `w>b`. See `infi_eq_of_forall_ge_of_forall_gt_exists_lt` for a version in complete lattices. -/ theorem cinfi_eq_of_forall_ge_of_forall_gt_exists_lt [nonempty ι] {f : ι → α} (h₁ : ∀ i, b ≤ f i) (h₂ : ∀ w, b < w → (∃ i, f i < w)) : (⨅ (i : ι), f i) = b := @csupr_eq_of_forall_le_of_forall_lt_exists_gt (order_dual α) _ _ _ _ ‹_› ‹_› ‹_› /-- Nested intervals lemma: if `f` is a monotone sequence, `g` is an antitone sequence, and `f n ≤ g n` for all `n`, then `⨆ n, f n` belongs to all the intervals `[f n, g n]`. -/ lemma monotone.csupr_mem_Inter_Icc_of_antitone [semilattice_sup β] {f g : β → α} (hf : monotone f) (hg : antitone g) (h : f ≤ g) : (⨆ n, f n) ∈ ⋂ n, Icc (f n) (g n) := begin refine mem_Inter.2 (λ n, _), haveI : nonempty β := ⟨n⟩, have : ∀ m, f m ≤ g n := λ m, hf.forall_le_of_antitone hg h m n, exact ⟨le_csupr ⟨g $ n, forall_range_iff.2 this⟩ _, csupr_le this⟩ end /-- Nested intervals lemma: if `[f n, g n]` is an antitone sequence of nonempty closed intervals, then `⨆ n, f n` belongs to all the intervals `[f n, g n]`. -/ lemma csupr_mem_Inter_Icc_of_antitone_Icc [semilattice_sup β] {f g : β → α} (h : antitone (λ n, Icc (f n) (g n))) (h' : ∀ n, f n ≤ g n) : (⨆ n, f n) ∈ ⋂ n, Icc (f n) (g n) := monotone.csupr_mem_Inter_Icc_of_antitone (λ m n hmn, ((Icc_subset_Icc_iff (h' n)).1 (h hmn)).1) (λ m n hmn, ((Icc_subset_Icc_iff (h' n)).1 (h hmn)).2) h' lemma finset.nonempty.sup'_eq_cSup_image {s : finset β} (hs : s.nonempty) (f : β → α) : s.sup' hs f = Sup (f '' s) := eq_of_forall_ge_iff $ λ a, by simp [cSup_le_iff (s.finite_to_set.image f).bdd_above (hs.to_set.image f)] lemma finset.nonempty.sup'_id_eq_cSup {s : finset α} (hs : s.nonempty) : s.sup' hs id = Sup s := by rw [hs.sup'_eq_cSup_image, image_id] end conditionally_complete_lattice instance pi.conditionally_complete_lattice {ι : Type*} {α : Π i : ι, Type*} [Π i, conditionally_complete_lattice (α i)] : conditionally_complete_lattice (Π i, α i) := { le_cSup := λ s f ⟨g, hg⟩ hf i, le_cSup ⟨g i, set.forall_range_iff.2 $ λ ⟨f', hf'⟩, hg hf' i⟩ ⟨⟨f, hf⟩, rfl⟩, cSup_le := λ s f hs hf i, cSup_le (by haveI := hs.to_subtype; apply range_nonempty) $ λ b ⟨⟨g, hg⟩, hb⟩, hb ▸ hf hg i, cInf_le := λ s f ⟨g, hg⟩ hf i, cInf_le ⟨g i, set.forall_range_iff.2 $ λ ⟨f', hf'⟩, hg hf' i⟩ ⟨⟨f, hf⟩, rfl⟩, le_cInf := λ s f hs hf i, le_cInf (by haveI := hs.to_subtype; apply range_nonempty) $ λ b ⟨⟨g, hg⟩, hb⟩, hb ▸ hf hg i, .. pi.lattice, .. pi.has_Sup, .. pi.has_Inf } section conditionally_complete_linear_order variables [conditionally_complete_linear_order α] {s t : set α} {a b : α} lemma finset.nonempty.cSup_eq_max' {s : finset α} (h : s.nonempty) : Sup ↑s = s.max' h := eq_of_forall_ge_iff $ λ a, (cSup_le_iff s.bdd_above h.to_set).trans (s.max'_le_iff h).symm lemma finset.nonempty.cInf_eq_min' {s : finset α} (h : s.nonempty) : Inf ↑s = s.min' h := @finset.nonempty.cSup_eq_max' (order_dual α) _ s h lemma finset.nonempty.cSup_mem {s : finset α} (h : s.nonempty) : Sup (s : set α) ∈ s := by { rw h.cSup_eq_max', exact s.max'_mem _ } lemma finset.nonempty.cInf_mem {s : finset α} (h : s.nonempty) : Inf (s : set α) ∈ s := @finset.nonempty.cSup_mem (order_dual α) _ _ h lemma set.nonempty.cSup_mem (h : s.nonempty) (hs : finite s) : Sup s ∈ s := by { lift s to finset α using hs, exact finset.nonempty.cSup_mem h } lemma set.nonempty.cInf_mem (h : s.nonempty) (hs : finite s) : Inf s ∈ s := @set.nonempty.cSup_mem (order_dual α) _ _ h hs lemma set.finite.cSup_lt_iff (hs : finite s) (h : s.nonempty) : Sup s < a ↔ ∀ x ∈ s, x < a := ⟨λ h x hx, (le_cSup hs.bdd_above hx).trans_lt h, λ H, H _ $ h.cSup_mem hs⟩ lemma set.finite.lt_cInf_iff (hs : finite s) (h : s.nonempty) : a < Inf s ↔ ∀ x ∈ s, a < x := @set.finite.cSup_lt_iff (order_dual α) _ _ _ hs h /-- When b < Sup s, there is an element a in s with b < a, if s is nonempty and the order is a linear order. -/ lemma exists_lt_of_lt_cSup (hs : s.nonempty) (hb : b < Sup s) : ∃a∈s, b < a := begin classical, contrapose! hb, exact cSup_le hs hb end /-- Indexed version of the above lemma `exists_lt_of_lt_cSup`. When `b < supr f`, there is an element `i` such that `b < f i`. -/ lemma exists_lt_of_lt_csupr [nonempty ι] {f : ι → α} (h : b < supr f) : ∃i, b < f i := let ⟨_, ⟨i, rfl⟩, h⟩ := exists_lt_of_lt_cSup (range_nonempty f) h in ⟨i, h⟩ /--When Inf s < b, there is an element a in s with a < b, if s is nonempty and the order is a linear order.-/ lemma exists_lt_of_cInf_lt (hs : s.nonempty) (hb : Inf s < b) : ∃a∈s, a < b := @exists_lt_of_lt_cSup (order_dual α) _ _ _ hs hb /-- Indexed version of the above lemma `exists_lt_of_cInf_lt` When `infi f < a`, there is an element `i` such that `f i < a`. -/ lemma exists_lt_of_cinfi_lt [nonempty ι] {f : ι → α} (h : infi f < a) : (∃i, f i < a) := @exists_lt_of_lt_csupr (order_dual α) _ _ _ _ _ h /--Introduction rule to prove that b is the supremum of s: it suffices to check that 1) b is an upper bound 2) every other upper bound b' satisfies b ≤ b'.-/ theorem cSup_eq_of_is_forall_le_of_forall_le_imp_ge (_ : s.nonempty) (h_is_ub : ∀ a ∈ s, a ≤ b) (h_b_le_ub : ∀ub, (∀ a ∈ s, a ≤ ub) → (b ≤ ub)) : Sup s = b := le_antisymm (show Sup s ≤ b, from cSup_le ‹s.nonempty› h_is_ub) (show b ≤ Sup s, from h_b_le_ub _ $ assume a, le_cSup ⟨b, h_is_ub⟩) open function variables [is_well_order α (<)] lemma Inf_eq_argmin_on (hs : s.nonempty) : Inf s = argmin_on id (@is_well_order.wf α (<) _) s hs := is_least.cInf_eq ⟨argmin_on_mem _ _ _ _, λ a ha, argmin_on_le id _ _ ha⟩ lemma is_least_Inf (hs : s.nonempty) : is_least s (Inf s) := by { rw Inf_eq_argmin_on hs, exact ⟨argmin_on_mem _ _ _ _, λ a ha, argmin_on_le id _ _ ha⟩ } lemma le_cInf_iff' (hs : s.nonempty) : b ≤ Inf s ↔ b ∈ lower_bounds s := le_is_glb_iff (is_least_Inf hs).is_glb lemma Inf_mem (hs : s.nonempty) : Inf s ∈ s := (is_least_Inf hs).1 end conditionally_complete_linear_order /-! ### Lemmas about a conditionally complete linear order with bottom element In this case we have `Sup ∅ = ⊥`, so we can drop some `nonempty`/`set.nonempty` assumptions. -/ section conditionally_complete_linear_order_bot variables [conditionally_complete_linear_order_bot α] lemma cSup_empty : (Sup ∅ : α) = ⊥ := conditionally_complete_linear_order_bot.cSup_empty lemma csupr_of_empty [is_empty ι] (f : ι → α) : (⨆ i, f i) = ⊥ := by rw [supr_of_empty', cSup_empty] @[simp] lemma csupr_false (f : false → α) : (⨆ i, f i) = ⊥ := csupr_of_empty f lemma is_lub_cSup' {s : set α} (hs : bdd_above s) : is_lub s (Sup s) := begin rcases eq_empty_or_nonempty s with (rfl|hne), { simp only [cSup_empty, is_lub_empty] }, { exact is_lub_cSup hne hs } end lemma cSup_le_iff' {s : set α} (hs : bdd_above s) {a : α} : Sup s ≤ a ↔ ∀ x ∈ s, x ≤ a := is_lub_le_iff (is_lub_cSup' hs) lemma cSup_le' {s : set α} {a : α} (h : a ∈ upper_bounds s) : Sup s ≤ a := (cSup_le_iff' ⟨a, h⟩).2 h lemma exists_lt_of_lt_cSup' {s : set α} {a : α} (h : a < Sup s) : ∃ b ∈ s, a < b := by { contrapose! h, exact cSup_le' h } lemma csupr_le_iff' {f : ι → α} (h : bdd_above (range f)) {a : α} : (⨆ i, f i) ≤ a ↔ ∀ i, f i ≤ a := (cSup_le_iff' h).trans forall_range_iff lemma csupr_le' {f : ι → α} {a : α} (h : ∀ i, f i ≤ a) : (⨆ i, f i) ≤ a := cSup_le' $ forall_range_iff.2 h lemma exists_lt_of_lt_csupr' {f : ι → α} {a : α} (h : a < ⨆ i, f i) : ∃ i, a < f i := by { contrapose! h, exact csupr_le' h } end conditionally_complete_linear_order_bot namespace with_top open_locale classical variables [conditionally_complete_linear_order_bot α] /-- The Sup of a non-empty set is its least upper bound for a conditionally complete lattice with a top. -/ lemma is_lub_Sup' {β : Type*} [conditionally_complete_lattice β] {s : set (with_top β)} (hs : s.nonempty) : is_lub s (Sup s) := begin split, { show ite _ _ _ ∈ _, split_ifs, { intros _ _, exact le_top }, { rintro (⟨⟩|a) ha, { contradiction }, apply some_le_some.2, exact le_cSup h_1 ha }, { intros _ _, exact le_top } }, { show ite _ _ _ ∈ _, split_ifs, { rintro (⟨⟩|a) ha, { exact _root_.le_refl _ }, { exact false.elim (not_top_le_coe a (ha h)) } }, { rintro (⟨⟩|b) hb, { exact le_top }, refine some_le_some.2 (cSup_le _ _), { rcases hs with ⟨⟨⟩|b, hb⟩, { exact absurd hb h }, { exact ⟨b, hb⟩ } }, { intros a ha, exact some_le_some.1 (hb ha) } }, { rintro (⟨⟩|b) hb, { exact _root_.le_refl _ }, { exfalso, apply h_1, use b, intros a ha, exact some_le_some.1 (hb ha) } } } end lemma is_lub_Sup (s : set (with_top α)) : is_lub s (Sup s) := begin cases s.eq_empty_or_nonempty with hs hs, { rw hs, show is_lub ∅ (ite _ _ _), split_ifs, { cases h }, { rw [preimage_empty, cSup_empty], exact is_lub_empty }, { exfalso, apply h_1, use ⊥, rintro a ⟨⟩ } }, exact is_lub_Sup' hs, end /-- The Inf of a bounded-below set is its greatest lower bound for a conditionally complete lattice with a top. -/ lemma is_glb_Inf' {β : Type*} [conditionally_complete_lattice β] {s : set (with_top β)} (hs : bdd_below s) : is_glb s (Inf s) := begin split, { show ite _ _ _ ∈ _, split_ifs, { intros a ha, exact top_le_iff.2 (set.mem_singleton_iff.1 (h ha)) }, { rintro (⟨⟩|a) ha, { exact le_top }, refine some_le_some.2 (cInf_le _ ha), rcases hs with ⟨⟨⟩|b, hb⟩, { exfalso, apply h, intros c hc, rw [mem_singleton_iff, ←top_le_iff], exact hb hc }, use b, intros c hc, exact some_le_some.1 (hb hc) } }, { show ite _ _ _ ∈ _, split_ifs, { intros _ _, exact le_top }, { rintro (⟨⟩|a) ha, { exfalso, apply h, intros b hb, exact set.mem_singleton_iff.2 (top_le_iff.1 (ha hb)) }, { refine some_le_some.2 (le_cInf _ _), { classical, contrapose! h, rintros (⟨⟩|a) ha, { exact mem_singleton ⊤ }, { exact (h ⟨a, ha⟩).elim }}, { intros b hb, rw ←some_le_some, exact ha hb } } } } end lemma is_glb_Inf (s : set (with_top α)) : is_glb s (Inf s) := begin by_cases hs : bdd_below s, { exact is_glb_Inf' hs }, { exfalso, apply hs, use ⊥, intros _ _, exact bot_le }, end noncomputable instance : complete_linear_order (with_top α) := { Sup := Sup, le_Sup := assume s, (is_lub_Sup s).1, Sup_le := assume s, (is_lub_Sup s).2, Inf := Inf, le_Inf := assume s, (is_glb_Inf s).2, Inf_le := assume s, (is_glb_Inf s).1, .. with_top.linear_order, ..with_top.lattice, ..with_top.order_top, ..with_top.order_bot } lemma coe_Sup {s : set α} (hb : bdd_above s) : (↑(Sup s) : with_top α) = (⨆a∈s, ↑a) := begin cases s.eq_empty_or_nonempty with hs hs, { rw [hs, cSup_empty], simp only [set.mem_empty_eq, supr_bot, supr_false], refl }, apply le_antisymm, { refine (coe_le_iff.2 $ assume b hb, cSup_le hs $ assume a has, coe_le_coe.1 $ hb ▸ _), exact (le_supr_of_le a $ le_supr_of_le has $ _root_.le_refl _) }, { exact (supr_le $ assume a, supr_le $ assume ha, coe_le_coe.2 $ le_cSup hb ha) } end lemma coe_Inf {s : set α} (hs : s.nonempty) : (↑(Inf s) : with_top α) = (⨅a∈s, ↑a) := let ⟨x, hx⟩ := hs in have (⨅a∈s, ↑a : with_top α) ≤ x, from infi_le_of_le x $ infi_le_of_le hx $ _root_.le_refl _, let ⟨r, r_eq, hr⟩ := le_coe_iff.1 this in le_antisymm (le_infi $ assume a, le_infi $ assume ha, coe_le_coe.2 $ cInf_le (order_bot.bdd_below s) ha) begin refine (r_eq.symm ▸ coe_le_coe.2 $ le_cInf hs $ assume a has, coe_le_coe.1 $ _), refine (r_eq ▸ infi_le_of_le a _), exact (infi_le_of_le has $ _root_.le_refl _), end end with_top namespace monotone variables [preorder α] [conditionally_complete_lattice β] {f : α → β} (h_mono : monotone f) /-! A monotone function into a conditionally complete lattice preserves the ordering properties of `Sup` and `Inf`. -/ lemma le_cSup_image {s : set α} {c : α} (hcs : c ∈ s) (h_bdd : bdd_above s) : f c ≤ Sup (f '' s) := le_cSup (map_bdd_above h_mono h_bdd) (mem_image_of_mem f hcs) lemma cSup_image_le {s : set α} (hs : s.nonempty) {B : α} (hB: B ∈ upper_bounds s) : Sup (f '' s) ≤ f B := cSup_le (nonempty.image f hs) (h_mono.mem_upper_bounds_image hB) lemma cInf_image_le {s : set α} {c : α} (hcs : c ∈ s) (h_bdd : bdd_below s) : Inf (f '' s) ≤ f c := @le_cSup_image (order_dual α) (order_dual β) _ _ _ (λ x y hxy, h_mono hxy) _ _ hcs h_bdd lemma le_cInf_image {s : set α} (hs : s.nonempty) {B : α} (hB: B ∈ lower_bounds s) : f B ≤ Inf (f '' s) := @cSup_image_le (order_dual α) (order_dual β) _ _ _ (λ x y hxy, h_mono hxy) _ hs _ hB end monotone namespace galois_connection variables {γ : Type*} [conditionally_complete_lattice α] [conditionally_complete_lattice β] [nonempty ι] {l : α → β} {u : β → α} lemma l_cSup (gc : galois_connection l u) {s : set α} (hne : s.nonempty) (hbdd : bdd_above s) : l (Sup s) = ⨆ x : s, l x := eq.symm $ is_lub.csupr_set_eq (gc.is_lub_l_image $ is_lub_cSup hne hbdd) hne lemma l_cSup' (gc : galois_connection l u) {s : set α} (hne : s.nonempty) (hbdd : bdd_above s) : l (Sup s) = Sup (l '' s) := by rw [gc.l_cSup hne hbdd, csupr_set] lemma l_csupr (gc : galois_connection l u) {f : ι → α} (hf : bdd_above (range f)) : l (⨆ i, f i) = ⨆ i, l (f i) := by rw [supr, gc.l_cSup (range_nonempty _) hf, supr_range'] lemma l_csupr_set (gc : galois_connection l u) {s : set γ} {f : γ → α} (hf : bdd_above (f '' s)) (hne : s.nonempty) : l (⨆ i : s, f i) = ⨆ i : s, l (f i) := by { haveI := hne.to_subtype, rw image_eq_range at hf, exact gc.l_csupr hf } lemma u_cInf (gc : galois_connection l u) {s : set β} (hne : s.nonempty) (hbdd : bdd_below s) : u (Inf s) = ⨅ x : s, u x := gc.dual.l_cSup hne hbdd lemma u_cInf' (gc : galois_connection l u) {s : set β} (hne : s.nonempty) (hbdd : bdd_below s) : u (Inf s) = Inf (u '' s) := gc.dual.l_cSup' hne hbdd lemma u_cinfi (gc : galois_connection l u) {f : ι → β} (hf : bdd_below (range f)) : u (⨅ i, f i) = ⨅ i, u (f i) := gc.dual.l_csupr hf lemma u_cinfi_set (gc : galois_connection l u) {s : set γ} {f : γ → β} (hf : bdd_below (f '' s)) (hne : s.nonempty) : u (⨅ i : s, f i) = ⨅ i : s, u (f i) := gc.dual.l_csupr_set hf hne end galois_connection namespace order_iso variables {γ : Type*} [conditionally_complete_lattice α] [conditionally_complete_lattice β] [nonempty ι] lemma map_cSup (e : α ≃o β) {s : set α} (hne : s.nonempty) (hbdd : bdd_above s) : e (Sup s) = ⨆ x : s, e x := e.to_galois_connection.l_cSup hne hbdd lemma map_cSup' (e : α ≃o β) {s : set α} (hne : s.nonempty) (hbdd : bdd_above s) : e (Sup s) = Sup (e '' s) := e.to_galois_connection.l_cSup' hne hbdd lemma map_csupr (e : α ≃o β) {f : ι → α} (hf : bdd_above (range f)) : e (⨆ i, f i) = ⨆ i, e (f i) := e.to_galois_connection.l_csupr hf lemma map_csupr_set (e : α ≃o β) {s : set γ} {f : γ → α} (hf : bdd_above (f '' s)) (hne : s.nonempty) : e (⨆ i : s, f i) = ⨆ i : s, e (f i) := e.to_galois_connection.l_csupr_set hf hne lemma map_cInf (e : α ≃o β) {s : set α} (hne : s.nonempty) (hbdd : bdd_below s) : e (Inf s) = ⨅ x : s, e x := e.dual.map_cSup hne hbdd lemma map_cInf' (e : α ≃o β) {s : set α} (hne : s.nonempty) (hbdd : bdd_below s) : e (Inf s) = Inf (e '' s) := e.dual.map_cSup' hne hbdd lemma map_cinfi (e : α ≃o β) {f : ι → α} (hf : bdd_below (range f)) : e (⨅ i, f i) = ⨅ i, e (f i) := e.dual.map_csupr hf lemma map_cinfi_set (e : α ≃o β) {s : set γ} {f : γ → α} (hf : bdd_below (f '' s)) (hne : s.nonempty) : e (⨅ i : s, f i) = ⨅ i : s, e (f i) := e.dual.map_csupr_set hf hne end order_iso /-! ### Relation between `Sup` / `Inf` and `finset.sup'` / `finset.inf'` Like the `Sup` of a `conditionally_complete_lattice`, `finset.sup'` also requires the set to be non-empty. As a result, we can translate between the two. -/ namespace finset lemma sup'_eq_cSup_image [conditionally_complete_lattice β] (s : finset α) (H) (f : α → β) : s.sup' H f = Sup (f '' s) := begin apply le_antisymm, { refine (finset.sup'_le _ _ $ λ a ha, _), refine le_cSup ⟨s.sup' H f, _⟩ ⟨a, ha, rfl⟩, rintros i ⟨j, hj, rfl⟩, exact finset.le_sup' _ hj }, { apply cSup_le ((coe_nonempty.mpr H).image _), rintros _ ⟨a, ha, rfl⟩, exact finset.le_sup' _ ha, } end lemma inf'_eq_cInf_image [conditionally_complete_lattice β] (s : finset α) (H) (f : α → β) : s.inf' H f = Inf (f '' s) := @sup'_eq_cSup_image _ (order_dual β) _ _ _ _ lemma sup'_id_eq_cSup [conditionally_complete_lattice α] (s : finset α) (H) : s.sup' H id = Sup s := by rw [sup'_eq_cSup_image s H, set.image_id] lemma inf'_id_eq_cInf [conditionally_complete_lattice α] (s : finset α) (H) : s.inf' H id = Inf s := @sup'_id_eq_cSup (order_dual α) _ _ _ end finset section with_top_bot /-! ### Complete lattice structure on `with_top (with_bot α)` If `α` is a `conditionally_complete_lattice`, then we show that `with_top α` and `with_bot α` also inherit the structure of conditionally complete lattices. Furthermore, we show that `with_top (with_bot α)` naturally inherits the structure of a complete lattice. Note that for α a conditionally complete lattice, `Sup` and `Inf` both return junk values for sets which are empty or unbounded. The extension of `Sup` to `with_top α` fixes the unboundedness problem and the extension to `with_bot α` fixes the problem with the empty set. This result can be used to show that the extended reals [-∞, ∞] are a complete lattice. -/ open_locale classical /-- Adding a top element to a conditionally complete lattice gives a conditionally complete lattice -/ noncomputable instance with_top.conditionally_complete_lattice {α : Type*} [conditionally_complete_lattice α] : conditionally_complete_lattice (with_top α) := { le_cSup := λ S a hS haS, (with_top.is_lub_Sup' ⟨a, haS⟩).1 haS, cSup_le := λ S a hS haS, (with_top.is_lub_Sup' hS).2 haS, cInf_le := λ S a hS haS, (with_top.is_glb_Inf' hS).1 haS, le_cInf := λ S a hS haS, (with_top.is_glb_Inf' ⟨a, haS⟩).2 haS, ..with_top.lattice, ..with_top.has_Sup, ..with_top.has_Inf } /-- Adding a bottom element to a conditionally complete lattice gives a conditionally complete lattice -/ noncomputable instance with_bot.conditionally_complete_lattice {α : Type*} [conditionally_complete_lattice α] : conditionally_complete_lattice (with_bot α) := { le_cSup := (@with_top.conditionally_complete_lattice (order_dual α) _).cInf_le, cSup_le := (@with_top.conditionally_complete_lattice (order_dual α) _).le_cInf, cInf_le := (@with_top.conditionally_complete_lattice (order_dual α) _).le_cSup, le_cInf := (@with_top.conditionally_complete_lattice (order_dual α) _).cSup_le, ..with_bot.lattice, ..with_bot.has_Sup, ..with_bot.has_Inf } /-- Adding a bottom and a top to a conditionally complete lattice gives a bounded lattice-/ noncomputable instance with_top.with_bot.bounded_lattice {α : Type*} [conditionally_complete_lattice α] : bounded_lattice (with_top (with_bot α)) := { ..with_top.order_bot, ..with_top.order_top, ..conditionally_complete_lattice.to_lattice _ } noncomputable instance with_top.with_bot.complete_lattice {α : Type*} [conditionally_complete_lattice α] : complete_lattice (with_top (with_bot α)) := { le_Sup := λ S a haS, (with_top.is_lub_Sup' ⟨a, haS⟩).1 haS, Sup_le := λ S a ha, begin cases S.eq_empty_or_nonempty with h, { show ite _ _ _ ≤ a, split_ifs, { rw h at h_1, cases h_1 }, { convert bot_le, convert with_bot.cSup_empty, rw h, refl }, { exfalso, apply h_2, use ⊥, rw h, rintro b ⟨⟩ } }, { refine (with_top.is_lub_Sup' h).2 ha } end, Inf_le := λ S a haS, show ite _ _ _ ≤ a, begin split_ifs, { cases a with a, exact _root_.le_refl _, cases (h haS); tauto }, { cases a, { exact le_top }, { apply with_top.some_le_some.2, refine cInf_le _ haS, use ⊥, intros b hb, exact bot_le } } end, le_Inf := λ S a haS, (with_top.is_glb_Inf' ⟨a, haS⟩).2 haS, ..with_top.has_Inf, ..with_top.has_Sup, ..with_top.with_bot.bounded_lattice } noncomputable instance with_top.with_bot.complete_linear_order {α : Type*} [conditionally_complete_linear_order α] : complete_linear_order (with_top (with_bot α)) := { .. with_top.with_bot.complete_lattice, .. with_top.linear_order } end with_top_bot section subtype variables (s : set α) /-! ### Subtypes of conditionally complete linear orders In this section we give conditions on a subset of a conditionally complete linear order, to ensure that the subtype is itself conditionally complete. We check that an `ord_connected` set satisfies these conditions. TODO There are several possible variants; the `conditionally_complete_linear_order` could be changed to `conditionally_complete_linear_order_bot` or `complete_linear_order`. -/ open_locale classical section has_Sup variables [has_Sup α] /-- `has_Sup` structure on a nonempty subset `s` of an object with `has_Sup`. This definition is non-canonical (it uses `default s`); it should be used only as here, as an auxiliary instance in the construction of the `conditionally_complete_linear_order` structure. -/ noncomputable def subset_has_Sup [inhabited s] : has_Sup s := {Sup := λ t, if ht : Sup (coe '' t : set α) ∈ s then ⟨Sup (coe '' t : set α), ht⟩ else default s} local attribute [instance] subset_has_Sup @[simp] lemma subset_Sup_def [inhabited s] : @Sup s _ = λ t, if ht : Sup (coe '' t : set α) ∈ s then ⟨Sup (coe '' t : set α), ht⟩ else default s := rfl lemma subset_Sup_of_within [inhabited s] {t : set s} (h : Sup (coe '' t : set α) ∈ s) : Sup (coe '' t : set α) = (@Sup s _ t : α) := by simp [dif_pos h] end has_Sup section has_Inf variables [has_Inf α] /-- `has_Inf` structure on a nonempty subset `s` of an object with `has_Inf`. This definition is non-canonical (it uses `default s`); it should be used only as here, as an auxiliary instance in the construction of the `conditionally_complete_linear_order` structure. -/ noncomputable def subset_has_Inf [inhabited s] : has_Inf s := {Inf := λ t, if ht : Inf (coe '' t : set α) ∈ s then ⟨Inf (coe '' t : set α), ht⟩ else default s} local attribute [instance] subset_has_Inf @[simp] lemma subset_Inf_def [inhabited s] : @Inf s _ = λ t, if ht : Inf (coe '' t : set α) ∈ s then ⟨Inf (coe '' t : set α), ht⟩ else default s := rfl lemma subset_Inf_of_within [inhabited s] {t : set s} (h : Inf (coe '' t : set α) ∈ s) : Inf (coe '' t : set α) = (@Inf s _ t : α) := by simp [dif_pos h] end has_Inf variables [conditionally_complete_linear_order α] local attribute [instance] subset_has_Sup local attribute [instance] subset_has_Inf /-- For a nonempty subset of a conditionally complete linear order to be a conditionally complete linear order, it suffices that it contain the `Sup` of all its nonempty bounded-above subsets, and the `Inf` of all its nonempty bounded-below subsets. See note [reducible non-instances]. -/ @[reducible] noncomputable def subset_conditionally_complete_linear_order [inhabited s] (h_Sup : ∀ {t : set s} (ht : t.nonempty) (h_bdd : bdd_above t), Sup (coe '' t : set α) ∈ s) (h_Inf : ∀ {t : set s} (ht : t.nonempty) (h_bdd : bdd_below t), Inf (coe '' t : set α) ∈ s) : conditionally_complete_linear_order s := { le_cSup := begin rintros t c h_bdd hct, -- The following would be a more natural way to finish, but gives a "deep recursion" error: -- simpa [subset_Sup_of_within (h_Sup t)] using -- (strict_mono_coe s).monotone.le_cSup_image hct h_bdd, have := (subtype.mono_coe s).le_cSup_image hct h_bdd, rwa subset_Sup_of_within s (h_Sup ⟨c, hct⟩ h_bdd) at this, end, cSup_le := begin rintros t B ht hB, have := (subtype.mono_coe s).cSup_image_le ht hB, rwa subset_Sup_of_within s (h_Sup ht ⟨B, hB⟩) at this, end, le_cInf := begin intros t B ht hB, have := (subtype.mono_coe s).le_cInf_image ht hB, rwa subset_Inf_of_within s (h_Inf ht ⟨B, hB⟩) at this, end, cInf_le := begin rintros t c h_bdd hct, have := (subtype.mono_coe s).cInf_image_le hct h_bdd, rwa subset_Inf_of_within s (h_Inf ⟨c, hct⟩ h_bdd) at this, end, ..subset_has_Sup s, ..subset_has_Inf s, ..distrib_lattice.to_lattice s, ..(infer_instance : linear_order s) } section ord_connected /-- The `Sup` function on a nonempty `ord_connected` set `s` in a conditionally complete linear order takes values within `s`, for all nonempty bounded-above subsets of `s`. -/ lemma Sup_within_of_ord_connected {s : set α} [hs : ord_connected s] ⦃t : set s⦄ (ht : t.nonempty) (h_bdd : bdd_above t) : Sup (coe '' t : set α) ∈ s := begin obtain ⟨c, hct⟩ : ∃ c, c ∈ t := ht, obtain ⟨B, hB⟩ : ∃ B, B ∈ upper_bounds t := h_bdd, refine hs.out c.2 B.2 ⟨_, _⟩, { exact (subtype.mono_coe s).le_cSup_image hct ⟨B, hB⟩ }, { exact (subtype.mono_coe s).cSup_image_le ⟨c, hct⟩ hB }, end /-- The `Inf` function on a nonempty `ord_connected` set `s` in a conditionally complete linear order takes values within `s`, for all nonempty bounded-below subsets of `s`. -/ lemma Inf_within_of_ord_connected {s : set α} [hs : ord_connected s] ⦃t : set s⦄ (ht : t.nonempty) (h_bdd : bdd_below t) : Inf (coe '' t : set α) ∈ s := begin obtain ⟨c, hct⟩ : ∃ c, c ∈ t := ht, obtain ⟨B, hB⟩ : ∃ B, B ∈ lower_bounds t := h_bdd, refine hs.out B.2 c.2 ⟨_, _⟩, { exact (subtype.mono_coe s).le_cInf_image ⟨c, hct⟩ hB }, { exact (subtype.mono_coe s).cInf_image_le hct ⟨B, hB⟩ }, end /-- A nonempty `ord_connected` set in a conditionally complete linear order is naturally a conditionally complete linear order. -/ noncomputable instance ord_connected_subset_conditionally_complete_linear_order [inhabited s] [ord_connected s] : conditionally_complete_linear_order s := subset_conditionally_complete_linear_order s Sup_within_of_ord_connected Inf_within_of_ord_connected end ord_connected end subtype
fc44fba508bcfd63eb811eccd4d2793e975eabba
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/tactic/omega/find_ees.lean
83968c777d89856f5e8728ef1e4f02ca9ad19dbb
[ "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
5,568
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Seul Baek -/ /- A tactic for finding a sequence of equality elimination rules for a given set of constraints. -/ import tactic.omega.eq_elim variables {α β : Type} open tactic namespace omega /-- The state of equality elimination proof search. `eqs` is the list of equality constraints, and each `t ∈ eqs` represents the constraint `0 = t`. Similarly, `les` is the list of inequality constraints, and each `t ∈ eqs` represents the constraint `0 < t`. `ees` is the sequence of equality elimination steps that have been used so far to obtain the current set of constraints. The list `ees` grows over time until `eqs` becomes empty. -/ @[derive inhabited] structure ee_state := (eqs : list term) (les : list term) (ees : list ee) @[reducible] meta def eqelim := state_t ee_state tactic meta def abort {α : Type} : eqelim α := ⟨λ x, failed⟩ private meta def mk_eqelim_state (eqs les : list term) : tactic ee_state := return (ee_state.mk eqs les []) /-- Get the current list of equality constraints. -/ meta def get_eqs : eqelim (list term) := ee_state.eqs <$> get /-- Get the current list of inequality constraints. -/ meta def get_les : eqelim (list term) := ee_state.les <$> get /-- Get the current sequence of equality elimiation steps. -/ meta def get_ees : eqelim (list ee) := ee_state.ees <$> get /-- Update the list of equality constraints. -/ meta def set_eqs (eqs : list term) : eqelim unit := modify $ λ s, {eqs := eqs, ..s} /-- Update the list of inequality constraints. -/ meta def set_les (les : list term) : eqelim unit := modify $ λ s, {les := les, ..s} /-- Update the sequence of equality elimiation steps. -/ meta def set_ees (es : list ee) : eqelim unit := modify $ λ s, {ees := es, ..s} /-- Add a new step to the sequence of equality elimination steps. -/ meta def add_ee (e : ee) : eqelim unit := do es ← get_ees, set_ees (es ++ [e]) /-- Return the first equality constraint in the current list of equality constraints. The returned constraint is 'popped' and no longer available in the state. -/ meta def head_eq : eqelim term := do eqs ← get_eqs, match eqs with | [] := abort | (eq::eqs') := set_eqs eqs' >> pure eq end meta def run {α : Type} (eqs les : list term) (r : eqelim α) : tactic α := prod.fst <$> (mk_eqelim_state eqs les >>= r.run) /-- If `t1` succeeds and returns a value, 'commit' to that choice and run `t3` with the returned value as argument. Do not backtrack to try `t2` even if `t3` fails. If `t1` fails outright, run `t2`. -/ meta def ee_commit (t1 : eqelim α) (t2 : eqelim β) (t3 : α → eqelim β) : eqelim β := do x ← ((t1 >>= return ∘ some) <|> return none), match x with | none := t2 | (some a) := t3 a end local notation t1 ` !>>= ` t2 `; ` t3 := ee_commit t1 t2 t3 private meta def of_tactic {α : Type} : tactic α → eqelim α := state_t.lift /-- GCD of all elements of the list. -/ def gcd : list int → nat | [] := 0 | (i::is) := nat.gcd i.nat_abs (gcd is) /-- GCD of all coefficients in a term. -/ meta def get_gcd (t : term) : eqelim int := pure ↑(gcd t.snd) /-- Divide a term by an integer if the integer divides the constant component of the term. It is assumed that the integer also divides all coefficients of the term. -/ meta def factor (i : int) (t : term) : eqelim term := if i ∣ t.fst then add_ee (ee.factor i) >> pure (t.div i) else abort /-- If list has a nonzero element, return the minimum element (by absolute value) with its index. Otherwise, return none. -/ meta def find_min_coeff_core : list int → eqelim (int × nat) | [] := abort | (i::is) := (do (j,n) ← find_min_coeff_core is, if i ≠ 0 ∧ i.nat_abs ≤ j.nat_abs then pure (i,0) else pure (j,n+1)) <|> (if i = (0 : int) then abort else pure (i,0)) /-- Find and return the smallest coefficient (by absolute value) in a term, along with the coefficient's variable index and the term itself. If the coefficient is negative, negate both the coefficient and the term before returning them. -/ meta def find_min_coeff (t : term) : eqelim (int × nat × term) := do (i,n) ← find_min_coeff_core t.snd, if 0 < i then pure (i,n,t) else add_ee (ee.neg) >> pure (-i,n,t.neg) /-- Find an appropriate equality elimination step for the current state and apply it. -/ meta def elim_eq : eqelim unit := do t ← head_eq, i ← get_gcd t, factor i t !>>= (set_eqs [] >> add_ee (ee.nondiv i)); λ s, find_min_coeff s !>>= add_ee ee.drop; λ ⟨i, n, u⟩, if i = 1 then do eqs ← get_eqs, les ← get_les, set_eqs (eqs.map (cancel n u)), set_les (les.map (cancel n u)), add_ee (ee.cancel n) else let v : term := coeffs_reduce n u.fst u.snd in let r : term := rhs n u.fst u.snd in do eqs ← get_eqs, les ← get_les, set_eqs (v::eqs.map (subst n r)), set_les (les.map (subst n r)), add_ee (ee.reduce n), elim_eq /-- Find and return the sequence of steps for eliminating all equality constraints in the current state. -/ meta def elim_eqs : eqelim (list ee) := elim_eq !>>= get_ees; λ _, elim_eqs /-- Given a linear constrain clause, return a list of steps for eliminating its equality constraints. -/ meta def find_ees : clause → tactic (list ee) | (eqs, les) := run eqs les elim_eqs end omega
69b86af4cd3f7403a31833af147b35eba3642d1e
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/order/with_zero.lean
33eba665df98db3271bfa1ba42fd3f71a87c2faf
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
12,922
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Johan Commelin, Patrick Massot -/ import algebra.order.group import tactic.abel /-! # Linearly ordered commutative groups and monoids with a zero element adjoined This file sets up a special class of linearly ordered commutative monoids that show up as the target of so-called “valuations” in algebraic number theory. Usually, in the informal literature, these objects are constructed by taking a linearly ordered commutative group Γ and formally adjoining a zero element: Γ ∪ {0}. The disadvantage is that a type such as `nnreal` is not of that form, whereas it is a very common target for valuations. The solutions is to use a typeclass, and that is exactly what we do in this file. Note that to avoid issues with import cycles, `linear_ordered_comm_monoid_with_zero` is defined in another file. However, the lemmas about it are stated here. -/ set_option old_structure_cmd true /-- A linearly ordered commutative group with a zero element. -/ class linear_ordered_comm_group_with_zero (α : Type*) extends linear_ordered_comm_monoid_with_zero α, comm_group_with_zero α variables {α : Type*} variables {a b c d x y z : α} instance [linear_ordered_add_comm_monoid_with_top α] : linear_ordered_comm_monoid_with_zero (multiplicative αᵒᵈ) := { zero := multiplicative.of_add (⊤ : α), zero_mul := top_add, mul_zero := add_top, zero_le_one := (le_top : (0 : α) ≤ ⊤), ..multiplicative.ordered_comm_monoid, ..multiplicative.linear_order } instance [linear_ordered_add_comm_group_with_top α] : linear_ordered_comm_group_with_zero (multiplicative αᵒᵈ) := { inv_zero := linear_ordered_add_comm_group_with_top.neg_top, mul_inv_cancel := linear_ordered_add_comm_group_with_top.add_neg_cancel, ..multiplicative.div_inv_monoid, ..multiplicative.linear_ordered_comm_monoid_with_zero, ..multiplicative.nontrivial } section monoid variable [monoid α] section preorder variable [preorder α] section left variable [covariant_class α α (*) (≤)] lemma left.one_le_pow_of_le : ∀ {n : ℕ} {x : α}, 1 ≤ x → 1 ≤ x^n | 0 x _ := (pow_zero x).symm.le | (n + 1) x H := calc 1 ≤ x : H ... = x * 1 : (mul_one x).symm ... ≤ x * x ^ n : mul_le_mul_left' (left.one_le_pow_of_le H) x ... = x ^ n.succ : (pow_succ x n).symm end left section right variable [covariant_class α α (function.swap (*)) (≤)] lemma right.one_le_pow_of_le {x : α} (H : 1 ≤ x) : ∀ {n : ℕ}, 1 ≤ x^n | 0 := (pow_zero _).symm.le | (n + 1) := calc 1 ≤ x : H ... = 1 * x : (one_mul x).symm ... ≤ x ^ n * x : mul_le_mul_right' right.one_le_pow_of_le x ... = x ^ n.succ : (pow_succ' x n).symm lemma right.pow_le_one_of_le {x : α} (H : x ≤ 1) : ∀ {n : ℕ}, x^n ≤ 1 | 0 := (pow_zero _).le | (n + 1) := calc x ^ n.succ = x ^ n * x : pow_succ' x n ... ≤ 1 * x : mul_le_mul_right' right.pow_le_one_of_le x ... = x : one_mul x ... ≤ 1 : H end right lemma pow_le_pow_of_le [covariant_class α α (*) (≤)] [covariant_class α α (function.swap (*)) (≤)] {x y : α} (H : x ≤ y) : ∀ {n : ℕ} , x^n ≤ y^n | 0 := (pow_zero _).le.trans (pow_zero _).symm.le | (n + 1) := calc x ^ n.succ = x * x ^ n : pow_succ x n ... ≤ y * x ^ n : mul_le_mul_right' H (x ^ n) ... ≤ y * y ^ n : mul_le_mul_left' pow_le_pow_of_le y ... = y ^ n.succ : (pow_succ y n).symm lemma left.pow_lt_one_of_lt [covariant_class α α (*) (<)] {n : ℕ} {x : α} (n0 : 0 < n) (H : x < 1) : x^n < 1 := begin refine nat.le_induction ((pow_one _).le.trans_lt H) (λ n n1 hn, _) _ (nat.succ_le_iff.mpr n0), calc x ^ (n + 1) = x * x ^ n : pow_succ x n ... < x * 1 : mul_lt_mul_left' hn x ... = x : mul_one x ... < 1 : H end lemma left.pow_lt_one_iff {α: Type*} [monoid α] [linear_order α] [covariant_class α α (*) (<)] {n : ℕ} {x : α} (n0 : 0 < n) : x^n < 1 ↔ x < 1 := ⟨λ H, not_le.mp (λ k, not_le.mpr H (by { haveI := has_mul.to_covariant_class_left α, exact left.one_le_pow_of_le k})), left.pow_lt_one_of_lt n0⟩ lemma right.pow_lt_one_of_lt [covariant_class α α (function.swap (*)) (<)] {n : ℕ} {x : α} (n0 : 0 < n) (H : x < 1) : x^n < 1 := begin refine nat.le_induction ((pow_one _).le.trans_lt H) (λ n n1 hn, _) _ (nat.succ_le_iff.mpr n0), calc x ^ (n + 1) = x ^ n * x : pow_succ' x n ... < 1 * x : mul_lt_mul_right' hn x ... = x : one_mul x ... < 1 : H end lemma right.pow_lt_one_iff {α: Type*} [monoid α] [linear_order α] [covariant_class α α (function.swap (*)) (<)] {n : ℕ} {x : α} (n0 : 0 < n) : x^n < 1 ↔ x < 1 := ⟨λ H, not_le.mp (λ k, not_le.mpr H (by { haveI := has_mul.to_covariant_class_right α, exact right.one_le_pow_of_le k})), right.pow_lt_one_of_lt n0⟩ end preorder section left_right variables [linear_order α] [covariant_class α α (*) (≤)] [covariant_class α α (function.swap (*)) (≤)] end left_right end monoid instance [linear_ordered_comm_monoid α] : linear_ordered_comm_monoid_with_zero (with_zero α) := { mul_le_mul_left := λ x y, mul_le_mul_left', zero_le_one := with_zero.zero_le _, ..with_zero.linear_order, ..with_zero.comm_monoid_with_zero } instance [linear_ordered_comm_group α] : linear_ordered_comm_group_with_zero (with_zero α) := { ..with_zero.linear_ordered_comm_monoid_with_zero, ..with_zero.comm_group_with_zero } section linear_ordered_comm_monoid variables [linear_ordered_comm_monoid_with_zero α] /- The following facts are true more generally in a (linearly) ordered commutative monoid. -/ /-- Pullback a `linear_ordered_comm_monoid_with_zero` under an injective map. See note [reducible non-instances]. -/ @[reducible] def function.injective.linear_ordered_comm_monoid_with_zero {β : Type*} [has_zero β] [has_one β] [has_mul β] [has_pow β ℕ] (f : β → α) (hf : function.injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) : linear_ordered_comm_monoid_with_zero β := { zero_le_one := show f 0 ≤ f 1, by simp only [zero, one, linear_ordered_comm_monoid_with_zero.zero_le_one], ..linear_order.lift f hf, ..hf.ordered_comm_monoid f one mul npow, ..hf.comm_monoid_with_zero f zero one mul npow } lemma zero_le_one' : (0 : α) ≤ 1 := linear_ordered_comm_monoid_with_zero.zero_le_one @[simp] lemma zero_le' : 0 ≤ a := by simpa only [mul_zero, mul_one] using mul_le_mul_left' (@zero_le_one' α _) a @[simp] lemma not_lt_zero' : ¬a < 0 := not_lt_of_le zero_le' @[simp] lemma le_zero_iff : a ≤ 0 ↔ a = 0 := ⟨λ h, le_antisymm h zero_le', λ h, h ▸ le_rfl⟩ lemma zero_lt_iff : 0 < a ↔ a ≠ 0 := ⟨ne_of_gt, λ h, lt_of_le_of_ne zero_le' h.symm⟩ lemma ne_zero_of_lt (h : b < a) : a ≠ 0 := λ h1, not_lt_zero' $ show b < 0, from h1 ▸ h lemma pow_pos_iff [no_zero_divisors α] {n : ℕ} (hn : 0 < n) : 0 < a ^ n ↔ 0 < a := by simp_rw [zero_lt_iff, pow_ne_zero_iff hn] instance : linear_ordered_add_comm_monoid_with_top (additive αᵒᵈ) := { top := (0 : α), top_add' := λ a, (zero_mul a : (0 : α) * a = 0), le_top := λ _, zero_le', ..additive.ordered_add_comm_monoid, ..additive.linear_order } end linear_ordered_comm_monoid variables [linear_ordered_comm_group_with_zero α] lemma zero_lt_one₀ : (0 : α) < 1 := lt_of_le_of_ne zero_le_one' zero_ne_one lemma le_of_le_mul_right (h : c ≠ 0) (hab : a * c ≤ b * c) : a ≤ b := by simpa only [mul_inv_cancel_right₀ h] using (mul_le_mul_right' hab c⁻¹) lemma le_mul_inv_of_mul_le (h : c ≠ 0) (hab : a * c ≤ b) : a ≤ b * c⁻¹ := le_of_le_mul_right h (by simpa [h] using hab) lemma mul_inv_le_of_le_mul (hab : a ≤ b * c) : a * c⁻¹ ≤ b := begin by_cases h : c = 0, { simp [h], }, { exact le_of_le_mul_right h (by simpa [h] using hab), }, end lemma le_mul_inv_iff₀ (hc : c ≠ 0) : a ≤ b * c⁻¹ ↔ a * c ≤ b := ⟨λ h, inv_inv c ▸ mul_inv_le_of_le_mul h, le_mul_inv_of_mul_le hc⟩ lemma mul_inv_le_iff₀ (hc : c ≠ 0) : a * c⁻¹ ≤ b ↔ a ≤ b * c := ⟨λ h, inv_inv c ▸ le_mul_inv_of_mul_le (inv_ne_zero hc) h, mul_inv_le_of_le_mul⟩ lemma div_le_div₀ (a b c d : α) (hb : b ≠ 0) (hd : d ≠ 0) : a * b⁻¹ ≤ c * d⁻¹ ↔ a * d ≤ c * b := if ha : a = 0 then by simp [ha] else if hc : c = 0 then by simp [inv_ne_zero hb, hc, hd] else show (units.mk0 a ha) * (units.mk0 b hb)⁻¹ ≤ (units.mk0 c hc) * (units.mk0 d hd)⁻¹ ↔ (units.mk0 a ha) * (units.mk0 d hd) ≤ (units.mk0 c hc) * (units.mk0 b hb), from mul_inv_le_mul_inv_iff' @[simp] lemma units.zero_lt (u : αˣ) : (0 : α) < u := zero_lt_iff.2 $ u.ne_zero lemma mul_lt_mul_of_lt_of_le₀ (hab : a ≤ b) (hb : b ≠ 0) (hcd : c < d) : a * c < b * d := have hd : d ≠ 0 := ne_zero_of_lt hcd, if ha : a = 0 then by { rw [ha, zero_mul, zero_lt_iff], exact mul_ne_zero hb hd } else if hc : c = 0 then by { rw [hc, mul_zero, zero_lt_iff], exact mul_ne_zero hb hd } else show (units.mk0 a ha) * (units.mk0 c hc) < (units.mk0 b hb) * (units.mk0 d hd), from mul_lt_mul_of_le_of_lt hab hcd lemma mul_lt_mul₀ (hab : a < b) (hcd : c < d) : a * c < b * d := mul_lt_mul_of_lt_of_le₀ hab.le (ne_zero_of_lt hab) hcd lemma mul_inv_lt_of_lt_mul₀ (h : x < y * z) : x * z⁻¹ < y := by { contrapose! h, simpa only [inv_inv] using mul_inv_le_of_le_mul h } lemma inv_mul_lt_of_lt_mul₀ (h : x < y * z) : y⁻¹ * x < z := by { rw mul_comm at *, exact mul_inv_lt_of_lt_mul₀ h } lemma mul_lt_right₀ (c : α) (h : a < b) (hc : c ≠ 0) : a * c < b * c := by { contrapose! h, exact le_of_le_mul_right hc h } lemma pow_lt_pow_succ {x : α} {n : ℕ} (hx : 1 < x) : x ^ n < x ^ n.succ := by { rw [← one_mul (x ^ n), pow_succ], exact mul_lt_right₀ _ hx (pow_ne_zero _ $ ne_of_gt (lt_trans zero_lt_one₀ hx)) } lemma pow_lt_pow₀ {x : α} {m n : ℕ} (hx : 1 < x) (hmn : m < n) : x ^ m < x ^ n := by { induction hmn with n hmn ih, exacts [pow_lt_pow_succ hx, lt_trans ih (pow_lt_pow_succ hx)] } lemma inv_lt_inv₀ (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ < b⁻¹ ↔ b < a := show (units.mk0 a ha)⁻¹ < (units.mk0 b hb)⁻¹ ↔ (units.mk0 b hb) < (units.mk0 a ha), from inv_lt_inv_iff lemma inv_le_inv₀ (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ ≤ b⁻¹ ↔ b ≤ a := show (units.mk0 a ha)⁻¹ ≤ (units.mk0 b hb)⁻¹ ↔ (units.mk0 b hb) ≤ (units.mk0 a ha), from inv_le_inv_iff lemma lt_of_mul_lt_mul_of_le₀ (h : a * b < c * d) (hc : 0 < c) (hh : c ≤ a) : b < d := begin have ha : a ≠ 0 := ne_of_gt (lt_of_lt_of_le hc hh), simp_rw ← inv_le_inv₀ ha (ne_of_gt hc) at hh, have := mul_lt_mul_of_lt_of_le₀ hh (inv_ne_zero (ne_of_gt hc)) h, simpa [inv_mul_cancel_left₀ ha, inv_mul_cancel_left₀ (ne_of_gt hc)] using this, end lemma mul_le_mul_right₀ (hc : c ≠ 0) : a * c ≤ b * c ↔ a ≤ b := ⟨le_of_le_mul_right hc, λ hab, mul_le_mul_right' hab _⟩ lemma div_le_div_right₀ (hc : c ≠ 0) : a/c ≤ b/c ↔ a ≤ b := by rw [div_eq_mul_inv, div_eq_mul_inv, mul_le_mul_right₀ (inv_ne_zero hc)] lemma le_div_iff₀ (hc : c ≠ 0) : a ≤ b/c ↔ a*c ≤ b := by rw [div_eq_mul_inv, le_mul_inv_iff₀ hc] lemma div_le_iff₀ (hc : c ≠ 0) : a/c ≤ b ↔ a ≤ b*c := by rw [div_eq_mul_inv, mul_inv_le_iff₀ hc] instance : linear_ordered_add_comm_group_with_top (additive αᵒᵈ) := { neg_top := inv_zero, add_neg_cancel := λ a ha, mul_inv_cancel ha, ..additive.sub_neg_monoid, ..additive.linear_ordered_add_comm_monoid_with_top, ..additive.nontrivial } namespace monoid_hom variables {R : Type*} [ring R] (f : R →* α) theorem map_neg_one : f (-1) = 1 := (pow_eq_one_iff (nat.succ_ne_zero 1)).1 $ calc f (-1) ^ 2 = f (-1) * f(-1) : sq _ ... = f ((-1) * - 1) : (f.map_mul _ _).symm ... = f ( - - 1) : congr_arg _ (neg_one_mul _) ... = f 1 : congr_arg _ (neg_neg _) ... = 1 : map_one f @[simp] lemma map_neg (x : R) : f (-x) = f x := calc f (-x) = f (-1 * x) : congr_arg _ (neg_one_mul _).symm ... = f (-1) * f x : map_mul _ _ _ ... = 1 * f x : _root_.congr_arg (λ g, g * (f x)) (map_neg_one f) ... = f x : one_mul _ lemma map_sub_swap (x y : R) : f (x - y) = f (y - x) := calc f (x - y) = f (-(y - x)) : congr_arg _ (neg_sub _ _).symm ... = _ : map_neg _ _ end monoid_hom
34c4f6e7a9fab6cee39bce0e8077eb8357013d55
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/list/func_auto.lean
81a67a6e69af13ec8da519fda9ee9bd7c20dbd8c
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
6,554
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Seul Baek -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.nat.basic import Mathlib.PostPort universes u v w namespace Mathlib namespace list namespace func /- Definitions for using lists as finite representations of functions with domain ℕ. -/ def neg {α : Type u} [Neg α] (as : List α) : List α := map (fun (a : α) => -a) as @[simp] def set {α : Type u} [Inhabited α] (a : α) : List α → ℕ → List α := sorry @[simp] def get {α : Type u} [Inhabited α] : ℕ → List α → α := sorry def equiv {α : Type u} [Inhabited α] (as1 : List α) (as2 : List α) := ∀ (m : ℕ), get m as1 = get m as2 @[simp] def pointwise {α : Type u} {β : Type v} {γ : Type w} [Inhabited α] [Inhabited β] (f : α → β → γ) : List α → List β → List γ := sorry def add {α : Type u} [HasZero α] [Add α] : List α → List α → List α := pointwise Add.add def sub {α : Type u} [HasZero α] [Sub α] : List α → List α → List α := pointwise Sub.sub /- set -/ theorem length_set {α : Type u} {a : α} [Inhabited α] {m : ℕ} {as : List α} : length (set a as m) = max (length as) (m + 1) := sorry @[simp] theorem get_nil {α : Type u} [Inhabited α] {k : ℕ} : get k [] = Inhabited.default := nat.cases_on k (Eq.refl (get 0 [])) fun (k : ℕ) => Eq.refl (get (Nat.succ k) []) theorem get_eq_default_of_le {α : Type u} [Inhabited α] (k : ℕ) {as : List α} : length as ≤ k → get k as = Inhabited.default := sorry @[simp] theorem get_set {α : Type u} [Inhabited α] {a : α} {k : ℕ} {as : List α} : get k (set a as k) = a := sorry theorem eq_get_of_mem {α : Type u} [Inhabited α] {a : α} {as : List α} : a ∈ as → ∃ (n : ℕ), α → a = get n as := sorry theorem mem_get_of_le {α : Type u} [Inhabited α] {n : ℕ} {as : List α} : n < length as → get n as ∈ as := sorry theorem mem_get_of_ne_zero {α : Type u} [Inhabited α] {n : ℕ} {as : List α} : get n as ≠ Inhabited.default → get n as ∈ as := sorry theorem get_set_eq_of_ne {α : Type u} [Inhabited α] {a : α} {as : List α} (k : ℕ) (m : ℕ) : m ≠ k → get m (set a as k) = get m as := sorry theorem get_map {α : Type u} {β : Type v} [Inhabited α] [Inhabited β] {f : α → β} {n : ℕ} {as : List α} : n < length as → get n (map f as) = f (get n as) := sorry theorem get_map' {α : Type u} {β : Type v} [Inhabited α] [Inhabited β] {f : α → β} {n : ℕ} {as : List α} : f Inhabited.default = Inhabited.default → get n (map f as) = f (get n as) := sorry theorem forall_val_of_forall_mem {α : Type u} [Inhabited α] {as : List α} {p : α → Prop} : p Inhabited.default → (∀ (x : α), x ∈ as → p x) → ∀ (n : ℕ), p (get n as) := sorry /- equiv -/ theorem equiv_refl {α : Type u} {as : List α} [Inhabited α] : equiv as as := fun (k : ℕ) => rfl theorem equiv_symm {α : Type u} {as1 : List α} {as2 : List α} [Inhabited α] : equiv as1 as2 → equiv as2 as1 := fun (h1 : equiv as1 as2) (k : ℕ) => Eq.symm (h1 k) theorem equiv_trans {α : Type u} {as1 : List α} {as2 : List α} {as3 : List α} [Inhabited α] : equiv as1 as2 → equiv as2 as3 → equiv as1 as3 := fun (h1 : equiv as1 as2) (h2 : equiv as2 as3) (k : ℕ) => Eq.trans (h1 k) (h2 k) theorem equiv_of_eq {α : Type u} {as1 : List α} {as2 : List α} [Inhabited α] : as1 = as2 → equiv as1 as2 := fun (h1 : as1 = as2) => eq.mpr (id (Eq._oldrec (Eq.refl (equiv as1 as2)) h1)) equiv_refl theorem eq_of_equiv {α : Type u} [Inhabited α] {as1 : List α} {as2 : List α} : length as1 = length as2 → equiv as1 as2 → as1 = as2 := sorry end func -- We want to drop the `inhabited` instances for a moment, -- so we close and open the namespace namespace func /- neg -/ @[simp] theorem get_neg {α : Type u} [add_group α] {k : ℕ} {as : List α} : get k (neg as) = -get k as := sorry @[simp] theorem length_neg {α : Type u} [Neg α] (as : List α) : length (neg as) = length as := sorry /- pointwise -/ theorem nil_pointwise {α : Type u} {β : Type v} {γ : Type w} [Inhabited α] [Inhabited β] {f : α → β → γ} (bs : List β) : pointwise f [] bs = map (f Inhabited.default) bs := sorry theorem pointwise_nil {α : Type u} {β : Type v} {γ : Type w} [Inhabited α] [Inhabited β] {f : α → β → γ} (as : List α) : pointwise f as [] = map (fun (a : α) => f a Inhabited.default) as := sorry theorem get_pointwise {α : Type u} {β : Type v} {γ : Type w} [Inhabited α] [Inhabited β] [Inhabited γ] {f : α → β → γ} (h1 : f Inhabited.default Inhabited.default = Inhabited.default) (k : ℕ) (as : List α) (bs : List β) : get k (pointwise f as bs) = f (get k as) (get k bs) := sorry theorem length_pointwise {α : Type u} {β : Type v} {γ : Type w} [Inhabited α] [Inhabited β] {f : α → β → γ} {as : List α} {bs : List β} : length (pointwise f as bs) = max (length as) (length bs) := sorry end func namespace func /- add -/ @[simp] theorem get_add {α : Type u} [add_monoid α] {k : ℕ} {xs : List α} {ys : List α} : get k (add xs ys) = get k xs + get k ys := get_pointwise (zero_add Inhabited.default) k xs ys @[simp] theorem length_add {α : Type u} [HasZero α] [Add α] {xs : List α} {ys : List α} : length (add xs ys) = max (length xs) (length ys) := length_pointwise @[simp] theorem nil_add {α : Type u} [add_monoid α] (as : List α) : add [] as = as := sorry @[simp] theorem add_nil {α : Type u} [add_monoid α] (as : List α) : add as [] = as := sorry theorem map_add_map {α : Type u} [add_monoid α] (f : α → α) (g : α → α) {as : List α} : add (map f as) (map g as) = map (fun (x : α) => f x + g x) as := sorry /- sub -/ @[simp] theorem get_sub {α : Type u} [add_group α] {k : ℕ} {xs : List α} {ys : List α} : get k (sub xs ys) = get k xs - get k ys := get_pointwise (sub_zero Inhabited.default) k xs ys @[simp] theorem length_sub {α : Type u} [HasZero α] [Sub α] {xs : List α} {ys : List α} : length (sub xs ys) = max (length xs) (length ys) := length_pointwise @[simp] theorem nil_sub {α : Type} [add_group α] (as : List α) : sub [] as = neg as := sorry @[simp] theorem sub_nil {α : Type} [add_group α] (as : List α) : sub as [] = as := sorry end Mathlib
0079335e814b77ff6fe5a49eeb6357714d6a65ef
b3fced0f3ff82d577384fe81653e47df68bb2fa1
/src/measure_theory/measurable_space.lean
c2b53de9234062096219ed48c6bf98f191147cdc
[ "Apache-2.0" ]
permissive
ratmice/mathlib
93b251ef5df08b6fd55074650ff47fdcc41a4c75
3a948a6a4cd5968d60e15ed914b1ad2f4423af8d
refs/heads/master
1,599,240,104,318
1,572,981,183,000
1,572,981,183,000
219,830,178
0
0
Apache-2.0
1,572,980,897,000
1,572,980,896,000
null
UTF-8
Lean
false
false
42,876
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 Measurable spaces -- σ-algberas -/ import data.set.disjointed order.galois_connection data.set.countable /-! # Measurable spaces and measurable functions This file defines measurable spaces and the functions and isomorphisms between them. A measurable space is a set equipped with a σ-algebra, a collection of subsets closed under complementation and countable union. A function between measurable spaces is measurable if the preimage of each measurable subset is measurable. σ-algebras on a fixed set α form a complete lattice. Here we order σ-algebras by writing m₁ ≤ m₂ if every set which is m₁-measurable is also m₂-measurable (that is, m₁ is a subset of m₂). In particular, any collection of subsets of α generates a smallest σ-algebra which contains all of them. A function f : α → β induces a Galois connection between the lattices of σ-algebras on α and β. A measurable equivalence between measurable spaces is an equivalence which respects the σ-algebras, that is, for which both directions of the equivalence are measurable functions. ## Main statements The main theorem of this file is Dynkin's π-λ theorem, which appears here as an induction principle `induction_on_inter`. Suppose s is a collection of subsets of α such that the intersection of two members of s belongs to s whenever it is nonempty. Let m be the σ-algebra generated by s. In order to check that a predicate C holds on every member of m, it suffices to check that C holds on the members of s and that C is preserved by complementation and *disjoint* countable unions. ## Implementation notes Measurability of a function f : α → β between measurable spaces is defined in terms of the Galois connection induced by f. ## References * https://en.wikipedia.org/wiki/Measurable_space * https://en.wikipedia.org/wiki/Sigma-algebra * https://en.wikipedia.org/wiki/Dynkin_system ## Tags measurable space, measurable function, dynkin system -/ local attribute [instance] classical.prop_decidable open set lattice encodable open_locale classical universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} {ι : Sort x} {s t u : set α} structure measurable_space (α : Type u) := (is_measurable : set α → Prop) (is_measurable_empty : is_measurable ∅) (is_measurable_compl : ∀s, is_measurable s → is_measurable (- s)) (is_measurable_Union : ∀f:ℕ → set α, (∀i, is_measurable (f i)) → is_measurable (⋃i, f i)) attribute [class] measurable_space section variable [measurable_space α] /-- `is_measurable s` means that `s` is measurable (in the ambient measure space on `α`) -/ def is_measurable : set α → Prop := ‹measurable_space α›.is_measurable lemma is_measurable.empty : is_measurable (∅ : set α) := ‹measurable_space α›.is_measurable_empty lemma is_measurable.compl : is_measurable s → is_measurable (-s) := ‹measurable_space α›.is_measurable_compl s lemma is_measurable.compl_iff : is_measurable (-s) ↔ is_measurable s := ⟨λ h, by simpa using h.compl, is_measurable.compl⟩ lemma is_measurable.univ : is_measurable (univ : set α) := by simpa using (@is_measurable.empty α _).compl lemma encodable.Union_decode2 {α} [encodable β] (f : β → set α) : (⋃ b, f b) = ⋃ (i : ℕ) (b ∈ decode2 β i), f b := ext $ by simp [mem_decode2, exists_swap] @[elab_as_eliminator] lemma encodable.Union_decode2_cases {α} [encodable β] {f : β → set α} {C : set α → Prop} (H0 : C ∅) (H1 : ∀ b, C (f b)) {n} : C (⋃ b ∈ decode2 β n, f b) := match decode2 β n with | none := by simp; apply H0 | (some b) := by convert H1 b; simp [ext_iff] end lemma is_measurable.Union [encodable β] {f : β → set α} (h : ∀b, is_measurable (f b)) : is_measurable (⋃b, f b) := by rw encodable.Union_decode2; exact ‹measurable_space α›.is_measurable_Union (λ n, ⋃ b ∈ decode2 β n, f b) (λ n, encodable.Union_decode2_cases is_measurable.empty h) lemma is_measurable.bUnion {f : β → set α} {s : set β} (hs : countable s) (h : ∀b∈s, is_measurable (f b)) : is_measurable (⋃b∈s, f b) := begin rw bUnion_eq_Union, haveI := hs.to_encodable, exact is_measurable.Union (by simpa using h) end lemma is_measurable.sUnion {s : set (set α)} (hs : countable s) (h : ∀t∈s, is_measurable t) : is_measurable (⋃₀ s) := by rw sUnion_eq_bUnion; exact is_measurable.bUnion hs h lemma is_measurable.Union_Prop {p : Prop} {f : p → set α} (hf : ∀b, is_measurable (f b)) : is_measurable (⋃b, f b) := by by_cases p; simp [h, hf, is_measurable.empty] lemma is_measurable.Inter [encodable β] {f : β → set α} (h : ∀b, is_measurable (f b)) : is_measurable (⋂b, f b) := is_measurable.compl_iff.1 $ by rw compl_Inter; exact is_measurable.Union (λ b, (h b).compl) lemma is_measurable.bInter {f : β → set α} {s : set β} (hs : countable s) (h : ∀b∈s, is_measurable (f b)) : is_measurable (⋂b∈s, f b) := is_measurable.compl_iff.1 $ by rw compl_bInter; exact is_measurable.bUnion hs (λ b hb, (h b hb).compl) lemma is_measurable.sInter {s : set (set α)} (hs : countable s) (h : ∀t∈s, is_measurable t) : is_measurable (⋂₀ s) := by rw sInter_eq_bInter; exact is_measurable.bInter hs h lemma is_measurable.Inter_Prop {p : Prop} {f : p → set α} (hf : ∀b, is_measurable (f b)) : is_measurable (⋂b, f b) := by by_cases p; simp [h, hf, is_measurable.univ] lemma is_measurable.union {s₁ s₂ : set α} (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) : is_measurable (s₁ ∪ s₂) := by rw union_eq_Union; exact is_measurable.Union (bool.forall_bool.2 ⟨h₂, h₁⟩) lemma is_measurable.inter {s₁ s₂ : set α} (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) : is_measurable (s₁ ∩ s₂) := by rw inter_eq_compl_compl_union_compl; exact (h₁.compl.union h₂.compl).compl lemma is_measurable.diff {s₁ s₂ : set α} (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) : is_measurable (s₁ \ s₂) := h₁.inter h₂.compl lemma is_measurable.sub {s₁ s₂ : set α} : is_measurable s₁ → is_measurable s₂ → is_measurable (s₁ - s₂) := is_measurable.diff lemma is_measurable.disjointed {f : ℕ → set α} (h : ∀i, is_measurable (f i)) (n) : is_measurable (disjointed f n) := disjointed_induct (h n) (assume t i ht, is_measurable.diff ht $ h _) lemma is_measurable.const (p : Prop) : is_measurable {a : α | p} := by by_cases p; simp [h, is_measurable.empty]; apply is_measurable.univ end @[extensionality] lemma measurable_space.ext : ∀{m₁ m₂ : measurable_space α}, (∀s:set α, m₁.is_measurable s ↔ m₂.is_measurable s) → m₁ = m₂ | ⟨s₁, _, _, _⟩ ⟨s₂, _, _, _⟩ h := have s₁ = s₂, from funext $ assume x, propext $ h x, by subst this namespace measurable_space section complete_lattice instance : partial_order (measurable_space α) := { le := λm₁ m₂, m₁.is_measurable ≤ m₂.is_measurable, le_refl := assume a b, le_refl _, le_trans := assume a b c, le_trans, le_antisymm := assume a b h₁ h₂, measurable_space.ext $ assume s, ⟨h₁ s, h₂ s⟩ } /-- The smallest σ-algebra containing a collection `s` of basic sets -/ inductive generate_measurable (s : set (set α)) : set α → Prop | basic : ∀u∈s, generate_measurable u | empty : generate_measurable ∅ | compl : ∀s, generate_measurable s → generate_measurable (-s) | union : ∀f:ℕ → set α, (∀n, generate_measurable (f n)) → generate_measurable (⋃i, f i) /-- Construct the smallest measure space containing a collection of basic sets -/ def generate_from (s : set (set α)) : measurable_space α := { is_measurable := generate_measurable s, is_measurable_empty := generate_measurable.empty s, is_measurable_compl := generate_measurable.compl, is_measurable_Union := generate_measurable.union } lemma is_measurable_generate_from {s : set (set α)} {t : set α} (ht : t ∈ s) : (generate_from s).is_measurable t := generate_measurable.basic t ht lemma generate_from_le {s : set (set α)} {m : measurable_space α} (h : ∀t∈s, m.is_measurable t) : generate_from s ≤ m := assume t (ht : generate_measurable s t), ht.rec_on h (is_measurable_empty m) (assume s _ hs, is_measurable_compl m s hs) (assume f _ hf, is_measurable_Union m f hf) lemma generate_from_le_iff {s : set (set α)} {m : measurable_space α} : generate_from s ≤ m ↔ s ⊆ {t | m.is_measurable t} := iff.intro (assume h u hu, h _ $ is_measurable_generate_from hu) (assume h, generate_from_le h) protected def mk_of_closure (g : set (set α)) (hg : {t | (generate_from g).is_measurable t} = g) : measurable_space α := { is_measurable := λs, s ∈ g, is_measurable_empty := hg ▸ is_measurable_empty _, is_measurable_compl := hg ▸ is_measurable_compl _, is_measurable_Union := hg ▸ is_measurable_Union _ } lemma mk_of_closure_sets {s : set (set α)} {hs : {t | (generate_from s).is_measurable t} = s} : measurable_space.mk_of_closure s hs = generate_from s := measurable_space.ext $ assume t, show t ∈ s ↔ _, by rw [← hs] {occs := occurrences.pos [1] }; refl def gi_generate_from : galois_insertion (@generate_from α) (λm, {t | @is_measurable α m t}) := { gc := assume s m, generate_from_le_iff, le_l_u := assume m s, is_measurable_generate_from, choice := λg hg, measurable_space.mk_of_closure g $ le_antisymm hg $ generate_from_le_iff.1 $ le_refl _, choice_eq := assume g hg, mk_of_closure_sets } instance : complete_lattice (measurable_space α) := gi_generate_from.lift_complete_lattice instance : inhabited (measurable_space α) := ⟨⊤⟩ lemma is_measurable_bot_iff {s : set α} : @is_measurable α ⊥ s ↔ (s = ∅ ∨ s = univ) := let b : measurable_space α := { is_measurable := λs, s = ∅ ∨ s = univ, is_measurable_empty := or.inl rfl, is_measurable_compl := by simp [or_imp_distrib] {contextual := tt}, is_measurable_Union := assume f hf, classical.by_cases (assume h : ∃i, f i = univ, let ⟨i, hi⟩ := h in or.inr $ eq_univ_of_univ_subset $ hi ▸ le_supr f i) (assume h : ¬ ∃i, f i = univ, or.inl $ eq_empty_of_subset_empty $ Union_subset $ assume i, (hf i).elim (by simp {contextual := tt}) (assume hi, false.elim $ h ⟨i, hi⟩)) } in have b = ⊥, from bot_unique $ assume s hs, hs.elim (assume s, s.symm ▸ @is_measurable_empty _ ⊥) (assume s, s.symm ▸ @is_measurable.univ _ ⊥), this ▸ iff.refl _ @[simp] theorem is_measurable_top {s : set α} : @is_measurable _ ⊤ s := trivial @[simp] theorem is_measurable_inf {m₁ m₂ : measurable_space α} {s : set α} : @is_measurable _ (m₁ ⊓ m₂) s ↔ @is_measurable _ m₁ s ∧ @is_measurable _ m₂ s := iff.rfl @[simp] theorem is_measurable_Inf {ms : set (measurable_space α)} {s : set α} : @is_measurable _ (Inf ms) s ↔ ∀ m ∈ ms, @is_measurable _ m s := show s ∈ (⋂m∈ms, {t | @is_measurable _ m t }) ↔ _, by simp @[simp] theorem is_measurable_infi {ι} {m : ι → measurable_space α} {s : set α} : @is_measurable _ (infi m) s ↔ ∀ i, @is_measurable _ (m i) s := show s ∈ (λm, {s | @is_measurable _ m s }) (infi m) ↔ _, by rw (@gi_generate_from α).gc.u_infi; simp; refl end complete_lattice section functors variables {m m₁ m₂ : measurable_space α} {m' : measurable_space β} {f : α → β} {g : β → α} /-- The forward image of a measure space under a function. `map f m` contains the sets `s : set β` whose preimage under `f` is measurable. -/ protected def map (f : α → β) (m : measurable_space α) : measurable_space β := { is_measurable := λs, m.is_measurable $ f ⁻¹' s, is_measurable_empty := m.is_measurable_empty, is_measurable_compl := assume s hs, m.is_measurable_compl _ hs, is_measurable_Union := assume f hf, by rw [preimage_Union]; exact m.is_measurable_Union _ hf } @[simp] lemma map_id : m.map id = m := measurable_space.ext $ assume s, iff.rfl @[simp] lemma map_comp {f : α → β} {g : β → γ} : (m.map f).map g = m.map (g ∘ f) := measurable_space.ext $ assume s, iff.rfl /-- The reverse image of a measure space under a function. `comap f m` contains the sets `s : set α` such that `s` is the `f`-preimage of a measurable set in `β`. -/ protected def comap (f : α → β) (m : measurable_space β) : measurable_space α := { is_measurable := λs, ∃s', m.is_measurable s' ∧ f ⁻¹' s' = s, is_measurable_empty := ⟨∅, m.is_measurable_empty, rfl⟩, is_measurable_compl := assume s ⟨s', h₁, h₂⟩, ⟨-s', m.is_measurable_compl _ h₁, h₂ ▸ rfl⟩, is_measurable_Union := assume s hs, let ⟨s', hs'⟩ := classical.axiom_of_choice hs in ⟨⋃i, s' i, m.is_measurable_Union _ (λi, (hs' i).left), by simp [hs'] ⟩ } @[simp] lemma comap_id : m.comap id = m := measurable_space.ext $ assume s, ⟨assume ⟨s', hs', h⟩, h ▸ hs', assume h, ⟨s, h, rfl⟩⟩ @[simp] lemma comap_comp {f : β → α} {g : γ → β} : (m.comap f).comap g = m.comap (f ∘ g) := measurable_space.ext $ assume s, ⟨assume ⟨t, ⟨u, h, hu⟩, ht⟩, ⟨u, h, ht ▸ hu ▸ rfl⟩, assume ⟨t, h, ht⟩, ⟨f ⁻¹' t, ⟨_, h, rfl⟩, ht⟩⟩ lemma comap_le_iff_le_map {f : α → β} : m'.comap f ≤ m ↔ m' ≤ m.map f := ⟨assume h s hs, h _ ⟨_, hs, rfl⟩, assume h s ⟨t, ht, heq⟩, heq ▸ h _ ht⟩ lemma gc_comap_map (f : α → β) : galois_connection (measurable_space.comap f) (measurable_space.map f) := assume f g, comap_le_iff_le_map lemma map_mono (h : m₁ ≤ m₂) : m₁.map f ≤ m₂.map f := (gc_comap_map f).monotone_u h lemma monotone_map : monotone (measurable_space.map f) := assume a b h, map_mono h lemma comap_mono (h : m₁ ≤ m₂) : m₁.comap g ≤ m₂.comap g := (gc_comap_map g).monotone_l h lemma monotone_comap : monotone (measurable_space.comap g) := assume a b h, comap_mono h @[simp] lemma comap_bot : (⊥:measurable_space α).comap g = ⊥ := (gc_comap_map g).l_bot @[simp] lemma comap_sup : (m₁ ⊔ m₂).comap g = m₁.comap g ⊔ m₂.comap g := (gc_comap_map g).l_sup @[simp] lemma comap_supr {m : ι → measurable_space α} :(⨆i, m i).comap g = (⨆i, (m i).comap g) := (gc_comap_map g).l_supr @[simp] lemma map_top : (⊤:measurable_space α).map f = ⊤ := (gc_comap_map f).u_top @[simp] lemma map_inf : (m₁ ⊓ m₂).map f = m₁.map f ⊓ m₂.map f := (gc_comap_map f).u_inf @[simp] lemma map_infi {m : ι → measurable_space α} : (⨅i, m i).map f = (⨅i, (m i).map f) := (gc_comap_map f).u_infi lemma comap_map_le : (m.map f).comap f ≤ m := (gc_comap_map f).l_u_le _ lemma le_map_comap : m ≤ (m.comap g).map g := (gc_comap_map g).le_u_l _ end functors lemma generate_from_le_generate_from {s t : set (set α)} (h : s ⊆ t) : generate_from s ≤ generate_from t := gi_generate_from.gc.monotone_l h lemma generate_from_sup_generate_from {s t : set (set α)} : generate_from s ⊔ generate_from t = generate_from (s ∪ t) := (@gi_generate_from α).gc.l_sup.symm lemma comap_generate_from {f : α → β} {s : set (set β)} : (generate_from s).comap f = generate_from (preimage f '' s) := le_antisymm (comap_le_iff_le_map.2 $ generate_from_le $ assume t hts, generate_measurable.basic _ $ mem_image_of_mem _ $ hts) (generate_from_le $ assume t ⟨u, hu, eq⟩, eq ▸ ⟨u, generate_measurable.basic _ hu, rfl⟩) end measurable_space section measurable_functions open measurable_space /-- A function `f` between measurable spaces is measurable if the preimage of every measurable set is measurable. -/ def measurable [m₁ : measurable_space α] [m₂ : measurable_space β] (f : α → β) : Prop := m₂ ≤ m₁.map f lemma measurable_id [measurable_space α] : measurable (@id α) := le_refl _ lemma measurable.preimage [measurable_space α] [measurable_space β] {f : α → β} (hf : measurable f) {s : set β} : is_measurable s → is_measurable (f ⁻¹' s) := hf _ lemma measurable.comp [measurable_space α] [measurable_space β] [measurable_space γ] {g : β → γ} {f : α → β} (hg : measurable g) (hf : measurable f) : measurable (g ∘ f) := le_trans hg $ map_mono hf lemma measurable_generate_from [measurable_space α] {s : set (set β)} {f : α → β} (h : ∀t∈s, is_measurable (f ⁻¹' t)) : @measurable _ _ _ (generate_from s) f := generate_from_le h lemma measurable.if [measurable_space α] [measurable_space β] {p : α → Prop} {h : decidable_pred p} {f g : α → β} (hp : is_measurable {a | p a}) (hf : measurable f) (hg : measurable g) : measurable (λa, if p a then f a else g a) := λ s hs, show is_measurable {a | (if p a then f a else g a) ∈ s}, begin convert (hp.inter $ hf s hs).union (hp.compl.inter $ hg s hs), exact ext (λ a, by by_cases p a ; { rw mem_def, simp [h] }) end lemma measurable_const {α β} [measurable_space α] [measurable_space β] {a : α} : measurable (λb:β, a) := assume s hs, show is_measurable {b : β | a ∈ s}, from classical.by_cases (assume h : a ∈ s, by simp [h]; from is_measurable.univ) (assume h : a ∉ s, by simp [h]; from is_measurable.empty) end measurable_functions section constructions instance : measurable_space empty := ⊤ instance : measurable_space unit := ⊤ instance : measurable_space bool := ⊤ instance : measurable_space ℕ := ⊤ instance : measurable_space ℤ := ⊤ lemma measurable_unit [measurable_space α] (f : unit → α) : measurable f := have f = (λu, f ()) := funext $ assume ⟨⟩, rfl, by rw this; exact measurable_const section nat lemma measurable_from_nat [measurable_space α] {f : ℕ → α} : measurable f := assume s hs, show is_measurable {n : ℕ | f n ∈ s}, from trivial lemma measurable_to_nat [measurable_space α] {f : α → ℕ} : (∀ k, is_measurable {x | f x = k}) → measurable f := begin assume h s hs, show is_measurable {x | f x ∈ s}, have : {x | f x ∈ s} = ⋃ (n ∈ s), {x | f x = n}, { ext, simp }, rw this, simp [is_measurable.Union, is_measurable.Union_Prop, h] end lemma measurable_find_greatest [measurable_space α] {p : ℕ → α → Prop} : ∀ {N}, (∀ k ≤ N, is_measurable {x | nat.find_greatest (λ n, p n x) N = k}) → measurable (λ x, nat.find_greatest (λ n, p n x) N) | 0 := assume h s hs, show is_measurable {x : α | (nat.find_greatest (λ n, p n x) 0) ∈ s}, begin by_cases h : 0 ∈ s, { convert is_measurable.univ, simp only [nat.find_greatest_zero, h] }, { convert is_measurable.empty, simp only [nat.find_greatest_zero, h], refl } end | (n + 1) := assume h, begin apply measurable_to_nat, assume k, by_cases hk : k ≤ n + 1, { exact h k hk }, { have := is_measurable.empty, rw ← set_of_false at this, convert this, funext, rw eq_false, assume h, rw ← h at hk, have := nat.find_greatest_le, contradiction } end end nat section subtype instance {p : α → Prop} [m : measurable_space α] : measurable_space (subtype p) := m.comap subtype.val lemma measurable_subtype_val [measurable_space α] [measurable_space β] {p : β → Prop} {f : α → subtype p} (hf : measurable f) : measurable (λa:α, (f a).val) := measurable.comp (measurable_space.comap_le_iff_le_map.mp (le_refl _)) hf lemma measurable_subtype_mk [measurable_space α] [measurable_space β] {p : β → Prop} {f : α → subtype p} (hf : measurable (λa, (f a).val)) : measurable f := measurable_space.comap_le_iff_le_map.mpr $ by rw [measurable_space.map_comp]; exact hf lemma is_measurable_subtype_image [measurable_space α] {s : set α} {t : set s} (hs : is_measurable s) : is_measurable t → is_measurable ((coe : s → α) '' t) | ⟨u, (hu : is_measurable u), (eq : coe ⁻¹' u = t)⟩ := begin rw [← eq, image_preimage_eq_inter_range, range_coe_subtype], exact is_measurable.inter hu hs end lemma measurable_of_measurable_union_cover [measurable_space α] [measurable_space β] {f : α → β} (s t : set α) (hs : is_measurable s) (ht : is_measurable t) (h : univ ⊆ s ∪ t) (hc : measurable (λa:s, f a)) (hd : measurable (λa:t, f a)) : measurable f := assume u (hu : is_measurable u), show is_measurable (f ⁻¹' u), from begin rw show f ⁻¹' u = coe '' (coe ⁻¹' (f ⁻¹' u) : set s) ∪ coe '' (coe ⁻¹' (f ⁻¹' u) : set t), by rw [image_preimage_eq_inter_range, image_preimage_eq_inter_range, range_coe_subtype, range_coe_subtype, ← inter_distrib_left, univ_subset_iff.1 h, inter_univ], exact is_measurable.union (is_measurable_subtype_image hs (hc _ hu)) (is_measurable_subtype_image ht (hd _ hu)) end end subtype section prod instance [m₁ : measurable_space α] [m₂ : measurable_space β] : measurable_space (α × β) := m₁.comap prod.fst ⊔ m₂.comap prod.snd lemma measurable_fst [measurable_space α] [measurable_space β] [measurable_space γ] {f : α → β × γ} (hf : measurable f) : measurable (λa:α, (f a).1) := measurable.comp (measurable_space.comap_le_iff_le_map.mp le_sup_left) hf lemma measurable_snd [measurable_space α] [measurable_space β] [measurable_space γ] {f : α → β × γ} (hf : measurable f) : measurable (λa:α, (f a).2) := measurable.comp (measurable_space.comap_le_iff_le_map.mp le_sup_right) hf lemma measurable.prod [measurable_space α] [measurable_space β] [measurable_space γ] {f : α → β × γ} (hf₁ : measurable (λa, (f a).1)) (hf₂ : measurable (λa, (f a).2)) : measurable f := sup_le (by rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp]; exact hf₁) (by rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp]; exact hf₂) lemma measurable_prod_mk [measurable_space α] [measurable_space β] [measurable_space γ] {f : α → β} {g : α → γ} (hf : measurable f) (hg : measurable g) : measurable (λa:α, (f a, g a)) := measurable.prod hf hg lemma is_measurable_set_prod [measurable_space α] [measurable_space β] {s : set α} {t : set β} (hs : is_measurable s) (ht : is_measurable t) : is_measurable (set.prod s t) := is_measurable.inter (measurable_fst measurable_id _ hs) (measurable_snd measurable_id _ ht) end prod section pi instance measurable_space.pi {α : Type u} {β : α → Type v} [m : Πa, measurable_space (β a)] : measurable_space (Πa, β a) := ⨆a, (m a).comap (λb, b a) lemma measurable_pi_apply {α : Type u} {β : α → Type v} [Πa, measurable_space (β a)] (a : α) : measurable (λf:Πa, β a, f a) := measurable_space.comap_le_iff_le_map.1 $ lattice.le_supr _ a lemma measurable_pi_lambda {α : Type u} {β : α → Type v} {γ : Type w} [Πa, measurable_space (β a)] [measurable_space γ] (f : γ → Πa, β a) (hf : ∀a, measurable (λc, f c a)) : measurable f := lattice.supr_le $ assume a, measurable_space.comap_le_iff_le_map.2 (hf a) end pi instance [m₁ : measurable_space α] [m₂ : measurable_space β] : measurable_space (α ⊕ β) := m₁.map sum.inl ⊓ m₂.map sum.inr section sum variables [measurable_space α] [measurable_space β] [measurable_space γ] lemma measurable_inl : measurable (@sum.inl α β) := inf_le_left lemma measurable_inr : measurable (@sum.inr α β) := inf_le_right lemma measurable_sum {f : α ⊕ β → γ} (hl : measurable (f ∘ sum.inl)) (hr : measurable (f ∘ sum.inr)) : measurable f := measurable_space.comap_le_iff_le_map.1 $ le_inf (measurable_space.comap_le_iff_le_map.2 $ hl) (measurable_space.comap_le_iff_le_map.2 $ hr) lemma measurable_sum_rec {f : α → γ} {g : β → γ} (hf : measurable f) (hg : measurable g) : @measurable (α ⊕ β) γ _ _ (@sum.rec α β (λ_, γ) f g) := measurable_sum hf hg lemma is_measurable_inl_image {s : set α} (hs : is_measurable s) : is_measurable (sum.inl '' s : set (α ⊕ β)) := ⟨show is_measurable (sum.inl ⁻¹' _), by rwa [preimage_image_eq]; exact (assume a b, sum.inl.inj), have sum.inr ⁻¹' (sum.inl '' s : set (α ⊕ β)) = ∅ := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show is_measurable (sum.inr ⁻¹' _), by rw [this]; exact is_measurable.empty⟩ lemma is_measurable_range_inl : is_measurable (range sum.inl : set (α ⊕ β)) := by rw [← image_univ]; exact is_measurable_inl_image is_measurable.univ lemma is_measurable_inr_image {s : set β} (hs : is_measurable s) : is_measurable (sum.inr '' s : set (α ⊕ β)) := ⟨ have sum.inl ⁻¹' (sum.inr '' s : set (α ⊕ β)) = ∅ := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show is_measurable (sum.inl ⁻¹' _), by rw [this]; exact is_measurable.empty, show is_measurable (sum.inr ⁻¹' _), by rwa [preimage_image_eq]; exact (assume a b, sum.inr.inj)⟩ lemma is_measurable_range_inr : is_measurable (range sum.inr : set (α ⊕ β)) := by rw [← image_univ]; exact is_measurable_inr_image is_measurable.univ end sum instance {β : α → Type v} [m : Πa, measurable_space (β a)] : measurable_space (sigma β) := ⨅a, (m a).map (sigma.mk a) end constructions /-- Equivalences between measurable spaces. Main application is the simplification of measurability statements along measurable equivalences. -/ structure measurable_equiv (α β : Type*) [measurable_space α] [measurable_space β] extends α ≃ β := (measurable_to_fun : measurable to_fun) (measurable_inv_fun : measurable inv_fun) namespace measurable_equiv instance (α β) [measurable_space α] [measurable_space β] : has_coe_to_fun (measurable_equiv α β) := ⟨λ_, α → β, λe, e.to_equiv⟩ lemma coe_eq {α β} [measurable_space α] [measurable_space β] (e : measurable_equiv α β) : (e : α → β) = e.to_equiv := rfl def refl (α : Type*) [measurable_space α] : measurable_equiv α α := { to_equiv := equiv.refl α, measurable_to_fun := measurable_id, measurable_inv_fun := measurable_id } def trans [measurable_space α] [measurable_space β] [measurable_space γ] (ab : measurable_equiv α β) (bc : measurable_equiv β γ) : measurable_equiv α γ := { to_equiv := ab.to_equiv.trans bc.to_equiv, measurable_to_fun := bc.measurable_to_fun.comp ab.measurable_to_fun, measurable_inv_fun := ab.measurable_inv_fun.comp bc.measurable_inv_fun } lemma trans_to_equiv {α β} [measurable_space α] [measurable_space β] [measurable_space γ] (e : measurable_equiv α β) (f : measurable_equiv β γ) : (e.trans f).to_equiv = e.to_equiv.trans f.to_equiv := rfl def symm [measurable_space α] [measurable_space β] (ab : measurable_equiv α β) : measurable_equiv β α := { to_equiv := ab.to_equiv.symm, measurable_to_fun := ab.measurable_inv_fun, measurable_inv_fun := ab.measurable_to_fun } lemma symm_to_equiv {α β} [measurable_space α] [measurable_space β] (e : measurable_equiv α β) : e.symm.to_equiv = e.to_equiv.symm := rfl protected def cast {α β} [i₁ : measurable_space α] [i₂ : measurable_space β] (h : α = β) (hi : i₁ == i₂) : measurable_equiv α β := { to_equiv := equiv.cast h, measurable_to_fun := by unfreezeI; subst h; subst hi; exact measurable_id, measurable_inv_fun := by unfreezeI; subst h; subst hi; exact measurable_id } protected lemma measurable {α β} [measurable_space α] [measurable_space β] (e : measurable_equiv α β) : measurable (e : α → β) := e.measurable_to_fun protected lemma measurable_coe_iff {α β γ} [measurable_space α] [measurable_space β] [measurable_space γ] {f : β → γ} (e : measurable_equiv α β) : measurable (f ∘ e) ↔ measurable f := iff.intro (assume hfe, have measurable (f ∘ (e.symm.trans e).to_equiv) := hfe.comp e.symm.measurable, by rwa [trans_to_equiv, symm_to_equiv, equiv.symm_trans] at this) (λh, h.comp e.measurable) def prod_congr [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] (ab : measurable_equiv α β) (cd : measurable_equiv γ δ) : measurable_equiv (α × γ) (β × δ) := { to_equiv := equiv.prod_congr ab.to_equiv cd.to_equiv, measurable_to_fun := measurable_prod_mk (ab.measurable_to_fun.comp (measurable_fst measurable_id)) (cd.measurable_to_fun.comp (measurable_snd measurable_id)), measurable_inv_fun := measurable_prod_mk (ab.measurable_inv_fun.comp (measurable_fst measurable_id)) (cd.measurable_inv_fun.comp (measurable_snd measurable_id)) } def prod_comm [measurable_space α] [measurable_space β] : measurable_equiv (α × β) (β × α) := { to_equiv := equiv.prod_comm α β, measurable_to_fun := measurable_prod_mk (measurable_snd measurable_id) (measurable_fst measurable_id), measurable_inv_fun := measurable_prod_mk (measurable_snd measurable_id) (measurable_fst measurable_id) } def sum_congr [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] (ab : measurable_equiv α β) (cd : measurable_equiv γ δ) : measurable_equiv (α ⊕ γ) (β ⊕ δ) := { to_equiv := equiv.sum_congr ab.to_equiv cd.to_equiv, measurable_to_fun := begin cases ab with ab' abm, cases ab', cases cd with cd' cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end, measurable_inv_fun := begin cases ab with ab' _ abm, cases ab', cases cd with cd' _ cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end } def set.prod [measurable_space α] [measurable_space β] (s : set α) (t : set β) : measurable_equiv (set.prod s t) (s × t) := { to_equiv := equiv.set.prod s t, measurable_to_fun := measurable_prod_mk (measurable_subtype_mk $ measurable_fst $ measurable_subtype_val $ measurable_id) (measurable_subtype_mk $ measurable_snd $ measurable_subtype_val $ measurable_id), measurable_inv_fun := measurable_subtype_mk $ measurable_prod_mk (measurable_subtype_val $ measurable_fst $ measurable_id) (measurable_subtype_val $ measurable_snd $ measurable_id) } def set.univ (α : Type*) [measurable_space α] : measurable_equiv (univ : set α) α := { to_equiv := equiv.set.univ α, measurable_to_fun := measurable_subtype_val measurable_id, measurable_inv_fun := measurable_subtype_mk measurable_id } def set.singleton [measurable_space α] (a:α) : measurable_equiv ({a} : set α) unit := { to_equiv := equiv.set.singleton a, measurable_to_fun := measurable_const, measurable_inv_fun := measurable_subtype_mk $ show measurable (λu:unit, a), from measurable_const } noncomputable def set.image [measurable_space α] [measurable_space β] (f : α → β) (s : set α) (hf : function.injective f) (hfm : measurable f) (hfi : ∀s, is_measurable s → is_measurable (f '' s)) : measurable_equiv s (f '' s) := { to_equiv := equiv.set.image f s hf, measurable_to_fun := begin have : measurable (λa:s, f a) := hfm.comp (measurable_subtype_val measurable_id), refine measurable_subtype_mk _, convert this, ext ⟨a, h⟩, refl end, measurable_inv_fun := assume t ⟨u, (hu : is_measurable u), eq⟩, begin clear_, subst eq, show is_measurable {x : f '' s | ((equiv.set.image f s hf).inv_fun x).val ∈ u}, have : ∀(a ∈ s) (h : ∃a', a' ∈ s ∧ a' = a), classical.some h = a := λa ha h, (classical.some_spec h).2, rw show {x:f '' s | ((equiv.set.image f s hf).inv_fun x).val ∈ u} = subtype.val ⁻¹' (f '' u), by ext ⟨b, a, hbs, rfl⟩; simp [equiv.set.image, equiv.set.image_of_inj_on, hf, this _ hbs], exact (measurable_subtype_val measurable_id) (f '' u) (hfi u hu) end } noncomputable def set.range [measurable_space α] [measurable_space β] (f : α → β) (hf : function.injective f) (hfm : measurable f) (hfi : ∀s, is_measurable s → is_measurable (f '' s)) : measurable_equiv α (range f) := (measurable_equiv.set.univ _).symm.trans $ (measurable_equiv.set.image f univ hf hfm hfi).trans $ measurable_equiv.cast (by rw image_univ) (by rw image_univ) def set.range_inl [measurable_space α] [measurable_space β] : measurable_equiv (range sum.inl : set (α ⊕ β)) α := { to_fun := λab, match ab with | ⟨sum.inl a, _⟩ := a | ⟨sum.inr b, p⟩ := have false, by cases p; contradiction, this.elim end, inv_fun := λa, ⟨sum.inl a, a, rfl⟩, left_inv := assume ⟨ab, a, eq⟩, by subst eq; refl, right_inv := assume a, rfl, measurable_to_fun := assume s (hs : is_measurable s), begin refine ⟨_, is_measurable_inl_image hs, set.ext _⟩, rintros ⟨ab, a, rfl⟩, simp [set.range_inl._match_1] end, measurable_inv_fun := measurable_subtype_mk measurable_inl } def set.range_inr [measurable_space α] [measurable_space β] : measurable_equiv (range sum.inr : set (α ⊕ β)) β := { to_fun := λab, match ab with | ⟨sum.inr b, _⟩ := b | ⟨sum.inl a, p⟩ := have false, by cases p; contradiction, this.elim end, inv_fun := λb, ⟨sum.inr b, b, rfl⟩, left_inv := assume ⟨ab, b, eq⟩, by subst eq; refl, right_inv := assume b, rfl, measurable_to_fun := assume s (hs : is_measurable s), begin refine ⟨_, is_measurable_inr_image hs, set.ext _⟩, rintros ⟨ab, b, rfl⟩, simp [set.range_inr._match_1] end, measurable_inv_fun := measurable_subtype_mk measurable_inr } def sum_prod_distrib (α β γ) [measurable_space α] [measurable_space β] [measurable_space γ] : measurable_equiv ((α ⊕ β) × γ) ((α × γ) ⊕ (β × γ)) := { to_equiv := equiv.sum_prod_distrib α β γ, measurable_to_fun := begin refine measurable_of_measurable_union_cover ((range sum.inl).prod univ) ((range sum.inr).prod univ) (is_measurable_set_prod is_measurable_range_inl is_measurable.univ) (is_measurable_set_prod is_measurable_range_inr is_measurable.univ) (assume ⟨ab, c⟩ s, by cases ab; simp [set.prod_eq]) _ _, { refine (set.prod (range sum.inl) univ).symm.measurable_coe_iff.1 _, refine (prod_congr set.range_inl (set.univ _)).symm.measurable_coe_iff.1 _, dsimp [(∘)], convert measurable_inl, ext ⟨a, c⟩, refl }, { refine (set.prod (range sum.inr) univ).symm.measurable_coe_iff.1 _, refine (prod_congr set.range_inr (set.univ _)).symm.measurable_coe_iff.1 _, dsimp [(∘)], convert measurable_inr, ext ⟨b, c⟩, refl } end, measurable_inv_fun := begin refine measurable_sum _ _, { convert measurable_prod_mk (measurable_inl.comp (measurable_fst measurable_id)) (measurable_snd measurable_id), ext ⟨a, c⟩; refl }, { convert measurable_prod_mk (measurable_inr.comp (measurable_fst measurable_id)) (measurable_snd measurable_id), ext ⟨b, c⟩; refl } end } def prod_sum_distrib (α β γ) [measurable_space α] [measurable_space β] [measurable_space γ] : measurable_equiv (α × (β ⊕ γ)) ((α × β) ⊕ (α × γ)) := prod_comm.trans $ (sum_prod_distrib _ _ _).trans $ sum_congr prod_comm prod_comm def sum_prod_sum (α β γ δ) [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] : measurable_equiv ((α ⊕ β) × (γ ⊕ δ)) (((α × γ) ⊕ (α × δ)) ⊕ ((β × γ) ⊕ (β × δ))) := (sum_prod_distrib _ _ _).trans $ sum_congr (prod_sum_distrib _ _ _) (prod_sum_distrib _ _ _) end measurable_equiv namespace measurable_equiv end measurable_equiv namespace measurable_space /-- Dynkin systems The main purpose of Dynkin systems is to provide a powerful induction rule for σ-algebras generated by intersection stable set systems. -/ structure dynkin_system (α : Type*) := (has : set α → Prop) (has_empty : has ∅) (has_compl : ∀{a}, has a → has (-a)) (has_Union_nat : ∀{f:ℕ → set α}, pairwise (disjoint on f) → (∀i, has (f i)) → has (⋃i, f i)) theorem Union_decode2_disjoint_on {β} [encodable β] {f : β → set α} (hd : pairwise (disjoint on f)) : pairwise (disjoint on λ i, ⋃ b ∈ decode2 β i, f b) := begin rintro i j ij x ⟨h₁, h₂⟩, revert h₁ h₂, simp, intros b₁ e₁ h₁ b₂ e₂ h₂, refine hd _ _ _ ⟨h₁, h₂⟩, cases encodable.mem_decode2.1 e₁, cases encodable.mem_decode2.1 e₂, exact mt (congr_arg _) ij end namespace dynkin_system @[extensionality] lemma ext : ∀{d₁ d₂ : dynkin_system α}, (∀s:set α, d₁.has s ↔ d₂.has s) → d₁ = d₂ | ⟨s₁, _, _, _⟩ ⟨s₂, _, _, _⟩ h := have s₁ = s₂, from funext $ assume x, propext $ h x, by subst this variable (d : dynkin_system α) lemma has_compl_iff {a} : d.has (-a) ↔ d.has a := ⟨λ h, by simpa using d.has_compl h, λ h, d.has_compl h⟩ lemma has_univ : d.has univ := by simpa using d.has_compl d.has_empty theorem has_Union {β} [encodable β] {f : β → set α} (hd : pairwise (disjoint on f)) (h : ∀i, d.has (f i)) : d.has (⋃i, f i) := by rw encodable.Union_decode2; exact d.has_Union_nat (Union_decode2_disjoint_on hd) (λ n, encodable.Union_decode2_cases d.has_empty h) theorem has_union {s₁ s₂ : set α} (h₁ : d.has s₁) (h₂ : d.has s₂) (h : s₁ ∩ s₂ ⊆ ∅) : d.has (s₁ ∪ s₂) := by rw union_eq_Union; exact d.has_Union (pairwise_disjoint_on_bool.2 h) (bool.forall_bool.2 ⟨h₂, h₁⟩) lemma has_diff {s₁ s₂ : set α} (h₁ : d.has s₁) (h₂ : d.has s₂) (h : s₂ ⊆ s₁) : d.has (s₁ \ s₂) := d.has_compl_iff.1 begin simp [diff_eq, compl_inter], exact d.has_union (d.has_compl h₁) h₂ (λ x ⟨h₁, h₂⟩, h₁ (h h₂)), end instance : partial_order (dynkin_system α) := { le := λm₁ m₂, m₁.has ≤ m₂.has, le_refl := assume a b, le_refl _, le_trans := assume a b c, le_trans, le_antisymm := assume a b h₁ h₂, ext $ assume s, ⟨h₁ s, h₂ s⟩ } def of_measurable_space (m : measurable_space α) : dynkin_system α := { has := m.is_measurable, has_empty := m.is_measurable_empty, has_compl := m.is_measurable_compl, has_Union_nat := assume f _ hf, m.is_measurable_Union f hf } lemma of_measurable_space_le_of_measurable_space_iff {m₁ m₂ : measurable_space α} : of_measurable_space m₁ ≤ of_measurable_space m₂ ↔ m₁ ≤ m₂ := iff.rfl /-- The least Dynkin system containing a collection of basic sets. -/ inductive generate_has (s : set (set α)) : set α → Prop | basic : ∀t∈s, generate_has t | empty : generate_has ∅ | compl : ∀{a}, generate_has a → generate_has (-a) | Union : ∀{f:ℕ → set α}, pairwise (disjoint on f) → (∀i, generate_has (f i)) → generate_has (⋃i, f i) def generate (s : set (set α)) : dynkin_system α := { has := generate_has s, has_empty := generate_has.empty s, has_compl := assume a, generate_has.compl, has_Union_nat := assume f, generate_has.Union } def to_measurable_space (h_inter : ∀s₁ s₂, d.has s₁ → d.has s₂ → d.has (s₁ ∩ s₂)) := { measurable_space . is_measurable := d.has, is_measurable_empty := d.has_empty, is_measurable_compl := assume s h, d.has_compl h, is_measurable_Union := assume f hf, have ∀n, d.has (disjointed f n), from assume n, disjointed_induct (hf n) (assume t i h, h_inter _ _ h $ d.has_compl $ hf i), have d.has (⋃n, disjointed f n), from d.has_Union disjoint_disjointed this, by rwa [Union_disjointed] at this } lemma of_measurable_space_to_measurable_space (h_inter : ∀s₁ s₂, d.has s₁ → d.has s₂ → d.has (s₁ ∩ s₂)) : of_measurable_space (d.to_measurable_space h_inter) = d := ext $ assume s, iff.rfl def restrict_on {s : set α} (h : d.has s) : dynkin_system α := { has := λt, d.has (t ∩ s), has_empty := by simp [d.has_empty], has_compl := assume t hts, have -t ∩ s = (- (t ∩ s)) \ -s, from set.ext $ assume x, by by_cases x ∈ s; simp [h], by rw [this]; from d.has_diff (d.has_compl hts) (d.has_compl h) (compl_subset_compl.mpr $ inter_subset_right _ _), has_Union_nat := assume f hd hf, begin rw [inter_comm, inter_Union], apply d.has_Union_nat, { exact λ i j h x ⟨⟨_, h₁⟩, _, h₂⟩, hd i j h ⟨h₁, h₂⟩ }, { simpa [inter_comm] using hf }, end } lemma generate_le {s : set (set α)} (h : ∀t∈s, d.has t) : generate s ≤ d := λ t ht, ht.rec_on h d.has_empty (assume a _ h, d.has_compl h) (assume f hd _ hf, d.has_Union hd hf) lemma generate_inter {s : set (set α)} (hs : ∀t₁ t₂, t₁ ∈ s → t₂ ∈ s → t₁ ∩ t₂ ≠ ∅ → t₁ ∩ t₂ ∈ s) {t₁ t₂ : set α} (ht₁ : (generate s).has t₁) (ht₂ : (generate s).has t₂) : (generate s).has (t₁ ∩ t₂) := have generate s ≤ (generate s).restrict_on ht₂, from generate_le _ $ assume s₁ hs₁, have (generate s).has s₁, from generate_has.basic s₁ hs₁, have generate s ≤ (generate s).restrict_on this, from generate_le _ $ assume s₂ hs₂, show (generate s).has (s₂ ∩ s₁), from if h : s₂ ∩ s₁ = ∅ then by rw [h]; exact generate_has.empty _ else generate_has.basic _ (hs _ _ hs₂ hs₁ h), have (generate s).has (t₂ ∩ s₁), from this _ ht₂, show (generate s).has (s₁ ∩ t₂), by rwa [inter_comm], this _ ht₁ lemma generate_from_eq {s : set (set α)} (hs : ∀t₁ t₂, t₁ ∈ s → t₂ ∈ s → t₁ ∩ t₂ ≠ ∅ → t₁ ∩ t₂ ∈ s) : generate_from s = (generate s).to_measurable_space (assume t₁ t₂, generate_inter hs) := le_antisymm (generate_from_le $ assume t ht, generate_has.basic t ht) (of_measurable_space_le_of_measurable_space_iff.mp $ by rw [of_measurable_space_to_measurable_space]; from (generate_le _ $ assume t ht, is_measurable_generate_from ht)) end dynkin_system lemma induction_on_inter {C : set α → Prop} {s : set (set α)} {m : measurable_space α} (h_eq : m = generate_from s) (h_inter : ∀t₁ t₂, t₁ ∈ s → t₂ ∈ s → t₁ ∩ t₂ ≠ ∅ → t₁ ∩ t₂ ∈ s) (h_empty : C ∅) (h_basic : ∀t∈s, C t) (h_compl : ∀t, m.is_measurable t → C t → C (- t)) (h_union : ∀f:ℕ → set α, (∀i j, i ≠ j → f i ∩ f j ⊆ ∅) → (∀i, m.is_measurable (f i)) → (∀i, C (f i)) → C (⋃i, f i)) : ∀{t}, m.is_measurable t → C t := have eq : m.is_measurable = dynkin_system.generate_has s, by rw [h_eq, dynkin_system.generate_from_eq h_inter]; refl, assume t ht, have dynkin_system.generate_has s t, by rwa [eq] at ht, this.rec_on h_basic h_empty (assume t ht, h_compl t $ by rw [eq]; exact ht) (assume f hf ht, h_union f hf $ assume i, by rw [eq]; exact ht _) end measurable_space
233b9ce75ef2da9ad5bc14c28ec1556ac73c9896
b815abf92ce063fe0d1fabf5b42da483552aa3e8
/library/init/algebra/ordered_ring.lean
42beca9c0df3a6a944db85b381e0111ebc28d25f
[ "Apache-2.0" ]
permissive
yodalee/lean
a368d842df12c63e9f79414ed7bbee805b9001ef
317989bf9ef6ae1dec7488c2363dbfcdc16e0756
refs/heads/master
1,610,551,176,860
1,481,430,138,000
1,481,646,441,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,705
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ prelude import init.algebra.ordered_group init.algebra.ring /- Make sure instances defined in this file have lower priority than the ones defined for concrete structures -/ set_option default_priority 100 universe variable u structure ordered_semiring (α : Type u) extends semiring α, ordered_mul_cancel_comm_monoid α renaming mul→add mul_assoc→add_assoc one→zero one_mul→zero_add mul_one→add_zero mul_comm→add_comm mul_left_cancel→add_left_cancel mul_right_cancel→add_right_cancel mul_le_mul_left→add_le_add_left mul_lt_mul_left→add_lt_add_left le_of_mul_le_mul_left→le_of_add_le_add_left lt_of_mul_lt_mul_left→lt_of_add_lt_add_left := (mul_le_mul_of_nonneg_left: ∀ a b c : α, a ≤ b → 0 ≤ c → c * a ≤ c * b) (mul_le_mul_of_nonneg_right: ∀ a b c : α, a ≤ b → 0 ≤ c → a * c ≤ b * c) (mul_lt_mul_of_pos_left: ∀ a b c : α, a < b → 0 < c → c * a < c * b) (mul_lt_mul_of_pos_right: ∀ a b c : α, a < b → 0 < c → a * c < b * c) /- we make it a class now (and not as part of the structure) to avoid ordered_semiring.to_ordered_mul_cancel_comm_monoid to be an instance -/ attribute [class] ordered_semiring variable {α : Type u} instance add_comm_group_of_ordered_semiring (α : Type u) [s : ordered_semiring α] : semiring α := @ordered_semiring.to_semiring α s instance monoid_of_ordered_semiring (α : Type u) [s : ordered_semiring α] : ordered_cancel_comm_monoid α := @ordered_semiring.to_ordered_mul_cancel_comm_monoid α s section variable [ordered_semiring α] lemma mul_le_mul_of_nonneg_left {a b c : α} (h₁ : a ≤ b) (h₂ : 0 ≤ c) : c * a ≤ c * b := ordered_semiring.mul_le_mul_of_nonneg_left _ a b c h₁ h₂ lemma mul_le_mul_of_nonneg_right {a b c : α} (h₁ : a ≤ b) (h₂ : 0 ≤ c) : a * c ≤ b * c := ordered_semiring.mul_le_mul_of_nonneg_right _ a b c h₁ h₂ lemma mul_lt_mul_of_pos_left {a b c : α} (h₁ : a < b) (h₂ : 0 < c) : c * a < c * b := ordered_semiring.mul_lt_mul_of_pos_left _ a b c h₁ h₂ lemma mul_lt_mul_of_pos_right {a b c : α} (h₁ : a < b) (h₂ : 0 < c) : a * c < b * c := ordered_semiring.mul_lt_mul_of_pos_right _ a b c h₁ h₂ end class linear_ordered_semiring (α : Type u) extends ordered_semiring α, linear_strong_order_pair α := (zero_lt_one : zero < one) lemma zero_lt_one [linear_ordered_semiring α] : 0 < (1:α) := linear_ordered_semiring.zero_lt_one α class decidable_linear_ordered_semiring (α : Type u) extends linear_ordered_semiring α, decidable_linear_order α structure ordered_ring (α : Type u) extends ring α, ordered_mul_comm_group α renaming mul→add mul_assoc→add_assoc one→zero one_mul→zero_add mul_one→add_zero inv→neg mul_left_inv→add_left_inv mul_comm→add_comm mul_le_mul_left→add_le_add_left mul_lt_mul_left→add_lt_add_left, zero_ne_one_class α := (mul_nonneg : ∀ a b : α, 0 ≤ a → 0 ≤ b → 0 ≤ a * b) (mul_pos : ∀ a b : α, 0 < a → 0 < b → 0 < a * b) /- we make it a class now (and not as part of the structure) to avoid ordered_ring.to_ordered_mul_comm_group to be an instance -/ attribute [class] ordered_ring instance add_comm_group_of_ordered_ring (α : Type u) [s : ordered_ring α] : ring α := @ordered_ring.to_ring α s instance monoid_of_ordered_ring (α : Type u) [s : ordered_ring α] : ordered_comm_group α := @ordered_ring.to_ordered_mul_comm_group α s instance zero_ne_one_class_of_ordered_ring (α : Type u) [s : ordered_ring α] : zero_ne_one_class α := @ordered_ring.to_zero_ne_one_class α s lemma ordered_ring.mul_le_mul_of_nonneg_left [s : ordered_ring α] {a b c : α} (h₁ : a ≤ b) (h₂ : 0 ≤ c) : c * a ≤ c * b := have 0 ≤ b - a, from sub_nonneg_of_le h₁, have 0 ≤ c * (b - a), from ordered_ring.mul_nonneg s c (b - a) h₂ this, begin rw mul_sub_left_distrib at this, apply le_of_sub_nonneg this end lemma ordered_ring.mul_le_mul_of_nonneg_right [s : ordered_ring α] {a b c : α} (h₁ : a ≤ b) (h₂ : 0 ≤ c) : a * c ≤ b * c := have 0 ≤ b - a, from sub_nonneg_of_le h₁, have 0 ≤ (b - a) * c, from ordered_ring.mul_nonneg s (b - a) c this h₂, begin rw mul_sub_right_distrib at this, apply le_of_sub_nonneg this end lemma ordered_ring.mul_lt_mul_of_pos_left [s : ordered_ring α] {a b c : α} (h₁ : a < b) (h₂ : 0 < c) : c * a < c * b := have 0 < b - a, from sub_pos_of_lt h₁, have 0 < c * (b - a), from ordered_ring.mul_pos s c (b - a) h₂ this, begin rw mul_sub_left_distrib at this, apply lt_of_sub_pos this end lemma ordered_ring.mul_lt_mul_of_pos_right [s : ordered_ring α] {a b c : α} (h₁ : a < b) (h₂ : 0 < c) : a * c < b * c := have 0 < b - a, from sub_pos_of_lt h₁, have 0 < (b - a) * c, from ordered_ring.mul_pos s (b - a) c this h₂, begin rw mul_sub_right_distrib at this, apply lt_of_sub_pos this end instance ordered_ring.to_ordered_semiring [s : ordered_ring α] : ordered_semiring α := { s with mul_zero := mul_zero, zero_mul := zero_mul, add_left_cancel := @add_left_cancel α _, add_right_cancel := @add_right_cancel α _, le_of_add_le_add_left := @le_of_add_le_add_left α _, mul_le_mul_of_nonneg_left := @ordered_ring.mul_le_mul_of_nonneg_left α _, mul_le_mul_of_nonneg_right := @ordered_ring.mul_le_mul_of_nonneg_right α _, mul_lt_mul_of_pos_left := @ordered_ring.mul_lt_mul_of_pos_left α _, mul_lt_mul_of_pos_right := @ordered_ring.mul_lt_mul_of_pos_right α _, lt_of_add_lt_add_left := @lt_of_add_lt_add_left α _} class linear_ordered_ring (α : Type u) extends ordered_ring α, linear_strong_order_pair α := (zero_lt_one : lt zero one) instance linear_ordered_ring.to_linear_ordered_semiring [s : linear_ordered_ring α] : linear_ordered_semiring α := { s with mul_zero := mul_zero, zero_mul := zero_mul, add_left_cancel := @add_left_cancel α _, add_right_cancel := @add_right_cancel α _, le_of_add_le_add_left := @le_of_add_le_add_left α _, mul_le_mul_of_nonneg_left := @mul_le_mul_of_nonneg_left α _, mul_le_mul_of_nonneg_right := @mul_le_mul_of_nonneg_right α _, mul_lt_mul_of_pos_left := @mul_lt_mul_of_pos_left α _, mul_lt_mul_of_pos_right := @mul_lt_mul_of_pos_right α _, le_total := linear_ordered_ring.le_total, lt_of_add_lt_add_left := @lt_of_add_lt_add_left α _ }
9ada85f1715912d7b454004277a00c60b6461beb
d642a6b1261b2cbe691e53561ac777b924751b63
/src/category_theory/endomorphism.lean
d505bbdad6622a635db87b91d26043912bcb8330
[ "Apache-2.0" ]
permissive
cipher1024/mathlib
fee56b9954e969721715e45fea8bcb95f9dc03fe
d077887141000fefa5a264e30fa57520e9f03522
refs/heads/master
1,651,806,490,504
1,573,508,694,000
1,573,508,694,000
107,216,176
0
0
Apache-2.0
1,647,363,136,000
1,508,213,014,000
Lean
UTF-8
Lean
false
false
2,805
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Scott Morrison, Simon Hudon Definition and basic properties of endomorphisms and automorphisms of an object in a category. -/ import category_theory.category category_theory.isomorphism category_theory.groupoid category_theory.functor import algebra.group.units data.equiv.algebra universes v v' u u' namespace category_theory /-- Endomorphisms of an object in a category. Arguments order in multiplication agrees with `function.comp`, not with `category.comp`. -/ def End {C : Type u} [𝒞_struct : category_struct.{v} C] (X : C) := X ⟶ X namespace End section struct variables {C : Type u} [𝒞_struct : category_struct.{v} C] (X : C) include 𝒞_struct instance has_one : has_one (End X) := ⟨𝟙 X⟩ /-- Multiplication of endomorphisms agrees with `function.comp`, not `category_struct.comp`. -/ instance has_mul : has_mul (End X) := ⟨λ x y, y ≫ x⟩ variable {X} @[simp] lemma one_def : (1 : End X) = 𝟙 X := rfl @[simp] lemma mul_def (xs ys : End X) : xs * ys = ys ≫ xs := rfl end struct /-- Endomorphisms of an object form a monoid -/ instance monoid {C : Type u} [category.{v} C] {X : C} : monoid (End X) := { mul_one := category.id_comp C, one_mul := category.comp_id C, mul_assoc := λ x y z, (category.assoc C z y x).symm, ..End.has_mul X, ..End.has_one X } /-- In a groupoid, endomorphisms form a group -/ instance group {C : Type u} [groupoid.{v} C] (X : C) : group (End X) := { mul_left_inv := groupoid.comp_inv C, inv := groupoid.inv, ..End.monoid } end End variables {C : Type u} [𝒞 : category.{v} C] (X : C) include 𝒞 def Aut (X : C) := X ≅ X attribute [ext Aut] iso.ext namespace Aut instance: group (Aut X) := by refine { one := iso.refl X, inv := iso.symm, mul := flip iso.trans, .. } ; dunfold flip; obviously def units_End_eqv_Aut : units (End X) ≃* Aut X := { to_fun := λ f, ⟨f.1, f.2, f.4, f.3⟩, inv_fun := λ f, ⟨f.1, f.2, f.4, f.3⟩, left_inv := λ ⟨f₁, f₂, f₃, f₄⟩, rfl, right_inv := λ ⟨f₁, f₂, f₃, f₄⟩, rfl, map_mul' := λ f g, by rcases f; rcases g; refl } end Aut namespace functor variables {D : Type u'} [𝒟 : category.{v'} D] (f : C ⥤ D) (X) include 𝒟 /-- `f.map` as a monoid hom between endomorphism monoids. -/ def map_End : End X →* End (f.obj X) := { to_fun := functor.map f, map_mul' := λ x y, f.map_comp y x, map_one' := f.map_id X } /-- `f.map_iso` as a group hom between automorphism groups. -/ def map_Aut : Aut X →* Aut (f.obj X) := { to_fun := f.map_iso, map_mul' := λ x y, f.map_iso_trans y x, map_one' := f.map_iso_refl X } end functor end category_theory
fa99661a34d663f3a6892f0e5d022e32fcfd875d
a7dd8b83f933e72c40845fd168dde330f050b1c9
/src/data/nat/enat.lean
32c0c7150826c7eb25bf9485ff121c12cadc8aff
[ "Apache-2.0" ]
permissive
NeilStrickland/mathlib
10420e92ee5cb7aba1163c9a01dea2f04652ed67
3efbd6f6dff0fb9b0946849b43b39948560a1ffe
refs/heads/master
1,589,043,046,346
1,558,938,706,000
1,558,938,706,000
181,285,984
0
0
Apache-2.0
1,568,941,848,000
1,555,233,833,000
Lean
UTF-8
Lean
false
false
7,886
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes Natural numbers with infinity, represented as roption ℕ. -/ import data.pfun algebra.ordered_group import tactic.norm_cast open roption lattice def enat : Type := roption ℕ namespace enat instance : has_zero enat := ⟨some 0⟩ instance : has_one enat := ⟨some 1⟩ instance : has_add enat := ⟨λ x y, ⟨x.dom ∧ y.dom, λ h, get x h.1 + get y h.2⟩⟩ instance : has_coe ℕ enat := ⟨some⟩ instance (n : ℕ) : decidable (n : enat).dom := is_true trivial @[simp] lemma coe_inj {x y : ℕ} : (x : enat) = y ↔ x = y := roption.some_inj instance : add_comm_monoid enat := { add := (+), zero := (0), add_comm := λ x y, roption.ext' and.comm (λ _ _, add_comm _ _), zero_add := λ x, roption.ext' (true_and _) (λ _ _, zero_add _), add_zero := λ x, roption.ext' (and_true _) (λ _ _, add_zero _), add_assoc := λ x y z, roption.ext' and.assoc (λ _ _, add_assoc _ _ _) } instance : has_le enat := ⟨λ x y, ∃ h : y.dom → x.dom, ∀ hy : y.dom, x.get (h hy) ≤ y.get hy⟩ instance : has_top enat := ⟨none⟩ instance : has_bot enat := ⟨0⟩ instance : has_sup enat := ⟨λ x y, ⟨x.dom ∧ y.dom, λ h, x.get h.1 ⊔ y.get h.2⟩⟩ @[elab_as_eliminator] protected lemma cases_on {P : enat → Prop} : ∀ a : enat, P ⊤ → (∀ n : ℕ, P n) → P a := roption.induction_on @[simp] lemma top_add (x : enat) : ⊤ + x = ⊤ := roption.ext' (false_and _) (λ h, h.left.elim) @[simp] lemma add_top (x : enat) : x + ⊤ = ⊤ := by rw [add_comm, top_add] @[simp, squash_cast] lemma coe_zero : ((0 : ℕ) : enat) = 0 := rfl @[simp, squash_cast] lemma coe_one : ((1 : ℕ) : enat) = 1 := rfl @[simp, move_cast] lemma coe_add (x y : ℕ) : ((x + y : ℕ) : enat) = x + y := roption.ext' (and_true _).symm (λ _ _, rfl) @[simp] lemma coe_add_get {x : ℕ} {y : enat} (h : ((x : enat) + y).dom) : get ((x : enat) + y) h = x + get y h.2 := rfl @[simp] lemma get_add {x y : enat} (h : (x + y).dom) : get (x + y) h = x.get h.1 + y.get h.2 := rfl @[simp, squash_cast] lemma coe_get {x : enat} (h : x.dom) : (x.get h : enat) = x := roption.ext' (iff_of_true trivial h) (λ _ _, rfl) @[simp] lemma get_zero (h : (0 : enat).dom) : (0 : enat).get h = 0 := rfl @[simp] lemma get_one (h : (1 : enat).dom) : (1 : enat).get h = 1 := rfl lemma dom_of_le_some {x : enat} {y : ℕ} : x ≤ y → x.dom := λ ⟨h, _⟩, h trivial instance : partial_order enat := { le := (≤), le_refl := λ x, ⟨id, λ _, le_refl _⟩, le_trans := λ x y z ⟨hxy₁, hxy₂⟩ ⟨hyz₁, hyz₂⟩, ⟨hxy₁ ∘ hyz₁, λ _, le_trans (hxy₂ _) (hyz₂ _)⟩, le_antisymm := λ x y ⟨hxy₁, hxy₂⟩ ⟨hyx₁, hyx₂⟩, roption.ext' ⟨hyx₁, hxy₁⟩ (λ _ _, le_antisymm (hxy₂ _) (hyx₂ _)) } @[simp, elim_cast] lemma coe_le_coe {x y : ℕ} : (x : enat) ≤ y ↔ x ≤ y := ⟨λ ⟨_, h⟩, h trivial, λ h, ⟨λ _, trivial, λ _, h⟩⟩ @[simp, elim_cast] lemma coe_lt_coe {x y : ℕ} : (x : enat) < y ↔ x < y := by rw [lt_iff_le_not_le, lt_iff_le_not_le, coe_le_coe, coe_le_coe] lemma get_le_get {x y : enat} {hx : x.dom} {hy : y.dom} : x.get hx ≤ y.get hy ↔ x ≤ y := by conv { to_lhs, rw [← coe_le_coe, coe_get, coe_get]} instance semilattice_sup_bot : semilattice_sup_bot enat := { sup := (⊔), bot := (⊥), bot_le := λ _, ⟨λ _, trivial, λ _, nat.zero_le _⟩, le_sup_left := λ _ _, ⟨and.left, λ _, le_sup_left⟩, le_sup_right := λ _ _, ⟨and.right, λ _, le_sup_right⟩, sup_le := λ x y z ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩, ⟨λ hz, ⟨hx₁ hz, hy₁ hz⟩, λ _, sup_le (hx₂ _) (hy₂ _)⟩, ..enat.partial_order } instance order_top : order_top enat := { top := (⊤), le_top := λ x, ⟨λ h, false.elim h, λ hy, false.elim hy⟩, ..enat.semilattice_sup_bot } lemma coe_lt_top (x : ℕ) : (x : enat) < ⊤ := lt_of_le_of_ne le_top (λ h, absurd (congr_arg dom h) true_ne_false) @[simp] lemma coe_ne_top (x : ℕ) : (x : enat) ≠ ⊤ := ne_of_lt (coe_lt_top x) lemma pos_iff_one_le {x : enat} : 0 < x ↔ 1 ≤ x := enat.cases_on x ⟨λ _, le_top, λ _, coe_lt_top _⟩ (λ n, ⟨λ h, enat.coe_le_coe.2 (enat.coe_lt_coe.1 h), λ h, enat.coe_lt_coe.2 (enat.coe_le_coe.1 h)⟩) noncomputable instance : decidable_linear_order enat := { le_total := λ x y, enat.cases_on x (or.inr le_top) (enat.cases_on y (λ _, or.inl le_top) (λ x y, (le_total x y).elim (or.inr ∘ coe_le_coe.2) (or.inl ∘ coe_le_coe.2))), decidable_le := classical.dec_rel _, ..enat.partial_order } noncomputable instance : bounded_lattice enat := { inf := min, inf_le_left := min_le_left, inf_le_right := min_le_right, le_inf := λ _ _ _, le_min, ..enat.order_top, ..enat.semilattice_sup_bot } lemma sup_eq_max {a b : enat} : a ⊔ b = max a b := le_antisymm (sup_le (le_max_left _ _) (le_max_right _ _)) (max_le le_sup_left le_sup_right) lemma inf_eq_min {a b : enat} : a ⊓ b = min a b := rfl instance : ordered_comm_monoid enat := { add_le_add_left := λ a b ⟨h₁, h₂⟩ c, enat.cases_on c (by simp) (λ c, ⟨λ h, and.intro trivial (h₁ h.2), λ _, add_le_add_left (h₂ _) c⟩), lt_of_add_lt_add_left := λ a b c, enat.cases_on a (λ h, by simpa [lt_irrefl] using h) (λ a, enat.cases_on b (λ h, absurd h (not_lt_of_ge (by rw add_top; exact le_top))) (λ b, enat.cases_on c (λ _, coe_lt_top _) (λ c h, coe_lt_coe.2 (by rw [← coe_add, ← coe_add, coe_lt_coe] at h; exact lt_of_add_lt_add_left h)))), ..enat.decidable_linear_order, ..enat.add_comm_monoid } instance : canonically_ordered_monoid enat := { le_iff_exists_add := λ a b, enat.cases_on b (iff_of_true le_top ⟨⊤, (add_top _).symm⟩) (λ b, enat.cases_on a (iff_of_false (not_le_of_gt (coe_lt_top _)) (not_exists.2 (λ x, ne_of_lt (by rw [top_add]; exact coe_lt_top _)))) (λ a, ⟨λ h, ⟨(b - a : ℕ), by rw [← coe_add, coe_inj, add_comm, nat.sub_add_cancel (coe_le_coe.1 h)]⟩, (λ ⟨c, hc⟩, enat.cases_on c (λ hc, hc.symm ▸ show (a : enat) ≤ a + ⊤, by rw [add_top]; exact le_top) (λ c (hc : (b : enat) = a + c), coe_le_coe.2 (by rw [← coe_add, coe_inj] at hc; rw hc; exact nat.le_add_right _ _)) hc)⟩)), ..enat.semilattice_sup_bot, ..enat.ordered_comm_monoid } section with_top def to_with_top (x : enat) [decidable x.dom]: with_top ℕ := x.to_option lemma to_with_top_top : to_with_top ⊤ = ⊤ := rfl @[simp] lemma to_with_top_top' [decidable (⊤ : enat).dom] : to_with_top ⊤ = ⊤ := by convert to_with_top_top lemma to_with_top_zero : to_with_top 0 = 0 := rfl @[simp] lemma to_with_top_zero' [decidable (0 : enat).dom]: to_with_top 0 = 0 := by convert to_with_top_zero lemma to_with_top_coe (n : ℕ) : to_with_top n = n := rfl @[simp] lemma to_with_top_coe' (n : ℕ) [decidable (n : enat).dom] : to_with_top (n : enat) = n := by convert to_with_top_coe n @[simp] lemma to_with_top_le {x y : enat} : Π [decidable x.dom] [decidable y.dom], by exactI to_with_top x ≤ to_with_top y ↔ x ≤ y := enat.cases_on y (by simp) (enat.cases_on x (by simp) (by intros; simp)) @[simp] lemma to_with_top_lt {x y : enat} [decidable x.dom] [decidable y.dom] : to_with_top x < to_with_top y ↔ x < y := by simp only [lt_iff_le_not_le, to_with_top_le] end with_top lemma lt_wf : well_founded ((<) : enat → enat → Prop) := show well_founded (λ a b : enat, a < b), by haveI := classical.dec; simp only [to_with_top_lt.symm] {eta := ff}; exact inv_image.wf _ (with_top.well_founded_lt nat.lt_wf) instance : has_well_founded enat := ⟨(<), lt_wf⟩ end enat
e2277ea9d907bf8f96ea9f60b1874eb0b450f456
efce24474b28579aba3272fdb77177dc2b11d7aa
/src/homotopy_theory/formal/cofibrations/track.lean
e2ae9af2e0b6347725cd29dd73f3650529fafe02
[ "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
26,209
lean
import category_theory.assoc_pushouts import category_theory.eq_to_hom import category_theory.groupoid import category_theory.transport import .homotopy universes v u open category_theory open category_theory.category local notation f ` ∘ `:80 g:80 := g ≫ f namespace homotopy_theory.cofibrations open homotopy_theory.weak_equivalences open homotopy_theory.weak_equivalences.category_with_weak_equivalences open precofibration_category cofibration_category variables {C : Type u} [category.{v} C] [cofibration_category.{v} C] -- Tracks, or "homotopies up to homotopy". This notion is a bit tricky -- because there is no canonical choice of cylinder object on which to -- define homotopies. Instead, we define an equivalence relation -- between homotopies defined on different cylinder objects and define -- a track to be an equivalence class, and then show that every -- cylinder object admits a unique homotopy class of homotopies -- representing each track. variables {a b : C} {j : a ⟶ b} (hj : is_cof j) variables {x : C} variables (f₀ f₁ : b ⟶ x) structure homotopy := (c : relative_cylinder hj) (h : homotopy_on c f₀ f₁) variables {hj f₀ f₁} -- An extension of homotopies. These are like acyclic cofibrations in -- a category of objects under b ⊔ₐ b and over b and x, where the -- compositions b ⊔ₐ b → b and b ⊔ₐ b → x are given by the fold map -- and (f₀, f₁) respectively. structure homotopy_extension (t t' : homotopy hj f₀ f₁) := (m : cylinder_embedding t.c t'.c) (e : t'.h.H ∘ m.k = t.h.H) def homotopy_extension.refl (t : homotopy hj f₀ f₁) : homotopy_extension t t := ⟨cylinder_embedding.refl t.c, show _ ∘ 𝟙 _ = _, by simp⟩ def homotopy_extension.trans {t₀ t₁ t₂ : homotopy hj f₀ f₁} (m₀ : homotopy_extension t₀ t₁) (m₁ : homotopy_extension t₁ t₂) : homotopy_extension t₀ t₂ := ⟨m₀.m.trans m₁.m, by dsimp [cylinder_embedding.trans]; rw [assoc, m₁.e, m₀.e]⟩ def homotopy_extension.pushout {t t₀ t₁ : homotopy hj f₀ f₁} (m₀ : homotopy_extension t t₀) (m₁ : homotopy_extension t t₁) : homotopy hj f₀ f₁ := ⟨cylinder_embedding.pushout m₀.m m₁.m, ⟨(cylinder_embedding.pushout.is_pushout m₀.m m₁.m).induced t₀.h.H t₁.h.H (by rw [m₀.e, m₁.e]), begin convert t₁.h.Hi₀ using 1, unfold relative_cylinder.i₀, dsimp [cylinder_embedding.pushout], simp end, begin convert t₁.h.Hi₁ using 1, unfold relative_cylinder.i₁, dsimp [cylinder_embedding.pushout], simp end⟩⟩ def homotopy_extension.pushout.map₀ {t t₀ t₁ : homotopy hj f₀ f₁} (m₀ : homotopy_extension t t₀) (m₁ : homotopy_extension t t₁) : homotopy_extension t₀ (homotopy_extension.pushout m₀ m₁) := ⟨cylinder_embedding.pushout.map₀ m₀.m m₁.m, by dsimp [cylinder_embedding.pushout.map₀, homotopy_extension.pushout]; simp⟩ def homotopy_extension.pushout.map₁ {t t₀ t₁ : homotopy hj f₀ f₁} (m₀ : homotopy_extension t t₀) (m₁ : homotopy_extension t t₁) : homotopy_extension t₁ (homotopy_extension.pushout m₀ m₁) := ⟨cylinder_embedding.pushout.map₁ m₀.m m₁.m, by dsimp [cylinder_embedding.pushout.map₁, homotopy_extension.pushout]; simp⟩ -- Two homotopies are equivalent if they have a common extension. def homotopy_equiv (t₀ t₁ : homotopy hj f₀ f₁) : Prop := ∃ t' (m₀ : homotopy_extension t₀ t') (m₁ : homotopy_extension t₁ t'), true -- Homotopy equivalence is an equivalence relation. lemma homotopy_equiv.refl (t : homotopy hj f₀ f₁) : homotopy_equiv t t := ⟨t, homotopy_extension.refl t, homotopy_extension.refl t, ⟨⟩⟩ lemma homotopy_equiv.symm {t₀ t₁ : homotopy hj f₀ f₁} : homotopy_equiv t₀ t₁ → homotopy_equiv t₁ t₀ := assume ⟨t', m₀, m₁, ⟨⟩⟩, ⟨t', m₁, m₀, ⟨⟩⟩ lemma homotopy_equiv.trans {t₀ t₁ t₂ : homotopy hj f₀ f₁} : homotopy_equiv t₀ t₁ → homotopy_equiv t₁ t₂ → homotopy_equiv t₀ t₂ := assume ⟨t, m₀, m₁, ⟨⟩⟩ ⟨t', m₁', m₂', ⟨⟩⟩, ⟨m₁.pushout m₁', m₀.trans (homotopy_extension.pushout.map₀ m₁ m₁'), m₂'.trans (homotopy_extension.pushout.map₁ m₁ m₁'), ⟨⟩⟩ structure homotopy_iso (t t' : homotopy hj f₀ f₁) := (k : t.c.ob ≅ t'.c.ob) (hkii : k.hom ∘ t.c.ii = t'.c.ii) (hpk : t'.c.p ∘ k.hom = t.c.p) (e : t'.h.H ∘ k.hom = t.h.H) lemma homotopy_equiv_of_iso {t t' : homotopy hj f₀ f₁} (i : homotopy_iso t t') : homotopy_equiv t t' := ⟨t', ⟨⟨i.k.hom, cof_iso _, i.hkii, i.hpk⟩, i.e⟩, homotopy_extension.refl t', ⟨⟩⟩ instance homotopy_equiv.setoid : setoid (homotopy hj f₀ f₁) := { r := homotopy_equiv, iseqv := ⟨λ t, homotopy_equiv.refl t, λ t₀ t₁, homotopy_equiv.symm, λ t₀ t₁ t₂, homotopy_equiv.trans⟩ } variables (hj f₀ f₁) def track := quotient (homotopy_equiv.setoid : setoid (homotopy hj f₀ f₁)) private noncomputable def chosen_cylinder : relative_cylinder hj := classical.choice (exists_relative_cylinder hj) variables {hj f₀ f₁} noncomputable def track.refl (f : b ⟶ x) : track hj f f := ⟦⟨chosen_cylinder hj, homotopy_on.refl f⟩⟧ lemma track.refl_eq {f : b ⟶ x} (c : relative_cylinder hj) : (track.refl f : track hj f f) = ⟦⟨c, homotopy_on.refl f⟩⟧ := quot.sound $ let c₀ := chosen_cylinder hj, ⟨⟨c', m₀, m₁⟩⟩ := exists_common_embedding c₀ c in ⟨⟨c', homotopy_on.refl f⟩, ⟨m₀, show f ∘ c'.p ∘ m₀.k = f ∘ c₀.p, by rw [←assoc, m₀.hpk]⟩, ⟨m₁, show f ∘ c'.p ∘ m₁.k = f ∘ c.p, by rw [←assoc, m₁.hpk]⟩, ⟨⟩⟩ local attribute [elab_with_expected_type] quotient.lift_on quotient.lift_on₂ def track.symm {f₀ f₁ : b ⟶ x} : track hj f₀ f₁ → track hj f₁ f₀ := λ t, quotient.lift_on t (λ t, ⟦⟨t.c.reverse, t.h.symm⟩⟧) (assume t t' ⟨t'', m₀, m₁, ⟨⟩⟩, quotient.sound $ ⟨⟨t''.c.reverse, t''.h.symm⟩, ⟨m₀.m.reverse, m₀.e⟩, ⟨m₁.m.reverse, m₁.e⟩, ⟨⟩⟩) def track.trans {f₀ f₁ f₂ : b ⟶ x} : track hj f₀ f₁ → track hj f₁ f₂ → track hj f₀ f₂ := λ t₀ t₁, quotient.lift_on₂ t₀ t₁ (λ t₀ t₁, ⟦⟨t₀.c.glue t₁.c, t₀.h.trans t₁.h⟩⟧) (assume t₀ t₁ t₀' t₁' ⟨t₀'', m₀₀, m₀₁, ⟨⟩⟩ ⟨t₁'', m₁₀, m₁₁, ⟨⟩⟩, quotient.sound $ ⟨⟨t₀''.c.glue t₁''.c, t₀''.h.trans t₁''.h⟩, ⟨m₀₀.m.glue m₁₀.m, begin apply (pushout_by_cof t₀.c.i₁ t₁.c.i₀ t₀.c.acof_i₁.1).is_pushout.uniqueness; dsimp [homotopy_on.trans, cylinder_embedding.glue]; rw ←assoc; simp [m₀₀.e, m₁₀.e], end⟩, ⟨m₀₁.m.glue m₁₁.m, begin apply (pushout_by_cof t₀'.c.i₁ t₁'.c.i₀ t₀'.c.acof_i₁.1).is_pushout.uniqueness; dsimp [homotopy_on.trans, cylinder_embedding.glue]; rw ←assoc; simp [m₀₁.e, m₁₁.e], end⟩, ⟨⟩⟩) -- The groupoid laws. lemma track.left_identity {f₀ f₁ : b ⟶ x} (t : track hj f₀ f₁) : track.trans (track.refl _) t = t := quotient.induction_on t $ λ ⟨c₁, h⟩, quotient.sound $ -- Set up variable names to match `exists_common_embedding` as -- closely as possible, so that what we construct is, in particular, -- a common embedding of c₀ and c₁. let c := chosen_cylinder hj, c₀ := c.glue c₁, p' : c₀.ob ⟶ c₁.ob := (pushout_by_cof c.i₁ c₁.i₀ c.acof_i₁.1).is_pushout.induced (c₁.i₀ ∘ c.p) (𝟙 c₁.ob) (by rw [←assoc, c.pi₁]; simp), po := pushout_by_cof c₀.ii c₁.ii c₀.hii, pp := po.is_pushout.induced p' (𝟙 c₁.ob) $ begin apply (pushout_by_cof j j hj).is_pushout.uniqueness, { rw [←assoc, ←assoc], change _ ∘ c₀.i₀ = _ ∘ c₁.i₀, simp, rw [←assoc, c.pi₀], simp }, { rw [←assoc, ←assoc], change _ ∘ c₀.i₁ = _ ∘ c₁.i₁, simp } end, ⟨c'_ob, l, q', hl, hq', q'l⟩ := factorization pp, cem := common_embedding_of_factorization c₀ c₁ po c'_ob l (c₁.p ∘ q') hl (weq_comp hq' c₁.hp) $ begin rw [←assoc, q'l], apply po.is_pushout.uniqueness; rw ←assoc; simp, apply (pushout_by_cof c.i₁ c₁.i₀ c.acof_i₁.1).is_pushout.uniqueness; rw ←assoc; simp; change _ = Is_pushout.induced _ _ _ _ ∘ _, { simp [c₁.pi₀] }, { simp }, end, h' : homotopy_on cem.c' f₀ f₁ := ⟨h.H ∘ q', calc h.H ∘ q' ∘ (l ∘ po.map₁ ∘ c₁.ii ∘ _) = h.H ∘ (q' ∘ l ∘ po.map₁) ∘ c₁.i₀ : by simp [relative_cylinder.i₀] ... = h.H ∘ c₁.i₀ : by rw q'l; simp ... = f₀ : h.Hi₀, calc h.H ∘ q' ∘ (l ∘ po.map₁ ∘ c₁.ii ∘ _) = h.H ∘ (q' ∘ l ∘ po.map₁) ∘ c₁.i₁ : by simp [relative_cylinder.i₁] ... = h.H ∘ c₁.i₁ : by rw q'l; simp ... = f₁ : h.Hi₁⟩ in ⟨⟨cem.c', h'⟩, ⟨cem.m₀, calc h.H ∘ q' ∘ (l ∘ po.map₀) = h.H ∘ ((q' ∘ l) ∘ po.map₀) : by simp ... = h.H ∘ (pp ∘ po.map₀) : by rw q'l ... = h.H ∘ p' : by simp ... = (homotopy_on.trans (homotopy_on.refl f₀) h).H : begin unfold homotopy_on.trans homotopy_on.refl, apply (pushout_by_cof c.i₁ c₁.i₀ c.acof_i₁.1).is_pushout.uniqueness; rw ←assoc; simp [h.Hi₀] end⟩, ⟨cem.m₁, calc h.H ∘ q' ∘ (l ∘ po.map₁) = h.H ∘ ((q' ∘ l) ∘ po.map₁) : by simp ... = h.H ∘ (pp ∘ po.map₁) : by rw q'l ... = h.H : by simp⟩, ⟨⟩⟩ lemma track.left_inverse {f₀ f₁ : b ⟶ x} (t : track hj f₀ f₁) : track.trans t.symm t = track.refl _ := quotient.induction_on t $ λ ⟨c, h⟩, quotient.sound $ -- Set up variable names to match `exists_common_embedding` as -- closely as possible, so that what we construct is, in particular, -- a common embedding of c₀ and c₁. let c₁ := chosen_cylinder hj, c₀ := c.reverse.glue c, p' : c₀.ob ⟶ c.ob := (pushout_by_cof c.reverse.i₁ c.i₀ c.reverse.acof_i₁.1).is_pushout.induced (𝟙 c.ob) (𝟙 c.ob) (by simp; erw comp_id), -- Yuck po := pushout_by_cof c₀.ii c₁.ii c₀.hii, pp := po.is_pushout.induced p' (c.i₁ ∘ c₁.p) $ begin apply (pushout_by_cof j j hj).is_pushout.uniqueness; rw [←assoc, ←assoc], { change _ ∘ c₀.i₀ = _ ∘ c₁.i₀, simp, erw [←assoc, c₁.pi₀, comp_id], simp }, { change _ ∘ c₀.i₁ = _ ∘ c₁.i₁, simp, rw [←assoc, c₁.pi₁], simp } end, ⟨c'_ob, l, q', hl, hq', q'l⟩ := factorization pp, cem := common_embedding_of_factorization c₀ c₁ po c'_ob l (c.p ∘ q') hl (weq_comp hq' c.hp) $ begin rw [←assoc, q'l], apply po.is_pushout.uniqueness; rw ←assoc; simp, apply (pushout_by_cof c.reverse.i₁ c.i₀ c.reverse.acof_i₁.1).is_pushout.uniqueness; rw ←assoc; simp; change _ = Is_pushout.induced _ _ _ _ ∘ _, { erw [id_comp, Is_pushout.induced_commutes₀], refl }, { simp }, { simp [c.pi₁] } -- What is this even for? end, h' : homotopy_on cem.c' f₁ f₁ := ⟨h.H ∘ q', calc h.H ∘ q' ∘ (l ∘ po.map₁ ∘ c₁.ii ∘ _) = h.H ∘ (q' ∘ l ∘ po.map₁) ∘ c₁.i₀ : by simp [relative_cylinder.i₀] ... = h.H ∘ c.i₁ ∘ (c₁.p ∘ c₁.i₀) : by rw q'l; simp ... = f₁ : by rw [c₁.pi₀, h.Hi₁]; simp, calc h.H ∘ q' ∘ (l ∘ po.map₁ ∘ c₁.ii ∘ _) = h.H ∘ (q' ∘ l ∘ po.map₁) ∘ c₁.i₁ : by simp [relative_cylinder.i₁] ... = h.H ∘ c.i₁ ∘ (c₁.p ∘ c₁.i₁) : by rw q'l; simp ... = f₁ : by rw [c₁.pi₁, h.Hi₁]; simp⟩ in ⟨⟨cem.c', h'⟩, ⟨cem.m₀, calc h.H ∘ q' ∘ (l ∘ po.map₀) = h.H ∘ ((q' ∘ l) ∘ po.map₀) : by simp ... = h.H ∘ (pp ∘ po.map₀) : by rw q'l ... = h.H ∘ p' : by simp ... = (homotopy_on.trans h.symm h).H : begin unfold homotopy_on.trans homotopy_on.symm, apply (pushout_by_cof c.reverse.i₁ c.i₀ c.reverse.acof_i₁.1).is_pushout.uniqueness; rw ←assoc; simp; erw id_comp end⟩, ⟨cem.m₁, calc h.H ∘ q' ∘ (l ∘ po.map₁) = h.H ∘ ((q' ∘ l) ∘ po.map₁) : by simp ... = h.H ∘ (pp ∘ po.map₁) : by rw q'l ... = h.H ∘ c.i₁ ∘ c₁.p : by simp ... = (homotopy_on.refl f₁).H : by rw h.Hi₁; refl⟩, ⟨⟩⟩ lemma track.inverse_inverse {f₀ f₁ : b ⟶ x} {t : track hj f₀ f₁} : t.symm.symm = t := -- t.symm.symm and t are homotopies defined on cylinder objects which -- are equal, but not definitionally equal. Rather than dealing with -- heterogeneous equality between the homotopies, it's easier to just -- use `homotopy_equiv_of_iso`. quotient.induction_on t $ λ t, quotient.sound $ homotopy_equiv_of_iso $ ⟨iso.refl _, by apply (pushout_by_cof j j hj).is_pushout.uniqueness; dsimp [relative_cylinder.reverse, Is_pushout.swap]; rw [←assoc, ←assoc, ←assoc]; simp, by dsimp [relative_cylinder.reverse]; simp, by simp [homotopy_on.symm]⟩ lemma track.right_inverse {f₀ f₁ : b ⟶ x} (t : track hj f₀ f₁) : track.trans t t.symm = track.refl _ := by convert track.left_inverse t.symm; rw track.inverse_inverse lemma track.assoc {f₀ f₁ f₂ f₃ : b ⟶ x} (t₀ : track hj f₀ f₁) (t₁ : track hj f₁ f₂) (t₂ : track hj f₂ f₃) : (t₀.trans t₁).trans t₂ = t₀.trans (t₁.trans t₂) := quotient.induction_on₃ t₀ t₁ t₂ $ λ t₀ t₁ t₂, quotient.sound $ homotopy_equiv_of_iso ⟨Is_pushout_assoc (pushout_by_cof t₀.c.i₁ t₁.c.i₀ t₀.c.acof_i₁.1).is_pushout (by convert (pushout_by_cof (t₀.c.glue t₁.c).i₁ t₂.c.i₀ _).is_pushout using 1; simp) (pushout_by_cof t₁.c.i₁ t₂.c.i₀ t₁.c.acof_i₁.1).is_pushout (by convert (pushout_by_cof t₀.c.i₁ (t₁.c.glue t₂.c).i₀ _).is_pushout using 1; simp), begin apply (pushout_by_cof j j hj).is_pushout.uniqueness; rw ←assoc, { change _ ∘ relative_cylinder.i₀ _ = relative_cylinder.i₀ _, simp }, { change _ ∘ relative_cylinder.i₁ _ = relative_cylinder.i₁ _, simp } end, begin symmetry, apply Is_pushout_assoc_uniqueness; dsimp [relative_cylinder.glue]; simp end, begin symmetry, apply Is_pushout_assoc_uniqueness; dsimp [relative_cylinder.glue, homotopy_on.trans]; simp end⟩ lemma track.right_identity {f₀ f₁ : b ⟶ x} (t : track hj f₀ f₁) : track.trans t (track.refl _) = t := calc t.trans (track.refl _) = t.trans (t.symm.trans t) : by rw track.left_inverse ... = (t.trans t.symm).trans t : by rw track.assoc ... = (track.refl _).trans t : by rw track.right_inverse ... = t : by rw track.left_identity section variables (hj x) include hj def track_groupoid_rel := b ⟶ x end noncomputable instance : groupoid (track_groupoid_rel hj x) := { hom := λ f₀ f₁, track hj f₀ f₁, id := λ f, track.refl f, comp := λ f₀ f₁ f₂ t₀ t₁, t₀.trans t₁, inv := λ f₀ f₁ t, t.symm, id_comp' := λ f₀ f₁, track.left_identity, comp_id' := λ f₀ f₁, track.right_identity, assoc' := λ f₀ f₁ f₂ f₃, track.assoc, inv_comp' := λ f₀ f₁, track.left_inverse, comp_inv' := λ f₀ f₁, track.right_inverse } section functoriality variables {y : C} (g : x ⟶ y) def track.congr_left {f₀ f₁ : b ⟶ x} (t : track hj f₀ f₁) : track hj (g ∘ f₀) (g ∘ f₁) := quotient.lift_on t (λ t, ⟦⟨t.c, t.h.congr_left hj g⟩⟧) (λ t t' ⟨t'', m₀, m₁, ⟨⟩⟩, quotient.sound ⟨⟨t''.c, t''.h.congr_left hj g⟩, ⟨m₀.m, show (g ∘ _) ∘ _ = _, by rw [←assoc, m₀.e]; refl⟩, ⟨m₁.m, show (g ∘ _) ∘ _ = _, by rw [←assoc, m₁.e]; refl⟩, ⟨⟩⟩) noncomputable def track_groupoid_rel_functor {y} (g : x ⟶ y) : track_groupoid_rel hj x ↝ track_groupoid_rel hj y := { obj := λ f, g ∘ f, map := λ f₀ f₁ t, t.congr_left g, map_id' := λ f, show (track.refl f).congr_left g = track.refl (g ∘ f), begin apply congr_arg quotient.mk, unfold homotopy_on.refl homotopy_on.congr_left, congr' 2, rw ←assoc, refl end, map_comp' := λ f₀ f₁ f₂ t₀ t₁, show (t₀.trans t₁).congr_left g = (t₀.congr_left g).trans (t₁.congr_left g), begin induction t₀ using quot.ind, induction t₁ using quot.ind, apply congr_arg quotient.mk, congr', apply homotopy_on.ext, apply pushout_induced_comp end } -- TODO: Precomposition is also a functor, & they are compatible, -- interchange, etc. end functoriality -- We next relate tracks back to the original notion of "homotopies up -- to homotopy", showing that for any particular relative cylinder -- object c on a → b, tracks from f₀ : b → x to f₁ : b → x are in -- one-to-one correspondence with homotopy classes rel b ⊔ₐ b → c of -- homotopies from f₀ to f₁ defined on the given relative cylinder c, -- provided that x is fibrant. section track_homotopy_class variables {c : relative_cylinder hj} -- Use g₀ g₁ instead of f₀ f₁, so that we can put the variables in the -- correct order (c first). variables (hx : fibrant x) variables {g₀ g₁ : b ⟶ x} section correspondence def homotopic_homotopies (h₀ h₁ : homotopy_on c g₀ g₁) : Prop := homotopic_rel c.hii h₀.H h₁.H variables (c g₀ g₁) instance homotopic_homotopies.setoid : setoid (homotopy_on c g₀ g₁) := { r := homotopic_homotopies, iseqv := ⟨λ h, homotopic_rel.refl _, λ h₀ h₁, homotopic_rel.symm, λ h₀ h₁ h₂, homotopic_rel.trans⟩ } def homotopy_up_to_homotopy : Type v := quotient (homotopic_homotopies.setoid c g₀ g₁) variables {c g₀ g₁} -- One direction of the correspondence is easy. def track_of_homotopy_on (h : homotopy_on c g₀ g₁) : track hj g₀ g₁ := ⟦⟨c, h⟩⟧ variables {x c g₀ g₁} lemma eq_track_of_homotopic_rel (h₀ h₁ : homotopy_on c g₀ g₁) : homotopic_homotopies h₀ h₁ → track_of_homotopy_on h₀ = track_of_homotopy_on h₁ := assume ⟨c', ⟨⟨H, Hi₀, Hi₁⟩⟩⟩, quotient.sound $ -- c' is a relative cylinder on b ⊔ₐ b → c. We can also view its -- underlying object as a relative cylinder on the original map a → b, -- and then H as a homotopy from g₀ to g₁ rel a → b. let c'' : relative_cylinder hj := ⟨c'.ob, c'.i₀ ∘ c.ii, c.p ∘ c'.p, cof_comp c.hii c'.acof_i₀.1, weq_comp c'.hp c.hp, calc (c.p ∘ c'.p) ∘ (c'.i₀ ∘ c.ii) = c.p ∘ (c'.p ∘ c'.i₀) ∘ c.ii : by simp ... = c.p ∘ c.ii : by rw c'.pi₀; simp ... = _ : c.pii⟩ in ⟨⟨c'', ⟨H, show H ∘ (c'.i₀ ∘ c.ii ∘ _) = _, by simp [Hi₀]; rw ←assoc; exact h₀.Hi₀, show H ∘ (c'.i₀ ∘ c.ii ∘ _) = _, by simp [Hi₀]; rw ←assoc; exact h₀.Hi₁⟩⟩, ⟨⟨c'.i₀, c'.acof_i₀.1, rfl, by rw ←assoc; simp [c'.pi₀]⟩, Hi₀⟩, ⟨⟨c'.i₁, c'.acof_i₁.1, c'.ij.symm, by rw ←assoc; simp [c'.pi₁]⟩, Hi₁⟩, ⟨⟩⟩ local attribute [elab_with_expected_type] quotient.lift def track_of_homotopy_class : homotopy_up_to_homotopy c g₀ g₁ → track hj g₀ g₁ := quotient.lift track_of_homotopy_on (λ h₀ h₁, eq_track_of_homotopic_rel h₀ h₁) -- Similar to `homotopic_iff`. We choose a common embedding of c and -- the cylinder on which t is defined, use the hypothesis that x is -- fibrant to extend the homotopy to this new cylinder, and then -- restrict it to c. private def surj (t : track hj g₀ g₁) : ∃ h : homotopy_up_to_homotopy c g₀ g₁, track_of_homotopy_class h = t := quotient.induction_on t $ λ ⟨c', ⟨H, Hi₀, Hi₁⟩⟩, let ⟨⟨c'', m₀, m₁⟩⟩ := exists_common_embedding c c', ⟨H', hH'⟩ := fibrant_iff_rlp.mp hx m₁.acof_k H in ⟨⟦⟨H' ∘ m₀.k, by rw [←assoc, m₀.hki₀, ←m₁.hki₀, assoc, hH', Hi₀], by rw [←assoc, m₀.hki₁, ←m₁.hki₁, assoc, hH', Hi₁]⟩⟧, quotient.sound ⟨⟨c'', ⟨H', by rw [←m₁.hki₀, assoc, hH', Hi₀], by rw [←m₁.hki₁, assoc, hH', Hi₁]⟩⟩, ⟨m₀, rfl⟩, ⟨m₁, hH'⟩, ⟨⟩⟩⟩ set_option eqn_compiler.zeta true private def inj (h₀ h₁ : homotopy_up_to_homotopy c g₀ g₁) : track_of_homotopy_class h₀ = track_of_homotopy_class h₁ → h₀ = h₁ := quotient.induction_on₂ h₀ h₁ $ λ h₀ h₁ e, quotient.sound $ let ⟨t', m₀, m₁, ⟨⟩⟩ := quotient.exact e, c' := chosen_cylinder c.hii, po := pushout_by_cof c'.ii ((pushout_by_cof c.ii c.ii c.hii).is_pushout.induced m₀.m.k m₁.m.k (by rw [m₀.m.hkii, m₁.m.hkii])) c'.hii, p' := po.is_pushout.induced (c.p ∘ c'.p) t'.c.p $ begin rw [←assoc, c'.pii], rw [pushout_induced_comp, pushout_induced_comp], congr' 1, { simp [m₀.m.hpk] }, { simp [m₁.m.hpk] } end, ⟨z, l, q, hl, hq, ql⟩ := factorization p' in have is_acof (l ∘ po.map₁), from ⟨cof_comp (pushout_is_cof po.is_pushout c'.hii) hl, weq_of_comp_weq_right hq (by convert t'.c.hp; simp [ql])⟩, let ⟨H', hH'⟩ := fibrant_iff_rlp.mp hx this t'.h.H in begin rw assoc at hH', refine ⟨c', ⟨⟨H' ∘ l ∘ po.map₀, _, _⟩⟩⟩; { change _ ∘ _ ∘ _ ∘ (_ ∘ _) = _, rw assoc, conv { to_lhs, congr, skip, rw ←assoc }, rw po.is_pushout.commutes, simp [hH'], rw ←assoc, simp [m₀.e, m₁.e] } end variables (c) -- FIXME: Why doesn't ≃ work here?? noncomputable def homotopy_class_equiv_track : equiv (homotopy_up_to_homotopy c g₀ g₁) (track hj g₀ g₁) := equiv.of_bijective _ ⟨inj hx, surj hx⟩ section variables (hj hx) include hj c hx def homotopy_class_groupoid := b ⟶ x end noncomputable instance homotopy_class_groupoid.groupoid : groupoid (homotopy_class_groupoid hj c hx) := transported_groupoid (by apply_instance : groupoid (track_groupoid_rel hj x)) (λ g₀ g₁, (homotopy_class_equiv_track c hx).symm) -- This is definitionally equal to .to_category of the above, -- but helps with defining homotopy_class_functor somehow. private noncomputable def homotopy_class_groupoid.category : category (homotopy_class_groupoid hj c hx) := transported_category (by apply_instance : category (track_groupoid_rel hj x)) (λ g₀ g₁, (homotopy_class_equiv_track c hx).symm) section functoriality variables {c} {y : C} (hy : fibrant y) (k : x ⟶ y) def homotopy_up_to_homotopy.congr_left {g₀ g₁ : b ⟶ x} (h : homotopy_up_to_homotopy c g₀ g₁) : homotopy_up_to_homotopy c (k ∘ g₀) (k ∘ g₁) := quotient.lift_on h (λ h, ⟦h.congr_left hj k⟧) (λ h h' H, quotient.sound (H.congr_left c.hii k)) noncomputable def homotopy_class_functor : homotopy_class_groupoid hj c hx ↝ homotopy_class_groupoid hj c hy := show @category_theory.functor _ (homotopy_class_groupoid.category c hx) _ (homotopy_class_groupoid.category c hy), from transported_functor (λ g₀ g₁, (homotopy_class_equiv_track c hx).symm) (λ g₀ g₁, (homotopy_class_equiv_track c hy).symm) (track_groupoid_rel_functor k) lemma homotopy_class_functor.obj {g : homotopy_class_groupoid hj c hx} : (homotopy_class_functor hx hy k).obj g = k ∘ g := rfl lemma homotopy_class_functor.hom {g₀ g₁ : homotopy_class_groupoid hj c hx} {h : g₀ ⟶ g₁} : homotopy_class_functor hx hy k &> h = h.congr_left k := quotient.induction_on h $ λ h, begin dsimp [homotopy_class_functor, transported_functor], rw ←equiv.eq_symm_apply, rw [equiv.symm_symm, equiv.symm_symm], refl end private lemma heq_of_homotopies_eq {g₀ g₀' g₁ g₁' : homotopy_class_groupoid hj c hx} (e₀ : g₀ = g₀') (e₁ : g₁ = g₁') (H : homotopy_on c g₀ g₁) (H' : homotopy_on c g₀' g₁') (e : H.H = H'.H) : (⟦H⟧ : g₀ ⟶ g₁) == (⟦H'⟧ : g₀' ⟶ g₁') := begin subst e₀, subst e₁, congr, cases H, cases H', congr, exact e end -- Next we show that homotopy_class_functor is functorial in "k". lemma homotopy_class_functor.map_id : homotopy_class_functor hx hx (𝟙 x) = functor.id (homotopy_class_groupoid hj c hx) := begin fapply functor.hext, { intro g, rw [homotopy_class_functor.obj], simp }, { intros g₀ g₁ h, rw homotopy_class_functor.hom, induction h using quot.ind, apply heq_of_homotopies_eq; { simp [homotopy_on.congr_left] } } end lemma homotopy_class_functor.map_comp {z : C} (hz : fibrant z) (k' : y ⟶ z) : (homotopy_class_functor hx hz (k' ∘ k) : homotopy_class_groupoid hj c hx ↝ _) = (homotopy_class_functor hx hy k).comp (homotopy_class_functor hy hz k') := begin fapply functor.hext, { intro g, rw [homotopy_class_functor.obj], rw [←assoc], refl }, { intros g₀ g₁ h, rw [functor.comp_map], repeat { rw homotopy_class_functor.hom }, induction h using quot.ind, apply heq_of_homotopies_eq hz; { simp [homotopy_class_functor.obj, homotopy_on.congr_left] } } end end functoriality end correspondence end track_homotopy_class end homotopy_theory.cofibrations
dc93b59824d22018112d585d8ba6040aa8d5678c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/module/default_auto.lean
b7c6c8a35165650f14e6528d674811fb6c5dbf7c
[]
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
341
lean
/- Copyright (c) 2020 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.module.basic import Mathlib.algebra.module.submodule import Mathlib.PostPort namespace Mathlib end Mathlib
7da348de536dd3ddf9a779b8e57bab81a15f7c0a
6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf
/src/game/world5/level3.lean
e2cf5e9240bbdd024753647f58681da5369fd2b1
[ "Apache-2.0" ]
permissive
arolihas/natural_number_game
4f0c93feefec93b8824b2b96adff8b702b8b43ce
8e4f7b4b42888a3b77429f90cce16292bd288138
refs/heads/master
1,621,872,426,808
1,586,270,467,000
1,586,270,467,000
253,648,466
0
0
null
1,586,219,694,000
1,586,219,694,000
null
UTF-8
Lean
false
false
3,713
lean
/- Tactic : have ## Summary `have h : P,` will create a new goal of creating a term of type `P`, and will add `h : P` to the hypotheses for the goal you were working on. ## Details If you want to name a term of some type (because you want it in your local context for some reason), and if you have the formula for the term, you can use `have` to give the term a name. ## Example (`have q := ...` or `have q : Q := ...`) If the local context contains ``` f : P → Q p : P ``` then the tactic `have q := f(p),` will add `q` to our local context, leaving it like this: ``` f : P → Q p : P q : Q ``` If you think about it, you don't ever really need `q`, because whenever you think you need it you coudl just use `f(p)` instead. But it's good that we can introduce convenient notation like this. ## Example (`have q : Q,`) A variant of this tactic can be used where you just declare the type of the term you want to have, finish the tactic statement with a comma and no `:=`, and then Lean just adds it as a new goal. The number of goals goes up by one if you use `have` like this. For example if the local context is ``` P Q R : Prop/Type, f : P → Q, g : Q → R, p : P ⊢ R ``` then after `have q : Q,`, there will be the new goal ``` f : P → Q, g : Q → R, p : P, ⊢ Q ``` and your original goal will have `q : Q` added to the list of hypotheses. -/ /- # Function world. ## Level 3: the `have` tactic. Say you have a whole bunch of sets and functions between them, and your goal is to build a certain element of a certain set. If it helps, you can build intermediate elements of other sets along the way, using the `have` command. `have` is the Lean analogue of saying "let's define an element $q\in Q$ by..." in the middle of a calculation. It is often not logically necessary, but on the other hand it is very convenient, for example it can save on notation, or it can break proofs or calculations up into smaller steps. In the level below, we have an element of $P$ and we want an element of $U$; during the proof we will make several intermediate elements of some of the other sets involved. The diagram of sets and functions looks like this pictorially: ![diagram](https://wwwf.imperial.ac.uk/~buzzard/xena/natural_number_game_images/function_diag.jpg) and so it's clear how to make the element of $U$ from the element of $P.$ Indeed, we could solve this level in one move by typing `exact l(j(h(p))),` But let us instead stroll more lazily through the level. We can start by using the `have` tactic to make an element of $Q$: `have q := h(p),` and then we note that $j(q)$ is an element of $T$ `have t : T := j(q),` (notice how on this occasion we explicitly told Lean what set we thought $t$ was in, with that `: T` thing before the `:=`) and we could even define $u$ to be $l(t)$: `have u : U := l(t),` and then finish the level with `exact u,` . -/ /- Definition Given an element of $P$ we can define an element of $U$. -/ example (P Q R S T U: Type) (p : P) (h : P → Q) (i : Q → R) (j : Q → T) (k : S → T) (l : T → U) : U := begin end /- If you solved the level using `have`, then click on the last line of your proof (you do know you can move your cursor around with the arrow keys and explore your proof, right?) and note that the local context at that point is in something like the following mess: ``` P Q R S T U : Type, p : P, h : P → Q, i : Q → R, j : Q → T, k : S → T, l : T → U, q : Q, t : T, u : U ⊢ U ``` It was already bad enough to start with, and we added three more terms to it. In level 4 we will learn about the `apply` tactic which solves the level using another technique, without leaving so much junk behind. -/
9e006922a0498e0c1f964ab58812fc37f39ac011
fecda8e6b848337561d6467a1e30cf23176d6ad0
/src/ring_theory/ideal/basic.lean
4469f485973aa99d3a8ce2f211ef6600d35f459b
[ "Apache-2.0" ]
permissive
spolu/mathlib
bacf18c3d2a561d00ecdc9413187729dd1f705ed
480c92cdfe1cf3c2d083abded87e82162e8814f4
refs/heads/master
1,671,684,094,325
1,600,736,045,000
1,600,736,045,000
297,564,749
1
0
null
1,600,758,368,000
1,600,758,367,000
null
UTF-8
Lean
false
false
31,677
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes, Mario Carneiro -/ import algebra.associated import linear_algebra.basic import order.zorn /-! # Ideals over a ring This file defines `ideal R`, the type of ideals over a commutative ring `R`. ## Implementation notes `ideal R` is implemented using `submodule R R`, where `•` is interpreted as `*`. ## TODO Support one-sided ideals, and ideals over non-commutative rings. See `algebra.ring_quot` for quotients of non-commutative rings. -/ universes u v w variables {α : Type u} {β : Type v} open set function open_locale classical big_operators /-- Ideal in a commutative ring is an additive subgroup `s` such that `a * b ∈ s` whenever `b ∈ s`. -/ @[reducible] def ideal (R : Type u) [comm_ring R] := submodule R R namespace ideal variables [comm_ring α] (I : ideal α) {a b : α} protected lemma zero_mem : (0 : α) ∈ I := I.zero_mem protected lemma add_mem : a ∈ I → b ∈ I → a + b ∈ I := I.add_mem lemma neg_mem_iff : -a ∈ I ↔ a ∈ I := I.neg_mem_iff lemma add_mem_iff_left : b ∈ I → (a + b ∈ I ↔ a ∈ I) := I.add_mem_iff_left lemma add_mem_iff_right : a ∈ I → (a + b ∈ I ↔ b ∈ I) := I.add_mem_iff_right protected lemma sub_mem : a ∈ I → b ∈ I → a - b ∈ I := I.sub_mem lemma mul_mem_left : b ∈ I → a * b ∈ I := I.smul_mem _ lemma mul_mem_right (h : a ∈ I) : a * b ∈ I := mul_comm b a ▸ I.mul_mem_left h end ideal variables {a b : α} -- A separate namespace definition is needed because the variables were historically in a different order namespace ideal variables [comm_ring α] (I : ideal α) @[ext] lemma ext {I J : ideal α} (h : ∀ x, x ∈ I ↔ x ∈ J) : I = J := submodule.ext h theorem eq_top_of_unit_mem (x y : α) (hx : x ∈ I) (h : y * x = 1) : I = ⊤ := eq_top_iff.2 $ λ z _, calc z = z * (y * x) : by simp [h] ... = (z * y) * x : eq.symm $ mul_assoc z y x ... ∈ I : I.mul_mem_left hx theorem eq_top_of_is_unit_mem {x} (hx : x ∈ I) (h : is_unit x) : I = ⊤ := let ⟨y, hy⟩ := is_unit_iff_exists_inv'.1 h in eq_top_of_unit_mem I x y hx hy theorem eq_top_iff_one : I = ⊤ ↔ (1:α) ∈ I := ⟨by rintro rfl; trivial, λ h, eq_top_of_unit_mem _ _ 1 h (by simp)⟩ theorem ne_top_iff_one : I ≠ ⊤ ↔ (1:α) ∉ I := not_congr I.eq_top_iff_one @[simp] theorem unit_mul_mem_iff_mem {x y : α} (hy : is_unit y) : y * x ∈ I ↔ x ∈ I := begin refine ⟨λ h, _, λ h, I.smul_mem y h⟩, obtain ⟨y', hy'⟩ := is_unit_iff_exists_inv.1 hy, have := I.smul_mem y' h, rwa [smul_eq_mul, ← mul_assoc, mul_comm y' y, hy', one_mul] at this, end @[simp] theorem mul_unit_mem_iff_mem {x y : α} (hy : is_unit y) : x * y ∈ I ↔ x ∈ I := mul_comm y x ▸ unit_mul_mem_iff_mem I hy /-- The ideal generated by a subset of a ring -/ def span (s : set α) : ideal α := submodule.span α s lemma subset_span {s : set α} : s ⊆ span s := submodule.subset_span lemma span_le {s : set α} {I} : span s ≤ I ↔ s ⊆ I := submodule.span_le lemma span_mono {s t : set α} : s ⊆ t → span s ≤ span t := submodule.span_mono @[simp] lemma span_eq : span (I : set α) = I := submodule.span_eq _ @[simp] lemma span_singleton_one : span (1 : set α) = ⊤ := (eq_top_iff_one _).2 $ subset_span $ mem_singleton _ lemma mem_span_insert {s : set α} {x y} : x ∈ span (insert y s) ↔ ∃ a (z ∈ span s), x = a * y + z := submodule.mem_span_insert lemma mem_span_insert' {s : set α} {x y} : x ∈ span (insert y s) ↔ ∃a, x + a * y ∈ span s := submodule.mem_span_insert' lemma mem_span_singleton' {x y : α} : x ∈ span ({y} : set α) ↔ ∃ a, a * y = x := submodule.mem_span_singleton lemma mem_span_singleton {x y : α} : x ∈ span ({y} : set α) ↔ y ∣ x := mem_span_singleton'.trans $ exists_congr $ λ _, by rw [eq_comm, mul_comm] lemma span_singleton_le_span_singleton {x y : α} : span ({x} : set α) ≤ span ({y} : set α) ↔ y ∣ x := span_le.trans $ singleton_subset_iff.trans mem_span_singleton lemma span_singleton_eq_span_singleton {α : Type u} [integral_domain α] {x y : α} : span ({x} : set α) = span ({y} : set α) ↔ associated x y := begin rw [←dvd_dvd_iff_associated, le_antisymm_iff, and_comm], apply and_congr; rw span_singleton_le_span_singleton, end lemma span_eq_bot {s : set α} : span s = ⊥ ↔ ∀ x ∈ s, (x:α) = 0 := submodule.span_eq_bot @[simp] lemma span_singleton_eq_bot {x} : span ({x} : set α) = ⊥ ↔ x = 0 := submodule.span_singleton_eq_bot @[simp] lemma span_zero : span (0 : set α) = ⊥ := by rw [←set.singleton_zero, span_singleton_eq_bot] lemma span_singleton_eq_top {x} : span ({x} : set α) = ⊤ ↔ is_unit x := by rw [is_unit_iff_dvd_one, ← span_singleton_le_span_singleton, singleton_one, span_singleton_one, eq_top_iff] lemma span_singleton_mul_right_unit {a : α} (h2 : is_unit a) (x : α) : span ({x * a} : set α) = span {x} := begin apply le_antisymm, { rw span_singleton_le_span_singleton, use a}, { rw span_singleton_le_span_singleton, rw is_unit.mul_right_dvd h2} end lemma span_singleton_mul_left_unit {a : α} (h2 : is_unit a) (x : α) : span ({a * x} : set α) = span {x} := by rw [mul_comm, span_singleton_mul_right_unit h2] /-- The ideal generated by an arbitrary binary relation. -/ def of_rel (r : α → α → Prop) : ideal α := submodule.span α { x | ∃ (a b) (h : r a b), x = a - b } /-- An ideal `P` of a ring `R` is prime if `P ≠ R` and `xy ∈ P → x ∈ P ∨ y ∈ P` -/ @[class] def is_prime (I : ideal α) : Prop := I ≠ ⊤ ∧ ∀ {x y : α}, x * y ∈ I → x ∈ I ∨ y ∈ I theorem is_prime.mem_or_mem {I : ideal α} (hI : I.is_prime) : ∀ {x y : α}, x * y ∈ I → x ∈ I ∨ y ∈ I := hI.2 theorem is_prime.mem_or_mem_of_mul_eq_zero {I : ideal α} (hI : I.is_prime) {x y : α} (h : x * y = 0) : x ∈ I ∨ y ∈ I := hI.2 (h.symm ▸ I.zero_mem) theorem is_prime.mem_of_pow_mem {I : ideal α} (hI : I.is_prime) {r : α} (n : ℕ) (H : r^n ∈ I) : r ∈ I := begin induction n with n ih, { exact (mt (eq_top_iff_one _).2 hI.1).elim H }, exact or.cases_on (hI.mem_or_mem H) id ih end theorem zero_ne_one_of_proper {I : ideal α} (h : I ≠ ⊤) : (0:α) ≠ 1 := λ hz, I.ne_top_iff_one.1 h $ hz ▸ I.zero_mem theorem span_singleton_prime {p : α} (hp : p ≠ 0) : is_prime (span ({p} : set α)) ↔ prime p := by simp [is_prime, prime, span_singleton_eq_top, hp, mem_span_singleton] lemma bot_prime {R : Type*} [integral_domain R] : (⊥ : ideal R).is_prime := ⟨λ h, one_ne_zero (by rwa [ideal.eq_top_iff_one, submodule.mem_bot] at h), λ x y h, mul_eq_zero.mp (by simpa only [submodule.mem_bot] using h)⟩ /-- An ideal is maximal if it is maximal in the collection of proper ideals. -/ @[class] def is_maximal (I : ideal α) : Prop := I ≠ ⊤ ∧ ∀ J, I < J → J = ⊤ theorem is_maximal_iff {I : ideal α} : I.is_maximal ↔ (1:α) ∉ I ∧ ∀ (J : ideal α) x, I ≤ J → x ∉ I → x ∈ J → (1:α) ∈ J := and_congr I.ne_top_iff_one $ forall_congr $ λ J, by rw [lt_iff_le_not_le]; exact ⟨λ H x h hx₁ hx₂, J.eq_top_iff_one.1 $ H ⟨h, not_subset.2 ⟨_, hx₂, hx₁⟩⟩, λ H ⟨h₁, h₂⟩, let ⟨x, xJ, xI⟩ := not_subset.1 h₂ in J.eq_top_iff_one.2 $ H x h₁ xI xJ⟩ theorem is_maximal.eq_of_le {I J : ideal α} (hI : I.is_maximal) (hJ : J ≠ ⊤) (IJ : I ≤ J) : I = J := eq_iff_le_not_lt.2 ⟨IJ, λ h, hJ (hI.2 _ h)⟩ theorem is_maximal.exists_inv {I : ideal α} (hI : I.is_maximal) {x} (hx : x ∉ I) : ∃ y, y * x - 1 ∈ I := begin cases is_maximal_iff.1 hI with H₁ H₂, rcases mem_span_insert'.1 (H₂ (span (insert x I)) x (set.subset.trans (subset_insert _ _) subset_span) hx (subset_span (mem_insert _ _))) with ⟨y, hy⟩, rw [span_eq, ← neg_mem_iff, add_comm, neg_add', neg_mul_eq_neg_mul] at hy, exact ⟨-y, hy⟩ end theorem is_maximal.is_prime {I : ideal α} (H : I.is_maximal) : I.is_prime := ⟨H.1, λ x y hxy, or_iff_not_imp_left.2 $ λ hx, begin cases H.exists_inv hx with z hz, have := I.mul_mem_left hz, rw [mul_sub, mul_one, mul_comm, mul_assoc] at this, exact I.neg_mem_iff.1 ((I.add_mem_iff_right $ I.mul_mem_left hxy).1 this) end⟩ @[priority 100] -- see Note [lower instance priority] instance is_maximal.is_prime' (I : ideal α) : ∀ [H : I.is_maximal], I.is_prime := is_maximal.is_prime /-- Krull's theorem: if `I` is an ideal that is not the whole ring, then it is included in some maximal ideal. -/ theorem exists_le_maximal (I : ideal α) (hI : I ≠ ⊤) : ∃ M : ideal α, M.is_maximal ∧ I ≤ M := begin rcases zorn.zorn_partial_order₀ { J : ideal α | J ≠ ⊤ } _ I hI with ⟨M, M0, IM, h⟩, { refine ⟨M, ⟨M0, λ J hJ, by_contradiction $ λ J0, _⟩, IM⟩, cases h J J0 (le_of_lt hJ), exact lt_irrefl _ hJ }, { intros S SC cC I IS, refine ⟨Sup S, λ H, _, λ _, le_Sup⟩, obtain ⟨J, JS, J0⟩ : ∃ J ∈ S, (1 : α) ∈ J, from (submodule.mem_Sup_of_directed ⟨I, IS⟩ cC.directed_on).1 ((eq_top_iff_one _).1 H), exact SC JS ((eq_top_iff_one _).2 J0) } end /-- Krull's theorem: a nontrivial ring has a maximal ideal. -/ theorem exists_maximal [nontrivial α] : ∃ M : ideal α, M.is_maximal := let ⟨I, ⟨hI, _⟩⟩ := exists_le_maximal (⊥ : ideal α) submodule.bot_ne_top in ⟨I, hI⟩ /-- If P is not properly contained in any maximal ideal then it is not properly contained in any proper ideal -/ lemma maximal_of_no_maximal {R : Type u} [comm_ring R] {P : ideal R} (hmax : ∀ m : ideal R, P < m → ¬is_maximal m) (J : ideal R) (hPJ : P < J) : J = ⊤ := begin by_contradiction hnonmax, rcases exists_le_maximal J hnonmax with ⟨M, hM1, hM2⟩, exact hmax M (lt_of_lt_of_le hPJ hM2) hM1, end theorem mem_span_pair {x y z : α} : z ∈ span ({x, y} : set α) ↔ ∃ a b, a * x + b * y = z := by simp [mem_span_insert, mem_span_singleton', @eq_comm _ _ z] lemma span_singleton_lt_span_singleton [integral_domain β] {x y : β} : span ({x} : set β) < span ({y} : set β) ↔ dvd_not_unit y x := by rw [lt_iff_le_not_le, span_singleton_le_span_singleton, span_singleton_le_span_singleton, dvd_and_not_dvd_iff] lemma factors_decreasing [integral_domain β] (b₁ b₂ : β) (h₁ : b₁ ≠ 0) (h₂ : ¬ is_unit b₂) : span ({b₁ * b₂} : set β) < span {b₁} := lt_of_le_not_le (ideal.span_le.2 $ singleton_subset_iff.2 $ ideal.mem_span_singleton.2 ⟨b₂, rfl⟩) $ λ h, h₂ $ is_unit_of_dvd_one _ $ (mul_dvd_mul_iff_left h₁).1 $ by rwa [mul_one, ← ideal.span_singleton_le_span_singleton] /-- The quotient `R/I` of a ring `R` by an ideal `I`. -/ def quotient (I : ideal α) := I.quotient namespace quotient variables {I} {x y : α} instance (I : ideal α) : has_one I.quotient := ⟨submodule.quotient.mk 1⟩ instance (I : ideal α) : has_mul I.quotient := ⟨λ a b, quotient.lift_on₂' a b (λ a b, submodule.quotient.mk (a * b)) $ λ a₁ a₂ b₁ b₂ h₁ h₂, quot.sound $ begin refine calc a₁ * a₂ - b₁ * b₂ = a₂ * (a₁ - b₁) + (a₂ - b₂) * b₁ : _ ... ∈ I : I.add_mem (I.mul_mem_left h₁) (I.mul_mem_right h₂), rw [mul_sub, sub_mul, sub_add_sub_cancel, mul_comm, mul_comm b₁] end⟩ instance (I : ideal α) : comm_ring I.quotient := { mul := (*), one := 1, mul_assoc := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg submodule.quotient.mk (mul_assoc a b c), mul_comm := λ a b, quotient.induction_on₂' a b $ λ a b, congr_arg submodule.quotient.mk (mul_comm a b), one_mul := λ a, quotient.induction_on' a $ λ a, congr_arg submodule.quotient.mk (one_mul a), mul_one := λ a, quotient.induction_on' a $ λ a, congr_arg submodule.quotient.mk (mul_one a), left_distrib := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg submodule.quotient.mk (left_distrib a b c), right_distrib := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg submodule.quotient.mk (right_distrib a b c), ..submodule.quotient.add_comm_group I } /-- The ring homomorphism from a ring `R` to a quotient ring `R/I`. -/ def mk (I : ideal α) : α →+* I.quotient := ⟨λ a, submodule.quotient.mk a, rfl, λ _ _, rfl, rfl, λ _ _, rfl⟩ instance : inhabited (quotient I) := ⟨mk I 37⟩ protected theorem eq : mk I x = mk I y ↔ x - y ∈ I := submodule.quotient.eq I @[simp] theorem mk_eq_mk (x : α) : (submodule.quotient.mk x : quotient I) = mk I x := rfl lemma eq_zero_iff_mem {I : ideal α} : mk I a = 0 ↔ a ∈ I := by conv {to_rhs, rw ← sub_zero a }; exact quotient.eq' theorem zero_eq_one_iff {I : ideal α} : (0 : I.quotient) = 1 ↔ I = ⊤ := eq_comm.trans $ eq_zero_iff_mem.trans (eq_top_iff_one _).symm theorem zero_ne_one_iff {I : ideal α} : (0 : I.quotient) ≠ 1 ↔ I ≠ ⊤ := not_congr zero_eq_one_iff protected theorem nontrivial {I : ideal α} (hI : I ≠ ⊤) : nontrivial I.quotient := ⟨⟨0, 1, zero_ne_one_iff.2 hI⟩⟩ lemma mk_surjective : function.surjective (mk I) := λ y, quotient.induction_on' y (λ x, exists.intro x rfl) instance (I : ideal α) [hI : I.is_prime] : integral_domain I.quotient := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b, quotient.induction_on₂' a b $ λ a b hab, (hI.mem_or_mem (eq_zero_iff_mem.1 hab)).elim (or.inl ∘ eq_zero_iff_mem.2) (or.inr ∘ eq_zero_iff_mem.2), .. quotient.comm_ring I, .. quotient.nontrivial hI.1 } lemma is_integral_domain_iff_prime (I : ideal α) : is_integral_domain I.quotient ↔ I.is_prime := ⟨ λ ⟨h1, h2, h3⟩, ⟨zero_ne_one_iff.1 $ @zero_ne_one _ _ ⟨h1⟩, λ x y h, by { simp only [←eq_zero_iff_mem, (mk I).map_mul] at ⊢ h, exact h3 _ _ h}⟩, λ h, by exactI integral_domain.to_is_integral_domain I.quotient⟩ lemma exists_inv {I : ideal α} [hI : I.is_maximal] : ∀ {a : I.quotient}, a ≠ 0 → ∃ b : I.quotient, a * b = 1 := begin rintro ⟨a⟩ h, cases hI.exists_inv (mt eq_zero_iff_mem.2 h) with b hb, rw [mul_comm] at hb, exact ⟨mk _ b, quot.sound hb⟩ end /-- quotient by maximal ideal is a field. def rather than instance, since users will have computable inverses in some applications -/ protected noncomputable def field (I : ideal α) [hI : I.is_maximal] : field I.quotient := { inv := λ a, if ha : a = 0 then 0 else classical.some (exists_inv ha), mul_inv_cancel := λ a (ha : a ≠ 0), show a * dite _ _ _ = _, by rw dif_neg ha; exact classical.some_spec (exists_inv ha), inv_zero := dif_pos rfl, ..quotient.integral_domain I } /-- If the quotient by an ideal is a field, then the ideal is maximal. -/ theorem maximal_of_is_field (I : ideal α) (hqf : is_field I.quotient) : I.is_maximal := begin apply ideal.is_maximal_iff.2, split, { intro h, rcases hqf.exists_pair_ne with ⟨⟨x⟩, ⟨y⟩, hxy⟩, exact hxy (ideal.quotient.eq.2 (mul_one (x - y) ▸ I.mul_mem_left h)) }, { intros J x hIJ hxnI hxJ, rcases hqf.mul_inv_cancel (mt ideal.quotient.eq_zero_iff_mem.1 hxnI) with ⟨⟨y⟩, hy⟩, rw [← zero_add (1 : α), ← sub_self (x * y), sub_add], refine J.sub_mem (J.mul_mem_right hxJ) (hIJ (ideal.quotient.eq.1 hy)) } end /-- The quotient of a ring by an ideal is a field iff the ideal is maximal. -/ theorem maximal_ideal_iff_is_field_quotient (I : ideal α) : I.is_maximal ↔ is_field I.quotient := ⟨λ h, @field.to_is_field I.quotient (@ideal.quotient.field _ _ I h), λ h, maximal_of_is_field I h⟩ variable [comm_ring β] /-- Given a ring homomorphism `f : α →+* β` sending all elements of an ideal to zero, lift it to the quotient by this ideal. -/ def lift (S : ideal α) (f : α →+* β) (H : ∀ (a : α), a ∈ S → f a = 0) : quotient S →+* β := { to_fun := λ x, quotient.lift_on' x f $ λ (a b) (h : _ ∈ _), eq_of_sub_eq_zero $ by rw [← f.map_sub, H _ h], map_one' := f.map_one, map_zero' := f.map_zero, map_add' := λ a₁ a₂, quotient.induction_on₂' a₁ a₂ f.map_add, map_mul' := λ a₁ a₂, quotient.induction_on₂' a₁ a₂ f.map_mul } @[simp] lemma lift_mk (S : ideal α) (f : α →+* β) (H : ∀ (a : α), a ∈ S → f a = 0) : lift S f H (mk S a) = f a := rfl end quotient section lattice variables {R : Type u} [comm_ring R] lemma mem_sup_left {S T : ideal R} : ∀ {x : R}, x ∈ S → x ∈ S ⊔ T := show S ≤ S ⊔ T, from le_sup_left lemma mem_sup_right {S T : ideal R} : ∀ {x : R}, x ∈ T → x ∈ S ⊔ T := show T ≤ S ⊔ T, from le_sup_right lemma mem_supr_of_mem {ι : Type*} {S : ι → ideal R} (i : ι) : ∀ {x : R}, x ∈ S i → x ∈ supr S := show S i ≤ supr S, from le_supr _ _ lemma mem_Sup_of_mem {S : set (ideal R)} {s : ideal R} (hs : s ∈ S) : ∀ {x : R}, x ∈ s → x ∈ Sup S := show s ≤ Sup S, from le_Sup hs theorem mem_Inf {s : set (ideal R)} {x : R} : x ∈ Inf s ↔ ∀ ⦃I⦄, I ∈ s → x ∈ I := ⟨λ hx I his, hx I ⟨I, infi_pos his⟩, λ H I ⟨J, hij⟩, hij ▸ λ S ⟨hj, hS⟩, hS ▸ H hj⟩ @[simp] lemma mem_inf {I J : ideal R} {x : R} : x ∈ I ⊓ J ↔ x ∈ I ∧ x ∈ J := iff.rfl @[simp] lemma mem_infi {ι : Type*} {I : ι → ideal R} {x : R} : x ∈ infi I ↔ ∀ i, x ∈ I i := submodule.mem_infi _ end lattice /-- All ideals in a field are trivial. -/ lemma eq_bot_or_top {K : Type u} [field K] (I : ideal K) : I = ⊥ ∨ I = ⊤ := begin rw or_iff_not_imp_right, change _ ≠ _ → _, rw ideal.ne_top_iff_one, intro h1, rw eq_bot_iff, intros r hr, by_cases H : r = 0, {simpa}, simpa [H, h1] using submodule.smul_mem I r⁻¹ hr, end lemma eq_bot_of_prime {K : Type u} [field K] (I : ideal K) [h : I.is_prime] : I = ⊥ := or_iff_not_imp_right.mp I.eq_bot_or_top h.1 lemma bot_is_maximal {K : Type u} [field K] : is_maximal (⊥ : ideal K) := ⟨λ h, absurd ((eq_top_iff_one (⊤ : ideal K)).mp rfl) (by rw ← h; simp), λ I hI, or_iff_not_imp_left.mp (eq_bot_or_top I) (ne_of_gt hI)⟩ section pi variables (ι : Type v) /-- `I^n` as an ideal of `R^n`. -/ def pi : ideal (ι → α) := { carrier := { x | ∀ i, x i ∈ I }, zero_mem' := λ i, submodule.zero_mem _, add_mem' := λ a b ha hb i, submodule.add_mem _ (ha i) (hb i), smul_mem' := λ a b hb i, ideal.mul_mem_left _ (hb i) } lemma mem_pi (x : ι → α) : x ∈ I.pi ι ↔ ∀ i, x i ∈ I := iff.rfl /-- `R^n/I^n` is a `R/I`-module. -/ instance module_pi : module (I.quotient) (I.pi ι).quotient := begin refine { smul := λ c m, quotient.lift_on₂' c m (λ r m, submodule.quotient.mk $ r • m) _, .. }, { intros c₁ m₁ c₂ m₂ hc hm, change c₁ - c₂ ∈ I at hc, change m₁ - m₂ ∈ (I.pi ι) at hm, apply ideal.quotient.eq.2, have : c₁ • (m₂ - m₁) ∈ I.pi ι, { rw ideal.mem_pi, intro i, simp only [smul_eq_mul, pi.smul_apply, pi.sub_apply], apply ideal.mul_mem_left, rw ←ideal.neg_mem_iff, simpa only [neg_sub] using hm i }, rw [←ideal.add_mem_iff_left (I.pi ι) this, sub_eq_add_neg, add_comm, ←add_assoc, ←smul_add, sub_add_cancel, ←sub_eq_add_neg, ←sub_smul, ideal.mem_pi], exact λ i, ideal.mul_mem_right _ hc }, all_goals { rintro ⟨a⟩ ⟨b⟩ ⟨c⟩ <|> rintro ⟨a⟩, simp only [(•), submodule.quotient.quot_mk_eq_mk, ideal.quotient.mk_eq_mk], change ideal.quotient.mk _ _ = ideal.quotient.mk _ _, congr' with i, simp [mul_assoc, mul_add, add_mul] } end /-- `R^n/I^n` is isomorphic to `(R/I)^n` as an `R/I`-module. -/ noncomputable def pi_quot_equiv : (I.pi ι).quotient ≃ₗ[I.quotient] (ι → I.quotient) := { to_fun := λ x, quotient.lift_on' x (λ f i, ideal.quotient.mk I (f i)) $ λ a b hab, funext (λ i, ideal.quotient.eq.2 (hab i)), map_add' := by { rintros ⟨_⟩ ⟨_⟩, refl }, map_smul' := by { rintros ⟨_⟩ ⟨_⟩, refl }, inv_fun := λ x, ideal.quotient.mk (I.pi ι) $ λ i, quotient.out' (x i), left_inv := begin rintro ⟨x⟩, exact ideal.quotient.eq.2 (λ i, ideal.quotient.eq.1 (quotient.out_eq' _)) end, right_inv := begin intro x, ext i, obtain ⟨r, hr⟩ := @quot.exists_rep _ _ (x i), simp_rw ←hr, convert quotient.out_eq' _ end } /-- If `f : R^n → R^m` is an `R`-linear map and `I ⊆ R` is an ideal, then the image of `I^n` is contained in `I^m`. -/ lemma map_pi {ι} [fintype ι] {ι' : Type w} (x : ι → α) (hi : ∀ i, x i ∈ I) (f : (ι → α) →ₗ[α] (ι' → α)) (i : ι') : f x i ∈ I := begin rw pi_eq_sum_univ x, simp only [finset.sum_apply, smul_eq_mul, linear_map.map_sum, pi.smul_apply, linear_map.map_smul], exact submodule.sum_mem _ (λ j hj, ideal.mul_mem_right _ (hi j)) end end pi end ideal namespace ring variables {R : Type*} [comm_ring R] lemma not_is_field_of_subsingleton {R : Type*} [ring R] [subsingleton R] : ¬ is_field R := λ ⟨⟨x, y, hxy⟩, _, _⟩, hxy (subsingleton.elim x y) lemma exists_not_is_unit_of_not_is_field [nontrivial R] (hf : ¬ is_field R) : ∃ x ≠ (0 : R), ¬ is_unit x := begin have : ¬ _ := λ h, hf ⟨exists_pair_ne R, mul_comm, h⟩, simp_rw is_unit_iff_exists_inv, push_neg at ⊢ this, obtain ⟨x, hx, not_unit⟩ := this, exact ⟨x, hx, not_unit⟩ end lemma not_is_field_iff_exists_ideal_bot_lt_and_lt_top [nontrivial R] : ¬ is_field R ↔ ∃ I : ideal R, ⊥ < I ∧ I < ⊤ := begin split, { intro h, obtain ⟨x, nz, nu⟩ := exists_not_is_unit_of_not_is_field h, use ideal.span {x}, rw [bot_lt_iff_ne_bot, lt_top_iff_ne_top], exact ⟨mt ideal.span_singleton_eq_bot.mp nz, mt ideal.span_singleton_eq_top.mp nu⟩ }, { rintros ⟨I, bot_lt, lt_top⟩ hf, obtain ⟨x, mem, ne_zero⟩ := submodule.exists_of_lt bot_lt, rw submodule.mem_bot at ne_zero, obtain ⟨y, hy⟩ := hf.mul_inv_cancel ne_zero, rw [lt_top_iff_ne_top, ne.def, ideal.eq_top_iff_one, ← hy] at lt_top, exact lt_top (ideal.mul_mem_right _ mem), } end lemma not_is_field_iff_exists_prime [nontrivial R] : ¬ is_field R ↔ ∃ p : ideal R, p ≠ ⊥ ∧ p.is_prime := not_is_field_iff_exists_ideal_bot_lt_and_lt_top.trans ⟨λ ⟨I, bot_lt, lt_top⟩, let ⟨p, hp, le_p⟩ := I.exists_le_maximal (lt_top_iff_ne_top.mp lt_top) in ⟨p, bot_lt_iff_ne_bot.mp (lt_of_lt_of_le bot_lt le_p), hp.is_prime⟩, λ ⟨p, ne_bot, prime⟩, ⟨p, bot_lt_iff_ne_bot.mpr ne_bot, lt_top_iff_ne_top.mpr prime.1⟩⟩ end ring namespace ideal /-- Maximal ideals in a non-field are nontrivial. -/ variables {R : Type u} [comm_ring R] [nontrivial R] lemma bot_lt_of_maximal (M : ideal R) [hm : M.is_maximal] (non_field : ¬ is_field R) : ⊥ < M := begin rcases (ring.not_is_field_iff_exists_ideal_bot_lt_and_lt_top.1 non_field) with ⟨I, Ibot, Itop⟩, split, finish, intro mle, apply @irrefl _ (<) _ (⊤ : ideal R), have : M = ⊥ := eq_bot_iff.mpr mle, rw this at *, rwa hm.2 I Ibot at Itop, end end ideal /-- The set of non-invertible elements of a monoid. -/ def nonunits (α : Type u) [monoid α] : set α := { a | ¬is_unit a } @[simp] theorem mem_nonunits_iff [comm_monoid α] : a ∈ nonunits α ↔ ¬ is_unit a := iff.rfl theorem mul_mem_nonunits_right [comm_monoid α] : b ∈ nonunits α → a * b ∈ nonunits α := mt is_unit_of_mul_is_unit_right theorem mul_mem_nonunits_left [comm_monoid α] : a ∈ nonunits α → a * b ∈ nonunits α := mt is_unit_of_mul_is_unit_left theorem zero_mem_nonunits [semiring α] : 0 ∈ nonunits α ↔ (0:α) ≠ 1 := not_congr is_unit_zero_iff @[simp] theorem one_not_mem_nonunits [monoid α] : (1:α) ∉ nonunits α := not_not_intro is_unit_one theorem coe_subset_nonunits [comm_ring α] {I : ideal α} (h : I ≠ ⊤) : (I : set α) ⊆ nonunits α := λ x hx hu, h $ I.eq_top_of_is_unit_mem hx hu lemma exists_max_ideal_of_mem_nonunits [comm_ring α] (h : a ∈ nonunits α) : ∃ I : ideal α, I.is_maximal ∧ a ∈ I := begin have : ideal.span ({a} : set α) ≠ ⊤, { intro H, rw ideal.span_singleton_eq_top at H, contradiction }, rcases ideal.exists_le_maximal _ this with ⟨I, Imax, H⟩, use [I, Imax], apply H, apply ideal.subset_span, exact set.mem_singleton a end /-- A commutative ring is local if it has a unique maximal ideal. Note that `local_ring` is a predicate. -/ class local_ring (α : Type u) [comm_ring α] extends nontrivial α : Prop := (is_local : ∀ (a : α), (is_unit a) ∨ (is_unit (1 - a))) namespace local_ring variables [comm_ring α] [local_ring α] lemma is_unit_or_is_unit_one_sub_self (a : α) : (is_unit a) ∨ (is_unit (1 - a)) := is_local a lemma is_unit_of_mem_nonunits_one_sub_self (a : α) (h : (1 - a) ∈ nonunits α) : is_unit a := or_iff_not_imp_right.1 (is_local a) h lemma is_unit_one_sub_self_of_mem_nonunits (a : α) (h : a ∈ nonunits α) : is_unit (1 - a) := or_iff_not_imp_left.1 (is_local a) h lemma nonunits_add {x y} (hx : x ∈ nonunits α) (hy : y ∈ nonunits α) : x + y ∈ nonunits α := begin rintros ⟨u, hu⟩, apply hy, suffices : is_unit ((↑u⁻¹ : α) * y), { rcases this with ⟨s, hs⟩, use u * s, convert congr_arg (λ z, (u : α) * z) hs, rw ← mul_assoc, simp }, rw show (↑u⁻¹ * y) = (1 - ↑u⁻¹ * x), { rw eq_sub_iff_add_eq, replace hu := congr_arg (λ z, (↑u⁻¹ : α) * z) hu.symm, simpa [mul_add, add_comm] using hu }, apply is_unit_one_sub_self_of_mem_nonunits, exact mul_mem_nonunits_right hx end variable (α) /-- The ideal of elements that are not units. -/ def maximal_ideal : ideal α := { carrier := nonunits α, zero_mem' := zero_mem_nonunits.2 $ zero_ne_one, add_mem' := λ x y hx hy, nonunits_add hx hy, smul_mem' := λ a x, mul_mem_nonunits_right } instance maximal_ideal.is_maximal : (maximal_ideal α).is_maximal := begin rw ideal.is_maximal_iff, split, { intro h, apply h, exact is_unit_one }, { intros I x hI hx H, erw not_not at hx, rcases hx with ⟨u,rfl⟩, simpa using I.smul_mem ↑u⁻¹ H } end lemma maximal_ideal_unique : ∃! I : ideal α, I.is_maximal := ⟨maximal_ideal α, maximal_ideal.is_maximal α, λ I hI, hI.eq_of_le (maximal_ideal.is_maximal α).1 $ λ x hx, hI.1 ∘ I.eq_top_of_is_unit_mem hx⟩ variable {α} lemma eq_maximal_ideal {I : ideal α} (hI : I.is_maximal) : I = maximal_ideal α := unique_of_exists_unique (maximal_ideal_unique α) hI $ maximal_ideal.is_maximal α lemma le_maximal_ideal {J : ideal α} (hJ : J ≠ ⊤) : J ≤ maximal_ideal α := begin rcases ideal.exists_le_maximal J hJ with ⟨M, hM1, hM2⟩, rwa ←eq_maximal_ideal hM1 end @[simp] lemma mem_maximal_ideal (x) : x ∈ maximal_ideal α ↔ x ∈ nonunits α := iff.rfl end local_ring lemma local_of_nonunits_ideal [comm_ring α] (hnze : (0:α) ≠ 1) (h : ∀ x y ∈ nonunits α, x + y ∈ nonunits α) : local_ring α := { exists_pair_ne := ⟨0, 1, hnze⟩, is_local := λ x, or_iff_not_imp_left.mpr $ λ hx, begin by_contra H, apply h _ _ hx H, simp [-sub_eq_add_neg, add_sub_cancel'_right] end } lemma local_of_unique_max_ideal [comm_ring α] (h : ∃! I : ideal α, I.is_maximal) : local_ring α := local_of_nonunits_ideal (let ⟨I, Imax, _⟩ := h in (λ (H : 0 = 1), Imax.1 $ I.eq_top_iff_one.2 $ H ▸ I.zero_mem)) $ λ x y hx hy H, let ⟨I, Imax, Iuniq⟩ := h in let ⟨Ix, Ixmax, Hx⟩ := exists_max_ideal_of_mem_nonunits hx in let ⟨Iy, Iymax, Hy⟩ := exists_max_ideal_of_mem_nonunits hy in have xmemI : x ∈ I, from ((Iuniq Ix Ixmax) ▸ Hx), have ymemI : y ∈ I, from ((Iuniq Iy Iymax) ▸ Hy), Imax.1 $ I.eq_top_of_is_unit_mem (I.add_mem xmemI ymemI) H lemma local_of_unique_nonzero_prime (R : Type u) [comm_ring R] (h : ∃! P : ideal R, P ≠ ⊥ ∧ ideal.is_prime P) : local_ring R := local_of_unique_max_ideal begin rcases h with ⟨P, ⟨hPnonzero, hPnot_top, _⟩, hPunique⟩, refine ⟨P, ⟨hPnot_top, _⟩, λ M hM, hPunique _ ⟨_, ideal.is_maximal.is_prime hM⟩⟩, { refine ideal.maximal_of_no_maximal (λ M hPM hM, ne_of_lt hPM _), exact (hPunique _ ⟨ne_bot_of_gt hPM, ideal.is_maximal.is_prime hM⟩).symm }, { rintro rfl, exact hPnot_top (hM.2 P (bot_lt_iff_ne_bot.2 hPnonzero)) }, end lemma local_of_surjective {A B : Type*} [comm_ring A] [local_ring A] [comm_ring B] [nontrivial B] (f : A →+* B) (hf : function.surjective f) : local_ring B := { is_local := begin intros b, obtain ⟨a, rfl⟩ := hf b, apply (local_ring.is_unit_or_is_unit_one_sub_self a).imp f.is_unit_map _, rw [← f.map_one, ← f.map_sub], apply f.is_unit_map, end, .. ‹nontrivial B› } /-- A local ring homomorphism is a homomorphism between local rings such that the image of the maximal ideal of the source is contained within the maximal ideal of the target. -/ class is_local_ring_hom [semiring α] [semiring β] (f : α →+* β) : Prop := (map_nonunit : ∀ a, is_unit (f a) → is_unit a) instance is_local_ring_hom_id (A : Type*) [semiring A] : is_local_ring_hom (ring_hom.id A) := { map_nonunit := λ a, id } @[simp] lemma is_unit_map_iff {A B : Type*} [semiring A] [semiring B] (f : A →+* B) [is_local_ring_hom f] (a) : is_unit (f a) ↔ is_unit a := ⟨is_local_ring_hom.map_nonunit a, f.is_unit_map⟩ instance is_local_ring_hom_comp {A B C : Type*} [semiring A] [semiring B] [semiring C] (g : B →+* C) (f : A →+* B) [is_local_ring_hom g] [is_local_ring_hom f] : is_local_ring_hom (g.comp f) := { map_nonunit := λ a, is_local_ring_hom.map_nonunit a ∘ is_local_ring_hom.map_nonunit (f a) } @[simp] lemma is_unit_of_map_unit [semiring α] [semiring β] (f : α →+* β) [is_local_ring_hom f] (a) (h : is_unit (f a)) : is_unit a := is_local_ring_hom.map_nonunit a h theorem of_irreducible_map [semiring α] [semiring β] (f : α →+* β) [h : is_local_ring_hom f] {x : α} (hfx : irreducible (f x)) : irreducible x := ⟨λ h, hfx.1 $ is_unit.map f.to_monoid_hom h, λ p q hx, let ⟨H⟩ := h in or.imp (H p) (H q) $ hfx.2 _ _ $ f.map_mul p q ▸ congr_arg f hx⟩ section open local_ring variables [comm_ring α] [local_ring α] [comm_ring β] [local_ring β] variables (f : α →+* β) [is_local_ring_hom f] lemma map_nonunit (a) (h : a ∈ maximal_ideal α) : f a ∈ maximal_ideal β := λ H, h $ is_unit_of_map_unit f a H end namespace local_ring variables [comm_ring α] [local_ring α] [comm_ring β] [local_ring β] variable (α) /-- The residue field of a local ring is the quotient of the ring by its maximal ideal. -/ def residue_field := (maximal_ideal α).quotient noncomputable instance residue_field.field : field (residue_field α) := ideal.quotient.field (maximal_ideal α) noncomputable instance : inhabited (residue_field α) := ⟨37⟩ /-- The quotient map from a local ring to its residue field. -/ def residue : α →+* (residue_field α) := ideal.quotient.mk _ namespace residue_field variables {α β} /-- The map on residue fields induced by a local homomorphism between local rings -/ noncomputable def map (f : α →+* β) [is_local_ring_hom f] : residue_field α →+* residue_field β := ideal.quotient.lift (maximal_ideal α) ((ideal.quotient.mk _).comp f) $ λ a ha, begin erw ideal.quotient.eq_zero_iff_mem, exact map_nonunit f a ha end end residue_field end local_ring namespace field variables [field α] @[priority 100] -- see Note [lower instance priority] instance : local_ring α := { is_local := λ a, if h : a = 0 then or.inr (by rw [h, sub_zero]; exact is_unit_one) else or.inl $ is_unit_of_mul_eq_one a a⁻¹ $ div_self h } end field
3e043c5fce474e5e3735b854d7c1dc8e9d420f78
947b78d97130d56365ae2ec264df196ce769371a
/src/Lean/Elab/DeclModifiers.lean
0bab18eabd1d5fc27db863db78abeadf6569438e
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,633
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Modifiers import Lean.Elab.Attributes import Lean.Elab.Exception import Lean.Elab.DeclUtil namespace Lean namespace Elab def checkNotAlreadyDeclared {m} [Monad m] [MonadEnv m] [MonadExceptOf Exception m] [Ref m] [AddErrorMessageContext m] (declName : Name) : m Unit := do env ← getEnv; when (env.contains declName) $ match privateToUserName? declName with | none => throwError ("'" ++ declName ++ "' has already been declared") | some declName => throwError ("private declaration '" ++ declName ++ "' has already been declared"); when (env.contains (mkPrivateName env declName)) $ throwError ("a private declaration '" ++ declName ++ "' has already been declared"); match privateToUserName? declName with | none => pure () | some declName => when (env.contains declName) $ throwError ("a non-private declaration '" ++ declName ++ "' has already been declared") inductive Visibility | regular | «protected» | «private» instance Visibility.hasToString : HasToString Visibility := ⟨fun v => match v with | Visibility.regular => "regular" | Visibility.private => "private" | Visibility.protected => "protected"⟩ structure Modifiers := (docString : Option String := none) (visibility : Visibility := Visibility.regular) (isNoncomputable : Bool := false) (isPartial : Bool := false) (isUnsafe : Bool := false) (attrs : Array Attribute := #[]) def Modifiers.isPrivate : Modifiers → Bool | { visibility := Visibility.private, .. } => true | _ => false def Modifiers.isProtected : Modifiers → Bool | { visibility := Visibility.protected, .. } => true | _ => false def Modifiers.addAttribute (modifiers : Modifiers) (attr : Attribute) : Modifiers := { modifiers with attrs := modifiers.attrs.push attr } instance Modifiers.hasFormat : HasFormat Modifiers := ⟨fun m => let components : List Format := (match m.docString with | some str => ["/--" ++ str ++ "-/"] | none => []) ++ (match m.visibility with | Visibility.regular => [] | Visibility.protected => ["protected"] | Visibility.private => ["private"]) ++ (if m.isNoncomputable then ["noncomputable"] else []) ++ (if m.isPartial then ["partial"] else []) ++ (if m.isUnsafe then ["unsafe"] else []) ++ m.attrs.toList.map (fun attr => fmt attr); Format.bracket "{" (Format.joinSep components ("," ++ Format.line)) "}"⟩ instance Modifiers.hasToString : HasToString Modifiers := ⟨toString ∘ format⟩ section Methods variables {m : Type → Type} [Monad m] [MonadEnv m] [MonadExceptOf Exception m] [Ref m] [AddErrorMessageContext m] def elabModifiers (stx : Syntax) : m Modifiers := do let docCommentStx := stx.getArg 0; let attrsStx := stx.getArg 1; let visibilityStx := stx.getArg 2; let noncompStx := stx.getArg 3; let unsafeStx := stx.getArg 4; let partialStx := stx.getArg 5; docString ← match docCommentStx.getOptional? with | none => pure none | some s => match s.getArg 1 with | Syntax.atom _ val => pure (some (val.extract 0 (val.bsize - 2))) | _ => throwErrorAt s ("unexpected doc string " ++ toString (s.getArg 1)); visibility ← match visibilityStx.getOptional? with | none => pure Visibility.regular | some v => let kind := v.getKind; if kind == `Lean.Parser.Command.private then pure Visibility.private else if kind == `Lean.Parser.Command.protected then pure Visibility.protected else throwErrorAt v "unexpected visibility modifier"; attrs ← match attrsStx.getOptional? with | none => pure #[] | some attrs => elabDeclAttrs attrs; pure { docString := docString, visibility := visibility, isPartial := !partialStx.isNone, isUnsafe := !unsafeStx.isNone, isNoncomputable := !noncompStx.isNone, attrs := attrs } def applyVisibility (visibility : Visibility) (declName : Name) : m Name := match visibility with | Visibility.private => do env ← getEnv; let declName := mkPrivateName env declName; checkNotAlreadyDeclared declName; pure declName | Visibility.protected => do checkNotAlreadyDeclared declName; env ← getEnv; let env := addProtected env declName; setEnv env; pure declName | _ => do checkNotAlreadyDeclared declName; pure declName def mkDeclName (currNamespace : Name) (modifiers : Modifiers) (shortName : Name) : m (Name × Name) := do let name := (extractMacroScopes shortName).name; unless (name.isAtomic || isFreshInstanceName name) $ throwError ("atomic identifier expected '" ++ shortName ++ "'"); let declName := currNamespace ++ shortName; declName ← applyVisibility modifiers.visibility declName; match modifiers.visibility with | Visibility.protected => match currNamespace with | Name.str _ s _ => pure (declName, mkNameSimple s ++ shortName) | _ => throwError ("protected declarations must be in a namespace") | _ => pure (declName, shortName) /- `declId` is of the form ``` parser! ident >> optional (".{" >> sepBy1 ident ", " >> "}") ``` but we also accept a single identifier to users to make macro writing more convenient . -/ def expandDeclIdCore (declId : Syntax) : Name × Syntax := if declId.isIdent then (declId.getId, mkNullNode) else let id := declId.getIdAt 0; let optUnivDeclStx := declId.getArg 1; (id, optUnivDeclStx) structure ExpandDeclIdResult := (shortName : Name) (declName : Name) (levelNames : List Name) def expandDeclId (currNamespace : Name) (currLevelNames : List Name) (declId : Syntax) (modifiers : Modifiers) : m ExpandDeclIdResult := do -- ident >> optional (".{" >> sepBy1 ident ", " >> "}") let (shortName, optUnivDeclStx) := expandDeclIdCore declId; levelNames ← if optUnivDeclStx.isNone then pure currLevelNames else do { let extraLevels := (optUnivDeclStx.getArg 1).getArgs.getEvenElems; extraLevels.foldlM (fun levelNames idStx => let id := idStx.getId; if levelNames.elem id then withRef idStx $ throwAlreadyDeclaredUniverseLevel id else pure (id :: levelNames)) currLevelNames }; (declName, shortName) ← withRef declId $ mkDeclName currNamespace modifiers shortName; pure { shortName := shortName, declName := declName, levelNames := levelNames } end Methods end Elab end Lean
3fa076b712d9f71d59e5033f885a44a47d49ab4f
a721fe7446524f18ba361625fc01033d9c8b7a78
/elaborate/add_comm_2.lean
5113d5434a7cbbecf5e7709c737b3911a0637b69
[]
no_license
Sterrs/leaning
8fd80d1f0a6117a220bb2e57ece639b9a63deadc
3901cc953694b33adda86cb88ca30ba99594db31
refs/heads/master
1,627,023,822,744
1,616,515,221,000
1,616,515,221,000
245,512,190
2
0
null
1,616,429,050,000
1,583,527,118,000
Lean
UTF-8
Lean
false
false
2,343
lean
λ (m n : mynat), (mynat.rec ⟨λ (m : mynat), eq.rec (eq.refl m) (eq.rec (eq.refl (m = add zero m)) (eq.rec (eq.refl (m = add zero m)) ((mynat.rec ⟨eq.refl zero, punit.star⟩ (λ (n : mynat) (ih : pprod (add zero n = n) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (add zero n = n) ih) punit) n)), ⟨eq.rec (eq.refl (succ n)) (eq.rec (eq.refl (succ (add zero n) = succ n)) (eq.rec (eq.refl (succ (add zero n) = succ n)) (ih.fst))), ⟨ih, punit.star⟩⟩) m).fst))), punit.star⟩ (λ (n : mynat) (ih : pprod (∀ (m : mynat), add m n = add n m) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (∀ (m : mynat), add m n = add n m) ih) punit) n)), ⟨λ (m : mynat), eq.rec (eq.rec (eq.refl (succ (add n m))) (eq.rec (eq.refl (succ (add m n) = succ (add n m))) (eq.rec (eq.refl (succ (add m n) = succ (add n m))) (ih.fst m)))) (eq.rec (eq.refl (succ (add m n) = add (succ n) m)) (eq.rec (eq.refl (succ (add m n) = add (succ n) m)) ((mynat.rec ⟨λ (m : mynat), eq.refl (succ m), punit.star⟩ (λ (n : mynat) (ih : pprod (∀ (m : mynat), add (succ m) n = succ (add m n)) (mynat.rec punit (λ (n : mynat) (ih : Type), pprod (pprod (∀ (m : mynat), add (succ m) n = succ (add m n)) ih) punit) n)), ⟨λ (m : mynat), eq.rec (eq.refl (succ (succ (add m n)))) (eq.rec (eq.refl (succ (add (succ m) n) = succ (succ (add m n)))) (eq.rec (eq.refl (succ (add (succ m) n) = succ (succ (add m n)))) (ih.fst m))), ⟨ih, punit.star⟩⟩) m).fst n))), ⟨ih, punit.star⟩⟩) n).fst m
92c9e3d48a0c3068bc226c8a160d0104a7869720
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/monad/monadicity.lean
be665e374b78a211c241074abd8e994f3d0fb9ee
[ "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
14,985
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import category_theory.limits.shapes.reflexive import category_theory.limits.preserves.limits import category_theory.monad.limits import category_theory.monad.coequalizer /-! # Monadicity theorems We prove monadicity theorems which can establish a given functor is monadic. In particular, we show three versions of Beck's monadicity theorem, and the reflexive (crude) monadicity theorem: `G` is a monadic right adjoint if it has a right adjoint, and: * `D` has, `G` preserves and reflects `G`-split coequalizers, see `category_theory.monad.monadic_of_has_preserves_reflects_G_split_coequalizers` * `G` creates `G`-split coequalizers, see `category_theory.monad.monadic_of_creates_G_split_coequalizers` (The converse of this is also shown, see `category_theory.monad.creates_G_split_coequalizers_of_monadic`) * `D` has and `G` preserves `G`-split coequalizers, and `G` reflects isomorphisms, see `category_theory.monad.monadic_of_has_preserves_G_split_coequalizers_of_reflects_isomorphisms` * `D` has and `G` preserves reflexive coequalizers, and `G` reflects isomorphisms, see `category_theory.monad.monadic_of_has_preserves_reflexive_coequalizers_of_reflects_isomorphisms` ## Tags Beck, monadicity, descent ## TODO Dualise to show comonadicity theorems. -/ universes v₁ v₂ u₁ u₂ namespace category_theory namespace monad open limits noncomputable theory -- Hide the implementation details in this namespace. namespace monadicity_internal section -- We use these parameters and notations to simplify the statements of internal constructions -- here. parameters {C : Type u₁} {D : Type u₂} parameters [category.{v₁} C] [category.{v₁} D] parameters {G : D ⥤ C} [is_right_adjoint G] -- An unfortunate consequence of the local notation is that it is only recognised if there is an -- extra space after the reference. local notation `F` := left_adjoint G local notation `adj` := adjunction.of_right_adjoint G /-- The "main pair" for an algebra `(A, α)` is the pair of morphisms `(F α, ε_FA)`. It is always a reflexive pair, and will be used to construct the left adjoint to the comparison functor and show it is an equivalence. -/ instance main_pair_reflexive (A : adj .to_monad.algebra) : is_reflexive_pair (F .map A.a) (adj .counit.app (F .obj A.A)) := begin apply is_reflexive_pair.mk' (F .map (adj .unit.app _)) _ _, { rw [← F .map_comp, ← F .map_id], exact congr_arg (λ _, F .map _) A.unit }, { rw adj .left_triangle_components, refl }, end /-- The "main pair" for an algebra `(A, α)` is the pair of morphisms `(F α, ε_FA)`. It is always a `G`-split pair, and will be used to construct the left adjoint to the comparison functor and show it is an equivalence. -/ instance main_pair_G_split (A : adj .to_monad.algebra) : G.is_split_pair (F .map A.a) (adj .counit.app (F .obj A.A)) := { splittable := ⟨_, _, ⟨beck_split_coequalizer A⟩⟩ } /-- The object function for the left adjoint to the comparison functor. -/ def comparison_left_adjoint_obj (A : adj .to_monad.algebra) [has_coequalizer (F .map A.a) (adj .counit.app _)] : D := coequalizer (F .map A.a) (adj .counit.app _) /-- We have a bijection of homsets which will be used to construct the left adjoint to the comparison functor. -/ @[simps] def comparison_left_adjoint_hom_equiv (A : adj .to_monad.algebra) (B : D) [has_coequalizer (F .map A.a) (adj .counit.app (F .obj A.A))] : (comparison_left_adjoint_obj A ⟶ B) ≃ (A ⟶ (comparison adj).obj B) := calc (comparison_left_adjoint_obj A ⟶ B) ≃ {f : F .obj A.A ⟶ B // _} : cofork.is_colimit.hom_iso (colimit.is_colimit _) B ... ≃ {g : A.A ⟶ G.obj B // G.map (F .map g) ≫ G.map (adj .counit.app B) = A.a ≫ g} : begin refine (adj .hom_equiv _ _).subtype_equiv _, intro f, rw [← (adj .hom_equiv _ _).injective.eq_iff, adjunction.hom_equiv_naturality_left, adj .hom_equiv_unit, adj .hom_equiv_unit, G.map_comp], dsimp, rw [adj .right_triangle_components_assoc, ← G.map_comp, F .map_comp, category.assoc, adj .counit_naturality, adj .left_triangle_components_assoc], apply eq_comm, end ... ≃ (A ⟶ (comparison adj).obj B) : { to_fun := λ g, { f := _, h' := g.prop }, inv_fun := λ f, ⟨f.f, f.h⟩, left_inv := λ g, begin ext, refl end, right_inv := λ f, begin ext, refl end } /-- Construct the adjunction to the comparison functor. -/ def left_adjoint_comparison [∀ (A : adj .to_monad.algebra), has_coequalizer (F .map A.a) (adj .counit.app (F .obj A.A))] : adj .to_monad.algebra ⥤ D := begin refine @adjunction.left_adjoint_of_equiv _ _ _ _ (comparison adj) (λ A, comparison_left_adjoint_obj A) (λ A B, _) _, { apply comparison_left_adjoint_hom_equiv }, { intros A B B' g h, ext1, dsimp [comparison_left_adjoint_hom_equiv], rw [← adj .hom_equiv_naturality_right, category.assoc] }, end /-- Provided we have the appropriate coequalizers, we have an adjunction to the comparison functor. -/ @[simps counit] def comparison_adjunction [∀ (A : adj .to_monad.algebra), has_coequalizer (F .map A.a) (adj .counit.app (F .obj A.A))] : left_adjoint_comparison ⊣ comparison adj := adjunction.adjunction_of_equiv_left _ _ lemma comparison_adjunction_unit_f_aux [∀ (A : adj .to_monad.algebra), has_coequalizer (F .map A.a) (adj .counit.app (F .obj A.A))] (A : adj .to_monad.algebra) : (comparison_adjunction.unit.app A).f = adj .hom_equiv A.A _ (coequalizer.π (F .map A.a) (adj .counit.app (F .obj A.A))) := congr_arg (adj .hom_equiv _ _) (category.comp_id _) /-- This is a cofork which is helpful for establishing monadicity: the morphism from the Beck coequalizer to this cofork is the unit for the adjunction on the comparison functor. -/ @[simps] def unit_cofork (A : adj .to_monad.algebra) [has_coequalizer (F .map A.a) (adj .counit.app (F .obj A.A))] : cofork (G.map (F .map A.a)) (G.map (adj .counit.app (F .obj A.A))) := cofork.of_π (G.map (coequalizer.π (F .map A.a) (adj .counit.app (F .obj A.A)))) begin change _ = G.map _ ≫ _, rw [← G.map_comp, coequalizer.condition, G.map_comp], end lemma comparison_adjunction_unit_f [∀ (A : adj .to_monad.algebra), has_coequalizer (F .map A.a) (adj .counit.app (F .obj A.A))] (A : adj .to_monad.algebra) : (comparison_adjunction.unit.app A).f = (beck_coequalizer A).desc (unit_cofork A) := begin apply limits.cofork.is_colimit.hom_ext (beck_coequalizer A), rw is_colimit.fac, dsimp only [cofork.π_eq_app_one, beck_cofork_ι_app, unit_cofork_ι_app], rw [comparison_adjunction_unit_f_aux, ← adj .hom_equiv_naturality_left A.a, coequalizer.condition, adj .hom_equiv_naturality_right, adj .hom_equiv_unit, category.assoc], apply adj .right_triangle_components_assoc, end /-- The cofork which describes the counit of the adjunction: the morphism from the coequalizer of this pair to this morphism is the counit. -/ @[simps] def counit_cofork (B : D) : cofork (F .map (G.map (adj .counit.app B))) (adj .counit.app (F .obj (G.obj B))) := cofork.of_π (adj .counit.app B) (adj .counit_naturality _) /-- The unit cofork is a colimit provided `G` preserves it. -/ def unit_colimit_of_preserves_coequalizer (A : adj .to_monad.algebra) [has_coequalizer (F .map A.a) (adj .counit.app (F .obj A.A))] [preserves_colimit (parallel_pair (F .map A.a) (adj .counit.app (F .obj A.A))) G] : is_colimit (unit_cofork A) := is_colimit_of_has_coequalizer_of_preserves_colimit G _ _ /-- The counit cofork is a colimit provided `G` reflects it. -/ def counit_coequalizer_of_reflects_coequalizer (B : D) [reflects_colimit (parallel_pair (F .map (G.map (adj .counit.app B))) (adj .counit.app (F .obj (G.obj B)))) G] : is_colimit (counit_cofork B) := is_colimit_of_is_colimit_cofork_map G _ (beck_coequalizer ((comparison adj).obj B)) lemma comparison_adjunction_counit_app [∀ (A : adj .to_monad.algebra), has_coequalizer (F .map A.a) (adj .counit.app (F .obj A.A))] (B : D) : comparison_adjunction.counit.app B = colimit.desc _ (counit_cofork B) := begin apply coequalizer.hom_ext, change coequalizer.π _ _ ≫ coequalizer.desc ((adj .hom_equiv _ B).symm (𝟙 _)) _ = coequalizer.π _ _ ≫ coequalizer.desc _ _, simp, end end end monadicity_internal open category_theory.adjunction open monadicity_internal variables {C : Type u₁} {D : Type u₂} variables [category.{v₁} C] [category.{v₁} D] variables (G : D ⥤ C) /-- If `G` is monadic, it creates colimits of `G`-split pairs. This is the "boring" direction of Beck's monadicity theorem, the converse is given in `monadic_of_creates_G_split_coequalizers`. -/ def creates_G_split_coequalizers_of_monadic [monadic_right_adjoint G] ⦃A B⦄ (f g : A ⟶ B) [G.is_split_pair f g] : creates_colimit (parallel_pair f g) G := begin apply monadic_creates_colimit_of_preserves_colimit _ _, apply_instance, { apply preserves_colimit_of_iso_diagram _ (diagram_iso_parallel_pair _).symm, dsimp, apply_instance }, { apply preserves_colimit_of_iso_diagram _ (diagram_iso_parallel_pair _).symm, dsimp, apply_instance } end variables [is_right_adjoint G] section beck_monadicity /-- To show `G` is a monadic right adjoint, we can show it preserves and reflects `G`-split coequalizers, and `C` has them. -/ def monadic_of_has_preserves_reflects_G_split_coequalizers [∀ ⦃A B⦄ (f g : A ⟶ B) [G.is_split_pair f g], has_coequalizer f g] [∀ ⦃A B⦄ (f g : A ⟶ B) [G.is_split_pair f g], preserves_colimit (parallel_pair f g) G] [∀ ⦃A B⦄ (f g : A ⟶ B) [G.is_split_pair f g], reflects_colimit (parallel_pair f g) G] : monadic_right_adjoint G := begin let L : (adjunction.of_right_adjoint G).to_monad.algebra ⥤ D := left_adjoint_comparison, letI i : is_right_adjoint (comparison (of_right_adjoint G)) := ⟨_, comparison_adjunction⟩, constructor, let : Π (X : (of_right_adjoint G).to_monad.algebra), is_iso ((of_right_adjoint (comparison (of_right_adjoint G))).unit.app X), { intro X, apply is_iso_of_reflects_iso _ (monad.forget (of_right_adjoint G).to_monad), { change is_iso (comparison_adjunction.unit.app X).f, rw comparison_adjunction_unit_f, change is_iso (is_colimit.cocone_point_unique_up_to_iso (beck_coequalizer X) (unit_colimit_of_preserves_coequalizer X)).hom, refine is_iso.of_iso (is_colimit.cocone_point_unique_up_to_iso _ _) } }, let : Π (Y : D), is_iso ((of_right_adjoint (comparison (of_right_adjoint G))).counit.app Y), { intro Y, change is_iso (comparison_adjunction.counit.app Y), rw comparison_adjunction_counit_app, change is_iso (is_colimit.cocone_point_unique_up_to_iso _ _).hom, apply_instance, apply counit_coequalizer_of_reflects_coequalizer _, letI : G.is_split_pair ((left_adjoint G).map (G.map ((adjunction.of_right_adjoint G).counit.app Y))) ((adjunction.of_right_adjoint G).counit.app ((left_adjoint G).obj (G.obj Y))) := monadicity_internal.main_pair_G_split ((comparison (adjunction.of_right_adjoint G)).obj Y), apply_instance }, exactI adjunction.is_right_adjoint_to_is_equivalence, end /-- Beck's monadicity theorem. If `G` has a right adjoint and creates coequalizers of `G`-split pairs, then it is monadic. This is the converse of `creates_G_split_of_monadic`. -/ def monadic_of_creates_G_split_coequalizers [∀ ⦃A B⦄ (f g : A ⟶ B) [G.is_split_pair f g], creates_colimit (parallel_pair f g) G] : monadic_right_adjoint G := begin letI : ∀ ⦃A B⦄ (f g : A ⟶ B) [G.is_split_pair f g], has_colimit (parallel_pair f g ⋙ G), { introsI A B f g i, apply has_colimit_of_iso (diagram_iso_parallel_pair _), change has_coequalizer (G.map f) (G.map g), apply_instance }, apply monadic_of_has_preserves_reflects_G_split_coequalizers _, { apply_instance }, { introsI A B f g i, apply has_colimit_of_created (parallel_pair f g) G }, { introsI A B f g i, apply_instance }, { introsI A B f g i, apply_instance } end /-- An alternate version of Beck's monadicity theorem. If `G` reflects isomorphisms, preserves coequalizers of `G`-split pairs and `C` has coequalizers of `G`-split pairs, then it is monadic. -/ def monadic_of_has_preserves_G_split_coequalizers_of_reflects_isomorphisms [reflects_isomorphisms G] [∀ ⦃A B⦄ (f g : A ⟶ B) [G.is_split_pair f g], has_coequalizer f g] [∀ ⦃A B⦄ (f g : A ⟶ B) [G.is_split_pair f g], preserves_colimit (parallel_pair f g) G] : monadic_right_adjoint G := begin apply monadic_of_has_preserves_reflects_G_split_coequalizers _, { apply_instance }, { assumption }, { assumption }, { introsI A B f g i, apply reflects_colimit_of_reflects_isomorphisms }, end end beck_monadicity section reflexive_monadicity variables [has_reflexive_coequalizers D] [reflects_isomorphisms G] variables [∀ ⦃A B⦄ (f g : A ⟶ B) [is_reflexive_pair f g], preserves_colimit (parallel_pair f g) G] /-- Reflexive (crude) monadicity theorem. If `G` has a right adjoint, `D` has and `G` preserves reflexive coequalizers and `G` reflects isomorphisms, then `G` is monadic. -/ def monadic_of_has_preserves_reflexive_coequalizers_of_reflects_isomorphisms : monadic_right_adjoint G := begin let L : (adjunction.of_right_adjoint G).to_monad.algebra ⥤ D := left_adjoint_comparison, letI i : is_right_adjoint (comparison (adjunction.of_right_adjoint G)) := ⟨_, comparison_adjunction⟩, constructor, let : Π (X : (adjunction.of_right_adjoint G).to_monad.algebra), is_iso ((adjunction.of_right_adjoint (comparison (adjunction.of_right_adjoint G))).unit.app X), { intro X, apply is_iso_of_reflects_iso _ (monad.forget (adjunction.of_right_adjoint G).to_monad), { change is_iso (comparison_adjunction.unit.app X).f, rw comparison_adjunction_unit_f, change is_iso (is_colimit.cocone_point_unique_up_to_iso (beck_coequalizer X) (unit_colimit_of_preserves_coequalizer X)).hom, apply is_iso.of_iso (is_colimit.cocone_point_unique_up_to_iso _ _) } }, let : Π (Y : D), is_iso ((of_right_adjoint (comparison (adjunction.of_right_adjoint G))).counit.app Y), { intro Y, change is_iso (comparison_adjunction.counit.app Y), rw comparison_adjunction_counit_app, change is_iso (is_colimit.cocone_point_unique_up_to_iso _ _).hom, apply_instance, apply counit_coequalizer_of_reflects_coequalizer _, apply reflects_colimit_of_reflects_isomorphisms }, exactI adjunction.is_right_adjoint_to_is_equivalence, end end reflexive_monadicity end monad end category_theory
35119052860df7a7a10943fb92270b01413c2a29
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/errorOnInductionForNested.lean
fc7d5dfc429afe22e5a9b7307ccf8edb7a68145d
[ "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
209
lean
inductive LazyList (α : Type u) | nil : LazyList α | cons (hd : α) (tl : LazyList α) : LazyList α | delayed (t : Thunk (LazyList α)) : LazyList α example (as : LazyList α) : True := by induction as
5d20052f015d166bcd6a552a82bb138c774dafa5
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/group_theory/abelianization.lean
c2915d7a3684051efa4c7164d0b2925dc1aed5b5
[ "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
10,559
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Michael Howes -/ import data.finite.card import group_theory.commutator import group_theory.finiteness /-! # The abelianization of a group > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the commutator and the abelianization of a group. It furthermore prepares for the result that the abelianization is left adjoint to the forgetful functor from abelian groups to groups, which can be found in `algebra/category/Group/adjunctions`. ## Main definitions * `commutator`: defines the commutator of a group `G` as a subgroup of `G`. * `abelianization`: defines the abelianization of a group `G` as the quotient of a group by its commutator subgroup. * `abelianization.map`: lifts a group homomorphism to a homomorphism between the abelianizations * `mul_equiv.abelianization_congr`: Equivalent groups have equivalent abelianizations -/ universes u v w -- Let G be a group. variables (G : Type u) [group G] open subgroup (centralizer) /-- The commutator subgroup of a group G is the normal subgroup generated by the commutators [p,q]=`p*q*p⁻¹*q⁻¹`. -/ @[derive subgroup.normal] def commutator : subgroup G := ⁅(⊤ : subgroup G), ⊤⁆ lemma commutator_def : commutator G = ⁅(⊤ : subgroup G), ⊤⁆ := rfl lemma commutator_eq_closure : commutator G = subgroup.closure (commutator_set G) := by simp [commutator, subgroup.commutator_def, commutator_set] lemma commutator_eq_normal_closure : commutator G = subgroup.normal_closure (commutator_set G) := by simp [commutator, subgroup.commutator_def', commutator_set] instance commutator_characteristic : (commutator G).characteristic := subgroup.commutator_characteristic ⊤ ⊤ instance [finite (commutator_set G)] : group.fg (commutator G) := begin rw commutator_eq_closure, apply group.closure_finite_fg, end lemma rank_commutator_le_card [finite (commutator_set G)] : group.rank (commutator G) ≤ nat.card (commutator_set G) := begin rw subgroup.rank_congr (commutator_eq_closure G), apply subgroup.rank_closure_finite_le_nat_card, end lemma commutator_centralizer_commutator_le_center : ⁅centralizer (commutator G : set G), centralizer (commutator G : set G)⁆ ≤ subgroup.center G := begin rw [←subgroup.centralizer_univ, ←subgroup.coe_top, ←subgroup.commutator_eq_bot_iff_le_centralizer], suffices : ⁅⁅⊤, centralizer (commutator G : set G)⁆, centralizer (commutator G : set G)⁆ = ⊥, { refine subgroup.commutator_commutator_eq_bot_of_rotate _ this, rwa subgroup.commutator_comm (centralizer (commutator G : set G)) }, rw [subgroup.commutator_comm, subgroup.commutator_eq_bot_iff_le_centralizer], exact set.centralizer_subset (subgroup.commutator_mono le_top le_top), end /-- The abelianization of G is the quotient of G by its commutator subgroup. -/ def abelianization : Type u := G ⧸ (commutator G) namespace abelianization local attribute [instance] quotient_group.left_rel instance : comm_group (abelianization G) := { mul_comm := λ x y, quotient.induction_on₂' x y $ λ a b, quotient.sound' $ quotient_group.left_rel_apply.mpr $ subgroup.subset_closure ⟨b⁻¹, subgroup.mem_top b⁻¹, a⁻¹, subgroup.mem_top a⁻¹, by group⟩, .. quotient_group.quotient.group _ } instance : inhabited (abelianization G) := ⟨1⟩ instance [fintype G] [decidable_pred (∈ commutator G)] : fintype (abelianization G) := quotient_group.fintype (commutator G) instance [finite G] : finite (abelianization G) := quotient.finite _ variable {G} /-- `of` is the canonical projection from G to its abelianization. -/ def of : G →* abelianization G := { to_fun := quotient_group.mk, map_one' := rfl, map_mul' := λ x y, rfl } @[simp] lemma mk_eq_of (a : G) : quot.mk _ a = of a := rfl section lift -- So far we have built Gᵃᵇ and proved it's an abelian group. -- Furthremore we defined the canonical projection `of : G → Gᵃᵇ` -- Let `A` be an abelian group and let `f` be a group homomorphism from `G` to `A`. variables {A : Type v} [comm_group A] (f : G →* A) lemma commutator_subset_ker : commutator G ≤ f.ker := begin rw [commutator_eq_closure, subgroup.closure_le], rintros x ⟨p, q, rfl⟩, simp [monoid_hom.mem_ker, mul_right_comm (f p) (f q), commutator_element_def], end /-- If `f : G → A` is a group homomorphism to an abelian group, then `lift f` is the unique map from the abelianization of a `G` to `A` that factors through `f`. -/ def lift : (G →* A) ≃ (abelianization G →* A) := { to_fun := λ f, quotient_group.lift _ f (λ x h, f.mem_ker.2 $ commutator_subset_ker _ h), inv_fun := λ F, F.comp of, left_inv := λ f, monoid_hom.ext $ λ x, rfl, right_inv := λ F, monoid_hom.ext $ λ x, quotient_group.induction_on x $ λ z, rfl } @[simp] lemma lift.of (x : G) : lift f (of x) = f x := rfl theorem lift.unique (φ : abelianization G →* A) -- hφ : φ agrees with f on the image of G in Gᵃᵇ (hφ : ∀ (x : G), φ (of x) = f x) {x : abelianization G} : φ x = lift f x := quotient_group.induction_on x hφ @[simp] lemma lift_of : lift of = monoid_hom.id (abelianization G) := lift.apply_symm_apply $ monoid_hom.id _ end lift variables {A : Type v} [monoid A] /-- See note [partially-applied ext lemmas]. -/ @[ext] theorem hom_ext (φ ψ : abelianization G →* A) (h : φ.comp of = ψ.comp of) : φ = ψ := monoid_hom.ext $ λ x, quotient_group.induction_on x $ monoid_hom.congr_fun h section map variables {H : Type v} [group H] (f : G →* H) /-- The map operation of the `abelianization` functor -/ def map : abelianization G →* abelianization H := lift (of.comp f) @[simp] lemma map_of (x : G) : map f (of x) = of (f x) := rfl @[simp] lemma map_id : map (monoid_hom.id G) = monoid_hom.id (abelianization G) := hom_ext _ _ rfl @[simp] lemma map_comp {I : Type w} [group I] (g : H →* I) : (map g).comp (map f) = map (g.comp f) := hom_ext _ _ rfl @[simp] lemma map_map_apply {I : Type w} [group I] {g : H →* I} {x : abelianization G}: map g (map f x) = map (g.comp f) x := monoid_hom.congr_fun (map_comp _ _) x end map end abelianization section abelianization_congr variables {G} {H : Type v} [group H] (e : G ≃* H) /-- Equivalent groups have equivalent abelianizations -/ def mul_equiv.abelianization_congr : abelianization G ≃* abelianization H := { to_fun := abelianization.map e.to_monoid_hom, inv_fun := abelianization.map e.symm.to_monoid_hom, left_inv := by { rintros ⟨a⟩, simp }, right_inv := by { rintros ⟨a⟩, simp }, map_mul' := monoid_hom.map_mul _ } @[simp] lemma abelianization_congr_of (x : G) : (e.abelianization_congr) (abelianization.of x) = abelianization.of (e x) := rfl @[simp] lemma abelianization_congr_refl : (mul_equiv.refl G).abelianization_congr = mul_equiv.refl (abelianization G) := mul_equiv.to_monoid_hom_injective abelianization.lift_of @[simp] lemma abelianization_congr_symm : e.abelianization_congr.symm = e.symm.abelianization_congr := rfl @[simp] lemma abelianization_congr_trans {I : Type v} [group I] (e₂ : H ≃* I) : e.abelianization_congr.trans e₂.abelianization_congr = (e.trans e₂).abelianization_congr := mul_equiv.to_monoid_hom_injective (abelianization.hom_ext _ _ rfl) end abelianization_congr /-- An Abelian group is equivalent to its own abelianization. -/ @[simps] def abelianization.equiv_of_comm {H : Type*} [comm_group H] : H ≃* abelianization H := { to_fun := abelianization.of, inv_fun := abelianization.lift (monoid_hom.id H), left_inv := λ a, rfl, right_inv := by { rintros ⟨a⟩, refl, }, .. abelianization.of } section commutator_representatives open subgroup /-- Representatives `(g₁, g₂) : G × G` of commutator_set `⁅g₁, g₂⁆ ∈ G`. -/ def commutator_representatives : set (G × G) := set.range (λ g : commutator_set G, (g.2.some, g.2.some_spec.some)) instance [finite (commutator_set G)] : finite (commutator_representatives G) := set.finite_coe_iff.mpr (set.finite_range _) /-- Subgroup generated by representatives `g₁ g₂ : G` of commutators `⁅g₁, g₂⁆ ∈ G`. -/ def closure_commutator_representatives : subgroup G := closure (prod.fst '' commutator_representatives G ∪ prod.snd '' commutator_representatives G) instance closure_commutator_representatives_fg [finite (commutator_set G)] : group.fg (closure_commutator_representatives G) := group.closure_finite_fg _ lemma rank_closure_commutator_representations_le [finite (commutator_set G)] : group.rank (closure_commutator_representatives G) ≤ 2 * nat.card (commutator_set G) := begin rw two_mul, exact (subgroup.rank_closure_finite_le_nat_card _).trans ((set.card_union_le _ _).trans (add_le_add ((finite.card_image_le _).trans (finite.card_range_le _)) ((finite.card_image_le _).trans (finite.card_range_le _ )))), end lemma image_commutator_set_closure_commutator_representatives : (closure_commutator_representatives G).subtype '' (commutator_set (closure_commutator_representatives G)) = commutator_set G := begin apply set.subset.antisymm, { rintros - ⟨-, ⟨g₁, g₂, rfl⟩, rfl⟩, exact ⟨g₁, g₂, rfl⟩ }, { exact λ g hg, ⟨_, ⟨⟨_, subset_closure (or.inl ⟨_, ⟨⟨g, hg⟩, rfl⟩, rfl⟩)⟩, ⟨_, subset_closure (or.inr ⟨_, ⟨⟨g, hg⟩, rfl⟩, rfl⟩)⟩, rfl⟩, hg.some_spec.some_spec⟩ }, end lemma card_commutator_set_closure_commutator_representatives : nat.card (commutator_set (closure_commutator_representatives G)) = nat.card (commutator_set G) := begin rw ← image_commutator_set_closure_commutator_representatives G, exact nat.card_congr (equiv.set.image _ _ (subtype_injective _)), end lemma card_commutator_closure_commutator_representatives : nat.card (commutator (closure_commutator_representatives G)) = nat.card (commutator G) := begin rw [commutator_eq_closure G, ←image_commutator_set_closure_commutator_representatives, ←monoid_hom.map_closure, ←commutator_eq_closure], exact nat.card_congr (equiv.set.image _ _ (subtype_injective _)), end instance [finite (commutator_set G)] : finite (commutator_set (closure_commutator_representatives G)) := begin apply nat.finite_of_card_ne_zero, rw card_commutator_set_closure_commutator_representatives, exact finite.card_pos.ne', end end commutator_representatives
f95ca3e7532af416c51833108b81dcbd899129c8
491068d2ad28831e7dade8d6dff871c3e49d9431
/library/theories/number_theory/irrational_roots.lean
11ad148bd7ec82b10b82fe9045d1590fd8a6da8d
[ "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
7,174
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad A proof that if n > 1 and a > 0, then the nth root of a is irrational, unless a is a perfect nth power. -/ import data.rat .prime_factorization open eq.ops /- First, a textbook proof that sqrt 2 is irrational. -/ section open nat theorem sqrt_two_irrational {a b : ℕ} (co : coprime a b) : a^2 ≠ 2 * b^2 := assume H : a^2 = 2 * b^2, have even (a^2), from even_of_exists (exists.intro _ H), have even a, from even_of_even_pow this, obtain c (aeq : a = 2 * c), from exists_of_even this, have 2 * (2 * c^2) = 2 * b^2, by rewrite [-H, aeq, *pow_two, mul.assoc, mul.left_comm c], have 2 * c^2 = b^2, from eq_of_mul_eq_mul_left dec_trivial this, have even (b^2), from even_of_exists (exists.intro _ (eq.symm this)), have even b, from even_of_even_pow this, have 2 ∣ gcd a b, from dvd_gcd (dvd_of_even `even a`) (dvd_of_even `even b`), have 2 ∣ 1, from co ▸ this, show false, from absurd `2 ∣ 1` dec_trivial end /- Replacing 2 by an arbitrary prime and the power 2 by any n ≥ 1 yields the stronger result that the nth root of an integer is irrational, unless the integer is already a perfect nth power. -/ section open nat decidable theorem root_irrational {a b c n : ℕ} (npos : n > 0) (apos : a > 0) (co : coprime a b) (H : a^n = c * b^n) : b = 1 := have bpos : b > 0, from pos_of_ne_zero (suppose b = 0, have a^n = 0, by rewrite [H, this, zero_pow npos], assert a = 0, from eq_zero_of_pow_eq_zero this, show false, from ne_of_lt `0 < a` this⁻¹), have H₁ : ∀ p, prime p → ¬ p ∣ b, from take p, suppose prime p, suppose p ∣ b, assert p ∣ b^n, from dvd_pow_of_dvd_of_pos `p ∣ b` `n > 0`, have p ∣ a^n, by rewrite H; apply dvd_mul_of_dvd_right this, have p ∣ a, from dvd_of_prime_of_dvd_pow `prime p` this, have ¬ coprime a b, from not_coprime_of_dvd_of_dvd (gt_one_of_prime `prime p`) `p ∣ a` `p ∣ b`, show false, from this `coprime a b`, have b < 2, from by_contradiction (suppose ¬ b < 2, have b ≥ 2, from le_of_not_gt this, obtain p [primep pdvdb], from exists_prime_and_dvd this, show false, from H₁ p primep pdvdb), show b = 1, from (le.antisymm (le_of_lt_succ `b < 2`) (succ_le_of_lt `b > 0`)) end /- Here we state this in terms of the rationals, ℚ. The main difficulty is casting between ℕ, ℤ, and ℚ. -/ section open rat int nat decidable theorem denom_eq_one_of_pow_eq {q : ℚ} {n : ℕ} {c : ℤ} (npos : n > 0) (H : q^n = c) : denom q = 1 := let a := num q, b := denom q in have b ≠ 0, from ne_of_gt (denom_pos q), have bnz : b ≠ (0 : ℚ), from assume H, `b ≠ 0` (of_int.inj H), have bnnz : (#rat b^n ≠ 0), from assume bneqz, bnz (eq_zero_of_pow_eq_zero bneqz), have a^n / b^n = c, using bnz, by rewrite [*of_int_pow, -div_pow, -eq_num_div_denom, -H], have a^n = c * b^n, from eq.symm (!mul_eq_of_eq_div bnnz this⁻¹), have a^n = c * b^n, -- int version using this, by rewrite [-of_int_pow at this, -of_int_mul at this]; exact of_int.inj this, have (abs a)^n = abs c * (abs b)^n, using this, by rewrite [-int.abs_pow, this, int.abs_mul, int.abs_pow], have H₁ : (nat_abs a)^n = nat_abs c * (nat_abs b)^n, using this, by apply int.of_nat.inj; rewrite [int.of_nat_mul, +int.of_nat_pow, +of_nat_nat_abs]; assumption, have H₂ : nat.coprime (nat_abs a) (nat_abs b), from of_nat.inj !coprime_num_denom, have nat_abs b = 1, from by_cases (suppose q = 0, by rewrite this) (suppose q ≠ 0, have a ≠ 0, from suppose a = 0, `q ≠ 0` (by rewrite [eq_num_div_denom, `a = 0`, zero_div]), have nat_abs a ≠ 0, from suppose nat_abs a = 0, `a ≠ 0` (eq_zero_of_nat_abs_eq_zero this), show nat_abs b = 1, from (root_irrational npos (pos_of_ne_zero this) H₂ H₁)), show b = 1, using this, by rewrite [-of_nat_nat_abs_of_nonneg (le_of_lt !denom_pos), this] theorem eq_num_pow_of_pow_eq {q : ℚ} {n : ℕ} {c : ℤ} (npos : n > 0) (H : q^n = c) : c = (num q)^n := have denom q = 1, from denom_eq_one_of_pow_eq npos H, have of_int c = (num q)^n, using this, by rewrite [-H, eq_num_div_denom q at {1}, this, div_one, of_int_pow], show c = (num q)^n , from of_int.inj this end /- As a corollary, for n > 1, the nth root of a prime is irrational. -/ section open nat theorem not_eq_pow_of_prime {p n : ℕ} (a : ℕ) (ngt1 : n > 1) (primep : prime p) : p ≠ a^n := assume peq : p = a^n, have npos : n > 0, from lt.trans dec_trivial ngt1, have pnez : p ≠ 0, from (suppose p = 0, show false, by let H := (pos_of_prime primep); rewrite this at H; exfalso; exact !lt.irrefl H), have agtz : a > 0, from pos_of_ne_zero (suppose a = 0, show false, using npos pnez, by revert peq; rewrite [this, zero_pow npos]; exact pnez), have n * mult p a = 1, from calc n * mult p a = mult p (a^n) : using agtz, by rewrite [mult_pow n agtz primep] ... = mult p p : peq ... = 1 : mult_self (gt_one_of_prime primep), have n ∣ 1, from dvd_of_mul_right_eq this, have n = 1, from eq_one_of_dvd_one this, show false, using this, by rewrite this at ngt1; exact !lt.irrefl ngt1 open int rat theorem root_prime_irrational {p n : ℕ} {q : ℚ} (qnonneg : q ≥ 0) (ngt1 : n > 1) (primep : prime p) : q^n ≠ p := have numq : num q ≥ 0, from num_nonneg_of_nonneg qnonneg, have npos : n > 0, from lt.trans dec_trivial ngt1, suppose q^n = p, have p = (num q)^n, from eq_num_pow_of_pow_eq npos this, have p = (nat_abs (num q))^n, using this numq, by apply of_nat.inj; rewrite [this, of_nat_pow, of_nat_nat_abs_of_nonneg numq], show false, from not_eq_pow_of_prime _ ngt1 primep this end /- Thaetetus, who lives in the fourth century BC, is said to have proved the irrationality of square roots up to seventeen. In Chapter 4 of /Why Prove it Again/, John Dawson notes that Thaetetus may have used an approach similar to the one below. (See data/nat/gcd.lean for the key theorem, "div_gcd_eq_div_gcd".) -/ section open int example {a b c : ℤ} (co : coprime a b) (apos : a > 0) (bpos : b > 0) (H : a * a = c * (b * b)) : b = 1 := assert H₁ : gcd (c * b) a = gcd c a, from gcd_mul_right_cancel_of_coprime _ (coprime_swap co), have a * a = c * b * b, by rewrite -mul.assoc at H; apply H, have a div (gcd a b) = c * b div gcd (c * b) a, from div_gcd_eq_div_gcd this bpos apos, have a = c * b div gcd c a, using this, by revert this; rewrite [↑coprime at co, co, div_one, H₁]; intros; assumption, have a = b * (c div gcd c a), using this, by revert this; rewrite [mul.comm, !mul_div_assoc !gcd_dvd_left]; intros; assumption, have b ∣ a, from dvd_of_mul_right_eq this⁻¹, have b ∣ gcd a b, from dvd_gcd this !dvd.refl, have b ∣ 1, using this, by rewrite [↑coprime at co, co at this]; apply this, show b = 1, from eq_one_of_dvd_one (le_of_lt bpos) this end
b44356a18285bc978e984a321923ec8298bc1f91
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/free_algebra.lean
fc2e81ca3a5b327992dafc87418f02a15c4347df
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
14,107
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Scott Morrison, Adam Topaz. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.algebra.subalgebra import Mathlib.algebra.monoid_algebra import Mathlib.linear_algebra.default import Mathlib.data.equiv.transfer_instance import Mathlib.PostPort universes u_1 u_2 l u_3 namespace Mathlib /-! # Free Algebras Given a commutative semiring `R`, and a type `X`, we construct the free `R`-algebra on `X`. ## Notation 1. `free_algebra R X` is the free algebra itself. It is endowed with an `R`-algebra structure. 2. `free_algebra.ι R` is the function `X → free_algebra R X`. 3. Given a function `f : X → A` to an R-algebra `A`, `lift R f` is the lift of `f` to an `R`-algebra morphism `free_algebra R X → A`. ## Theorems 1. `ι_comp_lift` states that the composition `(lift R f) ∘ (ι R)` is identical to `f`. 2. `lift_unique` states that whenever an R-algebra morphism `g : free_algebra R X → A` is given whose composition with `ι R` is `f`, then one has `g = lift R f`. 3. `hom_ext` is a variant of `lift_unique` in the form of an extensionality theorem. 4. `lift_comp_ι` is a combination of `ι_comp_lift` and `lift_unique`. It states that the lift of the composition of an algebra morphism with `ι` is the algebra morphism itself. 5. `equiv_monoid_algebra_free_monoid : free_algebra R X ≃ₐ[R] monoid_algebra R (free_monoid X)` 6. An inductive principle `induction`. ## Implementation details We construct the free algebra on `X` as a quotient of an inductive type `free_algebra.pre` by an inductively defined relation `free_algebra.rel`. Explicitly, the construction involves three steps: 1. We construct an inductive type `free_algebra.pre R X`, the terms of which should be thought of as representatives for the elements of `free_algebra R X`. It is the free type with maps from `R` and `X`, and with two binary operations `add` and `mul`. 2. We construct an inductive relation `free_algebra.rel R X` on `free_algebra.pre R X`. This is the smallest relation for which the quotient is an `R`-algebra where addition resp. multiplication are induced by `add` resp. `mul` from 1., and for which the map from `R` is the structure map for the algebra. 3. The free algebra `free_algebra R X` is the quotient of `free_algebra.pre R X` by the relation `free_algebra.rel R X`. -/ namespace free_algebra /-- This inductive type is used to express representatives of the free algebra. -/ inductive pre (R : Type u_1) [comm_semiring R] (X : Type u_2) where | of : X → pre R X | of_scalar : R → pre R X | add : pre R X → pre R X → pre R X | mul : pre R X → pre R X → pre R X namespace pre protected instance inhabited (R : Type u_1) [comm_semiring R] (X : Type u_2) : Inhabited (pre R X) := { default := of_scalar 0 } -- Note: These instances are only used to simplify the notation. /-- Coercion from `X` to `pre R X`. Note: Used for notation only. -/ /-- Coercion from `R` to `pre R X`. Note: Used for notation only. -/ def has_coe_generator (R : Type u_1) [comm_semiring R] (X : Type u_2) : has_coe X (pre R X) := has_coe.mk of /-- Multiplication in `pre R X` defined as `pre.mul`. Note: Used for notation only. -/ def has_coe_semiring (R : Type u_1) [comm_semiring R] (X : Type u_2) : has_coe R (pre R X) := has_coe.mk of_scalar /-- Addition in `pre R X` defined as `pre.add`. Note: Used for notation only. -/ def has_mul (R : Type u_1) [comm_semiring R] (X : Type u_2) : Mul (pre R X) := { mul := mul } /-- Zero in `pre R X` defined as the image of `0` from `R`. Note: Used for notation only. -/ def has_add (R : Type u_1) [comm_semiring R] (X : Type u_2) : Add (pre R X) := { add := add } /-- One in `pre R X` defined as the image of `1` from `R`. Note: Used for notation only. -/ def has_zero (R : Type u_1) [comm_semiring R] (X : Type u_2) : HasZero (pre R X) := { zero := of_scalar 0 } /-- def has_one (R : Type u_1) [comm_semiring R] (X : Type u_2) : HasOne (pre R X) := { one := of_scalar 1 } Scalar multiplication defined as multiplication by the image of elements from `R`. Note: Used for notation only. -/ def has_scalar (R : Type u_1) [comm_semiring R] (X : Type u_2) : has_scalar R (pre R X) := has_scalar.mk fun (r : R) (m : pre R X) => mul (of_scalar r) m end pre /-- Given a function from `X` to an `R`-algebra `A`, `lift_fun` provides a lift of `f` to a function from `pre R X` to `A`. This is mainly used in the construction of `free_algebra.lift`. -/ def lift_fun (R : Type u_1) [comm_semiring R] (X : Type u_2) {A : Type u_3} [semiring A] [algebra R A] (f : X → A) : pre R X → A := fun (t : pre R X) => pre.rec_on t f (⇑(algebra_map R A)) (fun (_x _x : pre R X) => Add.add) fun (_x _x : pre R X) => Mul.mul /-- An inductively defined relation on `pre R X` used to force the initial algebra structure on the associated quotient. -/ -- force `of_scalar` to be a central semiring morphism inductive rel (R : Type u_1) [comm_semiring R] (X : Type u_2) : pre R X → pre R X → Prop where | add_scalar : ∀ {r s : R}, rel R X (↑(r + s)) (↑r + ↑s) | mul_scalar : ∀ {r s : R}, rel R X (↑(r * s)) (↑r * ↑s) | central_scalar : ∀ {r : R} {a : pre R X}, rel R X (↑r * a) (a * ↑r) | add_assoc : ∀ {a b c : pre R X}, rel R X (a + b + c) (a + (b + c)) | add_comm : ∀ {a b : pre R X}, rel R X (a + b) (b + a) | zero_add : ∀ {a : pre R X}, rel R X (0 + a) a | mul_assoc : ∀ {a b c : pre R X}, rel R X (a * b * c) (a * (b * c)) | one_mul : ∀ {a : pre R X}, rel R X (1 * a) a | mul_one : ∀ {a : pre R X}, rel R X (a * 1) a | left_distrib : ∀ {a b c : pre R X}, rel R X (a * (b + c)) (a * b + a * c) | right_distrib : ∀ {a b c : pre R X}, rel R X ((a + b) * c) (a * c + b * c) | zero_mul : ∀ {a : pre R X}, rel R X (0 * a) 0 | mul_zero : ∀ {a : pre R X}, rel R X (a * 0) 0 | add_compat_left : ∀ {a b c : pre R X}, rel R X a b → rel R X (a + c) (b + c) | add_compat_right : ∀ {a b c : pre R X}, rel R X a b → rel R X (c + a) (c + b) | mul_compat_left : ∀ {a b c : pre R X}, rel R X a b → rel R X (a * c) (b * c) | mul_compat_right : ∀ {a b c : pre R X}, rel R X a b → rel R X (c * a) (c * b) -- commutative additive semigroup -- multiplicative monoid -- distributivity -- other relations needed for semiring -- compatibility end free_algebra /-- The free algebra for the type `X` over the commutative semiring `R`. -/ def free_algebra (R : Type u_1) [comm_semiring R] (X : Type u_2) := Quot sorry namespace free_algebra protected instance semiring (R : Type u_1) [comm_semiring R] (X : Type u_2) : semiring (free_algebra R X) := semiring.mk (quot.map₂ Add.add sorry sorry) sorry (Quot.mk (rel R X) 0) sorry sorry sorry (quot.map₂ Mul.mul sorry sorry) sorry (Quot.mk (rel R X) 1) sorry sorry sorry sorry sorry sorry protected instance inhabited (R : Type u_1) [comm_semiring R] (X : Type u_2) : Inhabited (free_algebra R X) := { default := 0 } protected instance has_scalar (R : Type u_1) [comm_semiring R] (X : Type u_2) : has_scalar R (free_algebra R X) := has_scalar.mk fun (r : R) (a : free_algebra R X) => quot.lift_on a (fun (x : pre R X) => Quot.mk (rel R X) (↑r * x)) sorry protected instance algebra (R : Type u_1) [comm_semiring R] (X : Type u_2) : algebra R (free_algebra R X) := algebra.mk (ring_hom.mk (fun (r : R) => Quot.mk (rel R X) ↑r) sorry sorry sorry sorry) sorry sorry protected instance ring (X : Type u_2) {S : Type u_1} [comm_ring S] : ring (free_algebra S X) := algebra.semiring_to_ring S /-- The canonical function `X → free_algebra R X`. -/ def ι (R : Type u_1) [comm_semiring R] {X : Type u_2} : X → free_algebra R X := fun (m : X) => Quot.mk (rel R X) ↑m @[simp] theorem quot_mk_eq_ι (R : Type u_1) [comm_semiring R] {X : Type u_2} (m : X) : Quot.mk (rel R X) ↑m = ι R m := rfl /-- Internal definition used to define `lift` -/ /-- Given a function `f : X → A` where `A` is an `R`-algebra, `lift R f` is the unique lift of `f` to a morphism of `R`-algebras `free_algebra R X → A`. -/ def lift (R : Type u_1) [comm_semiring R] {X : Type u_2} {A : Type u_3} [semiring A] [algebra R A] : (X → A) ≃ alg_hom R (free_algebra R X) A := equiv.mk (lift_aux R) (fun (F : alg_hom R (free_algebra R X) A) => ⇑F ∘ ι R) sorry sorry @[simp] theorem lift_aux_eq (R : Type u_1) [comm_semiring R] {X : Type u_2} {A : Type u_3} [semiring A] [algebra R A] (f : X → A) : lift_aux R f = coe_fn (lift R) f := rfl @[simp] theorem lift_symm_apply (R : Type u_1) [comm_semiring R] {X : Type u_2} {A : Type u_3} [semiring A] [algebra R A] (F : alg_hom R (free_algebra R X) A) : coe_fn (equiv.symm (lift R)) F = ⇑F ∘ ι R := rfl @[simp] theorem ι_comp_lift {R : Type u_1} [comm_semiring R] {X : Type u_2} {A : Type u_3} [semiring A] [algebra R A] (f : X → A) : ⇑(coe_fn (lift R) f) ∘ ι R = f := funext fun (x : X) => Eq.refl (function.comp (⇑(coe_fn (lift R) f)) (ι R) x) @[simp] theorem lift_ι_apply {R : Type u_1} [comm_semiring R] {X : Type u_2} {A : Type u_3} [semiring A] [algebra R A] (f : X → A) (x : X) : coe_fn (coe_fn (lift R) f) (ι R x) = f x := rfl @[simp] theorem lift_unique {R : Type u_1} [comm_semiring R] {X : Type u_2} {A : Type u_3} [semiring A] [algebra R A] (f : X → A) (g : alg_hom R (free_algebra R X) A) : ⇑g ∘ ι R = f ↔ g = coe_fn (lift R) f := equiv.symm_apply_eq (lift R) /-! At this stage we set the basic definitions as `@[irreducible]`, so from this point onwards one should only use the universal properties of the free algebra, and consider the actual implementation as a quotient of an inductive type as completely hidden. Of course, one still has the option to locally make these definitions `semireducible` if so desired, and Lean is still willing in some circumstances to do unification based on the underlying definition. -/ -- Marking `free_algebra` irreducible makes `ring` instances inaccessible on quotients. -- https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/algebra.2Esemiring_to_ring.20breaks.20semimodule.20typeclass.20lookup/near/212580241 -- For now, we avoid this by not marking it irreducible. @[simp] theorem lift_comp_ι {R : Type u_1} [comm_semiring R] {X : Type u_2} {A : Type u_3} [semiring A] [algebra R A] (g : alg_hom R (free_algebra R X) A) : coe_fn (lift R) (⇑g ∘ ι R) = g := eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn (lift R) (⇑g ∘ ι R) = g)) (Eq.symm (lift_symm_apply R g)))) (equiv.apply_symm_apply (lift R) g) /-- See note [partially-applied ext lemmas]. -/ theorem hom_ext {R : Type u_1} [comm_semiring R] {X : Type u_2} {A : Type u_3} [semiring A] [algebra R A] {f : alg_hom R (free_algebra R X) A} {g : alg_hom R (free_algebra R X) A} (w : ⇑f ∘ ι R = ⇑g ∘ ι R) : f = g := equiv.injective (equiv.symm (lift R)) (eq.mp (Eq._oldrec (Eq.refl (coe_fn (equiv.symm (lift R)) f = ⇑g ∘ ι R)) (Eq.symm (lift_symm_apply R g))) (eq.mp (Eq._oldrec (Eq.refl (⇑f ∘ ι R = ⇑g ∘ ι R)) (Eq.symm (lift_symm_apply R f))) w)) /-- The free algebra on `X` is "just" the monoid algebra on the free monoid on `X`. This would be useful when constructing linear maps out of a free algebra, for example. -/ def equiv_monoid_algebra_free_monoid {R : Type u_1} [comm_semiring R] {X : Type u_2} : alg_equiv R (free_algebra R X) (monoid_algebra R (free_monoid X)) := alg_equiv.of_alg_hom (coe_fn (lift R) fun (x : X) => coe_fn (monoid_algebra.of R (free_monoid X)) (free_monoid.of x)) (coe_fn (monoid_algebra.lift R (free_monoid X) (free_algebra R X)) (coe_fn free_monoid.lift (ι R))) sorry sorry protected instance nontrivial {R : Type u_1} [comm_semiring R] {X : Type u_2} [nontrivial R] : nontrivial (free_algebra R X) := equiv.nontrivial (alg_equiv.to_equiv equiv_monoid_algebra_free_monoid) /-- The left-inverse of `algebra_map`. -/ def algebra_map_inv {R : Type u_1} [comm_semiring R] {X : Type u_2} : alg_hom R (free_algebra R X) R := coe_fn (lift R) 0 theorem algebra_map_left_inverse {R : Type u_1} [comm_semiring R] {X : Type u_2} : function.left_inverse ⇑algebra_map_inv ⇑(algebra_map R (free_algebra R X)) := sorry -- this proof is copied from the approach in `free_abelian_group.of_injective` theorem ι_injective {R : Type u_1} [comm_semiring R] {X : Type u_2} [nontrivial R] : function.injective (ι R) := sorry end free_algebra /- There is something weird in the above namespace that breaks the typeclass resolution of `has_coe_to_sort` below. Closing it and reopening it fixes it... -/ namespace free_algebra /-- An induction principle for the free algebra. If `C` holds for the `algebra_map` of `r : R` into `free_algebra R X`, the `ι` of `x : X`, and is preserved under addition and muliplication, then it holds for all of `free_algebra R X`. -/ theorem induction (R : Type u_1) [comm_semiring R] (X : Type u_2) {C : free_algebra R X → Prop} (h_grade0 : ∀ (r : R), C (coe_fn (algebra_map R (free_algebra R X)) r)) (h_grade1 : ∀ (x : X), C (ι R x)) (h_mul : ∀ (a b : free_algebra R X), C a → C b → C (a * b)) (h_add : ∀ (a b : free_algebra R X), C a → C b → C (a + b)) (a : free_algebra R X) : C a := sorry /-- The star ring formed by reversing the elements of products -/ protected instance star_ring (R : Type u_1) [comm_semiring R] (X : Type u_2) : star_ring (free_algebra R X) := star_ring.mk sorry @[simp] theorem star_ι (R : Type u_1) [comm_semiring R] (X : Type u_2) (x : X) : star (ι R x) = ι R x := sorry @[simp] theorem star_algebra_map (R : Type u_1) [comm_semiring R] (X : Type u_2) (r : R) : star (coe_fn (algebra_map R (free_algebra R X)) r) = coe_fn (algebra_map R (free_algebra R X)) r := sorry /-- `star` as an `alg_equiv` -/ def star_hom (R : Type u_1) [comm_semiring R] (X : Type u_2) : alg_equiv R (free_algebra R X) (free_algebra R Xᵒᵖ) := alg_equiv.mk (ring_equiv.to_fun star_ring_equiv) (ring_equiv.inv_fun star_ring_equiv) sorry sorry sorry sorry sorry
46d69e3487ec781e3e5fb00c6badb14bd7cb0618
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/real/ereal.lean
0e7f0f8f7d11269f7cec71297db11623119850b9
[ "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
40,906
lean
/- Copyright (c) 2019 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard -/ import data.real.basic import data.real.ennreal import data.sign /-! # The extended reals [-∞, ∞]. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines `ereal`, the real numbers together with a top and bottom element, referred to as ⊤ and ⊥. It is implemented as `with_bot (with_top ℝ)` Addition and multiplication are problematic in the presence of ±∞, but negation has a natural definition and satisfies the usual properties. An ad hoc addition is defined, for which `ereal` is an `add_comm_monoid`, and even an ordered one (if `a ≤ a'` and `b ≤ b'` then `a + b ≤ a' + b'`). Note however that addition is badly behaved at `(⊥, ⊤)` and `(⊤, ⊥)` so this can not be upgraded to a group structure. Our choice is that `⊥ + ⊤ = ⊤ + ⊥ = ⊥`, to make sure that the exponential and the logarithm between `ereal` and `ℝ≥0∞` respect the operations (notice that the convention `0 * ∞ = 0` on `ℝ≥0∞` is enforced by measure theory). An ad hoc subtraction is then defined by `x - y = x + (-y)`. It does not have nice properties, but it is sometimes convenient to have. An ad hoc multiplication is defined, for which `ereal` is a `comm_monoid_with_zero`. We make the choice that `0 * x = x * 0 = 0` for any `x` (while the other cases are defined non-ambiguously). This does not distribute with addition, as `⊥ = ⊥ + ⊤ = 1*⊥ + (-1)*⊥ ≠ (1 - 1) * ⊥ = 0 * ⊥ = 0`. `ereal` is a `complete_linear_order`; this is deduced by type class inference from the fact that `with_bot (with_top L)` is a complete linear order if `L` is a conditionally complete linear order. Coercions from `ℝ` and from `ℝ≥0∞` are registered, and their basic properties are proved. The main one is the real coercion, and is usually referred to just as `coe` (lemmas such as `ereal.coe_add` deal with this coercion). The one from `ennreal` is usually called `coe_ennreal` in the `ereal` namespace. We define an absolute value `ereal.abs` from `ereal` to `ℝ≥0∞`. Two elements of `ereal` coincide if and only if they have the same absolute value and the same sign. ## Tags real, ereal, complete lattice -/ open function open_locale ennreal nnreal noncomputable theory /-- ereal : The type `[-∞, ∞]` -/ @[derive [has_bot, has_zero, has_one, nontrivial, add_monoid, has_Sup, has_Inf, complete_linear_order, linear_ordered_add_comm_monoid, zero_le_one_class]] def ereal := with_bot (with_top ℝ) /-- The canonical inclusion froms reals to ereals. Do not use directly: as this is registered as a coercion, use the coercion instead. -/ def real.to_ereal : ℝ → ereal := some ∘ some namespace ereal -- things unify with `with_bot.decidable_lt` later if we we don't provide this explicitly. instance decidable_lt : decidable_rel ((<) : ereal → ereal → Prop) := with_bot.decidable_lt -- TODO: Provide explicitly, otherwise it is inferred noncomputably from `complete_linear_order` instance : has_top ereal := ⟨some ⊤⟩ instance : has_coe ℝ ereal := ⟨real.to_ereal⟩ lemma coe_strict_mono : strict_mono (coe : ℝ → ereal) := with_bot.coe_strict_mono.comp with_top.coe_strict_mono lemma coe_injective : injective (coe : ℝ → ereal) := coe_strict_mono.injective @[simp, norm_cast] protected lemma coe_le_coe_iff {x y : ℝ} : (x : ereal) ≤ (y : ereal) ↔ x ≤ y := coe_strict_mono.le_iff_le @[simp, norm_cast] protected lemma coe_lt_coe_iff {x y : ℝ} : (x : ereal) < (y : ereal) ↔ x < y := coe_strict_mono.lt_iff_lt @[simp, norm_cast] protected lemma coe_eq_coe_iff {x y : ℝ} : (x : ereal) = (y : ereal) ↔ x = y := coe_injective.eq_iff protected lemma coe_ne_coe_iff {x y : ℝ} : (x : ereal) ≠ (y : ereal) ↔ x ≠ y := coe_injective.ne_iff /-- The canonical map from nonnegative extended reals to extended reals -/ def _root_.ennreal.to_ereal : ℝ≥0∞ → ereal | ⊤ := ⊤ | (some x) := x.1 instance has_coe_ennreal : has_coe ℝ≥0∞ ereal := ⟨ennreal.to_ereal⟩ instance : inhabited ereal := ⟨0⟩ @[simp, norm_cast] lemma coe_zero : ((0 : ℝ) : ereal) = 0 := rfl @[simp, norm_cast] lemma coe_one : ((1 : ℝ) : ereal) = 1 := rfl /-- A recursor for `ereal` in terms of the coercion. A typical invocation looks like `induction x using ereal.rec`. Note that using `induction` directly will unfold `ereal` to `option` which is undesirable. When working in term mode, note that pattern matching can be used directly. -/ @[elab_as_eliminator] protected def rec {C : ereal → Sort*} (h_bot : C ⊥) (h_real : Π a : ℝ, C a) (h_top : C ⊤) : ∀ a : ereal, C a | ⊥ := h_bot | (a : ℝ) := h_real a | ⊤ := h_top /-- The multiplication on `ereal`. Our definition satisfies `0 * x = x * 0 = 0` for any `x`, and picks the only sensible value elsewhere. -/ protected def mul : ereal → ereal → ereal | ⊥ ⊥ := ⊤ | ⊥ ⊤ := ⊥ | ⊥ (y : ℝ) := if 0 < y then ⊥ else if y = 0 then 0 else ⊤ | ⊤ ⊥ := ⊥ | ⊤ ⊤ := ⊤ | ⊤ (y : ℝ) := if 0 < y then ⊤ else if y = 0 then 0 else ⊥ | (x : ℝ) ⊤ := if 0 < x then ⊤ else if x = 0 then 0 else ⊥ | (x : ℝ) ⊥ := if 0 < x then ⊥ else if x = 0 then 0 else ⊤ | (x : ℝ) (y : ℝ) := (x * y : ℝ) instance : has_mul ereal := ⟨ereal.mul⟩ /-- Induct on two ereals by performing case splits on the sign of one whenever the other is infinite. -/ @[elab_as_eliminator] lemma induction₂ {P : ereal → ereal → Prop} (top_top : P ⊤ ⊤) (top_pos : ∀ x : ℝ, 0 < x → P ⊤ x) (top_zero : P ⊤ 0) (top_neg : ∀ x : ℝ, x < 0 → P ⊤ x) (top_bot : P ⊤ ⊥) (pos_top : ∀ x : ℝ, 0 < x → P x ⊤) (pos_bot : ∀ x : ℝ, 0 < x → P x ⊥) (zero_top : P 0 ⊤) (coe_coe : ∀ x y : ℝ, P x y) (zero_bot : P 0 ⊥) (neg_top : ∀ x : ℝ, x < 0 → P x ⊤) (neg_bot : ∀ x : ℝ, x < 0 → P x ⊥) (bot_top : P ⊥ ⊤) (bot_pos : ∀ x : ℝ, 0 < x → P ⊥ x) (bot_zero : P ⊥ 0) (bot_neg : ∀ x : ℝ, x < 0 → P ⊥ x) (bot_bot : P ⊥ ⊥) : ∀ x y, P x y | ⊥ ⊥ := bot_bot | ⊥ (y : ℝ) := by { rcases lt_trichotomy 0 y with hy|rfl|hy, exacts [bot_pos y hy, bot_zero, bot_neg y hy] } | ⊥ ⊤ := bot_top | (x : ℝ) ⊥ := by { rcases lt_trichotomy 0 x with hx|rfl|hx, exacts [pos_bot x hx, zero_bot, neg_bot x hx] } | (x : ℝ) (y : ℝ) := coe_coe _ _ | (x : ℝ) ⊤ := by { rcases lt_trichotomy 0 x with hx|rfl|hx, exacts [pos_top x hx, zero_top, neg_top x hx] } | ⊤ ⊥ := top_bot | ⊤ (y : ℝ) := by { rcases lt_trichotomy 0 y with hy|rfl|hy, exacts [top_pos y hy, top_zero, top_neg y hy] } | ⊤ ⊤ := top_top /-! `ereal` with its multiplication is a `comm_monoid_with_zero`. However, the proof of associativity by hand is extremely painful (with 125 cases...). Instead, we will deduce it later on from the facts that the absolute value and the sign are multiplicative functions taking value in associative objects, and that they characterize an extended real number. For now, we only record more basic properties of multiplication. -/ instance : mul_zero_one_class ereal := { one_mul := λ x, begin induction x using ereal.rec; { dsimp only [(*)], simp only [ereal.mul, ← ereal.coe_one, zero_lt_one, if_true, one_mul] }, end, mul_one := λ x, begin induction x using ereal.rec; { dsimp only [(*)], simp only [ereal.mul, ← ereal.coe_one, zero_lt_one, if_true, mul_one] }, end, zero_mul := λ x, begin induction x using ereal.rec; { simp only [(*)], simp only [ereal.mul, ← ereal.coe_zero, zero_lt_one, if_true, if_false, lt_irrefl (0 : ℝ), eq_self_iff_true, zero_mul] }, end, mul_zero := λ x, begin induction x using ereal.rec; { simp only [(*)], simp only [ereal.mul, ← ereal.coe_zero, zero_lt_one, if_true, if_false, lt_irrefl (0 : ℝ), eq_self_iff_true, mul_zero] }, end, ..ereal.has_mul, ..ereal.has_one, ..ereal.has_zero } /-! ### Real coercion -/ instance can_lift : can_lift ereal ℝ coe (λ r, r ≠ ⊤ ∧ r ≠ ⊥) := { prf := λ x hx, begin induction x using ereal.rec, { simpa using hx }, { simp }, { simpa using hx } end } /-- The map from extended reals to reals sending infinities to zero. -/ def to_real : ereal → ℝ | ⊥ := 0 | ⊤ := 0 | (x : ℝ) := x @[simp] lemma to_real_top : to_real ⊤ = 0 := rfl @[simp] lemma to_real_bot : to_real ⊥ = 0 := rfl @[simp] lemma to_real_zero : to_real 0 = 0 := rfl @[simp] lemma to_real_one : to_real 1 = 1 := rfl @[simp] lemma to_real_coe (x : ℝ) : to_real (x : ereal) = x := rfl @[simp] lemma bot_lt_coe (x : ℝ) : (⊥ : ereal) < x := with_bot.bot_lt_coe _ @[simp] lemma coe_ne_bot (x : ℝ) : (x : ereal) ≠ ⊥ := (bot_lt_coe x).ne' @[simp] lemma bot_ne_coe (x : ℝ) : (⊥ : ereal) ≠ x := (bot_lt_coe x).ne @[simp] lemma coe_lt_top (x : ℝ) : (x : ereal) < ⊤ := by { apply with_bot.coe_lt_coe.2, exact with_top.coe_lt_top _ } @[simp] lemma coe_ne_top (x : ℝ) : (x : ereal) ≠ ⊤ := (coe_lt_top x).ne @[simp] lemma top_ne_coe (x : ℝ) : (⊤ : ereal) ≠ x := (coe_lt_top x).ne' @[simp] lemma bot_lt_zero : (⊥ : ereal) < 0 := bot_lt_coe 0 @[simp] lemma bot_ne_zero : (⊥ : ereal) ≠ 0 := (coe_ne_bot 0).symm @[simp] lemma zero_ne_bot : (0 : ereal) ≠ ⊥ := coe_ne_bot 0 @[simp] lemma zero_lt_top : (0 : ereal) < ⊤ := coe_lt_top 0 @[simp] lemma zero_ne_top : (0 : ereal) ≠ ⊤ := coe_ne_top 0 @[simp] lemma top_ne_zero : (⊤ : ereal) ≠ 0 := (coe_ne_top 0).symm @[simp, norm_cast] lemma coe_add (x y : ℝ) : (↑(x + y) : ereal) = x + y := rfl @[simp, norm_cast] lemma coe_mul (x y : ℝ) : (↑(x * y) : ereal) = x * y := rfl @[norm_cast] lemma coe_nsmul (n : ℕ) (x : ℝ) : (↑(n • x) : ereal) = n • x := map_nsmul (⟨coe, coe_zero, coe_add⟩ : ℝ →+ ereal) _ _ @[simp, norm_cast] lemma coe_bit0 (x : ℝ) : (↑(bit0 x) : ereal) = bit0 x := rfl @[simp, norm_cast] lemma coe_bit1 (x : ℝ) : (↑(bit1 x) : ereal) = bit1 x := rfl @[simp, norm_cast] lemma coe_eq_zero {x : ℝ} : (x : ereal) = 0 ↔ x = 0 := ereal.coe_eq_coe_iff @[simp, norm_cast] lemma coe_eq_one {x : ℝ} : (x : ereal) = 1 ↔ x = 1 := ereal.coe_eq_coe_iff lemma coe_ne_zero {x : ℝ} : (x : ereal) ≠ 0 ↔ x ≠ 0 := ereal.coe_ne_coe_iff lemma coe_ne_one {x : ℝ} : (x : ereal) ≠ 1 ↔ x ≠ 1 := ereal.coe_ne_coe_iff @[simp, norm_cast] protected lemma coe_nonneg {x : ℝ} : (0 : ereal) ≤ x ↔ 0 ≤ x := ereal.coe_le_coe_iff @[simp, norm_cast] protected lemma coe_nonpos {x : ℝ} : (x : ereal) ≤ 0 ↔ x ≤ 0 := ereal.coe_le_coe_iff @[simp, norm_cast] protected lemma coe_pos {x : ℝ} : (0 : ereal) < x ↔ 0 < x := ereal.coe_lt_coe_iff @[simp, norm_cast] protected lemma coe_neg' {x : ℝ} : (x : ereal) < 0 ↔ x < 0 := ereal.coe_lt_coe_iff lemma to_real_le_to_real {x y : ereal} (h : x ≤ y) (hx : x ≠ ⊥) (hy : y ≠ ⊤) : x.to_real ≤ y.to_real := begin lift x to ℝ, { simp [hx, (h.trans_lt (lt_top_iff_ne_top.2 hy)).ne], }, lift y to ℝ, { simp [hy, ((bot_lt_iff_ne_bot.2 hx).trans_le h).ne'] }, simpa using h end lemma coe_to_real {x : ereal} (hx : x ≠ ⊤) (h'x : x ≠ ⊥) : (x.to_real : ereal) = x := begin induction x using ereal.rec, { simpa using h'x }, { refl }, { simpa using hx }, end lemma le_coe_to_real {x : ereal} (h : x ≠ ⊤) : x ≤ x.to_real := begin by_cases h' : x = ⊥, { simp only [h', bot_le] }, { simp only [le_refl, coe_to_real h h'] }, end lemma coe_to_real_le {x : ereal} (h : x ≠ ⊥) : ↑x.to_real ≤ x := begin by_cases h' : x = ⊤, { simp only [h', le_top] }, { simp only [le_refl, coe_to_real h' h] }, end lemma eq_top_iff_forall_lt (x : ereal) : x = ⊤ ↔ ∀ (y : ℝ), (y : ereal) < x := begin split, { rintro rfl, exact ereal.coe_lt_top }, { contrapose!, intro h, exact ⟨x.to_real, le_coe_to_real h⟩, }, end lemma eq_bot_iff_forall_lt (x : ereal) : x = ⊥ ↔ ∀ (y : ℝ), x < (y : ereal) := begin split, { rintro rfl, exact bot_lt_coe }, { contrapose!, intro h, exact ⟨x.to_real, coe_to_real_le h⟩, }, end /-! ### ennreal coercion -/ @[simp] lemma to_real_coe_ennreal : ∀ {x : ℝ≥0∞}, to_real (x : ereal) = ennreal.to_real x | ⊤ := rfl | (some x) := rfl @[simp] lemma coe_ennreal_of_real {x : ℝ} : (ennreal.of_real x : ereal) = max x 0 := rfl lemma coe_nnreal_eq_coe_real (x : ℝ≥0) : ((x : ℝ≥0∞) : ereal) = (x : ℝ) := rfl @[simp, norm_cast] lemma coe_ennreal_zero : ((0 : ℝ≥0∞) : ereal) = 0 := rfl @[simp, norm_cast] lemma coe_ennreal_one : ((1 : ℝ≥0∞) : ereal) = 1 := rfl @[simp, norm_cast] lemma coe_ennreal_top : ((⊤ : ℝ≥0∞) : ereal) = ⊤ := rfl @[simp] lemma coe_ennreal_eq_top_iff : ∀ {x : ℝ≥0∞}, (x : ereal) = ⊤ ↔ x = ⊤ | ⊤ := by simp | (some x) := by { simp only [ennreal.coe_ne_top, iff_false, ennreal.some_eq_coe], dec_trivial } lemma coe_nnreal_ne_top (x : ℝ≥0) : ((x : ℝ≥0∞) : ereal) ≠ ⊤ := dec_trivial @[simp] lemma coe_nnreal_lt_top (x : ℝ≥0) : ((x : ℝ≥0∞) : ereal) < ⊤ := dec_trivial lemma coe_ennreal_strict_mono : strict_mono (coe : ℝ≥0∞ → ereal) | ⊤ ⊤ := by simp | (some x) ⊤ := by simp | ⊤ (some y) := by simp | (some x) (some y) := by simp [coe_nnreal_eq_coe_real] lemma coe_ennreal_injective : injective (coe : ℝ≥0∞ → ereal) := coe_ennreal_strict_mono.injective @[simp, norm_cast] lemma coe_ennreal_le_coe_ennreal_iff {x y : ℝ≥0∞} : (x : ereal) ≤ (y : ereal) ↔ x ≤ y := coe_ennreal_strict_mono.le_iff_le @[simp, norm_cast] lemma coe_ennreal_lt_coe_ennreal_iff {x y : ℝ≥0∞} : (x : ereal) < (y : ereal) ↔ x < y := coe_ennreal_strict_mono.lt_iff_lt @[simp, norm_cast] lemma coe_ennreal_eq_coe_ennreal_iff {x y : ℝ≥0∞} : (x : ereal) = (y : ereal) ↔ x = y := coe_ennreal_injective.eq_iff lemma coe_ennreal_ne_coe_ennreal_iff {x y : ℝ≥0∞} : (x : ereal) ≠ (y : ereal) ↔ x ≠ y := coe_ennreal_injective.ne_iff @[simp, norm_cast] lemma coe_ennreal_eq_zero {x : ℝ≥0∞} : (x : ereal) = 0 ↔ x = 0 := by rw [←coe_ennreal_eq_coe_ennreal_iff, coe_ennreal_zero] @[simp, norm_cast] lemma coe_ennreal_eq_one {x : ℝ≥0∞} : (x : ereal) = 1 ↔ x = 1 := by rw [←coe_ennreal_eq_coe_ennreal_iff, coe_ennreal_one] @[norm_cast] lemma coe_ennreal_ne_zero {x : ℝ≥0∞} : (x : ereal) ≠ 0 ↔ x ≠ 0 := coe_ennreal_eq_zero.not @[norm_cast] lemma coe_ennreal_ne_one {x : ℝ≥0∞} : (x : ereal) ≠ 1 ↔ x ≠ 1 := coe_ennreal_eq_one.not lemma coe_ennreal_nonneg (x : ℝ≥0∞) : (0 : ereal) ≤ x := coe_ennreal_le_coe_ennreal_iff.2 (zero_le x) @[simp, norm_cast] lemma coe_ennreal_pos {x : ℝ≥0∞} : (0 : ereal) < x ↔ 0 < x := by rw [←coe_ennreal_zero, coe_ennreal_lt_coe_ennreal_iff] @[simp] lemma bot_lt_coe_ennreal (x : ℝ≥0∞) : (⊥ : ereal) < x := (bot_lt_coe 0).trans_le (coe_ennreal_nonneg _) @[simp] lemma coe_ennreal_ne_bot (x : ℝ≥0∞) : (x : ereal) ≠ ⊥ := (bot_lt_coe_ennreal x).ne' @[simp, norm_cast] lemma coe_ennreal_add (x y : ennreal) : ((x + y : ℝ≥0∞) : ereal) = x + y := by cases x; cases y; refl @[simp, norm_cast] lemma coe_ennreal_mul : ∀ (x y : ℝ≥0∞), ((x * y : ℝ≥0∞) : ereal) = x * y | ⊤ ⊤ := rfl | ⊤ (y : ℝ≥0) := begin rw ennreal.top_mul, split_ifs, { simp only [h, coe_ennreal_zero, mul_zero] }, { have A : (0 : ℝ) < y, { simp only [ennreal.coe_eq_zero] at h, exact nnreal.coe_pos.2 (bot_lt_iff_ne_bot.2 h) }, simp only [coe_nnreal_eq_coe_real, coe_ennreal_top, (*), ereal.mul, A, if_true], } end | (x : ℝ≥0) ⊤ := begin rw ennreal.mul_top, split_ifs, { simp only [h, coe_ennreal_zero, zero_mul] }, { have A : (0 : ℝ) < x, { simp only [ennreal.coe_eq_zero] at h, exact nnreal.coe_pos.2 (bot_lt_iff_ne_bot.2 h) }, simp only [coe_nnreal_eq_coe_real, coe_ennreal_top, (*), ereal.mul, A, if_true] } end | (x : ℝ≥0) (y : ℝ≥0) := by simp only [← ennreal.coe_mul, coe_nnreal_eq_coe_real, nnreal.coe_mul, ereal.coe_mul] @[norm_cast] lemma coe_ennreal_nsmul (n : ℕ) (x : ℝ≥0∞) : (↑(n • x) : ereal) = n • x := map_nsmul (⟨coe, coe_ennreal_zero, coe_ennreal_add⟩ : ℝ≥0∞ →+ ereal) _ _ @[simp, norm_cast] lemma coe_ennreal_bit0 (x : ℝ≥0∞) : (↑(bit0 x) : ereal) = bit0 x := coe_ennreal_add _ _ @[simp, norm_cast] lemma coe_ennreal_bit1 (x : ℝ≥0∞) : (↑(bit1 x) : ereal) = bit1 x := by simp_rw [bit1, coe_ennreal_add, coe_ennreal_bit0, coe_ennreal_one] /-! ### Order -/ lemma exists_rat_btwn_of_lt : Π {a b : ereal} (hab : a < b), ∃ (x : ℚ), a < (x : ℝ) ∧ ((x : ℝ) : ereal) < b | ⊤ b h := (not_top_lt h).elim | (a : ℝ) ⊥ h := (lt_irrefl _ ((bot_lt_coe a).trans h)).elim | (a : ℝ) (b : ℝ) h := by simp [exists_rat_btwn (ereal.coe_lt_coe_iff.1 h)] | (a : ℝ) ⊤ h := let ⟨b, hab⟩ := exists_rat_gt a in ⟨b, by simpa using hab, coe_lt_top _⟩ | ⊥ ⊥ h := (lt_irrefl _ h).elim | ⊥ (a : ℝ) h := let ⟨b, hab⟩ := exists_rat_lt a in ⟨b, bot_lt_coe _, by simpa using hab⟩ | ⊥ ⊤ h := ⟨0, bot_lt_coe _, coe_lt_top _⟩ lemma lt_iff_exists_rat_btwn {a b : ereal} : a < b ↔ ∃ (x : ℚ), a < (x : ℝ) ∧ ((x : ℝ) : ereal) < b := ⟨λ hab, exists_rat_btwn_of_lt hab, λ ⟨x, ax, xb⟩, ax.trans xb⟩ lemma lt_iff_exists_real_btwn {a b : ereal} : a < b ↔ ∃ (x : ℝ), a < x ∧ (x : ereal) < b := ⟨λ hab, let ⟨x, ax, xb⟩ := exists_rat_btwn_of_lt hab in ⟨(x : ℝ), ax, xb⟩, λ ⟨x, ax, xb⟩, ax.trans xb⟩ /-- The set of numbers in `ereal` that are not equal to `±∞` is equivalent to `ℝ`. -/ def ne_top_bot_equiv_real : ({⊥, ⊤}ᶜ : set ereal) ≃ ℝ := { to_fun := λ x, ereal.to_real x, inv_fun := λ x, ⟨x, by simp⟩, left_inv := λ ⟨x, hx⟩, subtype.eq $ begin lift x to ℝ, { simpa [not_or_distrib, and_comm] using hx }, { simp }, end, right_inv := λ x, by simp } /-! ### Addition -/ @[simp] lemma add_bot (x : ereal) : x + ⊥ = ⊥ := with_bot.add_bot _ @[simp] lemma bot_add (x : ereal) : ⊥ + x = ⊥ := with_bot.bot_add _ @[simp] lemma top_add_top : (⊤ : ereal) + ⊤ = ⊤ := rfl @[simp] lemma top_add_coe (x : ℝ) : (⊤ : ereal) + x = ⊤ := rfl @[simp] lemma coe_add_top (x : ℝ) : (x : ereal) + ⊤ = ⊤ := rfl lemma to_real_add : ∀ {x y : ereal} (hx : x ≠ ⊤) (h'x : x ≠ ⊥) (hy : y ≠ ⊤) (h'y : y ≠ ⊥), to_real (x + y) = to_real x + to_real y | ⊥ y hx h'x hy h'y := (h'x rfl).elim | ⊤ y hx h'x hy h'y := (hx rfl).elim | x ⊤ hx h'x hy h'y := (hy rfl).elim | x ⊥ hx h'x hy h'y := (h'y rfl).elim | (x : ℝ) (y : ℝ) hx h'x hy h'y := by simp [← ereal.coe_add] lemma add_lt_add_right_coe {x y : ereal} (h : x < y) (z : ℝ) : x + z < y + z := begin induction x using ereal.rec; induction y using ereal.rec, { exact (lt_irrefl _ h).elim }, { simp only [← coe_add, bot_add, bot_lt_coe] }, { simp }, { exact (lt_irrefl _ (h.trans (bot_lt_coe x))).elim }, { norm_cast at h ⊢, exact add_lt_add_right h _ }, { simp only [← coe_add, top_add_coe, coe_lt_top] }, { exact (lt_irrefl _ (h.trans_le le_top)).elim }, { exact (lt_irrefl _ (h.trans_le le_top)).elim }, { exact (lt_irrefl _ (h.trans_le le_top)).elim }, end lemma add_lt_add_of_lt_of_le {x y z t : ereal} (h : x < y) (h' : z ≤ t) (hz : z ≠ ⊥) (ht : t ≠ ⊤) : x + z < y + t := begin induction z using ereal.rec, { simpa only using hz }, { calc x + z < y + z : add_lt_add_right_coe h _ ... ≤ y + t : add_le_add le_rfl h' }, { exact (ht (top_le_iff.1 h')).elim } end lemma add_lt_add_left_coe {x y : ereal} (h : x < y) (z : ℝ) : (z : ereal) + x < z + y := by simpa [add_comm] using add_lt_add_right_coe h z lemma add_lt_add {x y z t : ereal} (h1 : x < y) (h2 : z < t) : x + z < y + t := begin induction x using ereal.rec, { simp [bot_lt_iff_ne_bot, h1.ne', (bot_le.trans_lt h2).ne'] }, { calc (x : ereal) + z < x + t : add_lt_add_left_coe h2 _ ... ≤ y + t : add_le_add h1.le le_rfl }, { exact (lt_irrefl _ (h1.trans_le le_top)).elim } end @[simp] lemma add_eq_bot_iff {x y : ereal} : x + y = ⊥ ↔ x = ⊥ ∨ y = ⊥ := begin induction x using ereal.rec; induction y using ereal.rec; simp [← ereal.coe_add], end @[simp] lemma bot_lt_add_iff {x y : ereal} : ⊥ < x + y ↔ ⊥ < x ∧ ⊥ < y := by simp [bot_lt_iff_ne_bot, not_or_distrib] lemma add_lt_top {x y : ereal} (hx : x ≠ ⊤) (hy : y ≠ ⊤) : x + y < ⊤ := by { rw ← ereal.top_add_top, exact ereal.add_lt_add hx.lt_top hy.lt_top } /-! ### Negation -/ /-- negation on `ereal` -/ protected def neg : ereal → ereal | ⊥ := ⊤ | ⊤ := ⊥ | (x : ℝ) := (-x : ℝ) instance : has_neg ereal := ⟨ereal.neg⟩ instance : sub_neg_zero_monoid ereal := { neg_zero := by { change ((-0 : ℝ) : ereal) = 0, simp }, ..ereal.add_monoid, ..ereal.has_neg } @[norm_cast] protected lemma neg_def (x : ℝ) : ((-x : ℝ) : ereal) = -x := rfl @[simp] lemma neg_top : - (⊤ : ereal) = ⊥ := rfl @[simp] lemma neg_bot : - (⊥ : ereal) = ⊤ := rfl @[simp, norm_cast] lemma coe_neg (x : ℝ) : (↑(-x) : ereal) = -x := rfl @[simp, norm_cast] lemma coe_sub (x y : ℝ) : (↑(x - y) : ereal) = x - y := rfl @[norm_cast] lemma coe_zsmul (n : ℤ) (x : ℝ) : (↑(n • x) : ereal) = n • x := map_zsmul' (⟨coe, coe_zero, coe_add⟩ : ℝ →+ ereal) coe_neg _ _ instance : has_involutive_neg ereal := { neg := has_neg.neg, neg_neg := λ a, match a with | ⊥ := rfl | ⊤ := rfl | (a : ℝ) := by { norm_cast, simp [neg_neg a] } end } @[simp] lemma to_real_neg : ∀ {a : ereal}, to_real (-a) = - to_real a | ⊤ := by simp | ⊥ := by simp | (x : ℝ) := rfl @[simp] lemma neg_eq_top_iff {x : ereal} : - x = ⊤ ↔ x = ⊥ := neg_eq_iff_eq_neg @[simp] lemma neg_eq_bot_iff {x : ereal} : - x = ⊥ ↔ x = ⊤ := neg_eq_iff_eq_neg @[simp] lemma neg_eq_zero_iff {x : ereal} : - x = 0 ↔ x = 0 := by rw [neg_eq_iff_eq_neg, neg_zero] /-- if `-a ≤ b` then `-b ≤ a` on `ereal`. -/ protected theorem neg_le_of_neg_le {a b : ereal} (h : -a ≤ b) : -b ≤ a := begin induction a using ereal.rec; induction b using ereal.rec, { exact h }, { simpa only [coe_ne_top, neg_bot, top_le_iff] using h }, { exact bot_le }, { simpa only [coe_ne_top, le_bot_iff] using h }, { norm_cast at h ⊢, exact neg_le.1 h }, { exact bot_le }, { exact le_top }, { exact le_top }, { exact le_top }, end /-- `-a ≤ b ↔ -b ≤ a` on `ereal`. -/ protected theorem neg_le {a b : ereal} : -a ≤ b ↔ -b ≤ a := ⟨ereal.neg_le_of_neg_le, ereal.neg_le_of_neg_le⟩ /-- `a ≤ -b → b ≤ -a` on ereal -/ theorem le_neg_of_le_neg {a b : ereal} (h : a ≤ -b) : b ≤ -a := by rwa [←neg_neg b, ereal.neg_le, neg_neg] @[simp] lemma neg_le_neg_iff {a b : ereal} : - a ≤ - b ↔ b ≤ a := by conv_lhs { rw [ereal.neg_le, neg_neg] } /-- Negation as an order reversing isomorphism on `ereal`. -/ def neg_order_iso : ereal ≃o erealᵒᵈ := { to_fun := λ x, order_dual.to_dual (-x), inv_fun := λ x, -x.of_dual, map_rel_iff' := λ x y, neg_le_neg_iff, ..equiv.neg ereal } lemma neg_lt_of_neg_lt {a b : ereal} (h : -a < b) : -b < a := begin apply lt_of_le_of_ne (ereal.neg_le_of_neg_le h.le), assume H, rw [← H, neg_neg] at h, exact lt_irrefl _ h end lemma neg_lt_iff_neg_lt {a b : ereal} : -a < b ↔ -b < a := ⟨λ h, ereal.neg_lt_of_neg_lt h, λ h, ereal.neg_lt_of_neg_lt h⟩ /-! ### Subtraction Subtraction on `ereal` is defined by `x - y = x + (-y)`. Since addition is badly behaved at some points, so is subtraction. There is no standard algebraic typeclass involving subtraction that is registered on `ereal`, beyond `sub_neg_zero_monoid`, because of this bad behavior. -/ @[simp] lemma bot_sub (x : ereal) : ⊥ - x = ⊥ := bot_add x @[simp] lemma sub_top (x : ereal) : x - ⊤ = ⊥ := add_bot x @[simp] lemma top_sub_bot : (⊤ : ereal) - ⊥ = ⊤ := rfl @[simp] lemma top_sub_coe (x : ℝ) : (⊤ : ereal) - x = ⊤ := rfl @[simp] lemma coe_sub_bot (x : ℝ) : (x : ereal) - ⊥ = ⊤ := rfl lemma sub_le_sub {x y z t : ereal} (h : x ≤ y) (h' : t ≤ z) : x - z ≤ y - t := add_le_add h (neg_le_neg_iff.2 h') lemma sub_lt_sub_of_lt_of_le {x y z t : ereal} (h : x < y) (h' : z ≤ t) (hz : z ≠ ⊥) (ht : t ≠ ⊤) : x - t < y - z := add_lt_add_of_lt_of_le h (neg_le_neg_iff.2 h') (by simp [ht]) (by simp [hz]) lemma coe_real_ereal_eq_coe_to_nnreal_sub_coe_to_nnreal (x : ℝ) : (x : ereal) = real.to_nnreal x - real.to_nnreal (-x) := begin rcases le_or_lt 0 x with h|h, { have : real.to_nnreal x = ⟨x, h⟩, by { ext, simp [h] }, simp only [real.to_nnreal_of_nonpos (neg_nonpos.mpr h), this, sub_zero, ennreal.coe_zero, coe_ennreal_zero, coe_coe], refl }, { have : (x : ereal) = - (- x : ℝ), by simp, conv_lhs { rw this }, have : real.to_nnreal (-x) = ⟨-x, neg_nonneg.mpr h.le⟩, by { ext, simp [neg_nonneg.mpr h.le], }, simp only [real.to_nnreal_of_nonpos h.le, this, zero_sub, neg_inj, coe_neg, ennreal.coe_zero, coe_ennreal_zero, coe_coe], refl } end lemma to_real_sub {x y : ereal} (hx : x ≠ ⊤) (h'x : x ≠ ⊥) (hy : y ≠ ⊤) (h'y : y ≠ ⊥) : to_real (x - y) = to_real x - to_real y := begin rw [sub_eq_add_neg, to_real_add hx h'x, to_real_neg], { refl }, { simpa using hy }, { simpa using h'y } end /-! ### Multiplication -/ protected lemma mul_comm (x y : ereal) : x * y = y * x := begin induction x using ereal.rec; induction y using ereal.rec; try { refl }, dsimp only [(*)], simp only [ereal.mul, mul_comm], end @[simp] lemma top_mul_top : (⊤ : ereal) * ⊤ = ⊤ := rfl @[simp] lemma top_mul_bot : (⊤ : ereal) * ⊥ = ⊥ := rfl @[simp] lemma bot_mul_top : (⊥ : ereal) * ⊤ = ⊥ := rfl @[simp] lemma bot_mul_bot : (⊥ : ereal) * ⊥ = ⊤ := rfl lemma mul_top_of_pos {x : ereal} (h : 0 < x) : x * ⊤ = ⊤ := begin induction x using ereal.rec, { simpa only [not_lt_bot] using h }, { simp only [has_mul.mul, ereal.mul, ereal.coe_pos.1 h, if_true] }, { refl } end lemma mul_top_of_neg {x : ereal} (h : x < 0) : x * ⊤ = ⊥ := begin induction x using ereal.rec, { refl }, { simp only [ereal.coe_neg'] at h, simp only [has_mul.mul, ereal.mul, not_lt.2 h.le, h.ne, if_false] }, { simpa only [not_top_lt] using h } end lemma top_mul_of_pos {x : ereal} (h : 0 < x) : ⊤ * x = ⊤ := by { rw ereal.mul_comm, exact mul_top_of_pos h } lemma top_mul_of_neg {x : ereal} (h : x < 0) : ⊤ * x = ⊥ := by { rw ereal.mul_comm, exact mul_top_of_neg h } lemma coe_mul_top_of_pos {x : ℝ} (h : 0 < x) : (x : ereal) * ⊤ = ⊤ := mul_top_of_pos (ereal.coe_pos.2 h) lemma coe_mul_top_of_neg {x : ℝ} (h : x < 0) : (x : ereal) * ⊤ = ⊥ := mul_top_of_neg (ereal.coe_neg'.2 h) lemma top_mul_coe_of_pos {x : ℝ} (h : 0 < x) : (⊤ : ereal) * x = ⊤ := top_mul_of_pos (ereal.coe_pos.2 h) lemma top_mul_coe_of_neg {x : ℝ} (h : x < 0) : (⊤ : ereal) * x = ⊥ := top_mul_of_neg (ereal.coe_neg'.2 h) lemma mul_bot_of_pos {x : ereal} (h : 0 < x) : x * ⊥ = ⊥ := begin induction x using ereal.rec, { simpa only [not_lt_bot] using h }, { simp only [has_mul.mul, ereal.mul, ereal.coe_pos.1 h, if_true] }, { refl } end lemma mul_bot_of_neg {x : ereal} (h : x < 0) : x * ⊥ = ⊤ := begin induction x using ereal.rec, { refl }, { simp only [ereal.coe_neg'] at h, simp only [has_mul.mul, ereal.mul, not_lt.2 h.le, h.ne, if_false] }, { simpa only [not_top_lt] using h } end lemma bot_mul_of_pos {x : ereal} (h : 0 < x) : ⊥ * x = ⊥ := by { rw ereal.mul_comm, exact mul_bot_of_pos h } lemma bot_mul_of_neg {x : ereal} (h : x < 0) : ⊥ * x = ⊤ := by { rw ereal.mul_comm, exact mul_bot_of_neg h } lemma coe_mul_bot_of_pos {x : ℝ} (h : 0 < x) : (x : ereal) * ⊥ = ⊥ := mul_bot_of_pos (ereal.coe_pos.2 h) lemma coe_mul_bot_of_neg {x : ℝ} (h : x < 0) : (x : ereal) * ⊥ = ⊤ := mul_bot_of_neg (ereal.coe_neg'.2 h) lemma bot_mul_coe_of_pos {x : ℝ} (h : 0 < x) : (⊥ : ereal) * x = ⊥ := bot_mul_of_pos (ereal.coe_pos.2 h) lemma bot_mul_coe_of_neg {x : ℝ} (h : x < 0) : (⊥ : ereal) * x = ⊤ := bot_mul_of_neg (ereal.coe_neg'.2 h) lemma to_real_mul {x y : ereal} : to_real (x * y) = to_real x * to_real y := begin -- TODO: replace with `induction using` in Lean 4, which supports multiple premises with_cases { apply @induction₂ (λ x y, to_real (x * y) = to_real x * to_real y) }; propagate_tags { try { dsimp only} }, case [top_zero, bot_zero, zero_top, zero_bot] { all_goals { simp only [zero_mul, mul_zero, to_real_zero] } }, case coe_coe : x y { norm_cast }, case top_top { rw [top_mul_top, to_real_top, mul_zero] }, case top_bot { rw [top_mul_bot, to_real_top, to_real_bot, zero_mul] }, case bot_top { rw [bot_mul_top, to_real_bot, zero_mul] }, case bot_bot { rw [bot_mul_bot, to_real_top, to_real_bot, zero_mul] }, case pos_bot : x hx { rw [to_real_bot, to_real_coe, coe_mul_bot_of_pos hx, to_real_bot, mul_zero] }, case neg_bot : x hx { rw [to_real_bot, to_real_coe, coe_mul_bot_of_neg hx, to_real_top, mul_zero] }, case pos_top : x hx { rw [to_real_top, to_real_coe, coe_mul_top_of_pos hx, to_real_top, mul_zero] }, case neg_top : x hx { rw [to_real_top, to_real_coe, coe_mul_top_of_neg hx, to_real_bot, mul_zero] }, case top_pos : y hy { rw [to_real_top, to_real_coe, top_mul_coe_of_pos hy, to_real_top, zero_mul] }, case top_neg : y hy { rw [to_real_top, to_real_coe, top_mul_coe_of_neg hy, to_real_bot, zero_mul] }, case bot_pos : y hy { rw [to_real_bot, to_real_coe, bot_mul_coe_of_pos hy, to_real_bot, zero_mul] }, case bot_neg : y hy { rw [to_real_bot, to_real_coe, bot_mul_coe_of_neg hy, to_real_top, zero_mul] }, end protected lemma neg_mul (x y : ereal) : -x * y = -(x * y) := begin -- TODO: replace with `induction using` in Lean 4, which supports multiple premises with_cases { apply @induction₂ (λ x y, -x * y = -(x * y)) }; propagate_tags { try { dsimp only} }, case [top_top, bot_top, top_bot, bot_bot] { all_goals { refl } }, case [top_zero, bot_zero, zero_top, zero_bot] { all_goals { simp only [zero_mul, mul_zero, neg_zero] } }, case coe_coe : x y { norm_cast, exact neg_mul _ _, }, case pos_bot : x hx { rw [coe_mul_bot_of_pos hx, neg_bot, ← coe_neg, coe_mul_bot_of_neg (neg_neg_of_pos hx)] }, case neg_bot : x hx { rw [coe_mul_bot_of_neg hx, neg_top, ← coe_neg, coe_mul_bot_of_pos (neg_pos_of_neg hx)] }, case pos_top : x hx { rw [coe_mul_top_of_pos hx, neg_top, ← coe_neg, coe_mul_top_of_neg (neg_neg_of_pos hx)] }, case neg_top : x hx { rw [coe_mul_top_of_neg hx, neg_bot, ← coe_neg, coe_mul_top_of_pos (neg_pos_of_neg hx)] }, case top_pos : y hy { rw [top_mul_coe_of_pos hy, neg_top, bot_mul_coe_of_pos hy] }, case top_neg : y hy { rw [top_mul_coe_of_neg hy, neg_top, neg_bot, bot_mul_coe_of_neg hy] }, case bot_pos : y hy { rw [bot_mul_coe_of_pos hy, neg_bot, top_mul_coe_of_pos hy] }, case bot_neg : y hy { rw [bot_mul_coe_of_neg hy, neg_bot, neg_top, top_mul_coe_of_neg hy] }, end instance : has_distrib_neg ereal := { neg_mul := ereal.neg_mul, mul_neg := λ x y, by { rw [x.mul_comm, x.mul_comm], exact y.neg_mul x, }, ..ereal.has_involutive_neg } /-! ### Absolute value -/ /-- The absolute value from `ereal` to `ℝ≥0∞`, mapping `⊥` and `⊤` to `⊤` and a real `x` to `|x|`. -/ protected def abs : ereal → ℝ≥0∞ | ⊥ := ⊤ | ⊤ := ⊤ | (x : ℝ) := ennreal.of_real (|x|) @[simp] lemma abs_top : (⊤ : ereal).abs = ⊤ := rfl @[simp] lemma abs_bot : (⊥ : ereal).abs = ⊤ := rfl lemma abs_def (x : ℝ) : (x : ereal).abs = ennreal.of_real (|x|) := rfl lemma abs_coe_lt_top (x : ℝ) : (x : ereal).abs < ⊤ := ennreal.of_real_lt_top @[simp] lemma abs_eq_zero_iff {x : ereal} : x.abs = 0 ↔ x = 0 := begin induction x using ereal.rec, { simp only [abs_bot, ennreal.top_ne_zero, bot_ne_zero] }, { simp only [ereal.abs, coe_eq_zero, ennreal.of_real_eq_zero, abs_nonpos_iff] }, { simp only [abs_top, ennreal.top_ne_zero, top_ne_zero] } end @[simp] lemma abs_zero : (0 : ereal).abs = 0 := by rw [abs_eq_zero_iff] @[simp] lemma coe_abs (x : ℝ) : ((x : ereal).abs : ereal) = (|x| : ℝ) := by rcases lt_trichotomy 0 x with hx | rfl | hx; simp [abs_def] @[simp] lemma abs_mul (x y : ereal) : (x * y).abs = x.abs * y.abs := begin -- TODO: replace with `induction using` in Lean 4, which supports multiple premises with_cases { apply @induction₂ (λ x y, (x * y).abs = x.abs * y.abs) }; propagate_tags { try { dsimp only} }, case [top_top, bot_top, top_bot, bot_bot] { all_goals { refl } }, case [top_zero, bot_zero, zero_top, zero_bot] { all_goals { simp only [zero_mul, mul_zero, abs_zero] } }, case coe_coe : x y { simp only [← coe_mul, ereal.abs, abs_mul, ennreal.of_real_mul (abs_nonneg _)], }, case pos_bot : x hx { simp only [coe_mul_bot_of_pos hx, hx.ne', abs_bot, with_top.mul_top, ne.def, abs_eq_zero_iff, coe_eq_zero, not_false_iff] }, case neg_bot : x hx { simp only [coe_mul_bot_of_neg hx, hx.ne, abs_bot, with_top.mul_top, ne.def, abs_eq_zero_iff, coe_eq_zero, not_false_iff, abs_top] }, case pos_top : x hx { simp only [coe_mul_top_of_pos hx, hx.ne', with_top.mul_top, ne.def, abs_eq_zero_iff, coe_eq_zero, not_false_iff, abs_top] }, case neg_top : x hx { simp only [coe_mul_top_of_neg hx, hx.ne, abs_bot, with_top.mul_top, ne.def, abs_eq_zero_iff, coe_eq_zero, not_false_iff, abs_top] }, case top_pos : y hy { simp only [top_mul_coe_of_pos hy, hy.ne', with_top.top_mul, ne.def, abs_eq_zero_iff, coe_eq_zero, not_false_iff, abs_top] }, case top_neg : y hy { simp only [top_mul_coe_of_neg hy, hy.ne, abs_bot, with_top.top_mul, ne.def, abs_eq_zero_iff, coe_eq_zero, not_false_iff, abs_top] }, case bot_pos : y hy { simp only [bot_mul_coe_of_pos hy, hy.ne', abs_bot, with_top.top_mul, ne.def, abs_eq_zero_iff, coe_eq_zero, not_false_iff] }, case bot_neg : y hy { simp only [bot_mul_coe_of_neg hy, hy.ne, abs_bot, with_top.top_mul, ne.def, abs_eq_zero_iff, coe_eq_zero, not_false_iff, abs_top] }, end /-! ### Sign -/ @[simp] lemma sign_top : sign (⊤ : ereal) = 1 := rfl @[simp] lemma sign_bot : sign (⊥ : ereal) = -1 := rfl @[simp] lemma sign_coe (x : ℝ) : sign (x : ereal) = sign x := by simp only [sign, order_hom.coe_fun_mk, ereal.coe_pos, ereal.coe_neg'] @[simp] lemma sign_mul (x y : ereal) : sign (x * y) = sign x * sign y := begin -- TODO: replace with `induction using` in Lean 4, which supports multiple premises with_cases { apply @induction₂ (λ x y, sign (x * y) = sign x * sign y) }; propagate_tags { try { dsimp only} }, case [top_top, bot_top, top_bot, bot_bot] { all_goals { refl } }, case [top_zero, bot_zero, zero_top, zero_bot] { all_goals { simp only [zero_mul, mul_zero, sign_zero] } }, case coe_coe : x y { simp only [← coe_mul, sign_coe, sign_mul], }, case pos_bot : x hx { simp_rw [coe_mul_bot_of_pos hx, sign_coe, sign_pos hx, one_mul] }, case neg_bot : x hx { simp_rw [coe_mul_bot_of_neg hx, sign_coe, sign_neg hx, sign_top, sign_bot, neg_one_mul, neg_neg] }, case pos_top : x hx { simp_rw [coe_mul_top_of_pos hx, sign_coe, sign_pos hx, one_mul] }, case neg_top : x hx { simp_rw [coe_mul_top_of_neg hx, sign_coe, sign_neg hx, sign_top, sign_bot, mul_one] }, case top_pos : y hy { simp_rw [top_mul_coe_of_pos hy, sign_coe, sign_pos hy, mul_one] }, case top_neg : y hy { simp_rw [top_mul_coe_of_neg hy, sign_coe, sign_neg hy, sign_top, sign_bot, one_mul] }, case bot_pos : y hy { simp_rw [bot_mul_coe_of_pos hy, sign_coe, sign_pos hy, mul_one] }, case bot_neg : y hy { simp_rw [bot_mul_coe_of_neg hy, sign_coe, sign_neg hy, sign_top, sign_bot, neg_one_mul, neg_neg] }, end lemma sign_mul_abs (x : ereal) : (sign x * x.abs : ereal) = x := begin induction x using ereal.rec, { simp }, { rcases lt_trichotomy 0 x with hx | rfl | hx, { simp [sign_pos hx, abs_of_pos hx] }, { simp }, { simp [sign_neg hx, abs_of_neg hx] } }, { simp } end lemma sign_eq_and_abs_eq_iff_eq {x y : ereal} : (x.abs = y.abs ∧ sign x = sign y) ↔ x = y := begin split, { rintros ⟨habs, hsign⟩, rw [← x.sign_mul_abs, ← y.sign_mul_abs, habs, hsign] }, { rintros rfl, simp only [eq_self_iff_true, and_self] } end lemma le_iff_sign {x y : ereal} : x ≤ y ↔ sign x < sign y ∨ sign x = sign_type.neg ∧ sign y = sign_type.neg ∧ y.abs ≤ x.abs ∨ sign x = sign_type.zero ∧ sign y = sign_type.zero ∨ sign x = sign_type.pos ∧ sign y = sign_type.pos ∧ x.abs ≤ y.abs := begin split, { intro h, rcases (sign.monotone h).lt_or_eq with hs | hs, { exact or.inl hs }, { rw [← x.sign_mul_abs, ← y.sign_mul_abs] at h, cases sign y; rw [hs] at *, { simp }, { simp at ⊢ h, exact or.inl h }, { simpa using h, }, }, }, { rintros (h | h | h | h), { exact (sign.monotone.reflect_lt h).le, }, all_goals { rw [← x.sign_mul_abs, ← y.sign_mul_abs], simp [h] } } end instance : comm_monoid_with_zero ereal := { mul_assoc := λ x y z, begin rw [← sign_eq_and_abs_eq_iff_eq], simp only [mul_assoc, abs_mul, eq_self_iff_true, sign_mul, and_self], end, mul_comm := ereal.mul_comm, ..ereal.has_mul, ..ereal.has_one, ..ereal.has_zero, ..ereal.mul_zero_one_class } instance : pos_mul_mono ereal := pos_mul_mono_iff_covariant_pos.2 ⟨begin rintros ⟨x, x0⟩ a b h, dsimp, rcases le_iff_sign.mp h with h | h | h | h, { rw [le_iff_sign], left, simp [sign_pos x0, h] }, all_goals { rw [← x.sign_mul_abs, ← a.sign_mul_abs, ← b.sign_mul_abs, sign_pos x0], simp only [h], dsimp, simp only [neg_mul, mul_neg, ereal.neg_le_neg_iff, one_mul, le_refl, zero_mul, mul_zero] }, all_goals { norm_cast, exact mul_le_mul_left' h.2.2 _, }, end⟩ instance : mul_pos_mono ereal := pos_mul_mono_iff_mul_pos_mono.1 ereal.pos_mul_mono instance : pos_mul_reflect_lt ereal := pos_mul_mono.to_pos_mul_reflect_lt instance : mul_pos_reflect_lt ereal := mul_pos_mono.to_mul_pos_reflect_lt @[simp, norm_cast] lemma coe_pow (x : ℝ) (n : ℕ) : (↑(x ^ n) : ereal) = x ^ n := map_pow (⟨coe, coe_one, coe_mul⟩ : ℝ →* ereal) _ _ @[simp, norm_cast] lemma coe_ennreal_pow (x : ℝ≥0∞) (n : ℕ) : (↑(x ^ n) : ereal) = x ^ n := map_pow (⟨coe, coe_ennreal_one, coe_ennreal_mul⟩ : ℝ≥0∞ →* ereal) _ _ end ereal namespace tactic open positivity private lemma ereal_coe_ne_zero {r : ℝ} : r ≠ 0 → (r : ereal) ≠ 0 := ereal.coe_ne_zero.2 private lemma ereal_coe_nonneg {r : ℝ} : 0 ≤ r → 0 ≤ (r : ereal) := ereal.coe_nonneg.2 private lemma ereal_coe_pos {r : ℝ} : 0 < r → 0 < (r : ereal) := ereal.coe_pos.2 private lemma ereal_coe_ennreal_pos {r : ℝ≥0∞} : 0 < r → 0 < (r : ereal) := ereal.coe_ennreal_pos.2 /-- Extension for the `positivity` tactic: cast from `ℝ` to `ereal`. -/ @[positivity] meta def positivity_coe_real_ereal : expr → tactic strictness | `(@coe _ _ %%inst %%a) := do unify inst `(@coe_to_lift _ _ $ @coe_base _ _ ereal.has_coe), strictness_a ← core a, match strictness_a with | positive p := positive <$> mk_app ``ereal_coe_pos [p] | nonnegative p := nonnegative <$> mk_mapp ``ereal_coe_nonneg [a, p] | nonzero p := nonzero <$> mk_mapp ``ereal_coe_ne_zero [a, p] end | e := pp e >>= fail ∘ format.bracket "The expression " " is not of the form `(r : ereal)` for `r : ℝ`" /-- Extension for the `positivity` tactic: cast from `ℝ≥0∞` to `ereal`. -/ @[positivity] meta def positivity_coe_ennreal_ereal : expr → tactic strictness | `(@coe _ _ %%inst %%a) := do unify inst `(@coe_to_lift _ _ $ @coe_base _ _ ereal.has_coe_ennreal), strictness_a ← core a, match strictness_a with | positive p := positive <$> mk_app ``ereal_coe_ennreal_pos [p] | _ := nonnegative <$> mk_mapp `ereal.coe_ennreal_nonneg [a] end | e := pp e >>= fail ∘ format.bracket "The expression " " is not of the form `(r : ereal)` for `r : ℝ≥0∞`" end tactic
81844f2c27bc0a47604952d32495206c266ee6f2
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/meta/constructor_tactic.lean
28d07ab5944b052220c6ac478c5c0762754c70bd
[ "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
3,281
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.tactic init.function namespace tactic /-- Return target after instantiating metavars and whnf -/ private meta def target' : tactic expr := target >>= instantiate_mvars >>= whnf meta def get_constructors_for (e : expr) : tactic (list name) := do env ← get_env, I ← return e.extract_opt_auto_param.get_app_fn.const_name, when (¬env.is_inductive I) (fail "constructor tactic failed, target is not an inductive datatype"), return $ env.constructors_of I private meta def try_constructors (cfg : apply_cfg): list name → tactic (list (name × expr)) | [] := fail "constructor tactic failed, none of the constructors is applicable" | (c::cs) := (mk_const c >>= λ e, apply e cfg) <|> try_constructors cs meta def constructor (cfg : apply_cfg := {}): tactic (list (name × expr)) := target' >>= get_constructors_for >>= try_constructors cfg meta def econstructor : tactic (list (name × expr)) := constructor {new_goals := new_goals.non_dep_only} meta def fconstructor : tactic (list (name × expr)) := constructor {new_goals := new_goals.all} meta def left : tactic (list (name × expr)) := do tgt ← target', [c₁, c₂] ← get_constructors_for tgt | fail "left tactic failed, target is not an inductive datatype with two constructors", mk_const c₁ >>= apply meta def right : tactic (list (name × expr)) := do tgt ← target', [c₁, c₂] ← get_constructors_for tgt | fail "left tactic failed, target is not an inductive datatype with two constructors", mk_const c₂ >>= apply meta def constructor_idx (idx : nat) : tactic (list (name × expr)) := do cs ← target' >>= get_constructors_for, some c ← return $ cs.nth (idx - 1) | fail "constructor_idx tactic failed, target is an inductive datatype, but it does not have sufficient constructors", mk_const c >>= apply meta def split : tactic (list (name × expr)) := do [c] ← target' >>= get_constructors_for | fail "split tactic failed, target is not an inductive datatype with only one constructor", mk_const c >>= apply open expr private meta def apply_num_metavars : expr → expr → nat → tactic expr | f ftype 0 := return f | f ftype (n+1) := do pi m bi d b ← whnf ftype, a ← mk_meta_var d, new_f ← return $ f a, new_ftype ← return $ b.instantiate_var a, apply_num_metavars new_f new_ftype n meta def existsi (e : expr) : tactic unit := do [c] ← target' >>= get_constructors_for | fail "existsi tactic failed, target is not an inductive datatype with only one constructor", fn ← mk_const c, fn_type ← infer_type fn, n ← get_arity fn, when (n < 2) (fail "existsi tactic failed, constructor must have at least two arguments"), t ← apply_num_metavars fn fn_type (n - 2), eapply (app t e), t_type ← infer_type t >>= whnf, e_type ← infer_type e, (guard t_type.is_pi <|> fail "existsi tactic failed, failed to infer type"), (unify t_type.binding_domain e_type <|> fail "existsi tactic failed, type mismatch between given term witness and expected type") end tactic
669b99eb00a266a389ad6f4a680d10880218c9c4
86ee6b815fda4c9a5aa42e17b61336a913552e60
/src/tactics/equality.lean
4a8e54f125312ed7d63c0e87073ea2ff5438a38a
[ "Apache-2.0" ]
permissive
fgdorais/lean-groupoids
6487f6d84427a757708fd4e795038ab159cc4f2c
54d1582f8d6ae642fc818e7672ac1c49fe7733e5
refs/heads/master
1,587,778,056,386
1,551,055,383,000
1,551,055,383,000
172,408,995
0
0
null
null
null
null
UTF-8
Lean
false
false
2,249
lean
/- Copyright © 2019 François G. Dorais. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. -/ import .groupoids.paths namespace tactic meta def match_ne' : expr → tactic (expr × expr) | `(%%a ≠ %%b) := return (a,b) | `(¬ %%a = %%b) := return (a,b) | _ := failed meta def eq_groupoid : groupoid (λ x y : expr, tactic expr) := { id := mk_eq_refl , inv := λ _ _ g, g >>= mk_eq_symm , comp := λ _ _ _ g h, g >>= λ g, h >>= λ h, mk_eq_trans g h } meta def eq_state := path_state eq_groupoid meta def eq_gather : list expr → tactic eq_state | [] := return [] | (e :: es) := do { `(%%a = %%b) ← infer_type e, eq_gather es >>= λ ps, return $ path_state.intro (path.mk eq_groupoid a b (return e)) ps } <|> eq_gather es meta def eq_find (eqs : eq_state) : expr → expr → tactic expr := λ a b, eqs.find a b >>= λ e, e meta def eq_solve (eqs : eq_state) : tactic unit := do { `(%%a = %%b) ← target, eq_find eqs a b >>= exact } meta def eq_rewrite : eq_state → tactic unit | [] := skip | (p :: ps) := repeat (p.map >>= λ e, rewrite_target e {symm:=tt}) >> eq_rewrite ps meta def eq_contra (eqs : eq_state) : list expr → tactic unit | [] := failed | (h :: hs) := do { (a,b) ← infer_type h >>= match_ne', e ← eq_find eqs a b, to_expr ``(absurd %%e %%h) >>= exact } <|> eq_contra hs meta def eq_path_to_string : path eq_groupoid → tactic string | ⟨_,a,b,e⟩ := e >>= λ e, return $ to_string a ++ " = " ++ to_string b ++ " := " ++ to_string e meta def eq_state_to_string : eq_state → tactic string | [] := return "" | (p :: ps) := eq_path_to_string p >>= λ s, eq_state_to_string ps >>= λ t, return $ s ++ "; " ++ t meta def eq_trace (eqs : eq_state) : tactic unit := eq_state_to_string eqs >>= trace meta def interactive.eq_trace : tactic unit := local_context >>= eq_gather >>= eq_trace meta def interactive.eq_solve : tactic unit := local_context >>= eq_gather >>= eq_solve meta def interactive.eq_rewrite : tactic unit := local_context >>= eq_gather >>= eq_rewrite meta def interactive.eq_contradiction : tactic unit := do { by_contradiction, ctx ← local_context, eqs ← eq_gather ctx, eq_contra eqs ctx } end tactic
a4da74f1a0f1eede3cefb28b790058c1c2a9a97f
5bf112cf7101c6c6303dc3fd0b3179c860e61e56
/lean/background/util.lean
ccbbc63a0aea1424b47b9fd770f2314b381bd40e
[ "Apache-2.0" ]
permissive
fredfeng/formal-encoding
7ab645f49a553dfad2af03fcb4289e40fc679759
024efcf58672ac6b817caa10dfe8cd9708b07f1b
refs/heads/master
1,597,236,551,123
1,568,832,149,000
1,568,832,149,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,254
lean
axiom SKIP {X : Type} : X axiom SORRY {P : Prop} : P namespace Nat def fact : Nat -> Nat | 0 => 1 | 1 => 1 | n+1 => (n+1) * fact n def choose (n : Nat) (k : Fin n) : Nat := n.fact / (k.val.fact * (n-k.val).fact) end Nat namespace Fin def cadd {n : Nat} {k₁ k₂ : Fin n} : Fin n := ⟨(k₁.val + k₂.val) % n, SORRY⟩ notation `⊕` := cadd end Fin namespace List def rangeFinAux (n : Nat) : ∀ (k : Nat), k ≤ n → List (Fin n) → List (Fin n) | 0, H, ns => ns | k+1, H, ns => let H₁ : k < n := H; let H₂ : k ≤ n := SORRY; rangeFinAux k H₂ (⟨k, H₁⟩ :: ns) def rangeFin (n : Nat) : List (Fin n) := rangeFinAux n n (Nat.leRefl n) [] def range1 (n : Nat) : List Nat := List.tail (List.range $ n+1) def sublists {X : Type} : Nat -> List X -> List (List X) | 0, xs => [[]] | k+1, [] => [] | k+1, (x::xs) => (sublists k xs).map (λ ys => x :: ys) ++ sublists (k+1) xs def sum {X : Type} [HasZero X] [HasAdd X] (xs : List X) : X := xs.foldl HasAdd.add 0 def prod {X : Type} [HasOne X] [HasMul X] (xs : List X) : X := xs.foldl HasMul.mul 1 def max {X : Type} (lt : X -> X -> Bool) (x₀ : X) (xs : List X) := xs.foldl (λ xₘ x => if lt xₘ x then x else xₘ) x₀ end List
1a7611f775f8dd4fa2f7d3fed42dd0585a2f6843
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/order/category/omega_complete_partial_order.lean
65d74186c9172b46ea0e6090c17f30d87fc2de7d
[ "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,223
lean
/- Copyright (c) 2020 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import order.omega_complete_partial_order import category_theory.limits.shapes.products import category_theory.limits.shapes.equalizers import category_theory.limits.constructions.limits_of_products_and_equalizers import category_theory.concrete_category.bundled_hom /-! # Category of types with a omega complete partial order In this file, we bundle the class `omega_complete_partial_order` into a concrete category and prove that continuous functions also form a `omega_complete_partial_order`. ## Main definitions * `ωCPO` * an instance of `category` and `concrete_category` -/ open category_theory universes u v /-- The category of types with a omega complete partial order. -/ def ωCPO : Type (u+1) := bundled omega_complete_partial_order namespace ωCPO open omega_complete_partial_order instance : bundled_hom @continuous_hom := { to_fun := @continuous_hom.simps.apply, id := @continuous_hom.id, comp := @continuous_hom.comp, hom_ext := @continuous_hom.coe_inj } attribute [derive [large_category, concrete_category]] ωCPO instance : has_coe_to_sort ωCPO Type* := bundled.has_coe_to_sort /-- Construct a bundled ωCPO from the underlying type and typeclass. -/ def of (α : Type*) [omega_complete_partial_order α] : ωCPO := bundled.of α @[simp] lemma coe_of (α : Type*) [omega_complete_partial_order α] : ↥(of α) = α := rfl instance : inhabited ωCPO := ⟨of punit⟩ instance (α : ωCPO) : omega_complete_partial_order α := α.str section open category_theory.limits namespace has_products /-- The pi-type gives a cone for a product. -/ def product {J : Type v} (f : J → ωCPO.{v}) : fan f := fan.mk (of (Π j, f j)) (λ j, continuous_hom.of_mono (pi.eval_order_hom j) (λ c, rfl)) /-- The pi-type is a limit cone for the product. -/ def is_product (J : Type v) (f : J → ωCPO) : is_limit (product f) := { lift := λ s, ⟨⟨λ t j, s.π.app ⟨j⟩ t, λ x y h j, (s.π.app ⟨j⟩).monotone h⟩, λ x, funext (λ j, (s.π.app ⟨j⟩).continuous x)⟩, uniq' := λ s m w, begin ext t j, change m t j = s.π.app ⟨j⟩ t, rw ← w ⟨j⟩, refl, end, fac' := λ s j, by { cases j, tidy, } }. instance (J : Type v) (f : J → ωCPO.{v}) : has_product f := has_limit.mk ⟨_, is_product _ f⟩ end has_products instance omega_complete_partial_order_equalizer {α β : Type*} [omega_complete_partial_order α] [omega_complete_partial_order β] (f g : α →𝒄 β) : omega_complete_partial_order {a : α // f a = g a} := omega_complete_partial_order.subtype _ $ λ c hc, begin rw [f.continuous, g.continuous], congr' 1, ext, apply hc _ ⟨_, rfl⟩, end namespace has_equalizers /-- The equalizer inclusion function as a `continuous_hom`. -/ def equalizer_ι {α β : Type*} [omega_complete_partial_order α] [omega_complete_partial_order β] (f g : α →𝒄 β) : {a : α // f a = g a} →𝒄 α := continuous_hom.of_mono (order_hom.subtype.val _) (λ c, rfl) /-- A construction of the equalizer fork. -/ def equalizer {X Y : ωCPO.{v}} (f g : X ⟶ Y) : fork f g := @fork.of_ι _ _ _ _ _ _ (ωCPO.of {a // f a = g a}) (equalizer_ι f g) (continuous_hom.ext _ _ (λ x, x.2)) /-- The equalizer fork is a limit. -/ def is_equalizer {X Y : ωCPO.{v}} (f g : X ⟶ Y) : is_limit (equalizer f g) := fork.is_limit.mk' _ $ λ s, ⟨{ to_fun := λ x, ⟨s.ι x, by apply continuous_hom.congr_fun s.condition⟩, monotone' := λ x y h, s.ι.monotone h, cont := λ x, subtype.ext (s.ι.continuous x) }, by { ext, refl }, λ m hm, begin ext, apply continuous_hom.congr_fun hm, end⟩ end has_equalizers instance : has_products.{v} ωCPO.{v} := λ J, { has_limit := λ F, has_limit_of_iso discrete.nat_iso_functor.symm } instance {X Y : ωCPO.{v}} (f g : X ⟶ Y) : has_limit (parallel_pair f g) := has_limit.mk ⟨_, has_equalizers.is_equalizer f g⟩ instance : has_equalizers ωCPO.{v} := has_equalizers_of_has_limit_parallel_pair _ instance : has_limits ωCPO.{v} := has_limits_of_has_equalizers_and_products end end ωCPO
112dee5d8a80bb860df773eef1a7e98c9949d0d7
ee8cdbabf07f77e7be63a449b8483ce308d37218
/lean/src/test/amc12-2000-p6.lean
1e18df28e574630ac6df7d43b0ff013ca961943e
[ "MIT", "Apache-2.0" ]
permissive
zeta1999/miniF2F
6d66c75d1c18152e224d07d5eed57624f731d4b7
c1ba9629559c5273c92ec226894baa0c1ce27861
refs/heads/main
1,681,897,460,642
1,620,646,361,000
1,620,646,361,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
333
lean
/- Copyright (c) 2021 OpenAI. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kunhao Zheng -/ import data.nat.prime example (p q : ℕ) (h₀ : nat.prime p ∧ nat.prime q) (h₁ : 4 ≤ p ∧ p ≤ 18) (h₂ : 4 ≤ q ∧ q ≤ 18) : p * q - ( p + q ) ≠ 194 := begin sorry end
cadd55482c0359d8c91dd0a0875d8f35b543d605
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/algebra/module/submodule_lattice.lean
5701c0062d2ab7bcff285f02e200bbbe53dc677a
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,174
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, Kevin Buzzard, Yury Kudryashov -/ import algebra.module.submodule import algebra.punit_instances /-! # The lattice structure on `submodule`s This file defines the lattice structure on submodules, `submodule.complete_lattice`, with `⊥` defined as `{0}` and `⊓` defined as intersection of the underlying carrier. If `p` and `q` are submodules of a module, `p ≤ q` means that `p ⊆ q`. Many results about operations on this lattice structure are defined in `linear_algebra/basic.lean`, most notably those which use `span`. ## Implementation notes This structure should match the `add_submonoid.complete_lattice` structure, and we should try to unify the APIs where possible. -/ variables {R S M : Type*} section add_comm_monoid variables [semiring R] [semiring S] [add_comm_monoid M] [module R M] [module S M] variables [has_scalar S R] [is_scalar_tower S R M] variables {p q : submodule R M} namespace submodule /-- The set `{0}` is the bottom element of the lattice of submodules. -/ instance : has_bot (submodule R M) := ⟨{ carrier := {0}, smul_mem' := by simp { contextual := tt }, .. (⊥ : add_submonoid M)}⟩ instance inhabited' : inhabited (submodule R M) := ⟨⊥⟩ @[simp] lemma bot_coe : ((⊥ : submodule R M) : set M) = {0} := rfl @[simp] lemma bot_to_add_submonoid : (⊥ : submodule R M).to_add_submonoid = ⊥ := rfl section variables (R) @[simp] lemma restrict_scalars_bot : restrict_scalars S (⊥ : submodule R M) = ⊥ := rfl @[simp] lemma mem_bot {x : M} : x ∈ (⊥ : submodule R M) ↔ x = 0 := set.mem_singleton_iff end instance unique_bot : unique (⊥ : submodule R M) := ⟨infer_instance, λ x, subtype.ext $ (mem_bot R).1 x.mem⟩ lemma nonzero_mem_of_bot_lt {I : submodule R M} (bot_lt : ⊥ < I) : ∃ a : I, a ≠ 0 := begin have h := (set_like.lt_iff_le_and_exists.1 bot_lt).2, tidy, end instance : order_bot (submodule R M) := { bot := ⊥, bot_le := λ p x, by simp {contextual := tt}, ..set_like.partial_order } protected lemma eq_bot_iff (p : submodule R M) : p = ⊥ ↔ ∀ x ∈ p, x = (0 : M) := ⟨ λ h, h.symm ▸ λ x hx, (mem_bot R).mp hx, λ h, eq_bot_iff.mpr (λ x hx, (mem_bot R).mpr (h x hx)) ⟩ @[ext] protected lemma bot_ext (x y : (⊥ : submodule R M)) : x = y := begin rcases x with ⟨x, xm⟩, rcases y with ⟨y, ym⟩, congr, rw (submodule.eq_bot_iff _).mp rfl x xm, rw (submodule.eq_bot_iff _).mp rfl y ym, end protected lemma ne_bot_iff (p : submodule R M) : p ≠ ⊥ ↔ ∃ x ∈ p, x ≠ (0 : M) := by { haveI := classical.prop_decidable, simp_rw [ne.def, p.eq_bot_iff, not_forall] } /-- The bottom submodule is linearly equivalent to punit as an `R`-module. -/ @[simps] def bot_equiv_punit : (⊥ : submodule R M) ≃ₗ[R] punit := { to_fun := λ x, punit.star, inv_fun := λ x, 0, map_add' := by { intros, ext, }, map_smul' := by { intros, ext, }, left_inv := by { intro x, ext, }, right_inv := by { intro x, ext, }, } /-- The universal set is the top element of the lattice of submodules. -/ instance : has_top (submodule R M) := ⟨{ carrier := set.univ, smul_mem' := λ _ _ _, trivial, .. (⊤ : add_submonoid M)}⟩ @[simp] lemma top_coe : ((⊤ : submodule R M) : set M) = set.univ := rfl @[simp] lemma top_to_add_submonoid : (⊤ : submodule R M).to_add_submonoid = ⊤ := rfl @[simp] lemma mem_top {x : M} : x ∈ (⊤ : submodule R M) := trivial section variables (R) @[simp] lemma restrict_scalars_top : restrict_scalars S (⊤ : submodule R M) = ⊤ := rfl end instance : order_top (submodule R M) := { top := ⊤, le_top := λ p x _, trivial, ..set_like.partial_order } lemma eq_top_iff' {p : submodule R M} : p = ⊤ ↔ ∀ x, x ∈ p := eq_top_iff.trans ⟨λ h x, h trivial, λ h x _, h x⟩ /-- The top submodule is linearly equivalent to the module. -/ @[simps] def top_equiv_self : (⊤ : submodule R M) ≃ₗ[R] M := { to_fun := λ x, x, inv_fun := λ x, ⟨x, by simp⟩, map_add' := by { intros, refl, }, map_smul' := by { intros, refl, }, left_inv := by { intro x, ext, refl, }, right_inv := by { intro x, refl, }, } instance : has_Inf (submodule R M) := ⟨λ S, { carrier := ⋂ s ∈ S, (s : set M), zero_mem' := by simp, add_mem' := by simp [add_mem] {contextual := tt}, smul_mem' := by simp [smul_mem] {contextual := tt} }⟩ private lemma Inf_le' {S : set (submodule R M)} {p} : p ∈ S → Inf S ≤ p := set.bInter_subset_of_mem private lemma le_Inf' {S : set (submodule R M)} {p} : (∀q ∈ S, p ≤ q) → p ≤ Inf S := set.subset_bInter instance : has_inf (submodule R M) := ⟨λ p q, { carrier := p ∩ q, zero_mem' := by simp, add_mem' := by simp [add_mem] {contextual := tt}, smul_mem' := by simp [smul_mem] {contextual := tt} }⟩ instance : complete_lattice (submodule R M) := { sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x}, le_sup_left := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, ha, le_sup_right := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, hb, sup_le := λ a b c h₁ h₂, Inf_le' ⟨h₁, h₂⟩, inf := (⊓), le_inf := λ a b c, set.subset_inter, inf_le_left := λ a b, set.inter_subset_left _ _, inf_le_right := λ a b, set.inter_subset_right _ _, Sup := λtt, Inf {t | ∀t'∈tt, t' ≤ t}, le_Sup := λ s p hs, le_Inf' $ λ q hq, hq _ hs, Sup_le := λ s p hs, Inf_le' hs, Inf := Inf, le_Inf := λ s a, le_Inf', Inf_le := λ s a, Inf_le', ..submodule.order_top, ..submodule.order_bot } @[simp] theorem inf_coe : (p ⊓ q : set M) = p ∩ q := rfl @[simp] theorem mem_inf {p q : submodule R M} {x : M} : x ∈ p ⊓ q ↔ x ∈ p ∧ x ∈ q := iff.rfl @[simp] theorem Inf_coe (P : set (submodule R M)) : (↑(Inf P) : set M) = ⋂ p ∈ P, ↑p := rfl @[simp] theorem infi_coe {ι} (p : ι → submodule R M) : (↑⨅ i, p i : set M) = ⋂ i, ↑(p i) := by rw [infi, Inf_coe]; ext a; simp; exact ⟨λ h i, h _ i rfl, λ h i x e, e ▸ h _⟩ @[simp] lemma mem_Inf {S : set (submodule R M)} {x : M} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_bInter_iff @[simp] theorem mem_infi {ι} (p : ι → submodule R M) {x} : x ∈ (⨅ i, p i) ↔ ∀ i, x ∈ p i := by rw [← set_like.mem_coe, infi_coe, set.mem_Inter]; refl lemma mem_sup_left {S T : submodule R M} : ∀ {x : M}, x ∈ S → x ∈ S ⊔ T := show S ≤ S ⊔ T, from le_sup_left lemma mem_sup_right {S T : submodule R M} : ∀ {x : M}, x ∈ T → x ∈ S ⊔ T := show T ≤ S ⊔ T, from le_sup_right lemma mem_supr_of_mem {ι : Sort*} {b : M} {p : ι → submodule R M} (i : ι) (h : b ∈ p i) : b ∈ (⨆i, p i) := have p i ≤ (⨆i, p i) := le_supr p i, @this b h /-! Note that `submodule.mem_supr` is provided in `linear_algebra/basic.lean`. -/ lemma mem_Sup_of_mem {S : set (submodule R M)} {s : submodule R M} (hs : s ∈ S) : ∀ {x : M}, x ∈ s → x ∈ Sup S := show s ≤ Sup S, from le_Sup hs end submodule section nat_submodule /-- An additive submonoid is equivalent to a ℕ-submodule. -/ def add_submonoid.to_nat_submodule : add_submonoid M ≃o submodule ℕ M := { to_fun := λ S, { smul_mem' := λ r s hs, S.nsmul_mem hs _, ..S }, inv_fun := submodule.to_add_submonoid, left_inv := λ ⟨S, _, _⟩, rfl, right_inv := λ ⟨S, _, _, _⟩, rfl, map_rel_iff' := λ a b, iff.rfl } @[simp] lemma add_submonoid.to_nat_submodule_symm : ⇑(add_submonoid.to_nat_submodule.symm : _ ≃o add_submonoid M) = submodule.to_add_submonoid := rfl @[simp] lemma add_submonoid.coe_to_nat_submodule (S : add_submonoid M) : (S.to_nat_submodule : set M) = S := rfl @[simp] lemma add_submonoid.to_nat_submodule_to_add_submonoid (S : add_submonoid M) : S.to_nat_submodule.to_add_submonoid = S := add_submonoid.to_nat_submodule.symm_apply_apply S @[simp] lemma submodule.to_add_submonoid_to_nat_submodule (S : submodule ℕ M) : S.to_add_submonoid.to_nat_submodule = S := add_submonoid.to_nat_submodule.apply_symm_apply S end nat_submodule end add_comm_monoid
2c6e723934a08e457a0dcf7d027a8bf6661874ff
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebraic_geometry/AffineScheme.lean
827f2d0f8852ba930573a8779d5263cd60b1ba2a
[ "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
32,256
lean
/- Copyright (c) 2022 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import algebraic_geometry.Gamma_Spec_adjunction import algebraic_geometry.open_immersion.Scheme import category_theory.limits.opposites import ring_theory.localization.inv_submonoid /-! # Affine schemes > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We define the category of `AffineScheme`s as the essential image of `Spec`. We also define predicates about affine schemes and affine open sets. ## Main definitions * `algebraic_geometry.AffineScheme`: The category of affine schemes. * `algebraic_geometry.is_affine`: A scheme is affine if the canonical map `X ⟶ Spec Γ(X)` is an isomorphism. * `algebraic_geometry.Scheme.iso_Spec`: The canonical isomorphism `X ≅ Spec Γ(X)` for an affine scheme. * `algebraic_geometry.AffineScheme.equiv_CommRing`: The equivalence of categories `AffineScheme ≌ CommRingᵒᵖ` given by `AffineScheme.Spec : CommRingᵒᵖ ⥤ AffineScheme` and `AffineScheme.Γ : AffineSchemeᵒᵖ ⥤ CommRing`. * `algebraic_geometry.is_affine_open`: An open subset of a scheme is affine if the open subscheme is affine. * `algebraic_geometry.is_affine_open.from_Spec`: The immersion `Spec 𝒪ₓ(U) ⟶ X` for an affine `U`. -/ noncomputable theory open category_theory category_theory.limits opposite topological_space universe u namespace algebraic_geometry open Spec (structure_sheaf) /-- The category of affine schemes -/ @[derive category, nolint has_nonempty_instance] def AffineScheme := Scheme.Spec.ess_image_subcategory /-- A Scheme is affine if the canonical map `X ⟶ Spec Γ(X)` is an isomorphism. -/ class is_affine (X : Scheme) : Prop := (affine : is_iso (Γ_Spec.adjunction.unit.app X)) attribute [instance] is_affine.affine /-- The canonical isomorphism `X ≅ Spec Γ(X)` for an affine scheme. -/ def Scheme.iso_Spec (X : Scheme) [is_affine X] : X ≅ Scheme.Spec.obj (op $ Scheme.Γ.obj $ op X) := as_iso (Γ_Spec.adjunction.unit.app X) /-- Construct an affine scheme from a scheme and the information that it is affine. Also see `AffineScheme.of` for a typclass version. -/ @[simps] def AffineScheme.mk (X : Scheme) (h : is_affine X) : AffineScheme := ⟨X, @@mem_ess_image_of_unit_is_iso _ _ _ _ h.1⟩ /-- Construct an affine scheme from a scheme. Also see `AffineScheme.mk` for a non-typeclass version. -/ def AffineScheme.of (X : Scheme) [h : is_affine X] : AffineScheme := AffineScheme.mk X h /-- Type check a morphism of schemes as a morphism in `AffineScheme`. -/ def AffineScheme.of_hom {X Y : Scheme} [is_affine X] [is_affine Y] (f : X ⟶ Y) : AffineScheme.of X ⟶ AffineScheme.of Y := f lemma mem_Spec_ess_image (X : Scheme) : X ∈ Scheme.Spec.ess_image ↔ is_affine X := ⟨λ h, ⟨functor.ess_image.unit_is_iso h⟩, λ h, @@mem_ess_image_of_unit_is_iso _ _ _ X h.1⟩ instance is_affine_AffineScheme (X : AffineScheme.{u}) : is_affine X.obj := ⟨functor.ess_image.unit_is_iso X.property⟩ instance Spec_is_affine (R : CommRingᵒᵖ) : is_affine (Scheme.Spec.obj R) := algebraic_geometry.is_affine_AffineScheme ⟨_, Scheme.Spec.obj_mem_ess_image R⟩ lemma is_affine_of_iso {X Y : Scheme} (f : X ⟶ Y) [is_iso f] [h : is_affine Y] : is_affine X := by { rw [← mem_Spec_ess_image] at h ⊢, exact functor.ess_image.of_iso (as_iso f).symm h } namespace AffineScheme /-- The `Spec` functor into the category of affine schemes. -/ @[derive [full, faithful, ess_surj]] def Spec : CommRingᵒᵖ ⥤ AffineScheme := Scheme.Spec.to_ess_image /-- The forgetful functor `AffineScheme ⥤ Scheme`. -/ @[derive [full, faithful], simps] def forget_to_Scheme : AffineScheme ⥤ Scheme := Scheme.Spec.ess_image_inclusion /-- The global section functor of an affine scheme. -/ def Γ : AffineSchemeᵒᵖ ⥤ CommRing := forget_to_Scheme.op ⋙ Scheme.Γ /-- The category of affine schemes is equivalent to the category of commutative rings. -/ def equiv_CommRing : AffineScheme ≌ CommRingᵒᵖ := equiv_ess_image_of_reflective.symm instance Γ_is_equiv : is_equivalence Γ.{u} := begin haveI : is_equivalence Γ.{u}.right_op.op := is_equivalence.of_equivalence equiv_CommRing.op, exact (functor.is_equivalence_trans Γ.{u}.right_op.op (op_op_equivalence _).functor : _), end instance : has_colimits AffineScheme.{u} := begin haveI := adjunction.has_limits_of_equivalence.{u} Γ.{u}, exactI adjunction.has_colimits_of_equivalence.{u} (op_op_equivalence AffineScheme.{u}).inverse end instance : has_limits AffineScheme.{u} := begin haveI := adjunction.has_colimits_of_equivalence Γ.{u}, haveI : has_limits AffineScheme.{u} ᵒᵖᵒᵖ := limits.has_limits_op_of_has_colimits, exactI adjunction.has_limits_of_equivalence (op_op_equivalence AffineScheme.{u}).inverse end noncomputable instance : preserves_limits Γ.{u}.right_op := @@adjunction.is_equivalence_preserves_limits _ _ Γ.right_op (is_equivalence.of_equivalence equiv_CommRing) noncomputable instance : preserves_limits forget_to_Scheme := begin apply_with (@@preserves_limits_of_nat_iso _ _ (iso_whisker_right equiv_CommRing.unit_iso forget_to_Scheme).symm) { instances := ff }, change preserves_limits (equiv_CommRing.functor ⋙ Scheme.Spec), apply_instance, end end AffineScheme /-- An open subset of a scheme is affine if the open subscheme is affine. -/ def is_affine_open {X : Scheme} (U : opens X.carrier) : Prop := is_affine (X.restrict U.open_embedding) /-- The set of affine opens as a subset of `opens X.carrier`. -/ def Scheme.affine_opens (X : Scheme) : set (opens X.carrier) := { U : opens X.carrier | is_affine_open U } lemma range_is_affine_open_of_open_immersion {X Y : Scheme} [is_affine X] (f : X ⟶ Y) [H : is_open_immersion f] : is_affine_open f.opens_range := begin refine is_affine_of_iso (is_open_immersion.iso_of_range_eq f (Y.of_restrict _) _).inv, exact subtype.range_coe.symm, apply_instance end lemma top_is_affine_open (X : Scheme) [is_affine X] : is_affine_open (⊤ : opens X.carrier) := begin convert range_is_affine_open_of_open_immersion (𝟙 X), ext1, exact set.range_id.symm end instance Scheme.affine_cover_is_affine (X : Scheme) (i : X.affine_cover.J) : is_affine (X.affine_cover.obj i) := algebraic_geometry.Spec_is_affine _ instance Scheme.affine_basis_cover_is_affine (X : Scheme) (i : X.affine_basis_cover.J) : is_affine (X.affine_basis_cover.obj i) := algebraic_geometry.Spec_is_affine _ lemma is_basis_affine_open (X : Scheme) : opens.is_basis X.affine_opens := begin rw opens.is_basis_iff_nbhd, rintros U x (hU : x ∈ (U : set X.carrier)), obtain ⟨S, hS, hxS, hSU⟩ := X.affine_basis_cover_is_basis.exists_subset_of_mem_open hU U.is_open, refine ⟨⟨S, X.affine_basis_cover_is_basis.is_open hS⟩, _, hxS, hSU⟩, rcases hS with ⟨i, rfl⟩, exact range_is_affine_open_of_open_immersion _, end /-- The open immersion `Spec 𝒪ₓ(U) ⟶ X` for an affine `U`. -/ def is_affine_open.from_Spec {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) : Scheme.Spec.obj (op $ X.presheaf.obj $ op U) ⟶ X := begin haveI : is_affine (X.restrict U.open_embedding) := hU, have : U.open_embedding.is_open_map.functor.obj ⊤ = U, { ext1, exact set.image_univ.trans subtype.range_coe }, exact Scheme.Spec.map (X.presheaf.map (eq_to_hom this.symm).op).op ≫ (X.restrict U.open_embedding).iso_Spec.inv ≫ X.of_restrict _ end instance is_affine_open.is_open_immersion_from_Spec {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) : is_open_immersion hU.from_Spec := by { delta is_affine_open.from_Spec, apply_instance } lemma is_affine_open.from_Spec_range {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) : set.range hU.from_Spec.1.base = (U : set X.carrier) := begin delta is_affine_open.from_Spec, erw [← category.assoc, Scheme.comp_val_base], rw [coe_comp, set.range_comp, set.range_iff_surjective.mpr, set.image_univ], exact subtype.range_coe, rw ← Top.epi_iff_surjective, apply_instance end lemma is_affine_open.from_Spec_image_top {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) : hU.is_open_immersion_from_Spec.base_open.is_open_map.functor.obj ⊤ = U := by { ext1, exact set.image_univ.trans hU.from_Spec_range } lemma is_affine_open.is_compact {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) : is_compact (U : set X.carrier) := begin convert @is_compact.image _ _ _ _ set.univ hU.from_Spec.1.base prime_spectrum.compact_space.1 (by continuity), convert hU.from_Spec_range.symm, exact set.image_univ end lemma is_affine_open.image_is_open_immersion {X Y : Scheme} {U : opens X.carrier} (hU : is_affine_open U) (f : X ⟶ Y) [H : is_open_immersion f] : is_affine_open (f.opens_functor.obj U) := begin haveI : is_affine _ := hU, convert range_is_affine_open_of_open_immersion (X.of_restrict U.open_embedding ≫ f), ext1, exact set.image_eq_range _ _ end lemma is_affine_open_iff_of_is_open_immersion {X Y : Scheme} (f : X ⟶ Y) [H : is_open_immersion f] (U : opens X.carrier) : is_affine_open (H.open_functor.obj U) ↔ is_affine_open U := begin refine ⟨λ hU, @@is_affine_of_iso _ _ hU, λ hU, hU.image_is_open_immersion f⟩, refine (is_open_immersion.iso_of_range_eq (X.of_restrict _ ≫ f) (Y.of_restrict _) _).hom, { rw [Scheme.comp_val_base, coe_comp, set.range_comp], dsimp [opens.inclusion], rw [subtype.range_coe, subtype.range_coe], refl }, { apply_instance } end instance Scheme.quasi_compact_of_affine (X : Scheme) [is_affine X] : compact_space X.carrier := ⟨(top_is_affine_open X).is_compact⟩ lemma is_affine_open.from_Spec_base_preimage {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) : (opens.map hU.from_Spec.val.base).obj U = ⊤ := begin ext1, change hU.from_Spec.1.base ⁻¹' (U : set X.carrier) = set.univ, rw [← hU.from_Spec_range, ← set.image_univ], exact set.preimage_image_eq _ PresheafedSpace.is_open_immersion.base_open.inj end lemma Scheme.Spec_map_presheaf_map_eq_to_hom {X : Scheme} {U V : opens X.carrier} (h : U = V) (W) : (Scheme.Spec.map (X.presheaf.map (eq_to_hom h).op).op).val.c.app W = eq_to_hom (by { cases h, induction W using opposite.rec, dsimp, simp, }) := begin have : Scheme.Spec.map (X.presheaf.map (𝟙 (op U))).op = 𝟙 _, { rw [X.presheaf.map_id, op_id, Scheme.Spec.map_id] }, cases h, refine (Scheme.congr_app this _).trans _, erw category.id_comp, simpa [eq_to_hom_map], end lemma is_affine_open.Spec_Γ_identity_hom_app_from_Spec {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) : (Spec_Γ_identity.hom.app (X.presheaf.obj $ op U)) ≫ hU.from_Spec.1.c.app (op U) = (Scheme.Spec.obj _).presheaf.map (eq_to_hom hU.from_Spec_base_preimage).op := begin haveI : is_affine _ := hU, have e₁ := Spec_Γ_identity.hom.naturality (X.presheaf.map (eq_to_hom U.open_embedding_obj_top).op), rw ← is_iso.comp_inv_eq at e₁, have e₂ := Γ_Spec.adjunction_unit_app_app_top (X.restrict U.open_embedding), erw ← e₂ at e₁, simp only [functor.id_map, quiver.hom.unop_op, functor.comp_map, ← functor.map_inv, ← op_inv, LocallyRingedSpace.Γ_map, category.assoc, functor.right_op_map, inv_eq_to_hom] at e₁, delta is_affine_open.from_Spec Scheme.iso_Spec, rw [Scheme.comp_val_c_app, Scheme.comp_val_c_app, ← e₁], simp_rw category.assoc, erw ← X.presheaf.map_comp_assoc, rw ← op_comp, have e₃ : U.open_embedding.is_open_map.adjunction.counit.app U ≫ eq_to_hom U.open_embedding_obj_top.symm = U.open_embedding.is_open_map.functor.map (eq_to_hom U.inclusion_map_eq_top) := subsingleton.elim _ _, have e₄ : X.presheaf.map _ ≫ _ = _ := (as_iso (Γ_Spec.adjunction.unit.app (X.restrict U.open_embedding))) .inv.1.c.naturality_assoc (eq_to_hom U.inclusion_map_eq_top).op _, erw [e₃, e₄, ← Scheme.comp_val_c_app_assoc, iso.inv_hom_id], simp only [eq_to_hom_map, eq_to_hom_op, Scheme.Spec_map_presheaf_map_eq_to_hom], erw [Scheme.Spec_map_presheaf_map_eq_to_hom, category.id_comp], simpa only [eq_to_hom_trans] end @[elementwise] lemma is_affine_open.from_Spec_app_eq {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) : hU.from_Spec.1.c.app (op U) = Spec_Γ_identity.inv.app (X.presheaf.obj $ op U) ≫ (Scheme.Spec.obj _).presheaf.map (eq_to_hom hU.from_Spec_base_preimage).op := by rw [← hU.Spec_Γ_identity_hom_app_from_Spec, iso.inv_hom_id_app_assoc] lemma is_affine_open.basic_open_is_affine {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) (f : X.presheaf.obj (op U)) : is_affine_open (X.basic_open f) := begin convert range_is_affine_open_of_open_immersion (Scheme.Spec.map (CommRing.of_hom (algebra_map (X.presheaf.obj (op U)) (localization.away f))).op ≫ hU.from_Spec), ext1, have : hU.from_Spec.val.base '' (hU.from_Spec.val.base ⁻¹' (X.basic_open f : set X.carrier)) = (X.basic_open f : set X.carrier), { rw [set.image_preimage_eq_inter_range, set.inter_eq_left_iff_subset, hU.from_Spec_range], exact Scheme.basic_open_le _ _ }, rw [Scheme.hom.opens_range_coe, Scheme.comp_val_base, ← this, coe_comp, set.range_comp], congr' 1, refine (congr_arg coe $ Scheme.preimage_basic_open hU.from_Spec f).trans _, refine eq.trans _ (prime_spectrum.localization_away_comap_range (localization.away f) f).symm, congr' 1, have : (opens.map hU.from_Spec.val.base).obj U = ⊤, { ext1, change hU.from_Spec.1.base ⁻¹' (U : set X.carrier) = set.univ, rw [← hU.from_Spec_range, ← set.image_univ], exact set.preimage_image_eq _ PresheafedSpace.is_open_immersion.base_open.inj }, refine eq.trans _ (basic_open_eq_of_affine f), have lm : ∀ s, (opens.map hU.from_Spec.val.base).obj U ⊓ s = s := λ s, this.symm ▸ top_inf_eq, refine eq.trans _ (lm _), refine eq.trans _ ((Scheme.Spec.obj $ op $ X.presheaf.obj $ op U).basic_open_res _ (eq_to_hom this).op), rw ← comp_apply, congr' 2, rw iso.eq_inv_comp, erw hU.Spec_Γ_identity_hom_app_from_Spec, end lemma is_affine_open.map_restrict_basic_open {X : Scheme} (r : X.presheaf.obj (op ⊤)) {U : opens X.carrier} (hU : is_affine_open U) : is_affine_open ((opens.map (X.of_restrict (X.basic_open r).open_embedding).1.base).obj U) := begin apply (is_affine_open_iff_of_is_open_immersion (X.of_restrict (X.basic_open r).open_embedding) _).mp, delta PresheafedSpace.is_open_immersion.open_functor, dsimp, erw [opens.functor_obj_map_obj, opens.open_embedding_obj_top, inf_comm, ← Scheme.basic_open_res _ _ (hom_of_le le_top).op], exact hU.basic_open_is_affine _, end lemma Scheme.map_prime_spectrum_basic_open_of_affine (X : Scheme) [is_affine X] (f : Scheme.Γ.obj (op X)) : (opens.map X.iso_Spec.hom.1.base).obj (prime_spectrum.basic_open f) = X.basic_open f := begin rw ← basic_open_eq_of_affine, transitivity (opens.map X.iso_Spec.hom.1.base).obj ((Scheme.Spec.obj (op (Scheme.Γ.obj (op X)))).basic_open ((inv (X.iso_Spec.hom.1.c.app (op ((opens.map (inv X.iso_Spec.hom).val.base).obj ⊤)))) ((X.presheaf.map (eq_to_hom _)) f))), congr, { rw [← is_iso.inv_eq_inv, is_iso.inv_inv, is_iso.iso.inv_inv, nat_iso.app_hom], erw ← Γ_Spec.adjunction_unit_app_app_top, refl }, { rw eq_to_hom_map, refl }, { dsimp, congr }, { refine (Scheme.preimage_basic_open _ _).trans _, rw [is_iso.inv_hom_id_apply, Scheme.basic_open_res_eq] } end lemma is_basis_basic_open (X : Scheme) [is_affine X] : opens.is_basis (set.range (X.basic_open : X.presheaf.obj (op ⊤) → opens X.carrier)) := begin delta opens.is_basis, convert prime_spectrum.is_basis_basic_opens.inducing (Top.homeo_of_iso (Scheme.forget_to_Top.map_iso X.iso_Spec)).inducing using 1, ext, simp only [set.mem_image, exists_exists_eq_and], split, { rintro ⟨_, ⟨x, rfl⟩, rfl⟩, refine ⟨_, ⟨_, ⟨x, rfl⟩, rfl⟩, _⟩, exact congr_arg opens.carrier (X.map_prime_spectrum_basic_open_of_affine x) }, { rintro ⟨_, ⟨_, ⟨x, rfl⟩, rfl⟩, rfl⟩, refine ⟨_, ⟨x, rfl⟩, _⟩, exact congr_arg opens.carrier (X.map_prime_spectrum_basic_open_of_affine x).symm } end lemma is_affine_open.exists_basic_open_le {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) {V : opens X.carrier} (x : V) (h : ↑x ∈ U) : ∃ f : X.presheaf.obj (op U), X.basic_open f ≤ V ∧ ↑x ∈ X.basic_open f := begin haveI : is_affine _ := hU, obtain ⟨_, ⟨_, ⟨r, rfl⟩, rfl⟩, h₁, h₂⟩ := (is_basis_basic_open (X.restrict U.open_embedding)) .exists_subset_of_mem_open _ ((opens.map U.inclusion).obj V).is_open, swap, exact ⟨x, h⟩, have : U.open_embedding.is_open_map.functor.obj ((X.restrict U.open_embedding).basic_open r) = X.basic_open (X.presheaf.map (eq_to_hom U.open_embedding_obj_top.symm).op r), { refine (Scheme.image_basic_open (X.of_restrict U.open_embedding) r).trans _, erw ← Scheme.basic_open_res_eq _ _ (eq_to_hom U.open_embedding_obj_top).op, rw [← comp_apply, ← category_theory.functor.map_comp, ← op_comp, eq_to_hom_trans, eq_to_hom_refl, op_id, category_theory.functor.map_id, Scheme.hom.inv_app], erw PresheafedSpace.is_open_immersion.of_restrict_inv_app, congr }, use X.presheaf.map (eq_to_hom U.open_embedding_obj_top.symm).op r, rw ← this, exact ⟨set.image_subset_iff.mpr h₂, set.mem_image_of_mem _ h₁⟩, exact x.prop, end instance {X : Scheme} {U : opens X.carrier} (f : X.presheaf.obj (op U)) : algebra (X.presheaf.obj (op U)) (X.presheaf.obj (op $ X.basic_open f)) := (X.presheaf.map (hom_of_le $ RingedSpace.basic_open_le _ f : _ ⟶ U).op).to_algebra lemma is_affine_open.opens_map_from_Spec_basic_open {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) (f : X.presheaf.obj (op U)) : (opens.map hU.from_Spec.val.base).obj (X.basic_open f) = RingedSpace.basic_open _ (Spec_Γ_identity.inv.app (X.presheaf.obj $ op U) f) := begin erw LocallyRingedSpace.preimage_basic_open, refine eq.trans _ (RingedSpace.basic_open_res_eq (Scheme.Spec.obj $ op $ X.presheaf.obj (op U)) .to_LocallyRingedSpace.to_RingedSpace (eq_to_hom hU.from_Spec_base_preimage).op _), congr, rw ← comp_apply, congr, erw ← hU.Spec_Γ_identity_hom_app_from_Spec, rw iso.inv_hom_id_app_assoc, end /-- The canonical map `Γ(𝒪ₓ, D(f)) ⟶ Γ(Spec 𝒪ₓ(U), D(Spec_Γ_identity.inv f))` This is an isomorphism, as witnessed by an `is_iso` instance. -/ def basic_open_sections_to_affine {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) (f : X.presheaf.obj (op U)) : X.presheaf.obj (op $ X.basic_open f) ⟶ (Scheme.Spec.obj $ op $ X.presheaf.obj (op U)).presheaf.obj (op $ Scheme.basic_open _ $ Spec_Γ_identity.inv.app (X.presheaf.obj (op U)) f) := hU.from_Spec.1.c.app (op $ X.basic_open f) ≫ (Scheme.Spec.obj $ op $ X.presheaf.obj (op U)) .presheaf.map (eq_to_hom $ (hU.opens_map_from_Spec_basic_open f).symm).op instance {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) (f : X.presheaf.obj (op U)) : is_iso (basic_open_sections_to_affine hU f) := begin delta basic_open_sections_to_affine, apply_with is_iso.comp_is_iso { instances := ff }, { apply PresheafedSpace.is_open_immersion.is_iso_of_subset, rw hU.from_Spec_range, exact RingedSpace.basic_open_le _ _ }, apply_instance end lemma is_localization_basic_open {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) (f : X.presheaf.obj (op U)) : is_localization.away f (X.presheaf.obj (op $ X.basic_open f)) := begin apply (is_localization.is_localization_iff_of_ring_equiv (submonoid.powers f) (as_iso $ basic_open_sections_to_affine hU f ≫ (Scheme.Spec.obj _).presheaf.map (eq_to_hom (basic_open_eq_of_affine _).symm).op).CommRing_iso_to_ring_equiv).mpr, convert structure_sheaf.is_localization.to_basic_open _ f, change _ ≫ (basic_open_sections_to_affine hU f ≫ _) = _, delta basic_open_sections_to_affine, erw ring_hom.algebra_map_to_algebra, simp only [Scheme.comp_val_c_app, category.assoc], erw hU.from_Spec.val.c.naturality_assoc, rw hU.from_Spec_app_eq, dsimp, simp only [category.assoc, ← functor.map_comp, ← op_comp], apply structure_sheaf.to_open_res, end instance {X : Scheme} [is_affine X] (r : X.presheaf.obj (op ⊤)) : is_localization.away r (X.presheaf.obj (op $ X.basic_open r)) := is_localization_basic_open (top_is_affine_open X) r lemma is_localization_of_eq_basic_open {X : Scheme} {U V : opens X.carrier} (i : V ⟶ U) (hU : is_affine_open U) (r : X.presheaf.obj (op U)) (e : V = X.basic_open r) : @@is_localization.away _ r (X.presheaf.obj (op V)) _ (X.presheaf.map i.op).to_algebra := by { subst e, convert is_localization_basic_open hU r using 3 } instance Γ_restrict_algebra {X : Scheme} {Y : Top} {f : Y ⟶ X.carrier} (hf : open_embedding f) : algebra (Scheme.Γ.obj (op X)) (Scheme.Γ.obj (op $ X.restrict hf)) := (Scheme.Γ.map (X.of_restrict hf).op).to_algebra instance Γ_restrict_is_localization (X : Scheme.{u}) [is_affine X] (r : Scheme.Γ.obj (op X)) : is_localization.away r (Scheme.Γ.obj (op $ X.restrict (X.basic_open r).open_embedding)) := is_localization_of_eq_basic_open _ (top_is_affine_open X) r (opens.open_embedding_obj_top _) lemma basic_open_basic_open_is_basic_open {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) (f : X.presheaf.obj (op U)) (g : X.presheaf.obj (op $ X.basic_open f)) : ∃ f' : X.presheaf.obj (op U), X.basic_open f' = X.basic_open g := begin haveI := is_localization_basic_open hU f, obtain ⟨x, ⟨_, n, rfl⟩, rfl⟩ := is_localization.surj' (submonoid.powers f) g, use f * x, rw [algebra.smul_def, Scheme.basic_open_mul, Scheme.basic_open_mul], erw Scheme.basic_open_res, refine (inf_eq_left.mpr _).symm, convert inf_le_left using 1, apply Scheme.basic_open_of_is_unit, apply submonoid.left_inv_le_is_unit _ (is_localization.to_inv_submonoid (submonoid.powers f) (X.presheaf.obj (op $ X.basic_open f)) _).prop end lemma exists_basic_open_le_affine_inter {X : Scheme} {U V : opens X.carrier} (hU : is_affine_open U) (hV : is_affine_open V) (x : X.carrier) (hx : x ∈ U ⊓ V) : ∃ (f : X.presheaf.obj $ op U) (g : X.presheaf.obj $ op V), X.basic_open f = X.basic_open g ∧ x ∈ X.basic_open f := begin obtain ⟨f, hf₁, hf₂⟩ := hU.exists_basic_open_le ⟨x, hx.2⟩ hx.1, obtain ⟨g, hg₁, hg₂⟩ := hV.exists_basic_open_le ⟨x, hf₂⟩ hx.2, obtain ⟨f', hf'⟩ := basic_open_basic_open_is_basic_open hU f (X.presheaf.map (hom_of_le hf₁ : _ ⟶ V).op g), replace hf' := (hf'.trans (RingedSpace.basic_open_res _ _ _)).trans (inf_eq_right.mpr hg₁), exact ⟨f', g, hf', hf'.symm ▸ hg₂⟩ end /-- The prime ideal of `𝒪ₓ(U)` corresponding to a point `x : U`. -/ noncomputable def is_affine_open.prime_ideal_of {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) (x : U) : prime_spectrum (X.presheaf.obj $ op U) := ((Scheme.Spec.map (X.presheaf.map (eq_to_hom $ show U.open_embedding.is_open_map.functor.obj ⊤ = U, from opens.ext (set.image_univ.trans subtype.range_coe)).op).op).1.base ((@@Scheme.iso_Spec (X.restrict U.open_embedding) hU).hom.1.base x)) lemma is_affine_open.from_Spec_prime_ideal_of {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) (x : U) : hU.from_Spec.val.base (hU.prime_ideal_of x) = x.1 := begin dsimp only [is_affine_open.from_Spec, subtype.coe_mk], erw [← Scheme.comp_val_base_apply, ← Scheme.comp_val_base_apply], simpa only [← functor.map_comp_assoc, ← functor.map_comp, ← op_comp, eq_to_hom_trans, op_id, eq_to_hom_refl, category_theory.functor.map_id, category.id_comp, iso.hom_inv_id_assoc] end lemma is_affine_open.is_localization_stalk_aux {X : Scheme} (U : opens X.carrier) [is_affine (X.restrict U.open_embedding)] : (inv (Γ_Spec.adjunction.unit.app (X.restrict U.open_embedding))).1.c.app (op ((opens.map U.inclusion).obj U)) = X.presheaf.map (eq_to_hom $ by rw opens.inclusion_map_eq_top : U.open_embedding.is_open_map.functor.obj ⊤ ⟶ (U.open_embedding.is_open_map.functor.obj ((opens.map U.inclusion).obj U))).op ≫ to_Spec_Γ (X.presheaf.obj $ op (U.open_embedding.is_open_map.functor.obj ⊤)) ≫ (Scheme.Spec.obj $ op $ X.presheaf.obj $ _).presheaf.map (eq_to_hom (by { rw [opens.inclusion_map_eq_top], refl }) : unop _ ⟶ ⊤).op := begin have e : (opens.map (inv (Γ_Spec.adjunction.unit.app (X.restrict U.open_embedding))).1.base).obj ((opens.map U.inclusion).obj U) = ⊤, by { rw [opens.inclusion_map_eq_top], refl }, rw [Scheme.inv_val_c_app, is_iso.comp_inv_eq, Scheme.app_eq _ e, Γ_Spec.adjunction_unit_app_app_top], simp only [category.assoc, eq_to_hom_op], erw ← functor.map_comp_assoc, rw [eq_to_hom_trans, eq_to_hom_refl, category_theory.functor.map_id, category.id_comp], erw Spec_Γ_identity.inv_hom_id_app_assoc, simp only [eq_to_hom_map, eq_to_hom_trans], end lemma is_affine_open.is_localization_stalk {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) (x : U) : is_localization.at_prime (X.presheaf.stalk x) (hU.prime_ideal_of x).as_ideal := begin haveI : is_affine _ := hU, haveI : nonempty U := ⟨x⟩, rcases x with ⟨x, hx⟩, let y := hU.prime_ideal_of ⟨x, hx⟩, have : hU.from_Spec.val.base y = x := hU.from_Spec_prime_ideal_of ⟨x, hx⟩, change is_localization y.as_ideal.prime_compl _, clear_value y, subst this, apply (is_localization.is_localization_iff_of_ring_equiv _ (as_iso $ PresheafedSpace.stalk_map hU.from_Spec.1 y).CommRing_iso_to_ring_equiv).mpr, convert structure_sheaf.is_localization.to_stalk _ _ using 1, delta structure_sheaf.stalk_algebra, congr' 1, rw ring_hom.algebra_map_to_algebra, refine (PresheafedSpace.stalk_map_germ hU.from_Spec.1 _ ⟨_, _⟩).trans _, delta is_affine_open.from_Spec Scheme.iso_Spec structure_sheaf.to_stalk, simp only [Scheme.comp_val_c_app, category.assoc], dsimp only [functor.op, as_iso_inv, unop_op], erw is_affine_open.is_localization_stalk_aux, simp only [category.assoc], conv_lhs { rw ← category.assoc }, erw [← X.presheaf.map_comp, Spec_Γ_naturality_assoc], congr' 1, simp only [← category.assoc], transitivity _ ≫ (structure_sheaf (X.presheaf.obj $ op U)).presheaf.germ ⟨_, _⟩, { refl }, convert ((structure_sheaf (X.presheaf.obj $ op U)).presheaf.germ_res (hom_of_le le_top) ⟨_, _⟩) using 2, rw category.assoc, erw nat_trans.naturality, rw [← LocallyRingedSpace.Γ_map_op, ← LocallyRingedSpace.Γ.map_comp_assoc, ← op_comp], erw ← Scheme.Spec.map_comp, rw [← op_comp, ← X.presheaf.map_comp], transitivity LocallyRingedSpace.Γ.map (quiver.hom.op $ Scheme.Spec.map (X.presheaf.map (𝟙 (op U))).op) ≫ _, { congr }, simp only [category_theory.functor.map_id, op_id], erw category_theory.functor.map_id, rw category.id_comp, refl end /-- The basic open set of a section `f` on an an affine open as an `X.affine_opens`. -/ @[simps] def Scheme.affine_basic_open (X : Scheme) {U : X.affine_opens} (f : X.presheaf.obj $ op U) : X.affine_opens := ⟨X.basic_open f, U.prop.basic_open_is_affine f⟩ @[simp] lemma is_affine_open.basic_open_from_Spec_app {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) (f : X.presheaf.obj (op U)) : @Scheme.basic_open (Scheme.Spec.obj $ op (X.presheaf.obj $ op U)) ((opens.map hU.from_Spec.1.base).obj U) (hU.from_Spec.1.c.app (op U) f) = prime_spectrum.basic_open f := begin rw [← Scheme.basic_open_res_eq _ _ (eq_to_hom hU.from_Spec_base_preimage.symm).op, basic_open_eq_of_affine', is_affine_open.from_Spec_app_eq], congr, rw [← comp_apply, ← comp_apply, category.assoc, ← functor.map_comp_assoc, eq_to_hom_op, eq_to_hom_op, eq_to_hom_trans, eq_to_hom_refl, category_theory.functor.map_id, category.id_comp, ← iso.app_inv, iso.inv_hom_id], refl end lemma is_affine_open.from_Spec_map_basic_open {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) (f : X.presheaf.obj (op U)) : (opens.map hU.from_Spec.val.base).obj (X.basic_open f) = prime_spectrum.basic_open f := by simp lemma is_affine_open.basic_open_union_eq_self_iff {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) (s : set (X.presheaf.obj $ op U)) : (⨆ (f : s), X.basic_open (f : X.presheaf.obj $ op U)) = U ↔ ideal.span s = ⊤ := begin transitivity (⋃ (i : s), (prime_spectrum.basic_open i.1).1) = set.univ, transitivity hU.from_Spec.1.base ⁻¹' (⨆ (f : s), X.basic_open (f : X.presheaf.obj $ op U)).1 = hU.from_Spec.1.base ⁻¹' U.1, { refine ⟨λ h, by rw h, _⟩, intro h, apply_fun set.image hU.from_Spec.1.base at h, rw [set.image_preimage_eq_inter_range, set.image_preimage_eq_inter_range, hU.from_Spec_range] at h, simp only [set.inter_self, opens.carrier_eq_coe, set.inter_eq_right_iff_subset] at h, ext1, refine set.subset.antisymm _ h, simp only [set.Union_subset_iff, set_coe.forall, opens.coe_supr], intros x hx, exact X.basic_open_le x }, { simp only [opens.supr_def, subtype.coe_mk, set.preimage_Union, subtype.val_eq_coe], congr' 3, { ext1 x, exact congr_arg opens.carrier (hU.from_Spec_map_basic_open _) }, { exact congr_arg opens.carrier hU.from_Spec_base_preimage } }, { simp only [opens.carrier_eq_coe, prime_spectrum.basic_open_eq_zero_locus_compl], rw [← set.compl_Inter, set.compl_univ_iff, ← prime_spectrum.zero_locus_Union, ← prime_spectrum.zero_locus_empty_iff_eq_top, prime_spectrum.zero_locus_span], simp only [set.Union_singleton_eq_range, subtype.range_val_subtype, set.set_of_mem_eq] } end lemma is_affine_open.self_le_basic_open_union_iff {X : Scheme} {U : opens X.carrier} (hU : is_affine_open U) (s : set (X.presheaf.obj $ op U)) : U ≤ (⨆ (f : s), X.basic_open (f : X.presheaf.obj $ op U)) ↔ ideal.span s = ⊤ := begin rw [← hU.basic_open_union_eq_self_iff, @comm _ eq], refine ⟨λ h, le_antisymm h _, le_of_eq⟩, simp only [supr_le_iff, set_coe.forall], intros x hx, exact X.basic_open_le x end /-- Let `P` be a predicate on the affine open sets of `X` satisfying 1. If `P` holds on `U`, then `P` holds on the basic open set of every section on `U`. 2. If `P` holds for a family of basic open sets covering `U`, then `P` holds for `U`. 3. There exists an affine open cover of `X` each satisfying `P`. Then `P` holds for every affine open of `X`. This is also known as the **Affine communication lemma** in [*The rising sea*][RisingSea]. -/ @[elab_as_eliminator] lemma of_affine_open_cover {X : Scheme} (V : X.affine_opens) (S : set X.affine_opens) {P : X.affine_opens → Prop} (hP₁ : ∀ (U : X.affine_opens) (f : X.presheaf.obj $ op U.1), P U → P (X.affine_basic_open f)) (hP₂ : ∀ (U : X.affine_opens) (s : finset (X.presheaf.obj $ op U)) (hs : ideal.span (s : set (X.presheaf.obj $ op U)) = ⊤), (∀ (f : s), P (X.affine_basic_open f.1)) → P U) (hS : (⋃ (i : S), i : set X.carrier) = set.univ) (hS' : ∀ (U : S), P U) : P V := begin classical, have : ∀ (x : V), ∃ (f : X.presheaf.obj $ op V.1), ↑x ∈ (X.basic_open f) ∧ P (X.affine_basic_open f), { intro x, have : ↑x ∈ (set.univ : set X.carrier) := trivial, rw ← hS at this, obtain ⟨W, hW⟩ := set.mem_Union.mp this, obtain ⟨f, g, e, hf⟩ := exists_basic_open_le_affine_inter V.prop W.1.prop x ⟨x.prop, hW⟩, refine ⟨f, hf, _⟩, convert hP₁ _ g (hS' W) using 1, ext1, exact e }, choose f hf₁ hf₂ using this, suffices : ideal.span (set.range f) = ⊤, { obtain ⟨t, ht₁, ht₂⟩ := (ideal.span_eq_top_iff_finite _).mp this, apply hP₂ V t ht₂, rintro ⟨i, hi⟩, obtain ⟨x, rfl⟩ := ht₁ hi, exact hf₂ x }, rw ← V.prop.self_le_basic_open_union_iff, intros x hx, rw [supr_range', opens.mem_supr], exact ⟨_, hf₁ ⟨x, hx⟩⟩ end end algebraic_geometry
c678d1160145cbc77737be484ae0a3a509b9646c
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/t6.lean
2729808e0a2044351354907ad49a733c4424a05e
[ "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
343
lean
prelude definition Prop : Type.{1} := Type.{0} section variable {A : Type*} -- Mark A as implicit parameter variable R : A → A → Prop definition id (a : A) : A := a definition refl : Prop := forall (a : A), R a a definition symm : Prop := forall (a b : A), R a b -> R b a end check id.{2} check refl.{1} check symm.{1}
883a9945e5802a032425d93eb7bbb336b0936245
82e44445c70db0f03e30d7be725775f122d72f3e
/src/analysis/mean_inequalities.lean
4da258c008edf41b99704c5eed82dcff5a413ea5
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
33,112
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Sébastien Gouëzel, Rémy Degenne -/ import analysis.convex.specific_functions import data.real.conjugate_exponents /-! # Mean value inequalities In this file we prove several inequalities for finite sums, including AM-GM inequality, Young's inequality, Hölder inequality, and Minkowski inequality. Versions for integrals of some of these inequalities are available in `measure_theory.mean_inequalities`. ## Main theorems ### AM-GM inequality: The inequality says that the geometric mean of a tuple of non-negative numbers is less than or equal to their arithmetic mean. We prove the weighted version of this inequality: if $w$ and $z$ are two non-negative vectors and $\sum_{i\in s} w_i=1$, then $$ \prod_{i\in s} z_i^{w_i} ≤ \sum_{i\in s} w_iz_i. $$ The classical version is a special case of this inequality for $w_i=\frac{1}{n}$. We prove a few versions of this inequality. Each of the following lemmas comes in two versions: a version for real-valued non-negative functions is in the `real` namespace, and a version for `nnreal`-valued functions is in the `nnreal` namespace. - `geom_mean_le_arith_mean_weighted` : weighted version for functions on `finset`s; - `geom_mean_le_arith_mean2_weighted` : weighted version for two numbers; - `geom_mean_le_arith_mean3_weighted` : weighted version for three numbers; - `geom_mean_le_arith_mean4_weighted` : weighted version for four numbers. ### Generalized mean inequality The inequality says that for two non-negative vectors $w$ and $z$ with $\sum_{i\in s} w_i=1$ and $p ≤ q$ we have $$ \sqrt[p]{\sum_{i\in s} w_i z_i^p} ≤ \sqrt[q]{\sum_{i\in s} w_i z_i^q}. $$ Currently we only prove this inequality for $p=1$. As in the rest of `mathlib`, we provide different theorems for natural exponents (`pow_arith_mean_le_arith_mean_pow`), integer exponents (`fpow_arith_mean_le_arith_mean_fpow`), and real exponents (`rpow_arith_mean_le_arith_mean_rpow` and `arith_mean_le_rpow_mean`). In the first two cases we prove $$ \left(\sum_{i\in s} w_i z_i\right)^n ≤ \sum_{i\in s} w_i z_i^n $$ in order to avoid using real exponents. For real exponents we prove both this and standard versions. ### Young's inequality Young's inequality says that for non-negative numbers `a`, `b`, `p`, `q` such that $\frac{1}{p}+\frac{1}{q}=1$ we have $$ ab ≤ \frac{a^p}{p} + \frac{b^q}{q}. $$ This inequality is a special case of the AM-GM inequality. It can be used to prove Hölder's inequality (see below) but we use a different proof. ### Hölder's inequality The inequality says that for two conjugate exponents `p` and `q` (i.e., for two positive numbers such that $\frac{1}{p}+\frac{1}{q}=1$) and any two non-negative vectors their inner product is less than or equal to the product of the $L_p$ norm of the first vector and the $L_q$ norm of the second vector: $$ \sum_{i\in s} a_ib_i ≤ \sqrt[p]{\sum_{i\in s} a_i^p}\sqrt[q]{\sum_{i\in s} b_i^q}. $$ We give versions of this result in `ℝ`, `ℝ≥0` and `ℝ≥0∞`. There are at least two short proofs of this inequality. In one proof we prenormalize both vectors, then apply Young's inequality to each $a_ib_i$. We use a different proof deducing this inequality from the generalized mean inequality for well-chosen vectors and weights. ### Minkowski's inequality The inequality says that for `p ≥ 1` the function $$ \|a\|_p=\sqrt[p]{\sum_{i\in s} a_i^p} $$ satisfies the triangle inequality $\|a+b\|_p\le \|a\|_p+\|b\|_p$. We give versions of this result in `real`, `ℝ≥0` and `ℝ≥0∞`. We deduce this inequality from Hölder's inequality. Namely, Hölder inequality implies that $\|a\|_p$ is the maximum of the inner product $\sum_{i\in s}a_ib_i$ over `b` such that $\|b\|_q\le 1$. Now Minkowski's inequality follows from the fact that the maximum value of the sum of two functions is less than or equal to the sum of the maximum values of the summands. ## TODO - each inequality `A ≤ B` should come with a theorem `A = B ↔ _`; one of the ways to prove them is to define `strict_convex_on` functions. - generalized mean inequality with any `p ≤ q`, including negative numbers; - prove that the power mean tends to the geometric mean as the exponent tends to zero. -/ universes u v open finset open_locale classical big_operators nnreal ennreal noncomputable theory variables {ι : Type u} (s : finset ι) namespace real /-- AM-GM inequality: the **geometric mean is less than or equal to the arithmetic mean**, weighted version for real-valued nonnegative functions. -/ theorem geom_mean_le_arith_mean_weighted (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) : (∏ i in s, (z i) ^ (w i)) ≤ ∑ i in s, w i * z i := begin -- If some number `z i` equals zero and has non-zero weight, then LHS is 0 and RHS is nonnegative. by_cases A : ∃ i ∈ s, z i = 0 ∧ w i ≠ 0, { rcases A with ⟨i, his, hzi, hwi⟩, rw [prod_eq_zero his], { exact sum_nonneg (λ j hj, mul_nonneg (hw j hj) (hz j hj)) }, { rw hzi, exact zero_rpow hwi } }, -- If all numbers `z i` with non-zero weight are positive, then we apply Jensen's inequality -- for `exp` and numbers `log (z i)` with weights `w i`. { simp only [not_exists, not_and, ne.def, not_not] at A, have := convex_on_exp.map_sum_le hw hw' (λ i _, set.mem_univ $ log (z i)), simp only [exp_sum, (∘), smul_eq_mul, mul_comm (w _) (log _)] at this, convert this using 1; [apply prod_congr rfl, apply sum_congr rfl]; intros i hi, { cases eq_or_lt_of_le (hz i hi) with hz hz, { simp [A i hi hz.symm] }, { exact rpow_def_of_pos hz _ } }, { cases eq_or_lt_of_le (hz i hi) with hz hz, { simp [A i hi hz.symm] }, { rw [exp_log hz] } } } end theorem pow_arith_mean_le_arith_mean_pow (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) (n : ℕ) : (∑ i in s, w i * z i) ^ n ≤ ∑ i in s, (w i * z i ^ n) := (convex_on_pow n).map_sum_le hw hw' hz theorem pow_arith_mean_le_arith_mean_pow_of_even (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) {n : ℕ} (hn : even n) : (∑ i in s, w i * z i) ^ n ≤ ∑ i in s, (w i * z i ^ n) := (convex_on_pow_of_even hn).map_sum_le hw hw' (λ _ _, trivial) theorem fpow_arith_mean_le_arith_mean_fpow (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 < z i) (m : ℤ) : (∑ i in s, w i * z i) ^ m ≤ ∑ i in s, (w i * z i ^ m) := (convex_on_fpow m).map_sum_le hw hw' hz theorem rpow_arith_mean_le_arith_mean_rpow (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) {p : ℝ} (hp : 1 ≤ p) : (∑ i in s, w i * z i) ^ p ≤ ∑ i in s, (w i * z i ^ p) := (convex_on_rpow hp).map_sum_le hw hw' hz theorem arith_mean_le_rpow_mean (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) {p : ℝ} (hp : 1 ≤ p) : ∑ i in s, w i * z i ≤ (∑ i in s, (w i * z i ^ p)) ^ (1 / p) := begin have : 0 < p := lt_of_lt_of_le zero_lt_one hp, rw [← rpow_le_rpow_iff _ _ this, ← rpow_mul, one_div_mul_cancel (ne_of_gt this), rpow_one], exact rpow_arith_mean_le_arith_mean_rpow s w z hw hw' hz hp, all_goals { apply_rules [sum_nonneg, rpow_nonneg_of_nonneg], intros i hi, apply_rules [mul_nonneg, rpow_nonneg_of_nonneg, hw i hi, hz i hi] }, end end real namespace nnreal /-- The geometric mean is less than or equal to the arithmetic mean, weighted version for `nnreal`-valued functions. -/ theorem geom_mean_le_arith_mean_weighted (w z : ι → ℝ≥0) (hw' : ∑ i in s, w i = 1) : (∏ i in s, (z i) ^ (w i:ℝ)) ≤ ∑ i in s, w i * z i := by exact_mod_cast real.geom_mean_le_arith_mean_weighted _ _ _ (λ i _, (w i).coe_nonneg) (by assumption_mod_cast) (λ i _, (z i).coe_nonneg) /-- The geometric mean is less than or equal to the arithmetic mean, weighted version for two `nnreal` numbers. -/ theorem geom_mean_le_arith_mean2_weighted (w₁ w₂ p₁ p₂ : ℝ≥0) : w₁ + w₂ = 1 → p₁ ^ (w₁:ℝ) * p₂ ^ (w₂:ℝ) ≤ w₁ * p₁ + w₂ * p₂ := by simpa only [fin.prod_univ_succ, fin.sum_univ_succ, finset.prod_empty, finset.sum_empty, fintype.univ_of_is_empty, fin.cons_succ, fin.cons_zero, add_zero, mul_one] using geom_mean_le_arith_mean_weighted (univ : finset (fin 2)) (fin.cons w₁ $ fin.cons w₂ fin_zero_elim) (fin.cons p₁ $ fin.cons p₂ $ fin_zero_elim) theorem geom_mean_le_arith_mean3_weighted (w₁ w₂ w₃ p₁ p₂ p₃ : ℝ≥0) : w₁ + w₂ + w₃ = 1 → p₁ ^ (w₁:ℝ) * p₂ ^ (w₂:ℝ) * p₃ ^ (w₃:ℝ) ≤ w₁ * p₁ + w₂ * p₂ + w₃ * p₃ := by simpa only [fin.prod_univ_succ, fin.sum_univ_succ, finset.prod_empty, finset.sum_empty, fintype.univ_of_is_empty, fin.cons_succ, fin.cons_zero, add_zero, mul_one, ← add_assoc, mul_assoc] using geom_mean_le_arith_mean_weighted (univ : finset (fin 3)) (fin.cons w₁ $ fin.cons w₂ $ fin.cons w₃ fin_zero_elim) (fin.cons p₁ $ fin.cons p₂ $ fin.cons p₃ fin_zero_elim) theorem geom_mean_le_arith_mean4_weighted (w₁ w₂ w₃ w₄ p₁ p₂ p₃ p₄ : ℝ≥0) : w₁ + w₂ + w₃ + w₄ = 1 → p₁ ^ (w₁:ℝ) * p₂ ^ (w₂:ℝ) * p₃ ^ (w₃:ℝ)* p₄ ^ (w₄:ℝ) ≤ w₁ * p₁ + w₂ * p₂ + w₃ * p₃ + w₄ * p₄ := by simpa only [fin.prod_univ_succ, fin.sum_univ_succ, finset.prod_empty, finset.sum_empty, fintype.univ_of_is_empty, fin.cons_succ, fin.cons_zero, add_zero, mul_one, ← add_assoc, mul_assoc] using geom_mean_le_arith_mean_weighted (univ : finset (fin 4)) (fin.cons w₁ $ fin.cons w₂ $ fin.cons w₃ $ fin.cons w₄ fin_zero_elim) (fin.cons p₁ $ fin.cons p₂ $ fin.cons p₃ $ fin.cons p₄ fin_zero_elim) /-- Weighted generalized mean inequality, version sums over finite sets, with `ℝ≥0`-valued functions and natural exponent. -/ theorem pow_arith_mean_le_arith_mean_pow (w z : ι → ℝ≥0) (hw' : ∑ i in s, w i = 1) (n : ℕ) : (∑ i in s, w i * z i) ^ n ≤ ∑ i in s, (w i * z i ^ n) := by exact_mod_cast real.pow_arith_mean_le_arith_mean_pow s _ _ (λ i _, (w i).coe_nonneg) (by exact_mod_cast hw') (λ i _, (z i).coe_nonneg) n /-- Weighted generalized mean inequality, version for sums over finite sets, with `ℝ≥0`-valued functions and real exponents. -/ theorem rpow_arith_mean_le_arith_mean_rpow (w z : ι → ℝ≥0) (hw' : ∑ i in s, w i = 1) {p : ℝ} (hp : 1 ≤ p) : (∑ i in s, w i * z i) ^ p ≤ ∑ i in s, (w i * z i ^ p) := by exact_mod_cast real.rpow_arith_mean_le_arith_mean_rpow s _ _ (λ i _, (w i).coe_nonneg) (by exact_mod_cast hw') (λ i _, (z i).coe_nonneg) hp /-- Weighted generalized mean inequality, version for two elements of `ℝ≥0` and real exponents. -/ theorem rpow_arith_mean_le_arith_mean2_rpow (w₁ w₂ z₁ z₂ : ℝ≥0) (hw' : w₁ + w₂ = 1) {p : ℝ} (hp : 1 ≤ p) : (w₁ * z₁ + w₂ * z₂) ^ p ≤ w₁ * z₁ ^ p + w₂ * z₂ ^ p := begin have h := rpow_arith_mean_le_arith_mean_rpow (univ : finset (fin 2)) (fin.cons w₁ $ fin.cons w₂ fin_zero_elim) (fin.cons z₁ $ fin.cons z₂ $ fin_zero_elim) _ hp, { simpa [fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero] using h, }, { simp [hw', fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero], }, end /-- Weighted generalized mean inequality, version for sums over finite sets, with `ℝ≥0`-valued functions and real exponents. -/ theorem arith_mean_le_rpow_mean (w z : ι → ℝ≥0) (hw' : ∑ i in s, w i = 1) {p : ℝ} (hp : 1 ≤ p) : ∑ i in s, w i * z i ≤ (∑ i in s, (w i * z i ^ p)) ^ (1 / p) := by exact_mod_cast real.arith_mean_le_rpow_mean s _ _ (λ i _, (w i).coe_nonneg) (by exact_mod_cast hw') (λ i _, (z i).coe_nonneg) hp end nnreal namespace ennreal /-- Weighted generalized mean inequality, version for sums over finite sets, with `ℝ≥0∞`-valued functions and real exponents. -/ theorem rpow_arith_mean_le_arith_mean_rpow (w z : ι → ℝ≥0∞) (hw' : ∑ i in s, w i = 1) {p : ℝ} (hp : 1 ≤ p) : (∑ i in s, w i * z i) ^ p ≤ ∑ i in s, (w i * z i ^ p) := begin have hp_pos : 0 < p, from lt_of_lt_of_le zero_lt_one hp, have hp_nonneg : 0 ≤ p, from le_of_lt hp_pos, have hp_not_nonpos : ¬ p ≤ 0, by simp [hp_pos], have hp_not_neg : ¬ p < 0, by simp [hp_nonneg], have h_top_iff_rpow_top : ∀ (i : ι) (hi : i ∈ s), w i * z i = ⊤ ↔ w i * (z i) ^ p = ⊤, by simp [hp_pos, hp_nonneg, hp_not_nonpos, hp_not_neg], refine le_of_top_imp_top_of_to_nnreal_le _ _, { -- first, prove `(∑ i in s, w i * z i) ^ p = ⊤ → ∑ i in s, (w i * z i ^ p) = ⊤` rw [rpow_eq_top_iff, sum_eq_top_iff, sum_eq_top_iff], intro h, simp only [and_false, hp_not_neg, false_or] at h, rcases h.left with ⟨a, H, ha⟩, use [a, H], rwa ←h_top_iff_rpow_top a H, }, { -- second, suppose both `(∑ i in s, w i * z i) ^ p ≠ ⊤` and `∑ i in s, (w i * z i ^ p) ≠ ⊤`, -- and prove `((∑ i in s, w i * z i) ^ p).to_nnreal ≤ (∑ i in s, (w i * z i ^ p)).to_nnreal`, -- by using `nnreal.rpow_arith_mean_le_arith_mean_rpow`. intros h_top_rpow_sum _, -- show hypotheses needed to put the `.to_nnreal` inside the sums. have h_top : ∀ (a : ι), a ∈ s → w a * z a < ⊤, { have h_top_sum : ∑ (i : ι) in s, w i * z i < ⊤, { by_contra h, rw [lt_top_iff_ne_top, not_not] at h, rw [h, top_rpow_of_pos hp_pos] at h_top_rpow_sum, exact h_top_rpow_sum rfl, }, rwa sum_lt_top_iff at h_top_sum, }, have h_top_rpow : ∀ (a : ι), a ∈ s → w a * z a ^ p < ⊤, { intros i hi, specialize h_top i hi, rw lt_top_iff_ne_top at h_top ⊢, rwa [ne.def, ←h_top_iff_rpow_top i hi], }, -- put the `.to_nnreal` inside the sums. simp_rw [to_nnreal_sum h_top_rpow, ←to_nnreal_rpow, to_nnreal_sum h_top, to_nnreal_mul, ←to_nnreal_rpow], -- use corresponding nnreal result refine nnreal.rpow_arith_mean_le_arith_mean_rpow s (λ i, (w i).to_nnreal) (λ i, (z i).to_nnreal) _ hp, -- verify the hypothesis `∑ i in s, (w i).to_nnreal = 1`, using `∑ i in s, w i = 1` . have h_sum_nnreal : (∑ i in s, w i) = ↑(∑ i in s, (w i).to_nnreal), { have hw_top : ∑ i in s, w i < ⊤, by { rw hw', exact one_lt_top, }, rw ←to_nnreal_sum, { rw coe_to_nnreal, rwa ←lt_top_iff_ne_top, }, { rwa sum_lt_top_iff at hw_top, }, }, rwa [←coe_eq_coe, ←h_sum_nnreal], }, end /-- Weighted generalized mean inequality, version for two elements of `ℝ≥0∞` and real exponents. -/ theorem rpow_arith_mean_le_arith_mean2_rpow (w₁ w₂ z₁ z₂ : ℝ≥0∞) (hw' : w₁ + w₂ = 1) {p : ℝ} (hp : 1 ≤ p) : (w₁ * z₁ + w₂ * z₂) ^ p ≤ w₁ * z₁ ^ p + w₂ * z₂ ^ p := begin have h := rpow_arith_mean_le_arith_mean_rpow (univ : finset (fin 2)) (fin.cons w₁ $ fin.cons w₂ fin_zero_elim) (fin.cons z₁ $ fin.cons z₂ $ fin_zero_elim) _ hp, { simpa [fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero] using h, }, { simp [hw', fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero], }, end end ennreal namespace real theorem geom_mean_le_arith_mean2_weighted {w₁ w₂ p₁ p₂ : ℝ} (hw₁ : 0 ≤ w₁) (hw₂ : 0 ≤ w₂) (hp₁ : 0 ≤ p₁) (hp₂ : 0 ≤ p₂) (hw : w₁ + w₂ = 1) : p₁ ^ w₁ * p₂ ^ w₂ ≤ w₁ * p₁ + w₂ * p₂ := nnreal.geom_mean_le_arith_mean2_weighted ⟨w₁, hw₁⟩ ⟨w₂, hw₂⟩ ⟨p₁, hp₁⟩ ⟨p₂, hp₂⟩ $ nnreal.coe_eq.1 $ by assumption theorem geom_mean_le_arith_mean3_weighted {w₁ w₂ w₃ p₁ p₂ p₃ : ℝ} (hw₁ : 0 ≤ w₁) (hw₂ : 0 ≤ w₂) (hw₃ : 0 ≤ w₃) (hp₁ : 0 ≤ p₁) (hp₂ : 0 ≤ p₂) (hp₃ : 0 ≤ p₃) (hw : w₁ + w₂ + w₃ = 1) : p₁ ^ w₁ * p₂ ^ w₂ * p₃ ^ w₃ ≤ w₁ * p₁ + w₂ * p₂ + w₃ * p₃ := nnreal.geom_mean_le_arith_mean3_weighted ⟨w₁, hw₁⟩ ⟨w₂, hw₂⟩ ⟨w₃, hw₃⟩ ⟨p₁, hp₁⟩ ⟨p₂, hp₂⟩ ⟨p₃, hp₃⟩ $ nnreal.coe_eq.1 hw theorem geom_mean_le_arith_mean4_weighted {w₁ w₂ w₃ w₄ p₁ p₂ p₃ p₄ : ℝ} (hw₁ : 0 ≤ w₁) (hw₂ : 0 ≤ w₂) (hw₃ : 0 ≤ w₃) (hw₄ : 0 ≤ w₄) (hp₁ : 0 ≤ p₁) (hp₂ : 0 ≤ p₂) (hp₃ : 0 ≤ p₃) (hp₄ : 0 ≤ p₄) (hw : w₁ + w₂ + w₃ + w₄ = 1) : p₁ ^ w₁ * p₂ ^ w₂ * p₃ ^ w₃ * p₄ ^ w₄ ≤ w₁ * p₁ + w₂ * p₂ + w₃ * p₃ + w₄ * p₄ := nnreal.geom_mean_le_arith_mean4_weighted ⟨w₁, hw₁⟩ ⟨w₂, hw₂⟩ ⟨w₃, hw₃⟩ ⟨w₄, hw₄⟩ ⟨p₁, hp₁⟩ ⟨p₂, hp₂⟩ ⟨p₃, hp₃⟩ ⟨p₄, hp₄⟩ $ nnreal.coe_eq.1 $ by assumption /-- Young's inequality, a version for nonnegative real numbers. -/ theorem young_inequality_of_nonneg {a b p q : ℝ} (ha : 0 ≤ a) (hb : 0 ≤ b) (hpq : p.is_conjugate_exponent q) : a * b ≤ a^p / p + b^q / q := by simpa [← rpow_mul, ha, hb, hpq.ne_zero, hpq.symm.ne_zero, div_eq_inv_mul] using geom_mean_le_arith_mean2_weighted hpq.one_div_nonneg hpq.symm.one_div_nonneg (rpow_nonneg_of_nonneg ha p) (rpow_nonneg_of_nonneg hb q) hpq.inv_add_inv_conj /-- Young's inequality, a version for arbitrary real numbers. -/ theorem young_inequality (a b : ℝ) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : a * b ≤ (abs a)^p / p + (abs b)^q / q := calc a * b ≤ abs (a * b) : le_abs_self (a * b) ... = abs a * abs b : abs_mul a b ... ≤ (abs a)^p / p + (abs b)^q / q : real.young_inequality_of_nonneg (abs_nonneg a) (abs_nonneg b) hpq end real namespace nnreal /-- Young's inequality, `ℝ≥0` version. We use `{p q : ℝ≥0}` in order to avoid constructing witnesses of `0 ≤ p` and `0 ≤ q` for the denominators. -/ theorem young_inequality (a b : ℝ≥0) {p q : ℝ≥0} (hp : 1 < p) (hpq : 1 / p + 1 / q = 1) : a * b ≤ a^(p:ℝ) / p + b^(q:ℝ) / q := real.young_inequality_of_nonneg a.coe_nonneg b.coe_nonneg ⟨hp, nnreal.coe_eq.2 hpq⟩ /-- Young's inequality, `ℝ≥0` version with real conjugate exponents. -/ theorem young_inequality_real (a b : ℝ≥0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : a * b ≤ a ^ p / real.to_nnreal p + b ^ q / real.to_nnreal q := begin nth_rewrite 0 ← real.coe_to_nnreal p hpq.nonneg, nth_rewrite 0 ← real.coe_to_nnreal q hpq.symm.nonneg, exact young_inequality a b hpq.one_lt_nnreal hpq.inv_add_inv_conj_nnreal, end /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with `ℝ≥0`-valued functions. -/ theorem inner_le_Lp_mul_Lq (f g : ι → ℝ≥0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : ∑ i in s, f i * g i ≤ (∑ i in s, (f i) ^ p) ^ (1 / p) * (∑ i in s, (g i) ^ q) ^ (1 / q) := begin -- Let `G=∥g∥_q` be the `L_q`-norm of `g`. set G := (∑ i in s, (g i) ^ q) ^ (1 / q), have hGq : G ^ q = ∑ i in s, (g i) ^ q, { rw [← rpow_mul, one_div_mul_cancel hpq.symm.ne_zero, rpow_one], }, -- First consider the trivial case `∥g∥_q=0` by_cases hG : G = 0, { rw [hG, sum_eq_zero, mul_zero], intros i hi, simp only [rpow_eq_zero_iff, sum_eq_zero_iff] at hG, simp [(hG.1 i hi).1] }, { -- Move power from right to left rw [← div_le_iff hG, sum_div], -- Now the inequality follows from the weighted generalized mean inequality -- with weights `w_i` and numbers `z_i` given by the following formulas. set w : ι → ℝ≥0 := λ i, (g i) ^ q / G ^ q, set z : ι → ℝ≥0 := λ i, f i * (G / g i) ^ (q / p), -- Show that the sum of weights equals one have A : ∑ i in s, w i = 1, { rw [← sum_div, hGq, div_self], simpa [rpow_eq_zero_iff, hpq.symm.ne_zero] using hG }, -- LHS of the goal equals LHS of the weighted generalized mean inequality calc (∑ i in s, f i * g i / G) = (∑ i in s, w i * z i) : begin refine sum_congr rfl (λ i hi, _), have : q - q / p = 1, by field_simp [hpq.ne_zero, hpq.symm.mul_eq_add], dsimp only [w, z], rw [← div_rpow, mul_left_comm, mul_div_assoc, ← @inv_div _ _ _ G, inv_rpow, ← div_eq_mul_inv, ← rpow_sub']; simp [this] end -- Apply the generalized mean inequality ... ≤ (∑ i in s, w i * (z i) ^ p) ^ (1 / p) : nnreal.arith_mean_le_rpow_mean s w z A (le_of_lt hpq.one_lt) -- Simplify the right hand side. Terms with `g i ≠ 0` are equal to `(f i) ^ p`, -- the others are zeros. ... ≤ (∑ i in s, (f i) ^ p) ^ (1 / p) : begin refine rpow_le_rpow (sum_le_sum (λ i hi, _)) hpq.one_div_nonneg, dsimp only [w, z], rw [mul_rpow, mul_left_comm, ← rpow_mul _ _ p, div_mul_cancel _ hpq.ne_zero, div_rpow, div_mul_div, mul_comm (G ^ q), mul_div_mul_right], { nth_rewrite 1 [← mul_one ((f i) ^ p)], exact mul_le_mul_left' (div_self_le _) _ }, { simpa [hpq.symm.ne_zero] using hG } end } end /-- The `L_p` seminorm of a vector `f` is the greatest value of the inner product `∑ i in s, f i * g i` over functions `g` of `L_q` seminorm less than or equal to one. -/ theorem is_greatest_Lp (f : ι → ℝ≥0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : is_greatest ((λ g : ι → ℝ≥0, ∑ i in s, f i * g i) '' {g | ∑ i in s, (g i)^q ≤ 1}) ((∑ i in s, (f i)^p) ^ (1 / p)) := begin split, { use λ i, ((f i) ^ p / f i / (∑ i in s, (f i) ^ p) ^ (1 / q)), by_cases hf : ∑ i in s, (f i)^p = 0, { simp [hf, hpq.ne_zero, hpq.symm.ne_zero] }, { have A : p + q - q ≠ 0, by simp [hpq.ne_zero], have B : ∀ y : ℝ≥0, y * y^p / y = y^p, { refine λ y, mul_div_cancel_left_of_imp (λ h, _), simpa [h, hpq.ne_zero] }, simp only [set.mem_set_of_eq, div_rpow, ← sum_div, ← rpow_mul, div_mul_cancel _ hpq.symm.ne_zero, rpow_one, div_le_iff hf, one_mul, hpq.mul_eq_add, ← rpow_sub' _ A, _root_.add_sub_cancel, le_refl, true_and, ← mul_div_assoc, B], rw [div_eq_iff, ← rpow_add hf, hpq.inv_add_inv_conj, rpow_one], simpa [hpq.symm.ne_zero] using hf } }, { rintros _ ⟨g, hg, rfl⟩, apply le_trans (inner_le_Lp_mul_Lq s f g hpq), simpa only [mul_one] using mul_le_mul_left' (nnreal.rpow_le_one hg (le_of_lt hpq.symm.one_div_pos)) _ } end /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `nnreal`-valued functions. -/ theorem Lp_add_le (f g : ι → ℝ≥0) {p : ℝ} (hp : 1 ≤ p) : (∑ i in s, (f i + g i) ^ p) ^ (1 / p) ≤ (∑ i in s, (f i) ^ p) ^ (1 / p) + (∑ i in s, (g i) ^ p) ^ (1 / p) := begin -- The result is trivial when `p = 1`, so we can assume `1 < p`. rcases eq_or_lt_of_le hp with rfl|hp, { simp [finset.sum_add_distrib] }, have hpq := real.is_conjugate_exponent_conjugate_exponent hp, have := is_greatest_Lp s (f + g) hpq, simp only [pi.add_apply, add_mul, sum_add_distrib] at this, rcases this.1 with ⟨φ, hφ, H⟩, rw ← H, exact add_le_add ((is_greatest_Lp s f hpq).2 ⟨φ, hφ, rfl⟩) ((is_greatest_Lp s g hpq).2 ⟨φ, hφ, rfl⟩) end end nnreal namespace real variables (f g : ι → ℝ) {p q : ℝ} /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with real-valued functions. -/ theorem inner_le_Lp_mul_Lq (hpq : is_conjugate_exponent p q) : ∑ i in s, f i * g i ≤ (∑ i in s, (abs $ f i)^p) ^ (1 / p) * (∑ i in s, (abs $ g i)^q) ^ (1 / q) := begin have := nnreal.coe_le_coe.2 (nnreal.inner_le_Lp_mul_Lq s (λ i, ⟨_, abs_nonneg (f i)⟩) (λ i, ⟨_, abs_nonneg (g i)⟩) hpq), push_cast at this, refine le_trans (sum_le_sum $ λ i hi, _) this, simp only [← abs_mul, le_abs_self] end /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `real`-valued functions. -/ theorem Lp_add_le (hp : 1 ≤ p) : (∑ i in s, (abs $ f i + g i) ^ p) ^ (1 / p) ≤ (∑ i in s, (abs $ f i) ^ p) ^ (1 / p) + (∑ i in s, (abs $ g i) ^ p) ^ (1 / p) := begin have := nnreal.coe_le_coe.2 (nnreal.Lp_add_le s (λ i, ⟨_, abs_nonneg (f i)⟩) (λ i, ⟨_, abs_nonneg (g i)⟩) hp), push_cast at this, refine le_trans (rpow_le_rpow _ (sum_le_sum $ λ i hi, _) _) this; simp [sum_nonneg, rpow_nonneg_of_nonneg, abs_nonneg, le_trans zero_le_one hp, abs_add, rpow_le_rpow] end variables {f g} /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with real-valued nonnegative functions. -/ theorem inner_le_Lp_mul_Lq_of_nonneg (hpq : is_conjugate_exponent p q) (hf : ∀ i ∈ s, 0 ≤ f i) (hg : ∀ i ∈ s, 0 ≤ g i) : ∑ i in s, f i * g i ≤ (∑ i in s, (f i)^p) ^ (1 / p) * (∑ i in s, (g i)^q) ^ (1 / q) := by convert inner_le_Lp_mul_Lq s f g hpq using 3; apply sum_congr rfl; intros i hi; simp only [abs_of_nonneg, hf i hi, hg i hi] /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `real`-valued nonnegative functions. -/ theorem Lp_add_le_of_nonneg (hp : 1 ≤ p) (hf : ∀ i ∈ s, 0 ≤ f i) (hg : ∀ i ∈ s, 0 ≤ g i) : (∑ i in s, (f i + g i) ^ p) ^ (1 / p) ≤ (∑ i in s, (f i) ^ p) ^ (1 / p) + (∑ i in s, (g i) ^ p) ^ (1 / p) := by convert Lp_add_le s f g hp using 2 ; [skip, congr' 1, congr' 1]; apply sum_congr rfl; intros i hi; simp only [abs_of_nonneg, hf i hi, hg i hi, add_nonneg] end real namespace ennreal /-- Young's inequality, `ℝ≥0∞` version with real conjugate exponents. -/ theorem young_inequality (a b : ℝ≥0∞) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : a * b ≤ a ^ p / ennreal.of_real p + b ^ q / ennreal.of_real q := begin by_cases h : a = ⊤ ∨ b = ⊤, { refine le_trans le_top (le_of_eq _), repeat { rw div_eq_mul_inv }, cases h; rw h; simp [h, hpq.pos, hpq.symm.pos], }, push_neg at h, -- if a ≠ ⊤ and b ≠ ⊤, use the nnreal version: nnreal.young_inequality_real rw [←coe_to_nnreal h.left, ←coe_to_nnreal h.right, ←coe_mul, coe_rpow_of_nonneg _ hpq.nonneg, coe_rpow_of_nonneg _ hpq.symm.nonneg, ennreal.of_real, ennreal.of_real, ←@coe_div (real.to_nnreal p) _ (by simp [hpq.pos]), ←@coe_div (real.to_nnreal q) _ (by simp [hpq.symm.pos]), ←coe_add, coe_le_coe], exact nnreal.young_inequality_real a.to_nnreal b.to_nnreal hpq, end variables (f g : ι → ℝ≥0∞) {p q : ℝ} /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with `ℝ≥0∞`-valued functions. -/ theorem inner_le_Lp_mul_Lq (hpq : p.is_conjugate_exponent q) : (∑ i in s, f i * g i) ≤ (∑ i in s, (f i)^p) ^ (1/p) * (∑ i in s, (g i)^q) ^ (1/q) := begin by_cases H : (∑ i in s, (f i)^p) ^ (1/p) = 0 ∨ (∑ i in s, (g i)^q) ^ (1/q) = 0, { replace H : (∀ i ∈ s, f i = 0) ∨ (∀ i ∈ s, g i = 0), by simpa [ennreal.rpow_eq_zero_iff, hpq.pos, hpq.symm.pos, asymm hpq.pos, asymm hpq.symm.pos, sum_eq_zero_iff_of_nonneg] using H, have : ∀ i ∈ s, f i * g i = 0 := λ i hi, by cases H; simp [H i hi], have : (∑ i in s, f i * g i) = (∑ i in s, 0) := sum_congr rfl this, simp [this] }, push_neg at H, by_cases H' : (∑ i in s, (f i)^p) ^ (1/p) = ⊤ ∨ (∑ i in s, (g i)^q) ^ (1/q) = ⊤, { cases H'; simp [H', -one_div, H] }, replace H' : (∀ i ∈ s, f i ≠ ⊤) ∧ (∀ i ∈ s, g i ≠ ⊤), by simpa [ennreal.rpow_eq_top_iff, asymm hpq.pos, asymm hpq.symm.pos, hpq.pos, hpq.symm.pos, ennreal.sum_eq_top_iff, not_or_distrib] using H', have := ennreal.coe_le_coe.2 (@nnreal.inner_le_Lp_mul_Lq _ s (λ i, ennreal.to_nnreal (f i)) (λ i, ennreal.to_nnreal (g i)) _ _ hpq), simp [← ennreal.coe_rpow_of_nonneg, le_of_lt (hpq.pos), le_of_lt (hpq.one_div_pos), le_of_lt (hpq.symm.pos), le_of_lt (hpq.symm.one_div_pos)] at this, convert this using 1; [skip, congr' 2]; [skip, skip, simp, skip, simp]; { apply finset.sum_congr rfl (λ i hi, _), simp [H'.1 i hi, H'.2 i hi, -with_zero.coe_mul, with_top.coe_mul.symm] }, end /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `ℝ≥0∞` valued nonnegative functions. -/ theorem Lp_add_le (hp : 1 ≤ p) : (∑ i in s, (f i + g i) ^ p)^(1/p) ≤ (∑ i in s, (f i)^p) ^ (1/p) + (∑ i in s, (g i)^p) ^ (1/p) := begin by_cases H' : (∑ i in s, (f i)^p) ^ (1/p) = ⊤ ∨ (∑ i in s, (g i)^p) ^ (1/p) = ⊤, { cases H'; simp [H', -one_div] }, have pos : 0 < p := lt_of_lt_of_le zero_lt_one hp, replace H' : (∀ i ∈ s, f i ≠ ⊤) ∧ (∀ i ∈ s, g i ≠ ⊤), by simpa [ennreal.rpow_eq_top_iff, asymm pos, pos, ennreal.sum_eq_top_iff, not_or_distrib] using H', have := ennreal.coe_le_coe.2 (@nnreal.Lp_add_le _ s (λ i, ennreal.to_nnreal (f i)) (λ i, ennreal.to_nnreal (g i)) _ hp), push_cast [← ennreal.coe_rpow_of_nonneg, le_of_lt (pos), le_of_lt (one_div_pos.2 pos)] at this, convert this using 2; [skip, congr' 1, congr' 1]; { apply finset.sum_congr rfl (λ i hi, _), simp [H'.1 i hi, H'.2 i hi] } end private lemma add_rpow_le_one_of_add_le_one {p : ℝ} (a b : ℝ≥0∞) (hab : a + b ≤ 1) (hp1 : 1 ≤ p) : a ^ p + b ^ p ≤ 1 := begin have h_le_one : ∀ x : ℝ≥0∞, x ≤ 1 → x ^ p ≤ x, from λ x hx, rpow_le_self_of_le_one hx hp1, have ha : a ≤ 1, from (self_le_add_right a b).trans hab, have hb : b ≤ 1, from (self_le_add_left b a).trans hab, exact (add_le_add (h_le_one a ha) (h_le_one b hb)).trans hab, end lemma add_rpow_le_rpow_add {p : ℝ} (a b : ℝ≥0∞) (hp1 : 1 ≤ p) : a ^ p + b ^ p ≤ (a + b) ^ p := begin have hp_pos : 0 < p := lt_of_lt_of_le zero_lt_one hp1, by_cases h_top : a + b = ⊤, { rw ←@ennreal.rpow_eq_top_iff_of_pos (a + b) p hp_pos at h_top, rw h_top, exact le_top, }, obtain ⟨ha_top, hb_top⟩ := add_ne_top.mp h_top, by_cases h_zero : a + b = 0, { simp [add_eq_zero_iff.mp h_zero, ennreal.zero_rpow_of_pos hp_pos], }, have h_nonzero : ¬(a = 0 ∧ b = 0), by rwa add_eq_zero_iff at h_zero, have h_add : a/(a+b) + b/(a+b) = 1, by rw [div_add_div_same, div_self h_zero h_top], have h := add_rpow_le_one_of_add_le_one (a/(a+b)) (b/(a+b)) h_add.le hp1, rw [div_rpow_of_nonneg a (a+b) hp_pos.le, div_rpow_of_nonneg b (a+b) hp_pos.le] at h, have hab_0 : (a + b)^p ≠ 0, by simp [ha_top, hb_top, hp_pos, h_nonzero], have hab_top : (a + b)^p ≠ ⊤, by simp [ha_top, hb_top, hp_pos, h_nonzero], have h_mul : (a + b)^p * (a ^ p / (a + b) ^ p + b ^ p / (a + b) ^ p) ≤ (a + b)^p, { nth_rewrite 3 ←mul_one ((a + b)^p), exact (mul_le_mul_left hab_0 hab_top).mpr h, }, rwa [div_eq_mul_inv, div_eq_mul_inv, mul_add, mul_comm (a^p), mul_comm (b^p), ←mul_assoc, ←mul_assoc, mul_inv_cancel hab_0 hab_top, one_mul, one_mul] at h_mul, end lemma rpow_add_rpow_le_add {p : ℝ} (a b : ℝ≥0∞) (hp1 : 1 ≤ p) : (a ^ p + b ^ p) ^ (1/p) ≤ a + b := begin rw ←@ennreal.le_rpow_one_div_iff _ _ (1/p) (by simp [lt_of_lt_of_le zero_lt_one hp1]), rw one_div_one_div, exact add_rpow_le_rpow_add _ _ hp1, end theorem rpow_add_rpow_le {p q : ℝ} (a b : ℝ≥0∞) (hp_pos : 0 < p) (hpq : p ≤ q) : (a ^ q + b ^ q) ^ (1/q) ≤ (a ^ p + b ^ p) ^ (1/p) := begin have h_rpow : ∀ a : ℝ≥0∞, a^q = (a^p)^(q/p), from λ a, by rw [←ennreal.rpow_mul, div_eq_inv_mul, ←mul_assoc, _root_.mul_inv_cancel hp_pos.ne.symm, one_mul], have h_rpow_add_rpow_le_add : ((a^p)^(q/p) + (b^p)^(q/p)) ^ (1/(q/p)) ≤ a^p + b^p, { refine rpow_add_rpow_le_add (a^p) (b^p) _, rwa one_le_div hp_pos, }, rw [h_rpow a, h_rpow b, ennreal.le_rpow_one_div_iff hp_pos, ←ennreal.rpow_mul, mul_comm, mul_one_div], rwa one_div_div at h_rpow_add_rpow_le_add, end lemma rpow_add_le_add_rpow {p : ℝ} (a b : ℝ≥0∞) (hp_pos : 0 < p) (hp1 : p ≤ 1) : (a + b) ^ p ≤ a ^ p + b ^ p := begin have h := rpow_add_rpow_le a b hp_pos hp1, rw one_div_one at h, repeat { rw ennreal.rpow_one at h }, exact (ennreal.le_rpow_one_div_iff hp_pos).mp h, end end ennreal
2bf48166e5261af31d4c51b04edb8bc8a390e3c5
6f510b1ed724f95a55b7d26a8dcd13e1264123dd
/src/help.lean
ae75786763b57ae588f24ffce23f4a20482cc569
[]
no_license
jcommelin/oberharmersbach2019
adaf2e54ba4eff7c178c933978055ff4d6b0593b
d2cdf780a10baa8502a9b0cae01c7efa318649a6
refs/heads/master
1,587,558,516,731
1,550,558,213,000
1,550,558,213,000
170,372,753
0
0
null
null
null
null
UTF-8
Lean
false
false
881
lean
import tactic.interactive tactic.tidy namespace nat lemma dvd_add_iff_right_of_left (k m n : ℕ) (h : k ∣ m) : k ∣ n ↔ k ∣ m + n := nat.dvd_add_iff_right h lemma dvd_right_of_dvd_add_of_dvd_left (k m n : ℕ) (h : k ∣ m) : k ∣ m + n → k ∣ n := (dvd_add_iff_right_of_left _ _ _ h).mpr end nat namespace tactic namespace interactive open interactive interactive.types meta def use_this (l : parse pexpr_list_or_texpr) : tactic unit := (tactic.use l >> (triv <|> try `[apply exists_prop.mpr])) @[tidy] meta def auto_aux : tactic unit := `[solve_by_elim [fact_ne_zero ,nat.dvd_right_of_dvd_add_of_dvd_left ,prime.not_dvd_one ,prime.pos ,nat.min_fac_dvd ,nat.dvd_fact ,le_of_not_ge]] meta def auto : tactic unit := auto_aux <|> tidy end interactive end tactic
702def3de3793431a919000c0c22a2fe5bf71d66
46125763b4dbf50619e8846a1371029346f4c3db
/src/set_theory/cardinal.lean
b750ab7bd894315ca579337d0dbe49918cbd0b30
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
45,226
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl, Mario Carneiro -/ import data.set.countable data.quot logic.function set_theory.schroeder_bernstein import data.fintype.card /-! # Cardinal Numbers We define cardinal numbers as a quotient of types under the equivalence relation of equinumerity. We define the order on cardinal numbers, define omega, and do basic cardinal arithmetic: addition, multiplication, power, cardinal successor, minimum, supremum, infinitary sums and products ## Implementation notes * There is a type of cardinal numbers in every universe level: `cardinal.{u} : Type (u + 1)` is the quotient of types in `Type u`. There is a lift operation lifting cardinal numbers to a higher level. * Cardinal arithmetic specifically for infinite cardinals (like `κ * κ = κ`) is in the file `set_theory/ordinal.lean`, because concepts from that file are used in the proof. ## References * <https://en.wikipedia.org/wiki/Cardinal_number> ## Tags cardinal number, cardinal arithmetic, cardinal exponentiation, omega -/ open function lattice set open_locale classical universes u v w x variables {α β : Type u} /-- The equivalence relation on types given by equivalence (bijective correspondence) of types. Quotienting by this equivalence relation gives the cardinal numbers. -/ instance cardinal.is_equivalent : setoid (Type u) := { r := λα β, nonempty (α ≃ β), iseqv := ⟨λα, ⟨equiv.refl α⟩, λα β ⟨e⟩, ⟨e.symm⟩, λα β γ ⟨e₁⟩ ⟨e₂⟩, ⟨e₁.trans e₂⟩⟩ } /-- `cardinal.{u}` is the type of cardinal numbers in `Type u`, defined as the quotient of `Type u` by existence of an equivalence (a bijection with explicit inverse). -/ def cardinal : Type (u + 1) := quotient cardinal.is_equivalent namespace cardinal /-- The cardinal number of a type -/ def mk : Type u → cardinal := quotient.mk localized "notation `#` := cardinal.mk" in cardinal protected lemma eq : mk α = mk β ↔ nonempty (α ≃ β) := quotient.eq @[simp] theorem mk_def (α : Type u) : @eq cardinal ⟦α⟧ (mk α) := rfl @[simp] theorem mk_out (c : cardinal) : mk (c.out) = c := quotient.out_eq _ /-- We define the order on cardinal numbers by `mk α ≤ mk β` if and only if there exists an embedding (injective function) from α to β. -/ instance : has_le cardinal.{u} := ⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, nonempty $ α ↪ β) $ assume α β γ δ ⟨e₁⟩ ⟨e₂⟩, propext ⟨assume ⟨e⟩, ⟨e.congr e₁ e₂⟩, assume ⟨e⟩, ⟨e.congr e₁.symm e₂.symm⟩⟩⟩ theorem mk_le_of_injective {α β : Type u} {f : α → β} (hf : injective f) : mk α ≤ mk β := ⟨⟨f, hf⟩⟩ theorem mk_le_of_surjective {α β : Type u} {f : α → β} (hf : surjective f) : mk β ≤ mk α := ⟨embedding.of_surjective hf⟩ theorem le_mk_iff_exists_set {c : cardinal} {α : Type u} : c ≤ mk α ↔ ∃ p : set α, mk p = c := ⟨quotient.induction_on c $ λ β ⟨⟨f, hf⟩⟩, ⟨set.range f, eq.symm $ quot.sound ⟨equiv.set.range f hf⟩⟩, λ ⟨p, e⟩, e ▸ ⟨⟨subtype.val, λ a b, subtype.eq⟩⟩⟩ theorem out_embedding {c c' : cardinal} : c ≤ c' ↔ nonempty (c.out ↪ c'.out) := by { transitivity _, rw [←quotient.out_eq c, ←quotient.out_eq c'], refl } instance : linear_order cardinal.{u} := { le := (≤), le_refl := by rintros ⟨α⟩; exact ⟨embedding.refl _⟩, le_trans := by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨e₁⟩ ⟨e₂⟩; exact ⟨e₁.trans e₂⟩, le_antisymm := by rintros ⟨α⟩ ⟨β⟩ ⟨e₁⟩ ⟨e₂⟩; exact quotient.sound (e₁.antisymm e₂), le_total := by rintros ⟨α⟩ ⟨β⟩; exact embedding.total } noncomputable instance : decidable_linear_order cardinal.{u} := classical.DLO _ noncomputable instance : distrib_lattice cardinal.{u} := by apply_instance -- short-circuit type class inference instance : has_zero cardinal.{u} := ⟨⟦pempty⟧⟩ instance : inhabited cardinal.{u} := ⟨0⟩ theorem ne_zero_iff_nonempty {α : Type u} : mk α ≠ 0 ↔ nonempty α := not_iff_comm.1 ⟨λ h, quotient.sound ⟨(equiv.empty_of_not_nonempty h).trans equiv.empty_equiv_pempty⟩, λ e, let ⟨h⟩ := quotient.exact e in λ ⟨a⟩, (h a).elim⟩ instance : has_one cardinal.{u} := ⟨⟦punit⟧⟩ instance : zero_ne_one_class cardinal.{u} := { zero := 0, one := 1, zero_ne_one := ne.symm $ ne_zero_iff_nonempty.2 ⟨punit.star⟩ } theorem le_one_iff_subsingleton {α : Type u} : mk α ≤ 1 ↔ subsingleton α := ⟨λ ⟨f⟩, ⟨λ a b, f.inj (subsingleton.elim _ _)⟩, λ ⟨h⟩, ⟨⟨λ a, punit.star, λ a b _, h _ _⟩⟩⟩ instance : has_add cardinal.{u} := ⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, mk (α ⊕ β)) $ assume α β γ δ ⟨e₁⟩ ⟨e₂⟩, quotient.sound ⟨equiv.sum_congr e₁ e₂⟩⟩ @[simp] theorem add_def (α β) : mk α + mk β = mk (α ⊕ β) := rfl instance : has_mul cardinal.{u} := ⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, mk (α × β)) $ assume α β γ δ ⟨e₁⟩ ⟨e₂⟩, quotient.sound ⟨equiv.prod_congr e₁ e₂⟩⟩ @[simp] theorem mul_def (α β : Type u) : mk α * mk β = mk (α × β) := rfl private theorem add_comm (a b : cardinal.{u}) : a + b = b + a := quotient.induction_on₂ a b $ assume α β, quotient.sound ⟨equiv.sum_comm α β⟩ private theorem mul_comm (a b : cardinal.{u}) : a * b = b * a := quotient.induction_on₂ a b $ assume α β, quotient.sound ⟨equiv.prod_comm α β⟩ private theorem zero_add (a : cardinal.{u}) : 0 + a = a := quotient.induction_on a $ assume α, quotient.sound ⟨equiv.pempty_sum α⟩ private theorem zero_mul (a : cardinal.{u}) : 0 * a = 0 := quotient.induction_on a $ assume α, quotient.sound ⟨equiv.pempty_prod α⟩ private theorem one_mul (a : cardinal.{u}) : 1 * a = a := quotient.induction_on a $ assume α, quotient.sound ⟨equiv.punit_prod α⟩ private theorem left_distrib (a b c : cardinal.{u}) : a * (b + c) = a * b + a * c := quotient.induction_on₃ a b c $ assume α β γ, quotient.sound ⟨equiv.prod_sum_distrib α β γ⟩ instance : comm_semiring cardinal.{u} := { zero := 0, one := 1, add := (+), mul := (*), zero_add := zero_add, add_zero := assume a, by rw [add_comm a 0, zero_add a], add_assoc := λa b c, quotient.induction_on₃ a b c $ assume α β γ, quotient.sound ⟨equiv.sum_assoc α β γ⟩, add_comm := add_comm, zero_mul := zero_mul, mul_zero := assume a, by rw [mul_comm a 0, zero_mul a], one_mul := one_mul, mul_one := assume a, by rw [mul_comm a 1, one_mul a], mul_assoc := λa b c, quotient.induction_on₃ a b c $ assume α β γ, quotient.sound ⟨equiv.prod_assoc α β γ⟩, mul_comm := mul_comm, left_distrib := left_distrib, right_distrib := assume a b c, by rw [mul_comm (a + b) c, left_distrib c a b, mul_comm c a, mul_comm c b] } /-- The cardinal exponential. `mk α ^ mk β` is the cardinal of `β → α`. -/ protected def power (a b : cardinal.{u}) : cardinal.{u} := quotient.lift_on₂ a b (λα β, mk (β → α)) $ assume α₁ α₂ β₁ β₂ ⟨e₁⟩ ⟨e₂⟩, quotient.sound ⟨equiv.arrow_congr e₂ e₁⟩ instance : has_pow cardinal cardinal := ⟨cardinal.power⟩ local infixr ^ := @has_pow.pow cardinal cardinal cardinal.has_pow @[simp] theorem power_def (α β) : mk α ^ mk β = mk (β → α) := rfl @[simp] theorem power_zero {a : cardinal} : a ^ 0 = 1 := quotient.induction_on a $ assume α, quotient.sound ⟨equiv.pempty_arrow_equiv_punit α⟩ @[simp] theorem power_one {a : cardinal} : a ^ 1 = a := quotient.induction_on a $ assume α, quotient.sound ⟨equiv.punit_arrow_equiv α⟩ @[simp] theorem one_power {a : cardinal} : 1 ^ a = 1 := quotient.induction_on a $ assume α, quotient.sound ⟨equiv.arrow_punit_equiv_punit α⟩ @[simp] theorem prop_eq_two : mk (ulift Prop) = 2 := quot.sound ⟨equiv.ulift.trans $ equiv.Prop_equiv_bool.trans equiv.bool_equiv_punit_sum_punit⟩ @[simp] theorem zero_power {a : cardinal} : a ≠ 0 → 0 ^ a = 0 := quotient.induction_on a $ assume α heq, nonempty.rec_on (ne_zero_iff_nonempty.1 heq) $ assume a, quotient.sound ⟨equiv.equiv_pempty $ assume f, pempty.rec (λ _, false) (f a)⟩ theorem power_ne_zero {a : cardinal} (b) : a ≠ 0 → a ^ b ≠ 0 := quotient.induction_on₂ a b $ λ α β h, let ⟨a⟩ := ne_zero_iff_nonempty.1 h in ne_zero_iff_nonempty.2 ⟨λ _, a⟩ theorem mul_power {a b c : cardinal} : (a * b) ^ c = a ^ c * b ^ c := quotient.induction_on₃ a b c $ assume α β γ, quotient.sound ⟨equiv.arrow_prod_equiv_prod_arrow α β γ⟩ theorem power_add {a b c : cardinal} : a ^ (b + c) = a ^ b * a ^ c := quotient.induction_on₃ a b c $ assume α β γ, quotient.sound ⟨equiv.sum_arrow_equiv_prod_arrow β γ α⟩ theorem power_mul {a b c : cardinal} : (a ^ b) ^ c = a ^ (b * c) := by rw [_root_.mul_comm b c]; from (quotient.induction_on₃ a b c $ assume α β γ, quotient.sound ⟨equiv.arrow_arrow_equiv_prod_arrow γ β α⟩) @[simp] lemma pow_cast_right (κ : cardinal.{u}) : ∀ n : ℕ, (κ ^ (↑n : cardinal.{u})) = @has_pow.pow _ _ monoid.has_pow κ n | 0 := by simp | (_+1) := by rw [nat.cast_succ, power_add, power_one, _root_.mul_comm, pow_succ, pow_cast_right] section order_properties open sum theorem zero_le : ∀(a : cardinal), 0 ≤ a := by rintro ⟨α⟩; exact ⟨embedding.of_not_nonempty $ λ ⟨a⟩, a.elim⟩ theorem le_zero (a : cardinal) : a ≤ 0 ↔ a = 0 := by simp [le_antisymm_iff, zero_le] theorem pos_iff_ne_zero {o : cardinal} : 0 < o ↔ o ≠ 0 := by simp [lt_iff_le_and_ne, eq_comm, zero_le] theorem zero_lt_one : (0 : cardinal) < 1 := lt_of_le_of_ne (zero_le _) zero_ne_one lemma zero_power_le (c : cardinal.{u}) : (0 : cardinal.{u}) ^ c ≤ 1 := by { by_cases h : c = 0, rw [h, power_zero], rw [zero_power h], apply zero_le } theorem add_le_add : ∀{a b c d : cardinal}, a ≤ b → c ≤ d → a + c ≤ b + d := by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨δ⟩ ⟨e₁⟩ ⟨e₂⟩; exact ⟨embedding.sum_congr e₁ e₂⟩ theorem add_le_add_left (a) {b c : cardinal} : b ≤ c → a + b ≤ a + c := add_le_add (le_refl _) theorem add_le_add_right {a b : cardinal} (c) (h : a ≤ b) : a + c ≤ b + c := add_le_add h (le_refl _) theorem le_add_right (a b : cardinal) : a ≤ a + b := by simpa using add_le_add_left a (zero_le b) theorem le_add_left (a b : cardinal) : a ≤ b + a := by simpa using add_le_add_right a (zero_le b) theorem mul_le_mul : ∀{a b c d : cardinal}, a ≤ b → c ≤ d → a * c ≤ b * d := by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨δ⟩ ⟨e₁⟩ ⟨e₂⟩; exact ⟨embedding.prod_congr e₁ e₂⟩ theorem mul_le_mul_left (a) {b c : cardinal} : b ≤ c → a * b ≤ a * c := mul_le_mul (le_refl _) theorem mul_le_mul_right {a b : cardinal} (c) (h : a ≤ b) : a * c ≤ b * c := mul_le_mul h (le_refl _) theorem power_le_power_left : ∀{a b c : cardinal}, a ≠ 0 → b ≤ c → a ^ b ≤ a ^ c := by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ hα ⟨e⟩; exact let ⟨a⟩ := ne_zero_iff_nonempty.1 hα in ⟨@embedding.arrow_congr_right _ _ _ ⟨a⟩ e⟩ theorem power_le_max_power_one {a b c : cardinal} (h : b ≤ c) : a ^ b ≤ max (a ^ c) 1 := begin by_cases ha : a = 0, simp [ha, zero_power_le], exact le_trans (power_le_power_left ha h) (le_max_left _ _) end theorem power_le_power_right {a b c : cardinal} : a ≤ b → a ^ c ≤ b ^ c := quotient.induction_on₃ a b c $ assume α β γ ⟨e⟩, ⟨embedding.arrow_congr_left e⟩ theorem le_iff_exists_add {a b : cardinal} : a ≤ b ↔ ∃ c, b = a + c := ⟨quotient.induction_on₂ a b $ λ α β ⟨⟨f, hf⟩⟩, have (α ⊕ ↥-range f) ≃ β, from (equiv.sum_congr (equiv.set.range f hf) (equiv.refl _)).trans $ (equiv.set.sum_compl (range f)), ⟨⟦(-range f : set β)⟧, quotient.sound ⟨this.symm⟩⟩, λ ⟨c, e⟩, add_zero a ▸ e.symm ▸ add_le_add_left _ (zero_le _)⟩ end order_properties instance : order_bot cardinal.{u} := { bot := 0, bot_le := zero_le, ..cardinal.linear_order } instance : canonically_ordered_monoid cardinal.{u} := { add_le_add_left := λ a b h c, add_le_add_left _ h, lt_of_add_lt_add_left := λ a b c, lt_imp_lt_of_le_imp_le (add_le_add_left _), le_iff_exists_add := @le_iff_exists_add, ..cardinal.lattice.order_bot, ..cardinal.comm_semiring, ..cardinal.linear_order } theorem cantor : ∀(a : cardinal.{u}), a < 2 ^ a := by rw ← prop_eq_two; rintros ⟨a⟩; exact ⟨ ⟨⟨λ a b, ⟨a = b⟩, λ a b h, cast (ulift.up.inj (@congr_fun _ _ _ _ h b)).symm rfl⟩⟩, λ ⟨⟨f, hf⟩⟩, cantor_injective (λ s, f (λ a, ⟨s a⟩)) $ λ s t h, by funext a; injection congr_fun (hf h) a⟩ instance : no_top_order cardinal.{u} := { no_top := λ a, ⟨_, cantor a⟩, ..cardinal.linear_order } /-- The minimum cardinal in a family of cardinals (the existence of which is provided by `injective_min`). -/ noncomputable def min {ι} (I : nonempty ι) (f : ι → cardinal) : cardinal := f $ classical.some $ @embedding.injective_min _ (λ i, (f i).out) I theorem min_eq {ι} (I) (f : ι → cardinal) : ∃ i, min I f = f i := ⟨_, rfl⟩ theorem min_le {ι I} (f : ι → cardinal) (i) : min I f ≤ f i := by rw [← mk_out (min I f), ← mk_out (f i)]; exact let ⟨g⟩ := classical.some_spec (@embedding.injective_min _ (λ i, (f i).out) I) in ⟨g i⟩ theorem le_min {ι I} {f : ι → cardinal} {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⟩ protected theorem wf : @well_founded cardinal.{u} (<) := ⟨λ a, classical.by_contradiction $ λ h, let ι := {c :cardinal // ¬ acc (<) c}, f : ι → cardinal := subtype.val, ⟨⟨c, hc⟩, hi⟩ := @min_eq ι ⟨⟨_, h⟩⟩ f in hc (acc.intro _ (λ j ⟨_, h'⟩, classical.by_contradiction $ λ hj, h' $ by have := min_le f ⟨j, hj⟩; rwa hi at this))⟩ instance has_wf : @has_well_founded cardinal.{u} := ⟨(<), cardinal.wf⟩ instance wo : @is_well_order cardinal.{u} (<) := ⟨cardinal.wf⟩ /-- The successor cardinal - the smallest cardinal greater than `c`. This is not the same as `c + 1` except in the case of finite `c`. -/ noncomputable def succ (c : cardinal) : cardinal := @min {c' // c < c'} ⟨⟨_, cantor _⟩⟩ subtype.val theorem lt_succ_self (c : cardinal) : c < succ c := by cases min_eq _ _ with s e; rw [succ, e]; exact s.2 theorem succ_le {a b : cardinal} : succ a ≤ b ↔ a < b := ⟨lt_of_lt_of_le (lt_succ_self _), λ h, by exact min_le _ (subtype.mk b h)⟩ theorem lt_succ {a b : cardinal} : a < succ b ↔ a ≤ b := by rw [← not_le, succ_le, not_lt] theorem add_one_le_succ (c : cardinal) : c + 1 ≤ succ c := begin refine quot.induction_on c (λ α, _) (lt_succ_self c), refine quot.induction_on (succ (quot.mk setoid.r α)) (λ β h, _), cases h.left with f, have : ¬ surjective f := λ hn, ne_of_lt h (quotient.sound ⟨equiv.of_bijective ⟨f.inj, hn⟩⟩), cases classical.not_forall.1 this with b nex, refine ⟨⟨sum.rec (by exact f) _, _⟩⟩, { exact λ _, b }, { intros a b h, rcases a with a|⟨⟨⟨⟩⟩⟩; rcases b with b|⟨⟨⟨⟩⟩⟩, { rw f.inj h }, { exact nex.elim ⟨_, h⟩ }, { exact nex.elim ⟨_, h.symm⟩ }, { refl } } end lemma succ_ne_zero (c : cardinal) : succ c ≠ 0 := by { rw [←pos_iff_ne_zero, lt_succ], apply zero_le } /-- The indexed sum of cardinals is the cardinality of the indexed disjoint union, i.e. sigma type. -/ def sum {ι} (f : ι → cardinal) : cardinal := mk Σ i, (f i).out theorem le_sum {ι} (f : ι → cardinal) (i) : f i ≤ sum f := by rw ← quotient.out_eq (f i); exact ⟨⟨λ a, ⟨i, a⟩, λ a b h, eq_of_heq $ by injection h⟩⟩ @[simp] theorem sum_mk {ι} (f : ι → Type*) : sum (λ i, mk (f i)) = mk (Σ i, f i) := quot.sound ⟨equiv.sigma_congr_right $ λ i, classical.choice $ quotient.exact $ quot.out_eq $ mk (f i)⟩ theorem sum_const (ι : Type u) (a : cardinal.{u}) : sum (λ _:ι, a) = mk ι * a := quotient.induction_on a $ λ α, by simp; exact quotient.sound ⟨equiv.sigma_equiv_prod _ _⟩ theorem sum_le_sum {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : sum f ≤ sum g := ⟨embedding.sigma_congr_right $ λ i, classical.choice $ by have := H i; rwa [← quot.out_eq (f i), ← quot.out_eq (g i)] at this⟩ /-- The indexed supremum of cardinals is the smallest cardinal above everything in the family. -/ noncomputable def sup {ι} (f : ι → cardinal) : cardinal := @min {c // ∀ i, f i ≤ c} ⟨⟨sum f, le_sum f⟩⟩ (λ a, a.1) theorem le_sup {ι} (f : ι → cardinal) (i) : f i ≤ sup f := by dsimp [sup]; cases min_eq _ _ with c hc; rw hc; exact c.2 i theorem sup_le {ι} {f : ι → cardinal} {a} : sup f ≤ a ↔ ∀ i, f i ≤ a := ⟨λ h i, le_trans (le_sup _ _) h, λ h, by dsimp [sup]; change a with (⟨a, h⟩:subtype _).1; apply min_le⟩ theorem sup_le_sup {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : sup f ≤ sup g := sup_le.2 $ λ i, le_trans (H i) (le_sup _ _) theorem sup_le_sum {ι} (f : ι → cardinal) : sup f ≤ sum f := sup_le.2 $ le_sum _ theorem sum_le_sup {ι : Type u} (f : ι → cardinal.{u}) : sum f ≤ mk ι * sup.{u u} f := by rw ← sum_const; exact sum_le_sum _ _ (le_sup _) theorem sup_eq_zero {ι} {f : ι → cardinal} (h : ι → false) : sup f = 0 := by { rw [←le_zero, sup_le], intro x, exfalso, exact h x } /-- The indexed product of cardinals is the cardinality of the Pi type (dependent product). -/ def prod {ι : Type u} (f : ι → cardinal) : cardinal := mk (Π i, (f i).out) @[simp] theorem prod_mk {ι} (f : ι → Type*) : prod (λ i, mk (f i)) = mk (Π i, f i) := quot.sound ⟨equiv.Pi_congr_right $ λ i, classical.choice $ quotient.exact $ mk_out $ mk (f i)⟩ theorem prod_const (ι : Type u) (a : cardinal.{u}) : prod (λ _:ι, a) = a ^ mk ι := quotient.induction_on a $ by simp theorem prod_le_prod {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : prod f ≤ prod g := ⟨embedding.Pi_congr_right $ λ i, classical.choice $ by have := H i; rwa [← mk_out (f i), ← mk_out (g i)] at this⟩ theorem prod_ne_zero {ι} (f : ι → cardinal) : prod f ≠ 0 ↔ ∀ i, f i ≠ 0 := begin conv in (f _) {rw ← mk_out (f i)}, simp [prod, ne_zero_iff_nonempty, -mk_out, -ne.def], exact ⟨λ ⟨F⟩ i, ⟨F i⟩, λ h, ⟨λ i, classical.choice (h i)⟩⟩, end theorem prod_eq_zero {ι} (f : ι → cardinal) : prod f = 0 ↔ ∃ i, f i = 0 := not_iff_not.1 $ by simpa using prod_ne_zero f /-- The universe lift operation on cardinals. You can specify the universes explicitly with `lift.{u v} : cardinal.{u} → cardinal.{max u v}` -/ def lift (c : cardinal.{u}) : cardinal.{max u v} := quotient.lift_on c (λ α, ⟦ulift α⟧) $ λ α β ⟨e⟩, quotient.sound ⟨equiv.ulift.trans $ e.trans equiv.ulift.symm⟩ theorem lift_mk (α) : lift.{u v} (mk α) = mk (ulift.{v u} α) := rfl theorem lift_umax : lift.{u (max u v)} = lift.{u v} := funext $ λ a, quot.induction_on a $ λ α, quotient.sound ⟨equiv.ulift.trans equiv.ulift.symm⟩ theorem lift_id' (a : cardinal) : lift a = a := quot.induction_on a $ λ α, quot.sound ⟨equiv.ulift⟩ @[simp] theorem lift_id : ∀ a, lift.{u u} a = a := lift_id'.{u u} @[simp] theorem lift_lift (a : cardinal) : lift.{(max u v) w} (lift.{u v} a) = lift.{u (max v w)} a := quot.induction_on a $ λ α, quotient.sound ⟨equiv.ulift.trans $ equiv.ulift.trans equiv.ulift.symm⟩ theorem lift_mk_le {α : Type u} {β : Type v} : lift.{u (max v w)} (mk α) ≤ lift.{v (max u w)} (mk β) ↔ nonempty (α ↪ β) := ⟨λ ⟨f⟩, ⟨embedding.congr equiv.ulift equiv.ulift f⟩, λ ⟨f⟩, ⟨embedding.congr equiv.ulift.symm equiv.ulift.symm f⟩⟩ theorem lift_mk_eq {α : Type u} {β : Type v} : lift.{u (max v w)} (mk α) = lift.{v (max u w)} (mk β) ↔ nonempty (α ≃ β) := quotient.eq.trans ⟨λ ⟨f⟩, ⟨equiv.ulift.symm.trans $ f.trans equiv.ulift⟩, λ ⟨f⟩, ⟨equiv.ulift.trans $ f.trans equiv.ulift.symm⟩⟩ @[simp] theorem lift_le {a b : cardinal} : lift a ≤ lift b ↔ a ≤ b := quotient.induction_on₂ a b $ λ α β, by rw ← lift_umax; exact lift_mk_le @[simp] theorem lift_inj {a b : cardinal} : lift a = lift b ↔ a = b := by simp [le_antisymm_iff] @[simp] theorem lift_lt {a b : cardinal} : lift a < lift b ↔ a < b := by simp [lt_iff_le_not_le, -not_le] @[simp] theorem lift_zero : lift 0 = 0 := quotient.sound ⟨equiv.ulift.trans equiv.pempty_equiv_pempty⟩ @[simp] theorem lift_one : lift 1 = 1 := quotient.sound ⟨equiv.ulift.trans equiv.punit_equiv_punit⟩ @[simp] theorem lift_add (a b) : lift (a + b) = lift a + lift b := quotient.induction_on₂ a b $ λ α β, quotient.sound ⟨equiv.ulift.trans (equiv.sum_congr equiv.ulift equiv.ulift).symm⟩ @[simp] theorem lift_mul (a b) : lift (a * b) = lift a * lift b := quotient.induction_on₂ a b $ λ α β, quotient.sound ⟨equiv.ulift.trans (equiv.prod_congr equiv.ulift equiv.ulift).symm⟩ @[simp] theorem lift_power (a b) : lift (a ^ b) = lift a ^ lift b := quotient.induction_on₂ a b $ λ α β, quotient.sound ⟨equiv.ulift.trans (equiv.arrow_congr equiv.ulift equiv.ulift).symm⟩ @[simp] theorem lift_two_power (a) : lift (2 ^ a) = 2 ^ lift a := by simp [bit0] @[simp] theorem lift_min {ι I} (f : ι → cardinal) : 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) theorem lift_down {a : cardinal.{u}} {b : cardinal.{max u v}} : b ≤ lift a → ∃ a', lift a' = b := quotient.induction_on₂ a b $ λ α β, by dsimp; rw [← lift_id (mk β), ← lift_umax, ← lift_umax.{u v}, lift_mk_le]; exact λ ⟨f⟩, ⟨mk (set.range f), eq.symm $ lift_mk_eq.2 ⟨embedding.equiv_of_surjective (embedding.cod_restrict _ f set.mem_range_self) $ λ ⟨a, ⟨b, e⟩⟩, ⟨b, subtype.eq e⟩⟩⟩ theorem le_lift_iff {a : cardinal.{u}} {b : cardinal.{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 : cardinal.{u}} {b : cardinal.{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⟩ @[simp] theorem lift_succ (a) : lift (succ a) = succ (lift a) := le_antisymm (le_of_not_gt $ λ h, begin rcases lt_lift_iff.1 h with ⟨b, e, h⟩, rw [lt_succ, ← lift_le, e] at h, exact not_lt_of_le h (lt_succ_self _) end) (succ_le.2 $ lift_lt.2 $ lt_succ_self _) @[simp] theorem lift_max {a : cardinal.{u}} {b : cardinal.{v}} : lift.{u (max v w)} a = lift.{v (max u w)} b ↔ lift.{u v} a = lift.{v u} b := calc lift.{u (max v w)} a = lift.{v (max u w)} b ↔ lift.{(max u v) w} (lift.{u v} a) = lift.{(max u v) w} (lift.{v u} b) : by simp ... ↔ lift.{u v} a = lift.{v u} b : lift_inj theorem mk_prod {α : Type u} {β : Type v} : mk (α × β) = lift.{u v} (mk α) * lift.{v u} (mk β) := quotient.sound ⟨equiv.prod_congr (equiv.ulift).symm (equiv.ulift).symm⟩ theorem sum_const_eq_lift_mul (ι : Type u) (a : cardinal.{v}) : sum (λ _:ι, a) = lift.{u v} (mk ι) * lift.{v u} a := begin apply quotient.induction_on a, intro α, simp only [cardinal.mk_def, cardinal.sum_mk, cardinal.lift_id], convert mk_prod using 1, exact quotient.sound ⟨equiv.sigma_equiv_prod ι α⟩, end /-- `ω` is the smallest infinite cardinal, also known as ℵ₀. -/ def omega : cardinal.{u} := lift (mk ℕ) lemma mk_nat : mk nat = omega := (lift_id _).symm theorem omega_ne_zero : omega ≠ 0 := ne_zero_iff_nonempty.2 ⟨⟨0⟩⟩ theorem omega_pos : 0 < omega := pos_iff_ne_zero.2 omega_ne_zero @[simp] theorem lift_omega : lift omega = omega := lift_lift _ /- properties about the cast from nat -/ @[simp] theorem mk_fin : ∀ (n : ℕ), mk (fin n) = n | 0 := quotient.sound ⟨(equiv.pempty_of_not_nonempty $ λ ⟨h⟩, h.elim0)⟩ | (n+1) := by rw [nat.cast_succ, ← mk_fin]; exact quotient.sound (fintype.card_eq.1 $ by simp) @[simp] theorem lift_nat_cast (n : ℕ) : lift n = n := by induction n; simp * lemma lift_eq_nat_iff {a : cardinal.{u}} {n : ℕ} : lift.{u v} a = n ↔ a = n := by rw [← lift_nat_cast.{u v} n, lift_inj] lemma nat_eq_lift_eq_iff {n : ℕ} {a : cardinal.{u}} : (n : cardinal) = lift.{u v} a ↔ (n : cardinal) = a := by rw [← lift_nat_cast.{u v} n, lift_inj] theorem lift_mk_fin (n : ℕ) : lift (mk (fin n)) = n := by simp theorem fintype_card (α : Type u) [fintype α] : mk α = fintype.card α := by rw [← lift_mk_fin.{u}, ← lift_id (mk α), lift_mk_eq.{u 0 u}]; exact fintype.card_eq.1 (by simp) theorem card_le_of_finset {α} (s : finset α) : (s.card : cardinal) ≤ cardinal.mk α := begin rw (_ : (s.card : cardinal) = cardinal.mk (↑s : set α)), { exact ⟨function.embedding.subtype _⟩ }, rw [cardinal.fintype_card, fintype.card_coe] end @[simp, move_cast] theorem nat_cast_pow {m n : ℕ} : (↑(pow m n) : cardinal) = m ^ n := by induction n; simp [nat.pow_succ, -_root_.add_comm, power_add, *] @[simp, elim_cast] theorem nat_cast_le {m n : ℕ} : (m : cardinal) ≤ n ↔ m ≤ n := by rw [← lift_mk_fin, ← lift_mk_fin, lift_le]; exact ⟨λ ⟨⟨f, hf⟩⟩, begin have : _ = fintype.card _ := finset.card_image_of_injective finset.univ hf, simp at this, rw [← fintype.card_fin n, ← this], exact finset.card_le_of_subset (finset.subset_univ _) end, λ h, ⟨⟨λ i, ⟨i.1, lt_of_lt_of_le i.2 h⟩, λ a b h, have _, from fin.veq_of_eq h, fin.eq_of_veq this⟩⟩⟩ @[simp, elim_cast] theorem nat_cast_lt {m n : ℕ} : (m : cardinal) < n ↔ m < n := by simp [lt_iff_le_not_le, -not_le] @[simp, elim_cast] theorem nat_cast_inj {m n : ℕ} : (m : cardinal) = n ↔ m = n := by simp [le_antisymm_iff] @[simp, elim_cast] theorem nat_succ (n : ℕ) : succ n = n.succ := le_antisymm (succ_le.2 $ nat_cast_lt.2 $ nat.lt_succ_self _) (add_one_le_succ _) @[simp] theorem succ_zero : succ 0 = 1 := by simpa using nat_succ 0 theorem cantor' (a) {b : cardinal} (hb : 1 < b) : a < b ^ a := by rw [← succ_le, (by simpa using nat_succ 1 : succ 1 = 2)] at hb; exact lt_of_lt_of_le (cantor _) (power_le_power_right hb) theorem one_le_iff_pos {c : cardinal} : 1 ≤ c ↔ 0 < c := by rw [← succ_zero, succ_le] theorem one_le_iff_ne_zero {c : cardinal} : 1 ≤ c ↔ c ≠ 0 := by rw [one_le_iff_pos, pos_iff_ne_zero] theorem nat_lt_omega (n : ℕ) : (n : cardinal.{u}) < omega := succ_le.1 $ by rw [nat_succ, ← lift_mk_fin, omega, lift_mk_le.{0 0 u}]; exact ⟨⟨fin.val, λ a b, fin.eq_of_veq⟩⟩ theorem one_lt_omega : 1 < omega := by simpa using nat_lt_omega 1 theorem lt_omega {c : cardinal.{u}} : c < omega ↔ ∃ n : ℕ, c = n := ⟨λ h, begin rcases lt_lift_iff.1 h with ⟨c, rfl, h'⟩, rcases le_mk_iff_exists_set.1 h'.1 with ⟨S, rfl⟩, suffices : finite S, { cases this, resetI, existsi fintype.card S, rw [← lift_nat_cast.{0 u}, lift_inj, fintype_card S] }, by_contra nf, have P : ∀ (n : ℕ) (IH : ∀ i<n, S), ∃ a : S, ¬ ∃ y h, IH y h = a := λ n IH, let g : {i | i < n} → S := λ ⟨i, h⟩, IH i h in classical.not_forall.1 (λ h, nf ⟨fintype.of_surjective g (λ a, subtype.exists.2 (h a))⟩), let F : ℕ → S := nat.lt_wf.fix (λ n IH, classical.some (P n IH)), refine not_le_of_lt h' ⟨⟨F, _⟩⟩, suffices : ∀ (n : ℕ) (m < n), F m ≠ F n, { refine λ m n, not_imp_not.1 (λ ne, _), rcases lt_trichotomy m n with h|h|h, { exact this n m h }, { contradiction }, { exact (this m n h).symm } }, intros n m h, have := classical.some_spec (P n (λ y _, F y)), rw [← show F n = classical.some (P n (λ y _, F y)), from nat.lt_wf.fix_eq (λ n IH, classical.some (P n IH)) n] at this, exact λ e, this ⟨m, h, e⟩, end, λ ⟨n, e⟩, e.symm ▸ nat_lt_omega _⟩ theorem omega_le {c : cardinal.{u}} : omega ≤ c ↔ ∀ n : ℕ, (n:cardinal) ≤ c := ⟨λ h n, le_trans (le_of_lt (nat_lt_omega _)) h, λ h, le_of_not_lt $ λ hn, begin rcases lt_omega.1 hn with ⟨n, rfl⟩, exact not_le_of_lt (nat.lt_succ_self _) (nat_cast_le.1 (h (n+1))) end⟩ theorem lt_omega_iff_fintype {α : Type u} : mk α < omega ↔ nonempty (fintype α) := lt_omega.trans ⟨λ ⟨n, e⟩, begin rw [← lift_mk_fin n] at e, cases quotient.exact e with f, exact ⟨fintype.of_equiv _ f.symm⟩ end, λ ⟨_⟩, by exactI ⟨_, fintype_card _⟩⟩ theorem lt_omega_iff_finite {α} {S : set α} : mk S < omega ↔ finite S := lt_omega_iff_fintype instance can_lift_cardinal_nat : can_lift cardinal ℕ := ⟨ coe, λ x, x < omega, λ x hx, let ⟨n, hn⟩ := lt_omega.mp hx in ⟨n, hn.symm⟩⟩ theorem add_lt_omega {a b : cardinal} (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 lemma add_lt_omega_iff {a b : cardinal} : a + b < omega ↔ a < omega ∧ b < omega := ⟨λ h, ⟨lt_of_le_of_lt (le_add_right _ _) h, lt_of_le_of_lt (le_add_left _ _) h⟩, λ⟨h1, h2⟩, add_lt_omega h1 h2⟩ theorem mul_lt_omega {a b : cardinal} (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 lemma mul_lt_omega_iff {a b : cardinal} : a * b < omega ↔ a = 0 ∨ b = 0 ∨ a < omega ∧ b < omega := begin split, { intro h, by_cases ha : a = 0, { left, exact ha }, right, by_cases hb : b = 0, { left, exact hb }, right, rw [← ne, ← one_le_iff_ne_zero] at ha hb, split, { rw [← mul_one a], refine lt_of_le_of_lt (mul_le_mul (le_refl a) hb) h }, { rw [← _root_.one_mul b], refine lt_of_le_of_lt (mul_le_mul ha (le_refl b)) h }}, rintro (rfl|rfl|⟨ha,hb⟩); simp only [*, mul_lt_omega, omega_pos, _root_.zero_mul, mul_zero] end lemma mul_lt_omega_iff_of_ne_zero {a b : cardinal} (ha : a ≠ 0) (hb : b ≠ 0) : a * b < omega ↔ a < omega ∧ b < omega := by simp [mul_lt_omega_iff, ha, hb] theorem power_lt_omega {a b : cardinal} (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_pow]; apply nat_lt_omega end lemma eq_one_iff_subsingleton_and_nonempty {α : Type*} : mk α = 1 ↔ (subsingleton α ∧ nonempty α) := calc mk α = 1 ↔ mk α ≤ 1 ∧ ¬mk α < 1 : eq_iff_le_not_lt ... ↔ subsingleton α ∧ nonempty α : begin apply and_congr le_one_iff_subsingleton, push_neg, rw [one_le_iff_ne_zero, ne_zero_iff_nonempty] end theorem infinite_iff {α : Type u} : infinite α ↔ omega ≤ mk α := by rw [←not_lt, lt_omega_iff_fintype, not_nonempty_fintype] lemma countable_iff (s : set α) : countable s ↔ mk s ≤ omega := begin rw [countable_iff_exists_injective], split, rintro ⟨f, hf⟩, exact ⟨embedding.trans ⟨f, hf⟩ equiv.ulift.symm.to_embedding⟩, rintro ⟨f'⟩, cases embedding.trans f' equiv.ulift.to_embedding with f hf, exact ⟨f, hf⟩ end lemma denumerable_iff {α : Type u} : nonempty (denumerable α) ↔ mk α = omega := ⟨λ⟨h⟩, quotient.sound $ by exactI ⟨ (denumerable.eqv α).trans equiv.ulift.symm ⟩, λ h, by { cases quotient.exact h with f, exact ⟨denumerable.mk' $ f.trans equiv.ulift⟩ }⟩ lemma mk_int : mk ℤ = omega := denumerable_iff.mp ⟨by apply_instance⟩ lemma mk_pnat : mk ℕ+ = omega := denumerable_iff.mp ⟨by apply_instance⟩ lemma two_le_iff : (2 : cardinal) ≤ mk α ↔ ∃x y : α, x ≠ y := begin split, { rintro ⟨f⟩, refine ⟨f $ sum.inl ⟨⟩, f $ sum.inr ⟨⟩, _⟩, intro h, cases f.2 h }, { rintro ⟨x, y, h⟩, by_contra h', rw [not_le, ←nat.cast_two, ←nat_succ, lt_succ, nat.cast_one, le_one_iff_subsingleton] at h', apply h, exactI subsingleton.elim _ _ } end lemma two_le_iff' (x : α) : (2 : cardinal) ≤ mk α ↔ ∃y : α, x ≠ y := begin rw [two_le_iff], split, { rintro ⟨y, z, h⟩, refine classical.by_cases (λ(h' : x = y), _) (λ h', ⟨y, h'⟩), rw [←h'] at h, exact ⟨z, h⟩ }, { rintro ⟨y, h⟩, exact ⟨x, y, h⟩ } end /-- König's theorem -/ theorem sum_lt_prod {ι} (f g : ι → cardinal) (H : ∀ i, f i < g i) : sum f < prod g := lt_of_not_ge $ λ ⟨F⟩, begin have : inhabited (Π (i : ι), (g i).out), { refine ⟨λ i, classical.choice $ ne_zero_iff_nonempty.1 _⟩, rw mk_out, exact ne_of_gt (lt_of_le_of_lt (zero_le _) (H i)) }, resetI, let G := inv_fun F, have sG : surjective G := inv_fun_surjective F.2, choose C hc using show ∀ i, ∃ b, ∀ a, G ⟨i, a⟩ i ≠ b, { assume i, simp only [- not_exists, not_exists.symm, classical.not_forall.symm], refine λ h, not_le_of_lt (H i) _, rw [← mk_out (f i), ← mk_out (g i)], exact ⟨embedding.of_surjective h⟩ }, exact (let ⟨⟨i, a⟩, h⟩ := sG C in hc i a (congr_fun h _)) end @[simp] theorem mk_empty : mk empty = 0 := fintype_card empty @[simp] theorem mk_pempty : mk pempty = 0 := fintype_card pempty @[simp] theorem mk_plift_of_false {p : Prop} (h : ¬ p) : mk (plift p) = 0 := quotient.sound ⟨equiv.plift.trans $ equiv.equiv_pempty h⟩ theorem mk_unit : mk unit = 1 := (fintype_card unit).trans nat.cast_one @[simp] theorem mk_punit : mk punit = 1 := (fintype_card punit).trans nat.cast_one @[simp] theorem mk_singleton {α : Type u} (x : α) : mk ({x} : set α) = 1 := quotient.sound ⟨equiv.set.singleton x⟩ @[simp] theorem mk_plift_of_true {p : Prop} (h : p) : mk (plift p) = 1 := quotient.sound ⟨equiv.plift.trans $ equiv.prop_equiv_punit h⟩ @[simp] theorem mk_bool : mk bool = 2 := quotient.sound ⟨equiv.bool_equiv_punit_sum_punit⟩ @[simp] theorem mk_Prop : mk Prop = 2 := (quotient.sound ⟨equiv.Prop_equiv_bool⟩ : mk Prop = mk bool).trans mk_bool @[simp] theorem mk_option {α : Type u} : mk (option α) = mk α + 1 := quotient.sound ⟨equiv.option_equiv_sum_punit α⟩ theorem mk_list_eq_sum_pow (α : Type u) : mk (list α) = sum (λ n : ℕ, (mk α)^(n:cardinal.{u})) := calc mk (list α) = mk (Σ n, vector α n) : quotient.sound ⟨(equiv.sigma_preimage_equiv list.length).symm⟩ ... = mk (Σ n, fin n → α) : quotient.sound ⟨equiv.sigma_congr_right $ λ n, ⟨vector.nth, vector.of_fn, vector.of_fn_nth, λ f, funext $ vector.nth_of_fn f⟩⟩ ... = mk (Σ n : ℕ, ulift.{u} (fin n) → α) : quotient.sound ⟨equiv.sigma_congr_right $ λ n, equiv.arrow_congr equiv.ulift.symm (equiv.refl α)⟩ ... = sum (λ n : ℕ, (mk α)^(n:cardinal.{u})) : by simp only [(lift_mk_fin _).symm, lift_mk, power_def, sum_mk] theorem mk_quot_le {α : Type u} {r : α → α → Prop} : mk (quot r) ≤ mk α := mk_le_of_surjective quot.exists_rep theorem mk_quotient_le {α : Type u} {s : setoid α} : mk (quotient s) ≤ mk α := mk_quot_le theorem mk_subtype_le {α : Type u} (p : α → Prop) : mk (subtype p) ≤ mk α := ⟨embedding.subtype p⟩ theorem mk_subtype_le_of_subset {α : Type u} {p q : α → Prop} (h : ∀ ⦃x⦄, p x → q x) : mk (subtype p) ≤ mk (subtype q) := ⟨embedding.subtype_map (embedding.refl α) h⟩ @[simp] theorem mk_emptyc (α : Type u) : mk (∅ : set α) = 0 := quotient.sound ⟨equiv.set.pempty α⟩ theorem mk_univ {α : Type u} : mk (@univ α) = mk α := quotient.sound ⟨equiv.set.univ α⟩ theorem mk_image_le {α β : Type u} {f : α → β} {s : set α} : mk (f '' s) ≤ mk s := mk_le_of_surjective surjective_onto_image theorem mk_image_le_lift {α : Type u} {β : Type v} {f : α → β} {s : set α} : lift.{v u} (mk (f '' s)) ≤ lift.{u v} (mk s) := lift_mk_le.{v u 0}.mpr ⟨embedding.of_surjective surjective_onto_image⟩ theorem mk_range_le {α β : Type u} {f : α → β} : mk (range f) ≤ mk α := mk_le_of_surjective surjective_onto_range lemma mk_range_eq (f : α → β) (h : injective f) : mk (range f) = mk α := quotient.sound ⟨(equiv.set.range f h).symm⟩ lemma mk_range_eq_of_inj {α : Type u} {β : Type v} {f : α → β} (hf : injective f) : lift.{v u} (mk (range f)) = lift.{u v} (mk α) := begin have := (@lift_mk_eq.{v u max u v} (range f) α).2 ⟨(equiv.set.range f hf).symm⟩, simp only [lift_umax.{u v}, lift_umax.{v u}] at this, exact this end lemma mk_range_eq_lift {α : Type u} {β : Type v} {f : α → β} (hf : injective f) : lift.{v (max u w)} (# (range f)) = lift.{u (max v w)} (# α) := lift_mk_eq.mpr ⟨(equiv.set.range f hf).symm⟩ theorem mk_image_eq {α β : Type u} {f : α → β} {s : set α} (hf : injective f) : mk (f '' s) = mk s := quotient.sound ⟨(equiv.set.image f s hf).symm⟩ theorem mk_Union_le_sum_mk {α ι : Type u} {f : ι → set α} : mk (⋃ i, f i) ≤ sum (λ i, mk (f i)) := calc mk (⋃ i, f i) ≤ mk (Σ i, f i) : mk_le_of_surjective (set.surjective_sigma_to_Union f) ... = sum (λ i, mk (f i)) : (sum_mk _).symm theorem mk_Union_eq_sum_mk {α ι : Type u} {f : ι → set α} (h : ∀i j, i ≠ j → disjoint (f i) (f j)) : mk (⋃ i, f i) = sum (λ i, mk (f i)) := calc mk (⋃ i, f i) = mk (Σi, f i) : quot.sound ⟨set.Union_eq_sigma_of_disjoint h⟩ ... = sum (λi, mk (f i)) : (sum_mk _).symm lemma mk_Union_le {α ι : Type u} (f : ι → set α) : mk (⋃ i, f i) ≤ mk ι * cardinal.sup.{u u} (λ i, mk (f i)) := le_trans mk_Union_le_sum_mk (sum_le_sup _) lemma mk_sUnion_le {α : Type u} (A : set (set α)) : mk (⋃₀ A) ≤ mk A * cardinal.sup.{u u} (λ s : A, mk s) := by { rw [sUnion_eq_Union], apply mk_Union_le } lemma mk_bUnion_le {ι α : Type u} (A : ι → set α) (s : set ι) : mk (⋃(x ∈ s), A x) ≤ mk s * cardinal.sup.{u u} (λ x : s, mk (A x.1)) := by { rw [bUnion_eq_Union], apply mk_Union_le } @[simp] lemma finset_card {α : Type u} {s : finset α} : ↑(finset.card s) = mk (↑s : set α) := by rw [fintype_card, nat_cast_inj, fintype.card_coe] lemma finset_card_lt_omega (s : finset α) : mk (↑s : set α) < omega := by { rw [lt_omega_iff_fintype], exact ⟨finset.subtype.fintype s⟩ } theorem mk_union_add_mk_inter {α : Type u} {S T : set α} : mk (S ∪ T : set α) + mk (S ∩ T : set α) = mk S + mk T := quot.sound ⟨equiv.set.union_sum_inter S T⟩ theorem mk_union_of_disjoint {α : Type u} {S T : set α} (H : disjoint S T) : mk (S ∪ T : set α) = mk S + mk T := quot.sound ⟨equiv.set.union (disjoint_iff.1 H)⟩ lemma mk_sum_compl {α} (s : set α) : #s + #(-s : set α) = #α := quotient.sound ⟨equiv.set.sum_compl s⟩ lemma mk_le_mk_of_subset {α} {s t : set α} (h : s ⊆ t) : mk s ≤ mk t := ⟨ set.embedding_of_subset h ⟩ lemma mk_subtype_mono {p q : α → Prop} (h : ∀x, p x → q x) : mk {x // p x} ≤ mk {x // q x} := ⟨embedding_of_subset h⟩ lemma mk_set_le (s : set α) : mk s ≤ mk α := mk_subtype_le s lemma mk_image_eq_lift {α : Type u} {β : Type v} (f : α → β) (s : set α) (h : injective f) : lift.{v u} (mk (f '' s)) = lift.{u v} (mk s) := lift_mk_eq.{v u 0}.mpr ⟨(equiv.set.image f s h).symm⟩ lemma mk_image_eq_of_inj_on_lift {α : Type u} {β : Type v} (f : α → β) (s : set α) (h : inj_on f s) : lift.{v u} (mk (f '' s)) = lift.{u v} (mk s) := lift_mk_eq.{v u 0}.mpr ⟨(equiv.set.image_of_inj_on f s h).symm⟩ lemma mk_image_eq_of_inj_on {α β : Type u} (f : α → β) (s : set α) (h : inj_on f s) : mk (f '' s) = mk s := quotient.sound ⟨(equiv.set.image_of_inj_on f s h).symm⟩ lemma mk_subtype_of_equiv {α β : Type u} (p : α → Prop) (e : α ≃ β) : mk {a : α // p a} = mk {b : β // p (e.symm b)} := quotient.sound ⟨equiv.subtype_equiv_of_subtype' e⟩ lemma mk_sep (s : set α) (t : α → Prop) : mk ({ x ∈ s | t x } : set α) = mk { x : s | t x.1 } := quotient.sound ⟨equiv.set.sep s t⟩ lemma mk_preimage_of_injective_lift {α : Type u} {β : Type v} (f : α → β) (s : set β) (h : injective f) : lift.{u v} (mk (f ⁻¹' s)) ≤ lift.{v u} (mk s) := begin rw lift_mk_le.{u v 0}, use subtype.coind (λ x, f x.1) (λ x, x.2), apply subtype.coind_injective, exact injective_comp h subtype.val_injective end lemma mk_preimage_of_subset_range_lift {α : Type u} {β : Type v} (f : α → β) (s : set β) (h : s ⊆ range f) : lift.{v u} (mk s) ≤ lift.{u v} (mk (f ⁻¹' s)) := begin rw lift_mk_le.{v u 0}, refine ⟨⟨_, _⟩⟩, { rintro ⟨y, hy⟩, rcases classical.subtype_of_exists (h hy) with ⟨x, rfl⟩, exact ⟨x, hy⟩ }, rintro ⟨y, hy⟩ ⟨y', hy'⟩, dsimp, rcases classical.subtype_of_exists (h hy) with ⟨x, rfl⟩, rcases classical.subtype_of_exists (h hy') with ⟨x', rfl⟩, simp, intro hxx', rw hxx' end lemma mk_preimage_of_injective_of_subset_range_lift {β : Type v} (f : α → β) (s : set β) (h : injective f) (h2 : s ⊆ range f) : lift.{u v} (mk (f ⁻¹' s)) = lift.{v u} (mk s) := le_antisymm (mk_preimage_of_injective_lift f s h) (mk_preimage_of_subset_range_lift f s h2) lemma mk_preimage_of_injective (f : α → β) (s : set β) (h : injective f) : mk (f ⁻¹' s) ≤ mk s := by { convert mk_preimage_of_injective_lift.{u u} f s h using 1; rw [lift_id] } lemma mk_preimage_of_subset_range (f : α → β) (s : set β) (h : s ⊆ range f) : mk s ≤ mk (f ⁻¹' s) := by { convert mk_preimage_of_subset_range_lift.{u u} f s h using 1; rw [lift_id] } lemma mk_preimage_of_injective_of_subset_range (f : α → β) (s : set β) (h : injective f) (h2 : s ⊆ range f) : mk (f ⁻¹' s) = mk s := by { convert mk_preimage_of_injective_of_subset_range_lift.{u u} f s h h2 using 1; rw [lift_id] } lemma mk_subset_ge_of_subset_image_lift {α : Type u} {β : Type v} (f : α → β) {s : set α} {t : set β} (h : t ⊆ f '' s) : lift.{v u} (mk t) ≤ lift.{u v} (mk ({ x ∈ s | f x ∈ t } : set α)) := by { rw [image_eq_range] at h, convert mk_preimage_of_subset_range_lift _ _ h using 1, rw [mk_sep], refl } lemma mk_subset_ge_of_subset_image (f : α → β) {s : set α} {t : set β} (h : t ⊆ f '' s) : mk t ≤ mk ({ x ∈ s | f x ∈ t } : set α) := by { rw [image_eq_range] at h, convert mk_preimage_of_subset_range _ _ h using 1, rw [mk_sep], refl } theorem le_mk_iff_exists_subset {c : cardinal} {α : Type u} {s : set α} : c ≤ mk s ↔ ∃ p : set α, p ⊆ s ∧ mk p = c := begin rw [le_mk_iff_exists_set, ←subtype.exists_set_subtype], apply exists_congr, intro t, rw [mk_image_eq], apply subtype.val_injective end /-- The function α^{<β}, defined to be sup_{γ < β} α^γ. We index over {s : set β.out // mk s < β } instead of {γ // γ < β}, because the latter lives in a higher universe -/ noncomputable def powerlt (α β : cardinal.{u}) : cardinal.{u} := sup.{u u} (λ(s : {s : set β.out // mk s < β}), α ^ mk.{u} s) infix ` ^< `:80 := powerlt theorem powerlt_aux {c c' : cardinal} (h : c < c') : ∃(s : {s : set c'.out // mk s < c'}), mk s = c := begin cases out_embedding.mp (le_of_lt h) with f, have : mk ↥(range ⇑f) = c, { rwa [mk_range_eq, mk, quotient.out_eq c], exact f.2 }, exact ⟨⟨range f, by convert h⟩, this⟩ end lemma le_powerlt {c₁ c₂ c₃ : cardinal} (h : c₂ < c₃) : c₁ ^ c₂ ≤ c₁ ^< c₃ := by { rcases powerlt_aux h with ⟨s, rfl⟩, apply le_sup _ s } lemma powerlt_le {c₁ c₂ c₃ : cardinal} : c₁ ^< c₂ ≤ c₃ ↔ ∀(c₄ < c₂), c₁ ^ c₄ ≤ c₃ := begin rw [powerlt, sup_le], split, { intros h c₄ hc₄, rcases powerlt_aux hc₄ with ⟨s, rfl⟩, exact h s }, intros h s, exact h _ s.2 end lemma powerlt_le_powerlt_left {a b c : cardinal} (h : b ≤ c) : a ^< b ≤ a ^< c := by { rw [powerlt, sup_le], rintro ⟨s, hs⟩, apply le_powerlt, exact lt_of_lt_of_le hs h } lemma powerlt_succ {c₁ c₂ : cardinal} (h : c₁ ≠ 0) : c₁ ^< c₂.succ = c₁ ^ c₂ := begin apply le_antisymm, { rw powerlt_le, intros c₃ h2, apply power_le_power_left h, rwa [←lt_succ] }, { apply le_powerlt, apply lt_succ_self } end lemma powerlt_max {c₁ c₂ c₃ : cardinal} : c₁ ^< max c₂ c₃ = max (c₁ ^< c₂) (c₁ ^< c₃) := by { cases le_total c₂ c₃; simp only [max_eq_left, max_eq_right, h, powerlt_le_powerlt_left] } lemma zero_powerlt {a : cardinal} (h : a ≠ 0) : 0 ^< a = 1 := begin apply le_antisymm, { rw [powerlt_le], intros c hc, apply zero_power_le }, convert le_powerlt (pos_iff_ne_zero.2 h), rw [power_zero] end lemma powerlt_zero {a : cardinal} : a ^< 0 = 0 := by { apply sup_eq_zero, rintro ⟨x, hx⟩, rw [←not_le] at hx, apply hx, apply zero_le } end cardinal
eca900c78e2c83ca4d962db454420d7b0c97bc72
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/measure_theory/measure/hausdorff.lean
c6278be73ab7048ca36a85a886e6101e94f546dd
[ "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
43,618
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import topology.metric_space.metric_separated import measure_theory.constructions.borel_space import measure_theory.measure.lebesgue import analysis.special_functions.pow import topology.metric_space.holder import data.equiv.list /-! # Hausdorff measure and metric (outer) measures In this file we define the `d`-dimensional Hausdorff measure on an (extended) metric space `X` and the Hausdorff dimension of a set in an (extended) metric space. Let `μ d δ` be the maximal outer measure such that `μ d δ s ≤ (emetric.diam s) ^ d` for every set of diameter less than `δ`. Then the Hausdorff measure `μH[d] s` of `s` is defined as `⨆ δ > 0, μ d δ s`. By Caratheodory theorem `measure_theory.outer_measure.is_metric.borel_le_caratheodory`, this is a Borel measure on `X`. The value of `μH[d]`, `d > 0`, on a set `s` (measurable or not) is given by ``` μH[d] s = ⨆ (r : ℝ≥0∞) (hr : 0 < r), ⨅ (t : ℕ → set X) (hts : s ⊆ ⋃ n, t n) (ht : ∀ n, emetric.diam (t n) ≤ r), ∑' n, emetric.diam (t n) ^ d ``` For every set `s` for any `d < d'` we have either `μH[d] s = ∞` or `μH[d'] s = 0`, see `measure_theory.measure.hausdorff_measure_zero_or_top`. In `topology.metric_space.hausdorff_dimension` we use this fact to define the Hausdorff dimension `dimH` of a set in an (extended) metric space. We also define two generalizations of the Hausdorff measure. In one generalization (see `measure_theory.measure.mk_metric`) we take any function `m (diam s)` instead of `(diam s) ^ d`. In an even more general definition (see `measure_theory.measure.mk_metric'`) we use any function of `m : set X → ℝ≥0∞`. Some authors start with a partial function `m` defined only on some sets `s : set X` (e.g., only on balls or only on measurable sets). This is equivalent to our definition applied to `measure_theory.extend m`. We also define a predicate `measure_theory.outer_measure.is_metric` which says that an outer measure is additive on metric separated pairs of sets: `μ (s ∪ t) = μ s + μ t` provided that `⨅ (x ∈ s) (y ∈ t), edist x y ≠ 0`. This is the property required for the Caratheodory theorem `measure_theory.outer_measure.is_metric.borel_le_caratheodory`, so we prove this theorem for any metric outer measure, then prove that outer measures constructed using `mk_metric'` are metric outer measures. ## Main definitions * `measure_theory.outer_measure.is_metric`: an outer measure `μ` is called *metric* if `μ (s ∪ t) = μ s + μ t` for any two metric separated sets `s` and `t`. A metric outer measure in a Borel extended metric space is guaranteed to satisfy the Caratheodory condition, see `measure_theory.outer_measure.is_metric.borel_le_caratheodory`. * `measure_theory.outer_measure.mk_metric'` and its particular case `measure_theory.outer_measure.mk_metric`: a construction of an outer measure that is guaranteed to be metric. Both constructions are generalizations of the Hausdorff measure. The same measures interpreted as Borel measures are called `measure_theory.measure.mk_metric'` and `measure_theory.measure.mk_metric`. * `measure_theory.measure.hausdorff_measure` a.k.a. `μH[d]`: the `d`-dimensional Hausdorff measure. There are many definitions of the Hausdorff measure that differ from each other by a multiplicative constant. We put `μH[d] s = ⨆ r > 0, ⨅ (t : ℕ → set X) (hts : s ⊆ ⋃ n, t n) (ht : ∀ n, emetric.diam (t n) ≤ r), ∑' n, ⨆ (ht : ¬set.subsingleton (t n)), (emetric.diam (t n)) ^ d`, see `measure_theory.measure.hausdorff_measure_apply'`. In the most interesting case `0 < d` one can omit the `⨆ (ht : ¬set.subsingleton (t n))` part. ## Main statements ### Basic properties * `measure_theory.outer_measure.is_metric.borel_le_caratheodory`: if `μ` is a metric outer measure on an extended metric space `X` (that is, it is additive on pairs of metric separated sets), then every Borel set is Caratheodory measurable (hence, `μ` defines an actual `measure_theory.measure`). See also `measure_theory.measure.mk_metric`. * `measure_theory.measure.hausdorff_measure_mono`: `μH[d] s` is an antitone function of `d`. * `measure_theory.measure.hausdorff_measure_zero_or_top`: if `d₁ < d₂`, then for any `s`, either `μH[d₂] s = 0` or `μH[d₁] s = ∞`. Together with the previous lemma, this means that `μH[d] s` is equal to infinity on some ray `(-∞, D)` and is equal to zero on `(D, +∞)`, where `D` is a possibly infinite number called the *Hausdorff dimension* of `s`; `μH[D] s` can be zero, infinity, or anything in between. * `measure_theory.measure.no_atoms_hausdorff`: Hausdorff measure has no atoms. ### Hausdorff measure in `ℝⁿ` * `measure_theory.hausdorff_measure_pi_real`: for a nonempty `ι`, `μH[card ι]` on `ι → ℝ` equals Lebesgue measure. ## Notations We use the following notation localized in `measure_theory`. - `μH[d]` : `measure_theory.measure.hausdorff_measure d` ## Implementation notes There are a few similar constructions called the `d`-dimensional Hausdorff measure. E.g., some sources only allow coverings by balls and use `r ^ d` instead of `(diam s) ^ d`. While these construction lead to different Hausdorff measures, they lead to the same notion of the Hausdorff dimension. Some sources define the `0`-dimensional Hausdorff measure to be the counting measure. We define it to be zero on subsingletons because this way we can have a `measure.has_no_atoms (measure.hausdorff_measure d)` instance. ## TODO * prove that `1`-dimensional Hausdorff measure on `ℝ` equals `volume`; * prove a similar statement for `ℝ × ℝ`. ## References * [Herbert Federer, Geometric Measure Theory, Chapter 2.10][Federer1996] ## Tags Hausdorff measure, measure, metric measure -/ open_locale nnreal ennreal topological_space big_operators open emetric set function filter encodable finite_dimensional topological_space noncomputable theory variables {ι X Y : Type*} [emetric_space X] [emetric_space Y] namespace measure_theory namespace outer_measure /-! ### Metric outer measures In this section we define metric outer measures and prove Caratheodory theorem: a metric outer measure has the Caratheodory property. -/ /-- We say that an outer measure `μ` in an (e)metric space is *metric* if `μ (s ∪ t) = μ s + μ t` for any two metric separated sets `s`, `t`. -/ def is_metric (μ : outer_measure X) : Prop := ∀ (s t : set X), is_metric_separated s t → μ (s ∪ t) = μ s + μ t namespace is_metric variables {μ : outer_measure X} /-- A metric outer measure is additive on a finite set of pairwise metric separated sets. -/ lemma finset_Union_of_pairwise_separated (hm : is_metric μ) {I : finset ι} {s : ι → set X} (hI : ∀ (i ∈ I) (j ∈ I), i ≠ j → is_metric_separated (s i) (s j)) : μ (⋃ i ∈ I, s i) = ∑ i in I, μ (s i) := begin classical, induction I using finset.induction_on with i I hiI ihI hI, { simp }, simp only [finset.mem_insert] at hI, rw [finset.set_bUnion_insert, hm, ihI, finset.sum_insert hiI], exacts [λ i hi j hj hij, (hI i (or.inr hi) j (or.inr hj) hij), is_metric_separated.finset_Union_right (λ j hj, hI i (or.inl rfl) j (or.inr hj) (ne_of_mem_of_not_mem hj hiI).symm)] end /-- Caratheodory theorem. If `m` is a metric outer measure, then every Borel measurable set `t` is Caratheodory measurable: for any (not necessarily measurable) set `s` we have `μ (s ∩ t) + μ (s \ t) = μ s`. -/ lemma borel_le_caratheodory (hm : is_metric μ) : borel X ≤ μ.caratheodory := begin rw [borel_eq_generate_from_is_closed], refine measurable_space.generate_from_le (λ t ht, μ.is_caratheodory_iff_le.2 $ λ s, _), set S : ℕ → set X := λ n, {x ∈ s | (↑n)⁻¹ ≤ inf_edist x t}, have n0 : ∀ {n : ℕ}, (n⁻¹ : ℝ≥0∞) ≠ 0, from λ n, ennreal.inv_ne_zero.2 ennreal.coe_nat_ne_top, have Ssep : ∀ n, is_metric_separated (S n) t, from λ n, ⟨n⁻¹, n0, λ x hx y hy, hx.2.trans $ inf_edist_le_edist_of_mem hy⟩, have Ssep' : ∀ n, is_metric_separated (S n) (s ∩ t), from λ n, (Ssep n).mono subset.rfl (inter_subset_right _ _), have S_sub : ∀ n, S n ⊆ s \ t, from λ n, subset_inter (inter_subset_left _ _) (Ssep n).subset_compl_right, have hSs : ∀ n, μ (s ∩ t) + μ (S n) ≤ μ s, from λ n, calc μ (s ∩ t) + μ (S n) = μ (s ∩ t ∪ S n) : eq.symm $ hm _ _ $ (Ssep' n).symm ... ≤ μ (s ∩ t ∪ s \ t) : by { mono*, exact le_rfl } ... = μ s : by rw [inter_union_diff], have Union_S : (⋃ n, S n) = s \ t, { refine subset.antisymm (Union_subset S_sub) _, rintro x ⟨hxs, hxt⟩, rw mem_iff_inf_edist_zero_of_closed ht at hxt, rcases ennreal.exists_inv_nat_lt hxt with ⟨n, hn⟩, exact mem_Union.2 ⟨n, hxs, hn.le⟩ }, /- Now we have `∀ n, μ (s ∩ t) + μ (S n) ≤ μ s` and we need to prove `μ (s ∩ t) + μ (⋃ n, S n) ≤ μ s`. We can't pass to the limit because `μ` is only an outer measure. -/ by_cases htop : μ (s \ t) = ∞, { rw [htop, ennreal.add_top, ← htop], exact μ.mono (diff_subset _ _) }, suffices : μ (⋃ n, S n) ≤ ⨆ n, μ (S n), calc μ (s ∩ t) + μ (s \ t) = μ (s ∩ t) + μ (⋃ n, S n) : by rw Union_S ... ≤ μ (s ∩ t) + ⨆ n, μ (S n) : add_le_add le_rfl this ... = ⨆ n, μ (s ∩ t) + μ (S n) : ennreal.add_supr ... ≤ μ s : supr_le hSs, /- It suffices to show that `∑' k, μ (S (k + 1) \ S k) ≠ ∞`. Indeed, if we have this, then for all `N` we have `μ (⋃ n, S n) ≤ μ (S N) + ∑' k, m (S (N + k + 1) \ S (N + k))` and the second term tends to zero, see `outer_measure.Union_nat_of_monotone_of_tsum_ne_top` for details. -/ have : ∀ n, S n ⊆ S (n + 1), from λ n x hx, ⟨hx.1, le_trans (ennreal.inv_le_inv.2 $ ennreal.coe_nat_le_coe_nat.2 n.le_succ) hx.2⟩, refine (μ.Union_nat_of_monotone_of_tsum_ne_top this _).le, clear this, /- While the sets `S (k + 1) \ S k` are not pairwise metric separated, the sets in each subsequence `S (2 * k + 1) \ S (2 * k)` and `S (2 * k + 2) \ S (2 * k)` are metric separated, so `m` is additive on each of those sequences. -/ rw [← tsum_even_add_odd ennreal.summable ennreal.summable, ennreal.add_ne_top], suffices : ∀ a, (∑' (k : ℕ), μ (S (2 * k + 1 + a) \ S (2 * k + a))) ≠ ∞, from ⟨by simpa using this 0, by simpa using this 1⟩, refine λ r, ne_top_of_le_ne_top htop _, rw [← Union_S, ennreal.tsum_eq_supr_nat, supr_le_iff], intro n, rw [← hm.finset_Union_of_pairwise_separated], { exact μ.mono (Union_subset $ λ i, Union_subset $ λ hi x hx, mem_Union.2 ⟨_, hx.1⟩) }, suffices : ∀ i j, i < j → is_metric_separated (S (2 * i + 1 + r)) (s \ S (2 * j + r)), from λ i _ j _ hij, hij.lt_or_lt.elim (λ h, (this i j h).mono (inter_subset_left _ _) (λ x hx, ⟨hx.1.1, hx.2⟩)) (λ h, (this j i h).symm.mono (λ x hx, ⟨hx.1.1, hx.2⟩) (inter_subset_left _ _)), intros i j hj, have A : ((↑(2 * j + r))⁻¹ : ℝ≥0∞) < (↑(2 * i + 1 + r))⁻¹, by { rw [ennreal.inv_lt_inv, ennreal.coe_nat_lt_coe_nat], linarith }, refine ⟨(↑(2 * i + 1 + r))⁻¹ - (↑(2 * j + r))⁻¹, by simpa using A, λ x hx y hy, _⟩, have : inf_edist y t < (↑(2 * j + r))⁻¹, from not_le.1 (λ hle, hy.2 ⟨hy.1, hle⟩), rcases exists_edist_lt_of_inf_edist_lt this with ⟨z, hzt, hyz⟩, have hxz : (↑(2 * i + 1 + r))⁻¹ ≤ edist x z, from le_inf_edist.1 hx.2 _ hzt, apply ennreal.le_of_add_le_add_right hyz.ne_top, refine le_trans _ (edist_triangle _ _ _), refine (add_le_add le_rfl hyz.le).trans (eq.trans_le _ hxz), rw [tsub_add_cancel_of_le A.le] end lemma le_caratheodory [measurable_space X] [borel_space X] (hm : is_metric μ) : ‹measurable_space X› ≤ μ.caratheodory := by { rw @borel_space.measurable_eq X _ _, exact hm.borel_le_caratheodory } end is_metric /-! ### Constructors of metric outer measures In this section we provide constructors `measure_theory.outer_measure.mk_metric'` and `measure_theory.outer_measure.mk_metric` and prove that these outer measures are metric outer measures. We also prove basic lemmas about `map`/`comap` of these measures. -/ /-- Auxiliary definition for `outer_measure.mk_metric'`: given a function on sets `m : set X → ℝ≥0∞`, returns the maximal outer measure `μ` such that `μ s ≤ m s` for any set `s` of diameter at most `r`.-/ def mk_metric'.pre (m : set X → ℝ≥0∞) (r : ℝ≥0∞) : outer_measure X := bounded_by $ extend (λ s (hs : diam s ≤ r), m s) /-- Given a function `m : set X → ℝ≥0∞`, `mk_metric' m` is the supremum of `mk_metric'.pre m r` over `r > 0`. Equivalently, it is the limit of `mk_metric'.pre m r` as `r` tends to zero from the right. -/ def mk_metric' (m : set X → ℝ≥0∞) : outer_measure X := ⨆ r > 0, mk_metric'.pre m r /-- Given a function `m : ℝ≥0∞ → ℝ≥0∞` and `r > 0`, let `μ r` be the maximal outer measure such that `μ s = 0` on subsingletons and `μ s ≤ m (emetric.diam s)` whenever `emetric.diam s < r`. Then `mk_metric m = ⨆ r > 0, μ r`. We add `⨆ (hs : ¬s.subsingleton)` to ensure that in the case `m x = x ^ d` the definition gives the expected result for `d = 0`. -/ def mk_metric (m : ℝ≥0∞ → ℝ≥0∞) : outer_measure X := mk_metric' (λ s, ⨆ (hs : ¬s.subsingleton), m (diam s)) namespace mk_metric' variables {m : set X → ℝ≥0∞} {r : ℝ≥0∞} {μ : outer_measure X} {s : set X} lemma le_pre : μ ≤ pre m r ↔ ∀ s : set X, diam s ≤ r → μ s ≤ m s := by simp only [pre, le_bounded_by, extend, le_infi_iff] lemma pre_le (hs : diam s ≤ r) : pre m r s ≤ m s := (bounded_by_le _).trans $ infi_le _ hs lemma mono_pre (m : set X → ℝ≥0∞) {r r' : ℝ≥0∞} (h : r ≤ r') : pre m r' ≤ pre m r := le_pre.2 $ λ s hs, pre_le (hs.trans h) lemma mono_pre_nat (m : set X → ℝ≥0∞) : monotone (λ k : ℕ, pre m k⁻¹) := λ k l h, le_pre.2 $ λ s hs, pre_le (hs.trans $ by simpa) lemma tendsto_pre (m : set X → ℝ≥0∞) (s : set X) : tendsto (λ r, pre m r s) (𝓝[Ioi 0] 0) (𝓝 $ mk_metric' m s) := begin rw [← map_coe_Ioi_at_bot, tendsto_map'_iff], simp only [mk_metric', outer_measure.supr_apply, supr_subtype'], exact tendsto_at_bot_supr (λ r r' hr, mono_pre _ hr _) end lemma tendsto_pre_nat (m : set X → ℝ≥0∞) (s : set X) : tendsto (λ n : ℕ, pre m n⁻¹ s) at_top (𝓝 $ mk_metric' m s) := begin refine (tendsto_pre m s).comp (tendsto_inf.2 ⟨ennreal.tendsto_inv_nat_nhds_zero, _⟩), refine tendsto_principal.2 (eventually_of_forall $ λ n, _), simp end lemma eq_supr_nat (m : set X → ℝ≥0∞) : mk_metric' m = ⨆ n : ℕ, mk_metric'.pre m n⁻¹ := begin ext1 s, rw supr_apply, refine tendsto_nhds_unique (mk_metric'.tendsto_pre_nat m s) (tendsto_at_top_supr $ λ k l hkl, mk_metric'.mono_pre_nat m hkl s) end /-- `measure_theory.outer_measure.mk_metric'.pre m r` is a trimmed measure provided that `m (closure s) = m s` for any set `s`. -/ lemma trim_pre [measurable_space X] [opens_measurable_space X] (m : set X → ℝ≥0∞) (hcl : ∀ s, m (closure s) = m s) (r : ℝ≥0∞) : (pre m r).trim = pre m r := begin refine le_antisymm (le_pre.2 $ λ s hs, _) (le_trim _), rw trim_eq_infi, refine (infi_le_of_le (closure s) $ infi_le_of_le subset_closure $ infi_le_of_le measurable_set_closure ((pre_le _).trans_eq (hcl _))), rwa diam_closure end end mk_metric' /-- An outer measure constructed using `outer_measure.mk_metric'` is a metric outer measure. -/ lemma mk_metric'_is_metric (m : set X → ℝ≥0∞) : (mk_metric' m).is_metric := begin rintros s t ⟨r, r0, hr⟩, refine tendsto_nhds_unique_of_eventually_eq (mk_metric'.tendsto_pre _ _) ((mk_metric'.tendsto_pre _ _).add (mk_metric'.tendsto_pre _ _)) _, rw [← pos_iff_ne_zero] at r0, filter_upwards [Ioo_mem_nhds_within_Ioi ⟨le_rfl, r0⟩], rintro ε ⟨ε0, εr⟩, refine bounded_by_union_of_top_of_nonempty_inter _, rintro u ⟨x, hxs, hxu⟩ ⟨y, hyt, hyu⟩, have : ε < diam u, from εr.trans_le ((hr x hxs y hyt).trans $ edist_le_diam_of_mem hxu hyu), exact infi_eq_top.2 (λ h, (this.not_le h).elim) end /-- If `c ∉ {0, ∞}` and `m₁ d ≤ c * m₂ d` for `0 < d < ε` for some `ε > 0` (we use `≤ᶠ[𝓝[Ioi 0]]` to state this), then `mk_metric m₁ hm₁ ≤ c • mk_metric m₂ hm₂`. -/ lemma mk_metric_mono_smul {m₁ m₂ : ℝ≥0∞ → ℝ≥0∞} {c : ℝ≥0∞} (hc : c ≠ ∞) (h0 : c ≠ 0) (hle : m₁ ≤ᶠ[𝓝[Ioi 0] 0] c • m₂) : (mk_metric m₁ : outer_measure X) ≤ c • mk_metric m₂ := begin classical, rcases (mem_nhds_within_Ioi_iff_exists_Ioo_subset' ennreal.zero_lt_one).1 hle with ⟨r, hr0, hr⟩, refine λ s, le_of_tendsto_of_tendsto (mk_metric'.tendsto_pre _ s) (ennreal.tendsto.const_mul (mk_metric'.tendsto_pre _ s) (or.inr hc)) (mem_of_superset (Ioo_mem_nhds_within_Ioi ⟨le_rfl, hr0⟩) (λ r' hr', _)), simp only [mem_set_of_eq, mk_metric'.pre], rw [← smul_apply, smul_bounded_by hc], refine le_bounded_by.2 (λ t, (bounded_by_le _).trans _) _, simp only [smul_eq_mul, pi.smul_apply, extend, infi_eq_if], split_ifs with ht ht, { refine supr_le (λ ht₁, _), rw [supr_eq_if, if_pos ht₁], refine hr ⟨_, ht.trans_lt hr'.2⟩, exact pos_iff_ne_zero.2 (mt diam_eq_zero_iff.1 ht₁) }, { simp [h0] } end /-- If `m₁ d ≤ m₂ d` for `0 < d < ε` for some `ε > 0` (we use `≤ᶠ[𝓝[Ioi 0]]` to state this), then `mk_metric m₁ hm₁ ≤ mk_metric m₂ hm₂`-/ lemma mk_metric_mono {m₁ m₂ : ℝ≥0∞ → ℝ≥0∞} (hle : m₁ ≤ᶠ[𝓝[Ioi 0] 0] m₂) : (mk_metric m₁ : outer_measure X) ≤ mk_metric m₂ := by { convert mk_metric_mono_smul ennreal.one_ne_top ennreal.zero_lt_one.ne' _; simp * } lemma isometry_comap_mk_metric (m : ℝ≥0∞ → ℝ≥0∞) {f : X → Y} (hf : isometry f) (H : monotone (λ d : {d : ℝ≥0∞ | d ≠ 0}, m d) ∨ surjective f) : comap f (mk_metric m) = mk_metric m := begin simp only [mk_metric, mk_metric', mk_metric'.pre, induced_outer_measure, comap_supr], refine supr_congr id surjective_id (λ ε, supr_congr id surjective_id $ λ hε, _), rw comap_bounded_by _ (H.imp (λ h_mono, _) id), { congr' with s : 1, apply extend_congr, { simp [hf.ediam_image] }, { intros, simp [hf.injective.subsingleton_image_iff, hf.ediam_image] } }, { refine λ s t hst, infi_le_infi2 (λ ht, ⟨(diam_mono hst).trans ht, supr_le $ λ hs, _⟩), have ht : ¬(t : set Y).subsingleton, from λ ht, hs (ht.mono hst), refine (@h_mono ⟨_, mt diam_eq_zero_iff.1 hs⟩ ⟨_, mt diam_eq_zero_iff.1 ht⟩ (diam_mono hst)).trans _, exact le_supr (λ h : ¬(t : set Y).subsingleton, m (diam (t : set Y))) ht } end lemma isometry_map_mk_metric (m : ℝ≥0∞ → ℝ≥0∞) {f : X → Y} (hf : isometry f) (H : monotone (λ d : {d : ℝ≥0∞ | d ≠ 0}, m d) ∨ surjective f) : map f (mk_metric m) = restrict (range f) (mk_metric m) := by rw [← isometry_comap_mk_metric _ hf H, map_comap] lemma isometric_comap_mk_metric (m : ℝ≥0∞ → ℝ≥0∞) (f : X ≃ᵢ Y) : comap f (mk_metric m) = mk_metric m := isometry_comap_mk_metric _ f.isometry (or.inr f.surjective) lemma isometric_map_mk_metric (m : ℝ≥0∞ → ℝ≥0∞) (f : X ≃ᵢ Y) : map f (mk_metric m) = mk_metric m := by rw [← isometric_comap_mk_metric _ f, map_comap_of_surjective f.surjective] lemma trim_mk_metric [measurable_space X] [borel_space X] (m : ℝ≥0∞ → ℝ≥0∞) : (mk_metric m : outer_measure X).trim = mk_metric m := begin simp only [mk_metric, mk_metric'.eq_supr_nat, trim_supr], congr' 1 with n : 1, refine mk_metric'.trim_pre _ (λ s, _) _, simp end lemma le_mk_metric (m : ℝ≥0∞ → ℝ≥0∞) (μ : outer_measure X) (hμ : ∀ x, μ {x} = 0) (r : ℝ≥0∞) (h0 : 0 < r) (hr : ∀ s, diam s ≤ r → ¬s.subsingleton → μ s ≤ m (diam s)) : μ ≤ mk_metric m := le_bsupr_of_le r h0 $ mk_metric'.le_pre.2 $ λ s hs, begin by_cases h : s.subsingleton, exacts [h.induction_on (μ.empty'.trans_le (zero_le _)) (λ x, ((hμ x).trans_le (zero_le _))), le_supr_of_le h (hr _ hs h)] end end outer_measure /-! ### Metric measures In this section we use `measure_theory.outer_measure.to_measure` and theorems about `measure_theory.outer_measure.mk_metric'`/`measure_theory.outer_measure.mk_metric` to define `measure_theory.measure.mk_metric'`/`measure_theory.measure.mk_metric`. We also restate some lemmas about metric outer measures for metric measures. -/ namespace measure variables [measurable_space X] [borel_space X] /-- Given a function `m : set X → ℝ≥0∞`, `mk_metric' m` is the supremum of `μ r` over `r > 0`, where `μ r` is the maximal outer measure `μ` such that `μ s ≤ m s` for all `s`. While each `μ r` is an *outer* measure, the supremum is a measure. -/ def mk_metric' (m : set X → ℝ≥0∞) : measure X := (outer_measure.mk_metric' m).to_measure (outer_measure.mk_metric'_is_metric _).le_caratheodory /-- Given a function `m : ℝ≥0∞ → ℝ≥0∞`, `mk_metric m` is the supremum of `μ r` over `r > 0`, where `μ r` is the maximal outer measure `μ` such that `μ s ≤ m s` for all sets `s` that contain at least two points. While each `mk_metric'.pre` is an *outer* measure, the supremum is a measure. -/ def mk_metric (m : ℝ≥0∞ → ℝ≥0∞) : measure X := (outer_measure.mk_metric m).to_measure (outer_measure.mk_metric'_is_metric _).le_caratheodory @[simp] lemma mk_metric'_to_outer_measure (m : set X → ℝ≥0∞) : (mk_metric' m).to_outer_measure = (outer_measure.mk_metric' m).trim := rfl @[simp] lemma mk_metric_to_outer_measure (m : ℝ≥0∞ → ℝ≥0∞) : (mk_metric m : measure X).to_outer_measure = outer_measure.mk_metric m := outer_measure.trim_mk_metric m end measure lemma outer_measure.coe_mk_metric [measurable_space X] [borel_space X] (m : ℝ≥0∞ → ℝ≥0∞) : ⇑(outer_measure.mk_metric m : outer_measure X) = measure.mk_metric m := by rw [← measure.mk_metric_to_outer_measure, coe_to_outer_measure] namespace measure variables [measurable_space X] [borel_space X] /-- If `c ∉ {0, ∞}` and `m₁ d ≤ c * m₂ d` for `0 < d < ε` for some `ε > 0` (we use `≤ᶠ[𝓝[Ioi 0]]` to state this), then `mk_metric m₁ hm₁ ≤ c • mk_metric m₂ hm₂`. -/ lemma mk_metric_mono_smul {m₁ m₂ : ℝ≥0∞ → ℝ≥0∞} {c : ℝ≥0∞} (hc : c ≠ ∞) (h0 : c ≠ 0) (hle : m₁ ≤ᶠ[𝓝[Ioi 0] 0] c • m₂) : (mk_metric m₁ : measure X) ≤ c • mk_metric m₂ := begin intros s hs, rw [← outer_measure.coe_mk_metric, coe_smul, ← outer_measure.coe_mk_metric], exact outer_measure.mk_metric_mono_smul hc h0 hle s end /-- If `m₁ d ≤ m₂ d` for `0 < d < ε` for some `ε > 0` (we use `≤ᶠ[𝓝[Ioi 0]]` to state this), then `mk_metric m₁ hm₁ ≤ mk_metric m₂ hm₂`-/ lemma mk_metric_mono {m₁ m₂ : ℝ≥0∞ → ℝ≥0∞} (hle : m₁ ≤ᶠ[𝓝[Ioi 0] 0] m₂) : (mk_metric m₁ : measure X) ≤ mk_metric m₂ := by { convert mk_metric_mono_smul ennreal.one_ne_top ennreal.zero_lt_one.ne' _; simp * } /-- A formula for `measure_theory.measure.mk_metric`. -/ lemma mk_metric_apply (m : ℝ≥0∞ → ℝ≥0∞) (s : set X) : mk_metric m s = ⨆ (r : ℝ≥0∞) (hr : 0 < r), ⨅ (t : ℕ → set X) (hts : s ⊆ ⋃ n, t n) (ht : ∀ n, diam (t n) ≤ r), ∑' n, ⨆ (ht : ¬(t n).subsingleton), m (diam (t n)) := begin -- We mostly unfold the definitions but we need to switch the order of `∑'` and `⨅` -- and merge `(t n).nonempty` with `¬subsingleton (t n)` classical, simp only [← outer_measure.coe_mk_metric, outer_measure.mk_metric, outer_measure.mk_metric', outer_measure.supr_apply, outer_measure.mk_metric'.pre, outer_measure.bounded_by_apply, extend], refine supr_congr (λ r, r) surjective_id (λ r, supr_congr_Prop iff.rfl $ λ hr, infi_congr _ surjective_id $ λ t, infi_congr_Prop iff.rfl $ λ ht, _), by_cases htr : ∀ n, diam (t n) ≤ r, { rw [infi_eq_if, if_pos htr], congr' 1 with n : 1, simp only [infi_eq_if, htr n, id, if_true, supr_and'], refine supr_congr_Prop (and_iff_right_of_imp $ λ h, _) (λ _, rfl), contrapose! h, rw [not_nonempty_iff_eq_empty.1 h], exact subsingleton_empty }, { rw [infi_eq_if, if_neg htr], push_neg at htr, rcases htr with ⟨n, hn⟩, refine ennreal.tsum_eq_top_of_eq_top ⟨n, _⟩, rw [supr_eq_if, if_pos, infi_eq_if, if_neg], exact hn.not_le, rcases diam_pos_iff.1 ((zero_le r).trans_lt hn) with ⟨x, hx, -⟩, exact ⟨x, hx⟩ } end lemma le_mk_metric (m : ℝ≥0∞ → ℝ≥0∞) (μ : measure X) [has_no_atoms μ] (ε : ℝ≥0∞) (h₀ : 0 < ε) (h : ∀ s : set X, diam s ≤ ε → ¬s.subsingleton → μ s ≤ m (diam s)) : μ ≤ mk_metric m := begin rw [← to_outer_measure_le, mk_metric_to_outer_measure], exact outer_measure.le_mk_metric m μ.to_outer_measure measure_singleton ε h₀ h end /-- To bound the Hausdorff measure (or, more generally, for a measure defined using `measure_theory.measure.mk_metric`) of a set, one may use coverings with maximum diameter tending to `0`, indexed by any sequence of encodable types. -/ lemma mk_metric_le_liminf_tsum {β : Type*} {ι : β → Type*} [∀ n, encodable (ι n)] (s : set X) {l : filter β} (r : β → ℝ≥0∞) (hr : tendsto r l (𝓝 0)) (t : Π (n : β), ι n → set X) (ht : ∀ᶠ n in l, ∀ i, diam (t n i) ≤ r n) (hst : ∀ᶠ n in l, s ⊆ ⋃ i, t n i) (m : ℝ≥0∞ → ℝ≥0∞) : mk_metric m s ≤ liminf l (λ n, ∑' i, m (diam (t n i))) := begin simp only [mk_metric_apply], refine bsupr_le (λ ε hε, _), refine le_of_forall_le_of_dense (λ c hc, _), rcases ((frequently_lt_of_liminf_lt (by apply_auto_param) hc).and_eventually ((hr.eventually (gt_mem_nhds hε)).and (ht.and hst))).exists with ⟨n, hn, hrn, htn, hstn⟩, set u : ℕ → set X := λ j, ⋃ b ∈ decode₂ (ι n) j, t n b, refine binfi_le_of_le u (by rwa Union_decode₂) _, refine infi_le_of_le (λ j, _) _, { rw emetric.diam_Union_mem_option, exact bsupr_le (λ _ _, (htn _).trans hrn.le) }, { calc (∑' (j : ℕ), ⨆ (ht : ¬(u j).subsingleton), m (diam (u j))) = _ : tsum_Union_decode₂ (λ t : set X, ⨆ (h : ¬t.subsingleton), m (diam t)) (by simp) _ ... ≤ _ : ennreal.tsum_le_tsum (λ b, supr_le $ λ htb, le_rfl) ... ≤ c : hn.le } end /-- To bound the Hausdorff measure (or, more generally, for a measure defined using `measure_theory.measure.mk_metric`) of a set, one may use coverings with maximum diameter tending to `0`, indexed by any sequence of finite types. -/ lemma mk_metric_le_liminf_sum {β : Type*} {ι : β → Type*} [hι : ∀ n, fintype (ι n)] (s : set X) {l : filter β} (r : β → ℝ≥0∞) (hr : tendsto r l (𝓝 0)) (t : Π (n : β), ι n → set X) (ht : ∀ᶠ n in l, ∀ i, diam (t n i) ≤ r n) (hst : ∀ᶠ n in l, s ⊆ ⋃ i, t n i) (m : ℝ≥0∞ → ℝ≥0∞) : mk_metric m s ≤ liminf l (λ n, ∑ i, m (diam (t n i))) := begin haveI : ∀ n, encodable (ι n), from λ n, fintype.encodable _, simpa only [tsum_fintype] using mk_metric_le_liminf_tsum s r hr t ht hst m, end /-! ### Hausdorff measure and Hausdorff dimension -/ /-- Hausdorff measure on an (e)metric space. -/ def hausdorff_measure (d : ℝ) : measure X := mk_metric (λ r, r ^ d) localized "notation `μH[` d `]` := measure_theory.measure.hausdorff_measure d" in measure_theory lemma le_hausdorff_measure (d : ℝ) (μ : measure X) [has_no_atoms μ] (ε : ℝ≥0∞) (h₀ : 0 < ε) (h : ∀ s : set X, diam s ≤ ε → ¬s.subsingleton → μ s ≤ diam s ^ d) : μ ≤ μH[d] := le_mk_metric _ μ ε h₀ h /-- A formula for `μH[d] s` that works for all `d`. In case of a positive `d` a simpler formula is available as `measure_theory.measure.hausdorff_measure_apply`. -/ lemma hausdorff_measure_apply' (d : ℝ) (s : set X) : μH[d] s = ⨆ (r : ℝ≥0∞) (hr : 0 < r), ⨅ (t : ℕ → set X) (hts : s ⊆ ⋃ n, t n) (ht : ∀ n, diam (t n) ≤ r), ∑' n, ⨆ (ht : ¬(t n).subsingleton), (diam (t n)) ^ d := mk_metric_apply _ _ /-- A formula for `μH[d] s` that works for all positive `d`. -/ lemma hausdorff_measure_apply {d : ℝ} (hd : 0 < d) (s : set X) : μH[d] s = ⨆ (r : ℝ≥0∞) (hr : 0 < r), ⨅ (t : ℕ → set X) (hts : s ⊆ ⋃ n, t n) (ht : ∀ n, diam (t n) ≤ r), ∑' n, diam (t n) ^ d := begin classical, rw hausdorff_measure_apply', -- I wish `congr'` was able to generate this refine supr_congr id surjective_id (λ r, supr_congr_Prop iff.rfl $ λ hr, infi_congr id surjective_id $ λ t, infi_congr_Prop iff.rfl $ λ hts, infi_congr_Prop iff.rfl $ λ ht, tsum_congr $ λ n, _), rw [supr_eq_if], split_ifs with ht', { erw [diam_eq_zero_iff.2 ht', ennreal.zero_rpow_of_pos hd, ennreal.bot_eq_zero] }, { refl } end /-- To bound the Hausdorff measure of a set, one may use coverings with maximum diameter tending to `0`, indexed by any sequence of encodable types. -/ lemma hausdorff_measure_le_liminf_tsum {β : Type*} {ι : β → Type*} [hι : ∀ n, encodable (ι n)] (d : ℝ) (s : set X) {l : filter β} (r : β → ℝ≥0∞) (hr : tendsto r l (𝓝 0)) (t : Π (n : β), ι n → set X) (ht : ∀ᶠ n in l, ∀ i, diam (t n i) ≤ r n) (hst : ∀ᶠ n in l, s ⊆ ⋃ i, t n i) : μH[d] s ≤ liminf l (λ n, ∑' i, diam (t n i) ^ d) := mk_metric_le_liminf_tsum s r hr t ht hst _ /-- To bound the Hausdorff measure of a set, one may use coverings with maximum diameter tending to `0`, indexed by any sequence of finite types. -/ lemma hausdorff_measure_le_liminf_sum {β : Type*} {ι : β → Type*} [hι : ∀ n, fintype (ι n)] (d : ℝ) (s : set X) {l : filter β} (r : β → ℝ≥0∞) (hr : tendsto r l (𝓝 0)) (t : Π (n : β), ι n → set X) (ht : ∀ᶠ n in l, ∀ i, diam (t n i) ≤ r n) (hst : ∀ᶠ n in l, s ⊆ ⋃ i, t n i) : μH[d] s ≤ liminf l (λ n, ∑ i, diam (t n i) ^ d) := mk_metric_le_liminf_sum s r hr t ht hst _ /-- If `d₁ < d₂`, then for any set `s` we have either `μH[d₂] s = 0`, or `μH[d₁] s = ∞`. -/ lemma hausdorff_measure_zero_or_top {d₁ d₂ : ℝ} (h : d₁ < d₂) (s : set X) : μH[d₂] s = 0 ∨ μH[d₁] s = ∞ := begin by_contra H, push_neg at H, suffices : ∀ (c : ℝ≥0), c ≠ 0 → μH[d₂] s ≤ c * μH[d₁] s, { rcases ennreal.exists_nnreal_pos_mul_lt H.2 H.1 with ⟨c, hc0, hc⟩, exact hc.not_le (this c (pos_iff_ne_zero.1 hc0)) }, intros c hc, refine le_iff'.1 (mk_metric_mono_smul ennreal.coe_ne_top (by exact_mod_cast hc) _) s, have : 0 < (c ^ (d₂ - d₁)⁻¹ : ℝ≥0∞), { rw [ennreal.coe_rpow_of_ne_zero hc, pos_iff_ne_zero, ne.def, ennreal.coe_eq_zero, nnreal.rpow_eq_zero_iff], exact mt and.left hc }, filter_upwards [Ioo_mem_nhds_within_Ioi ⟨le_rfl, this⟩], rintro r ⟨hr₀, hrc⟩, lift r to ℝ≥0 using ne_top_of_lt hrc, rw [pi.smul_apply, smul_eq_mul, ← ennreal.div_le_iff_le_mul (or.inr ennreal.coe_ne_top) (or.inr $ mt ennreal.coe_eq_zero.1 hc), ← ennreal.rpow_sub _ _ hr₀.ne' ennreal.coe_ne_top], refine (ennreal.rpow_lt_rpow hrc (sub_pos.2 h)).le.trans _, rw [← ennreal.rpow_mul, inv_mul_cancel (sub_pos.2 h).ne', ennreal.rpow_one], exact le_rfl end /-- Hausdorff measure `μH[d] s` is monotone in `d`. -/ lemma hausdorff_measure_mono {d₁ d₂ : ℝ} (h : d₁ ≤ d₂) (s : set X) : μH[d₂] s ≤ μH[d₁] s := begin rcases h.eq_or_lt with rfl|h, { exact le_rfl }, cases hausdorff_measure_zero_or_top h s with hs hs, { rw hs, exact zero_le _ }, { rw hs, exact le_top } end instance no_atoms_hausdorff (d : ℝ) : has_no_atoms (hausdorff_measure d : measure X) := begin refine ⟨λ x, _⟩, rw [← nonpos_iff_eq_zero, hausdorff_measure_apply'], refine bsupr_le (λ ε ε0, binfi_le_of_le (λ n, {x}) _ (infi_le_of_le (λ n, _) _)), { exact subset_Union (λ n, {x} : ℕ → set X) 0 }, { simp only [emetric.diam_singleton, zero_le] }, { simp } end end measure open_locale measure_theory open measure /-! ### Hausdorff measure and Lebesgue measure -/ /-- In the space `ι → ℝ`, Hausdorff measure coincides exactly with Lebesgue measure. -/ @[simp] theorem hausdorff_measure_pi_real {ι : Type*} [fintype ι] [nonempty ι] : (μH[fintype.card ι] : measure (ι → ℝ)) = volume := begin classical, -- it suffices to check that the two measures coincide on products of rational intervals refine (pi_eq_generate_from (λ i, real.borel_eq_generate_from_Ioo_rat.symm) (λ i, real.is_pi_system_Ioo_rat) (λ i, real.finite_spanning_sets_in_Ioo_rat _) _).symm, simp only [mem_Union, mem_singleton_iff], -- fix such a product `s` of rational intervals, of the form `Π (a i, b i)`. intros s hs, choose a b H using hs, obtain rfl : s = λ i, Ioo (a i) (b i), from funext (λ i, (H i).2), replace H := λ i, (H i).1, apply le_antisymm _, -- first check that `volume s ≤ μH s` { have Hle : volume ≤ (μH[fintype.card ι] : measure (ι → ℝ)), { refine le_hausdorff_measure _ _ ∞ ennreal.coe_lt_top (λ s h₁ h₂, _), rw [ennreal.rpow_nat_cast], exact real.volume_pi_le_diam_pow s }, rw [← volume_pi_pi (λ i, Ioo (a i : ℝ) (b i)) (λ i, measurable_set_Ioo)], exact measure.le_iff'.1 Hle _ }, /- For the other inequality `μH s ≤ volume s`, we use a covering of `s` by sets of small diameter `1/n`, namely cubes with left-most point of the form `a i + f i / n` with `f i` ranging between `0` and `⌈(b i - a i) * n⌉`. Their number is asymptotic to `n^d * Π (b i - a i)`. -/ have I : ∀ i, 0 ≤ (b i : ℝ) - a i := λ i, by simpa only [sub_nonneg, rat.cast_le] using (H i).le, let γ := λ (n : ℕ), (Π (i : ι), fin ⌈((b i : ℝ) - a i) * n⌉₊), let t : Π (n : ℕ), γ n → set (ι → ℝ) := λ n f, set.pi univ (λ i, Icc (a i + f i / n) (a i + (f i + 1) / n)), have A : tendsto (λ (n : ℕ), 1/(n : ℝ≥0∞)) at_top (𝓝 0), by simp only [one_div, ennreal.tendsto_inv_nat_nhds_zero], have B : ∀ᶠ n in at_top, ∀ (i : γ n), diam (t n i) ≤ 1 / n, { apply eventually_at_top.2 ⟨1, λ n hn, _⟩, assume f, apply diam_pi_le_of_le (λ b, _), simp only [real.ediam_Icc, add_div, ennreal.of_real_div_of_pos (nat.cast_pos.mpr hn), le_refl, add_sub_add_left_eq_sub, add_sub_cancel', ennreal.of_real_one, ennreal.of_real_coe_nat] }, have C : ∀ᶠ n in at_top, set.pi univ (λ (i : ι), Ioo (a i : ℝ) (b i)) ⊆ ⋃ (i : γ n), t n i, { apply eventually_at_top.2 ⟨1, λ n hn, _⟩, have npos : (0 : ℝ) < n := nat.cast_pos.2 hn, assume x hx, simp only [mem_Ioo, mem_univ_pi] at hx, simp only [mem_Union, mem_Ioo, mem_univ_pi, coe_coe], let f : γ n := λ i, ⟨⌊(x i - a i) * n⌋₊, begin apply nat.floor_lt_ceil_of_lt_of_pos, { refine (mul_lt_mul_right npos).2 _, simp only [(hx i).right, sub_lt_sub_iff_right] }, { refine mul_pos _ npos, simpa only [rat.cast_lt, sub_pos] using H i } end⟩, refine ⟨f, λ i, ⟨_, _⟩⟩, { calc (a i : ℝ) + ⌊(x i - a i) * n⌋₊ / n ≤ (a i : ℝ) + ((x i - a i) * n) / n : begin refine add_le_add le_rfl ((div_le_div_right npos).2 _), exact nat.floor_le (mul_nonneg (sub_nonneg.2 (hx i).1.le) npos.le), end ... = x i : by field_simp [npos.ne'] }, { calc x i = (a i : ℝ) + ((x i - a i) * n) / n : by field_simp [npos.ne'] ... ≤ (a i : ℝ) + (⌊(x i - a i) * n⌋₊ + 1) / n : add_le_add le_rfl ((div_le_div_right npos).2 (nat.lt_floor_add_one _).le) } }, calc μH[fintype.card ι] (set.pi univ (λ (i : ι), Ioo (a i : ℝ) (b i))) ≤ liminf at_top (λ (n : ℕ), ∑ (i : γ n), diam (t n i) ^ ↑(fintype.card ι)) : hausdorff_measure_le_liminf_sum _ (set.pi univ (λ i, Ioo (a i : ℝ) (b i))) (λ (n : ℕ), 1/(n : ℝ≥0∞)) A t B C ... ≤ liminf at_top (λ (n : ℕ), ∑ (i : γ n), (1/n) ^ (fintype.card ι)) : begin refine liminf_le_liminf _ (by is_bounded_default), filter_upwards [B], assume n hn, apply finset.sum_le_sum (λ i _, _), rw ennreal.rpow_nat_cast, exact pow_le_pow_of_le_left' (hn i) _, end ... = liminf at_top (λ (n : ℕ), ∏ (i : ι), (⌈((b i : ℝ) - a i) * n⌉₊ : ℝ≥0∞) / n) : begin simp only [finset.card_univ, nat.cast_prod, one_mul, fintype.card_fin, finset.sum_const, nsmul_eq_mul, fintype.card_pi, div_eq_mul_inv, finset.prod_mul_distrib, finset.prod_const] end ... = ∏ (i : ι), volume (Ioo (a i : ℝ) (b i)) : begin simp only [real.volume_Ioo], apply tendsto.liminf_eq, refine ennreal.tendsto_finset_prod_of_ne_top _ (λ i hi, _) (λ i hi, _), { apply tendsto.congr' _ ((ennreal.continuous_of_real.tendsto _).comp ((tendsto_nat_ceil_mul_div_at_top (I i)).comp tendsto_coe_nat_at_top_at_top)), apply eventually_at_top.2 ⟨1, λ n hn, _⟩, simp only [ennreal.of_real_div_of_pos (nat.cast_pos.mpr hn), comp_app, ennreal.of_real_coe_nat] }, { simp only [ennreal.of_real_ne_top, ne.def, not_false_iff] } end end end measure_theory /-! ### Hausdorff measure, Hausdorff dimension, and Hölder or Lipschitz continuous maps -/ open_locale measure_theory open measure_theory measure_theory.measure variables [measurable_space X] [borel_space X] [measurable_space Y] [borel_space Y] namespace holder_on_with variables {C r : ℝ≥0} {f : X → Y} {s t : set X} /-- If `f : X → Y` is Hölder continuous on `s` with a positive exponent `r`, then `μH[d] (f '' s) ≤ C ^ d * μH[r * d] s`. -/ lemma hausdorff_measure_image_le (h : holder_on_with C r f s) (hr : 0 < r) {d : ℝ} (hd : 0 ≤ d) : μH[d] (f '' s) ≤ C ^ d * μH[r * d] s := begin -- We start with the trivial case `C = 0` rcases (zero_le C).eq_or_lt with rfl|hC0, { have : (f '' s).subsingleton, by simpa [diam_eq_zero_iff] using h.ediam_image_le, rw this.measure_zero, exact zero_le _ }, { have hCd0 : (C : ℝ≥0∞) ^ d ≠ 0, by simp [hC0.ne'], have hCd : (C : ℝ≥0∞) ^ d ≠ ∞, by simp [hd], simp only [hausdorff_measure_apply', ennreal.mul_supr, ennreal.mul_infi_of_ne hCd0 hCd, ← ennreal.tsum_mul_left], refine supr_le (λ R, supr_le $ λ hR, _), have : tendsto (λ d : ℝ≥0∞, (C : ℝ≥0∞) * d ^ (r : ℝ)) (𝓝 0) (𝓝 0), from ennreal.tendsto_const_mul_rpow_nhds_zero_of_pos ennreal.coe_ne_top hr, rcases ennreal.nhds_zero_basis_Iic.eventually_iff.1 (this.eventually (gt_mem_nhds hR)) with ⟨δ, δ0, H⟩, refine le_supr_of_le δ (le_supr_of_le δ0 $ le_binfi $ λ t hst, le_infi $ λ htδ, _), refine binfi_le_of_le (λ n, f '' (t n ∩ s)) _ (infi_le_of_le (λ n, _) _), { rw [← image_Union, ← Union_inter], exact image_subset _ (subset_inter hst subset.rfl) }, { exact (h.ediam_image_inter_le (t n)).trans (H (htδ n)).le }, { refine ennreal.tsum_le_tsum (λ n, supr_le $ λ hft, le_supr_of_le (λ ht, hft $ (ht.mono (inter_subset_left _ _)).image f) _), rw [ennreal.rpow_mul, ← ennreal.mul_rpow_of_nonneg _ _ hd], exact ennreal.rpow_le_rpow (h.ediam_image_inter_le _) hd } } end end holder_on_with namespace lipschitz_on_with variables {K : ℝ≥0} {f : X → Y} {s t : set X} /-- If `f : X → Y` is `K`-Lipschitz on `s`, then `μH[d] (f '' s) ≤ K ^ d * μH[d] s`. -/ lemma hausdorff_measure_image_le (h : lipschitz_on_with K f s) {d : ℝ} (hd : 0 ≤ d) : μH[d] (f '' s) ≤ K ^ d * μH[d] s := by simpa only [nnreal.coe_one, one_mul] using h.holder_on_with.hausdorff_measure_image_le zero_lt_one hd end lipschitz_on_with namespace lipschitz_with variables {K : ℝ≥0} {f : X → Y} /-- If `f` is a `K`-Lipschitz map, then it increases the Hausdorff `d`-measures of sets at most by the factor of `K ^ d`.-/ lemma hausdorff_measure_image_le (h : lipschitz_with K f) {d : ℝ} (hd : 0 ≤ d) (s : set X) : μH[d] (f '' s) ≤ K ^ d * μH[d] s := (h.lipschitz_on_with s).hausdorff_measure_image_le hd end lipschitz_with /-! ### Antilipschitz maps do not decrease Hausdorff measures and dimension -/ namespace antilipschitz_with variables {f : X → Y} {K : ℝ≥0} {d : ℝ} lemma hausdorff_measure_preimage_le (hf : antilipschitz_with K f) (hd : 0 ≤ d) (s : set Y) : μH[d] (f ⁻¹' s) ≤ K ^ d * μH[d] s := begin rcases eq_or_ne K 0 with rfl|h0, { haveI : subsingleton X := hf.subsingleton, have : (f ⁻¹' s).subsingleton, from subsingleton_univ.mono (subset_univ _), rw this.measure_zero, exact zero_le _ }, have hKd0 : (K : ℝ≥0∞) ^ d ≠ 0, by simp [h0], have hKd : (K : ℝ≥0∞) ^ d ≠ ∞, by simp [hd], simp only [hausdorff_measure_apply', ennreal.mul_supr, ennreal.mul_infi_of_ne hKd0 hKd, ← ennreal.tsum_mul_left], refine bsupr_le (λ ε ε0, _), refine le_bsupr_of_le (ε / K) (by simp [ε0.ne']) _, refine le_binfi (λ t hst, le_infi $ λ htε, _), replace hst : f ⁻¹' s ⊆ _ := preimage_mono hst, rw preimage_Union at hst, refine binfi_le_of_le _ hst (infi_le_of_le (λ n, _) _), { exact (hf.ediam_preimage_le _).trans (ennreal.mul_le_of_le_div' $ htε n) }, { refine ennreal.tsum_le_tsum (λ n, supr_le $ λ H, le_supr_of_le (λ h, H $ h.preimage hf.injective) _), rw [← ennreal.mul_rpow_of_nonneg _ _ hd], exact ennreal.rpow_le_rpow (hf.ediam_preimage_le _) hd } end lemma le_hausdorff_measure_image (hf : antilipschitz_with K f) (hd : 0 ≤ d) (s : set X) : μH[d] s ≤ K ^ d * μH[d] (f '' s) := calc μH[d] s ≤ μH[d] (f ⁻¹' (f '' s)) : measure_mono (subset_preimage_image _ _) ... ≤ K ^ d * μH[d] (f '' s) : hf.hausdorff_measure_preimage_le hd (f '' s) end antilipschitz_with /-! ### Isometries preserve the Hausdorff measure and Hausdorff dimension -/ namespace isometry variables {f : X → Y} {d : ℝ} lemma hausdorff_measure_image (hf : isometry f) (hd : 0 ≤ d ∨ surjective f) (s : set X) : μH[d] (f '' s) = μH[d] s := begin simp only [hausdorff_measure, ← outer_measure.coe_mk_metric, ← outer_measure.comap_apply], rw [outer_measure.isometry_comap_mk_metric _ hf (hd.imp_left _)], exact λ hd x y hxy, ennreal.rpow_le_rpow hxy hd end lemma hausdorff_measure_preimage (hf : isometry f) (hd : 0 ≤ d ∨ surjective f) (s : set Y) : μH[d] (f ⁻¹' s) = μH[d] (s ∩ range f) := by rw [← hf.hausdorff_measure_image hd, image_preimage_eq_inter_range] lemma map_hausdorff_measure (hf : isometry f) (hd : 0 ≤ d ∨ surjective f) : measure.map f μH[d] = (μH[d]).restrict (range f) := begin ext1 s hs, rw [map_apply hf.continuous.measurable hs, restrict_apply hs, hf.hausdorff_measure_preimage hd] end end isometry namespace isometric @[simp] lemma hausdorff_measure_image (e : X ≃ᵢ Y) (d : ℝ) (s : set X) : μH[d] (e '' s) = μH[d] s := e.isometry.hausdorff_measure_image (or.inr e.surjective) s @[simp] lemma hausdorff_measure_preimage (e : X ≃ᵢ Y) (d : ℝ) (s : set Y) : μH[d] (e ⁻¹' s) = μH[d] s := by rw [← e.image_symm, e.symm.hausdorff_measure_image] end isometric
a24e5d3b994ab50ecf3a149aa1143685db36ab96
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/big_operators/intervals.lean
baf2ae60a8fafa6eaf6c4fd8f47a5d6753f15612
[ "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
10,559
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import algebra.big_operators.basic import algebra.module.basic import data.nat.interval import tactic.linarith import tactic.abel /-! # Results about big operators over intervals We prove results about big operators over intervals (mostly the `ℕ`-valued `Ico m n`). -/ universes u v w open_locale big_operators nat namespace finset section generic variables {α : Type u} {β : Type v} {γ : Type w} {s₂ s₁ s : finset α} {a : α} {g f : α → β} variables [comm_monoid β] @[to_additive] lemma prod_Ico_add' [ordered_cancel_add_comm_monoid α] [has_exists_add_of_le α] [locally_finite_order α] (f : α → β) (a b c : α) : (∏ x in Ico a b, f (x + c)) = (∏ x in Ico (a + c) (b + c), f x) := begin classical, rw [←image_add_right_Ico, prod_image (λ x hx y hy h, add_right_cancel h)], end @[to_additive] lemma prod_Ico_add [ordered_cancel_add_comm_monoid α] [has_exists_add_of_le α] [locally_finite_order α] (f : α → β) (a b c : α) : (∏ x in Ico a b, f (c + x)) = (∏ x in Ico (a + c) (b + c), f x) := begin convert prod_Ico_add' f a b c, simp_rw add_comm, end lemma sum_Ico_succ_top {δ : Type*} [add_comm_monoid δ] {a b : ℕ} (hab : a ≤ b) (f : ℕ → δ) : (∑ k in Ico a (b + 1), f k) = (∑ k in Ico a b, f k) + f b := by rw [nat.Ico_succ_right_eq_insert_Ico hab, sum_insert right_not_mem_Ico, add_comm] @[to_additive] lemma prod_Ico_succ_top {a b : ℕ} (hab : a ≤ b) (f : ℕ → β) : (∏ k in Ico a (b + 1), f k) = (∏ k in Ico a b, f k) * f b := @sum_Ico_succ_top (additive β) _ _ _ hab _ lemma sum_eq_sum_Ico_succ_bot {δ : Type*} [add_comm_monoid δ] {a b : ℕ} (hab : a < b) (f : ℕ → δ) : (∑ k in Ico a b, f k) = f a + (∑ k in Ico (a + 1) b, f k) := have ha : a ∉ Ico (a + 1) b, by simp, by rw [← sum_insert ha, nat.Ico_insert_succ_left hab] @[to_additive] lemma prod_eq_prod_Ico_succ_bot {a b : ℕ} (hab : a < b) (f : ℕ → β) : (∏ k in Ico a b, f k) = f a * (∏ k in Ico (a + 1) b, f k) := @sum_eq_sum_Ico_succ_bot (additive β) _ _ _ hab _ @[to_additive] lemma prod_Ico_consecutive (f : ℕ → β) {m n k : ℕ} (hmn : m ≤ n) (hnk : n ≤ k) : (∏ i in Ico m n, f i) * (∏ i in Ico n k, f i) = (∏ i in Ico m k, f i) := Ico_union_Ico_eq_Ico hmn hnk ▸ eq.symm $ prod_union $ Ico_disjoint_Ico_consecutive m n k @[to_additive] lemma prod_Ioc_consecutive (f : ℕ → β) {m n k : ℕ} (hmn : m ≤ n) (hnk : n ≤ k) : (∏ i in Ioc m n, f i) * (∏ i in Ioc n k, f i) = (∏ i in Ioc m k, f i) := begin rw [← Ioc_union_Ioc_eq_Ioc hmn hnk, prod_union], apply disjoint_left.2 (λ x hx h'x, _), exact lt_irrefl _ ((mem_Ioc.1 h'x).1.trans_le (mem_Ioc.1 hx).2), end @[to_additive] lemma prod_Ioc_succ_top {a b : ℕ} (hab : a ≤ b) (f : ℕ → β) : (∏ k in Ioc a (b + 1), f k) = (∏ k in Ioc a b, f k) * f (b + 1) := by rw [← prod_Ioc_consecutive _ hab (nat.le_succ b), nat.Ioc_succ_singleton, prod_singleton] @[to_additive] lemma prod_range_mul_prod_Ico (f : ℕ → β) {m n : ℕ} (h : m ≤ n) : (∏ k in range m, f k) * (∏ k in Ico m n, f k) = (∏ k in range n, f k) := nat.Ico_zero_eq_range ▸ nat.Ico_zero_eq_range ▸ prod_Ico_consecutive f m.zero_le h @[to_additive] lemma prod_Ico_eq_mul_inv {δ : Type*} [comm_group δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) : (∏ k in Ico m n, f k) = (∏ k in range n, f k) * (∏ k in range m, f k)⁻¹ := eq_mul_inv_iff_mul_eq.2 $ by rw [mul_comm]; exact prod_range_mul_prod_Ico f h @[to_additive] lemma prod_Ico_eq_div {δ : Type*} [comm_group δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) : (∏ k in Ico m n, f k) = (∏ k in range n, f k) / (∏ k in range m, f k) := by simpa only [div_eq_mul_inv] using prod_Ico_eq_mul_inv f h @[to_additive] lemma prod_range_sub_prod_range {α : Type*} [comm_group α] {f : ℕ → α} {n m : ℕ} (hnm : n ≤ m) : (∏ k in range m, f k) / (∏ k in range n, f k) = ∏ k in (range m).filter (λ k, n ≤ k), f k := begin rw [← prod_Ico_eq_div f hnm], congr, apply finset.ext, simp only [mem_Ico, mem_filter, mem_range, *], tauto, end /-- The two ways of summing over `(i,j)` in the range `a<=i<=j<b` are equal. -/ lemma sum_Ico_Ico_comm {M : Type*} [add_comm_monoid M] (a b : ℕ) (f : ℕ → ℕ → M) : ∑ i in finset.Ico a b, ∑ j in finset.Ico i b, f i j = ∑ j in finset.Ico a b, ∑ i in finset.Ico a (j+1), f i j := begin rw [finset.sum_sigma', finset.sum_sigma'], refine finset.sum_bij' (λ (x : Σ (i : ℕ), ℕ) _, (⟨x.2, x.1⟩ : Σ (i : ℕ), ℕ)) _ (λ _ _, rfl) (λ (x : Σ (i : ℕ), ℕ) _, (⟨x.2, x.1⟩ : Σ (i : ℕ), ℕ)) _ (by rintro ⟨⟩ _; refl) (by rintro ⟨⟩ _; refl); simp only [finset.mem_Ico, sigma.forall, finset.mem_sigma]; rintros a b ⟨⟨h₁,h₂⟩, ⟨h₃, h₄⟩⟩; refine ⟨⟨_, _⟩, ⟨_, _⟩⟩; linarith end @[to_additive] lemma prod_Ico_eq_prod_range (f : ℕ → β) (m n : ℕ) : (∏ k in Ico m n, f k) = (∏ k in range (n - m), f (m + k)) := begin by_cases h : m ≤ n, { rw [←nat.Ico_zero_eq_range, prod_Ico_add, zero_add, tsub_add_cancel_of_le h] }, { replace h : n ≤ m := le_of_not_ge h, rw [Ico_eq_empty_of_le h, tsub_eq_zero_iff_le.mpr h, range_zero, prod_empty, prod_empty] } end lemma prod_Ico_reflect (f : ℕ → β) (k : ℕ) {m n : ℕ} (h : m ≤ n + 1) : ∏ j in Ico k m, f (n - j) = ∏ j in Ico (n + 1 - m) (n + 1 - k), f j := begin have : ∀ i < m, i ≤ n, { intros i hi, exact (add_le_add_iff_right 1).1 (le_trans (nat.lt_iff_add_one_le.1 hi) h) }, cases lt_or_le k m with hkm hkm, { rw [← nat.Ico_image_const_sub_eq_Ico (this _ hkm)], refine (prod_image _).symm, simp only [mem_Ico], rintros i ⟨ki, im⟩ j ⟨kj, jm⟩ Hij, rw [← tsub_tsub_cancel_of_le (this _ im), Hij, tsub_tsub_cancel_of_le (this _ jm)] }, { simp [Ico_eq_empty_of_le, tsub_le_tsub_left, hkm] } end lemma sum_Ico_reflect {δ : Type*} [add_comm_monoid δ] (f : ℕ → δ) (k : ℕ) {m n : ℕ} (h : m ≤ n + 1) : ∑ j in Ico k m, f (n - j) = ∑ j in Ico (n + 1 - m) (n + 1 - k), f j := @prod_Ico_reflect (multiplicative δ) _ f k m n h lemma prod_range_reflect (f : ℕ → β) (n : ℕ) : ∏ j in range n, f (n - 1 - j) = ∏ j in range n, f j := begin cases n, { simp }, { simp only [←nat.Ico_zero_eq_range, nat.succ_sub_succ_eq_sub, tsub_zero], rw prod_Ico_reflect _ _ le_rfl, simp } end lemma sum_range_reflect {δ : Type*} [add_comm_monoid δ] (f : ℕ → δ) (n : ℕ) : ∑ j in range n, f (n - 1 - j) = ∑ j in range n, f j := @prod_range_reflect (multiplicative δ) _ f n @[simp] lemma prod_Ico_id_eq_factorial : ∀ n : ℕ, ∏ x in Ico 1 (n + 1), x = n! | 0 := rfl | (n+1) := by rw [prod_Ico_succ_top $ nat.succ_le_succ $ zero_le n, nat.factorial_succ, prod_Ico_id_eq_factorial n, nat.succ_eq_add_one, mul_comm] @[simp] lemma prod_range_add_one_eq_factorial : ∀ n : ℕ, ∏ x in range n, (x+1) = n! | 0 := rfl | (n+1) := by simp [finset.range_succ, prod_range_add_one_eq_factorial n] section gauss_sum /-- Gauss' summation formula -/ lemma sum_range_id_mul_two (n : ℕ) : (∑ i in range n, i) * 2 = n * (n - 1) := calc (∑ i in range n, i) * 2 = (∑ i in range n, i) + (∑ i in range n, (n - 1 - i)) : by rw [sum_range_reflect (λ i, i) n, mul_two] ... = ∑ i in range n, (i + (n - 1 - i)) : sum_add_distrib.symm ... = ∑ i in range n, (n - 1) : sum_congr rfl $ λ i hi, add_tsub_cancel_of_le $ nat.le_pred_of_lt $ mem_range.1 hi ... = n * (n - 1) : by rw [sum_const, card_range, nat.nsmul_eq_mul] /-- Gauss' summation formula -/ lemma sum_range_id (n : ℕ) : (∑ i in range n, i) = (n * (n - 1)) / 2 := by rw [← sum_range_id_mul_two n, nat.mul_div_cancel]; exact dec_trivial end gauss_sum end generic section nat variable {β : Type*} variables (f g : ℕ → β) {m n : ℕ} section group variable [comm_group β] @[to_additive] lemma prod_range_succ_div_prod : (∏ i in range (n+1), f i) / ∏ i in range n, f i = f n := div_eq_iff_eq_mul'.mpr $ prod_range_succ f n @[to_additive] lemma prod_range_succ_div_top : (∏ i in range (n+1), f i) / f n = ∏ i in range n, f i := div_eq_iff_eq_mul.mpr $ prod_range_succ f n @[to_additive] lemma prod_Ico_div_bot (hmn : m < n) : (∏ i in Ico m n, f i) / f m = ∏ i in Ico (m+1) n, f i := div_eq_iff_eq_mul'.mpr $ prod_eq_prod_Ico_succ_bot hmn _ @[to_additive] lemma prod_Ico_succ_div_top (hmn : m ≤ n) : (∏ i in Ico m (n+1), f i) / f n = ∏ i in Ico m n, f i := div_eq_iff_eq_mul.mpr $ prod_Ico_succ_top hmn _ end group end nat section module variables {R M : Type*} [ring R] [add_comm_group M] [module R M] (f : ℕ → R) (g : ℕ → M) {m n : ℕ} open finset -- The partial sum of `g`, starting from zero local notation `G ` n:80 := ∑ i in range n, g i /-- **Summation by parts**, also known as **Abel's lemma** or an **Abel transformation** -/ theorem sum_Ico_by_parts (hmn : m < n) : ∑ i in Ico m n, f i • g i = f (n-1) • G n - f m • G m - ∑ i in Ico m (n-1), (f (i+1) - f i) • G (i+1) := begin have h₁ : ∑ i in Ico (m+1) n, (f i • G i) = ∑ i in Ico m (n-1), (f (i+1) • G (i+1)), { conv in n { rw ←nat.sub_add_cancel (nat.one_le_of_lt hmn) }, rw ←sum_Ico_add' }, have h₂ : ∑ i in Ico (m+1) n, (f i • G (i+1)) = ∑ i in Ico m (n-1), (f i • G (i+1)) + f (n-1) • G n - f m • G (m+1) := by rw [←sum_Ico_sub_bot _ hmn, ←sum_Ico_succ_sub_top _ (nat.le_pred_of_lt hmn), nat.sub_add_cancel (pos_of_gt hmn), sub_add_cancel], rw sum_eq_sum_Ico_succ_bot hmn, conv { for (f _ • g _) [2] { rw ← sum_range_succ_sub_sum g } }, simp_rw [smul_sub, sum_sub_distrib, h₂, h₁], conv_lhs { congr, skip, rw [←add_sub, add_comm, ←add_sub, ←sum_sub_distrib] }, have : ∀ i, f i • G (i+1) - f (i+1) • G (i+1) = -((f (i+1) - f i) • G (i+1)), { intro i, rw sub_smul, abel }, simp_rw [this, sum_neg_distrib, sum_range_succ, smul_add], abel, end variable (n) /-- **Summation by parts** for ranges -/ lemma sum_range_by_parts : ∑ i in range n, (f i • g i) = f (n-1) • G n - ∑ i in range (n-1), (f (i+1) - f i) • G (i+1) := begin by_cases hn : n = 0, { simp [hn], }, { rw [range_eq_Ico, sum_Ico_by_parts f g (nat.pos_of_ne_zero hn), sum_range_zero, smul_zero, sub_zero, range_eq_Ico] }, end end module end finset
700baf024d7911dad55e599b2554784af069fb96
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/lake/Lake/Config/Workspace.lean
8bbba8fc14a351ed37c0987493d985cb5908d99f
[ "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
8,038
lean
/- Copyright (c) 2021 Mac Malone. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mac Malone -/ import Lean.Util.Paths import Lake.Config.FacetConfig import Lake.Config.TargetConfig import Lake.Config.Env import Lake.Util.Log open System namespace Lake /-- A Lake workspace -- the top-level package directory. -/ structure Workspace : Type where /-- The root package of the workspace. -/ root : Package /-- The detect `Lake.Env` of the workspace. -/ lakeEnv : Lake.Env /-- Name-package map of packages within the workspace. -/ packageMap : DNameMap NPackage := {} /-- Name-configuration map of module facets defined in the workspace. -/ moduleFacetConfigs : DNameMap ModuleFacetConfig /-- Name-configuration map of package facets defined in the workspace. -/ packageFacetConfigs : DNameMap PackageFacetConfig /-- Name-configuration map of library facets defined in the workspace. -/ libraryFacetConfigs : DNameMap LibraryFacetConfig instance : Nonempty Workspace := have : Inhabited Package := Classical.inhabited_of_nonempty inferInstance by refine' ⟨{..}⟩ <;> exact default hydrate_opaque_type OpaqueWorkspace Workspace namespace Workspace /-- The path to the workspace's directory (i.e., the directory of the root package). -/ @[inline] def dir (self : Workspace) : FilePath := self.root.dir /-- The workspace's configuration. -/ @[inline] def config (self : Workspace) : WorkspaceConfig := self.root.config.toWorkspaceConfig /-- The path to the workspace's remote packages directory relative to `dir`. -/ @[inline] def relPkgsDir (self : Workspace) : FilePath := self.root.relPkgsDir /-- The workspace's `dir` joined with its `relPkgsDir`. -/ @[inline] def pkgsDir (self : Workspace) : FilePath := self.root.pkgsDir /-- The workspace's Lake manifest. -/ @[inline] def manifestFile (self : Workspace) : FilePath := self.root.manifestFile /-- The `List` of packages to the workspace. -/ def packageList (self : Workspace) : List Package := self.packageMap.revFold (fun pkgs _ pkg => pkg.toPackage :: pkgs) [] /-- The `Array` of packages to the workspace. -/ def packageArray (self : Workspace) : Array Package := self.packageMap.fold (fun pkgs _ pkg => pkgs.push pkg.toPackage) #[] /-- Add a package to the workspace. -/ def addPackage (pkg : Package) (self : Workspace) : Workspace := {self with packageMap := self.packageMap.insert pkg.name pkg} /-- Try to find a package within the workspace with the given name. -/ @[inline] def findPackage? (name : Name) (self : Workspace) : Option (NPackage name) := self.packageMap.find? name /-- Check if the module is local to any package in the workspace. -/ def isLocalModule (mod : Name) (self : Workspace) : Bool := self.packageMap.any fun _ pkg => pkg.isLocalModule mod /-- Check if the module is buildable by any package in the workspace. -/ def isBuildableModule (mod : Name) (self : Workspace) : Bool := self.packageMap.any fun _ pkg => pkg.isBuildableModule mod /-- Locate the named module in the workspace (if it is local to it). -/ def findModule? (mod : Name) (self : Workspace) : Option Module := self.packageArray.findSome? (·.findModule? mod) /-- Try to find a Lean library in the workspace with the given name. -/ def findLeanLib? (name : Name) (self : Workspace) : Option LeanLib := self.packageArray.findSome? fun pkg => pkg.findLeanLib? name /-- Try to find a Lean executable in the workspace with the given name. -/ def findLeanExe? (name : Name) (self : Workspace) : Option LeanExe := self.packageArray.findSome? fun pkg => pkg.findLeanExe? name /-- Try to find an external library in the workspace with the given name. -/ def findExternLib? (name : Name) (self : Workspace) : Option ExternLib := self.packageArray.findSome? fun pkg => pkg.findExternLib? name /-- Try to find a target configuration in the workspace with the given name. -/ def findTargetConfig? (name : Name) (self : Workspace) : Option ((pkg : Package) × TargetConfig pkg.name name) := self.packageArray.findSome? fun pkg => pkg.findTargetConfig? name <&> (⟨pkg, ·⟩) /-- Add a module facet to the workspace. -/ def addModuleFacetConfig (cfg : ModuleFacetConfig name) (self : Workspace) : Workspace := {self with moduleFacetConfigs := self.moduleFacetConfigs.insert name cfg} /-- Try to find a module facet configuration in the workspace with the given name. -/ @[inline] def findModuleFacetConfig? (name : Name) (self : Workspace) : Option (ModuleFacetConfig name) := self.moduleFacetConfigs.find? name /-- Add a package facet to the workspace. -/ def addPackageFacetConfig (cfg : PackageFacetConfig name) (self : Workspace) : Workspace := {self with packageFacetConfigs := self.packageFacetConfigs.insert name cfg} /-- Try to find a package facet configuration in the workspace with the given name. -/ @[inline] def findPackageFacetConfig? (name : Name) (self : Workspace) : Option (PackageFacetConfig name) := self.packageFacetConfigs.find? name /-- Add a library facet to the workspace. -/ def addLibraryFacetConfig (cfg : LibraryFacetConfig name) (self : Workspace) : Workspace := {self with libraryFacetConfigs := self.libraryFacetConfigs.insert cfg.name cfg} /-- Try to find a library facet configuration in the workspace with the given name. -/ @[inline] def findLibraryFacetConfig? (name : Name) (self : Workspace) : Option (LibraryFacetConfig name) := self.libraryFacetConfigs.find? name /-- The workspace's binary directories (which are added to `Path`). -/ def binPath (self : Workspace) : SearchPath := self.packageList.map (·.binDir) /-- The workspace's Lean library directories (which are added to `LEAN_PATH`). -/ def leanPath (self : Workspace) : SearchPath := self.packageList.map (·.leanLibDir) /-- The workspace's source directories (which are added to `LEAN_SRC_PATH`). -/ def leanSrcPath (self : Workspace) : SearchPath := Id.run do let mut path : SearchPath := {} for pkg in self.packageArray do for (_, config) in pkg.leanLibConfigs do path := pkg.srcDir / config.srcDir :: path return path /-- The workspace's shared library path (e.g., for `--load-dynlib`). This is added to the `sharedLibPathEnvVar` by `lake env`. -/ def sharedLibPath (self : Workspace) : SearchPath := self.packageList.map (·.nativeLibDir) /-- The detected `PATH` of the environment augmented with the workspace's `binDir` and Lean and Lake installations' `binDir`. -/ def augmentedPath (self : Workspace) : SearchPath := self.binPath ++ self.lakeEnv.path /-- The detected `LEAN_PATH` of the environment augmented with the workspace's `leanPath` and Lake's `libDir`. -/ def augmentedLeanPath (self : Workspace) : SearchPath := self.leanPath ++ self.lakeEnv.leanPath /-- The detected `LEAN_SRC_PATH` of the environment augmented with the workspace's `leanSrcPath` and Lake's `srcDir`. -/ def augmentedLeanSrcPath (self : Workspace) : SearchPath := self.leanSrcPath ++ self.lakeEnv.leanSrcPath /- The detected `sharedLibPathEnv` value of the environment augmented with the workspace's `libPath` and Lean installation's shared library directories. -/ def augmentedSharedLibPath (self : Workspace) : SearchPath := self.sharedLibPath ++ self.lakeEnv.sharedLibPath /-- The detected environment augmented with Lake's and the workspace's paths. These are the settings use by `lake env` / `Lake.env` to run executables. -/ def augmentedEnvVars (self : Workspace) : Array (String × Option String) := let vars := self.lakeEnv.installVars ++ #[ ("LEAN_PATH", some self.augmentedLeanPath.toString), ("LEAN_SRC_PATH", some self.augmentedLeanSrcPath.toString), ("PATH", some self.augmentedPath.toString) ] if Platform.isWindows then vars else vars.push (sharedLibPathEnvVar, some self.augmentedSharedLibPath.toString) /-- Remove all packages' build outputs (i.e., delete their build directories). -/ def clean (self : Workspace) : IO Unit := do self.packageMap.forM fun _ pkg => pkg.clean
96948a656b233dc5c39a701c061409a9d1f5a74c
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Lean/Elab/PatternVar.lean
3dabeae54c5b26e692b47ea4ed81482a25b650bd
[ "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
13,894
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.Match.MatchPatternAttr import Lean.Elab.Arg import Lean.Elab.MatchAltView namespace Lean.Elab.Term open Meta abbrev PatternVar := Syntax -- TODO: should be `Ident` /-! Patterns define new local variables. This module collect them and preprocess `_` occurring in patterns. Recall that an `_` may represent anonymous variables or inaccessible terms that are implied by typing constraints. Thus, we represent them with fresh named holes `?x`. After we elaborate the pattern, if the metavariable remains unassigned, we transform it into a regular pattern variable. Otherwise, it becomes an inaccessible term. Macros occurring in patterns are expanded before the `collectPatternVars` method is executed. The following kinds of Syntax are handled by this module - Constructor applications - Applications of functions tagged with the `[matchPattern]` attribute - Identifiers - Anonymous constructors - Structure instances - Inaccessible terms - Named patterns - Tuple literals - Type ascriptions - Literals: num, string and char -/ namespace CollectPatternVars /-- State for the pattern variable collector monad. -/ structure State where /-- Pattern variables found so far. -/ found : NameSet := {} /-- Pattern variables found so far as an array. It contains the order they were found. -/ vars : Array PatternVar := #[] deriving Inhabited abbrev M := StateRefT State TermElabM private def throwCtorExpected {α} : M α := throwError "invalid pattern, constructor or constant marked with '[matchPattern]' expected" private def throwInvalidPattern {α} : M α := throwError "invalid pattern" /-! An application in a pattern can be 1- A constructor application The elaborator assumes fields are accessible and inductive parameters are not accessible. 2- A regular application `(f ...)` where `f` is tagged with `[matchPattern]`. The elaborator assumes implicit arguments are not accessible and explicit ones are accessible. -/ structure Context where funId : Ident ctorVal? : Option ConstructorVal -- It is `some`, if constructor application explicit : Bool ellipsis : Bool paramDecls : Array (Name × BinderInfo) -- parameters names and binder information paramDeclIdx : Nat := 0 namedArgs : Array NamedArg args : List Arg newArgs : Array Term := #[] deriving Inhabited private def isDone (ctx : Context) : Bool := ctx.paramDeclIdx ≥ ctx.paramDecls.size private def finalize (ctx : Context) : M Syntax := do if ctx.namedArgs.isEmpty && ctx.args.isEmpty then let fStx ← `(@$(ctx.funId):ident) return Syntax.mkApp fStx ctx.newArgs else throwError "too many arguments" private def isNextArgAccessible (ctx : Context) : Bool := let i := ctx.paramDeclIdx match ctx.ctorVal? with | some ctorVal => i ≥ ctorVal.numParams -- For constructor applications only fields are accessible | none => if h : i < ctx.paramDecls.size then -- For `[matchPattern]` applications, only explicit parameters are accessible. let d := ctx.paramDecls.get ⟨i, h⟩ d.2.isExplicit else false private def getNextParam (ctx : Context) : (Name × BinderInfo) × Context := let i := ctx.paramDeclIdx let d := ctx.paramDecls[i]! (d, { ctx with paramDeclIdx := ctx.paramDeclIdx + 1 }) private def processVar (idStx : Syntax) : M Syntax := do unless idStx.isIdent do throwErrorAt idStx "identifier expected" let id := idStx.getId unless id.eraseMacroScopes.isAtomic do throwError "invalid pattern variable, must be atomic" if (← get).found.contains id then throwError "invalid pattern, variable '{id}' occurred more than once" modify fun s => { s with vars := s.vars.push idStx, found := s.found.insert id } return idStx private def samePatternsVariables (startingAt : Nat) (s₁ s₂ : State) : Bool := if h : s₁.vars.size = s₂.vars.size then Array.isEqvAux s₁.vars s₂.vars h (.==.) startingAt else false open TSyntax.Compat in partial def collect (stx : Syntax) : M Syntax := withRef stx <| withFreshMacroScope do let k := stx.getKind if k == identKind then processId stx else if k == ``Lean.Parser.Term.app then processCtorApp stx else if k == ``Lean.Parser.Term.anonymousCtor then let elems ← stx[1].getArgs.mapSepElemsM collect return stx.setArg 1 <| mkNullNode elems else if k == ``Lean.Parser.Term.dotIdent then return stx else if k == ``Lean.Parser.Term.hole then `(.( $stx )) else if k == ``Lean.Parser.Term.syntheticHole then `(.( $stx )) else if k == ``Lean.Parser.Term.paren then let arg := stx[1] if arg.isNone then return stx -- `()` else let t := arg[0] let s := arg[1] if s.isNone || s[0].getKind == ``Lean.Parser.Term.typeAscription then -- Ignore `s`, since it empty or it is a type ascription let t ← collect t let arg := arg.setArg 0 t return stx.setArg 1 arg else return stx else if k == ``Lean.Parser.Term.explicitUniv then processCtor stx[0] else if k == ``Lean.Parser.Term.namedPattern then /- Recall that ``` def namedPattern := check... >> trailing_parser "@" >> optional (atomic (ident >> ":")) >> termParser ``` TODO: pattern variable for equality proof -/ let id := stx[0] discard <| processVar id let h ← if stx[2].isNone then `(h) else pure stx[2][0] let pat := stx[3] let pat ← collect pat discard <| processVar h ``(_root_.namedPattern $id $pat $h) else if k == ``Lean.Parser.Term.binop then let lhs ← collect stx[2] let rhs ← collect stx[3] return stx.setArg 2 lhs |>.setArg 3 rhs else if k == ``Lean.Parser.Term.inaccessible then return stx else if k == strLitKind then return stx else if k == numLitKind then return stx else if k == scientificLitKind then return stx else if k == charLitKind then return stx else if k == ``Lean.Parser.Term.quotedName || k == ``Lean.Parser.Term.doubleQuotedName then return stx else if k == choiceKind then /- Remark: If there are `Term.structInst` alternatives, we keep only them. This is a hack to get rid of Set-like notation in patterns. Recall that in Mathlib `{a, b}` can be a set with two elements or the structure instance `{ a := a, b := b }`. Possible alternative solution: add a `pattern` category, or at least register the `Syntax` node kinds that are allowed in patterns. -/ let args := let args := stx.getArgs if args.any (·.isOfKind ``Parser.Term.structInst) then args.filter (·.isOfKind ``Parser.Term.structInst) else args let stateSaved ← get let arg0 ← collect args[0]! let stateNew ← get let mut argsNew := #[arg0] for arg in args[1:] do set stateSaved argsNew := argsNew.push (← collect arg) unless samePatternsVariables stateSaved.vars.size stateNew (← get) do throwError "invalid pattern, overloaded notation is only allowed when all alternative have the same set of pattern variables" set stateNew return mkNode choiceKind argsNew else match stx with | `({ $[$srcs?,* with]? $fields,* $[..%$ell?]? $[: $ty?]? }) => if let some srcs := srcs? then throwErrorAt (mkNullNode srcs) "invalid struct instance pattern, 'with' is not allowed in patterns" let fields ← fields.getElems.mapM fun | `(Parser.Term.structInstField| $lval:structInstLVal := $val) => do let newVal ← collect val `(Parser.Term.structInstField| $lval:structInstLVal := $newVal) | _ => throwInvalidPattern -- `structInstFieldAbbrev` should be expanded at this point `({ $[$srcs?,* with]? $fields,* $[..%$ell?]? $[: $ty?]? }) | _ => throwInvalidPattern where processCtorApp (stx : Syntax) : M Syntax := do let (f, namedArgs, args, ellipsis) ← expandApp stx if f.getKind == ``Parser.Term.dotIdent then let namedArgsNew ← namedArgs.mapM fun | { ref, name, val := Arg.stx arg } => withRef ref do `(Lean.Parser.Term.namedArgument| ($(mkIdentFrom ref name) := $(← collect arg))) | _ => unreachable! let mut argsNew ← args.mapM fun | Arg.stx arg => collect arg | _ => unreachable! if ellipsis then argsNew := argsNew.push (mkNode ``Parser.Term.ellipsis #[mkAtomFrom stx ".."]) return Syntax.mkApp f (namedArgsNew ++ argsNew) else processCtorAppCore f namedArgs args ellipsis processCtor (stx : Syntax) : M Syntax := do processCtorAppCore stx #[] #[] false /-- Check whether `stx` is a pattern variable or constructor-like (i.e., constructor or constant tagged with `[matchPattern]` attribute) -/ processId (stx : Syntax) : M Syntax := do match (← resolveId? stx "pattern") with | none => processVar stx | some f => match f with | Expr.const fName _ => match (← getEnv).find? fName with | some (ConstantInfo.ctorInfo _) => processCtor stx | some _ => if hasMatchPatternAttribute (← getEnv) fName then processCtor stx else processVar stx | none => throwCtorExpected | _ => processVar stx pushNewArg (accessible : Bool) (ctx : Context) (arg : Arg) : M Context := do match arg with | Arg.stx stx => let stx ← if accessible then collect stx else pure stx return { ctx with newArgs := ctx.newArgs.push stx } | _ => unreachable! processExplicitArg (accessible : Bool) (ctx : Context) : M Context := do match ctx.args with | [] => if ctx.ellipsis then pushNewArg accessible ctx (Arg.stx (← `(_))) else throwError "explicit parameter is missing, unused named arguments {ctx.namedArgs.map fun narg => narg.name}" | arg::args => pushNewArg accessible { ctx with args := args } arg processImplicitArg (accessible : Bool) (ctx : Context) : M Context := do if ctx.explicit then processExplicitArg accessible ctx else pushNewArg accessible ctx (Arg.stx (← `(_))) processCtorAppContext (ctx : Context) : M Syntax := do if isDone ctx then finalize ctx else let accessible := isNextArgAccessible ctx let (d, ctx) := getNextParam ctx match ctx.namedArgs.findIdx? fun namedArg => namedArg.name == d.1 with | some idx => let arg := ctx.namedArgs[idx]! let ctx := { ctx with namedArgs := ctx.namedArgs.eraseIdx idx } let ctx ← pushNewArg accessible ctx arg.val processCtorAppContext ctx | none => let ctx ← match d.2 with | BinderInfo.implicit => processImplicitArg accessible ctx | BinderInfo.strictImplicit => processImplicitArg accessible ctx | BinderInfo.instImplicit => processImplicitArg accessible ctx | _ => processExplicitArg accessible ctx processCtorAppContext ctx processCtorAppCore (f : Syntax) (namedArgs : Array NamedArg) (args : Array Arg) (ellipsis : Bool) : M Syntax := do let args := args.toList let (fId, explicit) ← match f with | `($fId:ident) => pure (fId, false) | `(@$fId:ident) => pure (fId, true) | _ => throwError "identifier expected" let some (Expr.const fName _) ← resolveId? fId "pattern" (withInfo := true) | throwCtorExpected let fInfo ← getConstInfo fName let paramDecls ← forallTelescopeReducing fInfo.type fun xs _ => xs.mapM fun x => do let d ← getFVarLocalDecl x return (d.userName, d.binderInfo) match fInfo with | ConstantInfo.ctorInfo val => processCtorAppContext { funId := fId, explicit := explicit, ctorVal? := val, paramDecls := paramDecls, namedArgs := namedArgs, args := args, ellipsis := ellipsis } | _ => if hasMatchPatternAttribute (← getEnv) fName then processCtorAppContext { funId := fId, explicit := explicit, ctorVal? := none, paramDecls := paramDecls, namedArgs := namedArgs, args := args, ellipsis := ellipsis } else throwCtorExpected def main (alt : MatchAltView) : M MatchAltView := do let patterns ← alt.patterns.mapM fun p => do trace[Elab.match] "collecting variables at pattern: {p}" collect p return { alt with patterns := patterns } end CollectPatternVars /-- Collect pattern variables occurring in the `match`-alternative object views. It also returns the updated views. -/ def collectPatternVars (alt : MatchAltView) : TermElabM (Array PatternVar × MatchAltView) := do let (alt, s) ← (CollectPatternVars.main alt).run {} return (s.vars, alt) /-- Return the pattern variables in the given pattern. Remark: this method is not used by the main `match` elaborator, but in the precheck hook and other macros (e.g., at `Do.lean`). -/ def getPatternVars (patternStx : Syntax) : TermElabM (Array PatternVar) := do let patternStx ← liftMacroM <| expandMacros patternStx let (_, s) ← (CollectPatternVars.collect patternStx).run {} return s.vars /-- Return the pattern variables occurring in the given patterns. This method is used in the `match` and `do` notation elaborators -/ def getPatternsVars (patterns : Array Syntax) : TermElabM (Array PatternVar) := do let collect : CollectPatternVars.M Unit := do for pattern in patterns do discard <| CollectPatternVars.collect (← liftMacroM <| expandMacros pattern) let (_, s) ← collect.run {} return s.vars def getPatternVarNames (pvars : Array PatternVar) : Array Name := pvars.map fun x => x.getId end Lean.Elab.Term
360fc1a3326025af1ebfb4e02666fd9fa6687d1a
618003631150032a5676f229d13a079ac875ff77
/src/category_theory/limits/shapes/finite_products.lean
7b29e557f98c385b3fb01a3b42f99dcd6bfcd85b
[ "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
1,619
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.limits.shapes.finite_limits universes v u open category_theory namespace category_theory.limits variables (C : Type u) [category.{v} C] class has_finite_products := (has_limits_of_shape : Π (J : Type v) [fintype J] [decidable_eq J], has_limits_of_shape.{v} (discrete J) C) class has_finite_coproducts := (has_colimits_of_shape : Π (J : Type v) [fintype J] [decidable_eq J], has_colimits_of_shape.{v} (discrete J) C) attribute [instance] has_finite_products.has_limits_of_shape has_finite_coproducts.has_colimits_of_shape @[priority 100] -- see Note [lower instance priority] instance has_finite_products_of_has_products [has_products.{v} C] : has_finite_products.{v} C := { has_limits_of_shape := λ J _, by apply_instance } @[priority 100] -- see Note [lower instance priority] instance has_finite_coproducts_of_has_coproducts [has_coproducts.{v} C] : has_finite_coproducts.{v} C := { has_colimits_of_shape := λ J _, by apply_instance } @[priority 100] -- see Note [lower instance priority] instance has_finite_products_of_has_finite_limits [has_finite_limits.{v} C] : has_finite_products.{v} C := { has_limits_of_shape := λ J _ _, by { resetI, apply_instance } } @[priority 100] -- see Note [lower instance priority] instance has_finite_coproducts_of_has_finite_colimits [has_finite_colimits.{v} C] : has_finite_coproducts.{v} C := { has_colimits_of_shape := λ J _ _, by { resetI, apply_instance } } end category_theory.limits
14894c1b17c4c90d0854bde7ea54a33bd526bb86
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/group_theory/free_product.lean
a20b6ee73241f4d2a915bf5cd2eee86b95afe91a
[ "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
33,850
lean
/- Copyright (c) 2021 David Wärn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Wärn, Joachim Breitner -/ import algebra.free_monoid import group_theory.congruence import group_theory.is_free_group import group_theory.subgroup.pointwise import data.list.chain import set_theory.cardinal.ordinal /-! # The free product of groups or monoids Given an `ι`-indexed family `M` of monoids, we define their free product (categorical coproduct) `free_product M`. When `ι` and all `M i` have decidable equality, the free product bijects with the type `word M` of reduced words. This bijection is constructed by defining an action of `free_product M` on `word M`. When `M i` are all groups, `free_product M` is also a group (and the coproduct in the category of groups). ## Main definitions - `free_product M`: the free product, defined as a quotient of a free monoid. - `free_product.of {i} : M i →* free_product M`. - `free_product.lift : (Π {i}, M i →* N) ≃ (free_product M →* N)`: the universal property. - `free_product.word M`: the type of reduced words. - `free_product.word.equiv M : free_product M ≃ word M`. - `free_product.neword M i j`: an inductive description of non-empty words with first letter from `M i` and last letter from `M j`, together with an API (`singleton`, `append`, `head`, `tail`, `to_word`, `prod`, `inv`). Used in the proof of the Ping-Pong-lemma. - `free_product.lift_injective_of_ping_pong`: The Ping-Pong-lemma, proving injectivity of the `lift`. See the documentation of that theorem for more information. ## Remarks There are many answers to the question "what is the free product of a family `M` of monoids?", and they are all equivalent but not obviously equivalent. We provide two answers. The first, almost tautological answer is given by `free_product M`, which is a quotient of the type of words in the alphabet `Σ i, M i`. It's straightforward to define and easy to prove its universal property. But this answer is not completely satisfactory, because it's difficult to tell when two elements `x y : free_product M` are distinct since `free_product M` is defined as a quotient. The second, maximally efficient answer is given by `word M`. An element of `word M` is a word in the alphabet `Σ i, M i`, where the letter `⟨i, 1⟩` doesn't occur and no adjacent letters share an index `i`. Since we only work with reduced words, there is no need for quotienting, and it is easy to tell when two elements are distinct. However it's not obvious that this is even a monoid! We prove that every element of `free_product M` can be represented by a unique reduced word, i.e. `free_product M` and `word M` are equivalent types. This means that `word M` can be given a monoid structure, and it lets us tell when two elements of `free_product M` are distinct. There is also a completely tautological, maximally inefficient answer given by `algebra.category.Mon.colimits`. Whereas `free_product M` at least ensures that (any instance of) associativity holds by reflexivity, in this answer associativity holds because of quotienting. Yet another answer, which is constructively more satisfying, could be obtained by showing that `free_product.rel` is confluent. ## References [van der Waerden, *Free products of groups*][MR25465] -/ open set variables {ι : Type*} (M : Π i : ι, Type*) [Π i, monoid (M i)] /-- A relation on the free monoid on alphabet `Σ i, M i`, relating `⟨i, 1⟩` with `1` and `⟨i, x⟩ * ⟨i, y⟩` with `⟨i, x * y⟩`. -/ inductive free_product.rel : free_monoid (Σ i, M i) → free_monoid (Σ i, M i) → Prop | of_one (i : ι) : free_product.rel (free_monoid.of ⟨i, 1⟩) 1 | of_mul {i : ι} (x y : M i) : free_product.rel (free_monoid.of ⟨i, x⟩ * free_monoid.of ⟨i, y⟩) (free_monoid.of ⟨i, x * y⟩) /-- The free product (categorical coproduct) of an indexed family of monoids. -/ @[derive [monoid, inhabited]] def free_product : Type* := (con_gen (free_product.rel M)).quotient namespace free_product /-- The type of reduced words. A reduced word cannot contain a letter `1`, and no two adjacent letters can come from the same summand. -/ @[ext] structure word := (to_list : list (Σ i, M i)) (ne_one : ∀ l ∈ to_list, sigma.snd l ≠ 1) (chain_ne : to_list.chain' (λ l l', sigma.fst l ≠ sigma.fst l')) variable {M} /-- The inclusion of a summand into the free product. -/ def of {i : ι} : M i →* free_product M := { to_fun := λ x, con.mk' _ (free_monoid.of $ sigma.mk i x), map_one' := (con.eq _).mpr (con_gen.rel.of _ _ (free_product.rel.of_one i)), map_mul' := λ x y, eq.symm $ (con.eq _).mpr (con_gen.rel.of _ _ (free_product.rel.of_mul x y)) } lemma of_apply {i} (m : M i) : of m = con.mk' _ (free_monoid.of $ sigma.mk i m) := rfl variables {N : Type*} [monoid N] /-- See note [partially-applied ext lemmas]. -/ @[ext] lemma ext_hom (f g : free_product M →* N) (h : ∀ i, f.comp (of : M i →* _) = g.comp of) : f = g := (monoid_hom.cancel_right con.mk'_surjective).mp $ free_monoid.hom_eq $ λ ⟨i, x⟩, by rw [monoid_hom.comp_apply, monoid_hom.comp_apply, ←of_apply, ←monoid_hom.comp_apply, ←monoid_hom.comp_apply, h] /-- A map out of the free product corresponds to a family of maps out of the summands. This is the universal property of the free product, charaterizing it as a categorical coproduct. -/ @[simps symm_apply] def lift : (Π i, M i →* N) ≃ (free_product M →* N) := { to_fun := λ fi, con.lift _ (free_monoid.lift $ λ p : Σ i, M i, fi p.fst p.snd) $ con.con_gen_le begin simp_rw [con.rel_eq_coe, con.ker_rel], rintros _ _ (i | ⟨i, x, y⟩), { change free_monoid.lift _ (free_monoid.of _) = free_monoid.lift _ 1, simp only [monoid_hom.map_one, free_monoid.lift_eval_of], }, { change free_monoid.lift _ (free_monoid.of _ * free_monoid.of _) = free_monoid.lift _ (free_monoid.of _), simp only [monoid_hom.map_mul, free_monoid.lift_eval_of], } end, inv_fun := λ f i, f.comp of, left_inv := by { intro fi, ext i x, rw [monoid_hom.comp_apply, of_apply, con.lift_mk', free_monoid.lift_eval_of], }, right_inv := by { intro f, ext i x, simp only [monoid_hom.comp_apply, of_apply, con.lift_mk', free_monoid.lift_eval_of], } } @[simp] lemma lift_of {N} [monoid N] (fi : Π i, M i →* N) {i} (m : M i) : lift fi (of m) = fi i m := by conv_rhs { rw [←lift.symm_apply_apply fi, lift_symm_apply, monoid_hom.comp_apply] } @[elab_as_eliminator] lemma induction_on {C : free_product M → Prop} (m : free_product M) (h_one : C 1) (h_of : ∀ (i) (m : M i), C (of m)) (h_mul : ∀ (x y), C x → C y → C (x * y)) : C m := begin let S : submonoid (free_product M) := submonoid.mk (set_of C) h_mul h_one, convert subtype.prop (lift (λ i, of.cod_restrict S (h_of i)) m), change monoid_hom.id _ m = S.subtype.comp _ m, congr, ext, simp [monoid_hom.cod_restrict], end lemma of_left_inverse [decidable_eq ι] (i : ι) : function.left_inverse (lift $ pi.mul_single i (monoid_hom.id (M i))) of := λ x, by simp only [lift_of, pi.mul_single_eq_same, monoid_hom.id_apply] lemma of_injective (i : ι) : function.injective ⇑(of : M i →* _) := by { classical, exact (of_left_inverse i).injective } lemma lift_mrange_le {N} [monoid N] (f : Π i, M i →* N) {s : submonoid N} (h : ∀ i, (f i).mrange ≤ s) : (lift f).mrange ≤ s := begin rintros _ ⟨x, rfl⟩, induction x using free_product.induction_on with i x x y hx hy, { exact s.one_mem, }, { simp only [lift_of, set_like.mem_coe], exact h i (set.mem_range_self x), }, { simp only [map_mul, set_like.mem_coe], exact s.mul_mem hx hy, }, end lemma mrange_eq_supr {N} [monoid N] (f : Π i, M i →* N) : (lift f).mrange = ⨆ i, (f i).mrange := begin apply le_antisymm (lift_mrange_le f (λ i, le_supr _ i)), apply supr_le _, rintros i _ ⟨x, rfl⟩, exact ⟨of x, by simp only [lift_of]⟩ end section group variables (G : ι → Type*) [Π i, group (G i)] instance : has_inv (free_product G) := { inv := mul_opposite.unop ∘ lift (λ i, (of : G i →* _).op.comp (mul_equiv.inv' (G i)).to_monoid_hom) } lemma inv_def (x : free_product G) : x⁻¹ = mul_opposite.unop (lift (λ i, (of : G i →* _).op.comp (mul_equiv.inv' (G i)).to_monoid_hom) x) := rfl instance : group (free_product G) := { mul_left_inv := begin intro m, rw inv_def, apply m.induction_on, { rw [monoid_hom.map_one, mul_opposite.unop_one, one_mul], }, { intros i m, change of m⁻¹ * of m = 1, rw [←of.map_mul, mul_left_inv, of.map_one], }, { intros x y hx hy, rw [monoid_hom.map_mul, mul_opposite.unop_mul, mul_assoc, ← mul_assoc _ x y, hx, one_mul, hy], }, end, ..free_product.has_inv G, ..free_product.monoid G } lemma lift_range_le {N} [group N] (f : Π i, G i →* N) {s : subgroup N} (h : ∀ i, (f i).range ≤ s) : (lift f).range ≤ s := begin rintros _ ⟨x, rfl⟩, induction x using free_product.induction_on with i x x y hx hy, { exact s.one_mem, }, { simp only [lift_of, set_like.mem_coe], exact h i (set.mem_range_self x), }, { simp only [map_mul, set_like.mem_coe], exact s.mul_mem hx hy, }, end lemma range_eq_supr {N} [group N] (f : Π i, G i →* N) : (lift f).range = ⨆ i, (f i).range := begin apply le_antisymm (lift_range_le _ f (λ i, le_supr _ i)), apply supr_le _, rintros i _ ⟨x, rfl⟩, exact ⟨of x, by simp only [lift_of]⟩ end end group namespace word /-- The empty reduced word. -/ def empty : word M := { to_list := [], ne_one := λ _, false.elim, chain_ne := list.chain'_nil } instance : inhabited (word M) := ⟨empty⟩ /-- A reduced word determines an element of the free product, given by multiplication. -/ def prod (w : word M) : free_product M := list.prod (w.to_list.map $ λ l, of l.snd) @[simp] lemma prod_empty : prod (empty : word M) = 1 := rfl /-- `fst_idx w` is `some i` if the first letter of `w` is `⟨i, m⟩` with `m : M i`. If `w` is empty then it's `none`. -/ def fst_idx (w : word M) : option ι := w.to_list.head'.map sigma.fst lemma fst_idx_ne_iff {w : word M} {i} : fst_idx w ≠ some i ↔ ∀ l ∈ w.to_list.head', i ≠ sigma.fst l := not_iff_not.mp $ by simp [fst_idx] variable (M) /-- Given an index `i : ι`, `pair M i` is the type of pairs `(head, tail)` where `head : M i` and `tail : word M`, subject to the constraint that first letter of `tail` can't be `⟨i, m⟩`. By prepending `head` to `tail`, one obtains a new word. We'll show that any word can be uniquely obtained in this way. -/ @[ext] structure pair (i : ι) := (head : M i) (tail : word M) (fst_idx_ne : fst_idx tail ≠ some i) instance (i : ι) : inhabited (pair M i) := ⟨⟨1, empty, by tauto⟩⟩ variable {M} variables [∀ i, decidable_eq (M i)] /-- Given a pair `(head, tail)`, we can form a word by prepending `head` to `tail`, except if `head` is `1 : M i` then we have to just return `word` since we need the result to be reduced. -/ def rcons {i} (p : pair M i) : word M := if h : p.head = 1 then p.tail else { to_list := ⟨i, p.head⟩ :: p.tail.to_list, ne_one := by { rintros l (rfl | hl), exact h, exact p.tail.ne_one l hl }, chain_ne := p.tail.chain_ne.cons' (fst_idx_ne_iff.mp p.fst_idx_ne) } /-- Given a word of the form `⟨l :: ls, h1, h2⟩`, we can form a word of the form `⟨ls, _, _⟩`, dropping the first letter. -/ private def mk_aux {l} (ls : list (Σ i, M i)) (h1 : ∀ l' ∈ l :: ls, sigma.snd l' ≠ 1) (h2 : (l :: ls).chain' _) : word M := ⟨ls, λ l' hl, h1 _ (list.mem_cons_of_mem _ hl), h2.tail⟩ lemma cons_eq_rcons {i} {m : M i} {ls h1 h2} : word.mk (⟨i, m⟩ :: ls) h1 h2 = rcons ⟨m, mk_aux ls h1 h2, fst_idx_ne_iff.mpr h2.rel_head'⟩ := by { rw [rcons, dif_neg], refl, exact h1 ⟨i, m⟩ (ls.mem_cons_self _) } @[simp] lemma prod_rcons {i} (p : pair M i) : prod (rcons p) = of p.head * prod p.tail := if hm : p.head = 1 then by rw [rcons, dif_pos hm, hm, monoid_hom.map_one, one_mul] else by rw [rcons, dif_neg hm, prod, list.map_cons, list.prod_cons, prod] lemma rcons_inj {i} : function.injective (rcons : pair M i → word M) := begin rintros ⟨m, w, h⟩ ⟨m', w', h'⟩ he, by_cases hm : m = 1; by_cases hm' : m' = 1, { simp only [rcons, dif_pos hm, dif_pos hm'] at he, cc, }, { exfalso, simp only [rcons, dif_pos hm, dif_neg hm'] at he, rw he at h, exact h rfl }, { exfalso, simp only [rcons, dif_pos hm', dif_neg hm] at he, rw ←he at h', exact h' rfl, }, { have : m = m' ∧ w.to_list = w'.to_list, { simpa only [rcons, dif_neg hm, dif_neg hm', true_and, eq_self_iff_true, subtype.mk_eq_mk, heq_iff_eq, ←subtype.ext_iff_val] using he }, rcases this with ⟨rfl, h⟩, congr, exact word.ext _ _ h, } end variable [decidable_eq ι] /-- Given `i : ι`, any reduced word can be decomposed into a pair `p` such that `w = rcons p`. -/ -- This definition is computable but not very nice to look at. Thankfully we don't have to inspect -- it, since `rcons` is known to be injective. private def equiv_pair_aux (i) : Π w : word M, { p : pair M i // rcons p = w } | w@⟨[], _, _⟩ := ⟨⟨1, w, by rintro ⟨⟩⟩, dif_pos rfl⟩ | w@⟨⟨j, m⟩ :: ls, h1, h2⟩ := if ij : i = j then { val := { head := ij.symm.rec m, tail := mk_aux ls h1 h2, fst_idx_ne := by cases ij; exact fst_idx_ne_iff.mpr h2.rel_head' }, property := by cases ij; exact cons_eq_rcons.symm } else ⟨⟨1, w, (option.some_injective _).ne (ne.symm ij)⟩, dif_pos rfl⟩ /-- The equivalence between words and pairs. Given a word, it decomposes it as a pair by removing the first letter if it comes from `M i`. Given a pair, it prepends the head to the tail. -/ def equiv_pair (i) : word M ≃ pair M i := { to_fun := λ w, (equiv_pair_aux i w).val, inv_fun := rcons, left_inv := λ w, (equiv_pair_aux i w).property, right_inv := λ p, rcons_inj (equiv_pair_aux i _).property } lemma equiv_pair_symm (i) (p : pair M i) : (equiv_pair i).symm p = rcons p := rfl lemma equiv_pair_eq_of_fst_idx_ne {i} {w : word M} (h : fst_idx w ≠ some i) : equiv_pair i w = ⟨1, w, h⟩ := (equiv_pair i).apply_eq_iff_eq_symm_apply.mpr $ eq.symm (dif_pos rfl) instance summand_action (i) : mul_action (M i) (word M) := { smul := λ m w, rcons { head := m * (equiv_pair i w).head, ..equiv_pair i w }, one_smul := λ w, by { simp_rw [one_mul], apply (equiv_pair i).symm_apply_eq.mpr, ext; refl }, mul_smul := λ m m' w, by simp only [mul_assoc, ←equiv_pair_symm, equiv.apply_symm_apply], } instance : mul_action (free_product M) (word M) := mul_action.of_End_hom (lift (λ i, mul_action.to_End_hom)) lemma of_smul_def (i) (w : word M) (m : M i) : of m • w = rcons { head := m * (equiv_pair i w).head, ..equiv_pair i w } := rfl lemma cons_eq_smul {i} {m : M i} {ls h1 h2} : word.mk (⟨i, m⟩ :: ls) h1 h2 = of m • mk_aux ls h1 h2 := by rw [cons_eq_rcons, of_smul_def, equiv_pair_eq_of_fst_idx_ne _]; simp only [mul_one] lemma smul_induction {C : word M → Prop} (h_empty : C empty) (h_smul : ∀ i (m : M i) w, C w → C (of m • w)) (w : word M) : C w := begin cases w with ls h1 h2, induction ls with l ls ih, { exact h_empty }, cases l with i m, rw cons_eq_smul, exact h_smul _ _ _ (ih _ _), end @[simp] lemma prod_smul (m) : ∀ w : word M, prod (m • w) = m * prod w := begin apply m.induction_on, { intro, rw [one_smul, one_mul] }, { intros, rw [of_smul_def, prod_rcons, of.map_mul, mul_assoc, ←prod_rcons, ←equiv_pair_symm, equiv.symm_apply_apply] }, { intros x y hx hy w, rw [mul_smul, hx, hy, mul_assoc] }, end /-- Each element of the free product corresponds to a unique reduced word. -/ def equiv : free_product M ≃ word M := { to_fun := λ m, m • empty, inv_fun := λ w, prod w, left_inv := λ m, by dsimp only; rw [prod_smul, prod_empty, mul_one], right_inv := begin apply smul_induction, { dsimp only, rw [prod_empty, one_smul], }, { dsimp only, intros i m w ih, rw [prod_smul, mul_smul, ih], }, end } instance : decidable_eq (word M) := function.injective.decidable_eq word.ext instance : decidable_eq (free_product M) := word.equiv.decidable_eq end word variable (M) /-- A `neword M i j` is a representation of a non-empty reduced words where the first letter comes from `M i` and the last letter comes from `M j`. It can be constructed from singletons and via concatentation, and thus provides a useful induction principle. -/ @[nolint has_nonempty_instance] inductive neword : ι → ι → Type (max u_1 u_2) | singleton : ∀ {i} (x : M i) (hne1 : x ≠ 1), neword i i | append : ∀ {i j k l} (w₁ : neword i j) (hne : j ≠ k) (w₂ : neword k l), neword i l variable {M} namespace neword open word /-- The list represented by a given `neword` -/ @[simp] def to_list : Π {i j} (w : neword M i j), list (Σ i, M i) | i _ (singleton x hne1) := [⟨i, x⟩] | _ _ (append w₁ hne w₂) := w₁.to_list ++ w₂.to_list lemma to_list_ne_nil {i j} (w : neword M i j) : w.to_list ≠ list.nil := by { induction w, { rintros ⟨rfl⟩ }, { apply list.append_ne_nil_of_ne_nil_left, assumption } } /-- The first letter of a `neword` -/ @[simp] def head : Π {i j} (w : neword M i j), M i | i _ (singleton x hne1) := x | _ _ (append w₁ hne w₂) := w₁.head /-- The last letter of a `neword` -/ @[simp] def last : Π {i j} (w : neword M i j), M j | i _ (singleton x hne1) := x | _ _ (append w₁ hne w₂) := w₂.last @[simp] lemma to_list_head' {i j} (w : neword M i j) : w.to_list.head' = option.some ⟨i, w.head⟩ := begin rw ← option.mem_def, induction w, { rw option.mem_def, reflexivity, }, { exact list.head'_append w_ih_w₁, }, end @[simp] lemma to_list_last' {i j} (w : neword M i j) : w.to_list.last' = option.some ⟨j, w.last⟩ := begin rw ← option.mem_def, induction w, { rw option.mem_def, reflexivity, }, { exact list.last'_append w_ih_w₂, }, end /-- The `word M` represented by a `neword M i j` -/ def to_word {i j} (w : neword M i j) : word M := { to_list := w.to_list, ne_one := begin induction w, { rintros ⟨k,x⟩ ⟨rfl, rfl⟩, exact w_hne1, exfalso, apply H, }, { intros l h, simp only [to_list, list.mem_append] at h, cases h, { exact w_ih_w₁ _ h, }, { exact w_ih_w₂ _ h, }, }, end, chain_ne := begin induction w, { exact list.chain'_singleton _, }, { apply list.chain'.append w_ih_w₁ w_ih_w₂, intros x hx y hy, rw [w_w₁.to_list_last', option.mem_some_iff] at hx, rw [w_w₂.to_list_head', option.mem_some_iff] at hy, subst hx, subst hy, exact w_hne, }, end, } /-- Every nonempty `word M` can be constructed as a `neword M i j` -/ lemma of_word (w : word M) (h : w ≠ empty) : ∃ i j (w' : neword M i j), w'.to_word = w := begin suffices : ∃ i j (w' : neword M i j), w'.to_word.to_list = w.to_list, { obtain ⟨i, j, w, h⟩ := this, refine ⟨i, j, w, _⟩, ext, rw h, }, cases w with l hnot1 hchain, induction l with x l hi, { contradiction, }, { rw list.forall_mem_cons at hnot1, cases l with y l, { refine ⟨x.1, x.1, singleton x.2 hnot1.1, _ ⟩, simp [to_word], }, { rw list.chain'_cons at hchain, specialize hi hnot1.2 hchain.2 (by rintros ⟨rfl⟩), obtain ⟨i, j, w', hw' : w'.to_list = y :: l⟩ := hi, obtain rfl : y = ⟨i, w'.head⟩, by simpa [hw'] using w'.to_list_head', refine ⟨x.1, j, append (singleton x.2 hnot1.1) hchain.1 w', _⟩, { simpa [to_word] using hw', } } } end /-- A non-empty reduced word determines an element of the free product, given by multiplication. -/ def prod {i j} (w : neword M i j) := w.to_word.prod @[simp] lemma singleton_head {i} (x : M i) (hne_one : x ≠ 1) : (singleton x hne_one).head = x := rfl @[simp] lemma singleton_last {i} (x : M i) (hne_one : x ≠ 1) : (singleton x hne_one).last = x := rfl @[simp] lemma prod_singleton {i} (x : M i) (hne_one : x ≠ 1) : (singleton x hne_one).prod = of x := by simp [to_word, prod, word.prod] @[simp] lemma append_head {i j k l} {w₁ : neword M i j} {hne : j ≠ k} {w₂ : neword M k l} : (append w₁ hne w₂).head = w₁.head := rfl @[simp] lemma append_last {i j k l} {w₁ : neword M i j} {hne : j ≠ k} {w₂ : neword M k l} : (append w₁ hne w₂).last = w₂.last := rfl @[simp] lemma append_prod {i j k l} {w₁ : neword M i j} {hne : j ≠ k} {w₂ : neword M k l} : (append w₁ hne w₂).prod = w₁.prod * w₂.prod := by simp [to_word, prod, word.prod] /-- One can replace the first letter in a non-empty reduced word by an element of the same group -/ def replace_head : Π {i j : ι} (x : M i) (hnotone : x ≠ 1) (w : neword M i j), neword M i j | _ _ x h (singleton _ _) := singleton x h | _ _ x h (append w₁ hne w₂) := append (replace_head x h w₁) hne w₂ @[simp] lemma replace_head_head {i j : ι} (x : M i) (hnotone : x ≠ 1) (w : neword M i j) : (replace_head x hnotone w).head = x := by { induction w, refl, exact w_ih_w₁ _ _, } /-- One can multiply an element from the left to a non-empty reduced word if it does not cancel with the first element in the word. -/ def mul_head {i j : ι} (w : neword M i j) (x : M i) (hnotone : x * w.head ≠ 1) : neword M i j := replace_head (x * w.head) hnotone w @[simp] lemma mul_head_head {i j : ι} (w : neword M i j) (x : M i) (hnotone : x * w.head ≠ 1) : (mul_head w x hnotone).head = x * w.head := by { induction w, refl, exact w_ih_w₁ _ _, } @[simp] lemma mul_head_prod {i j : ι} (w : neword M i j) (x : M i) (hnotone : x * w.head ≠ 1) : (mul_head w x hnotone).prod = of x * w.prod := begin unfold mul_head, induction w, { simp [mul_head, replace_head], }, { specialize w_ih_w₁ _ hnotone, clear w_ih_w₂, simp [replace_head, ← mul_assoc] at *, congr' 1, } end section group variables {G : ι → Type*} [Π i, group (G i)] /-- The inverse of a non-empty reduced word -/ def inv : Π {i j} (w : neword G i j), neword G j i | _ _ (singleton x h) := singleton x⁻¹ (mt inv_eq_one.mp h) | _ _ (append w₁ h w₂) := append w₂.inv h.symm w₁.inv @[simp] lemma inv_prod {i j} (w : neword G i j) : w.inv.prod = w.prod⁻¹ := by induction w; simp [inv, *] @[simp] lemma inv_head {i j} (w : neword G i j) : w.inv.head = w.last⁻¹ := by induction w; simp [inv, *] @[simp] lemma inv_last {i j} (w : neword G i j) : w.inv.last = w.head⁻¹ := by induction w; simp [inv, *] end group end neword section ping_pong_lemma open_locale pointwise open_locale cardinal variables [hnontriv : nontrivial ι] variables {G : Type*} [group G] variables {H : ι → Type*} [∀ i, group (H i)] variables (f : Π i, H i →* G) -- We need many groups or one group with many elements variables (hcard : 3 ≤ # ι ∨ ∃ i, 3 ≤ # (H i)) -- A group action on α, and the ping-pong sets variables {α : Type*} [mul_action G α] variables (X : ι → set α) variables (hXnonempty : ∀ i, (X i).nonempty) variables (hXdisj : pairwise (λ i j, disjoint (X i) (X j))) variables (hpp : pairwise (λ i j, ∀ h : H i, h ≠ 1 → f i h • X j ⊆ X i)) include hpp lemma lift_word_ping_pong {i j k} (w : neword H i j) (hk : j ≠ k) : lift f w.prod • X k ⊆ X i := begin rename [i → i', j → j', k → m, hk → hm], induction w with i x hne_one i j k l w₁ hne w₂ hIw₁ hIw₂ generalizing m; clear i' j', { simpa using hpp _ _ hm _ hne_one, }, { calc lift f (neword.append w₁ hne w₂).prod • X m = lift f w₁.prod • lift f w₂.prod • X m : by simp [mul_action.mul_smul] ... ⊆ lift f w₁.prod • X k : set_smul_subset_set_smul_iff.mpr (hIw₂ hm) ... ⊆ X i : hIw₁ hne }, end include X hXnonempty hXdisj lemma lift_word_prod_nontrivial_of_other_i {i j k} (w : neword H i j) (hhead : k ≠ i) (hlast : k ≠ j) : lift f w.prod ≠ 1 := begin intro heq1, have : X k ⊆ X i, by simpa [heq1] using lift_word_ping_pong f X hpp w hlast.symm, obtain ⟨x, hx⟩ := hXnonempty k, exact hXdisj k i hhead ⟨hx, this hx⟩, end include hnontriv lemma lift_word_prod_nontrivial_of_head_eq_last {i} (w : neword H i i) : lift f w.prod ≠ 1 := begin obtain ⟨k, hk⟩ := exists_ne i, exact lift_word_prod_nontrivial_of_other_i f X hXnonempty hXdisj hpp w hk hk, end lemma lift_word_prod_nontrivial_of_head_card {i j} (w : neword H i j) (hcard : 3 ≤ # (H i)) (hheadtail : i ≠ j) : lift f w.prod ≠ 1 := begin obtain ⟨h, hn1, hnh⟩ := cardinal.three_le hcard 1 (w.head⁻¹), have hnot1 : h * w.head ≠ 1, by { rw ← div_inv_eq_mul, exact div_ne_one_of_ne hnh }, let w' : neword H i i := neword.append (neword.mul_head w h hnot1) hheadtail.symm (neword.singleton h⁻¹ (inv_ne_one.mpr hn1)), have hw' : lift f w'.prod ≠ 1 := lift_word_prod_nontrivial_of_head_eq_last f X hXnonempty hXdisj hpp w', intros heq1, apply hw', simp [w', heq1] end include hcard lemma lift_word_prod_nontrivial_of_not_empty {i j} (w : neword H i j) : lift f w.prod ≠ 1 := begin classical, cases hcard, { obtain ⟨i, h1, h2⟩ := cardinal.three_le hcard i j, exact lift_word_prod_nontrivial_of_other_i f X hXnonempty hXdisj hpp w h1 h2, }, { cases hcard with k hcard, by_cases hh : i = k; by_cases hl : j = k, { subst hh, subst hl, exact lift_word_prod_nontrivial_of_head_eq_last f X hXnonempty hXdisj hpp w, }, { subst hh, change j ≠ i at hl, exact lift_word_prod_nontrivial_of_head_card f X hXnonempty hXdisj hpp w hcard hl.symm, }, { subst hl, change i ≠ j at hh, have : lift f w.inv.prod ≠ 1 := lift_word_prod_nontrivial_of_head_card f X hXnonempty hXdisj hpp w.inv hcard hh.symm, intros heq, apply this, simpa using heq, }, { change i ≠ k at hh, change j ≠ k at hl, obtain ⟨h, hn1, -⟩ := cardinal.three_le hcard 1 1, let w' : neword H k k := neword.append (neword.append (neword.singleton h hn1) hh.symm w) hl (neword.singleton h⁻¹ (inv_ne_one.mpr hn1)) , have hw' : lift f w'.prod ≠ 1 := lift_word_prod_nontrivial_of_head_eq_last f X hXnonempty hXdisj hpp w', intros heq1, apply hw', simp [w', heq1], }, } end lemma empty_of_word_prod_eq_one {w : word H} (h : lift f w.prod = 1) : w = word.empty := begin by_contradiction hnotempty, obtain ⟨i, j, w, rfl⟩ := neword.of_word w hnotempty, exact lift_word_prod_nontrivial_of_not_empty f hcard X hXnonempty hXdisj hpp w h, end /-- The Ping-Pong-Lemma. Given a group action of `G` on `X` so that the `H i` acts in a specific way on disjoint subsets `X i` we can prove that `lift f` is injective, and thus the image of `lift f` is isomorphic to the direct product of the `H i`. Often the Ping-Pong-Lemma is stated with regard to subgroups `H i` that generate the whole group; we generalize to arbitrary group homomorphisms `f i : H i →* G` and do not require the group to be generated by the images. Usually the Ping-Pong-Lemma requires that one group `H i` has at least three elements. This condition is only needed if `# ι = 2`, and we accept `3 ≤ # ι` as an alternative. -/ theorem lift_injective_of_ping_pong: function.injective (lift f) := begin classical, apply (injective_iff_map_eq_one (lift f)).mpr, rw (free_product.word.equiv : _ ≃ word H).forall_congr_left', { intros w Heq, dsimp [word.equiv] at *, { rw empty_of_word_prod_eq_one f hcard X hXnonempty hXdisj hpp Heq, reflexivity, }, }, end end ping_pong_lemma /-- The free product of free groups is itself a free group -/ @[simps] instance {ι : Type*} (G : ι → Type*) [∀ i, group (G i)] [hG : ∀ i, is_free_group (G i)] : is_free_group (free_product G) := { generators := Σ i, is_free_group.generators (G i), mul_equiv := monoid_hom.to_mul_equiv (free_group.lift (λ (x : Σ i, is_free_group.generators (G i)), free_product.of (is_free_group.of x.2 : G x.1))) (free_product.lift (λ (i : ι), (is_free_group.lift (λ (x : is_free_group.generators (G i)), free_group.of (⟨i, x⟩ : Σ i, is_free_group.generators (G i))) : G i →* (free_group (Σ i, is_free_group.generators (G i)))))) (by {ext, simp, }) (by {ext, simp, }) } /-- A free group is a free product of copies of the free_group over one generator. -/ -- NB: One might expect this theorem to be phrased with ℤ, but ℤ is an additive group, -- and using `multiplicative ℤ` runs into diamond issues. @[simps] def _root_.free_group_equiv_free_product {ι : Type u_1} : free_group ι ≃* free_product (λ (_ : ι), free_group unit) := begin refine monoid_hom.to_mul_equiv _ _ _ _, exact free_group.lift (λ i, @free_product.of ι _ _ i (free_group.of unit.star)), exact free_product.lift (λ i, free_group.lift (λ pstar, free_group.of i)), { ext i, refl, }, { ext i a, cases a, refl, }, end section ping_pong_lemma open_locale pointwise cardinal variables [nontrivial ι] variables {G : Type u_1} [group G] (a : ι → G) -- A group action on α, and the ping-pong sets variables {α : Type*} [mul_action G α] variables (X Y : ι → set α) variables (hXnonempty : ∀ i, (X i).nonempty) variables (hXdisj : pairwise (λ i j, disjoint (X i) (X j))) variables (hYdisj : pairwise (λ i j, disjoint (Y i) (Y j))) variables (hXYdisj : ∀ i j, disjoint (X i) (Y j)) variables (hX : ∀ i, a i • (Y i)ᶜ ⊆ X i) variables (hY : ∀ i, a⁻¹ i • (X i)ᶜ ⊆ Y i) include hXnonempty hXdisj hYdisj hXYdisj hX hY /-- The Ping-Pong-Lemma. Given a group action of `G` on `X` so that the generators of the free groups act in specific ways on disjoint subsets `X i` and `Y i` we can prove that `lift f` is injective, and thus the image of `lift f` is isomorphic to the free group. Often the Ping-Pong-Lemma is stated with regard to group elements that generate the whole group; we generalize to arbitrary group homomorphisms from the free group to `G` and do not require the group to be generated by the elements. -/ theorem _root_.free_group.injective_lift_of_ping_pong : function.injective (free_group.lift a) := begin -- Step one: express the free group lift via the free product lift have : free_group.lift a = (free_product.lift (λ i, free_group.lift (λ _, a i))).comp (((@free_group_equiv_free_product ι)).to_monoid_hom), { ext i, simp, }, rw this, clear this, refine function.injective.comp _ (mul_equiv.injective _), -- Step two: Invoke the ping-pong lemma for free products show function.injective (lift (λ (i : ι), free_group.lift (λ _, a i))), -- Prepare to instantiate lift_injective_of_ping_pong let H : ι → Type _ := λ i, free_group unit, let f : Π i, H i →* G := λ i, free_group.lift (λ _, a i), let X' : ι → set α := λ i, X i ∪ Y i, apply lift_injective_of_ping_pong f _ X', show _ ∨ ∃ i, 3 ≤ # (H i), { inhabit ι, right, use arbitrary ι, simp only [H], rw [free_group.free_group_unit_equiv_int.cardinal_eq, cardinal.mk_denumerable], apply le_of_lt, simp }, show ∀ i, (X' i).nonempty, { exact (λ i, set.nonempty.inl (hXnonempty i)), }, show pairwise (λ i j, disjoint (X' i) (X' j)), { intros i j hij, simp only [X'], apply disjoint.union_left; apply disjoint.union_right, { exact hXdisj i j hij, }, { exact hXYdisj i j, }, { exact (hXYdisj j i).symm, }, { exact hYdisj i j hij, }, }, show pairwise (λ i j, ∀ h : H i, h ≠ 1 → f i h • X' j ⊆ X' i), { rintros i j hij, -- use free_group unit ≃ ℤ refine free_group.free_group_unit_equiv_int.forall_congr_left'.mpr _, intros n hne1, change free_group.lift (λ _, a i) (free_group.of () ^ n) • X' j ⊆ X' i, simp only [map_zpow, free_group.lift.of], change a i ^ n • X' j ⊆ X' i, have hnne0 : n ≠ 0, { rintro rfl, apply hne1, simpa, }, clear hne1, simp only [X'], -- Positive and negative powers separately cases (lt_or_gt_of_ne hnne0).swap with hlt hgt, { have h1n : 1 ≤ n := hlt, calc a i ^ n • X' j ⊆ a i ^ n • (Y i)ᶜ : smul_set_mono ((hXYdisj j i).union_left $ hYdisj j i hij.symm).subset_compl_right ... ⊆ X i : begin refine int.le_induction _ _ _ h1n, { rw zpow_one, exact hX i, }, { intros n hle hi, calc (a i ^ (n + 1)) • (Y i)ᶜ = (a i ^ n * a i) • (Y i)ᶜ : by rw [zpow_add, zpow_one] ... = a i ^ n • (a i • (Y i)ᶜ) : mul_action.mul_smul _ _ _ ... ⊆ a i ^ n • X i : smul_set_mono $ hX i ... ⊆ a i ^ n • (Y i)ᶜ : smul_set_mono (hXYdisj i i).subset_compl_right ... ⊆ X i : hi, }, end ... ⊆ X' i : set.subset_union_left _ _, }, { have h1n : n ≤ -1, { apply int.le_of_lt_add_one, simpa using hgt, }, calc a i ^ n • X' j ⊆ a i ^ n • (X i)ᶜ : smul_set_mono ((hXdisj j i hij.symm).union_left (hXYdisj i j).symm).subset_compl_right ... ⊆ Y i : begin refine int.le_induction_down _ _ _ h1n, { rw [zpow_neg, zpow_one], exact hY i, }, { intros n hle hi, calc (a i ^ (n - 1)) • (X i)ᶜ = (a i ^ n * (a i)⁻¹) • (X i)ᶜ : by rw [zpow_sub, zpow_one] ... = a i ^ n • ((a i)⁻¹ • (X i)ᶜ) : mul_action.mul_smul _ _ _ ... ⊆ a i ^ n • Y i : smul_set_mono $ hY i ... ⊆ a i ^ n • (X i)ᶜ : smul_set_mono (hXYdisj i i).symm.subset_compl_right ... ⊆ Y i : hi, }, end ... ⊆ X' i : set.subset_union_right _ _, }, }, end end ping_pong_lemma end free_product
8f6500f4a204bb2bce9fb057c6ff392e416dc042
947b78d97130d56365ae2ec264df196ce769371a
/src/Lean/Elab/Import.lean
f30c0fc9728753859edbcc7d966a3d2c34706be8
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,970
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Parser.Module namespace Lean namespace Elab def headerToImports (header : Syntax) : List Import := let header := header.asNode; let imports := if (header.getArg 0).isNone then [{ module := `Init : Import }] else []; imports ++ (header.getArg 1).getArgs.toList.map (fun stx => -- `stx` is of the form `(Module.import "import" "runtime"? id) let runtime := !(stx.getArg 1).isNone; let id := stx.getIdAt 2; { module := id, runtimeOnly := runtime }) def processHeader (header : Syntax) (messages : MessageLog) (inputCtx : Parser.InputContext) (trustLevel : UInt32 := 0) : IO (Environment × MessageLog) := catch (do env ← importModules (headerToImports header) trustLevel; pure (env, messages)) (fun e => do env ← mkEmptyEnvironment; let spos := header.getPos.getD 0; let pos := inputCtx.fileMap.toPosition spos; pure (env, messages.add { fileName := inputCtx.fileName, data := toString e, pos := pos })) def parseImports (input : String) (fileName : Option String := none) : IO (List Import × Position × MessageLog) := do env ← mkEmptyEnvironment; let fileName := fileName.getD "<input>"; let inputCtx := Parser.mkInputContext input fileName; match Parser.parseHeader env inputCtx with | (header, parserState, messages) => do pure (headerToImports header, inputCtx.fileMap.toPosition parserState.pos, messages) @[export lean_parse_imports] def parseImportsExport (input : String) (fileName : Option String) : IO (List Import × Position × List Message) := do (imports, pos, log) ← parseImports input fileName; pure (imports, pos, log.toList) @[export lean_print_deps] def printDeps (deps : List Import) : IO Unit := deps.forM $ fun dep => do fname ← findOLean dep.module; IO.println fname end Elab end Lean
bea81113fc6447e168825dc23be3722f810c1a46
9028d228ac200bbefe3a711342514dd4e4458bff
/src/linear_algebra/exterior_algebra.lean
351d9230a7e6f97400e2cdbfa3ef3b2e86817824
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,547
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhangir Azerbayev, Adam Topaz, Eric Wieser. -/ import algebra.ring_quot import linear_algebra.tensor_algebra import group_theory.perm.sign /-! # Exterior Algebras We construct the exterior algebra of a semimodule `M` over a commutative semiring `R`. ## Notation The exterior algebra of the `R`-semimodule `M` is denoted as `exterior_algebra R M`. It is endowed with the structure of an `R`-algebra. Given a linear morphism `f : M → A` from a semimodule `M` to another `R`-algebra `A`, such that `cond : ∀ m : M, f m * f m = 0`, there is a (unique) lift of `f` to an `R`-algebra morphism, which is denoted `exterior_algebra.lift R f cond`. The canonical linear map `M → exterior_algebra R M` is denoted `exterior_algebra.ι R`. ## Theorems The main theorems proved ensure that `exterior_algebra R M` satisfies the universal property of the exterior algebra. 1. `ι_comp_lift` is fact that the composition of `ι R` with `lift R f cond` agrees with `f`. 2. `lift_unique` ensures the uniqueness of `lift R f cond` with respect to 1. ## Implementation details The exterior algebra of `M` is constructed as a quotient of the tensor algebra, as follows. 1. We define a relation `exterior_algebra.rel R M` on `tensor_algebra R M`. This is the smallest relation which identifies squares of elements of `M` with `0`. 2. The exterior algebra is the quotient of the tensor algebra by this relation. -/ variables (R : Type*) [comm_semiring R] variables (M : Type*) [add_comm_monoid M] [semimodule R M] namespace exterior_algebra open tensor_algebra /-- `rel` relates each `ι m * ι m`, for `m : M`, with `0`. The exterior algebra of `M` is defined as the quotient modulo this relation. -/ inductive rel : tensor_algebra R M → tensor_algebra R M → Prop | of (m : M) : rel ((ι R m) * (ι R m)) 0 end exterior_algebra /-- The exterior algebra of an `R`-semimodule `M`. -/ @[derive [inhabited, semiring, algebra R]] def exterior_algebra := ring_quot (exterior_algebra.rel R M) namespace exterior_algebra variables {M} /-- The canonical linear map `M →ₗ[R] exterior_algebra R M`. -/ def ι : M →ₗ[R] exterior_algebra R M := (ring_quot.mk_alg_hom R _).to_linear_map.comp (tensor_algebra.ι R) variables {R} /-- As well as being linear, `ι m` squares to zero -/ @[simp] theorem ι_square_zero (m : M) : (ι R m) * (ι R m) = 0 := begin dsimp [ι], rw [←alg_hom.map_mul, ←alg_hom.map_zero _], exact ring_quot.mk_alg_hom_rel R (rel.of m), end variables (R) {A : Type*} [semiring A] [algebra R A] /-- Given a linear map `f : M →ₗ[R] A` into an `R`-algebra `A`, which satisfies the condition: `cond : ∀ m : M, f m * f m = 0`, this is the canonical lift of `f` to a morphism of `R`-algebras from `exterior_algebra R M` to `A`. -/ def lift (f : M →ₗ[R] A) (cond : ∀ m, f m * f m = 0) : exterior_algebra R M →ₐ[R] A := ring_quot.lift_alg_hom R (tensor_algebra.lift R f) (λ x y h, by { induction h, rw [alg_hom.map_zero, alg_hom.map_mul, tensor_algebra.lift_ι_apply, cond] }) @[simp] theorem ι_comp_lift (f : M →ₗ[R] A) (cond : ∀ m, f m * f m = 0) : (lift R f cond).to_linear_map.comp (ι R) = f := by { ext, simp [lift, ι] } @[simp] theorem lift_ι_apply (f : M →ₗ[R] A) (cond : ∀ m, f m * f m = 0) (x) : lift R f cond (ι R x) = f x := by { dsimp [lift, ι], rw tensor_algebra.lift_ι_apply } @[simp] theorem lift_unique (f : M →ₗ[R] A) (cond : ∀ m, f m * f m = 0) (g : exterior_algebra R M →ₐ[R] A) : g.to_linear_map.comp (ι R) = f ↔ g = lift R f cond := begin refine ⟨_, λ hyp, by rw [hyp, ι_comp_lift]⟩, rintro rfl, ext, simp [lift], refl, end attribute [irreducible] exterior_algebra ι lift variables {R M} @[simp] theorem comp_ι_square_zero (g : exterior_algebra R M →ₐ[R] A) (m : M) : g (ι R m) * g (ι R m) = 0 := by rw [←alg_hom.map_mul, ι_square_zero, alg_hom.map_zero] @[simp] theorem lift_comp_ι (g : exterior_algebra R M →ₐ[R] A) : lift R (g.to_linear_map.comp (ι R)) (comp_ι_square_zero _) = g := by { symmetry, rw ←lift_unique, } @[ext] theorem hom_ext {f g : exterior_algebra R M →ₐ[R] A} : f.to_linear_map.comp (ι R) = g.to_linear_map.comp (ι R) → f = g := begin intro hyp, let h := g.to_linear_map.comp (ι R), have : g = lift R h (comp_ι_square_zero _), by rw ←lift_unique, rw [this, ←lift_unique, hyp], end end exterior_algebra
3fa4b5a442a770b53a3cdcc4dad9f94cd2e147b2
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Elab/Deriving/Repr.lean
3e15d5e996cf3702116d48324e2dad12c13df923
[ "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
5,130
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.Meta.Transform import Lean.Meta.Inductive import Lean.Elab.Deriving.Basic import Lean.Elab.Deriving.Util namespace Lean.Elab.Deriving.Repr open Lean.Parser.Term open Meta open Std def mkReprHeader (ctx : Context) (indVal : InductiveVal) : TermElabM Header := do let prec ← `(prec) let header ← mkHeader ctx `Repr 1 indVal return { header with binders := header.binders.push (← `(explicitBinderF| (prec : Nat))) } def mkBodyForStruct (ctx : Context) (header : Header) (indVal : InductiveVal) : TermElabM Syntax := do let ctorVal ← getConstInfoCtor indVal.ctors.head! let fieldNames ← getStructureFields (← getEnv) indVal.name let numParams := indVal.numParams let target := mkIdent header.targetNames[0] forallTelescopeReducing ctorVal.type fun xs _ => do let mut fields : Syntax ← `(Format.nil) let mut first := true if xs.size != numParams + fieldNames.size then throwError "'deriving Repr' failed, unexpected number of fields in structure" for i in [:fieldNames.size] do let fieldName := fieldNames[i] let fieldNameLit := Syntax.mkStrLit (toString fieldName) let x := xs[numParams + i] if first then first := false else fields ← `($fields ++ "," ++ Format.line) if (← isType x <||> isProof x) then fields ← `($fields ++ $fieldNameLit ++ " := " ++ "_") else fields ← `($fields ++ $fieldNameLit ++ " := " ++ repr ($target.$(mkIdent fieldName):ident)) `(Format.bracket "{ " $fields:term " }") def mkBodyForInduct (ctx : Context) (header : Header) (indVal : InductiveVal) (auxFunName : Name) : TermElabM Syntax := do let discrs ← mkDiscrs header indVal let alts ← mkAlts `(match $[$discrs],* with $alts:matchAlt*) where mkAlts : TermElabM (Array Syntax) := do let mut alts := #[] for ctorName in indVal.ctors do let ctorInfo ← getConstInfoCtor ctorName let alt ← forallTelescopeReducing ctorInfo.type fun xs type => do let mut patterns := #[] -- add `_` pattern for indices for i in [:indVal.numIndices] do patterns := patterns.push (← `(_)) let mut ctorArgs := #[] let mut rhs := Syntax.mkStrLit (toString ctorInfo.name) rhs ← `(Format.text $rhs) -- add `_` for inductive parameters, they are inaccessible for i in [:indVal.numParams] do ctorArgs := ctorArgs.push (← `(_)) for i in [:ctorInfo.numFields] do let x := xs[indVal.numParams + i] let a := mkIdent (← mkFreshUserName `a) ctorArgs := ctorArgs.push a if (← inferType x).isAppOf indVal.name then rhs ← `($rhs ++ Format.line ++ $(mkIdent auxFunName):ident $a:ident max_prec) else rhs ← `($rhs ++ Format.line ++ reprArg $a) patterns := patterns.push (← `(@$(mkIdent ctorName):ident $ctorArgs:term*)) `(matchAltExpr| | $[$patterns:term],* => Repr.addAppParen (Format.group (Format.nest (if prec >= max_prec then 1 else 2) ($rhs:term))) prec) alts := alts.push alt return alts def mkBody (ctx : Context) (header : Header) (indVal : InductiveVal) (auxFunName : Name) : TermElabM Syntax := do if isStructure (← getEnv) indVal.name then mkBodyForStruct ctx header indVal else mkBodyForInduct ctx header indVal auxFunName def mkAuxFunction (ctx : Context) (i : Nat) : TermElabM Syntax := do let auxFunName ← ctx.auxFunNames[i] let indVal ← ctx.typeInfos[i] let header ← mkReprHeader ctx indVal let mut body ← mkBody ctx header indVal auxFunName if ctx.usePartial then let letDecls ← mkLocalInstanceLetDecls ctx `Repr header.argNames body ← mkLet letDecls body let binders := header.binders if ctx.usePartial then `(private partial def $(mkIdent auxFunName):ident $binders:explicitBinder* : Format := $body:term) else `(private def $(mkIdent auxFunName):ident $binders:explicitBinder* : Format := $body:term) def mkMutualBlock (ctx : Context) : TermElabM Syntax := do let mut auxDefs := #[] for i in [:ctx.typeInfos.size] do auxDefs := auxDefs.push (← mkAuxFunction ctx i) `(mutual $auxDefs:command* end) private def mkReprInstanceCmds (declNames : Array Name) : TermElabM (Array Syntax) := do let ctx ← mkContext "repr" declNames[0] let cmds := #[← mkMutualBlock ctx] ++ (← mkInstanceCmds ctx `Repr declNames) trace[Elab.Deriving.repr] "\n{cmds}" return cmds open Command def mkReprInstanceHandler (declNames : Array Name) : CommandElabM Bool := do if (← declNames.allM isInductive) && declNames.size > 0 then let cmds ← liftTermElabM none <| mkReprInstanceCmds declNames cmds.forM elabCommand return true else return false builtin_initialize registerBuiltinDerivingHandler `Repr mkReprInstanceHandler registerTraceClass `Elab.Deriving.repr end Lean.Elab.Deriving.Repr
55e8fbcd4f2fd1b8895f89aebf3cd5a5649fdc2b
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/field_theory/mv_polynomial.lean
7a34d1b8629116cc4726162cfa9b34fc89d7a835
[ "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
9,571
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 Multivariate functions of the form `α^n → α` are isomorphic to multivariate polynomials in `n` variables. -/ import linear_algebra.finsupp_vector_space import field_theory.finite noncomputable theory open_locale classical open set linear_map submodule open_locale big_operators namespace mv_polynomial universes u v variables {σ : Type u} {α : Type v} section variables (σ α) [field α] (m : ℕ) def restrict_total_degree : submodule α (mv_polynomial σ α) := finsupp.supported _ _ {n | n.sum (λn e, e) ≤ m } lemma mem_restrict_total_degree (p : mv_polynomial σ α) : p ∈ restrict_total_degree σ α m ↔ p.total_degree ≤ m := begin rw [total_degree, finset.sup_le_iff], refl end end section variables (σ α) def restrict_degree (m : ℕ) [field α] : submodule α (mv_polynomial σ α) := finsupp.supported _ _ {n | ∀i, n i ≤ m } end lemma mem_restrict_degree [field α] (p : mv_polynomial σ α) (n : ℕ) : p ∈ restrict_degree σ α n ↔ (∀s ∈ p.support, ∀i, (s : σ →₀ ℕ) i ≤ n) := begin rw [restrict_degree, finsupp.mem_supported], refl end lemma mem_restrict_degree_iff_sup [field α] (p : mv_polynomial σ α) (n : ℕ) : p ∈ restrict_degree σ α n ↔ ∀i, p.degrees.count i ≤ n := begin simp only [mem_restrict_degree, degrees, multiset.count_sup, finsupp.count_to_multiset, finset.sup_le_iff], exact ⟨assume h n s hs, h s hs n, assume h s hs n, h n s hs⟩ end lemma map_range_eq_map {β : Type*} [comm_ring α] [comm_ring β] (p : mv_polynomial σ α) (f : α → β) [is_semiring_hom f]: finsupp.map_range f (is_semiring_hom.map_zero f) p = p.map f := begin rw [← finsupp.sum_single p, finsupp.sum], -- It's not great that we need to use an `erw` here, -- but hopefully it will become smoother when we move entirely away from `is_semiring_hom`. erw [finsupp.map_range_finset_sum (add_monoid_hom.of f)], rw [← p.support.sum_hom (map f)], { refine finset.sum_congr rfl (assume n _, _), rw [finsupp.map_range_single, ← monomial, ← monomial, map_monomial, add_monoid_hom.coe_of], }, apply_instance end section variables (σ α) lemma is_basis_monomials [field α] : is_basis α ((λs, (monomial s 1 : mv_polynomial σ α))) := suffices is_basis α (λ (sa : Σ _, unit), (monomial sa.1 1 : mv_polynomial σ α)), begin apply is_basis.comp this (λ (s : σ →₀ ℕ), ⟨s, punit.star⟩), split, { intros x y hxy, simpa using hxy }, { intros x, rcases x with ⟨x₁, x₂⟩, use x₁, rw punit_eq punit.star x₂ } end, begin apply finsupp.is_basis_single (λ _ _, (1 : α)), intro _, apply is_basis_singleton_one, end end end mv_polynomial namespace mv_polynomial universe u variables (σ : Type u) (α : Type u) [field α] open_locale classical lemma dim_mv_polynomial : vector_space.dim α (mv_polynomial σ α) = cardinal.mk (σ →₀ ℕ) := by rw [← cardinal.lift_inj, ← (is_basis_monomials σ α).mk_eq_dim] end mv_polynomial namespace mv_polynomial variables {α : Type*} {σ : Type*} variables [field α] [fintype α] [fintype σ] def indicator (a : σ → α) : mv_polynomial σ α := ∏ n, (1 - (X n - C (a n))^(fintype.card α - 1)) lemma eval_indicator_apply_eq_one (a : σ → α) : eval a (indicator a) = 1 := have 0 < fintype.card α - 1, begin rw [← finite_field.card_units, fintype.card_pos_iff], exact ⟨1⟩ end, by simp only [indicator, (finset.univ.prod_hom (eval a)).symm, eval_sub, is_ring_hom.map_one (eval a), is_semiring_hom.map_pow (eval a), eval_X, eval_C, sub_self, zero_pow this, sub_zero, finset.prod_const_one] lemma eval_indicator_apply_eq_zero (a b : σ → α) (h : a ≠ b) : eval a (indicator b) = 0 := have ∃i, a i ≠ b i, by rwa [(≠), function.funext_iff, not_forall] at h, begin rcases this with ⟨i, hi⟩, simp only [indicator, (finset.univ.prod_hom (eval a)).symm, eval_sub, is_ring_hom.map_one (eval a), is_semiring_hom.map_pow (eval a), eval_X, eval_C, sub_self, finset.prod_eq_zero_iff], refine ⟨i, finset.mem_univ _, _⟩, rw [finite_field.pow_card_sub_one_eq_one, sub_self], rwa [(≠), sub_eq_zero], end lemma degrees_indicator (c : σ → α) : degrees (indicator c) ≤ ∑ s : σ, (fintype.card α - 1) •ℕ {s} := begin rw [indicator], refine le_trans (degrees_prod _ _) (finset.sum_le_sum $ assume s hs, _), refine le_trans (degrees_sub _ _) _, rw [degrees_one, ← bot_eq_zero, bot_sup_eq], refine le_trans (degrees_pow _ _) (nsmul_le_nsmul_of_le_right _ _), refine le_trans (degrees_sub _ _) _, rw [degrees_C, ← bot_eq_zero, sup_bot_eq], exact degrees_X _ end lemma indicator_mem_restrict_degree (c : σ → α) : indicator c ∈ restrict_degree σ α (fintype.card α - 1) := begin rw [mem_restrict_degree_iff_sup, indicator], assume n, refine le_trans (multiset.count_le_of_le _ $ degrees_indicator _) (le_of_eq _), rw [← finset.univ.sum_hom (multiset.count n)], simp only [is_add_monoid_hom.map_nsmul (multiset.count n), multiset.singleton_eq_singleton, nsmul_eq_mul, nat.cast_id], transitivity, refine finset.sum_eq_single n _ _, { assume b hb ne, rw [multiset.count_cons_of_ne ne.symm, multiset.count_zero, mul_zero] }, { assume h, exact (h $ finset.mem_univ _).elim }, { rw [multiset.count_cons_self, multiset.count_zero, mul_one] } end section variables (α σ) def evalₗ : mv_polynomial σ α →ₗ[α] (σ → α) → α := ⟨ λp e, p.eval e, assume p q, funext $ assume e, eval_add, assume a p, funext $ assume e, by rw [smul_eq_C_mul, eval_mul, eval_C]; refl ⟩ end section lemma evalₗ_apply (p : mv_polynomial σ α) (e : σ → α) : evalₗ α σ p e = p.eval e := rfl end lemma map_restrict_dom_evalₗ : (restrict_degree σ α (fintype.card α - 1)).map (evalₗ α σ) = ⊤ := begin refine top_unique (submodule.le_def'.2 $ assume e _, mem_map.2 _), refine ⟨∑ n : σ → α, e n • indicator n, _, _⟩, { exact sum_mem _ (assume c _, smul_mem _ _ (indicator_mem_restrict_degree _)) }, { ext n, simp only [linear_map.map_sum, @finset.sum_apply (σ → α) (λ_, α) _ _ _ _ _, pi.smul_apply, linear_map.map_smul], simp only [evalₗ_apply], transitivity, refine finset.sum_eq_single n _ _, { assume b _ h, rw [eval_indicator_apply_eq_zero _ _ h.symm, smul_zero] }, { assume h, exact (h $ finset.mem_univ n).elim }, { rw [eval_indicator_apply_eq_one, smul_eq_mul, mul_one] } } end end mv_polynomial namespace mv_polynomial universe u variables (σ : Type u) (α : Type u) [fintype σ] [field α] [fintype α] @[derive [add_comm_group, vector_space α, inhabited]] def R : Type u := restrict_degree σ α (fintype.card α - 1) noncomputable instance decidable_restrict_degree (m : ℕ) : decidable_pred (λn, n ∈ {n : σ →₀ ℕ | ∀i, n i ≤ m }) := by simp only [set.mem_set_of_eq]; apply_instance lemma dim_R : vector_space.dim α (R σ α) = fintype.card (σ → α) := calc vector_space.dim α (R σ α) = vector_space.dim α (↥{s : σ →₀ ℕ | ∀ (n : σ), s n ≤ fintype.card α - 1} →₀ α) : linear_equiv.dim_eq (finsupp.supported_equiv_finsupp {s : σ →₀ ℕ | ∀n:σ, s n ≤ fintype.card α - 1 }) ... = cardinal.mk {s : σ →₀ ℕ | ∀ (n : σ), s n ≤ fintype.card α - 1} : by rw [finsupp.dim_eq, dim_of_field, mul_one] ... = cardinal.mk {s : σ → ℕ | ∀ (n : σ), s n < fintype.card α } : begin refine quotient.sound ⟨equiv.subtype_congr finsupp.equiv_fun_on_fintype $ assume f, _⟩, refine forall_congr (assume n, nat.le_sub_right_iff_add_le _), exact fintype.card_pos_iff.2 ⟨0⟩ end ... = cardinal.mk (σ → {n // n < fintype.card α}) : quotient.sound ⟨@equiv.subtype_pi_equiv_pi σ (λ_, ℕ) (λs n, n < fintype.card α)⟩ ... = cardinal.mk (σ → fin (fintype.card α)) : quotient.sound ⟨equiv.arrow_congr (equiv.refl σ) (equiv.fin_equiv_subtype _).symm⟩ ... = cardinal.mk (σ → α) : begin refine (trunc.induction_on (fintype.equiv_fin α) $ assume (e : α ≃ fin (fintype.card α)), _), refine quotient.sound ⟨equiv.arrow_congr (equiv.refl σ) e.symm⟩ end ... = fintype.card (σ → α) : cardinal.fintype_card _ def evalᵢ : R σ α →ₗ[α] (σ → α) → α := ((evalₗ α σ).comp (restrict_degree σ α (fintype.card α - 1)).subtype) lemma range_evalᵢ : (evalᵢ σ α).range = ⊤ := begin rw [evalᵢ, linear_map.range_comp, range_subtype], exact map_restrict_dom_evalₗ end lemma ker_evalₗ : (evalᵢ σ α).ker = ⊥ := begin refine injective_of_surjective _ _ _ (range_evalᵢ _ _), { rw [dim_R], exact cardinal.nat_lt_omega _ }, { rw [dim_R, dim_fun, dim_of_field, mul_one] } end lemma eq_zero_of_eval_eq_zero (p : mv_polynomial σ α) (h : ∀v:σ → α, p.eval v = 0) (hp : p ∈ restrict_degree σ α (fintype.card α - 1)) : p = 0 := let p' : R σ α := ⟨p, hp⟩ in have p' ∈ (evalᵢ σ α).ker := by rw [mem_ker]; ext v; exact h v, show p'.1 = (0 : R σ α).1, begin rw [ker_evalₗ, mem_bot] at this, rw [this] end end mv_polynomial namespace mv_polynomial variables (σ : Type*) (R : Type*) [comm_ring R] (p : ℕ) instance [char_p R p] : char_p (mv_polynomial σ R) p := { cast_eq_zero_iff := λ n, by rw [← C_eq_coe_nat, ← C_0, C_inj, char_p.cast_eq_zero_iff R p] } end mv_polynomial
f22f2353e9de7a7556057ba678fff1fb64ac098b
c777c32c8e484e195053731103c5e52af26a25d1
/src/measure_theory/integral/divergence_theorem.lean
17ecec3e87575b25f8daa8883fcce3e1465da404
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
30,101
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.box_integral.divergence_theorem import analysis.box_integral.integrability import measure_theory.integral.interval_integral /-! # Divergence theorem for Bochner integral In this file we prove the Divergence theorem for Bochner integral on a box in `ℝⁿ⁺¹ = fin (n + 1) → ℝ`. More precisely, we prove the following theorem. Let `E` be a complete normed space. If `f : ℝⁿ⁺¹ → Eⁿ⁺¹` is continuous on a rectangular box `[a, b] : set ℝⁿ⁺¹`, `a ≤ b`, differentiable on its interior with derivative `f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] Eⁿ⁺¹`, and the divergence `λ x, ∑ i, f' x eᵢ i` is integrable on `[a, b]`, where `eᵢ = pi.single i 1` is the `i`-th basis vector, then its integral is equal to the sum of integrals of `f` over the faces of `[a, b]`, taken with appropriate signs. Moreover, the same is true if the function is not differentiable at countably many points of the interior of `[a, b]`. Once we prove the general theorem, we deduce corollaries for functions `ℝ → E` and pairs of functions `(ℝ × ℝ) → E`. ## Notations We use the following local notation to make the statement more readable. Note that the documentation website shows the actual terms, not those abbreviated using local notations. * `ℝⁿ`, `ℝⁿ⁺¹`, `Eⁿ⁺¹`: `fin n → ℝ`, `fin (n + 1) → ℝ`, `fin (n + 1) → E`; * `face i`: the `i`-th face of the box `[a, b]` as a closed segment in `ℝⁿ`, namely `[a ∘ fin.succ_above i, b ∘ fin.succ_above i]`; * `e i` : `i`-th basis vector `pi.single i 1`; * `front_face i`, `back_face i`: embeddings `ℝⁿ → ℝⁿ⁺¹` corresponding to the front face `{x | x i = b i}` and back face `{x | x i = a i}` of the box `[a, b]`, respectively. They are given by `fin.insert_nth i (b i)` and `fin.insert_nth i (a i)`. ## TODO * Add a version that assumes existence and integrability of partial derivatives. ## Tags divergence theorem, Bochner integral -/ open set finset topological_space function box_integral measure_theory filter open_locale big_operators classical topology interval universes u namespace measure_theory variables {E : Type u} [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] section variables {n : ℕ} local notation `ℝⁿ` := fin n → ℝ local notation `ℝⁿ⁺¹` := fin (n + 1) → ℝ local notation `Eⁿ⁺¹` := fin (n + 1) → E local notation `e ` i := pi.single i 1 section /-! ### Divergence theorem for functions on `ℝⁿ⁺¹ = fin (n + 1) → ℝ`. In this section we use the divergence theorem for a Henstock-Kurzweil-like integral `box_integral.has_integral_GP_divergence_of_forall_has_deriv_within_at` to prove the divergence theorem for Bochner integral. The divergence theorem for Bochner integral `measure_theory.integral_divergence_of_has_fderiv_within_at_off_countable` assumes that the function itself is continuous on a closed box, differentiable at all but countably many points of its interior, and the divergence is integrable on the box. This statement differs from `box_integral.has_integral_GP_divergence_of_forall_has_deriv_within_at` in several aspects. * We use Bochner integral instead of a Henstock-Kurzweil integral. This modification is done in `measure_theory.integral_divergence_of_has_fderiv_within_at_off_countable_aux₁`. As a side effect of this change, we need to assume that the divergence is integrable. * We don't assume differentiability on the boundary of the box. This modification is done in `measure_theory.integral_divergence_of_has_fderiv_within_at_off_countable_aux₂`. To prove it, we choose an increasing sequence of smaller boxes that cover the interior of the original box, then apply the previous lemma to these smaller boxes and take the limit of both sides of the equation. * We assume `a ≤ b` instead of `∀ i, a i < b i`. This is the last step of the proof, and it is done in the main theorem `measure_theory.integral_divergence_of_has_fderiv_within_at_off_countable`. -/ /-- An auxiliary lemma for `measure_theory.integral_divergence_of_has_fderiv_within_at_off_countable`. This is exactly `box_integral.has_integral_GP_divergence_of_forall_has_deriv_within_at` reformulated for the Bochner integral. -/ lemma integral_divergence_of_has_fderiv_within_at_off_countable_aux₁ (I : box (fin (n + 1))) (f : ℝⁿ⁺¹ → Eⁿ⁺¹) (f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] Eⁿ⁺¹) (s : set ℝⁿ⁺¹) (hs : s.countable) (Hc : continuous_on f I.Icc) (Hd : ∀ x ∈ I.Icc \ s, has_fderiv_within_at f (f' x) I.Icc x) (Hi : integrable_on (λ x, ∑ i, f' x (e i) i) I.Icc) : ∫ x in I.Icc, ∑ i, f' x (e i) i = ∑ i : fin (n + 1), ((∫ x in (I.face i).Icc, f (i.insert_nth (I.upper i) x) i) - ∫ x in (I.face i).Icc, f (i.insert_nth (I.lower i) x) i) := begin simp only [← set_integral_congr_set_ae (box.coe_ae_eq_Icc _)], have A := ((Hi.mono_set box.coe_subset_Icc).has_box_integral ⊥ rfl), have B := has_integral_GP_divergence_of_forall_has_deriv_within_at I f f' (s ∩ I.Icc) (hs.mono (inter_subset_left _ _)) (λ x hx, Hc _ hx.2) (λ x hx, Hd _ ⟨hx.1, λ h, hx.2 ⟨h, hx.1⟩⟩), rw continuous_on_pi at Hc, refine (A.unique B).trans (sum_congr rfl $ λ i hi, _), refine congr_arg2 has_sub.sub _ _, { have := box.continuous_on_face_Icc (Hc i) (set.right_mem_Icc.2 (I.lower_le_upper i)), have := (this.integrable_on_compact (box.is_compact_Icc _)).mono_set box.coe_subset_Icc, exact (this.has_box_integral ⊥ rfl).integral_eq, apply_instance }, { have := box.continuous_on_face_Icc (Hc i) (set.left_mem_Icc.2 (I.lower_le_upper i)), have := (this.integrable_on_compact (box.is_compact_Icc _)).mono_set box.coe_subset_Icc, exact (this.has_box_integral ⊥ rfl).integral_eq, apply_instance } end /-- An auxiliary lemma for `measure_theory.integral_divergence_of_has_fderiv_within_at_off_countable`. Compared to the previous lemma, here we drop the assumption of differentiability on the boundary of the box. -/ lemma integral_divergence_of_has_fderiv_within_at_off_countable_aux₂ (I : box (fin (n + 1))) (f : ℝⁿ⁺¹ → Eⁿ⁺¹) (f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] Eⁿ⁺¹) (s : set ℝⁿ⁺¹) (hs : s.countable) (Hc : continuous_on f I.Icc) (Hd : ∀ x ∈ I.Ioo \ s, has_fderiv_at f (f' x) x) (Hi : integrable_on (λ x, ∑ i, f' x (e i) i) I.Icc) : ∫ x in I.Icc, ∑ i, f' x (e i) i = ∑ i : fin (n + 1), ((∫ x in (I.face i).Icc, f (i.insert_nth (I.upper i) x) i) - ∫ x in (I.face i).Icc, f (i.insert_nth (I.lower i) x) i) := begin /- Choose a monotone sequence `J k` of subboxes that cover the interior of `I` and prove that these boxes satisfy the assumptions of the previous lemma. -/ rcases I.exists_seq_mono_tendsto with ⟨J, hJ_sub, hJl, hJu⟩, have hJ_sub' : ∀ k, (J k).Icc ⊆ I.Icc, from λ k, (hJ_sub k).trans I.Ioo_subset_Icc, have hJ_le : ∀ k, J k ≤ I, from λ k, box.le_iff_Icc.2 (hJ_sub' k), have HcJ : ∀ k, continuous_on f (J k).Icc, from λ k, Hc.mono (hJ_sub' k), have HdJ : ∀ k (x ∈ (J k).Icc \ s), has_fderiv_within_at f (f' x) (J k).Icc x, from λ k x hx, (Hd x ⟨hJ_sub k hx.1, hx.2⟩).has_fderiv_within_at, have HiJ : ∀ k, integrable_on (λ x, ∑ i, f' x (e i) i) (J k).Icc, from λ k, Hi.mono_set (hJ_sub' k), -- Apply the previous lemma to `J k`. have HJ_eq := λ k, integral_divergence_of_has_fderiv_within_at_off_countable_aux₁ (J k) f f' s hs (HcJ k) (HdJ k) (HiJ k), /- Note that the LHS of `HJ_eq k` tends to the LHS of the goal as `k → ∞`. -/ have hI_tendsto : tendsto (λ k, ∫ x in (J k).Icc, ∑ i, f' x (e i) i) at_top (𝓝 (∫ x in I.Icc, ∑ i, f' x (e i) i)), { simp only [integrable_on, ← measure.restrict_congr_set (box.Ioo_ae_eq_Icc _)] at Hi ⊢, rw ← box.Union_Ioo_of_tendsto J.monotone hJl hJu at Hi ⊢, exact tendsto_set_integral_of_monotone (λ k, (J k).measurable_set_Ioo) (box.Ioo.comp J).monotone Hi }, /- Thus it suffices to prove the same about the RHS. -/ refine tendsto_nhds_unique_of_eventually_eq hI_tendsto _ (eventually_of_forall HJ_eq), clear hI_tendsto, rw tendsto_pi_nhds at hJl hJu, /- We'll need to prove a similar statement about the integrals over the front sides and the integrals over the back sides. In order to avoid repeating ourselves, we formulate a lemma. -/ suffices : ∀ (i : fin (n + 1)) (c : ℕ → ℝ) d, (∀ k, c k ∈ Icc (I.lower i) (I.upper i)) → tendsto c at_top (𝓝 d) → tendsto (λ k, ∫ x in ((J k).face i).Icc, f (i.insert_nth (c k) x) i) at_top (𝓝 $ ∫ x in (I.face i).Icc, f (i.insert_nth d x) i), { rw box.Icc_eq_pi at hJ_sub', refine tendsto_finset_sum _ (λ i hi, (this _ _ _ _ (hJu _)).sub (this _ _ _ _ (hJl _))), exacts [λ k, hJ_sub' k (J k).upper_mem_Icc _ trivial, λ k, hJ_sub' k (J k).lower_mem_Icc _ trivial] }, intros i c d hc hcd, /- First we prove that the integrals of the restriction of `f` to `{x | x i = d}` over increasing boxes `((J k).face i).Icc` tend to the desired limit. The proof mostly repeats the one above. -/ have hd : d ∈ Icc (I.lower i) (I.upper i), from is_closed_Icc.mem_of_tendsto hcd (eventually_of_forall hc), have Hic : ∀ k, integrable_on (λ x, f (i.insert_nth (c k) x) i) (I.face i).Icc, from λ k, (box.continuous_on_face_Icc ((continuous_apply i).comp_continuous_on Hc) (hc k)).integrable_on_Icc, have Hid : integrable_on (λ x, f (i.insert_nth d x) i) (I.face i).Icc, from (box.continuous_on_face_Icc ((continuous_apply i).comp_continuous_on Hc) hd).integrable_on_Icc, have H : tendsto (λ k, ∫ x in ((J k).face i).Icc, f (i.insert_nth d x) i) at_top (𝓝 $ ∫ x in (I.face i).Icc, f (i.insert_nth d x) i), { have hIoo : (⋃ k, ((J k).face i).Ioo) = (I.face i).Ioo, from box.Union_Ioo_of_tendsto ((box.monotone_face i).comp J.monotone) (tendsto_pi_nhds.2 (λ _, hJl _)) (tendsto_pi_nhds.2 (λ _, hJu _)), simp only [integrable_on, ← measure.restrict_congr_set (box.Ioo_ae_eq_Icc _), ← hIoo] at Hid ⊢, exact tendsto_set_integral_of_monotone (λ k, ((J k).face i).measurable_set_Ioo) (box.Ioo.monotone.comp ((box.monotone_face i).comp J.monotone)) Hid }, /- Thus it suffices to show that the distance between the integrals of the restrictions of `f` to `{x | x i = c k}` and `{x | x i = d}` over `((J k).face i).Icc` tends to zero as `k → ∞`. Choose `ε > 0`. -/ refine H.congr_dist (metric.nhds_basis_closed_ball.tendsto_right_iff.2 (λ ε εpos, _)), have hvol_pos : ∀ J : box (fin n), 0 < ∏ j, (J.upper j - J.lower j), from λ J, (prod_pos $ λ j hj, sub_pos.2 $ J.lower_lt_upper _), /- Choose `δ > 0` such that for any `x y ∈ I.Icc` at distance at most `δ`, the distance between `f x` and `f y` is at most `ε / volume (I.face i).Icc`, then the distance between the integrals is at most `(ε / volume (I.face i).Icc) * volume ((J k).face i).Icc ≤ ε`. -/ rcases metric.uniform_continuous_on_iff_le.1 (I.is_compact_Icc.uniform_continuous_on_of_continuous Hc) (ε / ∏ j, ((I.face i).upper j - (I.face i).lower j)) (div_pos εpos (hvol_pos (I.face i))) with ⟨δ, δpos, hδ⟩, refine (hcd.eventually (metric.ball_mem_nhds _ δpos)).mono (λ k hk, _), have Hsub : ((J k).face i).Icc ⊆ (I.face i).Icc, from box.le_iff_Icc.1 (box.face_mono (hJ_le _) i), rw [mem_closed_ball_zero_iff, real.norm_eq_abs, abs_of_nonneg dist_nonneg, dist_eq_norm, ← integral_sub (Hid.mono_set Hsub) ((Hic _).mono_set Hsub)], calc ‖(∫ x in ((J k).face i).Icc, f (i.insert_nth d x) i - f (i.insert_nth (c k) x) i)‖ ≤ (ε / ∏ j, ((I.face i).upper j - (I.face i).lower j)) * (volume ((J k).face i).Icc).to_real : begin refine norm_set_integral_le_of_norm_le_const' (((J k).face i).measure_Icc_lt_top _) ((J k).face i).measurable_set_Icc (λ x hx, _), rw ← dist_eq_norm, calc dist (f (i.insert_nth d x) i) (f (i.insert_nth (c k) x) i) ≤ dist (f (i.insert_nth d x)) (f (i.insert_nth (c k) x)) : dist_le_pi_dist (f (i.insert_nth d x)) (f (i.insert_nth (c k) x)) i ... ≤ (ε / ∏ j, ((I.face i).upper j - (I.face i).lower j)) : hδ _ (I.maps_to_insert_nth_face_Icc hd $ Hsub hx) _ (I.maps_to_insert_nth_face_Icc (hc _) $ Hsub hx) _, rw [fin.dist_insert_nth_insert_nth, dist_self, dist_comm], exact max_le hk.le δpos.lt.le end ... ≤ ε : begin rw [box.Icc_def, real.volume_Icc_pi_to_real ((J k).face i).lower_le_upper, ← le_div_iff (hvol_pos _)], refine div_le_div_of_le_left εpos.le (hvol_pos _) (prod_le_prod (λ j hj, _) (λ j hj, _)), exacts [sub_nonneg.2 (box.lower_le_upper _ _), sub_le_sub ((hJ_sub' _ (J _).upper_mem_Icc).2 _) ((hJ_sub' _ (J _).lower_mem_Icc).1 _)] end end variables (a b : ℝⁿ⁺¹) local notation `face ` i := set.Icc (a ∘ fin.succ_above i) (b ∘ fin.succ_above i) local notation `front_face ` i:2000 := fin.insert_nth i (b i) local notation `back_face ` i:2000 := fin.insert_nth i (a i) /-- **Divergence theorem** for Bochner integral. If `f : ℝⁿ⁺¹ → Eⁿ⁺¹` is continuous on a rectangular box `[a, b] : set ℝⁿ⁺¹`, `a ≤ b`, is differentiable on its interior with derivative `f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] Eⁿ⁺¹` and the divergence `λ x, ∑ i, f' x eᵢ i` is integrable on `[a, b]`, where `eᵢ = pi.single i 1` is the `i`-th basis vector, then its integral is equal to the sum of integrals of `f` over the faces of `[a, b]`, taken with appropriat signs. Moreover, the same is true if the function is not differentiable at countably many points of the interior of `[a, b]`. We represent both faces `x i = a i` and `x i = b i` as the box `face i = [a ∘ fin.succ_above i, b ∘ fin.succ_above i]` in `ℝⁿ`, where `fin.succ_above : fin n ↪o fin (n + 1)` is the order embedding with range `{i}ᶜ`. The restrictions of `f : ℝⁿ⁺¹ → Eⁿ⁺¹` to these faces are given by `f ∘ back_face i` and `f ∘ front_face i`, where `back_face i = fin.insert_nth i (a i)` and `front_face i = fin.insert_nth i (b i)` are embeddings `ℝⁿ → ℝⁿ⁺¹` that take `y : ℝⁿ` and insert `a i` (resp., `b i`) as `i`-th coordinate. -/ lemma integral_divergence_of_has_fderiv_within_at_off_countable (hle : a ≤ b) (f : ℝⁿ⁺¹ → Eⁿ⁺¹) (f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] Eⁿ⁺¹) (s : set ℝⁿ⁺¹) (hs : s.countable) (Hc : continuous_on f (Icc a b)) (Hd : ∀ x ∈ set.pi univ (λ i, Ioo (a i) (b i)) \ s, has_fderiv_at f (f' x) x) (Hi : integrable_on (λ x, ∑ i, f' x (e i) i) (Icc a b)) : ∫ x in Icc a b, ∑ i, f' x (e i) i = ∑ i : fin (n + 1), ((∫ x in face i, f (front_face i x) i) - ∫ x in face i, f (back_face i x) i) := begin rcases em (∃ i, a i = b i) with ⟨i, hi⟩|hne, { /- First we sort out the trivial case `∃ i, a i = b i`. -/ simp only [volume_pi, ← set_integral_congr_set_ae measure.univ_pi_Ioc_ae_eq_Icc], have hi' : Ioc (a i) (b i) = ∅ := Ioc_eq_empty hi.not_lt, have : pi set.univ (λ j, Ioc (a j) (b j)) = ∅, from univ_pi_eq_empty hi', rw [this, integral_empty, sum_eq_zero], rintro j -, rcases eq_or_ne i j with rfl|hne, { simp [hi] }, { rcases fin.exists_succ_above_eq hne with ⟨i, rfl⟩, have : pi set.univ (λ k : fin n, Ioc (a $ j.succ_above k) (b $ j.succ_above k)) = ∅, from univ_pi_eq_empty hi', rw [this, integral_empty, integral_empty, sub_self] } }, { /- In the non-trivial case `∀ i, a i < b i`, we apply a lemma we proved above. -/ have hlt : ∀ i, a i < b i, from λ i, (hle i).lt_of_ne (λ hi, hne ⟨i, hi⟩), convert integral_divergence_of_has_fderiv_within_at_off_countable_aux₂ ⟨a, b, hlt⟩ f f' s hs Hc Hd Hi } end /-- **Divergence theorem** for a family of functions `f : fin (n + 1) → ℝⁿ⁺¹ → E`. See also `measure_theory.integral_divergence_of_has_fderiv_within_at_off_countable'` for a version formulated in terms of a vector-valued function `f : ℝⁿ⁺¹ → Eⁿ⁺¹`. -/ lemma integral_divergence_of_has_fderiv_within_at_off_countable' (hle : a ≤ b) (f : fin (n + 1) → ℝⁿ⁺¹ → E) (f' : fin (n + 1) → ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] E) (s : set ℝⁿ⁺¹) (hs : s.countable) (Hc : ∀ i, continuous_on (f i) (Icc a b)) (Hd : ∀ (x ∈ pi set.univ (λ i, Ioo (a i) (b i)) \ s) i, has_fderiv_at (f i) (f' i x) x) (Hi : integrable_on (λ x, ∑ i, f' i x (e i)) (Icc a b)) : ∫ x in Icc a b, ∑ i, f' i x (e i) = ∑ i : fin (n + 1), ((∫ x in face i, f i (front_face i x)) - ∫ x in face i, f i (back_face i x)) := integral_divergence_of_has_fderiv_within_at_off_countable a b hle (λ x i, f i x) (λ x, continuous_linear_map.pi (λ i, f' i x)) s hs (continuous_on_pi.2 Hc) (λ x hx, has_fderiv_at_pi.2 (Hd x hx)) Hi end /-- An auxiliary lemma that is used to specialize the general divergence theorem to spaces that do not have the form `fin n → ℝ`. -/ lemma integral_divergence_of_has_fderiv_within_at_off_countable_of_equiv {F : Type*} [normed_add_comm_group F] [normed_space ℝ F] [partial_order F] [measure_space F] [borel_space F] (eL : F ≃L[ℝ] ℝⁿ⁺¹) (he_ord : ∀ x y, eL x ≤ eL y ↔ x ≤ y) (he_vol : measure_preserving eL volume volume) (f : fin (n + 1) → F → E) (f' : fin (n + 1) → F → F →L[ℝ] E) (s : set F) (hs : s.countable) (a b : F) (hle : a ≤ b) (Hc : ∀ i, continuous_on (f i) (Icc a b)) (Hd : ∀ (x ∈ interior (Icc a b) \ s) i, has_fderiv_at (f i) (f' i x) x) (DF : F → E) (hDF : ∀ x, DF x = ∑ i, f' i x (eL.symm $ e i)) (Hi : integrable_on DF (Icc a b)) : ∫ x in Icc a b, DF x = ∑ i : fin (n + 1), ((∫ x in Icc (eL a ∘ i.succ_above) (eL b ∘ i.succ_above), f i (eL.symm $ i.insert_nth (eL b i) x)) - (∫ x in Icc (eL a ∘ i.succ_above) (eL b ∘ i.succ_above), f i (eL.symm $ i.insert_nth (eL a i) x))) := have he_emb : measurable_embedding eL := eL.to_homeomorph.to_measurable_equiv.measurable_embedding, have hIcc : eL ⁻¹' (Icc (eL a) (eL b)) = Icc a b, by { ext1 x, simp only [set.mem_preimage, set.mem_Icc, he_ord] }, have hIcc' : Icc (eL a) (eL b) = eL.symm ⁻¹' (Icc a b), by rw [← hIcc, eL.symm_preimage_preimage], calc ∫ x in Icc a b, DF x = ∫ x in Icc a b, ∑ i, f' i x (eL.symm $ e i) : by simp only [hDF] ... = ∫ x in Icc (eL a) (eL b), ∑ i, f' i (eL.symm x) (eL.symm $ e i) : begin rw [← he_vol.set_integral_preimage_emb he_emb], simp only [hIcc, eL.symm_apply_apply] end ... = ∑ i : fin (n + 1), ((∫ x in Icc (eL a ∘ i.succ_above) (eL b ∘ i.succ_above), f i (eL.symm $ i.insert_nth (eL b i) x)) - (∫ x in Icc (eL a ∘ i.succ_above) (eL b ∘ i.succ_above), f i (eL.symm $ i.insert_nth (eL a i) x))) : begin convert integral_divergence_of_has_fderiv_within_at_off_countable' (eL a) (eL b) ((he_ord _ _).2 hle) (λ i x, f i (eL.symm x)) (λ i x, f' i (eL.symm x) ∘L (eL.symm : ℝⁿ⁺¹ →L[ℝ] F)) (eL.symm ⁻¹' s) (hs.preimage eL.symm.injective) _ _ _, { exact λ i, (Hc i).comp eL.symm.continuous_on hIcc'.subset }, { refine λ x hx i, (Hd (eL.symm x) ⟨_, hx.2⟩ i).comp x eL.symm.has_fderiv_at, rw ← hIcc, refine preimage_interior_subset_interior_preimage eL.continuous _, simpa only [set.mem_preimage, eL.apply_symm_apply, ← pi_univ_Icc, interior_pi_set finite_univ, interior_Icc] using hx.1 }, { rw [← he_vol.integrable_on_comp_preimage he_emb, hIcc], simp [← hDF, (∘), Hi] } end end open_locale interval open continuous_linear_map (smul_right) local notation `ℝ¹` := fin 1 → ℝ local notation `ℝ²` := fin 2 → ℝ local notation `E¹` := fin 1 → E local notation `E²` := fin 2 → E /-- **Fundamental theorem of calculus, part 2**. This version assumes that `f` is continuous on the interval and is differentiable off a countable set `s`. See also * `interval_integral.integral_eq_sub_of_has_deriv_right_of_le` for a version that only assumes right differentiability of `f`; * `measure_theory.integral_eq_of_has_deriv_within_at_off_countable` for a version that works both for `a ≤ b` and `b ≤ a` at the expense of using unordered intervals instead of `set.Icc`. -/ theorem integral_eq_of_has_deriv_within_at_off_countable_of_le (f f' : ℝ → E) {a b : ℝ} (hle : a ≤ b) {s : set ℝ} (hs : s.countable) (Hc : continuous_on f (Icc a b)) (Hd : ∀ x ∈ Ioo a b \ s, has_deriv_at f (f' x) x) (Hi : interval_integrable f' volume a b) : ∫ x in a..b, f' x = f b - f a := begin set e : ℝ ≃L[ℝ] ℝ¹ := (continuous_linear_equiv.fun_unique (fin 1) ℝ ℝ).symm, have e_symm : ∀ x, e.symm x = x 0 := λ x, rfl, set F' : ℝ → ℝ →L[ℝ] E := λ x, smul_right (1 : ℝ →L[ℝ] ℝ) (f' x), have hF' : ∀ x y, F' x y = y • f' x := λ x y, rfl, calc ∫ x in a..b, f' x = ∫ x in Icc a b, f' x : by simp only [interval_integral.integral_of_le hle, set_integral_congr_set_ae Ioc_ae_eq_Icc] ... = ∑ i : fin 1, ((∫ x in Icc (e a ∘ i.succ_above) (e b ∘ i.succ_above), f (e.symm $ i.insert_nth (e b i) x)) - (∫ x in Icc (e a ∘ i.succ_above) (e b ∘ i.succ_above), f (e.symm $ i.insert_nth (e a i) x))) : begin simp only [← interior_Icc] at Hd, refine integral_divergence_of_has_fderiv_within_at_off_countable_of_equiv e _ _ (λ _, f) (λ _, F') s hs a b hle (λ i, Hc) (λ x hx i, Hd x hx) _ _ _, { exact λ x y, (order_iso.fun_unique (fin 1) ℝ).symm.le_iff_le }, { exact (volume_preserving_fun_unique (fin 1) ℝ).symm _ }, { intro x, rw [fin.sum_univ_one, hF', e_symm, pi.single_eq_same, one_smul] }, { rw [interval_integrable_iff_integrable_Ioc_of_le hle] at Hi, exact Hi.congr_set_ae Ioc_ae_eq_Icc.symm } end ... = f b - f a : begin simp only [fin.sum_univ_one, e_symm], have : ∀ (c : ℝ), const (fin 0) c = is_empty_elim := λ c, subsingleton.elim _ _, simp [this, volume_pi, measure.pi_of_empty (λ _ : fin 0, volume)] end end /-- **Fundamental theorem of calculus, part 2**. This version assumes that `f` is continuous on the interval and is differentiable off a countable set `s`. See also `measure_theory.interval_integral.integral_eq_sub_of_has_deriv_right` for a version that only assumes right differentiability of `f`. -/ theorem integral_eq_of_has_deriv_within_at_off_countable (f f' : ℝ → E) {a b : ℝ} {s : set ℝ} (hs : s.countable) (Hc : continuous_on f [a, b]) (Hd : ∀ x ∈ Ioo (min a b) (max a b) \ s, has_deriv_at f (f' x) x) (Hi : interval_integrable f' volume a b) : ∫ x in a..b, f' x = f b - f a := begin cases le_total a b with hab hab, { simp only [uIcc_of_le hab, min_eq_left hab, max_eq_right hab] at *, exact integral_eq_of_has_deriv_within_at_off_countable_of_le f f' hab hs Hc Hd Hi }, { simp only [uIcc_of_ge hab, min_eq_right hab, max_eq_left hab] at *, rw [interval_integral.integral_symm, neg_eq_iff_eq_neg, neg_sub], exact integral_eq_of_has_deriv_within_at_off_countable_of_le f f' hab hs Hc Hd Hi.symm } end /-- **Divergence theorem** for functions on the plane along rectangles. It is formulated in terms of two functions `f g : ℝ × ℝ → E` and an integral over `Icc a b = [a.1, b.1] × [a.2, b.2]`, where `a b : ℝ × ℝ`, `a ≤ b`. When thinking of `f` and `g` as the two coordinates of a single function `F : ℝ × ℝ → E × E` and when `E = ℝ`, this is the usual statement that the integral of the divergence of `F` inside the rectangle equals the integral of the normal derivative of `F` along the boundary. See also `measure_theory.integral2_divergence_prod_of_has_fderiv_within_at_off_countable` for a version that does not assume `a ≤ b` and uses iterated interval integral instead of the integral over `Icc a b`. -/ lemma integral_divergence_prod_Icc_of_has_fderiv_within_at_off_countable_of_le (f g : ℝ × ℝ → E) (f' g' : ℝ × ℝ → ℝ × ℝ →L[ℝ] E) (a b : ℝ × ℝ) (hle : a ≤ b) (s : set (ℝ × ℝ)) (hs : s.countable) (Hcf : continuous_on f (Icc a b)) (Hcg : continuous_on g (Icc a b)) (Hdf : ∀ x ∈ Ioo a.1 b.1 ×ˢ Ioo a.2 b.2 \ s, has_fderiv_at f (f' x) x) (Hdg : ∀ x ∈ Ioo a.1 b.1 ×ˢ Ioo a.2 b.2 \ s, has_fderiv_at g (g' x) x) (Hi : integrable_on (λ x, f' x (1, 0) + g' x (0, 1)) (Icc a b)) : ∫ x in Icc a b, f' x (1, 0) + g' x (0, 1) = (∫ x in a.1..b.1, g (x, b.2)) - (∫ x in a.1..b.1, g (x, a.2)) + (∫ y in a.2..b.2, f (b.1, y)) - ∫ y in a.2..b.2, f (a.1, y) := let e : (ℝ × ℝ) ≃L[ℝ] ℝ² := (continuous_linear_equiv.fin_two_arrow ℝ ℝ).symm in calc ∫ x in Icc a b, f' x (1, 0) + g' x (0, 1) = ∑ i : fin 2, ((∫ x in Icc (e a ∘ i.succ_above) (e b ∘ i.succ_above), ![f, g] i (e.symm $ i.insert_nth (e b i) x)) - (∫ x in Icc (e a ∘ i.succ_above) (e b ∘ i.succ_above), ![f, g] i (e.symm $ i.insert_nth (e a i) x))) : begin refine integral_divergence_of_has_fderiv_within_at_off_countable_of_equiv e _ _ ![f, g] ![f', g'] s hs a b hle _ (λ x hx, _) _ _ Hi, { exact λ x y, (order_iso.fin_two_arrow_iso ℝ).symm.le_iff_le }, { exact (volume_preserving_fin_two_arrow ℝ).symm _ }, { exact fin.forall_fin_two.2 ⟨Hcf, Hcg⟩ }, { rw [Icc_prod_eq, interior_prod_eq, interior_Icc, interior_Icc] at hx, exact fin.forall_fin_two.2 ⟨Hdf x hx, Hdg x hx⟩ }, { intro x, rw fin.sum_univ_two, simp } end ... = (∫ y in Icc a.2 b.2, f (b.1, y)) - (∫ y in Icc a.2 b.2, f (a.1, y)) + ((∫ x in Icc a.1 b.1, g (x, b.2)) - ∫ x in Icc a.1 b.1, g (x, a.2)) : begin have : ∀ (a b : ℝ¹) (f : ℝ¹ → E), ∫ x in Icc a b, f x = ∫ x in Icc (a 0) (b 0), f (λ _, x), { intros a b f, convert (((volume_preserving_fun_unique (fin 1) ℝ).symm _).set_integral_preimage_emb (measurable_equiv.measurable_embedding _) _ _).symm, exact ((order_iso.fun_unique (fin 1) ℝ).symm.preimage_Icc a b).symm }, simp only [fin.sum_univ_two, this], refl end ... = (∫ x in a.1..b.1, g (x, b.2)) - (∫ x in a.1..b.1, g (x, a.2)) + (∫ y in a.2..b.2, f (b.1, y)) - ∫ y in a.2..b.2, f (a.1, y) : begin simp only [interval_integral.integral_of_le hle.1, interval_integral.integral_of_le hle.2, set_integral_congr_set_ae Ioc_ae_eq_Icc], abel end /-- **Divergence theorem** for functions on the plane. It is formulated in terms of two functions `f g : ℝ × ℝ → E` and iterated integral `∫ x in a₁..b₁, ∫ y in a₂..b₂, _`, where `a₁ a₂ b₁ b₂ : ℝ`. When thinking of `f` and `g` as the two coordinates of a single function `F : ℝ × ℝ → E × E` and when `E = ℝ`, this is the usual statement that the integral of the divergence of `F` inside the rectangle with vertices `(aᵢ, bⱼ)`, `i, j =1,2`, equals the integral of the normal derivative of `F` along the boundary. See also `measure_theory.integral_divergence_prod_Icc_of_has_fderiv_within_at_off_countable_of_le` for a version that uses an integral over `Icc a b`, where `a b : ℝ × ℝ`, `a ≤ b`. -/ lemma integral2_divergence_prod_of_has_fderiv_within_at_off_countable (f g : ℝ × ℝ → E) (f' g' : ℝ × ℝ → ℝ × ℝ →L[ℝ] E) (a₁ a₂ b₁ b₂ : ℝ) (s : set (ℝ × ℝ)) (hs : s.countable) (Hcf : continuous_on f ([a₁, b₁] ×ˢ [a₂, b₂])) (Hcg : continuous_on g ([a₁, b₁] ×ˢ [a₂, b₂])) (Hdf : ∀ x ∈ Ioo (min a₁ b₁) (max a₁ b₁) ×ˢ Ioo (min a₂ b₂) (max a₂ b₂) \ s, has_fderiv_at f (f' x) x) (Hdg : ∀ x ∈ Ioo (min a₁ b₁) (max a₁ b₁) ×ˢ Ioo (min a₂ b₂) (max a₂ b₂) \ s, has_fderiv_at g (g' x) x) (Hi : integrable_on (λ x, f' x (1, 0) + g' x (0, 1)) ([a₁, b₁] ×ˢ [a₂, b₂])) : ∫ x in a₁..b₁, ∫ y in a₂..b₂, f' (x, y) (1, 0) + g' (x, y) (0, 1) = (∫ x in a₁..b₁, g (x, b₂)) - (∫ x in a₁..b₁, g (x, a₂)) + (∫ y in a₂..b₂, f (b₁, y)) - ∫ y in a₂..b₂, f (a₁, y) := begin wlog h₁ : a₁ ≤ b₁ generalizing a₁ b₁, { specialize this b₁ a₁, rw [uIcc_comm b₁ a₁, min_comm b₁ a₁, max_comm b₁ a₁] at this, simp only [interval_integral.integral_symm b₁ a₁], refine (congr_arg has_neg.neg (this Hcf Hcg Hdf Hdg Hi (le_of_not_le h₁))).trans _, abel }, wlog h₂ : a₂ ≤ b₂ generalizing a₂ b₂, { specialize this b₂ a₂, rw [uIcc_comm b₂ a₂, min_comm b₂ a₂, max_comm b₂ a₂] at this, simp only [interval_integral.integral_symm b₂ a₂, interval_integral.integral_neg], refine (congr_arg has_neg.neg (this Hcf Hcg Hdf Hdg Hi (le_of_not_le h₂))).trans _, abel }, simp only [uIcc_of_le h₁, uIcc_of_le h₂, min_eq_left, max_eq_right, h₁, h₂] at Hcf Hcg Hdf Hdg Hi, calc ∫ x in a₁..b₁, ∫ y in a₂..b₂, f' (x, y) (1, 0) + g' (x, y) (0, 1) = ∫ x in Icc a₁ b₁, ∫ y in Icc a₂ b₂, f' (x, y) (1, 0) + g' (x, y) (0, 1) : by simp only [interval_integral.integral_of_le, h₁, h₂, set_integral_congr_set_ae Ioc_ae_eq_Icc] ... = ∫ x in Icc a₁ b₁ ×ˢ Icc a₂ b₂, f' x (1, 0) + g' x (0, 1) : (set_integral_prod _ Hi).symm ... = (∫ x in a₁..b₁, g (x, b₂)) - (∫ x in a₁..b₁, g (x, a₂)) + (∫ y in a₂..b₂, f (b₁, y)) - ∫ y in a₂..b₂, f (a₁, y) : begin rw Icc_prod_Icc at *, apply integral_divergence_prod_Icc_of_has_fderiv_within_at_off_countable_of_le f g f' g' (a₁, a₂) (b₁, b₂) ⟨h₁, h₂⟩ s; assumption end end end measure_theory
c5a0740eacbd6d6109c138e646f8249941264a4f
137c667471a40116a7afd7261f030b30180468c2
/src/analysis/special_functions/exp_log.lean
8b5426b0b91fe7037f9d8109fa03bef5876e77c6
[ "Apache-2.0" ]
permissive
bragadeesh153/mathlib
46bf814cfb1eecb34b5d1549b9117dc60f657792
b577bb2cd1f96eb47031878256856020b76f73cd
refs/heads/master
1,687,435,188,334
1,626,384,207,000
1,626,384,207,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
34,879
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne -/ import data.complex.exponential import analysis.calculus.inverse import analysis.complex.real_deriv /-! # Complex and real exponential, real logarithm ## Main statements This file establishes the basic analytical properties of the complex and real exponential functions (continuity, differentiability, computation of the derivative). It also contains the definition of the real logarithm function (as the inverse of the exponential on `(0, +∞)`, extended to `ℝ` by setting `log (-x) = log x`) and its basic properties (continuity, differentiability, formula for the derivative). The complex logarithm is *not* defined in this file as it relies on trigonometric functions. See instead `trigonometric.lean`. ## Tags exp, log -/ noncomputable theory open finset filter metric asymptotics set function open_locale classical topological_space namespace complex /-- The complex exponential is everywhere differentiable, with the derivative `exp x`. -/ lemma has_deriv_at_exp (x : ℂ) : has_deriv_at exp (exp x) x := begin rw has_deriv_at_iff_is_o_nhds_zero, have : (1 : ℕ) < 2 := by norm_num, refine (is_O.of_bound (∥exp x∥) _).trans_is_o (is_o_pow_id this), filter_upwards [metric.ball_mem_nhds (0 : ℂ) zero_lt_one], simp only [metric.mem_ball, dist_zero_right, normed_field.norm_pow], intros z hz, calc ∥exp (x + z) - exp x - z * exp x∥ = ∥exp x * (exp z - 1 - z)∥ : by { congr, rw [exp_add], ring } ... = ∥exp x∥ * ∥exp z - 1 - z∥ : normed_field.norm_mul _ _ ... ≤ ∥exp x∥ * ∥z∥^2 : mul_le_mul_of_nonneg_left (abs_exp_sub_one_sub_id_le (le_of_lt hz)) (norm_nonneg _) end lemma differentiable_exp : differentiable ℂ exp := λx, (has_deriv_at_exp x).differentiable_at lemma differentiable_at_exp {x : ℂ} : differentiable_at ℂ exp x := differentiable_exp x @[simp] lemma deriv_exp : deriv exp = exp := funext $ λ x, (has_deriv_at_exp x).deriv @[simp] lemma iter_deriv_exp : ∀ n : ℕ, (deriv^[n] exp) = exp | 0 := rfl | (n+1) := by rw [iterate_succ_apply, deriv_exp, iter_deriv_exp n] @[continuity] lemma continuous_exp : continuous exp := differentiable_exp.continuous lemma continuous_on_exp {s : set ℂ} : continuous_on exp s := continuous_exp.continuous_on lemma times_cont_diff_exp : ∀ {n}, times_cont_diff ℂ n exp := begin refine times_cont_diff_all_iff_nat.2 (λ n, _), induction n with n ihn, { exact times_cont_diff_zero.2 continuous_exp }, { rw times_cont_diff_succ_iff_deriv, use differentiable_exp, rwa deriv_exp } end lemma has_strict_deriv_at_exp (x : ℂ) : has_strict_deriv_at exp (exp x) x := times_cont_diff_exp.times_cont_diff_at.has_strict_deriv_at' (has_deriv_at_exp x) le_rfl lemma is_open_map_exp : is_open_map exp := open_map_of_strict_deriv has_strict_deriv_at_exp exp_ne_zero end complex section variables {f : ℂ → ℂ} {f' x : ℂ} {s : set ℂ} lemma has_strict_deriv_at.cexp (hf : has_strict_deriv_at f f' x) : has_strict_deriv_at (λ x, complex.exp (f x)) (complex.exp (f x) * f') x := (complex.has_strict_deriv_at_exp (f x)).comp x hf lemma has_deriv_at.cexp (hf : has_deriv_at f f' x) : has_deriv_at (λ x, complex.exp (f x)) (complex.exp (f x) * f') x := (complex.has_deriv_at_exp (f x)).comp x hf lemma has_deriv_within_at.cexp (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, complex.exp (f x)) (complex.exp (f x) * f') s x := (complex.has_deriv_at_exp (f x)).comp_has_deriv_within_at x hf lemma deriv_within_cexp (hf : differentiable_within_at ℂ f s x) (hxs : unique_diff_within_at ℂ s x) : deriv_within (λx, complex.exp (f x)) s x = complex.exp (f x) * (deriv_within f s x) := hf.has_deriv_within_at.cexp.deriv_within hxs @[simp] lemma deriv_cexp (hc : differentiable_at ℂ f x) : deriv (λx, complex.exp (f x)) x = complex.exp (f x) * (deriv f x) := hc.has_deriv_at.cexp.deriv end section variables {E : Type*} [normed_group E] [normed_space ℂ E] {f : E → ℂ} {f' : E →L[ℂ] ℂ} {x : E} {s : set E} lemma has_strict_fderiv_at.cexp (hf : has_strict_fderiv_at f f' x) : has_strict_fderiv_at (λ x, complex.exp (f x)) (complex.exp (f x) • f') x := (complex.has_strict_deriv_at_exp (f x)).comp_has_strict_fderiv_at x hf lemma has_fderiv_within_at.cexp (hf : has_fderiv_within_at f f' s x) : has_fderiv_within_at (λ x, complex.exp (f x)) (complex.exp (f x) • f') s x := (complex.has_deriv_at_exp (f x)).comp_has_fderiv_within_at x hf lemma has_fderiv_at.cexp (hf : has_fderiv_at f f' x) : has_fderiv_at (λ x, complex.exp (f x)) (complex.exp (f x) • f') x := has_fderiv_within_at_univ.1 $ hf.has_fderiv_within_at.cexp lemma differentiable_within_at.cexp (hf : differentiable_within_at ℂ f s x) : differentiable_within_at ℂ (λ x, complex.exp (f x)) s x := hf.has_fderiv_within_at.cexp.differentiable_within_at @[simp] lemma differentiable_at.cexp (hc : differentiable_at ℂ f x) : differentiable_at ℂ (λx, complex.exp (f x)) x := hc.has_fderiv_at.cexp.differentiable_at lemma differentiable_on.cexp (hc : differentiable_on ℂ f s) : differentiable_on ℂ (λx, complex.exp (f x)) s := λx h, (hc x h).cexp @[simp] lemma differentiable.cexp (hc : differentiable ℂ f) : differentiable ℂ (λx, complex.exp (f x)) := λx, (hc x).cexp lemma times_cont_diff.cexp {n} (h : times_cont_diff ℂ n f) : times_cont_diff ℂ n (λ x, complex.exp (f x)) := complex.times_cont_diff_exp.comp h lemma times_cont_diff_at.cexp {n} (hf : times_cont_diff_at ℂ n f x) : times_cont_diff_at ℂ n (λ x, complex.exp (f x)) x := complex.times_cont_diff_exp.times_cont_diff_at.comp x hf lemma times_cont_diff_on.cexp {n} (hf : times_cont_diff_on ℂ n f s) : times_cont_diff_on ℂ n (λ x, complex.exp (f x)) s := complex.times_cont_diff_exp.comp_times_cont_diff_on hf lemma times_cont_diff_within_at.cexp {n} (hf : times_cont_diff_within_at ℂ n f s x) : times_cont_diff_within_at ℂ n (λ x, complex.exp (f x)) s x := complex.times_cont_diff_exp.times_cont_diff_at.comp_times_cont_diff_within_at x hf end namespace real variables {x y z : ℝ} lemma has_strict_deriv_at_exp (x : ℝ) : has_strict_deriv_at exp (exp x) x := (complex.has_strict_deriv_at_exp x).real_of_complex lemma has_deriv_at_exp (x : ℝ) : has_deriv_at exp (exp x) x := (complex.has_deriv_at_exp x).real_of_complex lemma times_cont_diff_exp {n} : times_cont_diff ℝ n exp := complex.times_cont_diff_exp.real_of_complex lemma differentiable_exp : differentiable ℝ exp := λx, (has_deriv_at_exp x).differentiable_at lemma differentiable_at_exp : differentiable_at ℝ exp x := differentiable_exp x @[simp] lemma deriv_exp : deriv exp = exp := funext $ λ x, (has_deriv_at_exp x).deriv @[simp] lemma iter_deriv_exp : ∀ n : ℕ, (deriv^[n] exp) = exp | 0 := rfl | (n+1) := by rw [iterate_succ_apply, deriv_exp, iter_deriv_exp n] @[continuity] lemma continuous_exp : continuous exp := differentiable_exp.continuous lemma continuous_on_exp {s : set ℝ} : continuous_on exp s := continuous_exp.continuous_on end real section /-! Register lemmas for the derivatives of the composition of `real.exp` with a differentiable function, for standalone use and use with `simp`. -/ variables {f : ℝ → ℝ} {f' x : ℝ} {s : set ℝ} lemma has_strict_deriv_at.exp (hf : has_strict_deriv_at f f' x) : has_strict_deriv_at (λ x, real.exp (f x)) (real.exp (f x) * f') x := (real.has_strict_deriv_at_exp (f x)).comp x hf lemma has_deriv_at.exp (hf : has_deriv_at f f' x) : has_deriv_at (λ x, real.exp (f x)) (real.exp (f x) * f') x := (real.has_deriv_at_exp (f x)).comp x hf lemma has_deriv_within_at.exp (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, real.exp (f x)) (real.exp (f x) * f') s x := (real.has_deriv_at_exp (f x)).comp_has_deriv_within_at x hf lemma deriv_within_exp (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, real.exp (f x)) s x = real.exp (f x) * (deriv_within f s x) := hf.has_deriv_within_at.exp.deriv_within hxs @[simp] lemma deriv_exp (hc : differentiable_at ℝ f x) : deriv (λx, real.exp (f x)) x = real.exp (f x) * (deriv f x) := hc.has_deriv_at.exp.deriv end section /-! Register lemmas for the derivatives of the composition of `real.exp` with a differentiable function, for standalone use and use with `simp`. -/ variables {E : Type*} [normed_group E] [normed_space ℝ E] {f : E → ℝ} {f' : E →L[ℝ] ℝ} {x : E} {s : set E} lemma times_cont_diff.exp {n} (hf : times_cont_diff ℝ n f) : times_cont_diff ℝ n (λ x, real.exp (f x)) := real.times_cont_diff_exp.comp hf lemma times_cont_diff_at.exp {n} (hf : times_cont_diff_at ℝ n f x) : times_cont_diff_at ℝ n (λ x, real.exp (f x)) x := real.times_cont_diff_exp.times_cont_diff_at.comp x hf lemma times_cont_diff_on.exp {n} (hf : times_cont_diff_on ℝ n f s) : times_cont_diff_on ℝ n (λ x, real.exp (f x)) s := real.times_cont_diff_exp.comp_times_cont_diff_on hf lemma times_cont_diff_within_at.exp {n} (hf : times_cont_diff_within_at ℝ n f s x) : times_cont_diff_within_at ℝ n (λ x, real.exp (f x)) s x := real.times_cont_diff_exp.times_cont_diff_at.comp_times_cont_diff_within_at x hf lemma has_fderiv_within_at.exp (hf : has_fderiv_within_at f f' s x) : has_fderiv_within_at (λ x, real.exp (f x)) (real.exp (f x) • f') s x := (real.has_deriv_at_exp (f x)).comp_has_fderiv_within_at x hf lemma has_fderiv_at.exp (hf : has_fderiv_at f f' x) : has_fderiv_at (λ x, real.exp (f x)) (real.exp (f x) • f') x := (real.has_deriv_at_exp (f x)).comp_has_fderiv_at x hf lemma has_strict_fderiv_at.exp (hf : has_strict_fderiv_at f f' x) : has_strict_fderiv_at (λ x, real.exp (f x)) (real.exp (f x) • f') x := (real.has_strict_deriv_at_exp (f x)).comp_has_strict_fderiv_at x hf lemma differentiable_within_at.exp (hf : differentiable_within_at ℝ f s x) : differentiable_within_at ℝ (λ x, real.exp (f x)) s x := hf.has_fderiv_within_at.exp.differentiable_within_at @[simp] lemma differentiable_at.exp (hc : differentiable_at ℝ f x) : differentiable_at ℝ (λx, real.exp (f x)) x := hc.has_fderiv_at.exp.differentiable_at lemma differentiable_on.exp (hc : differentiable_on ℝ f s) : differentiable_on ℝ (λx, real.exp (f x)) s := λ x h, (hc x h).exp @[simp] lemma differentiable.exp (hc : differentiable ℝ f) : differentiable ℝ (λx, real.exp (f x)) := λ x, (hc x).exp lemma fderiv_within_exp (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : fderiv_within ℝ (λx, real.exp (f x)) s x = real.exp (f x) • (fderiv_within ℝ f s x) := hf.has_fderiv_within_at.exp.fderiv_within hxs @[simp] lemma fderiv_exp (hc : differentiable_at ℝ f x) : fderiv ℝ (λx, real.exp (f x)) x = real.exp (f x) • (fderiv ℝ f x) := hc.has_fderiv_at.exp.fderiv end namespace real variables {x y z : ℝ} /-- The real exponential function tends to `+∞` at `+∞`. -/ lemma tendsto_exp_at_top : tendsto exp at_top at_top := begin have A : tendsto (λx:ℝ, x + 1) at_top at_top := tendsto_at_top_add_const_right at_top 1 tendsto_id, have B : ∀ᶠ x in at_top, x + 1 ≤ exp x := eventually_at_top.2 ⟨0, λx hx, add_one_le_exp_of_nonneg hx⟩, exact tendsto_at_top_mono' at_top B A end /-- The real exponential function tends to `0` at `-∞` or, equivalently, `exp(-x)` tends to `0` at `+∞` -/ lemma tendsto_exp_neg_at_top_nhds_0 : tendsto (λx, exp (-x)) at_top (𝓝 0) := (tendsto_inv_at_top_zero.comp tendsto_exp_at_top).congr (λx, (exp_neg x).symm) /-- The real exponential function tends to `1` at `0`. -/ lemma tendsto_exp_nhds_0_nhds_1 : tendsto exp (𝓝 0) (𝓝 1) := by { convert continuous_exp.tendsto 0, simp } lemma tendsto_exp_at_bot : tendsto exp at_bot (𝓝 0) := (tendsto_exp_neg_at_top_nhds_0.comp tendsto_neg_at_bot_at_top).congr $ λ x, congr_arg exp $ neg_neg x lemma tendsto_exp_at_bot_nhds_within : tendsto exp at_bot (𝓝[Ioi 0] 0) := tendsto_inf.2 ⟨tendsto_exp_at_bot, tendsto_principal.2 $ eventually_of_forall exp_pos⟩ /-- `real.exp` as an order isomorphism between `ℝ` and `(0, +∞)`. -/ def exp_order_iso : ℝ ≃o Ioi (0 : ℝ) := strict_mono.order_iso_of_surjective _ (exp_strict_mono.cod_restrict exp_pos) $ (continuous_subtype_mk _ continuous_exp).surjective (by simp only [tendsto_Ioi_at_top, subtype.coe_mk, tendsto_exp_at_top]) (by simp [tendsto_exp_at_bot_nhds_within]) @[simp] lemma coe_exp_order_iso_apply (x : ℝ) : (exp_order_iso x : ℝ) = exp x := rfl @[simp] lemma coe_comp_exp_order_iso : coe ∘ exp_order_iso = exp := rfl @[simp] lemma range_exp : range exp = Ioi 0 := by rw [← coe_comp_exp_order_iso, range_comp, exp_order_iso.range_eq, image_univ, subtype.range_coe] @[simp] lemma map_exp_at_top : map exp at_top = at_top := by rw [← coe_comp_exp_order_iso, ← filter.map_map, order_iso.map_at_top, map_coe_Ioi_at_top] @[simp] lemma comap_exp_at_top : comap exp at_top = at_top := by rw [← map_exp_at_top, comap_map exp_injective, map_exp_at_top] @[simp] lemma tendsto_exp_comp_at_top {α : Type*} {l : filter α} {f : α → ℝ} : tendsto (λ x, exp (f x)) l at_top ↔ tendsto f l at_top := by rw [← tendsto_comap_iff, comap_exp_at_top] lemma tendsto_comp_exp_at_top {α : Type*} {l : filter α} {f : ℝ → α} : tendsto (λ x, f (exp x)) at_top l ↔ tendsto f at_top l := by rw [← tendsto_map'_iff, map_exp_at_top] @[simp] lemma map_exp_at_bot : map exp at_bot = 𝓝[Ioi 0] 0 := by rw [← coe_comp_exp_order_iso, ← filter.map_map, exp_order_iso.map_at_bot, ← map_coe_Ioi_at_bot] lemma comap_exp_nhds_within_Ioi_zero : comap exp (𝓝[Ioi 0] 0) = at_bot := by rw [← map_exp_at_bot, comap_map exp_injective] lemma tendsto_comp_exp_at_bot {α : Type*} {l : filter α} {f : ℝ → α} : tendsto (λ x, f (exp x)) at_bot l ↔ tendsto f (𝓝[Ioi 0] 0) l := by rw [← map_exp_at_bot, tendsto_map'_iff] /-- The real logarithm function, equal to the inverse of the exponential for `x > 0`, to `log |x|` for `x < 0`, and to `0` for `0`. We use this unconventional extension to `(-∞, 0]` as it gives the formula `log (x * y) = log x + log y` for all nonzero `x` and `y`, and the derivative of `log` is `1/x` away from `0`. -/ @[pp_nodot] noncomputable def log (x : ℝ) : ℝ := if hx : x = 0 then 0 else exp_order_iso.symm ⟨abs x, abs_pos.2 hx⟩ lemma log_of_ne_zero (hx : x ≠ 0) : log x = exp_order_iso.symm ⟨abs x, abs_pos.2 hx⟩ := dif_neg hx lemma log_of_pos (hx : 0 < x) : log x = exp_order_iso.symm ⟨x, hx⟩ := by { rw [log_of_ne_zero hx.ne'], congr, exact abs_of_pos hx } lemma exp_log_eq_abs (hx : x ≠ 0) : exp (log x) = abs x := by rw [log_of_ne_zero hx, ← coe_exp_order_iso_apply, order_iso.apply_symm_apply, subtype.coe_mk] lemma exp_log (hx : 0 < x) : exp (log x) = x := by { rw exp_log_eq_abs hx.ne', exact abs_of_pos hx } lemma exp_log_of_neg (hx : x < 0) : exp (log x) = -x := by { rw exp_log_eq_abs (ne_of_lt hx), exact abs_of_neg hx } @[simp] lemma log_exp (x : ℝ) : log (exp x) = x := exp_injective $ exp_log (exp_pos x) lemma surj_on_log : surj_on log (Ioi 0) univ := λ x _, ⟨exp x, exp_pos x, log_exp x⟩ lemma log_surjective : surjective log := λ x, ⟨exp x, log_exp x⟩ @[simp] lemma range_log : range log = univ := log_surjective.range_eq @[simp] lemma log_zero : log 0 = 0 := dif_pos rfl @[simp] lemma log_one : log 1 = 0 := exp_injective $ by rw [exp_log zero_lt_one, exp_zero] @[simp] lemma log_abs (x : ℝ) : log (abs x) = log x := begin by_cases h : x = 0, { simp [h] }, { rw [← exp_eq_exp, exp_log_eq_abs h, exp_log_eq_abs (abs_pos.2 h).ne', abs_abs] } end @[simp] lemma log_neg_eq_log (x : ℝ) : log (-x) = log x := by rw [← log_abs x, ← log_abs (-x), abs_neg] lemma surj_on_log' : surj_on log (Iio 0) univ := λ x _, ⟨-exp x, neg_lt_zero.2 $ exp_pos x, by rw [log_neg_eq_log, log_exp]⟩ lemma log_mul (hx : x ≠ 0) (hy : y ≠ 0) : log (x * y) = log x + log y := exp_injective $ by rw [exp_log_eq_abs (mul_ne_zero hx hy), exp_add, exp_log_eq_abs hx, exp_log_eq_abs hy, abs_mul] lemma log_div (hx : x ≠ 0) (hy : y ≠ 0) : log (x / y) = log x - log y := exp_injective $ by rw [exp_log_eq_abs (div_ne_zero hx hy), exp_sub, exp_log_eq_abs hx, exp_log_eq_abs hy, abs_div] @[simp] lemma log_inv (x : ℝ) : log (x⁻¹) = -log x := begin by_cases hx : x = 0, { simp [hx] }, rw [← exp_eq_exp, exp_log_eq_abs (inv_ne_zero hx), exp_neg, exp_log_eq_abs hx, abs_inv] end lemma log_le_log (h : 0 < x) (h₁ : 0 < y) : real.log x ≤ real.log y ↔ x ≤ y := by rw [← exp_le_exp, exp_log h, exp_log h₁] lemma log_lt_log (hx : 0 < x) : x < y → log x < log y := by { intro h, rwa [← exp_lt_exp, exp_log hx, exp_log (lt_trans hx h)] } lemma log_lt_log_iff (hx : 0 < x) (hy : 0 < y) : log x < log y ↔ x < y := by { rw [← exp_lt_exp, exp_log hx, exp_log hy] } lemma log_pos_iff (hx : 0 < x) : 0 < log x ↔ 1 < x := by { rw ← log_one, exact log_lt_log_iff zero_lt_one hx } lemma log_pos (hx : 1 < x) : 0 < log x := (log_pos_iff (lt_trans zero_lt_one hx)).2 hx lemma log_neg_iff (h : 0 < x) : log x < 0 ↔ x < 1 := by { rw ← log_one, exact log_lt_log_iff h zero_lt_one } lemma log_neg (h0 : 0 < x) (h1 : x < 1) : log x < 0 := (log_neg_iff h0).2 h1 lemma log_nonneg_iff (hx : 0 < x) : 0 ≤ log x ↔ 1 ≤ x := by rw [← not_lt, log_neg_iff hx, not_lt] lemma log_nonneg (hx : 1 ≤ x) : 0 ≤ log x := (log_nonneg_iff (zero_lt_one.trans_le hx)).2 hx lemma log_nonpos_iff (hx : 0 < x) : log x ≤ 0 ↔ x ≤ 1 := by rw [← not_lt, log_pos_iff hx, not_lt] lemma log_nonpos_iff' (hx : 0 ≤ x) : log x ≤ 0 ↔ x ≤ 1 := begin rcases hx.eq_or_lt with (rfl|hx), { simp [le_refl, zero_le_one] }, exact log_nonpos_iff hx end lemma log_nonpos (hx : 0 ≤ x) (h'x : x ≤ 1) : log x ≤ 0 := (log_nonpos_iff' hx).2 h'x lemma strict_mono_incr_on_log : strict_mono_incr_on log (set.Ioi 0) := λ x hx y hy hxy, log_lt_log hx hxy lemma strict_mono_decr_on_log : strict_mono_decr_on log (set.Iio 0) := begin rintros x (hx : x < 0) y (hy : y < 0) hxy, rw [← log_abs y, ← log_abs x], refine log_lt_log (abs_pos.2 hy.ne) _, rwa [abs_of_neg hy, abs_of_neg hx, neg_lt_neg_iff] end lemma log_inj_on_pos : set.inj_on log (set.Ioi 0) := strict_mono_incr_on_log.inj_on lemma eq_one_of_pos_of_log_eq_zero {x : ℝ} (h₁ : 0 < x) (h₂ : log x = 0) : x = 1 := log_inj_on_pos (set.mem_Ioi.2 h₁) (set.mem_Ioi.2 zero_lt_one) (h₂.trans real.log_one.symm) lemma log_ne_zero_of_pos_of_ne_one {x : ℝ} (hx_pos : 0 < x) (hx : x ≠ 1) : log x ≠ 0 := mt (eq_one_of_pos_of_log_eq_zero hx_pos) hx /-- The real logarithm function tends to `+∞` at `+∞`. -/ lemma tendsto_log_at_top : tendsto log at_top at_top := tendsto_comp_exp_at_top.1 $ by simpa only [log_exp] using tendsto_id lemma tendsto_log_nhds_within_zero : tendsto log (𝓝[{0}ᶜ] 0) at_bot := begin rw [← (show _ = log, from funext log_abs)], refine tendsto.comp _ tendsto_abs_nhds_within_zero, simpa [← tendsto_comp_exp_at_bot] using tendsto_id end lemma continuous_on_log : continuous_on log {0}ᶜ := begin rw [continuous_on_iff_continuous_restrict, restrict], conv in (log _) { rw [log_of_ne_zero (show (x : ℝ) ≠ 0, from x.2)] }, exact exp_order_iso.symm.continuous.comp (continuous_subtype_mk _ continuous_subtype_coe.norm) end @[continuity] lemma continuous_log : continuous (λ x : {x : ℝ // x ≠ 0}, log x) := continuous_on_iff_continuous_restrict.1 $ continuous_on_log.mono $ λ x hx, hx @[continuity] lemma continuous_log' : continuous (λ x : {x : ℝ // 0 < x}, log x) := continuous_on_iff_continuous_restrict.1 $ continuous_on_log.mono $ λ x hx, ne_of_gt hx lemma continuous_at_log (hx : x ≠ 0) : continuous_at log x := (continuous_on_log x hx).continuous_at $ is_open.mem_nhds is_open_compl_singleton hx @[simp] lemma continuous_at_log_iff : continuous_at log x ↔ x ≠ 0 := begin refine ⟨_, continuous_at_log⟩, rintros h rfl, exact not_tendsto_nhds_of_tendsto_at_bot tendsto_log_nhds_within_zero _ (h.tendsto.mono_left inf_le_left) end lemma has_strict_deriv_at_log_of_pos (hx : 0 < x) : has_strict_deriv_at log x⁻¹ x := have has_strict_deriv_at log (exp $ log x)⁻¹ x, from (has_strict_deriv_at_exp $ log x).of_local_left_inverse (continuous_at_log hx.ne') (ne_of_gt $ exp_pos _) $ eventually.mono (lt_mem_nhds hx) @exp_log, by rwa [exp_log hx] at this lemma has_strict_deriv_at_log (hx : x ≠ 0) : has_strict_deriv_at log x⁻¹ x := begin cases hx.lt_or_lt with hx hx, { convert (has_strict_deriv_at_log_of_pos (neg_pos.mpr hx)).comp x (has_strict_deriv_at_neg x), { ext y, exact (log_neg_eq_log y).symm }, { field_simp [hx.ne] } }, { exact has_strict_deriv_at_log_of_pos hx } end lemma has_deriv_at_log (hx : x ≠ 0) : has_deriv_at log x⁻¹ x := (has_strict_deriv_at_log hx).has_deriv_at lemma differentiable_at_log (hx : x ≠ 0) : differentiable_at ℝ log x := (has_deriv_at_log hx).differentiable_at lemma differentiable_on_log : differentiable_on ℝ log {0}ᶜ := λ x hx, (differentiable_at_log hx).differentiable_within_at @[simp] lemma differentiable_at_log_iff : differentiable_at ℝ log x ↔ x ≠ 0 := ⟨λ h, continuous_at_log_iff.1 h.continuous_at, differentiable_at_log⟩ lemma deriv_log (x : ℝ) : deriv log x = x⁻¹ := if hx : x = 0 then by rw [deriv_zero_of_not_differentiable_at (mt differentiable_at_log_iff.1 (not_not.2 hx)), hx, inv_zero] else (has_deriv_at_log hx).deriv @[simp] lemma deriv_log' : deriv log = has_inv.inv := funext deriv_log lemma times_cont_diff_on_log {n : with_top ℕ} : times_cont_diff_on ℝ n log {0}ᶜ := begin suffices : times_cont_diff_on ℝ ⊤ log {0}ᶜ, from this.of_le le_top, refine (times_cont_diff_on_top_iff_deriv_of_open is_open_compl_singleton).2 _, simp [differentiable_on_log, times_cont_diff_on_inv] end lemma times_cont_diff_at_log {n : with_top ℕ} : times_cont_diff_at ℝ n log x ↔ x ≠ 0 := ⟨λ h, continuous_at_log_iff.1 h.continuous_at, λ hx, (times_cont_diff_on_log x hx).times_cont_diff_at $ is_open.mem_nhds is_open_compl_singleton hx⟩ end real section log_differentiable open real section continuity variables {α : Type*} lemma filter.tendsto.log {f : α → ℝ} {l : filter α} {x : ℝ} (h : tendsto f l (𝓝 x)) (hx : x ≠ 0) : tendsto (λ x, log (f x)) l (𝓝 (log x)) := (continuous_at_log hx).tendsto.comp h variables [topological_space α] {f : α → ℝ} {s : set α} {a : α} lemma continuous.log (hf : continuous f) (h₀ : ∀ x, f x ≠ 0) : continuous (λ x, log (f x)) := continuous_on_log.comp_continuous hf h₀ lemma continuous_at.log (hf : continuous_at f a) (h₀ : f a ≠ 0) : continuous_at (λ x, log (f x)) a := hf.log h₀ lemma continuous_within_at.log (hf : continuous_within_at f s a) (h₀ : f a ≠ 0) : continuous_within_at (λ x, log (f x)) s a := hf.log h₀ lemma continuous_on.log (hf : continuous_on f s) (h₀ : ∀ x ∈ s, f x ≠ 0) : continuous_on (λ x, log (f x)) s := λ x hx, (hf x hx).log (h₀ x hx) end continuity section deriv variables {f : ℝ → ℝ} {x f' : ℝ} {s : set ℝ} lemma has_deriv_within_at.log (hf : has_deriv_within_at f f' s x) (hx : f x ≠ 0) : has_deriv_within_at (λ y, log (f y)) (f' / (f x)) s x := begin rw div_eq_inv_mul, exact (has_deriv_at_log hx).comp_has_deriv_within_at x hf end lemma has_deriv_at.log (hf : has_deriv_at f f' x) (hx : f x ≠ 0) : has_deriv_at (λ y, log (f y)) (f' / f x) x := begin rw ← has_deriv_within_at_univ at *, exact hf.log hx end lemma has_strict_deriv_at.log (hf : has_strict_deriv_at f f' x) (hx : f x ≠ 0) : has_strict_deriv_at (λ y, log (f y)) (f' / f x) x := begin rw div_eq_inv_mul, exact (has_strict_deriv_at_log hx).comp x hf end lemma deriv_within.log (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, log (f x)) s x = (deriv_within f s x) / (f x) := (hf.has_deriv_within_at.log hx).deriv_within hxs @[simp] lemma deriv.log (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : deriv (λx, log (f x)) x = (deriv f x) / (f x) := (hf.has_deriv_at.log hx).deriv end deriv section fderiv variables {E : Type*} [normed_group E] [normed_space ℝ E] {f : E → ℝ} {x : E} {f' : E →L[ℝ] ℝ} {s : set E} lemma has_fderiv_within_at.log (hf : has_fderiv_within_at f f' s x) (hx : f x ≠ 0) : has_fderiv_within_at (λ x, log (f x)) ((f x)⁻¹ • f') s x := (has_deriv_at_log hx).comp_has_fderiv_within_at x hf lemma has_fderiv_at.log (hf : has_fderiv_at f f' x) (hx : f x ≠ 0) : has_fderiv_at (λ x, log (f x)) ((f x)⁻¹ • f') x := (has_deriv_at_log hx).comp_has_fderiv_at x hf lemma has_strict_fderiv_at.log (hf : has_strict_fderiv_at f f' x) (hx : f x ≠ 0) : has_strict_fderiv_at (λ x, log (f x)) ((f x)⁻¹ • f') x := (has_strict_deriv_at_log hx).comp_has_strict_fderiv_at x hf lemma differentiable_within_at.log (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) : differentiable_within_at ℝ (λx, log (f x)) s x := (hf.has_fderiv_within_at.log hx).differentiable_within_at @[simp] lemma differentiable_at.log (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : differentiable_at ℝ (λx, log (f x)) x := (hf.has_fderiv_at.log hx).differentiable_at lemma times_cont_diff_at.log {n} (hf : times_cont_diff_at ℝ n f x) (hx : f x ≠ 0) : times_cont_diff_at ℝ n (λ x, log (f x)) x := (times_cont_diff_at_log.2 hx).comp x hf lemma times_cont_diff_within_at.log {n} (hf : times_cont_diff_within_at ℝ n f s x) (hx : f x ≠ 0) : times_cont_diff_within_at ℝ n (λ x, log (f x)) s x := (times_cont_diff_at_log.2 hx).comp_times_cont_diff_within_at x hf lemma times_cont_diff_on.log {n} (hf : times_cont_diff_on ℝ n f s) (hs : ∀ x ∈ s, f x ≠ 0) : times_cont_diff_on ℝ n (λ x, log (f x)) s := λ x hx, (hf x hx).log (hs x hx) lemma times_cont_diff.log {n} (hf : times_cont_diff ℝ n f) (h : ∀ x, f x ≠ 0) : times_cont_diff ℝ n (λ x, log (f x)) := times_cont_diff_iff_times_cont_diff_at.2 $ λ x, hf.times_cont_diff_at.log (h x) lemma differentiable_on.log (hf : differentiable_on ℝ f s) (hx : ∀ x ∈ s, f x ≠ 0) : differentiable_on ℝ (λx, log (f x)) s := λx h, (hf x h).log (hx x h) @[simp] lemma differentiable.log (hf : differentiable ℝ f) (hx : ∀ x, f x ≠ 0) : differentiable ℝ (λx, log (f x)) := λx, (hf x).log (hx x) lemma fderiv_within.log (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) (hxs : unique_diff_within_at ℝ s x) : fderiv_within ℝ (λx, log (f x)) s x = (f x)⁻¹ • fderiv_within ℝ f s x := (hf.has_fderiv_within_at.log hx).fderiv_within hxs @[simp] lemma fderiv.log (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : fderiv ℝ (λx, log (f x)) x = (f x)⁻¹ • fderiv ℝ f x := (hf.has_fderiv_at.log hx).fderiv end fderiv end log_differentiable namespace real /-- The function `exp(x)/x^n` tends to `+∞` at `+∞`, for any natural number `n` -/ lemma tendsto_exp_div_pow_at_top (n : ℕ) : tendsto (λx, exp x / x^n) at_top at_top := begin refine (at_top_basis_Ioi.tendsto_iff (at_top_basis' 1)).2 (λ C hC₁, _), have hC₀ : 0 < C, from zero_lt_one.trans_le hC₁, have : 0 < (exp 1 * C)⁻¹ := inv_pos.2 (mul_pos (exp_pos _) hC₀), obtain ⟨N, hN⟩ : ∃ N, ∀ k ≥ N, (↑k ^ n : ℝ) / exp 1 ^ k < (exp 1 * C)⁻¹ := eventually_at_top.1 ((tendsto_pow_const_div_const_pow_of_one_lt n (one_lt_exp_iff.2 zero_lt_one)).eventually (gt_mem_nhds this)), simp only [← exp_nat_mul, mul_one, div_lt_iff, exp_pos, ← div_eq_inv_mul] at hN, refine ⟨N, trivial, λ x hx, _⟩, rw mem_Ioi at hx, have hx₀ : 0 < x, from N.cast_nonneg.trans_lt hx, rw [mem_Ici, le_div_iff (pow_pos hx₀ _), ← le_div_iff' hC₀], calc x ^ n ≤ (nat_ceil x) ^ n : pow_le_pow_of_le_left hx₀.le (le_nat_ceil _) _ ... ≤ exp (nat_ceil x) / (exp 1 * C) : (hN _ (lt_nat_ceil.2 hx).le).le ... ≤ exp (x + 1) / (exp 1 * C) : div_le_div_of_le (mul_pos (exp_pos _) hC₀).le (exp_le_exp.2 $ (nat_ceil_lt_add_one hx₀.le).le) ... = exp x / C : by rw [add_comm, exp_add, mul_div_mul_left _ _ (exp_pos _).ne'] end /-- The function `x^n * exp(-x)` tends to `0` at `+∞`, for any natural number `n`. -/ lemma tendsto_pow_mul_exp_neg_at_top_nhds_0 (n : ℕ) : tendsto (λx, x^n * exp (-x)) at_top (𝓝 0) := (tendsto_inv_at_top_zero.comp (tendsto_exp_div_pow_at_top n)).congr $ λx, by rw [comp_app, inv_eq_one_div, div_div_eq_mul_div, one_mul, div_eq_mul_inv, exp_neg] /-- The function `(b * exp x + c) / (x ^ n)` tends to `+∞` at `+∞`, for any positive natural number `n` and any real numbers `b` and `c` such that `b` is positive. -/ lemma tendsto_mul_exp_add_div_pow_at_top (b c : ℝ) (n : ℕ) (hb : 0 < b) (hn : 1 ≤ n) : tendsto (λ x, (b * (exp x) + c) / (x^n)) at_top at_top := begin refine tendsto.congr' (eventually_eq_of_mem (Ioi_mem_at_top 0) _) (((tendsto_exp_div_pow_at_top n).const_mul_at_top hb).at_top_add ((tendsto_pow_neg_at_top hn).mul (@tendsto_const_nhds _ _ _ c _))), intros x hx, simp only [fpow_neg x n], ring, end /-- The function `(x ^ n) / (b * exp x + c)` tends to `0` at `+∞`, for any positive natural number `n` and any real numbers `b` and `c` such that `b` is nonzero. -/ lemma tendsto_div_pow_mul_exp_add_at_top (b c : ℝ) (n : ℕ) (hb : 0 ≠ b) (hn : 1 ≤ n) : tendsto (λ x, x^n / (b * (exp x) + c)) at_top (𝓝 0) := begin have H : ∀ d e, 0 < d → tendsto (λ (x:ℝ), x^n / (d * (exp x) + e)) at_top (𝓝 0), { intros b' c' h, convert (tendsto_mul_exp_add_div_pow_at_top b' c' n h hn).inv_tendsto_at_top , ext x, simpa only [pi.inv_apply] using inv_div.symm }, cases lt_or_gt_of_ne hb, { exact H b c h }, { convert (H (-b) (-c) (neg_pos.mpr h)).neg, { ext x, field_simp, rw [← neg_add (b * exp x) c, neg_div_neg_eq] }, { exact neg_zero.symm } }, end /-- The function `x * log (1 + t / x)` tends to `t` at `+∞`. -/ lemma tendsto_mul_log_one_plus_div_at_top (t : ℝ) : tendsto (λ x, x * log (1 + t / x)) at_top (𝓝 t) := begin have h₁ : tendsto (λ h, h⁻¹ * log (1 + t * h)) (𝓝[{0}ᶜ] 0) (𝓝 t), { simpa [has_deriv_at_iff_tendsto_slope] using ((has_deriv_at_const _ 1).add ((has_deriv_at_id 0).const_mul t)).log (by simp) }, have h₂ : tendsto (λ x : ℝ, x⁻¹) at_top (𝓝[{0}ᶜ] 0) := tendsto_inv_at_top_zero'.mono_right (nhds_within_mono _ (λ x hx, (set.mem_Ioi.mp hx).ne')), convert h₁.comp h₂, ext, field_simp [mul_comm], end open_locale big_operators /-- A crude lemma estimating the difference between `log (1-x)` and its Taylor series at `0`, where the main point of the bound is that it tends to `0`. The goal is to deduce the series expansion of the logarithm, in `has_sum_pow_div_log_of_abs_lt_1`. -/ lemma abs_log_sub_add_sum_range_le {x : ℝ} (h : abs x < 1) (n : ℕ) : abs ((∑ i in range n, x^(i+1)/(i+1)) + log (1-x)) ≤ (abs x)^(n+1) / (1 - abs x) := begin /- For the proof, we show that the derivative of the function to be estimated is small, and then apply the mean value inequality. -/ let F : ℝ → ℝ := λ x, ∑ i in range n, x^(i+1)/(i+1) + log (1-x), -- First step: compute the derivative of `F` have A : ∀ y ∈ Ioo (-1 : ℝ) 1, deriv F y = - (y^n) / (1 - y), { assume y hy, have : (∑ i in range n, (↑i + 1) * y ^ i / (↑i + 1)) = (∑ i in range n, y ^ i), { congr' with i, have : (i : ℝ) + 1 ≠ 0 := ne_of_gt (nat.cast_add_one_pos i), field_simp [this, mul_comm] }, field_simp [F, this, ← geom_sum_def, geom_sum_eq (ne_of_lt hy.2), sub_ne_zero_of_ne (ne_of_gt hy.2), sub_ne_zero_of_ne (ne_of_lt hy.2)], ring }, -- second step: show that the derivative of `F` is small have B : ∀ y ∈ Icc (-abs x) (abs x), abs (deriv F y) ≤ (abs x)^n / (1 - abs x), { assume y hy, have : y ∈ Ioo (-(1 : ℝ)) 1 := ⟨lt_of_lt_of_le (neg_lt_neg h) hy.1, lt_of_le_of_lt hy.2 h⟩, calc abs (deriv F y) = abs (-(y^n) / (1 - y)) : by rw [A y this] ... ≤ (abs x)^n / (1 - abs x) : begin have : abs y ≤ abs x := abs_le.2 hy, have : 0 < 1 - abs x, by linarith, have : 1 - abs x ≤ abs (1 - y) := le_trans (by linarith [hy.2]) (le_abs_self _), simp only [← pow_abs, abs_div, abs_neg], apply_rules [div_le_div, pow_nonneg, abs_nonneg, pow_le_pow_of_le_left] end }, -- third step: apply the mean value inequality have C : ∥F x - F 0∥ ≤ ((abs x)^n / (1 - abs x)) * ∥x - 0∥, { have : ∀ y ∈ Icc (- abs x) (abs x), differentiable_at ℝ F y, { assume y hy, have : 1 - y ≠ 0 := sub_ne_zero_of_ne (ne_of_gt (lt_of_le_of_lt hy.2 h)), simp [F, this] }, apply convex.norm_image_sub_le_of_norm_deriv_le this B (convex_Icc _ _) _ _, { simpa using abs_nonneg x }, { simp [le_abs_self x, neg_le.mp (neg_le_abs_self x)] } }, -- fourth step: conclude by massaging the inequality of the third step simpa [F, norm_eq_abs, div_mul_eq_mul_div, pow_succ'] using C end /-- Power series expansion of the logarithm around `1`. -/ theorem has_sum_pow_div_log_of_abs_lt_1 {x : ℝ} (h : abs x < 1) : has_sum (λ (n : ℕ), x ^ (n + 1) / (n + 1)) (-log (1 - x)) := begin rw summable.has_sum_iff_tendsto_nat, show tendsto (λ (n : ℕ), ∑ (i : ℕ) in range n, x ^ (i + 1) / (i + 1)) at_top (𝓝 (-log (1 - x))), { rw [tendsto_iff_norm_tendsto_zero], simp only [norm_eq_abs, sub_neg_eq_add], refine squeeze_zero (λ n, abs_nonneg _) (abs_log_sub_add_sum_range_le h) _, suffices : tendsto (λ (t : ℕ), abs x ^ (t + 1) / (1 - abs x)) at_top (𝓝 (abs x * 0 / (1 - abs x))), by simpa, simp only [pow_succ], refine (tendsto_const_nhds.mul _).div_const, exact tendsto_pow_at_top_nhds_0_of_lt_1 (abs_nonneg _) h }, show summable (λ (n : ℕ), x ^ (n + 1) / (n + 1)), { refine summable_of_norm_bounded _ (summable_geometric_of_lt_1 (abs_nonneg _) h) (λ i, _), calc ∥x ^ (i + 1) / (i + 1)∥ = abs x ^ (i+1) / (i+1) : begin have : (0 : ℝ) ≤ i + 1 := le_of_lt (nat.cast_add_one_pos i), rw [norm_eq_abs, abs_div, ← pow_abs, abs_of_nonneg this], end ... ≤ abs x ^ (i+1) / (0 + 1) : begin apply_rules [div_le_div_of_le_left, pow_nonneg, abs_nonneg, add_le_add_right, i.cast_nonneg], norm_num, end ... ≤ abs x ^ i : by simpa [pow_succ'] using mul_le_of_le_one_right (pow_nonneg (abs_nonneg x) i) (le_of_lt h) } end end real
006c34e11881a69742658afedcaa0f01f00c79bb
5719a16e23dfc08cdea7a5bf035b81690f307965
/src/Init/Control/Lift.lean
1b7549edbb724b10f7e7a6b7172c8337be0a819c
[ "Apache-2.0" ]
permissive
postmasters/lean4
488b03969a371e1507e1e8a4df9ebf63c7cbe7ac
f3976fc53a883ac7606fc59357d43f4b51016ca7
refs/heads/master
1,655,582,707,480
1,588,682,595,000
1,588,682,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,416
lean
/- Copyright (c) 2016 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner, Sebastian Ullrich Classy functions for lifting monadic actions of different shapes. This theory is roughly modeled after the Haskell 'layers' package https://hackage.haskell.org/package/layers-0.1. Please see https://hackage.haskell.org/package/layers-0.1/docs/Documentation-Layers-Overview.html for an exhaustive discussion of the different approaches to lift functions. -/ prelude import Init.Control.Monad import Init.Coe universes u v w /-- A Function for lifting a computation from an inner Monad to an outer Monad. Like [MonadTrans](https://hackage.haskell.org/package/transformers-0.5.5.0/docs/Control-Monad-Trans-Class.html), but `n` does not have to be a monad transformer. Alternatively, an implementation of [MonadLayer](https://hackage.haskell.org/package/layers-0.1/docs/Control-Monad-Layer.html#t:MonadLayer) without `layerInvmap` (so far). -/ class HasMonadLift (m : Type u → Type v) (n : Type u → Type w) := (monadLift {} : ∀ {α}, m α → n α) /-- The reflexive-transitive closure of `HasMonadLift`. `monadLift` is used to transitively lift monadic computations such as `StateT.get` or `StateT.put s`. Corresponds to [MonadLift](https://hackage.haskell.org/package/layers-0.1/docs/Control-Monad-Layer.html#t:MonadLift). -/ class HasMonadLiftT (m : Type u → Type v) (n : Type u → Type w) := (monadLift {} : ∀ {α}, m α → n α) export HasMonadLiftT (monadLift) abbrev liftM := @monadLift @[inline] def liftCoeM {m : Type u → Type v} {n : Type u → Type w} {α β : Type u} [HasMonadLiftT m n] [∀ a, CoeT α a β] [Monad n] (x : m α) : n β := do a ← liftM $ x; pure $ coe a instance hasMonadLiftTTrans (m n o) [HasMonadLiftT m n] [HasMonadLift n o] : HasMonadLiftT m o := ⟨fun α ma => HasMonadLift.monadLift (monadLift ma : n α)⟩ instance hasMonadLiftTRefl (m) : HasMonadLiftT m m := ⟨fun α => id⟩ theorem monadLiftRefl {m : Type u → Type v} {α} : (monadLift : m α → m α) = id := rfl /-- A functor in the category of monads. Can be used to lift monad-transforming functions. Based on pipes' [MFunctor](https://hackage.haskell.org/package/pipes-2.4.0/docs/Control-MFunctor.html), but not restricted to monad transformers. Alternatively, an implementation of [MonadTransFunctor](http://duairc.netsoc.ie/layers-docs/Control-Monad-Layer.html#t:MonadTransFunctor). Remark: other libraries equate `m` and `m'`, and `n` and `n'`. We need to distinguish them to be able to implement gadgets such as `MonadStateAdapter` and `MonadReaderAdapter`. -/ class MonadFunctor (m m' : Type u → Type v) (n n' : Type u → Type w) := (monadMap {} {α : Type u} : (∀ {β}, m β → m' β) → n α → n' α) /-- The reflexive-transitive closure of `MonadFunctor`. `monadMap` is used to transitively lift Monad morphisms such as `StateT.zoom`. A generalization of [MonadLiftFunctor](http://duairc.netsoc.ie/layers-docs/Control-Monad-Layer.html#t:MonadLiftFunctor), which can only lift endomorphisms (i.e. m = m', n = n'). -/ class MonadFunctorT (m m' : Type u → Type v) (n n' : Type u → Type w) := (monadMap {} {α : Type u} : (∀ {β}, m β → m' β) → n α → n' α) export MonadFunctorT (monadMap) instance monadFunctorTTrans (m m' n n' o o') [MonadFunctorT m m' n n'] [MonadFunctor n n' o o'] : MonadFunctorT m m' o o' := ⟨fun α f => MonadFunctor.monadMap (fun β => (monadMap @f : n β → n' β))⟩ instance monadFunctorTRefl (m m') : MonadFunctorT m m' m m' := ⟨fun α f => f⟩ theorem monadMapRefl {m m' : Type u → Type v} (f : ∀ {β}, m β → m' β) {α} : (monadMap @f : m α → m' α) = f := rfl /-- Run a Monad stack to completion. `run` should be the composition of the transformers' individual `run` functions. This class mostly saves some typing when using highly nested Monad stacks: ``` @[reducible] def MyMonad := ReaderT myCfg $ StateT myState $ ExceptT myErr id -- def MyMonad.run {α : Type} (x : MyMonad α) (cfg : myCfg) (st : myState) := ((x.run cfg).run st).run def MyMonad.run {α : Type} (x : MyMonad α) := MonadRun.run x ``` -/ class MonadRun (out : outParam $ Type u → Type v) (m : Type u → Type v) := (run {} {α : Type u} : m α → out α) export MonadRun (run)
b89f566845f6d515d2bf19b37c75fc04933a6509
ebf7140a9ea507409ff4c994124fa36e79b4ae35
/src/hints/category_theory/exercise3/hint8.lean
f64f783e3f0a4afdbc53849b90bd2072600f5127
[]
no_license
fundou/lftcm2020
3e88d58a92755ea5dd49f19c36239c35286ecf5e
99d11bf3bcd71ffeaef0250caa08ecc46e69b55b
refs/heads/master
1,685,610,799,304
1,624,070,416,000
1,624,070,416,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
444
lean
import category_theory.equivalence open category_theory variables {C : Type*} [category C] variables {D : Type*} [category D] lemma equiv_preserves_mono {X Y : C} (f : X ⟶ Y) [mono f] (e : C ≌ D) : mono (e.functor.map f) := begin tidy, replace w := congr_arg (λ k, e.inverse.map k) w, simp at w, rw [←category.assoc, ←category.assoc, cancel_mono f] at w, -- Should be easy from here? See if `simp` can help. sorry end
399ad3ceb59af55aa3927012b4863c80cffa1d21
957a80ea22c5abb4f4670b250d55534d9db99108
/library/init/meta/hole_command.lean
dfbbe190b45938693a26f320c3f41e72b5ae4066
[ "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,970
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 -/ prelude import init.meta.tactic /-- The front-end (e.g., Emacs, VS Code) can invoke commands for holes {! ... !} in a declaration. A command is a tactic that takes zero or more pre-terms in the hole, and returns a list of pair (s, descr) where 's' is a substitution and 'descr' is a short explanation for the substitution. Each string 's' represents a different way to fill the hole. The front-end is responsible for replacing the hole with the string/alternative selected by the user. This infra-structure can be use to implement auto-fill and/or refine commands. An action may return an empty list. This is useful for actions that just return information such as: the type of an expression, its normal form, etc. -/ meta structure hole_command := (name : string) (descr : string) (action : list pexpr → tactic (list (string × string))) open tactic @[hole_command] meta def infer_type_cmd : hole_command := { name := "Infer", descr := "Infer type of the expression in the hole", action := λ ps, do [p] ← return ps | fail "Infer command failed, the hole must contain a single term", e ← to_expr p, t ← infer_type e, trace t, return [] } @[hole_command] meta def show_goal_cmd : hole_command := { name := "Show", descr := "Show the current goal", action := λ _, do trace_state, return [] } @[hole_command] meta def use_cmd : hole_command := { name := "Use", descr := "Try to fill the hole using the given argument", action := λ ps, do [p] ← return ps | fail "Use command failed, the hole must contain a single term", t ← target, e ← to_expr ``(%%p : %%t), ty ← infer_type e, is_def_eq t ty, fmt ← tactic_format_expr e, o ← get_options, let s := fmt.to_string o, return [(s, "")] }
811a73ebb334d951529491dd7cdb6fb82e7f8a6d
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/group_with_zero/basic.lean
bc47e022764d61d6acd458db55d289ef3933622d
[ "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
13,169
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.group.basic import algebra.group_with_zero.defs import algebra.group.order_synonym /-! # Groups with an adjoined zero element > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > https://github.com/leanprover-community/mathlib4/pull/669 > Any changes to this file require a corresponding PR to mathlib4. 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`. -/ open_locale classical open function variables {α M₀ G₀ M₀' G₀' F F' : Type*} section section mul_zero_class variables [mul_zero_class M₀] {a b : M₀} 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 section has_mul variables [has_mul M₀] [has_zero M₀] [no_zero_divisors M₀] {a b : M₀} lemma eq_zero_of_mul_self_eq_zero (h : a * a = 0) : a = 0 := (eq_zero_or_eq_zero_of_mul_eq_zero h).elim id id @[field_simps] theorem mul_ne_zero (ha : a ≠ 0) (hb : b ≠ 0) : a * b ≠ 0 := mt eq_zero_or_eq_zero_of_mul_eq_zero $ not_or_distrib.mpr ⟨ha, hb⟩ end has_mul namespace ne_zero instance mul [has_zero M₀] [has_mul M₀] [no_zero_divisors M₀] {x y : M₀} [ne_zero x] [ne_zero y] : ne_zero (x * y) := ⟨mul_ne_zero out out⟩ end ne_zero end section variables [mul_zero_one_class M₀] /-- 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₀} 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 end 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_left_injective₀ hc).eq_iff lemma mul_right_inj' (ha : a ≠ 0) : a * b = a * c ↔ b = c := (mul_right_injective₀ ha).eq_iff @[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 /-- 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 group_with_zero variables [group_with_zero G₀] {a b c g h x : G₀} @[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] private lemma inv_eq_of_mul (h : a * b = 1) : a⁻¹ = b := by rw [← inv_mul_cancel_left₀ (left_ne_zero_of_mul_eq_one h) b, h, mul_one] @[priority 100] -- See note [lower instance priority] instance group_with_zero.to_division_monoid : division_monoid G₀ := { inv := has_inv.inv, inv_inv := λ a, begin by_cases h : a = 0, { simp [h] }, { exact left_inv_eq_right_inv (inv_mul_cancel $ inv_ne_zero h) (inv_mul_cancel h) } end, mul_inv_rev := λ a b, begin by_cases ha : a = 0, { simp [ha] }, by_cases hb : b = 0, { simp [hb] }, refine inv_eq_of_mul _, simp [mul_assoc, ha, hb] end, inv_eq_of_mul := λ a b, inv_eq_of_mul, ..‹group_with_zero G₀› } end group_with_zero section group_with_zero variables [group_with_zero G₀] {a b c : G₀} @[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] /-- 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] 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 one_div_ne_zero {a : G₀} (h : a ≠ 0) : 1 / a ≠ 0 := by simpa only [one_div] using inv_ne_zero h @[simp] lemma inv_eq_zero {a : G₀} : a⁻¹ = 0 ↔ a = 0 := by rw [inv_eq_iff_inv_eq, inv_zero, eq_comm] @[simp] lemma zero_eq_inv {a : G₀} : 0 = a⁻¹ ↔ 0 = a := eq_comm.trans $ inv_eq_zero.trans eq_comm /-- 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 mul_left_surjective₀ {a : G₀} (h : a ≠ 0) : surjective (λ g, a * g) := λ g, ⟨a⁻¹ * g, by simp [← mul_assoc, mul_inv_cancel h]⟩ lemma mul_right_surjective₀ {a : G₀} (h : a ≠ 0) : surjective (λ g, g * a) := λ g, ⟨g * a⁻¹, by simp [mul_assoc, inv_mul_cancel h]⟩ end group_with_zero section comm_group_with_zero variables [comm_group_with_zero G₀] {a b c d : G₀} lemma div_mul_eq_mul_div₀ (a b c : G₀) : (a / c) * b = a * b / c := by simp_rw [div_eq_mul_inv, mul_assoc, mul_comm c⁻¹] end comm_group_with_zero /-! ### Order dual -/ open order_dual instance [h : mul_zero_class α] : mul_zero_class αᵒᵈ := h instance [h : mul_zero_one_class α] : mul_zero_one_class αᵒᵈ := h instance [has_mul α] [has_zero α] [h : no_zero_divisors α] : no_zero_divisors αᵒᵈ := h instance [h : semigroup_with_zero α] : semigroup_with_zero αᵒᵈ := h instance [h : monoid_with_zero α] : monoid_with_zero αᵒᵈ := h instance [h : cancel_monoid_with_zero α] : cancel_monoid_with_zero αᵒᵈ := h instance [h : comm_monoid_with_zero α] : comm_monoid_with_zero αᵒᵈ := h instance [h : cancel_comm_monoid_with_zero α] : cancel_comm_monoid_with_zero αᵒᵈ := h instance [h : group_with_zero α] : group_with_zero αᵒᵈ := h instance [h : comm_group_with_zero α] : comm_group_with_zero αᵒᵈ := h /-! ### Lexicographic order -/ instance [h : mul_zero_class α] : mul_zero_class (lex α) := h instance [h : mul_zero_one_class α] : mul_zero_one_class (lex α) := h instance [has_mul α] [has_zero α] [h : no_zero_divisors α] : no_zero_divisors (lex α) := h instance [h : semigroup_with_zero α] : semigroup_with_zero (lex α) := h instance [h : monoid_with_zero α] : monoid_with_zero (lex α) := h instance [h : cancel_monoid_with_zero α] : cancel_monoid_with_zero (lex α) := h instance [h : comm_monoid_with_zero α] : comm_monoid_with_zero (lex α) := h instance [h : cancel_comm_monoid_with_zero α] : cancel_comm_monoid_with_zero (lex α) := h instance [h : group_with_zero α] : group_with_zero (lex α) := h instance [h : comm_group_with_zero α] : comm_group_with_zero (lex α) := h
856971284935466c98b941e3d68d6c19cce50f58
eb798737e855f3521e075272ed9781f5a67ff135
/src/hello_world.lean
87acf4381891827286a6bdae3dfa3fe0538cc4c0
[]
no_license
BarinderBanwait/integrally_closedness
cd611ef1ff47eea950a6ebcc1220ddc920d3e7e1
f3b45be4f8b1ac64a2f0a48f0b9f3b281947bcd1
refs/heads/master
1,669,515,621,453
1,596,147,374,000
1,596,147,374,000
267,625,375
0
0
null
null
null
null
UTF-8
Lean
false
false
5,739
lean
/- Copyright (c) 2020 Barinder Singh Banwait. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Barinder Singh Banwait, with lots of help from the good folk on the Leanprover Zulip chat :) -/ import ring_theory.noetherian import ring_theory.integral_closure import ring_theory.adjoin import ring_theory.algebra import ring_theory.localization import linear_algebra.basic /-! # Integrally closedness Let R ⊆ A be an extension of integral domains. ## Main definitions * `is_integrally_closed_in R A : Prop` is the assertion that `R` is integrally closed in `A`. It is a structure, implemented as the predicate that all elements of `A` that are integral over `R` already belong to `R`. * `is_integrally_closed R` is the definition that `R` is integrally closed in an absolute sense. This is implemented as the following implication: if for all `r` and `s` in `R` with `s ≠ 0`, `r^n ∈ ⟨ r^{n-1}s, ⋯ , s^n ⟩ ## Main statements * `fundamental_theorem_integrally_closedness` is the statement that an integral domain is integrally closed if and only if it is integrall closed in a field of fractions. ## References * [J. Neukirch, *Algebraic Number Theory*][neukirch-ant] ## Tags Integrally closed -/ universes u v variables (R : Type u) {A : Type v} variables [comm_ring R] [comm_ring A] variables [algebra R A] variables i : ℕ variables (K : Type*) open submodule open function open finset open_locale big_operators structure is_integrally_closed_in (R : Type u) (A : Type v) [comm_ring R] [comm_ring A] [algebra R A] : Prop := (inj : injective (algebra_map R A)) (closed : ∀ (a : A), is_integral R a → ∃ r : R, algebra_map R A r = a) -- def my_set (R) [integral_domain R] (r : R) (s : R) (n : ℕ) := -- { x | ∃ (i:ℕ ) (h : 0≤ i) (h2 :i≤ n-1), x = r^(n-1-i) *s^(i+1) } def my_set (R) [integral_domain R] (r : R) (s : R) (n : ℕ) := { x | ∃ (c : ℕ × ℕ) (hy: c ∈ (finset.nat.antidiagonal n).erase ⟨0, n⟩), x = r ^ c.2 * s ^ c.1 } def is_integrally_closed (R) [integral_domain R] : Prop := ∀ (r : R) (s : R), (s ≠ 0) ∧ (∃ n : ℕ , r^n ∈ span R (my_set R r s n)) → s ∣ r open submodule lemma mwe (R) [integral_domain R] (n : ℕ) (f : ℕ → R) (hf : f 0 = 1) : ∀ ⦃r s : R⦄, s ≠ 0 → ∑ ij in finset.nat.antidiagonal n, f ij.1 * r ^ ij.2 * s ^ ij.1 = 0 → r^n ∈ span R (my_set R r s n) := begin intros r s, intro s_non_zero, intro H, let p' : ℕ × ℕ := ⟨0, n⟩, have LM : p' ∈ finset.nat.antidiagonal n, { rw finset.nat.mem_antidiagonal, linarith, }, rw ← finset.insert_erase LM at H, simp at H, rw hf at H, simp at H, have KL : r^n = -∑ (x : ℕ × ℕ) in (nat.antidiagonal n).erase p', f x.fst * r ^ x.snd * s ^ x.fst, { exact eq_neg_of_add_eq_zero H, }, rw KL, rw my_set, rw mem_span, intro p, intro p_H, rw ideal.neg_mem_iff p, apply sum_mem, intro c, intro c_H, have x_in_my_set : r ^ c.snd * s ^ c.fst ∈ {x : R | ∃ (c : ℕ × ℕ) (hy : c ∈ (nat.antidiagonal n).erase (0, n)), x = r ^ c.snd * s ^ c.fst}, { simp, use c.fst, use c.snd, split, split, intro something, by_contradiction HAPPY, have obv : c = p', { exact prod.ext something HAPPY, }, rw obv at c_H, rw mem_erase at c_H, cases c_H, contradiction, rw ← finset.nat.mem_antidiagonal, rw mem_erase at c_H, cases c_H with c_H_1 c_H_2, exact c_H_2, refl, }, have x_in_p : r ^ c.snd * s ^ c.fst ∈ ↑p, { exact set.mem_of_mem_of_subset x_in_my_set p_H, }, rw mul_assoc, exact p.smul_mem (f c.fst) x_in_p, end lemma lin_comb_mem (R) [integral_domain R] (n : ℕ) (r s : R) (s_non_zero : s ≠ 0) : r^n ∈ span R (my_set R r s n) → ∃ (f : ℕ → R) (h_f: f 0 = 1), ∑ ij in finset.nat.antidiagonal n, f ij.1 * r ^ ij.2 * s ^ ij.1 = 0 := begin intro H, -- trying finsupp.mem_span_iff_total but it just isn't working :( sorry, end lemma mwe_deluxe (R) [integral_domain R] (n : ℕ) {r s : R} (h_s : s ≠ 0) : r^n ∈ span R (my_set R r s n) ↔ ∃ (f : ℕ → R) (hf : f 0 = 1), ∑ ij in finset.nat.antidiagonal n, f ij.1 * r ^ ij.2 * s ^ ij.1 = 0 := begin split, intro H, apply lin_comb_mem, exact h_s, exact H, intro k, cases k with f B, cases B with C HC, apply mwe, exact C, exact h_s, exact HC, end lemma equiv_johan_absolute_deluxe (R) [integral_domain R] : is_integrally_closed R ↔ ∀ (r s : R), s ≠ 0 → (∃ (n : ℕ) (f : ℕ → R) (hf : f 0 = 1), ∑ ij in finset.nat.antidiagonal n, f ij.1 * r ^ ij.2 * s ^ ij.1 = 0) → s ∣ r := begin split, intros h r s, intro k, unfold is_integrally_closed at h, specialize h r s, intro m, apply h, split, exact k, cases m with n m1, cases m1 with f m2, cases m2 with hf m3, use n, rw mwe_deluxe, use f, split, exact hf, exact m3, exact k, unfold is_integrally_closed, intro H, intros r s, intro H2, cases H2 with A B, apply H, exact A, cases B with n B_n, use n, specialize H r s A, rw mwe_deluxe at B_n, exact B_n, exact A, end lemma fundamental_theorem_integrally_closedness (R : Type u) (A : Type v) [integral_domain R] [comm_ring A] [algebra R A] (H : fraction_map R A): is_integrally_closed R ↔ is_integrally_closed_in R A := begin split, rw equiv_johan_absolute_deluxe, intro H, sorry, sorry, end -- class dedekind_domain (α : Type*) extends integral_domain α := -- (noetherian : is_noetherian_ring α) -- (factors_prod : ∀{a : α}, a ≠ 0 → (factors a).prod ~ᵤ a) -- (prime_factors : ∀{a : α}, a ≠ 0 → ∀x∈factors a, prime x)
e770f3777821ffe3d73480b57b517240290cc2dd
98000ddc36f1ec26addea0df1853c69fa0e71d67
/reckless.lean
9ba46aa32df421d48478cf525ff076a2991e9e5e
[]
no_license
SCRK16/Intuitionism
50b7b5ed780a75c4ff1339c3d36f1133b8acdfe3
a3d9920ae056b39a66e37d1d0e03d246bca1e961
refs/heads/master
1,670,199,869,808
1,598,045,061,000
1,598,045,061,000
254,657,585
1
0
null
1,590,754,909,000
1,586,529,604,000
Lean
UTF-8
Lean
false
false
28,631
lean
/- This file captures the notion of a statement being reckless Meaning: A statement that cannot be proven, and whose negation also cannot be proven The most important example of recklessness is the law of exluded middle: ∀ P : Prop, P ∨ ¬ P, also called "the principle of omniscience" -/ import ..Intuitionism.nat_seq import ..Intuitionism.real import data.nat.basic import data.nat.parity variables P Q : Prop namespace reckless /-- The principle of omniscience, also called the law of the excluded middle -/ def PO : Prop := ∀ Q : Prop, Q ∨ ¬Q /-- The limited principle of omniscience This is the example Brouwer initially used to justify the idea of statements being reckless, though it is slightly weaker than PO Using the relations = and #, we could also have defined LPO as: ∀ a : 𝒩, a = nat_seq.zero ∨ a # nat_seq.zero Note: for such a : 𝒩 it could still be the case that we can prove a ≠ nat_seq.zero This is because (∃ n : ℕ, a n ≠ 0) is stronger than (¬ ∀ n : ℕ, a = 0) -/ def LPO : Prop := ∀ a : 𝒩, (∀ n : ℕ, a n = 0) ∨ (∃ n : ℕ, a n ≠ 0) def reckless_LPO : Prop → Prop := λ P : Prop, (PO → P) ∧ (P → LPO) /-- The lesser limited principle of omniscience In words, it says that: Before knowing when a sequence of natural numbers will stop being 0, the person who claims LLPO is true, can already say it will happen at an even index or an odd index It's important that the binding of ∨ is outside the binding of ∀ k : ℕ, because the statement ∀ k : ℕ, k % 2 = 0 ∨ k % 2 = 1, is always true -/ def LLPO : Prop := ∀ a : 𝒩, (∀ k : ℕ, (∀ i : ℕ, i < k → a i = 0) ∧ a k ≠ 0 → k % 2 = 0) ∨ (∀ k : ℕ, (∀ i : ℕ, i < k → a i = 0) ∧ a k ≠ 0 → k % 2 = 1) def reckless_LLPO : Prop → Prop := λ P : Prop, (PO → P) ∧ (P → LLPO) theorem PO_implies_LPO : PO → LPO := begin intro po, intro a, cases po (∃ n : ℕ, a n ≠ 0) with h₁ h₂, {-- case: ∃ n : ℕ, a n ≠ 0 right, exact h₁, }, {-- case: ¬∃ n : ℕ, a n ≠ 0 left, have h₃ := forall_not_of_not_exists h₂, simp at h₃, exact h₃, } end -- A simple lemma to show a reckless statement exists: PO itself is reckless lemma exists_reckless : ∃ P : Prop, reckless_LPO P := begin use PO, split, {-- need to prove: PO → PO tauto, }, {-- need to prove: PO → LPO exact PO_implies_LPO, } end theorem LPO_implies_LLPO : LPO → LLPO := begin intro lpo, intro a, cases lpo a with faeq eneq, {-- case: ∀ n : ℕ, a n = 0, the conclusion is vacuously true left, intros k hk, exfalso, apply and.elim_right hk, exact faeq k, }, {-- case: ∃ n : ℕ, a n ≠ 0 cases eneq with n hn, cases nat_seq.all_eq_or_exists_neq a nat_seq.zero n with alleq exneq, {-- case: ∀ i < n: a i = 0 cases nat.mod_two_eq_zero_or_one n with neven nodd, {-- case: n is even left, intros k hk, have keqn : k = n, by { have hkn := nat_seq.lt_eq_ne_le a nat_seq.zero k n hk.elim_left hn, have hnk := nat_seq.lt_eq_ne_le a nat_seq.zero n k alleq hk.elim_right, exact le_antisymm hkn hnk, }, rw keqn, exact neven, }, {-- case: n is odd right, intros k hk, have keqn : k = n, by { have hkn := nat_seq.lt_eq_ne_le a nat_seq.zero k n hk.elim_left hn, have hnk := nat_seq.lt_eq_ne_le a nat_seq.zero n k alleq hk.elim_right, exact le_antisymm hkn hnk, }, rw keqn, exact nodd, } }, {-- case: ∃ i < n, (∀ j < i, a j = 0) ∧ a i ≠ 0 cases exneq with i hi, cases nat.mod_two_eq_zero_or_one i with ieven iodd, {-- case: i is even left, intros k hk, have keqi : k = i, by { have hki := nat_seq.lt_eq_ne_le a nat_seq.zero k i hk.elim_left hi.elim_right.elim_right, have hik := nat_seq.lt_eq_ne_le a nat_seq.zero i k hi.elim_right.elim_left hk.elim_right, exact le_antisymm hki hik, }, rw keqi, exact ieven, }, {-- case: i is odd right, intros k hk, have keqi : k = i, by { have hki := nat_seq.lt_eq_ne_le a nat_seq.zero k i hk.elim_left hi.elim_right.elim_right, have hik := nat_seq.lt_eq_ne_le a nat_seq.zero i k hi.elim_right.elim_left hk.elim_right, exact le_antisymm hki hik, }, rw keqi, exact iodd, } } } end /-- Double negation cannot simply be eliminated for all propositions P Also shows that proof by contradiction cannot always be applied -/ theorem reckless_not_not_implies : reckless_LPO (∀ P : Prop, ¬¬P → P) := begin split, {-- need to prove: PO → ∀ P : Prop, ¬¬P → P intros po P nnp, cases po P with hp np, {-- case: P exact hp, }, {-- case: ¬P exfalso, exact nnp np, } }, {-- need to prove: (∀ P : Prop, ¬¬P → P) → LPO, we prove that it even implies PO intro h, apply PO_implies_LPO, intro P, apply h, exact not_not_em P, } end theorem reckless_implies_not_or : reckless_LPO (∀ P Q : Prop, (P → Q) → (Q ∨ ¬P)) := begin split, { intros po P Q h, cases po P with hp np, {-- case: P left, exact h hp, }, {-- case: ¬P right, exact np, } }, {-- need to prove: (∀ (P Q : Prop), (P → Q) → Q ∨ ¬P) → LPO, we prove that it even implies PO intros h, apply PO_implies_LPO, intro Q, apply h Q Q, tauto, } end /-- Given a b : 𝒩, we already know that a < b → a ≤ b, and that a = b → a ≤ b However, this theorem shows that the opposite is not true One might expect a ≤ b → (a < b ∨ a = b), but this statement actually implies LPO, and is therefore reckless -/ theorem reckless_LPO_le_implies_lt_or_eq : reckless_LPO (∀ a b : 𝒩, a ≤ b → a < b ∨ a =' b) := begin split, {-- need to prove: PO → (∀ a b : 𝒩, a ≤ b → a < b ∨ a =' b) intros po a b hab, cases po (a < b) with hl hnl, {-- case: a < b left, exact hl, }, {-- case: ¬(a < b), we prove: a =' b right, rw ← nat_seq.le_iff_not_lt at hnl, exact nat_seq.eq_of_le_le hab hnl, } }, {-- need to prove: (∀ a b : 𝒩, a ≤ b → a < b ∨ a =' b) → LPO intros h₁ a, cases h₁ nat_seq.zero a (nat_seq.zero_le a) with zlt zeq, {-- case: 0 < a right, have h₂ := or.intro_left (a < nat_seq.zero) zlt, rw ← nat_seq.apart_iff_lt_or_lt at h₂, rw nat_seq.apart_symm at h₂, exact h₂, }, {-- case: 0 = a left, exact nat_seq.eq_symm.mp zeq, } }, end -- The two following theorems look funny together theorem implies_not_implies_not : ∀ P Q : Prop, (P ∨ ¬P → ¬Q) → ¬Q := begin intros P Q h hq, have h₁ := mt h, have h₂ := h₁ (not_not_intro hq), exact (not_not_em P) h₂, end theorem reckless_LPO_implies_implies : reckless_LPO (∀ P Q : Prop, (P ∨ ¬P → Q) → Q) := begin split, { intros po P Q hpq, exact hpq (po P), }, { intro h, apply PO_implies_LPO, intro P, have hp := h P (P ∨ ¬P), apply hp, intro pop, exact pop, } end instance start_le_not_zero_decidable (a : 𝒩) (n : ℕ) : decidable (∃ i : ℕ, i ≤ n ∧ a i ≠ 0) := begin induction n with d hd, { simp only [nat.nat_zero_eq_zero, le_zero_iff_eq, exists_eq_left, ne.def], exact ne.decidable _ _, }, { have hds : decidable (a (nat.succ d) ≠ 0) := ne.decidable _ _, have hdt : decidable ((∃ (i : ℕ), i ≤ d ∧ a i ≠ 0) ∨ a (nat.succ d) ≠ 0), by { exact @or.decidable (∃ (i : ℕ), i ≤ d ∧ a i ≠ 0) (a (nat.succ d) ≠ 0) hd hds, }, have hiff : ((∃ (i : ℕ), i ≤ d ∧ a i ≠ 0) ∨ a (nat.succ d) ≠ 0) ↔ (∃ (i : ℕ), i ≤ nat.succ d ∧ a i ≠ 0), by { split, {-- need to prove: → intro h, cases h with ilt ieq, {-- case: ∃ (i : ℕ), i ≤ d ∧ a i ≠ 0 cases ilt with i hi, use i, split, {-- need to prove: i ≤ nat.succ d exact le_trans hi.elim_left (nat.le_succ d), }, {-- need to prove: a i ≠ 0 exact hi.elim_right, } }, {-- case: a (nat.succ d) ≠ 0 use nat.succ d, split, {-- need to prove: nat.succ d ≤ nat.succ d refl, }, {-- need to prove: a (nat.succ d) ≠ 0 exact ieq, } } }, {-- need to prove: ← intro h, cases h with i hi, cases lt_or_eq_of_le hi.elim_left with ilt ieq, {-- case: i < nat.succ d left, use i, split, {-- need to prove: i ≤ d exact nat.le_of_lt_succ ilt, }, {-- need to prove: a i ≠ 0 exact hi.elim_right, } }, {-- case: i = nat.succ d right, rw ← ieq, exact hi.elim_right, } }, }, apply decidable_of_decidable_of_iff hdt hiff, } end instance start_lt_not_zero_decidable (a : 𝒩) (n : ℕ) : decidable (∃ i : ℕ, i < n ∧ a i ≠ 0) := begin induction n with d hd, { apply is_false, intro h, cases h with i hi, exact (nat.not_lt_zero i) hi.elim_left, }, { cases hd with hdfalse hdtrue, {-- case: ¬∃ (i : ℕ), i < d ∧ a i ≠ 0 have hds : decidable (a d ≠ 0) := ne.decidable _ _, cases hds with hdsfalse hdstrue, {-- case: ¬a d ≠ 0 apply is_false, intro h, cases h with i hi, cases lt_or_eq_of_le (nat.le_of_lt_succ hi.elim_left) with iltd ieqd, {-- case: i < d apply hdfalse, use i, exact and.intro iltd hi.elim_right, }, {-- case: i = d rw ieqd at hi, exact hdsfalse hi.elim_right, } }, {-- case: a d ≠ 0 apply is_true, use d, exact and.intro (lt_add_one d) hdstrue, } }, {-- case: ∃ (i : ℕ), i < d ∧ a i ≠ 0 apply is_true, cases hdtrue with i hi, use i, split, {-- need to prove: i < nat.succ d exact lt_trans hi.elim_left (lt_add_one d), }, {-- need to prove: a i ≠ 0 exact hi.elim_right, } } } end def snap (a : 𝒩) : ℛ := subtype.mk (λ n : ℕ, if h : (∃ i : ℕ, i ≤ n ∧ a i ≠ 0) then segment.inclusion (1 / nat.succ (nat.find h)) else segment.two_sided_inclusion (1 / nat.succ n) begin -- need to prove: 1/n > 0 simp [nat.zero_lt_succ, nat.cast_add_one_pos], end) begin -- need to prove: The function defined above is a real number: It shrinks and it dwindles split, {-- need to prove: shrinking rw shrinking, intro n, split_ifs with h₁ h₂, {-- case: (∃ i ≤ n+1, a i ≠ 0) ∧ (∃ i ≤ n, a i ≠ 0) suffices hh : nat.find h₁ = nat.find h₂, {-- need to prove: nat.find h₁ = nat.find h₂ → shrinking rw hh, }, {-- need to prove: nat.find h₁ = nat.find h₂ have hh₁ := nat.find_spec h₁, have hh₂ := nat.find_spec h₂, cases lt_trichotomy (nat.find h₁) (nat.find h₂) with hlt hge, {-- case: nat.find h₁ < nat.find h₂ exfalso, apply nat.find_min h₂ hlt, split, {-- need to prove: nat.find h₁ ≤ n transitivity (nat.find h₂), exact (le_of_lt hlt), exact hh₂.elim_left, }, {-- need to prove: a (nat.find h₁) ≠ 0 exact hh₁.elim_right, } }, { cases hge with heq hgt, {-- case: nat.find h₁ = nat.find h₂ exact heq, }, {-- case: nat.find h₂ > nat.find h₁ exfalso, apply nat.find_min h₁ hgt, split, {-- need to prove: nat.find h₂ ≤ n + 1 transitivity n, exact hh₂.elim_left, exact nat.le_succ n, }, {-- need to prove: a (nat.find h₂) ≠ 0 exact hh₂.elim_right, } } } } }, {-- case: (∃ i ≤ n+1, a i ≠ 0) ∧ ¬(∃ i ≤ n, a i ≠ 0) dsimp [segment.inclusion, segment.two_sided_inclusion, segment.contained, segment.fst, segment.snd], split, {-- need to prove: -(1/(↑n+1)) ≤ 1/(↑(nat.find h₁) + 1) transitivity (rat.mk 0 1), repeat { rw rat.zero_mk 1, simp [le_of_lt, nat.cast_add_one_pos], }, }, {-- need to prove: 1/(↑(nat.find h₁) + 1) ≤ (1/(↑n+1)) cases lt_or_eq_of_le (nat.find_spec h₁).elim_left with hlt heq, {-- case: nat.find h₁ < n + 1 exfalso, apply h₂, use nat.find h₁, split, {-- need to prove: nat.find h₁ ≤ n exact nat.lt_succ_iff.mp hlt, }, {-- need to prove: a (nat.find h₁) ≠ 0 exact (nat.find_spec h₁).elim_right, } }, {-- case: nat.find h₁ = n + 1 rw heq, rw one_div_le_one_div, {-- need to prove: n + 1 ≤ n + 1 + 1 simp, }, {-- need to prove: 0 ≤ n + 1 exact nat.cast_add_one_pos _, }, {-- need to prove: 0 ≤ n + 1 + 1 exact nat.cast_add_one_pos _, } } } }, {-- case: ¬(∃ i ≤ n+1, a i ≠ 0) ∧ (∃ i ≤ n, a i ≠ 0) exfalso, cases h with i hi, apply h₁, use i, split, {-- need to prove: i < n + 1 transitivity n, exact hi.elim_left, exact nat.le_succ n, }, {-- need to prove: a i ≠ 0 exact hi.elim_right, } }, {-- case: ¬(∃ i ≤ n+1, a i ≠ 0) ∧ ¬(∃ i ≤ n, a i ≠ 0) apply segment.two_sided_inclusion_contained, rw one_div_le_one_div, {-- need to prove: ↑(nat.succ n) ≤ ↑(nat.succ (n + 1)) simp, }, {-- need to prove: 0 < ↑(nat.succ (n+1)) transitivity (↑n + 1 : ℚ), exact nat.cast_add_one_pos n, simp [zero_lt_one], }, {-- need to prove: 0 < ↑(nat.succ n) exact nat.cast_add_one_pos n, } } }, {-- need to prove: Dwindling rw dwindling, intros q hq, dsimp [segment.inclusion, segment.two_sided_inclusion, segment.snd, segment.fst], sorry, } end theorem reckless_LPO_real_lt_eq_gt : reckless_LPO (∀ x y : ℛ, x < y ∨ x =' y ∨ y < x) := begin split, { intros po x y, cases po (x < y) with xlt nxlt, {-- case: x < y left, exact xlt, }, {-- case: ¬x < y right, cases po (y < x) with xgt nxgt, {-- case: y < x right, exact xgt, }, {-- case: ¬y < x left, rw ← real_seq.le_iff_not_lt at *, exact real_seq.eq_of_le_of_le _ _ nxgt nxlt, } } }, {-- need to prove: (∀ x : ℛ, x < y ∨ x =' y ∨ y < x) → LPO intros h a, have hsnap := h (snap a) (real_seq.inclusion_const 0), cases hsnap with hlt hge, {-- case: snap a < 0 exfalso, cases hlt with n hn, simp [real_seq.seq, real_seq.inclusion_const, segment.inclusion, snap, segment.two_sided_inclusion, segment.lt, segment.fst, segment.snd] at hn, split_ifs at hn, {-- case: ∃ i : ℕ, i ≤ n ∧ a i ≠ 0 apply not_le_of_lt hn, simp [le_of_lt, nat.cast_add_one_pos], }, {-- case: ¬∃ i : ℕ, i ≤ n ∧ a i ≠ 0 apply not_le_of_lt hn, simp [le_of_lt, nat.cast_add_one_pos], } }, { cases hge with heq hgt, {-- case: snap a = 0 left, intro n, have hn := heq n, simp [real_seq.seq, segment.touches, segment.le, segment.fst, segment.snd, real_seq.seq, real_seq.inclusion_const, segment.inclusion, snap, segment.two_sided_inclusion] at hn, cases hn with hn₁ hn₂, split_ifs at hn₁, {-- case: ∃ i : ℕ, i ≤ n ∧ a i ≠ 0 exfalso, apply not_lt_of_le hn₁, simp [nat.cast_add_one_pos], }, {-- case: ¬∃ i : ℕ, i ≤ n ∧ a i ≠ 0 have han := (forall_not_of_not_exists h_1) n, simp at han, exact han, } }, {-- case: snap a > 0 right, cases hgt with n hn, simp [real_seq.seq, real_seq.inclusion_const, segment.inclusion, snap, segment.two_sided_inclusion, segment.lt, segment.fst, segment.snd] at hn, split_ifs at hn, {-- case: ∃ i : ℕ, i ≤ n ∧ a i ≠ 0 cases h_1 with i hi, use i, exact hi.elim_right, }, {-- case: ¬∃ i : ℕ, i ≤ n ∧ a i ≠ 0 exfalso, rwa [lt_neg, neg_zero] at hn, apply not_le_of_lt hn, simp [le_of_lt, nat.cast_add_one_pos], } } } } end def WLEM : Prop := ∀ P : Prop, ¬P ∨ ¬¬P def WLPO : Prop := ∀ a : 𝒩, (∀ n : ℕ, a n = 0) ∨ (¬∀ n : ℕ, a n = 0) theorem PO_implies_WLEM : PO → WLEM := begin intros po P, cases po P with hp np, {-- case: P right, -- need to prove: ¬¬P intro np, exact np hp, }, {-- case: ¬P left, -- need to prove: ¬P exact np, } end theorem LPO_implies_WLPO : LPO → WLPO := begin intros lpo a, cases lpo a with aeq ane, {-- case: ∀ n : ℕ, a n = 0 left, -- need to prove: ∀ n : ℕ, a n = 0 exact aeq, }, {-- case: ∃ n : ℕ, a n ≠ 0 right, -- need to prove: ¬∀ n : ℕ, a n = 0 intro aeq, cases ane with n hn, exact hn (aeq n), } end theorem weak_LEM_implies_weak_LPO : WLEM → WLPO := begin intros wlem a, cases wlem (∃ n : ℕ, a n ≠ 0) with nh nnh, {-- case: ¬∃ (n : ℕ), a n ≠ 0 left, have h : ∀ n : ℕ, ¬ a n ≠ 0 := forall_not_of_not_exists nh, intro n, have hn := h n, rwa [ne.def, not_not] at hn, }, {-- case: ¬¬∃ (n : ℕ), a n = 0 right, intro h, apply nnh, intro nex, cases nex with n nhn, exact nhn (h n), } end theorem weak_LPO_implies_LLPO : WLPO → LLPO := begin intros wlpo a, set d : 𝒩 := λ n, if n % 2 = 0 then if (∃ i : ℕ, i < n ∧ a i ≠ 0) then 0 else a n else 0 with ddef, cases wlpo d with deq nd, {-- case: ∀ n : ℕ, d n = 0 right, intros k hk, -- need to prove: k % 2 = 1 have hdk := deq k, rw ddef at hdk, simp at hdk, rw ← nat.mod_two_ne_zero, intro hkm, split_ifs at hdk, {-- case: ∃ i : ℕ, i < k ∧ a i ≠ 0 cases h with i hi, apply hi.elim_right, exact hk.elim_left i hi.elim_left, }, {-- case: ¬∃ i : ℕ, i < k ∧ a i ≠ 0 and a k = 0 exact hk.elim_right hdk, } }, {-- case: ¬∀ n : ℕ, d n = 0 left, intros k hk, -- need to prove: k % 2 = 0 rw ← nat.mod_two_ne_one, intro hkm, apply nd, intro n, rw ddef, simp, split_ifs, {-- need to prove: 0 = 0 refl, }, {-- need to prove: a n = 0, using n % 2 = 0 and ∀ x : ℕ, x < n → a x = 0 simp at h_1, cases nat.lt_trichotomy n k with nlt nge, {-- case: n < k exact hk.elim_left n nlt, }, {-- case: n ≥ k cases nge with neq ngt, {-- case: n = k exfalso, rwa [← nat.mod_two_ne_one, neq] at h, exact h hkm, }, {-- case: n > k exfalso, apply hk.elim_right, exact h_1 k ngt, } } }, {-- need to prove: 0 = 0 refl, } } end theorem weak_LEM_implies_LLPO : WLEM → LLPO := begin intro wlem, apply weak_LPO_implies_LLPO, exact weak_LEM_implies_weak_LPO wlem, end -- We can also prove the above statement directly theorem weak_LEM_implies_LLPO' : WLEM → LLPO := begin intros wlem b, cases wlem (∀ (k : ℕ), (∀ (i : ℕ), i < k → b i = 0) ∧ b k ≠ 0 → k % 2 = 0) with nh nnh, {-- case: ¬∀ (k : ℕ), (∀ (i : ℕ), i < k → b i = 0) ∧ b k ≠ 0 → k % 2 = 0 right, -- need to prove: ∀ (k : ℕ), (∀ (i : ℕ), i < k → b i = 0) ∧ b k ≠ 0 → k % 2 = 1 intros k hk, rw ← nat.mod_two_ne_zero, intro h, apply nh, intros j hj, have hjk : j = k := nat_seq.first_zero_eq b j k hj.elim_left hj.elim_right hk.elim_left hk.elim_right, rw hjk, exact h, }, {-- case: ¬¬∀ (k : ℕ), (∀ (i : ℕ), i < k → b i = 0) ∧ b k ≠ 0 → k % 2 = 0 left, -- need to prove: ∀ (k : ℕ), (∀ (i : ℕ), i < k → b i = 0) ∧ b k ≠ 0 → k % 2 = 0 intros k hk, rw ← nat.mod_two_ne_one, rw ← nat.mod_two_ne_zero, intro h0, apply nnh, intro h, exact h0 (h k hk), } end theorem reckless_LLPO_not_not_or : reckless_LLPO (∀ P Q : Prop, ¬¬(P ∨ Q) → (¬¬P ∨ ¬¬Q)) := begin split, {-- need to prove: PO → ∀ (P Q : Prop), ¬¬(P ∨ Q) → ¬¬P ∨ ¬¬Q intros po P Q h, cases po P with hp np, {-- case: P left, intro np, exact np hp, }, {-- case: ¬P cases po Q with hq nq, {-- case: Q right, intro nq, exact nq hq, }, {-- case: ¬Q exfalso, apply h, intro pq, cases pq with hp hq, {-- case: P exact np hp, }, {-- case: Q exact nq hq, } } } }, {-- need to prove: (∀ (P Q : Prop), ¬¬(P ∨ Q) → ¬¬P ∨ ¬¬Q) → LPO intro h₁, apply weak_LEM_implies_LLPO, intro P, have h₂ := h₁ P (¬P) (not_not_em P), cases h₂ with nn nnn, {-- case: ¬¬P right, exact nn }, {-- case: ¬¬¬P left, exact (not_not_not_iff P).mp nnn, } } end theorem reckless_LLPO_not_and_implies_not_or_not : reckless_LLPO (∀ P Q : Prop, ¬(P ∧ Q) → (¬P ∨ ¬Q)) := begin split, {-- need to prove: PO → ∀ (P Q : Prop), ¬(P ∧ Q) → ¬P ∨ ¬Q intros po P Q h, cases po P with hp np, {-- case: P cases po Q with hq nq, {-- case: Q exfalso, exact h (and.intro hp hq), }, {-- case: ¬Q right, exact nq, } }, {-- case: ¬P left, exact np, } }, {-- need to prove: (∀ (P Q : Prop), ¬(P ∧ Q) → ¬P ∨ ¬Q) → LLPO intro h, apply weak_LEM_implies_LLPO, intro P, exact h P (¬P) (and_not_self P).mp, } end /-- If P ∨ ¬P holds for some proposition P, then eliminating double negation is allowed for P -/ lemma or_not_implies_not_not_implies (h : P ∨ ¬P) : ¬¬P → P := begin intro hp, cases h with p np, exact p, exfalso, exact hp np, end theorem reckless_LLPO_not_not_implies_or : reckless_LLPO (∀ P : Prop, (¬¬P → P) → P ∨ ¬P) := begin split, {-- need to prove: PO → ∀ (P : Prop), (¬¬P → P) → P ∨ ¬P intros po P h, exact po P, }, {-- need to prove: (∀ (P : Prop), (¬¬P → P) → P ∨ ¬P) → LLPO intro h, apply weak_LEM_implies_LLPO, intro P, have hp := h (¬P), exact hp (not_not_not_iff P).mp, } end -- A reminder that brackets are important example : (∀ P : Prop, ¬¬P → P) → (∀ P : Prop, P ∨ ¬P) := begin intros h P, exact h (P ∨ ¬P) (not_not_em P), end end reckless
ee31201e22ecc0c7dba36cb51cb9696bfd81de23
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/data/rat/order.lean
dc44806d1c01289a7ff43c02e8043a6798da72c6
[ "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
9,024
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import data.rat.basic /-! # Order for Rational Numbers ## Summary We define the order on `ℚ`, prove that `ℚ` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Notations - `/.` is infix notation for `rat.mk`. ## Tags rat, rationals, field, ℚ, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace rat variables (a b c : ℚ) local infix ` /. `:70 := rat.mk protected def nonneg : ℚ → Prop | ⟨n, d, h, c⟩ := n ≥ 0 @[simp] theorem mk_nonneg (a : ℤ) {b : ℤ} (h : b > 0) : (a /. b).nonneg ↔ a ≥ 0 := begin generalize ha : a /. b = x, cases x with n₁ d₁ h₁ c₁, rw num_denom' at ha, simp [rat.nonneg], have d0 := int.coe_nat_lt.2 h₁, have := (mk_eq (ne_of_gt h) (ne_of_gt d0)).1 ha, constructor; intro h₂, { apply nonneg_of_mul_nonneg_right _ d0, rw this, exact mul_nonneg h₂ (le_of_lt h) }, { apply nonneg_of_mul_nonneg_right _ h, rw ← this, exact mul_nonneg h₂ (int.coe_zero_le _) }, end protected def nonneg_add {a b} : rat.nonneg a → rat.nonneg b → rat.nonneg (a + b) := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, begin have d₁0 : (d₁:ℤ) > 0 := int.coe_nat_pos.2 (nat.pos_of_ne_zero h₁), have d₂0 : (d₂:ℤ) > 0 := int.coe_nat_pos.2 (nat.pos_of_ne_zero h₂), simp [d₁0, d₂0, h₁, h₂, mul_pos d₁0 d₂0], intros n₁0 n₂0, apply add_nonneg; apply mul_nonneg; {assumption <|> apply int.coe_zero_le} end protected def nonneg_mul {a b} : rat.nonneg a → rat.nonneg b → rat.nonneg (a * b) := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, begin have d₁0 : (d₁:ℤ) > 0 := int.coe_nat_pos.2 (nat.pos_of_ne_zero h₁), have d₂0 : (d₂:ℤ) > 0 := int.coe_nat_pos.2 (nat.pos_of_ne_zero h₂), simp [d₁0, d₂0, h₁, h₂, mul_pos d₁0 d₂0], exact mul_nonneg end protected def nonneg_antisymm {a} : rat.nonneg a → rat.nonneg (-a) → a = 0 := num_denom_cases_on' a $ λ n d h, begin have d0 : (d:ℤ) > 0 := int.coe_nat_pos.2 (nat.pos_of_ne_zero h), simp [d0, h], exact λ h₁ h₂, le_antisymm (nonpos_of_neg_nonneg h₂) h₁ end protected def nonneg_total : rat.nonneg a ∨ rat.nonneg (-a) := by cases a with n; exact or.imp_right neg_nonneg_of_nonpos (le_total 0 n) instance decidable_nonneg : decidable (rat.nonneg a) := by cases a; unfold rat.nonneg; apply_instance protected def le (a b : ℚ) := rat.nonneg (b - a) instance : has_le ℚ := ⟨rat.le⟩ instance decidable_le : decidable_rel ((≤) : ℚ → ℚ → Prop) | a b := show decidable (rat.nonneg (b - a)), by apply_instance protected theorem le_def {a b c d : ℤ} (b0 : b > 0) (d0 : d > 0) : a /. b ≤ c /. d ↔ a * d ≤ c * b := show rat.nonneg _ ↔ _, by simpa [ne_of_gt b0, ne_of_gt d0, mul_pos b0 d0, mul_comm] using @sub_nonneg _ _ (b * c) (a * d) protected theorem le_refl : a ≤ a := show rat.nonneg (a - a), by rw sub_self; exact le_refl (0 : ℤ) protected theorem le_total : a ≤ b ∨ b ≤ a := by have := rat.nonneg_total (b - a); rwa neg_sub at this protected theorem le_antisymm {a b : ℚ} (hab : a ≤ b) (hba : b ≤ a) : a = b := by have := eq_neg_of_add_eq_zero (rat.nonneg_antisymm hba $ by simpa); rwa neg_neg at this protected theorem le_trans {a b c : ℚ} (hab : a ≤ b) (hbc : b ≤ c) : a ≤ c := have rat.nonneg (b - a + (c - b)), from rat.nonneg_add hab hbc, by simpa instance : decidable_linear_order ℚ := { le := rat.le, le_refl := rat.le_refl, le_trans := @rat.le_trans, le_antisymm := @rat.le_antisymm, le_total := rat.le_total, decidable_eq := by apply_instance, decidable_le := assume a b, rat.decidable_nonneg (b - a) } /- Extra instances to short-circuit type class resolution -/ instance : has_lt ℚ := by apply_instance instance : lattice.distrib_lattice ℚ := by apply_instance instance : lattice.lattice ℚ := by apply_instance instance : lattice.semilattice_inf ℚ := by apply_instance instance : lattice.semilattice_sup ℚ := by apply_instance instance : lattice.has_inf ℚ := by apply_instance instance : lattice.has_sup ℚ := by apply_instance instance : linear_order ℚ := by apply_instance instance : partial_order ℚ := by apply_instance instance : preorder ℚ := by apply_instance theorem nonneg_iff_zero_le {a} : rat.nonneg a ↔ 0 ≤ a := show rat.nonneg a ↔ rat.nonneg (a - 0), by simp theorem num_nonneg_iff_zero_le : ∀ {a : ℚ}, 0 ≤ a.num ↔ 0 ≤ a | ⟨n, d, h, c⟩ := @nonneg_iff_zero_le ⟨n, d, h, c⟩ theorem mk_le {a b c d : ℤ} (h₁ : b > 0) (h₂ : d > 0) : a /. b ≤ c /. d ↔ a * d ≤ c * b := by conv in (_ ≤ _) { simp only [(≤), rat.le], rw [sub_def (ne_of_gt h₂) (ne_of_gt h₁), mk_nonneg _ (mul_pos h₂ h₁), ge, sub_nonneg] } protected theorem add_le_add_left {a b c : ℚ} : c + a ≤ c + b ↔ a ≤ b := by unfold has_le.le rat.le; rw add_sub_add_left_eq_sub protected theorem mul_nonneg {a b : ℚ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a * b := by rw ← nonneg_iff_zero_le at ha hb ⊢; exact rat.nonneg_mul ha hb instance : discrete_linear_ordered_field ℚ := { zero_lt_one := dec_trivial, add_le_add_left := assume a b ab c, rat.add_le_add_left.2 ab, add_lt_add_left := assume a b ab c, lt_of_not_ge $ λ ba, not_le_of_lt ab $ rat.add_le_add_left.1 ba, mul_nonneg := @rat.mul_nonneg, mul_pos := assume a b ha hb, lt_of_le_of_ne (rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm, ..rat.discrete_field, ..rat.decidable_linear_order } /- Extra instances to short-circuit type class resolution -/ instance : linear_ordered_field ℚ := by apply_instance instance : decidable_linear_ordered_comm_ring ℚ := by apply_instance instance : linear_ordered_comm_ring ℚ := by apply_instance instance : linear_ordered_ring ℚ := by apply_instance instance : ordered_ring ℚ := by apply_instance instance : decidable_linear_ordered_semiring ℚ := by apply_instance instance : linear_ordered_semiring ℚ := by apply_instance instance : ordered_semiring ℚ := by apply_instance instance : decidable_linear_ordered_comm_group ℚ := by apply_instance instance : ordered_comm_group ℚ := by apply_instance instance : ordered_cancel_comm_monoid ℚ := by apply_instance instance : ordered_comm_monoid ℚ := by apply_instance attribute [irreducible] rat.le theorem num_pos_iff_pos {a : ℚ} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le $ by simpa [(by cases a; refl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a) theorem of_int_eq_mk (z : ℤ) : of_int z = z /. 1 := num_denom' _ _ _ _ theorem coe_int_eq_mk : ∀ z : ℤ, ↑z = z /. 1 | (n : ℕ) := show (n:ℚ) = n /. 1, by induction n with n IH n; simp [*, show (1:ℚ) = 1 /. 1, from rfl] | -[1+ n] := show (-(n + 1) : ℚ) = -[1+ n] /. 1, begin induction n with n IH, {refl}, show -(n + 1 + 1 : ℚ) = -[1+ n.succ] /. 1, rw [neg_add, IH], simpa [show -1 = (-1) /. 1, from rfl] end theorem coe_int_eq_of_int (z : ℤ) : ↑z = of_int z := (coe_int_eq_mk z).trans (of_int_eq_mk z).symm theorem mk_eq_div (n d : ℤ) : n /. d = (n / d : ℚ) := begin by_cases d0 : d = 0, {simp [d0, div_zero]}, rw [division_def, coe_int_eq_mk, coe_int_eq_mk, inv_def, mul_def one_ne_zero d0, one_mul, mul_one] end theorem abs_def (q : ℚ) : abs q = q.num.nat_abs /. q.denom := begin have hz : (0:ℚ) = 0 /. 1 := rfl, cases le_total q 0 with hq hq, { rw [abs_of_nonpos hq], rw [num_denom q, hz, rat.le_def (int.coe_nat_pos.2 q.pos) zero_lt_one, mul_one, zero_mul] at hq, rw [int.of_nat_nat_abs_of_nonpos hq, ← neg_def, ← num_denom q] }, { rw [abs_of_nonneg hq], rw [num_denom q, hz, rat.le_def zero_lt_one (int.coe_nat_pos.2 q.pos), mul_one, zero_mul] at hq, rw [int.nat_abs_of_nonneg hq, ← num_denom q] } end section sqrt def sqrt (q : ℚ) : ℚ := rat.mk (int.sqrt q.num) (nat.sqrt q.denom) theorem sqrt_eq (q : ℚ) : rat.sqrt (q*q) = abs q := by rw [sqrt, mul_self_num, mul_self_denom, int.sqrt_eq, nat.sqrt_eq, abs_def] theorem exists_mul_self (x : ℚ) : (∃ q, q * q = x) ↔ rat.sqrt x * rat.sqrt x = x := ⟨λ ⟨n, hn⟩, by rw [← hn, sqrt_eq, abs_mul_abs_self], λ h, ⟨rat.sqrt x, h⟩⟩ theorem sqrt_nonneg (q : ℚ) : 0 ≤ rat.sqrt q := nonneg_iff_zero_le.1 $ (mk_nonneg _ $ int.coe_nat_pos.2 $ nat.pos_of_ne_zero $ λ H, nat.pos_iff_ne_zero.1 q.pos $ nat.sqrt_eq_zero.1 H).2 trivial end sqrt end rat
3d8831fcfb76dbabffbc52dedd404b19838f254a
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/list/big_operators.lean
e9ed34eb77b50629809d4589c7728c95f956c351
[ "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,764
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, Floris van Doorn, Sébastien Gouëzel -/ import data.list.basic /-! # Sums and products from lists This file provides basic results about `list.prod` and `list.sum`, which calculate the product and sum of elements of a list. These are defined in [`data.list.defs`](./data/list/defs). -/ variables {ι M N P M₀ G R : Type*} namespace list section monoid variables [monoid M] [monoid N] [monoid P] {l l₁ l₂ : list M} {a : M} @[simp, to_additive] lemma prod_nil : ([] : list M).prod = 1 := rfl @[to_additive] lemma prod_singleton : [a].prod = a := one_mul a @[simp, to_additive] lemma prod_cons : (a :: l).prod = a * l.prod := calc (a :: l).prod = foldl (*) (a * 1) l : by simp only [list.prod, foldl_cons, one_mul, mul_one] ... = _ : foldl_assoc @[simp, to_additive] lemma prod_append : (l₁ ++ l₂).prod = l₁.prod * l₂.prod := calc (l₁ ++ l₂).prod = foldl (*) (foldl (*) 1 l₁ * 1) l₂ : by simp [list.prod] ... = l₁.prod * l₂.prod : foldl_assoc @[to_additive] lemma prod_concat : (l.concat a).prod = l.prod * a := by rw [concat_eq_append, prod_append, prod_singleton] @[simp, to_additive] lemma prod_join {l : list (list M)} : l.join.prod = (l.map list.prod).prod := by induction l; [refl, simp only [*, list.join, map, prod_append, prod_cons]] @[to_additive] lemma prod_eq_foldr : l.prod = foldr (*) 1 l := list.rec_on l rfl $ λ a l ihl, by rw [prod_cons, foldr_cons, ihl] @[to_additive] lemma prod_hom_rel (l : list ι) {r : M → N → Prop} {f : ι → M} {g : ι → N} (h₁ : r 1 1) (h₂ : ∀ ⦃i a b⦄, r a b → r (f i * a) (g i * b)) : r (l.map f).prod (l.map g).prod := list.rec_on l h₁ (λ a l hl, by simp only [map_cons, prod_cons, h₂ hl]) @[to_additive] lemma prod_hom (l : list M) (f : M →* N) : (l.map f).prod = f l.prod := by { simp only [prod, foldl_map, f.map_one.symm], exact l.foldl_hom _ _ _ 1 f.map_mul } @[to_additive] lemma prod_hom₂ (l : list ι) (f : M → N → P) (hf : ∀ a b c d, f (a * b) (c * d) = f a c * f b d) (hf' : f 1 1 = 1) (f₁ : ι → M) (f₂ : ι → N) : (l.map $ λ i, f (f₁ i) (f₂ i)).prod = f (l.map f₁).prod (l.map f₂).prod := begin simp only [prod, foldl_map], convert l.foldl_hom₂ (λ a b, f a b) _ _ _ _ _ (λ a b i, _), { exact hf'.symm }, { exact hf _ _ _ _ } end @[to_additive] lemma prod_map_hom (L : list ι) (f : ι → M) (g : M →* N) : (L.map (g ∘ f)).prod = g ((L.map f).prod) := by rw [← prod_hom, map_map] @[to_additive] lemma prod_is_unit : Π {L : list M} (u : ∀ m ∈ L, is_unit m), is_unit L.prod | [] _ := by simp | (h :: t) u := begin simp only [list.prod_cons], exact is_unit.mul (u h (mem_cons_self h t)) (prod_is_unit (λ m mt, u m (mem_cons_of_mem h mt))) end @[simp, to_additive] lemma prod_take_mul_prod_drop : ∀ (L : list M) (i : ℕ), (L.take i).prod * (L.drop i).prod = L.prod | [] i := by simp | L 0 := by simp | (h :: t) (n+1) := by { dsimp, rw [prod_cons, prod_cons, mul_assoc, prod_take_mul_prod_drop] } @[simp, to_additive] lemma prod_take_succ : ∀ (L : list M) (i : ℕ) (p), (L.take (i + 1)).prod = (L.take i).prod * L.nth_le i p | [] i p := by cases p | (h :: t) 0 _ := by simp | (h :: t) (n+1) _ := by { dsimp, rw [prod_cons, prod_cons, prod_take_succ, mul_assoc] } /-- A list with product not one must have positive length. -/ @[to_additive] lemma length_pos_of_prod_ne_one (L : list M) (h : L.prod ≠ 1) : 0 < L.length := by { cases L, { simp at h, cases h }, { simp } } @[to_additive] lemma prod_update_nth : ∀ (L : list M) (n : ℕ) (a : M), (L.update_nth n a).prod = (L.take n).prod * (if n < L.length then a else 1) * (L.drop (n + 1)).prod | (x :: xs) 0 a := by simp [update_nth] | (x :: xs) (i+1) a := by simp [update_nth, prod_update_nth xs i a, mul_assoc] | [] _ _ := by simp [update_nth, (nat.zero_le _).not_lt] open mul_opposite /-- We'd like to state this as `L.head * L.tail.prod = L.prod`, but because `L.head` relies on an inhabited instance to return a garbage value on the empty list, this is not possible. Instead, we write the statement in terms of `(L.nth 0).get_or_else 1`. -/ @[to_additive] lemma nth_zero_mul_tail_prod (l : list M) : (l.nth 0).get_or_else 1 * l.tail.prod = l.prod := by cases l; simp /-- Same as `nth_zero_mul_tail_prod`, but avoiding the `list.head` garbage complication by requiring the list to be nonempty. -/ @[to_additive] lemma head_mul_tail_prod_of_ne_nil [inhabited M] (l : list M) (h : l ≠ []) : l.head * l.tail.prod = l.prod := by cases l; [contradiction, simp] end monoid section monoid_with_zero variables [monoid_with_zero M₀] /-- If zero is an element of a list `L`, then `list.prod L = 0`. If the domain is a nontrivial monoid with zero with no divisors, then this implication becomes an `iff`, see `list.prod_eq_zero_iff`. -/ lemma prod_eq_zero {L : list M₀} (h : (0 : M₀) ∈ L) : L.prod = 0 := begin induction L with a L ihL, { exact absurd h (not_mem_nil _) }, { rw prod_cons, cases (mem_cons_iff _ _ _).1 h with ha hL, exacts [mul_eq_zero_of_left ha.symm _, mul_eq_zero_of_right _ (ihL hL)] } end /-- Product of elements of a list `L` equals zero if and only if `0 ∈ L`. See also `list.prod_eq_zero` for an implication that needs weaker typeclass assumptions. -/ @[simp] lemma prod_eq_zero_iff [nontrivial M₀] [no_zero_divisors M₀] {L : list M₀} : L.prod = 0 ↔ (0 : M₀) ∈ L := begin induction L with a L ihL, { simp }, { rw [prod_cons, mul_eq_zero, ihL, mem_cons_iff, eq_comm] } end lemma prod_ne_zero [nontrivial M₀] [no_zero_divisors M₀] {L : list M₀} (hL : (0 : M₀) ∉ L) : L.prod ≠ 0 := mt prod_eq_zero_iff.1 hL end monoid_with_zero section group variables [group G] /-- This is the `list.prod` version of `mul_inv_rev` -/ @[to_additive "This is the `list.sum` version of `add_neg_rev`"] lemma prod_inv_reverse : ∀ (L : list G), L.prod⁻¹ = (L.map (λ x, x⁻¹)).reverse.prod | [] := by simp | (x :: xs) := by simp [prod_inv_reverse xs] /-- A non-commutative variant of `list.prod_reverse` -/ @[to_additive "A non-commutative variant of `list.sum_reverse`"] lemma prod_reverse_noncomm : ∀ (L : list G), L.reverse.prod = (L.map (λ x, x⁻¹)).prod⁻¹ := by simp [prod_inv_reverse] /-- Counterpart to `list.prod_take_succ` when we have an inverse operation -/ @[simp, to_additive /-"Counterpart to `list.sum_take_succ` when we have an negation operation"-/] lemma prod_drop_succ : ∀ (L : list G) (i : ℕ) (p), (L.drop (i + 1)).prod = (L.nth_le i p)⁻¹ * (L.drop i).prod | [] i p := false.elim (nat.not_lt_zero _ p) | (x :: xs) 0 p := by simp | (x :: xs) (i + 1) p := prod_drop_succ xs i _ end group section comm_group variables [comm_group G] /-- This is the `list.prod` version of `mul_inv` -/ @[to_additive "This is the `list.sum` version of `add_neg`"] lemma prod_inv : ∀ (L : list G), L.prod⁻¹ = (L.map (λ x, x⁻¹)).prod | [] := by simp | (x :: xs) := by simp [mul_comm, prod_inv xs] /-- Alternative version of `list.prod_update_nth` when the list is over a group -/ @[to_additive /-"Alternative version of `list.sum_update_nth` when the list is over a group"-/] lemma prod_update_nth' (L : list G) (n : ℕ) (a : G) : (L.update_nth n a).prod = L.prod * (if hn : n < L.length then (L.nth_le n hn)⁻¹ * a else 1) := begin refine (prod_update_nth L n a).trans _, split_ifs with hn hn, { rw [mul_comm _ a, mul_assoc a, prod_drop_succ L n hn, mul_comm _ (drop n L).prod, ← mul_assoc (take n L).prod, prod_take_mul_prod_drop, mul_comm a, mul_assoc] }, { simp only [take_all_of_le (le_of_not_lt hn), prod_nil, mul_one, drop_eq_nil_of_le ((le_of_not_lt hn).trans n.le_succ)] } end end comm_group lemma eq_of_sum_take_eq [add_left_cancel_monoid M] {L L' : list M} (h : L.length = L'.length) (h' : ∀ i ≤ L.length, (L.take i).sum = (L'.take i).sum) : L = L' := begin apply ext_le h (λ i h₁ h₂, _), have : (L.take (i + 1)).sum = (L'.take (i + 1)).sum := h' _ (nat.succ_le_of_lt h₁), rw [sum_take_succ L i h₁, sum_take_succ L' i h₂, h' i (le_of_lt h₁)] at this, exact add_left_cancel this end lemma monotone_sum_take [canonically_ordered_add_monoid M] (L : list M) : monotone (λ i, (L.take i).sum) := begin apply monotone_nat_of_le_succ (λ n, _), by_cases h : n < L.length, { rw sum_take_succ _ _ h, exact le_self_add }, { push_neg at h, simp [take_all_of_le h, take_all_of_le (le_trans h (nat.le_succ _))] } end @[to_additive sum_nonneg] lemma one_le_prod_of_one_le [ordered_comm_monoid M] {l : list M} (hl₁ : ∀ x ∈ l, (1 : M) ≤ x) : 1 ≤ l.prod := begin induction l with hd tl ih, { simp }, rw prod_cons, exact one_le_mul (hl₁ hd (mem_cons_self hd tl)) (ih (λ x h, hl₁ x (mem_cons_of_mem hd h))), end @[to_additive sum_pos] lemma one_lt_prod_of_one_lt [ordered_comm_monoid M] : ∀ (l : list M) (hl : ∀ x ∈ l, (1 : M) < x) (hl₂ : l ≠ []), 1 < l.prod | [] _ h := (h rfl).elim | [b] h _ := by simpa using h | (a :: b :: l) hl₁ hl₂ := begin simp only [forall_eq_or_imp, list.mem_cons_iff _ a] at hl₁, rw list.prod_cons, apply one_lt_mul_of_lt_of_le' hl₁.1, apply le_of_lt ((b :: l).one_lt_prod_of_one_lt hl₁.2 (l.cons_ne_nil b)), end @[to_additive] lemma single_le_prod [ordered_comm_monoid M] {l : list M} (hl₁ : ∀ x ∈ l, (1 : M) ≤ x) : ∀ x ∈ l, x ≤ l.prod := begin induction l, { simp }, simp_rw [prod_cons, forall_mem_cons] at ⊢ hl₁, split, { exact le_mul_of_one_le_right' (one_le_prod_of_one_le hl₁.2) }, { exact λ x H, le_mul_of_one_le_of_le hl₁.1 (l_ih hl₁.right x H) }, end @[to_additive all_zero_of_le_zero_le_of_sum_eq_zero] lemma all_one_of_le_one_le_of_prod_eq_one [ordered_comm_monoid M] {l : list M} (hl₁ : ∀ x ∈ l, (1 : M) ≤ x) (hl₂ : l.prod = 1) {x : M} (hx : x ∈ l) : x = 1 := le_antisymm (hl₂ ▸ single_le_prod hl₁ _ hx) (hl₁ x hx) @[to_additive] lemma prod_eq_one_iff [canonically_ordered_monoid M] (l : list M) : l.prod = 1 ↔ ∀ x ∈ l, x = (1 : M) := ⟨all_one_of_le_one_le_of_prod_eq_one (λ _ _, one_le _), begin induction l, { simp }, { intro h, rw [prod_cons, mul_eq_one_iff], rw forall_mem_cons at h, exact ⟨h.1, l_ih h.2⟩ }, end⟩ /-- If all elements in a list are bounded below by `1`, then the length of the list is bounded by the sum of the elements. -/ lemma length_le_sum_of_one_le (L : list ℕ) (h : ∀ i ∈ L, 1 ≤ i) : L.length ≤ L.sum := begin induction L with j L IH h, { simp }, rw [sum_cons, length, add_comm], exact add_le_add (h _ (set.mem_insert _ _)) (IH (λ i hi, h i (set.mem_union_right _ hi))) end /-- A list with positive sum must have positive length. -/ -- This is an easy consequence of `length_pos_of_sum_ne_zero`, but often useful in applications. lemma length_pos_of_sum_pos [ordered_cancel_add_comm_monoid M] (L : list M) (h : 0 < L.sum) : 0 < L.length := length_pos_of_sum_ne_zero L h.ne' -- TODO: develop theory of tropical rings lemma sum_le_foldr_max [add_monoid M] [add_monoid N] [linear_order N] (f : M → N) (h0 : f 0 ≤ 0) (hadd : ∀ x y, f (x + y) ≤ max (f x) (f y)) (l : list M) : f l.sum ≤ (l.map f).foldr max 0 := begin induction l with hd tl IH, { simpa using h0 }, simp only [list.sum_cons, list.foldr_map, le_max_iff, list.foldr] at IH ⊢, cases le_or_lt (f tl.sum) (f hd), { left, refine (hadd _ _).trans _, simpa using h }, { right, refine (hadd _ _).trans _, simp only [IH, max_le_iff, and_true, h.le.trans IH] } end @[simp, to_additive] lemma prod_erase [decidable_eq M] [comm_monoid M] {a} : ∀ {l : list M}, a ∈ l → a * (l.erase a).prod = l.prod | (b :: l) h := begin obtain rfl | ⟨ne, h⟩ := decidable.list.eq_or_ne_mem_of_mem h, { simp only [list.erase, if_pos, prod_cons] }, { simp only [list.erase, if_neg (mt eq.symm ne), prod_cons, prod_erase h, mul_left_comm a b] } end lemma dvd_prod [comm_monoid M] {a} {l : list M} (ha : a ∈ l) : a ∣ l.prod := let ⟨s, t, h⟩ := mem_split ha in by { rw [h, prod_append, prod_cons, mul_left_comm], exact dvd_mul_right _ _ } @[simp] lemma sum_const_nat (m n : ℕ) : sum (list.repeat m n) = m * n := by induction n; [refl, simp only [*, repeat_succ, sum_cons, nat.mul_succ, add_comm]] lemma dvd_sum [semiring R] {a} {l : list R} (h : ∀ x ∈ l, a ∣ x) : a ∣ l.sum := begin induction l with x l ih, { exact dvd_zero _ }, { rw [list.sum_cons], exact dvd_add (h _ (mem_cons_self _ _)) (ih (λ x hx, h x (mem_cons_of_mem _ hx))) } end lemma exists_lt_of_sum_lt [linear_ordered_cancel_add_comm_monoid M] {l : list ι} (f g : ι → M) (h : (l.map f).sum < (l.map g).sum) : ∃ x ∈ l, f x < g x := begin induction l with x l, { exact (lt_irrefl _ h).elim }, obtain h' | h' := lt_or_le (f x) (g x), { exact ⟨x, mem_cons_self _ _, h'⟩ }, simp at h, obtain ⟨y, h1y, h2y⟩ := l_ih (lt_of_add_lt_add_left (h.trans_le $ add_le_add_right h' _)), exact ⟨y, mem_cons_of_mem x h1y, h2y⟩, end lemma exists_le_of_sum_le [linear_ordered_cancel_add_comm_monoid M] {l : list ι} (hl : l ≠ []) (f g : ι → M) (h : (l.map f).sum ≤ (l.map g).sum) : ∃ x ∈ l, f x ≤ g x := begin cases l with x l, { contradiction }, obtain h' | h' := le_or_lt (f x) (g x), { exact ⟨x, mem_cons_self _ _, h'⟩ }, obtain ⟨y, h1y, h2y⟩ := exists_lt_of_sum_lt f g _, exact ⟨y, mem_cons_of_mem x h1y, le_of_lt h2y⟩, simp at h, exact lt_of_add_lt_add_left (h.trans_lt $ add_lt_add_right h' _), end /-- The product of a list of positive natural numbers is positive, and likewise for any nontrivial ordered semiring. -/ lemma prod_pos [ordered_semiring R] [nontrivial R] (l : list R) (h : ∀ a ∈ l, (0 : R) < a) : 0 < l.prod := begin induction l with a l ih, { simp }, { rw prod_cons, exact mul_pos (h _ $ mem_cons_self _ _) (ih $ λ a ha, h a $ mem_cons_of_mem _ ha) } end /-! Several lemmas about sum/head/tail for `list ℕ`. These are hard to generalize well, as they rely on the fact that `default ℕ = 0`. If desired, we could add a class stating that `default = 0`. -/ /-- This relies on `default ℕ = 0`. -/ lemma head_add_tail_sum (L : list ℕ) : L.head + L.tail.sum = L.sum := by { cases L, { simp, refl }, { simp } } /-- This relies on `default ℕ = 0`. -/ lemma head_le_sum (L : list ℕ) : L.head ≤ L.sum := nat.le.intro (head_add_tail_sum L) /-- This relies on `default ℕ = 0`. -/ lemma tail_sum (L : list ℕ) : L.tail.sum = L.sum - L.head := by rw [← head_add_tail_sum L, add_comm, add_tsub_cancel_right] section alternating variables [comm_group G] @[simp, to_additive] lemma alternating_prod_nil : alternating_prod ([] : list G) = 1 := rfl @[simp, to_additive] lemma alternating_prod_singleton (g : G) : alternating_prod [g] = g := rfl @[simp, to_additive alternating_sum_cons_cons'] lemma alternating_prod_cons_cons (g h : G) (l : list G) : alternating_prod (g :: h :: l) = g * h⁻¹ * alternating_prod l := rfl lemma alternating_sum_cons_cons {G : Type*} [add_comm_group G] (g h : G) (l : list G) : alternating_sum (g :: h :: l) = g - h + alternating_sum l := by rw [sub_eq_add_neg, alternating_sum] end alternating lemma sum_map_mul_left [semiring R] (L : list ι) (f : ι → R) (r : R) : (L.map (λ b, r * f b)).sum = r * (L.map f).sum := sum_map_hom L f $ add_monoid_hom.mul_left r lemma sum_map_mul_right [semiring R] (L : list ι) (f : ι → R) (r : R) : (L.map (λ b, f b * r)).sum = (L.map f).sum * r := sum_map_hom L f $ add_monoid_hom.mul_right r end list namespace mul_opposite open list variables [monoid M] lemma op_list_prod : ∀ (l : list M), op (l.prod) = (l.map op).reverse.prod | [] := rfl | (x :: xs) := by rw [list.prod_cons, list.map_cons, list.reverse_cons', list.prod_concat, op_mul, op_list_prod] lemma _root_.mul_opposite.unop_list_prod (l : list Mᵐᵒᵖ) : (l.prod).unop = (l.map unop).reverse.prod := by rw [← op_inj, op_unop, mul_opposite.op_list_prod, map_reverse, map_map, reverse_reverse, op_comp_unop, map_id] end mul_opposite namespace monoid_hom variables [monoid M] [monoid N] @[to_additive] lemma map_list_prod (f : M →* N) (l : list M) : f l.prod = (l.map f).prod := (l.prod_hom f).symm /-- A morphism into the opposite monoid acts on the product by acting on the reversed elements -/ lemma unop_map_list_prod (f : M →* Nᵐᵒᵖ) (l : list M) : (f l.prod).unop = (l.map (mul_opposite.unop ∘ f)).reverse.prod := by rw [f.map_list_prod l, mul_opposite.unop_list_prod, list.map_map] end monoid_hom
f21ebd04fe39c4601dbc8a43f596dfc00b8a6982
d406927ab5617694ec9ea7001f101b7c9e3d9702
/archive/imo/imo1972_q5.lean
21178464e58dd0691fe95e82073bbcae524148d7
[ "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,244
lean
/- Copyright (c) 2020 Ruben Van de Velde, Stanislas Polu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ruben Van de Velde, Stanislas Polu -/ import data.real.basic import analysis.normed_space.basic /-! # IMO 1972 Q5 Problem: `f` and `g` are real-valued functions defined on the real line. For all `x` and `y`, `f(x + y) + f(x - y) = 2f(x)g(y)`. `f` is not identically zero and `|f(x)| ≤ 1` for all `x`. Prove that `|g(x)| ≤ 1` for all `x`. -/ /-- This proof begins by introducing the supremum of `f`, `k ≤ 1` as well as `k' = k / ‖g y‖`. We then suppose that the conclusion does not hold (`hneg`) and show that `k ≤ k'` (by `2 * (‖f x‖ * ‖g y‖) ≤ 2 * k` obtained from the main hypothesis `hf1`) and that `k' < k` (obtained from `hneg` directly), finally raising a contradiction with `k' < k'`. (Authored by Stanislas Polu inspired by Ruben Van de Velde). -/ example (f g : ℝ → ℝ) (hf1 : ∀ x, ∀ y, (f(x+y) + f(x-y)) = 2 * f(x) * g(y)) (hf2 : ∀ y, ‖f(y)‖ ≤ 1) (hf3 : ∃ x, f(x) ≠ 0) (y : ℝ) : ‖g(y)‖ ≤ 1 := begin set S := set.range (λ x, ‖f x‖), -- Introduce `k`, the supremum of `f`. let k : ℝ := Sup (S), -- Show that `‖f x‖ ≤ k`. have hk₁ : ∀ x, ‖f x‖ ≤ k, { have h : bdd_above S, from ⟨1, set.forall_range_iff.mpr hf2⟩, intro x, exact le_cSup h (set.mem_range_self x), }, -- Show that `2 * (‖f x‖ * ‖g y‖) ≤ 2 * k`. have hk₂ : ∀ x, 2 * (‖f x‖ * ‖g y‖) ≤ 2 * k, { intro x, calc 2 * (‖f x‖ * ‖g y‖) = ‖2 * f x * g y‖ : by simp [abs_mul, mul_assoc] ... = ‖f (x + y) + f (x - y)‖ : by rw hf1 ... ≤ ‖f (x + y)‖ + ‖f (x - y)‖ : norm_add_le _ _ ... ≤ k + k : add_le_add (hk₁ _) (hk₁ _) ... = 2 * k : (two_mul _).symm, }, -- Suppose the conclusion does not hold. by_contra' hneg, set k' := k / ‖g y‖, -- Demonstrate that `k' < k` using `hneg`. have H₁ : k' < k, { have h₁ : 0 < k, { obtain ⟨x, hx⟩ := hf3, calc 0 < ‖f x‖ : norm_pos_iff.mpr hx ... ≤ k : hk₁ x }, rw div_lt_iff, apply lt_mul_of_one_lt_right h₁ hneg, exact trans zero_lt_one hneg }, -- Demonstrate that `k ≤ k'` using `hk₂`. have H₂ : k ≤ k', { have h₁ : ∃ x : ℝ, x ∈ S, { use ‖f 0‖, exact set.mem_range_self 0, }, have h₂ : ∀ x, ‖f x‖ ≤ k', { intros x, rw le_div_iff, { apply (mul_le_mul_left zero_lt_two).mp (hk₂ x) }, { exact trans zero_lt_one hneg } }, apply cSup_le h₁, rintros y' ⟨yy, rfl⟩, exact h₂ yy }, -- Conclude by obtaining a contradiction, `k' < k'`. apply lt_irrefl k', calc k' < k : H₁ ... ≤ k' : H₂, end /-- IMO 1972 Q5 Problem: `f` and `g` are real-valued functions defined on the real line. For all `x` and `y`, `f(x + y) + f(x - y) = 2f(x)g(y)`. `f` is not identically zero and `|f(x)| ≤ 1` for all `x`. Prove that `|g(x)| ≤ 1` for all `x`. This is a more concise version of the proof proposed by Ruben Van de Velde. -/ example (f g : ℝ → ℝ) (hf1 : ∀ x, ∀ y, (f (x+y) + f(x-y)) = 2 * f(x) * g(y)) (hf2 : bdd_above (set.range (λ x, ‖f x‖))) (hf3 : ∃ x, f(x) ≠ 0) (y : ℝ) : ‖g(y)‖ ≤ 1 := begin obtain ⟨x, hx⟩ := hf3, set k := ⨆ x, ‖f x‖, have h : ∀ x, ‖f x‖ ≤ k := le_csupr hf2, by_contra' H, have hgy : 0 < ‖g y‖, by linarith, have k_pos : 0 < k := lt_of_lt_of_le (norm_pos_iff.mpr hx) (h x), have : k / ‖g y‖ < k := (div_lt_iff hgy).mpr (lt_mul_of_one_lt_right k_pos H), have : k ≤ k / ‖g y‖, { suffices : ∀ x, ‖f x‖ ≤ k / ‖g y‖, from csupr_le this, intro x, suffices : 2 * (‖f x‖ * ‖g y‖) ≤ 2 * k, by { rwa [le_div_iff hgy, ←mul_le_mul_left (zero_lt_two : (0 : ℝ) < 2)] }, calc 2 * (‖f x‖ * ‖g y‖) = ‖2 * f x * g y‖ : by simp [abs_mul, mul_assoc] ... = ‖f (x + y) + f (x - y)‖ : by rw hf1 ... ≤ ‖f (x + y)‖ + ‖f (x - y)‖ : abs_add _ _ ... ≤ 2 * k : by linarith [h (x+y), h (x -y)] }, linarith, end
30de73645e53b614b17aba69acb076ee921d5b71
ae9f8bf05de0928a4374adc7d6b36af3411d3400
/src/formal_ml/measurable_space.lean
3a8e59e49a5155f17992e5c5a44f16000cdcea8f
[ "Apache-2.0" ]
permissive
NeoTim/formal-ml
bc42cf6beba9cd2ed56c1cd054ab4eb5402ed445
c9cbad2837104160a9832a29245471468748bb8d
refs/heads/master
1,671,549,160,900
1,601,362,989,000
1,601,362,989,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
18,235
lean
/- Copyright 2020 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -/ import measure_theory.measurable_space import measure_theory.measure_space import formal_ml.set import formal_ml.finset import formal_ml.classical lemma set_Prop_le_def {α:Type*} (M M2:set α → Prop): M ≤ M2 ↔ (∀ X:set α, M X → M2 X) := begin refl, end lemma finset_union_measurable {α:Type*} {T:finset α} {β:Type*} [measurable_space β] {U:α → set β}: (∀ t∈ T, is_measurable (U t)) → is_measurable (⋃ x ∈ T, U x) := begin intros, have A1:(set.sUnion (set.image U ({a|a∈ T}:set α))) = (⋃ x ∈ T, U x), { simp, }, rw ← A1, apply is_measurable.sUnion, { apply set.countable.image, apply set.finite.countable, apply finite_finset, }, { intros, simp at H, cases H with x H, cases H with A2 A3, subst t, apply a, exact A2, } end lemma finset_inter_measurable {α:Type*} {T:finset α} {β:Type*} [measurable_space β] {U:α → set β}: (∀ t∈ T, is_measurable (U t)) → is_measurable (⋂ x ∈ T, U x) := begin intros, have A1:(set.sInter (set.image U ({a|a∈ T}:set α))) = (⋂ x ∈ T, U x), { simp, }, rw ← A1, apply is_measurable.sInter, { apply set.countable.image, apply set.finite.countable, apply finite_finset, }, { intros, simp at H, cases H with x H, cases H with A2 A3, subst t, apply a, exact A2, } end lemma measurable_space_le_def {α:Type*} (M:measurable_space α) (M2:measurable_space α): M.is_measurable' ≤ M2.is_measurable' ↔ M ≤ M2 := begin refl, end lemma measurable_space_le_def2 {α:Type*} (M:measurable_space α) (M2:measurable_space α): (∀ X:set α, M.is_measurable' X → M2.is_measurable' X) ↔ M ≤ M2 := begin intros, apply iff.trans, { apply set_Prop_le_def, }, { apply measurable_space_le_def, } end -- Delete? lemma measurable_space_le_intro {α:Type*} (M:measurable_space α) (M2:measurable_space α): (∀ X:set α, M.is_measurable' X → M2.is_measurable' X) → M ≤ M2 := begin intros, have A1:M.is_measurable' ≤ M2.is_measurable' ↔ M ≤ M2, { apply measurable_space_le_def, }, apply A1.mp, have A2:M.is_measurable' ≤ M2.is_measurable' ↔ (∀ X:set α, M.is_measurable' X → M2.is_measurable' X), { apply set_Prop_le_def, }, apply A2.mpr, apply a, end lemma measurable_def {α β:Type*} [M1:measurable_space α] [M2:measurable_space β] (f:α → β): (∀ B:(set β), (is_measurable B) → is_measurable (f ⁻¹' B)) ↔ (measurable f) := begin unfold measurable, end lemma measurable_intro {α β:Type*} [measurable_space α] [measurable_space β] (f:α → β): (∀ B:(set β), is_measurable B → is_measurable (f ⁻¹' B)) → (measurable f) := begin apply (measurable_def _).mp, end lemma measurable_elim {α β:Type*} [measurable_space α] [measurable_space β] (f:α → β) (B:set β): (measurable f)→ (is_measurable B) → (is_measurable (f ⁻¹' B)) := begin intros, apply (measurable_def _).mpr, apply a, apply a_1, end lemma measurable_fun_product_measurableh {α β:Type*} [M1:measurable_space α] [M2:measurable_space β]: (@prod.measurable_space α β M1 M2) = M1.comap prod.fst ⊔ M2.comap prod.snd := begin refl end lemma comap_elim {α β:Type*} [M2:measurable_space β] (f:α → β) (B:set β): (is_measurable B) → (M2.comap f).is_measurable' (set.preimage f B) := begin intros, unfold measurable_space.comap, simp, apply exists.intro B, split, apply a, refl end lemma measurable_comap {α β:Type*} [M1:measurable_space α] [M2:measurable_space β] (f:α → β): (M2.comap f) ≤ M1 → measurable f := begin intros, apply measurable_intro, intros, have A1:(M2.comap f).is_measurable' (set.preimage f B), { apply comap_elim, apply a_1, }, rw ← measurable_space_le_def2 at a, apply a, apply A1, end lemma fst_measurable {α β:Type*} [M1:measurable_space α] [M2:measurable_space β]:measurable (λ x:(α × β), x.fst) := begin apply measurable_comap, have A1:M1.comap prod.fst ≤ (@prod.measurable_space α β M1 M2), { rw measurable_fun_product_measurableh, apply complete_lattice.le_sup_left (M1.comap prod.fst) (M2.comap prod.snd), }, apply A1, end lemma snd_measurable {α β:Type*} [M1:measurable_space α] [M2:measurable_space β]:measurable (λ x:(α × β), x.snd) := begin apply measurable_comap, have A1:M2.comap prod.snd ≤ (@prod.measurable_space α β M1 M2), { rw measurable_fun_product_measurableh, apply complete_lattice.le_sup_right (M1.comap prod.fst) (M2.comap prod.snd), }, apply A1, end lemma comap_def {α β:Type*} {B:set (set β)} (f:α → β): @measurable_space.comap α β f (measurable_space.generate_from B) = (measurable_space.generate_from (set.image (set.preimage f) B)) := begin apply measurable_space.comap_generate_from, end lemma comap_fst_def {α β:Type*} {Bα:set (set α)}: (measurable_space.generate_from Bα).comap (@prod.fst α β) = measurable_space.generate_from {U:set (α × β)|∃ A∈ Bα, U = set.prod A set.univ} := begin rw measurable_space.comap_generate_from, rw set.preimage_fst_def, end lemma comap_snd_def {α β:Type*} {Bβ:set (set β)}: (measurable_space.generate_from Bβ).comap (@prod.snd α β) = measurable_space.generate_from {U:set (α × β)|∃ B∈ Bβ, U = set.prod set.univ B} := begin rw measurable_space.comap_generate_from, rw set.preimage_snd_def, end lemma measurable_space_sup_def {α:Type*} {B C:set (set α)}: (measurable_space.generate_from B) ⊔ (measurable_space.generate_from C) = (measurable_space.generate_from (B ∪ C)) := begin apply measurable_space.generate_from_sup_generate_from, end lemma prod_measurable_space_def {α β:Type*} {Bα:set (set α)} {Bβ:set (set β)}: (@prod.measurable_space α β (measurable_space.generate_from Bα) (measurable_space.generate_from Bβ)) = @measurable_space.generate_from (α × β) ( {U:set (α × β)|∃ A∈ Bα, U = set.prod A set.univ} ∪ {U:set (α × β)|∃ B∈ Bβ, U = set.prod set.univ B}) := begin rw measurable_fun_product_measurableh, rw comap_fst_def, rw comap_snd_def, rw measurable_space_sup_def, end lemma set.sUnion_eq_univ_elim {α:Type*} {S:set (set α)} (a:α): (set.sUnion S = set.univ) → (∃ T∈S, a∈ T) := begin intro A1, have A2:a∈ set.univ := set.mem_univ a, rw ← A1 at A2, simp at A2, cases A2 with T A2, apply exists.intro T, apply exists.intro A2.left, apply A2.right, end lemma prod_measurable_space_le {α β:Type*} {Bα:set (set α)} {Bβ:set (set β)}: @measurable_space.generate_from (α × β) {U:set (α × β)|∃ A∈ Bα, ∃ B∈Bβ, U = set.prod A B} ≤ (@prod.measurable_space α β (measurable_space.generate_from Bα) (measurable_space.generate_from Bβ)) := begin rw prod_measurable_space_def, apply measurable_space.generate_from_le, intros X A5, simp at A5, cases A5 with A A5, cases A5 with A5 A6, cases A6 with B A6, cases A6 with A6 A7, have A8:(set.prod A (@set.univ β)) ∩ (set.prod (@set.univ α) B) = set.prod A B, { ext p,split;intros A3A;{ simp at A3A, simp, --cases p, apply A3A, }, }, rw ← A8 at A7, rw A7, apply is_measurable.inter, { apply measurable_space.is_measurable_generate_from, apply set.mem_union_left, simp, apply exists.intro A, split, apply A5, refl, }, { apply measurable_space.is_measurable_generate_from, apply set.mem_union_right, simp, apply exists.intro B, split, apply A6, refl, }, end lemma prod_measurable_space_def2 {α β:Type*} {Bα:set (set α)} {Bβ:set (set β)} {Cα:set (set α)} {Cβ:set (set β)}: (set.countable Cα) → (set.countable Cβ) → (Cα ⊆ Bα) → (Cβ ⊆ Bβ) → (set.sUnion Cα = set.univ) → (set.sUnion Cβ = set.univ) → (@prod.measurable_space α β (measurable_space.generate_from Bα) (measurable_space.generate_from Bβ)) = @measurable_space.generate_from (α × β) {U:set (α × β)|∃ A∈ Bα, ∃ B∈Bβ, U = set.prod A B} := begin intros A1 A2 A3 A4 AX1 AX2, --rw prod_measurable_space_def, apply le_antisymm, { rw prod_measurable_space_def, apply measurable_space.generate_from_le, intros X A5, simp at A5, cases A5, { cases A5 with A A5, cases A5 with A5 A6, have A7:X = set.sUnion (set.image (set.prod A) Cβ), { rw A6, ext a,split;intro A7A;simp;simp at A7A, { have A7B := set.sUnion_eq_univ_elim a.snd AX2, cases A7B with i A7B, cases A7B with A7B A7C, apply exists.intro i, apply and.intro A7B (and.intro A7A A7C), }, { cases A7A with i A7A, apply A7A.right.left, }, }, rw A7, --apply measurable_space.is_measurable_generate_from, apply is_measurable.sUnion, apply set.countable.image, apply A2, intro U, intro A8, simp at A8, cases A8 with B A8, cases A8 with A8 A9, subst U, apply measurable_space.is_measurable_generate_from, simp, apply exists.intro A, split, apply A5, apply exists.intro B, split, rw set.subset_def at A4, apply A4, apply A8, refl, }, { cases A5 with B A5, cases A5 with A5 A6, have A7:X = set.sUnion (set.image (λ x, set.prod x B) Cα), { rw A6, ext a,split;intro A7A;simp;simp at A7A, { have A7B := set.sUnion_eq_univ_elim a.fst AX1, cases A7B with i A7B, cases A7B with A7B A7C, apply exists.intro i, apply and.intro A7B (and.intro A7C A7A), }, { cases A7A with i A7A, apply A7A.right.right, }, }, rw A7, --apply measurable_space.is_measurable_generate_from, apply is_measurable.sUnion, apply set.countable.image, apply A1, intro U, intro A8, simp at A8, cases A8 with A A8, cases A8 with A8 A9, subst U, apply measurable_space.is_measurable_generate_from, simp, apply exists.intro A, split, rw set.subset_def at A3, apply A3, apply A8, apply exists.intro B, split, apply A5, refl, }, }, { apply prod_measurable_space_le, } end lemma preimage_compl {α β:Type*} (f:α → β) (S:set β): (f ⁻¹' Sᶜ) = ((f ⁻¹' S)ᶜ) := begin ext, split;intros, { intro, unfold set.preimage at a, simp at a, apply a, apply a_1, }, { unfold set.preimage, simp, intro, apply a, apply a_1, } end lemma preimage_Union {α β:Type*} (f:α → β) (g:ℕ → set β): (f ⁻¹' ⋃ (i : ℕ), g i)=(⋃ (i : ℕ), f ⁻¹' (g i)) := begin ext, split;intros, { cases a with B a, cases a with H a, cases H with y H, split, simp, split, apply exists.intro y, { simp at H, }, { simp at H, subst B, apply a, } }, { cases a with A a, cases a with A1 A2, cases A1 with i A3, simp at A3, subst A, split, simp, split, { apply exists.intro i, refl, }, { apply A2, }, } end lemma generate_from_measurable {α β:Type*} [M:measurable_space α] [M2:measurable_space β] (X:set (set β)) (f:α → β): (measurable_space.generate_from X = M2)→ (∀ B∈ X, is_measurable (set.preimage f B))→ (measurable f) := begin intros, apply measurable_intro, intros, have A1:@is_measurable β (measurable_space.generate_from X) B, { rw a, apply a_2, }, clear a_2, -- Important for induction later. have A2:measurable_space.generate_measurable X B, { apply A1, }, induction A2, { apply a_1, apply A2_H, }, { simp, }, { -- ⊢ is_measurable (f ⁻¹' -A2_s) rw preimage_compl, apply measurable_space.is_measurable_compl, apply A2_ih, { apply (is_measurable.compl_iff).mp, apply A1, }, }, { rw preimage_Union, apply measurable_space.is_measurable_Union, intros, apply A2_ih, { apply A2_a, } } end lemma generate_from_self {α:Type*} (M:measurable_space α): M = measurable_space.generate_from {s : set α|measurable_space.is_measurable' M s} := begin ext, split;intros, { apply measurable_space.generate_measurable.basic, apply a, }, { induction a, { apply a_H, }, { apply measurable_space.is_measurable_empty, }, { apply measurable_space.is_measurable_compl, apply a_ih, }, { apply measurable_space.is_measurable_Union, apply a_ih, }, } end lemma measurable_fun_comap_def {α β:Type*} [M2:measurable_space β] (f:α → β): measurable_space.comap f M2 = measurable_space.generate_from {s : set α|∃ (s' : set β), measurable_space.is_measurable' M2 s' ∧ f ⁻¹' s' = s} := begin unfold measurable_space.comap, apply generate_from_self, end lemma measurable_fun_product_measurable {α β γ:Type*} [M1:measurable_space α] [M2:measurable_space β] [M3:measurable_space γ] (X: α → β) (Y: α → γ): measurable X → measurable Y → measurable (λ a:α, prod.mk (X a) (Y a)) := begin intros B1 B2, have A1:@measurable _ _ _ (@prod.measurable_space β γ M2 M3) (λ a:α, prod.mk (X a) (Y a)), { have A1A:(@prod.measurable_space β γ M2 M3)=measurable_space.generate_from ( {s : set (β × γ) | ∃ (s' : set β), measurable_space.is_measurable' M2 s' ∧ prod.fst ⁻¹' s' = s} ∪ {s : set (β × γ) | ∃ (s' : set γ), measurable_space.is_measurable' M3 s' ∧ prod.snd ⁻¹' s' = s}), { rw measurable_fun_product_measurableh, rw measurable_fun_comap_def, rw measurable_fun_comap_def, rw measurable_space.generate_from_sup_generate_from, }, rw A1A, apply generate_from_measurable, { refl, }, { intro BC, intros, cases H, { cases H with B H, cases H, subst BC, have A1B:(λ (a : α), (X a, Y a)) ⁻¹' (prod.fst ⁻¹' B) = (X ⁻¹' B), { ext,split;intros, { simp at a, apply a, }, { simp, apply a, } }, rw A1B, apply B1, apply H_left, }, { cases H with C H, cases H, subst BC, have A1C:(λ (a : α), (X a, Y a)) ⁻¹' (prod.snd ⁻¹' C) = (Y ⁻¹' C), { ext,split;intros, { simp at a, apply a, }, { simp, apply a, } }, rw A1C, apply B2, apply H_left, } } }, apply A1, end lemma compose_measurable_fun_measurable {α β γ:Type*} [measurable_space α] [measurable_space β] [measurable_space γ] (X:β → γ) (Y: α→ β): measurable X → measurable Y → measurable (X ∘ Y) := begin intros B1 B2, apply measurable_intro, intros, have A1:(X ∘ Y ⁻¹' B)=(Y ⁻¹' (X ⁻¹' B)), { refl, }, rw A1, apply measurable_elim Y _ B2, apply measurable_elim X _ B1, apply a end -- Constant functions are measurable. -- Different than is_measurable.const lemma const_measurable {Ω:Type*} [measurable_space Ω] {β:Type*} [measurable_space β] (c:β): (measurable (λ ω:Ω, c)) := begin apply measurable_const, end lemma is_measurable_of_le_of_is_measurable {α : Type*} {M1 : measurable_space α} {M2 : measurable_space α} {X:set α}: M1 ≤ M2 → measurable_space.is_measurable' M1 X → measurable_space.is_measurable' M2 X := begin intros A2 A1, rw ← measurable_space_le_def2 at A2, apply A2, apply A1, end -- cf. is_measurable_prod lemma is_measurable_prod' {β : Type*} {γ : Type*} {Mβ : measurable_space β} {Mγ : measurable_space γ} {X:set β} {Y:set γ}:is_measurable X → is_measurable Y → is_measurable (set.prod X Y) := begin --apply is_measurable_of_le_of_is_measurable, intros A1 A2, rw generate_from_self Mβ, rw generate_from_self Mγ, apply is_measurable_of_le_of_is_measurable, apply prod_measurable_space_le, apply measurable_space.is_measurable_generate_from, simp, apply exists.intro X, split, apply A1, apply exists.intro Y, split, apply A2, refl, end lemma measurable.preimage {α β:Type*} [measurable_space α] [measurable_space β] {f:α → β} {S:set β}:measurable f → is_measurable S → is_measurable (set.preimage f S) := begin intros A1 A2, apply A1, apply A2, end lemma measurable.if {α β:Type*} {Mα:measurable_space α} {Mβ:measurable_space β} {E:set α} {D:decidable_pred E} {X Y:α → β}:is_measurable E → measurable X → measurable Y → measurable (λ a:α, if (E a) then (X a) else (Y a)) := begin intros A1 A2 A3, intros S B1, rw preimage_if, apply is_measurable.union, { apply is_measurable.inter, apply A1, apply A2, apply B1, }, { apply is_measurable.inter, apply is_measurable.compl, apply A1, apply A3, apply B1, }, end
b2c8583c31e18da29b7cf302242c31c09ccb3850
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/data/equiv/basic.lean
a7f5894f52c73d3fb4c4a0cd6fab274d5a3d0c3b
[ "Apache-2.0" ]
permissive
DanielFabian/mathlib
efc3a50b5dde303c59eeb6353ef4c35a345d7112
f520d07eba0c852e96fe26da71d85bf6d40fcc2a
refs/heads/master
1,668,739,922,971
1,595,201,756,000
1,595,201,756,000
279,469,476
0
0
null
1,594,696,604,000
1,594,696,604,000
null
UTF-8
Lean
false
false
68,904
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro In the standard library we cannot assume the univalence axiom. We say two types are equivalent if they are isomorphic. Two equivalent types have the same cardinality. -/ import data.set.function import data.option.basic import algebra.group.basic open function universes u v w z variables {α : Sort u} {β : Sort v} {γ : Sort w} /-- `α ≃ β` is the type of functions from `α → β` with a two-sided inverse. -/ structure equiv (α : Sort*) (β : Sort*) := (to_fun : α → β) (inv_fun : β → α) (left_inv : left_inverse inv_fun to_fun) (right_inv : right_inverse inv_fun to_fun) infix ` ≃ `:25 := equiv /-- Convert an involutive function `f` to an equivalence with `to_fun = inv_fun = f`. -/ def function.involutive.to_equiv (f : α → α) (h : involutive f) : α ≃ α := ⟨f, f, h.left_inverse, h.right_inverse⟩ namespace equiv /-- `perm α` is the type of bijections from `α` to itself. -/ @[reducible] def perm (α : Sort*) := equiv α α instance : has_coe_to_fun (α ≃ β) := ⟨_, to_fun⟩ @[simp] theorem coe_fn_mk (f : α → β) (g l r) : (equiv.mk f g l r : α → β) = f := rfl /-- The map `coe_fn : (r ≃ s) → (r → s)` is injective. We can't use `function.injective` here but mimic its signature by using `⦃e₁ e₂⦄`. -/ theorem coe_fn_injective : ∀ ⦃e₁ e₂ : equiv α β⦄, (e₁ : α → β) = e₂ → e₁ = e₂ | ⟨f₁, g₁, l₁, r₁⟩ ⟨f₂, g₂, l₂, r₂⟩ h := have f₁ = f₂, from h, have g₁ = g₂, from l₁.eq_right_inverse (this.symm ▸ r₂), by simp * @[ext] lemma ext {f g : equiv α β} (H : ∀ x, f x = g x) : f = g := coe_fn_injective (funext H) @[ext] lemma perm.ext {σ τ : equiv.perm α} (H : ∀ x, σ x = τ x) : σ = τ := equiv.ext H /-- Any type is equivalent to itself. -/ @[refl] protected def refl (α : Sort*) : α ≃ α := ⟨id, id, λ x, rfl, λ x, rfl⟩ /-- Inverse of an equivalence `e : α ≃ β`. -/ @[symm] protected def symm (e : α ≃ β) : β ≃ α := ⟨e.inv_fun, e.to_fun, e.right_inv, e.left_inv⟩ /-- Composition of equivalences `e₁ : α ≃ β` and `e₂ : β ≃ γ`. -/ @[trans] protected def trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ := ⟨e₂.to_fun ∘ e₁.to_fun, e₁.inv_fun ∘ e₂.inv_fun, e₂.left_inv.comp e₁.left_inv, e₂.right_inv.comp e₁.right_inv⟩ @[simp] lemma to_fun_as_coe (e : α ≃ β) (a : α) : e.to_fun a = e a := rfl @[simp] lemma inv_fun_as_coe (e : α ≃ β) (b : β) : e.inv_fun b = e.symm b := rfl protected theorem injective (e : α ≃ β) : injective e := e.left_inv.injective protected theorem surjective (e : α ≃ β) : surjective e := e.right_inv.surjective protected theorem bijective (f : α ≃ β) : bijective f := ⟨f.injective, f.surjective⟩ @[simp] lemma range_eq_univ {α : Type*} {β : Type*} (e : α ≃ β) : set.range e = set.univ := set.eq_univ_of_forall e.surjective protected theorem subsingleton (e : α ≃ β) [subsingleton β] : subsingleton α := e.injective.comap_subsingleton /-- Transfer `decidable_eq` across an equivalence. -/ protected def decidable_eq (e : α ≃ β) [decidable_eq β] : decidable_eq α := e.injective.decidable_eq lemma nonempty_iff_nonempty (e : α ≃ β) : nonempty α ↔ nonempty β := nonempty.congr e e.symm /-- If `α ≃ β` and `β` is inhabited, then so is `α`. -/ protected def inhabited [inhabited β] (e : α ≃ β) : inhabited α := ⟨e.symm (default _)⟩ /-- If `α ≃ β` and `β` is a singleton type, then so is `α`. -/ protected def unique [unique β] (e : α ≃ β) : unique α := e.symm.surjective.unique /-- Equivalence between equal types. -/ protected def cast {α β : Sort*} (h : α = β) : α ≃ β := ⟨cast h, cast h.symm, λ x, by { cases h, refl }, λ x, by { cases h, refl }⟩ @[simp] theorem coe_fn_symm_mk (f : α → β) (g l r) : ((equiv.mk f g l r).symm : β → α) = g := rfl @[simp] theorem coe_refl : ⇑(equiv.refl α) = id := rfl theorem refl_apply (x : α) : equiv.refl α x = x := rfl @[simp] theorem coe_trans (f : α ≃ β) (g : β ≃ γ) : ⇑(f.trans g) = g ∘ f := rfl theorem trans_apply (f : α ≃ β) (g : β ≃ γ) (a : α) : (f.trans g) a = g (f a) := rfl @[simp] theorem apply_symm_apply (e : α ≃ β) (x : β) : e (e.symm x) = x := e.right_inv x @[simp] theorem symm_apply_apply (e : α ≃ β) (x : α) : e.symm (e x) = x := e.left_inv x @[simp] theorem symm_comp_self (e : α ≃ β) : e.symm ∘ e = id := funext e.symm_apply_apply @[simp] theorem self_comp_symm (e : α ≃ β) : e ∘ e.symm = id := funext e.apply_symm_apply @[simp] lemma symm_trans_apply (f : α ≃ β) (g : β ≃ γ) (a : γ) : (f.trans g).symm a = f.symm (g.symm a) := rfl @[simp] theorem apply_eq_iff_eq (f : α ≃ β) (x y : α) : f x = f y ↔ x = y := f.injective.eq_iff theorem apply_eq_iff_eq_symm_apply {α β : Sort*} (f : α ≃ β) (x : α) (y : β) : f x = y ↔ x = f.symm y := begin conv_lhs { rw ←apply_symm_apply f y, }, rw apply_eq_iff_eq, end @[simp] theorem cast_apply {α β} (h : α = β) (x : α) : equiv.cast h x = cast h x := rfl lemma symm_apply_eq {α β} (e : α ≃ β) {x y} : e.symm x = y ↔ x = e y := ⟨λ H, by simp [H.symm], λ H, by simp [H]⟩ lemma eq_symm_apply {α β} (e : α ≃ β) {x y} : y = e.symm x ↔ e y = x := (eq_comm.trans e.symm_apply_eq).trans eq_comm @[simp] theorem symm_symm (e : α ≃ β) : e.symm.symm = e := by { cases e, refl } @[simp] theorem trans_refl (e : α ≃ β) : e.trans (equiv.refl β) = e := by { cases e, refl } @[simp] theorem refl_symm : (equiv.refl α).symm = equiv.refl α := rfl @[simp] theorem refl_trans (e : α ≃ β) : (equiv.refl α).trans e = e := by { cases e, refl } @[simp] theorem symm_trans (e : α ≃ β) : e.symm.trans e = equiv.refl β := ext (by simp) @[simp] theorem trans_symm (e : α ≃ β) : e.trans e.symm = equiv.refl α := ext (by simp) lemma trans_assoc {δ} (ab : α ≃ β) (bc : β ≃ γ) (cd : γ ≃ δ) : (ab.trans bc).trans cd = ab.trans (bc.trans cd) := equiv.ext $ assume a, rfl theorem left_inverse_symm (f : equiv α β) : left_inverse f.symm f := f.left_inv theorem right_inverse_symm (f : equiv α β) : function.right_inverse f.symm f := f.right_inv /-- If `α` is equivalent to `β` and `γ` is equivalent to `δ`, then the type of equivalences `α ≃ γ` is equivalent to the type of equivalences `β ≃ δ`. -/ def equiv_congr {δ} (ab : α ≃ β) (cd : γ ≃ δ) : (α ≃ γ) ≃ (β ≃ δ) := ⟨ λac, (ab.symm.trans ac).trans cd, λbd, ab.trans $ bd.trans $ cd.symm, assume ac, by { ext x, simp }, assume ac, by { ext x, simp } ⟩ /-- If `α` is equivalent to `β`, then `perm α` is equivalent to `perm β`. -/ def perm_congr {α : Type*} {β : Type*} (e : α ≃ β) : perm α ≃ perm β := equiv_congr e e protected lemma image_eq_preimage {α β} (e : α ≃ β) (s : set α) : e '' s = e.symm ⁻¹' s := set.ext $ assume x, set.mem_image_iff_of_inverse e.left_inv e.right_inv protected lemma subset_image {α β} (e : α ≃ β) (s : set α) (t : set β) : t ⊆ e '' s ↔ e.symm '' t ⊆ s := by rw [set.image_subset_iff, e.image_eq_preimage] lemma symm_image_image {α β} (f : equiv α β) (s : set α) : f.symm '' (f '' s) = s := by { rw [← set.image_comp], simp } protected lemma image_compl {α β} (f : equiv α β) (s : set α) : f '' sᶜ = (f '' s)ᶜ := set.image_compl_eq f.bijective /- The group of permutations (self-equivalences) of a type `α` -/ namespace perm instance perm_group {α : Type u} : group (perm α) := begin refine { mul := λ f g, equiv.trans g f, one := equiv.refl α, inv:= equiv.symm, ..}; intros; apply equiv.ext; try { apply trans_apply }, apply symm_apply_apply end @[simp] theorem mul_apply {α : Type u} (f g : perm α) (x) : (f * g) x = f (g x) := equiv.trans_apply _ _ _ @[simp] theorem one_apply {α : Type u} (x) : (1 : perm α) x = x := rfl @[simp] lemma inv_apply_self {α : Type u} (f : perm α) (x) : f⁻¹ (f x) = x := equiv.symm_apply_apply _ _ @[simp] lemma apply_inv_self {α : Type u} (f : perm α) (x) : f (f⁻¹ x) = x := equiv.apply_symm_apply _ _ lemma one_def {α : Type u} : (1 : perm α) = equiv.refl α := rfl lemma mul_def {α : Type u} (f g : perm α) : f * g = g.trans f := rfl lemma inv_def {α : Type u} (f : perm α) : f⁻¹ = f.symm := rfl end perm /-- If `α` is an empty type, then it is equivalent to the `empty` type. -/ def equiv_empty (h : α → false) : α ≃ empty := ⟨λ x, (h x).elim, λ e, e.rec _, λ x, (h x).elim, λ e, e.rec _⟩ /-- `false` is equivalent to `empty`. -/ def false_equiv_empty : false ≃ empty := equiv_empty _root_.id /-- If `α` is an empty type, then it is equivalent to the `pempty` type in any universe. -/ def {u' v'} equiv_pempty {α : Sort v'} (h : α → false) : α ≃ pempty.{u'} := ⟨λ x, (h x).elim, λ e, e.rec _, λ x, (h x).elim, λ e, e.rec _⟩ /-- `false` is equivalent to `pempty`. -/ def false_equiv_pempty : false ≃ pempty := equiv_pempty _root_.id /-- `empty` is equivalent to `pempty`. -/ def empty_equiv_pempty : empty ≃ pempty := equiv_pempty $ empty.rec _ /-- `pempty` types from any two universes are equivalent. -/ def pempty_equiv_pempty : pempty.{v} ≃ pempty.{w} := equiv_pempty pempty.elim /-- If `α` is not `nonempty`, then it is equivalent to `empty`. -/ def empty_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : α ≃ empty := equiv_empty $ assume a, h ⟨a⟩ /-- If `α` is not `nonempty`, then it is equivalent to `pempty`. -/ def pempty_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : α ≃ pempty := equiv_pempty $ assume a, h ⟨a⟩ /-- The `Sort` of proofs of a true proposition is equivalent to `punit`. -/ def prop_equiv_punit {p : Prop} (h : p) : p ≃ punit := ⟨λ x, (), λ x, h, λ _, rfl, λ ⟨⟩, rfl⟩ /-- `true` is equivalent to `punit`. -/ def true_equiv_punit : true ≃ punit := prop_equiv_punit trivial /-- `ulift α` is equivalent to `α`. -/ protected def ulift {α : Type u} : ulift α ≃ α := ⟨ulift.down, ulift.up, ulift.up_down, λ a, rfl⟩ /-- `plift α` is equivalent to `α`. -/ protected def plift : plift α ≃ α := ⟨plift.down, plift.up, plift.up_down, plift.down_up⟩ /-- equivalence of propositions is the same as iff -/ def of_iff {P Q : Prop} (h : P ↔ Q) : P ≃ Q := { to_fun := h.mp, inv_fun := h.mpr, left_inv := λ x, rfl, right_inv := λ y, rfl } /-- If `α₁` is equivalent to `α₂` and `β₁` is equivalent to `β₂`, then the type of maps `α₁ → β₁` is equivalent to the type of maps `α₂ → β₂`. -/ @[congr] def arrow_congr {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (α₁ → β₁) ≃ (α₂ → β₂) := { to_fun := λ f, e₂.to_fun ∘ f ∘ e₁.inv_fun, inv_fun := λ f, e₂.inv_fun ∘ f ∘ e₁.to_fun, left_inv := λ f, funext $ λ x, by simp, right_inv := λ f, funext $ λ x, by simp } @[simp] lemma arrow_congr_apply {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) (f : α₁ → β₁) (x : α₂) : arrow_congr e₁ e₂ f x = (e₂ $ f $ e₁.symm x) := rfl lemma arrow_congr_comp {α₁ β₁ γ₁ α₂ β₂ γ₂ : Sort*} (ea : α₁ ≃ α₂) (eb : β₁ ≃ β₂) (ec : γ₁ ≃ γ₂) (f : α₁ → β₁) (g : β₁ → γ₁) : arrow_congr ea ec (g ∘ f) = (arrow_congr eb ec g) ∘ (arrow_congr ea eb f) := by { ext, simp only [comp, arrow_congr_apply, eb.symm_apply_apply] } @[simp] lemma arrow_congr_refl {α β : Sort*} : arrow_congr (equiv.refl α) (equiv.refl β) = equiv.refl (α → β) := rfl @[simp] lemma arrow_congr_trans {α₁ β₁ α₂ β₂ α₃ β₃ : Sort*} (e₁ : α₁ ≃ α₂) (e₁' : β₁ ≃ β₂) (e₂ : α₂ ≃ α₃) (e₂' : β₂ ≃ β₃) : arrow_congr (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr e₁ e₁').trans (arrow_congr e₂ e₂') := rfl @[simp] lemma arrow_congr_symm {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (arrow_congr e₁ e₂).symm = arrow_congr e₁.symm e₂.symm := rfl /-- A version of `equiv.arrow_congr` in `Type`, rather than `Sort`. The `equiv_rw` tactic is not able to use the default `Sort` level `equiv.arrow_congr`, because Lean's universe rules will not unify `?l_1` with `imax (1 ?m_1)`. -/ @[congr] def arrow_congr' {α₁ β₁ α₂ β₂ : Type*} (hα : α₁ ≃ α₂) (hβ : β₁ ≃ β₂) : (α₁ → β₁) ≃ (α₂ → β₂) := equiv.arrow_congr hα hβ @[simp] lemma arrow_congr'_apply {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) (f : α₁ → β₁) (x : α₂) : arrow_congr' e₁ e₂ f x = (e₂ $ f $ e₁.symm x) := rfl @[simp] lemma arrow_congr'_refl {α β : Type*} : arrow_congr' (equiv.refl α) (equiv.refl β) = equiv.refl (α → β) := rfl @[simp] lemma arrow_congr'_trans {α₁ β₁ α₂ β₂ α₃ β₃ : Type*} (e₁ : α₁ ≃ α₂) (e₁' : β₁ ≃ β₂) (e₂ : α₂ ≃ α₃) (e₂' : β₂ ≃ β₃) : arrow_congr' (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr' e₁ e₁').trans (arrow_congr' e₂ e₂') := rfl @[simp] lemma arrow_congr'_symm {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (arrow_congr' e₁ e₂).symm = arrow_congr' e₁.symm e₂.symm := rfl /-- Conjugate a map `f : α → α` by an equivalence `α ≃ β`. -/ def conj (e : α ≃ β) : (α → α) ≃ (β → β) := arrow_congr e e @[simp] lemma conj_apply (e : α ≃ β) (f : α → α) (x : β) : e.conj f x = (e $ f $ e.symm x) := rfl @[simp] lemma conj_refl : conj (equiv.refl α) = equiv.refl (α → α) := rfl @[simp] lemma conj_symm (e : α ≃ β) : e.conj.symm = e.symm.conj := rfl @[simp] lemma conj_trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : (e₁.trans e₂).conj = e₁.conj.trans e₂.conj := rfl -- This should not be a simp lemma as long as `(∘)` is reducible: -- when `(∘)` is reducible, Lean can unify `f₁ ∘ f₂` with any `g` using -- `f₁ := g` and `f₂ := λ x, x`. This causes nontermination. lemma conj_comp (e : α ≃ β) (f₁ f₂ : α → α) : e.conj (f₁ ∘ f₂) = (e.conj f₁) ∘ (e.conj f₂) := by apply arrow_congr_comp /-- `punit` sorts in any two universes are equivalent. -/ def punit_equiv_punit : punit.{v} ≃ punit.{w} := ⟨λ _, punit.star, λ _, punit.star, λ u, by { cases u, refl }, λ u, by { cases u, reflexivity }⟩ section /-- The sort of maps to `punit.{v}` is equivalent to `punit.{w}`. -/ def arrow_punit_equiv_punit (α : Sort*) : (α → punit.{v}) ≃ punit.{w} := ⟨λ f, punit.star, λ u f, punit.star, λ f, by { funext x, cases f x, refl }, λ u, by { cases u, reflexivity }⟩ /-- The sort of maps from `punit` is equivalent to the codomain. -/ def punit_arrow_equiv (α : Sort*) : (punit.{u} → α) ≃ α := ⟨λ f, f punit.star, λ a u, a, λ f, by { ext ⟨⟩, refl }, λ u, rfl⟩ /-- The sort of maps from `empty` is equivalent to `punit`. -/ def empty_arrow_equiv_punit (α : Sort*) : (empty → α) ≃ punit.{u} := ⟨λ f, punit.star, λ u e, e.rec _, λ f, funext $ λ x, x.rec _, λ u, by { cases u, refl }⟩ /-- The sort of maps from `pempty` is equivalent to `punit`. -/ def pempty_arrow_equiv_punit (α : Sort*) : (pempty → α) ≃ punit.{u} := ⟨λ f, punit.star, λ u e, e.rec _, λ f, funext $ λ x, x.rec _, λ u, by { cases u, refl }⟩ /-- The sort of maps from `false` is equivalent to `punit`. -/ def false_arrow_equiv_punit (α : Sort*) : (false → α) ≃ punit.{u} := calc (false → α) ≃ (empty → α) : arrow_congr false_equiv_empty (equiv.refl _) ... ≃ punit : empty_arrow_equiv_punit _ end /-- Product of two equivalences. If `α₁ ≃ α₂` and `β₁ ≃ β₂`, then `α₁ × β₁ ≃ α₂ × β₂`. -/ @[congr] def prod_congr {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : α₁ × β₁ ≃ α₂ × β₂ := ⟨prod.map e₁ e₂, prod.map e₁.symm e₂.symm, λ ⟨a, b⟩, by simp, λ ⟨a, b⟩, by simp⟩ @[simp] theorem coe_prod_congr {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : ⇑(prod_congr e₁ e₂) = prod.map e₁ e₂ := rfl @[simp] theorem prod_congr_symm {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (prod_congr e₁ e₂).symm = prod_congr e₁.symm e₂.symm := rfl /-- Type product is commutative up to an equivalence: `α × β ≃ β × α`. -/ def prod_comm (α β : Type*) : α × β ≃ β × α := ⟨prod.swap, prod.swap, λ⟨a, b⟩, rfl, λ⟨a, b⟩, rfl⟩ @[simp] lemma coe_prod_comm (α β) : ⇑(prod_comm α β)= prod.swap := rfl @[simp] lemma prod_comm_symm (α β) : (prod_comm α β).symm = prod_comm β α := rfl /-- Type product is associative up to an equivalence. -/ def prod_assoc (α β γ : Sort*) : (α × β) × γ ≃ α × (β × γ) := ⟨λ p, ⟨p.1.1, ⟨p.1.2, p.2⟩⟩, λp, ⟨⟨p.1, p.2.1⟩, p.2.2⟩, λ ⟨⟨a, b⟩, c⟩, rfl, λ ⟨a, ⟨b, c⟩⟩, rfl⟩ @[simp] theorem prod_assoc_apply {α β γ : Sort*} (p : (α × β) × γ) : prod_assoc α β γ p = ⟨p.1.1, ⟨p.1.2, p.2⟩⟩ := rfl @[simp] theorem prod_assoc_sym_apply {α β γ : Sort*} (p : α × (β × γ)) : (prod_assoc α β γ).symm p = ⟨⟨p.1, p.2.1⟩, p.2.2⟩ := rfl section /-- `punit` is a right identity for type product up to an equivalence. -/ def prod_punit (α : Type*) : α × punit.{u+1} ≃ α := ⟨λ p, p.1, λ a, (a, punit.star), λ ⟨_, punit.star⟩, rfl, λ a, rfl⟩ @[simp] theorem prod_punit_apply {α : Sort*} (a : α × punit.{u+1}) : prod_punit α a = a.1 := rfl /-- `punit` is a left identity for type product up to an equivalence. -/ def punit_prod (α : Type*) : punit.{u+1} × α ≃ α := calc punit × α ≃ α × punit : prod_comm _ _ ... ≃ α : prod_punit _ @[simp] theorem punit_prod_apply {α : Type*} (a : punit.{u+1} × α) : punit_prod α a = a.2 := rfl /-- `empty` type is a right absorbing element for type product up to an equivalence. -/ def prod_empty (α : Type*) : α × empty ≃ empty := equiv_empty (λ ⟨_, e⟩, e.rec _) /-- `empty` type is a left absorbing element for type product up to an equivalence. -/ def empty_prod (α : Type*) : empty × α ≃ empty := equiv_empty (λ ⟨e, _⟩, e.rec _) /-- `pempty` type is a right absorbing element for type product up to an equivalence. -/ def prod_pempty (α : Type*) : α × pempty ≃ pempty := equiv_pempty (λ ⟨_, e⟩, e.rec _) /-- `pempty` type is a left absorbing element for type product up to an equivalence. -/ def pempty_prod (α : Type*) : pempty × α ≃ pempty := equiv_pempty (λ ⟨e, _⟩, e.rec _) end section open sum /-- `psum` is equivalent to `sum`. -/ def psum_equiv_sum (α β : Type*) : psum α β ≃ α ⊕ β := ⟨λ s, psum.cases_on s inl inr, λ s, sum.cases_on s psum.inl psum.inr, λ s, by cases s; refl, λ s, by cases s; refl⟩ /-- If `α ≃ α'` and `β ≃ β'`, then `α ⊕ β ≃ α' ⊕ β'`. -/ def sum_congr {α₁ β₁ α₂ β₂ : Type*} (ea : α₁ ≃ α₂) (eb : β₁ ≃ β₂) : α₁ ⊕ β₁ ≃ α₂ ⊕ β₂ := ⟨sum.map ea eb, sum.map ea.symm eb.symm, λ x, by simp, λ x, by simp⟩ @[simp] theorem sum_congr_apply {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) (a : α₁ ⊕ β₁) : sum_congr e₁ e₂ a = a.map e₁ e₂ := rfl @[simp] lemma sum_congr_symm {α β γ δ : Type u} (e : α ≃ β) (f : γ ≃ δ) : (equiv.sum_congr e f).symm = (equiv.sum_congr (e.symm) (f.symm)) := rfl /-- `bool` is equivalent the sum of two `punit`s. -/ def bool_equiv_punit_sum_punit : bool ≃ punit.{u+1} ⊕ punit.{v+1} := ⟨λ b, cond b (inr punit.star) (inl punit.star), λ s, sum.rec_on s (λ_, ff) (λ_, tt), λ b, by cases b; refl, λ s, by rcases s with ⟨⟨⟩⟩ | ⟨⟨⟩⟩; refl⟩ /-- `Prop` is noncomputably equivalent to `bool`. -/ noncomputable def Prop_equiv_bool : Prop ≃ bool := ⟨λ p, @to_bool p (classical.prop_decidable _), λ b, b, λ p, by simp, λ b, by simp⟩ /-- Sum of types is commutative up to an equivalence. -/ def sum_comm (α β : Sort*) : α ⊕ β ≃ β ⊕ α := ⟨sum.swap, sum.swap, sum.swap_swap, sum.swap_swap⟩ @[simp] lemma sum_comm_apply (α β) (a) : sum_comm α β a = a.swap := rfl @[simp] lemma sum_comm_symm (α β) : (sum_comm α β).symm = sum_comm β α := rfl /-- Sum of types is associative up to an equivalence. -/ def sum_assoc (α β γ : Sort*) : (α ⊕ β) ⊕ γ ≃ α ⊕ (β ⊕ γ) := ⟨sum.elim (sum.elim sum.inl (sum.inr ∘ sum.inl)) (sum.inr ∘ sum.inr), sum.elim (sum.inl ∘ sum.inl) $ sum.elim (sum.inl ∘ sum.inr) sum.inr, by rintros (⟨_ | _⟩ | _); refl, by rintros (_ | ⟨_ | _⟩); refl⟩ @[simp] theorem sum_assoc_apply_in1 {α β γ} (a) : sum_assoc α β γ (inl (inl a)) = inl a := rfl @[simp] theorem sum_assoc_apply_in2 {α β γ} (b) : sum_assoc α β γ (inl (inr b)) = inr (inl b) := rfl @[simp] theorem sum_assoc_apply_in3 {α β γ} (c) : sum_assoc α β γ (inr c) = inr (inr c) := rfl /-- Sum with `empty` is equivalent to the original type. -/ def sum_empty (α : Type*) : α ⊕ empty ≃ α := ⟨sum.elim id (empty.rec _), inl, λ s, by { rcases s with _ | ⟨⟨⟩⟩, refl }, λ a, rfl⟩ @[simp] lemma sum_empty_apply_inl {α} (a) : sum_empty α (sum.inl a) = a := rfl /-- The sum of `empty` with any `Sort*` is equivalent to the right summand. -/ def empty_sum (α : Sort*) : empty ⊕ α ≃ α := (sum_comm _ _).trans $ sum_empty _ @[simp] lemma empty_sum_apply_inr {α} (a) : empty_sum α (sum.inr a) = a := rfl /-- Sum with `pempty` is equivalent to the original type. -/ def sum_pempty (α : Type*) : α ⊕ pempty ≃ α := ⟨sum.elim id (pempty.rec _), inl, λ s, by { rcases s with _ | ⟨⟨⟩⟩, refl }, λ a, rfl⟩ @[simp] lemma sum_pempty_apply_inl {α} (a) : sum_pempty α (sum.inl a) = a := rfl /-- The sum of `pempty` with any `Sort*` is equivalent to the right summand. -/ def pempty_sum (α : Sort*) : pempty ⊕ α ≃ α := (sum_comm _ _).trans $ sum_pempty _ @[simp] lemma pempty_sum_apply_inr {α} (a) : pempty_sum α (sum.inr a) = a := rfl /-- `option α` is equivalent to `α ⊕ punit` -/ def option_equiv_sum_punit (α : Sort*) : option α ≃ α ⊕ punit.{u+1} := ⟨λ o, match o with none := inr punit.star | some a := inl a end, λ s, match s with inr _ := none | inl a := some a end, λ o, by cases o; refl, λ s, by rcases s with _ | ⟨⟨⟩⟩; refl⟩ @[simp] lemma option_equiv_sum_punit_none {α} : option_equiv_sum_punit α none = sum.inr () := rfl @[simp] lemma option_equiv_sum_punit_some {α} (a) : option_equiv_sum_punit α (some a) = sum.inl a := rfl /-- The set of `x : option α` such that `is_some x` is equivalent to `α`. -/ def option_is_some_equiv (α : Type*) : {x : option α // x.is_some} ≃ α := { to_fun := λ o, option.get o.2, inv_fun := λ x, ⟨some x, dec_trivial⟩, left_inv := λ o, subtype.eq $ option.some_get _, right_inv := λ x, option.get_some _ _ } /-- `α ⊕ β` is equivalent to a `sigma`-type over `bool`. -/ def sum_equiv_sigma_bool (α β : Sort*) : α ⊕ β ≃ (Σ b: bool, cond b α β) := ⟨λ s, match s with inl a := ⟨tt, a⟩ | inr b := ⟨ff, b⟩ end, λ s, match s with ⟨tt, a⟩ := inl a | ⟨ff, b⟩ := inr b end, λ s, by cases s; refl, λ s, by rcases s with ⟨_|_, _⟩; refl⟩ /-- `sigma_preimage_equiv f` for `f : α → β` is the natural equivalence between the type of all fibres of `f` and the total space `α`. -/ def sigma_preimage_equiv {α β : Type*} (f : α → β) : (Σ y : β, {x // f x = y}) ≃ α := ⟨λ x, x.2.1, λ x, ⟨f x, x, rfl⟩, λ ⟨y, x, rfl⟩, rfl, λ x, rfl⟩ @[simp] lemma sigma_preimage_equiv_apply {α β : Type*} (f : α → β) (x : Σ y : β, {x // f x = y}) : (sigma_preimage_equiv f) x = x.2.1 := rfl @[simp] lemma sigma_preimage_equiv_symm_apply_fst {α β : Type*} (f : α → β) (a : α) : ((sigma_preimage_equiv f).symm a).1 = f a := rfl @[simp] lemma sigma_preimage_equiv_symm_apply_snd_fst {α β : Type*} (f : α → β) (a : α) : ((sigma_preimage_equiv f).symm a).2.1 = a := rfl end section sum_compl /-- For any predicate `p` on `α`, the sum of the two subtypes `{a // p a}` and its complement `{a // ¬ p a}` is naturally equivalent to `α`. -/ def sum_compl {α : Type*} (p : α → Prop) [decidable_pred p] : {a // p a} ⊕ {a // ¬ p a} ≃ α := { to_fun := sum.elim coe coe, inv_fun := λ a, if h : p a then sum.inl ⟨a, h⟩ else sum.inr ⟨a, h⟩, left_inv := by { rintros (⟨x,hx⟩|⟨x,hx⟩); dsimp; [rw dif_pos, rw dif_neg], }, right_inv := λ a, by { dsimp, split_ifs; refl } } @[simp] lemma sum_compl_apply_inl {α : Type*} (p : α → Prop) [decidable_pred p] (x : {a // p a}) : sum_compl p (sum.inl x) = x := rfl @[simp] lemma sum_compl_apply_inr {α : Type*} (p : α → Prop) [decidable_pred p] (x : {a // ¬ p a}) : sum_compl p (sum.inr x) = x := rfl @[simp] lemma sum_compl_apply_symm_of_pos {α : Type*} (p : α → Prop) [decidable_pred p] (a : α) (h : p a) : (sum_compl p).symm a = sum.inl ⟨a, h⟩ := dif_pos h @[simp] lemma sum_compl_apply_symm_of_neg {α : Type*} (p : α → Prop) [decidable_pred p] (a : α) (h : ¬ p a) : (sum_compl p).symm a = sum.inr ⟨a, h⟩ := dif_neg h end sum_compl section subtype_preimage variables (p : α → Prop) [decidable_pred p] (x₀ : {a // p a} → β) /-- For a fixed function `x₀ : {a // p a} → β` defined on a subtype of `α`, the subtype of functions `x : α → β` that agree with `x₀` on the subtype `{a // p a}` is naturally equivalent to the type of functions `{a // ¬ p a} → β`. -/ def subtype_preimage : {x : α → β // x ∘ coe = x₀} ≃ ({a // ¬ p a} → β) := { to_fun := λ (x : {x : α → β // x ∘ coe = x₀}) a, (x : α → β) a, inv_fun := λ x, ⟨λ a, if h : p a then x₀ ⟨a, h⟩ else x ⟨a, h⟩, funext $ λ ⟨a, h⟩, dif_pos h⟩, left_inv := λ ⟨x, hx⟩, subtype.val_injective $ funext $ λ a, (by { dsimp, split_ifs; [ rw ← hx, skip ]; refl }), right_inv := λ x, funext $ λ ⟨a, h⟩, show dite (p a) _ _ = _, by { dsimp, rw [dif_neg h] } } @[simp] lemma subtype_preimage_apply (x : {x : α → β // x ∘ coe = x₀}) : subtype_preimage p x₀ x = λ a, (x : α → β) a := rfl @[simp] lemma subtype_preimage_symm_apply_coe (x : {a // ¬ p a} → β) : ((subtype_preimage p x₀).symm x : α → β) = λ a, if h : p a then x₀ ⟨a, h⟩ else x ⟨a, h⟩ := rfl lemma subtype_preimage_symm_apply_coe_pos (x : {a // ¬ p a} → β) (a : α) (h : p a) : ((subtype_preimage p x₀).symm x : α → β) a = x₀ ⟨a, h⟩ := dif_pos h lemma subtype_preimage_symm_apply_coe_neg (x : {a // ¬ p a} → β) (a : α) (h : ¬ p a) : ((subtype_preimage p x₀).symm x : α → β) a = x ⟨a, h⟩ := dif_neg h end subtype_preimage section fun_unique variables (α β) [unique α] /-- If `α` has a unique term, then the type of function `α → β` is equivalent to `β`. -/ def fun_unique : (α → β) ≃ β := { to_fun := λ f, f (default α), inv_fun := λ b a, b, left_inv := λ f, funext $ λ a, congr_arg f $ subsingleton.elim _ _, right_inv := λ b, rfl } variables {α β} @[simp] lemma fun_unique_apply (f : α → β) : fun_unique α β f = f (default α) := rfl @[simp] lemma fun_unique_symm_apply (b : β) (a : α) : (fun_unique α β).symm b a = b := rfl end fun_unique section /-- A family of equivalences `Π a, β₁ a ≃ β₂ a` generates an equivalence between `Π a, β₁ a` and `Π a, β₂ a`. -/ def Pi_congr_right {α} {β₁ β₂ : α → Sort*} (F : Π a, β₁ a ≃ β₂ a) : (Π a, β₁ a) ≃ (Π a, β₂ a) := ⟨λ H a, F a (H a), λ H a, (F a).symm (H a), λ H, funext $ by simp, λ H, funext $ by simp⟩ /-- Dependent `curry` equivalence: the type of dependent functions on `Σ i, β i` is equivalent to the type of dependent functions of two arguments (i.e., functions to the space of functions). -/ def Pi_curry {α} {β : α → Sort*} (γ : Π a, β a → Sort*) : (Π x : Σ i, β i, γ x.1 x.2) ≃ (Π a b, γ a b) := { to_fun := λ f x y, f ⟨x,y⟩, inv_fun := λ f x, f x.1 x.2, left_inv := λ f, funext $ λ ⟨x,y⟩, rfl, right_inv := λ f, funext $ λ x, funext $ λ y, rfl } end section /-- A `psigma`-type is equivalent to the corresponding `sigma`-type. -/ def psigma_equiv_sigma {α} (β : α → Sort*) : (Σ' i, β i) ≃ Σ i, β i := ⟨λ ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, rfl, λ ⟨a, b⟩, rfl⟩ /-- A family of equivalences `Π a, β₁ a ≃ β₂ a` generates an equivalence between `Σ a, β₁ a` and `Σ a, β₂ a`. -/ def sigma_congr_right {α} {β₁ β₂ : α → Sort*} (F : Π a, β₁ a ≃ β₂ a) : (Σ a, β₁ a) ≃ Σ a, β₂ a := ⟨λ ⟨a, b⟩, ⟨a, F a b⟩, λ ⟨a, b⟩, ⟨a, (F a).symm b⟩, λ ⟨a, b⟩, congr_arg (sigma.mk a) $ symm_apply_apply (F a) b, λ ⟨a, b⟩, congr_arg (sigma.mk a) $ apply_symm_apply (F a) b⟩ /-- An equivalence `f : α₁ ≃ α₂` generates an equivalence between `Σ a, β (f a)` and `Σ a, β a`. -/ def sigma_congr_left {α₁ α₂} {β : α₂ → Sort*} : ∀ f : α₁ ≃ α₂, (Σ a:α₁, β (f a)) ≃ (Σ a:α₂, β a) | ⟨f, g, l, r⟩ := ⟨λ ⟨a, b⟩, ⟨f a, b⟩, λ ⟨a, b⟩, ⟨g a, @@eq.rec β b (r a).symm⟩, λ ⟨a, b⟩, match g (f a), l a : ∀ a' (h : a' = a), @sigma.mk _ (β ∘ f) _ (@@eq.rec β b (congr_arg f h.symm)) = ⟨a, b⟩ with | _, rfl := rfl end, λ ⟨a, b⟩, match f (g a), _ : ∀ a' (h : a' = a), sigma.mk a' (@@eq.rec β b h.symm) = ⟨a, b⟩ with | _, rfl := rfl end⟩ /-- Transporting a sigma type through an equivalence of the base -/ def sigma_congr_left' {α₁ α₂} {β : α₁ → Sort*} (f : α₁ ≃ α₂) : (Σ a:α₁, β a) ≃ (Σ a:α₂, β (f.symm a)) := (sigma_congr_left f.symm).symm /-- Transporting a sigma type through an equivalence of the base and a family of equivalences of matching fibers -/ def sigma_congr {α₁ α₂} {β₁ : α₁ → Sort*} {β₂ : α₂ → Sort*} (f : α₁ ≃ α₂) (F : ∀ a, β₁ a ≃ β₂ (f a)) : sigma β₁ ≃ sigma β₂ := (sigma_congr_right F).trans (sigma_congr_left f) /-- `sigma` type with a constant fiber is equivalent to the product. -/ def sigma_equiv_prod (α β : Sort*) : (Σ_:α, β) ≃ α × β := ⟨λ ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, rfl, λ ⟨a, b⟩, rfl⟩ /-- If each fiber of a `sigma` type is equivalent to a fixed type, then the sigma type is equivalent to the product. -/ def sigma_equiv_prod_of_equiv {α β} {β₁ : α → Sort*} (F : ∀ a, β₁ a ≃ β) : sigma β₁ ≃ α × β := (sigma_congr_right F).trans (sigma_equiv_prod α β) end section /-- The type of functions to a product `α × β` is equivalent to the type of pairs of functions `γ → α` and `γ → β`. -/ def arrow_prod_equiv_prod_arrow (α β γ : Type*) : (γ → α × β) ≃ (γ → α) × (γ → β) := ⟨λ f, (λ c, (f c).1, λ c, (f c).2), λ p c, (p.1 c, p.2 c), λ f, funext $ λ c, prod.mk.eta, λ p, by { cases p, refl }⟩ /-- Functions `α → β → γ` are equivalent to functions on `α × β`. -/ def arrow_arrow_equiv_prod_arrow (α β γ : Sort*) : (α → β → γ) ≃ (α × β → γ) := ⟨uncurry, curry, curry_uncurry, uncurry_curry⟩ open sum /-- The type of functions on a sum type `α ⊕ β` is equivalent to the type of pairs of functions on `α` and on `β`. -/ def sum_arrow_equiv_prod_arrow (α β γ : Type*) : ((α ⊕ β) → γ) ≃ (α → γ) × (β → γ) := ⟨λ f, (f ∘ inl, f ∘ inr), λ p, sum.elim p.1 p.2, λ f, by { ext ⟨⟩; refl }, λ p, by { cases p, refl }⟩ /-- Type product is right distributive with respect to type sum up to an equivalence. -/ def sum_prod_distrib (α β γ : Sort*) : (α ⊕ β) × γ ≃ (α × γ) ⊕ (β × γ) := ⟨λ p, match p with (inl a, c) := inl (a, c) | (inr b, c) := inr (b, c) end, λ s, match s with inl q := (inl q.1, q.2) | inr q := (inr q.1, q.2) end, λ p, by rcases p with ⟨_ | _, _⟩; refl, λ s, by rcases s with ⟨_, _⟩ | ⟨_, _⟩; refl⟩ @[simp] theorem sum_prod_distrib_apply_left {α β γ} (a : α) (c : γ) : sum_prod_distrib α β γ (sum.inl a, c) = sum.inl (a, c) := rfl @[simp] theorem sum_prod_distrib_apply_right {α β γ} (b : β) (c : γ) : sum_prod_distrib α β γ (sum.inr b, c) = sum.inr (b, c) := rfl /-- Type product is left distributive with respect to type sum up to an equivalence. -/ def prod_sum_distrib (α β γ : Sort*) : α × (β ⊕ γ) ≃ (α × β) ⊕ (α × γ) := calc α × (β ⊕ γ) ≃ (β ⊕ γ) × α : prod_comm _ _ ... ≃ (β × α) ⊕ (γ × α) : sum_prod_distrib _ _ _ ... ≃ (α × β) ⊕ (α × γ) : sum_congr (prod_comm _ _) (prod_comm _ _) @[simp] theorem prod_sum_distrib_apply_left {α β γ} (a : α) (b : β) : prod_sum_distrib α β γ (a, sum.inl b) = sum.inl (a, b) := rfl @[simp] theorem prod_sum_distrib_apply_right {α β γ} (a : α) (c : γ) : prod_sum_distrib α β γ (a, sum.inr c) = sum.inr (a, c) := rfl /-- The product of an indexed sum of types (formally, a `sigma`-type `Σ i, α i`) by a type `β` is equivalent to the sum of products `Σ i, (α i × β)`. -/ def sigma_prod_distrib {ι : Type*} (α : ι → Type*) (β : Type*) : ((Σ i, α i) × β) ≃ (Σ i, (α i × β)) := ⟨λ p, ⟨p.1.1, (p.1.2, p.2)⟩, λ p, (⟨p.1, p.2.1⟩, p.2.2), λ p, by { rcases p with ⟨⟨_, _⟩, _⟩, refl }, λ p, by { rcases p with ⟨_, ⟨_, _⟩⟩, refl }⟩ /-- The product `bool × α` is equivalent to `α ⊕ α`. -/ def bool_prod_equiv_sum (α : Type u) : bool × α ≃ α ⊕ α := calc bool × α ≃ (unit ⊕ unit) × α : prod_congr bool_equiv_punit_sum_punit (equiv.refl _) ... ≃ (unit × α) ⊕ (unit × α) : sum_prod_distrib _ _ _ ... ≃ α ⊕ α : sum_congr (punit_prod _) (punit_prod _) end section open sum nat /-- The set of natural numbers is equivalent to `ℕ ⊕ punit`. -/ def nat_equiv_nat_sum_punit : ℕ ≃ ℕ ⊕ punit.{u+1} := ⟨λ n, match n with zero := inr punit.star | succ a := inl a end, λ s, match s with inl n := succ n | inr punit.star := zero end, λ n, begin cases n, repeat { refl } end, λ s, begin cases s with a u, { refl }, {cases u, { refl }} end⟩ /-- `ℕ ⊕ punit` is equivalent to `ℕ`. -/ def nat_sum_punit_equiv_nat : ℕ ⊕ punit.{u+1} ≃ ℕ := nat_equiv_nat_sum_punit.symm /-- The type of integer numbers is equivalent to `ℕ ⊕ ℕ`. -/ def int_equiv_nat_sum_nat : ℤ ≃ ℕ ⊕ ℕ := by refine ⟨_, _, _, _⟩; intro z; {cases z; [left, right]; assumption} <|> {cases z; refl} end /-- An equivalence between `α` and `β` generates an equivalence between `list α` and `list β`. -/ def list_equiv_of_equiv {α β : Type*} (e : α ≃ β) : list α ≃ list β := { to_fun := list.map e, inv_fun := list.map e.symm, left_inv := λ l, by rw [list.map_map, e.symm_comp_self, list.map_id], right_inv := λ l, by rw [list.map_map, e.self_comp_symm, list.map_id] } /-- `fin n` is equivalent to `{m // m < n}`. -/ def fin_equiv_subtype (n : ℕ) : fin n ≃ {m // m < n} := ⟨λ x, ⟨x.1, x.2⟩, λ x, ⟨x.1, x.2⟩, λ ⟨a, b⟩, rfl,λ ⟨a, b⟩, rfl⟩ /-- If `α` is equivalent to `β`, then `unique α` is equivalent to `β`. -/ def unique_congr (e : α ≃ β) : unique α ≃ unique β := { to_fun := λ h, @equiv.unique _ _ h e.symm, inv_fun := λ h, @equiv.unique _ _ h e, left_inv := λ _, subsingleton.elim _ _, right_inv := λ _, subsingleton.elim _ _ } section open subtype /-- If `α` is equivalent to `β` and the predicates `p : α → Prop` and `q : β → Prop` are equivalent at corresponding points, then `{a // p a}` is equivalent to `{b // q b}`. -/ def subtype_congr {p : α → Prop} {q : β → Prop} (e : α ≃ β) (h : ∀ a, p a ↔ q (e a)) : {a : α // p a} ≃ {b : β // q b} := ⟨λ x, ⟨e x.1, (h _).1 x.2⟩, λ y, ⟨e.symm y.1, (h _).2 (by { simp, exact y.2 })⟩, λ ⟨x, h⟩, subtype.ext_val $ by simp, λ ⟨y, h⟩, subtype.ext_val $ by simp⟩ /-- If two predicates `p` and `q` are pointwise equivalent, then `{x // p x}` is equivalent to `{x // q x}`. -/ def subtype_congr_right {p q : α → Prop} (e : ∀x, p x ↔ q x) : {x // p x} ≃ {x // q x} := subtype_congr (equiv.refl _) e @[simp] lemma subtype_congr_right_mk {p q : α → Prop} (e : ∀x, p x ↔ q x) {x : α} (h : p x) : subtype_congr_right e ⟨x, h⟩ = ⟨x, (e x).1 h⟩ := rfl /-- If `α ≃ β`, then for any predicate `p : β → Prop` the subtype `{a // p (e a)}` is equivalent to the subtype `{b // p b}`. -/ def subtype_equiv_of_subtype {p : β → Prop} (e : α ≃ β) : {a : α // p (e a)} ≃ {b : β // p b} := subtype_congr e $ by simp /-- If `α ≃ β`, then for any predicate `p : α → Prop` the subtype `{a // p a}` is equivalent to the subtype `{b // p (e.symm b)}`. This version is used by `equiv_rw`. -/ def subtype_equiv_of_subtype' {p : α → Prop} (e : α ≃ β) : {a : α // p a} ≃ {b : β // p (e.symm b)} := e.symm.subtype_equiv_of_subtype.symm /-- If two predicates are equal, then the corresponding subtypes are equivalent. -/ def subtype_congr_prop {α : Type*} {p q : α → Prop} (h : p = q) : subtype p ≃ subtype q := subtype_congr (equiv.refl α) (assume a, h ▸ iff.rfl) /-- The subtypes corresponding to equal sets are equivalent. -/ def set_congr {α : Type*} {s t : set α} (h : s = t) : s ≃ t := subtype_congr_prop h /-- A subtype of a subtype is equivalent to the subtype of elements satisfying both predicates. This version allows the “inner” predicate to depend on `h : p a`. -/ def subtype_subtype_equiv_subtype_exists {α : Type u} (p : α → Prop) (q : subtype p → Prop) : subtype q ≃ {a : α // ∃h:p a, q ⟨a, h⟩ } := ⟨λ⟨⟨a, ha⟩, ha'⟩, ⟨a, ha, ha'⟩, λ⟨a, ha⟩, ⟨⟨a, ha.cases_on $ assume h _, h⟩, by { cases ha, exact ha_h }⟩, assume ⟨⟨a, ha⟩, h⟩, rfl, assume ⟨a, h₁, h₂⟩, rfl⟩ /-- A subtype of a subtype is equivalent to the subtype of elements satisfying both predicates. -/ def subtype_subtype_equiv_subtype_inter {α : Type u} (p q : α → Prop) : {x : subtype p // q x.1} ≃ subtype (λ x, p x ∧ q x) := (subtype_subtype_equiv_subtype_exists p _).trans $ subtype_congr_right $ λ x, exists_prop /-- If the outer subtype has more restrictive predicate than the inner one, then we can drop the latter. -/ def subtype_subtype_equiv_subtype {α : Type u} {p q : α → Prop} (h : ∀ {x}, q x → p x) : {x : subtype p // q x.1} ≃ subtype q := (subtype_subtype_equiv_subtype_inter p _).trans $ subtype_congr_right $ assume x, ⟨and.right, λ h₁, ⟨h h₁, h₁⟩⟩ /-- If a proposition holds for all elements, then the subtype is equivalent to the original type. -/ def subtype_univ_equiv {α : Type u} {p : α → Prop} (h : ∀ x, p x) : subtype p ≃ α := ⟨λ x, x, λ x, ⟨x, h x⟩, λ x, subtype.eq rfl, λ x, rfl⟩ /-- A subtype of a sigma-type is a sigma-type over a subtype. -/ def subtype_sigma_equiv {α : Type u} (p : α → Type v) (q : α → Prop) : { y : sigma p // q y.1 } ≃ Σ(x : subtype q), p x.1 := ⟨λ x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩, λ x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩, λ ⟨⟨x, h⟩, y⟩, rfl, λ ⟨⟨x, y⟩, h⟩, rfl⟩ /-- A sigma type over a subtype is equivalent to the sigma set over the original type, if the fiber is empty outside of the subset -/ def sigma_subtype_equiv_of_subset {α : Type u} (p : α → Type v) (q : α → Prop) (h : ∀ x, p x → q x) : (Σ x : subtype q, p x) ≃ Σ x : α, p x := (subtype_sigma_equiv p q).symm.trans $ subtype_univ_equiv $ λ x, h x.1 x.2 /-- If a predicate `p : β → Prop` is true on the range of a map `f : α → β`, then `Σ y : {y // p y}, {x // f x = y}` is equivalent to `α`. -/ def sigma_subtype_preimage_equiv {α : Type u} {β : Type v} (f : α → β) (p : β → Prop) (h : ∀ x, p (f x)) : (Σ y : subtype p, {x : α // f x = y}) ≃ α := calc _ ≃ Σ y : β, {x : α // f x = y} : sigma_subtype_equiv_of_subset _ p (λ y ⟨x, h'⟩, h' ▸ h x) ... ≃ α : sigma_preimage_equiv f /-- If for each `x` we have `p x ↔ q (f x)`, then `Σ y : {y // q y}, f ⁻¹' {y}` is equivalent to `{x // p x}`. -/ def sigma_subtype_preimage_equiv_subtype {α : Type u} {β : Type v} (f : α → β) {p : α → Prop} {q : β → Prop} (h : ∀ x, p x ↔ q (f x)) : (Σ y : subtype q, {x : α // f x = y}) ≃ subtype p := calc (Σ y : subtype q, {x : α // f x = y}) ≃ Σ y : subtype q, {x : subtype p // subtype.mk (f x) ((h x).1 x.2) = y} : begin apply sigma_congr_right, assume y, symmetry, refine (subtype_subtype_equiv_subtype_exists _ _).trans (subtype_congr_right _), assume x, exact ⟨λ ⟨hp, h'⟩, congr_arg subtype.val h', λ h', ⟨(h x).2 (h'.symm ▸ y.2), subtype.eq h'⟩⟩ end ... ≃ subtype p : sigma_preimage_equiv (λ x : subtype p, (⟨f x, (h x).1 x.property⟩ : subtype q)) /-- The `pi`-type `Π i, π i` is equivalent to the type of sections `f : ι → Σ i, π i` of the `sigma` type such that for all `i` we have `(f i).fst = i`. -/ def pi_equiv_subtype_sigma (ι : Type*) (π : ι → Type*) : (Πi, π i) ≃ {f : ι → Σi, π i | ∀i, (f i).1 = i } := ⟨ λf, ⟨λi, ⟨i, f i⟩, assume i, rfl⟩, λf i, begin rw ← f.2 i, exact (f.1 i).2 end, assume f, funext $ assume i, rfl, assume ⟨f, hf⟩, subtype.eq $ funext $ assume i, sigma.eq (hf i).symm $ eq_of_heq $ rec_heq_of_heq _ $ rec_heq_of_heq _ $ heq.refl _⟩ /-- The set of functions `f : Π a, β a` such that for all `a` we have `p a (f a)` is equivalent to the set of functions `Π a, {b : β a // p a b}`. -/ def subtype_pi_equiv_pi {α : Sort u} {β : α → Sort v} {p : Πa, β a → Prop} : {f : Πa, β a // ∀a, p a (f a) } ≃ Πa, { b : β a // p a b } := ⟨λf a, ⟨f.1 a, f.2 a⟩, λf, ⟨λa, (f a).1, λa, (f a).2⟩, by { rintro ⟨f, h⟩, refl }, by { rintro f, funext a, exact subtype.ext_val rfl }⟩ /-- A subtype of a product defined by componentwise conditions is equivalent to a product of subtypes. -/ def subtype_prod_equiv_prod {α : Type u} {β : Type v} {p : α → Prop} {q : β → Prop} : {c : α × β // p c.1 ∧ q c.2} ≃ ({a // p a} × {b // q b}) := ⟨λ x, ⟨⟨x.1.1, x.2.1⟩, ⟨x.1.2, x.2.2⟩⟩, λ x, ⟨⟨x.1.1, x.2.1⟩, ⟨x.1.2, x.2.2⟩⟩, λ ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl, λ ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl⟩ end section subtype_equiv_codomain variables {X : Type*} {Y : Type*} [decidable_eq X] {x : X} /-- The type of all functions `X → Y` with prescribed values for all `x' ≠ x` is equivalent to the codomain `Y`. -/ def subtype_equiv_codomain (f : {x' // x' ≠ x} → Y) : {g : X → Y // g ∘ coe = f} ≃ Y := (subtype_preimage _ f).trans $ @fun_unique {x' // ¬ x' ≠ x} _ $ show unique {x' // ¬ x' ≠ x}, from @equiv.unique _ _ (show unique {x' // x' = x}, from { default := ⟨x, rfl⟩, uniq := λ ⟨x', h⟩, subtype.val_injective h }) (subtype_congr_right $ λ a, not_not) @[simp] lemma coe_subtype_equiv_codomain (f : {x' // x' ≠ x} → Y) : (subtype_equiv_codomain f : {g : X → Y // g ∘ coe = f} → Y) = λ g, (g : X → Y) x := rfl @[simp] lemma subtype_equiv_codomain_apply (f : {x' // x' ≠ x} → Y) (g : {g : X → Y // g ∘ coe = f}) : subtype_equiv_codomain f g = (g : X → Y) x := rfl lemma coe_subtype_equiv_codomain_symm (f : {x' // x' ≠ x} → Y) : ((subtype_equiv_codomain f).symm : Y → {g : X → Y // g ∘ coe = f}) = λ y, ⟨λ x', if h : x' ≠ x then f ⟨x', h⟩ else y, by { funext x', dsimp, erw [dif_pos x'.2, subtype.coe_eta] }⟩ := rfl @[simp] lemma subtype_equiv_codomain_symm_apply (f : {x' // x' ≠ x} → Y) (y : Y) (x' : X) : ((subtype_equiv_codomain f).symm y : X → Y) x' = if h : x' ≠ x then f ⟨x', h⟩ else y := rfl @[simp] lemma subtype_equiv_codomain_symm_apply_eq (f : {x' // x' ≠ x} → Y) (y : Y) : ((subtype_equiv_codomain f).symm y : X → Y) x = y := dif_neg (not_not.mpr rfl) lemma subtype_equiv_codomain_symm_apply_ne (f : {x' // x' ≠ x} → Y) (y : Y) (x' : X) (h : x' ≠ x) : ((subtype_equiv_codomain f).symm y : X → Y) x' = f ⟨x', h⟩ := dif_pos h end subtype_equiv_codomain namespace set open set /-- `univ α` is equivalent to `α`. -/ protected def univ (α) : @univ α ≃ α := ⟨subtype.val, λ a, ⟨a, trivial⟩, λ ⟨a, _⟩, rfl, λ a, rfl⟩ @[simp] lemma univ_apply {α : Type u} (x : @univ α) : equiv.set.univ α x = x := rfl @[simp] lemma univ_symm_apply {α : Type u} (x : α) : (equiv.set.univ α).symm x = ⟨x, trivial⟩ := rfl /-- An empty set is equivalent to the `empty` type. -/ protected def empty (α) : (∅ : set α) ≃ empty := equiv_empty $ λ ⟨x, h⟩, not_mem_empty x h /-- An empty set is equivalent to a `pempty` type. -/ protected def pempty (α) : (∅ : set α) ≃ pempty := equiv_pempty $ λ ⟨x, h⟩, not_mem_empty x h /-- If sets `s` and `t` are separated by a decidable predicate, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union' {α} {s t : set α} (p : α → Prop) [decidable_pred p] (hs : ∀ x ∈ s, p x) (ht : ∀ x ∈ t, ¬ p x) : (s ∪ t : set α) ≃ s ⊕ t := { to_fun := λ x, if hp : p x then sum.inl ⟨_, x.2.resolve_right (λ xt, ht _ xt hp)⟩ else sum.inr ⟨_, x.2.resolve_left (λ xs, hp (hs _ xs))⟩, inv_fun := λ o, match o with | (sum.inl x) := ⟨x, or.inl x.2⟩ | (sum.inr x) := ⟨x, or.inr x.2⟩ end, left_inv := λ ⟨x, h'⟩, by by_cases p x; simp [union'._match_1, h]; congr, right_inv := λ o, begin rcases o with ⟨x, h⟩ | ⟨x, h⟩; dsimp [union'._match_1]; [simp [hs _ h], simp [ht _ h]] end } /-- If sets `s` and `t` are disjoint, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) : (s ∪ t : set α) ≃ s ⊕ t := set.union' (λ x, x ∈ s) (λ _, id) (λ x xt xs, H ⟨xs, xt⟩) lemma union_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) {a : (s ∪ t : set α)} (ha : ↑a ∈ s) : equiv.set.union H a = sum.inl ⟨a, ha⟩ := dif_pos ha lemma union_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) {a : (s ∪ t : set α)} (ha : ↑a ∈ t) : equiv.set.union H a = sum.inr ⟨a, ha⟩ := dif_neg $ λ h, H ⟨h, ha⟩ -- TODO: Any reason to use the same universe? /-- A singleton set is equivalent to a `punit` type. -/ protected def singleton {α} (a : α) : ({a} : set α) ≃ punit.{u} := ⟨λ _, punit.star, λ _, ⟨a, mem_singleton _⟩, λ ⟨x, h⟩, by { simp at h, subst x }, λ ⟨⟩, rfl⟩ /-- Equal sets are equivalent. -/ protected def of_eq {α : Type u} {s t : set α} (h : s = t) : s ≃ t := { to_fun := λ x, ⟨x.1, h ▸ x.2⟩, inv_fun := λ x, ⟨x.1, h.symm ▸ x.2⟩, left_inv := λ _, subtype.eq rfl, right_inv := λ _, subtype.eq rfl } @[simp] lemma of_eq_apply {α : Type u} {s t : set α} (h : s = t) (a : s) : equiv.set.of_eq h a = ⟨a, h ▸ a.2⟩ := rfl @[simp] lemma of_eq_symm_apply {α : Type u} {s t : set α} (h : s = t) (a : t) : (equiv.set.of_eq h).symm a = ⟨a, h.symm ▸ a.2⟩ := rfl /-- If `a ∉ s`, then `insert a s` is equivalent to `s ⊕ punit`. -/ protected def insert {α} {s : set.{u} α} [decidable_pred s] {a : α} (H : a ∉ s) : (insert a s : set α) ≃ s ⊕ punit.{u+1} := calc (insert a s : set α) ≃ ↥(s ∪ {a}) : equiv.set.of_eq (by simp) ... ≃ s ⊕ ({a} : set α) : equiv.set.union (by finish [set.subset_def]) ... ≃ s ⊕ punit.{u+1} : sum_congr (equiv.refl _) (equiv.set.singleton _) /-- If `s : set α` is a set with decidable membership, then `s ⊕ sᶜ` is equivalent to `α`. -/ protected def sum_compl {α} (s : set α) [decidable_pred s] : s ⊕ (sᶜ : set α) ≃ α := calc s ⊕ (sᶜ : set α) ≃ ↥(s ∪ sᶜ) : (equiv.set.union (by simp [set.ext_iff])).symm ... ≃ @univ α : equiv.set.of_eq (by simp) ... ≃ α : equiv.set.univ _ @[simp] lemma sum_compl_apply_inl {α : Type u} (s : set α) [decidable_pred s] (x : s) : equiv.set.sum_compl s (sum.inl x) = x := rfl @[simp] lemma sum_compl_apply_inr {α : Type u} (s : set α) [decidable_pred s] (x : sᶜ) : equiv.set.sum_compl s (sum.inr x) = x := rfl lemma sum_compl_symm_apply_of_mem {α : Type u} {s : set α} [decidable_pred s] {x : α} (hx : x ∈ s) : (equiv.set.sum_compl s).symm x = sum.inl ⟨x, hx⟩ := have ↑(⟨x, or.inl hx⟩ : (s ∪ sᶜ : set α)) ∈ s, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_left _ this } lemma sum_compl_symm_apply_of_not_mem {α : Type u} {s : set α} [decidable_pred s] {x : α} (hx : x ∉ s) : (equiv.set.sum_compl s).symm x = sum.inr ⟨x, hx⟩ := have ↑(⟨x, or.inr hx⟩ : (s ∪ sᶜ : set α)) ∈ sᶜ, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_right _ this } /-- `sum_diff_subset s t` is the natural equivalence between `s ⊕ (t \ s)` and `t`, where `s` and `t` are two sets. -/ protected def sum_diff_subset {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] : s ⊕ (t \ s : set α) ≃ t := calc s ⊕ (t \ s : set α) ≃ (s ∪ (t \ s) : set α) : (equiv.set.union (by simp [inter_diff_self])).symm ... ≃ t : equiv.set.of_eq (by { simp [union_diff_self, union_eq_self_of_subset_left h] }) @[simp] lemma sum_diff_subset_apply_inl {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] (x : s) : equiv.set.sum_diff_subset h (sum.inl x) = inclusion h x := rfl @[simp] lemma sum_diff_subset_apply_inr {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] (x : t \ s) : equiv.set.sum_diff_subset h (sum.inr x) = inclusion (diff_subset t s) x := rfl lemma sum_diff_subset_symm_apply_of_mem {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] {x : t} (hx : x.1 ∈ s) : (equiv.set.sum_diff_subset h).symm x = sum.inl ⟨x, hx⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inl], exact subtype.eq rfl, end lemma sum_diff_subset_symm_apply_of_not_mem {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] {x : t} (hx : x.1 ∉ s) : (equiv.set.sum_diff_subset h).symm x = sum.inr ⟨x, ⟨x.2, hx⟩⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inr], exact subtype.eq rfl, end /-- If `s` is a set with decidable membership, then the sum of `s ∪ t` and `s ∩ t` is equivalent to `s ⊕ t`. -/ protected def union_sum_inter {α : Type u} (s t : set α) [decidable_pred s] : (s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ s ⊕ t := calc (s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ (s ∪ t \ s : set α) ⊕ (s ∩ t : set α) : by rw [union_diff_self] ... ≃ (s ⊕ (t \ s : set α)) ⊕ (s ∩ t : set α) : sum_congr (set.union $ subset_empty_iff.2 (inter_diff_self _ _)) (equiv.refl _) ... ≃ s ⊕ (t \ s : set α) ⊕ (s ∩ t : set α) : sum_assoc _ _ _ ... ≃ s ⊕ (t \ s ∪ s ∩ t : set α) : sum_congr (equiv.refl _) begin refine (set.union' (∉ s) _ _).symm, exacts [λ x hx, hx.2, λ x hx, not_not_intro hx.1] end ... ≃ s ⊕ t : by { rw (_ : t \ s ∪ s ∩ t = t), rw [union_comm, inter_comm, inter_union_diff] } /-- The set product of two sets is equivalent to the type product of their coercions to types. -/ protected def prod {α β} (s : set α) (t : set β) : s.prod t ≃ s × t := @subtype_prod_equiv_prod α β s t /-- If a function `f` is injective on a set `s`, then `s` is equivalent to `f '' s`. -/ protected noncomputable def image_of_inj_on {α β} (f : α → β) (s : set α) (H : inj_on f s) : s ≃ (f '' s) := ⟨λ p, ⟨f p, mem_image_of_mem f p.2⟩, λ p, ⟨classical.some p.2, (classical.some_spec p.2).1⟩, λ ⟨x, h⟩, subtype.eq (H (classical.some_spec (mem_image_of_mem f h)).1 h (classical.some_spec (mem_image_of_mem f h)).2), λ ⟨y, h⟩, subtype.eq (classical.some_spec h).2⟩ /-- If `f` is an injective function, then `s` is equivalent to `f '' s`. -/ protected noncomputable def image {α β} (f : α → β) (s : set α) (H : injective f) : s ≃ (f '' s) := equiv.set.image_of_inj_on f s (λ x y hx hy hxy, H hxy) @[simp] theorem image_apply {α β} (f : α → β) (s : set α) (H : injective f) (a h) : set.image f s H ⟨a, h⟩ = ⟨f a, mem_image_of_mem _ h⟩ := rfl /-- If `f : α → β` is an injective function, then `α` is equivalent to the range of `f`. -/ protected noncomputable def range {α β} (f : α → β) (H : injective f) : α ≃ range f := { to_fun := λ x, ⟨f x, mem_range_self _⟩, inv_fun := λ x, classical.some x.2, left_inv := λ x, H (classical.some_spec (show f x ∈ range f, from mem_range_self _)), right_inv := λ x, subtype.eq $ classical.some_spec x.2 } @[simp] theorem range_apply {α β} (f : α → β) (H : injective f) (a) : set.range f H a = ⟨f a, set.mem_range_self _⟩ := rfl theorem apply_range_symm {α β} (f : α → β) (H : injective f) (b : range f) : f ((set.range f H).symm b) = b := begin conv_rhs { rw ←((set.range f H).right_inv b), }, simp, end /-- If `α` is equivalent to `β`, then `set α` is equivalent to `set β`. -/ protected def congr {α β : Type*} (e : α ≃ β) : set α ≃ set β := ⟨λ s, e '' s, λ t, e.symm '' t, symm_image_image e, symm_image_image e.symm⟩ /-- The set `{x ∈ s | t x}` is equivalent to the set of `x : s` such that `t x`. -/ protected def sep {α : Type u} (s : set α) (t : α → Prop) : ({ x ∈ s | t x } : set α) ≃ { x : s | t x } := (equiv.subtype_subtype_equiv_subtype_inter s t).symm end set /-- If `f` is a bijective function, then its domain is equivalent to its codomain. -/ noncomputable def of_bijective {α β} (f : α → β) (hf : bijective f) : α ≃ β := (equiv.set.range f hf.1).trans $ (set_congr hf.2.range_eq).trans $ equiv.set.univ β @[simp] theorem coe_of_bijective {α β} {f : α → β} (hf : bijective f) : (of_bijective f hf : α → β) = f := rfl /-- If `f` is an injective function, then its domain is equivalent to its range. -/ noncomputable def of_injective {α β} (f : α → β) (hf : injective f) : α ≃ _root_.set.range f := of_bijective (λ x, ⟨f x, set.mem_range_self x⟩) ⟨λ x y hxy, hf $ by injections, λ ⟨_, x, rfl⟩, ⟨x, rfl⟩⟩ @[simp] lemma of_injective_apply {α β} (f : α → β) (hf : injective f) (x : α) : of_injective f hf x = ⟨f x, set.mem_range_self x⟩ := rfl def subtype_quotient_equiv_quotient_subtype (p₁ : α → Prop) [s₁ : setoid α] [s₂ : setoid (subtype p₁)] (p₂ : quotient s₁ → Prop) (hp₂ : ∀ a, p₁ a ↔ p₂ ⟦a⟧) (h : ∀ x y : subtype p₁, @setoid.r _ s₂ x y ↔ (x : α) ≈ y) : {x // p₂ x} ≃ quotient s₂ := { to_fun := λ a, quotient.hrec_on a.1 (λ a h, ⟦⟨a, (hp₂ _).2 h⟩⟧) (λ a b hab, hfunext (by rw quotient.sound hab) (λ h₁ h₂ _, heq_of_eq (quotient.sound ((h _ _).2 hab)))) a.2, inv_fun := λ a, quotient.lift_on a (λ a, (⟨⟦a.1⟧, (hp₂ _).1 a.2⟩ : {x // p₂ x})) (λ a b hab, subtype.ext_val (quotient.sound ((h _ _).1 hab))), left_inv := λ ⟨a, ha⟩, quotient.induction_on a (λ a ha, rfl) ha, right_inv := λ a, quotient.induction_on a (λ ⟨a, ha⟩, rfl) } section swap variable [decidable_eq α] open decidable /-- A helper function for `equiv.swap`. -/ def swap_core (a b r : α) : α := if r = a then b else if r = b then a else r theorem swap_core_self (r a : α) : swap_core a a r = r := by { unfold swap_core, split_ifs; cc } theorem swap_core_swap_core (r a b : α) : swap_core a b (swap_core a b r) = r := by { unfold swap_core, split_ifs; cc } theorem swap_core_comm (r a b : α) : swap_core a b r = swap_core b a r := by { unfold swap_core, split_ifs; cc } /-- `swap a b` is the permutation that swaps `a` and `b` and leaves other values as is. -/ def swap (a b : α) : perm α := ⟨swap_core a b, swap_core a b, λr, swap_core_swap_core r a b, λr, swap_core_swap_core r a b⟩ theorem swap_self (a : α) : swap a a = equiv.refl _ := ext $ λ r, swap_core_self r a theorem swap_comm (a b : α) : swap a b = swap b a := ext $ λ r, swap_core_comm r _ _ theorem swap_apply_def (a b x : α) : swap a b x = if x = a then b else if x = b then a else x := rfl @[simp] theorem swap_apply_left (a b : α) : swap a b a = b := if_pos rfl @[simp] theorem swap_apply_right (a b : α) : swap a b b = a := by { by_cases h : b = a; simp [swap_apply_def, h], } theorem swap_apply_of_ne_of_ne {a b x : α} : x ≠ a → x ≠ b → swap a b x = x := by simp [swap_apply_def] {contextual := tt} @[simp] theorem swap_swap (a b : α) : (swap a b).trans (swap a b) = equiv.refl _ := ext $ λ x, swap_core_swap_core _ _ _ theorem swap_comp_apply {a b x : α} (π : perm α) : π.trans (swap a b) x = if π x = a then b else if π x = b then a else π x := by { cases π, refl } @[simp] lemma swap_inv {α : Type*} [decidable_eq α] (x y : α) : (swap x y)⁻¹ = swap x y := rfl @[simp] lemma symm_trans_swap_trans [decidable_eq β] (a b : α) (e : α ≃ β) : (e.symm.trans (swap a b)).trans e = swap (e a) (e b) := equiv.ext (λ x, begin have : ∀ a, e.symm x = a ↔ x = e a := λ a, by { rw @eq_comm _ (e.symm x), split; intros; simp * at * }, simp [swap_apply_def, this], split_ifs; simp end) @[simp] lemma swap_mul_self {α : Type*} [decidable_eq α] (i j : α) : swap i j * swap i j = 1 := equiv.swap_swap i j @[simp] lemma swap_apply_self {α : Type*} [decidable_eq α] (i j a : α) : swap i j (swap i j a) = a := by rw [← perm.mul_apply, swap_mul_self, perm.one_apply] /-- Augment an equivalence with a prescribed mapping `f a = b` -/ def set_value (f : α ≃ β) (a : α) (b : β) : α ≃ β := (swap a (f.symm b)).trans f @[simp] theorem set_value_eq (f : α ≃ β) (a : α) (b : β) : set_value f a b a = b := by { dsimp [set_value], simp [swap_apply_left] } end swap protected lemma forall_congr {p : α → Prop} {q : β → Prop} (f : α ≃ β) (h : ∀{x}, p x ↔ q (f x)) : (∀x, p x) ↔ (∀y, q y) := begin split; intros h₂ x, { rw [←f.right_inv x], apply h.mp, apply h₂ }, apply h.mpr, apply h₂ end protected lemma forall_congr' {p : α → Prop} {q : β → Prop} (f : α ≃ β) (h : ∀{x}, p (f.symm x) ↔ q x) : (∀x, p x) ↔ (∀y, q y) := (equiv.forall_congr f.symm (λ x, h.symm)).symm -- We next build some higher arity versions of `equiv.forall_congr`. -- Although they appear to just be repeated applications of `equiv.forall_congr`, -- unification of metavariables works better with these versions. -- In particular, they are necessary in `equiv_rw`. -- (Stopping at ternary functions seems reasonable: at least in 1-categorical mathematics, -- it's rare to have axioms involving more than 3 elements at once.) universes ua1 ua2 ub1 ub2 ug1 ug2 variables {α₁ : Sort ua1} {α₂ : Sort ua2} {β₁ : Sort ub1} {β₂ : Sort ub2} {γ₁ : Sort ug1} {γ₂ : Sort ug2} protected lemma forall₂_congr {p : α₁ → β₁ → Prop} {q : α₂ → β₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (h : ∀{x y}, p x y ↔ q (eα x) (eβ y)) : (∀x y, p x y) ↔ (∀x y, q x y) := begin apply equiv.forall_congr, intros, apply equiv.forall_congr, intros, apply h, end protected lemma forall₂_congr' {p : α₁ → β₁ → Prop} {q : α₂ → β₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (h : ∀{x y}, p (eα.symm x) (eβ.symm y) ↔ q x y) : (∀x y, p x y) ↔ (∀x y, q x y) := (equiv.forall₂_congr eα.symm eβ.symm (λ x y, h.symm)).symm protected lemma forall₃_congr {p : α₁ → β₁ → γ₁ → Prop} {q : α₂ → β₂ → γ₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (eγ : γ₁ ≃ γ₂) (h : ∀{x y z}, p x y z ↔ q (eα x) (eβ y) (eγ z)) : (∀x y z, p x y z) ↔ (∀x y z, q x y z) := begin apply equiv.forall₂_congr, intros, apply equiv.forall_congr, intros, apply h, end protected lemma forall₃_congr' {p : α₁ → β₁ → γ₁ → Prop} {q : α₂ → β₂ → γ₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (eγ : γ₁ ≃ γ₂) (h : ∀{x y z}, p (eα.symm x) (eβ.symm y) (eγ.symm z) ↔ q x y z) : (∀x y z, p x y z) ↔ (∀x y z, q x y z) := (equiv.forall₃_congr eα.symm eβ.symm eγ.symm (λ x y z, h.symm)).symm protected lemma forall_congr_left' {p : α → Prop} (f : α ≃ β) : (∀x, p x) ↔ (∀y, p (f.symm y)) := equiv.forall_congr f (λx, by simp) protected lemma forall_congr_left {p : β → Prop} (f : α ≃ β) : (∀x, p (f x)) ↔ (∀y, p y) := (equiv.forall_congr_left' f.symm).symm section variables (P : α → Sort w) (e : α ≃ β) /-- Transport dependent functions through an equivalence of the base space. -/ def Pi_congr_left' : (Π a, P a) ≃ (Π b, P (e.symm b)) := { to_fun := λ f x, f (e.symm x), inv_fun := λ f x, begin rw [← e.symm_apply_apply x], exact f (e x) end, left_inv := λ f, funext $ λ x, eq_of_heq ((eq_rec_heq _ _).trans (by { dsimp, rw e.symm_apply_apply })), right_inv := λ f, funext $ λ x, eq_of_heq ((eq_rec_heq _ _).trans (by { rw e.apply_symm_apply })) } @[simp] lemma Pi_congr_left'_apply (f : Π a, P a) (b : β) : ((Pi_congr_left' P e) f) b = f (e.symm b) := rfl @[simp] lemma Pi_congr_left'_symm_apply (g : Π b, P (e.symm b)) (a : α) : ((Pi_congr_left' P e).symm g) a = (by { convert g (e a), simp }) := rfl end section variables (P : β → Sort w) (e : α ≃ β) /-- Transporting dependent functions through an equivalence of the base, expressed as a "simplification". -/ def Pi_congr_left : (Π a, P (e a)) ≃ (Π b, P b) := (Pi_congr_left' P e.symm).symm end section variables {W : α → Sort w} {Z : β → Sort z} (h₁ : α ≃ β) (h₂ : Π a : α, (W a ≃ Z (h₁ a))) /-- Transport dependent functions through an equivalence of the base spaces and a family of equivalences of the matching fibers. -/ def Pi_congr : (Π a, W a) ≃ (Π b, Z b) := (equiv.Pi_congr_right h₂).trans (equiv.Pi_congr_left _ h₁) end section variables {W : α → Sort w} {Z : β → Sort z} (h₁ : α ≃ β) (h₂ : Π b : β, (W (h₁.symm b) ≃ Z b)) /-- Transport dependent functions through an equivalence of the base spaces and a family of equivalences of the matching fibres. -/ def Pi_congr' : (Π a, W a) ≃ (Π b, Z b) := (Pi_congr h₁.symm (λ b, (h₂ b).symm)).symm end end equiv instance {α} [subsingleton α] : subsingleton (ulift α) := equiv.ulift.subsingleton instance {α} [subsingleton α] : subsingleton (plift α) := equiv.plift.subsingleton instance {α} [decidable_eq α] : decidable_eq (ulift α) := equiv.ulift.decidable_eq instance {α} [decidable_eq α] : decidable_eq (plift α) := equiv.plift.decidable_eq /-- If both `α` and `β` are singletons, then `α ≃ β`. -/ def equiv_of_unique_of_unique [unique α] [unique β] : α ≃ β := { to_fun := λ _, default β, inv_fun := λ _, default α, left_inv := λ _, subsingleton.elim _ _, right_inv := λ _, subsingleton.elim _ _ } /-- If `α` is a singleton, then it is equivalent to any `punit`. -/ def equiv_punit_of_unique [unique α] : α ≃ punit.{v} := equiv_of_unique_of_unique /-- If `α` is a subsingleton, then it is equivalent to `α × α`. -/ def subsingleton_prod_self_equiv {α : Type*} [subsingleton α] : α × α ≃ α := { to_fun := λ p, p.1, inv_fun := λ a, (a, a), left_inv := λ p, subsingleton.elim _ _, right_inv := λ p, subsingleton.elim _ _, } /-- To give an equivalence between two subsingleton types, it is sufficient to give any two functions between them. -/ def equiv_of_subsingleton_of_subsingleton [subsingleton α] [subsingleton β] (f : α → β) (g : β → α) : α ≃ β := { to_fun := f, inv_fun := g, left_inv := λ _, subsingleton.elim _ _, right_inv := λ _, subsingleton.elim _ _ } /-- `unique (unique α)` is equivalent to `unique α`. -/ def unique_unique_equiv : unique (unique α) ≃ unique α := equiv_of_subsingleton_of_subsingleton (λ h, h.default) (λ h, { default := h, uniq := λ _, subsingleton.elim _ _ }) namespace quot /-- An equivalence `e : α ≃ β` generates an equivalence between quotient spaces, if `ra a₁ a₂ ↔ rb (e a₁) (e a₂). -/ protected def congr {ra : α → α → Prop} {rb : β → β → Prop} (e : α ≃ β) (eq : ∀a₁ a₂, ra a₁ a₂ ↔ rb (e a₁) (e a₂)) : quot ra ≃ quot rb := { to_fun := quot.map e (assume a₁ a₂, (eq a₁ a₂).1), inv_fun := quot.map e.symm (assume b₁ b₂ h, (eq (e.symm b₁) (e.symm b₂)).2 ((e.apply_symm_apply b₁).symm ▸ (e.apply_symm_apply b₂).symm ▸ h)), left_inv := by { rintros ⟨a⟩, dunfold quot.map, simp only [equiv.symm_apply_apply] }, right_inv := by { rintros ⟨a⟩, dunfold quot.map, simp only [equiv.apply_symm_apply] } } /-- Quotients are congruent on equivalences under equality of their relation. An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/ protected def congr_right {r r' : α → α → Prop} (eq : ∀a₁ a₂, r a₁ a₂ ↔ r' a₁ a₂) : quot r ≃ quot r' := quot.congr (equiv.refl α) eq /-- An equivalence `e : α ≃ β` generates an equivalence between the quotient space of `α` by a relation `ra` and the quotient space of `β` by the image of this relation under `e`. -/ protected def congr_left {r : α → α → Prop} (e : α ≃ β) : quot r ≃ quot (λ b b', r (e.symm b) (e.symm b')) := @quot.congr α β r (λ b b', r (e.symm b) (e.symm b')) e (λ a₁ a₂, by simp only [e.symm_apply_apply]) end quot namespace quotient /-- An equivalence `e : α ≃ β` generates an equivalence between quotient spaces, if `ra a₁ a₂ ↔ rb (e a₁) (e a₂). -/ protected def congr {ra : setoid α} {rb : setoid β} (e : α ≃ β) (eq : ∀a₁ a₂, @setoid.r α ra a₁ a₂ ↔ @setoid.r β rb (e a₁) (e a₂)) : quotient ra ≃ quotient rb := quot.congr e eq /-- Quotients are congruent on equivalences under equality of their relation. An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/ protected def congr_right {r r' : setoid α} (eq : ∀a₁ a₂, @setoid.r α r a₁ a₂ ↔ @setoid.r α r' a₁ a₂) : quotient r ≃ quotient r' := quot.congr_right eq end quotient /-- If a function is a bijection between `univ` and a set `s` in the target type, it induces an equivalence between the original type and the type `↑s`. -/ noncomputable def set.bij_on.equiv {α : Type*} {β : Type*} {s : set β} (f : α → β) (h : set.bij_on f set.univ s) : α ≃ s := begin have : function.bijective (λ (x : α), (⟨f x, begin exact h.maps_to (set.mem_univ x) end⟩ : s)), { split, { assume x y hxy, apply h.inj_on (set.mem_univ x) (set.mem_univ y) (subtype.mk.inj hxy) }, { assume x, rcases h.surj_on x.2 with ⟨y, hy⟩, exact ⟨y, subtype.eq hy.2⟩ } }, exact equiv.of_bijective _ this end /-- The composition of an updated function with an equiv on a subset can be expressed as an updated function. -/ lemma dite_comp_equiv_update {α : Type*} {β : Type*} {γ : Type*} {s : set α} (e : β ≃ s) (v : β → γ) (w : α → γ) (j : β) (x : γ) [decidable_eq β] [decidable_eq α] [∀ j, decidable (j ∈ s)] : (λ (i : α), if h : i ∈ s then (function.update v j x) (e.symm ⟨i, h⟩) else w i) = function.update (λ (i : α), if h : i ∈ s then v (e.symm ⟨i, h⟩) else w i) (e j) x := begin ext i, by_cases h : i ∈ s, { simp only [h, dif_pos], have A : e.symm ⟨i, h⟩ = j ↔ i = e j, by { rw equiv.symm_apply_eq, exact subtype.ext_iff_val }, by_cases h' : i = e j, { rw [A.2 h', h'], simp }, { have : ¬ e.symm ⟨i, h⟩ = j, by simpa [← A] using h', simp [h, h', this] } }, { have : i ≠ e j, by { contrapose! h, have : (e j : α) ∈ s := (e j).2, rwa ← h at this }, simp [h, this] } end
cc2a60b08dc467f58c6e0ed7672a964b0ca4c059
5ca7b1b12d14c4742e29366312ba2c2ef8201b21
/levels/idea_subtraction.lean
bbcaebe8c69bf4aa53f669b890c86bd9347d624e
[ "Apache-2.0" ]
permissive
MatthiasHu/natural_number_game
2e464482ef3001863430b0336133b6697b275ba3
2d764f72669ae30861f6a1057fce0257f3e466c4
refs/heads/master
1,609,719,110,419
1,576,345,737,000
1,576,345,737,000
240,296,314
0
0
Apache-2.0
1,581,608,357,000
1,581,608,356,000
null
UTF-8
Lean
false
false
645
lean
/- to be written (feel free to write it, anyone!) The natural numbers has this hideous "computer science natural number `subtraction`" which is far less well behaved than integer subtraction -- they define it so that `2 - 7 = 0` and more generally all negative numbers are mapped to zero. There are inqualities eveyywhere. Type `#print prefix nat` into Lean, click on the display that appears, and then do ctrl-F and search for sub. There are loads of lemmas. I think it's all a bit boring though. Is this worth doing? I don't think you get any new instances this way, which is some sort of an indication that mathematicians don't care. -/
27bd28f5a4a8f9e005f3ecd0f6b0af2deca1710d
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/limits/shapes/zero.lean
00e02e2b6ffd7ded3f924b25f55001e420f6b4fd
[]
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
16,377
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.category_theory.limits.shapes.terminal import Mathlib.category_theory.limits.shapes.binary_products import Mathlib.category_theory.limits.shapes.products import Mathlib.category_theory.limits.shapes.images import Mathlib.PostPort universes v u l u_1 v' u' namespace Mathlib /-! # Zero morphisms and zero objects A category "has zero morphisms" if there is a designated "zero morphism" in each morphism space, and compositions of zero morphisms with anything give the zero morphism. (Notice this is extra structure, not merely a property.) A category "has a zero object" if it has an object which is both initial and terminal. Having a zero object provides zero morphisms, as the unique morphisms factoring through the zero object. ## References * https://en.wikipedia.org/wiki/Zero_morphism * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] -/ namespace category_theory.limits /-- A category "has zero morphisms" if there is a designated "zero morphism" in each morphism space, and compositions of zero morphisms with anything give the zero morphism. -/ class has_zero_morphisms (C : Type u) [category C] where has_zero : (X Y : C) → HasZero (X ⟶ Y) comp_zero' : autoParam (∀ {X Y : C} (f : X ⟶ Y), C → f ≫ 0 = 0) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) zero_comp' : autoParam (C → ∀ {Y Z : C} (f : Y ⟶ Z), 0 ≫ f = 0) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) theorem has_zero_morphisms.comp_zero {C : Type u} [category C] [c : has_zero_morphisms C] {X : C} {Y : C} (f : X ⟶ Y) (Z : C) : f ≫ 0 = 0 := sorry theorem has_zero_morphisms.zero_comp {C : Type u} [category C] [c : has_zero_morphisms C] (X : C) {Y : C} {Z : C} (f : Y ⟶ Z) : 0 ≫ f = 0 := sorry @[simp] theorem comp_zero {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} {f : X ⟶ Y} {Z : C} : f ≫ 0 = 0 := has_zero_morphisms.comp_zero f Z @[simp] theorem zero_comp {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} {Z : C} {f : Y ⟶ Z} : 0 ≫ f = 0 := has_zero_morphisms.zero_comp X f protected instance has_zero_morphisms_pempty : has_zero_morphisms (discrete pempty) := has_zero_morphisms.mk protected instance has_zero_morphisms_punit : has_zero_morphisms (discrete PUnit) := has_zero_morphisms.mk namespace has_zero_morphisms /-- This lemma will be immediately superseded by `ext`, below. -/ /-- If you're tempted to use this lemma "in the wild", you should probably carefully consider whether you've made a mistake in allowing two instances of `has_zero_morphisms` to exist at all. See, particularly, the note on `zero_morphisms_of_zero_object` below. -/ theorem ext {C : Type u} [category C] (I : has_zero_morphisms C) (J : has_zero_morphisms C) : I = J := sorry protected instance subsingleton {C : Type u} [category C] : subsingleton (has_zero_morphisms C) := subsingleton.intro ext end has_zero_morphisms theorem zero_of_comp_mono {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} (g : Y ⟶ Z) [mono g] (h : f ≫ g = 0) : f = 0 := eq.mp (Eq._oldrec (Eq.refl (f ≫ g = 0 ≫ g)) (propext (cancel_mono g))) (eq.mp (Eq._oldrec (Eq.refl (f ≫ g = 0)) (Eq.symm zero_comp)) h) theorem zero_of_epi_comp {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) {g : Y ⟶ Z} [epi f] (h : f ≫ g = 0) : g = 0 := eq.mp (Eq._oldrec (Eq.refl (f ≫ g = f ≫ 0)) (propext (cancel_epi f))) (eq.mp (Eq._oldrec (Eq.refl (f ≫ g = 0)) (Eq.symm comp_zero)) h) theorem eq_zero_of_image_eq_zero {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} {f : X ⟶ Y} [has_image f] (w : image.ι f = 0) : f = 0 := sorry theorem nonzero_image_of_nonzero {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} {f : X ⟶ Y} [has_image f] (w : f ≠ 0) : image.ι f ≠ 0 := fun (h : image.ι f = 0) => w (eq_zero_of_image_eq_zero h) theorem equivalence_preserves_zero_morphisms {C : Type u} [category C] (D : Type u') [category D] [has_zero_morphisms C] [has_zero_morphisms D] (F : C ≌ D) (X : C) (Y : C) : functor.map (equivalence.functor F) 0 = 0 := sorry @[simp] theorem is_equivalence_preserves_zero_morphisms {C : Type u} [category C] (D : Type u') [category D] [has_zero_morphisms C] [has_zero_morphisms D] (F : C ⥤ D) [is_equivalence F] (X : C) (Y : C) : functor.map F 0 = 0 := sorry /-- A category "has a zero object" if it has an object which is both initial and terminal. -/ class has_zero_object (C : Type u) [category C] where zero : C unique_to : (X : C) → unique (zero ⟶ X) unique_from : (X : C) → unique (X ⟶ zero) protected instance has_zero_object_punit : has_zero_object (discrete PUnit) := has_zero_object.mk PUnit.unit (fun (X : discrete PUnit) => punit.cases_on X (unique.mk sorry sorry)) fun (X : discrete PUnit) => punit.cases_on X (unique.mk sorry sorry) namespace has_zero_object /-- Construct a `has_zero C` for a category with a zero object. This can not be a global instance as it will trigger for every `has_zero C` typeclass search. -/ protected def has_zero {C : Type u} [category C] [has_zero_object C] : HasZero C := { zero := zero } theorem to_zero_ext {C : Type u} [category C] [has_zero_object C] {X : C} (f : X ⟶ 0) (g : X ⟶ 0) : f = g := eq.mpr (id (Eq._oldrec (Eq.refl (f = g)) (unique.uniq (unique_from X) f))) (eq.mpr (id (Eq._oldrec (Eq.refl (Inhabited.default = g)) (unique.uniq (unique_from X) g))) (Eq.refl Inhabited.default)) theorem from_zero_ext {C : Type u} [category C] [has_zero_object C] {X : C} (f : 0 ⟶ X) (g : 0 ⟶ X) : f = g := eq.mpr (id (Eq._oldrec (Eq.refl (f = g)) (unique.uniq (unique_to X) f))) (eq.mpr (id (Eq._oldrec (Eq.refl (Inhabited.default = g)) (unique.uniq (unique_to X) g))) (Eq.refl Inhabited.default)) protected instance category_theory.iso.subsingleton {C : Type u} [category C] [has_zero_object C] (X : C) : subsingleton (X ≅ 0) := subsingleton.intro fun (a b : X ≅ 0) => iso.ext (of_as_true trivial) protected instance category_theory.mono {C : Type u} [category C] [has_zero_object C] {X : C} (f : 0 ⟶ X) : mono f := mono.mk fun (Z : C) (g h : Z ⟶ 0) (w : g ≫ f = h ≫ f) => to_zero_ext g h protected instance category_theory.epi {C : Type u} [category C] [has_zero_object C] {X : C} (f : X ⟶ 0) : epi f := epi.mk fun (Z : C) (g h : 0 ⟶ Z) (w : f ≫ g = f ≫ h) => from_zero_ext g h /-- A category with a zero object has zero morphisms. It is rarely a good idea to use this. Many categories that have a zero object have zero morphisms for some other reason, for example from additivity. Library code that uses `zero_morphisms_of_zero_object` will then be incompatible with these categories because the `has_zero_morphisms` instances will not be definitionally equal. For this reason library code should generally ask for an instance of `has_zero_morphisms` separately, even if it already asks for an instance of `has_zero_objects`. -/ def zero_morphisms_of_zero_object {C : Type u} [category C] [has_zero_object C] : has_zero_morphisms C := has_zero_morphisms.mk /-- A zero object is in particular initial. -/ theorem has_initial {C : Type u} [category C] [has_zero_object C] : has_initial C := has_initial_of_unique 0 /-- A zero object is in particular terminal. -/ theorem has_terminal {C : Type u} [category C] [has_zero_object C] : has_terminal C := has_terminal_of_unique 0 end has_zero_object @[simp] theorem id_zero {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] : 𝟙 = 0 := has_zero_object.from_zero_ext 𝟙 0 /-- An arrow ending in the zero object is zero -/ -- This can't be a `simp` lemma because the left hand side would be a metavariable. theorem zero_of_to_zero {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] {X : C} (f : X ⟶ 0) : f = 0 := has_zero_object.to_zero_ext f 0 theorem zero_of_target_iso_zero {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] {X : C} {Y : C} (f : X ⟶ Y) (i : Y ≅ 0) : f = 0 := sorry /-- An arrow starting at the zero object is zero -/ theorem zero_of_from_zero {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] {X : C} (f : 0 ⟶ X) : f = 0 := has_zero_object.from_zero_ext f 0 theorem zero_of_source_iso_zero {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] {X : C} {Y : C} (f : X ⟶ Y) (i : X ≅ 0) : f = 0 := sorry theorem mono_of_source_iso_zero {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] {X : C} {Y : C} (f : X ⟶ Y) (i : X ≅ 0) : mono f := sorry theorem epi_of_target_iso_zero {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] {X : C} {Y : C} (f : X ⟶ Y) (i : Y ≅ 0) : epi f := sorry /-- An object `X` has `𝟙 X = 0` if and only if it is isomorphic to the zero object. Because `X ≅ 0` contains data (even if a subsingleton), we express this `↔` as an `≃`. -/ def id_zero_equiv_iso_zero {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] (X : C) : 𝟙 = 0 ≃ (X ≅ 0) := equiv.mk (fun (h : 𝟙 = 0) => iso.mk 0 0) sorry sorry sorry @[simp] theorem id_zero_equiv_iso_zero_apply_hom {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] (X : C) (h : 𝟙 = 0) : iso.hom (coe_fn (id_zero_equiv_iso_zero X) h) = 0 := rfl @[simp] theorem id_zero_equiv_iso_zero_apply_inv {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] (X : C) (h : 𝟙 = 0) : iso.inv (coe_fn (id_zero_equiv_iso_zero X) h) = 0 := rfl /-- A zero morphism `0 : X ⟶ Y` is an isomorphism if and only if the identities on both `X` and `Y` are zero. -/ def is_iso_zero_equiv {C : Type u} [category C] [has_zero_morphisms C] (X : C) (Y : C) : is_iso 0 ≃ 𝟙 = 0 ∧ 𝟙 = 0 := equiv.mk sorry (fun (h : 𝟙 = 0 ∧ 𝟙 = 0) => is_iso.mk 0) sorry sorry /-- A zero morphism `0 : X ⟶ X` is an isomorphism if and only if the identity on `X` is zero. -/ def is_iso_zero_self_equiv {C : Type u} [category C] [has_zero_morphisms C] (X : C) : is_iso 0 ≃ 𝟙 = 0 := eq.mpr sorry (eq.mp sorry (is_iso_zero_equiv X X)) /-- A zero morphism `0 : X ⟶ Y` is an isomorphism if and only if `X` and `Y` are isomorphic to the zero object. -/ def is_iso_zero_equiv_iso_zero {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] (X : C) (Y : C) : is_iso 0 ≃ (X ≅ 0) × (Y ≅ 0) := equiv.trans (is_iso_zero_equiv X Y) (equiv.symm (equiv.mk sorry (fun (ᾰ : 𝟙 = 0 ∧ 𝟙 = 0) => and.dcases_on ᾰ fun (hX : 𝟙 = 0) (hY : 𝟙 = 0) => (coe_fn (id_zero_equiv_iso_zero X) hX, coe_fn (id_zero_equiv_iso_zero Y) hY)) sorry sorry)) /-- A zero morphism `0 : X ⟶ X` is an isomorphism if and only if `X` is isomorphic to the zero object. -/ def is_iso_zero_self_equiv_iso_zero {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] (X : C) : is_iso 0 ≃ (X ≅ 0) := equiv.trans (is_iso_zero_equiv_iso_zero X X) subsingleton_prod_self_equiv /-- If there are zero morphisms, any initial object is a zero object. -/ protected instance has_zero_object_of_has_initial_object {C : Type u} [category C] [has_zero_morphisms C] [has_initial C] : has_zero_object C := has_zero_object.mk (⊥_C) (fun (X : C) => unique.mk { default := 0 } sorry) fun (X : C) => unique.mk { default := 0 } sorry /-- If there are zero morphisms, any terminal object is a zero object. -/ protected instance has_zero_object_of_has_terminal_object {C : Type u} [category C] [has_zero_morphisms C] [has_terminal C] : has_zero_object C := has_zero_object.mk (⊤_C) (fun (X : C) => unique.mk { default := 0 } sorry) fun (X : C) => unique.mk { default := 0 } sorry theorem image_ι_comp_eq_zero {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} [has_image f] [epi (factor_thru_image f)] (h : f ≫ g = 0) : image.ι f ≫ g = 0 := sorry /-- The zero morphism has a `mono_factorisation` through the zero object. -/ @[simp] theorem mono_factorisation_zero_e {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] (X : C) (Y : C) : mono_factorisation.e (mono_factorisation_zero X Y) = 0 := Eq.refl (mono_factorisation.e (mono_factorisation_zero X Y)) /-- The factorisation through the zero object is an image factorisation. -/ def image_factorisation_zero {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] (X : C) (Y : C) : image_factorisation 0 := image_factorisation.mk (mono_factorisation_zero X Y) (is_image.mk fun (F' : mono_factorisation 0) => 0) protected instance has_image_zero {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] {X : C} {Y : C} : has_image 0 := has_image.mk (image_factorisation_zero X Y) /-- The image of a zero morphism is the zero object. -/ def image_zero {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] {X : C} {Y : C} : image 0 ≅ 0 := is_image.iso_ext (image.is_image 0) (image_factorisation.is_image (image_factorisation_zero X Y)) /-- The image of a morphism which is equal to zero is the zero object. -/ def image_zero' {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] {X : C} {Y : C} {f : X ⟶ Y} (h : f = 0) [has_image f] : image f ≅ 0 := image.eq_to_iso h ≪≫ image_zero @[simp] theorem image.ι_zero {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] {X : C} {Y : C} [has_image 0] : image.ι 0 = 0 := sorry /-- If we know `f = 0`, it requires a little work to conclude `image.ι f = 0`, because `f = g` only implies `image f ≅ image g`. -/ @[simp] theorem image.ι_zero' {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] [has_equalizers C] {X : C} {Y : C} {f : X ⟶ Y} (h : f = 0) [has_image f] : image.ι f = 0 := sorry /-- In the presence of zero morphisms, coprojections into a coproduct are (split) monomorphisms. -/ protected instance split_mono_sigma_ι {C : Type u} [category C] {β : Type v} [DecidableEq β] [has_zero_morphisms C] (f : β → C) [has_colimit (discrete.functor f)] (b : β) : split_mono (sigma.ι f b) := split_mono.mk (sigma.desc fun (b' : β) => dite (b' = b) (fun (h : b' = b) => eq_to_hom (congr_arg f h)) fun (h : ¬b' = b) => 0) /-- In the presence of zero morphisms, projections into a product are (split) epimorphisms. -/ protected instance split_epi_pi_π {C : Type u} [category C] {β : Type v} [DecidableEq β] [has_zero_morphisms C] (f : β → C) [has_limit (discrete.functor f)] (b : β) : split_epi (pi.π f b) := split_epi.mk (pi.lift fun (b' : β) => dite (b = b') (fun (h : b = b') => eq_to_hom (congr_arg f h)) fun (h : ¬b = b') => 0) /-- In the presence of zero morphisms, coprojections into a coproduct are (split) monomorphisms. -/ protected instance split_mono_coprod_inl {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} [has_colimit (pair X Y)] : split_mono coprod.inl := split_mono.mk (coprod.desc 𝟙 0) /-- In the presence of zero morphisms, coprojections into a coproduct are (split) monomorphisms. -/ protected instance split_mono_coprod_inr {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} [has_colimit (pair X Y)] : split_mono coprod.inr := split_mono.mk (coprod.desc 0 𝟙) /-- In the presence of zero morphisms, projections into a product are (split) epimorphisms. -/ protected instance split_epi_prod_fst {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} [has_limit (pair X Y)] : split_epi prod.fst := split_epi.mk (prod.lift 𝟙 0) /-- In the presence of zero morphisms, projections into a product are (split) epimorphisms. -/ protected instance split_epi_prod_snd {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} [has_limit (pair X Y)] : split_epi prod.snd := split_epi.mk (prod.lift 0 𝟙)
969d21d48ac0c2b6eea96a96319e3e7b90f74839
35c41e824c38dc6aa7f0b1467f4d5bc33d61f260
/Build_Maths/build_maths.lean
b8c534099ebc16818b5030a3687cbb5826eadaec
[]
no_license
kckennylau/xena
697eb8f55a8f1ce456a241e8b8e4e8a27b91438e
7dc03dc9588d30d7d36b6904b59ca63a72096229
refs/heads/master
1,627,104,810,584
1,509,486,416,000
1,509,486,416,000
107,829,951
0
0
null
1,509,486,381,000
1,508,637,567,000
Lean
UTF-8
Lean
false
false
2,705
lean
namespace xena inductive xnat | zero : xnat | succ : xnat → xnat open xnat definition plus : xnat → xnat → xnat | n zero := n | n (succ p) := succ (plus n p) notation a + b := plus a b definition one := succ zero definition two := succ one example : one + one = two := begin refl end theorem plus_assoc (a b c : xnat) : (a + b) + c = a + (b + c) := begin induction c with n Hn, unfold plus, unfold plus, rw [Hn], end theorem add_zero (n : xnat) : n + zero = n := by unfold plus theorem zero_add (n : xnat) : zero + n = n := begin induction n with t Ht, refl, unfold plus, rw [Ht], end theorem zero_add_eq_add_zero (n : xnat) : zero + n = n + zero := begin rewrite [zero_add,add_zero] end theorem one_plus_eq_succ (n : xnat) : (succ zero) + n = succ n := begin induction n with a Ha, refl, unfold plus, rw [Ha], end theorem plus_one_eq_succ (n : xnat) : n + (succ zero) = succ n := begin unfold plus end theorem plus_comm (a b : xnat) : plus a b = plus b a := begin induction a with m Hm, -- base case exact zero_add_eq_add_zero b, -- inductive step unfold plus, rewrite ←one_plus_eq_succ, rewrite ←one_plus_eq_succ (b+m), rewrite plus_assoc, rewrite Hm end -- Now your turn! definition times : xnat → xnat → xnat | n zero := zero | n (succ p) := (times n p) + n notation a * b := times a b theorem times_zero (a : xnat) : a * zero = zero := rfl theorem zero_times (a : xnat) : zero * a = zero := sorry theorem times_one (a : xnat) : a * (succ zero) = a := sorry theorem one_times (a : xnat) : (succ zero) * a = a := sorry theorem right_distrib (a b c : xnat) : a * (b + c) = a* b + a * c := sorry -- I'll do the next one for you because I found it the hardest. Feel -- free to delete it and find your own proof -- or even a better proof! theorem left_distrib (a b c : xnat) : (a + b) * c = a * c + b * c := begin induction c with n Hn, unfold times, refl, rw [←plus_one_eq_succ,right_distrib,Hn,right_distrib,right_distrib], rw [times_one,times_one,times_one], rw [plus_assoc,←plus_assoc (b*n),plus_comm (b*n),←plus_assoc,←plus_assoc,←plus_assoc], end theorem times_assoc (a b c : xnat) : (a * b) * c = a * (b * c) := sorry theorem times_comm (a b : xnat) : a * b = b * a := sorry definition lessthan : xnat → xnat → Prop | zero zero := false | (succ m) zero := false | zero (succ p) := true | (succ m) (succ p) := lessthan m p notation a < b := lessthan a b theorem plus_succ_equals_succ (a b : xnat) : a + (succ b) = succ (a + b) := sorry theorem inequality_A1 (a b t : xnat) : a < b → a + t < b + t := sorry -- A1 : a<b -> a+t<b+t -- A2 : a<b, b<c -> a<c -- A3 : x<y or x=y or x>y -- A4 : x>0,y>0 -> xy>0 end xena
6faafbc576f9627b446a1518a384801da81f1a23
33340b3a23ca62ef3c8a7f6a2d4e14c07c6d3354
/lia/eval_sqe.lean
d716daebbb9fe1b2958ad5eee7b23696f8322f1f
[]
no_license
lclem/cooper
79554e72ced343c64fed24b2d892d24bf9447dfe
812afc6b158821f2e7dac9c91d3b6123c7a19faf
refs/heads/master
1,607,554,257,488
1,578,694,133,000
1,578,694,133,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,744
lean
import .eval_sqe_core .qfree_sqe .wf_sqe open list lemma hco_le_eq_of_ne_zero {m i k ks} : k ≠ 0 → atom.unify m (atom.le i (k::ks)) = let m' := (m / (abs k)) in atom.le (m' * i) (znum.sign k :: map_mul m' ks) := begin intro h, simp [atom.unify], rw if_neg, apply h end lemma hco_dvd_eq_of_ne_zero {m d i k ks} : k ≠ 0 → atom.unify m (atom.dvd d i (k::ks)) = let m' := (m / k) in atom.dvd (m' * d) (m' * i) (1 :: map_mul m' ks) := begin intro h, simp [atom.unify], rw if_neg, apply h end lemma hco_ndvd_eq_of_ne_zero {m d i k ks} : k ≠ 0 → atom.unify m (atom.ndvd d i (k::ks)) = let m' := (m / k) in atom.ndvd (m' * d) (m' * i) (1 :: map_mul m' ks) := begin intro h, simp [atom.unify], rw if_neg, apply h end lemma clcm_le_eq_of_ne_zero {i k : znum} {ks : list znum} : k ≠ 0 → coeffs_lcm (A' (atom.le i (k::ks))) = abs k := begin intro h, simp [coeffs_lcm, formula.atoms_dep_0, dep_0, formula.atoms, filter, head_coeff ], rw if_pos, simp [head_coeff, znum.lcms, znum.lcm, num.lcm, znum.abs_eq_abs_to_znum, znum.abs_one, num.gcd_one_right, num.div_one], apply h end lemma clcm_dvd_eq_of_ne_zero {d i k : znum} {ks : list znum} : k ≠ 0 → coeffs_lcm (A' (atom.dvd d i (k::ks))) = abs k := begin intro h, simp [coeffs_lcm, formula.atoms_dep_0, dep_0, formula.atoms, filter, head_coeff ], rw if_pos, simp [head_coeff, znum.lcms, znum.lcm, num.lcm, znum.abs_eq_abs_to_znum, znum.abs_one, num.gcd_one_right, num.div_one], apply h end lemma clcm_ndvd_eq {d i : znum} {ks : list znum} : coeffs_lcm (A' (atom.ndvd d i ks)) = coeffs_lcm (A' (atom.dvd d i ks)) := begin cases ks with k ks; simp [coeffs_lcm, formula.atoms_dep_0, dep_0, formula.atoms, filter, head_coeff ], by_cases hkz : k = 0, { subst hkz, repeat {rw if_neg}, simp, simp, }, { repeat {rw if_pos}, simp [head_coeff], assumption, assumption } end lemma eval_hco_le_iff {lcm z i : znum} {zs ks : list znum} (hpos : 0 < lcm) (hdvd : coeffs_lcm (A' atom.le i ks) ∣ lcm) : (A' (atom.unify lcm (atom.le i ks))).eval (lcm * z :: zs) ↔ (A' (atom.le i ks)).eval (z :: zs) := begin cases ks with k ks, { simp [atom.unify, eval_le] }, { by_cases hkz : k = 0, { subst hkz, simp [atom.unify, eval_le, znum.dot_prod] }, { rw clcm_le_eq_of_ne_zero hkz at hdvd, rw (hco_le_eq_of_ne_zero hkz), simp [eval_le, znum.dot_prod], apply calc lcm / abs k * i ≤ znum.sign k * (lcm * z) + znum.dot_prod (map_mul (lcm / abs k) ks) zs ↔ (lcm / abs k) * i ≤ (lcm / abs k) * (znum.dot_prod ks zs + k * z) : begin rw [mul_add, add_comm, znum.map_mul_dot_prod], have heq : znum.sign k * (lcm * z) = lcm / abs k * (k * z), { rw [(mul_assoc _ k _).symm, (znum.div_mul_comm _ _ _ hdvd), (mul_comm lcm k), (znum.div_mul_comm _ _ _ _).symm, znum.sign_eq_div_abs, mul_assoc], rw znum.abs_dvd }, rw heq, end ... ↔ i ≤ znum.dot_prod ks zs + k * z : begin apply znum.mul_le_mul_iff_le_of_pos_left, apply znum.div_pos_of_pos_of_dvd hpos (abs_nonneg _) hdvd end } } end lemma eval_hco_dvd_iff {lcm z d i : znum} {zs ks} (hpos : lcm > 0) (hdvd : coeffs_lcm (A' atom.dvd d i ks) ∣ lcm) : (A' (atom.unify lcm (atom.dvd d i ks))).eval (lcm * z :: zs) ↔ (A' (atom.dvd d i ks)).eval (z :: zs) := begin cases ks with k ks, { simp [atom.unify, eval_dvd] }, { by_cases hkz : k = 0, { subst hkz, simp [atom.unify, eval_dvd, znum.dot_prod] }, { rw clcm_dvd_eq_of_ne_zero hkz at hdvd, rw (hco_dvd_eq_of_ne_zero hkz), simp [eval_dvd, znum.dot_prod], apply calc lcm / k * d ∣ lcm * z + (lcm / k * i + znum.dot_prod (map_mul (lcm / k) ks) zs) ↔ (lcm / k) * d ∣ (lcm / k) * (i + (znum.dot_prod ks zs + k * z)) : begin rw [mul_add, znum.map_mul_dot_prod, add_comm, mul_add, add_assoc], have heq : lcm / k * (k * z) = lcm * z, { rw [(mul_assoc _ _ _).symm, znum.div_mul_cancel], rw znum.abs_dvd at hdvd, apply hdvd }, rw heq end ... ↔ d ∣ i + (znum.dot_prod ks zs + k * z) : begin apply mul_dvd_mul_iff_left, apply znum.div_nonzero, apply znum.nonzero_of_pos hpos, rw znum.abs_dvd at hdvd, apply hdvd, end } } end lemma eval_hco_ndvd_iff {m d i ks} {zs : list znum} : (A' (atom.unify m (atom.ndvd d i ks))).eval zs ↔ ¬ (A' (atom.unify m (atom.dvd d i ks))).eval zs := begin cases ks with k ks; simp [atom.unify, eval_dvd, eval_ndvd], by_cases hkz : k = 0, { subst hkz, repeat {rw if_pos}, simp [eval_dvd, eval_ndvd], refl }, { repeat {rw if_neg}, simp [eval_dvd, eval_ndvd], assumption, assumption } end lemma coeffs_lcm_and (p q) : coeffs_lcm (p ∧' q) = znum.lcm (coeffs_lcm p) (coeffs_lcm q) := begin apply znum.lcms_distrib, apply list.equiv.trans, apply list.map_equiv_map_of_equiv, simp [formula.atoms_dep_0, formula.atoms], apply equiv.refl, simp [map_append, formula.atoms_dep_0], apply equiv.symm union_equiv_append, end lemma coeffs_lcm_or (p q) : coeffs_lcm (p ∨' q) = znum.lcm (coeffs_lcm p) (coeffs_lcm q) := begin apply znum.lcms_distrib, apply list.equiv.trans, apply list.map_equiv_map_of_equiv, simp [formula.atoms_dep_0, formula.atoms], apply equiv.refl, simp [map_append, formula.atoms_dep_0], apply equiv.symm union_equiv_append, end lemma hcso_prsv_1 (lcm z : znum) (zs) (hlcm1 : lcm > 0) (hdvd : lcm ∣ z) : ∀ (p : formula), nqfree p → formula.wf p → (has_dvd.dvd (coeffs_lcm p) lcm) → (formula.map (atom.unify lcm) p).eval (z::zs) → p.eval ((has_div.div z lcm)::zs) | ⊤' hf hn _ h := begin simp [eval_true] end | ⊥' hf hn _ h := begin exfalso, apply h end | (A' (atom.le i ks)) hf hn hlcm2 h := begin simp [formula.map] at h, have heq : z = lcm * (z / lcm), { rw [mul_comm, znum.div_mul_cancel hdvd] }, rw heq at h, rw eval_hco_le_iff at h; try {assumption}, end | (A' (atom.dvd d i ks)) hf hn hlcm2 h := begin simp [formula.map] at h, have heq : z = lcm * (z / lcm), { rw [mul_comm, znum.div_mul_cancel hdvd] }, rw heq at h, rewrite eval_hco_dvd_iff at h; try {assumption}, end | (A' (atom.ndvd d i ks)) hf hn hlcm2 h := begin simp [formula.map] at h, have heq : z = lcm * (z / lcm), { rw [mul_comm, znum.div_mul_cancel hdvd] }, rw heq at h, rewrite eval_hco_ndvd_iff at h, rewrite eval_hco_dvd_iff at h, rw eval_ndvd', assumption, assumption, rw clcm_ndvd_eq at hlcm2, assumption, end | (p ∧' q) hf hn hlcm2 h := begin rewrite eval_and, cases hf with hfp hfq, cases hn with hnp hnq, unfold formula.map at h, cases h with hp hq, rewrite coeffs_lcm_and at hlcm2, apply and.intro; apply hcso_prsv_1; try {assumption}, apply dvd.trans _ hlcm2, apply znum.dvd_lcm_left, apply dvd.trans _ hlcm2, apply znum.dvd_lcm_right, end | (p ∨' q) hf hn hlcm2 h := begin rewrite coeffs_lcm_or at hlcm2, rewrite eval_or, cases hf with hfp hfq, cases hn with hnp hnq, unfold formula.map at h, rewrite eval_or at h, cases h with hp hq, apply or.inl, apply hcso_prsv_1; try {assumption}, apply dvd.trans _ hlcm2, apply znum.dvd_lcm_left, apply or.inr, apply hcso_prsv_1; try {assumption}, apply dvd.trans _ hlcm2, apply znum.dvd_lcm_right end | (¬' p) hf hn _ h := by cases hf | (∃' p) hf hn _ h := by cases hf lemma hcso_prsv_2 (lcm z : znum) (zs) (hpos : lcm > 0) : ∀ (p : formula), nqfree p → formula.wf p → has_dvd.dvd (coeffs_lcm p) lcm → p.eval (z::zs) → (formula.map (atom.unify lcm) p).eval ((lcm * z)::zs) | ⊤' hf hn hdvd h := trivial | ⊥' hf hn hdvd h := by cases h | (A' (atom.le i ks)) hf hn hdvd h := begin unfold formula.map, rewrite eval_hco_le_iff, apply h, apply hpos, apply hdvd end | (A' (atom.dvd d i ks)) hf hn hdvd h := begin unfold formula.map, rewrite eval_hco_dvd_iff, apply h, apply hpos, apply hdvd end | (A' (atom.ndvd d i ks)) hf hn hdvd h := begin unfold formula.map, rw [eval_hco_ndvd_iff, eval_hco_dvd_iff], apply h, apply hpos, rw clcm_ndvd_eq at hdvd, apply hdvd end | (p ∧' q) hf hn hdvd h := begin unfold formula.map, rewrite eval_and, rewrite eval_and at h, cases h with hp hq, cases hn with hnp hnq, cases hf with hfp hfq, rewrite coeffs_lcm_and at hdvd, apply and.intro; apply hcso_prsv_2; try {assumption}, apply dvd.trans _ hdvd, apply znum.dvd_lcm_left, apply dvd.trans _ hdvd, apply znum.dvd_lcm_right end | (p ∨' q) hf hn hdvd h := begin unfold formula.map, rewrite eval_or, rewrite eval_or at h, cases hn with hnp hnq, cases hf with hfp hfq, rewrite coeffs_lcm_or at hdvd, cases h with hp hq, apply or.inl, apply hcso_prsv_2; try {assumption}, apply dvd.trans _ hdvd, apply znum.dvd_lcm_left, apply or.inr, apply hcso_prsv_2; try {assumption}, apply dvd.trans _ hdvd, apply znum.dvd_lcm_right end | (¬' p) hf hn hdvd h := by cases hf | (∃' p) hf hn hdvd h := by cases hf lemma coeffs_lcm_pos (p) : coeffs_lcm p > 0 := begin apply znum.lcms_pos, intros z hz, rewrite list.mem_map at hz, cases hz with a ha, cases ha with ha1 ha2, subst ha2, unfold formula.atoms_dep_0 at ha1, rw (@mem_filter _ dep_0 _ a (formula.atoms p)) at ha1, apply ha1^.elim_right end lemma hcso_prsv : ∀ (p : formula) (hf : nqfree p) (hn : formula.wf p) (bs : list znum), (∃ (b : znum), (p.unify).eval (b :: bs)) ↔ ∃ (b : znum), p.eval (b :: bs) := begin intros p hf hn bs, apply iff.intro; intro h; cases h with z hz, { unfold formula.unify at hz, rewrite eval_and at hz, existsi (has_div.div z (coeffs_lcm p)), cases hz with hz1 hz2, apply hcso_prsv_1; try {assumption}, rewrite eval_dvd at hz1, rewrite zero_add at hz1, apply coeffs_lcm_pos, simp [eval_dvd, znum.dot_prod] at hz1, apply hz1, apply dvd_refl }, { existsi (coeffs_lcm p * z), unfold formula.unify, rewrite eval_and, apply and.intro, rewrite eval_dvd, rewrite zero_add, simp [znum.dot_prod], apply hcso_prsv_2; try {assumption}, apply coeffs_lcm_pos, apply dvd_refl } end lemma unified_sign {k} : znum.sign k = -1 ∨ znum.sign k = 0 ∨ znum.sign k = 1 := begin cases lt_trichotomy k 0 with h h, { rw znum.sign_eq_neg_one_of_neg h, apply or.inl rfl }, cases h with h h, { rw znum.sign_eq_zero_iff_zero, apply or.inr (or.inl h) }, { rw znum.sign_eq_one_of_pos h, apply or.inr (or.inr rfl) }, end lemma unified_atom_hco {k} : ∀ {a}, (atom.unify k a).unified | (atom.le i []) := or.inr (or.inl rfl) | (atom.ndvd d i []) := or.inr (or.inl rfl) | (atom.le i (k::ks)) := begin by_cases hkz : k = 0, { subst hkz, apply or.inr (or.inl rfl) }, { rw hco_le_eq_of_ne_zero hkz, apply unified_sign } end | (atom.dvd d i []) := or.inr (or.inl rfl) | (atom.dvd d i (k::ks)) := begin by_cases hkz : k = 0, { subst hkz, apply or.inr (or.inl rfl) }, { rw hco_dvd_eq_of_ne_zero hkz, apply or.inr (or.inr rfl) } end | (atom.ndvd d i (k::ks)) := begin by_cases hkz : k = 0, { subst hkz, apply or.inr (or.inl rfl) }, { rw hco_ndvd_eq_of_ne_zero hkz, apply or.inr (or.inr rfl) } end lemma unified_formula.map_hco {k} : ∀ p, nqfree p → unified (formula.map (atom.unify k) p) | ⊤' hf a hm := by cases hm | ⊥' hf a hm := by cases hm | (A' a') hf a hm := begin simp [formula.atoms, formula.map] at hm, subst hm, apply unified_atom_hco, end | (p ∧' q) hf a hm := begin cases hf with hfp hfq, simp [formula.atoms, formula.map] at hm, cases hm, apply unified_formula.map_hco p hfp _ hm, apply unified_formula.map_hco q hfq _ hm, end | (p ∨' q) hf a hm := begin cases hf with hfp hfq, simp [formula.atoms, formula.map] at hm, cases hm, apply unified_formula.map_hco p hfp _ hm, apply unified_formula.map_hco q hfq _ hm, end | (¬' p) hf a hm := by cases hf | (∃' p) hf a hm := by cases hf lemma unified_hcso : ∀ p, nqfree p → unified p.unify := begin intros p hf, simp [unified, formula.atoms, formula.unify], constructor, apply (or.inr (or.inr rfl)), apply unified_formula.map_hco _ hf end lemma eval_sqe : ∀ {p : formula}, nqfree p → formula.wf p → ∀ {bs : list znum}, (sqe p).eval bs ↔ ∃ (b : znum), p.eval (b :: bs) := begin intros p hf hn bs, unfold sqe, rewrite eval_sqe_core_iff, apply hcso_prsv p hf hn bs, apply nqfree_unify hf, apply formula.wf_hcso hn, apply unified_hcso, apply hf end
3e5395837f8fb91014c78ace25c87ccb2aa2055c
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/src/Init/LeanInit.lean
790759176bc3a8099f22024e14b255b3e8d072e7
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
28,767
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 and Sebastian Ullrich -/ prelude import Init.Data.Option.BasicAux import Init.Data.String.Basic import Init.Data.Array.Basic import Init.Data.UInt import Init.Data.Hashable import Init.Control.Reader import Init.Control.EState import Init.Control.StateRef import Init.Control.Option namespace Lean /- Basic Lean types used to implement builtin commands and extensions. Note that this file is part of the Lean `Init` library instead of `Lean` actual implementation. The idea is to allow users to implement simple parsers, macros and tactics without importing the whole `Lean` module. It also allow us to use extensions to develop the `Init` library. -/ /- Valid identifier names -/ def isGreek (c : Char) : Bool := 0x391 ≤ c.val && c.val ≤ 0x3dd def isLetterLike (c : Char) : Bool := (0x3b1 ≤ c.val && c.val ≤ 0x3c9 && c.val ≠ 0x3bb) || -- Lower greek, but lambda (0x391 ≤ c.val && c.val ≤ 0x3A9 && c.val ≠ 0x3A0 && c.val ≠ 0x3A3) || -- Upper greek, but Pi and Sigma (0x3ca ≤ c.val && c.val ≤ 0x3fb) || -- Coptic letters (0x1f00 ≤ c.val && c.val ≤ 0x1ffe) || -- Polytonic Greek Extended Character Set (0x2100 ≤ c.val && c.val ≤ 0x214f) || -- Letter like block (0x1d49c ≤ c.val && c.val ≤ 0x1d59f) -- Latin letters, Script, Double-struck, Fractur def isSubScriptAlnum (c : Char) : Bool := (0x2080 ≤ c.val && c.val ≤ 0x2089) || -- numeric subscripts (0x2090 ≤ c.val && c.val ≤ 0x209c) || (0x1d62 ≤ c.val && c.val ≤ 0x1d6a) def isIdFirst (c : Char) : Bool := c.isAlpha || c = '_' || isLetterLike c def isIdRest (c : Char) : Bool := c.isAlphanum || c = '_' || c = '\'' || c == '!' || c == '?' || isLetterLike c || isSubScriptAlnum c def idBeginEscape := '«' def idEndEscape := '»' def isIdBeginEscape (c : Char) : Bool := c = idBeginEscape def isIdEndEscape (c : Char) : Bool := c = idEndEscape /- Hierarchical names -/ inductive Name | anonymous : Name | str : Name → String → USize → Name | num : Name → Nat → USize → Name instance Name.inhabited : Inhabited Name := ⟨Name.anonymous⟩ protected def Name.hash : Name → USize | Name.anonymous => 1723 | Name.str p s h => h | Name.num p v h => h instance Name.hashable : Hashable Name := ⟨Name.hash⟩ @[export lean_name_mk_string] def mkNameStr (p : Name) (s : String) : Name := Name.str p s $ mixHash (hash p) (hash s) @[export lean_name_mk_numeral] def mkNameNum (p : Name) (v : Nat) : Name := Name.num p v $ mixHash (hash p) (hash v) def mkNameSimple (s : String) : Name := mkNameStr Name.anonymous s namespace Name @[extern "lean_name_eq"] protected def Name.beq : (@& Name) → (@& Name) → Bool | anonymous, anonymous => true | str p₁ s₁ _, str p₂ s₂ _ => s₁ == s₂ && Name.beq p₁ p₂ | num p₁ n₁ _, num p₂ n₂ _ => n₁ == n₂ && Name.beq p₁ p₂ | _, _ => false instance : HasBeq Name := ⟨Name.beq⟩ def toStringWithSep (sep : String) : Name → String | anonymous => "[anonymous]" | str anonymous s _ => s | num anonymous v _ => toString v | str n s _ => toStringWithSep n ++ sep ++ s | num n v _ => toStringWithSep n ++ sep ++ repr v protected def toString : Name → String := toStringWithSep "." instance : HasToString Name := ⟨Name.toString⟩ protected def append : Name → Name → Name | n, anonymous => n | n, str p s _ => mkNameStr (append n p) s | n, num p d _ => mkNameNum (append n p) d instance : HasAppend Name := ⟨Name.append⟩ end Name structure NameGenerator := (namePrefix : Name := `_uniq) (idx : Nat := 1) namespace NameGenerator instance : Inhabited NameGenerator := ⟨{}⟩ @[inline] def curr (g : NameGenerator) : Name := mkNameNum g.namePrefix g.idx @[inline] def next (g : NameGenerator) : NameGenerator := { g with idx := g.idx + 1 } @[inline] def mkChild (g : NameGenerator) : NameGenerator × NameGenerator := ({ namePrefix := mkNameNum g.namePrefix g.idx, idx := 1 }, { g with idx := g.idx + 1 }) end NameGenerator class MonadNameGenerator (m : Type → Type) := (getNGen : m NameGenerator) (setNGen : NameGenerator → m Unit) export MonadNameGenerator (getNGen setNGen) def mkFreshId {m : Type → Type} [Monad m] [MonadNameGenerator m] : m Name := do ngen ← getNGen; let r := ngen.curr; setNGen ngen.next; pure r instance monadNameGeneratorLift (m n : Type → Type) [MonadNameGenerator m] [MonadLift m n] : MonadNameGenerator n := { getNGen := liftM (getNGen : m _), setNGen := fun ngen => liftM (setNGen ngen : m _) } /- Small DSL for describing parsers. We implement an interpreter for it at `Parser.lean` -/ inductive ParserDescr | andthen : ParserDescr → ParserDescr → ParserDescr | orelse : ParserDescr → ParserDescr → ParserDescr | optional : ParserDescr → ParserDescr | lookahead : ParserDescr → ParserDescr | try : ParserDescr → ParserDescr | many : ParserDescr → ParserDescr | many1 : ParserDescr → ParserDescr | sepBy : ParserDescr → ParserDescr → ParserDescr | sepBy1 : ParserDescr → ParserDescr → ParserDescr | node : Name → Nat → ParserDescr → ParserDescr | trailingNode : Name → Nat → ParserDescr → ParserDescr | symbol : String → ParserDescr | nonReservedSymbol : String → Bool → ParserDescr | numLit : ParserDescr | strLit : ParserDescr | charLit : ParserDescr | nameLit : ParserDescr | ident : ParserDescr | cat : Name → Nat → ParserDescr | parser : Name → ParserDescr instance ParserDescr.inhabited : Inhabited ParserDescr := ⟨ParserDescr.symbol ""⟩ abbrev TrailingParserDescr := ParserDescr /- Syntax -/ /-- Source information of syntax atoms. All information is generally set for unquoted syntax and unset for syntax in syntax quotations, but syntax transformations might want to invalidate only one side to make the pretty printer reformat it. In the special case of the delaborator, we also use purely synthetic position information without whitespace information. -/ structure SourceInfo := /- Will be inferred after parsing by `Syntax.updateLeading`. During parsing, it is not at all clear what the preceding token was, especially with backtracking. -/ (leading : Option Substring := none) (pos : Option String.Pos := none) (trailing : Option Substring := none) instance SourceInfo.inhabited : Inhabited SourceInfo := ⟨{}⟩ abbrev SyntaxNodeKind := Name /- Syntax AST -/ inductive Syntax | missing : Syntax | node (kind : SyntaxNodeKind) (args : Array Syntax) : Syntax | atom (info : SourceInfo) (val : String) : Syntax | ident (info : SourceInfo) (rawVal : Substring) (val : Name) (preresolved : List (Name × List String)) : Syntax instance Syntax.inhabited : Inhabited Syntax := ⟨Syntax.missing⟩ /- Builtin kinds -/ def choiceKind : SyntaxNodeKind := `choice def nullKind : SyntaxNodeKind := `null def identKind : SyntaxNodeKind := `ident def strLitKind : SyntaxNodeKind := `strLit def charLitKind : SyntaxNodeKind := `charLit def numLitKind : SyntaxNodeKind := `numLit def nameLitKind : SyntaxNodeKind := `nameLit def fieldIdxKind : SyntaxNodeKind := `fieldIdx namespace Syntax def getKind (stx : Syntax) : SyntaxNodeKind := match stx with | Syntax.node k args => k -- We use these "pseudo kinds" for antiquotation kinds. -- For example, an antiquotation `$id:ident` (using Lean.Parser.Term.ident) -- is compiled to ``if stx.isOfKind `ident ...`` | Syntax.missing => `missing | Syntax.atom _ v => mkNameSimple v | Syntax.ident _ _ _ _ => identKind def updateKind (stx : Syntax) (k : SyntaxNodeKind) : Syntax := match stx with | Syntax.node _ args => Syntax.node k args | _ => stx def isOfKind : Syntax → SyntaxNodeKind → Bool | stx, k => stx.getKind == k def getArg (stx : Syntax) (i : Nat) : Syntax := match stx with | Syntax.node _ args => args.get! i | _ => Syntax.missing -- panic! "Syntax.getArg: not a node" def getArgs (stx : Syntax) : Array Syntax := match stx with | Syntax.node _ args => args | _ => #[] -- panic! "Syntax.getArgs: not a node" /-- Retrieve the left-most leaf's info in the Syntax tree. -/ partial def getHeadInfo : Syntax → Option SourceInfo | atom info _ => info | ident info _ _ _ => info | node _ args => args.findSome? getHeadInfo | _ => none end Syntax /- Runtime support for making quotation terms auto-hygienic, by mangling identifiers introduced by them with a "macro scope" supplied by the context. Details to appear in a paper soon. -/ abbrev MacroScope := Nat /-- Macro scope used internally. It is not available for our frontend. -/ def reservedMacroScope := 0 /-- First macro scope available for our frontend -/ def firstFrontendMacroScope := reservedMacroScope + 1 /-- A monad that supports syntax quotations. Syntax quotations (in term position) are monadic values that when executed retrieve the current "macro scope" from the monad and apply it to every identifier they introduce (independent of whether this identifier turns out to be a reference to an existing declaration, or an actually fresh binding during further elaboration). -/ class MonadQuotation (m : Type → Type) := -- Get the fresh scope of the current macro invocation (getCurrMacroScope : m MacroScope) (getMainModule : m Name) /- Execute action in a new macro invocation context. This transformer should be used at all places that morally qualify as the beginning of a "macro call", e.g. `elabCommand` and `elabTerm` in the case of the elaborator. However, it can also be used internally inside a "macro" if identifiers introduced by e.g. different recursive calls should be independent and not collide. While returning an intermediate syntax tree that will recursively be expanded by the elaborator can be used for the same effect, doing direct recursion inside the macro guarded by this transformer is often easier because one is not restricted to passing a single syntax tree. Modelling this helper as a transformer and not just a monadic action ensures that the current macro scope before the recursive call is restored after it, as expected. -/ (withFreshMacroScope {α : Type} : m α → m α) export MonadQuotation /- We represent a name with macro scopes as ``` <actual name>._@.(<module_name>.<scopes>)*.<module_name>._hyg.<scopes> ``` Example: suppose the module name is `Init.Data.List.Basic`, and name is `foo.bla`, and macroscopes [2, 5] ``` foo.bla._@.Init.Data.List.Basic._hyg.2.5 ``` We may have to combine scopes from different files/modules. The main modules being processed is always the right most one. This situation may happen when we execute a macro generated in an imported file in the current file. ``` foo.bla._@.Init.Data.List.Basic.2.1.Init.Lean.Expr_hyg.4 ``` The delimiter `_hyg` is used just to improve the `hasMacroScopes` performance. -/ def Name.hasMacroScopes : Name → Bool | Name.str _ str _ => str == "_hyg" | Name.num p _ _ => Name.hasMacroScopes p | _ => false private def eraseMacroScopesAux : Name → Name | Name.str p str _ => if str == "_@" then p else eraseMacroScopesAux p | Name.num p _ _ => eraseMacroScopesAux p | Name.anonymous => unreachable! def Name.eraseMacroScopes (n : Name) : Name := if n.hasMacroScopes then eraseMacroScopesAux n else n private def simpMacroScopesAux : Name → Name | Name.num p i _ => mkNameNum (simpMacroScopesAux p) i | n => eraseMacroScopesAux n /- Helper function we use to create binder names that do not need to be unique. -/ @[export lean_simp_macro_scopes] def Name.simpMacroScopes (n : Name) : Name := if n.hasMacroScopes then simpMacroScopesAux n else n structure MacroScopesView := (name : Name) (imported : Name) (mainModule : Name) (scopes : List MacroScope) instance MacroScopesView.inhabited : Inhabited MacroScopesView := ⟨⟨arbitrary _, arbitrary _, arbitrary _, arbitrary _⟩⟩ def MacroScopesView.review (view : MacroScopesView) : Name := if view.scopes.isEmpty then view.name else let base := (mkNameStr ((mkNameStr view.name "_@") ++ view.imported ++ view.mainModule) "_hyg"); view.scopes.foldl mkNameNum base private def assembleParts : List Name → Name → Name | [], acc => acc | (Name.str _ s _) :: ps, acc => assembleParts ps (mkNameStr acc s) | (Name.num _ n _) :: ps, acc => assembleParts ps (mkNameNum acc n) | _, _ => unreachable! private def extractImported (scps : List MacroScope) (mainModule : Name) : Name → List Name → MacroScopesView | n@(Name.str p str _), parts => if str == "_@" then { name := p, mainModule := mainModule, imported := assembleParts parts Name.anonymous, scopes := scps } else extractImported p (n :: parts) | n@(Name.num p str _), parts => extractImported p (n :: parts) | _, _ => unreachable! private def extractMainModule (scps : List MacroScope) : Name → List Name → MacroScopesView | n@(Name.str p str _), parts => if str == "_@" then { name := p, mainModule := assembleParts parts Name.anonymous, imported := Name.anonymous, scopes := scps } else extractMainModule p (n :: parts) | n@(Name.num p num _), acc => extractImported scps (assembleParts acc Name.anonymous) n [] | _, _ => unreachable! private def extractMacroScopesAux : Name → List MacroScope → MacroScopesView | Name.num p scp _, acc => extractMacroScopesAux p (scp::acc) | Name.str p str _, acc => extractMainModule acc p [] -- str must be "_hyg" | _, _ => unreachable! /-- Revert all `addMacroScope` calls. `v = extractMacroScopes n → n = v.review`. This operation is useful for analyzing/transforming the original identifiers, then adding back the scopes (via `MacroScopesView.review`). -/ def extractMacroScopes (n : Name) : MacroScopesView := if n.hasMacroScopes then extractMacroScopesAux n [] else { name := n, scopes := [], imported := Name.anonymous, mainModule := Name.anonymous } def addMacroScope (mainModule : Name) (n : Name) (scp : MacroScope) : Name := if n.hasMacroScopes then let view := extractMacroScopes n; if view.mainModule == mainModule then mkNameNum n scp else { view with imported := view.scopes.foldl mkNameNum (view.imported ++ view.mainModule), mainModule := mainModule, scopes := [scp] }.review else mkNameNum (mkNameStr (mkNameStr n "_@" ++ mainModule) "_hyg") scp @[inline] def MonadQuotation.addMacroScope {m : Type → Type} [MonadQuotation m] [Monad m] (n : Name) : m Name := do mainModule ← getMainModule; scp ← getCurrMacroScope; pure $ addMacroScope mainModule n scp def defaultMaxRecDepth := 512 def maxRecDepthErrorMessage : String := "maximum recursion depth has been reached (use `set_option maxRecDepth <num>` to increase limit)" namespace Macro structure Context := (mainModule : Name) (currMacroScope : MacroScope) (currRecDepth : Nat := 0) (maxRecDepth : Nat := defaultMaxRecDepth) inductive Exception | error : Syntax → String → Exception | unsupportedSyntax : Exception end Macro abbrev MacroM := ReaderT Macro.Context (EStateM Macro.Exception MacroScope) def Macro.addMacroScope (n : Name) : MacroM Name := do ctx ← read; pure $ Lean.addMacroScope ctx.mainModule n ctx.currMacroScope def Macro.throwUnsupported {α} : MacroM α := throw Macro.Exception.unsupportedSyntax def Macro.throwError {α} (ref : Syntax) (msg : String) : MacroM α := throw $ Macro.Exception.error ref msg @[inline] protected def Macro.withFreshMacroScope {α} (x : MacroM α) : MacroM α := do fresh ← modifyGet (fun s => (s, s+1)); adaptReader (fun (ctx : Macro.Context) => { ctx with currMacroScope := fresh }) x @[inline] def Macro.withIncRecDepth {α} (ref : Syntax) (x : MacroM α) : MacroM α := do ctx ← read; when (ctx.currRecDepth == ctx.maxRecDepth) $ throw $ Macro.Exception.error ref maxRecDepthErrorMessage; adaptReader (fun (ctx : Macro.Context) => { ctx with currRecDepth := ctx.currRecDepth + 1 }) x instance MacroM.monadQuotation : MonadQuotation MacroM := { getCurrMacroScope := fun ctx => pure ctx.currMacroScope, getMainModule := fun ctx => pure ctx.mainModule, withFreshMacroScope := @Macro.withFreshMacroScope } instance monadQuotationTrans {m n : Type → Type} [MonadQuotation m] [MonadLift m n] [MonadFunctorT m m n n] : MonadQuotation n := { getCurrMacroScope := liftM (getCurrMacroScope : m MacroScope), getMainModule := liftM (getMainModule : m Name), withFreshMacroScope := fun α => monadMap (fun α => (withFreshMacroScope : m α → m α)) } abbrev Macro := Syntax → MacroM Syntax /- Helper functions for processing Syntax programmatically -/ /-- Create an identifier using `SourceInfo` from `src`. To refer to a specific constant, use `mkCIdentFrom` instead. -/ def mkIdentFrom (src : Syntax) (val : Name) : Syntax := let info := src.getHeadInfo.getD {}; Syntax.ident info (toString val).toSubstring val [] /-- Create an identifier referring to a constant `c` using `SourceInfo` from `src`. This variant of `mkIdentFrom` makes sure that the identifier cannot accidentally be captured. -/ def mkCIdentFrom (src : Syntax) (c : Name) : Syntax := let info := src.getHeadInfo.getD {}; -- Remark: We use the reserved macro scope to make sure there are no accidental collision with our frontend let id := addMacroScope `_internal c reservedMacroScope; Syntax.ident info (toString id).toSubstring id [(c, [])] def mkCIdent (c : Name) : Syntax := mkCIdentFrom Syntax.missing c def mkAtomFrom (src : Syntax) (val : String) : Syntax := let info := src.getHeadInfo.getD {}; Syntax.atom info val def Syntax.identToAtom (stx : Syntax) : Syntax := match stx with | Syntax.ident info _ val _ => Syntax.atom info val.eraseMacroScopes.toString | _ => stx @[export lean_mk_syntax_ident] def mkIdent (val : Name) : Syntax := Syntax.ident {} (toString val).toSubstring val [] @[inline] def mkNullNode (args : Array Syntax := #[]) : Syntax := Syntax.node nullKind args def mkSepStx (a : Array Syntax) (sep : Syntax) : Syntax := mkNullNode $ a.iterate #[] $ fun i a r => if i.val > 0 then (r.push sep).push a else r.push a def mkOptionalNode (arg : Option Syntax) : Syntax := match arg with | some arg => Syntax.node nullKind #[arg] | none => Syntax.node nullKind #[] /-- Create syntax representing a Lean term application -/ def mkAppStx (fn : Syntax) (args : Array Syntax) : Syntax := Syntax.node `Lean.Parser.Term.app #[fn, mkNullNode args] def mkHole (ref : Syntax) : Syntax := Syntax.node `Lean.Parser.Term.hole #[mkAtomFrom ref "_"] def mkCAppStx (fn : Name) (args : Array Syntax) : Syntax := mkAppStx (mkCIdent fn) args def mkStxLit (kind : SyntaxNodeKind) (val : String) (info : SourceInfo := {}) : Syntax := let atom : Syntax := Syntax.atom info val; Syntax.node kind #[atom] def mkStxStrLit (val : String) (info : SourceInfo := {}) : Syntax := mkStxLit strLitKind (repr val) info def mkStxNumLit (val : String) (info : SourceInfo := {}) : Syntax := mkStxLit numLitKind val info namespace Syntax /- Recall that we don't have special Syntax constructors for storing numeric and string atoms. The idea is to have an extensible approach where embedded DSLs may have new kind of atoms and/or different ways of representing them. So, our atoms contain just the parsed string. The main Lean parser uses the kind `numLitKind` for storing natural numbers that can be encoded in binary, octal, decimal and hexadecimal format. `isNatLit` implements a "decoder" for Syntax objects representing these numerals. -/ private partial def decodeBinLitAux (s : String) : String.Pos → Nat → Option Nat | i, val => if s.atEnd i then some val else let c := s.get i; if c == '0' then decodeBinLitAux (s.next i) (2*val) else if c == '1' then decodeBinLitAux (s.next i) (2*val + 1) else none private partial def decodeOctalLitAux (s : String) : String.Pos → Nat → Option Nat | i, val => if s.atEnd i then some val else let c := s.get i; if '0' ≤ c && c ≤ '7' then decodeOctalLitAux (s.next i) (8*val + c.toNat - '0'.toNat) else none private def decodeHexDigit (s : String) (i : String.Pos) : Option (Nat × String.Pos) := let c := s.get i; let i := s.next i; if '0' ≤ c && c ≤ '9' then some (c.toNat - '0'.toNat, i) else if 'a' ≤ c && c ≤ 'f' then some (10 + c.toNat - 'a'.toNat, i) else if 'A' ≤ c && c ≤ 'F' then some (10 + c.toNat - 'A'.toNat, i) else none private partial def decodeHexLitAux (s : String) : String.Pos → Nat → Option Nat | i, val => if s.atEnd i then some val else match decodeHexDigit s i with | some (d, i) => decodeHexLitAux i (16*val + d) | none => none private partial def decodeDecimalLitAux (s : String) : String.Pos → Nat → Option Nat | i, val => if s.atEnd i then some val else let c := s.get i; if '0' ≤ c && c ≤ '9' then decodeDecimalLitAux (s.next i) (10*val + c.toNat - '0'.toNat) else none def decodeNatLitVal (s : String) : Option Nat := let len := s.length; if len == 0 then none else let c := s.get 0; if c == '0' then if len == 1 then some 0 else let c := s.get 1; if c == 'x' || c == 'X' then decodeHexLitAux s 2 0 else if c == 'b' || c == 'B' then decodeBinLitAux s 2 0 else if c == 'o' || c == 'O' then decodeOctalLitAux s 2 0 else if c.isDigit then decodeDecimalLitAux s 0 0 else none else if c.isDigit then decodeDecimalLitAux s 0 0 else none def isLit? (litKind : SyntaxNodeKind) (stx : Syntax) : Option String := match stx with | Syntax.node k args => if k == litKind && args.size == 1 then match args.get! 0 with | (Syntax.atom _ val) => some val | _ => none else none | _ => none def isNatLitAux (litKind : SyntaxNodeKind) (stx : Syntax) : Option Nat := match isLit? litKind stx with | some val => decodeNatLitVal val | _ => none def isNatLit? (s : Syntax) : Option Nat := isNatLitAux numLitKind s def isFieldIdx? (s : Syntax) : Option Nat := isNatLitAux fieldIdxKind s def isIdOrAtom? : Syntax → Option String | Syntax.atom _ val => some val | Syntax.ident _ rawVal _ _ => some rawVal.toString | _ => none def toNat (stx : Syntax) : Nat := match stx.isNatLit? with | some val => val | none => 0 private def decodeQuotedChar (s : String) (i : String.Pos) : Option (Char × String.Pos) := let c := s.get i; let i := s.next i; if c == '\\' then pure ('\\', i) else if c = '\"' then pure ('\"', i) else if c = '\'' then pure ('\'', i) else if c = 'r' then pure ('\r', i) else if c = 'n' then pure ('\n', i) else if c = 't' then pure ('\t', i) else if c = 'x' then do (d₁, i) ← decodeHexDigit s i; (d₂, i) ← decodeHexDigit s i; pure (Char.ofNat (16*d₁ + d₂), i) else if c = 'u' then do (d₁, i) ← decodeHexDigit s i; (d₂, i) ← decodeHexDigit s i; (d₃, i) ← decodeHexDigit s i; (d₄, i) ← decodeHexDigit s i; pure $ (Char.ofNat (16*(16*(16*d₁ + d₂) + d₃) + d₄), i) else none partial def decodeStrLitAux (s : String) : String.Pos → String → Option String | i, acc => do let c := s.get i; let i := s.next i; if c == '\"' then pure acc else if c == '\\' then do (c, i) ← decodeQuotedChar s i; decodeStrLitAux i (acc.push c) else decodeStrLitAux i (acc.push c) def decodeStrLit (s : String) : Option String := decodeStrLitAux s 1 "" def isStrLit? (stx : Syntax) : Option String := match isLit? strLitKind stx with | some val => decodeStrLit val | _ => none def decodeCharLit (s : String) : Option Char := let c := s.get 1; if c == '\\' then do (c, _) ← decodeQuotedChar s 2; pure c else pure c def isCharLit? (stx : Syntax) : Option Char := match isLit? charLitKind stx with | some val => decodeCharLit val | _ => none private partial def decodeNameLitAux (s : String) : Nat → Name → Option Name | i, r => let continue? (i : Nat) (r : Name) : Option Name := if s.get i == '.' then decodeNameLitAux (s.next i) r else if s.atEnd i then pure r else none; let curr := s.get i; if isIdBeginEscape curr then let startPart := s.next i; let stopPart := s.nextUntil isIdEndEscape startPart; if !isIdEndEscape (s.get stopPart) then none else continue? (s.next stopPart) (mkNameStr r (s.extract startPart stopPart)) else if isIdFirst curr then let startPart := i; let stopPart := s.nextWhile isIdRest startPart; continue? stopPart (mkNameStr r (s.extract startPart stopPart)) else none def decodeNameLit (s : String) : Option Name := if s.get 0 == '`' then decodeNameLitAux s 1 Name.anonymous else none def isNameLit? (stx : Syntax) : Option Name := match isLit? nameLitKind stx with | some val => decodeNameLit val | _ => none def hasArgs : Syntax → Bool | Syntax.node _ args => args.size > 0 | _ => false def identToStrLit (stx : Syntax) : Syntax := match stx with | Syntax.ident info _ val _ => mkStxStrLit val.toString info | _ => stx def strLitToAtom (stx : Syntax) : Syntax := match stx.isStrLit? with | none => stx | some val => Syntax.atom stx.getHeadInfo.get! val def isAtom : Syntax → Bool | atom _ _ => true | _ => false def isIdent : Syntax → Bool | ident _ _ _ _ => true | _ => false def getId : Syntax → Name | ident _ _ val _ => val | _ => Name.anonymous def isNone (stx : Syntax) : Bool := match stx with | Syntax.node k args => k == nullKind && args.size == 0 | _ => false def getOptional? (stx : Syntax) : Option Syntax := match stx with | Syntax.node k args => if k == nullKind && args.size == 1 then some (args.get! 0) else none | _ => none def getOptionalIdent? (stx : Syntax) : Option Name := match stx.getOptional? with | some stx => some stx.getId | none => none partial def findAux (p : Syntax → Bool) : Syntax → Option Syntax | stx@(Syntax.node _ args) => if p stx then some stx else args.findSome? findAux | stx => if p stx then some stx else none def find? (stx : Syntax) (p : Syntax → Bool) : Option Syntax := findAux p stx end Syntax end Lean namespace Array abbrev getSepElems := @getEvenElems open Lean private partial def filterSepElemsMAux {m : Type → Type} [Monad m] (a : Array Syntax) (p : Syntax → m Bool) : Nat → Array Syntax → m (Array Syntax) | i, acc => if h : i < a.size then let stx := a.get ⟨i, h⟩; condM (p stx) (if acc.isEmpty then filterSepElemsMAux (i+2) (acc.push stx) else if hz : i ≠ 0 then have i.pred < i from Nat.predLt hz; let sepStx := a.get ⟨i.pred, Nat.ltTrans this h⟩; filterSepElemsMAux (i+2) ((acc.push sepStx).push stx) else filterSepElemsMAux (i+2) (acc.push stx)) (filterSepElemsMAux (i+2) acc) else pure acc def filterSepElemsM {m : Type → Type} [Monad m] (a : Array Syntax) (p : Syntax → m Bool) : m (Array Syntax) := filterSepElemsMAux a p 0 #[] def filterSepElems (a : Array Syntax) (p : Syntax → Bool) : Array Syntax := Id.run $ a.filterSepElemsM p private partial def mapSepElemsMAux {m : Type → Type} [Monad m] (a : Array Syntax) (f : Syntax → m Syntax) : Nat → Array Syntax → m (Array Syntax) | i, acc => if h : i < a.size then do let stx := a.get ⟨i, h⟩; if i % 2 == 0 then do stx ← f stx; mapSepElemsMAux (i+1) (acc.push stx) else mapSepElemsMAux (i+1) (acc.push stx) else pure acc def mapSepElemsM {m : Type → Type} [Monad m] (a : Array Syntax) (f : Syntax → m Syntax) : m (Array Syntax) := mapSepElemsMAux a f 0 #[] def mapSepElems (a : Array Syntax) (f : Syntax → Syntax) : Array Syntax := Id.run $ a.mapSepElemsM f end Array /-- Gadget for automatic parameter support. This is similar to the `optParam` gadget, but it uses the given tactic. Like `optParam`, this gadget only affects elaboration. For example, the tactic will *not* be invoked during type class resolution. -/ abbrev autoParam.{u} (α : Sort u) (tactic : Lean.Syntax) : Sort u := α