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
9ac38aef4b03dae4c0782f18ae03cb14fd776564
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/03_Propositions_and_Proofs.org.23.lean
74fbba8e953f2a0facd0e0ae2e1cda2ed1cb153a
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
152
lean
/- page 40 -/ import standard variables p q r: Prop -- BEGIN example (H : p ∨ q) : q ∨ p := or.elim H (λ Hp, or.inr Hp) (λ Hq, or.inl Hq) -- END
30e588efb29a257a0212f6b757000ad4fb28b7bd
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch2/ex0406.lean
40a8dda13a0162bffb46f9190d399c63c348852f
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
183
lean
def curry (α β γ : Type) (f : α × β → γ) : α → β → γ := λ a b, f (a, b) def uncurry (α β γ : Type) (f : α → β → γ) : α × β → γ := λ ab, f ab.1 ab.2
b7084f8746adb973ecf4b02fa25cfcf261782ed0
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/09_Type_Classes.org.2.lean
f920ec00892dc6a7748f54d8d50f6c0deb9ea632
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
275
lean
import standard import data.nat open nat attribute nat [class] definition one [instance] : ℕ := 1 definition foo [x : ℕ] : nat := x eval foo example : foo = 1 := rfl -- BEGIN definition two [instance] : ℕ := 2 eval foo example : foo ≠ 1 := dec_trivial -- END
3ec5eb6c6afe228fad314e6499464cb97d93d4ae
07f5f86b00fed90a419ccda4298d8b795a68f657
/library/init/logic.lean
1a0aa5d9d4f50a36a13c1e1756a916185343681c
[ "Apache-2.0" ]
permissive
VBaratham/lean
8ec5c3167b4835cfbcd7f25e2173d61ad9416b3a
450ca5834c1c35318e4b47d553bb9820c1b3eee7
refs/heads/master
1,629,649,471,814
1,512,060,373,000
1,512,060,469,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
38,619
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad, Floris van Doorn -/ prelude import init.core universes u v w @[inline] def id {α : Sort u} (a : α) : α := a def flip {α : Sort u} {β : Sort v} {φ : Sort w} (f : α → β → φ) : β → α → φ := λ b a, f a b /- implication -/ def implies (a b : Prop) := a → b @[trans] lemma implies.trans {p q r : Prop} (h₁ : implies p q) (h₂ : implies q r) : implies p r := assume hp, h₂ (h₁ hp) def trivial : true := ⟨⟩ @[inline] def absurd {a : Prop} {b : Sort v} (h₁ : a) (h₂ : ¬a) : b := false.rec b (h₂ h₁) lemma not.intro {a : Prop} (h : a → false) : ¬ a := h lemma mt {a b : Prop} (h₁ : a → b) (h₂ : ¬b) : ¬a := assume ha : a, h₂ (h₁ ha) /- not -/ lemma not_false : ¬false := id def non_contradictory (a : Prop) : Prop := ¬¬a lemma non_contradictory_intro {a : Prop} (ha : a) : ¬¬a := assume hna : ¬a, absurd ha hna /- false -/ @[inline] def false.elim {C : Sort u} (h : false) : C := false.rec C h /- eq -/ -- proof irrelevance is built in lemma proof_irrel {a : Prop} (h₁ h₂ : a) : h₁ = h₂ := rfl @[simp] lemma id.def {α : Sort u} (a : α) : id a = a := rfl @[inline] def eq.mp {α β : Sort u} : (α = β) → α → β := eq.rec_on @[inline] def eq.mpr {α β : Sort u} : (α = β) → β → α := λ h₁ h₂, eq.rec_on (eq.symm h₁) h₂ @[elab_as_eliminator] lemma eq.substr {α : Sort u} {p : α → Prop} {a b : α} (h₁ : b = a) : p a → p b := eq.subst (eq.symm h₁) lemma congr {α : Sort u} {β : Sort v} {f₁ f₂ : α → β} {a₁ a₂ : α} (h₁ : f₁ = f₂) (h₂ : a₁ = a₂) : f₁ a₁ = f₂ a₂ := eq.subst h₁ (eq.subst h₂ rfl) lemma congr_fun {α : Sort u} {β : α → Sort v} {f g : Π x, β x} (h : f = g) (a : α) : f a = g a := eq.subst h (eq.refl (f a)) lemma congr_arg {α : Sort u} {β : Sort v} {a₁ a₂ : α} (f : α → β) : a₁ = a₂ → f a₁ = f a₂ := congr rfl lemma trans_rel_left {α : Sort u} {a b c : α} (r : α → α → Prop) (h₁ : r a b) (h₂ : b = c) : r a c := h₂ ▸ h₁ lemma trans_rel_right {α : Sort u} {a b c : α} (r : α → α → Prop) (h₁ : a = b) (h₂ : r b c) : r a c := h₁.symm ▸ h₂ lemma of_eq_true {p : Prop} (h : p = true) : p := h.symm ▸ trivial lemma not_of_eq_false {p : Prop} (h : p = false) : ¬p := assume hp, h ▸ hp @[inline] def cast {α β : Sort u} (h : α = β) (a : α) : β := eq.rec a h lemma cast_proof_irrel {α β : Sort u} (h₁ h₂ : α = β) (a : α) : cast h₁ a = cast h₂ a := rfl lemma cast_eq {α : Sort u} (h : α = α) (a : α) : cast h a = a := rfl /- ne -/ @[reducible] def ne {α : Sort u} (a b : α) := ¬(a = b) notation a ≠ b := ne a b @[simp] lemma ne.def {α : Sort u} (a b : α) : a ≠ b = ¬ (a = b) := rfl namespace ne variable {α : Sort u} variables {a b : α} lemma intro (h : a = b → false) : a ≠ b := h lemma elim (h : a ≠ b) : a = b → false := h lemma irrefl (h : a ≠ a) : false := h rfl lemma symm (h : a ≠ b) : b ≠ a := assume (h₁ : b = a), h (h₁.symm) end ne lemma false_of_ne {α : Sort u} {a : α} : a ≠ a → false := ne.irrefl section variables {p : Prop} lemma ne_false_of_self : p → p ≠ false := assume (hp : p) (heq : p = false), heq ▸ hp lemma ne_true_of_not : ¬p → p ≠ true := assume (hnp : ¬p) (heq : p = true), (heq ▸ hnp) trivial lemma true_ne_false : ¬true = false := ne_false_of_self trivial end attribute [refl] heq.refl section variables {α β φ : Sort u} {a a' : α} {b b' : β} {c : φ} lemma heq.elim {α : Sort u} {a : α} {p : α → Sort v} {b : α} (h₁ : a == b) : p a → p b := eq.rec_on (eq_of_heq h₁) lemma heq.subst {p : ∀ T : Sort u, T → Prop} : a == b → p α a → p β b := heq.rec_on @[symm] lemma heq.symm (h : a == b) : b == a := heq.rec_on h (heq.refl a) lemma heq_of_eq (h : a = a') : a == a' := eq.subst h (heq.refl a) @[trans] lemma heq.trans (h₁ : a == b) (h₂ : b == c) : a == c := heq.subst h₂ h₁ @[trans] lemma heq_of_heq_of_eq (h₁ : a == b) (h₂ : b = b') : a == b' := heq.trans h₁ (heq_of_eq h₂) @[trans] lemma heq_of_eq_of_heq (h₁ : a = a') (h₂ : a' == b) : a == b := heq.trans (heq_of_eq h₁) h₂ def type_eq_of_heq (h : a == b) : α = β := heq.rec_on h (eq.refl α) end lemma eq_rec_heq {α : Sort u} {φ : α → Sort v} : ∀ {a a' : α} (h : a = a') (p : φ a), (eq.rec_on h p : φ a') == p | a _ rfl p := heq.refl p lemma heq_of_eq_rec_left {α : Sort u} {φ : α → Sort v} : ∀ {a a' : α} {p₁ : φ a} {p₂ : φ a'} (e : a = a') (h₂ : (eq.rec_on e p₁ : φ a') = p₂), p₁ == p₂ | a _ p₁ p₂ rfl h := eq.rec_on h (heq.refl p₁) lemma heq_of_eq_rec_right {α : Sort u} {φ : α → Sort v} : ∀ {a a' : α} {p₁ : φ a} {p₂ : φ a'} (e : a' = a) (h₂ : p₁ = eq.rec_on e p₂), p₁ == p₂ | a _ p₁ p₂ rfl h := have p₁ = p₂, from h, this ▸ heq.refl p₁ lemma of_heq_true {a : Prop} (h : a == true) : a := of_eq_true (eq_of_heq h) lemma eq_rec_compose : ∀ {α β φ : Sort u} (p₁ : β = φ) (p₂ : α = β) (a : α), (eq.rec_on p₁ (eq.rec_on p₂ a : β) : φ) = eq.rec_on (eq.trans p₂ p₁) a | α _ _ rfl rfl a := rfl lemma cast_heq : ∀ {α β : Sort u} (h : α = β) (a : α), cast h a == a | α _ rfl a := heq.refl a /- and -/ notation a /\ b := and a b notation a ∧ b := and a b variables {a b c d : Prop} lemma and.elim (h₁ : a ∧ b) (h₂ : a → b → c) : c := and.rec h₂ h₁ lemma and.swap : a ∧ b → b ∧ a := assume ⟨ha, hb⟩, ⟨hb, ha⟩ def and.symm := @and.swap /- or -/ notation a \/ b := or a b notation a ∨ b := or a b namespace or lemma elim (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → c) : c := or.rec h₂ h₃ h₁ end or lemma non_contradictory_em (a : Prop) : ¬¬(a ∨ ¬a) := assume not_em : ¬(a ∨ ¬a), have neg_a : ¬a, from assume pos_a : a, absurd (or.inl pos_a) not_em, absurd (or.inr neg_a) not_em def not_not_em := non_contradictory_em lemma or.swap : a ∨ b → b ∨ a := or.rec or.inr or.inl def or.symm := @or.swap /- xor -/ def xor (a b : Prop) := (a ∧ ¬ b) ∨ (b ∧ ¬ a) /- iff -/ structure iff (a b : Prop) : Prop := intro :: (mp : a → b) (mpr : b → a) notation a <-> b := iff a b notation a ↔ b := iff a b lemma iff.elim : ((a → b) → (b → a) → c) → (a ↔ b) → c := iff.rec attribute [recursor 5] iff.elim lemma iff.elim_left : (a ↔ b) → a → b := iff.mp lemma iff.elim_right : (a ↔ b) → b → a := iff.mpr lemma iff_iff_implies_and_implies (a b : Prop) : (a ↔ b) ↔ (a → b) ∧ (b → a) := iff.intro (λ h, and.intro h.mp h.mpr) (λ h, iff.intro h.left h.right) @[refl] lemma iff.refl (a : Prop) : a ↔ a := iff.intro (assume h, h) (assume h, h) lemma iff.rfl {a : Prop} : a ↔ a := iff.refl a @[trans] lemma iff.trans (h₁ : a ↔ b) (h₂ : b ↔ c) : a ↔ c := iff.intro (assume ha, iff.mp h₂ (iff.mp h₁ ha)) (assume hc, iff.mpr h₁ (iff.mpr h₂ hc)) @[symm] lemma iff.symm (h : a ↔ b) : b ↔ a := iff.intro (iff.elim_right h) (iff.elim_left h) lemma iff.comm : (a ↔ b) ↔ (b ↔ a) := iff.intro iff.symm iff.symm lemma eq.to_iff {a b : Prop} (h : a = b) : a ↔ b := eq.rec_on h iff.rfl lemma neq_of_not_iff {a b : Prop} : ¬(a ↔ b) → a ≠ b := λ h₁ h₂, have a ↔ b, from eq.subst h₂ (iff.refl a), absurd this h₁ lemma not_iff_not_of_iff (h₁ : a ↔ b) : ¬a ↔ ¬b := iff.intro (assume (hna : ¬ a) (hb : b), hna (iff.elim_right h₁ hb)) (assume (hnb : ¬ b) (ha : a), hnb (iff.elim_left h₁ ha)) lemma of_iff_true (h : a ↔ true) : a := iff.mp (iff.symm h) trivial lemma not_of_iff_false : (a ↔ false) → ¬a := iff.mp lemma iff_true_intro (h : a) : a ↔ true := iff.intro (λ hl, trivial) (λ hr, h) lemma iff_false_intro (h : ¬a) : a ↔ false := iff.intro h (false.rec a) lemma not_non_contradictory_iff_absurd (a : Prop) : ¬¬¬a ↔ ¬a := iff.intro (λ (hl : ¬¬¬a) (ha : a), hl (non_contradictory_intro ha)) absurd def not_not_not_iff := not_non_contradictory_iff_absurd lemma imp_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a → b) ↔ (c → d) := iff.intro (λ hab hc, iff.mp h₂ (hab (iff.mpr h₁ hc))) (λ hcd ha, iff.mpr h₂ (hcd (iff.mp h₁ ha))) lemma imp_congr_ctx (h₁ : a ↔ c) (h₂ : c → (b ↔ d)) : (a → b) ↔ (c → d) := iff.intro (λ hab hc, have ha : a, from iff.mpr h₁ hc, have hb : b, from hab ha, iff.mp (h₂ hc) hb) (λ hcd ha, have hc : c, from iff.mp h₁ ha, have hd : d, from hcd hc, iff.mpr (h₂ hc) hd) lemma imp_congr_right (h : a → (b ↔ c)) : (a → b) ↔ (a → c) := iff.intro (assume hab ha, iff.elim_left (h ha) (hab ha)) (assume hab ha, iff.elim_right (h ha) (hab ha)) lemma not_not_intro (ha : a) : ¬¬a := assume hna : ¬a, hna ha lemma not_of_not_not_not (h : ¬¬¬a) : ¬a := λ ha, absurd (not_not_intro ha) h @[simp] lemma not_true : (¬ true) ↔ false := iff_false_intro (not_not_intro trivial) def not_true_iff := not_true @[simp] lemma not_false_iff : (¬ false) ↔ true := iff_true_intro not_false @[congr] lemma not_congr (h : a ↔ b) : ¬a ↔ ¬b := iff.intro (λ h₁ h₂, h₁ (iff.mpr h h₂)) (λ h₁ h₂, h₁ (iff.mp h h₂)) @[simp] lemma ne_self_iff_false {α : Sort u} (a : α) : (not (a = a)) ↔ false := iff.intro false_of_ne false.elim @[simp] lemma eq_self_iff_true {α : Sort u} (a : α) : (a = a) ↔ true := iff_true_intro rfl @[simp] lemma heq_self_iff_true {α : Sort u} (a : α) : (a == a) ↔ true := iff_true_intro (heq.refl a) @[simp] lemma iff_not_self (a : Prop) : (a ↔ ¬a) ↔ false := iff_false_intro (λ h, have h' : ¬a, from (λ ha, (iff.mp h ha) ha), h' (iff.mpr h h')) @[simp] lemma not_iff_self (a : Prop) : (¬a ↔ a) ↔ false := iff_false_intro (λ h, have h' : ¬a, from (λ ha, (iff.mpr h ha) ha), h' (iff.mp h h')) @[simp] lemma true_iff_false : (true ↔ false) ↔ false := iff_false_intro (λ h, iff.mp h trivial) @[simp] lemma false_iff_true : (false ↔ true) ↔ false := iff_false_intro (λ h, iff.mpr h trivial) lemma false_of_true_iff_false : (true ↔ false) → false := assume h, iff.mp h trivial lemma false_of_true_eq_false : (true = false) → false := assume h, h ▸ trivial lemma true_eq_false_of_false : false → (true = false) := false.elim lemma eq_comm {α : Sort u} {a b : α} : a = b ↔ b = a := ⟨eq.symm, eq.symm⟩ /- and simp rules -/ lemma and.imp (hac : a → c) (hbd : b → d) : a ∧ b → c ∧ d := assume ⟨ha, hb⟩, ⟨hac ha, hbd hb⟩ def and_implies := @and.imp @[congr] lemma and_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ∧ b) ↔ (c ∧ d) := iff.intro (and.imp (iff.mp h₁) (iff.mp h₂)) (and.imp (iff.mpr h₁) (iff.mpr h₂)) lemma and_congr_right (h : a → (b ↔ c)) : (a ∧ b) ↔ (a ∧ c) := iff.intro (assume ⟨ha, hb⟩, ⟨ha, iff.elim_left (h ha) hb⟩) (assume ⟨ha, hc⟩, ⟨ha, iff.elim_right (h ha) hc⟩) @[simp] lemma and.comm : a ∧ b ↔ b ∧ a := iff.intro and.swap and.swap lemma and_comm (a b : Prop) : a ∧ b ↔ b ∧ a := and.comm @[simp] lemma and.assoc : (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) := iff.intro (assume ⟨⟨ha, hb⟩, hc⟩, ⟨ha, ⟨hb, hc⟩⟩) (assume ⟨ha, ⟨hb, hc⟩⟩, ⟨⟨ha, hb⟩, hc⟩) lemma and_assoc (a b : Prop) : (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) := and.assoc @[simp] lemma and.left_comm : a ∧ (b ∧ c) ↔ b ∧ (a ∧ c) := iff.trans (iff.symm and.assoc) (iff.trans (and_congr and.comm (iff.refl c)) and.assoc) lemma and_iff_left {a b : Prop} (hb : b) : (a ∧ b) ↔ a := iff.intro and.left (λ ha, ⟨ha, hb⟩) lemma and_iff_right {a b : Prop} (ha : a) : (a ∧ b) ↔ b := iff.intro and.right (and.intro ha) @[simp] lemma and_true (a : Prop) : a ∧ true ↔ a := and_iff_left trivial @[simp] lemma true_and (a : Prop) : true ∧ a ↔ a := and_iff_right trivial @[simp] lemma and_false (a : Prop) : a ∧ false ↔ false := iff_false_intro and.right @[simp] lemma false_and (a : Prop) : false ∧ a ↔ false := iff_false_intro and.left @[simp] lemma not_and_self (a : Prop) : (¬a ∧ a) ↔ false := iff_false_intro (λ h, and.elim h (λ h₁ h₂, absurd h₂ h₁)) @[simp] lemma and_not_self (a : Prop) : (a ∧ ¬a) ↔ false := iff_false_intro (assume ⟨h₁, h₂⟩, absurd h₁ h₂) @[simp] lemma and_self (a : Prop) : a ∧ a ↔ a := iff.intro and.left (assume h, ⟨h, h⟩) /- or simp rules -/ lemma or.imp (h₂ : a → c) (h₃ : b → d) : a ∨ b → c ∨ d := or.rec (λ h, or.inl (h₂ h)) (λ h, or.inr (h₃ h)) lemma or.imp_left (h : a → b) : a ∨ c → b ∨ c := or.imp h id lemma or.imp_right (h : a → b) : c ∨ a → c ∨ b := or.imp id h @[congr] lemma or_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ∨ b) ↔ (c ∨ d) := iff.intro (or.imp (iff.mp h₁) (iff.mp h₂)) (or.imp (iff.mpr h₁) (iff.mpr h₂)) @[simp] lemma or.comm : a ∨ b ↔ b ∨ a := iff.intro or.swap or.swap lemma or_comm (a b : Prop) : a ∨ b ↔ b ∨ a := or.comm @[simp] lemma or.assoc : (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) := iff.intro (or.rec (or.imp_right or.inl) (λ h, or.inr (or.inr h))) (or.rec (λ h, or.inl (or.inl h)) (or.imp_left or.inr)) lemma or_assoc (a b : Prop) : (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) := or.assoc @[simp] lemma or.left_comm : a ∨ (b ∨ c) ↔ b ∨ (a ∨ c) := iff.trans (iff.symm or.assoc) (iff.trans (or_congr or.comm (iff.refl c)) or.assoc) theorem or_iff_right_of_imp (ha : a → b) : (a ∨ b) ↔ b := iff.intro (or.rec ha id) or.inr theorem or_iff_left_of_imp (hb : b → a) : (a ∨ b) ↔ a := iff.intro (or.rec id hb) or.inl @[simp] lemma or_true (a : Prop) : a ∨ true ↔ true := iff_true_intro (or.inr trivial) @[simp] lemma true_or (a : Prop) : true ∨ a ↔ true := iff_true_intro (or.inl trivial) @[simp] lemma or_false (a : Prop) : a ∨ false ↔ a := iff.intro (or.rec id false.elim) or.inl @[simp] lemma false_or (a : Prop) : false ∨ a ↔ a := iff.trans or.comm (or_false a) @[simp] lemma or_self (a : Prop) : a ∨ a ↔ a := iff.intro (or.rec id id) or.inl lemma not_or {a b : Prop} : ¬ a → ¬ b → ¬ (a ∨ b) | hna hnb (or.inl ha) := absurd ha hna | hna hnb (or.inr hb) := absurd hb hnb /- or resolution rulse -/ def or.resolve_left {a b : Prop} (h : a ∨ b) (na : ¬ a) : b := or.elim h (λ ha, absurd ha na) id def or.neg_resolve_left {a b : Prop} (h : ¬ a ∨ b) (ha : a) : b := or.elim h (λ na, absurd ha na) id def or.resolve_right {a b : Prop} (h : a ∨ b) (nb : ¬ b) : a := or.elim h id (λ hb, absurd hb nb) def or.neg_resolve_right {a b : Prop} (h : a ∨ ¬ b) (hb : b) : a := or.elim h id (λ nb, absurd hb nb) /- iff simp rules -/ @[simp] lemma iff_true (a : Prop) : (a ↔ true) ↔ a := iff.intro (assume h, iff.mpr h trivial) iff_true_intro @[simp] lemma true_iff (a : Prop) : (true ↔ a) ↔ a := iff.trans iff.comm (iff_true a) @[simp] lemma iff_false (a : Prop) : (a ↔ false) ↔ ¬ a := iff.intro iff.mp iff_false_intro @[simp] lemma false_iff (a : Prop) : (false ↔ a) ↔ ¬ a := iff.trans iff.comm (iff_false a) @[simp] lemma iff_self (a : Prop) : (a ↔ a) ↔ true := iff_true_intro iff.rfl @[congr] lemma iff_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ↔ b) ↔ (c ↔ d) := (iff_iff_implies_and_implies a b).trans ((and_congr (imp_congr h₁ h₂) (imp_congr h₂ h₁)).trans (iff_iff_implies_and_implies c d).symm) /- implies simp rule -/ @[simp] lemma implies_true_iff (α : Sort u) : (α → true) ↔ true := iff.intro (λ h, trivial) (λ ha h, trivial) @[simp] lemma false_implies_iff (a : Prop) : (false → a) ↔ true := iff.intro (λ h, trivial) (λ ha h, false.elim h) @[simp] theorem true_implies_iff (α : Prop) : (true → α) ↔ α := iff.intro (λ h, h trivial) (λ h h', h) /- exists -/ inductive Exists {α : Sort u} (p : α → Prop) : Prop | intro : ∀ (a : α), p a → Exists attribute [intro] Exists.intro @[pattern] def exists.intro := @Exists.intro notation `exists` binders `, ` r:(scoped P, Exists P) := r notation `∃` binders `, ` r:(scoped P, Exists P) := r lemma exists.elim {α : Sort u} {p : α → Prop} {b : Prop} (h₁ : ∃ x, p x) (h₂ : ∀ (a : α), p a → b) : b := Exists.rec h₂ h₁ /- exists unique -/ def exists_unique {α : Sort u} (p : α → Prop) := ∃ x, p x ∧ ∀ y, p y → y = x notation `∃!` binders `, ` r:(scoped P, exists_unique P) := r @[intro] lemma exists_unique.intro {α : Sort u} {p : α → Prop} (w : α) (h₁ : p w) (h₂ : ∀ y, p y → y = w) : ∃! x, p x := exists.intro w ⟨h₁, h₂⟩ attribute [recursor 4] lemma exists_unique.elim {α : Sort u} {p : α → Prop} {b : Prop} (h₂ : ∃! x, p x) (h₁ : ∀ x, p x → (∀ y, p y → y = x) → b) : b := exists.elim h₂ (λ w hw, h₁ w (and.left hw) (and.right hw)) lemma exists_unique_of_exists_of_unique {α : Type u} {p : α → Prop} (hex : ∃ x, p x) (hunique : ∀ y₁ y₂, p y₁ → p y₂ → y₁ = y₂) : ∃! x, p x := exists.elim hex (λ x px, exists_unique.intro x px (assume y, assume : p y, hunique y x this px)) lemma exists_of_exists_unique {α : Sort u} {p : α → Prop} (h : ∃! x, p x) : ∃ x, p x := exists.elim h (λ x hx, ⟨x, and.left hx⟩) lemma unique_of_exists_unique {α : Sort u} {p : α → Prop} (h : ∃! x, p x) {y₁ y₂ : α} (py₁ : p y₁) (py₂ : p y₂) : y₁ = y₂ := exists_unique.elim h (assume x, assume : p x, assume unique : ∀ y, p y → y = x, show y₁ = y₂, from eq.trans (unique _ py₁) (eq.symm (unique _ py₂))) /- exists, forall, exists unique congruences -/ @[congr] lemma forall_congr {α : Sort u} {p q : α → Prop} (h : ∀ a, (p a ↔ q a)) : (∀ a, p a) ↔ ∀ a, q a := iff.intro (λ p a, iff.mp (h a) (p a)) (λ q a, iff.mpr (h a) (q a)) lemma exists_imp_exists {α : Sort u} {p q : α → Prop} (h : ∀ a, (p a → q a)) (p : ∃ a, p a) : ∃ a, q a := exists.elim p (λ a hp, ⟨a, h a hp⟩) @[congr] lemma exists_congr {α : Sort u} {p q : α → Prop} (h : ∀ a, (p a ↔ q a)) : (Exists p) ↔ ∃ a, q a := iff.intro (exists_imp_exists (λ a, iff.mp (h a))) (exists_imp_exists (λ a, iff.mpr (h a))) @[congr] lemma exists_unique_congr {α : Sort u} {p₁ p₂ : α → Prop} (h : ∀ x, p₁ x ↔ p₂ x) : (exists_unique p₁) ↔ (∃! x, p₂ x) := -- exists_congr (λ x, and_congr (h x) (forall_congr (λ y, imp_congr (h y) iff.rfl))) lemma forall_not_of_not_exists {α : Sort u} {p : α → Prop} : ¬(∃ x, p x) → (∀ x, ¬p x) := λ hne x hp, hne ⟨x, hp⟩ /- decidable -/ def decidable.to_bool (p : Prop) [h : decidable p] : bool := decidable.cases_on h (λ h₁, bool.ff) (λ h₂, bool.tt) export decidable (is_true is_false to_bool) instance decidable.true : decidable true := is_true trivial instance decidable.false : decidable false := is_false not_false -- We use "dependent" if-then-else to be able to communicate the if-then-else condition -- to the branches @[inline] def dite (c : Prop) [h : decidable c] {α : Sort u} : (c → α) → (¬ c → α) → α := λ t e, decidable.rec_on h e t /- if-then-else -/ @[inline] def ite (c : Prop) [h : decidable c] {α : Sort u} (t e : α) : α := decidable.rec_on h (λ hnc, e) (λ hc, t) namespace decidable variables {p q : Prop} def rec_on_true [h : decidable p] {h₁ : p → Sort u} {h₂ : ¬p → Sort u} (h₃ : p) (h₄ : h₁ h₃) : decidable.rec_on h h₂ h₁ := decidable.rec_on h (λ h, false.rec _ (h h₃)) (λ h, h₄) def rec_on_false [h : decidable p] {h₁ : p → Sort u} {h₂ : ¬p → Sort u} (h₃ : ¬p) (h₄ : h₂ h₃) : decidable.rec_on h h₂ h₁ := decidable.rec_on h (λ h, h₄) (λ h, false.rec _ (h₃ h)) def by_cases {q : Sort u} [φ : decidable p] : (p → q) → (¬p → q) → q := dite _ lemma em (p : Prop) [decidable p] : p ∨ ¬p := by_cases or.inl or.inr lemma by_contradiction [decidable p] (h : ¬p → false) : p := if h₁ : p then h₁ else false.rec _ (h h₁) lemma of_not_not [decidable p] : ¬ ¬ p → p := λ hnn, by_contradiction (λ hn, absurd hn hnn) lemma not_not_iff (p) [decidable p] : (¬ ¬ p) ↔ p := iff.intro of_not_not not_not_intro lemma not_and_iff_or_not (p q : Prop) [d₁ : decidable p] [d₂ : decidable q] : ¬ (p ∧ q) ↔ ¬ p ∨ ¬ q := iff.intro (λ h, match d₁ with | is_true h₁ := match d₂ with | is_true h₂ := absurd (and.intro h₁ h₂) h | is_false h₂ := or.inr h₂ end | is_false h₁ := or.inl h₁ end) (λ h ⟨hp, hq⟩, or.elim h (λ h, h hp) (λ h, h hq)) lemma not_or_iff_and_not (p q) [d₁ : decidable p] [d₂ : decidable q] : ¬ (p ∨ q) ↔ ¬ p ∧ ¬ q := iff.intro (λ h, match d₁ with | is_true h₁ := false.elim $ h (or.inl h₁) | is_false h₁ := match d₂ with | is_true h₂ := false.elim $ h (or.inr h₂) | is_false h₂ := ⟨h₁, h₂⟩ end end) (λ ⟨np, nq⟩ h, or.elim h np nq) end decidable section variables {p q : Prop} def decidable_of_decidable_of_iff (hp : decidable p) (h : p ↔ q) : decidable q := if hp : p then is_true (iff.mp h hp) else is_false (iff.mp (not_iff_not_of_iff h) hp) def decidable_of_decidable_of_eq (hp : decidable p) (h : p = q) : decidable q := decidable_of_decidable_of_iff hp h.to_iff protected def or.by_cases [decidable p] [decidable q] {α : Sort u} (h : p ∨ q) (h₁ : p → α) (h₂ : q → α) : α := if hp : p then h₁ hp else if hq : q then h₂ hq else false.rec _ (or.elim h hp hq) end section variables {p q : Prop} instance [decidable p] [decidable q] : decidable (p ∧ q) := if hp : p then if hq : q then is_true ⟨hp, hq⟩ else is_false (assume h : p ∧ q, hq (and.right h)) else is_false (assume h : p ∧ q, hp (and.left h)) instance [decidable p] [decidable q] : decidable (p ∨ q) := if hp : p then is_true (or.inl hp) else if hq : q then is_true (or.inr hq) else is_false (or.rec hp hq) instance [decidable p] : decidable (¬p) := if hp : p then is_false (absurd hp) else is_true hp instance implies.decidable [decidable p] [decidable q] : decidable (p → q) := if hp : p then if hq : q then is_true (assume h, hq) else is_false (assume h : p → q, absurd (h hp) hq) else is_true (assume h, absurd h hp) instance [decidable p] [decidable q] : decidable (p ↔ q) := if hp : p then if hq : q then is_true ⟨λ_, hq, λ_, hp⟩ else is_false $ λh, hq (h.1 hp) else if hq : q then is_false $ λh, hp (h.2 hq) else is_true $ ⟨λh, absurd h hp, λh, absurd h hq⟩ instance [decidable p] [decidable q] : decidable (xor p q) := if hp : p then if hq : q then is_false (or.rec (λ ⟨_, h⟩, h hq : ¬(p ∧ ¬ q)) (λ ⟨_, h⟩, h hp : ¬(q ∧ ¬ p))) else is_true $ or.inl ⟨hp, hq⟩ else if hq : q then is_true $ or.inr ⟨hq, hp⟩ else is_false (or.rec (λ ⟨h, _⟩, hp h : ¬(p ∧ ¬ q)) (λ ⟨h, _⟩, hq h : ¬(q ∧ ¬ p))) instance exists_prop_decidable {p} (P : p → Prop) [Dp : decidable p] [DP : ∀ h, decidable (P h)] : decidable (∃ h, P h) := if h : p then decidable_of_decidable_of_iff (DP h) ⟨λ h2, ⟨h, h2⟩, λ⟨h', h2⟩, h2⟩ else is_false (mt (λ⟨h, _⟩, h) h) instance forall_prop_decidable {p} (P : p → Prop) [Dp : decidable p] [DP : ∀ h, decidable (P h)] : decidable (∀ h, P h) := if h : p then decidable_of_decidable_of_iff (DP h) ⟨λ h2 _, h2, λal, al h⟩ else is_true (λ h2, absurd h2 h) end instance {α : Sort u} [decidable_eq α] (a b : α) : decidable (a ≠ b) := implies.decidable lemma bool.ff_ne_tt : ff = tt → false . def is_dec_eq {α : Sort u} (p : α → α → bool) : Prop := ∀ ⦃x y : α⦄, p x y = tt → x = y def is_dec_refl {α : Sort u} (p : α → α → bool) : Prop := ∀ x, p x x = tt open decidable instance : decidable_eq bool | ff ff := is_true rfl | ff tt := is_false bool.ff_ne_tt | tt ff := is_false (ne.symm bool.ff_ne_tt) | tt tt := is_true rfl def decidable_eq_of_bool_pred {α : Sort u} {p : α → α → bool} (h₁ : is_dec_eq p) (h₂ : is_dec_refl p) : decidable_eq α := assume x y : α, if hp : p x y = tt then is_true (h₁ hp) else is_false (assume hxy : x = y, absurd (h₂ y) (@eq.rec_on _ _ (λ z, ¬p z y = tt) _ hxy hp)) lemma decidable_eq_inl_refl {α : Sort u} [h : decidable_eq α] (a : α) : h a a = is_true (eq.refl a) := match (h a a) with | (is_true e) := rfl | (is_false n) := absurd rfl n end lemma decidable_eq_inr_neg {α : Sort u} [h : decidable_eq α] {a b : α} : Π n : a ≠ b, h a b = is_false n := assume n, match (h a b) with | (is_true e) := absurd e n | (is_false n₁) := proof_irrel n n₁ ▸ eq.refl (is_false n) end /- inhabited -/ class inhabited (α : Sort u) := (default : α) def default (α : Sort u) [inhabited α] : α := inhabited.default α @[inline, irreducible] def arbitrary (α : Sort u) [inhabited α] : α := default α instance prop.inhabited : inhabited Prop := ⟨true⟩ instance fun.inhabited (α : Sort u) {β : Sort v} [h : inhabited β] : inhabited (α → β) := inhabited.rec_on h (λ b, ⟨λ a, b⟩) instance pi.inhabited (α : Sort u) {β : α → Sort v} [Π x, inhabited (β x)] : inhabited (Π x, β x) := ⟨λ a, default (β a)⟩ instance : inhabited bool := ⟨ff⟩ instance : inhabited true := ⟨trivial⟩ class inductive nonempty (α : Sort u) : Prop | intro : α → nonempty protected def nonempty.elim {α : Sort u} {p : Prop} (h₁ : nonempty α) (h₂ : α → p) : p := nonempty.rec h₂ h₁ instance nonempty_of_inhabited {α : Sort u} [inhabited α] : nonempty α := ⟨default α⟩ lemma nonempty_of_exists {α : Sort u} {p : α → Prop} : (∃ x, p x) → nonempty α | ⟨w, h⟩ := ⟨w⟩ /- subsingleton -/ class inductive subsingleton (α : Sort u) : Prop | intro : (∀ a b : α, a = b) → subsingleton protected def subsingleton.elim {α : Sort u} [h : subsingleton α] : ∀ (a b : α), a = b := subsingleton.rec (λ p, p) h protected def subsingleton.helim {α β : Sort u} [h : subsingleton α] (h : α = β) : ∀ (a : α) (b : β), a == b := eq.rec_on h (λ a b : α, heq_of_eq (subsingleton.elim a b)) instance subsingleton_prop (p : Prop) : subsingleton p := ⟨λ a b, proof_irrel a b⟩ instance (p : Prop) : subsingleton (decidable p) := subsingleton.intro (λ d₁, match d₁ with | (is_true t₁) := (λ d₂, match d₂ with | (is_true t₂) := eq.rec_on (proof_irrel t₁ t₂) rfl | (is_false f₂) := absurd t₁ f₂ end) | (is_false f₁) := (λ d₂, match d₂ with | (is_true t₂) := absurd t₂ f₁ | (is_false f₂) := eq.rec_on (proof_irrel f₁ f₂) rfl end) end) protected lemma rec_subsingleton {p : Prop} [h : decidable p] {h₁ : p → Sort u} {h₂ : ¬p → Sort u} [h₃ : Π (h : p), subsingleton (h₁ h)] [h₄ : Π (h : ¬p), subsingleton (h₂ h)] : subsingleton (decidable.rec_on h h₂ h₁) := match h with | (is_true h) := h₃ h | (is_false h) := h₄ h end lemma if_pos {c : Prop} [h : decidable c] (hc : c) {α : Sort u} {t e : α} : (ite c t e) = t := match h with | (is_true hc) := rfl | (is_false hnc) := absurd hc hnc end lemma if_neg {c : Prop} [h : decidable c] (hnc : ¬c) {α : Sort u} {t e : α} : (ite c t e) = e := match h with | (is_true hc) := absurd hc hnc | (is_false hnc) := rfl end @[simp] lemma if_t_t (c : Prop) [h : decidable c] {α : Sort u} (t : α) : (ite c t t) = t := match h with | (is_true hc) := rfl | (is_false hnc) := rfl end lemma implies_of_if_pos {c t e : Prop} [decidable c] (h : ite c t e) : c → t := assume hc, eq.rec_on (if_pos hc : ite c t e = t) h lemma implies_of_if_neg {c t e : Prop} [decidable c] (h : ite c t e) : ¬c → e := assume hnc, eq.rec_on (if_neg hnc : ite c t e = e) h lemma if_ctx_congr {α : Sort u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c] {x y u v : α} (h_c : b ↔ c) (h_t : c → x = u) (h_e : ¬c → y = v) : ite b x y = ite c u v := match dec_b, dec_c with | (is_false h₁), (is_false h₂) := h_e h₂ | (is_true h₁), (is_true h₂) := h_t h₂ | (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁) | (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂) end @[congr] lemma if_congr {α : Sort u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c] {x y u v : α} (h_c : b ↔ c) (h_t : x = u) (h_e : y = v) : ite b x y = ite c u v := @if_ctx_congr α b c dec_b dec_c x y u v h_c (λ h, h_t) (λ h, h_e) lemma if_ctx_simp_congr {α : Sort u} {b c : Prop} [dec_b : decidable b] {x y u v : α} (h_c : b ↔ c) (h_t : c → x = u) (h_e : ¬c → y = v) : ite b x y = (@ite c (decidable_of_decidable_of_iff dec_b h_c) α u v) := @if_ctx_congr α b c dec_b (decidable_of_decidable_of_iff dec_b h_c) x y u v h_c h_t h_e @[congr] lemma if_simp_congr {α : Sort u} {b c : Prop} [dec_b : decidable b] {x y u v : α} (h_c : b ↔ c) (h_t : x = u) (h_e : y = v) : ite b x y = (@ite c (decidable_of_decidable_of_iff dec_b h_c) α u v) := @if_ctx_simp_congr α b c dec_b x y u v h_c (λ h, h_t) (λ h, h_e) @[simp] lemma if_true {α : Sort u} {h : decidable true} (t e : α) : (@ite true h α t e) = t := if_pos trivial @[simp] lemma if_false {α : Sort u} {h : decidable false} (t e : α) : (@ite false h α t e) = e := if_neg not_false lemma if_ctx_congr_prop {b c x y u v : Prop} [dec_b : decidable b] [dec_c : decidable c] (h_c : b ↔ c) (h_t : c → (x ↔ u)) (h_e : ¬c → (y ↔ v)) : ite b x y ↔ ite c u v := match dec_b, dec_c with | (is_false h₁), (is_false h₂) := h_e h₂ | (is_true h₁), (is_true h₂) := h_t h₂ | (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁) | (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂) end @[congr] lemma if_congr_prop {b c x y u v : Prop} [dec_b : decidable b] [dec_c : decidable c] (h_c : b ↔ c) (h_t : x ↔ u) (h_e : y ↔ v) : ite b x y ↔ ite c u v := if_ctx_congr_prop h_c (λ h, h_t) (λ h, h_e) lemma if_ctx_simp_congr_prop {b c x y u v : Prop} [dec_b : decidable b] (h_c : b ↔ c) (h_t : c → (x ↔ u)) (h_e : ¬c → (y ↔ v)) : ite b x y ↔ (@ite c (decidable_of_decidable_of_iff dec_b h_c) Prop u v) := @if_ctx_congr_prop b c x y u v dec_b (decidable_of_decidable_of_iff dec_b h_c) h_c h_t h_e @[congr] lemma if_simp_congr_prop {b c x y u v : Prop} [dec_b : decidable b] (h_c : b ↔ c) (h_t : x ↔ u) (h_e : y ↔ v) : ite b x y ↔ (@ite c (decidable_of_decidable_of_iff dec_b h_c) Prop u v) := @if_ctx_simp_congr_prop b c x y u v dec_b h_c (λ h, h_t) (λ h, h_e) @[simp] lemma dif_pos {c : Prop} [h : decidable c] (hc : c) {α : Sort u} {t : c → α} {e : ¬ c → α} : dite c t e = t hc := match h with | (is_true hc) := rfl | (is_false hnc) := absurd hc hnc end @[simp] lemma dif_neg {c : Prop} [h : decidable c] (hnc : ¬c) {α : Sort u} {t : c → α} {e : ¬ c → α} : dite c t e = e hnc := match h with | (is_true hc) := absurd hc hnc | (is_false hnc) := rfl end lemma dif_ctx_congr {α : Sort u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c] {x : b → α} {u : c → α} {y : ¬b → α} {v : ¬c → α} (h_c : b ↔ c) (h_t : ∀ (h : c), x (iff.mpr h_c h) = u h) (h_e : ∀ (h : ¬c), y (iff.mpr (not_iff_not_of_iff h_c) h) = v h) : (@dite b dec_b α x y) = (@dite c dec_c α u v) := match dec_b, dec_c with | (is_false h₁), (is_false h₂) := h_e h₂ | (is_true h₁), (is_true h₂) := h_t h₂ | (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁) | (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂) end lemma dif_ctx_simp_congr {α : Sort u} {b c : Prop} [dec_b : decidable b] {x : b → α} {u : c → α} {y : ¬b → α} {v : ¬c → α} (h_c : b ↔ c) (h_t : ∀ (h : c), x (iff.mpr h_c h) = u h) (h_e : ∀ (h : ¬c), y (iff.mpr (not_iff_not_of_iff h_c) h) = v h) : (@dite b dec_b α x y) = (@dite c (decidable_of_decidable_of_iff dec_b h_c) α u v) := @dif_ctx_congr α b c dec_b (decidable_of_decidable_of_iff dec_b h_c) x u y v h_c h_t h_e -- Remark: dite and ite are "defally equal" when we ignore the proofs. lemma dif_eq_if (c : Prop) [h : decidable c] {α : Sort u} (t : α) (e : α) : dite c (λ h, t) (λ h, e) = ite c t e := match h with | (is_true hc) := rfl | (is_false hnc) := rfl end instance {c t e : Prop} [d_c : decidable c] [d_t : decidable t] [d_e : decidable e] : decidable (if c then t else e) := match d_c with | (is_true hc) := d_t | (is_false hc) := d_e end instance {c : Prop} {t : c → Prop} {e : ¬c → Prop} [d_c : decidable c] [d_t : ∀ h, decidable (t h)] [d_e : ∀ h, decidable (e h)] : decidable (if h : c then t h else e h) := match d_c with | (is_true hc) := d_t hc | (is_false hc) := d_e hc end def as_true (c : Prop) [decidable c] : Prop := if c then true else false def as_false (c : Prop) [decidable c] : Prop := if c then false else true def of_as_true {c : Prop} [h₁ : decidable c] (h₂ : as_true c) : c := match h₁, h₂ with | (is_true h_c), h₂ := h_c | (is_false h_c), h₂ := false.elim h₂ end /-- Universe lifting operation -/ structure {r s} ulift (α : Type s) : Type (max s r) := up :: (down : α) namespace ulift /- Bijection between α and ulift.{v} α -/ lemma up_down {α : Type u} : ∀ (b : ulift.{v} α), up (down b) = b | (up a) := rfl lemma down_up {α : Type u} (a : α) : down (up.{v} a) = a := rfl end ulift /-- Universe lifting operation from Sort to Type -/ structure plift (α : Sort u) : Type u := up :: (down : α) namespace plift /- Bijection between α and plift α -/ lemma up_down {α : Sort u} : ∀ (b : plift α), up (down b) = b | (up a) := rfl lemma down_up {α : Sort u} (a : α) : down (up a) = a := rfl end plift /- Equalities for rewriting let-expressions -/ lemma let_value_eq {α : Sort u} {β : Sort v} {a₁ a₂ : α} (b : α → β) : a₁ = a₂ → (let x : α := a₁ in b x) = (let x : α := a₂ in b x) := λ h, eq.rec_on h rfl lemma let_value_heq {α : Sort v} {β : α → Sort u} {a₁ a₂ : α} (b : Π x : α, β x) : a₁ = a₂ → (let x : α := a₁ in b x) == (let x : α := a₂ in b x) := λ h, eq.rec_on h (heq.refl (b a₁)) lemma let_body_eq {α : Sort v} {β : α → Sort u} (a : α) {b₁ b₂ : Π x : α, β x} : (∀ x, b₁ x = b₂ x) → (let x : α := a in b₁ x) = (let x : α := a in b₂ x) := λ h, h a lemma let_eq {α : Sort v} {β : Sort u} {a₁ a₂ : α} {b₁ b₂ : α → β} : a₁ = a₂ → (∀ x, b₁ x = b₂ x) → (let x : α := a₁ in b₁ x) = (let x : α := a₂ in b₂ x) := λ h₁ h₂, eq.rec_on h₁ (h₂ a₁) section relation variables {α : Sort u} {β : Sort v} (r : β → β → Prop) local infix `≺`:50 := r def reflexive := ∀ x, x ≺ x def symmetric := ∀ ⦃x y⦄, x ≺ y → y ≺ x def transitive := ∀ ⦃x y z⦄, x ≺ y → y ≺ z → x ≺ z def equivalence := reflexive r ∧ symmetric r ∧ transitive r def total := ∀ x y, x ≺ y ∨ y ≺ x def mk_equivalence (rfl : reflexive r) (symm : symmetric r) (trans : transitive r) : equivalence r := ⟨rfl, symm, trans⟩ def irreflexive := ∀ x, ¬ x ≺ x def anti_symmetric := ∀ ⦃x y⦄, x ≺ y → y ≺ x → x = y def empty_relation := λ a₁ a₂ : α, false def subrelation (q r : β → β → Prop) := ∀ ⦃x y⦄, q x y → r x y def inv_image (f : α → β) : α → α → Prop := λ a₁ a₂, f a₁ ≺ f a₂ lemma inv_image.trans (f : α → β) (h : transitive r) : transitive (inv_image r f) := λ (a₁ a₂ a₃ : α) (h₁ : inv_image r f a₁ a₂) (h₂ : inv_image r f a₂ a₃), h h₁ h₂ lemma inv_image.irreflexive (f : α → β) (h : irreflexive r) : irreflexive (inv_image r f) := λ (a : α) (h₁ : inv_image r f a a), h (f a) h₁ inductive tc {α : Sort u} (r : α → α → Prop) : α → α → Prop | base : ∀ a b, r a b → tc a b | trans : ∀ a b c, tc a b → tc b c → tc a c end relation section binary variables {α : Type u} {β : Type v} variable f : α → α → α variable inv : α → α variable one : α local notation a * b := f a b local notation a ⁻¹ := inv a variable g : α → α → α local notation a + b := g a b def commutative := ∀ a b, a * b = b * a def associative := ∀ a b c, (a * b) * c = a * (b * c) def left_identity := ∀ a, one * a = a def right_identity := ∀ a, a * one = a def right_inverse := ∀ a, a * a⁻¹ = one def left_cancelative := ∀ a b c, a * b = a * c → b = c def right_cancelative := ∀ a b c, a * b = c * b → a = c def left_distributive := ∀ a b c, a * (b + c) = a * b + a * c def right_distributive := ∀ a b c, (a + b) * c = a * c + b * c def right_commutative (h : β → α → β) := ∀ b a₁ a₂, h (h b a₁) a₂ = h (h b a₂) a₁ def left_commutative (h : α → β → β) := ∀ a₁ a₂ b, h a₁ (h a₂ b) = h a₂ (h a₁ b) lemma left_comm : commutative f → associative f → left_commutative f := assume hcomm hassoc, assume a b c, calc a*(b*c) = (a*b)*c : eq.symm (hassoc a b c) ... = (b*a)*c : hcomm a b ▸ rfl ... = b*(a*c) : hassoc b a c lemma right_comm : commutative f → associative f → right_commutative f := assume hcomm hassoc, assume a b c, calc (a*b)*c = a*(b*c) : hassoc a b c ... = a*(c*b) : hcomm b c ▸ rfl ... = (a*c)*b : eq.symm (hassoc a c b) end binary
2c20136b17bac956355d473affcfeb3152f21449
c777c32c8e484e195053731103c5e52af26a25d1
/src/topology/instances/discrete.lean
3bb76457acdbccb08192e863af1cdb7de1c92736
[ "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
5,272
lean
/- Copyright (c) 2022 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import order.succ_pred.basic import topology.order.basic import topology.metric_space.metrizable_uniformity /-! # Instances related to the discrete topology We prove that the discrete topology is * first-countable, * second-countable for an encodable type, * equal to the order topology in linear orders which are also `pred_order` and `succ_order`, * metrizable. When importing this file and `data.nat.succ_pred`, the instances `second_countable_topology ℕ` and `order_topology ℕ` become available. -/ open order set topological_space filter variables {α : Type*} [topological_space α] @[priority 100] instance discrete_topology.first_countable_topology [discrete_topology α] : first_countable_topology α := { nhds_generated_countable := by { rw nhds_discrete, exact is_countably_generated_pure } } @[priority 100] instance discrete_topology.second_countable_topology_of_encodable [hd : discrete_topology α] [encodable α] : second_countable_topology α := begin haveI : ∀ (i : α), second_countable_topology ↥({i} : set α), from λ i, { is_open_generated_countable := ⟨{univ}, countable_singleton _, by simp only [eq_iff_true_of_subsingleton]⟩, }, exact second_countable_topology_of_countable_cover (singletons_open_iff_discrete.mpr hd) (Union_of_singleton α), end lemma bot_topological_space_eq_generate_from_of_pred_succ_order {α} [partial_order α] [pred_order α] [succ_order α] [no_min_order α] [no_max_order α] : (⊥ : topological_space α) = generate_from {s | ∃ a, s = Ioi a ∨ s = Iio a} := begin refine (eq_bot_of_singletons_open (λ a, _)).symm, have h_singleton_eq_inter : {a} = Iio (succ a) ∩ Ioi (pred a), { suffices h_singleton_eq_inter' : {a} = Iic a ∩ Ici a, by rw [h_singleton_eq_inter', ←Ioi_pred, ←Iio_succ], rw [inter_comm, Ici_inter_Iic, Icc_self a], }, rw h_singleton_eq_inter, apply is_open.inter, { exact is_open_generate_from_of_mem ⟨succ a, or.inr rfl⟩, }, { exact is_open_generate_from_of_mem ⟨pred a, or.inl rfl⟩, }, end lemma discrete_topology_iff_order_topology_of_pred_succ' [partial_order α] [pred_order α] [succ_order α] [no_min_order α] [no_max_order α] : discrete_topology α ↔ order_topology α := begin refine ⟨λ h, ⟨_⟩, λ h, ⟨_⟩⟩, { rw h.eq_bot, exact bot_topological_space_eq_generate_from_of_pred_succ_order, }, { rw h.topology_eq_generate_intervals, exact bot_topological_space_eq_generate_from_of_pred_succ_order.symm, }, end @[priority 100] instance discrete_topology.order_topology_of_pred_succ' [h : discrete_topology α] [partial_order α] [pred_order α] [succ_order α] [no_min_order α] [no_max_order α] : order_topology α := discrete_topology_iff_order_topology_of_pred_succ'.1 h lemma linear_order.bot_topological_space_eq_generate_from {α} [linear_order α] [pred_order α] [succ_order α] : (⊥ : topological_space α) = generate_from {s | ∃ a, s = Ioi a ∨ s = Iio a} := begin refine (eq_bot_of_singletons_open (λ a, _)).symm, have h_singleton_eq_inter : {a} = Iic a ∩ Ici a, by rw [inter_comm, Ici_inter_Iic, Icc_self a], by_cases ha_top : is_top a, { rw [ha_top.Iic_eq, inter_comm, inter_univ] at h_singleton_eq_inter, by_cases ha_bot : is_bot a, { rw ha_bot.Ici_eq at h_singleton_eq_inter, rw h_singleton_eq_inter, apply is_open_univ, }, { rw is_bot_iff_is_min at ha_bot, rw ←Ioi_pred_of_not_is_min ha_bot at h_singleton_eq_inter, rw h_singleton_eq_inter, exact is_open_generate_from_of_mem ⟨pred a, or.inl rfl⟩, }, }, { rw is_top_iff_is_max at ha_top, rw ←Iio_succ_of_not_is_max ha_top at h_singleton_eq_inter, by_cases ha_bot : is_bot a, { rw [ha_bot.Ici_eq, inter_univ] at h_singleton_eq_inter, rw h_singleton_eq_inter, exact is_open_generate_from_of_mem ⟨succ a, or.inr rfl⟩, }, { rw is_bot_iff_is_min at ha_bot, rw ←Ioi_pred_of_not_is_min ha_bot at h_singleton_eq_inter, rw h_singleton_eq_inter, apply is_open.inter, { exact is_open_generate_from_of_mem ⟨succ a, or.inr rfl⟩ }, { exact is_open_generate_from_of_mem ⟨pred a, or.inl rfl⟩ } } }, end lemma discrete_topology_iff_order_topology_of_pred_succ [linear_order α] [pred_order α] [succ_order α] : discrete_topology α ↔ order_topology α := begin refine ⟨λ h, ⟨_⟩, λ h, ⟨_⟩⟩, { rw h.eq_bot, exact linear_order.bot_topological_space_eq_generate_from, }, { rw h.topology_eq_generate_intervals, exact linear_order.bot_topological_space_eq_generate_from.symm, }, end @[priority 100] instance discrete_topology.order_topology_of_pred_succ [h : discrete_topology α] [linear_order α] [pred_order α] [succ_order α] : order_topology α := discrete_topology_iff_order_topology_of_pred_succ.mp h @[priority 100] instance discrete_topology.metrizable_space [discrete_topology α] : metrizable_space α := begin unfreezingI { obtain rfl := discrete_topology.eq_bot α }, exact @uniform_space.metrizable_space α ⊥ (is_countably_generated_principal _) _, end
81f3c2fa9d7aef36d576e3435a4399ead2ce33f7
f5f7e6fae601a5fe3cac7cc3ed353ed781d62419
/src/logic/basic.lean
897e30ebefd850e39b23fc99d120813d779fcc33
[ "Apache-2.0" ]
permissive
EdAyers/mathlib
9ecfb2f14bd6caad748b64c9c131befbff0fb4e0
ca5d4c1f16f9c451cf7170b10105d0051db79e1b
refs/heads/master
1,626,189,395,845
1,555,284,396,000
1,555,284,396,000
144,004,030
0
0
Apache-2.0
1,533,727,664,000
1,533,727,663,000
null
UTF-8
Lean
false
false
26,081
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura Theorems that require decidability hypotheses are in the namespace "decidable". Classical versions are in the namespace "classical". Note: in the presence of automation, this whole file may be unnecessary. On the other hand, maybe it is useful for writing automation. -/ import data.prod tactic.cache /- miscellany TODO: move elsewhere -/ section miscellany variables {α : Type*} {β : Type*} @[reducible] def hidden {a : α} := a def empty.elim {C : Sort*} : empty → C. instance : subsingleton empty := ⟨λa, a.elim⟩ instance : decidable_eq empty := λa, a.elim @[priority 0] instance decidable_eq_of_subsingleton {α} [subsingleton α] : decidable_eq α | a b := is_true (subsingleton.elim a b) /- Add an instance to "undo" coercion transitivity into a chain of coercions, because most simp lemmas are stated with respect to simple coercions and will not match when part of a chain. -/ @[simp] theorem coe_coe {α β γ} [has_coe α β] [has_coe_t β γ] (a : α) : (a : γ) = (a : β) := rfl @[simp] theorem coe_fn_coe_trans {α β γ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_fun γ] (x : α) : @coe_fn α _ x = @coe_fn β _ x := rfl @[simp] theorem coe_fn_coe_base {α β} [has_coe α β] [has_coe_to_fun β] (x : α) : @coe_fn α _ x = @coe_fn β _ x := rfl @[simp] theorem coe_sort_coe_trans {α β γ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_sort γ] (x : α) : @coe_sort α _ x = @coe_sort β _ x := rfl @[simp] theorem coe_sort_coe_base {α β} [has_coe α β] [has_coe_to_sort β] (x : α) : @coe_sort α _ x = @coe_sort β _ x := rfl /-- `pempty` is the universe-polymorphic analogue of `empty`. -/ @[derive decidable_eq] inductive {u} pempty : Sort u def pempty.elim {C : Sort*} : pempty → C. instance subsingleton_pempty : subsingleton pempty := ⟨λa, a.elim⟩ lemma congr_arg_heq {α} {β : α → Sort*} (f : ∀ a, β a) : ∀ {a₁ a₂ : α}, a₁ = a₂ → f a₁ == f a₂ | a _ rfl := heq.rfl lemma plift.down_inj {α : Sort*} : ∀ (a b : plift α), a.down = b.down → a = b | ⟨a⟩ ⟨b⟩ rfl := rfl end miscellany /- propositional connectives -/ @[simp] theorem false_ne_true : false ≠ true | h := h.symm ▸ trivial section propositional variables {a b c d : Prop} /- implies -/ theorem iff_of_eq (e : a = b) : a ↔ b := e ▸ iff.rfl theorem iff_iff_eq : (a ↔ b) ↔ a = b := ⟨propext, iff_of_eq⟩ @[simp] theorem imp_self : (a → a) ↔ true := iff_true_intro id theorem imp_intro {α β} (h : α) (h₂ : β) : α := h theorem imp_false : (a → false) ↔ ¬ a := iff.rfl theorem imp_and_distrib {α} : (α → b ∧ c) ↔ (α → b) ∧ (α → c) := ⟨λ h, ⟨λ ha, (h ha).left, λ ha, (h ha).right⟩, λ h ha, ⟨h.left ha, h.right ha⟩⟩ @[simp] theorem and_imp : (a ∧ b → c) ↔ (a → b → c) := iff.intro (λ h ha hb, h ⟨ha, hb⟩) (λ h ⟨ha, hb⟩, h ha hb) theorem iff_def : (a ↔ b) ↔ (a → b) ∧ (b → a) := iff_iff_implies_and_implies _ _ theorem iff_def' : (a ↔ b) ↔ (b → a) ∧ (a → b) := iff_def.trans and.comm @[simp] theorem imp_true_iff {α : Sort*} : (α → true) ↔ true := iff_true_intro $ λ_, trivial @[simp] theorem imp_iff_right (ha : a) : (a → b) ↔ b := ⟨λf, f ha, imp_intro⟩ /- not -/ theorem not.elim {α : Sort*} (H1 : ¬a) (H2 : a) : α := absurd H2 H1 @[reducible] theorem not.imp {a b : Prop} (H2 : ¬b) (H1 : a → b) : ¬a := mt H1 H2 theorem not_not_of_not_imp : ¬(a → b) → ¬¬a := mt not.elim theorem not_of_not_imp {α} : ¬(α → b) → ¬b := mt imp_intro theorem dec_em (p : Prop) [decidable p] : p ∨ ¬p := decidable.em p theorem by_contradiction {p} [decidable p] : (¬p → false) → p := decidable.by_contradiction @[simp] theorem not_not [decidable a] : ¬¬a ↔ a := iff.intro by_contradiction not_not_intro theorem of_not_not [decidable a] : ¬¬a → a := by_contradiction theorem of_not_imp [decidable a] (h : ¬ (a → b)) : a := by_contradiction (not_not_of_not_imp h) theorem not.imp_symm [decidable a] (h : ¬a → b) (hb : ¬b) : a := by_contradiction $ hb ∘ h theorem not_imp_comm [decidable a] [decidable b] : (¬a → b) ↔ (¬b → a) := ⟨not.imp_symm, not.imp_symm⟩ theorem imp.swap : (a → b → c) ↔ (b → a → c) := ⟨function.swap, function.swap⟩ theorem imp_not_comm : (a → ¬b) ↔ (b → ¬a) := imp.swap /- and -/ theorem not_and_of_not_left (b : Prop) : ¬a → ¬(a ∧ b) := mt and.left theorem not_and_of_not_right (a : Prop) {b : Prop} : ¬b → ¬(a ∧ b) := mt and.right theorem and.imp_left (h : a → b) : a ∧ c → b ∧ c := and.imp h id theorem and.imp_right (h : a → b) : c ∧ a → c ∧ b := and.imp id h lemma and.right_comm : (a ∧ b) ∧ c ↔ (a ∧ c) ∧ b := by simp [and.left_comm, and.comm] lemma and.rotate : a ∧ b ∧ c ↔ b ∧ c ∧ a := by simp [and.left_comm, and.comm] theorem and_not_self_iff (a : Prop) : a ∧ ¬ a ↔ false := iff.intro (assume h, (h.right) (h.left)) (assume h, h.elim) theorem not_and_self_iff (a : Prop) : ¬ a ∧ a ↔ false := iff.intro (assume ⟨hna, ha⟩, hna ha) false.elim theorem and_iff_left_of_imp {a b : Prop} (h : a → b) : (a ∧ b) ↔ a := iff.intro and.left (λ ha, ⟨ha, h ha⟩) theorem and_iff_right_of_imp {a b : Prop} (h : b → a) : (a ∧ b) ↔ b := iff.intro and.right (λ hb, ⟨h hb, hb⟩) lemma and.congr_right_iff : (a ∧ b ↔ a ∧ c) ↔ (a → (b ↔ c)) := ⟨λ h ha, by simp [ha] at h; exact h, and_congr_right⟩ /- or -/ theorem or_of_or_of_imp_of_imp (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → d) : c ∨ d := or.imp h₂ h₃ h₁ theorem or_of_or_of_imp_left (h₁ : a ∨ c) (h : a → b) : b ∨ c := or.imp_left h h₁ theorem or_of_or_of_imp_right (h₁ : c ∨ a) (h : a → b) : c ∨ b := or.imp_right h h₁ theorem or.elim3 (h : a ∨ b ∨ c) (ha : a → d) (hb : b → d) (hc : c → d) : d := or.elim h ha (assume h₂, or.elim h₂ hb hc) theorem or_imp_distrib : (a ∨ b → c) ↔ (a → c) ∧ (b → c) := ⟨assume h, ⟨assume ha, h (or.inl ha), assume hb, h (or.inr hb)⟩, assume ⟨ha, hb⟩, or.rec ha hb⟩ theorem or_iff_not_imp_left [decidable a] : a ∨ b ↔ (¬ a → b) := ⟨or.resolve_left, λ h, dite _ or.inl (or.inr ∘ h)⟩ theorem or_iff_not_imp_right [decidable b] : a ∨ b ↔ (¬ b → a) := or.comm.trans or_iff_not_imp_left theorem not_imp_not [decidable a] : (¬ a → ¬ b) ↔ (b → a) := ⟨assume h hb, by_contradiction $ assume na, h na hb, mt⟩ /- distributivity -/ theorem and_or_distrib_left : a ∧ (b ∨ c) ↔ (a ∧ b) ∨ (a ∧ c) := ⟨λ ⟨ha, hbc⟩, hbc.imp (and.intro ha) (and.intro ha), or.rec (and.imp_right or.inl) (and.imp_right or.inr)⟩ theorem or_and_distrib_right : (a ∨ b) ∧ c ↔ (a ∧ c) ∨ (b ∧ c) := (and.comm.trans and_or_distrib_left).trans (or_congr and.comm and.comm) theorem or_and_distrib_left : a ∨ (b ∧ c) ↔ (a ∨ b) ∧ (a ∨ c) := ⟨or.rec (λha, and.intro (or.inl ha) (or.inl ha)) (and.imp or.inr or.inr), and.rec $ or.rec (imp_intro ∘ or.inl) (or.imp_right ∘ and.intro)⟩ theorem and_or_distrib_right : (a ∧ b) ∨ c ↔ (a ∨ c) ∧ (b ∨ c) := (or.comm.trans or_and_distrib_left).trans (and_congr or.comm or.comm) /- iff -/ theorem iff_of_true (ha : a) (hb : b) : a ↔ b := ⟨λ_, hb, λ _, ha⟩ theorem iff_of_false (ha : ¬a) (hb : ¬b) : a ↔ b := ⟨ha.elim, hb.elim⟩ theorem iff_true_left (ha : a) : (a ↔ b) ↔ b := ⟨λ h, h.1 ha, iff_of_true ha⟩ theorem iff_true_right (ha : a) : (b ↔ a) ↔ b := iff.comm.trans (iff_true_left ha) theorem iff_false_left (ha : ¬a) : (a ↔ b) ↔ ¬b := ⟨λ h, mt h.2 ha, iff_of_false ha⟩ theorem iff_false_right (ha : ¬a) : (b ↔ a) ↔ ¬b := iff.comm.trans (iff_false_left ha) theorem not_or_of_imp [decidable a] (h : a → b) : ¬ a ∨ b := if ha : a then or.inr (h ha) else or.inl ha theorem imp_iff_not_or [decidable a] : (a → b) ↔ (¬ a ∨ b) := ⟨not_or_of_imp, or.neg_resolve_left⟩ theorem imp_or_distrib [decidable a] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by simp [imp_iff_not_or, or.comm, or.left_comm] theorem imp_or_distrib' [decidable b] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by by_cases b; simp [h, or_iff_right_of_imp ((∘) false.elim)] theorem not_imp_of_and_not : a ∧ ¬ b → ¬ (a → b) | ⟨ha, hb⟩ h := hb $ h ha @[simp] theorem not_imp [decidable a] : ¬(a → b) ↔ a ∧ ¬b := ⟨λ h, ⟨of_not_imp h, not_of_not_imp h⟩, not_imp_of_and_not⟩ -- for monotonicity lemma imp_imp_imp (h₀ : c → a) (h₁ : b → d) : (a → b) → (c → d) := assume (h₂ : a → b), h₁ ∘ h₂ ∘ h₀ theorem peirce (a b : Prop) [decidable a] : ((a → b) → a) → a := if ha : a then λ h, ha else λ h, h ha.elim theorem peirce' {a : Prop} (H : ∀ b : Prop, (a → b) → a) : a := H _ id theorem not_iff_not [decidable a] [decidable b] : (¬ a ↔ ¬ b) ↔ (a ↔ b) := by rw [@iff_def (¬ a), @iff_def' a]; exact and_congr not_imp_not not_imp_not theorem not_iff_comm [decidable a] [decidable b] : (¬ a ↔ b) ↔ (¬ b ↔ a) := by rw [@iff_def (¬ a), @iff_def (¬ b)]; exact and_congr not_imp_comm imp_not_comm theorem not_iff [decidable a] [decidable b] : ¬ (a ↔ b) ↔ (¬ a ↔ b) := by split; intro h; [split, skip]; intro h'; [by_contradiction,intro,skip]; try { refine h _; simp [*] }; rw [h',not_iff_self] at h; exact h theorem iff_not_comm [decidable a] [decidable b] : (a ↔ ¬ b) ↔ (b ↔ ¬ a) := by rw [@iff_def a, @iff_def b]; exact and_congr imp_not_comm not_imp_comm theorem iff_iff_and_or_not_and_not [decidable b] : (a ↔ b) ↔ (a ∧ b) ∨ (¬ a ∧ ¬ b) := by { split; intro h, { rw h; by_cases b; [left,right]; split; assumption }, { cases h with h h; cases h; split; intro; { contradiction <|> assumption } } } @[simp] theorem not_and_not_right [decidable b] : ¬(a ∧ ¬b) ↔ (a → b) := ⟨λ h ha, h.imp_symm $ and.intro ha, λ h ⟨ha, hb⟩, hb $ h ha⟩ @[inline] def decidable_of_iff (a : Prop) (h : a ↔ b) [D : decidable a] : decidable b := decidable_of_decidable_of_iff D h @[inline] def decidable_of_iff' (b : Prop) (h : a ↔ b) [D : decidable b] : decidable a := decidable_of_decidable_of_iff D h.symm def decidable_of_bool : ∀ (b : bool) (h : b ↔ a), decidable a | tt h := is_true (h.1 rfl) | ff h := is_false (mt h.2 bool.ff_ne_tt) /- de morgan's laws -/ theorem not_and_of_not_or_not (h : ¬ a ∨ ¬ b) : ¬ (a ∧ b) | ⟨ha, hb⟩ := or.elim h (absurd ha) (absurd hb) theorem not_and_distrib [decidable a] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if ha : a then or.inr (λ hb, h ⟨ha, hb⟩) else or.inl ha, not_and_of_not_or_not⟩ theorem not_and_distrib' [decidable b] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if hb : b then or.inl (λ ha, h ⟨ha, hb⟩) else or.inr hb, not_and_of_not_or_not⟩ @[simp] theorem not_and : ¬ (a ∧ b) ↔ (a → ¬ b) := and_imp theorem not_and' : ¬ (a ∧ b) ↔ b → ¬a := not_and.trans imp_not_comm theorem not_or_distrib : ¬ (a ∨ b) ↔ ¬ a ∧ ¬ b := ⟨λ h, ⟨λ ha, h (or.inl ha), λ hb, h (or.inr hb)⟩, λ ⟨h₁, h₂⟩ h, or.elim h h₁ h₂⟩ theorem or_iff_not_and_not [decidable a] [decidable b] : a ∨ b ↔ ¬ (¬a ∧ ¬b) := by rw [← not_or_distrib, not_not] theorem and_iff_not_or_not [decidable a] [decidable b] : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := by rw [← not_and_distrib, not_not] end propositional /- equality -/ section equality variables {α : Sort*} {a b : α} @[simp] theorem heq_iff_eq : a == b ↔ a = b := ⟨eq_of_heq, heq_of_eq⟩ theorem proof_irrel_heq {p q : Prop} (hp : p) (hq : q) : hp == hq := have p = q, from propext ⟨λ _, hq, λ _, hp⟩, by subst q; refl theorem ne_of_mem_of_not_mem {α β} [has_mem α β] {s : β} {a b : α} (h : a ∈ s) : b ∉ s → a ≠ b := mt $ λ e, e ▸ h theorem eq_equivalence : equivalence (@eq α) := ⟨eq.refl, @eq.symm _, @eq.trans _⟩ lemma heq_of_eq_mp : ∀ {α β : Sort*} {a : α} {a' : β} (e : α = β) (h₂ : (eq.mp e a) = a'), a == a' | α ._ a a' rfl h := eq.rec_on h (heq.refl _) lemma rec_heq_of_heq {β} {C : α → Sort*} {x : C a} {y : β} (eq : a = b) (h : x == y) : @eq.rec α a C x b eq == y := by subst eq; exact h @[simp] lemma {u} eq_mpr_heq {α β : Sort u} (h : β = α) (x : α) : eq.mpr h x == x := by subst h; refl end equality /- quantifiers -/ section quantifiers variables {α : Sort*} {p q : α → Prop} {b : Prop} def Exists.imp := @exists_imp_exists theorem forall_swap {α β} {p : α → β → Prop} : (∀ x y, p x y) ↔ ∀ y x, p x y := ⟨function.swap, function.swap⟩ theorem exists_swap {α β} {p : α → β → Prop} : (∃ x y, p x y) ↔ ∃ y x, p x y := ⟨λ ⟨x, y, h⟩, ⟨y, x, h⟩, λ ⟨y, x, h⟩, ⟨x, y, h⟩⟩ @[simp] theorem exists_imp_distrib : ((∃ x, p x) → b) ↔ ∀ x, p x → b := ⟨λ h x hpx, h ⟨x, hpx⟩, λ h ⟨x, hpx⟩, h x hpx⟩ --theorem forall_not_of_not_exists (h : ¬ ∃ x, p x) : ∀ x, ¬ p x := --forall_imp_of_exists_imp h theorem not_exists_of_forall_not (h : ∀ x, ¬ p x) : ¬ ∃ x, p x := exists_imp_distrib.2 h @[simp] theorem not_exists : (¬ ∃ x, p x) ↔ ∀ x, ¬ p x := exists_imp_distrib theorem not_forall_of_exists_not : (∃ x, ¬ p x) → ¬ ∀ x, p x | ⟨x, hn⟩ h := hn (h x) theorem not_forall {p : α → Prop} [decidable (∃ x, ¬ p x)] [∀ x, decidable (p x)] : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x := ⟨not.imp_symm $ λ nx x, nx.imp_symm $ λ h, ⟨x, h⟩, not_forall_of_exists_not⟩ @[simp] theorem not_forall_not [decidable (∃ x, p x)] : (¬ ∀ x, ¬ p x) ↔ ∃ x, p x := by haveI := decidable_of_iff (¬ ∃ x, p x) not_exists; exact not_iff_comm.1 not_exists @[simp] theorem not_exists_not [∀ x, decidable (p x)] : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := by simp @[simp] theorem forall_true_iff : (α → true) ↔ true := iff_true_intro (λ _, trivial) -- Unfortunately this causes simp to loop sometimes, so we -- add the 2 and 3 cases as simp lemmas instead theorem forall_true_iff' (h : ∀ a, p a ↔ true) : (∀ a, p a) ↔ true := iff_true_intro (λ _, of_iff_true (h _)) @[simp] theorem forall_2_true_iff {β : α → Sort*} : (∀ a, β a → true) ↔ true := forall_true_iff' $ λ _, forall_true_iff @[simp] theorem forall_3_true_iff {β : α → Sort*} {γ : Π a, β a → Sort*} : (∀ a (b : β a), γ a b → true) ↔ true := forall_true_iff' $ λ _, forall_2_true_iff @[simp] theorem forall_const (α : Sort*) [inhabited α] : (α → b) ↔ b := ⟨λ h, h (arbitrary α), λ hb x, hb⟩ @[simp] theorem exists_const (α : Sort*) [inhabited α] : (∃ x : α, b) ↔ b := ⟨λ ⟨x, h⟩, h, λ h, ⟨arbitrary α, h⟩⟩ theorem forall_and_distrib : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) := ⟨λ h, ⟨λ x, (h x).left, λ x, (h x).right⟩, λ ⟨h₁, h₂⟩ x, ⟨h₁ x, h₂ x⟩⟩ theorem exists_or_distrib : (∃ x, p x ∨ q x) ↔ (∃ x, p x) ∨ (∃ x, q x) := ⟨λ ⟨x, hpq⟩, hpq.elim (λ hpx, or.inl ⟨x, hpx⟩) (λ hqx, or.inr ⟨x, hqx⟩), λ hepq, hepq.elim (λ ⟨x, hpx⟩, ⟨x, or.inl hpx⟩) (λ ⟨x, hqx⟩, ⟨x, or.inr hqx⟩)⟩ @[simp] theorem exists_and_distrib_left {q : Prop} {p : α → Prop} : (∃x, q ∧ p x) ↔ q ∧ (∃x, p x) := ⟨λ ⟨x, hq, hp⟩, ⟨hq, x, hp⟩, λ ⟨hq, x, hp⟩, ⟨x, hq, hp⟩⟩ @[simp] theorem exists_and_distrib_right {q : Prop} {p : α → Prop} : (∃x, p x ∧ q) ↔ (∃x, p x) ∧ q := by simp [and_comm] @[simp] theorem forall_eq {a' : α} : (∀a, a = a' → p a) ↔ p a' := ⟨λ h, h a' rfl, λ h a e, e.symm ▸ h⟩ @[simp] theorem exists_eq {a' : α} : ∃ a, a = a' := ⟨_, rfl⟩ @[simp] theorem exists_eq_left {a' : α} : (∃ a, a = a' ∧ p a) ↔ p a' := ⟨λ ⟨a, e, h⟩, e ▸ h, λ h, ⟨_, rfl, h⟩⟩ @[simp] theorem exists_eq_right {a' : α} : (∃ a, p a ∧ a = a') ↔ p a' := (exists_congr $ by exact λ a, and.comm).trans exists_eq_left @[simp] theorem forall_eq' {a' : α} : (∀a, a' = a → p a) ↔ p a' := by simp [@eq_comm _ a'] @[simp] theorem exists_eq_left' {a' : α} : (∃ a, a' = a ∧ p a) ↔ p a' := by simp [@eq_comm _ a'] @[simp] theorem exists_eq_right' {a' : α} : (∃ a, p a ∧ a' = a) ↔ p a' := by simp [@eq_comm _ a'] theorem forall_or_of_or_forall (h : b ∨ ∀x, p x) (x) : b ∨ p x := h.imp_right $ λ h₂, h₂ x theorem forall_or_distrib_left {q : Prop} {p : α → Prop} [decidable q] : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := ⟨λ h, if hq : q then or.inl hq else or.inr $ λ x, (h x).resolve_left hq, forall_or_of_or_forall⟩ @[simp] theorem exists_prop {p q : Prop} : (∃ h : p, q) ↔ p ∧ q := ⟨λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩⟩ @[simp] theorem exists_false : ¬ (∃a:α, false) := assume ⟨a, h⟩, h theorem Exists.fst {p : b → Prop} : Exists p → b | ⟨h, _⟩ := h theorem Exists.snd {p : b → Prop} : ∀ h : Exists p, p h.fst | ⟨_, h⟩ := h @[simp] theorem forall_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∀ h' : p, q h') ↔ q h := @forall_const (q h) p ⟨h⟩ @[simp] theorem exists_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∃ h' : p, q h') ↔ q h := @exists_const (q h) p ⟨h⟩ @[simp] theorem forall_prop_of_false {p : Prop} {q : p → Prop} (hn : ¬ p) : (∀ h' : p, q h') ↔ true := iff_true_intro $ λ h, hn.elim h @[simp] theorem exists_prop_of_false {p : Prop} {q : p → Prop} : ¬ p → ¬ (∃ h' : p, q h') := mt Exists.fst end quantifiers /- classical versions -/ namespace classical variables {α : Sort*} {p : α → Prop} local attribute [instance] prop_decidable protected theorem not_forall : (¬ ∀ x, p x) ↔ (∃ x, ¬ p x) := not_forall protected theorem forall_or_distrib_left {q : Prop} {p : α → Prop} : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := forall_or_distrib_left theorem cases {p : Prop → Prop} (h1 : p true) (h2 : p false) : ∀a, p a := assume a, cases_on a h1 h2 theorem or_not {p : Prop} : p ∨ ¬ p := by_cases or.inl or.inr protected theorem or_iff_not_imp_left {p q : Prop} : p ∨ q ↔ (¬ p → q) := or_iff_not_imp_left protected theorem or_iff_not_imp_right {p q : Prop} : q ∨ p ↔ (¬ p → q) := or_iff_not_imp_right protected lemma not_not {p : Prop} : ¬¬p ↔ p := not_not /- use shortened names to avoid conflict when classical namespace is open -/ noncomputable theorem dec (p : Prop) : decidable p := by apply_instance noncomputable theorem dec_pred (p : α → Prop) : decidable_pred p := by apply_instance noncomputable theorem dec_rel (p : α → α → Prop) : decidable_rel p := by apply_instance noncomputable theorem dec_eq (α : Sort*) : decidable_eq α := by apply_instance @[elab_as_eliminator] noncomputable def {u} exists_cases {C : Sort u} (H0 : C) (H : ∀ a, p a → C) : C := if h : ∃ a, p a then H (classical.some h) (classical.some_spec h) else H0 lemma some_spec2 {α : Type*} {p : α → Prop} {h : ∃a, p a} (q : α → Prop) (hpq : ∀a, p a → q a) : q (some h) := hpq _ $ some_spec _ end classical @[elab_as_eliminator] noncomputable def {u} exists.classical_rec_on {α} {p : α → Prop} (h : ∃ a, p a) {C : Sort u} (H : ∀ a, p a → C) : C := H (classical.some h) (classical.some_spec h) /- bounded quantifiers -/ section bounded_quantifiers variables {α : Sort*} {r p q : α → Prop} {P Q : ∀ x, p x → Prop} {b : Prop} theorem bex_def : (∃ x (h : p x), q x) ↔ ∃ x, p x ∧ q x := ⟨λ ⟨x, px, qx⟩, ⟨x, px, qx⟩, λ ⟨x, px, qx⟩, ⟨x, px, qx⟩⟩ theorem bex.elim {b : Prop} : (∃ x h, P x h) → (∀ a h, P a h → b) → b | ⟨a, h₁, h₂⟩ h' := h' a h₁ h₂ theorem bex.intro (a : α) (h₁ : p a) (h₂ : P a h₁) : ∃ x (h : p x), P x h := ⟨a, h₁, h₂⟩ theorem ball_congr (H : ∀ x h, P x h ↔ Q x h) : (∀ x h, P x h) ↔ (∀ x h, Q x h) := forall_congr $ λ x, forall_congr (H x) theorem bex_congr (H : ∀ x h, P x h ↔ Q x h) : (∃ x h, P x h) ↔ (∃ x h, Q x h) := exists_congr $ λ x, exists_congr (H x) theorem ball.imp_right (H : ∀ x h, (P x h → Q x h)) (h₁ : ∀ x h, P x h) (x h) : Q x h := H _ _ $ h₁ _ _ theorem bex.imp_right (H : ∀ x h, (P x h → Q x h)) : (∃ x h, P x h) → ∃ x h, Q x h | ⟨x, h, h'⟩ := ⟨_, _, H _ _ h'⟩ theorem ball.imp_left (H : ∀ x, p x → q x) (h₁ : ∀ x, q x → r x) (x) (h : p x) : r x := h₁ _ $ H _ h theorem bex.imp_left (H : ∀ x, p x → q x) : (∃ x (_ : p x), r x) → ∃ x (_ : q x), r x | ⟨x, hp, hr⟩ := ⟨x, H _ hp, hr⟩ theorem ball_of_forall (h : ∀ x, p x) (x) (_ : q x) : p x := h x theorem forall_of_ball (H : ∀ x, p x) (h : ∀ x, p x → q x) (x) : q x := h x $ H x theorem bex_of_exists (H : ∀ x, p x) : (∃ x, q x) → ∃ x (_ : p x), q x | ⟨x, hq⟩ := ⟨x, H x, hq⟩ theorem exists_of_bex : (∃ x (_ : p x), q x) → ∃ x, q x | ⟨x, _, hq⟩ := ⟨x, hq⟩ @[simp] theorem bex_imp_distrib : ((∃ x h, P x h) → b) ↔ (∀ x h, P x h → b) := by simp theorem not_bex : (¬ ∃ x h, P x h) ↔ ∀ x h, ¬ P x h := bex_imp_distrib theorem not_ball_of_bex_not : (∃ x h, ¬ P x h) → ¬ ∀ x h, P x h | ⟨x, h, hp⟩ al := hp $ al x h theorem not_ball [decidable (∃ x h, ¬ P x h)] [∀ x h, decidable (P x h)] : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := ⟨not.imp_symm $ λ nx x h, nx.imp_symm $ λ h', ⟨x, h, h'⟩, not_ball_of_bex_not⟩ theorem ball_true_iff (p : α → Prop) : (∀ x, p x → true) ↔ true := iff_true_intro (λ h hrx, trivial) theorem ball_and_distrib : (∀ x h, P x h ∧ Q x h) ↔ (∀ x h, P x h) ∧ (∀ x h, Q x h) := iff.trans (forall_congr $ λ x, forall_and_distrib) forall_and_distrib theorem bex_or_distrib : (∃ x h, P x h ∨ Q x h) ↔ (∃ x h, P x h) ∨ (∃ x h, Q x h) := iff.trans (exists_congr $ λ x, exists_or_distrib) exists_or_distrib end bounded_quantifiers namespace classical local attribute [instance] prop_decidable theorem not_ball {α : Sort*} {p : α → Prop} {P : Π (x : α), p x → Prop} : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := _root_.not_ball end classical section nonempty universes u v w variables {α : Type u} {β : Type v} {γ : α → Type w} attribute [simp] nonempty_of_inhabited lemma exists_true_iff_nonempty {α : Sort*} : (∃a:α, true) ↔ nonempty α := iff.intro (λ⟨a, _⟩, ⟨a⟩) (λ⟨a⟩, ⟨a, trivial⟩) @[simp] lemma nonempty_Prop {p : Prop} : nonempty p ↔ p := iff.intro (assume ⟨h⟩, h) (assume h, ⟨h⟩) lemma not_nonempty_iff_imp_false {p : Prop} : ¬ nonempty α ↔ α → false := ⟨λ h a, h ⟨a⟩, λ h ⟨a⟩, h a⟩ @[simp] lemma nonempty_sigma : nonempty (Σa:α, γ a) ↔ (∃a:α, nonempty (γ a)) := iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩) @[simp] lemma nonempty_subtype {α : Sort u} {p : α → Prop} : nonempty (subtype p) ↔ (∃a:α, p a) := iff.intro (assume ⟨⟨a, h⟩⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a, h⟩⟩) @[simp] lemma nonempty_prod : nonempty (α × β) ↔ (nonempty α ∧ nonempty β) := iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩) @[simp] lemma nonempty_pprod {α : Sort u} {β : Sort v} : nonempty (pprod α β) ↔ (nonempty α ∧ nonempty β) := iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩) @[simp] lemma nonempty_sum : nonempty (α ⊕ β) ↔ (nonempty α ∨ nonempty β) := iff.intro (assume ⟨h⟩, match h with sum.inl a := or.inl ⟨a⟩ | sum.inr b := or.inr ⟨b⟩ end) (assume h, match h with or.inl ⟨a⟩ := ⟨sum.inl a⟩ | or.inr ⟨b⟩ := ⟨sum.inr b⟩ end) @[simp] lemma nonempty_psum {α : Sort u} {β : Sort v} : nonempty (psum α β) ↔ (nonempty α ∨ nonempty β) := iff.intro (assume ⟨h⟩, match h with psum.inl a := or.inl ⟨a⟩ | psum.inr b := or.inr ⟨b⟩ end) (assume h, match h with or.inl ⟨a⟩ := ⟨psum.inl a⟩ | or.inr ⟨b⟩ := ⟨psum.inr b⟩ end) @[simp] lemma nonempty_psigma {α : Sort u} {β : α → Sort v} : nonempty (psigma β) ↔ (∃a:α, nonempty (β a)) := iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩) @[simp] lemma nonempty_empty : ¬ nonempty empty := assume ⟨h⟩, h.elim @[simp] lemma nonempty_ulift : nonempty (ulift α) ↔ nonempty α := iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩) @[simp] lemma nonempty_plift {α : Sort u} : nonempty (plift α) ↔ nonempty α := iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩) @[simp] lemma nonempty.forall {α : Sort u} {p : nonempty α → Prop} : (∀h:nonempty α, p h) ↔ (∀a, p ⟨a⟩) := iff.intro (assume h a, h _) (assume h ⟨a⟩, h _) @[simp] lemma nonempty.exists {α : Sort u} {p : nonempty α → Prop} : (∃h:nonempty α, p h) ↔ (∃a, p ⟨a⟩) := iff.intro (assume ⟨⟨a⟩, h⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a⟩, h⟩) lemma classical.nonempty_pi {α : Sort u} {β : α → Sort v} : nonempty (Πa:α, β a) ↔ (∀a:α, nonempty (β a)) := iff.intro (assume ⟨f⟩ a, ⟨f a⟩) (assume f, ⟨assume a, classical.choice $ f a⟩) -- inhabited_of_nonempty already exists, in core/init/classical.lean, but the -- assumption is not [...], which makes it unsuitable for some applications noncomputable def classical.inhabited_of_nonempty' {α : Sort u} [h : nonempty α] : inhabited α := ⟨classical.choice h⟩ -- `nonempty` cannot be a `functor`, because `functor` is restricted to Types. lemma nonempty.map {α : Sort u} {β : Sort v} (f : α → β) : nonempty α → nonempty β | ⟨h⟩ := ⟨f h⟩ end nonempty
1de5ac3c7dfd95110ffc91eb36f85c2d9b430789
969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb
/src/measure_theory/ae_eq_fun_metric.lean
f26d80d25e83854c2a0cf93a15c190d20bb0de5e
[ "Apache-2.0" ]
permissive
SAAluthwela/mathlib
62044349d72dd63983a8500214736aa7779634d3
83a4b8b990907291421de54a78988c024dc8a552
refs/heads/master
1,679,433,873,417
1,615,998,031,000
1,615,998,031,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,351
lean
/- Copyright (c) 2019 Johannes Hölzl, Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Zhouhang Zhou -/ import measure_theory.ae_eq_fun /-! # Emetric space structure on almost everywhere equal functions Emetric on `L⁰` : If `β` is an `emetric_space`, then `L⁰` can be made into an `emetric_space`, where `edist [f] [g]` is defined to be `∫⁻ a, edist (f a) (g a)`. The integral used here is `lintegral : (α → ℝ≥0∞) → ℝ≥0∞`, which is defined in the file `integration.lean`. See `edist_mk_mk` and `edist_to_fun`. TODO: remove this file, and use instead the more general `Lp` space specialized to `p = 1`. -/ noncomputable theory open_locale classical ennreal open set filter topological_space ennreal emetric measure_theory function variables {α β γ δ : Type*} [measurable_space α] {μ ν : measure α} namespace measure_theory namespace ae_eq_fun variables [measurable_space β] [measurable_space γ] [measurable_space δ] section variables [emetric_space γ] [second_countable_topology γ] [opens_measurable_space γ] /-- `comp_edist [f] [g] a` will return `edist (f a) (g a)` -/ protected def edist (f g : α →ₘ[μ] γ) : α →ₘ[μ] ℝ≥0∞ := comp₂ edist measurable_edist f g protected lemma edist_comm (f g : α →ₘ[μ] γ) : f.edist g = g.edist f := induction_on₂ f g $ λ f hf g hg, mk_eq_mk.2 $ eventually_of_forall $ λ x, edist_comm (f x) (g x) lemma coe_fn_edist (f g : α →ₘ[μ] γ) : ⇑(f.edist g) =ᵐ[μ] λ a, edist (f a) (g a) := coe_fn_comp₂ _ _ _ _ protected lemma edist_self (f : α →ₘ[μ] γ) : f.edist f = 0 := induction_on f $ λ f hf, mk_eq_mk.2 $ eventually_of_forall $ λ x, edist_self (f x) /-- Almost everywhere equal functions form an `emetric_space`, with the emetric defined as `edist f g = ∫⁻ a, edist (f a) (g a)`. -/ instance : emetric_space (α →ₘ[μ] γ) := { edist := λf g, lintegral (f.edist g), edist_self := assume f, lintegral_eq_zero_iff.2 f.edist_self, edist_comm := λ f g, congr_arg lintegral $ f.edist_comm g, edist_triangle := λ f g h, induction_on₃ f g h $ λ f hf g hg h hh, calc ∫⁻ a, edist (f a) (h a) ∂μ ≤ ∫⁻ a, edist (f a) (g a) + edist (g a) (h a) ∂μ : measure_theory.lintegral_mono (λ a, edist_triangle (f a) (g a) (h a)) ... = ∫⁻ a, edist (f a) (g a) ∂μ + ∫⁻ a, edist (g a) (h a) ∂μ : lintegral_add' (hf.edist hg) (hg.edist hh), eq_of_edist_eq_zero := λ f g, begin exact induction_on₂ f g (λ f hf g hg H, mk_eq_mk.2 (((lintegral_eq_zero_iff' (hf.edist hg)).1 H).mono (λ x, eq_of_edist_eq_zero))) end } lemma edist_mk_mk {f g : α → γ} (hf hg) : edist (mk f hf : α →ₘ[μ] γ) (mk g hg) = ∫⁻ x, edist (f x) (g x) ∂μ := rfl lemma edist_eq_coe (f g : α →ₘ[μ] γ) : edist f g = ∫⁻ x, edist (f x) (g x) ∂μ := by rw [← edist_mk_mk, mk_coe_fn, mk_coe_fn] lemma edist_zero_eq_coe [has_zero γ] (f : α →ₘ[μ] γ) : edist f 0 = ∫⁻ x, edist (f x) 0 ∂μ := by rw [← edist_mk_mk, mk_coe_fn, zero_def] end section metric variables [metric_space γ] [second_countable_topology γ] [opens_measurable_space γ] lemma edist_mk_mk' {f g : α → γ} (hf hg) : edist (mk f hf : α →ₘ[μ] γ) (mk g hg) = ∫⁻ x, nndist (f x) (g x) ∂μ := by simp only [edist_mk_mk, edist_nndist] lemma edist_eq_coe' (f g : α →ₘ[μ] γ) : edist f g = ∫⁻ x, nndist (f x) (g x) ∂μ := by simp only [edist_eq_coe, edist_nndist] end metric lemma edist_add_right [normed_group γ] [second_countable_topology γ] [borel_space γ] (f g h : α →ₘ[μ] γ) : edist (f + h) (g + h) = edist f g := induction_on₃ f g h $ λ f hf g hg h hh, by simp [edist_mk_mk, edist_dist, dist_add_right] section normed_space variables {𝕜 : Type*} [normed_field 𝕜] variables [normed_group γ] [second_countable_topology γ] [normed_space 𝕜 γ] [borel_space γ] lemma edist_smul (c : 𝕜) (f : α →ₘ[μ] γ) : edist (c • f) 0 = (ennreal.of_real ∥c∥) * edist f 0 := induction_on f $ λ f hf, by simp [edist_mk_mk, zero_def, smul_mk, edist_dist, norm_smul, ennreal.of_real_mul, lintegral_const_mul'] end normed_space end ae_eq_fun end measure_theory
08ec40227aea3dc1d3857694d68bb4095807b3d1
c777c32c8e484e195053731103c5e52af26a25d1
/src/linear_algebra/adic_completion.lean
bc42c89538b275345497d6f43a4bee93f56d134a
[ "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
9,999
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.geom_sum import linear_algebra.smodeq import ring_theory.jacobson_ideal /-! # Completion of a module with respect to an ideal. In this file we define the notions of Hausdorff, precomplete, and complete for an `R`-module `M` with respect to an ideal `I`: ## Main definitions - `is_Hausdorff I M`: this says that the intersection of `I^n M` is `0`. - `is_precomplete I M`: this says that every Cauchy sequence converges. - `is_adic_complete I M`: this says that `M` is Hausdorff and precomplete. - `Hausdorffification I M`: this is the universal Hausdorff module with a map from `M`. - `completion I M`: if `I` is finitely generated, then this is the universal complete module (TODO) with a map from `M`. This map is injective iff `M` is Hausdorff and surjective iff `M` is precomplete. -/ open submodule variables {R : Type*} [comm_ring R] (I : ideal R) variables (M : Type*) [add_comm_group M] [module R M] variables {N : Type*} [add_comm_group N] [module R N] /-- A module `M` is Hausdorff with respect to an ideal `I` if `⋂ I^n M = 0`. -/ class is_Hausdorff : Prop := (haus' : ∀ x : M, (∀ n : ℕ, x ≡ 0 [SMOD (I ^ n • ⊤ : submodule R M)]) → x = 0) /-- A module `M` is precomplete with respect to an ideal `I` if every Cauchy sequence converges. -/ class is_precomplete : Prop := (prec' : ∀ f : ℕ → M, (∀ {m n}, m ≤ n → f m ≡ f n [SMOD (I ^ m • ⊤ : submodule R M)]) → ∃ L : M, ∀ n, f n ≡ L [SMOD (I ^ n • ⊤ : submodule R M)]) /-- A module `M` is `I`-adically complete if it is Hausdorff and precomplete. -/ class is_adic_complete extends is_Hausdorff I M, is_precomplete I M : Prop variables {I M} theorem is_Hausdorff.haus (h : is_Hausdorff I M) : ∀ x : M, (∀ n : ℕ, x ≡ 0 [SMOD (I ^ n • ⊤ : submodule R M)]) → x = 0 := is_Hausdorff.haus' theorem is_Hausdorff_iff : is_Hausdorff I M ↔ ∀ x : M, (∀ n : ℕ, x ≡ 0 [SMOD (I ^ n • ⊤ : submodule R M)]) → x = 0 := ⟨is_Hausdorff.haus, λ h, ⟨h⟩⟩ theorem is_precomplete.prec (h : is_precomplete I M) {f : ℕ → M} : (∀ {m n}, m ≤ n → f m ≡ f n [SMOD (I ^ m • ⊤ : submodule R M)]) → ∃ L : M, ∀ n, f n ≡ L [SMOD (I ^ n • ⊤ : submodule R M)] := is_precomplete.prec' _ theorem is_precomplete_iff : is_precomplete I M ↔ ∀ f : ℕ → M, (∀ {m n}, m ≤ n → f m ≡ f n [SMOD (I ^ m • ⊤ : submodule R M)]) → ∃ L : M, ∀ n, f n ≡ L [SMOD (I ^ n • ⊤ : submodule R M)] := ⟨λ h, h.1, λ h, ⟨h⟩⟩ variables (I M) /-- The Hausdorffification of a module with respect to an ideal. -/ @[reducible] def Hausdorffification : Type* := M ⧸ (⨅ n : ℕ, I ^ n • ⊤ : submodule R M) /-- The completion of a module with respect to an ideal. This is not necessarily Hausdorff. In fact, this is only complete if the ideal is finitely generated. -/ def adic_completion : submodule R (Π n : ℕ, (M ⧸ (I ^ n • ⊤ : submodule R M))) := { carrier := { f | ∀ {m n} (h : m ≤ n), liftq _ (mkq _) (by { rw ker_mkq, exact smul_mono (ideal.pow_le_pow h) le_rfl }) (f n) = f m }, zero_mem' := λ m n hmn, by rw [pi.zero_apply, pi.zero_apply, linear_map.map_zero], add_mem' := λ f g hf hg m n hmn, by rw [pi.add_apply, pi.add_apply, linear_map.map_add, hf hmn, hg hmn], smul_mem' := λ c f hf m n hmn, by rw [pi.smul_apply, pi.smul_apply, linear_map.map_smul, hf hmn] } namespace is_Hausdorff instance bot : is_Hausdorff (⊥ : ideal R) M := ⟨λ x hx, by simpa only [pow_one ⊥, bot_smul, smodeq.bot] using hx 1⟩ variables {M} protected theorem subsingleton (h : is_Hausdorff (⊤ : ideal R) M) : subsingleton M := ⟨λ x y, eq_of_sub_eq_zero $ h.haus (x - y) $ λ n, by { rw [ideal.top_pow, top_smul], exact smodeq.top }⟩ variables (M) @[priority 100] instance of_subsingleton [subsingleton M] : is_Hausdorff I M := ⟨λ x _, subsingleton.elim _ _⟩ variables {I M} theorem infi_pow_smul (h : is_Hausdorff I M) : (⨅ n : ℕ, I ^ n • ⊤ : submodule R M) = ⊥ := eq_bot_iff.2 $ λ x hx, (mem_bot _).2 $ h.haus x $ λ n, smodeq.zero.2 $ (mem_infi $ λ n : ℕ, I ^ n • ⊤).1 hx n end is_Hausdorff namespace Hausdorffification /-- The canonical linear map to the Hausdorffification. -/ def of : M →ₗ[R] Hausdorffification I M := mkq _ variables {I M} @[elab_as_eliminator] lemma induction_on {C : Hausdorffification I M → Prop} (x : Hausdorffification I M) (ih : ∀ x, C (of I M x)) : C x := quotient.induction_on' x ih variables (I M) instance : is_Hausdorff I (Hausdorffification I M) := ⟨λ x, quotient.induction_on' x $ λ x hx, (quotient.mk_eq_zero _).2 $ (mem_infi _).2 $ λ n, begin have := comap_map_mkq (⨅ n : ℕ, I ^ n • ⊤ : submodule R M) (I ^ n • ⊤), simp only [sup_of_le_right (infi_le (λ n, (I ^ n • ⊤ : submodule R M)) n)] at this, rw [← this, map_smul'', mem_comap, submodule.map_top, range_mkq, ← smodeq.zero], exact hx n end⟩ variables {M} [h : is_Hausdorff I N] include h /-- universal property of Hausdorffification: any linear map to a Hausdorff module extends to a unique map from the Hausdorffification. -/ def lift (f : M →ₗ[R] N) : Hausdorffification I M →ₗ[R] N := liftq _ f $ map_le_iff_le_comap.1 $ h.infi_pow_smul ▸ le_infi (λ n, le_trans (map_mono $ infi_le _ n) $ by { rw map_smul'', exact smul_mono le_rfl le_top }) theorem lift_of (f : M →ₗ[R] N) (x : M) : lift I f (of I M x) = f x := rfl theorem lift_comp_of (f : M →ₗ[R] N) : (lift I f).comp (of I M) = f := linear_map.ext $ λ _, rfl /-- Uniqueness of lift. -/ theorem lift_eq (f : M →ₗ[R] N) (g : Hausdorffification I M →ₗ[R] N) (hg : g.comp (of I M) = f) : g = lift I f := linear_map.ext $ λ x, induction_on x $ λ x, by rw [lift_of, ← hg, linear_map.comp_apply] end Hausdorffification namespace is_precomplete instance bot : is_precomplete (⊥ : ideal R) M := begin refine ⟨λ f hf, ⟨f 1, λ n, _⟩⟩, cases n, { rw [pow_zero, ideal.one_eq_top, top_smul], exact smodeq.top }, specialize hf (nat.le_add_left 1 n), rw [pow_one, bot_smul, smodeq.bot] at hf, rw hf end instance top : is_precomplete (⊤ : ideal R) M := ⟨λ f hf, ⟨0, λ n, by { rw [ideal.top_pow, top_smul], exact smodeq.top }⟩⟩ @[priority 100] instance of_subsingleton [subsingleton M] : is_precomplete I M := ⟨λ f hf, ⟨0, λ n, by rw subsingleton.elim (f n) 0⟩⟩ end is_precomplete namespace adic_completion /-- The canonical linear map to the completion. -/ def of : M →ₗ[R] adic_completion I M := { to_fun := λ x, ⟨λ n, mkq _ x, λ m n hmn, rfl⟩, map_add' := λ x y, rfl, map_smul' := λ c x, rfl } @[simp] lemma of_apply (x : M) (n : ℕ) : (of I M x).1 n = mkq _ x := rfl /-- Linearly evaluating a sequence in the completion at a given input. -/ def eval (n : ℕ) : adic_completion I M →ₗ[R] (M ⧸ (I ^ n • ⊤ : submodule R M)) := { to_fun := λ f, f.1 n, map_add' := λ f g, rfl, map_smul' := λ c f, rfl } @[simp] lemma coe_eval (n : ℕ) : (eval I M n : adic_completion I M → (M ⧸ (I ^ n • ⊤ : submodule R M))) = λ f, f.1 n := rfl lemma eval_apply (n : ℕ) (f : adic_completion I M) : eval I M n f = f.1 n := rfl lemma eval_of (n : ℕ) (x : M) : eval I M n (of I M x) = mkq _ x := rfl @[simp] lemma eval_comp_of (n : ℕ) : (eval I M n).comp (of I M) = mkq _ := rfl @[simp] lemma range_eval (n : ℕ) : (eval I M n).range = ⊤ := linear_map.range_eq_top.2 $ λ x, quotient.induction_on' x $ λ x, ⟨of I M x, rfl⟩ variables {I M} @[ext] lemma ext {x y : adic_completion I M} (h : ∀ n, eval I M n x = eval I M n y) : x = y := subtype.eq $ funext h variables (I M) instance : is_Hausdorff I (adic_completion I M) := ⟨λ x hx, ext $ λ n, smul_induction_on (smodeq.zero.1 $ hx n) (λ r hr x _, ((eval I M n).map_smul r x).symm ▸ quotient.induction_on' (eval I M n x) (λ x, smodeq.zero.2 $ smul_mem_smul hr mem_top)) (λ _ _ ih1 ih2, by rw [linear_map.map_add, ih1, ih2, linear_map.map_zero, add_zero])⟩ end adic_completion namespace is_adic_complete instance bot : is_adic_complete (⊥ : ideal R) M := {} protected theorem subsingleton (h : is_adic_complete (⊤ : ideal R) M) : subsingleton M := h.1.subsingleton @[priority 100] instance of_subsingleton [subsingleton M] : is_adic_complete I M := {} open_locale big_operators open finset lemma le_jacobson_bot [is_adic_complete I R] : I ≤ (⊥ : ideal R).jacobson := begin intros x hx, rw [← ideal.neg_mem_iff, ideal.mem_jacobson_bot], intros y, rw add_comm, let f : ℕ → R := λ n, ∑ i in range n, (x * y) ^ i, have hf : ∀ m n, m ≤ n → f m ≡ f n [SMOD I ^ m • (⊤ : submodule R R)], { intros m n h, simp only [f, algebra.id.smul_eq_mul, ideal.mul_top, smodeq.sub_mem], rw [← add_tsub_cancel_of_le h, finset.sum_range_add, ← sub_sub, sub_self, zero_sub, neg_mem_iff], apply submodule.sum_mem, intros n hn, rw [mul_pow, pow_add, mul_assoc], exact ideal.mul_mem_right _ (I ^ m) (ideal.pow_mem_pow hx m) }, obtain ⟨L, hL⟩ := is_precomplete.prec to_is_precomplete hf, { rw is_unit_iff_exists_inv, use L, rw [← sub_eq_zero, neg_mul], apply is_Hausdorff.haus (to_is_Hausdorff : is_Hausdorff I R), intros n, specialize hL n, rw [smodeq.sub_mem, algebra.id.smul_eq_mul, ideal.mul_top] at ⊢ hL, rw sub_zero, suffices : (1 - x * y) * (f n) - 1 ∈ I ^ n, { convert (ideal.sub_mem _ this (ideal.mul_mem_left _ (1 + - (x * y)) hL)) using 1, ring }, cases n, { simp only [ideal.one_eq_top, pow_zero] }, { dsimp [f], rw [← neg_sub _ (1:R), neg_mul, mul_geom_sum, neg_sub, sub_sub, add_comm, ← sub_sub, sub_self, zero_sub, neg_mem_iff, mul_pow], exact ideal.mul_mem_right _ (I ^ _) (ideal.pow_mem_pow hx _), } }, end end is_adic_complete
99936dd8cfac784c2632b8ca225ea89cd4e3d525
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/number_theory/padics/ring_homs.lean
c89e23076b962b2f71bcf8a3bc747fb2e231c423
[ "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
24,467
lean
/- Copyright (c) 2020 Johan Commelin, Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Robert Y. Lewis -/ import number_theory.padics.padic_integers /-! # Relating `ℤ_[p]` to `zmod (p ^ n)` In this file we establish connections between the `p`-adic integers $\mathbb{Z}_p$ and the integers modulo powers of `p`, $\mathbb{Z}/p^n\mathbb{Z}$. ## Main declarations We show that $\mathbb{Z}_p$ has a ring hom to $\mathbb{Z}/p^n\mathbb{Z}$ for each `n`. The case for `n = 1` is handled separately, since it is used in the general construction and we may want to use it without the `^1` getting in the way. * `padic_int.to_zmod`: ring hom to `zmod p` * `padic_int.to_zmod_pow`: ring hom to `zmod (p^n)` * `padic_int.ker_to_zmod` / `padic_int.ker_to_zmod_pow`: the kernels of these maps are the ideals generated by `p^n` We also establish the universal property of $\mathbb{Z}_p$ as a projective limit. Given a family of compatible ring homs $f_k : R \to \mathbb{Z}/p^n\mathbb{Z}$, there is a unique limit $R \to \mathbb{Z}_p$. * `padic_int.lift`: the limit function * `padic_int.lift_spec` / `padic_int.lift_unique`: the universal property ## Implementation notes The ring hom constructions go through an auxiliary constructor `padic_int.to_zmod_hom`, which removes some boilerplate code. -/ noncomputable theory open_locale classical open nat local_ring padic namespace padic_int variables {p : ℕ} [hp_prime : fact (p.prime)] include hp_prime section ring_homs /-! ### Ring homomorphisms to `zmod p` and `zmod (p ^ n)` -/ variables (p) (r : ℚ) omit hp_prime /-- `mod_part p r` is an integer that satisfies `∥(r - mod_part p r : ℚ_[p])∥ < 1` when `∥(r : ℚ_[p])∥ ≤ 1`, see `padic_int.norm_sub_mod_part`. It is the unique non-negative integer that is `< p` with this property. (Note that this definition assumes `r : ℚ`. See `padic_int.zmod_repr` for a version that takes values in `ℕ` and works for arbitrary `x : ℤ_[p]`.) -/ def mod_part : ℤ := (r.num * gcd_a r.denom p) % p include hp_prime variable {p} lemma mod_part_lt_p : mod_part p r < p := begin convert int.mod_lt _ _, { simp }, { exact_mod_cast hp_prime.1.ne_zero } end lemma mod_part_nonneg : 0 ≤ mod_part p r := int.mod_nonneg _ $ by exact_mod_cast hp_prime.1.ne_zero lemma is_unit_denom (r : ℚ) (h : ∥(r : ℚ_[p])∥ ≤ 1) : is_unit (r.denom : ℤ_[p]) := begin rw is_unit_iff, apply le_antisymm (r.denom : ℤ_[p]).2, rw [← not_lt, val_eq_coe, coe_coe], intro norm_denom_lt, have hr : ∥(r * r.denom : ℚ_[p])∥ = ∥(r.num : ℚ_[p])∥, { rw_mod_cast @rat.mul_denom_eq_num r, refl, }, rw padic_norm_e.mul at hr, have key : ∥(r.num : ℚ_[p])∥ < 1, { calc _ = _ : hr.symm ... < 1 * 1 : mul_lt_mul' h norm_denom_lt (norm_nonneg _) zero_lt_one ... = 1 : mul_one 1 }, have : ↑p ∣ r.num ∧ (p : ℤ) ∣ r.denom, { simp only [← norm_int_lt_one_iff_dvd, ← padic_norm_e_of_padic_int], norm_cast, exact ⟨key, norm_denom_lt⟩ }, apply hp_prime.1.not_dvd_one, rwa [← r.cop.gcd_eq_one, nat.dvd_gcd_iff, ← int.coe_nat_dvd_left, ← int.coe_nat_dvd], end lemma norm_sub_mod_part_aux (r : ℚ) (h : ∥(r : ℚ_[p])∥ ≤ 1) : ↑p ∣ r.num - r.num * r.denom.gcd_a p % p * ↑(r.denom) := begin rw ← zmod.int_coe_zmod_eq_zero_iff_dvd, simp only [int.cast_coe_nat, zmod.nat_cast_mod p, int.cast_mul, int.cast_sub], have := congr_arg (coe : ℤ → zmod p) (gcd_eq_gcd_ab r.denom p), simp only [int.cast_coe_nat, add_zero, int.cast_add, zmod.nat_cast_self, int.cast_mul, zero_mul] at this, push_cast, rw [mul_right_comm, mul_assoc, ←this], suffices rdcp : r.denom.coprime p, { rw rdcp.gcd_eq_one, simp only [mul_one, cast_one, sub_self], }, apply coprime.symm, apply (coprime_or_dvd_of_prime hp_prime.1 _).resolve_right, rw [← int.coe_nat_dvd, ← norm_int_lt_one_iff_dvd, not_lt], apply ge_of_eq, rw ← is_unit_iff, exact is_unit_denom r h, end lemma norm_sub_mod_part (h : ∥(r : ℚ_[p])∥ ≤ 1) : ∥(⟨r,h⟩ - mod_part p r : ℤ_[p])∥ < 1 := begin let n := mod_part p r, by_cases aux : (⟨r,h⟩ - n : ℤ_[p]) = 0, { rw [aux, norm_zero], exact zero_lt_one, }, rw [norm_lt_one_iff_dvd, ← (is_unit_denom r h).dvd_mul_right], suffices : ↑p ∣ r.num - n * r.denom, { convert (int.cast_ring_hom ℤ_[p]).map_dvd this, simp only [sub_mul, int.cast_coe_nat, ring_hom.eq_int_cast, int.cast_mul, sub_left_inj, int.cast_sub], apply subtype.coe_injective, simp only [coe_mul, subtype.coe_mk, coe_coe], rw_mod_cast @rat.mul_denom_eq_num r, refl }, exact norm_sub_mod_part_aux r h end lemma exists_mem_range_of_norm_rat_le_one (h : ∥(r : ℚ_[p])∥ ≤ 1) : ∃ n : ℤ, 0 ≤ n ∧ n < p ∧ ∥(⟨r,h⟩ - n : ℤ_[p])∥ < 1 := ⟨mod_part p r, mod_part_nonneg _, mod_part_lt_p _, norm_sub_mod_part _ h⟩ lemma zmod_congr_of_sub_mem_span_aux (n : ℕ) (x : ℤ_[p]) (a b : ℤ) (ha : x - a ∈ (ideal.span {p ^ n} : ideal ℤ_[p])) (hb : x - b ∈ (ideal.span {p ^ n} : ideal ℤ_[p])) : (a : zmod (p ^ n)) = b := begin rw [ideal.mem_span_singleton] at ha hb, rw [← sub_eq_zero, ← int.cast_sub, zmod.int_coe_zmod_eq_zero_iff_dvd, int.coe_nat_pow], rw [← dvd_neg, neg_sub] at ha, have := dvd_add ha hb, rwa [sub_eq_add_neg, sub_eq_add_neg, add_assoc, neg_add_cancel_left, ← sub_eq_add_neg, ← int.cast_sub, pow_p_dvd_int_iff] at this, end lemma zmod_congr_of_sub_mem_span (n : ℕ) (x : ℤ_[p]) (a b : ℕ) (ha : x - a ∈ (ideal.span {p ^ n} : ideal ℤ_[p])) (hb : x - b ∈ (ideal.span {p ^ n} : ideal ℤ_[p])) : (a : zmod (p ^ n)) = b := zmod_congr_of_sub_mem_span_aux n x a b ha hb lemma zmod_congr_of_sub_mem_max_ideal (x : ℤ_[p]) (m n : ℕ) (hm : x - m ∈ maximal_ideal ℤ_[p]) (hn : x - n ∈ maximal_ideal ℤ_[p]) : (m : zmod p) = n := begin rw maximal_ideal_eq_span_p at hm hn, have := zmod_congr_of_sub_mem_span_aux 1 x m n, simp only [pow_one] at this, specialize this hm hn, apply_fun zmod.cast_hom (show p ∣ p ^ 1, by rw pow_one) (zmod p) at this, simpa only [ring_hom.map_int_cast], end variable (x : ℤ_[p]) lemma exists_mem_range : ∃ n : ℕ, n < p ∧ (x - n ∈ maximal_ideal ℤ_[p]) := begin simp only [maximal_ideal_eq_span_p, ideal.mem_span_singleton, ← norm_lt_one_iff_dvd], obtain ⟨r, hr⟩ := rat_dense (x : ℚ_[p]) zero_lt_one, have H : ∥(r : ℚ_[p])∥ ≤ 1, { rw norm_sub_rev at hr, calc _ = ∥(r : ℚ_[p]) - x + x∥ : by ring_nf ... ≤ _ : padic_norm_e.nonarchimedean _ _ ... ≤ _ : max_le (le_of_lt hr) x.2 }, obtain ⟨n, hzn, hnp, hn⟩ := exists_mem_range_of_norm_rat_le_one r H, lift n to ℕ using hzn, use n, split, {exact_mod_cast hnp}, simp only [norm_def, coe_sub, subtype.coe_mk, coe_coe] at hn ⊢, rw show (x - n : ℚ_[p]) = (x - r) + (r - n), by ring, apply lt_of_le_of_lt (padic_norm_e.nonarchimedean _ _), apply max_lt hr, simpa using hn end /-- `zmod_repr x` is the unique natural number smaller than `p` satisfying `∥(x - zmod_repr x : ℤ_[p])∥ < 1`. -/ def zmod_repr : ℕ := classical.some (exists_mem_range x) lemma zmod_repr_spec : zmod_repr x < p ∧ (x - zmod_repr x ∈ maximal_ideal ℤ_[p]) := classical.some_spec (exists_mem_range x) lemma zmod_repr_lt_p : zmod_repr x < p := (zmod_repr_spec _).1 lemma sub_zmod_repr_mem : (x - zmod_repr x ∈ maximal_ideal ℤ_[p]) := (zmod_repr_spec _).2 /-- `to_zmod_hom` is an auxiliary constructor for creating ring homs from `ℤ_[p]` to `zmod v`. -/ def to_zmod_hom (v : ℕ) (f : ℤ_[p] → ℕ) (f_spec : ∀ x, x - f x ∈ (ideal.span {v} : ideal ℤ_[p])) (f_congr : ∀ (x : ℤ_[p]) (a b : ℕ), x - a ∈ (ideal.span {v} : ideal ℤ_[p]) → x - b ∈ (ideal.span {v} : ideal ℤ_[p]) → (a : zmod v) = b) : ℤ_[p] →+* zmod v := { to_fun := λ x, f x, map_zero' := begin rw [f_congr (0 : ℤ_[p]) _ 0, cast_zero], { exact f_spec _ }, { simp only [sub_zero, cast_zero, submodule.zero_mem], } end, map_one' := begin rw [f_congr (1 : ℤ_[p]) _ 1, cast_one], { exact f_spec _ }, { simp only [sub_self, cast_one, submodule.zero_mem], } end, map_add' := begin intros x y, rw [f_congr (x + y) _ (f x + f y), cast_add], { exact f_spec _ }, { convert ideal.add_mem _ (f_spec x) (f_spec y), rw cast_add, ring, } end, map_mul' := begin intros x y, rw [f_congr (x * y) _ (f x * f y), cast_mul], { exact f_spec _ }, { let I : ideal ℤ_[p] := ideal.span {v}, convert I.add_mem (I.mul_mem_left x (f_spec y)) (I.mul_mem_right (f y) (f_spec x)), rw cast_mul, ring, } end, } /-- `to_zmod` is a ring hom from `ℤ_[p]` to `zmod p`, with the equality `to_zmod x = (zmod_repr x : zmod p)`. -/ def to_zmod : ℤ_[p] →+* zmod p := to_zmod_hom p zmod_repr (by { rw ←maximal_ideal_eq_span_p, exact sub_zmod_repr_mem }) (by { rw ←maximal_ideal_eq_span_p, exact zmod_congr_of_sub_mem_max_ideal } ) /-- `z - (to_zmod z : ℤ_[p])` is contained in the maximal ideal of `ℤ_[p]`, for every `z : ℤ_[p]`. The coercion from `zmod p` to `ℤ_[p]` is `zmod.has_coe_t`, which coerces `zmod p` into artibrary rings. This is unfortunate, but a consequence of the fact that we allow `zmod p` to coerce to rings of arbitrary characteristic, instead of only rings of characteristic `p`. This coercion is only a ring homomorphism if it coerces into a ring whose characteristic divides `p`. While this is not the case here we can still make use of the coercion. -/ lemma to_zmod_spec (z : ℤ_[p]) : z - (to_zmod z : ℤ_[p]) ∈ maximal_ideal ℤ_[p] := begin convert sub_zmod_repr_mem z using 2, dsimp [to_zmod, to_zmod_hom], unfreezingI { rcases (exists_eq_add_of_lt (hp_prime.1.pos)) with ⟨p', rfl⟩ }, change ↑(zmod.val _) = _, simp only [zmod.val_nat_cast, add_zero, add_def, nat.cast_inj, zero_add], apply mod_eq_of_lt, simpa only [zero_add] using zmod_repr_lt_p z, end lemma ker_to_zmod : (to_zmod : ℤ_[p] →+* zmod p).ker = maximal_ideal ℤ_[p] := begin ext x, rw ring_hom.mem_ker, split, { intro h, simpa only [h, zmod.cast_zero, sub_zero] using to_zmod_spec x, }, { intro h, rw ← sub_zero x at h, dsimp [to_zmod, to_zmod_hom], convert zmod_congr_of_sub_mem_max_ideal x _ 0 _ h, apply sub_zmod_repr_mem, } end /-- `appr n x` gives a value `v : ℕ` such that `x` and `↑v : ℤ_p` are congruent mod `p^n`. See `appr_spec`. -/ noncomputable def appr : ℤ_[p] → ℕ → ℕ | x 0 := 0 | x (n+1) := let y := x - appr x n in if hy : y = 0 then appr x n else let u := unit_coeff hy in appr x n + p ^ n * (to_zmod ((u : ℤ_[p]) * (p ^ (y.valuation - n).nat_abs))).val lemma appr_lt (x : ℤ_[p]) (n : ℕ) : x.appr n < p ^ n := begin induction n with n ih generalizing x, { simp only [appr, succ_pos', pow_zero], }, simp only [appr, ring_hom.map_nat_cast, zmod.nat_cast_self, ring_hom.map_pow, int.nat_abs, ring_hom.map_mul], have hp : p ^ n < p ^ (n + 1), { apply pow_lt_pow hp_prime.1.one_lt (lt_add_one n) }, split_ifs with h, { apply lt_trans (ih _) hp, }, { calc _ < p ^ n + p ^ n * (p - 1) : _ ... = p ^ (n + 1) : _, { apply add_lt_add_of_lt_of_le (ih _), apply nat.mul_le_mul_left, apply le_pred_of_lt, apply zmod.val_lt }, { rw [mul_tsub, mul_one, ← pow_succ'], apply add_tsub_cancel_of_le (le_of_lt hp) } } end lemma appr_mono (x : ℤ_[p]) : monotone x.appr := begin apply monotone_nat_of_le_succ, intro n, dsimp [appr], split_ifs, { refl, }, apply nat.le_add_right, end lemma dvd_appr_sub_appr (x : ℤ_[p]) (m n : ℕ) (h : m ≤ n) : p ^ m ∣ x.appr n - x.appr m := begin obtain ⟨k, rfl⟩ := nat.exists_eq_add_of_le h, clear h, induction k with k ih, { simp only [add_zero, tsub_self, dvd_zero], }, rw [nat.succ_eq_add_one, ← add_assoc], dsimp [appr], split_ifs with h, { exact ih }, rw [add_comm, add_tsub_assoc_of_le (appr_mono _ (nat.le_add_right m k))], apply dvd_add _ ih, apply dvd_mul_of_dvd_left, apply pow_dvd_pow _ (nat.le_add_right m k), end lemma appr_spec (n : ℕ) : ∀ (x : ℤ_[p]), x - appr x n ∈ (ideal.span {p^n} : ideal ℤ_[p]) := begin simp only [ideal.mem_span_singleton], induction n with n ih, { simp only [is_unit_one, is_unit.dvd, pow_zero, forall_true_iff], }, intro x, dsimp only [appr], split_ifs with h, { rw h, apply dvd_zero }, push_cast, rw sub_add_eq_sub_sub, obtain ⟨c, hc⟩ := ih x, simp only [ring_hom.map_nat_cast, zmod.nat_cast_self, ring_hom.map_pow, ring_hom.map_mul, zmod.nat_cast_val], have hc' : c ≠ 0, { rintro rfl, simp only [mul_zero] at hc, contradiction }, conv_rhs { congr, simp only [hc], }, rw show (x - ↑(appr x n)).valuation = (↑p ^ n * c).valuation, { rw hc }, rw [valuation_p_pow_mul _ _ hc', add_sub_cancel', pow_succ', ← mul_sub], apply mul_dvd_mul_left, obtain hc0 | hc0 := c.valuation.nat_abs.eq_zero_or_pos, { simp only [hc0, mul_one, pow_zero], rw [mul_comm, unit_coeff_spec h] at hc, suffices : c = unit_coeff h, { rw [← this, ← ideal.mem_span_singleton, ← maximal_ideal_eq_span_p], apply to_zmod_spec }, obtain ⟨c, rfl⟩ : is_unit c, -- TODO: write a can_lift instance for units { rw int.nat_abs_eq_zero at hc0, rw [is_unit_iff, norm_eq_pow_val hc', hc0, neg_zero, zpow_zero], }, rw discrete_valuation_ring.unit_mul_pow_congr_unit _ _ _ _ _ hc, exact irreducible_p }, { rw zero_pow hc0, simp only [sub_zero, zmod.cast_zero, mul_zero], rw unit_coeff_spec hc', exact (dvd_pow_self _ hc0.ne').mul_left _ } end attribute [irreducible] appr /-- A ring hom from `ℤ_[p]` to `zmod (p^n)`, with underlying function `padic_int.appr n`. -/ def to_zmod_pow (n : ℕ) : ℤ_[p] →+* zmod (p ^ n) := to_zmod_hom (p^n) (λ x, appr x n) (by { intros, convert appr_spec n _ using 1, simp }) (by { intros x a b ha hb, apply zmod_congr_of_sub_mem_span n x a b, { simpa using ha }, { simpa using hb } }) lemma ker_to_zmod_pow (n : ℕ) : (to_zmod_pow n : ℤ_[p] →+* zmod (p ^ n)).ker = ideal.span {p ^ n} := begin ext x, rw ring_hom.mem_ker, split, { intro h, suffices : x.appr n = 0, { convert appr_spec n x, simp only [this, sub_zero, cast_zero], }, dsimp [to_zmod_pow, to_zmod_hom] at h, rw zmod.nat_coe_zmod_eq_zero_iff_dvd at h, apply eq_zero_of_dvd_of_lt h (appr_lt _ _), }, { intro h, rw ← sub_zero x at h, dsimp [to_zmod_pow, to_zmod_hom], rw [zmod_congr_of_sub_mem_span n x _ 0 _ h, cast_zero], apply appr_spec, } end @[simp] lemma zmod_cast_comp_to_zmod_pow (m n : ℕ) (h : m ≤ n) : (zmod.cast_hom (pow_dvd_pow p h) (zmod (p ^ m))).comp (to_zmod_pow n) = to_zmod_pow m := begin apply zmod.ring_hom_eq_of_ker_eq, ext x, rw [ring_hom.mem_ker, ring_hom.mem_ker], simp only [function.comp_app, zmod.cast_hom_apply, ring_hom.coe_comp], simp only [to_zmod_pow, to_zmod_hom, ring_hom.coe_mk], rw [zmod.cast_nat_cast (pow_dvd_pow p h), zmod_congr_of_sub_mem_span m (x.appr n) (x.appr n) (x.appr m)], { rw [sub_self], apply ideal.zero_mem _, }, { rw ideal.mem_span_singleton, rcases dvd_appr_sub_appr x m n h with ⟨c, hc⟩, use c, rw [← nat.cast_sub (appr_mono _ h), hc, nat.cast_mul, nat.cast_pow], }, { apply_instance } end @[simp] lemma cast_to_zmod_pow (m n : ℕ) (h : m ≤ n) (x : ℤ_[p]) : ↑(to_zmod_pow n x) = to_zmod_pow m x := by { rw ← zmod_cast_comp_to_zmod_pow _ _ h, refl } lemma dense_range_nat_cast : dense_range (nat.cast : ℕ → ℤ_[p]) := begin intro x, rw metric.mem_closure_range_iff, intros ε hε, obtain ⟨n, hn⟩ := exists_pow_neg_lt p hε, use (x.appr n), rw dist_eq_norm, apply lt_of_le_of_lt _ hn, rw norm_le_pow_iff_mem_span_pow, apply appr_spec, end lemma dense_range_int_cast : dense_range (int.cast : ℤ → ℤ_[p]) := begin intro x, apply dense_range_nat_cast.induction_on x, { exact is_closed_closure, }, { intro a, change (a.cast : ℤ_[p]) with (a : ℤ).cast, apply subset_closure, exact set.mem_range_self _ } end end ring_homs section lift /-! ### Universal property as projective limit -/ open cau_seq padic_seq variables {R : Type*} [comm_ring R] (f : Π k : ℕ, R →+* zmod (p^k)) (f_compat : ∀ k1 k2 (hk : k1 ≤ k2), (zmod.cast_hom (pow_dvd_pow p hk) _).comp (f k2) = f k1) omit hp_prime /-- Given a family of ring homs `f : Π n : ℕ, R →+* zmod (p ^ n)`, `nth_hom f r` is an integer-valued sequence whose `n`th value is the unique integer `k` such that `0 ≤ k < p ^ n` and `f n r = (k : zmod (p ^ n))`. -/ def nth_hom (r : R) : ℕ → ℤ := λ n, (f n r : zmod (p^n)).val @[simp] lemma nth_hom_zero : nth_hom f 0 = 0 := by simp [nth_hom]; refl variable {f} include hp_prime include f_compat lemma pow_dvd_nth_hom_sub (r : R) (i j : ℕ) (h : i ≤ j) : ↑p ^ i ∣ nth_hom f r j - nth_hom f r i := begin specialize f_compat i j h, rw [← int.coe_nat_pow, ← zmod.int_coe_zmod_eq_zero_iff_dvd, int.cast_sub], dsimp [nth_hom], rw [← f_compat, ring_hom.comp_apply], have : fact (p ^ i > 0) := ⟨pow_pos hp_prime.1.pos _⟩, have : fact (p ^ j > 0) := ⟨pow_pos hp_prime.1.pos _⟩, unfreezingI { simp only [zmod.cast_id, zmod.cast_hom_apply, sub_self, zmod.nat_cast_val], }, end lemma is_cau_seq_nth_hom (r : R): is_cau_seq (padic_norm p) (λ n, nth_hom f r n) := begin intros ε hε, obtain ⟨k, hk⟩ : ∃ k : ℕ, (p ^ - (↑(k : ℕ) : ℤ) : ℚ) < ε := exists_pow_neg_lt_rat p hε, use k, intros j hj, refine lt_of_le_of_lt _ hk, norm_cast, rw ← padic_norm.dvd_iff_norm_le, exact_mod_cast pow_dvd_nth_hom_sub f_compat r k j hj end /-- `nth_hom_seq f_compat r` bundles `padic_int.nth_hom f r` as a Cauchy sequence of rationals with respect to the `p`-adic norm. The `n`th value of the sequence is `((f n r).val : ℚ)`. -/ def nth_hom_seq (r : R) : padic_seq p := ⟨λ n, nth_hom f r n, is_cau_seq_nth_hom f_compat r⟩ lemma nth_hom_seq_one : nth_hom_seq f_compat 1 ≈ 1 := begin intros ε hε, change _ < _ at hε, use 1, intros j hj, haveI : fact (1 < p^j) := ⟨nat.one_lt_pow _ _ (by linarith) hp_prime.1.one_lt⟩, simp [nth_hom_seq, nth_hom, zmod.val_one, hε], end lemma nth_hom_seq_add (r s : R) : nth_hom_seq f_compat (r + s) ≈ nth_hom_seq f_compat r + nth_hom_seq f_compat s := begin intros ε hε, obtain ⟨n, hn⟩ := exists_pow_neg_lt_rat p hε, use n, intros j hj, dsimp [nth_hom_seq], apply lt_of_le_of_lt _ hn, rw [← int.cast_add, ← int.cast_sub, ← padic_norm.dvd_iff_norm_le, ← zmod.int_coe_zmod_eq_zero_iff_dvd], dsimp [nth_hom], have : fact (p ^ n > 0) := ⟨pow_pos hp_prime.1.pos _⟩, have : fact (p ^ j > 0) := ⟨pow_pos hp_prime.1.pos _⟩, unfreezingI { simp only [int.cast_coe_nat, int.cast_add, ring_hom.map_add, int.cast_sub, zmod.nat_cast_val] }, rw [zmod.cast_add (show p ^ n ∣ p ^ j, from _), sub_self], { apply_instance }, { apply pow_dvd_pow, linarith only [hj] }, end lemma nth_hom_seq_mul (r s : R) : nth_hom_seq f_compat (r * s) ≈ nth_hom_seq f_compat r * nth_hom_seq f_compat s := begin intros ε hε, obtain ⟨n, hn⟩ := exists_pow_neg_lt_rat p hε, use n, intros j hj, dsimp [nth_hom_seq], apply lt_of_le_of_lt _ hn, rw [← int.cast_mul, ← int.cast_sub, ← padic_norm.dvd_iff_norm_le, ← zmod.int_coe_zmod_eq_zero_iff_dvd], dsimp [nth_hom], have : fact (p ^ n > 0) := ⟨pow_pos hp_prime.1.pos _⟩, have : fact (p ^ j > 0) := ⟨pow_pos hp_prime.1.pos _⟩, unfreezingI { simp only [int.cast_coe_nat, int.cast_mul, int.cast_sub, ring_hom.map_mul, zmod.nat_cast_val] }, rw [zmod.cast_mul (show p ^ n ∣ p ^ j, from _), sub_self], { apply_instance }, { apply pow_dvd_pow, linarith only [hj] }, end /-- `lim_nth_hom f_compat r` is the limit of a sequence `f` of compatible ring homs `R →+* zmod (p^k)`. This is itself a ring hom: see `padic_int.lift`. -/ def lim_nth_hom (r : R) : ℤ_[p] := of_int_seq (nth_hom f r) (is_cau_seq_nth_hom f_compat r) lemma lim_nth_hom_spec (r : R) : ∀ ε : ℝ, 0 < ε → ∃ N : ℕ, ∀ n ≥ N, ∥lim_nth_hom f_compat r - nth_hom f r n∥ < ε := begin intros ε hε, obtain ⟨ε', hε'0, hε'⟩ : ∃ v : ℚ, (0 : ℝ) < v ∧ ↑v < ε := exists_rat_btwn hε, norm_cast at hε'0, obtain ⟨N, hN⟩ := padic_norm_e.defn (nth_hom_seq f_compat r) hε'0, use N, intros n hn, apply lt_trans _ hε', change ↑(padic_norm_e _) < _, norm_cast, convert hN _ hn, simp [nth_hom, lim_nth_hom, nth_hom_seq, of_int_seq], end lemma lim_nth_hom_zero : lim_nth_hom f_compat 0 = 0 := by simp [lim_nth_hom]; refl lemma lim_nth_hom_one : lim_nth_hom f_compat 1 = 1 := subtype.ext $ quot.sound $ nth_hom_seq_one _ lemma lim_nth_hom_add (r s : R) : lim_nth_hom f_compat (r + s) = lim_nth_hom f_compat r + lim_nth_hom f_compat s := subtype.ext $ quot.sound $ nth_hom_seq_add _ _ _ lemma lim_nth_hom_mul (r s : R) : lim_nth_hom f_compat (r * s) = lim_nth_hom f_compat r * lim_nth_hom f_compat s := subtype.ext $ quot.sound $ nth_hom_seq_mul _ _ _ -- TODO: generalize this to arbitrary complete discrete valuation rings /-- `lift f_compat` is the limit of a sequence `f` of compatible ring homs `R →+* zmod (p^k)`, with the equality `lift f_compat r = padic_int.lim_nth_hom f_compat r`. -/ def lift : R →+* ℤ_[p] := { to_fun := lim_nth_hom f_compat, map_one' := lim_nth_hom_one f_compat, map_mul' := lim_nth_hom_mul f_compat, map_zero' := lim_nth_hom_zero f_compat, map_add' := lim_nth_hom_add f_compat } omit f_compat lemma lift_sub_val_mem_span (r : R) (n : ℕ) : (lift f_compat r - (f n r).val) ∈ (ideal.span {↑p ^ n} : ideal ℤ_[p]) := begin obtain ⟨k, hk⟩ := lim_nth_hom_spec f_compat r _ (show (0 : ℝ) < p ^ (-n : ℤ), from nat.zpow_pos_of_pos hp_prime.1.pos _), have := le_of_lt (hk (max n k) (le_max_right _ _)), rw norm_le_pow_iff_mem_span_pow at this, dsimp [lift], rw sub_eq_sub_add_sub (lim_nth_hom f_compat r) _ ↑(nth_hom f r (max n k)), apply ideal.add_mem _ _ this, rw [ideal.mem_span_singleton], simpa only [ring_hom.eq_int_cast, ring_hom.map_pow, int.cast_sub] using (int.cast_ring_hom ℤ_[p]).map_dvd (pow_dvd_nth_hom_sub f_compat r n (max n k) (le_max_left _ _)), end /-- One part of the universal property of `ℤ_[p]` as a projective limit. See also `padic_int.lift_unique`. -/ lemma lift_spec (n : ℕ) : (to_zmod_pow n).comp (lift f_compat) = f n := begin ext r, haveI : fact (0 < p ^ n) := ⟨pow_pos hp_prime.1.pos n⟩, rw [ring_hom.comp_apply, ← zmod.nat_cast_zmod_val (f n r), ← (to_zmod_pow n).map_nat_cast, ← sub_eq_zero, ← ring_hom.map_sub, ← ring_hom.mem_ker, ker_to_zmod_pow], apply lift_sub_val_mem_span, end /-- One part of the universal property of `ℤ_[p]` as a projective limit. See also `padic_int.lift_spec`. -/ lemma lift_unique (g : R →+* ℤ_[p]) (hg : ∀ n, (to_zmod_pow n).comp g = f n) : lift f_compat = g := begin ext1 r, apply eq_of_forall_dist_le, intros ε hε, obtain ⟨n, hn⟩ := exists_pow_neg_lt p hε, apply le_trans _ (le_of_lt hn), rw [dist_eq_norm, norm_le_pow_iff_mem_span_pow, ← ker_to_zmod_pow, ring_hom.mem_ker, ring_hom.map_sub, ← ring_hom.comp_apply, ← ring_hom.comp_apply, lift_spec, hg, sub_self], end @[simp] lemma lift_self (z : ℤ_[p]) : @lift p _ ℤ_[p] _ to_zmod_pow zmod_cast_comp_to_zmod_pow z = z := begin show _ = ring_hom.id _ z, rw @lift_unique p _ ℤ_[p] _ _ zmod_cast_comp_to_zmod_pow (ring_hom.id ℤ_[p]), intro, rw ring_hom.comp_id, end end lift lemma ext_of_to_zmod_pow {x y : ℤ_[p]} : (∀ n, to_zmod_pow n x = to_zmod_pow n y) ↔ x = y := begin split, { intro h, rw [← lift_self x, ← lift_self y], simp [lift, lim_nth_hom, nth_hom, h] }, { rintro rfl _, refl } end lemma to_zmod_pow_eq_iff_ext {R : Type*} [comm_ring R] {g g' : R →+* ℤ_[p]} : (∀ n, (to_zmod_pow n).comp g = (to_zmod_pow n).comp g') ↔ g = g' := begin split, { intro hg, ext x : 1, apply ext_of_to_zmod_pow.mp, intro n, show (to_zmod_pow n).comp g x = (to_zmod_pow n).comp g' x, rw hg n }, { rintro rfl _, refl } end end padic_int
6d40736a2a2b64df3fb6e7806bc89bd4d87240db
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/analysis/mean_inequalities.lean
413de35b55e64a84e3ef9dd36b088e5b6987305e
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
52,363
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 analysis.special_functions.pow import data.real.conjugate_exponents import tactic.nth_rewrite import measure_theory.integration /-! # Mean value inequalities In this file we prove several inequalities, including AM-GM inequality, Young's inequality, Hölder inequality, and Minkowski inequality. ## 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. Hölder's inequality for the Lebesgue integral of `ℝ≥0∞` and `ℝ≥0` functions: we prove `∫ (f * g) ∂μ ≤ (∫ f^p ∂μ) ^ (1/p) * (∫ g^q ∂μ) ^ (1/q)` for `p`, `q` conjugate real exponents and `α→(e)nnreal` functions in two cases, * `ennreal.lintegral_mul_le_Lp_mul_Lq` : ℝ≥0∞ functions, * `nnreal.lintegral_mul_le_Lp_mul_Lq` : ℝ≥0 functions. ### 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. Minkowski's inequality for the Lebesgue integral of measurable functions with `ℝ≥0∞` values: we prove `(∫ (f + g)^p ∂μ) ^ (1/p) ≤ (∫ f^p ∂μ) ^ (1/p) + (∫ g^p ∂μ) ^ (1/p)` for `1 ≤ p`. ## 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. - prove integral versions of these inequalities. -/ 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, fin.prod_univ_zero, fin.sum_univ_zero, 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, fin.prod_univ_zero, fin.sum_univ_zero, 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, fin.prod_univ_zero, fin.sum_univ_zero, 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 / nnreal.of_real p + b ^ q / nnreal.of_real q := begin nth_rewrite 0 ←coe_of_real p hpq.nonneg, nth_rewrite 0 ←coe_of_real 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 canonically_ordered_semiring.mul_le_mul (le_refl _) (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 canonically_ordered_semiring.mul_le_mul (le_refl _) (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 (nnreal.of_real p) _ (by simp [hpq.pos]), ←@coe_div (nnreal.of_real 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 section lintegral /-! ### Hölder's inequality for the Lebesgue integral of ℝ≥0∞ and nnreal functions We prove `∫ (f * g) ∂μ ≤ (∫ f^p ∂μ) ^ (1/p) * (∫ g^q ∂μ) ^ (1/q)` for `p`, `q` conjugate real exponents and `α→(e)nnreal` functions in several cases, the first two being useful only to prove the more general results: * `ennreal.lintegral_mul_le_one_of_lintegral_rpow_eq_one` : ℝ≥0∞ functions for which the integrals on the right are equal to 1, * `ennreal.lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_ne_top` : ℝ≥0∞ functions for which the integrals on the right are neither ⊤ nor 0, * `ennreal.lintegral_mul_le_Lp_mul_Lq` : ℝ≥0∞ functions, * `nnreal.lintegral_mul_le_Lp_mul_Lq` : nnreal functions. -/ open measure_theory variables {α : Type*} [measurable_space α] {μ : measure α} namespace ennreal lemma lintegral_mul_le_one_of_lintegral_rpow_eq_one {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : α → ℝ≥0∞} (hf : ae_measurable f μ) (hg : ae_measurable g μ) (hf_norm : ∫⁻ a, (f a)^p ∂μ = 1) (hg_norm : ∫⁻ a, (g a)^q ∂μ = 1) : ∫⁻ a, (f * g) a ∂μ ≤ 1 := begin calc ∫⁻ (a : α), ((f * g) a) ∂μ ≤ ∫⁻ (a : α), ((f a)^p / ennreal.of_real p + (g a)^q / ennreal.of_real q) ∂μ : lintegral_mono (λ a, young_inequality (f a) (g a) hpq) ... = 1 : begin simp only [div_eq_mul_inv], rw lintegral_add', { rw [lintegral_mul_const'' _ (hf.pow_const p), lintegral_mul_const'' _ (hg.pow_const q), hf_norm, hg_norm, ← div_eq_mul_inv, ← div_eq_mul_inv, hpq.inv_add_inv_conj_ennreal], }, { exact (hf.pow_const _).mul_const _, }, { exact (hg.pow_const _).mul_const _, }, end end /-- Function multiplied by the inverse of its p-seminorm `(∫⁻ f^p ∂μ) ^ 1/p`-/ def fun_mul_inv_snorm (f : α → ℝ≥0∞) (p : ℝ) (μ : measure α) : α → ℝ≥0∞ := λ a, (f a) * ((∫⁻ c, (f c) ^ p ∂μ) ^ (1 / p))⁻¹ lemma fun_eq_fun_mul_inv_snorm_mul_snorm {p : ℝ} (f : α → ℝ≥0∞) (hf_nonzero : ∫⁻ a, (f a) ^ p ∂μ ≠ 0) (hf_top : ∫⁻ a, (f a) ^ p ∂μ ≠ ⊤) {a : α} : f a = (fun_mul_inv_snorm f p μ a) * (∫⁻ c, (f c)^p ∂μ)^(1/p) := by simp [fun_mul_inv_snorm, mul_assoc, inv_mul_cancel, hf_nonzero, hf_top] lemma fun_mul_inv_snorm_rpow {p : ℝ} (hp0 : 0 < p) {f : α → ℝ≥0∞} {a : α} : (fun_mul_inv_snorm f p μ a) ^ p = (f a)^p * (∫⁻ c, (f c) ^ p ∂μ)⁻¹ := begin rw [fun_mul_inv_snorm, mul_rpow_of_nonneg _ _ (le_of_lt hp0)], suffices h_inv_rpow : ((∫⁻ (c : α), f c ^ p ∂μ) ^ (1 / p))⁻¹ ^ p = (∫⁻ (c : α), f c ^ p ∂μ)⁻¹, by rw h_inv_rpow, rw [inv_rpow_of_pos hp0, ←rpow_mul, div_eq_mul_inv, one_mul, _root_.inv_mul_cancel (ne_of_lt hp0).symm, rpow_one], end lemma lintegral_rpow_fun_mul_inv_snorm_eq_one {p : ℝ} (hp0_lt : 0 < p) {f : α → ℝ≥0∞} (hf : ae_measurable f μ) (hf_nonzero : ∫⁻ a, (f a)^p ∂μ ≠ 0) (hf_top : ∫⁻ a, (f a)^p ∂μ ≠ ⊤) : ∫⁻ c, (fun_mul_inv_snorm f p μ c)^p ∂μ = 1 := begin simp_rw fun_mul_inv_snorm_rpow hp0_lt, rw [lintegral_mul_const'' _ (hf.pow_const p), mul_inv_cancel hf_nonzero hf_top], end /-- Hölder's inequality in case of finite non-zero integrals -/ lemma lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_ne_top {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : α → ℝ≥0∞} (hf : ae_measurable f μ) (hg : ae_measurable g μ) (hf_nontop : ∫⁻ a, (f a)^p ∂μ ≠ ⊤) (hg_nontop : ∫⁻ a, (g a)^q ∂μ ≠ ⊤) (hf_nonzero : ∫⁻ a, (f a)^p ∂μ ≠ 0) (hg_nonzero : ∫⁻ a, (g a)^q ∂μ ≠ 0) : ∫⁻ a, (f * g) a ∂μ ≤ (∫⁻ a, (f a)^p ∂μ)^(1/p) * (∫⁻ a, (g a)^q ∂μ)^(1/q) := begin let npf := (∫⁻ (c : α), (f c) ^ p ∂μ) ^ (1/p), let nqg := (∫⁻ (c : α), (g c) ^ q ∂μ) ^ (1/q), calc ∫⁻ (a : α), (f * g) a ∂μ = ∫⁻ (a : α), ((fun_mul_inv_snorm f p μ * fun_mul_inv_snorm g q μ) a) * (npf * nqg) ∂μ : begin refine lintegral_congr (λ a, _), rw [pi.mul_apply, fun_eq_fun_mul_inv_snorm_mul_snorm f hf_nonzero hf_nontop, fun_eq_fun_mul_inv_snorm_mul_snorm g hg_nonzero hg_nontop, pi.mul_apply], ring, end ... ≤ npf * nqg : begin rw lintegral_mul_const' (npf * nqg) _ (by simp [hf_nontop, hg_nontop, hf_nonzero, hg_nonzero]), nth_rewrite 1 ←one_mul (npf * nqg), refine mul_le_mul _ (le_refl (npf * nqg)), have hf1 := lintegral_rpow_fun_mul_inv_snorm_eq_one hpq.pos hf hf_nonzero hf_nontop, have hg1 := lintegral_rpow_fun_mul_inv_snorm_eq_one hpq.symm.pos hg hg_nonzero hg_nontop, exact lintegral_mul_le_one_of_lintegral_rpow_eq_one hpq (hf.mul_const _) (hg.mul_const _) hf1 hg1, end end lemma ae_eq_zero_of_lintegral_rpow_eq_zero {p : ℝ} (hp0_lt : 0 < p) {f : α → ℝ≥0∞} (hf : ae_measurable f μ) (hf_zero : ∫⁻ a, (f a)^p ∂μ = 0) : f =ᵐ[μ] 0 := begin rw lintegral_eq_zero_iff' (hf.pow_const p) at hf_zero, refine filter.eventually.mp hf_zero (filter.eventually_of_forall (λ x, _)), dsimp only, rw [pi.zero_apply, rpow_eq_zero_iff], intro hx, cases hx, { exact hx.left, }, { exfalso, linarith, }, end lemma lintegral_mul_eq_zero_of_lintegral_rpow_eq_zero {p : ℝ} (hp0_lt : 0 < p) {f g : α → ℝ≥0∞} (hf : ae_measurable f μ) (hf_zero : ∫⁻ a, (f a)^p ∂μ = 0) : ∫⁻ a, (f * g) a ∂μ = 0 := begin rw ←@lintegral_zero_fun α _ μ, refine lintegral_congr_ae _, suffices h_mul_zero : f * g =ᵐ[μ] 0 * g , by rwa zero_mul at h_mul_zero, have hf_eq_zero : f =ᵐ[μ] 0, from ae_eq_zero_of_lintegral_rpow_eq_zero hp0_lt hf hf_zero, exact filter.eventually_eq.mul hf_eq_zero (ae_eq_refl g), end lemma lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_eq_top {p q : ℝ} (hp0_lt : 0 < p) (hq0 : 0 ≤ q) {f g : α → ℝ≥0∞} (hf_top : ∫⁻ a, (f a)^p ∂μ = ⊤) (hg_nonzero : ∫⁻ a, (g a)^q ∂μ ≠ 0) : ∫⁻ a, (f * g) a ∂μ ≤ (∫⁻ a, (f a)^p ∂μ) ^ (1/p) * (∫⁻ a, (g a)^q ∂μ) ^ (1/q) := begin refine le_trans le_top (le_of_eq _), have hp0_inv_lt : 0 < 1/p, by simp [hp0_lt], rw [hf_top, ennreal.top_rpow_of_pos hp0_inv_lt], simp [hq0, hg_nonzero], end /-- Hölder's inequality for functions `α → ℝ≥0∞`. The integral of the product of two functions is bounded by the product of their `ℒp` and `ℒq` seminorms when `p` and `q` are conjugate exponents. -/ theorem lintegral_mul_le_Lp_mul_Lq (μ : measure α) {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : α → ℝ≥0∞} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ∫⁻ a, (f * g) a ∂μ ≤ (∫⁻ a, (f a)^p ∂μ) ^ (1/p) * (∫⁻ a, (g a)^q ∂μ) ^ (1/q) := begin by_cases hf_zero : ∫⁻ a, (f a) ^ p ∂μ = 0, { refine le_trans (le_of_eq _) (zero_le _), exact lintegral_mul_eq_zero_of_lintegral_rpow_eq_zero hpq.pos hf hf_zero, }, by_cases hg_zero : ∫⁻ a, (g a) ^ q ∂μ = 0, { refine le_trans (le_of_eq _) (zero_le _), rw mul_comm, exact lintegral_mul_eq_zero_of_lintegral_rpow_eq_zero hpq.symm.pos hg hg_zero, }, by_cases hf_top : ∫⁻ a, (f a) ^ p ∂μ = ⊤, { exact lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_eq_top hpq.pos hpq.symm.nonneg hf_top hg_zero, }, by_cases hg_top : ∫⁻ a, (g a) ^ q ∂μ = ⊤, { rw [mul_comm, mul_comm ((∫⁻ (a : α), (f a) ^ p ∂μ) ^ (1 / p))], exact lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_eq_top hpq.symm.pos hpq.nonneg hg_top hf_zero, }, -- non-⊤ non-zero case exact ennreal.lintegral_mul_le_Lp_mul_Lq_of_ne_zero_of_ne_top hpq hf hg hf_top hg_top hf_zero hg_zero, end lemma lintegral_rpow_add_lt_top_of_lintegral_rpow_lt_top {p : ℝ} {f g : α → ℝ≥0∞} (hf : ae_measurable f μ) (hf_top : ∫⁻ a, (f a) ^ p ∂μ < ⊤) (hg : ae_measurable g μ) (hg_top : ∫⁻ a, (g a) ^ p ∂μ < ⊤) (hp1 : 1 ≤ p) : ∫⁻ a, ((f + g) a) ^ p ∂μ < ⊤ := begin have hp0_lt : 0 < p, from lt_of_lt_of_le zero_lt_one hp1, have hp0 : 0 ≤ p, from le_of_lt hp0_lt, calc ∫⁻ (a : α), (f a + g a) ^ p ∂μ ≤ ∫⁻ a, ((2:ℝ≥0∞)^(p-1) * (f a) ^ p + (2:ℝ≥0∞)^(p-1) * (g a) ^ p) ∂ μ : begin refine lintegral_mono (λ a, _), dsimp only, have h_zero_lt_half_rpow : (0 : ℝ≥0∞) < (1 / 2) ^ p, { rw [←ennreal.zero_rpow_of_pos hp0_lt], exact ennreal.rpow_lt_rpow (by simp [zero_lt_one]) hp0_lt, }, have h_rw : (1 / 2) ^ p * (2:ℝ≥0∞) ^ (p - 1) = 1 / 2, { rw [sub_eq_add_neg, ennreal.rpow_add _ _ ennreal.two_ne_zero ennreal.coe_ne_top, ←mul_assoc, ←ennreal.mul_rpow_of_nonneg _ _ hp0, one_div, ennreal.inv_mul_cancel ennreal.two_ne_zero ennreal.coe_ne_top, ennreal.one_rpow, one_mul, ennreal.rpow_neg_one], }, rw ←ennreal.mul_le_mul_left (ne_of_lt h_zero_lt_half_rpow).symm _, { rw [mul_add, ← mul_assoc, ← mul_assoc, h_rw, ←ennreal.mul_rpow_of_nonneg _ _ hp0, mul_add], refine ennreal.rpow_arith_mean_le_arith_mean2_rpow (1/2 : ℝ≥0∞) (1/2 : ℝ≥0∞) (f a) (g a) _ hp1, rw [ennreal.div_add_div_same, one_add_one_eq_two, ennreal.div_self ennreal.two_ne_zero ennreal.coe_ne_top], }, { rw ←ennreal.lt_top_iff_ne_top, refine ennreal.rpow_lt_top_of_nonneg hp0 _, rw [one_div, ennreal.inv_ne_top], exact ennreal.two_ne_zero, }, end ... < ⊤ : begin rw [lintegral_add', lintegral_const_mul'' _ (hf.pow_const p), lintegral_const_mul'' _ (hg.pow_const p), ennreal.add_lt_top], { have h_two : (2 : ℝ≥0∞) ^ (p - 1) < ⊤, from ennreal.rpow_lt_top_of_nonneg (by simp [hp1]) ennreal.coe_ne_top, repeat {rw ennreal.mul_lt_top_iff}, simp [hf_top, hg_top, h_two], }, { exact (hf.pow_const _).const_mul _ }, { exact (hg.pow_const _).const_mul _ }, end end lemma lintegral_Lp_mul_le_Lq_mul_Lr {α} [measurable_space α] {p q r : ℝ} (hp0_lt : 0 < p) (hpq : p < q) (hpqr : 1/p = 1/q + 1/r) (μ : measure α) {f g : α → ℝ≥0∞} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : (∫⁻ a, ((f * g) a)^p ∂μ) ^ (1/p) ≤ (∫⁻ a, (f a)^q ∂μ) ^ (1/q) * (∫⁻ a, (g a)^r ∂μ) ^ (1/r) := begin have hp0_ne : p ≠ 0, from (ne_of_lt hp0_lt).symm, have hp0 : 0 ≤ p, from le_of_lt hp0_lt, have hq0_lt : 0 < q, from lt_of_le_of_lt hp0 hpq, have hq0_ne : q ≠ 0, from (ne_of_lt hq0_lt).symm, have h_one_div_r : 1/r = 1/p - 1/q, by simp [hpqr], have hr0_ne : r ≠ 0, { have hr_inv_pos : 0 < 1/r, by rwa [h_one_div_r, sub_pos, one_div_lt_one_div hq0_lt hp0_lt], rw [one_div, _root_.inv_pos] at hr_inv_pos, exact (ne_of_lt hr_inv_pos).symm, }, let p2 := q/p, let q2 := p2.conjugate_exponent, have hp2q2 : p2.is_conjugate_exponent q2, from real.is_conjugate_exponent_conjugate_exponent (by simp [lt_div_iff, hpq, hp0_lt]), calc (∫⁻ (a : α), ((f * g) a) ^ p ∂μ) ^ (1 / p) = (∫⁻ (a : α), (f a)^p * (g a)^p ∂μ) ^ (1 / p) : by simp_rw [pi.mul_apply, ennreal.mul_rpow_of_nonneg _ _ hp0] ... ≤ ((∫⁻ a, (f a)^(p * p2) ∂ μ)^(1/p2) * (∫⁻ a, (g a)^(p * q2) ∂ μ)^(1/q2)) ^ (1/p) : begin refine ennreal.rpow_le_rpow _ (by simp [hp0]), simp_rw ennreal.rpow_mul, exact ennreal.lintegral_mul_le_Lp_mul_Lq μ hp2q2 (hf.pow_const _) (hg.pow_const _) end ... = (∫⁻ (a : α), (f a) ^ q ∂μ) ^ (1 / q) * (∫⁻ (a : α), (g a) ^ r ∂μ) ^ (1 / r) : begin rw [@ennreal.mul_rpow_of_nonneg _ _ (1/p) (by simp [hp0]), ←ennreal.rpow_mul, ←ennreal.rpow_mul], have hpp2 : p * p2 = q, { symmetry, rw [mul_comm, ←div_eq_iff hp0_ne], }, have hpq2 : p * q2 = r, { rw [← inv_inv' r, ← one_div, ← one_div, h_one_div_r], field_simp [q2, real.conjugate_exponent, p2, hp0_ne, hq0_ne] }, simp_rw [div_mul_div, mul_one, mul_comm p2, mul_comm q2, hpp2, hpq2], end end lemma lintegral_mul_rpow_le_lintegral_rpow_mul_lintegral_rpow {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : α → ℝ≥0∞} (hf : ae_measurable f μ) (hg : ae_measurable g μ) (hf_top : ∫⁻ a, (f a) ^ p ∂μ ≠ ⊤) : ∫⁻ a, (f a) * (g a) ^ (p - 1) ∂μ ≤ (∫⁻ a, (f a)^p ∂μ) ^ (1/p) * (∫⁻ a, (g a)^p ∂μ) ^ (1/q) := begin refine le_trans (ennreal.lintegral_mul_le_Lp_mul_Lq μ hpq hf (hg.pow_const _)) _, by_cases hf_zero_rpow : (∫⁻ (a : α), (f a) ^ p ∂μ) ^ (1 / p) = 0, { rw [hf_zero_rpow, zero_mul], exact zero_le _, }, have hf_top_rpow : (∫⁻ (a : α), (f a) ^ p ∂μ) ^ (1 / p) ≠ ⊤, { by_contra h, push_neg at h, refine hf_top _, have hp_not_neg : ¬ p < 0, by simp [hpq.nonneg], simpa [hpq.pos, hp_not_neg] using h, }, refine (ennreal.mul_le_mul_left hf_zero_rpow hf_top_rpow).mpr (le_of_eq _), congr, ext1 a, rw [←ennreal.rpow_mul, hpq.sub_one_mul_conj], end lemma lintegral_rpow_add_le_add_snorm_mul_lintegral_rpow_add {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : α → ℝ≥0∞} (hf : ae_measurable f μ) (hf_top : ∫⁻ a, (f a) ^ p ∂μ ≠ ⊤) (hg : ae_measurable g μ) (hg_top : ∫⁻ a, (g a) ^ p ∂μ ≠ ⊤) : ∫⁻ a, ((f + g) a)^p ∂ μ ≤ ((∫⁻ a, (f a)^p ∂μ) ^ (1/p) + (∫⁻ a, (g a)^p ∂μ) ^ (1/p)) * (∫⁻ a, (f a + g a)^p ∂μ) ^ (1/q) := begin calc ∫⁻ a, ((f+g) a) ^ p ∂μ ≤ ∫⁻ a, ((f + g) a) * ((f + g) a) ^ (p - 1) ∂μ : begin refine lintegral_mono (λ a, _), dsimp only, by_cases h_zero : (f + g) a = 0, { rw [h_zero, ennreal.zero_rpow_of_pos hpq.pos], exact zero_le _, }, by_cases h_top : (f + g) a = ⊤, { rw [h_top, ennreal.top_rpow_of_pos hpq.sub_one_pos, ennreal.top_mul_top], exact le_top, }, refine le_of_eq _, nth_rewrite 1 ←ennreal.rpow_one ((f + g) a), rw [←ennreal.rpow_add _ _ h_zero h_top, add_sub_cancel'_right], end ... = ∫⁻ (a : α), f a * (f + g) a ^ (p - 1) ∂μ + ∫⁻ (a : α), g a * (f + g) a ^ (p - 1) ∂μ : begin have h_add_m : ae_measurable (λ (a : α), ((f + g) a) ^ (p-1)) μ, from (hf.add hg).pow_const _, have h_add_apply : ∫⁻ (a : α), (f + g) a * (f + g) a ^ (p - 1) ∂μ = ∫⁻ (a : α), (f a + g a) * (f + g) a ^ (p - 1) ∂μ, from rfl, simp_rw [h_add_apply, add_mul], rw lintegral_add' (hf.mul h_add_m) (hg.mul h_add_m), end ... ≤ ((∫⁻ a, (f a)^p ∂μ) ^ (1/p) + (∫⁻ a, (g a)^p ∂μ) ^ (1/p)) * (∫⁻ a, (f a + g a)^p ∂μ) ^ (1/q) : begin rw add_mul, exact add_le_add (lintegral_mul_rpow_le_lintegral_rpow_mul_lintegral_rpow hpq hf (hf.add hg) hf_top) (lintegral_mul_rpow_le_lintegral_rpow_mul_lintegral_rpow hpq hg (hf.add hg) hg_top), end end private lemma lintegral_Lp_add_le_aux {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : α → ℝ≥0∞} (hf : ae_measurable f μ) (hf_top : ∫⁻ a, (f a) ^ p ∂μ ≠ ⊤) (hg : ae_measurable g μ) (hg_top : ∫⁻ a, (g a) ^ p ∂μ ≠ ⊤) (h_add_zero : ∫⁻ a, ((f+g) a) ^ p ∂ μ ≠ 0) (h_add_top : ∫⁻ a, ((f+g) a) ^ p ∂ μ ≠ ⊤) : (∫⁻ a, ((f + g) a)^p ∂ μ) ^ (1/p) ≤ (∫⁻ a, (f a)^p ∂μ) ^ (1/p) + (∫⁻ a, (g a)^p ∂μ) ^ (1/p) := begin have hp_not_nonpos : ¬ p ≤ 0, by simp [hpq.pos], have htop_rpow : (∫⁻ a, ((f+g) a) ^ p ∂μ)^(1/p) ≠ ⊤, { by_contra h, push_neg at h, exact h_add_top (@ennreal.rpow_eq_top_of_nonneg _ (1/p) (by simp [hpq.nonneg]) h), }, have h0_rpow : (∫⁻ a, ((f+g) a) ^ p ∂ μ) ^ (1/p) ≠ 0, by simp [h_add_zero, h_add_top, hpq.nonneg, hp_not_nonpos, -pi.add_apply], suffices h : 1 ≤ (∫⁻ (a : α), ((f+g) a)^p ∂μ) ^ -(1/p) * ((∫⁻ (a : α), (f a)^p ∂μ) ^ (1/p) + (∫⁻ (a : α), (g a)^p ∂μ) ^ (1/p)), by rwa [←mul_le_mul_left h0_rpow htop_rpow, ←mul_assoc, ←rpow_add _ _ h_add_zero h_add_top, ←sub_eq_add_neg, _root_.sub_self, rpow_zero, one_mul, mul_one] at h, have h : ∫⁻ (a : α), ((f+g) a)^p ∂μ ≤ ((∫⁻ (a : α), (f a)^p ∂μ) ^ (1/p) + (∫⁻ (a : α), (g a)^p ∂μ) ^ (1/p)) * (∫⁻ (a : α), ((f+g) a)^p ∂μ) ^ (1/q), from lintegral_rpow_add_le_add_snorm_mul_lintegral_rpow_add hpq hf hf_top hg hg_top, have h_one_div_q : 1/q = 1 - 1/p, by { nth_rewrite 1 ←hpq.inv_add_inv_conj, ring, }, simp_rw [h_one_div_q, sub_eq_add_neg 1 (1/p), ennreal.rpow_add _ _ h_add_zero h_add_top, rpow_one] at h, nth_rewrite 1 mul_comm at h, nth_rewrite 0 ←one_mul (∫⁻ (a : α), ((f+g) a) ^ p ∂μ) at h, rwa [←mul_assoc, ennreal.mul_le_mul_right h_add_zero h_add_top, mul_comm] at h, end /-- Minkowski's inequality for functions `α → ℝ≥0∞`: the `ℒp` seminorm of the sum of two functions is bounded by the sum of their `ℒp` seminorms. -/ theorem lintegral_Lp_add_le {p : ℝ} {f g : α → ℝ≥0∞} (hf : ae_measurable f μ) (hg : ae_measurable g μ) (hp1 : 1 ≤ p) : (∫⁻ a, ((f + g) a)^p ∂ μ) ^ (1/p) ≤ (∫⁻ a, (f a)^p ∂μ) ^ (1/p) + (∫⁻ a, (g a)^p ∂μ) ^ (1/p) := begin have hp_pos : 0 < p, from lt_of_lt_of_le zero_lt_one hp1, by_cases hf_top : ∫⁻ a, (f a) ^ p ∂μ = ⊤, { simp [hf_top, hp_pos], }, by_cases hg_top : ∫⁻ a, (g a) ^ p ∂μ = ⊤, { simp [hg_top, hp_pos], }, by_cases h1 : p = 1, { refine le_of_eq _, simp_rw [h1, one_div_one, ennreal.rpow_one], exact lintegral_add' hf hg, }, have hp1_lt : 1 < p, by { refine lt_of_le_of_ne hp1 _, symmetry, exact h1, }, have hpq := real.is_conjugate_exponent_conjugate_exponent hp1_lt, by_cases h0 : ∫⁻ a, ((f+g) a) ^ p ∂ μ = 0, { rw [h0, @ennreal.zero_rpow_of_pos (1/p) (by simp [lt_of_lt_of_le zero_lt_one hp1])], exact zero_le _, }, have htop : ∫⁻ a, ((f+g) a) ^ p ∂ μ ≠ ⊤, { rw ←ne.def at hf_top hg_top, rw ←ennreal.lt_top_iff_ne_top at hf_top hg_top ⊢, exact lintegral_rpow_add_lt_top_of_lintegral_rpow_lt_top hf hf_top hg hg_top hp1, }, exact lintegral_Lp_add_le_aux hpq hf hf_top hg hg_top h0 htop, end end ennreal /-- Hölder's inequality for functions `α → ℝ≥0`. The integral of the product of two functions is bounded by the product of their `ℒp` and `ℒq` seminorms when `p` and `q` are conjugate exponents. -/ theorem nnreal.lintegral_mul_le_Lp_mul_Lq {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : α → ℝ≥0} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ∫⁻ a, (f * g) a ∂μ ≤ (∫⁻ a, (f a)^p ∂μ)^(1/p) * (∫⁻ a, (g a)^q ∂μ)^(1/q) := begin simp_rw [pi.mul_apply, ennreal.coe_mul], exact ennreal.lintegral_mul_le_Lp_mul_Lq μ hpq hf.ennreal_coe hg.ennreal_coe, end end lintegral
1baa6152efd74a82059131eff50a0ef4e22b089b
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/ring_theory/polynomial/homogeneous.lean
f87fb575191374686d7ec9a3fa4d08227daa6220
[ "Apache-2.0" ]
permissive
JLimperg/aesop3
306cc6570c556568897ed2e508c8869667252e8a
a4a116f650cc7403428e72bd2e2c4cda300fe03f
refs/heads/master
1,682,884,916,368
1,620,320,033,000
1,620,320,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,768
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import data.mv_polynomial import algebra.algebra.operations import data.fintype.card import algebra.direct_sum_graded /-! # Homogeneous polynomials A multivariate polynomial `φ` is homogeneous of degree `n` if all monomials occuring in `φ` have degree `n`. ## Main definitions/lemmas * `is_homogeneous φ n`: a predicate that asserts that `φ` is homogeneous of degree `n`. * `homogeneous_submodule σ R n`: the submodule of homogeneous polynomials of degree `n`. * `homogeneous_component n`: the additive morphism that projects polynomials onto their summand that is homogeneous of degree `n`. * `sum_homogeneous_component`: every polynomial is the sum of its homogeneous components -/ open_locale big_operators namespace mv_polynomial variables {σ : Type*} {τ : Type*} {R : Type*} {S : Type*} /- TODO * create definition for `∑ i in d.support, d i` * define graded rings, and show that mv_polynomial is an example -/ /-- A multivariate polynomial `φ` is homogeneous of degree `n` if all monomials occuring in `φ` have degree `n`. -/ def is_homogeneous [comm_semiring R] (φ : mv_polynomial σ R) (n : ℕ) := ∀ ⦃d⦄, coeff d φ ≠ 0 → ∑ i in d.support, d i = n variables (σ R) /-- The submodule of homogeneous `mv_polynomial`s of degree `n`. -/ noncomputable def homogeneous_submodule [comm_semiring R] (n : ℕ) : submodule R (mv_polynomial σ R) := { carrier := { x | x.is_homogeneous n }, smul_mem' := λ r a ha c hc, begin rw coeff_smul at hc, apply ha, intro h, apply hc, rw h, exact smul_zero r, end, zero_mem' := λ d hd, false.elim (hd $ coeff_zero _), add_mem' := λ a b ha hb c hc, begin rw coeff_add at hc, obtain h|h : coeff c a ≠ 0 ∨ coeff c b ≠ 0, { contrapose! hc, simp only [hc, add_zero] }, { exact ha h }, { exact hb h } end } variables {σ R} @[simp] lemma mem_homogeneous_submodule [comm_semiring R] (n : ℕ) (p : mv_polynomial σ R) : p ∈ homogeneous_submodule σ R n ↔ p.is_homogeneous n := iff.rfl variables (σ R) /-- While equal, the former has a convenient definitional reduction. -/ lemma homogenous_submodule_eq_finsupp_supported [comm_semiring R] (n : ℕ) : homogeneous_submodule σ R n = finsupp.supported _ R {d | ∑ i in d.support, d i = n} := begin ext, rw [finsupp.mem_supported, set.subset_def], simp only [finsupp.mem_support_iff, finset.mem_coe], refl, end variables {σ R} lemma homogenous_submodule_mul [comm_semiring R] (m n : ℕ) : homogeneous_submodule σ R m * homogeneous_submodule σ R n ≤ homogeneous_submodule σ R (m + n) := begin rw submodule.mul_le, intros φ hφ ψ hψ c hc, rw [coeff_mul] at hc, obtain ⟨⟨d, e⟩, hde, H⟩ := finset.exists_ne_zero_of_sum_ne_zero hc, have aux : coeff d φ ≠ 0 ∧ coeff e ψ ≠ 0, { contrapose! H, by_cases h : coeff d φ = 0; simp only [*, ne.def, not_false_iff, zero_mul, mul_zero] at * }, specialize hφ aux.1, specialize hψ aux.2, rw finsupp.mem_antidiagonal_support at hde, classical, have hd' : d.support ⊆ d.support ∪ e.support := finset.subset_union_left _ _, have he' : e.support ⊆ d.support ∪ e.support := finset.subset_union_right _ _, rw [← hde, ← hφ, ← hψ, finset.sum_subset (finsupp.support_add), finset.sum_subset hd', finset.sum_subset he', ← finset.sum_add_distrib], { congr }, all_goals { intros i hi, apply finsupp.not_mem_support_iff.mp }, end section variables [comm_semiring R] variables {σ R} lemma is_homogeneous_monomial (d : σ →₀ ℕ) (r : R) (n : ℕ) (hn : ∑ i in d.support, d i = n) : is_homogeneous (monomial d r) n := begin intros c hc, rw coeff_monomial at hc, split_ifs at hc with h, { subst c, exact hn }, { contradiction } end variables (σ) {R} lemma is_homogeneous_C (r : R) : is_homogeneous (C r : mv_polynomial σ R) 0 := begin apply is_homogeneous_monomial, simp only [finsupp.zero_apply, finset.sum_const_zero], end variables (σ R) lemma is_homogeneous_zero (n : ℕ) : is_homogeneous (0 : mv_polynomial σ R) n := (homogeneous_submodule σ R n).zero_mem lemma is_homogeneous_one : is_homogeneous (1 : mv_polynomial σ R) 0 := is_homogeneous_C _ _ variables {σ} (R) lemma is_homogeneous_X (i : σ) : is_homogeneous (X i : mv_polynomial σ R) 1 := begin apply is_homogeneous_monomial, simp only [finsupp.support_single_ne_zero one_ne_zero, finset.sum_singleton], exact finsupp.single_eq_same end end namespace is_homogeneous variables [comm_semiring R] {φ ψ : mv_polynomial σ R} {m n : ℕ} lemma coeff_eq_zero (hφ : is_homogeneous φ n) (d : σ →₀ ℕ) (hd : ∑ i in d.support, d i ≠ n) : coeff d φ = 0 := by { have aux := mt (@hφ d) hd, classical, rwa not_not at aux } lemma inj_right (hm : is_homogeneous φ m) (hn : is_homogeneous φ n) (hφ : φ ≠ 0) : m = n := begin obtain ⟨d, hd⟩ : ∃ d, coeff d φ ≠ 0 := exists_coeff_ne_zero hφ, rw [← hm hd, ← hn hd] end lemma add (hφ : is_homogeneous φ n) (hψ : is_homogeneous ψ n) : is_homogeneous (φ + ψ) n := (homogeneous_submodule σ R n).add_mem hφ hψ lemma sum {ι : Type*} (s : finset ι) (φ : ι → mv_polynomial σ R) (n : ℕ) (h : ∀ i ∈ s, is_homogeneous (φ i) n) : is_homogeneous (∑ i in s, φ i) n := (homogeneous_submodule σ R n).sum_mem h lemma mul (hφ : is_homogeneous φ m) (hψ : is_homogeneous ψ n) : is_homogeneous (φ * ψ) (m + n) := homogenous_submodule_mul m n $ submodule.mul_mem_mul hφ hψ lemma prod {ι : Type*} (s : finset ι) (φ : ι → mv_polynomial σ R) (n : ι → ℕ) (h : ∀ i ∈ s, is_homogeneous (φ i) (n i)) : is_homogeneous (∏ i in s, φ i) (∑ i in s, n i) := begin classical, revert h, apply finset.induction_on s, { intro, simp only [is_homogeneous_one, finset.sum_empty, finset.prod_empty] }, { intros i s his IH h, simp only [his, finset.prod_insert, finset.sum_insert, not_false_iff], apply (h i (finset.mem_insert_self _ _)).mul (IH _), intros j hjs, exact h j (finset.mem_insert_of_mem hjs) } end lemma total_degree (hφ : is_homogeneous φ n) (h : φ ≠ 0) : total_degree φ = n := begin rw total_degree, apply le_antisymm, { apply finset.sup_le, intros d hd, rw mem_support_iff at hd, rw [finsupp.sum, hφ hd], }, { obtain ⟨d, hd⟩ : ∃ d, coeff d φ ≠ 0 := exists_coeff_ne_zero h, simp only [← hφ hd, finsupp.sum], replace hd := finsupp.mem_support_iff.mpr hd, exact finset.le_sup hd, } end /-- The homogenous submodules form a graded ring. This instance is used by `direct_sum.comm_semiring`. -/ noncomputable instance homogeneous_submodule.gcomm_monoid : direct_sum.gcomm_monoid (λ i, homogeneous_submodule σ R i) := direct_sum.gcomm_monoid.of_submodules _ (is_homogeneous_one σ R) (λ i j hi hj, is_homogeneous.mul hi.prop hj.prop) open_locale direct_sum noncomputable example : comm_semiring (⨁ i, homogeneous_submodule σ R i) := infer_instance end is_homogeneous section noncomputable theory open_locale classical open finset /-- `homogeneous_component n φ` is the part of `φ` that is homogeneous of degree `n`. See `sum_homogeneous_component` for the statement that `φ` is equal to the sum of all its homogeneous components. -/ def homogeneous_component [comm_semiring R] (n : ℕ) : mv_polynomial σ R →ₗ[R] mv_polynomial σ R := (submodule.subtype _).comp $ finsupp.restrict_dom _ _ {d | ∑ i in d.support, d i = n} section homogeneous_component open finset variables [comm_semiring R] (n : ℕ) (φ : mv_polynomial σ R) lemma coeff_homogeneous_component (d : σ →₀ ℕ) : coeff d (homogeneous_component n φ) = if ∑ i in d.support, d i = n then coeff d φ else 0 := by convert finsupp.filter_apply (λ d : σ →₀ ℕ, ∑ i in d.support, d i = n) φ d lemma homogeneous_component_apply : homogeneous_component n φ = ∑ d in φ.support.filter (λ d, ∑ i in d.support, d i = n), monomial d (coeff d φ) := by convert finsupp.filter_eq_sum (λ d : σ →₀ ℕ, ∑ i in d.support, d i = n) φ lemma homogeneous_component_is_homogeneous : (homogeneous_component n φ).is_homogeneous n := begin intros d hd, contrapose! hd, rw [coeff_homogeneous_component, if_neg hd] end lemma homogeneous_component_zero : homogeneous_component 0 φ = C (coeff 0 φ) := begin ext1 d, rcases em (d = 0) with (rfl|hd), { simp only [coeff_homogeneous_component, sum_eq_zero_iff, finsupp.zero_apply, if_true, coeff_C, eq_self_iff_true, forall_true_iff] }, { rw [coeff_homogeneous_component, if_neg, coeff_C, if_neg (ne.symm hd)], simp only [finsupp.ext_iff, finsupp.zero_apply] at hd, simp [hd] } end lemma homogeneous_component_eq_zero' (h : ∀ d : σ →₀ ℕ, d ∈ φ.support → ∑ i in d.support, d i ≠ n) : homogeneous_component n φ = 0 := begin rw [homogeneous_component_apply, sum_eq_zero], intros d hd, rw mem_filter at hd, exfalso, exact h _ hd.1 hd.2 end lemma homogeneous_component_eq_zero (h : φ.total_degree < n) : homogeneous_component n φ = 0 := begin apply homogeneous_component_eq_zero', rw [total_degree, finset.sup_lt_iff] at h, { intros d hd, exact ne_of_lt (h d hd), }, { exact lt_of_le_of_lt (nat.zero_le _) h, } end lemma sum_homogeneous_component : ∑ i in range (φ.total_degree + 1), homogeneous_component i φ = φ := begin ext1 d, suffices : φ.total_degree < d.support.sum d → 0 = coeff d φ, by simpa [coeff_sum, coeff_homogeneous_component], exact λ h, (coeff_eq_zero_of_total_degree_lt h).symm end end homogeneous_component end end mv_polynomial
e5d0480f97d33002f03543dc0e0039a6a90073fe
c86b74188c4b7a462728b1abd659ab4e5828dd61
/src/Init/Data/String/Basic.lean
a39087ee8d6ab1aba0d72cb3b97956a6fa600051
[ "Apache-2.0" ]
permissive
cwb96/lean4
75e1f92f1ba98bbaa6b34da644b3dfab2ce7bf89
b48831cda76e64f13dd1c0edde7ba5fb172ed57a
refs/heads/master
1,686,347,881,407
1,624,483,842,000
1,624,483,842,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,123
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import Init.Data.List.Basic import Init.Data.Char.Basic import Init.Data.Option.Basic universes u def List.asString (s : List Char) : String := ⟨s⟩ namespace String instance : LT String := ⟨fun s₁ s₂ => s₁.data < s₂.data⟩ @[extern "lean_string_dec_lt"] instance decLt (s₁ s₂ : @& String) : Decidable (s₁ < s₂) := List.hasDecidableLt s₁.data s₂.data @[extern "lean_string_length"] def length : (@& String) → Nat | ⟨s⟩ => s.length /-- The internal implementation uses dynamic arrays and will perform destructive updates if the String is not shared. -/ @[extern "lean_string_push"] def push : String → Char → String | ⟨s⟩, c => ⟨s ++ [c]⟩ /-- The internal implementation uses dynamic arrays and will perform destructive updates if the String is not shared. -/ @[extern "lean_string_append"] def append : String → (@& String) → String | ⟨a⟩, ⟨b⟩ => ⟨a ++ b⟩ /-- O(n) in the runtime, where n is the length of the String -/ def toList (s : String) : List Char := s.data private def utf8GetAux : List Char → Pos → Pos → Char | [], i, p => arbitrary | c::cs, i, p => if i = p then c else utf8GetAux cs (i + csize c) p @[extern "lean_string_utf8_get"] def get : (@& String) → (@& Pos) → Char | ⟨s⟩, p => utf8GetAux s 0 p def getOp (self : String) (idx : Pos) : Char := self.get idx private def utf8SetAux (c' : Char) : List Char → Pos → Pos → List Char | [], i, p => [] | c::cs, i, p => if i = p then (c'::cs) else c::(utf8SetAux c' cs (i + csize c) p) @[extern "lean_string_utf8_set"] def set : String → (@& Pos) → Char → String | ⟨s⟩, i, c => ⟨utf8SetAux c s 0 i⟩ def modify (s : String) (i : Pos) (f : Char → Char) : String := s.set i <| f <| s.get i @[extern "lean_string_utf8_next"] def next (s : @& String) (p : @& Pos) : Pos := let c := get s p p + csize c private def utf8PrevAux : List Char → Pos → Pos → Pos | [], i, p => 0 | c::cs, i, p => let cz := csize c let i' := i + cz if i' = p then i else utf8PrevAux cs i' p @[extern "lean_string_utf8_prev"] def prev : (@& String) → (@& Pos) → Pos | ⟨s⟩, p => if p = 0 then 0 else utf8PrevAux s 0 p def front (s : String) : Char := get s 0 def back (s : String) : Char := get s (prev s (bsize s)) @[extern "lean_string_utf8_at_end"] def atEnd : (@& String) → (@& Pos) → Bool | s, p => p ≥ utf8ByteSize s /- TODO: remove `partial` keywords after we restore the tactic framework and wellfounded recursion support -/ partial def posOfAux (s : String) (c : Char) (stopPos : Pos) (pos : Pos) : Pos := if pos == stopPos then pos else if s.get pos == c then pos else posOfAux s c stopPos (s.next pos) @[inline] def posOf (s : String) (c : Char) : Pos := posOfAux s c s.bsize 0 partial def revPosOfAux (s : String) (c : Char) (pos : Pos) : Option Pos := if s.get pos == c then some pos else if pos == 0 then none else revPosOfAux s c (s.prev pos) def revPosOf (s : String) (c : Char) : Option Pos := if s.bsize == 0 then none else revPosOfAux s c (s.prev s.bsize) partial def findAux (s : String) (p : Char → Bool) (stopPos : Pos) (pos : Pos) : Pos := if pos == stopPos then pos else if p (s.get pos) then pos else findAux s p stopPos (s.next pos) @[inline] def find (s : String) (p : Char → Bool) : Pos := findAux s p s.bsize 0 partial def revFindAux (s : String) (p : Char → Bool) (pos : Pos) : Option Pos := if p (s.get pos) then some pos else if pos == 0 then none else revFindAux s p (s.prev pos) def revFind (s : String) (p : Char → Bool) : Option Pos := if s.bsize == 0 then none else revFindAux s p (s.prev s.bsize) private def utf8ExtractAux₂ : List Char → Pos → Pos → List Char | [], _, _ => [] | c::cs, i, e => if i = e then [] else c :: utf8ExtractAux₂ cs (i + csize c) e private def utf8ExtractAux₁ : List Char → Pos → Pos → Pos → List Char | [], _, _, _ => [] | s@(c::cs), i, b, e => if i = b then utf8ExtractAux₂ s i e else utf8ExtractAux₁ cs (i + csize c) b e @[extern "lean_string_utf8_extract"] def extract : (@& String) → (@& Pos) → (@& Pos) → String | ⟨s⟩, b, e => if b ≥ e then ⟨[]⟩ else ⟨utf8ExtractAux₁ s 0 b e⟩ @[specialize] partial def splitAux (s : String) (p : Char → Bool) (b : Pos) (i : Pos) (r : List String) : List String := if s.atEnd i then let r := (s.extract b i)::r r.reverse else if p (s.get i) then let i := s.next i splitAux s p i i (s.extract b (i-1)::r) else splitAux s p b (s.next i) r @[specialize] def split (s : String) (p : Char → Bool) : List String := splitAux s p 0 0 [] partial def splitOnAux (s sep : String) (b : Pos) (i : Pos) (j : Pos) (r : List String) : List String := if s.atEnd i then let r := if sep.atEnd j then ""::(s.extract b (i-j))::r else (s.extract b i)::r r.reverse else if s.get i == sep.get j then let i := s.next i let j := sep.next j if sep.atEnd j then splitOnAux s sep i i 0 (s.extract b (i-j)::r) else splitOnAux s sep b i j r else splitOnAux s sep b (s.next i) 0 r def splitOn (s : String) (sep : String := " ") : List String := if sep == "" then [s] else splitOnAux s sep 0 0 0 [] instance : Inhabited String := ⟨""⟩ instance : Append String := ⟨String.append⟩ def str : String → Char → String := push def pushn (s : String) (c : Char) (n : Nat) : String := n.repeat (fun s => s.push c) s def isEmpty (s : String) : Bool := s.bsize == 0 def join (l : List String) : String := l.foldl (fun r s => r ++ s) "" def singleton (c : Char) : String := "".push c def intercalate (s : String) (ss : List String) : String := (List.intercalate s.toList (ss.map toList)).asString structure Iterator where s : String i : Pos def mkIterator (s : String) : Iterator := ⟨s, 0⟩ namespace Iterator def toString : Iterator → String | ⟨s, _⟩ => s def remainingBytes : Iterator → Nat | ⟨s, i⟩ => s.bsize - i def pos : Iterator → Pos | ⟨s, i⟩ => i def curr : Iterator → Char | ⟨s, i⟩ => get s i def next : Iterator → Iterator | ⟨s, i⟩ => ⟨s, s.next i⟩ def prev : Iterator → Iterator | ⟨s, i⟩ => ⟨s, s.prev i⟩ def hasNext : Iterator → Bool | ⟨s, i⟩ => i < utf8ByteSize s def hasPrev : Iterator → Bool | ⟨s, i⟩ => i > 0 def setCurr : Iterator → Char → Iterator | ⟨s, i⟩, c => ⟨s.set i c, i⟩ def toEnd : Iterator → Iterator | ⟨s, _⟩ => ⟨s, s.bsize⟩ def extract : Iterator → Iterator → String | ⟨s₁, b⟩, ⟨s₂, e⟩ => if s₁ ≠ s₂ || b > e then "" else s₁.extract b e def forward : Iterator → Nat → Iterator | it, 0 => it | it, n+1 => forward it.next n def remainingToString : Iterator → String | ⟨s, i⟩ => s.extract i s.bsize /-- `(isPrefixOfRemaining it₁ it₂)` is `true` iff `it₁.remainingToString` is a prefix of `it₂.remainingToString`. -/ def isPrefixOfRemaining : Iterator → Iterator → Bool | ⟨s₁, i₁⟩, ⟨s₂, i₂⟩ => s₁.extract i₁ s₁.bsize = s₂.extract i₂ (i₂ + (s₁.bsize - i₁)) def nextn : Iterator → Nat → Iterator | it, 0 => it | it, i+1 => nextn it.next i def prevn : Iterator → Nat → Iterator | it, 0 => it | it, i+1 => prevn it.prev i end Iterator partial def offsetOfPosAux (s : String) (pos : Pos) (i : Pos) (offset : Nat) : Nat := if i == pos || s.atEnd i then offset else offsetOfPosAux s pos (s.next i) (offset+1) def offsetOfPos (s : String) (pos : Pos) : Nat := offsetOfPosAux s pos 0 0 @[specialize] partial def foldlAux {α : Type u} (f : α → Char → α) (s : String) (stopPos : Pos) (i : Pos) (a : α) : α := let rec loop (i : Pos) (a : α) := if i == stopPos then a else loop (s.next i) (f a (s.get i)) loop i a @[inline] def foldl {α : Type u} (f : α → Char → α) (init : α) (s : String) : α := foldlAux f s s.bsize 0 init @[specialize] partial def foldrAux {α : Type u} (f : Char → α → α) (a : α) (s : String) (stopPos : Pos) (i : Pos) : α := let rec loop (i : Pos) := if i == stopPos then a else f (s.get i) (loop (s.next i)) loop i @[inline] def foldr {α : Type u} (f : Char → α → α) (init : α) (s : String) : α := foldrAux f init s s.bsize 0 @[specialize] partial def anyAux (s : String) (stopPos : Pos) (p : Char → Bool) (i : Pos) : Bool := let rec loop (i : Pos) := if i == stopPos then false else if p (s.get i) then true else loop (s.next i) loop i @[inline] def any (s : String) (p : Char → Bool) : Bool := anyAux s s.bsize p 0 @[inline] def all (s : String) (p : Char → Bool) : Bool := !s.any (fun c => !p c) def contains (s : String) (c : Char) : Bool := s.any (fun a => a == c) @[specialize] partial def mapAux (f : Char → Char) (i : Pos) (s : String) : String := if s.atEnd i then s else let c := f (s.get i) let s := s.set i c mapAux f (s.next i) s @[inline] def map (f : Char → Char) (s : String) : String := mapAux f 0 s def isNat (s : String) : Bool := s.all fun c => c.isDigit def toNat? (s : String) : Option Nat := if s.isNat then some <| s.foldl (fun n c => n*10 + (c.toNat - '0'.toNat)) 0 else none /-- Return true iff `p` is a prefix of `s` -/ partial def isPrefixOf (p : String) (s : String) : Bool := let rec loop (i : Pos) := if p.atEnd i then true else let c₁ := p.get i let c₂ := s.get i c₁ == c₂ && loop (s.next i) p.length ≤ s.length && loop 0 end String namespace Substring @[inline] def isEmpty (ss : Substring) : Bool := ss.bsize == 0 @[inline] def toString : Substring → String | ⟨s, b, e⟩ => s.extract b e @[inline] def toIterator : Substring → String.Iterator | ⟨s, b, _⟩ => ⟨s, b⟩ /-- Return the codepoint at the given offset into the substring. -/ @[inline] def get : Substring → String.Pos → Char | ⟨s, b, _⟩, p => s.get (b+p) /-- Given an offset of a codepoint into the substring, return the offset there of the next codepoint. -/ @[inline] private def next : Substring → String.Pos → String.Pos | ⟨s, b, e⟩, p => let absP := b+p if absP = e then p else s.next absP - b /-- Given an offset of a codepoint into the substring, return the offset there of the previous codepoint. -/ @[inline] private def prev : Substring → String.Pos → String.Pos | ⟨s, b, _⟩, p => let absP := b+p if absP = b then p else s.prev absP - b private def nextn : Substring → Nat → String.Pos → String.Pos | ss, 0, p => p | ss, i+1, p => ss.nextn i (ss.next p) private def prevn : Substring → String.Pos → Nat → String.Pos | ss, 0, p => p | ss, i+1, p => ss.prevn i (ss.prev p) @[inline] def front (s : Substring) : Char := s.get 0 /-- Return the offset into `s` of the first occurence of `c` in `s`, or `s.bsize` if `c` doesn't occur. -/ @[inline] def posOf (s : Substring) (c : Char) : String.Pos := match s with | ⟨s, b, e⟩ => (String.posOfAux s c e b) - b @[inline] def drop : Substring → Nat → Substring | ss@⟨s, b, e⟩, n => ⟨s, b + ss.nextn n 0, e⟩ @[inline] def dropRight : Substring → Nat → Substring | ss@⟨s, b, e⟩, n => ⟨s, b, b + ss.prevn n ss.bsize⟩ @[inline] def take : Substring → Nat → Substring | ss@⟨s, b, e⟩, n => ⟨s, b, b + ss.nextn n 0⟩ @[inline] def takeRight : Substring → Nat → Substring | ss@⟨s, b, e⟩, n => ⟨s, b + ss.prevn n ss.bsize, e⟩ @[inline] def atEnd : Substring → String.Pos → Bool | ⟨s, b, e⟩, p => b + p == e @[inline] def extract : Substring → String.Pos → String.Pos → Substring | ⟨s, b, _⟩, b', e' => if b' ≥ e' then ⟨"", 0, 1⟩ else ⟨s, b+b', b+e'⟩ partial def splitOn (s : Substring) (sep : String := " ") : List Substring := if sep == "" then [s] else let stopPos := s.stopPos let str := s.str let rec loop (b i j : String.Pos) (r : List Substring) : List Substring := if i == stopPos then let r := if sep.atEnd j then "".toSubstring::{ str := str, startPos := b, stopPos := i-j } :: r else { str := str, startPos := b, stopPos := i } :: r r.reverse else if s.get i == sep.get j then let i := s.next i let j := sep.next j if sep.atEnd j then loop i i 0 ({ str := str, startPos := b, stopPos := i-j } :: r) else loop b i j r else loop b (s.next i) 0 r loop s.startPos s.startPos 0 [] @[inline] def foldl {α : Type u} (f : α → Char → α) (init : α) (s : Substring) : α := match s with | ⟨s, b, e⟩ => String.foldlAux f s e b init @[inline] def foldr {α : Type u} (f : Char → α → α) (init : α) (s : Substring) : α := match s with | ⟨s, b, e⟩ => String.foldrAux f init s e b @[inline] def any (s : Substring) (p : Char → Bool) : Bool := match s with | ⟨s, b, e⟩ => String.anyAux s e p b @[inline] def all (s : Substring) (p : Char → Bool) : Bool := !s.any (fun c => !p c) def contains (s : Substring) (c : Char) : Bool := s.any (fun a => a == c) @[specialize] private partial def takeWhileAux (s : String) (stopPos : String.Pos) (p : Char → Bool) (i : String.Pos) : String.Pos := if i == stopPos then i else if p (s.get i) then takeWhileAux s stopPos p (s.next i) else i @[inline] def takeWhile : Substring → (Char → Bool) → Substring | ⟨s, b, e⟩, p => let e := takeWhileAux s e p b; ⟨s, b, e⟩ @[inline] def dropWhile : Substring → (Char → Bool) → Substring | ⟨s, b, e⟩, p => let b := takeWhileAux s e p b; ⟨s, b, e⟩ @[specialize] private partial def takeRightWhileAux (s : String) (begPos : String.Pos) (p : Char → Bool) (i : String.Pos) : String.Pos := if i == begPos then i else let i' := s.prev i let c := s.get i' if !p c then i else takeRightWhileAux s begPos p i' @[inline] def takeRightWhile : Substring → (Char → Bool) → Substring | ⟨s, b, e⟩, p => let b := takeRightWhileAux s b p e ⟨s, b, e⟩ @[inline] def dropRightWhile : Substring → (Char → Bool) → Substring | ⟨s, b, e⟩, p => let e := takeRightWhileAux s b p e ⟨s, b, e⟩ @[inline] def trimLeft (s : Substring) : Substring := s.dropWhile Char.isWhitespace @[inline] def trimRight (s : Substring) : Substring := s.dropRightWhile Char.isWhitespace @[inline] def trim : Substring → Substring | ⟨s, b, e⟩ => let b := takeWhileAux s e Char.isWhitespace b let e := takeRightWhileAux s b Char.isWhitespace e ⟨s, b, e⟩ def isNat (s : Substring) : Bool := s.all fun c => c.isDigit def toNat? (s : Substring) : Option Nat := if s.isNat then some <| s.foldl (fun n c => n*10 + (c.toNat - '0'.toNat)) 0 else none def beq (ss1 ss2 : Substring) : Bool := -- TODO: should not allocate ss1.bsize == ss2.bsize && ss1.toString == ss2.toString instance hasBeq : BEq Substring := ⟨beq⟩ end Substring namespace String def drop (s : String) (n : Nat) : String := (s.toSubstring.drop n).toString def dropRight (s : String) (n : Nat) : String := (s.toSubstring.dropRight n).toString def take (s : String) (n : Nat) : String := (s.toSubstring.take n).toString def takeRight (s : String) (n : Nat) : String := (s.toSubstring.takeRight n).toString def takeWhile (s : String) (p : Char → Bool) : String := (s.toSubstring.takeWhile p).toString def dropWhile (s : String) (p : Char → Bool) : String := (s.toSubstring.dropWhile p).toString def takeRightWhile (s : String) (p : Char → Bool) : String := (s.toSubstring.takeRightWhile p).toString def dropRightWhile (s : String) (p : Char → Bool) : String := (s.toSubstring.dropRightWhile p).toString def startsWith (s pre : String) : Bool := s.toSubstring.take pre.length == pre.toSubstring def endsWith (s post : String) : Bool := s.toSubstring.takeRight post.length == post.toSubstring def trimRight (s : String) : String := s.toSubstring.trimRight.toString def trimLeft (s : String) : String := s.toSubstring.trimLeft.toString def trim (s : String) : String := s.toSubstring.trim.toString @[inline] def nextWhile (s : String) (p : Char → Bool) (i : String.Pos) : String.Pos := Substring.takeWhileAux s s.bsize p i @[inline] def nextUntil (s : String) (p : Char → Bool) (i : String.Pos) : String.Pos := nextWhile s (fun c => !p c) i def toUpper (s : String) : String := s.map Char.toUpper def toLower (s : String) : String := s.map Char.toLower def capitalize (s : String) := s.set 0 <| s.get 0 |>.toUpper def decapitalize (s : String) := s.set 0 <| s.get 0 |>.toLower end String protected def Char.toString (c : Char) : String := String.singleton c
39eb8d79ec583952077c6b156df47122a7af34b4
6b10c15e653d49d146378acda9f3692e9b5b1950
/examples/basic_skills/unnamed_424.lean
57e34613f529fabf062586ea16eefa3d9c70b4d4
[]
no_license
gebner/mathematics_in_lean
3cf7f18767208ea6c3307ec3a67c7ac266d8514d
6d1462bba46d66a9b948fc1aef2714fd265cde0b
refs/heads/master
1,655,301,945,565
1,588,697,505,000
1,588,697,505,000
261,523,603
0
0
null
1,588,695,611,000
1,588,695,610,000
null
UTF-8
Lean
false
false
353
lean
import data.real.basic variables a b c : ℝ -- BEGIN example : (c * b) * a = b * (a * c) := by ring example : (a + b) * (a + b) = a * a + 2 * (a * b) + b * b := by ring example : (a + b) * (a - b) = a^2 - b^2 := by ring example (a b c d : ℝ) (hyp : c = d * a + b) (hyp' : b = a * d) : c = 2 * a * d := begin rw [hyp, hyp'], ring end -- END
f3a64ea486ef3054db915e971b003869f253cafa
67dc347ff8e6eaa780fd69518f3e69b2fa5eef96
/src/smt2/syntax.lean
230aae92c9e497b1a58dc09d356586277ebcb6b7
[ "Apache-2.0" ]
permissive
Kha/smt2_interface
74589568b30d32a500fb3a9abb299a28f19d75e9
d92be33fbf4d56e3745e255ab423ba8da7d7695b
refs/heads/master
1,619,785,421,091
1,518,453,826,000
1,518,453,826,000
121,273,425
0
0
null
1,518,453,882,000
1,518,453,882,000
null
UTF-8
Lean
false
false
3,981
lean
namespace smt2 @[reducible] def symbol : Type := string @[reducible] def identifier : Type := string inductive special_constant : Type | number : int → special_constant | string : string → special_constant meta def special_constant.to_format : special_constant → format | (special_constant.number i) := to_fmt (to_string i) | (special_constant.string str) := to_fmt str instance int_to_special_constant : has_coe int special_constant := ⟨ special_constant.number ⟩ inductive sort : Type | id : identifier → sort | apply : identifier → list sort → sort instance : has_coe string sort := ⟨ fun str, sort.id str ⟩ meta def sort.to_format : sort → format | (sort.id i) := to_fmt i | (sort.apply _ _) := "NYI" meta instance sort_has_to_fmt : has_to_format sort := ⟨ sort.to_format ⟩ inductive attr : Type inductive qualified_name : Type | id : identifier → qualified_name | qual_id : identifier → sort → qualified_name meta def qualified_name.to_format : qualified_name → format | (qualified_name.id i) := i | (qualified_name.qual_id _ _) := "NYI" instance string_to_qual_name : has_coe string qualified_name := ⟨ fun str, qualified_name.id str ⟩ inductive term : Type | qual_id : qualified_name → term | const : special_constant → term | apply : qualified_name → list term → term | letb : list (name × term) → term → term | forallq : list (symbol × sort) → term → term | existsq : list (symbol × sort) → term → term | annotate : term → list attr → term instance qual_name_to_term : has_coe qualified_name term := ⟨ term.qual_id ⟩ instance special_const_to_term : has_coe special_constant term := ⟨ term.const ⟩ meta def term.to_format : term → format | (term.qual_id id) := id.to_format | (term.const spec_const) := spec_const.to_format | (term.apply qual_id ts) := let formatted_ts := format.join $ list.intersperse " " $ ts.map term.to_format in format.bracket "(" ")" ( qual_id.to_format ++ format.space ++ formatted_ts) | (term.letb ps ret) := "NYI" | (term.forallq bs body) := "(forall (" ++ format.join (bs.map (fun ⟨sym, sort⟩, format.bracket "(" ")" $ to_fmt sym ++ " " ++ to_fmt sort)) ++ ") " ++ term.to_format body ++ ")" | (term.existsq ps ret) := "NYI existsq" | (term.annotate _ _) := "NYI annotate" inductive fun_def : Type inductive info_flag : Type inductive keyword : Type inductive option : Type inductive cmd : Type | assert_cmd : term → cmd | check_sat : cmd | check_sat_assuming : term → cmd -- not complete | declare_const : symbol → sort → cmd | declare_fun : symbol → list sort → sort → cmd | declare_sort : symbol → nat → cmd | define_fun : fun_def → cmd | define_fun_rec : fun_def → cmd | define_funs_rec : cmd -- not complete | define_sort : symbol → list symbol → sort → cmd | echo : string → cmd | exit_cmd : cmd | get_assertions : cmd | get_assignment : cmd | get_info : info_flag → cmd | get_model : cmd | get_option : keyword → cmd | get_proof : cmd | get_unsat_assumtpions : cmd | get_unsat_core : cmd | get_value : cmd -- not complete | pop : nat → cmd | push : nat → cmd | reset : cmd | reset_assertions : cmd | set_info : attr → cmd | set_logic : symbol → cmd | set_opt : option → cmd open cmd meta def string_lit (s : string) : format := format.bracket "\"" "\"" (to_fmt s) meta def cmd.to_format : cmd → format | (echo msg) := "(echo " ++ string_lit msg ++ ")\n" | (declare_const sym srt) := "(declare-const " ++ sym ++ " " ++ to_fmt srt ++ ")" | (assert_cmd t) := "(assert " ++ t.to_format ++ ")" | (check_sat) := "(check-sat)" | (declare_fun sym ps rs) := "(declare-fun " ++ sym ++ format.bracket " (" ")" (format.join $ list.intersperse " " $ list.map to_fmt ps) ++ " " ++ to_fmt rs ++ ")" | (declare_sort sym arity) := "(declare-sort " ++ sym ++ " " ++ to_string arity ++ ")" | _ := "NYI" meta instance : has_to_format cmd := ⟨ cmd.to_format ⟩ end smt2
9b61dace21a37cb5b3e6f55d3bc51fe2208df4ce
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/nat/prime_fin.lean
9fe7a9f7ef4e1fca37038cb75d92972bcadb672c
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
2,013
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad, Mario Carneiro -/ import data.nat.factors import data.set.finite /-! # Prime numbers > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file contains some results about prime numbers which depend on finiteness of sets. -/ namespace nat /-- A version of `nat.exists_infinite_primes` using the `set.infinite` predicate. -/ lemma infinite_set_of_prime : {p | prime p}.infinite := set.infinite_of_not_bdd_above not_bdd_above_set_of_prime /-- If `a`, `b` are positive, the prime divisors of `a * b` are the union of those of `a` and `b` -/ lemma factors_mul_to_finset {a b : ℕ} (ha : a ≠ 0) (hb : b ≠ 0) : (a * b).factors.to_finset = a.factors.to_finset ∪ b.factors.to_finset := (list.to_finset.ext $ λ x, (mem_factors_mul ha hb).trans list.mem_union.symm).trans $ list.to_finset_union _ _ lemma pow_succ_factors_to_finset (n k : ℕ) : (n^(k+1)).factors.to_finset = n.factors.to_finset := begin rcases eq_or_ne n 0 with rfl | hn, { simp }, induction k with k ih, { simp }, rw [pow_succ, factors_mul_to_finset hn (pow_ne_zero _ hn), ih, finset.union_idempotent] end lemma pow_factors_to_finset (n : ℕ) {k : ℕ} (hk : k ≠ 0) : (n^k).factors.to_finset = n.factors.to_finset := begin cases k, { simpa using hk }, rw pow_succ_factors_to_finset end /-- The only prime divisor of positive prime power `p^k` is `p` itself -/ lemma prime_pow_prime_divisor {p k : ℕ} (hk : k ≠ 0) (hp : prime p) : (p^k).factors.to_finset = {p} := by simp [pow_factors_to_finset p hk, factors_prime hp] lemma factors_mul_to_finset_of_coprime {a b : ℕ} (hab : coprime a b) : (a * b).factors.to_finset = a.factors.to_finset ∪ b.factors.to_finset := (list.to_finset.ext $ mem_factors_mul_of_coprime hab).trans $ list.to_finset_union _ _ end nat
ff7252ae502d8a1a95ce504525cf3fb299129609
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/tactic/omega/int/form.lean
4aee2e53b517ed2871247dce9ed0ceea75af2891
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
4,173
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek Linear integer arithmetic formulas in pre-normalized form. -/ import tactic.omega.int.preterm namespace omega namespace int /-- Intermediate shadow syntax for LNA formulas that includes unreified exprs -/ meta inductive exprform | eq : exprterm → exprterm → exprform | le : exprterm → exprterm → exprform | not : exprform → exprform | or : exprform → exprform → exprform | and : exprform → exprform → exprform /-- Intermediate shadow syntax for LIA formulas that includes non-canonical terms -/ @[derive has_reflect, derive inhabited] inductive preform | eq : preterm → preterm → preform | le : preterm → preterm → preform | not : preform → preform | or : preform → preform → preform | and : preform → preform → preform localized "notation x ` =* ` y := omega.int.preform.eq x y" in omega.int localized "notation x ` ≤* ` y := omega.int.preform.le x y" in omega.int localized "notation `¬* ` p := omega.int.preform.not p" in omega.int localized "notation p ` ∨* ` q := omega.int.preform.or p q" in omega.int localized "notation p ` ∧* ` q := omega.int.preform.and p q" in omega.int namespace preform /-- Evaluate a preform into prop using the valuation v. -/ @[simp] def holds (v : nat → int) : preform → Prop | (t =* s) := t.val v = s.val v | (t ≤* s) := t.val v ≤ s.val v | (¬* p) := ¬ p.holds | (p ∨* q) := p.holds ∨ q.holds | (p ∧* q) := p.holds ∧ q.holds end preform /-- univ_close p n := p closed by prepending n universal quantifiers -/ @[simp] def univ_close (p : preform) : (nat → int) → nat → Prop | v 0 := p.holds v | v (k+1) := ∀ i : int, univ_close (update_zero i v) k namespace preform /-- Fresh de Brujin index not used by any variable in argument -/ def fresh_index : preform → nat | (t =* s) := max t.fresh_index s.fresh_index | (t ≤* s) := max t.fresh_index s.fresh_index | (¬* p) := p.fresh_index | (p ∨* q) := max p.fresh_index q.fresh_index | (p ∧* q) := max p.fresh_index q.fresh_index /-- All valuations satisfy argument -/ def valid (p : preform) : Prop := ∀ v, holds v p /-- There exists some valuation that satisfies argument -/ def sat (p : preform) : Prop := ∃ v, holds v p /-- implies p q := under any valuation, q holds if p holds -/ def implies (p q : preform) : Prop := ∀ v, (holds v p → holds v q) /-- equiv p q := under any valuation, p holds iff q holds -/ def equiv (p q : preform) : Prop := ∀ v, (holds v p ↔ holds v q) lemma sat_of_implies_of_sat {p q : preform} : implies p q → sat p → sat q := begin intros h1 h2, apply exists_imp_exists h1 h2 end lemma sat_or {p q : preform} : sat (p ∨* q) ↔ sat p ∨ sat q := begin constructor; intro h1, { cases h1 with v h1, cases h1 with h1 h1; [left,right]; refine ⟨v,_⟩; assumption }, { cases h1 with h1 h1; cases h1 with v h1; refine ⟨v,_⟩; [left,right]; assumption } end /-- There does not exist any valuation that satisfies argument -/ def unsat (p : preform) : Prop := ¬ sat p def repr : preform → string | (t =* s) := "(" ++ t.repr ++ " = " ++ s.repr ++ ")" | (t ≤* s) := "(" ++ t.repr ++ " ≤ " ++ s.repr ++ ")" | (¬* p) := "¬" ++ p.repr | (p ∨* q) := "(" ++ p.repr ++ " ∨ " ++ q.repr ++ ")" | (p ∧* q) := "(" ++ p.repr ++ " ∧ " ++ q.repr ++ ")" instance has_repr : has_repr preform := ⟨repr⟩ meta instance has_to_format : has_to_format preform := ⟨λ x, x.repr⟩ end preform lemma univ_close_of_valid {p : preform} : ∀ {m v}, p.valid → univ_close p v m | 0 v h1 := h1 _ | (m+1) v h1 := λ i, univ_close_of_valid h1 lemma valid_of_unsat_not {p : preform} : (¬*p).unsat → p.valid := begin simp only [preform.sat, preform.unsat, preform.valid, preform.holds], rw classical.not_exists_not, intro h, assumption end /-- Tactic for setting up proof by induction over preforms. -/ meta def preform.induce (t : tactic unit := tactic.skip) : tactic unit := `[ intro p, induction p with t s t s p ih p q ihp ihq p q ihp ihq; t] end int end omega
344ca5f9cfaffda676aac13582b00c96777df2b9
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/tactic8.lean
2d90991f6fcdaac7b4124f7c214c2e2c4099d532
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
195
lean
import logic open tactic theorem tst {A B : Prop} (H1 : A) (H2 : B) : A ∧ B ∧ A := by apply @and.intro; apply (show A, from H1); apply (show B ∧ A, from and.intro H2 H1) check @tst
7b54b41ee35c7f78b0beb3fe00c445691cff4d7a
e42377eeb4cc8aa830ef820dede679b43332552b
/src/showdown.lean
942bbfa15aeb30c28a16c0f79672cc56c960cbac
[]
no_license
cipher1024/prover-showdown
e8aa70f9f1fbfec8ba7ebca8aeddce12a42f8276
96d2d3b6ddc8a9cffc02911ab6d316c2775c2e0c
refs/heads/master
1,584,179,580,278
1,525,061,445,000
1,525,061,445,000
131,554,439
1
0
null
null
null
null
UTF-8
Lean
false
false
7,590
lean
import data.list.basic section left_pad variables {α : Type*} open nat lemma list.cons_repeat (c : α) : Π (n : ℕ), c :: list.repeat c n = list.repeat c n ++ [c] := by { intro, symmetry, induction n ; simp *, } lemma list.take_append (xs ys : list α) (n : ℕ) (h : xs.length = n) : (xs ++ ys).take n = xs := by { subst n, induction xs ; simp! [add_one,*], } lemma list.drop_append (xs ys : list α) (n : ℕ) (h : xs.length = n) : (xs ++ ys).drop n = ys := by { subst n, induction xs ; simp! [add_one,*], } def left_pad_aux (c : α) : ℕ → list α → list α | 0 xs := xs | (succ n) xs := left_pad_aux n (c :: xs) def left_pad (c : α) (n : ℕ) (x : list α) : list α := left_pad_aux c (n - x.length) x variables (c : α) (n : ℕ) (x : list α) lemma left_pad_def : left_pad c n x = list.repeat c (n - x.length) ++ x := begin simp [left_pad], generalize : (n - list.length x) = k, induction k generalizing x ; simp! [add_one,add_succ,succ_add,list.cons_repeat,*], end lemma length_left_pad : (left_pad c n x).length = max n x.length := begin simp [left_pad_def], cases le_total n x.length with Hk Hk, { rw max_eq_right Hk, rw ← nat.sub_eq_zero_iff_le at Hk, simp *, }, { simp [max_eq_left Hk], rw [← nat.add_sub_assoc Hk,nat.add_sub_cancel_left] }, end lemma left_pad_prefix : ∀ c' ∈ (left_pad c n x).take (n - x.length), c' = c := by { rw [left_pad_def,list.take_append] , { intro, apply list.eq_of_mem_repeat }, simp! , } lemma left_pad_suffix : (left_pad c n x).drop (n - x.length) = x := by simp [left_pad_def,list.drop_append] end left_pad section unique variables {α : Type*} [decidable_eq α] def unique : list α → list α | [] := [] | (x :: xs) := if x ∈ xs then unique xs else x :: unique xs lemma mem_unique (x : α) (xs : list α) : x ∈ unique xs ↔ x ∈ xs := begin induction xs with x' xs ; simp!, by_cases x' ∈ xs ; simp *, split ; intro h, { simp * }, { cases h ; cc, }, end lemma nodup_unique (xs : list α) : (unique xs).nodup := begin induction xs with x xs ; simp!, by_cases x ∈ xs ; simp [*,mem_unique], end end unique -- These are pretty general lemmas about lists. -- We could find them in the standard library namespace list universes u v w variables {α : Type u} {β : Type v} {γ : Type w} variables (f : α → β → γ) variables {x : α} {y : β} {xs : list α} {ys : list β} open function @[simp] lemma map_eq_nil {f : α → γ} : map f xs = nil ↔ xs = nil := by cases xs ; simp @[simp] lemma zip_eq_nil : zip xs ys = nil ↔ xs = nil ∨ ys = nil := by cases xs ; cases ys ; simp lemma map₂_eq_map_zip : map₂ f xs ys = map (uncurry f) (zip xs ys) := begin induction xs generalizing ys, { cases ys ; simp, }, { cases ys ; simp [*,uncurry], } end lemma mem_map₂ (h : (x,y) ∈ zip xs ys) : f x y ∈ map₂ f xs ys := by { rw map₂_eq_map_zip, refine @mem_map_of_mem _ _ (uncurry f) (x,y) _ h, } @[simp] lemma cons_tail_tails : xs :: tail (tails xs) = tails xs := by cases xs ; simp lemma tails_append {xs' : list α} : tails (xs ++ xs') = map (++ xs') (tails xs) ++ (tails xs').tail := by induction xs ; simp * lemma tails_reverse : tails (reverse xs) = reverse (map reverse (inits xs)) := by induction xs with x xs; simp [tails_append,*,comp] lemma inits_reverse : inits (reverse xs) = reverse (map reverse (tails xs)) := by { rw ← reverse_reverse xs, generalize : reverse xs = ys, simp [tails_reverse,comp], symmetry, apply map_id } @[simp] lemma sum_reverse [add_comm_monoid α] : sum (reverse xs) = sum xs := by induction xs ; simp * end list section minimum variables {α : Type*} [decidable_linear_order α] [inhabited α] def minimum (xs : list α) : α := list.foldl min xs.head xs.tail @[simp] lemma minimum_single (x : α) : minimum [x] = x := rfl open list @[simp] lemma minimum_cons (x : α) (xs : list α) (h : xs ≠ []) : minimum (x :: xs) = min x (minimum xs) := by { cases xs with y ys, contradiction, simp [minimum], rw ← foldl_hom, simp [min_assoc], } lemma le_minimum {x : α} {xs : list α} (h : xs ≠ []) (h' : ∀ i, i ∈ xs → x ≤ i) : x ≤ minimum xs := begin induction xs, contradiction, by_cases xs_tl = list.nil, { subst xs_tl, apply h', simp, }, simp *, apply le_min, { apply h', simp }, { apply xs_ih h, introv h'', apply h', right, apply h'' }, end @[simp] lemma minimum_cons_le_self {i : α} {xs : list α} : minimum (i :: xs) ≤ i := by cases xs ; simp [min_le_left] lemma minimum_le {i : α} {xs : list α} (h : i ∈ xs) : minimum xs ≤ i := by { induction xs ; cases h, { subst i, simp [min_le_left], }, { have : xs_tl ≠ [], { intro, subst xs_tl, cases h }, simp *, transitivity, apply min_le_right, solve_by_elim } } end minimum local attribute [instance, priority 0] classical.prop_decidable -- Here are the specifics of our solution. -- It hinges on computing lists of partial sums -- forward (`inits_sums`) and backward (`tails_sums`) section fulcrum open list int function instance : inhabited ℤ := ⟨ 0 ⟩ def inits_sums.f : list ℤ × ℤ → ℤ → list ℤ × ℤ | (xs,acc) x := ((acc+x) :: xs, acc+x) def partial_sums (xs : list ℤ) : list ℤ := (list.foldl inits_sums.f ([0],0) xs).1 def tails_sums (xs : list ℤ) : list ℤ := partial_sums xs.reverse def inits_sums (xs : list ℤ) : list ℤ := (partial_sums xs).reverse def fulcrum (xs : list ℤ) : ℤ := let xs' := map₂ (λ i j, abs (i - j)) (inits_sums xs) (tails_sums xs) in minimum xs' variables (xs : list ℤ) @[simp] def partial_sums_def : partial_sums xs.reverse = xs.tails.map sum := begin simp [partial_sums], suffices : foldl inits_sums.f ([0], 0) (reverse xs) = (map sum (tails xs), sum xs), { simpa * }, induction xs ; simp [*,foldl_reverse,inits_sums.f], end @[simp] def tails_sums_def : tails_sums xs = xs.tails.map sum := by simp [tails_sums] @[simp] def inits_sums_def : inits_sums xs = xs.inits.map sum := by { rw ← reverse_reverse xs, generalize : reverse xs = ys, simp [inits_sums,inits_reverse,comp], } lemma le_fulcrum (x : ℤ) (h : ∀ i j, (i,j) ∈ zip (xs.inits.map sum) (xs.tails.map sum) → x ≤ abs (i - j)) : x ≤ fulcrum xs := begin apply le_minimum, { simp [map₂_eq_map_zip,decidable.not_or_iff_and_not], cases xs ; simp, }, { simp, introv h', simp [map₂_eq_map_zip,uncurry] at h', rcases h' with ⟨ a, b, h₀, h₁ ⟩, subst i, apply h _ _ h₀, } end lemma fulcrum_le (i j : ℤ) (h : (i,j) ∈ zip (xs.inits.map sum) (xs.tails.map sum)) : fulcrum xs ≤ abs (i - j) := minimum_le $ by { simp, refine mem_map₂ (λ i j, abs (i + -j)) h, } def fulcrum_idx.f : ℕ × ℤ → ℕ × ℤ → ℕ × ℤ | (i,acc) (j,x) := if x < acc then (j,x) else (i,acc) def fulcrum_idx (xs : list ℤ) : ℕ := let xs' := map₂ (λ i j, abs (i - j)) (inits_sums xs) (tails_sums xs) in (xs'.enum.foldl fulcrum_idx.f (0,0)).1 -- lemma fulcrum_idx_lt_length_self (xs : list ℤ) -- (h : xs ≠ []) : -- fulcrum_idx xs < xs.length := sorry -- local notation `♯`:20 xs := (let h := fulcrum_idx_lt_length_self xs (by assumption) in by cc) -- lemma fulcrum_idx_spec (i : ℕ) -- (h₀ : xs ≠ []) -- (h₁ : i = fulcrum_idx xs) : -- abs ( (xs.inits.map list.sum).inth i - (xs.tails.map list.sum).inth i ) = fulcrum xs := -- begin -- subst i, -- simp [fulcrum,-inth,minimum], -- end end fulcrum
7c595dbce80f2a54d4ddfc2899fe9a29cc78b6a2
5d166a16ae129621cb54ca9dde86c275d7d2b483
/tests/lean/anc1.lean
37ac99282ad7e39c942b76f9fc8573de4f67bff8
[ "Apache-2.0" ]
permissive
jcarlson23/lean
b00098763291397e0ac76b37a2dd96bc013bd247
8de88701247f54d325edd46c0eed57aeacb64baf
refs/heads/master
1,611,571,813,719
1,497,020,963,000
1,497,021,515,000
93,882,536
1
0
null
1,497,029,896,000
1,497,029,896,000
null
UTF-8
Lean
false
false
1,310
lean
#check (⟨1, 2⟩ : nat × nat) #check (⟨trivial, trivial⟩ : true ∧ true) example : true := sorry #check (⟨1, sorry⟩ : Σ' x : nat, x > 0) open tactic #check show true, from ⟨⟩ #check (⟨1, by intro1 >> contradiction⟩ : ∃ x : nat, 1 ≠ 0) universe variables u v #check λ (A B C : Prop), assume (Ha : A) (Hb : B) (Hc : C), show B ∧ A, from ⟨Hb, Ha⟩ #check λ (A B C : Prop), assume (Ha : A) (Hb : B) (Hc : C), show B ∧ A ∧ C ∧ A, from ⟨Hb, ⟨Ha, ⟨Hc, Ha⟩⟩⟩ #check λ (A B C : Prop), assume (Ha : A) (Hb : B) (Hc : C), show B ∧ A ∧ C ∧ A, from ⟨Hb, Ha, Hc, Ha⟩ #check λ (A B C : Prop), assume (Ha : A) (Hb : B) (Hc : C), show ((B ∧ true) ∧ A) ∧ (C ∧ A), from ⟨⟨⟨Hb, ⟨⟩⟩, Ha⟩, ⟨Hc, Ha⟩⟩ #check λ (A : Type u) (P : A → Prop) (Q : A → Prop), take (a : A), assume (H1 : P a) (H2 : Q a), show ∃ x, P x ∧ Q x, from ⟨a, ⟨H1, H2⟩⟩ #check λ (A : Type u) (P : A → Prop) (Q : A → Prop), take (a : A) (b : A), assume (H1 : P a) (H2 : Q b), show ∃ x y, P x ∧ Q y, from ⟨a, ⟨b, ⟨H1, H2⟩⟩⟩ #check λ (A : Type u) (P : A → Prop) (Q : A → Prop), take (a : A) (b : A), assume (H1 : P a) (H2 : Q b), show ∃ x y, P x ∧ Q y, from ⟨a, b, H1, H2⟩
f1b63f449247701ae1af4751341998bec0544831
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/abelian/basic.lean
c8e7f0f58975591c61c37f2043946fd47bf6ea3f
[ "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
23,016
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import category_theory.limits.constructions.pullbacks import category_theory.limits.shapes.biproducts import category_theory.limits.shapes.images import category_theory.abelian.non_preadditive /-! # Abelian categories This file contains the definition and basic properties of abelian categories. There are many definitions of abelian category. Our definition is as follows: A category is called abelian if it is preadditive, has a finite products, kernels and cokernels, and if every monomorphism and epimorphism is normal. It should be noted that if we also assume coproducts, then preadditivity is actually a consequence of the other properties, as we show in `non_preadditive_abelian.lean`. However, this fact is of little practical relevance, since essentially all interesting abelian categories come with a preadditive structure. In this way, by requiring preadditivity, we allow the user to pass in the preadditive structure the specific category they are working with has natively. ## Main definitions * `abelian` is the type class indicating that a category is abelian. It extends `preadditive`. * `abelian.image f` is `kernel (cokernel.π f)`, and * `abelian.coimage f` is `cokernel (kernel.ι f)`. ## Main results * In an abelian category, mono + epi = iso. * If `f : X ⟶ Y`, then the map `factor_thru_image f : X ⟶ image f` is an epimorphism, and the map `factor_thru_coimage f : coimage f ⟶ Y` is a monomorphism. * Factoring through the image and coimage is a strong epi-mono factorisation. This means that * every abelian category has images. We instantiated this in such a way that `abelian.image f` is definitionally equal to `limits.image f`, and * there is a canonical isomorphism `coimage_iso_image : coimage f ≅ image f` such that `coimage.π f ≫ (coimage_iso_image f).hom ≫ image.ι f = f`. The lemma stating this is called `full_image_factorisation`. * Every epimorphism is a cokernel of its kernel. Every monomorphism is a kernel of its cokernel. * The pullback of an epimorphism is an epimorphism. The pushout of a monomorphism is a monomorphism. (This is not to be confused with the fact that the pullback of a monomorphism is a monomorphism, which is true in any category). ## Implementation notes The typeclass `abelian` does not extend `non_preadditive_abelian`, to avoid having to deal with comparing the two `has_zero_morphisms` instances (one from `preadditive` in `abelian`, and the other a field of `non_preadditive_abelian`). As a consequence, at the beginning of this file we trivially build a `non_preadditive_abelian` instance from an `abelian` instance, and use this to restate a number of theorems, in each case just reusing the proof from `non_preadditive_abelian.lean`. We don't show this yet, but abelian categories are finitely complete and finitely cocomplete. However, the limits we can construct at this level of generality will most likely be less nice than the ones that can be created in specific applications. For this reason, we adopt the following convention: * If the statement of a theorem involves limits, the existence of these limits should be made an explicit typeclass parameter. * If a limit only appears in a proof, but not in the statement of a theorem, the limit should not be a typeclass parameter, but instead be created using `abelian.has_pullbacks` or a similar definition. ## References * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] * [P. Aluffi, *Algebra: Chaper 0*][aluffi2016] -/ noncomputable theory open category_theory open category_theory.preadditive open category_theory.limits universes v u namespace category_theory variables {C : Type u} [category.{v} C] variables (C) /-- A (preadditive) category `C` is called abelian if it has all finite products, all kernels and cokernels, and if every monomorphism is the kernel of some morphism and every epimorphism is the cokernel of some morphism. (This definition implies the existence of zero objects: finite products give a terminal object, and in a preadditive category any terminal object is a zero object.) -/ class abelian extends preadditive C := [has_finite_products : has_finite_products C] [has_kernels : has_kernels C] [has_cokernels : has_cokernels C] (normal_mono : Π {X Y : C} (f : X ⟶ Y) [mono f], normal_mono f) (normal_epi : Π {X Y : C} (f : X ⟶ Y) [epi f], normal_epi f) attribute [instance, priority 100] abelian.has_finite_products attribute [instance, priority 100] abelian.has_kernels abelian.has_cokernels end category_theory open category_theory namespace category_theory.abelian variables {C : Type u} [category.{v} C] [abelian C] /-- An abelian category has finite biproducts. -/ @[priority 100] instance has_finite_biproducts : has_finite_biproducts C := limits.has_finite_biproducts.of_has_finite_products @[priority 100] instance has_binary_biproducts : has_binary_biproducts C := limits.has_binary_biproducts_of_finite_biproducts _ section to_non_preadditive_abelian /-- Every abelian category is, in particular, `non_preadditive_abelian`. -/ def non_preadditive_abelian : non_preadditive_abelian C := { ..‹abelian C› } end to_non_preadditive_abelian section strong local attribute [instance] abelian.normal_epi /-- In an abelian category, every epimorphism is strong. -/ lemma strong_epi_of_epi {P Q : C} (f : P ⟶ Q) [epi f] : strong_epi f := by apply_instance end strong section mono_epi_iso variables {X Y : C} (f : X ⟶ Y) local attribute [instance] strong_epi_of_epi /-- In an abelian category, a monomorphism which is also an epimorphism is an isomorphism. -/ lemma is_iso_of_mono_of_epi [mono f] [epi f] : is_iso f := is_iso_of_mono_of_strong_epi _ end mono_epi_iso section factor local attribute [instance] non_preadditive_abelian variables {P Q : C} (f : P ⟶ Q) section lemma mono_of_zero_kernel (R : C) (l : is_limit (kernel_fork.of_ι (0 : R ⟶ P) (show 0 ≫ f = 0, by simp))) : mono f := non_preadditive_abelian.mono_of_zero_kernel _ _ l lemma mono_of_kernel_ι_eq_zero (h : kernel.ι f = 0) : mono f := mono_of_kernel_zero h lemma epi_of_zero_cokernel (R : C) (l : is_colimit (cokernel_cofork.of_π (0 : Q ⟶ R) (show f ≫ 0 = 0, by simp))) : epi f := non_preadditive_abelian.epi_of_zero_cokernel _ _ l lemma epi_of_cokernel_π_eq_zero (h : cokernel.π f = 0) : epi f := begin apply epi_of_zero_cokernel _ (cokernel f), simp_rw ←h, exact is_colimit.of_iso_colimit (colimit.is_colimit (parallel_pair f 0)) (iso_of_π _) end end namespace images /-- The kernel of the cokernel of `f` is called the image of `f`. -/ protected abbreviation image : C := kernel (cokernel.π f) /-- The inclusion of the image into the codomain. -/ protected abbreviation image.ι : images.image f ⟶ Q := kernel.ι (cokernel.π f) /-- There is a canonical epimorphism `p : P ⟶ image f` for every `f`. -/ protected abbreviation factor_thru_image : P ⟶ images.image f := kernel.lift (cokernel.π f) f $ cokernel.condition f /-- `f` factors through its image via the canonical morphism `p`. -/ @[simp, reassoc] protected lemma image.fac : images.factor_thru_image f ≫ image.ι f = f := kernel.lift_ι _ _ _ /-- The map `p : P ⟶ image f` is an epimorphism -/ instance : epi (images.factor_thru_image f) := show epi (non_preadditive_abelian.factor_thru_image f), by apply_instance section variables {f} lemma image_ι_comp_eq_zero {R : C} {g : Q ⟶ R} (h : f ≫ g = 0) : images.image.ι f ≫ g = 0 := zero_of_epi_comp (images.factor_thru_image f) $ by simp [h] end instance mono_factor_thru_image [mono f] : mono (images.factor_thru_image f) := mono_of_mono_fac $ image.fac f instance is_iso_factor_thru_image [mono f] : is_iso (images.factor_thru_image f) := is_iso_of_mono_of_epi _ /-- Factoring through the image is a strong epi-mono factorisation. -/ @[simps] def image_strong_epi_mono_factorisation : strong_epi_mono_factorisation f := { I := images.image f, m := image.ι f, m_mono := by apply_instance, e := images.factor_thru_image f, e_strong_epi := strong_epi_of_epi _ } end images namespace coimages /-- The cokernel of the kernel of `f` is called the coimage of `f`. -/ protected abbreviation coimage : C := cokernel (kernel.ι f) /-- The projection onto the coimage. -/ protected abbreviation coimage.π : P ⟶ coimages.coimage f := cokernel.π (kernel.ι f) /-- There is a canonical monomorphism `i : coimage f ⟶ Q`. -/ protected abbreviation factor_thru_coimage : coimages.coimage f ⟶ Q := cokernel.desc (kernel.ι f) f $ kernel.condition f /-- `f` factors through its coimage via the canonical morphism `p`. -/ protected lemma coimage.fac : coimage.π f ≫ coimages.factor_thru_coimage f = f := cokernel.π_desc _ _ _ /-- The canonical morphism `i : coimage f ⟶ Q` is a monomorphism -/ instance : mono (coimages.factor_thru_coimage f) := show mono (non_preadditive_abelian.factor_thru_coimage f), by apply_instance instance epi_factor_thru_coimage [epi f] : epi (coimages.factor_thru_coimage f) := epi_of_epi_fac $ coimage.fac f instance is_iso_factor_thru_coimage [epi f] : is_iso (coimages.factor_thru_coimage f) := is_iso_of_mono_of_epi _ /-- Factoring through the coimage is a strong epi-mono factorisation. -/ @[simps] def coimage_strong_epi_mono_factorisation : strong_epi_mono_factorisation f := { I := coimages.coimage f, m := coimages.factor_thru_coimage f, m_mono := by apply_instance, e := coimage.π f, e_strong_epi := strong_epi_of_epi _ } end coimages end factor section has_strong_epi_mono_factorisations /-- An abelian category has strong epi-mono factorisations. -/ @[priority 100] instance : has_strong_epi_mono_factorisations C := has_strong_epi_mono_factorisations.mk $ λ X Y f, images.image_strong_epi_mono_factorisation f /- In particular, this means that it has well-behaved images. -/ example : has_images C := by apply_instance example : has_image_maps C := by apply_instance end has_strong_epi_mono_factorisations section images variables {X Y : C} (f : X ⟶ Y) /-- There is a canonical isomorphism between the coimage and the image of a morphism. -/ abbreviation coimage_iso_image : coimages.coimage f ≅ images.image f := is_image.iso_ext (coimages.coimage_strong_epi_mono_factorisation f).to_mono_is_image (images.image_strong_epi_mono_factorisation f).to_mono_is_image /-- There is a canonical isomorphism between the abelian image and the categorical image of a morphism. -/ abbreviation image_iso_image : images.image f ≅ image f := is_image.iso_ext (images.image_strong_epi_mono_factorisation f).to_mono_is_image (image.is_image f) /-- There is a canonical isomorphism between the abelian coimage and the categorical image of a morphism. -/ abbreviation coimage_iso_image' : coimages.coimage f ≅ image f := is_image.iso_ext (coimages.coimage_strong_epi_mono_factorisation f).to_mono_is_image (image.is_image f) lemma full_image_factorisation : coimages.coimage.π f ≫ (coimage_iso_image f).hom ≫ images.image.ι f = f := by rw [limits.is_image.iso_ext_hom, ←images.image_strong_epi_mono_factorisation_to_mono_factorisation_m, is_image.lift_fac, coimages.coimage_strong_epi_mono_factorisation_to_mono_factorisation_m, coimages.coimage.fac] end images section cokernel_of_kernel variables {X Y : C} {f : X ⟶ Y} local attribute [instance] non_preadditive_abelian /-- In an abelian category, an epi is the cokernel of its kernel. More precisely: If `f` is an epimorphism and `s` is some limit kernel cone on `f`, then `f` is a cokernel of `fork.ι s`. -/ def epi_is_cokernel_of_kernel [epi f] (s : fork f 0) (h : is_limit s) : is_colimit (cokernel_cofork.of_π f (kernel_fork.condition s)) := non_preadditive_abelian.epi_is_cokernel_of_kernel s h /-- In an abelian category, a mono is the kernel of its cokernel. More precisely: If `f` is a monomorphism and `s` is some colimit cokernel cocone on `f`, then `f` is a kernel of `cofork.π s`. -/ def mono_is_kernel_of_cokernel [mono f] (s : cofork f 0) (h : is_colimit s) : is_limit (kernel_fork.of_ι f (cokernel_cofork.condition s)) := non_preadditive_abelian.mono_is_kernel_of_cokernel s h end cokernel_of_kernel section @[priority 100] instance has_equalizers : has_equalizers C := preadditive.has_equalizers_of_has_kernels /-- Any abelian category has pullbacks -/ @[priority 100] instance has_pullbacks : has_pullbacks C := has_pullbacks_of_has_binary_products_of_has_equalizers C end section @[priority 100] instance has_coequalizers : has_coequalizers C := preadditive.has_coequalizers_of_has_cokernels /-- Any abelian category has pushouts -/ @[priority 100] instance has_pushouts : has_pushouts C := has_pushouts_of_has_binary_coproducts_of_has_coequalizers C end namespace pullback_to_biproduct_is_kernel variables [limits.has_pullbacks C] {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) /-! This section contains a slightly technical result about pullbacks and biproducts. We will need it in the proof that the pullback of an epimorphism is an epimorpism. -/ /-- The canonical map `pullback f g ⟶ X ⊞ Y` -/ abbreviation pullback_to_biproduct : pullback f g ⟶ X ⊞ Y := biprod.lift pullback.fst pullback.snd /-- The canonical map `pullback f g ⟶ X ⊞ Y` induces a kernel cone on the map `biproduct X Y ⟶ Z` induced by `f` and `g`. A slightly more intuitive way to think of this may be that it induces an equalizer fork on the maps induced by `(f, 0)` and `(0, g)`. -/ abbreviation pullback_to_biproduct_fork : kernel_fork (biprod.desc f (-g)) := kernel_fork.of_ι (pullback_to_biproduct f g) $ by rw [biprod.lift_desc, comp_neg, pullback.condition, add_right_neg] /-- The canonical map `pullback f g ⟶ X ⊞ Y` is a kernel of the map induced by `(f, -g)`. -/ def is_limit_pullback_to_biproduct : is_limit (pullback_to_biproduct_fork f g) := fork.is_limit.mk _ (λ s, pullback.lift (fork.ι s ≫ biprod.fst) (fork.ι s ≫ biprod.snd) $ sub_eq_zero.1 $ by rw [category.assoc, category.assoc, ←comp_sub, sub_eq_add_neg, ←comp_neg, ←biprod.desc_eq, kernel_fork.condition s]) (λ s, begin ext; rw [fork.ι_of_ι, category.assoc], { rw [biprod.lift_fst, pullback.lift_fst] }, { rw [biprod.lift_snd, pullback.lift_snd] } end) (λ s m h, by ext; simp [fork.ι_eq_app_zero, ←h walking_parallel_pair.zero]) end pullback_to_biproduct_is_kernel namespace biproduct_to_pushout_is_cokernel variables [limits.has_pushouts C] {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) /-- The canonical map `Y ⊞ Z ⟶ pushout f g` -/ abbreviation biproduct_to_pushout : Y ⊞ Z ⟶ pushout f g := biprod.desc pushout.inl pushout.inr /-- The canonical map `Y ⊞ Z ⟶ pushout f g` induces a cokernel cofork on the map `X ⟶ Y ⊞ Z` induced by `f` and `-g`. -/ abbreviation biproduct_to_pushout_cofork : cokernel_cofork (biprod.lift f (-g)) := cokernel_cofork.of_π (biproduct_to_pushout f g) $ by rw [biprod.lift_desc, neg_comp, pushout.condition, add_right_neg] /-- The cofork induced by the canonical map `Y ⊞ Z ⟶ pushout f g` is in fact a colimit cokernel cofork. -/ def is_colimit_biproduct_to_pushout : is_colimit (biproduct_to_pushout_cofork f g) := cofork.is_colimit.mk _ (λ s, pushout.desc (biprod.inl ≫ cofork.π s) (biprod.inr ≫ cofork.π s) $ sub_eq_zero.1 $ by rw [←category.assoc, ←category.assoc, ←sub_comp, sub_eq_add_neg, ←neg_comp, ←biprod.lift_eq, cofork.condition s, zero_comp]) (λ s, by ext; simp) (λ s m h, by ext; simp [cofork.π_eq_app_one, ←h walking_parallel_pair.one] ) end biproduct_to_pushout_is_cokernel section epi_pullback variables [limits.has_pullbacks C] {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) /-- In an abelian category, the pullback of an epimorphism is an epimorphism. Proof from [aluffi2016, IX.2.3], cf. [borceux-vol2, 1.7.6] -/ instance epi_pullback_of_epi_f [epi f] : epi (pullback.snd : pullback f g ⟶ Y) := -- It will suffice to consider some morphism e : Y ⟶ R such that -- pullback.snd ≫ e = 0 and show that e = 0. epi_of_cancel_zero _ $ λ R e h, begin -- Consider the morphism u := (0, e) : X ⊞ Y⟶ R. let u := biprod.desc (0 : X ⟶ R) e, -- The composite pullback f g ⟶ X ⊞ Y ⟶ R is zero by assumption. have hu : pullback_to_biproduct_is_kernel.pullback_to_biproduct f g ≫ u = 0 := by simpa, -- pullback_to_biproduct f g is a kernel of (f, -g), so (f, -g) is a -- cokernel of pullback_to_biproduct f g have := epi_is_cokernel_of_kernel _ (pullback_to_biproduct_is_kernel.is_limit_pullback_to_biproduct f g), -- We use this fact to obtain a factorization of u through (f, -g) via some d : Z ⟶ R. obtain ⟨d, hd⟩ := cokernel_cofork.is_colimit.desc' this u hu, change Z ⟶ R at d, change biprod.desc f (-g) ≫ d = u at hd, -- But then f ≫ d = 0: have : f ≫ d = 0, calc f ≫ d = (biprod.inl ≫ biprod.desc f (-g)) ≫ d : by rw biprod.inl_desc ... = biprod.inl ≫ u : by rw [category.assoc, hd] ... = 0 : biprod.inl_desc _ _, -- But f is an epimorphism, so d = 0... have : d = 0 := (cancel_epi f).1 (by simpa), -- ...or, in other words, e = 0. calc e = biprod.inr ≫ u : by rw biprod.inr_desc ... = biprod.inr ≫ biprod.desc f (-g) ≫ d : by rw ←hd ... = biprod.inr ≫ biprod.desc f (-g) ≫ 0 : by rw this ... = (biprod.inr ≫ biprod.desc f (-g)) ≫ 0 : by rw ←category.assoc ... = 0 : has_zero_morphisms.comp_zero _ _ end /-- In an abelian category, the pullback of an epimorphism is an epimorphism. -/ instance epi_pullback_of_epi_g [epi g] : epi (pullback.fst : pullback f g ⟶ X) := -- It will suffice to consider some morphism e : X ⟶ R such that -- pullback.fst ≫ e = 0 and show that e = 0. epi_of_cancel_zero _ $ λ R e h, begin -- Consider the morphism u := (e, 0) : X ⊞ Y ⟶ R. let u := biprod.desc e (0 : Y ⟶ R), -- The composite pullback f g ⟶ X ⊞ Y ⟶ R is zero by assumption. have hu : pullback_to_biproduct_is_kernel.pullback_to_biproduct f g ≫ u = 0 := by simpa, -- pullback_to_biproduct f g is a kernel of (f, -g), so (f, -g) is a -- cokernel of pullback_to_biproduct f g have := epi_is_cokernel_of_kernel _ (pullback_to_biproduct_is_kernel.is_limit_pullback_to_biproduct f g), -- We use this fact to obtain a factorization of u through (f, -g) via some d : Z ⟶ R. obtain ⟨d, hd⟩ := cokernel_cofork.is_colimit.desc' this u hu, change Z ⟶ R at d, change biprod.desc f (-g) ≫ d = u at hd, -- But then (-g) ≫ d = 0: have : (-g) ≫ d = 0, calc (-g) ≫ d = (biprod.inr ≫ biprod.desc f (-g)) ≫ d : by rw biprod.inr_desc ... = biprod.inr ≫ u : by rw [category.assoc, hd] ... = 0 : biprod.inr_desc _ _, -- But g is an epimorphism, thus so is -g, so d = 0... have : d = 0 := (cancel_epi (-g)).1 (by simpa), -- ...or, in other words, e = 0. calc e = biprod.inl ≫ u : by rw biprod.inl_desc ... = biprod.inl ≫ biprod.desc f (-g) ≫ d : by rw ←hd ... = biprod.inl ≫ biprod.desc f (-g) ≫ 0 : by rw this ... = (biprod.inl ≫ biprod.desc f (-g)) ≫ 0 : by rw ←category.assoc ... = 0 : has_zero_morphisms.comp_zero _ _ end end epi_pullback section mono_pushout variables [limits.has_pushouts C] {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) instance mono_pushout_of_mono_f [mono f] : mono (pushout.inr : Z ⟶ pushout f g) := mono_of_cancel_zero _ $ λ R e h, begin let u := biprod.lift (0 : R ⟶ Y) e, have hu : u ≫ biproduct_to_pushout_is_cokernel.biproduct_to_pushout f g = 0 := by simpa, have := mono_is_kernel_of_cokernel _ (biproduct_to_pushout_is_cokernel.is_colimit_biproduct_to_pushout f g), obtain ⟨d, hd⟩ := kernel_fork.is_limit.lift' this u hu, change R ⟶ X at d, change d ≫ biprod.lift f (-g) = u at hd, have : d ≫ f = 0, calc d ≫ f = d ≫ biprod.lift f (-g) ≫ biprod.fst : by rw biprod.lift_fst ... = u ≫ biprod.fst : by rw [←category.assoc, hd] ... = 0 : biprod.lift_fst _ _, have : d = 0 := (cancel_mono f).1 (by simpa), calc e = u ≫ biprod.snd : by rw biprod.lift_snd ... = (d ≫ biprod.lift f (-g)) ≫ biprod.snd : by rw ←hd ... = (0 ≫ biprod.lift f (-g)) ≫ biprod.snd : by rw this ... = 0 ≫ biprod.lift f (-g) ≫ biprod.snd : by rw category.assoc ... = 0 : zero_comp end instance mono_pushout_of_mono_g [mono g] : mono (pushout.inl : Y ⟶ pushout f g) := mono_of_cancel_zero _ $ λ R e h, begin let u := biprod.lift e (0 : R ⟶ Z), have hu : u ≫ biproduct_to_pushout_is_cokernel.biproduct_to_pushout f g = 0 := by simpa, have := mono_is_kernel_of_cokernel _ (biproduct_to_pushout_is_cokernel.is_colimit_biproduct_to_pushout f g), obtain ⟨d, hd⟩ := kernel_fork.is_limit.lift' this u hu, change R ⟶ X at d, change d ≫ biprod.lift f (-g) = u at hd, have : d ≫ (-g) = 0, calc d ≫ (-g) = d ≫ biprod.lift f (-g) ≫ biprod.snd : by rw biprod.lift_snd ... = u ≫ biprod.snd : by rw [←category.assoc, hd] ... = 0 : biprod.lift_snd _ _, have : d = 0 := (cancel_mono (-g)).1 (by simpa), calc e = u ≫ biprod.fst : by rw biprod.lift_fst ... = (d ≫ biprod.lift f (-g)) ≫ biprod.fst : by rw ←hd ... = (0 ≫ biprod.lift f (-g)) ≫ biprod.fst : by rw this ... = 0 ≫ biprod.lift f (-g) ≫ biprod.fst : by rw category.assoc ... = 0 : zero_comp end end mono_pushout end category_theory.abelian namespace category_theory.non_preadditive_abelian variables (C : Type u) [category.{v} C] [non_preadditive_abelian C] /-- Every non_preadditive_abelian category can be promoted to an abelian category. -/ def abelian : abelian C := { has_finite_products := by apply_instance, /- We need the `convert`s here because the instances we have are slightly different from the instances we need: `has_kernels` depends on an instance of `has_zero_morphisms`. In the case of `non_preadditive_abelian`, this instance is an explicit argument. However, in the case of `abelian`, the `has_zero_morphisms` instance is derived from `preadditive`. So we need to transform an instance of "has kernels with non_preadditive_abelian.has_zero_morphisms" to an instance of "has kernels with non_preadditive_abelian.preadditive.has_zero_morphisms". Luckily, we have a `subsingleton` instance for `has_zero_morphisms`, so `convert` can immediately close the goal it creates for the two instances of `has_zero_morphisms`, and the proof is complete. -/ has_kernels := by convert (by apply_instance : limits.has_kernels C), has_cokernels := by convert (by apply_instance : limits.has_cokernels C), normal_mono := by { introsI, convert normal_mono f }, normal_epi := by { introsI, convert normal_epi f }, ..non_preadditive_abelian.preadditive } end category_theory.non_preadditive_abelian
73361a0cdaa2b2e7fa6902ab3a00d5b1a8643b6b
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/linear_algebra/matrix/trace.lean
072a4a21a9eea4e191954ef0b57e336154fffc17
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
3,687
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, Patrick Massot, Casper Putz, Anne Baanen -/ import data.matrix.basic /-! # Trace of a matrix This file defines the trace of a matrix, the linear map sending a matrix to the sum of its diagonal entries. See also `linear_algebra.trace` for the trace of an endomorphism. ## Tags matrix, trace, diagonal -/ open_locale big_operators open_locale matrix namespace matrix section trace universes u v w variables {m : Type*} (n : Type*) {p : Type*} variables (R : Type*) (M : Type*) [semiring R] [add_comm_monoid M] [module R M] /-- The diagonal of a square matrix. -/ def diag : (matrix n n M) →ₗ[R] n → M := { to_fun := λ A i, A i i, map_add' := by { intros, ext, refl, }, map_smul' := by { intros, ext, refl, } } variables {n} {R} {M} @[simp] lemma diag_apply (A : matrix n n M) (i : n) : diag n R M A i = A i i := rfl @[simp] lemma diag_one [decidable_eq n] : diag n R R 1 = λ i, 1 := by { dunfold diag, ext, simp [one_apply_eq] } @[simp] lemma diag_transpose (A : matrix n n M) : diag n R M Aᵀ = diag n R M A := rfl @[simp] lemma diag_col_mul_row (a b : n → R) : diag n R R (col a ⬝ row b) = a * b := by { ext, simp [matrix.mul_apply] } variables (n) (R) (M) /-- The trace of a square matrix. -/ def trace [fintype n] : (matrix n n M) →ₗ[R] M := { to_fun := λ A, ∑ i, diag n R M A i, map_add' := by { intros, apply finset.sum_add_distrib, }, map_smul' := by { intros, simp [finset.smul_sum], } } variables {n} {R} {M} [fintype n] [fintype m] [fintype p] @[simp] lemma trace_diag (A : matrix n n M) : trace n R M A = ∑ i, diag n R M A i := rfl lemma trace_apply (A : matrix n n M) : trace n R M A = ∑ i, A i i := rfl @[simp] lemma trace_one [decidable_eq n] : trace n R R 1 = fintype.card n := have h : trace n R R 1 = ∑ i, diag n R R 1 i := rfl, by simp_rw [h, diag_one, finset.sum_const, nsmul_one]; refl @[simp] lemma trace_transpose (A : matrix n n M) : trace n R M Aᵀ = trace n R M A := rfl @[simp] lemma trace_transpose_mul (A : matrix m n R) (B : matrix n m R) : trace n R R (Aᵀ ⬝ Bᵀ) = trace m R R (A ⬝ B) := finset.sum_comm lemma trace_mul_comm {S : Type v} [comm_semiring S] (A : matrix m n S) (B : matrix n m S) : trace m S S (A ⬝ B) = trace n S S (B ⬝ A) := by rw [←trace_transpose, ←trace_transpose_mul, transpose_mul] lemma trace_mul_cycle {S : Type v} [comm_semiring S] (A : matrix m n S) (B : matrix n p S) (C : matrix p m S) : trace _ S S (A ⬝ B ⬝ C) = trace p S S (C ⬝ A ⬝ B) := by rw [trace_mul_comm, matrix.mul_assoc] lemma trace_mul_cycle' {S : Type v} [comm_semiring S] (A : matrix m n S) (B : matrix n p S) (C : matrix p m S) : trace _ S S (A ⬝ (B ⬝ C)) = trace p S S (C ⬝ (A ⬝ B)) := by rw [←matrix.mul_assoc, trace_mul_comm] @[simp] lemma trace_col_mul_row (a b : n → R) : trace n R R (col a ⬝ row b) = dot_product a b := by simp [dot_product] /-! ### Special cases for `fin n` While `simp [fin.sum_univ_succ]` can prove these, we include them for convenience and consistency with `matrix.det_fin_two` etc. -/ @[simp] lemma trace_fin_zero (A : matrix (fin 0) (fin 0) R) : trace _ R R A = 0 := rfl lemma trace_fin_one (A : matrix (fin 1) (fin 1) R) : trace _ R R A = A 0 0 := add_zero _ lemma trace_fin_two (A : matrix (fin 2) (fin 2) R) : trace _ R R A = A 0 0 + A 1 1 := congr_arg ((+) _) (add_zero (A 1 1)) lemma trace_fin_three (A : matrix (fin 3) (fin 3) R) : trace _ R R A = A 0 0 + A 1 1 + A 2 2 := by { rw [← add_zero (A 2 2), add_assoc], refl } end trace end matrix
438010083f2ab29f5d8eca196c20962ea65a66b7
ea5678cc400c34ff95b661fa26d15024e27ea8cd
/exp/exponential.lean
3ea1da1a3282165be8ca5e1b324b0fa1ab9501bb
[]
no_license
ChrisHughes24/leanstuff
dca0b5349c3ed893e8792ffbd98cbcadaff20411
9efa85f72efaccd1d540385952a6acc18fce8687
refs/heads/master
1,654,883,241,759
1,652,873,885,000
1,652,873,885,000
134,599,537
1
0
null
null
null
null
UTF-8
Lean
false
false
21,339
lean
import tactic.norm_num data.nat.basic tactic.ring algebra.archimedean .limits data.nat.binomial section a open real nat is_absolute_value finset noncomputable theory local attribute [instance, priority 0] classical.prop_decidable lemma pow_inv' {α : Type*} [discrete_field α] (a : α) (n : ℕ) : (a ^ n)⁻¹ = a⁻¹ ^ n := by induction n; simp [_root_.pow_succ, *, mul_inv', mul_comm] lemma pow_incrs_of_gt_one {α : Type*} [linear_ordered_semiring α] {x : α} {n m : ℕ} (h1x : 1 < x) (hnm : n < m) : x ^ n < x ^ m := begin rw [← nat.sub_add_cancel hnm, _root_.pow_add, _root_.pow_succ, ← mul_assoc], refine (lt_mul_iff_one_lt_left (pow_pos (lt_trans (by norm_num) h1x) _)).2 _, rw ← one_mul (1 : α), refine mul_lt_mul' (one_le_pow_of_one_le (le_of_lt h1x) _) h1x (by norm_num) (pow_pos (lt_trans (by norm_num) h1x) _) end lemma pow_dcrs_of_lt_one_of_pos {α : Type*} [discrete_linear_ordered_field α] {x : α} {n m : ℕ} (hx1 : x < 1) (h0x : 0 < x) (hnm : n < m) : x ^ m < x ^ n := begin refine (inv_lt_inv _ _).1 _, { exact pow_pos h0x _ }, { exact pow_pos h0x _ }, { rw [pow_inv', pow_inv'], refine pow_incrs_of_gt_one (one_lt_inv h0x hx1) hnm } end open finset lemma geo_series_eq {α : Type*} [field α] {x : α} (n : ℕ) (hx1 : x ≠ 1) : (range n).sum (λ m, x ^ m) = (1 - x ^ n) / (1 - x) := have 1 - x ≠ 0 := mt sub_eq_zero_iff_eq.1 hx1.symm, begin induction n with n ih, { simp }, { rw [sum_range_succ, ← mul_div_cancel (x ^ n) this, ih, ← add_div, _root_.pow_succ], refine congr_fun (congr_arg _ _) _, ring } end variables {α : Type*} {β : Type*} [discrete_linear_ordered_field α] [archimedean α] {abv : β → α} lemma forall_ge_le_of_forall_le_succ {α : Type*} [preorder α] (f : ℕ → α) {m : ℕ} (h : ∀ n ≥ m, f (succ n) ≤ f n) : ∀ l, ∀ k ≥ m, k ≤ l → f l ≤ f k := begin assume l k hkm hkl, generalize hp : l - k = p, have : l = k + p := add_comm p k ▸ (nat.sub_eq_iff_eq_add hkl).1 hp, subst this, clear hkl hp, induction p with p ih, { simp }, { exact le_trans (h _ (le_trans hkm (nat.le_add_right _ _))) ih } end lemma is_cau_of_dcrs_bounded {α : Type*} [discrete_linear_ordered_field α] [archimedean α] (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a) (hnm : ∀ n ≥ m, f (succ n) ≤ f n) : is_cau_seq abs f := λ ε ε0, let ⟨k, hk⟩ := archimedean.arch a ε0 in have h : ∃ l, ∀ n ≥ m, a - add_monoid.smul l ε < f n := ⟨k + k + 1, λ n hnm, lt_of_lt_of_le (show a - add_monoid.smul (k + (k + 1)) ε < -abs (f n), from lt_neg.1 $ lt_of_le_of_lt (ham n hnm) (begin rw [neg_sub, lt_sub_iff_add_lt, add_monoid.add_smul], exact add_lt_add_of_le_of_lt hk (lt_of_le_of_lt hk (lt_add_of_pos_left _ ε0)), end)) (neg_le.2 $ (abs_neg (f n)) ▸ le_abs_self _)⟩, let l := nat.find h in have hl : ∀ (n : ℕ), n ≥ m → f n > a - add_monoid.smul l ε := nat.find_spec h, have hl0 : l ≠ 0 := λ hl0, not_lt_of_ge (ham m (le_refl _)) (lt_of_lt_of_le (by have := hl m (le_refl m); simpa [hl0] using this) (le_abs_self (f m))), begin cases classical.not_forall.1 (nat.find_min h (pred_lt hl0)) with i hi, rw [not_imp, not_lt] at hi, existsi i, assume j hj, have hfij : f j ≤ f i := forall_ge_le_of_forall_le_succ f hnm _ _ hi.1 hj, rw [abs_of_nonpos (sub_nonpos.2 hfij), neg_sub, sub_lt_iff_lt_add'], exact calc f i ≤ a - add_monoid.smul (pred l) ε : hi.2 ... = a - add_monoid.smul l ε + ε : by conv {to_rhs, rw [← succ_pred_eq_of_pos (nat.pos_of_ne_zero hl0), succ_smul', sub_add, add_sub_cancel] } ... < f j + ε : add_lt_add_right (hl j (le_trans hi.1 hj)) _ end lemma is_cau_of_incrs_bounded {α : Type*} [discrete_linear_ordered_field α] [archimedean α] (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a) (hnm : ∀ n ≥ m, f n ≤ f (succ n)) : is_cau_seq abs f := begin refine @eq.rec_on (ℕ → α) _ (is_cau_seq abs) _ _ (-⟨_, @is_cau_of_dcrs_bounded _ _ _ (λ n, -f n) a m (by simpa) (by simpa)⟩ : cau_seq α abs).2, ext, exact neg_neg _ end lemma series_cau_of_abv_le_cau {α : Type*} {β : Type*} [discrete_linear_ordered_field α] [ring β] {f : ℕ → β} {g : ℕ → α} {abv : β → α} [is_absolute_value abv] (n : ℕ) : (∀ m, n ≤ m → abv (f m) ≤ g m) → is_cau_seq abs (λ n, (range n).sum g) → is_cau_seq abv (λ n, (range n).sum f) := begin assume hm hg ε ε0,cases hg (ε / 2) (div_pos ε0 (by norm_num)) with i hi, existsi max n i, assume j ji, have hi₁ := hi j (le_trans (le_max_right n i) ji), have hi₂ := hi (max n i) (le_max_right n i), have sub_le := abs_sub_le ((range j).sum g) ((range i).sum g) ((range (max n i)).sum g), have := add_lt_add hi₁ hi₂, rw abs_sub ((range (max n i)).sum g) at this, rw add_halves ε at this, refine lt_of_le_of_lt (le_trans (le_trans _ (le_abs_self _)) sub_le) this, generalize hk : j - max n i = k,clear this hi₂ hi₁ hi ε0 ε hg sub_le, rw nat.sub_eq_iff_eq_add ji at hk,rw hk, clear hk ji j, induction k with k' hi, { simp [abv_zero abv] }, { dsimp at *, rw [succ_add, sum_range_succ, sum_range_succ, add_assoc, add_assoc], refine le_trans (abv_add _ _ _) _, exact add_le_add (hm _ (le_add_of_nonneg_of_le (nat.zero_le _) (le_max_left _ _))) hi }, end lemma pow_abv {α β : Type*} [discrete_linear_ordered_field α] [domain β] (abv : β → α) [is_absolute_value abv] (a : β) (n : ℕ) : abv (a ^ n) = abv a ^ n := by induction n; simp [abv_mul abv, _root_.pow_succ, abv_one abv, *] lemma series_cau_of_abv_cau {α : Type*} {β : Type*} [discrete_linear_ordered_field α] [ring β] {abv : β → α} {f : ℕ → β} [is_absolute_value abv] : is_cau_seq abs (λ m, (range m).sum (λ n, abv (f n))) → is_cau_seq abv (λ m, (range m).sum f) := λ h, series_cau_of_abv_le_cau 0 (λ n h, le_refl _) h lemma geo_series_cau {α β : Type*} [discrete_linear_ordered_field α] [archimedean α] [field β] {abv : β → α} [is_absolute_value abv] (x : β) (hx1 : abv x < 1) : is_cau_seq abv (λ n, (range n).sum (λ m, x ^ m)) := have hx1' : abv x ≠ 1 := λ h, by simpa [h, lt_irrefl] using hx1, series_cau_of_abv_cau begin simp only [pow_abv abv, geo_series_eq _ hx1'] {eta := ff}, refine @is_cau_of_incrs_bounded _ _ _ _ ((1 : α) / (1 - abv x)) 0 _ _, { assume n hn, rw abs_of_nonneg , refine div_le_div_of_le_of_pos (sub_le_self _ (pow_abv abv x n ▸ abv_nonneg _ _)) (sub_pos.2 hx1), refine div_nonneg (sub_nonneg.2 _) (sub_pos.2 hx1), clear hn, induction n with n ih, { simp }, { rw [_root_.pow_succ, ← one_mul (1 : α)], refine mul_le_mul (le_of_lt hx1) ih (pow_abv abv x n ▸ abv_nonneg _ _) (by norm_num) } }, { assume n hn, refine div_le_div_of_le_of_pos (sub_le_sub_left _ _) (sub_pos.2 hx1), rw [← one_mul (_ ^ n), _root_.pow_succ], exact mul_le_mul_of_nonneg_right (le_of_lt hx1) (pow_nonneg (abv_nonneg _ _) _) } end lemma geo_series_const_cau {α : Type*} [discrete_linear_ordered_field α] [archimedean α] (a x : α) : abs x < 1 → is_cau_seq abs (λ m, (range m).sum (λ n, a * x ^ n)) := λ hx1, begin have : is_cau_seq abs (λ m, a * (range m).sum (λ n, x ^ n)) := (cau_seq.const abs a * ⟨_, geo_series_cau x hx1⟩).2, simpa only [mul_sum] using this end -- The form of ratio test with 0 ≤ r < 1, and abv (f (succ m)) ≤ r * abv (f m) handled zero terms of series the best lemma series_ratio_test {α : Type*} {β : Type*} [discrete_linear_ordered_field α] [ring β] [archimedean α] {abv : β → α} [is_absolute_value abv] {f : ℕ → β} (n : ℕ) (r : α) : 0 ≤ r → r < 1 → (∀ m, n ≤ m → abv (f (succ m)) ≤ r * abv (f m)) → is_cau_seq abv (λ m, (range m).sum f) := begin assume r0 r1 h, refine series_cau_of_abv_le_cau (succ n) _ (geo_series_const_cau (abv (f (succ n)) * r⁻¹ ^ (succ n)) r _), assume m mn, generalize hk : m - (succ n) = k,rw nat.sub_eq_iff_eq_add mn at hk, cases classical.em (r = 0) with r_zero r_pos,have m_pos := lt_of_lt_of_le (succ_pos n) mn, have := pred_le_pred mn,simp at this, have := h (pred m) this,simp[r_zero,succ_pred_eq_of_pos m_pos] at this, refine le_trans this _,refine mul_nonneg _ _, refine mul_nonneg (abv_nonneg _ _) (pow_nonneg (inv_nonneg.mpr r0) _),exact pow_nonneg r0 _, replace r_pos : 0 < r,cases lt_or_eq_of_le r0 with h h,exact h,exact absurd h.symm r_pos, revert m n, induction k with k' hi,assume m n h mn hk, rw [hk,zero_add,mul_right_comm,←pow_inv',←div_eq_mul_inv,mul_div_cancel], exact (ne_of_lt (pow_pos r_pos _)).symm, assume m n h mn hk,rw [hk,succ_add], have kn : k' + (succ n) ≥ (succ n), rw ←zero_add (succ n),refine add_le_add _ _,exact zero_le _,simp, replace hi := hi (k' + (succ n)) n h kn rfl, rw [(by simp [_root_.pow_succ'] : r ^ (succ (k' + succ n)) = r ^ (k' + succ n) * r),←mul_assoc], replace h := h (k' + succ n) (le_of_succ_le kn),rw mul_comm at h, exact le_trans h (mul_le_mul_of_nonneg_right hi r0), rwa abs_of_nonneg r0, end lemma sum_range_diag_flip {α : Type*} [add_comm_monoid α] (n : ℕ) (f : ℕ → ℕ → α) : (range n).sum (λ m, (range (m + 1)).sum (λ k, f k (m - k))) = (range n).sum (λ m, (range (n - m)).sum (f m)) := have h₁ : ((range n).sigma (range ∘ succ)).sum (λ (a : Σ m, ℕ), f (a.2) (a.1 - a.2)) = (range n).sum (λ m, (range (m + 1)).sum (λ k, f k (m - k))) := sum_sigma, have h₂ : ((range n).sigma (λ m, range (n - m))).sum (λ a : Σ (m : ℕ), ℕ, f (a.1) (a.2)) = (range n).sum (λ m, sum (range (n - m)) (f m)) := sum_sigma, h₁ ▸ h₂ ▸ sum_bij (λ a _, ⟨a.2, a.1 - a.2⟩) (λ a ha, have h₁ : a.1 < n := mem_range.1 (mem_sigma.1 ha).1, have h₂ : a.2 < succ a.1 := mem_range.1 (mem_sigma.1 ha).2, mem_sigma.2 ⟨mem_range.2 (lt_of_lt_of_le h₂ h₁), mem_range.2 ((nat.sub_lt_sub_right_iff (le_of_lt_succ h₂)).2 h₁)⟩) (λ _ _, rfl) (λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ha hb h, have ha : a₁ < n ∧ a₂ ≤ a₁ := ⟨mem_range.1 (mem_sigma.1 ha).1, le_of_lt_succ (mem_range.1 (mem_sigma.1 ha).2)⟩, have hb : b₁ < n ∧ b₂ ≤ b₁ := ⟨mem_range.1 (mem_sigma.1 hb).1, le_of_lt_succ (mem_range.1 (mem_sigma.1 hb).2)⟩, have h : a₂ = b₂ ∧ _ := sigma.mk.inj h, have h' : a₁ = b₁ - b₂ + a₂ := (nat.sub_eq_iff_eq_add ha.2).1 (eq_of_heq h.2), sigma.mk.inj_iff.2 ⟨nat.sub_add_cancel hb.2 ▸ h'.symm ▸ h.1 ▸ rfl, (heq_of_eq h.1)⟩) (λ ⟨a₁, a₂⟩ ha, have ha : a₁ < n ∧ a₂ < n - a₁ := ⟨mem_range.1 (mem_sigma.1 ha).1, (mem_range.1 (mem_sigma.1 ha).2)⟩, ⟨⟨a₂ + a₁, a₁⟩, ⟨mem_sigma.2 ⟨mem_range.2 ((nat.lt_sub_right_iff_add_lt (le_of_lt ha.1)).1 ha.2), mem_range.2 (lt_succ_of_le (le_add_left _ _))⟩, sigma.mk.inj_iff.2 ⟨rfl, heq_of_eq (nat.add_sub_cancel _ _).symm⟩⟩⟩) lemma abv_sum_le_sum_abv {α β γ : Type*} [discrete_linear_ordered_field α] [ring β] (abv : β → α) [is_absolute_value abv] (f : γ → β) (s : finset γ) : abv (s.sum f) ≤ s.sum (abv ∘ f) := finset.induction_on s (by simp [abv_zero abv]) $ λ a s has ih, by rw [sum_insert has, sum_insert has]; exact le_trans (abv_add abv _ _) (add_le_add_left ih _) lemma sum_nonneg {α β : Type*} [ordered_comm_monoid α] {f : β → α} {s : finset β} : (∀ x ∈ s, 0 ≤ f x) → 0 ≤ s.sum f := finset.induction_on s (by simp) $ λ a s has ih h, begin rw [sum_insert has], exact add_nonneg' (h a (by simp)) (ih (λx hx, by simp *)), end lemma neg_sum {α β : Type*} [add_comm_group α] {f : β → α} {s : finset β} : -s.sum f = s.sum (λ x, -f x) := finset.induction_on s (by simp) (by simp {contextual := tt}) @[simp] lemma filter_true {α : Type*} (s : finset α) : s.filter (λ _, true) = s := finset.ext.2 $ by simp lemma sum_range_sub_sum_range {α : Type*} [add_comm_group α] {f : ℕ → α} {n m : ℕ} (hnm : n ≤ m) : (range m).sum f - (range n).sum f = ((range m).filter (λ k, n ≤ k)).sum f := begin rw [← sum_sdiff (@filter_subset _ (λ k, n ≤ k) _ (range m)), sub_eq_iff_eq_add, ← eq_sub_iff_add_eq, add_sub_cancel'], refine finset.sum_congr (finset.ext.2 $ λ a, ⟨λ h, by simp at *; finish, λ h, have ham : a < m := lt_of_lt_of_le (mem_range.1 h) hnm, by simp * at *⟩) (λ _ _, rfl), end lemma series_cauchy_prod {α β : Type*} [discrete_linear_ordered_field α] [ring β] {a b : ℕ → β} {abv : β → α} [is_absolute_value abv] : is_cau_seq abs (λ m, (range m).sum (λ n, abv (a n))) → is_cau_seq abv (λ m, (range m).sum b) → ∀ ε : α, 0 < ε → ∃ i : ℕ, ∀ j ≥ i, abv ((range j).sum a * (range j).sum b - (range j).sum (λ n, (range (n + 1)).sum (λ m, a m * b (n - m))) ) < ε := begin -- slightly adapted version of theorem 9.4.7 from "The Real Numbers and Real Analysis", Ethan D. Bloch assume ha hb ε ε0, cases cau_seq.bounded ⟨_, hb⟩ with Q hQ,simp at hQ, cases cau_seq.bounded ⟨_, ha⟩ with P hP,simp at hP, have P0 : 0 < P,exact lt_of_le_of_lt (abs_nonneg _) (hP 0), have Pε0 := div_pos ε0 (mul_pos (show (2 : α) > 0, from by norm_num) P0), cases cau_seq.cauchy₂ ⟨_, hb⟩ Pε0 with N hN,simp at hN, have Qε0 := div_pos ε0 (mul_pos (show (4 : α) > 0, from by norm_num) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))), cases cau_seq.cauchy₂ ⟨_, ha⟩ Qε0 with M hM,simp at hM, existsi 2 * (max N M + 1), assume K hK, have := sum_range_diag_flip K (λ m n, a m * b n), dsimp at this, rw this, have : (λ (i : ℕ), (range (K - i)).sum (λ (k : ℕ), a i * b k)) = (λ (i : ℕ), a i * (range (K - i)).sum b), by simp [finset.mul_sum], rw this,clear this, have : (range K).sum (λ (i : ℕ), a i * (range (K - i)).sum b) = (range K).sum (λ (i : ℕ), a i * ((range (K - i)).sum b - (range K).sum b)) + (range K).sum (λ i, a i * (range K).sum b), {rw ←sum_add_distrib,simp[(mul_add _ _ _).symm]}, rw this, clear this, rw sum_mul, simp, rw abv_neg abv, refine lt_of_le_of_lt (abv_sum_le_sum_abv _ _ _) _, simp [abv_mul abv], suffices : (range (max N M + 1)).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) + ((range K).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) -(range (max N M + 1)).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b))) < ε / (2 * P) * P + ε / (4 * Q) * (2 * Q), { simp [(div_div_eq_div_mul _ _ _).symm] at this, rwa[div_mul_cancel _ (ne_of_lt P0).symm,(by norm_num : (4 : α) = 2 * 2),←div_div_eq_div_mul,mul_comm (2 : α),←_root_.mul_assoc, div_mul_cancel _ (ne_of_lt (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))).symm,div_mul_cancel,add_halves] at this, norm_num}, refine add_lt_add _ _, {have : (range (max N M + 1)).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) ≤ (range (max N M + 1)).sum (λ (i : ℕ), abv (a i) * (ε / (2 * P))) := by {refine sum_le_sum _,assume m mJ,refine mul_le_mul_of_nonneg_left _ _, {refine le_of_lt (hN (K - m) K _ _),{ refine nat.le_sub_left_of_add_le (le_trans _ hK), rw[succ_mul,_root_.one_mul], exact add_le_add (le_of_lt (mem_range.1 mJ)) (le_trans (le_max_left _ _) (le_of_lt (lt_add_one _)))}, {refine le_trans _ hK,rw ←_root_.one_mul N, refine mul_le_mul (by norm_num) (by rw _root_.one_mul;exact le_trans (le_max_left _ _) (le_of_lt (lt_add_one _))) (zero_le _) (zero_le _)}}, exact abv_nonneg abv _}, refine lt_of_le_of_lt this _, rw [← sum_mul, mul_comm], specialize hP (max N M + 1),rwa abs_of_nonneg at hP, exact (mul_lt_mul_left Pε0).mpr hP, exact sum_nonneg (λ x h, abv_nonneg abv _)}, {have hNMK : max N M + 1 < K := by {refine lt_of_lt_of_le _ hK, rw [succ_mul,_root_.one_mul,←add_zero (max N M + 1)], refine add_lt_add_of_le_of_lt (le_refl _) _,rw add_zero, refine add_pos_of_nonneg_of_pos (zero_le _) (by norm_num)}, rw sum_range_sub_sum_range (le_of_lt hNMK), exact calc sum (filter (λ (k : ℕ), max N M + 1 ≤ k) (range K)) (λ (i : ℕ), abv (a i) * abv (sum (range (K - i)) b - sum (range K) b)) ≤ sum (filter (λ (k : ℕ), max N M + 1 ≤ k) (range K)) (λ i, abv (a i) * (2 * Q)) : sum_le_sum (begin assume n hn, refine mul_le_mul_of_nonneg_left _ (abv_nonneg _ _), rw sub_eq_add_neg, refine le_trans (abv_add _ _ _) _, rw [two_mul, abv_neg abv], refine add_le_add (le_of_lt (hQ _)) (le_of_lt (hQ _)), end) ... < _ : begin rw [← sum_mul, ← sum_range_sub_sum_range (le_of_lt hNMK)], refine (mul_lt_mul_right _).2 _, rw two_mul, refine add_pos (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0)) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0)), refine lt_of_le_of_lt (le_abs_self _) _, refine hM _ _ (le_trans (le_succ_of_le (le_max_right _ _)) (le_of_lt hNMK)) (le_succ_of_le (le_max_right _ _)) end } end end a open nat finset lemma complex.exp_series_abs_cau (z : ℂ) : is_cau_seq abs (λ n, (range n).sum (λ m, complex.abs (z ^ m / fact m))) := begin cases exists_nat_gt (complex.abs z) with n hn, have n_pos : (0 : ℝ) < n := lt_of_le_of_lt (complex.abs_nonneg _) hn, refine series_ratio_test n (complex.abs z / n) _ _ _,exact div_nonneg_of_nonneg_of_pos (complex.abs_nonneg _) n_pos,rwa [div_lt_iff n_pos,one_mul], assume m mn,rw [abs_of_nonneg (complex.abs_nonneg _),abs_of_nonneg (complex.abs_nonneg _)], unfold fact,simp only [_root_.pow_succ, complex.abs_div,complex.abs_mul,div_eq_mul_inv,mul_inv', nat.cast_mul,complex.abs_inv], have : complex.abs z * complex.abs (z ^ m) * ((complex.abs ↑(fact m))⁻¹ * (complex.abs ↑(succ m))⁻¹) = complex.abs z * complex.abs (z ^ m) * (complex.abs ↑(fact m))⁻¹ * (complex.abs ↑(succ m))⁻¹,ring,rw this, have : complex.abs z * (↑n)⁻¹ * (complex.abs (z ^ m) * (complex.abs ↑(fact m))⁻¹) = complex.abs z * complex.abs (z ^ m) * (complex.abs ↑(fact m))⁻¹ * (↑n)⁻¹,ring, rw this, rw[(by simp : (succ m : ℂ) = ((succ m : ℝ) : ℂ)),complex.abs_of_nonneg], refine mul_le_mul_of_nonneg_left _ _, rw [inv_le_inv,nat.cast_le],exact le_succ_of_le mn, rw [←nat.cast_zero,nat.cast_lt],exact succ_pos _,exact n_pos,rw[←complex.abs_inv,←complex.abs_mul,←complex.abs_mul], exact complex.abs_nonneg _,rw[←nat.cast_zero,nat.cast_le],exact zero_le _, end lemma complex.exp_series_cau (z : ℂ) : is_cau_seq complex.abs (λ n, (range n).sum (λ m, z ^ m / fact m)) := series_cau_of_abv_cau (complex.exp_series_abs_cau z) def exp' (z : ℂ) : cau_seq ℂ complex.abs := ⟨_, complex.exp_series_cau z⟩ open complex def exp (z : ℂ) : ℂ := complex.lim (exp' z) def sin (z : ℂ) : ℂ := (exp (I * z) - exp (-I * z)) / (2 * I) def cos (z : ℂ) : ℂ := (exp (I * z) + exp (-I * z)) / 2 def tan (z : ℂ) : ℂ := sin z / cos z def sinh (z : ℂ) : ℂ := (exp z - exp (-z)) / 2 def cosh (z : ℂ) : ℂ := (exp z + exp (-z)) / 2 def tanh (z : ℂ) : ℂ := sinh z / cosh z @[simp] lemma exp_zero : exp 0 = 1 := begin unfold exp exp', refine lim_eq_of_equiv_const _, assume ε ε0, existsi 1, assume j hj, dsimp [exp'], suffices : complex.abs (sum (range j) (λ (m : ℕ), 0 ^ m / ↑(fact m)) + -1) = 0, rwa this, cases j, { exact absurd hj (by norm_num) }, { induction j, { simpa }, { rw ← j_ih dec_trivial, simp only [sum_range_succ, _root_.pow_succ], simp } } end lemma exp_add (x y : ℂ) : exp (x + y) = exp x * exp y := show complex.lim (⟨_, complex.exp_series_cau (x + y)⟩ : cau_seq ℂ abs) = complex.lim (show cau_seq ℂ abs, from ⟨_, complex.exp_series_cau x⟩) * complex.lim (show cau_seq ℂ abs, from ⟨_, complex.exp_series_cau y⟩), begin have hxa := complex.exp_series_abs_cau x, have hx := complex.exp_series_cau x, have hy := complex.exp_series_cau y, have hxy := complex.exp_series_cau (x + y), rw complex.lim_mul_lim, have hj : ∀ j : ℕ, (range j).sum (λ (m : ℕ), (x + y) ^ m / ↑(fact m)) = (range j).sum (λ i, (range (i + 1)).sum (λ k, x ^ k / fact k * (y ^ (i - k) / fact (i - k)))), { assume j, refine finset.sum_congr rfl (λ m hm, _), rw [add_pow, div_eq_mul_inv, sum_mul], refine finset.sum_congr rfl (λ i hi, _), have := choose_mul_fact_mul_fact (le_of_lt_succ $ finset.mem_range.1 hi), rw [← this, nat.cast_mul, nat.cast_mul, mul_inv', mul_inv'], simp only [mul_left_comm (choose m i : ℂ), mul_assoc, mul_left_comm (choose m i : ℂ)⁻¹, mul_comm (choose m i : ℂ)], have : (choose m i : ℂ) ≠ 0 := nat.cast_ne_zero.2 (λ h, by have := choose_pos (le_of_lt_succ (mem_range.1 hi)); simpa [h, lt_irrefl] using this), rw inv_mul_cancel this, simp [div_eq_mul_inv, mul_comm, mul_assoc, mul_left_comm] }, have hf := funext hj, have hxy1 := hxy, rw hf at hxy1, have := series_cauchy_prod hxa hy, refine eq.symm (lim_eq_lim_of_equiv _), assume ε ε0, dsimp, simp only [hj], exact this ε ε0, end
d438653937007d72d66d0245ace1f22a289ca14b
94e33a31faa76775069b071adea97e86e218a8ee
/src/group_theory/free_abelian_group_finsupp.lean
d5606a5b684ee7ddfaa155fa992370da1954da93
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
6,997
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.module.equiv import data.finsupp.basic import group_theory.free_abelian_group import group_theory.is_free_group import linear_algebra.dimension /-! # Isomorphism between `free_abelian_group X` and `X →₀ ℤ` In this file we construct the canonical isomorphism between `free_abelian_group X` and `X →₀ ℤ`. We use this to transport the notion of `support` from `finsupp` to `free_abelian_group`. ## Main declarations - `free_abelian_group.equiv_finsupp`: group isomorphism between `free_abelian_group X` and `X →₀ ℤ` - `free_abelian_group.coeff`: the multiplicity of `x : X` in `a : free_abelian_group X` - `free_abelian_group.support`: the finset of `x : X` that occur in `a : free_abelian_group X` -/ noncomputable theory open_locale big_operators variables {X : Type*} /-- The group homomorphism `free_abelian_group X →+ (X →₀ ℤ)`. -/ def free_abelian_group.to_finsupp : free_abelian_group X →+ (X →₀ ℤ) := free_abelian_group.lift $ λ x, finsupp.single x (1 : ℤ) /-- The group homomorphism `(X →₀ ℤ) →+ free_abelian_group X`. -/ def finsupp.to_free_abelian_group : (X →₀ ℤ) →+ free_abelian_group X := finsupp.lift_add_hom $ λ x, (smul_add_hom ℤ (free_abelian_group X)).flip (free_abelian_group.of x) open finsupp free_abelian_group @[simp] lemma finsupp.to_free_abelian_group_comp_single_add_hom (x : X) : finsupp.to_free_abelian_group.comp (finsupp.single_add_hom x) = (smul_add_hom ℤ (free_abelian_group X)).flip (of x) := begin ext, simp only [add_monoid_hom.coe_comp, finsupp.single_add_hom_apply, function.comp_app, one_smul, to_free_abelian_group, finsupp.lift_add_hom_apply_single] end @[simp] lemma free_abelian_group.to_finsupp_comp_to_free_abelian_group : to_finsupp.comp to_free_abelian_group = add_monoid_hom.id (X →₀ ℤ) := begin ext x y, simp only [add_monoid_hom.id_comp], rw [add_monoid_hom.comp_assoc, finsupp.to_free_abelian_group_comp_single_add_hom], simp only [to_finsupp, add_monoid_hom.coe_comp, finsupp.single_add_hom_apply, function.comp_app, one_smul, lift.of, add_monoid_hom.flip_apply, smul_add_hom_apply, add_monoid_hom.id_apply], end @[simp] lemma finsupp.to_free_abelian_group_comp_to_finsupp : to_free_abelian_group.comp to_finsupp = add_monoid_hom.id (free_abelian_group X) := begin ext, rw [to_free_abelian_group, to_finsupp, add_monoid_hom.comp_apply, lift.of, lift_add_hom_apply_single, add_monoid_hom.flip_apply, smul_add_hom_apply, one_smul, add_monoid_hom.id_apply], end @[simp] lemma finsupp.to_free_abelian_group_to_finsupp {X} (x : free_abelian_group X) : x.to_finsupp.to_free_abelian_group = x := by rw [← add_monoid_hom.comp_apply, finsupp.to_free_abelian_group_comp_to_finsupp, add_monoid_hom.id_apply] namespace free_abelian_group open finsupp variable {X} @[simp] lemma to_finsupp_of (x : X) : to_finsupp (of x) = finsupp.single x 1 := by simp only [to_finsupp, lift.of] @[simp] lemma to_finsupp_to_free_abelian_group (f : X →₀ ℤ) : f.to_free_abelian_group.to_finsupp = f := by rw [← add_monoid_hom.comp_apply, to_finsupp_comp_to_free_abelian_group, add_monoid_hom.id_apply] variable (X) /-- The additive equivalence between `free_abelian_group X` and `(X →₀ ℤ)`. -/ @[simps] def equiv_finsupp : free_abelian_group X ≃+ (X →₀ ℤ) := { to_fun := to_finsupp, inv_fun := to_free_abelian_group, left_inv := to_free_abelian_group_to_finsupp, right_inv := to_finsupp_to_free_abelian_group, map_add' := to_finsupp.map_add } /-- `A` is a basis of the ℤ-module `free_abelian_group A`. -/ noncomputable def basis (α : Type*) : basis α ℤ (free_abelian_group α) := ⟨(free_abelian_group.equiv_finsupp α).to_int_linear_equiv ⟩ /-- Isomorphic free ablian groups (as modules) have equivalent bases. -/ def equiv.of_free_abelian_group_linear_equiv {α β : Type*} (e : free_abelian_group α ≃ₗ[ℤ] free_abelian_group β) : α ≃ β := let t : _root_.basis α ℤ (free_abelian_group β) := (free_abelian_group.basis α).map e in t.index_equiv $ free_abelian_group.basis _ /-- Isomorphic free abelian groups (as additive groups) have equivalent bases. -/ def equiv.of_free_abelian_group_equiv {α β : Type*} (e : free_abelian_group α ≃+ free_abelian_group β) : α ≃ β := equiv.of_free_abelian_group_linear_equiv e.to_int_linear_equiv /-- Isomorphic free groups have equivalent bases. -/ def equiv.of_free_group_equiv {α β : Type*} (e : free_group α ≃* free_group β) : α ≃ β := equiv.of_free_abelian_group_equiv e.abelianization_congr.to_additive open is_free_group /-- Isomorphic free groups have equivalent bases (`is_free_group` variant`). -/ def equiv.of_is_free_group_equiv {G H : Type*} [group G] [group H] [is_free_group G] [is_free_group H] (e : G ≃* H) : generators G ≃ generators H := equiv.of_free_group_equiv $ mul_equiv.trans ((to_free_group G).symm) $ mul_equiv.trans e $ to_free_group H variable {X} /-- `coeff x` is the additive group homomorphism `free_abelian_group X →+ ℤ` that sends `a` to the multiplicity of `x : X` in `a`. -/ def coeff (x : X) : free_abelian_group X →+ ℤ := (finsupp.apply_add_hom x).comp to_finsupp /-- `support a` for `a : free_abelian_group X` is the finite set of `x : X` that occur in the formal sum `a`. -/ def support (a : free_abelian_group X) : finset X := a.to_finsupp.support lemma mem_support_iff (x : X) (a : free_abelian_group X) : x ∈ a.support ↔ coeff x a ≠ 0 := by { rw [support, finsupp.mem_support_iff], exact iff.rfl } lemma not_mem_support_iff (x : X) (a : free_abelian_group X) : x ∉ a.support ↔ coeff x a = 0 := by { rw [support, finsupp.not_mem_support_iff], exact iff.rfl } @[simp] lemma support_zero : support (0 : free_abelian_group X) = ∅ := by simp only [support, finsupp.support_zero, add_monoid_hom.map_zero] @[simp] lemma support_of (x : X) : support (of x) = {x} := by simp only [support, to_finsupp_of, finsupp.support_single_ne_zero _ one_ne_zero] @[simp] lemma support_neg (a : free_abelian_group X) : support (-a) = support a := by simp only [support, add_monoid_hom.map_neg, finsupp.support_neg] @[simp] lemma support_zsmul (k : ℤ) (h : k ≠ 0) (a : free_abelian_group X) : support (k • a) = support a := begin ext x, simp only [mem_support_iff, add_monoid_hom.map_zsmul], simp only [h, zsmul_int_int, false_or, ne.def, mul_eq_zero] end @[simp] lemma support_nsmul (k : ℕ) (h : k ≠ 0) (a : free_abelian_group X) : support (k • a) = support a := by { apply support_zsmul k _ a, exact_mod_cast h } open_locale classical lemma support_add (a b : free_abelian_group X) : (support (a + b)) ⊆ a.support ∪ b.support := begin simp only [support, add_monoid_hom.map_add], apply finsupp.support_add end end free_abelian_group
1a8c6b1b559fd73d719c805e1388fd079cb73e3a
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/tests/lean/run/set2.lean
26609b0f66ef439ae2bd7bcf2c3fe882c5bfcedc
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
367
lean
import standard using bool namespace set definition set (T : Type) := T → bool definition mem {T : Type} (a : T) (s : set T) := s a = tt infix `∈` := mem section variable {T : Type} definition empty : set T := λx, ff notation `∅` := empty theorem mem_empty (x : T) : ¬ (x ∈ ∅) := not_intro (λH : x ∈ ∅, absurd H ff_ne_tt) end end set
3c68270a8a2b12c3dc8b0589bdc896445b8a9032
40ad357bbd0d327dd1e3e7f7beb868bd4e5b0a9d
/src/temporal_logic/refinement/simulation.lean
e95a58c88663fd8a68aa132af97ac7a4c8b38b11
[]
no_license
unitb/temporal-logic
9966424f015976d5997a9ffa30cbd77cc3a9cb1c
accec04d1b09ca841be065511c9e206b725b16e9
refs/heads/master
1,633,868,382,769
1,541,072,223,000
1,541,072,223,000
114,790,987
5
3
null
null
null
null
UTF-8
Lean
false
false
4,273
lean
import temporal_logic.fairness import temporal_logic.pair import temporal_logic.lemmas universe variables u u₀ u₁ u₂ open predicate nat namespace temporal namespace simulation section parameters {α : Type u} {β : Type u₀} {γ : Type u₁ } parameters (p : pred' (γ×α)) (q : pred' (γ×β)) parameters (A : act (γ×α)) (C : act (γ×β)) parameters (J : pred' (γ×α×β)) variables (x : tvar α) (y : tvar β) (z : tvar γ) def SPEC₀ (v : tvar α) (o : tvar γ) : cpred := p ! ⦃ o,v ⦄ ⋀ ◻⟦ o,v | A ⟧ def SPEC₁ (v : tvar β) (o : tvar γ) : cpred := q ! ⦃ o,v ⦄ ⋀ ◻⟦ o,v | C ⟧ -- parameters [inhabited α] parameter SIM₀ : ∀ v o, (o,v) ⊨ q → ∃ w, (o,w) ⊨ p ∧ (o,w,v) ⊨ J parameter SIM : ∀ w v o v' o', (o,w,v) ⊨ J → C (o,v) (o',v') → ∃ w', A (o,w) (o',w') ∧ (o',w',v') ⊨ J parameters (v : tvar β) (o : tvar γ) parameters Γ : cpred parameters H : Γ ⊢ SPEC₁ v o def Wx₀ : tvar (α → Prop) := [| o, λ w, (o,w) ⊨ p |] def Wf : tvar (α → α → Prop) := ⟪ ℕ, λ o o' w w', A (o,w) (o',w') ⟫ o (⊙o) def Wtn (w : tvar α) := Wx₀ w ⋀ ◻(Wf w ⊙w) include SIM₀ variables w : tvar α -- variables Hw : Γ ⊢ Wtn w -- include Hw include H SIM -- omit Hw #check to_fun_var #check to_fun_var' lemma simulation : Γ ⊢ ∃∃ w, SPEC₀ w o := begin [temporal] cases H with H₀ Hnext, -- ⊢ ⇑(⇑(to_fun_var' (λ (w w_1 : tvar α), ⇑(⇑Wf w) w_1)) w) w' = ⇑(⇑Wf w) w' select_witness w : temporal.simulation.Wtn w with Hw hJ using (J!⦃o,w,v⦄), { }, explicit' [SPEC₀,Wx₀] with H₀ { solve_by_elim, } , -- intros, explicit' [Wf] with Hnext { intros, apply SIM ; assumption, }, existsi w, revert Hw, simp only [SPEC₀,SPEC₁,Wtn], apply ctx_p_and_p_imp_p_and', explicit' [Wx₀] { }, mono!, explicit' [Wf] { }, end omit H lemma simulation' : (∃∃ c, SPEC₁ c o) ⟹ (∃∃ a, SPEC₀ a o) := begin [temporal] rw p_exists_p_imp, intros x h, apply simulation p q A C J SIM₀ @SIM _ _ _ h , end end end simulation export simulation (simulation simulation') namespace witness_construction section witness_construction parameters {α : Sort u} parameters {p J : pred' α} parameters {A : act α} parameters H₀ : p ⟹ J parameters FIS₀ : ∃ σ, σ ⊨ p parameters FIS : ∀ σ, σ ⊨ J → ∃ σ', A σ σ' parameters INV : ∀ σ σ', σ ⊨ J → A σ σ' → σ' ⊨ J open classical simulation function include H₀ INV def A' : act $ unit × plift α := A on (plift.down ∘ prod.snd) -- parameters [_inst : inhabited α] include FIS₀ FIS lemma witness_construction : ⊩ ∃∃ v, p ! v ⋀ ◻⟦ v | A ⟧ := begin intro, let o : tvar unit := ↑(), let C : unit × unit → unit × unit → Prop := λ _ _, true, let prj : var (unit × plift α) α := ⟨plift.down⟩ ! pair.snd, let p' : pred' (unit × plift α) := p ! prj, -- cases FIS₀ with w Hw, -- have _inst : inhabited (plift α) := ⟨ plift.up w ⟩, let J' : pred' (unit × plift α × unit) := J ! ⟨plift.down⟩ ! pair.fst ! pair.snd, have := @simulation _ _ _ p' (@True $ unit × unit) (A' H₀ INV) C J' _ _ o o Γ _, -- ; try { auto }, -- have := @simulation _ _ _ _ (@True $ unit × unit) (A' H₀ INV) C J' True _inst _ _ o o Γ _, begin [temporal] revert this, let f : tvar (plift α) → tvar α := λ v, ⟨plift.down⟩ ! v, let SPEC := @SPEC₀ _ _ p' (A' H₀ INV), let SPEC' := λ (v : tvar α), p ! v ⋀ ◻⟦ v | A ⟧, apply p_exists_imp_p_exists' (λ w, SPEC w o) SPEC' f, intro, simp only [SPEC,f,SPEC',SPEC₀,p',prj,proj_assoc,pair.snd_mk,A'], monotonicity, rw [action_on,coe_over_comp,proj_assoc,pair.snd_mk'], refl, end, { intros, apply exists_imp_exists' plift.up _ FIS₀, introv Hw, split, simp [p',Hw], simp [J'], apply ew_str H₀ _ Hw, }, { introv hJ hC, simp [J'] at hJ, -- existsi w, have := FIS _ hJ, revert this, apply exists_imp_exists' plift.up, simp [A',function.comp,on_fun], introv hA, split, { apply hA }, { apply INV _ _ hJ hA } }, { simp only [SPEC₁,C] with tl_simp, } end end witness_construction end witness_construction end temporal
91a150bdb14d20801f9eef373aba907144ed9052
1fbca480c1574e809ae95a3eda58188ff42a5e41
/src/util/data/option.lean
1de3b5452a27903af78022570dd70a0ae13b7f78
[]
no_license
unitb/lean-lib
560eea0acf02b1fd4bcaac9986d3d7f1a4290e7e
439b80e606b4ebe4909a08b1d77f4f5c0ee3dee9
refs/heads/master
1,610,706,025,400
1,570,144,245,000
1,570,144,245,000
99,579,229
5
0
null
null
null
null
UTF-8
Lean
false
false
1,410
lean
universe variables u v variables {α : Type u} variables {β : Type u} @[simp] lemma none_or_else (x : option α) : (none <|> x) = x := by { cases x ; refl } @[simp] lemma or_else_none (x : option α) : (x <|> none) = x := by { cases x ; refl } @[simp] lemma some_or_else (x : α) (y : option α) : (some x <|> y) = some x := by { refl } @[simp] lemma or_else_assoc (x y z : option α) : ((x <|> y) <|> z) = (x <|> (y <|> z)) := by { cases x ; simp } @[simp] lemma or_else_eq_none_iff (x y : option α) : (x <|> y) = none ↔ x = none ∧ y = none := begin split ; intros h, cases x with x, { simp at h, simp [h] }, { simp at h, contradiction }, { simp [h.left,h.right] } end @[simp] lemma fmap_none (f : α → β) : f <$> none = none := rfl @[simp] lemma fmap_some (f : α → β) (x : α) : f <$> some x = some (f x) := rfl @[simp] lemma fmap_eq_none_iff (f : α → β) (x : option α) : f <$> x = none ↔ x = none := by cases x ; simp @[simp] lemma coe_eq_some (x : α) : ↑x = some x := rfl lemma is_some_of_eq_some {α : Type u} (x : α) {y : option α} (h : some x = y) : y.is_some := begin rw [← h,option.is_some], exact rfl end namespace option def to_suml {α β} (x : β) : option α → α ⊕ β | none := sum.inr x | (some y) := sum.inl y def to_sumr {α β} (x : α) : option β → α ⊕ β | none := sum.inl x | (some y) := sum.inr y end option
357249e0bc60345c64dbb6908a512b05ad79e03b
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/field_theory/finite/basic.lean
023113b782a9a0e3b666eaebf48a3406b2196496
[ "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
15,780
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Joey van Langen, Casper Putz -/ import tactic.apply_fun import data.equiv.ring import data.zmod.algebra import linear_algebra.finite_dimensional import ring_theory.integral_domain import field_theory.separable import field_theory.splitting_field /-! # Finite fields This file contains basic results about finite fields. Throughout most of this file, `K` denotes a finite field and `q` is notation for the cardinality of `K`. See `ring_theory.integral_domain` for the fact that the unit group of a finite field is a cyclic group, as well as the fact that every finite integral domain is a field (`field_of_is_domain`). ## Main results 1. `fintype.card_units`: The unit group of a finite field is has cardinality `q - 1`. 2. `sum_pow_units`: The sum of `x^i`, where `x` ranges over the units of `K`, is - `q-1` if `q-1 ∣ i` - `0` otherwise 3. `finite_field.card`: The cardinality `q` is a power of the characteristic of `K`. See `card'` for a variant. ## Notation Throughout most of this file, `K` denotes a finite field and `q` is notation for the cardinality of `K`. ## Implementation notes While `fintype (units K)` can be inferred from `fintype K` in the presence of `decidable_eq K`, in this file we take the `fintype (units K)` argument directly to reduce the chance of typeclass diamonds, as `fintype` carries data. -/ variables {K : Type*} {R : Type*} local notation `q` := fintype.card K open_locale big_operators namespace finite_field open finset function section polynomial variables [comm_ring R] [is_domain R] open polynomial /-- The cardinality of a field is at most `n` times the cardinality of the image of a degree `n` polynomial -/ lemma card_image_polynomial_eval [decidable_eq R] [fintype R] {p : polynomial R} (hp : 0 < p.degree) : fintype.card R ≤ nat_degree p * (univ.image (λ x, eval x p)).card := finset.card_le_mul_card_image _ _ (λ a _, calc _ = (p - C a).roots.to_finset.card : congr_arg card (by simp [finset.ext_iff, mem_roots_sub_C hp]) ... ≤ (p - C a).roots.card : multiset.to_finset_card_le _ ... ≤ _ : card_roots_sub_C' hp) /-- If `f` and `g` are quadratic polynomials, then the `f.eval a + g.eval b = 0` has a solution. -/ lemma exists_root_sum_quadratic [fintype R] {f g : polynomial R} (hf2 : degree f = 2) (hg2 : degree g = 2) (hR : fintype.card R % 2 = 1) : ∃ a b, f.eval a + g.eval b = 0 := by letI := classical.dec_eq R; exact suffices ¬ disjoint (univ.image (λ x : R, eval x f)) (univ.image (λ x : R, eval x (-g))), begin simp only [disjoint_left, mem_image] at this, push_neg at this, rcases this with ⟨x, ⟨a, _, ha⟩, ⟨b, _, hb⟩⟩, exact ⟨a, b, by rw [ha, ← hb, eval_neg, neg_add_self]⟩ end, assume hd : disjoint _ _, lt_irrefl (2 * ((univ.image (λ x : R, eval x f)) ∪ (univ.image (λ x : R, eval x (-g)))).card) $ calc 2 * ((univ.image (λ x : R, eval x f)) ∪ (univ.image (λ x : R, eval x (-g)))).card ≤ 2 * fintype.card R : nat.mul_le_mul_left _ (finset.card_le_univ _) ... = fintype.card R + fintype.card R : two_mul _ ... < nat_degree f * (univ.image (λ x : R, eval x f)).card + nat_degree (-g) * (univ.image (λ x : R, eval x (-g))).card : add_lt_add_of_lt_of_le (lt_of_le_of_ne (card_image_polynomial_eval (by rw hf2; exact dec_trivial)) (mt (congr_arg (%2)) (by simp [nat_degree_eq_of_degree_eq_some hf2, hR]))) (card_image_polynomial_eval (by rw [degree_neg, hg2]; exact dec_trivial)) ... = 2 * (univ.image (λ x : R, eval x f) ∪ univ.image (λ x : R, eval x (-g))).card : by rw [card_disjoint_union hd]; simp [nat_degree_eq_of_degree_eq_some hf2, nat_degree_eq_of_degree_eq_some hg2, bit0, mul_add] end polynomial lemma prod_univ_units_id_eq_neg_one [field K] [fintype (units K)] : (∏ x : units K, x) = (-1 : units K) := begin classical, have : (∏ x in (@univ (units K) _).erase (-1), x) = 1, from prod_involution (λ x _, x⁻¹) (by simp) (λ a, by simp [units.inv_eq_self_iff] {contextual := tt}) (λ a, by simp [@inv_eq_iff_inv_eq _ _ a, eq_comm] {contextual := tt}) (by simp), rw [← insert_erase (mem_univ (-1 : units K)), prod_insert (not_mem_erase _ _), this, mul_one] end section variables [group_with_zero K] [fintype K] lemma pow_card_sub_one_eq_one (a : K) (ha : a ≠ 0) : a ^ (q - 1) = 1 := calc a ^ (fintype.card K - 1) = (units.mk0 a ha ^ (fintype.card K - 1) : units K) : by rw [units.coe_pow, units.coe_mk0] ... = 1 : by { classical, rw [← fintype.card_units, pow_card_eq_one], refl } lemma pow_card (a : K) : a ^ q = a := begin have hp : 0 < fintype.card K := lt_trans zero_lt_one fintype.one_lt_card, by_cases h : a = 0, { rw h, apply zero_pow hp }, rw [← nat.succ_pred_eq_of_pos hp, pow_succ, nat.pred_eq_sub_one, pow_card_sub_one_eq_one a h, mul_one], end lemma pow_card_pow (n : ℕ) (a : K) : a ^ q ^ n = a := begin induction n with n ih, { simp, }, { simp [pow_succ, pow_mul, ih, pow_card], }, end end variables (K) [field K] [fintype K] theorem card (p : ℕ) [char_p K p] : ∃ (n : ℕ+), nat.prime p ∧ q = p^(n : ℕ) := begin haveI hp : fact p.prime := ⟨char_p.char_is_prime K p⟩, letI : module (zmod p) K := { .. (zmod.cast_hom dvd_rfl K).to_module }, obtain ⟨n, h⟩ := vector_space.card_fintype (zmod p) K, rw zmod.card at h, refine ⟨⟨n, _⟩, hp.1, h⟩, apply or.resolve_left (nat.eq_zero_or_pos n), rintro rfl, rw pow_zero at h, have : (0 : K) = 1, { apply fintype.card_le_one_iff.mp (le_of_eq h) }, exact absurd this zero_ne_one, end -- this statement doesn't use `q` because we want `K` to be an explicit parameter theorem card' : ∃ (p : ℕ) (n : ℕ+), nat.prime p ∧ fintype.card K = p^(n : ℕ) := let ⟨p, hc⟩ := char_p.exists K in ⟨p, @finite_field.card K _ _ p hc⟩ @[simp] lemma cast_card_eq_zero : (q : K) = 0 := begin rcases char_p.exists K with ⟨p, _char_p⟩, resetI, rcases card K p with ⟨n, hp, hn⟩, simp only [char_p.cast_eq_zero_iff K p, hn], conv { congr, rw [← pow_one p] }, exact pow_dvd_pow _ n.2, end lemma forall_pow_eq_one_iff (i : ℕ) : (∀ x : units K, x ^ i = 1) ↔ q - 1 ∣ i := begin classical, obtain ⟨x, hx⟩ := is_cyclic.exists_generator (units K), rw [←fintype.card_units, ←order_of_eq_card_of_forall_mem_zpowers hx, order_of_dvd_iff_pow_eq_one], split, { intro h, apply h }, { intros h y, simp_rw ← mem_powers_iff_mem_zpowers at hx, rcases hx y with ⟨j, rfl⟩, rw [← pow_mul, mul_comm, pow_mul, h, one_pow], } end /-- The sum of `x ^ i` as `x` ranges over the units of a finite field of cardinality `q` is equal to `0` unless `(q - 1) ∣ i`, in which case the sum is `q - 1`. -/ lemma sum_pow_units [fintype (units K)] (i : ℕ) : ∑ x : units K, (x ^ i : K) = if (q - 1) ∣ i then -1 else 0 := begin let φ : units K →* K := { to_fun := λ x, x ^ i, map_one' := by rw [units.coe_one, one_pow], map_mul' := by { intros, rw [units.coe_mul, mul_pow] } }, haveI : decidable (φ = 1), { classical, apply_instance }, calc ∑ x : units K, φ x = if φ = 1 then fintype.card (units K) else 0 : sum_hom_units φ ... = if (q - 1) ∣ i then -1 else 0 : _, suffices : (q - 1) ∣ i ↔ φ = 1, { simp only [this], split_ifs with h h, swap, refl, rw [fintype.card_units, nat.cast_sub, cast_card_eq_zero, nat.cast_one, zero_sub], show 1 ≤ q, from fintype.card_pos_iff.mpr ⟨0⟩ }, rw [← forall_pow_eq_one_iff, monoid_hom.ext_iff], apply forall_congr, intro x, rw [units.ext_iff, units.coe_pow, units.coe_one, monoid_hom.one_apply], refl, end /-- The sum of `x ^ i` as `x` ranges over a finite field of cardinality `q` is equal to `0` if `i < q - 1`. -/ lemma sum_pow_lt_card_sub_one (i : ℕ) (h : i < q - 1) : ∑ x : K, x ^ i = 0 := begin by_cases hi : i = 0, { simp only [hi, nsmul_one, sum_const, pow_zero, card_univ, cast_card_eq_zero], }, classical, have hiq : ¬ (q - 1) ∣ i, { contrapose! h, exact nat.le_of_dvd (nat.pos_of_ne_zero hi) h }, let φ : units K ↪ K := ⟨coe, units.ext⟩, have : univ.map φ = univ \ {0}, { ext x, simp only [true_and, embedding.coe_fn_mk, mem_sdiff, units.exists_iff_ne_zero, mem_univ, mem_map, exists_prop_of_true, mem_singleton] }, calc ∑ x : K, x ^ i = ∑ x in univ \ {(0 : K)}, x ^ i : by rw [← sum_sdiff ({0} : finset K).subset_univ, sum_singleton, zero_pow (nat.pos_of_ne_zero hi), add_zero] ... = ∑ x : units K, x ^ i : by { rw [← this, univ.sum_map φ], refl } ... = 0 : by { rw [sum_pow_units K i, if_neg], exact hiq, } end section is_splitting_field open polynomial section variables (K' : Type*) [field K'] {p n : ℕ} lemma X_pow_card_sub_X_nat_degree_eq (hp : 1 < p) : (X ^ p - X : polynomial K').nat_degree = p := begin have h1 : (X : polynomial K').degree < (X ^ p : polynomial K').degree, { rw [degree_X_pow, degree_X], exact_mod_cast hp }, rw [nat_degree_eq_of_degree_eq (degree_sub_eq_left_of_degree_lt h1), nat_degree_X_pow], end lemma X_pow_card_pow_sub_X_nat_degree_eq (hn : n ≠ 0) (hp : 1 < p) : (X ^ p ^ n - X : polynomial K').nat_degree = p ^ n := X_pow_card_sub_X_nat_degree_eq K' $ nat.one_lt_pow _ _ (nat.pos_of_ne_zero hn) hp lemma X_pow_card_sub_X_ne_zero (hp : 1 < p) : (X ^ p - X : polynomial K') ≠ 0 := ne_zero_of_nat_degree_gt $ calc 1 < _ : hp ... = _ : (X_pow_card_sub_X_nat_degree_eq K' hp).symm lemma X_pow_card_pow_sub_X_ne_zero (hn : n ≠ 0) (hp : 1 < p) : (X ^ p ^ n - X : polynomial K') ≠ 0 := X_pow_card_sub_X_ne_zero K' $ nat.one_lt_pow _ _ (nat.pos_of_ne_zero hn) hp end variables (p : ℕ) [fact p.prime] [char_p K p] lemma roots_X_pow_card_sub_X : roots (X^q - X : polynomial K) = finset.univ.val := begin classical, have aux : (X^q - X : polynomial K) ≠ 0 := X_pow_card_sub_X_ne_zero K fintype.one_lt_card, have : (roots (X^q - X : polynomial K)).to_finset = finset.univ, { rw eq_univ_iff_forall, intro x, rw [multiset.mem_to_finset, mem_roots aux, is_root.def, eval_sub, eval_pow, eval_X, sub_eq_zero, pow_card] }, rw [←this, multiset.to_finset_val, eq_comm, multiset.erase_dup_eq_self], apply nodup_roots, rw separable_def, convert is_coprime_one_right.neg_right, rw [derivative_sub, derivative_X, derivative_X_pow, ←C_eq_nat_cast, C_eq_zero.mpr (char_p.cast_card_eq_zero K), zero_mul, zero_sub], end instance : is_splitting_field (zmod p) K (X^q - X) := { splits := begin have h : (X^q - X : polynomial K).nat_degree = q := X_pow_card_sub_X_nat_degree_eq K fintype.one_lt_card, rw [←splits_id_iff_splits, splits_iff_card_roots, map_sub, map_pow, map_X, h, roots_X_pow_card_sub_X K, ←finset.card_def, finset.card_univ], end, adjoin_roots := begin classical, transitivity algebra.adjoin (zmod p) ((roots (X^q - X : polynomial K)).to_finset : set K), { simp only [map_pow, map_X, map_sub], convert rfl }, { rw [roots_X_pow_card_sub_X, val_to_finset, coe_univ, algebra.adjoin_univ], } end } end is_splitting_field variables {K} theorem frobenius_pow {p : ℕ} [fact p.prime] [char_p K p] {n : ℕ} (hcard : q = p^n) : (frobenius K p) ^ n = 1 := begin ext, conv_rhs { rw [ring_hom.one_def, ring_hom.id_apply, ← pow_card x, hcard], }, clear hcard, induction n, {simp}, rw [pow_succ, pow_succ', pow_mul, ring_hom.mul_def, ring_hom.comp_apply, frobenius_def, n_ih] end open polynomial lemma expand_card (f : polynomial K) : expand K q f = f ^ q := begin cases char_p.exists K with p hp, letI := hp, rcases finite_field.card K p with ⟨⟨n, npos⟩, ⟨hp, hn⟩⟩, haveI : fact p.prime := ⟨hp⟩, dsimp at hn, rw hn at *, rw ← map_expand_pow_char, rw [frobenius_pow hn, ring_hom.one_def, map_id], end end finite_field namespace zmod open finite_field polynomial lemma sq_add_sq (p : ℕ) [hp : fact p.prime] (x : zmod p) : ∃ a b : zmod p, a^2 + b^2 = x := begin cases hp.1.eq_two_or_odd with hp2 hp_odd, { substI p, change fin 2 at x, fin_cases x, { use 0, simp }, { use [0, 1], simp } }, let f : polynomial (zmod p) := X^2, let g : polynomial (zmod p) := X^2 - C x, obtain ⟨a, b, hab⟩ : ∃ a b, f.eval a + g.eval b = 0 := @exists_root_sum_quadratic _ _ _ _ f g (degree_X_pow 2) (degree_X_pow_sub_C dec_trivial _) (by rw [zmod.card, hp_odd]), refine ⟨a, b, _⟩, rw ← sub_eq_zero, simpa only [eval_C, eval_X, eval_pow, eval_sub, ← add_sub_assoc] using hab, end end zmod namespace char_p lemma sq_add_sq (R : Type*) [comm_ring R] [is_domain R] (p : ℕ) [fact (0 < p)] [char_p R p] (x : ℤ) : ∃ a b : ℕ, (a^2 + b^2 : R) = x := begin haveI := char_is_prime_of_pos R p, obtain ⟨a, b, hab⟩ := zmod.sq_add_sq p x, refine ⟨a.val, b.val, _⟩, simpa using congr_arg (zmod.cast_hom dvd_rfl R) hab end end char_p open_locale nat open zmod /-- The **Fermat-Euler totient theorem**. `nat.modeq.pow_totient` is an alternative statement of the same theorem. -/ @[simp] lemma zmod.pow_totient {n : ℕ} [fact (0 < n)] (x : units (zmod n)) : x ^ φ n = 1 := by rw [← card_units_eq_totient, pow_card_eq_one] /-- The **Fermat-Euler totient theorem**. `zmod.pow_totient` is an alternative statement of the same theorem. -/ lemma nat.modeq.pow_totient {x n : ℕ} (h : nat.coprime x n) : x ^ φ n ≡ 1 [MOD n] := begin cases n, {simp}, rw ← zmod.eq_iff_modeq_nat, let x' : units (zmod (n+1)) := zmod.unit_of_coprime _ h, have := zmod.pow_totient x', apply_fun (coe : units (zmod (n+1)) → zmod (n+1)) at this, simpa only [-zmod.pow_totient, nat.succ_eq_add_one, nat.cast_pow, units.coe_one, nat.cast_one, coe_unit_of_coprime, units.coe_pow], end section variables {V : Type*} [fintype K] [division_ring K] [add_comm_group V] [module K V] -- should this go in a namespace? -- finite_dimensional would be natural, -- but we don't assume it... lemma card_eq_pow_finrank [fintype V] : fintype.card V = q ^ (finite_dimensional.finrank K V) := begin let b := is_noetherian.finset_basis K V, rw [module.card_fintype b, ← finite_dimensional.finrank_eq_card_basis b], end end open finite_field namespace zmod /-- A variation on Fermat's little theorem. See `zmod.pow_card_sub_one_eq_one` -/ @[simp] lemma pow_card {p : ℕ} [fact p.prime] (x : zmod p) : x ^ p = x := by { have h := finite_field.pow_card x, rwa zmod.card p at h } @[simp] lemma pow_card_pow {n p : ℕ} [fact p.prime] (x : zmod p) : x ^ p ^ n = x := begin induction n with n ih, { simp, }, { simp [pow_succ, pow_mul, ih, pow_card], }, end @[simp] lemma frobenius_zmod (p : ℕ) [fact p.prime] : frobenius (zmod p) p = ring_hom.id _ := by { ext a, rw [frobenius_def, zmod.pow_card, ring_hom.id_apply] } @[simp] lemma card_units (p : ℕ) [fact p.prime] : fintype.card (units (zmod p)) = p - 1 := by rw [fintype.card_units, card] /-- **Fermat's Little Theorem**: for every unit `a` of `zmod p`, we have `a ^ (p - 1) = 1`. -/ theorem units_pow_card_sub_one_eq_one (p : ℕ) [fact p.prime] (a : units (zmod p)) : a ^ (p - 1) = 1 := by rw [← card_units p, pow_card_eq_one] /-- **Fermat's Little Theorem**: for all nonzero `a : zmod p`, we have `a ^ (p - 1) = 1`. -/ theorem pow_card_sub_one_eq_one {p : ℕ} [fact p.prime] {a : zmod p} (ha : a ≠ 0) : a ^ (p - 1) = 1 := by { have h := pow_card_sub_one_eq_one a ha, rwa zmod.card p at h } open polynomial lemma expand_card {p : ℕ} [fact p.prime] (f : polynomial (zmod p)) : expand (zmod p) p f = f ^ p := by { have h := finite_field.expand_card f, rwa zmod.card p at h } end zmod
e6287626f684a20aa8397c49e68ecf5e4e29ddcb
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/include_bug.lean
c6cdaaffd58f91c0b087f6fa4a4e4a15be2c3a55
[ "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
77
lean
variables x y : nat variable xy : x = y include xy lemma xyeq : x = y := xy
2f2f35062ac12aff09bafca905f3e751f8664a14
75bd9c50a345718d735a7533c007cf45f9da9a83
/src/data/mv_polynomial/equiv.lean
a5c08d4a22242d75170fe2b92cf772e699832f9f
[ "Apache-2.0" ]
permissive
jtbarker/mathlib
a1a3b1ddc16179826260578410746756ef18032c
392d3e376b44265ef2dedbd92231d3177acc1fd0
refs/heads/master
1,671,246,411,096
1,600,801,712,000
1,600,801,712,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,804
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Johan Commelin, Mario Carneiro -/ import data.mv_polynomial.rename import data.equiv.fin /-! # Equivalences between polynomial rings This file establishes a number of equivalences between polynomial rings, based on equivalences between the underlying types. ## Notation As in other polynomial files we typically use the notation: + `σ : Type*` (indexing the variables) + `R : Type*` `[comm_semiring R]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `mv_polynomial σ R` which mathematicians might call `X^s` + `a : R` + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : mv_polynomial σ R` ## Tags equivalence, isomorphism, morphism, ring hom, hom -/ noncomputable theory open_locale classical big_operators open set function finsupp add_monoid_algebra open_locale big_operators universes u v w x variables {R : Type u} {S₁ : Type v} {S₂ : Type w} {S₃ : Type x} namespace mv_polynomial variables {σ : Type*} {a a' a₁ a₂ : R} {e : ℕ} {n m : σ} {s : σ →₀ ℕ} section equiv variables (R) [comm_semiring R] /-- The ring isomorphism between multivariable polynomials in no variables and the ground ring. -/ def pempty_ring_equiv : mv_polynomial pempty R ≃+* R := { to_fun := mv_polynomial.eval₂ (ring_hom.id _) $ pempty.elim, inv_fun := C, left_inv := is_id (C.comp (eval₂_hom (ring_hom.id _) pempty.elim)) (assume a : R, by { dsimp, rw [eval₂_C], refl }) (assume a, a.elim), right_inv := λ r, eval₂_C _ _ _, map_mul' := λ _ _, eval₂_mul _ _, map_add' := λ _ _, eval₂_add _ _ } /-- The ring isomorphism between multivariable polynomials in a single variable and polynomials over the ground ring. -/ def punit_ring_equiv : mv_polynomial punit R ≃+* polynomial R := { to_fun := eval₂ polynomial.C (λu:punit, polynomial.X), inv_fun := polynomial.eval₂ mv_polynomial.C (X punit.star), left_inv := begin let f : polynomial R →+* mv_polynomial punit R := ring_hom.of (polynomial.eval₂ mv_polynomial.C (X punit.star)), let g : mv_polynomial punit R →+* polynomial R := ring_hom.of (eval₂ polynomial.C (λu:punit, polynomial.X)), show ∀ p, f.comp g p = p, apply is_id, { assume a, dsimp, rw [eval₂_C, polynomial.eval₂_C] }, { rintros ⟨⟩, dsimp, rw [eval₂_X, polynomial.eval₂_X] } end, right_inv := assume p, polynomial.induction_on p (assume a, by rw [polynomial.eval₂_C, mv_polynomial.eval₂_C]) (assume p q hp hq, by rw [polynomial.eval₂_add, mv_polynomial.eval₂_add, hp, hq]) (assume p n hp, by rw [polynomial.eval₂_mul, polynomial.eval₂_pow, polynomial.eval₂_X, polynomial.eval₂_C, eval₂_mul, eval₂_C, eval₂_pow, eval₂_X]), map_mul' := λ _ _, eval₂_mul _ _, map_add' := λ _ _, eval₂_add _ _ } /-- The ring isomorphism between multivariable polynomials induced by an equivalence of the variables. -/ def ring_equiv_of_equiv (e : S₁ ≃ S₂) : mv_polynomial S₁ R ≃+* mv_polynomial S₂ R := { to_fun := rename e, inv_fun := rename e.symm, left_inv := λ p, by simp only [rename_rename, (∘), e.symm_apply_apply]; exact rename_id p, right_inv := λ p, by simp only [rename_rename, (∘), e.apply_symm_apply]; exact rename_id p, map_mul' := (rename e).map_mul, map_add' := (rename e).map_add } /-- The ring isomorphism between multivariable polynomials induced by a ring isomorphism of the ground ring. -/ def ring_equiv_congr [comm_semiring S₂] (e : R ≃+* S₂) : mv_polynomial S₁ R ≃+* mv_polynomial S₁ S₂ := { to_fun := map (e : R →+* S₂), inv_fun := map (e.symm : S₂ →+* R), left_inv := assume p, have (e.symm : S₂ →+* R).comp (e : R →+* S₂) = ring_hom.id _, { ext a, exact e.symm_apply_apply a }, by simp only [map_map, this, map_id], right_inv := assume p, have (e : R →+* S₂).comp (e.symm : S₂ →+* R) = ring_hom.id _, { ext a, exact e.apply_symm_apply a }, by simp only [map_map, this, map_id], map_mul' := ring_hom.map_mul _, map_add' := ring_hom.map_add _ } section variables (S₁ S₂ S₃) /-- The function from multivariable polynomials in a sum of two types, to multivariable polynomials in one of the types, with coefficents in multivariable polynomials in the other type. See `sum_ring_equiv` for the ring isomorphism. -/ def sum_to_iter : mv_polynomial (S₁ ⊕ S₂) R →+* mv_polynomial S₁ (mv_polynomial S₂ R) := eval₂_hom (C.comp C) (λbc, sum.rec_on bc X (C ∘ X)) instance is_semiring_hom_sum_to_iter : is_semiring_hom (sum_to_iter R S₁ S₂) := eval₂.is_semiring_hom _ _ lemma sum_to_iter_C (a : R) : sum_to_iter R S₁ S₂ (C a) = C (C a) := eval₂_C _ _ a lemma sum_to_iter_Xl (b : S₁) : sum_to_iter R S₁ S₂ (X (sum.inl b)) = X b := eval₂_X _ _ (sum.inl b) lemma sum_to_iter_Xr (c : S₂) : sum_to_iter R S₁ S₂ (X (sum.inr c)) = C (X c) := eval₂_X _ _ (sum.inr c) /-- The function from multivariable polynomials in one type, with coefficents in multivariable polynomials in another type, to multivariable polynomials in the sum of the two types. See `sum_ring_equiv` for the ring isomorphism. -/ def iter_to_sum : mv_polynomial S₁ (mv_polynomial S₂ R) →+* mv_polynomial (S₁ ⊕ S₂) R := eval₂_hom (ring_hom.of (eval₂ C (X ∘ sum.inr))) (X ∘ sum.inl) lemma iter_to_sum_C_C (a : R) : iter_to_sum R S₁ S₂ (C (C a)) = C a := eq.trans (eval₂_C _ _ (C a)) (eval₂_C _ _ _) lemma iter_to_sum_X (b : S₁) : iter_to_sum R S₁ S₂ (X b) = X (sum.inl b) := eval₂_X _ _ _ lemma iter_to_sum_C_X (c : S₂) : iter_to_sum R S₁ S₂ (C (X c)) = X (sum.inr c) := eq.trans (eval₂_C _ _ (X c)) (eval₂_X _ _ _) /-- A helper function for `sum_ring_equiv`. -/ def mv_polynomial_equiv_mv_polynomial [comm_semiring S₃] (f : mv_polynomial S₁ R →+* mv_polynomial S₂ S₃) (g : mv_polynomial S₂ S₃ →+* mv_polynomial S₁ R) (hfgC : ∀a, f (g (C a)) = C a) (hfgX : ∀n, f (g (X n)) = X n) (hgfC : ∀a, g (f (C a)) = C a) (hgfX : ∀n, g (f (X n)) = X n) : mv_polynomial S₁ R ≃+* mv_polynomial S₂ S₃ := { to_fun := f, inv_fun := g, left_inv := is_id (ring_hom.comp _ _) hgfC hgfX, right_inv := is_id (ring_hom.comp _ _) hfgC hfgX, map_mul' := f.map_mul, map_add' := f.map_add } /-- The ring isomorphism between multivariable polynomials in a sum of two types, and multivariable polynomials in one of the types, with coefficents in multivariable polynomials in the other type. -/ def sum_ring_equiv : mv_polynomial (S₁ ⊕ S₂) R ≃+* mv_polynomial S₁ (mv_polynomial S₂ R) := begin apply @mv_polynomial_equiv_mv_polynomial R (S₁ ⊕ S₂) _ _ _ _ (sum_to_iter R S₁ S₂) (iter_to_sum R S₁ S₂), { assume p, convert hom_eq_hom ((sum_to_iter R S₁ S₂).comp ((iter_to_sum R S₁ S₂).comp C)) C _ _ p, { assume a, dsimp, rw [iter_to_sum_C_C R S₁ S₂, sum_to_iter_C R S₁ S₂] }, { assume c, dsimp, rw [iter_to_sum_C_X R S₁ S₂, sum_to_iter_Xr R S₁ S₂] } }, { assume b, rw [iter_to_sum_X R S₁ S₂, sum_to_iter_Xl R S₁ S₂] }, { assume a, rw [sum_to_iter_C R S₁ S₂, iter_to_sum_C_C R S₁ S₂] }, { assume n, cases n with b c, { rw [sum_to_iter_Xl, iter_to_sum_X] }, { rw [sum_to_iter_Xr, iter_to_sum_C_X] } }, end /-- The ring isomorphism between multivariable polynomials in `option S₁` and polynomials with coefficients in `mv_polynomial S₁ R`. -/ def option_equiv_left : mv_polynomial (option S₁) R ≃+* polynomial (mv_polynomial S₁ R) := (ring_equiv_of_equiv R $ (equiv.option_equiv_sum_punit S₁).trans (equiv.sum_comm _ _)).trans $ (sum_ring_equiv R _ _).trans $ punit_ring_equiv _ /-- The ring isomorphism between multivariable polynomials in `option S₁` and multivariable polynomials with coefficients in polynomials. -/ def option_equiv_right : mv_polynomial (option S₁) R ≃+* mv_polynomial S₁ (polynomial R) := (ring_equiv_of_equiv R $ equiv.option_equiv_sum_punit.{0} S₁).trans $ (sum_ring_equiv R S₁ unit).trans $ ring_equiv_congr (mv_polynomial unit R) (punit_ring_equiv R) /-- The ring isomorphism between multivariable polynomials in `fin (n + 1)` and polynomials over multivariable polynomials in `fin n`. -/ def fin_succ_equiv (n : ℕ) : mv_polynomial (fin (n + 1)) R ≃+* polynomial (mv_polynomial (fin n) R) := (ring_equiv_of_equiv R (fin_succ_equiv n)).trans (option_equiv_left R (fin n)) end end equiv end mv_polynomial
00c3b269e893205b7893536849648f3bb7d10858
a2ee6a66690e8da666951cac0c243d42db11f9f3
/src/linear_algebra/affine_space/finite_dimensional.lean
a66d6998f128dbb073d2ced0b343d9ecc74cff74
[ "Apache-2.0" ]
permissive
shyamalschandra/mathlib
6d414d7c334bf383e764336843f065bd14c44273
ca679acad147870b2c5087d90fe3550f107dea49
refs/heads/master
1,671,730,354,335
1,601,883,576,000
1,601,883,576,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,033
lean
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Joseph Myers. -/ import linear_algebra.affine_space.independent import linear_algebra.finite_dimensional noncomputable theory open_locale big_operators open_locale classical /-! # Finite-dimensional subspaces of affine spaces. This file provides a few results relating to finite-dimensional subspaces of affine spaces. -/ section affine_space' variables (k : Type*) {V : Type*} {P : Type*} [field k] [add_comm_group V] [module k V] [affine_space V P] variables {ι : Type*} include V open affine_subspace finite_dimensional /-- The `vector_span` of a finite set is finite-dimensional. -/ lemma finite_dimensional_vector_span_of_finite {s : set P} (h : set.finite s) : finite_dimensional k (vector_span k s) := span_of_finite k $ vsub_set_finite_of_finite h /-- The `vector_span` of a family indexed by a `fintype` is finite-dimensional. -/ instance finite_dimensional_vector_span_of_fintype [fintype ι] (p : ι → P) : finite_dimensional k (vector_span k (set.range p)) := finite_dimensional_vector_span_of_finite k (set.finite_range _) /-- The `vector_span` of a subset of a family indexed by a `fintype` is finite-dimensional. -/ instance finite_dimensional_vector_span_image_of_fintype [fintype ι] (p : ι → P) (s : set ι) : finite_dimensional k (vector_span k (p '' s)) := finite_dimensional_vector_span_of_finite k ((set.finite.of_fintype _).image _) /-- The direction of the affine span of a finite set is finite-dimensional. -/ lemma finite_dimensional_direction_affine_span_of_finite {s : set P} (h : set.finite s) : finite_dimensional k (affine_span k s).direction := (direction_affine_span k s).symm ▸ finite_dimensional_vector_span_of_finite k h /-- The direction of the affine span of a family indexed by a `fintype` is finite-dimensional. -/ instance finite_dimensional_direction_affine_span_of_fintype [fintype ι] (p : ι → P) : finite_dimensional k (affine_span k (set.range p)).direction := finite_dimensional_direction_affine_span_of_finite k (set.finite_range _) /-- The direction of the affine span of a subset of a family indexed by a `fintype` is finite-dimensional. -/ instance finite_dimensional_direction_affine_span_image_of_fintype [fintype ι] (p : ι → P) (s : set ι) : finite_dimensional k (affine_span k (p '' s)).direction := finite_dimensional_direction_affine_span_of_finite k ((set.finite.of_fintype _).image _) variables {k} /-- The `vector_span` of a finite subset of an affinely independent family has dimension one less than its cardinality. -/ lemma findim_vector_span_image_finset_of_affine_independent {p : ι → P} (hi : affine_independent k p) {s : finset ι} {n : ℕ} (hc : finset.card s = n + 1) : findim k (vector_span k (p '' ↑s)) = n := begin have hi' := affine_independent_of_subset_affine_independent (affine_independent_set_of_affine_independent hi) (set.image_subset_range p ↑s), have hc' : fintype.card (p '' ↑s) = n + 1, { rwa [set.card_image_of_injective ↑s (injective_of_affine_independent hi), fintype.card_coe] }, have hn : (p '' ↑s).nonempty, { simp [hc, ←finset.card_pos] }, rcases hn with ⟨p₁, hp₁⟩, rw affine_independent_set_iff_linear_independent_vsub k hp₁ at hi', have hfr : (p '' ↑s \ {p₁}).finite := ((set.finite_mem_finset _).image _).subset (set.diff_subset _ _), haveI := hfr.fintype, have hf : set.finite ((λ (p : P), p -ᵥ p₁) '' (p '' ↑s \ {p₁})) := hfr.image _, haveI := hf.fintype, have hc : hf.to_finset.card = n, { rw [hf.card_to_finset, set.card_image_of_injective (p '' ↑s \ {p₁}) (vsub_left_injective _)], have hd : insert p₁ (p '' ↑s \ {p₁}) = p '' ↑s, { rw [set.insert_diff_singleton, set.insert_eq_of_mem hp₁] }, have hc'' : fintype.card ↥(insert p₁ (p '' ↑s \ {p₁})) = n + 1, { convert hc' }, rw set.card_insert (p '' ↑s \ {p₁}) (λ h, ((set.mem_diff p₁).2 h).2 rfl) at hc'', simpa using hc'' }, rw [vector_span_eq_span_vsub_set_right_ne k hp₁, findim_span_set_eq_card _ hi', ←hc], congr end /-- The `vector_span` of a finite affinely independent family has dimension one less than its cardinality. -/ lemma findim_vector_span_of_affine_independent [fintype ι] {p : ι → P} (hi : affine_independent k p) {n : ℕ} (hc : fintype.card ι = n + 1) : findim k (vector_span k (set.range p)) = n := begin rw ←finset.card_univ at hc, rw [←set.image_univ, ←finset.coe_univ], exact findim_vector_span_image_finset_of_affine_independent hi hc end /-- If the `vector_span` of a finite subset of an affinely independent family lies in a submodule with dimension one less than its cardinality, it equals that submodule. -/ lemma vector_span_image_finset_eq_of_le_of_affine_independent_of_card_eq_findim_add_one {p : ι → P} (hi : affine_independent k p) {s : finset ι} {sm : submodule k V} [finite_dimensional k sm] (hle : vector_span k (p '' ↑s) ≤ sm) (hc : finset.card s = findim k sm + 1) : vector_span k (p '' ↑s) = sm := eq_of_le_of_findim_eq hle $ findim_vector_span_image_finset_of_affine_independent hi hc /-- If the `vector_span` of a finite affinely independent family lies in a submodule with dimension one less than its cardinality, it equals that submodule. -/ lemma vector_span_eq_of_le_of_affine_independent_of_card_eq_findim_add_one [fintype ι] {p : ι → P} (hi : affine_independent k p) {sm : submodule k V} [finite_dimensional k sm] (hle : vector_span k (set.range p) ≤ sm) (hc : fintype.card ι = findim k sm + 1) : vector_span k (set.range p) = sm := eq_of_le_of_findim_eq hle $ findim_vector_span_of_affine_independent hi hc /-- If the `affine_span` of a finite subset of an affinely independent family lies in an affine subspace whose direction has dimension one less than its cardinality, it equals that subspace. -/ lemma affine_span_image_finset_eq_of_le_of_affine_independent_of_card_eq_findim_add_one {p : ι → P} (hi : affine_independent k p) {s : finset ι} {sp : affine_subspace k P} [finite_dimensional k sp.direction] (hle : affine_span k (p '' ↑s) ≤ sp) (hc : finset.card s = findim k sp.direction + 1) : affine_span k (p '' ↑s) = sp := begin have hn : (p '' ↑s).nonempty, { simp [hc, ←finset.card_pos] }, refine eq_of_direction_eq_of_nonempty_of_le _ ((affine_span_nonempty k _).2 hn) hle, have hd := direction_le hle, rw direction_affine_span at ⊢ hd, exact vector_span_image_finset_eq_of_le_of_affine_independent_of_card_eq_findim_add_one hi hd hc end /-- If the `affine_span` of a finite affinely independent family lies in an affine subspace whose direction has dimension one less than its cardinality, it equals that subspace. -/ lemma affine_span_eq_of_le_of_affine_independent_of_card_eq_findim_add_one [fintype ι] {p : ι → P} (hi : affine_independent k p) {sp : affine_subspace k P} [finite_dimensional k sp.direction] (hle : affine_span k (set.range p) ≤ sp) (hc : fintype.card ι = findim k sp.direction + 1) : affine_span k (set.range p) = sp := begin rw ←finset.card_univ at hc, rw [←set.image_univ, ←finset.coe_univ] at ⊢ hle, exact affine_span_image_finset_eq_of_le_of_affine_independent_of_card_eq_findim_add_one hi hle hc end /-- The `vector_span` of a finite affinely independent family whose cardinality is one more than that of the finite-dimensional space is `⊤`. -/ lemma vector_span_eq_top_of_affine_independent_of_card_eq_findim_add_one [finite_dimensional k V] [fintype ι] {p : ι → P} (hi : affine_independent k p) (hc : fintype.card ι = findim k V + 1) : vector_span k (set.range p) = ⊤ := eq_top_of_findim_eq $ findim_vector_span_of_affine_independent hi hc /-- The `affine_span` of a finite affinely independent family whose cardinality is one more than that of the finite-dimensional space is `⊤`. -/ lemma affine_span_eq_top_of_affine_independent_of_card_eq_findim_add_one [finite_dimensional k V] [fintype ι] {p : ι → P} (hi : affine_independent k p) (hc : fintype.card ι = findim k V + 1) : affine_span k (set.range p) = ⊤ := begin rw [←findim_top, ←direction_top k V P] at hc, exact affine_span_eq_of_le_of_affine_independent_of_card_eq_findim_add_one hi le_top hc end variables (k) /-- The `vector_span` of `n + 1` points in an indexed family has dimension at most `n`. -/ lemma findim_vector_span_image_finset_le (p : ι → P) (s : finset ι) {n : ℕ} (hc : finset.card s = n + 1) : findim k (vector_span k (p '' ↑s)) ≤ n := begin have hn : (p '' ↑s).nonempty, { simp [hc, ←finset.card_pos] }, rcases hn with ⟨p₁, hp₁⟩, rw [vector_span_eq_span_vsub_set_right_ne k hp₁], have hfp₁ : (p '' ↑s \ {p₁}).finite := ((finset.finite_to_set _).image _).subset (set.diff_subset _ _), haveI := hfp₁.fintype, have hf : ((λ p, p -ᵥ p₁) '' (p '' ↑s \ {p₁})).finite := hfp₁.image _, haveI := hf.fintype, convert le_trans (findim_span_le_card ((λ p, p -ᵥ p₁) '' (p '' ↑s \ {p₁}))) _, have hm : p₁ ∉ p '' ↑s \ {p₁}, by simp, haveI := set.fintype_insert' (p '' ↑s \ {p₁}) hm, rw [set.to_finset_card, set.card_image_of_injective (p '' ↑s \ {p₁}) (vsub_left_injective p₁), ←add_le_add_iff_right 1, ←set.card_fintype_insert' _ hm], have h : fintype.card (↑(s.image p) : set P) ≤ n + 1, { rw [fintype.card_coe, ←hc], exact finset.card_image_le }, convert h, simp [hp₁] end /-- The `vector_span` of an indexed family of `n + 1` points has dimension at most `n`. -/ lemma findim_vector_span_range_le [fintype ι] (p : ι → P) {n : ℕ} (hc : fintype.card ι = n + 1) : findim k (vector_span k (set.range p)) ≤ n := begin rw [←set.image_univ, ←finset.coe_univ], rw ←finset.card_univ at hc, exact findim_vector_span_image_finset_le _ _ _ hc end /-- `n + 1` points are affinely independent if and only if their `vector_span` has dimension `n`. -/ lemma affine_independent_iff_findim_vector_span_eq [fintype ι] (p : ι → P) {n : ℕ} (hc : fintype.card ι = n + 1) : affine_independent k p ↔ findim k (vector_span k (set.range p)) = n := begin have hn : nonempty ι, by simp [←fintype.card_pos_iff, hc], cases hn with i₁, rw [affine_independent_iff_linear_independent_vsub _ _ i₁, linear_independent_iff_card_eq_findim_span, eq_comm, vector_span_range_eq_span_range_vsub_right_ne k p i₁], congr', rw ←finset.card_univ at hc, rw fintype.subtype_card, simp [finset.filter_ne', finset.card_erase_of_mem, hc] end /-- `n + 1` points are affinely independent if and only if their `vector_span` has dimension at least `n`. -/ lemma affine_independent_iff_le_findim_vector_span [fintype ι] (p : ι → P) {n : ℕ} (hc : fintype.card ι = n + 1) : affine_independent k p ↔ n ≤ findim k (vector_span k (set.range p)) := begin rw affine_independent_iff_findim_vector_span_eq k p hc, split, { rintro rfl, refl }, { exact λ hle, le_antisymm (findim_vector_span_range_le k p hc) hle } end /-- `n + 2` points are affinely independent if and only if their `vector_span` does not have dimension at most `n`. -/ lemma affine_independent_iff_not_findim_vector_span_le [fintype ι] (p : ι → P) {n : ℕ} (hc : fintype.card ι = n + 2) : affine_independent k p ↔ ¬ findim k (vector_span k (set.range p)) ≤ n := by rw [affine_independent_iff_le_findim_vector_span k p hc, ←nat.lt_iff_add_one_le, lt_iff_not_ge] /-- `n + 2` points have a `vector_span` with dimension at most `n` if and only if they are not affinely independent. -/ lemma findim_vector_span_le_iff_not_affine_independent [fintype ι] (p : ι → P) {n : ℕ} (hc : fintype.card ι = n + 2) : findim k (vector_span k (set.range p)) ≤ n ↔ ¬ affine_independent k p := (not_iff_comm.1 (affine_independent_iff_not_findim_vector_span_le k p hc).symm).symm end affine_space'
130bc6d02e951c53b977b5956762f8a6334f9c0a
d31b9f832ff922a603f76cf32e0f3aa822640508
/src/hott/init/meta/instances.lean
ddb75db6643e879dd7cf36c7d1f4e00bda57d898
[ "Apache-2.0" ]
permissive
javra/hott3
6e7a9e72a991a2fae32e5764982e521dca617b16
cd51f2ab2aa48c1246a188f9b525b30f76c3d651
refs/heads/master
1,585,819,679,148
1,531,232,382,000
1,536,682,965,000
154,294,022
0
0
Apache-2.0
1,540,284,376,000
1,540,284,375,000
null
UTF-8
Lean
false
false
2,627
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ open lean open lean.parser local postfix `?`:9001 := optional local postfix *:9001 := many namespace tactic /-- Reset the instance cache for the main goal. -/ meta def reset_instance_cache : tactic unit := unfreeze_local_instances namespace interactive open interactive interactive.types expr /-- Reset the instance cache. This allows any new instances added to the context to be used in typeclass inference. -/ meta def resetI := reset_instance_cache /-- Unfreeze local instances, which allows us to revert instances in the context. -/ meta def unfreezeI := tactic.unfreeze_local_instances /-- Like `intro`, but uses the introduced variable in typeclass inference. -/ meta def introI (p : parse ident_?) : tactic unit := intro p >> reset_instance_cache /-- Like `intros`, but uses the introduced variable(s) in typeclass inference. -/ meta def introsI (p : parse ident_*) : tactic unit := intros p >> reset_instance_cache /-- Used to add typeclasses to the context so that they can be used in typeclass inference. The syntax is the same as `have`. -/ meta def haveI (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) : tactic unit := do h ← match h with | none := get_unused_name "_inst" | some a := return a end, «have» (some h) q₁ q₂, match q₁ with | none := swap >> reset_instance_cache >> swap | some p₂ := reset_instance_cache end /-- Used to add typeclasses to the context so that they can be used in typeclass inference. The syntax is the same as `let`. -/ meta def letI (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) : tactic unit := do h ← match h with | none := get_unused_name "_inst" | some a := return a end, «let» (some h) q₁ q₂, match q₁ with | none := swap >> reset_instance_cache >> swap | some p₂ := reset_instance_cache end /-- Like `exact`, but uses all variables in the context for typeclass inference. -/ meta def exactI (q : parse texpr) : tactic unit := reset_instance_cache >> exact q /-- Like `apply`, but uses all variables in the context for typeclass inference. -/ meta def applyI (q : parse texpr) : tactic unit := reset_instance_cache >> apply q /-- Like `apply`, but doesn't use type class inference. -/ meta def napply (q : parse texpr) : tactic unit := concat_tags (do h ← i_to_expr_for_apply q, tactic.apply h {instances := ff}) end interactive end tactic
d2764057872ecb8ee433b73ef7bf9463b4752cd4
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/meta/mk_dec_eq_instance.lean
bcda0773e7c84380111feec4ef936f557f4c5843
[]
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
675
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 Helper tactic for showing that a type has decidable equality. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.meta.contradiction_tactic import Mathlib.Lean3Lib.init.meta.constructor_tactic import Mathlib.Lean3Lib.init.meta.injection_tactic import Mathlib.Lean3Lib.init.meta.relation_tactics import Mathlib.Lean3Lib.init.meta.rec_util import Mathlib.Lean3Lib.init.meta.interactive namespace Mathlib namespace tactic /- Retrieve the name of the type we are building a decidable equality proof for. -/
1786186ff2bc02dde5830b544396183706fcd4aa
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/box_integral/basic.lean
0449ea5728472887275ff87bb5fe42e3b152ff09
[ "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
43,281
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.partition.filter import analysis.box_integral.partition.measure import topology.uniform_space.compact_separated /-! # Integrals of Riemann, Henstock-Kurzweil, and McShane In this file we define the integral of a function over a box in `ℝⁿ. The same definition works for Riemann, Henstock-Kurzweil, and McShane integrals. As usual, we represent `ℝⁿ` as the type of functions `ι → ℝ` for some finite type `ι`. A rectangular box `(l, u]` in `ℝⁿ` is defined to be the set `{x : ι → ℝ | ∀ i, l i < x i ∧ x i ≤ u i}`, see `box_integral.box`. Let `vol` be a box-additive function on boxes in `ℝⁿ` with codomain `E →L[ℝ] F`. Given a function `f : ℝⁿ → E`, a box `I` and a tagged partition `π` of this box, the *integral sum* of `f` over `π` with respect to the volume `vol` is the sum of `vol J (f (π.tag J))` over all boxes of `π`. Here `π.tag J` is the point (tag) in `ℝⁿ` associated with the box `J`. The integral is defined as the limit of integral sums along a filter. Different filters correspond to different integration theories. In order to avoid code duplication, all our definitions and theorems take an argument `l : box_integral.integration_params`. This is a type that holds three boolean values, and encodes eight filters including those corresponding to Riemann, Henstock-Kurzweil, and McShane integrals. Following the design of infinite sums (see `has_sum` and `tsum`), we define a predicate `box_integral.has_integral` and a function `box_integral.integral` that returns a vector satisfying the predicate or zero if the function is not integrable. Then we prove some basic properties of box integrals (linearity, a formula for the integral of a constant). We also prove a version of the Henstock-Sacks inequality (see `box_integral.integrable.dist_integral_sum_le_of_mem_base_set` and `box_integral.integrable.dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq`), prove integrability of continuous functions, and provide a criterion for integrability w.r.t. a non-Riemann filter (e.g., Henstock-Kurzweil and McShane). ## Notation - `ℝⁿ`: local notation for `ι → ℝ` ## Tags integral -/ open_locale big_operators classical topological_space nnreal filter uniformity box_integral open set finset function filter metric box_integral.integration_params noncomputable theory namespace box_integral universes u v w variables {ι : Type u} {E : Type v} {F : Type w} [normed_group E] [normed_space ℝ E] [normed_group F] [normed_space ℝ F] {I J : box ι} {π : tagged_prepartition I} open tagged_prepartition local notation `ℝⁿ` := ι → ℝ /-! ### Integral sum and its basic properties -/ /-- The integral sum of `f : ℝⁿ → E` over a tagged prepartition `π` w.r.t. box-additive volume `vol` with codomain `E →L[ℝ] F` is the sum of `vol J (f (π.tag J))` over all boxes of `π`. -/ def integral_sum (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) : F := ∑ J in π.boxes, vol J (f (π.tag J)) lemma integral_sum_bUnion_tagged (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : prepartition I) (πi : Π J, tagged_prepartition J) : integral_sum f vol (π.bUnion_tagged πi) = ∑ J in π.boxes, integral_sum f vol (πi J) := begin refine (π.sum_bUnion_boxes _ _).trans (sum_congr rfl $ λ J hJ, sum_congr rfl $ λ J' hJ', _), rw π.tag_bUnion_tagged hJ hJ' end lemma integral_sum_bUnion_partition (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) (πi : Π J, prepartition J) (hπi : ∀ J ∈ π, (πi J).is_partition) : integral_sum f vol (π.bUnion_prepartition πi) = integral_sum f vol π := begin refine (π.to_prepartition.sum_bUnion_boxes _ _).trans (sum_congr rfl $ λ J hJ, _), calc ∑ J' in (πi J).boxes, vol J' (f (π.tag $ π.to_prepartition.bUnion_index πi J')) = ∑ J' in (πi J).boxes, vol J' (f (π.tag J)) : sum_congr rfl (λ J' hJ', by rw [prepartition.bUnion_index_of_mem _ hJ hJ']) ... = vol J (f (π.tag J)) : (vol.map ⟨λ g : E →L[ℝ] F, g (f (π.tag J)), rfl, λ _ _, rfl⟩).sum_partition_boxes le_top (hπi J hJ) end lemma integral_sum_inf_partition (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) {π' : prepartition I} (h : π'.is_partition) : integral_sum f vol (π.inf_prepartition π') = integral_sum f vol π := integral_sum_bUnion_partition f vol π _ $ λ J hJ, h.restrict (prepartition.le_of_mem _ hJ) lemma integral_sum_fiberwise {α} (g : box ι → α) (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) : ∑ y in π.boxes.image g, integral_sum f vol (π.filter (λ x, g x = y)) = integral_sum f vol π := π.to_prepartition.sum_fiberwise g (λ J, vol J (f $ π.tag J)) lemma integral_sum_sub_partitions (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) {π₁ π₂ : tagged_prepartition I} (h₁ : π₁.is_partition) (h₂ : π₂.is_partition) : integral_sum f vol π₁ - integral_sum f vol π₂ = ∑ J in (π₁.to_prepartition ⊓ π₂.to_prepartition).boxes, (vol J (f $ (π₁.inf_prepartition π₂.to_prepartition).tag J) - vol J (f $ (π₂.inf_prepartition π₁.to_prepartition).tag J)) := begin rw [← integral_sum_inf_partition f vol π₁ h₂, ← integral_sum_inf_partition f vol π₂ h₁, integral_sum, integral_sum, finset.sum_sub_distrib], simp only [inf_prepartition_to_prepartition, _root_.inf_comm] end @[simp] lemma integral_sum_disj_union (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) {π₁ π₂ : tagged_prepartition I} (h : disjoint π₁.Union π₂.Union) : integral_sum f vol (π₁.disj_union π₂ h) = integral_sum f vol π₁ + integral_sum f vol π₂ := begin refine (prepartition.sum_disj_union_boxes h _).trans (congr_arg2 (+) (sum_congr rfl $ λ J hJ, _) (sum_congr rfl $ λ J hJ, _)), { rw disj_union_tag_of_mem_left _ hJ }, { rw disj_union_tag_of_mem_right _ hJ } end @[simp] lemma integral_sum_add (f g : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) : integral_sum (f + g) vol π = integral_sum f vol π + integral_sum g vol π := by simp only [integral_sum, pi.add_apply, (vol _).map_add, finset.sum_add_distrib] @[simp] lemma integral_sum_neg (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) : integral_sum (-f) vol π = -integral_sum f vol π := by simp only [integral_sum, pi.neg_apply, (vol _).map_neg, finset.sum_neg_distrib] @[simp] lemma integral_sum_smul (c : ℝ) (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) : integral_sum (c • f) vol π = c • integral_sum f vol π := by simp only [integral_sum, finset.smul_sum, pi.smul_apply, continuous_linear_map.map_smul] variables [fintype ι] /-! ### Basic integrability theory -/ /-- The predicate `has_integral I l f vol y` says that `y` is the integral of `f` over `I` along `l` w.r.t. volume `vol`. This means that integral sums of `f` tend to `𝓝 y` along `box_integral.integration_params.to_filter_Union I ⊤`. -/ def has_integral (I : box ι) (l : integration_params) (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (y : F) : Prop := tendsto (integral_sum f vol) (l.to_filter_Union I ⊤) (𝓝 y) /-- A function is integrable if there exists a vector that satisfies the `has_integral` predicate. -/ def integrable (I : box ι) (l : integration_params) (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) := ∃ y, has_integral I l f vol y /-- The integral of a function `f` over a box `I` along a filter `l` w.r.t. a volume `vol`. Returns zero on non-integrable functions. -/ def integral (I : box ι) (l : integration_params) (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) := if h : integrable I l f vol then h.some else 0 variables {l : integration_params} {f g : ℝⁿ → E} {vol : ι →ᵇᵃ (E →L[ℝ] F)} {y y' : F} /-- Reinterpret `box_integral.has_integral` as `filter.tendsto`, e.g., dot-notation theorems that are shadowed in the `box_integral.has_integral` namespace. -/ lemma has_integral.tendsto (h : has_integral I l f vol y) : tendsto (integral_sum f vol) (l.to_filter_Union I ⊤) (𝓝 y) := h /-- The `ε`-`δ` definition of `box_integral.has_integral`. -/ lemma has_integral_iff : has_integral I l f vol y ↔ ∀ ε > (0 : ℝ), ∃ r : ℝ≥0 → ℝⁿ → Ioi (0 : ℝ), (∀ c, l.r_cond (r c)) ∧ ∀ c π, l.mem_base_set I c (r c) π → is_partition π → dist (integral_sum f vol π) y ≤ ε := ((l.has_basis_to_filter_Union_top I).tendsto_iff nhds_basis_closed_ball).trans $ by simp [@forall_swap ℝ≥0 (tagged_prepartition I)] /-- Quite often it is more natural to prove an estimate of the form `a * ε`, not `ε` in the RHS of `box_integral.has_integral_iff`, so we provide this auxiliary lemma. -/ lemma has_integral_of_mul (a : ℝ) (h : ∀ ε : ℝ, 0 < ε → ∃ r: ℝ≥0 → ℝⁿ → Ioi (0 : ℝ), (∀ c, l.r_cond (r c)) ∧ ∀ c π, l.mem_base_set I c (r c) π → is_partition π → dist (integral_sum f vol π) y ≤ a * ε) : has_integral I l f vol y := begin refine has_integral_iff.2 (λ ε hε, _), rcases exists_pos_mul_lt hε a with ⟨ε', hε', ha⟩, rcases h ε' hε' with ⟨r, hr, H⟩, exact ⟨r, hr, λ c π hπ hπp, (H c π hπ hπp).trans ha.le⟩ end lemma integrable_iff_cauchy [complete_space F] : integrable I l f vol ↔ cauchy ((l.to_filter_Union I ⊤).map (integral_sum f vol)) := cauchy_map_iff_exists_tendsto.symm /-- In a complete space, a function is integrable if and only if its integral sums form a Cauchy net. Here we restate this fact in terms of `∀ ε > 0, ∃ r, ...`. -/ lemma integrable_iff_cauchy_basis [complete_space F] : integrable I l f vol ↔ ∀ ε > (0 : ℝ), ∃ r : ℝ≥0 → ℝⁿ → Ioi (0 : ℝ), (∀ c, l.r_cond (r c)) ∧ ∀ c₁ c₂ π₁ π₂, l.mem_base_set I c₁ (r c₁) π₁ → π₁.is_partition → l.mem_base_set I c₂ (r c₂) π₂ → π₂.is_partition → dist (integral_sum f vol π₁) (integral_sum f vol π₂) ≤ ε := begin rw [integrable_iff_cauchy, cauchy_map_iff', (l.has_basis_to_filter_Union_top _).prod_self.tendsto_iff uniformity_basis_dist_le], refine forall₂_congr (λ ε ε0, exists_congr $ λ r, _), simp only [exists_prop, prod.forall, set.mem_Union, exists_imp_distrib, prod_mk_mem_set_prod_eq, and_imp, mem_inter_eq, mem_set_of_eq], exact and_congr iff.rfl ⟨λ H c₁ c₂ π₁ π₂ h₁ hU₁ h₂ hU₂, H π₁ π₂ c₁ h₁ hU₁ c₂ h₂ hU₂, λ H π₁ π₂ c₁ h₁ hU₁ c₂ h₂ hU₂, H c₁ c₂ π₁ π₂ h₁ hU₁ h₂ hU₂⟩ end lemma has_integral.mono {l₁ l₂ : integration_params} (h : has_integral I l₁ f vol y) (hl : l₂ ≤ l₁) : has_integral I l₂ f vol y := h.mono_left $ integration_params.to_filter_Union_mono _ hl _ protected lemma integrable.has_integral (h : integrable I l f vol) : has_integral I l f vol (integral I l f vol) := by { rw [integral, dif_pos h], exact classical.some_spec h } lemma integrable.mono {l'} (h : integrable I l f vol) (hle : l' ≤ l) : integrable I l' f vol := ⟨_, h.has_integral.mono hle⟩ lemma has_integral.unique (h : has_integral I l f vol y) (h' : has_integral I l f vol y') : y = y' := tendsto_nhds_unique h h' lemma has_integral.integrable (h : has_integral I l f vol y) : integrable I l f vol := ⟨_, h⟩ lemma has_integral.integral_eq (h : has_integral I l f vol y) : integral I l f vol = y := h.integrable.has_integral.unique h lemma has_integral.add (h : has_integral I l f vol y) (h' : has_integral I l g vol y') : has_integral I l (f + g) vol (y + y') := by simpa only [has_integral, ← integral_sum_add] using h.add h' lemma integrable.add (hf : integrable I l f vol) (hg : integrable I l g vol) : integrable I l (f + g) vol := (hf.has_integral.add hg.has_integral).integrable lemma integral_add (hf : integrable I l f vol) (hg : integrable I l g vol) : integral I l (f + g) vol = integral I l f vol + integral I l g vol := (hf.has_integral.add hg.has_integral).integral_eq lemma has_integral.neg (hf : has_integral I l f vol y) : has_integral I l (-f) vol (-y) := by simpa only [has_integral, ← integral_sum_neg] using hf.neg lemma integrable.neg (hf : integrable I l f vol) : integrable I l (-f) vol := hf.has_integral.neg.integrable lemma integrable.of_neg (hf : integrable I l (-f) vol) : integrable I l f vol := neg_neg f ▸ hf.neg @[simp] lemma integrable_neg : integrable I l (-f) vol ↔ integrable I l f vol := ⟨λ h, h.of_neg, λ h, h.neg⟩ @[simp] lemma integral_neg : integral I l (-f) vol = -integral I l f vol := if h : integrable I l f vol then h.has_integral.neg.integral_eq else by rw [integral, integral, dif_neg h, dif_neg (mt integrable.of_neg h), neg_zero] lemma has_integral.sub (h : has_integral I l f vol y) (h' : has_integral I l g vol y') : has_integral I l (f - g) vol (y - y') := by simpa only [sub_eq_add_neg] using h.add h'.neg lemma integrable.sub (hf : integrable I l f vol) (hg : integrable I l g vol) : integrable I l (f - g) vol := (hf.has_integral.sub hg.has_integral).integrable lemma integral_sub (hf : integrable I l f vol) (hg : integrable I l g vol) : integral I l (f - g) vol = integral I l f vol - integral I l g vol := (hf.has_integral.sub hg.has_integral).integral_eq lemma has_integral_const (c : E) : has_integral I l (λ _, c) vol (vol I c) := tendsto_const_nhds.congr' $ (l.eventually_is_partition I).mono $ λ π hπ, ((vol.map ⟨λ g : E →L[ℝ] F, g c, rfl, λ _ _, rfl⟩).sum_partition_boxes le_top hπ).symm @[simp] lemma integral_const (c : E) : integral I l (λ _, c) vol = vol I c := (has_integral_const c).integral_eq lemma integrable_const (c : E) : integrable I l (λ _, c) vol := ⟨_, has_integral_const c⟩ lemma has_integral_zero : has_integral I l (λ _, (0:E)) vol 0 := by simpa only [← (vol I).map_zero] using has_integral_const (0 : E) lemma integrable_zero : integrable I l (λ _, (0:E)) vol := ⟨0, has_integral_zero⟩ lemma integral_zero : integral I l (λ _, (0:E)) vol = 0 := has_integral_zero.integral_eq lemma has_integral_sum {α : Type*} {s : finset α} {f : α → ℝⁿ → E} {g : α → F} (h : ∀ i ∈ s, has_integral I l (f i) vol (g i)) : has_integral I l (λ x, ∑ i in s, f i x) vol (∑ i in s, g i) := begin induction s using finset.induction_on with a s ha ihs, { simp [has_integral_zero] }, simp only [finset.sum_insert ha], rw finset.forall_mem_insert at h, exact h.1.add (ihs h.2) end lemma has_integral.smul (hf : has_integral I l f vol y) (c : ℝ) : has_integral I l (c • f) vol (c • y) := by simpa only [has_integral, ← integral_sum_smul] using (tendsto_const_nhds : tendsto _ _ (𝓝 c)).smul hf lemma integrable.smul (hf : integrable I l f vol) (c : ℝ) : integrable I l (c • f) vol := (hf.has_integral.smul c).integrable lemma integrable.of_smul {c : ℝ} (hf : integrable I l (c • f) vol) (hc : c ≠ 0) : integrable I l f vol := by { convert hf.smul c⁻¹, ext x, simp only [pi.smul_apply, inv_smul_smul₀ hc] } @[simp] lemma integral_smul (c : ℝ) : integral I l (λ x, c • f x) vol = c • integral I l f vol := begin rcases eq_or_ne c 0 with rfl | hc, { simp only [zero_smul, integral_zero] }, by_cases hf : integrable I l f vol, { exact (hf.has_integral.smul c).integral_eq }, { have : ¬integrable I l (λ x, c • f x) vol, from mt (λ h, h.of_smul hc) hf, rw [integral, integral, dif_neg hf, dif_neg this, smul_zero] } end open measure_theory /-- The integral of a nonnegative function w.r.t. a volume generated by a locally-finite measure is nonnegative. -/ lemma integral_nonneg {g : ℝⁿ → ℝ} (hg : ∀ x ∈ I.Icc, 0 ≤ g x) (μ : measure ℝⁿ) [is_locally_finite_measure μ] : 0 ≤ integral I l g μ.to_box_additive.to_smul := begin by_cases hgi : integrable I l g μ.to_box_additive.to_smul, { refine ge_of_tendsto' hgi.has_integral (λ π, sum_nonneg $ λ J hJ, _), exact mul_nonneg ennreal.to_real_nonneg (hg _ $ π.tag_mem_Icc _) }, { rw [integral, dif_neg hgi] } end /-- If `∥f x∥ ≤ g x` on `[l, u]` and `g` is integrable, then the norm of the integral of `f` is less than or equal to the integral of `g`. -/ lemma norm_integral_le_of_norm_le {g : ℝⁿ → ℝ} (hle : ∀ x ∈ I.Icc, ∥f x∥ ≤ g x) (μ : measure ℝⁿ) [is_locally_finite_measure μ] (hg : integrable I l g μ.to_box_additive.to_smul) : ∥(integral I l f μ.to_box_additive.to_smul : E)∥ ≤ integral I l g μ.to_box_additive.to_smul := begin by_cases hfi : integrable.{u v v} I l f μ.to_box_additive.to_smul, { refine le_of_tendsto_of_tendsto' hfi.has_integral.norm hg.has_integral (λ π, _), refine norm_sum_le_of_le _ (λ J hJ, _), simp only [box_additive_map.to_smul_apply, norm_smul, smul_eq_mul, real.norm_eq_abs, μ.to_box_additive_apply, abs_of_nonneg ennreal.to_real_nonneg], exact mul_le_mul_of_nonneg_left (hle _ $ π.tag_mem_Icc _) ennreal.to_real_nonneg }, { rw [integral, dif_neg hfi, norm_zero], exact integral_nonneg (λ x hx, (norm_nonneg _).trans (hle x hx)) μ } end lemma norm_integral_le_of_le_const {c : ℝ} (hc : ∀ x ∈ I.Icc, ∥f x∥ ≤ c) (μ : measure ℝⁿ) [is_locally_finite_measure μ] : ∥(integral I l f μ.to_box_additive.to_smul : E)∥ ≤ (μ I).to_real * c := by simpa only [integral_const] using norm_integral_le_of_norm_le hc μ (integrable_const c) /-! # Henstock-Sacks inequality and integrability on subboxes Henstock-Sacks inequality for Henstock-Kurzweil integral says the following. Let `f` be a function integrable on a box `I`; let `r : ℝⁿ → (0, ∞)` be a function such that for any tagged partition of `I` subordinate to `r`, the integral sum over this partition is `ε`-close to the integral. Then for any tagged prepartition (i.e. a finite collections of pairwise disjoint subboxes of `I` with tagged points) `π`, the integral sum over `π` differs from the integral of `f` over the part of `I` covered by `π` by at most `ε`. The actual statement in the library is a bit more complicated to make it work for any `box_integral.integration_params`. We formalize several versions of this inequality in `box_integral.integrable.dist_integral_sum_le_of_mem_base_set`, `box_integral.integrable.dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq`, and `box_integral.integrable.dist_integral_sum_sum_integral_le_of_mem_base_set`. Instead of using predicate assumptions on `r`, we define `box_integral.integrable.convergence_r (h : integrable I l f vol) (ε : ℝ) (c : ℝ≥0) : ℝⁿ → (0, ∞)` to be a function `r` such that - if `l.bRiemann`, then `r` is a constant; - if `ε > 0`, then for any tagged partition `π` of `I` subordinate to `r` (more precisely, satisfying the predicate `l.mem_base_set I c r`), the integral sum of `f` over `π` differs from the integral of `f` over `I` by at most `ε`. The proof is mostly based on [Russel A. Gordon, *The integrals of Lebesgue, Denjoy, Perron, and Henstock*][Gordon55]. -/ namespace integrable /-- If `ε > 0`, then `box_integral.integrable.convergence_r` is a function `r : ℝ≥0 → ℝⁿ → (0, ∞)` such that for every `c : ℝ≥0`, for every tagged partition `π` subordinate to `r` (and satisfying additional distortion estimates if `box_integral.integration_params.bDistortion l = tt`), the corresponding integral sum is `ε`-close to the integral. If `box.integral.integration_params.bRiemann = tt`, then `r c x` does not depend on `x`. If `ε ≤ 0`, then we use `r c x = 1`. -/ def convergence_r (h : integrable I l f vol) (ε : ℝ) : ℝ≥0 → ℝⁿ → Ioi (0 : ℝ) := if hε : 0 < ε then (has_integral_iff.1 h.has_integral ε hε).some else λ _ _, ⟨1, set.mem_Ioi.2 zero_lt_one⟩ variables {c c₁ c₂ : ℝ≥0} {ε ε₁ ε₂ : ℝ} {π₁ π₂ : tagged_prepartition I} lemma convergence_r_cond (h : integrable I l f vol) (ε : ℝ) (c : ℝ≥0) : l.r_cond (h.convergence_r ε c) := begin rw convergence_r, split_ifs with h₀, exacts [(has_integral_iff.1 h.has_integral ε h₀).some_spec.1 _, λ _ x, rfl] end lemma dist_integral_sum_integral_le_of_mem_base_set (h : integrable I l f vol) (h₀ : 0 < ε) (hπ : l.mem_base_set I c (h.convergence_r ε c) π) (hπp : π.is_partition) : dist (integral_sum f vol π) (integral I l f vol) ≤ ε := begin rw [convergence_r, dif_pos h₀] at hπ, exact (has_integral_iff.1 h.has_integral ε h₀).some_spec.2 c _ hπ hπp end /-- **Henstock-Sacks inequality**. Let `r₁ r₂ : ℝⁿ → (0, ∞)` be function such that for any tagged *partition* of `I` subordinate to `rₖ`, `k=1,2`, the integral sum of `f` over this partition differs from the integral of `f` by at most `εₖ`. Then for any two tagged *prepartition* `π₁ π₂` subordinate to `r₁` and `r₂` respectively and covering the same part of `I`, the integral sums of `f` over these prepartitions differ from each other by at most `ε₁ + ε₂`. The actual statement - uses `box_integral.integrable.convergence_r` instead of a predicate assumption on `r`; - uses `box_integral.integration_params.mem_base_set` instead of “subordinate to `r`” to account for additional requirements like being a Henstock partition or having a bounded distortion. See also `box_integral.integrable.dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq` and `box_integral.integrable.dist_integral_sum_sum_integral_le_of_mem_base_set`. -/ lemma dist_integral_sum_le_of_mem_base_set (h : integrable I l f vol) (hpos₁ : 0 < ε₁) (hpos₂ : 0 < ε₂) (h₁ : l.mem_base_set I c₁ (h.convergence_r ε₁ c₁) π₁) (h₂ : l.mem_base_set I c₂ (h.convergence_r ε₂ c₂) π₂) (HU : π₁.Union = π₂.Union) : dist (integral_sum f vol π₁) (integral_sum f vol π₂) ≤ ε₁ + ε₂ := begin rcases h₁.exists_common_compl h₂ HU with ⟨π, hπU, hπc₁, hπc₂⟩, set r : ℝⁿ → Ioi (0 : ℝ) := λ x, min (h.convergence_r ε₁ c₁ x) (h.convergence_r ε₂ c₂ x), have hr : l.r_cond r := (h.convergence_r_cond _ c₁).min (h.convergence_r_cond _ c₂), set πr := π.to_subordinate r, have H₁ : dist (integral_sum f vol (π₁.union_compl_to_subordinate π hπU r)) (integral I l f vol) ≤ ε₁, from h.dist_integral_sum_integral_le_of_mem_base_set hpos₁ (h₁.union_compl_to_subordinate (λ _ _, min_le_left _ _) hπU hπc₁) (is_partition_union_compl_to_subordinate _ _ _ _), rw HU at hπU, have H₂ : dist (integral_sum f vol (π₂.union_compl_to_subordinate π hπU r)) (integral I l f vol) ≤ ε₂, from h.dist_integral_sum_integral_le_of_mem_base_set hpos₂ (h₂.union_compl_to_subordinate (λ _ _, min_le_right _ _) hπU hπc₂) (is_partition_union_compl_to_subordinate _ _ _ _), simpa [union_compl_to_subordinate] using (dist_triangle_right _ _ _).trans (add_le_add H₁ H₂) end /-- If `f` is integrable on `I` along `l`, then for two sufficiently fine tagged prepartitions (in the sense of the filter `box_integral.integration_params.to_filter l I`) such that they cover the same part of `I`, the integral sums of `f` over `π₁` and `π₂` are very close to each other. -/ lemma tendsto_integral_sum_to_filter_prod_self_inf_Union_eq_uniformity (h : integrable I l f vol) : tendsto (λ π : tagged_prepartition I × tagged_prepartition I, (integral_sum f vol π.1, integral_sum f vol π.2)) ((l.to_filter I ×ᶠ l.to_filter I) ⊓ 𝓟 {π | π.1.Union = π.2.Union}) (𝓤 F) := begin refine (((l.has_basis_to_filter I).prod_self.inf_principal _).tendsto_iff uniformity_basis_dist_le).2 (λ ε ε0, _), replace ε0 := half_pos ε0, use [h.convergence_r (ε / 2), h.convergence_r_cond (ε / 2)], rintro ⟨π₁, π₂⟩ ⟨⟨h₁, h₂⟩, hU⟩, rw ← add_halves ε, exact h.dist_integral_sum_le_of_mem_base_set ε0 ε0 h₁.some_spec h₂.some_spec hU end /-- If `f` is integrable on a box `I` along `l`, then for any fixed subset `s` of `I` that can be represented as a finite union of boxes, the integral sums of `f` over tagged prepartitions that cover exactly `s` form a Cauchy “sequence” along `l`. -/ lemma cauchy_map_integral_sum_to_filter_Union (h : integrable I l f vol) (π₀ : prepartition I) : cauchy ((l.to_filter_Union I π₀).map (integral_sum f vol)) := begin refine ⟨infer_instance, _⟩, rw [prod_map_map_eq, ← to_filter_inf_Union_eq, ← prod_inf_prod, prod_principal_principal], exact h.tendsto_integral_sum_to_filter_prod_self_inf_Union_eq_uniformity.mono_left (inf_le_inf_left _ $ principal_mono.2 $ λ π h, h.1.trans h.2.symm) end variable [complete_space F] lemma to_subbox_aux (h : integrable I l f vol) (hJ : J ≤ I) : ∃ y : F, has_integral J l f vol y ∧ tendsto (integral_sum f vol) (l.to_filter_Union I (prepartition.single I J hJ)) (𝓝 y) := begin refine (cauchy_map_iff_exists_tendsto.1 (h.cauchy_map_integral_sum_to_filter_Union (prepartition.single I J hJ))).imp (λ y hy, ⟨_, hy⟩), convert hy.comp (l.tendsto_embed_box_to_filter_Union_top hJ) -- faster than `exact` here end /-- If `f` is integrable on a box `I`, then it is integrable on any subbox of `I`. -/ lemma to_subbox (h : integrable I l f vol) (hJ : J ≤ I) : integrable J l f vol := (h.to_subbox_aux hJ).imp $ λ y, and.left /-- If `f` is integrable on a box `I`, then integral sums of `f` over tagged prepartitions that cover exactly a subbox `J ≤ I` tend to the integral of `f` over `J` along `l`. -/ lemma tendsto_integral_sum_to_filter_Union_single (h : integrable I l f vol) (hJ : J ≤ I) : tendsto (integral_sum f vol) (l.to_filter_Union I (prepartition.single I J hJ)) (𝓝 $ integral J l f vol) := let ⟨y, h₁, h₂⟩ := h.to_subbox_aux hJ in h₁.integral_eq.symm ▸ h₂ /-- **Henstock-Sacks inequality**. Let `r : ℝⁿ → (0, ∞)` be a function such that for any tagged *partition* of `I` subordinate to `r`, the integral sum of `f` over this partition differs from the integral of `f` by at most `ε`. Then for any tagged *prepartition* `π` subordinate to `r`, the integral sum of `f` over this prepartition differs from the integral of `f` over the part of `I` covered by `π` by at most `ε`. The actual statement - uses `box_integral.integrable.convergence_r` instead of a predicate assumption on `r`; - uses `box_integral.integration_params.mem_base_set` instead of “subordinate to `r`” to account for additional requirements like being a Henstock partition or having a bounded distortion; - takes an extra argument `π₀ : prepartition I` and an assumption `π.Union = π₀.Union` instead of using `π.to_prepartition`. -/ lemma dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq (h : integrable I l f vol) (h0 : 0 < ε) (hπ : l.mem_base_set I c (h.convergence_r ε c) π) {π₀ : prepartition I} (hU : π.Union = π₀.Union) : dist (integral_sum f vol π) (∑ J in π₀.boxes, integral J l f vol) ≤ ε := begin /- Let us prove that the distance is less than or equal to `ε + δ` for all positive `δ`. -/ refine le_of_forall_pos_le_add (λ δ δ0, _), /- First we choose some constants. -/ set δ' : ℝ := δ / (π₀.boxes.card + 1), have H0 : 0 < (π₀.boxes.card + 1 : ℝ) := nat.cast_add_one_pos _, have δ'0 : 0 < δ' := div_pos δ0 H0, set C := max π₀.distortion π₀.compl.distortion, /- Next we choose a tagged partition of each `J ∈ π₀` such that the integral sum of `f` over this partition is `δ'`-close to the integral of `f` over `J`. -/ have : ∀ J ∈ π₀, ∃ πi : tagged_prepartition J, πi.is_partition ∧ dist (integral_sum f vol πi) (integral J l f vol) ≤ δ' ∧ l.mem_base_set J C (h.convergence_r δ' C) πi, { intros J hJ, have Hle : J ≤ I := π₀.le_of_mem hJ, have HJi : integrable J l f vol := h.to_subbox Hle, set r := λ x, min (h.convergence_r δ' C x) (HJi.convergence_r δ' C x), have hr : l.r_cond r, from (h.convergence_r_cond _ C).min (HJi.convergence_r_cond _ C), have hJd : J.distortion ≤ C, from le_trans (finset.le_sup hJ) (le_max_left _ _), rcases l.exists_mem_base_set_is_partition J hJd r with ⟨πJ, hC, hp⟩, have hC₁ : l.mem_base_set J C (HJi.convergence_r δ' C) πJ, { refine hC.mono J le_rfl le_rfl (λ x hx, _), exact min_le_right _ _ }, have hC₂ : l.mem_base_set J C (h.convergence_r δ' C) πJ, { refine hC.mono J le_rfl le_rfl (λ x hx, _), exact min_le_left _ _ }, exact ⟨πJ, hp, HJi.dist_integral_sum_integral_le_of_mem_base_set δ'0 hC₁ hp, hC₂⟩ }, /- Now we combine these tagged partitions into a tagged prepartition of `I` that covers the same part of `I` as `π₀` and apply `box_integral.dist_integral_sum_le_of_mem_base_set` to `π` and this prepartition. -/ choose! πi hπip hπiδ' hπiC, have : l.mem_base_set I C (h.convergence_r δ' C) (π₀.bUnion_tagged πi), from bUnion_tagged_mem_base_set hπiC hπip (λ _, le_max_right _ _), have hU' : π.Union = (π₀.bUnion_tagged πi).Union, from hU.trans (prepartition.Union_bUnion_partition _ hπip).symm, have := h.dist_integral_sum_le_of_mem_base_set h0 δ'0 hπ this hU', rw integral_sum_bUnion_tagged at this, calc dist (integral_sum f vol π) (∑ J in π₀.boxes, integral J l f vol) ≤ dist (integral_sum f vol π) (∑ J in π₀.boxes, integral_sum f vol (πi J)) + dist (∑ J in π₀.boxes, integral_sum f vol (πi J)) (∑ J in π₀.boxes, integral J l f vol) : dist_triangle _ _ _ ... ≤ (ε + δ') + ∑ J in π₀.boxes, δ' : add_le_add this (dist_sum_sum_le_of_le _ hπiδ') ... = ε + δ : by { field_simp [H0.ne'], ring } end /-- **Henstock-Sacks inequality**. Let `r : ℝⁿ → (0, ∞)` be a function such that for any tagged *partition* of `I` subordinate to `r`, the integral sum of `f` over this partition differs from the integral of `f` by at most `ε`. Then for any tagged *prepartition* `π` subordinate to `r`, the integral sum of `f` over this prepartition differs from the integral of `f` over the part of `I` covered by `π` by at most `ε`. The actual statement - uses `box_integral.integrable.convergence_r` instead of a predicate assumption on `r`; - uses `box_integral.integration_params.mem_base_set` instead of “subordinate to `r`” to account for additional requirements like being a Henstock partition or having a bounded distortion; -/ lemma dist_integral_sum_sum_integral_le_of_mem_base_set (h : integrable I l f vol) (h0 : 0 < ε) (hπ : l.mem_base_set I c (h.convergence_r ε c) π) : dist (integral_sum f vol π) (∑ J in π.boxes, integral J l f vol) ≤ ε := h.dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq h0 hπ rfl /-- Integral sum of `f` over a tagged prepartition `π` such that `π.Union = π₀.Union` tends to the sum of integrals of `f` over the boxes of `π₀`. -/ lemma tendsto_integral_sum_sum_integral (h : integrable I l f vol) (π₀ : prepartition I) : tendsto (integral_sum f vol) (l.to_filter_Union I π₀) (𝓝 $ ∑ J in π₀.boxes, integral J l f vol) := begin refine ((l.has_basis_to_filter_Union I π₀).tendsto_iff nhds_basis_closed_ball).2 (λ ε ε0, _), refine ⟨h.convergence_r ε, h.convergence_r_cond ε, _⟩, simp only [mem_inter_eq, set.mem_Union, mem_set_of_eq], rintro π ⟨c, hc, hU⟩, exact h.dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq ε0 hc hU end /-- If `f` is integrable on `I`, then `λ J, integral J l f vol` is box-additive on subboxes of `I`: if `π₁`, `π₂` are two prepartitions of `I` covering the same part of `I`, then the sum of integrals of `f` over the boxes of `π₁` is equal to the sum of integrals of `f` over the boxes of `π₂`. See also `box_integral.integrable.to_box_additive` for a bundled version. -/ lemma sum_integral_congr (h : integrable I l f vol) {π₁ π₂ : prepartition I} (hU : π₁.Union = π₂.Union) : ∑ J in π₁.boxes, integral J l f vol = ∑ J in π₂.boxes, integral J l f vol := begin refine tendsto_nhds_unique (h.tendsto_integral_sum_sum_integral π₁) _, rw l.to_filter_Union_congr _ hU, exact h.tendsto_integral_sum_sum_integral π₂ end /-- If `f` is integrable on `I`, then `λ J, integral J l f vol` is box-additive on subboxes of `I`: if `π₁`, `π₂` are two prepartitions of `I` covering the same part of `I`, then the sum of integrals of `f` over the boxes of `π₁` is equal to the sum of integrals of `f` over the boxes of `π₂`. See also `box_integral.integrable.sum_integral_congr` for an unbundled version. -/ @[simps] def to_box_additive (h : integrable I l f vol) : ι →ᵇᵃ[I] F := { to_fun := λ J, integral J l f vol, sum_partition_boxes' := λ J hJ π hπ, begin replace hπ := hπ.Union_eq, rw ← prepartition.Union_top at hπ, rw [(h.to_subbox (with_top.coe_le_coe.1 hJ)).sum_integral_congr hπ, prepartition.top_boxes, sum_singleton] end } end integrable open measure_theory /-! ### Integrability conditions -/ variable (l) /-- A continuous function is box-integrable with respect to any locally finite measure. This is true for any volume with bounded variation. -/ lemma integrable_of_continuous_on [complete_space E] {I : box ι} {f : ℝⁿ → E} (hc : continuous_on f I.Icc) (μ : measure ℝⁿ) [is_locally_finite_measure μ] : integrable.{u v v} I l f μ.to_box_additive.to_smul := begin have huc := I.is_compact_Icc.uniform_continuous_on_of_continuous hc, rw metric.uniform_continuous_on_iff_le at huc, refine integrable_iff_cauchy_basis.2 (λ ε ε0, _), rcases exists_pos_mul_lt ε0 (μ.to_box_additive I) with ⟨ε', ε0', hε⟩, rcases huc ε' ε0' with ⟨δ, δ0 : 0 < δ, Hδ⟩, refine ⟨λ _ _, ⟨δ / 2, half_pos δ0⟩, λ _ _ _, rfl, λ c₁ c₂ π₁ π₂ h₁ h₁p h₂ h₂p, _⟩, simp only [dist_eq_norm, integral_sum_sub_partitions _ _ h₁p h₂p, box_additive_map.to_smul_apply, ← smul_sub], have : ∀ J ∈ π₁.to_prepartition ⊓ π₂.to_prepartition, ∥μ.to_box_additive J • (f ((π₁.inf_prepartition π₂.to_prepartition).tag J) - f ((π₂.inf_prepartition π₁.to_prepartition).tag J))∥ ≤ μ.to_box_additive J * ε', { intros J hJ, have : 0 ≤ μ.to_box_additive J, from ennreal.to_real_nonneg, rw [norm_smul, real.norm_eq_abs, abs_of_nonneg this, ← dist_eq_norm], refine mul_le_mul_of_nonneg_left _ this, refine Hδ _ (tagged_prepartition.tag_mem_Icc _ _) _ (tagged_prepartition.tag_mem_Icc _ _) _, rw [← add_halves δ], refine (dist_triangle_left _ _ J.upper).trans (add_le_add (h₁.1 _ _ _) (h₂.1 _ _ _)), { exact prepartition.bUnion_index_mem _ hJ }, { exact box.le_iff_Icc.1 (prepartition.le_bUnion_index _ hJ) J.upper_mem_Icc }, { rw _root_.inf_comm at hJ, exact prepartition.bUnion_index_mem _ hJ }, { rw _root_.inf_comm at hJ, exact box.le_iff_Icc.1 (prepartition.le_bUnion_index _ hJ) J.upper_mem_Icc } }, refine (norm_sum_le_of_le _ this).trans _, rw [← finset.sum_mul, μ.to_box_additive.sum_partition_boxes le_top (h₁p.inf h₂p)], exact hε.le end variable {l} /-- This is an auxiliary lemma used to prove two statements at once. Use one of the next two lemmas instead. -/ lemma has_integral_of_bRiemann_eq_ff_of_forall_is_o (hl : l.bRiemann = ff) (B : ι →ᵇᵃ[I] ℝ) (hB0 : ∀ J, 0 ≤ B J) (g : ι →ᵇᵃ[I] F) (s : set ℝⁿ) (hs : s.countable) (hlH : s.nonempty → l.bHenstock = tt) (H₁ : ∀ (c : ℝ≥0) (x ∈ I.Icc ∩ s) (ε > (0 : ℝ)), ∃ δ > 0, ∀ J ≤ I, J.Icc ⊆ metric.closed_ball x δ → x ∈ J.Icc → (l.bDistortion → J.distortion ≤ c) → dist (vol J (f x)) (g J) ≤ ε) (H₂ : ∀ (c : ℝ≥0) (x ∈ I.Icc \ s) (ε > (0 : ℝ)), ∃ δ > 0, ∀ J ≤ I, J.Icc ⊆ metric.closed_ball x δ → (l.bHenstock → x ∈ J.Icc) → (l.bDistortion → J.distortion ≤ c) → dist (vol J (f x)) (g J) ≤ ε * B J) : has_integral I l f vol (g I) := begin /- We choose `r x` differently for `x ∈ s` and `x ∉ s`. For `x ∈ s`, we choose `εs` such that `∑' x : s, εs x < ε / 2 / 2 ^ #ι`, then choose `r x` so that `dist (vol J (f x)) (g J) ≤ εs x` for `J` in the `r x`-neighborhood of `x`. This guarantees that the sum of these distances over boxes `J` such that `π.tag J ∈ s` is less than `ε / 2`. We need an additional multiplier `2 ^ #ι` because different boxes can have the same tag. For `x ∉ s`, we choose `r x` so that `dist (vol (J (f x))) (g J) ≤ (ε / 2 / B I) * B J` for a box `J` in the `δ`-neighborhood of `x`. -/ refine ((l.has_basis_to_filter_Union_top _).tendsto_iff metric.nhds_basis_closed_ball).2 _, intros ε ε0, simp only [subtype.exists'] at H₁ H₂, choose! δ₁ Hδ₁ using H₁, choose! δ₂ Hδ₂ using H₂, have ε0' := half_pos ε0, have H0 : 0 < (2 ^ fintype.card ι : ℝ), from pow_pos zero_lt_two _, rcases hs.exists_pos_forall_sum_le (div_pos ε0' H0) with ⟨εs, hεs0, hεs⟩, simp only [le_div_iff' H0, mul_sum] at hεs, rcases exists_pos_mul_lt ε0' (B I) with ⟨ε', ε'0, hεI⟩, set δ : ℝ≥0 → ℝⁿ → Ioi (0 : ℝ) := λ c x, if x ∈ s then δ₁ c x (εs x) else (δ₂ c) x ε', refine ⟨δ, λ c, l.r_cond_of_bRiemann_eq_ff hl, _⟩, simp only [set.mem_Union, mem_inter_eq, mem_set_of_eq], rintro π ⟨c, hπδ, hπp⟩, /- Now we split the sum into two parts based on whether `π.tag J` belongs to `s` or not. -/ rw [← g.sum_partition_boxes le_rfl hπp, mem_closed_ball, integral_sum, ← sum_filter_add_sum_filter_not π.boxes (λ J, π.tag J ∈ s), ← sum_filter_add_sum_filter_not π.boxes (λ J, π.tag J ∈ s), ← add_halves ε], refine dist_add_add_le_of_le _ _, { unfreezingI { rcases s.eq_empty_or_nonempty with rfl|hsne }, { simp [ε0'.le] }, /- For the boxes such that `π.tag J ∈ s`, we use the fact that at most `2 ^ #ι` boxes have the same tag. -/ specialize hlH hsne, have : ∀ J ∈ π.boxes.filter (λ J, π.tag J ∈ s), dist (vol J (f $ π.tag J)) (g J) ≤ εs (π.tag J), { intros J hJ, rw finset.mem_filter at hJ, cases hJ with hJ hJs, refine Hδ₁ c _ ⟨π.tag_mem_Icc _, hJs⟩ _ (hεs0 _) _ (π.le_of_mem' _ hJ) _ (hπδ.2 hlH J hJ) (λ hD, (finset.le_sup hJ).trans (hπδ.3 hD)), convert hπδ.1 J hJ, exact (dif_pos hJs).symm }, refine (dist_sum_sum_le_of_le _ this).trans _, rw sum_comp, refine (sum_le_sum _).trans (hεs _ _), { rintro b -, rw [← nat.cast_two, ← nat.cast_pow, ← nsmul_eq_mul], refine nsmul_le_nsmul (hεs0 _).le _, refine (finset.card_le_of_subset _).trans ((hπδ.is_Henstock hlH).card_filter_tag_eq_le b), exact filter_subset_filter _ (filter_subset _ _) }, { rw [finset.coe_image, set.image_subset_iff], exact λ J hJ, (finset.mem_filter.1 hJ).2 } }, /- Now we deal with boxes such that `π.tag J ∉ s`. In this case the estimate is straightforward. -/ have H₂ : ∀ J ∈ π.boxes.filter (λ J, π.tag J ∉ s), dist (vol J (f $ π.tag J)) (g J) ≤ ε' * B J, { intros J hJ, rw finset.mem_filter at hJ, cases hJ with hJ hJs, refine Hδ₂ c _ ⟨π.tag_mem_Icc _, hJs⟩ _ ε'0 _ (π.le_of_mem' _ hJ) _ (λ hH, hπδ.2 hH J hJ) (λ hD, (finset.le_sup hJ).trans (hπδ.3 hD)), convert hπδ.1 J hJ, exact (dif_neg hJs).symm }, refine (dist_sum_sum_le_of_le _ H₂).trans ((sum_le_sum_of_subset_of_nonneg (filter_subset _ _) _).trans _), { exact λ _ _ _, mul_nonneg ε'0.le (hB0 _) }, { rw [← mul_sum, B.sum_partition_boxes le_rfl hπp, mul_comm], exact hεI.le } end /-- A function `f` has Henstock (or `⊥`) integral over `I` is equal to the value of a box-additive function `g` on `I` provided that `vol J (f x)` is sufficiently close to `g J` for sufficiently small boxes `J ∋ x`. This lemma is useful to prove, e.g., to prove the Divergence theorem for integral along `⊥`. Let `l` be either `box_integral.integration_params.Henstock` or `⊥`. Let `g` a box-additive function on subboxes of `I`. Suppose that there exists a nonnegative box-additive function `B` and a countable set `s` with the following property. For every `c : ℝ≥0`, a point `x ∈ I.Icc`, and a positive `ε` there exists `δ > 0` such that for any box `J ≤ I` such that - `x ∈ J.Icc ⊆ metric.closed_ball x δ`; - if `l.bDistortion` (i.e., `l = ⊥`), then the distortion of `J` is less than or equal to `c`, the distance between the term `vol J (f x)` of an integral sum corresponding to `J` and `g J` is less than or equal to `ε` if `x ∈ s` and is less than or equal to `ε * B J` otherwise. Then `f` is integrable on `I along `l` with integral `g I`. -/ lemma has_integral_of_le_Henstock_of_forall_is_o (hl : l ≤ Henstock) (B : ι →ᵇᵃ[I] ℝ) (hB0 : ∀ J, 0 ≤ B J) (g : ι →ᵇᵃ[I] F) (s : set ℝⁿ) (hs : s.countable) (H₁ : ∀ (c : ℝ≥0) (x ∈ I.Icc ∩ s) (ε > (0 : ℝ)), ∃ δ > 0, ∀ J ≤ I, J.Icc ⊆ metric.closed_ball x δ → x ∈ J.Icc → (l.bDistortion → J.distortion ≤ c) → dist (vol J (f x)) (g J) ≤ ε) (H₂ : ∀ (c : ℝ≥0) (x ∈ I.Icc \ s) (ε > (0 : ℝ)), ∃ δ > 0, ∀ J ≤ I, J.Icc ⊆ metric.closed_ball x δ → x ∈ J.Icc → (l.bDistortion → J.distortion ≤ c) → dist (vol J (f x)) (g J) ≤ ε * B J) : has_integral I l f vol (g I) := have A : l.bHenstock, from hl.2.1.resolve_left dec_trivial, has_integral_of_bRiemann_eq_ff_of_forall_is_o (hl.1.resolve_right dec_trivial) B hB0 _ s hs (λ _, A) H₁ $ by simpa only [A, true_implies_iff] using H₂ /-- Suppose that there exists a nonnegative box-additive function `B` with the following property. For every `c : ℝ≥0`, a point `x ∈ I.Icc`, and a positive `ε` there exists `δ > 0` such that for any box `J ≤ I` such that - `J.Icc ⊆ metric.closed_ball x δ`; - if `l.bDistortion` (i.e., `l = ⊥`), then the distortion of `J` is less than or equal to `c`, the distance between the term `vol J (f x)` of an integral sum corresponding to `J` and `g J` is less than or equal to `ε * B J`. Then `f` is McShane integrable on `I` with integral `g I`. -/ lemma has_integral_McShane_of_forall_is_o (B : ι →ᵇᵃ[I] ℝ) (hB0 : ∀ J, 0 ≤ B J) (g : ι →ᵇᵃ[I] F) (H : ∀ (c : ℝ≥0) (x ∈ I.Icc) (ε > (0 : ℝ)), ∃ δ > 0, ∀ J ≤ I, J.Icc ⊆ metric.closed_ball x δ → dist (vol J (f x)) (g J) ≤ ε * B J) : has_integral I McShane f vol (g I) := has_integral_of_bRiemann_eq_ff_of_forall_is_o rfl B hB0 g ∅ countable_empty (λ ⟨x, hx⟩, hx.elim) (λ c x hx, hx.2.elim) $ by simpa only [McShane, coe_sort_ff, false_implies_iff, true_implies_iff, diff_empty] using H end box_integral
d04e8fe3d4f156e523f48872e6b30b9119f48faf
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/topology/category/Top/open_nhds.lean
8cb8dea87e060c1f34154b9f23d2d4c8f2a04bd9
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,324
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.category.Top.opens import category_theory.filtered open category_theory open topological_space open opposite universe u variables {X Y : Top.{u}} (f : X ⟶ Y) namespace topological_space /-- The type of open neighbourhoods of a point `x` in a (bundled) topological space. -/ def open_nhds (x : X) := { U : opens X // x ∈ U } namespace open_nhds instance (x : X) : partial_order (open_nhds x) := { le := λ U V, U.1 ≤ V.1, le_refl := λ _, le_refl _, le_trans := λ _ _ _, le_trans, le_antisymm := λ _ _ i j, subtype.eq $ le_antisymm i j } instance (x : X) : lattice (open_nhds x) := { inf := λ U V, ⟨U.1 ⊓ V.1, ⟨U.2, V.2⟩⟩, le_inf := λ U V W, @le_inf _ _ U.1.1 V.1.1 W.1.1, inf_le_left := λ U V, @inf_le_left _ _ U.1.1 V.1.1, inf_le_right := λ U V, @inf_le_right _ _ U.1.1 V.1.1, sup := λ U V, ⟨U.1 ⊔ V.1, V.1.1.mem_union_left U.2⟩, sup_le := λ U V W, @sup_le _ _ U.1.1 V.1.1 W.1.1, le_sup_left := λ U V, @le_sup_left _ _ U.1.1 V.1.1, le_sup_right := λ U V, @le_sup_right _ _ U.1.1 V.1.1, ..open_nhds.partial_order x } instance (x : X) : order_top (open_nhds x) := { top := ⟨⊤, trivial⟩, le_top := λ U, @le_top _ _ U.1.1, ..open_nhds.partial_order x } instance open_nhds_category (x : X) : category.{u} (open_nhds x) := by {unfold open_nhds, apply_instance} instance opens_nhds_hom_has_coe_to_fun {x : X} {U V : open_nhds x} : has_coe_to_fun (U ⟶ V) (λ _, U.1 → V.1) := ⟨λ f x, ⟨x, f.le x.2⟩⟩ /-- The inclusion `U ⊓ V ⟶ U` as a morphism in the category of open sets. -/ def inf_le_left {x : X} (U V : open_nhds x) : U ⊓ V ⟶ U := hom_of_le inf_le_left /-- The inclusion `U ⊓ V ⟶ V` as a morphism in the category of open sets. -/ def inf_le_right {x : X} (U V : open_nhds x) : U ⊓ V ⟶ V := hom_of_le inf_le_right /-- The inclusion functor from open neighbourhoods of `x` to open sets in the ambient topological space. -/ def inclusion (x : X) : open_nhds x ⥤ opens X := full_subcategory_inclusion _ @[simp] lemma inclusion_obj (x : X) (U) (p) : (inclusion x).obj ⟨U,p⟩ = U := rfl lemma open_embedding {x : X} (U : open_nhds x) : open_embedding (U.1.inclusion) := U.1.open_embedding def map (x : X) : open_nhds (f x) ⥤ open_nhds x := { obj := λ U, ⟨(opens.map f).obj U.1, by tidy⟩, map := λ U V i, (opens.map f).map i } @[simp] lemma map_obj (x : X) (U) (q) : (map f x).obj ⟨U, q⟩ = ⟨(opens.map f).obj U, by tidy⟩ := rfl @[simp] lemma map_id_obj (x : X) (U) : (map (𝟙 X) x).obj U = U := by tidy @[simp] lemma map_id_obj' (x : X) (U) (p) (q) : (map (𝟙 X) x).obj ⟨⟨U, p⟩, q⟩ = ⟨⟨U, p⟩, q⟩ := rfl @[simp] lemma map_id_obj_unop (x : X) (U : (open_nhds x)ᵒᵖ) : (map (𝟙 X) x).obj (unop U) = unop U := by simp @[simp] lemma op_map_id_obj (x : X) (U : (open_nhds x)ᵒᵖ) : (map (𝟙 X) x).op.obj U = U := by simp /-- `opens.map f` and `open_nhds.map f` form a commuting square (up to natural isomorphism) with the inclusion functors into `opens X`. -/ def inclusion_map_iso (x : X) : inclusion (f x) ⋙ opens.map f ≅ map f x ⋙ inclusion x := nat_iso.of_components (λ U, begin split, exact 𝟙 _, exact 𝟙 _ end) (by tidy) @[simp] lemma inclusion_map_iso_hom (x : X) : (inclusion_map_iso f x).hom = 𝟙 _ := rfl @[simp] lemma inclusion_map_iso_inv (x : X) : (inclusion_map_iso f x).inv = 𝟙 _ := rfl end open_nhds end topological_space namespace is_open_map open topological_space variables {f} /-- An open map `f : X ⟶ Y` induces a functor `open_nhds x ⥤ open_nhds (f x)`. -/ @[simps] def functor_nhds (h : is_open_map f) (x : X) : open_nhds x ⥤ open_nhds (f x) := { obj := λ U, ⟨h.functor.obj U.1, ⟨x, U.2, rfl⟩⟩, map := λ U V i, h.functor.map i } /-- An open map `f : X ⟶ Y` induces an adjunction between `open_nhds x` and `open_nhds (f x)`. -/ def adjunction_nhds (h : is_open_map f) (x : X) : is_open_map.functor_nhds h x ⊣ open_nhds.map f x := adjunction.mk_of_unit_counit { unit := { app := λ U, hom_of_le $ λ x hxU, ⟨x, hxU, rfl⟩ }, counit := { app := λ V, hom_of_le $ λ y ⟨x, hfxV, hxy⟩, hxy ▸ hfxV } } end is_open_map
6e7240bab90acd8ce6fc4bfb129081a418d12d4e
b074a51e20fdb737b2d4c635dd292fc54685e010
/src/category_theory/monad/limits.lean
92777b718c9494d4451a447fc1ec3b4c92466659
[ "Apache-2.0" ]
permissive
minchaowu/mathlib
2daf6ffdb5a56eeca403e894af88bcaaf65aec5e
879da1cf04c2baa9eaa7bd2472100bc0335e5c73
refs/heads/master
1,609,628,676,768
1,564,310,105,000
1,564,310,105,000
99,461,307
0
0
null
null
null
null
UTF-8
Lean
false
false
4,693
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.monad.adjunction import category_theory.adjunction.limits namespace category_theory open category open category_theory.limits universes v₁ v₂ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation namespace monad variables {C : Type u₁} [𝒞 : category.{v₁+1} C] include 𝒞 variables {T : C ⥤ C} [monad.{v₁+1} T] variables {J : Type v₁} [𝒥 : small_category J] include 𝒥 namespace forget_creates_limits variables (D : J ⥤ algebra T) [has_limit.{v₁} (D ⋙ forget T)] def γ : (D ⋙ forget T ⋙ T) ⟶ (D ⋙ forget T) := { app := λ j, (D.obj j).a } @[simp] lemma γ_app (j) : (γ D).app j = (D.obj j).a := rfl def c : cone (D ⋙ forget T) := { X := T.obj (limit (D ⋙ forget T)), π := (functor.const_comp _ _ T).inv ≫ whisker_right (limit.cone (D ⋙ forget T)).π T ≫ (γ D) } @[simp] lemma c_π (j) : (c D).π.app j = 𝟙 _ ≫ T.map (limit.π (D ⋙ forget T) j) ≫ (D.obj j).a := rfl def cone_point (D : J ⥤ algebra T) [has_limit.{v₁} (D ⋙ forget T)] : algebra T := { A := limit (D ⋙ forget T), a := limit.lift _ (c D), unit' := begin ext1, rw [category.assoc, limit.lift_π], dsimp, erw [id_comp, ←category.assoc, ←nat_trans.naturality, id_comp, category.assoc, algebra.unit, comp_id], refl, end, assoc' := begin ext1, dsimp, simp only [limit.lift_π, γ_app, c_π, limit.cone_π, id_comp, functor.const_comp, whisker_right.app, nat_trans.comp_app, category.assoc], conv { to_rhs, rw [←category.assoc, ←T.map_comp, limit.lift_π], dsimp [c], rw [id_comp], }, conv { to_lhs, rw [←category.assoc, ←nat_trans.naturality, category.assoc], erw [algebra.assoc (D.obj j), ←category.assoc, ←T.map_comp], }, end } @[simp] lemma cone_point_a (D : J ⥤ algebra T) [has_limit.{v₁} (D ⋙ forget T)] : (cone_point D).a = limit.lift _ ( let μ := limit.cone (D ⋙ forget T) in { X := T.obj μ.X, π := (functor.const_comp _ _ T).inv ≫ whisker_right μ.π T ≫ (γ D) }) := rfl end forget_creates_limits -- Theorem 5.6.5 from Riehl def forget_creates_limits (D : J ⥤ algebra T) [has_limit.{v₁} (D ⋙ forget T)] : has_limit D := { cone := { X := forget_creates_limits.cone_point D, π := { app := λ j, { f := limit.π (D ⋙ forget T) j }, naturality' := λ X Y f, by { ext, dsimp, erw [id_comp, limit.w] } } }, is_limit := { lift := λ s, { f := limit.lift _ ((forget T).map_cone s), h' := begin ext, dsimp, simp only [limit.lift_π, limit.cone_π, forget_map, id_comp, functor.const_comp, whisker_right.app, nat_trans.comp_app, category.assoc, functor.map_cone_π], dsimp, rw [id_comp, ←category.assoc, ←T.map_comp], simp only [limit.lift_π, monad.forget_map, algebra.hom.h, functor.map_cone_π], end }, uniq' := λ s m w, by { ext1, ext1, simpa using congr_arg algebra.hom.f (w j) } } } end monad variables {C : Type u₁} [𝒞 : category.{v₁+1} C] {D : Type u₁} [𝒟 : category.{v₁+1} D] include 𝒞 𝒟 variables {J : Type v₁} [𝒥 : small_category J] include 𝒥 instance comp_comparison_forget_has_limit (F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit.{v₁} (F ⋙ R)] : has_limit ((F ⋙ monad.comparison R) ⋙ monad.forget ((left_adjoint R) ⋙ R)) := (@has_limit_of_iso _ _ _ _ (F ⋙ R) _ _ (iso_whisker_left F (monad.comparison_forget R).symm)) instance comp_comparison_has_limit (F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit.{v₁} (F ⋙ R)] : has_limit (F ⋙ monad.comparison R) := monad.forget_creates_limits (F ⋙ monad.comparison R) def monadic_creates_limits (F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit.{v₁} (F ⋙ R)] : has_limit F := adjunction.has_limit_of_comp_equivalence _ (monad.comparison R) omit 𝒥 section def has_limits_of_reflective (R : D ⥤ C) [reflective R] [has_limits.{v₁} C] : has_limits.{v₁} D := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, monadic_creates_limits F R } } local attribute [instance] has_limits_of_reflective include 𝒥 -- We verify that, even jumping through these monadic hoops, -- the limit is actually calculated in the obvious way: example (R : D ⥤ C) [reflective R] [has_limits.{v₁} C] (F : J ⥤ D) : limit F = (left_adjoint R).obj (limit (F ⋙ R)) := rfl end end category_theory
4f6835a0aaf9dffd46497007cf3b0cd00dbb6f4f
63abd62053d479eae5abf4951554e1064a4c45b4
/src/algebra/category/CommRing/limits.lean
6632762e57366a5250dd2e9c37c504c135cb9672
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
14,540
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.ring.pi import algebra.category.CommRing.basic import algebra.category.Group.limits import ring_theory.subring /-! # The category of (commutative) rings has all limits Further, these limits are preserved by the forgetful functor --- that is, the underlying types are just the limits in the category of types. -/ open category_theory open category_theory.limits universe u noncomputable theory namespace SemiRing variables {J : Type u} [small_category J] instance semiring_obj (F : J ⥤ SemiRing) (j) : semiring ((F ⋙ forget SemiRing).obj j) := by { change semiring (F.obj j), apply_instance } /-- The flat sections of a functor into `SemiRing` form a subsemiring of all sections. -/ def sections_subsemiring (F : J ⥤ SemiRing) : subsemiring (Π j, F.obj j) := { carrier := (F ⋙ forget SemiRing).sections, ..(AddMon.sections_add_submonoid (F ⋙ forget₂ SemiRing AddCommMon ⋙ forget₂ AddCommMon AddMon)), ..(Mon.sections_submonoid (F ⋙ forget₂ SemiRing Mon)) } instance limit_semiring (F : J ⥤ SemiRing) : semiring (types.limit_cone (F ⋙ forget SemiRing.{u})).X := (sections_subsemiring F).to_semiring /-- `limit.π (F ⋙ forget SemiRing) j` as a `ring_hom`. -/ def limit_π_ring_hom (F : J ⥤ SemiRing.{u}) (j) : (types.limit_cone (F ⋙ forget SemiRing)).X →+* (F ⋙ forget SemiRing).obj j := { to_fun := (types.limit_cone (F ⋙ forget SemiRing)).π.app j, ..AddMon.limit_π_add_monoid_hom (F ⋙ forget₂ SemiRing AddCommMon.{u} ⋙ forget₂ AddCommMon AddMon) j, ..Mon.limit_π_monoid_hom (F ⋙ forget₂ SemiRing Mon) j, } namespace has_limits -- The next two definitions are used in the construction of `has_limits SemiRing`. -- After that, the limits should be constructed using the generic limits API, -- e.g. `limit F`, `limit.cone F`, and `limit.is_limit F`. /-- Construction of a limit cone in `SemiRing`. (Internal use only; use the limits API.) -/ def limit_cone (F : J ⥤ SemiRing) : cone F := { X := SemiRing.of (types.limit_cone (F ⋙ forget _)).X, π := { app := limit_π_ring_hom F, naturality' := λ j j' f, ring_hom.coe_inj ((types.limit_cone (F ⋙ forget _)).π.naturality f) } } /-- Witness that the limit cone in `SemiRing` is a limit cone. (Internal use only; use the limits API.) -/ def limit_cone_is_limit (F : J ⥤ SemiRing) : is_limit (limit_cone F) := begin refine is_limit.of_faithful (forget SemiRing) (types.limit_cone_is_limit _) (λ s, ⟨_, _, _, _, _⟩) (λ s, rfl); tidy end end has_limits open has_limits /-- The category of rings has all limits. -/ @[irreducible] instance has_limits : has_limits SemiRing := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, has_limit.mk { cone := limit_cone F, is_limit := limit_cone_is_limit F } } } /-- An auxiliary declaration to speed up typechecking. -/ def forget₂_AddCommMon_preserves_limits_aux (F : J ⥤ SemiRing) : is_limit ((forget₂ SemiRing AddCommMon).map_cone (limit_cone F)) := AddCommMon.limit_cone_is_limit (F ⋙ forget₂ SemiRing AddCommMon) /-- The forgetful functor from semirings to additive commutative monoids preserves all limits. -/ instance forget₂_AddCommMon_preserves_limits : preserves_limits (forget₂ SemiRing AddCommMon) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, preserves_limit_of_preserves_limit_cone (limit_cone_is_limit F) (forget₂_AddCommMon_preserves_limits_aux F) } } /-- An auxiliary declaration to speed up typechecking. -/ def forget₂_Mon_preserves_limits_aux (F : J ⥤ SemiRing) : is_limit ((forget₂ SemiRing Mon).map_cone (limit_cone F)) := Mon.has_limits.limit_cone_is_limit (F ⋙ forget₂ SemiRing Mon) /-- The forgetful functor from semirings to monoids preserves all limits. -/ instance forget₂_Mon_preserves_limits : preserves_limits (forget₂ SemiRing Mon) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, preserves_limit_of_preserves_limit_cone (limit_cone_is_limit F) (forget₂_Mon_preserves_limits_aux F) } } /-- The forgetful functor from semirings to types preserves all limits. -/ instance forget_preserves_limits : preserves_limits (forget SemiRing) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, preserves_limit_of_preserves_limit_cone (limit_cone_is_limit F) (types.limit_cone_is_limit (F ⋙ forget _)) } } end SemiRing namespace CommSemiRing variables {J : Type u} [small_category J] instance comm_semiring_obj (F : J ⥤ CommSemiRing) (j) : comm_semiring ((F ⋙ forget CommSemiRing).obj j) := by { change comm_semiring (F.obj j), apply_instance } instance limit_comm_semiring (F : J ⥤ CommSemiRing) : comm_semiring (types.limit_cone (F ⋙ forget CommSemiRing.{u})).X := @subsemiring.to_comm_semiring (Π j, F.obj j) _ (SemiRing.sections_subsemiring (F ⋙ forget₂ CommSemiRing SemiRing.{u})) /-- We show that the forgetful functor `CommSemiRing ⥤ SemiRing` creates limits. All we need to do is notice that the limit point has a `comm_semiring` instance available, and then reuse the existing limit. -/ instance (F : J ⥤ CommSemiRing) : creates_limit F (forget₂ CommSemiRing SemiRing.{u}) := creates_limit_of_reflects_iso (λ c' t, { lifted_cone := { X := CommSemiRing.of (types.limit_cone (F ⋙ forget _)).X, π := { app := SemiRing.limit_π_ring_hom (F ⋙ forget₂ CommSemiRing SemiRing), naturality' := (SemiRing.has_limits.limit_cone (F ⋙ forget₂ _ _)).π.naturality, } }, valid_lift := is_limit.unique_up_to_iso (SemiRing.has_limits.limit_cone_is_limit _) t, makes_limit := is_limit.of_faithful (forget₂ CommSemiRing SemiRing.{u}) (SemiRing.has_limits.limit_cone_is_limit _) (λ s, _) (λ s, rfl) }) /-- A choice of limit cone for a functor into `CommSemiRing`. (Generally, you'll just want to use `limit F`.) -/ def limit_cone (F : J ⥤ CommSemiRing) : cone F := lift_limit (limit.is_limit (F ⋙ (forget₂ CommSemiRing SemiRing.{u}))) /-- The chosen cone is a limit cone. (Generally, you'll just want to use `limit.cone F`.) -/ def limit_cone_is_limit (F : J ⥤ CommSemiRing) : is_limit (limit_cone F) := lifted_limit_is_limit _ /-- The category of rings has all limits. -/ @[irreducible] instance has_limits : has_limits CommSemiRing.{u} := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, has_limit_of_created F (forget₂ CommSemiRing SemiRing.{u}) } } /-- The forgetful functor from rings to semirings preserves all limits. -/ instance forget₂_SemiRing_preserves_limits : preserves_limits (forget₂ CommSemiRing SemiRing) := { preserves_limits_of_shape := λ J 𝒥, { preserves_limit := λ F, by apply_instance } } /-- The forgetful functor from rings to types preserves all limits. (That is, the underlying types could have been computed instead as limits in the category of types.) -/ instance forget_preserves_limits : preserves_limits (forget CommSemiRing) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, limits.comp_preserves_limit (forget₂ CommSemiRing SemiRing) (forget SemiRing) } } end CommSemiRing namespace Ring variables {J : Type u} [small_category J] instance ring_obj (F : J ⥤ Ring) (j) : ring ((F ⋙ forget Ring).obj j) := by { change ring (F.obj j), apply_instance } /-- The flat sections of a functor into `Ring` form a subring of all sections. -/ def sections_subring (F : J ⥤ Ring) : subring (Π j, F.obj j) := { carrier := (F ⋙ forget Ring).sections, ..(AddGroup.sections_add_subgroup (F ⋙ forget₂ Ring AddCommGroup ⋙ forget₂ AddCommGroup AddGroup)), ..(SemiRing.sections_subsemiring (F ⋙ forget₂ Ring SemiRing)) } instance limit_ring (F : J ⥤ Ring) : ring (types.limit_cone (F ⋙ forget Ring.{u})).X := (sections_subring F).to_ring /-- We show that the forgetful functor `CommRing ⥤ Ring` creates limits. All we need to do is notice that the limit point has a `ring` instance available, and then reuse the existing limit. -/ instance (F : J ⥤ Ring) : creates_limit F (forget₂ Ring SemiRing.{u}) := creates_limit_of_reflects_iso (λ c' t, { lifted_cone := { X := Ring.of (types.limit_cone (F ⋙ forget _)).X, π := { app := SemiRing.limit_π_ring_hom (F ⋙ forget₂ Ring SemiRing), naturality' := (SemiRing.has_limits.limit_cone (F ⋙ forget₂ _ _)).π.naturality, } }, valid_lift := is_limit.unique_up_to_iso (SemiRing.has_limits.limit_cone_is_limit _) t, makes_limit := is_limit.of_faithful (forget₂ Ring SemiRing.{u}) (SemiRing.has_limits.limit_cone_is_limit _) (λ s, _) (λ s, rfl) }) /-- A choice of limit cone for a functor into `Ring`. (Generally, you'll just want to use `limit F`.) -/ def limit_cone (F : J ⥤ Ring) : cone F := lift_limit (limit.is_limit (F ⋙ (forget₂ Ring SemiRing.{u}))) /-- The chosen cone is a limit cone. (Generally, you'll just want to use `limit.cone F`.) -/ def limit_cone_is_limit (F : J ⥤ Ring) : is_limit (limit_cone F) := lifted_limit_is_limit _ /-- The category of rings has all limits. -/ @[irreducible] instance has_limits : has_limits Ring := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, has_limit_of_created F (forget₂ Ring SemiRing) } } /-- The forgetful functor from rings to semirings preserves all limits. -/ instance forget₂_SemiRing_preserves_limits : preserves_limits (forget₂ Ring SemiRing) := { preserves_limits_of_shape := λ J 𝒥, { preserves_limit := λ F, by apply_instance } } /-- An auxiliary declaration to speed up typechecking. -/ def forget₂_AddCommGroup_preserves_limits_aux (F : J ⥤ Ring) : is_limit ((forget₂ Ring AddCommGroup).map_cone (limit_cone F)) := AddCommGroup.limit_cone_is_limit (F ⋙ forget₂ Ring AddCommGroup) /-- The forgetful functor from rings to additive commutative groups preserves all limits. -/ instance forget₂_AddCommGroup_preserves_limits : preserves_limits (forget₂ Ring AddCommGroup) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, preserves_limit_of_preserves_limit_cone (limit_cone_is_limit F) (forget₂_AddCommGroup_preserves_limits_aux F) } } /-- The forgetful functor from rings to types preserves all limits. (That is, the underlying types could have been computed instead as limits in the category of types.) -/ instance forget_preserves_limits : preserves_limits (forget Ring) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, limits.comp_preserves_limit (forget₂ Ring SemiRing) (forget SemiRing) } } end Ring namespace CommRing variables {J : Type u} [small_category J] instance comm_ring_obj (F : J ⥤ CommRing) (j) : comm_ring ((F ⋙ forget CommRing).obj j) := by { change comm_ring (F.obj j), apply_instance } instance limit_comm_ring (F : J ⥤ CommRing) : comm_ring (types.limit_cone (F ⋙ forget CommRing.{u})).X := @subring.to_comm_ring (Π j, F.obj j) _ (Ring.sections_subring (F ⋙ forget₂ CommRing Ring.{u})) /-- We show that the forgetful functor `CommRing ⥤ Ring` creates limits. All we need to do is notice that the limit point has a `comm_ring` instance available, and then reuse the existing limit. -/ instance (F : J ⥤ CommRing) : creates_limit F (forget₂ CommRing Ring.{u}) := /- A terse solution here would be ``` creates_limit_of_fully_faithful_of_iso (CommRing.of (limit (F ⋙ forget _))) (iso.refl _) ``` but it seems this would introduce additional identity morphisms in `limit.π`. -/ creates_limit_of_reflects_iso (λ c' t, { lifted_cone := { X := CommRing.of (types.limit_cone (F ⋙ forget _)).X, π := { app := SemiRing.limit_π_ring_hom (F ⋙ forget₂ CommRing Ring.{u} ⋙ forget₂ Ring SemiRing), naturality' := (SemiRing.has_limits.limit_cone (F ⋙ forget₂ _ _ ⋙ forget₂ _ _)).π.naturality, } }, valid_lift := is_limit.unique_up_to_iso (Ring.limit_cone_is_limit _) t, makes_limit := is_limit.of_faithful (forget₂ CommRing Ring.{u}) (Ring.limit_cone_is_limit _) (λ s, _) (λ s, rfl) }) /-- A choice of limit cone for a functor into `CommRing`. (Generally, you'll just want to use `limit F`.) -/ def limit_cone (F : J ⥤ CommRing) : cone F := lift_limit (limit.is_limit (F ⋙ (forget₂ CommRing Ring.{u}))) /-- The chosen cone is a limit cone. (Generally, you'll just want to use `limit.cone F`.) -/ def limit_cone_is_limit (F : J ⥤ CommRing) : is_limit (limit_cone F) := lifted_limit_is_limit _ /-- The category of commutative rings has all limits. -/ @[irreducible] instance has_limits : has_limits CommRing.{u} := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, has_limit_of_created F (forget₂ CommRing Ring.{u}) } } /-- The forgetful functor from commutative rings to rings preserves all limits. (That is, the underlying rings could have been computed instead as limits in the category of rings.) -/ instance forget₂_Ring_preserves_limits : preserves_limits (forget₂ CommRing Ring) := { preserves_limits_of_shape := λ J 𝒥, { preserves_limit := λ F, by apply_instance } } /-- An auxiliary declaration to speed up typechecking. -/ def forget₂_CommSemiRing_preserves_limits_aux (F : J ⥤ CommRing) : is_limit ((forget₂ CommRing CommSemiRing).map_cone (limit_cone F)) := CommSemiRing.limit_cone_is_limit (F ⋙ forget₂ CommRing CommSemiRing) /-- The forgetful functor from commutative rings to commutative semirings preserves all limits. (That is, the underlying commutative semirings could have been computed instead as limits in the category of commutative semirings.) -/ instance forget₂_CommSemiRing_preserves_limits : preserves_limits (forget₂ CommRing CommSemiRing) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, preserves_limit_of_preserves_limit_cone (limit_cone_is_limit F) (forget₂_CommSemiRing_preserves_limits_aux F) } } /-- The forgetful functor from commutative rings to types preserves all limits. (That is, the underlying types could have been computed instead as limits in the category of types.) -/ instance forget_preserves_limits : preserves_limits (forget CommRing) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, limits.comp_preserves_limit (forget₂ CommRing Ring) (forget Ring) } } end CommRing
e8e31e5b02ee4145c45fb615ebc2bf7fe5aab6ea
271e26e338b0c14544a889c31c30b39c989f2e0f
/src/Init/Lean/Meta/AppBuilder.lean
dbe8b2dd30f3b0cf5d45f0defd43b77af014573b
[ "Apache-2.0" ]
permissive
dgorokho/lean4
805f99b0b60c545b64ac34ab8237a8504f89d7d4
e949a052bad59b1c7b54a82d24d516a656487d8a
refs/heads/master
1,607,061,363,851
1,578,006,086,000
1,578,006,086,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,398
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 Init.Lean.Meta.SynthInstance namespace Lean @[inline] def Expr.eq? (p : Expr) : Option (Expr × Expr × Expr) := if p.isAppOfArity `Eq 3 then some (p.getArg! 0, p.getArg! 1, p.getArg! 2) else none @[inline] def Expr.heq? (p : Expr) : Option (Expr × Expr × Expr × Expr) := if p.isAppOfArity `HEq 4 then some (p.getArg! 0, p.getArg! 1, p.getArg! 2, p.getArg! 4) else none @[inline] def Expr.arrow? : Expr → Option (Expr × Expr) | Expr.forallE _ α β _ => if β.hasLooseBVars then none else some (α, β) | _ => none namespace Meta def mkEq (a b : Expr) : MetaM Expr := do aType ← inferType a; u ← getLevel aType; pure $ mkApp3 (mkConst `Eq [u]) aType a b def mkHEq (a b : Expr) : MetaM Expr := do aType ← inferType a; bType ← inferType b; u ← getLevel aType; pure $ mkApp4 (mkConst `HEq [u]) aType a bType b def mkEqRefl (a : Expr) : MetaM Expr := do aType ← inferType a; u ← getLevel aType; pure $ mkApp2 (mkConst `Eq.refl [u]) aType a def mkHEqRefl (a : Expr) : MetaM Expr := do aType ← inferType a; u ← getLevel aType; pure $ mkApp2 (mkConst `HEq.refl [u]) aType a private def infer (h : Expr) : MetaM Expr := do hType ← inferType h; whnfD hType def mkEqSymm (h : Expr) : MetaM Expr := if h.isAppOf `Eq.refl then pure h else do hType ← infer h; match hType.eq? with | some (α, a, b) => do u ← getLevel α; pure $ mkApp4 (mkConst `Eq.symm [u]) α a b h | none => throwEx $ Exception.appBuilder `Eq.symm "equality proof expected" #[h] def mkEqTrans (h₁ h₂ : Expr) : MetaM Expr := if h₁.isAppOf `Eq.refl then pure h₂ else if h₂.isAppOf `Eq.refl then pure h₁ else do hType₁ ← infer h₁; hType₂ ← infer h₂; match hType₁.eq?, hType₂.eq? with | some (α, a, b), some (_, _, c) => do u ← getLevel α; pure $ mkApp6 (mkConst `Eq.trans [u]) α a b c h₁ h₂ | _, _ => throwEx $ Exception.appBuilder `Eq.trans "equality proof expected" #[h₁, h₂] def mkHEqSymm (h : Expr) : MetaM Expr := if h.isAppOf `HEq.refl then pure h else do hType ← infer h; match hType.heq? with | some (α, a, β, b) => do u ← getLevel α; pure $ mkApp5 (mkConst `HEq.symm [u]) α β a b h | none => throwEx $ Exception.appBuilder `HEq.symm "heterogeneous equality proof expected" #[h] def mkHEqTrans (h₁ h₂ : Expr) : MetaM Expr := if h₁.isAppOf `HEq.refl then pure h₂ else if h₂.isAppOf `HEq.refl then pure h₁ else do hType₁ ← infer h₁; hType₂ ← infer h₂; match hType₁.heq?, hType₂.heq? with | some (α, a, β, b), some (_, _, γ, c) => do u ← getLevel α; pure $ mkApp8 (mkConst `HEq.trans [u]) α β γ a b c h₁ h₂ | _, _ => throwEx $ Exception.appBuilder `HEq.trans "heterogeneous equality proof expected" #[h₁, h₂] def mkCongrArg (f h : Expr) : MetaM Expr := do hType ← infer h; fType ← infer f; match fType.arrow?, hType.eq? with | some (α, β), some (_, a, b) => do u ← getLevel α; v ← getLevel β; pure $ mkApp6 (mkConst `congrArg [u, v]) α β a b f h | none, _ => throwEx $ Exception.appBuilder `congrArg "non-dependent function expected" #[f, h] | _, none => throwEx $ Exception.appBuilder `congrArg "equality proof expected" #[f, h] def mkCongrFun (h a : Expr) : MetaM Expr := do hType ← infer h; match hType.eq? with | some (ρ, f, g) => do ρ ← whnfD ρ; match ρ with | Expr.forallE n α β _ => do let β' := Lean.mkLambda n BinderInfo.default α β; u ← getLevel α; v ← getLevel (mkApp β' a); pure $ mkApp6 (mkConst `congrFun [u, v]) α β' f g h a | _ => throwEx $ Exception.appBuilder `congrFun "equality proof between functions expected" #[h, a] | _ => throwEx $ Exception.appBuilder `congrFun "equality proof expected" #[h, a] def mkCongr (h₁ h₂ : Expr) : MetaM Expr := do hType₁ ← infer h₁; hType₂ ← infer h₂; match hType₁.eq?, hType₂.eq? with | some (ρ, f, g), some (α, a, b) => do ρ ← whnfD ρ; match ρ.arrow? with | some (_, β) => do u ← getLevel α; v ← getLevel β; pure $ mkApp8 (mkConst `congr [u, v]) α β f g a b h₁ h₂ | _ => throwEx $ Exception.appBuilder `congr "non-dependent function expected" #[h₁, h₂] | _, _ => throwEx $ Exception.appBuilder `congr "equality proof expected" #[h₁, h₂] private def mkAppMFinal (f : Expr) (args : Array Expr) (instMVars : Array MVarId) : MetaM Expr := do instMVars.forM $ fun mvarId => do { mvarDecl ← getMVarDecl mvarId; mvarVal ← synthInstance mvarDecl.type; assignExprMVar mvarId mvarVal }; result ← instantiateMVars (mkAppN f args); whenM (hasAssignableMVar result) $ throwEx $ Exception.appBuilder `mkAppM "result contains metavariables" #[result]; pure result private partial def mkAppMAux (f : Expr) (xs : Array Expr) : Nat → Array Expr → Nat → Array MVarId → Expr → MetaM Expr | i, args, j, instMVars, Expr.forallE n d b c => do let d := d.instantiateRevRange j args.size args; match c.binderInfo with | BinderInfo.implicit => do mvar ← mkFreshExprMVar d n; mkAppMAux i (args.push mvar) j instMVars b | BinderInfo.instImplicit => do mvar ← mkFreshExprMVar d n MetavarKind.synthetic; mkAppMAux i (args.push mvar) j (instMVars.push mvar.mvarId!) b | _ => if h : i < xs.size then do let x := xs.get ⟨i, h⟩; xType ← inferType x; condM (isDefEq d xType) (mkAppMAux (i+1) (args.push x) j instMVars b) (throwEx $ Exception.appTypeMismatch (mkAppN f args) x) else mkAppMFinal f args instMVars | i, args, j, instMVars, type => do let type := type.instantiateRevRange j args.size args; type ← whnfD type; if type.isForall then mkAppMAux i args args.size instMVars type else if i == xs.size then mkAppMFinal f args instMVars else throwEx $ Exception.appBuilder `mkAppM "too many explicit arguments provided" (#[f] ++ xs) def mkAppM (constName : Name) (xs : Array Expr) : MetaM Expr := traceCtx `Meta.appBuilder $ withNewMCtxDepth $ do cinfo ← getConstInfo constName; us ← cinfo.lparams.mapM $ fun _ => mkFreshLevelMVar; let f := mkConst constName us; let fType := cinfo.instantiateTypeLevelParams us; mkAppMAux f xs 0 #[] 0 #[] fType end Meta end Lean
ea0032c3267d67dd1ea779f8dd7e39892ff9100c
c09f5945267fd905e23a77be83d9a78580e04a4a
/src/analysis/specific_limits.lean
7dec3fa50fded2e6369b35cfd96d1732f698e81e
[ "Apache-2.0" ]
permissive
OHIHIYA20/mathlib
023a6df35355b5b6eb931c404f7dd7535dccfa89
1ec0a1f49db97d45e8666a3bf33217ff79ca1d87
refs/heads/master
1,587,964,529,965
1,551,819,319,000
1,551,819,319,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,023
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 A collection of specific limit computations. -/ import analysis.normed_space.basic import topology.instances.ennreal noncomputable theory local attribute [instance] classical.prop_decidable open classical function lattice filter finset metric variables {α : Type*} {β : Type*} {ι : Type*} lemma has_sum_of_absolute_convergence_real {f : ℕ → ℝ} : (∃r, tendsto (λn, (range n).sum (λi, abs (f i))) at_top (nhds r)) → has_sum f | ⟨r, hr⟩ := begin refine has_sum_of_has_sum_norm ⟨r, (is_sum_iff_tendsto_nat_of_nonneg _ _).2 _⟩, exact assume i, norm_nonneg _, simpa only using hr end lemma tendsto_pow_at_top_at_top_of_gt_1 {r : ℝ} (h : r > 1) : tendsto (λn:ℕ, r ^ n) at_top at_top := tendsto_infi.2 $ assume p, tendsto_principal.2 $ let ⟨n, hn⟩ := exists_nat_gt (p / (r - 1)) in have hn_nn : (0:ℝ) ≤ n, from nat.cast_nonneg n, have r - 1 > 0, from sub_lt_iff_lt_add.mp $ by simp; assumption, have p ≤ r ^ n, from calc p = (p / (r - 1)) * (r - 1) : (div_mul_cancel _ $ ne_of_gt this).symm ... ≤ n * (r - 1) : mul_le_mul (le_of_lt hn) (le_refl _) (le_of_lt this) hn_nn ... ≤ 1 + n * (r - 1) : le_add_of_nonneg_of_le zero_le_one (le_refl _) ... = 1 + add_monoid.smul n (r - 1) : by rw [add_monoid.smul_eq_mul] ... ≤ (1 + (r - 1)) ^ n : pow_ge_one_add_mul (le_of_lt this) _ ... ≤ r ^ n : by simp; exact le_refl _, show {n | p ≤ r ^ n} ∈ at_top.sets, from mem_at_top_sets.mpr ⟨n, assume m hnm, le_trans this (pow_le_pow (le_of_lt h) hnm)⟩ lemma tendsto_inverse_at_top_nhds_0 : tendsto (λr:ℝ, r⁻¹) at_top (nhds 0) := tendsto_orderable_unbounded (no_top 0) (no_bot 0) $ assume l u hl hu, mem_at_top_sets.mpr ⟨u⁻¹ + 1, assume b hb, have u⁻¹ < b, from lt_of_lt_of_le (lt_add_of_pos_right _ zero_lt_one) hb, ⟨lt_trans hl $ inv_pos $ lt_trans (inv_pos hu) this, lt_of_one_div_lt_one_div hu $ begin rw [inv_eq_one_div], simp [-one_div_eq_inv, div_div_eq_mul_div, div_one], simp [this] end⟩⟩ lemma tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : tendsto (λn:ℕ, r^n) at_top (nhds 0) := by_cases (assume : r = 0, (tendsto_add_at_top_iff_nat 1).mp $ by simp [pow_succ, this, tendsto_const_nhds]) (assume : r ≠ 0, have tendsto (λn, (r⁻¹ ^ n)⁻¹) at_top (nhds 0), from (tendsto_pow_at_top_at_top_of_gt_1 $ one_lt_inv (lt_of_le_of_ne h₁ this.symm) h₂).comp tendsto_inverse_at_top_nhds_0, tendsto.congr' (univ_mem_sets' $ by simp *) this) lemma tendsto_pow_at_top_at_top_of_gt_1_nat {k : ℕ} (h : 1 < k) : tendsto (λn:ℕ, k ^ n) at_top at_top := tendsto_coe_nat_real_at_top_iff.1 $ have hr : 1 < (k : ℝ), by rw [← nat.cast_one, nat.cast_lt]; exact h, by simpa using tendsto_pow_at_top_at_top_of_gt_1 hr lemma tendsto_inverse_at_top_nhds_0_nat : tendsto (λ n : ℕ, (n : ℝ)⁻¹) at_top (nhds 0) := tendsto.comp (tendsto_coe_nat_real_at_top_iff.2 tendsto_id) tendsto_inverse_at_top_nhds_0 lemma tendsto_one_div_at_top_nhds_0_nat : tendsto (λ n : ℕ, 1/(n : ℝ)) at_top (nhds 0) := by simpa only [inv_eq_one_div] using tendsto_inverse_at_top_nhds_0_nat lemma tendsto_one_div_add_at_top_nhds_0_nat : tendsto (λ n : ℕ, 1 / ((n : ℝ) + 1)) at_top (nhds 0) := suffices tendsto (λ n : ℕ, 1 / (↑(n + 1) : ℝ)) at_top (nhds 0), by simpa, (tendsto_add_at_top_iff_nat 1).2 tendsto_one_div_at_top_nhds_0_nat lemma is_sum_geometric {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : is_sum (λn:ℕ, r ^ n) (1 / (1 - r)) := have r ≠ 1, from ne_of_lt h₂, have r + -1 ≠ 0, by rw [←sub_eq_add_neg, ne, sub_eq_iff_eq_add]; simp; assumption, have tendsto (λn, (r ^ n - 1) * (r - 1)⁻¹) at_top (nhds ((0 - 1) * (r - 1)⁻¹)), from tendsto_mul (tendsto_sub (tendsto_pow_at_top_nhds_0_of_lt_1 h₁ h₂) tendsto_const_nhds) tendsto_const_nhds, (is_sum_iff_tendsto_nat_of_nonneg (pow_nonneg h₁) _).mpr $ by simp [neg_inv, geom_sum, div_eq_mul_inv, *] at * lemma is_sum_geometric_two (a : ℝ) : is_sum (λn:ℕ, (a / 2) / 2 ^ n) a := begin convert is_sum_mul_left (a / 2) (is_sum_geometric (le_of_lt one_half_pos) one_half_lt_one), { funext n, simp, rw ← pow_inv; [refl, exact two_ne_zero] }, { norm_num, rw div_mul_cancel _ two_ne_zero } end def pos_sum_of_encodable {ε : ℝ} (hε : 0 < ε) (ι) [encodable ι] : {ε' : ι → ℝ // (∀ i, 0 < ε' i) ∧ ∃ c, is_sum ε' c ∧ c ≤ ε} := begin let f := λ n, (ε / 2) / 2 ^ n, have hf : is_sum f ε := is_sum_geometric_two _, have f0 : ∀ n, 0 < f n := λ n, div_pos (half_pos hε) (pow_pos two_pos _), refine ⟨f ∘ encodable.encode, λ i, f0 _, _⟩, rcases has_sum_comp_of_has_sum_of_injective f (has_sum_spec hf) (@encodable.encode_injective ι _) with ⟨c, hg⟩, refine ⟨c, hg, is_sum_le_inj _ (@encodable.encode_injective ι _) _ _ hg hf⟩, { assume i _, exact le_of_lt (f0 _) }, { assume n, exact le_refl _ } end lemma cauchy_seq_of_le_geometric [metric_space α] (r C : ℝ) (hr : r < 1) {f : ℕ → α} (hu : ∀n, dist (f n) (f (n+1)) ≤ C * r^n) : cauchy_seq f := begin refine cauchy_seq_of_has_sum_dist (has_sum_of_norm_bounded (λn, C * r^n) _ _), { by_cases h : C = 0, { simp [h, has_sum_zero] }, { have Cpos : C > 0, { have := le_trans dist_nonneg (hu 0), simp only [mul_one, pow_zero] at this, exact lt_of_le_of_ne this (ne.symm h) }, have rnonneg: r ≥ 0, { have := le_trans dist_nonneg (hu 1), simp only [pow_one] at this, exact nonneg_of_mul_nonneg_left this Cpos }, refine has_sum_mul_left C _, exact has_sum_spec (@is_sum_geometric r rnonneg hr) }}, show ∀n, abs (dist (f n) (f (n+1))) ≤ C * r^n, { assume n, rw abs_of_nonneg (dist_nonneg), exact hu n } end namespace nnreal theorem exists_pos_sum_of_encodable {ε : nnreal} (hε : 0 < ε) (ι) [encodable ι] : ∃ ε' : ι → nnreal, (∀ i, 0 < ε' i) ∧ ∃c, is_sum ε' c ∧ c < ε := let ⟨a, a0, aε⟩ := dense hε in let ⟨ε', hε', c, hc, hcε⟩ := pos_sum_of_encodable a0 ι in ⟨ λi, ⟨ε' i, le_of_lt $ hε' i⟩, assume i, nnreal.coe_lt.2 $ hε' i, ⟨c, is_sum_le (assume i, le_of_lt $ hε' i) is_sum_zero hc ⟩, nnreal.is_sum_coe.1 hc, lt_of_le_of_lt (nnreal.coe_le.1 hcε) aε ⟩ end nnreal namespace ennreal theorem exists_pos_sum_of_encodable {ε : ennreal} (hε : 0 < ε) (ι) [encodable ι] : ∃ ε' : ι → nnreal, (∀ i, 0 < ε' i) ∧ (∑ i, (ε' i : ennreal)) < ε := begin rcases dense hε with ⟨r, h0r, hrε⟩, rcases lt_iff_exists_coe.1 hrε with ⟨x, rfl, hx⟩, rcases nnreal.exists_pos_sum_of_encodable (coe_lt_coe.1 h0r) ι with ⟨ε', hp, c, hc, hcr⟩, exact ⟨ε', hp, (ennreal.tsum_coe_eq hc).symm ▸ lt_trans (coe_lt_coe.2 hcr) hrε⟩ end end ennreal
6189f7f6c21d9ac240383a1826cde7c14e1a2ec9
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/ref1.lean
7e5718a5a2a9a9a4cf9371448f358fc216415954
[ "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
599
lean
-- def inc (r : IO.Ref Nat) : IO Unit := do let v ← r.get; r.set (v+1); IO.println (">> " ++ toString v) def initArray (r : IO.Ref (Array Nat)) (n : Nat) : IO Unit := n.forM $ fun i => do r.modify $ fun a => a.push (2*i) def showArrayRef (r : IO.Ref (Array Nat)) : IO Unit := do let a ← r.swap ∅; a.size.forM (fun i => IO.println ("[" ++ toString i ++ "]: " ++ toString (a.get! i))); discard $ r.swap a; pure () def tst (n : Nat) : IO Unit := do let r₁ ← IO.mkRef 0; n.forM fun _ => inc r₁; let r₂ ← IO.mkRef (∅ : Array Nat); initArray r₂ n; showArrayRef r₂ #eval tst 10
3a33efdf23e1aa04c1e10015eda01f2dd532ea88
4727251e0cd73359b15b664c3170e5d754078599
/src/data/pi/interval.lean
4b4aace20a8b44e7fa75f5efa8b3eef7171c50cf
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
1,339
lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import data.finset.locally_finite import data.fintype.card /-! # Intervals in a pi type This file shows that (dependent) functions to locally finite orders equipped with the pointwise order are locally finite and calculates the cardinality of their intervals. -/ open finset fintype open_locale big_operators variables {ι : Type*} {α : ι → Type*} [decidable_eq ι] [fintype ι] [Π i, decidable_eq (α i)] [Π i, partial_order (α i)] [Π i, locally_finite_order (α i)] namespace pi instance : locally_finite_order (Π i, α i) := locally_finite_order.of_Icc _ (λ a b, pi_finset $ λ i, Icc (a i) (b i)) (λ a b x, by { simp_rw [mem_pi_finset, mem_Icc], exact forall_and_distrib }) variables (a b : Π i, α i) lemma card_Icc : (Icc a b).card = ∏ i, (Icc (a i) (b i)).card := card_pi_finset _ lemma card_Ico : (Ico a b).card = (∏ i, (Icc (a i) (b i)).card) - 1 := by rw [card_Ico_eq_card_Icc_sub_one, card_Icc] lemma card_Ioc : (Ioc a b).card = (∏ i, (Icc (a i) (b i)).card) - 1 := by rw [card_Ioc_eq_card_Icc_sub_one, card_Icc] lemma card_Ioo : (Ioo a b).card = (∏ i, (Icc (a i) (b i)).card) - 2 := by rw [card_Ioo_eq_card_Icc_sub_two, card_Icc] end pi
fa09908957dd450a47b70fc336ff776b698bf195
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/protected.lean
746f4d5c0bc41a92cd73b5037acb46c8e1536652
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
118
lean
import logic namespace foo protected definition C := true definition D := true end foo open foo check C check D
76b7a1ec01e0c24083f6d417d2581541bae6d796
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/convex/integral.lean
3c58c587d9e45a536844a21e885ea4bc15be4e85
[]
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,331
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury G. Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.convex.basic import Mathlib.measure_theory.set_integral import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-! # Jensen's inequality for integrals In this file we prove four theorems: * `convex.smul_integral_mem`: if `μ` is a non-zero finite measure on `α`, `s` is a convex closed set in `E`, and `f` is an integrable function sending `μ`-a.e. points to `s`, then the average value of `f` belongs to `s`: `(μ univ).to_real⁻¹ • ∫ x, f x ∂μ ∈ s`. See also `convex.center_mass_mem` for a finite sum version of this lemma. * `convex.integral_mem`: if `μ` is a probability measure on `α`, `s` is a convex closed set in `E`, and `f` is an integrable function sending `μ`-a.e. points to `s`, then the expected value of `f` belongs to `s`: `∫ x, f x ∂μ ∈ s`. See also `convex.sum_mem` for a finite sum version of this lemma. * `convex_on.map_smul_integral_le`: Jensen's inequality: if a function `g : E → ℝ` is convex and continuous on a convex closed set `s`, `μ` is a finite non-zero measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points to `s`, then the value of `g` at the average value of `f` is less than or equal to the average value of `g ∘ f` provided that both `f` and `g ∘ f` are integrable. See also `convex.map_center_mass_le` for a finite sum version of this lemma. * `convex_on.map_integral_le`: Jensen's inequality: if a function `g : E → ℝ` is convex and continuous on a convex closed set `s`, `μ` is a probability measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points to `s`, then the value of `g` at the expected value of `f` is less than or equal to the expected value of `g ∘ f` provided that both `f` and `g ∘ f` are integrable. See also `convex.map_sum_le` for a finite sum version of this lemma. ## Tags convex, integral, center mass, Jensen's inequality -/ /-- If `μ` is a non-zero finite measure on `α`, `s` is a convex closed set in `E`, and `f` is an integrable function sending `μ`-a.e. points to `s`, then the average value of `f` belongs to `s`: `(μ univ).to_real⁻¹ • ∫ x, f x ∂μ ∈ s`. See also `convex.center_mass_mem` for a finite sum version of this lemma. -/ theorem convex.smul_integral_mem {α : Type u_1} {E : Type u_2} [measurable_space α] {μ : measure_theory.measure α} [normed_group E] [normed_space ℝ E] [complete_space E] [topological_space.second_countable_topology E] [measurable_space E] [borel_space E] [measure_theory.finite_measure μ] {s : set E} (hs : convex s) (hsc : is_closed s) (hμ : μ ≠ 0) {f : α → E} (hfs : filter.eventually (fun (x : α) => f x ∈ s) (measure_theory.measure.ae μ)) (hfi : measure_theory.integrable f) : (ennreal.to_real (coe_fn μ set.univ)⁻¹ • measure_theory.integral μ fun (x : α) => f x) ∈ s := sorry /-- If `μ` is a probability measure on `α`, `s` is a convex closed set in `E`, and `f` is an integrable function sending `μ`-a.e. points to `s`, then the expected value of `f` belongs to `s`: `∫ x, f x ∂μ ∈ s`. See also `convex.sum_mem` for a finite sum version of this lemma. -/ theorem convex.integral_mem {α : Type u_1} {E : Type u_2} [measurable_space α] {μ : measure_theory.measure α} [normed_group E] [normed_space ℝ E] [complete_space E] [topological_space.second_countable_topology E] [measurable_space E] [borel_space E] [measure_theory.probability_measure μ] {s : set E} (hs : convex s) (hsc : is_closed s) {f : α → E} (hf : filter.eventually (fun (x : α) => f x ∈ s) (measure_theory.measure.ae μ)) (hfi : measure_theory.integrable f) : (measure_theory.integral μ fun (x : α) => f x) ∈ s := sorry /-- Jensen's inequality: if a function `g : E → ℝ` is convex and continuous on a convex closed set `s`, `μ` is a finite non-zero measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points to `s`, then the value of `g` at the average value of `f` is less than or equal to the average value of `g ∘ f` provided that both `f` and `g ∘ f` are integrable. See also `convex.map_center_mass_le` for a finite sum version of this lemma. -/ theorem convex_on.map_smul_integral_le {α : Type u_1} {E : Type u_2} [measurable_space α] {μ : measure_theory.measure α} [normed_group E] [normed_space ℝ E] [complete_space E] [topological_space.second_countable_topology E] [measurable_space E] [borel_space E] [measure_theory.finite_measure μ] {s : set E} {g : E → ℝ} (hg : convex_on s g) (hgc : continuous_on g s) (hsc : is_closed s) (hμ : μ ≠ 0) {f : α → E} (hfs : filter.eventually (fun (x : α) => f x ∈ s) (measure_theory.measure.ae μ)) (hfi : measure_theory.integrable f) (hgi : measure_theory.integrable (g ∘ f)) : g (ennreal.to_real (coe_fn μ set.univ)⁻¹ • measure_theory.integral μ fun (x : α) => f x) ≤ ennreal.to_real (coe_fn μ set.univ)⁻¹ • measure_theory.integral μ fun (x : α) => g (f x) := sorry /-- Jensen's inequality: if a function `g : E → ℝ` is convex and continuous on a convex closed set `s`, `μ` is a probability measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points to `s`, then the value of `g` at the expected value of `f` is less than or equal to the expected value of `g ∘ f` provided that both `f` and `g ∘ f` are integrable. See also `convex.map_sum_le` for a finite sum version of this lemma. -/ theorem convex_on.map_integral_le {α : Type u_1} {E : Type u_2} [measurable_space α] {μ : measure_theory.measure α} [normed_group E] [normed_space ℝ E] [complete_space E] [topological_space.second_countable_topology E] [measurable_space E] [borel_space E] [measure_theory.probability_measure μ] {s : set E} {g : E → ℝ} (hg : convex_on s g) (hgc : continuous_on g s) (hsc : is_closed s) {f : α → E} (hfs : filter.eventually (fun (x : α) => f x ∈ s) (measure_theory.measure.ae μ)) (hfi : measure_theory.integrable f) (hgi : measure_theory.integrable (g ∘ f)) : g (measure_theory.integral μ fun (x : α) => f x) ≤ measure_theory.integral μ fun (x : α) => g (f x) := sorry
b2230889a653b948b75b953c941fddbf30cf8169
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch2/ex0305.lean
85170a6a9b18b52a7e7f0233fcfb8cf32f08040d
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
125
lean
#check λ (α β : Type) (b : β) (x : α), x #check fun (α β γ : Type) (g: β → γ) (f : α → β) (x : α), g (f x)
eb56a02b7b95472be292244489d87955204aab66
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/ReducibilityAttrs.lean
84a05d74a934bd7fab1704f2a16eaddd8d98bd53
[ "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
1,889
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 namespace Lean inductive ReducibilityStatus where | reducible | semireducible | irreducible deriving Inhabited, Repr builtin_initialize reducibilityAttrs : EnumAttributes ReducibilityStatus ← registerEnumAttributes `reducibility [(`reducible, "reducible", ReducibilityStatus.reducible), (`semireducible, "semireducible", ReducibilityStatus.semireducible), (`irreducible, "irreducible", ReducibilityStatus.irreducible)] @[export lean_get_reducibility_status] def getReducibilityStatusImp (env : Environment) (declName : Name) : ReducibilityStatus := match reducibilityAttrs.getValue env declName with | some s => s | none => ReducibilityStatus.semireducible @[export lean_set_reducibility_status] def setReducibilityStatusImp (env : Environment) (declName : Name) (s : ReducibilityStatus) : Environment := match reducibilityAttrs.setValue env declName s with | Except.ok env => env | _ => env -- TODO(Leo): we should extend EnumAttributes.setValue in the future and ensure it never fails def getReducibilityStatus {m} [Monad m] [MonadEnv m] (declName : Name) : m ReducibilityStatus := do return getReducibilityStatusImp (← getEnv) declName def setReducibilityStatus {m} [Monad m] [MonadEnv m] (declName : Name) (s : ReducibilityStatus) : m Unit := do modifyEnv fun env => setReducibilityStatusImp env declName s def setReducibleAttribute {m} [Monad m] [MonadEnv m] (declName : Name) : m Unit := do setReducibilityStatus declName ReducibilityStatus.reducible def isReducible {m} [Monad m] [MonadEnv m] (declName : Name) : m Bool := do match (← getReducibilityStatus declName) with | ReducibilityStatus.reducible => true | _ => false end Lean
c07f41b1a8863bb243b92bedde7b60f39a61f2a7
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Meta/Closure.lean
68a1b8facd825ca0d5ba91a6fb6bc925b2b8d8d5
[ "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
14,482
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 Std.ShareCommon import Lean.MetavarContext import Lean.Environment import Lean.Util.FoldConsts import Lean.Meta.Basic import Lean.Meta.Check /- This module provides functions for "closing" open terms and creating auxiliary definitions. Here, we say a term is "open" if it contains free/meta-variables. The "closure" is performed by lambda abstracting the free/meta-variables. Recall that in dependent type theory lambda abstracting a let-variable may produce type incorrect terms. For example, given the context ```lean (n : Nat := 20) (x : Vector α n) (y : Vector α 20) ``` the term `x = y` is correct. However, its closure using lambda abstractions is not. ```lean fun (n : Nat) (x : Vector α n) (y : Vector α 20) => x = y ``` A previous version of this module would address this issue by always use let-expressions to abstract let-vars. In the example above, it would produce ```lean let n : Nat := 20; fun (x : Vector α n) (y : Vector α 20) => x = y ``` This approach produces correct result, but produces unsatisfactory results when we want to create auxiliary definitions. For example, consider the context ```lean (x : Nat) (y : Nat := fact x) ``` and the term `h (g y)`, now suppose we want to create an auxiliary definition for `y`. The previous version of this module would compute the auxiliary definition ```lean def aux := fun (x : Nat) => let y : Nat := fact x; h (g y) ``` and would return the term `aux x` as a substitute for `h (g y)`. This is correct, but we will re-evaluate `fact x` whenever we use `aux`. In this module, we produce ```lean def aux := fun (y : Nat) => h (g y) ``` Note that in this particular case, it is safe to lambda abstract the let-varible `y`. This module uses the following approach to decide whether it is safe or not to lambda abstract a let-variable. 1) We enable zeta-expansion tracking in `MetaM`. That is, whenever we perform type checking if a let-variable needs to zeta expanded, we store it in the set `zetaFVarIds`. We say a let-variable is zeta expanded when we replace it with its value. 2) We use the `MetaM` type checker `check` to type check the expression we want to close, and the type of the binders. 3) If a let-variable is not in `zetaFVarIds`, we lambda abstract it. Remark: We still use let-expressions for let-variables in `zetaFVarIds`, but we move the `let` inside the lambdas. The idea is to make sure the auxiliary definition does not have an interleaving of `lambda` and `let` expressions. Thus, if the let-variable occurs in the type of one of the lambdas, we simply zeta-expand it there. As a final example consider the context ```lean (x_1 : Nat) (x_2 : Nat) (x_3 : Nat) (x : Nat := fact (10 + x_1 + x_2 + x_3)) (ty : Type := Nat → Nat) (f : ty := fun x => x) (n : Nat := 20) (z : f 10) ``` and we use this module to compute an auxiliary definition for the term ```lean (let y : { v : Nat // v = n } := ⟨20, rfl⟩; y.1 + n + f x, z + 10) ``` we obtain ```lean def aux (x : Nat) (f : Nat → Nat) (z : Nat) : Nat×Nat := let n : Nat := 20; (let y : {v // v=n} := {val := 20, property := ex._proof_1}; y.val+n+f x, z+10) ``` BTW, this module also provides the `zeta : Bool` flag. When set to true, it expands all let-variables occurring in the target expression. -/ namespace Lean.Meta namespace Closure structure ToProcessElement where fvarId : FVarId newFVarId : FVarId deriving Inhabited structure Context where zeta : Bool structure State where visitedLevel : LevelMap Level := {} visitedExpr : ExprStructMap Expr := {} levelParams : Array Name := #[] nextLevelIdx : Nat := 1 levelArgs : Array Level := #[] newLocalDecls : Array LocalDecl := #[] newLocalDeclsForMVars : Array LocalDecl := #[] newLetDecls : Array LocalDecl := #[] nextExprIdx : Nat := 1 exprMVarArgs : Array Expr := #[] exprFVarArgs : Array Expr := #[] toProcess : Array ToProcessElement := #[] abbrev ClosureM := ReaderT Context $ StateRefT State MetaM @[inline] def visitLevel (f : Level → ClosureM Level) (u : Level) : ClosureM Level := do if !u.hasMVar && !u.hasParam then pure u else let s ← get match s.visitedLevel.find? u with | some v => pure v | none => do let v ← f u modify fun s => { s with visitedLevel := s.visitedLevel.insert u v } pure v @[inline] def visitExpr (f : Expr → ClosureM Expr) (e : Expr) : ClosureM Expr := do if !e.hasLevelParam && !e.hasFVar && !e.hasMVar then pure e else let s ← get match s.visitedExpr.find? e with | some r => pure r | none => let r ← f e modify fun s => { s with visitedExpr := s.visitedExpr.insert e r } pure r def mkNewLevelParam (u : Level) : ClosureM Level := do let s ← get let p := (`u).appendIndexAfter s.nextLevelIdx modify fun s => { s with levelParams := s.levelParams.push p, nextLevelIdx := s.nextLevelIdx + 1, levelArgs := s.levelArgs.push u } pure $ mkLevelParam p partial def collectLevelAux : Level → ClosureM Level | u@(Level.succ v _) => return u.updateSucc! (← visitLevel collectLevelAux v) | u@(Level.max v w _) => return u.updateMax! (← visitLevel collectLevelAux v) (← visitLevel collectLevelAux w) | u@(Level.imax v w _) => return u.updateIMax! (← visitLevel collectLevelAux v) (← visitLevel collectLevelAux w) | u@(Level.mvar mvarId _) => mkNewLevelParam u | u@(Level.param _ _) => mkNewLevelParam u | u@(Level.zero _) => pure u def collectLevel (u : Level) : ClosureM Level := do -- u ← instantiateLevelMVars u visitLevel collectLevelAux u def preprocess (e : Expr) : ClosureM Expr := do let e ← instantiateMVars e let ctx ← read -- If we are not zeta-expanding let-decls, then we use `check` to find -- which let-decls are dependent. We say a let-decl is dependent if its lambda abstraction is type incorrect. if !ctx.zeta then check e pure e /-- Remark: This method does not guarantee unique user names. The correctness of the procedure does not rely on unique user names. Recall that the pretty printer takes care of unintended collisions. -/ def mkNextUserName : ClosureM Name := do let s ← get let n := (`_x).appendIndexAfter s.nextExprIdx modify fun s => { s with nextExprIdx := s.nextExprIdx + 1 } pure n def pushToProcess (elem : ToProcessElement) : ClosureM Unit := modify fun s => { s with toProcess := s.toProcess.push elem } partial def collectExprAux (e : Expr) : ClosureM Expr := do let collect (e : Expr) := visitExpr collectExprAux e match e with | Expr.proj _ _ s _ => return e.updateProj! (← collect s) | Expr.forallE _ d b _ => return e.updateForallE! (← collect d) (← collect b) | Expr.lam _ d b _ => return e.updateLambdaE! (← collect d) (← collect b) | Expr.letE _ t v b _ => return e.updateLet! (← collect t) (← collect v) (← collect b) | Expr.app f a _ => return e.updateApp! (← collect f) (← collect a) | Expr.mdata _ b _ => return e.updateMData! (← collect b) | Expr.sort u _ => return e.updateSort! (← collectLevel u) | Expr.const c us _ => return e.updateConst! (← us.mapM collectLevel) | Expr.mvar mvarId _ => let mvarDecl ← getMVarDecl mvarId let type ← preprocess mvarDecl.type let type ← collect type let newFVarId ← mkFreshFVarId let userName ← mkNextUserName modify fun s => { s with newLocalDeclsForMVars := s.newLocalDeclsForMVars.push $ LocalDecl.cdecl arbitrary newFVarId userName type BinderInfo.default, exprMVarArgs := s.exprMVarArgs.push e } return mkFVar newFVarId | Expr.fvar fvarId _ => match (← read).zeta, (← getLocalDecl fvarId).value? with | true, some value => collect (← preprocess value) | _, _ => let newFVarId ← mkFreshFVarId pushToProcess ⟨fvarId, newFVarId⟩ return mkFVar newFVarId | e => pure e def collectExpr (e : Expr) : ClosureM Expr := do let e ← preprocess e visitExpr collectExprAux e partial def pickNextToProcessAux (lctx : LocalContext) (i : Nat) (toProcess : Array ToProcessElement) (elem : ToProcessElement) : ToProcessElement × Array ToProcessElement := if h : i < toProcess.size then let elem' := toProcess.get ⟨i, h⟩ if (lctx.get! elem.fvarId).index < (lctx.get! elem'.fvarId).index then pickNextToProcessAux lctx (i+1) (toProcess.set ⟨i, h⟩ elem) elem' else pickNextToProcessAux lctx (i+1) toProcess elem else (elem, toProcess) def pickNextToProcess? : ClosureM (Option ToProcessElement) := do let lctx ← getLCtx let s ← get if s.toProcess.isEmpty then pure none else modifyGet fun s => let elem := s.toProcess.back let toProcess := s.toProcess.pop let (elem, toProcess) := pickNextToProcessAux lctx 0 toProcess elem (some elem, { s with toProcess := toProcess }) def pushFVarArg (e : Expr) : ClosureM Unit := modify fun s => { s with exprFVarArgs := s.exprFVarArgs.push e } def pushLocalDecl (newFVarId : FVarId) (userName : Name) (type : Expr) (bi := BinderInfo.default) : ClosureM Unit := do let type ← collectExpr type modify fun s => { s with newLocalDecls := s.newLocalDecls.push <| LocalDecl.cdecl arbitrary newFVarId userName type bi } partial def process : ClosureM Unit := do match (← pickNextToProcess?) with | none => pure () | some ⟨fvarId, newFVarId⟩ => let localDecl ← getLocalDecl fvarId match localDecl with | LocalDecl.cdecl _ _ userName type bi => pushLocalDecl newFVarId userName type bi pushFVarArg (mkFVar fvarId) process | LocalDecl.ldecl _ _ userName type val _ => let zetaFVarIds ← getZetaFVarIds if !zetaFVarIds.contains fvarId then /- Non-dependent let-decl Recall that if `fvarId` is in `zetaFVarIds`, then we zeta-expanded it during type checking (see `check` at `collectExpr`). Our type checker may zeta-expand declarations that are not needed, but this check is conservative, and seems to work well in practice. -/ pushLocalDecl newFVarId userName type pushFVarArg (mkFVar fvarId) process else /- Dependent let-decl -/ let type ← collectExpr type let val ← collectExpr val modify fun s => { s with newLetDecls := s.newLetDecls.push <| LocalDecl.ldecl arbitrary newFVarId userName type val false } /- We don't want to interleave let and lambda declarations in our closure. So, we expand any occurrences of newFVarId at `newLocalDecls` -/ modify fun s => { s with newLocalDecls := s.newLocalDecls.map (replaceFVarIdAtLocalDecl newFVarId val) } process @[inline] def mkBinding (isLambda : Bool) (decls : Array LocalDecl) (b : Expr) : Expr := let xs := decls.map LocalDecl.toExpr let b := b.abstract xs decls.size.foldRev (init := b) fun i b => let decl := decls[i] match decl with | LocalDecl.cdecl _ _ n ty bi => let ty := ty.abstractRange i xs if isLambda then Lean.mkLambda n bi ty b else Lean.mkForall n bi ty b | LocalDecl.ldecl _ _ n ty val nonDep => if b.hasLooseBVar 0 then let ty := ty.abstractRange i xs let val := val.abstractRange i xs mkLet n ty val b nonDep else b.lowerLooseBVars 1 1 def mkLambda (decls : Array LocalDecl) (b : Expr) : Expr := mkBinding true decls b def mkForall (decls : Array LocalDecl) (b : Expr) : Expr := mkBinding false decls b structure MkValueTypeClosureResult where levelParams : Array Name type : Expr value : Expr levelArgs : Array Level exprArgs : Array Expr def mkValueTypeClosureAux (type : Expr) (value : Expr) : ClosureM (Expr × Expr) := do resetZetaFVarIds withTrackingZeta do let type ← collectExpr type let value ← collectExpr value process pure (type, value) def mkValueTypeClosure (type : Expr) (value : Expr) (zeta : Bool) : MetaM MkValueTypeClosureResult := do let ((type, value), s) ← ((mkValueTypeClosureAux type value).run { zeta := zeta }).run {} let newLocalDecls := s.newLocalDecls.reverse ++ s.newLocalDeclsForMVars let newLetDecls := s.newLetDecls.reverse let type := mkForall newLocalDecls (mkForall newLetDecls type) let value := mkLambda newLocalDecls (mkLambda newLetDecls value) pure { type := type, value := value, levelParams := s.levelParams, levelArgs := s.levelArgs, exprArgs := s.exprFVarArgs.reverse ++ s.exprMVarArgs } end Closure /-- Create an auxiliary definition with the given name, type and value. The parameters `type` and `value` may contain free and meta variables. A "closure" is computed, and a term of the form `name.{u_1 ... u_n} t_1 ... t_m` is returned where `u_i`s are universe parameters and metavariables `type` and `value` depend on, and `t_j`s are free and meta variables `type` and `value` depend on. -/ def mkAuxDefinition (name : Name) (type : Expr) (value : Expr) (zeta : Bool := false) (compile : Bool := true) : MetaM Expr := do trace[Meta.debug] "{name} : {type} := {value}" let result ← Closure.mkValueTypeClosure type value zeta let env ← getEnv let decl := Declaration.defnDecl { name := name, levelParams := result.levelParams.toList, type := result.type, value := result.value, hints := ReducibilityHints.regular (getMaxHeight env result.value + 1), safety := if env.hasUnsafe result.type || env.hasUnsafe result.value then DefinitionSafety.unsafe else DefinitionSafety.safe } trace[Meta.debug] "{name} : {result.type} := {result.value}" addDecl decl if compile then compileDecl decl return mkAppN (mkConst name result.levelArgs.toList) result.exprArgs /-- Similar to `mkAuxDefinition`, but infers the type of `value`. -/ def mkAuxDefinitionFor (name : Name) (value : Expr) (zeta : Bool := false) : MetaM Expr := do let type ← inferType value let type := type.headBeta mkAuxDefinition name type value (zeta := zeta) end Lean.Meta
d574658a55bca982ad616b2ff5e644cb3260bbe4
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/normed_space/riesz_lemma.lean
8be5c0bcc2557768593ab6c273d34fe02094e8ba
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,140
lean
/- Copyright (c) 2019 Jean Lo. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jean Lo -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.metric_space.hausdorff_distance import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-! # Riesz's lemma Riesz's lemma, stated for a normed space over a normed field: for any closed proper subspace F of E, there is a nonzero x such that ∥x - F∥ is at least r * ∥x∥ for any r < 1. -/ /-- Riesz's lemma, which usually states that it is possible to find a vector with norm 1 whose distance to a closed proper subspace is arbitrarily close to 1. The statement here is in terms of multiples of norms, since in general the existence of an element of norm exactly 1 is not guaranteed. -/ theorem riesz_lemma {𝕜 : Type u_1} [normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : subspace 𝕜 E} (hFc : is_closed ↑F) (hF : ∃ (x : E), ¬x ∈ F) {r : ℝ} (hr : r < 1) : ∃ (x₀ : E), ¬x₀ ∈ F ∧ ∀ (y : E), y ∈ F → r * norm x₀ ≤ norm (x₀ - y) := sorry
c82ed0c6ac4f528ee0aa6bf879ef6e7a549e797e
3618c6e11aa822fd542440674dfb9a7b9921dba0
/src/solve_by_trivial.lean
482acdced4f018e3c588e30396e5ea5d2578b2c1
[]
no_license
ChrisHughes24/single_relation
99ceedcc02d236ce46d6c65d72caa669857533c5
057e157a59de6d0e43b50fcb537d66792ec20450
refs/heads/master
1,683,652,062,698
1,683,360,089,000
1,683,360,089,000
279,346,432
0
0
null
null
null
null
UTF-8
Lean
false
false
425
lean
import P /-! # Solve by trivial This file contains one definition, `solve_by_trivial`. `solve_by_trivial` solves the word problem in the easy case when every letter in `w` is already in `T`. -/ open semidirect_product def solve_by_trivial {ι : Type} [decidable_eq ι] (T : set ι) [decidable_pred T] (w : free_group ι) (vars_w : list ι) : option (P (free_group ι)) := guard (vars_w.all (∈ T)) >> return (inr w)
7a8b376b1cf743176b2e41b1c64a78d37e3c2d1e
5e42295de7f5bcdf224b94603a8ec29b17c2d367
/radicals.lean
eac3ba4e1afe0c8d3004aac1922936bed0d041db
[]
no_license
pnmadelaine/lean_polya
9369e0d87dce773f91383bb58ac6fde0a00a1a40
1c62b0b3fa71044b0225ce28030627d251b08ebc
refs/heads/master
1,590,161,172,243
1,515,010,019,000
1,515,010,019,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,575
lean
import rat_additions norm_num--comp_val open tactic lemma rat_pow_mul (a : ℚ) (e : ℤ) : rat.pow a (e + e) = rat.pow a e * rat.pow a e := sorry @[simp] --lemma pow_bit0 (b : ℚ) (e : ℤ) : rat.pow b (bit0 e) = rat.pow b e * rat.pow b e := lemma pow_bit0 (b : ℚ) (e : ℤ) : rat.pow b (bit0 e) = let v := rat.pow b e in v * v := rat_pow_mul _ _ @[simp] --lemma pow_bit1 (b : ℚ) (e : ℤ) : rat.pow b (bit1 e) = b*rat.pow b e * rat.pow b e := lemma pow_bit1 (b : ℚ) (e : ℤ) : rat.pow b (bit1 e) = let v := rat.pow b e in b*v*v := sorry @[simp] lemma rat.pow_zero (b : ℚ) : rat.pow b 0 = 1 := sorry attribute [simp] rat.pow_one meta def pf_by_norm_num : tactic unit := do (lhs, rhs) ← target >>= match_eq, (lhsv, lhspf) ← norm_num lhs, (rhsv, rhspf) ← norm_num rhs, to_expr ``(eq.trans %%lhspf (eq.symm %%rhspf)) >>= exact lemma f1 (a) : rat.pow a ↑(0 : ℕ) = rat.pow a 0 := sorry lemma f2 (a) : rat.pow a ↑(1 : ℕ) = rat.pow a 1 := sorry lemma f3 (a b) : rat.pow a ↑(bit1 b : ℕ) = rat.pow a (bit1 ↑b) := sorry lemma f4 (a b) : rat.pow a ↑(bit0 b : ℕ) = rat.pow a (bit0 ↑b) := sorry meta def rat_pow_simp_lemmas : tactic simp_lemmas := to_simp_lemmas simp_lemmas.mk [`pow_bit0, `pow_bit1, `rat.pow_zero, `rat.pow_one, `f1, `f2, `f3, `f4] meta def simp_rat_pow : tactic unit := rat_pow_simp_lemmas >>= simp_target --`[simp only [pow_bit0, pow_bit1, rat.pow_zero, rat.pow_one, f1, f2, f3, f4]] meta def prove_rat_pow : tactic unit := simp_rat_pow >> pf_by_norm_num example : rat.pow 5 3 = 125 := by prove_rat_pow inductive approx_dir | over | under | none meta def approx_dir.to_expr : approx_dir → expr | approx_dir.over := `(@ge rat _) | approx_dir.under := `(@has_le.le rat _) | approx_dir.none := `(@eq rat) meta def correct_offset (A x prec : ℚ) (n : ℤ) : approx_dir → ℚ | approx_dir.over := if rat.pow x n ≥ A then x else x + prec | approx_dir.under := if rat.pow x n ≤ A then x else x - prec | approx_dir.none := x meta def round_to_denom (A : ℚ) (denom : ℕ) : ℚ := let num_q := (denom*A.num : ℤ) / A.denom in rat.mk num_q denom namespace rat meta def nth_root_aux'' (A : ℚ) (n : ℤ) (prec : ℚ) (dir : approx_dir) : ℚ → ℚ | guess := let delta_x := (1/(n : ℚ))*((A / rat.pow guess (n - 1)) - guess), x := guess + delta_x in /-correct_offset A-/ (if abs delta_x < prec then x else nth_root_aux'' x) /-prec n dir-/ meta def nth_root_aux' (A : ℚ) (n : ℤ) (prec : ℚ) (dir : approx_dir) (guess : ℚ) : ℚ := correct_offset A (nth_root_aux'' A n prec dir guess) prec n dir meta def nth_root_aux (A : ℚ) (n : ℤ) (prec guess : ℚ) (dir : approx_dir := approx_dir.none) : ℚ := nth_root_aux' A n prec dir guess /-meta def nth_root_approx (A : ℚ) (n : ℤ) (prec : ℚ) (dir : approx_dir := approx_dir.none) : ℚ := nth_root_aux A n prec (A / n) dir-/ meta def nth_root_approx_bin_aux (A : ℚ) (n : ℤ) (prec : ℚ) : ℕ → ℚ → ℚ | steps guess := if steps = 0 then guess else let v := rat.pow guess n in if v < A then nth_root_approx_bin_aux (steps/2) (guess + steps*prec) else if v > A then nth_root_approx_bin_aux (steps/2) (guess - steps*prec) else guess meta def nth_root_approx_bin (A : ℚ) (n : ℤ) (prec guess : ℚ) (dir : approx_dir := approx_dir.none) : ℚ := let v := nth_root_approx_bin_aux A n prec (ceil (A/4)).nat_abs guess in correct_offset A v prec n dir end rat namespace int /-def pow (base : ℤ) : ℤ → ℤ | (of_nat n) := pow_nat base n | -[1+n] := meta def nth_root_aux (A : ℤ) (n : ℤ) (prec : ℚ) : ℤ → ℤ | guess := let delta_x := ((A / pow_int guess (n - 1)) - guess) / n, x := guess + delta_x in if abs delta_x < prec then x else nth_root_aux x meta def nth_root_approx (A : ℚ) (n : ℤ) (prec : ℚ) : ℚ := nth_root_aux A n prec (A / n)-/ -- inl means success, inr means failed with meta def nth_root_aux (A : ℤ) (n : ℕ) : ℤ → ℤ → ℤ⊕ℤ | step guess := if step = 0 ∨ step = 1 then if guess^n = A then sum.inl guess else if (guess+1)^n=A then sum.inl $ guess+1 else if (guess-1)^n=A then sum.inl $ guess-1 else sum.inr guess else if guess^n = A then sum.inl guess else if guess^n < A then nth_root_aux ((step+1)/2) (guess+step) else nth_root_aux ((step+1)/2) (guess-step) meta def nth_root (A : ℤ) (n : ℕ) : ℤ⊕ℤ := nth_root_aux A n ((A+1)/4) ((A+1)/4) meta def nth_root_o (A : ℤ) (n : ℕ) : option ℤ := match nth_root A n with | sum.inl v := some v | sum.inr _ := none end end int /--- faster to skip first rat approx in non-1 denom case? meta def nth_root_approx' (dir : approx_dir) : Π (A : ℚ) (n : ℕ) (prec : ℚ), ℚ | A n prec := if A.denom = 1 then match int.nth_root A.num n with | sum.inl v := v | sum.inr v := rat.nth_root_aux A n prec (if v = 0 then prec else v) dir end else let num_apr := int.nth_root A.num n, den_apr := int.nth_root A.denom n in match num_apr, den_apr with | sum.inl vn, sum.inl vd := (vn : ℚ) / vd | sum.inl vn, sum.inr vd := rat.nth_root_aux A n prec (vn / vd) dir --(vn / rat.nth_root_aux A.denom n prec vd) dir | sum.inr vn, sum.inl vd := rat.nth_root_aux A n prec (vn / vd) dir --(rat.nth_root_aux A.num n prec vn / vd) dir | sum.inr vn, sum.inr vd := rat.nth_root_aux A n prec (vn / vd) dir--(rat.nth_root_aux A.num n prec vn / rat.nth_root_aux A.denom n prec vn) dir end -- rat.nth_root_aux A n prec ((nth_root_approx' A.num n prec) / (nth_root_approx' A.denom n prec)) dir -/ -- no rounding or direction fixing meta def nth_root_approx''_a (dir : approx_dir) : Π (A : ℚ) (n : ℕ) (prec : ℚ), ℚ | A n prec := if A.denom = 1 then match int.nth_root A.num n with | sum.inl v := v | sum.inr v := (rat.nth_root_aux'' A n prec dir (if v = 0 then prec else v)) --rat.nth_root_aux A n prec (if v = 0 then prec else v) dir end else let num_apr := int.nth_root A.num n, den_apr := int.nth_root A.denom n in match num_apr, den_apr with | sum.inl vn, sum.inl vd := (vn : ℚ) / vd | sum.inl vn, sum.inr vd := rat.nth_root_aux'' A n prec dir (vn / vd) --(vn / rat.nth_root_aux A.denom n prec vd) dir | sum.inr vn, sum.inl vd := rat.nth_root_aux'' A n prec dir (vn / vd) --(rat.nth_root_aux A.num n prec vn / vd) dir | sum.inr vn, sum.inr vd := rat.nth_root_aux'' A n prec dir (vn / vd) --(rat.nth_root_aux A.num n prec vn / rat.nth_root_aux A.denom n prec vn) dir end meta def nth_root_approx (dir : approx_dir) (A : ℚ) (n : ℕ) (denom : ℕ) /-(prec : ℚ)-/ : ℚ := let av := nth_root_approx''_a dir A n (1/(denom : ℚ)) in correct_offset A (round_to_denom av denom) (1/(denom : ℚ)) n dir /-meta def nth_root_approx (A : ℚ) (n : ℕ) (prec : ℚ) (dir : approx_dir := approx_dir.none) : ℚ := nth_root_approx' dir A n prec -/ open tactic meta def prove_nth_root_approx (A : ℚ) (n : ℕ) (prec : ℕ) (dir : approx_dir) : tactic expr := let apprx := nth_root_approx dir A n prec in do apprx_pow ← to_expr ``(rat.pow %%(reflect apprx) %%(reflect n)), let tgt := dir.to_expr apprx_pow `(A) in do (_, e) ← solve_aux tgt (trace_state >> simp_rat_pow >> trace_state >> (try `[norm_num]) >> done), return e --set_option pp.all true #exit local attribute [irreducible] rat.pow example : (0 : ℚ) ≤ 5+1 := begin simp end --example : (0 : ℚ) ≤ 500 := by reflexivity #eval let v := nth_root_approx 10 2 0.5 in v*v #eval nth_root_approx 2 2 10 approx_dir.under #eval int.nth_root 2 2 #eval rat.nth_root_aux 2 2 100 0 approx_dir.over #eval rat.nth_root_aux 2 4 100 0 approx_dir.over example : (198 : ℚ) / 100 ≤ 2 := by norm_num --btrivial run_cmd do prove_nth_root_approx 2 2 1000 approx_dir.under >>= infer_type >>= trace /-do trace "hi", let tgt := dir.to_expr `(rat.pow apprx n) A in failed -/ #exit set_option profiler true run_cmd trace $int.nth_root 1934434936 3 run_cmd trace $ nth_root_approx 19344349361234579569400000000000 2 0.0000000000000000000005 run_cmd trace $ nth_root_approx 54354358908309423742342 2 0.0000000000000000000005 run_cmd trace $ nth_root_approx (54354358908309423742342 / 19344349361234579569400000000000) 2 0.0000000000000000000005 run_cmd trace $ nth_root_approx (100/81) 2 0.00005 example : true := by do trace $ nth_root_approx 1934434936134579569400000000000 2 0.0000000000000000000005, triv --run_cmd trace $ int.nth_root 10 2 --run_cmd trace $ rat.nth_root_aux 10 2 0.5 4 #exit meta def nearest_int (q : ℚ) : ℤ := if ↑q.ceil - q < 0.5 then q.ceil else q.floor meta def find_integer_root (q : ℚ) (n : ℤ) : option ℚ := let n_o := nearest_int $ nth_root_approx q n 0.5 in if rat.pow n_o n = q then some n_o else none #exit meta def small_factor_precomp : list ((int × rat) × (rat × expr)) := [ ((2, 1), (1, `(by prove_rat_pow : rat.pow 1 2 = 1))), ((2, 4), (2, `(by prove_rat_pow : rat.pow 2 2 = 4))), ((2, 9), (3, `(by prove_rat_pow : rat.pow 3 2 = 9))), ((2, 16), (4, `(by prove_rat_pow : rat.pow 4 2 = 16))), ((2, 25), (5, `(by prove_rat_pow : rat.pow 5 2 = 25))), ((2, 36), (6, `(by prove_rat_pow : rat.pow 6 2 = 36))), ((2, 49), (7, `(by prove_rat_pow : rat.pow 7 2 = 49))), ((2, 64), (8, `(by prove_rat_pow : rat.pow 8 2 = 64))), ((2, 81), (9, `(by prove_rat_pow : rat.pow 9 2 = 81))), ((2, 100), (10, `(by prove_rat_pow : rat.pow 10 2 = 100))), ((3, 1), (1, `(by prove_rat_pow : rat.pow 1 3 = 1))), ((3, 8), (2, `(by prove_rat_pow : rat.pow 2 3 = 8))), ((3, 27), (3, `(by prove_rat_pow : rat.pow 3 3 = 27))), ((3, 64), (4, `(by prove_rat_pow : rat.pow 4 3 = 64))), ((3, 125), (5, `(by prove_rat_pow : rat.pow 5 3 = 125))), ((3, 216), (6, `(by prove_rat_pow : rat.pow 6 3 = 216))), ((3, 343), (7, `(by prove_rat_pow : rat.pow 7 3 = 343))), ((3, 512), (8, `(by prove_rat_pow : rat.pow 8 3 = 512))), ((3, 729), (9, `(by prove_rat_pow : rat.pow 9 3 = 729))), ((3, 1000), (10, `(by prove_rat_pow : rat.pow 10 3 = 1000))), ((4, 1), (1, `(by prove_rat_pow : rat.pow 1 4 = 1))), ((4, 16), (2, `(by prove_rat_pow : rat.pow 2 4 = 16))), ((4, 81), (3, `(by prove_rat_pow : rat.pow 3 4 = 81))), ((4, 256), (4, `(by prove_rat_pow : rat.pow 4 4 = 256))), ((4, 625), (5, `(by prove_rat_pow : rat.pow 5 4 = 625))), ((4, 1296), (6, `(by prove_rat_pow : rat.pow 6 4 = 1296))), ((4, 2401), (7, `(by prove_rat_pow : rat.pow 7 4 = 2401))), ((4, 4096), (8, `(by prove_rat_pow : rat.pow 8 4 = 4096))), ((4, 6561), (9, `(by prove_rat_pow : rat.pow 9 4 = 6561))), ((4, 10000), (10, `(by prove_rat_pow : rat.pow 10 4 = 10000))) ] meta def small_factor_map : rb_map (ℤ × ℚ) (ℚ × expr) := rb_map.of_list small_factor_precomp
302adf7b70bdca1f9dbbc9705262bf930189dc60
c31182a012eec69da0a1f6c05f42b0f0717d212d
/src/rescale/basic.lean
dfa6b781871e4e0c7b144312e194c365208f11fb
[]
no_license
Ja1941/lean-liquid
fbec3ffc7fc67df1b5ca95b7ee225685ab9ffbdc
8e80ed0cbdf5145d6814e833a674eaf05a1495c1
refs/heads/master
1,689,437,983,362
1,628,362,719,000
1,628,362,719,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
524
lean
import polyhedral_lattice.basic /-! # rescaling the norm on a type This file is little more than the definition of the type alias. -/ noncomputable theory open_locale big_operators classical nnreal @[nolint unused_arguments] def rescale (N : ℝ≥0) (V : Type*) := V namespace rescale variables {N : ℝ≥0} {V : Type*} instance [i : add_comm_monoid V] : add_comm_monoid (rescale N V) := i instance [i : add_comm_group V] : add_comm_group (rescale N V) := i def of : V ≃ rescale N V := equiv.refl _ end rescale
d1ac055a10aee5604f1d1ab2e71090b30dd5110b
6dafe3cbeb81b4a792fbf679e64eccb29225af1c
/src/Notices_AMS/injective_comp.lean
607729f115755d5b35f2f4efc720c6705b82cafe
[]
no_license
Shiney/xena
9bdf22f0036baa7b22753e1e0b8101ca367551b6
8ff8f04d79b29a7a33bfefa318fd552388d0b989
refs/heads/master
1,623,652,579,961
1,586,444,931,000
1,586,444,931,000
254,480,076
0
0
null
1,586,466,973,000
1,586,466,973,000
null
UTF-8
Lean
false
false
619
lean
import tactic -- tactic mode open function variables (X Y Z : Type) (f : X → Y) (g : Y → Z) /-- The composite of two injective functions is injective. -/ theorem injective_comp : injective f ∧ injective g → injective (g ∘ f) := begin -- assume f and g are injective. We want to prove g ∘ f is injective. rintro ⟨f_inj, g_inj⟩, -- So say a,b ∈ X and assume g(f(a))=g(f(b)). We want to prove that a = b. intros a b gfa_eq_gfb, -- By injectivity of f, it suffices to prove that f(a)=f(b). apply f_inj, -- By injectivity of g, and our assumption, we're done. exact g_inj gfa_eq_gfb end
16b0fac392f736bea5316d1c91a9acf51c39f684
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch4/ex0103.lean
1f3cfda4f45ca63e138052fd8b605dff6536091f
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
260
lean
variables (α : Type) (r : α → α → Prop) variable trans_r : forall x y z, r x y → r y z → r x z variables (a b c : α) variables (hab : r a b) (hbc : r b c) #check trans_r #check trans_r a b c #check trans_r a b c hab #check trans_r a b c hab hbc
96e969fac8c2643a4bf96dfd61eeae5b39de9f84
a523fc1740c8cb84cd0fa0f4b52a760da4e32a5c
/src/missing_mathlib/ring_theory/algebra.lean
9b6ac41b898e13f9d77aa2dec2799fb520a7cecb
[]
no_license
abentkamp/spectral
a1aff51e85d30b296a81d256ced1d382345d3396
751645679ef1cb6266316349de9e492eff85484c
refs/heads/master
1,669,994,798,064
1,597,591,646,000
1,597,591,646,000
287,544,072
0
0
null
null
null
null
UTF-8
Lean
false
false
1,065
lean
import ring_theory.algebra universes u v w u₁ v₁ namespace algebra variables {R : Type u} {S : Type v} {A : Type w} variables [comm_ring R] [comm_ring S] [ring A] [algebra R A] lemma mul_sub_algebra_map_commutes (x : A) (r : R) : x * (x - algebra_map R A r) = (x - algebra_map R A r) * x := by rw [mul_sub, ←commutes, sub_mul] lemma mul_sub_algebra_map_pow_commutes (x : A) (r : R) (n : ℕ) : x * (x - algebra_map R A r) ^ n = (x - algebra_map R A r) ^ n * x := begin induction n with n ih, { simp }, { rw [pow_succ, ←mul_assoc, mul_sub_algebra_map_commutes, mul_assoc, ih, ←mul_assoc], } end end algebra -- Put below endomorphism algebra lemma module.endomorphism_algebra_map_apply (R : Type u) (M : Type v) [comm_ring R] [add_comm_group M] [module R M] (a : R) : (algebra_map R (M →ₗ[R] M)) a = a • linear_map.id := rfl lemma module.endomorphism_algebra_map_apply2 (R : Type u) (M : Type v) [comm_ring R] [add_comm_group M] [module R M] (a : R) (m : M) : (algebra_map R (M →ₗ[R] M)) a m = a • m := rfl
9bfbf6134b59fec04ce0da2dd9ab326dd61e7642
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/logic/function/iterate.lean
7ee0de3cba9162f9a35ec8feca45afb4e408db92
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
7,037
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import logic.function.conjugate import tactic.alias /-! # Iterations of a function > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we prove simple properties of `nat.iterate f n` a.k.a. `f^[n]`: * `iterate_zero`, `iterate_succ`, `iterate_succ'`, `iterate_add`, `iterate_mul`: formulas for `f^[0]`, `f^[n+1]` (two versions), `f^[n+m]`, and `f^[n*m]`; * `iterate_id` : `id^[n]=id`; * `injective.iterate`, `surjective.iterate`, `bijective.iterate` : iterates of an injective/surjective/bijective function belong to the same class; * `left_inverse.iterate`, `right_inverse.iterate`, `commute.iterate_left`, `commute.iterate_right`, `commute.iterate_iterate`: some properties of pairs of functions survive under iterations * `iterate_fixed`, `semiconj.iterate_*`, `semiconj₂.iterate`: if `f` fixes a point (resp., semiconjugates unary/binary operarations), then so does `f^[n]`. -/ universes u v variables {α : Type u} {β : Type v} namespace function variable (f : α → α) @[simp] theorem iterate_zero : f^[0] = id := rfl theorem iterate_zero_apply (x : α) : f^[0] x = x := rfl @[simp] theorem iterate_succ (n : ℕ) : f^[n.succ] = (f^[n]) ∘ f := rfl theorem iterate_succ_apply (n : ℕ) (x : α) : f^[n.succ] x = (f^[n]) (f x) := rfl @[simp] theorem iterate_id (n : ℕ) : (id : α → α)^[n] = id := nat.rec_on n rfl $ λ n ihn, by rw [iterate_succ, ihn, comp.left_id] theorem iterate_add : ∀ (m n : ℕ), f^[m + n] = (f^[m]) ∘ (f^[n]) | m 0 := rfl | m (nat.succ n) := by rw [nat.add_succ, iterate_succ, iterate_succ, iterate_add] theorem iterate_add_apply (m n : ℕ) (x : α) : f^[m + n] x = (f^[m] (f^[n] x)) := by rw iterate_add @[simp] theorem iterate_one : f^[1] = f := funext $ λ a, rfl lemma iterate_mul (m : ℕ) : ∀ n, f^[m * n] = (f^[m]^[n]) | 0 := by simp only [nat.mul_zero, iterate_zero] | (n + 1) := by simp only [nat.mul_succ, nat.mul_one, iterate_one, iterate_add, iterate_mul n] variable {f} theorem iterate_fixed {x} (h : f x = x) (n : ℕ) : f^[n] x = x := nat.rec_on n rfl $ λ n ihn, by rw [iterate_succ_apply, h, ihn] theorem injective.iterate (Hinj : injective f) (n : ℕ) : injective (f^[n]) := nat.rec_on n injective_id $ λ n ihn, ihn.comp Hinj theorem surjective.iterate (Hsurj : surjective f) (n : ℕ) : surjective (f^[n]) := nat.rec_on n surjective_id $ λ n ihn, ihn.comp Hsurj theorem bijective.iterate (Hbij : bijective f) (n : ℕ) : bijective (f^[n]) := ⟨Hbij.1.iterate n, Hbij.2.iterate n⟩ namespace semiconj lemma iterate_right {f : α → β} {ga : α → α} {gb : β → β} (h : semiconj f ga gb) (n : ℕ) : semiconj f (ga^[n]) (gb^[n]) := nat.rec_on n id_right $ λ n ihn, ihn.comp_right h lemma iterate_left {g : ℕ → α → α} (H : ∀ n, semiconj f (g n) (g $ n + 1)) (n k : ℕ) : semiconj (f^[n]) (g k) (g $ n + k) := begin induction n with n ihn generalizing k, { rw [nat.zero_add], exact id_left }, { rw [nat.succ_eq_add_one, nat.add_right_comm, nat.add_assoc], exact (H k).comp_left (ihn (k + 1)) } end end semiconj namespace commute variable {g : α → α} lemma iterate_right (h : commute f g) (n : ℕ) : commute f (g^[n]) := h.iterate_right n lemma iterate_left (h : commute f g) (n : ℕ) : commute (f^[n]) g := (h.symm.iterate_right n).symm lemma iterate_iterate (h : commute f g) (m n : ℕ) : commute (f^[m]) (g^[n]) := (h.iterate_left m).iterate_right n lemma iterate_eq_of_map_eq (h : commute f g) (n : ℕ) {x} (hx : f x = g x) : f^[n] x = (g^[n]) x := nat.rec_on n rfl $ λ n ihn, by simp only [iterate_succ_apply, hx, (h.iterate_left n).eq, ihn, ((refl g).iterate_right n).eq] lemma comp_iterate (h : commute f g) (n : ℕ) : (f ∘ g)^[n] = (f^[n]) ∘ (g^[n]) := begin induction n with n ihn, { refl }, funext x, simp only [ihn, (h.iterate_right n).eq, iterate_succ, comp_app] end variable (f) lemma iterate_self (n : ℕ) : commute (f^[n]) f := (refl f).iterate_left n lemma self_iterate (n : ℕ) : commute f (f^[n]) := (refl f).iterate_right n lemma iterate_iterate_self (m n : ℕ) : commute (f^[m]) (f^[n]) := (refl f).iterate_iterate m n end commute lemma semiconj₂.iterate {f : α → α} {op : α → α → α} (hf : semiconj₂ f op op) (n : ℕ) : semiconj₂ (f^[n]) op op := nat.rec_on n (semiconj₂.id_left op) (λ n ihn, ihn.comp hf) variable (f) theorem iterate_succ' (n : ℕ) : f^[n.succ] = f ∘ (f^[n]) := by rw [iterate_succ, (commute.self_iterate f n).comp_eq] theorem iterate_succ_apply' (n : ℕ) (x : α) : f^[n.succ] x = f (f^[n] x) := by rw [iterate_succ'] theorem iterate_pred_comp_of_pos {n : ℕ} (hn : 0 < n) : f^[n.pred] ∘ f = (f^[n]) := by rw [← iterate_succ, nat.succ_pred_eq_of_pos hn] theorem comp_iterate_pred_of_pos {n : ℕ} (hn : 0 < n) : f ∘ (f^[n.pred]) = (f^[n]) := by rw [← iterate_succ', nat.succ_pred_eq_of_pos hn] /-- A recursor for the iterate of a function. -/ def iterate.rec (p : α → Sort*) {f : α → α} (h : ∀ a, p a → p (f a)) {a : α} (ha : p a) (n : ℕ) : p (f^[n] a) := nat.rec ha (λ m, by { rw iterate_succ', exact h _ }) n lemma iterate.rec_zero (p : α → Sort*) {f : α → α} (h : ∀ a, p a → p (f a)) {a : α} (ha : p a) : iterate.rec p h ha 0 = ha := rfl variables {f} {m n : ℕ} {a : α} theorem left_inverse.iterate {g : α → α} (hg : left_inverse g f) (n : ℕ) : left_inverse (g^[n]) (f^[n]) := nat.rec_on n (λ _, rfl) $ λ n ihn, by { rw [iterate_succ', iterate_succ], exact ihn.comp hg } theorem right_inverse.iterate {g : α → α} (hg : right_inverse g f) (n : ℕ) : right_inverse (g^[n]) (f^[n]) := hg.iterate n lemma iterate_comm (f : α → α) (m n : ℕ) : f^[n]^[m] = (f^[m]^[n]) := (iterate_mul _ _ _).symm.trans (eq.trans (by rw nat.mul_comm) (iterate_mul _ _ _)) lemma iterate_commute (m n : ℕ) : commute (λ f : α → α, f^[m]) (λ f, f^[n]) := λ f, iterate_comm f m n lemma iterate_add_eq_iterate (hf : injective f) : f^[m + n] a = (f^[n] a) ↔ (f^[m] a) = a := iff.trans (by rw [←iterate_add_apply, nat.add_comm]) (hf.iterate n).eq_iff alias iterate_add_eq_iterate ↔ iterate_cancel_of_add _ lemma iterate_cancel (hf : injective f) (ha : f^[m] a = (f^[n] a)) : f^[m - n] a = a := begin cases le_total m n, { simp [nat.sub_eq_zero_of_le h] }, { exact iterate_cancel_of_add hf (by rwa nat.sub_add_cancel h) } end end function namespace list open function theorem foldl_const (f : α → α) (a : α) (l : list β) : l.foldl (λ b _, f b) a = (f^[l.length]) a := begin induction l with b l H generalizing a, { refl }, { rw [length_cons, foldl, iterate_succ_apply, H] } end theorem foldr_const (f : β → β) (b : β) : Π l : list α, l.foldr (λ _, f) b = (f^[l.length]) b | [] := rfl | (a::l) := by rw [length_cons, foldr, foldr_const l, iterate_succ_apply'] end list
fbdc4d90adde160129b81ede4584d5b0af1598b3
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/tests/lean/run/balg.lean
5222c35eb3e9cdfcec0ef842f24b51a6e15a63a7
[ "Apache-2.0" ]
permissive
collares/lean4
861a9269c4592bce49b71059e232ff0bfe4594cc
52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee
refs/heads/master
1,691,419,031,324
1,618,678,138,000
1,618,678,138,000
358,989,750
0
0
Apache-2.0
1,618,696,333,000
1,618,696,333,000
null
UTF-8
Lean
false
false
2,462
lean
import Lean universe u structure Magma where α : Type u mul : α → α → α instance : CoeSort Magma (Type u) where coe s := s.α abbrev mul {M : Magma} (a b : M) : M := M.mul a b set_option pp.all true infixl:70 (priority := high) "*" => mul structure Semigroup extends Magma where mul_assoc (a b c : α) : a * b * c = a * (b * c) instance : CoeSort Semigroup (Type u) where coe s := s.α structure CommSemigroup extends Semigroup where mul_comm (a b : α) : a * b = b * a structure Monoid extends Semigroup where one : α one_mul (a : α) : one * a = a mul_one (a : α) : a * one = a instance : CoeSort Monoid (Type u) where coe s := s.α structure CommMonoid extends Monoid where mul_comm (a b : α) : a * b = b * a instance : Coe CommMonoid CommSemigroup where coe s := { α := s.α mul := s.mul mul_assoc := s.mul_assoc mul_comm := s.mul_comm } structure Group extends Monoid where inv : α → α mul_left_inv (a : α) : (inv a) * a = one instance : CoeSort Group (Type u) where coe s := s.α abbrev inv {G : Group} (a : G) : G := G.inv a postfix:max "⁻¹" => inv instance (G : Group) : OfNat (coeSort G.toMagma) (nat_lit 1) where ofNat := G.one instance (G : Group) : OfNat (G.toMagma.α) (nat_lit 1) where ofNat := G.one structure CommGroup extends Group where mul_comm (a b : α) : a * b = b * a instance : CoeSort CommGroup (Type u) where coe s := s.α theorem inv_mul_cancel_left {G : Group} (a b : G) : a⁻¹ * (a * b) = b := by rw [← G.mul_assoc, G.mul_left_inv, G.one_mul] theorem toMonoidOneEq {G : Group} : G.toMonoid.one = 1 := rfl theorem inv_eq_of_mul_eq_one {G : Group} {a b : G} (h : a * b = 1) : a⁻¹ = b := by rw [← G.mul_one a⁻¹, toMonoidOneEq, ←h, ← G.mul_assoc, G.mul_left_inv, G.one_mul] theorem inv_inv {G : Group} (a : G) : (a⁻¹)⁻¹ = a := inv_eq_of_mul_eq_one (G.mul_left_inv a) theorem mul_right_inv {G : Group} (a : G) : a * a⁻¹ = 1 := by have a⁻¹⁻¹ * a⁻¹ = 1 by rw [G.mul_left_inv]; rfl rw [inv_inv] at this assumption unif_hint (G : Group) where |- G.toMonoid.one =?= 1 theorem mul_inv_rev {G : Group} (a b : G) : (a * b)⁻¹ = b⁻¹ * a⁻¹ := by apply inv_eq_of_mul_eq_one rw [G.mul_assoc, ← G.mul_assoc b, mul_right_inv, G.one_mul, mul_right_inv] theorem mul_inv {G : CommGroup} (a b : G) : (a * b)⁻¹ = a⁻¹ * b⁻¹ := by rw [mul_inv_rev, G.mul_comm]
9320373d2439ae9ebbc2b50895d23227d7b32471
bb31430994044506fa42fd667e2d556327e18dfe
/src/measure_theory/integral/bochner.lean
a21333f01c18a5742eccb8812858a55c4c7a77ba
[ "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
80,595
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov, Sébastien Gouëzel, Rémy Degenne -/ import measure_theory.integral.set_to_l1 /-! # Bochner integral The Bochner integral extends the definition of the Lebesgue integral to functions that map from a measure space into a Banach space (complete normed vector space). It is constructed here by extending the integral on simple functions. ## Main definitions The Bochner integral is defined through the extension process described in the file `set_to_L1`, which follows these steps: 1. Define the integral of the indicator of a set. This is `weighted_smul μ s x = (μ s).to_real * x`. `weighted_smul μ` is shown to be linear in the value `x` and `dominated_fin_meas_additive` (defined in the file `set_to_L1`) with respect to the set `s`. 2. Define the integral on simple functions of the type `simple_func α E` (notation : `α →ₛ E`) where `E` is a real normed space. (See `simple_func.integral` for details.) 3. Transfer this definition to define the integral on `L1.simple_func α E` (notation : `α →₁ₛ[μ] E`), see `L1.simple_func.integral`. Show that this integral is a continuous linear map from `α →₁ₛ[μ] E` to `E`. 4. Define the Bochner integral on L1 functions by extending the integral on integrable simple functions `α →₁ₛ[μ] E` using `continuous_linear_map.extend` and the fact that the embedding of `α →₁ₛ[μ] E` into `α →₁[μ] E` is dense. 5. Define the Bochner integral on functions as the Bochner integral of its equivalence class in L1 space, if it is in L1, and 0 otherwise. The result of that construction is `∫ a, f a ∂μ`, which is definitionally equal to `set_to_fun (dominated_fin_meas_additive_weighted_smul μ) f`. Some basic properties of the integral (like linearity) are particular cases of the properties of `set_to_fun` (which are described in the file `set_to_L1`). ## Main statements 1. Basic properties of the Bochner integral on functions of type `α → E`, where `α` is a measure space and `E` is a real normed space. * `integral_zero` : `∫ 0 ∂μ = 0` * `integral_add` : `∫ x, f x + g x ∂μ = ∫ x, f ∂μ + ∫ x, g x ∂μ` * `integral_neg` : `∫ x, - f x ∂μ = - ∫ x, f x ∂μ` * `integral_sub` : `∫ x, f x - g x ∂μ = ∫ x, f x ∂μ - ∫ x, g x ∂μ` * `integral_smul` : `∫ x, r • f x ∂μ = r • ∫ x, f x ∂μ` * `integral_congr_ae` : `f =ᵐ[μ] g → ∫ x, f x ∂μ = ∫ x, g x ∂μ` * `norm_integral_le_integral_norm` : `‖∫ x, f x ∂μ‖ ≤ ∫ x, ‖f x‖ ∂μ` 2. Basic properties of the Bochner integral on functions of type `α → ℝ`, where `α` is a measure space. * `integral_nonneg_of_ae` : `0 ≤ᵐ[μ] f → 0 ≤ ∫ x, f x ∂μ` * `integral_nonpos_of_ae` : `f ≤ᵐ[μ] 0 → ∫ x, f x ∂μ ≤ 0` * `integral_mono_ae` : `f ≤ᵐ[μ] g → ∫ x, f x ∂μ ≤ ∫ x, g x ∂μ` * `integral_nonneg` : `0 ≤ f → 0 ≤ ∫ x, f x ∂μ` * `integral_nonpos` : `f ≤ 0 → ∫ x, f x ∂μ ≤ 0` * `integral_mono` : `f ≤ᵐ[μ] g → ∫ x, f x ∂μ ≤ ∫ x, g x ∂μ` 3. Propositions connecting the Bochner integral with the integral on `ℝ≥0∞`-valued functions, which is called `lintegral` and has the notation `∫⁻`. * `integral_eq_lintegral_max_sub_lintegral_min` : `∫ x, f x ∂μ = ∫⁻ x, f⁺ x ∂μ - ∫⁻ x, f⁻ x ∂μ`, where `f⁺` is the positive part of `f` and `f⁻` is the negative part of `f`. * `integral_eq_lintegral_of_nonneg_ae` : `0 ≤ᵐ[μ] f → ∫ x, f x ∂μ = ∫⁻ x, f x ∂μ` 4. `tendsto_integral_of_dominated_convergence` : the Lebesgue dominated convergence theorem 5. (In the file `set_integral`) integration commutes with continuous linear maps. * `continuous_linear_map.integral_comp_comm` * `linear_isometry.integral_comp_comm` ## Notes Some tips on how to prove a proposition if the API for the Bochner integral is not enough so that you need to unfold the definition of the Bochner integral and go back to simple functions. One method is to use the theorem `integrable.induction` in the file `simple_func_dense_lp` (or one of the related results, like `Lp.induction` for functions in `Lp`), which allows you to prove something for an arbitrary integrable function. Another method is using the following steps. See `integral_eq_lintegral_max_sub_lintegral_min` for a complicated example, which proves that `∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, with the first integral sign being the Bochner integral of a real-valued function `f : α → ℝ`, and second and third integral sign being the integral on `ℝ≥0∞`-valued functions (called `lintegral`). The proof of `integral_eq_lintegral_max_sub_lintegral_min` is scattered in sections with the name `pos_part`. Here are the usual steps of proving that a property `p`, say `∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, holds for all functions : 1. First go to the `L¹` space. For example, if you see `ennreal.to_real (∫⁻ a, ennreal.of_real $ ‖f a‖)`, that is the norm of `f` in `L¹` space. Rewrite using `L1.norm_of_fun_eq_lintegral_norm`. 2. Show that the set `{f ∈ L¹ | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻}` is closed in `L¹` using `is_closed_eq`. 3. Show that the property holds for all simple functions `s` in `L¹` space. Typically, you need to convert various notions to their `simple_func` counterpart, using lemmas like `L1.integral_coe_eq_integral`. 4. Since simple functions are dense in `L¹`, ``` univ = closure {s simple} = closure {s simple | ∫ s = ∫⁻ s⁺ - ∫⁻ s⁻} : the property holds for all simple functions ⊆ closure {f | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻} = {f | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻} : closure of a closed set is itself ``` Use `is_closed_property` or `dense_range.induction_on` for this argument. ## Notations * `α →ₛ E` : simple functions (defined in `measure_theory/integration`) * `α →₁[μ] E` : functions in L1 space, i.e., equivalence classes of integrable functions (defined in `measure_theory/lp_space`) * `α →₁ₛ[μ] E` : simple functions in L1 space, i.e., equivalence classes of integrable simple functions (defined in `measure_theory/simple_func_dense`) * `∫ a, f a ∂μ` : integral of `f` with respect to a measure `μ` * `∫ a, f a` : integral of `f` with respect to `volume`, the default measure on the ambient type We also define notations for integral on a set, which are described in the file `measure_theory/set_integral`. Note : `ₛ` is typed using `\_s`. Sometimes it shows as a box if the font is missing. ## Tags Bochner integral, simple function, function space, Lebesgue dominated convergence theorem -/ noncomputable theory open_locale topological_space big_operators nnreal ennreal measure_theory open set filter topological_space ennreal emetric namespace measure_theory variables {α E F 𝕜 : Type*} section weighted_smul open continuous_linear_map variables [normed_add_comm_group F] [normed_space ℝ F] {m : measurable_space α} {μ : measure α} /-- Given a set `s`, return the continuous linear map `λ x, (μ s).to_real • x`. The extension of that set function through `set_to_L1` gives the Bochner integral of L1 functions. -/ def weighted_smul {m : measurable_space α} (μ : measure α) (s : set α) : F →L[ℝ] F := (μ s).to_real • (continuous_linear_map.id ℝ F) lemma weighted_smul_apply {m : measurable_space α} (μ : measure α) (s : set α) (x : F) : weighted_smul μ s x = (μ s).to_real • x := by simp [weighted_smul] @[simp] lemma weighted_smul_zero_measure {m : measurable_space α} : weighted_smul (0 : measure α) = (0 : set α → F →L[ℝ] F) := by { ext1, simp [weighted_smul], } @[simp] lemma weighted_smul_empty {m : measurable_space α} (μ : measure α) : weighted_smul μ ∅ = (0 : F →L[ℝ] F) := by { ext1 x, rw [weighted_smul_apply], simp, } lemma weighted_smul_add_measure {m : measurable_space α} (μ ν : measure α) {s : set α} (hμs : μ s ≠ ∞) (hνs : ν s ≠ ∞) : (weighted_smul (μ + ν) s : F →L[ℝ] F) = weighted_smul μ s + weighted_smul ν s := begin ext1 x, push_cast, simp_rw [pi.add_apply, weighted_smul_apply], push_cast, rw [pi.add_apply, ennreal.to_real_add hμs hνs, add_smul], end lemma weighted_smul_smul_measure {m : measurable_space α} (μ : measure α) (c : ℝ≥0∞) {s : set α} : (weighted_smul (c • μ) s : F →L[ℝ] F) = c.to_real • weighted_smul μ s := begin ext1 x, push_cast, simp_rw [pi.smul_apply, weighted_smul_apply], push_cast, simp_rw [pi.smul_apply, smul_eq_mul, to_real_mul, smul_smul], end lemma weighted_smul_congr (s t : set α) (hst : μ s = μ t) : (weighted_smul μ s : F →L[ℝ] F) = weighted_smul μ t := by { ext1 x, simp_rw weighted_smul_apply, congr' 2, } lemma weighted_smul_null {s : set α} (h_zero : μ s = 0) : (weighted_smul μ s : F →L[ℝ] F) = 0 := by { ext1 x, rw [weighted_smul_apply, h_zero], simp, } lemma weighted_smul_union' (s t : set α) (ht : measurable_set t) (hs_finite : μ s ≠ ∞) (ht_finite : μ t ≠ ∞) (h_inter : s ∩ t = ∅) : (weighted_smul μ (s ∪ t) : F →L[ℝ] F) = weighted_smul μ s + weighted_smul μ t := begin ext1 x, simp_rw [add_apply, weighted_smul_apply, measure_union (set.disjoint_iff_inter_eq_empty.mpr h_inter) ht, ennreal.to_real_add hs_finite ht_finite, add_smul], end @[nolint unused_arguments] lemma weighted_smul_union (s t : set α) (hs : measurable_set s) (ht : measurable_set t) (hs_finite : μ s ≠ ∞) (ht_finite : μ t ≠ ∞) (h_inter : s ∩ t = ∅) : (weighted_smul μ (s ∪ t) : F →L[ℝ] F) = weighted_smul μ s + weighted_smul μ t := weighted_smul_union' s t ht hs_finite ht_finite h_inter lemma weighted_smul_smul [normed_field 𝕜] [normed_space 𝕜 F] [smul_comm_class ℝ 𝕜 F] (c : 𝕜) (s : set α) (x : F) : weighted_smul μ s (c • x) = c • weighted_smul μ s x := by { simp_rw [weighted_smul_apply, smul_comm], } lemma norm_weighted_smul_le (s : set α) : ‖(weighted_smul μ s : F →L[ℝ] F)‖ ≤ (μ s).to_real := calc ‖(weighted_smul μ s : F →L[ℝ] F)‖ = ‖(μ s).to_real‖ * ‖continuous_linear_map.id ℝ F‖ : norm_smul _ _ ... ≤ ‖(μ s).to_real‖ : (mul_le_mul_of_nonneg_left norm_id_le (norm_nonneg _)).trans (mul_one _).le ... = abs (μ s).to_real : real.norm_eq_abs _ ... = (μ s).to_real : abs_eq_self.mpr ennreal.to_real_nonneg lemma dominated_fin_meas_additive_weighted_smul {m : measurable_space α} (μ : measure α) : dominated_fin_meas_additive μ (weighted_smul μ : set α → F →L[ℝ] F) 1 := ⟨weighted_smul_union, λ s _ _, (norm_weighted_smul_le s).trans (one_mul _).symm.le⟩ lemma weighted_smul_nonneg (s : set α) (x : ℝ) (hx : 0 ≤ x) : 0 ≤ weighted_smul μ s x := begin simp only [weighted_smul, algebra.id.smul_eq_mul, coe_smul', id.def, coe_id', pi.smul_apply], exact mul_nonneg to_real_nonneg hx, end end weighted_smul local infixr ` →ₛ `:25 := simple_func namespace simple_func section pos_part variables [linear_order E] [has_zero E] [measurable_space α] /-- Positive part of a simple function. -/ def pos_part (f : α →ₛ E) : α →ₛ E := f.map (λ b, max b 0) /-- Negative part of a simple function. -/ def neg_part [has_neg E] (f : α →ₛ E) : α →ₛ E := pos_part (-f) lemma pos_part_map_norm (f : α →ₛ ℝ) : (pos_part f).map norm = pos_part f := by { ext, rw [map_apply, real.norm_eq_abs, abs_of_nonneg], exact le_max_right _ _ } lemma neg_part_map_norm (f : α →ₛ ℝ) : (neg_part f).map norm = neg_part f := by { rw neg_part, exact pos_part_map_norm _ } lemma pos_part_sub_neg_part (f : α →ₛ ℝ) : f.pos_part - f.neg_part = f := begin simp only [pos_part, neg_part], ext a, rw coe_sub, exact max_zero_sub_eq_self (f a) end end pos_part section integral /-! ### The Bochner integral of simple functions Define the Bochner integral of simple functions of the type `α →ₛ β` where `β` is a normed group, and prove basic property of this integral. -/ open finset variables [normed_add_comm_group E] [normed_add_comm_group F] [normed_space ℝ F] {p : ℝ≥0∞} {G F' : Type*} [normed_add_comm_group G] [normed_add_comm_group F'] [normed_space ℝ F'] {m : measurable_space α} {μ : measure α} /-- Bochner integral of simple functions whose codomain is a real `normed_space`. This is equal to `∑ x in f.range, (μ (f ⁻¹' {x})).to_real • x` (see `integral_eq`). -/ def integral {m : measurable_space α} (μ : measure α) (f : α →ₛ F) : F := f.set_to_simple_func (weighted_smul μ) lemma integral_def {m : measurable_space α} (μ : measure α) (f : α →ₛ F) : f.integral μ = f.set_to_simple_func (weighted_smul μ) := rfl lemma integral_eq {m : measurable_space α} (μ : measure α) (f : α →ₛ F) : f.integral μ = ∑ x in f.range, (μ (f ⁻¹' {x})).to_real • x := by simp [integral, set_to_simple_func, weighted_smul_apply] lemma integral_eq_sum_filter [decidable_pred (λ x : F, x ≠ 0)] {m : measurable_space α} (f : α →ₛ F) (μ : measure α) : f.integral μ = ∑ x in f.range.filter (λ x, x ≠ 0), (μ (f ⁻¹' {x})).to_real • x := by { rw [integral_def, set_to_simple_func_eq_sum_filter], simp_rw weighted_smul_apply, congr } /-- The Bochner integral is equal to a sum over any set that includes `f.range` (except `0`). -/ lemma integral_eq_sum_of_subset [decidable_pred (λ x : F, x ≠ 0)] {f : α →ₛ F} {s : finset F} (hs : f.range.filter (λ x, x ≠ 0) ⊆ s) : f.integral μ = ∑ x in s, (μ (f ⁻¹' {x})).to_real • x := begin rw [simple_func.integral_eq_sum_filter, finset.sum_subset hs], rintro x - hx, rw [finset.mem_filter, not_and_distrib, ne.def, not_not] at hx, rcases hx with hx|rfl; [skip, simp], rw [simple_func.mem_range] at hx, rw [preimage_eq_empty]; simp [set.disjoint_singleton_left, hx] end @[simp] lemma integral_const {m : measurable_space α} (μ : measure α) (y : F) : (const α y).integral μ = (μ univ).to_real • y := by classical; calc (const α y).integral μ = ∑ z in {y}, (μ ((const α y) ⁻¹' {z})).to_real • z : integral_eq_sum_of_subset $ (filter_subset _ _).trans (range_const_subset _ _) ... = (μ univ).to_real • y : by simp @[simp] lemma integral_piecewise_zero {m : measurable_space α} (f : α →ₛ F) (μ : measure α) {s : set α} (hs : measurable_set s) : (piecewise s hs f 0).integral μ = f.integral (μ.restrict s) := begin classical, refine (integral_eq_sum_of_subset _).trans ((sum_congr rfl $ λ y hy, _).trans (integral_eq_sum_filter _ _).symm), { intros y hy, simp only [mem_filter, mem_range, coe_piecewise, coe_zero, piecewise_eq_indicator, mem_range_indicator] at *, rcases hy with ⟨⟨rfl, -⟩|⟨x, hxs, rfl⟩, h₀⟩, exacts [(h₀ rfl).elim, ⟨set.mem_range_self _, h₀⟩] }, { dsimp, rw [set.piecewise_eq_indicator, indicator_preimage_of_not_mem, measure.restrict_apply (f.measurable_set_preimage _)], exact λ h₀, (mem_filter.1 hy).2 (eq.symm h₀) } end /-- Calculate the integral of `g ∘ f : α →ₛ F`, where `f` is an integrable function from `α` to `E` and `g` is a function from `E` to `F`. We require `g 0 = 0` so that `g ∘ f` is integrable. -/ lemma map_integral (f : α →ₛ E) (g : E → F) (hf : integrable f μ) (hg : g 0 = 0) : (f.map g).integral μ = ∑ x in f.range, (ennreal.to_real (μ (f ⁻¹' {x}))) • (g x) := map_set_to_simple_func _ weighted_smul_union hf hg /-- `simple_func.integral` and `simple_func.lintegral` agree when the integrand has type `α →ₛ ℝ≥0∞`. But since `ℝ≥0∞` is not a `normed_space`, we need some form of coercion. See `integral_eq_lintegral` for a simpler version. -/ lemma integral_eq_lintegral' {f : α →ₛ E} {g : E → ℝ≥0∞} (hf : integrable f μ) (hg0 : g 0 = 0) (ht : ∀ b, g b ≠ ∞) : (f.map (ennreal.to_real ∘ g)).integral μ = ennreal.to_real (∫⁻ a, g (f a) ∂μ) := begin have hf' : f.fin_meas_supp μ := integrable_iff_fin_meas_supp.1 hf, simp only [← map_apply g f, lintegral_eq_lintegral], rw [map_integral f _ hf, map_lintegral, ennreal.to_real_sum], { refine finset.sum_congr rfl (λb hb, _), rw [smul_eq_mul, to_real_mul, mul_comm] }, { assume a ha, by_cases a0 : a = 0, { rw [a0, hg0, zero_mul], exact with_top.zero_ne_top }, { apply mul_ne_top (ht a) (hf'.meas_preimage_singleton_ne_zero a0).ne } }, { simp [hg0] } end variables [normed_field 𝕜] [normed_space 𝕜 E] [normed_space ℝ E] [smul_comm_class ℝ 𝕜 E] lemma integral_congr {f g : α →ₛ E} (hf : integrable f μ) (h : f =ᵐ[μ] g) : f.integral μ = g.integral μ := set_to_simple_func_congr (weighted_smul μ) (λ s hs, weighted_smul_null) weighted_smul_union hf h /-- `simple_func.bintegral` and `simple_func.integral` agree when the integrand has type `α →ₛ ℝ≥0∞`. But since `ℝ≥0∞` is not a `normed_space`, we need some form of coercion. -/ lemma integral_eq_lintegral {f : α →ₛ ℝ} (hf : integrable f μ) (h_pos : 0 ≤ᵐ[μ] f) : f.integral μ = ennreal.to_real (∫⁻ a, ennreal.of_real (f a) ∂μ) := begin have : f =ᵐ[μ] f.map (ennreal.to_real ∘ ennreal.of_real) := h_pos.mono (λ a h, (ennreal.to_real_of_real h).symm), rw [← integral_eq_lintegral' hf], exacts [integral_congr hf this, ennreal.of_real_zero, λ b, ennreal.of_real_ne_top] end lemma integral_add {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) : integral μ (f + g) = integral μ f + integral μ g := set_to_simple_func_add _ weighted_smul_union hf hg lemma integral_neg {f : α →ₛ E} (hf : integrable f μ) : integral μ (-f) = - integral μ f := set_to_simple_func_neg _ weighted_smul_union hf lemma integral_sub {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) : integral μ (f - g) = integral μ f - integral μ g := set_to_simple_func_sub _ weighted_smul_union hf hg lemma integral_smul (c : 𝕜) {f : α →ₛ E} (hf : integrable f μ) : integral μ (c • f) = c • integral μ f := set_to_simple_func_smul _ weighted_smul_union weighted_smul_smul c hf lemma norm_set_to_simple_func_le_integral_norm (T : set α → E →L[ℝ] F) {C : ℝ} (hT_norm : ∀ s, measurable_set s → μ s < ∞ → ‖T s‖ ≤ C * (μ s).to_real) {f : α →ₛ E} (hf : integrable f μ) : ‖f.set_to_simple_func T‖ ≤ C * (f.map norm).integral μ := calc ‖f.set_to_simple_func T‖ ≤ C * ∑ x in f.range, ennreal.to_real (μ (f ⁻¹' {x})) * ‖x‖ : norm_set_to_simple_func_le_sum_mul_norm_of_integrable T hT_norm f hf ... = C * (f.map norm).integral μ : by { rw map_integral f norm hf norm_zero, simp_rw smul_eq_mul, } lemma norm_integral_le_integral_norm (f : α →ₛ E) (hf : integrable f μ) : ‖f.integral μ‖ ≤ (f.map norm).integral μ := begin refine (norm_set_to_simple_func_le_integral_norm _ (λ s _ _, _) hf).trans (one_mul _).le, exact (norm_weighted_smul_le s).trans (one_mul _).symm.le, end lemma integral_add_measure {ν} (f : α →ₛ E) (hf : integrable f (μ + ν)) : f.integral (μ + ν) = f.integral μ + f.integral ν := begin simp_rw [integral_def], refine set_to_simple_func_add_left' (weighted_smul μ) (weighted_smul ν) (weighted_smul (μ + ν)) (λ s hs hμνs, _) hf, rw [lt_top_iff_ne_top, measure.coe_add, pi.add_apply, ennreal.add_ne_top] at hμνs, rw weighted_smul_add_measure _ _ hμνs.1 hμνs.2, end end integral end simple_func namespace L1 open ae_eq_fun Lp.simple_func Lp variables [normed_add_comm_group E] [normed_add_comm_group F] {m : measurable_space α} {μ : measure α} variables {α E μ} namespace simple_func lemma norm_eq_integral (f : α →₁ₛ[μ] E) : ‖f‖ = ((to_simple_func f).map norm).integral μ := begin rw [norm_eq_sum_mul f, (to_simple_func f).map_integral norm (simple_func.integrable f) norm_zero], simp_rw smul_eq_mul, end section pos_part /-- Positive part of a simple function in L1 space. -/ def pos_part (f : α →₁ₛ[μ] ℝ) : α →₁ₛ[μ] ℝ := ⟨Lp.pos_part (f : α →₁[μ] ℝ), begin rcases f with ⟨f, s, hsf⟩, use s.pos_part, simp only [subtype.coe_mk, Lp.coe_pos_part, ← hsf, ae_eq_fun.pos_part_mk, simple_func.pos_part, simple_func.coe_map, mk_eq_mk], end ⟩ /-- Negative part of a simple function in L1 space. -/ def neg_part (f : α →₁ₛ[μ] ℝ) : α →₁ₛ[μ] ℝ := pos_part (-f) @[norm_cast] lemma coe_pos_part (f : α →₁ₛ[μ] ℝ) : (pos_part f : α →₁[μ] ℝ) = Lp.pos_part (f : α →₁[μ] ℝ) := rfl @[norm_cast] lemma coe_neg_part (f : α →₁ₛ[μ] ℝ) : (neg_part f : α →₁[μ] ℝ) = Lp.neg_part (f : α →₁[μ] ℝ) := rfl end pos_part section simple_func_integral /-! ### The Bochner integral of `L1` Define the Bochner integral on `α →₁ₛ[μ] E` by extension from the simple functions `α →₁ₛ[μ] E`, and prove basic properties of this integral. -/ variables [normed_field 𝕜] [normed_space 𝕜 E] [normed_space ℝ E] [smul_comm_class ℝ 𝕜 E] {F' : Type*} [normed_add_comm_group F'] [normed_space ℝ F'] local attribute [instance] simple_func.normed_space /-- The Bochner integral over simple functions in L1 space. -/ def integral (f : α →₁ₛ[μ] E) : E := ((to_simple_func f)).integral μ lemma integral_eq_integral (f : α →₁ₛ[μ] E) : integral f = ((to_simple_func f)).integral μ := rfl lemma integral_eq_lintegral {f : α →₁ₛ[μ] ℝ} (h_pos : 0 ≤ᵐ[μ] (to_simple_func f)) : integral f = ennreal.to_real (∫⁻ a, ennreal.of_real ((to_simple_func f) a) ∂μ) := by rw [integral, simple_func.integral_eq_lintegral (simple_func.integrable f) h_pos] lemma integral_eq_set_to_L1s (f : α →₁ₛ[μ] E) : integral f = set_to_L1s (weighted_smul μ) f := rfl lemma integral_congr {f g : α →₁ₛ[μ] E} (h : to_simple_func f =ᵐ[μ] to_simple_func g) : integral f = integral g := simple_func.integral_congr (simple_func.integrable f) h lemma integral_add (f g : α →₁ₛ[μ] E) : integral (f + g) = integral f + integral g := set_to_L1s_add _ (λ _ _, weighted_smul_null) weighted_smul_union _ _ lemma integral_smul (c : 𝕜) (f : α →₁ₛ[μ] E) : integral (c • f) = c • integral f := set_to_L1s_smul _ (λ _ _, weighted_smul_null) weighted_smul_union weighted_smul_smul c f lemma norm_integral_le_norm (f : α →₁ₛ[μ] E) : ‖integral f‖ ≤ ‖f‖ := begin rw [integral, norm_eq_integral], exact (to_simple_func f).norm_integral_le_integral_norm (simple_func.integrable f) end variables {E' : Type*} [normed_add_comm_group E'] [normed_space ℝ E'] [normed_space 𝕜 E'] variables (α E μ 𝕜) /-- The Bochner integral over simple functions in L1 space as a continuous linear map. -/ def integral_clm' : (α →₁ₛ[μ] E) →L[𝕜] E := linear_map.mk_continuous ⟨integral, integral_add, integral_smul⟩ 1 (λf, le_trans (norm_integral_le_norm _) $ by rw one_mul) /-- The Bochner integral over simple functions in L1 space as a continuous linear map over ℝ. -/ def integral_clm : (α →₁ₛ[μ] E) →L[ℝ] E := integral_clm' α E ℝ μ variables {α E μ 𝕜} local notation (name := simple_func.integral_clm) `Integral` := integral_clm α E μ open continuous_linear_map lemma norm_Integral_le_one : ‖Integral‖ ≤ 1 := linear_map.mk_continuous_norm_le _ (zero_le_one) _ section pos_part lemma pos_part_to_simple_func (f : α →₁ₛ[μ] ℝ) : to_simple_func (pos_part f) =ᵐ[μ] (to_simple_func f).pos_part := begin have eq : ∀ a, (to_simple_func f).pos_part a = max ((to_simple_func f) a) 0 := λa, rfl, have ae_eq : ∀ᵐ a ∂μ, to_simple_func (pos_part f) a = max ((to_simple_func f) a) 0, { filter_upwards [to_simple_func_eq_to_fun (pos_part f), Lp.coe_fn_pos_part (f : α →₁[μ] ℝ), to_simple_func_eq_to_fun f] with _ _ h₂ _, convert h₂, }, refine ae_eq.mono (assume a h, _), rw [h, eq], end lemma neg_part_to_simple_func (f : α →₁ₛ[μ] ℝ) : to_simple_func (neg_part f) =ᵐ[μ] (to_simple_func f).neg_part := begin rw [simple_func.neg_part, measure_theory.simple_func.neg_part], filter_upwards [pos_part_to_simple_func (-f), neg_to_simple_func f], assume a h₁ h₂, rw h₁, show max _ _ = max _ _, rw h₂, refl end lemma integral_eq_norm_pos_part_sub (f : α →₁ₛ[μ] ℝ) : integral f = ‖pos_part f‖ - ‖neg_part f‖ := begin -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq₁ : (to_simple_func f).pos_part =ᵐ[μ] (to_simple_func (pos_part f)).map norm, { filter_upwards [pos_part_to_simple_func f] with _ h, rw [simple_func.map_apply, h], conv_lhs { rw [← simple_func.pos_part_map_norm, simple_func.map_apply] } }, -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq₂ : (to_simple_func f).neg_part =ᵐ[μ] (to_simple_func (neg_part f)).map norm, { filter_upwards [neg_part_to_simple_func f] with _ h, rw [simple_func.map_apply, h], conv_lhs { rw [← simple_func.neg_part_map_norm, simple_func.map_apply], }, }, -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq : ∀ᵐ a ∂μ, (to_simple_func f).pos_part a - (to_simple_func f).neg_part a = (to_simple_func (pos_part f)).map norm a - (to_simple_func (neg_part f)).map norm a, { filter_upwards [ae_eq₁, ae_eq₂] with _ h₁ h₂, rw [h₁, h₂], }, rw [integral, norm_eq_integral, norm_eq_integral, ← simple_func.integral_sub], { show (to_simple_func f).integral μ = ((to_simple_func (pos_part f)).map norm - (to_simple_func (neg_part f)).map norm).integral μ, apply measure_theory.simple_func.integral_congr (simple_func.integrable f), filter_upwards [ae_eq₁, ae_eq₂] with _ h₁ h₂, show _ = _ - _, rw [← h₁, ← h₂], have := (to_simple_func f).pos_part_sub_neg_part, conv_lhs {rw ← this}, refl, }, { exact (simple_func.integrable f).pos_part.congr ae_eq₁ }, { exact (simple_func.integrable f).neg_part.congr ae_eq₂ } end end pos_part end simple_func_integral end simple_func open simple_func local notation (name := simple_func.integral_clm) `Integral` := @integral_clm α E _ _ _ _ _ μ _ variables [normed_space ℝ E] [nontrivially_normed_field 𝕜] [normed_space 𝕜 E] [smul_comm_class ℝ 𝕜 E] [normed_space ℝ F] [complete_space E] section integration_in_L1 local attribute [instance] simple_func.normed_space open continuous_linear_map variables (𝕜) /-- The Bochner integral in L1 space as a continuous linear map. -/ def integral_clm' : (α →₁[μ] E) →L[𝕜] E := (integral_clm' α E 𝕜 μ).extend (coe_to_Lp α E 𝕜) (simple_func.dense_range one_ne_top) simple_func.uniform_inducing variables {𝕜} /-- The Bochner integral in L1 space as a continuous linear map over ℝ. -/ def integral_clm : (α →₁[μ] E) →L[ℝ] E := integral_clm' ℝ /-- The Bochner integral in L1 space -/ def integral (f : α →₁[μ] E) : E := integral_clm f lemma integral_eq (f : α →₁[μ] E) : integral f = integral_clm f := rfl lemma integral_eq_set_to_L1 (f : α →₁[μ] E) : integral f = set_to_L1 (dominated_fin_meas_additive_weighted_smul μ) f := rfl @[norm_cast] lemma simple_func.integral_L1_eq_integral (f : α →₁ₛ[μ] E) : integral (f : α →₁[μ] E) = (simple_func.integral f) := set_to_L1_eq_set_to_L1s_clm (dominated_fin_meas_additive_weighted_smul μ) f variables (α E) @[simp] lemma integral_zero : integral (0 : α →₁[μ] E) = 0 := map_zero integral_clm variables {α E} lemma integral_add (f g : α →₁[μ] E) : integral (f + g) = integral f + integral g := map_add integral_clm f g lemma integral_neg (f : α →₁[μ] E) : integral (-f) = - integral f := map_neg integral_clm f lemma integral_sub (f g : α →₁[μ] E) : integral (f - g) = integral f - integral g := map_sub integral_clm f g lemma integral_smul (c : 𝕜) (f : α →₁[μ] E) : integral (c • f) = c • integral f := show (integral_clm' 𝕜) (c • f) = c • (integral_clm' 𝕜) f, from map_smul (integral_clm' 𝕜) c f local notation (name := integral_clm) `Integral` := @integral_clm α E _ _ μ _ _ local notation (name := simple_func.integral_clm') `sIntegral` := @simple_func.integral_clm α E _ _ μ _ lemma norm_Integral_le_one : ‖Integral‖ ≤ 1 := norm_set_to_L1_le (dominated_fin_meas_additive_weighted_smul μ) zero_le_one lemma norm_integral_le (f : α →₁[μ] E) : ‖integral f‖ ≤ ‖f‖ := calc ‖integral f‖ = ‖Integral f‖ : rfl ... ≤ ‖Integral‖ * ‖f‖ : le_op_norm _ _ ... ≤ 1 * ‖f‖ : mul_le_mul_of_nonneg_right norm_Integral_le_one $ norm_nonneg _ ... = ‖f‖ : one_mul _ @[continuity] lemma continuous_integral : continuous (λ (f : α →₁[μ] E), integral f) := L1.integral_clm.continuous section pos_part lemma integral_eq_norm_pos_part_sub (f : α →₁[μ] ℝ) : integral f = ‖Lp.pos_part f‖ - ‖Lp.neg_part f‖ := begin -- Use `is_closed_property` and `is_closed_eq` refine @is_closed_property _ _ _ (coe : (α →₁ₛ[μ] ℝ) → (α →₁[μ] ℝ)) (λ f : α →₁[μ] ℝ, integral f = ‖Lp.pos_part f‖ - ‖Lp.neg_part f‖) (simple_func.dense_range one_ne_top) (is_closed_eq _ _) _ f, { exact cont _ }, { refine continuous.sub (continuous_norm.comp Lp.continuous_pos_part) (continuous_norm.comp Lp.continuous_neg_part) }, -- Show that the property holds for all simple functions in the `L¹` space. { assume s, norm_cast, exact simple_func.integral_eq_norm_pos_part_sub _ } end end pos_part end integration_in_L1 end L1 /-! ## The Bochner integral on functions Define the Bochner integral on functions generally to be the `L1` Bochner integral, for integrable functions, and 0 otherwise; prove its basic properties. -/ variables [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] [nontrivially_normed_field 𝕜] [normed_space 𝕜 E] [smul_comm_class ℝ 𝕜 E] [normed_add_comm_group F] [normed_space ℝ F] [complete_space F] section open_locale classical /-- The Bochner integral -/ def integral {m : measurable_space α} (μ : measure α) (f : α → E) : E := if hf : integrable f μ then L1.integral (hf.to_L1 f) else 0 end /-! In the notation for integrals, an expression like `∫ x, g ‖x‖ ∂μ` will not be parsed correctly, and needs parentheses. We do not set the binding power of `r` to `0`, because then `∫ x, f x = 0` will be parsed incorrectly. -/ notation `∫` binders `, ` r:(scoped:60 f, f) ` ∂` μ:70 := integral μ r notation `∫` binders `, ` r:(scoped:60 f, integral volume f) := r notation `∫` binders ` in ` s `, ` r:(scoped:60 f, f) ` ∂` μ:70 := integral (measure.restrict μ s) r notation `∫` binders ` in ` s `, ` r:(scoped:60 f, integral (measure.restrict volume s) f) := r section properties open continuous_linear_map measure_theory.simple_func variables {f g : α → E} {m : measurable_space α} {μ : measure α} lemma integral_eq (f : α → E) (hf : integrable f μ) : ∫ a, f a ∂μ = L1.integral (hf.to_L1 f) := @dif_pos _ (id _) hf _ _ _ lemma integral_eq_set_to_fun (f : α → E) : ∫ a, f a ∂μ = set_to_fun μ (weighted_smul μ) (dominated_fin_meas_additive_weighted_smul μ) f := rfl lemma L1.integral_eq_integral (f : α →₁[μ] E) : L1.integral f = ∫ a, f a ∂μ := (L1.set_to_fun_eq_set_to_L1 (dominated_fin_meas_additive_weighted_smul μ) f).symm lemma integral_undef (h : ¬ integrable f μ) : ∫ a, f a ∂μ = 0 := @dif_neg _ (id _) h _ _ _ lemma integral_non_ae_strongly_measurable (h : ¬ ae_strongly_measurable f μ) : ∫ a, f a ∂μ = 0 := integral_undef $ not_and_of_not_left _ h variables (α E) lemma integral_zero : ∫ a : α, (0:E) ∂μ = 0 := set_to_fun_zero (dominated_fin_meas_additive_weighted_smul μ) @[simp] lemma integral_zero' : integral μ (0 : α → E) = 0 := integral_zero α E variables {α E} lemma integral_add (hf : integrable f μ) (hg : integrable g μ) : ∫ a, f a + g a ∂μ = ∫ a, f a ∂μ + ∫ a, g a ∂μ := set_to_fun_add (dominated_fin_meas_additive_weighted_smul μ) hf hg lemma integral_add' (hf : integrable f μ) (hg : integrable g μ) : ∫ a, (f + g) a ∂μ = ∫ a, f a ∂μ + ∫ a, g a ∂μ := integral_add hf hg lemma integral_finset_sum {ι} (s : finset ι) {f : ι → α → E} (hf : ∀ i ∈ s, integrable (f i) μ) : ∫ a, ∑ i in s, f i a ∂μ = ∑ i in s, ∫ a, f i a ∂μ := set_to_fun_finset_sum (dominated_fin_meas_additive_weighted_smul _) s hf lemma integral_neg (f : α → E) : ∫ a, -f a ∂μ = - ∫ a, f a ∂μ := set_to_fun_neg (dominated_fin_meas_additive_weighted_smul μ) f lemma integral_neg' (f : α → E) : ∫ a, (-f) a ∂μ = - ∫ a, f a ∂μ := integral_neg f lemma integral_sub (hf : integrable f μ) (hg : integrable g μ) : ∫ a, f a - g a ∂μ = ∫ a, f a ∂μ - ∫ a, g a ∂μ := set_to_fun_sub (dominated_fin_meas_additive_weighted_smul μ) hf hg lemma integral_sub' (hf : integrable f μ) (hg : integrable g μ) : ∫ a, (f - g) a ∂μ = ∫ a, f a ∂μ - ∫ a, g a ∂μ := integral_sub hf hg lemma integral_smul (c : 𝕜) (f : α → E) : ∫ a, c • (f a) ∂μ = c • ∫ a, f a ∂μ := set_to_fun_smul (dominated_fin_meas_additive_weighted_smul μ) weighted_smul_smul c f lemma integral_mul_left {L : Type*} [is_R_or_C L] (r : L) (f : α → L) : ∫ a, r * (f a) ∂μ = r * ∫ a, f a ∂μ := integral_smul r f lemma integral_mul_right {L : Type*} [is_R_or_C L] (r : L) (f : α → L) : ∫ a, (f a) * r ∂μ = ∫ a, f a ∂μ * r := by { simp only [mul_comm], exact integral_mul_left r f } lemma integral_div {L : Type*} [is_R_or_C L] (r : L) (f : α → L) : ∫ a, (f a) / r ∂μ = ∫ a, f a ∂μ / r := by simpa only [←div_eq_mul_inv] using integral_mul_right r⁻¹ f lemma integral_congr_ae (h : f =ᵐ[μ] g) : ∫ a, f a ∂μ = ∫ a, g a ∂μ := set_to_fun_congr_ae (dominated_fin_meas_additive_weighted_smul μ) h @[simp] lemma L1.integral_of_fun_eq_integral {f : α → E} (hf : integrable f μ) : ∫ a, (hf.to_L1 f) a ∂μ = ∫ a, f a ∂μ := set_to_fun_to_L1 (dominated_fin_meas_additive_weighted_smul μ) hf @[continuity] lemma continuous_integral : continuous (λ (f : α →₁[μ] E), ∫ a, f a ∂μ) := continuous_set_to_fun (dominated_fin_meas_additive_weighted_smul μ) lemma norm_integral_le_lintegral_norm (f : α → E) : ‖∫ a, f a ∂μ‖ ≤ ennreal.to_real (∫⁻ a, (ennreal.of_real ‖f a‖) ∂μ) := begin by_cases hf : integrable f μ, { rw [integral_eq f hf, ← integrable.norm_to_L1_eq_lintegral_norm f hf], exact L1.norm_integral_le _ }, { rw [integral_undef hf, norm_zero], exact to_real_nonneg } end lemma ennnorm_integral_le_lintegral_ennnorm (f : α → E) : (‖∫ a, f a ∂μ‖₊ : ℝ≥0∞) ≤ ∫⁻ a, ‖f a‖₊ ∂μ := by { simp_rw [← of_real_norm_eq_coe_nnnorm], apply ennreal.of_real_le_of_le_to_real, exact norm_integral_le_lintegral_norm f } lemma integral_eq_zero_of_ae {f : α → E} (hf : f =ᵐ[μ] 0) : ∫ a, f a ∂μ = 0 := by simp [integral_congr_ae hf, integral_zero] /-- If `f` has finite integral, then `∫ x in s, f x ∂μ` is absolutely continuous in `s`: it tends to zero as `μ s` tends to zero. -/ lemma has_finite_integral.tendsto_set_integral_nhds_zero {ι} {f : α → E} (hf : has_finite_integral f μ) {l : filter ι} {s : ι → set α} (hs : tendsto (μ ∘ s) l (𝓝 0)) : tendsto (λ i, ∫ x in s i, f x ∂μ) l (𝓝 0) := begin rw [tendsto_zero_iff_norm_tendsto_zero], simp_rw [← coe_nnnorm, ← nnreal.coe_zero, nnreal.tendsto_coe, ← ennreal.tendsto_coe, ennreal.coe_zero], exact tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds (tendsto_set_lintegral_zero (ne_of_lt hf) hs) (λ i, zero_le _) (λ i, ennnorm_integral_le_lintegral_ennnorm _) end /-- If `f` is integrable, then `∫ x in s, f x ∂μ` is absolutely continuous in `s`: it tends to zero as `μ s` tends to zero. -/ lemma integrable.tendsto_set_integral_nhds_zero {ι} {f : α → E} (hf : integrable f μ) {l : filter ι} {s : ι → set α} (hs : tendsto (μ ∘ s) l (𝓝 0)) : tendsto (λ i, ∫ x in s i, f x ∂μ) l (𝓝 0) := hf.2.tendsto_set_integral_nhds_zero hs /-- If `F i → f` in `L1`, then `∫ x, F i x ∂μ → ∫ x, f x ∂μ`. -/ lemma tendsto_integral_of_L1 {ι} (f : α → E) (hfi : integrable f μ) {F : ι → α → E} {l : filter ι} (hFi : ∀ᶠ i in l, integrable (F i) μ) (hF : tendsto (λ i, ∫⁻ x, ‖F i x - f x‖₊ ∂μ) l (𝓝 0)) : tendsto (λ i, ∫ x, F i x ∂μ) l (𝓝 $ ∫ x, f x ∂μ) := tendsto_set_to_fun_of_L1 (dominated_fin_meas_additive_weighted_smul μ) f hfi hFi hF /-- Lebesgue dominated convergence theorem provides sufficient conditions under which almost everywhere convergence of a sequence of functions implies the convergence of their integrals. We could weaken the condition `bound_integrable` to require `has_finite_integral bound μ` instead (i.e. not requiring that `bound` is measurable), but in all applications proving integrability is easier. -/ theorem tendsto_integral_of_dominated_convergence {F : ℕ → α → E} {f : α → E} (bound : α → ℝ) (F_measurable : ∀ n, ae_strongly_measurable (F n) μ) (bound_integrable : integrable bound μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ‖F n a‖ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫ a, F n a ∂μ) at_top (𝓝 $ ∫ a, f a ∂μ) := tendsto_set_to_fun_of_dominated_convergence (dominated_fin_meas_additive_weighted_smul μ) bound F_measurable bound_integrable h_bound h_lim /-- Lebesgue dominated convergence theorem for filters with a countable basis -/ lemma tendsto_integral_filter_of_dominated_convergence {ι} {l : filter ι} [l.is_countably_generated] {F : ι → α → E} {f : α → E} (bound : α → ℝ) (hF_meas : ∀ᶠ n in l, ae_strongly_measurable (F n) μ) (h_bound : ∀ᶠ n in l, ∀ᵐ a ∂μ, ‖F n a‖ ≤ bound a) (bound_integrable : integrable bound μ) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) l (𝓝 (f a))) : tendsto (λn, ∫ a, F n a ∂μ) l (𝓝 $ ∫ a, f a ∂μ) := tendsto_set_to_fun_filter_of_dominated_convergence (dominated_fin_meas_additive_weighted_smul μ) bound hF_meas h_bound bound_integrable h_lim /-- Lebesgue dominated convergence theorem for series. -/ lemma has_sum_integral_of_dominated_convergence {ι} [countable ι] {F : ι → α → E} {f : α → E} (bound : ι → α → ℝ) (hF_meas : ∀ n, ae_strongly_measurable (F n) μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ‖F n a‖ ≤ bound n a) (bound_summable : ∀ᵐ a ∂μ, summable (λ n, bound n a)) (bound_integrable : integrable (λ a, ∑' n, bound n a) μ) (h_lim : ∀ᵐ a ∂μ, has_sum (λ n, F n a) (f a)) : has_sum (λn, ∫ a, F n a ∂μ) (∫ a, f a ∂μ) := begin have hb_nonneg : ∀ᵐ a ∂μ, ∀ n, 0 ≤ bound n a := eventually_countable_forall.2 (λ n, (h_bound n).mono $ λ a, (norm_nonneg _).trans), have hb_le_tsum : ∀ n, bound n ≤ᵐ[μ] (λ a, ∑' n, bound n a), { intro n, filter_upwards [hb_nonneg, bound_summable] with _ ha0 ha_sum using le_tsum ha_sum _ (λ i _, ha0 i) }, have hF_integrable : ∀ n, integrable (F n) μ, { refine λ n, bound_integrable.mono' (hF_meas n) _, exact eventually_le.trans (h_bound n) (hb_le_tsum n) }, simp only [has_sum, ← integral_finset_sum _ (λ n _, hF_integrable n)], refine tendsto_integral_filter_of_dominated_convergence (λ a, ∑' n, bound n a) _ _ bound_integrable h_lim, { exact eventually_of_forall (λ s, s.ae_strongly_measurable_sum $ λ n hn, hF_meas n) }, { refine eventually_of_forall (λ s, _), filter_upwards [eventually_countable_forall.2 h_bound, hb_nonneg, bound_summable] with a hFa ha0 has, calc ‖∑ n in s, F n a‖ ≤ ∑ n in s, bound n a : norm_sum_le_of_le _ (λ n hn, hFa n) ... ≤ ∑' n, bound n a : sum_le_tsum _ (λ n hn, ha0 n) has }, end variables {X : Type*} [topological_space X] [first_countable_topology X] lemma continuous_within_at_of_dominated {F : X → α → E} {x₀ : X} {bound : α → ℝ} {s : set X} (hF_meas : ∀ᶠ x in 𝓝[s] x₀, ae_strongly_measurable (F x) μ) (h_bound : ∀ᶠ x in 𝓝[s] x₀, ∀ᵐ a ∂μ, ‖F x a‖ ≤ bound a) (bound_integrable : integrable bound μ) (h_cont : ∀ᵐ a ∂μ, continuous_within_at (λ x, F x a) s x₀) : continuous_within_at (λ x, ∫ a, F x a ∂μ) s x₀ := continuous_within_at_set_to_fun_of_dominated (dominated_fin_meas_additive_weighted_smul μ) hF_meas h_bound bound_integrable h_cont lemma continuous_at_of_dominated {F : X → α → E} {x₀ : X} {bound : α → ℝ} (hF_meas : ∀ᶠ x in 𝓝 x₀, ae_strongly_measurable (F x) μ) (h_bound : ∀ᶠ x in 𝓝 x₀, ∀ᵐ a ∂μ, ‖F x a‖ ≤ bound a) (bound_integrable : integrable bound μ) (h_cont : ∀ᵐ a ∂μ, continuous_at (λ x, F x a) x₀) : continuous_at (λ x, ∫ a, F x a ∂μ) x₀ := continuous_at_set_to_fun_of_dominated (dominated_fin_meas_additive_weighted_smul μ) hF_meas h_bound bound_integrable h_cont lemma continuous_on_of_dominated {F : X → α → E} {bound : α → ℝ} {s : set X} (hF_meas : ∀ x ∈ s, ae_strongly_measurable (F x) μ) (h_bound : ∀ x ∈ s, ∀ᵐ a ∂μ, ‖F x a‖ ≤ bound a) (bound_integrable : integrable bound μ) (h_cont : ∀ᵐ a ∂μ, continuous_on (λ x, F x a) s) : continuous_on (λ x, ∫ a, F x a ∂μ) s := continuous_on_set_to_fun_of_dominated (dominated_fin_meas_additive_weighted_smul μ) hF_meas h_bound bound_integrable h_cont lemma continuous_of_dominated {F : X → α → E} {bound : α → ℝ} (hF_meas : ∀ x, ae_strongly_measurable (F x) μ) (h_bound : ∀ x, ∀ᵐ a ∂μ, ‖F x a‖ ≤ bound a) (bound_integrable : integrable bound μ) (h_cont : ∀ᵐ a ∂μ, continuous (λ x, F x a)) : continuous (λ x, ∫ a, F x a ∂μ) := continuous_set_to_fun_of_dominated (dominated_fin_meas_additive_weighted_smul μ) hF_meas h_bound bound_integrable h_cont /-- The Bochner integral of a real-valued function `f : α → ℝ` is the difference between the integral of the positive part of `f` and the integral of the negative part of `f`. -/ lemma integral_eq_lintegral_pos_part_sub_lintegral_neg_part {f : α → ℝ} (hf : integrable f μ) : ∫ a, f a ∂μ = ennreal.to_real (∫⁻ a, (ennreal.of_real $ f a) ∂μ) - ennreal.to_real (∫⁻ a, (ennreal.of_real $ - f a) ∂μ) := let f₁ := hf.to_L1 f in -- Go to the `L¹` space have eq₁ : ennreal.to_real (∫⁻ a, (ennreal.of_real $ f a) ∂μ) = ‖Lp.pos_part f₁‖ := begin rw L1.norm_def, congr' 1, apply lintegral_congr_ae, filter_upwards [Lp.coe_fn_pos_part f₁, hf.coe_fn_to_L1] with _ h₁ h₂, rw [h₁, h₂, ennreal.of_real], congr' 1, apply nnreal.eq, rw real.nnnorm_of_nonneg (le_max_right _ _), simp only [real.coe_to_nnreal', subtype.coe_mk], end, -- Go to the `L¹` space have eq₂ : ennreal.to_real (∫⁻ a, (ennreal.of_real $ - f a) ∂μ) = ‖Lp.neg_part f₁‖ := begin rw L1.norm_def, congr' 1, apply lintegral_congr_ae, filter_upwards [Lp.coe_fn_neg_part f₁, hf.coe_fn_to_L1] with _ h₁ h₂, rw [h₁, h₂, ennreal.of_real], congr' 1, apply nnreal.eq, simp only [real.coe_to_nnreal', coe_nnnorm, nnnorm_neg], rw [real.norm_of_nonpos (min_le_right _ _), ← max_neg_neg, neg_zero], end, begin rw [eq₁, eq₂, integral, dif_pos], exact L1.integral_eq_norm_pos_part_sub _ end lemma integral_eq_lintegral_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfm : ae_strongly_measurable f μ) : ∫ a, f a ∂μ = ennreal.to_real (∫⁻ a, (ennreal.of_real $ f a) ∂μ) := begin by_cases hfi : integrable f μ, { rw integral_eq_lintegral_pos_part_sub_lintegral_neg_part hfi, have h_min : ∫⁻ a, ennreal.of_real (-f a) ∂μ = 0, { rw lintegral_eq_zero_iff', { refine hf.mono _, simp only [pi.zero_apply], assume a h, simp only [h, neg_nonpos, of_real_eq_zero], }, { exact measurable_of_real.comp_ae_measurable hfm.ae_measurable.neg } }, rw [h_min, zero_to_real, _root_.sub_zero] }, { rw integral_undef hfi, simp_rw [integrable, hfm, has_finite_integral_iff_norm, lt_top_iff_ne_top, ne.def, true_and, not_not] at hfi, have : ∫⁻ (a : α), ennreal.of_real (f a) ∂μ = ∫⁻ a, (ennreal.of_real ‖f a‖) ∂μ, { refine lintegral_congr_ae (hf.mono $ assume a h, _), rw [real.norm_eq_abs, abs_of_nonneg h] }, rw [this, hfi], refl } end lemma integral_norm_eq_lintegral_nnnorm {G} [normed_add_comm_group G] {f : α → G} (hf : ae_strongly_measurable f μ) : ∫ x, ‖f x‖ ∂μ = ennreal.to_real ∫⁻ x, ‖f x‖₊ ∂μ := begin rw integral_eq_lintegral_of_nonneg_ae _ hf.norm, { simp_rw [of_real_norm_eq_coe_nnnorm], }, { refine ae_of_all _ _, simp_rw [pi.zero_apply, norm_nonneg, imp_true_iff] }, end lemma of_real_integral_norm_eq_lintegral_nnnorm {G} [normed_add_comm_group G] {f : α → G} (hf : integrable f μ) : ennreal.of_real ∫ x, ‖f x‖ ∂μ = ∫⁻ x, ‖f x‖₊ ∂μ := by rw [integral_norm_eq_lintegral_nnnorm hf.ae_strongly_measurable, ennreal.of_real_to_real (lt_top_iff_ne_top.mp hf.2)] lemma integral_eq_integral_pos_part_sub_integral_neg_part {f : α → ℝ} (hf : integrable f μ) : ∫ a, f a ∂μ = (∫ a, real.to_nnreal (f a) ∂μ) - (∫ a, real.to_nnreal (-f a) ∂μ) := begin rw [← integral_sub hf.real_to_nnreal], { simp }, { exact hf.neg.real_to_nnreal } end lemma integral_nonneg_of_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) : 0 ≤ ∫ a, f a ∂μ := set_to_fun_nonneg (dominated_fin_meas_additive_weighted_smul μ) (λ s _ _, weighted_smul_nonneg s) hf lemma lintegral_coe_eq_integral (f : α → ℝ≥0) (hfi : integrable (λ x, (f x : ℝ)) μ) : ∫⁻ a, f a ∂μ = ennreal.of_real ∫ a, f a ∂μ := begin simp_rw [integral_eq_lintegral_of_nonneg_ae (eventually_of_forall (λ x, (f x).coe_nonneg)) hfi.ae_strongly_measurable, ← ennreal.coe_nnreal_eq], rw [ennreal.of_real_to_real], rw [← lt_top_iff_ne_top], convert hfi.has_finite_integral, ext1 x, rw [nnreal.nnnorm_eq] end lemma of_real_integral_eq_lintegral_of_real {f : α → ℝ} (hfi : integrable f μ) (f_nn : 0 ≤ᵐ[μ] f) : ennreal.of_real (∫ x, f x ∂μ) = ∫⁻ x, ennreal.of_real (f x) ∂μ := begin simp_rw [integral_congr_ae (show f =ᵐ[μ] λ x, ‖f x‖, by { filter_upwards [f_nn] with x hx, rw [real.norm_eq_abs, abs_eq_self.mpr hx], }), of_real_integral_norm_eq_lintegral_nnnorm hfi, ←of_real_norm_eq_coe_nnnorm], apply lintegral_congr_ae, filter_upwards [f_nn] with x hx, exact congr_arg ennreal.of_real (by rw [real.norm_eq_abs, abs_eq_self.mpr hx]), end lemma integral_to_real {f : α → ℝ≥0∞} (hfm : ae_measurable f μ) (hf : ∀ᵐ x ∂μ, f x < ∞) : ∫ a, (f a).to_real ∂μ = (∫⁻ a, f a ∂μ).to_real := begin rw [integral_eq_lintegral_of_nonneg_ae _ hfm.ennreal_to_real.ae_strongly_measurable], { rw lintegral_congr_ae, refine hf.mp (eventually_of_forall _), intros x hx, rw [lt_top_iff_ne_top] at hx, simp [hx] }, { exact (eventually_of_forall $ λ x, ennreal.to_real_nonneg) } end lemma lintegral_coe_le_coe_iff_integral_le {f : α → ℝ≥0} (hfi : integrable (λ x, (f x : ℝ)) μ) {b : ℝ≥0} : ∫⁻ a, f a ∂μ ≤ b ↔ ∫ a, (f a : ℝ) ∂μ ≤ b := by rw [lintegral_coe_eq_integral f hfi, ennreal.of_real, ennreal.coe_le_coe, real.to_nnreal_le_iff_le_coe] lemma integral_coe_le_of_lintegral_coe_le {f : α → ℝ≥0} {b : ℝ≥0} (h : ∫⁻ a, f a ∂μ ≤ b) : ∫ a, (f a : ℝ) ∂μ ≤ b := begin by_cases hf : integrable (λ a, (f a : ℝ)) μ, { exact (lintegral_coe_le_coe_iff_integral_le hf).1 h }, { rw integral_undef hf, exact b.2 } end lemma integral_nonneg {f : α → ℝ} (hf : 0 ≤ f) : 0 ≤ ∫ a, f a ∂μ := integral_nonneg_of_ae $ eventually_of_forall hf lemma integral_nonpos_of_ae {f : α → ℝ} (hf : f ≤ᵐ[μ] 0) : ∫ a, f a ∂μ ≤ 0 := begin have hf : 0 ≤ᵐ[μ] (-f) := hf.mono (assume a h, by rwa [pi.neg_apply, pi.zero_apply, neg_nonneg]), have : 0 ≤ ∫ a, -f a ∂μ := integral_nonneg_of_ae hf, rwa [integral_neg, neg_nonneg] at this, end lemma integral_nonpos {f : α → ℝ} (hf : f ≤ 0) : ∫ a, f a ∂μ ≤ 0 := integral_nonpos_of_ae $ eventually_of_forall hf lemma integral_eq_zero_iff_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfi : integrable f μ) : ∫ x, f x ∂μ = 0 ↔ f =ᵐ[μ] 0 := by simp_rw [integral_eq_lintegral_of_nonneg_ae hf hfi.1, ennreal.to_real_eq_zero_iff, lintegral_eq_zero_iff' (ennreal.measurable_of_real.comp_ae_measurable hfi.1.ae_measurable), ← ennreal.not_lt_top, ← has_finite_integral_iff_of_real hf, hfi.2, not_true, or_false, ← hf.le_iff_eq, filter.eventually_eq, filter.eventually_le, (∘), pi.zero_apply, ennreal.of_real_eq_zero] lemma integral_eq_zero_iff_of_nonneg {f : α → ℝ} (hf : 0 ≤ f) (hfi : integrable f μ) : ∫ x, f x ∂μ = 0 ↔ f =ᵐ[μ] 0 := integral_eq_zero_iff_of_nonneg_ae (eventually_of_forall hf) hfi lemma integral_pos_iff_support_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfi : integrable f μ) : (0 < ∫ x, f x ∂μ) ↔ 0 < μ (function.support f) := by simp_rw [(integral_nonneg_of_ae hf).lt_iff_ne, pos_iff_ne_zero, ne.def, @eq_comm ℝ 0, integral_eq_zero_iff_of_nonneg_ae hf hfi, filter.eventually_eq, ae_iff, pi.zero_apply, function.support] lemma integral_pos_iff_support_of_nonneg {f : α → ℝ} (hf : 0 ≤ f) (hfi : integrable f μ) : (0 < ∫ x, f x ∂μ) ↔ 0 < μ (function.support f) := integral_pos_iff_support_of_nonneg_ae (eventually_of_forall hf) hfi section normed_add_comm_group variables {H : Type*} [normed_add_comm_group H] lemma L1.norm_eq_integral_norm (f : α →₁[μ] H) : ‖f‖ = ∫ a, ‖f a‖ ∂μ := begin simp only [snorm, snorm', ennreal.one_to_real, ennreal.rpow_one, Lp.norm_def, if_false, ennreal.one_ne_top, one_ne_zero, _root_.div_one], rw integral_eq_lintegral_of_nonneg_ae (eventually_of_forall (by simp [norm_nonneg])) (Lp.ae_strongly_measurable f).norm, simp [of_real_norm_eq_coe_nnnorm] end lemma L1.norm_of_fun_eq_integral_norm {f : α → H} (hf : integrable f μ) : ‖hf.to_L1 f‖ = ∫ a, ‖f a‖ ∂μ := begin rw L1.norm_eq_integral_norm, refine integral_congr_ae _, apply hf.coe_fn_to_L1.mono, intros a ha, simp [ha] end lemma mem_ℒp.snorm_eq_integral_rpow_norm {f : α → H} {p : ℝ≥0∞} (hp1 : p ≠ 0) (hp2 : p ≠ ∞) (hf : mem_ℒp f p μ) : snorm f p μ = ennreal.of_real ((∫ a, ‖f a‖ ^ p.to_real ∂μ) ^ (p.to_real ⁻¹)) := begin have A : ∫⁻ (a : α), ennreal.of_real (‖f a‖ ^ p.to_real) ∂μ = ∫⁻ (a : α), ‖f a‖₊ ^ p.to_real ∂μ, { apply lintegral_congr (λ x, _), rw [← of_real_rpow_of_nonneg (norm_nonneg _) to_real_nonneg, of_real_norm_eq_coe_nnnorm] }, simp only [snorm_eq_lintegral_rpow_nnnorm hp1 hp2, one_div], rw integral_eq_lintegral_of_nonneg_ae, rotate, { exact eventually_of_forall (λ x, real.rpow_nonneg_of_nonneg (norm_nonneg _) _) }, { exact (hf.ae_strongly_measurable.norm.ae_measurable.pow_const _).ae_strongly_measurable }, rw [A, ← of_real_rpow_of_nonneg to_real_nonneg (inv_nonneg.2 to_real_nonneg), of_real_to_real], exact (lintegral_rpow_nnnorm_lt_top_of_snorm_lt_top hp1 hp2 hf.2).ne end end normed_add_comm_group lemma integral_mono_ae {f g : α → ℝ} (hf : integrable f μ) (hg : integrable g μ) (h : f ≤ᵐ[μ] g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ := set_to_fun_mono (dominated_fin_meas_additive_weighted_smul μ) (λ s _ _, weighted_smul_nonneg s) hf hg h @[mono] lemma integral_mono {f g : α → ℝ} (hf : integrable f μ) (hg : integrable g μ) (h : f ≤ g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ := integral_mono_ae hf hg $ eventually_of_forall h lemma integral_mono_of_nonneg {f g : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hgi : integrable g μ) (h : f ≤ᵐ[μ] g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ := begin by_cases hfm : ae_strongly_measurable f μ, { refine integral_mono_ae ⟨hfm, _⟩ hgi h, refine (hgi.has_finite_integral.mono $ h.mp $ hf.mono $ λ x hf hfg, _), simpa [abs_of_nonneg hf, abs_of_nonneg (le_trans hf hfg)] }, { rw [integral_non_ae_strongly_measurable hfm], exact integral_nonneg_of_ae (hf.trans h) } end lemma integral_mono_measure {f : α → ℝ} {ν} (hle : μ ≤ ν) (hf : 0 ≤ᵐ[ν] f) (hfi : integrable f ν) : ∫ a, f a ∂μ ≤ ∫ a, f a ∂ν := begin have hfi' : integrable f μ := hfi.mono_measure hle, have hf' : 0 ≤ᵐ[μ] f := hle.absolutely_continuous hf, rw [integral_eq_lintegral_of_nonneg_ae hf' hfi'.1, integral_eq_lintegral_of_nonneg_ae hf hfi.1, ennreal.to_real_le_to_real], exacts [lintegral_mono' hle le_rfl, ((has_finite_integral_iff_of_real hf').1 hfi'.2).ne, ((has_finite_integral_iff_of_real hf).1 hfi.2).ne] end lemma norm_integral_le_integral_norm (f : α → E) : ‖(∫ a, f a ∂μ)‖ ≤ ∫ a, ‖f a‖ ∂μ := have le_ae : ∀ᵐ a ∂μ, 0 ≤ ‖f a‖ := eventually_of_forall (λa, norm_nonneg _), classical.by_cases ( λh : ae_strongly_measurable f μ, calc ‖∫ a, f a ∂μ‖ ≤ ennreal.to_real (∫⁻ a, (ennreal.of_real ‖f a‖) ∂μ) : norm_integral_le_lintegral_norm _ ... = ∫ a, ‖f a‖ ∂μ : (integral_eq_lintegral_of_nonneg_ae le_ae $ h.norm).symm ) ( λh : ¬ae_strongly_measurable f μ, begin rw [integral_non_ae_strongly_measurable h, norm_zero], exact integral_nonneg_of_ae le_ae end ) lemma norm_integral_le_of_norm_le {f : α → E} {g : α → ℝ} (hg : integrable g μ) (h : ∀ᵐ x ∂μ, ‖f x‖ ≤ g x) : ‖∫ x, f x ∂μ‖ ≤ ∫ x, g x ∂μ := calc ‖∫ x, f x ∂μ‖ ≤ ∫ x, ‖f x‖ ∂μ : norm_integral_le_integral_norm f ... ≤ ∫ x, g x ∂μ : integral_mono_of_nonneg (eventually_of_forall $ λ x, norm_nonneg _) hg h lemma simple_func.integral_eq_integral (f : α →ₛ E) (hfi : integrable f μ) : f.integral μ = ∫ x, f x ∂μ := begin rw [integral_eq f hfi, ← L1.simple_func.to_Lp_one_eq_to_L1, L1.simple_func.integral_L1_eq_integral, L1.simple_func.integral_eq_integral], exact simple_func.integral_congr hfi (Lp.simple_func.to_simple_func_to_Lp _ _).symm end lemma simple_func.integral_eq_sum (f : α →ₛ E) (hfi : integrable f μ) : ∫ x, f x ∂μ = ∑ x in f.range, (ennreal.to_real (μ (f ⁻¹' {x}))) • x := by { rw [← f.integral_eq_integral hfi, simple_func.integral, ← simple_func.integral_eq], refl, } @[simp] lemma integral_const (c : E) : ∫ x : α, c ∂μ = (μ univ).to_real • c := begin cases (@le_top _ _ _ (μ univ)).lt_or_eq with hμ hμ, { haveI : is_finite_measure μ := ⟨hμ⟩, exact set_to_fun_const (dominated_fin_meas_additive_weighted_smul _) _, }, { by_cases hc : c = 0, { simp [hc, integral_zero] }, { have : ¬integrable (λ x : α, c) μ, { simp only [integrable_const_iff, not_or_distrib], exact ⟨hc, hμ.not_lt⟩ }, simp [integral_undef, *] } } end lemma norm_integral_le_of_norm_le_const [is_finite_measure μ] {f : α → E} {C : ℝ} (h : ∀ᵐ x ∂μ, ‖f x‖ ≤ C) : ‖∫ x, f x ∂μ‖ ≤ C * (μ univ).to_real := calc ‖∫ x, f x ∂μ‖ ≤ ∫ x, C ∂μ : norm_integral_le_of_norm_le (integrable_const C) h ... = C * (μ univ).to_real : by rw [integral_const, smul_eq_mul, mul_comm] lemma tendsto_integral_approx_on_of_measurable [measurable_space E] [borel_space E] {f : α → E} {s : set E} [separable_space s] (hfi : integrable f μ) (hfm : measurable f) (hs : ∀ᵐ x ∂μ, f x ∈ closure s) {y₀ : E} (h₀ : y₀ ∈ s) (h₀i : integrable (λ x, y₀) μ) : tendsto (λ n, (simple_func.approx_on f hfm s y₀ h₀ n).integral μ) at_top (𝓝 $ ∫ x, f x ∂μ) := begin have hfi' := simple_func.integrable_approx_on hfm hfi h₀ h₀i, simp only [simple_func.integral_eq_integral _ (hfi' _)], exact tendsto_set_to_fun_approx_on_of_measurable (dominated_fin_meas_additive_weighted_smul μ) hfi hfm hs h₀ h₀i, end lemma tendsto_integral_approx_on_of_measurable_of_range_subset [measurable_space E] [borel_space E] {f : α → E} (fmeas : measurable f) (hf : integrable f μ) (s : set E) [separable_space s] (hs : range f ∪ {0} ⊆ s) : tendsto (λ n, (simple_func.approx_on f fmeas s 0 (hs $ by simp) n).integral μ) at_top (𝓝 $ ∫ x, f x ∂μ) := begin apply tendsto_integral_approx_on_of_measurable hf fmeas _ _ (integrable_zero _ _ _), exact eventually_of_forall (λ x, subset_closure (hs (set.mem_union_left _ (mem_range_self _)))), end variable {ν : measure α} lemma integral_add_measure {f : α → E} (hμ : integrable f μ) (hν : integrable f ν) : ∫ x, f x ∂(μ + ν) = ∫ x, f x ∂μ + ∫ x, f x ∂ν := begin have hfi := hμ.add_measure hν, simp_rw [integral_eq_set_to_fun], have hμ_dfma : dominated_fin_meas_additive (μ + ν) (weighted_smul μ : set α → E →L[ℝ] E) 1, from dominated_fin_meas_additive.add_measure_right μ ν (dominated_fin_meas_additive_weighted_smul μ) zero_le_one, have hν_dfma : dominated_fin_meas_additive (μ + ν) (weighted_smul ν : set α → E →L[ℝ] E) 1, from dominated_fin_meas_additive.add_measure_left μ ν (dominated_fin_meas_additive_weighted_smul ν) zero_le_one, rw [← set_to_fun_congr_measure_of_add_right hμ_dfma (dominated_fin_meas_additive_weighted_smul μ) f hfi, ← set_to_fun_congr_measure_of_add_left hν_dfma (dominated_fin_meas_additive_weighted_smul ν) f hfi], refine set_to_fun_add_left' _ _ _ (λ s hs hμνs, _) f, rw [measure.coe_add, pi.add_apply, add_lt_top] at hμνs, rw [weighted_smul, weighted_smul, weighted_smul, ← add_smul, measure.coe_add, pi.add_apply, to_real_add hμνs.1.ne hμνs.2.ne], end @[simp] lemma integral_zero_measure {m : measurable_space α} (f : α → E) : ∫ x, f x ∂(0 : measure α) = 0 := set_to_fun_measure_zero (dominated_fin_meas_additive_weighted_smul _) rfl theorem integral_finset_sum_measure {ι} {m : measurable_space α} {f : α → E} {μ : ι → measure α} {s : finset ι} (hf : ∀ i ∈ s, integrable f (μ i)) : ∫ a, f a ∂(∑ i in s, μ i) = ∑ i in s, ∫ a, f a ∂μ i := begin classical, refine finset.induction_on' s _ _, -- `induction s using finset.induction_on'` fails { simp }, { intros i t hi ht hit iht, simp only [finset.sum_insert hit, ← iht], exact integral_add_measure (hf _ hi) (integrable_finset_sum_measure.2 $ λ j hj, hf j (ht hj)) } end lemma nndist_integral_add_measure_le_lintegral (h₁ : integrable f μ) (h₂ : integrable f ν) : (nndist (∫ x, f x ∂μ) (∫ x, f x ∂(μ + ν)) : ℝ≥0∞) ≤ ∫⁻ x, ‖f x‖₊ ∂ν := begin rw [integral_add_measure h₁ h₂, nndist_comm, nndist_eq_nnnorm, add_sub_cancel'], exact ennnorm_integral_le_lintegral_ennnorm _ end theorem has_sum_integral_measure {ι} {m : measurable_space α} {f : α → E} {μ : ι → measure α} (hf : integrable f (measure.sum μ)) : has_sum (λ i, ∫ a, f a ∂μ i) (∫ a, f a ∂measure.sum μ) := begin have hfi : ∀ i, integrable f (μ i) := λ i, hf.mono_measure (measure.le_sum _ _), simp only [has_sum, ← integral_finset_sum_measure (λ i _, hfi i)], refine metric.nhds_basis_ball.tendsto_right_iff.mpr (λ ε ε0, _), lift ε to ℝ≥0 using ε0.le, have hf_lt : ∫⁻ x, ‖f x‖₊ ∂(measure.sum μ) < ∞ := hf.2, have hmem : ∀ᶠ y in 𝓝 ∫⁻ x, ‖f x‖₊ ∂(measure.sum μ), ∫⁻ x, ‖f x‖₊ ∂(measure.sum μ) < y + ε, { refine tendsto_id.add tendsto_const_nhds (lt_mem_nhds $ ennreal.lt_add_right _ _), exacts [hf_lt.ne, ennreal.coe_ne_zero.2 (nnreal.coe_ne_zero.1 ε0.ne')] }, refine ((has_sum_lintegral_measure (λ x, ‖f x‖₊) μ).eventually hmem).mono (λ s hs, _), obtain ⟨ν, hν⟩ : ∃ ν, (∑ i in s, μ i) + ν = measure.sum μ, { refine ⟨measure.sum (λ i : ↥(sᶜ : set ι), μ i), _⟩, simpa only [← measure.sum_coe_finset] using measure.sum_add_sum_compl (s : set ι) μ }, rw [metric.mem_ball, ← coe_nndist, nnreal.coe_lt_coe, ← ennreal.coe_lt_coe, ← hν], rw [← hν, integrable_add_measure] at hf, refine (nndist_integral_add_measure_le_lintegral hf.1 hf.2).trans_lt _, rw [← hν, lintegral_add_measure, lintegral_finset_sum_measure] at hs, exact lt_of_add_lt_add_left hs end theorem integral_sum_measure {ι} {m : measurable_space α} {f : α → E} {μ : ι → measure α} (hf : integrable f (measure.sum μ)) : ∫ a, f a ∂measure.sum μ = ∑' i, ∫ a, f a ∂μ i := (has_sum_integral_measure hf).tsum_eq.symm @[simp] lemma integral_smul_measure (f : α → E) (c : ℝ≥0∞) : ∫ x, f x ∂(c • μ) = c.to_real • ∫ x, f x ∂μ := begin -- First we consider the “degenerate” case `c = ∞` rcases eq_or_ne c ∞ with rfl|hc, { rw [ennreal.top_to_real, zero_smul, integral_eq_set_to_fun, set_to_fun_top_smul_measure], }, -- Main case: `c ≠ ∞` simp_rw [integral_eq_set_to_fun, ← set_to_fun_smul_left], have hdfma : dominated_fin_meas_additive μ (weighted_smul (c • μ) : set α → E →L[ℝ] E) c.to_real, from mul_one c.to_real ▸ (dominated_fin_meas_additive_weighted_smul (c • μ)).of_smul_measure c hc, have hdfma_smul := (dominated_fin_meas_additive_weighted_smul (c • μ)), rw ← set_to_fun_congr_smul_measure c hc hdfma hdfma_smul f, exact set_to_fun_congr_left' _ _ (λ s hs hμs, weighted_smul_smul_measure μ c) f, end lemma integral_map_of_strongly_measurable {β} [measurable_space β] {φ : α → β} (hφ : measurable φ) {f : β → E} (hfm : strongly_measurable f) : ∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ := begin by_cases hfi : integrable f (measure.map φ μ), swap, { rw [integral_undef hfi, integral_undef], rwa [← integrable_map_measure hfm.ae_strongly_measurable hφ.ae_measurable] }, borelize E, haveI : separable_space (range f ∪ {0} : set E) := hfm.separable_space_range_union_singleton, refine tendsto_nhds_unique (tendsto_integral_approx_on_of_measurable_of_range_subset hfm.measurable hfi _ subset.rfl) _, convert tendsto_integral_approx_on_of_measurable_of_range_subset (hfm.measurable.comp hφ) ((integrable_map_measure hfm.ae_strongly_measurable hφ.ae_measurable).1 hfi) (range f ∪ {0}) (by simp [insert_subset_insert, set.range_comp_subset_range]) using 1, ext1 i, simp only [simple_func.approx_on_comp, simple_func.integral_eq, measure.map_apply, hφ, simple_func.measurable_set_preimage, ← preimage_comp, simple_func.coe_comp], refine (finset.sum_subset (simple_func.range_comp_subset_range _ hφ) (λ y _ hy, _)).symm, rw [simple_func.mem_range, ← set.preimage_singleton_eq_empty, simple_func.coe_comp] at hy, rw [hy], simp, end lemma integral_map {β} [measurable_space β] {φ : α → β} (hφ : ae_measurable φ μ) {f : β → E} (hfm : ae_strongly_measurable f (measure.map φ μ)) : ∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ := let g := hfm.mk f in calc ∫ y, f y ∂(measure.map φ μ) = ∫ y, g y ∂(measure.map φ μ) : integral_congr_ae hfm.ae_eq_mk ... = ∫ y, g y ∂(measure.map (hφ.mk φ) μ) : by { congr' 1, exact measure.map_congr hφ.ae_eq_mk } ... = ∫ x, g (hφ.mk φ x) ∂μ : integral_map_of_strongly_measurable hφ.measurable_mk hfm.strongly_measurable_mk ... = ∫ x, g (φ x) ∂μ : integral_congr_ae (hφ.ae_eq_mk.symm.fun_comp _) ... = ∫ x, f (φ x) ∂μ : integral_congr_ae $ ae_eq_comp hφ (hfm.ae_eq_mk).symm lemma _root_.measurable_embedding.integral_map {β} {_ : measurable_space β} {f : α → β} (hf : measurable_embedding f) (g : β → E) : ∫ y, g y ∂(measure.map f μ) = ∫ x, g (f x) ∂μ := begin by_cases hgm : ae_strongly_measurable g (measure.map f μ), { exact integral_map hf.measurable.ae_measurable hgm }, { rw [integral_non_ae_strongly_measurable hgm, integral_non_ae_strongly_measurable], rwa ← hf.ae_strongly_measurable_map_iff } end lemma _root_.closed_embedding.integral_map {β} [topological_space α] [borel_space α] [topological_space β] [measurable_space β] [borel_space β] {φ : α → β} (hφ : closed_embedding φ) (f : β → E) : ∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ := hφ.measurable_embedding.integral_map _ lemma integral_map_equiv {β} [measurable_space β] (e : α ≃ᵐ β) (f : β → E) : ∫ y, f y ∂(measure.map e μ) = ∫ x, f (e x) ∂μ := e.measurable_embedding.integral_map f lemma measure_preserving.integral_comp {β} {_ : measurable_space β} {f : α → β} {ν} (h₁ : measure_preserving f μ ν) (h₂ : measurable_embedding f) (g : β → E) : ∫ x, g (f x) ∂μ = ∫ y, g y ∂ν := h₁.map_eq ▸ (h₂.integral_map g).symm lemma set_integral_eq_subtype {α} [measure_space α] {s : set α} (hs : measurable_set s) (f : α → E) : ∫ x in s, f x = ∫ x : s, f x := by { rw ← map_comap_subtype_coe hs, exact (measurable_embedding.subtype_coe hs).integral_map _ } @[simp] lemma integral_dirac' [measurable_space α] (f : α → E) (a : α) (hfm : strongly_measurable f) : ∫ x, f x ∂(measure.dirac a) = f a := begin borelize E, calc ∫ x, f x ∂(measure.dirac a) = ∫ x, f a ∂(measure.dirac a) : integral_congr_ae $ ae_eq_dirac' hfm.measurable ... = f a : by simp [measure.dirac_apply_of_mem] end @[simp] lemma integral_dirac [measurable_space α] [measurable_singleton_class α] (f : α → E) (a : α) : ∫ x, f x ∂(measure.dirac a) = f a := calc ∫ x, f x ∂(measure.dirac a) = ∫ x, f a ∂(measure.dirac a) : integral_congr_ae $ ae_eq_dirac f ... = f a : by simp [measure.dirac_apply_of_mem] lemma mul_meas_ge_le_integral_of_nonneg [is_finite_measure μ] {f : α → ℝ} (hf_nonneg : 0 ≤ f) (hf_int : integrable f μ) (ε : ℝ) : ε * (μ {x | ε ≤ f x}).to_real ≤ ∫ x, f x ∂μ := begin cases lt_or_le ε 0 with hε hε, { exact (mul_nonpos_of_nonpos_of_nonneg hε.le ennreal.to_real_nonneg).trans (integral_nonneg hf_nonneg), }, rw [integral_eq_lintegral_of_nonneg_ae (eventually_of_forall (λ x, hf_nonneg x)) hf_int.ae_strongly_measurable, ← ennreal.to_real_of_real hε, ← ennreal.to_real_mul], have : {x : α | (ennreal.of_real ε).to_real ≤ f x} = {x : α | ennreal.of_real ε ≤ (λ x, ennreal.of_real (f x)) x}, { ext1 x, rw [set.mem_set_of_eq, set.mem_set_of_eq, ← ennreal.to_real_of_real (hf_nonneg x)], exact ennreal.to_real_le_to_real ennreal.of_real_ne_top ennreal.of_real_ne_top, }, rw this, have h_meas : ae_measurable (λ x, ennreal.of_real (f x)) μ, from measurable_id'.ennreal_of_real.comp_ae_measurable hf_int.ae_measurable, have h_mul_meas_le := @mul_meas_ge_le_lintegral₀ _ _ μ _ h_meas (ennreal.of_real ε), rw ennreal.to_real_le_to_real _ _, { exact h_mul_meas_le, }, { simp only [ne.def, with_top.mul_eq_top_iff, ennreal.of_real_eq_zero, not_le, ennreal.of_real_ne_top, false_and, or_false, not_and], exact λ _, measure_ne_top _ _, }, { have h_lt_top : ∫⁻ a, ‖f a‖₊ ∂μ < ∞ := hf_int.has_finite_integral, simp_rw [← of_real_norm_eq_coe_nnnorm, real.norm_eq_abs] at h_lt_top, convert h_lt_top.ne, ext1 x, rw abs_of_nonneg (hf_nonneg x), }, end /-- Hölder's inequality for the integral of a product of norms. The integral of the product of two norms of functions is bounded by the product of their `ℒp` and `ℒq` seminorms when `p` and `q` are conjugate exponents. -/ theorem integral_mul_norm_le_Lp_mul_Lq {E} [normed_add_comm_group E] {f g : α → E} {p q : ℝ} (hpq : p.is_conjugate_exponent q) (hf : mem_ℒp f (ennreal.of_real p) μ) (hg : mem_ℒp g (ennreal.of_real q) μ) : ∫ a, ‖f a‖ * ‖g a‖ ∂μ ≤ (∫ a, ‖f a‖ ^ p ∂μ) ^ (1/p) * (∫ a, ‖g a‖ ^ q ∂μ) ^ (1/q) := begin -- translate the Bochner integrals into Lebesgue integrals. rw [integral_eq_lintegral_of_nonneg_ae, integral_eq_lintegral_of_nonneg_ae, integral_eq_lintegral_of_nonneg_ae], rotate 1, { exact eventually_of_forall (λ x, real.rpow_nonneg_of_nonneg (norm_nonneg _) _), }, { exact (hg.1.norm.ae_measurable.pow ae_measurable_const).ae_strongly_measurable, }, { exact eventually_of_forall (λ x, real.rpow_nonneg_of_nonneg (norm_nonneg _) _),}, { exact (hf.1.norm.ae_measurable.pow ae_measurable_const).ae_strongly_measurable, }, { exact eventually_of_forall (λ x, mul_nonneg (norm_nonneg _) (norm_nonneg _)), }, { exact hf.1.norm.mul hg.1.norm, }, rw [ennreal.to_real_rpow, ennreal.to_real_rpow, ← ennreal.to_real_mul], -- replace norms by nnnorm have h_left : ∫⁻ a, ennreal.of_real (‖f a‖ * ‖g a‖) ∂μ = ∫⁻ a, ((λ x, (‖f x‖₊ : ℝ≥0∞)) * (λ x, ‖g x‖₊)) a ∂μ, { simp_rw [pi.mul_apply, ← of_real_norm_eq_coe_nnnorm, ennreal.of_real_mul (norm_nonneg _)], }, have h_right_f : ∫⁻ a, ennreal.of_real (‖f a‖ ^ p) ∂μ = ∫⁻ a, ‖f a‖₊ ^ p ∂μ, { refine lintegral_congr (λ x, _), rw [← of_real_norm_eq_coe_nnnorm, ennreal.of_real_rpow_of_nonneg (norm_nonneg _) hpq.nonneg], }, have h_right_g : ∫⁻ a, ennreal.of_real (‖g a‖ ^ q) ∂μ = ∫⁻ a, ‖g a‖₊ ^ q ∂μ, { refine lintegral_congr (λ x, _), rw [← of_real_norm_eq_coe_nnnorm, ennreal.of_real_rpow_of_nonneg (norm_nonneg _) hpq.symm.nonneg], }, rw [h_left, h_right_f, h_right_g], -- we can now apply `ennreal.lintegral_mul_le_Lp_mul_Lq` (up to the `to_real` application) refine ennreal.to_real_mono _ _, { refine ennreal.mul_ne_top _ _, { convert hf.snorm_ne_top, rw snorm_eq_lintegral_rpow_nnnorm, { rw ennreal.to_real_of_real hpq.nonneg, }, { rw [ne.def, ennreal.of_real_eq_zero, not_le], exact hpq.pos, }, { exact ennreal.coe_ne_top, }, }, { convert hg.snorm_ne_top, rw snorm_eq_lintegral_rpow_nnnorm, { rw ennreal.to_real_of_real hpq.symm.nonneg, }, { rw [ne.def, ennreal.of_real_eq_zero, not_le], exact hpq.symm.pos, }, { exact ennreal.coe_ne_top, }, }, }, { exact ennreal.lintegral_mul_le_Lp_mul_Lq μ hpq hf.1.nnnorm.ae_measurable.coe_nnreal_ennreal hg.1.nnnorm.ae_measurable.coe_nnreal_ennreal, }, end /-- Hölder's inequality for functions `α → ℝ`. The integral of the product of two nonnegative functions is bounded by the product of their `ℒp` and `ℒq` seminorms when `p` and `q` are conjugate exponents. -/ theorem integral_mul_le_Lp_mul_Lq_of_nonneg {p q : ℝ} (hpq : p.is_conjugate_exponent q) {f g : α → ℝ} (hf_nonneg : 0 ≤ᵐ[μ] f) (hg_nonneg : 0 ≤ᵐ[μ] g) (hf : mem_ℒp f (ennreal.of_real p) μ) (hg : mem_ℒp g (ennreal.of_real q) μ) : ∫ a, f a * g a ∂μ ≤ (∫ a, (f a) ^ p ∂μ) ^ (1/p) * (∫ a, (g a) ^ q ∂μ) ^ (1/q) := begin have h_left : ∫ a, f a * g a ∂μ = ∫ a, ‖f a‖ * ‖g a‖ ∂μ, { refine integral_congr_ae _, filter_upwards [hf_nonneg, hg_nonneg] with x hxf hxg, rw [real.norm_of_nonneg hxf, real.norm_of_nonneg hxg], }, have h_right_f : ∫ a, (f a) ^ p ∂μ = ∫ a, ‖f a‖ ^ p ∂μ, { refine integral_congr_ae _, filter_upwards [hf_nonneg] with x hxf, rw real.norm_of_nonneg hxf, }, have h_right_g : ∫ a, (g a) ^ q ∂μ = ∫ a, ‖g a‖ ^ q ∂μ, { refine integral_congr_ae _, filter_upwards [hg_nonneg] with x hxg, rw real.norm_of_nonneg hxg, }, rw [h_left, h_right_f, h_right_g], exact integral_mul_norm_le_Lp_mul_Lq hpq hf hg, end end properties mk_simp_attribute integral_simps "Simp set for integral rules." attribute [integral_simps] integral_neg integral_smul L1.integral_add L1.integral_sub L1.integral_smul L1.integral_neg attribute [irreducible] integral L1.integral section integral_trim variables {H β γ : Type*} [normed_add_comm_group H] {m m0 : measurable_space β} {μ : measure β} /-- Simple function seen as simple function of a larger `measurable_space`. -/ def simple_func.to_larger_space (hm : m ≤ m0) (f : @simple_func β m γ) : simple_func β γ := ⟨@simple_func.to_fun β m γ f, λ x, hm _ (@simple_func.measurable_set_fiber β γ m f x), @simple_func.finite_range β γ m f⟩ lemma simple_func.coe_to_larger_space_eq (hm : m ≤ m0) (f : @simple_func β m γ) : ⇑(f.to_larger_space hm) = f := rfl lemma integral_simple_func_larger_space (hm : m ≤ m0) (f : @simple_func β m F) (hf_int : integrable f μ) : ∫ x, f x ∂μ = ∑ x in (@simple_func.range β F m f), (ennreal.to_real (μ (f ⁻¹' {x}))) • x := begin simp_rw ← f.coe_to_larger_space_eq hm, have hf_int : integrable (f.to_larger_space hm) μ, by rwa simple_func.coe_to_larger_space_eq, rw simple_func.integral_eq_sum _ hf_int, congr, end lemma integral_trim_simple_func (hm : m ≤ m0) (f : @simple_func β m F) (hf_int : integrable f μ) : ∫ x, f x ∂μ = ∫ x, f x ∂(μ.trim hm) := begin have hf : strongly_measurable[m] f, from @simple_func.strongly_measurable β F m _ f, have hf_int_m := hf_int.trim hm hf, rw [integral_simple_func_larger_space (le_refl m) f hf_int_m, integral_simple_func_larger_space hm f hf_int], congr' with x, congr, exact (trim_measurable_set_eq hm (@simple_func.measurable_set_fiber β F m f x)).symm, end lemma integral_trim (hm : m ≤ m0) {f : β → F} (hf : strongly_measurable[m] f) : ∫ x, f x ∂μ = ∫ x, f x ∂(μ.trim hm) := begin borelize F, by_cases hf_int : integrable f μ, swap, { have hf_int_m : ¬ integrable f (μ.trim hm), from λ hf_int_m, hf_int (integrable_of_integrable_trim hm hf_int_m), rw [integral_undef hf_int, integral_undef hf_int_m], }, haveI : separable_space (range f ∪ {0} : set F) := hf.separable_space_range_union_singleton, let f_seq := @simple_func.approx_on F β _ _ _ m _ hf.measurable (range f ∪ {0}) 0 (by simp) _, have hf_seq_meas : ∀ n, strongly_measurable[m] (f_seq n), from λ n, @simple_func.strongly_measurable β F m _ (f_seq n), have hf_seq_int : ∀ n, integrable (f_seq n) μ, from simple_func.integrable_approx_on_range (hf.mono hm).measurable hf_int, have hf_seq_int_m : ∀ n, integrable (f_seq n) (μ.trim hm), from λ n, (hf_seq_int n).trim hm (hf_seq_meas n) , have hf_seq_eq : ∀ n, ∫ x, f_seq n x ∂μ = ∫ x, f_seq n x ∂(μ.trim hm), from λ n, integral_trim_simple_func hm (f_seq n) (hf_seq_int n), have h_lim_1 : at_top.tendsto (λ n, ∫ x, f_seq n x ∂μ) (𝓝 (∫ x, f x ∂μ)), { refine tendsto_integral_of_L1 f hf_int (eventually_of_forall hf_seq_int) _, exact simple_func.tendsto_approx_on_range_L1_nnnorm (hf.mono hm).measurable hf_int, }, have h_lim_2 : at_top.tendsto (λ n, ∫ x, f_seq n x ∂μ) (𝓝 (∫ x, f x ∂(μ.trim hm))), { simp_rw hf_seq_eq, refine @tendsto_integral_of_L1 β F _ _ _ m (μ.trim hm) _ f (hf_int.trim hm hf) _ _ (eventually_of_forall hf_seq_int_m) _, exact @simple_func.tendsto_approx_on_range_L1_nnnorm β F m _ _ _ f _ _ hf.measurable (hf_int.trim hm hf), }, exact tendsto_nhds_unique h_lim_1 h_lim_2, end lemma integral_trim_ae (hm : m ≤ m0) {f : β → F} (hf : ae_strongly_measurable f (μ.trim hm)) : ∫ x, f x ∂μ = ∫ x, f x ∂(μ.trim hm) := begin rw [integral_congr_ae (ae_eq_of_ae_eq_trim hf.ae_eq_mk), integral_congr_ae hf.ae_eq_mk], exact integral_trim hm hf.strongly_measurable_mk, end lemma ae_eq_trim_of_strongly_measurable [topological_space γ] [metrizable_space γ] (hm : m ≤ m0) {f g : β → γ} (hf : strongly_measurable[m] f) (hg : strongly_measurable[m] g) (hfg : f =ᵐ[μ] g) : f =ᵐ[μ.trim hm] g := begin rwa [eventually_eq, ae_iff, trim_measurable_set_eq hm _], exact (hf.measurable_set_eq_fun hg).compl end lemma ae_eq_trim_iff [topological_space γ] [metrizable_space γ] (hm : m ≤ m0) {f g : β → γ} (hf : strongly_measurable[m] f) (hg : strongly_measurable[m] g) : f =ᵐ[μ.trim hm] g ↔ f =ᵐ[μ] g := ⟨ae_eq_of_ae_eq_trim, ae_eq_trim_of_strongly_measurable hm hf hg⟩ lemma ae_le_trim_of_strongly_measurable [linear_order γ] [topological_space γ] [order_closed_topology γ] [pseudo_metrizable_space γ] (hm : m ≤ m0) {f g : β → γ} (hf : strongly_measurable[m] f) (hg : strongly_measurable[m] g) (hfg : f ≤ᵐ[μ] g) : f ≤ᵐ[μ.trim hm] g := begin rwa [eventually_le, ae_iff, trim_measurable_set_eq hm _], exact (hf.measurable_set_le hg).compl, end lemma ae_le_trim_iff [linear_order γ] [topological_space γ] [order_closed_topology γ] [pseudo_metrizable_space γ] (hm : m ≤ m0) {f g : β → γ} (hf : strongly_measurable[m] f) (hg : strongly_measurable[m] g) : f ≤ᵐ[μ.trim hm] g ↔ f ≤ᵐ[μ] g := ⟨ae_le_of_ae_le_trim, ae_le_trim_of_strongly_measurable hm hf hg⟩ end integral_trim section snorm_bound variables {m0 : measurable_space α} {μ : measure α} lemma snorm_one_le_of_le {r : ℝ≥0} {f : α → ℝ} (hfint : integrable f μ) (hfint' : 0 ≤ ∫ x, f x ∂μ) (hf : ∀ᵐ ω ∂μ, f ω ≤ r) : snorm f 1 μ ≤ 2 * μ set.univ * r := begin by_cases hr : r = 0, { suffices : f =ᵐ[μ] 0, { rw [snorm_congr_ae this, snorm_zero, hr, ennreal.coe_zero, mul_zero], exact le_rfl }, rw [hr, nonneg.coe_zero] at hf, have hnegf : ∫ x, -f x ∂μ = 0, { rw [integral_neg, neg_eq_zero], exact le_antisymm (integral_nonpos_of_ae hf) hfint' }, have := (integral_eq_zero_iff_of_nonneg_ae _ hfint.neg).1 hnegf, { filter_upwards [this] with ω hω, rwa [pi.neg_apply, pi.zero_apply, neg_eq_zero] at hω }, { filter_upwards [hf] with ω hω, rwa [pi.zero_apply, pi.neg_apply, right.nonneg_neg_iff] } }, by_cases hμ : is_finite_measure μ, swap, { have : μ set.univ = ∞, { by_contra hμ', exact hμ (is_finite_measure.mk $ lt_top_iff_ne_top.2 hμ') }, rw [this, ennreal.mul_top, if_neg, ennreal.top_mul, if_neg], { exact le_top }, { simp [hr] }, { norm_num } }, haveI := hμ, rw [integral_eq_integral_pos_part_sub_integral_neg_part hfint, sub_nonneg] at hfint', have hposbdd : ∫ ω, max (f ω) 0 ∂μ ≤ (μ set.univ).to_real • r, { rw ← integral_const, refine integral_mono_ae hfint.real_to_nnreal (integrable_const r) _, filter_upwards [hf] with ω hω using real.to_nnreal_le_iff_le_coe.2 hω }, rw [mem_ℒp.snorm_eq_integral_rpow_norm one_ne_zero ennreal.one_ne_top (mem_ℒp_one_iff_integrable.2 hfint), ennreal.of_real_le_iff_le_to_real (ennreal.mul_ne_top (ennreal.mul_ne_top ennreal.two_ne_top $ @measure_ne_top _ _ _ hμ _) ennreal.coe_ne_top)], simp_rw [ennreal.one_to_real, _root_.inv_one, real.rpow_one, real.norm_eq_abs, ← max_zero_add_max_neg_zero_eq_abs_self, ← real.coe_to_nnreal'], rw integral_add hfint.real_to_nnreal, { simp only [real.coe_to_nnreal', ennreal.to_real_mul, ennreal.to_real_bit0, ennreal.one_to_real, ennreal.coe_to_real] at hfint' ⊢, refine (add_le_add_left hfint' _).trans _, rwa [← two_mul, mul_assoc, mul_le_mul_left (two_pos : (0 : ℝ) < 2)] }, { exact hfint.neg.sup (integrable_zero _ _ μ) } end lemma snorm_one_le_of_le' {r : ℝ} {f : α → ℝ} (hfint : integrable f μ) (hfint' : 0 ≤ ∫ x, f x ∂μ) (hf : ∀ᵐ ω ∂μ, f ω ≤ r) : snorm f 1 μ ≤ 2 * μ set.univ * ennreal.of_real r := begin refine snorm_one_le_of_le hfint hfint' _, simp only [real.coe_to_nnreal', le_max_iff], filter_upwards [hf] with ω hω using or.inl hω, end end snorm_bound end measure_theory
344a864d59676bb414d86744779755c0fea0e292
0ed3609caf1962115b28aeb010d2bda5f67ddc4c
/src/data/set/intervals/basic.lean
c36e13fa5d85d3e4842917b1b77eca1ec32b0661
[ "Apache-2.0" ]
permissive
jonaslippert/mathlib
82dba29632969e3ed1c153a6454306f6bc9d9037
1435a196db69a7886a11e310e8923f3dcf249b81
refs/heads/master
1,609,938,673,069
1,582,018,388,000
1,582,018,388,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
28,375
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot, Yury Kudryashov -/ import order.lattice algebra.order_functions algebra.ordered_field tactic.tauto /-! # Intervals In any preorder `α`, we define intervals (which on each side can be either infinite, open, or closed) using the following naming conventions: - `i`: infinite - `o`: open - `c`: closed Each interval has the name `I` + letter for left side + letter for right side. For instance, `Ioc a b` denotes the inverval `(a, b]`. This file contains these definitions, and basic facts on inclusion, intersection, difference of intervals (where the precise statements may depend on the properties of the order, in particular for some statements it should be `linear_order` or `densely_ordered`). This file also contains statements on lower and upper bounds of intervals. TODO: This is just the beginning; a lot of rules are missing -/ universe u namespace set open set section intervals variables {α : Type u} [preorder α] {a a₁ a₂ b b₁ b₂ x : α} /-- Left-open right-open interval -/ def Ioo (a b : α) := {x | a < x ∧ x < b} /-- Left-closed right-open interval -/ def Ico (a b : α) := {x | a ≤ x ∧ x < b} /-- Left-infinite right-open interval -/ def Iio (a : α) := {x | x < a} /-- Left-closed right-closed interval -/ def Icc (a b : α) := {x | a ≤ x ∧ x ≤ b} /-- Left-infinite right-closed interval -/ def Iic (b : α) := {x | x ≤ b} /-- Left-open right-closed interval -/ def Ioc (a b : α) := {x | a < x ∧ x ≤ b} /-- Left-closed right-infinite interval -/ def Ici (a : α) := {x | a ≤ x} /-- Left-open right-infinite interval -/ def Ioi (a : α) := {x | a < x} @[simp] lemma mem_Ioo : x ∈ Ioo a b ↔ a < x ∧ x < b := iff.rfl @[simp] lemma mem_Ico : x ∈ Ico a b ↔ a ≤ x ∧ x < b := iff.rfl @[simp] lemma mem_Iio : x ∈ Iio b ↔ x < b := iff.rfl @[simp] lemma mem_Icc : x ∈ Icc a b ↔ a ≤ x ∧ x ≤ b := iff.rfl @[simp] lemma mem_Iic : x ∈ Iic b ↔ x ≤ b := iff.rfl @[simp] lemma mem_Ioc : x ∈ Ioc a b ↔ a < x ∧ x ≤ b := iff.rfl @[simp] lemma mem_Ici : x ∈ Ici a ↔ a ≤ x := iff.rfl @[simp] lemma mem_Ioi : x ∈ Ioi a ↔ a < x := iff.rfl @[simp] lemma left_mem_Ioo : a ∈ Ioo a b ↔ false := by simp [lt_irrefl] @[simp] lemma left_mem_Ico : a ∈ Ico a b ↔ a < b := by simp [le_refl] @[simp] lemma left_mem_Icc : a ∈ Icc a b ↔ a ≤ b := by simp [le_refl] @[simp] lemma left_mem_Ioc : a ∈ Ioc a b ↔ false := by simp [lt_irrefl] lemma left_mem_Ici : a ∈ Ici a := by simp @[simp] lemma right_mem_Ioo : b ∈ Ioo a b ↔ false := by simp [lt_irrefl] @[simp] lemma right_mem_Ico : b ∈ Ico a b ↔ false := by simp [lt_irrefl] @[simp] lemma right_mem_Icc : b ∈ Icc a b ↔ a ≤ b := by simp [le_refl] @[simp] lemma right_mem_Ioc : b ∈ Ioc a b ↔ a < b := by simp [le_refl] lemma right_mem_Iic : a ∈ Iic a := by simp @[simp] lemma dual_Ici : @Ici (order_dual α) _ a = @Iic α _ a := rfl @[simp] lemma dual_Iic : @Iic (order_dual α) _ a = @Ici α _ a := rfl @[simp] lemma dual_Ioi : @Ioi (order_dual α) _ a = @Iio α _ a := rfl @[simp] lemma dual_Iio : @Iio (order_dual α) _ a = @Ioi α _ a := rfl @[simp] lemma dual_Icc : @Icc (order_dual α) _ a b = @Icc α _ b a := set.ext $ λ x, and_comm _ _ @[simp] lemma dual_Ioc : @Ioc (order_dual α) _ a b = @Ico α _ b a := set.ext $ λ x, and_comm _ _ @[simp] lemma dual_Ico : @Ico (order_dual α) _ a b = @Ioc α _ b a := set.ext $ λ x, and_comm _ _ @[simp] lemma dual_Ioo : @Ioo (order_dual α) _ a b = @Ioo α _ b a := set.ext $ λ x, and_comm _ _ @[simp] lemma nonempty_Icc : (Icc a b).nonempty ↔ a ≤ b := ⟨λ ⟨x, hx⟩, le_trans hx.1 hx.2, λ h, ⟨a, left_mem_Icc.2 h⟩⟩ @[simp] lemma nonempty_Ico : (Ico a b).nonempty ↔ a < b := ⟨λ ⟨x, hx⟩, lt_of_le_of_lt hx.1 hx.2, λ h, ⟨a, left_mem_Ico.2 h⟩⟩ @[simp] lemma nonempty_Ioc : (Ioc a b).nonempty ↔ a < b := ⟨λ ⟨x, hx⟩, lt_of_lt_of_le hx.1 hx.2, λ h, ⟨b, right_mem_Ioc.2 h⟩⟩ @[simp] lemma nonempty_Ici : (Ici a).nonempty := ⟨a, left_mem_Ici⟩ @[simp] lemma nonempty_Iic : (Iic a).nonempty := ⟨a, right_mem_Iic⟩ @[simp] lemma nonempty_Ioo [densely_ordered α] : (Ioo a b).nonempty ↔ a < b := ⟨λ ⟨x, ha, hb⟩, lt_trans ha hb, dense⟩ @[simp] lemma nonempty_Ioi [no_top_order α] : (Ioi a).nonempty := no_top a @[simp] lemma nonempty_Iio [no_bot_order α] : (Iio a).nonempty := no_bot a @[simp] lemma Ioo_eq_empty (h : b ≤ a) : Ioo a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x ⟨h₁, h₂⟩, not_le_of_lt (lt_trans h₁ h₂) h @[simp] lemma Ico_eq_empty (h : b ≤ a) : Ico a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x ⟨h₁, h₂⟩, not_le_of_lt (lt_of_le_of_lt h₁ h₂) h @[simp] lemma Icc_eq_empty (h : b < a) : Icc a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x ⟨h₁, h₂⟩, not_lt_of_le (le_trans h₁ h₂) h @[simp] lemma Ioc_eq_empty (h : b ≤ a) : Ioc a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x ⟨h₁, h₂⟩, not_lt_of_le (le_trans h₂ h) h₁ @[simp] lemma Ioo_self (a : α) : Ioo a a = ∅ := Ioo_eq_empty $ le_refl _ @[simp] lemma Ico_self (a : α) : Ico a a = ∅ := Ico_eq_empty $ le_refl _ @[simp] lemma Ioc_self (a : α) : Ioc a a = ∅ := Ioc_eq_empty $ le_refl _ lemma Ici_subset_Ici : Ici a ⊆ Ici b ↔ b ≤ a := ⟨λ h, h $ left_mem_Ici, λ h x hx, le_trans h hx⟩ lemma Iic_subset_Iic : Iic a ⊆ Iic b ↔ a ≤ b := @Ici_subset_Ici (order_dual α) _ _ _ lemma Ici_subset_Ioi : Ici a ⊆ Ioi b ↔ b < a := ⟨λ h, h left_mem_Ici, λ h x hx, lt_of_lt_of_le h hx⟩ lemma Iic_subset_Iio : Iic a ⊆ Iio b ↔ a < b := ⟨λ h, h right_mem_Iic, λ h x hx, lt_of_le_of_lt hx h⟩ lemma Ioo_subset_Ioo (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Ioo a₁ b₁ ⊆ Ioo a₂ b₂ := λ x ⟨hx₁, hx₂⟩, ⟨lt_of_le_of_lt h₁ hx₁, lt_of_lt_of_le hx₂ h₂⟩ lemma Ioo_subset_Ioo_left (h : a₁ ≤ a₂) : Ioo a₂ b ⊆ Ioo a₁ b := Ioo_subset_Ioo h (le_refl _) lemma Ioo_subset_Ioo_right (h : b₁ ≤ b₂) : Ioo a b₁ ⊆ Ioo a b₂ := Ioo_subset_Ioo (le_refl _) h lemma Ico_subset_Ico (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Ico a₁ b₁ ⊆ Ico a₂ b₂ := λ x ⟨hx₁, hx₂⟩, ⟨le_trans h₁ hx₁, lt_of_lt_of_le hx₂ h₂⟩ lemma Ico_subset_Ico_left (h : a₁ ≤ a₂) : Ico a₂ b ⊆ Ico a₁ b := Ico_subset_Ico h (le_refl _) lemma Ico_subset_Ico_right (h : b₁ ≤ b₂) : Ico a b₁ ⊆ Ico a b₂ := Ico_subset_Ico (le_refl _) h lemma Icc_subset_Icc (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Icc a₁ b₁ ⊆ Icc a₂ b₂ := λ x ⟨hx₁, hx₂⟩, ⟨le_trans h₁ hx₁, le_trans hx₂ h₂⟩ lemma Icc_subset_Icc_left (h : a₁ ≤ a₂) : Icc a₂ b ⊆ Icc a₁ b := Icc_subset_Icc h (le_refl _) lemma Icc_subset_Icc_right (h : b₁ ≤ b₂) : Icc a b₁ ⊆ Icc a b₂ := Icc_subset_Icc (le_refl _) h lemma Ioc_subset_Ioc (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Ioc a₁ b₁ ⊆ Ioc a₂ b₂ := λ x ⟨hx₁, hx₂⟩, ⟨lt_of_le_of_lt h₁ hx₁, le_trans hx₂ h₂⟩ lemma Ioc_subset_Ioc_left (h : a₁ ≤ a₂) : Ioc a₂ b ⊆ Ioc a₁ b := Ioc_subset_Ioc h (le_refl _) lemma Ioc_subset_Ioc_right (h : b₁ ≤ b₂) : Ioc a b₁ ⊆ Ioc a b₂ := Ioc_subset_Ioc (le_refl _) h lemma Ico_subset_Ioo_left (h₁ : a₁ < a₂) : Ico a₂ b ⊆ Ioo a₁ b := λ x, and.imp_left $ lt_of_lt_of_le h₁ lemma Icc_subset_Ico_right (h₁ : b₁ < b₂) : Icc a b₁ ⊆ Ico a b₂ := λ x, and.imp_right $ λ h₂, lt_of_le_of_lt h₂ h₁ lemma Ioo_subset_Ico_self : Ioo a b ⊆ Ico a b := λ x, and.imp_left le_of_lt lemma Ioo_subset_Ioc_self : Ioo a b ⊆ Ioc a b := λ x, and.imp_right le_of_lt lemma Ico_subset_Icc_self : Ico a b ⊆ Icc a b := λ x, and.imp_right le_of_lt lemma Ioc_subset_Icc_self : Ioc a b ⊆ Icc a b := λ x, and.imp_left le_of_lt lemma Ioo_subset_Icc_self : Ioo a b ⊆ Icc a b := subset.trans Ioo_subset_Ico_self Ico_subset_Icc_self lemma Ico_subset_Iio_self : Ico a b ⊆ Iio b := λ x, and.right lemma Ioo_subset_Iio_self : Ioo a b ⊆ Iio b := λ x, and.right lemma Ioc_subset_Ioi_self : Ioc a b ⊆ Ioi a := λ x, and.left lemma Ioo_subset_Ioi_self : Ioo a b ⊆ Ioi a := λ x, and.left lemma Ioi_subset_Ici_self : Ioi a ⊆ Ici a := λx hx, le_of_lt hx lemma Iio_subset_Iic_self : Iio a ⊆ Iic a := λx hx, le_of_lt hx lemma Icc_subset_Icc_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Icc a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := ⟨λ h, ⟨(h ⟨le_refl _, h₁⟩).1, (h ⟨h₁, le_refl _⟩).2⟩, λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨le_trans h hx, le_trans hx' h'⟩⟩ lemma Icc_subset_Ioo_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioo a₂ b₂ ↔ a₂ < a₁ ∧ b₁ < b₂ := ⟨λ h, ⟨(h ⟨le_refl _, h₁⟩).1, (h ⟨h₁, le_refl _⟩).2⟩, λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨lt_of_lt_of_le h hx, lt_of_le_of_lt hx' h'⟩⟩ lemma Icc_subset_Ico_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ico a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ < b₂ := ⟨λ h, ⟨(h ⟨le_refl _, h₁⟩).1, (h ⟨h₁, le_refl _⟩).2⟩, λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨le_trans h hx, lt_of_le_of_lt hx' h'⟩⟩ lemma Icc_subset_Ioc_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioc a₂ b₂ ↔ a₂ < a₁ ∧ b₁ ≤ b₂ := ⟨λ h, ⟨(h ⟨le_refl _, h₁⟩).1, (h ⟨h₁, le_refl _⟩).2⟩, λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨lt_of_lt_of_le h hx, le_trans hx' h'⟩⟩ lemma Icc_subset_Iio_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Iio b₂ ↔ b₁ < b₂ := ⟨λ h, h ⟨h₁, le_refl _⟩, λ h x ⟨hx, hx'⟩, lt_of_le_of_lt hx' h⟩ lemma Icc_subset_Ioi_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioi a₂ ↔ a₂ < a₁ := ⟨λ h, h ⟨le_refl _, h₁⟩, λ h x ⟨hx, hx'⟩, lt_of_lt_of_le h hx⟩ lemma Icc_subset_Iic_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Iic b₂ ↔ b₁ ≤ b₂ := ⟨λ h, h ⟨h₁, le_refl _⟩, λ h x ⟨hx, hx'⟩, le_trans hx' h⟩ lemma Icc_subset_Ici_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ici a₂ ↔ a₂ ≤ a₁ := ⟨λ h, h ⟨le_refl _, h₁⟩, λ h x ⟨hx, hx'⟩, le_trans h hx⟩ /-- If `a ≤ b`, then `(b, +∞) ⊆ (a, +∞)`. In preorders, this is just an implication. If you need the equivalence in linear orders, use `Ioi_subset_Ioi_iff`. -/ lemma Ioi_subset_Ioi (h : a ≤ b) : Ioi b ⊆ Ioi a := λx hx, lt_of_le_of_lt h hx /-- If `a ≤ b`, then `(b, +∞) ⊆ [a, +∞)`. In preorders, this is just an implication. If you need the equivalence in dense linear orders, use `Ioi_subset_Ici_iff`. -/ lemma Ioi_subset_Ici (h : a ≤ b) : Ioi b ⊆ Ici a := subset.trans (Ioi_subset_Ioi h) Ioi_subset_Ici_self /-- If `a ≤ b`, then `(-∞, a) ⊆ (-∞, b)`. In preorders, this is just an implication. If you need the equivalence in linear orders, use `Iio_subset_Iio_iff`. -/ lemma Iio_subset_Iio (h : a ≤ b) : Iio a ⊆ Iio b := λx hx, lt_of_lt_of_le hx h /-- If `a ≤ b`, then `(-∞, a) ⊆ (-∞, b]`. In preorders, this is just an implication. If you need the equivalence in dense linear orders, use `Iio_subset_Iic_iff`. -/ lemma Iio_subset_Iic (h : a ≤ b) : Iio a ⊆ Iic b := subset.trans (Iio_subset_Iio h) Iio_subset_Iic_self lemma Ici_inter_Iic : Ici a ∩ Iic b = Icc a b := rfl lemma Ici_inter_Iio : Ici a ∩ Iio b = Ico a b := rfl lemma Ioi_inter_Iic : Ioi a ∩ Iic b = Ioc a b := rfl lemma Ioi_inter_Iio : Ioi a ∩ Iio b = Ioo a b := rfl end intervals section partial_order variables {α : Type u} [partial_order α] {a b : α} @[simp] lemma Icc_self (a : α) : Icc a a = {a} := set.ext $ by simp [Icc, le_antisymm_iff, and_comm] lemma Ico_diff_Ioo_eq_singleton (h : a < b) : Ico a b \ Ioo a b = {a} := set.ext $ λ x, begin simp [not_and'], split, { rintro ⟨⟨ax, xb⟩, hne⟩, exact (eq_or_lt_of_le ax).elim eq.symm (λ h', absurd h' (hne xb)) }, { rintro rfl, exact ⟨⟨le_refl _, h⟩, λ _, lt_irrefl x⟩ } end lemma Ioc_diff_Ioo_eq_singleton (h : a < b) : Ioc a b \ Ioo a b = {b} := set.ext $ λ x, begin simp, split, { rintro ⟨⟨ax, xb⟩, hne⟩, exact (eq_or_lt_of_le xb).elim id (λ h', absurd h' (hne ax)) }, { rintro rfl, exact ⟨⟨h, le_refl _⟩, λ _, lt_irrefl x⟩ } end lemma Icc_diff_Ico_eq_singleton (h : a ≤ b) : Icc a b \ Ico a b = {b} := set.ext $ λ x, begin simp, split, { rintro ⟨⟨ax, xb⟩, h⟩, exact classical.by_contradiction (λ ne, h ax (lt_of_le_of_ne xb ne)) }, { rintro rfl, exact ⟨⟨h, le_refl _⟩, λ _, lt_irrefl x⟩ } end lemma Icc_diff_Ioc_eq_singleton (h : a ≤ b) : Icc a b \ Ioc a b = {a} := set.ext $ λ x, begin simp [not_and'], split, { rintro ⟨⟨ax, xb⟩, h⟩, exact classical.by_contradiction (λ hne, h xb (lt_of_le_of_ne ax (ne.symm hne))) }, { rintro rfl, exact ⟨⟨le_refl _, h⟩, λ _, lt_irrefl x⟩ } end end partial_order section linear_order variables {α : Type u} [linear_order α] {a a₁ a₂ b b₁ b₂ : α} lemma Ioo_eq_empty_iff [densely_ordered α] : Ioo a b = ∅ ↔ b ≤ a := ⟨λ eq, le_of_not_lt $ λ h, let ⟨x, h₁, h₂⟩ := dense h in eq_empty_iff_forall_not_mem.1 eq x ⟨h₁, h₂⟩, Ioo_eq_empty⟩ lemma Ico_eq_empty_iff : Ico a b = ∅ ↔ b ≤ a := ⟨λ eq, le_of_not_lt $ λ h, eq_empty_iff_forall_not_mem.1 eq a ⟨le_refl _, h⟩, Ico_eq_empty⟩ lemma Icc_eq_empty_iff : Icc a b = ∅ ↔ b < a := ⟨λ eq, lt_of_not_ge $ λ h, eq_empty_iff_forall_not_mem.1 eq a ⟨le_refl _, h⟩, Icc_eq_empty⟩ lemma Ico_subset_Ico_iff (h₁ : a₁ < b₁) : Ico a₁ b₁ ⊆ Ico a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := ⟨λ h, have a₂ ≤ a₁ ∧ a₁ < b₂ := h ⟨le_refl _, h₁⟩, ⟨this.1, le_of_not_lt $ λ h', lt_irrefl b₂ (h ⟨le_of_lt this.2, h'⟩).2⟩, λ ⟨h₁, h₂⟩, Ico_subset_Ico h₁ h₂⟩ lemma Ioo_subset_Ioo_iff [densely_ordered α] (h₁ : a₁ < b₁) : Ioo a₁ b₁ ⊆ Ioo a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := ⟨λ h, begin rcases dense h₁ with ⟨x, xa, xb⟩, split; refine le_of_not_lt (λ h', _), { have ab := lt_trans (h ⟨xa, xb⟩).1 xb, exact lt_irrefl _ (h ⟨h', ab⟩).1 }, { have ab := lt_trans xa (h ⟨xa, xb⟩).2, exact lt_irrefl _ (h ⟨ab, h'⟩).2 } end, λ ⟨h₁, h₂⟩, Ioo_subset_Ioo h₁ h₂⟩ lemma Ico_eq_Ico_iff (h : a₁ < b₁ ∨ a₂ < b₂) : Ico a₁ b₁ = Ico a₂ b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ := ⟨λ e, begin simp [subset.antisymm_iff] at e, simp [le_antisymm_iff], cases h; simp [Ico_subset_Ico_iff h] at e; [ rcases e with ⟨⟨h₁, h₂⟩, e'⟩, rcases e with ⟨e', ⟨h₁, h₂⟩⟩ ]; have := (Ico_subset_Ico_iff (lt_of_le_of_lt h₁ $ lt_of_lt_of_le h h₂)).1 e'; tauto end, λ ⟨h₁, h₂⟩, by rw [h₁, h₂]⟩ open_locale classical @[simp] lemma Ioi_subset_Ioi_iff : Ioi b ⊆ Ioi a ↔ a ≤ b := begin refine ⟨λh, _, λh, Ioi_subset_Ioi h⟩, by_contradiction ba, exact lt_irrefl _ (h (not_le.mp ba)) end @[simp] lemma Ioi_subset_Ici_iff [densely_ordered α] : Ioi b ⊆ Ici a ↔ a ≤ b := begin refine ⟨λh, _, λh, Ioi_subset_Ici h⟩, by_contradiction ba, obtain ⟨c, bc, ca⟩ : ∃c, b < c ∧ c < a := dense (not_le.mp ba), exact lt_irrefl _ (lt_of_lt_of_le ca (h bc)) end @[simp] lemma Iio_subset_Iio_iff : Iio a ⊆ Iio b ↔ a ≤ b := begin refine ⟨λh, _, λh, Iio_subset_Iio h⟩, by_contradiction ab, exact lt_irrefl _ (h (not_le.mp ab)) end @[simp] lemma Iio_subset_Iic_iff [densely_ordered α] : Iio a ⊆ Iic b ↔ a ≤ b := begin refine ⟨λh, _, λh, Iio_subset_Iic h⟩, by_contradiction ba, obtain ⟨c, bc, ca⟩ : ∃c, b < c ∧ c < a := dense (not_le.mp ba), exact lt_irrefl _ (lt_of_lt_of_le bc (h ca)) end /-! ### Unions of adjacent intervals -/ /-! #### Two infinite intervals -/ @[simp] lemma Iic_union_Ici : Iic a ∪ Ici a = univ := eq_univ_of_forall (λ x, le_total x a) @[simp] lemma Iio_union_Ici : Iio a ∪ Ici a = univ := eq_univ_of_forall (λ x, lt_or_le x a) @[simp] lemma Iic_union_Ioi : Iic a ∪ Ioi a = univ := eq_univ_of_forall (λ x, le_or_lt x a) /-! #### A finite and an infinite interval -/ @[simp] lemma Ioc_union_Ici_eq_Ioi (h : a < b) : Ioc a b ∪ Ici b = Ioi a := ext $ λ x, ⟨λ hx, hx.elim and.left (lt_of_lt_of_le h), λ hx, (le_total x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb)⟩ @[simp] lemma Icc_union_Ici_eq_Ioi (h : a ≤ b) : Icc a b ∪ Ici b = Ici a := ext $ λ x, ⟨λ hx, hx.elim and.left (le_trans h), λ hx, (le_total x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb)⟩ @[simp] lemma Ioo_union_Ici_eq_Ioi (h : a < b) : Ioo a b ∪ Ici b = Ioi a := ext $ λ x, ⟨λ hx, hx.elim and.left (lt_of_lt_of_le h), λ hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb)⟩ @[simp] lemma Ico_union_Ici_eq_Ioi (h : a ≤ b) : Ico a b ∪ Ici b = Ici a := ext $ λ x, ⟨λ hx, hx.elim and.left (le_trans h), λ hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb)⟩ @[simp] lemma Ioc_union_Ioi_eq_Ioi (h : a ≤ b) : Ioc a b ∪ Ioi b = Ioi a := ext $ λ x, ⟨λ hx, hx.elim and.left (lt_of_le_of_lt h), λ hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb)⟩ @[simp] lemma Icc_union_Ioi_eq_Ioi (h : a ≤ b) : Icc a b ∪ Ioi b = Ici a := ext $ λ x, ⟨λ hx, hx.elim and.left (λ hx, le_trans h (le_of_lt hx)), λ hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb)⟩ /-! #### An infinite and a finite interval -/ @[simp] lemma Iic_union_Icc_eq_Iic (h : a ≤ b) : Iic a ∪ Icc a b = Iic b := ext $ λ x, ⟨λ hx, hx.elim (λ hx, le_trans hx h) and.right, λ hx, (le_total x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩)⟩ @[simp] lemma Iic_union_Ico_eq_Iio (h : a < b) : Iic a ∪ Ico a b = Iio b := ext $ λ x, ⟨λ hx, hx.elim (λ hx, lt_of_le_of_lt hx h) and.right, λ hx, (le_total x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩)⟩ @[simp] lemma Iio_union_Icc_eq_Iic (h : a ≤ b) : Iio a ∪ Icc a b = Iic b := ext $ λ x, ⟨λ hx, hx.elim (λ hx, le_trans (le_of_lt hx) h) and.right, λ hx, (lt_or_le x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩)⟩ @[simp] lemma Iio_union_Ico_eq_Iio (h : a ≤ b) : Iio a ∪ Ico a b = Iio b := ext $ λ x, ⟨λ hx, hx.elim (λ hx, lt_of_lt_of_le hx h) and.right, λ hx, (lt_or_le x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩)⟩ @[simp] lemma Iic_union_Ioc_eq_Iic (h : a ≤ b) : Iic a ∪ Ioc a b = Iic b := ext $ λ x, ⟨λ hx, hx.elim (λ hx, le_trans hx h) and.right, λ hx, (le_or_lt x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩)⟩ @[simp] lemma Iic_union_Ioo_eq_Iio (h : a < b) : Iic a ∪ Ioo a b = Iio b := ext $ λ x, ⟨λ hx, hx.elim (λ hx, lt_of_le_of_lt hx h) and.right, λ hx, (le_or_lt x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩)⟩ /-! #### Two finite intervals with a common point -/ @[simp] lemma Ioc_union_Ico_eq_Ioo {c} (h₁ : a < b) (h₂ : b < c) : Ioc a b ∪ Ico b c = Ioo a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, lt_of_le_of_lt hx.2 h₂⟩) (λ hx, ⟨lt_of_lt_of_le h₁ hx.1, hx.2⟩), λ hx, (le_total x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Icc_union_Ico_eq_Ico {c} (h₁ : a ≤ b) (h₂ : b < c) : Icc a b ∪ Ico b c = Ico a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, lt_of_le_of_lt hx.2 h₂⟩) (λ hx, ⟨le_trans h₁ hx.1, hx.2⟩), λ hx, (le_total x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Icc_union_Icc_eq_Icc {c} (h₁ : a ≤ b) (h₂ : b ≤ c) : Icc a b ∪ Icc b c = Icc a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, le_trans hx.2 h₂⟩) (λ hx, ⟨le_trans h₁ hx.1, hx.2⟩), λ hx, (le_total x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Ioc_union_Icc_eq_Ioc {c} (h₁ : a < b) (h₂ : b ≤ c) : Ioc a b ∪ Icc b c = Ioc a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, le_trans hx.2 h₂⟩) (λ hx, ⟨lt_of_lt_of_le h₁ hx.1, hx.2⟩), λ hx, (le_total x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ /-! #### Two finite intervals, `I?o` and `Ic?` -/ @[simp] lemma Ioo_union_Ico_eq_Ioo {c} (h₁ : a < b) (h₂ : b ≤ c) : Ioo a b ∪ Ico b c = Ioo a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, lt_of_lt_of_le hx.2 h₂⟩) (λ hx, ⟨lt_of_lt_of_le h₁ hx.1, hx.2⟩), λ hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Ico_union_Ico_eq_Ico {c} (h₁ : a ≤ b) (h₂ : b ≤ c) : Ico a b ∪ Ico b c = Ico a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, lt_of_lt_of_le hx.2 h₂⟩) (λ hx, ⟨le_trans h₁ hx.1, hx.2⟩), λ hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Ico_union_Icc_eq_Icc {c} (h₁ : a ≤ b) (h₂ : b ≤ c) : Ico a b ∪ Icc b c = Icc a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, le_trans (le_of_lt hx.2) h₂⟩) (λ hx, ⟨le_trans h₁ hx.1, hx.2⟩), λ hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Ioo_union_Icc_eq_Ioc {c} (h₁ : a < b) (h₂ : b ≤ c) : Ioo a b ∪ Icc b c = Ioc a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, le_trans (le_of_lt hx.2) h₂⟩) (λ hx, ⟨lt_of_lt_of_le h₁ hx.1, hx.2⟩), λ hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ /-! #### Two finite intervals, `I?c` and `Io?` -/ @[simp] lemma Ioc_union_Ioo_eq_Ioo {c} (h₁ : a ≤ b) (h₂ : b < c) : Ioc a b ∪ Ioo b c = Ioo a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, lt_of_le_of_lt hx.2 h₂⟩) (λ hx, ⟨lt_of_le_of_lt h₁ hx.1, hx.2⟩), λ hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Icc_union_Ioo_eq_Ico {c} (h₁ : a ≤ b) (h₂ : b < c) : Icc a b ∪ Ioo b c = Ico a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, lt_of_le_of_lt hx.2 h₂⟩) (λ hx, ⟨le_trans h₁ (le_of_lt hx.1), hx.2⟩), λ hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Icc_union_Ioc_eq_Icc {c} (h₁ : a ≤ b) (h₂ : b ≤ c) : Icc a b ∪ Ioc b c = Icc a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, le_trans hx.2 h₂⟩) (λ hx, ⟨le_trans h₁ (le_of_lt hx.1), hx.2⟩), λ hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Ioc_union_Ioc_eq_Ioc {c} (h₁ : a ≤ b) (h₂ : b ≤ c) : Ioc a b ∪ Ioc b c = Ioc a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, le_trans hx.2 h₂⟩) (λ hx, ⟨lt_of_le_of_lt h₁ hx.1, hx.2⟩), λ hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ end linear_order section lattice open lattice section inf variables {α : Type u} [semilattice_inf α] @[simp] lemma Iic_inter_Iic {a b : α} : Iic a ∩ Iic b = Iic (a ⊓ b) := by { ext x, simp [Iic] } @[simp] lemma Iio_inter_Iio [is_total α (≤)] {a b : α} : Iio a ∩ Iio b = Iio (a ⊓ b) := by { ext x, simp [Iio] } end inf section sup variables {α : Type u} [semilattice_sup α] @[simp] lemma Ici_inter_Ici {a b : α} : Ici a ∩ Ici b = Ici (a ⊔ b) := by { ext x, simp [Ici] } @[simp] lemma Ioi_inter_Ioi [is_total α (≤)] {a b : α} : Ioi a ∩ Ioi b = Ioi (a ⊔ b) := by { ext x, simp [Ioi] } end sup section both variables {α : Type u} [lattice α] [ht : is_total α (≤)] {a b c a₁ a₂ b₁ b₂ : α} lemma Icc_inter_Icc : Icc a₁ b₁ ∩ Icc a₂ b₂ = Icc (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ici_inter_Iic.symm, Ici_inter_Ici.symm, Iic_inter_Iic.symm]; ac_refl @[simp] lemma Icc_inter_Icc_eq_singleton (hab : a ≤ b) (hbc : b ≤ c) : Icc a b ∩ Icc b c = {b} := begin rw [Icc_inter_Icc], convert Icc_self b, exact sup_of_le_right hab, exact inf_of_le_left hbc end include ht lemma Ico_inter_Ico : Ico a₁ b₁ ∩ Ico a₂ b₂ = Ico (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ici_inter_Iio.symm, Ici_inter_Ici.symm, Iio_inter_Iio.symm]; ac_refl lemma Ioc_inter_Ioc : Ioc a₁ b₁ ∩ Ioc a₂ b₂ = Ioc (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ioi_inter_Iic.symm, Ioi_inter_Ioi.symm, Iic_inter_Iic.symm]; ac_refl lemma Ioo_inter_Ioo : Ioo a₁ b₁ ∩ Ioo a₂ b₂ = Ioo (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ioi_inter_Iio.symm, Ioi_inter_Ioi.symm, Iio_inter_Iio.symm]; ac_refl end both end lattice section decidable_linear_order variables {α : Type u} [decidable_linear_order α] {a a₁ a₂ b b₁ b₂ : α} @[simp] lemma Ico_diff_Iio {a b c : α} : Ico a b \ Iio c = Ico (max a c) b := set.ext $ by simp [Ico, Iio, iff_def, max_le_iff] {contextual:=tt} @[simp] lemma Ico_inter_Iio {a b c : α} : Ico a b ∩ Iio c = Ico a (min b c) := set.ext $ by simp [Ico, Iio, iff_def, lt_min_iff] {contextual:=tt} end decidable_linear_order section ordered_comm_group variables {α : Type u} [ordered_comm_group α] lemma image_add_left_Icc (a b c : α) : ((+) a) '' Icc b c = Icc (a + b) (a + c) := begin ext x, split, { rintros ⟨x, hx, rfl⟩, exact ⟨add_le_add_left hx.1 a, add_le_add_left hx.2 a⟩}, { intro hx, refine ⟨-a + x, _, add_neg_cancel_left _ _⟩, exact ⟨le_neg_add_iff_add_le.2 hx.1, neg_add_le_iff_le_add.2 hx.2⟩ } end lemma image_add_right_Icc (a b c : α) : (λ x, x + c) '' Icc a b = Icc (a + c) (b + c) := by convert image_add_left_Icc c a b using 1; simp only [add_comm _ c] lemma image_neg_Iio (r : α) : image (λz, -z) (Iio r) = Ioi (-r) := begin ext z, apply iff.intro, { intros hz, apply exists.elim hz, intros z' hz', rw [←hz'.2], simp only [mem_Ioi, neg_lt_neg_iff], exact hz'.1 }, { intros hz, simp only [mem_image, mem_Iio], use -z, simp [hz], exact neg_lt.1 hz } end lemma image_neg_Iic (r : α) : image (λz, -z) (Iic r) = Ici (-r) := begin apply set.ext, intros z, apply iff.intro, { intros hz, apply exists.elim hz, intros z' hz', rw [←hz'.2], simp only [neg_le_neg_iff, mem_Ici], exact hz'.1 }, { intros hz, simp only [mem_image, mem_Iic], use -z, simp [hz], exact neg_le.1 hz } end end ordered_comm_group section decidable_linear_ordered_comm_group variables {α : Type u} [decidable_linear_ordered_comm_group α] /-- If we remove a smaller interval from a larger, the result is nonempty -/ lemma nonempty_Ico_sdiff {x dx y dy : α} (h : dy < dx) (hx : 0 < dx) : nonempty ↥(Ico x (x + dx) \ Ico y (y + dy)) := begin cases lt_or_le x y with h' h', { use x, simp* }, { use max x (x + dy), simp [*, le_refl] } end end decidable_linear_ordered_comm_group section linear_ordered_field variables {α : Type u} [linear_ordered_field α] lemma image_mul_right_Icc' (a b : α) {c : α} (h : 0 < c) : (λ x, x * c) '' Icc a b = Icc (a * c) (b * c) := begin ext x, split, { rintros ⟨x, hx, rfl⟩, exact ⟨mul_le_mul_of_nonneg_right hx.1 (le_of_lt h), mul_le_mul_of_nonneg_right hx.2 (le_of_lt h)⟩ }, { intro hx, refine ⟨x / c, _, div_mul_cancel x (ne_of_gt h)⟩, exact ⟨le_div_of_mul_le h hx.1, div_le_of_le_mul h (mul_comm b c ▸ hx.2)⟩ } end lemma image_mul_right_Icc {a b c : α} (hab : a ≤ b) (hc : 0 ≤ c) : (λ x, x * c) '' Icc a b = Icc (a * c) (b * c) := begin cases eq_or_lt_of_le hc, { subst c, simp [(nonempty_Icc.2 hab).image_const] }, exact image_mul_right_Icc' a b ‹0 < c› end lemma image_mul_left_Icc' {a : α} (h : 0 < a) (b c : α) : ((*) a) '' Icc b c = Icc (a * b) (a * c) := by { convert image_mul_right_Icc' b c h using 1; simp only [mul_comm _ a] } lemma image_mul_left_Icc {a b c : α} (ha : 0 ≤ a) (hbc : b ≤ c) : ((*) a) '' Icc b c = Icc (a * b) (a * c) := by { convert image_mul_right_Icc hbc ha using 1; simp only [mul_comm _ a] } end linear_ordered_field end set
513db1923f8338328640248298c35f640b6a87b5
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/stage0/src/Init/Meta.lean
2c6c2cdec8a2a11dabec32227f1ce4a09fed221e
[ "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
29,072
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 Additional goodies for writing macros -/ prelude import Init.Data.Array.Basic namespace Lean @[extern c inline "lean_box(LEAN_VERSION_MAJOR)"] private constant version.getMajor (u : Unit) : Nat def version.major : Nat := version.getMajor () @[extern c inline "lean_box(LEAN_VERSION_MINOR)"] private constant version.getMinor (u : Unit) : Nat def version.minor : Nat := version.getMinor () @[extern c inline "lean_box(LEAN_VERSION_PATCH)"] private constant version.getPatch (u : Unit) : Nat def version.patch : Nat := version.getPatch () -- @[extern c inline "lean_mk_string(LEAN_GITHASH)"] -- constant getGithash (u : Unit) : String -- def githash : String := getGithash () @[extern c inline "LEAN_VERSION_IS_RELEASE"] constant version.getIsRelease (u : Unit) : Bool def version.isRelease : Bool := version.getIsRelease () /-- Additional version description like "nightly-2018-03-11" -/ @[extern c inline "lean_mk_string(LEAN_SPECIAL_VERSION_DESC)"] constant version.getSpecialDesc (u : Unit) : String def version.specialDesc : String := version.getSpecialDesc () /- 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 isNumericSubscript (c : Char) : Bool := 0x2080 ≤ c.val && c.val ≤ 0x2089 def isSubScriptAlnum (c : Char) : Bool := isNumericSubscript c || (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 namespace Name def toStringWithSep (sep : String) : Name → String | anonymous => "[anonymous]" | str anonymous s _ => s | num anonymous v _ => toString v | str n s _ => toStringWithSep sep n ++ sep ++ s | num n v _ => toStringWithSep sep n ++ sep ++ Nat.repr v protected def toString : Name → String := toStringWithSep "." instance : ToString Name where toString n := n.toString instance : Repr Name where reprPrec n _ := Std.Format.text "`" ++ n.toString def capitalize : Name → Name | Name.str p s _ => Name.mkStr p s.capitalize | n => n def appendAfter : Name → String → Name | str p s _, suffix => Name.mkStr p (s ++ suffix) | n, suffix => Name.mkStr n suffix def appendIndexAfter : Name → Nat → Name | str p s _, idx => Name.mkStr p (s ++ "_" ++ toString idx) | n, idx => Name.mkStr n ("_" ++ toString idx) def appendBefore : Name → String → Name | anonymous, pre => Name.mkStr anonymous pre | str p s _, pre => Name.mkStr p (pre ++ s) | num p n _, pre => Name.mkNum (Name.mkStr p pre) n def replacePrefix : Name → Name → Name → Name | anonymous, anonymous, newP => newP | anonymous, _, _ => anonymous | n@(str p s _), queryP, newP => if n == queryP then newP else Name.mkStr (p.replacePrefix queryP newP) s | n@(num p s _), queryP, newP => if n == queryP then newP else Name.mkNum (p.replacePrefix queryP newP) s end Name structure NameGenerator where namePrefix : Name := `_uniq idx : Nat := 1 deriving Inhabited namespace NameGenerator @[inline] def curr (g : NameGenerator) : Name := Name.mkNum g.namePrefix g.idx @[inline] def next (g : NameGenerator) : NameGenerator := { g with idx := g.idx + 1 } @[inline] def mkChild (g : NameGenerator) : NameGenerator × NameGenerator := ({ namePrefix := Name.mkNum g.namePrefix g.idx, idx := 1 }, { g with idx := g.idx + 1 }) end NameGenerator class MonadNameGenerator (m : Type → Type) where getNGen : m NameGenerator setNGen : NameGenerator → m Unit export MonadNameGenerator (getNGen setNGen) def mkFreshId {m : Type → Type} [Monad m] [MonadNameGenerator m] : m Name := do let ngen ← getNGen let r := ngen.curr setNGen ngen.next pure r instance monadNameGeneratorLift (m n : Type → Type) [MonadLift m n] [MonadNameGenerator m] : MonadNameGenerator n := { getNGen := liftM (getNGen : m _), setNGen := fun ngen => liftM (setNGen ngen : m _) } namespace Syntax partial def structEq : Syntax → Syntax → Bool | Syntax.missing, Syntax.missing => true | Syntax.node k args, Syntax.node k' args' => k == k' && args.isEqv args' structEq | Syntax.atom _ val, Syntax.atom _ val' => val == val' | Syntax.ident _ rawVal val preresolved, Syntax.ident _ rawVal' val' preresolved' => rawVal == rawVal' && val == val' && preresolved == preresolved' | _, _ => false instance : BEq Lean.Syntax := ⟨structEq⟩ partial def getTailInfo? : Syntax → Option SourceInfo | atom info _ => info | ident info .. => info | node _ args => args.findSomeRev? getTailInfo? | _ => none def getTailInfo (stx : Syntax) : SourceInfo := stx.getTailInfo?.getD SourceInfo.none @[specialize] private partial def updateLast {α} [Inhabited α] (a : Array α) (f : α → Option α) (i : Nat) : Option (Array α) := if i == 0 then none else let i := i - 1 let v := a[i] match f v with | some v => some <| a.set! i v | none => updateLast a f i partial def setTailInfoAux (info : SourceInfo) : Syntax → Option Syntax | atom _ val => some <| atom info val | ident _ rawVal val pre => some <| ident info rawVal val pre | node k args => match updateLast args (setTailInfoAux info) args.size with | some args => some <| node k args | none => none | stx => none def setTailInfo (stx : Syntax) (info : SourceInfo) : Syntax := match setTailInfoAux info stx with | some stx => stx | none => stx def unsetTrailing (stx : Syntax) : Syntax := match stx.getTailInfo with | SourceInfo.original lead pos trail => stx.setTailInfo (SourceInfo.original lead pos "".toSubstring) | _ => stx @[specialize] private partial def updateFirst {α} [Inhabited α] (a : Array α) (f : α → Option α) (i : Nat) : Option (Array α) := if h : i < a.size then let v := a.get ⟨i, h⟩; match f v with | some v => some <| a.set ⟨i, h⟩ v | none => updateFirst a f (i+1) else none partial def setHeadInfoAux (info : SourceInfo) : Syntax → Option Syntax | atom _ val => some <| atom info val | ident _ rawVal val pre => some <| ident info rawVal val pre | node k args => match updateFirst args (setHeadInfoAux info) 0 with | some args => some <| node k args | noxne => none | stx => none def setHeadInfo (stx : Syntax) (info : SourceInfo) : Syntax := match setHeadInfoAux info stx with | some stx => stx | none => stx def setInfo (info : SourceInfo) : Syntax → Syntax | atom _ val => atom info val | ident _ rawVal val pre => ident info rawVal val pre | stx => stx /-- Return the first atom/identifier that has position information -/ partial def getHead? : Syntax → Option Syntax | stx@(atom info ..) => info.getPos?.map fun _ => stx | stx@(ident info ..) => info.getPos?.map fun _ => stx | node _ args => args.findSome? getHead? | _ => none end Syntax /-- Use the head atom/identifier of the current `ref` as the `ref` -/ @[inline] def withHeadRefOnly {m : Type → Type} [Monad m] [MonadRef m] {α} (x : m α) : m α := do match (← getRef).getHead? with | none => x | some ref => withRef ref x def mkAtom (val : String) : Syntax := Syntax.atom SourceInfo.none val @[inline] def mkNode (k : SyntaxNodeKind) (args : Array Syntax) : Syntax := Syntax.node k args /- Syntax objects for a Lean module. -/ structure Module where header : Syntax commands : Array Syntax /-- Expand all macros in the given syntax -/ partial def expandMacros : Syntax → MacroM Syntax | stx@(Syntax.node k args) => do match (← expandMacro? stx) with | some stxNew => expandMacros stxNew | none => do let args ← Macro.withIncRecDepth stx <| args.mapM expandMacros pure <| Syntax.node k args | stx => pure stx /- Helper functions for processing Syntax programmatically -/ /-- Create an identifier copying the position from `src`. To refer to a specific constant, use `mkCIdentFrom` instead. -/ def mkIdentFrom (src : Syntax) (val : Name) : Syntax := Syntax.ident (SourceInfo.fromRef src) (toString val).toSubstring val [] def mkIdentFromRef [Monad m] [MonadRef m] (val : Name) : m Syntax := do return mkIdentFrom (← getRef) val /-- Create an identifier referring to a constant `c` copying the position from `src`. This variant of `mkIdentFrom` makes sure that the identifier cannot accidentally be captured. -/ def mkCIdentFrom (src : Syntax) (c : Name) : Syntax := -- 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 (SourceInfo.fromRef src) (toString id).toSubstring id [(c, [])] def mkCIdentFromRef [Monad m] [MonadRef m] (c : Name) : m Syntax := do return mkCIdentFrom (← getRef) c def mkCIdent (c : Name) : Syntax := mkCIdentFrom Syntax.missing c @[export lean_mk_syntax_ident] def mkIdent (val : Name) : Syntax := Syntax.ident SourceInfo.none (toString val).toSubstring val [] @[inline] def mkNullNode (args : Array Syntax := #[]) : Syntax := Syntax.node nullKind args def mkSepArray (as : Array Syntax) (sep : Syntax) : Array Syntax := do let mut i := 0 let mut r := #[] for a in as do if i > 0 then r := r.push sep |>.push a else r := r.push a i := i + 1 return r def mkOptionalNode (arg : Option Syntax) : Syntax := match arg with | some arg => Syntax.node nullKind #[arg] | none => Syntax.node nullKind #[] def mkHole (ref : Syntax) : Syntax := Syntax.node `Lean.Parser.Term.hole #[mkAtomFrom ref "_"] namespace Syntax def mkSep (a : Array Syntax) (sep : Syntax) : Syntax := mkNullNode <| mkSepArray a sep def SepArray.ofElems {sep} (elems : Array Syntax) : SepArray sep := ⟨mkSepArray elems (mkAtom sep)⟩ def SepArray.ofElemsUsingRef [Monad m] [MonadRef m] {sep} (elems : Array Syntax) : m (SepArray sep) := do let ref ← getRef; return ⟨mkSepArray elems (mkAtomFrom ref sep)⟩ instance (sep) : Coe (Array Syntax) (SepArray sep) where coe := SepArray.ofElems /-- Create syntax representing a Lean term application, but avoid degenerate empty applications. -/ def mkApp (fn : Syntax) : (args : Array Syntax) → Syntax | #[] => fn | args => Syntax.node `Lean.Parser.Term.app #[fn, mkNullNode args] def mkCApp (fn : Name) (args : Array Syntax) : Syntax := mkApp (mkCIdent fn) args def mkLit (kind : SyntaxNodeKind) (val : String) (info := SourceInfo.none) : Syntax := let atom : Syntax := Syntax.atom info val Syntax.node kind #[atom] def mkStrLit (val : String) (info := SourceInfo.none) : Syntax := mkLit strLitKind (String.quote val) info def mkNumLit (val : String) (info := SourceInfo.none) : Syntax := mkLit numLitKind val info def mkScientificLit (val : String) (info := SourceInfo.none) : Syntax := mkLit scientificLitKind val info /- 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) (i : String.Pos) (val : Nat) : Option Nat := if s.atEnd i then some val else let c := s.get i if c == '0' then decodeBinLitAux s (s.next i) (2*val) else if c == '1' then decodeBinLitAux s (s.next i) (2*val + 1) else none private partial def decodeOctalLitAux (s : String) (i : String.Pos) (val : Nat) : Option Nat := if s.atEnd i then some val else let c := s.get i if '0' ≤ c && c ≤ '7' then decodeOctalLitAux s (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) (i : String.Pos) (val : Nat) : Option Nat := if s.atEnd i then some val else match decodeHexDigit s i with | some (d, i) => decodeHexLitAux s i (16*val + d) | none => none private partial def decodeDecimalLitAux (s : String) (i : String.Pos) (val : Nat) : Option Nat := if s.atEnd i then some val else let c := s.get i if '0' ≤ c && c ≤ '9' then decodeDecimalLitAux s (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 private 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 partial def decodeScientificLitVal? (s : String) : Option (Nat × Bool × Nat) := let len := s.length if len == 0 then none else let c := s.get 0 if c.isDigit then decode 0 0 else none where decodeAfterExp (i : String.Pos) (val : Nat) (e : Nat) (sign : Bool) (exp : Nat) : Option (Nat × Bool × Nat) := if s.atEnd i then if sign then some (val, sign, exp + e) else if exp >= e then some (val, sign, exp - e) else some (val, true, e - exp) else let c := s.get i if '0' ≤ c && c ≤ '9' then decodeAfterExp (s.next i) val e sign (10*exp + c.toNat - '0'.toNat) else none decodeExp (i : String.Pos) (val : Nat) (e : Nat) : Option (Nat × Bool × Nat) := let c := s.get i if c == '-' then decodeAfterExp (s.next i) val e true 0 else decodeAfterExp i val e false 0 decodeAfterDot (i : String.Pos) (val : Nat) (e : Nat) : Option (Nat × Bool × Nat) := if s.atEnd i then some (val, true, e) else let c := s.get i if '0' ≤ c && c ≤ '9' then decodeAfterDot (s.next i) (10*val + c.toNat - '0'.toNat) (e+1) else if c == 'e' || c == 'E' then decodeExp (s.next i) val e else none decode (i : String.Pos) (val : Nat) : Option (Nat × Bool × Nat) := if s.atEnd i then none else let c := s.get i if '0' ≤ c && c ≤ '9' then decode (s.next i) (10*val + c.toNat - '0'.toNat) else if c == '.' then decodeAfterDot (s.next i) val 0 else if c == 'e' || c == 'E' then decodeExp (s.next i) val 0 else none def isScientificLit? (stx : Syntax) : Option (Nat × Bool × Nat) := match isLit? scientificLitKind stx with | some val => decodeScientificLitVal? val | _ => none 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 def decodeQuotedChar (s : String) (i : String.Pos) : Option (Char × String.Pos) := do 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 let (d₁, i) ← decodeHexDigit s i let (d₂, i) ← decodeHexDigit s i pure (Char.ofNat (16*d₁ + d₂), i) else if c = 'u' then do let (d₁, i) ← decodeHexDigit s i let (d₂, i) ← decodeHexDigit s i let (d₃, i) ← decodeHexDigit s i let (d₄, i) ← decodeHexDigit s i pure (Char.ofNat (16*(16*(16*d₁ + d₂) + d₃) + d₄), i) else none partial def decodeStrLitAux (s : String) (i : String.Pos) (acc : String) : Option String := do let c := s.get i let i := s.next i if c == '\"' then pure acc else if s.atEnd i then none else if c == '\\' then do let (c, i) ← decodeQuotedChar s i decodeStrLitAux s i (acc.push c) else decodeStrLitAux s 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 let (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) (i : Nat) (r : Name) : Option Name := do let continue? (i : Nat) (r : Name) : Option Name := if s.get i == '.' then decodeNameLitAux s (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) (Name.mkStr r (s.extract startPart stopPart)) else if isIdFirst curr then let startPart := i let stopPart := s.nextWhile isIdRest startPart continue? stopPart (Name.mkStr 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 isAtom : Syntax → Bool | atom _ _ => true | _ => false def isToken (token : String) : Syntax → Bool | atom _ val => val.trim == token.trim | _ => 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 -- when elaborating partial syntax trees, it's reasonable to interpret missing parts as `none` | Syntax.missing => true | _ => 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 p) | stx => if p stx then some stx else none def find? (stx : Syntax) (p : Syntax → Bool) : Option Syntax := findAux p stx end Syntax /-- Reflect a runtime datum back to surface syntax (best-effort). -/ class Quote (α : Type) where quote : α → Syntax export Quote (quote) instance : Quote Syntax := ⟨id⟩ instance : Quote Bool := ⟨fun | true => mkCIdent `Bool.true | false => mkCIdent `Bool.false⟩ instance : Quote String := ⟨Syntax.mkStrLit⟩ instance : Quote Nat := ⟨fun n => Syntax.mkNumLit <| toString n⟩ instance : Quote Substring := ⟨fun s => Syntax.mkCApp `String.toSubstring #[quote s.toString]⟩ private def quoteName : Name → Syntax | Name.anonymous => mkCIdent ``Name.anonymous | Name.str n s _ => Syntax.mkCApp ``Name.mkStr #[quoteName n, quote s] | Name.num n i _ => Syntax.mkCApp ``Name.mkNum #[quoteName n, quote i] instance : Quote Name := ⟨quoteName⟩ instance {α β : Type} [Quote α] [Quote β] : Quote (α × β) where quote | ⟨a, b⟩ => Syntax.mkCApp ``Prod.mk #[quote a, quote b] private def quoteList {α : Type} [Quote α] : List α → Syntax | [] => mkCIdent ``List.nil | (x::xs) => Syntax.mkCApp ``List.cons #[quote x, quoteList xs] instance {α : Type} [Quote α] : Quote (List α) where quote := quoteList instance {α : Type} [Quote α] : Quote (Array α) where quote xs := Syntax.mkCApp ``List.toArray #[quote xs.toList] private def quoteOption {α : Type} [Quote α] : Option α → Syntax | none => mkIdent ``none | (some x) => Syntax.mkCApp ``some #[quote x] instance Option.hasQuote {α : Type} [Quote α] : Quote (Option α) where quote := quoteOption /- Evaluator for `prec` DSL -/ def evalPrec (stx : Syntax) : MacroM Nat := Macro.withIncRecDepth stx do let stx ← expandMacros stx match stx with | `(prec| $num:numLit) => return num.isNatLit?.getD 0 | _ => Macro.throwErrorAt stx "unexpected precedence" macro_rules | `(prec| $a + $b) => do `(prec| $(quote <| (← evalPrec a) + (← evalPrec b)):numLit) macro_rules | `(prec| $a - $b) => do `(prec| $(quote <| (← evalPrec a) - (← evalPrec b)):numLit) macro "evalPrec! " p:prec:max : term => return quote (← evalPrec p) def evalOptPrec : Option Syntax → MacroM Nat | some prec => evalPrec prec | none => return 0 /- Evaluator for `prio` DSL -/ def evalPrio (stx : Syntax) : MacroM Nat := Macro.withIncRecDepth stx do let stx ← expandMacros stx match stx with | `(prio| $num:numLit) => return num.isNatLit?.getD 0 | _ => Macro.throwErrorAt stx "unexpected priority" macro_rules | `(prio| $a + $b) => do `(prio| $(quote <| (← evalPrio a) + (← evalPrio b)):numLit) macro_rules | `(prio| $a - $b) => do `(prio| $(quote <| (← evalPrio a) - (← evalPrio b)):numLit) macro "evalPrio! " p:prio:max : term => return quote (← evalPrio p) def evalOptPrio : Option Syntax → MacroM Nat | some prio => evalPrio prio | none => return evalPrio! default 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) (i : Nat) (acc : Array Syntax) : m (Array Syntax) := do if h : i < a.size then let stx := a.get ⟨i, h⟩ if (← p stx) then if acc.isEmpty then filterSepElemsMAux a p (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 a p (i+2) ((acc.push sepStx).push stx) else filterSepElemsMAux a p (i+2) (acc.push stx) else filterSepElemsMAux a p (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) (i : Nat) (acc : Array Syntax) : m (Array Syntax) := do if h : i < a.size then let stx := a.get ⟨i, h⟩ if i % 2 == 0 then do let stx ← f stx mapSepElemsMAux a f (i+1) (acc.push stx) else mapSepElemsMAux a f (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 namespace Lean.Syntax.SepArray def getElems {sep} (sa : SepArray sep) : Array Syntax := sa.elemsAndSeps.getSepElems /- We use `CoeTail` here instead of `Coe` to avoid a "loop" when computing `CoeTC`. The "loop" is interrupted using the maximum instance size threshold, but it is a performance bottleneck. The loop occurs because the predicate `isNewAnswer` is too imprecise. -/ instance (sep) : CoeTail (SepArray sep) (Array Syntax) where coe := getElems end Lean.Syntax.SepArray /-- 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 := α /- Helper functions for manipulating interpolated strings -/ namespace Lean.Syntax private def decodeInterpStrQuotedChar (s : String) (i : String.Pos) : Option (Char × String.Pos) := match decodeQuotedChar s i with | some r => some r | none => let c := s.get i let i := s.next i if c == '{' then pure ('{', i) else none private partial def decodeInterpStrLit (s : String) : Option String := let rec loop (i : String.Pos) (acc : String) := let c := s.get i let i := s.next i if c == '\"' || c == '{' then pure acc else if s.atEnd i then none else if c == '\\' then do let (c, i) ← decodeInterpStrQuotedChar s i loop i (acc.push c) else loop i (acc.push c) loop 1 "" partial def isInterpolatedStrLit? (stx : Syntax) : Option String := match isLit? interpolatedStrLitKind stx with | none => none | some val => decodeInterpStrLit val def expandInterpolatedStrChunks (chunks : Array Syntax) (mkAppend : Syntax → Syntax → MacroM Syntax) (mkElem : Syntax → MacroM Syntax) : MacroM Syntax := do let mut i := 0 let mut result := Syntax.missing for elem in chunks do let elem ← match elem.isInterpolatedStrLit? with | none => mkElem elem | some str => mkElem (Syntax.mkStrLit str) if i == 0 then result := elem else result ← mkAppend result elem i := i+1 return result def expandInterpolatedStr (interpStr : Syntax) (type : Syntax) (toTypeFn : Syntax) : MacroM Syntax := do let ref := interpStr let r ← expandInterpolatedStrChunks interpStr.getArgs (fun a b => `($a ++ $b)) (fun a => `($toTypeFn $a)) `(($r : $type)) def getSepArgs (stx : Syntax) : Array Syntax := stx.getArgs.getSepElems end Syntax namespace Meta.Simp def defaultMaxSteps := 100000 structure Config where maxSteps : Nat := defaultMaxSteps contextual : Bool := false memoize : Bool := true singlePass : Bool := false zeta : Bool := true beta : Bool := true eta : Bool := true iota : Bool := true proj : Bool := true ctorEq : Bool := true decide : Bool := true deriving Inhabited, BEq, Repr end Meta.Simp end Lean
c9e90f5a63b7c1c930fb9c8e4c9b7397fcf3b74f
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/sorry_hash.lean
dd96e764127f5fea62749698661d736a0d0d2033
[ "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
176
lean
example : true := begin sorry #, -- broken end example : true := by { sorry # } -- broken example : true := begin { sorry # } end -- broken example : true := by trivial #che
53359f2627fbd1a7be1c85739d5e649adc3e6893
ae9f8bf05de0928a4374adc7d6b36af3411d3400
/src/formal_ml/filter_util.lean
d7206711bc96db26a9c88bafb7188529adc015f3
[ "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
38,905
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 order.filter.basic import topology.bases import data.real.nnreal import topology.instances.real import topology.instances.nnreal import topology.instances.ennreal import topology.algebra.infinite_sum import formal_ml.set import formal_ml.finset import formal_ml.nat import formal_ml.ennreal import formal_ml.nnreal import data.finset import order.complete_lattice import formal_ml.real open finset lemma filter_le_intro (α:Type*) (A B:filter α): (∀ a, a∈ B → a∈ A)→ (A≤ B) := begin intros, apply (@filter.le_def α A B).mpr, assumption, end lemma filter_le_intro2 (α:Type*) (A B:filter α): (B.sets ⊆ A.sets)→ (A≤ B) := begin intros, apply (@filter.le_def α A B).mpr, have A1:((B.sets ⊆ A.sets) ↔ (∀ a∈ B.sets, a∈ A.sets)), { refl, }, assumption, end lemma filter_le_elim {α:Type*} {A B:filter α} {a:set α}: (A≤ B) → (a∈ B)→ (a∈ A) := begin intros, apply (@filter.le_def α A B).mp;assumption, end /- filter.map f A is the pre-image of the pre-image of the sets in A. --set.preimage f has type (set β)→ (set α) --set.preimage f B = {x:f x ∈ B} --set.preimage (set.preimage f) has type (set (set α)) → (set (set β)) --set.preimage (set.preimage f) AF = {y:(set.preimage f) y ∈ AF} --set.preimage (set.preimage f) AF = {y:{x:f x∈ y} ∈ AF} -/ lemma filter_map_def (α β:Type*) (A:filter α) (f:α → β): (filter.map f A).sets = set.preimage (set.preimage f) A.sets := begin refl, end lemma filter_mem_def (α:Type*) (A:filter α) (a:set α): (a∈ A) = (a ∈ A.sets) := begin refl, end lemma filter_map_intro (α β:Type*) (A:filter α) (f:α → β) (X:set α) (Y:set β): (X∈ A) → (X=set.preimage f Y)→ (Y∈ filter.map f A) := begin intros, rw filter_mem_def, rw filter_map_def, unfold set.preimage, simp, rw filter_mem_def at a, unfold set.preimage at a_1, rw ← a_1, assumption, end lemma filter_map_elim (α β:Type*) (A:filter α) (f:α → β) (Y:set β): (Y∈ filter.map f A)→ (∃ (X∈ A), (X=set.preimage f Y)) := begin intros, rw filter_mem_def at a, rw filter_map_def at a, --unfold set.preimage, simp, unfold set.preimage at a, simp at a, apply a, end lemma filter_tendsto_intro (α β:Type*) (f:α → β) (A:filter α) (B:filter β): (∀ b∈ B, set.preimage f b ∈ A) → (@filter.tendsto α β f A B) := begin intros, unfold filter.tendsto, apply filter_le_intro, intros, apply filter_map_intro, have A1:a_1 ∈ B → f ⁻¹' a_1 ∈ A, { apply a, }, have A2:f ⁻¹' a_1 ∈ A, { apply A1, apply a_2, }, apply A2, refl, end lemma filter_tendsto_elim {α β:Type*} {f:α → β} {A:filter α} {B:filter β} {s:set β}: (@filter.tendsto α β f A B) →(s∈ B)→ (set.preimage f s ∈ A) := begin intros, unfold filter.tendsto at a, have A1:s∈ filter.map f A, { apply filter_le_elim, apply a, apply a_1, }, have A2:(∃ (X∈ A), (X=set.preimage f s)), { apply (filter_map_elim), apply A1, }, cases A2, cases A2_h, subst A2_w, exact A2_h_w, end lemma filter_principal_intro {α:Type*} (a:finset α) (b:set (finset α)): ({c : finset α | a ⊆ c} ⊆ b) → b ∈ (filter.principal {c:finset α | a⊆ c}):= begin --apply b∈ (filter.principal {c:finset ℕ | a⊆ c}), intros, unfold filter.principal, have A1:b ∈ {t : set (finset α) | {c : finset α | a ⊆ c} ⊆ t}, { simp, apply a_1, }, apply A1, end lemma filter_principal_elim {α:Type*} (a:finset α) (b:set (finset α)): b ∈ (filter.principal {c:finset α | a⊆ c}) → ({c : finset α | a ⊆ c} ⊆ b) := begin intros, apply (@filter.mem_principal_sets (finset α) b {c:finset α | a⊆ c}).mp, apply a_1, end lemma filter_principal_intro2 {α:Type*} (a:finset α) (b:set (finset α)): (∀ c:finset α, a⊆ c → c∈ b) → b ∈ (filter.principal {c:finset α | a⊆ c}):= begin intros, apply filter_principal_intro, rw set.subset_def, intros, simp at a_2, apply a_1, apply a_2, end lemma filter_principal_intro3 {α:Type*} (a b:set α): (a⊆ b) → b ∈ filter.principal a:= begin unfold filter.principal, simp, end lemma filter_principal_elim2 {α:Type*} (a:finset α) (b:set (finset α)) (c:finset α): b ∈ (filter.principal {c:finset α | a⊆ c}) → (a ⊆ c) → (c∈ b) := begin intros, apply (@filter_principal_elim α a b), { apply a_1, }, apply a_2, end lemma filter_principal_elim3 {α:Type*} (a b:set α): b ∈ filter.principal a → (a⊆ b):= begin unfold filter.principal, simp, end /- Let's walk through a simple example. b={1,2} c={2,3} filter.principal {{1,2},{1,2,3},{1,2,3,4}, {1,2,4}} ⊓ filter.principal {{1,3},{1,2,3},{1,2,3,4}, {1,3,4}} = filter.principal {{1,2,3}, {1,2,3,4}} -/ lemma principal_inf (α:Type*) (A B:set α): (filter.principal A ⊓ filter.principal B = filter.principal (A∩B)) := begin simp, end lemma principal_inf_sets2 {α:Type*} [semilattice_sup_bot α] (b c:α): filter.principal {d:α |b ≤ d} ⊓ filter.principal {d: α |c ≤ d} = filter.principal {d:α |(b ⊔ c) ≤ d} := begin have A1:{d:α |b ≤ d} ∩ {d:α |c ≤ d} = {d: α |(b ⊔ c) ≤ d}, { ext, split;intros, { cases a, simp at a_left, simp at a_right, simp, split;assumption, }, { simp at a, cases a with A2 A3, split;simp;assumption, } }, rw ← A1, apply principal_inf, end -- Use apply principal_inf_sets2, lemma principal_inf_sets {α:Type*} [decidable_eq α] (b c:finset α): filter.principal {d:finset α |b ≤ d} ⊓ filter.principal {d:finset α |c ≤ d} = filter.principal {d:finset α |(b ∪ c) ≤ d} := begin apply principal_inf_sets2, end -- src/data/set/lattice.lean lemma set_Inf_def (α:Type*) (S:set (set α)): (Inf S) = {a | ∀ t ∈ S, a ∈ t } := begin refl, end lemma set_Inf_intro (α:Type*) (S:set (set α)) (x:α): (∀ X∈ S, x∈ X) → x∈ Inf S := begin intros, rw set_Inf_def, simp, exact a, end lemma set_Inf_range_intro (α β:Type*) (f:α → set (β)) (x:β): (∀ y:α, x∈ f y) → x∈ Inf (set.range (λ (a : α), f a)) := begin intros, apply set_Inf_intro, intros, unfold set.range at H, simp at H, cases H, rw ← H_h, apply a, end lemma glb_intro {α β:Type*} (f:α → set (β)) (x:β): (∀ a:α, x∈ f a) → x ∈ ⨅ a, f a := begin intros, unfold infi, unfold Inf, apply set_Inf_range_intro, assumption, end lemma le_Inf_simp : ∀ (α:Type*) (s : set (filter α)) (a : filter α), (∀ (b : filter α ), b ∈ s → a ≤ b) → a ≤ complete_lattice.Inf s := begin intros, apply (complete_lattice.le_Inf s), apply a_1, end lemma Inf_le_simp : ∀ (α : Type*) (s : set (filter α)) (a : filter α), a ∈ s → complete_lattice.Inf s ≤ a := begin intros, apply (complete_lattice.Inf_le s), assumption, end lemma mem_Inf_intro {α : Type*} {s : set (filter α)} {a : filter α} {t:set α}: t ∈ a → a ∈ s → t∈ complete_lattice.Inf s := begin intros, apply (complete_lattice.Inf_le s), assumption, assumption end -- This is equivalent to filter.mem_infi (or a mem_Inf variant of mem_infi) def inf_filter_sets_def (α:Type*) (S : set (filter α)):set (set α) := {s:set α | ∃ f∈ S, s∈ f} lemma inf_filter_sets_of_superset2 (α:Type*) (S : set (filter α)) (s t:set α): s∈ (inf_filter_sets_def α S) → s⊆ t → t∈ (inf_filter_sets_def α S) := begin intro, unfold inf_filter_sets_def, simp, unfold inf_filter_sets_def at a, simp at a, cases a, cases a_h, intros, apply exists.intro a_w, split, { assumption, }, { apply filter.sets_of_superset, apply a_h_right, assumption, } end --This suggests the definition is overly simplistic. def inf_filter_sets_inter_sets2 (α:Type*) (S : set (filter α)) (H:∀ a b:filter α, a∈ S→ b∈ S→ a⊓b∈ S) (s t:set α): s∈ (inf_filter_sets_def α S) → t∈ (inf_filter_sets_def α S) → s ∩ t ∈ (inf_filter_sets_def α S) := begin unfold inf_filter_sets_def, simp, intros, apply (exists.intro (x⊓x_1)), split, { apply H;assumption, }, { rw ← filter_mem_def, have B1:(s ∩ t) ∈ x ⊓ x_1 ↔ ∃t₁∈x, ∃t₂∈x_1, t₁ ∩ t₂ ⊆ (s ∩ t), apply filter.mem_inf_sets, apply B1.mpr, apply exists.intro s, apply exists.intro a_1, apply exists.intro t, apply exists.intro a_3, refl, } end lemma inf_filter_univ_sets2 (α:Type*) (S : set (filter α)) (b:filter α): (b∈ S) → set.univ ∈ (inf_filter_sets_def α S) := begin unfold inf_filter_sets_def, simp, intros, apply exists.intro b, split, assumption, apply filter.univ_sets, end def inf_filter_def2 (α:Type*) (S : set (filter α)) (b:filter α) (H:b∈ S) (H2:∀ a b:filter α, a∈ S→ b∈ S→ a⊓b∈ S):filter α := { sets := inf_filter_sets_def α S, sets_of_superset := inf_filter_sets_of_superset2 α S, univ_sets := (inf_filter_univ_sets2 α S b H), inter_sets := inf_filter_sets_inter_sets2 α S H2, } /- This definition is a little weak, as it assumes S is nonempty and closed under (binary) infimum. However, given these two constraints, the result is more elegant, and it corresponds to the generic result about filters, filter.eq_Inf_of_mem_sets_iff_exists_mem. -/ lemma Inf_filter_def (α:Type*) (S : set (filter α)) (b:filter α) (H:b∈ S) (H2:∀ a b:filter α, a∈ S→ b∈ S→ a⊓b∈ S) (s:set α): (s ∈ Inf S)↔ (∃ t∈ S, s∈ t) := begin have A1:(inf_filter_def2 α S b H H2) = Inf S, { apply filter.eq_Inf_of_mem_sets_iff_exists_mem, intros, unfold inf_filter_def2, simp, unfold inf_filter_sets_def, simp, }, rw ← A1, unfold inf_filter_def2, simp, unfold inf_filter_sets_def, simp, end lemma eq_Inf_of_mem_sets_iff_exists_mem {α:Type} {S : set (filter α)} {l : filter α} (h : ∀ {s}, s ∈ l ↔ ∃ f ∈ S, s ∈ f) : l = Inf S := le_antisymm (le_Inf $ λ f hf s hs, h.2 ⟨f, hf, hs⟩) (λ s hs, let ⟨f, hf, hs⟩ := h.1 hs in (Inf_le hf : Inf S ≤ f) hs) --def filter_from_element (α:Type) (a:α):filter α := filter.principal (λ a', a=a') --filter.principal s contains all the supersets of s. --if s is in F, then F also contains lemma le_principal (F:filter (finset ℕ)) (s:set (finset ℕ)): (s∈ F)→ (F ≤ filter.principal s) := begin intros, apply (filter_le_intro2 (finset ℕ) F (filter.principal s)), rw set.subset_def, intros, unfold filter.principal at a_1, simp at a_1, apply filter.sets_of_superset, { apply a, }, { assumption, } end /- What is a neighborhood of x? First, for every open set containing x, consider the principal filter (set of all supersets). Now, let's consider the infimum of all those settings, keeping in mind that for a set, the inf (filter.principal S) (filter.principal T)=(filter.principal S∪T) (at this point, we should be able to write a proof for this). Since the union of two open sets containing X itself is an open set containing X, and every element has at least one neighborhood that contains it, we are OK. Thus, a neighborhood of x is a superset of an open set containing x. --a more standard definition of neighborhood. lemma mem_nhds_def (x:nnreal) (b:set nnreal): (b∈ nhds x)↔ (∃ u:set nnreal, (u⊆ b) ∧ is_open u ∧ x∈ u) := -/ lemma mem_nhds_elim_real_rat (b:set real) (x:real): b∈ nhds x → (∃ p q:ℚ, (p < q) ∧ ((set.Ioo p q:set ℝ) ⊆ b) ∧ ((p:ℝ) < x) ∧ (x < (q:ℝ))) := begin let rat_basis := (⋃ (a b : ℚ) (h : a < b), {set.Ioo ↑a ↑b}), begin intros, have A1:topological_space.is_topological_basis rat_basis, { apply real.is_topological_basis_Ioo_rat, }, have A2: (b ∈ nhds x ↔ ∃ (t : set real) (H : t ∈ rat_basis), x ∈ t ∧ t ⊆ b), { apply @topological_space.mem_nhds_of_is_topological_basis, apply A1, }, have A3: (∃ (t : set real) (H : t ∈ rat_basis), x ∈ t ∧ t ⊆ b), { apply A2.mp, apply a, }, /- Now that we have proven A3, we are basically done. We just rewrite A3 into the statement of the theorem. -/ cases A3, cases A3_h, simp at A3_h_w, cases A3_h_w, cases A3_h_w_h, cases A3_h_w_h_h, subst A3_w, cases A3_h_h, apply exists.intro A3_h_w_w, apply exists.intro A3_h_w_h_w, unfold set.Ioo at A3_h_h_left, simp at A3_h_h_left, cases A3_h_h_left, split, { assumption, }, split, { assumption, }, split, { assumption, }, { assumption, }, end end ------ Move theorems here to where they belong ----------------------------------- lemma induced_topological_basis {α β:Type*} [Tα:topological_space α] [Tβ:topological_space β] {f:α → β} {S:set (set β)}:(inducing f) → (topological_space.is_topological_basis S) → (topological_space.is_topological_basis (set.image (set.preimage f) S)) := begin intros A1 A2, rw A1.induced, unfold topological_space.is_topological_basis, split, { intros t₁ A3 t₂ A4 x A5, cases A3 with u₁ A3, cases A4 with u₂ A4, cases A5 with A6 A7, cases A3 with A8 A9, cases A4 with A10 A11, subst t₁, subst t₂, simp at A6, simp at A7, have A12:f x ∈ u₁ ∩ u₂, { simp, apply and.intro A6 A7, }, unfold topological_space.is_topological_basis at A2, have A13 := A2.left u₁ A8 u₂ A10 (f x) A12, cases A13 with u₃ A13, cases A13 with A14 A15, cases A15 with A16 A17, apply exists.intro (f ⁻¹' u₃), have A18:f ⁻¹' u₃ ∈ set.preimage f '' S, { simp, apply exists.intro u₃, split, apply A14, refl, }, apply exists.intro A18, split, apply A16, { rw set.subset_inter_iff at A17, apply set.subset_inter;apply set.preimage_mono, apply A17.left, apply A17.right, }, }, split, { ext a,split;intro A3;simp, unfold topological_space.is_topological_basis at A2, have A4:f a ∈ ⋃₀ S, { rw A2.right.left, simp, }, cases A4 with X A5, cases A5 with A6 A7, apply exists.intro X, apply and.intro A6 A7, }, { unfold topological_space.is_topological_basis at A2, have A3 := A2.right.right, rw A3, apply induced_generate_from_eq, } end lemma nnreal_topological_space_def: nnreal.topological_space = @topological_space.induced nnreal real (@coe nnreal real _) (@uniform_space.to_topological_space real _) := rfl lemma inducing_nnreal_topological_space: inducing (@coe nnreal real _) := { induced := nnreal_topological_space_def, } lemma nnreal_nhds {x:nnreal}: nhds x = filter.comap (@coe nnreal real _) (nhds x.val) := begin apply inducing.nhds_eq_comap, apply inducing_nnreal_topological_space, end lemma nnreal_nhds2 {x:nnreal} {B:set nnreal}: B ∈ nhds x ↔ (∃ C∈ nhds (x.val),set.preimage (@coe nnreal real _) C⊆ B) := begin rw nnreal_nhds, apply filter.mem_comap_sets, end lemma coe_nnreal_real_val:(@coe nnreal real _) = subtype.val := rfl lemma mem_nhds_elim_real {b:set real} {x:real}: b∈ nhds x → (∃ p q:ℝ, (p < q) ∧ ((set.Ioo p q) ⊆ b) ∧ (p < x) ∧ (x < q)) := begin intros, have A1:(∃ prat qrat:ℚ, (prat < qrat) ∧ ((set.Ioo prat qrat:set ℝ) ⊆ b) ∧ ((prat:ℝ) < x) ∧ (x < (qrat:ℝ))), { apply mem_nhds_elim_real_rat, apply a, }, cases A1, cases A1_h, cases A1_h_h, cases A1_h_h_right, cases A1_h_h_right_right, apply exists.intro (A1_w:ℝ), apply exists.intro (A1_h_w:ℝ), split, { apply (@rat.cast_lt real _ A1_w A1_h_w).mpr, apply A1_h_h_left, }, { split, { assumption, }, split, { apply A1_h_h_right_right_left, }, { apply A1_h_h_right_right_right, } }, end lemma mem_nhds_elim_nnreal {b:set nnreal} {x:nnreal}: b∈ nhds x → (∃ p q:ℝ, (p < q) ∧ (set.preimage (@coe nnreal real _) (set.Ioo p q) ⊆ b) ∧ (p < ↑x) ∧ (↑x < q)) := begin intro A1, rw nnreal_nhds2 at A1, cases A1 with C A1, cases A1 with A2 A3, have A4 := mem_nhds_elim_real A2, cases A4 with p A4, cases A4 with q A4, cases A4 with A5 A6, cases A6 with A7 A8, --cases A8 with A9 A10, apply exists.intro p, apply exists.intro q, split, apply A5, split, apply set.subset.trans, { apply set.preimage_mono, apply A7, }, { apply A3, }, apply A8, end lemma preimage_coe_Ioo {p q:ℝ}:(p < 0) → (0 ≤ q) → set.preimage (@coe nnreal real _) (set.Ioo p q) = set.Iio (nnreal.of_real q) := begin intro A1, intro A2, unfold set.Ioo set.Iio, ext,split;intro A3;simp at A3;simp, { cases A3 with A4 A5, rw ← nnreal.coe_lt_coe, rw nnreal.coe_of_real, apply A5, apply A2, }, { split, { apply lt_of_lt_of_le, apply A1, apply x.property, }, { rw ← nnreal.coe_of_real q, rw nnreal.coe_lt_coe, apply A3, apply A2, }, }, end lemma preimage_coe_Ioo2 {p q:ℝ}:(0 ≤ p) → (p < q) → set.preimage (@coe nnreal real _) (set.Ioo p q) = set.Ioo (nnreal.of_real p) (nnreal.of_real q) := begin intro A1, intro A2, have B1:0 ≤ q, { apply le_trans, apply A1, apply le_of_lt, apply A2, }, unfold set.Ioo, ext,split;intro A3;simp at A3;simp, { cases A3 with A4 A5, split;rw ← nnreal.coe_lt_coe;rw nnreal.coe_of_real, apply A4, apply A1, apply A5, apply B1, }, { cases A3 with A4 A5, split, { rw ← nnreal.coe_of_real p, rw nnreal.coe_lt_coe, apply A4, apply A1, }, { rw ← nnreal.coe_of_real q, rw nnreal.coe_lt_coe, apply A5, apply B1, }, }, end lemma mem_nhds_elim_nnreal2 {b:set nnreal} {x:nnreal}:x ≠ 0 → b∈ nhds x → (∃ p q:nnreal, (p < q) ∧ ((set.Ioo p q) ⊆ b) ∧ (p < x) ∧ (x < q)) := begin intros A1 A2, have A3 := mem_nhds_elim_nnreal A2, cases A3 with p A3, cases A3 with q A3, cases A3 with A4 A5, cases A5 with A6 A7, cases A7 with A8 A9, have A10:p < 0 ∨ 0 ≤ p := lt_or_le p 0, have A11:0 < x := bot_lt_iff_ne_bot.mpr A1, have A12:(@coe nnreal real _ 0) = (0:real) := rfl, have A13:x.val = (@coe nnreal real _ x) := rfl, have A14:0 < x.val, { rw A13, rw ← A12, rw nnreal.coe_lt_coe, apply A11, }, have A15:0 ≤ q, { apply le_trans, apply x.property, apply le_of_lt, apply A9, }, have A16:x < nnreal.of_real q, { rw ← nnreal.coe_lt_coe, rw nnreal.coe_of_real, apply A9, apply A15, }, cases A10, { apply exists.intro (0:nnreal), apply exists.intro (nnreal.of_real q), split, { rw ← nnreal.coe_lt_coe, rw A12, apply lt_trans, apply A14, rw A13, apply lt_of_lt_of_le, apply A9, rw nnreal.coe_of_real, apply A15, }, split, { have B2:set.Ioo 0 (nnreal.of_real q) ⊆ (set.preimage (@coe nnreal real _) (set.Ioo p q)), { rw preimage_coe_Ioo, rw set.subset_def, intros y B2A, unfold set.Iio, unfold set.Ioo at B2A, simp at B2A, simp, apply B2A.right, apply A10, apply A15, }, apply set.subset.trans B2 A6, }, split, { apply A11, }, { apply A16, }, }, { apply exists.intro (nnreal.of_real p), apply exists.intro (nnreal.of_real q), have B1:nnreal.of_real p < x, { rw ← nnreal.coe_lt_coe, rw nnreal.coe_of_real, apply A8, apply A10, }, split, { apply lt_trans B1 A16, }, split, { rw ← preimage_coe_Ioo2, apply A6, apply A10, apply A4, }, split, { apply B1, }, { apply A16, }, }, end lemma mem_nhds_elim_real_bound (b:set real) (x:real): b∈ nhds x → (∃ r>0, (set.Ioo (x-r) (x+r)) ⊆ b) := begin intros, have A1:(∃ p q:ℝ, (p < q) ∧ ((set.Ioo p q) ⊆ b) ∧ (p < x) ∧ (x < q)), { apply mem_nhds_elim_real, apply a, }, cases A1, cases A1_h, cases A1_h_h, cases A1_h_h_right, cases A1_h_h_right_right, let r := min (x - A1_w) (A1_h_w -x), begin apply exists.intro r, have A2:r>0, { apply lt_min, { apply (@lt_sub real _ 0 x A1_w).mpr, simp, assumption, }, { apply (@lt_sub real _ 0 A1_h_w x).mpr, simp, assumption, } }, apply exists.intro A2, have A3:set.Ioo (x-r) (x+r) ⊆ set.Ioo A1_w A1_h_w, { unfold set.Ioo, simp, intros, split, { apply lt_of_le_of_lt, show x - r < a_1, { assumption, }, { have A4:r <= x - A1_w, apply min_le_left, apply (@le_sub real _ r x A1_w).mp, assumption, } }, { apply lt_of_lt_of_le, { apply a_3, }, { -- prove x + r ≤ A1_h_w have A6:r <= A1_h_w - x, apply min_le_right, --rw A7, apply (@le_neg_add_iff_add_le real _ x r A1_h_w).mp, have A7:((-x)+A1_h_w=A1_h_w-x), { apply real.decidable_linear_ordered_comm_ring.add_comm, }, rw A7, assumption, } }, }, apply set.subset.trans, apply A3, assumption, end end lemma nnreal_sub_le_sub_of_le2 {a b c:nnreal}:a ≤ b → a - c ≤ b - c := begin intro A1, have A2:(c ≤ a) ∨ (a ≤ c) := le_total c a, cases A2, { rw ← add_le_add_iff_right c, rw nnreal.sub_add_cancel_of_le A2, rw nnreal.sub_add_cancel_of_le (le_trans A2 A1), apply A1, }, { rw nnreal.sub_eq_zero A2, apply bot_le, }, end lemma nnreal_le_sub_of_le_sub_of_le {p x r:nnreal}:p ≤ x → r ≤ x - p → p ≤ x - r := begin intros A1 A2, rw ← add_le_add_iff_right p at A2, rw nnreal.sub_add_cancel_of_le A1 at A2, have A3:(r + p) - r ≤ x - r := nnreal_sub_le_sub_of_le2 A2, rw add_comm r p at A3, rw nnreal.add_sub_cancel at A3, apply A3, end --TODO: remove dependence on x≠0 lemma mem_nhds_elim_nnreal_bound (b:set nnreal) (x:nnreal):x ≠ 0 → b∈ nhds x → (∃ r>0, (set.Ioo (x-r) (x+r)) ⊆ b) := begin intros AX a, have A1:(∃ p q:nnreal, (p < q) ∧ ((set.Ioo p q) ⊆ b) ∧ (p < x) ∧ (x < q)), { apply mem_nhds_elim_nnreal2, apply AX, apply a, }, cases A1 with p A1, cases A1 with q A1, cases A1 with B1 B2, cases B2 with B2 B3, cases B3 with B3 B4, let r := min (x - p) (q -x), begin apply exists.intro r, have A2:r>0, { apply lt_min;rw nnreal.sub_pos, { apply B3, }, { apply B4, } }, apply exists.intro A2, have A3:set.Ioo (x-r) (x+r) ⊆ set.Ioo p q, { apply set.Ioo_subset_Ioo, { have A4:r <= x - p := min_le_left (x-p) (q - x), apply nnreal_le_sub_of_le_sub_of_le (le_of_lt B3) A4, }, { have A4:r <= q - x := min_le_right (x-p) (q - x), rw ← add_le_add_iff_left x at A4, apply le_trans A4, rw add_comm x, rw nnreal.sub_add_cancel_of_le (le_of_lt B4), }, }, apply set.subset.trans, apply A3, assumption, end end --This is filter.at_top_def lemma filter_at_top_def3 {α:Type*} [preorder α]: filter.at_top = ⨅ a:α, filter.principal {b | a ≤ b} := rfl lemma filter_at_top_def (α:Type*): (@filter.at_top (finset α) _)= ⨅ (a:(finset α)), filter.principal {b:finset α | a⊆ b} := rfl --This is (roughly) filter.at_top_mem_sets lemma mem_filter_at_top_def2 {α:Type*} [SL:semilattice_sup_bot α] {S:set α}: (S∈ @filter.at_top α _)↔ (∃ a:α, {b:α|a ≤ b}⊆ S) := begin have B1:order_bot α := semilattice_sup_bot.to_order_bot α, have B2:has_bot α := order_bot.to_has_bot α, rw filter_at_top_def3, unfold infi, have A1:(∀ X Y:filter α, X ∈ (set.range (λ (a :α), filter.principal {b : α | a ≤ b})) → Y ∈ (set.range (λ (a : α), filter.principal {b : α | a ≤ b})) → X ⊓ Y ∈ (set.range (λ (a : α), filter.principal {b : α | a ≤ b}))), { intros, simp at a, cases a, subst X, simp at a_1, cases a_1, subst Y, --simp, apply exists.intro (a_w ⊔ a_1_w), symmetry, apply principal_inf_sets2, }, have A2:filter.principal {b : α | ⊥ ≤ b} ∈ (set.range (λ (a : α), filter.principal {b : α | a ≤ b})), { rw set.mem_range, apply exists.intro ⊥, refl, }, have A3:(S ∈ Inf (set.range (λ (a : α), filter.principal {b : α | a ≤ b})))↔ (∃ t∈ (set.range (λ (a : α), filter.principal {b : α | a ≤ b})), S∈ t), { apply (@Inf_filter_def (α) (set.range (λ (a : α), filter.principal {b : α | a ≤ b})) (filter.principal {b : α | ⊥ ≤ b}) A2 A1 ), }, apply iff.trans, apply A3, split;intros, { cases a, cases a_h, simp at a_h_w, cases a_h_w, apply exists.intro a_h_w_w, simp, subst a_w, simp at a_h_h, exact a_h_h, }, { cases a, apply exists.intro (filter.principal {b : α | a_w ≤ b}), have A4:filter.principal {b : α | a_w ≤ b} ∈ set.range (λ (a : α), filter.principal {b : α | a ≤ b}), { simp, }, apply exists.intro A4, apply a_h, } end lemma filter_at_top_def2 (α:Type*) [decidable_eq α] (S:set (finset α)): (S∈ @filter.at_top (finset α) _)↔ (∃ a:finset α, {b:finset α|a ≤ b}⊆ S) := begin apply mem_filter_at_top_def2, end --See alternatives below. lemma filter_at_top_intro {α:Type*} (b:set (finset α)) (c:finset α): (b∈ (filter.principal {d:finset α |c ≤ d} )) → (b ∈ (@filter.at_top (finset α) _)) := begin intros, rw filter_at_top_def, unfold infi, apply Inf_le_simp, { have A1:(filter.principal {d:finset α |c ≤ d} ) ∈ set.range (λ (a : finset α), filter.principal {b : finset α | a ⊆ b}), { unfold set.range, simp, }, apply A1, }, { assumption, } end lemma filter_at_top_elim {α:Type*} [decidable_eq α] {S:set (finset α)} : (S ∈ (@filter.at_top (finset α) _))→ (∃ a:(finset α), {b:finset α|a ≤ b}⊆ S) := begin apply (filter_at_top_def2 α S).mp, end lemma filter_at_top_intro2 {α:Type*} (b:set (finset α)) (c:finset α): ({d:finset α |c ≤ d} ⊆ b) → (b ∈ (@filter.at_top (finset α) _)) := begin intros, have A1:b∈ filter.principal {d:finset α |c ≤ d}, { unfold filter.principal, simp, apply a, }, apply filter_at_top_intro, apply A1, end lemma filter_contains_preimage_superset (α β:Type) (B:filter β) (S T:set α) (f:β → α): (S⊆ T)→ ({x:β |f x ∈ S}∈ B) → ({x:β |f x ∈ T}∈ B) := begin intros, have A1:{x:β |f x ∈ S} ⊆ {x:β |f x ∈ T}, { apply set.preimage_mono, assumption, }, apply B.sets_of_superset, apply a_1, assumption, end lemma filter_at_top_intro3 {α β:Type*} (c:finset α) (S:set β) (f:finset α → β): (∀ d ≥ c, f d ∈ S) → ({x:finset α|f x∈ S} ∈ (@filter.at_top (finset α) _)) := begin intros, have A1:({d:finset α |c ≤ d} ⊆ {x:finset α|f x∈ S}), { rw set.subset_def, intros, simp, simp at a_1, apply a, apply a_1, }, apply filter_at_top_intro2, apply A1, end lemma mem_filter_at_top_intro {α:Type*} [P:semilattice_sup_bot α] {S:set α} {x:α}: {y|x≤ y}⊆ S → S∈ (@filter.at_top α _) := begin intro A1, rw mem_filter_at_top_def2, apply exists.intro x, apply A1, end lemma mem_filter_at_top_elim {α:Type*} [P:semilattice_sup_bot α] {S:set α} : (S ∈ (@filter.at_top α _))→ (∃ a:α, {b:α|a ≤ b}⊆ S) := begin apply (mem_filter_at_top_def2).mp, end lemma in_own_Ioo {x ε:ℝ}:(0 < ε)→ x ∈ set.Ioo (x -ε) (x + ε) := begin apply x_in_Ioo, end /- Unused. -/ lemma mem_nhds_elim_helper (b:set nnreal) (x:nnreal) (s:set nnreal) (H2 : s ∈ {s : set nnreal | x ∈ s ∧ is_open s}): (set.range (λ (H : s ∈ {s : set nnreal | x ∈ s ∧ is_open s}), filter.principal s)) = {x:filter nnreal| x=filter.principal s} := begin unfold set.range, simp, ext, split, { simp, intros, symmetry, exact a_2, }, { simp, intros, split, { apply H2, }, { symmetry, exact a, } } end lemma lim_Inf_filter_empty (α:Type*): (@Inf (filter α) _ ∅) = ⊤ := begin simp, end lemma set_in_lattice_infih (α:Type*) (s:set α) (S:set (set α)) (H2 : s ∉ S): (set.range (λ (H : s ∈ S), filter.principal s)) = ∅ := begin unfold set.range, simp, rw set.eq_empty_iff_forall_not_mem, intros F, simp, intros B1 B2, apply H2, apply B1 end lemma set_in_lattice_infih2 (α:Type*) (s:set α) (S:set (set α)) (H2 : s ∈ S): (set.range (λ (H : s ∈ S), filter.principal s)) = {filter.principal s} := begin unfold set.range, simp, ext, split, { simp, intros, symmetry, exact a_1, }, { simp, intros, split, { apply H2, }, { symmetry, exact a, } } end lemma lower_bounds_top (α:Type*) (S:set (filter α)):lower_bounds S = lower_bounds (S ∪ {⊤}) := begin ext, unfold lower_bounds, split;intros;simp;simp at a;intros, { cases a_2, { rw a_2, simp, }, { apply a, apply a_2, } }, { apply a, right, apply a_2, } end lemma Inf_union_top (α:Type*) (S:set (filter α)): Inf (S∪ {⊤}) = Inf S := begin have A2:is_glb S (Inf S), { apply is_glb_Inf, }, have A3:is_glb (S∪ {⊤}) (Inf S), { cases A2, split, { rw ← lower_bounds_top, exact A2_left, }, { rw ← lower_bounds_top, exact A2_right, } }, apply is_glb.Inf_eq A3, end /- If we unfold infi in nhds, we get a doubly-nested Infimum that is hard to work with. This rewrites it more simply. -/ lemma set_in_lattice_infi (α:Type*) (S:set (set α)): (⨅ (s∈ S), (filter.principal s)) = Inf (set.image (filter.principal) S) := begin unfold infi, rw ← (@set.image_union_image_compl_eq_range (set α) _ S), have A1:(λ (s : set α), Inf (set.range (λ (H : s ∈ S), filter.principal s))) '' (S)ᶜ = (λ (s : set α), ⊤) '' Sᶜ, { rw set.image_congr, intros, have A1AA:set.range (λ (H : a ∈ S), filter.principal a) = ∅, { apply set_in_lattice_infih, apply H, }, rw A1AA, rw lim_Inf_filter_empty, }, rw A1, have A2:(λ (s : set α), Inf (set.range (λ (H : s ∈ S), filter.principal s))) '' (S) = (λ (s : set α), filter.principal s) '' S, { rw set.image_congr, intros, have A2A:set.range (λ (H : a ∈ S), filter.principal a) = {filter.principal a}, { apply set_in_lattice_infih2, apply H, }, rw A2A, simp, }, rw A2, have A4:(Sᶜ = ∅)∨ set.nonempty Sᶜ, { apply set.eq_empty_or_nonempty, }, cases A4, { rw A4, simp, }, { rw set.nonempty_def at A4, cases A4, have A4A:∀ k:filter α,(λ (s : set α), k) '' Sᶜ = {k}, { intros, ext, split;intros, { cases a, cases a_h, simp at a_h_right, rw a_h_right, apply (set.mem_singleton), }, { split, { split, apply A4_h, simp, simp at a, rw a, } } }, have A4B:(λ (s : set α), ⊤) '' Sᶜ = {⊤}, { apply (A4A ⊤), }, rw A4B, apply Inf_union_top, } end lemma open_nhds_inter (α:Type*) [topological_space α] (x:α) (A B:set α): (A∈ {s:set α|x∈ s∧ is_open s}) → (B∈ {s:set α|x∈ s∧ is_open s}) → ((A∩ B) ∈ {s:set α|x∈ s∧ is_open s}) := begin intros, cases a, cases a_1, split, { split;assumption, }, { apply is_open_inter;assumption, }, end /- lemma Inf_filter_def (α:Type) (S : set (filter α)) (b:filter α) (H:b∈ S) (H2:∀ a b:filter α, a∈ S→ b∈ S→ a⊓b∈ S) (s:set α): (s ∈ Inf S)↔ (∃ t∈ S, s∈ t) := -/ lemma nhds_def1 (α:Type*) [topological_space α] (x:α): nhds x = Inf (set.image (filter.principal) {S:set α|x∈ S ∧ is_open S}) := begin unfold nhds, unfold infi, apply set_in_lattice_infi, end /- This holds for an arbitrary topology. See @mem_nhds_sets_iff -/ lemma mem_nhds_intro_real2 {b U:set real} {x:real}: x∈ U → is_open U → U ⊆ b → b∈ nhds x := begin intros A1 A2 A3, rw nhds_def1, have A4:b∈ filter.principal U, { apply filter_principal_intro3, apply A3, }, apply mem_Inf_intro A4, { simp, split;assumption, }, end /- This holds for a (closed) order topology (I think) -/ lemma mem_nhds_intro_real (b:set real) (x y z:real): x∈ set.Ioo y z → set.Ioo y z ⊆ b → b∈ nhds x := begin intros A1 A2, apply mem_nhds_intro_real2 A1 _ A2, apply @is_open_Ioo ℝ _ _ _ y z, end lemma mem_nhds_filter_principal_intro (α:Type*) [topological_space α] (x:α) (V:set α): V∈ {S:set α|x∈ S ∧ is_open S}→ filter.principal V ∈ (set.image (filter.principal) {S:set α|x∈ S ∧ is_open S}) := begin intros, simp, apply a, end lemma mem_nhds_filter_principal_elim (α:Type*) [topological_space α] (x:α) (V:set α): filter.principal V ∈ (set.image (filter.principal) {S:set α|x∈ S ∧ is_open S}) → V∈ {S:set α|x∈ S ∧ is_open S} := begin intros, simp at a, apply a, end lemma mem_nhds_filter_principal_elim2 (α:Type*) [topological_space α] (x:α) (Z:filter α): Z ∈ (set.image (filter.principal) {S:set α|x∈ S ∧ is_open S}) → (∃ V:set α, V∈ {S:set α|x∈ S ∧ is_open S} ∧ filter.principal V = Z) := begin intros, simp at a, apply a, end lemma has_open_neighborhood (α:Type*) [topological_space α] (x:α):∃ U:set α, x∈ U ∧ is_open U := begin apply exists.intro set.univ, split, { simp, }, { apply topological_space.is_open_univ, } end lemma nhds_def2 (α:Type*) [topological_space α] (x:α) (S:set α): S ∈ nhds x ↔ (∃ u:set α, (u⊆ S) ∧ is_open u ∧ (x∈ u)) := begin rw nhds_def1, have H:∃ U:set α, x∈ U ∧ is_open U, apply has_open_neighborhood, cases H, cases H_h, apply iff.trans, { apply Inf_filter_def, { apply mem_nhds_filter_principal_intro, simp, split, { apply H_h_left, }, { apply H_h_right, } }, { intros, have A1:(∃ V:set α, V∈ {S:set α|x∈ S ∧ is_open S} ∧ filter.principal V = a), { apply mem_nhds_filter_principal_elim2, apply a_1, }, cases A1, cases A1_h, subst a, have A2:(∃ V:set α, V∈ {S:set α|x∈ S ∧ is_open S} ∧ filter.principal V = b), { apply mem_nhds_filter_principal_elim2, apply a_2, }, cases A2, cases A2_h, subst b, have A3:((A1_w ∩ A2_w) ∈ {s:set α|x∈ s∧ is_open s}), { apply open_nhds_inter;assumption, }, rw principal_inf, split, { split, { apply A3, }, { refl, } } } }, { split;intros, { cases a, cases a_h, have A4:(∃ V:set α, V∈ {S:set α|x∈ S ∧ is_open S} ∧ filter.principal V = a_w), { apply mem_nhds_filter_principal_elim2, apply a_h_w, }, cases A4, cases A4_h, subst a_w, apply exists.intro A4_w, cases A4_h_left, split, { assumption, }, split;assumption, }, { cases a, cases a_h, cases a_h_right, apply exists.intro (filter.principal a_w), { have A5:filter.principal a_w ∈ filter.principal '' {S : set α | x ∈ S ∧ is_open S}, { apply mem_nhds_filter_principal_intro, split;assumption, }, apply exists.intro A5, apply filter_principal_intro3, assumption, } } } end lemma nhds_contain_point (α:Type*) [topological_space α] (x:α) (S:set α): S ∈ nhds x → x∈ S := begin intros, have A1:(∃ u:set α, (u⊆ S) ∧ is_open u ∧ (x∈ u)), { apply (nhds_def2 α x S).mp a, }, cases A1, cases A1_h, rw set.subset_def at A1_h_left, apply A1_h_left, apply A1_h_right.right, end lemma Ioo_nbhd {x ε:ℝ}:(0 < ε)→ set.Ioo (x -ε) (x + ε) ∈ nhds x := begin intro A1, apply mem_nhds_intro_real, apply in_own_Ioo A1, apply set.subset.refl, end --Remove dependency on not equal to zero. lemma set_Ioo_in_nhds_of_ne_zero {x:nnreal} {ε:nnreal}:x ≠ 0 → ε >0 → set.Ioo (x - ε) (x + ε) ∈ nhds x := begin intros A1 A2, rw @mem_nhds_sets_iff, -- have A3:(ε≤x) ∨ (x < ε) := le_or_lt ε x, apply exists.intro (set.Ioo (x - ε) (x + ε)), split, apply @complete_lattice.le_refl (set nnreal) (@set.lattice_set nnreal), split, apply is_open_Ioo, simp, split, { apply nnreal.sub_lt_self, { apply bot_lt_iff_ne_bot.mpr, apply A1, }, { apply A2, }, }, { apply A2, }, end --TODO: remove dependence on x ≠ 0 lemma set_Iio_in_nhds_of_lt {x y:nnreal}:x ≠ 0 → x < y → set.Iio y ∈ nhds x := begin intros AX A1, rw @mem_nhds_sets_iff, apply exists.intro (set.Ioo 0 y), split, { apply set.Ioo_subset_Iio_self, }, split, { apply is_open_Ioo, }, simp, split, { apply bot_lt_iff_ne_bot.mpr, apply AX, }, { apply A1, }, end
b107ec8c069fb590031632b1c0502feeb45a4c89
d642a6b1261b2cbe691e53561ac777b924751b63
/src/algebra/big_operators.lean
6735956804b33b3e3f5e1aff5c43bd6b4a1bb2c4
[ "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
34,813
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 Some big operators for lists and finite sets. -/ import tactic.tauto data.list.basic data.finset data.nat.enat import algebra.group algebra.ordered_group algebra.group_power universes u v w variables {α : Type u} {β : Type v} {γ : Type w} theorem directed.finset_le {r : α → α → Prop} [is_trans α r] {ι} (hι : nonempty ι) {f : ι → α} (D : directed r f) (s : finset ι) : ∃ z, ∀ i ∈ s, r (f i) (f z) := show ∃ z, ∀ i ∈ s.1, r (f i) (f z), from multiset.induction_on s.1 (let ⟨z⟩ := hι in ⟨z, λ _, false.elim⟩) $ λ i s ⟨j, H⟩, let ⟨k, h₁, h₂⟩ := D i j in ⟨k, λ a h, or.cases_on (multiset.mem_cons.1 h) (λ h, h.symm ▸ h₁) (λ h, trans (H _ h) h₂)⟩ theorem finset.exists_le {α : Type u} [nonempty α] [directed_order α] (s : finset α) : ∃ M, ∀ i ∈ s, i ≤ M := directed.finset_le (by apply_instance) directed_order.directed s namespace finset variables {s s₁ s₂ : finset α} {a : α} {f g : α → β} /-- `prod s f` is the product of `f x` as `x` ranges over the elements of the finite set `s`. -/ @[to_additive] protected def prod [comm_monoid β] (s : finset α) (f : α → β) : β := (s.1.map f).prod @[to_additive] lemma prod_eq_multiset_prod [comm_monoid β] (s : finset α) (f : α → β) : s.prod f = (s.1.map f).prod := rfl @[to_additive] theorem prod_eq_fold [comm_monoid β] (s : finset α) (f : α → β) : s.prod f = s.fold (*) 1 f := rfl section comm_monoid variables [comm_monoid β] @[simp, to_additive] lemma prod_empty {α : Type u} {f : α → β} : (∅:finset α).prod f = 1 := rfl @[simp, to_additive] lemma prod_insert [decidable_eq α] : a ∉ s → (insert a s).prod f = f a * s.prod f := fold_insert @[simp, to_additive] lemma prod_singleton : (singleton a).prod f = f a := eq.trans fold_singleton $ mul_one _ @[simp] lemma prod_const_one : s.prod (λx, (1 : β)) = 1 := by simp only [finset.prod, multiset.map_const, multiset.prod_repeat, one_pow] @[simp] lemma sum_const_zero {β} {s : finset α} [add_comm_monoid β] : s.sum (λx, (0 : β)) = 0 := @prod_const_one _ (multiplicative β) _ _ attribute [to_additive] prod_const_one @[simp, to_additive] lemma prod_image [decidable_eq α] {s : finset γ} {g : γ → α} : (∀x∈s, ∀y∈s, g x = g y → x = y) → (s.image g).prod f = s.prod (λx, f (g x)) := fold_image @[simp, to_additive] lemma prod_map (s : finset α) (e : α ↪ γ) (f : γ → β) : (s.map e).prod f = s.prod (λa, f (e a)) := by rw [finset.prod, finset.map_val, multiset.map_map]; refl @[congr, to_additive] lemma prod_congr (h : s₁ = s₂) : (∀x∈s₂, f x = g x) → s₁.prod f = s₂.prod g := by rw [h]; exact fold_congr attribute [congr] finset.sum_congr @[to_additive] lemma prod_union_inter [decidable_eq α] : (s₁ ∪ s₂).prod f * (s₁ ∩ s₂).prod f = s₁.prod f * s₂.prod f := fold_union_inter @[to_additive] lemma prod_union [decidable_eq α] (h : disjoint s₁ s₂) : (s₁ ∪ s₂).prod f = s₁.prod f * s₂.prod f := by rw [←prod_union_inter, (disjoint_iff_inter_eq_empty.mp h)]; exact (mul_one _).symm @[to_additive] lemma prod_sdiff [decidable_eq α] (h : s₁ ⊆ s₂) : (s₂ \ s₁).prod f * s₁.prod f = s₂.prod f := by rw [←prod_union sdiff_disjoint, sdiff_union_of_subset h] @[to_additive] lemma prod_bind [decidable_eq α] {s : finset γ} {t : γ → finset α} : (∀x∈s, ∀y∈s, x ≠ y → disjoint (t x) (t y)) → (s.bind t).prod f = s.prod (λx, (t x).prod f) := by haveI := classical.dec_eq γ; exact finset.induction_on s (λ _, by simp only [bind_empty, prod_empty]) (assume x s hxs ih hd, have hd' : ∀x∈s, ∀y∈s, x ≠ y → disjoint (t x) (t y), from assume _ hx _ hy, hd _ (mem_insert_of_mem hx) _ (mem_insert_of_mem hy), have ∀y∈s, x ≠ y, from assume _ hy h, by rw [←h] at hy; contradiction, have ∀y∈s, disjoint (t x) (t y), from assume _ hy, hd _ (mem_insert_self _ _) _ (mem_insert_of_mem hy) (this _ hy), have disjoint (t x) (finset.bind s t), from (disjoint_bind_right _ _ _).mpr this, by simp only [bind_insert, prod_insert hxs, prod_union this, ih hd']) @[to_additive] lemma prod_product {s : finset γ} {t : finset α} {f : γ×α → β} : (s.product t).prod f = s.prod (λx, t.prod $ λy, f (x, y)) := begin haveI := classical.dec_eq α, haveI := classical.dec_eq γ, rw [product_eq_bind, prod_bind], { congr, funext, exact prod_image (λ _ _ _ _ H, (prod.mk.inj H).2) }, simp only [disjoint_iff_ne, mem_image], rintros _ _ _ _ h ⟨_, _⟩ ⟨_, _, ⟨_, _⟩⟩ ⟨_, _⟩ ⟨_, _, ⟨_, _⟩⟩ _, apply h, cc end @[to_additive] lemma prod_sigma {σ : α → Type*} {s : finset α} {t : Πa, finset (σ a)} {f : sigma σ → β} : (s.sigma t).prod f = s.prod (λa, (t a).prod $ λs, f ⟨a, s⟩) := by haveI := classical.dec_eq α; haveI := (λ a, classical.dec_eq (σ a)); exact calc (s.sigma t).prod f = (s.bind (λa, (t a).image (λs, sigma.mk a s))).prod f : by rw sigma_eq_bind ... = s.prod (λa, ((t a).image (λs, sigma.mk a s)).prod f) : prod_bind $ assume a₁ ha a₂ ha₂ h, by simp only [disjoint_iff_ne, mem_image]; rintro ⟨_, _⟩ ⟨_, _, _⟩ ⟨_, _⟩ ⟨_, _, _⟩ ⟨_, _⟩; apply h; cc ... = (s.prod $ λa, (t a).prod $ λs, f ⟨a, s⟩) : prod_congr rfl $ λ _ _, prod_image $ λ _ _ _ _ _, by cc @[to_additive] lemma prod_image' [decidable_eq α] {s : finset γ} {g : γ → α} (h : γ → β) (eq : ∀c∈s, f (g c) = (s.filter (λc', g c' = g c)).prod h) : (s.image g).prod f = s.prod h := begin letI := classical.dec_eq γ, rw [← image_bind_filter_eq s g] {occs := occurrences.pos [2]}, rw [finset.prod_bind], { refine finset.prod_congr rfl (assume a ha, _), rcases finset.mem_image.1 ha with ⟨b, hb, rfl⟩, exact eq b hb }, assume a₀ _ a₁ _ ne, refine (disjoint_iff_ne.2 _), assume c₀ h₀ c₁ h₁, rcases mem_filter.1 h₀ with ⟨h₀, rfl⟩, rcases mem_filter.1 h₁ with ⟨h₁, rfl⟩, exact mt (congr_arg g) ne end @[to_additive] lemma prod_mul_distrib : s.prod (λx, f x * g x) = s.prod f * s.prod g := eq.trans (by rw one_mul; refl) fold_op_distrib @[to_additive] lemma prod_comm [decidable_eq γ] {s : finset γ} {t : finset α} {f : γ → α → β} : s.prod (λx, t.prod $ f x) = t.prod (λy, s.prod $ λx, f x y) := finset.induction_on s (by simp only [prod_empty, prod_const_one]) $ λ _ _ H ih, by simp only [prod_insert H, prod_mul_distrib, ih] lemma prod_hom [comm_monoid γ] (g : β → γ) [is_monoid_hom g] : s.prod (λx, g (f x)) = g (s.prod f) := eq.trans (by rw is_monoid_hom.map_one g; refl) (fold_hom $ is_monoid_hom.map_mul g) @[to_additive] lemma prod_hom_rel [comm_monoid γ] {r : β → γ → Prop} {f : α → β} {g : α → γ} {s : finset α} (h₁ : r 1 1) (h₂ : ∀a b c, r b c → r (f a * b) (g a * c)) : r (s.prod f) (s.prod g) := begin letI := classical.dec_eq α, refine finset.induction_on s h₁ (assume a s has ih, _), rw [prod_insert has, prod_insert has], exact h₂ a (s.prod f) (s.prod g) ih, end @[to_additive] lemma prod_subset (h : s₁ ⊆ s₂) (hf : ∀x∈s₂, x ∉ s₁ → f x = 1) : s₁.prod f = s₂.prod f := by haveI := classical.dec_eq α; exact have (s₂ \ s₁).prod f = (s₂ \ s₁).prod (λx, 1), from prod_congr rfl $ by simpa only [mem_sdiff, and_imp], by rw [←prod_sdiff h]; simp only [this, prod_const_one, one_mul] @[to_additive] lemma prod_filter (p : α → Prop) [decidable_pred p] (f : α → β) : (s.filter p).prod f = s.prod (λa, if p a then f a else 1) := calc (s.filter p).prod f = (s.filter p).prod (λa, if p a then f a else 1) : prod_congr rfl (assume a h, by rw [if_pos (mem_filter.1 h).2]) ... = s.prod (λa, if p a then f a else 1) : begin refine prod_subset (filter_subset s) (assume x hs h, _), rw [mem_filter, not_and] at h, exact if_neg (h hs) end @[to_additive] lemma prod_eq_single {s : finset α} {f : α → β} (a : α) (h₀ : ∀b∈s, b ≠ a → f b = 1) (h₁ : a ∉ s → f a = 1) : s.prod f = f a := by haveI := classical.dec_eq α; from classical.by_cases (assume : a ∈ s, calc s.prod f = ({a} : finset α).prod f : begin refine (prod_subset _ _).symm, { intros _ H, rwa mem_singleton.1 H }, { simpa only [mem_singleton] } end ... = f a : prod_singleton) (assume : a ∉ s, (prod_congr rfl $ λ b hb, h₀ b hb $ by rintro rfl; cc).trans $ prod_const_one.trans (h₁ this).symm) @[to_additive] lemma prod_ite [comm_monoid γ] {s : finset α} {p : α → Prop} {hp : decidable_pred p} (f g : α → γ) (h : γ → β) : s.prod (λ x, h (if p x then f x else g x)) = (s.filter p).prod (λ x, h (f x)) * (s.filter (λ x, ¬ p x)).prod (λ x, h (g x)) := by letI := classical.dec_eq α; exact calc s.prod (λ x, h (if p x then f x else g x)) = (s.filter p ∪ s.filter (λ x, ¬ p x)).prod (λ x, h (if p x then f x else g x)) : by rw [filter_union_filter_neg_eq] ... = (s.filter p).prod (λ x, h (if p x then f x else g x)) * (s.filter (λ x, ¬ p x)).prod (λ x, h (if p x then f x else g x)) : prod_union (by simp [disjoint_right] {contextual := tt}) ... = (s.filter p).prod (λ x, h (f x)) * (s.filter (λ x, ¬ p x)).prod (λ x, h (g x)) : congr_arg2 _ (prod_congr rfl (by simp {contextual := tt})) (prod_congr rfl (by simp {contextual := tt})) @[simp, to_additive] lemma prod_ite_eq [decidable_eq α] (s : finset α) (a : α) (b : β) : s.prod (λ x, (ite (a = x) b 1)) = ite (a ∈ s) b 1 := begin rw ←finset.prod_filter, split_ifs; simp only [filter_eq, if_true, if_false, h, prod_empty, prod_singleton, insert_empty_eq_singleton], end @[to_additive] lemma prod_attach {f : α → β} : s.attach.prod (λx, f x.val) = s.prod f := by haveI := classical.dec_eq α; exact calc s.attach.prod (λx, f x.val) = ((s.attach).image subtype.val).prod f : by rw [prod_image]; exact assume x _ y _, subtype.eq ... = _ : by rw [attach_image_val] @[to_additive] lemma prod_bij {s : finset α} {t : finset γ} {f : α → β} {g : γ → β} (i : Πa∈s, γ) (hi : ∀a ha, i a ha ∈ t) (h : ∀a ha, f a = g (i a ha)) (i_inj : ∀a₁ a₂ ha₁ ha₂, i a₁ ha₁ = i a₂ ha₂ → a₁ = a₂) (i_surj : ∀b∈t, ∃a ha, b = i a ha) : s.prod f = t.prod g := congr_arg multiset.prod (multiset.map_eq_map_of_bij_of_nodup f g s.2 t.2 i hi h i_inj i_surj) @[to_additive] lemma prod_bij_ne_one {s : finset α} {t : finset γ} {f : α → β} {g : γ → β} (i : Πa∈s, f a ≠ 1 → γ) (hi₁ : ∀a h₁ h₂, i a h₁ h₂ ∈ t) (hi₂ : ∀a₁ a₂ h₁₁ h₁₂ h₂₁ h₂₂, i a₁ h₁₁ h₁₂ = i a₂ h₂₁ h₂₂ → a₁ = a₂) (hi₃ : ∀b∈t, g b ≠ 1 → ∃a h₁ h₂, b = i a h₁ h₂) (h : ∀a h₁ h₂, f a = g (i a h₁ h₂)) : s.prod f = t.prod g := by haveI := classical.prop_decidable; exact calc s.prod f = (s.filter $ λx, f x ≠ 1).prod f : (prod_subset (filter_subset _) $ by simp only [not_imp_comm, mem_filter]; exact λ _, and.intro).symm ... = (t.filter $ λx, g x ≠ 1).prod g : prod_bij (assume a ha, i a (mem_filter.mp ha).1 (mem_filter.mp ha).2) (assume a ha, (mem_filter.mp ha).elim $ λh₁ h₂, mem_filter.mpr ⟨hi₁ a h₁ h₂, λ hg, h₂ (hg ▸ h a h₁ h₂)⟩) (assume a ha, (mem_filter.mp ha).elim $ h a) (assume a₁ a₂ ha₁ ha₂, (mem_filter.mp ha₁).elim $ λha₁₁ ha₁₂, (mem_filter.mp ha₂).elim $ λha₂₁ ha₂₂, hi₂ a₁ a₂ _ _ _ _) (assume b hb, (mem_filter.mp hb).elim $ λh₁ h₂, let ⟨a, ha₁, ha₂, eq⟩ := hi₃ b h₁ h₂ in ⟨a, mem_filter.mpr ⟨ha₁, ha₂⟩, eq⟩) ... = t.prod g : (prod_subset (filter_subset _) $ by simp only [not_imp_comm, mem_filter]; exact λ _, and.intro) @[to_additive] lemma exists_ne_one_of_prod_ne_one : s.prod f ≠ 1 → ∃a∈s, f a ≠ 1 := by haveI := classical.dec_eq α; exact finset.induction_on s (λ H, (H rfl).elim) (assume a s has ih h, classical.by_cases (assume ha : f a = 1, let ⟨a, ha, hfa⟩ := ih (by rwa [prod_insert has, ha, one_mul] at h) in ⟨a, mem_insert_of_mem ha, hfa⟩) (assume hna : f a ≠ 1, ⟨a, mem_insert_self _ _, hna⟩)) @[to_additive] lemma prod_range_succ (f : ℕ → β) (n : ℕ) : (range (nat.succ n)).prod f = f n * (range n).prod f := by rw [range_succ, prod_insert not_mem_range_self] lemma prod_range_succ' (f : ℕ → β) : ∀ n : ℕ, (range (nat.succ n)).prod f = (range n).prod (f ∘ nat.succ) * f 0 | 0 := (prod_range_succ _ _).trans $ mul_comm _ _ | (n + 1) := by rw [prod_range_succ (λ m, f (nat.succ m)), mul_assoc, ← prod_range_succ']; exact prod_range_succ _ _ lemma sum_Ico_add {δ : Type*} [add_comm_monoid δ] (f : ℕ → δ) (m n k : ℕ) : (Ico m n).sum (λ l, f (k + l)) = (Ico (m + k) (n + k)).sum f := Ico.image_add m n k ▸ eq.symm $ sum_image $ λ x hx y hy h, nat.add_left_cancel h @[to_additive] lemma prod_Ico_add (f : ℕ → β) (m n k : ℕ) : (Ico m n).prod (λ l, f (k + l)) = (Ico (m + k) (n + k)).prod f := Ico.image_add m n k ▸ eq.symm $ prod_image $ λ x hx y hy h, nat.add_left_cancel h @[to_additive] lemma prod_Ico_consecutive (f : ℕ → β) {m n k : ℕ} (hmn : m ≤ n) (hnk : n ≤ k) : (Ico m n).prod f * (Ico n k).prod f = (Ico m k).prod f := Ico.union_consecutive hmn hnk ▸ eq.symm $ prod_union $ Ico.disjoint_consecutive m n k @[to_additive] lemma prod_range_mul_prod_Ico (f : ℕ → β) {m n : ℕ} (h : m ≤ n) : (range m).prod f * (Ico m n).prod f = (range n).prod f := Ico.zero_bot m ▸ Ico.zero_bot n ▸ prod_Ico_consecutive f (nat.zero_le m) h @[to_additive sum_Ico_eq_add_neg] lemma prod_Ico_eq_div {δ : Type*} [comm_group δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) : (Ico m n).prod f = (range n).prod f * ((range m).prod f)⁻¹ := eq_mul_inv_iff_mul_eq.2 $ by rw [mul_comm]; exact prod_range_mul_prod_Ico f h lemma sum_Ico_eq_sub {δ : Type*} [add_comm_group δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) : (Ico m n).sum f = (range n).sum f - (range m).sum f := sum_Ico_eq_add_neg f h @[to_additive] lemma prod_Ico_eq_prod_range (f : ℕ → β) (m n : ℕ) : (Ico m n).prod f = (range (n - m)).prod (λ l, f (m + l)) := begin by_cases h : m ≤ n, { rw [← Ico.zero_bot, prod_Ico_add, zero_add, nat.sub_add_cancel h] }, { replace h : n ≤ m := le_of_not_ge h, rw [Ico.eq_empty_of_le h, nat.sub_eq_zero_of_le h, range_zero, prod_empty, prod_empty] } end @[to_additive] lemma prod_range_zero (f : ℕ → β) : (range 0).prod f = 1 := by rw [range_zero, prod_empty] lemma prod_range_one (f : ℕ → β) : (range 1).prod f = f 0 := by { rw [range_one], apply @prod_singleton ℕ β 0 f } lemma sum_range_one {δ : Type*} [add_comm_monoid δ] (f : ℕ → δ) : (range 1).sum f = f 0 := by { rw [range_one], apply @sum_singleton ℕ δ 0 f } attribute [to_additive finset.sum_range_one] prod_range_one @[simp] lemma prod_const (b : β) : s.prod (λ a, b) = b ^ s.card := by haveI := classical.dec_eq α; exact finset.induction_on s rfl (λ a s has ih, by rw [prod_insert has, card_insert_of_not_mem has, pow_succ, ih]) lemma prod_pow (s : finset α) (n : ℕ) (f : α → β) : s.prod (λ x, f x ^ n) = s.prod f ^ n := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (by simp [_root_.mul_pow] {contextual := tt}) lemma prod_nat_pow (s : finset α) (n : ℕ) (f : α → ℕ) : s.prod (λ x, f x ^ n) = s.prod f ^ n := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (by simp [nat.mul_pow] {contextual := tt}) @[to_additive] lemma prod_involution {s : finset α} {f : α → β} : ∀ (g : Π a ∈ s, α) (h₁ : ∀ a ha, f a * f (g a ha) = 1) (h₂ : ∀ a ha, f a ≠ 1 → g a ha ≠ a) (h₃ : ∀ a ha, g a ha ∈ s) (h₄ : ∀ a ha, g (g a ha) (h₃ a ha) = a), s.prod f = 1 := by haveI := classical.dec_eq α; haveI := classical.dec_eq β; exact finset.strong_induction_on s (λ s ih g h₁ h₂ h₃ h₄, if hs : s = ∅ then hs.symm ▸ rfl else let ⟨x, hx⟩ := exists_mem_of_ne_empty hs in have hmem : ∀ y ∈ (s.erase x).erase (g x hx), y ∈ s, from λ y hy, (mem_of_mem_erase (mem_of_mem_erase hy)), have g_inj : ∀ {x hx y hy}, g x hx = g y hy → x = y, from λ x hx y hy h, by rw [← h₄ x hx, ← h₄ y hy]; simp [h], have ih': (erase (erase s x) (g x hx)).prod f = (1 : β) := ih ((s.erase x).erase (g x hx)) ⟨subset.trans (erase_subset _ _) (erase_subset _ _), λ h, not_mem_erase (g x hx) (s.erase x) (h (h₃ x hx))⟩ (λ y hy, g y (hmem y hy)) (λ y hy, h₁ y (hmem y hy)) (λ y hy, h₂ y (hmem y hy)) (λ y hy, mem_erase.2 ⟨λ (h : g y _ = g x hx), by simpa [g_inj h] using hy, mem_erase.2 ⟨λ (h : g y _ = x), have y = g x hx, from h₄ y (hmem y hy) ▸ by simp [h], by simpa [this] using hy, h₃ y (hmem y hy)⟩⟩) (λ y hy, h₄ y (hmem y hy)), if hx1 : f x = 1 then ih' ▸ eq.symm (prod_subset hmem (λ y hy hy₁, have y = x ∨ y = g x hx, by simp [hy] at hy₁; tauto, this.elim (λ h, h.symm ▸ hx1) (λ h, h₁ x hx ▸ h ▸ hx1.symm ▸ (one_mul _).symm))) else by rw [← insert_erase hx, prod_insert (not_mem_erase _ _), ← insert_erase (mem_erase.2 ⟨h₂ x hx hx1, h₃ x hx⟩), prod_insert (not_mem_erase _ _), ih', mul_one, h₁ x hx]) @[to_additive] lemma prod_eq_one {f : α → β} {s : finset α} (h : ∀x∈s, f x = 1) : s.prod f = 1 := calc s.prod f = s.prod (λx, 1) : finset.prod_congr rfl h ... = 1 : finset.prod_const_one end comm_monoid attribute [to_additive] prod_hom lemma sum_smul [add_comm_monoid β] (s : finset α) (n : ℕ) (f : α → β) : s.sum (λ x, add_monoid.smul n (f x)) = add_monoid.smul n (s.sum f) := @prod_pow _ (multiplicative β) _ _ _ _ attribute [to_additive sum_smul] prod_pow @[simp] lemma sum_const [add_comm_monoid β] (b : β) : s.sum (λ a, b) = add_monoid.smul s.card b := @prod_const _ (multiplicative β) _ _ _ attribute [to_additive] prod_const lemma sum_range_succ' [add_comm_monoid β] (f : ℕ → β) : ∀ n : ℕ, (range (nat.succ n)).sum f = (range n).sum (f ∘ nat.succ) + f 0 := @prod_range_succ' (multiplicative β) _ _ attribute [to_additive] prod_range_succ' lemma sum_nat_cast [add_comm_monoid β] [has_one β] (s : finset α) (f : α → ℕ) : ↑(s.sum f) = s.sum (λa, f a : α → β) := (sum_hom _).symm lemma prod_nat_cast [comm_semiring β] (s : finset α) (f : α → ℕ) : ↑(s.prod f) = s.prod (λa, f a : α → β) := (prod_hom _).symm protected lemma sum_nat_coe_enat [decidable_eq α] (s : finset α) (f : α → ℕ) : s.sum (λ x, (f x : enat)) = (s.sum f : ℕ) := begin induction s using finset.induction with a s has ih h, { simp }, { simp [has, ih] } end lemma le_sum_of_subadditive [add_comm_monoid α] [ordered_comm_monoid β] (f : α → β) (h_zero : f 0 = 0) (h_add : ∀x y, f (x + y) ≤ f x + f y) (s : finset γ) (g : γ → α) : f (s.sum g) ≤ s.sum (λc, f (g c)) := begin refine le_trans (multiset.le_sum_of_subadditive f h_zero h_add _) _, rw [multiset.map_map], refl end lemma abs_sum_le_sum_abs [discrete_linear_ordered_field α] {f : β → α} {s : finset β} : abs (s.sum f) ≤ s.sum (λa, abs (f a)) := le_sum_of_subadditive _ abs_zero abs_add s f section comm_group variables [comm_group β] @[simp, to_additive] lemma prod_inv_distrib : s.prod (λx, (f x)⁻¹) = (s.prod f)⁻¹ := prod_hom has_inv.inv end comm_group @[simp] theorem card_sigma {σ : α → Type*} (s : finset α) (t : Π a, finset (σ a)) : card (s.sigma t) = s.sum (λ a, card (t a)) := multiset.card_sigma _ _ lemma card_bind [decidable_eq β] {s : finset α} {t : α → finset β} (h : ∀ x ∈ s, ∀ y ∈ s, x ≠ y → disjoint (t x) (t y)) : (s.bind t).card = s.sum (λ u, card (t u)) := calc (s.bind t).card = (s.bind t).sum (λ _, 1) : by simp ... = s.sum (λ a, (t a).sum (λ _, 1)) : finset.sum_bind h ... = s.sum (λ u, card (t u)) : by simp lemma card_bind_le [decidable_eq β] {s : finset α} {t : α → finset β} : (s.bind t).card ≤ s.sum (λ a, (t a).card) := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (λ a s has ih, calc ((insert a s).bind t).card ≤ (t a).card + (s.bind t).card : by rw bind_insert; exact finset.card_union_le _ _ ... ≤ (insert a s).sum (λ a, card (t a)) : by rw sum_insert has; exact add_le_add_left ih _) theorem card_eq_sum_card_image [decidable_eq β] (f : α → β) (s : finset α) : s.card = (s.image f).sum (λ a, (s.filter (λ x, f x = a)).card) := by letI := classical.dec_eq α; exact calc s.card = ((s.image f).bind (λ a, s.filter (λ x, f x = a))).card : congr_arg _ (finset.ext.2 $ λ x, ⟨λ hs, mem_bind.2 ⟨f x, mem_image_of_mem _ hs, mem_filter.2 ⟨hs, rfl⟩⟩, λ h, let ⟨a, ha₁, ha₂⟩ := mem_bind.1 h in by convert filter_subset s ha₂⟩) ... = (s.image f).sum (λ a, (s.filter (λ x, f x = a)).card) : card_bind (by simp [disjoint_left, finset.ext] {contextual := tt}) lemma gsmul_sum [add_comm_group β] {f : α → β} {s : finset α} (z : ℤ) : gsmul z (s.sum f) = s.sum (λa, gsmul z (f a)) := (finset.sum_hom (gsmul z)).symm end finset namespace finset variables {s s₁ s₂ : finset α} {f g : α → β} {b : β} {a : α} @[simp] lemma sum_sub_distrib [add_comm_group β] : s.sum (λx, f x - g x) = s.sum f - s.sum g := sum_add_distrib.trans $ congr_arg _ sum_neg_distrib section semiring variables [semiring β] lemma sum_mul : s.sum f * b = s.sum (λx, f x * b) := (sum_hom (λx, x * b)).symm lemma mul_sum : b * s.sum f = s.sum (λx, b * f x) := (sum_hom (λx, b * x)).symm @[simp] lemma sum_mul_boole [decidable_eq α] (s : finset α) (f : α → β) (a : α) : s.sum (λ x, (f x * ite (a = x) 1 0)) = ite (a ∈ s) (f a) 0 := begin convert sum_ite_eq s a (f a), funext, split_ifs with h; simp [h], end @[simp] lemma sum_boole_mul [decidable_eq α] (s : finset α) (f : α → β) (a : α) : s.sum (λ x, (ite (a = x) 1 0) * f x) = ite (a ∈ s) (f a) 0 := begin convert sum_ite_eq s a (f a), funext, split_ifs with h; simp [h], end end semiring section comm_semiring variables [decidable_eq α] [comm_semiring β] lemma prod_eq_zero (ha : a ∈ s) (h : f a = 0) : s.prod f = 0 := calc s.prod f = (insert a (erase s a)).prod f : by rw insert_erase ha ... = 0 : by rw [prod_insert (not_mem_erase _ _), h, zero_mul] lemma prod_sum {δ : α → Type*} [∀a, decidable_eq (δ a)] {s : finset α} {t : Πa, finset (δ a)} {f : Πa, δ a → β} : s.prod (λa, (t a).sum (λb, f a b)) = (s.pi t).sum (λp, s.attach.prod (λx, f x.1 (p x.1 x.2))) := begin induction s using finset.induction with a s ha ih, { rw [pi_empty, sum_singleton], refl }, { have h₁ : ∀x ∈ t a, ∀y ∈ t a, ∀h : x ≠ y, disjoint (image (pi.cons s a x) (pi s t)) (image (pi.cons s a y) (pi s t)), { assume x hx y hy h, simp only [disjoint_iff_ne, mem_image], rintros _ ⟨p₂, hp, eq₂⟩ _ ⟨p₃, hp₃, eq₃⟩ eq, have : pi.cons s a x p₂ a (mem_insert_self _ _) = pi.cons s a y p₃ a (mem_insert_self _ _), { rw [eq₂, eq₃, eq] }, rw [pi.cons_same, pi.cons_same] at this, exact h this }, rw [prod_insert ha, pi_insert ha, ih, sum_mul, sum_bind h₁], refine sum_congr rfl (λ b _, _), have h₂ : ∀p₁∈pi s t, ∀p₂∈pi s t, pi.cons s a b p₁ = pi.cons s a b p₂ → p₁ = p₂, from assume p₁ h₁ p₂ h₂ eq, injective_pi_cons ha eq, rw [sum_image h₂, mul_sum], refine sum_congr rfl (λ g _, _), rw [attach_insert, prod_insert, prod_image], { simp only [pi.cons_same], congr', ext ⟨v, hv⟩, congr', exact (pi.cons_ne (by rintro rfl; exact ha hv)).symm }, { exact λ _ _ _ _, subtype.eq ∘ subtype.mk.inj }, { simp only [mem_image], rintro ⟨⟨_, hm⟩, _, rfl⟩, exact ha hm } } end end comm_semiring section integral_domain /- add integral_semi_domain to support nat and ennreal -/ variables [decidable_eq α] [integral_domain β] lemma prod_eq_zero_iff : s.prod f = 0 ↔ (∃a∈s, f a = 0) := finset.induction_on s ⟨not.elim one_ne_zero, λ ⟨_, H, _⟩, H.elim⟩ $ λ a s ha ih, by rw [prod_insert ha, mul_eq_zero_iff_eq_zero_or_eq_zero, bex_def, exists_mem_insert, ih, ← bex_def] end integral_domain section ordered_comm_monoid variables [decidable_eq α] [ordered_comm_monoid β] lemma sum_le_sum : (∀x∈s, f x ≤ g x) → s.sum f ≤ s.sum g := finset.induction_on s (λ _, le_refl _) $ assume a s ha ih h, have f a + s.sum f ≤ g a + s.sum g, from add_le_add' (h _ (mem_insert_self _ _)) (ih $ assume x hx, h _ $ mem_insert_of_mem hx), by simpa only [sum_insert ha] lemma sum_nonneg (h : ∀x∈s, 0 ≤ f x) : 0 ≤ s.sum f := le_trans (by rw [sum_const_zero]) (sum_le_sum h) lemma sum_nonpos (h : ∀x∈s, f x ≤ 0) : s.sum f ≤ 0 := le_trans (sum_le_sum h) (by rw [sum_const_zero]) lemma sum_le_sum_of_subset_of_nonneg (h : s₁ ⊆ s₂) (hf : ∀x∈s₂, x ∉ s₁ → 0 ≤ f x) : s₁.sum f ≤ s₂.sum f := calc s₁.sum f ≤ (s₂ \ s₁).sum f + s₁.sum f : le_add_of_nonneg_left' $ sum_nonneg $ by simpa only [mem_sdiff, and_imp] ... = (s₂ \ s₁ ∪ s₁).sum f : (sum_union sdiff_disjoint).symm ... = s₂.sum f : by rw [sdiff_union_of_subset h] lemma sum_eq_zero_iff_of_nonneg : (∀x∈s, 0 ≤ f x) → (s.sum f = 0 ↔ ∀x∈s, f x = 0) := finset.induction_on s (λ _, ⟨λ _ _, false.elim, λ _, rfl⟩) $ λ a s ha ih H, have ∀ x ∈ s, 0 ≤ f x, from λ _, H _ ∘ mem_insert_of_mem, by rw [sum_insert ha, add_eq_zero_iff' (H _ $ mem_insert_self _ _) (sum_nonneg this), forall_mem_insert, ih this] lemma sum_eq_zero_iff_of_nonpos : (∀x∈s, f x ≤ 0) → (s.sum f = 0 ↔ ∀x∈s, f x = 0) := @sum_eq_zero_iff_of_nonneg _ (order_dual β) _ _ _ _ lemma single_le_sum (hf : ∀x∈s, 0 ≤ f x) {a} (h : a ∈ s) : f a ≤ s.sum f := have (singleton a).sum f ≤ s.sum f, from sum_le_sum_of_subset_of_nonneg (λ x e, (mem_singleton.1 e).symm ▸ h) (λ x h _, hf x h), by rwa sum_singleton at this end ordered_comm_monoid section canonically_ordered_monoid variables [decidable_eq α] [canonically_ordered_monoid β] lemma sum_le_sum_of_subset (h : s₁ ⊆ s₂) : s₁.sum f ≤ s₂.sum f := sum_le_sum_of_subset_of_nonneg h $ assume x h₁ h₂, zero_le _ lemma sum_le_sum_of_ne_zero [@decidable_rel β (≤)] (h : ∀x∈s₁, f x ≠ 0 → x ∈ s₂) : s₁.sum f ≤ s₂.sum f := calc s₁.sum f = (s₁.filter (λx, f x = 0)).sum f + (s₁.filter (λx, f x ≠ 0)).sum f : by rw [←sum_union, filter_union_filter_neg_eq]; exact disjoint_filter.2 (assume _ _ h n_h, n_h h) ... ≤ s₂.sum f : add_le_of_nonpos_of_le' (sum_nonpos $ by simp only [mem_filter, and_imp]; exact λ _ _, le_of_eq) (sum_le_sum_of_subset $ by simpa only [subset_iff, mem_filter, and_imp]) end canonically_ordered_monoid section linear_ordered_comm_ring variables [decidable_eq α] [linear_ordered_comm_ring β] /- this is also true for a ordered commutative multiplicative monoid -/ lemma prod_nonneg {s : finset α} {f : α → β} (h0 : ∀(x ∈ s), 0 ≤ f x) : 0 ≤ s.prod f := begin induction s using finset.induction with a s has ih h, { simp [zero_le_one] }, { simp [has], apply mul_nonneg, apply h0 a (mem_insert_self a s), exact ih (λ x H, h0 x (mem_insert_of_mem H)) } end /- this is also true for a ordered commutative multiplicative monoid -/ lemma prod_pos {s : finset α} {f : α → β} (h0 : ∀(x ∈ s), 0 < f x) : 0 < s.prod f := begin induction s using finset.induction with a s has ih h, { simp [zero_lt_one] }, { simp [has], apply mul_pos, apply h0 a (mem_insert_self a s), exact ih (λ x H, h0 x (mem_insert_of_mem H)) } end /- this is also true for a ordered commutative multiplicative monoid -/ lemma prod_le_prod {s : finset α} {f g : α → β} (h0 : ∀(x ∈ s), 0 ≤ f x) (h1 : ∀(x ∈ s), f x ≤ g x) : s.prod f ≤ s.prod g := begin induction s using finset.induction with a s has ih h, { simp }, { simp [has], apply mul_le_mul, exact h1 a (mem_insert_self a s), apply ih (λ x H, h0 _ _) (λ x H, h1 _ _); exact (mem_insert_of_mem H), apply prod_nonneg (λ x H, h0 x (mem_insert_of_mem H)), apply le_trans (h0 a (mem_insert_self a s)) (h1 a (mem_insert_self a s)) } end end linear_ordered_comm_ring @[simp] lemma card_pi [decidable_eq α] {δ : α → Type*} (s : finset α) (t : Π a, finset (δ a)) : (s.pi t).card = s.prod (λ a, card (t a)) := multiset.card_pi _ _ theorem card_le_mul_card_image [decidable_eq β] {f : α → β} (s : finset α) (n : ℕ) (hn : ∀ a ∈ s.image f, (s.filter (λ x, f x = a)).card ≤ n) : s.card ≤ n * (s.image f).card := calc s.card = (s.image f).sum (λ a, (s.filter (λ x, f x = a)).card) : card_eq_sum_card_image _ _ ... ≤ (s.image f).sum (λ _, n) : sum_le_sum hn ... = _ : by simp [mul_comm] @[simp] lemma prod_Ico_id_eq_fact (n : ℕ) : (Ico 1 n.succ).prod (λ x, x) = nat.fact n := calc (Ico 1 n.succ).prod (λ x, x) = (range n).prod nat.succ : eq.symm (prod_bij (λ x _, nat.succ x) (λ a h₁, by simp [*, nat.lt_succ_iff, nat.succ_le_iff] at *) (by simp) (λ _ _ _ _, nat.succ_inj) (λ b h, have b.pred.succ = b, from nat.succ_pred_eq_of_pos $ by simp [nat.pos_iff_ne_zero, nat.succ_le_iff] at *; tauto, ⟨nat.pred b, mem_range.2 $ nat.lt_of_succ_lt_succ (by simp [*] at *), this.symm⟩)) ... = nat.fact n : by induction n; simp [*, range_succ] end finset namespace finset section gauss_sum /-- Gauss' summation formula -/ lemma sum_range_id_mul_two : ∀(n : ℕ), (finset.range n).sum (λi, i) * 2 = n * (n - 1) | 0 := rfl | 1 := rfl | ((n + 1) + 1) := begin rw [sum_range_succ, add_mul, sum_range_id_mul_two (n + 1), mul_comm, two_mul, nat.add_sub_cancel, nat.add_sub_cancel, mul_comm _ n], simp only [add_mul, one_mul, add_comm, add_assoc, add_left_comm] end /-- Gauss' summation formula -/ lemma sum_range_id (n : ℕ) : (finset.range n).sum (λi, i) = (n * (n - 1)) / 2 := by rw [← sum_range_id_mul_two n, nat.mul_div_cancel]; exact dec_trivial end gauss_sum lemma card_eq_sum_ones (s : finset α) : s.card = s.sum (λ _, 1) := by simp end finset section group open list variables [group α] [group β] @[to_additive] theorem is_group_hom.map_prod (f : α → β) [is_group_hom f] (l : list α) : f (prod l) = prod (map f l) := by induction l; simp only [*, is_mul_hom.map_mul f, is_group_hom.map_one f, prod_nil, prod_cons, map] theorem is_group_anti_hom.map_prod (f : α → β) [is_group_anti_hom f] (l : list α) : f (prod l) = prod (map f (reverse l)) := by induction l with hd tl ih; [exact is_group_anti_hom.map_one f, simp only [prod_cons, is_group_anti_hom.map_mul f, ih, reverse_cons, map_append, prod_append, map_singleton, prod_cons, prod_nil, mul_one]] theorem inv_prod : ∀ l : list α, (prod l)⁻¹ = prod (map (λ x, x⁻¹) (reverse l)) := λ l, @is_group_anti_hom.map_prod _ _ _ _ _ inv_is_group_anti_hom l -- TODO there is probably a cleaner proof of this end group section comm_group variables [comm_group α] [comm_group β] (f : α → β) [is_group_hom f] @[to_additive] lemma is_group_hom.map_multiset_prod (m : multiset α) : f m.prod = (m.map f).prod := quotient.induction_on m $ assume l, by simp [is_group_hom.map_prod f l] @[to_additive] lemma is_group_hom.map_finset_prod (g : γ → α) (s : finset γ) : f (s.prod g) = s.prod (f ∘ g) := show f (s.val.map g).prod = (s.val.map (f ∘ g)).prod, by rw [is_group_hom.map_multiset_prod f]; simp end comm_group @[to_additive is_add_group_hom_finset_sum] lemma is_group_hom_finset_prod {α β γ} [group α] [comm_group β] (s : finset γ) (f : γ → α → β) [∀c, is_group_hom (f c)] : is_group_hom (λa, s.prod (λc, f c a)) := { map_mul := assume a b, by simp only [λc, is_mul_hom.map_mul (f c), finset.prod_mul_distrib] } attribute [instance] is_group_hom_finset_prod is_add_group_hom_finset_sum namespace multiset variables [decidable_eq α] @[simp] lemma to_finset_sum_count_eq (s : multiset α) : s.to_finset.sum (λa, s.count a) = s.card := multiset.induction_on s rfl (assume a s ih, calc (to_finset (a :: s)).sum (λx, count x (a :: s)) = (to_finset (a :: s)).sum (λx, (if x = a then 1 else 0) + count x s) : finset.sum_congr rfl $ λ _ _, by split_ifs; [simp only [h, count_cons_self, nat.one_add], simp only [count_cons_of_ne h, zero_add]] ... = card (a :: s) : begin by_cases a ∈ s.to_finset, { have : (to_finset s).sum (λx, ite (x = a) 1 0) = (finset.singleton a).sum (λx, ite (x = a) 1 0), { apply (finset.sum_subset _ _).symm, { intros _ H, rwa mem_singleton.1 H }, { exact λ _ _ H, if_neg (mt finset.mem_singleton.2 H) } }, rw [to_finset_cons, finset.insert_eq_of_mem h, finset.sum_add_distrib, ih, this, finset.sum_singleton, if_pos rfl, add_comm, card_cons] }, { have ha : a ∉ s, by rwa mem_to_finset at h, have : (to_finset s).sum (λx, ite (x = a) 1 0) = (to_finset s).sum (λx, 0), from finset.sum_congr rfl (λ x hx, if_neg $ by rintro rfl; cc), rw [to_finset_cons, finset.sum_insert h, if_pos rfl, finset.sum_add_distrib, this, finset.sum_const_zero, ih, count_eq_zero_of_not_mem ha, zero_add, add_comm, card_cons] } end) end multiset namespace with_top open finset variables [decidable_eq α] /-- sum of finte numbers is still finite -/ lemma sum_lt_top [ordered_comm_monoid β] {s : finset α} {f : α → with_top β} : (∀a∈s, f a < ⊤) → s.sum f < ⊤ := finset.induction_on s (by { intro h, rw sum_empty, exact coe_lt_top _ }) (λa s ha ih h, begin rw [sum_insert ha, add_lt_top], split, { apply h, apply mem_insert_self }, { apply ih, intros a ha, apply h, apply mem_insert_of_mem ha } end) /-- sum of finte numbers is still finite -/ lemma sum_lt_top_iff [canonically_ordered_monoid β] {s : finset α} {f : α → with_top β} : s.sum f < ⊤ ↔ (∀a∈s, f a < ⊤) := iff.intro (λh a ha, lt_of_le_of_lt (single_le_sum (λa ha, zero_le _) ha) h) sum_lt_top end with_top
11ca7f1bb632fbbbfb0fe27a6ba82b3ecbc688c1
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/fintype.lean
f5f0a5e97256f05291c2e378d1af930139587221
[ "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
585
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 data.fintype.units /-! # Some facts about finite rings > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ open_locale classical lemma card_units_lt (M₀ : Type*) [monoid_with_zero M₀] [nontrivial M₀] [fintype M₀] : fintype.card M₀ˣ < fintype.card M₀ := fintype.card_lt_of_injective_of_not_mem (coe : M₀ˣ → M₀) units.ext not_is_unit_zero
3c0f91dfe0c63b709515e52cc80fcc7fe83cb6c2
76c77df8a58af24dbf1d75c7012076a42244d728
/tutorial_src/exercises/05_sequence_limits.lean
2facb5942f4f68975392220a0df1a8eddbcc43f3
[]
no_license
kris-brown/theorem_proving_in_lean
7a7a584ba2c657a35335dc895d49d991c997a0c9
774460c21bf857daff158210741bd88d1c8323cd
refs/heads/master
1,668,278,123,743
1,593,445,161,000
1,593,445,161,000
265,748,924
0
1
null
null
null
null
UTF-8
Lean
false
false
6,420
lean
import data.real.basic import algebra.pi_instances import tuto_lib notation `|`x`|` := abs x /- In this file we manipulate the elementary definition of limits of sequences of real numbers. mathlib has a much more general definition of limits, but here we want to practice using the logical operators and relations covered in the previous files. A sequence u is a function from ℕ to ℝ, hence Lean says u : ℕ → ℝ The definition we'll be using is: -- Definition of « u tends to l » def seq_limit (u : ℕ → ℝ) (l : ℝ) : Prop := ∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - l| ≤ ε Note the use of `∀ ε > 0, ...` which is an abbreviation of `∀ ε, ε > 0 → ... ` In particular, a statement like `h : ∀ ε > 0, ...` can be specialized to a given ε₀ by `specialize h ε₀ hε₀` where hε₀ is a proof of ε₀ > 0. Also recall that, wherever Lean expects some proof term, we can start a tactic mode proof using the keyword `by` (followed by curly braces if you need more than one tactic invocation). For instance, if the local context contains: δ : ℝ δ_pos : δ > 0 h : ∀ ε > 0, ... then we can specialize h to the real number δ/2 using: `specialize h (δ/2) (by linarith)` where `by linarith` will provide the proof of `δ/2 > 0` expected by Lean. We'll take this opportunity to use two new tactics: `norm_num` will perform numerical normalization on the goal and `norm_num at h` will do the same in assumption `h`. This will get rid of trivial calculations on numbers, like replacing |l - l| by zero in the next exercise. `congr'` will try to prove equalities between applications of functions by recursively proving the arguments are the same. For instance, if the goal is `f x + g y = f z + g t` then congr will replace it by two goals: `x = z` and `y = t`. You can limit the recursion depth by specifying a natural number after `congr'`. For instance, in the above example, `congr' 1` will give new goals `f x = f z` and `g y = g t`, which only inspect arguments of the addition and not deeper. -/ variables (u v w : ℕ → ℝ) (l l' : ℝ) -- If u is constant with value l then u tends to l -- 0033 example : (∀ n, u n = l) → seq_limit u l := begin intros h e epos, have t : (∀ n : ℕ, n ≥ 0 -> |u n - l| ≤ e), by {intros n hn, have h1 : -0 ≤ (0:ℝ) ∧ (0:ℝ) ≤ 0, from ⟨by linarith, by linarith⟩, have h2 : |(0:ℝ)| ≤ 0, from abs_le.mpr h1, calc | u n - l | = | l - l | : by rw (h n) ... = |(0:ℝ)| : by rw (sub_self l) ... ≤ 0 : by exact h2 ... ≤ e : by linarith [epos]}, exact ⟨0, t⟩ end /- When dealing with absolute values, we'll use lemmas: abs_le (x y : ℝ) : |x| ≤ y ↔ -y ≤ x ∧ x ≤ y abs_add (x y : ℝ) : |x + y| ≤ |x| + |y| abs_sub (x y : ℝ) : |x - y| = |y - x| You should probably write them down on a sheet of paper that you keep at hand since they are used in many exercises. -/ -- Assume l > 0. Then u tends to l implies u n ≥ l/2 for large enough n -- 0034 example (hl : l > 0) : seq_limit u l → ∃ N, ∀ n ≥ N, u n ≥ l/2 := begin intros h, unfold seq_limit at h, specialize h (l/2) (by linarith [hl]), rcases h with ⟨t,rfl⟩, exact h end /- When dealing with max, you can use ge_max_iff (p q r) : r ≥ max p q ↔ r ≥ p ∧ r ≥ q le_max_left p q : p ≤ max p q le_max_right p q : q ≤ max p q You should probably add them to the sheet of paper where you wrote the `abs` lemmas since they are used in many exercises. Let's see an example. -/ -- If u tends to l and v tends l' then u+v tends to l+l' example (hu : seq_limit u l) (hv : seq_limit v l') : seq_limit (u + v) (l + l') := begin intros ε ε_pos, cases hu (ε/2) (by linarith) with N₁ hN₁, cases hv (ε/2) (by linarith) with N₂ hN₂, use max N₁ N₂, intros n hn, cases ge_max_iff.mp hn with hn₁ hn₂, have fact₁ : |u n - l| ≤ ε/2, from hN₁ n (by linarith), -- note the use of `from`. -- This is an alias for `exact`, -- but reads nicer in this context have fact₂ : |v n - l'| ≤ ε/2, from hN₂ n (by linarith), calc |(u + v) n - (l + l')| = |u n + v n - (l + l')| : rfl ... = |(u n - l) + (v n - l')| : by congr' 1 ; ring ... ≤ |u n - l| + |v n - l'| : by apply abs_add ... ≤ ε : by linarith, end /- In the above proof, we used `have` to prepare facts for `linarith` consumption in the last line. Since we have direct proof terms for them, we can feed them directly to `linarith` as in the next proof of the same statement. Another variation we introduce is rewriting using `ge_max_iff` and letting `linarith` handle the conjunction, instead of creating two new assumptions. -/ example (hu : seq_limit u l) (hv : seq_limit v l') : seq_limit (u + v) (l + l') := begin intros ε ε_pos, cases hu (ε/2) (by linarith) with N₁ hN₁, cases hv (ε/2) (by linarith) with N₂ hN₂, use max N₁ N₂, intros n hn, rw ge_max_iff at hn, calc |(u + v) n - (l + l')| = |u n + v n - (l + l')| : rfl ... = |(u n - l) + (v n - l')| : by congr' 1 ; ring ... ≤ |u n - l| + |v n - l'| : by apply abs_add ... ≤ ε : by linarith [hN₁ n (by sorry), hN₂ n (by sorry)], end /- Let's do something similar: the squeezing theorem. -/ -- 0035 example (hu : seq_limit u l) (hw : seq_limit w l) (h : ∀ n, u n ≤ v n) (h' : ∀ n, v n ≤ w n) : seq_limit v l := begin sorry end /- What about < ε? -/ -- 0036 example (u l) : seq_limit u l ↔ ∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - l| < ε := begin sorry end /- In the next exercise, we'll use eq_of_abs_sub_le_all (x y : ℝ) : (∀ ε > 0, |x - y| ≤ ε) → x = y -/ -- A sequence admits at most one limit -- 0037 example : seq_limit u l → seq_limit u l' → l = l' := begin sorry end /- Let's now practice deciphering definitions before proving. -/ def non_decreasing (u : ℕ → ℝ) := ∀ n m, n ≤ m → u n ≤ u m def is_seq_sup (M : ℝ) (u : ℕ → ℝ) := (∀ n, u n ≤ M) ∧ ∀ ε > 0, ∃ n₀, u n₀ ≥ M - ε -- 0038 example (M : ℝ) (h : is_seq_sup M u) (h' : non_decreasing u) : seq_limit u M := begin sorry end
e8018cd2e152b662c73e8af1dd7f64227d4aa3ac
2bafba05c98c1107866b39609d15e849a4ca2bb8
/src/week_1/kb_solutions/Part_B_sets_solutions.lean
ad48e28335ead1dbfdc99ac7f2c2b4651ef63707
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/formalising-mathematics
b54c83c94b5c315024ff09997fcd6b303892a749
7cf1d51c27e2038d2804561d63c74711924044a1
refs/heads/master
1,651,267,046,302
1,638,888,459,000
1,638,888,459,000
331,592,375
284
24
Apache-2.0
1,669,593,705,000
1,611,224,849,000
Lean
UTF-8
Lean
false
false
4,721
lean
import tactic -- Let `Ω` be a "big underlying set" and let `X` and `Y` and `Z` be subsets variables (Ω : Type) (X Y Z : set Ω) (a b c x y z : Ω) namespace xena /-! # subsets Let's think about `X ⊆ Y`. Typeset `⊆` with `\sub` or `\ss` -/ -- `X ⊆ Y` is the same as `∀ a, a ∈ X → a ∈ Y` , by definition. lemma subset_def : X ⊆ Y ↔ ∀ a, a ∈ X → a ∈ Y := begin -- true by definition refl end lemma subset_refl : X ⊆ X := begin rw subset_def, -- goal now "for all a, ..." and `intro` makes progress with this. intros a ha, exact ha, end lemma subset_trans (hXY : X ⊆ Y) (hYZ : Y ⊆ Z) : X ⊆ Z := begin rw subset_def at *, intros a ha, -- ⊢ a ∈ Z -- hYZ says: a ∈ Y → a ∈ Z. So... apply hYZ, -- ⊢ a ∈ Y apply hXY, exact ha, end /-! # Equality of sets Two sets are equal if and only if they have the same elements. The name of this theorem is `set.ext_iff`. -/ example : X = Y ↔ (∀ a, a ∈ X ↔ a ∈ Y) := begin exact set.ext_iff end -- In practice, you often have a goal `⊢ X = Y` and you want to reduce -- it to `a ∈ X ↔ a ∈ Y` for an arbitary `a : Ω`. This can be done with -- the `ext` tactic. lemma subset.antisymm (hXY : X ⊆ Y) (hYX : Y ⊆ X) : X = Y := begin ext a, split, { apply hXY }, { apply hYX } end /-! ### Unions and intersections Type `\cup` or `\un` for `∪`, and `\cap` or `\i` for `∩` -/ lemma mem_union_iff : a ∈ X ∪ Y ↔ a ∈ X ∨ a ∈ Y := begin -- true by definition refl, end lemma mem_inter_iff : a ∈ X ∩ Y ↔ a ∈ X ∧ a ∈ Y := begin -- true by definition refl, end -- You can rewrite with those lemmas above if you're not comfortable with -- assuming they're true by definition. -- union lemmas -- if you want to use `mem_union_iff` you should start with `ext` lemma union_self : X ∪ X = X := begin ext a, rw mem_union_iff, split, { intro h, cases h with ha ha; exact ha }, { intro ha, left, exact ha } end lemma subset_union_left : X ⊆ X ∪ Y := begin rw subset_def, intros a haX, rw mem_union_iff, left, assumption, end lemma subset_union_right : Y ⊆ X ∪ Y := begin -- don't need to rewrite subset_def or mem_union_iff -- as they're both true by definition intros a haY, right, assumption, end lemma union_subset_iff : X ∪ Y ⊆ Z ↔ X ⊆ Z ∧ Y ⊆ Z := begin -- NB Lean's simplifier `simp` solves this (and many others) -- as do the tactics `finish` and `tidy` -- We will talk a bit more about `simp` in week 2. split, { intro h, split, { intros a haX, apply h, left, assumption }, { intros a haY, apply h, right, assumption }, }, { rintros ⟨hXZ, hYZ⟩ a (haX | haY), { exact hXZ haX }, { exact hYZ haY } } end variable (W : set Ω) lemma union_subset_union (hWX : W ⊆ X) (hYZ : Y ⊆ Z) : W ∪ Y ⊆ X ∪ Z := begin rintros a (haW | haY), { left, exact hWX haW }, { right, exact hYZ haY } end lemma union_subset_union_left (hXY : X ⊆ Y) : X ∪ Z ⊆ Y ∪ Z := begin rintros a (haX | haZ), { left, exact hXY haX }, { right, assumption } end -- etc etc -- intersection lemmas lemma inter_subset_left : X ∩ Y ⊆ X := begin rintro a ⟨haX, haY⟩, assumption, end -- don't forget `ext` to make progress with equalities of sets lemma inter_self : X ∩ X = X := begin ext a, split, { rintro ⟨ha, -⟩, assumption }, { intro ha, exact ⟨ha, ha⟩ } end lemma inter_comm : X ∩ Y = Y ∩ X := begin ext a, split; { rintro ⟨h1, h2⟩, exact ⟨h2, h1⟩ } end lemma inter_assoc : X ∩ (Y ∩ Z) = (X ∩ Y) ∩ Z := begin -- finish, -- tidy works too, ext a, split, { rintro ⟨hx, hy, hz⟩, -- try `rintro?` to see the syntax exact ⟨⟨hx, hy⟩, hz⟩ }, { rintro ⟨⟨hx, hy⟩, hz⟩, exact ⟨hx, hy, hz⟩ }, end /-! ### Forall and exists -/ lemma not_exists_iff_forall_not : ¬ (∃ a, a ∈ X) ↔ ∀ b, ¬ (b ∈ X) := begin split, { intros h b hb, apply h, use b, assumption }, { rintro h ⟨a, ha⟩, exact h a ha }, end example : ¬ (∀ a, a ∈ X) ↔ ∃ b, ¬ (b ∈ X) := begin split, { -- you need classical logic to do this part -- `contrapose!` is a way of making progress -- `finish` does it completely. -- We use `by_contra`, which -- turns a goal `⊢ P` into -- a hypothesis `h : ¬ P` and -- a goal of `false` intro h, by_contra hnX, apply h, intro a, by_contra hXa, apply hnX, use a, }, { intro h, cases h with b hb, intro h, apply hb, apply h } end end xena
70bde8b58b37c2dc516d7a1dd27b210d5618bc84
d9ed0fce1c218297bcba93e046cb4e79c83c3af8
/library/init/logic.lean
2f6c965b3b288fea62ea4d936b4324ddd6edee16
[ "Apache-2.0" ]
permissive
leodemoura/lean_clone
005c63aa892a6492f2d4741ee3c2cb07a6be9d7f
cc077554b584d39bab55c360bc12a6fe7957afe6
refs/heads/master
1,610,506,475,484
1,482,348,354,000
1,482,348,543,000
77,091,586
0
0
null
null
null
null
UTF-8
Lean
false
false
36,371
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad, Floris van Doorn -/ prelude import init.core universe variables u v w @[reducible] def id {α : Type u} (a : α) : α := a def flip {α : Type u} {β : Type v} {φ : Type w} (f : α → β → φ) : β → α → φ := λ b a, f a b /- implication -/ def implies (a b : Prop) := a → b @[trans] lemma implies.trans {p q r : Prop} (h₁ : implies p q) (h₂ : implies q r) : implies p r := assume hp, h₂ (h₁ hp) def trivial : true := ⟨⟩ @[inline] def absurd {a : Prop} {b : Type v} (h₁ : a) (h₂ : ¬a) : b := false.rec b (h₂ h₁) lemma not.intro {a : Prop} (h : a → false) : ¬ a := h lemma mt {a b : Prop} (h₁ : a → b) (h₂ : ¬b) : ¬a := assume ha : a, absurd (h₁ ha) h₂ def implies.resolve {a b : Prop} (h : a → b) (nb : ¬ b) : ¬ a := assume ha, nb (h ha) /- not -/ lemma not_false : ¬false := assume h : false, h def non_contradictory (a : Prop) : Prop := ¬¬a lemma non_contradictory_intro {a : Prop} (ha : a) : ¬¬a := assume hna : ¬a, absurd ha hna /- false -/ lemma false.elim {c : Prop} (h : false) : c := false.rec c h /- eq -/ -- proof irrelevance is built in lemma proof_irrel {a : Prop} (h₁ h₂ : a) : h₁ = h₂ := rfl @[simp] lemma id.def {α : Type u} (a : α) : id a = a := rfl -- Remark: we provide the universe levels explicitly to make sure `eq.drec` has the same type of `eq.rec` in the hoTT library attribute [elab_as_eliminator] protected lemma {u₁ u₂} eq.drec {α : Type u₂} {a : α} {φ : Π (x : α), a = x → Type u₁} (h₁ : φ a (eq.refl a)) {b : α} (h₂ : a = b) : φ b h₂ := eq.rec (λ h₂ : a = a, show φ a h₂, from h₁) h₂ h₂ attribute [elab_as_eliminator] protected lemma drec_on {α : Type u} {a : α} {φ : Π (x : α), a = x → Type v} {b : α} (h₂ : a = b) (h₁ : φ a (eq.refl a)) : φ b h₂ := eq.drec h₁ h₂ lemma eq.mp {α β : Type u} : (α = β) → α → β := eq.rec_on lemma eq.mpr {α β : Type u} : (α = β) → β → α := λ h₁ h₂, eq.rec_on (eq.symm h₁) h₂ lemma eq.substr {α : Type u} {p : α → Prop} {a b : α} (h₁ : b = a) : p a → p b := eq.subst (eq.symm h₁) lemma congr {α : Type u} {β : Type v} {f₁ f₂ : α → β} {a₁ a₂ : α} (h₁ : f₁ = f₂) (h₂ : a₁ = a₂) : f₁ a₁ = f₂ a₂ := eq.subst h₁ (eq.subst h₂ rfl) lemma congr_fun {α : Type u} {β : α → Type v} {f g : Π x, β x} (h : f = g) (a : α) : f a = g a := eq.subst h (eq.refl (f a)) lemma congr_arg {α : Type u} {β : Type v} {a₁ a₂ : α} (f : α → β) : a₁ = a₂ → f a₁ = f a₂ := congr rfl lemma trans_rel_left {α : Type u} {a b c : α} (r : α → α → Prop) (h₁ : r a b) (h₂ : b = c) : r a c := h₂ ▸ h₁ lemma trans_rel_right {α : Type u} {a b c : α} (r : α → α → Prop) (h₁ : a = b) (h₂ : r b c) : r a c := h₁^.symm ▸ h₂ lemma of_eq_true {p : Prop} (h : p = true) : p := h^.symm ▸ trivial lemma not_of_eq_false {p : Prop} (h : p = false) : ¬p := assume hp, h ▸ hp @[inline] def cast {α β : Type u} (h : α = β) (a : α) : β := eq.rec a h lemma cast_proof_irrel {α β : Type u} (h₁ h₂ : α = β) (a : α) : cast h₁ a = cast h₂ a := rfl lemma cast_eq {α : Type u} (h : α = α) (a : α) : cast h a = a := rfl /- ne -/ @[reducible] def ne {α : Type u} (a b : α) := ¬(a = b) notation a ≠ b := ne a b @[simp] lemma ne.def {α : Type u} (a b : α) : a ≠ b = ¬ (a = b) := rfl namespace ne variable {α : Type u} variables {a b : α} lemma intro (h : a = b → false) : a ≠ b := h lemma elim (h : a ≠ b) : a = b → false := h lemma irrefl (h : a ≠ a) : false := h rfl lemma symm (h : a ≠ b) : b ≠ a := assume (h₁ : b = a), h (h₁^.symm) end ne lemma false_of_ne {α : Type u} {a : α} : a ≠ a → false := ne.irrefl section variables {p : Prop} lemma ne_false_of_self : p → p ≠ false := assume (hp : p) (heq : p = false), heq ▸ hp lemma ne_true_of_not : ¬p → p ≠ true := assume (hnp : ¬p) (heq : p = true), (heq ▸ hnp) trivial lemma true_ne_false : ¬true = false := ne_false_of_self trivial end attribute [refl] heq.refl section variables {α β φ : Type u} {a a' : α} {b b' : β} {c : φ} lemma eq_of_heq (h : a == a') : a = a' := have ∀ (α' : Type u) (a' : α') (h₁ : @heq α a α' a') (h₂ : α = α'), (eq.rec_on h₂ a : α') = a', from λ (α' : Type u) (a' : α') (h₁ : @heq α a α' a'), heq.rec_on h₁ (λ h₂ : α = α, rfl), show (eq.rec_on (eq.refl α) a : α) = a', from this α a' h (eq.refl α) lemma heq.elim {α : Type u} {a : α} {p : α → Type v} {b : α} (h₁ : a == b) : p a → p b := eq.rec_on (eq_of_heq h₁) lemma heq.subst {p : ∀ T : Type u, T → Prop} : a == b → p α a → p β b := heq.rec_on @[symm] lemma heq.symm (h : a == b) : b == a := heq.rec_on h (heq.refl a) lemma heq_of_eq (h : a = a') : a == a' := eq.subst h (heq.refl a) @[trans] lemma heq.trans (h₁ : a == b) (h₂ : b == c) : a == c := heq.subst h₂ h₁ @[trans] lemma heq_of_heq_of_eq (h₁ : a == b) (h₂ : b = b') : a == b' := heq.trans h₁ (heq_of_eq h₂) @[trans] lemma heq_of_eq_of_heq (h₁ : a = a') (h₂ : a' == b) : a == b := heq.trans (heq_of_eq h₁) h₂ def type_eq_of_heq (h : a == b) : α = β := heq.rec_on h (eq.refl α) end lemma eq_rec_heq {α : Type u} {φ : α → Type v} : ∀ {a a' : α} (h : a = a') (p : φ a), (eq.rec_on h p : φ a') == p | a .a rfl p := heq.refl p lemma heq_of_eq_rec_left {α : Type u} {φ : α → Type v} : ∀ {a a' : α} {p₁ : φ a} {p₂ : φ a'} (e : a = a') (h₂ : (eq.rec_on e p₁ : φ a') = p₂), p₁ == p₂ | a .a p₁ p₂ (eq.refl .a) h := eq.rec_on h (heq.refl p₁) lemma heq_of_eq_rec_right {α : Type u} {φ : α → Type v} : ∀ {a a' : α} {p₁ : φ a} {p₂ : φ a'} (e : a' = a) (h₂ : p₁ = eq.rec_on e p₂), p₁ == p₂ | a .a p₁ p₂ (eq.refl .a) h := have p₁ = p₂, from h, this ▸ heq.refl p₁ lemma of_heq_true {a : Prop} (h : a == true) : a := of_eq_true (eq_of_heq h) lemma eq_rec_compose : ∀ {α β φ : Type u} (p₁ : β = φ) (p₂ : α = β) (a : α), (eq.rec_on p₁ (eq.rec_on p₂ a : β) : φ) = eq.rec_on (eq.trans p₂ p₁) a | α .α .α (eq.refl .α) (eq.refl .α) a := rfl lemma eq_rec_eq_eq_rec : ∀ {α₁ α₂ : Type u} {p : α₁ = α₂} {a₁ : α₁} {a₂ : α₂}, (eq.rec_on p a₁ : α₂) = a₂ → a₁ = eq.rec_on (eq.symm p) a₂ | α .α rfl a .a rfl := rfl lemma eq_rec_of_heq_left : ∀ {α₁ α₂ : Type u} {a₁ : α₁} {a₂ : α₂} (h : a₁ == a₂), (eq.rec_on (type_eq_of_heq h) a₁ : α₂) = a₂ | α .α a .a (heq.refl .a) := rfl lemma eq_rec_of_heq_right {α₁ α₂ : Type u} {a₁ : α₁} {a₂ : α₂} (h : a₁ == a₂) : a₁ = eq.rec_on (eq.symm (type_eq_of_heq h)) a₂ := eq_rec_eq_eq_rec (eq_rec_of_heq_left h) lemma cast_heq : ∀ {α β : Type u} (h : α = β) (a : α), cast h a == a | α .α (eq.refl .α) a := heq.refl a /- and -/ notation a /\ b := and a b notation a ∧ b := and a b variables {a b c d : Prop} lemma and.elim (h₁ : a ∧ b) (h₂ : a → b → c) : c := and.rec h₂ h₁ lemma and.swap : a ∧ b → b ∧ a := assume ⟨ha, hb⟩, ⟨hb, ha⟩ def and.symm := @and.swap /- or -/ notation a \/ b := or a b notation a ∨ b := or a b namespace or lemma elim (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → c) : c := or.rec h₂ h₃ h₁ end or lemma non_contradictory_em (a : Prop) : ¬¬(a ∨ ¬a) := assume not_em : ¬(a ∨ ¬a), have neg_a : ¬a, from assume pos_a : a, absurd (or.inl pos_a) not_em, absurd (or.inr neg_a) not_em def not_not_em := non_contradictory_em lemma or.swap : a ∨ b → b ∨ a := or.rec or.inr or.inl def or.symm := @or.swap /- xor -/ def xor (a b : Prop) := (a ∧ ¬ b) ∨ (b ∧ ¬ a) /- iff -/ def iff (a b : Prop) := (a → b) ∧ (b → a) notation a <-> b := iff a b notation a ↔ b := iff a b lemma iff.intro : (a → b) → (b → a) → (a ↔ b) := and.intro lemma iff.elim : ((a → b) → (b → a) → c) → (a ↔ b) → c := and.rec attribute [recursor 5] iff.elim lemma iff.elim_left : (a ↔ b) → a → b := and.left def iff.mp := @iff.elim_left lemma iff.elim_right : (a ↔ b) → b → a := and.right def iff.mpr := @iff.elim_right attribute [refl] lemma iff.refl (a : Prop) : a ↔ a := iff.intro (assume h, h) (assume h, h) lemma iff.rfl {a : Prop} : a ↔ a := iff.refl a attribute [trans] lemma iff.trans (h₁ : a ↔ b) (h₂ : b ↔ c) : a ↔ c := iff.intro (assume ha, iff.mp h₂ (iff.mp h₁ ha)) (assume hc, iff.mpr h₁ (iff.mpr h₂ hc)) attribute [symm] lemma iff.symm (h : a ↔ b) : b ↔ a := iff.intro (iff.elim_right h) (iff.elim_left h) lemma iff.comm : (a ↔ b) ↔ (b ↔ a) := iff.intro iff.symm iff.symm lemma eq.to_iff {a b : Prop} (h : a = b) : a ↔ b := eq.rec_on h iff.rfl lemma not_iff_not_of_iff (h₁ : a ↔ b) : ¬a ↔ ¬b := iff.intro (assume (hna : ¬ a) (hb : b), hna (iff.elim_right h₁ hb)) (assume (hnb : ¬ b) (ha : a), hnb (iff.elim_left h₁ ha)) lemma of_iff_true (h : a ↔ true) : a := iff.mp (iff.symm h) trivial lemma not_of_iff_false : (a ↔ false) → ¬a := iff.mp lemma iff_true_intro (h : a) : a ↔ true := iff.intro (λ hl, trivial) (λ hr, h) lemma iff_false_intro (h : ¬a) : a ↔ false := iff.intro h (false.rec a) lemma not_non_contradictory_iff_absurd (a : Prop) : ¬¬¬a ↔ ¬a := iff.intro (λ (hl : ¬¬¬a) (ha : a), hl (non_contradictory_intro ha)) absurd def not_not_not_iff := not_non_contradictory_iff_absurd lemma imp_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a → b) ↔ (c → d) := iff.intro (λ hab hc, iff.mp h₂ (hab (iff.mpr h₁ hc))) (λ hcd ha, iff.mpr h₂ (hcd (iff.mp h₁ ha))) lemma imp_congr_ctx (h₁ : a ↔ c) (h₂ : c → (b ↔ d)) : (a → b) ↔ (c → d) := iff.intro (λ hab hc, have ha : a, from iff.mpr h₁ hc, have hb : b, from hab ha, iff.mp (h₂ hc) hb) (λ hcd ha, have hc : c, from iff.mp h₁ ha, have hd : d, from hcd hc, iff.mpr (h₂ hc) hd) lemma imp_congr_right (h : a → (b ↔ c)) : (a → b) ↔ (a → c) := iff.intro (take hab ha, iff.elim_left (h ha) (hab ha)) (take hab ha, iff.elim_right (h ha) (hab ha)) lemma not_not_intro (ha : a) : ¬¬a := assume hna : ¬a, hna ha lemma not_of_not_not_not (h : ¬¬¬a) : ¬a := λ ha, absurd (not_not_intro ha) h @[simp] lemma not_true : (¬ true) ↔ false := iff_false_intro (not_not_intro trivial) def not_true_iff := not_true @[simp] lemma not_false_iff : (¬ false) ↔ true := iff_true_intro not_false @[congr] lemma not_congr (h : a ↔ b) : ¬a ↔ ¬b := iff.intro (λ h₁ h₂, h₁ (iff.mpr h h₂)) (λ h₁ h₂, h₁ (iff.mp h h₂)) @[simp] lemma ne_self_iff_false {α : Type u} (a : α) : (not (a = a)) ↔ false := iff.intro false_of_ne false.elim @[simp] lemma eq_self_iff_true {α : Type u} (a : α) : (a = a) ↔ true := iff_true_intro rfl @[simp] lemma heq_self_iff_true {α : Type u} (a : α) : (a == a) ↔ true := iff_true_intro (heq.refl a) @[simp] lemma iff_not_self (a : Prop) : (a ↔ ¬a) ↔ false := iff_false_intro (λ h, have h' : ¬a, from (λ ha, (iff.mp h ha) ha), h' (iff.mpr h h')) @[simp] lemma not_iff_self (a : Prop) : (¬a ↔ a) ↔ false := iff_false_intro (λ h, have h' : ¬a, from (λ ha, (iff.mpr h ha) ha), h' (iff.mp h h')) @[simp] lemma true_iff_false : (true ↔ false) ↔ false := iff_false_intro (λ h, iff.mp h trivial) @[simp] lemma false_iff_true : (false ↔ true) ↔ false := iff_false_intro (λ h, iff.mpr h trivial) lemma false_of_true_iff_false : (true ↔ false) → false := assume h, iff.mp h trivial /- and simp rules -/ lemma and.imp (hac : a → c) (hbd : b → d) : a ∧ b → c ∧ d := assume ⟨ha, hb⟩, ⟨hac ha, hbd hb⟩ def and_implies := @and.imp @[congr] lemma and_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ∧ b) ↔ (c ∧ d) := iff.intro (and.imp (iff.mp h₁) (iff.mp h₂)) (and.imp (iff.mpr h₁) (iff.mpr h₂)) lemma and_congr_right (h : a → (b ↔ c)) : (a ∧ b) ↔ (a ∧ c) := iff.intro (assume ⟨ha, hb⟩, ⟨ha, iff.elim_left (h ha) hb⟩) (assume ⟨ha, hc⟩, ⟨ha, iff.elim_right (h ha) hc⟩) @[simp] lemma and.comm : a ∧ b ↔ b ∧ a := iff.intro and.swap and.swap lemma and_comm (a b : Prop) : a ∧ b ↔ b ∧ a := and.comm @[simp] lemma and.assoc : (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) := iff.intro (assume ⟨⟨ha, hb⟩, hc⟩, ⟨ha, ⟨hb, hc⟩⟩) (assume ⟨ha, ⟨hb, hc⟩⟩, ⟨⟨ha, hb⟩, hc⟩) lemma and_assoc (a b : Prop) : (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) := and.assoc @[simp] lemma and.left_comm : a ∧ (b ∧ c) ↔ b ∧ (a ∧ c) := iff.trans (iff.symm and.assoc) (iff.trans (and_congr and.comm (iff.refl c)) and.assoc) lemma and_iff_left {a b : Prop} (hb : b) : (a ∧ b) ↔ a := iff.intro and.left (λ ha, ⟨ha, hb⟩) lemma and_iff_right {a b : Prop} (ha : a) : (a ∧ b) ↔ b := iff.intro and.right (and.intro ha) @[simp] lemma and_true (a : Prop) : a ∧ true ↔ a := and_iff_left trivial @[simp] lemma true_and (a : Prop) : true ∧ a ↔ a := and_iff_right trivial @[simp] lemma and_false (a : Prop) : a ∧ false ↔ false := iff_false_intro and.right @[simp] lemma false_and (a : Prop) : false ∧ a ↔ false := iff_false_intro and.left @[simp] lemma not_and_self (a : Prop) : (¬a ∧ a) ↔ false := iff_false_intro (λ h, and.elim h (λ h₁ h₂, absurd h₂ h₁)) @[simp] lemma and_not_self (a : Prop) : (a ∧ ¬a) ↔ false := iff_false_intro (assume ⟨h₁, h₂⟩, absurd h₁ h₂) @[simp] lemma and_self (a : Prop) : a ∧ a ↔ a := iff.intro and.left (assume h, ⟨h, h⟩) /- or simp rules -/ lemma or.imp (h₂ : a → c) (h₃ : b → d) : a ∨ b → c ∨ d := or.rec (λ h, or.inl (h₂ h)) (λ h, or.inr (h₃ h)) lemma or.imp_left (h : a → b) : a ∨ c → b ∨ c := or.imp h id lemma or.imp_right (h : a → b) : c ∨ a → c ∨ b := or.imp id h @[congr] lemma or_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ∨ b) ↔ (c ∨ d) := iff.intro (or.imp (iff.mp h₁) (iff.mp h₂)) (or.imp (iff.mpr h₁) (iff.mpr h₂)) @[simp] lemma or.comm : a ∨ b ↔ b ∨ a := iff.intro or.swap or.swap lemma or_comm (a b : Prop) : a ∨ b ↔ b ∨ a := or.comm @[simp] lemma or.assoc : (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) := iff.intro (or.rec (or.imp_right or.inl) (λ h, or.inr (or.inr h))) (or.rec (λ h, or.inl (or.inl h)) (or.imp_left or.inr)) lemma or_assoc (a b : Prop) : (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) := or.assoc @[simp] lemma or.left_comm : a ∨ (b ∨ c) ↔ b ∨ (a ∨ c) := iff.trans (iff.symm or.assoc) (iff.trans (or_congr or.comm (iff.refl c)) or.assoc) @[simp] lemma or_true (a : Prop) : a ∨ true ↔ true := iff_true_intro (or.inr trivial) @[simp] lemma true_or (a : Prop) : true ∨ a ↔ true := iff_true_intro (or.inl trivial) @[simp] lemma or_false (a : Prop) : a ∨ false ↔ a := iff.intro (or.rec id false.elim) or.inl @[simp] lemma false_or (a : Prop) : false ∨ a ↔ a := iff.trans or.comm (or_false a) @[simp] lemma or_self (a : Prop) : a ∨ a ↔ a := iff.intro (or.rec id id) or.inl lemma not_or {a b : Prop} : ¬ a → ¬ b → ¬ (a ∨ b) | hna hnb (or.inl ha) := absurd ha hna | hna hnb (or.inr hb) := absurd hb hnb /- or resolution rulse -/ def or.resolve_left {a b : Prop} (h : a ∨ b) (na : ¬ a) : b := or.elim h (λ ha, absurd ha na) id def or.neg_resolve_left {a b : Prop} (h : ¬ a ∨ b) (ha : a) : b := or.elim h (λ na, absurd ha na) id def or.resolve_right {a b : Prop} (h : a ∨ b) (nb : ¬ b) : a := or.elim h id (λ hb, absurd hb nb) def or.neg_resolve_right {a b : Prop} (h : a ∨ ¬ b) (hb : b) : a := or.elim h id (λ nb, absurd hb nb) /- iff simp rules -/ @[simp] lemma iff_true (a : Prop) : (a ↔ true) ↔ a := iff.intro (assume h, iff.mpr h trivial) iff_true_intro @[simp] lemma true_iff (a : Prop) : (true ↔ a) ↔ a := iff.trans iff.comm (iff_true a) @[simp] lemma iff_false (a : Prop) : (a ↔ false) ↔ ¬ a := iff.intro and.left iff_false_intro @[simp] lemma false_iff (a : Prop) : (false ↔ a) ↔ ¬ a := iff.trans iff.comm (iff_false a) @[simp] lemma iff_self (a : Prop) : (a ↔ a) ↔ true := iff_true_intro iff.rfl @[congr] lemma iff_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ↔ b) ↔ (c ↔ d) := and_congr (imp_congr h₁ h₂) (imp_congr h₂ h₁) /- implies simp rule -/ @[simp] lemma implies_true_iff (a : Prop) : (a → true) ↔ true := iff.intro (λ h, trivial) (λ ha h, trivial) @[simp] lemma false_implies_iff (a : Prop) : (false → a) ↔ true := iff.intro (λ h, trivial) (λ ha h, false.elim h) /- exists -/ inductive Exists {α : Type u} (p : α → Prop) : Prop | intro : ∀ (a : α), p a → Exists attribute [intro] Exists.intro def exists.intro := @Exists.intro notation `exists` binders `, ` r:(scoped P, Exists P) := r notation `∃` binders `, ` r:(scoped P, Exists P) := r lemma exists.elim {α : Type u} {p : α → Prop} {b : Prop} (h₁ : ∃ x, p x) (h₂ : ∀ (a : α), p a → b) : b := Exists.rec h₂ h₁ /- exists unique -/ def exists_unique {α : Type u} (p : α → Prop) := ∃ x, p x ∧ ∀ y, p y → y = x notation `∃!` binders `, ` r:(scoped P, exists_unique P) := r attribute [intro] lemma exists_unique.intro {α : Type u} {p : α → Prop} (w : α) (h₁ : p w) (h₂ : ∀ y, p y → y = w) : ∃! x, p x := exists.intro w ⟨h₁, h₂⟩ attribute [recursor 4] lemma exists_unique.elim {α : Type u} {p : α → Prop} {b : Prop} (h₂ : ∃! x, p x) (h₁ : ∀ x, p x → (∀ y, p y → y = x) → b) : b := exists.elim h₂ (λ w hw, h₁ w (and.left hw) (and.right hw)) lemma exists_unique_of_exists_of_unique {α : Type u} {p : α → Prop} (hex : ∃ x, p x) (hunique : ∀ y₁ y₂, p y₁ → p y₂ → y₁ = y₂) : ∃! x, p x := exists.elim hex (λ x px, exists_unique.intro x px (take y, suppose p y, hunique y x this px)) lemma exists_of_exists_unique {α : Type u} {p : α → Prop} (h : ∃! x, p x) : ∃ x, p x := exists.elim h (λ x hx, ⟨x, and.left hx⟩) lemma unique_of_exists_unique {α : Type u} {p : α → Prop} (h : ∃! x, p x) {y₁ y₂ : α} (py₁ : p y₁) (py₂ : p y₂) : y₁ = y₂ := exists_unique.elim h (take x, suppose p x, assume unique : ∀ y, p y → y = x, show y₁ = y₂, from eq.trans (unique _ py₁) (eq.symm (unique _ py₂))) /- exists, forall, exists unique congruences -/ @[congr] lemma forall_congr {α : Type u} {p q : α → Prop} (h : ∀ a, (p a ↔ q a)) : (∀ a, p a) ↔ ∀ a, q a := iff.intro (λ p a, iff.mp (h a) (p a)) (λ q a, iff.mpr (h a) (q a)) lemma exists_imp_exists {α : Type u} {p q : α → Prop} (h : ∀ a, (p a → q a)) (p : ∃ a, p a) : ∃ a, q a := exists.elim p (λ a hp, ⟨a, h a hp⟩) @[congr] lemma exists_congr {α : Type u} {p q : α → Prop} (h : ∀ a, (p a ↔ q a)) : (Exists p) ↔ ∃ a, q a := iff.intro (exists_imp_exists (λ a, iff.mp (h a))) (exists_imp_exists (λ a, iff.mpr (h a))) @[congr] lemma exists_unique_congr {α : Type u} {p₁ p₂ : α → Prop} (h : ∀ x, p₁ x ↔ p₂ x) : (exists_unique p₁) ↔ (∃! x, p₂ x) := -- exists_congr (λ x, and_congr (h x) (forall_congr (λ y, imp_congr (h y) iff.rfl))) /- decidable -/ def decidable.to_bool (p : Prop) [h : decidable p] : bool := decidable.cases_on h (λ h₁, bool.ff) (λ h₂, bool.tt) export decidable (is_true is_false to_bool) instance decidable.true : decidable true := is_true trivial instance decidable.false : decidable false := is_false not_false -- We use "dependent" if-then-else to be able to communicate the if-then-else condition -- to the branches @[inline] def dite (c : Prop) [h : decidable c] {α : Type u} : (c → α) → (¬ c → α) → α := λ t e, decidable.rec_on h e t /- if-then-else -/ @[inline] def ite (c : Prop) [h : decidable c] {α : Type u} (t e : α) : α := decidable.rec_on h (λ hnc, e) (λ hc, t) namespace decidable variables {p q : Prop} def rec_on_true [h : decidable p] {h₁ : p → Type u} {h₂ : ¬p → Type u} (h₃ : p) (h₄ : h₁ h₃) : decidable.rec_on h h₂ h₁ := decidable.rec_on h (λ h, false.rec _ (h h₃)) (λ h, h₄) def rec_on_false [h : decidable p] {h₁ : p → Type u} {h₂ : ¬p → Type u} (h₃ : ¬p) (h₄ : h₂ h₃) : decidable.rec_on h h₂ h₁ := decidable.rec_on h (λ h, h₄) (λ h, false.rec _ (h₃ h)) def by_cases {q : Type u} [φ : decidable p] : (p → q) → (¬p → q) → q := dite _ lemma em (p : Prop) [decidable p] : p ∨ ¬p := by_cases or.inl or.inr lemma by_contradiction [decidable p] (h : ¬p → false) : p := if h₁ : p then h₁ else false.rec _ (h h₁) end decidable section variables {p q : Prop} def decidable_of_decidable_of_iff (hp : decidable p) (h : p ↔ q) : decidable q := if hp : p then is_true (iff.mp h hp) else is_false (iff.mp (not_iff_not_of_iff h) hp) def decidable_of_decidable_of_eq (hp : decidable p) (h : p = q) : decidable q := decidable_of_decidable_of_iff hp h^.to_iff protected def or.by_cases [decidable p] [decidable q] {α : Type u} (h : p ∨ q) (h₁ : p → α) (h₂ : q → α) : α := if hp : p then h₁ hp else if hq : q then h₂ hq else false.rec _ (or.elim h hp hq) end section variables {p q : Prop} instance [decidable p] [decidable q] : decidable (p ∧ q) := if hp : p then if hq : q then is_true ⟨hp, hq⟩ else is_false (assume h : p ∧ q, hq (and.right h)) else is_false (assume h : p ∧ q, hp (and.left h)) instance [decidable p] [decidable q] : decidable (p ∨ q) := if hp : p then is_true (or.inl hp) else if hq : q then is_true (or.inr hq) else is_false (or.rec hp hq) instance [decidable p] : decidable (¬p) := if hp : p then is_false (absurd hp) else is_true hp instance implies.decidable [decidable p] [decidable q] : decidable (p → q) := if hp : p then if hq : q then is_true (assume h, hq) else is_false (assume h : p → q, absurd (h hp) hq) else is_true (assume h, absurd h hp) instance [decidable p] [decidable q] : decidable (p ↔ q) := and.decidable end instance {α : Type u} [decidable_eq α] (a b : α) : decidable (a ≠ b) := implies.decidable lemma bool.ff_ne_tt : ff = tt → false . def is_dec_eq {α : Type u} (p : α → α → bool) : Prop := ∀ ⦃x y : α⦄, p x y = tt → x = y def is_dec_refl {α : Type u} (p : α → α → bool) : Prop := ∀ x, p x x = tt open decidable instance : decidable_eq bool | ff ff := is_true rfl | ff tt := is_false bool.ff_ne_tt | tt ff := is_false (ne.symm bool.ff_ne_tt) | tt tt := is_true rfl def decidable_eq_of_bool_pred {α : Type u} {p : α → α → bool} (h₁ : is_dec_eq p) (h₂ : is_dec_refl p) : decidable_eq α := take x y : α, if hp : p x y = tt then is_true (h₁ hp) else is_false (assume hxy : x = y, absurd (h₂ y) (@eq.rec_on _ _ (λ z, ¬p z y = tt) _ hxy hp)) lemma decidable_eq_inl_refl {α : Type u} [h : decidable_eq α] (a : α) : h a a = is_true (eq.refl a) := match (h a a) with | (is_true e) := rfl | (is_false n) := absurd rfl n end lemma decidable_eq_inr_neg {α : Type u} [h : decidable_eq α] {a b : α} : Π n : a ≠ b, h a b = is_false n := assume n, match (h a b) with | (is_true e) := absurd e n | (is_false n₁) := proof_irrel n n₁ ▸ eq.refl (is_false n) end /- inhabited -/ class inhabited (α : Type u) := (default : α) def default (α : Type u) [inhabited α] : α := inhabited.default α @[inline, irreducible] def arbitrary (α : Type u) [inhabited α] : α := default α instance prop.inhabited : inhabited Prop := ⟨true⟩ instance fun.inhabited (α : Type u) {β : Type v} [h : inhabited β] : inhabited (α → β) := inhabited.rec_on h (λ b, ⟨λ a, b⟩) instance pi.inhabited (α : Type u) {β : α → Type v} [Π x, inhabited (β x)] : inhabited (Π x, β x) := ⟨λ a, default (β a)⟩ instance : inhabited bool := ⟨ff⟩ instance : inhabited pos_num := ⟨pos_num.one⟩ instance : inhabited num := ⟨num.zero⟩ class inductive nonempty (α : Type u) : Prop | intro : α → nonempty protected def nonempty.elim {α : Type u} {p : Prop} (h₁ : nonempty α) (h₂ : α → p) : p := nonempty.rec h₂ h₁ instance nonempty_of_inhabited {α : Type u} [inhabited α] : nonempty α := ⟨default α⟩ lemma nonempty_of_exists {α : Type u} {p : α → Prop} : (∃ x, p x) → nonempty α | ⟨w, h⟩ := ⟨w⟩ /- subsingleton -/ class inductive subsingleton (α : Type u) : Prop | intro : (∀ a b : α, a = b) → subsingleton protected def subsingleton.elim {α : Type u} [h : subsingleton α] : ∀ (a b : α), a = b := subsingleton.rec (λ p, p) h protected def subsingleton.helim {α β : Type u} [h : subsingleton α] (h : α = β) : ∀ (a : α) (b : β), a == b := eq.rec_on h (λ a b : α, heq_of_eq (subsingleton.elim a b)) instance subsingleton_prop (p : Prop) : subsingleton p := ⟨λ a b, proof_irrel a b⟩ instance (p : Prop) : subsingleton (decidable p) := subsingleton.intro (λ d₁, match d₁ with | (is_true t₁) := (λ d₂, match d₂ with | (is_true t₂) := eq.rec_on (proof_irrel t₁ t₂) rfl | (is_false f₂) := absurd t₁ f₂ end) | (is_false f₁) := (λ d₂, match d₂ with | (is_true t₂) := absurd t₂ f₁ | (is_false f₂) := eq.rec_on (proof_irrel f₁ f₂) rfl end) end) protected lemma rec_subsingleton {p : Prop} [h : decidable p] {h₁ : p → Type u} {h₂ : ¬p → Type u} [h₃ : Π (h : p), subsingleton (h₁ h)] [h₄ : Π (h : ¬p), subsingleton (h₂ h)] : subsingleton (decidable.rec_on h h₂ h₁) := match h with | (is_true h) := h₃ h | (is_false h) := h₄ h end lemma if_pos {c : Prop} [h : decidable c] (hc : c) {α : Type u} {t e : α} : (ite c t e) = t := match h with | (is_true hc) := rfl | (is_false hnc) := absurd hc hnc end lemma if_neg {c : Prop} [h : decidable c] (hnc : ¬c) {α : Type u} {t e : α} : (ite c t e) = e := match h with | (is_true hc) := absurd hc hnc | (is_false hnc) := rfl end attribute [simp] lemma if_t_t (c : Prop) [h : decidable c] {α : Type u} (t : α) : (ite c t t) = t := match h with | (is_true hc) := rfl | (is_false hnc) := rfl end lemma implies_of_if_pos {c t e : Prop} [decidable c] (h : ite c t e) : c → t := assume hc, eq.rec_on (if_pos hc : ite c t e = t) h lemma implies_of_if_neg {c t e : Prop} [decidable c] (h : ite c t e) : ¬c → e := assume hnc, eq.rec_on (if_neg hnc : ite c t e = e) h lemma if_ctx_congr {α : Type u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c] {x y u v : α} (h_c : b ↔ c) (h_t : c → x = u) (h_e : ¬c → y = v) : ite b x y = ite c u v := match dec_b, dec_c with | (is_false h₁), (is_false h₂) := h_e h₂ | (is_true h₁), (is_true h₂) := h_t h₂ | (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁) | (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂) end @[congr] lemma if_congr {α : Type u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c] {x y u v : α} (h_c : b ↔ c) (h_t : x = u) (h_e : y = v) : ite b x y = ite c u v := @if_ctx_congr α b c dec_b dec_c x y u v h_c (λ h, h_t) (λ h, h_e) lemma if_ctx_simp_congr {α : Type u} {b c : Prop} [dec_b : decidable b] {x y u v : α} (h_c : b ↔ c) (h_t : c → x = u) (h_e : ¬c → y = v) : ite b x y = (@ite c (decidable_of_decidable_of_iff dec_b h_c) α u v) := @if_ctx_congr α b c dec_b (decidable_of_decidable_of_iff dec_b h_c) x y u v h_c h_t h_e @[congr] lemma if_simp_congr {α : Type u} {b c : Prop} [dec_b : decidable b] {x y u v : α} (h_c : b ↔ c) (h_t : x = u) (h_e : y = v) : ite b x y = (@ite c (decidable_of_decidable_of_iff dec_b h_c) α u v) := @if_ctx_simp_congr α b c dec_b x y u v h_c (λ h, h_t) (λ h, h_e) @[simp] def if_true {α : Type u} {h : decidable true} (t e : α) : (@ite true h α t e) = t := if_pos trivial @[simp] def if_false {α : Type u} {h : decidable false} (t e : α) : (@ite false h α t e) = e := if_neg not_false lemma if_ctx_congr_prop {b c x y u v : Prop} [dec_b : decidable b] [dec_c : decidable c] (h_c : b ↔ c) (h_t : c → (x ↔ u)) (h_e : ¬c → (y ↔ v)) : ite b x y ↔ ite c u v := match dec_b, dec_c with | (is_false h₁), (is_false h₂) := h_e h₂ | (is_true h₁), (is_true h₂) := h_t h₂ | (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁) | (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂) end @[congr] lemma if_congr_prop {b c x y u v : Prop} [dec_b : decidable b] [dec_c : decidable c] (h_c : b ↔ c) (h_t : x ↔ u) (h_e : y ↔ v) : ite b x y ↔ ite c u v := if_ctx_congr_prop h_c (λ h, h_t) (λ h, h_e) lemma if_ctx_simp_congr_prop {b c x y u v : Prop} [dec_b : decidable b] (h_c : b ↔ c) (h_t : c → (x ↔ u)) (h_e : ¬c → (y ↔ v)) : ite b x y ↔ (@ite c (decidable_of_decidable_of_iff dec_b h_c) Prop u v) := @if_ctx_congr_prop b c x y u v dec_b (decidable_of_decidable_of_iff dec_b h_c) h_c h_t h_e @[congr] lemma if_simp_congr_prop {b c x y u v : Prop} [dec_b : decidable b] (h_c : b ↔ c) (h_t : x ↔ u) (h_e : y ↔ v) : ite b x y ↔ (@ite c (decidable_of_decidable_of_iff dec_b h_c) Prop u v) := @if_ctx_simp_congr_prop b c x y u v dec_b h_c (λ h, h_t) (λ h, h_e) lemma dif_pos {c : Prop} [h : decidable c] (hc : c) {α : Type u} {t : c → α} {e : ¬ c → α} : dite c t e = t hc := match h with | (is_true hc) := rfl | (is_false hnc) := absurd hc hnc end lemma dif_neg {c : Prop} [h : decidable c] (hnc : ¬c) {α : Type u} {t : c → α} {e : ¬ c → α} : dite c t e = e hnc := match h with | (is_true hc) := absurd hc hnc | (is_false hnc) := rfl end lemma dif_ctx_congr {α : Type u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c] {x : b → α} {u : c → α} {y : ¬b → α} {v : ¬c → α} (h_c : b ↔ c) (h_t : ∀ (h : c), x (iff.mpr h_c h) = u h) (h_e : ∀ (h : ¬c), y (iff.mpr (not_iff_not_of_iff h_c) h) = v h) : (@dite b dec_b α x y) = (@dite c dec_c α u v) := match dec_b, dec_c with | (is_false h₁), (is_false h₂) := h_e h₂ | (is_true h₁), (is_true h₂) := h_t h₂ | (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁) | (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂) end lemma dif_ctx_simp_congr {α : Type u} {b c : Prop} [dec_b : decidable b] {x : b → α} {u : c → α} {y : ¬b → α} {v : ¬c → α} (h_c : b ↔ c) (h_t : ∀ (h : c), x (iff.mpr h_c h) = u h) (h_e : ∀ (h : ¬c), y (iff.mpr (not_iff_not_of_iff h_c) h) = v h) : (@dite b dec_b α x y) = (@dite c (decidable_of_decidable_of_iff dec_b h_c) α u v) := @dif_ctx_congr α b c dec_b (decidable_of_decidable_of_iff dec_b h_c) x u y v h_c h_t h_e -- Remark: dite and ite are "defally equal" when we ignore the proofs. lemma dite_ite_eq (c : Prop) [h : decidable c] {α : Type u} (t : α) (e : α) : dite c (λ h, t) (λ h, e) = ite c t e := match h with | (is_true hc) := rfl | (is_false hnc) := rfl end def as_true (c : Prop) [decidable c] : Prop := if c then true else false def as_false (c : Prop) [decidable c] : Prop := if c then false else true def of_as_true {c : Prop} [h₁ : decidable c] (h₂ : as_true c) : c := match h₁, h₂ with | (is_true h_c), h₂ := h_c | (is_false h_c), h₂ := false.elim h₂ end /- Universe lifting operation -/ structure {r s} ulift (α : Type s) : Type (max 1 s r) := up :: (down : α) namespace ulift /- βijection between α and ulift.{v} α -/ lemma up_down {α : Type u} : ∀ (b : ulift.{v} α), up (down b) = b | (up a) := rfl lemma down_up {α : Type u} (a : α) : down (up.{v} a) = a := rfl end ulift /- Equalities for rewriting let-expressions -/ lemma let_value_eq {α : Type u} {β : Type v} {a₁ a₂ : α} (b : α → β) : a₁ = a₂ → (let x : α := a₁ in b x) = (let x : α := a₂ in b x) := λ h, eq.rec_on h rfl lemma let_value_heq {α : Type v} {β : α → Type u} {a₁ a₂ : α} (b : Π x : α, β x) : a₁ = a₂ → (let x : α := a₁ in b x) == (let x : α := a₂ in b x) := λ h, eq.rec_on h (heq.refl (b a₁)) lemma let_body_eq {α : Type v} {β : α → Type u} (a : α) {b₁ b₂ : Π x : α, β x} : (∀ x, b₁ x = b₂ x) → (let x : α := a in b₁ x) = (let x : α := a in b₂ x) := λ h, h a lemma let_eq {α : Type v} {β : Type u} {a₁ a₂ : α} {b₁ b₂ : α → β} : a₁ = a₂ → (∀ x, b₁ x = b₂ x) → (let x : α := a₁ in b₁ x) = (let x : α := a₂ in b₂ x) := λ h₁ h₂, eq.rec_on h₁ (h₂ a₁) section relation variables {α : Type u} {β : Type v} (r : β → β → Prop) local infix `≺`:50 := r def reflexive := ∀ x, x ≺ x def symmetric := ∀ ⦃x y⦄, x ≺ y → y ≺ x def transitive := ∀ ⦃x y z⦄, x ≺ y → y ≺ z → x ≺ z def equivalence := reflexive r ∧ symmetric r ∧ transitive r def total := ∀ x y, x ≺ y ∨ y ≺ x def mk_equivalence (rfl : reflexive r) (symm : symmetric r) (trans : transitive r) : equivalence r := ⟨rfl, symm, trans⟩ def irreflexive := ∀ x, ¬ x ≺ x def anti_symmetric := ∀ ⦃x y⦄, x ≺ y → y ≺ x → x = y def empty_relation := λ a₁ a₂ : α, false def subrelation (q r : β → β → Prop) := ∀ ⦃x y⦄, q x y → r x y def inv_image (f : α → β) : α → α → Prop := λ a₁ a₂, f a₁ ≺ f a₂ lemma inv_image.trans (f : α → β) (h : transitive r) : transitive (inv_image r f) := λ (a₁ a₂ a₃ : α) (h₁ : inv_image r f a₁ a₂) (h₂ : inv_image r f a₂ a₃), h h₁ h₂ lemma inv_image.irreflexive (f : α → β) (h : irreflexive r) : irreflexive (inv_image r f) := λ (a : α) (h₁ : inv_image r f a a), h (f a) h₁ inductive tc {α : Type u} (r : α → α → Prop) : α → α → Prop | base : ∀ a b, r a b → tc a b | trans : ∀ a b c, tc a b → tc b c → tc a c end relation section binary variables {α : Type u} {β : Type v} variable f : α → α → α variable inv : α → α variable one : α local notation a * b := f a b local notation a ⁻¹ := inv a variable g : α → α → α local notation a + b := g a b def commutative := ∀ a b, a * b = b * a def associative := ∀ a b c, (a * b) * c = a * (b * c) def left_identity := ∀ a, one * a = a def right_identity := ∀ a, a * one = a def right_inverse := ∀ a, a * a⁻¹ = one def left_cancelative := ∀ a b c, a * b = a * c → b = c def right_cancelative := ∀ a b c, a * b = c * b → a = c def left_distributive := ∀ a b c, a * (b + c) = a * b + a * c def right_distributive := ∀ a b c, (a + b) * c = a * c + b * c def right_commutative (h : β → α → β) := ∀ b a₁ a₂, h (h b a₁) a₂ = h (h b a₂) a₁ def left_commutative (h : α → β → β) := ∀ a₁ a₂ b, h a₁ (h a₂ b) = h a₂ (h a₁ b) lemma left_comm : commutative f → associative f → left_commutative f := assume hcomm hassoc, take a b c, calc a*(b*c) = (a*b)*c : eq.symm (hassoc a b c) ... = (b*a)*c : hcomm a b ▸ rfl ... = b*(a*c) : hassoc b a c lemma right_comm : commutative f → associative f → right_commutative f := assume hcomm hassoc, take a b c, calc (a*b)*c = a*(b*c) : hassoc a b c ... = a*(c*b) : hcomm b c ▸ rfl ... = (a*c)*b : eq.symm (hassoc a c b) end binary
b39cf8652602007ab953984d8175ec09e1c220fe
4727251e0cd73359b15b664c3170e5d754078599
/src/data/tprod.lean
a653cb6bcb69c31e594cda5a13447abad0e39044
[ "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
5,902
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 data.list.nodup /-! # Finite products of types This file defines the product of types over a list. For `l : list ι` and `α : ι → Type*` we define `list.tprod α l = l.foldr (λ i β, α i × β) punit`. This type should not be used if `Π i, α i` or `Π i ∈ l, α i` can be used instead (in the last expression, we could also replace the list `l` by a set or a finset). This type is used as an intermediary between binary products and finitary products. The application of this type is finitary product measures, but it could be used in any construction/theorem that is easier to define/prove on binary products than on finitary products. * Once we have the construction on binary products (like binary product measures in `measure_theory.prod`), we can easily define a finitary version on the type `tprod l α` by iterating. Properties can also be easily extended from the binary case to the finitary case by iterating. * Then we can use the equivalence `list.tprod.pi_equiv_tprod` below (or enhanced versions of it, like a `measurable_equiv` for product measures) to get the construction on `Π i : ι, α i`, at least when assuming `[fintype ι] [encodable ι]` (using `encodable.sorted_univ`). Using `local attribute [instance] fintype.to_encodable` we can get rid of the argument `[encodable ι]`. ## Main definitions * We have the equivalence `tprod.pi_equiv_tprod : (Π i, α i) ≃ tprod α l` if `l` contains every element of `ι` exactly once. * The product of sets is `set.tprod : (Π i, set (α i)) → set (tprod α l)`. -/ open list function variables {ι : Type*} {α : ι → Type*} {i j : ι} {l : list ι} {f : Π i, α i} namespace list variable (α) /-- The product of a family of types over a list. -/ def tprod (l : list ι) : Type* := l.foldr (λ i β, α i × β) punit variable {α} namespace tprod open list /-- Turning a function `f : Π i, α i` into an element of the iterated product `tprod α l`. -/ protected def mk : ∀ (l : list ι) (f : Π i, α i), tprod α l | [] := λ f, punit.star | (i :: is) := λ f, (f i, mk is f) instance [∀ i, inhabited (α i)] : inhabited (tprod α l) := ⟨tprod.mk l (λ _, default)⟩ @[simp] lemma fst_mk (i : ι) (l : list ι) (f : Π i, α i) : (tprod.mk (i::l) f).1 = f i := rfl @[simp] lemma snd_mk (i : ι) (l : list ι) (f : Π i, α i) : (tprod.mk (i::l) f).2 = tprod.mk l f := rfl variables [decidable_eq ι] /-- Given an element of the iterated product `l.prod α`, take a projection into direction `i`. If `i` appears multiple times in `l`, this chooses the first component in direction `i`. -/ protected def elim : ∀ {l : list ι} (v : tprod α l) {i : ι} (hi : i ∈ l), α i | (i :: is) v j hj := if hji : j = i then by { subst hji, exact v.1 } else elim v.2 (hj.resolve_left hji) @[simp] lemma elim_self (v : tprod α (i :: l)) : v.elim (l.mem_cons_self i) = v.1 := by simp [tprod.elim] @[simp] lemma elim_of_ne (hj : j ∈ i :: l) (hji : j ≠ i) (v : tprod α (i :: l)) : v.elim hj = tprod.elim v.2 (hj.resolve_left hji) := by simp [tprod.elim, hji] @[simp] lemma elim_of_mem (hl : (i :: l).nodup) (hj : j ∈ l) (v : tprod α (i :: l)) : v.elim (mem_cons_of_mem _ hj) = tprod.elim v.2 hj := by { apply elim_of_ne, rintro rfl, exact hl.not_mem hj } lemma elim_mk : ∀ (l : list ι) (f : Π i, α i) {i : ι} (hi : i ∈ l), (tprod.mk l f).elim hi = f i | (i :: is) f j hj := begin by_cases hji : j = i, { subst hji, simp }, { rw [elim_of_ne _ hji, snd_mk, elim_mk] } end @[ext] lemma ext : ∀ {l : list ι} (hl : l.nodup) {v w : tprod α l} (hvw : ∀ i (hi : i ∈ l), v.elim hi = w.elim hi), v = w | [] hl v w hvw := punit.ext | (i :: is) hl v w hvw := begin ext, rw [← elim_self v, hvw, elim_self], refine ext (nodup_cons.mp hl).2 (λ j hj, _), rw [← elim_of_mem hl, hvw, elim_of_mem hl] end /-- A version of `tprod.elim` when `l` contains all elements. In this case we get a function into `Π i, α i`. -/ @[simp] protected def elim' (h : ∀ i, i ∈ l) (v : tprod α l) (i : ι) : α i := v.elim (h i) lemma mk_elim (hnd : l.nodup) (h : ∀ i, i ∈ l) (v : tprod α l) : tprod.mk l (v.elim' h) = v := tprod.ext hnd (λ i hi, by simp [elim_mk]) /-- Pi-types are equivalent to iterated products. -/ def pi_equiv_tprod (hnd : l.nodup) (h : ∀ i, i ∈ l) : (Π i, α i) ≃ tprod α l := ⟨tprod.mk l, tprod.elim' h, λ f, funext $ λ i, elim_mk l f (h i), mk_elim hnd h⟩ end tprod end list namespace set open list /-- A product of sets in `tprod α l`. -/ @[simp] protected def tprod : ∀ (l : list ι) (t : Π i, set (α i)), set (tprod α l) | [] t := univ | (i :: is) t := t i ×ˢ tprod is t lemma mk_preimage_tprod : ∀ (l : list ι) (t : Π i, set (α i)), tprod.mk l ⁻¹' set.tprod l t = {i | i ∈ l}.pi t | [] t := by simp [set.tprod] | (i :: l) t := begin ext f, have : f ∈ tprod.mk l ⁻¹' set.tprod l t ↔ f ∈ {x | x ∈ l}.pi t, { rw [mk_preimage_tprod l t] }, change tprod.mk l f ∈ set.tprod l t ↔ ∀ (i : ι), i ∈ l → f i ∈ t i at this, /- `simp [set.tprod, tprod.mk, this]` can close this goal but is slow. -/ rw [set.tprod, tprod.mk, mem_preimage, mem_pi, prod_mk_mem_set_prod_eq], simp_rw [mem_set_of_eq, mem_cons_iff], rw [forall_eq_or_imp, and.congr_right_iff], exact λ _, this end lemma elim_preimage_pi [decidable_eq ι] {l : list ι} (hnd : l.nodup) (h : ∀ i, i ∈ l) (t : Π i, set (α i)) : tprod.elim' h ⁻¹' pi univ t = set.tprod l t := begin have : { i | i ∈ l} = univ, { ext i, simp [h] }, rw [← this, ← mk_preimage_tprod, preimage_preimage], convert preimage_id, simp [tprod.mk_elim hnd h, id_def] end end set
fb100a082951db512d84a104f16051647f4a81b9
3d2a7f1582fe5bae4d0bdc2fe86e997521239a65
/spatial-reasoning/spatial-reasoning-problem-04.lean
d34ededcec4c1932beed0737cbf51384849738c3
[]
no_license
own-pt/common-sense-lean
e4fa643ae010459de3d1bf673be7cbc7062563c9
f672210aecb4172f5bae265e43e6867397e13b1c
refs/heads/master
1,622,065,660,261
1,589,487,533,000
1,589,487,533,000
254,167,782
3
2
null
1,589,487,535,000
1,586,370,214,000
Lean
UTF-8
Lean
false
false
2,112
lean
/- Spatial Reasoning Problem 04 -/ /- it can be found at: SpatialQs.txt -/ /- (4) John is carrying a vase. There is a flower in the vase. Is John carrying a flower?-/ constant U : Type constants John Inside Carrying Flower Container Transfer On : U constant objectTransferred : U → U → Prop constant orientation : U → U → U → Prop constant subclass : U → U → Prop constant agent : U → U → Prop constant ins : U → U → Prop /- axioms from SUMO -/ axiom a1 : ∀ VAR1 VAR2 VAR3, (orientation VAR2 VAR1 Inside) ∧ (objectTransferred VAR3 VAR1) ∧ (ins VAR3 Transfer) → (objectTransferred VAR3 VAR2) axiom a2 : subclass Carrying Transfer axiom a3 : ∀ X Y Z, (subclass X Y) ∧ (ins Z X) → (ins Z Y) /- axioms from problem -/ axiom a4 : ∃ C V F, (ins C Carrying) ∧ (objectTransferred C V) ∧ (agent C John) ∧ (ins F Flower) ∧ (ins V Container) ∧ (orientation F V Inside) /- axioms to be added to SUMO -/ axiom a5 : ∀ T O O2, (ins T Transfer) ∧ (objectTransferred T O) ∧ (orientation O2 O Inside) → (objectTransferred T O2) axiom a6 : ∀ T O O2, (ins T Transfer) ∧ (objectTransferred T O) ∧ (orientation O O2 On) → (objectTransferred T O2) /- demonstration start -/ theorem john_is_carrying_a_flower : ∃ C F, (ins C Carrying) ∧ (agent C John) ∧ (ins F Flower) ∧ (objectTransferred C F) := begin apply exists.elim a4, assume C r1, apply exists.elim r1, assume V r2, apply exists.elim r2, assume F h, have h1, from h.left, have h2, from h.right.right.left, have h3, from h.right.right.right.left, have s1, from (a3 _ _ _) ⟨a2,h1⟩, have s2, from h.right.left, have s3, from h.right.right.right.right.right, have h4, from (a5 _ _ _) ⟨s1,⟨s2,s3⟩⟩, have h5 : ∃ F, ins C Carrying ∧ agent C John ∧ ins F Flower ∧ objectTransferred C F, from exists.intro F ⟨h1,⟨h2,⟨h3,h4⟩⟩⟩, have h6 : ∃ C F, ins C Carrying ∧ agent C John ∧ ins F Flower ∧ objectTransferred C F, from exists.intro C h5, assumption end
8264f42d8300760220fb4b03921e85194139ec0e
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/num/basic.lean
97f97da0bb97cd3d76c2eb299523cd196c1e48eb
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
17,479
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, Mario Carneiro Binary representation of integers using inductive types. Note: Unlike in Coq, where this representation is preferred because of the reliance on kernel reduction, in Lean this representation is discouraged in favor of the "Peano" natural numbers `nat`, and the purpose of this collection of theorems is to show the equivalence of the different approaches. -/ import data.vector data.bitvec /-- The type of positive binary numbers. 13 = 1101(base 2) = bit1 (bit0 (bit1 one)) -/ @[derive has_reflect, derive decidable_eq] inductive pos_num : Type | one : pos_num | bit1 : pos_num → pos_num | bit0 : pos_num → pos_num instance : has_one pos_num := ⟨pos_num.one⟩ instance : inhabited pos_num := ⟨1⟩ /-- The type of nonnegative binary numbers, using `pos_num`. 13 = 1101(base 2) = pos (bit1 (bit0 (bit1 one))) -/ @[derive has_reflect, derive decidable_eq] inductive num : Type | zero : num | pos : pos_num → num instance : has_zero num := ⟨num.zero⟩ instance : has_one num := ⟨num.pos 1⟩ instance : inhabited num := ⟨0⟩ /-- Representation of integers using trichotomy around zero. 13 = 1101(base 2) = pos (bit1 (bit0 (bit1 one))) -13 = -1101(base 2) = neg (bit1 (bit0 (bit1 one))) -/ @[derive has_reflect, derive decidable_eq] inductive znum : Type | zero : znum | pos : pos_num → znum | neg : pos_num → znum instance : has_zero znum := ⟨znum.zero⟩ instance : has_one znum := ⟨znum.pos 1⟩ instance : inhabited znum := ⟨0⟩ /-- See `snum`. -/ @[derive has_reflect, derive decidable_eq] inductive nzsnum : Type | msb : bool → nzsnum | bit : bool → nzsnum → nzsnum /-- Alternative representation of integers using a sign bit at the end. The convention on sign here is to have the argument to `msb` denote the sign of the MSB itself, with all higher bits set to the negation of this sign. The result is interpreted in two's complement. 13 = ..0001101(base 2) = nz (bit1 (bit0 (bit1 (msb tt)))) -13 = ..1110011(base 2) = nz (bit1 (bit1 (bit0 (msb ff)))) As with `num`, a special case must be added for zero, which has no msb, but by two's complement symmetry there is a second special case for -1. Here the `bool` field indicates the sign of the number. 0 = ..0000000(base 2) = zero ff -1 = ..1111111(base 2) = zero tt -/ @[derive has_reflect, derive decidable_eq] inductive snum : Type | zero : bool → snum | nz : nzsnum → snum instance : has_coe nzsnum snum := ⟨snum.nz⟩ instance : has_zero snum := ⟨snum.zero ff⟩ instance : has_one nzsnum := ⟨nzsnum.msb tt⟩ instance : has_one snum := ⟨snum.nz 1⟩ instance : inhabited nzsnum := ⟨1⟩ instance : inhabited snum := ⟨0⟩ namespace pos_num def bit (b : bool) : pos_num → pos_num := cond b bit1 bit0 def succ : pos_num → pos_num | 1 := bit0 one | (bit1 n) := bit0 (succ n) | (bit0 n) := bit1 n def is_one : pos_num → bool | 1 := tt | _ := ff protected def add : pos_num → pos_num → pos_num | 1 b := succ b | a 1 := succ a | (bit0 a) (bit0 b) := bit0 (add a b) | (bit1 a) (bit1 b) := bit0 (succ (add a b)) | (bit0 a) (bit1 b) := bit1 (add a b) | (bit1 a) (bit0 b) := bit1 (add a b) instance : has_add pos_num := ⟨pos_num.add⟩ def pred' : pos_num → num | 1 := 0 | (bit0 n) := num.pos (num.cases_on (pred' n) 1 bit1) | (bit1 n) := num.pos (bit0 n) def pred (a : pos_num) : pos_num := num.cases_on (pred' a) 1 id def size : pos_num → pos_num | 1 := 1 | (bit0 n) := succ (size n) | (bit1 n) := succ (size n) def nat_size : pos_num → nat | 1 := 1 | (bit0 n) := nat.succ (nat_size n) | (bit1 n) := nat.succ (nat_size n) protected def mul (a : pos_num) : pos_num → pos_num | 1 := a | (bit0 b) := bit0 (mul b) | (bit1 b) := bit0 (mul b) + a instance : has_mul pos_num := ⟨pos_num.mul⟩ def of_nat_succ : ℕ → pos_num | 0 := 1 | (nat.succ n) := succ (of_nat_succ n) def of_nat (n : ℕ) : pos_num := of_nat_succ (nat.pred n) open ordering def cmp : pos_num → pos_num → ordering | 1 1 := eq | _ 1 := gt | 1 _ := lt | (bit0 a) (bit0 b) := cmp a b | (bit0 a) (bit1 b) := ordering.cases_on (cmp a b) lt lt gt | (bit1 a) (bit0 b) := ordering.cases_on (cmp a b) lt gt gt | (bit1 a) (bit1 b) := cmp a b instance : has_lt pos_num := ⟨λa b, cmp a b = ordering.lt⟩ instance : has_le pos_num := ⟨λa b, ¬ b < a⟩ instance decidable_lt : @decidable_rel pos_num (<) | a b := by dsimp [(<)]; apply_instance instance decidable_le : @decidable_rel pos_num (≤) | a b := by dsimp [(≤)]; apply_instance end pos_num section variables {α : Type*} [has_zero α] [has_one α] [has_add α] def cast_pos_num : pos_num → α | 1 := 1 | (pos_num.bit0 a) := bit0 (cast_pos_num a) | (pos_num.bit1 a) := bit1 (cast_pos_num a) def cast_num : num → α | 0 := 0 | (num.pos p) := cast_pos_num p @[priority 10] instance pos_num_coe : has_coe pos_num α := ⟨cast_pos_num⟩ @[priority 10] instance num_nat_coe : has_coe num α := ⟨cast_num⟩ instance : has_repr pos_num := ⟨λ n, repr (n : ℕ)⟩ instance : has_repr num := ⟨λ n, repr (n : ℕ)⟩ end namespace num open pos_num def succ' : num → pos_num | 0 := 1 | (pos p) := succ p def succ (n : num) : num := pos (succ' n) protected def add : num → num → num | 0 a := a | b 0 := b | (pos a) (pos b) := pos (a + b) instance : has_add num := ⟨num.add⟩ protected def bit0 : num → num | 0 := 0 | (pos n) := pos (pos_num.bit0 n) protected def bit1 : num → num | 0 := 1 | (pos n) := pos (pos_num.bit1 n) def bit (b : bool) : num → num := cond b num.bit1 num.bit0 def size : num → num | 0 := 0 | (pos n) := pos (pos_num.size n) def nat_size : num → nat | 0 := 0 | (pos n) := pos_num.nat_size n protected def mul : num → num → num | 0 _ := 0 | _ 0 := 0 | (pos a) (pos b) := pos (a * b) instance : has_mul num := ⟨num.mul⟩ open ordering def cmp : num → num → ordering | 0 0 := eq | _ 0 := gt | 0 _ := lt | (pos a) (pos b) := pos_num.cmp a b instance : has_lt num := ⟨λa b, cmp a b = ordering.lt⟩ instance : has_le num := ⟨λa b, ¬ b < a⟩ instance decidable_lt : @decidable_rel num (<) | a b := by dsimp [(<)]; apply_instance instance decidable_le : @decidable_rel num (≤) | a b := by dsimp [(≤)]; apply_instance def to_znum : num → znum | 0 := 0 | (pos a) := znum.pos a def to_znum_neg : num → znum | 0 := 0 | (pos a) := znum.neg a def of_nat' : ℕ → num := nat.binary_rec 0 (λ b n, cond b num.bit1 num.bit0) end num namespace znum open pos_num def zneg : znum → znum | 0 := 0 | (pos a) := neg a | (neg a) := pos a instance : has_neg znum := ⟨zneg⟩ def abs : znum → num | 0 := 0 | (pos a) := num.pos a | (neg a) := num.pos a def succ : znum → znum | 0 := 1 | (pos a) := pos (pos_num.succ a) | (neg a) := (pos_num.pred' a).to_znum_neg def pred : znum → znum | 0 := neg 1 | (pos a) := (pos_num.pred' a).to_znum | (neg a) := neg (pos_num.succ a) protected def bit0 : znum → znum | 0 := 0 | (pos n) := pos (pos_num.bit0 n) | (neg n) := neg (pos_num.bit0 n) protected def bit1 : znum → znum | 0 := 1 | (pos n) := pos (pos_num.bit1 n) | (neg n) := neg (num.cases_on (pred' n) 1 pos_num.bit1) protected def bitm1 : znum → znum | 0 := neg 1 | (pos n) := pos (num.cases_on (pred' n) 1 pos_num.bit1) | (neg n) := neg (pos_num.bit1 n) def of_int' : ℤ → znum | (n : ℕ) := num.to_znum (num.of_nat' n) | -[1+ n] := num.to_znum_neg (num.of_nat' (n+1)) end znum namespace pos_num open znum def sub' : pos_num → pos_num → znum | a 1 := (pred' a).to_znum | 1 b := (pred' b).to_znum_neg | (bit0 a) (bit0 b) := (sub' a b).bit0 | (bit0 a) (bit1 b) := (sub' a b).bitm1 | (bit1 a) (bit0 b) := (sub' a b).bit1 | (bit1 a) (bit1 b) := (sub' a b).bit0 def of_znum' : znum → option pos_num | (znum.pos p) := some p | _ := none def of_znum : znum → pos_num | (znum.pos p) := p | _ := 1 protected def sub (a b : pos_num) : pos_num := match sub' a b with | (znum.pos p) := p | _ := 1 end instance : has_sub pos_num := ⟨pos_num.sub⟩ end pos_num namespace num def ppred : num → option num | 0 := none | (pos p) := some p.pred' def pred : num → num | 0 := 0 | (pos p) := p.pred' def div2 : num → num | 0 := 0 | 1 := 0 | (pos (pos_num.bit0 p)) := pos p | (pos (pos_num.bit1 p)) := pos p def of_znum' : znum → option num | 0 := some 0 | (znum.pos p) := some (pos p) | (znum.neg p) := none def of_znum : znum → num | (znum.pos p) := pos p | _ := 0 def sub' : num → num → znum | 0 0 := 0 | (pos a) 0 := znum.pos a | 0 (pos b) := znum.neg b | (pos a) (pos b) := a.sub' b def psub (a b : num) : option num := of_znum' (sub' a b) protected def sub (a b : num) : num := of_znum (sub' a b) instance : has_sub num := ⟨num.sub⟩ end num namespace znum open pos_num protected def add : znum → znum → znum | 0 a := a | b 0 := b | (pos a) (pos b) := pos (a + b) | (pos a) (neg b) := sub' a b | (neg a) (pos b) := sub' b a | (neg a) (neg b) := neg (a + b) instance : has_add znum := ⟨znum.add⟩ protected def mul : znum → znum → znum | 0 a := 0 | b 0 := 0 | (pos a) (pos b) := pos (a * b) | (pos a) (neg b) := neg (a * b) | (neg a) (pos b) := neg (a * b) | (neg a) (neg b) := pos (a * b) instance : has_mul znum := ⟨znum.mul⟩ open ordering def cmp : znum → znum → ordering | 0 0 := eq | (pos a) (pos b) := pos_num.cmp a b | (neg a) (neg b) := pos_num.cmp b a | (pos _) _ := gt | (neg _) _ := lt | _ (pos _) := lt | _ (neg _) := gt instance : has_lt znum := ⟨λa b, cmp a b = ordering.lt⟩ instance : has_le znum := ⟨λa b, ¬ b < a⟩ instance decidable_lt : @decidable_rel znum (<) | a b := by dsimp [(<)]; apply_instance instance decidable_le : @decidable_rel znum (≤) | a b := by dsimp [(≤)]; apply_instance end znum namespace pos_num def divmod_aux (d : pos_num) (q r : num) : num × num := match num.of_znum' (num.sub' r (num.pos d)) with | some r' := (num.bit1 q, r') | none := (num.bit0 q, r) end def divmod (d : pos_num) : pos_num → num × num | (bit0 n) := let (q, r₁) := divmod n in divmod_aux d q (num.bit0 r₁) | (bit1 n) := let (q, r₁) := divmod n in divmod_aux d q (num.bit1 r₁) | 1 := divmod_aux d 0 1 def div' (n d : pos_num) : num := (divmod d n).1 def mod' (n d : pos_num) : num := (divmod d n).2 def sqrt_aux1 (b : pos_num) (r n : num) : num × num := match num.of_znum' (n.sub' (r + num.pos b)) with | some n' := (r.div2 + num.pos b, n') | none := (r.div2, n) end def sqrt_aux : pos_num → num → num → num | b@(bit0 b') r n := let (r', n') := sqrt_aux1 b r n in sqrt_aux b' r' n' | b@(bit1 b') r n := let (r', n') := sqrt_aux1 b r n in sqrt_aux b' r' n' | 1 r n := (sqrt_aux1 1 r n).1 /- def sqrt_aux : ℕ → ℕ → ℕ → ℕ | b r n := if b0 : b = 0 then r else let b' := shiftr b 2 in have b' < b, from sqrt_aux_dec b0, match (n - (r + b : ℕ) : ℤ) with | (n' : ℕ) := sqrt_aux b' (div2 r + b) n' | _ := sqrt_aux b' (div2 r) n end /-- `sqrt n` is the square root of a natural number `n`. If `n` is not a perfect square, it returns the largest `k:ℕ` such that `k*k ≤ n`. -/ def sqrt (n : ℕ) : ℕ := match size n with | 0 := 0 | succ s := sqrt_aux (shiftl 1 (bit0 (div2 s))) 0 n end -/ end pos_num namespace num def div : num → num → num | 0 _ := 0 | _ 0 := 0 | (pos n) (pos d) := pos_num.div' n d def mod : num → num → num | 0 _ := 0 | n 0 := n | (pos n) (pos d) := pos_num.mod' n d instance : has_div num := ⟨num.div⟩ instance : has_mod num := ⟨num.mod⟩ def gcd_aux : nat → num → num → num | 0 a b := b | (nat.succ n) 0 b := b | (nat.succ n) a b := gcd_aux n (b % a) a def gcd (a b : num) : num := if a ≤ b then gcd_aux (a.nat_size + b.nat_size) a b else gcd_aux (b.nat_size + a.nat_size) b a end num namespace znum def div : znum → znum → znum | 0 _ := 0 | _ 0 := 0 | (pos n) (pos d) := num.to_znum (pos_num.div' n d) | (pos n) (neg d) := num.to_znum_neg (pos_num.div' n d) | (neg n) (pos d) := neg (pos_num.pred' n / num.pos d).succ' | (neg n) (neg d) := pos (pos_num.pred' n / num.pos d).succ' def mod : znum → znum → znum | 0 d := 0 | (pos n) d := num.to_znum (num.pos n % d.abs) | (neg n) d := d.abs.sub' (pos_num.pred' n % d.abs).succ instance : has_div znum := ⟨znum.div⟩ instance : has_mod znum := ⟨znum.mod⟩ def gcd (a b : znum) : num := a.abs.gcd b.abs end znum section variables {α : Type*} [has_zero α] [has_one α] [has_add α] [has_neg α] def cast_znum : znum → α | 0 := 0 | (znum.pos p) := p | (znum.neg p) := -p @[priority 10] instance znum_coe : has_coe znum α := ⟨cast_znum⟩ instance : has_repr znum := ⟨λ n, repr (n : ℤ)⟩ end /- The snum representation uses a bit string, essentially a list of 0 (ff) and 1 (tt) bits, and the negation of the MSB is sign-extended to all higher bits. -/ namespace nzsnum notation a :: b := bit a b def sign : nzsnum → bool | (msb b) := bnot b | (b :: p) := sign p @[pattern] def not : nzsnum → nzsnum | (msb b) := msb (bnot b) | (b :: p) := bnot b :: not p prefix ~ := not def bit0 : nzsnum → nzsnum := bit ff def bit1 : nzsnum → nzsnum := bit tt def head : nzsnum → bool | (msb b) := b | (b :: p) := b def tail : nzsnum → snum | (msb b) := snum.zero (bnot b) | (b :: p) := p end nzsnum namespace snum open nzsnum def sign : snum → bool | (zero z) := z | (nz p) := p.sign @[pattern] def not : snum → snum | (zero z) := zero (bnot z) | (nz p) := ~p prefix ~ := not @[pattern] def bit : bool → snum → snum | b (zero z) := if b = z then zero b else msb b | b (nz p) := p.bit b notation a :: b := bit a b def bit0 : snum → snum := bit ff def bit1 : snum → snum := bit tt theorem bit_zero (b) : b :: zero b = zero b := by cases b; refl theorem bit_one (b) : b :: zero (bnot b) = msb b := by cases b; refl end snum namespace nzsnum open snum def drec' {C : snum → Sort*} (z : Π b, C (snum.zero b)) (s : Π b p, C p → C (b :: p)) : Π p : nzsnum, C p | (msb b) := by rw ←bit_one; exact s b (snum.zero (bnot b)) (z (bnot b)) | (bit b p) := s b p (drec' p) end nzsnum namespace snum open nzsnum def head : snum → bool | (zero z) := z | (nz p) := p.head def tail : snum → snum | (zero z) := zero z | (nz p) := p.tail def drec' {C : snum → Sort*} (z : Π b, C (snum.zero b)) (s : Π b p, C p → C (b :: p)) : Π p, C p | (zero b) := z b | (nz p) := p.drec' z s def rec' {α} (z : bool → α) (s : bool → snum → α → α) : snum → α := drec' z s def bits : snum → Π n, vector bool n | p 0 := vector.nil | p (n+1) := head p :: bits (tail p) n def test_bit : nat → snum → bool | 0 p := head p | (n+1) p := test_bit n (tail p) def succ : snum → snum := rec' (λ b, cond b 0 1) (λb p succp, cond b (ff :: succp) (tt :: p)) def pred : snum → snum := rec' (λ b, cond b (~1) ~0) (λb p predp, cond b (ff :: p) (tt :: predp)) protected def neg (n : snum) : snum := succ ~n instance : has_neg snum := ⟨snum.neg⟩ -- First bit is 0 or 1 (tt), second bit is 0 or -1 (tt) def czadd : bool → bool → snum → snum | ff ff p := p | ff tt p := pred p | tt ff p := succ p | tt tt p := p def cadd : snum → snum → bool → snum := rec' (λ a p c, czadd c a p) $ λa p IH, rec' (λb c, czadd c b (a :: p)) $ λb q _ c, bitvec.xor3 a b c :: IH q (bitvec.carry a b c) protected def add (a b : snum) : snum := cadd a b ff instance : has_add snum := ⟨snum.add⟩ protected def sub (a b : snum) : snum := a + -b instance : has_sub snum := ⟨snum.sub⟩ protected def mul (a : snum) : snum → snum := rec' (λ b, cond b (-a) 0) $ λb q IH, cond b (bit0 IH + a) (bit0 IH) instance : has_mul snum := ⟨snum.mul⟩ end snum namespace int def of_snum : snum → ℤ := snum.rec' (λ a, cond a (-1) 0) (λa p IH, cond a (bit1 IH) (bit0 IH)) instance snum_coe : has_coe snum ℤ := ⟨of_snum⟩ end int instance : has_lt snum := ⟨λa b, (a : ℤ) < b⟩ instance : has_le snum := ⟨λa b, (a : ℤ) ≤ b⟩
21499b7987c8248bf83bd622b6c899956fd42727
92b50235facfbc08dfe7f334827d47281471333b
/library/data/nat/basic.lean
c3ef4904a4efd74197bf3e448226c13acc3182a9
[ "Apache-2.0" ]
permissive
htzh/lean
24f6ed7510ab637379ec31af406d12584d31792c
d70c79f4e30aafecdfc4a60b5d3512199200ab6e
refs/heads/master
1,607,677,731,270
1,437,089,952,000
1,437,089,952,000
37,078,816
0
0
null
1,433,780,956,000
1,433,780,955,000
null
UTF-8
Lean
false
false
10,515
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad Basic operations on the natural numbers. -/ import logic.connectives data.num algebra.binary algebra.ring open binary eq.ops namespace nat /- a variant of add, defined by recursion on the first argument -/ definition addl (x y : ℕ) : ℕ := nat.rec y (λ n r, succ r) x infix `⊕`:65 := addl theorem addl_succ_right (n m : ℕ) : n ⊕ succ m = succ (n ⊕ m) := nat.induction_on n rfl (λ n₁ ih, calc succ n₁ ⊕ succ m = succ (n₁ ⊕ succ m) : rfl ... = succ (succ (n₁ ⊕ m)) : ih ... = succ (succ n₁ ⊕ m) : rfl) theorem add_eq_addl (x : ℕ) : ∀y, x + y = x ⊕ y := nat.induction_on x (λ y, nat.induction_on y rfl (λ y₁ ih, calc zero + succ y₁ = succ (zero + y₁) : rfl ... = succ (zero ⊕ y₁) : {ih} ... = zero ⊕ (succ y₁) : rfl)) (λ x₁ ih₁ y, nat.induction_on y (calc succ x₁ + zero = succ (x₁ + zero) : rfl ... = succ (x₁ ⊕ zero) : {ih₁ zero} ... = succ x₁ ⊕ zero : rfl) (λ y₁ ih₂, calc succ x₁ + succ y₁ = succ (succ x₁ + y₁) : rfl ... = succ (succ x₁ ⊕ y₁) : {ih₂} ... = succ x₁ ⊕ succ y₁ : addl_succ_right)) /- successor and predecessor -/ theorem succ_ne_zero (n : ℕ) : succ n ≠ 0 := by contradiction -- add_rewrite succ_ne_zero theorem pred_zero [rewrite] : pred 0 = 0 := rfl theorem pred_succ [rewrite] (n : ℕ) : pred (succ n) = n := rfl theorem eq_zero_or_eq_succ_pred (n : ℕ) : n = 0 ∨ n = succ (pred n) := nat.induction_on n (or.inl rfl) (take m IH, or.inr (show succ m = succ (pred (succ m)), from congr_arg succ !pred_succ⁻¹)) theorem exists_eq_succ_of_ne_zero {n : ℕ} (H : n ≠ 0) : ∃k : ℕ, n = succ k := exists.intro _ (or_resolve_right !eq_zero_or_eq_succ_pred H) theorem succ.inj {n m : ℕ} (H : succ n = succ m) : n = m := nat.no_confusion H (λe, e) abbreviation eq_of_succ_eq_succ := @succ.inj theorem succ_ne_self {n : ℕ} : succ n ≠ n := nat.induction_on n (take H : 1 = 0, have ne : 1 ≠ 0, from !succ_ne_zero, absurd H ne) (take k IH H, IH (succ.inj H)) theorem discriminate {B : Prop} {n : ℕ} (H1: n = 0 → B) (H2 : ∀m, n = succ m → B) : B := have H : n = n → B, from nat.cases_on n H1 H2, H rfl theorem two_step_induction_on {P : ℕ → Prop} (a : ℕ) (H1 : P 0) (H2 : P 1) (H3 : ∀ (n : ℕ) (IH1 : P n) (IH2 : P (succ n)), P (succ (succ n))) : P a := have stronger : P a ∧ P (succ a), from nat.induction_on a (and.intro H1 H2) (take k IH, have IH1 : P k, from and.elim_left IH, have IH2 : P (succ k), from and.elim_right IH, and.intro IH2 (H3 k IH1 IH2)), and.elim_left stronger theorem sub_induction {P : ℕ → ℕ → Prop} (n m : ℕ) (H1 : ∀m, P 0 m) (H2 : ∀n, P (succ n) 0) (H3 : ∀n m, P n m → P (succ n) (succ m)) : P n m := have general : ∀m, P n m, from nat.induction_on n (take m : ℕ, H1 m) (take k : ℕ, assume IH : ∀m, P k m, take m : ℕ, nat.cases_on m (H2 k) (take l, (H3 k l (IH l)))), general m /- addition -/ theorem add_zero [rewrite] (n : ℕ) : n + 0 = n := rfl theorem add_succ [rewrite] (n m : ℕ) : n + succ m = succ (n + m) := rfl theorem zero_add [rewrite] (n : ℕ) : 0 + n = n := nat.induction_on n !add_zero (take m IH, show 0 + succ m = succ m, from calc 0 + succ m = succ (0 + m) : add_succ ... = succ m : IH) theorem succ_add [rewrite] (n m : ℕ) : (succ n) + m = succ (n + m) := nat.induction_on m (!add_zero ▸ !add_zero) (take k IH, calc succ n + succ k = succ (succ n + k) : add_succ ... = succ (succ (n + k)) : IH ... = succ (n + succ k) : add_succ) theorem add.comm [rewrite] (n m : ℕ) : n + m = m + n := nat.induction_on m (!add_zero ⬝ !zero_add⁻¹) (take k IH, calc n + succ k = succ (n+k) : add_succ ... = succ (k + n) : IH ... = succ k + n : succ_add) theorem succ_add_eq_succ_add (n m : ℕ) : succ n + m = n + succ m := !succ_add ⬝ !add_succ⁻¹ theorem add.assoc [rewrite] (n m k : ℕ) : (n + m) + k = n + (m + k) := nat.induction_on k (!add_zero ▸ !add_zero) (take l IH, calc (n + m) + succ l = succ ((n + m) + l) : add_succ ... = succ (n + (m + l)) : IH ... = n + succ (m + l) : add_succ ... = n + (m + succ l) : add_succ) theorem add.left_comm [rewrite] (n m k : ℕ) : n + (m + k) = m + (n + k) := left_comm add.comm add.assoc n m k theorem add.right_comm (n m k : ℕ) : n + m + k = n + k + m := right_comm add.comm add.assoc n m k theorem add.cancel_left {n m k : ℕ} : n + m = n + k → m = k := nat.induction_on n (take H : 0 + m = 0 + k, !zero_add⁻¹ ⬝ H ⬝ !zero_add) (take (n : ℕ) (IH : n + m = n + k → m = k) (H : succ n + m = succ n + k), have H2 : succ (n + m) = succ (n + k), from calc succ (n + m) = succ n + m : succ_add ... = succ n + k : H ... = succ (n + k) : succ_add, have H3 : n + m = n + k, from succ.inj H2, IH H3) theorem add.cancel_right {n m k : ℕ} (H : n + m = k + m) : n = k := have H2 : m + n = m + k, from !add.comm ⬝ H ⬝ !add.comm, add.cancel_left H2 theorem eq_zero_of_add_eq_zero_right {n m : ℕ} : n + m = 0 → n = 0 := nat.induction_on n (take (H : 0 + m = 0), rfl) (take k IH, assume H : succ k + m = 0, absurd (show succ (k + m) = 0, from calc succ (k + m) = succ k + m : succ_add ... = 0 : H) !succ_ne_zero) theorem eq_zero_of_add_eq_zero_left {n m : ℕ} (H : n + m = 0) : m = 0 := eq_zero_of_add_eq_zero_right (!add.comm ⬝ H) theorem eq_zero_and_eq_zero_of_add_eq_zero {n m : ℕ} (H : n + m = 0) : n = 0 ∧ m = 0 := and.intro (eq_zero_of_add_eq_zero_right H) (eq_zero_of_add_eq_zero_left H) theorem add_one [rewrite] (n : ℕ) : n + 1 = succ n := !add_zero ▸ !add_succ theorem one_add (n : ℕ) : 1 + n = succ n := !zero_add ▸ !succ_add /- multiplication -/ theorem mul_zero [rewrite] (n : ℕ) : n * 0 = 0 := rfl theorem mul_succ [rewrite] (n m : ℕ) : n * succ m = n * m + n := rfl -- commutativity, distributivity, associativity, identity theorem zero_mul [rewrite] (n : ℕ) : 0 * n = 0 := nat.induction_on n !mul_zero (take m IH, !mul_succ ⬝ !add_zero ⬝ IH) theorem succ_mul [rewrite] (n m : ℕ) : (succ n) * m = (n * m) + m := nat.induction_on m (!mul_zero ⬝ !mul_zero⁻¹ ⬝ !add_zero⁻¹) (take k IH, calc succ n * succ k = succ n * k + succ n : mul_succ ... = n * k + k + succ n : IH ... = n * k + (k + succ n) : add.assoc ... = n * k + (succ n + k) : add.comm ... = n * k + (n + succ k) : succ_add_eq_succ_add ... = n * k + n + succ k : add.assoc ... = n * succ k + succ k : mul_succ) theorem mul.comm [rewrite] (n m : ℕ) : n * m = m * n := nat.induction_on m (!mul_zero ⬝ !zero_mul⁻¹) (take k IH, calc n * succ k = n * k + n : mul_succ ... = k * n + n : IH ... = (succ k) * n : succ_mul) theorem mul.right_distrib (n m k : ℕ) : (n + m) * k = n * k + m * k := nat.induction_on k (calc (n + m) * 0 = 0 : mul_zero ... = 0 + 0 : add_zero ... = n * 0 + 0 : mul_zero ... = n * 0 + m * 0 : mul_zero) (take l IH, calc (n + m) * succ l = (n + m) * l + (n + m) : mul_succ ... = n * l + m * l + (n + m) : IH ... = n * l + m * l + n + m : add.assoc ... = n * l + n + m * l + m : add.right_comm ... = n * l + n + (m * l + m) : add.assoc ... = n * succ l + (m * l + m) : mul_succ ... = n * succ l + m * succ l : mul_succ) theorem mul.left_distrib (n m k : ℕ) : n * (m + k) = n * m + n * k := calc n * (m + k) = (m + k) * n : mul.comm ... = m * n + k * n : mul.right_distrib ... = n * m + k * n : mul.comm ... = n * m + n * k : mul.comm theorem mul.assoc [rewrite] (n m k : ℕ) : (n * m) * k = n * (m * k) := nat.induction_on k (calc (n * m) * 0 = n * (m * 0) : mul_zero) (take l IH, calc (n * m) * succ l = (n * m) * l + n * m : mul_succ ... = n * (m * l) + n * m : IH ... = n * (m * l + m) : mul.left_distrib ... = n * (m * succ l) : mul_succ) theorem mul_one [rewrite] (n : ℕ) : n * 1 = n := calc n * 1 = n * 0 + n : mul_succ ... = 0 + n : mul_zero ... = n : zero_add theorem one_mul [rewrite] (n : ℕ) : 1 * n = n := calc 1 * n = n * 1 : mul.comm ... = n : mul_one theorem eq_zero_or_eq_zero_of_mul_eq_zero {n m : ℕ} : n * m = 0 → n = 0 ∨ m = 0 := nat.cases_on n (assume H, or.inl rfl) (take n', nat.cases_on m (assume H, or.inr rfl) (take m', assume H : succ n' * succ m' = 0, absurd ((calc 0 = succ n' * succ m' : H ... = succ n' * m' + succ n' : mul_succ ... = succ (succ n' * m' + n') : add_succ)⁻¹) !succ_ne_zero)) section migrate_algebra open [classes] algebra protected definition comm_semiring [reducible] : algebra.comm_semiring nat := ⦃algebra.comm_semiring, add := add, add_assoc := add.assoc, zero := zero, zero_add := zero_add, add_zero := add_zero, add_comm := add.comm, mul := mul, mul_assoc := mul.assoc, one := succ zero, one_mul := one_mul, mul_one := mul_one, left_distrib := mul.left_distrib, right_distrib := mul.right_distrib, zero_mul := zero_mul, mul_zero := mul_zero, mul_comm := mul.comm⦄ local attribute nat.comm_semiring [instance] definition dvd (a b : ℕ) : Prop := algebra.dvd a b notation a ∣ b := dvd a b migrate from algebra with nat replacing dvd → dvd end migrate_algebra end nat
b6716c4045a7a9482436a5483a22cb9e12ca1951
491068d2ad28831e7dade8d6dff871c3e49d9431
/hott/algebra/category/adjoint.hlean
20ffa0a86e80204a217d29a0d24786903c459097
[ "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
12,497
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 -/ import algebra.category.constructions function arity open category functor nat_trans eq is_trunc iso equiv prod trunc function pi is_equiv namespace category variables {C D : Precategory} {F : C ⇒ D} {G : D ⇒ C} -- TODO: define a structure "adjoint" and then define -- structure is_left_adjoint (F : C ⇒ D) := -- (G : D ⇒ C) -- G -- (is_adjoint : adjoint F G) structure is_left_adjoint [class] (F : C ⇒ D) := (G : D ⇒ C) (η : 1 ⟹ G ∘f F) (ε : F ∘f G ⟹ 1) (H : Π(c : C), ε (F c) ∘ F (η c) = ID (F c)) (K : Π(d : D), G (ε d) ∘ η (G d) = ID (G d)) abbreviation right_adjoint := @is_left_adjoint.G abbreviation unit := @is_left_adjoint.η abbreviation counit := @is_left_adjoint.ε structure is_equivalence [class] (F : C ⇒ D) extends is_left_adjoint F := mk' :: (is_iso_unit : is_iso η) (is_iso_counit : is_iso ε) abbreviation inverse := @is_equivalence.G postfix ⁻¹ := inverse --a second notation for the inverse, which is not overloaded postfix [parsing-only] `⁻¹F`:std.prec.max_plus := inverse --TODO: review and change definition faithful [class] (F : C ⇒ D) := Π⦃c c' : C⦄ ⦃f f' : c ⟶ c'⦄, F f = F f' → f = f' definition full [class] (F : C ⇒ D) := Π⦃c c' : C⦄, is_surjective (@(to_fun_hom F) c c') definition fully_faithful [class] (F : C ⇒ D) := Π(c c' : C), is_equiv (@(to_fun_hom F) c c') definition split_essentially_surjective [class] (F : C ⇒ D) := Π(d : D), Σ(c : C), F c ≅ d definition essentially_surjective [class] (F : C ⇒ D) := Π(d : D), ∃(c : C), F c ≅ d definition is_weak_equivalence [class] (F : C ⇒ D) := fully_faithful F × essentially_surjective F definition is_isomorphism [class] (F : C ⇒ D) := fully_faithful F × is_equiv (to_fun_ob F) structure equivalence (C D : Precategory) := (to_functor : C ⇒ D) (struct : is_equivalence to_functor) structure isomorphism (C D : Precategory) := (to_functor : C ⇒ D) (struct : is_isomorphism to_functor) -- infix `⊣`:55 := adjoint infix ` ⋍ `:25 := equivalence -- \backsimeq or \equiv infix ` ≌ `:25 := isomorphism -- \backcong or \iso definition is_equiv_of_fully_faithful [instance] [reducible] (F : C ⇒ D) [H : fully_faithful F] (c c' : C) : is_equiv (@(to_fun_hom F) c c') := !H definition hom_inv [reducible] (F : C ⇒ D) [H : fully_faithful F] (c c' : C) (f : F c ⟶ F c') : c ⟶ c' := (to_fun_hom F)⁻¹ᶠ f definition hom_equiv_F_hom_F [constructor] (F : C ⇒ D) [H : fully_faithful F] (c c' : C) : (c ⟶ c') ≃ (F c ⟶ F c') := equiv.mk _ !H definition iso_of_F_iso_F (F : C ⇒ D) [H : fully_faithful F] (c c' : C) (g : F c ≅ F c') : c ≅ c' := begin induction g with g G, induction G with h p q, fapply iso.MK, { rexact (to_fun_hom F)⁻¹ᶠ g}, { rexact (to_fun_hom F)⁻¹ᶠ h}, { exact abstract begin apply eq_of_fn_eq_fn' (to_fun_hom F), rewrite [respect_comp, respect_id, right_inv (to_fun_hom F), right_inv (to_fun_hom F), p], end end}, { exact abstract begin apply eq_of_fn_eq_fn' (to_fun_hom F), rewrite [respect_comp, respect_id, right_inv (to_fun_hom F), right_inv (@(to_fun_hom F) c' c), q], end end} end definition iso_equiv_F_iso_F [constructor] (F : C ⇒ D) [H : fully_faithful F] (c c' : C) : (c ≅ c') ≃ (F c ≅ F c') := begin fapply equiv.MK, { exact to_fun_iso F}, { apply iso_of_F_iso_F}, { exact abstract begin intro f, induction f with f F', induction F' with g p q, apply iso_eq, esimp [iso_of_F_iso_F], apply right_inv end end}, { exact abstract begin intro f, induction f with f F', induction F' with g p q, apply iso_eq, esimp [iso_of_F_iso_F], apply right_inv end end}, end definition is_iso_unit [instance] (F : C ⇒ D) [H : is_equivalence F] : is_iso (unit F) := !is_equivalence.is_iso_unit definition is_iso_counit [instance] (F : C ⇒ D) [H : is_equivalence F] : is_iso (counit F) := !is_equivalence.is_iso_counit theorem is_hprop_is_left_adjoint {C : Category} {D : Precategory} (F : C ⇒ D) : is_hprop (is_left_adjoint F) := begin apply is_hprop.mk, intro G G', cases G with G η ε H K, cases G' with G' η' ε' H' K', assert lem₁ : Π(p : G = G'), p ▸ η = η' → p ▸ ε = ε' → is_left_adjoint.mk G η ε H K = is_left_adjoint.mk G' η' ε' H' K', { intros p q r, induction p, induction q, induction r, esimp, apply apd011 (is_left_adjoint.mk G η ε) !is_hprop.elim !is_hprop.elim}, assert lem₂ : Π (d : carrier D), (to_fun_hom G (natural_map ε' d) ∘ natural_map η (to_fun_ob G' d)) ∘ to_fun_hom G' (natural_map ε d) ∘ natural_map η' (to_fun_ob G d) = id, { intro d, esimp, rewrite [assoc], rewrite [-assoc (G (ε' d))], esimp, rewrite [nf_fn_eq_fn_nf_pt' G' ε η d], esimp, rewrite [assoc], esimp, rewrite [-assoc], rewrite [↑functor.compose, -respect_comp G], rewrite [nf_fn_eq_fn_nf_pt ε ε' d,nf_fn_eq_fn_nf_pt η' η (G d),▸*], rewrite [respect_comp G], rewrite [assoc,▸*,-assoc (G (ε d))], rewrite [↑functor.compose, -respect_comp G], rewrite [H' (G d)], rewrite [respect_id,▸*,id_right], apply K}, assert lem₃ : Π (d : carrier D), (to_fun_hom G' (natural_map ε d) ∘ natural_map η' (to_fun_ob G d)) ∘ to_fun_hom G (natural_map ε' d) ∘ natural_map η (to_fun_ob G' d) = id, { intro d, esimp, rewrite [assoc, -assoc (G' (ε d))], esimp, rewrite [nf_fn_eq_fn_nf_pt' G ε' η' d], esimp, rewrite [assoc], esimp, rewrite [-assoc], rewrite [↑functor.compose, -respect_comp G'], rewrite [nf_fn_eq_fn_nf_pt ε' ε d,nf_fn_eq_fn_nf_pt η η' (G' d)], esimp, rewrite [respect_comp G'], rewrite [assoc,▸*,-assoc (G' (ε' d))], rewrite [↑functor.compose, -respect_comp G'], rewrite [H (G' d)], rewrite [respect_id,▸*,id_right], apply K'}, fapply lem₁, { fapply functor.eq_of_pointwise_iso, { fapply change_natural_map, { exact (G' ∘fn1 ε) ∘n !assoc_natural_rev ∘n (η' ∘1nf G)}, { intro d, exact (G' (ε d) ∘ η' (G d))}, { intro d, exact ap (λx, _ ∘ x) !id_left}}, { intro d, fconstructor, { exact (G (ε' d) ∘ η (G' d))}, { exact lem₂ d }, { exact lem₃ d }}}, { clear lem₁, refine transport_hom_of_eq_right _ η ⬝ _, krewrite hom_of_eq_compose_right, rewrite functor.hom_of_eq_eq_of_pointwise_iso, apply nat_trans_eq, intro c, esimp, refine !assoc⁻¹ ⬝ ap (λx, _ ∘ x) (nf_fn_eq_fn_nf_pt η η' c) ⬝ !assoc ⬝ _, esimp, rewrite [-respect_comp G',H c,respect_id G',▸*,id_left]}, { clear lem₁, refine transport_hom_of_eq_left _ ε ⬝ _, krewrite inv_of_eq_compose_left, rewrite functor.inv_of_eq_eq_of_pointwise_iso, apply nat_trans_eq, intro d, esimp, krewrite [respect_comp], rewrite [assoc,nf_fn_eq_fn_nf_pt ε' ε d,-assoc,▸*,H (G' d),id_right]} end definition full_of_fully_faithful (H : fully_faithful F) : full F := λc c' g, tr (fiber.mk ((@(to_fun_hom F) c c')⁻¹ᶠ g) !right_inv) definition faithful_of_fully_faithful (H : fully_faithful F) : faithful F := λc c' f f' p, is_injective_of_is_embedding p definition fully_faithful_of_full_of_faithful (H : faithful F) (K : full F) : fully_faithful F := begin intro c c', apply is_equiv_of_is_surjective_of_is_embedding, { apply is_embedding_of_is_injective, intros f f' p, exact H p}, { apply K} end definition split_essentially_surjective_of_is_equivalence (F : C ⇒ D) [H : is_equivalence F] : split_essentially_surjective F := begin intro d, fconstructor, { exact F⁻¹ d}, { exact componentwise_iso (@(iso.mk (counit F)) !is_iso_counit) d} end definition reflect_is_iso [constructor] (F : C ⇒ D) [H : fully_faithful F] {c c' : C} (f : c ⟶ c') [H : is_iso (F f)] : is_iso f := begin fconstructor, { exact (to_fun_hom F)⁻¹ᶠ (F f)⁻¹}, { apply eq_of_fn_eq_fn' (to_fun_hom F), rewrite [respect_comp,right_inv (to_fun_hom F),respect_id,left_inverse]}, { apply eq_of_fn_eq_fn' (to_fun_hom F), rewrite [respect_comp,right_inv (to_fun_hom F),respect_id,right_inverse]}, end definition reflect_iso [constructor] (F : C ⇒ D) [H : fully_faithful F] {c c' : C} (f : F c ≅ F c') : c ≅ c' := begin fconstructor, { exact (to_fun_hom F)⁻¹ᶠ f}, { assert H : is_iso (F ((to_fun_hom F)⁻¹ᶠ f)), { have H' : is_iso (to_hom f), from _, exact (right_inv (to_fun_hom F) (to_hom f))⁻¹ ▸ H'}, exact reflect_is_iso F _}, end theorem reflect_inverse (F : C ⇒ D) [H : fully_faithful F] {c c' : C} (f : c ⟶ c') [H : is_iso f] : (to_fun_hom F)⁻¹ᶠ (F f)⁻¹ = f⁻¹ := inverse_eq_inverse (idp : to_hom (@(iso.mk f) (reflect_is_iso F f)) = f) /- section variables (η : Πc, G (F c) ≅ c) (ε : Πd, F (G d) ≅ d) -- we need some kind of naturality include η ε --definition inverse_of_unit_counit private definition adj_η (c : C) : G (F c) ≅ c := to_fun_iso G (to_fun_iso F (η c)⁻¹ⁱ) ⬝i to_fun_iso G (ε (F c)) ⬝i η c open iso private theorem adjointify_adjH (c : C) : to_hom (ε (F c)) ∘ F (to_hom (adj_η η ε c)⁻¹ⁱ) = id := begin exact sorry end private theorem adjointify_adjK (d : D) : G (to_hom (ε d)) ∘ to_hom (adj_η η ε (G d))⁻¹ⁱ = id := begin exact sorry end variables (F G) definition is_equivalence.mk : is_equivalence F := begin fconstructor, { exact G}, { } end end definition fully_faithful_of_is_equivalence (F : C ⇒ D) [H : is_equivalence F] : fully_faithful F := begin intro c c', fapply adjointify, { intro g, exact natural_map (@(iso.inverse (unit F)) !is_iso_unit) c' ∘ F⁻¹ g ∘ unit F c}, { intro g, rewrite [+respect_comp,▸*], krewrite [natural_map_inverse], xrewrite [respect_inv'], apply inverse_comp_eq_of_eq_comp, exact sorry /-this is basically the naturality of the counit-/ }, { exact sorry}, end definition fully_faithful_equiv (F : C ⇒ D) : fully_faithful F ≃ (faithful F × full F) := sorry definition is_equivalence_equiv (F : C ⇒ D) : is_equivalence F ≃ (fully_faithful F × split_essentially_surjective F) := sorry definition is_hprop_is_weak_equivalence (F : C ⇒ D) : is_hprop (is_weak_equivalence F) := sorry definition is_hprop_is_equivalence {C D : Category} (F : C ⇒ D) : is_hprop (is_equivalence F) := sorry definition is_equivalence_equiv_is_weak_equivalence {C D : Category} (F : C ⇒ D) : is_equivalence F ≃ is_weak_equivalence F := sorry definition is_hprop_is_isomorphism (F : C ⇒ D) : is_hprop (is_isomorphism F) := sorry definition is_isomorphism_equiv1 (F : C ⇒ D) : is_equivalence F ≃ Σ(G : D ⇒ C) (η : 1 = G ∘f F) (ε : F ∘f G = 1), sorry ▸ ap (λ(H : C ⇒ C), F ∘f H) η = ap (λ(H : D ⇒ D), H ∘f F) ε⁻¹ := sorry definition is_isomorphism_equiv2 (F : C ⇒ D) : is_equivalence F ≃ ∃(G : D ⇒ C), 1 = G ∘f F × F ∘f G = 1 := sorry definition is_equivalence_of_isomorphism (H : is_isomorphism F) : is_equivalence F := sorry definition is_isomorphism_of_is_equivalence {C D : Category} {F : C ⇒ D} (H : is_equivalence F) : is_isomorphism F := sorry definition isomorphism_of_eq {C D : Precategory} (p : C = D) : C ≌ D := sorry definition is_equiv_isomorphism_of_eq (C D : Precategory) : is_equiv (@isomorphism_of_eq C D) := sorry definition equivalence_of_eq {C D : Precategory} (p : C = D) : C ⋍ D := sorry definition is_equiv_equivalence_of_eq (C D : Category) : is_equiv (@equivalence_of_eq C D) := sorry -/ end category
09a6187e7011d6b96ca5ab9088ea9669c3e642f4
1446f520c1db37e157b631385707cc28a17a595e
/stage0/src/Init/Data/UInt.lean
c2c3ef5e3707bdf4ab391be9d88ff9ace443e3ab
[ "Apache-2.0" ]
permissive
bdbabiak/lean4
cab06b8a2606d99a168dd279efdd404edb4e825a
3f4d0d78b2ce3ef541cb643bbe21496bd6b057ac
refs/heads/master
1,615,045,275,530
1,583,793,696,000
1,583,793,696,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,979
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Data.Fin.Basic import Init.System.Platform open Nat def uint8Sz : Nat := 256 structure UInt8 := (val : Fin uint8Sz) @[extern "lean_uint8_of_nat"] def UInt8.ofNat (n : @& Nat) : UInt8 := ⟨Fin.ofNat n⟩ abbrev Nat.toUInt8 := UInt8.ofNat @[extern "lean_uint8_to_nat"] def UInt8.toNat (n : UInt8) : Nat := n.val.val @[extern c inline "#1 + #2"] def UInt8.add (a b : UInt8) : UInt8 := ⟨a.val + b.val⟩ @[extern c inline "#1 - #2"] def UInt8.sub (a b : UInt8) : UInt8 := ⟨a.val - b.val⟩ @[extern c inline "#1 * #2"] def UInt8.mul (a b : UInt8) : UInt8 := ⟨a.val * b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 / #2"] def UInt8.div (a b : UInt8) : UInt8 := ⟨a.val / b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 % #2"] def UInt8.mod (a b : UInt8) : UInt8 := ⟨a.val % b.val⟩ @[extern "lean_uint8_modn"] def UInt8.modn (a : UInt8) (n : @& Nat) : UInt8 := ⟨a.val %ₙ n⟩ @[extern c inline "#1 & #2"] def UInt8.land (a b : UInt8) : UInt8 := ⟨Fin.land a.val b.val⟩ @[extern c inline "#1 | #2"] def UInt8.lor (a b : UInt8) : UInt8 := ⟨Fin.lor a.val b.val⟩ def UInt8.lt (a b : UInt8) : Prop := a.val < b.val def UInt8.le (a b : UInt8) : Prop := a.val ≤ b.val instance : HasZero UInt8 := ⟨UInt8.ofNat 0⟩ instance : HasOne UInt8 := ⟨UInt8.ofNat 1⟩ instance : HasOfNat UInt8 := ⟨UInt8.ofNat⟩ instance : HasAdd UInt8 := ⟨UInt8.add⟩ instance : HasSub UInt8 := ⟨UInt8.sub⟩ instance : HasMul UInt8 := ⟨UInt8.mul⟩ instance : HasMod UInt8 := ⟨UInt8.mod⟩ instance : HasModN UInt8 := ⟨UInt8.modn⟩ instance : HasDiv UInt8 := ⟨UInt8.div⟩ instance : HasLess UInt8 := ⟨UInt8.lt⟩ instance : HasLessEq UInt8 := ⟨UInt8.le⟩ instance : Inhabited UInt8 := ⟨0⟩ @[extern c inline "#1 == #2"] def UInt8.decEq (a b : UInt8) : Decidable (a = b) := UInt8.casesOn a $ fun n => UInt8.casesOn b $ fun m => if h : n = m then isTrue (h ▸ rfl) else isFalse (fun h' => UInt8.noConfusion h' (fun h' => absurd h' h)) @[extern c inline "#1 < #2"] def UInt8.decLt (a b : UInt8) : Decidable (a < b) := UInt8.casesOn a $ fun n => UInt8.casesOn b $ fun m => inferInstanceAs (Decidable (n < m)) @[extern c inline "#1 <= #2"] def UInt8.decLe (a b : UInt8) : Decidable (a ≤ b) := UInt8.casesOn a $ fun n => UInt8.casesOn b $ fun m => inferInstanceAs (Decidable (n <= m)) instance : DecidableEq UInt8 := UInt8.decEq instance UInt8.hasDecidableLt (a b : UInt8) : Decidable (a < b) := UInt8.decLt a b instance UInt8.hasDecidableLe (a b : UInt8) : Decidable (a ≤ b) := UInt8.decLe a b def uint16Sz : Nat := 65536 structure UInt16 := (val : Fin uint16Sz) @[extern "lean_uint16_of_nat"] def UInt16.ofNat (n : @& Nat) : UInt16 := ⟨Fin.ofNat n⟩ abbrev Nat.toUInt16 := UInt16.ofNat @[extern "lean_uint16_to_nat"] def UInt16.toNat (n : UInt16) : Nat := n.val.val @[extern c inline "#1 + #2"] def UInt16.add (a b : UInt16) : UInt16 := ⟨a.val + b.val⟩ @[extern c inline "#1 - #2"] def UInt16.sub (a b : UInt16) : UInt16 := ⟨a.val - b.val⟩ @[extern c inline "#1 * #2"] def UInt16.mul (a b : UInt16) : UInt16 := ⟨a.val * b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 / #2"] def UInt16.div (a b : UInt16) : UInt16 := ⟨a.val / b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 % #2"] def UInt16.mod (a b : UInt16) : UInt16 := ⟨a.val % b.val⟩ @[extern "lean_uint16_modn"] def UInt16.modn (a : UInt16) (n : @& Nat) : UInt16 := ⟨a.val %ₙ n⟩ @[extern c inline "#1 & #2"] def UInt16.land (a b : UInt16) : UInt16 := ⟨Fin.land a.val b.val⟩ @[extern c inline "#1 | #2"] def UInt16.lor (a b : UInt16) : UInt16 := ⟨Fin.lor a.val b.val⟩ def UInt16.lt (a b : UInt16) : Prop := a.val < b.val def UInt16.le (a b : UInt16) : Prop := a.val ≤ b.val instance : HasZero UInt16 := ⟨UInt16.ofNat 0⟩ instance : HasOne UInt16 := ⟨UInt16.ofNat 1⟩ instance : HasOfNat UInt16 := ⟨UInt16.ofNat⟩ instance : HasAdd UInt16 := ⟨UInt16.add⟩ instance : HasSub UInt16 := ⟨UInt16.sub⟩ instance : HasMul UInt16 := ⟨UInt16.mul⟩ instance : HasMod UInt16 := ⟨UInt16.mod⟩ instance : HasModN UInt16 := ⟨UInt16.modn⟩ instance : HasDiv UInt16 := ⟨UInt16.div⟩ instance : HasLess UInt16 := ⟨UInt16.lt⟩ instance : HasLessEq UInt16 := ⟨UInt16.le⟩ instance : Inhabited UInt16 := ⟨0⟩ @[extern c inline "#1 == #2"] def UInt16.decEq (a b : UInt16) : Decidable (a = b) := UInt16.casesOn a $ fun n => UInt16.casesOn b $ fun m => if h : n = m then isTrue (h ▸ rfl) else isFalse (fun h' => UInt16.noConfusion h' (fun h' => absurd h' h)) @[extern c inline "#1 < #2"] def UInt16.decLt (a b : UInt16) : Decidable (a < b) := UInt16.casesOn a $ fun n => UInt16.casesOn b $ fun m => inferInstanceAs (Decidable (n < m)) @[extern c inline "#1 <= #2"] def UInt16.decLe (a b : UInt16) : Decidable (a ≤ b) := UInt16.casesOn a $ fun n => UInt16.casesOn b $ fun m => inferInstanceAs (Decidable (n <= m)) instance : DecidableEq UInt16 := UInt16.decEq instance UInt16.hasDecidableLt (a b : UInt16) : Decidable (a < b) := UInt16.decLt a b instance UInt16.hasDecidableLe (a b : UInt16) : Decidable (a ≤ b) := UInt16.decLe a b def uint32Sz : Nat := 4294967296 structure UInt32 := (val : Fin uint32Sz) @[extern "lean_uint32_of_nat"] def UInt32.ofNat (n : @& Nat) : UInt32 := ⟨Fin.ofNat n⟩ abbrev Nat.toUInt32 := UInt32.ofNat @[extern "lean_uint32_to_nat"] def UInt32.toNat (n : UInt32) : Nat := n.val.val @[extern c inline "#1 + #2"] def UInt32.add (a b : UInt32) : UInt32 := ⟨a.val + b.val⟩ @[extern c inline "#1 - #2"] def UInt32.sub (a b : UInt32) : UInt32 := ⟨a.val - b.val⟩ @[extern c inline "#1 * #2"] def UInt32.mul (a b : UInt32) : UInt32 := ⟨a.val * b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 / #2"] def UInt32.div (a b : UInt32) : UInt32 := ⟨a.val / b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 % #2"] def UInt32.mod (a b : UInt32) : UInt32 := ⟨a.val % b.val⟩ @[extern "lean_uint32_modn"] def UInt32.modn (a : UInt32) (n : @& Nat) : UInt32 := ⟨a.val %ₙ n⟩ @[extern c inline "#1 & #2"] def UInt32.land (a b : UInt32) : UInt32 := ⟨Fin.land a.val b.val⟩ @[extern c inline "#1 | #2"] def UInt32.lor (a b : UInt32) : UInt32 := ⟨Fin.lor a.val b.val⟩ def UInt32.lt (a b : UInt32) : Prop := a.val < b.val def UInt32.le (a b : UInt32) : Prop := a.val ≤ b.val instance : HasZero UInt32 := ⟨UInt32.ofNat 0⟩ instance : HasOne UInt32 := ⟨UInt32.ofNat 1⟩ instance : HasOfNat UInt32 := ⟨UInt32.ofNat⟩ instance : HasAdd UInt32 := ⟨UInt32.add⟩ instance : HasSub UInt32 := ⟨UInt32.sub⟩ instance : HasMul UInt32 := ⟨UInt32.mul⟩ instance : HasMod UInt32 := ⟨UInt32.mod⟩ instance : HasModN UInt32 := ⟨UInt32.modn⟩ instance : HasDiv UInt32 := ⟨UInt32.div⟩ instance : HasLess UInt32 := ⟨UInt32.lt⟩ instance : HasLessEq UInt32 := ⟨UInt32.le⟩ instance : Inhabited UInt32 := ⟨0⟩ @[extern c inline "#1 == #2"] def UInt32.decEq (a b : UInt32) : Decidable (a = b) := UInt32.casesOn a $ fun n => UInt32.casesOn b $ fun m => if h : n = m then isTrue (h ▸ rfl) else isFalse (fun h' => UInt32.noConfusion h' (fun h' => absurd h' h)) @[extern c inline "#1 < #2"] def UInt32.decLt (a b : UInt32) : Decidable (a < b) := UInt32.casesOn a $ fun n => UInt32.casesOn b $ fun m => inferInstanceAs (Decidable (n < m)) @[extern c inline "#1 <= #2"] def UInt32.decLe (a b : UInt32) : Decidable (a ≤ b) := UInt32.casesOn a $ fun n => UInt32.casesOn b $ fun m => inferInstanceAs (Decidable (n <= m)) @[extern c inline "#1 << #2"] constant UInt32.shiftLeft (a b : UInt32) : UInt32 := (arbitrary Nat).toUInt32 @[extern c inline "#1 >> #2"] constant UInt32.shiftRight (a b : UInt32) : UInt32 := (arbitrary Nat).toUInt32 instance : DecidableEq UInt32 := UInt32.decEq instance UInt32.hasDecidableLt (a b : UInt32) : Decidable (a < b) := UInt32.decLt a b instance UInt32.hasDecidableLe (a b : UInt32) : Decidable (a ≤ b) := UInt32.decLe a b def uint64Sz : Nat := 18446744073709551616 structure UInt64 := (val : Fin uint64Sz) @[extern "lean_uint64_of_nat"] def UInt64.ofNat (n : @& Nat) : UInt64 := ⟨Fin.ofNat n⟩ abbrev Nat.toUInt64 := UInt64.ofNat @[extern "lean_uint64_to_nat"] def UInt64.toNat (n : UInt64) : Nat := n.val.val @[extern c inline "#1 + #2"] def UInt64.add (a b : UInt64) : UInt64 := ⟨a.val + b.val⟩ @[extern c inline "#1 - #2"] def UInt64.sub (a b : UInt64) : UInt64 := ⟨a.val - b.val⟩ @[extern c inline "#1 * #2"] def UInt64.mul (a b : UInt64) : UInt64 := ⟨a.val * b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 / #2"] def UInt64.div (a b : UInt64) : UInt64 := ⟨a.val / b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 % #2"] def UInt64.mod (a b : UInt64) : UInt64 := ⟨a.val % b.val⟩ @[extern "lean_uint64_modn"] def UInt64.modn (a : UInt64) (n : @& Nat) : UInt64 := ⟨a.val %ₙ n⟩ @[extern c inline "#1 & #2"] def UInt64.land (a b : UInt64) : UInt64 := ⟨Fin.land a.val b.val⟩ @[extern c inline "#1 | #2"] def UInt64.lor (a b : UInt64) : UInt64 := ⟨Fin.lor a.val b.val⟩ def UInt64.lt (a b : UInt64) : Prop := a.val < b.val def UInt64.le (a b : UInt64) : Prop := a.val ≤ b.val @[extern c inline "((uint8_t)#1)"] def UInt64.toUInt8 (a : UInt64) : UInt8 := a.toNat.toUInt8 @[extern c inline "((uint16_t)#1)"] def UInt64.toUInt16 (a : UInt64) : UInt16 := a.toNat.toUInt16 @[extern c inline "((uint32_t)#1)"] def UInt64.toUInt32 (a : UInt64) : UInt32 := a.toNat.toUInt32 @[extern c inline "((uint64_t)#1)"] def UInt32.toUInt64 (a : UInt32) : UInt64 := a.toNat.toUInt64 -- TODO(Leo): give reference implementation for shiftLeft and shiftRight, and define them for other UInt types @[extern c inline "#1 << #2"] constant UInt64.shiftLeft (a b : UInt64) : UInt64 := (arbitrary Nat).toUInt64 @[extern c inline "#1 >> #2"] constant UInt64.shiftRight (a b : UInt64) : UInt64 := (arbitrary Nat).toUInt64 instance : HasZero UInt64 := ⟨UInt64.ofNat 0⟩ instance : HasOne UInt64 := ⟨UInt64.ofNat 1⟩ instance : HasOfNat UInt64 := ⟨UInt64.ofNat⟩ instance : HasAdd UInt64 := ⟨UInt64.add⟩ instance : HasSub UInt64 := ⟨UInt64.sub⟩ instance : HasMul UInt64 := ⟨UInt64.mul⟩ instance : HasMod UInt64 := ⟨UInt64.mod⟩ instance : HasModN UInt64 := ⟨UInt64.modn⟩ instance : HasDiv UInt64 := ⟨UInt64.div⟩ instance : HasLess UInt64 := ⟨UInt64.lt⟩ instance : HasLessEq UInt64 := ⟨UInt64.le⟩ instance : Inhabited UInt64 := ⟨0⟩ @[extern c inline "(uint64_t)#1"] def Bool.toUInt64 (b : Bool) : UInt64 := if b then 1 else 0 @[extern c inline "#1 == #2"] def UInt64.decEq (a b : UInt64) : Decidable (a = b) := UInt64.casesOn a $ fun n => UInt64.casesOn b $ fun m => if h : n = m then isTrue (h ▸ rfl) else isFalse (fun h' => UInt64.noConfusion h' (fun h' => absurd h' h)) @[extern c inline "#1 < #2"] def UInt64.decLt (a b : UInt64) : Decidable (a < b) := UInt64.casesOn a $ fun n => UInt64.casesOn b $ fun m => inferInstanceAs (Decidable (n < m)) @[extern c inline "#1 <= #2"] def UInt64.decLe (a b : UInt64) : Decidable (a ≤ b) := UInt64.casesOn a $ fun n => UInt64.casesOn b $ fun m => inferInstanceAs (Decidable (n <= m)) instance : DecidableEq UInt64 := UInt64.decEq instance UInt64.hasDecidableLt (a b : UInt64) : Decidable (a < b) := UInt64.decLt a b instance UInt64.hasDecidableLe (a b : UInt64) : Decidable (a ≤ b) := UInt64.decLe a b def usizeSz : Nat := (2:Nat) ^ System.Platform.numBits structure USize := (val : Fin usizeSz) theorem usizeSzGt0 : usizeSz > 0 := Nat.posPowOfPos System.Platform.numBits (Nat.zeroLtSucc _) @[extern "lean_usize_of_nat"] def USize.ofNat (n : @& Nat) : USize := ⟨Fin.ofNat' n usizeSzGt0⟩ abbrev Nat.toUSize := USize.ofNat @[extern "lean_usize_to_nat"] def USize.toNat (n : USize) : Nat := n.val.val @[extern c inline "#1 + #2"] def USize.add (a b : USize) : USize := ⟨a.val + b.val⟩ @[extern c inline "#1 - #2"] def USize.sub (a b : USize) : USize := ⟨a.val - b.val⟩ @[extern c inline "#1 * #2"] def USize.mul (a b : USize) : USize := ⟨a.val * b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 / #2"] def USize.div (a b : USize) : USize := ⟨a.val / b.val⟩ @[extern c inline "#2 == 0 ? 0 : #1 % #2"] def USize.mod (a b : USize) : USize := ⟨a.val % b.val⟩ @[extern "lean_usize_modn"] def USize.modn (a : USize) (n : @& Nat) : USize := ⟨a.val %ₙ n⟩ @[extern c inline "#1 & #2"] def USize.land (a b : USize) : USize := ⟨Fin.land a.val b.val⟩ @[extern c inline "#1 | #2"] def USize.lor (a b : USize) : USize := ⟨Fin.lor a.val b.val⟩ @[extern c inline "#1"] def UInt32.toUSize (a : UInt32) : USize := a.toNat.toUSize @[extern c inline "((size_t)#1)"] def UInt64.toUSize (a : UInt64) : USize := a.toNat.toUSize @[extern c inline "(uint32_t)#1"] def USize.toUInt32 (a : USize) : UInt32 := a.toNat.toUInt32 -- TODO(Leo): give reference implementation for shiftLeft and shiftRight, and define them for other UInt types @[extern c inline "#1 << #2"] constant USize.shiftLeft (a b : USize) : USize := (arbitrary Nat).toUSize @[extern c inline "#1 >> #2"] constant USize.shiftRight (a b : USize) : USize := (arbitrary Nat).toUSize def USize.lt (a b : USize) : Prop := a.val < b.val def USize.le (a b : USize) : Prop := a.val ≤ b.val instance : HasZero USize := ⟨USize.ofNat 0⟩ instance : HasOne USize := ⟨USize.ofNat 1⟩ instance : HasOfNat USize := ⟨USize.ofNat⟩ instance : HasAdd USize := ⟨USize.add⟩ instance : HasSub USize := ⟨USize.sub⟩ instance : HasMul USize := ⟨USize.mul⟩ instance : HasMod USize := ⟨USize.mod⟩ instance : HasModN USize := ⟨USize.modn⟩ instance : HasDiv USize := ⟨USize.div⟩ instance : HasLess USize := ⟨USize.lt⟩ instance : HasLessEq USize := ⟨USize.le⟩ instance : Inhabited USize := ⟨0⟩ @[extern c inline "#1 == #2"] def USize.decEq (a b : USize) : Decidable (a = b) := USize.casesOn a $ fun n => USize.casesOn b $ fun m => if h : n = m then isTrue (h ▸ rfl) else isFalse (fun h' => USize.noConfusion h' (fun h' => absurd h' h)) @[extern c inline "#1 < #2"] def USize.decLt (a b : USize) : Decidable (a < b) := USize.casesOn a $ fun n => USize.casesOn b $ fun m => inferInstanceAs (Decidable (n < m)) @[extern c inline "#1 <= #2"] def USize.decLe (a b : USize) : Decidable (a ≤ b) := USize.casesOn a $ fun n => USize.casesOn b $ fun m => inferInstanceAs (Decidable (n <= m)) instance : DecidableEq USize := USize.decEq instance USize.hasDecidableLt (a b : USize) : Decidable (a < b) := USize.decLt a b instance USize.hasDecidableLe (a b : USize) : Decidable (a ≤ b) := USize.decLe a b theorem USize.modnLt {m : Nat} : ∀ (u : USize), m > 0 → USize.toNat (u %ₙ m) < m | ⟨u⟩, h => Fin.modnLt u h
dacd9fcced5870f5a4175293206908a47604a9b8
88fb7558b0636ec6b181f2a548ac11ad3919f8a5
/tests/lean/run/qexpr1.lean
bfeb5881b41869c721b0a53dbdcb6714a0f64dc0
[ "Apache-2.0" ]
permissive
moritayasuaki/lean
9f666c323cb6fa1f31ac597d777914aed41e3b7a
ae96ebf6ee953088c235ff7ae0e8c95066ba8001
refs/heads/master
1,611,135,440,814
1,493,852,869,000
1,493,852,869,000
90,269,903
0
0
null
1,493,906,291,000
1,493,906,291,000
null
UTF-8
Lean
false
false
591
lean
open tactic #check λ (A : Type) (a b c d : A) (H1 : a = b) (H2 : c = b) (H3 : d = c), have Hac : a = c, by do { h ← get_local `H2, hs ← mk_app `eq.symm [h], x ← to_expr `(eq.trans H1 %%hs), exact x }, show a = d, by do { x ← to_expr `( have aux : a = c, from Hac, have c = d, by do { symmetry, assumption }, show a = d, by do { get_local `Hac >>= clear, get_local `H1 >>= clear, trace "NESTED CALL:", trace_state, transitivity, get_local `aux >>= exact, assumption }), trace "-----------", trace_state, exact x }
b4abb0005cd15867111bdb7aff0c9f2f80576a0d
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/filter/interval.lean
03653c55c390c99b56444203dbf73cf00f84c20f
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
12,555
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury G. Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.set.intervals.ord_connected import Mathlib.order.filter.lift import Mathlib.order.filter.at_top_bot import Mathlib.PostPort universes u_1 l u_2 namespace Mathlib /-! # Convergence of intervals If both `a` and `b` tend to some filter `l₁`, sometimes this implies that `Ixx a b` tends to `l₂.lift' powerset`, i.e., for any `s ∈ l₂` eventually `Ixx a b` becomes a subset of `s`. Here and below `Ixx` is one of `Icc`, `Ico`, `Ioc`, and `Ioo`. We define `filter.tendsto_Ixx_class Ixx l₁ l₂` to be a typeclass representing this property. The instances provide the best `l₂` for a given `l₁`. In many cases `l₁ = l₂` but sometimes we can drop an endpoint from an interval: e.g., we prove `tendsto_Ixx_class Ico (𝓟 $ Iic a) (𝓟 $ Iio a)`, i.e., if `u₁ n` and `u₂ n` belong eventually to `Iic a`, then the interval `Ico (u₁ n) (u₂ n)` is eventually included in `Iio a`. The next table shows “output” filters `l₂` for different values of `Ixx` and `l₁`. The instances that need topology are defined in `topology/algebra/ordered`. | Input filter | `Ixx = Icc` | `Ixx = Ico` | `Ixx = Ioc` | `Ixx = Ioo` | | -----------: | :-----------: | :-----------: | :-----------: | :-----------: | | `at_top` | `at_top` | `at_top` | `at_top` | `at_top` | | `at_bot` | `at_bot` | `at_bot` | `at_bot` | `at_bot` | | `pure a` | `pure a` | `⊥` | `⊥` | `⊥` | | `𝓟 (Iic a)` | `𝓟 (Iic a)` | `𝓟 (Iio a)` | `𝓟 (Iic a)` | `𝓟 (Iio a)` | | `𝓟 (Ici a)` | `𝓟 (Ici a)` | `𝓟 (Ici a)` | `𝓟 (Ioi a)` | `𝓟 (Ioi a)` | | `𝓟 (Ioi a)` | `𝓟 (Ioi a)` | `𝓟 (Ioi a)` | `𝓟 (Ioi a)` | `𝓟 (Ioi a)` | | `𝓟 (Iio a)` | `𝓟 (Iio a)` | `𝓟 (Iio a)` | `𝓟 (Iio a)` | `𝓟 (Iio a)` | | `𝓝 a` | `𝓝 a` | `𝓝 a` | `𝓝 a` | `𝓝 a` | | `𝓝[Iic a] b` | `𝓝[Iic a] b` | `𝓝[Iio a] b` | `𝓝[Iic a] b` | `𝓝[Iio a] b` | | `𝓝[Ici a] b` | `𝓝[Ici a] b` | `𝓝[Ici a] b` | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | | `𝓝[Iio a] b` | `𝓝[Iio a] b` | `𝓝[Iio a] b` | `𝓝[Iio a] b` | `𝓝[Iio a] b` | -/ namespace filter /-- A pair of filters `l₁`, `l₂` has `tendsto_Ixx_class Ixx` property if `Ixx a b` tends to `l₂.lift' powerset` as `a` and `b` tend to `l₁`. In all instances `Ixx` is one of `Icc`, `Ico`, `Ioc`, or `Ioo`. The instances provide the best `l₂` for a given `l₁`. In many cases `l₁ = l₂` but sometimes we can drop an endpoint from an interval: e.g., we prove `tendsto_Ixx_class Ico (𝓟 $ Iic a) (𝓟 $ Iio a)`, i.e., if `u₁ n` and `u₂ n` belong eventually to `Iic a`, then the interval `Ico (u₁ n) (u₂ n)` is eventually included in `Iio a`. We mark `l₂` as an `out_param` so that Lean can automatically find an appropriate `l₂` based on `Ixx` and `l₁`. This way, e.g., `tendsto.Ico h₁ h₂` works without specifying explicitly `l₂`. -/ class tendsto_Ixx_class {α : Type u_1} [preorder α] (Ixx : α → α → set α) (l₁ : filter α) (l₂ : outParam (filter α)) where tendsto_Ixx : tendsto (fun (p : α × α) => Ixx (prod.fst p) (prod.snd p)) (filter.prod l₁ l₁) (filter.lift' l₂ set.powerset) theorem tendsto.Icc {α : Type u_1} {β : Type u_2} [preorder α] {l₁ : filter α} {l₂ : filter α} [tendsto_Ixx_class set.Icc l₁ l₂] {lb : filter β} {u₁ : β → α} {u₂ : β → α} (h₁ : tendsto u₁ lb l₁) (h₂ : tendsto u₂ lb l₁) : tendsto (fun (x : β) => set.Icc (u₁ x) (u₂ x)) lb (filter.lift' l₂ set.powerset) := tendsto.comp tendsto_Ixx_class.tendsto_Ixx (tendsto.prod_mk h₁ h₂) theorem tendsto.Ioc {α : Type u_1} {β : Type u_2} [preorder α] {l₁ : filter α} {l₂ : filter α} [tendsto_Ixx_class set.Ioc l₁ l₂] {lb : filter β} {u₁ : β → α} {u₂ : β → α} (h₁ : tendsto u₁ lb l₁) (h₂ : tendsto u₂ lb l₁) : tendsto (fun (x : β) => set.Ioc (u₁ x) (u₂ x)) lb (filter.lift' l₂ set.powerset) := tendsto.comp tendsto_Ixx_class.tendsto_Ixx (tendsto.prod_mk h₁ h₂) theorem tendsto.Ico {α : Type u_1} {β : Type u_2} [preorder α] {l₁ : filter α} {l₂ : filter α} [tendsto_Ixx_class set.Ico l₁ l₂] {lb : filter β} {u₁ : β → α} {u₂ : β → α} (h₁ : tendsto u₁ lb l₁) (h₂ : tendsto u₂ lb l₁) : tendsto (fun (x : β) => set.Ico (u₁ x) (u₂ x)) lb (filter.lift' l₂ set.powerset) := tendsto.comp tendsto_Ixx_class.tendsto_Ixx (tendsto.prod_mk h₁ h₂) theorem tendsto.Ioo {α : Type u_1} {β : Type u_2} [preorder α] {l₁ : filter α} {l₂ : filter α} [tendsto_Ixx_class set.Ioo l₁ l₂] {lb : filter β} {u₁ : β → α} {u₂ : β → α} (h₁ : tendsto u₁ lb l₁) (h₂ : tendsto u₂ lb l₁) : tendsto (fun (x : β) => set.Ioo (u₁ x) (u₂ x)) lb (filter.lift' l₂ set.powerset) := tendsto.comp tendsto_Ixx_class.tendsto_Ixx (tendsto.prod_mk h₁ h₂) theorem tendsto_Ixx_class_principal {α : Type u_1} [preorder α] {s : set α} {t : set α} {Ixx : α → α → set α} : tendsto_Ixx_class Ixx (principal s) (principal t) ↔ ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → Ixx x y ⊆ t := sorry theorem tendsto_Ixx_class_inf {α : Type u_1} [preorder α] {l₁ : filter α} {l₁' : filter α} {l₂ : filter α} {l₂' : filter α} {Ixx : α → α → set α} [h : tendsto_Ixx_class Ixx l₁ l₂] [h' : tendsto_Ixx_class Ixx l₁' l₂'] : tendsto_Ixx_class Ixx (l₁ ⊓ l₁') (l₂ ⊓ l₂') := sorry theorem tendsto_Ixx_class_of_subset {α : Type u_1} [preorder α] {l₁ : filter α} {l₂ : filter α} {Ixx : α → α → set α} {Ixx' : α → α → set α} (h : ∀ (a b : α), Ixx a b ⊆ Ixx' a b) [h' : tendsto_Ixx_class Ixx' l₁ l₂] : tendsto_Ixx_class Ixx l₁ l₂ := tendsto_Ixx_class.mk (tendsto_lift'_powerset_mono tendsto_Ixx_class.tendsto_Ixx (eventually_of_forall (iff.mpr prod.forall h))) theorem has_basis.tendsto_Ixx_class {α : Type u_1} [preorder α] {ι : Type u_2} {p : ι → Prop} {s : ι → set α} {l : filter α} (hl : has_basis l p s) {Ixx : α → α → set α} (H : ∀ (i : ι), p i → ∀ (x : α), x ∈ s i → ∀ (y : α), y ∈ s i → Ixx x y ⊆ s i) : tendsto_Ixx_class Ixx l l := sorry protected instance tendsto_Icc_at_top_at_top {α : Type u_1} [preorder α] : tendsto_Ixx_class set.Icc at_top at_top := has_basis.tendsto_Ixx_class (has_basis_infi_principal_finite fun (a : α) => set.Ici a) fun (s : set α) (hs : set.finite s) => set.ord_connected_bInter fun (i : α) (hi : i ∈ s) => set.ord_connected_Ici protected instance tendsto_Ico_at_top_at_top {α : Type u_1} [preorder α] : tendsto_Ixx_class set.Ico at_top at_top := tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ico_subset_Icc_self protected instance tendsto_Ioc_at_top_at_top {α : Type u_1} [preorder α] : tendsto_Ixx_class set.Ioc at_top at_top := tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ioc_subset_Icc_self protected instance tendsto_Ioo_at_top_at_top {α : Type u_1} [preorder α] : tendsto_Ixx_class set.Ioo at_top at_top := tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ioo_subset_Icc_self protected instance tendsto_Icc_at_bot_at_bot {α : Type u_1} [preorder α] : tendsto_Ixx_class set.Icc at_bot at_bot := has_basis.tendsto_Ixx_class (has_basis_infi_principal_finite fun (a : α) => set.Iic a) fun (s : set α) (hs : set.finite s) => set.ord_connected_bInter fun (i : α) (hi : i ∈ s) => set.ord_connected_Iic protected instance tendsto_Ico_at_bot_at_bot {α : Type u_1} [preorder α] : tendsto_Ixx_class set.Ico at_bot at_bot := tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ico_subset_Icc_self protected instance tendsto_Ioc_at_bot_at_bot {α : Type u_1} [preorder α] : tendsto_Ixx_class set.Ioc at_bot at_bot := tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ioc_subset_Icc_self protected instance tendsto_Ioo_at_bot_at_bot {α : Type u_1} [preorder α] : tendsto_Ixx_class set.Ioo at_bot at_bot := tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ioo_subset_Icc_self protected instance ord_connected.tendsto_Icc {α : Type u_1} [preorder α] {s : set α} [set.ord_connected s] : tendsto_Ixx_class set.Icc (principal s) (principal s) := iff.mpr tendsto_Ixx_class_principal _inst_2 protected instance tendsto_Ico_Ici_Ici {α : Type u_1} [preorder α] {a : α} : tendsto_Ixx_class set.Ico (principal (set.Ici a)) (principal (set.Ici a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ico_subset_Icc_self protected instance tendsto_Ico_Ioi_Ioi {α : Type u_1} [preorder α] {a : α} : tendsto_Ixx_class set.Ico (principal (set.Ioi a)) (principal (set.Ioi a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ico_subset_Icc_self protected instance tendsto_Ico_Iic_Iio {α : Type u_1} [preorder α] {a : α} : tendsto_Ixx_class set.Ico (principal (set.Iic a)) (principal (set.Iio a)) := iff.mpr tendsto_Ixx_class_principal fun (a_1 : α) (ha : a_1 ∈ set.Iic a) (b : α) (hb : b ∈ set.Iic a) (x : α) (hx : x ∈ set.Ico a_1 b) => lt_of_lt_of_le (and.right hx) hb protected instance tendsto_Ico_Iio_Iio {α : Type u_1} [preorder α] {a : α} : tendsto_Ixx_class set.Ico (principal (set.Iio a)) (principal (set.Iio a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ico_subset_Icc_self protected instance tendsto_Ioc_Ici_Ioi {α : Type u_1} [preorder α] {a : α} : tendsto_Ixx_class set.Ioc (principal (set.Ici a)) (principal (set.Ioi a)) := iff.mpr tendsto_Ixx_class_principal fun (x : α) (hx : x ∈ set.Ici a) (y : α) (hy : y ∈ set.Ici a) (t : α) (ht : t ∈ set.Ioc x y) => lt_of_le_of_lt hx (and.left ht) protected instance tendsto_Ioc_Iic_Iic {α : Type u_1} [preorder α] {a : α} : tendsto_Ixx_class set.Ioc (principal (set.Iic a)) (principal (set.Iic a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ioc_subset_Icc_self protected instance tendsto_Ioc_Iio_Iio {α : Type u_1} [preorder α] {a : α} : tendsto_Ixx_class set.Ioc (principal (set.Iio a)) (principal (set.Iio a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ioc_subset_Icc_self protected instance tendsto_Ioc_Ioi_Ioi {α : Type u_1} [preorder α] {a : α} : tendsto_Ixx_class set.Ioc (principal (set.Ioi a)) (principal (set.Ioi a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ioc_subset_Icc_self protected instance tendsto_Ioo_Ici_Ioi {α : Type u_1} [preorder α] {a : α} : tendsto_Ixx_class set.Ioo (principal (set.Ici a)) (principal (set.Ioi a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ioo_subset_Ioc_self protected instance tendsto_Ioo_Iic_Iio {α : Type u_1} [preorder α] {a : α} : tendsto_Ixx_class set.Ioo (principal (set.Iic a)) (principal (set.Iio a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ioo_subset_Ico_self protected instance tendsto_Ioo_Ioi_Ioi {α : Type u_1} [preorder α] {a : α} : tendsto_Ixx_class set.Ioo (principal (set.Ioi a)) (principal (set.Ioi a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ioo_subset_Ioc_self protected instance tendsto_Ioo_Iio_Iio {α : Type u_1} [preorder α] {a : α} : tendsto_Ixx_class set.Ioo (principal (set.Iio a)) (principal (set.Iio a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ioo_subset_Ioc_self protected instance tendsto_Icc_pure_pure {β : Type u_2} [partial_order β] {a : β} : tendsto_Ixx_class set.Icc (pure a) (pure a) := eq.mpr (id (Eq._oldrec (Eq.refl (tendsto_Ixx_class set.Icc (pure a) (pure a))) (Eq.symm (principal_singleton a)))) (iff.mpr tendsto_Ixx_class_principal set.ord_connected_singleton) protected instance tendsto_Ico_pure_bot {β : Type u_2} [partial_order β] {a : β} : tendsto_Ixx_class set.Ico (pure a) ⊥ := sorry protected instance tendsto_Ioc_pure_bot {β : Type u_2} [partial_order β] {a : β} : tendsto_Ixx_class set.Ioc (pure a) ⊥ := sorry protected instance tendsto_Ioo_pure_bot {β : Type u_2} [partial_order β] {a : β} : tendsto_Ixx_class set.Ioo (pure a) ⊥ := tendsto_Ixx_class_of_subset fun (_x _x_1 : β) => set.Ioo_subset_Ioc_self
ea068a1b5be598b6040dcf8715f678df210dd8e7
5d62e434e81e3303af5bef665e46bef75f10b45e
/noetherian/src/test.lean
30daa00710e429f588257d3bfec6c8b080a1f18c
[]
no_license
ChrisHughes24/type_class
4b49764e9b96b2c7d2b6e4e7827730ca7a64a3b2
b582b60bdc84b7cd17b4243600088143afddc65c
refs/heads/master
1,595,404,449,845
1,567,951,656,000
1,567,951,656,000
207,121,926
0
0
null
null
null
null
UTF-8
Lean
false
false
336
lean
import ring_theory.noetherian ring_theory.principal_ideal_domain -- The fix was to make the `fintype` argument come first in `ring.is_noetherian_of_fintype` example {K : Type*} [discrete_field K] : is_noetherian_ring K := by apply_instance --works example {K : Type*} [discrete_field K] : is_noetherian K K := by apply_instance --fails
74f89f204a95f0f3bc82529c30612876a02e95b9
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/tactic/chain.lean
740a2953d57ddaddaa43b25b30a1a8a3d8e8d9a3
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
4,947
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Mario Carneiro -/ import tactic.ext import data.option.defs open interactive namespace tactic /- This file defines a `chain` tactic, which takes a list of tactics, and exhaustively tries to apply them to the goals, until no tactic succeeds on any goal. Along the way, it generates auxiliary declarations, in order to speed up elaboration time of the resulting (sometimes long!) proofs. This tactic is used by the `tidy` tactic. -/ -- α is the return type of our tactics. When `chain` is called by `tidy`, this is string, -- describing what that tactic did as an interactive tactic. variable {α : Type} /- Because chain sometimes pauses work on the first goal and works on later goals, we need a method for combining a list of results generated while working on a later goal into a single result. This enables `tidy {trace_result := tt}` to output faithfully reproduces its operation, e.g. ```` intros, simp, apply lemma_1, work_on_goal 2 { dsimp, simp }, refl ```` -/ namespace interactive open lean.parser meta def work_on_goal : parse small_nat → itactic → tactic unit | n t := do goals ← get_goals, let earlier_goals := goals.take n, let later_goals := goals.drop (n+1), set_goals (goals.nth n).to_list, t, new_goals ← get_goals, set_goals (earlier_goals ++ new_goals ++ later_goals) end interactive inductive tactic_script (α : Type) : Type | base : α → tactic_script | work (index : ℕ) (first : α) (later : list tactic_script) (closed : bool) : tactic_script meta def tactic_script.to_string : tactic_script string → string | (tactic_script.base a) := a | (tactic_script.work n a l c) := "work_on_goal " ++ (to_string n) ++ " { " ++ (", ".intercalate (a :: l.map tactic_script.to_string)) ++ " }" meta instance : has_to_string (tactic_script string) := { to_string := λ s, s.to_string } meta instance tactic_script_unit_has_to_string : has_to_string (tactic_script unit) := { to_string := λ s, "[chain tactic]" } meta def abstract_if_success (tac : expr → tactic α) (g : expr) : tactic α := do type ← infer_type g, is_lemma ← is_prop type, if is_lemma then -- there's no point making the abstraction, and indeed it's slower tac g else do m ← mk_meta_var type, a ← tac m, do { val ← instantiate_mvars m, guard (val.list_meta_vars = []), c ← new_aux_decl_name, gs ← get_goals, set_goals [g], add_aux_decl c type val ff >>= unify g, set_goals gs } <|> unify m g, return a /-- `chain_many tac` recursively tries `tac` on all goals, working depth-first on generated subgoals, until it no longer succeeds on any goal. `chain_many` automatically makes auxiliary definitions. -/ meta mutual def chain_single, chain_many, chain_iter {α} (tac : tactic α) with chain_single : expr → tactic (α × list (tactic_script α)) | g := do set_goals [g], a ← tac, l ← get_goals >>= chain_many, return (a, l) with chain_many : list expr → tactic (list (tactic_script α)) | [] := return [] | [g] := do { (a, l) ← chain_single g, return (tactic_script.base a :: l) } <|> return [] | gs := chain_iter gs [] with chain_iter : list expr → list expr → tactic (list (tactic_script α)) | [] _ := return [] | (g :: later_goals) stuck_goals := do { (a, l) ← abstract_if_success chain_single g, new_goals ← get_goals, let w := tactic_script.work stuck_goals.length a l (new_goals = []), let current_goals := stuck_goals.reverse ++ new_goals ++ later_goals, set_goals current_goals, -- we keep the goals up to date, so they are correct at the end l' ← chain_many current_goals, return (w :: l') } <|> chain_iter later_goals (g :: stuck_goals) meta def chain_core {α : Type} [has_to_string (tactic_script α)] (tactics : list (tactic α)) : tactic (list string) := do results ← (get_goals >>= chain_many (first tactics)), when results.empty (fail "`chain` tactic made no progress"), return (results.map to_string) variables [has_to_string (tactic_script α)] [has_to_format α] declare_trace chain meta def trace_output (t : tactic α) : tactic α := do tgt ← target, r ← t, name ← decl_name, trace format!"`chain` successfully applied a tactic during elaboration of {name}:", tgt ← pp tgt, trace format!"previous target: {tgt}", trace format!"tactic result: {r}", tgt ← try_core target, tgt ← match tgt with | (some tgt) := pp tgt | none := return "no goals" end, trace format!"new target: {tgt}", pure r meta def chain (tactics : list (tactic α)) : tactic (list string) := chain_core (if is_trace_enabled_for `chain then (tactics.map trace_output) else tactics) end tactic
c4c88eb7007c39ca66948124d098f095e1fb6ccd
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/test/instance_cache.lean
228ac9c2e2fd7fbad5e3f66eeee350546d2d4443
[ "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
1,321
lean
import tactic.cache meta def assert_frozen_instances : tactic unit := do frozen ← tactic.frozen_local_instances, when frozen.is_none $ tactic.fail "instances are not frozen" example (α) (a : α) := begin haveI h : inhabited α := ⟨a⟩, assert_frozen_instances, exact default α end example (α) (a : α) := begin haveI h := inhabited.mk a, assert_frozen_instances, exact default α end example (α) (a : α) := begin letI h : inhabited α := ⟨a⟩, assert_frozen_instances, exact default α end example (α) (a : α) := begin letI h : inhabited α, all_goals { assert_frozen_instances }, exact ⟨a⟩, exact default α end example (α) (a : α) := begin letI h := inhabited.mk a, exact default α end example (α) : inhabited α → α := by intro a; exactI default α example (α) : inhabited α → α := begin introsI a, assert_frozen_instances, exact default α end example (α β) (h : α = β) [inhabited α] : β := begin substI h, assert_frozen_instances, exact default _ end example (α β) (h : α = β) [inhabited α] : β := begin unfreezingI { cases _inst_1 }, assert_frozen_instances, subst h, assumption end example (α β) (h : α = β) [inhabited α] : β := begin casesI _inst_1, assert_frozen_instances, subst h, assumption end
d9ae2e5c3d56977e018296054a83fb15fe059624
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/topology/algebra/open_subgroup.lean
d9f7fda557d382888b942ccf710abe13384a9c85
[ "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
7,280
lean
/- Copyright (c) 2019 Johan Commelin All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import order.filter.lift import topology.opens import topology.algebra.ring open topological_space open_locale topological_space set_option old_structure_cmd true /-- The type of open subgroups of a topological additive group. -/ @[ancestor add_subgroup] structure open_add_subgroup (G : Type*) [add_group G] [topological_space G] extends add_subgroup G := (is_open' : is_open carrier) /-- The type of open subgroups of a topological group. -/ @[ancestor subgroup, to_additive open_add_subgroup] structure open_subgroup (G : Type*) [group G] [topological_space G] extends subgroup G := (is_open' : is_open carrier) /-- Reinterpret an `open_subgroup` as a `subgroup`. -/ add_decl_doc open_subgroup.to_subgroup /-- Reinterpret an `open_add_subgroup` as an `add_subgroup`. -/ add_decl_doc open_add_subgroup.to_add_subgroup -- Tell Lean that `open_add_subgroup` is a namespace namespace open_add_subgroup end open_add_subgroup namespace open_subgroup open function topological_space variables {G : Type*} [group G] [topological_space G] variables {U V : open_subgroup G} {g : G} @[to_additive] instance has_coe_set : has_coe_t (open_subgroup G) (set G) := ⟨λ U, U.1⟩ @[to_additive] instance : has_mem G (open_subgroup G) := ⟨λ g U, g ∈ (U : set G)⟩ @[to_additive] instance has_coe_subgroup : has_coe_t (open_subgroup G) (subgroup G) := ⟨to_subgroup⟩ @[to_additive] instance has_coe_opens : has_coe_t (open_subgroup G) (opens G) := ⟨λ U, ⟨U, U.is_open'⟩⟩ @[simp, to_additive] lemma mem_coe : g ∈ (U : set G) ↔ g ∈ U := iff.rfl @[simp, to_additive] lemma mem_coe_opens : g ∈ (U : opens G) ↔ g ∈ U := iff.rfl @[simp, to_additive mem_coe_add_subgroup] lemma mem_coe_subgroup : g ∈ (U : subgroup G) ↔ g ∈ U := iff.rfl attribute [norm_cast] mem_coe mem_coe_opens mem_coe_subgroup open_add_subgroup.mem_coe open_add_subgroup.mem_coe_opens open_add_subgroup.mem_coe_add_subgroup @[to_additive] lemma coe_injective : injective (coe : open_subgroup G → set G) := λ U V h, by cases U; cases V; congr; assumption @[ext, to_additive] lemma ext (h : ∀ x, x ∈ U ↔ x ∈ V) : (U = V) := coe_injective $ set.ext h @[to_additive] lemma ext_iff : (U = V) ↔ (∀ x, x ∈ U ↔ x ∈ V) := ⟨λ h x, h ▸ iff.rfl, ext⟩ variable (U) @[to_additive] protected lemma is_open : is_open (U : set G) := U.is_open' @[to_additive] protected lemma one_mem : (1 : G) ∈ U := U.one_mem' @[to_additive] protected lemma inv_mem {g : G} (h : g ∈ U) : g⁻¹ ∈ U := U.inv_mem' h @[to_additive] protected lemma mul_mem {g₁ g₂ : G} (h₁ : g₁ ∈ U) (h₂ : g₂ ∈ U) : g₁ * g₂ ∈ U := U.mul_mem' h₁ h₂ @[to_additive] lemma mem_nhds_one : (U : set G) ∈ 𝓝 (1 : G) := mem_nhds_sets U.is_open U.one_mem variable {U} @[to_additive] instance : has_top (open_subgroup G) := ⟨{ is_open' := is_open_univ, .. (⊤ : subgroup G) }⟩ @[to_additive] instance : inhabited (open_subgroup G) := ⟨⊤⟩ @[to_additive] lemma is_closed [has_continuous_mul G] (U : open_subgroup G) : is_closed (U : set G) := begin refine is_open_iff_forall_mem_open.2 (λ x hx, ⟨(λ y, y * x⁻¹) ⁻¹' U, _, _, _⟩), { intros u hux, simp only [set.mem_preimage, set.mem_compl_iff, mem_coe] at hux hx ⊢, refine mt (λ hu, _) hx, convert U.mul_mem (U.inv_mem hux) hu, simp }, { exact (continuous_mul_right _) _ U.is_open }, { simp [U.one_mem] } end section variables {H : Type*} [group H] [topological_space H] @[to_additive] def prod (U : open_subgroup G) (V : open_subgroup H) : open_subgroup (G × H) := { carrier := (U : set G).prod (V : set H), is_open' := is_open_prod U.is_open V.is_open, .. (U : subgroup G).prod (V : subgroup H) } end @[to_additive] instance : partial_order (open_subgroup G) := { le := λ U V, ∀ ⦃x⦄, x ∈ U → x ∈ V, .. partial_order.lift (coe : open_subgroup G → set G) coe_injective } @[to_additive] instance : semilattice_inf_top (open_subgroup G) := { inf := λ U V, { is_open' := is_open_inter U.is_open V.is_open, .. (U : subgroup G) ⊓ V }, inf_le_left := λ U V, set.inter_subset_left _ _, inf_le_right := λ U V, set.inter_subset_right _ _, le_inf := λ U V W hV hW, set.subset_inter hV hW, top := ⊤, le_top := λ U, set.subset_univ _, ..open_subgroup.partial_order } @[simp, to_additive] lemma coe_inf : (↑(U ⊓ V) : set G) = (U : set G) ∩ V := rfl @[simp, to_additive] lemma coe_subset : (U : set G) ⊆ V ↔ U ≤ V := iff.rfl @[simp, to_additive] lemma coe_subgroup_le : (U : subgroup G) ≤ (V : subgroup G) ↔ U ≤ V := iff.rfl attribute [norm_cast] coe_inf coe_subset coe_subgroup_le open_add_subgroup.coe_inf open_add_subgroup.coe_subset open_add_subgroup.coe_subgroup_le end open_subgroup namespace subgroup variables {G : Type*} [group G] [topological_space G] [has_continuous_mul G] (H : subgroup G) @[to_additive] lemma is_open_of_mem_nhds {g : G} (hg : (H : set G) ∈ 𝓝 g) : is_open (H : set G) := begin simp only [is_open_iff_mem_nhds, subgroup.mem_coe] at hg ⊢, intros x hx, have : filter.tendsto (λ y, y * (x⁻¹ * g)) (𝓝 x) (𝓝 $ x * (x⁻¹ * g)) := (continuous_id.mul continuous_const).tendsto _, rw [mul_inv_cancel_left] at this, have := filter.mem_map.1 (this hg), replace hg : g ∈ H := subgroup.mem_coe.1 (mem_of_nhds hg), simp only [subgroup.mem_coe, H.mul_mem_cancel_right (H.mul_mem (H.inv_mem hx) hg)] at this, exact this end @[to_additive is_open_of_open_add_subgroup] lemma is_open_of_open_subgroup {U : open_subgroup G} (h : U.1 ≤ H) : is_open (H : set G) := H.is_open_of_mem_nhds (filter.mem_sets_of_superset U.mem_nhds_one h) @[to_additive] lemma is_open_mono {H₁ H₂ : subgroup G} (h : H₁ ≤ H₂) (h₁ : is_open (H₁ :set G)) : is_open (H₂ : set G) := @is_open_of_open_subgroup _ _ _ _ H₂ { is_open' := h₁, .. H₁ } h end subgroup namespace open_subgroup variables {G : Type*} [group G] [topological_space G] [has_continuous_mul G] @[to_additive] instance : semilattice_sup_top (open_subgroup G) := { sup := λ U V, { is_open' := show is_open (((U : subgroup G) ⊔ V : subgroup G) : set G), from subgroup.is_open_mono le_sup_left U.is_open, .. ((U : subgroup G) ⊔ V) }, le_sup_left := λ U V, coe_subgroup_le.1 le_sup_left, le_sup_right := λ U V, coe_subgroup_le.1 le_sup_right, sup_le := λ U V W hU hV, coe_subgroup_le.1 (sup_le hU hV), ..open_subgroup.semilattice_inf_top } end open_subgroup namespace submodule open open_add_subgroup variables {R : Type*} {M : Type*} [comm_ring R] variables [add_comm_group M] [topological_space M] [topological_add_group M] [module R M] lemma is_open_mono {U P : submodule R M} (h : U ≤ P) (hU : is_open (U : set M)) : is_open (P : set M) := @add_subgroup.is_open_mono M _ _ _ U.to_add_subgroup P.to_add_subgroup h hU end submodule namespace ideal variables {R : Type*} [comm_ring R] variables [topological_space R] [topological_ring R] lemma is_open_of_open_subideal {U I : ideal R} (h : U ≤ I) (hU : is_open (U : set R)) : is_open (I : set R) := submodule.is_open_mono h hU end ideal
129c874f46f62da804a50e5f8602e9ffdd24a30f
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/qpf/multivariate/constructions/const.lean
df3d8298469712b8e193ec66f485ea88762ea9a5
[]
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
2,430
lean
/- Copyright (c) 2020 Simon Hudon All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Simon Hudon -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.control.functor.multivariate import Mathlib.data.qpf.multivariate.basic import Mathlib.PostPort universes u u_1 u_2 namespace Mathlib /-! # Constant functors are QPFs Constant functors map every type vectors to the same target type. This is a useful device for constructing data types from more basic types that are not actually functorial. For instance `const n nat` makes `nat` into a functor that can be used in a functor-based data type specification. -/ namespace mvqpf /-- Constant multivariate functor -/ def const (n : ℕ) (A : Type u_1) (v : typevec n) := A protected instance const.inhabited (n : ℕ) {A : Type u_1} {α : typevec n} [Inhabited A] : Inhabited (const n A α) := { default := Inhabited.default } namespace const /-- Constructor for constant functor -/ protected def mk {n : ℕ} {A : Type u} {α : typevec n} (x : A) : const n A α := x /-- Destructor for constant functor -/ protected def get {n : ℕ} {A : Type u} {α : typevec n} (x : const n A α) : A := x @[simp] protected theorem mk_get {n : ℕ} {A : Type u} {α : typevec n} (x : const n A α) : const.mk (const.get x) = x := rfl @[simp] protected theorem get_mk {n : ℕ} {A : Type u} {α : typevec n} (x : A) : const.get (const.mk x) = x := rfl /-- `map` for constant functor -/ protected def map {n : ℕ} {A : Type u} {α : typevec n} {β : typevec n} : const n A α → const n A β := fun (x : const n A α) => x protected instance mvfunctor {n : ℕ} {A : Type u} : mvfunctor (const n A) := mvfunctor.mk fun (α β : typevec n) (f : typevec.arrow α β) => const.map theorem map_mk {n : ℕ} {A : Type u} {α : typevec n} {β : typevec n} (f : typevec.arrow α β) (x : A) : mvfunctor.map f (const.mk x) = const.mk x := rfl theorem get_map {n : ℕ} {A : Type u} {α : typevec n} {β : typevec n} (f : typevec.arrow α β) (x : const n A α) : const.get (mvfunctor.map f x) = const.get x := rfl protected instance mvqpf {n : ℕ} {A : Type u} : mvqpf (const n A) := mk (mvpfunctor.const n A) (fun (α : typevec n) (x : mvpfunctor.obj (mvpfunctor.const n A) α) => mvpfunctor.const.get x) (fun (α : typevec n) (x : const n A α) => mvpfunctor.const.mk n x) sorry sorry
427adcee2ab5896df1396c5e470d99c30c3bb859
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/control/lawful_fix.lean
6fb7b789b71ea55b914ca5f21919ec50f77fadaf
[ "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
7,709
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 tactic.apply import control.fix import order.omega_complete_partial_order /-! # Lawful fixed point operators This module defines the laws required of a `has_fix` instance, using the theory of omega complete partial orders (ωCPO). Proofs of the lawfulness of all `has_fix` instances in `control.fix` are provided. ## Main definition * class `lawful_fix` -/ universes u v open_locale classical variables {α : Type*} {β : α → Type*} open omega_complete_partial_order /-- Intuitively, a fixed point operator `fix` is lawful if it satisfies `fix f = f (fix f)` for all `f`, but this is inconsistent / uninteresting in most cases due to the existence of "exotic" functions `f`, such as the function that is defined iff its argument is not, familiar from the halting problem. Instead, this requirement is limited to only functions that are `continuous` in the sense of `ω`-complete partial orders, which excludes the example because it is not monotone (making the input argument less defined can make `f` more defined). -/ class lawful_fix (α : Type*) [omega_complete_partial_order α] extends has_fix α := (fix_eq : ∀ {f : α →ₘ α}, continuous f → has_fix.fix f = f (has_fix.fix f)) lemma lawful_fix.fix_eq' {α} [omega_complete_partial_order α] [lawful_fix α] {f : α → α} (hf : continuous' f) : has_fix.fix f = f (has_fix.fix f) := lawful_fix.fix_eq (hf.to_bundled _) namespace part open part nat nat.upto namespace fix variables (f : (Π a, part $ β a) →ₘ (Π a, part $ β a)) lemma approx_mono' {i : ℕ} : fix.approx f i ≤ fix.approx f (succ i) := begin induction i, dsimp [approx], apply @bot_le _ _ (f ⊥), intro, apply f.monotone, apply i_ih end lemma approx_mono ⦃i j : ℕ⦄ (hij : i ≤ j) : approx f i ≤ approx f j := begin induction j, cases hij, refine @le_refl _ _ _, cases hij, apply @le_refl _ _ _, apply @le_trans _ _ _ (approx f j_n) _ (j_ih ‹_›), apply approx_mono' f end lemma mem_iff (a : α) (b : β a) : b ∈ part.fix f a ↔ ∃ i, b ∈ approx f i a := begin by_cases h₀ : ∃ (i : ℕ), (approx f i a).dom, { simp only [part.fix_def f h₀], split; intro hh, exact ⟨_,hh⟩, have h₁ := nat.find_spec h₀, rw [dom_iff_mem] at h₁, cases h₁ with y h₁, replace h₁ := approx_mono' f _ _ h₁, suffices : y = b, subst this, exact h₁, cases hh with i hh, revert h₁, generalize : (succ (nat.find h₀)) = j, intro, wlog : i ≤ j := le_total i j using [i j b y,j i y b], replace hh := approx_mono f case _ _ hh, apply part.mem_unique h₁ hh }, { simp only [fix_def' ⇑f h₀, not_exists, false_iff, not_mem_none], simp only [dom_iff_mem, not_exists] at h₀, intro, apply h₀ } end lemma approx_le_fix (i : ℕ) : approx f i ≤ part.fix f := assume a b hh, by { rw [mem_iff f], exact ⟨_,hh⟩ } lemma exists_fix_le_approx (x : α) : ∃ i, part.fix f x ≤ approx f i x := begin by_cases hh : ∃ i b, b ∈ approx f i x, { rcases hh with ⟨i,b,hb⟩, existsi i, intros b' h', have hb' := approx_le_fix f i _ _ hb, have hh := part.mem_unique h' hb', subst hh, exact hb }, { simp only [not_exists] at hh, existsi 0, intros b' h', simp only [mem_iff f] at h', cases h' with i h', cases hh _ _ h' } end include f /-- The series of approximations of `fix f` (see `approx`) as a `chain` -/ def approx_chain : chain (Π a, part $ β a) := ⟨approx f, approx_mono f⟩ lemma le_f_of_mem_approx {x} (hx : x ∈ approx_chain f) : x ≤ f x := begin revert hx, simp [(∈)], intros i hx, subst x, apply approx_mono' end lemma approx_mem_approx_chain {i} : approx f i ∈ approx_chain f := stream.mem_of_nth_eq rfl end fix open fix variables {α} variables (f : (Π a, part $ β a) →ₘ (Π a, part $ β a)) open omega_complete_partial_order open part (hiding ωSup) nat open nat.upto omega_complete_partial_order lemma fix_eq_ωSup : part.fix f = ωSup (approx_chain f) := begin apply le_antisymm, { intro x, cases exists_fix_le_approx f x with i hx, transitivity' approx f i.succ x, { transitivity', apply hx, apply approx_mono' f }, apply' le_ωSup_of_le i.succ, dsimp [approx], refl', }, { apply ωSup_le _ _ _, simp only [fix.approx_chain, preorder_hom.coe_fun_mk], intros y x, apply approx_le_fix f }, end lemma fix_le {X : Π a, part $ β a} (hX : f X ≤ X) : part.fix f ≤ X := begin rw fix_eq_ωSup f, apply ωSup_le _ _ _, simp only [fix.approx_chain, preorder_hom.coe_fun_mk], intros i, induction i, dsimp [fix.approx], apply' bot_le, transitivity' f X, apply f.monotone i_ih, apply hX end variables {f} (hc : continuous f) include hc lemma fix_eq : part.fix f = f (part.fix f) := begin rw [fix_eq_ωSup f,hc], apply le_antisymm, { apply ωSup_le_ωSup_of_le _, intros i, existsi [i], intro x, -- intros x y hx, apply le_f_of_mem_approx _ ⟨i, rfl⟩, }, { apply ωSup_le_ωSup_of_le _, intros i, existsi i.succ, refl', } end end part namespace part /-- `to_unit` as a monotone function -/ @[simps] def to_unit_mono (f : part α →ₘ part α) : (unit → part α) →ₘ (unit → part α) := { to_fun := λ x u, f (x u), monotone' := λ x y (h : x ≤ y) u, f.monotone $ h u } lemma to_unit_cont (f : part α →ₘ part α) (hc : continuous f) : continuous (to_unit_mono f) | c := begin ext ⟨⟩ : 1, dsimp [omega_complete_partial_order.ωSup], erw [hc, chain.map_comp], refl end noncomputable instance : lawful_fix (part α) := ⟨λ f hc, show part.fix (to_unit_mono f) () = _, by rw part.fix_eq (to_unit_cont f hc); refl⟩ end part open sigma namespace pi noncomputable instance {β} : lawful_fix (α → part β) := ⟨λ f, part.fix_eq⟩ variables {γ : Π a : α, β a → Type*} section monotone variables (α β γ) /-- `sigma.curry` as a monotone function. -/ @[simps] def monotone_curry [∀ x y, preorder $ γ x y] : (Π x : Σ a, β a, γ x.1 x.2) →ₘ (Π a (b : β a), γ a b) := { to_fun := curry, monotone' := λ x y h a b, h ⟨a,b⟩ } /-- `sigma.uncurry` as a monotone function. -/ @[simps] def monotone_uncurry [∀ x y, preorder $ γ x y] : (Π a (b : β a), γ a b) →ₘ (Π x : Σ a, β a, γ x.1 x.2) := { to_fun := uncurry, monotone' := λ x y h a, h a.1 a.2 } variables [∀ x y, omega_complete_partial_order $ γ x y] open omega_complete_partial_order.chain lemma continuous_curry : continuous $ monotone_curry α β γ := λ c, by { ext x y, dsimp [curry,ωSup], rw [map_comp,map_comp], refl } lemma continuous_uncurry : continuous $ monotone_uncurry α β γ := λ c, by { ext x y, dsimp [uncurry,ωSup], rw [map_comp,map_comp], refl } end monotone open has_fix instance [has_fix $ Π x : sigma β, γ x.1 x.2] : has_fix (Π x (y : β x), γ x y) := ⟨ λ f, curry (fix $ uncurry ∘ f ∘ curry) ⟩ variables [∀ x y, omega_complete_partial_order $ γ x y] section curry variables {f : (Π x (y : β x), γ x y) →ₘ (Π x (y : β x), γ x y)} variables (hc : continuous f) lemma uncurry_curry_continuous : continuous $ (monotone_uncurry α β γ).comp $ f.comp $ monotone_curry α β γ := continuous_comp _ _ (continuous_comp _ _ (continuous_curry _ _ _) hc) (continuous_uncurry _ _ _) end curry instance pi.lawful_fix' [lawful_fix $ Π x : sigma β, γ x.1 x.2] : lawful_fix (Π x y, γ x y) := { fix_eq := λ f hc, begin dsimp [fix], conv { to_lhs, erw [lawful_fix.fix_eq (uncurry_curry_continuous hc)] }, refl, end, } end pi
bd5b37504aad3cdf91f710d2b0b649d29b676fa0
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/toArrayEq.lean
26d40ebceb84b1870f248fd79993ee88470f3188
[ "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
278
lean
inductive Foo | foo : Nat → Foo | foos : Array Foo → Foo deriving BEq example : Foo.foo 0 ≠ Foo.foo 1 := by simp example : #[0] ≠ #[1] := by simp example : #[Foo.foo 0] ≠ #[Foo.foo 1] := by simp example : Foo.foos #[.foo 0] ≠ Foo.foos #[.foo 1] := by simp
353c3273969f8bb7b6fa8fcf8043b7dd7dab31fd
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/mutual_sizeof.lean
adbcdd13db338148a5635d59ef62dae2194e42a7
[ "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
502
lean
inductive tree : Type | node : list tree → tree mutual def g, f, h with g : list tree → ℕ | [] := 0 | (x :: xs) := f x + g xs with f : tree → ℕ | (tree.node children) := 1 + g children with h : ℕ → ℕ | x := x mutual def f1, f2, f3, f4, f5 with f1 : ℕ → unit | (n+1) := f5 n | _ := () with f2 : ℕ → unit | (n+1) := f3 n | _ := () with f3 : ℕ → unit | (n+1) := f3 n | _ := () with f4 : ℕ → unit | (n+1) := f1 n | _ := () with f5 : ℕ → unit | (n+1) := f4 n | _ := ()
33f5bf0b1084c24ba7346b0d921ee6c88ff44523
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/category_theory/limits/yoneda.lean
ce6cd9c12c91fbfa048b43c161f8dc4c4c8e8298
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,603
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Bhavik Mehta -/ import category_theory.limits.functor_category /-! # Limit properties relating to the (co)yoneda embedding. We calculate the colimit of `Y ↦ (X ⟶ Y)`, which is just `punit`. (This is used in characterising cofinal functors.) We also show the (co)yoneda embeddings preserve limits and jointly reflect them. -/ open opposite open category_theory open category_theory.limits universes v u namespace category_theory namespace coyoneda variables {C : Type v} [small_category C] /-- The colimit cocone over `coyoneda.obj X`, with cocone point `punit`. -/ @[simps] def colimit_cocone (X : Cᵒᵖ) : cocone (coyoneda.obj X) := { X := punit, ι := { app := by tidy, } } /-- The proposed colimit cocone over `coyoneda.obj X` is a colimit cocone. -/ @[simps] def colimit_cocone_is_colimit (X : Cᵒᵖ) : is_colimit (colimit_cocone X) := { desc := λ s x, s.ι.app (unop X) (𝟙 _), fac' := λ s Y, by { ext f, convert congr_fun (s.w f).symm (𝟙 (unop X)), simp, }, uniq' := λ s m w, by { ext ⟨⟩, rw ← w, simp, } } instance (X : Cᵒᵖ) : has_colimit (coyoneda.obj X) := has_colimit.mk { cocone := _, is_colimit := colimit_cocone_is_colimit X } /-- The colimit of `coyoneda.obj X` is isomorphic to `punit`. -/ noncomputable def colimit_coyoneda_iso (X : Cᵒᵖ) : colimit (coyoneda.obj X) ≅ punit := colimit.iso_colimit_cocone { cocone := _, is_colimit := colimit_cocone_is_colimit X } end coyoneda variables {C : Type u} [category.{v} C] open limits /-- The yoneda embedding `yoneda.obj X : Cᵒᵖ ⥤ Type v` for `X : C` preserves limits. -/ instance yoneda_preserves_limits (X : C) : preserves_limits (yoneda.obj X) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ K, { preserves := λ c t, { lift := λ s x, has_hom.hom.unop (t.lift ⟨op X, λ j, (s.π.app j x).op, λ j₁ j₂ α, _⟩), fac' := λ s j, funext $ λ x, has_hom.hom.op_inj (t.fac _ _), uniq' := λ s m w, funext $ λ x, begin refine has_hom.hom.op_inj (t.uniq ⟨op X, _, _⟩ _ (λ j, _)), { dsimp, simp [← s.w α] }, -- See library note [dsimp, simp] { exact has_hom.hom.unop_inj (congr_fun (w j) x) }, end } } } } /-- The coyoneda embedding `coyoneda.obj X : C ⥤ Type v` for `X : Cᵒᵖ` preserves limits. -/ instance coyoneda_preserves_limits (X : Cᵒᵖ) : preserves_limits (coyoneda.obj X) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ K, { preserves := λ c t, { lift := λ s x, t.lift ⟨unop X, λ j, s.π.app j x, λ j₁ j₂ α, by { dsimp, simp [← s.w α]}⟩, -- See library note [dsimp, simp] fac' := λ s j, funext $ λ x, t.fac _ _, uniq' := λ s m w, funext $ λ x, begin refine (t.uniq ⟨unop X, _⟩ _ (λ j, _)), exact congr_fun (w j) x, end } } } } /-- The yoneda embeddings jointly reflect limits. -/ def yoneda_jointly_reflects_limits (J : Type v) [small_category J] (K : J ⥤ Cᵒᵖ) (c : cone K) (t : Π (X : C), is_limit ((yoneda.obj X).map_cone c)) : is_limit c := let s' : Π (s : cone K), cone (K ⋙ yoneda.obj s.X.unop) := λ s, ⟨punit, λ j _, (s.π.app j).unop, λ j₁ j₂ α, funext $ λ _, has_hom.hom.op_inj (s.w α).symm⟩ in { lift := λ s, ((t s.X.unop).lift (s' s) punit.star).op, fac' := λ s j, has_hom.hom.unop_inj (congr_fun ((t s.X.unop).fac (s' s) j) punit.star), uniq' := λ s m w, begin apply has_hom.hom.unop_inj, suffices : (λ (x : punit), m.unop) = (t s.X.unop).lift (s' s), { apply congr_fun this punit.star }, apply (t _).uniq (s' s) _ (λ j, _), ext, exact has_hom.hom.op_inj (w j), end } /-- The coyoneda embeddings jointly reflect limits. -/ def coyoneda_jointly_reflects_limits (J : Type v) [small_category J] (K : J ⥤ C) (c : cone K) (t : Π (X : Cᵒᵖ), is_limit ((coyoneda.obj X).map_cone c)) : is_limit c := let s' : Π (s : cone K), cone (K ⋙ coyoneda.obj (op s.X)) := λ s, ⟨punit, λ j _, s.π.app j, λ j₁ j₂ α, funext $ λ _, (s.w α).symm⟩ in { lift := λ s, (t (op s.X)).lift (s' s) punit.star, fac' := λ s j, congr_fun ((t _).fac (s' s) j) punit.star, uniq' := λ s m w, begin suffices : (λ (x : punit), m) = (t _).lift (s' s), { apply congr_fun this punit.star }, apply (t _).uniq (s' s) _ (λ j, _), ext, exact (w j), end } end category_theory
ca06ff8120ff6ca034f683fa3c828032c1d61f87
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/Util/PPExt.lean
688be7a9aa9490c17f4dce0f9f241b453253b937
[ "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,938
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ import Lean.Environment import Lean.MetavarContext import Lean.Data.OpenDecl namespace Lean @[init] private def registerOptions : IO Unit := do registerOption `syntaxMaxDepth { defValue := (2 : Nat), group := "", descr := "maximum depth when displaying syntax objects in messages" }; registerOption `pp.raw { defValue := false, group := "pp", descr := "(pretty printer) print raw expression/syntax tree" } def getSyntaxMaxDepth (opts : Options) : Nat := opts.getNat `syntaxMaxDepth 2 def getPPRaw (opts : Options) : Bool := opts.getBool `pp.raw false structure PPContext := (env : Environment) (mctx : MetavarContext := {}) (lctx : LocalContext := {}) (opts : Options := {}) (currNamespace : Name := Name.anonymous) (openDecls : List OpenDecl := []) structure PPFns := (ppExpr : PPContext → Expr → IO Format) (ppTerm : PPContext → Syntax → IO Format) instance PPFns.inhabited : Inhabited PPFns := ⟨⟨arbitrary _, arbitrary _⟩⟩ def mkPPFnsRef : IO (IO.Ref PPFns) := IO.mkRef { ppExpr := fun ctx e => pure $ format (toString e), ppTerm := fun ctx stx => pure $ stx.formatStx (getSyntaxMaxDepth ctx.opts), } @[init mkPPFnsRef] def ppFnsRef : IO.Ref PPFns := arbitrary _ def mkPPExt : IO (EnvExtension PPFns) := registerEnvExtension $ ppFnsRef.get @[init mkPPExt] constant ppExt : EnvExtension PPFns := arbitrary _ def ppExpr (ctx : PPContext) (e : Expr) : IO Format := let e := (ctx.mctx.instantiateMVars e).1; if getPPRaw ctx.opts then pure $ format (toString e) else (ppExt.getState ctx.env).ppExpr ctx e def ppTerm (ctx : PPContext) (stx : Syntax) : IO Format := if getPPRaw ctx.opts then pure $ stx.formatStx (getSyntaxMaxDepth ctx.opts) else (ppExt.getState ctx.env).ppTerm ctx stx end Lean
e8939433da97c14e890a88c732c340a905777d95
c777c32c8e484e195053731103c5e52af26a25d1
/src/data/fintype/quotient.lean
09fe4cf45be7dbafe5b44103f9b59d659325cae9
[ "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
3,356
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.fintype.basic /-! # Quotients of families indexed by a finite type This file provides `quotient.fin_choice`, a mechanism to go from a finite family of quotients to a quotient of finite families. ## Main definitions * `quotient.fin_choice` -/ /-- An auxiliary function for `quotient.fin_choice`. Given a collection of setoids indexed by a type `ι`, a (finite) list `l` of indices, and a function that for each `i ∈ l` gives a term of the corresponding quotient type, then there is a corresponding term in the quotient of the product of the setoids indexed by `l`. -/ def quotient.fin_choice_aux {ι : Type*} [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] : Π (l : list ι), (Π i ∈ l, quotient (S i)) → @quotient (Π i ∈ l, α i) (by apply_instance) | [] f := ⟦λ i, false.elim⟧ | (i :: l) f := begin refine quotient.lift_on₂ (f i (list.mem_cons_self _ _)) (quotient.fin_choice_aux l (λ j h, f j (list.mem_cons_of_mem _ h))) _ _, exact λ a l, ⟦λ j h, if e : j = i then by rw e; exact a else l _ (h.resolve_left e)⟧, refine λ a₁ l₁ a₂ l₂ h₁ h₂, quotient.sound (λ j h, _), by_cases e : j = i; simp [e], { subst j, exact h₁ }, { exact h₂ _ _ } end theorem quotient.fin_choice_aux_eq {ι : Type*} [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] : ∀ (l : list ι) (f : Π i ∈ l, α i), quotient.fin_choice_aux l (λ i h, ⟦f i h⟧) = ⟦f⟧ | [] f := quotient.sound (λ i h, h.elim) | (i :: l) f := begin simp [quotient.fin_choice_aux, quotient.fin_choice_aux_eq l], refine quotient.sound (λ j h, _), by_cases e : j = i; simp [e], subst j, refl end /-- Given a collection of setoids indexed by a fintype `ι` and a function that for each `i : ι` gives a term of the corresponding quotient type, then there is corresponding term in the quotient of the product of the setoids. -/ def quotient.fin_choice {ι : Type*} [decidable_eq ι] [fintype ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] (f : Π i, quotient (S i)) : @quotient (Π i, α i) (by apply_instance) := quotient.lift_on (@quotient.rec_on _ _ (λ l : multiset ι, @quotient (Π i ∈ l, α i) (by apply_instance)) finset.univ.1 (λ l, quotient.fin_choice_aux l (λ i _, f i)) (λ a b h, begin have := λ a, quotient.fin_choice_aux_eq a (λ i h, quotient.out (f i)), simp [quotient.out_eq] at this, simp [this], let g := λ a:multiset ι, ⟦λ (i : ι) (h : i ∈ a), quotient.out (f i)⟧, refine eq_of_heq ((eq_rec_heq _ _).trans (_ : g a == g b)), congr' 1, exact quotient.sound h, end)) (λ f, ⟦λ i, f i (finset.mem_univ _)⟧) (λ a b h, quotient.sound $ λ i, h _ _) theorem quotient.fin_choice_eq {ι : Type*} [decidable_eq ι] [fintype ι] {α : ι → Type*} [∀ i, setoid (α i)] (f : Π i, α i) : quotient.fin_choice (λ i, ⟦f i⟧) = ⟦f⟧ := begin let q, swap, change quotient.lift_on q _ _ = _, have : q = ⟦λ i h, f i⟧, { dsimp only [q], exact quotient.induction_on (@finset.univ ι _).1 (λ l, quotient.fin_choice_aux_eq _ _) }, simp [this], exact setoid.refl _ end
3e4eb5a6c8d63f32c1b069ef6d55d7e1bfb4e578
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/star/basic.lean
827576a9b9fb2f9d37a7acd52c8725d5a9a67442
[ "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
15,218
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import tactic.apply_fun import algebra.field.opposite import algebra.field_power import data.equiv.ring_aut import group_theory.group_action.units import group_theory.group_action.opposite import algebra.ring.comp_typeclasses /-! # Star monoids, rings, and modules We introduce the basic algebraic notions of star monoids, star rings, and star modules. A star algebra is simply a star ring that is also a star module. These are implemented as "mixin" typeclasses, so to summon a star ring (for example) one needs to write `(R : Type) [ring R] [star_ring R]`. This avoids difficulties with diamond inheritance. We also define the class `star_ordered_ring R`, which says that the order on `R` respects the star operation, i.e. an element `r` is nonnegative iff there exists an `s` such that `r = star s * s`. For now we simply do not introduce notations, as different users are expected to feel strongly about the relative merits of `r^*`, `r†`, `rᘁ`, and so on. Our star rings are actually star semirings, but of course we can prove `star_neg : star (-r) = - star r` when the underlying semiring is a ring. ## TODO * In a Banach star algebra without a well-defined square root, the natural ordering is given by the positive cone which is the closure of the sums of elements `star r * r`. A weaker version of `star_ordered_ring` could be defined for this case. Note that the current definition has the advantage of not requiring a topology. -/ universes u v open mul_opposite /-- Notation typeclass (with no default notation!) for an algebraic structure with a star operation. -/ class has_star (R : Type u) := (star : R → R) variables {R : Type u} export has_star (star) /-- A star operation (e.g. complex conjugate). -/ add_decl_doc star /-- Typeclass for a star operation with is involutive. -/ class has_involutive_star (R : Type u) extends has_star R := (star_involutive : function.involutive star) export has_involutive_star (star_involutive) @[simp] lemma star_star [has_involutive_star R] (r : R) : star (star r) = r := star_involutive _ lemma star_injective [has_involutive_star R] : function.injective (star : R → R) := star_involutive.injective /-- Typeclass for a trivial star operation. This is mostly meant for `ℝ`. -/ class has_trivial_star (R : Type u) [has_star R] := (star_trivial : ∀ (r : R), star r = r) export has_trivial_star (star_trivial) attribute [simp] star_trivial /-- A `*`-monoid is a monoid `R` with an involutive operations `star` so `star (r * s) = star s * star r`. -/ class star_monoid (R : Type u) [monoid R] extends has_involutive_star R := (star_mul : ∀ r s : R, star (r * s) = star s * star r) export star_monoid (star_mul) attribute [simp] star_mul /-- In a commutative ring, make `simp` prefer leaving the order unchanged. -/ @[simp] lemma star_mul' [comm_monoid R] [star_monoid R] (x y : R) : star (x * y) = star x * star y := (star_mul x y).trans (mul_comm _ _) /-- `star` as an `mul_equiv` from `R` to `Rᵐᵒᵖ` -/ @[simps apply] def star_mul_equiv [monoid R] [star_monoid R] : R ≃* Rᵐᵒᵖ := { to_fun := λ x, mul_opposite.op (star x), map_mul' := λ x y, (star_mul x y).symm ▸ (mul_opposite.op_mul _ _), ..(has_involutive_star.star_involutive.to_equiv star).trans op_equiv} /-- `star` as a `mul_aut` for commutative `R`. -/ @[simps apply] def star_mul_aut [comm_monoid R] [star_monoid R] : mul_aut R := { to_fun := star, map_mul' := star_mul', ..(has_involutive_star.star_involutive.to_equiv star) } variables (R) @[simp] lemma star_one [monoid R] [star_monoid R] : star (1 : R) = 1 := op_injective $ (star_mul_equiv : R ≃* Rᵐᵒᵖ).map_one.trans (op_one _).symm variables {R} @[simp] lemma star_pow [monoid R] [star_monoid R] (x : R) (n : ℕ) : star (x ^ n) = star x ^ n := op_injective $ ((star_mul_equiv : R ≃* Rᵐᵒᵖ).to_monoid_hom.map_pow x n).trans (op_pow (star x) n).symm @[simp] lemma star_inv [group R] [star_monoid R] (x : R) : star (x⁻¹) = (star x)⁻¹ := op_injective $ ((star_mul_equiv : R ≃* Rᵐᵒᵖ).to_monoid_hom.map_inv x).trans (op_inv (star x)).symm @[simp] lemma star_zpow [group R] [star_monoid R] (x : R) (z : ℤ) : star (x ^ z) = star x ^ z := op_injective $ ((star_mul_equiv : R ≃* Rᵐᵒᵖ).to_monoid_hom.map_zpow x z).trans (op_zpow (star x) z).symm /-- When multiplication is commutative, `star` preserves division. -/ @[simp] lemma star_div [comm_group R] [star_monoid R] (x y : R) : star (x / y) = star x / star y := (star_mul_aut : R ≃* R).to_monoid_hom.map_div _ _ section open_locale big_operators @[simp] lemma star_prod [comm_monoid R] [star_monoid R] {α : Type*} (s : finset α) (f : α → R): star (∏ x in s, f x) = ∏ x in s, star (f x) := (star_mul_aut : R ≃* R).map_prod _ _ end /-- Any commutative monoid admits the trivial `*`-structure. See note [reducible non-instances]. -/ @[reducible] def star_monoid_of_comm {R : Type*} [comm_monoid R] : star_monoid R := { star := id, star_involutive := λ x, rfl, star_mul := mul_comm } section local attribute [instance] star_monoid_of_comm /-- Note that since `star_monoid_of_comm` is reducible, `simp` can already prove this. --/ lemma star_id_of_comm {R : Type*} [comm_semiring R] {x : R} : star x = x := rfl end /-- A `*`-additive monoid `R` is an additive monoid with an involutive `star` operation which preserves addition. -/ class star_add_monoid (R : Type u) [add_monoid R] extends has_involutive_star R := (star_add : ∀ r s : R, star (r + s) = star r + star s) export star_add_monoid (star_add) attribute [simp] star_add /-- `star` as an `add_equiv` -/ @[simps apply] def star_add_equiv [add_monoid R] [star_add_monoid R] : R ≃+ R := { to_fun := star, map_add' := star_add, ..(has_involutive_star.star_involutive.to_equiv star)} variables (R) @[simp] lemma star_zero [add_monoid R] [star_add_monoid R] : star (0 : R) = 0 := (star_add_equiv : R ≃+ R).map_zero variables {R} @[simp] lemma star_neg [add_group R] [star_add_monoid R] (r : R) : star (-r) = - star r := (star_add_equiv : R ≃+ R).map_neg _ @[simp] lemma star_sub [add_group R] [star_add_monoid R] (r s : R) : star (r - s) = star r - star s := (star_add_equiv : R ≃+ R).map_sub _ _ @[simp] lemma star_nsmul [add_comm_monoid R] [star_add_monoid R] (x : R) (n : ℕ) : star (n • x) = n • star x := (star_add_equiv : R ≃+ R).to_add_monoid_hom.map_nsmul _ _ @[simp] lemma star_zsmul [add_comm_group R] [star_add_monoid R] (x : R) (n : ℤ) : star (n • x) = n • star x := (star_add_equiv : R ≃+ R).to_add_monoid_hom.map_zsmul _ _ section open_locale big_operators @[simp] lemma star_sum [add_comm_monoid R] [star_add_monoid R] {α : Type*} (s : finset α) (f : α → R): star (∑ x in s, f x) = ∑ x in s, star (f x) := (star_add_equiv : R ≃+ R).map_sum _ _ end /-- A `*`-ring `R` is a (semi)ring with an involutive `star` operation which is additive which makes `R` with its multiplicative structure into a `*`-monoid (i.e. `star (r * s) = star s * star r`). -/ class star_ring (R : Type u) [semiring R] extends star_monoid R := (star_add : ∀ r s : R, star (r + s) = star r + star s) @[priority 100] instance star_ring.to_star_add_monoid [semiring R] [star_ring R] : star_add_monoid R := { star_add := star_ring.star_add } /-- `star` as an `ring_equiv` from `R` to `Rᵐᵒᵖ` -/ @[simps apply] def star_ring_equiv [semiring R] [star_ring R] : R ≃+* Rᵐᵒᵖ := { to_fun := λ x, mul_opposite.op (star x), ..star_add_equiv.trans (mul_opposite.op_add_equiv : R ≃+ Rᵐᵒᵖ), ..star_mul_equiv } /-- `star` as a ring automorphism, for commutative `R`. -/ @[simps apply] def star_ring_aut [comm_semiring R] [star_ring R] : ring_aut R := { to_fun := star, ..star_add_equiv, ..star_mul_aut } variables (R) /-- `star` as a ring endomorphism, for commutative `R`. This is used to denote complex conjugation, and is available under the notation `conj` in the locale `complex_conjugate`. Note that this is the preferred form (over `star_ring_aut`, available under the same hypotheses) because the notation `E →ₗ⋆[R] F` for an `R`-conjugate-linear map (short for `E →ₛₗ[star_ring_end R] F`) does not pretty-print if there is a coercion involved, as would be the case for `(↑star_ring_aut : R →* R)`. -/ def star_ring_end [comm_semiring R] [star_ring R] : R →+* R := @star_ring_aut R _ _ variables {R} localized "notation `conj` := star_ring_end _" in complex_conjugate /-- This is not a simp lemma, since we usually want simp to keep `star_ring_end` bundled. For example, for complex conjugation, we don't want simp to turn `conj x` into the bare function `star x` automatically since most lemmas are about `conj x`. -/ lemma star_ring_end_apply [comm_semiring R] [star_ring R] {x : R} : star_ring_end R x = star x := rfl @[simp] lemma star_ring_end_self_apply [comm_semiring R] [star_ring R] (x : R) : star_ring_end R (star_ring_end R x) = x := star_star x -- A more convenient name for complex conjugation alias star_ring_end_self_apply ← complex.conj_conj alias star_ring_end_self_apply ← is_R_or_C.conj_conj @[simp] lemma star_inv' [division_ring R] [star_ring R] (x : R) : star (x⁻¹) = (star x)⁻¹ := op_injective $ ((star_ring_equiv : R ≃+* Rᵐᵒᵖ).to_ring_hom.map_inv x).trans (op_inv (star x)).symm @[simp] lemma star_zpow₀ [division_ring R] [star_ring R] (x : R) (z : ℤ) : star (x ^ z) = star x ^ z := op_injective $ ((star_ring_equiv : R ≃+* Rᵐᵒᵖ).to_ring_hom.map_zpow x z).trans (op_zpow (star x) z).symm /-- When multiplication is commutative, `star` preserves division. -/ @[simp] lemma star_div' [field R] [star_ring R] (x y : R) : star (x / y) = star x / star y := (star_ring_end R).map_div _ _ @[simp] lemma star_bit0 [ring R] [star_ring R] (r : R) : star (bit0 r) = bit0 (star r) := by simp [bit0] @[simp] lemma star_bit1 [ring R] [star_ring R] (r : R) : star (bit1 r) = bit1 (star r) := by simp [bit1] /-- Any commutative semiring admits the trivial `*`-structure. See note [reducible non-instances]. -/ @[reducible] def star_ring_of_comm {R : Type*} [comm_semiring R] : star_ring R := { star := id, star_add := λ x y, rfl, ..star_monoid_of_comm } /-- An ordered `*`-ring is a ring which is both an `ordered_add_comm_group` and a `*`-ring, and `0 ≤ r ↔ ∃ s, r = star s * s`. -/ class star_ordered_ring (R : Type u) [ring R] [partial_order R] extends star_ring R := (add_le_add_left : ∀ a b : R, a ≤ b → ∀ c : R, c + a ≤ c + b) (nonneg_iff : ∀ r : R, 0 ≤ r ↔ ∃ s, r = star s * s) namespace star_ordered_ring variables [ring R] [partial_order R] [star_ordered_ring R] @[priority 100] -- see note [lower instance priority] instance : ordered_add_comm_group R := { ..show ring R, by apply_instance, ..show partial_order R, by apply_instance, ..show star_ordered_ring R, by apply_instance } end star_ordered_ring lemma star_mul_self_nonneg [ring R] [partial_order R] [star_ordered_ring R] {r : R} : 0 ≤ star r * r := (star_ordered_ring.nonneg_iff _).mpr ⟨r, rfl⟩ lemma star_mul_self_nonneg' [ring R] [partial_order R] [star_ordered_ring R] {r : R} : 0 ≤ r * star r := by { nth_rewrite_rhs 0 [←star_star r], exact star_mul_self_nonneg } /-- A star module `A` over a star ring `R` is a module which is a star add monoid, and the two star structures are compatible in the sense `star (r • a) = star r • star a`. Note that it is up to the user of this typeclass to enforce `[semiring R] [star_ring R] [add_comm_monoid A] [star_add_monoid A] [module R A]`, and that the statement only requires `[has_star R] [has_star A] [has_scalar R A]`. If used as `[comm_ring R] [star_ring R] [semiring A] [star_ring A] [algebra R A]`, this represents a star algebra. -/ class star_module (R : Type u) (A : Type v) [has_star R] [has_star A] [has_scalar R A] := (star_smul : ∀ (r : R) (a : A), star (r • a) = star r • star a) export star_module (star_smul) attribute [simp] star_smul /-- A commutative star monoid is a star module over itself via `monoid.to_mul_action`. -/ instance star_monoid.to_star_module [comm_monoid R] [star_monoid R] : star_module R R := ⟨star_mul'⟩ namespace ring_hom_inv_pair /-- Instance needed to define star-linear maps over a commutative star ring (ex: conjugate-linear maps when R = ℂ). -/ instance [comm_semiring R] [star_ring R] : ring_hom_inv_pair (star_ring_end R) (star_ring_end R) := ⟨ring_hom.ext star_star, ring_hom.ext star_star⟩ end ring_hom_inv_pair /-! ### Instances -/ namespace units variables [monoid R] [star_monoid R] instance : star_monoid Rˣ := { star := λ u, { val := star u, inv := star ↑u⁻¹, val_inv := (star_mul _ _).symm.trans $ (congr_arg star u.inv_val).trans $ star_one _, inv_val := (star_mul _ _).symm.trans $ (congr_arg star u.val_inv).trans $ star_one _ }, star_involutive := λ u, units.ext (star_involutive _), star_mul := λ u v, units.ext (star_mul _ _) } @[simp] lemma coe_star (u : Rˣ) : ↑(star u) = (star ↑u : R) := rfl @[simp] lemma coe_star_inv (u : Rˣ) : ↑(star u)⁻¹ = (star ↑u⁻¹ : R) := rfl instance {A : Type*} [has_star A] [has_scalar R A] [star_module R A] : star_module Rˣ A := ⟨λ u a, (star_smul ↑u a : _)⟩ end units lemma is_unit.star [monoid R] [star_monoid R] {a : R} : is_unit a → is_unit (star a) | ⟨u, hu⟩ := ⟨star u, hu ▸ rfl⟩ @[simp] lemma is_unit_star [monoid R] [star_monoid R] {a : R} : is_unit (star a) ↔ is_unit a := ⟨λ h, star_star a ▸ h.star, is_unit.star⟩ lemma ring.inverse_star [semiring R] [star_ring R] (a : R) : ring.inverse (star a) = star (ring.inverse a) := begin by_cases ha : is_unit a, { obtain ⟨u, rfl⟩ := ha, rw [ring.inverse_unit, ←units.coe_star, ring.inverse_unit, ←units.coe_star_inv], }, rw [ring.inverse_non_unit _ ha, ring.inverse_non_unit _ (mt is_unit_star.mp ha), star_zero], end namespace mul_opposite /-- The opposite type carries the same star operation. -/ instance [has_star R] : has_star (Rᵐᵒᵖ) := { star := λ r, op (star (r.unop)) } @[simp] lemma unop_star [has_star R] (r : Rᵐᵒᵖ) : unop (star r) = star (unop r) := rfl @[simp] lemma op_star [has_star R] (r : R) : op (star r) = star (op r) := rfl instance [has_involutive_star R] : has_involutive_star (Rᵐᵒᵖ) := { star_involutive := λ r, unop_injective (star_star r.unop) } instance [monoid R] [star_monoid R] : star_monoid (Rᵐᵒᵖ) := { star_mul := λ x y, unop_injective (star_mul y.unop x.unop) } instance [add_monoid R] [star_add_monoid R] : star_add_monoid (Rᵐᵒᵖ) := { star_add := λ x y, unop_injective (star_add x.unop y.unop) } instance [semiring R] [star_ring R] : star_ring (Rᵐᵒᵖ) := { .. mul_opposite.star_add_monoid } end mul_opposite /-- A commutative star monoid is a star module over its opposite via `monoid.to_opposite_mul_action`. -/ instance star_monoid.to_opposite_star_module [comm_monoid R] [star_monoid R] : star_module Rᵐᵒᵖ R := ⟨λ r s, star_mul' s r.unop⟩
da18933dcfc89067358ea844db017818f61f7643
618003631150032a5676f229d13a079ac875ff77
/src/set_theory/schroeder_bernstein.lean
cf04538811416202a9b21b230ebb22d025c67853
[ "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
5,000
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro The Schröder-Bernstein theorem, and well ordering of cardinals. -/ import order.fixed_points import order.zorn open set classical open_locale classical universes u v namespace function namespace embedding section antisymm variables {α : Type u} {β : Type v} theorem schroeder_bernstein {f : α → β} {g : β → α} (hf : injective f) (hg : injective g) : ∃h:α→β, bijective h := let s : set α := lfp $ λs, - (g '' - (f '' s)) in have hs : s = - (g '' - (f '' s)), from lfp_eq $ assume s t h, compl_subset_compl.mpr $ image_subset _ $ compl_subset_compl.mpr $ image_subset _ h, have hns : - s = g '' - (f '' s), from compl_inj $ by simp [hs.symm], let g' := λa, @inv_fun β ⟨f a⟩ α g a in have g'g : g' ∘ g = id, from funext $ assume b, @left_inverse_inv_fun _ ⟨f (g b)⟩ _ _ hg b, have hg'ns : g' '' (-s) = - (f '' s), by rw [hns, ←image_comp, g'g, image_id], let h := λa, if a ∈ s then f a else g' a in have h '' univ = univ, from calc h '' univ = h '' s ∪ h '' (- s) : by rw [←image_union, union_compl_self] ... = f '' s ∪ g' '' (-s) : congr (congr_arg (∪) (image_congr $ by simp [h, if_pos] {contextual := tt})) (image_congr $ by simp [h, if_neg] {contextual := tt}) ... = univ : by rw [hg'ns, union_compl_self], have surjective h, from assume b, have b ∈ h '' univ, by rw [this]; trivial, let ⟨a, _, eq⟩ := this in ⟨a, eq⟩, have split : ∀x∈s, ∀y∉s, h x = h y → false, from assume x hx y hy eq, have y ∈ g '' - (f '' s), by rwa [←hns], let ⟨y', hy', eq_y'⟩ := this in have f x = y', from calc f x = g' y : by simp [h, hx, hy, if_pos, if_neg] at eq; assumption ... = (g' ∘ g) y' : by simp [(∘), eq_y'] ... = _ : by simp [g'g], have y' ∈ f '' s, from this ▸ mem_image_of_mem _ hx, hy' this, have injective h, from assume x y eq, by_cases (assume hx : x ∈ s, by_cases (assume hy : y ∈ s, by simp [h, hx, hy, if_pos, if_neg] at eq; exact hf eq) (assume hy : y ∉ s, (split x hx y hy eq).elim)) (assume hx : x ∉ s, by_cases (assume hy : y ∈ s, (split y hy x hx eq.symm).elim) (assume hy : y ∉ s, have x ∈ g '' - (f '' s), by rwa [←hns], let ⟨x', hx', eqx⟩ := this in have y ∈ g '' - (f '' s), by rwa [←hns], let ⟨y', hy', eqy⟩ := this in have g' x = g' y, by simp [h, hx, hy, if_pos, if_neg] at eq; assumption, have (g' ∘ g) x' = (g' ∘ g) y', by simp [(∘), eqx, eqy, this], have x' = y', by rwa [g'g] at this, calc x = g x' : eqx.symm ... = g y' : by rw [this] ... = y : eqy)), ⟨h, ‹injective h›, ‹surjective h›⟩ theorem antisymm : (α ↪ β) → (β ↪ α) → nonempty (α ≃ β) | ⟨e₁, h₁⟩ ⟨e₂, h₂⟩ := let ⟨f, hf⟩ := schroeder_bernstein h₁ h₂ in ⟨equiv.of_bijective hf⟩ end antisymm section wo parameters {ι : Type u} {β : ι → Type v} @[reducible] private def sets := {s : set (∀ i, β i) | ∀ (x ∈ s) (y ∈ s) i, (x : ∀ i, β i) i = y i → x = y} theorem injective_min (I : nonempty ι) : ∃ i, nonempty (∀ j, β i ↪ β j) := let ⟨s, hs, ms⟩ := show ∃s∈sets, ∀a∈sets, s ⊆ a → a = s, from zorn.zorn_subset sets (λ c hc hcc, ⟨⋃₀ c, λ x ⟨p, hpc, hxp⟩ y ⟨q, hqc, hyq⟩ i hi, (hcc.total hpc hqc).elim (λ h, hc hqc x (h hxp) y hyq i hi) (λ h, hc hpc x hxp y (h hyq) i hi), λ _, subset_sUnion_of_mem⟩) in let ⟨i, e⟩ := show ∃ i, ∀ y, ∃ x ∈ s, (x : ∀ i, β i) i = y, from classical.by_contradiction $ λ h, have h : ∀ i, ∃ y, ∀ x ∈ s, (x : ∀ i, β i) i ≠ y, by simpa only [not_exists, classical.not_forall] using h, let ⟨f, hf⟩ := axiom_of_choice h in have f ∈ s, from have insert f s ∈ sets := λ x hx y hy, begin cases hx; cases hy, {simp [hx, hy]}, { subst x, exact λ i e, (hf i y hy e.symm).elim }, { subst y, exact λ i e, (hf i x hx e).elim }, { exact hs x hx y hy } end, ms _ this (subset_insert f s) ▸ mem_insert _ _, let ⟨i⟩ := I in hf i f this rfl in let ⟨f, hf⟩ := axiom_of_choice e in ⟨i, ⟨λ j, ⟨λ a, f a j, λ a b e', let ⟨sa, ea⟩ := hf a, ⟨sb, eb⟩ := hf b in by rw [← ea, ← eb, hs _ sa _ sb _ e']⟩⟩⟩ end wo theorem total {α : Type u} {β : Type v} : nonempty (α ↪ β) ∨ nonempty (β ↪ α) := match @injective_min bool (λ b, cond b (ulift α) (ulift.{(max u v) v} β)) ⟨tt⟩ with | ⟨tt, ⟨h⟩⟩ := let ⟨f, hf⟩ := h ff in or.inl ⟨embedding.congr equiv.ulift equiv.ulift ⟨f, hf⟩⟩ | ⟨ff, ⟨h⟩⟩ := let ⟨f, hf⟩ := h tt in or.inr ⟨embedding.congr equiv.ulift equiv.ulift ⟨f, hf⟩⟩ end end embedding end function
7460fc0bfc716e00ab644dd1351cd397ac4d1eee
efae59ef7a34d2c8311cc27d495a5feddaddc997
/src/pending_lemmas.lean
af99ade3d107dd2abc5179809a847721ffd44c75
[]
no_license
ChrisHughes24/bigop
e4de482154ecdc73c588b2c326f8358d1e3d1e34
95354e18208b2847ad07539c2a0dfff78336f9b3
refs/heads/master
1,584,582,600,883
1,527,536,982,000
1,527,536,982,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,465
lean
/- The lemmas in this file will soon be in mathlib -/ import data.list.basic open list nat variables {α : Type*} {β : Type*} theorem nth_map (f : α → β) : ∀ l n, nth (map f l) n = (nth l n).map f | [] n := rfl | (a :: l) 0 := rfl | (a :: l) (n+1) := nth_map l n theorem nth_le_map (f : α → β) {l n} (H1 H2) : nth_le (map f l) n H1 = f (nth_le l n H2) := option.some.inj $ by rw [← nth_le_nth, nth_map, nth_le_nth]; refl theorem map_add_range' (a) : ∀ s n : ℕ, map ((+) a) (range' s n) = range' (a + s) n | s 0 := rfl | s (n+1) := congr_arg (cons _) (map_add_range' (s+1) n) theorem range_succ_eq_map (n : ℕ) : range (n + 1) = 0 :: map succ (range n) := by rw [range_eq_range', range_eq_range', range', add_comm, ← map_add_range']; congr; exact funext one_add theorem reverse_range' : ∀ s n : ℕ, reverse (range' s n) = map (λ i, s + n - 1 - i) (range n) | s 0 := rfl | s (n+1) := by rw [range'_concat, reverse_append, range_succ_eq_map]; simpa [show s + (n + 1) - 1 = s + n, from rfl, (∘), λ a i, show a - 1 - i = a - succ i, by rw [nat.sub_sub, add_comm]; refl] using reverse_range' s n theorem range'_eq_map_range (s n : ℕ) : range' s n = map ((+) s) (range n) := by rw [range_eq_range', map_add_range']; refl lemma reverse_range'_map_range' (a b : ℕ) : reverse (range' a (b+1-a)) = map (λ i, a+b-i) (range' a (b+1-a)) := begin rw [reverse_range', range'_eq_map_range, map_map], apply map_congr, intros i H, simp at *, rw [nat.add_sub_add_left, nat.add_sub_cancel'], {refl}, apply le_of_not_le (λ h, _), rw sub_eq_zero_of_le h at H, exact not_lt_zero _ H end lemma filter_congr {α : Type*} {p q : α → Prop} [decidable_pred p] [decidable_pred q] : ∀ {l : list α}, (∀ x ∈ l, p x ↔ q x) → filter p l = filter q l | [] _ := rfl | (a::l) h := by simp at h; by_cases pa : p a; [simp [pa, h.1.1 pa, filter_congr h.2], simp [pa, mt h.1.2 pa, filter_congr h.2]] lemma filter_ext {α : Type*} {r: list α} (P P') [decidable_pred P] [decidable_pred P'] (HP : ∀ i ∈ r, P i = P' i) : filter P r = filter P' r := begin induction r with h t IH, { simp }, { have HPh : P h = P' h := HP h (by simp), have : ∀ (i : α), i ∈ t → P i = P' i, { intros i i_t, exact (HP i $ by simp [i_t]) }, by_cases H : P h, { have H' : P' h := HPh ▸ H, simp [H, H', IH this] }, { have H' : ¬ P' h := HPh ▸ H, simp [H, H', IH this] } } end lemma foldr_ext {α : Type*} {β : Type*} {l : list α} (f f' : α → β → β) (s : β) (H : ∀ a ∈ l, ∀ b : β, f a b = f' a b) : foldr f s l = foldr f' s l := by induction l; simp * {contextual := tt} lemma range'_add_map (a b k : ℕ) : range' (a+k) b = map (λ x, x + k) (range' a b) := begin revert a, induction b with b IH; intro a, { refl }, { simpa using (IH $ a + 1) } end lemma range'_sub_map (a b k : ℕ) : range' a b = map (λ x, x - k) (range' (a+k) b) := begin suffices : (λ (x : ℕ), x - k) ∘ (λ (x : ℕ), x + k) = id, { rw [range'_add_map, map_map, this, map_id] }, { funext, simp [nat.add_sub_cancel_left] } end lemma filter_map_comm {I : Type*} {J : Type*} (f : I → J) (P : J → Prop) (r: list I) [decidable_pred P] : filter P (map f r) = map f (filter (P ∘ f) r) := begin induction r with h _ IH, { simp }, { by_cases H : P (f h) ; simp [filter_cons_of_pos, filter_cons_of_neg, H, IH] } end
141e1601151067cc2baee4675be55aeb8665791b
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/field_theory/finiteness.lean
93f0d00857f633d35197dbc38c17c7351ce81279
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
4,313
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import ring_theory.finiteness import linear_algebra.dimension /-! # A module over a division ring is noetherian if and only if it is finite. -/ universes u v open_locale classical open cardinal submodule module function namespace is_noetherian variables {K : Type u} {V : Type v} [division_ring K] [add_comm_group V] [module K V] -- PROJECT: Show all division rings are noetherian. -- This is currently annoying because we only have ideal of commutative rings. variables [is_noetherian_ring K] /-- A module over a division ring is noetherian if and only if its dimension (as a cardinal) is strictly less than the first infinite cardinal `omega`. -/ lemma iff_dim_lt_omega : is_noetherian K V ↔ module.rank K V < omega.{v} := begin let b := basis.of_vector_space K V, have := b.mk_eq_dim, simp only [lift_id] at this, rw [← this, lt_omega_iff_fintype, ← @set.set_of_mem_eq _ (basis.of_vector_space_index K V), ← subtype.range_coe_subtype], split, { intro, resetI, simpa using finite_of_linear_independent (basis.of_vector_space_index.linear_independent K V) }, { assume hbfinite, refine @is_noetherian_of_linear_equiv K (⊤ : submodule K V) V _ _ _ _ _ (linear_equiv.of_top _ rfl) (id _), refine is_noetherian_of_fg_of_noetherian _ ⟨set.finite.to_finset hbfinite, _⟩, rw [set.finite.coe_to_finset, ← b.span_eq, basis.coe_of_vector_space] } end variables (K V) /-- The dimension of a noetherian module over a division ring, as a cardinal, is strictly less than the first infinite cardinal `omega`. -/ lemma dim_lt_omega : ∀ [is_noetherian K V], module.rank K V < omega.{v} := is_noetherian.iff_dim_lt_omega.1 variables {K V} /-- In a noetherian module over a division ring, all bases are indexed by a finite type. -/ noncomputable def fintype_basis_index {ι : Type*} [is_noetherian K V] (b : basis ι K V) : fintype ι := b.fintype_index_of_dim_lt_omega (dim_lt_omega K V) /-- In a noetherian module over a division ring, `basis.of_vector_space` is indexed by a finite type. -/ noncomputable instance [is_noetherian K V] : fintype (basis.of_vector_space_index K V) := fintype_basis_index (basis.of_vector_space K V) /-- In a noetherian module over a division ring, if a basis is indexed by a set, that set is finite. -/ lemma finite_basis_index {ι : Type*} {s : set ι} [is_noetherian K V] (b : basis s K V) : s.finite := b.finite_index_of_dim_lt_omega (dim_lt_omega K V) variables (K V) /-- In a noetherian module over a division ring, there exists a finite basis. This is the indexing `finset`. -/ noncomputable def finset_basis_index [is_noetherian K V] : finset V := (finite_basis_index (basis.of_vector_space K V)).to_finset @[simp] lemma coe_finset_basis_index [is_noetherian K V] : (↑(finset_basis_index K V) : set V) = basis.of_vector_space_index K V := set.finite.coe_to_finset _ @[simp] lemma coe_sort_finset_basis_index [is_noetherian K V] : ((finset_basis_index K V) : Type*) = basis.of_vector_space_index K V := set.finite.coe_sort_to_finset _ /-- In a noetherian module over a division ring, there exists a finite basis. This is indexed by the `finset` `finite_dimensional.finset_basis_index`. This is in contrast to the result `finite_basis_index (basis.of_vector_space K V)`, which provides a set and a `set.finite`. -/ noncomputable def finset_basis [is_noetherian K V] : basis (finset_basis_index K V) K V := (basis.of_vector_space K V).reindex (by simp) @[simp] lemma range_finset_basis [is_noetherian K V] : set.range (finset_basis K V) = basis.of_vector_space_index K V := by rw [finset_basis, basis.range_reindex, basis.range_of_vector_space] variables {K V} /-- A module over a division ring is noetherian if and only if it is finitely generated. -/ lemma iff_fg : is_noetherian K V ↔ module.finite K V := begin split, { introI h, exact ⟨⟨finset_basis_index K V, by { convert (finset_basis K V).span_eq, simp }⟩⟩ }, { rintros ⟨s, hs⟩, rw [is_noetherian.iff_dim_lt_omega, ← dim_top, ← hs], exact lt_of_le_of_lt (dim_span_le _) (lt_omega_iff_finite.2 (set.finite_mem_finset s)) } end end is_noetherian
7a310b3d609cf0eaab0e9dc46a088c2f00dd26b0
f08e5018e0d696ec84edb728e81a5744332d856e
/logic.lean
1368cd3145b5c2a9d348f8b90ea9c7443ca2f7cd
[]
no_license
Shamrock-Frost/tao-analysis-one
8ad24ac6f69920ed2b81d2c6646e73a2328bfbe7
82b3efa0f79880a1acbc09e88afc852be33d1c4f
refs/heads/master
1,611,264,143,494
1,495,584,000,000
1,495,584,000,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,667
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura Theorems that require decidability hypotheses are in the namespace "decidable". Classical versions are in the namespace "classical". Note: in the presence of automation, this whole file may be unnecessary. On the other hand, maybe it is useful for writing automation. -/ /- propositional connectives -/ section propositional variables {a b c d : Prop} /- implies -/ theorem contrapos {a b : Prop} (h : a → b) : ¬ b → ¬ a := λ hnb ha, hnb (h ha) theorem implies_self (h : a) : a := h theorem implies_intro (h : a) (h₂ : b) : a := h theorem true_implies_iff (a : Prop) : (true → a) ↔ a := iff.intro (assume H, H trivial) implies_intro theorem implies_false_iff (a : Prop) : (a → false) ↔ ¬ a := iff.rfl /- not -/ theorem {u} not_elim {A : Sort u} (H1 : ¬a) (H2 : a) : A := absurd H2 H1 theorem not_not_of_not_implies : ¬(a → b) → ¬¬a := contrapos not_elim theorem not_of_not_implies : ¬(a → b) → ¬b := contrapos implies_intro theorem decidable.not_not_iff (a : Prop) [decidable a] : ¬¬a ↔ a := iff.intro decidable.by_contradiction not_not_intro theorem decidable.not_not_elim {a : Prop} [decidable a] : ¬¬a → a := decidable.by_contradiction theorem decidable.of_not_implies [decidable a] (h : ¬ (a → b)) : a := decidable.by_contradiction (not_not_of_not_implies h) /- and -/ theorem not_and_of_not_left (b : Prop) : ¬a → ¬(a ∧ b) := contrapos and.left theorem not_and_of_not_right (a : Prop) {b : Prop} : ¬b → ¬(a ∧ b) := contrapos and.right theorem and_implies_left (h : a → b) : a ∧ c → b ∧ c := and_implies h implies_self theorem and_implies_right (h : a → b) : c ∧ a → c ∧ b := and_implies implies_self h theorem and_of_and_of_implies_of_implies (h₁ : a ∧ b) (h₂ : a → c) (h₃ : b → d) : c ∧ d := and_implies h₂ h₃ h₁ theorem and_of_and_of_imp_left (h₁ : a ∧ c) (h : a → b) : b ∧ c := and_implies_left h h₁ theorem and_of_and_of_imp_right (h₁ : c ∧ a) (h : a → b) : c ∧ b := and_implies_right h h₁ theorem and_imp_iff (a b c : Prop) : (a ∧ b → c) ↔ (a → b → c) := iff.intro (λ h a b, h ⟨a, b⟩) and.rec theorem and_not_self_iff (a : Prop) : a ∧ ¬ a ↔ false := iff.intro (assume h, (h^.right) (h^.left)) (assume h, h^.elim) theorem not_and_self_iff (a : Prop) : ¬ a ∧ a ↔ false := iff.intro (assume ⟨hna, ha⟩, hna ha) false.elim /- or -/ theorem or_of_or_of_implies_of_implies (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → d) : c ∨ d := or.imp h₂ h₃ h₁ theorem or_of_or_of_implies_left (h₁ : a ∨ c) (h : a → b) : b ∨ c := or.imp_left h h₁ theorem or_of_or_of_implies_right (h₁ : c ∨ a) (h : a → b) : c ∨ b := or.imp_right h h₁ theorem or.elim3 (h : a ∨ b ∨ c) (ha : a → d) (hb : b → d) (hc : c → d) : d := or.elim h ha (assume h₂, or.elim h₂ hb hc) theorem or_resolve_right (h₁ : a ∨ b) (h₂ : ¬a) : b := or.elim h₁ (not_elim h₂) implies_self theorem or_resolve_left (h₁ : a ∨ b) (h₂ : ¬b) : a := or_resolve_right h₁^.symm h₂ theorem or_implies_distrib (a b c : Prop) : ((a ∨ b) → c) ↔ ((a → c) ∧ (b → c)) := iff.intro (λh, and.intro (implies.trans or.inl h) (implies.trans or.inr h)) (and.rec or.rec) theorem or_iff_right_of_imp (ha : a → b) : (a ∨ b) ↔ b := iff.intro (or.rec ha implies_self) or.inr theorem or_iff_left_of_imp (hb : b → a) : (a ∨ b) ↔ a := iff.intro (or.rec implies_self hb) or.inl theorem or_iff_or (h1 : a ↔ c) (h2 : b ↔ d) : (a ∨ b) ↔ (c ∨ d) := iff.intro (or.imp (iff.mp h1) (iff.mp h2)) (or.imp (iff.mpr h1) (iff.mpr h2)) theorem decidable.or_not_self_iff (a : Prop) [decidable a] : a ∨ ¬ a ↔ true := iff.intro (assume h, trivial) (assume h, decidable.em a) theorem decidable.not_or_self_iff (a : Prop) [decidable a] : ¬ a ∨ a ↔ true := iff.intro (assume h, trivial) (assume h, (decidable.em a)^.symm) /- distributivity -/ theorem and_distrib (a b c : Prop) : a ∧ (b ∨ c) ↔ (a ∧ b) ∨ (a ∧ c) := iff.intro (and.rec (λh, or.imp (and.intro h) (and.intro h))) (or.rec (and_implies_right or.inl) (and_implies_right or.inr)) theorem and_distrib_right (a b c : Prop) : (a ∨ b) ∧ c ↔ (a ∧ c) ∨ (b ∧ c) := iff.trans (iff.trans and.comm (and_distrib c a b)) (or_iff_or and.comm and.comm) theorem or_distrib (a b c : Prop) : a ∨ (b ∧ c) ↔ (a ∨ b) ∧ (a ∨ c) := iff.intro (or.rec (λh, and.intro (or.inl h) (or.inl h)) (and_implies or.inr or.inr)) (and.rec (or.rec (implies.trans or.inl implies_intro) (implies.trans and.intro or.imp_right))) theorem or_distrib_right (a b c : Prop) : (a ∧ b) ∨ c ↔ (a ∨ c) ∧ (b ∨ c) := iff.trans (iff.trans or.comm (or_distrib c a b)) (and_congr or.comm or.comm) /- iff -/ theorem implies_iff {a : Prop} (n : Prop) (ha : a) : (a → b) ↔ b := iff.intro (λf, f ha) implies_intro theorem decidable.not_or_of_implies [decidable a] (h : a → b) : ¬ a ∨ b := if ha : a then or.inr (h ha) else or.inl ha theorem implies_of_not_or (h : ¬ a ∨ b) : a → b := assume ha, or.elim h (assume hna, absurd ha hna) (assume hb, hb) theorem decidable.implies_iff_not_or (a b : Prop) [decidable a] : (a → b) ↔ (¬ a ∨ b) := iff.intro decidable.not_or_of_implies implies_of_not_or theorem not_implies_of_and_not (h : a ∧ ¬ b) : ¬ (a → b) := assume h₁, and.right h (h₁ (and.left h)) theorem decidable.and_not_of_not_implies [decidable a] (h : ¬ (a → b)) : a ∧ ¬ b := ⟨decidable.of_not_implies h, not_of_not_implies h⟩ theorem decidable.not_implies_iff_and_not (a b : Prop) [decidable a] : ¬(a → b) ↔ a ∧ ¬b := iff.intro decidable.and_not_of_not_implies not_implies_of_and_not theorem decidable.peirce (a b : Prop) [decidable a] : ((a → b) → a) → a := if ha : a then λ h, ha else λ h, h (λ h', absurd h' ha) /- de morgan's laws -/ theorem not_and_of_not_or_not (h : ¬ a ∨ ¬ b) : ¬ (a ∧ b) := assume ⟨ha, hb⟩, or.elim h (assume hna, hna ha) (assume hnb, hnb hb) theorem decidable.not_or_not_of_not_and [decidable a] (h : ¬ (a ∧ b)) : ¬ a ∨ ¬ b := if ha : a then or.inr (show ¬ b, from assume hb, h ⟨ha, hb⟩) else or.inl ha theorem decidable.not_or_not_of_not_and' [decidable b] (h : ¬ (a ∧ b)) : ¬ a ∨ ¬ b := if hb : b then or.inl (show ¬ a, from assume ha, h ⟨ha, hb⟩) else or.inr hb theorem decidable.not_and_iff (a b : Prop) [decidable a] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := iff.intro decidable.not_or_not_of_not_and not_and_of_not_or_not theorem not_or_of_not_and_not (h : ¬ a ∧ ¬ b) : ¬ (a ∨ b) := assume h₁, or.elim h₁ (assume ha, and.left h ha) (assume hb, and.right h hb) theorem not_and_not_of_not_or (h : ¬ (a ∨ b)) : ¬ a ∧ ¬ b := and.intro (assume ha, h (or.inl ha)) (assume hb, h (or.inr hb)) theorem not_or_iff (a b : Prop) : ¬ (a ∨ b) ↔ ¬ a ∧ ¬ b := iff.intro not_and_not_of_not_or not_or_of_not_and_not theorem decidable.or_iff_not_and_not (a b : Prop) [decidable a] [decidable b] : a ∨ b ↔ ¬ (¬a ∧ ¬b) := by rewrite [-not_or_iff, decidable.not_not_iff] theorem decidable.and_iff_not_or_not (a b : Prop) [decidable a] [decidable b] : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := by rewrite [-decidable.not_and_iff, decidable.not_not_iff] end propositional /- classical versions -/ namespace classical variables {a b c d : Prop} local attribute [instance] prop_decidable theorem not_not_iff (a : Prop) : ¬¬a ↔ a := decidable.not_not_iff a theorem not_not_elim {a : Prop} : ¬¬a → a := decidable.not_not_elim theorem of_not_implies (h : ¬ (a → b)) : a := decidable.of_not_implies h theorem or_not_self_iff (a : Prop) : a ∨ ¬ a ↔ true := decidable.or_not_self_iff a theorem not_or_self_iff (a : Prop) : ¬ a ∨ a ↔ true := decidable.not_or_self_iff a theorem not_or_of_implies (h : a → b) : ¬ a ∨ b := decidable.not_or_of_implies h theorem implies_iff_not_or (a b : Prop) : (a → b) ↔ (¬ a ∨ b) := decidable.implies_iff_not_or a b theorem and_not_of_not_implies (h : ¬ (a → b)) : a ∧ ¬ b := decidable.and_not_of_not_implies h theorem not_implies_iff_and_not (a b : Prop) : ¬(a → b) ↔ a ∧ ¬b := decidable.not_implies_iff_and_not a b theorem peirce (a b : Prop) : ((a → b) → a) → a := decidable.peirce a b theorem not_or_not_of_not_and (h : ¬ (a ∧ b)) : ¬ a ∨ ¬ b := decidable.not_or_not_of_not_and h theorem not_and_iff (a b : Prop) : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := decidable.not_and_iff a b theorem or_iff_not_and_not (a b : Prop) : a ∨ b ↔ ¬ (¬a ∧ ¬b) := decidable.or_iff_not_and_not a b theorem and_iff_not_or_not (a b : Prop) : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := decidable.and_iff_not_or_not a b end classical /- quantifiers -/ section quantifiers universe variable u variables {A : Type u} {p q : A → Prop} {b : Prop} theorem forall_of_forall (h : ∀ x, (p x → q x)) (h₁ : ∀ x, p x) : ∀ x, q x := take x, h x (h₁ x) theorem exists_of_exists (h : ∀ x, (p x → q x)) (h₁ : ∃ x, p x) : ∃ x, q x := match h₁ with ⟨x, hpx⟩ := ⟨x, h x hpx⟩ end theorem forall_implies_of_exists_implies (h : (∃ x, p x) → b) : ∀ x, p x → b := take x, assume hpx, h ⟨x, hpx⟩ theorem exists_implies_of_forall_implies (h : ∀ x, p x → b) : (∃ x, p x) → b := Exists.rec h theorem exists_implies_distrib (p : A → Prop) (b : Prop) : ((∃ x, p x) → b) ↔ (∀ x, p x → b) := iff.intro forall_implies_of_exists_implies exists_implies_of_forall_implies --theorem forall_not_of_not_exists (h : ¬ ∃ x, p x) : ∀ x, ¬ p x := --forall_implies_of_exists_implies h theorem not_exists_of_forall_not (h : ∀ x, ¬ p x) : ¬ ∃ x, p x := exists_implies_of_forall_implies h theorem not_exists_iff_forall_not (p : A → Prop) : (¬ ∃ x, p x) ↔ (∀ x, ¬ p x) := exists_implies_distrib p false theorem decidable.exists_not_of_not_forall [decidable (∃ x, ¬ p x)] [∀ x, decidable (p x)] (h : ¬ ∀ x, p x) : ∃ x, ¬ p x := decidable.by_contradiction (assume h₁, h (take x, decidable.by_contradiction (assume hnpx, h₁ ⟨x, hnpx⟩))) theorem not_forall_of_exists_not (h : ∃ x, ¬ p x) : ¬ ∀ x, p x := assume h₁, match h with ⟨x, hnpx⟩ := hnpx (h₁ x) end theorem decidable.not_forall_iff_exists_not (p : A → Prop) [decidable (∃ x, ¬ p x)] [∀ x, decidable (p x)] : (¬ ∀ x, p x) ↔ (∃ x, ¬ p x) := iff.intro decidable.exists_not_of_not_forall not_forall_of_exists_not theorem forall_true_iff : (∀ x : A, true) ↔ true := iff_true_intro (λ h, trivial) theorem forall_p_iff_p (A : Type u) [inhabited A] (p : Prop) : (∀ x : A, p) ↔ p := iff.intro (λ h, h (inhabited.default A)) (λ hp x, hp) theorem exists_p_iff_p (A : Type u) [inhabited A] (p : Prop) : (∃ x : A, p) ↔ p := iff.intro (Exists.rec (λ x (hpx : p), hpx)) (λ hp, ⟨default A, hp⟩) theorem forall_and_distrib (p q : A → Prop) : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) := iff.intro (assume h, ⟨(take x, (h x)^.left), (take x, (h x)^.right)⟩) (assume h x, ⟨h^.left x, h^.right x⟩) theorem exists_or_distrib (p q : A → Prop) : (∃ x, p x ∨ q x) ↔ (∃ x, p x) ∨ (∃ x, q x) := iff.intro (assume ⟨x, hpq⟩, or.elim hpq (assume hpx, or.inl (exists.intro x hpx)) (assume hqx, or.inr (exists.intro x hqx))) (assume hepq, or.elim hepq (assume hepx, match hepx : _ → ∃ x, p x ∨ q x with ⟨x, hpx⟩ := ⟨x, or.inl hpx⟩ end) (assume ⟨x, hqx⟩, ⟨x, or.inr hqx⟩)) @[simp] theorem exists_and_iff_and_exists {q : Prop} {p : A → Prop} : (∃x, q ∧ p x) ↔ q ∧ (∃x, p x) := ⟨take ⟨x, hq, hp⟩, ⟨hq, x, hp⟩, take ⟨hq, x, hp⟩, ⟨x, hq, hp⟩⟩ end quantifiers /- classical versions -/ namespace classical universe variable u variables {A : Type u} {p : A → Prop} local attribute [instance] prop_decidable theorem exists_not_of_not_forall (h : ¬ ∀ x, p x) : ∃ x, ¬ p x := decidable.exists_not_of_not_forall h theorem not_forall_iff_exists_not (p : A → Prop) : (¬ ∀ x, p x) ↔ (∃ x, ¬ p x) := decidable.not_forall_iff_exists_not p theorem forall_or_iff_or_forall {q : Prop} {p : A → Prop} : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := ⟨take h, if hq : q then or.inl hq else or.inr $ take x, or.resolve_left (h x) hq, take h x, or.imp_right (suppose ∀x, p x, this x) h⟩ end classical /- bounded quantifiers -/ section bounded_quantifiers universe variable u variables {A : Type u} {r p q : A → Prop} {b : Prop} theorem bforall_congr (h : ∀ x (hrx : r x), p x ↔ q x) : (∀ x (hrx : r x), p x) ↔ (∀ x (hrx : r x), q x) := begin apply forall_congr, intro x, apply forall_congr, apply h end theorem bexists_congr (h : ∀ x (hrx : r x), p x ↔ q x) : (∃ x (hrx : r x), p x) ↔ (∃ x (hrx : r x), q x) := begin apply exists_congr, intros, apply exists_congr, apply h end theorem bforall_of_bforall (h : ∀ x (hrx : r x), (p x → q x)) (h₁ : ∀ x (hrx : r x), p x) : ∀ x (hrx : r x) , q x := take x, assume hrx, h x hrx (h₁ x hrx) theorem bexists_of_bexists {A : Type} {p q : A → Prop} (h : ∀ x, (p x → q x)) (h₁ : ∃ x, p x) : ∃ x, q x := match h₁ with ⟨x, hpx⟩ := ⟨x, h x hpx⟩ end theorem bforall_of_forall (h : ∀ x, p x) : ∀ x (hrx : r x), p x := λ x hrx, h x theorem forall_of_bforall (h : ∀ x (ht : true), p x) : ∀ x, p x := λ x, h x trivial theorem bexists_of_exists (h : ∃ x, p x) : ∃ x (ht : true), p x := match h with ⟨x, hpx⟩ := ⟨x, trivial, hpx⟩ end theorem exists_of_bexists (h : ∃ x (hrx : r x), p x) : ∃ x, p x := match h with ⟨x, hrx, hpx⟩ := ⟨x, hpx⟩ end theorem bforall_implies_of_bexists_implies (h : (∃ x (hrx : r x), p x) → b) : ∀ x (hrx : r x), p x → b := λ x hrx hpx, h ⟨x, hrx, hpx⟩ theorem bexists_implies_of_bforall_implies (h : ∀ x (hrx : r x), p x → b) : (∃ x (hrx : r x), p x) → b := assume ⟨x, hrx, hpx⟩, h x hrx hpx theorem bexists_implies_distrib (r p : A → Prop) (b : Prop) : ((∃ x (hrx : r x), p x) → b) ↔ (∀ x (hrx : r x), p x → b) := iff.intro bforall_implies_of_bexists_implies bexists_implies_of_bforall_implies theorem bforall_not_of_not_bexists (h : ¬ ∃ x (hrx : r x), p x) : ∀ x (hrx : r x), ¬ p x := bforall_implies_of_bexists_implies h theorem not_bexists_of_bforall_not (h : ∀ x (hrx : r x), ¬ p x) : ¬ ∃ x (hrx : r x), p x := bexists_implies_of_bforall_implies h theorem not_bexists_iff_bforall_not (r p : A → Prop) : (¬ ∃ x (hrx : r x) , p x) ↔ (∀ x (h : r x), ¬ p x) := bexists_implies_distrib r p false theorem decidable.bexists_not_of_not_bforall [decidable (∃ x (hrx : r x), ¬ p x)] [∀ x, decidable (p x)] (h : ¬ ∀ x (hrx : r x), p x) : ∃ x (hr : r x), ¬ p x := decidable.by_contradiction (assume h₁, h (take x, assume hrx, decidable.by_contradiction (assume hnpx, h₁ ⟨x, hrx, hnpx⟩))) theorem not_bforall_of_bexists_not (h : ∃ x (hrx : r x), ¬ p x) : ¬ ∀ x (hrx : r x), p x := assume h₁, match h with ⟨x, hrx, hnpx⟩ := hnpx (h₁ x hrx) end theorem decidable.not_bforall_iff_bexists_not (r p : A → Prop) [decidable (∃ x (hrx : r x), ¬ p x)] [∀ x, decidable (p x)] : (¬ ∀ x (hrx : r x), p x) ↔ (∃ x (hrx : r x), ¬ p x) := iff.intro decidable.bexists_not_of_not_bforall not_bforall_of_bexists_not theorem bforall_true_iff (r : A → Prop): (∀ x (hrx : r x), true) ↔ true := iff_true_intro (λ h hrx, trivial) theorem bforall_and_distrib : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) := iff.intro (assume h, ⟨(take x, (h x)^.left), (take x, (h x)^.right)⟩) (assume h x, ⟨h^.left x, h^.right x⟩) theorem bexists_or_distrib (r p q : A → Prop) : (∃ x (hrx : r x), p x ∨ q x) ↔ (∃ x (hrx : r x), p x) ∨ (∃ x (hrx : r x), q x) := iff.intro (assume ⟨x, hrx, hpq⟩, or.elim hpq (assume hpx, or.inl (exists.intro x (exists.intro hrx hpx))) (assume hqx, or.inr (exists.intro x (exists.intro hrx hqx)))) (assume hepq, or.elim hepq (assume hepx, match hepx : _ → ∃ x (hrx : r x), p x ∨ q x with ⟨x, hrx, hpx⟩ := ⟨x, hrx, or.inl hpx⟩ end) (assume ⟨x, hrx, hqx⟩, ⟨x, hrx, or.inr hqx⟩)) end bounded_quantifiers -- TODO(Jeremy): merge with previous section section universe variable uu variables {α : Type uu} {p q : α → Prop} @[simp] theorem exists_prop_iff (p q : Prop) : (∃ h : p, q) ↔ p ∧ q := iff.intro begin intro h', cases h', split, repeat { assumption } end begin intro h', cases h', split, repeat { assumption } end theorem bexists.elim {b : Prop} (h : ∃ x : α, ∃ h : p x, q x) (h' : ∀ (a : α), p a → q a → b) : b := exists.elim h (λ a h₁, exists.elim h₁ (h' a)) theorem bexists.intro (a : α) (h₁ : p a) (h₂ : q a) : ∃ x, ∃ h : p x, q x := exists.intro a (exists.intro h₁ h₂) end namespace classical universe variable u variables {A : Type u} {r p : A → Prop} local attribute [instance] prop_decidable theorem bexists_not_of_not_bforall (h : ¬ ∀ x (hrx : r x), p x) : ∃ x (hr : r x), ¬ p x := decidable.bexists_not_of_not_bforall h theorem not_bforall_iff_bexists_not (r p : A → Prop) : (¬ ∀ x (hrx : r x), p x) ↔ (∃ x (hrx : r x), ¬ p x) := decidable.not_bforall_iff_bexists_not r p end classical
93e5f183074c62ff835ef057bb497d08cc46898e
fecda8e6b848337561d6467a1e30cf23176d6ad0
/src/measure_theory/bochner_integration.lean
0bb1df5a981df6810784f664cc808fa905a5c012
[ "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
62,875
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import measure_theory.simple_func_dense import analysis.normed_space.bounded_linear_maps import topology.sequences /-! # Bochner integral The Bochner integral extends the definition of the Lebesgue integral to functions that map from a measure space into a Banach space (complete normed vector space). It is constructed here by extending the integral on simple functions. ## Main definitions The Bochner integral is defined following these steps: 1. Define the integral on simple functions of the type `simple_func α E` (notation : `α →ₛ E`) where `E` is a real normed space. (See `simple_func.bintegral` and section `bintegral` for details. Also see `simple_func.integral` for the integral on simple functions of the type `simple_func α ennreal`.) 2. Use `α →ₛ E` to cut out the simple functions from L1 functions, and define integral on these. The type of simple functions in L1 space is written as `α →₁ₛ[μ] E`. 3. Show that the embedding of `α →₁ₛ[μ] E` into L1 is a dense and uniform one. 4. Show that the integral defined on `α →₁ₛ[μ] E` is a continuous linear map. 5. Define the Bochner integral on L1 functions by extending the integral on integrable simple functions `α →₁ₛ[μ] E` using `continuous_linear_map.extend`. Define the Bochner integral on functions as the Bochner integral of its equivalence class in L1 space. ## Main statements 1. Basic properties of the Bochner integral on functions of type `α → E`, where `α` is a measure space and `E` is a real normed space. * `integral_zero` : `∫ 0 = 0` * `integral_add` : `∫ f + g = ∫ f + ∫ g` * `integral_neg` : `∫ -f = - ∫ f` * `integral_sub` : `∫ f - g = ∫ f - ∫ g` * `integral_smul` : `∫ r • f = r • ∫ f` * `integral_congr_ae` : `∀ᵐ a, f a = g a → ∫ f = ∫ g` * `norm_integral_le_integral_norm` : `∥∫ f∥ ≤ ∫ ∥f∥` 2. Basic properties of the Bochner integral on functions of type `α → ℝ`, where `α` is a measure space. * `integral_nonneg_of_ae` : `∀ᵐ a, 0 ≤ f a → 0 ≤ ∫ f` * `integral_nonpos_of_nonpos_ae` : `∀ᵐ a, f a ≤ 0 → ∫ f ≤ 0` * `integral_le_integral_of_le_ae` : `∀ᵐ a, f a ≤ g a → ∫ f ≤ ∫ g` 3. Propositions connecting the Bochner integral with the integral on `ennreal`-valued functions, which is called `lintegral` and has the notation `∫⁻`. * `integral_eq_lintegral_max_sub_lintegral_min` : `∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, where `f⁺` is the positive part of `f` and `f⁻` is the negative part of `f`. * `integral_eq_lintegral_of_nonneg_ae` : `∀ᵐ a, 0 ≤ f a → ∫ f = ∫⁻ f` 4. `tendsto_integral_of_dominated_convergence` : the Lebesgue dominated convergence theorem ## Notes Some tips on how to prove a proposition if the API for the Bochner integral is not enough so that you need to unfold the definition of the Bochner integral and go back to simple functions. One method is to use the theorem `integrable.induction` in the file `set_integral`, which allows you to prove something for an arbitrary measurable + integrable function. Another method is using the following steps. See `integral_eq_lintegral_max_sub_lintegral_min` for a complicated example, which proves that `∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, with the first integral sign being the Bochner integral of a real-valued function `f : α → ℝ`, and second and third integral sign being the integral on ennreal-valued functions (called `lintegral`). The proof of `integral_eq_lintegral_max_sub_lintegral_min` is scattered in sections with the name `pos_part`. Here are the usual steps of proving that a property `p`, say `∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, holds for all functions : 1. First go to the `L¹` space. For example, if you see `ennreal.to_real (∫⁻ a, ennreal.of_real $ ∥f a∥)`, that is the norm of `f` in `L¹` space. Rewrite using `l1.norm_of_fun_eq_lintegral_norm`. 2. Show that the set `{f ∈ L¹ | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻}` is closed in `L¹` using `is_closed_eq`. 3. Show that the property holds for all simple functions `s` in `L¹` space. Typically, you need to convert various notions to their `simple_func` counterpart, using lemmas like `l1.integral_coe_eq_integral`. 4. Since simple functions are dense in `L¹`, ``` univ = closure {s simple} = closure {s simple | ∫ s = ∫⁻ s⁺ - ∫⁻ s⁻} : the property holds for all simple functions ⊆ closure {f | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻} = {f | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻} : closure of a closed set is itself ``` Use `is_closed_property` or `dense_range.induction_on` for this argument. ## Notations * `α →ₛ E` : simple functions (defined in `measure_theory/integration`) * `α →₁[μ] E` : functions in L1 space, i.e., equivalence classes of integrable functions (defined in `measure_theory/l1_space`) * `α →₁ₛ[μ] E` : simple functions in L1 space, i.e., equivalence classes of integrable simple functions Note : `ₛ` is typed using `\_s`. Sometimes it shows as a box if font is missing. ## Tags Bochner integral, simple function, function space, Lebesgue dominated convergence theorem -/ noncomputable theory open_locale classical topological_space big_operators namespace measure_theory variables {α E : Type*} [measurable_space α] [decidable_linear_order E] [has_zero E] local infixr ` →ₛ `:25 := simple_func namespace simple_func section pos_part /-- Positive part of a simple function. -/ def pos_part (f : α →ₛ E) : α →ₛ E := f.map (λb, max b 0) /-- Negative part of a simple function. -/ def neg_part [has_neg E] (f : α →ₛ E) : α →ₛ E := pos_part (-f) lemma pos_part_map_norm (f : α →ₛ ℝ) : (pos_part f).map norm = pos_part f := begin ext, rw [map_apply, real.norm_eq_abs, abs_of_nonneg], rw [pos_part, map_apply], exact le_max_right _ _ end lemma neg_part_map_norm (f : α →ₛ ℝ) : (neg_part f).map norm = neg_part f := by { rw neg_part, exact pos_part_map_norm _ } lemma pos_part_sub_neg_part (f : α →ₛ ℝ) : f.pos_part - f.neg_part = f := begin simp only [pos_part, neg_part], ext, exact max_zero_sub_eq_self (f a) end end pos_part end simple_func end measure_theory namespace measure_theory open set filter topological_space ennreal emetric variables {α E F : Type*} [measurable_space α] local infixr ` →ₛ `:25 := simple_func namespace simple_func section integral /-! ### The Bochner integral of simple functions Define the Bochner integral of simple functions of the type `α →ₛ β` where `β` is a normed group, and prove basic property of this integral. -/ open finset variables [normed_group E] [normed_group F] {μ : measure α} /-- For simple functions with a `normed_group` as codomain, being integrable is the same as having finite volume support. -/ lemma integrable_iff_fin_meas_supp {f : α →ₛ E} {μ : measure α} : integrable f μ ↔ f.fin_meas_supp μ := calc integrable f μ ↔ ∫⁻ x, f.map (coe ∘ nnnorm : E → ennreal) x ∂μ < ⊤ : iff.rfl ... ↔ (f.map (coe ∘ nnnorm : E → ennreal)).lintegral μ < ⊤ : by rw lintegral_eq_lintegral ... ↔ (f.map (coe ∘ nnnorm : E → ennreal)).fin_meas_supp μ : iff.symm $ fin_meas_supp.iff_lintegral_lt_top $ eventually_of_forall $ λ x, coe_lt_top ... ↔ _ : fin_meas_supp.map_iff $ λ b, coe_eq_zero.trans nnnorm_eq_zero lemma fin_meas_supp.integrable {f : α →ₛ E} (h : f.fin_meas_supp μ) : integrable f μ := integrable_iff_fin_meas_supp.2 h lemma integrable_pair {f : α →ₛ E} {g : α →ₛ F} : integrable f μ → integrable g μ → integrable (pair f g) μ := by simpa only [integrable_iff_fin_meas_supp] using fin_meas_supp.pair variables [normed_space ℝ F] /-- Bochner integral of simple functions whose codomain is a real `normed_space`. -/ def integral (μ : measure α) (f : α →ₛ F) : F := ∑ x in f.range, (ennreal.to_real (μ (f ⁻¹' {x}))) • x lemma integral_eq_sum_filter (f : α →ₛ F) (μ) : f.integral μ = ∑ x in f.range.filter (λ x, x ≠ 0), (ennreal.to_real (μ (f ⁻¹' {x}))) • x := eq.symm $ sum_filter_of_ne $ λ x _, mt $ λ h0, h0.symm ▸ smul_zero _ /-- Calculate the integral of `g ∘ f : α →ₛ F`, where `f` is an integrable function from `α` to `E` and `g` is a function from `E` to `F`. We require `g 0 = 0` so that `g ∘ f` is integrable. -/ lemma map_integral (f : α →ₛ E) (g : E → F) (hf : integrable f μ) (hg : g 0 = 0) : (f.map g).integral μ = ∑ x in f.range, (ennreal.to_real (μ (f ⁻¹' {x}))) • (g x) := begin -- We start as in the proof of `map_lintegral` simp only [integral, range_map], refine finset.sum_image' _ (assume b hb, _), rcases mem_range.1 hb with ⟨a, rfl⟩, rw [map_preimage_singleton, ← sum_measure_preimage_singleton _ (λ _ _, f.is_measurable_preimage _)], -- Now we use `hf : integrable f μ` to show that `ennreal.to_real` is additive. by_cases ha : g (f a) = 0, { simp only [ha, smul_zero], refine (sum_eq_zero $ λ x hx, _).symm, simp only [mem_filter] at hx, simp [hx.2] }, { rw [to_real_sum, sum_smul], { refine sum_congr rfl (λ x hx, _), simp only [mem_filter] at hx, rw [hx.2] }, { intros x hx, simp only [mem_filter] at hx, refine (integrable_iff_fin_meas_supp.1 hf).meas_preimage_singleton_ne_zero _, exact λ h0, ha (hx.2 ▸ h0.symm ▸ hg) } }, end /-- `simple_func.integral` and `simple_func.lintegral` agree when the integrand has type `α →ₛ ennreal`. But since `ennreal` is not a `normed_space`, we need some form of coercion. See `integral_eq_lintegral` for a simpler version. -/ lemma integral_eq_lintegral' {f : α →ₛ E} {g : E → ennreal} (hf : integrable f μ) (hg0 : g 0 = 0) (hgt : ∀b, g b < ⊤): (f.map (ennreal.to_real ∘ g)).integral μ = ennreal.to_real (∫⁻ a, g (f a) ∂μ) := begin have hf' : f.fin_meas_supp μ := integrable_iff_fin_meas_supp.1 hf, simp only [← map_apply g f, lintegral_eq_lintegral], rw [map_integral f _ hf, map_lintegral, ennreal.to_real_sum], { refine finset.sum_congr rfl (λb hb, _), rw [smul_eq_mul, to_real_mul_to_real, mul_comm] }, { assume a ha, by_cases a0 : a = 0, { rw [a0, hg0, zero_mul], exact with_top.zero_lt_top }, { apply mul_lt_top (hgt a) (hf'.meas_preimage_singleton_ne_zero a0) } }, { simp [hg0] } end variables [normed_space ℝ E] lemma integral_congr {f g : α →ₛ E} (hf : integrable f μ) (h : f =ᵐ[μ] g): f.integral μ = g.integral μ := show ((pair f g).map prod.fst).integral μ = ((pair f g).map prod.snd).integral μ, from begin have inte := integrable_pair hf (hf.congr h), rw [map_integral (pair f g) _ inte prod.fst_zero, map_integral (pair f g) _ inte prod.snd_zero], refine finset.sum_congr rfl (assume p hp, _), rcases mem_range.1 hp with ⟨a, rfl⟩, by_cases eq : f a = g a, { dsimp only [pair_apply], rw eq }, { have : μ ((pair f g) ⁻¹' {(f a, g a)}) = 0, { refine measure_mono_null (assume a' ha', _) h, simp only [set.mem_preimage, mem_singleton_iff, pair_apply, prod.mk.inj_iff] at ha', show f a' ≠ g a', rwa [ha'.1, ha'.2] }, simp only [this, pair_apply, zero_smul, ennreal.zero_to_real] }, end /-- `simple_func.bintegral` and `simple_func.integral` agree when the integrand has type `α →ₛ ennreal`. But since `ennreal` is not a `normed_space`, we need some form of coercion. -/ lemma integral_eq_lintegral {f : α →ₛ ℝ} (hf : integrable f μ) (h_pos : 0 ≤ᵐ[μ] f) : f.integral μ = ennreal.to_real (∫⁻ a, ennreal.of_real (f a) ∂μ) := begin have : f =ᵐ[μ] f.map (ennreal.to_real ∘ ennreal.of_real) := h_pos.mono (λ a h, (ennreal.to_real_of_real h).symm), rw [← integral_eq_lintegral' hf], { exact integral_congr hf this }, { exact ennreal.of_real_zero }, { assume b, rw ennreal.lt_top_iff_ne_top, exact ennreal.of_real_ne_top } end lemma integral_add {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) : integral μ (f + g) = integral μ f + integral μ g := calc integral μ (f + g) = ∑ x in (pair f g).range, ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • (x.fst + x.snd) : begin rw [add_eq_map₂, map_integral (pair f g)], { exact integrable_pair hf hg }, { simp only [add_zero, prod.fst_zero, prod.snd_zero] } end ... = ∑ x in (pair f g).range, (ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • x.fst + ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • x.snd) : finset.sum_congr rfl $ assume a ha, smul_add _ _ _ ... = ∑ x in (pair f g).range, ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • x.fst + ∑ x in (pair f g).range, ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • x.snd : by rw finset.sum_add_distrib ... = ((pair f g).map prod.fst).integral μ + ((pair f g).map prod.snd).integral μ : begin rw [map_integral (pair f g), map_integral (pair f g)], { exact integrable_pair hf hg }, { refl }, { exact integrable_pair hf hg }, { refl } end ... = integral μ f + integral μ g : rfl lemma integral_neg {f : α →ₛ E} (hf : integrable f μ) : integral μ (-f) = - integral μ f := calc integral μ (-f) = integral μ (f.map (has_neg.neg)) : rfl ... = - integral μ f : begin rw [map_integral f _ hf neg_zero, integral, ← sum_neg_distrib], refine finset.sum_congr rfl (λx h, smul_neg _ _), end lemma integral_sub {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) : integral μ (f - g) = integral μ f - integral μ g := begin rw [sub_eq_add_neg, integral_add hf, integral_neg hg, sub_eq_add_neg], exact hg.neg end lemma integral_smul (r : ℝ) {f : α →ₛ E} (hf : integrable f μ) : integral μ (r • f) = r • integral μ f := calc integral μ (r • f) = ∑ x in f.range, ennreal.to_real (μ (f ⁻¹' {x})) • r • x : by rw [smul_eq_map r f, map_integral f _ hf (smul_zero _)] ... = ∑ x in f.range, ((ennreal.to_real (μ (f ⁻¹' {x}))) * r) • x : finset.sum_congr rfl $ λb hb, by apply smul_smul ... = r • integral μ f : by simp only [integral, smul_sum, smul_smul, mul_comm] lemma norm_integral_le_integral_norm (f : α →ₛ E) (hf : integrable f μ) : ∥f.integral μ∥ ≤ (f.map norm).integral μ := begin rw [map_integral f norm hf norm_zero, integral], calc ∥∑ x in f.range, ennreal.to_real (μ (f ⁻¹' {x})) • x∥ ≤ ∑ x in f.range, ∥ennreal.to_real (μ (f ⁻¹' {x})) • x∥ : norm_sum_le _ _ ... = ∑ x in f.range, ennreal.to_real (μ (f ⁻¹' {x})) • ∥x∥ : begin refine finset.sum_congr rfl (λb hb, _), rw [norm_smul, smul_eq_mul, real.norm_eq_abs, abs_of_nonneg to_real_nonneg] end end lemma integral_add_measure {ν} (f : α →ₛ E) (hf : integrable f (μ + ν)) : f.integral (μ + ν) = f.integral μ + f.integral ν := begin simp only [integral_eq_sum_filter, ← sum_add_distrib, ← add_smul, measure.add_apply], refine sum_congr rfl (λ x hx, _), rw [to_real_add]; refine ne_of_lt ((integrable_iff_fin_meas_supp.1 _).meas_preimage_singleton_ne_zero (mem_filter.1 hx).2), exacts [hf.left_of_add_measure, hf.right_of_add_measure] end variables [second_countable_topology E] [measurable_space E] [borel_space E] end integral end simple_func namespace l1 open ae_eq_fun variables [normed_group E] [second_countable_topology E] [measurable_space E] [borel_space E] [normed_group F] [second_countable_topology F] [measurable_space F] [borel_space F] {μ : measure α} variables (α E μ) -- We use `Type*` instead of `add_subgroup` because otherwise we loose dot notation. /-- `l1.simple_func` is a subspace of L1 consisting of equivalence classes of an integrable simple function. -/ def simple_func : Type* := ↥({ carrier := {f : α →₁[μ] E | ∃ (s : α →ₛ E), (ae_eq_fun.mk s s.measurable : α →ₘ[μ] E) = f}, zero_mem' := ⟨0, rfl⟩, add_mem' := λ f g ⟨s, hs⟩ ⟨t, ht⟩, ⟨s + t, by simp only [coe_add, ← hs, ← ht, mk_add_mk, ← simple_func.coe_add]⟩, neg_mem' := λ f ⟨s, hs⟩, ⟨-s, by simp only [coe_neg, ← hs, neg_mk, ← simple_func.coe_neg]⟩ } : add_subgroup (α →₁[μ] E)) variables {α E μ} notation α ` →₁ₛ[`:25 μ `] ` E := measure_theory.l1.simple_func α E μ namespace simple_func section instances /-! Simple functions in L1 space form a `normed_space`. -/ instance : has_coe (α →₁ₛ[μ] E) (α →₁[μ] E) := coe_subtype instance : has_coe_to_fun (α →₁ₛ[μ] E) := ⟨λ f, α → E, λ f, ⇑(f : α →₁[μ] E)⟩ @[simp, norm_cast] lemma coe_coe (f : α →₁ₛ[μ] E) : ⇑(f : α →₁[μ] E) = f := rfl protected lemma eq {f g : α →₁ₛ[μ] E} : (f : α →₁[μ] E) = (g : α →₁[μ] E) → f = g := subtype.eq protected lemma eq' {f g : α →₁ₛ[μ] E} : (f : α →ₘ[μ] E) = (g : α →ₘ[μ] E) → f = g := subtype.eq ∘ subtype.eq @[norm_cast] protected lemma eq_iff {f g : α →₁ₛ[μ] E} : (f : α →₁[μ] E) = g ↔ f = g := subtype.ext_iff.symm @[norm_cast] protected lemma eq_iff' {f g : α →₁ₛ[μ] E} : (f : α →ₘ[μ] E) = g ↔ f = g := iff.intro (simple_func.eq') (congr_arg _) /-- L1 simple functions forms a `emetric_space`, with the emetric being inherited from L1 space, i.e., `edist f g = ∫⁻ a, edist (f a) (g a)`. Not declared as an instance as `α →₁ₛ[μ] β` will only be useful in the construction of the bochner integral. -/ protected def emetric_space : emetric_space (α →₁ₛ[μ] E) := subtype.emetric_space /-- L1 simple functions forms a `metric_space`, with the metric being inherited from L1 space, i.e., `dist f g = ennreal.to_real (∫⁻ a, edist (f a) (g a)`). Not declared as an instance as `α →₁ₛ[μ] β` will only be useful in the construction of the bochner integral. -/ protected def metric_space : metric_space (α →₁ₛ[μ] E) := subtype.metric_space local attribute [instance] simple_func.metric_space simple_func.emetric_space /-- Functions `α →₁ₛ[μ] E` form an additive commutative group. -/ local attribute [instance, priority 10000] protected def add_comm_group : add_comm_group (α →₁ₛ[μ] E) := add_subgroup.to_add_comm_group _ instance : inhabited (α →₁ₛ[μ] E) := ⟨0⟩ @[simp, norm_cast] lemma coe_zero : ((0 : α →₁ₛ[μ] E) : α →₁[μ] E) = 0 := rfl @[simp, norm_cast] lemma coe_add (f g : α →₁ₛ[μ] E) : ((f + g : α →₁ₛ[μ] E) : α →₁[μ] E) = f + g := rfl @[simp, norm_cast] lemma coe_neg (f : α →₁ₛ[μ] E) : ((-f : α →₁ₛ[μ] E) : α →₁[μ] E) = -f := rfl @[simp, norm_cast] lemma coe_sub (f g : α →₁ₛ[μ] E) : ((f - g : α →₁ₛ[μ] E) : α →₁[μ] E) = f - g := rfl @[simp] lemma edist_eq (f g : α →₁ₛ[μ] E) : edist f g = edist (f : α →₁[μ] E) (g : α →₁[μ] E) := rfl @[simp] lemma dist_eq (f g : α →₁ₛ[μ] E) : dist f g = dist (f : α →₁[μ] E) (g : α →₁[μ] E) := rfl /-- The norm on `α →₁ₛ[μ] E` is inherited from L1 space. That is, `∥f∥ = ∫⁻ a, edist (f a) 0`. Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the bochner integral. -/ protected def has_norm : has_norm (α →₁ₛ[μ] E) := ⟨λf, ∥(f : α →₁[μ] E)∥⟩ local attribute [instance] simple_func.has_norm lemma norm_eq (f : α →₁ₛ[μ] E) : ∥f∥ = ∥(f : α →₁[μ] E)∥ := rfl lemma norm_eq' (f : α →₁ₛ[μ] E) : ∥f∥ = ennreal.to_real (edist (f : α →ₘ[μ] E) 0) := rfl /-- Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the bochner integral. -/ protected def normed_group : normed_group (α →₁ₛ[μ] E) := normed_group.of_add_dist (λ x, rfl) $ by { intros, simp only [dist_eq, coe_add, l1.dist_eq, l1.coe_add], rw edist_add_right } variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E] /-- Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the bochner integral. -/ protected def has_scalar : has_scalar 𝕜 (α →₁ₛ[μ] E) := ⟨λk f, ⟨k • f, begin rcases f with ⟨f, ⟨s, hs⟩⟩, use k • s, rw [coe_smul, subtype.coe_mk, ← hs], refl end ⟩⟩ local attribute [instance, priority 10000] simple_func.has_scalar @[simp, norm_cast] lemma coe_smul (c : 𝕜) (f : α →₁ₛ[μ] E) : ((c • f : α →₁ₛ[μ] E) : α →₁[μ] E) = c • (f : α →₁[μ] E) := rfl /-- Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the bochner integral. -/ protected def semimodule : semimodule 𝕜 (α →₁ₛ[μ] E) := { one_smul := λf, simple_func.eq (by { simp only [coe_smul], exact one_smul _ _ }), mul_smul := λx y f, simple_func.eq (by { simp only [coe_smul], exact mul_smul _ _ _ }), smul_add := λx f g, simple_func.eq (by { simp only [coe_smul, coe_add], exact smul_add _ _ _ }), smul_zero := λx, simple_func.eq (by { simp only [coe_zero, coe_smul], exact smul_zero _ }), add_smul := λx y f, simple_func.eq (by { simp only [coe_smul], exact add_smul _ _ _ }), zero_smul := λf, simple_func.eq (by { simp only [coe_smul], exact zero_smul _ _ }) } local attribute [instance] simple_func.normed_group simple_func.semimodule /-- Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the bochner integral. -/ protected def normed_space : normed_space 𝕜 (α →₁ₛ[μ] E) := ⟨ λc f, by { rw [norm_eq, norm_eq, coe_smul, norm_smul] } ⟩ end instances local attribute [instance] simple_func.normed_group simple_func.normed_space section of_simple_func /-- Construct the equivalence class `[f]` of an integrable simple function `f`. -/ @[reducible] def of_simple_func (f : α →ₛ E) (hf : integrable f μ) : (α →₁ₛ[μ] E) := ⟨l1.of_fun f f.measurable hf, ⟨f, rfl⟩⟩ lemma of_simple_func_eq_of_fun (f : α →ₛ E) (hf : integrable f μ) : (of_simple_func f hf : α →₁[μ] E) = l1.of_fun f f.measurable hf := rfl lemma of_simple_func_eq_mk (f : α →ₛ E) (hf : integrable f μ) : (of_simple_func f hf : α →ₘ[μ] E) = ae_eq_fun.mk f f.measurable := rfl lemma of_simple_func_zero : of_simple_func (0 : α →ₛ E) (integrable_zero α μ E) = 0 := rfl lemma of_simple_func_add (f g : α →ₛ E) (hf hg) : (of_simple_func (f + g) (integrable.add f.measurable hf g.measurable hg) : α →₁ₛ[μ] E) = of_simple_func f hf + of_simple_func g hg := rfl lemma of_simple_func_neg (f : α →ₛ E) (hf : integrable f μ) : of_simple_func (-f) hf.neg = -of_simple_func f hf := rfl lemma of_simple_func_sub (f g : α →ₛ E) (hf : integrable f μ) (hg) : of_simple_func (f - g) (hf.sub f.measurable g.measurable hg) = of_simple_func f hf - of_simple_func g hg := rfl variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E] lemma of_simple_func_smul (f : α →ₛ E) (hf : integrable f μ) (c : 𝕜) : of_simple_func (c • f) (hf.smul c) = c • of_simple_func f hf := rfl lemma norm_of_simple_func (f : α →ₛ E) (hf : integrable f μ) : ∥of_simple_func f hf∥ = ennreal.to_real (∫⁻ a, edist (f a) 0 ∂μ) := rfl end of_simple_func section to_simple_func /-- Find a representative of a `l1.simple_func`. -/ def to_simple_func (f : α →₁ₛ[μ] E) : α →ₛ E := classical.some f.2 /-- `f.to_simple_func` is measurable. -/ protected lemma measurable (f : α →₁ₛ[μ] E) : measurable f.to_simple_func := f.to_simple_func.measurable /-- `f.to_simple_func` is integrable. -/ protected lemma integrable (f : α →₁ₛ[μ] E) : integrable f.to_simple_func μ := let h := classical.some_spec f.2 in (integrable_mk f.measurable).1 $ h.symm ▸ (f : α →₁[μ] E).2 lemma of_simple_func_to_simple_func (f : α →₁ₛ[μ] E) : of_simple_func (f.to_simple_func) f.integrable = f := by { rw ← simple_func.eq_iff', exact classical.some_spec f.2 } lemma to_simple_func_of_simple_func (f : α →ₛ E) (hfi : integrable f μ) : (of_simple_func f hfi).to_simple_func =ᵐ[μ] f := by { rw ← mk_eq_mk, exact classical.some_spec (of_simple_func f hfi).2 } lemma to_simple_func_eq_to_fun (f : α →₁ₛ[μ] E) : f.to_simple_func =ᵐ[μ] f := begin rw [← of_fun_eq_of_fun f.to_simple_func f f.measurable f.integrable (f : α →₁[μ] E).measurable (f : α →₁[μ] E).integrable, ← l1.eq_iff], simp only [of_fun_eq_mk, ← coe_coe, mk_to_fun], exact classical.some_spec f.coe_prop end variables (α E) lemma zero_to_simple_func : (0 : α →₁ₛ[μ] E).to_simple_func =ᵐ[μ] 0 := begin filter_upwards [to_simple_func_eq_to_fun (0 : α →₁ₛ[μ] E), l1.zero_to_fun α E], simp only [mem_set_of_eq], assume a h, rw h, exact id end variables {α E} lemma add_to_simple_func (f g : α →₁ₛ[μ] E) : (f + g).to_simple_func =ᵐ[μ] f.to_simple_func + g.to_simple_func := begin filter_upwards [to_simple_func_eq_to_fun (f + g), to_simple_func_eq_to_fun f, to_simple_func_eq_to_fun g, l1.add_to_fun (f : α →₁[μ] E) g], assume a, simp only [mem_set_of_eq, ← coe_coe, coe_add, pi.add_apply], iterate 4 { assume h, rw h } end lemma neg_to_simple_func (f : α →₁ₛ[μ] E) : (-f).to_simple_func =ᵐ[μ] - f.to_simple_func := begin filter_upwards [to_simple_func_eq_to_fun (-f), to_simple_func_eq_to_fun f, l1.neg_to_fun (f : α →₁[μ] E)], assume a, simp only [mem_set_of_eq, pi.neg_apply, coe_neg, ← coe_coe], repeat { assume h, rw h } end lemma sub_to_simple_func (f g : α →₁ₛ[μ] E) : (f - g).to_simple_func =ᵐ[μ] f.to_simple_func - g.to_simple_func := begin filter_upwards [to_simple_func_eq_to_fun (f - g), to_simple_func_eq_to_fun f, to_simple_func_eq_to_fun g, l1.sub_to_fun (f : α →₁[μ] E) g], assume a, simp only [mem_set_of_eq, coe_sub, pi.sub_apply, ← coe_coe], repeat { assume h, rw h } end variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E] lemma smul_to_simple_func (k : 𝕜) (f : α →₁ₛ[μ] E) : (k • f).to_simple_func =ᵐ[μ] k • f.to_simple_func := begin filter_upwards [to_simple_func_eq_to_fun (k • f), to_simple_func_eq_to_fun f, l1.smul_to_fun k (f : α →₁[μ] E)], assume a, simp only [mem_set_of_eq, pi.smul_apply, coe_smul, ← coe_coe], repeat { assume h, rw h } end lemma lintegral_edist_to_simple_func_lt_top (f g : α →₁ₛ[μ] E) : ∫⁻ (x : α), edist ((to_simple_func f) x) ((to_simple_func g) x) ∂μ < ⊤ := begin rw lintegral_rw₂ (to_simple_func_eq_to_fun f) (to_simple_func_eq_to_fun g), exact lintegral_edist_to_fun_lt_top _ _ end lemma dist_to_simple_func (f g : α →₁ₛ[μ] E) : dist f g = ennreal.to_real (∫⁻ x, edist (f.to_simple_func x) (g.to_simple_func x) ∂μ) := begin rw [dist_eq, l1.dist_to_fun, ennreal.to_real_eq_to_real], { rw lintegral_rw₂, repeat { exact ae_eq_symm (to_simple_func_eq_to_fun _) } }, { exact l1.lintegral_edist_to_fun_lt_top _ _ }, { exact lintegral_edist_to_simple_func_lt_top _ _ } end lemma norm_to_simple_func (f : α →₁ₛ[μ] E) : ∥f∥ = ennreal.to_real (∫⁻ (a : α), nnnorm ((to_simple_func f) a) ∂μ) := calc ∥f∥ = ennreal.to_real (∫⁻x, edist (f.to_simple_func x) ((0 : α →₁ₛ[μ] E).to_simple_func x) ∂μ) : begin rw [← dist_zero_right, dist_to_simple_func] end ... = ennreal.to_real (∫⁻ (x : α), (coe ∘ nnnorm) (f.to_simple_func x) ∂μ) : begin rw lintegral_nnnorm_eq_lintegral_edist, have : ∫⁻ x, edist ((to_simple_func f) x) ((to_simple_func (0 : α →₁ₛ[μ] E)) x) ∂μ = ∫⁻ x, edist ((to_simple_func f) x) 0 ∂μ, { refine lintegral_congr_ae ((zero_to_simple_func α E).mono (λ a h, _)), rw [h, pi.zero_apply] }, rw [ennreal.to_real_eq_to_real], { exact this }, { exact lintegral_edist_to_simple_func_lt_top _ _ }, { rw ← this, exact lintegral_edist_to_simple_func_lt_top _ _ } end lemma norm_eq_integral (f : α →₁ₛ[μ] E) : ∥f∥ = (f.to_simple_func.map norm).integral μ := -- calc ∥f∥ = ennreal.to_real (∫⁻ (x : α), (coe ∘ nnnorm) (f.to_simple_func x) ∂μ) : -- by { rw norm_to_simple_func } -- ... = (f.to_simple_func.map norm).integral μ : begin rw [norm_to_simple_func, simple_func.integral_eq_lintegral], { simp only [simple_func.map_apply, of_real_norm_eq_coe_nnnorm] }, { exact f.integrable.norm }, { exact eventually_of_forall (λ x, norm_nonneg _) } end end to_simple_func section coe_to_l1 protected lemma uniform_continuous : uniform_continuous (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := uniform_continuous_comap protected lemma uniform_embedding : uniform_embedding (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := uniform_embedding_comap subtype.val_injective protected lemma uniform_inducing : uniform_inducing (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := simple_func.uniform_embedding.to_uniform_inducing protected lemma dense_embedding : dense_embedding (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := begin apply simple_func.uniform_embedding.dense_embedding, rintros ⟨⟨f, hfm⟩, hfi⟩, rw mem_closure_iff_seq_limit, rcases simple_func_sequence_tendsto' hfm ((integrable_mk hfm).1 hfi) with ⟨F, hF⟩, refine ⟨λ n, ↑(of_simple_func (F n) (hF.1 n)), λ n, mem_range_self _, _⟩, rw tendsto_iff_edist_tendsto_0, simpa [edist_mk_mk, ← edist_nndist] using hF.2 end protected lemma dense_inducing : dense_inducing (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := simple_func.dense_embedding.to_dense_inducing protected lemma dense_range : dense_range (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) := simple_func.dense_inducing.dense variables (𝕜 : Type*) [normed_field 𝕜] [normed_space 𝕜 E] variables (α E) /-- The uniform and dense embedding of L1 simple functions into L1 functions. -/ def coe_to_l1 : (α →₁ₛ[μ] E) →L[𝕜] (α →₁[μ] E) := { to_fun := (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)), map_add' := λf g, rfl, map_smul' := λk f, rfl, cont := l1.simple_func.uniform_continuous.continuous, } variables {α E 𝕜} end coe_to_l1 section pos_part /-- Positive part of a simple function in L1 space. -/ def pos_part (f : α →₁ₛ[μ] ℝ) : α →₁ₛ[μ] ℝ := ⟨l1.pos_part (f : α →₁[μ] ℝ), begin rcases f with ⟨f, s, hsf⟩, use s.pos_part, simp only [subtype.coe_mk, l1.coe_pos_part, ← hsf, ae_eq_fun.pos_part_mk, simple_func.pos_part, simple_func.coe_map] end ⟩ /-- Negative part of a simple function in L1 space. -/ def neg_part (f : α →₁ₛ[μ] ℝ) : α →₁ₛ[μ] ℝ := pos_part (-f) @[norm_cast] lemma coe_pos_part (f : α →₁ₛ[μ] ℝ) : (f.pos_part : α →₁[μ] ℝ) = (f : α →₁[μ] ℝ).pos_part := rfl @[norm_cast] lemma coe_neg_part (f : α →₁ₛ[μ] ℝ) : (f.neg_part : α →₁[μ] ℝ) = (f : α →₁[μ] ℝ).neg_part := rfl end pos_part section simple_func_integral /-! Define the Bochner integral on `α →₁ₛ[μ] E` and prove basic properties of this integral. -/ variables [normed_space ℝ E] /-- The Bochner integral over simple functions in l1 space. -/ def integral (f : α →₁ₛ[μ] E) : E := (f.to_simple_func).integral μ lemma integral_eq_integral (f : α →₁ₛ[μ] E) : integral f = (f.to_simple_func).integral μ := rfl lemma integral_eq_lintegral {f : α →₁ₛ[μ] ℝ} (h_pos : 0 ≤ᵐ[μ] f.to_simple_func) : integral f = ennreal.to_real (∫⁻ a, ennreal.of_real (f.to_simple_func a) ∂μ) := by rw [integral, simple_func.integral_eq_lintegral f.integrable h_pos] lemma integral_congr {f g : α →₁ₛ[μ] E} (h : f.to_simple_func =ᵐ[μ] g.to_simple_func) : integral f = integral g := simple_func.integral_congr f.integrable h lemma integral_add (f g : α →₁ₛ[μ] E) : integral (f + g) = integral f + integral g := begin simp only [integral], rw ← simple_func.integral_add f.integrable g.integrable, apply measure_theory.simple_func.integral_congr (f + g).integrable, apply add_to_simple_func end lemma integral_smul (r : ℝ) (f : α →₁ₛ[μ] E) : integral (r • f) = r • integral f := begin simp only [integral], rw ← simple_func.integral_smul _ f.integrable, apply measure_theory.simple_func.integral_congr (r • f).integrable, apply smul_to_simple_func end lemma norm_integral_le_norm (f : α →₁ₛ[μ] E) : ∥ integral f ∥ ≤ ∥f∥ := begin rw [integral, norm_eq_integral], exact f.to_simple_func.norm_integral_le_integral_norm f.integrable end /-- The Bochner integral over simple functions in l1 space as a continuous linear map. -/ def integral_clm : (α →₁ₛ[μ] E) →L[ℝ] E := linear_map.mk_continuous ⟨integral, integral_add, integral_smul⟩ 1 (λf, le_trans (norm_integral_le_norm _) $ by rw one_mul) local notation `Integral` := @integral_clm α E _ _ _ _ _ μ _ open continuous_linear_map lemma norm_Integral_le_one : ∥Integral∥ ≤ 1 := linear_map.mk_continuous_norm_le _ (zero_le_one) _ section pos_part lemma pos_part_to_simple_func (f : α →₁ₛ[μ] ℝ) : f.pos_part.to_simple_func =ᵐ[μ] f.to_simple_func.pos_part := begin have eq : ∀ a, f.to_simple_func.pos_part a = max (f.to_simple_func a) 0 := λa, rfl, have ae_eq : ∀ᵐ a ∂μ, f.pos_part.to_simple_func a = max (f.to_simple_func a) 0, { filter_upwards [to_simple_func_eq_to_fun f.pos_part, pos_part_to_fun (f : α →₁[μ] ℝ), to_simple_func_eq_to_fun f], simp only [mem_set_of_eq], assume a h₁ h₂ h₃, rw [h₁, ← coe_coe, coe_pos_part, h₂, coe_coe, ← h₃] }, refine ae_eq.mono (assume a h, _), rw [h, eq] end lemma neg_part_to_simple_func (f : α →₁ₛ[μ] ℝ) : f.neg_part.to_simple_func =ᵐ[μ] f.to_simple_func.neg_part := begin rw [simple_func.neg_part, measure_theory.simple_func.neg_part], filter_upwards [pos_part_to_simple_func (-f), neg_to_simple_func f], simp only [mem_set_of_eq], assume a h₁ h₂, rw h₁, show max _ _ = max _ _, rw h₂, refl end lemma integral_eq_norm_pos_part_sub (f : α →₁ₛ[μ] ℝ) : f.integral = ∥f.pos_part∥ - ∥f.neg_part∥ := begin -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq₁ : f.to_simple_func.pos_part =ᵐ[μ] (f.pos_part).to_simple_func.map norm, { filter_upwards [pos_part_to_simple_func f], simp only [mem_set_of_eq], assume a h, rw [simple_func.map_apply, h], conv_lhs { rw [← simple_func.pos_part_map_norm, simple_func.map_apply] } }, -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq₂ : f.to_simple_func.neg_part =ᵐ[μ] (f.neg_part).to_simple_func.map norm, { filter_upwards [neg_part_to_simple_func f], simp only [mem_set_of_eq], assume a h, rw [simple_func.map_apply, h], conv_lhs { rw [← simple_func.neg_part_map_norm, simple_func.map_apply] } }, -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq : ∀ᵐ a ∂μ, f.to_simple_func.pos_part a - f.to_simple_func.neg_part a = (f.pos_part).to_simple_func.map norm a - (f.neg_part).to_simple_func.map norm a, { filter_upwards [ae_eq₁, ae_eq₂], simp only [mem_set_of_eq], assume a h₁ h₂, rw [h₁, h₂] }, rw [integral, norm_eq_integral, norm_eq_integral, ← simple_func.integral_sub], { show f.to_simple_func.integral μ = ((f.pos_part.to_simple_func).map norm - f.neg_part.to_simple_func.map norm).integral μ, apply measure_theory.simple_func.integral_congr f.integrable, filter_upwards [ae_eq₁, ae_eq₂], simp only [mem_set_of_eq], assume a h₁ h₂, show _ = _ - _, rw [← h₁, ← h₂], have := f.to_simple_func.pos_part_sub_neg_part, conv_lhs {rw ← this}, refl }, { exact (integrable.max_zero f.integrable).congr ae_eq₁ }, { exact (integrable.max_zero f.integrable.neg).congr ae_eq₂ } end end pos_part end simple_func_integral end simple_func open simple_func variables [normed_space ℝ E] [normed_space ℝ F] [complete_space E] section integration_in_l1 local notation `to_l1` := coe_to_l1 α E ℝ local attribute [instance] simple_func.normed_group simple_func.normed_space open continuous_linear_map /-- The Bochner integral in l1 space as a continuous linear map. -/ def integral_clm : (α →₁[μ] E) →L[ℝ] E := integral_clm.extend to_l1 simple_func.dense_range simple_func.uniform_inducing /-- The Bochner integral in l1 space -/ def integral (f : α →₁[μ] E) : E := integral_clm f lemma integral_eq (f : α →₁[μ] E) : integral f = integral_clm f := rfl @[norm_cast] lemma simple_func.integral_l1_eq_integral (f : α →₁ₛ[μ] E) : integral (f : α →₁[μ] E) = f.integral := uniformly_extend_of_ind simple_func.uniform_inducing simple_func.dense_range simple_func.integral_clm.uniform_continuous _ variables (α E) @[simp] lemma integral_zero : integral (0 : α →₁[μ] E) = 0 := map_zero integral_clm variables {α E} lemma integral_add (f g : α →₁[μ] E) : integral (f + g) = integral f + integral g := map_add integral_clm f g lemma integral_neg (f : α →₁[μ] E) : integral (-f) = - integral f := map_neg integral_clm f lemma integral_sub (f g : α →₁[μ] E) : integral (f - g) = integral f - integral g := map_sub integral_clm f g lemma integral_smul (r : ℝ) (f : α →₁[μ] E) : integral (r • f) = r • integral f := map_smul r integral_clm f local notation `Integral` := @integral_clm α E _ _ _ _ _ μ _ _ local notation `sIntegral` := @simple_func.integral_clm α E _ _ _ _ _ μ _ lemma norm_Integral_le_one : ∥Integral∥ ≤ 1 := calc ∥Integral∥ ≤ (1 : nnreal) * ∥sIntegral∥ : op_norm_extend_le _ _ _ $ λs, by {rw [nnreal.coe_one, one_mul], refl} ... = ∥sIntegral∥ : one_mul _ ... ≤ 1 : norm_Integral_le_one lemma norm_integral_le (f : α →₁[μ] E) : ∥integral f∥ ≤ ∥f∥ := calc ∥integral f∥ = ∥Integral f∥ : rfl ... ≤ ∥Integral∥ * ∥f∥ : le_op_norm _ _ ... ≤ 1 * ∥f∥ : mul_le_mul_of_nonneg_right norm_Integral_le_one $ norm_nonneg _ ... = ∥f∥ : one_mul _ @[continuity] lemma continuous_integral : continuous (λ (f : α →₁[μ] E), f.integral) := by simp [l1.integral, l1.integral_clm.continuous] section pos_part lemma integral_eq_norm_pos_part_sub (f : α →₁[μ] ℝ) : integral f = ∥pos_part f∥ - ∥neg_part f∥ := begin -- Use `is_closed_property` and `is_closed_eq` refine @is_closed_property _ _ _ (coe : (α →₁ₛ[μ] ℝ) → (α →₁[μ] ℝ)) (λ f : α →₁[μ] ℝ, integral f = ∥pos_part f∥ - ∥neg_part f∥) l1.simple_func.dense_range (is_closed_eq _ _) _ f, { exact cont _ }, { refine continuous.sub (continuous_norm.comp l1.continuous_pos_part) (continuous_norm.comp l1.continuous_neg_part) }, -- Show that the property holds for all simple functions in the `L¹` space. { assume s, norm_cast, rw [← simple_func.norm_eq, ← simple_func.norm_eq], exact simple_func.integral_eq_norm_pos_part_sub _} end end pos_part end integration_in_l1 end l1 variables [normed_group E] [second_countable_topology E] [normed_space ℝ E] [complete_space E] [measurable_space E] [borel_space E] [normed_group F] [second_countable_topology F] [normed_space ℝ F] [complete_space F] [measurable_space F] [borel_space F] /-- The Bochner integral -/ def integral (μ : measure α) (f : α → E) : E := if hf : measurable f ∧ integrable f μ then (l1.of_fun f hf.1 hf.2).integral else 0 notation `∫` binders `, ` r:(scoped:60 f, f) ` ∂` μ:70 := integral μ r notation `∫` binders `, ` r:(scoped:60 f, integral volume f) := r notation `∫` binders ` in ` s `, ` r:(scoped:60 f, f) ` ∂` μ:70 := integral (measure.restrict μ s) r notation `∫` binders ` in ` s `, ` r:(scoped:60 f, integral (measure.restrict volume s) f) := r section properties open continuous_linear_map measure_theory.simple_func variables {f g : α → E} {μ : measure α} lemma integral_eq (f : α → E) (h₁ : measurable f) (h₂ : integrable f μ) : ∫ a, f a ∂μ = (l1.of_fun f h₁ h₂).integral := dif_pos ⟨h₁, h₂⟩ lemma l1.integral_eq_integral (f : α →₁[μ] E) : f.integral = ∫ a, f a ∂μ := by rw [integral_eq, l1.of_fun_to_fun] lemma integral_undef (h : ¬ (measurable f ∧ integrable f μ)) : ∫ a, f a ∂μ = 0 := dif_neg h lemma integral_non_integrable (h : ¬ integrable f μ) : ∫ a, f a ∂μ = 0 := integral_undef $ not_and_of_not_right _ h lemma integral_non_measurable (h : ¬ measurable f) : ∫ a, f a ∂μ = 0 := integral_undef $ not_and_of_not_left _ h variables (α E) local attribute [simp] -- Follows from `integral_const` below lemma integral_zero : ∫ a : α, (0:E) ∂μ = 0 := by rw [integral_eq, l1.of_fun_zero, l1.integral_zero] variables {α E} lemma integral_add (hfm : measurable f) (hfi : integrable f μ) (hgm : measurable g) (hgi : integrable g μ) : ∫ a, f a + g a ∂μ = ∫ a, f a ∂μ + ∫ a, g a ∂μ := by rw [integral_eq, integral_eq f hfm hfi, integral_eq g hgm hgi, ← l1.integral_add, ← l1.of_fun_add]; refl lemma integral_neg (f : α → E) : ∫ a, -f a ∂μ = - ∫ a, f a ∂μ := begin by_cases hf : measurable f ∧ integrable f μ, { rw [integral_eq f hf.1 hf.2, integral_eq (λa, - f a) hf.1.neg hf.2.neg, ← l1.integral_neg, ← l1.of_fun_neg], refl }, { rw [integral_undef hf, integral_undef, neg_zero], exact mt (and.imp measurable.of_neg integrable_neg_iff.1) hf } end lemma integral_sub (hfm : measurable f) (hfi : integrable f μ) (hgm : measurable g) (hgi : integrable g μ) : ∫ a, f a - g a ∂μ = ∫ a, f a ∂μ - ∫ a, g a ∂μ := by { rw [sub_eq_add_neg, ← integral_neg], exact integral_add hfm hfi hgm.neg hgi.neg } lemma integral_smul (r : ℝ) (f : α → E) : ∫ a, r • (f a) ∂μ = r • ∫ a, f a ∂μ := begin by_cases hf : measurable f ∧ integrable f μ, { rw [integral_eq f hf.1 hf.2, integral_eq (λa, r • (f a)), l1.of_fun_smul, l1.integral_smul] }, { by_cases hr : r = 0, { simp only [hr, measure_theory.integral_zero, zero_smul] }, have hf' : ¬(measurable (λa, r • f a) ∧ integrable (r • f) μ), { rwa [measurable_const_smul_iff hr, integrable_smul_iff hr f]; apply_instance }, rw [integral_undef hf, integral_undef hf', smul_zero] } end lemma integral_mul_left (r : ℝ) (f : α → ℝ) : ∫ a, r * (f a) ∂μ = r * ∫ a, f a ∂μ := integral_smul r f lemma integral_mul_right (r : ℝ) (f : α → ℝ) : ∫ a, (f a) * r ∂μ = ∫ a, f a ∂μ * r := by { simp only [mul_comm], exact integral_mul_left r f } lemma integral_div (r : ℝ) (f : α → ℝ) : ∫ a, (f a) / r ∂μ = ∫ a, f a ∂μ / r := integral_mul_right r⁻¹ f lemma integral_congr_ae (hfm : measurable f) (hgm : measurable g) (h : f =ᵐ[μ] g) : ∫ a, f a ∂μ = ∫ a, g a ∂μ := begin by_cases hfi : integrable f μ, { have hgi : integrable g μ := hfi.congr h, rw [integral_eq f hfm hfi, integral_eq g hgm hgi, (l1.of_fun_eq_of_fun f g hfm hfi hgm hgi).2 h] }, { have hgi : ¬ integrable g μ, { rw integrable_congr h at hfi, exact hfi }, rw [integral_non_integrable hfi, integral_non_integrable hgi] }, end @[simp] lemma l1.integral_of_fun_eq_integral {f : α → E} (f_m : measurable f) (f_i : integrable f μ) : ∫ a, (l1.of_fun f f_m f_i) a ∂μ = ∫ a, f a ∂μ := integral_congr_ae (l1.measurable _) f_m (l1.to_fun_of_fun f f_m f_i) @[continuity] lemma continuous_integral : continuous (λ (f : α →₁[μ] E), ∫ a, f a ∂μ) := begin convert l1.continuous_integral, ext f, rw l1.integral_eq_integral end lemma norm_integral_le_lintegral_norm (f : α → E) : ∥∫ a, f a ∂μ∥ ≤ ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) := begin by_cases hf : measurable f ∧ integrable f μ, { rw [integral_eq f hf.1 hf.2, ← l1.norm_of_fun_eq_lintegral_norm f hf.1 hf.2], exact l1.norm_integral_le _ }, { rw [integral_undef hf, norm_zero], exact to_real_nonneg } end lemma integral_eq_zero_of_ae {f : α → E} (hf : f =ᵐ[μ] 0) : ∫ a, f a ∂μ = 0 := if hfm : measurable f then by simp [integral_congr_ae hfm measurable_zero hf] else integral_non_measurable hfm /-- If `F i → f` in `L1`, then `∫ x, F i x ∂μ → ∫ x, f x∂μ`. -/ lemma tendsto_integral_of_l1 {ι} (f : α → E) (hfm : measurable f) (hfi : integrable f μ) {F : ι → α → E} {l : filter ι} (hFm : ∀ᶠ i in l, measurable (F i)) (hFi : ∀ᶠ i in l, integrable (F i) μ) (hF : tendsto (λ i, ∫⁻ x, edist (F i x) (f x) ∂μ) l (𝓝 0)) : tendsto (λ i, ∫ x, F i x ∂μ) l (𝓝 $ ∫ x, f x ∂μ) := begin rw [tendsto_iff_norm_tendsto_zero], replace hF : tendsto (λ i, ennreal.to_real $ ∫⁻ x, edist (F i x) (f x) ∂μ) l (𝓝 0) := (ennreal.tendsto_to_real zero_ne_top).comp hF, refine squeeze_zero_norm' (hFm.mp $ hFi.mono $ λ i hFi hFm, _) hF, simp only [norm_norm, ← integral_sub hFm hFi hfm hfi, edist_dist, dist_eq_norm], apply norm_integral_le_lintegral_norm end /-- Lebesgue dominated convergence theorem provides sufficient conditions under which almost everywhere convergence of a sequence of functions implies the convergence of their integrals. -/ theorem tendsto_integral_of_dominated_convergence {F : ℕ → α → E} {f : α → E} (bound : α → ℝ) (F_measurable : ∀ n, measurable (F n)) (f_measurable : measurable f) (bound_integrable : integrable bound μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫ a, F n a ∂μ) at_top (𝓝 $ ∫ a, f a ∂μ) := begin /- To show `(∫ a, F n a) --> (∫ f)`, suffices to show `∥∫ a, F n a - ∫ f∥ --> 0` -/ rw tendsto_iff_norm_tendsto_zero, /- But `0 ≤ ∥∫ a, F n a - ∫ f∥ = ∥∫ a, (F n a - f a) ∥ ≤ ∫ a, ∥F n a - f a∥, and thus we apply the sandwich theorem and prove that `∫ a, ∥F n a - f a∥ --> 0` -/ have lintegral_norm_tendsto_zero : tendsto (λn, ennreal.to_real $ ∫⁻ a, (ennreal.of_real ∥F n a - f a∥) ∂μ) at_top (𝓝 0) := (tendsto_to_real zero_ne_top).comp (tendsto_lintegral_norm_of_dominated_convergence F_measurable f_measurable bound_integrable h_bound h_lim), -- Use the sandwich theorem refine squeeze_zero (λ n, norm_nonneg _) _ lintegral_norm_tendsto_zero, -- Show `∥∫ a, F n a - ∫ f∥ ≤ ∫ a, ∥F n a - f a∥` for all `n` { assume n, have h₁ : integrable (F n) μ := bound_integrable.mono' (h_bound _), have h₂ : integrable f μ := integrable_of_dominated_convergence bound_integrable h_bound h_lim, rw ← integral_sub (F_measurable _) h₁ f_measurable h₂, exact norm_integral_le_lintegral_norm _ } end /-- Lebesgue dominated convergence theorem for filters with a countable basis -/ lemma tendsto_integral_filter_of_dominated_convergence {ι} {l : filter ι} {F : ι → α → E} {f : α → E} (bound : α → ℝ) (hl_cb : l.is_countably_generated) (hF_meas : ∀ᶠ n in l, measurable (F n)) (f_measurable : measurable f) (h_bound : ∀ᶠ n in l, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) (bound_integrable : integrable bound μ) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) l (𝓝 (f a))) : tendsto (λn, ∫ a, F n a ∂μ) l (𝓝 $ ∫ a, f a ∂μ) := begin rw hl_cb.tendsto_iff_seq_tendsto, { intros x xl, have hxl, { rw tendsto_at_top' at xl, exact xl }, have h := inter_mem_sets hF_meas h_bound, replace h := hxl _ h, rcases h with ⟨k, h⟩, rw ← tendsto_add_at_top_iff_nat k, refine tendsto_integral_of_dominated_convergence _ _ _ _ _ _, { exact bound }, { intro, refine (h _ _).1, exact nat.le_add_left _ _ }, { assumption }, { assumption }, { intro, refine (h _ _).2, exact nat.le_add_left _ _ }, { filter_upwards [h_lim], simp only [mem_set_of_eq], assume a h_lim, apply @tendsto.comp _ _ _ (λn, x (n + k)) (λn, F n a), { assumption }, rw tendsto_add_at_top_iff_nat, assumption } }, end /-- The Bochner integral of a real-valued function `f : α → ℝ` is the difference between the integral of the positive part of `f` and the integral of the negative part of `f`. -/ lemma integral_eq_lintegral_max_sub_lintegral_min {f : α → ℝ} (hfm : measurable f) (hfi : integrable f μ) : ∫ a, f a ∂μ = ennreal.to_real (∫⁻ a, (ennreal.of_real $ max (f a) 0) ∂μ) - ennreal.to_real (∫⁻ a, (ennreal.of_real $ - min (f a) 0) ∂μ) := let f₁ : α →₁[μ] ℝ := l1.of_fun f hfm hfi in -- Go to the `L¹` space have eq₁ : ennreal.to_real (∫⁻ a, (ennreal.of_real $ max (f a) 0) ∂μ) = ∥l1.pos_part f₁∥ := begin rw l1.norm_eq_norm_to_fun, congr' 1, apply lintegral_congr_ae, filter_upwards [l1.pos_part_to_fun f₁, l1.to_fun_of_fun f hfm hfi], simp only [mem_set_of_eq], assume a h₁ h₂, rw [h₁, h₂, real.norm_eq_abs, abs_of_nonneg], exact le_max_right _ _ end, -- Go to the `L¹` space have eq₂ : ennreal.to_real (∫⁻ a, (ennreal.of_real $ -min (f a) 0) ∂μ) = ∥l1.neg_part f₁∥ := begin rw l1.norm_eq_norm_to_fun, congr' 1, apply lintegral_congr_ae, filter_upwards [l1.neg_part_to_fun_eq_min f₁, l1.to_fun_of_fun f hfm hfi], simp only [mem_set_of_eq], assume a h₁ h₂, rw [h₁, h₂, real.norm_eq_abs, abs_of_nonneg], rw [min_eq_neg_max_neg_neg, _root_.neg_neg, neg_zero], exact le_max_right _ _ end, begin rw [eq₁, eq₂, integral, dif_pos], exact l1.integral_eq_norm_pos_part_sub _, { exact ⟨hfm, hfi⟩ } end lemma integral_eq_lintegral_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfm : measurable f) : ∫ a, f a ∂μ = ennreal.to_real (∫⁻ a, (ennreal.of_real $ f a) ∂μ) := begin by_cases hfi : integrable f μ, { rw integral_eq_lintegral_max_sub_lintegral_min hfm hfi, have h_min : ∫⁻ a, ennreal.of_real (-min (f a) 0) ∂μ = 0, { rw lintegral_eq_zero_iff, { refine hf.mono _, simp only [pi.zero_apply], assume a h, simp only [min_eq_right h, neg_zero, ennreal.of_real_zero] }, { exact measurable_of_real.comp (measurable_id.neg.comp $ hfm.min measurable_const) } }, have h_max : ∫⁻ a, ennreal.of_real (max (f a) 0) ∂μ = ∫⁻ a, ennreal.of_real (f a) ∂μ, { refine lintegral_congr_ae (hf.mono (λ a h, _)), rw [pi.zero_apply] at h, rw max_eq_left h }, rw [h_min, h_max, zero_to_real, _root_.sub_zero] }, { rw integral_non_integrable hfi, rw [integrable_iff_norm, lt_top_iff_ne_top, ne.def, not_not] at hfi, have : ∫⁻ (a : α), ennreal.of_real (f a) ∂μ = ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ, { refine lintegral_congr_ae (hf.mono $ assume a h, _), rw [real.norm_eq_abs, abs_of_nonneg h] }, rw [this, hfi], refl } end lemma integral_nonneg_of_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) : 0 ≤ ∫ a, f a ∂μ := begin by_cases hfm : measurable f, { rw integral_eq_lintegral_of_nonneg_ae hf hfm, exact to_real_nonneg }, { rw integral_non_measurable hfm } end lemma integral_nonpos_of_nonpos_ae {f : α → ℝ} (hf : f ≤ᵐ[μ] 0) : ∫ a, f a ∂μ ≤ 0 := begin have hf : 0 ≤ᵐ[μ] (-f) := hf.mono (assume a h, by rwa [pi.neg_apply, pi.zero_apply, neg_nonneg]), have : 0 ≤ ∫ a, -f a ∂μ := integral_nonneg_of_ae hf, rwa [integral_neg, neg_nonneg] at this, end section normed_group variables {H : Type*} [normed_group H] [second_countable_topology H] [measurable_space H] [borel_space H] lemma l1.norm_eq_integral_norm (f : α →₁[μ] H) : ∥f∥ = ∫ a, ∥f a∥ ∂μ := by rw [l1.norm_eq_norm_to_fun, integral_eq_lintegral_of_nonneg_ae (eventually_of_forall $ by simp [norm_nonneg]) (continuous_norm.measurable.comp f.measurable)] lemma l1.norm_of_fun_eq_integral_norm {f : α → H} (f_m : measurable f) (f_i : integrable f μ) : ∥l1.of_fun f f_m f_i∥ = ∫ a, ∥f a∥ ∂μ := begin rw l1.norm_eq_integral_norm, refine integral_congr_ae (l1.measurable_norm _) f_m.norm _, apply (l1.to_fun_of_fun f f_m f_i).mono, intros a ha, simp [ha] end end normed_group lemma integral_mono {f g : α → ℝ} (hfm : measurable f) (hfi : integrable f μ) (hgm : measurable g) (hgi : integrable g μ) (h : f ≤ᵐ[μ] g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ := le_of_sub_nonneg $ integral_sub hgm hgi hfm hfi ▸ integral_nonneg_of_ae $ h.mono (λ a, sub_nonneg_of_le) lemma integral_mono_of_nonneg {f g : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hgm : measurable g) (hgi : integrable g μ) (h : f ≤ᵐ[μ] g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ := begin by_cases hfm : measurable f, { refine integral_mono hfm _ hgm hgi h, refine (hgi.mono $ h.mp $ hf.mono $ λ x hf hfg, _), simpa [real.norm_eq_abs, abs_of_nonneg hf, abs_of_nonneg (le_trans hf hfg)] }, { rw [integral_non_measurable hfm], exact integral_nonneg_of_ae (hf.trans h) } end lemma norm_integral_le_integral_norm (f : α → E) : ∥(∫ a, f a ∂μ)∥ ≤ ∫ a, ∥f a∥ ∂μ := have le_ae : ∀ᵐ a ∂μ, 0 ≤ ∥f a∥ := eventually_of_forall (λa, norm_nonneg _), classical.by_cases ( λh : measurable f, calc ∥∫ a, f a ∂μ∥ ≤ ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) : norm_integral_le_lintegral_norm _ ... = ∫ a, ∥f a∥ ∂μ : (integral_eq_lintegral_of_nonneg_ae le_ae $ measurable.norm h).symm ) ( λh : ¬measurable f, begin rw [integral_non_measurable h, norm_zero], exact integral_nonneg_of_ae le_ae end ) lemma norm_integral_le_of_norm_le {f : α → E} {g : α → ℝ} (hgm : measurable g) (hgi : integrable g μ) (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ g x) : ∥∫ x, f x ∂μ∥ ≤ ∫ x, g x ∂μ := calc ∥∫ x, f x ∂μ∥ ≤ ∫ x, ∥f x∥ ∂μ : norm_integral_le_integral_norm f ... ≤ ∫ x, g x ∂μ : integral_mono_of_nonneg (eventually_of_forall $ λ x, norm_nonneg _) hgm hgi h lemma integral_finset_sum {ι} (s : finset ι) {f : ι → α → E} (hfm : ∀ i, measurable (f i)) (hfi : ∀ i, integrable (f i) μ) : ∫ a, ∑ i in s, f i a ∂μ = ∑ i in s, ∫ a, f i a ∂μ := begin refine finset.induction_on s _ _, { simp only [integral_zero, finset.sum_empty] }, { assume i s his ih, simp only [his, finset.sum_insert, not_false_iff], rw [integral_add (hfm _) (hfi _) (s.measurable_sum hfm) (integrable_finset_sum s hfm hfi), ih] } end lemma simple_func.integral_eq_integral (f : α →ₛ E) (hfi : integrable f μ) : f.integral μ = ∫ x, f x ∂μ := begin rw [integral_eq f f.measurable hfi, ← l1.simple_func.of_simple_func_eq_of_fun, l1.simple_func.integral_l1_eq_integral, l1.simple_func.integral_eq_integral], exact simple_func.integral_congr hfi (l1.simple_func.to_simple_func_of_simple_func _ _).symm end @[simp] lemma integral_const (c : E) : ∫ x : α, c ∂μ = (μ univ).to_real • c := begin by_cases hμ : μ univ < ⊤, { haveI : finite_measure μ := ⟨hμ⟩, calc ∫ x : α, c ∂μ = (simple_func.const α c).integral μ : ((simple_func.const α c).integral_eq_integral (integrable_const _)).symm ... = _ : _, rw [simple_func.integral], by_cases ha : nonempty α, { resetI, simp [preimage_const_of_mem] }, { simp [μ.eq_zero_of_not_nonempty ha] } }, { by_cases hc : c = 0, { simp [hc] }, { have : ¬integrable (λ x : α, c) μ, { simp only [integrable_const_iff, not_or_distrib], exact ⟨hc, hμ⟩ }, simp only [not_lt, top_le_iff] at hμ, simp [integral_non_integrable, *] } } end lemma norm_integral_le_of_norm_le_const [finite_measure μ] {f : α → E} {C : ℝ} (h : ∀ᵐ x ∂μ, ∥f x∥ ≤ C) : ∥∫ x, f x ∂μ∥ ≤ C * (μ univ).to_real := calc ∥∫ x, f x ∂μ∥ ≤ ∫ x, C ∂μ : norm_integral_le_of_norm_le measurable_const (integrable_const C) h ... = C * (μ univ).to_real : by rw [integral_const, smul_eq_mul, mul_comm] variable {ν : measure α} lemma integral_add_measure {f : α → E} (hμ : integrable f μ) (hν : integrable f ν) : ∫ x, f x ∂(μ + ν) = ∫ x, f x ∂μ + ∫ x, f x ∂ν := begin by_cases hfm : measurable f; [skip, by simp only [integral_non_measurable hfm, zero_add]], have hfi := hμ.add_measure hν, rcases simple_func_sequence_tendsto' hfm hfi with ⟨F, hFi, hFt⟩, have hFiμ : ∀ i, integrable (F i) μ := λ i, (hFi i).left_of_add_measure, have hFiν : ∀ i, integrable (F i) ν := λ i, (hFi i).right_of_add_measure, simp only [← edist_nndist] at hFt, have hμν : tendsto (λ i, ∫ x, F i x ∂(μ + ν)) at_top (𝓝 ∫ x, f x ∂(μ + ν)) := tendsto_integral_of_l1 _ hfm hfi (eventually_of_forall $ λ i, (F i).measurable) (eventually_of_forall hFi) hFt, have hμ : tendsto (λ i, ∫ x, F i x ∂μ) at_top (𝓝 ∫ x, f x ∂μ), { refine tendsto_integral_of_l1 _ hfm hμ (eventually_of_forall $ λ i, (F i).measurable) (eventually_of_forall hFiμ) _, refine tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds hFt (λ _, zero_le _) _, exact λ i, lintegral_mono' (measure.le_add_right $ le_refl μ) (le_refl _) }, have hν : tendsto (λ i, ∫ x, F i x ∂ν) at_top (𝓝 ∫ x, f x ∂ν), { refine tendsto_integral_of_l1 _ hfm hν (eventually_of_forall $ λ i, (F i).measurable) (eventually_of_forall hFiν) _, refine tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds hFt (λ _, zero_le _) _, exact λ i, lintegral_mono' (measure.le_add_left $ le_refl ν) (le_refl _) }, apply tendsto_nhds_unique hμν, simpa only [← simple_func.integral_eq_integral, *, simple_func.integral_add_measure] using hμ.add hν end @[simp] lemma integral_zero_measure (f : α → E) : ∫ x, f x ∂0 = 0 := norm_le_zero_iff.1 $ le_trans (norm_integral_le_lintegral_norm f) $ by simp @[simp] lemma integral_smul_measure (f : α → E) (c : ennreal) : ∫ x, f x ∂(c • μ) = c.to_real • ∫ x, f x ∂μ := begin -- First we consider “degenerate” cases: -- `f` is not measurable by_cases hfm : measurable f, swap, { simp [integral_non_measurable hfm] }, -- `c = 0` rcases (zero_le c).eq_or_lt with rfl|h0, { simp }, -- `c = ⊤` rcases (le_top : c ≤ ⊤).eq_or_lt with rfl|hc, { rw [ennreal.top_to_real, zero_smul], by_cases hf : f =ᵐ[μ] 0, { have : f =ᵐ[⊤ • μ] 0 := ae_smul_measure hf ⊤, exact integral_eq_zero_of_ae this }, { apply integral_non_integrable, rw [integrable, lintegral_smul_measure, top_mul, if_neg], { apply lt_irrefl }, { rw [lintegral_eq_zero_iff hfm.ennnorm], refine λ h, hf (h.mono $ λ x, _), simp } } }, -- `f` is not integrable by_cases hfi : integrable f μ, swap, { rw [integral_non_integrable hfi, smul_zero], refine integral_non_integrable (mt (λ h, _) hfi), convert h.smul_measure (ennreal.inv_lt_top.2 h0), rw [smul_smul, ennreal.inv_mul_cancel (ne_of_gt h0) (ne_of_lt hc), one_smul] }, -- Main case: `0 < c < ⊤`, `f` is measurable and integrable rcases simple_func_sequence_tendsto' hfm hfi with ⟨F, hFi, hFt⟩, have hFi' := λ n, (hFi n).smul_measure hc, simp only [← edist_nndist] at hFt, have hμ : tendsto (λ i, ∫ x, F i x ∂μ) at_top (𝓝 ∫ x, f x ∂μ) := tendsto_integral_of_l1 _ hfm hfi (eventually_of_forall $ λ i, (F i).measurable) (eventually_of_forall hFi) hFt, refine tendsto_nhds_unique _ (tendsto_const_nhds.smul hμ), replace hFt := ennreal.tendsto.mul (tendsto_const_nhds : tendsto (λ _, c) _ _) (or.inr ennreal.zero_ne_top) hFt (or.inr $ ne_of_lt hc), simp only [mul_zero, ← lintegral_smul_measure] at hFt, convert tendsto_integral_of_l1 _ hfm (hfi.smul_measure hc) (eventually_of_forall $ λ i, (F i).measurable) (eventually_of_forall hFi') hFt, ext1 n, simp only [← (F n).integral_eq_integral, hFi, hFi', simple_func.integral, measure.smul_apply, finset.smul_sum, smul_smul, ennreal.to_real_mul_to_real] end lemma integral_map_measure {β} [measurable_space β] {φ : α → β} (hφ : measurable φ) {f : β → E} (hfm : measurable f) : ∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ := begin by_cases hfi : integrable f (measure.map φ μ), swap, { rw [integral_non_integrable hfi, integral_non_integrable], rwa [← integrable_map_measure hφ hfm] }, rcases simple_func_sequence_tendsto' hfm hfi with ⟨F, hFi, hFt⟩, simp only [← edist_nndist] at hFt, have hF : tendsto (λ i, ∫ x, F i x ∂(measure.map φ μ)) at_top (𝓝 ∫ x, f x ∂(measure.map φ μ)) := tendsto_integral_of_l1 _ hfm hfi (eventually_of_forall $ λ i, (F i).measurable) (eventually_of_forall hFi) hFt, refine tendsto_nhds_unique hF _, clear hF, simp only [lintegral_map ((F _).measurable.edist hfm) hφ] at hFt, have hFi' := hFi, simp only [integrable_map_measure, hφ, hfm, (F _).measurable] at hfi hFi', refine (tendsto_integral_of_l1 _ (hfm.comp hφ) hfi (eventually_of_forall $ λ i, (F i).measurable.comp hφ) (eventually_of_forall $ hFi') hFt).congr (λ n, _), rw [← simple_func.integral_eq_integral _ (hFi n), ← simple_func.coe_comp _ hφ, ← simple_func.integral_eq_integral ((F n).comp φ hφ) (hFi' n)], simp only [simple_func.integral, measure.map_apply, *, simple_func.is_measurable_preimage], refine finset.sum_subset ((F n).range_comp_subset_range _) (λ y _ hy, _), rw [simple_func.mem_range, ← set.preimage_singleton_eq_empty] at hy, rw [hy], simp end lemma integral_dirac (f : α → E) (a : α) (hfm : measurable f) : ∫ x, f x ∂(measure.dirac a) = f a := calc ∫ x, f x ∂(measure.dirac a) = ∫ x, f a ∂(measure.dirac a) : integral_congr_ae hfm measurable_const $ eventually_eq_dirac hfm ... = f a : by simp [measure.dirac_apply_of_mem] end properties mk_simp_attribute integral_simps "Simp set for integral rules." attribute [integral_simps] integral_neg integral_smul l1.integral_add l1.integral_sub l1.integral_smul l1.integral_neg attribute [irreducible] integral l1.integral end measure_theory
249ba274a779f642ac8710407d0085b7d10d4e3d
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/normed_space/is_R_or_C.lean
5579d501b96b7060781ed1c1e226a43e93da2afc
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
4,111
lean
/- Copyright (c) 2021 Kalle Kytölä. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kalle Kytölä -/ import data.complex.is_R_or_C import analysis.normed_space.operator_norm import analysis.normed_space.pointwise /-! # Normed spaces over R or C This file is about results on normed spaces over the fields `ℝ` and `ℂ`. ## Main definitions None. ## Main theorems * `continuous_linear_map.op_norm_bound_of_ball_bound`: A bound on the norms of values of a linear map in a ball yields a bound on the operator norm. ## Notes This file exists mainly to avoid importing `is_R_or_C` in the main normed space theory files. -/ open metric @[simp, is_R_or_C_simps] lemma is_R_or_C.norm_coe_norm {𝕜 : Type*} [is_R_or_C 𝕜] {E : Type*} [normed_add_comm_group E] {z : E} : ∥(∥z∥ : 𝕜)∥ = ∥z∥ := by { unfold_coes, simp only [norm_algebra_map', ring_hom.to_fun_eq_coe, norm_norm], } variables {𝕜 : Type*} [is_R_or_C 𝕜] {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] /-- Lemma to normalize a vector in a normed space `E` over either `ℂ` or `ℝ` to unit length. -/ @[simp] lemma norm_smul_inv_norm {x : E} (hx : x ≠ 0) : ∥(∥x∥⁻¹ : 𝕜) • x∥ = 1 := begin have : ∥x∥ ≠ 0 := by simp [hx], field_simp [norm_smul] end /-- Lemma to normalize a vector in a normed space `E` over either `ℂ` or `ℝ` to length `r`. -/ lemma norm_smul_inv_norm' {r : ℝ} (r_nonneg : 0 ≤ r) {x : E} (hx : x ≠ 0) : ∥(r * ∥x∥⁻¹ : 𝕜) • x∥ = r := begin have : ∥x∥ ≠ 0 := by simp [hx], field_simp [norm_smul, is_R_or_C.norm_eq_abs, r_nonneg] with is_R_or_C_simps end lemma linear_map.bound_of_sphere_bound {r : ℝ} (r_pos : 0 < r) (c : ℝ) (f : E →ₗ[𝕜] 𝕜) (h : ∀ z ∈ sphere (0 : E) r, ∥f z∥ ≤ c) (z : E) : ∥f z∥ ≤ c / r * ∥z∥ := begin by_cases z_zero : z = 0, { rw z_zero, simp only [linear_map.map_zero, norm_zero, mul_zero], }, set z₁ := (r * ∥z∥⁻¹ : 𝕜) • z with hz₁, have norm_f_z₁ : ∥f z₁∥ ≤ c, { apply h, rw mem_sphere_zero_iff_norm, exact norm_smul_inv_norm' r_pos.le z_zero }, have r_ne_zero : (r : 𝕜) ≠ 0 := is_R_or_C.of_real_ne_zero.mpr r_pos.ne', have eq : f z = ∥z∥ / r * (f z₁), { rw [hz₁, linear_map.map_smul, smul_eq_mul], rw [← mul_assoc, ← mul_assoc, div_mul_cancel _ r_ne_zero, mul_inv_cancel, one_mul], simp only [z_zero, is_R_or_C.of_real_eq_zero, norm_eq_zero, ne.def, not_false_iff], }, rw [eq, norm_mul, norm_div, is_R_or_C.norm_coe_norm, is_R_or_C.norm_of_nonneg r_pos.le, div_mul_eq_mul_div, div_mul_eq_mul_div, mul_comm], apply div_le_div _ _ r_pos rfl.ge, { exact mul_nonneg ((norm_nonneg _).trans norm_f_z₁) (norm_nonneg z), }, apply mul_le_mul norm_f_z₁ rfl.le (norm_nonneg z) ((norm_nonneg _).trans norm_f_z₁), end /-- `linear_map.bound_of_ball_bound` is a version of this over arbitrary nontrivially normed fields. It produces a less precise bound so we keep both versions. -/ lemma linear_map.bound_of_ball_bound' {r : ℝ} (r_pos : 0 < r) (c : ℝ) (f : E →ₗ[𝕜] 𝕜) (h : ∀ z ∈ closed_ball (0 : E) r, ∥f z∥ ≤ c) (z : E) : ∥f z∥ ≤ c / r * ∥z∥ := f.bound_of_sphere_bound r_pos c (λ z hz, h z hz.le) z lemma continuous_linear_map.op_norm_bound_of_ball_bound {r : ℝ} (r_pos : 0 < r) (c : ℝ) (f : E →L[𝕜] 𝕜) (h : ∀ z ∈ closed_ball (0 : E) r, ∥f z∥ ≤ c) : ∥f∥ ≤ c / r := begin apply continuous_linear_map.op_norm_le_bound, { apply div_nonneg _ r_pos.le, exact (norm_nonneg _).trans (h 0 (by simp only [norm_zero, mem_closed_ball, dist_zero_left, r_pos.le])), }, apply linear_map.bound_of_ball_bound' r_pos, exact λ z hz, h z hz, end variables (𝕜) include 𝕜 lemma normed_space.sphere_nonempty_is_R_or_C [nontrivial E] {r : ℝ} (hr : 0 ≤ r) : nonempty (sphere (0:E) r) := begin letI : normed_space ℝ E := normed_space.restrict_scalars ℝ 𝕜 E, exact set.nonempty_coe_sort.mpr (normed_space.sphere_nonempty.mpr hr), end
0f31f3200c9591a3f6e5ef776efe06ffcc1aa19f
88fb7558b0636ec6b181f2a548ac11ad3919f8a5
/library/init/wf.lean
c9b69edb5b7b50ec2c3247f8705c5ecacfb27d03
[ "Apache-2.0" ]
permissive
moritayasuaki/lean
9f666c323cb6fa1f31ac597d777914aed41e3b7a
ae96ebf6ee953088c235ff7ae0e8c95066ba8001
refs/heads/master
1,611,135,440,814
1,493,852,869,000
1,493,852,869,000
90,269,903
0
0
null
1,493,906,291,000
1,493,906,291,000
null
UTF-8
Lean
false
false
6,441
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import init.data.nat.basic init.data.prod universes u v inductive acc {α : Type u} (r : α → α → Prop) : α → Prop | intro : ∀ x, (∀ y, r y x → acc y) → acc x namespace acc variables {α : Type u} {r : α → α → Prop} def inv {x y : α} (h₁ : acc r x) (h₂ : r y x) : acc r y := acc.rec_on h₁ (λ x₁ ac₁ ih h₂, ac₁ y h₂) h₂ end acc inductive well_founded {α : Type u} (r : α → α → Prop) : Prop | intro : (∀ a, acc r a) → well_founded namespace well_founded def apply {α : Type u} {r : α → α → Prop} (wf : well_founded r) : ∀ a, acc r a := take a, well_founded.rec_on wf (λ p, p) a section parameters {α : Type u} {r : α → α → Prop} local infix `≺`:50 := r parameter hwf : well_founded r lemma recursion {C : α → Sort v} (a : α) (h : Π x, (Π y, y ≺ x → C y) → C x) : C a := acc.rec_on (apply hwf a) (λ x₁ ac₁ ih, h x₁ ih) lemma induction {C : α → Prop} (a : α) (h : ∀ x, (∀ y, y ≺ x → C y) → C x) : C a := recursion a h variable {C : α → Sort v} variable F : Π x, (Π y, y ≺ x → C y) → C x def fix_F (x : α) (a : acc r x) : C x := acc.rec_on a (λ x₁ ac₁ ih, F x₁ ih) lemma fix_F_eq (x : α) (acx : acc r x) : fix_F F x acx = F x (λ (y : α) (p : y ≺ x), fix_F F y (acc.inv acx p)) := acc.drec (λ x r ih, rfl) acx end variables {α : Type u} {C : α → Sort v} {r : α → α → Prop} -- Well-founded fixpoint def fix (hwf : well_founded r) (F : Π x, (Π y, r y x → C y) → C x) (x : α) : C x := fix_F F x (apply hwf x) -- Well-founded fixpoint satisfies fixpoint equation lemma fix_eq (hwf : well_founded r) (F : Π x, (Π y, r y x → C y) → C x) (x : α) : fix hwf F x = F x (λ y h, fix hwf F y) := fix_F_eq F x (apply hwf x) end well_founded open well_founded -- Empty relation is well-founded def empty_wf {α : Type u} : well_founded empty_relation := well_founded.intro (λ (a : α), acc.intro a (λ (b : α) (lt : false), false.rec _ lt)) -- Subrelation of a well-founded relation is well-founded namespace subrelation section parameters {α : Type u} {r Q : α → α → Prop} parameters (h₁ : subrelation Q r) parameters (h₂ : well_founded r) def accessible {a : α} (ac : acc r a) : acc Q a := acc.rec_on ac (λ x ax ih, acc.intro x (λ (y : α) (lt : Q y x), ih y (h₁ lt))) def wf : well_founded Q := ⟨λ a, accessible (apply h₂ a)⟩ end end subrelation -- The inverse image of a well-founded relation is well-founded namespace inv_image section parameters {α : Type u} {β : Type v} {r : β → β → Prop} parameters (f : α → β) parameters (h : well_founded r) private def acc_aux {b : β} (ac : acc r b) : ∀ (x : α), f x = b → acc (inv_image r f) x := acc.rec_on ac (λ x acx ih z e, acc.intro z (λ y lt, eq.rec_on e (λ acx ih, ih (f y) lt y rfl) acx ih)) def accessible {a : α} (ac : acc r (f a)) : acc (inv_image r f) a := acc_aux ac a rfl def wf : well_founded (inv_image r f) := ⟨λ a, accessible (apply h (f a))⟩ end end inv_image -- The transitive closure of a well-founded relation is well-founded namespace tc section parameters {α : Type u} {r : α → α → Prop} local notation `r⁺` := tc r def accessible {z : α} (ac : acc r z) : acc (tc r) z := acc.rec_on ac (λ x acx ih, acc.intro x (λ y rel, tc.rec_on rel (λ a b rab acx ih, ih a rab) (λ a b c rab rbc ih₁ ih₂ acx ih, acc.inv (ih₂ acx ih) rab) acx ih)) def wf (h : well_founded r) : well_founded r⁺ := ⟨λ a, accessible (apply h a)⟩ end end tc -- less-than is well-founded def nat.lt_wf : well_founded nat.lt := ⟨nat.rec (acc.intro 0 (λ n h, absurd h (nat.not_lt_zero n))) (λ n ih, acc.intro (nat.succ n) (λ m h, or.elim (nat.eq_or_lt_of_le (nat.le_of_succ_le_succ h)) (λ e, eq.substr e ih) (acc.inv ih)))⟩ def measure {α : Type u} : (α → ℕ) → α → α → Prop := inv_image (<) def measure_wf {α : Type u} (f : α → ℕ) : well_founded (measure f) := inv_image.wf f nat.lt_wf namespace prod open well_founded section variables {α : Type u} {β : Type v} variable (ra : α → α → Prop) variable (rb : β → β → Prop) -- Lexicographical order based on ra and rb inductive lex : α × β → α × β → Prop | left : ∀ {a₁} b₁ {a₂} b₂, ra a₁ a₂ → lex (a₁, b₁) (a₂, b₂) | right : ∀ a {b₁ b₂}, rb b₁ b₂ → lex (a, b₁) (a, b₂) -- relational product based on ra and rb inductive rprod : α × β → α × β → Prop | intro : ∀ {a₁ b₁ a₂ b₂}, ra a₁ a₂ → rb b₁ b₂ → rprod (a₁, b₁) (a₂, b₂) end section parameters {α : Type u} {β : Type v} parameters {ra : α → α → Prop} {rb : β → β → Prop} local infix `≺`:50 := lex ra rb def lex_accessible {a} (aca : acc ra a) (acb : ∀ b, acc rb b): ∀ b, acc (lex ra rb) (a, b) := acc.rec_on aca (λ xa aca iha b, acc.rec_on (acb b) (λ xb acb ihb, acc.intro (xa, xb) (λ p lt, have aux : xa = xa → xb = xb → acc (lex ra rb) p, from @prod.lex.rec_on α β ra rb (λ p₁ p₂, fst p₂ = xa → snd p₂ = xb → acc (lex ra rb) p₁) p (xa, xb) lt (λ a₁ b₁ a₂ b₂ h (eq₂ : a₂ = xa) (eq₃ : b₂ = xb), iha a₁ (eq.rec_on eq₂ h) b₁) (λ a b₁ b₂ h (eq₂ : a = xa) (eq₃ : b₂ = xb), eq.rec_on eq₂.symm (ihb b₁ (eq.rec_on eq₃ h))), aux rfl rfl))) -- The lexicographical order of well founded relations is well-founded def lex_wf (ha : well_founded ra) (hb : well_founded rb) : well_founded (lex ra rb) := ⟨λ p, cases_on p (λ a b, lex_accessible (apply ha a) (well_founded.apply hb) b)⟩ -- relational product is a subrelation of the lex def rprod_sub_lex : ∀ a b, rprod ra rb a b → lex ra rb a b := λ a b h, prod.rprod.rec_on h (λ a₁ b₁ a₂ b₂ h₁ h₂, lex.left rb b₁ b₂ h₁) -- The relational product of well founded relations is well-founded def rprod_wf (ha : well_founded ra) (hb : well_founded rb) : well_founded (rprod ra rb) := subrelation.wf (rprod_sub_lex) (lex_wf ha hb) end end prod
f41f131fecaaf8ad6e5bfae70d7d8f563c010c22
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/tactic/ext.lean
f7e422bc6ed89654436db09dfe082ce0c76b3e1c
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
19,974
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Jesse Michael Han -/ import tactic.rcases import logic.function.basic universes u₁ u₂ open interactive interactive.types section ext open lean.parser nat tactic declare_trace ext /-- `derive_struct_ext_lemma n` generates two extensionality lemmas based on the equality of all non-propositional projections. On the following: ```lean @[ext] structure foo (α : Type*) := (x y : ℕ) (z : {z // z < x}) (k : α) (h : x < y) ``` `derive_struct_lemma` generates: ```lean lemma foo.ext : ∀ {α : Type u_1} (x y : foo α), x.x = y.x → x.y = y.y → x.z == y.z → x.k = y.k → x = y lemma foo.ext_iff : ∀ {α : Type u_1} (x y : foo α), x = y ↔ x.x = y.x ∧ x.y = y.y ∧ x.z == y.z ∧ x.k = y.k ``` -/ meta def derive_struct_ext_lemma (n : name) : tactic name := do e ← get_env, fs ← e.structure_fields n, d ← get_decl n, n ← resolve_constant n, let r := @expr.const tt n $ d.univ_params.map level.param, (args,_) ← infer_type r >>= open_pis, let args := args.map expr.to_implicit_local_const, let t := r.mk_app args, x ← mk_local_def `x t, y ← mk_local_def `y t, let args_x := args ++ [x], let args_y := args ++ [y], bs ← fs.mmap $ λ f, do { d ← get_decl (n ++ f), let a := @expr.const tt (n ++ f) $ d.univ_params.map level.param, t ← infer_type a, s ← infer_type t, if s ≠ `(Prop) then do let x := a.mk_app args_x, let y := a.mk_app args_y, t ← infer_type x, t' ← infer_type y, some <$> if t = t' then mk_app `eq [x,y] >>= mk_local_def `h else mk_mapp `heq [none,x,none,y] >>= mk_local_def `h else pure none }, let bs := bs.filter_map id, eq_t ← mk_app `eq [x,y], t ← pis (args ++ [x,y] ++ bs) eq_t, pr ← run_async $ do { (_,pr) ← solve_aux t (do { args ← intron args.length, x ← intro1, y ← intro1, cases x, cases y, bs.mmap' (λ _, do e ← intro1, cases e), reflexivity }), instantiate_mvars pr }, let decl_n := n <.> "ext", add_decl (declaration.thm decl_n d.univ_params t pr), bs ← bs.mmap infer_type, let rhs := expr.mk_and_lst bs, iff_t ← mk_app `iff [eq_t,rhs], t ← pis (args ++ [x,y]) iff_t, pr ← run_async $ do { (_,pr) ← solve_aux t $ do { args ← intron args.length, x ← intro1, y ← intro1, cases x, cases y, split, solve1 $ do { h ← intro1, hs ← injection h, subst_vars, repeat (refine ``( and.intro _ _ ) >> reflexivity ), done <|> reflexivity }, solve1 $ do { repeat (do refine ``(and_imp.mpr _), h ← intro1, cases h, skip ), h ← intro1, cases h, reflexivity } }, instantiate_mvars pr }, add_decl (declaration.thm (n <.> "ext_iff") d.univ_params t pr), pure decl_n meta def get_ext_subject : expr → tactic name | (expr.pi n bi d b) := do v ← mk_local' n bi d, b' ← whnf $ b.instantiate_var v, get_ext_subject b' | (expr.app _ e) := do t ← infer_type e >>= instantiate_mvars >>= head_beta, if t.get_app_fn.is_constant then pure $ t.get_app_fn.const_name else if t.is_pi then pure $ name.mk_numeral 0 name.anonymous else if t.is_sort then pure $ name.mk_numeral 1 name.anonymous else do t ← pp t, fail format!"only constants and Pi types are supported: {t}" | e := fail format!"Only expressions of the form `_ → _ → ... → R ... e are supported: {e}" open native meta def saturate_fun : name → tactic expr | (name.mk_numeral 0 name.anonymous) := do v₀ ← mk_mvar, v₁ ← mk_mvar, return $ v₀.imp v₁ | (name.mk_numeral 1 name.anonymous) := do u ← mk_meta_univ, pure $ expr.sort u | n := do e ← resolve_constant n >>= mk_const, a ← get_arity e, e.mk_app <$> (list.iota a).mmap (λ _, mk_mvar) meta def equiv_type_constr (n n' : name) : tactic unit := do e ← saturate_fun n, e' ← saturate_fun n', unify e e' <|> fail format!"{n} and {n'} are not definitionally equal types" section performance_hack /-- For performance reasons, it is inadvisable to use `user_attribute.get_param`. The parameter is stored as a reflected expression. When calling `get_param`, the stored parameter is evaluated using `eval_expr`, which first compiles the expression into VM bytecode. The unevaluated expression is available using `user_attribute.get_param_untyped`. In particular, `user_attribute.get_param` MUST NEVER BE USED in the implementation of an attribute cache. This is because calling `eval_expr` disables the attribute cache. There are several possible workarounds: 1. Set a different attribute depending on the parameter. 2. Use your own evaluation function instead of `eval_expr`, such as e.g. `expr.to_nat`. 3. Write your own `has_reflect Param` instance (using a more efficient serialization format). The `user_attribute` code unfortunately checks whether the expression has the correct type, but you can use `` `(id %%e : Param) `` to pretend that your expression `e` has type `Param`. -/ library_note "user attribute parameters" /-! For performance reasons, the parameters of the `@[ext]` attribute are stored in two auxiliary attributes: ```lean attribute [ext thunk] funext -- is turned into attribute [_ext_core (@id name @funext)] thunk attribute [_ext_lemma_core] funext ``` see Note [user attribute parameters] -/ local attribute [semireducible] reflected local attribute [instance, priority 9000] private meta def hacky_name_reflect : has_reflect name := λ n, `(id %%(expr.const n []) : name) @[user_attribute] private meta def ext_attr_core : user_attribute (name_map name) name := { name := `_ext_core, descr := "(internal attribute used by ext)", cache_cfg := { dependencies := [], mk_cache := λ ns, ns.mfoldl (λ m n, do ext_l ← ext_attr_core.get_param_untyped n, pure (m.insert n ext_l.app_arg.const_name)) mk_name_map }, parser := failure } end performance_hack /-- Private attribute used to tag extensionality lemmas. -/ @[user_attribute] private meta def ext_lemma_attr_core : user_attribute := { name := `_ext_lemma_core, descr := "(internal attribute used by ext)", parser := failure } /-- Returns the extensionality lemmas in the environment, as a map from structure name to lemma name. -/ meta def get_ext_lemmas : tactic (name_map name) := ext_attr_core.get_cache /-- Returns the extensionality lemmas in the environment, as a list of lemma names. -/ meta def get_ext_lemma_names : tactic (list name) := attribute.get_instances ext_lemma_attr_core.name /-- Marks `lem` as an extensionality lemma corresponding to type constructor `constr`; if `persistent` is true then this is a global attribute, else local. -/ meta def add_ext_lemma (constr lem : name) (persistent : bool) : tactic unit := ext_attr_core.set constr lem persistent >> ext_lemma_attr_core.set lem () persistent /-- Tag lemmas of the form: ```lean @[ext] lemma my_collection.ext (a b : my_collection) (h : ∀ x, a.lookup x = b.lookup y) : a = b := ... ``` The attribute indexes extensionality lemma using the type of the objects (i.e. `my_collection`) which it gets from the statement of the lemma. In some cases, the same lemma can be used to state the extensionality of multiple types that are definitionally equivalent. ```lean attribute [ext thunk, ext stream] funext ``` Also, the following: ```lean @[ext] lemma my_collection.ext (a b : my_collection) (h : ∀ x, a.lookup x = b.lookup y) : a = b := ... ``` is equivalent to ```lean @[ext my_collection] lemma my_collection.ext (a b : my_collection) (h : ∀ x, a.lookup x = b.lookup y) : a = b := ... ``` This allows us specify type synonyms along with the type that is referred to in the lemma statement. ```lean @[ext, ext my_type_synonym] lemma my_collection.ext (a b : my_collection) (h : ∀ x, a.lookup x = b.lookup y) : a = b := ... ``` The `ext` attribute can be applied to a structure to generate its extensionality lemmas: ```lean @[ext] structure foo (α : Type*) := (x y : ℕ) (z : {z // z < x}) (k : α) (h : x < y) ``` will generate: ```lean @[ext] lemma foo.ext : ∀ {α : Type u_1} (x y : foo α), x.x = y.x → x.y = y.y → x.z == y.z → x.k = y.k → x = y lemma foo.ext_iff : ∀ {α : Type u_1} (x y : foo α), x = y ↔ x.x = y.x ∧ x.y = y.y ∧ x.z == y.z ∧ x.k = y.k ``` -/ @[user_attribute] meta def extensional_attribute : user_attribute unit (option name) := { name := `ext, descr := "lemmas usable by `ext` tactic", parser := optional ident, after_set := some $ λ n _ b, do add ← extensional_attribute.get_param n, e ← get_env, n ← if (e.structure_fields n).is_some then derive_struct_ext_lemma n else pure n, s ← mk_const n >>= infer_type >>= get_ext_subject, match add with | none := add_ext_lemma s n b | some add := equiv_type_constr s add >> add_ext_lemma add n b end } add_tactic_doc { name := "ext", category := doc_category.attr, decl_names := [`extensional_attribute], tags := ["rewrite", "logic"] } /-- When possible, `ext` lemmas are stated without a full set of arguments. As an example, for bundled homs `f`, `g`, and `of`, `f.comp of = g.comp of → f = g` is a better `ext` lemma than `(∀ x, f (of x) = g (of x)) → f = g`, as the former allows a second type-specific extensionality lemmas to be applied to `f.comp of = g.comp of`. If the domain of `of` is `ℕ` or `ℤ` and `of` is a `ring_hom`, such a lemma could then make the goal `f (of 1) = g (of 1)`. For bundled morphisms, there is a `ext` lemma that always applies of the form `(∀ x, ⇑f x = ⇑g x) → f = g`. When adding type-specific `ext` lemmas like the one above, we want these to be tried first. This happens automatically since the type-specific lemmas are inevitably defined later. -/ library_note "partially-applied ext lemmas" -- We mark some existing extensionality lemmas. attribute [ext] array.ext propext function.hfunext attribute [ext thunk] _root_.funext -- This line is equivalent to: -- attribute [ext (→)] _root_.funext -- but (→) is not actually a binary relation with a constant at the head, -- so we use the special name [anon].0 to represent (→). run_cmd add_ext_lemma (name.mk_numeral 0 name.anonymous) ``_root_.funext tt -- We create some extensionality lemmas for existing structures. attribute [ext] ulift namespace plift -- This is stronger than the one generated automatically. @[ext] lemma ext {P : Prop} (a b : plift P) : a = b := begin cases a, cases b, refl end end plift -- Conservatively, we'll only add extensionality lemmas for `has_*` structures -- as they become useful. attribute [ext] has_zero @[ext] lemma unit.ext {x y : unit} : x = y := by { cases x, cases y, refl, } @[ext] lemma punit.ext {x y : punit} : x = y := by { cases x, cases y, refl, } namespace tactic /-- Helper structure for `ext` and `ext1`. `lemmas` keeps track of extensionality lemmas applied so far. -/ meta structure ext_state : Type := (patts : list rcases_patt := []) (trace_msg : list string := []) (fuel : option ℕ := none) /-- Helper function for `try_intros`. Additionally populates the `trace_msg` field of `ext_state`. -/ private meta def try_intros_core : state_t ext_state tactic unit := do ⟨patts, trace_msg, fuel⟩ ← get, match patts with | [] := do { es ← state_t.lift intros, when (es.length > 0) $ do let msg := "intros " ++ (" ".intercalate (es.map (λ e, e.local_pp_name.to_string))), modify (λ ⟨patts, trace_msg, fuel⟩, ⟨patts, trace_msg ++ [msg], fuel⟩) } <|> pure () | (x::xs) := do tgt ← state_t.lift (target >>= whnf), when tgt.is_pi $ do state_t.lift (rintro [x]), msg ← state_t.lift (((++) "rintro ") <$> format.to_string <$> x.format ff), modify (λ ⟨_, trace_msg, fuel⟩, ⟨xs, trace_msg ++ [msg], fuel⟩), try_intros_core end /-- Try to introduce as many arguments as possible, using the given patterns to destruct the introduced variables. Returns the unused patterns. -/ meta def try_intros (patts : list rcases_patt) : tactic (list rcases_patt) := let σ := ext_state.mk patts [] none in (ext_state.patts ∘ prod.snd) <$> state_t.run try_intros_core σ /-- Apply one extensionality lemma, and destruct the arguments using the patterns in the ext_state. -/ meta def ext1_core (cfg : apply_cfg := {}) : state_t ext_state tactic unit := do ⟨patts, trace_msg, _⟩ ← get, (new_msgs) ← state_t.lift $ focus1 $ do { m ← get_ext_lemmas, tgt ← target, when_tracing `ext $ trace!"[ext] goal: {tgt}", subject ← get_ext_subject tgt, new_trace_msg ← do { rule ← (m.find subject), if is_trace_enabled_for `ext then trace!"[ext] matched goal to rule: {rule}" >> timetac "[ext] application attempt time" (applyc rule cfg) else applyc rule cfg, pure (["apply " ++ rule.to_string]) } <|> do { ls ← get_ext_lemma_names, let nms := ls.map name.to_string, rule ← (ls.any_of (λ n, (if is_trace_enabled_for `ext then trace!"[ext] trying to apply ext lemma: {n}" >> timetac "[ext] application attempt time" (applyc n cfg) else applyc n cfg) *> pure n)), pure (["apply " ++ rule.to_string]) } <|> (fail format!"no applicable extensionality rule found for {subject}"), pure new_trace_msg }, modify (λ ⟨patts, trace_msg, fuel⟩, ⟨patts, trace_msg ++ new_msgs, fuel⟩), try_intros_core /-- Apply multiple extensionality lemmas, destructing the arguments using the given patterns. -/ meta def ext_core (cfg : apply_cfg := {}) : state_t ext_state tactic unit := do acc@⟨_, _, fuel⟩ ← get, match fuel with | (some 0) := pure () | n := do { ext1_core cfg, modify (λ ⟨patts, lemmas, _⟩, ⟨patts, lemmas, nat.pred <$> n⟩), ext_core <|> pure () } end /-- Apply one extensionality lemma, and destruct the arguments using the given patterns. Returns the unused patterns. -/ meta def ext1 (xs : list rcases_patt) (cfg : apply_cfg := {}) (trace : bool := ff) : tactic (list rcases_patt) := do ⟨_, σ⟩ ← state_t.run (ext1_core cfg) {patts := xs}, when trace $ tactic.trace $ "Try this: " ++ ", ".intercalate σ.trace_msg, pure σ.patts /-- Apply multiple extensionality lemmas, destructing the arguments using the given patterns. `ext ps (some n)` applies at most `n` extensionality lemmas. Returns the unused patterns. -/ meta def ext (xs : list rcases_patt) (fuel : option ℕ) (cfg : apply_cfg := {}) (trace : bool := ff) : tactic (list rcases_patt) := do ⟨_, σ⟩ ← state_t.run (ext_core cfg) {patts := xs, fuel := fuel}, when trace $ tactic.trace $ "Try this: " ++ ", ".intercalate σ.trace_msg, pure σ.patts local postfix (name := parser.optional) `?`:9001 := optional local postfix (name := parser.many) *:9001 := many /-- `ext1 id` selects and apply one extensionality lemma (with attribute `ext`), using `id`, if provided, to name a local constant introduced by the lemma. If `id` is omitted, the local constant is named automatically, as per `intro`. Placing a `?` after `ext1` (e.g. `ext1? i ⟨a,b⟩ : 3`) will display a sequence of tactic applications that can replace the call to `ext1`. -/ meta def interactive.ext1 (trace : parse (tk "?")?) (xs : parse rcases_patt_parse_hi*) : tactic unit := ext1 xs {} trace.is_some $> () /-- - `ext` applies as many extensionality lemmas as possible; - `ext ids`, with `ids` a list of identifiers, finds extentionality and applies them until it runs out of identifiers in `ids` to name the local constants. - `ext` can also be given an `rcases` pattern in place of an identifier. This will destruct the introduced local constant. - Placing a `?` after `ext` (e.g. `ext? i ⟨a,b⟩ : 3`) will display a sequence of tactic applications that can replace the call to `ext`. - `set_option trace.ext true` will trace every attempted lemma application, along with the time it takes for the application to succeed or fail. This is useful for debugging slow `ext` calls. When trying to prove: ```lean α β : Type, f g : α → set β ⊢ f = g ``` applying `ext x y` yields: ```lean α β : Type, f g : α → set β, x : α, y : β ⊢ y ∈ f x ↔ y ∈ f x ``` by applying functional extensionality and set extensionality. When trying to prove: ```lean α β γ : Type f g : α × β → γ ⊢ f = g ``` applying `ext ⟨a, b⟩` yields: ```lean α β γ : Type, f g : α × β → γ, a : α, b : β ⊢ f (a, b) = g (a, b) ``` by applying functional extensionality and destructing the introduced pair. In the previous example, applying `ext? ⟨a,b⟩` will produce the trace message: ```lean Try this: apply funext, rintro ⟨a, b⟩ ``` A maximum depth can be provided with `ext x y z : 3`. -/ meta def interactive.ext : (parse $ (tk "?")?) → parse rintro_patt_parse_hi* → parse (tk ":" *> small_nat)? → tactic unit | trace [] (some n) := iterate_range 1 n (ext1 [] {} trace.is_some $> ()) | trace [] none := repeat1 (ext1 [] {} trace.is_some $> ()) | trace xs n := ext xs.join n {} trace.is_some $> () /-- * `ext1 id` selects and apply one extensionality lemma (with attribute `ext`), using `id`, if provided, to name a local constant introduced by the lemma. If `id` is omitted, the local constant is named automatically, as per `intro`. * `ext` applies as many extensionality lemmas as possible; * `ext ids`, with `ids` a list of identifiers, finds extensionality lemmas and applies them until it runs out of identifiers in `ids` to name the local constants. * `ext` can also be given an `rcases` pattern in place of an identifier. This will destruct the introduced local constant. - Placing a `?` after `ext`/`ext1` (e.g. `ext? i ⟨a,b⟩ : 3`) will display a sequence of tactic applications that can replace the call to `ext`/`ext1`. - `set_option trace.ext true` will trace every attempted lemma application, along with the time it takes for the application to succeed or fail. This is useful for debugging slow `ext` calls. When trying to prove: ```lean α β : Type, f g : α → set β ⊢ f = g ``` applying `ext x y` yields: ```lean α β : Type, f g : α → set β, x : α, y : β ⊢ y ∈ f x ↔ y ∈ g x ``` by applying functional extensionality and set extensionality. When trying to prove: ```lean α β γ : Type f g : α × β → γ ⊢ f = g ``` applying `ext ⟨a, b⟩` yields: ```lean α β γ : Type, f g : α × β → γ, a : α, b : β ⊢ f (a, b) = g (a, b) ``` by applying functional extensionality and destructing the introduced pair. In the previous example, applying `ext? ⟨a,b⟩` will produce the trace message: ```lean Try this: apply funext, rintro ⟨a, b⟩ ``` A maximum depth can be provided with `ext x y z : 3`. -/ add_tactic_doc { name := "ext1 / ext", category := doc_category.tactic, decl_names := [`tactic.interactive.ext1, `tactic.interactive.ext], tags := ["rewriting", "logic"] } end tactic end ext
b6cbe54f7dc2661d920586d6af72c56d2f83f219
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/basics/unnamed_1515.lean
0c45d1413a9b19821a5734adecb027d794b2b35b
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
263
lean
import data.real.basic variables a b : ℝ -- BEGIN example : min a b = min b a := begin have h : ∀ x y, min x y ≤ min y x, { intros x y, apply le_min, apply min_le_right, apply min_le_left }, apply le_antisymm, apply h, apply h end -- END
eb64295a672c218d71fd42397204759d3076740d
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/hom_functor_auto.lean
29edd0b208bc4f4c8e295f78dbf78e6a8acab98b
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,203
lean
/- Copyright (c) 2018 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.products.basic import Mathlib.PostPort universes u v namespace Mathlib /-! The hom functor, sending `(X, Y)` to the type `X ⟶ Y`. -/ namespace category_theory.functor /-- `functor.hom` is the hom-pairing, sending (X,Y) to X → Y, contravariant in X and covariant in Y. -/ def hom (C : Type u) [category C] : Cᵒᵖ × C ⥤ Type v := mk (fun (p : Cᵒᵖ × C) => opposite.unop (prod.fst p) ⟶ prod.snd p) fun (X Y : Cᵒᵖ × C) (f : X ⟶ Y) (h : opposite.unop (prod.fst X) ⟶ prod.snd X) => has_hom.hom.unop (prod.fst f) ≫ h ≫ prod.snd f @[simp] theorem hom_obj (C : Type u) [category C] (X : Cᵒᵖ × C) : obj (hom C) X = (opposite.unop (prod.fst X) ⟶ prod.snd X) := rfl @[simp] theorem hom_pairing_map (C : Type u) [category C] {X : Cᵒᵖ × C} {Y : Cᵒᵖ × C} (f : X ⟶ Y) : map (hom C) f = fun (h : obj (hom C) X) => has_hom.hom.unop (prod.fst f) ≫ h ≫ prod.snd f := rfl end Mathlib
680a69bcea387f2c6a0e2bcc5cd94fa16a83bac6
76ce87faa6bc3c2aa9af5962009e01e04f2a074a
/AA_Algebras/00_intro.lean
e0bc2230d73a15f56f7c41dc28039287b279ba5f
[]
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
932
lean
/- An algebra is a set, A, of values of some type, along with a number of functions, f_i, that are closed over A. By the phrase, closed over A, we mean that each such functions takes values in A as its arguments and returns values in A as its results. In this unit, we make this concept clear through the study of two important examples: Boolean algebra and the arithmetic of natural numbers. We characterize each algebra by first defining its set, A, of values, and by then defining a set of functions that are closed on that particular set. In the course of studying these two simple algebras, for which you already have strong intuition, we will encounter two of the most interesting and useful ideas in all of computer science and discrete mathematics: the inductive definition of sets of values, which we will call types, and the recursive definition of functions taking and returning values of such types. -/
9bb90f2ebe4a64bb464ecfd04bf2be2b83b171e7
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/tactic/omega/nat/preterm.lean
b55af2eb5857745241d7311a854a8451f86dfc63
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
4,968
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Seul Baek -/ /- Linear natural number arithmetic terms in pre-normalized form. -/ import tactic.omega.term open tactic namespace omega namespace nat /-- The shadow syntax for arithmetic terms. All constants are reified to `cst` (e.g., `5` is reified to `cst 5`) and all other atomic terms are reified to `exp` (e.g., `5 * (list.length l)` is reified to `exp 5 \`(list.length l)`). `exp` accepts a coefficient of type `nat` as its first argument because multiplication by constant is allowed by the omega test. -/ meta inductive exprterm : Type | cst : nat → exprterm | exp : nat → expr → exprterm | add : exprterm → exprterm → exprterm | sub : exprterm → exprterm → exprterm /-- Similar to `exprterm`, except that all exprs are now replaced with de Brujin indices of type `nat`. This is akin to generalizing over the terms represented by the said exprs. -/ @[derive has_reflect, derive decidable_eq, derive inhabited] inductive preterm : Type | cst : nat → preterm | var : nat → nat → preterm | add : preterm → preterm → preterm | sub : preterm → preterm → preterm localized "notation (name := preterm.cst) `&` k := omega.nat.preterm.cst k" in omega.nat localized "infix (name := preterm.var) ` ** `:300 := omega.nat.preterm.var" in omega.nat localized "notation (name := preterm.add) t ` +* ` s := omega.nat.preterm.add t s" in omega.nat localized "notation (name := preterm.sub) t ` -* ` s := omega.nat.preterm.sub t s" in omega.nat namespace preterm /-- Helper tactic for proof by induction over preterms -/ meta def induce (tac : tactic unit := tactic.skip) : tactic unit := `[ intro t, induction t with m m n t s iht ihs t s iht ihs; tac] /-- Preterm evaluation -/ def val (v : nat → nat) : preterm → nat | (& i) := i | (i ** n) := if i = 1 then v n else (v n) * i | (t1 +* t2) := t1.val + t2.val | (t1 -* t2) := t1.val - t2.val @[simp] lemma val_const {v : nat → nat} {m : nat} : (& m).val v = m := rfl @[simp] lemma val_var {v : nat → nat} {m n : nat} : (m ** n).val v = m * (v n) := begin simp only [val], by_cases h1 : m = 1, rw [if_pos h1, h1, one_mul], rw [if_neg h1, mul_comm], end @[simp] lemma val_add {v : nat → nat} {t s : preterm} : (t +* s).val v = t.val v + s.val v := rfl @[simp] lemma val_sub {v : nat → nat} {t s : preterm} : (t -* s).val v = t.val v - s.val v := rfl /-- Fresh de Brujin index not used by any variable in argument -/ def fresh_index : preterm → nat | (& _) := 0 | (i ** n) := n + 1 | (t1 +* t2) := max t1.fresh_index t2.fresh_index | (t1 -* t2) := max t1.fresh_index t2.fresh_index /-- If variable assignments `v` and `w` agree on all variables that occur in term `t`, the value of `t` under `v` and `w` are identical. -/ lemma val_constant (v w : nat → nat) : ∀ t : preterm, (∀ x < t.fresh_index, v x = w x) → t.val v = t.val w | (& n) h1 := rfl | (m ** n) h1 := begin simp only [val_var], apply congr_arg (λ y, m * y), apply h1 _ (lt_add_one _) end | (t +* s) h1 := begin simp only [val_add], have ht := val_constant t (λ x hx, h1 _ (lt_of_lt_of_le hx (le_max_left _ _))), have hs := val_constant s (λ x hx, h1 _ (lt_of_lt_of_le hx (le_max_right _ _))), rw [ht, hs] end | (t -* s) h1 := begin simp only [val_sub], have ht := val_constant t (λ x hx, h1 _ (lt_of_lt_of_le hx (le_max_left _ _))), have hs := val_constant s (λ x hx, h1 _ (lt_of_lt_of_le hx (le_max_right _ _))), rw [ht, hs] end def repr : preterm → string | (& i) := i.repr | (i ** n) := i.repr ++ "*x" ++ n.repr | (t1 +* t2) := "(" ++ t1.repr ++ " + " ++ t2.repr ++ ")" | (t1 -* t2) := "(" ++ t1.repr ++ " - " ++ t2.repr ++ ")" @[simp] def add_one (t : preterm) : preterm := t +* (&1) /-- Preterm is free of subtractions -/ def sub_free : preterm → Prop | (& m) := true | (m ** n) := true | (t +* s) := t.sub_free ∧ s.sub_free | (_ -* _) := false end preterm open_locale list.func -- get notation for list.func.set /-- Return a term (which is in canonical form by definition) that is equivalent to the input preterm -/ @[simp] def canonize : preterm → term | (& m) := ⟨↑m, []⟩ | (m ** n) := ⟨0, [] {n ↦ ↑m}⟩ | (t +* s) := term.add (canonize t) (canonize s) | (_ -* _) := ⟨0, []⟩ @[simp] lemma val_canonize {v : nat → nat} : ∀ {t : preterm}, t.sub_free → (canonize t).val (λ x, ↑(v x)) = t.val v | (& i) h1 := by simp only [canonize, preterm.val_const, term.val, coeffs.val_nil, add_zero] | (i ** n) h1 := by simp only [preterm.val_var, coeffs.val_set, term.val, zero_add, int.coe_nat_mul, canonize] | (t +* s) h1 := by simp only [val_canonize h1.left, val_canonize h1.right, int.coe_nat_add, canonize, term.val_add, preterm.val_add] end nat end omega
af3c26b520bf8bb4e5821c59c0726b96044c7027
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/topology/subset_properties.lean
d9ba24d767ac8ecc2560d90958996da3d2c08da5
[ "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
70,789
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, Yury Kudryashov -/ import topology.bases import data.finset.order import data.set.accumulate import tactic.tfae /-! # Properties of subsets of topological spaces In this file we define various properties of subsets of a topological space, and some classes on topological spaces. ## Main definitions We define the following properties for sets in a topological space: * `is_compact`: each open cover has a finite subcover. This is defined in mathlib using filters. The main property of a compact set is `is_compact.elim_finite_subcover`. * `is_clopen`: a set that is both open and closed. * `is_irreducible`: a nonempty set that has contains no non-trivial pair of disjoint opens. See also the section below in the module doc. For each of these definitions (except for `is_clopen`), we also have a class stating that the whole space satisfies that property: `compact_space`, `irreducible_space` Furthermore, we have three more classes: * `locally_compact_space`: for every point `x`, every open neighborhood of `x` contains a compact neighborhood of `x`. The definition is formulated in terms of the neighborhood filter. * `sigma_compact_space`: a space that is the union of a countably many compact subspaces; * `noncompact_space`: a space that is not a compact space. ## On the definition of irreducible and connected sets/spaces In informal mathematics, irreducible spaces are assumed to be nonempty. We formalise the predicate without that assumption as `is_preirreducible`. In other words, the only difference is whether the empty space counts as irreducible. There are good reasons to consider the empty space to be “too simple to be simple” See also https://ncatlab.org/nlab/show/too+simple+to+be+simple, and in particular https://ncatlab.org/nlab/show/too+simple+to+be+simple#relationship_to_biased_definitions. -/ open set filter classical topological_space open_locale classical topological_space filter universes u v variables {α : Type u} {β : Type v} [topological_space α] {s t : set α} /- compact sets -/ section compact /-- A set `s` is compact if for every nontrivial filter `f` that contains `s`, there exists `a ∈ s` such that every set of `f` meets every neighborhood of `a`. -/ def is_compact (s : set α) := ∀ ⦃f⦄ [ne_bot f], f ≤ 𝓟 s → ∃a∈s, cluster_pt a f /-- The complement to a compact set belongs to a filter `f` if it belongs to each filter `𝓝 a ⊓ f`, `a ∈ s`. -/ lemma is_compact.compl_mem_sets (hs : is_compact s) {f : filter α} (hf : ∀ a ∈ s, sᶜ ∈ 𝓝 a ⊓ f) : sᶜ ∈ f := begin contrapose! hf, simp only [not_mem_iff_inf_principal_compl, compl_compl, inf_assoc, ← exists_prop] at hf ⊢, exact @hs _ hf inf_le_right end /-- The complement to a compact set belongs to a filter `f` if each `a ∈ s` has a neighborhood `t` within `s` such that `tᶜ` belongs to `f`. -/ lemma is_compact.compl_mem_sets_of_nhds_within (hs : is_compact s) {f : filter α} (hf : ∀ a ∈ s, ∃ t ∈ 𝓝[s] a, tᶜ ∈ f) : sᶜ ∈ f := begin refine hs.compl_mem_sets (λ a ha, _), rcases hf a ha with ⟨t, ht, hst⟩, replace ht := mem_inf_principal.1 ht, apply mem_inf_of_inter ht hst, rintros x ⟨h₁, h₂⟩ hs, exact h₂ (h₁ hs) end /-- If `p : set α → Prop` is stable under restriction and union, and each point `x` of a compact set `s` has a neighborhood `t` within `s` such that `p t`, then `p s` holds. -/ @[elab_as_eliminator] lemma is_compact.induction_on {s : set α} (hs : is_compact s) {p : set α → Prop} (he : p ∅) (hmono : ∀ ⦃s t⦄, s ⊆ t → p t → p s) (hunion : ∀ ⦃s t⦄, p s → p t → p (s ∪ t)) (hnhds : ∀ x ∈ s, ∃ t ∈ 𝓝[s] x, p t) : p s := let f : filter α := { sets := {t | p tᶜ}, univ_sets := by simpa, sets_of_superset := λ t₁ t₂ ht₁ ht, hmono (compl_subset_compl.2 ht) ht₁, inter_sets := λ t₁ t₂ ht₁ ht₂, by simp [compl_inter, hunion ht₁ ht₂] } in have sᶜ ∈ f, from hs.compl_mem_sets_of_nhds_within (by simpa using hnhds), by simpa /-- The intersection of a compact set and a closed set is a compact set. -/ lemma is_compact.inter_right (hs : is_compact s) (ht : is_closed t) : is_compact (s ∩ t) := begin introsI f hnf hstf, obtain ⟨a, hsa, ha⟩ : ∃ a ∈ s, cluster_pt a f := hs (le_trans hstf (le_principal_iff.2 (inter_subset_left _ _))), have : a ∈ t := (ht.mem_of_nhds_within_ne_bot $ ha.mono $ le_trans hstf (le_principal_iff.2 (inter_subset_right _ _))), exact ⟨a, ⟨hsa, this⟩, ha⟩ end /-- The intersection of a closed set and a compact set is a compact set. -/ lemma is_compact.inter_left (ht : is_compact t) (hs : is_closed s) : is_compact (s ∩ t) := inter_comm t s ▸ ht.inter_right hs /-- The set difference of a compact set and an open set is a compact set. -/ lemma is_compact.diff (hs : is_compact s) (ht : is_open t) : is_compact (s \ t) := hs.inter_right (is_closed_compl_iff.mpr ht) /-- A closed subset of a compact set is a compact set. -/ lemma compact_of_is_closed_subset (hs : is_compact s) (ht : is_closed t) (h : t ⊆ s) : is_compact t := inter_eq_self_of_subset_right h ▸ hs.inter_right ht lemma is_compact.adherence_nhdset {f : filter α} (hs : is_compact s) (hf₂ : f ≤ 𝓟 s) (ht₁ : is_open t) (ht₂ : ∀a∈s, cluster_pt a f → a ∈ t) : t ∈ f := classical.by_cases mem_of_eq_bot $ assume : f ⊓ 𝓟 tᶜ ≠ ⊥, let ⟨a, ha, (hfa : cluster_pt a $ f ⊓ 𝓟 tᶜ)⟩ := @@hs ⟨this⟩ $ inf_le_of_left_le hf₂ in have a ∈ t, from ht₂ a ha (hfa.of_inf_left), have tᶜ ∩ t ∈ 𝓝[tᶜ] a, from inter_mem_nhds_within _ (is_open.mem_nhds ht₁ this), have A : 𝓝[tᶜ] a = ⊥, from empty_mem_iff_bot.1 $ compl_inter_self t ▸ this, have 𝓝[tᶜ] a ≠ ⊥, from hfa.of_inf_right.ne, absurd A this lemma is_compact_iff_ultrafilter_le_nhds : is_compact s ↔ (∀f : ultrafilter α, ↑f ≤ 𝓟 s → ∃a∈s, ↑f ≤ 𝓝 a) := begin refine (forall_ne_bot_le_iff _).trans _, { rintro f g hle ⟨a, has, haf⟩, exact ⟨a, has, haf.mono hle⟩ }, { simp only [ultrafilter.cluster_pt_iff] } end alias is_compact_iff_ultrafilter_le_nhds ↔ is_compact.ultrafilter_le_nhds _ /-- For every open directed cover of a compact set, there exists a single element of the cover which itself includes the set. -/ lemma is_compact.elim_directed_cover {ι : Type v} [hι : nonempty ι] (hs : is_compact s) (U : ι → set α) (hUo : ∀i, is_open (U i)) (hsU : s ⊆ ⋃ i, U i) (hdU : directed (⊆) U) : ∃ i, s ⊆ U i := hι.elim $ λ i₀, is_compact.induction_on hs ⟨i₀, empty_subset _⟩ (λ s₁ s₂ hs ⟨i, hi⟩, ⟨i, subset.trans hs hi⟩) (λ s₁ s₂ ⟨i, hi⟩ ⟨j, hj⟩, let ⟨k, hki, hkj⟩ := hdU i j in ⟨k, union_subset (subset.trans hi hki) (subset.trans hj hkj)⟩) (λ x hx, let ⟨i, hi⟩ := mem_Union.1 (hsU hx) in ⟨U i, mem_nhds_within_of_mem_nhds (is_open.mem_nhds (hUo i) hi), i, subset.refl _⟩) /-- For every open cover of a compact set, there exists a finite subcover. -/ lemma is_compact.elim_finite_subcover {ι : Type v} (hs : is_compact s) (U : ι → set α) (hUo : ∀i, is_open (U i)) (hsU : s ⊆ ⋃ i, U i) : ∃ t : finset ι, s ⊆ ⋃ i ∈ t, U i := hs.elim_directed_cover _ (λ t, is_open_bUnion $ λ i _, hUo i) (Union_eq_Union_finset U ▸ hsU) (directed_of_sup $ λ t₁ t₂ h, bUnion_subset_bUnion_left h) lemma is_compact.elim_nhds_subcover' (hs : is_compact s) (U : Π x ∈ s, set α) (hU : ∀ x ∈ s, U x ‹x ∈ s› ∈ 𝓝 x) : ∃ t : finset s, s ⊆ ⋃ x ∈ t, U (x : s) x.2 := (hs.elim_finite_subcover (λ x : s, interior (U x x.2)) (λ x, is_open_interior) (λ x hx, mem_Union.2 ⟨⟨x, hx⟩, mem_interior_iff_mem_nhds.2 $ hU _ _⟩)).imp $ λ t ht, subset.trans ht $ bUnion_mono $ λ _ _, interior_subset lemma is_compact.elim_nhds_subcover (hs : is_compact s) (U : α → set α) (hU : ∀ x ∈ s, U x ∈ 𝓝 x) : ∃ t : finset α, (∀ x ∈ t, x ∈ s) ∧ s ⊆ ⋃ x ∈ t, U x := let ⟨t, ht⟩ := hs.elim_nhds_subcover' (λ x _, U x) hU in ⟨t.image coe, λ x hx, let ⟨y, hyt, hyx⟩ := finset.mem_image.1 hx in hyx ▸ y.2, by rwa finset.set_bUnion_finset_image⟩ /-- For every family of closed sets whose intersection avoids a compact set, there exists a finite subfamily whose intersection avoids this compact set. -/ lemma is_compact.elim_finite_subfamily_closed {s : set α} {ι : Type v} (hs : is_compact s) (Z : ι → set α) (hZc : ∀i, is_closed (Z i)) (hsZ : s ∩ (⋂ i, Z i) = ∅) : ∃ t : finset ι, s ∩ (⋂ i ∈ t, Z i) = ∅ := let ⟨t, ht⟩ := hs.elim_finite_subcover (λ i, (Z i)ᶜ) (λ i, (hZc i).is_open_compl) (by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, mem_Union, exists_prop, mem_inter_eq, not_and, iff_self, mem_Inter, mem_compl_eq] using hsZ) in ⟨t, by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, mem_Union, exists_prop, mem_inter_eq, not_and, iff_self, mem_Inter, mem_compl_eq] using ht⟩ /-- If `s` is a compact set in a topological space `α` and `f : ι → set α` is a locally finite family of sets, then `f i ∩ s` is nonempty only for a finitely many `i`. -/ lemma locally_finite.finite_nonempty_inter_compact {ι : Type*} {f : ι → set α} (hf : locally_finite f) {s : set α} (hs : is_compact s) : finite {i | (f i ∩ s).nonempty} := begin choose U hxU hUf using hf, rcases hs.elim_nhds_subcover U (λ x _, hxU x) with ⟨t, -, hsU⟩, refine (t.finite_to_set.bUnion (λ x _, hUf x)).subset _, rintro i ⟨x, hx⟩, rcases mem_bUnion_iff.1 (hsU hx.2) with ⟨c, hct, hcx⟩, exact mem_bUnion hct ⟨x, hx.1, hcx⟩ end /-- To show that a compact set intersects the intersection of a family of closed sets, it is sufficient to show that it intersects every finite subfamily. -/ lemma is_compact.inter_Inter_nonempty {s : set α} {ι : Type v} (hs : is_compact s) (Z : ι → set α) (hZc : ∀i, is_closed (Z i)) (hsZ : ∀ t : finset ι, (s ∩ ⋂ i ∈ t, Z i).nonempty) : (s ∩ ⋂ i, Z i).nonempty := begin simp only [← ne_empty_iff_nonempty] at hsZ ⊢, apply mt (hs.elim_finite_subfamily_closed Z hZc), push_neg, exact hsZ end /-- Cantor's intersection theorem: the intersection of a directed family of nonempty compact closed sets is nonempty. -/ lemma is_compact.nonempty_Inter_of_directed_nonempty_compact_closed {ι : Type v} [hι : nonempty ι] (Z : ι → set α) (hZd : directed (⊇) Z) (hZn : ∀ i, (Z i).nonempty) (hZc : ∀ i, is_compact (Z i)) (hZcl : ∀ i, is_closed (Z i)) : (⋂ i, Z i).nonempty := begin apply hι.elim, intro i₀, let Z' := λ i, Z i ∩ Z i₀, suffices : (⋂ i, Z' i).nonempty, { exact nonempty.mono (Inter_subset_Inter $ assume i, inter_subset_left (Z i) (Z i₀)) this }, rw ← ne_empty_iff_nonempty, intro H, obtain ⟨t, ht⟩ : ∃ (t : finset ι), ((Z i₀) ∩ ⋂ (i ∈ t), Z' i) = ∅, from (hZc i₀).elim_finite_subfamily_closed Z' (assume i, is_closed.inter (hZcl i) (hZcl i₀)) (by rw [H, inter_empty]), obtain ⟨i₁, hi₁⟩ : ∃ i₁ : ι, Z i₁ ⊆ Z i₀ ∧ ∀ i ∈ t, Z i₁ ⊆ Z' i, { rcases directed.finset_le hZd t with ⟨i, hi⟩, rcases hZd i i₀ with ⟨i₁, hi₁, hi₁₀⟩, use [i₁, hi₁₀], intros j hj, exact subset_inter (subset.trans hi₁ (hi j hj)) hi₁₀ }, suffices : ((Z i₀) ∩ ⋂ (i ∈ t), Z' i).nonempty, { rw ← ne_empty_iff_nonempty at this, contradiction }, refine nonempty.mono _ (hZn i₁), exact subset_inter hi₁.left (subset_bInter hi₁.right) end /-- Cantor's intersection theorem for sequences indexed by `ℕ`: the intersection of a decreasing sequence of nonempty compact closed sets is nonempty. -/ lemma is_compact.nonempty_Inter_of_sequence_nonempty_compact_closed (Z : ℕ → set α) (hZd : ∀ i, Z (i+1) ⊆ Z i) (hZn : ∀ i, (Z i).nonempty) (hZ0 : is_compact (Z 0)) (hZcl : ∀ i, is_closed (Z i)) : (⋂ i, Z i).nonempty := have Zmono : antitone Z := antitone_nat_of_succ_le hZd, have hZd : directed (⊇) Z, from directed_of_sup Zmono, have ∀ i, Z i ⊆ Z 0, from assume i, Zmono $ zero_le i, have hZc : ∀ i, is_compact (Z i), from assume i, compact_of_is_closed_subset hZ0 (hZcl i) (this i), is_compact.nonempty_Inter_of_directed_nonempty_compact_closed Z hZd hZn hZc hZcl /-- For every open cover of a compact set, there exists a finite subcover. -/ lemma is_compact.elim_finite_subcover_image {b : set β} {c : β → set α} (hs : is_compact s) (hc₁ : ∀i∈b, is_open (c i)) (hc₂ : s ⊆ ⋃i∈b, c i) : ∃b'⊆b, finite b' ∧ s ⊆ ⋃i∈b', c i := begin rcases hs.elim_finite_subcover (λ i, c i : b → set α) _ _ with ⟨d, hd⟩; [skip, simpa using hc₁, simpa using hc₂], refine ⟨↑(d.image coe), _, finset.finite_to_set _, _⟩, { simp }, { rwa [finset.coe_image, bUnion_image] } end /-- A set `s` is compact if for every family of closed sets whose intersection avoids `s`, there exists a finite subfamily whose intersection avoids `s`. -/ theorem is_compact_of_finite_subfamily_closed (h : Π {ι : Type u} (Z : ι → (set α)), (∀ i, is_closed (Z i)) → s ∩ (⋂ i, Z i) = ∅ → (∃ (t : finset ι), s ∩ (⋂ i ∈ t, Z i) = ∅)) : is_compact s := assume f hfn hfs, classical.by_contradiction $ assume : ¬ (∃x∈s, cluster_pt x f), have hf : ∀x∈s, 𝓝 x ⊓ f = ⊥, by simpa only [cluster_pt, not_exists, not_not, ne_bot_iff], have ¬ ∃x∈s, ∀t∈f.sets, x ∈ closure t, from assume ⟨x, hxs, hx⟩, have ∅ ∈ 𝓝 x ⊓ f, by rw [empty_mem_iff_bot, hf x hxs], let ⟨t₁, ht₁, t₂, ht₂, ht⟩ := by rw [mem_inf_iff] at this; exact this in have ∅ ∈ 𝓝[t₂] x, by { rw [ht, inter_comm], exact inter_mem_nhds_within _ ht₁ }, have 𝓝[t₂] x = ⊥, by rwa [empty_mem_iff_bot] at this, by simp only [closure_eq_cluster_pts] at hx; exact (hx t₂ ht₂).ne this, let ⟨t, ht⟩ := h (λ i : f.sets, closure i.1) (λ i, is_closed_closure) (by simpa [eq_empty_iff_forall_not_mem, not_exists]) in have (⋂i∈t, subtype.val i) ∈ f, from t.Inter_mem_sets.2 $ assume i hi, i.2, have s ∩ (⋂i∈t, subtype.val i) ∈ f, from inter_mem (le_principal_iff.1 hfs) this, have ∅ ∈ f, from mem_of_superset this $ assume x ⟨hxs, hx⟩, let ⟨i, hit, hxi⟩ := (show ∃i ∈ t, x ∉ closure (subtype.val i), by { rw [eq_empty_iff_forall_not_mem] at ht, simpa [hxs, not_forall] using ht x }) in have x ∈ closure i.val, from subset_closure (mem_bInter_iff.mp hx i hit), show false, from hxi this, hfn.ne $ by rwa [empty_mem_iff_bot] at this /-- A set `s` is compact if for every open cover of `s`, there exists a finite subcover. -/ lemma is_compact_of_finite_subcover (h : Π {ι : Type u} (U : ι → (set α)), (∀ i, is_open (U i)) → s ⊆ (⋃ i, U i) → (∃ (t : finset ι), s ⊆ (⋃ i ∈ t, U i))) : is_compact s := is_compact_of_finite_subfamily_closed $ assume ι Z hZc hsZ, let ⟨t, ht⟩ := h (λ i, (Z i)ᶜ) (assume i, is_open_compl_iff.mpr $ hZc i) (by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, mem_Union, exists_prop, mem_inter_eq, not_and, iff_self, mem_Inter, mem_compl_eq] using hsZ) in ⟨t, by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, mem_Union, exists_prop, mem_inter_eq, not_and, iff_self, mem_Inter, mem_compl_eq] using ht⟩ /-- A set `s` is compact if and only if for every open cover of `s`, there exists a finite subcover. -/ lemma is_compact_iff_finite_subcover : is_compact s ↔ (Π {ι : Type u} (U : ι → (set α)), (∀ i, is_open (U i)) → s ⊆ (⋃ i, U i) → (∃ (t : finset ι), s ⊆ (⋃ i ∈ t, U i))) := ⟨assume hs ι, hs.elim_finite_subcover, is_compact_of_finite_subcover⟩ /-- A set `s` is compact if and only if for every family of closed sets whose intersection avoids `s`, there exists a finite subfamily whose intersection avoids `s`. -/ theorem is_compact_iff_finite_subfamily_closed : is_compact s ↔ (Π {ι : Type u} (Z : ι → (set α)), (∀ i, is_closed (Z i)) → s ∩ (⋂ i, Z i) = ∅ → (∃ (t : finset ι), s ∩ (⋂ i ∈ t, Z i) = ∅)) := ⟨assume hs ι, hs.elim_finite_subfamily_closed, is_compact_of_finite_subfamily_closed⟩ @[simp] lemma is_compact_empty : is_compact (∅ : set α) := assume f hnf hsf, not.elim hnf.ne $ empty_mem_iff_bot.1 $ le_principal_iff.1 hsf @[simp] lemma is_compact_singleton {a : α} : is_compact ({a} : set α) := λ f hf hfa, ⟨a, rfl, cluster_pt.of_le_nhds' (hfa.trans $ by simpa only [principal_singleton] using pure_le_nhds a) hf⟩ lemma set.subsingleton.is_compact {s : set α} (hs : s.subsingleton) : is_compact s := subsingleton.induction_on hs is_compact_empty $ λ x, is_compact_singleton lemma set.finite.compact_bUnion {s : set β} {f : β → set α} (hs : finite s) (hf : ∀i ∈ s, is_compact (f i)) : is_compact (⋃i ∈ s, f i) := is_compact_of_finite_subcover $ assume ι U hUo hsU, have ∀i : subtype s, ∃t : finset ι, f i ⊆ (⋃ j ∈ t, U j), from assume ⟨i, hi⟩, (hf i hi).elim_finite_subcover _ hUo (calc f i ⊆ ⋃i ∈ s, f i : subset_bUnion_of_mem hi ... ⊆ ⋃j, U j : hsU), let ⟨finite_subcovers, h⟩ := axiom_of_choice this in by haveI : fintype (subtype s) := hs.fintype; exact let t := finset.bUnion finset.univ finite_subcovers in have (⋃i ∈ s, f i) ⊆ (⋃ i ∈ t, U i), from bUnion_subset $ assume i hi, calc f i ⊆ (⋃ j ∈ finite_subcovers ⟨i, hi⟩, U j) : (h ⟨i, hi⟩) ... ⊆ (⋃ j ∈ t, U j) : bUnion_subset_bUnion_left $ assume j hj, finset.mem_bUnion.mpr ⟨_, finset.mem_univ _, hj⟩, ⟨t, this⟩ lemma finset.compact_bUnion (s : finset β) {f : β → set α} (hf : ∀i ∈ s, is_compact (f i)) : is_compact (⋃i ∈ s, f i) := s.finite_to_set.compact_bUnion hf lemma compact_accumulate {K : ℕ → set α} (hK : ∀ n, is_compact (K n)) (n : ℕ) : is_compact (accumulate K n) := (finite_le_nat n).compact_bUnion $ λ k _, hK k lemma compact_Union {f : β → set α} [fintype β] (h : ∀i, is_compact (f i)) : is_compact (⋃i, f i) := by rw ← bUnion_univ; exact finite_univ.compact_bUnion (λ i _, h i) lemma set.finite.is_compact (hs : finite s) : is_compact s := bUnion_of_singleton s ▸ hs.compact_bUnion (λ _ _, is_compact_singleton) lemma finite_of_is_compact_of_discrete [discrete_topology α] (s : set α) (hs : is_compact s) : s.finite := begin have := hs.elim_finite_subcover (λ x : α, ({x} : set α)) (λ x, is_open_discrete _), simp only [set.subset_univ, forall_prop_of_true, set.Union_of_singleton] at this, rcases this with ⟨t, ht⟩, suffices : (⋃ (i : α) (H : i ∈ t), {i} : set α) = (t : set α), { rw this at ht, exact t.finite_to_set.subset ht }, ext x, simp only [exists_prop, set.mem_Union, set.mem_singleton_iff, exists_eq_right', finset.mem_coe] end lemma is_compact.union (hs : is_compact s) (ht : is_compact t) : is_compact (s ∪ t) := by rw union_eq_Union; exact compact_Union (λ b, by cases b; assumption) lemma is_compact.insert (hs : is_compact s) (a) : is_compact (insert a s) := is_compact_singleton.union hs /-- If `V : ι → set α` is a decreasing family of closed compact sets then any neighborhood of `⋂ i, V i` contains some `V i`. We assume each `V i` is compact *and* closed because `α` is not assumed to be Hausdorff. See `exists_subset_nhd_of_compact` for version assuming this. -/ lemma exists_subset_nhd_of_compact' {ι : Type*} [nonempty ι] {V : ι → set α} (hV : directed (⊇) V) (hV_cpct : ∀ i, is_compact (V i)) (hV_closed : ∀ i, is_closed (V i)) {U : set α} (hU : ∀ x ∈ ⋂ i, V i, U ∈ 𝓝 x) : ∃ i, V i ⊆ U := begin set Y := ⋂ i, V i, obtain ⟨W, hsubW, W_op, hWU⟩ : ∃ W, Y ⊆ W ∧ is_open W ∧ W ⊆ U, from exists_open_set_nhds hU, suffices : ∃ i, V i ⊆ W, { rcases this with ⟨i, hi⟩, refine ⟨i, set.subset.trans hi hWU⟩ }, by_contradiction H, push_neg at H, replace H : ∀ i, (V i ∩ Wᶜ).nonempty := λ i, set.inter_compl_nonempty_iff.mpr (H i), have : (⋂ i, V i ∩ Wᶜ).nonempty, { apply is_compact.nonempty_Inter_of_directed_nonempty_compact_closed _ _ H, { intro i, exact (hV_cpct i).inter_right W_op.is_closed_compl }, { intro i, apply (hV_closed i).inter W_op.is_closed_compl }, { intros i j, rcases hV i j with ⟨k, hki, hkj⟩, use k, split ; intro x ; simp only [and_imp, mem_inter_eq, mem_compl_eq] ; tauto } }, have : ¬ (⋂ (i : ι), V i) ⊆ W, by simpa [← Inter_inter, inter_compl_nonempty_iff], contradiction end namespace filter /-- `filter.cocompact` is the filter generated by complements to compact sets. -/ def cocompact (α : Type*) [topological_space α] : filter α := ⨅ (s : set α) (hs : is_compact s), 𝓟 (sᶜ) lemma has_basis_cocompact : (cocompact α).has_basis is_compact compl := has_basis_binfi_principal' (λ s hs t ht, ⟨s ∪ t, hs.union ht, compl_subset_compl.2 (subset_union_left s t), compl_subset_compl.2 (subset_union_right s t)⟩) ⟨∅, is_compact_empty⟩ lemma mem_cocompact : s ∈ cocompact α ↔ ∃ t, is_compact t ∧ tᶜ ⊆ s := has_basis_cocompact.mem_iff.trans $ exists_congr $ λ t, exists_prop lemma mem_cocompact' : s ∈ cocompact α ↔ ∃ t, is_compact t ∧ sᶜ ⊆ t := mem_cocompact.trans $ exists_congr $ λ t, and_congr_right $ λ ht, compl_subset_comm lemma _root_.is_compact.compl_mem_cocompact (hs : is_compact s) : sᶜ ∈ filter.cocompact α := has_basis_cocompact.mem_of_mem hs /-- `filter.coclosed_compact` is the filter generated by complements to closed compact sets. In a Hausdorff space, this is the same as `filter.cocompact`. -/ def coclosed_compact (α : Type*) [topological_space α] : filter α := ⨅ (s : set α) (h₁ : is_closed s) (h₂ : is_compact s), 𝓟 (sᶜ) lemma has_basis_coclosed_compact : (filter.coclosed_compact α).has_basis (λ s, is_closed s ∧ is_compact s) compl := begin simp only [filter.coclosed_compact, infi_and'], refine has_basis_binfi_principal' _ ⟨∅, is_closed_empty, is_compact_empty⟩, rintro s ⟨hs₁, hs₂⟩ t ⟨ht₁, ht₂⟩, exact ⟨s ∪ t, ⟨⟨hs₁.union ht₁, hs₂.union ht₂⟩, compl_subset_compl.2 (subset_union_left _ _), compl_subset_compl.2 (subset_union_right _ _)⟩⟩ end lemma mem_coclosed_compact : s ∈ coclosed_compact α ↔ ∃ t, is_closed t ∧ is_compact t ∧ tᶜ ⊆ s := by simp [has_basis_coclosed_compact.mem_iff, and_assoc] lemma mem_coclosed_compact' : s ∈ coclosed_compact α ↔ ∃ t, is_closed t ∧ is_compact t ∧ sᶜ ⊆ t := by simp only [mem_coclosed_compact, compl_subset_comm] lemma cocompact_le_coclosed_compact : cocompact α ≤ coclosed_compact α := infi_le_infi $ λ s, le_infi $ λ _, le_rfl end filter section tube_lemma variables [topological_space β] /-- `nhds_contain_boxes s t` means that any open neighborhood of `s × t` in `α × β` includes a product of an open neighborhood of `s` by an open neighborhood of `t`. -/ def nhds_contain_boxes (s : set α) (t : set β) : Prop := ∀ (n : set (α × β)) (hn : is_open n) (hp : set.prod s t ⊆ n), ∃ (u : set α) (v : set β), is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ set.prod u v ⊆ n lemma nhds_contain_boxes.symm {s : set α} {t : set β} : nhds_contain_boxes s t → nhds_contain_boxes t s := assume H n hn hp, let ⟨u, v, uo, vo, su, tv, p⟩ := H (prod.swap ⁻¹' n) (hn.preimage continuous_swap) (by rwa [←image_subset_iff, image_swap_prod]) in ⟨v, u, vo, uo, tv, su, by rwa [←image_subset_iff, image_swap_prod] at p⟩ lemma nhds_contain_boxes.comm {s : set α} {t : set β} : nhds_contain_boxes s t ↔ nhds_contain_boxes t s := iff.intro nhds_contain_boxes.symm nhds_contain_boxes.symm lemma nhds_contain_boxes_of_singleton {x : α} {y : β} : nhds_contain_boxes ({x} : set α) ({y} : set β) := assume n hn hp, let ⟨u, v, uo, vo, xu, yv, hp'⟩ := is_open_prod_iff.mp hn x y (hp $ by simp) in ⟨u, v, uo, vo, by simpa, by simpa, hp'⟩ lemma nhds_contain_boxes_of_compact {s : set α} (hs : is_compact s) (t : set β) (H : ∀ x ∈ s, nhds_contain_boxes ({x} : set α) t) : nhds_contain_boxes s t := assume n hn hp, have ∀x : subtype s, ∃uv : set α × set β, is_open uv.1 ∧ is_open uv.2 ∧ {↑x} ⊆ uv.1 ∧ t ⊆ uv.2 ∧ set.prod uv.1 uv.2 ⊆ n, from assume ⟨x, hx⟩, have set.prod {x} t ⊆ n, from subset.trans (prod_mono (by simpa) (subset.refl _)) hp, let ⟨ux,vx,H1⟩ := H x hx n hn this in ⟨⟨ux,vx⟩,H1⟩, let ⟨uvs, h⟩ := classical.axiom_of_choice this in have us_cover : s ⊆ ⋃i, (uvs i).1, from assume x hx, subset_Union _ ⟨x,hx⟩ (by simpa using (h ⟨x,hx⟩).2.2.1), let ⟨s0, s0_cover⟩ := hs.elim_finite_subcover _ (λi, (h i).1) us_cover in let u := ⋃(i ∈ s0), (uvs i).1 in let v := ⋂(i ∈ s0), (uvs i).2 in have is_open u, from is_open_bUnion (λi _, (h i).1), have is_open v, from is_open_bInter s0.finite_to_set (λi _, (h i).2.1), have t ⊆ v, from subset_bInter (λi _, (h i).2.2.2.1), have set.prod u v ⊆ n, from assume ⟨x',y'⟩ ⟨hx',hy'⟩, have ∃i ∈ s0, x' ∈ (uvs i).1, by simpa using hx', let ⟨i,is0,hi⟩ := this in (h i).2.2.2.2 ⟨hi, (bInter_subset_of_mem is0 : v ⊆ (uvs i).2) hy'⟩, ⟨u, v, ‹is_open u›, ‹is_open v›, s0_cover, ‹t ⊆ v›, ‹set.prod u v ⊆ n›⟩ /-- If `s` and `t` are compact sets and `n` is an open neighborhood of `s × t`, then there exist open neighborhoods `u ⊇ s` and `v ⊇ t` such that `u × v ⊆ n`. -/ lemma generalized_tube_lemma {s : set α} (hs : is_compact s) {t : set β} (ht : is_compact t) {n : set (α × β)} (hn : is_open n) (hp : set.prod s t ⊆ n) : ∃ (u : set α) (v : set β), is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ set.prod u v ⊆ n := have _, from nhds_contain_boxes_of_compact hs t $ assume x _, nhds_contain_boxes.symm $ nhds_contain_boxes_of_compact ht {x} $ assume y _, nhds_contain_boxes_of_singleton, this n hn hp end tube_lemma /-- Type class for compact spaces. Separation is sometimes included in the definition, especially in the French literature, but we do not include it here. -/ class compact_space (α : Type*) [topological_space α] : Prop := (compact_univ : is_compact (univ : set α)) @[priority 10] -- see Note [lower instance priority] instance subsingleton.compact_space [subsingleton α] : compact_space α := ⟨subsingleton_univ.is_compact⟩ lemma is_compact_univ_iff : is_compact (univ : set α) ↔ compact_space α := ⟨λ h, ⟨h⟩, λ h, h.1⟩ lemma compact_univ [h : compact_space α] : is_compact (univ : set α) := h.compact_univ lemma cluster_point_of_compact [compact_space α] (f : filter α) [ne_bot f] : ∃ x, cluster_pt x f := by simpa using compact_univ (show f ≤ 𝓟 univ, by simp) lemma compact_space.elim_nhds_subcover {α : Type*} [topological_space α] [compact_space α] (U : α → set α) (hU : ∀ x, U x ∈ 𝓝 x) : ∃ t : finset α, (⋃ x ∈ t, U x) = ⊤ := begin obtain ⟨t, -, s⟩ := is_compact.elim_nhds_subcover compact_univ U (λ x m, hU x), exact ⟨t, by { rw eq_top_iff, exact s }⟩, end theorem compact_space_of_finite_subfamily_closed {α : Type u} [topological_space α] (h : Π {ι : Type u} (Z : ι → (set α)), (∀ i, is_closed (Z i)) → (⋂ i, Z i) = ∅ → ∃ (t : finset ι), (⋂ i ∈ t, Z i) = ∅) : compact_space α := { compact_univ := begin apply is_compact_of_finite_subfamily_closed, intros ι Z, specialize h Z, simpa using h end } lemma is_closed.is_compact [compact_space α] {s : set α} (h : is_closed s) : is_compact s := compact_of_is_closed_subset compact_univ h (subset_univ _) /-- `α` is a noncompact topological space if it not a compact space. -/ class noncompact_space (α : Type*) [topological_space α] : Prop := (noncompact_univ [] : ¬is_compact (univ : set α)) export noncompact_space (noncompact_univ) instance [noncompact_space α] : ne_bot (filter.cocompact α) := begin refine filter.has_basis_cocompact.ne_bot_iff.2 (λ s hs, _), contrapose hs, rw [not_nonempty_iff_eq_empty, compl_empty_iff] at hs, rw hs, exact noncompact_univ α end instance [noncompact_space α] : ne_bot (filter.coclosed_compact α) := ne_bot_of_le filter.cocompact_le_coclosed_compact lemma noncompact_space_of_ne_bot (h : ne_bot (filter.cocompact α)) : noncompact_space α := ⟨λ h', (filter.nonempty_of_mem h'.compl_mem_cocompact).ne_empty compl_univ⟩ lemma filter.cocompact_ne_bot_iff : ne_bot (filter.cocompact α) ↔ noncompact_space α := ⟨noncompact_space_of_ne_bot, @filter.cocompact.filter.ne_bot _ _⟩ lemma not_compact_space_iff : ¬compact_space α ↔ noncompact_space α := ⟨λ h₁, ⟨λ h₂, h₁ ⟨h₂⟩⟩, λ ⟨h₁⟩ ⟨h₂⟩, h₁ h₂⟩ /-- A compact discrete space is finite. -/ noncomputable def fintype_of_compact_of_discrete [compact_space α] [discrete_topology α] : fintype α := fintype_of_univ_finite $ finite_of_is_compact_of_discrete _ compact_univ lemma finite_cover_nhds_interior [compact_space α] {U : α → set α} (hU : ∀ x, U x ∈ 𝓝 x) : ∃ t : finset α, (⋃ x ∈ t, interior (U x)) = univ := let ⟨t, ht⟩ := compact_univ.elim_finite_subcover (λ x, interior (U x)) (λ x, is_open_interior) (λ x _, mem_Union.2 ⟨x, mem_interior_iff_mem_nhds.2 (hU x)⟩) in ⟨t, univ_subset_iff.1 ht⟩ lemma finite_cover_nhds [compact_space α] {U : α → set α} (hU : ∀ x, U x ∈ 𝓝 x) : ∃ t : finset α, (⋃ x ∈ t, U x) = univ := let ⟨t, ht⟩ := finite_cover_nhds_interior hU in ⟨t, univ_subset_iff.1 $ ht ▸ bUnion_mono (λ x hx, interior_subset)⟩ /-- If `α` is a compact space, then a locally finite family of sets of `α` can have only finitely many nonempty elements. -/ lemma locally_finite.finite_nonempty_of_compact {ι : Type*} [compact_space α] {f : ι → set α} (hf : locally_finite f) : finite {i | (f i).nonempty} := by simpa only [inter_univ] using hf.finite_nonempty_inter_compact compact_univ /-- If `α` is a compact space, then a locally finite family of nonempty sets of `α` can have only finitely many elements, `set.finite` version. -/ lemma locally_finite.finite_of_compact {ι : Type*} [compact_space α] {f : ι → set α} (hf : locally_finite f) (hne : ∀ i, (f i).nonempty) : finite (univ : set ι) := by simpa only [hne] using hf.finite_nonempty_of_compact /-- If `α` is a compact space, then a locally finite family of nonempty sets of `α` can have only finitely many elements, `fintype` version. -/ noncomputable def locally_finite.fintype_of_compact {ι : Type*} [compact_space α] {f : ι → set α} (hf : locally_finite f) (hne : ∀ i, (f i).nonempty) : fintype ι := fintype_of_univ_finite (hf.finite_of_compact hne) variables [topological_space β] lemma is_compact.image_of_continuous_on {f : α → β} (hs : is_compact s) (hf : continuous_on f s) : is_compact (f '' s) := begin intros l lne ls, have : ne_bot (l.comap f ⊓ 𝓟 s) := comap_inf_principal_ne_bot_of_image_mem lne (le_principal_iff.1 ls), obtain ⟨a, has, ha⟩ : ∃ a ∈ s, cluster_pt a (l.comap f ⊓ 𝓟 s) := @@hs this inf_le_right, use [f a, mem_image_of_mem f has], have : tendsto f (𝓝 a ⊓ (comap f l ⊓ 𝓟 s)) (𝓝 (f a) ⊓ l), { convert (hf a has).inf (@tendsto_comap _ _ f l) using 1, rw nhds_within, ac_refl }, exact @@tendsto.ne_bot _ this ha, end lemma is_compact.image {f : α → β} (hs : is_compact s) (hf : continuous f) : is_compact (f '' s) := hs.image_of_continuous_on hf.continuous_on /-- The comap of the cocompact filter on `β` by a continuous function `f : α → β` is less than or equal to the cocompact filter on `α`. This is a reformulation of the fact that images of compact sets are compact. -/ lemma filter.comap_cocompact {f : α → β} (hf : continuous f) : (filter.cocompact β).comap f ≤ filter.cocompact α := begin rw (filter.has_basis_cocompact.comap f).le_basis_iff filter.has_basis_cocompact, intros t ht, refine ⟨f '' t, ht.image hf, _⟩, simpa using t.subset_preimage_image f end lemma is_compact_range [compact_space α] {f : α → β} (hf : continuous f) : is_compact (range f) := by rw ← image_univ; exact compact_univ.image hf /-- If X is is_compact then pr₂ : X × Y → Y is a closed map -/ theorem is_closed_proj_of_is_compact {X : Type*} [topological_space X] [compact_space X] {Y : Type*} [topological_space Y] : is_closed_map (prod.snd : X × Y → Y) := begin set πX := (prod.fst : X × Y → X), set πY := (prod.snd : X × Y → Y), assume C (hC : is_closed C), rw is_closed_iff_cluster_pt at hC ⊢, assume y (y_closure : cluster_pt y $ 𝓟 (πY '' C)), have : ne_bot (map πX (comap πY (𝓝 y) ⊓ 𝓟 C)), { suffices : ne_bot (map πY (comap πY (𝓝 y) ⊓ 𝓟 C)), by simpa only [map_ne_bot_iff], convert y_closure, calc map πY (comap πY (𝓝 y) ⊓ 𝓟 C) = 𝓝 y ⊓ map πY (𝓟 C) : filter.push_pull' _ _ _ ... = 𝓝 y ⊓ 𝓟 (πY '' C) : by rw map_principal }, resetI, obtain ⟨x, hx⟩ : ∃ x, cluster_pt x (map πX (comap πY (𝓝 y) ⊓ 𝓟 C)), from cluster_point_of_compact _, refine ⟨⟨x, y⟩, _, by simp [πY]⟩, apply hC, rw [cluster_pt, ← filter.map_ne_bot_iff πX], convert hx, calc map πX (𝓝 (x, y) ⊓ 𝓟 C) = map πX (comap πX (𝓝 x) ⊓ comap πY (𝓝 y) ⊓ 𝓟 C) : by rw [nhds_prod_eq, filter.prod] ... = map πX (comap πY (𝓝 y) ⊓ 𝓟 C ⊓ comap πX (𝓝 x)) : by ac_refl ... = map πX (comap πY (𝓝 y) ⊓ 𝓟 C) ⊓ 𝓝 x : by rw filter.push_pull ... = 𝓝 x ⊓ map πX (comap πY (𝓝 y) ⊓ 𝓟 C) : by rw inf_comm end lemma exists_subset_nhd_of_compact_space [compact_space α] {ι : Type*} [nonempty ι] {V : ι → set α} (hV : directed (⊇) V) (hV_closed : ∀ i, is_closed (V i)) {U : set α} (hU : ∀ x ∈ ⋂ i, V i, U ∈ 𝓝 x) : ∃ i, V i ⊆ U := exists_subset_nhd_of_compact' hV (λ i, (hV_closed i).is_compact) hV_closed hU lemma embedding.is_compact_iff_is_compact_image {f : α → β} (hf : embedding f) : is_compact s ↔ is_compact (f '' s) := iff.intro (assume h, h.image hf.continuous) $ assume h, begin rw is_compact_iff_ultrafilter_le_nhds at ⊢ h, intros u us', have : ↑(u.map f) ≤ 𝓟 (f '' s), begin rw [ultrafilter.coe_map, map_le_iff_le_comap, comap_principal], convert us', exact preimage_image_eq _ hf.inj end, rcases h (u.map f) this with ⟨_, ⟨a, ha, ⟨⟩⟩, _⟩, refine ⟨a, ha, _⟩, rwa [hf.induced, nhds_induced, ←map_le_iff_le_comap] end /-- A closed embedding is proper, ie, inverse images of compact sets are contained in compacts. -/ lemma closed_embedding.tendsto_cocompact {f : α → β} (hf : closed_embedding f) : tendsto f (filter.cocompact α) (filter.cocompact β) := begin rw filter.has_basis_cocompact.tendsto_iff filter.has_basis_cocompact, intros K hK, refine ⟨f ⁻¹' (K ∩ (set.range f)), _, λ x hx, by simpa using hx⟩, apply hf.to_embedding.is_compact_iff_is_compact_image.mpr, rw set.image_preimage_eq_of_subset (set.inter_subset_right _ _), exact hK.inter_right hf.closed_range, end lemma compact_iff_compact_in_subtype {p : α → Prop} {s : set {a // p a}} : is_compact s ↔ is_compact ((coe : _ → α) '' s) := embedding_subtype_coe.is_compact_iff_is_compact_image lemma is_compact_iff_is_compact_univ {s : set α} : is_compact s ↔ is_compact (univ : set s) := by rw [compact_iff_compact_in_subtype, image_univ, subtype.range_coe]; refl lemma is_compact_iff_compact_space {s : set α} : is_compact s ↔ compact_space s := is_compact_iff_is_compact_univ.trans ⟨λ h, ⟨h⟩, @compact_space.compact_univ _ _⟩ protected lemma closed_embedding.noncompact_space [noncompact_space α] {f : α → β} (hf : closed_embedding f) : noncompact_space β := noncompact_space_of_ne_bot hf.tendsto_cocompact.ne_bot protected lemma closed_embedding.compact_space [h : compact_space β] {f : α → β} (hf : closed_embedding f) : compact_space α := by { unfreezingI { contrapose! h, rw not_compact_space_iff at h ⊢ }, exact hf.noncompact_space } lemma is_compact.prod {s : set α} {t : set β} (hs : is_compact s) (ht : is_compact t) : is_compact (set.prod s t) := begin rw is_compact_iff_ultrafilter_le_nhds at hs ht ⊢, intros f hfs, rw le_principal_iff at hfs, obtain ⟨a : α, sa : a ∈ s, ha : map prod.fst ↑f ≤ 𝓝 a⟩ := hs (f.map prod.fst) (le_principal_iff.2 $ mem_map.2 $ mem_of_superset hfs (λ x, and.left)), obtain ⟨b : β, tb : b ∈ t, hb : map prod.snd ↑f ≤ 𝓝 b⟩ := ht (f.map prod.snd) (le_principal_iff.2 $ mem_map.2 $ mem_of_superset hfs (λ x, and.right)), rw map_le_iff_le_comap at ha hb, refine ⟨⟨a, b⟩, ⟨sa, tb⟩, _⟩, rw nhds_prod_eq, exact le_inf ha hb end lemma inducing.is_compact_iff {f : α → β} (hf : inducing f) {s : set α} : is_compact (f '' s) ↔ is_compact s := begin split, { introsI hs F F_ne_bot F_le, obtain ⟨_, ⟨x, x_in : x ∈ s, rfl⟩, hx : cluster_pt (f x) (map f F)⟩ := hs (calc map f F ≤ map f (𝓟 s) : map_mono F_le ... = 𝓟 (f '' s) : map_principal), use [x, x_in], suffices : (map f (𝓝 x ⊓ F)).ne_bot, by simpa [filter.map_ne_bot_iff], rwa calc map f (𝓝 x ⊓ F) = map f ((comap f $ 𝓝 $ f x) ⊓ F) : by rw hf.nhds_eq_comap ... = 𝓝 (f x) ⊓ map f F : filter.push_pull' _ _ _ }, { intro hs, exact hs.image hf.continuous } end /-- Finite topological spaces are compact. -/ @[priority 100] instance fintype.compact_space [fintype α] : compact_space α := { compact_univ := finite_univ.is_compact } /-- The product of two compact spaces is compact. -/ instance [compact_space α] [compact_space β] : compact_space (α × β) := ⟨by { rw ← univ_prod_univ, exact compact_univ.prod compact_univ }⟩ /-- The disjoint union of two compact spaces is compact. -/ instance [compact_space α] [compact_space β] : compact_space (α ⊕ β) := ⟨begin rw ← range_inl_union_range_inr, exact (is_compact_range continuous_inl).union (is_compact_range continuous_inr) end⟩ /-- The coproduct of the cocompact filters on two topological spaces is the cocompact filter on their product. -/ lemma filter.coprod_cocompact : (filter.cocompact α).coprod (filter.cocompact β) = filter.cocompact (α × β) := begin ext S, simp only [mem_coprod_iff, exists_prop, mem_comap, filter.mem_cocompact], split, { rintro ⟨⟨A, ⟨t, ht, hAt⟩, hAS⟩, B, ⟨t', ht', hBt'⟩, hBS⟩, refine ⟨t.prod t', ht.prod ht', _⟩, refine subset.trans _ (union_subset hAS hBS), rw compl_subset_comm at ⊢ hAt hBt', refine subset.trans _ (set.prod_mono hAt hBt'), intros x, simp only [compl_union, mem_inter_eq, mem_prod, mem_preimage, mem_compl_eq], tauto }, { rintros ⟨t, ht, htS⟩, refine ⟨⟨(prod.fst '' t)ᶜ, _, _⟩, ⟨(prod.snd '' t)ᶜ, _, _⟩⟩, { exact ⟨prod.fst '' t, ht.image continuous_fst, subset.rfl⟩ }, { rw preimage_compl, rw compl_subset_comm at ⊢ htS, exact subset.trans htS (subset_preimage_image prod.fst _) }, { exact ⟨prod.snd '' t, ht.image continuous_snd, subset.rfl⟩ }, { rw preimage_compl, rw compl_subset_comm at ⊢ htS, exact subset.trans htS (subset_preimage_image prod.snd _) } } end lemma prod.noncompact_space_iff : noncompact_space (α × β) ↔ noncompact_space α ∧ nonempty β ∨ nonempty α ∧ noncompact_space β := by simp [← filter.cocompact_ne_bot_iff, ← filter.coprod_cocompact, filter.coprod_ne_bot_iff] @[priority 100] -- See Note [lower instance priority] instance prod.noncompact_space_left [noncompact_space α] [nonempty β] : noncompact_space (α × β) := prod.noncompact_space_iff.2 (or.inl ⟨‹_›, ‹_›⟩) @[priority 100] -- See Note [lower instance priority] instance prod.noncompact_space_right [nonempty α] [noncompact_space β] : noncompact_space (α × β) := prod.noncompact_space_iff.2 (or.inr ⟨‹_›, ‹_›⟩) section tychonoff variables {ι : Type*} {π : ι → Type*} [∀ i, topological_space (π i)] /-- **Tychonoff's theorem** -/ lemma is_compact_pi_infinite {s : Π i, set (π i)} : (∀ i, is_compact (s i)) → is_compact {x : Π i, π i | ∀ i, x i ∈ s i} := begin simp only [is_compact_iff_ultrafilter_le_nhds, nhds_pi, exists_prop, mem_set_of_eq, le_infi_iff, le_principal_iff], intros h f hfs, have : ∀i:ι, ∃a, a∈s i ∧ tendsto (λx:Πi:ι, π i, x i) f (𝓝 a), { refine λ i, h i (f.map _) (mem_map.2 _), exact mem_of_superset hfs (λ x hx, hx i) }, choose a ha, exact ⟨a, assume i, (ha i).left, assume i, (ha i).right.le_comap⟩ end /-- A version of Tychonoff's theorem that uses `set.pi`. -/ lemma is_compact_univ_pi {s : Π i, set (π i)} (h : ∀ i, is_compact (s i)) : is_compact (pi univ s) := by { convert is_compact_pi_infinite h, simp only [pi, forall_prop_of_true, mem_univ] } instance pi.compact_space [∀ i, compact_space (π i)] : compact_space (Πi, π i) := ⟨by { rw [← pi_univ univ], exact is_compact_univ_pi (λ i, compact_univ) }⟩ /-- Product of compact sets is compact -/ lemma filter.Coprod_cocompact {δ : Type*} {κ : δ → Type*} [Π d, topological_space (κ d)] : filter.Coprod (λ d, filter.cocompact (κ d)) = filter.cocompact (Π d, κ d) := begin ext S, simp only [mem_coprod_iff, exists_prop, mem_comap, filter.mem_cocompact], split, { intros h, rw filter.mem_Coprod_iff at h, choose t ht1 ht2 using h, choose t1 ht11 ht12 using λ d, filter.mem_cocompact.mp (ht1 d), refine ⟨set.pi set.univ t1, _, _⟩, { convert is_compact_pi_infinite ht11, ext, simp }, { refine subset.trans _ (set.Union_subset ht2), intros x, simp only [mem_Union, mem_univ_pi, exists_imp_distrib, mem_compl_eq, not_forall], intros d h, exact ⟨d, ht12 d h⟩ } }, { rintros ⟨t, h1, h2⟩, rw filter.mem_Coprod_iff, intros d, refine ⟨((λ (k : Π (d : δ), κ d), k d) '' t)ᶜ, _, _⟩, { rw filter.mem_cocompact, refine ⟨(λ (k : Π (d : δ), κ d), k d) '' t, _, set.subset.refl _⟩, exact is_compact.image h1 (continuous_pi_iff.mp (continuous_id) d) }, refine subset.trans _ h2, intros x hx, simp only [not_exists, mem_image, mem_preimage, mem_compl_eq] at hx, simpa using mt (hx x) }, end end tychonoff instance quot.compact_space {r : α → α → Prop} [compact_space α] : compact_space (quot r) := ⟨by { rw ← range_quot_mk, exact is_compact_range continuous_quot_mk }⟩ instance quotient.compact_space {s : setoid α} [compact_space α] : compact_space (quotient s) := quot.compact_space /-- There are various definitions of "locally compact space" in the literature, which agree for Hausdorff spaces but not in general. This one is the precise condition on X needed for the evaluation `map C(X, Y) × X → Y` to be continuous for all `Y` when `C(X, Y)` is given the compact-open topology. -/ class locally_compact_space (α : Type*) [topological_space α] : Prop := (local_compact_nhds : ∀ (x : α) (n ∈ 𝓝 x), ∃ s ∈ 𝓝 x, s ⊆ n ∧ is_compact s) lemma compact_basis_nhds [locally_compact_space α] (x : α) : (𝓝 x).has_basis (λ s, s ∈ 𝓝 x ∧ is_compact s) (λ s, s) := has_basis_self.2 $ by simpa only [and_comm] using locally_compact_space.local_compact_nhds x lemma locally_compact_space_of_has_basis {ι : α → Type*} {p : Π x, ι x → Prop} {s : Π x, ι x → set α} (h : ∀ x, (𝓝 x).has_basis (p x) (s x)) (hc : ∀ x i, p x i → is_compact (s x i)) : locally_compact_space α := ⟨λ x t ht, let ⟨i, hp, ht⟩ := (h x).mem_iff.1 ht in ⟨s x i, (h x).mem_of_mem hp, ht, hc x i hp⟩⟩ instance locally_compact_space.prod (α : Type*) (β : Type*) [topological_space α] [topological_space β] [locally_compact_space α] [locally_compact_space β] : locally_compact_space (α × β) := have _ := λ x : α × β, (compact_basis_nhds x.1).prod_nhds' (compact_basis_nhds x.2), locally_compact_space_of_has_basis this $ λ x s ⟨⟨_, h₁⟩, _, h₂⟩, h₁.prod h₂ /-- A reformulation of the definition of locally compact space: In a locally compact space, every open set containing `x` has a compact subset containing `x` in its interior. -/ lemma exists_compact_subset [locally_compact_space α] {x : α} {U : set α} (hU : is_open U) (hx : x ∈ U) : ∃ (K : set α), is_compact K ∧ x ∈ interior K ∧ K ⊆ U := begin rcases locally_compact_space.local_compact_nhds x U (hU.mem_nhds hx) with ⟨K, h1K, h2K, h3K⟩, exact ⟨K, h3K, mem_interior_iff_mem_nhds.2 h1K, h2K⟩, end /-- In a locally compact space every point has a compact neighborhood. -/ lemma exists_compact_mem_nhds [locally_compact_space α] (x : α) : ∃ K, is_compact K ∧ K ∈ 𝓝 x := let ⟨K, hKc, hx, H⟩ := exists_compact_subset is_open_univ (mem_univ x) in ⟨K, hKc, mem_interior_iff_mem_nhds.1 hx⟩ /-- In a locally compact space, every compact set is contained in the interior of a compact set. -/ lemma exists_compact_superset [locally_compact_space α] {K : set α} (hK : is_compact K) : ∃ K', is_compact K' ∧ K ⊆ interior K' := begin choose U hUc hxU using λ x : K, exists_compact_mem_nhds (x : α), have : K ⊆ ⋃ x, interior (U x), from λ x hx, mem_Union.2 ⟨⟨x, hx⟩, mem_interior_iff_mem_nhds.2 (hxU _)⟩, rcases hK.elim_finite_subcover _ _ this with ⟨t, ht⟩, { refine ⟨_, t.compact_bUnion (λ x _, hUc x), λ x hx, _⟩, rcases mem_bUnion_iff.1 (ht hx) with ⟨y, hyt, hy⟩, exact interior_mono (subset_bUnion_of_mem hyt) hy }, { exact λ _, is_open_interior } end lemma ultrafilter.le_nhds_Lim [compact_space α] (F : ultrafilter α) : ↑F ≤ 𝓝 (@Lim _ _ (F : filter α).nonempty_of_ne_bot F) := begin rcases compact_univ.ultrafilter_le_nhds F (by simp) with ⟨x, -, h⟩, exact le_nhds_Lim ⟨x,h⟩, end theorem is_closed.exists_minimal_nonempty_closed_subset [compact_space α] {S : set α} (hS : is_closed S) (hne : S.nonempty) : ∃ (V : set α), V ⊆ S ∧ V.nonempty ∧ is_closed V ∧ (∀ (V' : set α), V' ⊆ V → V'.nonempty → is_closed V' → V' = V) := begin let opens := {U : set α | Sᶜ ⊆ U ∧ is_open U ∧ Uᶜ.nonempty}, obtain ⟨U, ⟨Uc, Uo, Ucne⟩, h⟩ := zorn.zorn_subset opens (λ c hc hz, begin by_cases hcne : c.nonempty, { obtain ⟨U₀, hU₀⟩ := hcne, haveI : nonempty {U // U ∈ c} := ⟨⟨U₀, hU₀⟩⟩, obtain ⟨U₀compl, U₀opn, U₀ne⟩ := hc hU₀, use ⋃₀ c, refine ⟨⟨_, _, _⟩, λ U hU a ha, ⟨U, hU, ha⟩⟩, { exact λ a ha, ⟨U₀, hU₀, U₀compl ha⟩ }, { exact is_open_sUnion (λ _ h, (hc h).2.1) }, { convert_to (⋂(U : {U // U ∈ c}), U.1ᶜ).nonempty, { ext, simp only [not_exists, exists_prop, not_and, set.mem_Inter, subtype.forall, set.mem_set_of_eq, set.mem_compl_eq, subtype.val_eq_coe], refl, }, apply is_compact.nonempty_Inter_of_directed_nonempty_compact_closed, { rintros ⟨U, hU⟩ ⟨U', hU'⟩, obtain ⟨V, hVc, hVU, hVU'⟩ := zorn.chain.directed_on hz U hU U' hU', exact ⟨⟨V, hVc⟩, set.compl_subset_compl.mpr hVU, set.compl_subset_compl.mpr hVU'⟩, }, { exact λ U, (hc U.2).2.2, }, { exact λ U, (is_closed_compl_iff.mpr (hc U.2).2.1).is_compact, }, { exact λ U, (is_closed_compl_iff.mpr (hc U.2).2.1), } } }, { use Sᶜ, refine ⟨⟨set.subset.refl _, is_open_compl_iff.mpr hS, _⟩, λ U Uc, (hcne ⟨U, Uc⟩).elim⟩, rw compl_compl, exact hne, } end), refine ⟨Uᶜ, set.compl_subset_comm.mp Uc, Ucne, is_closed_compl_iff.mpr Uo, _⟩, intros V' V'sub V'ne V'cls, have : V'ᶜ = U, { refine h V'ᶜ ⟨_, is_open_compl_iff.mpr V'cls, _⟩ (set.subset_compl_comm.mp V'sub), exact set.subset.trans Uc (set.subset_compl_comm.mp V'sub), simp only [compl_compl, V'ne], }, rw [←this, compl_compl], end /-- A σ-compact space is a space that is the union of a countable collection of compact subspaces. Note that a locally compact separable T₂ space need not be σ-compact. The sequence can be extracted using `topological_space.compact_covering`. -/ class sigma_compact_space (α : Type*) [topological_space α] : Prop := (exists_compact_covering : ∃ K : ℕ → set α, (∀ n, is_compact (K n)) ∧ (⋃ n, K n) = univ) @[priority 200] -- see Note [lower instance priority] instance compact_space.sigma_compact [compact_space α] : sigma_compact_space α := ⟨⟨λ _, univ, λ _, compact_univ, Union_const _⟩⟩ lemma sigma_compact_space.of_countable (S : set (set α)) (Hc : countable S) (Hcomp : ∀ s ∈ S, is_compact s) (HU : ⋃₀ S = univ) : sigma_compact_space α := ⟨(exists_seq_cover_iff_countable ⟨_, is_compact_empty⟩).2 ⟨S, Hc, Hcomp, HU⟩⟩ @[priority 100] -- see Note [lower instance priority] instance sigma_compact_space_of_locally_compact_second_countable [locally_compact_space α] [second_countable_topology α] : sigma_compact_space α := begin choose K hKc hxK using λ x : α, exists_compact_mem_nhds x, rcases countable_cover_nhds hxK with ⟨s, hsc, hsU⟩, refine sigma_compact_space.of_countable _ (hsc.image K) (ball_image_iff.2 $ λ x _, hKc x) _, rwa sUnion_image end variables (α) [sigma_compact_space α] open sigma_compact_space /-- A choice of compact covering for a `σ`-compact space, chosen to be monotone. -/ def compact_covering : ℕ → set α := accumulate exists_compact_covering.some lemma is_compact_compact_covering (n : ℕ) : is_compact (compact_covering α n) := compact_accumulate (classical.some_spec sigma_compact_space.exists_compact_covering).1 n lemma Union_compact_covering : (⋃ n, compact_covering α n) = univ := begin rw [compact_covering, Union_accumulate], exact (classical.some_spec sigma_compact_space.exists_compact_covering).2 end @[mono] lemma compact_covering_subset ⦃m n : ℕ⦄ (h : m ≤ n) : compact_covering α m ⊆ compact_covering α n := monotone_accumulate h variable {α} lemma exists_mem_compact_covering (x : α) : ∃ n, x ∈ compact_covering α n := Union_eq_univ_iff.mp (Union_compact_covering α) x /-- If `α` is a `σ`-compact space, then a locally finite family of nonempty sets of `α` can have only countably many elements, `set.countable` version. -/ lemma locally_finite.countable_of_sigma_compact {ι : Type*} {f : ι → set α} (hf : locally_finite f) (hne : ∀ i, (f i).nonempty) : countable (univ : set ι) := begin have := λ n, hf.finite_nonempty_inter_compact (is_compact_compact_covering α n), refine (countable_Union (λ n, (this n).countable)).mono (λ i hi, _), rcases hne i with ⟨x, hx⟩, rcases Union_eq_univ_iff.1 (Union_compact_covering α) x with ⟨n, hn⟩, exact mem_Union.2 ⟨n, x, hx, hn⟩ end /-- In a topological space with sigma compact topology, if `f` is a function that sends each point `x` of a closed set `s` to a neighborhood of `x` within `s`, then for some countable set `t ⊆ s`, the neighborhoods `f x`, `x ∈ t`, cover the whole set `s`. -/ lemma countable_cover_nhds_within_of_sigma_compact {f : α → set α} {s : set α} (hs : is_closed s) (hf : ∀ x ∈ s, f x ∈ 𝓝[s] x) : ∃ t ⊆ s, countable t ∧ s ⊆ ⋃ x ∈ t, f x := begin simp only [nhds_within, mem_inf_principal] at hf, choose t ht hsub using λ n, ((is_compact_compact_covering α n).inter_right hs).elim_nhds_subcover _ (λ x hx, hf x hx.right), refine ⟨⋃ n, (t n : set α), Union_subset $ λ n x hx, (ht n x hx).2, countable_Union $ λ n, (t n).countable_to_set, λ x hx, mem_bUnion_iff.2 _⟩, rcases exists_mem_compact_covering x with ⟨n, hn⟩, rcases mem_bUnion_iff.1 (hsub n ⟨hn, hx⟩) with ⟨y, hyt : y ∈ t n, hyf : x ∈ s → x ∈ f y⟩, exact ⟨y, mem_Union.2 ⟨n, hyt⟩, hyf hx⟩ end /-- In a topological space with sigma compact topology, if `f` is a function that sends each point `x` to a neighborhood of `x`, then for some countable set `s`, the neighborhoods `f x`, `x ∈ s`, cover the whole space. -/ lemma countable_cover_nhds_of_sigma_compact {f : α → set α} (hf : ∀ x, f x ∈ 𝓝 x) : ∃ s : set α, countable s ∧ (⋃ x ∈ s, f x) = univ := begin simp only [← nhds_within_univ] at hf, rcases countable_cover_nhds_within_of_sigma_compact is_closed_univ (λ x _, hf x) with ⟨s, -, hsc, hsU⟩, exact ⟨s, hsc, univ_subset_iff.1 hsU⟩ end end compact /-- An [exhaustion by compact sets](https://en.wikipedia.org/wiki/Exhaustion_by_compact_sets) of a topological space is a sequence of compact sets `K n` such that `K n ⊆ interior (K (n + 1))` and `(⋃ n, K n) = univ`. If `X` is a locally compact sigma compact space, then `compact_exhaustion.choice X` provides a choice of an exhaustion by compact sets. This choice is also available as `(default : compact_exhaustion X)`. -/ structure compact_exhaustion (X : Type*) [topological_space X] := (to_fun : ℕ → set X) (is_compact' : ∀ n, is_compact (to_fun n)) (subset_interior_succ' : ∀ n, to_fun n ⊆ interior (to_fun (n + 1))) (Union_eq' : (⋃ n, to_fun n) = univ) namespace compact_exhaustion instance : has_coe_to_fun (compact_exhaustion α) (λ _, ℕ → set α) := ⟨to_fun⟩ variables {α} (K : compact_exhaustion α) protected lemma is_compact (n : ℕ) : is_compact (K n) := K.is_compact' n lemma subset_interior_succ (n : ℕ) : K n ⊆ interior (K (n + 1)) := K.subset_interior_succ' n lemma subset_succ (n : ℕ) : K n ⊆ K (n + 1) := subset.trans (K.subset_interior_succ n) interior_subset @[mono] protected lemma subset ⦃m n : ℕ⦄ (h : m ≤ n) : K m ⊆ K n := show K m ≤ K n, from monotone_nat_of_le_succ K.subset_succ h lemma subset_interior ⦃m n : ℕ⦄ (h : m < n) : K m ⊆ interior (K n) := subset.trans (K.subset_interior_succ m) $ interior_mono $ K.subset h lemma Union_eq : (⋃ n, K n) = univ := K.Union_eq' lemma exists_mem (x : α) : ∃ n, x ∈ K n := Union_eq_univ_iff.1 K.Union_eq x /-- The minimal `n` such that `x ∈ K n`. -/ protected noncomputable def find (x : α) : ℕ := nat.find (K.exists_mem x) lemma mem_find (x : α) : x ∈ K (K.find x) := nat.find_spec (K.exists_mem x) lemma mem_iff_find_le {x : α} {n : ℕ} : x ∈ K n ↔ K.find x ≤ n := ⟨λ h, nat.find_min' (K.exists_mem x) h, λ h, K.subset h $ K.mem_find x⟩ /-- Prepend the empty set to a compact exhaustion `K n`. -/ def shiftr : compact_exhaustion α := { to_fun := λ n, nat.cases_on n ∅ K, is_compact' := λ n, nat.cases_on n is_compact_empty K.is_compact, subset_interior_succ' := λ n, nat.cases_on n (empty_subset _) K.subset_interior_succ, Union_eq' := Union_eq_univ_iff.2 $ λ x, ⟨K.find x + 1, K.mem_find x⟩ } @[simp] lemma find_shiftr (x : α) : K.shiftr.find x = K.find x + 1 := nat.find_comp_succ _ _ (not_mem_empty _) lemma mem_diff_shiftr_find (x : α) : x ∈ K.shiftr (K.find x + 1) \ K.shiftr (K.find x) := ⟨K.mem_find _, mt K.shiftr.mem_iff_find_le.1 $ by simp only [find_shiftr, not_le, nat.lt_succ_self]⟩ /-- A choice of an [exhaustion by compact sets](https://en.wikipedia.org/wiki/Exhaustion_by_compact_sets) of a locally compact sigma compact space. -/ noncomputable def choice (X : Type*) [topological_space X] [locally_compact_space X] [sigma_compact_space X] : compact_exhaustion X := begin apply classical.choice, let K : ℕ → {s : set X // is_compact s} := λ n, nat.rec_on n ⟨∅, is_compact_empty⟩ (λ n s, ⟨(exists_compact_superset s.2).some ∪ compact_covering X n, (exists_compact_superset s.2).some_spec.1.union (is_compact_compact_covering _ _)⟩), refine ⟨⟨λ n, K n, λ n, (K n).2, λ n, _, _⟩⟩, { exact subset.trans (exists_compact_superset (K n).2).some_spec.2 (interior_mono $ subset_union_left _ _) }, { refine univ_subset_iff.1 (Union_compact_covering X ▸ _), exact Union_subset_Union2 (λ n, ⟨n + 1, subset_union_right _ _⟩) } end noncomputable instance [locally_compact_space α] [sigma_compact_space α] : inhabited (compact_exhaustion α) := ⟨compact_exhaustion.choice α⟩ end compact_exhaustion section clopen /-- A set is clopen if it is both open and closed. -/ def is_clopen (s : set α) : Prop := is_open s ∧ is_closed s theorem is_clopen.union {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∪ t) := ⟨is_open.union hs.1 ht.1, is_closed.union hs.2 ht.2⟩ theorem is_clopen.inter {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∩ t) := ⟨is_open.inter hs.1 ht.1, is_closed.inter hs.2 ht.2⟩ @[simp] theorem is_clopen_empty : is_clopen (∅ : set α) := ⟨is_open_empty, is_closed_empty⟩ @[simp] theorem is_clopen_univ : is_clopen (univ : set α) := ⟨is_open_univ, is_closed_univ⟩ theorem is_clopen.compl {s : set α} (hs : is_clopen s) : is_clopen sᶜ := ⟨hs.2.is_open_compl, is_closed_compl_iff.2 hs.1⟩ @[simp] theorem is_clopen_compl_iff {s : set α} : is_clopen sᶜ ↔ is_clopen s := ⟨λ h, compl_compl s ▸ is_clopen.compl h, is_clopen.compl⟩ theorem is_clopen.diff {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s \ t) := hs.inter ht.compl lemma is_clopen_Union {β : Type*} [fintype β] {s : β → set α} (h : ∀ i, is_clopen (s i)) : is_clopen (⋃ i, s i) := ⟨is_open_Union (forall_and_distrib.1 h).1, is_closed_Union (forall_and_distrib.1 h).2⟩ lemma is_clopen_bUnion {β : Type*} {s : finset β} {f : β → set α} (h : ∀i ∈ s, is_clopen $ f i) : is_clopen (⋃ i ∈ s, f i) := begin refine ⟨is_open_bUnion (λ i hi, (h i hi).1), _⟩, show is_closed (⋃ (i : β) (H : i ∈ (s : set β)), f i), rw bUnion_eq_Union, exact is_closed_Union (λ ⟨i, hi⟩,(h i hi).2) end lemma is_clopen_Inter {β : Type*} [fintype β] {s : β → set α} (h : ∀ i, is_clopen (s i)) : is_clopen (⋂ i, s i) := ⟨(is_open_Inter (forall_and_distrib.1 h).1), (is_closed_Inter (forall_and_distrib.1 h).2)⟩ lemma is_clopen_bInter {β : Type*} {s : finset β} {f : β → set α} (h : ∀i∈s, is_clopen (f i)) : is_clopen (⋂i∈s, f i) := ⟨ is_open_bInter ⟨finset_coe.fintype s⟩ (λ i hi, (h i hi).1), by {show is_closed (⋂ (i : β) (H : i ∈ (↑s : set β)), f i), rw bInter_eq_Inter, apply is_closed_Inter, rintro ⟨i, hi⟩, exact (h i hi).2}⟩ lemma continuous_on.preimage_clopen_of_clopen {β: Type*} [topological_space β] {f : α → β} {s : set α} {t : set β} (hf : continuous_on f s) (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∩ f⁻¹' t) := ⟨continuous_on.preimage_open_of_open hf hs.1 ht.1, continuous_on.preimage_closed_of_closed hf hs.2 ht.2⟩ /-- The intersection of a disjoint covering by two open sets of a clopen set will be clopen. -/ theorem is_clopen_inter_of_disjoint_cover_clopen {Z a b : set α} (h : is_clopen Z) (cover : Z ⊆ a ∪ b) (ha : is_open a) (hb : is_open b) (hab : a ∩ b = ∅) : is_clopen (Z ∩ a) := begin refine ⟨is_open.inter h.1 ha, _⟩, have : is_closed (Z ∩ bᶜ) := is_closed.inter h.2 (is_closed_compl_iff.2 hb), convert this using 1, apply subset.antisymm, { exact inter_subset_inter_right Z (subset_compl_iff_disjoint.2 hab) }, { rintros x ⟨hx₁, hx₂⟩, exact ⟨hx₁, by simpa [not_mem_of_mem_compl hx₂] using cover hx₁⟩ } end @[simp] lemma is_clopen_discrete [discrete_topology α] (x : set α) : is_clopen x := ⟨is_open_discrete _, is_closed_discrete _⟩ end clopen section preirreducible /-- A preirreducible set `s` is one where there is no non-trivial pair of disjoint opens on `s`. -/ def is_preirreducible (s : set α) : Prop := ∀ (u v : set α), is_open u → is_open v → (s ∩ u).nonempty → (s ∩ v).nonempty → (s ∩ (u ∩ v)).nonempty /-- An irreducible set `s` is one that is nonempty and where there is no non-trivial pair of disjoint opens on `s`. -/ def is_irreducible (s : set α) : Prop := s.nonempty ∧ is_preirreducible s lemma is_irreducible.nonempty {s : set α} (h : is_irreducible s) : s.nonempty := h.1 lemma is_irreducible.is_preirreducible {s : set α} (h : is_irreducible s) : is_preirreducible s := h.2 theorem is_preirreducible_empty : is_preirreducible (∅ : set α) := λ _ _ _ _ _ ⟨x, h1, h2⟩, h1.elim theorem is_irreducible_singleton {x} : is_irreducible ({x} : set α) := ⟨singleton_nonempty x, λ u v _ _ ⟨y, h1, h2⟩ ⟨z, h3, h4⟩, by rw mem_singleton_iff at h1 h3; substs y z; exact ⟨x, rfl, h2, h4⟩⟩ theorem is_preirreducible.closure {s : set α} (H : is_preirreducible s) : is_preirreducible (closure s) := λ u v hu hv ⟨y, hycs, hyu⟩ ⟨z, hzcs, hzv⟩, let ⟨p, hpu, hps⟩ := mem_closure_iff.1 hycs u hu hyu in let ⟨q, hqv, hqs⟩ := mem_closure_iff.1 hzcs v hv hzv in let ⟨r, hrs, hruv⟩ := H u v hu hv ⟨p, hps, hpu⟩ ⟨q, hqs, hqv⟩ in ⟨r, subset_closure hrs, hruv⟩ lemma is_irreducible.closure {s : set α} (h : is_irreducible s) : is_irreducible (closure s) := ⟨h.nonempty.closure, h.is_preirreducible.closure⟩ theorem exists_preirreducible (s : set α) (H : is_preirreducible s) : ∃ t : set α, is_preirreducible t ∧ s ⊆ t ∧ ∀ u, is_preirreducible u → t ⊆ u → u = t := let ⟨m, hm, hsm, hmm⟩ := zorn.zorn_subset_nonempty {t : set α | is_preirreducible t} (λ c hc hcc hcn, let ⟨t, htc⟩ := hcn in ⟨⋃₀ c, λ u v hu hv ⟨y, hy, hyu⟩ ⟨z, hz, hzv⟩, let ⟨p, hpc, hyp⟩ := mem_sUnion.1 hy, ⟨q, hqc, hzq⟩ := mem_sUnion.1 hz in or.cases_on (zorn.chain.total hcc hpc hqc) (assume hpq : p ⊆ q, let ⟨x, hxp, hxuv⟩ := hc hqc u v hu hv ⟨y, hpq hyp, hyu⟩ ⟨z, hzq, hzv⟩ in ⟨x, mem_sUnion_of_mem hxp hqc, hxuv⟩) (assume hqp : q ⊆ p, let ⟨x, hxp, hxuv⟩ := hc hpc u v hu hv ⟨y, hyp, hyu⟩ ⟨z, hqp hzq, hzv⟩ in ⟨x, mem_sUnion_of_mem hxp hpc, hxuv⟩), λ x hxc, subset_sUnion_of_mem hxc⟩) s H in ⟨m, hm, hsm, λ u hu hmu, hmm _ hu hmu⟩ /-- A maximal irreducible set that contains a given point. -/ def irreducible_component (x : α) : set α := classical.some (exists_preirreducible {x} is_irreducible_singleton.is_preirreducible) lemma irreducible_component_property (x : α) : is_preirreducible (irreducible_component x) ∧ {x} ⊆ (irreducible_component x) ∧ ∀ u, is_preirreducible u → (irreducible_component x) ⊆ u → u = (irreducible_component x) := classical.some_spec (exists_preirreducible {x} is_irreducible_singleton.is_preirreducible) theorem mem_irreducible_component {x : α} : x ∈ irreducible_component x := singleton_subset_iff.1 (irreducible_component_property x).2.1 theorem is_irreducible_irreducible_component {x : α} : is_irreducible (irreducible_component x) := ⟨⟨x, mem_irreducible_component⟩, (irreducible_component_property x).1⟩ theorem eq_irreducible_component {x : α} : ∀ {s : set α}, is_preirreducible s → irreducible_component x ⊆ s → s = irreducible_component x := (irreducible_component_property x).2.2 theorem is_closed_irreducible_component {x : α} : is_closed (irreducible_component x) := closure_eq_iff_is_closed.1 $ eq_irreducible_component is_irreducible_irreducible_component.is_preirreducible.closure subset_closure /-- A preirreducible space is one where there is no non-trivial pair of disjoint opens. -/ class preirreducible_space (α : Type u) [topological_space α] : Prop := (is_preirreducible_univ [] : is_preirreducible (univ : set α)) /-- An irreducible space is one that is nonempty and where there is no non-trivial pair of disjoint opens. -/ class irreducible_space (α : Type u) [topological_space α] extends preirreducible_space α : Prop := (to_nonempty [] : nonempty α) -- see Note [lower instance priority] attribute [instance, priority 50] irreducible_space.to_nonempty theorem nonempty_preirreducible_inter [preirreducible_space α] {s t : set α} : is_open s → is_open t → s.nonempty → t.nonempty → (s ∩ t).nonempty := by simpa only [univ_inter, univ_subset_iff] using @preirreducible_space.is_preirreducible_univ α _ _ s t theorem is_preirreducible.image [topological_space β] {s : set α} (H : is_preirreducible s) (f : α → β) (hf : continuous_on f s) : is_preirreducible (f '' s) := begin rintros u v hu hv ⟨_, ⟨⟨x, hx, rfl⟩, hxu⟩⟩ ⟨_, ⟨⟨y, hy, rfl⟩, hyv⟩⟩, rw ← mem_preimage at hxu hyv, rcases continuous_on_iff'.1 hf u hu with ⟨u', hu', u'_eq⟩, rcases continuous_on_iff'.1 hf v hv with ⟨v', hv', v'_eq⟩, have := H u' v' hu' hv', rw [inter_comm s u', ← u'_eq] at this, rw [inter_comm s v', ← v'_eq] at this, rcases this ⟨x, hxu, hx⟩ ⟨y, hyv, hy⟩ with ⟨z, hzs, hzu', hzv'⟩, refine ⟨f z, mem_image_of_mem f hzs, _, _⟩, all_goals { rw ← mem_preimage, apply mem_of_mem_inter_left, show z ∈ _ ∩ s, simp [*] } end theorem is_irreducible.image [topological_space β] {s : set α} (H : is_irreducible s) (f : α → β) (hf : continuous_on f s) : is_irreducible (f '' s) := ⟨nonempty_image_iff.mpr H.nonempty, H.is_preirreducible.image f hf⟩ lemma subtype.preirreducible_space {s : set α} (h : is_preirreducible s) : preirreducible_space s := { is_preirreducible_univ := begin intros u v hu hv hsu hsv, rw is_open_induced_iff at hu hv, rcases hu with ⟨u, hu, rfl⟩, rcases hv with ⟨v, hv, rfl⟩, rcases hsu with ⟨⟨x, hxs⟩, hxs', hxu⟩, rcases hsv with ⟨⟨y, hys⟩, hys', hyv⟩, rcases h u v hu hv ⟨x, hxs, hxu⟩ ⟨y, hys, hyv⟩ with ⟨z, hzs, ⟨hzu, hzv⟩⟩, exact ⟨⟨z, hzs⟩, ⟨set.mem_univ _, ⟨hzu, hzv⟩⟩⟩ end } lemma subtype.irreducible_space {s : set α} (h : is_irreducible s) : irreducible_space s := { is_preirreducible_univ := (subtype.preirreducible_space h.is_preirreducible).is_preirreducible_univ, to_nonempty := h.nonempty.to_subtype } /-- A set `s` is irreducible if and only if for every finite collection of open sets all of whose members intersect `s`, `s` also intersects the intersection of the entire collection (i.e., there is an element of `s` contained in every member of the collection). -/ lemma is_irreducible_iff_sInter {s : set α} : is_irreducible s ↔ ∀ (U : finset (set α)) (hU : ∀ u ∈ U, is_open u) (H : ∀ u ∈ U, (s ∩ u).nonempty), (s ∩ ⋂₀ ↑U).nonempty := begin split; intro h, { intro U, apply finset.induction_on U, { intros, simpa using h.nonempty }, { intros u U hu IH hU H, rw [finset.coe_insert, sInter_insert], apply h.2, { solve_by_elim [finset.mem_insert_self] }, { apply is_open_sInter (finset.finite_to_set U), intros, solve_by_elim [finset.mem_insert_of_mem] }, { solve_by_elim [finset.mem_insert_self] }, { apply IH, all_goals { intros, solve_by_elim [finset.mem_insert_of_mem] } } } }, { split, { simpa using h ∅ _ _; intro u; simp }, intros u v hu hv hu' hv', simpa using h {u,v} _ _, all_goals { intro t, rw [finset.mem_insert, finset.mem_singleton], rintro (rfl|rfl); assumption } } end /-- A set is preirreducible if and only if for every cover by two closed sets, it is contained in one of the two covering sets. -/ lemma is_preirreducible_iff_closed_union_closed {s : set α} : is_preirreducible s ↔ ∀ (z₁ z₂ : set α), is_closed z₁ → is_closed z₂ → s ⊆ z₁ ∪ z₂ → s ⊆ z₁ ∨ s ⊆ z₂ := begin split, all_goals { intros h t₁ t₂ ht₁ ht₂, specialize h t₁ᶜ t₂ᶜ, simp only [is_open_compl_iff, is_closed_compl_iff] at h, specialize h ht₁ ht₂ }, { contrapose!, simp only [not_subset], rintro ⟨⟨x, hx, hx'⟩, ⟨y, hy, hy'⟩⟩, rcases h ⟨x, hx, hx'⟩ ⟨y, hy, hy'⟩ with ⟨z, hz, hz'⟩, rw ← compl_union at hz', exact ⟨z, hz, hz'⟩ }, { rintro ⟨x, hx, hx'⟩ ⟨y, hy, hy'⟩, rw ← compl_inter at h, delta set.nonempty, rw imp_iff_not_or at h, contrapose! h, split, { intros z hz hz', exact h z ⟨hz, hz'⟩ }, { split; intro H; refine H _ ‹_›; assumption } } end /-- A set is irreducible if and only if for every cover by a finite collection of closed sets, it is contained in one of the members of the collection. -/ lemma is_irreducible_iff_sUnion_closed {s : set α} : is_irreducible s ↔ ∀ (Z : finset (set α)) (hZ : ∀ z ∈ Z, is_closed z) (H : s ⊆ ⋃₀ ↑Z), ∃ z ∈ Z, s ⊆ z := begin rw [is_irreducible, is_preirreducible_iff_closed_union_closed], split; intro h, { intro Z, apply finset.induction_on Z, { intros, rw [finset.coe_empty, sUnion_empty] at H, rcases h.1 with ⟨x, hx⟩, exfalso, tauto }, { intros z Z hz IH hZ H, cases h.2 z (⋃₀ ↑Z) _ _ _ with h' h', { exact ⟨z, finset.mem_insert_self _ _, h'⟩ }, { rcases IH _ h' with ⟨z', hz', hsz'⟩, { exact ⟨z', finset.mem_insert_of_mem hz', hsz'⟩ }, { intros, solve_by_elim [finset.mem_insert_of_mem] } }, { solve_by_elim [finset.mem_insert_self] }, { rw sUnion_eq_bUnion, apply is_closed_bUnion (finset.finite_to_set Z), { intros, solve_by_elim [finset.mem_insert_of_mem] } }, { simpa using H } } }, { split, { by_contradiction hs, simpa using h ∅ _ _, { intro z, simp }, { simpa [set.nonempty] using hs } }, intros z₁ z₂ hz₁ hz₂ H, have := h {z₁, z₂} _ _, simp only [exists_prop, finset.mem_insert, finset.mem_singleton] at this, { rcases this with ⟨z, rfl|rfl, hz⟩; tauto }, { intro t, rw [finset.mem_insert, finset.mem_singleton], rintro (rfl|rfl); assumption }, { simpa using H } } end end preirreducible
01b5c3ff26db8d272f39be64d0b578d1cd21e05c
29fc903502a2f31a2009389392484557342601fe
/src/data-as-subtypes.lean
13f81f106069f0b96515fb83a9270cd4c56f8c48
[]
no_license
NicolasRouquette/digraphs
9a7ed10ce54ce3d60983db153624d33e47391f8f
c6a88abc2565b0b25a0b3a93665d4b285c79e51b
refs/heads/master
1,673,439,036,710
1,604,799,582,000
1,604,799,582,000
310,965,050
0
0
null
null
null
null
UTF-8
Lean
false
false
1,724
lean
import .graph -- The list of unique vertex identifiers. def vs : list string := ["a", "b", "c", "d"] def isV (s: string) : Prop := s ∈ vs -- vT is the subtype of strings that are vertex identifiers. def vT := {s:string // isV s} -- The elements of vT are pairs: a string s and a proof that it satisfies isV s def vt_a : vT := ⟨ "a", begin refine list.nth_mem _, let n := 0, exact n, exact rfl, end⟩ def vt_b : vT := ⟨ "b", begin refine list.nth_mem _, let n := 1, exact n, exact rfl, end⟩ def vt_c : vT := ⟨ "c", begin refine list.nth_mem _, let n := 2, exact n, exact rfl, end⟩ def vt_d : vT := ⟨ "d", begin refine list.nth_mem _, let n := 3, exact n, exact rfl, end⟩ -- The list of unique edge identifiers. def es : list string := [ "e1", "e2", "e3" ] def isE (e: string) : Prop := e ∈ es -- eT is the subtype of strings that are edge identifiers. def eT := { e:string // isE e } -- The elements of eT are pairs: a string s and a proof that it satisfies isE s def e1 : eT := ⟨ "e1", begin refine list.nth_mem _, let n := 0, exact n, exact rfl, end ⟩ def e2 : eT := ⟨ "e2", begin refine list.nth_mem _, let n := 1, exact n, exact rfl, end ⟩ def e3 : eT := ⟨ "e3", begin refine list.nth_mem _, let n := 2, exact n, exact rfl, end ⟩ -- The type of directed graphs with vertices in vT and edges in eT def gT := digraph vT eT def arc1 : arcT vT := arcT.mk vt_a vt_b def arc2 : arcT vT := arcT.mk vt_a vt_c def arc3 : arcT vT := arcT.mk vt_c vt_d def phi1: eT → arcT vT | e1 := arc1 def g1 : gT := digraph.mk phi1 def e1_source := digraph.source g1 e1 #eval e1_source.1 -- result: "a" def phi2: eT → arcT vT | e1 := arc1 | e2 := arc2 | e3 := arc3 def g2 : gT := digraph.mk phi2
be58ef7203295622d8a0be03fb6e8619168ab710
4b846d8dabdc64e7ea03552bad8f7fa74763fc67
/tests/lean/eqn_compiler_error_msg.lean
9e01986291b1abbebc428dd97f8f4229f40816c0
[ "Apache-2.0" ]
permissive
pacchiano/lean
9324b33f3ac3b5c5647285160f9f6ea8d0d767dc
fdadada3a970377a6df8afcd629a6f2eab6e84e8
refs/heads/master
1,611,357,380,399
1,489,870,101,000
1,489,870,101,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
122
lean
inductive R : ℕ → Prop | pos : ∀p n, R (p + n) lemma R_id : ∀n, R n → R n | (.p + .n) (R.pos p n) := R.pos p n
21dc4a182c6a2759bdf41594e3e7c42e231133b0
8cb37a089cdb4af3af9d8bf1002b417e407a8e9e
/library/init/algebra/functions.lean
f9c0f0d852089eb211eb996e4870faf2453794a8
[ "Apache-2.0" ]
permissive
kbuzzard/lean
ae3c3db4bb462d750dbf7419b28bafb3ec983ef7
ed1788fd674bb8991acffc8fca585ec746711928
refs/heads/master
1,620,983,366,617
1,618,937,600,000
1,618,937,600,000
359,886,396
1
0
Apache-2.0
1,618,936,987,000
1,618,936,987,000
null
UTF-8
Lean
false
false
4,426
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.order init.meta universe u definition min {α : Type u} [linear_order α] (a b : α) : α := if a ≤ b then a else b definition max {α : Type u} [linear_order α] (a b : α) : α := if b ≤ a then a else b section open decidable tactic variables {α : Type u} [linear_order α] private meta def min_tac_step : tactic unit := solve1 $ intros >> `[unfold min max] >> try `[simp [*, if_pos, if_neg]] >> try `[apply le_refl] >> try `[apply le_of_not_le, assumption] meta def tactic.interactive.min_tac (a b : interactive.parse lean.parser.pexpr) : tactic unit := interactive.by_cases (none, ``(%%a ≤ %%b)); min_tac_step lemma min_le_left (a b : α) : min a b ≤ a := by min_tac a b lemma min_le_right (a b : α) : min a b ≤ b := by min_tac a b lemma le_min {a b c : α} (h₁ : c ≤ a) (h₂ : c ≤ b) : c ≤ min a b := by min_tac a b lemma le_max_left (a b : α) : a ≤ max a b := by min_tac b a lemma le_max_right (a b : α) : b ≤ max a b := by min_tac b a lemma max_le {a b c : α} (h₁ : a ≤ c) (h₂ : b ≤ c) : max a b ≤ c := by min_tac b a lemma eq_min {a b c : α} (h₁ : c ≤ a) (h₂ : c ≤ b) (h₃ : ∀{d}, d ≤ a → d ≤ b → d ≤ c) : c = min a b := le_antisymm (le_min h₁ h₂) (h₃ (min_le_left a b) (min_le_right a b)) lemma min_comm (a b : α) : min a b = min b a := eq_min (min_le_right a b) (min_le_left a b) (λ c h₁ h₂, le_min h₂ h₁) lemma min_assoc (a b c : α) : min (min a b) c = min a (min b c) := begin apply eq_min, { apply le_trans, apply min_le_left, apply min_le_left }, { apply le_min, apply le_trans, apply min_le_left, apply min_le_right, apply min_le_right }, { intros d h₁ h₂, apply le_min, apply le_min h₁, apply le_trans h₂, apply min_le_left, apply le_trans h₂, apply min_le_right } end lemma min_left_comm : ∀ (a b c : α), min a (min b c) = min b (min a c) := left_comm (@min α _) (@min_comm α _) (@min_assoc α _) @[simp] lemma min_self (a : α) : min a a = a := by min_tac a a @[ematch] lemma min_eq_left {a b : α} (h : a ≤ b) : min a b = a := begin apply eq.symm, apply eq_min (le_refl _) h, intros, assumption end @[ematch] lemma min_eq_right {a b : α} (h : b ≤ a) : min a b = b := eq.subst (min_comm b a) (min_eq_left h) lemma eq_max {a b c : α} (h₁ : a ≤ c) (h₂ : b ≤ c) (h₃ : ∀{d}, a ≤ d → b ≤ d → c ≤ d) : c = max a b := le_antisymm (h₃ (le_max_left a b) (le_max_right a b)) (max_le h₁ h₂) lemma max_comm (a b : α) : max a b = max b a := eq_max (le_max_right a b) (le_max_left a b) (λ c h₁ h₂, max_le h₂ h₁) lemma max_assoc (a b c : α) : max (max a b) c = max a (max b c) := begin apply eq_max, { apply le_trans, apply le_max_left a b, apply le_max_left }, { apply max_le, apply le_trans, apply le_max_right a b, apply le_max_left, apply le_max_right }, { intros d h₁ h₂, apply max_le, apply max_le h₁, apply le_trans (le_max_left _ _) h₂, apply le_trans (le_max_right _ _) h₂} end lemma max_left_comm : ∀ (a b c : α), max a (max b c) = max b (max a c) := left_comm (@max α _) (@max_comm α _) (@max_assoc α _) @[simp] lemma max_self (a : α) : max a a = a := by min_tac a a lemma max_eq_left {a b : α} (h : b ≤ a) : max a b = a := begin apply eq.symm, apply eq_max (le_refl _) h, intros, assumption end lemma max_eq_right {a b : α} (h : a ≤ b) : max a b = b := eq.subst (max_comm b a) (max_eq_left h) /- these rely on lt_of_lt -/ lemma min_eq_left_of_lt {a b : α} (h : a < b) : min a b = a := min_eq_left (le_of_lt h) lemma min_eq_right_of_lt {a b : α} (h : b < a) : min a b = b := min_eq_right (le_of_lt h) lemma max_eq_left_of_lt {a b : α} (h : b < a) : max a b = a := max_eq_left (le_of_lt h) lemma max_eq_right_of_lt {a b : α} (h : a < b) : max a b = b := max_eq_right (le_of_lt h) /- these use the fact that it is a linear ordering -/ lemma lt_min {a b c : α} (h₁ : a < b) (h₂ : a < c) : a < min b c := or.elim (le_or_gt b c) (assume h : b ≤ c, by min_tac b c) (assume h : b > c, by min_tac b c) lemma max_lt {a b c : α} (h₁ : a < c) (h₂ : b < c) : max a b < c := or.elim (le_or_gt a b) (assume h : a ≤ b, by min_tac b a) (assume h : a > b, by min_tac b a) end