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
813e82d4962be85c84516eb6e7742f044c081dc2
e5169dbb8b1bea3ec2a32737442bc91a4a94b46a
/library/data/list/basic.lean
619c23684cc3bcd45cf4a369ea44a6cf9dd2cfe2
[ "Apache-2.0" ]
permissive
pazthor/lean
733b775e3123f6bbd2c4f7ccb5b560b467b76800
c923120db54276a22a75b12c69765765608a8e76
refs/heads/master
1,610,703,744,289
1,448,419,395,000
1,448,419,703,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
28,394
lean
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn Basic properties of lists. -/ import logic tools.helper_tactics data.nat.order open eq.ops helper_tactics nat prod function option open algebra inductive list (T : Type) : Type := | nil {} : list T | cons : T → list T → list T protected definition list.is_inhabited [instance] (A : Type) : inhabited (list A) := inhabited.mk list.nil namespace list notation h :: t := cons h t notation `[` l:(foldr `, ` (h t, cons h t) nil `]`) := l variable {T : Type} lemma cons_ne_nil [simp] (a : T) (l : list T) : a::l ≠ [] := by contradiction lemma head_eq_of_cons_eq {A : Type} {h₁ h₂ : A} {t₁ t₂ : list A} : (h₁::t₁) = (h₂::t₂) → h₁ = h₂ := assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pheq) lemma tail_eq_of_cons_eq {A : Type} {h₁ h₂ : A} {t₁ t₂ : list A} : (h₁::t₁) = (h₂::t₂) → t₁ = t₂ := assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pteq) lemma cons_inj {A : Type} {a : A} : injective (cons a) := take l₁ l₂, assume Pe, tail_eq_of_cons_eq Pe /- append -/ definition append : list T → list T → list T | [] l := l | (h :: s) t := h :: (append s t) notation l₁ ++ l₂ := append l₁ l₂ theorem append_nil_left [simp] (t : list T) : [] ++ t = t theorem append_cons [simp] (x : T) (s t : list T) : (x::s) ++ t = x::(s ++ t) theorem append_nil_right [simp] : ∀ (t : list T), t ++ [] = t | [] := rfl | (a :: l) := calc (a :: l) ++ [] = a :: (l ++ []) : rfl ... = a :: l : append_nil_right l theorem append.assoc [simp] : ∀ (s t u : list T), s ++ t ++ u = s ++ (t ++ u) | [] t u := rfl | (a :: l) t u := show a :: (l ++ t ++ u) = (a :: l) ++ (t ++ u), by rewrite (append.assoc l t u) /- length -/ definition length : list T → nat | [] := 0 | (a :: l) := length l + 1 theorem length_nil [simp] : length (@nil T) = 0 theorem length_cons [simp] (x : T) (t : list T) : length (x::t) = length t + 1 theorem length_append [simp] : ∀ (s t : list T), length (s ++ t) = length s + length t | [] t := calc length ([] ++ t) = length t : rfl ... = length [] + length t : by rewrite [length_nil, zero_add] | (a :: s) t := calc length (a :: s ++ t) = length (s ++ t) + 1 : rfl ... = length s + length t + 1 : length_append ... = (length s + 1) + length t : succ_add ... = length (a :: s) + length t : rfl theorem eq_nil_of_length_eq_zero : ∀ {l : list T}, length l = 0 → l = [] | [] H := rfl | (a::s) H := by contradiction theorem ne_nil_of_length_eq_succ : ∀ {l : list T} {n : nat}, length l = succ n → l ≠ [] | [] n h := by contradiction | (a::l) n h := by contradiction -- add_rewrite length_nil length_cons /- concat -/ definition concat : Π (x : T), list T → list T | a [] := [a] | a (b :: l) := b :: concat a l theorem concat_nil [simp] (x : T) : concat x [] = [x] theorem concat_cons [simp] (x y : T) (l : list T) : concat x (y::l) = y::(concat x l) theorem concat_eq_append (a : T) : ∀ (l : list T), concat a l = l ++ [a] | [] := rfl | (b :: l) := show b :: (concat a l) = (b :: l) ++ (a :: []), by rewrite concat_eq_append theorem concat_ne_nil [simp] (a : T) : ∀ (l : list T), concat a l ≠ [] := by intro l; induction l; repeat contradiction theorem length_concat [simp] (a : T) : ∀ (l : list T), length (concat a l) = length l + 1 | [] := rfl | (x::xs) := by rewrite [concat_cons, *length_cons, length_concat] theorem concat_append (a : T) : ∀ (l₁ l₂ : list T), concat a l₁ ++ l₂ = l₁ ++ a :: l₂ | [] := λl₂, rfl | (x::xs) := λl₂, begin rewrite [concat_cons,append_cons, concat_append] end theorem append_concat (a : T) : ∀(l₁ l₂ : list T), l₁ ++ concat a l₂ = concat a (l₁ ++ l₂) | [] := λl₂, rfl | (x::xs) := λl₂, begin rewrite [+append_cons, concat_cons, append_concat] end /- last -/ definition last : Π l : list T, l ≠ [] → T | [] h := absurd rfl h | [a] h := a | (a₁::a₂::l) h := last (a₂::l) !cons_ne_nil lemma last_singleton [simp] (a : T) (h : [a] ≠ []) : last [a] h = a := rfl lemma last_cons_cons [simp] (a₁ a₂ : T) (l : list T) (h : a₁::a₂::l ≠ []) : last (a₁::a₂::l) h = last (a₂::l) !cons_ne_nil := rfl theorem last_congr {l₁ l₂ : list T} (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) (h₃ : l₁ = l₂) : last l₁ h₁ = last l₂ h₂ := by subst l₁ theorem last_concat [simp] {x : T} : ∀ {l : list T} (h : concat x l ≠ []), last (concat x l) h = x | [] h := rfl | [a] h := rfl | (a₁::a₂::l) h := begin change last (a₁::a₂::concat x l) !cons_ne_nil = x, rewrite last_cons_cons, change last (concat x (a₂::l)) !concat_ne_nil = x, apply last_concat end -- add_rewrite append_nil append_cons /- reverse -/ definition reverse : list T → list T | [] := [] | (a :: l) := concat a (reverse l) theorem reverse_nil [simp] : reverse (@nil T) = [] theorem reverse_cons [simp] (x : T) (l : list T) : reverse (x::l) = concat x (reverse l) theorem reverse_singleton [simp] (x : T) : reverse [x] = [x] theorem reverse_append [simp] : ∀ (s t : list T), reverse (s ++ t) = (reverse t) ++ (reverse s) | [] t2 := calc reverse ([] ++ t2) = reverse t2 : rfl ... = (reverse t2) ++ [] : append_nil_right ... = (reverse t2) ++ (reverse []) : by rewrite reverse_nil | (a2 :: s2) t2 := calc reverse ((a2 :: s2) ++ t2) = concat a2 (reverse (s2 ++ t2)) : rfl ... = concat a2 (reverse t2 ++ reverse s2) : reverse_append ... = (reverse t2 ++ reverse s2) ++ [a2] : concat_eq_append ... = reverse t2 ++ (reverse s2 ++ [a2]) : append.assoc ... = reverse t2 ++ concat a2 (reverse s2) : concat_eq_append ... = reverse t2 ++ reverse (a2 :: s2) : rfl theorem reverse_reverse [simp] : ∀ (l : list T), reverse (reverse l) = l | [] := rfl | (a :: l) := calc reverse (reverse (a :: l)) = reverse (concat a (reverse l)) : rfl ... = reverse (reverse l ++ [a]) : concat_eq_append ... = reverse [a] ++ reverse (reverse l) : reverse_append ... = reverse [a] ++ l : reverse_reverse ... = a :: l : rfl theorem concat_eq_reverse_cons (x : T) (l : list T) : concat x l = reverse (x :: reverse l) := calc concat x l = concat x (reverse (reverse l)) : reverse_reverse ... = reverse (x :: reverse l) : rfl theorem length_reverse : ∀ (l : list T), length (reverse l) = length l | [] := rfl | (x::xs) := begin unfold reverse, rewrite [length_concat, length_cons, length_reverse] end /- head and tail -/ definition head [h : inhabited T] : list T → T | [] := arbitrary T | (a :: l) := a theorem head_cons [simp] [h : inhabited T] (a : T) (l : list T) : head (a::l) = a theorem head_append [simp] [h : inhabited T] (t : list T) : ∀ {s : list T}, s ≠ [] → head (s ++ t) = head s | [] H := absurd rfl H | (a :: s) H := show head (a :: (s ++ t)) = head (a :: s), by rewrite head_cons definition tail : list T → list T | [] := [] | (a :: l) := l theorem tail_nil [simp] : tail (@nil T) = [] theorem tail_cons [simp] (a : T) (l : list T) : tail (a::l) = l theorem cons_head_tail [h : inhabited T] {l : list T} : l ≠ [] → (head l)::(tail l) = l := list.cases_on l (suppose [] ≠ [], absurd rfl this) (take x l, suppose x::l ≠ [], rfl) /- list membership -/ definition mem : T → list T → Prop | a [] := false | a (b :: l) := a = b ∨ mem a l notation e ∈ s := mem e s notation e ∉ s := ¬ e ∈ s theorem mem_nil_iff [simp] (x : T) : x ∈ [] ↔ false := iff.rfl theorem not_mem_nil (x : T) : x ∉ [] := iff.mp !mem_nil_iff theorem mem_cons [simp] (x : T) (l : list T) : x ∈ x :: l := or.inl rfl theorem mem_cons_of_mem (y : T) {x : T} {l : list T} : x ∈ l → x ∈ y :: l := assume H, or.inr H theorem mem_cons_iff (x y : T) (l : list T) : x ∈ y::l ↔ (x = y ∨ x ∈ l) := iff.rfl theorem eq_or_mem_of_mem_cons {x y : T} {l : list T} : x ∈ y::l → x = y ∨ x ∈ l := assume h, h theorem mem_singleton {x a : T} : x ∈ [a] → x = a := suppose x ∈ [a], or.elim (eq_or_mem_of_mem_cons this) (suppose x = a, this) (suppose x ∈ [], absurd this !not_mem_nil) theorem mem_of_mem_cons_of_mem {a b : T} {l : list T} : a ∈ b::l → b ∈ l → a ∈ l := assume ainbl binl, or.elim (eq_or_mem_of_mem_cons ainbl) (suppose a = b, by substvars; exact binl) (suppose a ∈ l, this) theorem mem_or_mem_of_mem_append {x : T} {s t : list T} : x ∈ s ++ t → x ∈ s ∨ x ∈ t := list.induction_on s or.inr (take y s, assume IH : x ∈ s ++ t → x ∈ s ∨ x ∈ t, suppose x ∈ y::s ++ t, have x = y ∨ x ∈ s ++ t, from this, have x = y ∨ x ∈ s ∨ x ∈ t, from or_of_or_of_imp_right this IH, iff.elim_right or.assoc this) theorem mem_append_of_mem_or_mem {x : T} {s t : list T} : x ∈ s ∨ x ∈ t → x ∈ s ++ t := list.induction_on s (take H, or.elim H false.elim (assume H, H)) (take y s, assume IH : x ∈ s ∨ x ∈ t → x ∈ s ++ t, suppose x ∈ y::s ∨ x ∈ t, or.elim this (suppose x ∈ y::s, or.elim (eq_or_mem_of_mem_cons this) (suppose x = y, or.inl this) (suppose x ∈ s, or.inr (IH (or.inl this)))) (suppose x ∈ t, or.inr (IH (or.inr this)))) theorem mem_append_iff (x : T) (s t : list T) : x ∈ s ++ t ↔ x ∈ s ∨ x ∈ t := iff.intro mem_or_mem_of_mem_append mem_append_of_mem_or_mem theorem not_mem_of_not_mem_append_left {x : T} {s t : list T} : x ∉ s++t → x ∉ s := λ nxinst xins, absurd (mem_append_of_mem_or_mem (or.inl xins)) nxinst theorem not_mem_of_not_mem_append_right {x : T} {s t : list T} : x ∉ s++t → x ∉ t := λ nxinst xint, absurd (mem_append_of_mem_or_mem (or.inr xint)) nxinst theorem not_mem_append {x : T} {s t : list T} : x ∉ s → x ∉ t → x ∉ s++t := λ nxins nxint xinst, or.elim (mem_or_mem_of_mem_append xinst) (λ xins, by contradiction) (λ xint, by contradiction) lemma length_pos_of_mem {a : T} : ∀ {l : list T}, a ∈ l → 0 < length l | [] := assume Pinnil, by contradiction | (b::l) := assume Pin, !zero_lt_succ local attribute mem [reducible] local attribute append [reducible] theorem mem_split {x : T} {l : list T} : x ∈ l → ∃s t : list T, l = s ++ (x::t) := list.induction_on l (suppose x ∈ [], false.elim (iff.elim_left !mem_nil_iff this)) (take y l, assume IH : x ∈ l → ∃s t : list T, l = s ++ (x::t), suppose x ∈ y::l, or.elim (eq_or_mem_of_mem_cons this) (suppose x = y, exists.intro [] (!exists.intro (this ▸ rfl))) (suppose x ∈ l, obtain s (H2 : ∃t : list T, l = s ++ (x::t)), from IH this, obtain t (H3 : l = s ++ (x::t)), from H2, have y :: l = (y::s) ++ (x::t), from H3 ▸ rfl, !exists.intro (!exists.intro this))) theorem mem_append_left {a : T} {l₁ : list T} (l₂ : list T) : a ∈ l₁ → a ∈ l₁ ++ l₂ := assume ainl₁, mem_append_of_mem_or_mem (or.inl ainl₁) theorem mem_append_right {a : T} (l₁ : list T) {l₂ : list T} : a ∈ l₂ → a ∈ l₁ ++ l₂ := assume ainl₂, mem_append_of_mem_or_mem (or.inr ainl₂) definition decidable_mem [instance] [H : decidable_eq T] (x : T) (l : list T) : decidable (x ∈ l) := list.rec_on l (decidable.inr (not_of_iff_false !mem_nil_iff)) (take (h : T) (l : list T) (iH : decidable (x ∈ l)), show decidable (x ∈ h::l), from decidable.rec_on iH (assume Hp : x ∈ l, decidable.rec_on (H x h) (suppose x = h, decidable.inl (or.inl this)) (suppose x ≠ h, decidable.inl (or.inr Hp))) (suppose ¬x ∈ l, decidable.rec_on (H x h) (suppose x = h, decidable.inl (or.inl this)) (suppose x ≠ h, have ¬(x = h ∨ x ∈ l), from suppose x = h ∨ x ∈ l, or.elim this (suppose x = h, by contradiction) (suppose x ∈ l, by contradiction), have ¬x ∈ h::l, from iff.elim_right (not_iff_not_of_iff !mem_cons_iff) this, decidable.inr this))) theorem mem_of_ne_of_mem {x y : T} {l : list T} (H₁ : x ≠ y) (H₂ : x ∈ y :: l) : x ∈ l := or.elim (eq_or_mem_of_mem_cons H₂) (λe, absurd e H₁) (λr, r) theorem ne_of_not_mem_cons {a b : T} {l : list T} : a ∉ b::l → a ≠ b := assume nin aeqb, absurd (or.inl aeqb) nin theorem not_mem_of_not_mem_cons {a b : T} {l : list T} : a ∉ b::l → a ∉ l := assume nin nainl, absurd (or.inr nainl) nin lemma not_mem_cons_of_ne_of_not_mem {x y : T} {l : list T} : x ≠ y → x ∉ l → x ∉ y::l := assume P1 P2, not.intro (assume Pxin, absurd (eq_or_mem_of_mem_cons Pxin) (not_or P1 P2)) lemma ne_and_not_mem_of_not_mem_cons {x y : T} {l : list T} : x ∉ y::l → x ≠ y ∧ x ∉ l := assume P, and.intro (ne_of_not_mem_cons P) (not_mem_of_not_mem_cons P) definition sublist (l₁ l₂ : list T) := ∀ ⦃a : T⦄, a ∈ l₁ → a ∈ l₂ infix ⊆ := sublist theorem nil_sub [simp] (l : list T) : [] ⊆ l := λ b i, false.elim (iff.mp (mem_nil_iff b) i) theorem sub.refl [simp] (l : list T) : l ⊆ l := λ b i, i theorem sub.trans {l₁ l₂ l₃ : list T} (H₁ : l₁ ⊆ l₂) (H₂ : l₂ ⊆ l₃) : l₁ ⊆ l₃ := λ b i, H₂ (H₁ i) theorem sub_cons [simp] (a : T) (l : list T) : l ⊆ a::l := λ b i, or.inr i theorem sub_of_cons_sub {a : T} {l₁ l₂ : list T} : a::l₁ ⊆ l₂ → l₁ ⊆ l₂ := λ s b i, s b (mem_cons_of_mem _ i) theorem cons_sub_cons {l₁ l₂ : list T} (a : T) (s : l₁ ⊆ l₂) : (a::l₁) ⊆ (a::l₂) := λ b Hin, or.elim (eq_or_mem_of_mem_cons Hin) (λ e : b = a, or.inl e) (λ i : b ∈ l₁, or.inr (s i)) theorem sub_append_left [simp] (l₁ l₂ : list T) : l₁ ⊆ l₁++l₂ := λ b i, iff.mpr (mem_append_iff b l₁ l₂) (or.inl i) theorem sub_append_right [simp] (l₁ l₂ : list T) : l₂ ⊆ l₁++l₂ := λ b i, iff.mpr (mem_append_iff b l₁ l₂) (or.inr i) theorem sub_cons_of_sub (a : T) {l₁ l₂ : list T} : l₁ ⊆ l₂ → l₁ ⊆ (a::l₂) := λ (s : l₁ ⊆ l₂) (x : T) (i : x ∈ l₁), or.inr (s i) theorem sub_app_of_sub_left (l l₁ l₂ : list T) : l ⊆ l₁ → l ⊆ l₁++l₂ := λ (s : l ⊆ l₁) (x : T) (xinl : x ∈ l), have x ∈ l₁, from s xinl, mem_append_of_mem_or_mem (or.inl this) theorem sub_app_of_sub_right (l l₁ l₂ : list T) : l ⊆ l₂ → l ⊆ l₁++l₂ := λ (s : l ⊆ l₂) (x : T) (xinl : x ∈ l), have x ∈ l₂, from s xinl, mem_append_of_mem_or_mem (or.inr this) theorem cons_sub_of_sub_of_mem {a : T} {l m : list T} : a ∈ m → l ⊆ m → a::l ⊆ m := λ (ainm : a ∈ m) (lsubm : l ⊆ m) (x : T) (xinal : x ∈ a::l), or.elim (eq_or_mem_of_mem_cons xinal) (suppose x = a, by substvars; exact ainm) (suppose x ∈ l, lsubm this) theorem app_sub_of_sub_of_sub {l₁ l₂ l : list T} : l₁ ⊆ l → l₂ ⊆ l → l₁++l₂ ⊆ l := λ (l₁subl : l₁ ⊆ l) (l₂subl : l₂ ⊆ l) (x : T) (xinl₁l₂ : x ∈ l₁++l₂), or.elim (mem_or_mem_of_mem_append xinl₁l₂) (suppose x ∈ l₁, l₁subl this) (suppose x ∈ l₂, l₂subl this) /- find -/ section variable [H : decidable_eq T] include H definition find : T → list T → nat | a [] := 0 | a (b :: l) := if a = b then 0 else succ (find a l) theorem find_nil [simp] (x : T) : find x [] = 0 theorem find_cons (x y : T) (l : list T) : find x (y::l) = if x = y then 0 else succ (find x l) theorem find_cons_of_eq {x y : T} (l : list T) : x = y → find x (y::l) = 0 := assume e, if_pos e theorem find_cons_of_ne {x y : T} (l : list T) : x ≠ y → find x (y::l) = succ (find x l) := assume n, if_neg n theorem find_of_not_mem {l : list T} {x : T} : ¬x ∈ l → find x l = length l := list.rec_on l (suppose ¬x ∈ [], _) (take y l, assume iH : ¬x ∈ l → find x l = length l, suppose ¬x ∈ y::l, have ¬(x = y ∨ x ∈ l), from iff.elim_right (not_iff_not_of_iff !mem_cons_iff) this, have ¬x = y ∧ ¬x ∈ l, from (iff.elim_left not_or_iff_not_and_not this), calc find x (y::l) = if x = y then 0 else succ (find x l) : !find_cons ... = succ (find x l) : if_neg (and.elim_left this) ... = succ (length l) : {iH (and.elim_right this)} ... = length (y::l) : !length_cons⁻¹) lemma find_le_length : ∀ {a} {l : list T}, find a l ≤ length l | a [] := !le.refl | a (b::l) := decidable.rec_on (H a b) (assume Peq, by rewrite [find_cons_of_eq l Peq]; exact !zero_le) (assume Pne, begin rewrite [find_cons_of_ne l Pne, length_cons], apply succ_le_succ, apply find_le_length end) lemma not_mem_of_find_eq_length : ∀ {a} {l : list T}, find a l = length l → a ∉ l | a [] := assume Peq, !not_mem_nil | a (b::l) := decidable.rec_on (H a b) (assume Peq, by rewrite [find_cons_of_eq l Peq, length_cons]; contradiction) (assume Pne, begin rewrite [find_cons_of_ne l Pne, length_cons, mem_cons_iff], intro Plen, apply (not_or Pne), exact not_mem_of_find_eq_length (succ.inj Plen) end) lemma find_lt_length {a} {l : list T} (Pin : a ∈ l) : find a l < length l := begin apply nat.lt_of_le_and_ne, apply find_le_length, apply not.intro, intro Peq, exact absurd Pin (not_mem_of_find_eq_length Peq) end end /- nth element -/ section nth definition nth : list T → nat → option T | [] n := none | (a :: l) 0 := some a | (a :: l) (n+1) := nth l n theorem nth_zero [simp] (a : T) (l : list T) : nth (a :: l) 0 = some a theorem nth_succ [simp] (a : T) (l : list T) (n : nat) : nth (a::l) (succ n) = nth l n theorem nth_eq_some : ∀ {l : list T} {n : nat}, n < length l → Σ a : T, nth l n = some a | [] n h := absurd h !not_lt_zero | (a::l) 0 h := ⟨a, rfl⟩ | (a::l) (succ n) h := have n < length l, from lt_of_succ_lt_succ h, obtain (r : T) (req : nth l n = some r), from nth_eq_some this, ⟨r, by rewrite [nth_succ, req]⟩ open decidable theorem find_nth [h : decidable_eq T] {a : T} : ∀ {l}, a ∈ l → nth l (find a l) = some a | [] ain := absurd ain !not_mem_nil | (b::l) ainbl := by_cases (λ aeqb : a = b, by rewrite [find_cons_of_eq _ aeqb, nth_zero, aeqb]) (λ aneb : a ≠ b, or.elim (eq_or_mem_of_mem_cons ainbl) (λ aeqb : a = b, absurd aeqb aneb) (λ ainl : a ∈ l, by rewrite [find_cons_of_ne _ aneb, nth_succ, find_nth ainl])) definition inth [h : inhabited T] (l : list T) (n : nat) : T := match nth l n with | some a := a | none := arbitrary T end theorem inth_zero [h : inhabited T] (a : T) (l : list T) : inth (a :: l) 0 = a theorem inth_succ [h : inhabited T] (a : T) (l : list T) (n : nat) : inth (a::l) (n+1) = inth l n end nth section ith definition ith : Π (l : list T) (i : nat), i < length l → T | nil i h := absurd h !not_lt_zero | (x::xs) 0 h := x | (x::xs) (succ i) h := ith xs i (lt_of_succ_lt_succ h) lemma ith_zero [simp] (a : T) (l : list T) (h : 0 < length (a::l)) : ith (a::l) 0 h = a := rfl lemma ith_succ [simp] (a : T) (l : list T) (i : nat) (h : succ i < length (a::l)) : ith (a::l) (succ i) h = ith l i (lt_of_succ_lt_succ h) := rfl end ith open decidable definition has_decidable_eq {A : Type} [H : decidable_eq A] : ∀ l₁ l₂ : list A, decidable (l₁ = l₂) | [] [] := inl rfl | [] (b::l₂) := inr (by contradiction) | (a::l₁) [] := inr (by contradiction) | (a::l₁) (b::l₂) := match H a b with | inl Hab := match has_decidable_eq l₁ l₂ with | inl He := inl (by congruence; repeat assumption) | inr Hn := inr (by intro H; injection H; contradiction) end | inr Hnab := inr (by intro H; injection H; contradiction) end /- quasiequal a l l' means that l' is exactly l, with a added once somewhere -/ section qeq variable {A : Type} inductive qeq (a : A) : list A → list A → Prop := | qhead : ∀ l, qeq a l (a::l) | qcons : ∀ (b : A) {l l' : list A}, qeq a l l' → qeq a (b::l) (b::l') open qeq notation l' `≈`:50 a `|` l:50 := qeq a l l' theorem qeq_app : ∀ (l₁ : list A) (a : A) (l₂ : list A), l₁++(a::l₂) ≈ a|l₁++l₂ | [] a l₂ := qhead a l₂ | (x::xs) a l₂ := qcons x (qeq_app xs a l₂) theorem mem_head_of_qeq {a : A} {l₁ l₂ : list A} : l₁≈a|l₂ → a ∈ l₁ := take q, qeq.induction_on q (λ l, !mem_cons) (λ b l l' q r, or.inr r) theorem mem_tail_of_qeq {a : A} {l₁ l₂ : list A} : l₁≈a|l₂ → ∀ x, x ∈ l₂ → x ∈ l₁ := take q, qeq.induction_on q (λ l x i, or.inr i) (λ b l l' q r x xinbl, or.elim (eq_or_mem_of_mem_cons xinbl) (λ xeqb : x = b, xeqb ▸ mem_cons x l') (λ xinl : x ∈ l, or.inr (r x xinl))) theorem mem_cons_of_qeq {a : A} {l₁ l₂ : list A} : l₁≈a|l₂ → ∀ x, x ∈ l₁ → x ∈ a::l₂ := take q, qeq.induction_on q (λ l x i, i) (λ b l l' q r x xinbl', or.elim (eq_or_mem_of_mem_cons xinbl') (λ xeqb : x = b, xeqb ▸ or.inr (mem_cons x l)) (λ xinl' : x ∈ l', or.elim (eq_or_mem_of_mem_cons (r x xinl')) (λ xeqa : x = a, xeqa ▸ mem_cons x (b::l)) (λ xinl : x ∈ l, or.inr (or.inr xinl)))) theorem length_eq_of_qeq {a : A} {l₁ l₂ : list A} : l₁≈a|l₂ → length l₁ = succ (length l₂) := take q, qeq.induction_on q (λ l, rfl) (λ b l l' q r, by rewrite [*length_cons, r]) theorem qeq_of_mem {a : A} {l : list A} : a ∈ l → (∃l', l≈a|l') := list.induction_on l (λ h : a ∈ nil, absurd h (not_mem_nil a)) (λ x xs r ainxxs, or.elim (eq_or_mem_of_mem_cons ainxxs) (λ aeqx : a = x, assert aux : ∃ l, x::xs≈x|l, from exists.intro xs (qhead x xs), by rewrite aeqx; exact aux) (λ ainxs : a ∈ xs, have ∃l', xs ≈ a|l', from r ainxs, obtain (l' : list A) (q : xs ≈ a|l'), from this, have x::xs ≈ a | x::l', from qcons x q, exists.intro (x::l') this)) theorem qeq_split {a : A} {l l' : list A} : l'≈a|l → ∃l₁ l₂, l = l₁++l₂ ∧ l' = l₁++(a::l₂) := take q, qeq.induction_on q (λ t, have t = []++t ∧ a::t = []++(a::t), from and.intro rfl rfl, exists.intro [] (exists.intro t this)) (λ b t t' q r, obtain (l₁ l₂ : list A) (h : t = l₁++l₂ ∧ t' = l₁++(a::l₂)), from r, have b::t = (b::l₁)++l₂ ∧ b::t' = (b::l₁)++(a::l₂), begin rewrite [and.elim_right h, and.elim_left h], constructor, repeat reflexivity end, exists.intro (b::l₁) (exists.intro l₂ this)) theorem sub_of_mem_of_sub_of_qeq {a : A} {l : list A} {u v : list A} : a ∉ l → a::l ⊆ v → v≈a|u → l ⊆ u := λ (nainl : a ∉ l) (s : a::l ⊆ v) (q : v≈a|u) (x : A) (xinl : x ∈ l), have x ∈ v, from s (or.inr xinl), have x ∈ a::u, from mem_cons_of_qeq q x this, or.elim (eq_or_mem_of_mem_cons this) (suppose x = a, by substvars; contradiction) (suppose x ∈ u, this) end qeq section firstn variable {A : Type} definition firstn : nat → list A → list A | 0 l := [] | (n+1) [] := [] | (n+1) (a::l) := a :: firstn n l lemma firstn_zero : ∀ (l : list A), firstn 0 l = [] := by intros; reflexivity lemma firstn_nil : ∀ n, firstn n [] = ([] : list A) | 0 := rfl | (n+1) := rfl lemma firstn_cons : ∀ n (a : A) (l : list A), firstn (succ n) (a::l) = a :: firstn n l := by intros; reflexivity lemma firstn_all : ∀ (l : list A), firstn (length l) l = l | [] := rfl | (a::l) := begin unfold [length, firstn], rewrite firstn_all end lemma firstn_all_of_ge : ∀ {n} {l : list A}, n ≥ length l → firstn n l = l | 0 [] h := rfl | 0 (a::l) h := absurd h (not_le_of_gt !succ_pos) | (n+1) [] h := rfl | (n+1) (a::l) h := begin unfold firstn, rewrite [firstn_all_of_ge (le_of_succ_le_succ h)] end lemma firstn_firstn : ∀ (n m) (l : list A), firstn n (firstn m l) = firstn (min n m) l | n 0 l := by rewrite [min_zero, firstn_zero, firstn_nil] | 0 m l := by rewrite [zero_min] | (succ n) (succ m) nil := by rewrite [*firstn_nil] | (succ n) (succ m) (a::l) := by rewrite [*firstn_cons, firstn_firstn, min_succ_succ] lemma length_firstn_le : ∀ (n) (l : list A), length (firstn n l) ≤ n | 0 l := by rewrite [firstn_zero] | (succ n) (a::l) := by rewrite [firstn_cons, length_cons, add_one]; apply succ_le_succ; apply length_firstn_le | (succ n) [] := by rewrite [firstn_nil, length_nil]; apply zero_le lemma length_firstn_eq : ∀ (n) (l : list A), length (firstn n l) = min n (length l) | 0 l := by rewrite [firstn_zero, zero_min] | (succ n) (a::l) := by rewrite [firstn_cons, *length_cons, *add_one, min_succ_succ, length_firstn_eq] | (succ n) [] := by rewrite [firstn_nil] end firstn section count variable {A : Type} variable [decA : decidable_eq A] include decA definition count (a : A) : list A → nat | [] := 0 | (x::xs) := if a = x then succ (count xs) else count xs lemma count_nil (a : A) : count a [] = 0 := rfl lemma count_cons (a b : A) (l : list A) : count a (b::l) = if a = b then succ (count a l) else count a l := rfl lemma count_cons_eq (a : A) (l : list A) : count a (a::l) = succ (count a l) := if_pos rfl lemma count_cons_of_ne {a b : A} (h : a ≠ b) (l : list A) : count a (b::l) = count a l := if_neg h lemma count_cons_ge_count (a b : A) (l : list A) : count a (b::l) ≥ count a l := by_cases (suppose a = b, begin subst b, rewrite count_cons_eq, apply le_succ end) (suppose a ≠ b, begin rewrite (count_cons_of_ne this), apply le.refl end) lemma count_singleton (a : A) : count a [a] = 1 := by rewrite count_cons_eq lemma count_append (a : A) : ∀ l₁ l₂, count a (l₁++l₂) = count a l₁ + count a l₂ | [] l₂ := by rewrite [append_nil_left, count_nil, zero_add] | (b::l₁) l₂ := by_cases (suppose a = b, by rewrite [-this, append_cons, *count_cons_eq, succ_add, count_append]) (suppose a ≠ b, by rewrite [append_cons, *count_cons_of_ne this, count_append]) lemma count_concat (a : A) (l : list A) : count a (concat a l) = succ (count a l) := by rewrite [concat_eq_append, count_append, count_singleton] lemma mem_of_count_gt_zero : ∀ {a : A} {l : list A}, count a l > 0 → a ∈ l | a [] h := absurd h !lt.irrefl | a (b::l) h := by_cases (suppose a = b, begin subst b, apply mem_cons end) (suppose a ≠ b, have count a l > 0, by rewrite [count_cons_of_ne this at h]; exact h, have a ∈ l, from mem_of_count_gt_zero this, show a ∈ b::l, from mem_cons_of_mem _ this) lemma count_gt_zero_of_mem : ∀ {a : A} {l : list A}, a ∈ l → count a l > 0 | a [] h := absurd h !not_mem_nil | a (b::l) h := or.elim h (suppose a = b, begin subst b, rewrite count_cons_eq, apply zero_lt_succ end) (suppose a ∈ l, calc count a (b::l) ≥ count a l : count_cons_ge_count ... > 0 : count_gt_zero_of_mem this) lemma count_eq_zero_of_not_mem {a : A} {l : list A} (h : a ∉ l) : count a l = 0 := match count a l with | zero := suppose count a l = zero, this | (succ n) := suppose count a l = succ n, absurd (mem_of_count_gt_zero (begin rewrite this, exact dec_trivial end)) h end rfl end count end list attribute list.has_decidable_eq [instance] attribute list.decidable_mem [instance]
f9bd1580d4578c1f431f084690e20de42d7ea59d
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/field_theory/splitting_field.lean
ea41b0339a78875967c18067b1a86c0735f3932e
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
37,514
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import ring_theory.adjoin_root import ring_theory.algebra_tower import ring_theory.algebraic import ring_theory.polynomial import field_theory.minpoly import linear_algebra.finite_dimensional import tactic.field_simp import algebra.polynomial.big_operators /-! # Splitting fields This file introduces the notion of a splitting field of a polynomial and provides an embedding from a splitting field to any field that splits the polynomial. A polynomial `f : polynomial K` splits over a field extension `L` of `K` if it is zero or all of its irreducible factors over `L` have degree `1`. A field extension of `K` of a polynomial `f : polynomial K` is called a splitting field if it is the smallest field extension of `K` such that `f` splits. ## Main definitions * `polynomial.splits i f`: A predicate on a field homomorphism `i : K → L` and a polynomial `f` saying that `f` is zero or all of its irreducible factors over `L` have degree `1`. * `polynomial.splitting_field f`: A fixed splitting field of the polynomial `f`. * `polynomial.is_splitting_field`: A predicate on a field to be a splitting field of a polynomial `f`. ## Main statements * `polynomial.C_leading_coeff_mul_prod_multiset_X_sub_C`: If a polynomial has as many roots as its degree, it can be written as the product of its leading coefficient with `∏ (X - a)` where `a` ranges through its roots. * `lift_of_splits`: If `K` and `L` are field extensions of a field `F` and for some finite subset `S` of `K`, the minimal polynomial of every `x ∈ K` splits as a polynomial with coefficients in `L`, then `algebra.adjoin F S` embeds into `L`. * `polynomial.is_splitting_field.lift`: An embedding of a splitting field of the polynomial `f` into another field such that `f` splits. * `polynomial.is_splitting_field.alg_equiv`: Every splitting field of a polynomial `f` is isomorpic to `splitting_field f` and thus, being a splitting field is unique up to isomorphism. -/ noncomputable theory open_locale classical big_operators universes u v w variables {F : Type u} {K : Type v} {L : Type w} namespace polynomial variables [field K] [field L] [field F] open polynomial section splits variables (i : K →+* L) /-- A polynomial `splits` iff it is zero or all of its irreducible factors have `degree` 1. -/ def splits (f : polynomial K) : Prop := f = 0 ∨ ∀ {g : polynomial L}, irreducible g → g ∣ f.map i → degree g = 1 @[simp] lemma splits_zero : splits i (0 : polynomial K) := or.inl rfl @[simp] lemma splits_C (a : K) : splits i (C a) := if ha : a = 0 then ha.symm ▸ (@C_0 K _).symm ▸ splits_zero i else have hia : i a ≠ 0, from mt ((i.injective_iff).1 i.injective _) ha, or.inr $ λ g hg ⟨p, hp⟩, absurd hg.1 (not_not.2 (is_unit_iff_degree_eq_zero.2 $ by have := congr_arg degree hp; simp [degree_C hia, @eq_comm (with_bot ℕ) 0, nat.with_bot.add_eq_zero_iff] at this; clear _fun_match; tauto)) lemma splits_of_degree_eq_one {f : polynomial K} (hf : degree f = 1) : splits i f := or.inr $ λ g hg ⟨p, hp⟩, by have := congr_arg degree hp; simp [nat.with_bot.add_eq_one_iff, hf, @eq_comm (with_bot ℕ) 1, mt is_unit_iff_degree_eq_zero.2 hg.1] at this; clear _fun_match; tauto lemma splits_of_degree_le_one {f : polynomial K} (hf : degree f ≤ 1) : splits i f := begin cases h : degree f with n, { rw [degree_eq_bot.1 h]; exact splits_zero i }, { cases n with n, { rw [eq_C_of_degree_le_zero (trans_rel_right (≤) h (le_refl _))]; exact splits_C _ _ }, { have hn : n = 0, { rw h at hf, cases n, { refl }, { exact absurd hf dec_trivial } }, exact splits_of_degree_eq_one _ (by rw [h, hn]; refl) } } end lemma splits_of_nat_degree_le_one {f : polynomial K} (hf : nat_degree f ≤ 1) : splits i f := splits_of_degree_le_one i (degree_le_of_nat_degree_le hf) lemma splits_of_nat_degree_eq_one {f : polynomial K} (hf : nat_degree f = 1) : splits i f := splits_of_nat_degree_le_one i (le_of_eq hf) lemma splits_mul {f g : polynomial K} (hf : splits i f) (hg : splits i g) : splits i (f * g) := if h : f * g = 0 then by simp [h] else or.inr $ λ p hp hpf, ((principal_ideal_ring.irreducible_iff_prime.1 hp).2.2 _ _ (show p ∣ map i f * map i g, by convert hpf; rw polynomial.map_mul)).elim (hf.resolve_left (λ hf, by simpa [hf] using h) hp) (hg.resolve_left (λ hg, by simpa [hg] using h) hp) lemma splits_of_splits_mul {f g : polynomial K} (hfg : f * g ≠ 0) (h : splits i (f * g)) : splits i f ∧ splits i g := ⟨or.inr $ λ g hgi hg, or.resolve_left h hfg hgi (by rw map_mul; exact hg.trans (dvd_mul_right _ _)), or.inr $ λ g hgi hg, or.resolve_left h hfg hgi (by rw map_mul; exact hg.trans (dvd_mul_left _ _))⟩ lemma splits_of_splits_of_dvd {f g : polynomial K} (hf0 : f ≠ 0) (hf : splits i f) (hgf : g ∣ f) : splits i g := by { obtain ⟨f, rfl⟩ := hgf, exact (splits_of_splits_mul i hf0 hf).1 } lemma splits_of_splits_gcd_left {f g : polynomial K} (hf0 : f ≠ 0) (hf : splits i f) : splits i (euclidean_domain.gcd f g) := polynomial.splits_of_splits_of_dvd i hf0 hf (euclidean_domain.gcd_dvd_left f g) lemma splits_of_splits_gcd_right {f g : polynomial K} (hg0 : g ≠ 0) (hg : splits i g) : splits i (euclidean_domain.gcd f g) := polynomial.splits_of_splits_of_dvd i hg0 hg (euclidean_domain.gcd_dvd_right f g) lemma splits_map_iff (j : L →+* F) {f : polynomial K} : splits j (f.map i) ↔ splits (j.comp i) f := by simp [splits, polynomial.map_map] theorem splits_one : splits i 1 := splits_C i 1 theorem splits_of_is_unit {u : polynomial K} (hu : is_unit u) : u.splits i := splits_of_splits_of_dvd i one_ne_zero (splits_one _) $ is_unit_iff_dvd_one.1 hu theorem splits_X_sub_C {x : K} : (X - C x).splits i := splits_of_degree_eq_one _ $ degree_X_sub_C x theorem splits_X : X.splits i := splits_of_degree_eq_one _ $ degree_X theorem splits_id_iff_splits {f : polynomial K} : (f.map i).splits (ring_hom.id L) ↔ f.splits i := by rw [splits_map_iff, ring_hom.id_comp] theorem splits_mul_iff {f g : polynomial K} (hf : f ≠ 0) (hg : g ≠ 0) : (f * g).splits i ↔ f.splits i ∧ g.splits i := ⟨splits_of_splits_mul i (mul_ne_zero hf hg), λ ⟨hfs, hgs⟩, splits_mul i hfs hgs⟩ theorem splits_prod {ι : Type u} {s : ι → polynomial K} {t : finset ι} : (∀ j ∈ t, (s j).splits i) → (∏ x in t, s x).splits i := begin refine finset.induction_on t (λ _, splits_one i) (λ a t hat ih ht, _), rw finset.forall_mem_insert at ht, rw finset.prod_insert hat, exact splits_mul i ht.1 (ih ht.2) end lemma splits_pow {f : polynomial K} (hf : f.splits i) (n : ℕ) : (f ^ n).splits i := begin rw [←finset.card_range n, ←finset.prod_const], exact splits_prod i (λ j hj, hf), end lemma splits_X_pow (n : ℕ) : (X ^ n).splits i := splits_pow i (splits_X i) n theorem splits_prod_iff {ι : Type u} {s : ι → polynomial K} {t : finset ι} : (∀ j ∈ t, s j ≠ 0) → ((∏ x in t, s x).splits i ↔ ∀ j ∈ t, (s j).splits i) := begin refine finset.induction_on t (λ _, ⟨λ _ _ h, h.elim, λ _, splits_one i⟩) (λ a t hat ih ht, _), rw finset.forall_mem_insert at ht ⊢, rw [finset.prod_insert hat, splits_mul_iff i ht.1 (finset.prod_ne_zero_iff.2 ht.2), ih ht.2] end lemma degree_eq_one_of_irreducible_of_splits {p : polynomial L} (h_nz : p ≠ 0) (hp : irreducible p) (hp_splits : splits (ring_hom.id L) p) : p.degree = 1 := begin rcases hp_splits, { contradiction }, { apply hp_splits hp, simp } end lemma exists_root_of_splits {f : polynomial K} (hs : splits i f) (hf0 : degree f ≠ 0) : ∃ x, eval₂ i x f = 0 := if hf0 : f = 0 then ⟨37, by simp [hf0]⟩ else let ⟨g, hg⟩ := wf_dvd_monoid.exists_irreducible_factor (show ¬ is_unit (f.map i), from mt is_unit_iff_degree_eq_zero.1 (by rwa degree_map)) (map_ne_zero hf0) in let ⟨x, hx⟩ := exists_root_of_degree_eq_one (hs.resolve_left hf0 hg.1 hg.2) in let ⟨i, hi⟩ := hg.2 in ⟨x, by rw [← eval_map, hi, eval_mul, show _ = _, from hx, zero_mul]⟩ lemma exists_multiset_of_splits {f : polynomial K} : splits i f → ∃ (s : multiset L), f.map i = C (i f.leading_coeff) * (s.map (λ a : L, (X : polynomial L) - C a)).prod := suffices splits (ring_hom.id _) (f.map i) → ∃ s : multiset L, f.map i = (C (f.map i).leading_coeff) * (s.map (λ a : L, (X : polynomial L) - C a)).prod, by rwa [splits_map_iff, leading_coeff_map i] at this, wf_dvd_monoid.induction_on_irreducible (f.map i) (λ _, ⟨{37}, by simp [i.map_zero]⟩) (λ u hu _, ⟨0, by conv_lhs { rw eq_C_of_degree_eq_zero (is_unit_iff_degree_eq_zero.1 hu) }; simp [leading_coeff, nat_degree_eq_of_degree_eq_some (is_unit_iff_degree_eq_zero.1 hu)]⟩) (λ f p hf0 hp ih hfs, have hpf0 : p * f ≠ 0, from mul_ne_zero hp.ne_zero hf0, let ⟨s, hs⟩ := ih (splits_of_splits_mul _ hpf0 hfs).2 in ⟨-(p * norm_unit p).coeff 0 ::ₘ s, have hp1 : degree p = 1, from hfs.resolve_left hpf0 hp (by simp), begin rw [multiset.map_cons, multiset.prod_cons, leading_coeff_mul, C_mul, mul_assoc, mul_left_comm (C f.leading_coeff), ← hs, ← mul_assoc, mul_left_inj' hf0], conv_lhs {rw eq_X_add_C_of_degree_eq_one hp1}, simp only [mul_add, coe_norm_unit_of_ne_zero hp.ne_zero, mul_comm p, coeff_neg, C_neg, sub_eq_add_neg, neg_neg, coeff_C_mul, (mul_assoc _ _ _).symm, C_mul.symm, mul_inv_cancel (show p.leading_coeff ≠ 0, from mt leading_coeff_eq_zero.1 hp.ne_zero), one_mul], end⟩) /-- Pick a root of a polynomial that splits. -/ def root_of_splits {f : polynomial K} (hf : f.splits i) (hfd : f.degree ≠ 0) : L := classical.some $ exists_root_of_splits i hf hfd theorem map_root_of_splits {f : polynomial K} (hf : f.splits i) (hfd) : f.eval₂ i (root_of_splits i hf hfd) = 0 := classical.some_spec $ exists_root_of_splits i hf hfd theorem roots_map {f : polynomial K} (hf : f.splits $ ring_hom.id K) : (f.map i).roots = (f.roots).map i := if hf0 : f = 0 then by rw [hf0, map_zero, roots_zero, roots_zero, multiset.map_zero] else have hmf0 : f.map i ≠ 0 := map_ne_zero hf0, let ⟨m, hm⟩ := exists_multiset_of_splits _ hf in have h1 : (0 : polynomial K) ∉ m.map (λ r, X - C r), from zero_nmem_multiset_map_X_sub_C _ _, have h2 : (0 : polynomial L) ∉ m.map (λ r, X - C (i r)), from zero_nmem_multiset_map_X_sub_C _ _, begin rw map_id at hm, rw hm at hf0 hmf0 ⊢, rw map_mul at hmf0 ⊢, rw [roots_mul hf0, roots_mul hmf0, map_C, roots_C, zero_add, roots_C, zero_add, map_multiset_prod, multiset.map_map], simp_rw [(∘), map_sub, map_X, map_C], rw [roots_multiset_prod _ h2, multiset.bind_map, roots_multiset_prod _ h1, multiset.bind_map], simp_rw roots_X_sub_C, rw [multiset.bind_cons, multiset.bind_zero, add_zero, multiset.bind_cons, multiset.bind_zero, add_zero, multiset.map_id'] end lemma eq_prod_roots_of_splits {p : polynomial K} {i : K →+* L} (hsplit : splits i p) : p.map i = C (i p.leading_coeff) * ((p.map i).roots.map (λ a, X - C a)).prod := begin by_cases p_eq_zero : p = 0, { rw [p_eq_zero, map_zero, leading_coeff_zero, i.map_zero, C.map_zero, zero_mul] }, obtain ⟨s, hs⟩ := exists_multiset_of_splits i hsplit, have map_ne_zero : p.map i ≠ 0 := map_ne_zero (p_eq_zero), have prod_ne_zero : C (i p.leading_coeff) * (multiset.map (λ a, X - C a) s).prod ≠ 0 := by rwa hs at map_ne_zero, have zero_nmem : (0 : polynomial L) ∉ s.map (λ a, X - C a), from zero_nmem_multiset_map_X_sub_C _ _, have map_bind_roots_eq : (s.map (λ a, X - C a)).bind (λ a, a.roots) = s, { refine multiset.induction_on s (by rw [multiset.map_zero, multiset.zero_bind]) _, intros a s ih, rw [multiset.map_cons, multiset.cons_bind, ih, roots_X_sub_C, multiset.cons_add, zero_add] }, rw [hs, roots_mul prod_ne_zero, roots_C, zero_add, roots_multiset_prod _ zero_nmem, map_bind_roots_eq] end lemma eq_prod_roots_of_splits_id {p : polynomial K} (hsplit : splits (ring_hom.id K) p) : p = C (p.leading_coeff) * (p.roots.map (λ a, X - C a)).prod := by simpa using eq_prod_roots_of_splits hsplit lemma eq_prod_roots_of_monic_of_splits_id {p : polynomial K} (m : monic p) (hsplit : splits (ring_hom.id K) p) : p = (p.roots.map (λ a, X - C a)).prod := begin convert eq_prod_roots_of_splits_id hsplit, simp [m], end lemma eq_X_sub_C_of_splits_of_single_root {x : K} {h : polynomial K} (h_splits : splits i h) (h_roots : (h.map i).roots = {i x}) : h = (C (leading_coeff h)) * (X - C x) := begin apply polynomial.map_injective _ i.injective, rw [eq_prod_roots_of_splits h_splits, h_roots], simp, end lemma nat_degree_eq_card_roots {p : polynomial K} {i : K →+* L} (hsplit : splits i p) : p.nat_degree = (p.map i).roots.card := begin by_cases p_eq_zero : p = 0, { rw [p_eq_zero, nat_degree_zero, map_zero, roots_zero, multiset.card_zero] }, have map_ne_zero : p.map i ≠ 0 := map_ne_zero (p_eq_zero), rw eq_prod_roots_of_splits hsplit at map_ne_zero, conv_lhs { rw [← nat_degree_map i, eq_prod_roots_of_splits hsplit] }, have : (0 : polynomial L) ∉ (map i p).roots.map (λ a, X - C a), from zero_nmem_multiset_map_X_sub_C _ _, simp [nat_degree_mul (left_ne_zero_of_mul map_ne_zero) (right_ne_zero_of_mul map_ne_zero), nat_degree_multiset_prod _ this] end lemma degree_eq_card_roots {p : polynomial K} {i : K →+* L} (p_ne_zero : p ≠ 0) (hsplit : splits i p) : p.degree = (p.map i).roots.card := by rw [degree_eq_nat_degree p_ne_zero, nat_degree_eq_card_roots hsplit] section UFD local attribute [instance, priority 10] principal_ideal_ring.to_unique_factorization_monoid local infix ` ~ᵤ ` : 50 := associated open unique_factorization_monoid associates lemma splits_of_exists_multiset {f : polynomial K} {s : multiset L} (hs : f.map i = C (i f.leading_coeff) * (s.map (λ a : L, (X : polynomial L) - C a)).prod) : splits i f := if hf0 : f = 0 then or.inl hf0 else or.inr $ λ p hp hdp, have ht : multiset.rel associated (factors (f.map i)) (s.map (λ a : L, (X : polynomial L) - C a)) := factors_unique (λ p hp, irreducible_of_factor _ hp) (λ p' m, begin obtain ⟨a,m,rfl⟩ := multiset.mem_map.1 m, exact irreducible_of_degree_eq_one (degree_X_sub_C _), end) (associated.symm $ calc _ ~ᵤ f.map i : ⟨(units.map C.to_monoid_hom : units L →* units (polynomial L)) (units.mk0 (f.map i).leading_coeff (mt leading_coeff_eq_zero.1 (map_ne_zero hf0))), by conv_rhs { rw [hs, ← leading_coeff_map i, mul_comm] }; refl⟩ ... ~ᵤ _ : associated.symm (unique_factorization_monoid.factors_prod (by simpa using hf0))), let ⟨q, hq, hpq⟩ := exists_mem_factors_of_dvd (by simpa) hp hdp in let ⟨q', hq', hqq'⟩ := multiset.exists_mem_of_rel_of_mem ht hq in let ⟨a, ha⟩ := multiset.mem_map.1 hq' in by rw [← degree_X_sub_C a, ha.2]; exact degree_eq_degree_of_associated (hpq.trans hqq') lemma splits_of_splits_id {f : polynomial K} : splits (ring_hom.id _) f → splits i f := unique_factorization_monoid.induction_on_prime f (λ _, splits_zero _) (λ _ hu _, splits_of_degree_le_one _ ((is_unit_iff_degree_eq_zero.1 hu).symm ▸ dec_trivial)) (λ a p ha0 hp ih hfi, splits_mul _ (splits_of_degree_eq_one _ ((splits_of_splits_mul _ (mul_ne_zero hp.1 ha0) hfi).1.resolve_left hp.1 hp.irreducible (by rw map_id))) (ih (splits_of_splits_mul _ (mul_ne_zero hp.1 ha0) hfi).2)) end UFD lemma splits_iff_exists_multiset {f : polynomial K} : splits i f ↔ ∃ (s : multiset L), f.map i = C (i f.leading_coeff) * (s.map (λ a : L, (X : polynomial L) - C a)).prod := ⟨exists_multiset_of_splits i, λ ⟨s, hs⟩, splits_of_exists_multiset i hs⟩ lemma splits_comp_of_splits (j : L →+* F) {f : polynomial K} (h : splits i f) : splits (j.comp i) f := begin change i with ((ring_hom.id _).comp i) at h, rw [← splits_map_iff], rw [← splits_map_iff i] at h, exact splits_of_splits_id _ h end /-- A monic polynomial `p` that has as many roots as its degree can be written `p = ∏(X - a)`, for `a` in `p.roots`. -/ lemma prod_multiset_X_sub_C_of_monic_of_roots_card_eq {p : polynomial K} (hmonic : p.monic) (hroots : p.roots.card = p.nat_degree) : (multiset.map (λ (a : K), X - C a) p.roots).prod = p := begin have hprodmonic : (multiset.map (λ (a : K), X - C a) p.roots).prod.monic, { simp only [prod_multiset_root_eq_finset_root (ne_zero_of_monic hmonic), monic_prod_of_monic, monic_X_sub_C, monic_pow, forall_true_iff] }, have hdegree : (multiset.map (λ (a : K), X - C a) p.roots).prod.nat_degree = p.nat_degree, { rw [← hroots, nat_degree_multiset_prod _ (zero_nmem_multiset_map_X_sub_C _ (λ a : K, a))], simp only [eq_self_iff_true, mul_one, nat.cast_id, nsmul_eq_mul, multiset.sum_repeat, multiset.map_const,nat_degree_X_sub_C, function.comp, multiset.map_map] }, obtain ⟨q, hq⟩ := prod_multiset_X_sub_C_dvd p, have qzero : q ≠ 0, { rintro rfl, apply hmonic.ne_zero, simpa only [mul_zero] using hq }, have degp : p.nat_degree = (multiset.map (λ (a : K), X - C a) p.roots).prod.nat_degree + q.nat_degree, { nth_rewrite 0 [hq], simp only [nat_degree_mul (ne_zero_of_monic hprodmonic) qzero] }, have degq : q.nat_degree = 0, { rw hdegree at degp, exact (add_right_inj p.nat_degree).mp (tactic.ring_exp.add_pf_sum_z degp rfl).symm }, obtain ⟨u, hu⟩ := is_unit_iff_degree_eq_zero.2 ((degree_eq_iff_nat_degree_eq qzero).2 degq), have hassoc : associated (multiset.map (λ (a : K), X - C a) p.roots).prod p, { rw associated, use u, rw [hu, ← hq] }, exact eq_of_monic_of_associated hprodmonic hmonic hassoc end /-- A polynomial `p` that has as many roots as its degree can be written `p = p.leading_coeff * ∏(X - a)`, for `a` in `p.roots`. -/ lemma C_leading_coeff_mul_prod_multiset_X_sub_C {p : polynomial K} (hroots : p.roots.card = p.nat_degree) : (C p.leading_coeff) * (multiset.map (λ (a : K), X - C a) p.roots).prod = p := begin by_cases hzero : p = 0, { rw [hzero, leading_coeff_zero, ring_hom.map_zero, zero_mul], }, { have hcoeff : p.leading_coeff ≠ 0, { intro h, exact hzero (leading_coeff_eq_zero.1 h) }, have hrootsnorm : (normalize p).roots.card = (normalize p).nat_degree, { rw [roots_normalize, normalize_apply, nat_degree_mul hzero (units.ne_zero _), hroots, coe_norm_unit, nat_degree_C, add_zero], }, have hprod := prod_multiset_X_sub_C_of_monic_of_roots_card_eq (monic_normalize hzero) hrootsnorm, rw [roots_normalize, normalize_apply, coe_norm_unit_of_ne_zero hzero] at hprod, calc (C p.leading_coeff) * (multiset.map (λ (a : K), X - C a) p.roots).prod = p * C ((p.leading_coeff)⁻¹ * p.leading_coeff) : by rw [hprod, mul_comm, mul_assoc, ← C_mul] ... = p * C 1 : by field_simp ... = p : by simp only [mul_one, ring_hom.map_one], }, end /-- A polynomial splits if and only if it has as many roots as its degree. -/ lemma splits_iff_card_roots {p : polynomial K} : splits (ring_hom.id K) p ↔ p.roots.card = p.nat_degree := begin split, { intro H, rw [nat_degree_eq_card_roots H, map_id] }, { intro hroots, apply (splits_iff_exists_multiset (ring_hom.id K)).2, use p.roots, simp only [ring_hom.id_apply, map_id], exact (C_leading_coeff_mul_prod_multiset_X_sub_C hroots).symm }, end end splits end polynomial section embeddings variables (F) [field F] /-- If `p` is the minimal polynomial of `a` over `F` then `F[a] ≃ₐ[F] F[x]/(p)` -/ def alg_equiv.adjoin_singleton_equiv_adjoin_root_minpoly {R : Type*} [comm_ring R] [algebra F R] (x : R) : algebra.adjoin F ({x} : set R) ≃ₐ[F] adjoin_root (minpoly F x) := alg_equiv.symm $ alg_equiv.of_bijective (alg_hom.cod_restrict (adjoin_root.lift_hom _ x $ minpoly.aeval F x) _ (λ p, adjoin_root.induction_on _ p $ λ p, (algebra.adjoin_singleton_eq_range F x).symm ▸ (polynomial.aeval _).mem_range.mpr ⟨p, rfl⟩)) ⟨(alg_hom.injective_cod_restrict _ _ _).2 $ (alg_hom.injective_iff _).2 $ λ p, adjoin_root.induction_on _ p $ λ p hp, ideal.quotient.eq_zero_iff_mem.2 $ ideal.mem_span_singleton.2 $ minpoly.dvd F x hp, λ y, let ⟨p, hp⟩ := (set_like.ext_iff.1 (algebra.adjoin_singleton_eq_range F x) (y : R)).1 y.2 in ⟨adjoin_root.mk _ p, subtype.eq hp⟩⟩ open finset /-- If a `subalgebra` is finite_dimensional as a submodule then it is `finite_dimensional`. -/ lemma finite_dimensional.of_subalgebra_to_submodule {K V : Type*} [field K] [ring V] [algebra K V] {s : subalgebra K V} (h : finite_dimensional K s.to_submodule) : finite_dimensional K s := h /-- If `K` and `L` are field extensions of `F` and we have `s : finset K` such that the minimal polynomial of each `x ∈ s` splits in `L` then `algebra.adjoin F s` embeds in `L`. -/ theorem lift_of_splits {F K L : Type*} [field F] [field K] [field L] [algebra F K] [algebra F L] (s : finset K) : (∀ x ∈ s, is_integral F x ∧ polynomial.splits (algebra_map F L) (minpoly F x)) → nonempty (algebra.adjoin F (↑s : set K) →ₐ[F] L) := begin refine finset.induction_on s (λ H, _) (λ a s has ih H, _), { rw [coe_empty, algebra.adjoin_empty], exact ⟨(algebra.of_id F L).comp (algebra.bot_equiv F K)⟩ }, rw forall_mem_insert at H, rcases H with ⟨⟨H1, H2⟩, H3⟩, cases ih H3 with f, choose H3 H4 using H3, rw [coe_insert, set.insert_eq, set.union_comm, algebra.adjoin_union_eq_under], letI := (f : algebra.adjoin F (↑s : set K) →+* L).to_algebra, haveI : finite_dimensional F (algebra.adjoin F (↑s : set K)) := ( (submodule.fg_iff_finite_dimensional _).1 (fg_adjoin_of_finite (set.finite_mem_finset s) H3)).of_subalgebra_to_submodule, letI := field_of_finite_dimensional F (algebra.adjoin F (↑s : set K)), have H5 : is_integral (algebra.adjoin F (↑s : set K)) a := is_integral_of_is_scalar_tower a H1, have H6 : (minpoly (algebra.adjoin F (↑s : set K)) a).splits (algebra_map (algebra.adjoin F (↑s : set K)) L), { refine polynomial.splits_of_splits_of_dvd _ (polynomial.map_ne_zero $ minpoly.ne_zero H1 : polynomial.map (algebra_map _ _) _ ≠ 0) ((polynomial.splits_map_iff _ _).2 _) (minpoly.dvd _ _ _), { rw ← is_scalar_tower.algebra_map_eq, exact H2 }, { rw [← is_scalar_tower.aeval_apply, minpoly.aeval] } }, obtain ⟨y, hy⟩ := polynomial.exists_root_of_splits _ H6 (ne_of_lt (minpoly.degree_pos H5)).symm, refine ⟨subalgebra.of_under _ _ _⟩, refine (adjoin_root.lift_hom (minpoly (algebra.adjoin F (↑s : set K)) a) y hy).comp _, exact alg_equiv.adjoin_singleton_equiv_adjoin_root_minpoly (algebra.adjoin F (↑s : set K)) a end end embeddings namespace polynomial variables [field K] [field L] [field F] open polynomial section splitting_field /-- Non-computably choose an irreducible factor from a polynomial. -/ def factor (f : polynomial K) : polynomial K := if H : ∃ g, irreducible g ∧ g ∣ f then classical.some H else X instance irreducible_factor (f : polynomial K) : irreducible (factor f) := begin rw factor, split_ifs with H, { exact (classical.some_spec H).1 }, { exact irreducible_X } end theorem factor_dvd_of_not_is_unit {f : polynomial K} (hf1 : ¬is_unit f) : factor f ∣ f := begin by_cases hf2 : f = 0, { rw hf2, exact dvd_zero _ }, rw [factor, dif_pos (wf_dvd_monoid.exists_irreducible_factor hf1 hf2)], exact (classical.some_spec $ wf_dvd_monoid.exists_irreducible_factor hf1 hf2).2 end theorem factor_dvd_of_degree_ne_zero {f : polynomial K} (hf : f.degree ≠ 0) : factor f ∣ f := factor_dvd_of_not_is_unit (mt degree_eq_zero_of_is_unit hf) theorem factor_dvd_of_nat_degree_ne_zero {f : polynomial K} (hf : f.nat_degree ≠ 0) : factor f ∣ f := factor_dvd_of_degree_ne_zero (mt nat_degree_eq_of_degree_eq_some hf) /-- Divide a polynomial f by X - C r where r is a root of f in a bigger field extension. -/ def remove_factor (f : polynomial K) : polynomial (adjoin_root $ factor f) := map (adjoin_root.of f.factor) f /ₘ (X - C (adjoin_root.root f.factor)) theorem X_sub_C_mul_remove_factor (f : polynomial K) (hf : f.nat_degree ≠ 0) : (X - C (adjoin_root.root f.factor)) * f.remove_factor = map (adjoin_root.of f.factor) f := let ⟨g, hg⟩ := factor_dvd_of_nat_degree_ne_zero hf in mul_div_by_monic_eq_iff_is_root.2 $ by rw [is_root.def, eval_map, hg, eval₂_mul, ← hg, adjoin_root.eval₂_root, zero_mul] theorem nat_degree_remove_factor (f : polynomial K) : f.remove_factor.nat_degree = f.nat_degree - 1 := by rw [remove_factor, nat_degree_div_by_monic _ (monic_X_sub_C _), nat_degree_map, nat_degree_X_sub_C] theorem nat_degree_remove_factor' {f : polynomial K} {n : ℕ} (hfn : f.nat_degree = n+1) : f.remove_factor.nat_degree = n := by rw [nat_degree_remove_factor, hfn, n.add_sub_cancel] /-- Auxiliary construction to a splitting field of a polynomial. Uses induction on the degree. -/ def splitting_field_aux (n : ℕ) : Π {K : Type u} [field K], by exactI Π (f : polynomial K), f.nat_degree = n → Type u := nat.rec_on n (λ K _ _ _, K) $ λ n ih K _ f hf, by exactI ih f.remove_factor (nat_degree_remove_factor' hf) namespace splitting_field_aux theorem succ (n : ℕ) (f : polynomial K) (hfn : f.nat_degree = n + 1) : splitting_field_aux (n+1) f hfn = splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn) := rfl instance field (n : ℕ) : Π {K : Type u} [field K], by exactI Π {f : polynomial K} (hfn : f.nat_degree = n), field (splitting_field_aux n f hfn) := nat.rec_on n (λ K _ _ _, ‹field K›) $ λ n ih K _ f hf, ih _ instance inhabited {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n) : inhabited (splitting_field_aux n f hfn) := ⟨37⟩ instance algebra (n : ℕ) : Π {K : Type u} [field K], by exactI Π {f : polynomial K} (hfn : f.nat_degree = n), algebra K (splitting_field_aux n f hfn) := nat.rec_on n (λ K _ _ _, by exactI algebra.id K) $ λ n ih K _ f hfn, by exactI @@restrict_scalars.algebra _ _ _ _ _ (ih _) _ _ instance algebra' {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n + 1) : algebra (adjoin_root f.factor) (splitting_field_aux _ _ hfn) := splitting_field_aux.algebra n _ instance algebra'' {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n + 1) : algebra K (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)) := splitting_field_aux.algebra (n+1) hfn instance algebra''' {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n + 1) : algebra (adjoin_root f.factor) (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)) := splitting_field_aux.algebra n _ instance scalar_tower {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n + 1) : is_scalar_tower K (adjoin_root f.factor) (splitting_field_aux _ _ hfn) := is_scalar_tower.of_algebra_map_eq $ λ x, rfl instance scalar_tower' {n : ℕ} {f : polynomial K} (hfn : f.nat_degree = n + 1) : is_scalar_tower K (adjoin_root f.factor) (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)) := is_scalar_tower.of_algebra_map_eq $ λ x, rfl theorem algebra_map_succ (n : ℕ) (f : polynomial K) (hfn : f.nat_degree = n + 1) : by exact algebra_map K (splitting_field_aux _ _ hfn) = (algebra_map (adjoin_root f.factor) (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn))).comp (adjoin_root.of f.factor) := rfl protected theorem splits (n : ℕ) : ∀ {K : Type u} [field K], by exactI ∀ (f : polynomial K) (hfn : f.nat_degree = n), splits (algebra_map K $ splitting_field_aux n f hfn) f := nat.rec_on n (λ K _ _ hf, by exactI splits_of_degree_le_one _ (le_trans degree_le_nat_degree $ hf.symm ▸ with_bot.coe_le_coe.2 zero_le_one)) $ λ n ih K _ f hf, by { resetI, rw [← splits_id_iff_splits, algebra_map_succ, ← map_map, splits_id_iff_splits, ← X_sub_C_mul_remove_factor f (λ h, by { rw h at hf, cases hf })], exact splits_mul _ (splits_X_sub_C _) (ih _ _) } theorem exists_lift (n : ℕ) : ∀ {K : Type u} [field K], by exactI ∀ (f : polynomial K) (hfn : f.nat_degree = n) {L : Type*} [field L], by exactI ∀ (j : K →+* L) (hf : splits j f), ∃ k : splitting_field_aux n f hfn →+* L, k.comp (algebra_map _ _) = j := nat.rec_on n (λ K _ _ _ L _ j _, by exactI ⟨j, j.comp_id⟩) $ λ n ih K _ f hf L _ j hj, by exactI have hndf : f.nat_degree ≠ 0, by { intro h, rw h at hf, cases hf }, have hfn0 : f ≠ 0, by { intro h, rw h at hndf, exact hndf rfl }, let ⟨r, hr⟩ := exists_root_of_splits _ (splits_of_splits_of_dvd j hfn0 hj (factor_dvd_of_nat_degree_ne_zero hndf)) (mt is_unit_iff_degree_eq_zero.2 f.irreducible_factor.1) in have hmf0 : map (adjoin_root.of f.factor) f ≠ 0, from map_ne_zero hfn0, have hsf : splits (adjoin_root.lift j r hr) f.remove_factor, by { rw ← X_sub_C_mul_remove_factor _ hndf at hmf0, refine (splits_of_splits_mul _ hmf0 _).2, rwa [X_sub_C_mul_remove_factor _ hndf, ← splits_id_iff_splits, map_map, adjoin_root.lift_comp_of, splits_id_iff_splits] }, let ⟨k, hk⟩ := ih f.remove_factor (nat_degree_remove_factor' hf) (adjoin_root.lift j r hr) hsf in ⟨k, by rw [algebra_map_succ, ← ring_hom.comp_assoc, hk, adjoin_root.lift_comp_of]⟩ theorem adjoin_roots (n : ℕ) : ∀ {K : Type u} [field K], by exactI ∀ (f : polynomial K) (hfn : f.nat_degree = n), algebra.adjoin K (↑(f.map $ algebra_map K $ splitting_field_aux n f hfn).roots.to_finset : set (splitting_field_aux n f hfn)) = ⊤ := nat.rec_on n (λ K _ f hf, by exactI algebra.eq_top_iff.2 (λ x, subalgebra.range_le _ ⟨x, rfl⟩)) $ λ n ih K _ f hfn, by exactI have hndf : f.nat_degree ≠ 0, by { intro h, rw h at hfn, cases hfn }, have hfn0 : f ≠ 0, by { intro h, rw h at hndf, exact hndf rfl }, have hmf0 : map (algebra_map K (splitting_field_aux n.succ f hfn)) f ≠ 0 := map_ne_zero hfn0, by { rw [algebra_map_succ, ← map_map, ← X_sub_C_mul_remove_factor _ hndf, map_mul] at hmf0 ⊢, rw [roots_mul hmf0, map_sub, map_X, map_C, roots_X_sub_C, multiset.to_finset_add, finset.coe_union, multiset.to_finset_cons, multiset.to_finset_zero, insert_emptyc_eq, finset.coe_singleton, algebra.adjoin_union_eq_under, ← set.image_singleton, algebra.adjoin_algebra_map K (adjoin_root f.factor) (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)), adjoin_root.adjoin_root_eq_top, algebra.map_top, is_scalar_tower.range_under_adjoin K (adjoin_root f.factor) (splitting_field_aux n f.remove_factor (nat_degree_remove_factor' hfn)), ih, subalgebra.res_top] } end splitting_field_aux /-- A splitting field of a polynomial. -/ def splitting_field (f : polynomial K) := splitting_field_aux _ f rfl namespace splitting_field variables (f : polynomial K) instance : field (splitting_field f) := splitting_field_aux.field _ _ instance inhabited : inhabited (splitting_field f) := ⟨37⟩ instance : algebra K (splitting_field f) := splitting_field_aux.algebra _ _ protected theorem splits : splits (algebra_map K (splitting_field f)) f := splitting_field_aux.splits _ _ _ variables [algebra K L] (hb : splits (algebra_map K L) f) /-- Embeds the splitting field into any other field that splits the polynomial. -/ def lift : splitting_field f →ₐ[K] L := { commutes' := λ r, by { have := classical.some_spec (splitting_field_aux.exists_lift _ _ _ _ hb), exact ring_hom.ext_iff.1 this r }, .. classical.some (splitting_field_aux.exists_lift _ _ _ _ hb) } theorem adjoin_roots : algebra.adjoin K (↑(f.map (algebra_map K $ splitting_field f)).roots.to_finset : set (splitting_field f)) = ⊤ := splitting_field_aux.adjoin_roots _ _ _ theorem adjoin_root_set : algebra.adjoin K (f.root_set f.splitting_field) = ⊤ := adjoin_roots f end splitting_field variables (K L) [algebra K L] /-- Typeclass characterising splitting fields. -/ class is_splitting_field (f : polynomial K) : Prop := (splits [] : splits (algebra_map K L) f) (adjoin_roots [] : algebra.adjoin K (↑(f.map (algebra_map K L)).roots.to_finset : set L) = ⊤) namespace is_splitting_field variables {K} instance splitting_field (f : polynomial K) : is_splitting_field K (splitting_field f) f := ⟨splitting_field.splits f, splitting_field.adjoin_roots f⟩ section scalar_tower variables {K L F} [algebra F K] [algebra F L] [is_scalar_tower F K L] variables {K} instance map (f : polynomial F) [is_splitting_field F L f] : is_splitting_field K L (f.map $ algebra_map F K) := ⟨by { rw [splits_map_iff, ← is_scalar_tower.algebra_map_eq], exact splits L f }, subalgebra.res_inj F $ by { rw [map_map, ← is_scalar_tower.algebra_map_eq, subalgebra.res_top, eq_top_iff, ← adjoin_roots L f, algebra.adjoin_le_iff], exact λ x hx, @algebra.subset_adjoin K _ _ _ _ _ _ hx }⟩ variables {K} (L) theorem splits_iff (f : polynomial K) [is_splitting_field K L f] : polynomial.splits (ring_hom.id K) f ↔ (⊤ : subalgebra K L) = ⊥ := ⟨λ h, eq_bot_iff.2 $ adjoin_roots L f ▸ (roots_map (algebra_map K L) h).symm ▸ algebra.adjoin_le_iff.2 (λ y hy, let ⟨x, hxs, hxy⟩ := finset.mem_image.1 (by rwa multiset.to_finset_map at hy) in hxy ▸ set_like.mem_coe.2 $ subalgebra.algebra_map_mem _ _), λ h, @ring_equiv.to_ring_hom_refl K _ ▸ ring_equiv.trans_symm (ring_equiv.of_bijective _ $ algebra.bijective_algebra_map_iff.2 h) ▸ by { rw ring_equiv.to_ring_hom_trans, exact splits_comp_of_splits _ _ (splits L f) }⟩ theorem mul (f g : polynomial F) (hf : f ≠ 0) (hg : g ≠ 0) [is_splitting_field F K f] [is_splitting_field K L (g.map $ algebra_map F K)] : is_splitting_field F L (f * g) := ⟨(is_scalar_tower.algebra_map_eq F K L).symm ▸ splits_mul _ (splits_comp_of_splits _ _ (splits K f)) ((splits_map_iff _ _).1 (splits L $ g.map $ algebra_map F K)), by rw [map_mul, roots_mul (mul_ne_zero (map_ne_zero hf : f.map (algebra_map F L) ≠ 0) (map_ne_zero hg)), multiset.to_finset_add, finset.coe_union, algebra.adjoin_union_eq_under, is_scalar_tower.algebra_map_eq F K L, ← map_map, roots_map (algebra_map K L) ((splits_id_iff_splits $ algebra_map F K).2 $ splits K f), multiset.to_finset_map, finset.coe_image, algebra.adjoin_algebra_map, adjoin_roots, algebra.map_top, is_scalar_tower.range_under_adjoin, ← map_map, adjoin_roots, subalgebra.res_top]⟩ end scalar_tower /-- Splitting field of `f` embeds into any field that splits `f`. -/ def lift [algebra K F] (f : polynomial K) [is_splitting_field K L f] (hf : polynomial.splits (algebra_map K F) f) : L →ₐ[K] F := if hf0 : f = 0 then (algebra.of_id K F).comp $ (algebra.bot_equiv K L : (⊥ : subalgebra K L) →ₐ[K] K).comp $ by { rw ← (splits_iff L f).1 (show f.splits (ring_hom.id K), from hf0.symm ▸ splits_zero _), exact algebra.to_top } else alg_hom.comp (by { rw ← adjoin_roots L f, exact classical.choice (lift_of_splits _ $ λ y hy, have aeval y f = 0, from (eval₂_eq_eval_map _).trans $ (mem_roots $ by exact map_ne_zero hf0).1 (multiset.mem_to_finset.mp hy), ⟨(is_algebraic_iff_is_integral _).1 ⟨f, hf0, this⟩, splits_of_splits_of_dvd _ hf0 hf $ minpoly.dvd _ _ this⟩) }) algebra.to_top theorem finite_dimensional (f : polynomial K) [is_splitting_field K L f] : finite_dimensional K L := is_noetherian.iff_fg.2 ⟨@algebra.top_to_submodule K L _ _ _ ▸ adjoin_roots L f ▸ fg_adjoin_of_finite (set.finite_mem_finset _) (λ y hy, if hf : f = 0 then by { rw [hf, map_zero, roots_zero] at hy, cases hy } else (is_algebraic_iff_is_integral _).1 ⟨f, hf, (eval₂_eq_eval_map _).trans $ (mem_roots $ by exact map_ne_zero hf).1 (multiset.mem_to_finset.mp hy)⟩)⟩ instance (f : polynomial K) : _root_.finite_dimensional K f.splitting_field := finite_dimensional f.splitting_field f /-- Any splitting field is isomorphic to `splitting_field f`. -/ def alg_equiv (f : polynomial K) [is_splitting_field K L f] : L ≃ₐ[K] splitting_field f := begin refine alg_equiv.of_bijective (lift L f $ splits (splitting_field f) f) ⟨ring_hom.injective (lift L f $ splits (splitting_field f) f).to_ring_hom, _⟩, haveI := finite_dimensional (splitting_field f) f, haveI := finite_dimensional L f, have : finite_dimensional.finrank K L = finite_dimensional.finrank K (splitting_field f) := le_antisymm (linear_map.finrank_le_finrank_of_injective (show function.injective (lift L f $ splits (splitting_field f) f).to_linear_map, from ring_hom.injective (lift L f $ splits (splitting_field f) f : L →+* f.splitting_field))) (linear_map.finrank_le_finrank_of_injective (show function.injective (lift (splitting_field f) f $ splits L f).to_linear_map, from ring_hom.injective (lift (splitting_field f) f $ splits L f : f.splitting_field →+* L))), change function.surjective (lift L f $ splits (splitting_field f) f).to_linear_map, refine (linear_map.injective_iff_surjective_of_finrank_eq_finrank this).1 _, exact ring_hom.injective (lift L f $ splits (splitting_field f) f : L →+* f.splitting_field) end end is_splitting_field end splitting_field end polynomial
2a96513b9ccb236c07025b753465f4628936d9a3
63abd62053d479eae5abf4951554e1064a4c45b4
/src/analysis/calculus/mean_value.lean
544d9875c74b15f55ba7cffe17aff52cd7daf7aa
[ "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
53,700
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Yury Kudryashov -/ import analysis.calculus.local_extr import analysis.convex.topology /-! # The mean value inequality and equalities In this file we prove the following facts: * `convex.norm_image_sub_le_of_norm_deriv_le` : if `f` is differentiable on a convex set `s` and the norm of its derivative is bounded by `C`, then `f` is Lipschitz continuous on `s` with constant `C`; also a variant in which what is bounded by `C` is the norm of the difference of the derivative from a fixed linear map. * `image_le_of*`, `image_norm_le_of_*` : several similar lemmas deducing `f x ≤ B x` or `∥f x∥ ≤ B x` from upper estimates on `f'` or `∥f'∥`, respectively. These lemmas differ by their assumptions: * `of_liminf_*` lemmas assume that limit inferior of some ratio is less than `B' x`; * `of_deriv_right_*`, `of_norm_deriv_right_*` lemmas assume that the right derivative or its norm is less than `B' x`; * `of_*_lt_*` lemmas assume a strict inequality whenever `f x = B x` or `∥f x∥ = B x`; * `of_*_le_*` lemmas assume a non-strict inequality everywhere on `[a, b)`; * name of a lemma ends with `'` if (1) it assumes that `B` is continuous on `[a, b]` and has a right derivative at every point of `[a, b)`, and (2) the lemma has a counterpart assuming that `B` is differentiable everywhere on `ℝ` * `norm_image_sub_le_*_segment` : if derivative of `f` on `[a, b]` is bounded above by a constant `C`, then `∥f x - f a∥ ≤ C * ∥x - a∥`; several versions deal with right derivative and derivative within `[a, b]` (`has_deriv_within_at` or `deriv_within`). * `convex.is_const_of_fderiv_within_eq_zero` : if a function has derivative `0` on a convex set `s`, then it is a constant on `s`. * `exists_ratio_has_deriv_at_eq_ratio_slope` and `exists_ratio_deriv_eq_ratio_slope` : Cauchy's Mean Value Theorem. * `exists_has_deriv_at_eq_slope` and `exists_deriv_eq_slope` : Lagrange's Mean Value Theorem. * `domain_mvt` : Lagrange's Mean Value Theorem, applied to a segment in a convex domain. * `convex.image_sub_lt_mul_sub_of_deriv_lt`, `convex.mul_sub_lt_image_sub_of_lt_deriv`, `convex.image_sub_le_mul_sub_of_deriv_le`, `convex.mul_sub_le_image_sub_of_le_deriv`, if `∀ x, C (</≤/>/≥) (f' x)`, then `C * (y - x) (</≤/>/≥) (f y - f x)` whenever `x < y`. * `convex.mono_of_deriv_nonneg`, `convex.antimono_of_deriv_nonpos`, `convex.strict_mono_of_deriv_pos`, `convex.strict_antimono_of_deriv_neg` : if the derivative of a function is non-negative/non-positive/positive/negative, then the function is monotone/monotonically decreasing/strictly monotone/strictly monotonically decreasing. * `convex_on_of_deriv_mono`, `convex_on_of_deriv2_nonneg` : if the derivative of a function is increasing or its second derivative is nonnegative, then the original function is convex. * `strict_fderiv_of_cont_diff` : a C^1 function over the reals is strictly differentiable. (This is a corollary of the mean value inequality.) -/ variables {E : Type*} [normed_group E] [normed_space ℝ E] {F : Type*} [normed_group F] [normed_space ℝ F] open metric set asymptotics continuous_linear_map filter open_locale classical topological_space nnreal /-! ### One-dimensional fencing inequalities -/ /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_liminf_slope_right_lt_deriv_boundary' {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) -- `hf'` actually says `liminf (z - x)⁻¹ * (f z - f x) ≤ f' x` (hf' : ∀ x ∈ Ico a b, ∀ r, f' x < r → ∃ᶠ z in 𝓝[Ioi x] x, (z - x)⁻¹ * (f z - f x) < r) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := begin change Icc a b ⊆ {x | f x ≤ B x}, set s := {x | f x ≤ B x} ∩ Icc a b, have A : continuous_on (λ x, (f x, B x)) (Icc a b), from hf.prod hB, have : is_closed s, { simp only [s, inter_comm], exact A.preimage_closed_of_closed is_closed_Icc order_closed_topology.is_closed_le' }, apply this.Icc_subset_of_forall_exists_gt ha, rintros x ⟨hxB, xab⟩ y hy, change f x ≤ B x at hxB, cases lt_or_eq_of_le hxB with hxB hxB, { -- If `f x < B x`, then all we need is continuity of both sides apply @nonempty_of_mem_sets _ (𝓝[Ioi x] x), refine inter_mem_sets _ (Ioc_mem_nhds_within_Ioi ⟨le_refl x, hy⟩), have : ∀ᶠ x in 𝓝[Icc a b] x, f x < B x, from A x (Ico_subset_Icc_self xab) (mem_nhds_sets (is_open_lt continuous_fst continuous_snd) hxB), have : ∀ᶠ x in 𝓝[Ioi x] x, f x < B x, from nhds_within_le_of_mem (Icc_mem_nhds_within_Ioi xab) this, refine mem_sets_of_superset this (set_of_subset_set_of.2 $ λ y, le_of_lt) }, { rcases exists_between (bound x xab hxB) with ⟨r, hfr, hrB⟩, specialize hf' x xab r hfr, have HB : ∀ᶠ z in 𝓝[Ioi x] x, r < (z - x)⁻¹ * (B z - B x), from (has_deriv_within_at_iff_tendsto_slope' $ lt_irrefl x).1 (hB' x xab) (mem_nhds_sets is_open_Ioi hrB), obtain ⟨z, ⟨hfz, hzB⟩, hz⟩ : ∃ z, ((z - x)⁻¹ * (f z - f x) < r ∧ r < (z - x)⁻¹ * (B z - B x)) ∧ z ∈ Ioc x y, from ((hf'.and_eventually HB).and_eventually (Ioc_mem_nhds_within_Ioi ⟨le_refl _, hy⟩)).exists, have := le_of_lt (lt_trans hfz hzB), refine ⟨z, _, hz⟩, rw [mul_le_mul_left (inv_pos.2 $ sub_pos.2 hz.1), hxB, sub_le_sub_iff_right] at this, exact this } end /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_liminf_slope_right_lt_deriv_boundary {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) -- `hf'` actually says `liminf (z - x)⁻¹ * (f z - f x) ≤ f' x` (hf' : ∀ x ∈ Ico a b, ∀ r, f' x < r → ∃ᶠ z in 𝓝[Ioi x] x, (z - x)⁻¹ * (f z - f x) < r) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : ∀ x, has_deriv_at B (B' x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' hf hf' ha (λ x hx, (hB x).continuous_at.continuous_within_at) (λ x hx, (hB x).has_deriv_within_at) bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by `B'`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_liminf_slope_right_le_deriv_boundary {f : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) -- `bound` actually says `liminf (z - x)⁻¹ * (f z - f x) ≤ B' x` (bound : ∀ x ∈ Ico a b, ∀ r, B' x < r → ∃ᶠ z in 𝓝[Ioi x] x, (z - x)⁻¹ * (f z - f x) < r) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := begin have Hr : ∀ x ∈ Icc a b, ∀ r ∈ Ioi (0:ℝ), f x ≤ B x + r * (x - a), { intros x hx r hr, apply image_le_of_liminf_slope_right_lt_deriv_boundary' hf bound, { rwa [sub_self, mul_zero, add_zero] }, { exact hB.add (continuous_on_const.mul (continuous_id.continuous_on.sub continuous_on_const)) }, { assume x hx, exact (hB' x hx).add (((has_deriv_within_at_id x (Ioi x)).sub_const a).const_mul r) }, { assume x hx _, rw [mul_one], exact (lt_add_iff_pos_right _).2 hr }, exact hx }, assume x hx, have : continuous_within_at (λ r, B x + r * (x - a)) (Ioi 0) 0, from continuous_within_at_const.add (continuous_within_at_id.mul continuous_within_at_const), convert continuous_within_at_const.closure_le _ this (Hr x hx); simp [closure_Ioi] end /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_deriv_right_lt_deriv_boundary' {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' hf (λ x hx r hr, (hf' x hx).liminf_right_slope_le hr) ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_deriv_right_lt_deriv_boundary {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : ∀ x, has_deriv_at B (B' x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_deriv_right_lt_deriv_boundary' hf hf' ha (λ x hx, (hB x).continuous_at.continuous_within_at) (λ x hx, (hB x).has_deriv_within_at) bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x ≤ B' x` on `[a, b)`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_deriv_right_le_deriv_boundary {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, f' x ≤ B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_le_deriv_boundary hf ha hB hB' $ assume x hx r hr, (hf' x hx).liminf_right_slope_le (lt_of_le_of_lt (bound x hx) hr) /-! ### Vector-valued functions `f : ℝ → E` -/ section variables {f : ℝ → E} {a b : ℝ} /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `B` has right derivative at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(∥f z∥ - ∥f x∥) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `∥f x∥ = B x`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. -/ lemma image_norm_le_of_liminf_right_slope_norm_lt_deriv_boundary {E : Type*} [normed_group E] {f : ℝ → E} {f' : ℝ → ℝ} (hf : continuous_on f (Icc a b)) -- `hf'` actually says `liminf ∥z - x∥⁻¹ * (∥f z∥ - ∥f x∥) ≤ f' x` (hf' : ∀ x ∈ Ico a b, ∀ r, f' x < r → ∃ᶠ z in 𝓝[Ioi x] x, (z - x)⁻¹ * (∥f z∥ - ∥f x∥) < r) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, ∥f x∥ = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' (continuous_norm.comp_continuous_on hf) hf' ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` and `B` have right derivatives `f'` and `B'` respectively at every point of `[a, b)`; * the norm of `f'` is strictly less than `B'` whenever `∥f x∥ = B x`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_lt_deriv_boundary' {f' : ℝ → E} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, ∥f x∥ = B x → ∥f' x∥ < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_norm_le_of_liminf_right_slope_norm_lt_deriv_boundary hf (λ x hx r hr, (hf' x hx).liminf_right_slope_norm_le hr) ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` has right derivative `f'` at every point of `[a, b)`; * `B` has derivative `B'` everywhere on `ℝ`; * the norm of `f'` is strictly less than `B'` whenever `∥f x∥ = B x`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_lt_deriv_boundary {f' : ℝ → E} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : ∀ x, has_deriv_at B (B' x) x) (bound : ∀ x ∈ Ico a b, ∥f x∥ = B x → ∥f' x∥ < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_norm_le_of_norm_deriv_right_lt_deriv_boundary' hf hf' ha (λ x hx, (hB x).continuous_at.continuous_within_at) (λ x hx, (hB x).has_deriv_within_at) bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` and `B` have right derivatives `f'` and `B'` respectively at every point of `[a, b)`; * we have `∥f' x∥ ≤ B x` everywhere on `[a, b)`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_le_deriv_boundary' {f' : ℝ → E} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, ∥f' x∥ ≤ B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_le_of_liminf_slope_right_le_deriv_boundary (continuous_norm.comp_continuous_on hf) ha hB hB' $ (λ x hx r hr, (hf' x hx).liminf_right_slope_norm_le (lt_of_le_of_lt (bound x hx) hr)) /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` has right derivative `f'` at every point of `[a, b)`; * `B` has derivative `B'` everywhere on `ℝ`; * we have `∥f' x∥ ≤ B x` everywhere on `[a, b)`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_le_deriv_boundary {f' : ℝ → E} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : ∀ x, has_deriv_at B (B' x) x) (bound : ∀ x ∈ Ico a b, ∥f' x∥ ≤ B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_norm_le_of_norm_deriv_right_le_deriv_boundary' hf hf' ha (λ x hx, (hB x).continuous_at.continuous_within_at) (λ x hx, (hB x).has_deriv_within_at) bound /-- A function on `[a, b]` with the norm of the right derivative bounded by `C` satisfies `∥f x - f a∥ ≤ C * (x - a)`. -/ theorem norm_image_sub_le_of_norm_deriv_right_le_segment {f' : ℝ → E} {C : ℝ} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) (bound : ∀x ∈ Ico a b, ∥f' x∥ ≤ C) : ∀ x ∈ Icc a b, ∥f x - f a∥ ≤ C * (x - a) := begin let g := λ x, f x - f a, have hg : continuous_on g (Icc a b), from hf.sub continuous_on_const, have hg' : ∀ x ∈ Ico a b, has_deriv_within_at g (f' x) (Ioi x) x, { assume x hx, simpa using (hf' x hx).sub (has_deriv_within_at_const _ _ _) }, let B := λ x, C * (x - a), have hB : ∀ x, has_deriv_at B C x, { assume x, simpa using (has_deriv_at_const x C).mul ((has_deriv_at_id x).sub (has_deriv_at_const x a)) }, convert image_norm_le_of_norm_deriv_right_le_deriv_boundary hg hg' _ hB bound, { simp only [g, B] }, { simp only [g, B], rw [sub_self, norm_zero, sub_self, mul_zero] } end /-- A function on `[a, b]` with the norm of the derivative within `[a, b]` bounded by `C` satisfies `∥f x - f a∥ ≤ C * (x - a)`, `has_deriv_within_at` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment' {f' : ℝ → E} {C : ℝ} (hf : ∀ x ∈ Icc a b, has_deriv_within_at f (f' x) (Icc a b) x) (bound : ∀x ∈ Ico a b, ∥f' x∥ ≤ C) : ∀ x ∈ Icc a b, ∥f x - f a∥ ≤ C * (x - a) := begin refine norm_image_sub_le_of_norm_deriv_right_le_segment (λ x hx, (hf x hx).continuous_within_at) (λ x hx, _) bound, apply (hf x $ Ico_subset_Icc_self hx).nhds_within, exact Icc_mem_nhds_within_Ioi hx end /-- A function on `[a, b]` with the norm of the derivative within `[a, b]` bounded by `C` satisfies `∥f x - f a∥ ≤ C * (x - a)`, `deriv_within` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment {C : ℝ} (hf : differentiable_on ℝ f (Icc a b)) (bound : ∀x ∈ Ico a b, ∥deriv_within f (Icc a b) x∥ ≤ C) : ∀ x ∈ Icc a b, ∥f x - f a∥ ≤ C * (x - a) := begin refine norm_image_sub_le_of_norm_deriv_le_segment' _ bound, exact λ x hx, (hf x hx).has_deriv_within_at end /-- A function on `[0, 1]` with the norm of the derivative within `[0, 1]` bounded by `C` satisfies `∥f 1 - f 0∥ ≤ C`, `has_deriv_within_at` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment_01' {f' : ℝ → E} {C : ℝ} (hf : ∀ x ∈ Icc (0:ℝ) 1, has_deriv_within_at f (f' x) (Icc (0:ℝ) 1) x) (bound : ∀x ∈ Ico (0:ℝ) 1, ∥f' x∥ ≤ C) : ∥f 1 - f 0∥ ≤ C := by simpa only [sub_zero, mul_one] using norm_image_sub_le_of_norm_deriv_le_segment' hf bound 1 (right_mem_Icc.2 zero_le_one) /-- A function on `[0, 1]` with the norm of the derivative within `[0, 1]` bounded by `C` satisfies `∥f 1 - f 0∥ ≤ C`, `deriv_within` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment_01 {C : ℝ} (hf : differentiable_on ℝ f (Icc (0:ℝ) 1)) (bound : ∀x ∈ Ico (0:ℝ) 1, ∥deriv_within f (Icc (0:ℝ) 1) x∥ ≤ C) : ∥f 1 - f 0∥ ≤ C := by simpa only [sub_zero, mul_one] using norm_image_sub_le_of_norm_deriv_le_segment hf bound 1 (right_mem_Icc.2 zero_le_one) end /-! ### Vector-valued functions `f : E → F` -/ /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `has_fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_has_fderiv_within_le {f : E → F} {C : ℝ} {s : set E} {x y : E} {f' : E → (E →L[ℝ] F)} (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := begin /- By composition with `t ↦ x + t • (y-x)`, we reduce to a statement for functions defined on `[0,1]`, for which it is proved in `norm_image_sub_le_of_norm_deriv_le_segment`. We just have to check the differentiability of the composition and bounds on its derivative, which is straightforward but tedious for lack of automation. -/ have C0 : 0 ≤ C := le_trans (norm_nonneg _) (bound x xs), set g : ℝ → E := λ t, x + t • (y - x), have Dg : ∀ t, has_deriv_at g (y-x) t, { assume t, simpa only [one_smul] using ((has_deriv_at_id t).smul_const (y - x)).const_add x }, have segm : Icc 0 1 ⊆ g ⁻¹' s, { rw [← image_subset_iff, ← segment_eq_image'], apply hs.segment_subset xs ys }, have : f x = f (g 0), by { simp only [g], rw [zero_smul, add_zero] }, rw this, have : f y = f (g 1), by { simp only [g], rw [one_smul, add_sub_cancel'_right] }, rw this, have D2: ∀ t ∈ Icc (0:ℝ) 1, has_deriv_within_at (f ∘ g) ((f' (g t) : E → F) (y-x)) (Icc (0:ℝ) 1) t, { intros t ht, exact (hf (g t) $ segm ht).comp_has_deriv_within_at _ (Dg t).has_deriv_within_at segm }, apply norm_image_sub_le_of_norm_deriv_le_segment_01' D2, assume t ht, refine le_trans (le_op_norm _ _) (mul_le_mul_of_nonneg_right _ (norm_nonneg _)), exact bound (g t) (segm $ Ico_subset_Icc_self ht) end /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `has_fderiv_within` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_has_fderiv_within_le {f : E → F} {C : ℝ} {s : set E} {f' : E → (E →L[ℝ] F)} (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x∥ ≤ C) (hs : convex s) : lipschitz_on_with (nnreal.of_real C) f s := begin rw lipschitz_on_with_iff_norm_sub_le, intros x x_in y y_in, convert hs.norm_image_sub_le_of_norm_has_fderiv_within_le hf bound y_in x_in, exact nnreal.coe_of_real C ((norm_nonneg $ f' x).trans $ bound x x_in) end /-- The mean value theorem on a convex set: if the derivative of a function within this set is bounded by `C`, then the function is `C`-Lipschitz. Version with `fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_within_le {f : E → F} {C : ℝ} {s : set E} {x y : E} (hf : differentiable_on ℝ f s) (bound : ∀x∈s, ∥fderiv_within ℝ f s x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_within_at) bound xs ys /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `fderiv_within` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_fderiv_within_le {f : E → F} {C : ℝ} {s : set E} (hf : differentiable_on ℝ f s) (bound : ∀x∈s, ∥fderiv_within ℝ f s x∥ ≤ C) (hs : convex s) : lipschitz_on_with (nnreal.of_real C) f s:= hs.lipschitz_on_with_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_within_at) bound /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `fderiv`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_le {f : E → F} {C : ℝ} {s : set E} {x y : E} (hf : ∀ x ∈ s, differentiable_at ℝ f x) (bound : ∀x∈s, ∥fderiv ℝ f x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound xs ys /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `fderiv` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_fderiv_le {f : E → F} {C : ℝ} {s : set E} (hf : ∀ x ∈ s, differentiable_at ℝ f x) (bound : ∀x∈s, ∥fderiv ℝ f x∥ ≤ C) (hs : convex s) : lipschitz_on_with (nnreal.of_real C) f s := hs.lipschitz_on_with_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound /-- Variant of the mean value inequality on a convex set, using a bound on the difference between the derivative and a fixed linear map, rather than a bound on the derivative itself. Version with `has_fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_has_fderiv_within_le' {f : E → F} {C : ℝ} {s : set E} {x y : E} {f' : E → (E →L[ℝ] F)} {φ : E →L[ℝ] F} (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x - φ∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x - φ (y - x)∥ ≤ C * ∥y - x∥ := begin /- We subtract `φ` to define a new function `g` for which `g' = 0`, for which the previous theorem applies, `convex.norm_image_sub_le_of_norm_has_fderiv_within_le`. Then, we just need to glue together the pieces, expressing back `f` in terms of `g`. -/ let g := λy, f y - φ y, have hg : ∀ x ∈ s, has_fderiv_within_at g (f' x - φ) s x := λ x xs, (hf x xs).sub φ.has_fderiv_within_at, calc ∥f y - f x - φ (y - x)∥ = ∥f y - f x - (φ y - φ x)∥ : by simp ... = ∥(f y - φ y) - (f x - φ x)∥ : by abel ... = ∥g y - g x∥ : by simp ... ≤ C * ∥y - x∥ : convex.norm_image_sub_le_of_norm_has_fderiv_within_le hg bound hs xs ys, end /-- Variant of the mean value inequality on a convex set. Version with `fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_within_le' {f : E → F} {C : ℝ} {s : set E} {x y : E} {φ : E →L[ℝ] F} (hf : differentiable_on ℝ f s) (bound : ∀x∈s, ∥fderiv_within ℝ f s x - φ∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x - φ (y - x)∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_fderiv_within_le' (λ x hx, (hf x hx).has_fderiv_within_at) bound xs ys /-- Variant of the mean value inequality on a convex set. Version with `fderiv`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_le' {f : E → F} {C : ℝ} {s : set E} {x y : E} {φ : E →L[ℝ] F} (hf : ∀ x ∈ s, differentiable_at ℝ f x) (bound : ∀x∈s, ∥fderiv ℝ f x - φ∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x - φ (y - x)∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_fderiv_within_le' (λ x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound xs ys /-- If a function has zero Fréchet derivative at every point of a convex set, then it is a constant on this set. -/ theorem convex.is_const_of_fderiv_within_eq_zero {s : set E} (hs : convex s) {f : E → F} (hf : differentiable_on ℝ f s) (hf' : ∀ x ∈ s, fderiv_within ℝ f s x = 0) {x y : E} (hx : x ∈ s) (hy : y ∈ s) : f x = f y := have bound : ∀ x ∈ s, ∥fderiv_within ℝ f s x∥ ≤ 0, from λ x hx, by simp only [hf' x hx, norm_zero], by simpa only [(dist_eq_norm _ _).symm, zero_mul, dist_le_zero, eq_comm] using hs.norm_image_sub_le_of_norm_fderiv_within_le hf bound hx hy theorem is_const_of_fderiv_eq_zero {f : E → F} (hf : differentiable ℝ f) (hf' : ∀ x, fderiv ℝ f x = 0) (x y : E) : f x = f y := convex_univ.is_const_of_fderiv_within_eq_zero hf.differentiable_on (λ x _, by rw fderiv_within_univ; exact hf' x) trivial trivial /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `has_deriv_within`. -/ theorem convex.norm_image_sub_le_of_norm_has_deriv_within_le {f f' : ℝ → F} {C : ℝ} {s : set ℝ} {x y : ℝ} (hf : ∀ x ∈ s, has_deriv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := convex.norm_image_sub_le_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_within_at) (λ x hx, le_trans (by simp) (bound x hx)) hs xs ys /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `has_deriv_within` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_has_deriv_within_le {f f' : ℝ → F} {C : ℝ} {s : set ℝ} (hs : convex s) (hf : ∀ x ∈ s, has_deriv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x∥ ≤ C) : lipschitz_on_with (nnreal.of_real C) f s := convex.lipschitz_on_with_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_within_at) (λ x hx, le_trans (by simp) (bound x hx)) hs /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function within this set is bounded by `C`, then the function is `C`-Lipschitz. Version with `deriv_within` -/ theorem convex.norm_image_sub_le_of_norm_deriv_within_le {f : ℝ → F} {C : ℝ} {s : set ℝ} {x y : ℝ} (hf : differentiable_on ℝ f s) (bound : ∀x∈s, ∥deriv_within f s x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_deriv_within_le (λ x hx, (hf x hx).has_deriv_within_at) bound xs ys /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `deriv_within` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_deriv_within_le {f : ℝ → F} {C : ℝ} {s : set ℝ} (hs : convex s) (hf : differentiable_on ℝ f s) (bound : ∀x∈s, ∥deriv_within f s x∥ ≤ C) : lipschitz_on_with (nnreal.of_real C) f s := hs.lipschitz_on_with_of_norm_has_deriv_within_le (λ x hx, (hf x hx).has_deriv_within_at) bound /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `deriv`. -/ theorem convex.norm_image_sub_le_of_norm_deriv_le {f : ℝ → F} {C : ℝ} {s : set ℝ} {x y : ℝ} (hf : ∀ x ∈ s, differentiable_at ℝ f x) (bound : ∀x∈s, ∥deriv f x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_deriv_within_le (λ x hx, (hf x hx).has_deriv_at.has_deriv_within_at) bound xs ys /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `deriv` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_deriv_le {f : ℝ → F} {C : ℝ} {s : set ℝ} (hf : ∀ x ∈ s, differentiable_at ℝ f x) (bound : ∀x∈s, ∥deriv f x∥ ≤ C) (hs : convex s) : lipschitz_on_with (nnreal.of_real C) f s := hs.lipschitz_on_with_of_norm_has_deriv_within_le (λ x hx, (hf x hx).has_deriv_at.has_deriv_within_at) bound /-! ### Functions `[a, b] → ℝ`. -/ section interval -- Declare all variables here to make sure they come in a correct order variables (f f' : ℝ → ℝ) {a b : ℝ} (hab : a < b) (hfc : continuous_on f (Icc a b)) (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hfd : differentiable_on ℝ f (Ioo a b)) (g g' : ℝ → ℝ) (hgc : continuous_on g (Icc a b)) (hgg' : ∀ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hgd : differentiable_on ℝ g (Ioo a b)) include hab hfc hff' hgc hgg' /-- Cauchy's Mean Value Theorem, `has_deriv_at` version. -/ lemma exists_ratio_has_deriv_at_eq_ratio_slope : ∃ c ∈ Ioo a b, (g b - g a) * f' c = (f b - f a) * g' c := begin let h := λ x, (g b - g a) * f x - (f b - f a) * g x, have hI : h a = h b, { simp only [h], ring }, let h' := λ x, (g b - g a) * f' x - (f b - f a) * g' x, have hhh' : ∀ x ∈ Ioo a b, has_deriv_at h (h' x) x, from λ x hx, ((hff' x hx).const_mul (g b - g a)).sub ((hgg' x hx).const_mul (f b - f a)), have hhc : continuous_on h (Icc a b), from (continuous_on_const.mul hfc).sub (continuous_on_const.mul hgc), rcases exists_has_deriv_at_eq_zero h h' hab hhc hI hhh' with ⟨c, cmem, hc⟩, exact ⟨c, cmem, sub_eq_zero.1 hc⟩ end omit hfc hgc /-- Cauchy's Mean Value Theorem, extended `has_deriv_at` version. -/ lemma exists_ratio_has_deriv_at_eq_ratio_slope' {lfa lga lfb lgb : ℝ} (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : ∀ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hfa : tendsto f (𝓝[Ioi a] a) (𝓝 lfa)) (hga : tendsto g (𝓝[Ioi a] a) (𝓝 lga)) (hfb : tendsto f (𝓝[Iio b] b) (𝓝 lfb)) (hgb : tendsto g (𝓝[Iio b] b) (𝓝 lgb)) : ∃ c ∈ Ioo a b, (lgb - lga) * (f' c) = (lfb - lfa) * (g' c) := begin let h := λ x, (lgb - lga) * f x - (lfb - lfa) * g x, have hha : tendsto h (𝓝[Ioi a] a) (𝓝 $ lgb * lfa - lfb * lga), { have : tendsto h (𝓝[Ioi a] a)(𝓝 $ (lgb - lga) * lfa - (lfb - lfa) * lga) := (tendsto_const_nhds.mul hfa).sub (tendsto_const_nhds.mul hga), convert this using 2, ring }, have hhb : tendsto h (𝓝[Iio b] b) (𝓝 $ lgb * lfa - lfb * lga), { have : tendsto h (𝓝[Iio b] b)(𝓝 $ (lgb - lga) * lfb - (lfb - lfa) * lgb) := (tendsto_const_nhds.mul hfb).sub (tendsto_const_nhds.mul hgb), convert this using 2, ring }, let h' := λ x, (lgb - lga) * f' x - (lfb - lfa) * g' x, have hhh' : ∀ x ∈ Ioo a b, has_deriv_at h (h' x) x, { intros x hx, exact ((hff' x hx).const_mul _ ).sub (((hgg' x hx)).const_mul _) }, rcases exists_has_deriv_at_eq_zero' hab hha hhb hhh' with ⟨c, cmem, hc⟩, exact ⟨c, cmem, sub_eq_zero.1 hc⟩ end include hfc omit hgg' /-- Lagrange's Mean Value Theorem, `has_deriv_at` version -/ lemma exists_has_deriv_at_eq_slope : ∃ c ∈ Ioo a b, f' c = (f b - f a) / (b - a) := begin rcases exists_ratio_has_deriv_at_eq_ratio_slope f f' hab hfc hff' id 1 continuous_id.continuous_on (λ x hx, has_deriv_at_id x) with ⟨c, cmem, hc⟩, use [c, cmem], simp only [_root_.id, pi.one_apply, mul_one] at hc, rw [← hc, mul_div_cancel_left], exact ne_of_gt (sub_pos.2 hab) end omit hff' /-- Cauchy's Mean Value Theorem, `deriv` version. -/ lemma exists_ratio_deriv_eq_ratio_slope : ∃ c ∈ Ioo a b, (g b - g a) * (deriv f c) = (f b - f a) * (deriv g c) := exists_ratio_has_deriv_at_eq_ratio_slope f (deriv f) hab hfc (λ x hx, ((hfd x hx).differentiable_at $ mem_nhds_sets is_open_Ioo hx).has_deriv_at) g (deriv g) hgc (λ x hx, ((hgd x hx).differentiable_at $ mem_nhds_sets is_open_Ioo hx).has_deriv_at) omit hfc /-- Cauchy's Mean Value Theorem, extended `deriv` version. -/ lemma exists_ratio_deriv_eq_ratio_slope' {lfa lga lfb lgb : ℝ} (hdf : differentiable_on ℝ f $ Ioo a b) (hdg : differentiable_on ℝ g $ Ioo a b) (hfa : tendsto f (𝓝[Ioi a] a) (𝓝 lfa)) (hga : tendsto g (𝓝[Ioi a] a) (𝓝 lga)) (hfb : tendsto f (𝓝[Iio b] b) (𝓝 lfb)) (hgb : tendsto g (𝓝[Iio b] b) (𝓝 lgb)) : ∃ c ∈ Ioo a b, (lgb - lga) * (deriv f c) = (lfb - lfa) * (deriv g c) := exists_ratio_has_deriv_at_eq_ratio_slope' _ _ hab _ _ (λ x hx, ((hdf x hx).differentiable_at $ Ioo_mem_nhds hx.1 hx.2).has_deriv_at) (λ x hx, ((hdg x hx).differentiable_at $ Ioo_mem_nhds hx.1 hx.2).has_deriv_at) hfa hga hfb hgb /-- Lagrange's Mean Value Theorem, `deriv` version. -/ lemma exists_deriv_eq_slope : ∃ c ∈ Ioo a b, deriv f c = (f b - f a) / (b - a) := exists_has_deriv_at_eq_slope f (deriv f) hab hfc (λ x hx, ((hfd x hx).differentiable_at $ mem_nhds_sets is_open_Ioo hx).has_deriv_at) end interval /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `C < f'`, then `f` grows faster than `C * x` on `D`, i.e., `C * (y - x) < f y - f x` whenever `x, y ∈ D`, `x < y`. -/ theorem convex.mul_sub_lt_image_sub_of_lt_deriv {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (hf'_gt : ∀ x ∈ interior D, C < deriv f x) : ∀ x y ∈ D, x < y → C * (y - x) < f y - f x := begin assume x y hx hy hxy, have hxyD : Icc x y ⊆ D, from hD.ord_connected hx hy, have hxyD' : Ioo x y ⊆ interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩, obtain ⟨a, a_mem, ha⟩ : ∃ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy (hf.mono hxyD) (hf'.mono hxyD'), have : C < (f y - f x) / (y - x), by { rw [← ha], exact hf'_gt _ (hxyD' a_mem) }, exact (lt_div_iff (sub_pos.2 hxy)).1 this end /-- Let `f : ℝ → ℝ` be a differentiable function. If `C < f'`, then `f` grows faster than `C * x`, i.e., `C * (y - x) < f y - f x` whenever `x < y`. -/ theorem mul_sub_lt_image_sub_of_lt_deriv {f : ℝ → ℝ} (hf : differentiable ℝ f) {C} (hf'_gt : ∀ x, C < deriv f x) ⦃x y⦄ (hxy : x < y) : C * (y - x) < f y - f x := convex_univ.mul_sub_lt_image_sub_of_lt_deriv hf.continuous.continuous_on hf.differentiable_on (λ x _, hf'_gt x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `C ≤ f'`, then `f` grows at least as fast as `C * x` on `D`, i.e., `C * (y - x) ≤ f y - f x` whenever `x, y ∈ D`, `x ≤ y`. -/ theorem convex.mul_sub_le_image_sub_of_le_deriv {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (hf'_ge : ∀ x ∈ interior D, C ≤ deriv f x) : ∀ x y ∈ D, x ≤ y → C * (y - x) ≤ f y - f x := begin assume x y hx hy hxy, cases eq_or_lt_of_le hxy with hxy' hxy', by rw [hxy', sub_self, sub_self, mul_zero], have hxyD : Icc x y ⊆ D, from hD.ord_connected hx hy, have hxyD' : Ioo x y ⊆ interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩, obtain ⟨a, a_mem, ha⟩ : ∃ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy' (hf.mono hxyD) (hf'.mono hxyD'), have : C ≤ (f y - f x) / (y - x), by { rw [← ha], exact hf'_ge _ (hxyD' a_mem) }, exact (le_div_iff (sub_pos.2 hxy')).1 this end /-- Let `f : ℝ → ℝ` be a differentiable function. If `C ≤ f'`, then `f` grows at least as fast as `C * x`, i.e., `C * (y - x) ≤ f y - f x` whenever `x ≤ y`. -/ theorem mul_sub_le_image_sub_of_le_deriv {f : ℝ → ℝ} (hf : differentiable ℝ f) {C} (hf'_ge : ∀ x, C ≤ deriv f x) ⦃x y⦄ (hxy : x ≤ y) : C * (y - x) ≤ f y - f x := convex_univ.mul_sub_le_image_sub_of_le_deriv hf.continuous.continuous_on hf.differentiable_on (λ x _, hf'_ge x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f' < C`, then `f` grows slower than `C * x` on `D`, i.e., `f y - f x < C * (y - x)` whenever `x, y ∈ D`, `x < y`. -/ theorem convex.image_sub_lt_mul_sub_of_deriv_lt {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (lt_hf' : ∀ x ∈ interior D, deriv f x < C) : ∀ x y ∈ D, x < y → f y - f x < C * (y - x) := begin assume x y hx hy hxy, have hf'_gt : ∀ x ∈ interior D, -C < deriv (λ y, -f y) x, { assume x hx, rw [deriv.neg, neg_lt_neg_iff], exact lt_hf' x hx }, simpa [-neg_lt_neg_iff] using neg_lt_neg (hD.mul_sub_lt_image_sub_of_lt_deriv hf.neg hf'.neg hf'_gt x y hx hy hxy) end /-- Let `f : ℝ → ℝ` be a differentiable function. If `f' < C`, then `f` grows slower than `C * x` on `D`, i.e., `f y - f x < C * (y - x)` whenever `x < y`. -/ theorem image_sub_lt_mul_sub_of_deriv_lt {f : ℝ → ℝ} (hf : differentiable ℝ f) {C} (lt_hf' : ∀ x, deriv f x < C) ⦃x y⦄ (hxy : x < y) : f y - f x < C * (y - x) := convex_univ.image_sub_lt_mul_sub_of_deriv_lt hf.continuous.continuous_on hf.differentiable_on (λ x _, lt_hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f' ≤ C`, then `f` grows at most as fast as `C * x` on `D`, i.e., `f y - f x ≤ C * (y - x)` whenever `x, y ∈ D`, `x ≤ y`. -/ theorem convex.image_sub_le_mul_sub_of_deriv_le {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (le_hf' : ∀ x ∈ interior D, deriv f x ≤ C) : ∀ x y ∈ D, x ≤ y → f y - f x ≤ C * (y - x) := begin assume x y hx hy hxy, have hf'_ge : ∀ x ∈ interior D, -C ≤ deriv (λ y, -f y) x, { assume x hx, rw [deriv.neg, neg_le_neg_iff], exact le_hf' x hx }, simpa [-neg_le_neg_iff] using neg_le_neg (hD.mul_sub_le_image_sub_of_le_deriv hf.neg hf'.neg hf'_ge x y hx hy hxy) end /-- Let `f : ℝ → ℝ` be a differentiable function. If `f' ≤ C`, then `f` grows at most as fast as `C * x`, i.e., `f y - f x ≤ C * (y - x)` whenever `x ≤ y`. -/ theorem image_sub_le_mul_sub_of_deriv_le {f : ℝ → ℝ} (hf : differentiable ℝ f) {C} (le_hf' : ∀ x, deriv f x ≤ C) ⦃x y⦄ (hxy : x ≤ y) : f y - f x ≤ C * (y - x) := convex_univ.image_sub_le_mul_sub_of_deriv_le hf.continuous.continuous_on hf.differentiable_on (λ x _, le_hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is positive, then `f` is a strictly monotonically increasing function on `D`. -/ theorem convex.strict_mono_of_deriv_pos {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_pos : ∀ x ∈ interior D, 0 < deriv f x) : ∀ x y ∈ D, x < y → f x < f y := by simpa only [zero_mul, sub_pos] using hD.mul_sub_lt_image_sub_of_lt_deriv hf hf' hf'_pos /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is positive, then `f` is a strictly monotonically increasing function. -/ theorem strict_mono_of_deriv_pos {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf'_pos : ∀ x, 0 < deriv f x) : strict_mono f := λ x y hxy, convex_univ.strict_mono_of_deriv_pos hf.continuous.continuous_on hf.differentiable_on (λ x _, hf'_pos x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is nonnegative, then `f` is a monotonically increasing function on `D`. -/ theorem convex.mono_of_deriv_nonneg {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_nonneg : ∀ x ∈ interior D, 0 ≤ deriv f x) : ∀ x y ∈ D, x ≤ y → f x ≤ f y := by simpa only [zero_mul, sub_nonneg] using hD.mul_sub_le_image_sub_of_le_deriv hf hf' hf'_nonneg /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is nonnegative, then `f` is a monotonically increasing function. -/ theorem mono_of_deriv_nonneg {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf' : ∀ x, 0 ≤ deriv f x) : monotone f := λ x y hxy, convex_univ.mono_of_deriv_nonneg hf.continuous.continuous_on hf.differentiable_on (λ x _, hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is negative, then `f` is a strictly monotonically decreasing function on `D`. -/ theorem convex.strict_antimono_of_deriv_neg {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_neg : ∀ x ∈ interior D, deriv f x < 0) : ∀ x y ∈ D, x < y → f y < f x := by simpa only [zero_mul, sub_lt_zero] using hD.image_sub_lt_mul_sub_of_deriv_lt hf hf' hf'_neg /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is negative, then `f` is a strictly monotonically decreasing function. -/ theorem strict_antimono_of_deriv_neg {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf' : ∀ x, deriv f x < 0) : ∀ ⦃x y⦄, x < y → f y < f x := λ x y hxy, convex_univ.strict_antimono_of_deriv_neg hf.continuous.continuous_on hf.differentiable_on (λ x _, hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is nonpositive, then `f` is a monotonically decreasing function on `D`. -/ theorem convex.antimono_of_deriv_nonpos {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_nonpos : ∀ x ∈ interior D, deriv f x ≤ 0) : ∀ x y ∈ D, x ≤ y → f y ≤ f x := by simpa only [zero_mul, sub_nonpos] using hD.image_sub_le_mul_sub_of_deriv_le hf hf' hf'_nonpos /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is nonpositive, then `f` is a monotonically decreasing function. -/ theorem antimono_of_deriv_nonpos {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf' : ∀ x, deriv f x ≤ 0) : ∀ ⦃x y⦄, x ≤ y → f y ≤ f x := λ x y hxy, convex_univ.antimono_of_deriv_nonpos hf.continuous.continuous_on hf.differentiable_on (λ x _, hf' x) x y trivial trivial hxy /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is differentiable on its interior, and `f'` is monotone on the interior, then `f` is convex on `D`. -/ theorem convex_on_of_deriv_mono {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_mono : ∀ x y ∈ interior D, x ≤ y → deriv f x ≤ deriv f y) : convex_on D f := convex_on_real_of_slope_mono_adjacent hD begin intros x y z hx hz hxy hyz, -- First we prove some trivial inclusions have hxzD : Icc x z ⊆ D, from hD.ord_connected hx hz, have hxyD : Icc x y ⊆ D, from subset.trans (Icc_subset_Icc_right $ le_of_lt hyz) hxzD, have hxyD' : Ioo x y ⊆ interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩, have hyzD : Icc y z ⊆ D, from subset.trans (Icc_subset_Icc_left $ le_of_lt hxy) hxzD, have hyzD' : Ioo y z ⊆ interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hyzD⟩, -- Then we apply MVT to both `[x, y]` and `[y, z]` obtain ⟨a, ⟨hxa, hay⟩, ha⟩ : ∃ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy (hf.mono hxyD) (hf'.mono hxyD'), obtain ⟨b, ⟨hyb, hbz⟩, hb⟩ : ∃ b ∈ Ioo y z, deriv f b = (f z - f y) / (z - y), from exists_deriv_eq_slope f hyz (hf.mono hyzD) (hf'.mono hyzD'), rw [← ha, ← hb], exact hf'_mono a b (hxyD' ⟨hxa, hay⟩) (hyzD' ⟨hyb, hbz⟩) (le_of_lt $ lt_trans hay hyb) end /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is differentiable on its interior, and `f'` is monotone on the interior, then `f` is convex on `ℝ`. -/ theorem convex_on_univ_of_deriv_mono {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf'_mono : monotone (deriv f)) : convex_on univ f := convex_on_of_deriv_mono convex_univ hf.continuous.continuous_on hf.differentiable_on (λ x y _ _ h, hf'_mono h) /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is twice differentiable on its interior, and `f''` is nonnegative on the interior, then `f` is convex on `D`. -/ theorem convex_on_of_deriv2_nonneg {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'' : differentiable_on ℝ (deriv f) (interior D)) (hf''_nonneg : ∀ x ∈ interior D, 0 ≤ (deriv^[2] f x)) : convex_on D f := convex_on_of_deriv_mono hD hf hf' $ assume x y hx hy hxy, hD.interior.mono_of_deriv_nonneg hf''.continuous_on (by rwa [interior_interior]) (by rwa [interior_interior]) _ _ hx hy hxy /-- If a function `f` is twice differentiable on `ℝ`, and `f''` is nonnegative on `ℝ`, then `f` is convex on `ℝ`. -/ theorem convex_on_univ_of_deriv2_nonneg {f : ℝ → ℝ} (hf' : differentiable ℝ f) (hf'' : differentiable ℝ (deriv f)) (hf''_nonneg : ∀ x, 0 ≤ (deriv^[2] f x)) : convex_on univ f := convex_on_of_deriv2_nonneg convex_univ hf'.continuous.continuous_on hf'.differentiable_on hf''.differentiable_on (λ x _, hf''_nonneg x) /-! ### Functions `f : E → ℝ` -/ /-- Lagrange's Mean Value Theorem, applied to convex domains. -/ theorem domain_mvt {f : E → ℝ} {s : set E} {x y : E} {f' : E → (E →L[ℝ] ℝ)} (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∃ z ∈ segment x y, f y - f x = f' z (y - x) := begin have hIccIoo := @Ioo_subset_Icc_self ℝ _ 0 1, -- parametrize segment set g : ℝ → E := λ t, x + t • (y - x), have hseg : ∀ t ∈ Icc (0:ℝ) 1, g t ∈ segment x y, { rw segment_eq_image', simp only [mem_image, and_imp, add_right_inj], intros t ht, exact ⟨t, ht, rfl⟩ }, have hseg' : Icc 0 1 ⊆ g ⁻¹' s, { rw ← image_subset_iff, unfold image, change ∀ _, _, intros z Hz, rw mem_set_of_eq at Hz, rcases Hz with ⟨t, Ht, hgt⟩, rw ← hgt, exact hs.segment_subset xs ys (hseg t Ht) }, -- derivative of pullback of f under parametrization have hfg: ∀ t ∈ Icc (0:ℝ) 1, has_deriv_within_at (f ∘ g) ((f' (g t) : E → ℝ) (y-x)) (Icc (0:ℝ) 1) t, { intros t Ht, have hg : has_deriv_at g (y-x) t, { have := ((has_deriv_at_id t).smul_const (y - x)).const_add x, rwa one_smul at this }, exact (hf (g t) $ hseg' Ht).comp_has_deriv_within_at _ hg.has_deriv_within_at hseg' }, -- apply 1-variable mean value theorem to pullback have hMVT : ∃ (t ∈ Ioo (0:ℝ) 1), ((f' (g t) : E → ℝ) (y-x)) = (f (g 1) - f (g 0)) / (1 - 0), { refine exists_has_deriv_at_eq_slope (f ∘ g) _ (by norm_num) _ _, { unfold continuous_on, exact λ t Ht, (hfg t Ht).continuous_within_at }, { refine λ t Ht, (hfg t $ hIccIoo Ht).has_deriv_at _, refine mem_nhds_sets_iff.mpr _, use (Ioo (0:ℝ) 1), refine ⟨hIccIoo, _, Ht⟩, simp [real.Ioo_eq_ball, is_open_ball] } }, -- reinterpret on domain rcases hMVT with ⟨t, Ht, hMVT'⟩, use g t, refine ⟨hseg t $ hIccIoo Ht, _⟩, simp [g, hMVT'], end /-! ### Vector-valued functions `f : E → F`. Strict differentiability. -/ /-- Over the reals, a continuously differentiable function is strictly differentiable. -/ lemma strict_fderiv_of_cont_diff {f : E → F} {s : set E} {x : E} {f' : E → (E →L[ℝ] F)} (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hcont : continuous_on f' s) (hs : s ∈ 𝓝 x) : has_strict_fderiv_at f (f' x) x := begin -- turn little-o definition of strict_fderiv into an epsilon-delta statement apply is_o_iff_forall_is_O_with.mpr, intros c hc, refine is_O_with.of_bound (eventually_iff.mpr (mem_nhds_iff.mpr _)), -- the correct ε is the modulus of continuity of f', shrunk to be inside s rcases (metric.continuous_on_iff.mp hcont x (mem_of_nhds hs) c hc) with ⟨ε₁, H₁, hcont'⟩, rcases (mem_nhds_iff.mp hs) with ⟨ε₂, H₂, hε₂⟩, refine ⟨min ε₁ ε₂, lt_min H₁ H₂, _⟩, -- mess with ε construction set t := ball x (min ε₁ ε₂), have hts : t ⊆ s := λ _ hy, hε₂ (ball_subset_ball (min_le_right ε₁ ε₂) hy), have Hf : ∀ y ∈ t, has_fderiv_within_at f (f' y) t y := λ y yt, has_fderiv_within_at.mono (hf y (hts yt)) hts, have hconv := convex_ball x (min ε₁ ε₂), -- simplify formulas involving the product E × E rintros ⟨a, b⟩ h, simp only [mem_set_of_eq, map_sub], have hab : a ∈ t ∧ b ∈ t := by rwa [mem_ball, prod.dist_eq, max_lt_iff] at h, -- exploit the choice of ε as the modulus of continuity of f' have hf' : ∀ x' ∈ t, ∥f' x' - f' x∥ ≤ c, { intros x' H', refine le_of_lt (hcont' x' (hts H') _), exact ball_subset_ball (min_le_left ε₁ ε₂) H' }, -- apply mean value theorem simpa using convex.norm_image_sub_le_of_norm_has_fderiv_within_le' Hf hf' hconv hab.2 hab.1, end
a563045bbb37a47ef0dc5e409e1d4c737448795a
9dc8cecdf3c4634764a18254e94d43da07142918
/src/order/upper_lower.lean
e89c2245b90c064f172e88bdd7d01182d1dec0cc
[ "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
30,958
lean
/- Copyright (c) 2022 Yaël Dillies, Sara Rousta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Sara Rousta -/ import data.set_like.basic import data.set.intervals.ord_connected import order.hom.complete_lattice /-! # Up-sets and down-sets This file defines upper and lower sets in an order. ## Main declarations * `is_upper_set`: Predicate for a set to be an upper set. This means every element greater than a member of the set is in the set itself. * `is_lower_set`: Predicate for a set to be a lower set. This means every element less than a member of the set is in the set itself. * `upper_set`: The type of upper sets. * `lower_set`: The type of lower sets. * `upper_closure`: The greatest upper set containing a set. * `lower_closure`: The least lower set containing a set. * `upper_set.Ici`: Principal upper set. `set.Ici` as an upper set. * `upper_set.Ioi`: Strict principal upper set. `set.Ioi` as an upper set. * `lower_set.Iic`: Principal lower set. `set.Iic` as an lower set. * `lower_set.Iio`: Strict principal lower set. `set.Iio` as an lower set. ## Notes Upper sets are ordered by **reverse** inclusion. This convention is motivated by the fact that this makes them order-isomorphic to lower sets and antichains, and matches the convention on `filter`. ## TODO Lattice structure on antichains. Order equivalence between upper/lower sets and antichains. -/ open order_dual set variables {α : Type*} {ι : Sort*} {κ : ι → Sort*} /-! ### Unbundled upper/lower sets -/ section has_le variables [has_le α] {s t : set α} /-- An upper set in an order `α` is a set such that any element greater than one of its members is also a member. Also called up-set, upward-closed set. -/ def is_upper_set (s : set α) : Prop := ∀ ⦃a b : α⦄, a ≤ b → a ∈ s → b ∈ s /-- A lower set in an order `α` is a set such that any element less than one of its members is also a member. Also called down-set, downward-closed set. -/ def is_lower_set (s : set α) : Prop := ∀ ⦃a b : α⦄, b ≤ a → a ∈ s → b ∈ s lemma is_upper_set_empty : is_upper_set (∅ : set α) := λ _ _ _, id lemma is_lower_set_empty : is_lower_set (∅ : set α) := λ _ _ _, id lemma is_upper_set_univ : is_upper_set (univ : set α) := λ _ _ _, id lemma is_lower_set_univ : is_lower_set (univ : set α) := λ _ _ _, id lemma is_upper_set.compl (hs : is_upper_set s) : is_lower_set sᶜ := λ a b h hb ha, hb $ hs h ha lemma is_lower_set.compl (hs : is_lower_set s) : is_upper_set sᶜ := λ a b h hb ha, hb $ hs h ha @[simp] lemma is_upper_set_compl : is_upper_set sᶜ ↔ is_lower_set s := ⟨λ h, by { convert h.compl, rw compl_compl }, is_lower_set.compl⟩ @[simp] lemma is_lower_set_compl : is_lower_set sᶜ ↔ is_upper_set s := ⟨λ h, by { convert h.compl, rw compl_compl }, is_upper_set.compl⟩ lemma is_upper_set.union (hs : is_upper_set s) (ht : is_upper_set t) : is_upper_set (s ∪ t) := λ a b h, or.imp (hs h) (ht h) lemma is_lower_set.union (hs : is_lower_set s) (ht : is_lower_set t) : is_lower_set (s ∪ t) := λ a b h, or.imp (hs h) (ht h) lemma is_upper_set.inter (hs : is_upper_set s) (ht : is_upper_set t) : is_upper_set (s ∩ t) := λ a b h, and.imp (hs h) (ht h) lemma is_lower_set.inter (hs : is_lower_set s) (ht : is_lower_set t) : is_lower_set (s ∩ t) := λ a b h, and.imp (hs h) (ht h) lemma is_upper_set_Union {f : ι → set α} (hf : ∀ i, is_upper_set (f i)) : is_upper_set (⋃ i, f i) := λ a b h, Exists₂.imp $ forall_range_iff.2 $ λ i, hf i h lemma is_lower_set_Union {f : ι → set α} (hf : ∀ i, is_lower_set (f i)) : is_lower_set (⋃ i, f i) := λ a b h, Exists₂.imp $ forall_range_iff.2 $ λ i, hf i h lemma is_upper_set_Union₂ {f : Π i, κ i → set α} (hf : ∀ i j, is_upper_set (f i j)) : is_upper_set (⋃ i j, f i j) := is_upper_set_Union $ λ i, is_upper_set_Union $ hf i lemma is_lower_set_Union₂ {f : Π i, κ i → set α} (hf : ∀ i j, is_lower_set (f i j)) : is_lower_set (⋃ i j, f i j) := is_lower_set_Union $ λ i, is_lower_set_Union $ hf i lemma is_upper_set_sUnion {S : set (set α)} (hf : ∀ s ∈ S, is_upper_set s) : is_upper_set (⋃₀ S) := λ a b h, Exists₂.imp $ λ s hs, hf s hs h lemma is_lower_set_sUnion {S : set (set α)} (hf : ∀ s ∈ S, is_lower_set s) : is_lower_set (⋃₀ S) := λ a b h, Exists₂.imp $ λ s hs, hf s hs h lemma is_upper_set_Inter {f : ι → set α} (hf : ∀ i, is_upper_set (f i)) : is_upper_set (⋂ i, f i) := λ a b h, forall₂_imp $ forall_range_iff.2 $ λ i, hf i h lemma is_lower_set_Inter {f : ι → set α} (hf : ∀ i, is_lower_set (f i)) : is_lower_set (⋂ i, f i) := λ a b h, forall₂_imp $ forall_range_iff.2 $ λ i, hf i h lemma is_upper_set_Inter₂ {f : Π i, κ i → set α} (hf : ∀ i j, is_upper_set (f i j)) : is_upper_set (⋂ i j, f i j) := is_upper_set_Inter $ λ i, is_upper_set_Inter $ hf i lemma is_lower_set_Inter₂ {f : Π i, κ i → set α} (hf : ∀ i j, is_lower_set (f i j)) : is_lower_set (⋂ i j, f i j) := is_lower_set_Inter $ λ i, is_lower_set_Inter $ hf i lemma is_upper_set_sInter {S : set (set α)} (hf : ∀ s ∈ S, is_upper_set s) : is_upper_set (⋂₀ S) := λ a b h, forall₂_imp $ λ s hs, hf s hs h lemma is_lower_set_sInter {S : set (set α)} (hf : ∀ s ∈ S, is_lower_set s) : is_lower_set (⋂₀ S) := λ a b h, forall₂_imp $ λ s hs, hf s hs h @[simp] lemma is_lower_set_preimage_of_dual_iff : is_lower_set (of_dual ⁻¹' s) ↔ is_upper_set s := iff.rfl @[simp] lemma is_upper_set_preimage_of_dual_iff : is_upper_set (of_dual ⁻¹' s) ↔ is_lower_set s := iff.rfl @[simp] lemma is_lower_set_preimage_to_dual_iff {s : set αᵒᵈ} : is_lower_set (to_dual ⁻¹' s) ↔ is_upper_set s := iff.rfl @[simp] lemma is_upper_set_preimage_to_dual_iff {s : set αᵒᵈ} : is_upper_set (to_dual ⁻¹' s) ↔ is_lower_set s := iff.rfl alias is_lower_set_preimage_of_dual_iff ↔ _ is_upper_set.of_dual alias is_upper_set_preimage_of_dual_iff ↔ _ is_lower_set.of_dual alias is_lower_set_preimage_to_dual_iff ↔ _ is_upper_set.to_dual alias is_upper_set_preimage_to_dual_iff ↔ _ is_lower_set.to_dual end has_le section preorder variables [preorder α] {s : set α} (a : α) lemma is_upper_set_Ici : is_upper_set (Ici a) := λ _ _, ge_trans lemma is_lower_set_Iic : is_lower_set (Iic a) := λ _ _, le_trans lemma is_upper_set_Ioi : is_upper_set (Ioi a) := λ _ _, flip lt_of_lt_of_le lemma is_lower_set_Iio : is_lower_set (Iio a) := λ _ _, lt_of_le_of_lt lemma is_upper_set_iff_Ici_subset : is_upper_set s ↔ ∀ ⦃a⦄, a ∈ s → Ici a ⊆ s := by simp [is_upper_set, subset_def, @forall_swap (_ ∈ s)] lemma is_lower_set_iff_Iic_subset : is_lower_set s ↔ ∀ ⦃a⦄, a ∈ s → Iic a ⊆ s := by simp [is_lower_set, subset_def, @forall_swap (_ ∈ s)] alias is_upper_set_iff_Ici_subset ↔ is_upper_set.Ici_subset _ alias is_lower_set_iff_Iic_subset ↔ is_lower_set.Iic_subset _ lemma is_upper_set.ord_connected (h : is_upper_set s) : s.ord_connected := ⟨λ a ha b _, Icc_subset_Ici_self.trans $ h.Ici_subset ha⟩ lemma is_lower_set.ord_connected (h : is_lower_set s) : s.ord_connected := ⟨λ a _ b hb, Icc_subset_Iic_self.trans $ h.Iic_subset hb⟩ section order_top variables [order_top α] lemma is_lower_set.top_mem (hs : is_lower_set s) : ⊤ ∈ s ↔ s = univ := ⟨λ h, eq_univ_of_forall $ λ a, hs le_top h, λ h, h.symm ▸ mem_univ _⟩ lemma is_upper_set.top_mem (hs : is_upper_set s) : ⊤ ∈ s ↔ s.nonempty := ⟨λ h, ⟨_, h⟩, λ ⟨a, ha⟩, hs le_top ha⟩ lemma is_upper_set.not_top_mem (hs : is_upper_set s) : ⊤ ∉ s ↔ s = ∅ := hs.top_mem.not.trans not_nonempty_iff_eq_empty end order_top section order_bot variables [order_bot α] lemma is_upper_set.bot_mem (hs : is_upper_set s) : ⊥ ∈ s ↔ s = univ := ⟨λ h, eq_univ_of_forall $ λ a, hs bot_le h, λ h, h.symm ▸ mem_univ _⟩ lemma is_lower_set.bot_mem (hs : is_lower_set s) : ⊥ ∈ s ↔ s.nonempty := ⟨λ h, ⟨_, h⟩, λ ⟨a, ha⟩, hs bot_le ha⟩ lemma is_lower_set.not_bot_mem (hs : is_lower_set s) : ⊥ ∉ s ↔ s = ∅ := hs.bot_mem.not.trans not_nonempty_iff_eq_empty end order_bot section no_max_order variables [no_max_order α] (a) lemma is_upper_set.not_bdd_above (hs : is_upper_set s) : s.nonempty → ¬ bdd_above s := begin rintro ⟨a, ha⟩ ⟨b, hb⟩, obtain ⟨c, hc⟩ := exists_gt b, exact hc.not_le (hb $ hs ((hb ha).trans hc.le) ha), end lemma not_bdd_above_Ici : ¬ bdd_above (Ici a) := (is_upper_set_Ici _).not_bdd_above nonempty_Ici lemma not_bdd_above_Ioi : ¬ bdd_above (Ioi a) := (is_upper_set_Ioi _).not_bdd_above nonempty_Ioi end no_max_order section no_min_order variables [no_min_order α] (a) lemma is_lower_set.not_bdd_below (hs : is_lower_set s) : s.nonempty → ¬ bdd_below s := begin rintro ⟨a, ha⟩ ⟨b, hb⟩, obtain ⟨c, hc⟩ := exists_lt b, exact hc.not_le (hb $ hs (hc.le.trans $ hb ha) ha), end lemma not_bdd_below_Iic : ¬ bdd_below (Iic a) := (is_lower_set_Iic _).not_bdd_below nonempty_Iic lemma not_bdd_below_Iio : ¬ bdd_below (Iio a) := (is_lower_set_Iio _).not_bdd_below nonempty_Iio end no_min_order end preorder /-! ### Bundled upper/lower sets -/ section has_le variables [has_le α] /-- The type of upper sets of an order. -/ structure upper_set (α : Type*) [has_le α] := (carrier : set α) (upper' : is_upper_set carrier) /-- The type of lower sets of an order. -/ structure lower_set (α : Type*) [has_le α] := (carrier : set α) (lower' : is_lower_set carrier) namespace upper_set instance : set_like (upper_set α) α := { coe := upper_set.carrier, coe_injective' := λ s t h, by { cases s, cases t, congr' } } @[ext] lemma ext {s t : upper_set α} : (s : set α) = t → s = t := set_like.ext' @[simp] lemma carrier_eq_coe (s : upper_set α) : s.carrier = s := rfl protected lemma upper (s : upper_set α) : is_upper_set (s : set α) := s.upper' end upper_set namespace lower_set instance : set_like (lower_set α) α := { coe := lower_set.carrier, coe_injective' := λ s t h, by { cases s, cases t, congr' } } @[ext] lemma ext {s t : lower_set α} : (s : set α) = t → s = t := set_like.ext' @[simp] lemma carrier_eq_coe (s : lower_set α) : s.carrier = s := rfl protected lemma lower (s : lower_set α) : is_lower_set (s : set α) := s.lower' end lower_set /-! #### Order -/ namespace upper_set variables {S : set (upper_set α)} {s t : upper_set α} {a : α} instance : has_sup (upper_set α) := ⟨λ s t, ⟨s ∩ t, s.upper.inter t.upper⟩⟩ instance : has_inf (upper_set α) := ⟨λ s t, ⟨s ∪ t, s.upper.union t.upper⟩⟩ instance : has_top (upper_set α) := ⟨⟨∅, is_upper_set_empty⟩⟩ instance : has_bot (upper_set α) := ⟨⟨univ, is_upper_set_univ⟩⟩ instance : has_Sup (upper_set α) := ⟨λ S, ⟨⋂ s ∈ S, ↑s, is_upper_set_Inter₂ $ λ s _, s.upper⟩⟩ instance : has_Inf (upper_set α) := ⟨λ S, ⟨⋃ s ∈ S, ↑s, is_upper_set_Union₂ $ λ s _, s.upper⟩⟩ instance : complete_distrib_lattice (upper_set α) := (to_dual.injective.comp $ set_like.coe_injective).complete_distrib_lattice _ (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) rfl rfl instance : inhabited (upper_set α) := ⟨⊥⟩ @[simp, norm_cast] lemma coe_subset_coe : (s : set α) ⊆ t ↔ t ≤ s := iff.rfl @[simp, norm_cast] lemma coe_top : ((⊤ : upper_set α) : set α) = ∅ := rfl @[simp, norm_cast] lemma coe_bot : ((⊥ : upper_set α) : set α) = univ := rfl @[simp, norm_cast] lemma coe_sup (s t : upper_set α) : (↑(s ⊔ t) : set α) = s ∩ t := rfl @[simp, norm_cast] lemma coe_inf (s t : upper_set α) : (↑(s ⊓ t) : set α) = s ∪ t := rfl @[simp, norm_cast] lemma coe_Sup (S : set (upper_set α)) : (↑(Sup S) : set α) = ⋂ s ∈ S, ↑s := rfl @[simp, norm_cast] lemma coe_Inf (S : set (upper_set α)) : (↑(Inf S) : set α) = ⋃ s ∈ S, ↑s := rfl @[simp, norm_cast] lemma coe_supr (f : ι → upper_set α) : (↑(⨆ i, f i) : set α) = ⋂ i, f i := by simp [supr] @[simp, norm_cast] lemma coe_infi (f : ι → upper_set α) : (↑(⨅ i, f i) : set α) = ⋃ i, f i := by simp [infi] @[simp, norm_cast] lemma coe_supr₂ (f : Π i, κ i → upper_set α) : (↑(⨆ i j, f i j) : set α) = ⋂ i j, f i j := by simp_rw coe_supr @[simp, norm_cast] lemma coe_infi₂ (f : Π i, κ i → upper_set α) : (↑(⨅ i j, f i j) : set α) = ⋃ i j, f i j := by simp_rw coe_infi @[simp] lemma not_mem_top : a ∉ (⊤ : upper_set α) := id @[simp] lemma mem_bot : a ∈ (⊥ : upper_set α) := trivial @[simp] lemma mem_sup_iff : a ∈ s ⊔ t ↔ a ∈ s ∧ a ∈ t := iff.rfl @[simp] lemma mem_inf_iff : a ∈ s ⊓ t ↔ a ∈ s ∨ a ∈ t := iff.rfl @[simp] lemma mem_Sup_iff : a ∈ Sup S ↔ ∀ s ∈ S, a ∈ s := mem_Inter₂ @[simp] lemma mem_Inf_iff : a ∈ Inf S ↔ ∃ s ∈ S, a ∈ s := mem_Union₂ @[simp] lemma mem_supr_iff {f : ι → upper_set α} : a ∈ (⨆ i, f i) ↔ ∀ i, a ∈ f i := by { rw [←set_like.mem_coe, coe_supr], exact mem_Inter } @[simp] lemma mem_infi_iff {f : ι → upper_set α} : a ∈ (⨅ i, f i) ↔ ∃ i, a ∈ f i := by { rw [←set_like.mem_coe, coe_infi], exact mem_Union } @[simp] lemma mem_supr₂_iff {f : Π i, κ i → upper_set α} : a ∈ (⨆ i j, f i j) ↔ ∀ i j, a ∈ f i j := by simp_rw mem_supr_iff @[simp] lemma mem_infi₂_iff {f : Π i, κ i → upper_set α} : a ∈ (⨅ i j, f i j) ↔ ∃ i j, a ∈ f i j := by simp_rw mem_infi_iff end upper_set namespace lower_set variables {S : set (lower_set α)} {s t : lower_set α} {a : α} instance : has_sup (lower_set α) := ⟨λ s t, ⟨s ∪ t, λ a b h, or.imp (s.lower h) (t.lower h)⟩⟩ instance : has_inf (lower_set α) := ⟨λ s t, ⟨s ∩ t, λ a b h, and.imp (s.lower h) (t.lower h)⟩⟩ instance : has_top (lower_set α) := ⟨⟨univ, λ a b h, id⟩⟩ instance : has_bot (lower_set α) := ⟨⟨∅, λ a b h, id⟩⟩ instance : has_Sup (lower_set α) := ⟨λ S, ⟨⋃ s ∈ S, ↑s, is_lower_set_Union₂ $ λ s _, s.lower⟩⟩ instance : has_Inf (lower_set α) := ⟨λ S, ⟨⋂ s ∈ S, ↑s, is_lower_set_Inter₂ $ λ s _, s.lower⟩⟩ instance : complete_distrib_lattice (lower_set α) := set_like.coe_injective.complete_distrib_lattice _ (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) rfl rfl instance : inhabited (lower_set α) := ⟨⊥⟩ @[simp, norm_cast] lemma coe_subset_coe : (s : set α) ⊆ t ↔ s ≤ t := iff.rfl @[simp, norm_cast] lemma coe_top : ((⊤ : lower_set α) : set α) = univ := rfl @[simp, norm_cast] lemma coe_bot : ((⊥ : lower_set α) : set α) = ∅ := rfl @[simp, norm_cast] lemma coe_sup (s t : lower_set α) : (↑(s ⊔ t) : set α) = s ∪ t := rfl @[simp, norm_cast] lemma coe_inf (s t : lower_set α) : (↑(s ⊓ t) : set α) = s ∩ t := rfl @[simp, norm_cast] lemma coe_Sup (S : set (lower_set α)) : (↑(Sup S) : set α) = ⋃ s ∈ S, ↑s := rfl @[simp, norm_cast] lemma coe_Inf (S : set (lower_set α)) : (↑(Inf S) : set α) = ⋂ s ∈ S, ↑s := rfl @[simp, norm_cast] lemma coe_supr (f : ι → lower_set α) : (↑(⨆ i, f i) : set α) = ⋃ i, f i := by simp_rw [supr, coe_Sup, mem_range, Union_exists, Union_Union_eq'] @[simp, norm_cast] lemma coe_infi (f : ι → lower_set α) : (↑(⨅ i, f i) : set α) = ⋂ i, f i := by simp_rw [infi, coe_Inf, mem_range, Inter_exists, Inter_Inter_eq'] @[simp, norm_cast] lemma coe_supr₂ (f : Π i, κ i → lower_set α) : (↑(⨆ i j, f i j) : set α) = ⋃ i j, f i j := by simp_rw coe_supr @[simp, norm_cast] lemma coe_infi₂ (f : Π i, κ i → lower_set α) : (↑(⨅ i j, f i j) : set α) = ⋂ i j, f i j := by simp_rw coe_infi @[simp] lemma mem_top : a ∈ (⊤ : lower_set α) := trivial @[simp] lemma not_mem_bot : a ∉ (⊥ : lower_set α) := id @[simp] lemma mem_sup_iff : a ∈ s ⊔ t ↔ a ∈ s ∨ a ∈ t := iff.rfl @[simp] lemma mem_inf_iff : a ∈ s ⊓ t ↔ a ∈ s ∧ a ∈ t := iff.rfl @[simp] lemma mem_Sup_iff : a ∈ Sup S ↔ ∃ s ∈ S, a ∈ s := mem_Union₂ @[simp] lemma mem_Inf_iff : a ∈ Inf S ↔ ∀ s ∈ S, a ∈ s := mem_Inter₂ @[simp] lemma mem_supr_iff {f : ι → lower_set α} : a ∈ (⨆ i, f i) ↔ ∃ i, a ∈ f i := by { rw [←set_like.mem_coe, coe_supr], exact mem_Union } @[simp] lemma mem_infi_iff {f : ι → lower_set α} : a ∈ (⨅ i, f i) ↔ ∀ i, a ∈ f i := by { rw [←set_like.mem_coe, coe_infi], exact mem_Inter } @[simp] lemma mem_supr₂_iff {f : Π i, κ i → lower_set α} : a ∈ (⨆ i j, f i j) ↔ ∃ i j, a ∈ f i j := by simp_rw mem_supr_iff @[simp] lemma mem_infi₂_iff {f : Π i, κ i → lower_set α} : a ∈ (⨅ i j, f i j) ↔ ∀ i j, a ∈ f i j := by simp_rw mem_infi_iff end lower_set /-! #### Complement -/ /-- The complement of a lower set as an upper set. -/ def upper_set.compl (s : upper_set α) : lower_set α := ⟨sᶜ, s.upper.compl⟩ /-- The complement of a lower set as an upper set. -/ def lower_set.compl (s : lower_set α) : upper_set α := ⟨sᶜ, s.lower.compl⟩ namespace upper_set variables {s t : upper_set α} {a : α} @[simp] lemma coe_compl (s : upper_set α) : (s.compl : set α) = sᶜ := rfl @[simp] lemma mem_compl_iff : a ∈ s.compl ↔ a ∉ s := iff.rfl @[simp] lemma compl_compl (s : upper_set α) : s.compl.compl = s := upper_set.ext $ compl_compl _ @[simp] lemma compl_le_compl : s.compl ≤ t.compl ↔ s ≤ t := compl_subset_compl @[simp] protected lemma compl_sup (s t : upper_set α) : (s ⊔ t).compl = s.compl ⊔ t.compl := lower_set.ext compl_inf @[simp] protected lemma compl_inf (s t : upper_set α) : (s ⊓ t).compl = s.compl ⊓ t.compl := lower_set.ext compl_sup @[simp] protected lemma compl_top : (⊤ : upper_set α).compl = ⊤ := lower_set.ext compl_empty @[simp] protected lemma compl_bot : (⊥ : upper_set α).compl = ⊥ := lower_set.ext compl_univ @[simp] protected lemma compl_Sup (S : set (upper_set α)) : (Sup S).compl = ⨆ s ∈ S, upper_set.compl s := lower_set.ext $ by simp only [coe_compl, coe_Sup, compl_Inter₂, lower_set.coe_supr₂] @[simp] protected lemma compl_Inf (S : set (upper_set α)) : (Inf S).compl = ⨅ s ∈ S, upper_set.compl s := lower_set.ext $ by simp only [coe_compl, coe_Inf, compl_Union₂, lower_set.coe_infi₂] @[simp] protected lemma compl_supr (f : ι → upper_set α) : (⨆ i, f i).compl = ⨆ i, (f i).compl := lower_set.ext $ by simp only [coe_compl, coe_supr, compl_Inter, lower_set.coe_supr] @[simp] protected lemma compl_infi (f : ι → upper_set α) : (⨅ i, f i).compl = ⨅ i, (f i).compl := lower_set.ext $ by simp only [coe_compl, coe_infi, compl_Union, lower_set.coe_infi] @[simp] lemma compl_supr₂ (f : Π i, κ i → upper_set α) : (⨆ i j, f i j).compl = ⨆ i j, (f i j).compl := by simp_rw upper_set.compl_supr @[simp] lemma compl_infi₂ (f : Π i, κ i → upper_set α) : (⨅ i j, f i j).compl = ⨅ i j, (f i j).compl := by simp_rw upper_set.compl_infi end upper_set namespace lower_set variables {s t : lower_set α} {a : α} @[simp] lemma coe_compl (s : lower_set α) : (s.compl : set α) = sᶜ := rfl @[simp] lemma mem_compl_iff : a ∈ s.compl ↔ a ∉ s := iff.rfl @[simp] lemma compl_compl (s : lower_set α) : s.compl.compl = s := lower_set.ext $ compl_compl _ @[simp] lemma compl_le_compl : s.compl ≤ t.compl ↔ s ≤ t := compl_subset_compl protected lemma compl_sup (s t : lower_set α) : (s ⊔ t).compl = s.compl ⊔ t.compl := upper_set.ext compl_sup protected lemma compl_inf (s t : lower_set α) : (s ⊓ t).compl = s.compl ⊓ t.compl := upper_set.ext compl_inf protected lemma compl_top : (⊤ : lower_set α).compl = ⊤ := upper_set.ext compl_univ protected lemma compl_bot : (⊥ : lower_set α).compl = ⊥ := upper_set.ext compl_empty protected lemma compl_Sup (S : set (lower_set α)) : (Sup S).compl = ⨆ s ∈ S, lower_set.compl s := upper_set.ext $ by simp only [coe_compl, coe_Sup, compl_Union₂, upper_set.coe_supr₂] protected lemma compl_Inf (S : set (lower_set α)) : (Inf S).compl = ⨅ s ∈ S, lower_set.compl s := upper_set.ext $ by simp only [coe_compl, coe_Inf, compl_Inter₂, upper_set.coe_infi₂] protected lemma compl_supr (f : ι → lower_set α) : (⨆ i, f i).compl = ⨆ i, (f i).compl := upper_set.ext $ by simp only [coe_compl, coe_supr, compl_Union, upper_set.coe_supr] protected lemma compl_infi (f : ι → lower_set α) : (⨅ i, f i).compl = ⨅ i, (f i).compl := upper_set.ext $ by simp only [coe_compl, coe_infi, compl_Inter, upper_set.coe_infi] @[simp] lemma compl_supr₂ (f : Π i, κ i → lower_set α) : (⨆ i j, f i j).compl = ⨆ i j, (f i j).compl := by simp_rw lower_set.compl_supr @[simp] lemma compl_infi₂ (f : Π i, κ i → lower_set α) : (⨅ i j, f i j).compl = ⨅ i j, (f i j).compl := by simp_rw lower_set.compl_infi end lower_set /-- Upper sets are order-isomorphic to lower sets under complementation. -/ @[simps] def upper_set_iso_lower_set : upper_set α ≃o lower_set α := { to_fun := upper_set.compl, inv_fun := lower_set.compl, left_inv := upper_set.compl_compl, right_inv := lower_set.compl_compl, map_rel_iff' := λ _ _, upper_set.compl_le_compl } end has_le /-! #### Principal sets -/ namespace upper_set section preorder variables [preorder α] {a b : α} /-- The smallest upper set containing a given element. -/ def Ici (a : α) : upper_set α := ⟨Ici a, is_upper_set_Ici a⟩ /-- The smallest upper set containing a given element. -/ def Ioi (a : α) : upper_set α := ⟨Ioi a, is_upper_set_Ioi a⟩ @[simp] lemma coe_Ici (a : α) : ↑(Ici a) = set.Ici a := rfl @[simp] lemma coe_Ioi (a : α) : ↑(Ioi a) = set.Ioi a := rfl @[simp] lemma mem_Ici_iff : b ∈ Ici a ↔ a ≤ b := iff.rfl @[simp] lemma mem_Ioi_iff : b ∈ Ioi a ↔ a < b := iff.rfl lemma Ici_le_Ioi (a : α) : Ici a ≤ Ioi a := Ioi_subset_Ici_self @[simp] lemma Ioi_top [order_top α] : Ioi (⊤ : α) = ⊤ := set_like.coe_injective Ioi_top @[simp] lemma Ici_bot [order_bot α] : Ici (⊥ : α) = ⊥ := set_like.coe_injective Ici_bot end preorder section semilattice_sup variables [semilattice_sup α] @[simp] lemma Ici_sup (a b : α) : Ici (a ⊔ b) = Ici a ⊔ Ici b := ext Ici_inter_Ici.symm /-- `upper_set.Ici` as a `sup_hom`. -/ def Ici_sup_hom : sup_hom α (upper_set α) := ⟨Ici, Ici_sup⟩ @[simp] lemma Ici_sup_hom_apply (a : α) : Ici_sup_hom a = (Ici a) := rfl end semilattice_sup section complete_lattice variables [complete_lattice α] @[simp] lemma Ici_Sup (S : set α) : Ici (Sup S) = ⨆ a ∈ S, Ici a := set_like.ext $ λ c, by simp only [mem_Ici_iff, mem_supr_iff, Sup_le_iff] @[simp] lemma Ici_supr (f : ι → α) : Ici (⨆ i, f i) = ⨆ i, Ici (f i) := set_like.ext $ λ c, by simp only [mem_Ici_iff, mem_supr_iff, supr_le_iff] @[simp] lemma Ici_supr₂ (f : Π i, κ i → α) : Ici (⨆ i j, f i j) = ⨆ i j, Ici (f i j) := by simp_rw Ici_supr /-- `upper_set.Ici` as a `Sup_hom`. -/ def Ici_Sup_hom : Sup_hom α (upper_set α) := ⟨Ici, λ s, (Ici_Sup s).trans Sup_image.symm⟩ @[simp] lemma Ici_Sup_hom_apply (a : α) : Ici_Sup_hom a = to_dual (Ici a) := rfl end complete_lattice end upper_set namespace lower_set section preorder variables [preorder α] {a b : α} /-- Principal lower set. `set.Iic` as a lower set. The smallest lower set containing a given element. -/ def Iic (a : α) : lower_set α := ⟨Iic a, is_lower_set_Iic a⟩ /-- Strict principal lower set. `set.Iio` as a lower set. -/ def Iio (a : α) : lower_set α := ⟨Iio a, is_lower_set_Iio a⟩ @[simp] lemma coe_Iic (a : α) : ↑(Iic a) = set.Iic a := rfl @[simp] lemma coe_Iio (a : α) : ↑(Iio a) = set.Iio a := rfl @[simp] lemma mem_Iic_iff : b ∈ Iic a ↔ b ≤ a := iff.rfl @[simp] lemma mem_Iio_iff : b ∈ Iio a ↔ b < a := iff.rfl lemma Ioi_le_Ici (a : α) : Ioi a ≤ Ici a := Ioi_subset_Ici_self @[simp] lemma Iic_top [order_top α] : Iic (⊤ : α) = ⊤ := set_like.coe_injective Iic_top @[simp] lemma Iio_bot [order_bot α] : Iio (⊥ : α) = ⊥ := set_like.coe_injective Iio_bot end preorder section semilattice_inf variables [semilattice_inf α] @[simp] lemma Iic_inf (a b : α) : Iic (a ⊓ b) = Iic a ⊓ Iic b := set_like.coe_injective Iic_inter_Iic.symm /-- `lower_set.Iic` as an `inf_hom`. -/ def Iic_inf_hom : inf_hom α (lower_set α) := ⟨Iic, Iic_inf⟩ @[simp] lemma coe_Iic_inf_hom : (Iic_inf_hom : α → lower_set α) = Iic := rfl @[simp] lemma Iic_inf_hom_apply (a : α) : Iic_inf_hom a = Iic a := rfl end semilattice_inf section complete_lattice variables [complete_lattice α] @[simp] lemma Iic_Inf (S : set α) : Iic (Inf S) = ⨅ a ∈ S, Iic a := set_like.ext $ λ c, by simp only [mem_Iic_iff, mem_infi₂_iff, le_Inf_iff] @[simp] lemma Iic_infi (f : ι → α) : Iic (⨅ i, f i) = ⨅ i, Iic (f i) := set_like.ext $ λ c, by simp only [mem_Iic_iff, mem_infi_iff, le_infi_iff] @[simp] lemma Iic_infi₂ (f : Π i, κ i → α) : Iic (⨅ i j, f i j) = ⨅ i j, Iic (f i j) := by simp_rw Iic_infi /-- `lower_set.Iic` as an `Inf_hom`. -/ def Iic_Inf_hom : Inf_hom α (lower_set α) := ⟨Iic, λ s, (Iic_Inf s).trans Inf_image.symm⟩ @[simp] lemma coe_Iic_Inf_hom : (Iic_Inf_hom : α → lower_set α) = Iic := rfl @[simp] lemma Iic_Inf_hom_apply (a : α) : Iic_Inf_hom a = Iic a := rfl end complete_lattice end lower_set section closure variables [preorder α] {s t : set α} {x : α} /-- The greatest upper set containing a given set. -/ def upper_closure (s : set α) : upper_set α := ⟨{x | ∃ a ∈ s, a ≤ x}, λ x y h, Exists₂.imp $ λ a _, h.trans'⟩ /-- The least lower set containing a given set. -/ def lower_closure (s : set α) : lower_set α := ⟨{x | ∃ a ∈ s, x ≤ a}, λ x y h, Exists₂.imp $ λ a _, h.trans⟩ @[simp, norm_cast] lemma coe_upper_closure (s : set α) : ↑(upper_closure s) = {x | ∃ a ∈ s, a ≤ x} := rfl @[simp, norm_cast] lemma coe_lower_closure (s : set α) : ↑(lower_closure s) = {x | ∃ a ∈ s, x ≤ a} := rfl @[simp] lemma mem_upper_closure : x ∈ upper_closure s ↔ ∃ a ∈ s, a ≤ x := iff.rfl @[simp] lemma mem_lower_closure : x ∈ lower_closure s ↔ ∃ a ∈ s, x ≤ a := iff.rfl lemma subset_upper_closure : s ⊆ upper_closure s := λ x hx, ⟨x, hx, le_rfl⟩ lemma subset_lower_closure : s ⊆ lower_closure s := λ x hx, ⟨x, hx, le_rfl⟩ lemma upper_closure_min (h : s ⊆ t) (ht : is_upper_set t) : ↑(upper_closure s) ⊆ t := λ a ⟨b, hb, hba⟩, ht hba $ h hb lemma lower_closure_min (h : s ⊆ t) (ht : is_lower_set t) : ↑(lower_closure s) ⊆ t := λ a ⟨b, hb, hab⟩, ht hab $ h hb @[simp] lemma upper_set.infi_Ici (s : set α) : (⨅ a ∈ s, upper_set.Ici a) = upper_closure s := by { ext, simp } @[simp] lemma lower_set.supr_Iic (s : set α) : (⨆ a ∈ s, lower_set.Iic a) = lower_closure s := by { ext, simp } lemma gc_upper_closure_coe : galois_connection (to_dual ∘ upper_closure : set α → (upper_set α)ᵒᵈ) (coe ∘ of_dual) := λ s t, ⟨λ h, subset_upper_closure.trans $ upper_set.coe_subset_coe.2 h, λ h, upper_closure_min h t.upper⟩ lemma gc_lower_closure_coe : galois_connection (lower_closure : set α → lower_set α) coe := λ s t, ⟨λ h, subset_lower_closure.trans $ lower_set.coe_subset_coe.2 h, λ h, lower_closure_min h t.lower⟩ /-- `upper_closure` forms a reversed Galois insertion with the coercion from upper sets to sets. -/ def gi_upper_closure_coe : galois_insertion (to_dual ∘ upper_closure : set α → (upper_set α)ᵒᵈ) (coe ∘ of_dual) := { choice := λ s hs, to_dual (⟨s, λ a b hab ha, hs ⟨a, ha, hab⟩⟩ : upper_set α), gc := gc_upper_closure_coe, le_l_u := λ _, subset_upper_closure, choice_eq := λ s hs, of_dual.injective $ set_like.coe_injective $ subset_upper_closure.antisymm hs } /-- `lower_closure` forms a Galois insertion with the coercion from lower sets to sets. -/ def gi_lower_closure_coe : galois_insertion (lower_closure : set α → lower_set α) coe := { choice := λ s hs, ⟨s, λ a b hba ha, hs ⟨a, ha, hba⟩⟩, gc := gc_lower_closure_coe, le_l_u := λ _, subset_lower_closure, choice_eq := λ s hs, set_like.coe_injective $ subset_lower_closure.antisymm hs } lemma upper_closure_anti : antitone (upper_closure : set α → upper_set α) := gc_upper_closure_coe.monotone_l lemma lower_closure_mono : monotone (lower_closure : set α → lower_set α) := gc_lower_closure_coe.monotone_l @[simp] lemma upper_closure_empty : upper_closure (∅ : set α) = ⊤ := by { ext, simp } @[simp] lemma lower_closure_empty : lower_closure (∅ : set α) = ⊥ := by { ext, simp } @[simp] lemma upper_closure_univ : upper_closure (univ : set α) = ⊥ := le_bot_iff.1 subset_upper_closure @[simp] lemma lower_closure_univ : lower_closure (univ : set α) = ⊤ := top_le_iff.1 subset_lower_closure @[simp] lemma upper_closure_eq_top_iff : upper_closure s = ⊤ ↔ s = ∅ := ⟨λ h, subset_empty_iff.1 $ subset_upper_closure.trans (congr_arg coe h).subset, by { rintro rfl, exact upper_closure_empty }⟩ @[simp] lemma lower_closure_eq_bot_iff : lower_closure s = ⊥ ↔ s = ∅ := ⟨λ h, subset_empty_iff.1 $ subset_lower_closure.trans (congr_arg coe h).subset, by { rintro rfl, exact lower_closure_empty }⟩ @[simp] lemma upper_closure_union (s t : set α) : upper_closure (s ∪ t) = upper_closure s ⊓ upper_closure t := by { ext, simp [or_and_distrib_right, exists_or_distrib] } @[simp] lemma lower_closure_union (s t : set α) : lower_closure (s ∪ t) = lower_closure s ⊔ lower_closure t := by { ext, simp [or_and_distrib_right, exists_or_distrib] } @[simp] lemma upper_closure_Union (f : ι → set α) : upper_closure (⋃ i, f i) = ⨅ i, upper_closure (f i) := by { ext, simp [←exists_and_distrib_right, @exists_comm α] } @[simp] lemma lower_closure_Union (f : ι → set α) : lower_closure (⋃ i, f i) = ⨆ i, lower_closure (f i) := by { ext, simp [←exists_and_distrib_right, @exists_comm α] } @[simp] lemma upper_closure_sUnion (S : set (set α)) : upper_closure (⋃₀ S) = ⨅ s ∈ S, upper_closure s := by simp_rw [sUnion_eq_bUnion, upper_closure_Union] @[simp] lemma lower_closure_sUnion (S : set (set α)) : lower_closure (⋃₀ S) = ⨆ s ∈ S, lower_closure s := by simp_rw [sUnion_eq_bUnion, lower_closure_Union] lemma set.ord_connected.upper_closure_inter_lower_closure (h : s.ord_connected) : ↑(upper_closure s) ∩ ↑(lower_closure s) = s := (subset_inter subset_upper_closure subset_lower_closure).antisymm' $ λ a ⟨⟨b, hb, hba⟩, c, hc, hac⟩, h.out hb hc ⟨hba, hac⟩ lemma ord_connected_iff_upper_closure_inter_lower_closure : s.ord_connected ↔ ↑(upper_closure s) ∩ ↑(lower_closure s) = s := begin refine ⟨set.ord_connected.upper_closure_inter_lower_closure, λ h, _⟩, rw ←h, exact (upper_set.upper _).ord_connected.inter (lower_set.lower _).ord_connected, end end closure
f2cfcd2c0d6b52157b9835dccc47cebf13d764f2
5749d8999a76f3a8fddceca1f6941981e33aaa96
/src/order/zorn.lean
44a51cbc5d5bbba2d30dae9cc240a67e9ab9b10e
[ "Apache-2.0" ]
permissive
jdsalchow/mathlib
13ab43ef0d0515a17e550b16d09bd14b76125276
497e692b946d93906900bb33a51fd243e7649406
refs/heads/master
1,585,819,143,348
1,580,072,892,000
1,580,072,892,000
154,287,128
0
0
Apache-2.0
1,540,281,610,000
1,540,281,609,000
null
UTF-8
Lean
false
false
12,698
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 Zorn's lemmas. Ported from Isabelle/HOL (written by Jacques D. Fleuriot, Tobias Nipkow, and Christian Sternagel). -/ import data.set.lattice noncomputable theory universes u open set classical open_locale classical namespace zorn section chain parameters {α : Type u} (r : α → α → Prop) local infix ` ≺ `:50 := r /-- A chain is a subset `c` satisfying `x ≺ y ∨ x = y ∨ y ≺ x` for all `x y ∈ c`. -/ def chain (c : set α) := pairwise_on c (λx y, x ≺ y ∨ y ≺ x) parameters {r} theorem chain.total_of_refl [is_refl α r] {c} (H : chain c) {x y} (hx : x ∈ c) (hy : y ∈ c) : x ≺ y ∨ y ≺ x := if e : x = y then or.inl (e ▸ refl _) else H _ hx _ hy e theorem chain.mono {c c'} : c' ⊆ c → chain c → chain c' := pairwise_on.mono theorem chain.directed_on [is_refl α r] {c} (H : chain c) : directed_on (≺) c := assume x hx y hy, match H.total_of_refl hx hy with | or.inl h := ⟨y, hy, h, refl _⟩ | or.inr h := ⟨x, hx, refl _, h⟩ end theorem chain_insert {c : set α} {a : α} (hc : chain c) (ha : ∀b∈c, b ≠ a → a ≺ b ∨ b ≺ a) : chain (insert a c) := forall_insert_of_forall (assume x hx, forall_insert_of_forall (hc x hx) (assume hneq, (ha x hx hneq).symm)) (forall_insert_of_forall (assume x hx hneq, ha x hx $ assume h', hneq h'.symm) (assume h, (h rfl).rec _)) /-- `super_chain c₁ c₂` means that `c₂ is a chain that strictly includes `c₁`. -/ def super_chain (c₁ c₂ : set α) : Prop := chain c₂ ∧ c₁ ⊂ c₂ /-- A chain `c` is a maximal chain if there does not exists a chain strictly including `c`. -/ def is_max_chain (c : set α) := chain c ∧ ¬ (∃c', super_chain c c') /-- Given a set `c`, if there exists a chain `c'` strictly including `c`, then `succ_chain c` is one of these chains. Otherwise it is `c`. -/ def succ_chain (c : set α) : set α := if h : ∃c', chain c ∧ super_chain c c' then some h else c theorem succ_spec {c : set α} (h : ∃c', chain c ∧ super_chain c c') : super_chain c (succ_chain c) := let ⟨c', hc'⟩ := h in have chain c ∧ super_chain c (some h), from @some_spec _ (λc', chain c ∧ super_chain c c') _, by simp [succ_chain, dif_pos, h, this.right] theorem chain_succ {c : set α} (hc : chain c) : chain (succ_chain c) := if h : ∃c', chain c ∧ super_chain c c' then (succ_spec h).left else by simp [succ_chain, dif_neg, h]; exact hc theorem super_of_not_max {c : set α} (hc₁ : chain c) (hc₂ : ¬ is_max_chain c) : super_chain c (succ_chain c) := begin simp [is_max_chain, not_and_distrib, not_forall_not] at hc₂, cases hc₂.neg_resolve_left hc₁ with c' hc', exact succ_spec ⟨c', hc₁, hc'⟩ end theorem succ_increasing {c : set α} : c ⊆ succ_chain c := if h : ∃c', chain c ∧ super_chain c c' then have super_chain c (succ_chain c), from succ_spec h, this.right.left else by simp [succ_chain, dif_neg, h, subset.refl] /-- Set of sets reachable from `∅` using `succ_chain` and `⋃₀`. -/ inductive chain_closure : set α → Prop | succ : ∀{s}, chain_closure s → chain_closure (succ_chain s) | union : ∀{s}, (∀a∈s, chain_closure a) → chain_closure (⋃₀ s) theorem chain_closure_empty : chain_closure ∅ := have chain_closure (⋃₀ ∅), from chain_closure.union $ assume a h, h.rec _, by simp at this; assumption theorem chain_closure_closure : chain_closure (⋃₀ chain_closure) := chain_closure.union $ assume s hs, hs variables {c c₁ c₂ c₃ : set α} private lemma chain_closure_succ_total_aux (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h : ∀{c₃}, chain_closure c₃ → c₃ ⊆ c₂ → c₂ = c₃ ∨ succ_chain c₃ ⊆ c₂) : c₁ ⊆ c₂ ∨ succ_chain c₂ ⊆ c₁ := begin induction hc₁, case _root_.zorn.chain_closure.succ : c₃ hc₃ ih { cases ih with ih ih, { have h := h hc₃ ih, cases h with h h, { exact or.inr (h ▸ subset.refl _) }, { exact or.inl h } }, { exact or.inr (subset.trans ih succ_increasing) } }, case _root_.zorn.chain_closure.union : s hs ih { refine (classical.or_iff_not_imp_right.2 $ λ hn, sUnion_subset $ λ a ha, _), apply (ih a ha).resolve_right, apply mt (λ h, _) hn, exact subset.trans h (subset_sUnion_of_mem ha) } end private lemma chain_closure_succ_total (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h : c₁ ⊆ c₂) : c₂ = c₁ ∨ succ_chain c₁ ⊆ c₂ := begin induction hc₂ generalizing c₁ hc₁ h, case _root_.zorn.chain_closure.succ : c₂ hc₂ ih { have h₁ : c₁ ⊆ c₂ ∨ @succ_chain α r c₂ ⊆ c₁ := (chain_closure_succ_total_aux hc₁ hc₂ $ assume c₁, ih), cases h₁ with h₁ h₁, { have h₂ := ih hc₁ h₁, cases h₂ with h₂ h₂, { exact (or.inr $ h₂ ▸ subset.refl _) }, { exact (or.inr $ subset.trans h₂ succ_increasing) } }, { exact (or.inl $ subset.antisymm h₁ h) } }, case _root_.zorn.chain_closure.union : s hs ih { apply or.imp_left (assume h', subset.antisymm h' h), apply classical.by_contradiction, simp [not_or_distrib, sUnion_subset_iff, classical.not_forall], intros c₃ hc₃ h₁ h₂, have h := chain_closure_succ_total_aux hc₁ (hs c₃ hc₃) (assume c₄, ih _ hc₃), cases h with h h, { have h' := ih c₃ hc₃ hc₁ h, cases h' with h' h', { exact (h₁ $ h' ▸ subset.refl _) }, { exact (h₂ $ subset.trans h' $ subset_sUnion_of_mem hc₃) } }, { exact (h₁ $ subset.trans succ_increasing h) } } end theorem chain_closure_total (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) : c₁ ⊆ c₂ ∨ c₂ ⊆ c₁ := have c₁ ⊆ c₂ ∨ succ_chain c₂ ⊆ c₁, from chain_closure_succ_total_aux hc₁ hc₂ $ assume c₃ hc₃, chain_closure_succ_total hc₃ hc₂, or.imp_right (assume : succ_chain c₂ ⊆ c₁, subset.trans succ_increasing this) this theorem chain_closure_succ_fixpoint (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h_eq : succ_chain c₂ = c₂) : c₁ ⊆ c₂ := begin induction hc₁, case _root_.zorn.chain_closure.succ : c₁ hc₁ h { exact or.elim (chain_closure_succ_total hc₁ hc₂ h) (assume h, h ▸ h_eq.symm ▸ subset.refl c₂) id }, case _root_.zorn.chain_closure.union : s hs ih { exact (sUnion_subset $ assume c₁ hc₁, ih c₁ hc₁) } end theorem chain_closure_succ_fixpoint_iff (hc : chain_closure c) : succ_chain c = c ↔ c = ⋃₀ chain_closure := ⟨assume h, subset.antisymm (subset_sUnion_of_mem hc) (chain_closure_succ_fixpoint chain_closure_closure hc h), assume : c = ⋃₀{c : set α | chain_closure c}, subset.antisymm (calc succ_chain c ⊆ ⋃₀{c : set α | chain_closure c} : subset_sUnion_of_mem $ chain_closure.succ hc ... = c : this.symm) succ_increasing⟩ theorem chain_chain_closure (hc : chain_closure c) : chain c := begin induction hc, case _root_.zorn.chain_closure.succ : c hc h { exact chain_succ h }, case _root_.zorn.chain_closure.union : s hs h { have h : ∀c∈s, zorn.chain c := h, exact assume c₁ ⟨t₁, ht₁, (hc₁ : c₁ ∈ t₁)⟩ c₂ ⟨t₂, ht₂, (hc₂ : c₂ ∈ t₂)⟩ hneq, have t₁ ⊆ t₂ ∨ t₂ ⊆ t₁, from chain_closure_total (hs _ ht₁) (hs _ ht₂), or.elim this (assume : t₁ ⊆ t₂, h t₂ ht₂ c₁ (this hc₁) c₂ hc₂ hneq) (assume : t₂ ⊆ t₁, h t₁ ht₁ c₁ hc₁ c₂ (this hc₂) hneq) } end /-- `max_chain` is the union of all sets in the chain closure. -/ def max_chain := ⋃₀ chain_closure /-- Hausdorff's maximality principle There exists a maximal totally ordered subset of `α`. Note that we do not require `α` to be partially ordered by `r`. -/ theorem max_chain_spec : is_max_chain max_chain := classical.by_contradiction $ assume : ¬ is_max_chain (⋃₀ chain_closure), have super_chain (⋃₀ chain_closure) (succ_chain (⋃₀ chain_closure)), from super_of_not_max (chain_chain_closure chain_closure_closure) this, let ⟨h₁, H⟩ := this, ⟨h₂, (h₃ : (⋃₀ chain_closure) ≠ succ_chain (⋃₀ chain_closure))⟩ := ssubset_iff_subset_ne.1 H in have succ_chain (⋃₀ chain_closure) = (⋃₀ chain_closure), from (chain_closure_succ_fixpoint_iff chain_closure_closure).mpr rfl, h₃ this.symm /-- Zorn's lemma If every chain has an upper bound, then there is a maximal element -/ theorem exists_maximal_of_chains_bounded (h : ∀c, chain c → ∃ub, ∀a∈c, a ≺ ub) (trans : ∀{a b c}, a ≺ b → b ≺ c → a ≺ c) : ∃m, ∀a, m ≺ a → a ≺ m := have ∃ub, ∀a∈max_chain, a ≺ ub, from h _ $ max_chain_spec.left, let ⟨ub, (hub : ∀a∈max_chain, a ≺ ub)⟩ := this in ⟨ub, assume a ha, have chain (insert a max_chain), from chain_insert max_chain_spec.left $ assume b hb _, or.inr $ trans (hub b hb) ha, have a ∈ max_chain, from classical.by_contradiction $ assume h : a ∉ max_chain, max_chain_spec.right $ ⟨insert a max_chain, this, ssubset_insert h⟩, hub a this⟩ end chain theorem zorn_partial_order {α : Type u} [partial_order α] (h : ∀c:set α, chain (≤) c → ∃ub, ∀a∈c, a ≤ ub) : ∃m:α, ∀a, m ≤ a → a = m := let ⟨m, hm⟩ := @exists_maximal_of_chains_bounded α (≤) h (assume a b c, le_trans) in ⟨m, assume a ha, le_antisymm (hm a ha) ha⟩ theorem zorn_partial_order₀ {α : Type u} [partial_order α] (s : set α) (ih : ∀ c ⊆ s, chain (≤) c → ∀ y ∈ c, ∃ ub ∈ s, ∀ z ∈ c, z ≤ ub) (x : α) (hxs : x ∈ s) : ∃ m ∈ s, x ≤ m ∧ ∀ z ∈ s, m ≤ z → z = m := let ⟨⟨m, hms, hxm⟩, h⟩ := @zorn_partial_order {m // m ∈ s ∧ x ≤ m} _ (λ c hc, classical.by_cases (assume hce : c = ∅, hce.symm ▸ ⟨⟨x, hxs, le_refl _⟩, λ _, false.elim⟩) (assume hce : c ≠ ∅, let ⟨m, hmc⟩ := set.exists_mem_of_ne_empty hce in let ⟨ub, hubs, hub⟩ := ih (subtype.val '' c) (image_subset_iff.2 $ λ z hzc, z.2.1) (by rintro _ ⟨p, hpc, rfl⟩ _ ⟨q, hqc, rfl⟩ hpq; exact hc p hpc q hqc (mt (by rintro rfl; refl) hpq)) m.1 (mem_image_of_mem _ hmc) in ⟨⟨ub, hubs, le_trans m.2.2 $ hub m.1 $ mem_image_of_mem _ hmc⟩, λ a hac, hub a.1 ⟨a, hac, rfl⟩⟩)) in ⟨m, hms, hxm, λ z hzs hmz, congr_arg subtype.val $ h ⟨z, hzs, le_trans hxm hmz⟩ hmz⟩ theorem zorn_subset {α : Type u} (S : set (set α)) (h : ∀c ⊆ S, chain (⊆) c → ∃ub ∈ S, ∀ s ∈ c, s ⊆ ub) : ∃ m ∈ S, ∀a ∈ S, m ⊆ a → a = m := begin letI : partial_order S := partial_order.lift subtype.val (λ _ _, subtype.eq') (by apply_instance), have : ∀c:set S, @chain S (≤) c → ∃ub, ∀a∈c, a ≤ ub, { intros c hc, rcases h (subtype.val '' c) (image_subset_iff.2 _) _ with ⟨s, sS, hs⟩, { exact ⟨⟨s, sS⟩, λ ⟨x, hx⟩ H, hs _ (mem_image_of_mem _ H)⟩ }, { rintro ⟨x, hx⟩ _, exact hx }, { rintro _ ⟨x, cx, rfl⟩ _ ⟨y, cy, rfl⟩ xy, exact hc x cx y cy (mt (congr_arg _) xy) } }, rcases zorn_partial_order this with ⟨⟨m, mS⟩, hm⟩, exact ⟨m, mS, λ a aS ha, congr_arg subtype.val (hm ⟨a, aS⟩ ha)⟩ end theorem zorn_subset₀ {α : Type u} (S : set (set α)) (H : ∀c ⊆ S, chain (⊆) c → c ≠ ∅ → ∃ub ∈ S, ∀ s ∈ c, s ⊆ ub) (x) (hx : x ∈ S) : ∃ m ∈ S, x ⊆ m ∧ ∀a ∈ S, m ⊆ a → a = m := begin let T := {s ∈ S | x ⊆ s}, rcases zorn_subset T _ with ⟨m, ⟨mS, mx⟩, hm⟩, { exact ⟨m, mS, mx, λ a ha ha', hm a ⟨ha, subset.trans mx ha'⟩ ha'⟩ }, { intros c cT hc, by_cases c0 : c = ∅, { rw c0, exact ⟨x, ⟨hx, subset.refl _⟩, λ _, false.elim⟩ }, { rcases H _ (subset.trans cT (sep_subset _ _)) hc c0 with ⟨ub, us, h⟩, refine ⟨ub, ⟨us, _⟩, h⟩, rcases ne_empty_iff_exists_mem.1 c0 with ⟨s, hs⟩, exact subset.trans (cT hs).2 (h _ hs) } } end theorem chain.total {α : Type u} [preorder α] {c : set α} (H : chain (≤) c) : ∀ {x y}, x ∈ c → y ∈ c → x ≤ y ∨ y ≤ x := λ x y, H.total_of_refl theorem chain.image {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) (f : α → β) (h : ∀ x y, r x y → s (f x) (f y)) {c : set α} (hrc : chain r c) : chain s (f '' c) := λ x ⟨a, ha₁, ha₂⟩ y ⟨b, hb₁, hb₂⟩, ha₂ ▸ hb₂ ▸ λ hxy, (hrc a ha₁ b hb₁ (mt (congr_arg f) $ hxy)).elim (or.inl ∘ h _ _) (or.inr ∘ h _ _) end zorn
cf8176ef7146d5cb7a6fe7ba554091ddb75cc540
94e33a31faa76775069b071adea97e86e218a8ee
/src/category_theory/structured_arrow.lean
e4b6497d49b479691d2e5db86b24087bbb3e7428
[ "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
13,410
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz, Scott Morrison -/ import category_theory.punit import category_theory.comma import category_theory.limits.shapes.terminal /-! # The category of "structured arrows" For `T : C ⥤ D`, a `T`-structured arrow with source `S : D` is just a morphism `S ⟶ T.obj Y`, for some `Y : C`. These form a category with morphisms `g : Y ⟶ Y'` making the obvious diagram commute. We prove that `𝟙 (T.obj Y)` is the initial object in `T`-structured objects with source `T.obj Y`. -/ namespace category_theory -- morphism levels before object levels. See note [category_theory universes]. universes v₁ v₂ v₃ v₄ u₁ u₂ u₃ u₄ variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] /-- The category of `T`-structured arrows with domain `S : D` (here `T : C ⥤ D`), has as its objects `D`-morphisms of the form `S ⟶ T Y`, for some `Y : C`, and morphisms `C`-morphisms `Y ⟶ Y'` making the obvious triangle commute. -/ @[derive category, nolint has_inhabited_instance] def structured_arrow (S : D) (T : C ⥤ D) := comma (functor.from_punit S) T namespace structured_arrow /-- The obvious projection functor from structured arrows. -/ @[simps] def proj (S : D) (T : C ⥤ D) : structured_arrow S T ⥤ C := comma.snd _ _ variables {S S' S'' : D} {Y Y' : C} {T : C ⥤ D} /-- Construct a structured arrow from a morphism. -/ def mk (f : S ⟶ T.obj Y) : structured_arrow S T := ⟨⟨⟨⟩⟩, Y, f⟩ @[simp] lemma mk_left (f : S ⟶ T.obj Y) : (mk f).left = ⟨⟨⟩⟩ := rfl @[simp] lemma mk_right (f : S ⟶ T.obj Y) : (mk f).right = Y := rfl @[simp] lemma mk_hom_eq_self (f : S ⟶ T.obj Y) : (mk f).hom = f := rfl @[simp, reassoc] lemma w {A B : structured_arrow S T} (f : A ⟶ B) : A.hom ≫ T.map f.right = B.hom := by { have := f.w; tidy } lemma eq_mk (f : structured_arrow S T) : f = mk f.hom := by { cases f, congr, ext, } /-- To construct a morphism of structured arrows, we need a morphism of the objects underlying the target, and to check that the triangle commutes. -/ @[simps] def hom_mk {f f' : structured_arrow S T} (g : f.right ⟶ f'.right) (w : f.hom ≫ T.map g = f'.hom) : f ⟶ f' := { left := eq_to_hom (by ext), right := g, w' := by { dsimp, simpa using w.symm, }, } /-- Given a structured arrow `X ⟶ F(U)`, and an arrow `U ⟶ Y`, we can construct a morphism of structured arrow given by `(X ⟶ F(U)) ⟶ (X ⟶ F(U) ⟶ F(Y))`. -/ def hom_mk' {F : C ⥤ D} {X : D} {Y : C} (U : structured_arrow X F) (f : U.right ⟶ Y) : U ⟶ mk (U.hom ≫ F.map f) := { right := f } /-- To construct an isomorphism of structured arrows, we need an isomorphism of the objects underlying the target, and to check that the triangle commutes. -/ @[simps] def iso_mk {f f' : structured_arrow S T} (g : f.right ≅ f'.right) (w : f.hom ≫ T.map g.hom = f'.hom) : f ≅ f' := comma.iso_mk (eq_to_iso (by ext)) g (by simpa [eq_to_hom_map] using w.symm) /-- A morphism between source objects `S ⟶ S'` contravariantly induces a functor between structured arrows, `structured_arrow S' T ⥤ structured_arrow S T`. Ideally this would be described as a 2-functor from `D` (promoted to a 2-category with equations as 2-morphisms) to `Cat`. -/ @[simps] def map (f : S ⟶ S') : structured_arrow S' T ⥤ structured_arrow S T := comma.map_left _ ((functor.const _).map f) @[simp] lemma map_mk {f : S' ⟶ T.obj Y} (g : S ⟶ S') : (map g).obj (mk f) = mk (g ≫ f) := rfl @[simp] lemma map_id {f : structured_arrow S T} : (map (𝟙 S)).obj f = f := by { rw eq_mk f, simp, } @[simp] lemma map_comp {f : S ⟶ S'} {f' : S' ⟶ S''} {h : structured_arrow S'' T} : (map (f ≫ f')).obj h = (map f).obj ((map f').obj h) := by { rw eq_mk h, simp, } instance proj_reflects_iso : reflects_isomorphisms (proj S T) := { reflects := λ Y Z f t, by exactI ⟨⟨structured_arrow.hom_mk (inv ((proj S T).map f)) (by simp), by tidy⟩⟩ } open category_theory.limits local attribute [tidy] tactic.discrete_cases /-- The identity structured arrow is initial. -/ def mk_id_initial [full T] [faithful T] : is_initial (mk (𝟙 (T.obj Y))) := { desc := λ c, hom_mk (T.preimage c.X.hom) (by { dsimp, simp, }), uniq' := λ c m _, begin ext, apply T.map_injective, simpa only [hom_mk_right, T.image_preimage, ←w m] using (category.id_comp _).symm, end } variables {A : Type u₃} [category.{v₃} A] {B : Type u₄} [category.{v₄} B] /-- The functor `(S, F ⋙ G) ⥤ (S, G)`. -/ @[simps] def pre (S : D) (F : B ⥤ C) (G : C ⥤ D) : structured_arrow S (F ⋙ G) ⥤ structured_arrow S G := comma.pre_right _ F G /-- The functor `(S, F) ⥤ (G(S), F ⋙ G)`. -/ @[simps] def post (S : C) (F : B ⥤ C) (G : C ⥤ D) : structured_arrow S F ⥤ structured_arrow (G.obj S) (F ⋙ G) := { obj := λ X, { right := X.right, hom := G.map X.hom }, map := λ X Y f, { right := f.right, w' := by { simp [functor.comp_map, ←G.map_comp, ← f.w] } } } end structured_arrow /-- The category of `S`-costructured arrows with target `T : D` (here `S : C ⥤ D`), has as its objects `D`-morphisms of the form `S Y ⟶ T`, for some `Y : C`, and morphisms `C`-morphisms `Y ⟶ Y'` making the obvious triangle commute. -/ @[derive category, nolint has_inhabited_instance] def costructured_arrow (S : C ⥤ D) (T : D) := comma S (functor.from_punit T) namespace costructured_arrow /-- The obvious projection functor from costructured arrows. -/ @[simps] def proj (S : C ⥤ D) (T : D) : costructured_arrow S T ⥤ C := comma.fst _ _ variables {T T' T'' : D} {Y Y' : C} {S : C ⥤ D} /-- Construct a costructured arrow from a morphism. -/ def mk (f : S.obj Y ⟶ T) : costructured_arrow S T := ⟨Y, ⟨⟨⟩⟩, f⟩ @[simp] lemma mk_left (f : S.obj Y ⟶ T) : (mk f).left = Y := rfl @[simp] lemma mk_right (f : S.obj Y ⟶ T) : (mk f).right = ⟨⟨⟩⟩ := rfl @[simp] lemma mk_hom_eq_self (f : S.obj Y ⟶ T) : (mk f).hom = f := rfl @[simp, reassoc] lemma w {A B : costructured_arrow S T} (f : A ⟶ B) : S.map f.left ≫ B.hom = A.hom := by tidy lemma eq_mk (f : costructured_arrow S T) : f = mk f.hom := by { cases f, congr, ext, } /-- To construct a morphism of costructured arrows, we need a morphism of the objects underlying the source, and to check that the triangle commutes. -/ @[simps] def hom_mk {f f' : costructured_arrow S T} (g : f.left ⟶ f'.left) (w : S.map g ≫ f'.hom = f.hom) : f ⟶ f' := { left := g, right := eq_to_hom (by ext), w' := by simpa [eq_to_hom_map] using w, } /-- To construct an isomorphism of costructured arrows, we need an isomorphism of the objects underlying the source, and to check that the triangle commutes. -/ @[simps] def iso_mk {f f' : costructured_arrow S T} (g : f.left ≅ f'.left) (w : S.map g.hom ≫ f'.hom = f.hom) : f ≅ f' := comma.iso_mk g (eq_to_iso (by ext)) (by simpa [eq_to_hom_map] using w) /-- A morphism between target objects `T ⟶ T'` covariantly induces a functor between costructured arrows, `costructured_arrow S T ⥤ costructured_arrow S T'`. Ideally this would be described as a 2-functor from `D` (promoted to a 2-category with equations as 2-morphisms) to `Cat`. -/ @[simps] def map (f : T ⟶ T') : costructured_arrow S T ⥤ costructured_arrow S T' := comma.map_right _ ((functor.const _).map f) @[simp] lemma map_mk {f : S.obj Y ⟶ T} (g : T ⟶ T') : (map g).obj (mk f) = mk (f ≫ g) := rfl @[simp] lemma map_id {f : costructured_arrow S T} : (map (𝟙 T)).obj f = f := by { rw eq_mk f, simp, } @[simp] lemma map_comp {f : T ⟶ T'} {f' : T' ⟶ T''} {h : costructured_arrow S T} : (map (f ≫ f')).obj h = (map f').obj ((map f).obj h) := by { rw eq_mk h, simp, } instance proj_reflects_iso : reflects_isomorphisms (proj S T) := { reflects := λ Y Z f t, by exactI ⟨⟨costructured_arrow.hom_mk (inv ((proj S T).map f)) (by simp), by tidy⟩⟩ } open category_theory.limits local attribute [tidy] tactic.discrete_cases /-- The identity costructured arrow is terminal. -/ def mk_id_terminal [full S] [faithful S] : is_terminal (mk (𝟙 (S.obj Y))) := { lift := λ c, hom_mk (S.preimage c.X.hom) (by { dsimp, simp, }), uniq' := begin rintros c m -, ext, apply S.map_injective, simpa only [hom_mk_left, S.image_preimage, ←w m] using (category.comp_id _).symm, end } variables {A : Type u₃} [category.{v₃} A] {B : Type u₄} [category.{v₄} B] /-- The functor `(F ⋙ G, S) ⥤ (G, S)`. -/ @[simps] def pre (F : B ⥤ C) (G : C ⥤ D) (S : D) : costructured_arrow (F ⋙ G) S ⥤ costructured_arrow G S := comma.pre_left F G _ /-- The functor `(F, S) ⥤ (F ⋙ G, G(S))`. -/ @[simps] def post (F : B ⥤ C) (G : C ⥤ D) (S : C) : costructured_arrow F S ⥤ costructured_arrow (F ⋙ G) (G.obj S) := { obj := λ X, { left := X.left, hom := G.map X.hom }, map := λ X Y f, { left := f.left, w' := by { simp [functor.comp_map, ←G.map_comp, ← f.w] } } } end costructured_arrow open opposite namespace structured_arrow /-- For a functor `F : C ⥤ D` and an object `d : D`, we obtain a contravariant functor from the category of structured arrows `d ⟶ F.obj c` to the category of costructured arrows `F.op.obj c ⟶ (op d)`. -/ @[simps] def to_costructured_arrow (F : C ⥤ D) (d : D) : (structured_arrow d F)ᵒᵖ ⥤ costructured_arrow F.op (op d) := { obj := λ X, @costructured_arrow.mk _ _ _ _ _ (op X.unop.right) F.op X.unop.hom.op, map := λ X Y f, costructured_arrow.hom_mk (f.unop.right.op) begin dsimp, rw [← op_comp, ← f.unop.w, functor.const.obj_map], erw category.id_comp, end } /-- For a functor `F : C ⥤ D` and an object `d : D`, we obtain a contravariant functor from the category of structured arrows `op d ⟶ F.op.obj c` to the category of costructured arrows `F.obj c ⟶ d`. -/ @[simps] def to_costructured_arrow' (F : C ⥤ D) (d : D) : (structured_arrow (op d) F.op)ᵒᵖ ⥤ costructured_arrow F d := { obj := λ X, @costructured_arrow.mk _ _ _ _ _ (unop X.unop.right) F X.unop.hom.unop, map := λ X Y f, costructured_arrow.hom_mk f.unop.right.unop begin dsimp, rw [← quiver.hom.unop_op (F.map (quiver.hom.unop f.unop.right)), ← unop_comp, ← F.op_map, ← f.unop.w, functor.const.obj_map], erw category.id_comp, end } end structured_arrow namespace costructured_arrow /-- For a functor `F : C ⥤ D` and an object `d : D`, we obtain a contravariant functor from the category of costructured arrows `F.obj c ⟶ d` to the category of structured arrows `op d ⟶ F.op.obj c`. -/ @[simps] def to_structured_arrow (F : C ⥤ D) (d : D) : (costructured_arrow F d)ᵒᵖ ⥤ structured_arrow (op d) F.op := { obj := λ X, @structured_arrow.mk _ _ _ _ _ (op X.unop.left) F.op X.unop.hom.op, map := λ X Y f, structured_arrow.hom_mk f.unop.left.op begin dsimp, rw [← op_comp, f.unop.w, functor.const.obj_map], erw category.comp_id, end } /-- For a functor `F : C ⥤ D` and an object `d : D`, we obtain a contravariant functor from the category of costructured arrows `F.op.obj c ⟶ op d` to the category of structured arrows `d ⟶ F.obj c`. -/ @[simps] def to_structured_arrow' (F : C ⥤ D) (d : D) : (costructured_arrow F.op (op d))ᵒᵖ ⥤ structured_arrow d F := { obj := λ X, @structured_arrow.mk _ _ _ _ _ (unop X.unop.left) F X.unop.hom.unop, map := λ X Y f, structured_arrow.hom_mk (f.unop.left.unop) begin dsimp, rw [← quiver.hom.unop_op (F.map f.unop.left.unop), ← unop_comp, ← F.op_map, f.unop.w, functor.const.obj_map], erw category.comp_id, end } end costructured_arrow /-- For a functor `F : C ⥤ D` and an object `d : D`, the category of structured arrows `d ⟶ F.obj c` is contravariantly equivalent to the category of costructured arrows `F.op.obj c ⟶ op d`. -/ def structured_arrow_op_equivalence (F : C ⥤ D) (d : D) : (structured_arrow d F)ᵒᵖ ≌ costructured_arrow F.op (op d) := equivalence.mk (structured_arrow.to_costructured_arrow F d) (costructured_arrow.to_structured_arrow' F d).right_op (nat_iso.of_components (λ X, (@structured_arrow.iso_mk _ _ _ _ _ _ (structured_arrow.mk (unop X).hom) (unop X) (iso.refl _) (by tidy)).op) (λ X Y f, quiver.hom.unop_inj $ begin ext, dsimp, simp end)) (nat_iso.of_components (λ X, @costructured_arrow.iso_mk _ _ _ _ _ _ (costructured_arrow.mk X.hom) X (iso.refl _) (by tidy)) (λ X Y f, begin ext, dsimp, simp end)) /-- For a functor `F : C ⥤ D` and an object `d : D`, the category of costructured arrows `F.obj c ⟶ d` is contravariantly equivalent to the category of structured arrows `op d ⟶ F.op.obj c`. -/ def costructured_arrow_op_equivalence (F : C ⥤ D) (d : D) : (costructured_arrow F d)ᵒᵖ ≌ structured_arrow (op d) F.op := equivalence.mk (costructured_arrow.to_structured_arrow F d) (structured_arrow.to_costructured_arrow' F d).right_op (nat_iso.of_components (λ X, (@costructured_arrow.iso_mk _ _ _ _ _ _ (costructured_arrow.mk (unop X).hom) (unop X) (iso.refl _) (by tidy)).op) (λ X Y f, quiver.hom.unop_inj $ begin ext, dsimp, simp end)) (nat_iso.of_components (λ X, @structured_arrow.iso_mk _ _ _ _ _ _ (structured_arrow.mk X.hom) X (iso.refl _) (by tidy)) (λ X Y f, begin ext, dsimp, simp end)) end category_theory
722ab88fa2082a1a0f0b711749de7f67da3cc340
82e44445c70db0f03e30d7be725775f122d72f3e
/src/analysis/special_functions/exp_log.lean
7a4e95bf1e5de75744b477669777dc30e71be6fd
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
34,879
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne -/ import analysis.calculus.inverse import analysis.complex.real_deriv import data.complex.exponential /-! # Complex and real exponential, real logarithm ## Main statements This file establishes the basic analytical properties of the complex and real exponential functions (continuity, differentiability, computation of the derivative). It also contains the definition of the real logarithm function (as the inverse of the exponential on `(0, +∞)`, extended to `ℝ` by setting `log (-x) = log x`) and its basic properties (continuity, differentiability, formula for the derivative). The complex logarithm is *not* defined in this file as it relies on trigonometric functions. See instead `trigonometric.lean`. ## Tags exp, log -/ noncomputable theory open finset filter metric asymptotics set function open_locale classical topological_space namespace complex /-- The complex exponential is everywhere differentiable, with the derivative `exp x`. -/ lemma has_deriv_at_exp (x : ℂ) : has_deriv_at exp (exp x) x := begin rw has_deriv_at_iff_is_o_nhds_zero, have : (1 : ℕ) < 2 := by norm_num, refine (is_O.of_bound (∥exp x∥) _).trans_is_o (is_o_pow_id this), filter_upwards [metric.ball_mem_nhds (0 : ℂ) zero_lt_one], simp only [metric.mem_ball, dist_zero_right, normed_field.norm_pow], intros z hz, calc ∥exp (x + z) - exp x - z * exp x∥ = ∥exp x * (exp z - 1 - z)∥ : by { congr, rw [exp_add], ring } ... = ∥exp x∥ * ∥exp z - 1 - z∥ : normed_field.norm_mul _ _ ... ≤ ∥exp x∥ * ∥z∥^2 : mul_le_mul_of_nonneg_left (abs_exp_sub_one_sub_id_le (le_of_lt hz)) (norm_nonneg _) end lemma differentiable_exp : differentiable ℂ exp := λx, (has_deriv_at_exp x).differentiable_at lemma differentiable_at_exp {x : ℂ} : differentiable_at ℂ exp x := differentiable_exp x @[simp] lemma deriv_exp : deriv exp = exp := funext $ λ x, (has_deriv_at_exp x).deriv @[simp] lemma iter_deriv_exp : ∀ n : ℕ, (deriv^[n] exp) = exp | 0 := rfl | (n+1) := by rw [iterate_succ_apply, deriv_exp, iter_deriv_exp n] @[continuity] lemma continuous_exp : continuous exp := differentiable_exp.continuous lemma continuous_on_exp {s : set ℂ} : continuous_on exp s := continuous_exp.continuous_on lemma times_cont_diff_exp : ∀ {n}, times_cont_diff ℂ n exp := begin refine times_cont_diff_all_iff_nat.2 (λ n, _), induction n with n ihn, { exact times_cont_diff_zero.2 continuous_exp }, { rw times_cont_diff_succ_iff_deriv, use differentiable_exp, rwa deriv_exp } end lemma has_strict_deriv_at_exp (x : ℂ) : has_strict_deriv_at exp (exp x) x := times_cont_diff_exp.times_cont_diff_at.has_strict_deriv_at' (has_deriv_at_exp x) le_rfl lemma is_open_map_exp : is_open_map exp := open_map_of_strict_deriv has_strict_deriv_at_exp exp_ne_zero end complex section variables {f : ℂ → ℂ} {f' x : ℂ} {s : set ℂ} lemma has_strict_deriv_at.cexp (hf : has_strict_deriv_at f f' x) : has_strict_deriv_at (λ x, complex.exp (f x)) (complex.exp (f x) * f') x := (complex.has_strict_deriv_at_exp (f x)).comp x hf lemma has_deriv_at.cexp (hf : has_deriv_at f f' x) : has_deriv_at (λ x, complex.exp (f x)) (complex.exp (f x) * f') x := (complex.has_deriv_at_exp (f x)).comp x hf lemma has_deriv_within_at.cexp (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, complex.exp (f x)) (complex.exp (f x) * f') s x := (complex.has_deriv_at_exp (f x)).comp_has_deriv_within_at x hf lemma deriv_within_cexp (hf : differentiable_within_at ℂ f s x) (hxs : unique_diff_within_at ℂ s x) : deriv_within (λx, complex.exp (f x)) s x = complex.exp (f x) * (deriv_within f s x) := hf.has_deriv_within_at.cexp.deriv_within hxs @[simp] lemma deriv_cexp (hc : differentiable_at ℂ f x) : deriv (λx, complex.exp (f x)) x = complex.exp (f x) * (deriv f x) := hc.has_deriv_at.cexp.deriv end section variables {E : Type*} [normed_group E] [normed_space ℂ E] {f : E → ℂ} {f' : E →L[ℂ] ℂ} {x : E} {s : set E} lemma has_strict_fderiv_at.cexp (hf : has_strict_fderiv_at f f' x) : has_strict_fderiv_at (λ x, complex.exp (f x)) (complex.exp (f x) • f') x := (complex.has_strict_deriv_at_exp (f x)).comp_has_strict_fderiv_at x hf lemma has_fderiv_within_at.cexp (hf : has_fderiv_within_at f f' s x) : has_fderiv_within_at (λ x, complex.exp (f x)) (complex.exp (f x) • f') s x := (complex.has_deriv_at_exp (f x)).comp_has_fderiv_within_at x hf lemma has_fderiv_at.cexp (hf : has_fderiv_at f f' x) : has_fderiv_at (λ x, complex.exp (f x)) (complex.exp (f x) • f') x := has_fderiv_within_at_univ.1 $ hf.has_fderiv_within_at.cexp lemma differentiable_within_at.cexp (hf : differentiable_within_at ℂ f s x) : differentiable_within_at ℂ (λ x, complex.exp (f x)) s x := hf.has_fderiv_within_at.cexp.differentiable_within_at @[simp] lemma differentiable_at.cexp (hc : differentiable_at ℂ f x) : differentiable_at ℂ (λx, complex.exp (f x)) x := hc.has_fderiv_at.cexp.differentiable_at lemma differentiable_on.cexp (hc : differentiable_on ℂ f s) : differentiable_on ℂ (λx, complex.exp (f x)) s := λx h, (hc x h).cexp @[simp] lemma differentiable.cexp (hc : differentiable ℂ f) : differentiable ℂ (λx, complex.exp (f x)) := λx, (hc x).cexp lemma times_cont_diff.cexp {n} (h : times_cont_diff ℂ n f) : times_cont_diff ℂ n (λ x, complex.exp (f x)) := complex.times_cont_diff_exp.comp h lemma times_cont_diff_at.cexp {n} (hf : times_cont_diff_at ℂ n f x) : times_cont_diff_at ℂ n (λ x, complex.exp (f x)) x := complex.times_cont_diff_exp.times_cont_diff_at.comp x hf lemma times_cont_diff_on.cexp {n} (hf : times_cont_diff_on ℂ n f s) : times_cont_diff_on ℂ n (λ x, complex.exp (f x)) s := complex.times_cont_diff_exp.comp_times_cont_diff_on hf lemma times_cont_diff_within_at.cexp {n} (hf : times_cont_diff_within_at ℂ n f s x) : times_cont_diff_within_at ℂ n (λ x, complex.exp (f x)) s x := complex.times_cont_diff_exp.times_cont_diff_at.comp_times_cont_diff_within_at x hf end namespace real variables {x y z : ℝ} lemma has_strict_deriv_at_exp (x : ℝ) : has_strict_deriv_at exp (exp x) x := (complex.has_strict_deriv_at_exp x).real_of_complex lemma has_deriv_at_exp (x : ℝ) : has_deriv_at exp (exp x) x := (complex.has_deriv_at_exp x).real_of_complex lemma times_cont_diff_exp {n} : times_cont_diff ℝ n exp := complex.times_cont_diff_exp.real_of_complex lemma differentiable_exp : differentiable ℝ exp := λx, (has_deriv_at_exp x).differentiable_at lemma differentiable_at_exp : differentiable_at ℝ exp x := differentiable_exp x @[simp] lemma deriv_exp : deriv exp = exp := funext $ λ x, (has_deriv_at_exp x).deriv @[simp] lemma iter_deriv_exp : ∀ n : ℕ, (deriv^[n] exp) = exp | 0 := rfl | (n+1) := by rw [iterate_succ_apply, deriv_exp, iter_deriv_exp n] @[continuity] lemma continuous_exp : continuous exp := differentiable_exp.continuous lemma continuous_on_exp {s : set ℝ} : continuous_on exp s := continuous_exp.continuous_on end real section /-! Register lemmas for the derivatives of the composition of `real.exp` with a differentiable function, for standalone use and use with `simp`. -/ variables {f : ℝ → ℝ} {f' x : ℝ} {s : set ℝ} lemma has_strict_deriv_at.exp (hf : has_strict_deriv_at f f' x) : has_strict_deriv_at (λ x, real.exp (f x)) (real.exp (f x) * f') x := (real.has_strict_deriv_at_exp (f x)).comp x hf lemma has_deriv_at.exp (hf : has_deriv_at f f' x) : has_deriv_at (λ x, real.exp (f x)) (real.exp (f x) * f') x := (real.has_deriv_at_exp (f x)).comp x hf lemma has_deriv_within_at.exp (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, real.exp (f x)) (real.exp (f x) * f') s x := (real.has_deriv_at_exp (f x)).comp_has_deriv_within_at x hf lemma deriv_within_exp (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, real.exp (f x)) s x = real.exp (f x) * (deriv_within f s x) := hf.has_deriv_within_at.exp.deriv_within hxs @[simp] lemma deriv_exp (hc : differentiable_at ℝ f x) : deriv (λx, real.exp (f x)) x = real.exp (f x) * (deriv f x) := hc.has_deriv_at.exp.deriv end section /-! Register lemmas for the derivatives of the composition of `real.exp` with a differentiable function, for standalone use and use with `simp`. -/ variables {E : Type*} [normed_group E] [normed_space ℝ E] {f : E → ℝ} {f' : E →L[ℝ] ℝ} {x : E} {s : set E} lemma times_cont_diff.exp {n} (hf : times_cont_diff ℝ n f) : times_cont_diff ℝ n (λ x, real.exp (f x)) := real.times_cont_diff_exp.comp hf lemma times_cont_diff_at.exp {n} (hf : times_cont_diff_at ℝ n f x) : times_cont_diff_at ℝ n (λ x, real.exp (f x)) x := real.times_cont_diff_exp.times_cont_diff_at.comp x hf lemma times_cont_diff_on.exp {n} (hf : times_cont_diff_on ℝ n f s) : times_cont_diff_on ℝ n (λ x, real.exp (f x)) s := real.times_cont_diff_exp.comp_times_cont_diff_on hf lemma times_cont_diff_within_at.exp {n} (hf : times_cont_diff_within_at ℝ n f s x) : times_cont_diff_within_at ℝ n (λ x, real.exp (f x)) s x := real.times_cont_diff_exp.times_cont_diff_at.comp_times_cont_diff_within_at x hf lemma has_fderiv_within_at.exp (hf : has_fderiv_within_at f f' s x) : has_fderiv_within_at (λ x, real.exp (f x)) (real.exp (f x) • f') s x := (real.has_deriv_at_exp (f x)).comp_has_fderiv_within_at x hf lemma has_fderiv_at.exp (hf : has_fderiv_at f f' x) : has_fderiv_at (λ x, real.exp (f x)) (real.exp (f x) • f') x := (real.has_deriv_at_exp (f x)).comp_has_fderiv_at x hf lemma has_strict_fderiv_at.exp (hf : has_strict_fderiv_at f f' x) : has_strict_fderiv_at (λ x, real.exp (f x)) (real.exp (f x) • f') x := (real.has_strict_deriv_at_exp (f x)).comp_has_strict_fderiv_at x hf lemma differentiable_within_at.exp (hf : differentiable_within_at ℝ f s x) : differentiable_within_at ℝ (λ x, real.exp (f x)) s x := hf.has_fderiv_within_at.exp.differentiable_within_at @[simp] lemma differentiable_at.exp (hc : differentiable_at ℝ f x) : differentiable_at ℝ (λx, real.exp (f x)) x := hc.has_fderiv_at.exp.differentiable_at lemma differentiable_on.exp (hc : differentiable_on ℝ f s) : differentiable_on ℝ (λx, real.exp (f x)) s := λ x h, (hc x h).exp @[simp] lemma differentiable.exp (hc : differentiable ℝ f) : differentiable ℝ (λx, real.exp (f x)) := λ x, (hc x).exp lemma fderiv_within_exp (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : fderiv_within ℝ (λx, real.exp (f x)) s x = real.exp (f x) • (fderiv_within ℝ f s x) := hf.has_fderiv_within_at.exp.fderiv_within hxs @[simp] lemma fderiv_exp (hc : differentiable_at ℝ f x) : fderiv ℝ (λx, real.exp (f x)) x = real.exp (f x) • (fderiv ℝ f x) := hc.has_fderiv_at.exp.fderiv end namespace real variables {x y z : ℝ} /-- The real exponential function tends to `+∞` at `+∞`. -/ lemma tendsto_exp_at_top : tendsto exp at_top at_top := begin have A : tendsto (λx:ℝ, x + 1) at_top at_top := tendsto_at_top_add_const_right at_top 1 tendsto_id, have B : ∀ᶠ x in at_top, x + 1 ≤ exp x := eventually_at_top.2 ⟨0, λx hx, add_one_le_exp_of_nonneg hx⟩, exact tendsto_at_top_mono' at_top B A end /-- The real exponential function tends to `0` at `-∞` or, equivalently, `exp(-x)` tends to `0` at `+∞` -/ lemma tendsto_exp_neg_at_top_nhds_0 : tendsto (λx, exp (-x)) at_top (𝓝 0) := (tendsto_inv_at_top_zero.comp tendsto_exp_at_top).congr (λx, (exp_neg x).symm) /-- The real exponential function tends to `1` at `0`. -/ lemma tendsto_exp_nhds_0_nhds_1 : tendsto exp (𝓝 0) (𝓝 1) := by { convert continuous_exp.tendsto 0, simp } lemma tendsto_exp_at_bot : tendsto exp at_bot (𝓝 0) := (tendsto_exp_neg_at_top_nhds_0.comp tendsto_neg_at_bot_at_top).congr $ λ x, congr_arg exp $ neg_neg x lemma tendsto_exp_at_bot_nhds_within : tendsto exp at_bot (𝓝[Ioi 0] 0) := tendsto_inf.2 ⟨tendsto_exp_at_bot, tendsto_principal.2 $ eventually_of_forall exp_pos⟩ /-- `real.exp` as an order isomorphism between `ℝ` and `(0, +∞)`. -/ def exp_order_iso : ℝ ≃o Ioi (0 : ℝ) := strict_mono.order_iso_of_surjective _ (exp_strict_mono.cod_restrict exp_pos) $ (continuous_subtype_mk _ continuous_exp).surjective (by simp only [tendsto_Ioi_at_top, subtype.coe_mk, tendsto_exp_at_top]) (by simp [tendsto_exp_at_bot_nhds_within]) @[simp] lemma coe_exp_order_iso_apply (x : ℝ) : (exp_order_iso x : ℝ) = exp x := rfl @[simp] lemma coe_comp_exp_order_iso : coe ∘ exp_order_iso = exp := rfl @[simp] lemma range_exp : range exp = Ioi 0 := by rw [← coe_comp_exp_order_iso, range_comp, exp_order_iso.range_eq, image_univ, subtype.range_coe] @[simp] lemma map_exp_at_top : map exp at_top = at_top := by rw [← coe_comp_exp_order_iso, ← filter.map_map, order_iso.map_at_top, map_coe_Ioi_at_top] @[simp] lemma comap_exp_at_top : comap exp at_top = at_top := by rw [← map_exp_at_top, comap_map exp_injective, map_exp_at_top] @[simp] lemma tendsto_exp_comp_at_top {α : Type*} {l : filter α} {f : α → ℝ} : tendsto (λ x, exp (f x)) l at_top ↔ tendsto f l at_top := by rw [← tendsto_comap_iff, comap_exp_at_top] lemma tendsto_comp_exp_at_top {α : Type*} {l : filter α} {f : ℝ → α} : tendsto (λ x, f (exp x)) at_top l ↔ tendsto f at_top l := by rw [← tendsto_map'_iff, map_exp_at_top] @[simp] lemma map_exp_at_bot : map exp at_bot = 𝓝[Ioi 0] 0 := by rw [← coe_comp_exp_order_iso, ← filter.map_map, exp_order_iso.map_at_bot, ← map_coe_Ioi_at_bot] lemma comap_exp_nhds_within_Ioi_zero : comap exp (𝓝[Ioi 0] 0) = at_bot := by rw [← map_exp_at_bot, comap_map exp_injective] lemma tendsto_comp_exp_at_bot {α : Type*} {l : filter α} {f : ℝ → α} : tendsto (λ x, f (exp x)) at_bot l ↔ tendsto f (𝓝[Ioi 0] 0) l := by rw [← map_exp_at_bot, tendsto_map'_iff] /-- The real logarithm function, equal to the inverse of the exponential for `x > 0`, to `log |x|` for `x < 0`, and to `0` for `0`. We use this unconventional extension to `(-∞, 0]` as it gives the formula `log (x * y) = log x + log y` for all nonzero `x` and `y`, and the derivative of `log` is `1/x` away from `0`. -/ @[pp_nodot] noncomputable def log (x : ℝ) : ℝ := if hx : x = 0 then 0 else exp_order_iso.symm ⟨abs x, abs_pos.2 hx⟩ lemma log_of_ne_zero (hx : x ≠ 0) : log x = exp_order_iso.symm ⟨abs x, abs_pos.2 hx⟩ := dif_neg hx lemma log_of_pos (hx : 0 < x) : log x = exp_order_iso.symm ⟨x, hx⟩ := by { rw [log_of_ne_zero hx.ne'], congr, exact abs_of_pos hx } lemma exp_log_eq_abs (hx : x ≠ 0) : exp (log x) = abs x := by rw [log_of_ne_zero hx, ← coe_exp_order_iso_apply, order_iso.apply_symm_apply, subtype.coe_mk] lemma exp_log (hx : 0 < x) : exp (log x) = x := by { rw exp_log_eq_abs hx.ne', exact abs_of_pos hx } lemma exp_log_of_neg (hx : x < 0) : exp (log x) = -x := by { rw exp_log_eq_abs (ne_of_lt hx), exact abs_of_neg hx } @[simp] lemma log_exp (x : ℝ) : log (exp x) = x := exp_injective $ exp_log (exp_pos x) lemma surj_on_log : surj_on log (Ioi 0) univ := λ x _, ⟨exp x, exp_pos x, log_exp x⟩ lemma log_surjective : surjective log := λ x, ⟨exp x, log_exp x⟩ @[simp] lemma range_log : range log = univ := log_surjective.range_eq @[simp] lemma log_zero : log 0 = 0 := dif_pos rfl @[simp] lemma log_one : log 1 = 0 := exp_injective $ by rw [exp_log zero_lt_one, exp_zero] @[simp] lemma log_abs (x : ℝ) : log (abs x) = log x := begin by_cases h : x = 0, { simp [h] }, { rw [← exp_eq_exp, exp_log_eq_abs h, exp_log_eq_abs (abs_pos.2 h).ne', abs_abs] } end @[simp] lemma log_neg_eq_log (x : ℝ) : log (-x) = log x := by rw [← log_abs x, ← log_abs (-x), abs_neg] lemma surj_on_log' : surj_on log (Iio 0) univ := λ x _, ⟨-exp x, neg_lt_zero.2 $ exp_pos x, by rw [log_neg_eq_log, log_exp]⟩ lemma log_mul (hx : x ≠ 0) (hy : y ≠ 0) : log (x * y) = log x + log y := exp_injective $ by rw [exp_log_eq_abs (mul_ne_zero hx hy), exp_add, exp_log_eq_abs hx, exp_log_eq_abs hy, abs_mul] lemma log_div (hx : x ≠ 0) (hy : y ≠ 0) : log (x / y) = log x - log y := exp_injective $ by rw [exp_log_eq_abs (div_ne_zero hx hy), exp_sub, exp_log_eq_abs hx, exp_log_eq_abs hy, abs_div] @[simp] lemma log_inv (x : ℝ) : log (x⁻¹) = -log x := begin by_cases hx : x = 0, { simp [hx] }, rw [← exp_eq_exp, exp_log_eq_abs (inv_ne_zero hx), exp_neg, exp_log_eq_abs hx, abs_inv] end lemma log_le_log (h : 0 < x) (h₁ : 0 < y) : real.log x ≤ real.log y ↔ x ≤ y := by rw [← exp_le_exp, exp_log h, exp_log h₁] lemma log_lt_log (hx : 0 < x) : x < y → log x < log y := by { intro h, rwa [← exp_lt_exp, exp_log hx, exp_log (lt_trans hx h)] } lemma log_lt_log_iff (hx : 0 < x) (hy : 0 < y) : log x < log y ↔ x < y := by { rw [← exp_lt_exp, exp_log hx, exp_log hy] } lemma log_pos_iff (hx : 0 < x) : 0 < log x ↔ 1 < x := by { rw ← log_one, exact log_lt_log_iff zero_lt_one hx } lemma log_pos (hx : 1 < x) : 0 < log x := (log_pos_iff (lt_trans zero_lt_one hx)).2 hx lemma log_neg_iff (h : 0 < x) : log x < 0 ↔ x < 1 := by { rw ← log_one, exact log_lt_log_iff h zero_lt_one } lemma log_neg (h0 : 0 < x) (h1 : x < 1) : log x < 0 := (log_neg_iff h0).2 h1 lemma log_nonneg_iff (hx : 0 < x) : 0 ≤ log x ↔ 1 ≤ x := by rw [← not_lt, log_neg_iff hx, not_lt] lemma log_nonneg (hx : 1 ≤ x) : 0 ≤ log x := (log_nonneg_iff (zero_lt_one.trans_le hx)).2 hx lemma log_nonpos_iff (hx : 0 < x) : log x ≤ 0 ↔ x ≤ 1 := by rw [← not_lt, log_pos_iff hx, not_lt] lemma log_nonpos_iff' (hx : 0 ≤ x) : log x ≤ 0 ↔ x ≤ 1 := begin rcases hx.eq_or_lt with (rfl|hx), { simp [le_refl, zero_le_one] }, exact log_nonpos_iff hx end lemma log_nonpos (hx : 0 ≤ x) (h'x : x ≤ 1) : log x ≤ 0 := (log_nonpos_iff' hx).2 h'x lemma strict_mono_incr_on_log : strict_mono_incr_on log (set.Ioi 0) := λ x hx y hy hxy, log_lt_log hx hxy lemma strict_mono_decr_on_log : strict_mono_decr_on log (set.Iio 0) := begin rintros x (hx : x < 0) y (hy : y < 0) hxy, rw [← log_abs y, ← log_abs x], refine log_lt_log (abs_pos.2 hy.ne) _, rwa [abs_of_neg hy, abs_of_neg hx, neg_lt_neg_iff] end lemma log_inj_on_pos : set.inj_on log (set.Ioi 0) := strict_mono_incr_on_log.inj_on lemma eq_one_of_pos_of_log_eq_zero {x : ℝ} (h₁ : 0 < x) (h₂ : log x = 0) : x = 1 := log_inj_on_pos (set.mem_Ioi.2 h₁) (set.mem_Ioi.2 zero_lt_one) (h₂.trans real.log_one.symm) lemma log_ne_zero_of_pos_of_ne_one {x : ℝ} (hx_pos : 0 < x) (hx : x ≠ 1) : log x ≠ 0 := mt (eq_one_of_pos_of_log_eq_zero hx_pos) hx /-- The real logarithm function tends to `+∞` at `+∞`. -/ lemma tendsto_log_at_top : tendsto log at_top at_top := tendsto_comp_exp_at_top.1 $ by simpa only [log_exp] using tendsto_id lemma tendsto_log_nhds_within_zero : tendsto log (𝓝[{0}ᶜ] 0) at_bot := begin rw [← (show _ = log, from funext log_abs)], refine tendsto.comp _ tendsto_abs_nhds_within_zero, simpa [← tendsto_comp_exp_at_bot] using tendsto_id end lemma continuous_on_log : continuous_on log {0}ᶜ := begin rw [continuous_on_iff_continuous_restrict, restrict], conv in (log _) { rw [log_of_ne_zero (show (x : ℝ) ≠ 0, from x.2)] }, exact exp_order_iso.symm.continuous.comp (continuous_subtype_mk _ continuous_subtype_coe.norm) end @[continuity] lemma continuous_log : continuous (λ x : {x : ℝ // x ≠ 0}, log x) := continuous_on_iff_continuous_restrict.1 $ continuous_on_log.mono $ λ x hx, hx @[continuity] lemma continuous_log' : continuous (λ x : {x : ℝ // 0 < x}, log x) := continuous_on_iff_continuous_restrict.1 $ continuous_on_log.mono $ λ x hx, ne_of_gt hx lemma continuous_at_log (hx : x ≠ 0) : continuous_at log x := (continuous_on_log x hx).continuous_at $ is_open.mem_nhds is_open_compl_singleton hx @[simp] lemma continuous_at_log_iff : continuous_at log x ↔ x ≠ 0 := begin refine ⟨_, continuous_at_log⟩, rintros h rfl, exact not_tendsto_nhds_of_tendsto_at_bot tendsto_log_nhds_within_zero _ (h.tendsto.mono_left inf_le_left) end lemma has_strict_deriv_at_log_of_pos (hx : 0 < x) : has_strict_deriv_at log x⁻¹ x := have has_strict_deriv_at log (exp $ log x)⁻¹ x, from (has_strict_deriv_at_exp $ log x).of_local_left_inverse (continuous_at_log hx.ne') (ne_of_gt $ exp_pos _) $ eventually.mono (lt_mem_nhds hx) @exp_log, by rwa [exp_log hx] at this lemma has_strict_deriv_at_log (hx : x ≠ 0) : has_strict_deriv_at log x⁻¹ x := begin cases hx.lt_or_lt with hx hx, { convert (has_strict_deriv_at_log_of_pos (neg_pos.mpr hx)).comp x (has_strict_deriv_at_neg x), { ext y, exact (log_neg_eq_log y).symm }, { field_simp [hx.ne] } }, { exact has_strict_deriv_at_log_of_pos hx } end lemma has_deriv_at_log (hx : x ≠ 0) : has_deriv_at log x⁻¹ x := (has_strict_deriv_at_log hx).has_deriv_at lemma differentiable_at_log (hx : x ≠ 0) : differentiable_at ℝ log x := (has_deriv_at_log hx).differentiable_at lemma differentiable_on_log : differentiable_on ℝ log {0}ᶜ := λ x hx, (differentiable_at_log hx).differentiable_within_at @[simp] lemma differentiable_at_log_iff : differentiable_at ℝ log x ↔ x ≠ 0 := ⟨λ h, continuous_at_log_iff.1 h.continuous_at, differentiable_at_log⟩ lemma deriv_log (x : ℝ) : deriv log x = x⁻¹ := if hx : x = 0 then by rw [deriv_zero_of_not_differentiable_at (mt differentiable_at_log_iff.1 (not_not.2 hx)), hx, inv_zero] else (has_deriv_at_log hx).deriv @[simp] lemma deriv_log' : deriv log = has_inv.inv := funext deriv_log lemma times_cont_diff_on_log {n : with_top ℕ} : times_cont_diff_on ℝ n log {0}ᶜ := begin suffices : times_cont_diff_on ℝ ⊤ log {0}ᶜ, from this.of_le le_top, refine (times_cont_diff_on_top_iff_deriv_of_open is_open_compl_singleton).2 _, simp [differentiable_on_log, times_cont_diff_on_inv] end lemma times_cont_diff_at_log {n : with_top ℕ} : times_cont_diff_at ℝ n log x ↔ x ≠ 0 := ⟨λ h, continuous_at_log_iff.1 h.continuous_at, λ hx, (times_cont_diff_on_log x hx).times_cont_diff_at $ is_open.mem_nhds is_open_compl_singleton hx⟩ end real section log_differentiable open real section continuity variables {α : Type*} lemma filter.tendsto.log {f : α → ℝ} {l : filter α} {x : ℝ} (h : tendsto f l (𝓝 x)) (hx : x ≠ 0) : tendsto (λ x, log (f x)) l (𝓝 (log x)) := (continuous_at_log hx).tendsto.comp h variables [topological_space α] {f : α → ℝ} {s : set α} {a : α} lemma continuous.log (hf : continuous f) (h₀ : ∀ x, f x ≠ 0) : continuous (λ x, log (f x)) := continuous_on_log.comp_continuous hf h₀ lemma continuous_at.log (hf : continuous_at f a) (h₀ : f a ≠ 0) : continuous_at (λ x, log (f x)) a := hf.log h₀ lemma continuous_within_at.log (hf : continuous_within_at f s a) (h₀ : f a ≠ 0) : continuous_within_at (λ x, log (f x)) s a := hf.log h₀ lemma continuous_on.log (hf : continuous_on f s) (h₀ : ∀ x ∈ s, f x ≠ 0) : continuous_on (λ x, log (f x)) s := λ x hx, (hf x hx).log (h₀ x hx) end continuity section deriv variables {f : ℝ → ℝ} {x f' : ℝ} {s : set ℝ} lemma has_deriv_within_at.log (hf : has_deriv_within_at f f' s x) (hx : f x ≠ 0) : has_deriv_within_at (λ y, log (f y)) (f' / (f x)) s x := begin rw div_eq_inv_mul, exact (has_deriv_at_log hx).comp_has_deriv_within_at x hf end lemma has_deriv_at.log (hf : has_deriv_at f f' x) (hx : f x ≠ 0) : has_deriv_at (λ y, log (f y)) (f' / f x) x := begin rw ← has_deriv_within_at_univ at *, exact hf.log hx end lemma has_strict_deriv_at.log (hf : has_strict_deriv_at f f' x) (hx : f x ≠ 0) : has_strict_deriv_at (λ y, log (f y)) (f' / f x) x := begin rw div_eq_inv_mul, exact (has_strict_deriv_at_log hx).comp x hf end lemma deriv_within.log (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, log (f x)) s x = (deriv_within f s x) / (f x) := (hf.has_deriv_within_at.log hx).deriv_within hxs @[simp] lemma deriv.log (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : deriv (λx, log (f x)) x = (deriv f x) / (f x) := (hf.has_deriv_at.log hx).deriv end deriv section fderiv variables {E : Type*} [normed_group E] [normed_space ℝ E] {f : E → ℝ} {x : E} {f' : E →L[ℝ] ℝ} {s : set E} lemma has_fderiv_within_at.log (hf : has_fderiv_within_at f f' s x) (hx : f x ≠ 0) : has_fderiv_within_at (λ x, log (f x)) ((f x)⁻¹ • f') s x := (has_deriv_at_log hx).comp_has_fderiv_within_at x hf lemma has_fderiv_at.log (hf : has_fderiv_at f f' x) (hx : f x ≠ 0) : has_fderiv_at (λ x, log (f x)) ((f x)⁻¹ • f') x := (has_deriv_at_log hx).comp_has_fderiv_at x hf lemma has_strict_fderiv_at.log (hf : has_strict_fderiv_at f f' x) (hx : f x ≠ 0) : has_strict_fderiv_at (λ x, log (f x)) ((f x)⁻¹ • f') x := (has_strict_deriv_at_log hx).comp_has_strict_fderiv_at x hf lemma differentiable_within_at.log (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) : differentiable_within_at ℝ (λx, log (f x)) s x := (hf.has_fderiv_within_at.log hx).differentiable_within_at @[simp] lemma differentiable_at.log (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : differentiable_at ℝ (λx, log (f x)) x := (hf.has_fderiv_at.log hx).differentiable_at lemma times_cont_diff_at.log {n} (hf : times_cont_diff_at ℝ n f x) (hx : f x ≠ 0) : times_cont_diff_at ℝ n (λ x, log (f x)) x := (times_cont_diff_at_log.2 hx).comp x hf lemma times_cont_diff_within_at.log {n} (hf : times_cont_diff_within_at ℝ n f s x) (hx : f x ≠ 0) : times_cont_diff_within_at ℝ n (λ x, log (f x)) s x := (times_cont_diff_at_log.2 hx).comp_times_cont_diff_within_at x hf lemma times_cont_diff_on.log {n} (hf : times_cont_diff_on ℝ n f s) (hs : ∀ x ∈ s, f x ≠ 0) : times_cont_diff_on ℝ n (λ x, log (f x)) s := λ x hx, (hf x hx).log (hs x hx) lemma times_cont_diff.log {n} (hf : times_cont_diff ℝ n f) (h : ∀ x, f x ≠ 0) : times_cont_diff ℝ n (λ x, log (f x)) := times_cont_diff_iff_times_cont_diff_at.2 $ λ x, hf.times_cont_diff_at.log (h x) lemma differentiable_on.log (hf : differentiable_on ℝ f s) (hx : ∀ x ∈ s, f x ≠ 0) : differentiable_on ℝ (λx, log (f x)) s := λx h, (hf x h).log (hx x h) @[simp] lemma differentiable.log (hf : differentiable ℝ f) (hx : ∀ x, f x ≠ 0) : differentiable ℝ (λx, log (f x)) := λx, (hf x).log (hx x) lemma fderiv_within.log (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) (hxs : unique_diff_within_at ℝ s x) : fderiv_within ℝ (λx, log (f x)) s x = (f x)⁻¹ • fderiv_within ℝ f s x := (hf.has_fderiv_within_at.log hx).fderiv_within hxs @[simp] lemma fderiv.log (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : fderiv ℝ (λx, log (f x)) x = (f x)⁻¹ • fderiv ℝ f x := (hf.has_fderiv_at.log hx).fderiv end fderiv end log_differentiable namespace real /-- The function `exp(x)/x^n` tends to `+∞` at `+∞`, for any natural number `n` -/ lemma tendsto_exp_div_pow_at_top (n : ℕ) : tendsto (λx, exp x / x^n) at_top at_top := begin refine (at_top_basis_Ioi.tendsto_iff (at_top_basis' 1)).2 (λ C hC₁, _), have hC₀ : 0 < C, from zero_lt_one.trans_le hC₁, have : 0 < (exp 1 * C)⁻¹ := inv_pos.2 (mul_pos (exp_pos _) hC₀), obtain ⟨N, hN⟩ : ∃ N, ∀ k ≥ N, (↑k ^ n : ℝ) / exp 1 ^ k < (exp 1 * C)⁻¹ := eventually_at_top.1 ((tendsto_pow_const_div_const_pow_of_one_lt n (one_lt_exp_iff.2 zero_lt_one)).eventually (gt_mem_nhds this)), simp only [← exp_nat_mul, mul_one, div_lt_iff, exp_pos, ← div_eq_inv_mul] at hN, refine ⟨N, trivial, λ x hx, _⟩, rw mem_Ioi at hx, have hx₀ : 0 < x, from N.cast_nonneg.trans_lt hx, rw [mem_Ici, le_div_iff (pow_pos hx₀ _), ← le_div_iff' hC₀], calc x ^ n ≤ (nat_ceil x) ^ n : pow_le_pow_of_le_left hx₀.le (le_nat_ceil _) _ ... ≤ exp (nat_ceil x) / (exp 1 * C) : (hN _ (lt_nat_ceil.2 hx).le).le ... ≤ exp (x + 1) / (exp 1 * C) : div_le_div_of_le (mul_pos (exp_pos _) hC₀).le (exp_le_exp.2 $ (nat_ceil_lt_add_one hx₀.le).le) ... = exp x / C : by rw [add_comm, exp_add, mul_div_mul_left _ _ (exp_pos _).ne'] end /-- The function `x^n * exp(-x)` tends to `0` at `+∞`, for any natural number `n`. -/ lemma tendsto_pow_mul_exp_neg_at_top_nhds_0 (n : ℕ) : tendsto (λx, x^n * exp (-x)) at_top (𝓝 0) := (tendsto_inv_at_top_zero.comp (tendsto_exp_div_pow_at_top n)).congr $ λx, by rw [comp_app, inv_eq_one_div, div_div_eq_mul_div, one_mul, div_eq_mul_inv, exp_neg] /-- The function `(b * exp x + c) / (x ^ n)` tends to `+∞` at `+∞`, for any positive natural number `n` and any real numbers `b` and `c` such that `b` is positive. -/ lemma tendsto_mul_exp_add_div_pow_at_top (b c : ℝ) (n : ℕ) (hb : 0 < b) (hn : 1 ≤ n) : tendsto (λ x, (b * (exp x) + c) / (x^n)) at_top at_top := begin refine tendsto.congr' (eventually_eq_of_mem (Ioi_mem_at_top 0) _) (((tendsto_exp_div_pow_at_top n).const_mul_at_top hb).at_top_add ((tendsto_pow_neg_at_top hn).mul (@tendsto_const_nhds _ _ _ c _))), intros x hx, simp only [fpow_neg x n], ring, end /-- The function `(x ^ n) / (b * exp x + c)` tends to `0` at `+∞`, for any positive natural number `n` and any real numbers `b` and `c` such that `b` is nonzero. -/ lemma tendsto_div_pow_mul_exp_add_at_top (b c : ℝ) (n : ℕ) (hb : 0 ≠ b) (hn : 1 ≤ n) : tendsto (λ x, x^n / (b * (exp x) + c)) at_top (𝓝 0) := begin have H : ∀ d e, 0 < d → tendsto (λ (x:ℝ), x^n / (d * (exp x) + e)) at_top (𝓝 0), { intros b' c' h, convert (tendsto_mul_exp_add_div_pow_at_top b' c' n h hn).inv_tendsto_at_top , ext x, simpa only [pi.inv_apply] using inv_div.symm }, cases lt_or_gt_of_ne hb, { exact H b c h }, { convert (H (-b) (-c) (neg_pos.mpr h)).neg, { ext x, field_simp, rw [← neg_add (b * exp x) c, neg_div_neg_eq] }, { exact neg_zero.symm } }, end /-- The function `x * log (1 + t / x)` tends to `t` at `+∞`. -/ lemma tendsto_mul_log_one_plus_div_at_top (t : ℝ) : tendsto (λ x, x * log (1 + t / x)) at_top (𝓝 t) := begin have h₁ : tendsto (λ h, h⁻¹ * log (1 + t * h)) (𝓝[{0}ᶜ] 0) (𝓝 t), { simpa [has_deriv_at_iff_tendsto_slope] using ((has_deriv_at_const _ 1).add ((has_deriv_at_id 0).const_mul t)).log (by simp) }, have h₂ : tendsto (λ x : ℝ, x⁻¹) at_top (𝓝[{0}ᶜ] 0) := tendsto_inv_at_top_zero'.mono_right (nhds_within_mono _ (λ x hx, (set.mem_Ioi.mp hx).ne')), convert h₁.comp h₂, ext, field_simp [mul_comm], end open_locale big_operators /-- A crude lemma estimating the difference between `log (1-x)` and its Taylor series at `0`, where the main point of the bound is that it tends to `0`. The goal is to deduce the series expansion of the logarithm, in `has_sum_pow_div_log_of_abs_lt_1`. -/ lemma abs_log_sub_add_sum_range_le {x : ℝ} (h : abs x < 1) (n : ℕ) : abs ((∑ i in range n, x^(i+1)/(i+1)) + log (1-x)) ≤ (abs x)^(n+1) / (1 - abs x) := begin /- For the proof, we show that the derivative of the function to be estimated is small, and then apply the mean value inequality. -/ let F : ℝ → ℝ := λ x, ∑ i in range n, x^(i+1)/(i+1) + log (1-x), -- First step: compute the derivative of `F` have A : ∀ y ∈ Ioo (-1 : ℝ) 1, deriv F y = - (y^n) / (1 - y), { assume y hy, have : (∑ i in range n, (↑i + 1) * y ^ i / (↑i + 1)) = (∑ i in range n, y ^ i), { congr' with i, have : (i : ℝ) + 1 ≠ 0 := ne_of_gt (nat.cast_add_one_pos i), field_simp [this, mul_comm] }, field_simp [F, this, ← geom_sum_def, geom_sum_eq (ne_of_lt hy.2), sub_ne_zero_of_ne (ne_of_gt hy.2), sub_ne_zero_of_ne (ne_of_lt hy.2)], ring }, -- second step: show that the derivative of `F` is small have B : ∀ y ∈ Icc (-abs x) (abs x), abs (deriv F y) ≤ (abs x)^n / (1 - abs x), { assume y hy, have : y ∈ Ioo (-(1 : ℝ)) 1 := ⟨lt_of_lt_of_le (neg_lt_neg h) hy.1, lt_of_le_of_lt hy.2 h⟩, calc abs (deriv F y) = abs (-(y^n) / (1 - y)) : by rw [A y this] ... ≤ (abs x)^n / (1 - abs x) : begin have : abs y ≤ abs x := abs_le.2 hy, have : 0 < 1 - abs x, by linarith, have : 1 - abs x ≤ abs (1 - y) := le_trans (by linarith [hy.2]) (le_abs_self _), simp only [← pow_abs, abs_div, abs_neg], apply_rules [div_le_div, pow_nonneg, abs_nonneg, pow_le_pow_of_le_left] end }, -- third step: apply the mean value inequality have C : ∥F x - F 0∥ ≤ ((abs x)^n / (1 - abs x)) * ∥x - 0∥, { have : ∀ y ∈ Icc (- abs x) (abs x), differentiable_at ℝ F y, { assume y hy, have : 1 - y ≠ 0 := sub_ne_zero_of_ne (ne_of_gt (lt_of_le_of_lt hy.2 h)), simp [F, this] }, apply convex.norm_image_sub_le_of_norm_deriv_le this B (convex_Icc _ _) _ _, { simpa using abs_nonneg x }, { simp [le_abs_self x, neg_le.mp (neg_le_abs_self x)] } }, -- fourth step: conclude by massaging the inequality of the third step simpa [F, norm_eq_abs, div_mul_eq_mul_div, pow_succ'] using C end /-- Power series expansion of the logarithm around `1`. -/ theorem has_sum_pow_div_log_of_abs_lt_1 {x : ℝ} (h : abs x < 1) : has_sum (λ (n : ℕ), x ^ (n + 1) / (n + 1)) (-log (1 - x)) := begin rw summable.has_sum_iff_tendsto_nat, show tendsto (λ (n : ℕ), ∑ (i : ℕ) in range n, x ^ (i + 1) / (i + 1)) at_top (𝓝 (-log (1 - x))), { rw [tendsto_iff_norm_tendsto_zero], simp only [norm_eq_abs, sub_neg_eq_add], refine squeeze_zero (λ n, abs_nonneg _) (abs_log_sub_add_sum_range_le h) _, suffices : tendsto (λ (t : ℕ), abs x ^ (t + 1) / (1 - abs x)) at_top (𝓝 (abs x * 0 / (1 - abs x))), by simpa, simp only [pow_succ], refine (tendsto_const_nhds.mul _).div_const, exact tendsto_pow_at_top_nhds_0_of_lt_1 (abs_nonneg _) h }, show summable (λ (n : ℕ), x ^ (n + 1) / (n + 1)), { refine summable_of_norm_bounded _ (summable_geometric_of_lt_1 (abs_nonneg _) h) (λ i, _), calc ∥x ^ (i + 1) / (i + 1)∥ = abs x ^ (i+1) / (i+1) : begin have : (0 : ℝ) ≤ i + 1 := le_of_lt (nat.cast_add_one_pos i), rw [norm_eq_abs, abs_div, ← pow_abs, abs_of_nonneg this], end ... ≤ abs x ^ (i+1) / (0 + 1) : begin apply_rules [div_le_div_of_le_left, pow_nonneg, abs_nonneg, add_le_add_right, i.cast_nonneg], norm_num, end ... ≤ abs x ^ i : by simpa [pow_succ'] using mul_le_of_le_one_right (pow_nonneg (abs_nonneg x) i) (le_of_lt h) } end end real
8182d309d7d92bebe28dcd37062307bbb0ab8f8d
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/computability/primrec.lean
5782258691c6ebd7cfbec3b49c3149f869d5153a
[ "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
51,973
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.equiv.list import logic.function.iterate /-! # The primitive recursive functions The primitive recursive functions are the least collection of functions `nat → nat` which are closed under projections (using the mkpair pairing function), composition, zero, successor, and primitive recursion (i.e. nat.rec where the motive is C n := nat). We can extend this definition to a large class of basic types by using canonical encodings of types as natural numbers (Gödel numbering), which we implement through the type class `encodable`. (More precisely, we need that the composition of encode with decode yields a primitive recursive function, so we have the `primcodable` type class for this.) ## References * [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019] -/ open denumerable encodable namespace nat def elim {C : Sort*} : C → (ℕ → C → C) → ℕ → C := @nat.rec (λ _, C) @[simp] theorem elim_zero {C} (a f) : @nat.elim C a f 0 = a := rfl @[simp] theorem elim_succ {C} (a f n) : @nat.elim C a f (succ n) = f n (nat.elim a f n) := rfl def cases {C : Sort*} (a : C) (f : ℕ → C) : ℕ → C := nat.elim a (λ n _, f n) @[simp] theorem cases_zero {C} (a f) : @nat.cases C a f 0 = a := rfl @[simp] theorem cases_succ {C} (a f n) : @nat.cases C a f (succ n) = f n := rfl @[simp, reducible] def unpaired {α} (f : ℕ → ℕ → α) (n : ℕ) : α := f n.unpair.1 n.unpair.2 /-- The primitive recursive functions `ℕ → ℕ`. -/ inductive primrec : (ℕ → ℕ) → Prop | zero : primrec (λ n, 0) | succ : primrec succ | left : primrec (λ n, n.unpair.1) | right : primrec (λ n, n.unpair.2) | pair {f g} : primrec f → primrec g → primrec (λ n, mkpair (f n) (g n)) | comp {f g} : primrec f → primrec g → primrec (λ n, f (g n)) | prec {f g} : primrec f → primrec g → primrec (unpaired (λ z n, n.elim (f z) (λ y IH, g $ mkpair z $ mkpair y IH))) namespace primrec theorem of_eq {f g : ℕ → ℕ} (hf : primrec f) (H : ∀ n, f n = g n) : primrec g := (funext H : f = g) ▸ hf theorem const : ∀ (n : ℕ), primrec (λ _, n) | 0 := zero | (n+1) := succ.comp (const n) protected theorem id : primrec id := (left.pair right).of_eq $ λ n, by simp theorem prec1 {f} (m : ℕ) (hf : primrec f) : primrec (λ n, n.elim m (λ y IH, f $ mkpair y IH)) := ((prec (const m) (hf.comp right)).comp (zero.pair primrec.id)).of_eq $ λ n, by simp; dsimp; rw [unpair_mkpair] theorem cases1 {f} (m : ℕ) (hf : primrec f) : primrec (nat.cases m f) := (prec1 m (hf.comp left)).of_eq $ by simp [cases] theorem cases {f g} (hf : primrec f) (hg : primrec g) : primrec (unpaired (λ z n, n.cases (f z) (λ y, g $ mkpair z y))) := (prec hf (hg.comp (pair left (left.comp right)))).of_eq $ by simp [cases] protected theorem swap : primrec (unpaired (function.swap mkpair)) := (pair right left).of_eq $ λ n, by simp theorem swap' {f} (hf : primrec (unpaired f)) : primrec (unpaired (function.swap f)) := (hf.comp primrec.swap).of_eq $ λ n, by simp theorem pred : primrec pred := (cases1 0 primrec.id).of_eq $ λ n, by cases n; simp * theorem add : primrec (unpaired (+)) := (prec primrec.id ((succ.comp right).comp right)).of_eq $ λ p, by simp; induction p.unpair.2; simp [*, -add_comm, add_succ] theorem sub : primrec (unpaired has_sub.sub) := (prec primrec.id ((pred.comp right).comp right)).of_eq $ λ p, by simp; induction p.unpair.2; simp [*, -add_comm, sub_succ] theorem mul : primrec (unpaired (*)) := (prec zero (add.comp (pair left (right.comp right)))).of_eq $ λ p, by simp; induction p.unpair.2; simp [*, mul_succ, add_comm] theorem pow : primrec (unpaired (^)) := (prec (const 1) (mul.comp (pair (right.comp right) left))).of_eq $ λ p, by simp; induction p.unpair.2; simp [*, pow_succ'] end primrec end nat /-- A `primcodable` type is an `encodable` type for which the encode/decode functions are primitive recursive. -/ class primcodable (α : Type*) extends encodable α := (prim [] : nat.primrec (λ n, encodable.encode (decode n))) namespace primcodable open nat.primrec @[priority 10] instance of_denumerable (α) [denumerable α] : primcodable α := ⟨succ.of_eq $ by simp⟩ def of_equiv (α) {β} [primcodable α] (e : β ≃ α) : primcodable β := { prim := (primcodable.prim α).of_eq $ λ n, show encode (decode α n) = (option.cases_on (option.map e.symm (decode α n)) 0 (λ a, nat.succ (encode (e a))) : ℕ), by cases decode α n; dsimp; simp, ..encodable.of_equiv α e } instance empty : primcodable empty := ⟨zero⟩ instance unit : primcodable punit := ⟨(cases1 1 zero).of_eq $ λ n, by cases n; simp⟩ instance option {α : Type*} [h : primcodable α] : primcodable (option α) := ⟨(cases1 1 ((cases1 0 (succ.comp succ)).comp (primcodable.prim α))).of_eq $ λ n, by cases n; simp; cases decode α n; refl⟩ instance bool : primcodable bool := ⟨(cases1 1 (cases1 2 zero)).of_eq $ λ n, begin cases n, {refl}, cases n, {refl}, rw decode_ge_two, {refl}, exact dec_trivial end⟩ end primcodable /-- `primrec f` means `f` is primitive recursive (after encoding its input and output as natural numbers). -/ def primrec {α β} [primcodable α] [primcodable β] (f : α → β) : Prop := nat.primrec (λ n, encode ((decode α n).map f)) namespace primrec variables {α : Type*} {β : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable σ] open nat.primrec protected theorem encode : primrec (@encode α _) := (primcodable.prim α).of_eq $ λ n, by cases decode α n; refl protected theorem decode : primrec (decode α) := succ.comp (primcodable.prim α) theorem dom_denumerable {α β} [denumerable α] [primcodable β] {f : α → β} : primrec f ↔ nat.primrec (λ n, encode (f (of_nat α n))) := ⟨λ h, (pred.comp h).of_eq $ λ n, by simp; refl, λ h, (succ.comp h).of_eq $ λ n, by simp; refl⟩ theorem nat_iff {f : ℕ → ℕ} : primrec f ↔ nat.primrec f := dom_denumerable theorem encdec : primrec (λ n, encode (decode α n)) := nat_iff.2 (primcodable.prim α) theorem option_some : primrec (@some α) := ((cases1 0 (succ.comp succ)).comp (primcodable.prim α)).of_eq $ λ n, by cases decode α n; simp theorem of_eq {f g : α → σ} (hf : primrec f) (H : ∀ n, f n = g n) : primrec g := (funext H : f = g) ▸ hf theorem const (x : σ) : primrec (λ a : α, x) := ((cases1 0 (const (encode x).succ)).comp (primcodable.prim α)).of_eq $ λ n, by cases decode α n; refl protected theorem id : primrec (@id α) := (primcodable.prim α).of_eq $ by simp theorem comp {f : β → σ} {g : α → β} (hf : primrec f) (hg : primrec g) : primrec (λ a, f (g a)) := ((cases1 0 (hf.comp $ pred.comp hg)).comp (primcodable.prim α)).of_eq $ λ n, begin cases decode α n, {refl}, simp [encodek] end theorem succ : primrec nat.succ := nat_iff.2 nat.primrec.succ theorem pred : primrec nat.pred := nat_iff.2 nat.primrec.pred theorem encode_iff {f : α → σ} : primrec (λ a, encode (f a)) ↔ primrec f := ⟨λ h, nat.primrec.of_eq h $ λ n, by cases decode α n; refl, primrec.encode.comp⟩ theorem of_nat_iff {α β} [denumerable α] [primcodable β] {f : α → β} : primrec f ↔ primrec (λ n, f (of_nat α n)) := dom_denumerable.trans $ nat_iff.symm.trans encode_iff protected theorem of_nat (α) [denumerable α] : primrec (of_nat α) := of_nat_iff.1 primrec.id theorem option_some_iff {f : α → σ} : primrec (λ a, some (f a)) ↔ primrec f := ⟨λ h, encode_iff.1 $ pred.comp $ encode_iff.2 h, option_some.comp⟩ theorem of_equiv {β} {e : β ≃ α} : by haveI := primcodable.of_equiv α e; exact primrec e := by letI : primcodable β := primcodable.of_equiv α e; exact encode_iff.1 primrec.encode theorem of_equiv_symm {β} {e : β ≃ α} : by haveI := primcodable.of_equiv α e; exact primrec e.symm := by letI := primcodable.of_equiv α e; exact encode_iff.1 (show primrec (λ a, encode (e (e.symm a))), by simp [primrec.encode]) theorem of_equiv_iff {β} (e : β ≃ α) {f : σ → β} : by haveI := primcodable.of_equiv α e; exact primrec (λ a, e (f a)) ↔ primrec f := by letI := primcodable.of_equiv α e; exact ⟨λ h, (of_equiv_symm.comp h).of_eq (λ a, by simp), of_equiv.comp⟩ theorem of_equiv_symm_iff {β} (e : β ≃ α) {f : σ → α} : by haveI := primcodable.of_equiv α e; exact primrec (λ a, e.symm (f a)) ↔ primrec f := by letI := primcodable.of_equiv α e; exact ⟨λ h, (of_equiv.comp h).of_eq (λ a, by simp), of_equiv_symm.comp⟩ end primrec namespace primcodable open nat.primrec instance prod {α β} [primcodable α] [primcodable β] : primcodable (α × β) := ⟨((cases zero ((cases zero succ).comp (pair right ((primcodable.prim β).comp left)))).comp (pair right ((primcodable.prim α).comp left))).of_eq $ λ n, begin simp [nat.unpaired], cases decode α n.unpair.1, { simp }, cases decode β n.unpair.2; simp end⟩ end primcodable namespace primrec variables {α : Type*} {σ : Type*} [primcodable α] [primcodable σ] open nat.primrec theorem fst {α β} [primcodable α] [primcodable β] : primrec (@prod.fst α β) := ((cases zero ((cases zero (nat.primrec.succ.comp left)).comp (pair right ((primcodable.prim β).comp left)))).comp (pair right ((primcodable.prim α).comp left))).of_eq $ λ n, begin simp, cases decode α n.unpair.1; simp, cases decode β n.unpair.2; simp end theorem snd {α β} [primcodable α] [primcodable β] : primrec (@prod.snd α β) := ((cases zero ((cases zero (nat.primrec.succ.comp right)).comp (pair right ((primcodable.prim β).comp left)))).comp (pair right ((primcodable.prim α).comp left))).of_eq $ λ n, begin simp, cases decode α n.unpair.1; simp, cases decode β n.unpair.2; simp end theorem pair {α β γ} [primcodable α] [primcodable β] [primcodable γ] {f : α → β} {g : α → γ} (hf : primrec f) (hg : primrec g) : primrec (λ a, (f a, g a)) := ((cases1 0 (nat.primrec.succ.comp $ pair (nat.primrec.pred.comp hf) (nat.primrec.pred.comp hg))).comp (primcodable.prim α)).of_eq $ λ n, by cases decode α n; simp [encodek]; refl theorem unpair : primrec nat.unpair := (pair (nat_iff.2 nat.primrec.left) (nat_iff.2 nat.primrec.right)).of_eq $ λ n, by simp theorem list_nth₁ : ∀ (l : list α), primrec l.nth | [] := dom_denumerable.2 zero | (a::l) := dom_denumerable.2 $ (cases1 (encode a).succ $ dom_denumerable.1 $ list_nth₁ l).of_eq $ λ n, by cases n; simp end primrec /-- `primrec₂ f` means `f` is a binary primitive recursive function. This is technically unnecessary since we can always curry all the arguments together, but there are enough natural two-arg functions that it is convenient to express this directly. -/ def primrec₂ {α β σ} [primcodable α] [primcodable β] [primcodable σ] (f : α → β → σ) := primrec (λ p : α × β, f p.1 p.2) /-- `primrec_pred p` means `p : α → Prop` is a (decidable) primitive recursive predicate, which is to say that `to_bool ∘ p : α → bool` is primitive recursive. -/ def primrec_pred {α} [primcodable α] (p : α → Prop) [decidable_pred p] := primrec (λ a, to_bool (p a)) /-- `primrec_rel p` means `p : α → β → Prop` is a (decidable) primitive recursive relation, which is to say that `to_bool ∘ p : α → β → bool` is primitive recursive. -/ def primrec_rel {α β} [primcodable α] [primcodable β] (s : α → β → Prop) [∀ a b, decidable (s a b)] := primrec₂ (λ a b, to_bool (s a b)) namespace primrec₂ variables {α : Type*} {β : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable σ] theorem of_eq {f g : α → β → σ} (hg : primrec₂ f) (H : ∀ a b, f a b = g a b) : primrec₂ g := (by funext a b; apply H : f = g) ▸ hg theorem const (x : σ) : primrec₂ (λ (a : α) (b : β), x) := primrec.const _ protected theorem pair : primrec₂ (@prod.mk α β) := primrec.pair primrec.fst primrec.snd theorem left : primrec₂ (λ (a : α) (b : β), a) := primrec.fst theorem right : primrec₂ (λ (a : α) (b : β), b) := primrec.snd theorem mkpair : primrec₂ nat.mkpair := by simp [primrec₂, primrec]; constructor theorem unpaired {f : ℕ → ℕ → α} : primrec (nat.unpaired f) ↔ primrec₂ f := ⟨λ h, by simpa using h.comp mkpair, λ h, h.comp primrec.unpair⟩ theorem unpaired' {f : ℕ → ℕ → ℕ} : nat.primrec (nat.unpaired f) ↔ primrec₂ f := primrec.nat_iff.symm.trans unpaired theorem encode_iff {f : α → β → σ} : primrec₂ (λ a b, encode (f a b)) ↔ primrec₂ f := primrec.encode_iff theorem option_some_iff {f : α → β → σ} : primrec₂ (λ a b, some (f a b)) ↔ primrec₂ f := primrec.option_some_iff theorem of_nat_iff {α β σ} [denumerable α] [denumerable β] [primcodable σ] {f : α → β → σ} : primrec₂ f ↔ primrec₂ (λ m n : ℕ, f (of_nat α m) (of_nat β n)) := (primrec.of_nat_iff.trans $ by simp).trans unpaired theorem uncurry {f : α → β → σ} : primrec (function.uncurry f) ↔ primrec₂ f := by rw [show function.uncurry f = λ (p : α × β), f p.1 p.2, from funext $ λ ⟨a, b⟩, rfl]; refl theorem curry {f : α × β → σ} : primrec₂ (function.curry f) ↔ primrec f := by rw [← uncurry, function.uncurry_curry] end primrec₂ section comp variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable δ] [primcodable σ] theorem primrec.comp₂ {f : γ → σ} {g : α → β → γ} (hf : primrec f) (hg : primrec₂ g) : primrec₂ (λ a b, f (g a b)) := hf.comp hg theorem primrec₂.comp {f : β → γ → σ} {g : α → β} {h : α → γ} (hf : primrec₂ f) (hg : primrec g) (hh : primrec h) : primrec (λ a, f (g a) (h a)) := hf.comp (hg.pair hh) theorem primrec₂.comp₂ {f : γ → δ → σ} {g : α → β → γ} {h : α → β → δ} (hf : primrec₂ f) (hg : primrec₂ g) (hh : primrec₂ h) : primrec₂ (λ a b, f (g a b) (h a b)) := hf.comp hg hh theorem primrec_pred.comp {p : β → Prop} [decidable_pred p] {f : α → β} : primrec_pred p → primrec f → primrec_pred (λ a, p (f a)) := primrec.comp theorem primrec_rel.comp {R : β → γ → Prop} [∀ a b, decidable (R a b)] {f : α → β} {g : α → γ} : primrec_rel R → primrec f → primrec g → primrec_pred (λ a, R (f a) (g a)) := primrec₂.comp theorem primrec_rel.comp₂ {R : γ → δ → Prop} [∀ a b, decidable (R a b)] {f : α → β → γ} {g : α → β → δ} : primrec_rel R → primrec₂ f → primrec₂ g → primrec_rel (λ a b, R (f a b) (g a b)) := primrec_rel.comp end comp theorem primrec_pred.of_eq {α} [primcodable α] {p q : α → Prop} [decidable_pred p] [decidable_pred q] (hp : primrec_pred p) (H : ∀ a, p a ↔ q a) : primrec_pred q := primrec.of_eq hp (λ a, to_bool_congr (H a)) theorem primrec_rel.of_eq {α β} [primcodable α] [primcodable β] {r s : α → β → Prop} [∀ a b, decidable (r a b)] [∀ a b, decidable (s a b)] (hr : primrec_rel r) (H : ∀ a b, r a b ↔ s a b) : primrec_rel s := primrec₂.of_eq hr (λ a b, to_bool_congr (H a b)) namespace primrec₂ variables {α : Type*} {β : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable σ] open nat.primrec theorem swap {f : α → β → σ} (h : primrec₂ f) : primrec₂ (function.swap f) := h.comp₂ primrec₂.right primrec₂.left theorem nat_iff {f : α → β → σ} : primrec₂ f ↔ nat.primrec (nat.unpaired $ λ m n : ℕ, encode $ (decode α m).bind $ λ a, (decode β n).map (f a)) := have ∀ (a : option α) (b : option β), option.map (λ (p : α × β), f p.1 p.2) (option.bind a (λ (a : α), option.map (prod.mk a) b)) = option.bind a (λ a, option.map (f a) b), by intros; cases a; [refl, {cases b; refl}], by simp [primrec₂, primrec, this] theorem nat_iff' {f : α → β → σ} : primrec₂ f ↔ primrec₂ (λ m n : ℕ, option.bind (decode α m) (λ a, option.map (f a) (decode β n))) := nat_iff.trans $ unpaired'.trans encode_iff end primrec₂ namespace primrec variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable δ] [primcodable σ] theorem to₂ {f : α × β → σ} (hf : primrec f) : primrec₂ (λ a b, f (a, b)) := hf.of_eq $ λ ⟨a, b⟩, rfl theorem nat_elim {f : α → β} {g : α → ℕ × β → β} (hf : primrec f) (hg : primrec₂ g) : primrec₂ (λ a (n : ℕ), n.elim (f a) (λ n IH, g a (n, IH))) := primrec₂.nat_iff.2 $ ((nat.primrec.cases nat.primrec.zero $ (nat.primrec.prec hf $ nat.primrec.comp hg $ nat.primrec.left.pair $ (nat.primrec.left.comp nat.primrec.right).pair $ nat.primrec.pred.comp $ nat.primrec.right.comp nat.primrec.right).comp $ nat.primrec.right.pair $ nat.primrec.right.comp nat.primrec.left).comp $ nat.primrec.id.pair $ (primcodable.prim α).comp nat.primrec.left).of_eq $ λ n, begin simp, cases decode α n.unpair.1 with a, {refl}, simp [encodek], induction n.unpair.2 with m; simp [encodek], simp [ih, encodek] end theorem nat_elim' {f : α → ℕ} {g : α → β} {h : α → ℕ × β → β} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : primrec (λ a, (f a).elim (g a) (λ n IH, h a (n, IH))) := (nat_elim hg hh).comp primrec.id hf theorem nat_elim₁ {f : ℕ → α → α} (a : α) (hf : primrec₂ f) : primrec (nat.elim a f) := nat_elim' primrec.id (const a) $ comp₂ hf primrec₂.right theorem nat_cases' {f : α → β} {g : α → ℕ → β} (hf : primrec f) (hg : primrec₂ g) : primrec₂ (λ a, nat.cases (f a) (g a)) := nat_elim hf $ hg.comp₂ primrec₂.left $ comp₂ fst primrec₂.right theorem nat_cases {f : α → ℕ} {g : α → β} {h : α → ℕ → β} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : primrec (λ a, (f a).cases (g a) (h a)) := (nat_cases' hg hh).comp primrec.id hf theorem nat_cases₁ {f : ℕ → α} (a : α) (hf : primrec f) : primrec (nat.cases a f) := nat_cases primrec.id (const a) (comp₂ hf primrec₂.right) theorem nat_iterate {f : α → ℕ} {g : α → β} {h : α → β → β} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : primrec (λ a, (h a)^[f a] (g a)) := (nat_elim' hf hg (hh.comp₂ primrec₂.left $ snd.comp₂ primrec₂.right)).of_eq $ λ a, by induction f a; simp [*, function.iterate_succ'] theorem option_cases {o : α → option β} {f : α → σ} {g : α → β → σ} (ho : primrec o) (hf : primrec f) (hg : primrec₂ g) : @primrec _ σ _ _ (λ a, option.cases_on (o a) (f a) (g a)) := encode_iff.1 $ (nat_cases (encode_iff.2 ho) (encode_iff.2 hf) $ pred.comp₂ $ primrec₂.encode_iff.2 $ (primrec₂.nat_iff'.1 hg).comp₂ ((@primrec.encode α _).comp fst).to₂ primrec₂.right).of_eq $ λ a, by cases o a with b; simp [encodek]; refl theorem option_bind {f : α → option β} {g : α → β → option σ} (hf : primrec f) (hg : primrec₂ g) : primrec (λ a, (f a).bind (g a)) := (option_cases hf (const none) hg).of_eq $ λ a, by cases f a; refl theorem option_bind₁ {f : α → option σ} (hf : primrec f) : primrec (λ o, option.bind o f) := option_bind primrec.id (hf.comp snd).to₂ theorem option_map {f : α → option β} {g : α → β → σ} (hf : primrec f) (hg : primrec₂ g) : primrec (λ a, (f a).map (g a)) := option_bind hf (option_some.comp₂ hg) theorem option_map₁ {f : α → σ} (hf : primrec f) : primrec (option.map f) := option_map primrec.id (hf.comp snd).to₂ theorem option_iget [inhabited α] : primrec (@option.iget α _) := (option_cases primrec.id (const $ default α) primrec₂.right).of_eq $ λ o, by cases o; refl theorem option_is_some : primrec (@option.is_some α) := (option_cases primrec.id (const ff) (const tt).to₂).of_eq $ λ o, by cases o; refl theorem option_get_or_else : primrec₂ (@option.get_or_else α) := primrec.of_eq (option_cases primrec₂.left primrec₂.right primrec₂.right) $ λ ⟨o, a⟩, by cases o; refl theorem bind_decode_iff {f : α → β → option σ} : primrec₂ (λ a n, (decode β n).bind (f a)) ↔ primrec₂ f := ⟨λ h, by simpa [encodek] using h.comp fst ((@primrec.encode β _).comp snd), λ h, option_bind (primrec.decode.comp snd) $ h.comp (fst.comp fst) snd⟩ theorem map_decode_iff {f : α → β → σ} : primrec₂ (λ a n, (decode β n).map (f a)) ↔ primrec₂ f := bind_decode_iff.trans primrec₂.option_some_iff theorem nat_add : primrec₂ ((+) : ℕ → ℕ → ℕ) := primrec₂.unpaired'.1 nat.primrec.add theorem nat_sub : primrec₂ (has_sub.sub : ℕ → ℕ → ℕ) := primrec₂.unpaired'.1 nat.primrec.sub theorem nat_mul : primrec₂ ((*) : ℕ → ℕ → ℕ) := primrec₂.unpaired'.1 nat.primrec.mul theorem cond {c : α → bool} {f : α → σ} {g : α → σ} (hc : primrec c) (hf : primrec f) (hg : primrec g) : primrec (λ a, cond (c a) (f a) (g a)) := (nat_cases (encode_iff.2 hc) hg (hf.comp fst).to₂).of_eq $ λ a, by cases c a; refl theorem ite {c : α → Prop} [decidable_pred c] {f : α → σ} {g : α → σ} (hc : primrec_pred c) (hf : primrec f) (hg : primrec g) : primrec (λ a, if c a then f a else g a) := by simpa using cond hc hf hg theorem nat_le : primrec_rel ((≤) : ℕ → ℕ → Prop) := (nat_cases nat_sub (const tt) (const ff).to₂).of_eq $ λ p, begin dsimp [function.swap], cases e : p.1 - p.2 with n, { simp [nat.sub_eq_zero_iff_le.1 e] }, { simp [not_le.2 (nat.lt_of_sub_eq_succ e)] } end theorem nat_min : primrec₂ (@min ℕ _) := ite nat_le fst snd theorem nat_max : primrec₂ (@max ℕ _) := ite (nat_le.comp primrec.snd primrec.fst) fst snd theorem dom_bool (f : bool → α) : primrec f := (cond primrec.id (const (f tt)) (const (f ff))).of_eq $ λ b, by cases b; refl theorem dom_bool₂ (f : bool → bool → α) : primrec₂ f := (cond fst ((dom_bool (f tt)).comp snd) ((dom_bool (f ff)).comp snd)).of_eq $ λ ⟨a, b⟩, by cases a; refl protected theorem bnot : primrec bnot := dom_bool _ protected theorem band : primrec₂ band := dom_bool₂ _ protected theorem bor : primrec₂ bor := dom_bool₂ _ protected theorem not {p : α → Prop} [decidable_pred p] (hp : primrec_pred p) : primrec_pred (λ a, ¬ p a) := (primrec.bnot.comp hp).of_eq $ λ n, by simp protected theorem and {p q : α → Prop} [decidable_pred p] [decidable_pred q] (hp : primrec_pred p) (hq : primrec_pred q) : primrec_pred (λ a, p a ∧ q a) := (primrec.band.comp hp hq).of_eq $ λ n, by simp protected theorem or {p q : α → Prop} [decidable_pred p] [decidable_pred q] (hp : primrec_pred p) (hq : primrec_pred q) : primrec_pred (λ a, p a ∨ q a) := (primrec.bor.comp hp hq).of_eq $ λ n, by simp protected theorem eq [decidable_eq α] : primrec_rel (@eq α) := have primrec_rel (λ a b : ℕ, a = b), from (primrec.and nat_le nat_le.swap).of_eq $ λ a, by simp [le_antisymm_iff], (this.comp₂ (primrec.encode.comp₂ primrec₂.left) (primrec.encode.comp₂ primrec₂.right)).of_eq $ λ a b, encode_injective.eq_iff theorem nat_lt : primrec_rel ((<) : ℕ → ℕ → Prop) := (nat_le.comp snd fst).not.of_eq $ λ p, by simp theorem option_guard {p : α → β → Prop} [∀ a b, decidable (p a b)] (hp : primrec_rel p) {f : α → β} (hf : primrec f) : primrec (λ a, option.guard (p a) (f a)) := ite (hp.comp primrec.id hf) (option_some_iff.2 hf) (const none) theorem option_orelse : primrec₂ ((<|>) : option α → option α → option α) := (option_cases fst snd (fst.comp fst).to₂).of_eq $ λ ⟨o₁, o₂⟩, by cases o₁; cases o₂; refl protected theorem decode2 : primrec (decode2 α) := option_bind primrec.decode $ option_guard ((@primrec.eq _ _ nat.decidable_eq).comp (encode_iff.2 snd) (fst.comp fst)) snd theorem list_find_index₁ {p : α → β → Prop} [∀ a b, decidable (p a b)] (hp : primrec_rel p) : ∀ (l : list β), primrec (λ a, l.find_index (p a)) | [] := const 0 | (a::l) := ite (hp.comp primrec.id (const a)) (const 0) (succ.comp (list_find_index₁ l)) theorem list_index_of₁ [decidable_eq α] (l : list α) : primrec (λ a, l.index_of a) := list_find_index₁ primrec.eq l theorem dom_fintype [fintype α] (f : α → σ) : primrec f := let ⟨l, nd, m⟩ := fintype.exists_univ_list α in option_some_iff.1 $ begin haveI := decidable_eq_of_encodable α, refine ((list_nth₁ (l.map f)).comp (list_index_of₁ l)).of_eq (λ a, _), rw [list.nth_map, list.nth_le_nth (list.index_of_lt_length.2 (m _)), list.index_of_nth_le]; refl end theorem nat_bodd_div2 : primrec nat.bodd_div2 := (nat_elim' primrec.id (const (ff, 0)) (((cond fst (pair (const ff) (succ.comp snd)) (pair (const tt) snd)).comp snd).comp snd).to₂).of_eq $ λ n, begin simp [-nat.bodd_div2_eq], induction n with n IH, {refl}, simp [-nat.bodd_div2_eq, nat.bodd_div2, *], rcases nat.bodd_div2 n with ⟨_|_, m⟩; simp [nat.bodd_div2] end theorem nat_bodd : primrec nat.bodd := fst.comp nat_bodd_div2 theorem nat_div2 : primrec nat.div2 := snd.comp nat_bodd_div2 theorem nat_bit0 : primrec (@bit0 ℕ _) := nat_add.comp primrec.id primrec.id theorem nat_bit1 : primrec (@bit1 ℕ _ _) := nat_add.comp nat_bit0 (const 1) theorem nat_bit : primrec₂ nat.bit := (cond primrec.fst (nat_bit1.comp primrec.snd) (nat_bit0.comp primrec.snd)).of_eq $ λ n, by cases n.1; refl theorem nat_div_mod : primrec₂ (λ n k : ℕ, (n / k, n % k)) := let f (a : ℕ × ℕ) : ℕ × ℕ := a.1.elim (0, 0) (λ _ IH, if nat.succ IH.2 = a.2 then (nat.succ IH.1, 0) else (IH.1, nat.succ IH.2)) in have hf : primrec f, from nat_elim' fst (const (0, 0)) $ ((ite ((@primrec.eq ℕ _ _).comp (succ.comp $ snd.comp snd) fst) (pair (succ.comp $ fst.comp snd) (const 0)) (pair (fst.comp snd) (succ.comp $ snd.comp snd))) .comp (pair (snd.comp fst) (snd.comp snd))).to₂, suffices ∀ k n, (n / k, n % k) = f (n, k), from hf.of_eq $ λ ⟨m, n⟩, by simp [this], λ k n, begin have : (f (n, k)).2 + k * (f (n, k)).1 = n ∧ (0 < k → (f (n, k)).2 < k) ∧ (k = 0 → (f (n, k)).1 = 0), { induction n with n IH, {exact ⟨rfl, id, λ _, rfl⟩}, rw [λ n:ℕ, show f (n.succ, k) = _root_.ite ((f (n, k)).2.succ = k) (nat.succ (f (n, k)).1, 0) ((f (n, k)).1, (f (n, k)).2.succ), from rfl], by_cases h : (f (n, k)).2.succ = k; simp [h], { have := congr_arg nat.succ IH.1, refine ⟨_, λ k0, nat.no_confusion (h.trans k0)⟩, rwa [← nat.succ_add, h, add_comm, ← nat.mul_succ] at this }, { exact ⟨by rw [nat.succ_add, IH.1], λ k0, lt_of_le_of_ne (IH.2.1 k0) h, IH.2.2⟩ } }, revert this, cases f (n, k) with D M, simp, intros h₁ h₂ h₃, cases nat.eq_zero_or_pos k, { simp [h, h₃ h] at h₁ ⊢, simp [h₁] }, { exact (nat.div_mod_unique h).2 ⟨h₁, h₂ h⟩ } end theorem nat_div : primrec₂ ((/) : ℕ → ℕ → ℕ) := fst.comp₂ nat_div_mod theorem nat_mod : primrec₂ ((%) : ℕ → ℕ → ℕ) := snd.comp₂ nat_div_mod end primrec section variables {α : Type*} {β : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable σ] variable (H : nat.primrec (λ n, encodable.encode (decode (list β) n))) include H open primrec private def prim : primcodable (list β) := ⟨H⟩ private lemma list_cases' {f : α → list β} {g : α → σ} {h : α → β × list β → σ} (hf : by haveI := prim H; exact primrec f) (hg : primrec g) (hh : by haveI := prim H; exact primrec₂ h) : @primrec _ σ _ _ (λ a, list.cases_on (f a) (g a) (λ b l, h a (b, l))) := by letI := prim H; exact have @primrec _ (option σ) _ _ (λ a, (decode (option (β × list β)) (encode (f a))).map (λ o, option.cases_on o (g a) (h a))), from ((@map_decode_iff _ (option (β × list β)) _ _ _ _ _).2 $ to₂ $ option_cases snd (hg.comp fst) (hh.comp₂ (fst.comp₂ primrec₂.left) primrec₂.right)) .comp primrec.id (encode_iff.2 hf), option_some_iff.1 $ this.of_eq $ λ a, by cases f a with b l; simp [encodek]; refl private lemma list_foldl' {f : α → list β} {g : α → σ} {h : α → σ × β → σ} (hf : by haveI := prim H; exact primrec f) (hg : primrec g) (hh : by haveI := prim H; exact primrec₂ h) : primrec (λ a, (f a).foldl (λ s b, h a (s, b)) (g a)) := by letI := prim H; exact let G (a : α) (IH : σ × list β) : σ × list β := list.cases_on IH.2 IH (λ b l, (h a (IH.1, b), l)) in let F (a : α) (n : ℕ) := (G a)^[n] (g a, f a) in have primrec (λ a, (F a (encode (f a))).1), from fst.comp $ nat_iterate (encode_iff.2 hf) (pair hg hf) $ list_cases' H (snd.comp snd) snd $ to₂ $ pair (hh.comp (fst.comp fst) $ pair ((fst.comp snd).comp fst) (fst.comp snd)) (snd.comp snd), this.of_eq $ λ a, begin have : ∀ n, F a n = ((list.take n (f a)).foldl (λ s b, h a (s, b)) (g a), list.drop n (f a)), { intro, simp [F], generalize : f a = l, generalize : g a = x, induction n with n IH generalizing l x, {refl}, simp, cases l with b l; simp [IH] }, rw [this, list.take_all_of_le (length_le_encode _)] end private lemma list_cons' : by haveI := prim H; exact primrec₂ (@list.cons β) := by letI := prim H; exact encode_iff.1 (succ.comp $ primrec₂.mkpair.comp (encode_iff.2 fst) (encode_iff.2 snd)) private lemma list_reverse' : by haveI := prim H; exact primrec (@list.reverse β) := by letI := prim H; exact (list_foldl' H primrec.id (const []) $ to₂ $ ((list_cons' H).comp snd fst).comp snd).of_eq (suffices ∀ l r, list.foldl (λ (s : list β) (b : β), b :: s) r l = list.reverse_core l r, from λ l, this l [], λ l, by induction l; simp [*, list.reverse_core]) end namespace primcodable variables {α : Type*} {β : Type*} variables [primcodable α] [primcodable β] open primrec instance sum : primcodable (α ⊕ β) := ⟨primrec.nat_iff.1 $ (encode_iff.2 (cond nat_bodd (((@primrec.decode β _).comp nat_div2).option_map $ to₂ $ nat_bit.comp (const tt) (primrec.encode.comp snd)) (((@primrec.decode α _).comp nat_div2).option_map $ to₂ $ nat_bit.comp (const ff) (primrec.encode.comp snd)))).of_eq $ λ n, show _ = encode (decode_sum n), begin simp [decode_sum], cases nat.bodd n; simp [decode_sum], { cases decode α n.div2; refl }, { cases decode β n.div2; refl } end⟩ instance list : primcodable (list α) := ⟨ by letI H := primcodable.prim (list ℕ); exact have primrec₂ (λ (a : α) (o : option (list ℕ)), o.map (list.cons (encode a))), from option_map snd $ (list_cons' H).comp ((@primrec.encode α _).comp (fst.comp fst)) snd, have primrec (λ n, (of_nat (list ℕ) n).reverse.foldl (λ o m, (decode α m).bind (λ a, o.map (list.cons (encode a)))) (some [])), from list_foldl' H ((list_reverse' H).comp (primrec.of_nat (list ℕ))) (const (some [])) (primrec.comp₂ (bind_decode_iff.2 $ primrec₂.swap this) primrec₂.right), nat_iff.1 $ (encode_iff.2 this).of_eq $ λ n, begin rw list.foldl_reverse, apply nat.case_strong_induction_on n, { simp }, intros n IH, simp, cases decode α n.unpair.1 with a, {refl}, simp, suffices : ∀ (o : option (list ℕ)) p (_ : encode o = encode p), encode (option.map (list.cons (encode a)) o) = encode (option.map (list.cons a) p), from this _ _ (IH _ (nat.unpair_le_right n)), intros o p IH, cases o; cases p; injection IH with h, exact congr_arg (λ k, (nat.mkpair (encode a) k).succ.succ) h end⟩ end primcodable namespace primrec variables {α : Type*} {β : Type*} {γ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable σ] theorem sum_inl : primrec (@sum.inl α β) := encode_iff.1 $ nat_bit0.comp primrec.encode theorem sum_inr : primrec (@sum.inr α β) := encode_iff.1 $ nat_bit1.comp primrec.encode theorem sum_cases {f : α → β ⊕ γ} {g : α → β → σ} {h : α → γ → σ} (hf : primrec f) (hg : primrec₂ g) (hh : primrec₂ h) : @primrec _ σ _ _ (λ a, sum.cases_on (f a) (g a) (h a)) := option_some_iff.1 $ (cond (nat_bodd.comp $ encode_iff.2 hf) (option_map (primrec.decode.comp $ nat_div2.comp $ encode_iff.2 hf) hh) (option_map (primrec.decode.comp $ nat_div2.comp $ encode_iff.2 hf) hg)).of_eq $ λ a, by cases f a with b c; simp [nat.div2_bit, nat.bodd_bit, encodek]; refl theorem list_cons : primrec₂ (@list.cons α) := list_cons' (primcodable.prim _) theorem list_cases {f : α → list β} {g : α → σ} {h : α → β × list β → σ} : primrec f → primrec g → primrec₂ h → @primrec _ σ _ _ (λ a, list.cases_on (f a) (g a) (λ b l, h a (b, l))) := list_cases' (primcodable.prim _) theorem list_foldl {f : α → list β} {g : α → σ} {h : α → σ × β → σ} : primrec f → primrec g → primrec₂ h → primrec (λ a, (f a).foldl (λ s b, h a (s, b)) (g a)) := list_foldl' (primcodable.prim _) theorem list_reverse : primrec (@list.reverse α) := list_reverse' (primcodable.prim _) theorem list_foldr {f : α → list β} {g : α → σ} {h : α → β × σ → σ} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : primrec (λ a, (f a).foldr (λ b s, h a (b, s)) (g a)) := (list_foldl (list_reverse.comp hf) hg $ to₂ $ hh.comp fst $ (pair snd fst).comp snd).of_eq $ λ a, by simp [list.foldl_reverse] theorem list_head' : primrec (@list.head' α) := (list_cases primrec.id (const none) (option_some_iff.2 $ (fst.comp snd)).to₂).of_eq $ λ l, by cases l; refl theorem list_head [inhabited α] : primrec (@list.head α _) := (option_iget.comp list_head').of_eq $ λ l, l.head_eq_head'.symm theorem list_tail : primrec (@list.tail α) := (list_cases primrec.id (const []) (snd.comp snd).to₂).of_eq $ λ l, by cases l; refl theorem list_rec {f : α → list β} {g : α → σ} {h : α → β × list β × σ → σ} (hf : primrec f) (hg : primrec g) (hh : primrec₂ h) : @primrec _ σ _ _ (λ a, list.rec_on (f a) (g a) (λ b l IH, h a (b, l, IH))) := let F (a : α) := (f a).foldr (λ (b : β) (s : list β × σ), (b :: s.1, h a (b, s))) ([], g a) in have primrec F, from list_foldr hf (pair (const []) hg) $ to₂ $ pair ((list_cons.comp fst (fst.comp snd)).comp snd) hh, (snd.comp this).of_eq $ λ a, begin suffices : F a = (f a, list.rec_on (f a) (g a) (λ b l IH, h a (b, l, IH))), {rw this}, simp [F], induction f a with b l IH; simp * end theorem list_nth : primrec₂ (@list.nth α) := let F (l : list α) (n : ℕ) := l.foldl (λ (s : ℕ ⊕ α) (a : α), sum.cases_on s (@nat.cases (ℕ ⊕ α) (sum.inr a) sum.inl) sum.inr) (sum.inl n) in have hF : primrec₂ F, from list_foldl fst (sum_inl.comp snd) ((sum_cases fst (nat_cases snd (sum_inr.comp $ snd.comp fst) (sum_inl.comp snd).to₂).to₂ (sum_inr.comp snd).to₂).comp snd).to₂, have @primrec _ (option α) _ _ (λ p : list α × ℕ, sum.cases_on (F p.1 p.2) (λ _, none) some), from sum_cases hF (const none).to₂ (option_some.comp snd).to₂, this.to₂.of_eq $ λ l n, begin dsimp, symmetry, induction l with a l IH generalizing n, {refl}, cases n with n, { rw [(_ : F (a :: l) 0 = sum.inr a)], {refl}, clear IH, dsimp [F], induction l with b l IH; simp * }, { apply IH } end theorem list_inth [inhabited α] : primrec₂ (@list.inth α _) := option_iget.comp₂ list_nth theorem list_append : primrec₂ ((++) : list α → list α → list α) := (list_foldr fst snd $ to₂ $ comp (@list_cons α _) snd).to₂.of_eq $ λ l₁ l₂, by induction l₁; simp * theorem list_concat : primrec₂ (λ l (a:α), l ++ [a]) := list_append.comp fst (list_cons.comp snd (const [])) theorem list_map {f : α → list β} {g : α → β → σ} (hf : primrec f) (hg : primrec₂ g) : primrec (λ a, (f a).map (g a)) := (list_foldr hf (const []) $ to₂ $ list_cons.comp (hg.comp fst (fst.comp snd)) (snd.comp snd)).of_eq $ λ a, by induction f a; simp * theorem list_range : primrec list.range := (nat_elim' primrec.id (const []) ((list_concat.comp snd fst).comp snd).to₂).of_eq $ λ n, by simp; induction n; simp [*, list.range_succ]; refl theorem list_join : primrec (@list.join α) := (list_foldr primrec.id (const []) $ to₂ $ comp (@list_append α _) snd).of_eq $ λ l, by dsimp; induction l; simp * theorem list_length : primrec (@list.length α) := (list_foldr (@primrec.id (list α) _) (const 0) $ to₂ $ (succ.comp $ snd.comp snd).to₂).of_eq $ λ l, by dsimp; induction l; simp [*, -add_comm] theorem list_find_index {f : α → list β} {p : α → β → Prop} [∀ a b, decidable (p a b)] (hf : primrec f) (hp : primrec_rel p) : primrec (λ a, (f a).find_index (p a)) := (list_foldr hf (const 0) $ to₂ $ ite (hp.comp fst $ fst.comp snd) (const 0) (succ.comp $ snd.comp snd)).of_eq $ λ a, eq.symm $ by dsimp; induction f a with b l; [refl, simp [*, list.find_index]] theorem list_index_of [decidable_eq α] : primrec₂ (@list.index_of α _) := to₂ $ list_find_index snd $ primrec.eq.comp₂ (fst.comp fst).to₂ snd.to₂ theorem nat_strong_rec (f : α → ℕ → σ) {g : α → list σ → option σ} (hg : primrec₂ g) (H : ∀ a n, g a ((list.range n).map (f a)) = some (f a n)) : primrec₂ f := suffices primrec₂ (λ a n, (list.range n).map (f a)), from primrec₂.option_some_iff.1 $ (list_nth.comp (this.comp fst (succ.comp snd)) snd).to₂.of_eq $ λ a n, by simp [list.nth_range (nat.lt_succ_self n)]; refl, primrec₂.option_some_iff.1 $ (nat_elim (const (some [])) (to₂ $ option_bind (snd.comp snd) $ to₂ $ option_map (hg.comp (fst.comp fst) snd) (to₂ $ list_concat.comp (snd.comp fst) snd))).of_eq $ λ a n, begin simp, induction n with n IH, {refl}, simp [IH, H, list.range_succ] end end primrec namespace primcodable variables {α : Type*} {β : Type*} variables [primcodable α] [primcodable β] open primrec def subtype {p : α → Prop} [decidable_pred p] (hp : primrec_pred p) : primcodable (subtype p) := ⟨have primrec (λ n, (decode α n).bind (λ a, option.guard p a)), from option_bind primrec.decode (option_guard (hp.comp snd) snd), nat_iff.1 $ (encode_iff.2 this).of_eq $ λ n, show _ = encode ((decode α n).bind (λ a, _)), begin cases decode α n with a, {refl}, dsimp [option.guard], by_cases h : p a; simp [h]; refl end⟩ instance fin {n} : primcodable (fin n) := @of_equiv _ _ (subtype $ nat_lt.comp primrec.id (const n)) (equiv.fin_equiv_subtype _) instance vector {n} : primcodable (vector α n) := subtype ((@primrec.eq _ _ nat.decidable_eq).comp list_length (const _)) instance fin_arrow {n} : primcodable (fin n → α) := of_equiv _ (equiv.vector_equiv_fin _ _).symm instance array {n} : primcodable (array n α) := of_equiv _ (equiv.array_equiv_fin _ _) section ulower local attribute [instance, priority 100] encodable.decidable_range_encode encodable.decidable_eq_of_encodable instance ulower : primcodable (ulower α) := have primrec_pred (λ n, encodable.decode2 α n ≠ none), from primrec.not (primrec.eq.comp (primrec.option_bind primrec.decode (primrec.ite (primrec.eq.comp (primrec.encode.comp primrec.snd) primrec.fst) (primrec.option_some.comp primrec.snd) (primrec.const _))) (primrec.const _)), primcodable.subtype $ primrec_pred.of_eq this $ by simp [set.range, option.eq_none_iff_forall_not_mem, encodable.mem_decode2] end ulower end primcodable namespace primrec variables {α : Type*} {β : Type*} {γ : Type*} {σ : Type*} variables [primcodable α] [primcodable β] [primcodable γ] [primcodable σ] theorem subtype_val {p : α → Prop} [decidable_pred p] {hp : primrec_pred p} : by haveI := primcodable.subtype hp; exact primrec (@subtype.val α p) := begin letI := primcodable.subtype hp, refine (primcodable.prim (subtype p)).of_eq (λ n, _), rcases decode (subtype p) n with _|⟨a,h⟩; refl end theorem subtype_val_iff {p : β → Prop} [decidable_pred p] {hp : primrec_pred p} {f : α → subtype p} : by haveI := primcodable.subtype hp; exact primrec (λ a, (f a).1) ↔ primrec f := begin letI := primcodable.subtype hp, refine ⟨λ h, _, λ hf, subtype_val.comp hf⟩, refine nat.primrec.of_eq h (λ n, _), cases decode α n with a, {refl}, simp, cases f a; refl end theorem subtype_mk {p : β → Prop} [decidable_pred p] {hp : primrec_pred p} {f : α → β} {h : ∀ a, p (f a)} (hf : primrec f) : by haveI := primcodable.subtype hp; exact primrec (λ a, @subtype.mk β p (f a) (h a)) := subtype_val_iff.1 hf theorem option_get {f : α → option β} {h : ∀ a, (f a).is_some} : primrec f → primrec (λ a, option.get (h a)) := begin intro hf, refine (nat.primrec.pred.comp hf).of_eq (λ n, _), generalize hx : decode α n = x, cases x; simp end theorem ulower_down : primrec (ulower.down : α → ulower α) := by letI : ∀ a, decidable (a ∈ set.range (encode : α → ℕ)) := decidable_range_encode _; exact subtype_mk primrec.encode theorem ulower_up : primrec (ulower.up : ulower α → α) := by letI : ∀ a, decidable (a ∈ set.range (encode : α → ℕ)) := decidable_range_encode _; exact option_get (primrec.decode2.comp subtype_val) theorem fin_val_iff {n} {f : α → fin n} : primrec (λ a, (f a).1) ↔ primrec f := begin let : primcodable {a//id a<n}, swap, exactI (iff.trans (by refl) subtype_val_iff).trans (of_equiv_iff _) end theorem fin_val {n} : primrec (coe : fin n → ℕ) := fin_val_iff.2 primrec.id theorem fin_succ {n} : primrec (@fin.succ n) := fin_val_iff.1 $ by simp [succ.comp fin_val] theorem vector_to_list {n} : primrec (@vector.to_list α n) := subtype_val theorem vector_to_list_iff {n} {f : α → vector β n} : primrec (λ a, (f a).to_list) ↔ primrec f := subtype_val_iff theorem vector_cons {n} : primrec₂ (@vector.cons α n) := vector_to_list_iff.1 $ by simp; exact list_cons.comp fst (vector_to_list_iff.2 snd) theorem vector_length {n} : primrec (@vector.length α n) := const _ theorem vector_head {n} : primrec (@vector.head α n) := option_some_iff.1 $ (list_head'.comp vector_to_list).of_eq $ λ ⟨a::l, h⟩, rfl theorem vector_tail {n} : primrec (@vector.tail α n) := vector_to_list_iff.1 $ (list_tail.comp vector_to_list).of_eq $ λ ⟨l, h⟩, by cases l; refl theorem vector_nth {n} : primrec₂ (@vector.nth α n) := option_some_iff.1 $ (list_nth.comp (vector_to_list.comp fst) (fin_val.comp snd)).of_eq $ λ a, by simp [vector.nth_eq_nth_le]; rw [← list.nth_le_nth] theorem list_of_fn : ∀ {n} {f : fin n → α → σ}, (∀ i, primrec (f i)) → primrec (λ a, list.of_fn (λ i, f i a)) | 0 f hf := const [] | (n+1) f hf := by simp [list.of_fn_succ]; exact list_cons.comp (hf 0) (list_of_fn (λ i, hf i.succ)) theorem vector_of_fn {n} {f : fin n → α → σ} (hf : ∀ i, primrec (f i)) : primrec (λ a, vector.of_fn (λ i, f i a)) := vector_to_list_iff.1 $ by simp [list_of_fn hf] theorem vector_nth' {n} : primrec (@vector.nth α n) := of_equiv_symm theorem vector_of_fn' {n} : primrec (@vector.of_fn α n) := of_equiv theorem fin_app {n} : primrec₂ (@id (fin n → σ)) := (vector_nth.comp (vector_of_fn'.comp fst) snd).of_eq $ λ ⟨v, i⟩, by simp theorem fin_curry₁ {n} {f : fin n → α → σ} : primrec₂ f ↔ ∀ i, primrec (f i) := ⟨λ h i, h.comp (const i) primrec.id, λ h, (vector_nth.comp ((vector_of_fn h).comp snd) fst).of_eq $ λ a, by simp⟩ theorem fin_curry {n} {f : α → fin n → σ} : primrec f ↔ primrec₂ f := ⟨λ h, fin_app.comp (h.comp fst) snd, λ h, (vector_nth'.comp (vector_of_fn (λ i, show primrec (λ a, f a i), from h.comp primrec.id (const i)))).of_eq $ λ a, by funext i; simp⟩ end primrec namespace nat open vector /-- An alternative inductive definition of `primrec` which does not use the pairing function on ℕ, and so has to work with n-ary functions on ℕ instead of unary functions. We prove that this is equivalent to the regular notion in `to_prim` and `of_prim`. -/ inductive primrec' : ∀ {n}, (vector ℕ n → ℕ) → Prop | zero : @primrec' 0 (λ _, 0) | succ : @primrec' 1 (λ v, succ v.head) | nth {n} (i : fin n) : primrec' (λ v, v.nth i) | comp {m n f} (g : fin n → vector ℕ m → ℕ) : primrec' f → (∀ i, primrec' (g i)) → primrec' (λ a, f (of_fn (λ i, g i a))) | prec {n f g} : @primrec' n f → @primrec' (n+2) g → primrec' (λ v : vector ℕ (n+1), v.head.elim (f v.tail) (λ y IH, g (y ::ᵥ IH ::ᵥ v.tail))) end nat namespace nat.primrec' open vector primrec nat (primrec') nat.primrec' hide ite theorem to_prim {n f} (pf : @primrec' n f) : primrec f := begin induction pf, case nat.primrec'.zero { exact const 0 }, case nat.primrec'.succ { exact primrec.succ.comp vector_head }, case nat.primrec'.nth : n i { exact vector_nth.comp primrec.id (const i) }, case nat.primrec'.comp : m n f g _ _ hf hg { exact hf.comp (vector_of_fn (λ i, hg i)) }, case nat.primrec'.prec : n f g _ _ hf hg { exact nat_elim' vector_head (hf.comp vector_tail) (hg.comp $ vector_cons.comp (fst.comp snd) $ vector_cons.comp (snd.comp snd) $ (@vector_tail _ _ (n+1)).comp fst).to₂ }, end theorem of_eq {n} {f g : vector ℕ n → ℕ} (hf : primrec' f) (H : ∀ i, f i = g i) : primrec' g := (funext H : f = g) ▸ hf theorem const {n} : ∀ m, @primrec' n (λ v, m) | 0 := zero.comp fin.elim0 (λ i, i.elim0) | (m+1) := succ.comp _ (λ i, const m) theorem head {n : ℕ} : @primrec' n.succ head := (nth 0).of_eq $ λ v, by simp [nth_zero] theorem tail {n f} (hf : @primrec' n f) : @primrec' n.succ (λ v, f v.tail) := (hf.comp _ (λ i, @nth _ i.succ)).of_eq $ λ v, by rw [← of_fn_nth v.tail]; congr; funext i; simp def vec {n m} (f : vector ℕ n → vector ℕ m) := ∀ i, primrec' (λ v, (f v).nth i) protected theorem nil {n} : @vec n 0 (λ _, nil) := λ i, i.elim0 protected theorem cons {n m f g} (hf : @primrec' n f) (hg : @vec n m g) : vec (λ v, (f v ::ᵥ g v)) := λ i, fin.cases (by simp *) (λ i, by simp [hg i]) i theorem idv {n} : @vec n n id := nth theorem comp' {n m f g} (hf : @primrec' m f) (hg : @vec n m g) : primrec' (λ v, f (g v)) := (hf.comp _ hg).of_eq $ λ v, by simp theorem comp₁ (f : ℕ → ℕ) (hf : @primrec' 1 (λ v, f v.head)) {n g} (hg : @primrec' n g) : primrec' (λ v, f (g v)) := hf.comp _ (λ i, hg) theorem comp₂ (f : ℕ → ℕ → ℕ) (hf : @primrec' 2 (λ v, f v.head v.tail.head)) {n g h} (hg : @primrec' n g) (hh : @primrec' n h) : primrec' (λ v, f (g v) (h v)) := by simpa using hf.comp' (hg.cons $ hh.cons primrec'.nil) theorem prec' {n f g h} (hf : @primrec' n f) (hg : @primrec' n g) (hh : @primrec' (n+2) h) : @primrec' n (λ v, (f v).elim (g v) (λ (y IH : ℕ), h (y ::ᵥ IH ::ᵥ v))) := by simpa using comp' (prec hg hh) (hf.cons idv) theorem pred : @primrec' 1 (λ v, v.head.pred) := (prec' head (const 0) head).of_eq $ λ v, by simp; cases v.head; refl theorem add : @primrec' 2 (λ v, v.head + v.tail.head) := (prec head (succ.comp₁ _ (tail head))).of_eq $ λ v, by simp; induction v.head; simp [*, nat.succ_add] theorem sub : @primrec' 2 (λ v, v.head - v.tail.head) := begin suffices, simpa using comp₂ (λ a b, b - a) this (tail head) head, refine (prec head (pred.comp₁ _ (tail head))).of_eq (λ v, _), simp, induction v.head; simp [*, nat.sub_succ] end theorem mul : @primrec' 2 (λ v, v.head * v.tail.head) := (prec (const 0) (tail (add.comp₂ _ (tail head) (head)))).of_eq $ λ v, by simp; induction v.head; simp [*, nat.succ_mul]; rw add_comm theorem if_lt {n a b f g} (ha : @primrec' n a) (hb : @primrec' n b) (hf : @primrec' n f) (hg : @primrec' n g) : @primrec' n (λ v, if a v < b v then f v else g v) := (prec' (sub.comp₂ _ hb ha) hg (tail $ tail hf)).of_eq $ λ v, begin cases e : b v - a v, { simp [not_lt.2 (nat.le_of_sub_eq_zero e)] }, { simp [nat.lt_of_sub_eq_succ e] } end theorem mkpair : @primrec' 2 (λ v, v.head.mkpair v.tail.head) := if_lt head (tail head) (add.comp₂ _ (tail $ mul.comp₂ _ head head) head) (add.comp₂ _ (add.comp₂ _ (mul.comp₂ _ head head) head) (tail head)) protected theorem encode : ∀ {n}, @primrec' n encode | 0 := (const 0).of_eq (λ v, by rw v.eq_nil; refl) | (n+1) := (succ.comp₁ _ (mkpair.comp₂ _ head (tail encode))) .of_eq $ λ ⟨a::l, e⟩, rfl theorem sqrt : @primrec' 1 (λ v, v.head.sqrt) := begin suffices H : ∀ n : ℕ, n.sqrt = n.elim 0 (λ x y, if x.succ < y.succ*y.succ then y else y.succ), { simp [H], have := @prec' 1 _ _ (λ v, by have x := v.head; have y := v.tail.head; from if x.succ < y.succ*y.succ then y else y.succ) head (const 0) _, { convert this, funext, congr, funext x y, congr; simp }, have x1 := succ.comp₁ _ head, have y1 := succ.comp₁ _ (tail head), exact if_lt x1 (mul.comp₂ _ y1 y1) (tail head) y1 }, intro, symmetry, induction n with n IH, {simp}, dsimp, rw IH, split_ifs, { exact le_antisymm (nat.sqrt_le_sqrt (nat.le_succ _)) (nat.lt_succ_iff.1 $ nat.sqrt_lt.2 h) }, { exact nat.eq_sqrt.2 ⟨not_lt.1 h, nat.sqrt_lt.1 $ nat.lt_succ_iff.2 $ nat.sqrt_succ_le_succ_sqrt _⟩ }, end theorem unpair₁ {n f} (hf : @primrec' n f) : @primrec' n (λ v, (f v).unpair.1) := begin have s := sqrt.comp₁ _ hf, have fss := sub.comp₂ _ hf (mul.comp₂ _ s s), refine (if_lt fss s fss s).of_eq (λ v, _), simp [nat.unpair], split_ifs; refl end theorem unpair₂ {n f} (hf : @primrec' n f) : @primrec' n (λ v, (f v).unpair.2) := begin have s := sqrt.comp₁ _ hf, have fss := sub.comp₂ _ hf (mul.comp₂ _ s s), refine (if_lt fss s s (sub.comp₂ _ fss s)).of_eq (λ v, _), simp [nat.unpair], split_ifs; refl end theorem of_prim : ∀ {n f}, primrec f → @primrec' n f := suffices ∀ f, nat.primrec f → @primrec' 1 (λ v, f v.head), from λ n f hf, (pred.comp₁ _ $ (this _ hf).comp₁ (λ m, encodable.encode $ (decode (vector ℕ n) m).map f) primrec'.encode).of_eq (λ i, by simp [encodek]), λ f hf, begin induction hf, case nat.primrec.zero { exact const 0 }, case nat.primrec.succ { exact succ }, case nat.primrec.left { exact unpair₁ head }, case nat.primrec.right { exact unpair₂ head }, case nat.primrec.pair : f g _ _ hf hg { exact mkpair.comp₂ _ hf hg }, case nat.primrec.comp : f g _ _ hf hg { exact hf.comp₁ _ hg }, case nat.primrec.prec : f g _ _ hf hg { simpa using prec' (unpair₂ head) (hf.comp₁ _ (unpair₁ head)) (hg.comp₁ _ $ mkpair.comp₂ _ (unpair₁ $ tail $ tail head) (mkpair.comp₂ _ head (tail head))) }, end theorem prim_iff {n f} : @primrec' n f ↔ primrec f := ⟨to_prim, of_prim⟩ theorem prim_iff₁ {f : ℕ → ℕ} : @primrec' 1 (λ v, f v.head) ↔ primrec f := prim_iff.trans ⟨ λ h, (h.comp $ vector_of_fn $ λ i, primrec.id).of_eq (λ v, by simp), λ h, h.comp vector_head⟩ theorem prim_iff₂ {f : ℕ → ℕ → ℕ} : @primrec' 2 (λ v, f v.head v.tail.head) ↔ primrec₂ f := prim_iff.trans ⟨ λ h, (h.comp $ vector_cons.comp fst $ vector_cons.comp snd (primrec.const nil)).of_eq (λ v, by simp), λ h, h.comp vector_head (vector_head.comp vector_tail)⟩ theorem vec_iff {m n f} : @vec m n f ↔ primrec f := ⟨λ h, by simpa using vector_of_fn (λ i, to_prim (h i)), λ h i, of_prim $ vector_nth.comp h (primrec.const i)⟩ end nat.primrec' theorem primrec.nat_sqrt : primrec nat.sqrt := nat.primrec'.prim_iff₁.1 nat.primrec'.sqrt
2d98e1f6ce91b1eaba6b3363a793c941a8279a65
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/homeomorph.lean
1435154d553072e5ebc5fce828de9071b12b3227
[ "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
21,132
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Patrick Massot, Sébastien Gouëzel, Zhouhang Zhou, Reid Barton -/ import topology.dense_embedding import data.equiv.fin /-! # Homeomorphisms This file defines homeomorphisms between two topological spaces. They are bijections with both directions continuous. We denote homeomorphisms with the notation `≃ₜ`. # Main definitions * `homeomorph α β`: The type of homeomorphisms from `α` to `β`. This type can be denoted using the following notation: `α ≃ₜ β`. # Main results * Pretty much every topological property is preserved under homeomorphisms. * `homeomorph.homeomorph_of_continuous_open`: A continuous bijection that is an open map is a homeomorphism. -/ open set filter open_locale topological_space variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} /-- Homeomorphism between `α` and `β`, also called topological isomorphism -/ @[nolint has_inhabited_instance] -- not all spaces are homeomorphic to each other structure homeomorph (α : Type*) (β : Type*) [topological_space α] [topological_space β] extends α ≃ β := (continuous_to_fun : continuous to_fun . tactic.interactive.continuity') (continuous_inv_fun : continuous inv_fun . tactic.interactive.continuity') infix ` ≃ₜ `:25 := homeomorph namespace homeomorph variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] instance : has_coe_to_fun (α ≃ₜ β) (λ _, α → β) := ⟨λe, e.to_equiv⟩ @[simp] lemma homeomorph_mk_coe (a : equiv α β) (b c) : ((homeomorph.mk a b c) : α → β) = a := rfl @[simp] lemma coe_to_equiv (h : α ≃ₜ β) : ⇑h.to_equiv = h := rfl /-- Inverse of a homeomorphism. -/ protected def symm (h : α ≃ₜ β) : β ≃ₜ α := { continuous_to_fun := h.continuous_inv_fun, continuous_inv_fun := h.continuous_to_fun, to_equiv := h.to_equiv.symm } /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def simps.apply (h : α ≃ₜ β) : α → β := h /-- See Note [custom simps projection] -/ def simps.symm_apply (h : α ≃ₜ β) : β → α := h.symm initialize_simps_projections homeomorph (to_equiv_to_fun → apply, to_equiv_inv_fun → symm_apply, -to_equiv) lemma to_equiv_injective : function.injective (to_equiv : α ≃ₜ β → α ≃ β) | ⟨e, h₁, h₂⟩ ⟨e', h₁', h₂'⟩ rfl := rfl @[ext] lemma ext {h h' : α ≃ₜ β} (H : ∀ x, h x = h' x) : h = h' := to_equiv_injective $ equiv.ext H /-- Identity map as a homeomorphism. -/ @[simps apply {fully_applied := ff}] protected def refl (α : Type*) [topological_space α] : α ≃ₜ α := { continuous_to_fun := continuous_id, continuous_inv_fun := continuous_id, to_equiv := equiv.refl α } /-- Composition of two homeomorphisms. -/ protected def trans (h₁ : α ≃ₜ β) (h₂ : β ≃ₜ γ) : α ≃ₜ γ := { continuous_to_fun := h₂.continuous_to_fun.comp h₁.continuous_to_fun, continuous_inv_fun := h₁.continuous_inv_fun.comp h₂.continuous_inv_fun, to_equiv := equiv.trans h₁.to_equiv h₂.to_equiv } @[simp] lemma trans_apply (h₁ : α ≃ₜ β) (h₂ : β ≃ₜ γ) (a : α) : h₁.trans h₂ a = h₂ (h₁ a) := rfl @[simp] lemma homeomorph_mk_coe_symm (a : equiv α β) (b c) : ((homeomorph.mk a b c).symm : β → α) = a.symm := rfl @[simp] lemma refl_symm : (homeomorph.refl α).symm = homeomorph.refl α := rfl @[continuity] protected lemma continuous (h : α ≃ₜ β) : continuous h := h.continuous_to_fun @[continuity] -- otherwise `by continuity` can't prove continuity of `h.to_equiv.symm` protected lemma continuous_symm (h : α ≃ₜ β) : continuous (h.symm) := h.continuous_inv_fun @[simp] lemma apply_symm_apply (h : α ≃ₜ β) (x : β) : h (h.symm x) = x := h.to_equiv.apply_symm_apply x @[simp] lemma symm_apply_apply (h : α ≃ₜ β) (x : α) : h.symm (h x) = x := h.to_equiv.symm_apply_apply x protected lemma bijective (h : α ≃ₜ β) : function.bijective h := h.to_equiv.bijective protected lemma injective (h : α ≃ₜ β) : function.injective h := h.to_equiv.injective protected lemma surjective (h : α ≃ₜ β) : function.surjective h := h.to_equiv.surjective /-- Change the homeomorphism `f` to make the inverse function definitionally equal to `g`. -/ def change_inv (f : α ≃ₜ β) (g : β → α) (hg : function.right_inverse g f) : α ≃ₜ β := have g = f.symm, from funext (λ x, calc g x = f.symm (f (g x)) : (f.left_inv (g x)).symm ... = f.symm x : by rw hg x), { to_fun := f, inv_fun := g, left_inv := by convert f.left_inv, right_inv := by convert f.right_inv, continuous_to_fun := f.continuous, continuous_inv_fun := by convert f.symm.continuous } @[simp] lemma symm_comp_self (h : α ≃ₜ β) : ⇑h.symm ∘ ⇑h = id := funext h.symm_apply_apply @[simp] lemma self_comp_symm (h : α ≃ₜ β) : ⇑h ∘ ⇑h.symm = id := funext h.apply_symm_apply @[simp] lemma range_coe (h : α ≃ₜ β) : range h = univ := h.surjective.range_eq lemma image_symm (h : α ≃ₜ β) : image h.symm = preimage h := funext h.symm.to_equiv.image_eq_preimage lemma preimage_symm (h : α ≃ₜ β) : preimage h.symm = image h := (funext h.to_equiv.image_eq_preimage).symm @[simp] lemma image_preimage (h : α ≃ₜ β) (s : set β) : h '' (h ⁻¹' s) = s := h.to_equiv.image_preimage s @[simp] lemma preimage_image (h : α ≃ₜ β) (s : set α) : h ⁻¹' (h '' s) = s := h.to_equiv.preimage_image s protected lemma inducing (h : α ≃ₜ β) : inducing h := inducing_of_inducing_compose h.continuous h.symm.continuous $ by simp only [symm_comp_self, inducing_id] lemma induced_eq (h : α ≃ₜ β) : topological_space.induced h ‹_› = ‹_› := h.inducing.1.symm protected lemma quotient_map (h : α ≃ₜ β) : quotient_map h := quotient_map.of_quotient_map_compose h.symm.continuous h.continuous $ by simp only [self_comp_symm, quotient_map.id] lemma coinduced_eq (h : α ≃ₜ β) : topological_space.coinduced h ‹_› = ‹_› := h.quotient_map.2.symm protected lemma embedding (h : α ≃ₜ β) : embedding h := ⟨h.inducing, h.injective⟩ /-- Homeomorphism given an embedding. -/ noncomputable def of_embedding (f : α → β) (hf : embedding f) : α ≃ₜ (set.range f) := { continuous_to_fun := continuous_subtype_mk _ hf.continuous, continuous_inv_fun := by simp [hf.continuous_iff, continuous_subtype_coe], .. equiv.of_injective f hf.inj } protected lemma second_countable_topology [topological_space.second_countable_topology β] (h : α ≃ₜ β) : topological_space.second_countable_topology α := h.inducing.second_countable_topology lemma compact_image {s : set α} (h : α ≃ₜ β) : is_compact (h '' s) ↔ is_compact s := h.embedding.is_compact_iff_is_compact_image.symm lemma compact_preimage {s : set β} (h : α ≃ₜ β) : is_compact (h ⁻¹' s) ↔ is_compact s := by rw ← image_symm; exact h.symm.compact_image protected lemma compact_space [compact_space α] (h : α ≃ₜ β) : compact_space β := { compact_univ := by { rw [← image_univ_of_surjective h.surjective, h.compact_image], apply compact_space.compact_univ } } protected lemma t2_space [t2_space α] (h : α ≃ₜ β) : t2_space β := { t2 := begin intros x y hxy, obtain ⟨u, v, hu, hv, hxu, hyv, huv⟩ := t2_separation (h.symm.injective.ne hxy), refine ⟨h.symm ⁻¹' u, h.symm ⁻¹' v, h.symm.continuous.is_open_preimage _ hu, h.symm.continuous.is_open_preimage _ hv, hxu, hyv, _⟩, rw [← preimage_inter, huv, preimage_empty], end } protected lemma dense_embedding (h : α ≃ₜ β) : dense_embedding h := { dense := h.surjective.dense_range, .. h.embedding } @[simp] lemma is_open_preimage (h : α ≃ₜ β) {s : set β} : is_open (h ⁻¹' s) ↔ is_open s := h.quotient_map.is_open_preimage @[simp] lemma is_open_image (h : α ≃ₜ β) {s : set α} : is_open (h '' s) ↔ is_open s := by rw [← preimage_symm, is_open_preimage] protected lemma is_open_map (h : α ≃ₜ β) : is_open_map h := λ s, h.is_open_image.2 @[simp] lemma is_closed_preimage (h : α ≃ₜ β) {s : set β} : is_closed (h ⁻¹' s) ↔ is_closed s := by simp only [← is_open_compl_iff, ← preimage_compl, is_open_preimage] @[simp] lemma is_closed_image (h : α ≃ₜ β) {s : set α} : is_closed (h '' s) ↔ is_closed s := by rw [← preimage_symm, is_closed_preimage] protected lemma is_closed_map (h : α ≃ₜ β) : is_closed_map h := λ s, h.is_closed_image.2 protected lemma open_embedding (h : α ≃ₜ β) : open_embedding h := open_embedding_of_embedding_open h.embedding h.is_open_map protected lemma closed_embedding (h : α ≃ₜ β) : closed_embedding h := closed_embedding_of_embedding_closed h.embedding h.is_closed_map lemma preimage_closure (h : α ≃ₜ β) (s : set β) : h ⁻¹' (closure s) = closure (h ⁻¹' s) := h.is_open_map.preimage_closure_eq_closure_preimage h.continuous _ lemma image_closure (h : α ≃ₜ β) (s : set α) : h '' (closure s) = closure (h '' s) := by rw [← preimage_symm, preimage_closure] lemma preimage_interior (h : α ≃ₜ β) (s : set β) : h⁻¹' (interior s) = interior (h ⁻¹' s) := h.is_open_map.preimage_interior_eq_interior_preimage h.continuous _ lemma image_interior (h : α ≃ₜ β) (s : set α) : h '' (interior s) = interior (h '' s) := by rw [← preimage_symm, preimage_interior] lemma preimage_frontier (h : α ≃ₜ β) (s : set β) : h ⁻¹' (frontier s) = frontier (h ⁻¹' s) := h.is_open_map.preimage_frontier_eq_frontier_preimage h.continuous _ @[simp] lemma map_nhds_eq (h : α ≃ₜ β) (x : α) : map h (𝓝 x) = 𝓝 (h x) := h.embedding.map_nhds_of_mem _ (by simp) lemma symm_map_nhds_eq (h : α ≃ₜ β) (x : α) : map h.symm (𝓝 (h x)) = 𝓝 x := by rw [h.symm.map_nhds_eq, h.symm_apply_apply] lemma nhds_eq_comap (h : α ≃ₜ β) (x : α) : 𝓝 x = comap h (𝓝 (h x)) := h.embedding.to_inducing.nhds_eq_comap x @[simp] lemma comap_nhds_eq (h : α ≃ₜ β) (y : β) : comap h (𝓝 y) = 𝓝 (h.symm y) := by rw [h.nhds_eq_comap, h.apply_symm_apply] /-- If an bijective map `e : α ≃ β` is continuous and open, then it is a homeomorphism. -/ def homeomorph_of_continuous_open (e : α ≃ β) (h₁ : continuous e) (h₂ : is_open_map e) : α ≃ₜ β := { continuous_to_fun := h₁, continuous_inv_fun := begin rw continuous_def, intros s hs, convert ← h₂ s hs using 1, apply e.image_eq_preimage end, to_equiv := e } @[simp] lemma comp_continuous_on_iff (h : α ≃ₜ β) (f : γ → α) (s : set γ) : continuous_on (h ∘ f) s ↔ continuous_on f s := h.inducing.continuous_on_iff.symm @[simp] lemma comp_continuous_iff (h : α ≃ₜ β) {f : γ → α} : continuous (h ∘ f) ↔ continuous f := h.inducing.continuous_iff.symm @[simp] lemma comp_continuous_iff' (h : α ≃ₜ β) {f : β → γ} : continuous (f ∘ h) ↔ continuous f := h.quotient_map.continuous_iff.symm lemma comp_continuous_at_iff (h : α ≃ₜ β) (f : γ → α) (x : γ) : continuous_at (h ∘ f) x ↔ continuous_at f x := h.inducing.continuous_at_iff.symm lemma comp_continuous_at_iff' (h : α ≃ₜ β) (f : β → γ) (x : α) : continuous_at (f ∘ h) x ↔ continuous_at f (h x) := h.inducing.continuous_at_iff' (by simp) lemma comp_continuous_within_at_iff (h : α ≃ₜ β) (f : γ → α) (s : set γ) (x : γ) : continuous_within_at f s x ↔ continuous_within_at (h ∘ f) s x := h.inducing.continuous_within_at_iff @[simp] lemma comp_is_open_map_iff (h : α ≃ₜ β) {f : γ → α} : is_open_map (h ∘ f) ↔ is_open_map f := begin refine ⟨_, λ hf, h.is_open_map.comp hf⟩, intros hf, rw [← function.comp.left_id f, ← h.symm_comp_self, function.comp.assoc], exact h.symm.is_open_map.comp hf, end @[simp] lemma comp_is_open_map_iff' (h : α ≃ₜ β) {f : β → γ} : is_open_map (f ∘ h) ↔ is_open_map f := begin refine ⟨_, λ hf, hf.comp h.is_open_map⟩, intros hf, rw [← function.comp.right_id f, ← h.self_comp_symm, ← function.comp.assoc], exact hf.comp h.symm.is_open_map, end /-- If two sets are equal, then they are homeomorphic. -/ def set_congr {s t : set α} (h : s = t) : s ≃ₜ t := { continuous_to_fun := continuous_subtype_mk _ continuous_subtype_val, continuous_inv_fun := continuous_subtype_mk _ continuous_subtype_val, to_equiv := equiv.set_congr h } /-- Sum of two homeomorphisms. -/ def sum_congr (h₁ : α ≃ₜ β) (h₂ : γ ≃ₜ δ) : α ⊕ γ ≃ₜ β ⊕ δ := { continuous_to_fun := begin convert continuous_sum_rec (continuous_inl.comp h₁.continuous) (continuous_inr.comp h₂.continuous), ext x, cases x; refl, end, continuous_inv_fun := begin convert continuous_sum_rec (continuous_inl.comp h₁.symm.continuous) (continuous_inr.comp h₂.symm.continuous), ext x, cases x; refl end, to_equiv := h₁.to_equiv.sum_congr h₂.to_equiv } /-- Product of two homeomorphisms. -/ def prod_congr (h₁ : α ≃ₜ β) (h₂ : γ ≃ₜ δ) : α × γ ≃ₜ β × δ := { continuous_to_fun := (h₁.continuous.comp continuous_fst).prod_mk (h₂.continuous.comp continuous_snd), continuous_inv_fun := (h₁.symm.continuous.comp continuous_fst).prod_mk (h₂.symm.continuous.comp continuous_snd), to_equiv := h₁.to_equiv.prod_congr h₂.to_equiv } @[simp] lemma prod_congr_symm (h₁ : α ≃ₜ β) (h₂ : γ ≃ₜ δ) : (h₁.prod_congr h₂).symm = h₁.symm.prod_congr h₂.symm := rfl @[simp] lemma coe_prod_congr (h₁ : α ≃ₜ β) (h₂ : γ ≃ₜ δ) : ⇑(h₁.prod_congr h₂) = prod.map h₁ h₂ := rfl section variables (α β γ) /-- `α × β` is homeomorphic to `β × α`. -/ def prod_comm : α × β ≃ₜ β × α := { continuous_to_fun := continuous_snd.prod_mk continuous_fst, continuous_inv_fun := continuous_snd.prod_mk continuous_fst, to_equiv := equiv.prod_comm α β } @[simp] lemma prod_comm_symm : (prod_comm α β).symm = prod_comm β α := rfl @[simp] lemma coe_prod_comm : ⇑(prod_comm α β) = prod.swap := rfl /-- `(α × β) × γ` is homeomorphic to `α × (β × γ)`. -/ def prod_assoc : (α × β) × γ ≃ₜ α × (β × γ) := { continuous_to_fun := (continuous_fst.comp continuous_fst).prod_mk ((continuous_snd.comp continuous_fst).prod_mk continuous_snd), continuous_inv_fun := (continuous_fst.prod_mk (continuous_fst.comp continuous_snd)).prod_mk (continuous_snd.comp continuous_snd), to_equiv := equiv.prod_assoc α β γ } /-- `α × {*}` is homeomorphic to `α`. -/ @[simps apply {fully_applied := ff}] def prod_punit : α × punit ≃ₜ α := { to_equiv := equiv.prod_punit α, continuous_to_fun := continuous_fst, continuous_inv_fun := continuous_id.prod_mk continuous_const } /-- `{*} × α` is homeomorphic to `α`. -/ def punit_prod : punit × α ≃ₜ α := (prod_comm _ _).trans (prod_punit _) @[simp] lemma coe_punit_prod : ⇑(punit_prod α) = prod.snd := rfl end /-- `ulift α` is homeomorphic to `α`. -/ def {u v} ulift {α : Type u} [topological_space α] : ulift.{v u} α ≃ₜ α := { continuous_to_fun := continuous_ulift_down, continuous_inv_fun := continuous_ulift_up, to_equiv := equiv.ulift } section distrib /-- `(α ⊕ β) × γ` is homeomorphic to `α × γ ⊕ β × γ`. -/ def sum_prod_distrib : (α ⊕ β) × γ ≃ₜ α × γ ⊕ β × γ := begin refine (homeomorph.homeomorph_of_continuous_open (equiv.sum_prod_distrib α β γ).symm _ _).symm, { convert continuous_sum_rec ((continuous_inl.comp continuous_fst).prod_mk continuous_snd) ((continuous_inr.comp continuous_fst).prod_mk continuous_snd), ext1 x, cases x; refl, }, { exact (is_open_map_sum (open_embedding_inl.prod open_embedding_id).is_open_map (open_embedding_inr.prod open_embedding_id).is_open_map) } end /-- `α × (β ⊕ γ)` is homeomorphic to `α × β ⊕ α × γ`. -/ def prod_sum_distrib : α × (β ⊕ γ) ≃ₜ α × β ⊕ α × γ := (prod_comm _ _).trans $ sum_prod_distrib.trans $ sum_congr (prod_comm _ _) (prod_comm _ _) variables {ι : Type*} {σ : ι → Type*} [Π i, topological_space (σ i)] /-- `(Σ i, σ i) × β` is homeomorphic to `Σ i, (σ i × β)`. -/ def sigma_prod_distrib : ((Σ i, σ i) × β) ≃ₜ (Σ i, (σ i × β)) := homeomorph.symm $ homeomorph_of_continuous_open (equiv.sigma_prod_distrib σ β).symm (continuous_sigma $ λ i, (continuous_sigma_mk.comp continuous_fst).prod_mk continuous_snd) (is_open_map_sigma $ λ i, (open_embedding_sigma_mk.prod open_embedding_id).is_open_map) end distrib /-- If `ι` has a unique element, then `ι → α` is homeomorphic to `α`. -/ @[simps { fully_applied := ff }] def fun_unique (ι α : Type*) [unique ι] [topological_space α] : (ι → α) ≃ₜ α := { to_equiv := equiv.fun_unique ι α, continuous_to_fun := continuous_apply _, continuous_inv_fun := continuous_pi (λ _, continuous_id) } /-- Homeomorphism between dependent functions `Π i : fin 2, α i` and `α 0 × α 1`. -/ @[simps { fully_applied := ff }] def {u} pi_fin_two (α : fin 2 → Type u) [Π i, topological_space (α i)] : (Π i, α i) ≃ₜ α 0 × α 1 := { to_equiv := pi_fin_two_equiv α, continuous_to_fun := (continuous_apply 0).prod_mk (continuous_apply 1), continuous_inv_fun := continuous_pi $ fin.forall_fin_two.2 ⟨continuous_fst, continuous_snd⟩ } /-- Homeomorphism between `α² = fin 2 → α` and `α × α`. -/ @[simps { fully_applied := ff }] def fin_two_arrow : (fin 2 → α) ≃ₜ α × α := { to_equiv := fin_two_arrow_equiv α, .. pi_fin_two (λ _, α) } /-- A subset of a topological space is homeomorphic to its image under a homeomorphism. -/ def image (e : α ≃ₜ β) (s : set α) : s ≃ₜ e '' s := { continuous_to_fun := by continuity!, continuous_inv_fun := by continuity!, ..e.to_equiv.image s, } end homeomorph namespace continuous variables [topological_space α] [topological_space β] lemma continuous_symm_of_equiv_compact_to_t2 [compact_space α] [t2_space β] {f : α ≃ β} (hf : continuous f) : continuous f.symm := begin rw continuous_iff_is_closed, intros C hC, have hC' : is_closed (f '' C) := (hC.is_compact.image hf).is_closed, rwa equiv.image_eq_preimage at hC', end /-- Continuous equivalences from a compact space to a T2 space are homeomorphisms. This is not true when T2 is weakened to T1 (see `continuous.homeo_of_equiv_compact_to_t2.t1_counterexample`). -/ @[simps] def homeo_of_equiv_compact_to_t2 [compact_space α] [t2_space β] {f : α ≃ β} (hf : continuous f) : α ≃ₜ β := { continuous_to_fun := hf, continuous_inv_fun := hf.continuous_symm_of_equiv_compact_to_t2, ..f } /-- A concrete counterexample shows that `continuous.homeo_of_equiv_compact_to_t2` cannot be generalized from `t2_space` to `t1_space`. Let `α = ℕ` be the one-point compactification of `{1, 2, ...}` with the discrete topology, where `0` is the adjoined point, and let `β = ℕ` be given the cofinite topology. Then `α` is compact, `β` is T1, and the identity map `id : α → β` is a continuous equivalence that is not a homeomorphism. -/ lemma homeo_of_equiv_compact_to_t2.t1_counterexample : ∃ (α β : Type) (Iα : topological_space α) (Iβ : topological_space β), by exactI compact_space α ∧ t1_space β ∧ ∃ f : α ≃ β, continuous f ∧ ¬ continuous f.symm := begin /- In the `nhds_adjoint 0 filter.cofinite` topology, a set is open if (1) 0 is not in the set or (2) 0 is in the set and the set is cofinite. This coincides with the one-point compactification of {1, 2, ...} with the discrete topology. -/ let topα : topological_space ℕ := nhds_adjoint 0 filter.cofinite, let topβ : topological_space ℕ := cofinite_topology ℕ, refine ⟨ℕ, ℕ, topα, topβ, _, t1_space_cofinite, equiv.refl ℕ, _, _⟩, { fsplit, rw is_compact_iff_ultrafilter_le_nhds, intros f, suffices : ∃ a, ↑f ≤ @nhds _ topα a, by simpa, by_cases hf : ↑f ≤ @nhds _ topα 0, { exact ⟨0, hf⟩ }, { obtain ⟨U, h0U, hU_fin, hUf⟩ : ∃ U : set ℕ, 0 ∈ U ∧ Uᶜ.finite ∧ Uᶜ ∈ f, { rw [nhds_adjoint_nhds, filter.le_def] at hf, push_neg at hf, simpa [and_assoc, ← ultrafilter.compl_mem_iff_not_mem] using hf }, obtain ⟨n, hn', hn⟩ := ultrafilter.eq_principal_of_finite_mem hU_fin hUf, rw hn, exact ⟨n, @mem_of_mem_nhds _ topα n⟩ } }, { rw continuous_iff_coinduced_le, change topα ≤ topβ, rw gc_nhds, simp [nhds_cofinite] }, { intros h, replace h : topβ ≤ topα := by simpa [continuous_iff_coinduced_le, coinduced_id] using h, rw le_nhds_adjoint_iff at h, exact (finite_singleton 1).infinite_compl (h.2 1 one_ne_zero ⟨1, mem_singleton 1⟩) } end end continuous
d5823983daead61640277ebe469d140357ebf242
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/category/Group/limits.lean
461221bd979000f71ab0ede5b999d47f651ac036
[ "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
12,580
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 algebra.category.Mon.limits import algebra.category.Group.preadditive import category_theory.over import category_theory.limits.concrete_category import category_theory.limits.shapes.concrete_category import group_theory.subgroup.basic /-! # The category of (commutative) (additive) groups 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 universes v u noncomputable theory variables {J : Type v} [small_category J] namespace Group @[to_additive] instance group_obj (F : J ⥤ Group.{max v u}) (j) : group ((F ⋙ forget Group).obj j) := by { change group (F.obj j), apply_instance } /-- The flat sections of a functor into `Group` form a subgroup of all sections. -/ @[to_additive "The flat sections of a functor into `AddGroup` form an additive subgroup of all sections."] def sections_subgroup (F : J ⥤ Group) : subgroup (Π j, F.obj j) := { carrier := (F ⋙ forget Group).sections, inv_mem' := λ a ah j j' f, begin simp only [forget_map_eq_coe, functor.comp_map, pi.inv_apply, monoid_hom.map_inv, inv_inj], dsimp [functor.sections] at ah, rw ah f, end, ..(Mon.sections_submonoid (F ⋙ forget₂ Group Mon)) } @[to_additive] instance limit_group (F : J ⥤ Group.{max v u}) : group (types.limit_cone (F ⋙ forget Group)).X := begin change group (sections_subgroup F), apply_instance, end /-- We show that the forgetful functor `Group ⥤ Mon` creates limits. All we need to do is notice that the limit point has a `group` instance available, and then reuse the existing limit. -/ @[to_additive "We show that the forgetful functor `AddGroup ⥤ AddMon` creates limits. All we need to do is notice that the limit point has an `add_group` instance available, and then reuse the existing limit."] instance forget₂.creates_limit (F : J ⥤ Group.{max v u}) : creates_limit F (forget₂ Group.{max v u} Mon.{max v u}) := creates_limit_of_reflects_iso (λ c' t, { lifted_cone := { X := Group.of (types.limit_cone (F ⋙ forget Group)).X, π := { app := Mon.limit_π_monoid_hom (F ⋙ forget₂ Group Mon.{max v u}), naturality' := (Mon.has_limits.limit_cone (F ⋙ forget₂ Group Mon.{max v u})).π.naturality, } }, valid_lift := by apply is_limit.unique_up_to_iso (Mon.has_limits.limit_cone_is_limit _) t, makes_limit := is_limit.of_faithful (forget₂ Group Mon.{max v u}) (Mon.has_limits.limit_cone_is_limit _) (λ s, _) (λ s, rfl) }) /-- A choice of limit cone for a functor into `Group`. (Generally, you'll just want to use `limit F`.) -/ @[to_additive "A choice of limit cone for a functor into `Group`. (Generally, you'll just want to use `limit F`.)"] def limit_cone (F : J ⥤ Group.{max v u}) : cone F := lift_limit (limit.is_limit (F ⋙ (forget₂ Group Mon.{max v u}))) /-- The chosen cone is a limit cone. (Generally, you'll just want to use `limit.cone F`.) -/ @[to_additive "The chosen cone is a limit cone. (Generally, you'll just want to use `limit.cone F`.)"] def limit_cone_is_limit (F : J ⥤ Group.{max v u}) : is_limit (limit_cone F) := lifted_limit_is_limit _ /-- The category of groups has all limits. -/ @[to_additive "The category of additive groups has all limits."] instance has_limits_of_size : has_limits_of_size.{v v} Group.{max v u} := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, has_limit_of_created F (forget₂ Group Mon.{max v u}) } } @[to_additive] instance has_limits : has_limits Group.{u} := Group.has_limits_of_size.{u u} /-- The forgetful functor from groups to monoids preserves all limits. This means the underlying monoid of a limit can be computed as a limit in the category of monoids. -/ @[to_additive AddGroup.forget₂_AddMon_preserves_limits "The forgetful functor from additive groups to additive monoids preserves all limits. This means the underlying additive monoid of a limit can be computed as a limit in the category of additive monoids."] instance forget₂_Mon_preserves_limits_of_size : preserves_limits_of_size.{v v} (forget₂ Group Mon.{max v u}) := { preserves_limits_of_shape := λ J 𝒥, { preserves_limit := λ F, by apply_instance } } @[to_additive] instance forget₂_Mon_preserves_limits : preserves_limits (forget₂ Group Mon.{u}) := Group.forget₂_Mon_preserves_limits_of_size.{u u} /-- The forgetful functor from groups to types preserves all limits. This means the underlying type of a limit can be computed as a limit in the category of types. -/ @[to_additive "The forgetful functor from additive groups to types preserves all limits. This means the underlying type of a limit can be computed as a limit in the category of types."] instance forget_preserves_limits_of_size : preserves_limits_of_size.{v v} (forget Group.{max v u}) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, limits.comp_preserves_limit (forget₂ Group Mon) (forget Mon) } } @[to_additive] instance forget_preserves_limits : preserves_limits (forget Group.{u}) := Group.forget_preserves_limits_of_size.{u u} end Group namespace CommGroup @[to_additive] instance comm_group_obj (F : J ⥤ CommGroup.{max v u}) (j) : comm_group ((F ⋙ forget CommGroup).obj j) := by { change comm_group (F.obj j), apply_instance } @[to_additive] instance limit_comm_group (F : J ⥤ CommGroup.{max v u}) : comm_group (types.limit_cone (F ⋙ forget CommGroup.{max v u})).X := @subgroup.to_comm_group (Π j, F.obj j) _ (Group.sections_subgroup (F ⋙ forget₂ CommGroup Group.{max v u})) /-- We show that the forgetful functor `CommGroup ⥤ Group` creates limits. All we need to do is notice that the limit point has a `comm_group` instance available, and then reuse the existing limit. -/ @[to_additive "We show that the forgetful functor `AddCommGroup ⥤ AddGroup` creates limits. All we need to do is notice that the limit point has an `add_comm_group` instance available, and then reuse the existing limit."] instance forget₂.creates_limit (F : J ⥤ CommGroup.{max v u}) : creates_limit F (forget₂ CommGroup Group.{max v u}) := creates_limit_of_reflects_iso (λ c' t, { lifted_cone := { X := CommGroup.of (types.limit_cone (F ⋙ forget CommGroup)).X, π := { app := Mon.limit_π_monoid_hom (F ⋙ forget₂ CommGroup Group.{max v u} ⋙ forget₂ Group Mon.{max v u}), naturality' := (Mon.has_limits.limit_cone _).π.naturality, } }, valid_lift := by apply is_limit.unique_up_to_iso (Group.limit_cone_is_limit _) t, makes_limit := is_limit.of_faithful (forget₂ _ Group.{max v u} ⋙ forget₂ _ Mon.{max v u}) (by apply Mon.has_limits.limit_cone_is_limit _) (λ s, _) (λ s, rfl) }) /-- A choice of limit cone for a functor into `CommGroup`. (Generally, you'll just want to use `limit F`.) -/ @[to_additive "A choice of limit cone for a functor into `CommGroup`. (Generally, you'll just want to use `limit F`.)"] def limit_cone (F : J ⥤ CommGroup.{max v u}) : cone F := lift_limit (limit.is_limit (F ⋙ (forget₂ CommGroup Group.{max v u}))) /-- The chosen cone is a limit cone. (Generally, you'll just want to use `limit.cone F`.) -/ @[to_additive "The chosen cone is a limit cone. (Generally, you'll just wantto use `limit.cone F`.)"] def limit_cone_is_limit (F : J ⥤ CommGroup.{max v u}) : is_limit (limit_cone F) := lifted_limit_is_limit _ /-- The category of commutative groups has all limits. -/ @[to_additive "The category of additive commutative groups has all limits."] instance has_limits_of_size : has_limits_of_size.{v v} CommGroup.{max v u} := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, has_limit_of_created F (forget₂ CommGroup Group.{max v u}) } } @[to_additive] instance has_limits : has_limits CommGroup.{u} := CommGroup.has_limits_of_size.{u u} /-- The forgetful functor from commutative groups to groups preserves all limits. (That is, the underlying group could have been computed instead as limits in the category of groups.) -/ @[to_additive AddCommGroup.forget₂_AddGroup_preserves_limits "The forgetful functor from additive commutative groups to groups preserves all limits. (That is, the underlying group could have been computed instead as limits in the category of additive groups.)"] instance forget₂_Group_preserves_limits_of_size : preserves_limits_of_size.{v v} (forget₂ CommGroup Group.{max v u}) := { preserves_limits_of_shape := λ J 𝒥, { preserves_limit := λ F, by apply_instance } } @[to_additive] instance forget₂_Group_preserves_limits : preserves_limits (forget₂ CommGroup Group.{u}) := CommGroup.forget₂_Group_preserves_limits_of_size.{u u} /-- An auxiliary declaration to speed up typechecking. -/ @[to_additive AddCommGroup.forget₂_AddCommMon_preserves_limits_aux "An auxiliary declaration to speed up typechecking."] def forget₂_CommMon_preserves_limits_aux (F : J ⥤ CommGroup.{max v u}) : is_limit ((forget₂ CommGroup CommMon).map_cone (limit_cone F)) := CommMon.limit_cone_is_limit (F ⋙ forget₂ CommGroup CommMon) /-- The forgetful functor from commutative groups to commutative monoids preserves all limits. (That is, the underlying commutative monoids could have been computed instead as limits in the category of commutative monoids.) -/ @[to_additive AddCommGroup.forget₂_AddCommMon_preserves_limits "The forgetful functor from additive commutative groups to additive commutative monoids preserves all limits. (That is, the underlying additive commutative monoids could have been computed instead as limits in the category of additive commutative monoids.)"] instance forget₂_CommMon_preserves_limits_of_size : preserves_limits_of_size.{v v} (forget₂ CommGroup CommMon.{max v u}) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, preserves_limit_of_preserves_limit_cone (limit_cone_is_limit F) (forget₂_CommMon_preserves_limits_aux F) } } /-- The forgetful functor from commutative groups to types preserves all limits. (That is, the underlying types could have been computed instead as limits in the category of types.) -/ @[to_additive AddCommGroup.forget_preserves_limits "The forgetful functor from additive commutative groups 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_of_size : preserves_limits_of_size.{v v} (forget CommGroup.{max v u}) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, limits.comp_preserves_limit (forget₂ CommGroup Group) (forget Group) } } -- Verify we can form limits indexed over smaller categories. example (f : ℕ → AddCommGroup) : has_product f := by apply_instance end CommGroup namespace AddCommGroup /-- The categorical kernel of a morphism in `AddCommGroup` agrees with the usual group-theoretical kernel. -/ def kernel_iso_ker {G H : AddCommGroup.{u}} (f : G ⟶ H) : kernel f ≅ AddCommGroup.of f.ker := { hom := { to_fun := λ g, ⟨kernel.ι f g, begin -- TODO where is this `has_coe_t_aux.coe` coming from? can we prevent it appearing? change (kernel.ι f) g ∈ f.ker, simp [add_monoid_hom.mem_ker], end⟩, map_zero' := by { ext, simp, }, map_add' := λ g g', by { ext, simp, }, }, inv := kernel.lift f (add_subgroup.subtype f.ker) (by tidy), hom_inv_id' := by { apply equalizer.hom_ext _, ext, simp, }, inv_hom_id' := begin apply AddCommGroup.ext, simp only [add_monoid_hom.coe_mk, coe_id, coe_comp], rintro ⟨x, mem⟩, simp, end, }. @[simp] lemma kernel_iso_ker_hom_comp_subtype {G H : AddCommGroup} (f : G ⟶ H) : (kernel_iso_ker f).hom ≫ add_subgroup.subtype f.ker = kernel.ι f := by ext; refl @[simp] lemma kernel_iso_ker_inv_comp_ι {G H : AddCommGroup} (f : G ⟶ H) : (kernel_iso_ker f).inv ≫ kernel.ι f = add_subgroup.subtype f.ker := begin ext, simp [kernel_iso_ker], end /-- The categorical kernel inclusion for `f : G ⟶ H`, as an object over `G`, agrees with the `subtype` map. -/ @[simps] def kernel_iso_ker_over {G H : AddCommGroup.{u}} (f : G ⟶ H) : over.mk (kernel.ι f) ≅ @over.mk _ _ G (AddCommGroup.of f.ker) (add_subgroup.subtype f.ker) := over.iso_mk (kernel_iso_ker f) (by simp) end AddCommGroup
dad96017847c5f977d94ec6ce9603de8282f16af
4727251e0cd73359b15b664c3170e5d754078599
/src/linear_algebra/matrix/charpoly/basic.lean
9527ee1bc89f7f41048239a4d28196394d4a60af
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
4,377
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 linear_algebra.matrix.adjugate import ring_theory.matrix_algebra import ring_theory.polynomial_algebra import tactic.apply_fun import tactic.squeeze /-! # Characteristic polynomials and the Cayley-Hamilton theorem We define characteristic polynomials of matrices and prove the Cayley–Hamilton theorem over arbitrary commutative rings. See the file `matrix/charpoly/coeff` for corollaries of this theorem. ## Main definitions * `matrix.charpoly` is the characteristic polynomial of a matrix. ## Implementation details We follow a nice proof from http://drorbn.net/AcademicPensieve/2015-12/CayleyHamilton.pdf -/ noncomputable theory universes u v w open polynomial matrix open_locale big_operators polynomial variables {R : Type u} [comm_ring R] variables {n : Type w} [decidable_eq n] [fintype n] open finset /-- The "characteristic matrix" of `M : matrix n n R` is the matrix of polynomials $t I - M$. The determinant of this matrix is the characteristic polynomial. -/ def charmatrix (M : matrix n n R) : matrix n n R[X] := matrix.scalar n (X : R[X]) - (C : R →+* R[X]).map_matrix M @[simp] lemma charmatrix_apply_eq (M : matrix n n R) (i : n) : charmatrix M i i = (X : R[X]) - C (M i i) := by simp only [charmatrix, sub_left_inj, pi.sub_apply, scalar_apply_eq, ring_hom.map_matrix_apply, map_apply, dmatrix.sub_apply] @[simp] lemma charmatrix_apply_ne (M : matrix n n R) (i j : n) (h : i ≠ j) : charmatrix M i j = - C (M i j) := by simp only [charmatrix, pi.sub_apply, scalar_apply_ne _ _ _ h, zero_sub, ring_hom.map_matrix_apply, map_apply, dmatrix.sub_apply] lemma mat_poly_equiv_charmatrix (M : matrix n n R) : mat_poly_equiv (charmatrix M) = X - C M := begin ext k i j, simp only [mat_poly_equiv_coeff_apply, coeff_sub, pi.sub_apply], by_cases h : i = j, { subst h, rw [charmatrix_apply_eq, coeff_sub], simp only [coeff_X, coeff_C], split_ifs; simp, }, { rw [charmatrix_apply_ne _ _ _ h, coeff_X, coeff_neg, coeff_C, coeff_C], split_ifs; simp [h], } end lemma charmatrix_reindex {m : Type v} [decidable_eq m] [fintype m] (e : n ≃ m) (M : matrix n n R) : charmatrix (reindex e e M) = reindex e e (charmatrix M) := begin ext i j x, by_cases h : i = j, all_goals { simp [h] } end /-- The characteristic polynomial of a matrix `M` is given by $\det (t I - M)$. -/ def matrix.charpoly (M : matrix n n R) : R[X] := (charmatrix M).det lemma matrix.charpoly_reindex {m : Type v} [decidable_eq m] [fintype m] (e : n ≃ m) (M : matrix n n R) : (reindex e e M).charpoly = M.charpoly := begin unfold matrix.charpoly, rw [charmatrix_reindex, matrix.det_reindex_self] end /-- The **Cayley-Hamilton Theorem**, that the characteristic polynomial of a matrix, applied to the matrix itself, is zero. This holds over any commutative ring. See `linear_map.aeval_self_charpoly` for the equivalent statement about endomorphisms. -/ -- This proof follows http://drorbn.net/AcademicPensieve/2015-12/CayleyHamilton.pdf theorem matrix.aeval_self_charpoly (M : matrix n n R) : aeval M M.charpoly = 0 := begin -- We begin with the fact $χ_M(t) I = adjugate (t I - M) * (t I - M)$, -- as an identity in `matrix n n R[X]`. have h : M.charpoly • (1 : matrix n n R[X]) = adjugate (charmatrix M) * (charmatrix M) := (adjugate_mul _).symm, -- Using the algebra isomorphism `matrix n n R[X] ≃ₐ[R] polynomial (matrix n n R)`, -- we have the same identity in `polynomial (matrix n n R)`. apply_fun mat_poly_equiv at h, simp only [mat_poly_equiv.map_mul, mat_poly_equiv_charmatrix] at h, -- Because the coefficient ring `matrix n n R` is non-commutative, -- evaluation at `M` is not multiplicative. -- However, any polynomial which is a product of the form $N * (t I - M)$ -- is sent to zero, because the evaluation function puts the polynomial variable -- to the right of any coefficients, so everything telescopes. apply_fun (λ p, p.eval M) at h, rw eval_mul_X_sub_C at h, -- Now $χ_M (t) I$, when thought of as a polynomial of matrices -- and evaluated at some `N` is exactly $χ_M (N)$. rw [mat_poly_equiv_smul_one, eval_map] at h, -- Thus we have $χ_M(M) = 0$, which is the desired result. exact h, end
2623ab70fc64940a84538a4b8bacf42194a1dff9
a45212b1526d532e6e83c44ddca6a05795113ddc
/docs/tutorial/Zmod37.lean
b5ffcf256b46f5ca0bc5b0929b465af2fd024d33
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
8,102
lean
/- Integers mod 37 A demonstration of how to use equivalence relations and equivalence classes in Lean. We define the "congruent mod 37" relation on integers, prove it is an equivalence relation, define Zmod37 to be the equivalence classes, and put a ring structure on the quotient. -/ -- this import is helpful for some intermediate calculations import tactic.ring -- Definition of the equivalence relation definition cong_mod37 (a b : ℤ) : Prop := ∃ (k : ℤ), k * 37 = b - a -- Now check it's an equivalence reln! theorem cong_mod_refl : reflexive (cong_mod37) := begin intro x, -- to prove cong_mod37 x x we just observe that k = 0 will do. use (0 : ℤ), -- this is k simp, end theorem cong_mod_symm : symmetric (cong_mod37) := begin intros a b H, -- H : cond_mod37 a b cases H with l Hl, -- Hl : l * 37 = (b - a) -- Goal is to find an integer k with k * 37 = a - b use -l, simp [Hl], end theorem cong_mod_trans : transitive (cong_mod37) := begin intros a b c Hab Hbc, cases Hab with l Hl, cases Hbc with m Hm, -- Hl : l * 37 = b - a, and Hm : m * 37 = c - b -- Goal : ∃ k, k * 37 = c - a use (l + m), simp [add_mul, Hl, Hm], end -- so we've now seen a general technique for proving a ≈ b -- use (the k that works) theorem cong_mod_equiv : equivalence (cong_mod37) := ⟨cong_mod_refl, cong_mod_symm, cong_mod_trans⟩ -- Now let's put an equivalence relation on ℤ definition Zmod37.setoid : setoid ℤ := { r := cong_mod37, iseqv := cong_mod_equiv } -- Tell the type class inference system about this equivalence relation. local attribute [instance] Zmod37.setoid -- Now we can make the quotient. definition Zmod37 := quotient (Zmod37.setoid) -- now a little bit of basic interface namespace Zmod37 -- Let's give a name to the reduction mod 37 map. definition reduce_mod37 : ℤ → Zmod37 := quot.mk (cong_mod37) -- Let's now set up a coercion. definition coe_int_Zmod37 : has_coe ℤ (Zmod37) := ⟨reduce_mod37⟩ -- Let's tell Lean that given an integer, it can consider it as -- an integer mod 37 automatically. local attribute [instance] coe_int_Zmod37 -- Notation for 0 and 1 instance : has_zero (Zmod37) := ⟨reduce_mod37 0⟩ instance : has_one (Zmod37) := ⟨reduce_mod37 1⟩ -- Add basic facts about 0 and 1 to the set of simp facts @[simp] theorem of_int_zero : (0 : (Zmod37)) = reduce_mod37 0 := rfl @[simp] theorem of_int_one : (1 : (Zmod37)) = reduce_mod37 1 := rfl -- now back to the maths -- here's a useful lemma -- it's needed to prove addition is well-defined on the quotient. -- Note the use of quotient.sound to get from Zmod37 back to Z lemma congr_add (a₁ a₂ b₁ b₂ : ℤ) : a₁ ≈ b₁ → a₂ ≈ b₂ → ⟦a₁ + a₂⟧ = ⟦b₁ + b₂⟧ := begin intros H1 H2, cases H1 with m Hm, -- Hm : m * 37 = b₁ - a₁ cases H2 with n Hn, -- Hn : n * 37 = b₂ - a₂ -- goal is ⟦a₁ + a₂⟧ = ⟦b₁ + b₂⟧ apply quotient.sound, -- goal now a₁ + a₂ ≈ b₁ + b₂, and we know how to do these. use (m + n), simp [add_mul, Hm, Hn] end -- That lemma above is *exactly* what we need to make sure addition is -- well-defined on Zmod37, so let's do this now, using quotient.lift -- note: stuff like "add" is used everywhere so it's best to protect. protected definition add : Zmod37 → Zmod37 → Zmod37 := quotient.lift₂ (λ a b : ℤ, ⟦a + b⟧) (begin show ∀ (a₁ a₂ b₁ b₂ : ℤ), a₁ ≈ b₁ → a₂ ≈ b₂ → ⟦a₁ + a₂⟧ = ⟦b₁ + b₂⟧, -- that's what quotient.lift₂ reduces us to doing. But we did it already! exact congr_add, end) -- Now here's the lemma we need for the definition of neg -- I spelt out the proof for add, here's a quick term proof for neg. lemma congr_neg (a b : ℤ) : a ≈ b → ⟦-a⟧ = ⟦-b⟧ := λ ⟨m, Hm⟩, quotient.sound ⟨-m, by simp [Hm]⟩ protected def neg : Zmod37 → Zmod37 := quotient.lift (λ a : ℤ, ⟦-a⟧) congr_neg -- For multiplication I won't even bother proving the lemma, I'll just let ring do it protected def mul : Zmod37 → Zmod37 → Zmod37 := quotient.lift₂ (λ a b : ℤ, ⟦a * b⟧) (λ a₁ a₂ b₁ b₂ ⟨m₁, H₁⟩ ⟨m₂, H₂⟩, quotient.sound ⟨b₁ * m₂ + a₂ * m₁, by rw [add_mul, mul_assoc, mul_assoc, H₁, H₂]; ring⟩) -- this adds notation to the quotient instance : has_add (Zmod37) := ⟨Zmod37.add⟩ instance : has_neg (Zmod37) := ⟨Zmod37.neg⟩ instance : has_mul (Zmod37) := ⟨Zmod37.mul⟩ -- these are now very cool proofs: @[simp] lemma coe_add {a b : ℤ} : (↑(a + b) : Zmod37) = ↑a + ↑b := rfl @[simp] lemma coe_neg {a : ℤ} : (↑(-a) : Zmod37) = -↑a := rfl @[simp] lemma coe_mul {a b : ℤ} : (↑(a * b) : Zmod37) = ↑a * ↑b := rfl -- Note that the proof of these results is `rfl`. If we had defined addition -- on the quotient in the standard way that mathematicians do, -- by choosing representatives and then adding them, -- then the proof would not be rfl. This is the power of quotient.lift. -- Now here's how to use quotient.induction_on and quotient.sound instance : add_comm_group (Zmod37) := { add_comm_group . zero := 0, -- because we already defined has_zero add := (+), -- could also have written has_add.add or Zmod37.add neg := has_neg.neg, zero_add := λ abar, quotient.induction_on abar (begin -- goal is ∀ (a : ℤ), 0 + ⟦a⟧ = ⟦a⟧ -- that's what quotient.induction_on does for us intro a, apply quotient.sound, -- works because 0 + ⟦a⟧ is by definition ⟦0⟧ + ⟦a⟧ which -- is by definition ⟦0 + a⟧ -- goal is now 0 + a ≈ a -- here's the way we used to do it. use (0 : ℤ), simp, -- but there are tricks now, which I'll show you with add_zero and add_assoc. end), add_assoc := λ abar bbar cbar,quotient.induction_on₃ abar bbar cbar (λ a b c, begin -- goal now ⟦a⟧ + ⟦b⟧ + ⟦c⟧ = ⟦a⟧ + (⟦b⟧ + ⟦c⟧) apply quotient.sound, -- goal now a + b + c ≈ a + (b + c) rw add_assoc, -- done :-) because after a rw a goal is closed if it's of the form x ≈ x, -- as ≈ is known by Lean to be reflexive. end), add_zero := -- I will intrroduce some more sneaky stuff now now -- add_zero for Zmod37 follows from add_zero on Z. -- Note use of $ instead of the brackets λ abar, quotient.induction_on abar $ λ a, quotient.sound $ by rw add_zero, -- that's it! Term mode proof. add_left_neg := -- super-slow method not even using quotient.induction_on begin intro abar, cases (quot.exists_rep abar) with a Ha, rw [←Ha], apply quot.sound, use (0 : ℤ), simp, end, -- but really all proofs should just look something like this add_comm := λ abar bbar, quotient.induction_on₂ abar bbar $ λ _ _,quotient.sound $ by rw add_comm, -- the noise at the beginning is just the machine; all the work is done by the rewrite } -- Now let's just nail this using all the tricks in the book. All ring axioms on the quotient -- follow from the corresponding axioms for Z. instance : comm_ring (Zmod37) := { mul := Zmod37.mul, -- could have written (*) -- Now look how the proof of mul_assoc is just the same structure as add_comm above -- but with three variables not two mul_assoc := λ a b c, quotient.induction_on₃ a b c $ λ _ _ _, quotient.sound $ by rw mul_assoc, one := 1, one_mul := λ a, quotient.induction_on a $ λ _, quotient.sound $ by rw one_mul, mul_one := λ a, quotient.induction_on a $ λ _, quotient.sound $ by rw mul_one, left_distrib := λ a b c, quotient.induction_on₃ a b c $ λ _ _ _, quotient.sound $ by rw left_distrib, right_distrib := λ a b c, quotient.induction_on₃ a b c $ λ _ _ _, quotient.sound $ by rw right_distrib, mul_comm := λ a b, quotient.induction_on₂ a b $ λ _ _, quotient.sound $ by rw mul_comm, ..Zmod37.add_comm_group } end Zmod37
c23d83a7bb5ae5587b14462e8932a200ed9d2ea0
02fbe05a45fda5abde7583464416db4366eedfbf
/library/init/data/setoid.lean
235b2619d3d75dccf0ba979f416b428390c576c7
[ "Apache-2.0" ]
permissive
jasonrute/lean
cc12807e11f9ac6b01b8951a8bfb9c2eb35a0154
4be962c167ca442a0ec5e84472d7ff9f5302788f
refs/heads/master
1,672,036,664,637
1,601,642,826,000
1,601,642,826,000
260,777,966
0
0
Apache-2.0
1,588,454,819,000
1,588,454,818,000
null
UTF-8
Lean
false
false
840
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 -/ prelude import init.logic universes u class setoid (α : Sort u) := (r : α → α → Prop) (iseqv : equivalence r) instance setoid_has_equiv {α : Sort u} [setoid α] : has_equiv α := ⟨setoid.r⟩ namespace setoid variables {α : Sort u} [setoid α] @[refl] lemma refl (a : α) : a ≈ a := match setoid.iseqv with | ⟨h_refl, h_symm, h_trans⟩ := h_refl a end @[symm] lemma symm {a b : α} (hab : a ≈ b) : b ≈ a := match setoid.iseqv with | ⟨h_refl, h_symm, h_trans⟩ := h_symm hab end @[trans] lemma trans {a b c : α} (hab : a ≈ b) (hbc : b ≈ c) : a ≈ c := match setoid.iseqv with | ⟨h_refl, h_symm, h_trans⟩ := h_trans hab hbc end end setoid
029bc9f8d012056667f544ad8c9e3efb56b99733
9cba98daa30c0804090f963f9024147a50292fa0
/old/src/test/phys_test_1.lean
b165cfa71967d5c53e8acd0e149767e740d72b04
[]
no_license
kevinsullivan/phys
dcb192f7b3033797541b980f0b4a7e75d84cea1a
ebc2df3779d3605ff7a9b47eeda25c2a551e011f
refs/heads/master
1,637,490,575,500
1,629,899,064,000
1,629,899,064,000
168,012,884
0
3
null
1,629,644,436,000
1,548,699,832,000
Lean
UTF-8
Lean
false
false
348
lean
import ..physlib /- worldTime = ClassicalTime() si = SI() timeFrame = worldTime.stdFrame() with si timePoint = Point(worldTime,stdFrame,<10>, si_measurement_system) timeVector = Vector(worldTime, stdFrame,<60>, si_measurement_system) newTime = timeFrame(worldTime, timePoint, <timeVector>) -/ def world_time : _ := _ -- fill in the rest --
ef7b2719e0ff2f66cd7877424d13ef7670acaa6e
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/control/monad/cont.lean
752eec2e83cfef35afdad309679fefe91a8fa62b
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
9,445
lean
/- Copyright (c) 2019 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Simon Hudon Monad encapsulating continuation passing programming style, similar to Haskell's `Cont`, `ContT` and `MonadCont`: <http://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-Cont.html> -/ import control.monad.writer universes u v w u₀ u₁ v₀ v₁ structure monad_cont.label (α : Type w) (m : Type u → Type v) (β : Type u) := (apply : α → m β) def monad_cont.goto {α β} {m : Type u → Type v} (f : monad_cont.label α m β) (x : α) := f.apply x class monad_cont (m : Type u → Type v) := (call_cc : Π {α β}, ((monad_cont.label α m β) → m α) → m α) open monad_cont section prio set_option default_priority 100 -- see Note [default priority] class is_lawful_monad_cont (m : Type u → Type v) [monad m] [monad_cont m] extends is_lawful_monad m := (call_cc_bind_right {α ω γ} (cmd : m α) (next : (label ω m γ) → α → m ω) : call_cc (λ f, cmd >>= next f) = cmd >>= λ x, call_cc (λ f, next f x)) (call_cc_bind_left {α} (β) (x : α) (dead : label α m β → β → m α) : call_cc (λ f : label α m β, goto f x >>= dead f) = pure x) (call_cc_dummy {α β} (dummy : m α) : call_cc (λ f : label α m β, dummy) = dummy) end prio export is_lawful_monad_cont def cont_t (r : Type u) (m : Type u → Type v) (α : Type w) := (α → m r) → m r @[reducible] def cont (r : Type u) (α : Type w) := cont_t r id α namespace cont_t export monad_cont (label goto) variables {r : Type u} {m : Type u → Type v} {α β γ ω : Type w} def run : cont_t r m α → (α → m r) → m r := id def map (f : m r → m r) (x : cont_t r m α) : cont_t r m α := f ∘ x lemma run_cont_t_map_cont_t (f : m r → m r) (x : cont_t r m α) : run (map f x) = f ∘ run x := rfl def with_cont_t (f : (β → m r) → α → m r) (x : cont_t r m α) : cont_t r m β := λ g, x $ f g lemma run_with_cont_t (f : (β → m r) → α → m r) (x : cont_t r m α) : run (with_cont_t f x) = run x ∘ f := rfl @[ext] protected lemma ext {x y : cont_t r m α} (h : ∀ f, x.run f = y.run f) : x = y := by { ext; apply h } instance : monad (cont_t r m) := { pure := λ α x f, f x, bind := λ α β x f g, x $ λ i, f i g } instance : is_lawful_monad (cont_t r m) := { id_map := by { intros, refl }, pure_bind := by { intros, ext, refl }, bind_assoc := by { intros, ext, refl } } def monad_lift [monad m] {α} : m α → cont_t r m α := λ x f, x >>= f instance [monad m] : has_monad_lift m (cont_t r m) := { monad_lift := λ α, cont_t.monad_lift } lemma monad_lift_bind [monad m] [is_lawful_monad m] {α β} (x : m α) (f : α → m β) : (monad_lift (x >>= f) : cont_t r m β) = monad_lift x >>= monad_lift ∘ f := by { ext, simp only [monad_lift,has_monad_lift.monad_lift,(∘),(>>=),bind_assoc,id.def,run,cont_t.monad_lift] } instance : monad_cont (cont_t r m) := { call_cc := λ α β f g, f ⟨λ x h, g x⟩ g } instance : is_lawful_monad_cont (cont_t r m) := { call_cc_bind_right := by intros; ext; refl, call_cc_bind_left := by intros; ext; refl, call_cc_dummy := by intros; ext; refl } instance (ε) [monad_except ε m] : monad_except ε (cont_t r m) := { throw := λ x e f, throw e, catch := λ α act h f, catch (act f) (λ e, h e f) } instance : monad_run (λ α, (α → m r) → ulift.{u v} (m r)) (cont_t.{u v u} r m) := { run := λ α f x, ⟨ f x ⟩ } end cont_t variables {m : Type u → Type v} [monad m] def except_t.mk_label {α β ε} : label (except.{u u} ε α) m β → label α (except_t ε m) β | ⟨ f ⟩ := ⟨ λ a, monad_lift $ f (except.ok a) ⟩ lemma except_t.goto_mk_label {α β ε : Type*} (x : label (except.{u u} ε α) m β) (i : α) : goto (except_t.mk_label x) i = ⟨ except.ok <$> goto x (except.ok i) ⟩ := by cases x; refl def except_t.call_cc {ε} [monad_cont m] {α β : Type*} (f : label α (except_t ε m) β → except_t ε m α) : except_t ε m α := except_t.mk (call_cc $ λ x : label _ m β, except_t.run $ f (except_t.mk_label x) : m (except ε α)) instance {ε} [monad_cont m] : monad_cont (except_t ε m) := { call_cc := λ α β, except_t.call_cc } instance {ε} [monad_cont m] [is_lawful_monad_cont m] : is_lawful_monad_cont (except_t ε m) := { call_cc_bind_right := by { intros, simp [call_cc,except_t.call_cc,call_cc_bind_right], ext, dsimp, congr' with ⟨ ⟩; simp [except_t.bind_cont,@call_cc_dummy m _], }, call_cc_bind_left := by { intros, simp [call_cc,except_t.call_cc,call_cc_bind_right,except_t.goto_mk_label,map_eq_bind_pure_comp, bind_assoc,@call_cc_bind_left m _], ext, refl }, call_cc_dummy := by { intros, simp [call_cc,except_t.call_cc,@call_cc_dummy m _], ext, refl }, } def option_t.mk_label {α β} : label (option.{u} α) m β → label α (option_t m) β | ⟨ f ⟩ := ⟨ λ a, monad_lift $ f (some a) ⟩ lemma option_t.goto_mk_label {α β : Type*} (x : label (option.{u} α) m β) (i : α) : goto (option_t.mk_label x) i = ⟨ some <$> goto x (some i) ⟩ := by cases x; refl def option_t.call_cc [monad_cont m] {α β : Type*} (f : label α (option_t m) β → option_t m α) : option_t m α := option_t.mk (call_cc $ λ x : label _ m β, option_t.run $ f (option_t.mk_label x) : m (option α)) instance [monad_cont m] : monad_cont (option_t m) := { call_cc := λ α β, option_t.call_cc } instance [monad_cont m] [is_lawful_monad_cont m] : is_lawful_monad_cont (option_t m) := { call_cc_bind_right := by { intros, simp [call_cc,option_t.call_cc,call_cc_bind_right], ext, dsimp, congr' with ⟨ ⟩; simp [option_t.bind_cont,@call_cc_dummy m _], }, call_cc_bind_left := by { intros, simp [call_cc,option_t.call_cc,call_cc_bind_right, option_t.goto_mk_label,map_eq_bind_pure_comp,bind_assoc,@call_cc_bind_left m _], ext, refl }, call_cc_dummy := by { intros, simp [call_cc,option_t.call_cc,@call_cc_dummy m _], ext, refl }, } def writer_t.mk_label {α β ω} [has_one ω] : label (α × ω) m β → label α (writer_t ω m) β | ⟨ f ⟩ := ⟨ λ a, monad_lift $ f (a,1) ⟩ lemma writer_t.goto_mk_label {α β ω : Type*} [has_one ω] (x : label (α × ω) m β) (i : α) : goto (writer_t.mk_label x) i = monad_lift (goto x (i,1)) := by cases x; refl def writer_t.call_cc [monad_cont m] {α β ω : Type*} [has_one ω] (f : label α (writer_t ω m) β → writer_t ω m α) : writer_t ω m α := ⟨ call_cc (writer_t.run ∘ f ∘ writer_t.mk_label : label (α × ω) m β → m (α × ω)) ⟩ instance (ω) [monad m] [has_one ω] [monad_cont m] : monad_cont (writer_t ω m) := { call_cc := λ α β, writer_t.call_cc } def state_t.mk_label {α β σ : Type u} : label (α × σ) m (β × σ) → label α (state_t σ m) β | ⟨ f ⟩ := ⟨ λ a, ⟨ λ s, f (a,s) ⟩ ⟩ lemma state_t.goto_mk_label {α β σ : Type u} (x : label (α × σ) m (β × σ)) (i : α) : goto (state_t.mk_label x) i = ⟨ λ s, (goto x (i,s)) ⟩ := by cases x; refl def state_t.call_cc {σ} [monad_cont m] {α β : Type*} (f : label α (state_t σ m) β → state_t σ m α) : state_t σ m α := ⟨ λ r, call_cc (λ f', (f $ state_t.mk_label f').run r) ⟩ instance {σ} [monad_cont m] : monad_cont (state_t σ m) := { call_cc := λ α β, state_t.call_cc } instance {σ} [monad_cont m] [is_lawful_monad_cont m] : is_lawful_monad_cont (state_t σ m) := { call_cc_bind_right := by { intros, simp [call_cc,state_t.call_cc,call_cc_bind_right,(>>=),state_t.bind], ext, dsimp, congr' with ⟨x₀,x₁⟩, refl }, call_cc_bind_left := by { intros, simp [call_cc,state_t.call_cc,call_cc_bind_left,(>>=), state_t.bind,state_t.goto_mk_label], ext, refl }, call_cc_dummy := by { intros, simp [call_cc,state_t.call_cc,call_cc_bind_right,(>>=), state_t.bind,@call_cc_dummy m _], ext, refl }, } def reader_t.mk_label {α β} (ρ) : label α m β → label α (reader_t ρ m) β | ⟨ f ⟩ := ⟨ monad_lift ∘ f ⟩ lemma reader_t.goto_mk_label {α ρ β} (x : label α m β) (i : α) : goto (reader_t.mk_label ρ x) i = monad_lift (goto x i) := by cases x; refl def reader_t.call_cc {ε} [monad_cont m] {α β : Type*} (f : label α (reader_t ε m) β → reader_t ε m α) : reader_t ε m α := ⟨ λ r, call_cc (λ f', (f $ reader_t.mk_label _ f').run r) ⟩ instance {ρ} [monad_cont m] : monad_cont (reader_t ρ m) := { call_cc := λ α β, reader_t.call_cc } instance {ρ} [monad_cont m] [is_lawful_monad_cont m] : is_lawful_monad_cont (reader_t ρ m) := { call_cc_bind_right := by { intros, simp [call_cc,reader_t.call_cc,call_cc_bind_right], ext, refl }, call_cc_bind_left := by { intros, simp [call_cc,reader_t.call_cc,call_cc_bind_left, reader_t.goto_mk_label], ext, refl }, call_cc_dummy := by { intros, simp [call_cc,reader_t.call_cc,@call_cc_dummy m _], ext, refl } } /-- reduce the equivalence between two continuation passing monads to the equivalence between their underlying monad -/ def cont_t.equiv {m₁ : Type u₀ → Type v₀} {m₂ : Type u₁ → Type v₁} {α₁ r₁ : Type u₀} {α₂ r₂ : Type u₁} (F : m₁ r₁ ≃ m₂ r₂) (G : α₁ ≃ α₂) : cont_t r₁ m₁ α₁ ≃ cont_t r₂ m₂ α₂ := { to_fun := λ f r, F $ f $ λ x, F.symm $ r $ G x, inv_fun := λ f r, F.symm $ f $ λ x, F $ r $ G.symm x, left_inv := λ f, by funext r; simp, right_inv := λ f, by funext r; simp }
b1359085c501a789c36eac57b20351d6921728e4
9bb72db9297f7837f673785604fb89b3184e13f8
/library/init/data/int/order.lean
f0c463f535f968399240c764f399891521df89ae
[ "Apache-2.0" ]
permissive
dselsam/lean
ec83d7592199faa85687d884bbaaa570b62c1652
6b0bd5bc2e07e13880d332c89093fe3032bb2469
refs/heads/master
1,621,807,064,966
1,611,454,685,000
1,611,975,642,000
42,734,348
3
3
null
1,498,748,560,000
1,442,594,289,000
C++
UTF-8
Lean
false
false
36,954
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad The order relation on the integers. -/ prelude import init.data.int.basic init.data.ordering.basic namespace int def nonneg (a : ℤ) : Prop := int.cases_on a (assume n, true) (assume n, false) protected def le (a b : ℤ) : Prop := nonneg (b - a) instance : has_le int := ⟨int.le⟩ protected def lt (a b : ℤ) : Prop := (a + 1) ≤ b instance : has_lt int := ⟨int.lt⟩ def decidable_nonneg (a : ℤ) : decidable (nonneg a) := int.cases_on a (assume a, decidable.true) (assume a, decidable.false) instance decidable_le (a b : ℤ) : decidable (a ≤ b) := decidable_nonneg _ instance decidable_lt (a b : ℤ) : decidable (a < b) := decidable_nonneg _ lemma lt_iff_add_one_le (a b : ℤ) : a < b ↔ a + 1 ≤ b := iff.refl _ lemma nonneg.elim {a : ℤ} : nonneg a → ∃ n : ℕ, a = n := int.cases_on a (assume n H, exists.intro n rfl) (assume n', false.elim) lemma nonneg_or_nonneg_neg (a : ℤ) : nonneg a ∨ nonneg (-a) := int.cases_on a (assume n, or.inl trivial) (assume n, or.inr trivial) lemma le.intro_sub {a b : ℤ} {n : ℕ} (h : b - a = n) : a ≤ b := show nonneg (b - a), by rw h; trivial local attribute [simp] int.sub_eq_add_neg int.add_assoc int.add_right_neg int.add_left_neg int.zero_add int.add_zero int.neg_add int.neg_neg int.neg_zero lemma le.intro {a b : ℤ} {n : ℕ} (h : a + n = b) : a ≤ b := le.intro_sub (by rw [← h, int.add_comm]; simp) lemma le.dest_sub {a b : ℤ} (h : a ≤ b) : ∃ n : ℕ, b - a = n := nonneg.elim h lemma le.dest {a b : ℤ} (h : a ≤ b) : ∃ n : ℕ, a + n = b := match (le.dest_sub h) with | ⟨n, h₁⟩ := exists.intro n begin rw [← h₁, int.add_comm], simp end end lemma le.elim {a b : ℤ} (h : a ≤ b) {P : Prop} (h' : ∀ n : ℕ, a + ↑n = b → P) : P := exists.elim (le.dest h) h' protected lemma le_total (a b : ℤ) : a ≤ b ∨ b ≤ a := or.imp_right (assume H : nonneg (-(b - a)), have -(b - a) = a - b, by simp [int.add_comm], show nonneg (a - b), from this ▸ H) (nonneg_or_nonneg_neg (b - a)) lemma coe_nat_le_coe_nat_of_le {m n : ℕ} (h : m ≤ n) : (↑m : ℤ) ≤ ↑n := match nat.le.dest h with | ⟨k, (hk : m + k = n)⟩ := le.intro (begin rw [← hk], reflexivity end) end lemma le_of_coe_nat_le_coe_nat {m n : ℕ} (h : (↑m : ℤ) ≤ ↑n) : m ≤ n := le.elim h (assume k, assume hk : ↑m + ↑k = ↑n, have m + k = n, from int.coe_nat_inj ((int.coe_nat_add m k).trans hk), nat.le.intro this) lemma coe_nat_le_coe_nat_iff (m n : ℕ) : (↑m : ℤ) ≤ ↑n ↔ m ≤ n := iff.intro le_of_coe_nat_le_coe_nat coe_nat_le_coe_nat_of_le lemma coe_zero_le (n : ℕ) : 0 ≤ (↑n : ℤ) := coe_nat_le_coe_nat_of_le n.zero_le lemma eq_coe_of_zero_le {a : ℤ} (h : 0 ≤ a) : ∃ n : ℕ, a = n := by { have t := le.dest_sub h, simp at t, exact t } lemma eq_succ_of_zero_lt {a : ℤ} (h : 0 < a) : ∃ n : ℕ, a = n.succ := let ⟨n, (h : ↑(1+n) = a)⟩ := le.dest h in ⟨n, by rw nat.add_comm at h; exact h.symm⟩ lemma lt_add_succ (a : ℤ) (n : ℕ) : a < a + ↑(nat.succ n) := le.intro (show a + 1 + n = a + nat.succ n, begin simp [int.coe_nat_eq, int.add_comm, int.add_left_comm], reflexivity end) lemma lt.intro {a b : ℤ} {n : ℕ} (h : a + nat.succ n = b) : a < b := h ▸ lt_add_succ a n lemma lt.dest {a b : ℤ} (h : a < b) : ∃ n : ℕ, a + ↑(nat.succ n) = b := le.elim h (assume n, assume hn : a + 1 + n = b, exists.intro n begin rw [← hn, int.add_assoc, int.add_comm 1], reflexivity end) lemma lt.elim {a b : ℤ} (h : a < b) {P : Prop} (h' : ∀ n : ℕ, a + ↑(nat.succ n) = b → P) : P := exists.elim (lt.dest h) h' lemma coe_nat_lt_coe_nat_iff (n m : ℕ) : (↑n : ℤ) < ↑m ↔ n < m := begin rw [lt_iff_add_one_le, ← int.coe_nat_succ, coe_nat_le_coe_nat_iff], reflexivity end lemma lt_of_coe_nat_lt_coe_nat {m n : ℕ} (h : (↑m : ℤ) < ↑n) : m < n := (coe_nat_lt_coe_nat_iff _ _).mp h lemma coe_nat_lt_coe_nat_of_lt {m n : ℕ} (h : m < n) : (↑m : ℤ) < ↑n := (coe_nat_lt_coe_nat_iff _ _).mpr h /- show that the integers form an ordered additive group -/ protected lemma le_refl (a : ℤ) : a ≤ a := le.intro (int.add_zero a) protected lemma le_trans {a b c : ℤ} (h₁ : a ≤ b) (h₂ : b ≤ c) : a ≤ c := le.elim h₁ (assume n, assume hn : a + n = b, le.elim h₂ (assume m, assume hm : b + m = c, begin apply le.intro, rw [← hm, ← hn, int.add_assoc], reflexivity end)) protected lemma le_antisymm {a b : ℤ} (h₁ : a ≤ b) (h₂ : b ≤ a) : a = b := le.elim h₁ (assume n, assume hn : a + n = b, le.elim h₂ (assume m, assume hm : b + m = a, have a + ↑(n + m) = a + 0, by rw [int.coe_nat_add, ← int.add_assoc, hn, hm, int.add_zero a], have (↑(n + m) : ℤ) = 0, from int.add_left_cancel this, have n + m = 0, from int.coe_nat_inj this, have n = 0, from nat.eq_zero_of_add_eq_zero_right this, show a = b, begin rw [← hn, this, int.coe_nat_zero, int.add_zero a] end)) protected lemma lt_irrefl (a : ℤ) : ¬ a < a := assume : a < a, lt.elim this (assume n, assume hn : a + nat.succ n = a, have a + nat.succ n = a + 0, by rw [hn, int.add_zero], have nat.succ n = 0, from int.coe_nat_inj (int.add_left_cancel this), show false, from nat.succ_ne_zero _ this) protected lemma ne_of_lt {a b : ℤ} (h : a < b) : a ≠ b := (assume : a = b, absurd (begin rewrite this at h, exact h end) (int.lt_irrefl b)) lemma le_of_lt {a b : ℤ} (h : a < b) : a ≤ b := lt.elim h (assume n, assume hn : a + nat.succ n = b, le.intro hn) protected lemma lt_iff_le_and_ne (a b : ℤ) : a < b ↔ (a ≤ b ∧ a ≠ b) := iff.intro (assume h, ⟨le_of_lt h, int.ne_of_lt h⟩) (assume ⟨aleb, aneb⟩, le.elim aleb (assume n, assume hn : a + n = b, have n ≠ 0, from (assume : n = 0, aneb begin rw [← hn, this, int.coe_nat_zero, int.add_zero] end), have n = nat.succ (nat.pred n), from eq.symm (nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero this)), lt.intro (begin rewrite this at hn, exact hn end))) lemma lt_succ (a : ℤ) : a < a + 1 := int.le_refl (a + 1) protected lemma add_le_add_left {a b : ℤ} (h : a ≤ b) (c : ℤ) : c + a ≤ c + b := le.elim h (assume n, assume hn : a + n = b, le.intro (show c + a + n = c + b, begin rw [int.add_assoc, hn] end)) protected lemma add_lt_add_left {a b : ℤ} (h : a < b) (c : ℤ) : c + a < c + b := iff.mpr (int.lt_iff_le_and_ne _ _) (and.intro (int.add_le_add_left (le_of_lt h) _) (assume heq, int.lt_irrefl b begin rw int.add_left_cancel heq at h, exact h end)) protected lemma mul_nonneg {a b : ℤ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a * b := le.elim ha (assume n, assume hn, le.elim hb (assume m, assume hm, le.intro (show 0 + ↑n * ↑m = a * b, begin rw [← hn, ← hm], simp [int.zero_add] end))) protected lemma mul_pos {a b : ℤ} (ha : 0 < a) (hb : 0 < b) : 0 < a * b := lt.elim ha (assume n, assume hn, lt.elim hb (assume m, assume hm, lt.intro (show 0 + ↑(nat.succ (nat.succ n * m + n)) = a * b, begin rw [← hn, ← hm], simp [int.coe_nat_zero], rw [← int.coe_nat_mul], simp [nat.mul_succ, nat.succ_add] end))) protected lemma zero_lt_one : (0 : ℤ) < 1 := trivial protected lemma lt_iff_le_not_le {a b : ℤ} : a < b ↔ (a ≤ b ∧ ¬ b ≤ a) := begin simp [int.lt_iff_le_and_ne], split; intro h, { cases h with hab hn, split, { assumption }, { intro hba, simp [int.le_antisymm hab hba] at *, contradiction } }, { cases h with hab hn, split, { assumption }, { intro h, simp [*] at * } } end instance : linear_order int := { le := int.le, le_refl := int.le_refl, le_trans := @int.le_trans, le_antisymm := @int.le_antisymm, lt := int.lt, lt_iff_le_not_le := @int.lt_iff_le_not_le, le_total := int.le_total, decidable_eq := int.decidable_eq, decidable_le := int.decidable_le, decidable_lt := int.decidable_lt } lemma eq_nat_abs_of_zero_le {a : ℤ} (h : 0 ≤ a) : a = nat_abs a := let ⟨n, e⟩ := eq_coe_of_zero_le h in by rw e; refl lemma le_nat_abs {a : ℤ} : a ≤ nat_abs a := or.elim (le_total 0 a) (λh, by rw eq_nat_abs_of_zero_le h; refl) (λh, le_trans h (coe_zero_le _)) lemma neg_succ_lt_zero (n : ℕ) : -[1+ n] < 0 := lt_of_not_ge $ λ h, let ⟨m, h⟩ := eq_coe_of_zero_le h in by contradiction lemma eq_neg_succ_of_lt_zero : ∀ {a : ℤ}, a < 0 → ∃ n : ℕ, a = -[1+ n] | (n : ℕ) h := absurd h (not_lt_of_ge (coe_zero_le _)) | -[1+ n] h := ⟨n, rfl⟩ /- int is an ordered add comm group -/ protected lemma eq_neg_of_eq_neg {a b : ℤ} (h : a = -b) : b = -a := by rw [h, int.neg_neg] protected lemma neg_add_cancel_left (a b : ℤ) : -a + (a + b) = b := by rw [← int.add_assoc, int.add_left_neg, int.zero_add] protected lemma add_neg_cancel_left (a b : ℤ) : a + (-a + b) = b := by rw [← int.add_assoc, int.add_right_neg, int.zero_add] protected lemma add_neg_cancel_right (a b : ℤ) : a + b + -b = a := by rw [int.add_assoc, int.add_right_neg, int.add_zero] protected lemma neg_add_cancel_right (a b : ℤ) : a + -b + b = a := by rw [int.add_assoc, int.add_left_neg, int.add_zero] protected lemma sub_self (a : ℤ) : a - a = 0 := by rw [int.sub_eq_add_neg, int.add_right_neg] protected lemma sub_eq_zero_of_eq {a b : ℤ} (h : a = b) : a - b = 0 := by rw [h, int.sub_self] protected lemma eq_of_sub_eq_zero {a b : ℤ} (h : a - b = 0) : a = b := have 0 + b = b, by rw int.zero_add, have (a - b) + b = b, by rwa h, by rwa [int.sub_eq_add_neg, int.neg_add_cancel_right] at this protected lemma sub_eq_zero_iff_eq {a b : ℤ} : a - b = 0 ↔ a = b := ⟨int.eq_of_sub_eq_zero, int.sub_eq_zero_of_eq⟩ @[simp] protected lemma neg_eq_of_add_eq_zero {a b : ℤ} (h : a + b = 0) : -a = b := by rw [← int.add_zero (-a), ←h, ←int.add_assoc, int.add_left_neg, int.zero_add] protected lemma neg_mul_eq_neg_mul (a b : ℤ) : -(a * b) = -a * b := int.neg_eq_of_add_eq_zero begin rw [← int.distrib_right, int.add_right_neg, int.zero_mul] end protected lemma neg_mul_eq_mul_neg (a b : ℤ) : -(a * b) = a * -b := int.neg_eq_of_add_eq_zero begin rw [← int.distrib_left, int.add_right_neg, int.mul_zero] end @[simp] lemma neg_mul_eq_neg_mul_symm (a b : ℤ) : - a * b = - (a * b) := eq.symm (int.neg_mul_eq_neg_mul a b) @[simp] lemma mul_neg_eq_neg_mul_symm (a b : ℤ) : a * - b = - (a * b) := eq.symm (int.neg_mul_eq_mul_neg a b) protected lemma neg_mul_neg (a b : ℤ) : -a * -b = a * b := by simp protected lemma neg_mul_comm (a b : ℤ) : -a * b = a * -b := by simp protected lemma mul_sub (a b c : ℤ) : a * (b - c) = a * b - a * c := calc a * (b - c) = a * b + a * -c : int.distrib_left a b (-c) ... = a * b - a * c : by simp protected lemma sub_mul (a b c : ℤ) : (a - b) * c = a * c - b * c := calc (a - b) * c = a * c + -b * c : int.distrib_right a (-b) c ... = a * c - b * c : by simp section protected lemma le_of_add_le_add_left {a b c : ℤ} (h : a + b ≤ a + c) : b ≤ c := have -a + (a + b) ≤ -a + (a + c), from int.add_le_add_left h _, begin simp [int.neg_add_cancel_left] at this, assumption end protected lemma lt_of_add_lt_add_left {a b c : ℤ} (h : a + b < a + c) : b < c := have -a + (a + b) < -a + (a + c), from int.add_lt_add_left h _, begin simp [int.neg_add_cancel_left] at this, assumption end protected lemma add_le_add_right {a b : ℤ} (h : a ≤ b) (c : ℤ) : a + c ≤ b + c := int.add_comm c a ▸ int.add_comm c b ▸ int.add_le_add_left h c protected theorem add_lt_add_right {a b : ℤ} (h : a < b) (c : ℤ) : a + c < b + c := begin rw [int.add_comm a c, int.add_comm b c], exact (int.add_lt_add_left h c) end protected lemma add_le_add {a b c d : ℤ} (h₁ : a ≤ b) (h₂ : c ≤ d) : a + c ≤ b + d := le_trans (int.add_le_add_right h₁ c) (int.add_le_add_left h₂ b) protected lemma le_add_of_nonneg_right {a b : ℤ} (h : b ≥ 0) : a ≤ a + b := have a + b ≥ a + 0, from int.add_le_add_left h a, by rwa int.add_zero at this protected lemma le_add_of_nonneg_left {a b : ℤ} (h : b ≥ 0) : a ≤ b + a := have 0 + a ≤ b + a, from int.add_le_add_right h a, by rwa int.zero_add at this protected lemma add_lt_add {a b c d : ℤ} (h₁ : a < b) (h₂ : c < d) : a + c < b + d := lt_trans (int.add_lt_add_right h₁ c) (int.add_lt_add_left h₂ b) protected lemma add_lt_add_of_le_of_lt {a b c d : ℤ} (h₁ : a ≤ b) (h₂ : c < d) : a + c < b + d := lt_of_le_of_lt (int.add_le_add_right h₁ c) (int.add_lt_add_left h₂ b) protected lemma add_lt_add_of_lt_of_le {a b c d : ℤ} (h₁ : a < b) (h₂ : c ≤ d) : a + c < b + d := lt_of_lt_of_le (int.add_lt_add_right h₁ c) (int.add_le_add_left h₂ b) protected lemma lt_add_of_pos_right (a : ℤ) {b : ℤ} (h : b > 0) : a < a + b := have a + 0 < a + b, from int.add_lt_add_left h a, by rwa [int.add_zero] at this protected lemma lt_add_of_pos_left (a : ℤ) {b : ℤ} (h : b > 0) : a < b + a := have 0 + a < b + a, from int.add_lt_add_right h a, by rwa [int.zero_add] at this protected lemma le_of_add_le_add_right {a b c : ℤ} (h : a + b ≤ c + b) : a ≤ c := int.le_of_add_le_add_left (show b + a ≤ b + c, begin rw [int.add_comm b a, int.add_comm b c], assumption end) protected lemma lt_of_add_lt_add_right {a b c : ℤ} (h : a + b < c + b) : a < c := int.lt_of_add_lt_add_left (show b + a < b + c, begin rw [int.add_comm b a, int.add_comm b c], assumption end) -- here we start using properties of zero. protected lemma add_nonneg {a b : ℤ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b := int.zero_add (0:ℤ) ▸ (int.add_le_add ha hb) protected lemma add_pos {a b : ℤ} (ha : 0 < a) (hb : 0 < b) : 0 < a + b := int.zero_add (0:ℤ) ▸ (int.add_lt_add ha hb) protected lemma add_pos_of_pos_of_nonneg {a b : ℤ} (ha : 0 < a) (hb : 0 ≤ b) : 0 < a + b := int.zero_add (0:ℤ) ▸ (int.add_lt_add_of_lt_of_le ha hb) protected lemma add_pos_of_nonneg_of_pos {a b : ℤ} (ha : 0 ≤ a) (hb : 0 < b) : 0 < a + b := int.zero_add (0:ℤ) ▸ (int.add_lt_add_of_le_of_lt ha hb) protected lemma add_nonpos {a b : ℤ} (ha : a ≤ 0) (hb : b ≤ 0) : a + b ≤ 0 := int.zero_add (0:ℤ) ▸ (int.add_le_add ha hb) protected lemma add_neg {a b : ℤ} (ha : a < 0) (hb : b < 0) : a + b < 0 := int.zero_add (0:ℤ) ▸ (int.add_lt_add ha hb) protected lemma add_neg_of_neg_of_nonpos {a b : ℤ} (ha : a < 0) (hb : b ≤ 0) : a + b < 0 := int.zero_add (0:ℤ) ▸ (int.add_lt_add_of_lt_of_le ha hb) protected lemma add_neg_of_nonpos_of_neg {a b : ℤ} (ha : a ≤ 0) (hb : b < 0) : a + b < 0 := int.zero_add (0:ℤ) ▸ (int.add_lt_add_of_le_of_lt ha hb) protected lemma lt_add_of_le_of_pos {a b c : ℤ} (hbc : b ≤ c) (ha : 0 < a) : b < c + a := int.add_zero b ▸ int.add_lt_add_of_le_of_lt hbc ha protected lemma sub_add_cancel (a b : ℤ) : a - b + b = a := int.neg_add_cancel_right a b protected lemma add_sub_cancel (a b : ℤ) : a + b - b = a := int.add_neg_cancel_right a b protected lemma add_sub_assoc (a b c : ℤ) : a + b - c = a + (b - c) := by rw [int.sub_eq_add_neg, int.add_assoc, ←int.sub_eq_add_neg] protected lemma neg_le_neg {a b : ℤ} (h : a ≤ b) : -b ≤ -a := have 0 ≤ -a + b, from int.add_left_neg a ▸ int.add_le_add_left h (-a), have 0 + -b ≤ -a + b + -b, from int.add_le_add_right this (-b), by rwa [int.add_neg_cancel_right, int.zero_add] at this protected lemma le_of_neg_le_neg {a b : ℤ} (h : -b ≤ -a) : a ≤ b := suffices -(-a) ≤ -(-b), from begin simp [int.neg_neg] at this, assumption end, int.neg_le_neg h protected lemma nonneg_of_neg_nonpos {a : ℤ} (h : -a ≤ 0) : 0 ≤ a := have -a ≤ -0, by rwa int.neg_zero, int.le_of_neg_le_neg this protected lemma neg_nonpos_of_nonneg {a : ℤ} (h : 0 ≤ a) : -a ≤ 0 := have -a ≤ -0, from int.neg_le_neg h, by rwa int.neg_zero at this protected lemma nonpos_of_neg_nonneg {a : ℤ} (h : 0 ≤ -a) : a ≤ 0 := have -0 ≤ -a, by rwa int.neg_zero, int.le_of_neg_le_neg this protected lemma neg_nonneg_of_nonpos {a : ℤ} (h : a ≤ 0) : 0 ≤ -a := have -0 ≤ -a, from int.neg_le_neg h, by rwa int.neg_zero at this protected lemma neg_lt_neg {a b : ℤ} (h : a < b) : -b < -a := have 0 < -a + b, from int.add_left_neg a ▸ int.add_lt_add_left h (-a), have 0 + -b < -a + b + -b, from int.add_lt_add_right this (-b), by rwa [int.add_neg_cancel_right, int.zero_add] at this protected lemma lt_of_neg_lt_neg {a b : ℤ} (h : -b < -a) : a < b := int.neg_neg a ▸ int.neg_neg b ▸ int.neg_lt_neg h protected lemma pos_of_neg_neg {a : ℤ} (h : -a < 0) : 0 < a := have -a < -0, by rwa int.neg_zero, int.lt_of_neg_lt_neg this protected lemma neg_neg_of_pos {a : ℤ} (h : 0 < a) : -a < 0 := have -a < -0, from int.neg_lt_neg h, by rwa int.neg_zero at this protected lemma neg_of_neg_pos {a : ℤ} (h : 0 < -a) : a < 0 := have -0 < -a, by rwa int.neg_zero, int.lt_of_neg_lt_neg this protected lemma neg_pos_of_neg {a : ℤ} (h : a < 0) : 0 < -a := have -0 < -a, from int.neg_lt_neg h, by rwa int.neg_zero at this protected lemma le_neg_of_le_neg {a b : ℤ} (h : a ≤ -b) : b ≤ -a := begin have h := int.neg_le_neg h, rwa int.neg_neg at h end protected lemma neg_le_of_neg_le {a b : ℤ} (h : -a ≤ b) : -b ≤ a := begin have h := int.neg_le_neg h, rwa int.neg_neg at h end protected lemma lt_neg_of_lt_neg {a b : ℤ} (h : a < -b) : b < -a := begin have h := int.neg_lt_neg h, rwa int.neg_neg at h end protected lemma neg_lt_of_neg_lt {a b : ℤ} (h : -a < b) : -b < a := begin have h := int.neg_lt_neg h, rwa int.neg_neg at h end protected lemma sub_nonneg_of_le {a b : ℤ} (h : b ≤ a) : 0 ≤ a - b := begin have h := int.add_le_add_right h (-b), rwa int.add_right_neg at h end protected lemma le_of_sub_nonneg {a b : ℤ} (h : 0 ≤ a - b) : b ≤ a := begin have h := int.add_le_add_right h b, rwa [int.sub_add_cancel, int.zero_add] at h end protected lemma sub_nonpos_of_le {a b : ℤ} (h : a ≤ b) : a - b ≤ 0 := begin have h := int.add_le_add_right h (-b), rwa int.add_right_neg at h end protected lemma le_of_sub_nonpos {a b : ℤ} (h : a - b ≤ 0) : a ≤ b := begin have h := int.add_le_add_right h b, rwa [int.sub_add_cancel, int.zero_add] at h end protected lemma sub_pos_of_lt {a b : ℤ} (h : b < a) : 0 < a - b := begin have h := int.add_lt_add_right h (-b), rwa int.add_right_neg at h end protected lemma lt_of_sub_pos {a b : ℤ} (h : 0 < a - b) : b < a := begin have h := int.add_lt_add_right h b, rwa [int.sub_add_cancel, int.zero_add] at h end protected lemma sub_neg_of_lt {a b : ℤ} (h : a < b) : a - b < 0 := begin have h := int.add_lt_add_right h (-b), rwa int.add_right_neg at h end protected lemma lt_of_sub_neg {a b : ℤ} (h : a - b < 0) : a < b := begin have h := int.add_lt_add_right h b, rwa [int.sub_add_cancel, int.zero_add] at h end protected lemma add_le_of_le_neg_add {a b c : ℤ} (h : b ≤ -a + c) : a + b ≤ c := begin have h := int.add_le_add_left h a, rwa int.add_neg_cancel_left at h end protected lemma le_neg_add_of_add_le {a b c : ℤ} (h : a + b ≤ c) : b ≤ -a + c := begin have h := int.add_le_add_left h (-a), rwa int.neg_add_cancel_left at h end protected lemma add_le_of_le_sub_left {a b c : ℤ} (h : b ≤ c - a) : a + b ≤ c := begin have h := int.add_le_add_left h a, rwa [← int.add_sub_assoc, int.add_comm a c, int.add_sub_cancel] at h end protected lemma le_sub_left_of_add_le {a b c : ℤ} (h : a + b ≤ c) : b ≤ c - a := begin have h := int.add_le_add_right h (-a), rwa [int.add_comm a b, int.add_neg_cancel_right] at h end protected lemma add_le_of_le_sub_right {a b c : ℤ} (h : a ≤ c - b) : a + b ≤ c := begin have h := int.add_le_add_right h b, rwa int.sub_add_cancel at h end protected lemma le_sub_right_of_add_le {a b c : ℤ} (h : a + b ≤ c) : a ≤ c - b := begin have h := int.add_le_add_right h (-b), rwa int.add_neg_cancel_right at h end protected lemma le_add_of_neg_add_le {a b c : ℤ} (h : -b + a ≤ c) : a ≤ b + c := begin have h := int.add_le_add_left h b, rwa int.add_neg_cancel_left at h end protected lemma neg_add_le_of_le_add {a b c : ℤ} (h : a ≤ b + c) : -b + a ≤ c := begin have h := int.add_le_add_left h (-b), rwa int.neg_add_cancel_left at h end protected lemma le_add_of_sub_left_le {a b c : ℤ} (h : a - b ≤ c) : a ≤ b + c := begin have h := int.add_le_add_right h b, rwa [int.sub_add_cancel, int.add_comm] at h end protected lemma sub_left_le_of_le_add {a b c : ℤ} (h : a ≤ b + c) : a - b ≤ c := begin have h := int.add_le_add_right h (-b), rwa [int.add_comm b c, int.add_neg_cancel_right] at h end protected lemma le_add_of_sub_right_le {a b c : ℤ} (h : a - c ≤ b) : a ≤ b + c := begin have h := int.add_le_add_right h c, rwa int.sub_add_cancel at h end protected lemma sub_right_le_of_le_add {a b c : ℤ} (h : a ≤ b + c) : a - c ≤ b := begin have h := int.add_le_add_right h (-c), rwa int.add_neg_cancel_right at h end protected lemma le_add_of_neg_add_le_left {a b c : ℤ} (h : -b + a ≤ c) : a ≤ b + c := begin rw int.add_comm at h, exact int.le_add_of_sub_left_le h end protected lemma neg_add_le_left_of_le_add {a b c : ℤ} (h : a ≤ b + c) : -b + a ≤ c := begin rw int.add_comm, exact int.sub_left_le_of_le_add h end protected lemma le_add_of_neg_add_le_right {a b c : ℤ} (h : -c + a ≤ b) : a ≤ b + c := begin rw int.add_comm at h, exact int.le_add_of_sub_right_le h end protected lemma neg_add_le_right_of_le_add {a b c : ℤ} (h : a ≤ b + c) : -c + a ≤ b := begin rw int.add_comm at h, exact int.neg_add_le_left_of_le_add h end protected lemma le_add_of_neg_le_sub_left {a b c : ℤ} (h : -a ≤ b - c) : c ≤ a + b := int.le_add_of_neg_add_le_left (int.add_le_of_le_sub_right h) protected lemma neg_le_sub_left_of_le_add {a b c : ℤ} (h : c ≤ a + b) : -a ≤ b - c := begin have h := int.le_neg_add_of_add_le (int.sub_left_le_of_le_add h), rwa int.add_comm at h end protected lemma le_add_of_neg_le_sub_right {a b c : ℤ} (h : -b ≤ a - c) : c ≤ a + b := int.le_add_of_sub_right_le (int.add_le_of_le_sub_left h) protected lemma neg_le_sub_right_of_le_add {a b c : ℤ} (h : c ≤ a + b) : -b ≤ a - c := int.le_sub_left_of_add_le (int.sub_right_le_of_le_add h) protected lemma sub_le_of_sub_le {a b c : ℤ} (h : a - b ≤ c) : a - c ≤ b := int.sub_left_le_of_le_add (int.le_add_of_sub_right_le h) protected lemma sub_le_sub_left {a b : ℤ} (h : a ≤ b) (c : ℤ) : c - b ≤ c - a := int.add_le_add_left (int.neg_le_neg h) c protected lemma sub_le_sub_right {a b : ℤ} (h : a ≤ b) (c : ℤ) : a - c ≤ b - c := int.add_le_add_right h (-c) protected lemma sub_le_sub {a b c d : ℤ} (hab : a ≤ b) (hcd : c ≤ d) : a - d ≤ b - c := int.add_le_add hab (int.neg_le_neg hcd) protected lemma add_lt_of_lt_neg_add {a b c : ℤ} (h : b < -a + c) : a + b < c := begin have h := int.add_lt_add_left h a, rwa int.add_neg_cancel_left at h end protected lemma lt_neg_add_of_add_lt {a b c : ℤ} (h : a + b < c) : b < -a + c := begin have h := int.add_lt_add_left h (-a), rwa int.neg_add_cancel_left at h end protected lemma add_lt_of_lt_sub_left {a b c : ℤ} (h : b < c - a) : a + b < c := begin have h := int.add_lt_add_left h a, rwa [← int.add_sub_assoc, int.add_comm a c, int.add_sub_cancel] at h end protected lemma lt_sub_left_of_add_lt {a b c : ℤ} (h : a + b < c) : b < c - a := begin have h := int.add_lt_add_right h (-a), rwa [int.add_comm a b, int.add_neg_cancel_right] at h end protected lemma add_lt_of_lt_sub_right {a b c : ℤ} (h : a < c - b) : a + b < c := begin have h := int.add_lt_add_right h b, rwa int.sub_add_cancel at h end protected lemma lt_sub_right_of_add_lt {a b c : ℤ} (h : a + b < c) : a < c - b := begin have h := int.add_lt_add_right h (-b), rwa int.add_neg_cancel_right at h end protected lemma lt_add_of_neg_add_lt {a b c : ℤ} (h : -b + a < c) : a < b + c := begin have h := int.add_lt_add_left h b, rwa int.add_neg_cancel_left at h end protected lemma neg_add_lt_of_lt_add {a b c : ℤ} (h : a < b + c) : -b + a < c := begin have h := int.add_lt_add_left h (-b), rwa int.neg_add_cancel_left at h end protected lemma lt_add_of_sub_left_lt {a b c : ℤ} (h : a - b < c) : a < b + c := begin have h := int.add_lt_add_right h b, rwa [int.sub_add_cancel, int.add_comm] at h end protected lemma sub_left_lt_of_lt_add {a b c : ℤ} (h : a < b + c) : a - b < c := begin have h := int.add_lt_add_right h (-b), rwa [int.add_comm b c, int.add_neg_cancel_right] at h end protected lemma lt_add_of_sub_right_lt {a b c : ℤ} (h : a - c < b) : a < b + c := begin have h := int.add_lt_add_right h c, rwa int.sub_add_cancel at h end protected lemma sub_right_lt_of_lt_add {a b c : ℤ} (h : a < b + c) : a - c < b := begin have h := int.add_lt_add_right h (-c), rwa int.add_neg_cancel_right at h end protected lemma lt_add_of_neg_add_lt_left {a b c : ℤ} (h : -b + a < c) : a < b + c := begin rw int.add_comm at h, exact int.lt_add_of_sub_left_lt h end protected lemma neg_add_lt_left_of_lt_add {a b c : ℤ} (h : a < b + c) : -b + a < c := begin rw int.add_comm, exact int.sub_left_lt_of_lt_add h end protected lemma lt_add_of_neg_add_lt_right {a b c : ℤ} (h : -c + a < b) : a < b + c := begin rw int.add_comm at h, exact int.lt_add_of_sub_right_lt h end protected lemma neg_add_lt_right_of_lt_add {a b c : ℤ} (h : a < b + c) : -c + a < b := begin rw int.add_comm at h, exact int.neg_add_lt_left_of_lt_add h end protected lemma lt_add_of_neg_lt_sub_left {a b c : ℤ} (h : -a < b - c) : c < a + b := int.lt_add_of_neg_add_lt_left (int.add_lt_of_lt_sub_right h) protected lemma neg_lt_sub_left_of_lt_add {a b c : ℤ} (h : c < a + b) : -a < b - c := begin have h := int.lt_neg_add_of_add_lt (int.sub_left_lt_of_lt_add h), rwa int.add_comm at h end protected lemma lt_add_of_neg_lt_sub_right {a b c : ℤ} (h : -b < a - c) : c < a + b := int.lt_add_of_sub_right_lt (int.add_lt_of_lt_sub_left h) protected lemma neg_lt_sub_right_of_lt_add {a b c : ℤ} (h : c < a + b) : -b < a - c := int.lt_sub_left_of_add_lt (int.sub_right_lt_of_lt_add h) protected lemma sub_lt_of_sub_lt {a b c : ℤ} (h : a - b < c) : a - c < b := int.sub_left_lt_of_lt_add (int.lt_add_of_sub_right_lt h) protected lemma sub_lt_sub_left {a b : ℤ} (h : a < b) (c : ℤ) : c - b < c - a := int.add_lt_add_left (int.neg_lt_neg h) c protected lemma sub_lt_sub_right {a b : ℤ} (h : a < b) (c : ℤ) : a - c < b - c := int.add_lt_add_right h (-c) protected lemma sub_lt_sub {a b c d : ℤ} (hab : a < b) (hcd : c < d) : a - d < b - c := int.add_lt_add hab (int.neg_lt_neg hcd) protected lemma sub_lt_sub_of_le_of_lt {a b c d : ℤ} (hab : a ≤ b) (hcd : c < d) : a - d < b - c := int.add_lt_add_of_le_of_lt hab (int.neg_lt_neg hcd) protected lemma sub_lt_sub_of_lt_of_le {a b c d : ℤ} (hab : a < b) (hcd : c ≤ d) : a - d < b - c := int.add_lt_add_of_lt_of_le hab (int.neg_le_neg hcd) protected lemma sub_le_self (a : ℤ) {b : ℤ} (h : b ≥ 0) : a - b ≤ a := calc a - b = a + -b : rfl ... ≤ a + 0 : int.add_le_add_left (int.neg_nonpos_of_nonneg h) _ ... = a : by rw int.add_zero protected lemma sub_lt_self (a : ℤ) {b : ℤ} (h : b > 0) : a - b < a := calc a - b = a + -b : rfl ... < a + 0 : int.add_lt_add_left (int.neg_neg_of_pos h) _ ... = a : by rw int.add_zero protected lemma add_le_add_three {a b c d e f : ℤ} (h₁ : a ≤ d) (h₂ : b ≤ e) (h₃ : c ≤ f) : a + b + c ≤ d + e + f := begin apply le_trans, apply int.add_le_add, apply int.add_le_add, assumption', apply le_refl end end /- missing facts -/ protected lemma mul_lt_mul_of_pos_left {a b c : ℤ} (h₁ : a < b) (h₂ : 0 < c) : c * a < c * b := have 0 < b - a, from int.sub_pos_of_lt h₁, have 0 < c * (b - a), from int.mul_pos h₂ this, begin rw int.mul_sub at this, exact int.lt_of_sub_pos this end protected lemma mul_lt_mul_of_pos_right {a b c : ℤ} (h₁ : a < b) (h₂ : 0 < c) : a * c < b * c := have 0 < b - a, from int.sub_pos_of_lt h₁, have 0 < (b - a) * c, from int.mul_pos this h₂, begin rw int.sub_mul at this, exact int.lt_of_sub_pos this end protected lemma mul_le_mul_of_nonneg_left {a b c : ℤ} (h₁ : a ≤ b) (h₂ : 0 ≤ c) : c * a ≤ c * b := begin by_cases hba : b ≤ a, { simp [le_antisymm hba h₁] }, by_cases hc0 : c ≤ 0, { simp [le_antisymm hc0 h₂, int.zero_mul] }, exact (le_not_le_of_lt (int.mul_lt_mul_of_pos_left (lt_of_le_not_le h₁ hba) (lt_of_le_not_le h₂ hc0))).left, end protected lemma mul_le_mul_of_nonneg_right {a b c : ℤ} (h₁ : a ≤ b) (h₂ : 0 ≤ c) : a * c ≤ b * c := begin by_cases hba : b ≤ a, { simp [le_antisymm hba h₁] }, by_cases hc0 : c ≤ 0, { simp [le_antisymm hc0 h₂, int.mul_zero] }, exact (le_not_le_of_lt (int.mul_lt_mul_of_pos_right (lt_of_le_not_le h₁ hba) (lt_of_le_not_le h₂ hc0))).left, end -- TODO: there are four variations, depending on which variables we assume to be nonneg protected lemma mul_le_mul {a b c d : ℤ} (hac : a ≤ c) (hbd : b ≤ d) (nn_b : 0 ≤ b) (nn_c : 0 ≤ c) : a * b ≤ c * d := calc a * b ≤ c * b : int.mul_le_mul_of_nonneg_right hac nn_b ... ≤ c * d : int.mul_le_mul_of_nonneg_left hbd nn_c protected lemma mul_nonpos_of_nonneg_of_nonpos {a b : ℤ} (ha : a ≥ 0) (hb : b ≤ 0) : a * b ≤ 0 := have h : a * b ≤ a * 0, from int.mul_le_mul_of_nonneg_left hb ha, by rwa int.mul_zero at h protected lemma mul_nonpos_of_nonpos_of_nonneg {a b : ℤ} (ha : a ≤ 0) (hb : b ≥ 0) : a * b ≤ 0 := have h : a * b ≤ 0 * b, from int.mul_le_mul_of_nonneg_right ha hb, by rwa int.zero_mul at h protected lemma mul_lt_mul {a b c d : ℤ} (hac : a < c) (hbd : b ≤ d) (pos_b : 0 < b) (nn_c : 0 ≤ c) : a * b < c * d := calc a * b < c * b : int.mul_lt_mul_of_pos_right hac pos_b ... ≤ c * d : int.mul_le_mul_of_nonneg_left hbd nn_c protected lemma mul_lt_mul' {a b c d : ℤ} (h1 : a ≤ c) (h2 : b < d) (h3 : b ≥ 0) (h4 : c > 0) : a * b < c * d := calc a * b ≤ c * b : int.mul_le_mul_of_nonneg_right h1 h3 ... < c * d : int.mul_lt_mul_of_pos_left h2 h4 protected lemma mul_neg_of_pos_of_neg {a b : ℤ} (ha : a > 0) (hb : b < 0) : a * b < 0 := have h : a * b < a * 0, from int.mul_lt_mul_of_pos_left hb ha, by rwa int.mul_zero at h protected lemma mul_neg_of_neg_of_pos {a b : ℤ} (ha : a < 0) (hb : b > 0) : a * b < 0 := have h : a * b < 0 * b, from int.mul_lt_mul_of_pos_right ha hb, by rwa int.zero_mul at h protected lemma mul_le_mul_of_nonpos_right {a b c : ℤ} (h : b ≤ a) (hc : c ≤ 0) : a * c ≤ b * c := have -c ≥ 0, from int.neg_nonneg_of_nonpos hc, have b * -c ≤ a * -c, from int.mul_le_mul_of_nonneg_right h this, have -(b * c) ≤ -(a * c), by rwa [← int.neg_mul_eq_mul_neg, ← int.neg_mul_eq_mul_neg] at this, int.le_of_neg_le_neg this protected lemma mul_nonneg_of_nonpos_of_nonpos {a b : ℤ} (ha : a ≤ 0) (hb : b ≤ 0) : 0 ≤ a * b := have 0 * b ≤ a * b, from int.mul_le_mul_of_nonpos_right ha hb, by rwa int.zero_mul at this protected lemma mul_lt_mul_of_neg_left {a b c : ℤ} (h : b < a) (hc : c < 0) : c * a < c * b := have -c > 0, from int.neg_pos_of_neg hc, have -c * b < -c * a, from int.mul_lt_mul_of_pos_left h this, have -(c * b) < -(c * a), by rwa [← int.neg_mul_eq_neg_mul, ← int.neg_mul_eq_neg_mul] at this, int.lt_of_neg_lt_neg this protected lemma mul_lt_mul_of_neg_right {a b c : ℤ} (h : b < a) (hc : c < 0) : a * c < b * c := have -c > 0, from int.neg_pos_of_neg hc, have b * -c < a * -c, from int.mul_lt_mul_of_pos_right h this, have -(b * c) < -(a * c), by rwa [← int.neg_mul_eq_mul_neg, ← int.neg_mul_eq_mul_neg] at this, int.lt_of_neg_lt_neg this protected lemma mul_pos_of_neg_of_neg {a b : ℤ} (ha : a < 0) (hb : b < 0) : 0 < a * b := have 0 * b < a * b, from int.mul_lt_mul_of_neg_right ha hb, by rwa int.zero_mul at this protected lemma mul_self_le_mul_self {a b : ℤ} (h1 : 0 ≤ a) (h2 : a ≤ b) : a * a ≤ b * b := int.mul_le_mul h2 h2 h1 (le_trans h1 h2) protected lemma mul_self_lt_mul_self {a b : ℤ} (h1 : 0 ≤ a) (h2 : a < b) : a * a < b * b := int.mul_lt_mul' (le_of_lt h2) h2 h1 (lt_of_le_of_lt h1 h2) /- more facts specific to int -/ theorem of_nat_nonneg (n : ℕ) : 0 ≤ of_nat n := trivial theorem coe_succ_pos (n : nat) : (nat.succ n : ℤ) > 0 := coe_nat_lt_coe_nat_of_lt (nat.succ_pos _) theorem exists_eq_neg_of_nat {a : ℤ} (H : a ≤ 0) : ∃n : ℕ, a = -n := let ⟨n, h⟩ := eq_coe_of_zero_le (int.neg_nonneg_of_nonpos H) in ⟨n, int.eq_neg_of_eq_neg h.symm⟩ theorem nat_abs_of_nonneg {a : ℤ} (H : a ≥ 0) : (nat_abs a : ℤ) = a := match a, eq_coe_of_zero_le H with ._, ⟨n, rfl⟩ := rfl end theorem of_nat_nat_abs_of_nonpos {a : ℤ} (H : a ≤ 0) : (nat_abs a : ℤ) = -a := by rw [← nat_abs_neg, nat_abs_of_nonneg (int.neg_nonneg_of_nonpos H)] theorem lt_of_add_one_le {a b : ℤ} (H : a + 1 ≤ b) : a < b := H theorem add_one_le_of_lt {a b : ℤ} (H : a < b) : a + 1 ≤ b := H theorem lt_add_one_of_le {a b : ℤ} (H : a ≤ b) : a < b + 1 := int.add_le_add_right H 1 theorem le_of_lt_add_one {a b : ℤ} (H : a < b + 1) : a ≤ b := int.le_of_add_le_add_right H theorem sub_one_le_of_lt {a b : ℤ} (H : a ≤ b) : a - 1 < b := int.sub_right_lt_of_lt_add $ lt_add_one_of_le H theorem lt_of_sub_one_le {a b : ℤ} (H : a - 1 < b) : a ≤ b := le_of_lt_add_one $ int.lt_add_of_sub_right_lt H theorem le_sub_one_of_lt {a b : ℤ} (H : a < b) : a ≤ b - 1 := int.le_sub_right_of_add_le H theorem lt_of_le_sub_one {a b : ℤ} (H : a ≤ b - 1) : a < b := int.add_le_of_le_sub_right H theorem sign_of_succ (n : nat) : sign (nat.succ n) = 1 := rfl theorem sign_eq_one_of_pos {a : ℤ} (h : 0 < a) : sign a = 1 := match a, eq_succ_of_zero_lt h with ._, ⟨n, rfl⟩ := rfl end theorem sign_eq_neg_one_of_neg {a : ℤ} (h : a < 0) : sign a = -1 := match a, eq_neg_succ_of_lt_zero h with ._, ⟨n, rfl⟩ := rfl end lemma eq_zero_of_sign_eq_zero : Π {a : ℤ}, sign a = 0 → a = 0 | 0 _ := rfl theorem pos_of_sign_eq_one : ∀ {a : ℤ}, sign a = 1 → 0 < a | (n+1:ℕ) _ := coe_nat_lt_coe_nat_of_lt (nat.succ_pos _) theorem neg_of_sign_eq_neg_one : ∀ {a : ℤ}, sign a = -1 → a < 0 | (n+1:ℕ) h := match h with end | 0 h := match h with end | -[1+ n] _ := neg_succ_lt_zero _ theorem sign_eq_one_iff_pos (a : ℤ) : sign a = 1 ↔ 0 < a := ⟨pos_of_sign_eq_one, sign_eq_one_of_pos⟩ theorem sign_eq_neg_one_iff_neg (a : ℤ) : sign a = -1 ↔ a < 0 := ⟨neg_of_sign_eq_neg_one, sign_eq_neg_one_of_neg⟩ theorem sign_eq_zero_iff_zero (a : ℤ) : sign a = 0 ↔ a = 0 := ⟨eq_zero_of_sign_eq_zero, λ h, by rw [h, sign_zero]⟩ protected lemma eq_zero_or_eq_zero_of_mul_eq_zero {a b : ℤ} (h : a * b = 0) : a = 0 ∨ b = 0 := match decidable.lt_trichotomy 0 a with | or.inl hlt₁ := match decidable.lt_trichotomy 0 b with | or.inl hlt₂ := have 0 < a * b, from int.mul_pos hlt₁ hlt₂, begin rw h at this, exact absurd this (lt_irrefl _) end | or.inr (or.inl heq₂) := or.inr heq₂.symm | or.inr (or.inr hgt₂) := have 0 > a * b, from int.mul_neg_of_pos_of_neg hlt₁ hgt₂, begin rw h at this, exact absurd this (lt_irrefl _) end end | or.inr (or.inl heq₁) := or.inl heq₁.symm | or.inr (or.inr hgt₁) := match decidable.lt_trichotomy 0 b with | or.inl hlt₂ := have 0 > a * b, from int.mul_neg_of_neg_of_pos hgt₁ hlt₂, begin rw h at this, exact absurd this (lt_irrefl _) end | or.inr (or.inl heq₂) := or.inr heq₂.symm | or.inr (or.inr hgt₂) := have 0 < a * b, from int.mul_pos_of_neg_of_neg hgt₁ hgt₂, begin rw h at this, exact absurd this (lt_irrefl _) end end end protected lemma eq_of_mul_eq_mul_right {a b c : ℤ} (ha : a ≠ 0) (h : b * a = c * a) : b = c := have b * a - c * a = 0, from int.sub_eq_zero_of_eq h, have (b - c) * a = 0, by rw [int.sub_mul, this], have b - c = 0, from (int.eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_right ha, int.eq_of_sub_eq_zero this protected lemma eq_of_mul_eq_mul_left {a b c : ℤ} (ha : a ≠ 0) (h : a * b = a * c) : b = c := have a * b - a * c = 0, from int.sub_eq_zero_of_eq h, have a * (b - c) = 0, by rw [int.mul_sub, this], have b - c = 0, from (int.eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_left ha, int.eq_of_sub_eq_zero this theorem eq_one_of_mul_eq_self_left {a b : ℤ} (Hpos : a ≠ 0) (H : b * a = a) : b = 1 := int.eq_of_mul_eq_mul_right Hpos (by rw [int.one_mul, H]) theorem eq_one_of_mul_eq_self_right {a b : ℤ} (Hpos : b ≠ 0) (H : b * a = b) : a = 1 := int.eq_of_mul_eq_mul_left Hpos (by rw [int.mul_one, H]) end int
fe8005f493df0e7d6dcd2a4d720db21413d9479a
a46270e2f76a375564f3b3e9c1bf7b635edc1f2c
/3-3.lean
d573812e4582d268a62c8a7983140993e6eac640
[ "CC0-1.0" ]
permissive
wudcscheme/lean-exercise
88ea2506714eac343de2a294d1132ee8ee6d3a20
5b23b9be3d361fff5e981d5be3a0a1175504b9f6
refs/heads/master
1,678,958,930,293
1,583,197,205,000
1,583,197,205,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,266
lean
variables p q: Prop #check p -> ¬ p #check p -> q -> p ∧ q example (hp: p) (hq: q): p ∧ q := and.intro hp hq example (h: p ∧ q): p := and.elim_left h example (h: p ∧ q): q := and.elim_right h #check assume (h: p ∧ q), and.elim_left h example (h: p ∧ q): q ∧ p := and.intro (and.right h) (and.left h) #check assume (hp: p) (hq: q), (⟨hp, hq⟩: p ∧ q) example (h: p ∧ q): q ∧ p := ⟨h.right, h.left⟩ example (h: p ∧ q): q ∧ p ∧ q := ⟨h.right, h.left, h.right⟩ example (hp: p): p ∨ q := or.intro_left q hp #print or.elim example (h: p ∨ q) : q ∨ p := or.elim h (assume hp: p, or.inr hp) (assume hq: q, or.inl hq) example (hpq: p -> q) (hnq: ¬ q) : ¬ p := assume hp: p, show false, from hnq (hpq hp) #print absurd example (hp: p) (hnp: ¬ p) : q := absurd hp hnp theorem and_swap: p ∧ q ↔ q ∧ p := ⟨ λ h, ⟨h.right, h.left⟩, λ h, ⟨h.right, h.left⟩ ⟩ example (h: p ∧ q) : q ∧ p := (and_swap p q).mp h example (h: p ∧ q): q ∧ p := have hp: p, from h.left, have hq: q, from h.right, ⟨hq, hp⟩ example (h: p ∧ q): q ∧ p := have hp: p, from h.left, suffices hq: q, from ⟨hq, hp⟩, show q, from h.right
793b333ae827450539cbaba9eb6b48df6593bcff
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/limits/shapes/normal_mono_auto.lean
54404d8543ef4932c7fd30cb9dcf8ed7601109af
[]
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
7,748
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.limits.shapes.regular_mono import Mathlib.category_theory.limits.shapes.kernels import Mathlib.PostPort universes v₁ u₁ l u₂ namespace Mathlib /-! # Definitions and basic properties of normal monomorphisms and epimorphisms. A normal monomorphism is a morphism that is the kernel of some other morphism. We give the construction `normal_mono → regular_mono` (`category_theory.normal_mono.regular_mono`) as well as the dual construction for normal epimorphisms. We show equivalences reflect normal monomorphisms (`category_theory.equivalence_reflects_normal_mono`), and that the pullback of a normal monomorphism is normal (`category_theory.normal_of_is_pullback_snd_of_normal`). -/ namespace category_theory /-- A normal monomorphism is a morphism which is the kernel of some morphism. -/ class normal_mono {C : Type u₁} [category C] {X : C} {Y : C} [limits.has_zero_morphisms C] (f : X ⟶ Y) where Z : C g : Y ⟶ Z w : f ≫ g = 0 is_limit : limits.is_limit (limits.kernel_fork.of_ι f w) /-- If `F` is an equivalence and `F.map f` is a normal mono, then `f` is a normal mono. -/ def equivalence_reflects_normal_mono {C : Type u₁} [category C] [limits.has_zero_morphisms C] {D : Type u₂} [category D] [limits.has_zero_morphisms D] (F : C ⥤ D) [is_equivalence F] {X : C} {Y : C} {f : X ⟶ Y} (hf : normal_mono (functor.map F f)) : normal_mono f := normal_mono.mk (functor.obj_preimage F (normal_mono.Z (functor.map F f))) (full.preimage (normal_mono.g ≫ iso.inv (functor.obj_obj_preimage_iso F (normal_mono.Z (functor.map F f))))) sorry (limits.reflects_limit.reflects (coe_fn (limits.is_limit.of_cone_equiv (limits.cones.postcompose_equivalence (limits.comp_nat_iso F))) (limits.is_limit.of_iso_limit (limits.is_limit.of_iso_limit (limits.is_kernel.of_comp_iso normal_mono.g (functor.map F (full.preimage (normal_mono.g ≫ iso.inv (functor.obj_obj_preimage_iso F (normal_mono.Z (functor.map F f)))))) (functor.obj_obj_preimage_iso F (normal_mono.Z (functor.map F f))) sorry normal_mono.is_limit) (limits.of_ι_congr sorry)) (iso.symm (limits.iso_of_ι (functor.obj (equivalence.functor (limits.cones.postcompose_equivalence (limits.comp_nat_iso F))) (functor.map_cone F (limits.kernel_fork.of_ι f sorry)))))))) /-- Every normal monomorphism is a regular monomorphism. -/ protected instance normal_mono.regular_mono {C : Type u₁} [category C] {X : C} {Y : C} [limits.has_zero_morphisms C] (f : X ⟶ Y) [I : normal_mono f] : regular_mono f := regular_mono.mk (normal_mono.Z f) normal_mono.g 0 sorry normal_mono.is_limit /-- If `f` is a normal mono, then any map `k : W ⟶ Y` such that `k ≫ normal_mono.g = 0` induces a morphism `l : W ⟶ X` such that `l ≫ f = k`. -/ def normal_mono.lift' {C : Type u₁} [category C] {X : C} {Y : C} [limits.has_zero_morphisms C] {W : C} (f : X ⟶ Y) [normal_mono f] (k : W ⟶ Y) (h : k ≫ normal_mono.g = 0) : Subtype fun (l : W ⟶ X) => l ≫ f = k := limits.kernel_fork.is_limit.lift' normal_mono.is_limit k h /-- The second leg of a pullback cone is a normal monomorphism if the right component is too. See also `pullback.snd_of_mono` for the basic monomorphism version, and `normal_of_is_pullback_fst_of_normal` for the flipped version. -/ def normal_of_is_pullback_snd_of_normal {C : Type u₁} [category C] [limits.has_zero_morphisms C] {P : C} {Q : C} {R : C} {S : C} {f : P ⟶ Q} {g : P ⟶ R} {h : Q ⟶ S} {k : R ⟶ S} [hn : normal_mono h] (comm : f ≫ h = g ≫ k) (t : limits.is_limit (limits.pullback_cone.mk f g comm)) : normal_mono g := normal_mono.mk (normal_mono.Z h) (k ≫ normal_mono.g) sorry (let gr : regular_mono g := regular_of_is_pullback_snd_of_regular comm t; eq.mpr sorry regular_mono.is_limit) /-- The first leg of a pullback cone is a normal monomorphism if the left component is too. See also `pullback.fst_of_mono` for the basic monomorphism version, and `normal_of_is_pullback_snd_of_normal` for the flipped version. -/ def normal_of_is_pullback_fst_of_normal {C : Type u₁} [category C] [limits.has_zero_morphisms C] {P : C} {Q : C} {R : C} {S : C} {f : P ⟶ Q} {g : P ⟶ R} {h : Q ⟶ S} {k : R ⟶ S} [hn : normal_mono k] (comm : f ≫ h = g ≫ k) (t : limits.is_limit (limits.pullback_cone.mk f g comm)) : normal_mono f := normal_of_is_pullback_snd_of_normal sorry (limits.pullback_cone.flip_is_limit t) /-- A normal epimorphism is a morphism which is the cokernel of some morphism. -/ class normal_epi {C : Type u₁} [category C] {X : C} {Y : C} [limits.has_zero_morphisms C] (f : X ⟶ Y) where W : C g : W ⟶ X w : g ≫ f = 0 is_colimit : limits.is_colimit (limits.cokernel_cofork.of_π f w) /-- If `F` is an equivalence and `F.map f` is a normal epi, then `f` is a normal epi. -/ def equivalence_reflects_normal_epi {C : Type u₁} [category C] [limits.has_zero_morphisms C] {D : Type u₂} [category D] [limits.has_zero_morphisms D] (F : C ⥤ D) [is_equivalence F] {X : C} {Y : C} {f : X ⟶ Y} (hf : normal_epi (functor.map F f)) : normal_epi f := sorry /-- Every normal epimorphism is a regular epimorphism. -/ protected instance normal_epi.regular_epi {C : Type u₁} [category C] {X : C} {Y : C} [limits.has_zero_morphisms C] (f : X ⟶ Y) [I : normal_epi f] : regular_epi f := regular_epi.mk (normal_epi.W f) normal_epi.g 0 sorry normal_epi.is_colimit /-- If `f` is a normal epi, then every morphism `k : X ⟶ W` satisfying `normal_epi.g ≫ k = 0` induces `l : Y ⟶ W` such that `f ≫ l = k`. -/ def normal_epi.desc' {C : Type u₁} [category C] {X : C} {Y : C} [limits.has_zero_morphisms C] {W : C} (f : X ⟶ Y) [normal_epi f] (k : X ⟶ W) (h : normal_epi.g ≫ k = 0) : Subtype fun (l : Y ⟶ W) => f ≫ l = k := limits.cokernel_cofork.is_colimit.desc' normal_epi.is_colimit k h /-- The second leg of a pushout cocone is a normal epimorphism if the right component is too. See also `pushout.snd_of_epi` for the basic epimorphism version, and `normal_of_is_pushout_fst_of_normal` for the flipped version. -/ def normal_of_is_pushout_snd_of_normal {C : Type u₁} [category C] [limits.has_zero_morphisms C] {P : C} {Q : C} {R : C} {S : C} {f : P ⟶ Q} {g : P ⟶ R} {h : Q ⟶ S} {k : R ⟶ S} [gn : normal_epi g] (comm : f ≫ h = g ≫ k) (t : limits.is_colimit (limits.pushout_cocone.mk h k comm)) : normal_epi h := normal_epi.mk (normal_epi.W g) (normal_epi.g ≫ f) sorry (let hn : regular_epi h := regular_of_is_pushout_snd_of_regular comm t; eq.mpr sorry regular_epi.is_colimit) /-- The first leg of a pushout cocone is a normal epimorphism if the left component is too. See also `pushout.fst_of_epi` for the basic epimorphism version, and `normal_of_is_pushout_snd_of_normal` for the flipped version. -/ def normal_of_is_pushout_fst_of_normal {C : Type u₁} [category C] [limits.has_zero_morphisms C] {P : C} {Q : C} {R : C} {S : C} {f : P ⟶ Q} {g : P ⟶ R} {h : Q ⟶ S} {k : R ⟶ S} [hn : normal_epi f] (comm : f ≫ h = g ≫ k) (t : limits.is_colimit (limits.pushout_cocone.mk h k comm)) : normal_epi k := normal_of_is_pushout_snd_of_normal sorry (limits.pushout_cocone.flip_is_colimit t) end Mathlib
800a28005acd516875afb2b40070cbb4db0f3994
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/locally_finite.lean
02bbec2d1364b1266f87867d07ec8f23a8cfc0dd
[ "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
7,914
lean
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import topology.basic /-! ### Locally finite families of sets We say that a family of sets in a topological space is *locally finite* if at every point `x : X`, there is a neighborhood of `x` which meets only finitely many sets in the family. In this file we give the definition and prove basic properties of locally finite families of sets. -/ /- locally finite family [General Topology (Bourbaki, 1995)] -/ open set function filter open_locale topological_space filter variables {ι ι' α X Y : Type*} [topological_space X] [topological_space Y] {f g : ι → set X} /-- A family of sets in `set X` is locally finite if at every point `x : X`, there is a neighborhood of `x` which meets only finitely many sets in the family. -/ def locally_finite (f : ι → set X) := ∀ x : X, ∃t ∈ 𝓝 x, {i | (f i ∩ t).nonempty}.finite lemma locally_finite_of_finite [finite ι] (f : ι → set X) : locally_finite f := assume x, ⟨univ, univ_mem, to_finite _⟩ namespace locally_finite lemma point_finite (hf : locally_finite f) (x : X) : {b | x ∈ f b}.finite := let ⟨t, hxt, ht⟩ := hf x in ht.subset $ λ b hb, ⟨x, hb, mem_of_mem_nhds hxt⟩ protected lemma subset (hf : locally_finite f) (hg : ∀ i, g i ⊆ f i) : locally_finite g := assume a, let ⟨t, ht₁, ht₂⟩ := hf a in ⟨t, ht₁, ht₂.subset $ assume i hi, hi.mono $ inter_subset_inter (hg i) subset.rfl⟩ lemma comp_inj_on {g : ι' → ι} (hf : locally_finite f) (hg : inj_on g {i | (f (g i)).nonempty}) : locally_finite (f ∘ g) := λ x, let ⟨t, htx, htf⟩ := hf x in ⟨t, htx, htf.preimage $ hg.mono $ λ i hi, hi.out.mono $ inter_subset_left _ _⟩ lemma comp_injective {g : ι' → ι} (hf : locally_finite f) (hg : function.injective g) : locally_finite (f ∘ g) := hf.comp_inj_on (hg.inj_on _) lemma eventually_finite (hf : locally_finite f) (x : X) : ∀ᶠ s in (𝓝 x).small_sets, {i | (f i ∩ s).nonempty}.finite := eventually_small_sets.2 $ let ⟨s, hsx, hs⟩ := hf x in ⟨s, hsx, λ t hts, hs.subset $ λ i hi, hi.out.mono $ inter_subset_inter_right _ hts⟩ lemma exists_mem_basis {ι' : Sort*} (hf : locally_finite f) {p : ι' → Prop} {s : ι' → set X} {x : X} (hb : (𝓝 x).has_basis p s) : ∃ i (hi : p i), {j | (f j ∩ s i).nonempty}.finite := let ⟨i, hpi, hi⟩ := hb.small_sets.eventually_iff.mp (hf.eventually_finite x) in ⟨i, hpi, hi subset.rfl⟩ lemma sum_elim {g : ι' → set X} (hf : locally_finite f) (hg : locally_finite g) : locally_finite (sum.elim f g) := begin intro x, obtain ⟨s, hsx, hsf, hsg⟩ : ∃ s, s ∈ 𝓝 x ∧ {i | (f i ∩ s).nonempty}.finite ∧ {j | (g j ∩ s).nonempty}.finite, from ((𝓝 x).frequently_small_sets_mem.and_eventually ((hf.eventually_finite x).and (hg.eventually_finite x))).exists, refine ⟨s, hsx, _⟩, convert (hsf.image sum.inl).union (hsg.image sum.inr) using 1, ext (i|j); simp end protected lemma closure (hf : locally_finite f) : locally_finite (λ i, closure (f i)) := begin intro x, rcases hf x with ⟨s, hsx, hsf⟩, refine ⟨interior s, interior_mem_nhds.2 hsx, hsf.subset $ λ i hi, _⟩, exact (hi.mono (closure_inter_open' is_open_interior)).of_closure.mono (inter_subset_inter_right _ interior_subset) end lemma is_closed_Union (hf : locally_finite f) (hc : ∀i, is_closed (f i)) : is_closed (⋃i, f i) := begin simp only [← is_open_compl_iff, compl_Union, is_open_iff_mem_nhds, mem_Inter], intros a ha, replace ha : ∀ i, (f i)ᶜ ∈ 𝓝 a := λ i, (hc i).is_open_compl.mem_nhds (ha i), rcases hf a with ⟨t, h_nhds, h_fin⟩, have : t ∩ (⋂ i ∈ {i | (f i ∩ t).nonempty}, (f i)ᶜ) ∈ 𝓝 a, from inter_mem h_nhds ((bInter_mem h_fin).2 (λ i _, ha i)), filter_upwards [this], simp only [mem_inter_eq, mem_Inter], rintros b ⟨hbt, hn⟩ i hfb, exact hn i ⟨b, hfb, hbt⟩ hfb, end lemma closure_Union (h : locally_finite f) : closure (⋃ i, f i) = ⋃ i, closure (f i) := subset.antisymm (closure_minimal (Union_mono $ λ _, subset_closure) $ h.closure.is_closed_Union $ λ _, is_closed_closure) (Union_subset $ λ i, closure_mono $ subset_Union _ _) /-- If `f : β → set α` is a locally finite family of closed sets, then for any `x : α`, the intersection of the complements to `f i`, `x ∉ f i`, is a neighbourhood of `x`. -/ lemma Inter_compl_mem_nhds (hf : locally_finite f) (hc : ∀ i, is_closed (f i)) (x : X) : (⋂ i (hi : x ∉ f i), (f i)ᶜ) ∈ 𝓝 x := begin refine is_open.mem_nhds _ (mem_Inter₂.2 $ λ i, id), suffices : is_closed (⋃ i : {i // x ∉ f i}, f i), by rwa [← is_open_compl_iff, compl_Union, Inter_subtype] at this, exact (hf.comp_injective subtype.coe_injective).is_closed_Union (λ i, hc _) end /-- Let `f : ℕ → Π a, β a` be a sequence of (dependent) functions on a topological space. Suppose that the family of sets `s n = {x | f (n + 1) x ≠ f n x}` is locally finite. Then there exists a function `F : Π a, β a` such that for any `x`, we have `f n x = F x` on the product of an infinite interval `[N, +∞)` and a neighbourhood of `x`. We formulate the conclusion in terms of the product of filter `filter.at_top` and `𝓝 x`. -/ lemma exists_forall_eventually_eq_prod {π : X → Sort*} {f : ℕ → Π x : X, π x} (hf : locally_finite (λ n, {x | f (n + 1) x ≠ f n x})) : ∃ F : Π x : X, π x, ∀ x, ∀ᶠ p : ℕ × X in at_top ×ᶠ 𝓝 x, f p.1 p.2 = F p.2 := begin choose U hUx hU using hf, choose N hN using λ x, (hU x).bdd_above, replace hN : ∀ x (n > N x) (y ∈ U x), f (n + 1) y = f n y, from λ x n hn y hy, by_contra (λ hne, hn.lt.not_le $ hN x ⟨y, hne, hy⟩), replace hN : ∀ x (n ≥ N x + 1) (y ∈ U x), f n y = f (N x + 1) y, from λ x n hn y hy, nat.le_induction rfl (λ k hle, (hN x _ hle _ hy).trans) n hn, refine ⟨λ x, f (N x + 1) x, λ x, _⟩, filter_upwards [filter.prod_mem_prod (eventually_gt_at_top (N x)) (hUx x)], rintro ⟨n, y⟩ ⟨hn : N x < n, hy : y ∈ U x⟩, calc f n y = f (N x + 1) y : hN _ _ hn _ hy ... = f (max (N x + 1) (N y + 1)) y : (hN _ _ (le_max_left _ _) _ hy).symm ... = f (N y + 1) y : hN _ _ (le_max_right _ _) _ (mem_of_mem_nhds $ hUx y) end /-- Let `f : ℕ → Π a, β a` be a sequence of (dependent) functions on a topological space. Suppose that the family of sets `s n = {x | f (n + 1) x ≠ f n x}` is locally finite. Then there exists a function `F : Π a, β a` such that for any `x`, for sufficiently large values of `n`, we have `f n y = F y` in a neighbourhood of `x`. -/ lemma exists_forall_eventually_at_top_eventually_eq' {π : X → Sort*} {f : ℕ → Π x : X, π x} (hf : locally_finite (λ n, {x | f (n + 1) x ≠ f n x})) : ∃ F : Π x : X, π x, ∀ x, ∀ᶠ n : ℕ in at_top, ∀ᶠ y : X in 𝓝 x, f n y = F y := hf.exists_forall_eventually_eq_prod.imp $ λ F hF x, (hF x).curry /-- Let `f : ℕ → α → β` be a sequence of functions on a topological space. Suppose that the family of sets `s n = {x | f (n + 1) x ≠ f n x}` is locally finite. Then there exists a function `F : α → β` such that for any `x`, for sufficiently large values of `n`, we have `f n =ᶠ[𝓝 x] F`. -/ lemma exists_forall_eventually_at_top_eventually_eq {f : ℕ → X → α} (hf : locally_finite (λ n, {x | f (n + 1) x ≠ f n x})) : ∃ F : X → α, ∀ x, ∀ᶠ n : ℕ in at_top, f n =ᶠ[𝓝 x] F := hf.exists_forall_eventually_at_top_eventually_eq' lemma preimage_continuous {g : Y → X} (hf : locally_finite f) (hg : continuous g) : locally_finite (λ i, g ⁻¹' (f i)) := λ x, let ⟨s, hsx, hs⟩ := hf (g x) in ⟨g ⁻¹' s, hg.continuous_at hsx, hs.subset $ λ i ⟨y, hy⟩, ⟨g y, hy⟩⟩ end locally_finite
a2a499db24f2cba5bf4f0bc2edf9d64b3a72746f
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/gen_as.lean
7a7756a2c08e537f2b2bde745a17bd8149a3f86b
[ "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
142
lean
import data.nat open nat example (x y : nat) : x + y + y ≥ 0 := begin generalize x + y + y as n, state, intro n, exact zero_le n end
e2ac55c45b507f88294d94af1e30120001588fd4
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/tests/lean/StxQuot.lean
da5cd3eb3084abdf6d41f80fa222748466d3cff4
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,249
lean
import Lean namespace Lean open Lean.Elab def run {α} [HasToString α] : Unhygienic α → String := toString ∘ Unhygienic.run #eval run `(Nat.one) #eval run `($Syntax.missing) namespace Syntax #eval run `($missing) #eval run `($(missing)) #eval run `($(id Syntax.missing) + 1) #eval run $ let id := Syntax.missing; `($id + 1) end Syntax #eval run `(1 + 1) #eval run $ `(fun a => a) >>= pure #eval run $ `(def foo := 1) #eval run $ `(def foo := 1 def bar := 2) #eval run $ do a ← `(Nat.one); `($a) #eval run $ do a ← `(Nat.one); `(f $a $a) #eval run $ do a ← `(Nat.one); `(f $ f $a 1) #eval run $ do a ← `(Nat.one); `(f $(id a)) #eval run $ do a ← `(Nat.one); `($(a).b) #eval run $ do a ← `(1 + 2); match_syntax a with `($a + $b) => `($b + $a) | _ => pure Syntax.missing #eval run $ do a ← `(def foo := 1); match_syntax a with `($f:command) => pure f | _ => pure Syntax.missing #eval run $ do a ← `(def foo := 1 def bar := 2); match_syntax a with `($f:command $g:command) => `($g:command $f:command) | _ => pure Syntax.missing #eval run $ do a ← `(aa); match_syntax a with `($id:ident) => pure 0 | `($e) => pure 1 | _ => pure 2 #eval run $ do a ← `(1 + 2); match_syntax a with `($id:ident) => pure 0 | `($e) => pure 1 | _ => pure 2 #eval run $ do params ← #[`(a), `((b : Nat))].mapM id; `(fun $params* => 1) #eval run $ do a ← `(fun (a : Nat) b => c); match_syntax a with `(fun $aa* => $e) => pure aa | _ => pure #[] #eval run $ do a ← `(∀ a, c); match_syntax a with `(∀ $id:ident, $e) => pure id | _ => pure a #eval run $ do a ← `(∀ _, c); match_syntax a with `(∀ $id:ident, $e) => pure id | _ => pure a -- this one should NOT check the kind of the matched node #eval run $ do a ← `(∀ _, c); match_syntax a with `(∀ $a, $e) => pure a | _ => pure a #eval run $ do a ← `(a); match_syntax a with `($id:ident) => pure id | _ => pure a #eval run $ do a ← `(a.{0}); match_syntax a with `($id:ident) => pure id | _ => pure a #eval run $ do a ← `(match a with | a => 1 | _ => 2); match_syntax a with `(match $e with $eqns:matchAlt*) => pure eqns | _ => pure #[] #eval run $ do a ← `(match a with | a => 1 | _ => 2); match_syntax a with `(match $e with $eqns*) => pure eqns | _ => pure #[] end Lean
97ec9238d7ccb5bce4909725e01acc25ad0d563a
367134ba5a65885e863bdc4507601606690974c1
/src/data/int/modeq.lean
aedd25550bde7b2e808aec0c2c4cd05278420d6a
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
6,157
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.nat.modeq import tactic.ring namespace int /-- `a ≡ b [ZMOD n]` when `a % n = b % n`. -/ def modeq (n a b : ℤ) := a % n = b % n notation a ` ≡ `:50 b ` [ZMOD `:50 n `]`:0 := modeq n a b namespace modeq variables {n m a b c d : ℤ} @[refl] protected theorem refl (a : ℤ) : a ≡ a [ZMOD n] := @rfl _ _ @[symm] protected theorem symm : a ≡ b [ZMOD n] → b ≡ a [ZMOD n] := eq.symm @[trans] protected theorem trans : a ≡ b [ZMOD n] → b ≡ c [ZMOD n] → a ≡ c [ZMOD n] := eq.trans lemma coe_nat_modeq_iff {a b n : ℕ} : a ≡ b [ZMOD n] ↔ a ≡ b [MOD n] := by unfold modeq nat.modeq; rw ← int.coe_nat_eq_coe_nat_iff; simp [int.coe_nat_mod] instance : decidable (a ≡ b [ZMOD n]) := by unfold modeq; apply_instance theorem modeq_zero_iff : a ≡ 0 [ZMOD n] ↔ n ∣ a := by rw [modeq, zero_mod, dvd_iff_mod_eq_zero] theorem modeq_iff_dvd : a ≡ b [ZMOD n] ↔ (n:ℤ) ∣ b - a := by rw [modeq, eq_comm]; simp [int.mod_eq_mod_iff_mod_sub_eq_zero, int.dvd_iff_mod_eq_zero, -euclidean_domain.mod_eq_zero] theorem modeq_of_dvd_of_modeq (d : m ∣ n) (h : a ≡ b [ZMOD n]) : a ≡ b [ZMOD m] := modeq_iff_dvd.2 $ dvd_trans d (modeq_iff_dvd.1 h) theorem modeq_mul_left' (hc : 0 ≤ c) (h : a ≡ b [ZMOD n]) : c * a ≡ c * b [ZMOD (c * n)] := or.cases_on (lt_or_eq_of_le hc) (λ hc, by unfold modeq; simp [mul_mod_mul_of_pos _ _ hc, (show _ = _, from h)] ) (λ hc, by simp [hc.symm]) theorem modeq_mul_right' (hc : 0 ≤ c) (h : a ≡ b [ZMOD n]) : a * c ≡ b * c [ZMOD (n * c)] := by rw [mul_comm a, mul_comm b, mul_comm n]; exact modeq_mul_left' hc h theorem modeq_add (h₁ : a ≡ b [ZMOD n]) (h₂ : c ≡ d [ZMOD n]) : a + c ≡ b + d [ZMOD n] := modeq_iff_dvd.2 $ by {convert dvd_add (modeq_iff_dvd.1 h₁) (modeq_iff_dvd.1 h₂), ring} theorem modeq_add_cancel_left (h₁ : a ≡ b [ZMOD n]) (h₂ : a + c ≡ b + d [ZMOD n]) : c ≡ d [ZMOD n] := have d - c = b + d - (a + c) - (b - a) := by ring, modeq_iff_dvd.2 $ by { rw [this], exact dvd_sub (modeq_iff_dvd.1 h₂) (modeq_iff_dvd.1 h₁) } theorem modeq_add_cancel_right (h₁ : c ≡ d [ZMOD n]) (h₂ : a + c ≡ b + d [ZMOD n]) : a ≡ b [ZMOD n] := by rw [add_comm a, add_comm b] at h₂; exact modeq_add_cancel_left h₁ h₂ theorem mod_modeq (a n) : a % n ≡ a [ZMOD n] := int.mod_mod _ _ theorem modeq_neg (h : a ≡ b [ZMOD n]) : -a ≡ -b [ZMOD n] := modeq_add_cancel_left h (by simp) theorem modeq_sub (h₁ : a ≡ b [ZMOD n]) (h₂ : c ≡ d [ZMOD n]) : a - c ≡ b - d [ZMOD n] := by rw [sub_eq_add_neg, sub_eq_add_neg]; exact modeq_add h₁ (modeq_neg h₂) theorem modeq_mul_left (c : ℤ) (h : a ≡ b [ZMOD n]) : c * a ≡ c * b [ZMOD n] := or.cases_on (le_total 0 c) (λ hc, modeq_of_dvd_of_modeq (dvd_mul_left _ _) (modeq_mul_left' hc h)) (λ hc, by rw [← neg_neg c, ← neg_mul_eq_neg_mul, ← neg_mul_eq_neg_mul _ b]; exact modeq_neg (modeq_of_dvd_of_modeq (dvd_mul_left _ _) (modeq_mul_left' (neg_nonneg.2 hc) h))) theorem modeq_mul_right (c : ℤ) (h : a ≡ b [ZMOD n]) : a * c ≡ b * c [ZMOD n] := by rw [mul_comm a, mul_comm b]; exact modeq_mul_left c h theorem modeq_mul (h₁ : a ≡ b [ZMOD n]) (h₂ : c ≡ d [ZMOD n]) : a * c ≡ b * d [ZMOD n] := (modeq_mul_left _ h₂).trans (modeq_mul_right _ h₁) theorem modeq_of_modeq_mul_left (m : ℤ) (h : a ≡ b [ZMOD m * n]) : a ≡ b [ZMOD n] := by rw [modeq_iff_dvd] at *; exact dvd.trans (dvd_mul_left n m) h theorem modeq_of_modeq_mul_right (m : ℤ) : a ≡ b [ZMOD n * m] → a ≡ b [ZMOD n] := mul_comm m n ▸ modeq_of_modeq_mul_left _ lemma modeq_and_modeq_iff_modeq_mul {a b m n : ℤ} (hmn : nat.coprime m.nat_abs n.nat_abs) : a ≡ b [ZMOD m] ∧ a ≡ b [ZMOD n] ↔ (a ≡ b [ZMOD m * n]) := ⟨λ h, begin rw [int.modeq.modeq_iff_dvd, int.modeq.modeq_iff_dvd] at h, rw [int.modeq.modeq_iff_dvd, ← int.nat_abs_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd, int.nat_abs_mul], refine hmn.mul_dvd_of_dvd_of_dvd _ _; rw [← int.coe_nat_dvd, int.nat_abs_dvd, int.dvd_nat_abs]; tauto end, λ h, ⟨int.modeq.modeq_of_modeq_mul_right _ h, int.modeq.modeq_of_modeq_mul_left _ h⟩⟩ lemma gcd_a_modeq (a b : ℕ) : (a : ℤ) * nat.gcd_a a b ≡ nat.gcd a b [ZMOD b] := by rw [← add_zero ((a : ℤ) * _), nat.gcd_eq_gcd_ab]; exact int.modeq.modeq_add rfl (int.modeq.modeq_zero_iff.2 (dvd_mul_right _ _)).symm theorem modeq_add_fac {a b n : ℤ} (c : ℤ) (ha : a ≡ b [ZMOD n]) : a + n*c ≡ b [ZMOD n] := calc a + n*c ≡ b + n*c [ZMOD n] : int.modeq.modeq_add ha (int.modeq.refl _) ... ≡ b + 0 [ZMOD n] : int.modeq.modeq_add (int.modeq.refl _) (int.modeq.modeq_zero_iff.2 (dvd_mul_right _ _)) ... ≡ b [ZMOD n] : by simp open nat lemma mod_coprime {a b : ℕ} (hab : coprime a b) : ∃ y : ℤ, a * y ≡ 1 [ZMOD b] := ⟨ nat.gcd_a a b, have hgcd : nat.gcd a b = 1, from coprime.gcd_eq_one hab, calc ↑a * nat.gcd_a a b ≡ ↑a * nat.gcd_a a b + ↑b * nat.gcd_b a b [ZMOD ↑b] : int.modeq.symm $ modeq_add_fac _ $ int.modeq.refl _ ... ≡ 1 [ZMOD ↑b] : by rw [← nat.gcd_eq_gcd_ab, hgcd]; reflexivity ⟩ lemma exists_unique_equiv (a : ℤ) {b : ℤ} (hb : 0 < b) : ∃ z : ℤ, 0 ≤ z ∧ z < b ∧ z ≡ a [ZMOD b] := ⟨ a % b, int.mod_nonneg _ (ne_of_gt hb), have a % b < abs b, from int.mod_lt _ (ne_of_gt hb), by rwa abs_of_pos hb at this, by simp [int.modeq] ⟩ lemma exists_unique_equiv_nat (a : ℤ) {b : ℤ} (hb : 0 < b) : ∃ z : ℕ, ↑z < b ∧ ↑z ≡ a [ZMOD b] := let ⟨z, hz1, hz2, hz3⟩ := exists_unique_equiv a hb in ⟨z.nat_abs, by split; rw [←int.of_nat_eq_coe, int.of_nat_nat_abs_eq_of_nonneg hz1]; assumption⟩ end modeq @[simp] lemma mod_mul_right_mod (a b c : ℤ) : a % (b * c) % b = a % b := int.modeq.modeq_of_modeq_mul_right _ (int.modeq.mod_modeq _ _) @[simp] lemma mod_mul_left_mod (a b c : ℤ) : a % (b * c) % c = a % c := int.modeq.modeq_of_modeq_mul_left _ (int.modeq.mod_modeq _ _) end int
42b1ff41f3d6dd4b75247d8c2d10b53b6c53f95b
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/src/Lean/DeclarationRange.lean
81078f8955d2798d2c02db191b0449220c5f5d40
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,244
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.MonadEnv import Lean.AuxRecursor namespace Lean structure DeclarationRange where pos : Position endPos : Position deriving Inhabited, DecidableEq, Repr structure DeclarationRanges where range : DeclarationRange selectionRange : DeclarationRange deriving Inhabited, Repr builtin_initialize declRangeExt : MapDeclarationExtension DeclarationRanges ← mkMapDeclarationExtension `declranges def addDeclarationRanges [MonadEnv m] (declName : Name) (declRanges : DeclarationRanges) : m Unit := modifyEnv fun env => declRangeExt.insert env declName declRanges def findDeclarationRangesCore? [Monad m] [MonadEnv m] (declName : Name) : m (Option DeclarationRanges) := return declRangeExt.find? (← getEnv) declName def findDeclarationRanges? [Monad m] [MonadEnv m] (declName : Name) : m (Option DeclarationRanges) := do let env ← getEnv if isAuxRecursor env declName || isNoConfusion env declName || (← isRec declName) then findDeclarationRangesCore? declName.getPrefix else findDeclarationRangesCore? declName end Lean
1e3ceba17b32efd7155496d6f2809daf072e2b0e
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebraic_topology/dold_kan/gamma_comp_n.lean
86e9a3e0230c0678e2cff34e1ce2fb46ff0934f1
[ "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
6,703
lean
/- Copyright (c) 2022 Joël Riou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joël Riou -/ import algebraic_topology.dold_kan.functor_gamma import category_theory.idempotents.homological_complex /-! > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. The counit isomorphism of the Dold-Kan equivalence The purpose of this file is to construct natural isomorphisms `N₁Γ₀ : Γ₀ ⋙ N₁ ≅ to_karoubi (chain_complex C ℕ)` and `N₂Γ₂ : Γ₂ ⋙ N₂ ≅ 𝟭 (karoubi (chain_complex C ℕ))`. (See `equivalence.lean` for the general strategy of proof of the Dold-Kan equivalence.) -/ noncomputable theory open category_theory category_theory.category category_theory.limits category_theory.idempotents opposite simplicial_object open_locale simplicial namespace algebraic_topology namespace dold_kan variables {C : Type*} [category C] [preadditive C] [has_finite_coproducts C] /-- The isomorphism `(Γ₀.splitting K).nondeg_complex ≅ K` for all `K : chain_complex C ℕ`. -/ @[simps] def Γ₀_nondeg_complex_iso (K : chain_complex C ℕ) : (Γ₀.splitting K).nondeg_complex ≅ K := homological_complex.hom.iso_of_components (λ n, iso.refl _) begin rintros _ n (rfl : n+1=_), dsimp, simp only [id_comp, comp_id, alternating_face_map_complex.obj_d_eq, preadditive.sum_comp, preadditive.comp_sum], rw fintype.sum_eq_single (0 : fin (n+2)), { simp only [fin.coe_zero, pow_zero, one_zsmul], erw [Γ₀.obj.map_mono_on_summand_id_assoc, Γ₀.obj.termwise.map_mono_δ₀, splitting.ι_π_summand_eq_id, comp_id], }, { intros i hi, dsimp, simp only [preadditive.zsmul_comp, preadditive.comp_zsmul, assoc], erw [Γ₀.obj.map_mono_on_summand_id_assoc, Γ₀.obj.termwise.map_mono_eq_zero, zero_comp, zsmul_zero], { intro h, replace h := congr_arg simplex_category.len h, change n+1 = n at h, linarith, }, { simpa only [is_δ₀.iff] using hi, }, }, end /-- The natural isomorphism `(Γ₀.splitting K).nondeg_complex ≅ K` for `K : chain_complex C ℕ`. -/ def Γ₀'_comp_nondeg_complex_functor : Γ₀' ⋙ split.nondeg_complex_functor ≅ 𝟭 (chain_complex C ℕ) := nat_iso.of_components Γ₀_nondeg_complex_iso (λ X Y f, by { ext n, dsimp, simp only [comp_id, id_comp], }) /-- The natural isomorphism `Γ₀ ⋙ N₁ ≅ to_karoubi (chain_complex C ℕ)`. -/ def N₁Γ₀ : Γ₀ ⋙ N₁ ≅ to_karoubi (chain_complex C ℕ) := calc Γ₀ ⋙ N₁ ≅ Γ₀' ⋙ split.forget C ⋙ N₁ : functor.associator _ _ _ ... ≅ Γ₀' ⋙ split.nondeg_complex_functor ⋙ to_karoubi _ : iso_whisker_left Γ₀' split.to_karoubi_nondeg_complex_functor_iso_N₁.symm ... ≅ (Γ₀' ⋙ split.nondeg_complex_functor) ⋙ to_karoubi _ : (functor.associator _ _ _).symm ... ≅ 𝟭 _ ⋙ to_karoubi (chain_complex C ℕ) : iso_whisker_right Γ₀'_comp_nondeg_complex_functor _ ... ≅ to_karoubi (chain_complex C ℕ) : functor.left_unitor _ lemma N₁Γ₀_app (K : chain_complex C ℕ) : N₁Γ₀.app K = (Γ₀.splitting K).to_karoubi_nondeg_complex_iso_N₁.symm ≪≫ (to_karoubi _).map_iso (Γ₀_nondeg_complex_iso K) := begin ext1, dsimp [N₁Γ₀], erw [id_comp, comp_id, comp_id], refl, end lemma N₁Γ₀_hom_app (K : chain_complex C ℕ) : N₁Γ₀.hom.app K = (Γ₀.splitting K).to_karoubi_nondeg_complex_iso_N₁.inv ≫ (to_karoubi _).map (Γ₀_nondeg_complex_iso K).hom := by { change (N₁Γ₀.app K).hom = _, simpa only [N₁Γ₀_app], } lemma N₁Γ₀_inv_app (K : chain_complex C ℕ) : N₁Γ₀.inv.app K = (to_karoubi _).map (Γ₀_nondeg_complex_iso K).inv ≫ (Γ₀.splitting K).to_karoubi_nondeg_complex_iso_N₁.hom := by { change (N₁Γ₀.app K).inv = _, simpa only [N₁Γ₀_app], } @[simp] lemma N₁Γ₀_hom_app_f_f (K : chain_complex C ℕ) (n : ℕ) : (N₁Γ₀.hom.app K).f.f n = (Γ₀.splitting K).to_karoubi_nondeg_complex_iso_N₁.inv.f.f n := by { rw N₁Γ₀_hom_app, apply comp_id, } @[simp] lemma N₁Γ₀_inv_app_f_f (K : chain_complex C ℕ) (n : ℕ) : (N₁Γ₀.inv.app K).f.f n = (Γ₀.splitting K).to_karoubi_nondeg_complex_iso_N₁.hom.f.f n := by { rw N₁Γ₀_inv_app, apply id_comp, } lemma N₂Γ₂_to_karoubi : to_karoubi (chain_complex C ℕ) ⋙ Γ₂ ⋙ N₂ = Γ₀ ⋙ N₁ := begin have h := functor.congr_obj (functor_extension₂_comp_whiskering_left_to_karoubi (chain_complex C ℕ) (simplicial_object C)) Γ₀, have h' := functor.congr_obj (functor_extension₁_comp_whiskering_left_to_karoubi (simplicial_object C) (chain_complex C ℕ)) N₁, dsimp [N₂, Γ₂, functor_extension₁] at h h' ⊢, rw [← functor.assoc, h, functor.assoc, h'], end /-- Compatibility isomorphism between `to_karoubi _ ⋙ Γ₂ ⋙ N₂` and `Γ₀ ⋙ N₁` which are functors `chain_complex C ℕ ⥤ karoubi (chain_complex C ℕ)`. -/ @[simps] def N₂Γ₂_to_karoubi_iso : to_karoubi (chain_complex C ℕ) ⋙ Γ₂ ⋙ N₂ ≅ Γ₀ ⋙ N₁ := eq_to_iso (N₂Γ₂_to_karoubi) /-- The counit isomorphism of the Dold-Kan equivalence for additive categories. -/ def N₂Γ₂ : Γ₂ ⋙ N₂ ≅ 𝟭 (karoubi (chain_complex C ℕ)) := ((whiskering_left _ _ _).obj (to_karoubi (chain_complex C ℕ))).preimage_iso (N₂Γ₂_to_karoubi_iso ≪≫ N₁Γ₀) lemma N₂Γ₂_compatible_with_N₁Γ₀ (K : chain_complex C ℕ) : N₂Γ₂.hom.app ((to_karoubi _).obj K) = N₂Γ₂_to_karoubi_iso.hom.app K ≫ N₁Γ₀.hom.app K := congr_app (((whiskering_left _ _ (karoubi (chain_complex C ℕ ))).obj (to_karoubi (chain_complex C ℕ))).image_preimage (N₂Γ₂_to_karoubi_iso.hom ≫ N₁Γ₀.hom : _ ⟶ to_karoubi _ ⋙ 𝟭 _)) K @[simp] lemma N₂Γ₂_inv_app_f_f (X : karoubi (chain_complex C ℕ)) (n : ℕ) : (N₂Γ₂.inv.app X).f.f n = X.p.f n ≫ (Γ₀.splitting X.X).ι_summand (splitting.index_set.id (op [n])) := begin dsimp only [N₂Γ₂, functor.preimage_iso, iso.trans], simp only [whiskering_left_obj_preimage_app, N₂Γ₂_to_karoubi_iso_inv, functor.id_map, nat_trans.comp_app, eq_to_hom_app, functor.comp_map, assoc, karoubi.comp_f, karoubi.eq_to_hom_f, eq_to_hom_refl, comp_id, karoubi.comp_p_assoc, N₂_map_f_f, homological_complex.comp_f, N₁Γ₀_inv_app_f_f, P_infty_on_Γ₀_splitting_summand_eq_self_assoc, splitting.to_karoubi_nondeg_complex_iso_N₁_hom_f_f, Γ₂_map_f_app, karoubi.decomp_id_p_f], dsimp [to_karoubi], rw [splitting.ι_desc], dsimp [splitting.index_set.id], rw karoubi.homological_complex.p_idem_assoc, end end dold_kan end algebraic_topology
319a81d8e44b06b3bf7193a242e9ada1dd430bb0
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/combinatorics/simple_graph/regularity/uniform.lean
cc649b1077f5a54263b4122d05484c0c287b245e
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
10,288
lean
/- Copyright (c) 2022 Yaël Dillies, Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Bhavik Mehta -/ import combinatorics.simple_graph.density import set_theory.ordinal.basic /-! # Graph uniformity and uniform partitions In this file we define uniformity of a pair of vertices in a graph and uniformity of a partition of vertices of a graph. Both are also known as ε-regularity. Finsets of vertices `s` and `t` are `ε`-uniform in a graph `G` if their edge density is at most `ε`-far from the density of any big enough `s'` and `t'` where `s' ⊆ s`, `t' ⊆ t`. The definition is pretty technical, but it amounts to the edges between `s` and `t` being "random" The literature contains several definitions which are equivalent up to scaling `ε` by some constant when the partition is equitable. A partition `P` of the vertices is `ε`-uniform if the proportion of non `ε`-uniform pairs of parts is less than `ε`. ## Main declarations * `simple_graph.is_uniform`: Graph uniformity of a pair of finsets of vertices. * `simple_graph.nonuniform_witness`: `G.nonuniform_witness ε s t` and `G.nonuniform_witness ε t s` together witness the non-uniformity of `s` and `t`. * `finpartition.non_uniforms`: Non uniform pairs of parts of a partition. * `finpartition.is_uniform`: Uniformity of a partition. * `finpartition.nonuniform_witnesses`: For each non-uniform pair of parts of a partition, pick witnesses of non-uniformity and dump them all together. -/ open finset variables {α 𝕜 : Type*} [linear_ordered_field 𝕜] /-! ### Graph uniformity -/ namespace simple_graph variables (G : simple_graph α) [decidable_rel G.adj] (ε : 𝕜) {s t : finset α} {a b : α} /-- A pair of finsets of vertices is `ε`-uniform (aka `ε`-regular) iff their edge density is close to the density of any big enough pair of subsets. Intuitively, the edges between them are random-like. -/ def is_uniform (s t : finset α) : Prop := ∀ ⦃s'⦄, s' ⊆ s → ∀ ⦃t'⦄, t' ⊆ t → (s.card : 𝕜) * ε ≤ s'.card → (t.card : 𝕜) * ε ≤ t'.card → |(G.edge_density s' t' : 𝕜) - (G.edge_density s t : 𝕜)| < ε variables {G ε} lemma is_uniform.mono {ε' : 𝕜} (h : ε ≤ ε') (hε : is_uniform G ε s t) : is_uniform G ε' s t := λ s' hs' t' ht' hs ht, by refine (hε hs' ht' (le_trans _ hs) (le_trans _ ht)).trans_le h; exact mul_le_mul_of_nonneg_left h (nat.cast_nonneg _) lemma is_uniform.symm : symmetric (is_uniform G ε) := λ s t h t' ht' s' hs' ht hs, by { rw [edge_density_comm _ t', edge_density_comm _ t], exact h hs' ht' hs ht } variables (G) lemma is_uniform_comm : is_uniform G ε s t ↔ is_uniform G ε t s := ⟨λ h, h.symm, λ h, h.symm⟩ lemma is_uniform_singleton (hε : 0 < ε) : G.is_uniform ε {a} {b} := begin intros s' hs' t' ht' hs ht, rw [card_singleton, nat.cast_one, one_mul] at hs ht, obtain rfl | rfl := finset.subset_singleton_iff.1 hs', { replace hs : ε ≤ 0 := by simpa using hs, exact (hε.not_le hs).elim }, obtain rfl | rfl := finset.subset_singleton_iff.1 ht', { replace ht : ε ≤ 0 := by simpa using ht, exact (hε.not_le ht).elim }, { rwa [sub_self, abs_zero] } end lemma not_is_uniform_zero : ¬ G.is_uniform (0 : 𝕜) s t := λ h, (abs_nonneg _).not_lt $ h (empty_subset _) (empty_subset _) (by simp) (by simp) lemma is_uniform_one : G.is_uniform (1 : 𝕜) s t := begin intros s' hs' t' ht' hs ht, rw mul_one at hs ht, rw [eq_of_subset_of_card_le hs' (nat.cast_le.1 hs), eq_of_subset_of_card_le ht' (nat.cast_le.1 ht), sub_self, abs_zero], exact zero_lt_one, end variables {G} lemma not_is_uniform_iff : ¬ G.is_uniform ε s t ↔ ∃ s', s' ⊆ s ∧ ∃ t', t' ⊆ t ∧ ↑s.card * ε ≤ s'.card ∧ ↑t.card * ε ≤ t'.card ∧ ε ≤ |G.edge_density s' t' - G.edge_density s t| := by { unfold is_uniform, simp only [not_forall, not_lt, exists_prop] } open_locale classical variables (G) /-- An arbitrary pair of subsets witnessing the non-uniformity of `(s, t)`. If `(s, t)` is uniform, returns `(s, t)`. Witnesses for `(s, t)` and `(t, s)` don't necessarily match. See `simple_graph.nonuniform_witness`. -/ noncomputable def nonuniform_witnesses (ε : 𝕜) (s t : finset α) : finset α × finset α := if h : ¬ G.is_uniform ε s t then ((not_is_uniform_iff.1 h).some, (not_is_uniform_iff.1 h).some_spec.2.some) else (s, t) lemma left_nonuniform_witnesses_subset (h : ¬ G.is_uniform ε s t) : (G.nonuniform_witnesses ε s t).1 ⊆ s := by { rw [nonuniform_witnesses, dif_pos h], exact (not_is_uniform_iff.1 h).some_spec.1 } lemma left_nonuniform_witnesses_card (h : ¬ G.is_uniform ε s t) : (s.card : 𝕜) * ε ≤ (G.nonuniform_witnesses ε s t).1.card := by { rw [nonuniform_witnesses, dif_pos h], exact (not_is_uniform_iff.1 h).some_spec.2.some_spec.2.1 } lemma right_nonuniform_witnesses_subset (h : ¬ G.is_uniform ε s t) : (G.nonuniform_witnesses ε s t).2 ⊆ t := by { rw [nonuniform_witnesses, dif_pos h], exact (not_is_uniform_iff.1 h).some_spec.2.some_spec.1 } lemma right_nonuniform_witnesses_card (h : ¬ G.is_uniform ε s t) : (t.card : 𝕜) * ε ≤ (G.nonuniform_witnesses ε s t).2.card := by { rw [nonuniform_witnesses, dif_pos h], exact (not_is_uniform_iff.1 h).some_spec.2.some_spec.2.2.1 } lemma nonuniform_witnesses_spec (h : ¬ G.is_uniform ε s t) : ε ≤ |G.edge_density (G.nonuniform_witnesses ε s t).1 (G.nonuniform_witnesses ε s t).2 - G.edge_density s t| := by { rw [nonuniform_witnesses, dif_pos h], exact (not_is_uniform_iff.1 h).some_spec.2.some_spec.2.2.2 } /-- Arbitrary witness of non-uniformity. `G.nonuniform_witness ε s t` and `G.nonuniform_witness ε t s` form a pair of subsets witnessing the non-uniformity of `(s, t)`. If `(s, t)` is uniform, returns `s`. -/ noncomputable def nonuniform_witness (ε : 𝕜) (s t : finset α) : finset α := if well_ordering_rel s t then (G.nonuniform_witnesses ε s t).1 else (G.nonuniform_witnesses ε t s).2 lemma nonuniform_witness_subset (h : ¬ G.is_uniform ε s t) : G.nonuniform_witness ε s t ⊆ s := begin unfold nonuniform_witness, split_ifs, { exact G.left_nonuniform_witnesses_subset h }, { exact G.right_nonuniform_witnesses_subset (λ i, h i.symm) } end lemma nonuniform_witness_card_le (h : ¬ G.is_uniform ε s t) : (s.card : 𝕜) * ε ≤ (G.nonuniform_witness ε s t).card := begin unfold nonuniform_witness, split_ifs, { exact G.left_nonuniform_witnesses_card h }, { exact G.right_nonuniform_witnesses_card (λ i, h i.symm) } end lemma nonuniform_witness_spec (h₁ : s ≠ t) (h₂ : ¬ G.is_uniform ε s t) : ε ≤ |G.edge_density (G.nonuniform_witness ε s t) (G.nonuniform_witness ε t s) - G.edge_density s t| := begin unfold nonuniform_witness, rcases trichotomous_of well_ordering_rel s t with lt | rfl | gt, { rw [if_pos lt, if_neg (asymm lt)], exact G.nonuniform_witnesses_spec h₂ }, { cases h₁ rfl }, { rw [if_neg (asymm gt), if_pos gt, edge_density_comm, edge_density_comm _ s], apply G.nonuniform_witnesses_spec (λ i, h₂ i.symm) } end end simple_graph /-! ### Uniform partitions -/ variables [decidable_eq α] {A : finset α} (P : finpartition A) (G : simple_graph α) [decidable_rel G.adj] {ε : 𝕜} namespace finpartition open_locale classical /-- The pairs of parts of a partition `P` which are not `ε`-uniform in a graph `G`. Note that we dismiss the diagonal. We do not care whether `s` is `ε`-uniform with itself. -/ noncomputable def non_uniforms (ε : 𝕜) : finset (finset α × finset α) := P.parts.off_diag.filter $ λ uv, ¬G.is_uniform ε uv.1 uv.2 lemma mk_mem_non_uniforms_iff (u v : finset α) (ε : 𝕜) : (u, v) ∈ P.non_uniforms G ε ↔ u ∈ P.parts ∧ v ∈ P.parts ∧ u ≠ v ∧ ¬G.is_uniform ε u v := by rw [non_uniforms, mem_filter, mem_off_diag, and_assoc, and_assoc] lemma non_uniforms_mono {ε ε' : 𝕜} (h : ε ≤ ε') : P.non_uniforms G ε' ⊆ P.non_uniforms G ε := monotone_filter_right _ $ λ uv, mt $ simple_graph.is_uniform.mono h lemma non_uniforms_bot (hε : 0 < ε) : (⊥ : finpartition A).non_uniforms G ε = ∅ := begin rw eq_empty_iff_forall_not_mem, rintro ⟨u, v⟩, simp only [finpartition.mk_mem_non_uniforms_iff, finpartition.parts_bot, mem_map, not_and, not_not, exists_imp_distrib], rintro x hx rfl y hy rfl h, exact G.is_uniform_singleton hε, end /-- A finpartition of a graph's vertex set is `ε`-uniform (aka `ε`-regular) iff the proportion of its pairs of parts that are not `ε`-uniform is at most `ε`. -/ def is_uniform (ε : 𝕜) : Prop := ((P.non_uniforms G ε).card : 𝕜) ≤ (P.parts.card * (P.parts.card - 1) : ℕ) * ε lemma bot_is_uniform (hε : 0 < ε) : (⊥ : finpartition A).is_uniform G ε := begin rw [finpartition.is_uniform, finpartition.card_bot, non_uniforms_bot _ hε, finset.card_empty, nat.cast_zero], exact mul_nonneg (nat.cast_nonneg _) hε.le, end lemma is_uniform_one : P.is_uniform G (1 : 𝕜) := begin rw [is_uniform, mul_one, nat.cast_le], refine (card_filter_le _ _).trans _, rw [off_diag_card, nat.mul_sub_left_distrib, mul_one], end variables {P G} lemma is_uniform.mono {ε ε' : 𝕜} (hP : P.is_uniform G ε) (h : ε ≤ ε') : P.is_uniform G ε' := ((nat.cast_le.2 $ card_le_of_subset $ P.non_uniforms_mono G h).trans hP).trans $ mul_le_mul_of_nonneg_left h $ nat.cast_nonneg _ lemma is_uniform_of_empty (hP : P.parts = ∅) : P.is_uniform G ε := by simp [is_uniform, hP, non_uniforms] lemma nonempty_of_not_uniform (h : ¬ P.is_uniform G ε) : P.parts.nonempty := nonempty_of_ne_empty $ λ h₁, h $ is_uniform_of_empty h₁ variables (P G ε) (s : finset α) /-- A choice of witnesses of non-uniformity among the parts of a finpartition. -/ noncomputable def nonuniform_witnesses : finset (finset α) := (P.parts.filter $ λ t, s ≠ t ∧ ¬ G.is_uniform ε s t).image (G.nonuniform_witness ε s) variables {P G ε s} {t : finset α} lemma nonuniform_witness_mem_nonuniform_witnesses (h : ¬ G.is_uniform ε s t) (ht : t ∈ P.parts) (hst : s ≠ t) : G.nonuniform_witness ε s t ∈ P.nonuniform_witnesses G ε s := mem_image_of_mem _ $ mem_filter.2 ⟨ht, hst, h⟩ end finpartition
20809394fbcfd9c959fa552222c9697838d913c0
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/list/forall2.lean
56d05479bc8a34e644134312a961ed95fd05fd56
[ "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
10,098
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johannes Hölzl -/ import data.list.basic import logic.relator import tactic.mk_iff_of_inductive_prop universes u v w z open nat function variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type z} namespace list /- forall₂ -/ variables {r : α → β → Prop} {p : γ → δ → Prop} open relator run_cmd tactic.mk_iff_of_inductive_prop `list.forall₂ `list.forall₂_iff @[simp] theorem forall₂_cons {R : α → β → Prop} {a b l₁ l₂} : forall₂ R (a::l₁) (b::l₂) ↔ R a b ∧ forall₂ R l₁ l₂ := ⟨λ h, by cases h with h₁ h₂; split; assumption, λ ⟨h₁, h₂⟩, forall₂.cons h₁ h₂⟩ theorem forall₂.imp {R S : α → β → Prop} (H : ∀ a b, R a b → S a b) {l₁ l₂} (h : forall₂ R l₁ l₂) : forall₂ S l₁ l₂ := by induction h; constructor; solve_by_elim lemma forall₂.mp {r q s : α → β → Prop} (h : ∀a b, r a b → q a b → s a b) : ∀{l₁ l₂}, forall₂ r l₁ l₂ → forall₂ q l₁ l₂ → forall₂ s l₁ l₂ | [] [] forall₂.nil forall₂.nil := forall₂.nil | (a::l₁) (b::l₂) (forall₂.cons hr hrs) (forall₂.cons hq hqs) := forall₂.cons (h a b hr hq) (forall₂.mp hrs hqs) lemma forall₂.flip : ∀{a b}, forall₂ (flip r) b a → forall₂ r a b | _ _ forall₂.nil := forall₂.nil | (a :: as) (b :: bs) (forall₂.cons h₁ h₂) := forall₂.cons h₁ h₂.flip lemma forall₂_same {r : α → α → Prop} : ∀{l}, (∀x∈l, r x x) → forall₂ r l l | [] _ := forall₂.nil | (a::as) h := forall₂.cons (h _ (mem_cons_self _ _)) (forall₂_same $ assume a ha, h a $ mem_cons_of_mem _ ha) lemma forall₂_refl {r} [is_refl α r] (l : list α) : forall₂ r l l := forall₂_same $ assume a h, is_refl.refl _ lemma forall₂_eq_eq_eq : forall₂ ((=) : α → α → Prop) = (=) := begin funext a b, apply propext, split, { assume h, induction h, {refl}, simp only [*]; split; refl }, { assume h, subst h, exact forall₂_refl _ } end @[simp] lemma forall₂_nil_left_iff {l} : forall₂ r nil l ↔ l = nil := ⟨λ H, by cases H; refl, by rintro rfl; exact forall₂.nil⟩ @[simp] lemma forall₂_nil_right_iff {l} : forall₂ r l nil ↔ l = nil := ⟨λ H, by cases H; refl, by rintro rfl; exact forall₂.nil⟩ lemma forall₂_cons_left_iff {a l u} : forall₂ r (a::l) u ↔ (∃b u', r a b ∧ forall₂ r l u' ∧ u = b :: u') := iff.intro (assume h, match u, h with (b :: u'), forall₂.cons h₁ h₂ := ⟨b, u', h₁, h₂, rfl⟩ end) (assume h, match u, h with _, ⟨b, u', h₁, h₂, rfl⟩ := forall₂.cons h₁ h₂ end) lemma forall₂_cons_right_iff {b l u} : forall₂ r u (b::l) ↔ (∃a u', r a b ∧ forall₂ r u' l ∧ u = a :: u') := iff.intro (assume h, match u, h with (b :: u'), forall₂.cons h₁ h₂ := ⟨b, u', h₁, h₂, rfl⟩ end) (assume h, match u, h with _, ⟨b, u', h₁, h₂, rfl⟩ := forall₂.cons h₁ h₂ end) lemma forall₂_and_left {r : α → β → Prop} {p : α → Prop} : ∀l u, forall₂ (λa b, p a ∧ r a b) l u ↔ (∀a∈l, p a) ∧ forall₂ r l u | [] u := by simp only [forall₂_nil_left_iff, forall_prop_of_false (not_mem_nil _), imp_true_iff, true_and] | (a::l) u := by simp only [forall₂_and_left l, forall₂_cons_left_iff, forall_mem_cons, and_assoc, and_comm, and.left_comm, exists_and_distrib_left.symm] @[simp] lemma forall₂_map_left_iff {f : γ → α} : ∀{l u}, forall₂ r (map f l) u ↔ forall₂ (λc b, r (f c) b) l u | [] _ := by simp only [map, forall₂_nil_left_iff] | (a::l) _ := by simp only [map, forall₂_cons_left_iff, forall₂_map_left_iff] @[simp] lemma forall₂_map_right_iff {f : γ → β} : ∀{l u}, forall₂ r l (map f u) ↔ forall₂ (λa c, r a (f c)) l u | _ [] := by simp only [map, forall₂_nil_right_iff] | _ (b::u) := by simp only [map, forall₂_cons_right_iff, forall₂_map_right_iff] lemma left_unique_forall₂ (hr : left_unique r) : left_unique (forall₂ r) | a₀ nil a₁ forall₂.nil forall₂.nil := rfl | (a₀::l₀) (b::l) (a₁::l₁) (forall₂.cons ha₀ h₀) (forall₂.cons ha₁ h₁) := hr ha₀ ha₁ ▸ left_unique_forall₂ h₀ h₁ ▸ rfl lemma right_unique_forall₂ (hr : right_unique r) : right_unique (forall₂ r) | nil a₀ a₁ forall₂.nil forall₂.nil := rfl | (b::l) (a₀::l₀) (a₁::l₁) (forall₂.cons ha₀ h₀) (forall₂.cons ha₁ h₁) := hr ha₀ ha₁ ▸ right_unique_forall₂ h₀ h₁ ▸ rfl lemma bi_unique_forall₂ (hr : bi_unique r) : bi_unique (forall₂ r) := ⟨assume a b c, left_unique_forall₂ hr.1, assume a b c, right_unique_forall₂ hr.2⟩ theorem forall₂_length_eq {R : α → β → Prop} : ∀ {l₁ l₂}, forall₂ R l₁ l₂ → length l₁ = length l₂ | _ _ forall₂.nil := rfl | _ _ (forall₂.cons h₁ h₂) := congr_arg succ (forall₂_length_eq h₂) theorem forall₂_zip {R : α → β → Prop} : ∀ {l₁ l₂}, forall₂ R l₁ l₂ → ∀ {a b}, (a, b) ∈ zip l₁ l₂ → R a b | _ _ (forall₂.cons h₁ h₂) x y (or.inl rfl) := h₁ | _ _ (forall₂.cons h₁ h₂) x y (or.inr h₃) := forall₂_zip h₂ h₃ theorem forall₂_iff_zip {R : α → β → Prop} {l₁ l₂} : forall₂ R l₁ l₂ ↔ length l₁ = length l₂ ∧ ∀ {a b}, (a, b) ∈ zip l₁ l₂ → R a b := ⟨λ h, ⟨forall₂_length_eq h, @forall₂_zip _ _ _ _ _ h⟩, λ h, begin cases h with h₁ h₂, induction l₁ with a l₁ IH generalizing l₂, { cases length_eq_zero.1 h₁.symm, constructor }, { cases l₂ with b l₂; injection h₁ with h₁, exact forall₂.cons (h₂ $ or.inl rfl) (IH h₁ $ λ a b h, h₂ $ or.inr h) } end⟩ theorem forall₂_take {R : α → β → Prop} : ∀ n {l₁ l₂}, forall₂ R l₁ l₂ → forall₂ R (take n l₁) (take n l₂) | 0 _ _ _ := by simp only [forall₂.nil, take] | (n+1) _ _ (forall₂.nil) := by simp only [forall₂.nil, take] | (n+1) _ _ (forall₂.cons h₁ h₂) := by simp [and.intro h₁ h₂, forall₂_take n] theorem forall₂_drop {R : α → β → Prop} : ∀ n {l₁ l₂}, forall₂ R l₁ l₂ → forall₂ R (drop n l₁) (drop n l₂) | 0 _ _ h := by simp only [drop, h] | (n+1) _ _ (forall₂.nil) := by simp only [forall₂.nil, drop] | (n+1) _ _ (forall₂.cons h₁ h₂) := by simp [and.intro h₁ h₂, forall₂_drop n] theorem forall₂_take_append {R : α → β → Prop} (l : list α) (l₁ : list β) (l₂ : list β) (h : forall₂ R l (l₁ ++ l₂)) : forall₂ R (list.take (length l₁) l) l₁ := have h': forall₂ R (take (length l₁) l) (take (length l₁) (l₁ ++ l₂)), from forall₂_take (length l₁) h, by rwa [take_left] at h' theorem forall₂_drop_append {R : α → β → Prop} (l : list α) (l₁ : list β) (l₂ : list β) (h : forall₂ R l (l₁ ++ l₂)) : forall₂ R (list.drop (length l₁) l) l₂ := have h': forall₂ R (drop (length l₁) l) (drop (length l₁) (l₁ ++ l₂)), from forall₂_drop (length l₁) h, by rwa [drop_left] at h' lemma rel_mem (hr : bi_unique r) : (r ⇒ forall₂ r ⇒ iff) (∈) (∈) | a b h [] [] forall₂.nil := by simp only [not_mem_nil] | a b h (a'::as) (b'::bs) (forall₂.cons h₁ h₂) := rel_or (rel_eq hr h h₁) (rel_mem h h₂) lemma rel_map : ((r ⇒ p) ⇒ forall₂ r ⇒ forall₂ p) map map | f g h [] [] forall₂.nil := forall₂.nil | f g h (a::as) (b::bs) (forall₂.cons h₁ h₂) := forall₂.cons (h h₁) (rel_map @h h₂) lemma rel_append : (forall₂ r ⇒ forall₂ r ⇒ forall₂ r) append append | [] [] h l₁ l₂ hl := hl | (a::as) (b::bs) (forall₂.cons h₁ h₂) l₁ l₂ hl := forall₂.cons h₁ (rel_append h₂ hl) lemma rel_join : (forall₂ (forall₂ r) ⇒ forall₂ r) join join | [] [] forall₂.nil := forall₂.nil | (a::as) (b::bs) (forall₂.cons h₁ h₂) := rel_append h₁ (rel_join h₂) lemma rel_bind : (forall₂ r ⇒ (r ⇒ forall₂ p) ⇒ forall₂ p) list.bind list.bind := assume a b h₁ f g h₂, rel_join (rel_map @h₂ h₁) lemma rel_foldl : ((p ⇒ r ⇒ p) ⇒ p ⇒ forall₂ r ⇒ p) foldl foldl | f g hfg _ _ h _ _ forall₂.nil := h | f g hfg x y hxy _ _ (forall₂.cons hab hs) := rel_foldl @hfg (hfg hxy hab) hs lemma rel_foldr : ((r ⇒ p ⇒ p) ⇒ p ⇒ forall₂ r ⇒ p) foldr foldr | f g hfg _ _ h _ _ forall₂.nil := h | f g hfg x y hxy _ _ (forall₂.cons hab hs) := hfg hab (rel_foldr @hfg hxy hs) lemma rel_filter {p : α → Prop} {q : β → Prop} [decidable_pred p] [decidable_pred q] (hpq : (r ⇒ (↔)) p q) : (forall₂ r ⇒ forall₂ r) (filter p) (filter q) | _ _ forall₂.nil := forall₂.nil | (a::as) (b::bs) (forall₂.cons h₁ h₂) := begin by_cases p a, { have : q b, { rwa [← hpq h₁] }, simp only [filter_cons_of_pos _ h, filter_cons_of_pos _ this, forall₂_cons, h₁, rel_filter h₂, and_true], }, { have : ¬ q b, { rwa [← hpq h₁] }, simp only [filter_cons_of_neg _ h, filter_cons_of_neg _ this, rel_filter h₂], }, end theorem filter_map_cons (f : α → option β) (a : α) (l : list α) : filter_map f (a :: l) = option.cases_on (f a) (filter_map f l) (λb, b :: filter_map f l) := begin generalize eq : f a = b, cases b, { rw filter_map_cons_none _ _ eq }, { rw filter_map_cons_some _ _ _ eq }, end lemma rel_filter_map : ((r ⇒ option.rel p) ⇒ forall₂ r ⇒ forall₂ p) filter_map filter_map | f g hfg _ _ forall₂.nil := forall₂.nil | f g hfg (a::as) (b::bs) (forall₂.cons h₁ h₂) := by rw [filter_map_cons, filter_map_cons]; from match f a, g b, hfg h₁ with | _, _, option.rel.none := rel_filter_map @hfg h₂ | _, _, option.rel.some h := forall₂.cons h (rel_filter_map @hfg h₂) end @[to_additive] lemma rel_prod [monoid α] [monoid β] (h : r 1 1) (hf : (r ⇒ r ⇒ r) (*) (*)) : (forall₂ r ⇒ r) prod prod := rel_foldl hf h end list
f1c545a2e277033b7ff05f930f475f1c34101915
bdb33f8b7ea65f7705fc342a178508e2722eb851
/analysis/topology/uniform_space.lean
61ec198daf53f2589f244b31bf4dfc4e5c50ba05
[ "Apache-2.0" ]
permissive
rwbarton/mathlib
939ae09bf8d6eb1331fc2f7e067d39567e10e33d
c13c5ea701bb1eec057e0a242d9f480a079105e9
refs/heads/master
1,584,015,335,862
1,524,142,167,000
1,524,142,167,000
130,614,171
0
0
Apache-2.0
1,548,902,667,000
1,524,437,371,000
Lean
UTF-8
Lean
false
false
77,788
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 Theory of uniform spaces. Uniform spaces are a generalization of metric spaces and topological groups. Many concepts directly generalize to uniform spaces, e.g. * completeness * completion (on Cauchy filters instead of Cauchy sequences) * extension of uniform continuous functions to complete spaces * uniform contiunuity & embedding * totally bounded * totally bounded ∧ complete → compact One reason to directly formalize uniform spaces is foundational: we define ℝ as a completion of ℚ. The central concept of uniform spaces is its uniformity: a filter relating two elements of the space. This filter is reflexive, symmetric and transitive. So a set (i.e. a relation) in this filter represents a 'distance': it is reflexive, symmetric and the uniformity contains a set for which the `triangular` rule holds. The formalization is mostly based on the books: N. Bourbaki: General Topology I. M. James: Topologies and Uniformities A major difference is that this formalization is heavily based on the filter library. -/ import order.filter data.quot analysis.topology.topological_space analysis.topology.continuity open set lattice filter classical local attribute [instance] prop_decidable set_option eqn_compiler.zeta true universes u section variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {ι : Sort*} /-- The identity relation, or the graph of the identity function -/ def id_rel {α : Type*} := {p : α × α | p.1 = p.2} @[simp] theorem mem_id_rel {a b : α} : (a, b) ∈ @id_rel α ↔ a = b := iff.rfl @[simp] theorem id_rel_subset {s : set (α × α)} : id_rel ⊆ s ↔ ∀ a, (a, a) ∈ s := by simp [subset_def]; exact forall_congr (λ a, by simp) /-- The composition of relations -/ def comp_rel {α : Type u} (r₁ r₂ : set (α×α)) := {p : α × α | ∃z:α, (p.1, z) ∈ r₁ ∧ (z, p.2) ∈ r₂} @[simp] theorem mem_comp_rel {r₁ r₂ : set (α×α)} {x y : α} : (x, y) ∈ comp_rel r₁ r₂ ↔ ∃ z, (x, z) ∈ r₁ ∧ (z, y) ∈ r₂ := iff.rfl @[simp] theorem swap_id_rel : prod.swap '' id_rel = @id_rel α := set.ext $ assume ⟨a, b⟩, by simp [image_swap_eq_preimage_swap]; exact eq_comm theorem monotone_comp_rel [preorder β] {f g : β → set (α×α)} (hf : monotone f) (hg : monotone g) : monotone (λx, comp_rel (f x) (g x)) := assume a b h p ⟨z, h₁, h₂⟩, ⟨z, hf h h₁, hg h h₂⟩ lemma prod_mk_mem_comp_rel {a b c : α} {s t : set (α×α)} (h₁ : (a, c) ∈ s) (h₂ : (c, b) ∈ t) : (a, b) ∈ comp_rel s t := ⟨c, h₁, h₂⟩ @[simp] lemma id_comp_rel {r : set (α×α)} : comp_rel id_rel r = r := set.ext $ assume ⟨a, b⟩, by simp /-- This core description of a uniform space is outside of the type class hierarchy. It is useful for constructions of uniform spaces, when the topology is derived from the uniform space. -/ structure uniform_space.core (α : Type u) := (uniformity : filter (α × α)) (refl : principal id_rel ≤ uniformity) (symm : tendsto prod.swap uniformity uniformity) (comp : uniformity.lift' (λs, comp_rel s s) ≤ uniformity) def uniform_space.core.mk' {α : Type u} (U : filter (α × α)) (refl : ∀ (r ∈ U.sets) x, (x, x) ∈ r) (symm : ∀ r ∈ U.sets, {p | prod.swap p ∈ r} ∈ U.sets) (comp : ∀ r ∈ U.sets, ∃ t ∈ U.sets, comp_rel t t ⊆ r) : uniform_space.core α := ⟨U, λ r ru, id_rel_subset.2 (refl _ ru), symm, begin intros r ru, rw [mem_lift'_sets], exact comp _ ru, apply monotone_comp_rel; exact monotone_id, end⟩ /-- A uniform space generates a topological space -/ def uniform_space.core.to_topological_space {α : Type u} (u : uniform_space.core α) : topological_space α := { is_open := λs, ∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ u.uniformity.sets, is_open_univ := by simp; intro; exact univ_mem_sets, is_open_inter := assume s t hs ht x ⟨xs, xt⟩, by filter_upwards [hs x xs, ht x xt]; simp {contextual := tt}, is_open_sUnion := assume s hs x ⟨t, ts, xt⟩, by filter_upwards [hs t ts x xt] assume p ph h, ⟨t, ts, ph h⟩ } lemma uniform_space.core_eq : ∀{u₁ u₂ : uniform_space.core α}, u₁.uniformity = u₂.uniformity → u₁ = u₂ | ⟨u₁, _, _, _⟩ ⟨u₂, _, _, _⟩ h := have u₁ = u₂, from h, by simp [*] /-- A uniform space is a generalization of the "uniform" topological aspects of a metric space. It consists of a filter on `α × α` called the "uniformity", which satisfies properties analogous to the reflexivity, symmetry, and triangle properties of a metric. A metric space has a natural uniformity, and a uniform space has a natural topology. A topological group also has a natural uniformity, even when it is not metrizable. -/ class uniform_space (α : Type u) extends topological_space α, uniform_space.core α := (is_open_uniformity : ∀s, is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ uniformity.sets)) @[pattern] def uniform_space.mk' {α} (t : topological_space α) (c : uniform_space.core α) (is_open_uniformity : ∀s:set α, t.is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ c.uniformity.sets)) : uniform_space α := ⟨c, is_open_uniformity⟩ def uniform_space.of_core {α : Type u} (u : uniform_space.core α) : uniform_space α := { to_core := u, to_topological_space := u.to_topological_space, is_open_uniformity := assume a, iff.refl _ } def uniform_space.of_core_eq {α : Type u} (u : uniform_space.core α) (t : topological_space α) (h : t = u.to_topological_space) : uniform_space α := { to_core := u, to_topological_space := t, is_open_uniformity := assume a, h.symm ▸ iff.refl _ } lemma uniform_space.to_core_to_topological_space (u : uniform_space α) : u.to_core.to_topological_space = u.to_topological_space := topological_space_eq $ funext $ assume s, by rw [uniform_space.core.to_topological_space, uniform_space.is_open_uniformity] lemma uniform_space_eq : ∀{u₁ u₂ : uniform_space α}, u₁.uniformity = u₂.uniformity → u₁ = u₂ | (uniform_space.mk' t₁ u₁ o₁) (uniform_space.mk' t₂ u₂ o₂) h := have u₁ = u₂, from uniform_space.core_eq h, have t₁ = t₂, from topological_space_eq $ funext $ assume s, by rw [o₁, o₂]; simp [this], by simp [*] lemma uniform_space.of_core_eq_to_core (u : uniform_space α) (t : topological_space α) (h : t = u.to_core.to_topological_space) : uniform_space.of_core_eq u.to_core t h = u := uniform_space_eq rfl section uniform_space variables [uniform_space α] /-- The uniformity is a filter on α × α (inferred from an ambient uniform space structure on α). -/ def uniformity : filter (α × α) := (@uniform_space.to_core α _).uniformity lemma is_open_uniformity {s : set α} : is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ (@uniformity α _).sets) := uniform_space.is_open_uniformity s lemma refl_le_uniformity : principal id_rel ≤ @uniformity α _ := (@uniform_space.to_core α _).refl lemma refl_mem_uniformity {x : α} {s : set (α × α)} (h : s ∈ (@uniformity α _).sets) : (x, x) ∈ s := refl_le_uniformity h rfl lemma symm_le_uniformity : map (@prod.swap α α) uniformity ≤ uniformity := (@uniform_space.to_core α _).symm lemma comp_le_uniformity : uniformity.lift' (λs:set (α×α), comp_rel s s) ≤ uniformity := (@uniform_space.to_core α _).comp lemma tendsto_swap_uniformity : tendsto prod.swap (@uniformity α _) uniformity := symm_le_uniformity lemma tendsto_const_uniformity {a : α} {f : filter β} : tendsto (λ_, (a, a)) f uniformity := assume s hs, show {x | (a, a) ∈ s} ∈ f.sets, from univ_mem_sets' $ assume b, refl_mem_uniformity hs lemma comp_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ (@uniformity α _).sets) : ∃t∈(@uniformity α _).sets, comp_rel t t ⊆ s := have s ∈ (uniformity.lift' (λt:set (α×α), comp_rel t t)).sets, from comp_le_uniformity hs, (mem_lift'_sets $ monotone_comp_rel monotone_id monotone_id).mp this lemma symm_of_uniformity {s : set (α × α)} (hs : s ∈ (@uniformity α _).sets) : ∃t∈(@uniformity α _).sets, (∀a b, (a, b) ∈ t → (b, a) ∈ t) ∧ t ⊆ s := have preimage prod.swap s ∈ (@uniformity α _).sets, from symm_le_uniformity hs, ⟨s ∩ preimage prod.swap s, inter_mem_sets hs this, assume a b ⟨h₁, h₂⟩, ⟨h₂, h₁⟩, inter_subset_left _ _⟩ lemma comp_symm_of_uniformity {s : set (α × α)} (hs : s ∈ (@uniformity α _).sets) : ∃t∈(@uniformity α _).sets, (∀{a b}, (a, b) ∈ t → (b, a) ∈ t) ∧ comp_rel t t ⊆ s := let ⟨t, ht₁, ht₂⟩ := comp_mem_uniformity_sets hs in let ⟨t', ht', ht'₁, ht'₂⟩ := symm_of_uniformity ht₁ in ⟨t', ht', ht'₁, subset.trans (monotone_comp_rel monotone_id monotone_id ht'₂) ht₂⟩ lemma uniformity_le_symm : uniformity ≤ (@prod.swap α α) <$> uniformity := by rw [map_swap_eq_vmap_swap]; from map_le_iff_le_vmap.1 tendsto_swap_uniformity lemma uniformity_eq_symm : uniformity = (@prod.swap α α) <$> uniformity := le_antisymm uniformity_le_symm symm_le_uniformity theorem uniformity_lift_le_swap {g : set (α×α) → filter β} {f : filter β} (hg : monotone g) (h : uniformity.lift (λs, g (preimage prod.swap s)) ≤ f) : uniformity.lift g ≤ f := calc uniformity.lift g ≤ (filter.map prod.swap (@uniformity α _)).lift g : lift_mono uniformity_le_symm (le_refl _) ... ≤ _ : by rw [map_lift_eq2 hg, image_swap_eq_preimage_swap]; exact h lemma uniformity_lift_le_comp {f : set (α×α) → filter β} (h : monotone f): uniformity.lift (λs, f (comp_rel s s)) ≤ uniformity.lift f := calc uniformity.lift (λs, f (comp_rel s s)) = (uniformity.lift' (λs:set (α×α), comp_rel s s)).lift f : begin rw [lift_lift'_assoc], exact monotone_comp_rel monotone_id monotone_id, exact h end ... ≤ uniformity.lift f : lift_mono comp_le_uniformity (le_refl _) lemma comp_le_uniformity3 : uniformity.lift' (λs:set (α×α), comp_rel s (comp_rel s s)) ≤ uniformity := calc uniformity.lift' (λd, comp_rel d (comp_rel d d)) = uniformity.lift (λs, uniformity.lift' (λt:set(α×α), comp_rel s (comp_rel t t))) : begin rw [lift_lift'_same_eq_lift'], exact (assume x, monotone_comp_rel monotone_const $ monotone_comp_rel monotone_id monotone_id), exact (assume x, monotone_comp_rel monotone_id monotone_const), end ... ≤ uniformity.lift (λs, uniformity.lift' (λt:set(α×α), comp_rel s t)) : lift_mono' $ assume s hs, @uniformity_lift_le_comp α _ _ (principal ∘ comp_rel s) $ monotone_comp (monotone_comp_rel monotone_const monotone_id) monotone_principal ... = uniformity.lift' (λs:set(α×α), comp_rel s s) : lift_lift'_same_eq_lift' (assume s, monotone_comp_rel monotone_const monotone_id) (assume s, monotone_comp_rel monotone_id monotone_const) ... ≤ uniformity : comp_le_uniformity lemma mem_nhds_uniformity_iff {x : α} {s : set α} : (s ∈ (nhds x).sets) ↔ ({p : α × α | p.1 = x → p.2 ∈ s} ∈ (@uniformity α _).sets) := ⟨ begin simp [mem_nhds_sets_iff, is_open_uniformity], exact assume t ts ht xt, by filter_upwards [ht x xt] assume ⟨x', y⟩ h eq, ts $ h eq end, assume hs, mem_nhds_sets_iff.mpr ⟨{x | {p : α × α | p.1 = x → p.2 ∈ s} ∈ (@uniformity α _).sets}, assume x' hx', refl_mem_uniformity hx' rfl, is_open_uniformity.mpr $ assume x' hx', let ⟨t, ht, tr⟩ := comp_mem_uniformity_sets hx' in by filter_upwards [ht] assume ⟨a, b⟩ hp' (hax' : a = x'), by filter_upwards [ht] assume ⟨a, b'⟩ hp'' (hab : a = b), have hp : (x', b) ∈ t, from hax' ▸ hp', have (b, b') ∈ t, from hab ▸ hp'', have (x', b') ∈ comp_rel t t, from ⟨b, hp, this⟩, show b' ∈ s, from tr this rfl, hs⟩⟩ lemma nhds_eq_vmap_uniformity {x : α} : nhds x = uniformity.vmap (prod.mk x) := filter.ext.2 $ assume s, by rw [mem_nhds_uniformity_iff, mem_vmap_sets]; from iff.intro (assume hs, ⟨_, hs, assume x hx, hx rfl⟩) (assume ⟨t, h, ht⟩, uniformity.upwards_sets h $ assume ⟨p₁, p₂⟩ hp (h : p₁ = x), ht $ by simp [h.symm, hp]) lemma nhds_eq_uniformity {x : α} : nhds x = uniformity.lift' (λs:set (α×α), {y | (x, y) ∈ s}) := filter_eq $ set.ext $ assume s, begin rw [mem_lift'_sets], tactic.swap, apply monotone_preimage, simp [mem_nhds_uniformity_iff], exact ⟨assume h, ⟨_, h, assume y h, h rfl⟩, assume ⟨t, h₁, h₂⟩, uniformity.upwards_sets h₁ $ assume ⟨x', y⟩ hp (eq : x' = x), h₂ $ show (x, y) ∈ t, from eq ▸ hp⟩ end lemma mem_nhds_left (x : α) {s : set (α×α)} (h : s ∈ (uniformity.sets : set (set (α×α)))) : {y : α | (x, y) ∈ s} ∈ (nhds x).sets := have nhds x ≤ principal {y : α | (x, y) ∈ s}, by rw [nhds_eq_uniformity]; exact infi_le_of_le s (infi_le _ h), by simp at this; assumption lemma mem_nhds_right (y : α) {s : set (α×α)} (h : s ∈ (uniformity.sets : set (set (α×α)))) : {x : α | (x, y) ∈ s} ∈ (nhds y).sets := mem_nhds_left _ (symm_le_uniformity h) lemma tendsto_right_nhds_uniformity {a : α} : tendsto (λa', (a', a)) (nhds a) uniformity := assume s, mem_nhds_right a lemma tendsto_left_nhds_uniformity {a : α} : tendsto (λa', (a, a')) (nhds a) uniformity := assume s, mem_nhds_left a lemma lift_nhds_left {x : α} {g : set α → filter β} (hg : monotone g) : (nhds x).lift g = uniformity.lift (λs:set (α×α), g {y | (x, y) ∈ s}) := eq.trans begin rw [nhds_eq_uniformity], exact (filter.lift_assoc $ monotone_comp monotone_preimage $ monotone_comp monotone_preimage monotone_principal) end (congr_arg _ $ funext $ assume s, filter.lift_principal hg) lemma lift_nhds_right {x : α} {g : set α → filter β} (hg : monotone g) : (nhds x).lift g = uniformity.lift (λs:set (α×α), g {y | (y, x) ∈ s}) := calc (nhds x).lift g = uniformity.lift (λs:set (α×α), g {y | (x, y) ∈ s}) : lift_nhds_left hg ... = ((@prod.swap α α) <$> uniformity).lift (λs:set (α×α), g {y | (x, y) ∈ s}) : by rw [←uniformity_eq_symm] ... = uniformity.lift (λs:set (α×α), g {y | (x, y) ∈ image prod.swap s}) : map_lift_eq2 $ monotone_comp monotone_preimage hg ... = _ : by simp [image_swap_eq_preimage_swap] lemma nhds_nhds_eq_uniformity_uniformity_prod {a b : α} : filter.prod (nhds a) (nhds b) = uniformity.lift (λs:set (α×α), uniformity.lift' (λt:set (α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (b, y) ∈ t})) := begin rw [prod_def], show (nhds a).lift (λs:set α, (nhds b).lift (λt:set α, principal (set.prod s t))) = _, rw [lift_nhds_right], apply congr_arg, funext s, rw [lift_nhds_left], refl, exact monotone_comp (monotone_prod monotone_const monotone_id) monotone_principal, exact (monotone_lift' monotone_const $ monotone_lam $ assume x, monotone_prod monotone_id monotone_const) end lemma nhds_eq_uniformity_prod {a b : α} : nhds (a, b) = uniformity.lift' (λs:set (α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (b, y) ∈ s}) := begin rw [nhds_prod_eq, nhds_nhds_eq_uniformity_uniformity_prod, lift_lift'_same_eq_lift'], { intro s, exact monotone_prod monotone_const monotone_preimage }, { intro t, exact monotone_prod monotone_preimage monotone_const } end lemma nhdset_of_mem_uniformity {d : set (α×α)} (s : set (α×α)) (hd : d ∈ (@uniformity α _).sets) : ∃(t : set (α×α)), is_open t ∧ s ⊆ t ∧ t ⊆ {p | ∃x y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} := let cl_d := {p:α×α | ∃x y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} in have ∀p ∈ s, ∃t ⊆ cl_d, is_open t ∧ p ∈ t, from assume ⟨x, y⟩ hp, mem_nhds_sets_iff.mp $ show cl_d ∈ (nhds (x, y)).sets, begin rw [nhds_eq_uniformity_prod, mem_lift'_sets], exact ⟨d, hd, assume ⟨a, b⟩ ⟨ha, hb⟩, ⟨x, y, ha, hp, hb⟩⟩, exact monotone_prod monotone_preimage monotone_preimage end, have ∃t:(Π(p:α×α) (h:p ∈ s), set (α×α)), ∀p, ∀h:p ∈ s, t p h ⊆ cl_d ∧ is_open (t p h) ∧ p ∈ t p h, by simp [classical.skolem] at this; simp; assumption, match this with | ⟨t, ht⟩ := ⟨(⋃ p:α×α, ⋃ h : p ∈ s, t p h : set (α×α)), is_open_Union $ assume (p:α×α), is_open_Union $ assume hp, (ht p hp).right.left, assume ⟨a, b⟩ hp, begin simp; exact ⟨a, b, hp, (ht (a,b) hp).right.right⟩ end, Union_subset $ assume p, Union_subset $ assume hp, (ht p hp).left⟩ end lemma closure_eq_inter_uniformity {t : set (α×α)} : closure t = (⋂ d∈(@uniformity α _).sets, comp_rel d (comp_rel t d)) := set.ext $ assume ⟨a, b⟩, calc (a, b) ∈ closure t ↔ (nhds (a, b) ⊓ principal t ≠ ⊥) : by simp [closure_eq_nhds] ... ↔ (((@prod.swap α α) <$> uniformity).lift' (λ (s : set (α × α)), set.prod {x : α | (x, a) ∈ s} {y : α | (b, y) ∈ s}) ⊓ principal t ≠ ⊥) : by rw [←uniformity_eq_symm, nhds_eq_uniformity_prod] ... ↔ ((map (@prod.swap α α) uniformity).lift' (λ (s : set (α × α)), set.prod {x : α | (x, a) ∈ s} {y : α | (b, y) ∈ s}) ⊓ principal t ≠ ⊥) : by refl ... ↔ (uniformity.lift' (λ (s : set (α × α)), set.prod {y : α | (a, y) ∈ s} {x : α | (x, b) ∈ s}) ⊓ principal t ≠ ⊥) : begin rw [map_lift'_eq2], simp [image_swap_eq_preimage_swap, function.comp], exact monotone_prod monotone_preimage monotone_preimage end ... ↔ (∀s∈(@uniformity α _).sets, ∃x, x ∈ set.prod {y : α | (a, y) ∈ s} {x : α | (x, b) ∈ s} ∩ t) : begin rw [lift'_inf_principal_eq, lift'_neq_bot_iff], apply forall_congr, intro s, rw [ne_empty_iff_exists_mem], exact monotone_inter (monotone_prod monotone_preimage monotone_preimage) monotone_const end ... ↔ (∀s∈(@uniformity α _).sets, (a, b) ∈ comp_rel s (comp_rel t s)) : forall_congr $ assume s, forall_congr $ assume hs, ⟨assume ⟨⟨x, y⟩, ⟨⟨hx, hy⟩, hxyt⟩⟩, ⟨x, hx, y, hxyt, hy⟩, assume ⟨x, hx, y, hxyt, hy⟩, ⟨⟨x, y⟩, ⟨⟨hx, hy⟩, hxyt⟩⟩⟩ ... ↔ _ : by simp lemma uniformity_eq_uniformity_closure : (@uniformity α _) = uniformity.lift' closure := le_antisymm (le_infi $ assume s, le_infi $ assume hs, by simp; filter_upwards [hs] subset_closure) (calc uniformity.lift' closure ≤ uniformity.lift' (λd, comp_rel d (comp_rel d d)) : lift'_mono' (by intros s hs; rw [closure_eq_inter_uniformity]; exact bInter_subset_of_mem hs) ... ≤ uniformity : comp_le_uniformity3) lemma uniformity_eq_uniformity_interior : (@uniformity α _) = uniformity.lift' interior := le_antisymm (le_infi $ assume d, le_infi $ assume hd, let ⟨s, hs, hs_comp⟩ := (mem_lift'_sets $ monotone_comp_rel monotone_id $ monotone_comp_rel monotone_id monotone_id).mp (comp_le_uniformity3 hd) in let ⟨t, ht, hst, ht_comp⟩ := nhdset_of_mem_uniformity s hs in have s ⊆ interior d, from calc s ⊆ t : hst ... ⊆ interior d : (subset_interior_iff_subset_of_open ht).mpr $ assume x, assume : x ∈ t, let ⟨x, y, h₁, h₂, h₃⟩ := ht_comp this in hs_comp ⟨x, h₁, y, h₂, h₃⟩, have interior d ∈ (@uniformity α _).sets, by filter_upwards [hs] this, by simp [this]) (assume s hs, (uniformity.lift' interior).upwards_sets (mem_lift' hs) interior_subset) lemma interior_mem_uniformity {s : set (α × α)} (hs : s ∈ (@uniformity α _).sets) : interior s ∈ (@uniformity α _).sets := by rw [uniformity_eq_uniformity_interior]; exact mem_lift' hs lemma mem_uniformity_is_closed [uniform_space α] {s : set (α×α)} (h : s ∈ (@uniformity α _).sets) : ∃t∈(@uniformity α _).sets, is_closed t ∧ t ⊆ s := have s ∈ ((@uniformity α _).lift' closure).sets, by rwa [uniformity_eq_uniformity_closure] at h, have ∃t∈(@uniformity α _).sets, closure t ⊆ s, by rwa [mem_lift'_sets] at this; apply closure_mono, let ⟨t, ht, hst⟩ := this in ⟨closure t, uniformity.upwards_sets ht subset_closure, is_closed_closure, hst⟩ /- uniform continuity -/ def uniform_continuous [uniform_space β] (f : α → β) := tendsto (λx:α×α, (f x.1, f x.2)) uniformity uniformity theorem uniform_continuous_def [uniform_space β] {f : α → β} : uniform_continuous f ↔ ∀ r ∈ (@uniformity β _).sets, {x : α × α | (f x.1, f x.2) ∈ r} ∈ (@uniformity α _).sets := iff.rfl lemma uniform_continuous_id : uniform_continuous (@id α) := by simp [uniform_continuous]; exact tendsto_id lemma uniform_continuous_const [uniform_space β] {b : β} : uniform_continuous (λa:α, b) := @tendsto_const_uniformity _ _ _ b uniformity lemma uniform_continuous.comp [uniform_space β] [uniform_space γ] {f : α → β} {g : β → γ} (hf : uniform_continuous f) (hg : uniform_continuous g) : uniform_continuous (g ∘ f) := hf.comp hg def uniform_embedding [uniform_space β] (f : α → β) := function.injective f ∧ vmap (λx:α×α, (f x.1, f x.2)) uniformity = uniformity theorem uniform_embedding_def [uniform_space β] {f : α → β} : uniform_embedding f ↔ function.injective f ∧ ∀ s, s ∈ (@uniformity α _).sets ↔ ∃ t ∈ (@uniformity β _).sets, ∀ x y : α, (f x, f y) ∈ t → (x, y) ∈ s := by rw [uniform_embedding, eq_comm, filter.ext]; simp [subset_def] theorem uniform_embedding_def' [uniform_space β] {f : α → β} : uniform_embedding f ↔ function.injective f ∧ uniform_continuous f ∧ ∀ s, s ∈ (@uniformity α _).sets → ∃ t ∈ (@uniformity β _).sets, ∀ x y : α, (f x, f y) ∈ t → (x, y) ∈ s := by simp [uniform_embedding_def, uniform_continuous_def]; exact ⟨λ ⟨I, H⟩, ⟨I, λ s su, (H _).2 ⟨s, su, λ x y, id⟩, λ s, (H s).1⟩, λ ⟨I, H₁, H₂⟩, ⟨I, λ s, ⟨H₂ s, λ ⟨t, tu, h⟩, upwards_sets _ (H₁ t tu) (λ ⟨a, b⟩, h a b)⟩⟩⟩ lemma uniform_embedding.uniform_continuous [uniform_space β] {f : α → β} (hf : uniform_embedding f) : uniform_continuous f := (uniform_embedding_def'.1 hf).2.1 lemma uniform_embedding.uniform_continuous_iff [uniform_space β] [uniform_space γ] {f : α → β} {g : β → γ} (hg : uniform_embedding g) : uniform_continuous f ↔ uniform_continuous (g ∘ f) := by simp [uniform_continuous, tendsto]; rw [← hg.2, ← map_le_iff_le_vmap, map_map] lemma uniform_embedding.dense_embedding [uniform_space β] {f : α → β} (h : uniform_embedding f) (hd : ∀x, x ∈ closure (range f)) : dense_embedding f := { dense := hd, inj := h.left, induced := begin intro a, simp [h.right.symm, nhds_eq_uniformity], rw [vmap_lift'_eq, vmap_lift'_eq2], refl, exact monotone_preimage, exact monotone_preimage end } lemma uniform_continuous.continuous [uniform_space β] {f : α → β} (hf : uniform_continuous f) : continuous f := continuous_iff_tendsto.mpr $ assume a, calc map f (nhds a) ≤ (map (λp:α×α, (f p.1, f p.2)) uniformity).lift' (λs:set (β×β), {y | (f a, y) ∈ s}) : begin rw [nhds_eq_uniformity, map_lift'_eq, map_lift'_eq2], exact (lift'_mono' $ assume s hs b ⟨a', (ha' : (_, a') ∈ s), a'_eq⟩, ⟨(a, a'), ha', show (f a, f a') = (f a, b), from a'_eq ▸ rfl⟩), exact monotone_preimage, exact monotone_preimage end ... ≤ nhds (f a) : by rw [nhds_eq_uniformity]; exact lift'_mono hf (le_refl _) lemma closure_image_mem_nhds_of_uniform_embedding [uniform_space α] [uniform_space β] {s : set (α×α)} {e : α → β} (b : β) (he₁ : uniform_embedding e) (he₂ : dense_embedding e) (hs : s ∈ (@uniformity α _).sets) : ∃a, closure (e '' {a' | (a, a') ∈ s}) ∈ (nhds b).sets := have s ∈ (vmap (λp:α×α, (e p.1, e p.2)) $ uniformity).sets, from he₁.right.symm ▸ hs, let ⟨t₁, ht₁u, ht₁⟩ := this in have ht₁ : ∀p:α×α, (e p.1, e p.2) ∈ t₁ → p ∈ s, from ht₁, let ⟨t₂, ht₂u, ht₂s, ht₂c⟩ := comp_symm_of_uniformity ht₁u in let ⟨t, htu, hts, htc⟩ := comp_symm_of_uniformity ht₂u in have preimage e {b' | (b, b') ∈ t₂} ∈ (vmap e $ nhds b).sets, from preimage_mem_vmap $ mem_nhds_left b ht₂u, let ⟨a, (ha : (b, e a) ∈ t₂)⟩ := inhabited_of_mem_sets (he₂.vmap_nhds_neq_bot) this in have ∀b' (s' : set (β × β)), (b, b') ∈ t → s' ∈ (@uniformity β _).sets → {y : β | (b', y) ∈ s'} ∩ e '' {a' : α | (a, a') ∈ s} ≠ ∅, from assume b' s' hb' hs', have preimage e {b'' | (b', b'') ∈ s' ∩ t} ∈ (vmap e $ nhds b').sets, from preimage_mem_vmap $ mem_nhds_left b' $ inter_mem_sets hs' htu, let ⟨a₂, ha₂s', ha₂t⟩ := inhabited_of_mem_sets (he₂.vmap_nhds_neq_bot) this in have (e a, e a₂) ∈ t₁, from ht₂c $ prod_mk_mem_comp_rel (ht₂s ha) $ htc $ prod_mk_mem_comp_rel hb' ha₂t, have e a₂ ∈ {b'':β | (b', b'') ∈ s'} ∩ e '' {a' | (a, a') ∈ s}, from ⟨ha₂s', mem_image_of_mem _ $ ht₁ (a, a₂) this⟩, ne_empty_of_mem this, have ∀b', (b, b') ∈ t → nhds b' ⊓ principal (e '' {a' | (a, a') ∈ s}) ≠ ⊥, begin intros b' hb', rw [nhds_eq_uniformity, lift'_inf_principal_eq, lift'_neq_bot_iff], exact assume s, this b' s hb', exact monotone_inter monotone_preimage monotone_const end, have ∀b', (b, b') ∈ t → b' ∈ closure (e '' {a' | (a, a') ∈ s}), from assume b' hb', by rw [closure_eq_nhds]; exact this b' hb', ⟨a, (nhds b).upwards_sets (mem_nhds_left b htu) this⟩ /-- A filter `f` is Cauchy if for every entourage `r`, there exists an `s ∈ f` such that `s × s ⊆ r`. This is a generalization of Cauchy sequences, because if `a : ℕ → α` then the filter of sets containing cofinitely many of the `a n` is Cauchy iff `a` is a Cauchy sequence. -/ def cauchy (f : filter α) := f ≠ ⊥ ∧ filter.prod f f ≤ uniformity lemma cauchy_iff [uniform_space α] {f : filter α} : cauchy f ↔ (f ≠ ⊥ ∧ (∀s∈(@uniformity α _).sets, ∃t∈f.sets, set.prod t t ⊆ s)) := and_congr (iff.refl _) $ forall_congr $ assume s, forall_congr $ assume hs, mem_prod_same_iff lemma cauchy_downwards {f g : filter α} (h_c : cauchy f) (hg : g ≠ ⊥) (h_le : g ≤ f) : cauchy g := ⟨hg, le_trans (filter.prod_mono h_le h_le) h_c.right⟩ lemma cauchy_nhds {a : α} : cauchy (nhds a) := ⟨nhds_neq_bot, calc filter.prod (nhds a) (nhds a) = uniformity.lift (λs:set (α×α), uniformity.lift' (λt:set(α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (a, y) ∈ t})) : nhds_nhds_eq_uniformity_uniformity_prod ... ≤ uniformity.lift' (λs:set (α×α), comp_rel s s) : le_infi $ assume s, le_infi $ assume hs, infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le s $ infi_le_of_le hs $ principal_mono.mpr $ assume ⟨x, y⟩ ⟨(hx : (x, a) ∈ s), (hy : (a, y) ∈ s)⟩, ⟨a, hx, hy⟩ ... ≤ uniformity : comp_le_uniformity⟩ lemma cauchy_pure {a : α} : cauchy (pure a) := cauchy_downwards cauchy_nhds (show principal {a} ≠ ⊥, by simp) (return_le_nhds a) lemma le_nhds_of_cauchy_adhp {f : filter α} {x : α} (hf : cauchy f) (adhs : f ⊓ nhds x ≠ ⊥) : f ≤ nhds x := have ∀s∈f.sets, x ∈ closure s, begin intros s hs, simp [closure_eq_nhds, inf_comm], exact assume h', adhs $ bot_unique $ h' ▸ inf_le_inf (by simp; exact hs) (le_refl _) end, calc f ≤ f.lift' (λs:set α, {y | x ∈ closure s ∧ y ∈ closure s}) : le_infi $ assume s, le_infi $ assume hs, begin rw [←forall_sets_neq_empty_iff_neq_bot] at adhs, simp [this s hs], exact f.upwards_sets hs subset_closure end ... ≤ f.lift' (λs:set α, {y | (x, y) ∈ closure (set.prod s s)}) : by simp [closure_prod_eq]; exact le_refl _ ... = (filter.prod f f).lift' (λs:set (α×α), {y | (x, y) ∈ closure s}) : begin rw [prod_same_eq], rw [lift'_lift'_assoc], exact monotone_prod monotone_id monotone_id, exact monotone_comp (assume s t h x h', closure_mono h h') monotone_preimage end ... ≤ uniformity.lift' (λs:set (α×α), {y | (x, y) ∈ closure s}) : lift'_mono hf.right (le_refl _) ... = (uniformity.lift' closure).lift' (λs:set (α×α), {y | (x, y) ∈ s}) : begin rw [lift'_lift'_assoc], exact assume s t h, closure_mono h, exact monotone_preimage end ... = uniformity.lift' (λs:set (α×α), {y | (x, y) ∈ s}) : by rw [←uniformity_eq_uniformity_closure] ... = nhds x : by rw [nhds_eq_uniformity] lemma le_nhds_iff_adhp_of_cauchy {f : filter α} {x : α} (hf : cauchy f) : f ≤ nhds x ↔ f ⊓ nhds x ≠ ⊥ := ⟨assume h, (inf_of_le_left h).symm ▸ hf.left, le_nhds_of_cauchy_adhp hf⟩ lemma cauchy_map [uniform_space β] {f : filter α} {m : α → β} (hm : uniform_continuous m) (hf : cauchy f) : cauchy (map m f) := ⟨have f ≠ ⊥, from hf.left, by simp; assumption, calc filter.prod (map m f) (map m f) = map (λp:α×α, (m p.1, m p.2)) (filter.prod f f) : filter.prod_map_map_eq ... ≤ map (λp:α×α, (m p.1, m p.2)) uniformity : map_mono hf.right ... ≤ uniformity : hm⟩ lemma cauchy_vmap [uniform_space β] {f : filter β} {m : α → β} (hm : vmap (λp:α×α, (m p.1, m p.2)) uniformity ≤ uniformity) (hf : cauchy f) (hb : vmap m f ≠ ⊥) : cauchy (vmap m f) := ⟨hb, calc filter.prod (vmap m f) (vmap m f) = vmap (λp:α×α, (m p.1, m p.2)) (filter.prod f f) : filter.prod_vmap_vmap_eq ... ≤ vmap (λp:α×α, (m p.1, m p.2)) uniformity : vmap_mono hf.right ... ≤ uniformity : hm⟩ /- separated uniformity -/ /-- The separation relation is the intersection of all entourages. Two points which are related by the separation relation are "indistinguishable" according to the uniform structure. -/ protected def separation_rel (α : Type u) [u : uniform_space α] := ⋂₀ (@uniformity α _).sets lemma separated_equiv : equivalence (λx y, (x, y) ∈ separation_rel α) := ⟨assume x, assume s, refl_mem_uniformity, assume x y, assume h (s : set (α×α)) hs, have preimage prod.swap s ∈ (@uniformity α _).sets, from symm_le_uniformity hs, h _ this, assume x y z (hxy : (x, y) ∈ separation_rel α) (hyz : (y, z) ∈ separation_rel α) s (hs : s ∈ (@uniformity α _).sets), let ⟨t, ht, (h_ts : comp_rel t t ⊆ s)⟩ := comp_mem_uniformity_sets hs in h_ts $ show (x, z) ∈ comp_rel t t, from ⟨y, hxy t ht, hyz t ht⟩⟩ protected def separation_setoid (α : Type u) [u : uniform_space α] : setoid α := ⟨λx y, (x, y) ∈ separation_rel α, separated_equiv⟩ @[class] def separated (α : Type u) [uniform_space α] := separation_rel α = id_rel theorem separated_def {α : Type u} [uniform_space α] : separated α ↔ ∀ x y, (∀ r ∈ (@uniformity α _).sets, (x, y) ∈ r) → x = y := by simp [separated, id_rel_subset.2 separated_equiv.1, subset.antisymm_iff]; simp [subset_def, separation_rel] theorem separated_def' {α : Type u} [uniform_space α] : separated α ↔ ∀ x y, x ≠ y → ∃ r ∈ (@uniformity α _).sets, (x, y) ∉ r := separated_def.trans $ forall_congr $ λ x, forall_congr $ λ y, by rw ← not_imp_not; simp [classical.not_forall] instance separated_t2 [s : separated α] : t2_space α := ⟨assume x y, assume h : x ≠ y, let ⟨d, hd, (hxy : (x, y) ∉ d)⟩ := separated_def'.1 s x y h in let ⟨d', hd', (hd'd' : comp_rel d' d' ⊆ d)⟩ := comp_mem_uniformity_sets hd in have {y | (x, y) ∈ d'} ∈ (nhds x).sets, from mem_nhds_left x hd', let ⟨u, hu₁, hu₂, hu₃⟩ := mem_nhds_sets_iff.mp this in have {x | (x, y) ∈ d'} ∈ (nhds y).sets, from mem_nhds_right y hd', let ⟨v, hv₁, hv₂, hv₃⟩ := mem_nhds_sets_iff.mp this in have u ∩ v = ∅, from eq_empty_of_subset_empty $ assume z ⟨(h₁ : z ∈ u), (h₂ : z ∈ v)⟩, have (x, y) ∈ comp_rel d' d', from ⟨z, hu₁ h₁, hv₁ h₂⟩, hxy $ hd'd' this, ⟨u, v, hu₂, hv₂, hu₃, hv₃, this⟩⟩ instance separated_regular [separated α] : regular_space α := { regular := λs a hs ha, have -s ∈ (nhds a).sets, from mem_nhds_sets hs ha, have {p : α × α | p.1 = a → p.2 ∈ -s} ∈ uniformity.sets, from mem_nhds_uniformity_iff.mp this, let ⟨d, hd, h⟩ := comp_mem_uniformity_sets this in let e := {y:α| (a, y) ∈ d} in have hae : a ∈ closure e, from subset_closure $ refl_mem_uniformity hd, have set.prod (closure e) (closure e) ⊆ comp_rel d (comp_rel (set.prod e e) d), begin rw [←closure_prod_eq, closure_eq_inter_uniformity], change (⨅d' ∈ uniformity.sets, _) ≤ comp_rel d (comp_rel _ d), exact (infi_le_of_le d $ infi_le_of_le hd $ le_refl _) end, have e_subset : closure e ⊆ -s, from assume a' ha', let ⟨x, (hx : (a, x) ∈ d), y, ⟨hx₁, hx₂⟩, (hy : (y, _) ∈ d)⟩ := @this ⟨a, a'⟩ ⟨hae, ha'⟩ in have (a, a') ∈ comp_rel d d, from ⟨y, hx₂, hy⟩, h this rfl, have closure e ∈ (nhds a).sets, from (nhds a).upwards_sets (mem_nhds_left a hd) subset_closure, have nhds a ⊓ principal (-closure e) = ⊥, from (@inf_eq_bot_iff_le_compl _ _ _ (principal (- closure e)) (principal (closure e)) (by simp [principal_univ, union_comm]) (by simp)).mpr (by simp [this]), ⟨- closure e, is_closed_closure, assume x h₁ h₂, @e_subset x h₂ h₁, this⟩, ..separated_t2 } /-- A set `s` is totally bounded if for every entourage `d` there is a finite set of points `t` such that every element of `s` is `d`-near to some element of `t`. -/ def totally_bounded (s : set α) : Prop := ∀d ∈ (@uniformity α _).sets, ∃t : set α, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) theorem totally_bounded_iff_subset {s : set α} : totally_bounded s ↔ ∀d ∈ (@uniformity α _).sets, ∃t ⊆ s, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) := ⟨λ H d hd, begin rcases comp_symm_of_uniformity hd with ⟨r, hr, rs, rd⟩, rcases H r hr with ⟨k, fk, ks⟩, let u := {y ∈ k | ∃ x, x ∈ s ∧ (x, y) ∈ r}, let f : u → α := λ x, classical.some x.2.2, have : ∀ x : u, f x ∈ s ∧ (f x, x.1) ∈ r := λ x, classical.some_spec x.2.2, refine ⟨range f, _, _, _⟩, { exact range_subset_iff.2 (λ x, (this x).1) }, { have : finite u := finite_subset fk (λ x h, h.1), exact ⟨@set.fintype_range _ _ _ _ this.fintype⟩ }, { intros x xs, have := ks xs, simp at this, rcases this with ⟨y, hy, xy⟩, let z : coe_sort u := ⟨y, hy, x, xs, xy⟩, simp, exact ⟨_, ⟨_, z.2, rfl⟩, rd $ mem_comp_rel.2 ⟨_, xy, rs (this z).2⟩⟩ } end, λ H d hd, let ⟨t, _, ht⟩ := H d hd in ⟨t, ht⟩⟩ lemma totally_bounded_subset [uniform_space α] {s₁ s₂ : set α} (hs : s₁ ⊆ s₂) (h : totally_bounded s₂) : totally_bounded s₁ := assume d hd, let ⟨t, ht₁, ht₂⟩ := h d hd in ⟨t, ht₁, subset.trans hs ht₂⟩ lemma totally_bounded_closure [uniform_space α] {s : set α} (h : totally_bounded s) : totally_bounded (closure s) := assume t ht, let ⟨t', ht', hct', htt'⟩ := mem_uniformity_is_closed ht, ⟨c, hcf, hc⟩ := h t' ht' in ⟨c, hcf, calc closure s ⊆ closure (⋃ (y : α) (H : y ∈ c), {x : α | (x, y) ∈ t'}) : closure_mono hc ... = _ : closure_eq_of_is_closed $ is_closed_Union hcf $ assume i hi, continuous_iff_is_closed.mp (continuous_id.prod_mk continuous_const) _ hct' ... ⊆ _ : bUnion_subset $ assume i hi, subset.trans (assume x, @htt' (x, i)) (subset_bUnion_of_mem hi)⟩ lemma totally_bounded_image [uniform_space α] [uniform_space β] {f : α → β} {s : set α} (hf : uniform_continuous f) (hs : totally_bounded s) : totally_bounded (f '' s) := assume t ht, have {p:α×α | (f p.1, f p.2) ∈ t} ∈ (@uniformity α _).sets, from hf ht, let ⟨c, hfc, hct⟩ := hs _ this in ⟨f '' c, finite_image f hfc, begin simp [image_subset_iff], simp [subset_def] at hct, intros x hx, simp [-mem_image], exact let ⟨i, hi, ht⟩ := hct x hx in ⟨f i, mem_image_of_mem f hi, ht⟩ end⟩ lemma totally_bounded_preimage [uniform_space α] [uniform_space β] {f : α → β} {s : set β} (hf : uniform_embedding f) (hs : totally_bounded s) : totally_bounded (f ⁻¹' s) := λ t ht, begin rw ← hf.2 at ht, rcases mem_vmap_sets.2 ht with ⟨t', ht', ts⟩, rcases totally_bounded_iff_subset.1 (totally_bounded_subset (image_preimage_subset f s) hs) _ ht' with ⟨c, cs, hfc, hct⟩, refine ⟨f ⁻¹' c, finite_preimage hf.1 hfc, λ x h, _⟩, have := hct (mem_image_of_mem f h), simp at this ⊢, rcases this with ⟨z, zc, zt⟩, rcases cs zc with ⟨y, yc, rfl⟩, exact ⟨y, zc, ts (by exact zt)⟩ end lemma cauchy_of_totally_bounded_of_ultrafilter {s : set α} {f : filter α} (hs : totally_bounded s) (hf : ultrafilter f) (h : f ≤ principal s) : cauchy f := ⟨hf.left, assume t ht, let ⟨t', ht'₁, ht'_symm, ht'_t⟩ := comp_symm_of_uniformity ht in let ⟨i, hi, hs_union⟩ := hs t' ht'₁ in have (⋃y∈i, {x | (x,y) ∈ t'}) ∈ f.sets, from f.upwards_sets (le_principal_iff.mp h) hs_union, have ∃y∈i, {x | (x,y) ∈ t'} ∈ f.sets, from mem_of_finite_Union_ultrafilter hf hi this, let ⟨y, hy, hif⟩ := this in have set.prod {x | (x,y) ∈ t'} {x | (x,y) ∈ t'} ⊆ comp_rel t' t', from assume ⟨x₁, x₂⟩ ⟨(h₁ : (x₁, y) ∈ t'), (h₂ : (x₂, y) ∈ t')⟩, ⟨y, h₁, ht'_symm h₂⟩, (filter.prod f f).upwards_sets (prod_mem_prod hif hif) (subset.trans this ht'_t)⟩ lemma totally_bounded_iff_filter {s : set α} : totally_bounded s ↔ (∀f, f ≠ ⊥ → f ≤ principal s → ∃c ≤ f, cauchy c) := ⟨assume : totally_bounded s, assume f hf hs, ⟨ultrafilter_of f, ultrafilter_of_le, cauchy_of_totally_bounded_of_ultrafilter this (ultrafilter_ultrafilter_of hf) (le_trans ultrafilter_of_le hs)⟩, assume h : ∀f, f ≠ ⊥ → f ≤ principal s → ∃c ≤ f, cauchy c, assume d hd, classical.by_contradiction $ assume hs, have hd_cover : ∀{t:set α}, finite t → ¬ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}), by simpa using hs, let f := ⨅t:{t : set α // finite t}, principal (s \ (⋃y∈t.val, {x | (x,y) ∈ d})), ⟨a, ha⟩ := @exists_mem_of_ne_empty α s (assume h, hd_cover finite_empty $ h.symm ▸ empty_subset _) in have f ≠ ⊥, from infi_neq_bot_of_directed ⟨a⟩ (assume ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩, ⟨⟨t₁ ∪ t₂, finite_union ht₁ ht₂⟩, principal_mono.mpr $ diff_right_antimono $ Union_subset_Union $ assume t, Union_subset_Union_const or.inl, principal_mono.mpr $ diff_right_antimono $ Union_subset_Union $ assume t, Union_subset_Union_const or.inr⟩) (assume ⟨t, ht⟩, by simp [diff_neq_empty]; exact hd_cover ht), have f ≤ principal s, from infi_le_of_le ⟨∅, finite_empty⟩ $ by simp; exact subset.refl s, let ⟨c, (hc₁ : c ≤ f), (hc₂ : cauchy c)⟩ := h f ‹f ≠ ⊥› this, ⟨m, hm, (hmd : set.prod m m ⊆ d)⟩ := (@mem_prod_same_iff α c d).mp $ hc₂.right hd in have c ≤ principal s, from le_trans ‹c ≤ f› this, have m ∩ s ∈ c.sets, from inter_mem_sets hm $ le_principal_iff.mp this, let ⟨y, hym, hys⟩ := inhabited_of_mem_sets hc₂.left this in let ys := (⋃y'∈({y}:set α), {x | (x, y') ∈ d}) in have m ⊆ ys, from assume y' hy', show y' ∈ (⋃y'∈({y}:set α), {x | (x, y') ∈ d}), by simp; exact @hmd (y', y) ⟨hy', hym⟩, have c ≤ principal (s - ys), from le_trans hc₁ $ infi_le_of_le ⟨{y}, finite_singleton _⟩ $ le_refl _, have (s - ys) ∩ (m ∩ s) ∈ c.sets, from inter_mem_sets (le_principal_iff.mp this) ‹m ∩ s ∈ c.sets›, have ∅ ∈ c.sets, from c.upwards_sets this $ assume x ⟨⟨hxs, hxys⟩, hxm, _⟩, hxys $ ‹m ⊆ ys› hxm, hc₂.left $ empty_in_sets_eq_bot.mp this⟩ lemma totally_bounded_iff_ultrafilter {s : set α} : totally_bounded s ↔ (∀f, ultrafilter f → f ≤ principal s → cauchy f) := ⟨assume hs f, cauchy_of_totally_bounded_of_ultrafilter hs, assume h, totally_bounded_iff_filter.mpr $ assume f hf hfs, have cauchy (ultrafilter_of f), from h (ultrafilter_of f) (ultrafilter_ultrafilter_of hf) (le_trans ultrafilter_of_le hfs), ⟨ultrafilter_of f, ultrafilter_of_le, this⟩⟩ lemma compact_of_totally_bounded_complete {s : set α} (ht : totally_bounded s) (hc : ∀{f:filter α}, cauchy f → f ≤ principal s → ∃x∈s, f ≤ nhds x) : compact s := begin rw [compact_iff_ultrafilter_le_nhds], rw [totally_bounded_iff_ultrafilter] at ht, exact assume f hf hfs, hc (ht _ hf hfs) hfs end /-- A complete space is defined here using uniformities. A uniform space is complete if every Cauchy filter converges. -/ class complete_space (α : Type u) [uniform_space α] : Prop := (complete : ∀{f:filter α}, cauchy f → ∃x, f ≤ nhds x) theorem le_nhds_lim_of_cauchy {α} [uniform_space α] [complete_space α] [inhabited α] {f : filter α} (hf : cauchy f) : f ≤ nhds (lim f) := lim_spec (complete_space.complete hf) lemma complete_of_is_closed [complete_space α] {s : set α} {f : filter α} (h : is_closed s) (hf : cauchy f) (hfs : f ≤ principal s) : ∃x∈s, f ≤ nhds x := let ⟨x, hx⟩ := complete_space.complete hf in have x ∈ s, from is_closed_iff_nhds.mp h x $ neq_bot_of_le_neq_bot hf.left $ le_inf hx hfs, ⟨x, this, hx⟩ lemma compact_of_totally_bounded_is_closed [complete_space α] {s : set α} (ht : totally_bounded s) (hc : is_closed s) : compact s := @compact_of_totally_bounded_complete α _ s ht $ assume f, complete_of_is_closed hc lemma complete_space_extension [uniform_space β] {m : β → α} (hm : uniform_embedding m) (dense : ∀x, x ∈ closure (range m)) (h : ∀f:filter β, cauchy f → ∃x:α, map m f ≤ nhds x) : complete_space α := ⟨assume (f : filter α), assume hf : cauchy f, let p : set (α × α) → set α → set α := λs t, {y : α| ∃x:α, x ∈ t ∧ (x, y) ∈ s}, g := uniformity.lift (λs, f.lift' (p s)) in have mp₀ : monotone p, from assume a b h t s ⟨x, xs, xa⟩, ⟨x, xs, h xa⟩, have mp₁ : ∀{s}, monotone (p s), from assume s a b h x ⟨y, ya, yxs⟩, ⟨y, h ya, yxs⟩, have f ≤ g, from le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht, le_principal_iff.mpr $ f.upwards_sets ht $ assume x hx, ⟨x, hx, refl_mem_uniformity hs⟩, have g ≠ ⊥, from neq_bot_of_le_neq_bot hf.left this, have vmap m g ≠ ⊥, from vmap_neq_bot $ assume t ht, let ⟨t', ht', ht_mem⟩ := (mem_lift_sets $ monotone_lift' monotone_const mp₀).mp ht in let ⟨t'', ht'', ht'_sub⟩ := (mem_lift'_sets mp₁).mp ht_mem in let ⟨x, (hx : x ∈ t'')⟩ := inhabited_of_mem_sets hf.left ht'' in have h₀ : nhds x ⊓ principal (range m) ≠ ⊥, by simp [closure_eq_nhds] at dense; exact dense x, have h₁ : {y | (x, y) ∈ t'} ∈ (nhds x ⊓ principal (range m)).sets, from @mem_inf_sets_of_left α (nhds x) (principal (range m)) _ $ mem_nhds_left x ht', have h₂ : range m ∈ (nhds x ⊓ principal (range m)).sets, from @mem_inf_sets_of_right α (nhds x) (principal (range m)) _ $ subset.refl _, have {y | (x, y) ∈ t'} ∩ range m ∈ (nhds x ⊓ principal (range m)).sets, from @inter_mem_sets α (nhds x ⊓ principal (range m)) _ _ h₁ h₂, let ⟨y, xyt', b, b_eq⟩ := inhabited_of_mem_sets h₀ this in ⟨b, b_eq.symm ▸ ht'_sub ⟨x, hx, xyt'⟩⟩, have cauchy g, from ⟨‹g ≠ ⊥›, assume s hs, let ⟨s₁, hs₁, (comp_s₁ : comp_rel s₁ s₁ ⊆ s)⟩ := comp_mem_uniformity_sets hs, ⟨s₂, hs₂, (comp_s₂ : comp_rel s₂ s₂ ⊆ s₁)⟩ := comp_mem_uniformity_sets hs₁, ⟨t, ht, (prod_t : set.prod t t ⊆ s₂)⟩ := mem_prod_same_iff.mp (hf.right hs₂) in have hg₁ : p (preimage prod.swap s₁) t ∈ g.sets, from mem_lift (symm_le_uniformity hs₁) $ @mem_lift' α α f _ t ht, have hg₂ : p s₂ t ∈ g.sets, from mem_lift hs₂ $ @mem_lift' α α f _ t ht, have hg : set.prod (p (preimage prod.swap s₁) t) (p s₂ t) ∈ (filter.prod g g).sets, from @prod_mem_prod α α _ _ g g hg₁ hg₂, (filter.prod g g).upwards_sets hg (assume ⟨a, b⟩ ⟨⟨c₁, c₁t, hc₁⟩, ⟨c₂, c₂t, hc₂⟩⟩, have (c₁, c₂) ∈ set.prod t t, from ⟨c₁t, c₂t⟩, comp_s₁ $ prod_mk_mem_comp_rel hc₁ $ comp_s₂ $ prod_mk_mem_comp_rel (prod_t this) hc₂)⟩, have cauchy (filter.vmap m g), from cauchy_vmap (le_of_eq hm.right) ‹cauchy g› (by assumption), let ⟨x, (hx : map m (filter.vmap m g) ≤ nhds x)⟩ := h _ this in have map m (filter.vmap m g) ⊓ nhds x ≠ ⊥, from (le_nhds_iff_adhp_of_cauchy (cauchy_map hm.uniform_continuous this)).mp hx, have g ⊓ nhds x ≠ ⊥, from neq_bot_of_le_neq_bot this (inf_le_inf (assume s hs, ⟨s, hs, subset.refl _⟩) (le_refl _)), ⟨x, calc f ≤ g : by assumption ... ≤ nhds x : le_nhds_of_cauchy_adhp ‹cauchy g› this⟩⟩ /- separation space -/ section separation_space local attribute [instance] separation_setoid instance {α : Type u} [u : uniform_space α] : uniform_space (quotient (separation_setoid α)) := { to_topological_space := u.to_topological_space.coinduced (λx, ⟦x⟧), uniformity := map (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)) uniformity, refl := assume s hs ⟨a, b⟩ (h : a = b), have ∀a:α, (a, a) ∈ preimage (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) s, from assume a, refl_mem_uniformity hs, h ▸ quotient.induction_on a this, symm := tendsto_map' $ by simp [prod.swap, (∘)]; exact tendsto_swap_uniformity.comp tendsto_map, comp := calc (map (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) uniformity).lift' (λs, comp_rel s s) = uniformity.lift' ((λs, comp_rel s s) ∘ image (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧))) : map_lift'_eq2 $ monotone_comp_rel monotone_id monotone_id ... ≤ uniformity.lift' (image (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) ∘ (λs:set (α×α), comp_rel s (comp_rel s s))) : lift'_mono' $ assume s hs ⟨a, b⟩ ⟨c, ⟨⟨a₁, a₂⟩, ha, a_eq⟩, ⟨⟨b₁, b₂⟩, hb, b_eq⟩⟩, begin simp at a_eq, simp at b_eq, have h : ⟦a₂⟧ = ⟦b₁⟧, { rw [a_eq.right, b_eq.left] }, have h : (a₂, b₁) ∈ separation_rel α := quotient.exact h, simp [function.comp, set.image, comp_rel, and.comm, and.left_comm, and.assoc], exact ⟨a₁, a_eq.left, b₂, b_eq.right, a₂, ha, b₁, h s hs, hb⟩ end ... = map (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)) (uniformity.lift' (λs:set (α×α), comp_rel s (comp_rel s s))) : by rw [map_lift'_eq]; exact monotone_comp_rel monotone_id (monotone_comp_rel monotone_id monotone_id) ... ≤ map (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)) uniformity : map_mono comp_le_uniformity3, is_open_uniformity := assume s, have ∀a, ⟦a⟧ ∈ s → ({p:α×α | p.1 = a → ⟦p.2⟧ ∈ s} ∈ (@uniformity α _).sets ↔ {p:α×α | p.1 ≈ a → ⟦p.2⟧ ∈ s} ∈ (@uniformity α _).sets), from assume a ha, ⟨assume h, let ⟨t, ht, hts⟩ := comp_mem_uniformity_sets h in have hts : ∀{a₁ a₂}, (a, a₁) ∈ t → (a₁, a₂) ∈ t → ⟦a₂⟧ ∈ s, from assume a₁ a₂ ha₁ ha₂, @hts (a, a₂) ⟨a₁, ha₁, ha₂⟩ rfl, have ht' : ∀{a₁ a₂}, a₁ ≈ a₂ → (a₁, a₂) ∈ t, from assume a₁ a₂ h, sInter_subset_of_mem ht h, uniformity.upwards_sets ht $ assume ⟨a₁, a₂⟩ h₁ h₂, hts (ht' $ setoid.symm h₂) h₁, assume h, uniformity.upwards_sets h $ by simp {contextual := tt}⟩, begin simp [topological_space.coinduced, u.is_open_uniformity, uniformity, forall_quotient_iff], exact ⟨λh a ha, (this a ha).mp $ h a ha, λh a ha, (this a ha).mpr $ h a ha⟩ end } lemma uniform_continuous_quotient_mk : uniform_continuous (quotient.mk : α → quotient (separation_setoid α)) := le_refl _ lemma vmap_quotient_le_uniformity : vmap (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) uniformity ≤ uniformity := assume t' ht', let ⟨t, ht, tt_t'⟩ := comp_mem_uniformity_sets ht' in let ⟨s, hs, ss_t⟩ := comp_mem_uniformity_sets ht in ⟨(λp:α×α, (⟦p.1⟧, ⟦p.2⟧)) '' s, (@uniformity α _).upwards_sets hs $ assume x hx, ⟨x, hx, rfl⟩, assume ⟨a₁, a₂⟩ ⟨⟨b₁, b₂⟩, hb, ab_eq⟩, have ⟦b₁⟧ = ⟦a₁⟧ ∧ ⟦b₂⟧ = ⟦a₂⟧, from prod.mk.inj ab_eq, have b₁ ≈ a₁ ∧ b₂ ≈ a₂, from and.imp quotient.exact quotient.exact this, have ab₁ : (a₁, b₁) ∈ t, from (setoid.symm this.left) t ht, have ba₂ : (b₂, a₂) ∈ s, from this.right s hs, tt_t' ⟨b₁, show ((a₁, a₂).1, b₁) ∈ t, from ab₁, ss_t ⟨b₂, show ((b₁, a₂).1, b₂) ∈ s, from hb, ba₂⟩⟩⟩ lemma vmap_quotient_eq_uniformity : vmap (λ (p : α × α), (⟦p.fst⟧, ⟦p.snd⟧)) uniformity = uniformity := le_antisymm vmap_quotient_le_uniformity le_vmap_map lemma complete_space_separation [h : complete_space α] : complete_space (quotient (separation_setoid α)) := ⟨assume f, assume hf : cauchy f, have cauchy (vmap (λx, ⟦x⟧) f), from cauchy_vmap vmap_quotient_le_uniformity hf $ vmap_neq_bot_of_surj hf.left $ assume b, quotient.exists_rep _, let ⟨x, (hx : vmap (λx, ⟦x⟧) f ≤ nhds x)⟩ := complete_space.complete this in ⟨⟦x⟧, calc f ≤ map (λx, ⟦x⟧) (vmap (λx, ⟦x⟧) f) : le_map_vmap $ assume b, quotient.exists_rep _ ... ≤ map (λx, ⟦x⟧) (nhds x) : map_mono hx ... ≤ _ : continuous_iff_tendsto.mp uniform_continuous_quotient_mk.continuous _⟩⟩ lemma separated_separation [h : complete_space α] : separated (quotient (separation_setoid α)) := set.ext $ assume ⟨a, b⟩, quotient.induction_on₂ a b $ assume a b, ⟨assume h, have a ≈ b, from assume s hs, have s ∈ (vmap (λp:(α×α), (⟦p.1⟧, ⟦p.2⟧)) uniformity).sets, from vmap_quotient_le_uniformity hs, let ⟨t, ht, hts⟩ := this in hts begin dsimp, exact h t ht end, show ⟦a⟧ = ⟦b⟧, from quotient.sound this, assume heq : ⟦a⟧ = ⟦b⟧, assume h hs, heq ▸ refl_mem_uniformity hs⟩ end separation_space section uniform_extension variables [uniform_space β] [uniform_space γ] {e : β → α} (h_e : uniform_embedding e) (h_dense : ∀x, x ∈ closure (range e)) {f : β → γ} (h_f : uniform_continuous f) [inhabited γ] local notation `ψ` := (h_e.dense_embedding h_dense).ext f lemma uniformly_extend_of_emb [cγ : complete_space γ] [sγ : separated γ] {b : β} : ψ (e b) = f b := dense_embedding.ext_e_eq _ $ continuous_iff_tendsto.mp h_f.continuous b lemma uniformly_extend_exists [complete_space γ] [sγ : separated γ] {a : α} : ∃c, tendsto f (vmap e (nhds a)) (nhds c) := let de := (h_e.dense_embedding h_dense) in have cauchy (nhds a), from cauchy_nhds, have cauchy (vmap e (nhds a)), from cauchy_vmap (le_of_eq h_e.right) this de.vmap_nhds_neq_bot, have cauchy (map f (vmap e (nhds a))), from cauchy_map h_f this, complete_space.complete this lemma uniformly_extend_spec [complete_space γ] [sγ : separated γ] {a : α} : tendsto f (vmap e (nhds a)) (nhds (ψ a)) := lim_spec $ uniformly_extend_exists h_e h_dense h_f lemma uniform_continuous_uniformly_extend [cγ : complete_space γ] [sγ : separated γ] : uniform_continuous ψ := assume d hd, let ⟨s, hs, hs_comp⟩ := (mem_lift'_sets $ monotone_comp_rel monotone_id $ monotone_comp_rel monotone_id monotone_id).mp (comp_le_uniformity3 hd) in have h_pnt : ∀{a m}, m ∈ (nhds a).sets → ∃c, c ∈ f '' preimage e m ∧ (c, ψ a) ∈ s ∧ (ψ a, c) ∈ s, from assume a m hm, have nb : map f (vmap e (nhds a)) ≠ ⊥, from map_ne_bot (h_e.dense_embedding h_dense).vmap_nhds_neq_bot, have (f '' preimage e m) ∩ ({c | (c, ψ a) ∈ s } ∩ {c | (ψ a, c) ∈ s }) ∈ (map f (vmap e (nhds a))).sets, from inter_mem_sets (image_mem_map $ preimage_mem_vmap $ hm) (uniformly_extend_spec h_e h_dense h_f $ inter_mem_sets (mem_nhds_right _ hs) (mem_nhds_left _ hs)), inhabited_of_mem_sets nb this, have preimage (λp:β×β, (f p.1, f p.2)) s ∈ (@uniformity β _).sets, from h_f hs, have preimage (λp:β×β, (f p.1, f p.2)) s ∈ (vmap (λx:β×β, (e x.1, e x.2)) uniformity).sets, by rwa [h_e.right.symm] at this, let ⟨t, ht, ts⟩ := this in show preimage (λp:(α×α), (ψ p.1, ψ p.2)) d ∈ uniformity.sets, from (@uniformity α _).upwards_sets (interior_mem_uniformity ht) $ assume ⟨x₁, x₂⟩ hx_t, have nhds (x₁, x₂) ≤ principal (interior t), from is_open_iff_nhds.mp is_open_interior (x₁, x₂) hx_t, have interior t ∈ (filter.prod (nhds x₁) (nhds x₂)).sets, by rwa [nhds_prod_eq, le_principal_iff] at this, let ⟨m₁, hm₁, m₂, hm₂, (hm : set.prod m₁ m₂ ⊆ interior t)⟩ := mem_prod_iff.mp this in let ⟨a, ha₁, _, ha₂⟩ := h_pnt hm₁ in let ⟨b, hb₁, hb₂, _⟩ := h_pnt hm₂ in have set.prod (preimage e m₁) (preimage e m₂) ⊆ preimage (λp:(β×β), (f p.1, f p.2)) s, from calc _ ⊆ preimage (λp:(β×β), (e p.1, e p.2)) (interior t) : preimage_mono hm ... ⊆ preimage (λp:(β×β), (e p.1, e p.2)) t : preimage_mono interior_subset ... ⊆ preimage (λp:(β×β), (f p.1, f p.2)) s : ts, have set.prod (f '' preimage e m₁) (f '' preimage e m₂) ⊆ s, from calc set.prod (f '' preimage e m₁) (f '' preimage e m₂) = (λp:(β×β), (f p.1, f p.2)) '' (set.prod (preimage e m₁) (preimage e m₂)) : prod_image_image_eq ... ⊆ (λp:(β×β), (f p.1, f p.2)) '' preimage (λp:(β×β), (f p.1, f p.2)) s : mono_image this ... ⊆ s : image_subset_iff.mpr $ subset.refl _, have (a, b) ∈ s, from @this (a, b) ⟨ha₁, hb₁⟩, hs_comp $ show (ψ x₁, ψ x₂) ∈ comp_rel s (comp_rel s s), from ⟨a, ha₂, ⟨b, this, hb₂⟩⟩ end uniform_extension end uniform_space end /-- Space of Cauchy filters This is essentially the completion of a uniform space. The embeddings are the neighbourhood filters. This space is not minimal, the separated uniform space (i.e. quotiented on the intersection of all entourages) is necessary for this. -/ def Cauchy (α : Type u) [uniform_space α] : Type u := { f : filter α // cauchy f } namespace Cauchy section parameters {α : Type u} [uniform_space α] def gen (s : set (α × α)) : set (Cauchy α × Cauchy α) := {p | s ∈ (filter.prod (p.1.val) (p.2.val)).sets } lemma monotone_gen : monotone gen := monotone_set_of $ assume p, @monotone_mem_sets (α×α) (filter.prod (p.1.val) (p.2.val)) private lemma symm_gen : map prod.swap (uniformity.lift' gen) ≤ uniformity.lift' gen := calc map prod.swap (uniformity.lift' gen) = uniformity.lift' (λs:set (α×α), {p | s ∈ (filter.prod (p.2.val) (p.1.val)).sets }) : begin delta gen, simp [map_lift'_eq, monotone_set_of, monotone_mem_sets, function.comp, image_swap_eq_preimage_swap] end ... ≤ uniformity.lift' gen : uniformity_lift_le_swap (monotone_comp (monotone_set_of $ assume p, @monotone_mem_sets (α×α) ((filter.prod ((p.2).val) ((p.1).val)))) monotone_principal) begin have h := λ(p:Cauchy α×Cauchy α), @filter.prod_comm _ _ (p.2.val) (p.1.val), simp [function.comp, h], exact le_refl _ end private lemma comp_rel_gen_gen_subset_gen_comp_rel {s t : set (α×α)} : comp_rel (gen s) (gen t) ⊆ (gen (comp_rel s t) : set (Cauchy α × Cauchy α)) := assume ⟨f, g⟩ ⟨h, h₁, h₂⟩, let ⟨t₁, (ht₁ : t₁ ∈ f.val.sets), t₂, (ht₂ : t₂ ∈ h.val.sets), (h₁ : set.prod t₁ t₂ ⊆ s)⟩ := mem_prod_iff.mp h₁ in let ⟨t₃, (ht₃ : t₃ ∈ h.val.sets), t₄, (ht₄ : t₄ ∈ g.val.sets), (h₂ : set.prod t₃ t₄ ⊆ t)⟩ := mem_prod_iff.mp h₂ in have t₂ ∩ t₃ ∈ h.val.sets, from inter_mem_sets ht₂ ht₃, let ⟨x, xt₂, xt₃⟩ := inhabited_of_mem_sets (h.property.left) this in (filter.prod f.val g.val).upwards_sets (prod_mem_prod ht₁ ht₄) (assume ⟨a, b⟩ ⟨(ha : a ∈ t₁), (hb : b ∈ t₄)⟩, ⟨x, h₁ (show (a, x) ∈ set.prod t₁ t₂, from ⟨ha, xt₂⟩), h₂ (show (x, b) ∈ set.prod t₃ t₄, from ⟨xt₃, hb⟩)⟩) private lemma comp_gen : (uniformity.lift' gen).lift' (λs, comp_rel s s) ≤ uniformity.lift' gen := calc (uniformity.lift' gen).lift' (λs, comp_rel s s) = uniformity.lift' (λs, comp_rel (gen s) (gen s)) : begin rw [lift'_lift'_assoc], exact monotone_gen, exact (monotone_comp_rel monotone_id monotone_id) end ... ≤ uniformity.lift' (λs, gen $ comp_rel s s) : lift'_mono' $ assume s hs, comp_rel_gen_gen_subset_gen_comp_rel ... = (uniformity.lift' $ λs:set(α×α), comp_rel s s).lift' gen : begin rw [lift'_lift'_assoc], exact (monotone_comp_rel monotone_id monotone_id), exact monotone_gen end ... ≤ uniformity.lift' gen : lift'_mono comp_le_uniformity (le_refl _) instance completion_space : uniform_space (Cauchy α) := uniform_space.of_core { uniformity := uniformity.lift' gen, refl := principal_le_lift' $ assume s hs ⟨a, b⟩ (a_eq_b : a = b), a_eq_b ▸ a.property.right hs, symm := symm_gen, comp := comp_gen } theorem mem_uniformity {s : set (Cauchy α × Cauchy α)} : s ∈ (@uniformity (Cauchy α) _).sets ↔ ∃ t ∈ (@uniformity α _).sets, gen t ⊆ s := mem_lift'_sets monotone_gen theorem mem_uniformity' {s : set (Cauchy α × Cauchy α)} : s ∈ (@uniformity (Cauchy α) _).sets ↔ ∃ t ∈ (@uniformity α _).sets, ∀ f g : Cauchy α, t ∈ (filter.prod f.1 g.1).sets → (f, g) ∈ s := mem_uniformity.trans $ bex_congr $ λ t h, prod.forall /-- Embedding of `α` into its completion -/ def pure_cauchy (a : α) : Cauchy α := ⟨pure a, cauchy_pure⟩ lemma uniform_embedding_pure_cauchy : uniform_embedding (pure_cauchy : α → Cauchy α) := ⟨assume a₁ a₂ h, have (pure_cauchy a₁).val = (pure_cauchy a₂).val, from congr_arg _ h, have {a₁} = ({a₂} : set α), from principal_eq_iff_eq.mp this, by simp at this; assumption, have (preimage (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ∘ gen) = id, from funext $ assume s, set.ext $ assume ⟨a₁, a₂⟩, by simp [preimage, gen, pure_cauchy, prod_principal_principal], calc vmap (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) (uniformity.lift' gen) = uniformity.lift' (preimage (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ∘ gen) : vmap_lift'_eq monotone_gen ... = uniformity : by simp [this]⟩ lemma pure_cauchy_dense : ∀x, x ∈ closure (range pure_cauchy) := assume f, have h_ex : ∀s∈(@uniformity (Cauchy α) _).sets, ∃y:α, (f, pure_cauchy y) ∈ s, from assume s hs, let ⟨t'', ht''₁, (ht''₂ : gen t'' ⊆ s)⟩ := (mem_lift'_sets monotone_gen).mp hs in let ⟨t', ht'₁, ht'₂⟩ := comp_mem_uniformity_sets ht''₁ in have t' ∈ (filter.prod (f.val) (f.val)).sets, from f.property.right ht'₁, let ⟨t, ht, (h : set.prod t t ⊆ t')⟩ := mem_prod_same_iff.mp this in let ⟨x, (hx : x ∈ t)⟩ := inhabited_of_mem_sets f.property.left ht in have t'' ∈ (filter.prod f.val (pure x)).sets, from mem_prod_iff.mpr ⟨t, ht, {y:α | (x, y) ∈ t'}, assume y, begin simp, intro h, simp [h], exact refl_mem_uniformity ht'₁ end, assume ⟨a, b⟩ ⟨(h₁ : a ∈ t), (h₂ : (x, b) ∈ t')⟩, ht'₂ $ prod_mk_mem_comp_rel (@h (a, x) ⟨h₁, hx⟩) h₂⟩, ⟨x, ht''₂ $ by dsimp [gen]; exact this⟩, begin simp [closure_eq_nhds, nhds_eq_uniformity, lift'_inf_principal_eq, set.inter_comm], exact (lift'_neq_bot_iff $ monotone_inter monotone_const monotone_preimage).mpr (assume s hs, let ⟨y, hy⟩ := h_ex s hs in have pure_cauchy y ∈ range pure_cauchy ∩ {y : Cauchy α | (f, y) ∈ s}, from ⟨mem_range_self y, hy⟩, ne_empty_of_mem this) end instance : complete_space (Cauchy α) := complete_space_extension uniform_embedding_pure_cauchy pure_cauchy_dense $ assume f hf, let f' : Cauchy α := ⟨f, hf⟩ in have map pure_cauchy f ≤ uniformity.lift' (preimage (prod.mk f')), from le_lift' $ assume s hs, let ⟨t, ht₁, (ht₂ : gen t ⊆ s)⟩ := (mem_lift'_sets monotone_gen).mp hs in let ⟨t', ht', (h : set.prod t' t' ⊆ t)⟩ := mem_prod_same_iff.mp (hf.right ht₁) in have t' ⊆ { y : α | (f', pure_cauchy y) ∈ gen t }, from assume x hx, (filter.prod f (pure x)).upwards_sets (prod_mem_prod ht' $ mem_pure hx) h, f.upwards_sets ht' $ subset.trans this (preimage_mono ht₂), ⟨f', by simp [nhds_eq_uniformity]; assumption⟩ end end Cauchy instance nonempty_Cauchy {α : Type u} [h : nonempty α] [uniform_space α] : nonempty (Cauchy α) := h.rec_on $ assume a, nonempty.intro $ Cauchy.pure_cauchy a instance inhabited_Cauchy {α : Type u} [inhabited α] [uniform_space α] : inhabited (Cauchy α) := ⟨Cauchy.pure_cauchy $ default α⟩ section constructions variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {ι : Sort*} instance : partial_order (uniform_space α) := { le := λt s, s.uniformity ≤ t.uniformity, le_antisymm := assume t s h₁ h₂, uniform_space_eq $ le_antisymm h₂ h₁, le_refl := assume t, le_refl _, le_trans := assume a b c h₁ h₂, @le_trans _ _ c.uniformity b.uniformity a.uniformity h₂ h₁ } instance : has_Sup (uniform_space α) := ⟨assume s, uniform_space.of_core { uniformity := (⨅u∈s, @uniformity α u), refl := le_infi $ assume u, le_infi $ assume hu, u.refl, symm := le_infi $ assume u, le_infi $ assume hu, le_trans (map_mono $ infi_le_of_le _ $ infi_le _ hu) u.symm, comp := le_infi $ assume u, le_infi $ assume hu, le_trans (lift'_mono (infi_le_of_le _ $ infi_le _ hu) $ le_refl _) u.comp }⟩ private lemma le_Sup {tt : set (uniform_space α)} {t : uniform_space α} (h : t ∈ tt) : t ≤ Sup tt := show (⨅u∈tt, @uniformity α u) ≤ t.uniformity, from infi_le_of_le t $ infi_le _ h private lemma Sup_le {tt : set (uniform_space α)} {t : uniform_space α} (h : ∀t'∈tt, t' ≤ t) : Sup tt ≤ t := show t.uniformity ≤ (⨅u∈tt, @uniformity α u), from le_infi $ assume t', le_infi $ assume ht', h t' ht' instance : has_bot (uniform_space α) := ⟨uniform_space.of_core { uniformity := ⊤, refl := le_top, symm := le_top, comp := le_top }⟩ instance : has_top (uniform_space α) := ⟨{ to_topological_space := ⊤, uniformity := principal id_rel, refl := le_refl _, symm := by simp [tendsto]; apply subset.refl, comp := begin rw [lift'_principal], {simp}, exact monotone_comp_rel monotone_id monotone_id end, is_open_uniformity := by rw [topological_space.lattice.has_top]; simp [subset_def, id_rel] {contextual := tt } }⟩ instance : complete_lattice (uniform_space α) := { sup := λa b, Sup {a, b}, le_sup_left := assume a b, le_Sup $ by simp, le_sup_right := assume a b, le_Sup $ by simp, sup_le := assume a b c h₁ h₂, Sup_le $ assume t', begin simp, intro h, cases h with h h, repeat { subst h; assumption } end, inf := λa b, Sup {x | x ≤ a ∧ x ≤ b}, le_inf := assume a b c h₁ h₂, le_Sup ⟨h₁, h₂⟩, inf_le_left := assume a b, Sup_le $ assume x ⟨ha, hb⟩, ha, inf_le_right := assume a b, Sup_le $ assume x ⟨ha, hb⟩, hb, top := ⊤, le_top := assume u, u.refl, bot := ⊥, bot_le := assume a, show a.uniformity ≤ ⊤, from le_top, Sup := Sup, le_Sup := assume s u, le_Sup, Sup_le := assume s u, Sup_le, Inf := λtt, Sup {t | ∀t'∈tt, t ≤ t'}, le_Inf := assume s a hs, le_Sup hs, Inf_le := assume s a ha, Sup_le $ assume u hs, hs _ ha, ..uniform_space.partial_order } lemma supr_uniformity {ι : Sort*} {u : ι → uniform_space α} : (supr u).uniformity = (⨅i, (u i).uniformity) := show (⨅a (h : ∃i:ι, a = u i), a.uniformity) = _, from le_antisymm (le_infi $ assume i, infi_le_of_le (u i) $ infi_le _ ⟨i, rfl⟩) (le_infi $ assume a, le_infi $ assume ⟨i, (ha : a = u i)⟩, ha.symm ▸ infi_le _ _) lemma sup_uniformity {u v : uniform_space α} : (u ⊔ v).uniformity = u.uniformity ⊓ v.uniformity := have (u ⊔ v) = (⨆i (h : i = u ∨ i = v), i), by simp [supr_or, supr_sup_eq], calc (u ⊔ v).uniformity = ((⨆i (h : i = u ∨ i = v), i) : uniform_space α).uniformity : by rw [this] ... = _ : by simp [supr_uniformity, infi_or, infi_inf_eq] instance inhabited_uniform_space : inhabited (uniform_space α) := ⟨⊤⟩ /-- Given `f : α → β` and a uniformity `u` on `β`, the inverse image of `u` under `f` is the inverse image in the filter sense of the induced function `α × α → β × β`. -/ def uniform_space.vmap (f : α → β) (u : uniform_space β) : uniform_space α := { uniformity := u.uniformity.vmap (λp:α×α, (f p.1, f p.2)), to_topological_space := u.to_topological_space.induced f, refl := le_trans (by simp; exact assume ⟨a, b⟩ (h : a = b), h ▸ rfl) (vmap_mono u.refl), symm := by simp [tendsto_vmap_iff, prod.swap, (∘)]; exact tendsto_vmap.comp tendsto_swap_uniformity, comp := le_trans begin rw [vmap_lift'_eq, vmap_lift'_eq2], exact (lift'_mono' $ assume s hs ⟨a₁, a₂⟩ ⟨x, h₁, h₂⟩, ⟨f x, h₁, h₂⟩), repeat { exact monotone_comp_rel monotone_id monotone_id } end (vmap_mono u.comp), is_open_uniformity := begin intro s, change (@is_open α (u.to_topological_space.induced f) s ↔ _), simp [is_open_iff_nhds, nhds_induced_eq_vmap, mem_nhds_uniformity_iff, filter.vmap, and_comm], exact (ball_congr $ assume x hx, ⟨assume ⟨t, hts, ht⟩, ⟨_, ht, assume ⟨x₁, x₂⟩, by simp [*, subset_def] at * {contextual := tt} ⟩, assume ⟨t, ht, hts⟩, ⟨{y:β | (f x, y) ∈ t}, assume y (hy : (f x, f y) ∈ t), @hts (x, y) hy rfl, mem_nhds_uniformity_iff.mp $ mem_nhds_left _ ht⟩⟩) end } lemma uniform_continuous_vmap {f : α → β} [u : uniform_space β] : @uniform_continuous α β (uniform_space.vmap f u) u f := tendsto_vmap theorem to_topological_space_vmap {f : α → β} {u : uniform_space β} : @uniform_space.to_topological_space _ (uniform_space.vmap f u) = topological_space.induced f (@uniform_space.to_topological_space β u) := eq_of_nhds_eq_nhds $ assume a, begin simp [nhds_induced_eq_vmap, nhds_eq_uniformity, nhds_eq_uniformity], change vmap f (uniformity.lift' (preimage (λb, (f a, b)))) = (u.uniformity.vmap (λp:α×α, (f p.1, f p.2))).lift' (preimage (λa', (a, a'))), rw [vmap_lift'_eq monotone_preimage, vmap_lift'_eq2 monotone_preimage], exact rfl end lemma uniform_continuous_vmap' {f : γ → β} {g : α → γ} [v : uniform_space β] [u : uniform_space α] (h : uniform_continuous (f ∘ g)) : @uniform_continuous α γ u (uniform_space.vmap f v) g := tendsto_vmap_iff.2 h lemma to_topological_space_mono {u₁ u₂ : uniform_space α} (h : u₁ ≤ u₂) : @uniform_space.to_topological_space _ u₁ ≤ @uniform_space.to_topological_space _ u₂ := le_of_nhds_le_nhds $ assume a, by rw [@nhds_eq_uniformity α u₁ a, @nhds_eq_uniformity α u₂ a]; exact (lift'_mono h $ le_refl _) lemma to_topological_space_top : @uniform_space.to_topological_space α ⊤ = ⊤ := rfl lemma to_topological_space_bot : @uniform_space.to_topological_space α ⊥ = ⊥ := bot_unique $ assume s hs, classical.by_cases (assume : s = ∅, this.symm ▸ @is_open_empty _ ⊥) (assume : s ≠ ∅, let ⟨x, hx⟩ := exists_mem_of_ne_empty this in have univ ⊆ _, from hs x hx, have s = univ, from top_unique $ assume y hy, @this (x, y) ⟨⟩ rfl, this.symm ▸ @is_open_univ _ ⊥) lemma to_topological_space_supr {ι : Sort*} {u : ι → uniform_space α} : @uniform_space.to_topological_space α (supr u) = (⨆i, @uniform_space.to_topological_space α (u i)) := classical.by_cases (assume h : nonempty ι, eq_of_nhds_eq_nhds $ assume a, begin rw [nhds_supr, nhds_eq_uniformity], change _ = (supr u).uniformity.lift' (preimage $ prod.mk a), begin rw [supr_uniformity, lift'_infi], exact (congr_arg _ $ funext $ assume i, @nhds_eq_uniformity α (u i) a), exact h, exact assume a b, rfl end end) (assume : ¬ nonempty ι, le_antisymm (have supr u = ⊥, from bot_unique $ supr_le $ assume i, (this ⟨i⟩).elim, have @uniform_space.to_topological_space _ (supr u) = ⊥, from this.symm ▸ to_topological_space_bot, this.symm ▸ bot_le) (supr_le $ assume i, to_topological_space_mono $ le_supr _ _)) lemma to_topological_space_Sup {s : set (uniform_space α)} : @uniform_space.to_topological_space α (Sup s) = (⨆i∈s, @uniform_space.to_topological_space α i) := begin rw [Sup_eq_supr, to_topological_space_supr], apply congr rfl, funext x, exact to_topological_space_supr end lemma to_topological_space_sup {u v : uniform_space α} : @uniform_space.to_topological_space α (u ⊔ v) = @uniform_space.to_topological_space α u ⊔ @uniform_space.to_topological_space α v := ord_continuous_sup $ assume s, to_topological_space_Sup instance : uniform_space empty := ⊤ instance : uniform_space unit := ⊤ instance : uniform_space bool := ⊤ instance : uniform_space ℕ := ⊤ instance : uniform_space ℤ := ⊤ instance {p : α → Prop} [t : uniform_space α] : uniform_space (subtype p) := uniform_space.vmap subtype.val t lemma uniformity_subtype {p : α → Prop} [t : uniform_space α] : (@uniformity (subtype p) _) = vmap (λq:subtype p × subtype p, (q.1.1, q.2.1)) uniformity := rfl lemma uniform_continuous_subtype_val {p : α → Prop} [uniform_space α] : uniform_continuous (subtype.val : {a : α // p a} → α) := uniform_continuous_vmap lemma uniform_continuous_subtype_mk {p : α → Prop} [uniform_space α] [uniform_space β] {f : β → α} (hf : uniform_continuous f) (h : ∀x, p (f x)) : uniform_continuous (λx, ⟨f x, h x⟩ : β → subtype p) := uniform_continuous_vmap' hf lemma tendsto_of_uniform_continuous_subtype [uniform_space α] [uniform_space β] {f : α → β} {s : set α} {a : α} (hf : uniform_continuous (λx:s, f x.val)) (ha : s ∈ (nhds a).sets) : tendsto f (nhds a) (nhds (f a)) := by rw [(@map_nhds_subtype_val_eq α _ s a (mem_of_nhds ha) ha).symm]; exact tendsto_map' (continuous_iff_tendsto.mp hf.continuous _) instance [u₁ : uniform_space α] [u₂ : uniform_space β] : uniform_space (α × β) := uniform_space.of_core_eq (u₁.vmap prod.fst ⊔ u₂.vmap prod.snd).to_core prod.topological_space (calc prod.topological_space = (u₁.vmap prod.fst ⊔ u₂.vmap prod.snd).to_topological_space : by rw [to_topological_space_sup, to_topological_space_vmap, to_topological_space_vmap]; refl ... = _ : by rw [uniform_space.to_core_to_topological_space]) theorem prod_uniformity [uniform_space α] [uniform_space β] : @uniformity (α × β) _ = uniformity.vmap (λp:(α × β) × α × β, (p.1.1, p.2.1)) ⊓ uniformity.vmap (λp:(α × β) × α × β, (p.1.2, p.2.2)) := sup_uniformity lemma uniform_embedding_subtype_emb {α : Type*} {β : Type*} [uniform_space α] [uniform_space β] (p : α → Prop) {e : α → β} (ue : uniform_embedding e) (de : dense_embedding e) : uniform_embedding (de.subtype_emb p) := ⟨(de.subtype p).inj, by simp [vmap_vmap_comp, (∘), dense_embedding.subtype_emb, uniformity_subtype, ue.right.symm]⟩ lemma uniform_extend_subtype {α : Type*} {β : Type*} {γ : Type*} [uniform_space α] [uniform_space β] [uniform_space γ] [complete_space γ] [inhabited γ] [separated γ] {p : α → Prop} {e : α → β} {f : α → γ} {b : β} {s : set α} (hf : uniform_continuous (λx:subtype p, f x.val)) (he : uniform_embedding e) (hd : ∀x:β, x ∈ closure (range e)) (hb : closure (e '' s) ∈ (nhds b).sets) (hs : is_closed s) (hp : ∀x∈s, p x) : ∃c, tendsto f (vmap e (nhds b)) (nhds c) := have de : dense_embedding e, from he.dense_embedding hd, have de' : dense_embedding (de.subtype_emb p), by exact de.subtype p, have ue' : uniform_embedding (de.subtype_emb p), from uniform_embedding_subtype_emb _ he de, have b ∈ closure (e '' {x | p x}), from (closure_mono $ mono_image $ hp) (mem_of_nhds hb), let ⟨c, (hc : tendsto (f ∘ subtype.val) (vmap (de.subtype_emb p) (nhds ⟨b, this⟩)) (nhds c))⟩ := uniformly_extend_exists ue' de'.dense hf in begin rw [nhds_subtype_eq_vmap] at hc, simp [vmap_vmap_comp] at hc, change (tendsto (f ∘ @subtype.val α p) (vmap (e ∘ @subtype.val α p) (nhds b)) (nhds c)) at hc, rw [←vmap_vmap_comp] at hc, existsi c, apply tendsto_vmap'' s _ _ hc, exact ⟨_, hb, assume x, begin change e x ∈ (closure (e '' s)) → x ∈ s, rw [←closure_induced, closure_eq_nhds], dsimp, rw [nhds_induced_eq_vmap, de.induced], change x ∈ {x | nhds x ⊓ principal s ≠ ⊥} → x ∈ s, rw [←closure_eq_nhds, closure_eq_of_is_closed hs], exact id, exact de.inj end⟩, exact (assume x hx, ⟨⟨x, hp x hx⟩, rfl⟩) end /- a similar product space is possible on the function space (uniformity of pointwise convergence), but we want to have the uniformity of uniform convergence on function spaces -/ lemma uniformity_prod [uniform_space α] [uniform_space β] : @uniformity (α×β) _ = vmap (λp:(α×β)×(α×β), (p.1.1, p.2.1)) uniformity ⊓ vmap (λp:(α×β)×(α×β), (p.1.2, p.2.2)) uniformity := by rw [prod.uniform_space, uniform_space.of_core_eq_to_core, uniformity, sup_uniformity]; refl lemma uniformity_prod_eq_prod [uniform_space α] [uniform_space β] : @uniformity (α×β) _ = map (λp:(α×α)×(β×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))) (filter.prod uniformity uniformity) := have map (λp:(α×α)×(β×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))) = vmap (λp:(α×β)×(α×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))), from funext $ assume f, map_eq_vmap_of_inverse (funext $ assume ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl) (funext $ assume ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl), by rw [this, uniformity_prod, filter.prod, vmap_inf, vmap_vmap_comp, vmap_vmap_comp] lemma mem_uniform_prod [t₁ : uniform_space α] [t₂ : uniform_space β] {a : set (α × α)} {b : set (β × β)} (ha : a ∈ (@uniformity α _).sets) (hb : b ∈ (@uniformity β _).sets) : {p:(α×β)×(α×β) | (p.1.1, p.2.1) ∈ a ∧ (p.1.2, p.2.2) ∈ b } ∈ (@uniformity (α × β) _).sets := by rw [uniformity_prod]; exact inter_mem_inf_sets (preimage_mem_vmap ha) (preimage_mem_vmap hb) lemma tendsto_prod_uniformity_fst [uniform_space α] [uniform_space β] : tendsto (λp:(α×β)×(α×β), (p.1.1, p.2.1)) uniformity uniformity := le_trans (map_mono (@le_sup_left (uniform_space (α×β)) _ _ _)) map_vmap_le lemma tendsto_prod_uniformity_snd [uniform_space α] [uniform_space β] : tendsto (λp:(α×β)×(α×β), (p.1.2, p.2.2)) uniformity uniformity := le_trans (map_mono (@le_sup_right (uniform_space (α×β)) _ _ _)) map_vmap_le lemma uniform_continuous_fst [uniform_space α] [uniform_space β] : uniform_continuous (λp:α×β, p.1) := tendsto_prod_uniformity_fst lemma uniform_continuous_snd [uniform_space α] [uniform_space β] : uniform_continuous (λp:α×β, p.2) := tendsto_prod_uniformity_snd lemma uniform_continuous.prod_mk [uniform_space α] [uniform_space β] [uniform_space γ] {f₁ : α → β} {f₂ : α → γ} (h₁ : uniform_continuous f₁) (h₂ : uniform_continuous f₂) : uniform_continuous (λa, (f₁ a, f₂ a)) := by rw [uniform_continuous, uniformity_prod]; exact tendsto_inf.2 ⟨tendsto_vmap_iff.2 h₁, tendsto_vmap_iff.2 h₂⟩ lemma uniform_embedding.prod {α' : Type*} {β' : Type*} [uniform_space α] [uniform_space β] [uniform_space α'] [uniform_space β'] {e₁ : α → α'} {e₂ : β → β'} (h₁ : uniform_embedding e₁) (h₂ : uniform_embedding e₂) : uniform_embedding (λp:α×β, (e₁ p.1, e₂ p.2)) := ⟨assume ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, by simp [prod.mk.inj_iff]; exact assume eq₁ eq₂, ⟨h₁.left eq₁, h₂.left eq₂⟩, by simp [(∘), uniformity_prod, h₁.right.symm, h₂.right.symm, vmap_inf, vmap_vmap_comp]⟩ lemma to_topological_space_prod [u : uniform_space α] [v : uniform_space β] : @uniform_space.to_topological_space (α × β) prod.uniform_space = @prod.topological_space α β u.to_topological_space v.to_topological_space := rfl lemma to_topological_space_subtype [u : uniform_space α] {p : α → Prop} : @uniform_space.to_topological_space (subtype p) subtype.uniform_space = @subtype.topological_space α p u.to_topological_space := rfl end constructions
87163831f39077ecf2baf649fa34a72ca50c11a0
7282d49021d38dacd06c4ce45a48d09627687fe0
/tests/lean/simp22.lean
c459e69e1244ac37a0c07c368550e73e71317228
[ "Apache-2.0" ]
permissive
steveluc/lean
5a0b4431acefaf77f15b25bbb49294c2449923ad
92ba4e8b2d040a799eda7deb8d2a7cdd3e69c496
refs/heads/master
1,611,332,256,930
1,391,013,244,000
1,391,013,244,000
16,361,079
1
0
null
null
null
null
UTF-8
Lean
false
false
1,441
lean
import cast variable vec : Nat → Type variable concat {n m : Nat} (v : vec n) (w : vec m) : vec (n + m) infixl 65 ; : concat axiom concat_assoc {n1 n2 n3 : Nat} (v1 : vec n1) (v2 : vec n2) (v3 : vec n3) : (v1 ; v2) ; v3 = cast (congr2 vec (symm (Nat::add_assoc n1 n2 n3))) (v1 ; (v2 ; v3)) variable empty : vec 0 axiom concat_empty {n : Nat} (v : vec n) : v ; empty = cast (congr2 vec (symm (Nat::add_zeror n))) v rewrite_set simple -- The simplification rules used for Nat and Vectors should "mirror" each other. -- Concatenation is not commutative. So, by adding Nat::add_comm to the -- rewrite set, we prevent the simplifier from reducing the following example add_rewrite concat_assoc concat_empty Nat::add_assoc Nat::add_zeror Nat::add_comm : simple variable n : Nat variable v : vec n variable w : vec n variable f {A : TypeM} : A → A (* local t = parse_lean([[ f ((v ; w) ; empty ; (v ; empty)) ]]) print(t) print("===>") local t2, pr = simplify(t, "simple") print(t2) print(pr) get_environment():type_check(pr) *) -- Now, if we disable Nat::add_comm, the simplifier works disable_rewrite Nat::add_comm : simple print "After disabling Nat::add_comm" (* local t = parse_lean([[ f ((v ; w) ; empty ; (v ; empty)) ]]) print(t) print("===>") local t2, pr = simplify(t, "simple") print(t2) print(pr) get_environment():type_check(pr) *)
52b6bbedaea534a6c08a4db077f810686438638f
63abd62053d479eae5abf4951554e1064a4c45b4
/src/category_theory/monoidal/of_has_finite_products.lean
49ae1d020a69728ab2d227e4a8da0418e71e0c78
[ "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
12,039
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Simon Hudon -/ import category_theory.monoidal.braided import category_theory.limits.shapes.binary_products import category_theory.limits.shapes.terminal /-! # The natural monoidal structure on any category with finite (co)products. A category with a monoidal structure provided in this way is sometimes called a (co)cartesian category, although this is also sometimes used to mean a finitely complete category. (See <https://ncatlab.org/nlab/show/cartesian+category>.) As this works with either products or coproducts, and sometimes we want to think of a different monoidal structure entirely, we don't set up either construct as an instance. ## Implementation We had previously chosen to rely on `has_terminal` and `has_binary_products` instead of `has_finite_products`, because we were later relying on the definitional form of the tensor product. Now that `has_limit` has been refactored to be a `Prop`, this issue is irrelevant and we could simplify the construction here. See `category_theory.monoidal.of_chosen_finite_products` for a variant of this construction which allows specifying a particular choice of terminal object and binary products. -/ universes v u noncomputable theory namespace category_theory variables (C : Type u) [category.{v} C] {X Y : C} namespace limits section variables {C} [has_binary_products C] /-- The braiding isomorphism which swaps a binary product. -/ @[simps] def prod.braiding (P Q : C) : P ⨯ Q ≅ Q ⨯ P := { hom := prod.lift prod.snd prod.fst, inv := prod.lift prod.snd prod.fst } /-- The braiding isomorphism can be passed through a map by swapping the order. -/ @[reassoc] lemma braid_natural {W X Y Z : C} (f : X ⟶ Y) (g : Z ⟶ W) : prod.map f g ≫ (prod.braiding _ _).hom = (prod.braiding _ _).hom ≫ prod.map g f := by tidy @[simp, reassoc] lemma prod.symmetry' (P Q : C) : prod.lift prod.snd prod.fst ≫ prod.lift prod.snd prod.fst = 𝟙 (P ⨯ Q) := by tidy /-- The braiding isomorphism is symmetric. -/ @[reassoc] lemma prod.symmetry (P Q : C) : (prod.braiding P Q).hom ≫ (prod.braiding Q P).hom = 𝟙 _ := by simp /-- The associator isomorphism for binary products. -/ @[simps] def prod.associator (P Q R : C) : (P ⨯ Q) ⨯ R ≅ P ⨯ (Q ⨯ R) := { hom := prod.lift (prod.fst ≫ prod.fst) (prod.lift (prod.fst ≫ prod.snd) prod.snd), inv := prod.lift (prod.lift prod.fst (prod.snd ≫ prod.fst)) (prod.snd ≫ prod.snd) } /-- The product functor can be decomposed. -/ def prod.functor_left_comp (X Y : C) : prod.functor.obj (X ⨯ Y) ≅ prod.functor.obj Y ⋙ prod.functor.obj X := nat_iso.of_components (prod.associator _ _) (by tidy) @[reassoc] lemma prod.pentagon (W X Y Z : C) : prod.map ((prod.associator W X Y).hom) (𝟙 Z) ≫ (prod.associator W (X ⨯ Y) Z).hom ≫ prod.map (𝟙 W) ((prod.associator X Y Z).hom) = (prod.associator (W ⨯ X) Y Z).hom ≫ (prod.associator W X (Y ⨯ Z)).hom := by tidy @[reassoc] lemma prod.associator_naturality {X₁ X₂ X₃ Y₁ Y₂ Y₃ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (f₃ : X₃ ⟶ Y₃) : prod.map (prod.map f₁ f₂) f₃ ≫ (prod.associator Y₁ Y₂ Y₃).hom = (prod.associator X₁ X₂ X₃).hom ≫ prod.map f₁ (prod.map f₂ f₃) := by tidy variables [has_terminal C] /-- The left unitor isomorphism for binary products with the terminal object. -/ @[simps] def prod.left_unitor (P : C) : ⊤_ C ⨯ P ≅ P := { hom := prod.snd, inv := prod.lift (terminal.from P) (𝟙 _) } /-- The right unitor isomorphism for binary products with the terminal object. -/ @[simps] def prod.right_unitor (P : C) : P ⨯ ⊤_ C ≅ P := { hom := prod.fst, inv := prod.lift (𝟙 _) (terminal.from P) } @[reassoc] lemma prod.left_unitor_hom_naturality (f : X ⟶ Y): prod.map (𝟙 _) f ≫ (prod.left_unitor Y).hom = (prod.left_unitor X).hom ≫ f := prod.map_snd _ _ @[reassoc] lemma prod.left_unitor_inv_naturality (f : X ⟶ Y): (prod.left_unitor X).inv ≫ prod.map (𝟙 _) f = f ≫ (prod.left_unitor Y).inv := by rw [iso.inv_comp_eq, ← category.assoc, iso.eq_comp_inv, prod.left_unitor_hom_naturality] @[reassoc] lemma prod.right_unitor_hom_naturality (f : X ⟶ Y): prod.map f (𝟙 _) ≫ (prod.right_unitor Y).hom = (prod.right_unitor X).hom ≫ f := prod.map_fst _ _ @[reassoc] lemma prod_right_unitor_inv_naturality (f : X ⟶ Y): (prod.right_unitor X).inv ≫ prod.map f (𝟙 _) = f ≫ (prod.right_unitor Y).inv := by rw [iso.inv_comp_eq, ← category.assoc, iso.eq_comp_inv, prod.right_unitor_hom_naturality] lemma prod.triangle (X Y : C) : (prod.associator X (⊤_ C) Y).hom ≫ prod.map (𝟙 X) ((prod.left_unitor Y).hom) = prod.map ((prod.right_unitor X).hom) (𝟙 Y) := by tidy end section variables {C} [has_binary_coproducts C] /-- The braiding isomorphism which swaps a binary coproduct. -/ @[simps] def coprod.braiding (P Q : C) : P ⨿ Q ≅ Q ⨿ P := { hom := coprod.desc coprod.inr coprod.inl, inv := coprod.desc coprod.inr coprod.inl } @[simp] lemma coprod.symmetry' (P Q : C) : coprod.desc coprod.inr coprod.inl ≫ coprod.desc coprod.inr coprod.inl = 𝟙 (P ⨿ Q) := by tidy /-- The braiding isomorphism is symmetric. -/ lemma coprod.symmetry (P Q : C) : (coprod.braiding P Q).hom ≫ (coprod.braiding Q P).hom = 𝟙 _ := by simp /-- The associator isomorphism for binary coproducts. -/ @[simps] def coprod.associator (P Q R : C) : (P ⨿ Q) ⨿ R ≅ P ⨿ (Q ⨿ R) := { hom := coprod.desc (coprod.desc coprod.inl (coprod.inl ≫ coprod.inr)) (coprod.inr ≫ coprod.inr), inv := coprod.desc (coprod.inl ≫ coprod.inl) (coprod.desc (coprod.inr ≫ coprod.inl) coprod.inr) } lemma coprod.pentagon (W X Y Z : C) : coprod.map ((coprod.associator W X Y).hom) (𝟙 Z) ≫ (coprod.associator W (X ⨿ Y) Z).hom ≫ coprod.map (𝟙 W) ((coprod.associator X Y Z).hom) = (coprod.associator (W ⨿ X) Y Z).hom ≫ (coprod.associator W X (Y ⨿ Z)).hom := by tidy lemma coprod.associator_naturality {X₁ X₂ X₃ Y₁ Y₂ Y₃ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (f₃ : X₃ ⟶ Y₃) : coprod.map (coprod.map f₁ f₂) f₃ ≫ (coprod.associator Y₁ Y₂ Y₃).hom = (coprod.associator X₁ X₂ X₃).hom ≫ coprod.map f₁ (coprod.map f₂ f₃) := by tidy variables [has_initial C] /-- The left unitor isomorphism for binary coproducts with the initial object. -/ @[simps] def coprod.left_unitor (P : C) : ⊥_ C ⨿ P ≅ P := { hom := coprod.desc (initial.to P) (𝟙 _), inv := coprod.inr } /-- The right unitor isomorphism for binary coproducts with the initial object. -/ @[simps] def coprod.right_unitor (P : C) : P ⨿ ⊥_ C ≅ P := { hom := coprod.desc (𝟙 _) (initial.to P), inv := coprod.inl } lemma coprod.triangle (X Y : C) : (coprod.associator X (⊥_ C) Y).hom ≫ coprod.map (𝟙 X) ((coprod.left_unitor Y).hom) = coprod.map ((coprod.right_unitor X).hom) (𝟙 Y) := by tidy end end limits open category_theory.limits section local attribute [tidy] tactic.case_bash /-- A category with a terminal object and binary products has a natural monoidal structure. -/ def monoidal_of_has_finite_products [has_terminal C] [has_binary_products C] : monoidal_category C := { tensor_unit := ⊤_ C, tensor_obj := λ X Y, X ⨯ Y, tensor_hom := λ _ _ _ _ f g, limits.prod.map f g, associator := prod.associator, left_unitor := prod.left_unitor, right_unitor := prod.right_unitor, pentagon' := prod.pentagon, triangle' := prod.triangle, associator_naturality' := @prod.associator_naturality _ _ _, } end section local attribute [instance] monoidal_of_has_finite_products open monoidal_category /-- The monoidal structure coming from finite products is symmetric. -/ @[simps] def symmetric_of_has_finite_products [has_terminal C] [has_binary_products C] : symmetric_category C := { braiding := limits.prod.braiding, braiding_naturality' := λ X X' Y Y' f g, by { dsimp [tensor_hom], ext; simp, }, hexagon_forward' := λ X Y Z, by ext; { dsimp [monoidal_of_has_finite_products], simp; dsimp; simp, }, hexagon_reverse' := λ X Y Z, by ext; { dsimp [monoidal_of_has_finite_products], simp; dsimp; simp, }, symmetry' := λ X Y, by { dsimp, simp, refl, }, } end namespace monoidal_of_has_finite_products variables [has_terminal C] [has_binary_products C] local attribute [instance] monoidal_of_has_finite_products @[simp] lemma tensor_obj (X Y : C) : X ⊗ Y = (X ⨯ Y) := rfl @[simp] lemma tensor_hom {W X Y Z : C} (f : W ⟶ X) (g : Y ⟶ Z) : f ⊗ g = limits.prod.map f g := rfl @[simp] lemma left_unitor_hom (X : C) : (λ_ X).hom = limits.prod.snd := rfl @[simp] lemma left_unitor_inv (X : C) : (λ_ X).inv = prod.lift (terminal.from X) (𝟙 _) := rfl @[simp] lemma right_unitor_hom (X : C) : (ρ_ X).hom = limits.prod.fst := rfl @[simp] lemma right_unitor_inv (X : C) : (ρ_ X).inv = prod.lift (𝟙 _) (terminal.from X) := rfl -- We don't mark this as a simp lemma, even though in many particular -- categories the right hand side will simplify significantly further. -- For now, we'll plan to create specialised simp lemmas in each particular category. lemma associator_hom (X Y Z : C) : (α_ X Y Z).hom = prod.lift (limits.prod.fst ≫ limits.prod.fst) (prod.lift (limits.prod.fst ≫ limits.prod.snd) limits.prod.snd) := rfl end monoidal_of_has_finite_products section local attribute [tidy] tactic.case_bash /-- A category with an initial object and binary coproducts has a natural monoidal structure. -/ def monoidal_of_has_finite_coproducts [has_initial C] [has_binary_coproducts C] : monoidal_category C := { tensor_unit := ⊥_ C, tensor_obj := λ X Y, X ⨿ Y, tensor_hom := λ _ _ _ _ f g, limits.coprod.map f g, associator := coprod.associator, left_unitor := coprod.left_unitor, right_unitor := coprod.right_unitor, pentagon' := coprod.pentagon, triangle' := coprod.triangle, associator_naturality' := @coprod.associator_naturality _ _ _, } end section local attribute [instance] monoidal_of_has_finite_coproducts open monoidal_category /-- The monoidal structure coming from finite coproducts is symmetric. -/ @[simps] def symmetric_of_has_finite_coproducts [has_initial C] [has_binary_coproducts C] : symmetric_category C := { braiding := limits.coprod.braiding, braiding_naturality' := λ X X' Y Y' f g, by { dsimp [tensor_hom], ext; simp, }, hexagon_forward' := λ X Y Z, by ext; { dsimp [monoidal_of_has_finite_coproducts], simp; dsimp; simp, }, hexagon_reverse' := λ X Y Z, by ext; { dsimp [monoidal_of_has_finite_coproducts], simp; dsimp; simp, }, symmetry' := λ X Y, by { dsimp, simp, refl, }, } end namespace monoidal_of_has_finite_coproducts variables [has_initial C] [has_binary_coproducts C] local attribute [instance] monoidal_of_has_finite_coproducts @[simp] lemma tensor_obj (X Y : C) : X ⊗ Y = (X ⨿ Y) := rfl @[simp] lemma tensor_hom {W X Y Z : C} (f : W ⟶ X) (g : Y ⟶ Z) : f ⊗ g = limits.coprod.map f g := rfl @[simp] lemma left_unitor_hom (X : C) : (λ_ X).hom = coprod.desc (initial.to X) (𝟙 _) := rfl @[simp] lemma right_unitor_hom (X : C) : (ρ_ X).hom = coprod.desc (𝟙 _) (initial.to X) := rfl @[simp] lemma left_unitor_inv (X : C) : (λ_ X).inv = limits.coprod.inr := rfl @[simp] lemma right_unitor_inv (X : C) : (ρ_ X).inv = limits.coprod.inl := rfl -- We don't mark this as a simp lemma, even though in many particular -- categories the right hand side will simplify significantly further. -- For now, we'll plan to create specialised simp lemmas in each particular category. lemma associator_hom (X Y Z : C) : (α_ X Y Z).hom = coprod.desc (coprod.desc coprod.inl (coprod.inl ≫ coprod.inr)) (coprod.inr ≫ coprod.inr) := rfl end monoidal_of_has_finite_coproducts end category_theory
750599d1e23114b926a98c962a735b14730af7d5
ee8cdbabf07f77e7be63a449b8483ce308d37218
/lean/src/test/aime-1984-p7.lean
2a6554b8c6346b5d8d6735d31be134bdb6d60e92
[ "MIT", "Apache-2.0" ]
permissive
zeta1999/miniF2F
6d66c75d1c18152e224d07d5eed57624f731d4b7
c1ba9629559c5273c92ec226894baa0c1ce27861
refs/heads/main
1,681,897,460,642
1,620,646,361,000
1,620,646,361,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
346
lean
/- Copyright (c) 2021 OpenAI. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kunhao Zheng -/ import data.real.basic import data.nat.basic example (f : ℕ+ → ℕ+) (h₀ : ∀ n, 1000 ≤ n → f n = n - 3) (h₁ : ∀ n, n < 1000 → f n = f ( f ( n + 5 ))): f 84 = 997 := begin sorry end
56e18cd5ae9b6f6d64957fb2f4ece1f8cbde4820
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/category_theory/isomorphism.lean
f349d337f9ba764d521c9f8cd24516a5a9cf49c8
[ "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
14,506
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tim Baumann, Stephen Morgan, Scott Morrison, Floris van Doorn -/ import category_theory.functor /-! # Isomorphisms This file defines isomorphisms between objects of a category. ## Main definitions - `structure iso` : a bundled isomorphism between two objects of a category; - `class is_iso` : an unbundled version of `iso`; note that `is_iso f` is a `Prop`, and only asserts the existence of an inverse. Of course, this inverse is unique, so it doesn't cost us much to use choice to retrieve it. - `inv f`, for the inverse of a morphism with `[is_iso f]` - `as_iso` : convert from `is_iso` to `iso` (noncomputable); - `of_iso` : convert from `iso` to `is_iso`; - standard operations on isomorphisms (composition, inverse etc) ## Notations - `X ≅ Y` : same as `iso X Y`; - `α ≪≫ β` : composition of two isomorphisms; it is called `iso.trans` ## Tags category, category theory, isomorphism -/ universes v u -- morphism levels before object levels. See note [category_theory universes]. namespace category_theory open category /-- An isomorphism (a.k.a. an invertible morphism) between two objects of a category. The inverse morphism is bundled. See also `category_theory.core` for the category with the same objects and isomorphisms playing the role of morphisms. See https://stacks.math.columbia.edu/tag/0017. -/ structure iso {C : Type u} [category.{v} C] (X Y : C) := (hom : X ⟶ Y) (inv : Y ⟶ X) (hom_inv_id' : hom ≫ inv = 𝟙 X . obviously) (inv_hom_id' : inv ≫ hom = 𝟙 Y . obviously) restate_axiom iso.hom_inv_id' restate_axiom iso.inv_hom_id' attribute [simp, reassoc] iso.hom_inv_id iso.inv_hom_id infixr ` ≅ `:10 := iso -- type as \cong or \iso variables {C : Type u} [category.{v} C] variables {X Y Z : C} namespace iso @[ext] lemma ext ⦃α β : X ≅ Y⦄ (w : α.hom = β.hom) : α = β := suffices α.inv = β.inv, by cases α; cases β; cc, calc α.inv = α.inv ≫ (β.hom ≫ β.inv) : by rw [iso.hom_inv_id, category.comp_id] ... = (α.inv ≫ α.hom) ≫ β.inv : by rw [category.assoc, ←w] ... = β.inv : by rw [iso.inv_hom_id, category.id_comp] /-- Inverse isomorphism. -/ @[symm] def symm (I : X ≅ Y) : Y ≅ X := { hom := I.inv, inv := I.hom, hom_inv_id' := I.inv_hom_id', inv_hom_id' := I.hom_inv_id' } @[simp] lemma symm_hom (α : X ≅ Y) : α.symm.hom = α.inv := rfl @[simp] lemma symm_inv (α : X ≅ Y) : α.symm.inv = α.hom := rfl @[simp] lemma symm_mk {X Y : C} (hom : X ⟶ Y) (inv : Y ⟶ X) (hom_inv_id) (inv_hom_id) : iso.symm {hom := hom, inv := inv, hom_inv_id' := hom_inv_id, inv_hom_id' := inv_hom_id} = {hom := inv, inv := hom, hom_inv_id' := inv_hom_id, inv_hom_id' := hom_inv_id} := rfl @[simp] lemma symm_symm_eq {X Y : C} (α : X ≅ Y) : α.symm.symm = α := by cases α; refl @[simp] lemma symm_eq_iff {X Y : C} {α β : X ≅ Y} : α.symm = β.symm ↔ α = β := ⟨λ h, symm_symm_eq α ▸ symm_symm_eq β ▸ congr_arg symm h, congr_arg symm⟩ /-- Identity isomorphism. -/ @[refl, simps] def refl (X : C) : X ≅ X := { hom := 𝟙 X, inv := 𝟙 X } instance : inhabited (X ≅ X) := ⟨iso.refl X⟩ @[simp] lemma refl_symm (X : C) : (iso.refl X).symm = iso.refl X := rfl /-- Composition of two isomorphisms -/ @[trans, simps] def trans (α : X ≅ Y) (β : Y ≅ Z) : X ≅ Z := { hom := α.hom ≫ β.hom, inv := β.inv ≫ α.inv } infixr ` ≪≫ `:80 := iso.trans -- type as `\ll \gg`. @[simp] lemma trans_mk {X Y Z : C} (hom : X ⟶ Y) (inv : Y ⟶ X) (hom_inv_id) (inv_hom_id) (hom' : Y ⟶ Z) (inv' : Z ⟶ Y) (hom_inv_id') (inv_hom_id') (hom_inv_id'') (inv_hom_id'') : iso.trans {hom := hom, inv := inv, hom_inv_id' := hom_inv_id, inv_hom_id' := inv_hom_id} {hom := hom', inv := inv', hom_inv_id' := hom_inv_id', inv_hom_id' := inv_hom_id'} = { hom := hom ≫ hom', inv := inv' ≫ inv, hom_inv_id' := hom_inv_id'', inv_hom_id' := inv_hom_id''} := rfl @[simp] lemma trans_symm (α : X ≅ Y) (β : Y ≅ Z) : (α ≪≫ β).symm = β.symm ≪≫ α.symm := rfl @[simp] lemma trans_assoc {Z' : C} (α : X ≅ Y) (β : Y ≅ Z) (γ : Z ≅ Z') : (α ≪≫ β) ≪≫ γ = α ≪≫ β ≪≫ γ := by ext; simp only [trans_hom, category.assoc] @[simp] lemma refl_trans (α : X ≅ Y) : (iso.refl X) ≪≫ α = α := by ext; apply category.id_comp @[simp] lemma trans_refl (α : X ≅ Y) : α ≪≫ (iso.refl Y) = α := by ext; apply category.comp_id @[simp] lemma symm_self_id (α : X ≅ Y) : α.symm ≪≫ α = iso.refl Y := ext α.inv_hom_id @[simp] lemma self_symm_id (α : X ≅ Y) : α ≪≫ α.symm = iso.refl X := ext α.hom_inv_id @[simp] lemma symm_self_id_assoc (α : X ≅ Y) (β : Y ≅ Z) : α.symm ≪≫ α ≪≫ β = β := by rw [← trans_assoc, symm_self_id, refl_trans] @[simp] lemma self_symm_id_assoc (α : X ≅ Y) (β : X ≅ Z) : α ≪≫ α.symm ≪≫ β = β := by rw [← trans_assoc, self_symm_id, refl_trans] lemma inv_comp_eq (α : X ≅ Y) {f : X ⟶ Z} {g : Y ⟶ Z} : α.inv ≫ f = g ↔ f = α.hom ≫ g := ⟨λ H, by simp [H.symm], λ H, by simp [H]⟩ lemma eq_inv_comp (α : X ≅ Y) {f : X ⟶ Z} {g : Y ⟶ Z} : g = α.inv ≫ f ↔ α.hom ≫ g = f := (inv_comp_eq α.symm).symm lemma comp_inv_eq (α : X ≅ Y) {f : Z ⟶ Y} {g : Z ⟶ X} : f ≫ α.inv = g ↔ f = g ≫ α.hom := ⟨λ H, by simp [H.symm], λ H, by simp [H]⟩ lemma eq_comp_inv (α : X ≅ Y) {f : Z ⟶ Y} {g : Z ⟶ X} : g = f ≫ α.inv ↔ g ≫ α.hom = f := (comp_inv_eq α.symm).symm lemma inv_eq_inv (f g : X ≅ Y) : f.inv = g.inv ↔ f.hom = g.hom := have ∀{X Y : C} (f g : X ≅ Y), f.hom = g.hom → f.inv = g.inv, from λ X Y f g h, by rw [ext h], ⟨this f.symm g.symm, this f g⟩ lemma hom_comp_eq_id (α : X ≅ Y) {f : Y ⟶ X} : α.hom ≫ f = 𝟙 X ↔ f = α.inv := by rw [←eq_inv_comp, comp_id] lemma comp_hom_eq_id (α : X ≅ Y) {f : Y ⟶ X} : f ≫ α.hom = 𝟙 Y ↔ f = α.inv := by rw [←eq_comp_inv, id_comp] lemma hom_eq_inv (α : X ≅ Y) (β : Y ≅ X) : α.hom = β.inv ↔ β.hom = α.inv := by { erw [inv_eq_inv α.symm β, eq_comm], refl } end iso /-- `is_iso` typeclass expressing that a morphism is invertible. -/ class is_iso (f : X ⟶ Y) : Prop := (out : ∃ inv : Y ⟶ X, f ≫ inv = 𝟙 X ∧ inv ≫ f = 𝟙 Y) /-- The inverse of a morphism `f` when we have `[is_iso f]`. -/ noncomputable def inv (f : X ⟶ Y) [I : is_iso f] := classical.some I.1 namespace is_iso @[simp, reassoc] lemma hom_inv_id (f : X ⟶ Y) [I : is_iso f] : f ≫ inv f = 𝟙 X := (classical.some_spec I.1).left @[simp, reassoc] lemma inv_hom_id (f : X ⟶ Y) [I : is_iso f] : inv f ≫ f = 𝟙 Y := (classical.some_spec I.1).right end is_iso open is_iso /-- Reinterpret a morphism `f` with an `is_iso f` instance as an `iso`. -/ noncomputable def as_iso (f : X ⟶ Y) [h : is_iso f] : X ≅ Y := ⟨f, inv f, hom_inv_id f, inv_hom_id f⟩ @[simp] lemma as_iso_hom (f : X ⟶ Y) [is_iso f] : (as_iso f).hom = f := rfl @[simp] lemma as_iso_inv (f : X ⟶ Y) [is_iso f] : (as_iso f).inv = inv f := rfl namespace is_iso @[priority 100] -- see Note [lower instance priority] instance epi_of_iso (f : X ⟶ Y) [is_iso f] : epi f := { left_cancellation := λ Z g h w, -- This is an interesting test case for better rewrite automation. by rw [← is_iso.inv_hom_id_assoc f g, w, is_iso.inv_hom_id_assoc f h] } @[priority 100] -- see Note [lower instance priority] instance mono_of_iso (f : X ⟶ Y) [is_iso f] : mono f := { right_cancellation := λ Z g h w, by rw [← category.comp_id g, ← category.comp_id h, ← is_iso.hom_inv_id f, ← category.assoc, w, ← category.assoc] } @[ext] lemma inv_eq_of_hom_inv_id {f : X ⟶ Y} [is_iso f] {g : Y ⟶ X} (hom_inv_id : f ≫ g = 𝟙 X) : inv f = g := begin apply (cancel_epi f).mp, simp [hom_inv_id], end lemma inv_eq_of_inv_hom_id {f : X ⟶ Y} [is_iso f] {g : Y ⟶ X} (inv_hom_id : g ≫ f = 𝟙 Y) : inv f = g := begin apply (cancel_mono f).mp, simp [inv_hom_id], end @[ext] lemma eq_inv_of_hom_inv_id {f : X ⟶ Y} [is_iso f] {g : Y ⟶ X} (hom_inv_id : f ≫ g = 𝟙 X) : g = inv f := (inv_eq_of_hom_inv_id hom_inv_id).symm lemma eq_inv_of_inv_hom_id {f : X ⟶ Y} [is_iso f] {g : Y ⟶ X} (inv_hom_id : g ≫ f = 𝟙 Y) : g = inv f := (inv_eq_of_inv_hom_id inv_hom_id).symm instance id (X : C) : is_iso (𝟙 X) := ⟨⟨𝟙 X, by simp⟩⟩ instance of_iso (f : X ≅ Y) : is_iso f.hom := ⟨⟨f.inv, by simp⟩⟩ instance of_iso_inv (f : X ≅ Y) : is_iso f.inv := is_iso.of_iso f.symm variables {f g : X ⟶ Y} {h : Y ⟶ Z} instance inv_is_iso [is_iso f] : is_iso (inv f) := is_iso.of_iso_inv (as_iso f) /- The following instance has lower priority for the following reason: Suppose we are given `f : X ≅ Y` with `X Y : Type u`. Without the lower priority, typeclass inference cannot deduce `is_iso f.hom` because `f.hom` is defeq to `(λ x, x) ≫ f.hom`, triggering a loop. -/ @[priority 900] instance comp_is_iso [is_iso f] [is_iso h] : is_iso (f ≫ h) := is_iso.of_iso $ (as_iso f) ≪≫ (as_iso h) @[simp] lemma inv_id : inv (𝟙 X) = 𝟙 X := by { ext, simp, } @[simp] lemma inv_comp [is_iso f] [is_iso h] : inv (f ≫ h) = inv h ≫ inv f := by { ext, simp, } @[simp] lemma inv_inv [is_iso f] : inv (inv f) = f := by { ext, simp, } @[simp] lemma iso.inv_inv (f : X ≅ Y) : inv (f.inv) = f.hom := by { ext, simp, } @[simp] lemma iso.inv_hom (f : X ≅ Y) : inv (f.hom) = f.inv := by { ext, simp, } @[simp] lemma inv_comp_eq (α : X ⟶ Y) [is_iso α] {f : X ⟶ Z} {g : Y ⟶ Z} : inv α ≫ f = g ↔ f = α ≫ g := (as_iso α).inv_comp_eq @[simp] lemma eq_inv_comp (α : X ⟶ Y) [is_iso α] {f : X ⟶ Z} {g : Y ⟶ Z} : g = inv α ≫ f ↔ α ≫ g = f := (as_iso α).eq_inv_comp @[simp] lemma comp_inv_eq (α : X ⟶ Y) [is_iso α] {f : Z ⟶ Y} {g : Z ⟶ X} : f ≫ inv α = g ↔ f = g ≫ α := (as_iso α).comp_inv_eq @[simp] lemma eq_comp_inv (α : X ⟶ Y) [is_iso α] {f : Z ⟶ Y} {g : Z ⟶ X} : g = f ≫ inv α ↔ g ≫ α = f := (as_iso α).eq_comp_inv end is_iso open is_iso lemma eq_of_inv_eq_inv {f g : X ⟶ Y} [is_iso f] [is_iso g] (p : inv f = inv g) : f = g := begin apply (cancel_epi (inv f)).1, erw [inv_hom_id, p, inv_hom_id], end lemma is_iso.inv_eq_inv {f g : X ⟶ Y} [is_iso f] [is_iso g] : inv f = inv g ↔ f = g := iso.inv_eq_inv (as_iso f) (as_iso g) lemma hom_comp_eq_id (g : X ⟶ Y) [is_iso g] {f : Y ⟶ X} : g ≫ f = 𝟙 X ↔ f = inv g := (as_iso g).hom_comp_eq_id lemma comp_hom_eq_id (g : X ⟶ Y) [is_iso g] {f : Y ⟶ X} : f ≫ g = 𝟙 Y ↔ f = inv g := (as_iso g).comp_hom_eq_id namespace iso @[ext] lemma inv_ext {f : X ≅ Y} {g : Y ⟶ X} (hom_inv_id : f.hom ≫ g = 𝟙 X) : f.inv = g := begin apply (cancel_epi f.hom).mp, simp [hom_inv_id], end @[ext] lemma inv_ext' {f : X ≅ Y} {g : Y ⟶ X} (hom_inv_id : f.hom ≫ g = 𝟙 X) : g = f.inv := by { symmetry, ext, assumption, } /-! All these cancellation lemmas can be solved by `simp [cancel_mono]` (or `simp [cancel_epi]`), but with the current design `cancel_mono` is not a good `simp` lemma, because it generates a typeclass search. When we can see syntactically that a morphism is a `mono` or an `epi` because it came from an isomorphism, it's fine to do the cancellation via `simp`. In the longer term, it might be worth exploring making `mono` and `epi` structures, rather than typeclasses, with coercions back to `X ⟶ Y`. Presumably we could write `X ↪ Y` and `X ↠ Y`. -/ @[simp] lemma cancel_iso_hom_left {X Y Z : C} (f : X ≅ Y) (g g' : Y ⟶ Z) : f.hom ≫ g = f.hom ≫ g' ↔ g = g' := by simp only [cancel_epi] @[simp] lemma cancel_iso_inv_left {X Y Z : C} (f : Y ≅ X) (g g' : Y ⟶ Z) : f.inv ≫ g = f.inv ≫ g' ↔ g = g' := by simp only [cancel_epi] @[simp] lemma cancel_iso_hom_right {X Y Z : C} (f f' : X ⟶ Y) (g : Y ≅ Z) : f ≫ g.hom = f' ≫ g.hom ↔ f = f' := by simp only [cancel_mono] @[simp] lemma cancel_iso_inv_right {X Y Z : C} (f f' : X ⟶ Y) (g : Z ≅ Y) : f ≫ g.inv = f' ≫ g.inv ↔ f = f' := by simp only [cancel_mono] /- Unfortunately cancelling an isomorphism from the right of a chain of compositions is awkward. We would need separate lemmas for each chain length (worse: for each pair of chain lengths). We provide two more lemmas, for case of three morphisms, because this actually comes up in practice, but then stop. -/ @[simp] lemma cancel_iso_hom_right_assoc {W X X' Y Z : C} (f : W ⟶ X) (g : X ⟶ Y) (f' : W ⟶ X') (g' : X' ⟶ Y) (h : Y ≅ Z) : f ≫ g ≫ h.hom = f' ≫ g' ≫ h.hom ↔ f ≫ g = f' ≫ g' := by simp only [←category.assoc, cancel_mono] @[simp] lemma cancel_iso_inv_right_assoc {W X X' Y Z : C} (f : W ⟶ X) (g : X ⟶ Y) (f' : W ⟶ X') (g' : X' ⟶ Y) (h : Z ≅ Y) : f ≫ g ≫ h.inv = f' ≫ g' ≫ h.inv ↔ f ≫ g = f' ≫ g' := by simp only [←category.assoc, cancel_mono] end iso namespace functor universes u₁ v₁ u₂ v₂ variables {D : Type u₂} variables [category.{v₂} D] /-- A functor `F : C ⥤ D` sends isomorphisms `i : X ≅ Y` to isomorphisms `F.obj X ≅ F.obj Y` -/ @[simps] def map_iso (F : C ⥤ D) {X Y : C} (i : X ≅ Y) : F.obj X ≅ F.obj Y := { hom := F.map i.hom, inv := F.map i.inv, hom_inv_id' := by rw [←map_comp, iso.hom_inv_id, ←map_id], inv_hom_id' := by rw [←map_comp, iso.inv_hom_id, ←map_id] } @[simp] lemma map_iso_symm (F : C ⥤ D) {X Y : C} (i : X ≅ Y) : F.map_iso i.symm = (F.map_iso i).symm := rfl @[simp] lemma map_iso_trans (F : C ⥤ D) {X Y Z : C} (i : X ≅ Y) (j : Y ≅ Z) : F.map_iso (i ≪≫ j) = (F.map_iso i) ≪≫ (F.map_iso j) := by ext; apply functor.map_comp @[simp] lemma map_iso_refl (F : C ⥤ D) (X : C) : F.map_iso (iso.refl X) = iso.refl (F.obj X) := iso.ext $ F.map_id X instance map_is_iso (F : C ⥤ D) (f : X ⟶ Y) [is_iso f] : is_iso (F.map f) := is_iso.of_iso $ F.map_iso (as_iso f) @[simp] lemma map_inv (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) [is_iso f] : F.map (inv f) = inv (F.map f) := by { ext, simp [←F.map_comp], } lemma map_hom_inv (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) [is_iso f] : F.map f ≫ F.map (inv f) = 𝟙 (F.obj X) := by simp lemma map_inv_hom (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) [is_iso f] : F.map (inv f) ≫ F.map f = 𝟙 (F.obj Y) := by simp end functor end category_theory
15813e7e6b078c07164ad0cade3d44d2735f0e92
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Data/Random.lean
6f537bed41797a06fc936df0968cc6db2ed1eac3
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
3,967
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.System.IO import Init.Data.Int universes u /- Basic random number generator support based on the one available on the Haskell library -/ /- Interface for random number generators. -/ class RandomGen (g : Type u) := /- `range` returns the range of values returned by the generator. -/ (range : g → Nat × Nat) /- `next` operation returns a natural number that is uniformly distributed the range returned by `range` (including both end points), and a new generator. -/ (next : g → Nat × g) /- The 'split' operation allows one to obtain two distinct random number generators. This is very useful in functional programs (for example, when passing a random number generator down to recursive calls). -/ (split : g → g × g) /- "Standard" random number generator. -/ structure StdGen := (s1 : Nat) (s2 : Nat) def stdRange := (1, 2147483562) instance : HasRepr StdGen := { repr := fun ⟨s1, s2⟩ => "⟨" ++ toString s1 ++ ", " ++ toString s2 ++ "⟩" } def stdNext : StdGen → Nat × StdGen | ⟨s1, s2⟩ => let k : Int := s1 / 53668; let s1' : Int := 40014 * ((s1 : Int) - k * 53668) - k * 12211; let s1'' : Int := if s1' < 0 then s1' + 2147483563 else s1'; let k' : Int := s2 / 52774; let s2' : Int := 40692 * ((s2 : Int) - k' * 52774) - k' * 3791; let s2'' : Int := if s2' < 0 then s2' + 2147483399 else s2'; let z : Int := s1'' - s2''; let z' : Int := if z < 1 then z + 2147483562 else z % 2147483562; (z'.toNat, ⟨s1''.toNat, s2''.toNat⟩) def stdSplit : StdGen → StdGen × StdGen | g@⟨s1, s2⟩ => let newS1 := if s1 = 2147483562 then 1 else s1 + 1; let newS2 := if s2 = 1 then 2147483398 else s2 - 1; let newG := (stdNext g).2; let leftG := StdGen.mk newS1 newG.2; let rightG := StdGen.mk newG.1 newS2; (leftG, rightG) instance : RandomGen StdGen := {range := fun _ => stdRange, next := stdNext, split := stdSplit} /-- Return a standard number generator. -/ def mkStdGen (s : Nat := 0) : StdGen := let q := s / 2147483562; let s1 := s % 2147483562; let s2 := q % 2147483398; ⟨s1 + 1, s2 + 1⟩ /- Auxiliary function for randomNatVal. Generate random values until we exceed the target magnitude. `genLo` and `genMag` are the generator lower bound and magnitude. The parameter `r` is the "remaining" magnitude. -/ private partial def randNatAux {gen : Type u} [RandomGen gen] (genLo genMag : Nat) : Nat → (Nat × gen) → Nat × gen | 0, (v, g) => (v, g) | r'@(r+1), (v, g) => let (x, g') := RandomGen.next g; let v' := v*genMag + (x - genLo); randNatAux (r' / genMag - 1) (v', g') /-- Generate a random natural number in the interval [lo, hi]. -/ def randNat {gen : Type u} [RandomGen gen] (g : gen) (lo hi : Nat) : Nat × gen := let lo' := if lo > hi then hi else lo; let hi' := if lo > hi then lo else hi; let (genLo, genHi) := RandomGen.range g; let genMag := genHi - genLo + 1; /- Probabilities of the most likely and least likely result will differ at most by a factor of (1 +- 1/q). Assuming the RandomGen is uniform, of course -/ let q := 1000; let k := hi' - lo' + 1; let tgtMag := k * q; let (v, g') := randNatAux genLo genMag tgtMag (0, g); let v' := lo' + (v % k); (v', g') /-- Generate a random Boolean. -/ def randBool {gen : Type u} [RandomGen gen] (g : gen) : Bool × gen := let (v, g') := randNat g 0 1; (v = 1, g') def IO.mkStdGenRef : IO (IO.Ref StdGen) := IO.mkRef mkStdGen @[init IO.mkStdGenRef] constant IO.stdGenRef : IO.Ref StdGen := arbitrary _ def IO.setRandSeed (n : Nat) : IO Unit := IO.stdGenRef.set (mkStdGen n) def IO.rand (lo hi : Nat) : IO Nat := do gen ← IO.stdGenRef.get; let (r, gen) := randNat gen lo hi; IO.stdGenRef.set gen; pure r
49682b55a63bbaf687527145e3ce67f9d4d2908a
30b012bb72d640ec30c8fdd4c45fdfa67beb012c
/data/set/disjointed.lean
4de19908d9d267f10d448969d12d13493ce9f03c
[ "Apache-2.0" ]
permissive
kckennylau/mathlib
21fb810b701b10d6606d9002a4004f7672262e83
47b3477e20ffb5a06588dd3abb01fe0fe3205646
refs/heads/master
1,634,976,409,281
1,542,042,832,000
1,542,319,733,000
109,560,458
0
0
Apache-2.0
1,542,369,208,000
1,509,867,494,000
Lean
UTF-8
Lean
false
false
3,414
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 Disjointed sets -/ import data.set.lattice data.nat.basic open set classical lattice local attribute [instance] prop_decidable universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {s t u : set α} /-- A relation `p` holds pairwise if `p i j` for all `i ≠ j`. -/ def pairwise {α : Type*} (p : α → α → Prop) := ∀i j, i ≠ j → p i j theorem pairwise_on_bool {r} (hr : symmetric r) {a b : α} : pairwise (r on (λ c, cond c a b)) ↔ r a b := by simp [pairwise, bool.forall_bool, function.on_fun]; exact and_iff_right_of_imp (@hr _ _) theorem pairwise_disjoint_on_bool [semilattice_inf_bot α] {a b : α} : pairwise (disjoint on (λ c, cond c a b)) ↔ disjoint a b := pairwise_on_bool $ λ _ _, disjoint.symm namespace set /-- If `f : ℕ → set α` is a sequence of sets, then `disjointed f` is the sequence formed with each set subtracted from the later ones in the sequence, to form a disjoint sequence. -/ def disjointed (f : ℕ → set α) (n : ℕ) : set α := f n ∩ (⋂i<n, - f i) lemma disjoint_disjointed {f : ℕ → set α} : pairwise (disjoint on disjointed f) := λ i j h, begin wlog h' : i ≤ j; [skip, {revert a, exact (this h.symm).symm}], rintro a ⟨⟨h₁, _⟩, h₂, h₃⟩, simp at h₃, exact h₃ _ (lt_of_le_of_ne h' h) h₁ end lemma Union_lt_succ {f : ℕ → set α} {n} : (⋃i < nat.succ n, f i) = f n ∪ (⋃i < n, f i) := ext $ λ a, by simp [nat.lt_succ_iff_lt_or_eq, or_and_distrib_right, exists_or_distrib, or_comm] lemma Inter_lt_succ {f : ℕ → set α} {n} : (⋂i < nat.succ n, f i) = f n ∩ (⋂i < n, f i) := ext $ λ a, by simp [nat.lt_succ_iff_lt_or_eq, or_imp_distrib, forall_and_distrib, and_comm] lemma Union_disjointed {f : ℕ → set α} : (⋃n, disjointed f n) = (⋃n, f n) := subset.antisymm (Union_subset_Union $ assume i, inter_subset_left _ _) $ assume x h, have ∃n, x ∈ f n, from mem_Union.mp h, have hn : ∀ (i : ℕ), i < nat.find this → x ∉ f i, from assume i, nat.find_min this, mem_Union.mpr ⟨nat.find this, nat.find_spec this, by simp; assumption⟩ lemma disjointed_induct {f : ℕ → set α} {n : ℕ} {p : set α → Prop} (h₁ : p (f n)) (h₂ : ∀t i, p t → p (t \ f i)) : p (disjointed f n) := begin rw disjointed, generalize_hyp : f n = t at h₁ ⊢, induction n, case nat.zero { simp [nat.not_lt_zero, h₁] }, case nat.succ : n ih { rw [Inter_lt_succ, inter_comm (-f n), ← inter_assoc], exact h₂ _ n ih } end lemma disjointed_of_mono {f : ℕ → set α} {n : ℕ} (hf : monotone f) : disjointed f (n + 1) = f (n + 1) \ f n := have (⋂i (h : i < n + 1), -f i) = - f n, from le_antisymm (infi_le_of_le n $ infi_le_of_le (nat.lt_succ_self _) $ subset.refl _) (le_infi $ assume i, le_infi $ assume hi, neg_le_neg $ hf $ nat.le_of_succ_le_succ hi), by simp [disjointed, this, diff_eq] lemma Union_disjointed_of_mono {f : ℕ → set α} (hf : monotone f) : ∀ n : ℕ, (⋃i<n.succ, disjointed f i) = f n | 0 := by rw [Union_lt_succ]; simp [disjointed, nat.not_lt_zero] | (n+1) := by rw [Union_lt_succ, disjointed_of_mono hf, Union_disjointed_of_mono n, diff_union_self, union_eq_self_of_subset_right (hf (nat.le_succ _))] end set
ac6abff510295739adfc7a60f70eac65c2260af7
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Compiler/IR/CompilerM.lean
99e9ef50fc4c53663b3ed4be2804fbc45389fde6
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
5,077
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Environment import Lean.Compiler.IR.Basic import Lean.Compiler.IR.Format namespace Lean.IR inductive LogEntry where | step (cls : Name) (decls : Array Decl) | message (msg : Format) namespace LogEntry protected def fmt : LogEntry → Format | step cls decls => Format.bracket "[" (format cls) "]" ++ decls.foldl (fun fmt decl => fmt ++ Format.line ++ format decl) Format.nil | message msg => msg instance : ToFormat LogEntry := ⟨LogEntry.fmt⟩ end LogEntry abbrev Log := Array LogEntry def Log.format (log : Log) : Format := log.foldl (init := Format.nil) fun fmt entry => f!"{fmt}{Format.line}{entry}" @[export lean_ir_log_to_string] def Log.toString (log : Log) : String := log.format.pretty structure CompilerState where env : Environment log : Log := #[] abbrev CompilerM := ReaderT Options (EStateM String CompilerState) def log (entry : LogEntry) : CompilerM Unit := modify fun s => { s with log := s.log.push entry } def tracePrefixOptionName := `trace.compiler.ir private def isLogEnabledFor (opts : Options) (optName : Name) : Bool := match opts.find optName with | some (DataValue.ofBool v) => v | other => opts.getBool tracePrefixOptionName private def logDeclsAux (optName : Name) (cls : Name) (decls : Array Decl) : CompilerM Unit := do let opts ← read if isLogEnabledFor opts optName then log (LogEntry.step cls decls) @[inline] def logDecls (cls : Name) (decl : Array Decl) : CompilerM Unit := logDeclsAux (tracePrefixOptionName ++ cls) cls decl private def logMessageIfAux {α : Type} [ToFormat α] (optName : Name) (a : α) : CompilerM Unit := do let opts ← read if isLogEnabledFor opts optName then log (LogEntry.message (format a)) @[inline] def logMessageIf {α : Type} [ToFormat α] (cls : Name) (a : α) : CompilerM Unit := logMessageIfAux (tracePrefixOptionName ++ cls) a @[inline] def logMessage {α : Type} [ToFormat α] (cls : Name) (a : α) : CompilerM Unit := logMessageIfAux tracePrefixOptionName a @[inline] def modifyEnv (f : Environment → Environment) : CompilerM Unit := modify fun s => { s with env := f s.env } open Std (HashMap) abbrev DeclMap := SMap Name Decl /- Create an array of decls to be saved on .olean file. `decls` may contain duplicate entries, but we assume the one that occurs last is the most recent one. -/ private def mkEntryArray (decls : List Decl) : Array Decl := /- Remove duplicates by adding decls into a map -/ let map : HashMap Name Decl := {} let map := decls.foldl (init := map) fun map decl => map.insert decl.name decl map.fold (fun a k v => a.push v) #[] builtin_initialize declMapExt : SimplePersistentEnvExtension Decl DeclMap ← registerSimplePersistentEnvExtension { name := `IRDecls, addImportedFn := fun as => let m : DeclMap := mkStateFromImportedEntries (fun s (d : Decl) => s.insert d.name d) {} as m.switch, addEntryFn := fun s d => s.insert d.name d, toArrayFn := mkEntryArray } @[export lean_ir_find_env_decl] def findEnvDecl (env : Environment) (n : Name) : Option Decl := (declMapExt.getState env).find? n def findDecl (n : Name) : CompilerM (Option Decl) := do let s ← get pure $ findEnvDecl s.env n def containsDecl (n : Name) : CompilerM Bool := do let s ← get pure $ (declMapExt.getState s.env).contains n def getDecl (n : Name) : CompilerM Decl := do let (some decl) ← findDecl n | throw s!"unknown declaration '{n}'" pure decl @[export lean_ir_add_decl] def addDeclAux (env : Environment) (decl : Decl) : Environment := declMapExt.addEntry env decl def getDecls (env : Environment) : List Decl := declMapExt.getEntries env def getEnv : CompilerM Environment := do let s ← get; pure s.env def addDecl (decl : Decl) : CompilerM Unit := modifyEnv fun env => declMapExt.addEntry env decl def addDecls (decls : Array Decl) : CompilerM Unit := decls.forM addDecl def findEnvDecl' (env : Environment) (n : Name) (decls : Array Decl) : Option Decl := match decls.find? (fun decl => decl.name == n) with | some decl => some decl | none => (declMapExt.getState env).find? n def findDecl' (n : Name) (decls : Array Decl) : CompilerM (Option Decl) := do let s ← get; pure $ findEnvDecl' s.env n decls def containsDecl' (n : Name) (decls : Array Decl) : CompilerM Bool := do if decls.any fun decl => decl.name == n then pure true else let s ← get pure $ (declMapExt.getState s.env).contains n def getDecl' (n : Name) (decls : Array Decl) : CompilerM Decl := do let (some decl) ← findDecl' n decls | throw s!"unknown declaration '{n}'" pure decl @[export lean_decl_get_sorry_dep] def getSorryDep (env : Environment) (declName : Name) : Option Name := match (declMapExt.getState env).find? declName with | some (Decl.fdecl (info := { sorryDep? := dep?, .. }) ..) => dep? | _ => none end IR end Lean
8a3e0c4ab19aa389e10549edc58a516cf7922a58
e0b0b1648286e442507eb62344760d5cd8d13f2d
/stage0/src/Lean/Syntax.lean
024ab614f5bc32ca8e5ba172559247965deef5ee
[ "Apache-2.0" ]
permissive
MULXCODE/lean4
743ed389e05e26e09c6a11d24607ad5a697db39b
4675817a9e89824eca37192364cd47a4027c6437
refs/heads/master
1,682,231,879,857
1,620,423,501,000
1,620,423,501,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,814
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sebastian Ullrich, Leonardo de Moura -/ import Lean.Data.Name import Lean.Data.Format namespace Lean def SourceInfo.updateTrailing (trailing : Substring) : SourceInfo → SourceInfo | SourceInfo.original leading pos _ => SourceInfo.original leading pos trailing | info => info /- Syntax AST -/ inductive IsNode : Syntax → Prop where | mk (kind : SyntaxNodeKind) (args : Array Syntax) : IsNode (Syntax.node kind args) def SyntaxNode : Type := {s : Syntax // IsNode s } def unreachIsNodeMissing {β} (h : IsNode Syntax.missing) : β := False.elim (nomatch h) def unreachIsNodeAtom {β} {info val} (h : IsNode (Syntax.atom info val)) : β := False.elim (nomatch h) def unreachIsNodeIdent {β info rawVal val preresolved} (h : IsNode (Syntax.ident info rawVal val preresolved)) : β := False.elim (nomatch h) namespace SyntaxNode @[inline] def getKind (n : SyntaxNode) : SyntaxNodeKind := match n with | ⟨Syntax.node k args, _⟩ => k | ⟨Syntax.missing, h⟩ => unreachIsNodeMissing h | ⟨Syntax.atom .., h⟩ => unreachIsNodeAtom h | ⟨Syntax.ident .., h⟩ => unreachIsNodeIdent h @[inline] def withArgs {β} (n : SyntaxNode) (fn : Array Syntax → β) : β := match n with | ⟨Syntax.node _ args, _⟩ => fn args | ⟨Syntax.missing, h⟩ => unreachIsNodeMissing h | ⟨Syntax.atom _ _, h⟩ => unreachIsNodeAtom h | ⟨Syntax.ident _ _ _ _, h⟩ => unreachIsNodeIdent h @[inline] def getNumArgs (n : SyntaxNode) : Nat := withArgs n $ fun args => args.size @[inline] def getArg (n : SyntaxNode) (i : Nat) : Syntax := withArgs n $ fun args => args.get! i @[inline] def getArgs (n : SyntaxNode) : Array Syntax := withArgs n $ fun args => args @[inline] def modifyArgs (n : SyntaxNode) (fn : Array Syntax → Array Syntax) : Syntax := match n with | ⟨Syntax.node kind args, _⟩ => Syntax.node kind (fn args) | ⟨Syntax.missing, h⟩ => unreachIsNodeMissing h | ⟨Syntax.atom _ _, h⟩ => unreachIsNodeAtom h | ⟨Syntax.ident _ _ _ _, h⟩ => unreachIsNodeIdent h end SyntaxNode namespace Syntax def getAtomVal! : Syntax → String | atom _ val => val | _ => panic! "getAtomVal!: not an atom" def setAtomVal : Syntax → String → Syntax | atom info _, v => (atom info v) | stx, _ => stx @[inline] def ifNode {β} (stx : Syntax) (hyes : SyntaxNode → β) (hno : Unit → β) : β := match stx with | Syntax.node k args => hyes ⟨Syntax.node k args, IsNode.mk k args⟩ | _ => hno () @[inline] def ifNodeKind {β} (stx : Syntax) (kind : SyntaxNodeKind) (hyes : SyntaxNode → β) (hno : Unit → β) : β := match stx with | Syntax.node k args => if k == kind then hyes ⟨Syntax.node k args, IsNode.mk k args⟩ else hno () | _ => hno () def asNode : Syntax → SyntaxNode | Syntax.node kind args => ⟨Syntax.node kind args, IsNode.mk kind args⟩ | _ => ⟨Syntax.node nullKind #[], IsNode.mk nullKind #[]⟩ def getIdAt (stx : Syntax) (i : Nat) : Name := (stx.getArg i).getId @[inline] def modifyArgs (stx : Syntax) (fn : Array Syntax → Array Syntax) : Syntax := match stx with | node k args => node k (fn args) | stx => stx @[inline] def modifyArg (stx : Syntax) (i : Nat) (fn : Syntax → Syntax) : Syntax := match stx with | node k args => node k (args.modify i fn) | stx => stx @[specialize] partial def replaceM {m : Type → Type} [Monad m] (fn : Syntax → m (Option Syntax)) : Syntax → m (Syntax) | stx@(node kind args) => do match (← fn stx) with | some stx => return stx | none => return node kind (← args.mapM (replaceM fn)) | stx => do let o ← fn stx return o.getD stx @[specialize] partial def rewriteBottomUpM {m : Type → Type} [Monad m] (fn : Syntax → m (Syntax)) : Syntax → m (Syntax) | node kind args => do let args ← args.mapM (rewriteBottomUpM fn) fn (node kind args) | stx => fn stx @[inline] def rewriteBottomUp (fn : Syntax → Syntax) (stx : Syntax) : Syntax := Id.run $ stx.rewriteBottomUpM fn private def updateInfo : SourceInfo → String.Pos → String.Pos → SourceInfo | SourceInfo.original lead pos trail, leadStart, trailStop => SourceInfo.original { lead with startPos := leadStart } pos { trail with stopPos := trailStop } | info, _, _ => info private def chooseNiceTrailStop (trail : Substring) : String.Pos := trail.startPos + trail.posOf '\n' /- Remark: the State `String.Pos` is the `SourceInfo.trailing.stopPos` of the previous token, or the beginning of the String. -/ @[inline] private def updateLeadingAux : Syntax → StateM String.Pos (Option Syntax) | atom info@(SourceInfo.original lead pos trail) val => do let trailStop := chooseNiceTrailStop trail let newInfo := updateInfo info (← get) trailStop set trailStop pure $ some (atom newInfo val) | ident info@(SourceInfo.original lead pos trail) rawVal val pre => do let trailStop := chooseNiceTrailStop trail let newInfo := updateInfo info (← get) trailStop set trailStop pure $ some (ident newInfo rawVal val pre) | _ => pure none /-- Set `SourceInfo.leading` according to the trailing stop of the preceding token. The result is a round-tripping syntax tree IF, in the input syntax tree, * all leading stops, atom contents, and trailing starts are correct * trailing stops are between the trailing start and the next leading stop. Remark: after parsing, all `SourceInfo.leading` fields are empty. The `Syntax` argument is the output produced by the parser for `source`. This function "fixes" the `source.leading` field. Additionally, we try to choose "nicer" splits between leading and trailing stops according to some heuristics so that e.g. comments are associated to the (intuitively) correct token. Note that the `SourceInfo.trailing` fields must be correct. The implementation of this Function relies on this property. -/ def updateLeading : Syntax → Syntax := fun stx => (replaceM updateLeadingAux stx).run' 0 partial def updateTrailing (trailing : Substring) : Syntax → Syntax | Syntax.atom info val => Syntax.atom (info.updateTrailing trailing) val | Syntax.ident info rawVal val pre => Syntax.ident (info.updateTrailing trailing) rawVal val pre | n@(Syntax.node k args) => if args.size == 0 then n else let i := args.size - 1 let last := updateTrailing trailing args[i] let args := args.set! i last; Syntax.node k args | s => s partial def getTailWithPos : Syntax → Option Syntax | stx@(atom info _) => info.getPos?.map fun _ => stx | stx@(ident info ..) => info.getPos?.map fun _ => stx | node _ args => args.findSomeRev? getTailWithPos | _ => none structure TopDown where firstChoiceOnly : Bool stx : Syntax /-- `for _ in stx.topDown` iterates through each node and leaf in `stx` top-down, left-to-right. If `firstChoiceOnly` is `true`, only visit the first argument of each choice node. -/ def topDown (stx : Syntax) (firstChoiceOnly := false) : TopDown := ⟨firstChoiceOnly, stx⟩ partial instance : ForIn m TopDown Syntax where forIn := fun ⟨firstChoiceOnly, stx⟩ init f => do let rec @[specialize] loop stx b [Inhabited (typeOf% b)] := do match ← f stx b with | ForInStep.yield b' => let mut b := b' if let Syntax.node k args := stx then if firstChoiceOnly && k == choiceKind then return ← loop args[0] b else for arg in args do match ← loop arg b with | ForInStep.yield b' => b := b' | ForInStep.done b => return ForInStep.done b return ForInStep.yield b | ForInStep.done b => return ForInStep.done b match ← @loop stx init ⟨init⟩ with | ForInStep.yield b => return b | ForInStep.done b => return b partial def reprint (stx : Syntax) : Option String := OptionM.run do let mut s := "" for stx in stx.topDown (firstChoiceOnly := true) do match stx with | atom info val => s := s ++ reprintLeaf info val | ident info rawVal _ _ => s := s ++ reprintLeaf info rawVal.toString | node kind args => if kind == choiceKind then -- this visit the first arg twice, but that should hardly be a problem -- given that choice nodes are quite rare and small let s ← reprint args[0] for arg in args[1:] do let s' ← reprint stx guard (s == s') | _ => pure () return s where reprintLeaf (info : SourceInfo) (val : String) : String := match info with | SourceInfo.original lead _ trail => s!"{lead}{val}{trail}" -- no source info => add gracious amounts of whitespace to definitely separate tokens -- Note that the proper pretty printer does not use this function. -- The parser as well always produces source info, so round-tripping is still -- guaranteed. | _ => s!" {val} " def hasMissing (stx : Syntax) : Bool := do for stx in stx.topDown do if stx.isMissing then return true return false /-- Represents a cursor into a syntax tree that can be read, written, and advanced down/up/left/right. Indices are allowed to be out-of-bound, in which case `cur` is `Syntax.missing`. If the `Traverser` is used linearly, updates are linear in the `Syntax` object as well. -/ structure Traverser where cur : Syntax parents : Array Syntax idxs : Array Nat namespace Traverser def fromSyntax (stx : Syntax) : Traverser := ⟨stx, #[], #[]⟩ def setCur (t : Traverser) (stx : Syntax) : Traverser := { t with cur := stx } /-- Advance to the `idx`-th child of the current node. -/ def down (t : Traverser) (idx : Nat) : Traverser := if idx < t.cur.getNumArgs then { cur := t.cur.getArg idx, parents := t.parents.push $ t.cur.setArg idx arbitrary, idxs := t.idxs.push idx } else { cur := Syntax.missing, parents := t.parents.push t.cur, idxs := t.idxs.push idx } /-- Advance to the parent of the current node, if any. -/ def up (t : Traverser) : Traverser := if t.parents.size > 0 then let cur := if t.idxs.back < t.parents.back.getNumArgs then t.parents.back.setArg t.idxs.back t.cur else t.parents.back { cur := cur, parents := t.parents.pop, idxs := t.idxs.pop } else t /-- Advance to the left sibling of the current node, if any. -/ def left (t : Traverser) : Traverser := if t.parents.size > 0 then t.up.down (t.idxs.back - 1) else t /-- Advance to the right sibling of the current node, if any. -/ def right (t : Traverser) : Traverser := if t.parents.size > 0 then t.up.down (t.idxs.back + 1) else t end Traverser /-- Monad class that gives read/write access to a `Traverser`. -/ class MonadTraverser (m : Type → Type) where st : MonadState Traverser m namespace MonadTraverser variable {m : Type → Type} [Monad m] [t : MonadTraverser m] def getCur : m Syntax := Traverser.cur <$> t.st.get def setCur (stx : Syntax) : m Unit := @modify _ _ t.st (fun t => t.setCur stx) def goDown (idx : Nat) : m Unit := @modify _ _ t.st (fun t => t.down idx) def goUp : m Unit := @modify _ _ t.st (fun t => t.up) def goLeft : m Unit := @modify _ _ t.st (fun t => t.left) def goRight : m Unit := @modify _ _ t.st (fun t => t.right) def getIdx : m Nat := do let st ← t.st.get st.idxs.back?.getD 0 end MonadTraverser end Syntax namespace SyntaxNode @[inline] def getIdAt (n : SyntaxNode) (i : Nat) : Name := (n.getArg i).getId end SyntaxNode def mkListNode (args : Array Syntax) : Syntax := Syntax.node nullKind args namespace Syntax -- quotation node kinds are formed from a unique quotation name plus "quot" def isQuot : Syntax → Bool | Syntax.node (Name.str _ "quot" _) _ => true | Syntax.node `Lean.Parser.Term.dynamicQuot _ => true | _ => false def getQuotContent (stx : Syntax) : Syntax := if stx.isOfKind `Lean.Parser.Term.dynamicQuot then stx[3] else stx[1] -- antiquotation node kinds are formed from the original node kind (if any) plus "antiquot" def isAntiquot : Syntax → Bool | Syntax.node (Name.str _ "antiquot" _) _ => true | _ => false def mkAntiquotNode (term : Syntax) (nesting := 0) (name : Option String := none) (kind := Name.anonymous) : Syntax := let nesting := mkNullNode (mkArray nesting (mkAtom "$")) let term := match term.isIdent with | true => term | false => mkNode `antiquotNestedExpr #[mkAtom "(", term, mkAtom ")"] let name := match name with | some name => mkNode `antiquotName #[mkAtom ":", mkAtom name] | none => mkNullNode mkNode (kind ++ `antiquot) #[mkAtom "$", nesting, term, name] -- Antiquotations can be escaped as in `$$x`, which is useful for nesting macros. Also works for antiquotation splices. def isEscapedAntiquot (stx : Syntax) : Bool := !stx[1].getArgs.isEmpty -- Also works for antiquotation splices. def unescapeAntiquot (stx : Syntax) : Syntax := if isAntiquot stx then stx.setArg 1 $ mkNullNode stx[1].getArgs.pop else stx -- Also works for token antiquotations. def getAntiquotTerm (stx : Syntax) : Syntax := let e := if stx.isAntiquot then stx[2] else stx[3] if e.isIdent then e else -- `e` is from `"(" >> termParser >> ")"` e[1] def antiquotKind? : Syntax → Option SyntaxNodeKind | Syntax.node (Name.str k "antiquot" _) args => if args[3].isOfKind `antiquotName then some k else -- we treat all antiquotations where the kind was left implicit (`$e`) the same (see `elimAntiquotChoices`) some Name.anonymous | _ => none -- An "antiquotation splice" is something like `$[...]?` or `$[...]*`. def antiquotSpliceKind? : Syntax → Option SyntaxNodeKind | Syntax.node (Name.str k "antiquot_scope" _) args => some k | _ => none def isAntiquotSplice (stx : Syntax) : Bool := antiquotSpliceKind? stx |>.isSome def getAntiquotSpliceContents (stx : Syntax) : Array Syntax := stx[3].getArgs -- `$[..],*` or `$x,*` ~> `,*` def getAntiquotSpliceSuffix (stx : Syntax) : Syntax := if stx.isAntiquotSplice then stx[5] else stx[1] def mkAntiquotSpliceNode (kind : SyntaxNodeKind) (contents : Array Syntax) (suffix : String) (nesting := 0) : Syntax := let nesting := mkNullNode (mkArray nesting (mkAtom "$")) mkNode (kind ++ `antiquot_splice) #[mkAtom "$", nesting, mkAtom "[", mkNullNode contents, mkAtom "]", mkAtom suffix] -- `$x,*` etc. def antiquotSuffixSplice? : Syntax → Option SyntaxNodeKind | Syntax.node (Name.str k "antiquot_suffix_splice" _) args => some k | _ => none def isAntiquotSuffixSplice (stx : Syntax) : Bool := antiquotSuffixSplice? stx |>.isSome -- `$x` in the example above def getAntiquotSuffixSpliceInner (stx : Syntax) : Syntax := stx[0] def mkAntiquotSuffixSpliceNode (kind : SyntaxNodeKind) (inner : Syntax) (suffix : String) : Syntax := mkNode (kind ++ `antiquot_suffix_splice) #[inner, mkAtom suffix] def isTokenAntiquot (stx : Syntax) : Bool := stx.isOfKind `token_antiquot def isAnyAntiquot (stx : Syntax) : Bool := stx.isAntiquot || stx.isAntiquotSplice || stx.isAntiquotSuffixSplice || stx.isTokenAntiquot end Syntax end Lean
96ba679ac278ebf09fa4dac6182efa25dab9e73c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/galois_connection.lean
d248f07c181d33aba1c4f40161543c27da07a584
[]
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
28,686
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.order.complete_lattice import Mathlib.order.rel_iso import Mathlib.PostPort universes u v x w u_1 u_2 l namespace Mathlib /-! # Galois connections, insertions and coinsertions Galois connections are order theoretic adjoints, i.e. a pair of functions `u` and `l`, such that `∀a b, l a ≤ b ↔ a ≤ u b`. ## Main definitions * `galois_connection`: A Galois connection is a pair of functions `l` and `u` satisfying `l a ≤ b ↔ a ≤ u b`. They are special cases of adjoint functors in category theory, but do not depend on the category theory library in mathlib. * `galois_insertion`: A Galois insertion is a Galois connection where `l ∘ u = id` * `galois_coinsertion`: A Galois coinsertion is a Galois connection where `u ∘ l = id` ## Implementation details Galois insertions can be used to lift order structures from one type to another. For example if `α` is a complete lattice, and `l : α → β`, and `u : β → α` form a Galois insertion, then `β` is also a complete lattice. `l` is the lower adjoint and `u` is the upper adjoint. An example of this is the Galois insertion is in group thery. If `G` is a topological space, then there is a Galois insertion between the set of subsets of `G`, `set G`, and the set of subgroups of `G`, `subgroup G`. The left adjoint is `subgroup.closure`, taking the `subgroup` generated by a `set`, The right adjoint is the coercion from `subgroup G` to `set G`, taking the underlying set of an subgroup. Naively lifting a lattice structure along this Galois insertion would mean that the definition of `inf` on subgroups would be `subgroup.closure (↑S ∩ ↑T)`. This is an undesirable definition because the intersection of subgroups is already a subgroup, so there is no need to take the closure. For this reason a `choice` function is added as a field to the `galois_insertion` structure. It has type `Π S : set G, ↑(closure S) ≤ S → subgroup G`. When `↑(closure S) ≤ S`, then `S` is already a subgroup, so this function can be defined using `subgroup.mk` and not `closure`. This means the infimum of subgroups will be defined to be the intersection of sets, paired with a proof that intersection of subgroups is a subgroup, rather than the closure of the intersection. -/ /-- A Galois connection is a pair of functions `l` and `u` satisfying `l a ≤ b ↔ a ≤ u b`. They are special cases of adjoint functors in category theory, but do not depend on the category theory library in mathlib. -/ def galois_connection {α : Type u} {β : Type v} [preorder α] [preorder β] (l : α → β) (u : β → α) := ∀ (a : α) (b : β), l a ≤ b ↔ a ≤ u b /-- Makes a Galois connection from an order-preserving bijection. -/ theorem order_iso.to_galois_connection {α : Type u} {β : Type v} [preorder α] [preorder β] (oi : α ≃o β) : galois_connection ⇑oi ⇑(order_iso.symm oi) := fun (b : α) (g : β) => iff.symm (rel_iso.rel_symm_apply oi) namespace galois_connection theorem monotone_intro {α : Type u} {β : Type v} [preorder α] [preorder β] {l : α → β} {u : β → α} (hu : monotone u) (hl : monotone l) (hul : ∀ (a : α), a ≤ u (l a)) (hlu : ∀ (a : β), l (u a) ≤ a) : galois_connection l u := fun (a : α) (b : β) => { mp := fun (h : l a ≤ b) => le_trans (hul a) (hu h), mpr := fun (h : a ≤ u b) => le_trans (hl h) (hlu b) } theorem l_le {α : Type u} {β : Type v} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) {a : α} {b : β} : a ≤ u b → l a ≤ b := iff.mpr (gc a b) theorem le_u {α : Type u} {β : Type v} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) {a : α} {b : β} : l a ≤ b → a ≤ u b := iff.mp (gc a b) theorem le_u_l {α : Type u} {β : Type v} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) (a : α) : a ≤ u (l a) := le_u gc (le_refl (l a)) theorem l_u_le {α : Type u} {β : Type v} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) (a : β) : l (u a) ≤ a := l_le gc (le_refl (u a)) theorem monotone_u {α : Type u} {β : Type v} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) : monotone u := fun (a b : β) (H : a ≤ b) => le_u gc (le_trans (l_u_le gc a) H) theorem monotone_l {α : Type u} {β : Type v} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) : monotone l := fun (a b : α) (H : a ≤ b) => l_le gc (le_trans H (le_u_l gc b)) theorem upper_bounds_l_image_subset {α : Type u} {β : Type v} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) {s : set α} : upper_bounds (l '' s) ⊆ u ⁻¹' upper_bounds s := fun (b : β) (hb : b ∈ upper_bounds (l '' s)) (c : α) (this : c ∈ s) => le_u gc (hb (set.mem_image_of_mem l this)) theorem lower_bounds_u_image_subset {α : Type u} {β : Type v} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) {s : set β} : lower_bounds (u '' s) ⊆ l ⁻¹' lower_bounds s := fun (a : α) (ha : a ∈ lower_bounds (u '' s)) (c : β) (this : c ∈ s) => l_le gc (ha (set.mem_image_of_mem u this)) theorem is_lub_l_image {α : Type u} {β : Type v} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) {s : set α} {a : α} (h : is_lub s a) : is_lub (l '' s) (l a) := sorry theorem is_glb_u_image {α : Type u} {β : Type v} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) {s : set β} {b : β} (h : is_glb s b) : is_glb (u '' s) (u b) := sorry theorem is_glb_l {α : Type u} {β : Type v} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) {a : α} : is_glb (set_of fun (b : β) => a ≤ u b) (l a) := { left := fun (b : β) => l_le gc, right := fun (b : β) (h : b ∈ lower_bounds (set_of fun (b : β) => a ≤ u b)) => h (le_u_l gc a) } theorem is_lub_u {α : Type u} {β : Type v} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) {b : β} : is_lub (set_of fun (a : α) => l a ≤ b) (u b) := { left := fun (b_1 : α) => le_u gc, right := fun (b_1 : α) (h : b_1 ∈ upper_bounds (set_of fun (a : α) => l a ≤ b)) => h (l_u_le gc b) } theorem u_l_u_eq_u {α : Type u} {β : Type v} [partial_order α] [partial_order β] {l : α → β} {u : β → α} (gc : galois_connection l u) : u ∘ l ∘ u = u := funext fun (x : β) => le_antisymm (monotone_u gc (l_u_le gc x)) (le_u_l gc (u x)) theorem l_u_l_eq_l {α : Type u} {β : Type v} [partial_order α] [partial_order β] {l : α → β} {u : β → α} (gc : galois_connection l u) : l ∘ u ∘ l = l := funext fun (x : α) => le_antisymm (l_u_le gc (l x)) (monotone_l gc (le_u_l gc x)) theorem l_unique {α : Type u} {β : Type v} [partial_order α] [partial_order β] {l : α → β} {u : β → α} (gc : galois_connection l u) {l' : α → β} {u' : β → α} (gc' : galois_connection l' u') (hu : ∀ (b : β), u b = u' b) {a : α} : l a = l' a := le_antisymm (l_le gc (Eq.symm (hu (l' a)) ▸ le_u_l gc' a)) (l_le gc' (hu (l a) ▸ le_u_l gc a)) theorem u_unique {α : Type u} {β : Type v} [partial_order α] [partial_order β] {l : α → β} {u : β → α} (gc : galois_connection l u) {l' : α → β} {u' : β → α} (gc' : galois_connection l' u') (hl : ∀ (a : α), l a = l' a) {b : β} : u b = u' b := le_antisymm (le_u gc' (hl (u b) ▸ l_u_le gc b)) (le_u gc (Eq.symm (hl (u' b)) ▸ l_u_le gc' b)) theorem u_top {α : Type u} {β : Type v} [order_top α] [order_top β] {l : α → β} {u : β → α} (gc : galois_connection l u) : u ⊤ = ⊤ := sorry theorem l_bot {α : Type u} {β : Type v} [order_bot α] [order_bot β] {l : α → β} {u : β → α} (gc : galois_connection l u) : l ⊥ = ⊥ := sorry theorem l_sup {α : Type u} {β : Type v} {a₁ : α} {a₂ : α} [semilattice_sup α] [semilattice_sup β] {l : α → β} {u : β → α} (gc : galois_connection l u) : l (a₁ ⊔ a₂) = l a₁ ⊔ l a₂ := sorry theorem u_inf {α : Type u} {β : Type v} {b₁ : β} {b₂ : β} [semilattice_inf α] [semilattice_inf β] {l : α → β} {u : β → α} (gc : galois_connection l u) : u (b₁ ⊓ b₂) = u b₁ ⊓ u b₂ := sorry theorem l_supr {α : Type u} {β : Type v} {ι : Sort x} [complete_lattice α] [complete_lattice β] {l : α → β} {u : β → α} (gc : galois_connection l u) {f : ι → α} : l (supr f) = supr fun (i : ι) => l (f i) := sorry theorem u_infi {α : Type u} {β : Type v} {ι : Sort x} [complete_lattice α] [complete_lattice β] {l : α → β} {u : β → α} (gc : galois_connection l u) {f : ι → β} : u (infi f) = infi fun (i : ι) => u (f i) := sorry theorem l_Sup {α : Type u} {β : Type v} [complete_lattice α] [complete_lattice β] {l : α → β} {u : β → α} (gc : galois_connection l u) {s : set α} : l (Sup s) = supr fun (a : α) => supr fun (H : a ∈ s) => l a := sorry theorem u_Inf {α : Type u} {β : Type v} [complete_lattice α] [complete_lattice β] {l : α → β} {u : β → α} (gc : galois_connection l u) {s : set β} : u (Inf s) = infi fun (a : β) => infi fun (H : a ∈ s) => u a := sorry /- Constructing Galois connections -/ protected theorem id {α : Type u} [pα : preorder α] : galois_connection id id := fun (a b : α) => { mp := fun (x : id a ≤ b) => x, mpr := fun (x : a ≤ id b) => x } protected theorem compose {α : Type u} {β : Type v} {γ : Type w} [preorder α] [preorder β] [preorder γ] (l1 : α → β) (u1 : β → α) (l2 : β → γ) (u2 : γ → β) (gc1 : galois_connection l1 u1) (gc2 : galois_connection l2 u2) : galois_connection (l2 ∘ l1) (u1 ∘ u2) := sorry protected theorem dual {α : Type u} {β : Type v} [pα : preorder α] [pβ : preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) : galois_connection u l := fun (a : order_dual β) (b : order_dual α) => iff.symm (gc b a) protected theorem dfun {ι : Type u} {α : ι → Type v} {β : ι → Type w} [(i : ι) → preorder (α i)] [(i : ι) → preorder (β i)] (l : (i : ι) → α i → β i) (u : (i : ι) → β i → α i) (gc : ∀ (i : ι), galois_connection (l i) (u i)) : galois_connection (fun (a : (i : ι) → α i) (i : ι) => l i (a i)) fun (b : (i : ι) → β i) (i : ι) => u i (b i) := fun (a : (i : ι) → α i) (b : (i : ι) → β i) => forall_congr fun (i : ι) => gc i (a i) (b i) end galois_connection namespace nat theorem galois_connection_mul_div {k : ℕ} (h : 0 < k) : galois_connection (fun (n : ℕ) => n * k) fun (n : ℕ) => n / k := fun (x y : ℕ) => iff.symm (le_div_iff_mul_le x y h) end nat /-- A Galois insertion is a Galois connection where `l ∘ u = id`. It also contains a constructive choice function, to give better definitional equalities when lifting order structures. Dual to `galois_coinsertion` -/ structure galois_insertion {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (l : α → β) (u : β → α) where choice : (x : α) → u (l x) ≤ x → β gc : galois_connection l u le_l_u : ∀ (x : β), x ≤ l (u x) choice_eq : ∀ (a : α) (h : u (l a) ≤ a), choice a h = l a /-- A constructor for a Galois insertion with the trivial `choice` function. -/ def galois_insertion.monotone_intro {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] {l : α → β} {u : β → α} (hu : monotone u) (hl : monotone l) (hul : ∀ (a : α), a ≤ u (l a)) (hlu : ∀ (b : β), l (u b) = b) : galois_insertion l u := galois_insertion.mk (fun (x : α) (_x : u (l x) ≤ x) => l x) sorry sorry sorry /-- Makes a Galois insertion from an order-preserving bijection. -/ protected def rel_iso.to_galois_insertion {α : Type u} {β : Type v} [preorder α] [preorder β] (oi : α ≃o β) : galois_insertion ⇑oi ⇑(order_iso.symm oi) := galois_insertion.mk (fun (b : α) (h : coe_fn (order_iso.symm oi) (coe_fn oi b) ≤ b) => coe_fn oi b) (order_iso.to_galois_connection oi) sorry sorry /-- Make a `galois_insertion l u` from a `galois_connection l u` such that `∀ b, b ≤ l (u b)` -/ def galois_connection.to_galois_insertion {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) (h : ∀ (b : β), b ≤ l (u b)) : galois_insertion l u := galois_insertion.mk (fun (x : α) (_x : u (l x) ≤ x) => l x) gc h sorry /-- Lift the bottom along a Galois connection -/ def galois_connection.lift_order_bot {α : Type u_1} {β : Type u_2} [order_bot α] [partial_order β] {l : α → β} {u : β → α} (gc : galois_connection l u) : order_bot β := order_bot.mk (l ⊥) partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry namespace galois_insertion theorem l_u_eq {α : Type u} {β : Type v} {l : α → β} {u : β → α} [preorder α] [partial_order β] (gi : galois_insertion l u) (b : β) : l (u b) = b := le_antisymm (galois_connection.l_u_le (gc gi) b) (le_l_u gi b) theorem l_surjective {α : Type u} {β : Type v} {l : α → β} {u : β → α} [preorder α] [partial_order β] (gi : galois_insertion l u) : function.surjective l := fun (b : β) => Exists.intro (u b) (l_u_eq gi b) theorem u_injective {α : Type u} {β : Type v} {l : α → β} {u : β → α} [preorder α] [partial_order β] (gi : galois_insertion l u) : function.injective u := fun (a b : β) (h : u a = u b) => Eq.trans (Eq.trans (Eq.symm (l_u_eq gi a)) (congr_arg l h)) (l_u_eq gi b) theorem l_sup_u {α : Type u} {β : Type v} {l : α → β} {u : β → α} [semilattice_sup α] [semilattice_sup β] (gi : galois_insertion l u) (a : β) (b : β) : l (u a ⊔ u b) = a ⊔ b := sorry theorem l_supr_u {α : Type u} {β : Type v} {l : α → β} {u : β → α} [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u) {ι : Sort x} (f : ι → β) : l (supr fun (i : ι) => u (f i)) = supr fun (i : ι) => f i := Eq.trans (galois_connection.l_supr (gc gi)) (congr_arg supr (funext fun (i : ι) => l_u_eq gi (f i))) theorem l_supr_of_ul_eq_self {α : Type u} {β : Type v} {l : α → β} {u : β → α} [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u) {ι : Sort x} (f : ι → α) (hf : ∀ (i : ι), u (l (f i)) = f i) : l (supr fun (i : ι) => f i) = supr fun (i : ι) => l (f i) := sorry theorem l_inf_u {α : Type u} {β : Type v} {l : α → β} {u : β → α} [semilattice_inf α] [semilattice_inf β] (gi : galois_insertion l u) (a : β) (b : β) : l (u a ⊓ u b) = a ⊓ b := sorry theorem l_infi_u {α : Type u} {β : Type v} {l : α → β} {u : β → α} [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u) {ι : Sort x} (f : ι → β) : l (infi fun (i : ι) => u (f i)) = infi fun (i : ι) => f i := Eq.trans (congr_arg l (Eq.symm (galois_connection.u_infi (gc gi)))) (l_u_eq gi (infi fun (i : ι) => f i)) theorem l_infi_of_ul_eq_self {α : Type u} {β : Type v} {l : α → β} {u : β → α} [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u) {ι : Sort x} (f : ι → α) (hf : ∀ (i : ι), u (l (f i)) = f i) : l (infi fun (i : ι) => f i) = infi fun (i : ι) => l (f i) := sorry theorem u_le_u_iff {α : Type u} {β : Type v} {l : α → β} {u : β → α} [preorder α] [preorder β] (gi : galois_insertion l u) {a : β} {b : β} : u a ≤ u b ↔ a ≤ b := { mp := fun (h : u a ≤ u b) => le_trans (le_l_u gi a) (galois_connection.l_le (gc gi) h), mpr := fun (h : a ≤ b) => galois_connection.monotone_u (gc gi) h } theorem strict_mono_u {α : Type u} {β : Type v} {l : α → β} {u : β → α} [preorder α] [partial_order β] (gi : galois_insertion l u) : strict_mono u := strict_mono_of_le_iff_le fun (_x _x_1 : β) => iff.symm (u_le_u_iff gi) /-- Lift the suprema along a Galois insertion -/ def lift_semilattice_sup {α : Type u} {β : Type v} {l : α → β} {u : β → α} [partial_order β] [semilattice_sup α] (gi : galois_insertion l u) : semilattice_sup β := semilattice_sup.mk (fun (a b : β) => l (u a ⊔ u b)) partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry sorry sorry /-- Lift the infima along a Galois insertion -/ def lift_semilattice_inf {α : Type u} {β : Type v} {l : α → β} {u : β → α} [partial_order β] [semilattice_inf α] (gi : galois_insertion l u) : semilattice_inf β := semilattice_inf.mk (fun (a b : β) => choice gi (u a ⊓ u b) sorry) partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry sorry sorry /-- Lift the suprema and infima along a Galois insertion -/ def lift_lattice {α : Type u} {β : Type v} {l : α → β} {u : β → α} [partial_order β] [lattice α] (gi : galois_insertion l u) : lattice β := lattice.mk semilattice_sup.sup semilattice_sup.le semilattice_sup.lt sorry sorry sorry sorry sorry sorry semilattice_inf.inf sorry sorry sorry /-- Lift the top along a Galois insertion -/ def lift_order_top {α : Type u} {β : Type v} {l : α → β} {u : β → α} [partial_order β] [order_top α] (gi : galois_insertion l u) : order_top β := order_top.mk (choice gi ⊤ sorry) partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry /-- Lift the top, bottom, suprema, and infima along a Galois insertion -/ def lift_bounded_lattice {α : Type u} {β : Type v} {l : α → β} {u : β → α} [partial_order β] [bounded_lattice α] (gi : galois_insertion l u) : bounded_lattice β := bounded_lattice.mk lattice.sup lattice.le lattice.lt sorry sorry sorry sorry sorry sorry lattice.inf sorry sorry sorry order_top.top sorry order_bot.bot sorry /-- Lift all suprema and infima along a Galois insertion -/ def lift_complete_lattice {α : Type u} {β : Type v} {l : α → β} {u : β → α} [partial_order β] [complete_lattice α] (gi : galois_insertion l u) : complete_lattice β := complete_lattice.mk bounded_lattice.sup bounded_lattice.le bounded_lattice.lt sorry sorry sorry sorry sorry sorry bounded_lattice.inf sorry sorry sorry bounded_lattice.top sorry bounded_lattice.bot sorry (fun (s : set β) => l (supr fun (b : β) => supr fun (H : b ∈ s) => u b)) (fun (s : set β) => choice gi (infi fun (b : β) => infi fun (H : b ∈ s) => u b) sorry) sorry sorry sorry sorry end galois_insertion /-- A Galois coinsertion is a Galois connection where `u ∘ l = id`. It also contains a constructive choice function, to give better definitional equalities when lifting order structures. Dual to `galois_insertion` -/ structure galois_coinsertion {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (l : α → β) (u : β → α) where choice : (x : β) → x ≤ l (u x) → α gc : galois_connection l u u_l_le : ∀ (x : α), u (l x) ≤ x choice_eq : ∀ (a : β) (h : a ≤ l (u a)), choice a h = u a /-- Make a `galois_insertion u l` in the `order_dual`, from a `galois_coinsertion l u` -/ def galois_coinsertion.dual {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] {l : α → β} {u : β → α} : galois_coinsertion l u → galois_insertion u l := fun (x : galois_coinsertion l u) => galois_insertion.mk (galois_coinsertion.choice x) sorry (galois_coinsertion.u_l_le x) (galois_coinsertion.choice_eq x) /-- Make a `galois_coinsertion u l` in the `order_dual`, from a `galois_insertion l u` -/ def galois_insertion.dual {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] {l : α → β} {u : β → α} : galois_insertion l u → galois_coinsertion u l := fun (x : galois_insertion l u) => galois_coinsertion.mk (galois_insertion.choice x) sorry (galois_insertion.le_l_u x) (galois_insertion.choice_eq x) /-- Make a `galois_coinsertion l u` from a `galois_insertion l u` in the `order_dual` -/ def galois_coinsertion.of_dual {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] {l : α → β} {u : β → α} : galois_insertion u l → galois_coinsertion l u := fun (x : galois_insertion u l) => galois_coinsertion.mk (galois_insertion.choice x) sorry sorry sorry /-- Make a `galois_insertion l u` from a `galois_coinsertion l u` in the `order_dual` -/ def galois_insertion.of_dual {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] {l : α → β} {u : β → α} : galois_coinsertion u l → galois_insertion l u := fun (x : galois_coinsertion u l) => galois_insertion.mk (galois_coinsertion.choice x) sorry sorry sorry /-- Makes a Galois coinsertion from an order-preserving bijection. -/ protected def rel_iso.to_galois_coinsertion {α : Type u} {β : Type v} [preorder α] [preorder β] (oi : α ≃o β) : galois_coinsertion ⇑oi ⇑(order_iso.symm oi) := galois_coinsertion.mk (fun (b : β) (h : b ≤ coe_fn oi (coe_fn (order_iso.symm oi) b)) => coe_fn (order_iso.symm oi) b) (order_iso.to_galois_connection oi) sorry sorry /-- A constructor for a Galois coinsertion with the trivial `choice` function. -/ def galois_coinsertion.monotone_intro {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] {l : α → β} {u : β → α} (hu : monotone u) (hl : monotone l) (hlu : ∀ (b : β), l (u b) ≤ b) (hul : ∀ (a : α), u (l a) = a) : galois_coinsertion l u := galois_coinsertion.of_dual (galois_insertion.monotone_intro (monotone.order_dual hl) (monotone.order_dual hu) hlu hul) /-- Make a `galois_coinsertion l u` from a `galois_connection l u` such that `∀ b, b ≤ l (u b)` -/ def galois_connection.to_galois_coinsertion {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) (h : ∀ (a : α), u (l a) ≤ a) : galois_coinsertion l u := galois_coinsertion.mk (fun (x : β) (_x : x ≤ l (u x)) => u x) gc h sorry /-- Lift the top along a Galois connection -/ def galois_connection.lift_order_top {α : Type u_1} {β : Type u_2} [partial_order α] [order_top β] {l : α → β} {u : β → α} (gc : galois_connection l u) : order_top α := order_top.mk (u ⊤) partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry namespace galois_coinsertion theorem u_l_eq {α : Type u} {β : Type v} {l : α → β} {u : β → α} [partial_order α] [preorder β] (gi : galois_coinsertion l u) (a : α) : u (l a) = a := galois_insertion.l_u_eq (dual gi) a theorem u_surjective {α : Type u} {β : Type v} {l : α → β} {u : β → α} [partial_order α] [preorder β] (gi : galois_coinsertion l u) : function.surjective u := galois_insertion.l_surjective (dual gi) theorem l_injective {α : Type u} {β : Type v} {l : α → β} {u : β → α} [partial_order α] [preorder β] (gi : galois_coinsertion l u) : function.injective l := galois_insertion.u_injective (dual gi) theorem u_inf_l {α : Type u} {β : Type v} {l : α → β} {u : β → α} [semilattice_inf α] [semilattice_inf β] (gi : galois_coinsertion l u) (a : α) (b : α) : u (l a ⊓ l b) = a ⊓ b := galois_insertion.l_sup_u (dual gi) a b theorem u_infi_l {α : Type u} {β : Type v} {l : α → β} {u : β → α} [complete_lattice α] [complete_lattice β] (gi : galois_coinsertion l u) {ι : Sort x} (f : ι → α) : u (infi fun (i : ι) => l (f i)) = infi fun (i : ι) => f i := galois_insertion.l_supr_u (dual gi) fun (i : ι) => f i theorem u_infi_of_lu_eq_self {α : Type u} {β : Type v} {l : α → β} {u : β → α} [complete_lattice α] [complete_lattice β] (gi : galois_coinsertion l u) {ι : Sort x} (f : ι → β) (hf : ∀ (i : ι), l (u (f i)) = f i) : u (infi fun (i : ι) => f i) = infi fun (i : ι) => u (f i) := galois_insertion.l_supr_of_ul_eq_self (dual gi) (fun (i : ι) => f i) hf theorem u_sup_l {α : Type u} {β : Type v} {l : α → β} {u : β → α} [semilattice_sup α] [semilattice_sup β] (gi : galois_coinsertion l u) (a : α) (b : α) : u (l a ⊔ l b) = a ⊔ b := galois_insertion.l_inf_u (dual gi) a b theorem u_supr_l {α : Type u} {β : Type v} {l : α → β} {u : β → α} [complete_lattice α] [complete_lattice β] (gi : galois_coinsertion l u) {ι : Sort x} (f : ι → α) : u (supr fun (i : ι) => l (f i)) = supr fun (i : ι) => f i := galois_insertion.l_infi_u (dual gi) fun (i : ι) => f i theorem u_supr_of_lu_eq_self {α : Type u} {β : Type v} {l : α → β} {u : β → α} [complete_lattice α] [complete_lattice β] (gi : galois_coinsertion l u) {ι : Sort x} (f : ι → β) (hf : ∀ (i : ι), l (u (f i)) = f i) : u (supr fun (i : ι) => f i) = supr fun (i : ι) => u (f i) := galois_insertion.l_infi_of_ul_eq_self (dual gi) (fun (i : ι) => f i) hf theorem l_le_l_iff {α : Type u} {β : Type v} {l : α → β} {u : β → α} [preorder α] [preorder β] (gi : galois_coinsertion l u) {a : α} {b : α} : l a ≤ l b ↔ a ≤ b := galois_insertion.u_le_u_iff (dual gi) theorem strict_mono_l {α : Type u} {β : Type v} {l : α → β} {u : β → α} [partial_order α] [preorder β] (gi : galois_coinsertion l u) : strict_mono l := fun (a b : α) (h : a < b) => galois_insertion.strict_mono_u (dual gi) h /-- Lift the infima along a Galois coinsertion -/ def lift_semilattice_inf {α : Type u} {β : Type v} {l : α → β} {u : β → α} [partial_order α] [semilattice_inf β] (gi : galois_coinsertion l u) : semilattice_inf α := semilattice_inf.mk (fun (a b : α) => u (l a ⊓ l b)) partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry sorry sorry /-- Lift the suprema along a Galois coinsertion -/ def lift_semilattice_sup {α : Type u} {β : Type v} {l : α → β} {u : β → α} [partial_order α] [semilattice_sup β] (gi : galois_coinsertion l u) : semilattice_sup α := semilattice_sup.mk (fun (a b : α) => choice gi (l a ⊔ l b) sorry) partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry sorry sorry /-- Lift the suprema and infima along a Galois coinsertion -/ def lift_lattice {α : Type u} {β : Type v} {l : α → β} {u : β → α} [partial_order α] [lattice β] (gi : galois_coinsertion l u) : lattice α := lattice.mk semilattice_sup.sup semilattice_sup.le semilattice_sup.lt sorry sorry sorry sorry sorry sorry semilattice_inf.inf sorry sorry sorry /-- Lift the bot along a Galois coinsertion -/ def lift_order_bot {α : Type u} {β : Type v} {l : α → β} {u : β → α} [partial_order α] [order_bot β] (gi : galois_coinsertion l u) : order_bot α := order_bot.mk (choice gi ⊥ sorry) partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry /-- Lift the top, bottom, suprema, and infima along a Galois coinsertion -/ def lift_bounded_lattice {α : Type u} {β : Type v} {l : α → β} {u : β → α} [partial_order α] [bounded_lattice β] (gi : galois_coinsertion l u) : bounded_lattice α := bounded_lattice.mk lattice.sup lattice.le lattice.lt sorry sorry sorry sorry sorry sorry lattice.inf sorry sorry sorry order_top.top sorry order_bot.bot sorry /-- Lift all suprema and infima along a Galois coinsertion -/ def lift_complete_lattice {α : Type u} {β : Type v} {l : α → β} {u : β → α} [partial_order α] [complete_lattice β] (gi : galois_coinsertion l u) : complete_lattice α := complete_lattice.mk bounded_lattice.sup bounded_lattice.le bounded_lattice.lt sorry sorry sorry sorry sorry sorry bounded_lattice.inf sorry sorry sorry bounded_lattice.top sorry bounded_lattice.bot sorry (fun (s : set α) => choice gi (supr fun (a : α) => supr fun (H : a ∈ s) => l a) sorry) (fun (s : set α) => u (infi fun (a : α) => infi fun (H : a ∈ s) => l a)) sorry sorry sorry sorry end galois_coinsertion /-- If `α` is a partial order with bottom element (e.g., `ℕ`, `ℝ≥0`), then `λ o : with_bot α, o.get_or_else ⊥` and coercion form a Galois insertion. -/ def with_bot.gi_get_or_else_bot {α : Type u} [order_bot α] : galois_insertion (fun (o : with_bot α) => option.get_or_else o ⊥) coe := galois_insertion.mk (fun (o : with_bot α) (ho : ↑(option.get_or_else o ⊥) ≤ o) => option.get_or_else o ⊥) sorry sorry sorry
3d42fabed860f938a3ec5eb18a0677e443f4b659
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/linear_algebra/bilinear_form/tensor_product.lean
47f45b2f6360ad391f2ae02336d45fd441e00cad
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
3,177
lean
/- Copyright (c) 2023 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import linear_algebra.bilinear_form import linear_algebra.tensor_product /-! # The bilinear form on a tensor product > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Main definitions * `bilin_form.tensor_distrib (B₁ ⊗ₜ B₂)`: the bilinear form on `M₁ ⊗ M₂` constructed by applying `B₁` on `M₁` and `B₂` on `M₂`. * `bilin_form.tensor_distrib_equiv`: `bilin_form.tensor_distrib` as an equivalence on finite free modules. -/ universes u v w variables {ι : Type*} {R : Type*} {M₁ M₂ : Type*} open_locale tensor_product namespace bilin_form section comm_semiring variables [comm_semiring R] variables [add_comm_monoid M₁] [add_comm_monoid M₂] variables [module R M₁] [module R M₂] /-- The tensor product of two bilinear forms injects into bilinear forms on tensor products. -/ def tensor_distrib : bilin_form R M₁ ⊗[R] bilin_form R M₂ →ₗ[R] bilin_form R (M₁ ⊗[R] M₂) := ((tensor_product.tensor_tensor_tensor_comm R _ _ _ _).dual_map ≪≫ₗ (tensor_product.lift.equiv R _ _ _).symm ≪≫ₗ linear_map.to_bilin).to_linear_map ∘ₗ tensor_product.dual_distrib R _ _ ∘ₗ (tensor_product.congr (bilin_form.to_lin ≪≫ₗ tensor_product.lift.equiv R _ _ _) (bilin_form.to_lin ≪≫ₗ tensor_product.lift.equiv R _ _ _)).to_linear_map @[simp] lemma tensor_distrib_tmul (B₁ : bilin_form R M₁) (B₂ : bilin_form R M₂) (m₁ : M₁) (m₂ : M₂) (m₁' : M₁) (m₂' : M₂) : tensor_distrib (B₁ ⊗ₜ B₂) (m₁ ⊗ₜ m₂) (m₁' ⊗ₜ m₂') = B₁ m₁ m₁' * B₂ m₂ m₂' := rfl /-- The tensor product of two bilinear forms, a shorthand for dot notation. -/ @[reducible] protected def tmul (B₁ : bilin_form R M₁) (B₂ : bilin_form R M₂) : bilin_form R (M₁ ⊗[R] M₂) := tensor_distrib (B₁ ⊗ₜ[R] B₂) end comm_semiring section comm_ring variables [comm_ring R] variables [add_comm_group M₁] [add_comm_group M₂] variables [module R M₁] [module R M₂] variables [module.free R M₁] [module.finite R M₁] variables [module.free R M₂] [module.finite R M₂] variables [nontrivial R] /-- `tensor_distrib` as an equivalence. -/ noncomputable def tensor_distrib_equiv : bilin_form R M₁ ⊗[R] bilin_form R M₂ ≃ₗ[R] bilin_form R (M₁ ⊗[R] M₂) := -- the same `linear_equiv`s as from `tensor_distrib`, but with the inner linear map also as an -- equiv tensor_product.congr (bilin_form.to_lin ≪≫ₗ tensor_product.lift.equiv R _ _ _) (bilin_form.to_lin ≪≫ₗ tensor_product.lift.equiv R _ _ _) ≪≫ₗ tensor_product.dual_distrib_equiv R (M₁ ⊗ M₁) (M₂ ⊗ M₂) ≪≫ₗ (tensor_product.tensor_tensor_tensor_comm R _ _ _ _).dual_map ≪≫ₗ (tensor_product.lift.equiv R _ _ _).symm ≪≫ₗ linear_map.to_bilin @[simp] lemma tensor_distrib_equiv_apply (B : bilin_form R M₁ ⊗ bilin_form R M₂) : tensor_distrib_equiv B = tensor_distrib B := rfl end comm_ring end bilin_form
18233871c4566f77e9fb3061d512cf3911198225
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebraic_topology/Moore_complex.lean
d9760eb05bb791e6925ee8473e6f3722f62c4667
[ "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
5,545
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.homology.homological_complex import algebraic_topology.simplicial_object import category_theory.abelian.basic /-! ## Moore complex We construct the normalized Moore complex, as a functor `simplicial_object C ⥤ chain_complex C ℕ`, for any abelian category `C`. The `n`-th object is intersection of the kernels of `X.δ i : X.obj n ⟶ X.obj (n-1)`, for `i = 1, ..., n`. The differentials are induced from `X.δ 0`, which maps each of these intersections of kernels to the next. This functor is one direction of the Dold-Kan equivalence, which we're still working towards. ### References * https://stacks.math.columbia.edu/tag/0194 * https://ncatlab.org/nlab/show/Moore+complex -/ universes v u noncomputable theory open category_theory category_theory.limits open opposite namespace algebraic_topology variables {C : Type*} [category C] [abelian C] local attribute [instance] abelian.has_pullbacks /-! The definitions in this namespace are all auxiliary definitions for `normalized_Moore_complex` and should usually only be accessed via that. -/ namespace normalized_Moore_complex open category_theory.subobject variables (X : simplicial_object C) /-- The normalized Moore complex in degree `n`, as a subobject of `X n`. -/ @[simp] def obj_X : Π n : ℕ, subobject (X.obj (op (simplex_category.mk n))) | 0 := ⊤ | (n+1) := finset.univ.inf (λ k : fin (n+1), kernel_subobject (X.δ k.succ)) /-- The differentials in the normalized Moore complex. -/ @[simp] def obj_d : Π n : ℕ, (obj_X X (n+1) : C) ⟶ (obj_X X n : C) | 0 := subobject.arrow _ ≫ X.δ (0 : fin 2) ≫ inv ((⊤ : subobject _).arrow) | (n+1) := begin -- The differential is `subobject.arrow _ ≫ X.δ (0 : fin (n+3))`, -- factored through the intersection of the kernels. refine factor_thru _ (arrow _ ≫ X.δ (0 : fin (n+3))) _, -- We now need to show that it factors! -- A morphism factors through an intersection of subobjects if it factors through each. refine ((finset_inf_factors _).mpr (λ i m, _)), -- A morphism `f` factors through the kernel of `g` exactly if `f ≫ g = 0`. apply kernel_subobject_factors, -- Use a simplicial identity dsimp [obj_X], erw [category.assoc, ←X.δ_comp_δ (fin.zero_le i.succ), ←category.assoc], -- It's the first two factors which are zero. convert zero_comp, -- We can rewrite the arrow out of the intersection of all the kernels as a composition -- of a morphism we don't care about with the arrow out of the kernel of `X.δ i.succ.succ`. rw ←factor_thru_arrow _ _ (finset_inf_arrow_factors finset.univ _ i.succ (by simp)), -- It's the second two factors which are zero. rw [category.assoc], convert comp_zero, exact kernel_subobject_arrow_comp _, end lemma d_squared (n : ℕ) : obj_d X (n+1) ≫ obj_d X n = 0 := begin -- It's a pity we need to do a case split here; -- after the first simp the proofs are almost identical cases n; dsimp, { simp only [subobject.factor_thru_arrow_assoc], slice_lhs 2 3 { erw ←X.δ_comp_δ (fin.zero_le 0), }, rw ←factor_thru_arrow _ _ (finset_inf_arrow_factors finset.univ _ (0 : fin 2) (by simp)), slice_lhs 2 3 { rw [kernel_subobject_arrow_comp], }, simp, }, { simp [factor_thru_right], slice_lhs 2 3 { erw ←X.δ_comp_δ (fin.zero_le 0), }, rw ←factor_thru_arrow _ _ (finset_inf_arrow_factors finset.univ _ (0 : fin (n+3)) (by simp)), slice_lhs 2 3 { rw [kernel_subobject_arrow_comp], }, simp, }, end /-- The normalized Moore complex functor, on objects. -/ @[simps] def obj (X : simplicial_object C) : chain_complex C ℕ := chain_complex.of (λ n, (obj_X X n : C)) -- the coercion here picks a representative of the subobject (obj_d X) (d_squared X) variables {X} {Y : simplicial_object C} (f : X ⟶ Y) /-- The normalized Moore complex functor, on morphisms. -/ @[simps] def map (f : X ⟶ Y) : obj X ⟶ obj Y := chain_complex.of_hom _ _ _ _ _ _ (λ n, begin refine factor_thru _ (arrow _ ≫ f.app (op (simplex_category.mk n))) _, cases n; dsimp, { apply top_factors, }, { refine (finset_inf_factors _).mpr (λ i m, _), apply kernel_subobject_factors, slice_lhs 2 3 { erw ←f.naturality, }, rw ←factor_thru_arrow _ _ (finset_inf_arrow_factors finset.univ _ i (by simp)), slice_lhs 2 3 { erw [kernel_subobject_arrow_comp], }, simp, } end) (λ n, begin cases n; dsimp, { ext, simp, erw f.naturality, refl, }, { ext, simp, erw f.naturality, refl, }, end) end normalized_Moore_complex open normalized_Moore_complex variables (C) /-- The (normalized) Moore complex of a simplicial object `X` in an abelian category `C`. The `n`-th object is intersection of the kernels of `X.δ i : X.obj n ⟶ X.obj (n-1)`, for `i = 1, ..., n`. The differentials are induced from `X.δ 0`, which maps each of these intersections of kernels to the next. -/ @[simps] def normalized_Moore_complex : simplicial_object C ⥤ chain_complex C ℕ := { obj := obj, map := λ X Y f, map f, map_id' := λ X, by { ext n, cases n; { dsimp, simp, }, }, map_comp' := λ X Y Z f g, by { ext n, cases n; simp, }, } variable {C} @[simp] lemma normalized_Moore_complex_obj_d (X : simplicial_object C) (n : ℕ) : ((normalized_Moore_complex C).obj X).d (n+1) n = normalized_Moore_complex.obj_d X n := by apply chain_complex.of_d end algebraic_topology
9705c93d3dd8beb6133c8aaaf0487b01a1db46b8
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/category/Top/adjunctions.lean
d819c51090958fafdeb1ccd171e76dc44ff48699
[]
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,521
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.category.Top.basic import Mathlib.category_theory.adjunction.basic import Mathlib.PostPort universes u namespace Mathlib namespace Top /-- Equipping a type with the discrete topology is left adjoint to the forgetful functor `Top ⥤ Type`. -/ def adj₁ : discrete ⊣ category_theory.forget Top := category_theory.adjunction.mk (fun (X : Type u) (Y : Top) => equiv.mk (fun (f : category_theory.functor.obj discrete X ⟶ Y) => ⇑f) (fun (f : X ⟶ category_theory.functor.obj (category_theory.forget Top) Y) => continuous_map.mk f) sorry sorry) (category_theory.nat_trans.mk fun (X : Type u) => id) (category_theory.nat_trans.mk fun (X : Top) => continuous_map.mk id) /-- Equipping a type with the trivial topology is right adjoint to the forgetful functor `Top ⥤ Type`. -/ def adj₂ : category_theory.forget Top ⊣ trivial := category_theory.adjunction.mk (fun (X : Top) (Y : Type u) => equiv.mk (fun (f : category_theory.functor.obj (category_theory.forget Top) X ⟶ Y) => continuous_map.mk f) (fun (f : X ⟶ category_theory.functor.obj trivial Y) => ⇑f) sorry sorry) (category_theory.nat_trans.mk fun (X : Top) => continuous_map.mk id) (category_theory.nat_trans.mk fun (X : Type u) => id)
ae462b61fb084875d9c27067f98132a0214cc7d9
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/data/polynomial/degree/card_pow_degree.lean
87eedfab5ab1362332da594620da004f506ef416
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,696
lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import algebra.euclidean_absolute_value import data.polynomial.field_division /-! # Absolute value on polynomials over a finite field. Let `Fq` be a finite field of cardinality `q`, then the map sending a polynomial `p` to `q ^ degree p` (where `q ^ degree 0 = 0`) is an absolute value. ## Main definitions * `polynomial.card_pow_degree` is an absolute value on `𝔽_q[t]`, the ring of polynomials over a finite field of cardinality `q`, mapping a polynomial `p` to `q ^ degree p` (where `q ^ degree 0 = 0`) ## Main results * `polynomial.card_pow_degree_is_euclidean`: `card_pow_degree` respects the Euclidean domain structure on the ring of polynomials -/ namespace polynomial variables {Fq : Type*} [field Fq] [fintype Fq] open absolute_value open_locale classical /-- `card_pow_degree` is the absolute value on `𝔽_q[t]` sending `f` to `q ^ degree f`. `card_pow_degree 0` is defined to be `0`. -/ noncomputable def card_pow_degree : absolute_value (polynomial Fq) ℤ := have card_pos : 0 < fintype.card Fq := fintype.card_pos_iff.mpr infer_instance, have pow_pos : ∀ n, 0 < (fintype.card Fq : ℤ) ^ n := λ n, pow_pos (int.coe_nat_pos.mpr card_pos) n, { to_fun := λ p, if p = 0 then 0 else fintype.card Fq ^ p.nat_degree, nonneg' := λ p, by { split_ifs, { refl }, exact pow_nonneg (int.coe_zero_le _) _ }, eq_zero' := λ p, ite_eq_left_iff.trans $ ⟨λ h, by { contrapose! h, exact ⟨h, (pow_pos _).ne'⟩ }, absurd⟩, add_le' := λ p q, begin by_cases hp : p = 0, { simp [hp] }, by_cases hq : q = 0, { simp [hq] }, by_cases hpq : p + q = 0, { simp only [hpq, hp, hq, eq_self_iff_true, if_true, if_false], exact add_nonneg (pow_pos _).le (pow_pos _).le }, simp only [hpq, hp, hq, if_false], refine le_trans (pow_le_pow (by linarith) (polynomial.nat_degree_add_le _ _)) _, refine le_trans (le_max_iff.mpr _) (max_le_add_of_nonneg (pow_nonneg (by linarith) _) (pow_nonneg (by linarith) _)), exact (max_choice p.nat_degree q.nat_degree).imp (λ h, by rw [h]) (λ h, by rw [h]) end, map_mul' := λ p q, begin by_cases hp : p = 0, { simp [hp] }, by_cases hq : q = 0, { simp [hq] }, have hpq : p * q ≠ 0 := mul_ne_zero hp hq, simp only [hpq, hp, hq, eq_self_iff_true, if_true, if_false, polynomial.nat_degree_mul hp hq, pow_add], end } lemma card_pow_degree_apply (p : polynomial Fq) : card_pow_degree p = if p = 0 then 0 else fintype.card Fq ^ nat_degree p := rfl @[simp] lemma card_pow_degree_zero : card_pow_degree (0 : polynomial Fq) = 0 := if_pos rfl @[simp] lemma card_pow_degree_nonzero (p : polynomial Fq) (hp : p ≠ 0) : card_pow_degree p = fintype.card Fq ^ p.nat_degree := if_neg hp lemma card_pow_degree_is_euclidean : is_euclidean (card_pow_degree : absolute_value (polynomial Fq) ℤ) := have card_pos : 0 < fintype.card Fq := fintype.card_pos_iff.mpr infer_instance, have pow_pos : ∀ n, 0 < (fintype.card Fq : ℤ) ^ n := λ n, pow_pos (int.coe_nat_pos.mpr card_pos) n, { map_lt_map_iff' := λ p q, begin simp only [euclidean_domain.r, card_pow_degree_apply], split_ifs with hp hq hq, { simp only [hp, hq, lt_self_iff_false] }, { simp only [hp, hq, degree_zero, ne.def, bot_lt_iff_ne_bot, degree_eq_bot, pow_pos, not_false_iff] }, { simp only [hp, hq, degree_zero, not_lt_bot, (pow_pos _).not_lt] }, { rw [degree_eq_nat_degree hp, degree_eq_nat_degree hq, with_bot.coe_lt_coe, pow_lt_pow_iff], exact_mod_cast @fintype.one_lt_card Fq _ _ }, end } end polynomial
ea26c79add7ec50900bb992fee979d5b1c0567b4
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/tactic/tidy.lean
d153a3e21fa5a646330c8f6d8508e6939ef1b8b8
[ "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,344
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import tactic.auto_cases import tactic.chain import tactic.norm_cast namespace tactic namespace tidy /-- Tag interactive tactics (locally) with `[tidy]` to add them to the list of default tactics called by `tidy`. -/ meta def tidy_attribute : user_attribute := { name := `tidy, descr := "A tactic that should be called by `tidy`." } add_tactic_doc { name := "tidy", category := doc_category.attr, decl_names := [`tactic.tidy.tidy_attribute], tags := ["search"] } run_cmd attribute.register ``tidy_attribute meta def run_tactics : tactic string := do names ← attribute.get_instances `tidy, first (names.map name_to_tactic) <|> fail "no @[tidy] tactics succeeded" @[hint_tactic] meta def ext1_wrapper : tactic string := do ng ← num_goals, ext1 [] {apply_cfg . new_goals := new_goals.all}, ng' ← num_goals, return $ if ng' > ng then "tactic.ext1 [] {new_goals := tactic.new_goals.all}" else "ext1" meta def default_tactics : list (tactic string) := [ reflexivity >> pure "refl", `[exact dec_trivial] >> pure "exact dec_trivial", propositional_goal >> assumption >> pure "assumption", intros1 >>= λ ns, pure ("intros " ++ (" ".intercalate (ns.map (λ e, e.to_string)))), auto_cases, `[apply_auto_param] >> pure "apply_auto_param", `[dsimp at *] >> pure "dsimp at *", `[simp at *] >> pure "simp at *", ext1_wrapper, fsplit >> pure "fsplit", injections_and_clear >> pure "injections_and_clear", propositional_goal >> (`[solve_by_elim]) >> pure "solve_by_elim", `[norm_cast] >> pure "norm_cast", `[unfold_coes] >> pure "unfold_coes", `[unfold_aux] >> pure "unfold_aux", tidy.run_tactics ] meta structure cfg := (trace_result : bool := ff) (trace_result_prefix : string := "Try this: ") (tactics : list (tactic string) := default_tactics) declare_trace tidy meta def core (cfg : cfg := {}) : tactic (list string) := do results ← chain cfg.tactics, when (cfg.trace_result) $ trace (cfg.trace_result_prefix ++ (", ".intercalate results)), return results end tidy meta def tidy (cfg : tidy.cfg := {}) := tactic.tidy.core cfg >> skip namespace interactive open lean.parser interactive /-- Use a variety of conservative tactics to solve goals. `tidy?` reports back the tactic script it found. As an example ```lean example : ∀ x : unit, x = unit.star := begin tidy? -- Prints the trace message: "Try this: intros x, exact dec_trivial" end ``` The default list of tactics is stored in `tactic.tidy.default_tidy_tactics`. This list can be overridden using `tidy { tactics := ... }`. (The list must be a `list` of `tactic string`, so that `tidy?` can report a usable tactic script.) Tactics can also be added to the list by tagging them (locally) with the `[tidy]` attribute. -/ meta def tidy (trace : parse $ optional (tk "?")) (cfg : tidy.cfg := {}) := tactic.tidy { trace_result := trace.is_some, ..cfg } end interactive add_tactic_doc { name := "tidy", category := doc_category.tactic, decl_names := [`tactic.interactive.tidy], tags := ["search", "Try this", "finishing"] } /-- Invoking the hole command `tidy` ("Use `tidy` to complete the goal") runs the tactic of the same name, replacing the hole with the tactic script `tidy` produces. -/ @[hole_command] meta def tidy_hole_cmd : hole_command := { name := "tidy", descr := "Use `tidy` to complete the goal.", action := λ _, do script ← tidy.core, return [("begin " ++ (", ".intercalate script) ++ " end", "by tidy")] } add_tactic_doc { name := "tidy", category := doc_category.hole_cmd, decl_names := [`tactic.tidy_hole_cmd], tags := ["search"] } end tactic
99f4a78a1da5e3dad489515ad822c40c4b7bd1e2
92e157ec9825b5e4597a6d715a8928703bc8e3b2
/src/mywork/lecture_13.lean
32bb9334e0fba6989a1da6475e01fd4abea52c0c
[]
no_license
exb3dg/cs2120f21
9e566bc508762573c023d3e70f83cb839c199ec8
319b8bf0d63bf96437bf17970ce0198d0b3525cd
refs/heads/main
1,692,970,909,568
1,634,584,540,000
1,634,584,540,000
399,947,025
0
0
null
null
null
null
UTF-8
Lean
false
false
5,268
lean
/- UPDATE: Test distributed after class on Monday. Monday will be a review day. The test is due back Wednesday before class. In class Wednesday we will have at least a short quiz to sanity check what you will have submitted for the test. We reserve the right to do follow-on in-person testing if the results indicate a possible problem. -/ /- REVIEW: Last time we focused on the question, how do we construct a proof of ∃ x, P x. To do so, you apply the introduction rule for exists. It's called exists.intro in Lean. You apply it to two arguments: a specific value, w, in place of x, and a proof that that particular w satisfies the predicate, P, i.e., that there is a proof of the proposition, P w. In other words, you can think of a proof of ∃ x, P x, as a pair, ⟨w, pf ⟩, where w is a witness and pf is a proof of P w. -/ /- Today we'll delve deeper into the mysteries of exists elimination, or how you can *use* a proof of ∃ x, P x. Here's the idea: If you have a proof, ex, of of ∃ (x : X), P x, you can apply exists.elim to ex, and (after a few more simple maneuvers) have yourself a specific value, (w : X), and a proof that w satisfies P, i.e., (pf : P w). The idea is that you can then uses the values in your subsequent proof steps. Why does this rule make sense? Consider a very simple example. If I tell you there exists some green ball, you can say, "well, call it b," and give that we know it's green, we also know that it satisfies the isGreen _ predicate, so we can also assume we have a proof of (isGreen b). In this example, b is a witness to the fact that some object satisfies the predicate. The proof then shows for sure that that is so. -/ example : ∃ (b : bool), b && tt = ff := begin apply exists.intro ff, exact rfl, end example : (exists (b : bool), b && tt = ff) → (∃ (b : bool), true) := -- if there exists a bool b and b and true equals false then bool is true begin assume h, cases h with w pf, apply exists.intro w, trivial, end /- Let's set up some assumptions so that we can explore their consequences when it comes to existentially quantified propositions. -/ /- Beachballs! What could be more fun -/ axioms (Ball : Type) -- There are balls (Green : Ball → Prop) -- a Ball can be Green (Red : Ball → Prop) -- a Ball can be Red (b1 b2 : Ball) -- b1 and b2 are balls (b1r : Red b1) -- b1 is red (b1g : Green b1) -- b1 is green (b2r : Red b2) -- b2 is red example : (∃ (b : Ball), Red b ∧ Green b) → (∃ (b : Ball), Red b) := begin assume h, cases h with ball RaG, apply exists.intro ball, exact RaG.left, end example : (∃ (b : Ball), Red b ∨ Green b) → (∃ (b : Ball), Green b ∨ Red b) := begin assume h, cases h with ball RoG, apply exists.intro ball, cases RoG with r g, _, end example : (∃ (b : Ball), Red b ∨ Green b) → (∃ (b : Ball), Red b) := begin assume h, cases h with w pf, cases pf, apply exists.intro w, assumption, --contradiction, end example : (∃ (b : Ball), Red b) → (∃ (b : Ball), Red b ∨ Green b) := begin end /- Social Networks -/ axioms (Person : Type) (Nice : Person → Prop) (Likes : Person → Person → Prop) /- a person is a type --> a person existd nice is a function, given a person it'll return that person is nice likes is a function that takes a person, it'll return that that person likes that other person -/ example : (∃ (p1 : Person), ∀ (p2 : Person), Likes p2 p1) → (∀ (e : Person), ∃ (s : Person), Likes e s) := /- there exists a person p1 such that forall person p2, all person p2 likes p1 forall person e, there exists a person s that they like -/ begin assume h, cases h with person1 pf, assume e, apply exists.intro person1, exact (pf e), end /- Write formal expressions for each of the following English language sentences. -/ -- Everyone likes him or herself -- (∀ p : people), Likes p p -- Someone doesn't like him or herself -- (∃ p : person), ¬(Likes p p) -- There is someone who likes someone else -- (∃ p1 p2: person), Likes p1 p2 -- No one likes anyone who dislikes them -- ¬(∃ p : person), ∀ (p2 : person), ¬ Nice p2 → LIkes p1 p2 -- Everyone likes anyone who is nice -- No one likes anyone who is not nice /- If everyone who's nice likes someone, then there is someone whom everyone who is nice likes. -/ -- ((∀ p1 : person), ∃ (p2 : person), Nice p1 → Likes p1 p2) → -- ((∃ p1 : person), (∀ p2 : person), Nice p2 → Likes p2 p1) example: ∃ n : ℕ, n = 1 := begin exact exists.intro 1 (eq.refl 1), end example: ¬ (∀ p : Person, Likes p p) ↔ ∃ p : Person, ¬ Likes p p := begin apply iff.intro _ _, assume h, have f := classical.em (∃ p : Person, ¬ Likes p p), cases f, -- case #1 assumption, -- case #2 -- propose new fact have contra : ∀ (p : Person), Likes p p := _, contradiction, assume p, have g := classical.em (Likes p p), cases g, assumption, have a : ∃ (p : Person), ¬Likes p p := exists.intro p g, contradiction, -- backward assume h, cases h with p t, end
c1a9fdf4c891d160c11891c94cd7b1c0358afb27
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Data/NameTrie.lean
113ee7e1363836e6882790a7fecf04dc5cb72fa6
[ "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
2,124
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Data.PrefixTree namespace Lean inductive NamePart | str (s : String) | num (n : Nat) instance : ToString NamePart where toString | NamePart.str s => s | NamePart.num n => toString n def NamePart.cmp : NamePart → NamePart → Ordering | NamePart.str a, NamePart.str b => compare a b | NamePart.num a, NamePart.num b => compare a b | NamePart.num _, NamePart.str _ => Ordering.lt | _, _ => Ordering.gt def NamePart.lt : NamePart → NamePart → Bool | NamePart.str a, NamePart.str b => a < b | NamePart.num a, NamePart.num b => a < b | NamePart.num _, NamePart.str _ => true | _, _ => false def NameTrie (β : Type u) := PrefixTree NamePart β NamePart.cmp private def toKey (n : Name) : List NamePart := loop n [] where loop | Name.str p s _, parts => loop p (NamePart.str s :: parts) | Name.num p n _, parts => loop p (NamePart.num n :: parts) | Name.anonymous, parts => parts def NameTrie.insert (t : NameTrie β) (n : Name) (b : β) : NameTrie β := PrefixTree.insert t (toKey n) b def NameTrie.empty : NameTrie β := PrefixTree.empty instance : Inhabited (NameTrie β) where default := NameTrie.empty instance : EmptyCollection (NameTrie β) where emptyCollection := NameTrie.empty def NameTrie.find? (t : NameTrie β) (k : Name) : Option β := PrefixTree.find? t (toKey k) @[inline] def NameTrie.foldMatchingM [Monad m] (t : NameTrie β) (k : Name) (init : σ) (f : β → σ → m σ) : m σ := PrefixTree.foldMatchingM t (toKey k) init f @[inline] def NameTrie.foldM [Monad m] (t : NameTrie β) (init : σ) (f : β → σ → m σ) : m σ := t.foldMatchingM Name.anonymous init f @[inline] def NameTrie.forMatchingM [Monad m] (t : NameTrie β) (k : Name) (f : β → m Unit) : m Unit := PrefixTree.forMatchingM t (toKey k) f @[inline] def NameTrie.forM [Monad m] (t : NameTrie β) (f : β → m Unit) : m Unit := t.forMatchingM Name.anonymous f end Lean
5e4919fe9227d00a440f2871bc4afca001f7fa44
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/category/monad/basic.lean
e9eda9155962c978715b16c0a6f595ed169f4f35
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
361
lean
import tactic.basic attribute [extensionality] reader_t.ext state_t.ext except_t.ext option_t.ext attribute [functor_norm] bind_assoc pure_bind bind_pure universes u v lemma map_eq_bind_pure_comp (m : Type u → Type v) [monad m] [is_lawful_monad m] {α β : Type u} (f : α → β) (x : m α) : f <$> x = x >>= pure ∘ f := by rw bind_pure_comp_eq_map
dfa0c875fefc99c3d135b58458fd76c384f84dd3
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/src/Lean/Meta/AppBuilder.lean
46157adecbcaa9a23ebd9b5e1a8476ad02350eba
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
20,822
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.Structure import Lean.Util.Recognizers import Lean.Meta.SynthInstance import Lean.Meta.Check namespace Lean.Meta /-- Return `id e` -/ def mkId (e : Expr) : MetaM Expr := do let type ← inferType e let u ← getLevel type return mkApp2 (mkConst ``id [u]) type e /-- Given `e` s.t. `inferType e` is definitionally equal to `expectedType`, return term `@id expectedType e`. -/ def mkExpectedTypeHint (e : Expr) (expectedType : Expr) : MetaM Expr := do let u ← getLevel expectedType return mkApp2 (mkConst ``id [u]) expectedType e def mkEq (a b : Expr) : MetaM Expr := do let aType ← inferType a let u ← getLevel aType return mkApp3 (mkConst ``Eq [u]) aType a b def mkHEq (a b : Expr) : MetaM Expr := do let aType ← inferType a let bType ← inferType b let u ← getLevel aType return mkApp4 (mkConst ``HEq [u]) aType a bType b def mkEqRefl (a : Expr) : MetaM Expr := do let aType ← inferType a let u ← getLevel aType return mkApp2 (mkConst ``Eq.refl [u]) aType a def mkHEqRefl (a : Expr) : MetaM Expr := do let aType ← inferType a let u ← getLevel aType return mkApp2 (mkConst ``HEq.refl [u]) aType a def mkAbsurd (e : Expr) (hp hnp : Expr) : MetaM Expr := do let p ← inferType hp let u ← getLevel e return mkApp4 (mkConst ``absurd [u]) p e hp hnp def mkFalseElim (e : Expr) (h : Expr) : MetaM Expr := do let u ← getLevel e return mkApp2 (mkConst ``False.elim [u]) e h private def infer (h : Expr) : MetaM Expr := do let hType ← inferType h whnfD hType private def hasTypeMsg (e type : Expr) : MessageData := m!"{indentExpr e}\nhas type{indentExpr type}" private def throwAppBuilderException {α} (op : Name) (msg : MessageData) : MetaM α := throwError "AppBuilder for '{op}', {msg}" def mkEqSymm (h : Expr) : MetaM Expr := do if h.isAppOf ``Eq.refl then return h else let hType ← infer h match hType.eq? with | some (α, a, b) => let u ← getLevel α return mkApp4 (mkConst ``Eq.symm [u]) α a b h | none => throwAppBuilderException ``Eq.symm ("equality proof expected" ++ hasTypeMsg h hType) def mkEqTrans (h₁ h₂ : Expr) : MetaM Expr := do if h₁.isAppOf ``Eq.refl then return h₂ else if h₂.isAppOf ``Eq.refl then return h₁ else let hType₁ ← infer h₁ let hType₂ ← infer h₂ match hType₁.eq?, hType₂.eq? with | some (α, a, b), some (_, _, c) => let u ← getLevel α return mkApp6 (mkConst ``Eq.trans [u]) α a b c h₁ h₂ | none, _ => throwAppBuilderException ``Eq.trans ("equality proof expected" ++ hasTypeMsg h₁ hType₁) | _, none => throwAppBuilderException ``Eq.trans ("equality proof expected" ++ hasTypeMsg h₂ hType₂) def mkHEqSymm (h : Expr) : MetaM Expr := do if h.isAppOf ``HEq.refl then return h else let hType ← infer h match hType.heq? with | some (α, a, β, b) => let u ← getLevel α return mkApp5 (mkConst ``HEq.symm [u]) α β a b h | none => throwAppBuilderException ``HEq.symm ("heterogeneous equality proof expected" ++ hasTypeMsg h hType) def mkHEqTrans (h₁ h₂ : Expr) : MetaM Expr := do if h₁.isAppOf ``HEq.refl then return h₂ else if h₂.isAppOf ``HEq.refl then return h₁ else let hType₁ ← infer h₁ let hType₂ ← infer h₂ match hType₁.heq?, hType₂.heq? with | some (α, a, β, b), some (_, _, γ, c) => let u ← getLevel α return mkApp8 (mkConst ``HEq.trans [u]) α β γ a b c h₁ h₂ | none, _ => throwAppBuilderException ``HEq.trans ("heterogeneous equality proof expected" ++ hasTypeMsg h₁ hType₁) | _, none => throwAppBuilderException ``HEq.trans ("heterogeneous equality proof expected" ++ hasTypeMsg h₂ hType₂) def mkEqOfHEq (h : Expr) : MetaM Expr := do let hType ← infer h match hType.heq? with | some (α, a, β, b) => unless (← isDefEq α β) do throwAppBuilderException ``eq_of_heq m!"heterogeneous equality types are not definitionally equal{indentExpr α}\nis not definitionally equal to{indentExpr β}" let u ← getLevel α return mkApp4 (mkConst ``eq_of_heq [u]) α a b h | _ => throwAppBuilderException ``HEq.trans m!"heterogeneous equality proof expected{indentExpr h}" def mkCongrArg (f h : Expr) : MetaM Expr := do if h.isAppOf ``Eq.refl then mkEqRefl (mkApp f h.appArg!) else let hType ← infer h let fType ← infer f match fType.arrow?, hType.eq? with | some (α, β), some (_, a, b) => let u ← getLevel α let v ← getLevel β return mkApp6 (mkConst ``congrArg [u, v]) α β a b f h | none, _ => throwAppBuilderException ``congrArg ("non-dependent function expected" ++ hasTypeMsg f fType) | _, none => throwAppBuilderException ``congrArg ("equality proof expected" ++ hasTypeMsg h hType) def mkCongrFun (h a : Expr) : MetaM Expr := do if h.isAppOf ``Eq.refl then mkEqRefl (mkApp h.appArg! a) else let hType ← infer h match hType.eq? with | some (ρ, f, g) => do let ρ ← whnfD ρ match ρ with | Expr.forallE n α β _ => let β' := Lean.mkLambda n BinderInfo.default α β let u ← getLevel α let v ← getLevel (mkApp β' a) return mkApp6 (mkConst ``congrFun [u, v]) α β' f g h a | _ => throwAppBuilderException ``congrFun ("equality proof between functions expected" ++ hasTypeMsg h hType) | _ => throwAppBuilderException ``congrFun ("equality proof expected" ++ hasTypeMsg h hType) def mkCongr (h₁ h₂ : Expr) : MetaM Expr := do if h₁.isAppOf ``Eq.refl then mkCongrArg h₁.appArg! h₂ else if h₂.isAppOf ``Eq.refl then mkCongrFun h₁ h₂.appArg! else let hType₁ ← infer h₁ let hType₂ ← infer h₂ match hType₁.eq?, hType₂.eq? with | some (ρ, f, g), some (α, a, b) => let ρ ← whnfD ρ match ρ.arrow? with | some (_, β) => do let u ← getLevel α let v ← getLevel β return mkApp8 (mkConst ``congr [u, v]) α β f g a b h₁ h₂ | _ => throwAppBuilderException ``congr ("non-dependent function expected" ++ hasTypeMsg h₁ hType₁) | none, _ => throwAppBuilderException ``congr ("equality proof expected" ++ hasTypeMsg h₁ hType₁) | _, none => throwAppBuilderException ``congr ("equality proof expected" ++ hasTypeMsg h₂ hType₂) private def mkAppMFinal (methodName : Name) (f : Expr) (args : Array Expr) (instMVars : Array MVarId) : MetaM Expr := do instMVars.forM fun mvarId => do let mvarDecl ← getMVarDecl mvarId let mvarVal ← synthInstance mvarDecl.type assignExprMVar mvarId mvarVal let result ← instantiateMVars (mkAppN f args) if (← hasAssignableMVar result) then throwAppBuilderException methodName ("result contains metavariables" ++ indentExpr result) return result private partial def mkAppMArgs (f : Expr) (fType : Expr) (xs : Array Expr) : MetaM Expr := let rec loop (type : Expr) (i : Nat) (j : Nat) (args : Array Expr) (instMVars : Array MVarId) : MetaM Expr := do if i >= xs.size then mkAppMFinal `mkAppM f args instMVars else match type with | Expr.forallE n d b c => let d := d.instantiateRevRange j args.size args match c.binderInfo with | BinderInfo.implicit => let mvar ← mkFreshExprMVar d MetavarKind.natural n loop b i j (args.push mvar) instMVars | BinderInfo.strictImplicit => let mvar ← mkFreshExprMVar d MetavarKind.natural n loop b i j (args.push mvar) instMVars | BinderInfo.instImplicit => let mvar ← mkFreshExprMVar d MetavarKind.synthetic n loop b i j (args.push mvar) (instMVars.push mvar.mvarId!) | _ => let x := xs[i] let xType ← inferType x if (← isDefEq d xType) then loop b (i+1) j (args.push x) instMVars else throwAppTypeMismatch (mkAppN f args) x | type => let type := type.instantiateRevRange j args.size args let type ← whnfD type if type.isForall then loop type i args.size args instMVars else throwAppBuilderException `mkAppM m!"too many explicit arguments provided to{indentExpr f}\narguments{indentD xs}" loop fType 0 0 #[] #[] private def mkFun (constName : Name) : MetaM (Expr × Expr) := do let cinfo ← getConstInfo constName let us ← cinfo.levelParams.mapM fun _ => mkFreshLevelMVar let f := mkConst constName us let fType := cinfo.instantiateTypeLevelParams us return (f, fType) /-- Return the application `constName xs`. It tries to fill the implicit arguments before the last element in `xs`. Remark: ``mkAppM `arbitrary #[α]`` returns `@arbitrary.{u} α` without synthesizing the implicit argument occurring after `α`. Given a `x : (([Decidable p] → Bool) × Nat`, ``mkAppM `Prod.fst #[x]`` returns `@Prod.fst ([Decidable p] → Bool) Nat x` -/ def mkAppM (constName : Name) (xs : Array Expr) : MetaM Expr := do traceCtx `Meta.appBuilder <| withNewMCtxDepth do let (f, fType) ← mkFun constName let r ← mkAppMArgs f fType xs trace[Meta.appBuilder] "constName: {constName}, xs: {xs}, result: {r}" return r /-- Similar to `mkAppM`, but takes an `Expr` instead of a constant name. -/ def mkAppM' (f : Expr) (xs : Array Expr) : MetaM Expr := do let fType ← inferType f traceCtx `Meta.appBuilder <| withNewMCtxDepth do let r ← mkAppMArgs f fType xs trace[Meta.appBuilder] "f: {f}, xs: {xs}, result: {r}" return r private partial def mkAppOptMAux (f : Expr) (xs : Array (Option 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 if h : i < xs.size then match xs.get ⟨i, h⟩ with | none => match c.binderInfo with | BinderInfo.instImplicit => do let mvar ← mkFreshExprMVar d MetavarKind.synthetic n mkAppOptMAux f xs (i+1) (args.push mvar) j (instMVars.push mvar.mvarId!) b | _ => do let mvar ← mkFreshExprMVar d MetavarKind.natural n mkAppOptMAux f xs (i+1) (args.push mvar) j instMVars b | some x => let xType ← inferType x if (← isDefEq d xType) then mkAppOptMAux f xs (i+1) (args.push x) j instMVars b else throwAppTypeMismatch (mkAppN f args) x else mkAppMFinal `mkAppOptM f args instMVars | i, args, j, instMVars, type => do let type := type.instantiateRevRange j args.size args let type ← whnfD type if type.isForall then mkAppOptMAux f xs i args args.size instMVars type else if i == xs.size then mkAppMFinal `mkAppOptM f args instMVars else do let xs : Array Expr := xs.foldl (fun r x? => match x? with | none => r | some x => r.push x) #[] throwAppBuilderException `mkAppOptM ("too many arguments provided to" ++ indentExpr f ++ Format.line ++ "arguments" ++ xs) /-- Similar to `mkAppM`, but it allows us to specify which arguments are provided explicitly using `Option` type. Example: Given `Pure.pure {m : Type u → Type v} [Pure m] {α : Type u} (a : α) : m α`, ``` mkAppOptM `Pure.pure #[m, none, none, a] ``` returns a `Pure.pure` application if the instance `Pure m` can be synthesized, and the universe match. Note that, ``` mkAppM `Pure.pure #[a] ``` fails because the only explicit argument `(a : α)` is not sufficient for inferring the remaining arguments, we would need the expected type. -/ def mkAppOptM (constName : Name) (xs : Array (Option Expr)) : MetaM Expr := do traceCtx `Meta.appBuilder <| withNewMCtxDepth do let (f, fType) ← mkFun constName mkAppOptMAux f xs 0 #[] 0 #[] fType /-- Similar to `mkAppOptM`, but takes an `Expr` instead of a constant name -/ def mkAppOptM' (f : Expr) (xs : Array (Option Expr)) : MetaM Expr := do let fType ← inferType f traceCtx `Meta.appBuilder <| withNewMCtxDepth do mkAppOptMAux f xs 0 #[] 0 #[] fType def mkEqNDRec (motive h1 h2 : Expr) : MetaM Expr := do if h2.isAppOf ``Eq.refl then return h1 else let h2Type ← infer h2 match h2Type.eq? with | none => throwAppBuilderException ``Eq.ndrec ("equality proof expected" ++ hasTypeMsg h2 h2Type) | some (α, a, b) => let u2 ← getLevel α let motiveType ← infer motive match motiveType with | Expr.forallE _ _ (Expr.sort u1 _) _ => return mkAppN (mkConst ``Eq.ndrec [u1, u2]) #[α, a, motive, h1, b, h2] | _ => throwAppBuilderException ``Eq.ndrec ("invalid motive" ++ indentExpr motive) def mkEqRec (motive h1 h2 : Expr) : MetaM Expr := do if h2.isAppOf ``Eq.refl then return h1 else let h2Type ← infer h2 match h2Type.eq? with | none => throwAppBuilderException ``Eq.rec ("equality proof expected" ++ indentExpr h2) | some (α, a, b) => let u2 ← getLevel α let motiveType ← infer motive match motiveType with | Expr.forallE _ _ (Expr.forallE _ _ (Expr.sort u1 _) _) _ => return mkAppN (mkConst ``Eq.rec [u1, u2]) #[α, a, motive, h1, b, h2] | _ => throwAppBuilderException ``Eq.rec ("invalid motive" ++ indentExpr motive) def mkEqMP (eqProof pr : Expr) : MetaM Expr := mkAppM ``Eq.mp #[eqProof, pr] def mkEqMPR (eqProof pr : Expr) : MetaM Expr := mkAppM ``Eq.mpr #[eqProof, pr] def mkNoConfusion (target : Expr) (h : Expr) : MetaM Expr := do let type ← inferType h let type ← whnf type match type.eq? with | none => throwAppBuilderException `noConfusion ("equality expected" ++ hasTypeMsg h type) | some (α, a, b) => let α ← whnf α matchConstInduct α.getAppFn (fun _ => throwAppBuilderException `noConfusion ("inductive type expected" ++ indentExpr α)) fun v us => do let u ← getLevel target return mkAppN (mkConst (Name.mkStr v.name "noConfusion") (u :: us)) (α.getAppArgs ++ #[target, a, b, h]) def mkPure (monad : Expr) (e : Expr) : MetaM Expr := mkAppOptM ``Pure.pure #[monad, none, none, e] /-- `mkProjection s fieldName` return an expression for accessing field `fieldName` of the structure `s`. Remark: `fieldName` may be a subfield of `s`. -/ partial def mkProjection : Expr → Name → MetaM Expr | s, fieldName => do let type ← inferType s let type ← whnf type match type.getAppFn with | Expr.const structName us _ => let env ← getEnv unless isStructure env structName do throwAppBuilderException `mkProjection ("structure expected" ++ hasTypeMsg s type) match getProjFnForField? env structName fieldName with | some projFn => let params := type.getAppArgs return mkApp (mkAppN (mkConst projFn us) params) s | none => let fields := getStructureFields env structName let r? ← fields.findSomeM? fun fieldName' => do match isSubobjectField? env structName fieldName' with | none => pure none | some _ => let parent ← mkProjection s fieldName' (do let r ← mkProjection parent fieldName; return some r) <|> pure none match r? with | some r => pure r | none => throwAppBuilderException `mkProjectionn ("invalid field name '" ++ toString fieldName ++ "' for" ++ hasTypeMsg s type) | _ => throwAppBuilderException `mkProjectionn ("structure expected" ++ hasTypeMsg s type) private def mkListLitAux (nil : Expr) (cons : Expr) : List Expr → Expr | [] => nil | x::xs => mkApp (mkApp cons x) (mkListLitAux nil cons xs) def mkListLit (type : Expr) (xs : List Expr) : MetaM Expr := do let u ← getDecLevel type let nil := mkApp (mkConst ``List.nil [u]) type match xs with | [] => return nil | _ => let cons := mkApp (mkConst ``List.cons [u]) type return mkListLitAux nil cons xs def mkArrayLit (type : Expr) (xs : List Expr) : MetaM Expr := do let u ← getDecLevel type let listLit ← mkListLit type xs return mkApp (mkApp (mkConst ``List.toArray [u]) type) listLit def mkSorry (type : Expr) (synthetic : Bool) : MetaM Expr := do let u ← getLevel type return mkApp2 (mkConst ``sorryAx [u]) type (toExpr synthetic) /-- Return `Decidable.decide p` -/ def mkDecide (p : Expr) : MetaM Expr := mkAppOptM ``Decidable.decide #[p, none] /-- Return a proof for `p : Prop` using `decide p` -/ def mkDecideProof (p : Expr) : MetaM Expr := do let decP ← mkDecide p let decEqTrue ← mkEq decP (mkConst ``Bool.true) let h ← mkEqRefl (mkConst ``Bool.true) let h ← mkExpectedTypeHint h decEqTrue mkAppM ``of_decide_eq_true #[h] /-- Return `a < b` -/ def mkLt (a b : Expr) : MetaM Expr := mkAppM ``LT.lt #[a, b] /-- Return `a <= b` -/ def mkLe (a b : Expr) : MetaM Expr := mkAppM ``LE.le #[a, b] /-- Return `arbitrary α` -/ def mkArbitrary (α : Expr) : MetaM Expr := mkAppOptM ``arbitrary #[α, none] /-- Return `sorryAx type` -/ def mkSyntheticSorry (type : Expr) : MetaM Expr := return mkApp2 (mkConst ``sorryAx [← getLevel type]) type (mkConst ``Bool.true) /-- Return `funext h` -/ def mkFunExt (h : Expr) : MetaM Expr := mkAppM ``funext #[h] /-- Return `propext h` -/ def mkPropExt (h : Expr) : MetaM Expr := mkAppM ``propext #[h] /-- Return `let_congr h₁ h₂` -/ def mkLetCongr (h₁ h₂ : Expr) : MetaM Expr := mkAppM ``let_congr #[h₁, h₂] /-- Return `let_val_congr b h` -/ def mkLetValCongr (b h : Expr) : MetaM Expr := mkAppM ``let_val_congr #[b, h] /-- Return `let_body_congr a h` -/ def mkLetBodyCongr (a h : Expr) : MetaM Expr := mkAppM ``let_body_congr #[a, h] /-- Return `of_eq_true h` -/ def mkOfEqTrue (h : Expr) : MetaM Expr := mkAppM ``of_eq_true #[h] /-- Return `eq_true h` -/ def mkEqTrue (h : Expr) : MetaM Expr := mkAppM ``eq_true #[h] /-- Return `eq_false h` `h` must have type definitionally equal to `¬ p` in the current reducibility setting. -/ def mkEqFalse (h : Expr) : MetaM Expr := mkAppM ``eq_false #[h] /-- Return `eq_false' h` `h` must have type definitionally equal to `p → False` in the current reducibility setting. -/ def mkEqFalse' (h : Expr) : MetaM Expr := mkAppM ``eq_false' #[h] def mkImpCongr (h₁ h₂ : Expr) : MetaM Expr := mkAppM ``implies_congr #[h₁, h₂] def mkImpCongrCtx (h₁ h₂ : Expr) : MetaM Expr := mkAppM ``implies_congr_ctx #[h₁, h₂] def mkForallCongr (h : Expr) : MetaM Expr := mkAppM ``forall_congr #[h] /-- Return instance for `[Monad m]` if there is one -/ def isMonad? (m : Expr) : MetaM (Option Expr) := try let monadType ← mkAppM `Monad #[m] let result ← trySynthInstance monadType match result with | LOption.some inst => pure inst | _ => pure none catch _ => pure none /-- Return `(n : type)`, a numeric literal of type `type`. The method fails if we don't have an instance `OfNat type n` -/ def mkNumeral (type : Expr) (n : Nat) : MetaM Expr := do let u ← getDecLevel type let inst ← synthInstance (mkApp2 (mkConst ``OfNat [u]) type (mkRawNatLit n)) return mkApp3 (mkConst ``OfNat.ofNat [u]) type (mkRawNatLit n) inst /-- Return `a op b`, where `op` has name `opName` and is implemented using the typeclass `className`. This method assumes `a` and `b` have the same type, and typeclass `className` is heterogeneous. Examples of supported clases: `HAdd`, `HSub`, `HMul`. We use heterogeneous operators to ensure we have a uniform representation. -/ private def mkBinaryOp (className : Name) (opName : Name) (a b : Expr) : MetaM Expr := do let aType ← inferType a let u ← getDecLevel aType let inst ← synthInstance (mkApp3 (mkConst className [u, u, u]) aType aType aType) return mkApp6 (mkConst opName [u, u, u]) aType aType aType inst a b /-- Return `a + b` using a heterogeneous `+`. This method assumes `a` and `b` have the same type. -/ def mkAdd (a b : Expr) : MetaM Expr := mkBinaryOp ``HAdd ``HAdd.hAdd a b /-- Return `a - b` using a heterogeneous `-`. This method assumes `a` and `b` have the same type. -/ def mkSub (a b : Expr) : MetaM Expr := mkBinaryOp ``HSub ``HSub.hSub a b /-- Return `a * b` using a heterogeneous `*`. This method assumes `a` and `b` have the same type. -/ def mkMul (a b : Expr) : MetaM Expr := mkBinaryOp ``HMul ``HMul.hMul a b builtin_initialize registerTraceClass `Meta.appBuilder end Lean.Meta
f256a20dcc4635b41eaea90909831180098d69b6
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Widget.lean
4345d487dd7a4cb784a7ad3cb091215a6efe952d
[ "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
306
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki -/ import Lean.Widget.InteractiveCode import Lean.Widget.InteractiveDiagnostic import Lean.Widget.InteractiveGoal import Lean.Widget.TaggedText
01247b3005a5064d8d2e8621754b964c875b918b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/lint/simp_auto.lean
8a763197b21614326bb9f95844e37e4abb6e0a37
[]
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,825
lean
/- Copyright (c) 2020 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.lint.basic import Mathlib.PostPort namespace Mathlib /-! # Linter for simplification lemmas This files defines several linters that prevent common mistakes when declaring simp lemmas: * `simp_nf` checks that the left-hand side of a simp lemma is not simplified by a different lemma. * `simp_var_head` checks that the head symbol of the left-hand side is not a variable. * `simp_comm` checks that commutativity lemmas are not marked as simplification lemmas. -/ /-- `simp_lhs_rhs ty` returns the left-hand and right-hand side of a simp lemma with type `ty`. -/ -- We only detect a fixed set of simp relations here. -- This is somewhat justified since for a custom simp relation R, -- the simp lemma `R a b` is implicitly converted to `R a b ↔ true` as well. /-- `simp_lhs ty` returns the left-hand side of a simp lemma with type `ty`. -/ /-- `simp_is_conditional_core ty` returns `none` if `ty` is a conditional simp lemma, and `some lhs` otherwise. -/ /-- `simp_is_conditional ty` returns true iff the simp lemma with type `ty` is conditional. -/ /-- Checks whether two expressions are equal for the simplifier. That is, they are reducibly-definitional equal, and they have the same head symbol. -/ /-- Reports declarations that are simp lemmas whose left-hand side is not in simp-normal form. -/ -- Sometimes, a definition is tagged @[simp] to add the equational lemmas to the simp set. -- In this case, ignore the declaration if it is not a valid simp lemma by itself. /-- This note gives you some tips to debug any errors that the simp-normal form linter raises end Mathlib
5e2c4a0f223efe16693c109deb7da1d3f8c6b957
c31182a012eec69da0a1f6c05f42b0f0717d212d
/src/polyhedral_lattice/finsupp.lean
7459395e3ee2c33a316b1fbc6dc4e8cf5bab7ea6
[]
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
3,500
lean
import linear_algebra.finsupp_vector_space import for_mathlib.finite_free import polyhedral_lattice.basic /-! # Hom(ι, Λ) for Λ a polyhedral lattice If Λ is a polyhedral lattice and ι is a finite type, then ι → Λ is a polyhedral lattice. ## Implementation issue We use `ι →₀ Λ` rather than `ι → Λ` to make life easier with sums. -/ noncomputable theory open_locale big_operators classical variables (ι Λ : Type*) [fintype ι] namespace finsupp section normed_group variables [normed_group Λ] instance : has_norm (ι →₀ Λ) := ⟨λ x, x.sum $ λ _, norm⟩ variables {ι Λ} lemma norm_def (x : ι →₀ Λ) : ∥x∥ = x.sum (λ _, norm) := rfl @[simp] lemma norm_single (i : ι) (l : Λ) : ∥single i l∥ = ∥l∥ := by simp only [norm_def, sum_single_index, norm_zero] variables (ι Λ) instance : normed_group (ι →₀ Λ) := normed_group.of_core _ $ { norm_eq_zero_iff := λ x, begin simp only [norm_def, sum, ← coe_nnnorm, ← nnreal.coe_sum, nnreal.coe_eq_zero, coe_zero, finset.sum_eq_zero_iff, nnnorm_eq_zero, mem_support_iff, ext_iff, pi.zero_apply, not_imp_self] end, triangle := begin intros x y, have aux := λ z : ι →₀ Λ, @sum_fintype ι Λ _ _ _ _ z (λ i, norm) (λ i, norm_zero), simp only [norm_def, aux, ← finset.sum_add_distrib, add_apply], apply finset.sum_le_sum, rintro i -, apply norm_add_le, end, norm_neg := λ x, begin have aux := λ z : ι →₀ Λ, @sum_fintype ι Λ _ _ _ _ z (λ i, norm) (λ i, norm_zero), simp only [norm_def, aux, norm_neg, neg_apply] end } variables {ι Λ} lemma nnnorm_def (x : ι →₀ Λ) : ∥x∥₊ = x.sum (λ _, nnnorm) := begin ext, simpa only [coe_nnnorm, finsupp.sum, nnreal.coe_sum] using norm_def x, end end normed_group end finsupp namespace generates_norm open finsupp variables [polyhedral_lattice Λ] variables {J : Type*} [fintype J] (x : J → Λ) (hx : generates_norm x) def finsupp_generators : ι × J → (ι →₀ Λ) := λ j, single j.1 (x j.2) variables {Λ x} include hx lemma finsupp : generates_norm (finsupp_generators ι Λ x) := begin dsimp only [finsupp_generators], intro l, have := λ i, hx (l i), choose c H1 H2 using this, have hl : l = ∑ i, single i (l i), { conv_lhs { rw [← sum_single l, finsupp.sum] }, apply finset.sum_subset (finset.subset_univ _), rintro i - hi, rw not_mem_support_iff at hi, rw [hi, single_zero] }, simp only [← single_add_hom_apply, ← add_monoid_hom.map_nsmul] at hl ⊢, refine ⟨λ j, c j.1 j.2, _, _⟩, { simp only [H1, add_monoid_hom.map_sum] at hl, rw [hl, ← finset.univ_product_univ, finset.sum_product] }, { have aux := λ z : ι →₀ Λ, @sum_fintype ι Λ _ _ _ _ z (λ i, norm) (λ i, norm_zero), simp only [norm_def, aux, ← finset.univ_product_univ, finset.sum_product, H2, single_add_hom_apply, norm_single], } end end generates_norm namespace finsupp variables [polyhedral_lattice Λ] instance {ι : Type} [fintype ι] : polyhedral_lattice (ι →₀ Λ) := { finite := module.finite.of_basis ℤ _ (finsupp.basis (λ i, module.free.choose_basis ℤ Λ)), free := module.free.of_basis (finsupp.basis (λ i, module.free.choose_basis ℤ Λ)), polyhedral' := begin obtain ⟨J, _instJ, x, hx⟩ := polyhedral_lattice.polyhedral' Λ, resetI, refine ⟨ι × J, infer_instance, λ j, single j.1 (x j.2), _⟩, exact hx.finsupp _ end } end finsupp
0ec17f833f51d8868823ad1c784bb4018d7d83ce
6de8ea38e7f58ace8fbf74ba3ad0bf3b3d1d7ab5
/lab6/code/lambda.lean
327973c57364370a516140da34d2b8dbbfd80eca
[]
no_license
KinanBab/CS591K1-Labs
72f4e2c7d230d4e4f548a343a47bf815272b1f58
d4569bf99d20c22cd56721024688cda247d1447f
refs/heads/master
1,587,016,758,873
1,558,148,366,000
1,558,148,366,000
165,329,114
5
2
null
1,550,689,848,000
1,547,252,664,000
TeX
UTF-8
Lean
false
false
2,214
lean
-- Syntax inductive term | var : string → term | abs : string → term → term | app : term → term → term -- Custom notation instance : has_coe string term := ⟨term.var⟩ notation `λ:`x `.` t := term.abs x t notation `[`t1`](`t2`)` := term.app t1 t2 -- Substitution: the core of beta reduction @[simp] def substitute : term -> string -> term -> term | t v (term.var x) := if to_bool (x = v) then t else (term.var x) | t v (term.abs x t') := if to_bool (x = v) then (term.abs x t') else (term.abs x (substitute t v t')) | t v (term.app t1 t2) := term.app (substitute t v t1) (substitute t v t2) -- reflexive transative closure inductive rStar {T} (rel: T -> T -> Prop) : T -> T -> Prop | base : ∀ {t}, rStar t t | trans {t1 t2 t3} : rel t1 t2 -> rStar t2 t3 -> rStar t1 t3 -- beta reduction inductive beta : term -> term -> Prop | appl: ∀{t1 t1' t2: term}, beta t1 t1' -> beta (term.app t1 t2) (term.app t1' t2) | appr: ∀{t1 t2 t2': term}, beta t2 t2' -> beta (term.app t1 t2) (term.app t1 t2') | app :∀{x: string}, ∀{t1 t2: term}, beta (term.app (term.abs x t1) t2) (substitute t2 x t1) notation t `→β`:45 t' := beta t t' notation t `↠β`:45 t' := (rStar beta) t t' -- What is a value? inductive value : term -> Prop | var: ∀{x}, value (term.var x) | abs: ∀{x t}, value (term.abs x t) -- Our simple types inductive type | dot -- dot or constant type | func: type -> type -> type -- function type: has domain and range types -- Our typing rules def env := list (string × type) instance : has_append env := ⟨list.append⟩ @[simp] def type_from_env : env -> string -> type -> type | list.nil v default := default | (list.cons (x, t) env') v default := if to_bool (v = x) then t else type_from_env env' v default inductive typing : env -> term -> type -> Prop | var: ∀{E x t}, (type_from_env E x type.dot) = t -> typing E (term.var x) t | abs: ∀{E x t1 b t2}, typing (list.cons (x, t1) E) b t2 -> typing E (term.abs x b) (type.func t1 t2) | app: ∀{E term1 t1 term2 t2}, typing E term1 (type.func t1 t2) -> typing E term2 t1 -> typing E (term.app term1 term2) t2
5e4a14734d2b7e7a2eec92e25735e9601537f69b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebraic_geometry/sheafed_space.lean
fe2af59504d702b80e28da27a6db50c5044f445e
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
8,986
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebraic_geometry.presheafed_space import Mathlib.topology.sheaves.sheaf import Mathlib.PostPort universes v u l u_1 namespace Mathlib /-! # Sheafed spaces Introduces the category of topological spaces equipped with a sheaf (taking values in an arbitrary target category `C`.) We further describe how to apply functors and natural transformations to the values of the presheaves. -/ namespace algebraic_geometry /-- A `SheafedSpace C` is a topological space equipped with a sheaf of `C`s. -/ structure SheafedSpace (C : Type u) [category_theory.category C] [category_theory.limits.has_products C] extends PresheafedSpace C where sheaf_condition : Top.presheaf.sheaf_condition (PresheafedSpace.presheaf _to_PresheafedSpace) namespace SheafedSpace protected instance coe_carrier {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] : has_coe (SheafedSpace C) Top := has_coe.mk fun (X : SheafedSpace C) => PresheafedSpace.carrier (to_PresheafedSpace X) /-- Extract the `sheaf C (X : Top)` from a `SheafedSpace C`. -/ def sheaf {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] (X : SheafedSpace C) : Top.sheaf C ↑X := Top.sheaf.mk (PresheafedSpace.presheaf (to_PresheafedSpace X)) (sheaf_condition X) @[simp] theorem as_coe {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] (X : SheafedSpace C) : PresheafedSpace.carrier (to_PresheafedSpace X) = ↑X := rfl @[simp] theorem mk_coe {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] (carrier : Top) (presheaf : Top.presheaf C carrier) (h : Top.presheaf.sheaf_condition (PresheafedSpace.presheaf (PresheafedSpace.mk carrier presheaf))) : ↑(mk (PresheafedSpace.mk carrier presheaf) h) = carrier := rfl protected instance topological_space {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] (X : SheafedSpace C) : topological_space ↥X := category_theory.bundled.str (PresheafedSpace.carrier (to_PresheafedSpace X)) /-- The trivial `punit` valued sheaf on any topological space. -/ def punit (X : Top) : SheafedSpace (category_theory.discrete PUnit) := mk (PresheafedSpace.mk (PresheafedSpace.carrier (PresheafedSpace.const X PUnit.unit)) (PresheafedSpace.presheaf (PresheafedSpace.const X PUnit.unit))) (Top.presheaf.sheaf_condition_punit (PresheafedSpace.presheaf (PresheafedSpace.mk (PresheafedSpace.carrier (PresheafedSpace.const X PUnit.unit)) (PresheafedSpace.presheaf (PresheafedSpace.const X PUnit.unit))))) protected instance inhabited : Inhabited (SheafedSpace (category_theory.discrete PUnit)) := { default := punit (Top.of pempty) } protected instance category_theory.category {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] : category_theory.category (SheafedSpace C) := (fun (this : category_theory.category (category_theory.induced_category (PresheafedSpace C) to_PresheafedSpace)) => this) (category_theory.induced_category.category to_PresheafedSpace) /-- Forgetting the sheaf condition is a functor from `SheafedSpace C` to `PresheafedSpace C`. -/ def forget_to_PresheafedSpace {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] : SheafedSpace C ⥤ PresheafedSpace C := category_theory.induced_functor to_PresheafedSpace @[simp] theorem id_base {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] (X : SheafedSpace C) : PresheafedSpace.hom.base 𝟙 = 𝟙 := rfl theorem id_c {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] (X : SheafedSpace C) : PresheafedSpace.hom.c 𝟙 = category_theory.iso.inv (category_theory.functor.left_unitor (PresheafedSpace.presheaf (to_PresheafedSpace X))) ≫ category_theory.whisker_right (category_theory.nat_trans.op (category_theory.iso.hom (topological_space.opens.map_id (PresheafedSpace.carrier (to_PresheafedSpace X))))) (PresheafedSpace.presheaf (to_PresheafedSpace X)) := rfl @[simp] theorem id_c_app {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] (X : SheafedSpace C) (U : topological_space.opens ↥(PresheafedSpace.carrier (to_PresheafedSpace X))ᵒᵖ) : category_theory.nat_trans.app (PresheafedSpace.hom.c 𝟙) U = category_theory.eq_to_hom (opposite.op_induction (fun (U : topological_space.opens ↥(PresheafedSpace.carrier (to_PresheafedSpace X))) => subtype.cases_on U fun (U_val : set ↥(PresheafedSpace.carrier (to_PresheafedSpace X))) (U_property : is_open U_val) => Eq.refl (category_theory.functor.obj (PresheafedSpace.presheaf (to_PresheafedSpace X)) (opposite.op { val := U_val, property := U_property }))) U) := sorry @[simp] theorem comp_base {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : SheafedSpace C} {Y : SheafedSpace C} {Z : SheafedSpace C} (f : X ⟶ Y) (g : Y ⟶ Z) : PresheafedSpace.hom.base (f ≫ g) = PresheafedSpace.hom.base f ≫ PresheafedSpace.hom.base g := rfl @[simp] theorem comp_c_app {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : SheafedSpace C} {Y : SheafedSpace C} {Z : SheafedSpace C} (α : X ⟶ Y) (β : Y ⟶ Z) (U : topological_space.opens ↥(PresheafedSpace.carrier (to_PresheafedSpace Z))ᵒᵖ) : category_theory.nat_trans.app (PresheafedSpace.hom.c (α ≫ β)) U = category_theory.nat_trans.app (PresheafedSpace.hom.c β) U ≫ category_theory.nat_trans.app (PresheafedSpace.hom.c α) (opposite.op (category_theory.functor.obj (topological_space.opens.map (PresheafedSpace.hom.base β)) (opposite.unop U))) ≫ category_theory.nat_trans.app (category_theory.iso.inv (Top.presheaf.pushforward.comp (PresheafedSpace.presheaf (to_PresheafedSpace X)) (PresheafedSpace.hom.base α) (PresheafedSpace.hom.base β))) U := rfl /-- The forgetful functor from `SheafedSpace` to `Top`. -/ def forget (C : Type u) [category_theory.category C] [category_theory.limits.has_products C] : SheafedSpace C ⥤ Top := category_theory.functor.mk (fun (X : SheafedSpace C) => ↑X) fun (X Y : SheafedSpace C) (f : X ⟶ Y) => PresheafedSpace.hom.base f /-- The restriction of a sheafed space along an open embedding into the space. -/ def restrict {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {U : Top} (X : SheafedSpace C) (f : U ⟶ ↑X) (h : open_embedding ⇑f) : SheafedSpace C := sorry /-- The global sections, notated Gamma. -/ def Γ {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] : SheafedSpace Cᵒᵖ ⥤ C := category_theory.functor.op forget_to_PresheafedSpace ⋙ PresheafedSpace.Γ theorem Γ_def {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] : Γ = category_theory.functor.op forget_to_PresheafedSpace ⋙ PresheafedSpace.Γ := rfl @[simp] theorem Γ_obj {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] (X : SheafedSpace Cᵒᵖ) : category_theory.functor.obj Γ X = category_theory.functor.obj (PresheafedSpace.presheaf (to_PresheafedSpace (opposite.unop X))) (opposite.op ⊤) := rfl theorem Γ_obj_op {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] (X : SheafedSpace C) : category_theory.functor.obj Γ (opposite.op X) = category_theory.functor.obj (PresheafedSpace.presheaf (to_PresheafedSpace X)) (opposite.op ⊤) := rfl @[simp] theorem Γ_map {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : SheafedSpace Cᵒᵖ} {Y : SheafedSpace Cᵒᵖ} (f : X ⟶ Y) : category_theory.functor.map Γ f = category_theory.nat_trans.app (PresheafedSpace.hom.c (category_theory.has_hom.hom.unop f)) (opposite.op ⊤) ≫ category_theory.functor.map (PresheafedSpace.presheaf (to_PresheafedSpace (opposite.unop Y))) (category_theory.has_hom.hom.op (topological_space.opens.le_map_top (PresheafedSpace.hom.base (category_theory.has_hom.hom.unop f)) ⊤)) := rfl theorem Γ_map_op {C : Type u} [category_theory.category C] [category_theory.limits.has_products C] {X : SheafedSpace C} {Y : SheafedSpace C} (f : X ⟶ Y) : category_theory.functor.map Γ (category_theory.has_hom.hom.op f) = category_theory.nat_trans.app (PresheafedSpace.hom.c f) (opposite.op ⊤) ≫ category_theory.functor.map (PresheafedSpace.presheaf (to_PresheafedSpace X)) (category_theory.has_hom.hom.op (topological_space.opens.le_map_top (PresheafedSpace.hom.base f) ⊤)) := rfl
4010b3af4c510b2ebb6ebc1f15f85bcdb06b8547
7cef822f3b952965621309e88eadf618da0c8ae9
/src/tactic/norm_num.lean
9226c12e3d5051d9b6538e74edcbbe2bcd8ceb81
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
18,235
lean
/- Copyright (c) 2017 Simon Hudon All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Mario Carneiro Evaluating arithmetic expressions including *, +, -, ^, ≤ -/ import algebra.group_power data.rat.order data.rat.cast data.rat.meta_defs data.nat.prime import tactic.interactive tactic.converter.interactive universes u v w namespace tactic meta def refl_conv (e : expr) : tactic (expr × expr) := do p ← mk_eq_refl e, return (e, p) meta def trans_conv (t₁ t₂ : expr → tactic (expr × expr)) (e : expr) : tactic (expr × expr) := (do (e₁, p₁) ← t₁ e, (do (e₂, p₂) ← t₂ e₁, p ← mk_eq_trans p₁ p₂, return (e₂, p)) <|> return (e₁, p₁)) <|> t₂ e end tactic open tactic namespace norm_num variable {α : Type u} lemma subst_into_neg {α} [has_neg α] (a ta t : α) (pra : a = ta) (prt : -ta = t) : -a = t := by simp [pra, prt] theorem bit0_zero [add_group α] : bit0 (0 : α) = 0 := add_zero _ theorem bit1_zero [add_group α] [has_one α] : bit1 (0 : α) = 1 := by rw [bit1, bit0_zero, zero_add] lemma pow_bit0_helper [monoid α] (a t : α) (b : ℕ) (h : a ^ b = t) : a ^ bit0 b = t * t := by simp [pow_bit0, h] lemma pow_bit1_helper [monoid α] (a t : α) (b : ℕ) (h : a ^ b = t) : a ^ bit1 b = t * t * a := by simp [pow_bit1, h] lemma lt_add_of_pos_helper [ordered_cancel_comm_monoid α] (a b c : α) (h : a + b = c) (h₂ : 0 < b) : a < c := h ▸ (lt_add_iff_pos_right _).2 h₂ lemma nat_div_helper (a b q r : ℕ) (h : r + q * b = a) (h₂ : r < b) : a / b = q := by rw [← h, nat.add_mul_div_right _ _ (lt_of_le_of_lt (nat.zero_le _) h₂), nat.div_eq_of_lt h₂, zero_add] lemma int_div_helper (a b q r : ℤ) (h : r + q * b = a) (h₁ : 0 ≤ r) (h₂ : r < b) : a / b = q := by rw [← h, int.add_mul_div_right _ _ (ne_of_gt (lt_of_le_of_lt h₁ h₂)), int.div_eq_zero_of_lt h₁ h₂, zero_add] lemma nat_mod_helper (a b q r : ℕ) (h : r + q * b = a) (h₂ : r < b) : a % b = r := by rw [← h, nat.add_mul_mod_self_right, nat.mod_eq_of_lt h₂] lemma int_mod_helper (a b q r : ℤ) (h : r + q * b = a) (h₁ : 0 ≤ r) (h₂ : r < b) : a % b = r := by rw [← h, int.add_mul_mod_self, int.mod_eq_of_lt h₁ h₂] meta def eval_pow (simp : expr → tactic (expr × expr)) : expr → tactic (expr × expr) | `(@has_pow.pow %%α _ %%m %%e₁ %%e₂) := match m with | `(nat.has_pow) := mk_app ``nat.pow [e₁, e₂] >>= eval_pow | `(@monoid.has_pow %%α %%m) := mk_app ``monoid.pow [e₁, e₂] >>= eval_pow | _ := failed end | `(monoid.pow %%e₁ 0) := do p ← mk_app ``pow_zero [e₁], a ← infer_type e₁, o ← mk_app ``has_one.one [a], return (o, p) | `(monoid.pow %%e₁ 1) := do p ← mk_app ``pow_one [e₁], return (e₁, p) | `(monoid.pow %%e₁ (bit0 %%e₂)) := do e ← mk_app ``monoid.pow [e₁, e₂], (e', p) ← simp e, p' ← mk_app ``norm_num.pow_bit0_helper [e₁, e', e₂, p], e'' ← to_expr ``(%%e' * %%e'), return (e'', p') | `(monoid.pow %%e₁ (bit1 %%e₂)) := do e ← mk_app ``monoid.pow [e₁, e₂], (e', p) ← simp e, p' ← mk_app ``norm_num.pow_bit1_helper [e₁, e', e₂, p], e'' ← to_expr ``(%%e' * %%e' * %%e₁), return (e'', p') | `(nat.pow %%e₁ %%e₂) := do p₁ ← mk_app ``nat.pow_eq_pow [e₁, e₂] >>= mk_eq_symm, e ← mk_app ``monoid.pow [e₁, e₂], (e', p₂) ← simp e, p ← mk_eq_trans p₁ p₂, return (e', p) | _ := failed meta def prove_pos : instance_cache → expr → tactic (instance_cache × expr) | c `(has_one.one _) := do (c, p) ← c.mk_app ``zero_lt_one [], return (c, p) | c `(bit0 %%e) := do (c, p) ← prove_pos c e, (c, p) ← c.mk_app ``bit0_pos [e, p], return (c, p) | c `(bit1 %%e) := do (c, p) ← prove_pos c e, (c, p) ← c.mk_app ``bit1_pos' [e, p], return (c, p) | c `(%%e₁ / %%e₂) := do (c, p₁) ← prove_pos c e₁, (c, p₂) ← prove_pos c e₂, (c, p) ← c.mk_app ``div_pos_of_pos_of_pos [e₁, e₂, p₁, p₂], return (c, p) | c e := failed meta def prove_lt (simp : expr → tactic (expr × expr)) : instance_cache → expr → expr → tactic (instance_cache × expr) | c `(- %%e₁) `(- %%e₂) := do (c, p) ← prove_lt c e₁ e₂, (c, p) ← c.mk_app ``neg_lt_neg [e₁, e₂, p], return (c, p) | c `(- %%e₁) `(has_zero.zero _) := do (c, p) ← prove_pos c e₁, (c, p) ← c.mk_app ``neg_neg_of_pos [e₁, p], return (c, p) | c `(- %%e₁) e₂ := do (c, p₁) ← prove_pos c e₁, (c, me₁) ← c.mk_app ``has_neg.neg [e₁], (c, p₁) ← c.mk_app ``neg_neg_of_pos [e₁, p₁], (c, p₂) ← prove_pos c e₂, (c, z) ← c.mk_app ``has_zero.zero [], (c, p) ← c.mk_app ``lt_trans [me₁, z, e₂, p₁, p₂], return (c, p) | c `(has_zero.zero _) e₂ := prove_pos c e₂ | c e₁ e₂ := do n₁ ← e₁.to_rat, n₂ ← e₂.to_rat, d ← expr.of_rat c.α (n₂ - n₁), (c, e₃) ← c.mk_app ``has_add.add [e₁, d], (e₂', p) ← norm_num e₃, guard (e₂'.is_num_eq e₂), (c, p') ← prove_pos c d, (c, p) ← c.mk_app ``norm_num.lt_add_of_pos_helper [e₁, d, e₂, p, p'], return (c, p) private meta def true_intro (p : expr) : tactic (expr × expr) := prod.mk <$> mk_const `true <*> mk_app ``eq_true_intro [p] private meta def false_intro (p : expr) : tactic (expr × expr) := prod.mk <$> mk_const `false <*> mk_app ``eq_false_intro [p] meta def eval_ineq (simp : expr → tactic (expr × expr)) : expr → tactic (expr × expr) | `(%%e₁ < %%e₂) := do n₁ ← e₁.to_rat, n₂ ← e₂.to_rat, c ← infer_type e₁ >>= mk_instance_cache, if n₁ < n₂ then do (_, p) ← prove_lt simp c e₁ e₂, true_intro p else do (c, p) ← if n₁ = n₂ then c.mk_app ``lt_irrefl [e₁] else (do (c, p') ← prove_lt simp c e₂ e₁, c.mk_app ``not_lt_of_gt [e₁, e₂, p']), false_intro p | `(%%e₁ ≤ %%e₂) := do n₁ ← e₁.to_rat, n₂ ← e₂.to_rat, c ← infer_type e₁ >>= mk_instance_cache, if n₁ ≤ n₂ then do (c, p) ← if n₁ = n₂ then c.mk_app ``le_refl [e₁] else (do (c, p') ← prove_lt simp c e₁ e₂, c.mk_app ``le_of_lt [e₁, e₂, p']), true_intro p else do (c, p) ← prove_lt simp c e₂ e₁, (c, p) ← c.mk_app ``not_le_of_gt [e₁, e₂, p], false_intro p | `(%%e₁ = %%e₂) := do n₁ ← e₁.to_rat, n₂ ← e₂.to_rat, c ← infer_type e₁ >>= mk_instance_cache, if n₁ < n₂ then do (c, p) ← prove_lt simp c e₁ e₂, (c, p) ← c.mk_app ``ne_of_lt [e₁, e₂, p], false_intro p else if n₂ < n₁ then do (c, p) ← prove_lt simp c e₂ e₁, (c, p) ← c.mk_app ``ne_of_gt [e₁, e₂, p], false_intro p else mk_eq_refl e₁ >>= true_intro | `(%%e₁ > %%e₂) := mk_app ``has_lt.lt [e₂, e₁] >>= simp | `(%%e₁ ≥ %%e₂) := mk_app ``has_le.le [e₂, e₁] >>= simp | `(%%e₁ ≠ %%e₂) := do e ← mk_app ``eq [e₁, e₂], mk_app ``not [e] >>= simp | _ := failed meta def eval_div_ext (simp : expr → tactic (expr × expr)) : expr → tactic (expr × expr) | `(has_inv.inv %%e) := do c ← infer_type e >>= mk_instance_cache, (c, p₁) ← c.mk_app ``inv_eq_one_div [e], (c, o) ← c.mk_app ``has_one.one [], (c, e') ← c.mk_app ``has_div.div [o, e], (do (e'', p₂) ← simp e', p ← mk_eq_trans p₁ p₂, return (e'', p)) <|> return (e', p₁) | `(%%e₁ / %%e₂) := do α ← infer_type e₁, c ← mk_instance_cache α, match α with | `(nat) := do n₁ ← e₁.to_nat, n₂ ← e₂.to_nat, q ← expr.of_nat α (n₁ / n₂), r ← expr.of_nat α (n₁ % n₂), (c, e₃) ← c.mk_app ``has_mul.mul [q, e₂], (c, e₃) ← c.mk_app ``has_add.add [r, e₃], (e₁', p) ← norm_num e₃, guard (e₁' =ₐ e₁), (c, p') ← prove_lt simp c r e₂, p ← mk_app ``norm_num.nat_div_helper [e₁, e₂, q, r, p, p'], return (q, p) | `(int) := match e₂ with | `(- %%e₂') := do (c, p₁) ← c.mk_app ``int.div_neg [e₁, e₂'], (c, e) ← c.mk_app ``has_div.div [e₁, e₂'], (c, e) ← c.mk_app ``has_neg.neg [e], (e', p₂) ← simp e, p ← mk_eq_trans p₁ p₂, return (e', p) | _ := do n₁ ← e₁.to_int, n₂ ← e₂.to_int, q ← expr.of_rat α $ rat.of_int (n₁ / n₂), r ← expr.of_rat α $ rat.of_int (n₁ % n₂), (c, e₃) ← c.mk_app ``has_mul.mul [q, e₂], (c, e₃) ← c.mk_app ``has_add.add [r, e₃], (e₁', p) ← norm_num e₃, guard (e₁' =ₐ e₁), (c, r0) ← c.mk_app ``has_zero.zero [], (c, r0) ← c.mk_app ``has_le.le [r0, r], (_, p₁) ← simp r0, p₁ ← mk_app ``of_eq_true [p₁], (c, p₂) ← prove_lt simp c r e₂, p ← mk_app ``norm_num.int_div_helper [e₁, e₂, q, r, p, p₁, p₂], return (q, p) end | _ := failed end | `(%%e₁ % %%e₂) := do α ← infer_type e₁, c ← mk_instance_cache α, match α with | `(nat) := do n₁ ← e₁.to_nat, n₂ ← e₂.to_nat, q ← expr.of_nat α (n₁ / n₂), r ← expr.of_nat α (n₁ % n₂), (c, e₃) ← c.mk_app ``has_mul.mul [q, e₂], (c, e₃) ← c.mk_app ``has_add.add [r, e₃], (e₁', p) ← norm_num e₃, guard (e₁' =ₐ e₁), (c, p') ← prove_lt simp c r e₂, p ← mk_app ``norm_num.nat_mod_helper [e₁, e₂, q, r, p, p'], return (r, p) | `(int) := match e₂ with | `(- %%e₂') := do let p₁ := (expr.const ``int.mod_neg []).mk_app [e₁, e₂'], (c, e) ← c.mk_app ``has_mod.mod [e₁, e₂'], (e', p₂) ← simp e, p ← mk_eq_trans p₁ p₂, return (e', p) | _ := do n₁ ← e₁.to_int, n₂ ← e₂.to_int, q ← expr.of_rat α $ rat.of_int (n₁ / n₂), r ← expr.of_rat α $ rat.of_int (n₁ % n₂), (c, e₃) ← c.mk_app ``has_mul.mul [q, e₂], (c, e₃) ← c.mk_app ``has_add.add [r, e₃], (e₁', p) ← norm_num e₃, guard (e₁' =ₐ e₁), (c, r0) ← c.mk_app ``has_zero.zero [], (c, r0) ← c.mk_app ``has_le.le [r0, r], (_, p₁) ← simp r0, p₁ ← mk_app ``of_eq_true [p₁], (c, p₂) ← prove_lt simp c r e₂, p ← mk_app ``norm_num.int_mod_helper [e₁, e₂, q, r, p, p₁, p₂], return (r, p) end | _ := failed end | `(%%e₁ ∣ %%e₂) := do α ← infer_type e₁, c ← mk_instance_cache α, n ← match α with | `(nat) := return ``nat.dvd_iff_mod_eq_zero | `(int) := return ``int.dvd_iff_mod_eq_zero | _ := failed end, p₁ ← mk_app ``propext [@expr.const tt n [] e₁ e₂], (e', p₂) ← simp `(%%e₂ % %%e₁ = 0), p' ← mk_eq_trans p₁ p₂, return (e', p') | _ := failed lemma not_prime_helper (a b n : ℕ) (h : a * b = n) (h₁ : 1 < a) (h₂ : 1 < b) : ¬ nat.prime n := by rw ← h; exact nat.not_prime_mul h₁ h₂ lemma is_prime_helper (n : ℕ) (h₁ : 1 < n) (h₂ : nat.min_fac n = n) : nat.prime n := nat.prime_def_min_fac.2 ⟨h₁, h₂⟩ lemma min_fac_bit0 (n : ℕ) : nat.min_fac (bit0 n) = 2 := by simp [nat.min_fac_eq, show 2 ∣ bit0 n, by simp [bit0_eq_two_mul n]] def min_fac_helper (n k : ℕ) : Prop := 0 < k ∧ bit1 k ≤ nat.min_fac (bit1 n) theorem min_fac_helper.n_pos {n k : ℕ} (h : min_fac_helper n k) : 0 < n := nat.pos_iff_ne_zero.2 $ λ e, by rw e at h; exact not_le_of_lt (nat.bit1_lt h.1) h.2 lemma min_fac_ne_bit0 {n k : ℕ} : nat.min_fac (bit1 n) ≠ bit0 k := by rw bit0_eq_two_mul; exact λ e, absurd ((nat.dvd_add_iff_right (by simp [bit0_eq_two_mul n])).2 (dvd_trans ⟨_, e⟩ (nat.min_fac_dvd _))) dec_trivial lemma min_fac_helper_0 (n : ℕ) (h : 0 < n) : min_fac_helper n 1 := begin refine ⟨zero_lt_one, lt_of_le_of_ne _ min_fac_ne_bit0.symm⟩, refine @lt_of_le_of_ne ℕ _ _ _ (nat.min_fac_pos _) _, intro e, have := nat.min_fac_prime _, { rw ← e at this, exact nat.not_prime_one this }, { exact ne_of_gt (nat.bit1_lt h) } end lemma min_fac_helper_1 {n k k' : ℕ} (e : k + 1 = k') (np : nat.min_fac (bit1 n) ≠ bit1 k) (h : min_fac_helper n k) : min_fac_helper n k' := begin rw ← e, refine ⟨nat.succ_pos _, (lt_of_le_of_ne (lt_of_le_of_ne _ _ : k+1+k < _) min_fac_ne_bit0.symm : bit0 (k+1) < _)⟩, { rw add_right_comm, exact h.2 }, { rw add_right_comm, exact np.symm } end lemma min_fac_helper_2 (n k k' : ℕ) (e : k + 1 = k') (np : ¬ nat.prime (bit1 k)) (h : min_fac_helper n k) : min_fac_helper n k' := begin refine min_fac_helper_1 e _ h, intro e₁, rw ← e₁ at np, exact np (nat.min_fac_prime $ ne_of_gt $ nat.bit1_lt h.n_pos) end lemma min_fac_helper_3 (n k k' : ℕ) (e : k + 1 = k') (nd : bit1 k ∣ bit1 n = false) (h : min_fac_helper n k) : min_fac_helper n k' := begin refine min_fac_helper_1 e _ h, intro e₁, rw [eq_false, ← e₁] at nd, exact nd (nat.min_fac_dvd _) end lemma min_fac_helper_4 (n k : ℕ) (hd : bit1 k ∣ bit1 n = true) (h : min_fac_helper n k) : nat.min_fac (bit1 n) = bit1 k := by rw eq_true at hd; exact le_antisymm (nat.min_fac_le_of_dvd (nat.bit1_lt h.1) hd) h.2 lemma min_fac_helper_5 (n k k' : ℕ) (e : bit1 k * bit1 k = k') (hd : bit1 n < k') (h : min_fac_helper n k) : nat.min_fac (bit1 n) = bit1 n := begin refine (nat.prime_def_min_fac.1 (nat.prime_def_le_sqrt.2 ⟨nat.bit1_lt h.n_pos, _⟩)).2, rw ← e at hd, intros m m2 hm md, have := le_trans h.2 (le_trans (nat.min_fac_le_of_dvd m2 md) hm), rw nat.le_sqrt at this, exact not_le_of_lt hd this end meta def prove_non_prime (simp : expr → tactic (expr × expr)) (e : expr) (n d₁ : ℕ) : tactic expr := do let e₁ := reflect d₁, c ← mk_instance_cache `(nat), (c, p₁) ← prove_lt simp c `(1) e₁, let d₂ := n / d₁, let e₂ := reflect d₂, (e', p) ← mk_app ``has_mul.mul [e₁, e₂] >>= norm_num, guard (e' =ₐ e), (c, p₂) ← prove_lt simp c `(1) e₂, return $ (expr.const ``not_prime_helper []).mk_app [e₁, e₂, e, p, p₁, p₂] meta def prove_min_fac (simp : expr → tactic (expr × expr)) (e₁ : expr) (n1 : ℕ) : expr → expr → tactic (expr × expr) | e₂ p := do k ← e₂.to_nat, let k1 := bit1 k, e₁1 ← mk_app ``bit1 [e₁], e₂1 ← mk_app ``bit1 [e₂], if n1 < k1*k1 then do c ← mk_instance_cache `(nat), (c, e') ← c.mk_app ``has_mul.mul [e₂1, e₂1], (e', p₁) ← norm_num e', (c, p₂) ← prove_lt simp c e₁1 e', p' ← mk_app ``min_fac_helper_5 [e₁, e₂, e', p₁, p₂, p], return (e₁1, p') else let d := k1.min_fac in if to_bool (d < k1) then do (e', p₁) ← norm_num `(%%e₂ + 1), p₂ ← prove_non_prime simp e₂1 k1 d, mk_app ``min_fac_helper_2 [e₁, e₂, e', p₁, p₂, p] >>= prove_min_fac e' else do (_, p₂) ← simp `((%%e₂1 : ℕ) ∣ %%e₁1), if k1 ∣ n1 then do p' ← mk_app ``min_fac_helper_4 [e₁, e₂, p₂, p], return (e₂1, p') else do (e', p₁) ← norm_num `(%%e₂ + 1), mk_app ``min_fac_helper_3 [e₁, e₂, e', p₁, p₂, p] >>= prove_min_fac e' meta def eval_prime (simp : expr → tactic (expr × expr)) : expr → tactic (expr × expr) | `(nat.prime %%e) := do n ← e.to_nat, match n with | 0 := false_intro `(nat.not_prime_zero) | 1 := false_intro `(nat.not_prime_one) | _ := let d₁ := n.min_fac in if d₁ < n then prove_non_prime simp e n d₁ >>= false_intro else do let e₁ := reflect d₁, c ← mk_instance_cache `(nat), (c, p₁) ← prove_lt simp c `(1) e₁, (e₁, p) ← simp `(nat.min_fac %%e), true_intro $ (expr.const ``is_prime_helper []).mk_app [e, p₁, p] end | `(nat.min_fac 0) := refl_conv (reflect (0:ℕ)) | `(nat.min_fac 1) := refl_conv (reflect (1:ℕ)) | `(nat.min_fac (bit0 %%e)) := prod.mk `(2) <$> mk_app ``min_fac_bit0 [e] | `(nat.min_fac (bit1 %%e)) := do n ← e.to_nat, c ← mk_instance_cache `(nat), (c, p) ← prove_pos c e, mk_app ``min_fac_helper_0 [e, p] >>= prove_min_fac simp e (bit1 n) `(1) | _ := failed meta def derive1 (simp : expr → tactic (expr × expr)) (e : expr) : tactic (expr × expr) := norm_num e <|> eval_div_ext simp e <|> eval_pow simp e <|> eval_ineq simp e <|> eval_prime simp e meta def derive : expr → tactic (expr × expr) | e := do e ← instantiate_mvars e, (_, e', pr) ← ext_simplify_core () {} simp_lemmas.mk (λ _, failed) (λ _ _ _ _ _, failed) (λ _ _ _ _ e, do (new_e, pr) ← derive1 derive e, guard (¬ new_e =ₐ e), return ((), new_e, some pr, tt)) `eq e, return (e', pr) end norm_num namespace tactic.interactive open norm_num interactive interactive.types /-- Basic version of `norm_num` that does not call `simp`. -/ meta def norm_num1 (loc : parse location) : tactic unit := do ns ← loc.get_locals, tt ← tactic.replace_at derive ns loc.include_goal | fail "norm_num failed to simplify", when loc.include_goal $ try tactic.triv, when (¬ ns.empty) $ try tactic.contradiction /-- Normalize numerical expressions. Supports the operations `+` `-` `*` `/` `^` and `%` over numerical types such as `ℕ`, `ℤ`, `ℚ`, `ℝ`, `ℂ` and some general algebraic types, and can prove goals of the form `A = B`, `A ≠ B`, `A < B` and `A ≤ B`, where `A` and `B` are numerical expressions. It also has a relatively simple primality prover. -/ meta def norm_num (hs : parse simp_arg_list) (l : parse location) : tactic unit := repeat1 $ orelse' (norm_num1 l) $ simp_core {} (norm_num1 (loc.ns [none])) ff hs [] l meta def apply_normed (x : parse texpr) : tactic unit := do x₁ ← to_expr x, (x₂,_) ← derive x₁, tactic.exact x₂ end tactic.interactive namespace conv.interactive open conv interactive tactic.interactive open norm_num (derive) meta def norm_num1 : conv unit := replace_lhs derive meta def norm_num (hs : parse simp_arg_list) : conv unit := repeat1 $ orelse' norm_num1 $ simp_core {} norm_num1 ff hs [] (loc.ns [none]) end conv.interactive
272da27f999ac6a736c1f2afe7b11aa8a5e196b8
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/measure_theory/bochner_integration.lean
ebab11ab93865ea55fb8461c01ed8765cdb9138e
[ "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
73,117
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 -/ import measure_theory.simple_func_dense import analysis.normed_space.bounded_linear_maps import measure_theory.l1_space import measure_theory.group 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 α ℝ≥0∞`.) 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` : `∫ 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 `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 `ℝ≥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 * `∫ 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 font is missing. ## Tags Bochner integral, simple function, function space, Lebesgue dominated convergence theorem -/ noncomputable theory open_locale classical topological_space big_operators nnreal ennreal measure_theory namespace measure_theory variables {α E : Type*} [measurable_space α] [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 a, rw coe_sub, 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] [measurable_space E] [normed_group F] variables {μ : 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 → ℝ≥0∞) x ∂μ < ∞ : and_iff_right f.ae_measurable ... ↔ (f.map (coe ∘ nnnorm : E → ℝ≥0∞)).lintegral μ < ∞ : by rw lintegral_eq_lintegral ... ↔ (f.map (coe ∘ nnnorm : E → ℝ≥0∞)).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 [measurable_space F] {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 _ /-- The Bochner integral is equal to a sum over any set that includes `f.range` (except `0`). -/ lemma integral_eq_sum_of_subset {f : α →ₛ F} {μ : measure α} {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 [disjoint_singleton_left, hx] 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) := 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.measurable_set_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 `α →ₛ ℝ≥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) (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, 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_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 μ := 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 `α →ₛ ℝ≥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], { 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 [borel_space E] {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 (c : 𝕜) {f : α →ₛ E} (hf : integrable f μ) : integral μ (c • f) = c • integral μ f := calc integral μ (c • f) = ∑ x in f.range, ennreal.to_real (μ (f ⁻¹' {x})) • c • x : by rw [smul_eq_map c f, map_integral f _ hf (smul_zero _)] ... = ∑ x in f.range, c • (ennreal.to_real (μ (f ⁻¹' {x}))) • x : finset.sum_congr rfl $ λ b hb, by { exact smul_comm _ _ _} ... = c • 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 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 μ) /-- `L1.simple_func` is a subspace of L1 consisting of equivalence classes of an integrable simple function. -/ def simple_func : add_subgroup (Lp E 1 μ) := { carrier := {f : α →₁[μ] E | ∃ (s : α →ₛ E), (ae_eq_fun.mk s s.ae_measurable : α →ₘ[μ] E) = f}, zero_mem' := ⟨0, rfl⟩, add_mem' := λ f g ⟨s, hs⟩ ⟨t, ht⟩, ⟨s + t, by simp only [←hs, ←ht, mk_add_mk, add_subgroup.coe_add, mk_eq_mk, simple_func.coe_add]⟩, neg_mem' := λ f ⟨s, hs⟩, ⟨-s, by simp only [←hs, neg_mk, simple_func.coe_neg, mk_eq_mk, add_subgroup.coe_neg]⟩ } 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 `normed_group`, 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 normed_group : normed_group (α →₁ₛ[μ] E) := by apply_instance local attribute [instance] simple_func.normed_group /-- Functions `α →₁ₛ[μ] E` form an additive commutative 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 lemma norm_eq (f : α →₁ₛ[μ] E) : ∥f∥ = ∥(f : α →₁[μ] E)∥ := rfl variables [normed_field 𝕜] [normed_space 𝕜 E] [measurable_space 𝕜] [opens_measurable_space 𝕜] /-- 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, apply eq.trans (smul_mk k s s.ae_measurable).symm _, rw 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 module : module 𝕜 (α →₁ₛ[μ] 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], exact smul_add _ _ _ }), smul_zero := λx, simple_func.eq (by { simp only [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.module /-- 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 to_L1 /-- Construct the equivalence class `[f]` of an integrable simple function `f`. -/ @[reducible] def to_L1 (f : α →ₛ E) (hf : integrable f μ) : (α →₁ₛ[μ] E) := ⟨hf.to_L1 f, ⟨f, rfl⟩⟩ lemma to_L1_eq_to_L1 (f : α →ₛ E) (hf : integrable f μ) : (to_L1 f hf : α →₁[μ] E) = hf.to_L1 f := rfl lemma to_L1_eq_mk (f : α →ₛ E) (hf : integrable f μ) : (to_L1 f hf : α →ₘ[μ] E) = ae_eq_fun.mk f f.ae_measurable := rfl lemma to_L1_zero : to_L1 (0 : α →ₛ E) (integrable_zero α E μ) = 0 := rfl lemma to_L1_add (f g : α →ₛ E) (hf : integrable f μ) (hg : integrable g μ) : to_L1 (f + g) (hf.add hg) = to_L1 f hf + to_L1 g hg := rfl lemma to_L1_neg (f : α →ₛ E) (hf : integrable f μ) : to_L1 (-f) hf.neg = -to_L1 f hf := rfl lemma to_L1_sub (f g : α →ₛ E) (hf : integrable f μ) (hg : integrable g μ) : to_L1 (f - g) (hf.sub hg) = to_L1 f hf - to_L1 g hg := by { simp only [sub_eq_add_neg, ← to_L1_neg, ← to_L1_add], refl } variables [normed_field 𝕜] [normed_space 𝕜 E] [measurable_space 𝕜] [opens_measurable_space 𝕜] lemma to_L1_smul (f : α →ₛ E) (hf : integrable f μ) (c : 𝕜) : to_L1 (c • f) (hf.smul c) = c • to_L1 f hf := rfl lemma norm_to_L1 (f : α →ₛ E) (hf : integrable f μ) : ∥to_L1 f hf∥ = ennreal.to_real (∫⁻ a, edist (f a) 0 ∂μ) := by simp [to_L1, integrable.norm_to_L1] end to_L1 section to_simple_func /-- Find a representative of a `L1.simple_func`. -/ def to_simple_func (f : α →₁ₛ[μ] E) : α →ₛ E := classical.some f.2 /-- `(to_simple_func f)` is measurable. -/ protected lemma measurable (f : α →₁ₛ[μ] E) : measurable (to_simple_func f) := (to_simple_func f).measurable protected lemma ae_measurable (f : α →₁ₛ[μ] E) : ae_measurable (to_simple_func f) μ := (simple_func.measurable f).ae_measurable /-- `to_simple_func f` is integrable. -/ protected lemma integrable (f : α →₁ₛ[μ] E) : integrable (to_simple_func f) μ := begin apply (integrable_mk (simple_func.ae_measurable f)).1, convert integrable_coe_fn f.val, exact classical.some_spec f.2 end lemma to_L1_to_simple_func (f : α →₁ₛ[μ] E) : to_L1 (to_simple_func f) (simple_func.integrable f) = f := by { rw ← simple_func.eq_iff', exact classical.some_spec f.2 } lemma to_simple_func_to_L1 (f : α →ₛ E) (hfi : integrable f μ) : to_simple_func (to_L1 f hfi) =ᵐ[μ] f := by { rw ← mk_eq_mk, exact classical.some_spec (to_L1 f hfi).2 } lemma to_simple_func_eq_to_fun (f : α →₁ₛ[μ] E) : to_simple_func f =ᵐ[μ] f := begin simp_rw [← integrable.to_L1_eq_to_L1_iff (to_simple_func f) f (simple_func.integrable f) (integrable_coe_fn ↑f), subtype.ext_iff], convert classical.some_spec f.coe_prop, exact integrable.to_L1_coe_fn _ _, end variables (E μ) lemma zero_to_simple_func : to_simple_func (0 : α →₁ₛ[μ] E) =ᵐ[μ] 0 := begin filter_upwards [to_simple_func_eq_to_fun (0 : α →₁ₛ[μ] E), Lp.coe_fn_zero E 1 μ], assume a h₁ h₂, rwa h₁, end variables {E μ} lemma add_to_simple_func (f g : α →₁ₛ[μ] E) : to_simple_func (f + g) =ᵐ[μ] to_simple_func f + to_simple_func g := 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, Lp.coe_fn_add (f : α →₁[μ] E) g], assume a, simp only [← coe_coe, coe_add, pi.add_apply], iterate 4 { assume h, rw h } end lemma neg_to_simple_func (f : α →₁ₛ[μ] E) : to_simple_func (-f) =ᵐ[μ] - to_simple_func f := begin filter_upwards [to_simple_func_eq_to_fun (-f), to_simple_func_eq_to_fun f, Lp.coe_fn_neg (f : α →₁[μ] E)], assume a, simp only [pi.neg_apply, coe_neg, ← coe_coe], repeat { assume h, rw h } end lemma sub_to_simple_func (f g : α →₁ₛ[μ] E) : to_simple_func (f - g) =ᵐ[μ] to_simple_func f - to_simple_func g := 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, Lp.coe_fn_sub (f : α →₁[μ] E) g], assume a, simp only [coe_sub, pi.sub_apply, ← coe_coe], repeat { assume h, rw h } end variables [normed_field 𝕜] [normed_space 𝕜 E] [measurable_space 𝕜] [opens_measurable_space 𝕜] lemma smul_to_simple_func (k : 𝕜) (f : α →₁ₛ[μ] E) : to_simple_func (k • f) =ᵐ[μ] k • to_simple_func f := begin filter_upwards [to_simple_func_eq_to_fun (k • f), to_simple_func_eq_to_fun f, Lp.coe_fn_smul k (f : α →₁[μ] E)], assume a, simp only [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_lt_top (integrable_coe_fn _) (integrable_coe_fn _) end lemma dist_to_simple_func (f g : α →₁ₛ[μ] E) : dist f g = ennreal.to_real (∫⁻ x, edist (to_simple_func f x) (to_simple_func g x) ∂μ) := begin rw [dist_eq, L1.dist_def, ennreal.to_real_eq_to_real], { rw lintegral_rw₂, repeat { exact ae_eq_symm (to_simple_func_eq_to_fun _) } }, { exact lintegral_edist_lt_top (integrable_coe_fn _) (integrable_coe_fn _) }, { 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 ((to_simple_func f) x) (to_simple_func (0 : α →₁ₛ[μ] E) x) ∂μ) : begin rw [← dist_zero_right, dist_to_simple_func] end ... = ennreal.to_real (∫⁻ (x : α), (coe ∘ nnnorm) ((to_simple_func f) 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∥ = ((to_simple_func f).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 (simple_func.integrable f).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, assume f, rw mem_closure_iff_seq_limit, have hfi' : integrable f μ := integrable_coe_fn f, refine ⟨λ n, ↑(to_L1 (simple_func.approx_on f (Lp.measurable f) univ 0 trivial n) (simple_func.integrable_approx_on_univ (Lp.measurable f) hfi' n)), λ n, mem_range_self _, _⟩, convert simple_func.tendsto_approx_on_univ_L1 (Lp.measurable f) hfi', rw integrable.to_L1_coe_fn 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 [normed_field 𝕜] [normed_space 𝕜 E] [measurable_space 𝕜] [opens_measurable_space 𝕜] 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 : α →₁ₛ[μ] ℝ) : α →₁ₛ[μ] ℝ := ⟨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] 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 /-! Define the Bochner integral on `α →₁ₛ[μ] E` and prove basic properties of this integral. -/ variables [normed_field 𝕜] [normed_space 𝕜 E] [normed_space ℝ E] [smul_comm_class ℝ 𝕜 E] /-- 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_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 := begin simp only [integral], rw ← simple_func.integral_add (simple_func.integrable f) (simple_func.integrable g), apply measure_theory.simple_func.integral_congr (simple_func.integrable (f + g)), apply add_to_simple_func end lemma integral_smul [measurable_space 𝕜] [opens_measurable_space 𝕜] (c : 𝕜) (f : α →₁ₛ[μ] E) : integral (c • f) = c • integral f := begin simp only [integral], rw ← simple_func.integral_smul _ (simple_func.integrable f), apply measure_theory.simple_func.integral_congr (simple_func.integrable (c • f)), apply smul_to_simple_func, repeat { assumption }, end 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 μ 𝕜) [measurable_space 𝕜] [opens_measurable_space 𝕜] /-- 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 `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], 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 : α →₁ₛ[μ] ℝ) : 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], 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₂ : (to_simple_func f).neg_part =ᵐ[μ] (to_simple_func (neg_part f)).map norm, { filter_upwards [neg_part_to_simple_func f], 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 ∂μ, (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₂], assume a 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₂], assume a 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).max_zero.congr ae_eq₁ }, { exact (simple_func.integrable f).neg.max_zero.congr ae_eq₂ } end end pos_part end simple_func_integral end simple_func open simple_func local notation `Integral` := @integral_clm α E _ _ _ _ _ μ _ variables [normed_space ℝ E] [nondiscrete_normed_field 𝕜] [normed_space 𝕜 E] [smul_comm_class ℝ 𝕜 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 variables (𝕜) [measurable_space 𝕜] [opens_measurable_space 𝕜] /-- The Bochner integral in L1 space as a continuous linear map. -/ def integral_clm' : (α →₁[μ] E) →L[𝕜] E := (integral_clm' α E 𝕜 μ).extend (coe_to_L1 α E 𝕜) simple_func.dense_range 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 @[norm_cast] lemma simple_func.integral_L1_eq_integral (f : α →₁ₛ[μ] E) : integral (f : α →₁[μ] E) = (simple_func.integral f) := uniformly_extend_of_ind simple_func.uniform_inducing simple_func.dense_range (simple_func.integral_clm α E μ).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 (c : 𝕜) (f : α →₁[μ] E) : integral (c • f) = c • integral f := map_smul (integral_clm' 𝕜) c 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 : ℝ≥0) * ∥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), integral f) := by simp [L1.integral, L1.integral_clm.continuous] section pos_part local attribute [instance] fact_one_le_one_ennreal 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∥) L1.simple_func.dense_range (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, 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] [nondiscrete_normed_field 𝕜] [normed_space 𝕜 E] [smul_comm_class ℝ 𝕜 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 : integrable f μ then L1.integral (hf.to_L1 f) else 0 /-! 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} {μ : measure α} lemma integral_eq (f : α → E) (hf : integrable f μ) : ∫ a, f a ∂μ = L1.integral (hf.to_L1 f) := dif_pos hf lemma L1.integral_eq_integral (f : α →₁[μ] E) : L1.integral f = ∫ a, f a ∂μ := by rw [integral_eq _ (L1.integrable_coe_fn f), integrable.to_L1_coe_fn] lemma integral_undef (h : ¬ integrable f μ) : ∫ a, f a ∂μ = 0 := dif_neg h lemma integral_non_ae_measurable (h : ¬ ae_measurable f μ) : ∫ a, f a ∂μ = 0 := integral_undef $ not_and_of_not_left _ h variables (α E) lemma integral_zero : ∫ a : α, (0:E) ∂μ = 0 := by { rw [integral_eq _ (integrable_zero α E μ)], exact L1.integral_zero _ _ } @[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 ∂μ := begin rw [integral_eq, integral_eq f hf, integral_eq g hg, ← L1.integral_add], { refl }, { exact hf.add hg } end 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_neg (f : α → E) : ∫ a, -f a ∂μ = - ∫ a, f a ∂μ := begin by_cases hf : integrable f μ, { rw [integral_eq f hf, integral_eq (λa, - f a) hf.neg, ← L1.integral_neg], refl }, { rw [integral_undef hf, integral_undef, neg_zero], rwa [← integrable_neg_iff] at hf } end 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 ∂μ := by { simp only [sub_eq_add_neg, ← integral_neg], exact integral_add hf hg.neg } 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 [measurable_space 𝕜] [opens_measurable_space 𝕜] (c : 𝕜) (f : α → E) : ∫ a, c • (f a) ∂μ = c • ∫ a, f a ∂μ := begin by_cases hf : integrable f μ, { rw [integral_eq f hf, integral_eq (λa, c • (f a)), integrable.to_L1_smul, L1.integral_smul], }, { by_cases hr : c = 0, { simp only [hr, measure_theory.integral_zero, zero_smul] }, have hf' : ¬ integrable (λ x, c • f x) μ, { change ¬ integrable (c • f) μ, rwa [integrable_smul_iff hr f] }, 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 (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 hfi, integral_eq g hgi, (integrable.to_L1_eq_to_L1_iff f g hfi hgi).2 h] }, { have hgi : ¬ integrable g μ, { rw integrable_congr h at hfi, exact hfi }, rw [integral_undef hfi, integral_undef hgi] }, end @[simp] lemma L1.integral_of_fun_eq_integral {f : α → E} (hf : integrable f μ) : ∫ a, (hf.to_L1 f) a ∂μ = ∫ a, f a ∂μ := integral_congr_ae $ by simp [integrable.coe_fn_to_L1] @[continuity] lemma continuous_integral : continuous (λ (f : α →₁[μ] E), ∫ a, f a ∂μ) := by { simp only [← L1.integral_eq_integral], exact L1.continuous_integral } 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) : (nnnorm (∫ a, f a ∂μ) : ℝ≥0∞) ≤ ∫⁻ a, (nnnorm (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 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, 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' (hFi.mp $ hFi.mono $ λ i hFi hFm, _) hF, simp only [norm_norm, ← integral_sub hFi 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, ae_measurable (F n) μ) (f_measurable : ae_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.has_finite_integral 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' (F_measurable n) (h_bound _), have h₂ : integrable f μ := ⟨f_measurable, has_finite_integral_of_dominated_convergence bound_integrable.has_finite_integral h_bound h_lim⟩, rw ← integral_sub h₁ 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, ae_measurable (F n) μ) (f_measurable : ae_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], 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 variables {X : Type*} [topological_space X] [first_countable_topology X] lemma continuous_at_of_dominated {F : X → α → E} {x₀ : X} {bound : α → ℝ} (hF_meas : ∀ᶠ x in 𝓝 x₀, ae_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₀ := tendsto_integral_filter_of_dominated_convergence bound (first_countable_topology.nhds_generated_countable x₀) ‹_› (mem_of_mem_nhds hF_meas : _) ‹_› ‹_› ‹_› lemma continuous_of_dominated {F : X → α → E} {bound : α → ℝ} (hF_meas : ∀ x, ae_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_iff_continuous_at.mpr (λ x₀, continuous_at_of_dominated (eventually_of_forall hF_meas) (eventually_of_forall h_bound) ‹_› $ h_cont.mono $ λ _, continuous.continuous_at) /-- 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 : α → ℝ} (hf : 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₁ := hf.to_L1 f in -- Go to the `L¹` space have eq₁ : ennreal.to_real (∫⁻ a, (ennreal.of_real $ max (f a) 0) ∂μ) = ∥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], assume a h₁ h₂, rw [h₁, h₂, ennreal.of_real, nnnorm], congr' 1, apply nnreal.eq, simp [real.norm_of_nonneg, le_max_right, nnreal.coe_of_real] end, -- Go to the `L¹` space have eq₂ : ennreal.to_real (∫⁻ a, (ennreal.of_real $ -min (f a) 0) ∂μ) = ∥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], assume a h₁ h₂, rw [h₁, h₂, ennreal.of_real, nnnorm], congr' 1, apply nnreal.eq, simp [real.norm_of_nonneg, min_le_right, nnreal.coe_of_real, neg_nonneg], 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_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 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_ae_measurable (measurable_id.neg.comp_ae_measurable $ hfm.min ae_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_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_nonneg_of_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) : 0 ≤ ∫ a, f a ∂μ := begin by_cases hfm : ae_measurable f μ, { rw integral_eq_lintegral_of_nonneg_ae hf hfm, exact to_real_nonneg }, { rw integral_non_ae_measurable hfm } end lemma lintegral_coe_eq_integral (f : α → ℝ≥0) (hfi : integrable (λ x, (f x : real)) μ) : ∫⁻ 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_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 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.to_real], { 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 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), ← 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_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∥ ∂μ := 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])) (continuous_norm.measurable.comp_ae_measurable (Lp.ae_measurable f)), 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 end normed_group lemma integral_mono_ae {f g : α → ℝ} (hf : integrable f μ) (hg : integrable g μ) (h : f ≤ᵐ[μ] g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ := le_of_sub_nonneg $ integral_sub hg hf ▸ integral_nonneg_of_ae $ h.mono (λ a, sub_nonneg_of_le) @[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_measurable f μ, { refine integral_mono_ae ⟨hfm, _⟩ hgi h, refine (hgi.has_finite_integral.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_ae_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 : ae_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 $ ae_measurable.norm h).symm ) ( λh : ¬ae_measurable f μ, begin rw [integral_non_ae_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 integral_finset_sum {ι} (s : finset ι) {f : ι → α → E} (hf : ∀ 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 (hf _) (integrable_finset_sum s hf), ih] } end 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_L1_eq_to_L1, 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_to_L1 _ _).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, integral_zero] }, { 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_undef, *] } } 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 (integrable_const C) h ... = C * (μ univ).to_real : by rw [integral_const, smul_eq_mul, mul_comm] lemma tendsto_integral_approx_on_univ_of_measurable {f : α → E} (fmeas : measurable f) (hf : integrable f μ) : tendsto (λ n, (simple_func.approx_on f fmeas univ 0 trivial n).integral μ) at_top (𝓝 $ ∫ x, f x ∂μ) := begin have : tendsto (λ n, ∫ x, simple_func.approx_on f fmeas univ 0 trivial n x ∂μ) at_top (𝓝 $ ∫ x, f x ∂μ) := tendsto_integral_of_L1 _ hf (eventually_of_forall $ simple_func.integrable_approx_on_univ fmeas hf) (simple_func.tendsto_approx_on_univ_L1_edist fmeas hf), simpa only [simple_func.integral_eq_integral, simple_func.integrable_approx_on_univ fmeas hf] end variable {ν : measure α} private lemma integral_add_measure_of_measurable {f : α → E} (fmeas : measurable f) (hμ : integrable f μ) (hν : integrable f ν) : ∫ x, f x ∂(μ + ν) = ∫ x, f x ∂μ + ∫ x, f x ∂ν := begin have hfi := hμ.add_measure hν, refine tendsto_nhds_unique (tendsto_integral_approx_on_univ_of_measurable fmeas hfi) _, simpa only [simple_func.integral_add_measure _ (simple_func.integrable_approx_on_univ fmeas hfi _)] using (tendsto_integral_approx_on_univ_of_measurable fmeas hμ).add (tendsto_integral_approx_on_univ_of_measurable fmeas hν) end lemma integral_add_measure {f : α → E} (hμ : integrable f μ) (hν : integrable f ν) : ∫ x, f x ∂(μ + ν) = ∫ x, f x ∂μ + ∫ x, f x ∂ν := begin have h : ae_measurable f (μ + ν) := hμ.ae_measurable.add_measure hν.ae_measurable, let g := h.mk f, have A : f =ᵐ[μ + ν] g := h.ae_eq_mk, have B : f =ᵐ[μ] g := A.filter_mono (ae_mono (measure.le_add_right (le_refl μ))), have C : f =ᵐ[ν] g := A.filter_mono (ae_mono (measure.le_add_left (le_refl ν))), calc ∫ x, f x ∂(μ + ν) = ∫ x, g x ∂(μ + ν) : integral_congr_ae A ... = ∫ x, g x ∂μ + ∫ x, g x ∂ν : integral_add_measure_of_measurable h.measurable_mk ((integrable_congr B).1 hμ) ((integrable_congr C).1 hν) ... = ∫ x, f x ∂μ + ∫ x, f x ∂ν : by { congr' 1, { exact integral_congr_ae B.symm }, { exact integral_congr_ae C.symm } } 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 private lemma integral_smul_measure_aux {f : α → E} {c : ℝ≥0∞} (h0 : 0 < c) (hc : c < ∞) (fmeas : measurable f) (hfi : integrable f μ) : ∫ x, f x ∂(c • μ) = c.to_real • ∫ x, f x ∂μ := begin refine tendsto_nhds_unique _ (tendsto_const_nhds.smul (tendsto_integral_approx_on_univ_of_measurable fmeas hfi)), convert tendsto_integral_approx_on_univ_of_measurable fmeas (hfi.smul_measure hc), simp only [simple_func.integral, measure.smul_apply, finset.smul_sum, smul_smul, ennreal.to_real_mul] end @[simp] lemma integral_smul_measure (f : α → E) (c : ℝ≥0∞) : ∫ x, f x ∂(c • μ) = c.to_real • ∫ x, f x ∂μ := begin -- First we consider “degenerate” cases: -- `c = 0` rcases (zero_le c).eq_or_lt with rfl|h0, { simp }, -- `f` is not almost everywhere measurable by_cases hfm : ae_measurable f μ, swap, { have : ¬ (ae_measurable f (c • μ)), by simpa [ne_of_gt h0] using hfm, simp [integral_non_ae_measurable, hfm, this] }, -- `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_undef, rw [integrable, has_finite_integral, iff_true_intro (hfm.smul_measure ∞), true_and, 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 and `0 < c < ∞` by_cases hfi : integrable f μ, swap, { rw [integral_undef hfi, smul_zero], refine integral_undef (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 almost everywhere measurable and integrable let g := hfm.mk f, calc ∫ x, f x ∂(c • μ) = ∫ x, g x ∂(c • μ) : integral_congr_ae $ ae_smul_measure hfm.ae_eq_mk c ... = c.to_real • ∫ x, g x ∂μ : integral_smul_measure_aux h0 hc hfm.measurable_mk $ hfi.congr hfm.ae_eq_mk ... = c.to_real • ∫ x, f x ∂μ : by { congr' 1, exact integral_congr_ae (hfm.ae_eq_mk.symm) } end lemma integral_map_of_measurable {β} [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_undef hfi, integral_undef], rwa [← integrable_map_measure hfm.ae_measurable hφ] }, refine tendsto_nhds_unique (tendsto_integral_approx_on_univ_of_measurable hfm hfi) _, convert tendsto_integral_approx_on_univ_of_measurable (hfm.comp hφ) ((integrable_map_measure hfm.ae_measurable hφ).1 hfi), ext1 i, simp only [simple_func.approx_on_comp, simple_func.integral, 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, simp [hy] end lemma integral_map {β} [measurable_space β] {φ : α → β} (hφ : measurable φ) {f : β → E} (hfm : ae_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 ... = ∫ x, g (φ x) ∂μ : integral_map_of_measurable hφ hfm.measurable_mk ... = ∫ x, f (φ x) ∂μ : integral_congr_ae $ ae_eq_comp hφ (hfm.ae_eq_mk).symm lemma integral_map_of_closed_embedding {β} [topological_space α] [borel_space α] [topological_space β] [measurable_space β] [borel_space β] {φ : α → β} (hφ : closed_embedding φ) (f : β → E) : ∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ := begin by_cases hfm : ae_measurable f (measure.map φ μ), { exact integral_map hφ.continuous.measurable hfm }, { rw [integral_non_ae_measurable hfm, integral_non_ae_measurable], rwa ae_measurable_comp_right_iff_of_closed_embedding hφ } 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 $ ae_eq_dirac' hfm ... = f a : by simp [measure.dirac_apply_of_mem] lemma integral_dirac [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] end properties section group variables {G : Type*} [measurable_space G] [topological_space G] [group G] [has_continuous_mul G] [borel_space G] variables {μ : measure G} open measure /-- Translating a function by left-multiplication does not change its integral with respect to a left-invariant measure. -/ @[to_additive] lemma integral_mul_left_eq_self (hμ : is_mul_left_invariant μ) {f : G → E} (g : G) : ∫ x, f (g * x) ∂μ = ∫ x, f x ∂μ := begin have hgμ : measure.map (has_mul.mul g) μ = μ, { rw ← map_mul_left_eq_self at hμ, exact hμ g }, have h_mul : closed_embedding (λ x, g * x) := (homeomorph.mul_left g).closed_embedding, rw [← integral_map_of_closed_embedding h_mul, hgμ] end /-- Translating a function by right-multiplication does not change its integral with respect to a right-invariant measure. -/ @[to_additive] lemma integral_mul_right_eq_self (hμ : is_mul_right_invariant μ) {f : G → E} (g : G) : ∫ x, f (x * g) ∂μ = ∫ x, f x ∂μ := begin have hgμ : measure.map (λ x, x * g) μ = μ, { rw ← map_mul_right_eq_self at hμ, exact hμ g }, have h_mul : closed_embedding (λ x, x * g) := (homeomorph.mul_right g).closed_embedding, rw [← integral_map_of_closed_embedding h_mul, hgμ] end /-- If some left-translate of a function negates it, then the integral of the function with respect to a left-invariant measure is 0. -/ @[to_additive] lemma integral_zero_of_mul_left_eq_neg (hμ : is_mul_left_invariant μ) {f : G → E} {g : G} (hf' : ∀ x, f (g * x) = - f x) : ∫ x, f x ∂μ = 0 := begin refine eq_zero_of_eq_neg ℝ (eq.symm _), have : ∫ x, f (g * x) ∂μ = ∫ x, - f x ∂μ, { congr, ext x, exact hf' x }, convert integral_mul_left_eq_self hμ g using 1, rw [this, integral_neg] end /-- If some right-translate of a function negates it, then the integral of the function with respect to a right-invariant measure is 0. -/ @[to_additive] lemma integral_zero_of_mul_right_eq_neg (hμ : is_mul_right_invariant μ) {f : G → E} {g : G} (hf' : ∀ x, f (x * g) = - f x) : ∫ x, f x ∂μ = 0 := begin refine eq_zero_of_eq_neg ℝ (eq.symm _), have : ∫ x, f (x * g) ∂μ = ∫ x, - f x ∂μ, { congr, ext x, exact hf' x }, convert integral_mul_right_eq_self hμ g using 1, rw [this, integral_neg] end end group 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
73dd385b7cfd98dbb0a64ac984a6dd45efc200f0
4b846d8dabdc64e7ea03552bad8f7fa74763fc67
/tests/lean/run/pack_unpack1.lean
c5eb249d2dc8f4b7065a124f92e20a11191c9f10
[ "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
2,287
lean
inductive {u} tree_core (A : Type u) : bool → (Type u) | leaf' : A → tree_core ff | node' : tree_core tt → tree_core ff | nil' {} : tree_core tt | cons' : tree_core ff → tree_core tt → tree_core tt attribute [reducible] definition tree (A : Sort*) := tree_core A ff attribute [reducible] definition tree_list (A : Sort*) := tree_core A tt open tree_core definition pack {A : Sort*} : list (tree A) → tree_core A tt | [] := nil' | (a::l) := cons' a (pack l) definition unpack {A : Sort*} : ∀ {b}, tree_core A b → list (tree A) | .tt nil' := [] | .tt (cons' a t) := a :: unpack t | .ff (leaf' a) := [] | .ff (node' l) := [] attribute [inverse] lemma unpack_pack {A : Sort*} : ∀ (l : list (tree A)), unpack (pack l) = l | [] := rfl | (a::l) := show a :: unpack (pack l) = a :: l, from congr_arg (λ x, a :: x) (unpack_pack l) attribute [inverse] lemma pack_unpack {A : Sort*} : ∀ t : tree_core A tt, pack (unpack t) = t := λ t, @tree_core.rec_on A (λ b, bool.cases_on b (λ t, true) (λ t, pack (unpack t) = t)) tt t (λ a, trivial) (λ t ih, trivial) rfl (λ h t ih1 ih2, show cons' h (pack (unpack t)) = cons' h t, from congr_arg (λ x, cons' h x) ih2) attribute [pattern] definition tree.node {A : Sort*} (l : list (tree A)) : tree A := tree_core.node' (pack l) attribute [pattern] definition tree.leaf {A : Sort*} : A → tree A := tree_core.leaf' set_option trace.eqn_compiler true definition sz {A : Sort*} : tree A → nat | (tree.leaf a) := 1 | (tree.node l) := list.length l + 1 constant P {A : Sort*} : tree A → Type 1 constant mk1 {A : Sort*} (a : A) : P (tree.leaf a) constant mk2 {A : Sort*} (l : list (tree A)) : P (tree.node l) noncomputable definition bla {A : Sort*} : ∀ n : tree A, P n | (tree.leaf a) := mk1 a | (tree.node l) := mk2 l #check bla._main.equations._eqn_1 #check bla._main.equations._eqn_2 definition foo {A : Sort*} : nat → tree A → nat | 0 _ := 0 | (n+1) (tree.leaf a) := 0 | (n+1) (tree.node []) := foo n (tree.node []) | (n+1) (tree.node (x::xs)) := foo n x #check @foo._main.equations._eqn_1 #check @foo._main.equations._eqn_2 #check @foo._main.equations._eqn_3 #check @foo._main.equations._eqn_4
ee627f51438300305678be34bc2224b48bd9cb94
2a2864136cf8f2871e86f5fd14e624e3daa8fd24
/Monads.lean
7e2567d84714b765e52707bcfb0eef4375202f5c
[ "MIT" ]
permissive
hargoniX/lean-monads
d054ac71a351b7c86f318a477977cc166117b8ec
2e87ca7ddf394641ea1b16bcbd8c384026d68e2f
refs/heads/main
1,693,530,528,286
1,633,100,386,000
1,633,100,386,000
412,509,769
1
0
null
null
null
null
UTF-8
Lean
false
false
107
lean
import Monads.List import Monads.Maybe import Monads.Functor import Monads.Applicative import Monads.Monad
bbc5be5ec17eef03f4d5e89766946fc5a59b2a36
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/von_neumann_algebra/basic.lean
dc3ec7acc525004dac339e25726244de492ec30d
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
5,417
lean
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import analysis.normed_space.dual import analysis.normed_space.star.basic import analysis.complex.basic import analysis.inner_product_space.adjoint import algebra.star.subalgebra /-! # Von Neumann algebras > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We give the "abstract" and "concrete" definitions of a von Neumann algebra. We still have a major project ahead of us to show the equivalence between these definitions! An abstract von Neumann algebra `wstar_algebra M` is a C^* algebra with a Banach space predual, per Sakai (1971). A concrete von Neumann algebra `von_neumann_algebra H` (where `H` is a Hilbert space) is a *-closed subalgebra of bounded operators on `H` which is equal to its double commutant. We'll also need to prove the von Neumann double commutant theorem, that the concrete definition is equivalent to a *-closed subalgebra which is weakly closed. -/ universes u v /-- Sakai's definition of a von Neumann algebra as a C^* algebra with a Banach space predual. So that we can unambiguously talk about these "abstract" von Neumann algebras in parallel with the "concrete" ones (weakly closed *-subalgebras of B(H)), we name this definition `wstar_algebra`. Note that for now we only assert the mere existence of predual, rather than picking one. This may later prove problematic, and need to be revisited. Picking one may cause problems with definitional unification of different instances. One the other hand, not picking one means that the weak-* topology (which depends on a choice of predual) must be defined using the choice, and we may be unhappy with the resulting opaqueness of the definition. -/ class wstar_algebra (M : Type u) [normed_ring M] [star_ring M] [cstar_ring M] [module ℂ M] [normed_algebra ℂ M] [star_module ℂ M] := (exists_predual : ∃ (X : Type u) [normed_add_comm_group X] [normed_space ℂ X] [complete_space X], nonempty (normed_space.dual ℂ X ≃ₗᵢ⋆[ℂ] M)) -- TODO: Without this, `von_neumann_algebra` times out. Why? set_option old_structure_cmd true /-- The double commutant definition of a von Neumann algebra, as a *-closed subalgebra of bounded operators on a Hilbert space, which is equal to its double commutant. Note that this definition is parameterised by the Hilbert space on which the algebra faithfully acts, as is standard in the literature. See `wstar_algebra` for the abstract notion (a C^*-algebra with Banach space predual). Note this is a bundled structure, parameterised by the Hilbert space `H`, rather than a typeclass on the type of elements. Thus we can't say that the bounded operators `H →L[ℂ] H` form a `von_neumann_algebra` (although we will later construct the instance `wstar_algebra (H →L[ℂ] H)`), and instead will use `⊤ : von_neumann_algebra H`. -/ @[nolint has_nonempty_instance] structure von_neumann_algebra (H : Type u) [normed_add_comm_group H] [inner_product_space ℂ H] [complete_space H] extends star_subalgebra ℂ (H →L[ℂ] H) := (centralizer_centralizer' : set.centralizer (set.centralizer carrier) = carrier) /-- Consider a von Neumann algebra acting on a Hilbert space `H` as a *-subalgebra of `H →L[ℂ] H`. (That is, we forget that it is equal to its double commutant or equivalently that it is closed in the weak and strong operator topologies.) -/ add_decl_doc von_neumann_algebra.to_star_subalgebra namespace von_neumann_algebra variables {H : Type u} [normed_add_comm_group H] [inner_product_space ℂ H] [complete_space H] instance : set_like (von_neumann_algebra H) (H →L[ℂ] H) := ⟨von_neumann_algebra.carrier, λ S T h, by cases S; cases T; congr'⟩ instance : star_mem_class (von_neumann_algebra H) (H →L[ℂ] H) := { star_mem := λ s a, s.star_mem' } instance : subring_class (von_neumann_algebra H) (H →L[ℂ] H) := { add_mem := add_mem', mul_mem := mul_mem', one_mem := one_mem', zero_mem := zero_mem' , neg_mem := λ s a ha, show -a ∈ s.to_star_subalgebra, from neg_mem ha } @[simp] lemma mem_carrier {S : von_neumann_algebra H} {x : H →L[ℂ] H}: x ∈ S.carrier ↔ x ∈ (S : set (H →L[ℂ] H)) := iff.rfl @[ext] theorem ext {S T : von_neumann_algebra H} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h @[simp] lemma centralizer_centralizer (S : von_neumann_algebra H) : set.centralizer (set.centralizer (S : set (H →L[ℂ] H))) = S := S.centralizer_centralizer' /-- The centralizer of a `von_neumann_algebra`, as a `von_neumann_algebra`.-/ def commutant (S : von_neumann_algebra H) : von_neumann_algebra H := { carrier := set.centralizer (S : set (H →L[ℂ] H)), centralizer_centralizer' := by rw S.centralizer_centralizer, .. star_subalgebra.centralizer ℂ (S : set (H →L[ℂ] H)) (λ a (ha : a ∈ S), (star_mem ha : _)) } @[simp] lemma coe_commutant (S : von_neumann_algebra H) : ↑S.commutant = set.centralizer (S : set (H →L[ℂ] H)) := rfl @[simp] lemma mem_commutant_iff {S : von_neumann_algebra H} {z : H →L[ℂ] H} : z ∈ S.commutant ↔ ∀ g ∈ S, g * z = z * g := iff.rfl @[simp] lemma commutant_commutant (S : von_neumann_algebra H) : S.commutant.commutant = S := set_like.coe_injective S.centralizer_centralizer' end von_neumann_algebra
f490a8fd481a246d75fb11df37d7ef0a78082cb7
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/03_Propositions_and_Proofs.org.35.lean
cdde25dbf54a1cf599b477742e9504364e44e35d
[]
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
166
lean
/- page 43 -/ import standard open classical variable p : Prop -- BEGIN example (H : ¬¬p) : p := by_contradiction (assume H1 : ¬p, show false, from H H1) -- END
727d9e3c0f36bfa89049423ad0855fed8ef15841
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/data/bitvec/basic.lean
0827fb5caaa45e01bd8c6a1900b838ba263b69a1
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
3,677
lean
/- Copyright (c) 2020 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author(s): Simon Hudon -/ import data.bitvec import data.fin import tactic.norm_num import tactic.monotonicity namespace bitvec instance (n : ℕ) : preorder (bitvec n) := preorder.lift bitvec.to_nat /-- convert `fin` to `bitvec` -/ def of_fin {n : ℕ} (i : fin $ 2^n) : bitvec n := bitvec.of_nat _ i.val lemma of_fin_val {n : ℕ} (i : fin $ 2^n) : (of_fin i).to_nat = i.val := by rw [of_fin,to_nat_of_nat,nat.mod_eq_of_lt]; apply i.is_lt /-- convert `bitvec` to `fin` -/ def to_fin {n : ℕ} (i : bitvec n) : fin $ 2^n := @fin.of_nat' _ (nat.pow_pos (by norm_num) _) i.to_nat lemma add_lsb_eq_twice_add_one {x b} : add_lsb x b = 2 * x + cond b 1 0 := by simp [add_lsb,two_mul] lemma to_nat_eq_foldr_reverse {n : ℕ} (v : bitvec n) : v.to_nat = v.to_list.reverse.foldr (flip add_lsb) 0 := by rw [list.foldr_reverse, flip]; refl lemma to_nat_lt {n : ℕ} (v : bitvec n) : v.to_nat < 2^n := begin suffices : v.to_nat + 1 ≤ 2 ^ n, { simpa }, rw to_nat_eq_foldr_reverse, cases v with xs h, dsimp [bitvec.to_nat,bits_to_nat], rw ← list.length_reverse at h, generalize_hyp : xs.reverse = ys at ⊢ h, clear xs, induction ys generalizing n, { simp [← h] }, { simp only [←h, nat.pow_add, flip, list.length, list.foldr, nat.pow_one], rw [add_lsb_eq_twice_add_one], transitivity 2 * list.foldr (λ (x : bool) (y : ℕ), add_lsb y x) 0 ys_tl + 2 * 1, { ac_mono, rw two_mul, mono, cases ys_hd; simp }, { rw ← left_distrib, ac_mono, norm_num, apply ys_ih, refl } }, end lemma add_lsb_div_two {x b} : add_lsb x b / 2 = x := by cases b; simp only [nat.add_mul_div_left, add_lsb, ←two_mul, add_comm, nat.succ_pos', nat.mul_div_right, gt_iff_lt, zero_add, cond]; norm_num lemma to_bool_add_lsb_mod_two {x b} : to_bool (add_lsb x b % 2 = 1) = b := by cases b; simp only [to_bool_iff, nat.add_mul_mod_self_left, add_lsb, ←two_mul, add_comm, bool.to_bool_false, nat.mul_mod_right, zero_add, cond, zero_ne_one]; norm_num lemma of_nat_to_nat {n : ℕ} (v : bitvec n) : bitvec.of_nat _ v.to_nat = v := begin cases v with xs h, ext1, change vector.to_list _ = xs, dsimp [bitvec.to_nat,bits_to_nat], rw ← list.length_reverse at h, rw [← list.reverse_reverse xs,list.foldl_reverse], generalize_hyp : xs.reverse = ys at ⊢ h, clear xs, induction ys generalizing n, { cases h, simp [bitvec.of_nat] }, { simp only [←nat.succ_eq_add_one, list.length] at h, cases h, simp only [bitvec.of_nat, vector.to_list_cons, vector.to_list_nil, list.reverse_cons, vector.to_list_append, list.foldr], erw [add_lsb_div_two,to_bool_add_lsb_mod_two], congr, apply ys_ih, refl } end lemma to_fin_val {n : ℕ} (v : bitvec n) : (to_fin v : ℕ) = v.to_nat := by rw [to_fin, fin.coe_of_nat_eq_mod', nat.mod_eq_of_lt]; apply to_nat_lt lemma to_fin_le_to_fin_of_le {n} {v₀ v₁ : bitvec n} (h : v₀ ≤ v₁) : v₀.to_fin ≤ v₁.to_fin := show (v₀.to_fin : ℕ) ≤ v₁.to_fin, by rw [to_fin_val,to_fin_val]; exact h lemma of_fin_le_of_fin_of_le {n : ℕ} {i j : fin (2^n)} (h : i ≤ j) : of_fin i ≤ of_fin j := show (bitvec.of_nat n i).to_nat ≤ (bitvec.of_nat n j).to_nat, by { simp only [to_nat_of_nat, nat.mod_eq_of_lt, fin.is_lt], exact h } lemma to_fin_of_fin {n} (i : fin $ 2^n) : (of_fin i).to_fin = i := fin.eq_of_veq (by simp [to_fin_val, of_fin, to_nat_of_nat, nat.mod_eq_of_lt, i.is_lt]) lemma of_fin_to_fin {n} (v : bitvec n) : of_fin (to_fin v) = v := by dsimp [of_fin]; rw [to_fin_val, of_nat_to_nat] end bitvec
a0e91d2e0d9b260c96a5d7c75d87e89135d094b3
4727251e0cd73359b15b664c3170e5d754078599
/src/combinatorics/set_family/lym.lean
524adeccc4f93c3b6d37231bc7583fc15d7c186f
[ "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
9,319
lean
/- Copyright (c) 2022 Bhavik Mehta, Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Alena Gusakov, Yaël Dillies -/ import algebra.big_operators.ring import combinatorics.double_counting import combinatorics.set_family.shadow import data.rat.order import tactic.linarith /-! # Lubell-Yamamoto-Meshalkin inequality and Sperner's theorem This file proves the local LYM and LYM inequalities as well as Sperner's theorem. ## Main declarations * `finset.card_div_choose_le_card_shadow_div_choose`: Local Lubell-Yamamoto-Meshalkin inequality. The shadow of a set `𝒜` in a layer takes a greater proportion of its layer than `𝒜` does. * `finset.sum_card_slice_div_choose_le_one`: Lubell-Yamamoto-Meshalkin inequality. The sum of densities of `𝒜` in each layer is at most `1` for any antichain `𝒜`. * `is_antichain.sperner`: Sperner's theorem. The size of any antichain in `finset α` is at most the size of the maximal layer of `finset α`. It is a corollary of `sum_card_slice_div_choose_le_one`. ## TODO Prove upward local LYM. Provide equality cases. Local LYM gives that the equality case of LYM and Sperner is precisely when `𝒜` is a middle layer. `falling` could be useful more generally in grade orders. ## References * http://b-mehta.github.io/maths-notes/iii/mich/combinatorics.pdf * http://discretemath.imp.fu-berlin.de/DMII-2015-16/kruskal.pdf ## Tags shadow, lym, slice, sperner, antichain -/ open finset nat open_locale big_operators finset_family variables {𝕜 α : Type*} [linear_ordered_field 𝕜] namespace finset /-! ### Local LYM inequality -/ section local_lym variables [decidable_eq α] [fintype α] {𝒜 : finset (finset α)} {r : ℕ} /-- The downward **local LYM inequality**, with cancelled denominators. `𝒜` takes up less of `α^(r)` (the finsets of card `r`) than `∂𝒜` takes up of `α^(r - 1)`. -/ lemma card_mul_le_card_shadow_mul (h𝒜 : (𝒜 : set (finset α)).sized r) : 𝒜.card * r ≤ (∂𝒜).card * (fintype.card α - r + 1) := begin refine card_mul_le_card_mul' (⊆) (λ s hs, _) (λ s hs, _), { rw [←h𝒜 hs, ←card_image_of_inj_on s.erase_inj_on], refine card_le_of_subset _, simp_rw [image_subset_iff, mem_bipartite_below], exact λ a ha, ⟨erase_mem_shadow hs ha, erase_subset _ _⟩ }, refine le_trans _ tsub_tsub_le_tsub_add, rw [←h𝒜.shadow hs, ←card_compl, ←card_image_of_inj_on (insert_inj_on' _)], refine card_le_of_subset (λ t ht, _), apply_instance, rw mem_bipartite_above at ht, have : ∅ ∉ 𝒜, { rw [←mem_coe, h𝒜.empty_mem_iff, coe_eq_singleton], rintro rfl, rwa shadow_singleton_empty at hs }, obtain ⟨a, ha, rfl⟩ := exists_eq_insert_iff.2 ⟨ht.2, by rw [(sized_shadow_iff this).1 h𝒜.shadow ht.1, h𝒜.shadow hs]⟩, exact mem_image_of_mem _ (mem_compl.2 ha), end /-- The downward **local LYM inequality**. `𝒜` takes up less of `α^(r)` (the finsets of card `r`) than `∂𝒜` takes up of `α^(r - 1)`. -/ lemma card_div_choose_le_card_shadow_div_choose (hr : r ≠ 0) (h𝒜 : (𝒜 : set (finset α)).sized r) : (𝒜.card : 𝕜) / (fintype.card α).choose r ≤ (∂𝒜).card / (fintype.card α).choose (r - 1) := begin obtain hr' | hr' := lt_or_le (fintype.card α) r, { rw [choose_eq_zero_of_lt hr', cast_zero, div_zero], exact div_nonneg (cast_nonneg _) (cast_nonneg _) }, replace h𝒜 := card_mul_le_card_shadow_mul h𝒜, rw div_le_div_iff; norm_cast, { cases r, { exact (hr rfl).elim }, rw nat.succ_eq_add_one at *, rw [tsub_add_eq_add_tsub hr', add_tsub_add_eq_tsub_right] at h𝒜, apply le_of_mul_le_mul_right _ (pos_iff_ne_zero.2 hr), convert nat.mul_le_mul_right ((fintype.card α).choose r) h𝒜 using 1, { simp [mul_assoc, nat.choose_succ_right_eq], exact or.inl (mul_comm _ _) }, { simp only [mul_assoc, choose_succ_right_eq, mul_eq_mul_left_iff], exact or.inl (mul_comm _ _) } }, { exact nat.choose_pos hr' }, { exact nat.choose_pos (r.pred_le.trans hr') } end end local_lym /-! ### LYM inequality -/ section lym section falling variables [decidable_eq α] (k : ℕ) (𝒜 : finset (finset α)) /-- `falling k 𝒜` is all the finsets of cardinality `k` which are a subset of something in `𝒜`. -/ def falling : finset (finset α) := 𝒜.sup $ powerset_len k variables {𝒜 k} {s : finset α} lemma mem_falling : s ∈ falling k 𝒜 ↔ (∃ t ∈ 𝒜, s ⊆ t) ∧ s.card = k := by simp_rw [falling, mem_sup, mem_powerset_len, exists_and_distrib_right] variables (𝒜 k) lemma sized_falling : (falling k 𝒜 : set (finset α)).sized k := λ s hs, (mem_falling.1 hs).2 lemma slice_subset_falling : 𝒜 # k ⊆ falling k 𝒜 := λ s hs, mem_falling.2 $ (mem_slice.1 hs).imp_left $ λ h, ⟨s, h, subset.refl _⟩ lemma falling_zero_subset : falling 0 𝒜 ⊆ {∅} := subset_singleton_iff'.2 $ λ t ht, card_eq_zero.1 $ sized_falling _ _ ht lemma slice_union_shadow_falling_succ : 𝒜 # k ∪ ∂ (falling (k + 1) 𝒜) = falling k 𝒜 := begin ext s, simp_rw [mem_union, mem_slice, mem_shadow_iff, exists_prop, mem_falling], split, { rintro (h | ⟨s, ⟨⟨t, ht, hst⟩, hs⟩, a, ha, rfl⟩), { exact ⟨⟨s, h.1, subset.refl _⟩, h.2⟩ }, refine ⟨⟨t, ht, (erase_subset _ _).trans hst⟩, _⟩, rw [card_erase_of_mem ha, hs], refl }, { rintro ⟨⟨t, ht, hst⟩, hs⟩, by_cases s ∈ 𝒜, { exact or.inl ⟨h, hs⟩ }, obtain ⟨a, ha, hst⟩ := ssubset_iff_exists_insert_subset.1 (ssubset_of_subset_of_ne hst (ht.ne_of_not_mem h).symm), refine or.inr ⟨insert a s, ⟨⟨t, ht, hst⟩, _⟩, a, mem_insert_self _ _, erase_insert ha⟩, rw [card_insert_of_not_mem ha, hs] } end variables {𝒜 k} /-- The shadow of `falling m 𝒜` is disjoint from the `n`-sized elements of `𝒜`, thanks to the antichain property. -/ lemma _root_.is_antichain.disjoint_slice_shadow_falling {m n : ℕ} (h𝒜 : is_antichain (⊆) (𝒜 : set (finset α))) : disjoint (𝒜 # m) (∂ (falling n 𝒜)) := disjoint_right.2 $ λ s h₁ h₂, begin simp_rw [mem_shadow_iff, exists_prop, mem_falling] at h₁, obtain ⟨s, ⟨⟨t, ht, hst⟩, hs⟩, a, ha, rfl⟩ := h₁, refine h𝒜 (slice_subset h₂) ht _ ((erase_subset _ _).trans hst), rintro rfl, exact not_mem_erase _ _ (hst ha), end /-- A bound on any top part of the sum in LYM in terms of the size of `falling k 𝒜`. -/ lemma le_card_falling_div_choose [fintype α] (hk : k ≤ fintype.card α) (h𝒜 : is_antichain (⊆) (𝒜 : set (finset α))) : ∑ r in range (k + 1), ((𝒜 # (fintype.card α - r)).card : 𝕜) / (fintype.card α).choose (fintype.card α - r) ≤ (falling (fintype.card α - k) 𝒜).card / (fintype.card α).choose (fintype.card α - k) := begin induction k with k ih, { simp only [tsub_zero, cast_one, cast_le, sum_singleton, div_one, choose_self, range_one], exact card_le_of_subset (slice_subset_falling _ _) }, rw succ_eq_add_one at *, rw [sum_range_succ, ←slice_union_shadow_falling_succ, card_disjoint_union h𝒜.disjoint_slice_shadow_falling, cast_add, _root_.add_div, add_comm], rw [←tsub_tsub, tsub_add_cancel_of_le (le_tsub_of_add_le_left hk)], exact add_le_add_left ((ih $ le_of_succ_le hk).trans $ card_div_choose_le_card_shadow_div_choose (tsub_pos_iff_lt.2 $ nat.succ_le_iff.1 hk).ne' $ sized_falling _ _) _, end end falling variables {𝒜 : finset (finset α)} {s : finset α} {k : ℕ} /-- The **Lubell-Yamamoto-Meshalkin inequality**. If `𝒜` is an antichain, then the sum of the proportion of elements it takes from each layer is less than `1`. -/ lemma sum_card_slice_div_choose_le_one [fintype α] (h𝒜 : is_antichain (⊆) (𝒜 : set (finset α))) : ∑ r in range (fintype.card α + 1), ((𝒜 # r).card : 𝕜) / (fintype.card α).choose r ≤ 1 := begin classical, rw ←sum_flip, refine (le_card_falling_div_choose le_rfl h𝒜).trans _, rw div_le_iff; norm_cast, { simpa only [nat.sub_self, one_mul, nat.choose_zero_right, falling] using (sized_falling 0 𝒜).card_le }, { rw [tsub_self, choose_zero_right], exact zero_lt_one } end end lym /-! ### Sperner's theorem -/ /-- **Sperner's theorem**. The size of an antichain in `finset α` is bounded by the size of the maximal layer in `finset α`. This precisely means that `finset α` is a Sperner order. -/ lemma _root_.is_antichain.sperner [fintype α] {𝒜 : finset (finset α)} (h𝒜 : is_antichain (⊆) (𝒜 : set (finset α))) : 𝒜.card ≤ (fintype.card α).choose (fintype.card α / 2) := begin classical, suffices : ∑ r in Iic (fintype.card α), ((𝒜 # r).card : ℚ) / (fintype.card α).choose (fintype.card α / 2) ≤ 1, { rwa [←sum_div, ←nat.cast_sum, div_le_one, cast_le, sum_card_slice] at this, norm_cast, exact choose_pos (nat.div_le_self _ _) }, rw [Iic, ←Ico_succ_right, bot_eq_zero, Ico_zero_eq_range], refine (sum_le_sum $ λ r hr, _).trans (sum_card_slice_div_choose_le_one h𝒜), rw mem_range at hr, refine div_le_div_of_le_left _ _ _; norm_cast, { exact nat.zero_le _ }, { exact choose_pos (lt_succ_iff.1 hr) }, { exact choose_le_middle _ _ } end end finset
13516935543983922986ec03f4ae7caac7dbf5ad
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/sheaves/sheaf.lean
a2ca6ff6b43c3ae7bff004376881b989b601cd86
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
3,931
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 topology.sheaves.sheaf_condition.equalizer_products import category_theory.full_subcategory import category_theory.limits.punit /-! # Sheaves We define sheaves on a topological space, with values in an arbitrary category with products. The sheaf condition for a `F : presheaf C X` requires that the morphism `F.obj U ⟶ ∏ F.obj (U i)` (where `U` is some open set which is the union of the `U i`) is the equalizer of the two morphisms `∏ F.obj (U i) ⟶ ∏ F.obj (U i ⊓ U j)`. We provide the instance `category (sheaf C X)` as the full subcategory of presheaves, and the fully faithful functor `sheaf.forget : sheaf C X ⥤ presheaf C X`. ## Equivalent conditions While the "official" definition is in terms of an equalizer diagram, in `src/topology/sheaves/sheaf_condition/pairwise_intersections.lean` and in `src/topology/sheaves/sheaf_condition/open_le_cover.lean` we provide two equivalent conditions (and prove they are equivalent). The first is that `F.obj U` is the limit point of the diagram consisting of all the `F.obj (U i)` and `F.obj (U i ⊓ U j)`. (That is, we explode the equalizer of two products out into its component pieces.) The second is that `F.obj U` is the limit point of the diagram constisting of all the `F.obj V`, for those `V : opens X` such that `V ≤ U i` for some `i`. (This condition is particularly easy to state, and perhaps should become the "official" definition.) -/ universes v u noncomputable theory open category_theory open category_theory.limits open topological_space open opposite open topological_space.opens namespace Top variables {C : Type u} [category.{v} C] [has_products C] variables {X : Top.{v}} (F : presheaf C X) {ι : Type v} (U : ι → opens X) namespace presheaf open sheaf_condition_equalizer_products /-- The sheaf condition for a `F : presheaf C X` requires that the morphism `F.obj U ⟶ ∏ F.obj (U i)` (where `U` is some open set which is the union of the `U i`) is the equalizer of the two morphisms `∏ F.obj (U i) ⟶ ∏ F.obj (U i) ⊓ (U j)`. -/ def is_sheaf (F : presheaf C X) : Prop := ∀ ⦃ι : Type v⦄ (U : ι → opens X), nonempty (is_limit (sheaf_condition_equalizer_products.fork F U)) /-- The presheaf valued in `punit` over any topological space is a sheaf. -/ lemma is_sheaf_punit (F : presheaf (category_theory.discrete punit) X) : F.is_sheaf := λ ι U, ⟨punit_cone_is_limit⟩ /-- Transfer the sheaf condition across an isomorphism of presheaves. -/ lemma is_sheaf_of_iso {F G : presheaf C X} (α : F ≅ G) (h : F.is_sheaf) : G.is_sheaf := λ ι U, ⟨is_limit.of_iso_limit ((is_limit.postcompose_inv_equiv _ _).symm (h U).some) (sheaf_condition_equalizer_products.fork.iso_of_iso U α.symm).symm⟩ lemma is_sheaf_iso_iff {F G : presheaf C X} (α : F ≅ G) : F.is_sheaf ↔ G.is_sheaf := ⟨(λ h, is_sheaf_of_iso α h), (λ h, is_sheaf_of_iso α.symm h)⟩ end presheaf variables (C X) /-- A `sheaf C X` is a presheaf of objects from `C` over a (bundled) topological space `X`, satisfying the sheaf condition. -/ @[derive category] def sheaf : Type (max u v) := { F : presheaf C X // F.is_sheaf } -- Let's construct a trivial example, to keep the inhabited linter happy. instance sheaf_inhabited : inhabited (sheaf (category_theory.discrete punit) X) := ⟨⟨functor.star _, presheaf.is_sheaf_punit _⟩⟩ namespace sheaf /-- The forgetful functor from sheaves to presheaves. -/ @[derive [full, faithful]] def forget : Top.sheaf C X ⥤ Top.presheaf C X := full_subcategory_inclusion presheaf.is_sheaf @[simp] lemma id_app (F : sheaf C X) (t) : (𝟙 F : F ⟶ F).app t = 𝟙 _ := rfl @[simp] lemma comp_app {F G H : sheaf C X} (f : F ⟶ G) (g : G ⟶ H) (t) : (f ≫ g).app t = f.app t ≫ g.app t := rfl end sheaf end Top
0f176bd7f3c13d9aacc52a21ce88a631dc80f21e
0dbd5f7001f62ee8d54ed48bada66bfeaf55e550
/src/ent/order.lean
4501c82dcebca6ccaf3cf3f45fadd834e0c61950
[]
no_license
rwbarton/lean-elementary-number-theory
667203b08501792eef48217759539f6c1e2da25a
fabef0737fd2486e3f24f9e04652db4c182d5425
refs/heads/master
1,670,605,651,029
1,599,565,470,000
1,599,565,470,000
293,792,043
2
0
null
null
null
null
UTF-8
Lean
false
false
2,971
lean
import data.nat.prime import data.pnat import data.nat.exactly_divides open nat namespace order section raw_order variables (p : ℕ) (hp : p > 1) protected def order_core : Π (n : ℕ), n > 0 → {r // p^r ∣∣ n} | 0 := λ n_pos, absurd n_pos dec_trivial | n@(k+1) := λ n_pos, have n / p < n, from div_lt_self n_pos hp, if h : p ∣ n then have p * (n / p) = n, from nat.mul_div_cancel' h, let ⟨s, hs⟩ := order_core (n / p) (pos_of_mul_pos_left (this.symm ▸ n_pos : 0 < p * (n / p)) dec_trivial) in ⟨succ s, this ▸ (exactly_divides_succ (hp.trans dec_trivial)).mp hs⟩ else ⟨0, (exactly_divides_zero (hp.trans dec_trivial)).mp h⟩ def order (n : ℕ) (n_pos : n > 0) : ℕ := (order.order_core p hp n n_pos).val lemma exactly_divides_order (n : ℕ) (n_pos : n > 0) : p^(order p hp n n_pos) ∣∣ n := (order.order_core p hp n n_pos).property end raw_order /- XXX comments -/ instance : has_dvd ℕ+ := ⟨λ a b, a.val ∣ b.val⟩ def 𝓟 := {p : ℕ // prime p} notation `PP` := 𝓟 def 𝓟.gt_one (p : 𝓟) : p.val > 1 := p.property.gt_one def 𝓟.pos (p : 𝓟) : p.val > 0 := p.property.pos instance : has_coe 𝓟 ℕ+ := ⟨λ p, ⟨p.val, p.pos⟩⟩ def ord (p : 𝓟) (n : ℕ+) : ℕ := order p p.gt_one n n.property def exactly_divides_ord {p : 𝓟} {n : ℕ+} : p^(ord p n) ∣∣ n := exactly_divides_order p p.gt_one n n.property def exactly_divides_iff_ord {p : 𝓟} {r : ℕ} {n : ℕ+} : ord p n = r ↔ p^r ∣∣ n := iff.intro (λ e, e ▸ exactly_divides_ord) (exactly_divides_unique exactly_divides_ord) variable {p : 𝓟} -- Recursion (though we prove them in a round-about fashion) lemma ord_not_div {n : ℕ+} : ¬(↑p ∣ n) ↔ ord p n = 0 := (exactly_divides_zero p.pos).trans exactly_divides_iff_ord.symm lemma ord_div {n : ℕ+} : ord p (p * n) = succ (ord p n) := exactly_divides_iff_ord.mpr ((exactly_divides_succ p.pos).mp exactly_divides_ord) -- Multiplicative lemma ord_one : ord p 1 = 0 := exactly_divides_iff_ord.mpr (exactly_divides_one p.property) lemma ord_mul (a b : ℕ+) : ord p (a * b) = ord p a + ord p b := exactly_divides_iff_ord.mpr (exactly_divides_mul p.property exactly_divides_ord exactly_divides_ord) lemma ord_ppow {k : ℕ} {a : ℕ+} : ord p (pnat.pow a k) = k * ord p a := exactly_divides_iff_ord.mpr (exactly_divides_pow p.property exactly_divides_ord) lemma ord_pow {k : ℕ} {a : ℕ+} : ord p (a^k) = k * ord p a := have pnat.pow a k = a^k, from (pnat.coe_nat_coe _).symm, this ▸ ord_ppow -- Gcd def pgcd (a b : ℕ+) : ℕ+ := ⟨gcd a b, gcd_pos_of_pos_left b a.pos⟩ lemma ord_pgcd {a b : ℕ+} : ord p (pgcd a b) = min (ord p a) (ord p b) := exactly_divides_iff_ord.mpr (exactly_divides_gcd exactly_divides_ord exactly_divides_ord) lemma ord_gcd {a b : ℕ+} : ord p (gcd a b) = min (ord p a) (ord p b) := have pgcd a b = gcd a b, from (pnat.coe_nat_coe _).symm, this ▸ ord_pgcd end order
5b37e0dc19b7115e4a7a2d9a1150c064804f1905
97f752b44fd85ec3f635078a2dd125ddae7a82b6
/hott/types/nat/hott.hlean
d23f5fda720fe7acb4189021840d589537f78962
[ "Apache-2.0" ]
permissive
tectronics/lean
ab977ba6be0fcd46047ddbb3c8e16e7c26710701
f38af35e0616f89c6e9d7e3eb1d48e47ee666efe
refs/heads/master
1,532,358,526,384
1,456,276,623,000
1,456,276,623,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,734
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Theorems about the natural numbers specific to HoTT -/ import .order types.pointed open is_trunc unit empty eq equiv algebra pointed namespace nat definition is_prop_le [instance] (n m : ℕ) : is_prop (n ≤ m) := begin assert lem : Π{n m : ℕ} (p : n ≤ m) (q : n = m), p = q ▸ le.refl n, { intros, cases p, { assert H' : q = idp, apply is_set.elim, cases H', reflexivity}, { cases q, exfalso, apply not_succ_le_self a}}, apply is_prop.mk, intro H1 H2, induction H2, { apply lem}, { cases H1, { exfalso, apply not_succ_le_self a}, { exact ap le.step !v_0}}, end definition is_prop_lt [instance] (n m : ℕ) : is_prop (n < m) := !is_prop_le definition le_equiv_succ_le_succ (n m : ℕ) : (n ≤ m) ≃ (succ n ≤ succ m) := equiv_of_is_prop succ_le_succ le_of_succ_le_succ definition le_succ_equiv_pred_le (n m : ℕ) : (n ≤ succ m) ≃ (pred n ≤ m) := equiv_of_is_prop pred_le_pred le_succ_of_pred_le theorem lt_by_cases_lt {a b : ℕ} {P : Type} (H1 : a < b → P) (H2 : a = b → P) (H3 : a > b → P) (H : a < b) : lt.by_cases H1 H2 H3 = H1 H := begin unfold lt.by_cases, induction (lt.trichotomy a b) with H' H', { esimp, exact ap H1 !is_prop.elim}, { exfalso, cases H' with H' H', apply lt.irrefl, exact H' ▸ H, exact lt.asymm H H'} end theorem lt_by_cases_eq {a b : ℕ} {P : Type} (H1 : a < b → P) (H2 : a = b → P) (H3 : a > b → P) (H : a = b) : lt.by_cases H1 H2 H3 = H2 H := begin unfold lt.by_cases, induction (lt.trichotomy a b) with H' H', { exfalso, apply lt.irrefl, exact H ▸ H'}, { cases H' with H' H', esimp, exact ap H2 !is_prop.elim, exfalso, apply lt.irrefl, exact H ▸ H'} end theorem lt_by_cases_ge {a b : ℕ} {P : Type} (H1 : a < b → P) (H2 : a = b → P) (H3 : a > b → P) (H : a > b) : lt.by_cases H1 H2 H3 = H3 H := begin unfold lt.by_cases, induction (lt.trichotomy a b) with H' H', { exfalso, exact lt.asymm H H'}, { cases H' with H' H', exfalso, apply lt.irrefl, exact H' ▸ H, esimp, exact ap H3 !is_prop.elim} end theorem lt_ge_by_cases_lt {n m : ℕ} {P : Type} (H1 : n < m → P) (H2 : n ≥ m → P) (H : n < m) : lt_ge_by_cases H1 H2 = H1 H := by apply lt_by_cases_lt theorem lt_ge_by_cases_ge {n m : ℕ} {P : Type} (H1 : n < m → P) (H2 : n ≥ m → P) (H : n ≥ m) : lt_ge_by_cases H1 H2 = H2 H := begin unfold [lt_ge_by_cases,lt.by_cases], induction (lt.trichotomy n m) with H' H', { exfalso, apply lt.irrefl, exact lt_of_le_of_lt H H'}, { cases H' with H' H'; all_goals (esimp; apply ap H2 !is_prop.elim)} end theorem lt_ge_by_cases_le {n m : ℕ} {P : Type} {H1 : n ≤ m → P} {H2 : n ≥ m → P} (H : n ≤ m) (Heq : Π(p : n = m), H1 (le_of_eq p) = H2 (le_of_eq p⁻¹)) : lt_ge_by_cases (λH', H1 (le_of_lt H')) H2 = H1 H := begin unfold [lt_ge_by_cases,lt.by_cases], induction (lt.trichotomy n m) with H' H', { esimp, apply ap H1 !is_prop.elim}, { cases H' with H' H', { esimp, induction H', esimp, symmetry, exact ap H1 !is_prop.elim ⬝ Heq idp ⬝ ap H2 !is_prop.elim}, { exfalso, apply lt.irrefl, apply lt_of_le_of_lt H H'}} end protected definition code [reducible] [unfold 1 2] : ℕ → ℕ → Type₀ | code 0 0 := unit | code 0 (succ m) := empty | code (succ n) 0 := empty | code (succ n) (succ m) := code n m protected definition refl : Πn, nat.code n n | refl 0 := star | refl (succ n) := refl n protected definition encode [unfold 3] {n m : ℕ} (p : n = m) : nat.code n m := p ▸ nat.refl n protected definition decode : Π(n m : ℕ), nat.code n m → n = m | decode 0 0 := λc, idp | decode 0 (succ l) := λc, empty.elim c _ | decode (succ k) 0 := λc, empty.elim c _ | decode (succ k) (succ l) := λc, ap succ (decode k l c) definition nat_eq_equiv (n m : ℕ) : (n = m) ≃ nat.code n m := equiv.MK nat.encode !nat.decode begin revert m, induction n, all_goals (intro m;induction m;all_goals intro c), all_goals try contradiction, induction c, reflexivity, xrewrite [↑nat.decode,-tr_compose,v_0], end begin intro p, induction p, esimp, induction n, reflexivity, rewrite [↑nat.decode,↑nat.refl,v_0] end definition pointed_nat [instance] [constructor] : pointed ℕ := pointed.mk 0 end nat
0284b0c2ea1e31e7ad3017e45a01388c9cdec2c2
a959f48a0621edea632487cf2130bbf70d301e05
/src/indexed_product.lean
0db78979d0235c8e7682145c8f99b5ea67b2dfe8
[]
no_license
cipher1024/lean-differential-topology
cf441b36af9fdb022f10afff6a2fdc5aa4afa379
1938b0a5d9e89faff89dac4bc51598698cae6dbb
refs/heads/master
1,619,477,568,536
1,527,790,354,000
1,527,790,354,000
124,159,851
0
0
null
1,520,385,485,000
1,520,385,485,000
null
UTF-8
Lean
false
false
2,500
lean
import algebra.module import tactic.refine universes u v namespace tactic open tactic.interactive meta def derive_field : tactic unit := do b ← target >>= is_prop, if b then do field ← get_current_field, intros >> funext, applyc field else do field ← get_current_field, xs ← intros <* intro1, applyc field, xs.mmap' apply run_cmd add_interactive [`derive_field] end tactic -- following does not work, always need (x[i]) -- local notation x`[`:max i`]`:0 := x i -- it would be nice to have a notation making it clear we don't think of x as a function namespace indexed_product variable {I : Type u} -- The indexing type variable {f : I → Type v} instance semigroup [∀ i, semigroup $ f i] : semigroup (Π i : I, f i) := by refine_struct { .. } ; derive_field instance comm_semigroup [∀ i, comm_semigroup $ f i] : comm_semigroup (Π i : I, f i) := by refine_struct { .. } ; derive_field instance monoid [∀ i, monoid $ f i] : monoid (Π i : I, f i) := by refine_struct { .. } ; try { derive_field } instance comm_monoid [∀ i, comm_monoid $ f i] : comm_monoid (Π i : I, f i) := by refine_struct { .. } ; try { derive_field } instance group [∀ i, group $ f i] : group (Π i : I, f i) := by refine_struct { .. } ; try { derive_field } instance add_semigroup [∀ i, add_semigroup $ f i] : add_semigroup (Π i : I, f i) := by refine_struct { .. } ; try { derive_field } instance add_group [∀ i, add_group $ f i] : add_group (Π i : I, f i) := by refine_struct { .. } ; try { derive_field } instance add_comm_group [∀ i, add_comm_group $ f i] : add_comm_group (Π i : I, f i) := by refine_struct { .. } ; try { derive_field } instance distrib [∀ i, distrib $ f i] : distrib (Π i : I, f i) := by refine_struct { .. } ; try { derive_field } instance ring [∀ i, ring $ f i] : ring (Π i : I, f i) := by refine_struct { .. } ; try { derive_field } instance comm_ring [∀ i, comm_ring $ f i] : comm_ring (Π i : I, f i) := by refine_struct { .. } ; try { derive_field } instance has_scalar {α : Type*} [∀ i, has_scalar α $ f i] : has_scalar α (Π i : I, f i) := by refine_struct { .. } ; try { derive_field } instance module {α : Type*} [ring α] [∀ i, module α $ f i] : module α (Π i : I, f i) := by refine_struct { .. } ; try { derive_field } instance vector_space (α : Type*) [field α] [∀ i, vector_space α $ f i] : vector_space α (Π i : I, f i) := { ..indexed_product.module } end indexed_product
5f81d7ac1cbd7d1edb5fcd3214c71464038d36a2
28be2ab6091504b6ba250b367205fb94d50ab284
/src/game/world10/level11.lean
858b8e36e5d86b73ca96c4645e1b8fdccab1198a
[ "Apache-2.0" ]
permissive
postmasters/natural_number_game
87304ac22e5e1c5ac2382d6e523d6914dd67a92d
38a7adcdfdb18c49c87b37831736c8f15300d821
refs/heads/master
1,649,856,819,031
1,586,444,676,000
1,586,444,676,000
255,006,061
0
0
Apache-2.0
1,586,664,599,000
1,586,664,598,000
null
UTF-8
Lean
false
false
571
lean
import game.world10.level10 -- hide namespace mynat -- hide /- # Inequality world. ## Level 11: `add_le_add_right` If you're faced with a goal of the form `forall t, ...`, then the next line is "so let $t$ be arbitrary". The way to do this in Lean is `intro t`. -/ /- Lemma For all naturals $a$ and $b$, $a\le b$ implies that for all naturals $t$, $a+t\le b+t$. -/ theorem add_le_add_right (a b : mynat) : a ≤ b → ∀ t, (a + t) ≤ (b + t) := begin [nat_num_game] intro h, cases h with c hc, intro t, use c, rw hc, ring, end end mynat -- hide
460a0509f8f3b207109ad45ee29c2689f9f5738b
fecda8e6b848337561d6467a1e30cf23176d6ad0
/src/data/nat/log.lean
2b86ee553813d003f15f4628b0d21475caa44b45
[ "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
2,170
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 data.nat.basic /-! # Natural number logarithm This file defines `log b n`, the logarithm of `n` with base `b`, to be the largest `k` such that `b ^ k ≤ n`. -/ namespace nat /-- `log b n`, is the logarithm of natural number `n` in base `b`. It returns the largest `k : ℕ` such that `b^k ≤ n`, so if `b^k = n`, it returns exactly `k`. -/ @[pp_nodot] def log (b : ℕ) : ℕ → ℕ | n := if h : b ≤ n ∧ 1 < b then have n / b < n, from div_lt_self (nat.lt_of_lt_of_le (lt_trans zero_lt_one h.2) h.1) h.2, log (n / b) + 1 else 0 lemma pow_le_iff_le_log (x y : ℕ) {b} (hb : 1 < b) (hy : 1 ≤ y) : b^x ≤ y ↔ x ≤ log b y := begin induction y using nat.strong_induction_on with y ih generalizing x, rw [log], split_ifs, { have h'' : 0 < b := lt_of_le_of_lt (zero_le _) hb, cases h with h₀ h₁, rw [← nat.sub_le_right_iff_le_add,← ih (y / b), le_div_iff_mul_le _ _ h'',← nat.pow_succ], { cases x; simp [h₀,hy] }, { apply div_lt_self; assumption }, { rwa [le_div_iff_mul_le _ _ h'',one_mul], } }, { replace h := lt_of_not_ge (not_and'.1 h hb), split; intros h', { have := lt_of_le_of_lt h' h, apply le_of_succ_le_succ, change x < 1, rw [← pow_lt_iff_lt_right hb,pow_one], exact this }, { replace h' := le_antisymm h' (zero_le _), rw [h',nat.pow_zero], exact hy} }, end lemma log_pow (b x : ℕ) (hb : 1 < b) : log b (b ^ x) = x := eq_of_forall_le_iff $ λ z, by { rwa [← pow_le_iff_le_log _ _ hb,pow_le_iff_le_right], rw ← nat.pow_zero b, apply pow_le_pow_of_le_right, apply lt_of_le_of_lt (zero_le _) hb, apply zero_le } lemma pow_succ_log_gt_self (b x : ℕ) (hb : 1 < b) (hy : 1 ≤ x) : x < b ^ succ (log b x) := begin apply lt_of_not_ge, rw [(≥),pow_le_iff_le_log _ _ hb hy], apply not_le_of_lt, apply lt_succ_self, end lemma pow_log_le_self (b x : ℕ) (hb : 1 < b) (hx : 1 ≤ x) : b ^ log b x ≤ x := by rw [pow_le_iff_le_log _ _ hb hx] end nat
df42c3c278a679a16846730fbbb81a90f5341fc9
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/order/locally_finite.lean
a7d3bcd28f630a03a77fd858191e38d81170c01b
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
39,635
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.preimage /-! # Locally finite orders This file defines locally finite orders. A locally finite order is an order for which all bounded intervals are finite. This allows to make sense of `Icc`/`Ico`/`Ioc`/`Ioo` as lists, multisets, or finsets. Further, if the order is bounded above (resp. below), then we can also make sense of the "unbounded" intervals `Ici`/`Ioi` (resp. `Iic`/`Iio`). Many theorems about these intervals can be found in `data.finset.locally_finite`. ## Examples Naturally occurring locally finite orders are `ℕ`, `ℤ`, `ℕ+`, `fin n`, `α × β` the product of two locally finite orders, `α →₀ β` the finitely supported functions to a locally finite order `β`... ## Main declarations In a `locally_finite_order`, * `finset.Icc`: Closed-closed interval as a finset. * `finset.Ico`: Closed-open interval as a finset. * `finset.Ioc`: Open-closed interval as a finset. * `finset.Ioo`: Open-open interval as a finset. * `multiset.Icc`: Closed-closed interval as a multiset. * `multiset.Ico`: Closed-open interval as a multiset. * `multiset.Ioc`: Open-closed interval as a multiset. * `multiset.Ioo`: Open-open interval as a multiset. In a `locally_finite_order_top`, * `finset.Ici`: Closed-infinite interval as a finset. * `finset.Ioi`: Open-infinite interval as a finset. * `multiset.Ici`: Closed-infinite interval as a multiset. * `multiset.Ioi`: Open-infinite interval as a multiset. In a `locally_finite_order_bot`, * `finset.Iic`: Infinite-open interval as a finset. * `finset.Iio`: Infinite-closed interval as a finset. * `multiset.Iic`: Infinite-open interval as a multiset. * `multiset.Iio`: Infinite-closed interval as a multiset. ## Instances A `locally_finite_order` instance can be built * for a subtype of a locally finite order. See `subtype.locally_finite_order`. * for the product of two locally finite orders. See `prod.locally_finite_order`. * for any fintype (but not as an instance). See `fintype.to_locally_finite_order`. * from a definition of `finset.Icc` alone. See `locally_finite_order.of_Icc`. * by pulling back `locally_finite_order β` through an order embedding `f : α →o β`. See `order_embedding.locally_finite_order`. Instances for concrete types are proved in their respective files: * `ℕ` is in `data.nat.interval` * `ℤ` is in `data.int.interval` * `ℕ+` is in `data.pnat.interval` * `fin n` is in `data.fin.interval` * `finset α` is in `data.finset.interval` * `Σ i, α i` is in `data.sigma.interval` Along, you will find lemmas about the cardinality of those finite intervals. ## TODO Provide the `locally_finite_order` instance for `α ×ₗ β` where `locally_finite_order α` and `fintype β`. Provide the `locally_finite_order` instance for `α →₀ β` where `β` is locally finite. Provide the `locally_finite_order` instance for `Π₀ i, β i` where all the `β i` are locally finite. From `linear_order α`, `no_max_order α`, `locally_finite_order α`, we can also define an order isomorphism `α ≃ ℕ` or `α ≃ ℤ`, depending on whether we have `order_bot α` or `no_min_order α` and `nonempty α`. When `order_bot α`, we can match `a : α` to `(Iio a).card`. We can provide `succ_order α` from `linear_order α` and `locally_finite_order α` using ```lean lemma exists_min_greater [linear_order α] [locally_finite_order α] {x ub : α} (hx : x < ub) : ∃ lub, x < lub ∧ ∀ y, x < y → lub ≤ y := begin -- very non golfed have h : (finset.Ioc x ub).nonempty := ⟨ub, finset.mem_Ioc_iff.2 ⟨hx, le_rfl⟩⟩, use finset.min' (finset.Ioc x ub) h, split, { have := finset.min'_mem _ h, simp * at * }, rintro y hxy, obtain hy | hy := le_total y ub, apply finset.min'_le, simp * at *, exact (finset.min'_le _ _ (finset.mem_Ioc_iff.2 ⟨hx, le_rfl⟩)).trans hy, end ``` Note that the converse is not true. Consider `{-2^z | z : ℤ} ∪ {2^z | z : ℤ}`. Any element has a successor (and actually a predecessor as well), so it is a `succ_order`, but it's not locally finite as `Icc (-1) 1` is infinite. -/ open finset function /-- A locally finite order is an order where bounded intervals are finite. When you don't care too much about definitional equality, you can use `locally_finite_order.of_Icc` or `locally_finite_order.of_finite_Icc` to build a locally finite order from just `finset.Icc`. -/ class locally_finite_order (α : Type*) [preorder α] := (finset_Icc : α → α → finset α) (finset_Ico : α → α → finset α) (finset_Ioc : α → α → finset α) (finset_Ioo : α → α → finset α) (finset_mem_Icc : ∀ a b x : α, x ∈ finset_Icc a b ↔ a ≤ x ∧ x ≤ b) (finset_mem_Ico : ∀ a b x : α, x ∈ finset_Ico a b ↔ a ≤ x ∧ x < b) (finset_mem_Ioc : ∀ a b x : α, x ∈ finset_Ioc a b ↔ a < x ∧ x ≤ b) (finset_mem_Ioo : ∀ a b x : α, x ∈ finset_Ioo a b ↔ a < x ∧ x < b) /-- A locally finite order top is an order where all intervals bounded above are finite. This is slightly weaker than `locally_finite_order` + `order_top` as it allows empty types. -/ class locally_finite_order_top (α : Type*) [preorder α] := (finset_Ioi : α → finset α) (finset_Ici : α → finset α) (finset_mem_Ici : ∀ a x : α, x ∈ finset_Ici a ↔ a ≤ x) (finset_mem_Ioi : ∀ a x : α, x ∈ finset_Ioi a ↔ a < x) /-- A locally finite order bot is an order where all intervals bounded below are finite. This is slightly weaker than `locally_finite_order` + `order_bot` as it allows empty types. -/ class locally_finite_order_bot (α : Type*) [preorder α] := (finset_Iio : α → finset α) (finset_Iic : α → finset α) (finset_mem_Iic : ∀ a x : α, x ∈ finset_Iic a ↔ x ≤ a) (finset_mem_Iio : ∀ a x : α, x ∈ finset_Iio a ↔ x < a) /-- A constructor from a definition of `finset.Icc` alone, the other ones being derived by removing the ends. As opposed to `locally_finite_order.of_Icc`, this one requires `decidable_rel (≤)` but only `preorder`. -/ def locally_finite_order.of_Icc' (α : Type*) [preorder α] [decidable_rel ((≤) : α → α → Prop)] (finset_Icc : α → α → finset α) (mem_Icc : ∀ a b x, x ∈ finset_Icc a b ↔ a ≤ x ∧ x ≤ b) : locally_finite_order α := { finset_Icc := finset_Icc, finset_Ico := λ a b, (finset_Icc a b).filter (λ x, ¬b ≤ x), finset_Ioc := λ a b, (finset_Icc a b).filter (λ x, ¬x ≤ a), finset_Ioo := λ a b, (finset_Icc a b).filter (λ x, ¬x ≤ a ∧ ¬b ≤ x), finset_mem_Icc := mem_Icc, finset_mem_Ico := λ a b x, by rw [finset.mem_filter, mem_Icc, and_assoc, lt_iff_le_not_le], finset_mem_Ioc := λ a b x, by rw [finset.mem_filter, mem_Icc, and.right_comm, lt_iff_le_not_le], finset_mem_Ioo := λ a b x, by rw [finset.mem_filter, mem_Icc, and_and_and_comm, lt_iff_le_not_le, lt_iff_le_not_le] } /-- A constructor from a definition of `finset.Icc` alone, the other ones being derived by removing the ends. As opposed to `locally_finite_order.of_Icc`, this one requires `partial_order` but only `decidable_eq`. -/ def locally_finite_order.of_Icc (α : Type*) [partial_order α] [decidable_eq α] (finset_Icc : α → α → finset α) (mem_Icc : ∀ a b x, x ∈ finset_Icc a b ↔ a ≤ x ∧ x ≤ b) : locally_finite_order α := { finset_Icc := finset_Icc, finset_Ico := λ a b, (finset_Icc a b).filter (λ x, x ≠ b), finset_Ioc := λ a b, (finset_Icc a b).filter (λ x, a ≠ x), finset_Ioo := λ a b, (finset_Icc a b).filter (λ x, a ≠ x ∧ x ≠ b), finset_mem_Icc := mem_Icc, finset_mem_Ico := λ a b x, by rw [finset.mem_filter, mem_Icc, and_assoc, lt_iff_le_and_ne], finset_mem_Ioc := λ a b x, by rw [finset.mem_filter, mem_Icc, and.right_comm, lt_iff_le_and_ne], finset_mem_Ioo := λ a b x, by rw [finset.mem_filter, mem_Icc, and_and_and_comm, lt_iff_le_and_ne, lt_iff_le_and_ne] } /-- A constructor from a definition of `finset.Iic` alone, the other ones being derived by removing the ends. As opposed to `locally_finite_order_top.of_Ici`, this one requires `decidable_rel (≤)` but only `preorder`. -/ def locally_finite_order_top.of_Ici' (α : Type*) [preorder α] [decidable_rel ((≤) : α → α → Prop)] (finset_Ici : α → finset α) (mem_Ici : ∀ a x, x ∈ finset_Ici a ↔ a ≤ x) : locally_finite_order_top α := { finset_Ici := finset_Ici, finset_Ioi := λ a, (finset_Ici a).filter (λ x, ¬x ≤ a), finset_mem_Ici := mem_Ici, finset_mem_Ioi := λ a x, by rw [mem_filter, mem_Ici, lt_iff_le_not_le] } /-- A constructor from a definition of `finset.Iic` alone, the other ones being derived by removing the ends. As opposed to `locally_finite_order_top.of_Ici'`, this one requires `partial_order` but only `decidable_eq`. -/ def locally_finite_order_top.of_Ici (α : Type*) [partial_order α] [decidable_eq α] (finset_Ici : α → finset α) (mem_Ici : ∀ a x, x ∈ finset_Ici a ↔ a ≤ x) : locally_finite_order_top α := { finset_Ici := finset_Ici, finset_Ioi := λ a, (finset_Ici a).filter (λ x, a ≠ x), finset_mem_Ici := mem_Ici, finset_mem_Ioi := λ a x, by rw [mem_filter, mem_Ici, lt_iff_le_and_ne] } /-- A constructor from a definition of `finset.Iic` alone, the other ones being derived by removing the ends. As opposed to `locally_finite_order.of_Icc`, this one requires `decidable_rel (≤)` but only `preorder`. -/ def locally_finite_order_bot.of_Iic' (α : Type*) [preorder α] [decidable_rel ((≤) : α → α → Prop)] (finset_Iic : α → finset α) (mem_Iic : ∀ a x, x ∈ finset_Iic a ↔ x ≤ a) : locally_finite_order_bot α := { finset_Iic := finset_Iic, finset_Iio := λ a, (finset_Iic a).filter (λ x, ¬a ≤ x), finset_mem_Iic := mem_Iic, finset_mem_Iio := λ a x, by rw [mem_filter, mem_Iic, lt_iff_le_not_le] } /-- A constructor from a definition of `finset.Iic` alone, the other ones being derived by removing the ends. As opposed to `locally_finite_order_top.of_Ici'`, this one requires `partial_order` but only `decidable_eq`. -/ def locally_finite_order_top.of_Iic (α : Type*) [partial_order α] [decidable_eq α] (finset_Iic : α → finset α) (mem_Iic : ∀ a x, x ∈ finset_Iic a ↔ x ≤ a) : locally_finite_order_bot α := { finset_Iic := finset_Iic, finset_Iio := λ a, (finset_Iic a).filter (λ x, x ≠ a), finset_mem_Iic := mem_Iic, finset_mem_Iio := λ a x, by rw [mem_filter, mem_Iic, lt_iff_le_and_ne] } variables {α β : Type*} /-- An empty type is locally finite. This is not an instance as it would be not be defeq to more specific instances. -/ @[reducible] -- See note [reducible non-instances] protected def _root_.is_empty.to_locally_finite_order [preorder α] [is_empty α] : locally_finite_order α := { finset_Icc := is_empty_elim, finset_Ico := is_empty_elim, finset_Ioc := is_empty_elim, finset_Ioo := is_empty_elim, finset_mem_Icc := is_empty_elim, finset_mem_Ico := is_empty_elim, finset_mem_Ioc := is_empty_elim, finset_mem_Ioo := is_empty_elim } /-- An empty type is locally finite. This is not an instance as it would be not be defeq to more specific instances. -/ @[reducible] -- See note [reducible non-instances] protected def _root_.is_empty.to_locally_finite_order_top [preorder α] [is_empty α] : locally_finite_order_top α := { finset_Ici := is_empty_elim, finset_Ioi := is_empty_elim, finset_mem_Ici := is_empty_elim, finset_mem_Ioi := is_empty_elim } /-- An empty type is locally finite. This is not an instance as it would be not be defeq to more specific instances. -/ @[reducible] -- See note [reducible non-instances] protected def _root_.is_empty.to_locally_finite_order_bot [preorder α] [is_empty α] : locally_finite_order_bot α := { finset_Iic := is_empty_elim, finset_Iio := is_empty_elim, finset_mem_Iic := is_empty_elim, finset_mem_Iio := is_empty_elim } /-! ### Intervals as finsets -/ namespace finset variables [preorder α] section locally_finite_order variables [locally_finite_order α] {a b x : α} /-- The finset of elements `x` such that `a ≤ x` and `x ≤ b`. Basically `set.Icc a b` as a finset. -/ def Icc (a b : α) : finset α := locally_finite_order.finset_Icc a b /-- The finset of elements `x` such that `a ≤ x` and `x < b`. Basically `set.Ico a b` as a finset. -/ def Ico (a b : α) : finset α := locally_finite_order.finset_Ico a b /-- The finset of elements `x` such that `a < x` and `x ≤ b`. Basically `set.Ioc a b` as a finset. -/ def Ioc (a b : α) : finset α := locally_finite_order.finset_Ioc a b /-- The finset of elements `x` such that `a < x` and `x < b`. Basically `set.Ioo a b` as a finset. -/ def Ioo (a b : α) : finset α := locally_finite_order.finset_Ioo a b @[simp] lemma mem_Icc : x ∈ Icc a b ↔ a ≤ x ∧ x ≤ b := locally_finite_order.finset_mem_Icc a b x @[simp] lemma mem_Ico : x ∈ Ico a b ↔ a ≤ x ∧ x < b := locally_finite_order.finset_mem_Ico a b x @[simp] lemma mem_Ioc : x ∈ Ioc a b ↔ a < x ∧ x ≤ b := locally_finite_order.finset_mem_Ioc a b x @[simp] lemma mem_Ioo : x ∈ Ioo a b ↔ a < x ∧ x < b := locally_finite_order.finset_mem_Ioo a b x @[simp, norm_cast] lemma coe_Icc (a b : α) : (Icc a b : set α) = set.Icc a b := set.ext $ λ _, mem_Icc @[simp, norm_cast] lemma coe_Ico (a b : α) : (Ico a b : set α) = set.Ico a b := set.ext $ λ _, mem_Ico @[simp, norm_cast] lemma coe_Ioc (a b : α) : (Ioc a b : set α) = set.Ioc a b := set.ext $ λ _, mem_Ioc @[simp, norm_cast] lemma coe_Ioo (a b : α) : (Ioo a b : set α) = set.Ioo a b := set.ext $ λ _, mem_Ioo end locally_finite_order section locally_finite_order_top variables [locally_finite_order_top α] {a x : α} /-- The finset of elements `x` such that `a ≤ x`. Basically `set.Ici a` as a finset. -/ def Ici (a : α) : finset α := locally_finite_order_top.finset_Ici a /-- The finset of elements `x` such that `a < x`. Basically `set.Ioi a` as a finset. -/ def Ioi (a : α) : finset α := locally_finite_order_top.finset_Ioi a @[simp] lemma mem_Ici : x ∈ Ici a ↔ a ≤ x := locally_finite_order_top.finset_mem_Ici _ _ @[simp] lemma mem_Ioi : x ∈ Ioi a ↔ a < x := locally_finite_order_top.finset_mem_Ioi _ _ @[simp, norm_cast] lemma coe_Ici (a : α) : (Ici a : set α) = set.Ici a := set.ext $ λ _, mem_Ici @[simp, norm_cast] lemma coe_Ioi (a : α) : (Ioi a : set α) = set.Ioi a := set.ext $ λ _, mem_Ioi end locally_finite_order_top section locally_finite_order_bot variables [locally_finite_order_bot α] {a x : α} /-- The finset of elements `x` such that `a ≤ x`. Basically `set.Iic a` as a finset. -/ def Iic (a : α) : finset α := locally_finite_order_bot.finset_Iic a /-- The finset of elements `x` such that `a < x`. Basically `set.Iio a` as a finset. -/ def Iio (a : α) : finset α := locally_finite_order_bot.finset_Iio a @[simp] lemma mem_Iic : x ∈ Iic a ↔ x ≤ a := locally_finite_order_bot.finset_mem_Iic _ _ @[simp] lemma mem_Iio : x ∈ Iio a ↔ x < a := locally_finite_order_bot.finset_mem_Iio _ _ @[simp, norm_cast] lemma coe_Iic (a : α) : (Iic a : set α) = set.Iic a := set.ext $ λ _, mem_Iic @[simp, norm_cast] lemma coe_Iio (a : α) : (Iio a : set α) = set.Iio a := set.ext $ λ _, mem_Iio end locally_finite_order_bot section order_top variables [locally_finite_order α] [order_top α] {a x : α} @[priority 100] -- See note [lower priority instance] instance _root_.locally_finite_order.to_locally_finite_order_top : locally_finite_order_top α := { finset_Ici := λ b, Icc b ⊤, finset_Ioi := λ b, Ioc b ⊤, finset_mem_Ici := λ a x, by rw [mem_Icc, and_iff_left le_top], finset_mem_Ioi := λ a x, by rw [mem_Ioc, and_iff_left le_top] } lemma Ici_eq_Icc (a : α) : Ici a = Icc a ⊤ := rfl lemma Ioi_eq_Ioc (a : α) : Ioi a = Ioc a ⊤ := rfl end order_top section order_bot variables [order_bot α] [locally_finite_order α] {b x : α} @[priority 100] -- See note [lower priority instance] instance locally_finite_order.to_locally_finite_order_bot : locally_finite_order_bot α := { finset_Iic := Icc ⊥, finset_Iio := Ico ⊥, finset_mem_Iic := λ a x, by rw [mem_Icc, and_iff_right bot_le], finset_mem_Iio := λ a x, by rw [mem_Ico, and_iff_right bot_le] } lemma Iic_eq_Icc : Iic = Icc (⊥ : α) := rfl lemma Iio_eq_Ico : Iio = Ico (⊥ : α) := rfl end order_bot end finset /-! ### Intervals as multisets -/ namespace multiset variables [preorder α] section locally_finite_order variables [locally_finite_order α] /-- The multiset of elements `x` such that `a ≤ x` and `x ≤ b`. Basically `set.Icc a b` as a multiset. -/ def Icc (a b : α) : multiset α := (finset.Icc a b).val /-- The multiset of elements `x` such that `a ≤ x` and `x < b`. Basically `set.Ico a b` as a multiset. -/ def Ico (a b : α) : multiset α := (finset.Ico a b).val /-- The multiset of elements `x` such that `a < x` and `x ≤ b`. Basically `set.Ioc a b` as a multiset. -/ def Ioc (a b : α) : multiset α := (finset.Ioc a b).val /-- The multiset of elements `x` such that `a < x` and `x < b`. Basically `set.Ioo a b` as a multiset. -/ def Ioo (a b : α) : multiset α := (finset.Ioo a b).val @[simp] lemma mem_Icc {a b x : α} : x ∈ Icc a b ↔ a ≤ x ∧ x ≤ b := by rw [Icc, ←finset.mem_def, finset.mem_Icc] @[simp] lemma mem_Ico {a b x : α} : x ∈ Ico a b ↔ a ≤ x ∧ x < b := by rw [Ico, ←finset.mem_def, finset.mem_Ico] @[simp] lemma mem_Ioc {a b x : α} : x ∈ Ioc a b ↔ a < x ∧ x ≤ b := by rw [Ioc, ←finset.mem_def, finset.mem_Ioc] @[simp] lemma mem_Ioo {a b x : α} : x ∈ Ioo a b ↔ a < x ∧ x < b := by rw [Ioo, ←finset.mem_def, finset.mem_Ioo] end locally_finite_order section locally_finite_order_top variables [locally_finite_order_top α] /-- The multiset of elements `x` such that `a ≤ x`. Basically `set.Ici a` as a multiset. -/ def Ici (a : α) : multiset α := (finset.Ici a).val /-- The multiset of elements `x` such that `a < x`. Basically `set.Ioi a` as a multiset. -/ def Ioi (a : α) : multiset α := (finset.Ioi a).val @[simp] lemma mem_Ici {a x : α} : x ∈ Ici a ↔ a ≤ x := by rw [Ici, ←finset.mem_def, finset.mem_Ici] @[simp] lemma mem_Ioi {a x : α} : x ∈ Ioi a ↔ a < x := by rw [Ioi, ←finset.mem_def, finset.mem_Ioi] end locally_finite_order_top section locally_finite_order_bot variables [locally_finite_order_bot α] /-- The multiset of elements `x` such that `x ≤ b`. Basically `set.Iic b` as a multiset. -/ def Iic (b : α) : multiset α := (finset.Iic b).val /-- The multiset of elements `x` such that `x < b`. Basically `set.Iio b` as a multiset. -/ def Iio (b : α) : multiset α := (finset.Iio b).val @[simp] lemma mem_Iic {b x : α} : x ∈ Iic b ↔ x ≤ b := by rw [Iic, ←finset.mem_def, finset.mem_Iic] @[simp] lemma mem_Iio {b x : α} : x ∈ Iio b ↔ x < b := by rw [Iio, ←finset.mem_def, finset.mem_Iio] end locally_finite_order_bot end multiset /-! ### Finiteness of `set` intervals -/ namespace set section preorder variables [preorder α] [locally_finite_order α] (a b : α) instance fintype_Icc : fintype (Icc a b) := fintype.of_finset (finset.Icc a b) (λ x, by rw [finset.mem_Icc, mem_Icc]) instance fintype_Ico : fintype (Ico a b) := fintype.of_finset (finset.Ico a b) (λ x, by rw [finset.mem_Ico, mem_Ico]) instance fintype_Ioc : fintype (Ioc a b) := fintype.of_finset (finset.Ioc a b) (λ x, by rw [finset.mem_Ioc, mem_Ioc]) instance fintype_Ioo : fintype (Ioo a b) := fintype.of_finset (finset.Ioo a b) (λ x, by rw [finset.mem_Ioo, mem_Ioo]) lemma finite_Icc : (Icc a b).finite := (Icc a b).to_finite lemma finite_Ico : (Ico a b).finite := (Ico a b).to_finite lemma finite_Ioc : (Ioc a b).finite := (Ioc a b).to_finite lemma finite_Ioo : (Ioo a b).finite := (Ioo a b).to_finite end preorder section order_top variables [preorder α] [locally_finite_order_top α] (a : α) instance fintype_Ici : fintype (Ici a) := fintype.of_finset (finset.Ici a) (λ x, by rw [finset.mem_Ici, mem_Ici]) instance fintype_Ioi : fintype (Ioi a) := fintype.of_finset (finset.Ioi a) (λ x, by rw [finset.mem_Ioi, mem_Ioi]) lemma finite_Ici : (Ici a).finite := (Ici a).to_finite lemma finite_Ioi : (Ioi a).finite := (Ioi a).to_finite end order_top section order_bot variables [preorder α] [locally_finite_order_bot α] (b : α) instance fintype_Iic : fintype (Iic b) := fintype.of_finset (finset.Iic b) (λ x, by rw [finset.mem_Iic, mem_Iic]) instance fintype_Iio : fintype (Iio b) := fintype.of_finset (finset.Iio b) (λ x, by rw [finset.mem_Iio, mem_Iio]) lemma finite_Iic : (Iic b).finite := (Iic b).to_finite lemma finite_Iio : (Iio b).finite := (Iio b).to_finite end order_bot end set /-! ### Instances -/ open finset section preorder variables [preorder α] [preorder β] /-- A noncomputable constructor from the finiteness of all closed intervals. -/ noncomputable def locally_finite_order.of_finite_Icc (h : ∀ a b : α, (set.Icc a b).finite) : locally_finite_order α := @locally_finite_order.of_Icc' α _ (classical.dec_rel _) (λ a b, (h a b).to_finset) (λ a b x, by rw [set.finite.mem_to_finset, set.mem_Icc]) /-- A fintype is a locally finite order. This is not an instance as it would not be defeq to better instances such as `fin.locally_finite_order`. -/ @[reducible] def fintype.to_locally_finite_order [fintype α] [@decidable_rel α (<)] [@decidable_rel α (≤)] : locally_finite_order α := { finset_Icc := λ a b, (set.Icc a b).to_finset, finset_Ico := λ a b, (set.Ico a b).to_finset, finset_Ioc := λ a b, (set.Ioc a b).to_finset, finset_Ioo := λ a b, (set.Ioo a b).to_finset, finset_mem_Icc := λ a b x, by simp only [set.mem_to_finset, set.mem_Icc], finset_mem_Ico := λ a b x, by simp only [set.mem_to_finset, set.mem_Ico], finset_mem_Ioc := λ a b x, by simp only [set.mem_to_finset, set.mem_Ioc], finset_mem_Ioo := λ a b x, by simp only [set.mem_to_finset, set.mem_Ioo] } instance : subsingleton (locally_finite_order α) := subsingleton.intro (λ h₀ h₁, begin cases h₀, cases h₁, have hIcc : h₀_finset_Icc = h₁_finset_Icc, { ext a b x, rw [h₀_finset_mem_Icc, h₁_finset_mem_Icc] }, have hIco : h₀_finset_Ico = h₁_finset_Ico, { ext a b x, rw [h₀_finset_mem_Ico, h₁_finset_mem_Ico] }, have hIoc : h₀_finset_Ioc = h₁_finset_Ioc, { ext a b x, rw [h₀_finset_mem_Ioc, h₁_finset_mem_Ioc] }, have hIoo : h₀_finset_Ioo = h₁_finset_Ioo, { ext a b x, rw [h₀_finset_mem_Ioo, h₁_finset_mem_Ioo] }, simp_rw [hIcc, hIco, hIoc, hIoo], end) instance : subsingleton (locally_finite_order_top α) := subsingleton.intro $ λ h₀ h₁, begin cases h₀, cases h₁, have hIci : h₀_finset_Ici = h₁_finset_Ici, { ext a b x, rw [h₀_finset_mem_Ici, h₁_finset_mem_Ici] }, have hIoi : h₀_finset_Ioi = h₁_finset_Ioi, { ext a b x, rw [h₀_finset_mem_Ioi, h₁_finset_mem_Ioi] }, simp_rw [hIci, hIoi], end instance : subsingleton (locally_finite_order_bot α) := subsingleton.intro $ λ h₀ h₁, begin cases h₀, cases h₁, have hIic : h₀_finset_Iic = h₁_finset_Iic, { ext a b x, rw [h₀_finset_mem_Iic, h₁_finset_mem_Iic] }, have hIio : h₀_finset_Iio = h₁_finset_Iio, { ext a b x, rw [h₀_finset_mem_Iio, h₁_finset_mem_Iio] }, simp_rw [hIic, hIio], end -- Should this be called `locally_finite_order.lift`? /-- Given an order embedding `α ↪o β`, pulls back the `locally_finite_order` on `β` to `α`. -/ protected noncomputable def order_embedding.locally_finite_order [locally_finite_order β] (f : α ↪o β) : locally_finite_order α := { finset_Icc := λ a b, (Icc (f a) (f b)).preimage f (f.to_embedding.injective.inj_on _), finset_Ico := λ a b, (Ico (f a) (f b)).preimage f (f.to_embedding.injective.inj_on _), finset_Ioc := λ a b, (Ioc (f a) (f b)).preimage f (f.to_embedding.injective.inj_on _), finset_Ioo := λ a b, (Ioo (f a) (f b)).preimage f (f.to_embedding.injective.inj_on _), finset_mem_Icc := λ a b x, by rw [mem_preimage, mem_Icc, f.le_iff_le, f.le_iff_le], finset_mem_Ico := λ a b x, by rw [mem_preimage, mem_Ico, f.le_iff_le, f.lt_iff_lt], finset_mem_Ioc := λ a b x, by rw [mem_preimage, mem_Ioc, f.lt_iff_lt, f.le_iff_le], finset_mem_Ioo := λ a b x, by rw [mem_preimage, mem_Ioo, f.lt_iff_lt, f.lt_iff_lt] } open order_dual section locally_finite_order variables [locally_finite_order α] (a b : α) /-- Note we define `Icc (to_dual a) (to_dual b)` as `Icc α _ _ b a` (which has type `finset α` not `finset αᵒᵈ`!) instead of `(Icc b a).map to_dual.to_embedding` as this means the following is defeq: ``` lemma this : (Icc (to_dual (to_dual a)) (to_dual (to_dual b)) : _) = (Icc a b : _) := rfl ``` -/ instance : locally_finite_order αᵒᵈ := { finset_Icc := λ a b, @Icc α _ _ (of_dual b) (of_dual a), finset_Ico := λ a b, @Ioc α _ _ (of_dual b) (of_dual a), finset_Ioc := λ a b, @Ico α _ _ (of_dual b) (of_dual a), finset_Ioo := λ a b, @Ioo α _ _ (of_dual b) (of_dual a), finset_mem_Icc := λ a b x, mem_Icc.trans (and_comm _ _), finset_mem_Ico := λ a b x, mem_Ioc.trans (and_comm _ _), finset_mem_Ioc := λ a b x, mem_Ico.trans (and_comm _ _), finset_mem_Ioo := λ a b x, mem_Ioo.trans (and_comm _ _) } lemma Icc_to_dual : Icc (to_dual a) (to_dual b) = (Icc b a).map to_dual.to_embedding := by { refine eq.trans _ map_refl.symm, ext c, rw [mem_Icc, mem_Icc], exact and_comm _ _ } lemma Ico_to_dual : Ico (to_dual a) (to_dual b) = (Ioc b a).map to_dual.to_embedding := by { refine eq.trans _ map_refl.symm, ext c, rw [mem_Ico, mem_Ioc], exact and_comm _ _ } lemma Ioc_to_dual : Ioc (to_dual a) (to_dual b) = (Ico b a).map to_dual.to_embedding := by { refine eq.trans _ map_refl.symm, ext c, rw [mem_Ioc, mem_Ico], exact and_comm _ _ } lemma Ioo_to_dual : Ioo (to_dual a) (to_dual b) = (Ioo b a).map to_dual.to_embedding := by { refine eq.trans _ map_refl.symm, ext c, rw [mem_Ioo, mem_Ioo], exact and_comm _ _ } lemma Icc_of_dual (a b : αᵒᵈ) : Icc (of_dual a) (of_dual b) = (Icc b a).map of_dual.to_embedding := by { refine eq.trans _ map_refl.symm, ext c, rw [mem_Icc, mem_Icc], exact and_comm _ _ } lemma Ico_of_dual (a b : αᵒᵈ) : Ico (of_dual a) (of_dual b) = (Ioc b a).map of_dual.to_embedding := by { refine eq.trans _ map_refl.symm, ext c, rw [mem_Ico, mem_Ioc], exact and_comm _ _ } lemma Ioc_of_dual (a b : αᵒᵈ) : Ioc (of_dual a) (of_dual b) = (Ico b a).map of_dual.to_embedding := by { refine eq.trans _ map_refl.symm, ext c, rw [mem_Ioc, mem_Ico], exact and_comm _ _ } lemma Ioo_of_dual (a b : αᵒᵈ) : Ioo (of_dual a) (of_dual b) = (Ioo b a).map of_dual.to_embedding := by { refine eq.trans _ map_refl.symm, ext c, rw [mem_Ioo, mem_Ioo], exact and_comm _ _ } end locally_finite_order section locally_finite_order_top variables [locally_finite_order_top α] /-- Note we define `Iic (to_dual a)` as `Ici a` (which has type `finset α` not `finset αᵒᵈ`!) instead of `(Ici a).map to_dual.to_embedding` as this means the following is defeq: ``` lemma this : (Iic (to_dual (to_dual a)) : _) = (Iic a : _) := rfl ``` -/ instance : locally_finite_order_bot αᵒᵈ := { finset_Iic := λ a, @Ici α _ _ (of_dual a), finset_Iio := λ a, @Ioi α _ _ (of_dual a), finset_mem_Iic := λ a x, mem_Ici, finset_mem_Iio := λ a x, mem_Ioi } lemma Iic_to_dual (a : α) : Iic (to_dual a) = (Ici a).map to_dual.to_embedding := map_refl.symm lemma Iio_to_dual (a : α) : Iio (to_dual a) = (Ioi a).map to_dual.to_embedding := map_refl.symm lemma Ici_of_dual (a : αᵒᵈ) : Ici (of_dual a) = (Iic a).map of_dual.to_embedding := map_refl.symm lemma Ioi_of_dual (a : αᵒᵈ) : Ioi (of_dual a) = (Iio a).map of_dual.to_embedding := map_refl.symm end locally_finite_order_top section locally_finite_order_top variables [locally_finite_order_bot α] /-- Note we define `Ici (to_dual a)` as `Iic a` (which has type `finset α` not `finset αᵒᵈ`!) instead of `(Iic a).map to_dual.to_embedding` as this means the following is defeq: ``` lemma this : (Ici (to_dual (to_dual a)) : _) = (Ici a : _) := rfl ``` -/ instance : locally_finite_order_top αᵒᵈ := { finset_Ici := λ a, @Iic α _ _ (of_dual a), finset_Ioi := λ a, @Iio α _ _ (of_dual a), finset_mem_Ici := λ a x, mem_Iic, finset_mem_Ioi := λ a x, mem_Iio } lemma Ici_to_dual (a : α) : Ici (to_dual a) = (Iic a).map to_dual.to_embedding := map_refl.symm lemma Ioi_to_dual (a : α) : Ioi (to_dual a) = (Iio a).map to_dual.to_embedding := map_refl.symm lemma Iic_of_dual (a : αᵒᵈ) : Iic (of_dual a) = (Ici a).map of_dual.to_embedding := map_refl.symm lemma Iio_of_dual (a : αᵒᵈ) : Iio (of_dual a) = (Ioi a).map of_dual.to_embedding := map_refl.symm end locally_finite_order_top instance [locally_finite_order α] [locally_finite_order β] [decidable_rel ((≤) : α × β → α × β → Prop)] : locally_finite_order (α × β) := locally_finite_order.of_Icc' (α × β) (λ a b, Icc a.fst b.fst ×ˢ Icc a.snd b.snd) (λ a b x, by { rw [mem_product, mem_Icc, mem_Icc, and_and_and_comm], refl }) instance [locally_finite_order_top α] [locally_finite_order_top β] [decidable_rel ((≤) : α × β → α × β → Prop)] : locally_finite_order_top (α × β) := locally_finite_order_top.of_Ici' (α × β) (λ a, Ici a.fst ×ˢ Ici a.snd) (λ a x, by { rw [mem_product, mem_Ici, mem_Ici], refl }) instance [locally_finite_order_bot α] [locally_finite_order_bot β] [decidable_rel ((≤) : α × β → α × β → Prop)] : locally_finite_order_bot (α × β) := locally_finite_order_bot.of_Iic' (α × β) (λ a, Iic a.fst ×ˢ Iic a.snd) (λ a x, by { rw [mem_product, mem_Iic, mem_Iic], refl }) end preorder /-! #### `with_top`, `with_bot` Adding a `⊤` to a locally finite `order_top` keeps it locally finite. Adding a `⊥` to a locally finite `order_bot` keeps it locally finite. -/ namespace with_top variables (α) [partial_order α] [order_top α] [locally_finite_order α] local attribute [pattern] coe local attribute [simp] option.mem_iff instance : locally_finite_order (with_top α) := { finset_Icc := λ a b, match a, b with | ⊤, ⊤ := {⊤} | ⊤, (b : α) := ∅ | (a : α), ⊤ := insert_none (Ici a) | (a : α), (b : α) := (Icc a b).map embedding.coe_option end, finset_Ico := λ a b, match a, b with | ⊤, _ := ∅ | (a : α), ⊤ := (Ici a).map embedding.coe_option | (a : α), (b : α) := (Ico a b).map embedding.coe_option end, finset_Ioc := λ a b, match a, b with | ⊤, _ := ∅ | (a : α), ⊤ := insert_none (Ioi a) | (a : α), (b : α) := (Ioc a b).map embedding.coe_option end, finset_Ioo := λ a b, match a, b with | ⊤, _ := ∅ | (a : α), ⊤ := (Ioi a).map embedding.coe_option | (a : α), (b : α) := (Ioo a b).map embedding.coe_option end, finset_mem_Icc := λ a b x, match a, b, x with | ⊤, ⊤, x := mem_singleton.trans (le_antisymm_iff.trans $ and_comm _ _) | ⊤, (b : α), x := iff_of_false (not_mem_empty _) (λ h, (h.1.trans h.2).not_lt $ coe_lt_top _) | (a : α), ⊤, ⊤ := by simp [with_top.locally_finite_order._match_1] | (a : α), ⊤, (x : α) := by simp [with_top.locally_finite_order._match_1, coe_eq_coe] | (a : α), (b : α), ⊤ := by simp [with_top.locally_finite_order._match_1] | (a : α), (b : α), (x : α) := by simp [with_top.locally_finite_order._match_1, coe_eq_coe] end, finset_mem_Ico := λ a b x, match a, b, x with | ⊤, b, x := iff_of_false (not_mem_empty _) (λ h, not_top_lt $ h.1.trans_lt h.2) | (a : α), ⊤, ⊤ := by simp [with_top.locally_finite_order._match_2] | (a : α), ⊤, (x : α) := by simp [with_top.locally_finite_order._match_2, coe_eq_coe, coe_lt_top] | (a : α), (b : α), ⊤ := by simp [with_top.locally_finite_order._match_2] | (a : α), (b : α), (x : α) := by simp [with_top.locally_finite_order._match_2, coe_eq_coe, coe_lt_coe] end, finset_mem_Ioc := λ a b x, match a, b, x with | ⊤, b, x := iff_of_false (not_mem_empty _) (λ h, not_top_lt $ h.1.trans_le h.2) | (a : α), ⊤, ⊤ := by simp [with_top.locally_finite_order._match_3, coe_lt_top] | (a : α), ⊤, (x : α) := by simp [with_top.locally_finite_order._match_3, coe_eq_coe, coe_lt_coe] | (a : α), (b : α), ⊤ := by simp [with_top.locally_finite_order._match_3] | (a : α), (b : α), (x : α) := by simp [with_top.locally_finite_order._match_3, coe_eq_coe, coe_lt_coe] end, finset_mem_Ioo := λ a b x, match a, b, x with | ⊤, b, x := iff_of_false (not_mem_empty _) (λ h, not_top_lt $ h.1.trans h.2) | (a : α), ⊤, ⊤ := by simp [with_top.locally_finite_order._match_4, coe_lt_top] | (a : α), ⊤, (x : α) := by simp [with_top.locally_finite_order._match_4, coe_eq_coe, coe_lt_coe, coe_lt_top] | (a : α), (b : α), ⊤ := by simp [with_top.locally_finite_order._match_4] | (a : α), (b : α), (x : α) := by simp [with_top.locally_finite_order._match_4, coe_eq_coe, coe_lt_coe] end } variables (a b : α) lemma Icc_coe_top : Icc (a : with_top α) ⊤ = insert_none (Ici a) := rfl lemma Icc_coe_coe : Icc (a : with_top α) b = (Icc a b).map embedding.coe_option := rfl lemma Ico_coe_top : Ico (a : with_top α) ⊤ = (Ici a).map embedding.coe_option := rfl lemma Ico_coe_coe : Ico (a : with_top α) b = (Ico a b).map embedding.coe_option := rfl lemma Ioc_coe_top : Ioc (a : with_top α) ⊤ = insert_none (Ioi a) := rfl lemma Ioc_coe_coe : Ioc (a : with_top α) b = (Ioc a b).map embedding.coe_option := rfl lemma Ioo_coe_top : Ioo (a : with_top α) ⊤ = (Ioi a).map embedding.coe_option := rfl lemma Ioo_coe_coe : Ioo (a : with_top α) b = (Ioo a b).map embedding.coe_option := rfl end with_top namespace with_bot variables (α) [partial_order α] [order_bot α] [locally_finite_order α] instance : locally_finite_order (with_bot α) := @order_dual.locally_finite_order (with_top αᵒᵈ) _ _ variables (a b : α) lemma Icc_bot_coe : Icc (⊥ : with_bot α) b = insert_none (Iic b) := rfl lemma Icc_coe_coe : Icc (a : with_bot α) b = (Icc a b).map embedding.coe_option := rfl lemma Ico_bot_coe : Ico (⊥ : with_bot α) b = insert_none (Iio b) := rfl lemma Ico_coe_coe : Ico (a : with_bot α) b = (Ico a b).map embedding.coe_option := rfl lemma Ioc_bot_coe : Ioc (⊥ : with_bot α) b = (Iic b).map embedding.coe_option := rfl lemma Ioc_coe_coe : Ioc (a : with_bot α) b = (Ioc a b).map embedding.coe_option := rfl lemma Ioo_bot_coe : Ioo (⊥ : with_bot α) b = (Iio b).map embedding.coe_option := rfl lemma Ioo_coe_coe : Ioo (a : with_bot α) b = (Ioo a b).map embedding.coe_option := rfl end with_bot /-! #### Subtype of a locally finite order -/ variables [preorder α] (p : α → Prop) [decidable_pred p] instance [locally_finite_order α] : locally_finite_order (subtype p) := { finset_Icc := λ a b, (Icc (a : α) b).subtype p, finset_Ico := λ a b, (Ico (a : α) b).subtype p, finset_Ioc := λ a b, (Ioc (a : α) b).subtype p, finset_Ioo := λ a b, (Ioo (a : α) b).subtype p, finset_mem_Icc := λ a b x, by simp_rw [finset.mem_subtype, mem_Icc, subtype.coe_le_coe], finset_mem_Ico := λ a b x, by simp_rw [finset.mem_subtype, mem_Ico, subtype.coe_le_coe, subtype.coe_lt_coe], finset_mem_Ioc := λ a b x, by simp_rw [finset.mem_subtype, mem_Ioc, subtype.coe_le_coe, subtype.coe_lt_coe], finset_mem_Ioo := λ a b x, by simp_rw [finset.mem_subtype, mem_Ioo, subtype.coe_lt_coe] } instance [locally_finite_order_top α] : locally_finite_order_top (subtype p) := { finset_Ici := λ a, (Ici (a : α)).subtype p, finset_Ioi := λ a, (Ioi (a : α)).subtype p, finset_mem_Ici := λ a x, by simp_rw [finset.mem_subtype, mem_Ici, subtype.coe_le_coe], finset_mem_Ioi := λ a x, by simp_rw [finset.mem_subtype, mem_Ioi, subtype.coe_lt_coe] } instance [locally_finite_order_bot α] : locally_finite_order_bot (subtype p) := { finset_Iic := λ a, (Iic (a : α)).subtype p, finset_Iio := λ a, (Iio (a : α)).subtype p, finset_mem_Iic := λ a x, by simp_rw [finset.mem_subtype, mem_Iic, subtype.coe_le_coe], finset_mem_Iio := λ a x, by simp_rw [finset.mem_subtype, mem_Iio, subtype.coe_lt_coe] } namespace finset section locally_finite_order variables [locally_finite_order α] (a b : subtype p) lemma subtype_Icc_eq : Icc a b = (Icc (a : α) b).subtype p := rfl lemma subtype_Ico_eq : Ico a b = (Ico (a : α) b).subtype p := rfl lemma subtype_Ioc_eq : Ioc a b = (Ioc (a : α) b).subtype p := rfl lemma subtype_Ioo_eq : Ioo a b = (Ioo (a : α) b).subtype p := rfl variables (hp : ∀ ⦃a b x⦄, a ≤ x → x ≤ b → p a → p b → p x) include hp lemma map_subtype_embedding_Icc : (Icc a b).map (embedding.subtype p) = Icc a b := begin rw subtype_Icc_eq, refine finset.subtype_map_of_mem (λ x hx, _), rw mem_Icc at hx, exact hp hx.1 hx.2 a.prop b.prop, end lemma map_subtype_embedding_Ico : (Ico a b).map (embedding.subtype p) = Ico a b := begin rw subtype_Ico_eq, refine finset.subtype_map_of_mem (λ x hx, _), rw mem_Ico at hx, exact hp hx.1 hx.2.le a.prop b.prop, end lemma map_subtype_embedding_Ioc : (Ioc a b).map (embedding.subtype p) = Ioc a b := begin rw subtype_Ioc_eq, refine finset.subtype_map_of_mem (λ x hx, _), rw mem_Ioc at hx, exact hp hx.1.le hx.2 a.prop b.prop, end lemma map_subtype_embedding_Ioo : (Ioo a b).map (embedding.subtype p) = Ioo a b := begin rw subtype_Ioo_eq, refine finset.subtype_map_of_mem (λ x hx, _), rw mem_Ioo at hx, exact hp hx.1.le hx.2.le a.prop b.prop, end end locally_finite_order section locally_finite_order_top variables [locally_finite_order_top α] (a : subtype p) lemma subtype_Ici_eq : Ici a = (Ici (a : α)).subtype p := rfl lemma subtype_Ioi_eq : Ioi a = (Ioi (a : α)).subtype p := rfl variables (hp : ∀ ⦃a x⦄, a ≤ x → p a → p x) include hp lemma map_subtype_embedding_Ici : (Ici a).map (embedding.subtype p) = Ici a := by { rw subtype_Ici_eq, exact finset.subtype_map_of_mem (λ x hx, hp (mem_Ici.1 hx) a.prop) } lemma map_subtype_embedding_Ioi : (Ioi a).map (embedding.subtype p) = Ioi a := by { rw subtype_Ioi_eq, exact finset.subtype_map_of_mem (λ x hx, hp (mem_Ioi.1 hx).le a.prop) } end locally_finite_order_top section locally_finite_order_bot variables [locally_finite_order_bot α] (a : subtype p) lemma subtype_Iic_eq : Iic a = (Iic (a : α)).subtype p := rfl lemma subtype_Iio_eq : Iio a = (Iio (a : α)).subtype p := rfl variables (hp : ∀ ⦃a x⦄, x ≤ a → p a → p x) include hp lemma map_subtype_embedding_Iic : (Iic a).map (embedding.subtype p) = Iic a := by { rw subtype_Iic_eq, exact finset.subtype_map_of_mem (λ x hx, hp (mem_Iic.1 hx) a.prop) } lemma map_subtype_embedding_Iio : (Iio a).map (embedding.subtype p) = Iio a := by { rw subtype_Iio_eq, exact finset.subtype_map_of_mem (λ x hx, hp (mem_Iio.1 hx).le a.prop) } end locally_finite_order_bot end finset
e2071b90bb4c1c8c5b11ed4df8112cb630e12308
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/unify3.lean
6c92d3bc8a1ffed018c387372c6056602fbdd616
[ "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
673
lean
open tactic set_option pp.all true example (a b : nat) : a = b → a = a := by do intro `H, eqc : expr ← mk_const `eq, A ← mk_mvar, m₁ ← mk_mvar, m₂ ← mk_mvar, e ← return (expr.app_of_list eqc [A, m₁, m₂]), trace "pattern: ", trace e, H ← get_local `H, Ht ← infer_type H, trace "term to unify: ", trace Ht, unify Ht e, trace "unification results using whnf: ", whnf A >>= trace, whnf m₁ >>= trace, whnf m₂ >>= trace, trace "unification results using get_assignment: ", get_assignment A >>= trace, get_assignment m₁ >>= trace, get_assignment m₂ >>= trace, mk_app `eq.refl [m₁] >>= exact
c76ad4d7de1c58a1da481ed19d7396719e45e3f0
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/calc_imp.lean
3b90350d4808eda0947e98e718f13b34429b2f65
[ "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
137
lean
example (A B C D : Prop) (h1 : A → B) (h2 : B → C) (h3 : C → D) : A → D := calc A → B : h1 ... → C : h2 ... → D : h3
2c2d287159f1958cff0014aa70cfb94563a13788
ee8cdbabf07f77e7be63a449b8483ce308d37218
/lean/src/valid/mathd-algebra-123.lean
a0fcf5bdd9c5c772f67babd99261d83a0a4ad9ee
[ "MIT", "Apache-2.0" ]
permissive
zeta1999/miniF2F
6d66c75d1c18152e224d07d5eed57624f731d4b7
c1ba9629559c5273c92ec226894baa0c1ce27861
refs/heads/main
1,681,897,460,642
1,620,646,361,000
1,620,646,361,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
598
lean
/- Copyright (c) 2021 OpenAI. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kunhao Zheng -/ import tactic.gptf import data.real.basic import data.pnat.basic example (a b : ℕ+) (h₀ : a + b = 20) (h₁ : a = 3 * b) : a - b = 10 := begin rw h₁ at h₀, rw h₁, have h₂ : 3 * (b:ℕ) + (b:ℕ) = (20:ℕ), { exact subtype.mk.inj h₀, }, have h₃ : (b:ℕ) = 5, linarith, have h₄ : b = 5, { exact pnat.eq h₃, }, rw h₄, calc (3:ℕ+) * 5 - 5 = 15 - 5 : by {congr,} ... = 10 : by {exact rfl}, end
dd6adbc003a38384b1066968b5fc92301a150866
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/order/conditionally_complete_lattice.lean
ddcbcdf34d69defd00a54d079269feec03b10856
[ "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
49,759
lean
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import data.set.intervals.ord_connected /-! # Theory of conditionally complete lattices. A conditionally complete lattice is a lattice in which every non-empty bounded subset s has a least upper bound and a greatest lower bound, denoted below by Sup s and Inf s. Typical examples are real, nat, int with their usual orders. The theory is very comparable to the theory of complete lattices, except that suitable boundedness and nonemptiness assumptions have to be added to most statements. We introduce two predicates bdd_above and bdd_below to express this boundedness, prove their basic properties, and then go on to prove most useful properties of Sup and Inf in conditionally complete lattices. To differentiate the statements between complete lattices and conditionally complete lattices, we prefix Inf and Sup in the statements by c, giving cInf and cSup. For instance, Inf_le is a statement in complete lattices ensuring Inf s ≤ x, while cInf_le is the same statement in conditionally complete lattices with an additional assumption that s is bounded below. -/ set_option old_structure_cmd true open set variables {α β : Type*} {ι : Sort*} section /-! Extension of Sup and Inf from a preorder `α` to `with_top α` and `with_bot α` -/ open_locale classical noncomputable instance {α : Type*} [preorder α] [has_Sup α] : has_Sup (with_top α) := ⟨λ S, if ⊤ ∈ S then ⊤ else if bdd_above (coe ⁻¹' S : set α) then ↑(Sup (coe ⁻¹' S : set α)) else ⊤⟩ noncomputable instance {α : Type*} [has_Inf α] : has_Inf (with_top α) := ⟨λ S, if S ⊆ {⊤} then ⊤ else ↑(Inf (coe ⁻¹' S : set α))⟩ noncomputable instance {α : Type*} [has_Sup α] : has_Sup (with_bot α) := ⟨(@with_top.has_Inf (order_dual α) _).Inf⟩ noncomputable instance {α : Type*} [preorder α] [has_Inf α] : has_Inf (with_bot α) := ⟨(@with_top.has_Sup (order_dual α) _ _).Sup⟩ @[simp] theorem with_top.cInf_empty {α : Type*} [has_Inf α] : Inf (∅ : set (with_top α)) = ⊤ := if_pos $ set.empty_subset _ @[simp] theorem with_bot.cSup_empty {α : Type*} [has_Sup α] : Sup (∅ : set (with_bot α)) = ⊥ := if_pos $ set.empty_subset _ end -- section /-- A conditionally complete lattice is a lattice in which every nonempty subset which is bounded above has a supremum, and every nonempty subset which is bounded below has an infimum. Typical examples are real numbers or natural numbers. To differentiate the statements from the corresponding statements in (unconditional) complete lattices, we prefix Inf and Sup by a c everywhere. The same statements should hold in both worlds, sometimes with additional assumptions of nonemptiness or boundedness.-/ class conditionally_complete_lattice (α : Type*) extends lattice α, has_Sup α, has_Inf α := (le_cSup : ∀s a, bdd_above s → a ∈ s → a ≤ Sup s) (cSup_le : ∀ s a, set.nonempty s → a ∈ upper_bounds s → Sup s ≤ a) (cInf_le : ∀s a, bdd_below s → a ∈ s → Inf s ≤ a) (le_cInf : ∀s a, set.nonempty s → a ∈ lower_bounds s → a ≤ Inf s) /-- A conditionally complete linear order is a linear order in which every nonempty subset which is bounded above has a supremum, and every nonempty subset which is bounded below has an infimum. Typical examples are real numbers or natural numbers. To differentiate the statements from the corresponding statements in (unconditional) complete linear orders, we prefix Inf and Sup by a c everywhere. The same statements should hold in both worlds, sometimes with additional assumptions of nonemptiness or boundedness.-/ class conditionally_complete_linear_order (α : Type*) extends conditionally_complete_lattice α, linear_order α /-- A conditionally complete linear order with `bot` is a linear order with least element, in which every nonempty subset which is bounded above has a supremum, and every nonempty subset (necessarily bounded below) has an infimum. A typical example is the natural numbers. To differentiate the statements from the corresponding statements in (unconditional) complete linear orders, we prefix Inf and Sup by a c everywhere. The same statements should hold in both worlds, sometimes with additional assumptions of nonemptiness or boundedness.-/ class conditionally_complete_linear_order_bot (α : Type*) extends conditionally_complete_linear_order α, order_bot α := (cSup_empty : Sup ∅ = ⊥) /- A complete lattice is a conditionally complete lattice, as there are no restrictions on the properties of Inf and Sup in a complete lattice.-/ @[priority 100] -- see Note [lower instance priority] instance conditionally_complete_lattice_of_complete_lattice [complete_lattice α]: conditionally_complete_lattice α := { le_cSup := by intros; apply le_Sup; assumption, cSup_le := by intros; apply Sup_le; assumption, cInf_le := by intros; apply Inf_le; assumption, le_cInf := by intros; apply le_Inf; assumption, ..‹complete_lattice α› } @[priority 100] -- see Note [lower instance priority] instance conditionally_complete_linear_order_of_complete_linear_order [complete_linear_order α]: conditionally_complete_linear_order α := { ..conditionally_complete_lattice_of_complete_lattice, .. ‹complete_linear_order α› } section open_locale classical /-- A well founded linear order is conditionally complete, with a bottom element. -/ @[reducible] noncomputable def well_founded.conditionally_complete_linear_order_with_bot {α : Type*} [i : linear_order α] (h : well_founded ((<) : α → α → Prop)) (c : α) (hc : c = h.min set.univ ⟨c, mem_univ c⟩) : conditionally_complete_linear_order_bot α := { sup := max, le_sup_left := le_max_left, le_sup_right := le_max_right, sup_le := λ a b c, max_le, inf := min, inf_le_left := min_le_left, inf_le_right := min_le_right, le_inf := λ a b c, le_min, Inf := λ s, if hs : s.nonempty then h.min s hs else c, cInf_le := begin assume s a hs has, have s_ne : s.nonempty := ⟨a, has⟩, simpa [s_ne] using not_lt.1 (h.not_lt_min s s_ne has), end, le_cInf := begin assume s a hs has, simp only [hs, dif_pos], exact has (h.min_mem s hs), end, Sup := λ s, if hs : (upper_bounds s).nonempty then h.min _ hs else c, le_cSup := begin assume s a hs has, have h's : (upper_bounds s).nonempty := hs, simp only [h's, dif_pos], exact h.min_mem _ h's has, end, cSup_le := begin assume s a hs has, have h's : (upper_bounds s).nonempty := ⟨a, has⟩, simp only [h's, dif_pos], simpa using h.not_lt_min _ h's has, end, bot := c, bot_le := λ x, by convert not_lt.1 (h.not_lt_min set.univ ⟨c, mem_univ c⟩ (mem_univ x)), cSup_empty := begin have : (set.univ : set α).nonempty := ⟨c, mem_univ c⟩, simp only [this, dif_pos, upper_bounds_empty], exact hc.symm end, .. i } end section order_dual instance (α : Type*) [conditionally_complete_lattice α] : conditionally_complete_lattice (order_dual α) := { le_cSup := @conditionally_complete_lattice.cInf_le α _, cSup_le := @conditionally_complete_lattice.le_cInf α _, le_cInf := @conditionally_complete_lattice.cSup_le α _, cInf_le := @conditionally_complete_lattice.le_cSup α _, ..order_dual.has_Inf α, ..order_dual.has_Sup α, ..order_dual.lattice α } instance (α : Type*) [conditionally_complete_linear_order α] : conditionally_complete_linear_order (order_dual α) := { ..order_dual.conditionally_complete_lattice α, ..order_dual.linear_order α } end order_dual section conditionally_complete_lattice variables [conditionally_complete_lattice α] {s t : set α} {a b : α} theorem le_cSup (h₁ : bdd_above s) (h₂ : a ∈ s) : a ≤ Sup s := conditionally_complete_lattice.le_cSup s a h₁ h₂ theorem cSup_le (h₁ : s.nonempty) (h₂ : ∀b∈s, b ≤ a) : Sup s ≤ a := conditionally_complete_lattice.cSup_le s a h₁ h₂ theorem cInf_le (h₁ : bdd_below s) (h₂ : a ∈ s) : Inf s ≤ a := conditionally_complete_lattice.cInf_le s a h₁ h₂ theorem le_cInf (h₁ : s.nonempty) (h₂ : ∀b∈s, a ≤ b) : a ≤ Inf s := conditionally_complete_lattice.le_cInf s a h₁ h₂ theorem le_cSup_of_le (_ : bdd_above s) (hb : b ∈ s) (h : a ≤ b) : a ≤ Sup s := le_trans h (le_cSup ‹bdd_above s› hb) theorem cInf_le_of_le (_ : bdd_below s) (hb : b ∈ s) (h : b ≤ a) : Inf s ≤ a := le_trans (cInf_le ‹bdd_below s› hb) h theorem cSup_le_cSup (_ : bdd_above t) (_ : s.nonempty) (h : s ⊆ t) : Sup s ≤ Sup t := cSup_le ‹_› (assume (a) (ha : a ∈ s), le_cSup ‹bdd_above t› (h ha)) theorem cInf_le_cInf (_ : bdd_below t) (_ : s.nonempty) (h : s ⊆ t) : Inf t ≤ Inf s := le_cInf ‹_› (assume (a) (ha : a ∈ s), cInf_le ‹bdd_below t› (h ha)) lemma is_lub_cSup (ne : s.nonempty) (H : bdd_above s) : is_lub s (Sup s) := ⟨assume x, le_cSup H, assume x, cSup_le ne⟩ lemma is_lub_csupr [nonempty ι] {f : ι → α} (H : bdd_above (range f)) : is_lub (range f) (⨆ i, f i) := is_lub_cSup (range_nonempty f) H lemma is_lub_csupr_set {f : β → α} {s : set β} (H : bdd_above (f '' s)) (Hne : s.nonempty) : is_lub (f '' s) (⨆ i : s, f i) := by { rw ← Sup_image', exact is_lub_cSup (Hne.image _) H } lemma is_glb_cInf (ne : s.nonempty) (H : bdd_below s) : is_glb s (Inf s) := ⟨assume x, cInf_le H, assume x, le_cInf ne⟩ lemma is_glb_cinfi [nonempty ι] {f : ι → α} (H : bdd_below (range f)) : is_glb (range f) (⨅ i, f i) := is_glb_cInf (range_nonempty f) H lemma is_glb_cinfi_set {f : β → α} {s : set β} (H : bdd_below (f '' s)) (Hne : s.nonempty) : is_glb (f '' s) (⨅ i : s, f i) := @is_lub_csupr_set (order_dual α) _ _ _ _ H Hne lemma is_lub.cSup_eq (H : is_lub s a) (ne : s.nonempty) : Sup s = a := (is_lub_cSup ne ⟨a, H.1⟩).unique H lemma is_lub.csupr_eq [nonempty ι] {f : ι → α} (H : is_lub (range f) a) : (⨆ i, f i) = a := H.cSup_eq (range_nonempty f) lemma is_lub.csupr_set_eq {s : set β} {f : β → α} (H : is_lub (f '' s) a) (Hne : s.nonempty) : (⨆ i : s, f i) = a := is_lub.cSup_eq (image_eq_range f s ▸ H) (image_eq_range f s ▸ Hne.image f) /-- A greatest element of a set is the supremum of this set. -/ lemma is_greatest.cSup_eq (H : is_greatest s a) : Sup s = a := H.is_lub.cSup_eq H.nonempty lemma is_greatest.Sup_mem (H : is_greatest s a) : Sup s ∈ s := H.cSup_eq.symm ▸ H.1 lemma is_glb.cInf_eq (H : is_glb s a) (ne : s.nonempty) : Inf s = a := (is_glb_cInf ne ⟨a, H.1⟩).unique H lemma is_glb.cinfi_eq [nonempty ι] {f : ι → α} (H : is_glb (range f) a) : (⨅ i, f i) = a := H.cInf_eq (range_nonempty f) lemma is_glb.cinfi_set_eq {s : set β} {f : β → α} (H : is_glb (f '' s) a) (Hne : s.nonempty) : (⨅ i : s, f i) = a := is_glb.cInf_eq (image_eq_range f s ▸ H) (image_eq_range f s ▸ Hne.image f) /-- A least element of a set is the infimum of this set. -/ lemma is_least.cInf_eq (H : is_least s a) : Inf s = a := H.is_glb.cInf_eq H.nonempty lemma is_least.Inf_mem (H : is_least s a) : Inf s ∈ s := H.cInf_eq.symm ▸ H.1 lemma subset_Icc_cInf_cSup (hb : bdd_below s) (ha : bdd_above s) : s ⊆ Icc (Inf s) (Sup s) := λ x hx, ⟨cInf_le hb hx, le_cSup ha hx⟩ theorem cSup_le_iff (hb : bdd_above s) (ne : s.nonempty) : Sup s ≤ a ↔ (∀b ∈ s, b ≤ a) := is_lub_le_iff (is_lub_cSup ne hb) theorem le_cInf_iff (hb : bdd_below s) (ne : s.nonempty) : a ≤ Inf s ↔ (∀b ∈ s, a ≤ b) := le_is_glb_iff (is_glb_cInf ne hb) lemma cSup_lower_bounds_eq_cInf {s : set α} (h : bdd_below s) (hs : s.nonempty) : Sup (lower_bounds s) = Inf s := (is_lub_cSup h $ hs.mono $ λ x hx y hy, hy hx).unique (is_glb_cInf hs h).is_lub lemma cInf_upper_bounds_eq_cSup {s : set α} (h : bdd_above s) (hs : s.nonempty) : Inf (upper_bounds s) = Sup s := (is_glb_cInf h $ hs.mono $ λ x hx y hy, hy hx).unique (is_lub_cSup hs h).is_glb lemma not_mem_of_lt_cInf {x : α} {s : set α} (h : x < Inf s) (hs : bdd_below s) : x ∉ s := λ hx, lt_irrefl _ (h.trans_le (cInf_le hs hx)) lemma not_mem_of_cSup_lt {x : α} {s : set α} (h : Sup s < x) (hs : bdd_above s) : x ∉ s := @not_mem_of_lt_cInf (order_dual α) _ x s h hs /--Introduction rule to prove that `b` is the supremum of `s`: it suffices to check that `b` is larger than all elements of `s`, and that this is not the case of any `w<b`. See `Sup_eq_of_forall_le_of_forall_lt_exists_gt` for a version in complete lattices. -/ theorem cSup_eq_of_forall_le_of_forall_lt_exists_gt (_ : s.nonempty) (_ : ∀a∈s, a ≤ b) (H : ∀w, w < b → (∃a∈s, w < a)) : Sup s = b := have bdd_above s := ⟨b, by assumption⟩, have (Sup s < b) ∨ (Sup s = b) := lt_or_eq_of_le (cSup_le ‹_› ‹∀a∈s, a ≤ b›), have ¬(Sup s < b) := assume: Sup s < b, let ⟨a, _, _⟩ := (H (Sup s) ‹Sup s < b›) in /- a ∈ s, Sup s < a-/ have Sup s < Sup s := lt_of_lt_of_le ‹Sup s < a› (le_cSup ‹bdd_above s› ‹a ∈ s›), show false, by finish [lt_irrefl (Sup s)], show Sup s = b, by finish /--Introduction rule to prove that `b` is the infimum of `s`: it suffices to check that `b` is smaller than all elements of `s`, and that this is not the case of any `w>b`. See `Inf_eq_of_forall_ge_of_forall_gt_exists_lt` for a version in complete lattices. -/ theorem cInf_eq_of_forall_ge_of_forall_gt_exists_lt (_ : s.nonempty) (_ : ∀a∈s, b ≤ a) (H : ∀w, b < w → (∃a∈s, a < w)) : Inf s = b := @cSup_eq_of_forall_le_of_forall_lt_exists_gt (order_dual α) _ _ _ ‹_› ‹_› ‹_› /--b < Sup s when there is an element a in s with b < a, when s is bounded above. This is essentially an iff, except that the assumptions for the two implications are slightly different (one needs boundedness above for one direction, nonemptiness and linear order for the other one), so we formulate separately the two implications, contrary to the complete_lattice case.-/ lemma lt_cSup_of_lt (_ : bdd_above s) (_ : a ∈ s) (_ : b < a) : b < Sup s := lt_of_lt_of_le ‹b < a› (le_cSup ‹bdd_above s› ‹a ∈ s›) /--Inf s < b when there is an element a in s with a < b, when s is bounded below. This is essentially an iff, except that the assumptions for the two implications are slightly different (one needs boundedness below for one direction, nonemptiness and linear order for the other one), so we formulate separately the two implications, contrary to the complete_lattice case.-/ lemma cInf_lt_of_lt (_ : bdd_below s) (_ : a ∈ s) (_ : a < b) : Inf s < b := @lt_cSup_of_lt (order_dual α) _ _ _ _ ‹_› ‹_› ‹_› /-- If all elements of a nonempty set `s` are less than or equal to all elements of a nonempty set `t`, then there exists an element between these sets. -/ lemma exists_between_of_forall_le (sne : s.nonempty) (tne : t.nonempty) (hst : ∀ (x ∈ s) (y ∈ t), x ≤ y) : (upper_bounds s ∩ lower_bounds t).nonempty := ⟨Inf t, λ x hx, le_cInf tne $ hst x hx, λ y hy, cInf_le (sne.mono hst) hy⟩ /--The supremum of a singleton is the element of the singleton-/ @[simp] theorem cSup_singleton (a : α) : Sup {a} = a := is_greatest_singleton.cSup_eq /--The infimum of a singleton is the element of the singleton-/ @[simp] theorem cInf_singleton (a : α) : Inf {a} = a := is_least_singleton.cInf_eq /--If a set is bounded below and above, and nonempty, its infimum is less than or equal to its supremum.-/ theorem cInf_le_cSup (hb : bdd_below s) (ha : bdd_above s) (ne : s.nonempty) : Inf s ≤ Sup s := is_glb_le_is_lub (is_glb_cInf ne hb) (is_lub_cSup ne ha) ne /--The sup of a union of two sets is the max of the suprema of each subset, under the assumptions that all sets are bounded above and nonempty.-/ theorem cSup_union (hs : bdd_above s) (sne : s.nonempty) (ht : bdd_above t) (tne : t.nonempty) : Sup (s ∪ t) = Sup s ⊔ Sup t := ((is_lub_cSup sne hs).union (is_lub_cSup tne ht)).cSup_eq sne.inl /--The inf of a union of two sets is the min of the infima of each subset, under the assumptions that all sets are bounded below and nonempty.-/ theorem cInf_union (hs : bdd_below s) (sne : s.nonempty) (ht : bdd_below t) (tne : t.nonempty) : Inf (s ∪ t) = Inf s ⊓ Inf t := @cSup_union (order_dual α) _ _ _ hs sne ht tne /--The supremum of an intersection of two sets is bounded by the minimum of the suprema of each set, if all sets are bounded above and nonempty.-/ theorem cSup_inter_le (_ : bdd_above s) (_ : bdd_above t) (hst : (s ∩ t).nonempty) : Sup (s ∩ t) ≤ Sup s ⊓ Sup t := begin apply cSup_le hst, simp only [le_inf_iff, and_imp, set.mem_inter_eq], intros b _ _, split, apply le_cSup ‹bdd_above s› ‹b ∈ s›, apply le_cSup ‹bdd_above t› ‹b ∈ t› end /--The infimum of an intersection of two sets is bounded below by the maximum of the infima of each set, if all sets are bounded below and nonempty.-/ theorem le_cInf_inter (_ : bdd_below s) (_ : bdd_below t) (hst : (s ∩ t).nonempty) : Inf s ⊔ Inf t ≤ Inf (s ∩ t) := @cSup_inter_le (order_dual α) _ _ _ ‹_› ‹_› hst /-- The supremum of insert a s is the maximum of a and the supremum of s, if s is nonempty and bounded above.-/ theorem cSup_insert (hs : bdd_above s) (sne : s.nonempty) : Sup (insert a s) = a ⊔ Sup s := ((is_lub_cSup sne hs).insert a).cSup_eq (insert_nonempty a s) /-- The infimum of insert a s is the minimum of a and the infimum of s, if s is nonempty and bounded below.-/ theorem cInf_insert (hs : bdd_below s) (sne : s.nonempty) : Inf (insert a s) = a ⊓ Inf s := @cSup_insert (order_dual α) _ _ _ hs sne @[simp] lemma cInf_Icc (h : a ≤ b) : Inf (Icc a b) = a := (is_glb_Icc h).cInf_eq (nonempty_Icc.2 h) @[simp] lemma cInf_Ici : Inf (Ici a) = a := is_least_Ici.cInf_eq @[simp] lemma cInf_Ico (h : a < b) : Inf (Ico a b) = a := (is_glb_Ico h).cInf_eq (nonempty_Ico.2 h) @[simp] lemma cInf_Ioc [densely_ordered α] (h : a < b) : Inf (Ioc a b) = a := (is_glb_Ioc h).cInf_eq (nonempty_Ioc.2 h) @[simp] lemma cInf_Ioi [no_top_order α] [densely_ordered α] : Inf (Ioi a) = a := cInf_eq_of_forall_ge_of_forall_gt_exists_lt nonempty_Ioi (λ _, le_of_lt) (λ w hw, by simpa using exists_between hw) @[simp] lemma cInf_Ioo [densely_ordered α] (h : a < b) : Inf (Ioo a b) = a := (is_glb_Ioo h).cInf_eq (nonempty_Ioo.2 h) @[simp] lemma cSup_Icc (h : a ≤ b) : Sup (Icc a b) = b := (is_lub_Icc h).cSup_eq (nonempty_Icc.2 h) @[simp] lemma cSup_Ico [densely_ordered α] (h : a < b) : Sup (Ico a b) = b := (is_lub_Ico h).cSup_eq (nonempty_Ico.2 h) @[simp] lemma cSup_Iic : Sup (Iic a) = a := is_greatest_Iic.cSup_eq @[simp] lemma cSup_Iio [no_bot_order α] [densely_ordered α] : Sup (Iio a) = a := cSup_eq_of_forall_le_of_forall_lt_exists_gt nonempty_Iio (λ _, le_of_lt) (λ w hw, by simpa [and_comm] using exists_between hw) @[simp] lemma cSup_Ioc (h : a < b) : Sup (Ioc a b) = b := (is_lub_Ioc h).cSup_eq (nonempty_Ioc.2 h) @[simp] lemma cSup_Ioo [densely_ordered α] (h : a < b) : Sup (Ioo a b) = b := (is_lub_Ioo h).cSup_eq (nonempty_Ioo.2 h) /--The indexed supremum of a function is bounded above by a uniform bound-/ lemma csupr_le [nonempty ι] {f : ι → α} {c : α} (H : ∀x, f x ≤ c) : supr f ≤ c := cSup_le (range_nonempty f) (by rwa forall_range_iff) /--The indexed supremum of a function is bounded below by the value taken at one point-/ lemma le_csupr {f : ι → α} (H : bdd_above (range f)) (c : ι) : f c ≤ supr f := le_cSup H (mem_range_self _) lemma le_csupr_of_le {f : ι → α} (H : bdd_above (range f)) (c : ι) (h : a ≤ f c) : a ≤ supr f := le_trans h (le_csupr H c) /--The indexed supremum of two functions are comparable if the functions are pointwise comparable-/ lemma csupr_le_csupr {f g : ι → α} (B : bdd_above (range g)) (H : ∀x, f x ≤ g x) : supr f ≤ supr g := begin casesI is_empty_or_nonempty ι, { rw [supr_of_empty', supr_of_empty'] }, { exact csupr_le (λ x, le_csupr_of_le B x (H x)) }, end /--The indexed infimum of two functions are comparable if the functions are pointwise comparable-/ lemma cinfi_le_cinfi {f g : ι → α} (B : bdd_below (range f)) (H : ∀x, f x ≤ g x) : infi f ≤ infi g := @csupr_le_csupr (order_dual α) _ _ _ _ B H /--The indexed minimum of a function is bounded below by a uniform lower bound-/ lemma le_cinfi [nonempty ι] {f : ι → α} {c : α} (H : ∀x, c ≤ f x) : c ≤ infi f := @csupr_le (order_dual α) _ _ _ _ _ H /--The indexed infimum of a function is bounded above by the value taken at one point-/ lemma cinfi_le {f : ι → α} (H : bdd_below (range f)) (c : ι) : infi f ≤ f c := @le_csupr (order_dual α) _ _ _ H c lemma cinfi_le_of_le {f : ι → α} (H : bdd_below (range f)) (c : ι) (h : f c ≤ a) : infi f ≤ a := @le_csupr_of_le (order_dual α) _ _ _ _ H c h @[simp] theorem csupr_const [hι : nonempty ι] {a : α} : (⨆ b:ι, a) = a := by rw [supr, range_const, cSup_singleton] @[simp] theorem cinfi_const [hι : nonempty ι] {a : α} : (⨅ b:ι, a) = a := @csupr_const (order_dual α) _ _ _ _ theorem supr_unique [unique ι] {s : ι → α} : (⨆ i, s i) = s (default ι) := have ∀ i, s i = s (default ι) := λ i, congr_arg s (unique.eq_default i), by simp only [this, csupr_const] theorem infi_unique [unique ι] {s : ι → α} : (⨅ i, s i) = s (default ι) := @supr_unique (order_dual α) _ _ _ _ @[simp] theorem supr_unit {f : unit → α} : (⨆ x, f x) = f () := by { convert supr_unique, apply_instance } @[simp] theorem infi_unit {f : unit → α} : (⨅ x, f x) = f () := @supr_unit (order_dual α) _ _ @[simp] lemma csupr_pos {p : Prop} {f : p → α} (hp : p) : (⨆ h : p, f h) = f hp := by haveI := unique_prop hp; exact supr_unique @[simp] lemma cinfi_pos {p : Prop} {f : p → α} (hp : p) : (⨅ h : p, f h) = f hp := @csupr_pos (order_dual α) _ _ _ hp lemma csupr_set {s : set β} {f : β → α} : (⨆ x : s, f x) = Sup (f '' s) := begin rw supr, congr, ext, rw [mem_image, mem_range, set_coe.exists], simp_rw [subtype.coe_mk, exists_prop], end lemma cinfi_set {s : set β} {f : β → α} : (⨅ x : s, f x) = Inf (f '' s) := @csupr_set (order_dual α) _ _ _ _ /--Introduction rule to prove that `b` is the supremum of `f`: it suffices to check that `b` is larger than `f i` for all `i`, and that this is not the case of any `w<b`. See `supr_eq_of_forall_le_of_forall_lt_exists_gt` for a version in complete lattices. -/ theorem csupr_eq_of_forall_le_of_forall_lt_exists_gt [nonempty ι] {f : ι → α} (h₁ : ∀ i, f i ≤ b) (h₂ : ∀ w, w < b → (∃ i, w < f i)) : (⨆ (i : ι), f i) = b := cSup_eq_of_forall_le_of_forall_lt_exists_gt (range_nonempty f) (forall_range_iff.mpr h₁) (λ w hw, exists_range_iff.mpr $ h₂ w hw) /--Introduction rule to prove that `b` is the infimum of `f`: it suffices to check that `b` is smaller than `f i` for all `i`, and that this is not the case of any `w>b`. See `infi_eq_of_forall_ge_of_forall_gt_exists_lt` for a version in complete lattices. -/ theorem cinfi_eq_of_forall_ge_of_forall_gt_exists_lt [nonempty ι] {f : ι → α} (h₁ : ∀ i, b ≤ f i) (h₂ : ∀ w, b < w → (∃ i, f i < w)) : (⨅ (i : ι), f i) = b := @csupr_eq_of_forall_le_of_forall_lt_exists_gt (order_dual α) _ _ _ _ ‹_› ‹_› ‹_› /-- Nested intervals lemma: if `f` is a monotone sequence, `g` is an antitone sequence, and `f n ≤ g n` for all `n`, then `⨆ n, f n` belongs to all the intervals `[f n, g n]`. -/ lemma monotone.csupr_mem_Inter_Icc_of_antitone [nonempty β] [semilattice_sup β] {f g : β → α} (hf : monotone f) (hg : antitone g) (h : f ≤ g) : (⨆ n, f n) ∈ ⋂ n, Icc (f n) (g n) := begin inhabit β, refine mem_Inter.2 (λ n, ⟨le_csupr ⟨g $ default β, forall_range_iff.2 $ λ m, _⟩ _, csupr_le $ λ m, _⟩); exact hf.forall_le_of_antitone hg h _ _ end /-- Nested intervals lemma: if `[f n, g n]` is an antitone sequence of nonempty closed intervals, then `⨆ n, f n` belongs to all the intervals `[f n, g n]`. -/ lemma csupr_mem_Inter_Icc_of_antitone_Icc [nonempty β] [semilattice_sup β] {f g : β → α} (h : antitone (λ n, Icc (f n) (g n))) (h' : ∀ n, f n ≤ g n) : (⨆ n, f n) ∈ ⋂ n, Icc (f n) (g n) := monotone.csupr_mem_Inter_Icc_of_antitone (λ m n hmn, ((Icc_subset_Icc_iff (h' n)).1 (h hmn)).1) (λ m n hmn, ((Icc_subset_Icc_iff (h' n)).1 (h hmn)).2) h' lemma finset.nonempty.sup'_eq_cSup_image {s : finset β} (hs : s.nonempty) (f : β → α) : s.sup' hs f = Sup (f '' s) := eq_of_forall_ge_iff $ λ a, by simp [cSup_le_iff (s.finite_to_set.image f).bdd_above (hs.to_set.image f)] lemma finset.nonempty.sup'_id_eq_cSup {s : finset α} (hs : s.nonempty) : s.sup' hs id = Sup s := by rw [hs.sup'_eq_cSup_image, image_id] end conditionally_complete_lattice instance pi.conditionally_complete_lattice {ι : Type*} {α : Π i : ι, Type*} [Π i, conditionally_complete_lattice (α i)] : conditionally_complete_lattice (Π i, α i) := { le_cSup := λ s f ⟨g, hg⟩ hf i, le_cSup ⟨g i, set.forall_range_iff.2 $ λ ⟨f', hf'⟩, hg hf' i⟩ ⟨⟨f, hf⟩, rfl⟩, cSup_le := λ s f hs hf i, cSup_le (by haveI := hs.to_subtype; apply range_nonempty) $ λ b ⟨⟨g, hg⟩, hb⟩, hb ▸ hf hg i, cInf_le := λ s f ⟨g, hg⟩ hf i, cInf_le ⟨g i, set.forall_range_iff.2 $ λ ⟨f', hf'⟩, hg hf' i⟩ ⟨⟨f, hf⟩, rfl⟩, le_cInf := λ s f hs hf i, le_cInf (by haveI := hs.to_subtype; apply range_nonempty) $ λ b ⟨⟨g, hg⟩, hb⟩, hb ▸ hf hg i, .. pi.lattice, .. pi.has_Sup, .. pi.has_Inf } section conditionally_complete_linear_order variables [conditionally_complete_linear_order α] {s t : set α} {a b : α} lemma finset.nonempty.cSup_eq_max' {s : finset α} (h : s.nonempty) : Sup ↑s = s.max' h := eq_of_forall_ge_iff $ λ a, (cSup_le_iff s.bdd_above h.to_set).trans (s.max'_le_iff h).symm lemma finset.nonempty.cInf_eq_min' {s : finset α} (h : s.nonempty) : Inf ↑s = s.min' h := @finset.nonempty.cSup_eq_max' (order_dual α) _ s h lemma finset.nonempty.cSup_mem {s : finset α} (h : s.nonempty) : Sup (s : set α) ∈ s := by { rw h.cSup_eq_max', exact s.max'_mem _ } lemma finset.nonempty.cInf_mem {s : finset α} (h : s.nonempty) : Inf (s : set α) ∈ s := @finset.nonempty.cSup_mem (order_dual α) _ _ h lemma set.nonempty.cSup_mem (h : s.nonempty) (hs : finite s) : Sup s ∈ s := by { lift s to finset α using hs, exact finset.nonempty.cSup_mem h } lemma set.nonempty.cInf_mem (h : s.nonempty) (hs : finite s) : Inf s ∈ s := @set.nonempty.cSup_mem (order_dual α) _ _ h hs lemma set.finite.cSup_lt_iff (hs : finite s) (h : s.nonempty) : Sup s < a ↔ ∀ x ∈ s, x < a := ⟨λ h x hx, (le_cSup hs.bdd_above hx).trans_lt h, λ H, H _ $ h.cSup_mem hs⟩ lemma set.finite.lt_cInf_iff (hs : finite s) (h : s.nonempty) : a < Inf s ↔ ∀ x ∈ s, a < x := @set.finite.cSup_lt_iff (order_dual α) _ _ _ hs h /-- When b < Sup s, there is an element a in s with b < a, if s is nonempty and the order is a linear order. -/ lemma exists_lt_of_lt_cSup (hs : s.nonempty) (hb : b < Sup s) : ∃a∈s, b < a := begin classical, contrapose! hb, exact cSup_le hs hb end /-- Indexed version of the above lemma `exists_lt_of_lt_cSup`. When `b < supr f`, there is an element `i` such that `b < f i`. -/ lemma exists_lt_of_lt_csupr [nonempty ι] {f : ι → α} (h : b < supr f) : ∃i, b < f i := let ⟨_, ⟨i, rfl⟩, h⟩ := exists_lt_of_lt_cSup (range_nonempty f) h in ⟨i, h⟩ /--When Inf s < b, there is an element a in s with a < b, if s is nonempty and the order is a linear order.-/ lemma exists_lt_of_cInf_lt (hs : s.nonempty) (hb : Inf s < b) : ∃a∈s, a < b := @exists_lt_of_lt_cSup (order_dual α) _ _ _ hs hb /-- Indexed version of the above lemma `exists_lt_of_cInf_lt` When `infi f < a`, there is an element `i` such that `f i < a`. -/ lemma exists_lt_of_cinfi_lt [nonempty ι] {f : ι → α} (h : infi f < a) : (∃i, f i < a) := @exists_lt_of_lt_csupr (order_dual α) _ _ _ _ _ h /--Introduction rule to prove that b is the supremum of s: it suffices to check that 1) b is an upper bound 2) every other upper bound b' satisfies b ≤ b'.-/ theorem cSup_eq_of_is_forall_le_of_forall_le_imp_ge (_ : s.nonempty) (h_is_ub : ∀ a ∈ s, a ≤ b) (h_b_le_ub : ∀ub, (∀ a ∈ s, a ≤ ub) → (b ≤ ub)) : Sup s = b := le_antisymm (show Sup s ≤ b, from cSup_le ‹s.nonempty› h_is_ub) (show b ≤ Sup s, from h_b_le_ub _ $ assume a, le_cSup ⟨b, h_is_ub⟩) open function variables [is_well_order α (<)] lemma Inf_eq_argmin_on (hs : s.nonempty) : Inf s = argmin_on id (@is_well_order.wf α (<) _) s hs := is_least.cInf_eq ⟨argmin_on_mem _ _ _ _, λ a ha, argmin_on_le id _ _ ha⟩ lemma is_least_Inf (hs : s.nonempty) : is_least s (Inf s) := by { rw Inf_eq_argmin_on hs, exact ⟨argmin_on_mem _ _ _ _, λ a ha, argmin_on_le id _ _ ha⟩ } lemma le_cInf_iff' (hs : s.nonempty) : b ≤ Inf s ↔ b ∈ lower_bounds s := le_is_glb_iff (is_least_Inf hs).is_glb lemma Inf_mem (hs : s.nonempty) : Inf s ∈ s := (is_least_Inf hs).1 end conditionally_complete_linear_order /-! ### Lemmas about a conditionally complete linear order with bottom element In this case we have `Sup ∅ = ⊥`, so we can drop some `nonempty`/`set.nonempty` assumptions. -/ section conditionally_complete_linear_order_bot variables [conditionally_complete_linear_order_bot α] lemma cSup_empty : (Sup ∅ : α) = ⊥ := conditionally_complete_linear_order_bot.cSup_empty lemma csupr_of_empty [is_empty ι] (f : ι → α) : (⨆ i, f i) = ⊥ := by rw [supr_of_empty', cSup_empty] @[simp] lemma csupr_false (f : false → α) : (⨆ i, f i) = ⊥ := csupr_of_empty f lemma is_lub_cSup' {s : set α} (hs : bdd_above s) : is_lub s (Sup s) := begin rcases eq_empty_or_nonempty s with (rfl|hne), { simp only [cSup_empty, is_lub_empty] }, { exact is_lub_cSup hne hs } end lemma cSup_le_iff' {s : set α} (hs : bdd_above s) {a : α} : Sup s ≤ a ↔ ∀ x ∈ s, x ≤ a := is_lub_le_iff (is_lub_cSup' hs) lemma cSup_le' {s : set α} {a : α} (h : a ∈ upper_bounds s) : Sup s ≤ a := (cSup_le_iff' ⟨a, h⟩).2 h lemma exists_lt_of_lt_cSup' {s : set α} {a : α} (h : a < Sup s) : ∃ b ∈ s, a < b := by { contrapose! h, exact cSup_le' h } lemma csupr_le_iff' {f : ι → α} (h : bdd_above (range f)) {a : α} : (⨆ i, f i) ≤ a ↔ ∀ i, f i ≤ a := (cSup_le_iff' h).trans forall_range_iff lemma csupr_le' {f : ι → α} {a : α} (h : ∀ i, f i ≤ a) : (⨆ i, f i) ≤ a := cSup_le' $ forall_range_iff.2 h lemma exists_lt_of_lt_csupr' {f : ι → α} {a : α} (h : a < ⨆ i, f i) : ∃ i, a < f i := by { contrapose! h, exact csupr_le' h } end conditionally_complete_linear_order_bot namespace with_top open_locale classical variables [conditionally_complete_linear_order_bot α] /-- The Sup of a non-empty set is its least upper bound for a conditionally complete lattice with a top. -/ lemma is_lub_Sup' {β : Type*} [conditionally_complete_lattice β] {s : set (with_top β)} (hs : s.nonempty) : is_lub s (Sup s) := begin split, { show ite _ _ _ ∈ _, split_ifs, { intros _ _, exact le_top }, { rintro (⟨⟩|a) ha, { contradiction }, apply some_le_some.2, exact le_cSup h_1 ha }, { intros _ _, exact le_top } }, { show ite _ _ _ ∈ _, split_ifs, { rintro (⟨⟩|a) ha, { exact _root_.le_refl _ }, { exact false.elim (not_top_le_coe a (ha h)) } }, { rintro (⟨⟩|b) hb, { exact le_top }, refine some_le_some.2 (cSup_le _ _), { rcases hs with ⟨⟨⟩|b, hb⟩, { exact absurd hb h }, { exact ⟨b, hb⟩ } }, { intros a ha, exact some_le_some.1 (hb ha) } }, { rintro (⟨⟩|b) hb, { exact _root_.le_refl _ }, { exfalso, apply h_1, use b, intros a ha, exact some_le_some.1 (hb ha) } } } end lemma is_lub_Sup (s : set (with_top α)) : is_lub s (Sup s) := begin cases s.eq_empty_or_nonempty with hs hs, { rw hs, show is_lub ∅ (ite _ _ _), split_ifs, { cases h }, { rw [preimage_empty, cSup_empty], exact is_lub_empty }, { exfalso, apply h_1, use ⊥, rintro a ⟨⟩ } }, exact is_lub_Sup' hs, end /-- The Inf of a bounded-below set is its greatest lower bound for a conditionally complete lattice with a top. -/ lemma is_glb_Inf' {β : Type*} [conditionally_complete_lattice β] {s : set (with_top β)} (hs : bdd_below s) : is_glb s (Inf s) := begin split, { show ite _ _ _ ∈ _, split_ifs, { intros a ha, exact top_le_iff.2 (set.mem_singleton_iff.1 (h ha)) }, { rintro (⟨⟩|a) ha, { exact le_top }, refine some_le_some.2 (cInf_le _ ha), rcases hs with ⟨⟨⟩|b, hb⟩, { exfalso, apply h, intros c hc, rw [mem_singleton_iff, ←top_le_iff], exact hb hc }, use b, intros c hc, exact some_le_some.1 (hb hc) } }, { show ite _ _ _ ∈ _, split_ifs, { intros _ _, exact le_top }, { rintro (⟨⟩|a) ha, { exfalso, apply h, intros b hb, exact set.mem_singleton_iff.2 (top_le_iff.1 (ha hb)) }, { refine some_le_some.2 (le_cInf _ _), { classical, contrapose! h, rintros (⟨⟩|a) ha, { exact mem_singleton ⊤ }, { exact (h ⟨a, ha⟩).elim }}, { intros b hb, rw ←some_le_some, exact ha hb } } } } end lemma is_glb_Inf (s : set (with_top α)) : is_glb s (Inf s) := begin by_cases hs : bdd_below s, { exact is_glb_Inf' hs }, { exfalso, apply hs, use ⊥, intros _ _, exact bot_le }, end noncomputable instance : complete_linear_order (with_top α) := { Sup := Sup, le_Sup := assume s, (is_lub_Sup s).1, Sup_le := assume s, (is_lub_Sup s).2, Inf := Inf, le_Inf := assume s, (is_glb_Inf s).2, Inf_le := assume s, (is_glb_Inf s).1, .. with_top.linear_order, ..with_top.lattice, ..with_top.order_top, ..with_top.order_bot } lemma coe_Sup {s : set α} (hb : bdd_above s) : (↑(Sup s) : with_top α) = (⨆a∈s, ↑a) := begin cases s.eq_empty_or_nonempty with hs hs, { rw [hs, cSup_empty], simp only [set.mem_empty_eq, supr_bot, supr_false], refl }, apply le_antisymm, { refine (coe_le_iff.2 $ assume b hb, cSup_le hs $ assume a has, coe_le_coe.1 $ hb ▸ _), exact (le_supr_of_le a $ le_supr_of_le has $ _root_.le_refl _) }, { exact (supr_le $ assume a, supr_le $ assume ha, coe_le_coe.2 $ le_cSup hb ha) } end lemma coe_Inf {s : set α} (hs : s.nonempty) : (↑(Inf s) : with_top α) = (⨅a∈s, ↑a) := let ⟨x, hx⟩ := hs in have (⨅a∈s, ↑a : with_top α) ≤ x, from infi_le_of_le x $ infi_le_of_le hx $ _root_.le_refl _, let ⟨r, r_eq, hr⟩ := le_coe_iff.1 this in le_antisymm (le_infi $ assume a, le_infi $ assume ha, coe_le_coe.2 $ cInf_le (order_bot.bdd_below s) ha) begin refine (r_eq.symm ▸ coe_le_coe.2 $ le_cInf hs $ assume a has, coe_le_coe.1 $ _), refine (r_eq ▸ infi_le_of_le a _), exact (infi_le_of_le has $ _root_.le_refl _), end end with_top namespace monotone variables [preorder α] [conditionally_complete_lattice β] {f : α → β} (h_mono : monotone f) /-! A monotone function into a conditionally complete lattice preserves the ordering properties of `Sup` and `Inf`. -/ lemma le_cSup_image {s : set α} {c : α} (hcs : c ∈ s) (h_bdd : bdd_above s) : f c ≤ Sup (f '' s) := le_cSup (map_bdd_above h_mono h_bdd) (mem_image_of_mem f hcs) lemma cSup_image_le {s : set α} (hs : s.nonempty) {B : α} (hB: B ∈ upper_bounds s) : Sup (f '' s) ≤ f B := cSup_le (nonempty.image f hs) (h_mono.mem_upper_bounds_image hB) lemma cInf_image_le {s : set α} {c : α} (hcs : c ∈ s) (h_bdd : bdd_below s) : Inf (f '' s) ≤ f c := @le_cSup_image (order_dual α) (order_dual β) _ _ _ (λ x y hxy, h_mono hxy) _ _ hcs h_bdd lemma le_cInf_image {s : set α} (hs : s.nonempty) {B : α} (hB: B ∈ lower_bounds s) : f B ≤ Inf (f '' s) := @cSup_image_le (order_dual α) (order_dual β) _ _ _ (λ x y hxy, h_mono hxy) _ hs _ hB end monotone namespace galois_connection variables {γ : Type*} [conditionally_complete_lattice α] [conditionally_complete_lattice β] [nonempty ι] {l : α → β} {u : β → α} lemma l_cSup (gc : galois_connection l u) {s : set α} (hne : s.nonempty) (hbdd : bdd_above s) : l (Sup s) = ⨆ x : s, l x := eq.symm $ is_lub.csupr_set_eq (gc.is_lub_l_image $ is_lub_cSup hne hbdd) hne lemma l_cSup' (gc : galois_connection l u) {s : set α} (hne : s.nonempty) (hbdd : bdd_above s) : l (Sup s) = Sup (l '' s) := by rw [gc.l_cSup hne hbdd, csupr_set] lemma l_csupr (gc : galois_connection l u) {f : ι → α} (hf : bdd_above (range f)) : l (⨆ i, f i) = ⨆ i, l (f i) := by rw [supr, gc.l_cSup (range_nonempty _) hf, supr_range'] lemma l_csupr_set (gc : galois_connection l u) {s : set γ} {f : γ → α} (hf : bdd_above (f '' s)) (hne : s.nonempty) : l (⨆ i : s, f i) = ⨆ i : s, l (f i) := by { haveI := hne.to_subtype, rw image_eq_range at hf, exact gc.l_csupr hf } lemma u_cInf (gc : galois_connection l u) {s : set β} (hne : s.nonempty) (hbdd : bdd_below s) : u (Inf s) = ⨅ x : s, u x := gc.dual.l_cSup hne hbdd lemma u_cInf' (gc : galois_connection l u) {s : set β} (hne : s.nonempty) (hbdd : bdd_below s) : u (Inf s) = Inf (u '' s) := gc.dual.l_cSup' hne hbdd lemma u_cinfi (gc : galois_connection l u) {f : ι → β} (hf : bdd_below (range f)) : u (⨅ i, f i) = ⨅ i, u (f i) := gc.dual.l_csupr hf lemma u_cinfi_set (gc : galois_connection l u) {s : set γ} {f : γ → β} (hf : bdd_below (f '' s)) (hne : s.nonempty) : u (⨅ i : s, f i) = ⨅ i : s, u (f i) := gc.dual.l_csupr_set hf hne end galois_connection namespace order_iso variables {γ : Type*} [conditionally_complete_lattice α] [conditionally_complete_lattice β] [nonempty ι] lemma map_cSup (e : α ≃o β) {s : set α} (hne : s.nonempty) (hbdd : bdd_above s) : e (Sup s) = ⨆ x : s, e x := e.to_galois_connection.l_cSup hne hbdd lemma map_cSup' (e : α ≃o β) {s : set α} (hne : s.nonempty) (hbdd : bdd_above s) : e (Sup s) = Sup (e '' s) := e.to_galois_connection.l_cSup' hne hbdd lemma map_csupr (e : α ≃o β) {f : ι → α} (hf : bdd_above (range f)) : e (⨆ i, f i) = ⨆ i, e (f i) := e.to_galois_connection.l_csupr hf lemma map_csupr_set (e : α ≃o β) {s : set γ} {f : γ → α} (hf : bdd_above (f '' s)) (hne : s.nonempty) : e (⨆ i : s, f i) = ⨆ i : s, e (f i) := e.to_galois_connection.l_csupr_set hf hne lemma map_cInf (e : α ≃o β) {s : set α} (hne : s.nonempty) (hbdd : bdd_below s) : e (Inf s) = ⨅ x : s, e x := e.dual.map_cSup hne hbdd lemma map_cInf' (e : α ≃o β) {s : set α} (hne : s.nonempty) (hbdd : bdd_below s) : e (Inf s) = Inf (e '' s) := e.dual.map_cSup' hne hbdd lemma map_cinfi (e : α ≃o β) {f : ι → α} (hf : bdd_below (range f)) : e (⨅ i, f i) = ⨅ i, e (f i) := e.dual.map_csupr hf lemma map_cinfi_set (e : α ≃o β) {s : set γ} {f : γ → α} (hf : bdd_below (f '' s)) (hne : s.nonempty) : e (⨅ i : s, f i) = ⨅ i : s, e (f i) := e.dual.map_csupr_set hf hne end order_iso /-! ### Relation between `Sup` / `Inf` and `finset.sup'` / `finset.inf'` Like the `Sup` of a `conditionally_complete_lattice`, `finset.sup'` also requires the set to be non-empty. As a result, we can translate between the two. -/ namespace finset lemma sup'_eq_cSup_image [conditionally_complete_lattice β] (s : finset α) (H) (f : α → β) : s.sup' H f = Sup (f '' s) := begin apply le_antisymm, { refine (finset.sup'_le _ _ $ λ a ha, _), refine le_cSup ⟨s.sup' H f, _⟩ ⟨a, ha, rfl⟩, rintros i ⟨j, hj, rfl⟩, exact finset.le_sup' _ hj }, { apply cSup_le ((coe_nonempty.mpr H).image _), rintros _ ⟨a, ha, rfl⟩, exact finset.le_sup' _ ha, } end lemma inf'_eq_cInf_image [conditionally_complete_lattice β] (s : finset α) (H) (f : α → β) : s.inf' H f = Inf (f '' s) := @sup'_eq_cSup_image _ (order_dual β) _ _ _ _ lemma sup'_id_eq_cSup [conditionally_complete_lattice α] (s : finset α) (H) : s.sup' H id = Sup s := by rw [sup'_eq_cSup_image s H, set.image_id] lemma inf'_id_eq_cInf [conditionally_complete_lattice α] (s : finset α) (H) : s.inf' H id = Inf s := @sup'_id_eq_cSup (order_dual α) _ _ _ end finset section with_top_bot /-! ### Complete lattice structure on `with_top (with_bot α)` If `α` is a `conditionally_complete_lattice`, then we show that `with_top α` and `with_bot α` also inherit the structure of conditionally complete lattices. Furthermore, we show that `with_top (with_bot α)` naturally inherits the structure of a complete lattice. Note that for α a conditionally complete lattice, `Sup` and `Inf` both return junk values for sets which are empty or unbounded. The extension of `Sup` to `with_top α` fixes the unboundedness problem and the extension to `with_bot α` fixes the problem with the empty set. This result can be used to show that the extended reals [-∞, ∞] are a complete lattice. -/ open_locale classical /-- Adding a top element to a conditionally complete lattice gives a conditionally complete lattice -/ noncomputable instance with_top.conditionally_complete_lattice {α : Type*} [conditionally_complete_lattice α] : conditionally_complete_lattice (with_top α) := { le_cSup := λ S a hS haS, (with_top.is_lub_Sup' ⟨a, haS⟩).1 haS, cSup_le := λ S a hS haS, (with_top.is_lub_Sup' hS).2 haS, cInf_le := λ S a hS haS, (with_top.is_glb_Inf' hS).1 haS, le_cInf := λ S a hS haS, (with_top.is_glb_Inf' ⟨a, haS⟩).2 haS, ..with_top.lattice, ..with_top.has_Sup, ..with_top.has_Inf } /-- Adding a bottom element to a conditionally complete lattice gives a conditionally complete lattice -/ noncomputable instance with_bot.conditionally_complete_lattice {α : Type*} [conditionally_complete_lattice α] : conditionally_complete_lattice (with_bot α) := { le_cSup := (@with_top.conditionally_complete_lattice (order_dual α) _).cInf_le, cSup_le := (@with_top.conditionally_complete_lattice (order_dual α) _).le_cInf, cInf_le := (@with_top.conditionally_complete_lattice (order_dual α) _).le_cSup, le_cInf := (@with_top.conditionally_complete_lattice (order_dual α) _).cSup_le, ..with_bot.lattice, ..with_bot.has_Sup, ..with_bot.has_Inf } /-- Adding a bottom and a top to a conditionally complete lattice gives a bounded lattice-/ noncomputable instance with_top.with_bot.bounded_lattice {α : Type*} [conditionally_complete_lattice α] : bounded_lattice (with_top (with_bot α)) := { ..with_top.order_bot, ..with_top.order_top, ..conditionally_complete_lattice.to_lattice _ } noncomputable instance with_top.with_bot.complete_lattice {α : Type*} [conditionally_complete_lattice α] : complete_lattice (with_top (with_bot α)) := { le_Sup := λ S a haS, (with_top.is_lub_Sup' ⟨a, haS⟩).1 haS, Sup_le := λ S a ha, begin cases S.eq_empty_or_nonempty with h, { show ite _ _ _ ≤ a, split_ifs, { rw h at h_1, cases h_1 }, { convert bot_le, convert with_bot.cSup_empty, rw h, refl }, { exfalso, apply h_2, use ⊥, rw h, rintro b ⟨⟩ } }, { refine (with_top.is_lub_Sup' h).2 ha } end, Inf_le := λ S a haS, show ite _ _ _ ≤ a, begin split_ifs, { cases a with a, exact _root_.le_refl _, cases (h haS); tauto }, { cases a, { exact le_top }, { apply with_top.some_le_some.2, refine cInf_le _ haS, use ⊥, intros b hb, exact bot_le } } end, le_Inf := λ S a haS, (with_top.is_glb_Inf' ⟨a, haS⟩).2 haS, ..with_top.has_Inf, ..with_top.has_Sup, ..with_top.with_bot.bounded_lattice } noncomputable instance with_top.with_bot.complete_linear_order {α : Type*} [conditionally_complete_linear_order α] : complete_linear_order (with_top (with_bot α)) := { .. with_top.with_bot.complete_lattice, .. with_top.linear_order } end with_top_bot section subtype variables (s : set α) /-! ### Subtypes of conditionally complete linear orders In this section we give conditions on a subset of a conditionally complete linear order, to ensure that the subtype is itself conditionally complete. We check that an `ord_connected` set satisfies these conditions. TODO There are several possible variants; the `conditionally_complete_linear_order` could be changed to `conditionally_complete_linear_order_bot` or `complete_linear_order`. -/ open_locale classical section has_Sup variables [has_Sup α] /-- `has_Sup` structure on a nonempty subset `s` of an object with `has_Sup`. This definition is non-canonical (it uses `default s`); it should be used only as here, as an auxiliary instance in the construction of the `conditionally_complete_linear_order` structure. -/ noncomputable def subset_has_Sup [inhabited s] : has_Sup s := {Sup := λ t, if ht : Sup (coe '' t : set α) ∈ s then ⟨Sup (coe '' t : set α), ht⟩ else default s} local attribute [instance] subset_has_Sup @[simp] lemma subset_Sup_def [inhabited s] : @Sup s _ = λ t, if ht : Sup (coe '' t : set α) ∈ s then ⟨Sup (coe '' t : set α), ht⟩ else default s := rfl lemma subset_Sup_of_within [inhabited s] {t : set s} (h : Sup (coe '' t : set α) ∈ s) : Sup (coe '' t : set α) = (@Sup s _ t : α) := by simp [dif_pos h] end has_Sup section has_Inf variables [has_Inf α] /-- `has_Inf` structure on a nonempty subset `s` of an object with `has_Inf`. This definition is non-canonical (it uses `default s`); it should be used only as here, as an auxiliary instance in the construction of the `conditionally_complete_linear_order` structure. -/ noncomputable def subset_has_Inf [inhabited s] : has_Inf s := {Inf := λ t, if ht : Inf (coe '' t : set α) ∈ s then ⟨Inf (coe '' t : set α), ht⟩ else default s} local attribute [instance] subset_has_Inf @[simp] lemma subset_Inf_def [inhabited s] : @Inf s _ = λ t, if ht : Inf (coe '' t : set α) ∈ s then ⟨Inf (coe '' t : set α), ht⟩ else default s := rfl lemma subset_Inf_of_within [inhabited s] {t : set s} (h : Inf (coe '' t : set α) ∈ s) : Inf (coe '' t : set α) = (@Inf s _ t : α) := by simp [dif_pos h] end has_Inf variables [conditionally_complete_linear_order α] local attribute [instance] subset_has_Sup local attribute [instance] subset_has_Inf /-- For a nonempty subset of a conditionally complete linear order to be a conditionally complete linear order, it suffices that it contain the `Sup` of all its nonempty bounded-above subsets, and the `Inf` of all its nonempty bounded-below subsets. See note [reducible non-instances]. -/ @[reducible] noncomputable def subset_conditionally_complete_linear_order [inhabited s] (h_Sup : ∀ {t : set s} (ht : t.nonempty) (h_bdd : bdd_above t), Sup (coe '' t : set α) ∈ s) (h_Inf : ∀ {t : set s} (ht : t.nonempty) (h_bdd : bdd_below t), Inf (coe '' t : set α) ∈ s) : conditionally_complete_linear_order s := { le_cSup := begin rintros t c h_bdd hct, -- The following would be a more natural way to finish, but gives a "deep recursion" error: -- simpa [subset_Sup_of_within (h_Sup t)] using -- (strict_mono_coe s).monotone.le_cSup_image hct h_bdd, have := (subtype.mono_coe s).le_cSup_image hct h_bdd, rwa subset_Sup_of_within s (h_Sup ⟨c, hct⟩ h_bdd) at this, end, cSup_le := begin rintros t B ht hB, have := (subtype.mono_coe s).cSup_image_le ht hB, rwa subset_Sup_of_within s (h_Sup ht ⟨B, hB⟩) at this, end, le_cInf := begin intros t B ht hB, have := (subtype.mono_coe s).le_cInf_image ht hB, rwa subset_Inf_of_within s (h_Inf ht ⟨B, hB⟩) at this, end, cInf_le := begin rintros t c h_bdd hct, have := (subtype.mono_coe s).cInf_image_le hct h_bdd, rwa subset_Inf_of_within s (h_Inf ⟨c, hct⟩ h_bdd) at this, end, ..subset_has_Sup s, ..subset_has_Inf s, ..distrib_lattice.to_lattice s, ..(infer_instance : linear_order s) } section ord_connected /-- The `Sup` function on a nonempty `ord_connected` set `s` in a conditionally complete linear order takes values within `s`, for all nonempty bounded-above subsets of `s`. -/ lemma Sup_within_of_ord_connected {s : set α} [hs : ord_connected s] ⦃t : set s⦄ (ht : t.nonempty) (h_bdd : bdd_above t) : Sup (coe '' t : set α) ∈ s := begin obtain ⟨c, hct⟩ : ∃ c, c ∈ t := ht, obtain ⟨B, hB⟩ : ∃ B, B ∈ upper_bounds t := h_bdd, refine hs.out c.2 B.2 ⟨_, _⟩, { exact (subtype.mono_coe s).le_cSup_image hct ⟨B, hB⟩ }, { exact (subtype.mono_coe s).cSup_image_le ⟨c, hct⟩ hB }, end /-- The `Inf` function on a nonempty `ord_connected` set `s` in a conditionally complete linear order takes values within `s`, for all nonempty bounded-below subsets of `s`. -/ lemma Inf_within_of_ord_connected {s : set α} [hs : ord_connected s] ⦃t : set s⦄ (ht : t.nonempty) (h_bdd : bdd_below t) : Inf (coe '' t : set α) ∈ s := begin obtain ⟨c, hct⟩ : ∃ c, c ∈ t := ht, obtain ⟨B, hB⟩ : ∃ B, B ∈ lower_bounds t := h_bdd, refine hs.out B.2 c.2 ⟨_, _⟩, { exact (subtype.mono_coe s).le_cInf_image ⟨c, hct⟩ hB }, { exact (subtype.mono_coe s).cInf_image_le hct ⟨B, hB⟩ }, end /-- A nonempty `ord_connected` set in a conditionally complete linear order is naturally a conditionally complete linear order. -/ noncomputable instance ord_connected_subset_conditionally_complete_linear_order [inhabited s] [ord_connected s] : conditionally_complete_linear_order s := subset_conditionally_complete_linear_order s Sup_within_of_ord_connected Inf_within_of_ord_connected end ord_connected end subtype
36565f9fce712bbf6b671a1a0b42cb420e6ae5d3
1a61aba1b67cddccce19532a9596efe44be4285f
/library/algebra/ring.lean
c62089bd8c2322d71e4073710b761eb769537748
[ "Apache-2.0" ]
permissive
eigengrau/lean
07986a0f2548688c13ba36231f6cdbee82abf4c6
f8a773be1112015e2d232661ce616d23f12874d0
refs/heads/master
1,610,939,198,566
1,441,352,386,000
1,441,352,494,000
41,903,576
0
0
null
1,441,352,210,000
1,441,352,210,000
null
UTF-8
Lean
false
false
14,110
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura Structures with multiplicative and additive components, including semirings, rings, and fields. The development is modeled after Isabelle's library. -/ import logic.eq logic.connectives data.unit data.sigma data.prod import algebra.binary algebra.group open eq eq.ops namespace algebra variable {A : Type} /- auxiliary classes -/ structure distrib [class] (A : Type) extends has_mul A, has_add A := (left_distrib : ∀a b c, mul a (add b c) = add (mul a b) (mul a c)) (right_distrib : ∀a b c, mul (add a b) c = add (mul a c) (mul b c)) theorem left_distrib [s : distrib A] (a b c : A) : a * (b + c) = a * b + a * c := !distrib.left_distrib theorem right_distrib [s: distrib A] (a b c : A) : (a + b) * c = a * c + b * c := !distrib.right_distrib structure mul_zero_class [class] (A : Type) extends has_mul A, has_zero A := (zero_mul : ∀a, mul zero a = zero) (mul_zero : ∀a, mul a zero = zero) theorem zero_mul [s : mul_zero_class A] (a : A) : 0 * a = 0 := !mul_zero_class.zero_mul theorem mul_zero [s : mul_zero_class A] (a : A) : a * 0 = 0 := !mul_zero_class.mul_zero structure zero_ne_one_class [class] (A : Type) extends has_zero A, has_one A := (zero_ne_one : zero ≠ one) theorem zero_ne_one [s: zero_ne_one_class A] : 0 ≠ (1:A) := @zero_ne_one_class.zero_ne_one A s /- semiring -/ structure semiring [class] (A : Type) extends add_comm_monoid A, monoid A, distrib A, mul_zero_class A section semiring variables [s : semiring A] (a b c : A) include s theorem ne_zero_of_mul_ne_zero_right {a b : A} (H : a * b ≠ 0) : a ≠ 0 := suppose a = 0, have a * b = 0, from this⁻¹ ▸ zero_mul b, H this theorem ne_zero_of_mul_ne_zero_left {a b : A} (H : a * b ≠ 0) : b ≠ 0 := suppose b = 0, have a * b = 0, from this⁻¹ ▸ mul_zero a, H this theorem distrib_three_right (a b c d : A) : (a + b + c) * d = a * d + b * d + c * d := by rewrite *right_distrib end semiring /- comm semiring -/ structure comm_semiring [class] (A : Type) extends semiring A, comm_monoid A -- TODO: we could also define a cancelative comm_semiring, i.e. satisfying -- c ≠ 0 → c * a = c * b → a = b. section comm_semiring variables [s : comm_semiring A] (a b c : A) include s definition dvd (a b : A) : Prop := ∃c, b = a * c notation [priority algebra.prio] a ∣ b := dvd a b theorem dvd.intro {a b c : A} (H : a * c = b) : a ∣ b := exists.intro _ H⁻¹ theorem dvd_of_mul_right_eq {a b c : A} (H : a * c = b) : a ∣ b := dvd.intro H theorem dvd.intro_left {a b c : A} (H : c * a = b) : a ∣ b := dvd.intro (!mul.comm ▸ H) theorem dvd_of_mul_left_eq {a b c : A} (H : c * a = b) : a ∣ b := dvd.intro_left H theorem exists_eq_mul_right_of_dvd {a b : A} (H : a ∣ b) : ∃c, b = a * c := H theorem dvd.elim {P : Prop} {a b : A} (H₁ : a ∣ b) (H₂ : ∀c, b = a * c → P) : P := exists.elim H₁ H₂ theorem exists_eq_mul_left_of_dvd {a b : A} (H : a ∣ b) : ∃c, b = c * a := dvd.elim H (take c, assume H1 : b = a * c, exists.intro c (H1 ⬝ !mul.comm)) theorem dvd.elim_left {P : Prop} {a b : A} (H₁ : a ∣ b) (H₂ : ∀c, b = c * a → P) : P := exists.elim (exists_eq_mul_left_of_dvd H₁) (take c, assume H₃ : b = c * a, H₂ c H₃) theorem dvd.refl : a ∣ a := dvd.intro !mul_one theorem dvd.trans {a b c : A} (H₁ : a ∣ b) (H₂ : b ∣ c) : a ∣ c := dvd.elim H₁ (take d, assume H₃ : b = a * d, dvd.elim H₂ (take e, assume H₄ : c = b * e, dvd.intro (show a * (d * e) = c, by rewrite [-mul.assoc, -H₃, H₄]))) theorem eq_zero_of_zero_dvd {a : A} (H : 0 ∣ a) : a = 0 := dvd.elim H (take c, assume H' : a = 0 * c, H' ⬝ !zero_mul) theorem dvd_zero : a ∣ 0 := dvd.intro !mul_zero theorem one_dvd : 1 ∣ a := dvd.intro !one_mul theorem dvd_mul_right : a ∣ a * b := dvd.intro rfl theorem dvd_mul_left : a ∣ b * a := mul.comm a b ▸ dvd_mul_right a b theorem dvd_mul_of_dvd_left {a b : A} (H : a ∣ b) (c : A) : a ∣ b * c := dvd.elim H (take d, suppose b = a * d, dvd.intro (show a * (d * c) = b * c, from by rewrite [-mul.assoc]; substvars)) theorem dvd_mul_of_dvd_right {a b : A} (H : a ∣ b) (c : A) : a ∣ c * b := !mul.comm ▸ (dvd_mul_of_dvd_left H _) theorem mul_dvd_mul {a b c d : A} (dvd_ab : a ∣ b) (dvd_cd : c ∣ d) : a * c ∣ b * d := dvd.elim dvd_ab (take e, suppose b = a * e, dvd.elim dvd_cd (take f, suppose d = c * f, dvd.intro (show a * c * (e * f) = b * d, by rewrite [mul.assoc, {c*_}mul.left_comm, -mul.assoc]; substvars))) theorem dvd_of_mul_right_dvd {a b c : A} (H : a * b ∣ c) : a ∣ c := dvd.elim H (take d, assume Habdc : c = a * b * d, dvd.intro (!mul.assoc⁻¹ ⬝ Habdc⁻¹)) theorem dvd_of_mul_left_dvd {a b c : A} (H : a * b ∣ c) : b ∣ c := dvd_of_mul_right_dvd (mul.comm a b ▸ H) theorem dvd_add {a b c : A} (Hab : a ∣ b) (Hac : a ∣ c) : a ∣ b + c := dvd.elim Hab (take d, suppose b = a * d, dvd.elim Hac (take e, suppose c = a * e, dvd.intro (show a * (d + e) = b + c, by rewrite [left_distrib]; substvars))) end comm_semiring /- ring -/ structure ring [class] (A : Type) extends add_comm_group A, monoid A, distrib A theorem ring.mul_zero [s : ring A] (a : A) : a * 0 = 0 := have a * 0 + 0 = a * 0 + a * 0, from calc a * 0 + 0 = a * 0 : by rewrite add_zero ... = a * (0 + 0) : by rewrite add_zero ... = a * 0 + a * 0 : by rewrite {a*_}ring.left_distrib, show a * 0 = 0, from (add.left_cancel this)⁻¹ theorem ring.zero_mul [s : ring A] (a : A) : 0 * a = 0 := have 0 * a + 0 = 0 * a + 0 * a, from calc 0 * a + 0 = 0 * a : by rewrite add_zero ... = (0 + 0) * a : by rewrite add_zero ... = 0 * a + 0 * a : by rewrite {_*a}ring.right_distrib, show 0 * a = 0, from (add.left_cancel this)⁻¹ definition ring.to_semiring [trans-instance] [coercion] [reducible] [s : ring A] : semiring A := ⦃ semiring, s, mul_zero := ring.mul_zero, zero_mul := ring.zero_mul ⦄ section variables [s : ring A] (a b c d e : A) include s theorem neg_mul_eq_neg_mul : -(a * b) = -a * b := neg_eq_of_add_eq_zero begin rewrite [-right_distrib, add.right_inv, zero_mul] end theorem neg_mul_eq_mul_neg : -(a * b) = a * -b := neg_eq_of_add_eq_zero begin rewrite [-left_distrib, add.right_inv, mul_zero] end theorem neg_mul_neg : -a * -b = a * b := calc -a * -b = -(a * -b) : by rewrite -neg_mul_eq_neg_mul ... = - -(a * b) : by rewrite -neg_mul_eq_mul_neg ... = a * b : by rewrite neg_neg theorem neg_mul_comm : -a * b = a * -b := !neg_mul_eq_neg_mul⁻¹ ⬝ !neg_mul_eq_mul_neg theorem neg_eq_neg_one_mul : -a = -1 * a := calc -a = -(1 * a) : by rewrite one_mul ... = -1 * a : by rewrite neg_mul_eq_neg_mul theorem mul_sub_left_distrib : a * (b - c) = a * b - a * c := calc a * (b - c) = a * b + a * -c : left_distrib ... = a * b + - (a * c) : by rewrite -neg_mul_eq_mul_neg ... = a * b - a * c : rfl theorem mul_sub_right_distrib : (a - b) * c = a * c - b * c := calc (a - b) * c = a * c + -b * c : right_distrib ... = a * c + - (b * c) : by rewrite neg_mul_eq_neg_mul ... = a * c - b * c : rfl -- TODO: can calc mode be improved to make this easier? -- TODO: there is also the other direction. It will be easier when we -- have the simplifier. theorem mul_add_eq_mul_add_iff_sub_mul_add_eq : a * e + c = b * e + d ↔ (a - b) * e + c = d := calc a * e + c = b * e + d ↔ a * e + c = d + b * e : by rewrite {b*e+_}add.comm ... ↔ a * e + c - b * e = d : iff.symm !sub_eq_iff_eq_add ... ↔ a * e - b * e + c = d : by rewrite sub_add_eq_add_sub ... ↔ (a - b) * e + c = d : by rewrite mul_sub_right_distrib theorem mul_add_eq_mul_add_of_sub_mul_add_eq : (a - b) * e + c = d → a * e + c = b * e + d := iff.mpr !mul_add_eq_mul_add_iff_sub_mul_add_eq theorem sub_mul_add_eq_of_mul_add_eq_mul_add : a * e + c = b * e + d → (a - b) * e + c = d := iff.mp !mul_add_eq_mul_add_iff_sub_mul_add_eq theorem mul_neg_one_eq_neg : a * (-1) = -a := have a + a * -1 = 0, from calc a + a * -1 = a * 1 + a * -1 : mul_one ... = a * (1 + -1) : left_distrib ... = a * 0 : add.right_inv ... = 0 : mul_zero, symm (neg_eq_of_add_eq_zero this) theorem ne_zero_and_ne_zero_of_mul_ne_zero {a b : A} (H : a * b ≠ 0) : a ≠ 0 ∧ b ≠ 0 := have a ≠ 0, from (suppose a = 0, have a * b = 0, by rewrite [this, zero_mul], absurd this H), have b ≠ 0, from (suppose b = 0, have a * b = 0, by rewrite [this, mul_zero], absurd this H), and.intro `a ≠ 0` `b ≠ 0` end structure comm_ring [class] (A : Type) extends ring A, comm_semigroup A definition comm_ring.to_comm_semiring [trans-instance] [coercion] [reducible] [s : comm_ring A] : comm_semiring A := ⦃ comm_semiring, s, mul_zero := mul_zero, zero_mul := zero_mul ⦄ section variables [s : comm_ring A] (a b c d e : A) include s theorem mul_self_sub_mul_self_eq : a * a - b * b = (a + b) * (a - b) := begin krewrite [left_distrib, *right_distrib, add.assoc], rewrite [-{b*a + _}add.assoc, -*neg_mul_eq_mul_neg, {a*b}mul.comm, add.right_inv, zero_add] end theorem mul_self_sub_one_eq : a * a - 1 = (a + 1) * (a - 1) := by rewrite [-mul_self_sub_mul_self_eq, mul_one] theorem dvd_neg_iff_dvd : (a ∣ -b) ↔ (a ∣ b) := iff.intro (suppose a ∣ -b, dvd.elim this (take c, suppose -b = a * c, dvd.intro (show a * -c = b, by rewrite [-neg_mul_eq_mul_neg, -this, neg_neg]))) (suppose a ∣ b, dvd.elim this (take c, suppose b = a * c, dvd.intro (show a * -c = -b, by rewrite [-neg_mul_eq_mul_neg, -this]))) theorem dvd_neg_of_dvd : (a ∣ b) → (a ∣ -b) := iff.mpr !dvd_neg_iff_dvd theorem dvd_of_dvd_neg : (a ∣ -b) → (a ∣ b) := iff.mp !dvd_neg_iff_dvd theorem neg_dvd_iff_dvd : (-a ∣ b) ↔ (a ∣ b) := iff.intro (suppose -a ∣ b, dvd.elim this (take c, suppose b = -a * c, dvd.intro (show a * -c = b, by rewrite [-neg_mul_comm, this]))) (suppose a ∣ b, dvd.elim this (take c, suppose b = a * c, dvd.intro (show -a * -c = b, by rewrite [neg_mul_neg, this]))) theorem neg_dvd_of_dvd : (a ∣ b) → (-a ∣ b) := iff.mpr !neg_dvd_iff_dvd theorem dvd_of_neg_dvd : (-a ∣ b) → (a ∣ b) := iff.mp !neg_dvd_iff_dvd theorem dvd_sub (H₁ : (a ∣ b)) (H₂ : (a ∣ c)) : (a ∣ b - c) := dvd_add H₁ (!dvd_neg_of_dvd H₂) end /- integral domains -/ structure no_zero_divisors [class] (A : Type) extends has_mul A, has_zero A := (eq_zero_or_eq_zero_of_mul_eq_zero : ∀a b, mul a b = zero → a = zero ∨ b = zero) theorem eq_zero_or_eq_zero_of_mul_eq_zero {A : Type} [s : no_zero_divisors A] {a b : A} (H : a * b = 0) : a = 0 ∨ b = 0 := !no_zero_divisors.eq_zero_or_eq_zero_of_mul_eq_zero H structure integral_domain [class] (A : Type) extends comm_ring A, no_zero_divisors A, zero_ne_one_class A section variables [s : integral_domain A] (a b c d e : A) include s theorem mul_ne_zero {a b : A} (H1 : a ≠ 0) (H2 : b ≠ 0) : a * b ≠ 0 := suppose a * b = 0, or.elim (eq_zero_or_eq_zero_of_mul_eq_zero this) (assume H3, H1 H3) (assume H4, H2 H4) theorem eq_of_mul_eq_mul_right {a b c : A} (Ha : a ≠ 0) (H : b * a = c * a) : b = c := have b * a - c * a = 0, from iff.mp !eq_iff_sub_eq_zero H, have (b - c) * a = 0, using this, by rewrite [mul_sub_right_distrib, this], have b - c = 0, from or_resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero this) Ha, iff.elim_right !eq_iff_sub_eq_zero this theorem eq_of_mul_eq_mul_left {a b c : A} (Ha : a ≠ 0) (H : a * b = a * c) : b = c := have a * b - a * c = 0, from iff.mp !eq_iff_sub_eq_zero H, have a * (b - c) = 0, using this, by rewrite [mul_sub_left_distrib, this], have b - c = 0, from or_resolve_right (eq_zero_or_eq_zero_of_mul_eq_zero this) Ha, iff.elim_right !eq_iff_sub_eq_zero this -- TODO: do we want the iff versions? theorem mul_self_eq_mul_self_iff (a b : A) : a * a = b * b ↔ a = b ∨ a = -b := iff.intro (suppose a * a = b * b, have (a - b) * (a + b) = 0, by rewrite [mul.comm, -mul_self_sub_mul_self_eq, this, sub_self], assert a - b = 0 ∨ a + b = 0, from !eq_zero_or_eq_zero_of_mul_eq_zero this, or.elim this (suppose a - b = 0, or.inl (eq_of_sub_eq_zero this)) (suppose a + b = 0, or.inr (eq_neg_of_add_eq_zero this))) (suppose a = b ∨ a = -b, or.elim this (suppose a = b, by rewrite this) (suppose a = -b, by rewrite [this, neg_mul_neg])) theorem mul_self_eq_one_iff (a : A) : a * a = 1 ↔ a = 1 ∨ a = -1 := assert a * a = 1 * 1 ↔ a = 1 ∨ a = -1, from mul_self_eq_mul_self_iff a 1, by rewrite mul_one at this; exact this -- TODO: c - b * c → c = 0 ∨ b = 1 and variants theorem dvd_of_mul_dvd_mul_left {a b c : A} (Ha : a ≠ 0) (Hdvd : (a * b ∣ a * c)) : (b ∣ c) := dvd.elim Hdvd (take d, suppose a * c = a * b * d, have b * d = c, from eq_of_mul_eq_mul_left Ha (mul.assoc a b d ▸ this⁻¹), dvd.intro this) theorem dvd_of_mul_dvd_mul_right {a b c : A} (Ha : a ≠ 0) (Hdvd : (b * a ∣ c * a)) : (b ∣ c) := dvd.elim Hdvd (take d, suppose c * a = b * a * d, have b * d * a = c * a, from by rewrite [mul.right_comm, -this], have b * d = c, from eq_of_mul_eq_mul_right Ha this, dvd.intro this) end end algebra
3412803c3cd16713f97632d1f7004d0824f2c055
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/library/tools/helper_tactics.lean
cba7c953dcb0fb59b1e97b5f3eba883b5a38101c
[ "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
404
lean
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Leonardo de Moura, Jeremy Avigad -- tools.helper_tactics -- ==================== -- Useful tactics. import tools.tactic logic.eq open tactic namespace helper_tactics definition apply_refl := apply eq.refl tactic_hint apply_refl end helper_tactics
87be54f02b7a7d89afae989a6bbe72d863e49c5e
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/group_theory/group_action/units.lean
108b93c18e669766184274e8efc48ac83192acb7
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
5,775
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import group_theory.group_action.defs /-! # Group actions on and by `Mˣ` This file provides the action of a unit on a type `α`, `has_smul Mˣ α`, in the presence of `has_smul M α`, with the obvious definition stated in `units.smul_def`. This definition preserves `mul_action` and `distrib_mul_action` structures too. Additionally, a `mul_action G M` for some group `G` satisfying some additional properties admits a `mul_action G Mˣ` structure, again with the obvious definition stated in `units.coe_smul`. These instances use a primed name. The results are repeated for `add_units` and `has_vadd` where relevant. -/ variables {G H M N : Type*} {α : Type*} namespace units /-! ### Action of the units of `M` on a type `α` -/ @[to_additive] instance [monoid M] [has_smul M α] : has_smul Mˣ α := { smul := λ m a, (m : M) • a } @[to_additive] lemma smul_def [monoid M] [has_smul M α] (m : Mˣ) (a : α) : m • a = (m : M) • a := rfl @[simp] lemma smul_is_unit [monoid M] [has_smul M α] {m : M} (hm : is_unit m) (a : α) : hm.unit • a = m • a := rfl lemma _root_.is_unit.inv_smul [monoid α] {a : α} (h : is_unit a) : (h.unit)⁻¹ • a = 1 := h.coe_inv_mul @[to_additive] instance [monoid M] [has_smul M α] [has_faithful_smul M α] : has_faithful_smul Mˣ α := { eq_of_smul_eq_smul := λ u₁ u₂ h, units.ext $ eq_of_smul_eq_smul h, } @[to_additive] instance [monoid M] [mul_action M α] : mul_action Mˣ α := { one_smul := (one_smul M : _), mul_smul := λ m n, mul_smul (m : M) n, } instance [monoid M] [has_zero α] [smul_zero_class M α] : smul_zero_class Mˣ α := { smul := (•), smul_zero := λ m, smul_zero m } instance [monoid M] [add_zero_class α] [distrib_smul M α] : distrib_smul Mˣ α := { smul_add := λ m, smul_add (m : M) } instance [monoid M] [add_monoid α] [distrib_mul_action M α] : distrib_mul_action Mˣ α := { .. units.distrib_smul } instance [monoid M] [monoid α] [mul_distrib_mul_action M α] : mul_distrib_mul_action Mˣ α := { smul_mul := λ m, smul_mul' (m : M), smul_one := λ m, smul_one m, } instance smul_comm_class_left [monoid M] [has_smul M α] [has_smul N α] [smul_comm_class M N α] : smul_comm_class Mˣ N α := { smul_comm := λ m n, (smul_comm (m : M) n : _)} instance smul_comm_class_right [monoid N] [has_smul M α] [has_smul N α] [smul_comm_class M N α] : smul_comm_class M Nˣ α := { smul_comm := λ m n, (smul_comm m (n : N) : _)} instance [monoid M] [has_smul M N] [has_smul M α] [has_smul N α] [is_scalar_tower M N α] : is_scalar_tower Mˣ N α := { smul_assoc := λ m n, (smul_assoc (m : M) n : _)} /-! ### Action of a group `G` on units of `M` -/ /-- If an action `G` associates and commutes with multiplication on `M`, then it lifts to an action on `Mˣ`. Notably, this provides `mul_action Mˣ Nˣ` under suitable conditions. -/ instance mul_action' [group G] [monoid M] [mul_action G M] [smul_comm_class G M M] [is_scalar_tower G M M] : mul_action G Mˣ := { smul := λ g m, ⟨g • (m : M), g⁻¹ • ↑(m⁻¹), by rw [smul_mul_smul, units.mul_inv, mul_right_inv, one_smul], by rw [smul_mul_smul, units.inv_mul, mul_left_inv, one_smul]⟩, one_smul := λ m, units.ext $ one_smul _ _, mul_smul := λ g₁ g₂ m, units.ext $ mul_smul _ _ _ } @[simp] lemma coe_smul [group G] [monoid M] [mul_action G M] [smul_comm_class G M M] [is_scalar_tower G M M] (g : G) (m : Mˣ) : ↑(g • m) = g • (m : M) := rfl /-- Note that this lemma exists more generally as the global `smul_inv` -/ @[simp] lemma smul_inv [group G] [monoid M] [mul_action G M] [smul_comm_class G M M] [is_scalar_tower G M M] (g : G) (m : Mˣ) : (g • m)⁻¹ = g⁻¹ • m⁻¹ := ext rfl /-- Transfer `smul_comm_class G H M` to `smul_comm_class G H Mˣ` -/ instance smul_comm_class' [group G] [group H] [monoid M] [mul_action G M] [smul_comm_class G M M] [mul_action H M] [smul_comm_class H M M] [is_scalar_tower G M M] [is_scalar_tower H M M] [smul_comm_class G H M] : smul_comm_class G H Mˣ := { smul_comm := λ g h m, units.ext $ smul_comm g h (m : M) } /-- Transfer `is_scalar_tower G H M` to `is_scalar_tower G H Mˣ` -/ instance is_scalar_tower' [has_smul G H] [group G] [group H] [monoid M] [mul_action G M] [smul_comm_class G M M] [mul_action H M] [smul_comm_class H M M] [is_scalar_tower G M M] [is_scalar_tower H M M] [is_scalar_tower G H M] : is_scalar_tower G H Mˣ := { smul_assoc := λ g h m, units.ext $ smul_assoc g h (m : M) } /-- Transfer `is_scalar_tower G M α` to `is_scalar_tower G Mˣ α` -/ instance is_scalar_tower'_left [group G] [monoid M] [mul_action G M] [has_smul M α] [has_smul G α] [smul_comm_class G M M] [is_scalar_tower G M M] [is_scalar_tower G M α] : is_scalar_tower G Mˣ α := { smul_assoc := λ g m, (smul_assoc g (m : M) : _)} -- Just to prove this transfers a particularly useful instance. example [monoid M] [monoid N] [mul_action M N] [smul_comm_class M N N] [is_scalar_tower M N N] : mul_action Mˣ Nˣ := units.mul_action' /-- A stronger form of `units.mul_action'`. -/ instance mul_distrib_mul_action' [group G] [monoid M] [mul_distrib_mul_action G M] [smul_comm_class G M M] [is_scalar_tower G M M] : mul_distrib_mul_action G Mˣ := { smul := (•), smul_one := λ m, units.ext $ smul_one _, smul_mul := λ g m₁ m₂, units.ext $ smul_mul' _ _ _, .. units.mul_action' } end units lemma is_unit.smul [group G] [monoid M] [mul_action G M] [smul_comm_class G M M] [is_scalar_tower G M M] {m : M} (g : G) (h : is_unit m) : is_unit (g • m) := let ⟨u, hu⟩ := h in hu ▸ ⟨g • u, units.coe_smul _ _⟩
8d443587487b5fa4eb166ec53919a795c890e2a7
bb31430994044506fa42fd667e2d556327e18dfe
/src/ring_theory/subring/basic.lean
54ea6c2f32073b88644872adb0acef3a8dbeb261
[ "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
47,477
lean
/- Copyright (c) 2020 Ashvni Narayanan. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ashvni Narayanan -/ import group_theory.subgroup.basic import ring_theory.subsemiring.basic /-! # Subrings Let `R` be a ring. This file defines the "bundled" subring type `subring R`, a type whose terms correspond to subrings of `R`. This is the preferred way to talk about subrings in mathlib. Unbundled subrings (`s : set R` and `is_subring s`) are not in this file, and they will ultimately be deprecated. We prove that subrings are a complete lattice, and that you can `map` (pushforward) and `comap` (pull back) them along ring homomorphisms. We define the `closure` construction from `set R` to `subring R`, sending a subset of `R` to the subring it generates, and prove that it is a Galois insertion. ## Main definitions Notation used here: `(R : Type u) [ring R] (S : Type u) [ring S] (f g : R →+* S)` `(A : subring R) (B : subring S) (s : set R)` * `subring R` : the type of subrings of a ring `R`. * `instance : complete_lattice (subring R)` : the complete lattice structure on the subrings. * `subring.center` : the center of a ring `R`. * `subring.closure` : subring closure of a set, i.e., the smallest subring that includes the set. * `subring.gi` : `closure : set M → subring M` and coercion `coe : subring M → set M` form a `galois_insertion`. * `comap f B : subring A` : the preimage of a subring `B` along the ring homomorphism `f` * `map f A : subring B` : the image of a subring `A` along the ring homomorphism `f`. * `prod A B : subring (R × S)` : the product of subrings * `f.range : subring B` : the range of the ring homomorphism `f`. * `eq_locus f g : subring R` : given ring homomorphisms `f g : R →+* S`, the subring of `R` where `f x = g x` ## Implementation notes A subring is implemented as a subsemiring which is also an additive subgroup. The initial PR was as a submonoid which is also an additive subgroup. Lattice inclusion (e.g. `≤` and `⊓`) is used rather than set notation (`⊆` and `∩`), although `∈` is defined as membership of a subring's underlying set. ## Tags subring, subrings -/ open_locale big_operators universes u v w variables {R : Type u} {S : Type v} {T : Type w} [ring R] section subring_class /-- `subring_class S R` states that `S` is a type of subsets `s ⊆ R` that are both a multiplicative submonoid and an additive subgroup. -/ class subring_class (S : Type*) (R : out_param $ Type u) [ring R] [set_like S R] extends subsemiring_class S R, neg_mem_class S R : Prop @[priority 100] -- See note [lower instance priority] instance subring_class.add_subgroup_class (S : Type*) (R : out_param $ Type u) [set_like S R] [ring R] [h : subring_class S R] : add_subgroup_class S R := { .. h } variables [set_like S R] [hSR : subring_class S R] (s : S) include hSR lemma coe_int_mem (n : ℤ) : (n : R) ∈ s := by simp only [← zsmul_one, zsmul_mem, one_mem] namespace subring_class @[priority 75] instance to_has_int_cast : has_int_cast s := ⟨λ n, ⟨n, coe_int_mem s n⟩⟩ /-- A subring of a ring inherits a ring structure -/ @[priority 75] -- Prefer subclasses of `ring` over subclasses of `subring_class`. instance to_ring : ring s := subtype.coe_injective.ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) omit hSR /-- A subring of a `comm_ring` is a `comm_ring`. -/ @[priority 75] -- Prefer subclasses of `ring` over subclasses of `subring_class`. instance to_comm_ring {R} [comm_ring R] [set_like S R] [subring_class S R] : comm_ring s := subtype.coe_injective.comm_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) /-- A subring of a domain is a domain. -/ @[priority 75] -- Prefer subclasses of `ring` over subclasses of `subring_class`. instance {R} [ring R] [is_domain R] [set_like S R] [subring_class S R] : is_domain s := no_zero_divisors.to_is_domain _ /-- A subring of an `ordered_ring` is an `ordered_ring`. -/ @[priority 75] -- Prefer subclasses of `ring` over subclasses of `subring_class`. instance to_ordered_ring {R} [ordered_ring R] [set_like S R] [subring_class S R] : ordered_ring s := subtype.coe_injective.ordered_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) /-- A subring of an `ordered_comm_ring` is an `ordered_comm_ring`. -/ @[priority 75] -- Prefer subclasses of `ring` over subclasses of `subring_class`. instance to_ordered_comm_ring {R} [ordered_comm_ring R] [set_like S R] [subring_class S R] : ordered_comm_ring s := subtype.coe_injective.ordered_comm_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) /-- A subring of a `linear_ordered_ring` is a `linear_ordered_ring`. -/ @[priority 75] -- Prefer subclasses of `ring` over subclasses of `subring_class`. instance to_linear_ordered_ring {R} [linear_ordered_ring R] [set_like S R] [subring_class S R] : linear_ordered_ring s := subtype.coe_injective.linear_ordered_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A subring of a `linear_ordered_comm_ring` is a `linear_ordered_comm_ring`. -/ @[priority 75] -- Prefer subclasses of `ring` over subclasses of `subring_class`. instance to_linear_ordered_comm_ring {R} [linear_ordered_comm_ring R] [set_like S R] [subring_class S R] : linear_ordered_comm_ring s := subtype.coe_injective.linear_ordered_comm_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) include hSR /-- The natural ring hom from a subring of ring `R` to `R`. -/ def subtype (s : S) : s →+* R := { to_fun := coe, .. submonoid_class.subtype s, .. add_subgroup_class.subtype s } @[simp] theorem coe_subtype : (subtype s : s → R) = coe := rfl @[simp, norm_cast] lemma coe_nat_cast (n : ℕ) : ((n : s) : R) = n := map_nat_cast (subtype s) n @[simp, norm_cast] lemma coe_int_cast (n : ℤ) : ((n : s) : R) = n := map_int_cast (subtype s) n end subring_class end subring_class variables [ring S] [ring T] set_option old_structure_cmd true /-- `subring R` is the type of subrings of `R`. A subring of `R` is a subset `s` that is a multiplicative submonoid and an additive subgroup. Note in particular that it shares the same 0 and 1 as R. -/ structure subring (R : Type u) [ring R] extends subsemiring R, add_subgroup R /-- Reinterpret a `subring` as a `subsemiring`. -/ add_decl_doc subring.to_subsemiring /-- Reinterpret a `subring` as an `add_subgroup`. -/ add_decl_doc subring.to_add_subgroup namespace subring /-- The underlying submonoid of a subring. -/ def to_submonoid (s : subring R) : submonoid R := { carrier := s.carrier, ..s.to_subsemiring.to_submonoid } instance : set_like (subring R) R := { coe := subring.carrier, coe_injective' := λ p q h, by cases p; cases q; congr' } instance : subring_class (subring R) R := { zero_mem := zero_mem', add_mem := add_mem', one_mem := one_mem', mul_mem := mul_mem', neg_mem := neg_mem' } @[simp] lemma mem_carrier {s : subring R} {x : R} : x ∈ s.carrier ↔ x ∈ s := iff.rfl @[simp] lemma mem_mk {S : set R} {x : R} (h₁ h₂ h₃ h₄ h₅) : x ∈ (⟨S, h₁, h₂, h₃, h₄, h₅⟩ : subring R) ↔ x ∈ S := iff.rfl @[simp] lemma coe_set_mk (S : set R) (h₁ h₂ h₃ h₄ h₅) : ((⟨S, h₁, h₂, h₃, h₄, h₅⟩ : subring R) : set R) = S := rfl @[simp] lemma mk_le_mk {S S' : set R} (h₁ h₂ h₃ h₄ h₅ h₁' h₂' h₃' h₄' h₅') : (⟨S, h₁, h₂, h₃, h₄, h₅⟩ : subring R) ≤ (⟨S', h₁', h₂', h₃', h₄', h₅'⟩ : subring R) ↔ S ⊆ S' := iff.rfl /-- Two subrings are equal if they have the same elements. -/ @[ext] theorem ext {S T : subring R} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h /-- Copy of a subring with a new `carrier` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (S : subring R) (s : set R) (hs : s = ↑S) : subring R := { carrier := s, neg_mem' := λ _, hs.symm ▸ S.neg_mem', ..S.to_subsemiring.copy s hs } @[simp] lemma coe_copy (S : subring R) (s : set R) (hs : s = ↑S) : (S.copy s hs : set R) = s := rfl lemma copy_eq (S : subring R) (s : set R) (hs : s = ↑S) : S.copy s hs = S := set_like.coe_injective hs lemma to_subsemiring_injective : function.injective (to_subsemiring : subring R → subsemiring R) | r s h := ext (set_like.ext_iff.mp h : _) @[mono] lemma to_subsemiring_strict_mono : strict_mono (to_subsemiring : subring R → subsemiring R) := λ _ _, id @[mono] lemma to_subsemiring_mono : monotone (to_subsemiring : subring R → subsemiring R) := to_subsemiring_strict_mono.monotone lemma to_add_subgroup_injective : function.injective (to_add_subgroup : subring R → add_subgroup R) | r s h := ext (set_like.ext_iff.mp h : _) @[mono] lemma to_add_subgroup_strict_mono : strict_mono (to_add_subgroup : subring R → add_subgroup R) := λ _ _, id @[mono] lemma to_add_subgroup_mono : monotone (to_add_subgroup : subring R → add_subgroup R) := to_add_subgroup_strict_mono.monotone lemma to_submonoid_injective : function.injective (to_submonoid : subring R → submonoid R) | r s h := ext (set_like.ext_iff.mp h : _) @[mono] lemma to_submonoid_strict_mono : strict_mono (to_submonoid : subring R → submonoid R) := λ _ _, id @[mono] lemma to_submonoid_mono : monotone (to_submonoid : subring R → submonoid R) := to_submonoid_strict_mono.monotone /-- Construct a `subring R` from a set `s`, a submonoid `sm`, and an additive subgroup `sa` such that `x ∈ s ↔ x ∈ sm ↔ x ∈ sa`. -/ protected def mk' (s : set R) (sm : submonoid R) (sa : add_subgroup R) (hm : ↑sm = s) (ha : ↑sa = s) : subring R := { carrier := s, zero_mem' := ha ▸ sa.zero_mem, one_mem' := hm ▸ sm.one_mem, add_mem' := λ x y, by simpa only [← ha] using sa.add_mem, mul_mem' := λ x y, by simpa only [← hm] using sm.mul_mem, neg_mem' := λ x, by simpa only [← ha] using sa.neg_mem, } @[simp] lemma coe_mk' {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) : (subring.mk' s sm sa hm ha : set R) = s := rfl @[simp] lemma mem_mk' {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) {x : R} : x ∈ subring.mk' s sm sa hm ha ↔ x ∈ s := iff.rfl @[simp] lemma mk'_to_submonoid {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) : (subring.mk' s sm sa hm ha).to_submonoid = sm := set_like.coe_injective hm.symm @[simp] lemma mk'_to_add_subgroup {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa =s) : (subring.mk' s sm sa hm ha).to_add_subgroup = sa := set_like.coe_injective ha.symm end subring /-- A `subsemiring` containing -1 is a `subring`. -/ def subsemiring.to_subring (s : subsemiring R) (hneg : (-1 : R) ∈ s) : subring R := { neg_mem' := by { rintros x, rw <-neg_one_mul, apply subsemiring.mul_mem, exact hneg, } ..s.to_submonoid, ..s.to_add_submonoid } namespace subring variables (s : subring R) /-- A subring contains the ring's 1. -/ protected theorem one_mem : (1 : R) ∈ s := one_mem _ /-- A subring contains the ring's 0. -/ protected theorem zero_mem : (0 : R) ∈ s := zero_mem _ /-- A subring is closed under multiplication. -/ protected theorem mul_mem {x y : R} : x ∈ s → y ∈ s → x * y ∈ s := mul_mem /-- A subring is closed under addition. -/ protected theorem add_mem {x y : R} : x ∈ s → y ∈ s → x + y ∈ s := add_mem /-- A subring is closed under negation. -/ protected theorem neg_mem {x : R} : x ∈ s → -x ∈ s := neg_mem /-- A subring is closed under subtraction -/ protected theorem sub_mem {x y : R} (hx : x ∈ s) (hy : y ∈ s) : x - y ∈ s := sub_mem hx hy /-- Product of a list of elements in a subring is in the subring. -/ protected lemma list_prod_mem {l : list R} : (∀x ∈ l, x ∈ s) → l.prod ∈ s := list_prod_mem /-- Sum of a list of elements in a subring is in the subring. -/ protected lemma list_sum_mem {l : list R} : (∀x ∈ l, x ∈ s) → l.sum ∈ s := list_sum_mem /-- Product of a multiset of elements in a subring of a `comm_ring` is in the subring. -/ protected lemma multiset_prod_mem {R} [comm_ring R] (s : subring R) (m : multiset R) : (∀a ∈ m, a ∈ s) → m.prod ∈ s := multiset_prod_mem _ /-- Sum of a multiset of elements in an `subring` of a `ring` is in the `subring`. -/ protected lemma multiset_sum_mem {R} [ring R] (s : subring R) (m : multiset R) : (∀a ∈ m, a ∈ s) → m.sum ∈ s := multiset_sum_mem _ /-- Product of elements of a subring of a `comm_ring` indexed by a `finset` is in the subring. -/ protected lemma prod_mem {R : Type*} [comm_ring R] (s : subring R) {ι : Type*} {t : finset ι} {f : ι → R} (h : ∀c ∈ t, f c ∈ s) : ∏ i in t, f i ∈ s := prod_mem h /-- Sum of elements in a `subring` of a `ring` indexed by a `finset` is in the `subring`. -/ protected lemma sum_mem {R : Type*} [ring R] (s : subring R) {ι : Type*} {t : finset ι} {f : ι → R} (h : ∀c ∈ t, f c ∈ s) : ∑ i in t, f i ∈ s := sum_mem h /-- A subring of a ring inherits a ring structure -/ instance to_ring : ring s := subtype.coe_injective.ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) protected lemma zsmul_mem {x : R} (hx : x ∈ s) (n : ℤ) : n • x ∈ s := zsmul_mem hx n protected lemma pow_mem {x : R} (hx : x ∈ s) (n : ℕ) : x^n ∈ s := pow_mem hx n @[simp, norm_cast] lemma coe_add (x y : s) : (↑(x + y) : R) = ↑x + ↑y := rfl @[simp, norm_cast] lemma coe_neg (x : s) : (↑(-x) : R) = -↑x := rfl @[simp, norm_cast] lemma coe_mul (x y : s) : (↑(x * y) : R) = ↑x * ↑y := rfl @[simp, norm_cast] lemma coe_zero : ((0 : s) : R) = 0 := rfl @[simp, norm_cast] lemma coe_one : ((1 : s) : R) = 1 := rfl @[simp, norm_cast] lemma coe_pow (x : s) (n : ℕ) : (↑(x ^ n) : R) = x ^ n := submonoid_class.coe_pow x n -- TODO: can be generalized to `add_submonoid_class` @[simp] lemma coe_eq_zero_iff {x : s} : (x : R) = 0 ↔ x = 0 := ⟨λ h, subtype.ext (trans h s.coe_zero.symm), λ h, h.symm ▸ s.coe_zero⟩ /-- A subring of a `comm_ring` is a `comm_ring`. -/ instance to_comm_ring {R} [comm_ring R] (s : subring R) : comm_ring s := subtype.coe_injective.comm_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) /-- A subring of a non-trivial ring is non-trivial. -/ instance {R} [ring R] [nontrivial R] (s : subring R) : nontrivial s := s.to_subsemiring.nontrivial /-- A subring of a ring with no zero divisors has no zero divisors. -/ instance {R} [ring R] [no_zero_divisors R] (s : subring R) : no_zero_divisors s := s.to_subsemiring.no_zero_divisors /-- A subring of a domain is a domain. -/ instance {R} [ring R] [is_domain R] (s : subring R) : is_domain s := no_zero_divisors.to_is_domain _ /-- A subring of an `ordered_ring` is an `ordered_ring`. -/ instance to_ordered_ring {R} [ordered_ring R] (s : subring R) : ordered_ring s := subtype.coe_injective.ordered_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) /-- A subring of an `ordered_comm_ring` is an `ordered_comm_ring`. -/ instance to_ordered_comm_ring {R} [ordered_comm_ring R] (s : subring R) : ordered_comm_ring s := subtype.coe_injective.ordered_comm_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) /-- A subring of a `linear_ordered_ring` is a `linear_ordered_ring`. -/ instance to_linear_ordered_ring {R} [linear_ordered_ring R] (s : subring R) : linear_ordered_ring s := subtype.coe_injective.linear_ordered_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A subring of a `linear_ordered_comm_ring` is a `linear_ordered_comm_ring`. -/ instance to_linear_ordered_comm_ring {R} [linear_ordered_comm_ring R] (s : subring R) : linear_ordered_comm_ring s := subtype.coe_injective.linear_ordered_comm_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- The natural ring hom from a subring of ring `R` to `R`. -/ def subtype (s : subring R) : s →+* R := { to_fun := coe, .. s.to_submonoid.subtype, .. s.to_add_subgroup.subtype } @[simp] theorem coe_subtype : ⇑s.subtype = coe := rfl @[simp, norm_cast] lemma coe_nat_cast : ∀ n : ℕ, ((n : s) : R) = n := map_nat_cast s.subtype @[simp, norm_cast] lemma coe_int_cast : ∀ n : ℤ, ((n : s) : R) = n := map_int_cast s.subtype /-! ## Partial order -/ @[simp] lemma mem_to_submonoid {s : subring R} {x : R} : x ∈ s.to_submonoid ↔ x ∈ s := iff.rfl @[simp] lemma coe_to_submonoid (s : subring R) : (s.to_submonoid : set R) = s := rfl @[simp] lemma mem_to_add_subgroup {s : subring R} {x : R} : x ∈ s.to_add_subgroup ↔ x ∈ s := iff.rfl @[simp] lemma coe_to_add_subgroup (s : subring R) : (s.to_add_subgroup : set R) = s := rfl /-! ## top -/ /-- The subring `R` of the ring `R`. -/ instance : has_top (subring R) := ⟨{ .. (⊤ : submonoid R), .. (⊤ : add_subgroup R) }⟩ @[simp] lemma mem_top (x : R) : x ∈ (⊤ : subring R) := set.mem_univ x @[simp] lemma coe_top : ((⊤ : subring R) : set R) = set.univ := rfl /-- The ring equiv between the top element of `subring R` and `R`. -/ @[simps] def top_equiv : (⊤ : subring R) ≃+* R := subsemiring.top_equiv /-! ## comap -/ /-- The preimage of a subring along a ring homomorphism is a subring. -/ def comap {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring S) : subring R := { carrier := f ⁻¹' s.carrier, .. s.to_submonoid.comap (f : R →* S), .. s.to_add_subgroup.comap (f : R →+ S) } @[simp] lemma coe_comap (s : subring S) (f : R →+* S) : (s.comap f : set R) = f ⁻¹' s := rfl @[simp] lemma mem_comap {s : subring S} {f : R →+* S} {x : R} : x ∈ s.comap f ↔ f x ∈ s := iff.rfl lemma comap_comap (s : subring T) (g : S →+* T) (f : R →+* S) : (s.comap g).comap f = s.comap (g.comp f) := rfl /-! ## map -/ /-- The image of a subring along a ring homomorphism is a subring. -/ def map {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring R) : subring S := { carrier := f '' s.carrier, .. s.to_submonoid.map (f : R →* S), .. s.to_add_subgroup.map (f : R →+ S) } @[simp] lemma coe_map (f : R →+* S) (s : subring R) : (s.map f : set S) = f '' s := rfl @[simp] lemma mem_map {f : R →+* S} {s : subring R} {y : S} : y ∈ s.map f ↔ ∃ x ∈ s, f x = y := set.mem_image_iff_bex @[simp] lemma map_id : s.map (ring_hom.id R) = s := set_like.coe_injective $ set.image_id _ lemma map_map (g : S →+* T) (f : R →+* S) : (s.map f).map g = s.map (g.comp f) := set_like.coe_injective $ set.image_image _ _ _ lemma map_le_iff_le_comap {f : R →+* S} {s : subring R} {t : subring S} : s.map f ≤ t ↔ s ≤ t.comap f := set.image_subset_iff lemma gc_map_comap (f : R →+* S) : galois_connection (map f) (comap f) := λ S T, map_le_iff_le_comap /-- A subring is isomorphic to its image under an injective function -/ noncomputable def equiv_map_of_injective (f : R →+* S) (hf : function.injective f) : s ≃+* s.map f := { map_mul' := λ _ _, subtype.ext (f.map_mul _ _), map_add' := λ _ _, subtype.ext (f.map_add _ _), ..equiv.set.image f s hf } @[simp] lemma coe_equiv_map_of_injective_apply (f : R →+* S) (hf : function.injective f) (x : s) : (equiv_map_of_injective s f hf x : S) = f x := rfl end subring namespace ring_hom variables (g : S →+* T) (f : R →+* S) /-! ## range -/ /-- The range of a ring homomorphism, as a subring of the target. See Note [range copy pattern]. -/ def range {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : subring S := ((⊤ : subring R).map f).copy (set.range f) set.image_univ.symm @[simp] lemma coe_range : (f.range : set S) = set.range f := rfl @[simp] lemma mem_range {f : R →+* S} {y : S} : y ∈ f.range ↔ ∃ x, f x = y := iff.rfl lemma range_eq_map (f : R →+* S) : f.range = subring.map f ⊤ := by { ext, simp } lemma mem_range_self (f : R →+* S) (x : R) : f x ∈ f.range := mem_range.mpr ⟨x, rfl⟩ lemma map_range : f.range.map g = (g.comp f).range := by simpa only [range_eq_map] using (⊤ : subring R).map_map g f /-- The range of a ring homomorphism is a fintype, if the domain is a fintype. Note: this instance can form a diamond with `subtype.fintype` in the presence of `fintype S`. -/ instance fintype_range [fintype R] [decidable_eq S] (f : R →+* S) : fintype (range f) := set.fintype_range f end ring_hom namespace subring /-! ## bot -/ instance : has_bot (subring R) := ⟨(int.cast_ring_hom R).range⟩ instance : inhabited (subring R) := ⟨⊥⟩ lemma coe_bot : ((⊥ : subring R) : set R) = set.range (coe : ℤ → R) := ring_hom.coe_range (int.cast_ring_hom R) lemma mem_bot {x : R} : x ∈ (⊥ : subring R) ↔ ∃ (n : ℤ), ↑n = x := ring_hom.mem_range /-! ## inf -/ /-- The inf of two subrings is their intersection. -/ instance : has_inf (subring R) := ⟨λ s t, { carrier := s ∩ t, .. s.to_submonoid ⊓ t.to_submonoid, .. s.to_add_subgroup ⊓ t.to_add_subgroup }⟩ @[simp] lemma coe_inf (p p' : subring R) : ((p ⊓ p' : subring R) : set R) = p ∩ p' := rfl @[simp] lemma mem_inf {p p' : subring R} {x : R} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl instance : has_Inf (subring R) := ⟨λ s, subring.mk' (⋂ t ∈ s, ↑t) (⨅ t ∈ s, subring.to_submonoid t ) (⨅ t ∈ s, subring.to_add_subgroup t) (by simp) (by simp)⟩ @[simp, norm_cast] lemma coe_Inf (S : set (subring R)) : ((Inf S : subring R) : set R) = ⋂ s ∈ S, ↑s := rfl lemma mem_Inf {S : set (subring R)} {x : R} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_Inter₂ @[simp, norm_cast] lemma coe_infi {ι : Sort*} {S : ι → subring R} : (↑(⨅ i, S i) : set R) = ⋂ i, S i := by simp only [infi, coe_Inf, set.bInter_range] lemma mem_infi {ι : Sort*} {S : ι → subring R} {x : R} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [infi, mem_Inf, set.forall_range_iff] @[simp] lemma Inf_to_submonoid (s : set (subring R)) : (Inf s).to_submonoid = ⨅ t ∈ s, subring.to_submonoid t := mk'_to_submonoid _ _ @[simp] lemma Inf_to_add_subgroup (s : set (subring R)) : (Inf s).to_add_subgroup = ⨅ t ∈ s, subring.to_add_subgroup t := mk'_to_add_subgroup _ _ /-- Subrings of a ring form a complete lattice. -/ instance : complete_lattice (subring R) := { bot := (⊥), bot_le := λ s x hx, let ⟨n, hn⟩ := mem_bot.1 hx in hn ▸ coe_int_mem s n, top := (⊤), le_top := λ s x hx, trivial, inf := (⊓), inf_le_left := λ s t x, and.left, inf_le_right := λ s t x, and.right, le_inf := λ s t₁ t₂ h₁ h₂ x hx, ⟨h₁ hx, h₂ hx⟩, .. complete_lattice_of_Inf (subring R) (λ s, is_glb.of_image (λ s t, show (s : set R) ≤ t ↔ s ≤ t, from set_like.coe_subset_coe) is_glb_binfi)} lemma eq_top_iff' (A : subring R) : A = ⊤ ↔ ∀ x : R, x ∈ A := eq_top_iff.trans ⟨λ h m, h $ mem_top m, λ h m _, h m⟩ /-! ## Center of a ring -/ section variables (R) /-- The center of a ring `R` is the set of elements that commute with everything in `R` -/ def center : subring R := { carrier := set.center R, neg_mem' := λ a, set.neg_mem_center, .. subsemiring.center R } lemma coe_center : ↑(center R) = set.center R := rfl @[simp] lemma center_to_subsemiring : (center R).to_subsemiring = subsemiring.center R := rfl variables {R} lemma mem_center_iff {z : R} : z ∈ center R ↔ ∀ g, g * z = z * g := iff.rfl instance decidable_mem_center [decidable_eq R] [fintype R] : decidable_pred (∈ center R) := λ _, decidable_of_iff' _ mem_center_iff @[simp] lemma center_eq_top (R) [comm_ring R] : center R = ⊤ := set_like.coe_injective (set.center_eq_univ R) /-- The center is commutative. -/ instance : comm_ring (center R) := { ..subsemiring.center.comm_semiring, ..(center R).to_ring} end section division_ring variables {K : Type u} [division_ring K] instance : field (center K) := { inv := λ a, ⟨a⁻¹, set.inv_mem_center₀ a.prop⟩, mul_inv_cancel := λ ⟨a, ha⟩ h, subtype.ext $ mul_inv_cancel $ subtype.coe_injective.ne h, div := λ a b, ⟨a / b, set.div_mem_center₀ a.prop b.prop⟩, div_eq_mul_inv := λ a b, subtype.ext $ div_eq_mul_inv _ _, inv_zero := subtype.ext inv_zero, ..(center K).nontrivial, ..center.comm_ring } @[simp] lemma center.coe_inv (a : center K) : ((a⁻¹ : center K) : K) = (a : K)⁻¹ := rfl @[simp] lemma center.coe_div (a b : center K) : ((a / b : center K) : K) = (a : K) / (b : K) := rfl end division_ring /-! ## subring closure of a subset -/ /-- The `subring` generated by a set. -/ def closure (s : set R) : subring R := Inf {S | s ⊆ S} lemma mem_closure {x : R} {s : set R} : x ∈ closure s ↔ ∀ S : subring R, s ⊆ S → x ∈ S := mem_Inf /-- The subring generated by a set includes the set. -/ @[simp] lemma subset_closure {s : set R} : s ⊆ closure s := λ x hx, mem_closure.2 $ λ S hS, hS hx lemma not_mem_of_not_mem_closure {s : set R} {P : R} (hP : P ∉ closure s) : P ∉ s := λ h, hP (subset_closure h) /-- A subring `t` includes `closure s` if and only if it includes `s`. -/ @[simp] lemma closure_le {s : set R} {t : subring R} : closure s ≤ t ↔ s ⊆ t := ⟨set.subset.trans subset_closure, λ h, Inf_le h⟩ /-- Subring closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`. -/ lemma closure_mono ⦃s t : set R⦄ (h : s ⊆ t) : closure s ≤ closure t := closure_le.2 $ set.subset.trans h subset_closure lemma closure_eq_of_le {s : set R} {t : subring R} (h₁ : s ⊆ t) (h₂ : t ≤ closure s) : closure s = t := le_antisymm (closure_le.2 h₁) h₂ /-- An induction principle for closure membership. If `p` holds for `0`, `1`, and all elements of `s`, and is preserved under addition, negation, and multiplication, then `p` holds for all elements of the closure of `s`. -/ @[elab_as_eliminator] lemma closure_induction {s : set R} {p : R → Prop} {x} (h : x ∈ closure s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : p 1) (Hadd : ∀ x y, p x → p y → p (x + y)) (Hneg : ∀ (x : R), p x → p (-x)) (Hmul : ∀ x y, p x → p y → p (x * y)) : p x := (@closure_le _ _ _ ⟨p, Hmul, H1, Hadd, H0, Hneg⟩).2 Hs h /-- An induction principle for closure membership, for predicates with two arguments. -/ @[elab_as_eliminator] lemma closure_induction₂ {s : set R} {p : R → R → Prop} {a b : R} (ha : a ∈ closure s) (hb : b ∈ closure s) (Hs : ∀ (x ∈ s) (y ∈ s), p x y) (H0_left : ∀ x, p 0 x) (H0_right : ∀ x, p x 0) (H1_left : ∀ x, p 1 x) (H1_right : ∀ x, p x 1) (Hneg_left : ∀ x y, p x y → p (-x) y) (Hneg_right : ∀ x y, p x y → p x (-y)) (Hadd_left : ∀ x₁ x₂ y, p x₁ y → p x₂ y → p (x₁ + x₂) y) (Hadd_right : ∀ x y₁ y₂, p x y₁ → p x y₂ → p x (y₁ + y₂)) (Hmul_left : ∀ x₁ x₂ y, p x₁ y → p x₂ y → p (x₁ * x₂) y) (Hmul_right : ∀ x y₁ y₂, p x y₁ → p x y₂ → p x (y₁ * y₂)) : p a b := begin refine closure_induction hb _ (H0_right _) (H1_right _) (Hadd_right a) (Hneg_right a) (Hmul_right a), refine closure_induction ha Hs (λ x _, H0_left x) (λ x _, H1_left x) _ _ _, { exact (λ x y H₁ H₂ z zs, Hadd_left x y z (H₁ z zs) (H₂ z zs)) }, { exact (λ x hx z zs, Hneg_left x z (hx z zs)) }, { exact (λ x y H₁ H₂ z zs, Hmul_left x y z (H₁ z zs) (H₂ z zs)) } end lemma mem_closure_iff {s : set R} {x} : x ∈ closure s ↔ x ∈ add_subgroup.closure (submonoid.closure s : set R) := ⟨λ h, closure_induction h (λ x hx, add_subgroup.subset_closure $ submonoid.subset_closure hx) (add_subgroup.zero_mem _) (add_subgroup.subset_closure ( submonoid.one_mem (submonoid.closure s)) ) (λ x y hx hy, add_subgroup.add_mem _ hx hy ) (λ x hx, add_subgroup.neg_mem _ hx ) (λ x y hx hy, add_subgroup.closure_induction hy (λ q hq, add_subgroup.closure_induction hx (λ p hp, add_subgroup.subset_closure ((submonoid.closure s).mul_mem hp hq)) (begin rw zero_mul q, apply add_subgroup.zero_mem _, end) (λ p₁ p₂ ihp₁ ihp₂, begin rw add_mul p₁ p₂ q, apply add_subgroup.add_mem _ ihp₁ ihp₂, end) (λ x hx, begin have f : -x * q = -(x*q) := by simp, rw f, apply add_subgroup.neg_mem _ hx, end)) (begin rw mul_zero x, apply add_subgroup.zero_mem _, end) (λ q₁ q₂ ihq₁ ihq₂, begin rw mul_add x q₁ q₂, apply add_subgroup.add_mem _ ihq₁ ihq₂ end) (λ z hz, begin have f : x * -z = -(x*z) := by simp, rw f, apply add_subgroup.neg_mem _ hz, end)), λ h, add_subgroup.closure_induction h (λ x hx, submonoid.closure_induction hx (λ x hx, subset_closure hx) (one_mem _) (λ x y hx hy, mul_mem hx hy)) (zero_mem _) (λ x y hx hy, add_mem hx hy) (λ x hx, neg_mem hx)⟩ /-- If all elements of `s : set A` commute pairwise, then `closure s` is a commutative ring. -/ def closure_comm_ring_of_comm {s : set R} (hcomm : ∀ (a ∈ s) (b ∈ s), a * b = b * a) : comm_ring (closure s) := { mul_comm := λ x y, begin ext, simp only [subring.coe_mul], refine closure_induction₂ x.prop y.prop hcomm (λ x, by simp only [mul_zero, zero_mul]) (λ x, by simp only [mul_zero, zero_mul]) (λ x, by simp only [mul_one, one_mul]) (λ x, by simp only [mul_one, one_mul]) (λ x y hxy, by simp only [mul_neg, neg_mul, hxy]) (λ x y hxy, by simp only [mul_neg, neg_mul, hxy]) (λ x₁ x₂ y h₁ h₂, by simp only [add_mul, mul_add, h₁, h₂]) (λ x₁ x₂ y h₁ h₂, by simp only [add_mul, mul_add, h₁, h₂]) (λ x₁ x₂ y h₁ h₂, by rw [←mul_assoc, ←h₁, mul_assoc x₁ y x₂, ←h₂, mul_assoc]) (λ x₁ x₂ y h₁ h₂, by rw [←mul_assoc, h₁, mul_assoc, h₂, ←mul_assoc]) end, ..(closure s).to_ring } theorem exists_list_of_mem_closure {s : set R} {x : R} (h : x ∈ closure s) : (∃ L : list (list R), (∀ t ∈ L, ∀ y ∈ t, y ∈ s ∨ y = (-1:R)) ∧ (L.map list.prod).sum = x) := add_subgroup.closure_induction (mem_closure_iff.1 h) (λ x hx, let ⟨l, hl, h⟩ :=submonoid.exists_list_of_mem_closure hx in ⟨[l], by simp [h]; clear_aux_decl; tauto!⟩) ⟨[], by simp⟩ (λ x y ⟨l, hl1, hl2⟩ ⟨m, hm1, hm2⟩, ⟨l ++ m, λ t ht, (list.mem_append.1 ht).elim (hl1 t) (hm1 t), by simp [hl2, hm2]⟩) (λ x ⟨L, hL⟩, ⟨L.map (list.cons (-1)), list.forall_mem_map_iff.2 $ λ j hj, list.forall_mem_cons.2 ⟨or.inr rfl, hL.1 j hj⟩, hL.2 ▸ list.rec_on L (by simp) (by simp [list.map_cons, add_comm] {contextual := tt})⟩) variable (R) /-- `closure` forms a Galois insertion with the coercion to set. -/ protected def gi : galois_insertion (@closure R _) coe := { choice := λ s _, closure s, gc := λ s t, closure_le, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variable {R} /-- Closure of a subring `S` equals `S`. -/ lemma closure_eq (s : subring R) : closure (s : set R) = s := (subring.gi R).l_u_eq s @[simp] lemma closure_empty : closure (∅ : set R) = ⊥ := (subring.gi R).gc.l_bot @[simp] lemma closure_univ : closure (set.univ : set R) = ⊤ := @coe_top R _ ▸ closure_eq ⊤ lemma closure_union (s t : set R) : closure (s ∪ t) = closure s ⊔ closure t := (subring.gi R).gc.l_sup lemma closure_Union {ι} (s : ι → set R) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (subring.gi R).gc.l_supr lemma closure_sUnion (s : set (set R)) : closure (⋃₀ s) = ⨆ t ∈ s, closure t := (subring.gi R).gc.l_Sup lemma map_sup (s t : subring R) (f : R →+* S) : (s ⊔ t).map f = s.map f ⊔ t.map f := (gc_map_comap f).l_sup lemma map_supr {ι : Sort*} (f : R →+* S) (s : ι → subring R) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr lemma comap_inf (s t : subring S) (f : R →+* S) : (s ⊓ t).comap f = s.comap f ⊓ t.comap f := (gc_map_comap f).u_inf lemma comap_infi {ι : Sort*} (f : R →+* S) (s : ι → subring S) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp] lemma map_bot (f : R →+* S) : (⊥ : subring R).map f = ⊥ := (gc_map_comap f).l_bot @[simp] lemma comap_top (f : R →+* S) : (⊤ : subring S).comap f = ⊤ := (gc_map_comap f).u_top /-- Given `subring`s `s`, `t` of rings `R`, `S` respectively, `s.prod t` is `s ×̂ t` as a subring of `R × S`. -/ def prod (s : subring R) (t : subring S) : subring (R × S) := { carrier := s ×ˢ t, .. s.to_submonoid.prod t.to_submonoid, .. s.to_add_subgroup.prod t.to_add_subgroup} @[norm_cast] lemma coe_prod (s : subring R) (t : subring S) : (s.prod t : set (R × S)) = s ×ˢ t := rfl lemma mem_prod {s : subring R} {t : subring S} {p : R × S} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[mono] lemma prod_mono ⦃s₁ s₂ : subring R⦄ (hs : s₁ ≤ s₂) ⦃t₁ t₂ : subring S⦄ (ht : t₁ ≤ t₂) : s₁.prod t₁ ≤ s₂.prod t₂ := set.prod_mono hs ht lemma prod_mono_right (s : subring R) : monotone (λ t : subring S, s.prod t) := prod_mono (le_refl s) lemma prod_mono_left (t : subring S) : monotone (λ s : subring R, s.prod t) := λ s₁ s₂ hs, prod_mono hs (le_refl t) lemma prod_top (s : subring R) : s.prod (⊤ : subring S) = s.comap (ring_hom.fst R S) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] lemma top_prod (s : subring S) : (⊤ : subring R).prod s = s.comap (ring_hom.snd R S) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp] lemma top_prod_top : (⊤ : subring R).prod (⊤ : subring S) = ⊤ := (top_prod _).trans $ comap_top _ /-- Product of subrings is isomorphic to their product as rings. -/ def prod_equiv (s : subring R) (t : subring S) : s.prod t ≃+* s × t := { map_mul' := λ x y, rfl, map_add' := λ x y, rfl, .. equiv.set.prod ↑s ↑t } /-- The underlying set of a non-empty directed Sup of subrings is just a union of the subrings. Note that this fails without the directedness assumption (the union of two subrings is typically not a subring) -/ lemma mem_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subring R} (hS : directed (≤) S) {x : R} : x ∈ (⨆ i, S i) ↔ ∃ i, x ∈ S i := begin refine ⟨_, λ ⟨i, hi⟩, (set_like.le_def.1 $ le_supr S i) hi⟩, let U : subring R := subring.mk' (⋃ i, (S i : set R)) (⨆ i, (S i).to_submonoid) (⨆ i, (S i).to_add_subgroup) (submonoid.coe_supr_of_directed $ hS.mono_comp _ (λ _ _, id)) (add_subgroup.coe_supr_of_directed $ hS.mono_comp _ (λ _ _, id)), suffices : (⨆ i, S i) ≤ U, by simpa using @this x, exact supr_le (λ i x hx, set.mem_Union.2 ⟨i, hx⟩), end lemma coe_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subring R} (hS : directed (≤) S) : ((⨆ i, S i : subring R) : set R) = ⋃ i, ↑(S i) := set.ext $ λ x, by simp [mem_supr_of_directed hS] lemma mem_Sup_of_directed_on {S : set (subring R)} (Sne : S.nonempty) (hS : directed_on (≤) S) {x : R} : x ∈ Sup S ↔ ∃ s ∈ S, x ∈ s := begin haveI : nonempty S := Sne.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed hS.directed_coe, set_coe.exists, subtype.coe_mk] end lemma coe_Sup_of_directed_on {S : set (subring R)} (Sne : S.nonempty) (hS : directed_on (≤) S) : (↑(Sup S) : set R) = ⋃ s ∈ S, ↑s := set.ext $ λ x, by simp [mem_Sup_of_directed_on Sne hS] lemma mem_map_equiv {f : R ≃+* S} {K : subring R} {x : S} : x ∈ K.map (f : R →+* S) ↔ f.symm x ∈ K := @set.mem_image_equiv _ _ ↑K f.to_equiv x lemma map_equiv_eq_comap_symm (f : R ≃+* S) (K : subring R) : K.map (f : R →+* S) = K.comap f.symm := set_like.coe_injective (f.to_equiv.image_eq_preimage K) lemma comap_equiv_eq_map_symm (f : R ≃+* S) (K : subring S) : K.comap (f : R →+* S) = K.map f.symm := (map_equiv_eq_comap_symm f.symm K).symm end subring namespace ring_hom variables {s : subring R} open subring /-- Restriction of a ring homomorphism to its range interpreted as a subsemiring. This is the bundled version of `set.range_factorization`. -/ def range_restrict (f : R →+* S) : R →+* f.range := f.cod_restrict f.range $ λ x, ⟨x, rfl⟩ @[simp] lemma coe_range_restrict (f : R →+* S) (x : R) : (f.range_restrict x : S) = f x := rfl lemma range_restrict_surjective (f : R →+* S) : function.surjective f.range_restrict := λ ⟨y, hy⟩, let ⟨x, hx⟩ := mem_range.mp hy in ⟨x, subtype.ext hx⟩ lemma range_top_iff_surjective {f : R →+* S} : f.range = (⊤ : subring S) ↔ function.surjective f := set_like.ext'_iff.trans $ iff.trans (by rw [coe_range, coe_top]) set.range_iff_surjective /-- The range of a surjective ring homomorphism is the whole of the codomain. -/ lemma range_top_of_surjective (f : R →+* S) (hf : function.surjective f) : f.range = (⊤ : subring S) := range_top_iff_surjective.2 hf /-- The subring of elements `x : R` such that `f x = g x`, i.e., the equalizer of f and g as a subring of R -/ def eq_locus (f g : R →+* S) : subring R := { carrier := {x | f x = g x}, .. (f : R →* S).eq_mlocus g, .. (f : R →+ S).eq_locus g } @[simp] lemma eq_locus_same (f : R →+* S) : f.eq_locus f = ⊤ := set_like.ext $ λ _, eq_self_iff_true _ /-- If two ring homomorphisms are equal on a set, then they are equal on its subring closure. -/ lemma eq_on_set_closure {f g : R →+* S} {s : set R} (h : set.eq_on f g s) : set.eq_on f g (closure s) := show closure s ≤ f.eq_locus g, from closure_le.2 h lemma eq_of_eq_on_set_top {f g : R →+* S} (h : set.eq_on f g (⊤ : subring R)) : f = g := ext $ λ x, h trivial lemma eq_of_eq_on_set_dense {s : set R} (hs : closure s = ⊤) {f g : R →+* S} (h : s.eq_on f g) : f = g := eq_of_eq_on_set_top $ hs ▸ eq_on_set_closure h lemma closure_preimage_le (f : R →+* S) (s : set S) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, set_like.mem_coe.2 $ mem_comap.2 $ subset_closure hx /-- The image under a ring homomorphism of the subring generated by a set equals the subring generated by the image of the set. -/ lemma map_closure (f : R →+* S) (s : set R) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _) (closure_preimage_le _ _)) (closure_le.2 $ set.image_subset _ subset_closure) end ring_hom namespace subring open ring_hom /-- The ring homomorphism associated to an inclusion of subrings. -/ def inclusion {S T : subring R} (h : S ≤ T) : S →+* T := S.subtype.cod_restrict _ (λ x, h x.2) @[simp] lemma range_subtype (s : subring R) : s.subtype.range = s := set_like.coe_injective $ (coe_srange _).trans subtype.range_coe @[simp] lemma range_fst : (fst R S).srange = ⊤ := (fst R S).srange_top_of_surjective $ prod.fst_surjective @[simp] lemma range_snd : (snd R S).srange = ⊤ := (snd R S).srange_top_of_surjective $ prod.snd_surjective @[simp] lemma prod_bot_sup_bot_prod (s : subring R) (t : subring S) : (s.prod ⊥) ⊔ (prod ⊥ t) = s.prod t := le_antisymm (sup_le (prod_mono_right s bot_le) (prod_mono_left t bot_le)) $ assume p hp, prod.fst_mul_snd p ▸ mul_mem ((le_sup_left : s.prod ⊥ ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨hp.1, set_like.mem_coe.2 $ one_mem ⊥⟩) ((le_sup_right : prod ⊥ t ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨set_like.mem_coe.2 $ one_mem ⊥, hp.2⟩) end subring namespace ring_equiv variables {s t : subring R} /-- Makes the identity isomorphism from a proof two subrings of a multiplicative monoid are equal. -/ def subring_congr (h : s = t) : s ≃+* t := { map_mul' := λ _ _, rfl, map_add' := λ _ _, rfl, ..equiv.set_congr $ congr_arg _ h } /-- Restrict a ring homomorphism with a left inverse to a ring isomorphism to its `ring_hom.range`. -/ def of_left_inverse {g : S → R} {f : R →+* S} (h : function.left_inverse g f) : R ≃+* f.range := { to_fun := λ x, f.range_restrict x, inv_fun := λ x, (g ∘ f.range.subtype) x, left_inv := h, right_inv := λ x, subtype.ext $ let ⟨x', hx'⟩ := ring_hom.mem_range.mp x.prop in show f (g x) = x, by rw [←hx', h x'], ..f.range_restrict } @[simp] lemma of_left_inverse_apply {g : S → R} {f : R →+* S} (h : function.left_inverse g f) (x : R) : ↑(of_left_inverse h x) = f x := rfl @[simp] lemma of_left_inverse_symm_apply {g : S → R} {f : R →+* S} (h : function.left_inverse g f) (x : f.range) : (of_left_inverse h).symm x = g x := rfl /-- Given an equivalence `e : R ≃+* S` of rings and a subring `s` of `R`, `subring_equiv_map e s` is the induced equivalence between `s` and `s.map e` -/ @[simps] def subring_map (e : R ≃+* S) : s ≃+* s.map e.to_ring_hom := e.subsemiring_map s.to_subsemiring end ring_equiv namespace subring variables {s : set R} local attribute [reducible] closure @[elab_as_eliminator] protected theorem in_closure.rec_on {C : R → Prop} {x : R} (hx : x ∈ closure s) (h1 : C 1) (hneg1 : C (-1)) (hs : ∀ z ∈ s, ∀ n, C n → C (z * n)) (ha : ∀ {x y}, C x → C y → C (x + y)) : C x := begin have h0 : C 0 := add_neg_self (1:R) ▸ ha h1 hneg1, rcases exists_list_of_mem_closure hx with ⟨L, HL, rfl⟩, clear hx, induction L with hd tl ih, { exact h0 }, rw list.forall_mem_cons at HL, suffices : C (list.prod hd), { rw [list.map_cons, list.sum_cons], exact ha this (ih HL.2) }, replace HL := HL.1, clear ih tl, rsuffices ⟨L, HL', HP | HP⟩ : ∃ L : list R, (∀ x ∈ L, x ∈ s) ∧ (list.prod hd = list.prod L ∨ list.prod hd = -list.prod L), { rw HP, clear HP HL hd, induction L with hd tl ih, { exact h1 }, rw list.forall_mem_cons at HL', rw list.prod_cons, exact hs _ HL'.1 _ (ih HL'.2) }, { rw HP, clear HP HL hd, induction L with hd tl ih, { exact hneg1 }, rw [list.prod_cons, neg_mul_eq_mul_neg], rw list.forall_mem_cons at HL', exact hs _ HL'.1 _ (ih HL'.2) }, induction hd with hd tl ih, { exact ⟨[], list.forall_mem_nil _, or.inl rfl⟩ }, rw list.forall_mem_cons at HL, rcases ih HL.2 with ⟨L, HL', HP | HP⟩; cases HL.1 with hhd hhd, { exact ⟨hd :: L, list.forall_mem_cons.2 ⟨hhd, HL'⟩, or.inl $ by rw [list.prod_cons, list.prod_cons, HP]⟩ }, { exact ⟨L, HL', or.inr $ by rw [list.prod_cons, hhd, neg_one_mul, HP]⟩ }, { exact ⟨hd :: L, list.forall_mem_cons.2 ⟨hhd, HL'⟩, or.inr $ by rw [list.prod_cons, list.prod_cons, HP, neg_mul_eq_mul_neg]⟩ }, { exact ⟨L, HL', or.inl $ by rw [list.prod_cons, hhd, HP, neg_one_mul, neg_neg]⟩ } end lemma closure_preimage_le (f : R →+* S) (s : set S) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, set_like.mem_coe.2 $ mem_comap.2 $ subset_closure hx end subring lemma add_subgroup.int_mul_mem {G : add_subgroup R} (k : ℤ) {g : R} (h : g ∈ G) : (k : R) * g ∈ G := by { convert add_subgroup.zsmul_mem G h k, simp } /-! ## Actions by `subring`s These are just copies of the definitions about `subsemiring` starting from `subsemiring.mul_action`. When `R` is commutative, `algebra.of_subring` provides a stronger result than those found in this file, which uses the same scalar action. -/ section actions namespace subring variables {α β : Type*} /-- The action by a subring is the action by the underlying ring. -/ instance [has_smul R α] (S : subring R) : has_smul S α := S.to_subsemiring.has_smul lemma smul_def [has_smul R α] {S : subring R} (g : S) (m : α) : g • m = (g : R) • m := rfl instance smul_comm_class_left [has_smul R β] [has_smul α β] [smul_comm_class R α β] (S : subring R) : smul_comm_class S α β := S.to_subsemiring.smul_comm_class_left instance smul_comm_class_right [has_smul α β] [has_smul R β] [smul_comm_class α R β] (S : subring R) : smul_comm_class α S β := S.to_subsemiring.smul_comm_class_right /-- Note that this provides `is_scalar_tower S R R` which is needed by `smul_mul_assoc`. -/ instance [has_smul α β] [has_smul R α] [has_smul R β] [is_scalar_tower R α β] (S : subring R) : is_scalar_tower S α β := S.to_subsemiring.is_scalar_tower instance [has_smul R α] [has_faithful_smul R α] (S : subring R) : has_faithful_smul S α := S.to_subsemiring.has_faithful_smul /-- The action by a subring is the action by the underlying ring. -/ instance [mul_action R α] (S : subring R) : mul_action S α := S.to_subsemiring.mul_action /-- The action by a subring is the action by the underlying ring. -/ instance [add_monoid α] [distrib_mul_action R α] (S : subring R) : distrib_mul_action S α := S.to_subsemiring.distrib_mul_action /-- The action by a subring is the action by the underlying ring. -/ instance [monoid α] [mul_distrib_mul_action R α] (S : subring R) : mul_distrib_mul_action S α := S.to_subsemiring.mul_distrib_mul_action /-- The action by a subring is the action by the underlying ring. -/ instance [has_zero α] [smul_with_zero R α] (S : subring R) : smul_with_zero S α := S.to_subsemiring.smul_with_zero /-- The action by a subring is the action by the underlying ring. -/ instance [has_zero α] [mul_action_with_zero R α] (S : subring R) : mul_action_with_zero S α := S.to_subsemiring.mul_action_with_zero /-- The action by a subring is the action by the underlying ring. -/ instance [add_comm_monoid α] [module R α] (S : subring R) : module S α := S.to_subsemiring.module /-- The action by a subsemiring is the action by the underlying ring. -/ instance [semiring α] [mul_semiring_action R α] (S : subring R) : mul_semiring_action S α := S.to_submonoid.mul_semiring_action /-- The center of a semiring acts commutatively on that semiring. -/ instance center.smul_comm_class_left : smul_comm_class (center R) R R := subsemiring.center.smul_comm_class_left /-- The center of a semiring acts commutatively on that semiring. -/ instance center.smul_comm_class_right : smul_comm_class R (center R) R := subsemiring.center.smul_comm_class_right end subring end actions -- while this definition is not about subrings, this is the earliest we have -- both ordered ring structures and submonoids available /-- The subgroup of positive units of a linear ordered semiring. -/ def units.pos_subgroup (R : Type*) [linear_ordered_semiring R] : subgroup Rˣ := { carrier := {x | (0 : R) < x}, inv_mem' := λ x, units.inv_pos.mpr, ..(pos_submonoid R).comap (units.coe_hom R)} @[simp] lemma units.mem_pos_subgroup {R : Type*} [linear_ordered_semiring R] (u : Rˣ) : u ∈ units.pos_subgroup R ↔ (0 : R) < u := iff.rfl
617bf64018cfd10d5011f05ae5f507a2a82a49ef
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/library/algebra/group_power.lean
167bdb6099718c44c55d31f416a049a6a4fc57b9
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
8,380
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad The power operation on monoids and groups. We separate this from group, because it depends on nat, which in turn depends on other parts of algebra. We have "pow a n" for natural number powers, and "gpow a i" for integer powers. The notation a^n is used for the first, but users can locally redefine it to gpow when needed. Note: power adopts the convention that 0^0=1. -/ import data.nat.basic data.int.basic variables {A : Type} structure has_pow_nat [class] (A : Type) := (pow_nat : A → nat → A) definition pow_nat {A : Type} [s : has_pow_nat A] : A → nat → A := has_pow_nat.pow_nat infix ` ^ ` := pow_nat structure has_pow_int [class] (A : Type) := (pow_int : A → int → A) definition pow_int {A : Type} [s : has_pow_int A] : A → int → A := has_pow_int.pow_int /- monoid -/ section monoid open nat variable [s : monoid A] include s definition monoid.pow (a : A) : ℕ → A | 0 := 1 | (n+1) := a * monoid.pow n definition monoid_has_pow_nat [reducible] [instance] : has_pow_nat A := has_pow_nat.mk monoid.pow theorem pow_zero (a : A) : a^0 = 1 := rfl theorem pow_succ (a : A) (n : ℕ) : a^(succ n) = a * a^n := rfl theorem pow_one (a : A) : a^1 = a := !mul_one theorem pow_two (a : A) : a^2 = a * a := calc a^2 = a * (a * 1) : rfl ... = a * a : mul_one theorem pow_three (a : A) : a^3 = a * (a * a) := calc a^3 = a * (a * (a * 1)) : rfl ... = a * (a * a) : mul_one theorem pow_four (a : A) : a^4 = a * (a * (a * a)) := calc a^4 = a * a^3 : rfl ... = a * (a * (a * a)) : pow_three theorem pow_succ' (a : A) : ∀n, a^(succ n) = a^n * a | 0 := by rewrite [pow_succ, *pow_zero, one_mul, mul_one] | (succ n) := by rewrite [pow_succ, pow_succ' at {1}, pow_succ, mul.assoc] theorem one_pow : ∀ n : ℕ, 1^n = (1:A) | 0 := rfl | (succ n) := by rewrite [pow_succ, one_mul, one_pow] theorem pow_add (a : A) (m n : ℕ) : a^(m + n) = a^m * a^n := begin induction n with n ih, {krewrite [nat.add_zero, pow_zero, mul_one]}, rewrite [add_succ, *pow_succ', ih, mul.assoc] end theorem pow_mul (a : A) (m : ℕ) : ∀ n, a^(m * n) = (a^m)^n | 0 := by rewrite [nat.mul_zero, pow_zero] | (succ n) := by rewrite [nat.mul_succ, pow_add, pow_succ', pow_mul] theorem pow_comm (a : A) (m n : ℕ) : a^m * a^n = a^n * a^m := by rewrite [-*pow_add, add.comm] end monoid /- commutative monoid -/ section comm_monoid open nat variable [s : comm_monoid A] include s theorem mul_pow (a b : A) : ∀ n, (a * b)^n = a^n * b^n | 0 := by rewrite [*pow_zero, mul_one] | (succ n) := by rewrite [*pow_succ', mul_pow, *mul.assoc, mul.left_comm a] end comm_monoid section group variable [s : group A] include s section nat open nat theorem inv_pow (a : A) : ∀n, (a⁻¹)^n = (a^n)⁻¹ | 0 := by rewrite [*pow_zero, one_inv] | (succ n) := by rewrite [pow_succ, pow_succ', inv_pow, mul_inv] theorem pow_sub (a : A) {m n : ℕ} (H : m ≥ n) : a^(m - n) = a^m * (a^n)⁻¹ := assert H1 : m - n + n = m, from nat.sub_add_cancel H, have H2 : a^(m - n) * a^n = a^m, by rewrite [-pow_add, H1], eq_mul_inv_of_mul_eq H2 theorem pow_inv_comm (a : A) : ∀m n, (a⁻¹)^m * a^n = a^n * (a⁻¹)^m | 0 n := by rewrite [*pow_zero, one_mul, mul_one] | m 0 := by rewrite [*pow_zero, one_mul, mul_one] | (succ m) (succ n) := by rewrite [pow_succ' at {1}, pow_succ at {1}, pow_succ', pow_succ, *mul.assoc, inv_mul_cancel_left, mul_inv_cancel_left, pow_inv_comm] end nat open int definition gpow (a : A) : ℤ → A | (of_nat n) := a^n | -[1+n] := (a^(nat.succ n))⁻¹ open nat private lemma gpow_add_aux (a : A) (m n : nat) : gpow a ((of_nat m) + -[1+n]) = gpow a (of_nat m) * gpow a (-[1+n]) := or.elim (nat.lt_or_ge m (nat.succ n)) (assume H : (m < nat.succ n), assert H1 : (#nat nat.succ n - m > nat.zero), from nat.sub_pos_of_lt H, calc gpow a ((of_nat m) + -[1+n]) = gpow a (sub_nat_nat m (nat.succ n)) : rfl ... = gpow a (-[1+ nat.pred (nat.sub (nat.succ n) m)]) : {sub_nat_nat_of_lt H} ... = (a ^ (nat.succ (nat.pred (nat.sub (nat.succ n) m))))⁻¹ : rfl ... = (a ^ (nat.succ n) * (a ^ m)⁻¹)⁻¹ : by krewrite [succ_pred_of_pos H1, pow_sub a (nat.le_of_lt H)] ... = a ^ m * (a ^ (nat.succ n))⁻¹ : by rewrite [mul_inv, inv_inv] ... = gpow a (of_nat m) * gpow a (-[1+n]) : rfl) (assume H : (m ≥ nat.succ n), calc gpow a ((of_nat m) + -[1+n]) = gpow a (sub_nat_nat m (nat.succ n)) : rfl ... = gpow a (#nat m - nat.succ n) : {sub_nat_nat_of_ge H} ... = a ^ m * (a ^ (nat.succ n))⁻¹ : pow_sub a H ... = gpow a (of_nat m) * gpow a (-[1+n]) : rfl) theorem gpow_add (a : A) : ∀i j : int, gpow a (i + j) = gpow a i * gpow a j | (of_nat m) (of_nat n) := !pow_add | (of_nat m) -[1+n] := !gpow_add_aux | -[1+m] (of_nat n) := by rewrite [add.comm, gpow_add_aux, ↑gpow, -*inv_pow, pow_inv_comm] | -[1+m] -[1+n] := calc gpow a (-[1+m] + -[1+n]) = (a^(#nat nat.succ m + nat.succ n))⁻¹ : rfl ... = (a^(nat.succ m))⁻¹ * (a^(nat.succ n))⁻¹ : by rewrite [pow_add, pow_comm, mul_inv] ... = gpow a (-[1+m]) * gpow a (-[1+n]) : rfl theorem gpow_comm (a : A) (i j : ℤ) : gpow a i * gpow a j = gpow a j * gpow a i := by rewrite [-*gpow_add, add.comm] end group section ordered_ring open nat variable [s : linear_ordered_ring A] include s theorem pow_pos {a : A} (H : a > 0) (n : ℕ) : a ^ n > 0 := begin induction n, krewrite pow_zero, apply zero_lt_one, rewrite pow_succ', apply mul_pos, apply v_0, apply H end theorem pow_ge_one_of_ge_one {a : A} (H : a ≥ 1) (n : ℕ) : a ^ n ≥ 1 := begin induction n, krewrite pow_zero, apply le.refl, rewrite [pow_succ', -mul_one 1], apply mul_le_mul v_0 H zero_le_one, apply le_of_lt, apply pow_pos, apply gt_of_ge_of_gt H zero_lt_one end theorem pow_two_add (n : ℕ) : (2:A)^n + 2^n = 2^(succ n) := by rewrite [pow_succ', -one_add_one_eq_two, left_distrib, *mul_one] end ordered_ring /- additive monoid -/ section add_monoid variable [s : add_monoid A] include s local attribute add_monoid.to_monoid [trans_instance] open nat definition nmul : ℕ → A → A := λ n a, a^n infix [priority algebra.prio] `⬝` := nmul theorem zero_nmul (a : A) : (0:ℕ) ⬝ a = 0 := pow_zero a theorem succ_nmul (n : ℕ) (a : A) : nmul (succ n) a = a + (nmul n a) := pow_succ a n theorem succ_nmul' (n : ℕ) (a : A) : succ n ⬝ a = nmul n a + a := pow_succ' a n theorem nmul_zero (n : ℕ) : n ⬝ 0 = (0:A) := one_pow n theorem one_nmul (a : A) : 1 ⬝ a = a := pow_one a theorem add_nmul (m n : ℕ) (a : A) : (m + n) ⬝ a = (m ⬝ a) + (n ⬝ a) := pow_add a m n theorem mul_nmul (m n : ℕ) (a : A) : (m * n) ⬝ a = m ⬝ (n ⬝ a) := eq.subst (mul.comm n m) (pow_mul a n m) theorem nmul_comm (m n : ℕ) (a : A) : (m ⬝ a) + (n ⬝ a) = (n ⬝ a) + (m ⬝ a) := pow_comm a m n end add_monoid /- additive commutative monoid -/ section add_comm_monoid open nat variable [s : add_comm_monoid A] include s local attribute add_comm_monoid.to_comm_monoid [trans_instance] theorem nmul_add (n : ℕ) (a b : A) : n ⬝ (a + b) = (n ⬝ a) + (n ⬝ b) := mul_pow a b n end add_comm_monoid section add_group variable [s : add_group A] include s local attribute add_group.to_group [trans_instance] section nat open nat theorem nmul_neg (n : ℕ) (a : A) : n ⬝ (-a) = -(n ⬝ a) := inv_pow a n theorem sub_nmul {m n : ℕ} (a : A) (H : m ≥ n) : (m - n) ⬝ a = (m ⬝ a) + -(n ⬝ a) := pow_sub a H theorem nmul_neg_comm (m n : ℕ) (a : A) : (m ⬝ (-a)) + (n ⬝ a) = (n ⬝ a) + (m ⬝ (-a)) := pow_inv_comm a m n end nat open int definition imul : ℤ → A → A := λ i a, gpow a i theorem add_imul (i j : ℤ) (a : A) : imul (i + j) a = imul i a + imul j a := gpow_add a i j theorem imul_comm (i j : ℤ) (a : A) : imul i a + imul j a = imul j a + imul i a := gpow_comm a i j end add_group
3b791bf5760db07e2da58a720391ede4511c417c
36c7a18fd72e5b57229bd8ba36493daf536a19ce
/library/algebra/order.lean
d5bd3b91eecff71d064468683c983e45a1b673b7
[ "Apache-2.0" ]
permissive
YHVHvx/lean
732bf0fb7a298cd7fe0f15d82f8e248c11db49e9
038369533e0136dd395dc252084d3c1853accbf2
refs/heads/master
1,610,701,080,210
1,449,128,595,000
1,449,128,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
16,402
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad Weak orders "≤", strict orders "<", and structures that include both. -/ import logic.eq logic.connectives algebra.binary algebra.priority open eq eq.ops namespace algebra variable {A : Type} /- weak orders -/ structure weak_order [class] (A : Type) extends has_le A := (le_refl : ∀a, le a a) (le_trans : ∀a b c, le a b → le b c → le a c) (le_antisymm : ∀a b, le a b → le b a → a = b) section variable [s : weak_order A] include s theorem le.refl (a : A) : a ≤ a := !weak_order.le_refl theorem le.trans [trans] {a b c : A} : a ≤ b → b ≤ c → a ≤ c := !weak_order.le_trans theorem ge.trans [trans] {a b c : A} (H1 : a ≥ b) (H2: b ≥ c) : a ≥ c := le.trans H2 H1 theorem le.antisymm {a b : A} : a ≤ b → b ≤ a → a = b := !weak_order.le_antisymm -- Alternate syntax. (Abbreviations do not migrate well.) theorem eq_of_le_of_ge {a b : A} : a ≤ b → b ≤ a → a = b := !le.antisymm end structure linear_weak_order [class] (A : Type) extends weak_order A := (le_total : ∀a b, le a b ∨ le b a) theorem le.total [s : linear_weak_order A] (a b : A) : a ≤ b ∨ b ≤ a := !linear_weak_order.le_total /- strict orders -/ structure strict_order [class] (A : Type) extends has_lt A := (lt_irrefl : ∀a, ¬ lt a a) (lt_trans : ∀a b c, lt a b → lt b c → lt a c) section variable [s : strict_order A] include s theorem lt.irrefl (a : A) : ¬ a < a := !strict_order.lt_irrefl theorem not_lt_self (a : A) : ¬ a < a := !lt.irrefl -- alternate syntax theorem lt_self_iff_false [simp] (a : A) : a < a ↔ false := iff_false_intro (lt.irrefl a) theorem lt.trans [trans] {a b c : A} : a < b → b < c → a < c := !strict_order.lt_trans theorem gt.trans [trans] {a b c : A} (H1 : a > b) (H2: b > c) : a > c := lt.trans H2 H1 theorem ne_of_lt {a b : A} (lt_ab : a < b) : a ≠ b := assume eq_ab : a = b, show false, from lt.irrefl b (eq_ab ▸ lt_ab) theorem ne_of_gt {a b : A} (gt_ab : a > b) : a ≠ b := ne.symm (ne_of_lt gt_ab) theorem lt.asymm {a b : A} (H : a < b) : ¬ b < a := assume H1 : b < a, lt.irrefl _ (lt.trans H H1) theorem not_lt_of_gt {a b : A} (H : a > b) : ¬ a < b := !lt.asymm H -- alternate syntax end /- well-founded orders -/ structure wf_strict_order [class] (A : Type) extends strict_order A := (wf_rec : ∀P : A → Type, (∀x, (∀y, lt y x → P y) → P x) → ∀x, P x) definition wf.rec_on {A : Type} [s : wf_strict_order A] {P : A → Type} (x : A) (H : ∀x, (∀y, wf_strict_order.lt y x → P y) → P x) : P x := wf_strict_order.wf_rec P H x theorem wf.ind_on.{u v} {A : Type.{u}} [s : wf_strict_order.{u 0} A] {P : A → Prop} (x : A) (H : ∀x, (∀y, wf_strict_order.lt y x → P y) → P x) : P x := wf.rec_on x H /- structures with a weak and a strict order -/ structure order_pair [class] (A : Type) extends weak_order A, has_lt A := (le_of_lt : ∀ a b, lt a b → le a b) (lt_of_lt_of_le : ∀ a b c, lt a b → le b c → lt a c) (lt_of_le_of_lt : ∀ a b c, le a b → lt b c → lt a c) (lt_irrefl : ∀ a, ¬ lt a a) section variable [s : order_pair A] variables {a b c : A} include s theorem le_of_lt : a < b → a ≤ b := !order_pair.le_of_lt theorem lt_of_lt_of_le [trans] : a < b → b ≤ c → a < c := !order_pair.lt_of_lt_of_le theorem lt_of_le_of_lt [trans] : a ≤ b → b < c → a < c := !order_pair.lt_of_le_of_lt private theorem lt_irrefl (s' : order_pair A) (a : A) : ¬ a < a := !order_pair.lt_irrefl private theorem lt_trans (s' : order_pair A) (a b c: A) (lt_ab : a < b) (lt_bc : b < c) : a < c := lt_of_lt_of_le lt_ab (le_of_lt lt_bc) definition order_pair.to_strict_order [trans_instance] [reducible] : strict_order A := ⦃ strict_order, s, lt_irrefl := lt_irrefl s, lt_trans := lt_trans s ⦄ theorem gt_of_gt_of_ge [trans] (H1 : a > b) (H2 : b ≥ c) : a > c := lt_of_le_of_lt H2 H1 theorem gt_of_ge_of_gt [trans] (H1 : a ≥ b) (H2 : b > c) : a > c := lt_of_lt_of_le H2 H1 theorem not_le_of_gt (H : a > b) : ¬ a ≤ b := assume H1 : a ≤ b, lt.irrefl _ (lt_of_lt_of_le H H1) theorem not_lt_of_ge (H : a ≥ b) : ¬ a < b := assume H1 : a < b, lt.irrefl _ (lt_of_le_of_lt H H1) end structure strong_order_pair [class] (A : Type) extends weak_order A, has_lt A := (le_iff_lt_or_eq : ∀a b, le a b ↔ lt a b ∨ a = b) (lt_irrefl : ∀ a, ¬ lt a a) theorem le_iff_lt_or_eq [s : strong_order_pair A] {a b : A} : a ≤ b ↔ a < b ∨ a = b := !strong_order_pair.le_iff_lt_or_eq theorem lt_or_eq_of_le [s : strong_order_pair A] {a b : A} (le_ab : a ≤ b) : a < b ∨ a = b := iff.mp le_iff_lt_or_eq le_ab theorem le_of_lt_or_eq [s : strong_order_pair A] {a b : A} (lt_or_eq : a < b ∨ a = b) : a ≤ b := iff.mpr le_iff_lt_or_eq lt_or_eq private theorem lt_irrefl' [s : strong_order_pair A] (a : A) : ¬ a < a := !strong_order_pair.lt_irrefl private theorem le_of_lt' [s : strong_order_pair A] (a b : A) : a < b → a ≤ b := take Hlt, le_of_lt_or_eq (or.inl Hlt) private theorem lt_iff_le_and_ne [s : strong_order_pair A] {a b : A} : a < b ↔ (a ≤ b ∧ a ≠ b) := iff.intro (take Hlt, and.intro (le_of_lt_or_eq (or.inl Hlt)) (take Hab, absurd (Hab ▸ Hlt) !lt_irrefl')) (take Hand, have Hor : a < b ∨ a = b, from lt_or_eq_of_le (and.left Hand), or_resolve_left Hor (and.right Hand)) theorem lt_of_le_of_ne [s : strong_order_pair A] {a b : A} : a ≤ b → a ≠ b → a < b := take H1 H2, iff.mpr lt_iff_le_and_ne (and.intro H1 H2) private theorem ne_of_lt' [s : strong_order_pair A] {a b : A} (H : a < b) : a ≠ b := and.right ((iff.mp lt_iff_le_and_ne) H) private theorem lt_of_lt_of_le' [s : strong_order_pair A] (a b c : A) : a < b → b ≤ c → a < c := assume lt_ab : a < b, assume le_bc : b ≤ c, have le_ac : a ≤ c, from le.trans (le_of_lt' _ _ lt_ab) le_bc, have ne_ac : a ≠ c, from assume eq_ac : a = c, have le_ba : b ≤ a, from eq_ac⁻¹ ▸ le_bc, have eq_ab : a = b, from le.antisymm (le_of_lt' _ _ lt_ab) le_ba, show false, from ne_of_lt' lt_ab eq_ab, show a < c, from iff.mpr (lt_iff_le_and_ne) (and.intro le_ac ne_ac) theorem lt_of_le_of_lt' [s : strong_order_pair A] (a b c : A) : a ≤ b → b < c → a < c := assume le_ab : a ≤ b, assume lt_bc : b < c, have le_ac : a ≤ c, from le.trans le_ab (le_of_lt' _ _ lt_bc), have ne_ac : a ≠ c, from assume eq_ac : a = c, have le_cb : c ≤ b, from eq_ac ▸ le_ab, have eq_bc : b = c, from le.antisymm (le_of_lt' _ _ lt_bc) le_cb, show false, from ne_of_lt' lt_bc eq_bc, show a < c, from iff.mpr (lt_iff_le_and_ne) (and.intro le_ac ne_ac) definition strong_order_pair.to_order_pair [trans_instance] [reducible] [s : strong_order_pair A] : order_pair A := ⦃ order_pair, s, lt_irrefl := lt_irrefl', le_of_lt := le_of_lt', lt_of_le_of_lt := lt_of_le_of_lt', lt_of_lt_of_le := lt_of_lt_of_le' ⦄ /- linear orders -/ structure linear_order_pair [class] (A : Type) extends order_pair A, linear_weak_order A structure linear_strong_order_pair [class] (A : Type) extends strong_order_pair A, linear_weak_order A definition linear_strong_order_pair.to_linear_order_pair [trans_instance] [reducible] [s : linear_strong_order_pair A] : linear_order_pair A := ⦃ linear_order_pair, s, strong_order_pair.to_order_pair ⦄ section variable [s : linear_strong_order_pair A] variables (a b c : A) include s theorem lt.trichotomy : a < b ∨ a = b ∨ b < a := or.elim (le.total a b) (assume H : a ≤ b, or.elim (iff.mp !le_iff_lt_or_eq H) (assume H1, or.inl H1) (assume H1, or.inr (or.inl H1))) (assume H : b ≤ a, or.elim (iff.mp !le_iff_lt_or_eq H) (assume H1, or.inr (or.inr H1)) (assume H1, or.inr (or.inl (H1⁻¹)))) theorem lt.by_cases {a b : A} {P : Prop} (H1 : a < b → P) (H2 : a = b → P) (H3 : b < a → P) : P := or.elim !lt.trichotomy (assume H, H1 H) (assume H, or.elim H (assume H', H2 H') (assume H', H3 H')) theorem le_of_not_gt {a b : A} (H : ¬ a > b) : a ≤ b := lt.by_cases (assume H', absurd H' H) (assume H', H' ▸ !le.refl) (assume H', le_of_lt H') theorem lt_of_not_ge {a b : A} (H : ¬ a ≥ b) : a < b := lt.by_cases (assume H', absurd (le_of_lt H') H) (assume H', absurd (H' ▸ !le.refl) H) (assume H', H') theorem lt_or_ge : a < b ∨ a ≥ b := lt.by_cases (assume H1 : a < b, or.inl H1) (assume H1 : a = b, or.inr (H1 ▸ le.refl a)) (assume H1 : a > b, or.inr (le_of_lt H1)) theorem le_or_gt : a ≤ b ∨ a > b := !or.swap (lt_or_ge b a) theorem lt_or_gt_of_ne {a b : A} (H : a ≠ b) : a < b ∨ a > b := lt.by_cases (assume H1, or.inl H1) (assume H1, absurd H1 H) (assume H1, or.inr H1) end open decidable structure decidable_linear_order [class] (A : Type) extends linear_strong_order_pair A := (decidable_lt : decidable_rel lt) section variable [s : decidable_linear_order A] variables {a b c d : A} include s open decidable definition decidable_lt [instance] : decidable (a < b) := @decidable_linear_order.decidable_lt _ _ _ _ definition decidable_le [instance] : decidable (a ≤ b) := by_cases (assume H : a < b, inl (le_of_lt H)) (assume H : ¬ a < b, have H1 : b ≤ a, from le_of_not_gt H, by_cases (assume H2 : b < a, inr (not_le_of_gt H2)) (assume H2 : ¬ b < a, inl (le_of_not_gt H2))) definition has_decidable_eq [instance] : decidable (a = b) := by_cases (assume H : a ≤ b, by_cases (assume H1 : b ≤ a, inl (le.antisymm H H1)) (assume H1 : ¬ b ≤ a, inr (assume H2 : a = b, H1 (H2 ▸ le.refl a)))) (assume H : ¬ a ≤ b, (inr (assume H1 : a = b, H (H1 ▸ !le.refl)))) theorem eq_or_lt_of_not_lt {a b : A} (H : ¬ a < b) : a = b ∨ b < a := if Heq : a = b then or.inl Heq else or.inr (lt_of_not_ge (λ Hge, H (lt_of_le_of_ne Hge Heq))) theorem eq_or_lt_of_le {a b : A} (H : a ≤ b) : a = b ∨ a < b := begin cases eq_or_lt_of_not_lt (not_lt_of_ge H), exact or.inl a_1⁻¹, exact or.inr a_1 end -- testing equality first may result in more definitional equalities definition lt.cases {B : Type} (a b : A) (t_lt t_eq t_gt : B) : B := if a = b then t_eq else (if a < b then t_lt else t_gt) theorem lt.cases_of_eq {B : Type} {a b : A} {t_lt t_eq t_gt : B} (H : a = b) : lt.cases a b t_lt t_eq t_gt = t_eq := if_pos H theorem lt.cases_of_lt {B : Type} {a b : A} {t_lt t_eq t_gt : B} (H : a < b) : lt.cases a b t_lt t_eq t_gt = t_lt := if_neg (ne_of_lt H) ⬝ if_pos H theorem lt.cases_of_gt {B : Type} {a b : A} {t_lt t_eq t_gt : B} (H : a > b) : lt.cases a b t_lt t_eq t_gt = t_gt := if_neg (ne.symm (ne_of_lt H)) ⬝ if_neg (lt.asymm H) definition min (a b : A) : A := if a ≤ b then a else b definition max (a b : A) : A := if a ≤ b then b else a /- these show min and max form a lattice -/ theorem min_le_left (a b : A) : min a b ≤ a := by_cases (assume H : a ≤ b, by rewrite [↑min, if_pos H]; apply le.refl) (assume H : ¬ a ≤ b, by rewrite [↑min, if_neg H]; apply le_of_lt (lt_of_not_ge H)) theorem min_le_right (a b : A) : min a b ≤ b := by_cases (assume H : a ≤ b, by rewrite [↑min, if_pos H]; apply H) (assume H : ¬ a ≤ b, by rewrite [↑min, if_neg H]; apply le.refl) theorem le_min {a b c : A} (H₁ : c ≤ a) (H₂ : c ≤ b) : c ≤ min a b := by_cases (assume H : a ≤ b, by rewrite [↑min, if_pos H]; apply H₁) (assume H : ¬ a ≤ b, by rewrite [↑min, if_neg H]; apply H₂) theorem le_max_left (a b : A) : a ≤ max a b := by_cases (assume H : a ≤ b, by rewrite [↑max, if_pos H]; apply H) (assume H : ¬ a ≤ b, by rewrite [↑max, if_neg H]; apply le.refl) theorem le_max_right (a b : A) : b ≤ max a b := by_cases (assume H : a ≤ b, by rewrite [↑max, if_pos H]; apply le.refl) (assume H : ¬ a ≤ b, by rewrite [↑max, if_neg H]; apply le_of_lt (lt_of_not_ge H)) theorem max_le {a b c : A} (H₁ : a ≤ c) (H₂ : b ≤ c) : max a b ≤ c := by_cases (assume H : a ≤ b, by rewrite [↑max, if_pos H]; apply H₂) (assume H : ¬ a ≤ b, by rewrite [↑max, if_neg H]; apply H₁) theorem le_max_left_iff_true [simp] (a b : A) : a ≤ max a b ↔ true := iff_true_intro (le_max_left a b) theorem le_max_right_iff_true [simp] (a b : A) : b ≤ max a b ↔ true := iff_true_intro (le_max_right a b) /- these are also proved for lattices, but with inf and sup in place of min and max -/ theorem eq_min {a b c : A} (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 !min_le_right) theorem min.comm (a b : A) : min a b = min b a := eq_min !min_le_right !min_le_left (λ c H₁ H₂, le_min H₂ H₁) theorem min.assoc (a b c : A) : 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 theorem min.left_comm (a b c : A) : min a (min b c) = min b (min a c) := binary.left_comm (@min.comm A s) (@min.assoc A s) a b c theorem min.right_comm (a b c : A) : min (min a b) c = min (min a c) b := binary.right_comm (@min.comm A s) (@min.assoc A s) a b c theorem min_self (a : A) : min a a = a := by apply eq.symm; apply eq_min (le.refl a) !le.refl; intros; assumption theorem min_eq_left {a b : A} (H : a ≤ b) : min a b = a := by apply eq.symm; apply eq_min !le.refl H; intros; assumption theorem min_eq_right {a b : A} (H : b ≤ a) : min a b = b := eq.subst !min.comm (min_eq_left H) theorem eq_max {a b c : A} (H₁ : a ≤ c) (H₂ : b ≤ c) (H₃ : ∀{d}, a ≤ d → b ≤ d → c ≤ d) : c = max a b := le.antisymm (H₃ !le_max_left !le_max_right) (max_le H₁ H₂) theorem max.comm (a b : A) : max a b = max b a := eq_max !le_max_right !le_max_left (λ c H₁ H₂, max_le H₂ H₁) theorem max.assoc (a b c : A) : 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 theorem max.left_comm (a b c : A) : max a (max b c) = max b (max a c) := binary.left_comm (@max.comm A s) (@max.assoc A s) a b c theorem max.right_comm (a b c : A) : max (max a b) c = max (max a c) b := binary.right_comm (@max.comm A s) (@max.assoc A s) a b c theorem max_self (a : A) : max a a = a := by apply eq.symm; apply eq_max (le.refl a) !le.refl; intros; assumption theorem max_eq_left {a b : A} (H : b ≤ a) : max a b = a := by apply eq.symm; apply eq_max !le.refl H; intros; assumption theorem max_eq_right {a b : A} (H : a ≤ b) : max a b = b := eq.subst !max.comm (max_eq_left H) /- these rely on lt_of_lt -/ theorem min_eq_left_of_lt {a b : A} (H : a < b) : min a b = a := min_eq_left (le_of_lt H) theorem min_eq_right_of_lt {a b : A} (H : b < a) : min a b = b := min_eq_right (le_of_lt H) theorem max_eq_left_of_lt {a b : A} (H : b < a) : max a b = a := max_eq_left (le_of_lt H) theorem max_eq_right_of_lt {a b : A} (H : a < b) : max a b = b := max_eq_right (le_of_lt H) /- these use the fact that it is a linear ordering -/ theorem lt_min {a b c : A} (H₁ : a < b) (H₂ : a < c) : a < min b c := or.elim !le_or_gt (assume H : b ≤ c, by rewrite (min_eq_left H); apply H₁) (assume H : b > c, by rewrite (min_eq_right_of_lt H); apply H₂) theorem max_lt {a b c : A} (H₁ : a < c) (H₂ : b < c) : max a b < c := or.elim !le_or_gt (assume H : a ≤ b, by rewrite (max_eq_right H); apply H₂) (assume H : a > b, by rewrite (max_eq_left_of_lt H); apply H₁) end end algebra
0dbbcca9fd890d934909163f15c7487cf51632b0
d450724ba99f5b50b57d244eb41fef9f6789db81
/src/mywork/lectures/lecture_7.lean
b17d986f168fafc1e7b395bdff93adf4b22f7ae5
[]
no_license
jakekauff/CS2120F21
4f009adeb4ce4a148442b562196d66cc6c04530c
e69529ec6f5d47a554291c4241a3d8ec4fe8f5ad
refs/heads/main
1,693,841,880,030
1,637,604,848,000
1,637,604,848,000
399,946,698
0
0
null
null
null
null
UTF-8
Lean
false
false
7,825
lean
namespace implies /- When working on logic in Lean, we can represent assumptions as axioms. For example, assume that an indentifier is bound to a value of a given type. -/ axioms (P Q : Prop) /- In this example, we use axiom to "assume" that P and Q are names (identifiers) bound to two arbitrary propositions. A proposition, in our logic, is a type, the values of which constitute proofs of that proposition. Propositions are special types, each being of the special type, Prop. Prop is the type of all propositions. A proposition both is a type (of its proofs) and has a type (Prop). -/ /- Having assumed that P and Q are propositions, next we assume that P is true. In a constructive logic such as Lean, the way we represent that a proposition is true is by assuming that we have a proof of it. We thus represent the assumption that there is a proof (value) of type P. -/ axiom p : P /- Next we want to assume that P → Q. In Enlish we'd say that we want to assume P implies Q: if P is true, then Q is true. P → Q is itself a proposition, and to assume it's true, we'll assume that we have a proof of it. -/ axiom pq : P → Q /- We now assumed both P → Q and P, represented by the proof objects (values of these types) p, and pq, respectively. These assumptions put us in a position to use the *elimination* rule for → to deduce that Q is true (which is to say, to construct a proof of Q). In English, we could give a proof of Q like this: Theorem: Q is true. Proof: We have that P → Q is true and so is P. It follows from the elimination rule for → that Q must be true. In constructive logic, we'd say this: Apply our (assumed) proof of P → Q to our (assumed) proof of P to produce a proof of Q. This show that if we're given any proof of P we can always create a proof of Q by *applying* our proof of P → Q to our proof of P. -/ #check pq #check p #check (pq p) -- applying pq to p returns a proof of Q! /- Proof: By the elimination rule for → (with pq applied to p). Proof: By "modus ponens". QED -/ /- Inference rule notation. (P Q : Prop) (pq : P → Q) (p : P) --------------------------------- → elim q : Q -/ end implies /- FORALL -/ namespace all /- To understand ∀ propositions, you really have to understand *predicates*. A predicate is a parameterized proposition. Think of it as a proposition with blanks that you can fill in. Filling them in reduces a predicate to a proposition *about* the specific values that you used to fill in the blanks. In constructive logic, (2) a predicate is just a *function* that takes values for the blanks and yields a proposition with the blanks filled in with the given values. Predicates can take any number of parameters. A great example is *equality* it's a predicate that takes two parameters (of the same type) and yeilds the proposition that those particular values are equal: 0 = 0, 1 = 5, tt = ff, etc. Some of the resulting propositions are true (and can be proved, e.g., 0 = 0), while some are not true (e.g., 1=5), and have no proofs. Another good example of a predicate is ev, our "evenness predicate." It takes any arbitrary natural number (say, n) as an argument and then reduces to the proposition that that n is even, in the sense that n % 2 = 0. -/ def ev (n : ℕ) : Prop := n % 2 = 0 /- The predicate ev applied to a natural number, n, yields a result of type Prop (a proposition): namely, the equality proposition n % 2 = 0. Here are a few examples of propositions generated by applying ev to various arguments (of type ℕ). -/ -- Note that Lean reduces n%2 for us, given n #reduce ev 0 -- yay #reduce ev 1 -- boo #reduce ev 2 -- yay #reduce ev 3 -- boo #reduce ev 4 -- yay #reduce ev 5 -- boo /- A predicate in effect defines a *set of values: those values that satisfy the predicate: that produce a proposition that is true (for which there are proofs.) Here it's clear that there are proofs by reflexivity of equality for the propositions, ev 0, ev 2, ev 4, but no proofs for ev 1, ev 3, ev 5, etc. Predicate thus also can be seen as defining *properties* of objects. Here 0, 2, 4 have the property of being "ev", while 1, 3, and 5 do not. -/ /- Now we can really get to a forall proposition. Let's assert that every natural number is equal to itself. -/ theorem neqn : ∀ (n : ℕ), n = n := begin assume n, exact eq.refl n, end /- The proof of this simple theorem is easy. First we use the introduction rule for ∀: we assume we're given an arbitrary natural number, n. In that context we then need to prove the rest, which we do by constructing a proof that this arbitrary but specific n is equal to itself. -/ /- What's remarkable is that we can *use* a proof of a ∀ as a sort of function: in this case, one that takes an arbitrary n as an argument and that returns a proof of n = n. -/ #reduce neqn 0 #reduce neqn 1 #reduce neqn 2 /- Now let's be much more general about it. We will make a few assumptions for purposes of an example. -/ axioms (T : Type) -- T is any type (P : T → Prop) -- P is any property (like ev) (t : T) -- t is a specific value (like 1) (a : ∀ (x : T), P x) -- a is a proof of this forall prop /- We can now use the proof, a of the ∀ proposition, to show that any specific value, such as t here, has the property P. We do this by applying the proof a to t. This is the elimination rule for ∀ and is called universal specialization or instantiation. -/ example : P t := a t /- Applying a to t yields a proof of the proposition P t. In English, we've got a proof (a) of ∀ (x : T), P x. We thus know that "every x has property P." To show that a particular x, namely t, as property P, we use the elimination rule for ∀ by applying a to t, which then yields a proof that t, in particular, satisfies the predicate P (has property P). QED. -/ #check (a t) -- syntax for application of a to t /- Inference rule notation. (T : Type) (P : T → Prop) (a : ∀ (x : T), P x) (t : T) ------------------------------------------------------ ∀ elim pf: P t -/ end all /- AND & → -/ /- Inference rule / axioms for ∧ Introduction: (P Q : Prop) (p : P) (q : Q) ---------------------------- ∧ intro ⟨p, q⟩ : P ∧ Q (P Q : Prop) (pq : P ∧ Q) ------------------------- ∧ elim (left) p : P (P Q : Prop) (pq : P ∧ Q) ------------------------- ∧ elim (right) q : Q -/ axioms (P Q : Prop) -- let's assume P, Q are propositions -- Then so is (P ∧ Q) a proposition #check P ∧ Q -- Let's assume we have proofs of P and Q, respectively axioms (p : P) (q : Q) -- we can apply the introduction rule for ∧ def pq : P ∧ Q := and.intro p q -- Given a proof of P ∧ Q we can have proofs of P, Q, resp. #check and.elim_left pq #check and.elim_right pq -- Here's a nice shorthand #check pq.left #check pq.right /- Theorem: Logical and is associative. What this means is that if P ∧ (Q ∧ R) is true, then so is (P ∧ Q) ∧ R. First, think about it intuitively. If P is true, we have a proof of it, call it p. If (Q ∧ R) is true, we have a proof of it, call it qr. From qr we can have a proof of Q and a proof of R, call them q and r, respectively. To show that (P ∧ Q) ∧ R is true, we need a proof of P ∧ Q and a proof of R. We can apply and.intro to p and q to get a proof, pq, of P ∧ Q. Finally, we can apply and.intro again to pq and r to get the proof we sought. QED. -/ /- Exercise: formalize the proposition and a proof of it. -/ theorem and_associative : _ := _ -- Lean's proof of associativity #check @and.assoc /- Note the use of a different connective, bi-implication. We'll talk about how it differs from simple implication shortly. -/
e46c17e975509cd9a3543e88d697af3c804134b6
53618200bef52920c1e974173f78cd378d268f3e
/hott/init/pointed.hlean
256b5b7673456e6363bf47c773dc555707d05f09
[ "Apache-2.0" ]
permissive
sayantangkhan/lean2
cc41e61102e0fcc8b65e8501186dcca40b19b22e
0fc0378969678eec25ea425a426bb48a184a6db0
refs/heads/master
1,590,323,112,724
1,489,425,932,000
1,489,425,932,000
84,854,525
0
0
null
1,489,425,509,000
1,489,425,509,000
null
UTF-8
Lean
false
false
4,066
hlean
/- Copyright (c) 2016 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn The definition of pointed types. This file is here to avoid circularities in the import graph -/ prelude import init.trunc open eq equiv is_equiv is_trunc structure pointed [class] (A : Type) := (point : A) structure pType := (carrier : Type) (Point : carrier) notation `Type*` := pType namespace pointed attribute pType.carrier [coercion] variables {A : Type} definition pt [reducible] [unfold 2] [H : pointed A] := point A definition Point [reducible] [unfold 1] (A : Type*) := pType.Point A definition carrier [reducible] [unfold 1] (A : Type*) := pType.carrier A protected definition Mk [constructor] {A : Type} (a : A) := pType.mk A a protected definition MK [constructor] (A : Type) (a : A) := pType.mk A a protected definition mk' [constructor] (A : Type) [H : pointed A] : Type* := pType.mk A (point A) definition pointed_carrier [instance] [constructor] (A : Type*) : pointed A := pointed.mk (Point A) end pointed open pointed section universe variable u structure ptrunctype (n : ℕ₋₂) extends trunctype.{u} n, pType.{u} definition is_trunc_ptrunctype [instance] {n : ℕ₋₂} (X : ptrunctype n) : is_trunc n (ptrunctype.to_pType X) := trunctype.struct X end notation n `-Type*` := ptrunctype n abbreviation pSet [parsing_only] := 0-Type* notation `Set*` := pSet namespace pointed protected definition ptrunctype.mk' [constructor] (n : ℕ₋₂) (A : Type) [pointed A] [is_trunc n A] : n-Type* := ptrunctype.mk A _ pt protected definition pSet.mk [constructor] := @ptrunctype.mk (-1.+1) protected definition pSet.mk' [constructor] := ptrunctype.mk' (-1.+1) definition ptrunctype_of_trunctype [constructor] {n : ℕ₋₂} (A : n-Type) (a : A) : n-Type* := ptrunctype.mk A _ a definition ptrunctype_of_pType [constructor] {n : ℕ₋₂} (A : Type*) (H : is_trunc n A) : n-Type* := ptrunctype.mk A _ pt definition pSet_of_Set [constructor] (A : Set) (a : A) : Set* := ptrunctype.mk A _ a definition pSet_of_pType [constructor] (A : Type*) (H : is_set A) : Set* := ptrunctype.mk A _ pt attribute ptrunctype._trans_of_to_pType ptrunctype.to_pType ptrunctype.to_trunctype [unfold 2] -- Any contractible type is pointed definition pointed_of_is_contr [instance] [priority 800] [constructor] (A : Type) [H : is_contr A] : pointed A := pointed.mk !center end pointed /- pointed maps -/ structure ppi (A : Type*) (P : A → Type*) := (to_fun : Π a : A, P a) (resp_pt : to_fun (Point A) = Point (P (Point A))) -- definition pmap (A B : Type*) := @ppi A (λa, B) structure pmap (A B : Type*) := (to_fun : A → B) (resp_pt : to_fun (Point A) = Point B) namespace pointed abbreviation respect_pt [unfold 3] := @pmap.resp_pt notation `map₊` := pmap infix ` →* `:30 := pmap attribute pmap.to_fun ppi.to_fun [coercion] notation `Π*` binders `, ` r:(scoped P, ppi _ P) := r -- definition pmap.mk [constructor] {A B : Type*} (f : A → B) (p : f pt = pt) : A →* B := -- ppi.mk f p -- definition pmap.to_fun [coercion] [unfold 3] {A B : Type*} (f : A →* B) : A → B := f end pointed open pointed /- pointed homotopies -/ structure phomotopy {A B : Type*} (f g : A →* B) := (homotopy : f ~ g) (homotopy_pt : homotopy pt ⬝ respect_pt g = respect_pt f) namespace pointed variables {A B : Type*} {f g : A →* B} infix ` ~* `:50 := phomotopy abbreviation to_homotopy_pt [unfold 5] := @phomotopy.homotopy_pt abbreviation to_homotopy [coercion] [unfold 5] (p : f ~* g) : Πa, f a = g a := phomotopy.homotopy p /- pointed equivalences -/ structure pequiv (A B : Type*) extends equiv A B, pmap A B attribute pequiv._trans_of_to_pmap pequiv._trans_of_to_equiv pequiv.to_pmap pequiv.to_equiv [unfold 3] attribute pequiv.to_is_equiv [instance] attribute pequiv.to_pmap [coercion] infix ` ≃* `:25 := pequiv end pointed
693d8770790282f90644e487e0d831efb57f34a1
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/special_functions/pow.lean
49f9037f671ffa1810ae8179d2886f3ff65e4e0f
[]
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
37,284
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Sébastien Gouëzel, Rémy Degenne -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.special_functions.trigonometric import Mathlib.analysis.calculus.extend_deriv import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Power function on `ℂ`, `ℝ`, `ℝ≥0`, and `ennreal` We construct the power functions `x ^ y` where * `x` and `y` are complex numbers, * or `x` and `y` are real numbers, * or `x` is a nonnegative real number and `y` is a real number; * or `x` is a number from `[0, +∞]` (a.k.a. `ennreal`) and `y` is a real number. We also prove basic properties of these functions. -/ namespace complex /-- The complex power function `x^y`, given by `x^y = exp(y log x)` (where `log` is the principal determination of the logarithm), unless `x = 0` where one sets `0^0 = 1` and `0^y = 0` for `y ≠ 0`. -/ def cpow (x : ℂ) (y : ℂ) : ℂ := ite (x = 0) (ite (y = 0) 1 0) (exp (log x * y)) protected instance has_pow : has_pow ℂ ℂ := has_pow.mk cpow @[simp] theorem cpow_eq_pow (x : ℂ) (y : ℂ) : cpow x y = x ^ y := rfl theorem cpow_def (x : ℂ) (y : ℂ) : x ^ y = ite (x = 0) (ite (y = 0) 1 0) (exp (log x * y)) := rfl @[simp] theorem cpow_zero (x : ℂ) : x ^ 0 = 1 := sorry @[simp] theorem cpow_eq_zero_iff (x : ℂ) (y : ℂ) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := sorry @[simp] theorem zero_cpow {x : ℂ} (h : x ≠ 0) : 0 ^ x = 0 := sorry @[simp] theorem cpow_one (x : ℂ) : x ^ 1 = x := sorry @[simp] theorem one_cpow (x : ℂ) : 1 ^ x = 1 := sorry theorem cpow_add {x : ℂ} (y : ℂ) (z : ℂ) (hx : x ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := sorry theorem cpow_mul {x : ℂ} {y : ℂ} (z : ℂ) (h₁ : -real.pi < im (log x * y)) (h₂ : im (log x * y) ≤ real.pi) : x ^ (y * z) = (x ^ y) ^ z := sorry theorem cpow_neg (x : ℂ) (y : ℂ) : x ^ (-y) = (x ^ y⁻¹) := sorry theorem cpow_neg_one (x : ℂ) : x ^ (-1) = (x⁻¹) := sorry @[simp] theorem cpow_nat_cast (x : ℂ) (n : ℕ) : x ^ ↑n = x ^ n := sorry @[simp] theorem cpow_int_cast (x : ℂ) (n : ℤ) : x ^ ↑n = x ^ n := sorry theorem cpow_nat_inv_pow (x : ℂ) {n : ℕ} (hn : 0 < n) : (x ^ (↑n⁻¹)) ^ n = x := sorry end complex namespace real /-- The real power function `x^y`, defined as the real part of the complex power function. For `x > 0`, it is equal to `exp(y log x)`. For `x = 0`, one sets `0^0=1` and `0^y=0` for `y ≠ 0`. For `x < 0`, the definition is somewhat arbitary as it depends on the choice of a complex determination of the logarithm. With our conventions, it is equal to `exp (y log x) cos (πy)`. -/ def rpow (x : ℝ) (y : ℝ) : ℝ := complex.re (↑x ^ ↑y) protected instance has_pow : has_pow ℝ ℝ := has_pow.mk rpow @[simp] theorem rpow_eq_pow (x : ℝ) (y : ℝ) : rpow x y = x ^ y := rfl theorem rpow_def (x : ℝ) (y : ℝ) : x ^ y = complex.re (↑x ^ ↑y) := rfl theorem rpow_def_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ y = ite (x = 0) (ite (y = 0) 1 0) (exp (log x * y)) := sorry theorem rpow_def_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : x ^ y = exp (log x * y) := sorry theorem exp_mul (x : ℝ) (y : ℝ) : exp (x * y) = exp x ^ y := eq.mpr (id (Eq._oldrec (Eq.refl (exp (x * y) = exp x ^ y)) (rpow_def_of_pos (exp_pos x) y))) (eq.mpr (id (Eq._oldrec (Eq.refl (exp (x * y) = exp (log (exp x) * y))) (log_exp x))) (Eq.refl (exp (x * y)))) theorem rpow_eq_zero_iff_of_nonneg {x : ℝ} {y : ℝ} (hx : 0 ≤ x) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := sorry theorem rpow_def_of_neg {x : ℝ} (hx : x < 0) (y : ℝ) : x ^ y = exp (log x * y) * cos (y * pi) := sorry theorem rpow_def_of_nonpos {x : ℝ} (hx : x ≤ 0) (y : ℝ) : x ^ y = ite (x = 0) (ite (y = 0) 1 0) (exp (log x * y) * cos (y * pi)) := sorry theorem rpow_pos_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : 0 < x ^ y := eq.mpr (id (Eq._oldrec (Eq.refl (0 < x ^ y)) (rpow_def_of_pos hx y))) (exp_pos (log x * y)) @[simp] theorem rpow_zero (x : ℝ) : x ^ 0 = 1 := sorry @[simp] theorem zero_rpow {x : ℝ} (h : x ≠ 0) : 0 ^ x = 0 := sorry @[simp] theorem rpow_one (x : ℝ) : x ^ 1 = x := sorry @[simp] theorem one_rpow (x : ℝ) : 1 ^ x = 1 := sorry theorem zero_rpow_le_one (x : ℝ) : 0 ^ x ≤ 1 := sorry theorem zero_rpow_nonneg (x : ℝ) : 0 ≤ 0 ^ x := sorry theorem rpow_nonneg_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : 0 ≤ x ^ y := sorry theorem abs_rpow_le_abs_rpow (x : ℝ) (y : ℝ) : abs (x ^ y) ≤ abs x ^ y := sorry end real namespace complex theorem of_real_cpow {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : ↑(x ^ y) = ↑x ^ ↑y := sorry @[simp] theorem abs_cpow_real (x : ℂ) (y : ℝ) : abs (x ^ ↑y) = abs x ^ y := sorry @[simp] theorem abs_cpow_inv_nat (x : ℂ) (n : ℕ) : abs (x ^ (↑n⁻¹)) = abs x ^ (↑n⁻¹) := sorry end complex namespace real theorem rpow_add {x : ℝ} (hx : 0 < x) (y : ℝ) (z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := sorry theorem rpow_add' {x : ℝ} (hx : 0 ≤ x) {y : ℝ} {z : ℝ} (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := sorry /-- For `0 ≤ x`, the only problematic case in the equality `x ^ y * x ^ z = x ^ (y + z)` is for `x = 0` and `y + z = 0`, where the right hand side is `1` while the left hand side can vanish. The inequality is always true, though, and given in this lemma. -/ theorem le_rpow_add {x : ℝ} (hx : 0 ≤ x) (y : ℝ) (z : ℝ) : x ^ y * x ^ z ≤ x ^ (y + z) := sorry theorem rpow_mul {x : ℝ} (hx : 0 ≤ x) (y : ℝ) (z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := sorry theorem rpow_neg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ (-y) = (x ^ y⁻¹) := sorry theorem rpow_sub {x : ℝ} (hx : 0 < x) (y : ℝ) (z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := sorry theorem rpow_sub' {x : ℝ} (hx : 0 ≤ x) {y : ℝ} {z : ℝ} (h : y - z ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := sorry @[simp] theorem rpow_nat_cast (x : ℝ) (n : ℕ) : x ^ ↑n = x ^ n := sorry @[simp] theorem rpow_int_cast (x : ℝ) (n : ℤ) : x ^ ↑n = x ^ n := sorry theorem rpow_neg_one (x : ℝ) : x ^ (-1) = (x⁻¹) := sorry theorem mul_rpow {x : ℝ} {y : ℝ} {z : ℝ} (h : 0 ≤ x) (h₁ : 0 ≤ y) : (x * y) ^ z = x ^ z * y ^ z := sorry theorem inv_rpow {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x⁻¹ ^ y = (x ^ y⁻¹) := sorry theorem div_rpow {x : ℝ} {y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) (z : ℝ) : (x / y) ^ z = x ^ z / y ^ z := sorry theorem log_rpow {x : ℝ} (hx : 0 < x) (y : ℝ) : log (x ^ y) = y * log x := sorry theorem rpow_lt_rpow {x : ℝ} {y : ℝ} {z : ℝ} (hx : 0 ≤ x) (hxy : x < y) (hz : 0 < z) : x ^ z < y ^ z := sorry theorem rpow_le_rpow {x : ℝ} {y : ℝ} {z : ℝ} (h : 0 ≤ x) (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x ^ z ≤ y ^ z := sorry theorem rpow_lt_rpow_iff {x : ℝ} {y : ℝ} {z : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := { mp := lt_imp_lt_of_le_imp_le fun (h : y ≤ x) => rpow_le_rpow hy h (le_of_lt hz), mpr := fun (h : x < y) => rpow_lt_rpow hx h hz } theorem rpow_le_rpow_iff {x : ℝ} {y : ℝ} {z : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := iff.mpr le_iff_le_iff_lt_iff_lt (rpow_lt_rpow_iff hy hx hz) theorem rpow_lt_rpow_of_exponent_lt {x : ℝ} {y : ℝ} {z : ℝ} (hx : 1 < x) (hyz : y < z) : x ^ y < x ^ z := sorry theorem rpow_le_rpow_of_exponent_le {x : ℝ} {y : ℝ} {z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x ^ y ≤ x ^ z := sorry theorem rpow_lt_rpow_of_exponent_gt {x : ℝ} {y : ℝ} {z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x ^ y < x ^ z := sorry theorem rpow_le_rpow_of_exponent_ge {x : ℝ} {y : ℝ} {z : ℝ} (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x ^ y ≤ x ^ z := sorry theorem rpow_lt_one {x : ℝ} {z : ℝ} (hx1 : 0 ≤ x) (hx2 : x < 1) (hz : 0 < z) : x ^ z < 1 := eq.mpr (id (Eq._oldrec (Eq.refl (x ^ z < 1)) (Eq.symm (one_rpow z)))) (rpow_lt_rpow hx1 hx2 hz) theorem rpow_le_one {x : ℝ} {z : ℝ} (hx1 : 0 ≤ x) (hx2 : x ≤ 1) (hz : 0 ≤ z) : x ^ z ≤ 1 := eq.mpr (id (Eq._oldrec (Eq.refl (x ^ z ≤ 1)) (Eq.symm (one_rpow z)))) (rpow_le_rpow hx1 hx2 hz) theorem rpow_lt_one_of_one_lt_of_neg {x : ℝ} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x ^ z < 1 := sorry theorem rpow_le_one_of_one_le_of_nonpos {x : ℝ} {z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x ^ z ≤ 1 := sorry theorem one_lt_rpow {x : ℝ} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x ^ z := eq.mpr (id (Eq._oldrec (Eq.refl (1 < x ^ z)) (Eq.symm (one_rpow z)))) (rpow_lt_rpow zero_le_one hx hz) theorem one_le_rpow {x : ℝ} {z : ℝ} (hx : 1 ≤ x) (hz : 0 ≤ z) : 1 ≤ x ^ z := eq.mpr (id (Eq._oldrec (Eq.refl (1 ≤ x ^ z)) (Eq.symm (one_rpow z)))) (rpow_le_rpow zero_le_one hx hz) theorem one_lt_rpow_of_pos_of_lt_one_of_neg {x : ℝ} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x ^ z := sorry theorem one_le_rpow_of_pos_of_le_one_of_nonpos {x : ℝ} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x ^ z := sorry theorem rpow_lt_one_iff_of_pos {x : ℝ} {y : ℝ} (hx : 0 < x) : x ^ y < 1 ↔ 1 < x ∧ y < 0 ∨ x < 1 ∧ 0 < y := sorry theorem rpow_lt_one_iff {x : ℝ} {y : ℝ} (hx : 0 ≤ x) : x ^ y < 1 ↔ x = 0 ∧ y ≠ 0 ∨ 1 < x ∧ y < 0 ∨ x < 1 ∧ 0 < y := sorry theorem one_lt_rpow_iff_of_pos {x : ℝ} {y : ℝ} (hx : 0 < x) : 1 < x ^ y ↔ 1 < x ∧ 0 < y ∨ x < 1 ∧ y < 0 := sorry theorem one_lt_rpow_iff {x : ℝ} {y : ℝ} (hx : 0 ≤ x) : 1 < x ^ y ↔ 1 < x ∧ 0 < y ∨ 0 < x ∧ x < 1 ∧ y < 0 := sorry theorem rpow_le_one_iff_of_pos {x : ℝ} {y : ℝ} (hx : 0 < x) : x ^ y ≤ 1 ↔ 1 ≤ x ∧ y ≤ 0 ∨ x ≤ 1 ∧ 0 ≤ y := sorry theorem pow_nat_rpow_nat_inv {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : 0 < n) : (x ^ n) ^ (↑n⁻¹) = x := sorry theorem rpow_nat_inv_pow_nat {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : 0 < n) : (x ^ (↑n⁻¹)) ^ n = x := sorry theorem continuous_rpow_aux1 : continuous fun (p : Subtype fun (p : ℝ × ℝ) => 0 < prod.fst p) => prod.fst (subtype.val p) ^ prod.snd (subtype.val p) := sorry theorem continuous_rpow_aux2 : continuous fun (p : Subtype fun (p : ℝ × ℝ) => prod.fst p < 0) => prod.fst (subtype.val p) ^ prod.snd (subtype.val p) := sorry theorem continuous_at_rpow_of_ne_zero {x : ℝ} (hx : x ≠ 0) (y : ℝ) : continuous_at (fun (p : ℝ × ℝ) => prod.fst p ^ prod.snd p) (x, y) := sorry theorem continuous_rpow_aux3 : continuous fun (p : Subtype fun (p : ℝ × ℝ) => 0 < prod.snd p) => prod.fst (subtype.val p) ^ prod.snd (subtype.val p) := sorry theorem continuous_at_rpow_of_pos {y : ℝ} (hy : 0 < y) (x : ℝ) : continuous_at (fun (p : ℝ × ℝ) => prod.fst p ^ prod.snd p) (x, y) := sorry theorem continuous_at_rpow {x : ℝ} {y : ℝ} (h : x ≠ 0 ∨ 0 < y) : continuous_at (fun (p : ℝ × ℝ) => prod.fst p ^ prod.snd p) (x, y) := or.dcases_on h (fun (h : x ≠ 0) => continuous_at_rpow_of_ne_zero h y) fun (h : 0 < y) => continuous_at_rpow_of_pos h x /-- `real.rpow` is continuous at all points except for the lower half of the y-axis. In other words, the function `λp:ℝ×ℝ, p.1^p.2` is continuous at `(x, y)` if `x ≠ 0` or `y > 0`. Multiple forms of the claim is provided in the current section. -/ theorem continuous_rpow {α : Type u_1} [topological_space α] {f : α → ℝ} {g : α → ℝ} (h : ∀ (a : α), f a ≠ 0 ∨ 0 < g a) (hf : continuous f) (hg : continuous g) : continuous fun (a : α) => f a ^ g a := sorry theorem continuous_rpow_of_ne_zero {α : Type u_1} [topological_space α] {f : α → ℝ} {g : α → ℝ} (h : ∀ (a : α), f a ≠ 0) (hf : continuous f) (hg : continuous g) : continuous fun (a : α) => f a ^ g a := continuous_rpow (fun (a : α) => Or.inl (h a)) hf hg theorem continuous_rpow_of_pos {α : Type u_1} [topological_space α] {f : α → ℝ} {g : α → ℝ} (h : ∀ (a : α), 0 < g a) (hf : continuous f) (hg : continuous g) : continuous fun (a : α) => f a ^ g a := continuous_rpow (fun (a : α) => Or.inr (h a)) hf hg theorem has_deriv_at_rpow_of_pos {x : ℝ} (h : 0 < x) (p : ℝ) : has_deriv_at (fun (x : ℝ) => x ^ p) (p * x ^ (p - 1)) x := sorry theorem has_deriv_at_rpow_of_neg {x : ℝ} (h : x < 0) (p : ℝ) : has_deriv_at (fun (x : ℝ) => x ^ p) (p * x ^ (p - 1)) x := sorry theorem has_deriv_at_rpow {x : ℝ} (h : x ≠ 0) (p : ℝ) : has_deriv_at (fun (x : ℝ) => x ^ p) (p * x ^ (p - 1)) x := or.dcases_on (lt_trichotomy x 0) (fun (H : x < 0) => has_deriv_at_rpow_of_neg H p) fun (h_1 : x = 0 ∨ 0 < x) => or.dcases_on h_1 (fun (H : x = 0) => false.elim (h H)) fun (H : 0 < x) => has_deriv_at_rpow_of_pos H p theorem has_deriv_at_rpow_zero_of_one_le {p : ℝ} (h : 1 ≤ p) : has_deriv_at (fun (x : ℝ) => x ^ p) (p * 0 ^ (p - 1)) 0 := sorry theorem has_deriv_at_rpow_of_one_le (x : ℝ) {p : ℝ} (h : 1 ≤ p) : has_deriv_at (fun (x : ℝ) => x ^ p) (p * x ^ (p - 1)) x := sorry theorem sqrt_eq_rpow : sqrt = fun (x : ℝ) => x ^ (1 / bit0 1) := sorry end real theorem real.measurable_rpow : measurable fun (p : ℝ × ℝ) => prod.fst p ^ prod.snd p := sorry theorem measurable.rpow {α : Type u_1} [measurable_space α] {f : α → ℝ} {g : α → ℝ} (hf : measurable f) (hg : measurable g) : measurable fun (a : α) => f a ^ g a := id (measurable.comp real.measurable_rpow (measurable.prod hf hg)) theorem real.measurable_rpow_const {y : ℝ} : measurable fun (x : ℝ) => x ^ y := id (measurable.comp real.measurable_rpow (measurable.prod measurable_id (id measurable_const))) theorem measurable.rpow_const {α : Type u_1} [measurable_space α] {f : α → ℝ} (hf : measurable f) {y : ℝ} : measurable fun (a : α) => f a ^ y := measurable.rpow hf measurable_const /- Differentiability statements for the power of a function, when the function does not vanish and the exponent is arbitrary-/ theorem has_deriv_within_at.rpow {f : ℝ → ℝ} {x : ℝ} {f' : ℝ} {s : set ℝ} (p : ℝ) (hf : has_deriv_within_at f f' s x) (hx : f x ≠ 0) : has_deriv_within_at (fun (y : ℝ) => f y ^ p) (f' * p * f x ^ (p - 1)) s x := sorry theorem has_deriv_at.rpow {f : ℝ → ℝ} {x : ℝ} {f' : ℝ} (p : ℝ) (hf : has_deriv_at f f' x) (hx : f x ≠ 0) : has_deriv_at (fun (y : ℝ) => f y ^ p) (f' * p * f x ^ (p - 1)) x := sorry theorem differentiable_within_at.rpow {f : ℝ → ℝ} {x : ℝ} {s : set ℝ} (p : ℝ) (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) : differentiable_within_at ℝ (fun (x : ℝ) => f x ^ p) s x := has_deriv_within_at.differentiable_within_at (has_deriv_within_at.rpow p (differentiable_within_at.has_deriv_within_at hf) hx) @[simp] theorem differentiable_at.rpow {f : ℝ → ℝ} {x : ℝ} (p : ℝ) (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : differentiable_at ℝ (fun (x : ℝ) => f x ^ p) x := has_deriv_at.differentiable_at (has_deriv_at.rpow p (differentiable_at.has_deriv_at hf) hx) theorem differentiable_on.rpow {f : ℝ → ℝ} {s : set ℝ} (p : ℝ) (hf : differentiable_on ℝ f s) (hx : ∀ (x : ℝ), x ∈ s → f x ≠ 0) : differentiable_on ℝ (fun (x : ℝ) => f x ^ p) s := fun (x : ℝ) (h : x ∈ s) => differentiable_within_at.rpow p (hf x h) (hx x h) @[simp] theorem differentiable.rpow {f : ℝ → ℝ} (p : ℝ) (hf : differentiable ℝ f) (hx : ∀ (x : ℝ), f x ≠ 0) : differentiable ℝ fun (x : ℝ) => f x ^ p := fun (x : ℝ) => differentiable_at.rpow p (hf x) (hx x) theorem deriv_within_rpow {f : ℝ → ℝ} {x : ℝ} {s : set ℝ} (p : ℝ) (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) (hxs : unique_diff_within_at ℝ s x) : deriv_within (fun (x : ℝ) => f x ^ p) s x = deriv_within f s x * p * f x ^ (p - 1) := has_deriv_within_at.deriv_within (has_deriv_within_at.rpow p (differentiable_within_at.has_deriv_within_at hf) hx) hxs @[simp] theorem deriv_rpow {f : ℝ → ℝ} {x : ℝ} (p : ℝ) (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : deriv (fun (x : ℝ) => f x ^ p) x = deriv f x * p * f x ^ (p - 1) := has_deriv_at.deriv (has_deriv_at.rpow p (differentiable_at.has_deriv_at hf) hx) /- Differentiability statements for the power of a function, when the function may vanish but the exponent is at least one. -/ theorem has_deriv_within_at.rpow_of_one_le {f : ℝ → ℝ} {x : ℝ} {f' : ℝ} {s : set ℝ} {p : ℝ} (hf : has_deriv_within_at f f' s x) (hp : 1 ≤ p) : has_deriv_within_at (fun (y : ℝ) => f y ^ p) (f' * p * f x ^ (p - 1)) s x := sorry theorem has_deriv_at.rpow_of_one_le {f : ℝ → ℝ} {x : ℝ} {f' : ℝ} {p : ℝ} (hf : has_deriv_at f f' x) (hp : 1 ≤ p) : has_deriv_at (fun (y : ℝ) => f y ^ p) (f' * p * f x ^ (p - 1)) x := sorry theorem differentiable_within_at.rpow_of_one_le {f : ℝ → ℝ} {x : ℝ} {s : set ℝ} {p : ℝ} (hf : differentiable_within_at ℝ f s x) (hp : 1 ≤ p) : differentiable_within_at ℝ (fun (x : ℝ) => f x ^ p) s x := has_deriv_within_at.differentiable_within_at (has_deriv_within_at.rpow_of_one_le (differentiable_within_at.has_deriv_within_at hf) hp) @[simp] theorem differentiable_at.rpow_of_one_le {f : ℝ → ℝ} {x : ℝ} {p : ℝ} (hf : differentiable_at ℝ f x) (hp : 1 ≤ p) : differentiable_at ℝ (fun (x : ℝ) => f x ^ p) x := has_deriv_at.differentiable_at (has_deriv_at.rpow_of_one_le (differentiable_at.has_deriv_at hf) hp) theorem differentiable_on.rpow_of_one_le {f : ℝ → ℝ} {s : set ℝ} {p : ℝ} (hf : differentiable_on ℝ f s) (hp : 1 ≤ p) : differentiable_on ℝ (fun (x : ℝ) => f x ^ p) s := fun (x : ℝ) (h : x ∈ s) => differentiable_within_at.rpow_of_one_le (hf x h) hp @[simp] theorem differentiable.rpow_of_one_le {f : ℝ → ℝ} {p : ℝ} (hf : differentiable ℝ f) (hp : 1 ≤ p) : differentiable ℝ fun (x : ℝ) => f x ^ p := fun (x : ℝ) => differentiable_at.rpow_of_one_le (hf x) hp theorem deriv_within_rpow_of_one_le {f : ℝ → ℝ} {x : ℝ} {s : set ℝ} {p : ℝ} (hf : differentiable_within_at ℝ f s x) (hp : 1 ≤ p) (hxs : unique_diff_within_at ℝ s x) : deriv_within (fun (x : ℝ) => f x ^ p) s x = deriv_within f s x * p * f x ^ (p - 1) := has_deriv_within_at.deriv_within (has_deriv_within_at.rpow_of_one_le (differentiable_within_at.has_deriv_within_at hf) hp) hxs @[simp] theorem deriv_rpow_of_one_le {f : ℝ → ℝ} {x : ℝ} {p : ℝ} (hf : differentiable_at ℝ f x) (hp : 1 ≤ p) : deriv (fun (x : ℝ) => f x ^ p) x = deriv f x * p * f x ^ (p - 1) := has_deriv_at.deriv (has_deriv_at.rpow_of_one_le (differentiable_at.has_deriv_at hf) hp) /- Differentiability statements for the square root of a function, when the function does not vanish -/ theorem has_deriv_within_at.sqrt {f : ℝ → ℝ} {x : ℝ} {f' : ℝ} {s : set ℝ} (hf : has_deriv_within_at f f' s x) (hx : f x ≠ 0) : has_deriv_within_at (fun (y : ℝ) => real.sqrt (f y)) (f' / (bit0 1 * real.sqrt (f x))) s x := sorry theorem has_deriv_at.sqrt {f : ℝ → ℝ} {x : ℝ} {f' : ℝ} (hf : has_deriv_at f f' x) (hx : f x ≠ 0) : has_deriv_at (fun (y : ℝ) => real.sqrt (f y)) (f' / (bit0 1 * real.sqrt (f x))) x := sorry theorem differentiable_within_at.sqrt {f : ℝ → ℝ} {x : ℝ} {s : set ℝ} (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) : differentiable_within_at ℝ (fun (x : ℝ) => real.sqrt (f x)) s x := has_deriv_within_at.differentiable_within_at (has_deriv_within_at.sqrt (differentiable_within_at.has_deriv_within_at hf) hx) @[simp] theorem differentiable_at.sqrt {f : ℝ → ℝ} {x : ℝ} (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : differentiable_at ℝ (fun (x : ℝ) => real.sqrt (f x)) x := has_deriv_at.differentiable_at (has_deriv_at.sqrt (differentiable_at.has_deriv_at hf) hx) theorem differentiable_on.sqrt {f : ℝ → ℝ} {s : set ℝ} (hf : differentiable_on ℝ f s) (hx : ∀ (x : ℝ), x ∈ s → f x ≠ 0) : differentiable_on ℝ (fun (x : ℝ) => real.sqrt (f x)) s := fun (x : ℝ) (h : x ∈ s) => differentiable_within_at.sqrt (hf x h) (hx x h) @[simp] theorem differentiable.sqrt {f : ℝ → ℝ} (hf : differentiable ℝ f) (hx : ∀ (x : ℝ), f x ≠ 0) : differentiable ℝ fun (x : ℝ) => real.sqrt (f x) := fun (x : ℝ) => differentiable_at.sqrt (hf x) (hx x) theorem deriv_within_sqrt {f : ℝ → ℝ} {x : ℝ} {s : set ℝ} (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) (hxs : unique_diff_within_at ℝ s x) : deriv_within (fun (x : ℝ) => real.sqrt (f x)) s x = deriv_within f s x / (bit0 1 * real.sqrt (f x)) := has_deriv_within_at.deriv_within (has_deriv_within_at.sqrt (differentiable_within_at.has_deriv_within_at hf) hx) hxs @[simp] theorem deriv_sqrt {f : ℝ → ℝ} {x : ℝ} (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : deriv (fun (x : ℝ) => real.sqrt (f x)) x = deriv f x / (bit0 1 * real.sqrt (f x)) := has_deriv_at.deriv (has_deriv_at.sqrt (differentiable_at.has_deriv_at hf) hx) /-- The function `x ^ y` tends to `+∞` at `+∞` for any positive real `y`. -/ theorem tendsto_rpow_at_top {y : ℝ} (hy : 0 < y) : filter.tendsto (fun (x : ℝ) => x ^ y) filter.at_top filter.at_top := sorry /-- The function `x ^ (-y)` tends to `0` at `+∞` for any positive real `y`. -/ theorem tendsto_rpow_neg_at_top {y : ℝ} (hy : 0 < y) : filter.tendsto (fun (x : ℝ) => x ^ (-y)) filter.at_top (nhds 0) := sorry /-- The function `x ^ (a / (b * x + c))` tends to `1` at `+∞`, for any real numbers `a`, `b`, and `c` such that `b` is nonzero. -/ theorem tendsto_rpow_div_mul_add (a : ℝ) (b : ℝ) (c : ℝ) (hb : 0 ≠ b) : filter.tendsto (fun (x : ℝ) => x ^ (a / (b * x + c))) filter.at_top (nhds 1) := sorry /-- The function `x ^ (1 / x)` tends to `1` at `+∞`. -/ theorem tendsto_rpow_div : filter.tendsto (fun (x : ℝ) => x ^ (1 / x)) filter.at_top (nhds 1) := sorry /-- The function `x ^ (-1 / x)` tends to `1` at `+∞`. -/ theorem tendsto_rpow_neg_div : filter.tendsto (fun (x : ℝ) => x ^ (-1 / x)) filter.at_top (nhds 1) := sorry namespace nnreal /-- The nonnegative real power function `x^y`, defined for `x : ℝ≥0` and `y : ℝ ` as the restriction of the real power function. For `x > 0`, it is equal to `exp (y log x)`. For `x = 0`, one sets `0 ^ 0 = 1` and `0 ^ y = 0` for `y ≠ 0`. -/ def rpow (x : nnreal) (y : ℝ) : nnreal := { val := ↑x ^ y, property := sorry } protected instance real.has_pow : has_pow nnreal ℝ := has_pow.mk rpow @[simp] theorem rpow_eq_pow (x : nnreal) (y : ℝ) : rpow x y = x ^ y := rfl @[simp] theorem coe_rpow (x : nnreal) (y : ℝ) : ↑(x ^ y) = ↑x ^ y := rfl @[simp] theorem rpow_zero (x : nnreal) : x ^ 0 = 1 := nnreal.eq (real.rpow_zero ↑x) @[simp] theorem rpow_eq_zero_iff {x : nnreal} {y : ℝ} : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := sorry @[simp] theorem zero_rpow {x : ℝ} (h : x ≠ 0) : 0 ^ x = 0 := nnreal.eq (real.zero_rpow h) @[simp] theorem rpow_one (x : nnreal) : x ^ 1 = x := nnreal.eq (real.rpow_one ↑x) @[simp] theorem one_rpow (x : ℝ) : 1 ^ x = 1 := nnreal.eq (real.one_rpow x) theorem rpow_add {x : nnreal} (hx : x ≠ 0) (y : ℝ) (z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := nnreal.eq (real.rpow_add (iff.mpr pos_iff_ne_zero hx) y z) theorem rpow_add' (x : nnreal) {y : ℝ} {z : ℝ} (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := nnreal.eq (real.rpow_add' (subtype.property x) h) theorem rpow_mul (x : nnreal) (y : ℝ) (z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := nnreal.eq (real.rpow_mul (subtype.property x) y z) theorem rpow_neg (x : nnreal) (y : ℝ) : x ^ (-y) = (x ^ y⁻¹) := nnreal.eq (real.rpow_neg (subtype.property x) y) theorem rpow_neg_one (x : nnreal) : x ^ (-1) = (x⁻¹) := sorry theorem rpow_sub {x : nnreal} (hx : x ≠ 0) (y : ℝ) (z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := nnreal.eq (real.rpow_sub (iff.mpr pos_iff_ne_zero hx) y z) theorem rpow_sub' (x : nnreal) {y : ℝ} {z : ℝ} (h : y - z ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := nnreal.eq (real.rpow_sub' (subtype.property x) h) theorem inv_rpow (x : nnreal) (y : ℝ) : x⁻¹ ^ y = (x ^ y⁻¹) := nnreal.eq (real.inv_rpow (subtype.property x) y) theorem div_rpow (x : nnreal) (y : nnreal) (z : ℝ) : (x / y) ^ z = x ^ z / y ^ z := nnreal.eq (real.div_rpow (subtype.property x) (subtype.property y) z) @[simp] theorem rpow_nat_cast (x : nnreal) (n : ℕ) : x ^ ↑n = x ^ n := sorry theorem mul_rpow {x : nnreal} {y : nnreal} {z : ℝ} : (x * y) ^ z = x ^ z * y ^ z := nnreal.eq (real.mul_rpow (subtype.property x) (subtype.property y)) theorem rpow_le_rpow {x : nnreal} {y : nnreal} {z : ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x ^ z ≤ y ^ z := real.rpow_le_rpow (subtype.property x) h₁ h₂ theorem rpow_lt_rpow {x : nnreal} {y : nnreal} {z : ℝ} (h₁ : x < y) (h₂ : 0 < z) : x ^ z < y ^ z := real.rpow_lt_rpow (subtype.property x) h₁ h₂ theorem rpow_lt_rpow_iff {x : nnreal} {y : nnreal} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := real.rpow_lt_rpow_iff (subtype.property x) (subtype.property y) hz theorem rpow_le_rpow_iff {x : nnreal} {y : nnreal} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := real.rpow_le_rpow_iff (subtype.property x) (subtype.property y) hz theorem rpow_lt_rpow_of_exponent_lt {x : nnreal} {y : ℝ} {z : ℝ} (hx : 1 < x) (hyz : y < z) : x ^ y < x ^ z := real.rpow_lt_rpow_of_exponent_lt hx hyz theorem rpow_le_rpow_of_exponent_le {x : nnreal} {y : ℝ} {z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x ^ y ≤ x ^ z := real.rpow_le_rpow_of_exponent_le hx hyz theorem rpow_lt_rpow_of_exponent_gt {x : nnreal} {y : ℝ} {z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x ^ y < x ^ z := real.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz theorem rpow_le_rpow_of_exponent_ge {x : nnreal} {y : ℝ} {z : ℝ} (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x ^ y ≤ x ^ z := real.rpow_le_rpow_of_exponent_ge hx0 hx1 hyz theorem rpow_lt_one {x : nnreal} {z : ℝ} (hx : 0 ≤ x) (hx1 : x < 1) (hz : 0 < z) : x ^ z < 1 := real.rpow_lt_one hx hx1 hz theorem rpow_le_one {x : nnreal} {z : ℝ} (hx2 : x ≤ 1) (hz : 0 ≤ z) : x ^ z ≤ 1 := real.rpow_le_one (subtype.property x) hx2 hz theorem rpow_lt_one_of_one_lt_of_neg {x : nnreal} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x ^ z < 1 := real.rpow_lt_one_of_one_lt_of_neg hx hz theorem rpow_le_one_of_one_le_of_nonpos {x : nnreal} {z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x ^ z ≤ 1 := real.rpow_le_one_of_one_le_of_nonpos hx hz theorem one_lt_rpow {x : nnreal} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x ^ z := real.one_lt_rpow hx hz theorem one_le_rpow {x : nnreal} {z : ℝ} (h : 1 ≤ x) (h₁ : 0 ≤ z) : 1 ≤ x ^ z := real.one_le_rpow h h₁ theorem one_lt_rpow_of_pos_of_lt_one_of_neg {x : nnreal} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x ^ z := real.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz theorem one_le_rpow_of_pos_of_le_one_of_nonpos {x : nnreal} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x ^ z := real.one_le_rpow_of_pos_of_le_one_of_nonpos hx1 hx2 hz theorem pow_nat_rpow_nat_inv (x : nnreal) {n : ℕ} (hn : 0 < n) : (x ^ n) ^ (↑n⁻¹) = x := sorry theorem rpow_nat_inv_pow_nat (x : nnreal) {n : ℕ} (hn : 0 < n) : (x ^ (↑n⁻¹)) ^ n = x := sorry theorem continuous_at_rpow {x : nnreal} {y : ℝ} (h : x ≠ 0 ∨ 0 < y) : continuous_at (fun (p : nnreal × ℝ) => prod.fst p ^ prod.snd p) (x, y) := sorry theorem of_real_rpow_of_nonneg {x : ℝ} {y : ℝ} (hx : 0 ≤ x) : nnreal.of_real (x ^ y) = nnreal.of_real x ^ y := sorry end nnreal theorem nnreal.measurable_rpow : measurable fun (p : nnreal × ℝ) => prod.fst p ^ prod.snd p := sorry theorem measurable.nnreal_rpow {α : Type u_1} [measurable_space α] {f : α → nnreal} (hf : measurable f) {g : α → ℝ} (hg : measurable g) : measurable fun (a : α) => f a ^ g a := id (measurable.comp nnreal.measurable_rpow (measurable.prod hf hg)) theorem nnreal.measurable_rpow_const {y : ℝ} : measurable fun (a : nnreal) => a ^ y := sorry theorem measurable.nnreal_rpow_const {α : Type u_1} [measurable_space α] {f : α → nnreal} (hf : measurable f) {y : ℝ} : measurable fun (a : α) => f a ^ y := measurable.nnreal_rpow hf measurable_const theorem filter.tendsto.nnrpow {α : Type u_1} {f : filter α} {u : α → nnreal} {v : α → ℝ} {x : nnreal} {y : ℝ} (hx : filter.tendsto u f (nhds x)) (hy : filter.tendsto v f (nhds y)) (h : x ≠ 0 ∨ 0 < y) : filter.tendsto (fun (a : α) => u a ^ v a) f (nhds (x ^ y)) := filter.tendsto.comp (nnreal.continuous_at_rpow h) (filter.tendsto.prod_mk_nhds hx hy) namespace nnreal theorem continuous_at_rpow_const {x : nnreal} {y : ℝ} (h : x ≠ 0 ∨ 0 ≤ y) : continuous_at (fun (z : nnreal) => z ^ y) x := sorry theorem continuous_rpow_const {y : ℝ} (h : 0 ≤ y) : continuous fun (x : nnreal) => x ^ y := iff.mpr continuous_iff_continuous_at fun (x : nnreal) => continuous_at_rpow_const (Or.inr h) end nnreal namespace ennreal /-- The real power function `x^y` on extended nonnegative reals, defined for `x : ennreal` and `y : ℝ` as the restriction of the real power function if `0 < x < ⊤`, and with the natural values for `0` and `⊤` (i.e., `0 ^ x = 0` for `x > 0`, `1` for `x = 0` and `⊤` for `x < 0`, and `⊤ ^ x = 1 / 0 ^ x`). -/ def rpow : ennreal → ℝ → ennreal := sorry protected instance real.has_pow : has_pow ennreal ℝ := has_pow.mk rpow @[simp] theorem rpow_eq_pow (x : ennreal) (y : ℝ) : rpow x y = x ^ y := rfl @[simp] theorem rpow_zero {x : ennreal} : x ^ 0 = 1 := sorry theorem top_rpow_def (y : ℝ) : ⊤ ^ y = ite (0 < y) ⊤ (ite (y = 0) 1 0) := rfl @[simp] theorem top_rpow_of_pos {y : ℝ} (h : 0 < y) : ⊤ ^ y = ⊤ := sorry @[simp] theorem top_rpow_of_neg {y : ℝ} (h : y < 0) : ⊤ ^ y = 0 := sorry @[simp] theorem zero_rpow_of_pos {y : ℝ} (h : 0 < y) : 0 ^ y = 0 := sorry @[simp] theorem zero_rpow_of_neg {y : ℝ} (h : y < 0) : 0 ^ y = ⊤ := sorry theorem zero_rpow_def (y : ℝ) : 0 ^ y = ite (0 < y) 0 (ite (y = 0) 1 ⊤) := sorry theorem coe_rpow_of_ne_zero {x : nnreal} (h : x ≠ 0) (y : ℝ) : ↑x ^ y = ↑(x ^ y) := sorry theorem coe_rpow_of_nonneg (x : nnreal) {y : ℝ} (h : 0 ≤ y) : ↑x ^ y = ↑(x ^ y) := sorry theorem coe_rpow_def (x : nnreal) (y : ℝ) : ↑x ^ y = ite (x = 0 ∧ y < 0) ⊤ ↑(x ^ y) := rfl @[simp] theorem rpow_one (x : ennreal) : x ^ 1 = x := sorry @[simp] theorem one_rpow (x : ℝ) : 1 ^ x = 1 := sorry @[simp] theorem rpow_eq_zero_iff {x : ennreal} {y : ℝ} : x ^ y = 0 ↔ x = 0 ∧ 0 < y ∨ x = ⊤ ∧ y < 0 := sorry @[simp] theorem rpow_eq_top_iff {x : ennreal} {y : ℝ} : x ^ y = ⊤ ↔ x = 0 ∧ y < 0 ∨ x = ⊤ ∧ 0 < y := sorry theorem rpow_eq_top_iff_of_pos {x : ennreal} {y : ℝ} (hy : 0 < y) : x ^ y = ⊤ ↔ x = ⊤ := sorry theorem rpow_eq_top_of_nonneg (x : ennreal) {y : ℝ} (hy0 : 0 ≤ y) : x ^ y = ⊤ → x = ⊤ := sorry theorem rpow_ne_top_of_nonneg {x : ennreal} {y : ℝ} (hy0 : 0 ≤ y) (h : x ≠ ⊤) : x ^ y ≠ ⊤ := mt (rpow_eq_top_of_nonneg x hy0) h theorem rpow_lt_top_of_nonneg {x : ennreal} {y : ℝ} (hy0 : 0 ≤ y) (h : x ≠ ⊤) : x ^ y < ⊤ := iff.mpr ennreal.lt_top_iff_ne_top (rpow_ne_top_of_nonneg hy0 h) theorem rpow_add {x : ennreal} (y : ℝ) (z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y + z) = x ^ y * x ^ z := sorry theorem rpow_neg (x : ennreal) (y : ℝ) : x ^ (-y) = (x ^ y⁻¹) := sorry theorem rpow_neg_one (x : ennreal) : x ^ (-1) = (x⁻¹) := sorry theorem rpow_mul (x : ennreal) (y : ℝ) (z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := sorry @[simp] theorem rpow_nat_cast (x : ennreal) (n : ℕ) : x ^ ↑n = x ^ n := sorry theorem coe_mul_rpow (x : nnreal) (y : nnreal) (z : ℝ) : (↑x * ↑y) ^ z = ↑x ^ z * ↑y ^ z := sorry theorem mul_rpow_of_ne_top {x : ennreal} {y : ennreal} (hx : x ≠ ⊤) (hy : y ≠ ⊤) (z : ℝ) : (x * y) ^ z = x ^ z * y ^ z := sorry theorem mul_rpow_of_ne_zero {x : ennreal} {y : ennreal} (hx : x ≠ 0) (hy : y ≠ 0) (z : ℝ) : (x * y) ^ z = x ^ z * y ^ z := sorry theorem mul_rpow_of_nonneg (x : ennreal) (y : ennreal) {z : ℝ} (hz : 0 ≤ z) : (x * y) ^ z = x ^ z * y ^ z := sorry theorem inv_rpow_of_pos {x : ennreal} {y : ℝ} (hy : 0 < y) : x⁻¹ ^ y = (x ^ y⁻¹) := sorry theorem div_rpow_of_nonneg (x : ennreal) (y : ennreal) {z : ℝ} (hz : 0 ≤ z) : (x / y) ^ z = x ^ z / y ^ z := sorry theorem rpow_le_rpow {x : ennreal} {y : ennreal} {z : ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x ^ z ≤ y ^ z := sorry theorem rpow_lt_rpow {x : ennreal} {y : ennreal} {z : ℝ} (h₁ : x < y) (h₂ : 0 < z) : x ^ z < y ^ z := sorry theorem rpow_le_rpow_iff {x : ennreal} {y : ennreal} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := sorry theorem rpow_lt_rpow_iff {x : ennreal} {y : ennreal} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := sorry theorem le_rpow_one_div_iff {x : ennreal} {y : ennreal} {z : ℝ} (hz : 0 < z) : x ≤ y ^ (1 / z) ↔ x ^ z ≤ y := sorry theorem lt_rpow_one_div_iff {x : ennreal} {y : ennreal} {z : ℝ} (hz : 0 < z) : x < y ^ (1 / z) ↔ x ^ z < y := sorry theorem rpow_lt_rpow_of_exponent_lt {x : ennreal} {y : ℝ} {z : ℝ} (hx : 1 < x) (hx' : x ≠ ⊤) (hyz : y < z) : x ^ y < x ^ z := sorry theorem rpow_le_rpow_of_exponent_le {x : ennreal} {y : ℝ} {z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x ^ y ≤ x ^ z := sorry theorem rpow_lt_rpow_of_exponent_gt {x : ennreal} {y : ℝ} {z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x ^ y < x ^ z := sorry theorem rpow_le_rpow_of_exponent_ge {x : ennreal} {y : ℝ} {z : ℝ} (hx1 : x ≤ 1) (hyz : z ≤ y) : x ^ y ≤ x ^ z := sorry theorem rpow_le_self_of_le_one {x : ennreal} {z : ℝ} (hx : x ≤ 1) (h_one_le : 1 ≤ z) : x ^ z ≤ x := eq.mpr (id (congr_arg (LessEq (x ^ z)) (Eq._oldrec (Eq.refl x) (Eq.symm (rpow_one x))))) (rpow_le_rpow_of_exponent_ge hx h_one_le) theorem le_rpow_self_of_one_le {x : ennreal} {z : ℝ} (hx : 1 ≤ x) (h_one_le : 1 ≤ z) : x ≤ x ^ z := eq.mpr (id (congr_fun (congr_arg LessEq (Eq._oldrec (Eq.refl x) (Eq.symm (rpow_one x)))) (x ^ z))) (rpow_le_rpow_of_exponent_le hx h_one_le) theorem rpow_pos_of_nonneg {p : ℝ} {x : ennreal} (hx_pos : 0 < x) (hp_nonneg : 0 ≤ p) : 0 < x ^ p := sorry theorem rpow_pos {p : ℝ} {x : ennreal} (hx_pos : 0 < x) (hx_ne_top : x ≠ ⊤) : 0 < x ^ p := sorry theorem rpow_lt_one {x : ennreal} {z : ℝ} (hx : x < 1) (hz : 0 < z) : x ^ z < 1 := sorry theorem rpow_le_one {x : ennreal} {z : ℝ} (hx : x ≤ 1) (hz : 0 ≤ z) : x ^ z ≤ 1 := sorry theorem rpow_lt_one_of_one_lt_of_neg {x : ennreal} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x ^ z < 1 := sorry theorem rpow_le_one_of_one_le_of_neg {x : ennreal} {z : ℝ} (hx : 1 ≤ x) (hz : z < 0) : x ^ z ≤ 1 := sorry theorem one_lt_rpow {x : ennreal} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x ^ z := sorry theorem one_le_rpow {x : ennreal} {z : ℝ} (hx : 1 ≤ x) (hz : 0 < z) : 1 ≤ x ^ z := sorry theorem one_lt_rpow_of_pos_of_lt_one_of_neg {x : ennreal} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x ^ z := sorry theorem one_le_rpow_of_pos_of_le_one_of_neg {x : ennreal} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z < 0) : 1 ≤ x ^ z := sorry theorem to_nnreal_rpow (x : ennreal) (z : ℝ) : ennreal.to_nnreal x ^ z = ennreal.to_nnreal (x ^ z) := sorry theorem to_real_rpow (x : ennreal) (z : ℝ) : ennreal.to_real x ^ z = ennreal.to_real (x ^ z) := sorry theorem rpow_left_injective {x : ℝ} (hx : x ≠ 0) : function.injective fun (y : ennreal) => y ^ x := sorry theorem rpow_left_surjective {x : ℝ} (hx : x ≠ 0) : function.surjective fun (y : ennreal) => y ^ x := sorry theorem rpow_left_bijective {x : ℝ} (hx : x ≠ 0) : function.bijective fun (y : ennreal) => y ^ x := { left := rpow_left_injective hx, right := rpow_left_surjective hx } theorem rpow_left_monotone_of_nonneg {x : ℝ} (hx : 0 ≤ x) : monotone fun (y : ennreal) => y ^ x := fun (y z : ennreal) (hyz : y ≤ z) => rpow_le_rpow hyz hx theorem rpow_left_strict_mono_of_pos {x : ℝ} (hx : 0 < x) : strict_mono fun (y : ennreal) => y ^ x := fun (y z : ennreal) (hyz : y < z) => rpow_lt_rpow hyz hx end ennreal theorem ennreal.measurable_rpow : measurable fun (p : ennreal × ℝ) => prod.fst p ^ prod.snd p := sorry theorem measurable.ennreal_rpow {α : Type u_1} [measurable_space α] {f : α → ennreal} (hf : measurable f) {g : α → ℝ} (hg : measurable g) : measurable fun (a : α) => f a ^ g a := id (measurable.comp ennreal.measurable_rpow (measurable.prod hf hg)) theorem ennreal.measurable_rpow_const {y : ℝ} : measurable fun (a : ennreal) => a ^ y := id (measurable.comp ennreal.measurable_rpow (measurable.prod measurable_id (id measurable_const))) theorem measurable.ennreal_rpow_const {α : Type u_1} [measurable_space α] {f : α → ennreal} (hf : measurable f) {y : ℝ} : measurable fun (a : α) => f a ^ y := measurable.ennreal_rpow hf measurable_const theorem ae_measurable.ennreal_rpow_const {α : Type u_1} [measurable_space α] {f : α → ennreal} {μ : measure_theory.measure α} (hf : ae_measurable f) {y : ℝ} : ae_measurable fun (a : α) => f a ^ y := measurable.comp_ae_measurable ennreal.measurable_rpow_const hf
aa4f84aa5118398ed9e281dc8067a84d821f1709
367134ba5a65885e863bdc4507601606690974c1
/src/analysis/convex/basic.lean
c2ea543643aede761e232e4e69b2876121bf600b
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
58,308
lean
/- Copyright (c) 2019 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, Yury Kudriashov -/ import data.set.intervals.ord_connected import data.set.intervals.image_preimage import data.complex.module import linear_algebra.affine_space.affine_map import algebra.module.ordered /-! # Convex sets and functions on real vector spaces In a real vector space, we define the following objects and properties. * `segment x y` is the closed segment joining `x` and `y`. * A set `s` is `convex` if for any two points `x y ∈ s` it includes `segment x y`; * A function `f : E → β` is `convex_on` a set `s` if `s` is itself a convex set, and for any two points `x y ∈ s` the segment joining `(x, f x)` to `(y, f y)` is (non-strictly) above the graph of `f`; equivalently, `convex_on f s` means that the epigraph `{p : E × β | p.1 ∈ s ∧ f p.1 ≤ p.2}` is a convex set; * Center mass of a finite set of points with prescribed weights. * Convex hull of a set `s` is the minimal convex set that includes `s`. * Standard simplex `std_simplex ι [fintype ι]` is the intersection of the positive quadrant with the hyperplane `s.sum = 1` in the space `ι → ℝ`. We also provide various equivalent versions of the definitions above, prove that some specific sets are convex, and prove Jensen's inequality. Note: To define convexity for functions `f : E → β`, we need `β` to be an ordered vector space, defined using the instance `ordered_semimodule ℝ β`. ## Notations We use the following local notations: * `I = Icc (0:ℝ) 1`; * `[x, y] = segment x y`. They are defined using `local notation`, so they are not available outside of this file. -/ universes u' u v v' w x variables {E : Type u} {F : Type v} {ι : Type w} {ι' : Type x} {α : Type v'} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] [linear_ordered_field α] {s : set E} open set linear_map open_locale classical big_operators local notation `I` := (Icc 0 1 : set ℝ) section sets /-! ### Segment -/ /-- Segments in a vector space. -/ def segment (x y : E) : set E := {z : E | ∃ (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), a • x + b • y = z} local notation `[`x `, ` y `]` := segment x y lemma segment_symm (x y : E) : [x, y] = [y, x] := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩, λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩⟩ lemma left_mem_segment (x y : E) : x ∈ [x, y] := ⟨1, 0, zero_le_one, le_refl 0, add_zero 1, by rw [zero_smul, one_smul, add_zero]⟩ lemma right_mem_segment (x y : E) : y ∈ [x, y] := segment_symm y x ▸ left_mem_segment y x lemma segment_same (x : E) : [x, x] = {x} := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, by simpa only [(add_smul _ _ _).symm, mem_singleton_iff, hab, one_smul, eq_comm] using hz, λ h, mem_singleton_iff.1 h ▸ left_mem_segment z z⟩ lemma segment_eq_image (x y : E) : segment x y = (λ (θ : ℝ), (1 - θ) • x + θ • y) '' I := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, ⟨b, ⟨hb, hab ▸ le_add_of_nonneg_left ha⟩, hab ▸ hz ▸ by simp only [add_sub_cancel]⟩, λ ⟨θ, ⟨hθ₀, hθ₁⟩, hz⟩, ⟨1-θ, θ, sub_nonneg.2 hθ₁, hθ₀, sub_add_cancel _ _, hz⟩⟩ lemma segment_eq_image' (x y : E) : segment x y = (λ (θ : ℝ), x + θ • (y - x)) '' I := by { convert segment_eq_image x y, ext θ, simp only [smul_sub, sub_smul, one_smul], abel } lemma segment_eq_image₂ (x y : E) : segment x y = (λ p:ℝ×ℝ, p.1 • x + p.2 • y) '' {p | 0 ≤ p.1 ∧ 0 ≤ p.2 ∧ p.1 + p.2 = 1} := by simp only [segment, image, prod.exists, mem_set_of_eq, exists_prop, and_assoc] lemma segment_eq_Icc {a b : ℝ} (h : a ≤ b) : [a, b] = Icc a b := begin rw [segment_eq_image'], show (((+) a) ∘ (λ t, t * (b - a))) '' Icc 0 1 = Icc a b, rw [image_comp, image_mul_right_Icc (@zero_le_one ℝ _) (sub_nonneg.2 h), image_const_add_Icc], simp end lemma segment_eq_Icc' (a b : ℝ) : [a, b] = Icc (min a b) (max a b) := by cases le_total a b; [skip, rw segment_symm]; simp [segment_eq_Icc, *] lemma segment_eq_interval (a b : ℝ) : segment a b = interval a b := segment_eq_Icc' _ _ lemma mem_segment_translate (a : E) {x b c} : a + x ∈ [a + b, a + c] ↔ x ∈ [b, c] := begin rw [segment_eq_image', segment_eq_image'], refine exists_congr (λ θ, and_congr iff.rfl _), simp only [add_sub_add_left_eq_sub, add_assoc, add_right_inj] end lemma segment_translate_preimage (a b c : E) : (λ x, a + x) ⁻¹' [a + b, a + c] = [b, c] := set.ext $ λ x, mem_segment_translate a lemma segment_translate_image (a b c: E) : (λx, a + x) '' [b, c] = [a + b, a + c] := segment_translate_preimage a b c ▸ image_preimage_eq _ $ add_left_surjective a lemma segment_image (f : E →ₗ[ℝ] F) (a b : E) : f '' [a, b] = [f a, f b] := set.ext (λ x, by simp [segment_eq_image]) /-! ### Convexity of sets -/ /-- Convexity of sets. -/ def convex (s : set E) := ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • x + b • y ∈ s lemma convex_iff_forall_pos : convex s ↔ ∀ ⦃x y⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := begin refine ⟨λ h x y hx hy a b ha hb hab, h hx hy (le_of_lt ha) (le_of_lt hb) hab, _⟩, intros h x y hx hy a b ha hb hab, cases eq_or_lt_of_le ha with ha ha, { subst a, rw [zero_add] at hab, simp [hab, hy] }, cases eq_or_lt_of_le hb with hb hb, { subst b, rw [add_zero] at hab, simp [hab, hx] }, exact h hx hy ha hb hab end lemma convex_iff_segment_subset : convex s ↔ ∀ ⦃x y⦄, x ∈ s → y ∈ s → [x, y] ⊆ s := by simp only [convex, segment_eq_image₂, subset_def, ball_image_iff, prod.forall, mem_set_of_eq, and_imp] lemma convex.segment_subset (h : convex s) {x y:E} (hx : x ∈ s) (hy : y ∈ s) : [x, y] ⊆ s := convex_iff_segment_subset.1 h hx hy /-- Alternative definition of set convexity, in terms of pointwise set operations. -/ lemma convex_iff_pointwise_add_subset: convex s ↔ ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • s + b • s ⊆ s := iff.intro begin rintros hA a b ha hb hab w ⟨au, bv, ⟨u, hu, rfl⟩, ⟨v, hv, rfl⟩, rfl⟩, exact hA hu hv ha hb hab end (λ h x y hx hy a b ha hb hab, (h ha hb hab) (set.add_mem_add ⟨_, hx, rfl⟩ ⟨_, hy, rfl⟩)) /-- Alternative definition of set convexity, using division. -/ lemma convex_iff_div: convex s ↔ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → (a/(a+b)) • x + (b/(a+b)) • y ∈ s := ⟨begin assume h x y hx hy a b ha hb hab, apply h hx hy, have ha', from mul_le_mul_of_nonneg_left ha (le_of_lt (inv_pos.2 hab)), rwa [mul_zero, ←div_eq_inv_mul] at ha', have hb', from mul_le_mul_of_nonneg_left hb (le_of_lt (inv_pos.2 hab)), rwa [mul_zero, ←div_eq_inv_mul] at hb', rw [←add_div], exact div_self (ne_of_lt hab).symm end, begin assume h x y hx hy a b ha hb hab, have h', from h hx hy ha hb, rw [hab, div_one, div_one] at h', exact h' zero_lt_one end⟩ /-! ### Examples of convex sets -/ lemma convex_empty : convex (∅ : set E) := by finish lemma convex_singleton (c : E) : convex ({c} : set E) := begin intros x y hx hy a b ha hb hab, rw [set.eq_of_mem_singleton hx, set.eq_of_mem_singleton hy, ←add_smul, hab, one_smul], exact mem_singleton c end lemma convex_univ : convex (set.univ : set E) := λ _ _ _ _ _ _ _ _ _, trivial lemma convex.inter {t : set E} (hs: convex s) (ht: convex t) : convex (s ∩ t) := λ x y (hx : x ∈ s ∩ t) (hy : y ∈ s ∩ t) a b (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), ⟨hs hx.left hy.left ha hb hab, ht hx.right hy.right ha hb hab⟩ lemma convex_sInter {S : set (set E)} (h : ∀ s ∈ S, convex s) : convex (⋂₀ S) := assume x y hx hy a b ha hb hab s hs, h s hs (hx s hs) (hy s hs) ha hb hab lemma convex_Inter {ι : Sort*} {s: ι → set E} (h: ∀ i : ι, convex (s i)) : convex (⋂ i, s i) := (sInter_range s) ▸ convex_sInter $ forall_range_iff.2 h lemma convex.prod {s : set E} {t : set F} (hs : convex s) (ht : convex t) : convex (s.prod t) := begin intros x y hx hy a b ha hb hab, apply mem_prod.2, exact ⟨hs (mem_prod.1 hx).1 (mem_prod.1 hy).1 ha hb hab, ht (mem_prod.1 hx).2 (mem_prod.1 hy).2 ha hb hab⟩ end lemma convex.combo_to_vadd {a b : ℝ} {x y : E} (h : a + b = 1) : a • x + b • y = b • (y - x) + x := calc a • x + b • y = (b • y - b • x) + (a • x + b • x) : by abel ... = b • (y - x) + (a + b) • x : by rw [smul_sub, add_smul] ... = b • (y - x) + (1 : ℝ) • x : by rw [h] ... = b • (y - x) + x : by rw [one_smul] /-- Applying an affine map to an affine combination of two points yields an affine combination of the images. -/ lemma convex.combo_affine_apply {a b : ℝ} {x y : E} {f : E →ᵃ[ℝ] F} (h : a + b = 1) : f (a • x + b • y) = a • f x + b • f y := begin simp only [convex.combo_to_vadd h, ← vsub_eq_sub], exact f.apply_line_map _ _ _, end /-- The preimage of a convex set under an affine map is convex. -/ lemma convex.affine_preimage (f : E →ᵃ[ℝ] F) {s : set F} (hs : convex s) : convex (f ⁻¹' s) := begin intros x y xs ys a b ha hb hab, rw [mem_preimage, convex.combo_affine_apply hab], exact hs xs ys ha hb hab, end /-- The image of a convex set under an affine map is convex. -/ lemma convex.affine_image (f : E →ᵃ[ℝ] F) {s : set E} (hs : convex s) : convex (f '' s) := begin rintros x y ⟨x', ⟨hx', hx'f⟩⟩ ⟨y', ⟨hy', hy'f⟩⟩ a b ha hb hab, refine ⟨a • x' + b • y', ⟨hs hx' hy' ha hb hab, _⟩⟩, rw [convex.combo_affine_apply hab, hx'f, hy'f] end lemma convex.linear_image (hs : convex s) (f : E →ₗ[ℝ] F) : convex (image f s) := hs.affine_image f.to_affine_map lemma convex.is_linear_image (hs : convex s) {f : E → F} (hf : is_linear_map ℝ f) : convex (f '' s) := hs.linear_image $ hf.mk' f lemma convex.linear_preimage {s : set F} (hs : convex s) (f : E →ₗ[ℝ] F) : convex (preimage f s) := hs.affine_preimage f.to_affine_map lemma convex.is_linear_preimage {s : set F} (hs : convex s) {f : E → F} (hf : is_linear_map ℝ f) : convex (preimage f s) := hs.linear_preimage $ hf.mk' f lemma convex.neg (hs : convex s) : convex ((λ z, -z) '' s) := hs.is_linear_image is_linear_map.is_linear_map_neg lemma convex.neg_preimage (hs : convex s) : convex ((λ z, -z) ⁻¹' s) := hs.is_linear_preimage is_linear_map.is_linear_map_neg lemma convex.smul (c : ℝ) (hs : convex s) : convex (c • s) := hs.linear_image (linear_map.lsmul _ _ c) lemma convex.smul_preimage (c : ℝ) (hs : convex s) : convex ((λ z, c • z) ⁻¹' s) := hs.linear_preimage (linear_map.lsmul _ _ c) lemma convex.add {t : set E} (hs : convex s) (ht : convex t) : convex (s + t) := by { rw ← add_image_prod, exact (hs.prod ht).is_linear_image is_linear_map.is_linear_map_add } lemma convex.sub {t : set E} (hs : convex s) (ht : convex t) : convex ((λx : E × E, x.1 - x.2) '' (s.prod t)) := (hs.prod ht).is_linear_image is_linear_map.is_linear_map_sub lemma convex.translate (hs : convex s) (z : E) : convex ((λx, z + x) '' s) := hs.affine_image $ affine_map.const ℝ E z +ᵥ affine_map.id ℝ E /-- The translation of a convex set is also convex. -/ lemma convex.translate_preimage_right (hs : convex s) (a : E) : convex ((λ z, a + z) ⁻¹' s) := hs.affine_preimage $ affine_map.const ℝ E a +ᵥ affine_map.id ℝ E /-- The translation of a convex set is also convex. -/ lemma convex.translate_preimage_left (hs : convex s) (a : E) : convex ((λ z, z + a) ⁻¹' s) := by simpa only [add_comm] using hs.translate_preimage_right a lemma convex.affinity (hs : convex s) (z : E) (c : ℝ) : convex ((λx, z + c • x) '' s) := hs.affine_image $ affine_map.const ℝ E z +ᵥ c • affine_map.id ℝ E lemma real.convex_iff_ord_connected {s : set ℝ} : convex s ↔ ord_connected s := begin simp only [convex_iff_segment_subset, segment_eq_interval, ord_connected_iff_interval_subset], exact forall_congr (λ x, forall_swap) end alias real.convex_iff_ord_connected ↔ convex.ord_connected set.ord_connected.convex lemma convex_Iio (r : ℝ) : convex (Iio r) := ord_connected_Iio.convex lemma convex_Ioi (r : ℝ) : convex (Ioi r) := ord_connected_Ioi.convex lemma convex_Iic (r : ℝ) : convex (Iic r) := ord_connected_Iic.convex lemma convex_Ici (r : ℝ) : convex (Ici r) := ord_connected_Ici.convex lemma convex_Ioo (r s : ℝ) : convex (Ioo r s) := ord_connected_Ioo.convex lemma convex_Ico (r s : ℝ) : convex (Ico r s) := ord_connected_Ico.convex lemma convex_Ioc (r : ℝ) (s : ℝ) : convex (Ioc r s) := ord_connected_Ioc.convex lemma convex_Icc (r : ℝ) (s : ℝ) : convex (Icc r s) := ord_connected_Icc.convex lemma convex_interval (r : ℝ) (s : ℝ) : convex (interval r s) := ord_connected_interval.convex lemma convex_segment (a b : E) : convex [a, b] := begin have : (λ (t : ℝ), a + t • (b - a)) = (λz : E, a + z) ∘ (λt:ℝ, t • (b - a)) := rfl, rw [segment_eq_image', this, image_comp], refine ((convex_Icc _ _).is_linear_image _).translate _, exact is_linear_map.is_linear_map_smul' _ end lemma convex_halfspace_lt {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w < r} := (convex_Iio r).is_linear_preimage h lemma convex_halfspace_le {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w ≤ r} := (convex_Iic r).is_linear_preimage h lemma convex_halfspace_gt {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | r < f w} := (convex_Ioi r).is_linear_preimage h lemma convex_halfspace_ge {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | r ≤ f w} := (convex_Ici r).is_linear_preimage h lemma convex_hyperplane {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w = r} := begin show convex (f ⁻¹' {p | p = r}), rw set_of_eq_eq_singleton, exact (convex_singleton r).is_linear_preimage h end lemma convex_halfspace_re_lt (r : ℝ) : convex {c : ℂ | c.re < r} := convex_halfspace_lt (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_le (r : ℝ) : convex {c : ℂ | c.re ≤ r} := convex_halfspace_le (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_gt (r : ℝ) : convex {c : ℂ | r < c.re } := convex_halfspace_gt (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_lge (r : ℝ) : convex {c : ℂ | r ≤ c.re} := convex_halfspace_ge (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_im_lt (r : ℝ) : convex {c : ℂ | c.im < r} := convex_halfspace_lt (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_le (r : ℝ) : convex {c : ℂ | c.im ≤ r} := convex_halfspace_le (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_gt (r : ℝ) : convex {c : ℂ | r < c.im } := convex_halfspace_gt (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_lge (r : ℝ) : convex {c : ℂ | r ≤ c.im} := convex_halfspace_ge (is_linear_map.mk complex.add_im complex.smul_im) _ /-! ### Convex combinations in intervals -/ lemma convex.combo_self (a : α) {x y : α} (h : x + y = 1) : a = x * a + y * a := calc a = 1 * a : by rw [one_mul] ... = (x + y) * a : by rw [h] ... = x * a + y * a : by rw [add_mul] /-- If `x` is in an `Ioo`, it can be expressed as a convex combination of the endpoints. -/ lemma convex.mem_Ioo {a b x : α} (h : a < b) : x ∈ Ioo a b ↔ ∃ (x_a x_b : α), 0 < x_a ∧ 0 < x_b ∧ x_a + x_b = 1 ∧ x_a * a + x_b * b = x := begin split, { rintros ⟨h_ax, h_bx⟩, by_cases hab : ¬a < b, { exfalso; exact hab h }, { refine ⟨(b-x) / (b-a), (x-a) / (b-a), _⟩, refine ⟨div_pos (by linarith) (by linarith), div_pos (by linarith) (by linarith),_,_⟩; { field_simp [show b - a ≠ 0, by linarith], ring } } }, { rw [mem_Ioo], rintros ⟨xa, xb, ⟨hxa, hxb, hxaxb, h₂⟩⟩, rw [←h₂], exact ⟨by nlinarith [convex.combo_self a hxaxb], by nlinarith [convex.combo_self b hxaxb]⟩ } end /-- If `x` is in an `Ioc`, it can be expressed as a convex combination of the endpoints. -/ lemma convex.mem_Ioc {a b x : α} (h : a < b) : x ∈ Ioc a b ↔ ∃ (x_a x_b : α), 0 ≤ x_a ∧ 0 < x_b ∧ x_a + x_b = 1 ∧ x_a * a + x_b * b = x := begin split, { rintros ⟨h_ax, h_bx⟩, by_cases h_x : x = b, { exact ⟨0, 1, by linarith, by linarith, by ring, by {rw [h_x], ring}⟩ }, { rcases (convex.mem_Ioo h).mp ⟨h_ax, lt_of_le_of_ne h_bx h_x⟩ with ⟨x_a, x_b, Ioo_case⟩, exact ⟨x_a, x_b, by linarith, Ioo_case.2⟩ } }, { rw [mem_Ioc], rintros ⟨xa, xb, ⟨hxa, hxb, hxaxb, h₂⟩⟩, rw [←h₂], exact ⟨by nlinarith [convex.combo_self a hxaxb], by nlinarith [convex.combo_self b hxaxb]⟩ } end /-- If `x` is in an `Ico`, it can be expressed as a convex combination of the endpoints. -/ lemma convex.mem_Ico {a b x : α} (h : a < b) : x ∈ Ico a b ↔ ∃ (x_a x_b : α), 0 < x_a ∧ 0 ≤ x_b ∧ x_a + x_b = 1 ∧ x_a * a + x_b * b = x := begin split, { rintros ⟨h_ax, h_bx⟩, by_cases h_x : x = a, { exact ⟨1, 0, by linarith, by linarith, by ring, by {rw [h_x], ring}⟩ }, { rcases (convex.mem_Ioo h).mp ⟨lt_of_le_of_ne h_ax (ne.symm h_x), h_bx⟩ with ⟨x_a, x_b, Ioo_case⟩, exact ⟨x_a, x_b, Ioo_case.1, by linarith, (Ioo_case.2).2⟩ } }, { rw [mem_Ico], rintros ⟨xa, xb, ⟨hxa, hxb, hxaxb, h₂⟩⟩, rw [←h₂], exact ⟨by nlinarith [convex.combo_self a hxaxb], by nlinarith [convex.combo_self b hxaxb]⟩ } end /-- If `x` is in an `Icc`, it can be expressed as a convex combination of the endpoints. -/ lemma convex.mem_Icc {a b x : α} (h : a ≤ b) : x ∈ Icc a b ↔ ∃ (x_a x_b : α), 0 ≤ x_a ∧ 0 ≤ x_b ∧ x_a + x_b = 1 ∧ x_a * a + x_b * b = x := begin split, { intro x_in_I, rw [Icc, mem_set_of_eq] at x_in_I, rcases x_in_I with ⟨h_ax, h_bx⟩, by_cases hab' : a = b, { exact ⟨0, 1, le_refl 0, by linarith, by ring, by linarith⟩ }, change a ≠ b at hab', replace h : a < b, exact lt_of_le_of_ne h hab', by_cases h_x : x = a, { exact ⟨1, 0, by linarith, by linarith, by ring, by {rw [h_x], ring}⟩ }, { rcases (convex.mem_Ioc h).mp ⟨lt_of_le_of_ne h_ax (ne.symm h_x), h_bx⟩ with ⟨x_a, x_b, Ioo_case⟩, exact ⟨x_a, x_b, Ioo_case.1, by linarith, (Ioo_case.2).2⟩ } }, { rw [mem_Icc], rintros ⟨xa, xb, ⟨hxa, hxb, hxaxb, h₂⟩⟩, rw [←h₂], exact ⟨by nlinarith [convex.combo_self a hxaxb], by nlinarith [convex.combo_self b hxaxb]⟩ } end section submodule open submodule lemma submodule.convex (K : submodule ℝ E) : convex (↑K : set E) := by { repeat {intro}, refine add_mem _ (smul_mem _ _ _) (smul_mem _ _ _); assumption } lemma subspace.convex (K : subspace ℝ E) : convex (↑K : set E) := K.convex end submodule end sets /-! ### Convex and concave functions -/ section functions variables {β : Type*} [ordered_add_comm_monoid β] [semimodule ℝ β] local notation `[`x `, ` y `]` := segment x y /-- Convexity of functions -/ def convex_on (s : set E) (f : E → β) : Prop := convex s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y /-- Concavity of functions -/ def concave_on (s : set E) (f : E → β) : Prop := convex s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • f x + b • f y ≤ f (a • x + b • y) section variables [ordered_semimodule ℝ β] /-- A function `f` is concave iff `-f` is convex. -/ @[simp] lemma neg_convex_on_iff {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] (s : set E) (f : E → γ) : convex_on s (-f) ↔ concave_on s f := begin split, { rintros ⟨hconv, h⟩, refine ⟨hconv, _⟩, intros x y xs ys a b ha hb hab, specialize h xs ys ha hb hab, simp [neg_apply, neg_le, add_comm] at h, exact h }, { rintros ⟨hconv, h⟩, refine ⟨hconv, _⟩, intros x y xs ys a b ha hb hab, specialize h xs ys ha hb hab, simp [neg_apply, neg_le, add_comm, h] } end /-- A function `f` is concave iff `-f` is convex. -/ @[simp] lemma neg_concave_on_iff {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] (s : set E) (f : E → γ) : concave_on s (-f) ↔ convex_on s f:= by rw [← neg_convex_on_iff s (-f), neg_neg f] end lemma convex_on_id {s : set ℝ} (hs : convex s) : convex_on s id := ⟨hs, by { intros, refl }⟩ lemma concave_on_id {s : set ℝ} (hs : convex s) : concave_on s id := ⟨hs, by { intros, refl }⟩ lemma convex_on_const (c : β) (hs : convex s) : convex_on s (λ x:E, c) := ⟨hs, by { intros, simp only [← add_smul, *, one_smul] }⟩ lemma concave_on_const (c : β) (hs : convex s) : concave_on s (λ x:E, c) := @convex_on_const _ _ _ _ (order_dual β) _ _ c hs variables {t : set E} lemma convex_on_iff_div {f : E → β} : convex_on s f ↔ convex s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → f ((a/(a+b)) • x + (b/(a+b)) • y) ≤ (a/(a+b)) • f x + (b/(a+b)) • f y := and_congr iff.rfl ⟨begin intros h x y hx hy a b ha hb hab, apply h hx hy (div_nonneg ha $ le_of_lt hab) (div_nonneg hb $ le_of_lt hab), rw [←add_div], exact div_self (ne_of_gt hab) end, begin intros h x y hx hy a b ha hb hab, simpa [hab, zero_lt_one] using h hx hy ha hb, end⟩ lemma concave_on_iff_div {f : E → β} : concave_on s f ↔ convex s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → (a/(a+b)) • f x + (b/(a+b)) • f y ≤ f ((a/(a+b)) • x + (b/(a+b)) • y) := @convex_on_iff_div _ _ _ _ (order_dual β) _ _ _ /-- For a function on a convex set in a linear ordered space, in order to prove that it is convex it suffices to verify the inequality `f (a • x + b • y) ≤ a • f x + b • f y` only for `x < y` and positive `a`, `b`. The main use case is `E = ℝ` however one can apply it, e.g., to `ℝ^n` with lexicographic order. -/ lemma linear_order.convex_on_of_lt {f : E → β} [linear_order E] (hs : convex s) (hf : ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → x < y → ∀ ⦃a b : ℝ⦄, 0 < a → 0 < b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y) : convex_on s f := begin use hs, intros x y hx hy a b ha hb hab, wlog hxy : x<=y using [x y a b, y x b a], { exact le_total _ _ }, { cases eq_or_lt_of_le hxy with hxy hxy, by { subst y, rw [← add_smul, ← add_smul, hab, one_smul, one_smul] }, cases eq_or_lt_of_le ha with ha ha, by { subst a, rw [zero_add] at hab, subst b, simp }, cases eq_or_lt_of_le hb with hb hb, by { subst b, rw [add_zero] at hab, subst a, simp }, exact hf hx hy hxy ha hb hab } end /-- For a function on a convex set in a linear ordered space, in order to prove that it is concave it suffices to verify the inequality `a • f x + b • f y ≤ f (a • x + b • y)` only for `x < y` and positive `a`, `b`. The main use case is `E = ℝ` however one can apply it, e.g., to `ℝ^n` with lexicographic order. -/ lemma linear_order.concave_on_of_lt {f : E → β} [linear_order E] (hs : convex s) (hf : ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → x < y → ∀ ⦃a b : ℝ⦄, 0 < a → 0 < b → a + b = 1 → a • f x + b • f y ≤ f (a • x + b • y)) : concave_on s f := @linear_order.convex_on_of_lt _ _ _ _ (order_dual β) _ _ f _ hs hf /-- For a function `f` defined on a convex subset `D` of `ℝ`, if for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is less than or equal to the slope of the secant line of `f` on `[x, z]`, then `f` is convex on `D`. This way of proving convexity of a function is used in the proof of convexity of a function with a monotone derivative. -/ lemma convex_on_real_of_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} (hf : ∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f y - f x) / (y - x) ≤ (f z - f y) / (z - y)) : convex_on s f := linear_order.convex_on_of_lt hs begin assume x z hx hz hxz a b ha hb hab, let y := a * x + b * z, have hxy : x < y, { rw [← one_mul x, ← hab, add_mul], exact add_lt_add_left ((mul_lt_mul_left hb).2 hxz) _ }, have hyz : y < z, { rw [← one_mul z, ← hab, add_mul], exact add_lt_add_right ((mul_lt_mul_left ha).2 hxz) _ }, have : (f y - f x) * (z - y) ≤ (f z - f y) * (y - x), from (div_le_div_iff (sub_pos.2 hxy) (sub_pos.2 hyz)).1 (hf hx hz hxy hyz), have A : z - y + (y - x) = z - x, by abel, have B : 0 < z - x, from sub_pos.2 (lt_trans hxy hyz), rw [sub_mul, sub_mul, sub_le_iff_le_add', ← add_sub_assoc, le_sub_iff_add_le, ← mul_add, A, ← le_div_iff B, add_div, mul_div_assoc, mul_div_assoc, mul_comm (f x), mul_comm (f z)] at this, rw [eq_comm, ← sub_eq_iff_eq_add] at hab; subst a, convert this; symmetry; simp only [div_eq_iff (ne_of_gt B), y]; ring end /-- For a function `f` defined on a subset `D` of `ℝ`, if `f` is convex on `D`, then for any three points `x<y<z`, the slope of the secant line of `f` on `[x, y]` is less than or equal to the slope of the secant line of `f` on `[x, z]`. -/ lemma convex_on.slope_mono_adjacent {s : set ℝ} {f : ℝ → ℝ} (hf : convex_on s f) {x y z : ℝ} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) : (f y - f x) / (y - x) ≤ (f z - f y) / (z - y) := begin have h₁ : 0 < y - x := by linarith, have h₂ : 0 < z - y := by linarith, have h₃ : 0 < z - x := by linarith, suffices : f y / (y - x) + f y / (z - y) ≤ f x / (y - x) + f z / (z - y), by { ring at this ⊢, linarith }, set a := (z - y) / (z - x), set b := (y - x) / (z - x), have heqz : a • x + b • z = y, by { field_simp, rw div_eq_iff; [ring, linarith], }, have key, from hf.2 hx hz (show 0 ≤ a, by apply div_nonneg; linarith) (show 0 ≤ b, by apply div_nonneg; linarith) (show a + b = 1, by { field_simp, rw div_eq_iff; [ring, linarith], }), rw heqz at key, replace key := mul_le_mul_of_nonneg_left key (le_of_lt h₃), field_simp [ne_of_gt h₁, ne_of_gt h₂, ne_of_gt h₃, mul_comm (z - x) _] at key ⊢, rw div_le_div_right, { linarith, }, { nlinarith, }, end /-- For a function `f` defined on a convex subset `D` of `ℝ`, `f` is convex on `D` iff for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is less than or equal to the slope of the secant line of `f` on `[x, z]`. -/ lemma convex_on_real_iff_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} : convex_on s f ↔ (∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f y - f x) / (y - x) ≤ (f z - f y) / (z - y)) := ⟨convex_on.slope_mono_adjacent, convex_on_real_of_slope_mono_adjacent hs⟩ /-- For a function `f` defined on a convex subset `D` of `ℝ`, if for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is greater than or equal to the slope of the secant line of `f` on `[x, z]`, then `f` is concave on `D`. -/ lemma concave_on_real_of_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} (hf : ∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f z - f y) / (z - y) ≤ (f y - f x) / (y - x)) : concave_on s f := begin rw [←neg_convex_on_iff], apply convex_on_real_of_slope_mono_adjacent hs, intros x y z xs zs xy yz, rw [←neg_le_neg_iff, ←neg_div, ←neg_div, neg_sub, neg_sub], simp only [hf xs zs xy yz, neg_sub_neg, pi.neg_apply], end /-- For a function `f` defined on a subset `D` of `ℝ`, if `f` is concave on `D`, then for any three points `x<y<z`, the slope of the secant line of `f` on `[x, y]` is greater than or equal to the slope of the secant line of `f` on `[x, z]`. -/ lemma concave_on.slope_mono_adjacent {s : set ℝ} {f : ℝ → ℝ} (hf : concave_on s f) {x y z : ℝ} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) : (f z - f y) / (z - y) ≤ (f y - f x) / (y - x) := begin rw [←neg_le_neg_iff, ←neg_div, ←neg_div, neg_sub, neg_sub], rw [←neg_sub_neg (f y), ←neg_sub_neg (f z)], simp_rw [←pi.neg_apply], rw [←neg_convex_on_iff] at hf, apply convex_on.slope_mono_adjacent hf; assumption, end /-- For a function `f` defined on a convex subset `D` of `ℝ`, `f` is concave on `D` iff for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is greater than or equal to the slope of the secant line of `f` on `[x, z]`. -/ lemma concave_on_real_iff_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} : concave_on s f ↔ (∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f z - f y) / (z - y) ≤ (f y - f x) / (y - x)) := ⟨concave_on.slope_mono_adjacent, concave_on_real_of_slope_mono_adjacent hs⟩ lemma convex_on.subset {f : E → β} (h_convex_on : convex_on t f) (h_subset : s ⊆ t) (h_convex : convex s) : convex_on s f := begin apply and.intro h_convex, intros x y hx hy, exact h_convex_on.2 (h_subset hx) (h_subset hy), end lemma concave_on.subset {f : E → β} (h_concave_on : concave_on t f) (h_subset : s ⊆ t) (h_convex : convex s) : concave_on s f := @convex_on.subset _ _ _ _ (order_dual β) _ _ t f h_concave_on h_subset h_convex lemma convex_on.add {f g : E → β} (hf : convex_on s f) (hg : convex_on s g) : convex_on s (λx, f x + g x) := begin apply and.intro hf.1, intros x y hx hy a b ha hb hab, calc f (a • x + b • y) + g (a • x + b • y) ≤ (a • f x + b • f y) + (a • g x + b • g y) : add_le_add (hf.2 hx hy ha hb hab) (hg.2 hx hy ha hb hab) ... = a • f x + a • g x + b • f y + b • g y : by abel ... = a • (f x + g x) + b • (f y + g y) : by simp [smul_add, add_assoc] end lemma concave_on.add {f g : E → β} (hf : concave_on s f) (hg : concave_on s g) : concave_on s (λx, f x + g x) := @convex_on.add _ _ _ _ (order_dual β) _ _ f g hf hg lemma convex_on.smul [ordered_semimodule ℝ β] {f : E → β} {c : ℝ} (hc : 0 ≤ c) (hf : convex_on s f) : convex_on s (λx, c • f x) := begin apply and.intro hf.1, intros x y hx hy a b ha hb hab, calc c • f (a • x + b • y) ≤ c • (a • f x + b • f y) : smul_le_smul_of_nonneg (hf.2 hx hy ha hb hab) hc ... = a • (c • f x) + b • (c • f y) : by simp only [smul_add, smul_comm c] end lemma concave_on.smul [ordered_semimodule ℝ β] {f : E → β} {c : ℝ} (hc : 0 ≤ c) (hf : concave_on s f) : concave_on s (λx, c • f x) := @convex_on.smul _ _ _ _ (order_dual β) _ _ _ f c hc hf /-- A convex function on a segment is upper-bounded by the max of its endpoints. -/ lemma convex_on.le_on_segment' {γ : Type*} [linear_ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} {x y : E} {a b : ℝ} (hf : convex_on s f) (hx : x ∈ s) (hy : y ∈ s) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : f (a • x + b • y) ≤ max (f x) (f y) := calc f (a • x + b • y) ≤ a • f x + b • f y : hf.2 hx hy ha hb hab ... ≤ a • max (f x) (f y) + b • max (f x) (f y) : add_le_add (smul_le_smul_of_nonneg (le_max_left _ _) ha) (smul_le_smul_of_nonneg (le_max_right _ _) hb) ... ≤ max (f x) (f y) : by rw [←add_smul, hab, one_smul] /-- A concave function on a segment is lower-bounded by the min of its endpoints. -/ lemma concave_on.le_on_segment' {γ : Type*} [linear_ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} {x y : E} {a b : ℝ} (hf : concave_on s f) (hx : x ∈ s) (hy : y ∈ s) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : min (f x) (f y) ≤ f (a • x + b • y) := @convex_on.le_on_segment' _ _ _ _ (order_dual γ) _ _ _ f x y a b hf hx hy ha hb hab /-- A convex function on a segment is upper-bounded by the max of its endpoints. -/ lemma convex_on.le_on_segment {γ : Type*} [linear_ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : convex_on s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ [x, y]) : f z ≤ max (f x) (f y) := let ⟨a, b, ha, hb, hab, hz⟩ := hz in hz ▸ hf.le_on_segment' hx hy ha hb hab /-- A concave function on a segment is lower-bounded by the min of its endpoints. -/ lemma concave_on.le_on_segment {γ : Type*} [linear_ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : concave_on s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ [x, y]) : min (f x) (f y) ≤ f z := @convex_on.le_on_segment _ _ _ _ (order_dual γ) _ _ _ f hf x y z hx hy hz lemma convex_on.convex_le [ordered_semimodule ℝ β] {f : E → β} (hf : convex_on s f) (r : β) : convex {x ∈ s | f x ≤ r} := convex_iff_segment_subset.2 $ λ x y hx hy z hz, begin refine ⟨hf.1.segment_subset hx.1 hy.1 hz,_⟩, rcases hz with ⟨za,zb,hza,hzb,hzazb,H⟩, rw ←H, calc f (za • x + zb • y) ≤ za • (f x) + zb • (f y) : hf.2 hx.1 hy.1 hza hzb hzazb ... ≤ za • r + zb • r : add_le_add (smul_le_smul_of_nonneg hx.2 hza) (smul_le_smul_of_nonneg hy.2 hzb) ... ≤ r : by simp [←add_smul, hzazb] end lemma concave_on.concave_le [ordered_semimodule ℝ β] {f : E → β} (hf : concave_on s f) (r : β) : convex {x ∈ s | r ≤ f x} := @convex_on.convex_le _ _ _ _ (order_dual β) _ _ _ f hf r lemma convex_on.convex_lt {γ : Type*} [ordered_cancel_add_comm_monoid γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : convex_on s f) (r : γ) : convex {x ∈ s | f x < r} := begin intros a b as bs xa xb hxa hxb hxaxb, refine ⟨hf.1 as.1 bs.1 hxa hxb hxaxb, _⟩, dsimp, by_cases H : xa = 0, { have H' : xb = 1 := by rwa [H, zero_add] at hxaxb, rw [H, H', zero_smul, one_smul, zero_add], exact bs.2 }, { calc f (xa • a + xb • b) ≤ xa • (f a) + xb • (f b) : hf.2 as.1 bs.1 hxa hxb hxaxb ... < xa • r + xb • (f b) : (add_lt_add_iff_right (xb • (f b))).mpr (smul_lt_smul_of_pos as.2 (lt_of_le_of_ne hxa (ne.symm H))) ... ≤ xa • r + xb • r : (add_le_add_iff_left (xa • r)).mpr (smul_le_smul_of_nonneg bs.2.le hxb) ... = r : by simp only [←add_smul, hxaxb, one_smul] } end lemma concave_on.convex_lt {γ : Type*} [ordered_cancel_add_comm_monoid γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : concave_on s f) (r : γ) : convex {x ∈ s | r < f x} := @convex_on.convex_lt _ _ _ _ (order_dual γ) _ _ _ f hf r lemma convex_on.convex_epigraph {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : convex_on s f) : convex {p : E × γ | p.1 ∈ s ∧ f p.1 ≤ p.2} := begin rintros ⟨x, r⟩ ⟨y, t⟩ ⟨hx, hr⟩ ⟨hy, ht⟩ a b ha hb hab, refine ⟨hf.1 hx hy ha hb hab, _⟩, calc f (a • x + b • y) ≤ a • f x + b • f y : hf.2 hx hy ha hb hab ... ≤ a • r + b • t : add_le_add (smul_le_smul_of_nonneg hr ha) (smul_le_smul_of_nonneg ht hb) end lemma concave_on.convex_hypograph {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : concave_on s f) : convex {p : E × γ | p.1 ∈ s ∧ p.2 ≤ f p.1} := @convex_on.convex_epigraph _ _ _ _ (order_dual γ) _ _ _ f hf lemma convex_on_iff_convex_epigraph {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} : convex_on s f ↔ convex {p : E × γ | p.1 ∈ s ∧ f p.1 ≤ p.2} := begin refine ⟨convex_on.convex_epigraph, λ h, ⟨_, _⟩⟩, { assume x y hx hy a b ha hb hab, exact (@h (x, f x) (y, f y) ⟨hx, le_refl _⟩ ⟨hy, le_refl _⟩ a b ha hb hab).1 }, { assume x y hx hy a b ha hb hab, exact (@h (x, f x) (y, f y) ⟨hx, le_refl _⟩ ⟨hy, le_refl _⟩ a b ha hb hab).2 } end lemma concave_on_iff_convex_hypograph {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} : concave_on s f ↔ convex {p : E × γ | p.1 ∈ s ∧ p.2 ≤ f p.1} := @convex_on_iff_convex_epigraph _ _ _ _ (order_dual γ) _ _ _ f /-- If a function is convex on `s`, it remains convex when precomposed by an affine map. -/ lemma convex_on.comp_affine_map {f : F → β} (g : E →ᵃ[ℝ] F) {s : set F} (hf : convex_on s f) : convex_on (g ⁻¹' s) (f ∘ g) := begin refine ⟨hf.1.affine_preimage _,_⟩, intros x y xs ys a b ha hb hab, calc (f ∘ g) (a • x + b • y) = f (g (a • x + b • y)) : rfl ... = f (a • (g x) + b • (g y)) : by rw [convex.combo_affine_apply hab] ... ≤ a • f (g x) + b • f (g y) : hf.2 xs ys ha hb hab ... = a • (f ∘ g) x + b • (f ∘ g) y : rfl end /-- If a function is concave on `s`, it remains concave when precomposed by an affine map. -/ lemma concave_on.comp_affine_map {f : F → β} (g : E →ᵃ[ℝ] F) {s : set F} (hf : concave_on s f) : concave_on (g ⁻¹' s) (f ∘ g) := @convex_on.comp_affine_map _ _ _ _ _ _ (order_dual β) _ _ f g s hf /-- If `g` is convex on `s`, so is `(g ∘ f)` on `f ⁻¹' s` for a linear `f`. -/ lemma convex_on.comp_linear_map {g : F → β} {s : set F} (hg : convex_on s g) (f : E →ₗ[ℝ] F) : convex_on (f ⁻¹' s) (g ∘ f) := hg.comp_affine_map f.to_affine_map /-- If `g` is concave on `s`, so is `(g ∘ f)` on `f ⁻¹' s` for a linear `f`. -/ lemma concave_on.comp_linear_map {g : F → β} {s : set F} (hg : concave_on s g) (f : E →ₗ[ℝ] F) : concave_on (f ⁻¹' s) (g ∘ f) := hg.comp_affine_map f.to_affine_map /-- If a function is convex on `s`, it remains convex after a translation. -/ lemma convex_on.translate_right {f : E → β} {s : set E} {a : E} (hf : convex_on s f) : convex_on ((λ z, a + z) ⁻¹' s) (f ∘ (λ z, a + z)) := hf.comp_affine_map $ affine_map.const ℝ E a +ᵥ affine_map.id ℝ E /-- If a function is concave on `s`, it remains concave after a translation. -/ lemma concave_on.translate_right {f : E → β} {s : set E} {a : E} (hf : concave_on s f) : concave_on ((λ z, a + z) ⁻¹' s) (f ∘ (λ z, a + z)) := hf.comp_affine_map $ affine_map.const ℝ E a +ᵥ affine_map.id ℝ E /-- If a function is convex on `s`, it remains convex after a translation. -/ lemma convex_on.translate_left {f : E → β} {s : set E} {a : E} (hf : convex_on s f) : convex_on ((λ z, a + z) ⁻¹' s) (f ∘ (λ z, z + a)) := by simpa only [add_comm] using hf.translate_right /-- If a function is concave on `s`, it remains concave after a translation. -/ lemma concave_on.translate_left {f : E → β} {s : set E} {a : E} (hf : concave_on s f) : concave_on ((λ z, a + z) ⁻¹' s) (f ∘ (λ z, z + a)) := by simpa only [add_comm] using hf.translate_right end functions /-! ### Center of mass -/ section center_mass /-- Center of mass of a finite collection of points with prescribed weights. Note that we require neither `0 ≤ w i` nor `∑ w = 1`. -/ noncomputable def finset.center_mass (t : finset ι) (w : ι → ℝ) (z : ι → E) : E := (∑ i in t, w i)⁻¹ • (∑ i in t, w i • z i) variables (i j : ι) (c : ℝ) (t : finset ι) (w : ι → ℝ) (z : ι → E) open finset lemma finset.center_mass_empty : (∅ : finset ι).center_mass w z = 0 := by simp only [center_mass, sum_empty, smul_zero] lemma finset.center_mass_pair (hne : i ≠ j) : ({i, j} : finset ι).center_mass w z = (w i / (w i + w j)) • z i + (w j / (w i + w j)) • z j := by simp only [center_mass, sum_pair hne, smul_add, (mul_smul _ _ _).symm, div_eq_inv_mul] variable {w} lemma finset.center_mass_insert (ha : i ∉ t) (hw : ∑ j in t, w j ≠ 0) : (insert i t).center_mass w z = (w i / (w i + ∑ j in t, w j)) • z i + ((∑ j in t, w j) / (w i + ∑ j in t, w j)) • t.center_mass w z := begin simp only [center_mass, sum_insert ha, smul_add, (mul_smul _ _ _).symm, ← div_eq_inv_mul], congr' 2, rw [div_mul_eq_mul_div, mul_inv_cancel hw, one_div] end lemma finset.center_mass_singleton (hw : w i ≠ 0) : ({i} : finset ι).center_mass w z = z i := by rw [center_mass, sum_singleton, sum_singleton, ← mul_smul, inv_mul_cancel hw, one_smul] lemma finset.center_mass_eq_of_sum_1 (hw : ∑ i in t, w i = 1) : t.center_mass w z = ∑ i in t, w i • z i := by simp only [finset.center_mass, hw, inv_one, one_smul] lemma finset.center_mass_smul : t.center_mass w (λ i, c • z i) = c • t.center_mass w z := by simp only [finset.center_mass, finset.smul_sum, (mul_smul _ _ _).symm, mul_comm c, mul_assoc] /-- A convex combination of two centers of mass is a center of mass as well. This version deals with two different index types. -/ lemma finset.center_mass_segment' (s : finset ι) (t : finset ι') (ws : ι → ℝ) (zs : ι → E) (wt : ι' → ℝ) (zt : ι' → E) (hws : ∑ i in s, ws i = 1) (hwt : ∑ i in t, wt i = 1) (a b : ℝ) (hab : a + b = 1) : a • s.center_mass ws zs + b • t.center_mass wt zt = (s.map function.embedding.inl ∪ t.map function.embedding.inr).center_mass (sum.elim (λ i, a * ws i) (λ j, b * wt j)) (sum.elim zs zt) := begin rw [s.center_mass_eq_of_sum_1 _ hws, t.center_mass_eq_of_sum_1 _ hwt, smul_sum, smul_sum, ← finset.sum_sum_elim, finset.center_mass_eq_of_sum_1], { congr' with ⟨⟩; simp only [sum.elim_inl, sum.elim_inr, mul_smul] }, { rw [sum_sum_elim, ← mul_sum, ← mul_sum, hws, hwt, mul_one, mul_one, hab] } end /-- A convex combination of two centers of mass is a center of mass as well. This version works if two centers of mass share the set of original points. -/ lemma finset.center_mass_segment (s : finset ι) (w₁ w₂ : ι → ℝ) (z : ι → E) (hw₁ : ∑ i in s, w₁ i = 1) (hw₂ : ∑ i in s, w₂ i = 1) (a b : ℝ) (hab : a + b = 1) : a • s.center_mass w₁ z + b • s.center_mass w₂ z = s.center_mass (λ i, a * w₁ i + b * w₂ i) z := have hw : ∑ i in s, (a * w₁ i + b * w₂ i) = 1, by simp only [mul_sum.symm, sum_add_distrib, mul_one, *], by simp only [finset.center_mass_eq_of_sum_1, smul_sum, sum_add_distrib, add_smul, mul_smul, *] lemma finset.center_mass_ite_eq (hi : i ∈ t) : t.center_mass (λ j, if (i = j) then 1 else 0) z = z i := begin rw [finset.center_mass_eq_of_sum_1], transitivity ∑ j in t, if (i = j) then z i else 0, { congr' with i, split_ifs, exacts [h ▸ one_smul _ _, zero_smul _ _] }, { rw [sum_ite_eq, if_pos hi] }, { rw [sum_ite_eq, if_pos hi] } end variables {t w} lemma finset.center_mass_subset {t' : finset ι} (ht : t ⊆ t') (h : ∀ i ∈ t', i ∉ t → w i = 0) : t.center_mass w z = t'.center_mass w z := begin rw [center_mass, sum_subset ht h, smul_sum, center_mass, smul_sum], apply sum_subset ht, assume i hit' hit, rw [h i hit' hit, zero_smul, smul_zero] end lemma finset.center_mass_filter_ne_zero : (t.filter (λ i, w i ≠ 0)).center_mass w z = t.center_mass w z := finset.center_mass_subset z (filter_subset _ _) $ λ i hit hit', by simpa only [hit, mem_filter, true_and, ne.def, not_not] using hit' variable {z} /-- The center of mass of a finite subset of a convex set belongs to the set provided that all weights are non-negative, and the total weight is positive. -/ lemma convex.center_mass_mem (hs : convex s) : (∀ i ∈ t, 0 ≤ w i) → (0 < ∑ i in t, w i) → (∀ i ∈ t, z i ∈ s) → t.center_mass w z ∈ s := begin induction t using finset.induction with i t hi ht, { simp [lt_irrefl] }, intros h₀ hpos hmem, have zi : z i ∈ s, from hmem _ (mem_insert_self _ _), have hs₀ : ∀ j ∈ t, 0 ≤ w j, from λ j hj, h₀ j $ mem_insert_of_mem hj, rw [sum_insert hi] at hpos, by_cases hsum_t : ∑ j in t, w j = 0, { have ws : ∀ j ∈ t, w j = 0, from (sum_eq_zero_iff_of_nonneg hs₀).1 hsum_t, have wz : ∑ j in t, w j • z j = 0, from sum_eq_zero (λ i hi, by simp [ws i hi]), simp only [center_mass, sum_insert hi, wz, hsum_t, add_zero], simp only [hsum_t, add_zero] at hpos, rw [← mul_smul, inv_mul_cancel (ne_of_gt hpos), one_smul], exact zi }, { rw [finset.center_mass_insert _ _ _ hi hsum_t], refine convex_iff_div.1 hs zi (ht hs₀ _ _) _ (sum_nonneg hs₀) hpos, { exact lt_of_le_of_ne (sum_nonneg hs₀) (ne.symm hsum_t) }, { intros j hj, exact hmem j (mem_insert_of_mem hj) }, { exact h₀ _ (mem_insert_self _ _) } } end lemma convex.sum_mem (hs : convex s) (h₀ : ∀ i ∈ t, 0 ≤ w i) (h₁ : ∑ i in t, w i = 1) (hz : ∀ i ∈ t, z i ∈ s) : ∑ i in t, w i • z i ∈ s := by simpa only [h₁, center_mass, inv_one, one_smul] using hs.center_mass_mem h₀ (h₁.symm ▸ zero_lt_one) hz lemma convex_iff_sum_mem : convex s ↔ (∀ (t : finset E) (w : E → ℝ), (∀ i ∈ t, 0 ≤ w i) → ∑ i in t, w i = 1 → (∀ x ∈ t, x ∈ s) → ∑ x in t, w x • x ∈ s ) := begin refine ⟨λ hs t w hw₀ hw₁ hts, hs.sum_mem hw₀ hw₁ hts, _⟩, intros h x y hx hy a b ha hb hab, by_cases h_cases: x = y, { rw [h_cases, ←add_smul, hab, one_smul], exact hy }, { convert h {x, y} (λ z, if z = y then b else a) _ _ _, { simp only [sum_pair h_cases, if_neg h_cases, if_pos rfl] }, { simp_intros i hi, cases hi; subst i; simp [ha, hb, if_neg h_cases] }, { simp only [sum_pair h_cases, if_neg h_cases, if_pos rfl, hab] }, { simp_intros i hi, cases hi; subst i; simp [hx, hy, if_neg h_cases] } } end /-- Jensen's inequality, `finset.center_mass` version. -/ lemma convex_on.map_center_mass_le {f : E → ℝ} (hf : convex_on s f) (h₀ : ∀ i ∈ t, 0 ≤ w i) (hpos : 0 < ∑ i in t, w i) (hmem : ∀ i ∈ t, z i ∈ s) : f (t.center_mass w z) ≤ t.center_mass w (f ∘ z) := begin have hmem' : ∀ i ∈ t, (z i, (f ∘ z) i) ∈ {p : E × ℝ | p.1 ∈ s ∧ f p.1 ≤ p.2}, from λ i hi, ⟨hmem i hi, le_refl _⟩, convert (hf.convex_epigraph.center_mass_mem h₀ hpos hmem').2; simp only [center_mass, function.comp, prod.smul_fst, prod.fst_sum, prod.smul_snd, prod.snd_sum] end /-- Jensen's inequality, `finset.sum` version. -/ lemma convex_on.map_sum_le {f : E → ℝ} (hf : convex_on s f) (h₀ : ∀ i ∈ t, 0 ≤ w i) (h₁ : ∑ i in t, w i = 1) (hmem : ∀ i ∈ t, z i ∈ s) : f (∑ i in t, w i • z i) ≤ ∑ i in t, w i * (f (z i)) := by simpa only [center_mass, h₁, inv_one, one_smul] using hf.map_center_mass_le h₀ (h₁.symm ▸ zero_lt_one) hmem /-- If a function `f` is convex on `s` takes value `y` at the center of mass of some points `z i ∈ s`, then for some `i` we have `y ≤ f (z i)`. -/ lemma convex_on.exists_ge_of_center_mass {f : E → ℝ} (h : convex_on s f) (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hws : 0 < ∑ i in t, w i) (hz : ∀ i ∈ t, z i ∈ s) : ∃ i ∈ t, f (t.center_mass w z) ≤ f (z i) := begin set y := t.center_mass w z, have : f y ≤ t.center_mass w (f ∘ z) := h.map_center_mass_le hw₀ hws hz, rw ← sum_filter_ne_zero at hws, rw [← finset.center_mass_filter_ne_zero (f ∘ z), center_mass, smul_eq_mul, ← div_eq_inv_mul, le_div_iff hws, mul_sum] at this, replace : ∃ i ∈ t.filter (λ i, w i ≠ 0), f y * w i ≤ w i • (f ∘ z) i := exists_le_of_sum_le (nonempty_of_sum_ne_zero (ne_of_gt hws)) this, rcases this with ⟨i, hi, H⟩, rw [mem_filter] at hi, use [i, hi.1], simp only [smul_eq_mul, mul_comm (w i)] at H, refine (mul_le_mul_right _).1 H, exact lt_of_le_of_ne (hw₀ i hi.1) hi.2.symm end end center_mass /-! ### Convex hull -/ section convex_hull variable {t : set E} /-- The convex hull of a set `s` is the minimal convex set that includes `s`. -/ def convex_hull (s : set E) : set E := ⋂ (t : set E) (hst : s ⊆ t) (ht : convex t), t variable (s) lemma subset_convex_hull : s ⊆ convex_hull s := set.subset_Inter $ λ t, set.subset_Inter $ λ hst, set.subset_Inter $ λ ht, hst lemma convex_convex_hull : convex (convex_hull s) := convex_Inter $ λ t, convex_Inter $ λ ht, convex_Inter id variable {s} lemma convex_hull_min (hst : s ⊆ t) (ht : convex t) : convex_hull s ⊆ t := set.Inter_subset_of_subset t $ set.Inter_subset_of_subset hst $ set.Inter_subset _ ht lemma convex_hull_mono (hst : s ⊆ t) : convex_hull s ⊆ convex_hull t := convex_hull_min (set.subset.trans hst $ subset_convex_hull t) (convex_convex_hull t) lemma convex.convex_hull_eq {s : set E} (hs : convex s) : convex_hull s = s := set.subset.antisymm (convex_hull_min (set.subset.refl _) hs) (subset_convex_hull s) @[simp] lemma convex_hull_singleton {x : E} : convex_hull ({x} : set E) = {x} := (convex_singleton x).convex_hull_eq lemma is_linear_map.image_convex_hull {f : E → F} (hf : is_linear_map ℝ f) : f '' (convex_hull s) = convex_hull (f '' s) := begin refine set.subset.antisymm _ _, { rw [set.image_subset_iff], exact convex_hull_min (set.image_subset_iff.1 $ subset_convex_hull $ f '' s) ((convex_convex_hull (f '' s)).is_linear_preimage hf) }, { exact convex_hull_min (set.image_subset _ $ subset_convex_hull s) ((convex_convex_hull s).is_linear_image hf) } end lemma linear_map.image_convex_hull (f : E →ₗ[ℝ] F) : f '' (convex_hull s) = convex_hull (f '' s) := f.is_linear.image_convex_hull lemma finset.center_mass_mem_convex_hull (t : finset ι) {w : ι → ℝ} (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hws : 0 < ∑ i in t, w i) {z : ι → E} (hz : ∀ i ∈ t, z i ∈ s) : t.center_mass w z ∈ convex_hull s := (convex_convex_hull s).center_mass_mem hw₀ hws (λ i hi, subset_convex_hull s $ hz i hi) -- TODO : Do we need other versions of the next lemma? /-- Convex hull of `s` is equal to the set of all centers of masses of `finset`s `t`, `z '' t ⊆ s`. This version allows finsets in any type in any universe. -/ lemma convex_hull_eq (s : set E) : convex_hull s = {x : E | ∃ (ι : Type u') (t : finset ι) (w : ι → ℝ) (z : ι → E) (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hw₁ : ∑ i in t, w i = 1) (hz : ∀ i ∈ t, z i ∈ s), t.center_mass w z = x} := begin refine subset.antisymm (convex_hull_min _ _) _, { intros x hx, use [punit, {punit.star}, λ _, 1, λ _, x, λ _ _, zero_le_one, finset.sum_singleton, λ _ _, hx], simp only [finset.center_mass, finset.sum_singleton, inv_one, one_smul] }, { rintros x y ⟨ι, sx, wx, zx, hwx₀, hwx₁, hzx, rfl⟩ ⟨ι', sy, wy, zy, hwy₀, hwy₁, hzy, rfl⟩ a b ha hb hab, rw [finset.center_mass_segment' _ _ _ _ _ _ hwx₁ hwy₁ _ _ hab], refine ⟨_, _, _, _, _, _, _, rfl⟩, { rintros i hi, rw [finset.mem_union, finset.mem_map, finset.mem_map] at hi, rcases hi with ⟨j, hj, rfl⟩|⟨j, hj, rfl⟩; simp only [sum.elim_inl, sum.elim_inr]; apply_rules [mul_nonneg, hwx₀, hwy₀] }, { simp [finset.sum_sum_elim, finset.mul_sum.symm, *] }, { intros i hi, rw [finset.mem_union, finset.mem_map, finset.mem_map] at hi, rcases hi with ⟨j, hj, rfl⟩|⟨j, hj, rfl⟩; apply_rules [hzx, hzy] } }, { rintros _ ⟨ι, t, w, z, hw₀, hw₁, hz, rfl⟩, exact t.center_mass_mem_convex_hull hw₀ (hw₁.symm ▸ zero_lt_one) hz } end /-- Maximum principle for convex functions. If a function `f` is convex on the convex hull of `s`, then `f` can't have a maximum on `convex_hull s` outside of `s`. -/ lemma convex_on.exists_ge_of_mem_convex_hull {f : E → ℝ} (hf : convex_on (convex_hull s) f) {x} (hx : x ∈ convex_hull s) : ∃ y ∈ s, f x ≤ f y := begin rw convex_hull_eq at hx, rcases hx with ⟨α, t, w, z, hw₀, hw₁, hz, rfl⟩, rcases hf.exists_ge_of_center_mass hw₀ (hw₁.symm ▸ zero_lt_one) (λ i hi, subset_convex_hull s (hz i hi)) with ⟨i, hit, Hi⟩, exact ⟨z i, hz i hit, Hi⟩ end lemma finset.convex_hull_eq (s : finset E) : convex_hull ↑s = {x : E | ∃ (w : E → ℝ) (hw₀ : ∀ y ∈ s, 0 ≤ w y) (hw₁ : ∑ y in s, w y = 1), s.center_mass w id = x} := begin refine subset.antisymm (convex_hull_min _ _) _, { intros x hx, rw [finset.mem_coe] at hx, refine ⟨_, _, _, finset.center_mass_ite_eq _ _ _ hx⟩, { intros, split_ifs, exacts [zero_le_one, le_refl 0] }, { rw [finset.sum_ite_eq, if_pos hx] } }, { rintros x y ⟨wx, hwx₀, hwx₁, rfl⟩ ⟨wy, hwy₀, hwy₁, rfl⟩ a b ha hb hab, rw [finset.center_mass_segment _ _ _ _ hwx₁ hwy₁ _ _ hab], refine ⟨_, _, _, rfl⟩, { rintros i hi, apply_rules [add_nonneg, mul_nonneg, hwx₀, hwy₀], }, { simp only [finset.sum_add_distrib, finset.mul_sum.symm, mul_one, *] } }, { rintros _ ⟨w, hw₀, hw₁, rfl⟩, exact s.center_mass_mem_convex_hull (λ x hx, hw₀ _ hx) (hw₁.symm ▸ zero_lt_one) (λ x hx, hx) } end lemma set.finite.convex_hull_eq {s : set E} (hs : finite s) : convex_hull s = {x : E | ∃ (w : E → ℝ) (hw₀ : ∀ y ∈ s, 0 ≤ w y) (hw₁ : ∑ y in hs.to_finset, w y = 1), hs.to_finset.center_mass w id = x} := by simpa only [set.finite.coe_to_finset, set.finite.mem_to_finset, exists_prop] using hs.to_finset.convex_hull_eq lemma convex_hull_eq_union_convex_hull_finite_subsets (s : set E) : convex_hull s = ⋃ (t : finset E) (w : ↑t ⊆ s), convex_hull ↑t := begin refine subset.antisymm _ _, { rw [convex_hull_eq.{u}], rintros x ⟨ι, t, w, z, hw₀, hw₁, hz, rfl⟩, simp only [mem_Union], refine ⟨t.image z, _, _⟩, { rw [finset.coe_image, image_subset_iff], exact hz }, { apply t.center_mass_mem_convex_hull hw₀, { simp only [hw₁, zero_lt_one] }, { exact λ i hi, finset.mem_coe.2 (finset.mem_image_of_mem _ hi) } } }, { exact Union_subset (λ i, Union_subset convex_hull_mono), }, end lemma is_linear_map.convex_hull_image {f : E → F} (hf : is_linear_map ℝ f) (s : set E) : convex_hull (f '' s) = f '' convex_hull s := set.subset.antisymm (convex_hull_min (image_subset _ (subset_convex_hull s)) $ (convex_convex_hull s).is_linear_image hf) (image_subset_iff.2 $ convex_hull_min (image_subset_iff.1 $ subset_convex_hull _) ((convex_convex_hull _).is_linear_preimage hf)) lemma linear_map.convex_hull_image (f : E →ₗ[ℝ] F) (s : set E) : convex_hull (f '' s) = f '' convex_hull s := f.is_linear.convex_hull_image s end convex_hull /-! ### Simplex -/ section simplex variables (ι) [fintype ι] {f : ι → ℝ} /-- The standard simplex in the space of functions `ι → ℝ` is the set of vectors with non-negative coordinates with total sum `1`. -/ def std_simplex (ι : Type*) [fintype ι] : set (ι → ℝ) := {f | (∀ x, 0 ≤ f x) ∧ ∑ x, f x = 1} lemma std_simplex_eq_inter : std_simplex ι = (⋂ x, {f | 0 ≤ f x}) ∩ {f | ∑ x, f x = 1} := by { ext f, simp only [std_simplex, set.mem_inter_eq, set.mem_Inter, set.mem_set_of_eq] } lemma convex_std_simplex : convex (std_simplex ι) := begin refine λ f g hf hg a b ha hb hab, ⟨λ x, _, _⟩, { apply_rules [add_nonneg, mul_nonneg, hf.1, hg.1] }, { erw [finset.sum_add_distrib, ← finset.smul_sum, ← finset.smul_sum, hf.2, hg.2, smul_eq_mul, smul_eq_mul, mul_one, mul_one], exact hab } end variable {ι} lemma ite_eq_mem_std_simplex (i : ι) : (λ j, ite (i = j) (1:ℝ) 0) ∈ std_simplex ι := ⟨λ j, by simp only; split_ifs; norm_num, by rw [finset.sum_ite_eq, if_pos (finset.mem_univ _)]⟩ /-- `std_simplex ι` is the convex hull of the canonical basis in `ι → ℝ`. -/ lemma convex_hull_basis_eq_std_simplex : convex_hull (range $ λ(i j:ι), if i = j then (1:ℝ) else 0) = std_simplex ι := begin refine subset.antisymm (convex_hull_min _ (convex_std_simplex ι)) _, { rintros _ ⟨i, rfl⟩, exact ite_eq_mem_std_simplex i }, { rintros w ⟨hw₀, hw₁⟩, rw [pi_eq_sum_univ w, ← finset.univ.center_mass_eq_of_sum_1 _ hw₁], exact finset.univ.center_mass_mem_convex_hull (λ i hi, hw₀ i) (hw₁.symm ▸ zero_lt_one) (λ i hi, mem_range_self i) } end variable {ι} /-- The convex hull of a finite set is the image of the standard simplex in `s → ℝ` under the linear map sending each function `w` to `∑ x in s, w x • x`. Since we have no sums over finite sets, we use sum over `@finset.univ _ hs.fintype`. The map is defined in terms of operations on `(s → ℝ) →ₗ[ℝ] ℝ` so that later we will not need to prove that this map is linear. -/ lemma set.finite.convex_hull_eq_image {s : set E} (hs : finite s) : convex_hull s = by haveI := hs.fintype; exact (⇑(∑ x : s, (@linear_map.proj ℝ s _ (λ i, ℝ) _ _ x).smul_right x.1)) '' (std_simplex s) := begin rw [← convex_hull_basis_eq_std_simplex, ← linear_map.convex_hull_image, ← set.range_comp, (∘)], apply congr_arg, convert subtype.range_coe.symm, ext x, simp [linear_map.sum_apply, ite_smul, finset.filter_eq] end /-- All values of a function `f ∈ std_simplex ι` belong to `[0, 1]`. -/ lemma mem_Icc_of_mem_std_simplex (hf : f ∈ std_simplex ι) (x) : f x ∈ I := ⟨hf.1 x, hf.2 ▸ finset.single_le_sum (λ y hy, hf.1 y) (finset.mem_univ x)⟩ end simplex
3930231ef5f40abe216d564f49232b2457bae8d0
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/unit_interval.lean
098dfaf0505640b8455ba2c66be74885763c8de4
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
5,970
lean
/- Copyright (c) 2020 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Scott Morrison -/ import topology.instances.real import topology.algebra.field import data.set.intervals.proj_Icc import data.set.intervals.instances /-! # The unit interval, as a topological space Use `open_locale unit_interval` to turn on the notation `I := set.Icc (0 : ℝ) (1 : ℝ)`. We provide basic instances, as well as a custom tactic for discharging `0 ≤ ↑x`, `0 ≤ 1 - ↑x`, `↑x ≤ 1`, and `1 - ↑x ≤ 1` when `x : I`. -/ noncomputable theory open_locale classical topological_space filter open set int set.Icc /-! ### The unit interval -/ /-- The unit interval `[0,1]` in ℝ. -/ abbreviation unit_interval : set ℝ := set.Icc 0 1 localized "notation (name := unit_interval) `I` := unit_interval" in unit_interval namespace unit_interval lemma zero_mem : (0 : ℝ) ∈ I := ⟨le_rfl, zero_le_one⟩ lemma one_mem : (1 : ℝ) ∈ I := ⟨zero_le_one, le_rfl⟩ lemma mul_mem {x y : ℝ} (hx : x ∈ I) (hy : y ∈ I) : x * y ∈ I := ⟨mul_nonneg hx.1 hy.1, (mul_le_mul hx.2 hy.2 hy.1 zero_le_one).trans_eq $ one_mul 1⟩ lemma div_mem {x y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) (hxy : x ≤ y) : x / y ∈ I := ⟨div_nonneg hx hy, div_le_one_of_le hxy hy⟩ lemma fract_mem (x : ℝ) : fract x ∈ I := ⟨fract_nonneg _, (fract_lt_one _).le⟩ lemma mem_iff_one_sub_mem {t : ℝ} : t ∈ I ↔ 1 - t ∈ I := begin rw [mem_Icc, mem_Icc], split ; intro ; split ; linarith end instance has_zero : has_zero I := ⟨⟨0, zero_mem⟩⟩ instance has_one : has_one I := ⟨⟨1, by split ; norm_num⟩⟩ lemma coe_ne_zero {x : I} : (x : ℝ) ≠ 0 ↔ x ≠ 0 := not_iff_not.mpr coe_eq_zero lemma coe_ne_one {x : I} : (x : ℝ) ≠ 1 ↔ x ≠ 1 := not_iff_not.mpr coe_eq_one instance : nonempty I := ⟨0⟩ instance : has_mul I := ⟨λ x y, ⟨x * y, mul_mem x.2 y.2⟩⟩ -- todo: we could set up a `linear_ordered_comm_monoid_with_zero I` instance lemma mul_le_left {x y : I} : x * y ≤ x := subtype.coe_le_coe.mp $ (mul_le_mul_of_nonneg_left y.2.2 x.2.1).trans_eq $ mul_one x lemma mul_le_right {x y : I} : x * y ≤ y := subtype.coe_le_coe.mp $ (mul_le_mul_of_nonneg_right x.2.2 y.2.1).trans_eq $ one_mul y /-- Unit interval central symmetry. -/ def symm : I → I := λ t, ⟨1 - t, mem_iff_one_sub_mem.mp t.prop⟩ localized "notation (name := unit_interval.symm) `σ` := unit_interval.symm" in unit_interval @[simp] lemma symm_zero : σ 0 = 1 := subtype.ext $ by simp [symm] @[simp] lemma symm_one : σ 1 = 0 := subtype.ext $ by simp [symm] @[simp] lemma symm_symm (x : I) : σ (σ x) = x := subtype.ext $ by simp [symm] @[simp] lemma coe_symm_eq (x : I) : (σ x : ℝ) = 1 - x := rfl @[continuity] lemma continuous_symm : continuous σ := by continuity! instance : connected_space I := subtype.connected_space ⟨nonempty_Icc.mpr zero_le_one, is_preconnected_Icc⟩ /-- Verify there is an instance for `compact_space I`. -/ example : compact_space I := by apply_instance lemma nonneg (x : I) : 0 ≤ (x : ℝ) := x.2.1 lemma one_minus_nonneg (x : I) : 0 ≤ 1 - (x : ℝ) := by simpa using x.2.2 lemma le_one (x : I) : (x : ℝ) ≤ 1 := x.2.2 lemma one_minus_le_one (x : I) : 1 - (x : ℝ) ≤ 1 := by simpa using x.2.1 lemma add_pos {t : I} {x : ℝ} (hx : 0 < x) : 0 < (x + t : ℝ) := add_pos_of_pos_of_nonneg hx $ nonneg _ /-- like `unit_interval.nonneg`, but with the inequality in `I`. -/ lemma nonneg' {t : I} : 0 ≤ t := t.2.1 /-- like `unit_interval.le_one`, but with the inequality in `I`. -/ lemma le_one' {t : I} : t ≤ 1 := t.2.2 lemma mul_pos_mem_iff {a t : ℝ} (ha : 0 < a) : a * t ∈ I ↔ t ∈ set.Icc (0 : ℝ) (1/a) := begin split; rintros ⟨h₁, h₂⟩; split, { exact nonneg_of_mul_nonneg_right h₁ ha }, { rwa [le_div_iff ha, mul_comm] }, { exact mul_nonneg ha.le h₁ }, { rwa [le_div_iff ha, mul_comm] at h₂ } end lemma two_mul_sub_one_mem_iff {t : ℝ} : 2 * t - 1 ∈ I ↔ t ∈ set.Icc (1/2 : ℝ) 1 := by split; rintros ⟨h₁, h₂⟩; split; linarith end unit_interval @[simp] lemma proj_Icc_eq_zero {x : ℝ} : proj_Icc (0 : ℝ) 1 zero_le_one x = 0 ↔ x ≤ 0 := proj_Icc_eq_left zero_lt_one @[simp] lemma proj_Icc_eq_one {x : ℝ} : proj_Icc (0 : ℝ) 1 zero_le_one x = 1 ↔ 1 ≤ x := proj_Icc_eq_right zero_lt_one namespace tactic.interactive /-- A tactic that solves `0 ≤ ↑x`, `0 ≤ 1 - ↑x`, `↑x ≤ 1`, and `1 - ↑x ≤ 1` for `x : I`. -/ meta def unit_interval : tactic unit := `[apply unit_interval.nonneg] <|> `[apply unit_interval.one_minus_nonneg] <|> `[apply unit_interval.le_one] <|> `[apply unit_interval.one_minus_le_one] end tactic.interactive section variables {𝕜 : Type*} [linear_ordered_field 𝕜] [topological_space 𝕜] [topological_ring 𝕜] /-- The image of `[0,1]` under the homeomorphism `λ x, a * x + b` is `[b, a+b]`. -/ -- We only need the ordering on `𝕜` here to avoid talking about flipping the interval over. -- At the end of the day I only care about `ℝ`, so I'm hesitant to put work into generalizing. lemma affine_homeomorph_image_I (a b : 𝕜) (h : 0 < a) : affine_homeomorph a b h.ne.symm '' set.Icc 0 1 = set.Icc b (a + b) := by simp [h] /-- The affine homeomorphism from a nontrivial interval `[a,b]` to `[0,1]`. -/ def Icc_homeo_I (a b : 𝕜) (h : a < b) : set.Icc a b ≃ₜ set.Icc (0 : 𝕜) (1 : 𝕜) := begin let e := homeomorph.image (affine_homeomorph (b-a) a (sub_pos.mpr h).ne.symm) (set.Icc 0 1), refine (e.trans _).symm, apply homeomorph.set_congr, simp [sub_pos.mpr h], end @[simp] lemma Icc_homeo_I_apply_coe (a b : 𝕜) (h : a < b) (x : set.Icc a b) : ((Icc_homeo_I a b h) x : 𝕜) = (x - a) / (b - a) := rfl @[simp] lemma Icc_homeo_I_symm_apply_coe (a b : 𝕜) (h : a < b) (x : set.Icc (0 : 𝕜) (1 : 𝕜)) : ((Icc_homeo_I a b h).symm x : 𝕜) = (b - a) * x + a := rfl end
6d84bb5a6fa38a41737ba31cafad516f01bac769
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/stage0/src/Lean/Elab/Structure.lean
4e1fee149c79f3f0134a35b9fad381f4ec30c8d9
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
24,810
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Closure import Lean.Elab.Command import Lean.Elab.DeclModifiers import Lean.Elab.DeclUtil import Lean.Elab.Inductive namespace Lean namespace Elab namespace Command open Meta /- Recall that the `structure command syntax is ``` parser! (structureTk <|> classTk) >> declId >> many Term.bracketedBinder >> optional «extends» >> Term.optType >> " := " >> optional structCtor >> structFields ``` -/ structure StructCtorView := (ref : Syntax) (modifiers : Modifiers) (inferMod : Bool) -- true if `{}` is used in the constructor declaration (name : Name) (declName : Name) structure StructFieldView := (ref : Syntax) (modifiers : Modifiers) (binderInfo : BinderInfo) (inferMod : Bool) (declName : Name) (name : Name) (binders : Syntax) (type? : Option Syntax) (value? : Option Syntax) structure StructView := (ref : Syntax) (modifiers : Modifiers) (scopeLevelNames : List Name) -- All `universe` declarations in the current scope (allUserLevelNames : List Name) -- `scopeLevelNames` ++ explicit universe parameters provided in the `structure` command (isClass : Bool) (declName : Name) (scopeVars : Array Expr) -- All `variable` declaration in the current scope (params : Array Expr) -- Explicit parameters provided in the `structure` command (parents : Array Syntax) (type : Syntax) (ctor : StructCtorView) (fields : Array StructFieldView) inductive StructFieldKind | newField | fromParent | subobject structure StructFieldInfo := (name : Name) (declName : Name) -- Remark: this field value doesn't matter for fromParent fields. (fvar : Expr) (kind : StructFieldKind) (inferMod : Bool := false) (value? : Option Expr := none) instance StructFieldInfo.inhabited : Inhabited StructFieldInfo := ⟨{ name := arbitrary _, declName := arbitrary _, fvar := arbitrary _, kind := StructFieldKind.newField }⟩ def StructFieldInfo.isFromParent (info : StructFieldInfo) : Bool := match info.kind with | StructFieldKind.fromParent => true | _ => false def StructFieldInfo.isSubobject (info : StructFieldInfo) : Bool := match info.kind with | StructFieldKind.subobject => true | _ => false /- Auxiliary declaration for `mkProjections` -/ structure ProjectionInfo := (declName : Name) (inferMod : Bool) structure ElabStructResult := (decl : Declaration) (projInfos : List ProjectionInfo) (projInstances : List Name) -- projections (to parent classes) that must be marked as instances. (mctx : MetavarContext) (lctx : LocalContext) (localInsts : LocalInstances) (defaultAuxDecls : Array (Name × Expr × Expr)) private def defaultCtorName := `mk /- The structore constructor syntax is ``` parser! try (declModifiers >> ident >> optional inferMod >> " :: ") ``` -/ private def expandCtor (structStx : Syntax) (structModifiers : Modifiers) (structDeclName : Name) : CommandElabM StructCtorView := let optCtor := structStx.getArg 6; if optCtor.isNone then pure { ref := structStx, modifiers := {}, inferMod := false, name := defaultCtorName, declName := structDeclName ++ defaultCtorName } else let ctor := optCtor.getArg 0; withRef ctor do ctorModifiers ← elabModifiers (ctor.getArg 0); checkValidCtorModifier ctorModifiers; when (ctorModifiers.isPrivate && structModifiers.isPrivate) $ throwError "invalid 'private' constructor in a 'private' structure"; when (ctorModifiers.isProtected && structModifiers.isPrivate) $ throwError "invalid 'protected' constructor in a 'private' structure"; let inferMod := !(ctor.getArg 2).isNone; let name := ctor.getIdAt 1; let declName := structDeclName ++ name; declName ← applyVisibility ctorModifiers.visibility declName; pure { ref := ctor, name := name, modifiers := ctorModifiers, inferMod := inferMod, declName := declName } def checkValidFieldModifier (modifiers : Modifiers) : CommandElabM Unit := do when modifiers.isNoncomputable $ throwError "invalid use of 'noncomputable' in field declaration"; when modifiers.isPartial $ throwError "invalid use of 'partial' in field declaration"; when modifiers.isUnsafe $ throwError "invalid use of 'unsafe' in field declaration"; when (modifiers.attrs.size != 0) $ throwError "invalid use of attributes in field declaration"; when modifiers.isPrivate $ throwError "private fields are not supported yet"; pure () /- ``` def structExplicitBinder := parser! try (declModifiers >> "(") >> many1 ident >> optional inferMod >> optDeclSig >> optional Term.binderDefault >> ")" def structImplicitBinder := parser! try (declModifiers >> "{") >> many1 ident >> optional inferMod >> declSig >> "}" def structInstBinder := parser! try (declModifiers >> "[") >> many1 ident >> optional inferMod >> declSig >> "]" def structFields := parser! many (structExplicitBinder <|> structImplicitBinder <|> structInstBinder) ``` -/ private def expandFields (structStx : Syntax) (structModifiers : Modifiers) (structDeclName : Name) : CommandElabM (Array StructFieldView) := let fieldBinders := ((structStx.getArg 7).getArg 0).getArgs; fieldBinders.foldlM (fun (views : Array StructFieldView) fieldBinder => withRef fieldBinder do let k := fieldBinder.getKind; binfo ← if k == `Lean.Parser.Command.structExplicitBinder then pure BinderInfo.default else if k == `Lean.Parser.Command.structImplicitBinder then pure BinderInfo.implicit else if k == `Lean.Parser.Command.structInstBinder then pure BinderInfo.instImplicit else throwError "unexpected kind of structure field"; fieldModifiers ← elabModifiers (fieldBinder.getArg 0); checkValidFieldModifier fieldModifiers; when (fieldModifiers.isPrivate && structModifiers.isPrivate) $ throwError "invalid 'private' field in a 'private' structure"; when (fieldModifiers.isProtected && structModifiers.isPrivate) $ throwError "invalid 'protected' field in a 'private' structure"; let inferMod := !(fieldBinder.getArg 3).isNone; let (binders, type?) := if binfo == BinderInfo.default then expandOptDeclSig (fieldBinder.getArg 4) else let (binders, type) := expandDeclSig (fieldBinder.getArg 4); (binders, some type); let value? := if binfo != BinderInfo.default then none else let optBinderDefault := fieldBinder.getArg 5; if optBinderDefault.isNone then none else -- binderDefault := parser! " := " >> termParser some $ (optBinderDefault.getArg 0).getArg 1; let idents := (fieldBinder.getArg 2).getArgs; idents.foldlM (fun (views : Array StructFieldView) ident => withRef ident do let name := ident.getId; when (isInternalSubobjectFieldName name) $ throwError ("invalid field name '" ++ name ++ "', identifiers starting with '_' are reserved to the system"); let declName := structDeclName ++ name; declName ← applyVisibility fieldModifiers.visibility declName; pure $ views.push { ref := ident, modifiers := fieldModifiers, binderInfo := binfo, inferMod := inferMod, declName := declName, name := name, binders := binders, type? := type?, value? := value? }) views) #[] private def validStructType (type : Expr) : Bool := match type with | Expr.sort (Level.succ _ _) _ => true | _ => false private def checkParentIsStructure (parent : Expr) : TermElabM Name := match parent.getAppFn with | Expr.const c _ _ => do env ← getEnv; unless (isStructure env c) $ throwError $ "'" ++ toString c ++ "' is not a structure"; pure c | _ => throwError $ "expected structure" private def findFieldInfo? (infos : Array StructFieldInfo) (fieldName : Name) : Option StructFieldInfo := infos.find? fun info => info.name == fieldName private def containsFieldName (infos : Array StructFieldInfo) (fieldName : Name) : Bool := (findFieldInfo? infos fieldName).isSome private partial def processSubfields {α} (structDeclName : Name) (parentFVar : Expr) (parentStructName : Name) (subfieldNames : Array Name) : Nat → Array StructFieldInfo → (Array StructFieldInfo → TermElabM α) → TermElabM α | i, infos, k => if h : i < subfieldNames.size then do let subfieldName := subfieldNames.get ⟨i, h⟩; env ← getEnv; when (containsFieldName infos subfieldName) $ throwError ("field '" ++ subfieldName ++ "' from '" ++ parentStructName ++ "' has already been declared"); val ← mkProjection parentFVar subfieldName; type ← inferType val; withLetDecl subfieldName type val fun subfieldFVar => /- The following `declName` is only used for creating the `_default` auxiliary declaration name when its default value is overwritten in the structure. -/ let declName := structDeclName ++ subfieldName; let infos := infos.push { name := subfieldName, declName := declName, fvar := subfieldFVar, kind := StructFieldKind.fromParent }; processSubfields (i+1) infos k else k infos private partial def withParents {α} (view : StructView) : Nat → Array StructFieldInfo → (Array StructFieldInfo → TermElabM α) → TermElabM α | i, infos, k => if h : i < view.parents.size then let parentStx := view.parents.get ⟨i, h⟩; withRef parentStx do parent ← Term.elabType parentStx; parentName ← checkParentIsStructure parent; let toParentName := mkNameSimple $ "to" ++ parentName.eraseMacroScopes.getString!; -- erase macro scopes? when (containsFieldName infos toParentName) $ throwErrorAt parentStx ("field '" ++ toParentName ++ "' has already been declared"); env ← getEnv; let binfo := if view.isClass && isClass env parentName then BinderInfo.instImplicit else BinderInfo.default; withLocalDecl toParentName binfo parent $ fun parentFVar => let infos := infos.push { name := toParentName, declName := view.declName ++ toParentName, fvar := parentFVar, kind := StructFieldKind.subobject }; let subfieldNames := getStructureFieldsFlattened env parentName; processSubfields view.declName parentFVar parentName subfieldNames 0 infos fun infos => withParents (i+1) infos k else k infos private def elabFieldTypeValue (view : StructFieldView) (params : Array Expr) : TermElabM (Option Expr × Option Expr) := do match view.type? with | none => match view.value? with | none => pure (none, none) | some valStx => do value ← Term.elabTerm valStx none; value ← mkLambdaFVars params value; pure (none, value) | some typeStx => do type ← Term.elabType typeStx; match view.value? with | none => pure (type, none) | some valStx => do value ← Term.elabTermEnsuringType valStx type; type ← mkForallFVars params type; value ← mkLambdaFVars params value; pure (type, value) private partial def withFields {α} (views : Array StructFieldView) : Nat → Array StructFieldInfo → (Array StructFieldInfo → TermElabM α) → TermElabM α | i, infos, k => if h : i < views.size then do let view := views.get ⟨i, h⟩; withRef view.ref $ match findFieldInfo? infos view.name with | none => do (type?, value?) ← Term.elabBinders view.binders.getArgs $ fun params => elabFieldTypeValue view params; match type?, value? with | none, none => throwError "invalid field, type expected" | some type, _ => withLocalDecl view.name view.binderInfo type $ fun fieldFVar => let infos := infos.push { name := view.name, declName := view.declName, fvar := fieldFVar, value? := value?, kind := StructFieldKind.newField, inferMod := view.inferMod }; withFields (i+1) infos k | none, some value => do type ← inferType value; withLocalDecl view.name view.binderInfo type $ fun fieldFVar => let infos := infos.push { name := view.name, declName := view.declName, fvar := fieldFVar, kind := StructFieldKind.newField, inferMod := view.inferMod }; withFields (i+1) infos k | some info => match info.kind with | StructFieldKind.newField => throwError ("field '" ++ view.name ++ "' has already been declared") | StructFieldKind.fromParent => match view.value? with | none => throwError ("field '" ++ view.name ++ "' has been declared in parent structure") | some valStx => do when (!view.binders.getArgs.isEmpty || view.type?.isSome) $ throwErrorAt view.type?.get! ("omit field '" ++ view.name ++ "' type to set default value"); fvarType ← inferType info.fvar; value ← Term.elabTermEnsuringType valStx fvarType; let infos := infos.push { info with value? := value }; withFields (i+1) infos k | StructFieldKind.subobject => unreachable! else k infos private def getResultUniverse (type : Expr) : TermElabM Level := do type ← whnf type; match type with | Expr.sort u _ => pure u | _ => throwError "unexpected structure resulting type" private def collectUsed (params : Array Expr) (fieldInfos : Array StructFieldInfo) : StateRefT CollectFVars.State TermElabM Unit := do params.forM fun p => do { type ← inferType p; Term.collectUsedFVars type }; fieldInfos.forM fun info => do { fvarType ← inferType info.fvar; Term.collectUsedFVars fvarType; match info.value? with | none => pure () | some value => Term.collectUsedFVars value } private def removeUnused (scopeVars : Array Expr) (params : Array Expr) (fieldInfos : Array StructFieldInfo) : TermElabM (LocalContext × LocalInstances × Array Expr) := do (_, used) ← (collectUsed params fieldInfos).run {}; Term.removeUnused scopeVars used private def withUsed {α} (scopeVars : Array Expr) (params : Array Expr) (fieldInfos : Array StructFieldInfo) (k : Array Expr → TermElabM α) : TermElabM α := do (lctx, localInsts, vars) ← removeUnused scopeVars params fieldInfos; withLCtx lctx localInsts $ k vars private def levelMVarToParamFVar (fvar : Expr) : StateRefT Nat TermElabM Unit := do type ← inferType fvar; _ ← Term.levelMVarToParam' type; pure () private def levelMVarToParamFVars (fvars : Array Expr) : StateRefT Nat TermElabM Unit := fvars.forM levelMVarToParamFVar private def levelMVarToParamAux (scopeVars : Array Expr) (params : Array Expr) (fieldInfos : Array StructFieldInfo) : StateRefT Nat TermElabM (Array StructFieldInfo) := do levelMVarToParamFVars scopeVars; levelMVarToParamFVars params; fieldInfos.mapM fun info => do levelMVarToParamFVar info.fvar; match info.value? with | none => pure info | some value => do value ← Term.levelMVarToParam' value; pure { info with value? := value } private def levelMVarToParam (scopeVars : Array Expr) (params : Array Expr) (fieldInfos : Array StructFieldInfo) : TermElabM (Array StructFieldInfo) := (levelMVarToParamAux scopeVars params fieldInfos).run' 1 private partial def collectUniversesFromFields (r : Level) (rOffset : Nat) (fieldInfos : Array StructFieldInfo) : TermElabM (Array Level) := do fieldInfos.foldlM (fun (us : Array Level) (info : StructFieldInfo) => do type ← inferType info.fvar; u ← getLevel type; u ← instantiateLevelMVars u; match accLevelAtCtor u r rOffset us with | Except.error msg => throwError msg | Except.ok us => pure us) #[] private def updateResultingUniverse (fieldInfos : Array StructFieldInfo) (type : Expr) : TermElabM Expr := do r ← getResultUniverse type; let rOffset : Nat := r.getOffset; let r : Level := r.getLevelOffset; match r with | Level.mvar mvarId _ => do us ← collectUniversesFromFields r rOffset fieldInfos; let rNew := Level.mkNaryMax us.toList; assignLevelMVar mvarId rNew; instantiateMVars type | _ => throwError "failed to compute resulting universe level of structure, provide universe explicitly" private def collectLevelParamsInFVar (s : CollectLevelParams.State) (fvar : Expr) : TermElabM CollectLevelParams.State := do type ← inferType fvar; type ← instantiateMVars type; pure $ collectLevelParams s type private def collectLevelParamsInFVars (fvars : Array Expr) (s : CollectLevelParams.State) : TermElabM CollectLevelParams.State := fvars.foldlM collectLevelParamsInFVar s private def collectLevelParamsInStructure (scopeVars : Array Expr) (params : Array Expr) (fieldInfos : Array StructFieldInfo) : TermElabM (Array Name) := do s ← collectLevelParamsInFVars scopeVars {}; s ← collectLevelParamsInFVars params s; s ← fieldInfos.foldlM (fun (s : CollectLevelParams.State) info => collectLevelParamsInFVar s info.fvar) s; pure s.params private def addCtorFields (fieldInfos : Array StructFieldInfo) : Nat → Expr → TermElabM Expr | 0, type => pure type | i+1, type => do let info := fieldInfos.get! i; decl ← Term.getFVarLocalDecl! info.fvar; type ← instantiateMVars type; let type := type.abstract #[info.fvar]; match info.kind with | StructFieldKind.fromParent => let val := decl.value; addCtorFields i (type.instantiate1 val) | StructFieldKind.subobject => let n := mkInternalSubobjectFieldName $ decl.userName; addCtorFields i (mkForall n decl.binderInfo decl.type type) | StructFieldKind.newField => addCtorFields i (mkForall decl.userName decl.binderInfo decl.type type) private def mkCtor (view : StructView) (levelParams : List Name) (params : Array Expr) (fieldInfos : Array StructFieldInfo) : TermElabM Constructor := withRef view.ref do let type := mkAppN (mkConst view.declName (levelParams.map mkLevelParam)) params; type ← addCtorFields fieldInfos fieldInfos.size type; type ← mkForallFVars params type; type ← instantiateMVars type; let type := type.inferImplicit params.size !view.ctor.inferMod; pure { name := view.ctor.declName, type := type } @[extern "lean_mk_projections"] private constant mkProjections (env : Environment) (structName : @& Name) (projs : @& List ProjectionInfo) (isClass : Bool) : Except String Environment := arbitrary _ private def addProjections (structName : Name) (projs : List ProjectionInfo) (isClass : Bool) : TermElabM Unit := do env ← getEnv; match mkProjections env structName projs isClass with | Except.ok env => setEnv env | Except.error msg => throwError msg private def mkAuxConstructions (declName : Name) : TermElabM Unit := do env ← getEnv; let hasUnit := env.contains `PUnit; let hasEq := env.contains `Eq; let hasHEq := env.contains `HEq; modifyEnv fun env => mkRecOn env declName; when hasUnit $ modifyEnv fun env => mkCasesOn env declName; when (hasUnit && hasEq && hasHEq) $ modifyEnv fun env => mkNoConfusion env declName private def addDefaults (lctx : LocalContext) (defaultAuxDecls : Array (Name × Expr × Expr)) : TermElabM Unit := do localInsts ← getLocalInstances; withLCtx lctx localInsts do defaultAuxDecls.forM fun ⟨declName, type, value⟩ => do /- The identity function is used as "marker". -/ value ← mkId value; let zeta := true; -- expand `let-declarations` _ ← mkAuxDefinition declName type value zeta; modifyEnv fun env => setReducibilityStatus env declName ReducibilityStatus.reducible; pure () private def elabStructureView (view : StructView) : TermElabM Unit := do let numExplicitParams := view.params.size; type ← Term.elabType view.type; unless (validStructType type) $ throwErrorAt view.type "expected Type"; withRef view.ref do withParents view 0 #[] fun fieldInfos => withFields view.fields 0 fieldInfos fun fieldInfos => do Term.synthesizeSyntheticMVarsNoPostponing; u ← getResultUniverse type; inferLevel ← shouldInferResultUniverse u; withUsed view.scopeVars view.params fieldInfos $ fun scopeVars => do let numParams := scopeVars.size + numExplicitParams; fieldInfos ← levelMVarToParam scopeVars view.params fieldInfos; type ← if inferLevel then updateResultingUniverse fieldInfos type else pure type; usedLevelNames ← collectLevelParamsInStructure scopeVars view.params fieldInfos; match sortDeclLevelParams view.scopeLevelNames view.allUserLevelNames usedLevelNames with | Except.error msg => throwError msg | Except.ok levelParams => do let params := scopeVars ++ view.params; ctor ← mkCtor view levelParams params fieldInfos; type ← mkForallFVars params type; type ← instantiateMVars type; let indType := { name := view.declName, type := type, ctors := [ctor] : InductiveType }; let decl := Declaration.inductDecl levelParams params.size [indType] view.modifiers.isUnsafe; Term.ensureNoUnassignedMVars decl; addDecl decl; let projInfos := (fieldInfos.filter fun (info : StructFieldInfo) => !info.isFromParent).toList.map fun (info : StructFieldInfo) => { declName := info.declName, inferMod := info.inferMod : ProjectionInfo }; addProjections view.declName projInfos view.isClass; mkAuxConstructions view.declName; instParents ← fieldInfos.filterM fun info => do { decl ← Term.getFVarLocalDecl! info.fvar; pure (info.isSubobject && decl.binderInfo.isInstImplicit) }; let projInstances := instParents.toList.map fun info => info.declName; applyAttributes view.declName view.modifiers.attrs AttributeApplicationTime.afterTypeChecking; projInstances.forM addGlobalInstance; lctx ← getLCtx; let fieldsWithDefault := fieldInfos.filter fun info => info.value?.isSome; defaultAuxDecls ← fieldsWithDefault.mapM fun info => do { type ← inferType info.fvar; pure (info.declName ++ `_default, type, info.value?.get!) }; /- The `lctx` and `defaultAuxDecls` are used to create the auxiliary `_default` declarations The parameters `params` for these definitions must be marked as implicit, and all others as explicit. -/ let lctx := params.foldl (fun (lctx : LocalContext) (p : Expr) => lctx.updateBinderInfo p.fvarId! BinderInfo.implicit) lctx; let lctx := fieldInfos.foldl (fun (lctx : LocalContext) (info : StructFieldInfo) => if info.isFromParent then lctx -- `fromParent` fields are elaborated as let-decls, and are zeta-expanded when creating `_default`. else lctx.updateBinderInfo info.fvar.fvarId! BinderInfo.default) lctx; addDefaults lctx defaultAuxDecls /- parser! (structureTk <|> classTk) >> declId >> many Term.bracketedBinder >> optional «extends» >> Term.optType >> " := " >> optional structCtor >> structFields where def «extends» := parser! " extends " >> sepBy1 termParser ", " def typeSpec := parser! " : " >> termParser def optType : Parser := optional typeSpec def structFields := parser! many (structExplicitBinder <|> structImplicitBinder <|> structInstBinder) def structCtor := parser! try (declModifiers >> ident >> optional inferMod >> " :: ") -/ def elabStructure (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit := do checkValidInductiveModifier modifiers; let isClass := (stx.getArg 0).getKind == `Lean.Parser.Command.classTk; let modifiers := if isClass then modifiers.addAttribute { name := `class } else modifiers; let declId := stx.getArg 1; let params := (stx.getArg 2).getArgs; let exts := stx.getArg 3; let parents := if exts.isNone then #[] else ((exts.getArg 0).getArg 1).getArgs.getSepElems; let optType := stx.getArg 4; type ← if optType.isNone then `(Type _) else pure $ (optType.getArg 0).getArg 1; scopeLevelNames ← getLevelNames; ⟨name, declName, allUserLevelNames⟩ ← expandDeclId declId modifiers; ctor ← expandCtor stx modifiers declName; fields ← expandFields stx modifiers declName; runTermElabM declName $ fun scopeVars => Term.withLevelNames allUserLevelNames $ Term.elabBinders params fun params => elabStructureView { ref := stx, modifiers := modifiers, scopeLevelNames := scopeLevelNames, allUserLevelNames := allUserLevelNames, declName := declName, isClass := isClass, scopeVars := scopeVars, params := params, parents := parents, type := type, ctor := ctor, fields := fields } end Command end Elab end Lean
6f85693e1fd267828bc9148fa2afcfcde56269e9
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/ring_theory/jacobson.lean
140e627a5bfd23fcbfaa003c73da90001a306ed0
[ "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
33,183
lean
/- Copyright (c) 2020 Devon Tuma. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Devon Tuma -/ import data.mv_polynomial import ring_theory.ideal.over import ring_theory.jacobson_ideal import ring_theory.localization /-! # Jacobson Rings The following conditions are equivalent for a ring `R`: 1. Every radical ideal `I` is equal to its Jacobson radical 2. Every radical ideal `I` can be written as an intersection of maximal ideals 3. Every prime ideal `I` is equal to its Jacobson radical Any ring satisfying any of these equivalent conditions is said to be Jacobson. Some particular examples of Jacobson rings are also proven. `is_jacobson_quotient` says that the quotient of a Jacobson ring is Jacobson. `is_jacobson_localization` says the localization of a Jacobson ring to a single element is Jacobson. `is_jacobson_polynomial_iff_is_jacobson` says polynomials over a Jacobson ring form a Jacobson ring. ## Main definitions Let `R` be a commutative ring. Jacobson Rings are defined using the first of the above conditions * `is_jacobson R` is the proposition that `R` is a Jacobson ring. It is a class, implemented as the predicate that for any ideal, `I.radical = I` implies `I.jacobson = I`. ## Main statements * `is_jacobson_iff_prime_eq` is the equivalence between conditions 1 and 3 above. * `is_jacobson_iff_Inf_maximal` is the equivalence between conditions 1 and 2 above. * `is_jacobson_of_surjective` says that if `R` is a Jacobson ring and `f : R →+* S` is surjective, then `S` is also a Jacobson ring * `is_jacobson_mv_polynomial` says that multi-variate polynomials over a Jacobson ring are Jacobson. ## Tags Jacobson, Jacobson Ring -/ namespace ideal open polynomial section is_jacobson variables {R S : Type*} [comm_ring R] [comm_ring S] {I : ideal R} /-- A ring is a Jacobson ring if for every radical ideal `I`, the Jacobson radical of `I` is equal to `I`. See `is_jacobson_iff_prime_eq` and `is_jacobson_iff_Inf_maximal` for equivalent definitions. -/ class is_jacobson (R : Type*) [comm_ring R] : Prop := (out' : ∀ (I : ideal R), I.radical = I → I.jacobson = I) theorem is_jacobson_iff {R} [comm_ring R] : is_jacobson R ↔ ∀ (I : ideal R), I.radical = I → I.jacobson = I := ⟨λ h, h.1, λ h, ⟨h⟩⟩ theorem is_jacobson.out {R} [comm_ring R] : is_jacobson R → ∀ {I : ideal R}, I.radical = I → I.jacobson = I := is_jacobson_iff.1 /-- A ring is a Jacobson ring if and only if for all prime ideals `P`, the Jacobson radical of `P` is equal to `P`. -/ lemma is_jacobson_iff_prime_eq : is_jacobson R ↔ ∀ P : ideal R, is_prime P → P.jacobson = P := begin refine is_jacobson_iff.trans ⟨λ h I hI, h I (is_prime.radical hI), _⟩, refine λ h I hI, le_antisymm (λ x hx, _) (λ x hx, mem_Inf.mpr (λ _ hJ, hJ.left hx)), rw [← hI, radical_eq_Inf I, mem_Inf], intros P hP, rw set.mem_set_of_eq at hP, erw mem_Inf at hx, erw [← h P hP.right, mem_Inf], exact λ J hJ, hx ⟨le_trans hP.left hJ.left, hJ.right⟩ end /-- A ring `R` is Jacobson if and only if for every prime ideal `I`, `I` can be written as the infimum of some collection of maximal ideals. Allowing ⊤ in the set `M` of maximal ideals is equivalent, but makes some proofs cleaner. -/ lemma is_jacobson_iff_Inf_maximal : is_jacobson R ↔ ∀ {I : ideal R}, I.is_prime → ∃ M : set (ideal R), (∀ J ∈ M, is_maximal J ∨ J = ⊤) ∧ I = Inf M := ⟨λ H I h, eq_jacobson_iff_Inf_maximal.1 (H.out (is_prime.radical h)), λ H, is_jacobson_iff_prime_eq.2 (λ P hP, eq_jacobson_iff_Inf_maximal.2 (H hP))⟩ lemma is_jacobson_iff_Inf_maximal' : is_jacobson R ↔ ∀ {I : ideal R}, I.is_prime → ∃ M : set (ideal R), (∀ (J ∈ M) (K : ideal R), J < K → K = ⊤) ∧ I = Inf M := ⟨λ H I h, eq_jacobson_iff_Inf_maximal'.1 (H.out (is_prime.radical h)), λ H, is_jacobson_iff_prime_eq.2 (λ P hP, eq_jacobson_iff_Inf_maximal'.2 (H hP))⟩ lemma radical_eq_jacobson [H : is_jacobson R] (I : ideal R) : I.radical = I.jacobson := le_antisymm (le_Inf (λ J ⟨hJ, hJ_max⟩, (is_prime.radical_le_iff hJ_max.is_prime).mpr hJ)) ((H.out (radical_idem I)) ▸ (jacobson_mono le_radical)) /-- Fields have only two ideals, and the condition holds for both of them. -/ @[priority 100] instance is_jacobson_field {K : Type*} [field K] : is_jacobson K := ⟨λ I hI, or.rec_on (eq_bot_or_top I) (λ h, le_antisymm (Inf_le ⟨le_of_eq rfl, (eq.symm h) ▸ bot_is_maximal⟩) ((eq.symm h) ▸ bot_le)) (λ h, by rw [h, jacobson_eq_top_iff])⟩ theorem is_jacobson_of_surjective [H : is_jacobson R] : (∃ (f : R →+* S), function.surjective f) → is_jacobson S := begin rintros ⟨f, hf⟩, rw is_jacobson_iff_Inf_maximal, intros p hp, use map f '' {J : ideal R | comap f p ≤ J ∧ J.is_maximal }, use λ j ⟨J, hJ, hmap⟩, hmap ▸ or.symm (map_eq_top_or_is_maximal_of_surjective f hf hJ.right), have : p = map f ((comap f p).jacobson), from (is_jacobson.out' (comap f p) (by rw [← comap_radical, is_prime.radical hp])).symm ▸ (map_comap_of_surjective f hf p).symm, exact eq.trans this (map_Inf hf (λ J ⟨hJ, _⟩, le_trans (ideal.ker_le_comap f) hJ)), end @[priority 100] instance is_jacobson_quotient [is_jacobson R] : is_jacobson (quotient I) := is_jacobson_of_surjective ⟨quotient.mk I, (by rintro ⟨x⟩; use x; refl)⟩ lemma is_jacobson_iso (e : R ≃+* S) : is_jacobson R ↔ is_jacobson S := ⟨λ h, @is_jacobson_of_surjective _ _ _ _ h ⟨(e : R →+* S), e.surjective⟩, λ h, @is_jacobson_of_surjective _ _ _ _ h ⟨(e.symm : S →+* R), e.symm.surjective⟩⟩ lemma is_jacobson_of_is_integral [algebra R S] (hRS : algebra.is_integral R S) (hR : is_jacobson R) : is_jacobson S := begin rw is_jacobson_iff_prime_eq, introsI P hP, by_cases hP_top : comap (algebra_map R S) P = ⊤, { simp [comap_eq_top_iff.1 hP_top] }, { haveI : nontrivial (comap (algebra_map R S) P).quotient := quotient.nontrivial hP_top, rw jacobson_eq_iff_jacobson_quotient_eq_bot, refine eq_bot_of_comap_eq_bot (is_integral_quotient_of_is_integral hRS) _, rw [eq_bot_iff, ← jacobson_eq_iff_jacobson_quotient_eq_bot.1 ((is_jacobson_iff_prime_eq.1 hR) (comap (algebra_map R S) P) (comap_is_prime _ _)), comap_jacobson], refine Inf_le_Inf (λ J hJ, _), simp only [true_and, set.mem_image, bot_le, set.mem_set_of_eq], haveI : J.is_maximal := by simpa using hJ, exact exists_ideal_over_maximal_of_is_integral (is_integral_quotient_of_is_integral hRS) J (comap_bot_le_of_injective _ algebra_map_quotient_injective) } end lemma is_jacobson_of_is_integral' (f : R →+* S) (hf : f.is_integral) (hR : is_jacobson R) : is_jacobson S := @is_jacobson_of_is_integral _ _ _ _ f.to_algebra hf hR end is_jacobson section localization open localization_map submonoid variables {R S : Type*} [comm_ring R] [comm_ring S] {I : ideal R} variables {y : R} (f : away_map y S) lemma disjoint_powers_iff_not_mem (hI : I.radical = I) : disjoint ((submonoid.powers y) : set R) ↑I ↔ y ∉ I.1 := begin refine ⟨λ h, set.disjoint_left.1 h (mem_powers _), λ h, (disjoint_iff).mpr (eq_bot_iff.mpr _)⟩, rintros x ⟨⟨n, rfl⟩, hx'⟩, rw [← hI] at hx', exact absurd (hI ▸ mem_radical_of_pow_mem hx' : y ∈ I.carrier) h end /-- If `R` is a Jacobson ring, then maximal ideals in the localization at `y` correspond to maximal ideals in the original ring `R` that don't contain `y`. This lemma gives the correspondence in the particular case of an ideal and its comap. See `le_rel_iso_of_maximal` for the more general relation isomorphism -/ lemma is_maximal_iff_is_maximal_disjoint [H : is_jacobson R] (J : ideal S) : J.is_maximal ↔ (comap f.to_map J).is_maximal ∧ y ∉ ideal.comap f.to_map J := begin split, { refine λ h, ⟨_, λ hy, h.ne_top (ideal.eq_top_of_is_unit_mem _ hy (map_units f ⟨y, submonoid.mem_powers _⟩))⟩, have hJ : J.is_prime := is_maximal.is_prime h, rw is_prime_iff_is_prime_disjoint f at hJ, have : y ∉ (comap f.to_map J).1 := set.disjoint_left.1 hJ.right (submonoid.mem_powers _), erw [← H.out (is_prime.radical hJ.left), mem_Inf] at this, push_neg at this, rcases this with ⟨I, hI, hI'⟩, convert hI.right, by_cases hJ : J = map f.to_map I, { rw [hJ, comap_map_of_is_prime_disjoint f I (is_maximal.is_prime hI.right)], rwa disjoint_powers_iff_not_mem (is_maximal.is_prime hI.right).radical}, { have hI_p : (map f.to_map I).is_prime, { refine is_prime_of_is_prime_disjoint f I hI.right.is_prime _, rwa disjoint_powers_iff_not_mem (is_maximal.is_prime hI.right).radical }, have : J ≤ map f.to_map I := (map_comap f J) ▸ (map_mono hI.left), exact absurd (h.1.2 _ (lt_of_le_of_ne this hJ)) hI_p.1 } }, { refine λ h, ⟨⟨λ hJ, h.1.ne_top (eq_top_iff.2 _), λ I hI, _⟩⟩, { rwa [eq_top_iff, ← f.order_embedding.le_iff_le] at hJ }, { have := congr_arg (map f.to_map) (h.1.1.2 _ ⟨comap_mono (le_of_lt hI), _⟩), rwa [map_comap f I, map_top f.to_map] at this, refine λ hI', hI.right _, rw [← map_comap f I, ← map_comap f J], exact map_mono hI' } } end /-- If `R` is a Jacobson ring, then maximal ideals in the localization at `y` correspond to maximal ideals in the original ring `R` that don't contain `y`. This lemma gives the correspondence in the particular case of an ideal and its map. See `le_rel_iso_of_maximal` for the more general statement, and the reverse of this implication -/ lemma is_maximal_of_is_maximal_disjoint [is_jacobson R] (I : ideal R) (hI : I.is_maximal) (hy : y ∉ I) : (map f.to_map I).is_maximal := begin rw [is_maximal_iff_is_maximal_disjoint f, comap_map_of_is_prime_disjoint f I (is_maximal.is_prime hI) ((disjoint_powers_iff_not_mem (is_maximal.is_prime hI).radical).2 hy)], exact ⟨hI, hy⟩ end /-- If `R` is a Jacobson ring, then maximal ideals in the localization at `y` correspond to maximal ideals in the original ring `R` that don't contain `y` -/ def order_iso_of_maximal [is_jacobson R] : {p : ideal S // p.is_maximal} ≃o {p : ideal R // p.is_maximal ∧ y ∉ p} := { to_fun := λ p, ⟨ideal.comap f.to_map p.1, (is_maximal_iff_is_maximal_disjoint f p.1).1 p.2⟩, inv_fun := λ p, ⟨ideal.map f.to_map p.1, is_maximal_of_is_maximal_disjoint f p.1 p.2.1 p.2.2⟩, left_inv := λ J, subtype.eq (map_comap f J), right_inv := λ I, subtype.eq (comap_map_of_is_prime_disjoint f I.1 (is_maximal.is_prime I.2.1) ((disjoint_powers_iff_not_mem I.2.1.is_prime.radical).2 I.2.2)), map_rel_iff' := λ I I', ⟨λ h, (show I.val ≤ I'.val, from (map_comap f I.val) ▸ (map_comap f I'.val) ▸ (ideal.map_mono h)), λ h x hx, h hx⟩ } /-- If `S` is the localization of the Jacobson ring `R` at the submonoid generated by `y : R`, then `S` is Jacobson. -/ lemma is_jacobson_localization [H : is_jacobson R] (f : away_map y S) : is_jacobson S := begin rw is_jacobson_iff_prime_eq, refine λ P' hP', le_antisymm _ le_jacobson, obtain ⟨hP', hPM⟩ := (localization_map.is_prime_iff_is_prime_disjoint f P').mp hP', have hP := H.out (is_prime.radical hP'), refine le_trans (le_trans (le_of_eq (localization_map.map_comap f P'.jacobson).symm) (map_mono _)) (le_of_eq (localization_map.map_comap f P')), have : Inf { I : ideal R | comap f.to_map P' ≤ I ∧ I.is_maximal ∧ y ∉ I } ≤ comap f.to_map P', { intros x hx, have hxy : x * y ∈ (comap f.to_map P').jacobson, { rw [ideal.jacobson, mem_Inf], intros J hJ, by_cases y ∈ J, { exact J.smul_mem x h }, { exact (mul_comm y x) ▸ J.smul_mem y ((mem_Inf.1 hx) ⟨hJ.left, ⟨hJ.right, h⟩⟩) } }, rw hP at hxy, cases hP'.mem_or_mem hxy with hxy hxy, { exact hxy }, { exact (hPM ⟨submonoid.mem_powers _, hxy⟩).elim } }, refine le_trans _ this, rw [ideal.jacobson, comap_Inf', Inf_eq_infi], refine infi_le_infi_of_subset (λ I hI, ⟨map f.to_map I, ⟨_, _⟩⟩), { exact ⟨le_trans (le_of_eq ((localization_map.map_comap f P').symm)) (map_mono hI.1), is_maximal_of_is_maximal_disjoint f _ hI.2.1 hI.2.2⟩ }, { exact localization_map.comap_map_of_is_prime_disjoint f I (is_maximal.is_prime hI.2.1) ((disjoint_powers_iff_not_mem hI.2.1.is_prime.radical).2 hI.2.2) } end end localization namespace polynomial open polynomial section comm_ring variables {R S : Type*} [comm_ring R] [integral_domain S] variables {Rₘ Sₘ : Type*} [comm_ring Rₘ] [comm_ring Sₘ] /-- If `I` is a prime ideal of `polynomial R` and `pX ∈ I` is a non-constant polynomial, then the map `R →+* R[x]/I` descends to an integral map when localizing at `pX.leading_coeff`. In particular `X` is integral because it satisfies `pX`, and constants are trivially integral, so integrality of the entire extension follows by closure under addition and multiplication. -/ lemma is_integral_localization_map_polynomial_quotient (P : ideal (polynomial R)) [P.is_prime] (pX : polynomial R) (hpX : pX ∈ P) (ϕ : localization_map (submonoid.powers (pX.map (quotient.mk (P.comap C))).leading_coeff) Rₘ) (ϕ' : localization_map ((submonoid.powers (pX.map (quotient.mk (P.comap C))).leading_coeff).map (quotient_map P C le_rfl) : submonoid P.quotient) Sₘ) : (ϕ.map ((submonoid.powers (pX.map (quotient.mk (P.comap C))).leading_coeff).mem_map_of_mem (quotient_map P C le_rfl : (P.comap C : ideal R).quotient →* P.quotient)) ϕ').is_integral := begin let P' : ideal R := P.comap C, let M : submonoid P'.quotient := submonoid.powers (pX.map (quotient.mk (P.comap C))).leading_coeff, let φ : P'.quotient →+* P.quotient := quotient_map P C le_rfl, let φ' := (ϕ.map (M.mem_map_of_mem (φ : P'.quotient →* P.quotient)) ϕ'), have hφ' : φ.comp (quotient.mk P') = (quotient.mk P).comp C := rfl, intro p, obtain ⟨⟨p', ⟨q, hq⟩⟩, hp⟩ := ϕ'.surj p, suffices : φ'.is_integral_elem (ϕ'.to_map p'), { obtain ⟨q', hq', rfl⟩ := hq, obtain ⟨q'', hq''⟩ := is_unit_iff_exists_inv'.1 (ϕ.map_units ⟨q', hq'⟩), refine φ'.is_integral_of_is_integral_mul_unit p (ϕ'.to_map (φ q')) q'' _ (hp.symm ▸ this), convert trans (trans (φ'.map_mul _ _).symm (congr_arg φ' hq'')) φ'.map_one using 2, rw [← φ'.comp_apply, localization_map.map_comp, ϕ'.to_map.comp_apply, subtype.coe_mk] }, refine is_integral_of_mem_closure'' ((ϕ'.to_map.comp (quotient.mk P)) '' (insert X {p | p.degree ≤ 0})) _ _ _, { rintros x ⟨p, hp, rfl⟩, refine hp.rec_on (λ hy, _) (λ hy, _), { refine hy.symm ▸ (φ.is_integral_elem_localization_at_leading_coeff ((quotient.mk P) X) (pX.map (quotient.mk P')) _ M ⟨1, pow_one _⟩ _ _), rwa [eval₂_map, hφ', ← hom_eval₂, quotient.eq_zero_iff_mem, eval₂_C_X] }, { rw [set.mem_set_of_eq, degree_le_zero_iff] at hy, refine hy.symm ▸ ⟨X - C (ϕ.to_map ((quotient.mk P') (p.coeff 0))), monic_X_sub_C _, _⟩, simp only [eval₂_sub, eval₂_C, eval₂_X], rw [sub_eq_zero, ← φ'.comp_apply, localization_map.map_comp, ring_hom.comp_apply], refl } }, { obtain ⟨p, rfl⟩ := quotient.mk_surjective p', refine polynomial.induction_on p (λ r, subring.subset_closure $ set.mem_image_of_mem _ (or.inr degree_C_le)) (λ _ _ h1 h2, _) (λ n _ hr, _), { convert subring.add_mem _ h1 h2, rw [ring_hom.map_add, ring_hom.map_add] }, { rw [pow_succ X n, mul_comm X, ← mul_assoc, ring_hom.map_mul, ϕ'.to_map.map_mul], exact subring.mul_mem _ hr (subring.subset_closure (set.mem_image_of_mem _ (or.inl rfl))) } }, end /-- If `f : R → S` descends to an integral map in the localization at `x`, and `R` is a Jacobson ring, then the intersection of all maximal ideals in `S` is trivial -/ lemma jacobson_bot_of_integral_localization {R : Type*} [integral_domain R] [is_jacobson R] (φ : R →+* S) (hφ : function.injective φ) (x : R) (hx : x ≠ 0) (ϕ : localization_map (submonoid.powers x) Rₘ) (ϕ' : localization_map ((submonoid.powers x).map φ : submonoid S) Sₘ) (hφ' : (ϕ.map ((submonoid.powers x).mem_map_of_mem (φ : R →* S)) ϕ').is_integral) : (⊥ : ideal S).jacobson = ⊥ := begin have hM : ((submonoid.powers x).map φ : submonoid S) ≤ non_zero_divisors S := map_le_non_zero_divisors_of_injective hφ (powers_le_non_zero_divisors_of_domain hx), letI : integral_domain Sₘ := localization_map.integral_domain_of_le_non_zero_divisors ϕ' hM, let φ' : Rₘ →+* Sₘ := ϕ.map ((submonoid.powers x).mem_map_of_mem (φ : R →* S)) ϕ', suffices : ∀ I : ideal Sₘ, I.is_maximal → (I.comap ϕ'.to_map).is_maximal, { have hϕ' : comap ϕ'.to_map ⊥ = ⊥, { simpa [ring_hom.injective_iff_ker_eq_bot, ring_hom.ker_eq_comap_bot] using ϕ'.injective hM }, refine eq_bot_iff.2 (le_trans _ (le_of_eq hϕ')), have hSₘ : is_jacobson Sₘ := is_jacobson_of_is_integral' φ' hφ' (is_jacobson_localization ϕ), rw [← hSₘ.out radical_bot_of_integral_domain, comap_jacobson], exact Inf_le_Inf (λ j hj, ⟨bot_le, let ⟨J, hJ⟩ := hj in hJ.2 ▸ this J hJ.1.2⟩) }, introsI I hI, -- Remainder of the proof is pulling and pushing ideals around the square and the quotient square haveI : (I.comap ϕ'.to_map).is_prime := comap_is_prime ϕ'.to_map I, haveI : (I.comap φ').is_prime := comap_is_prime φ' I, haveI : (⊥ : ideal (I.comap ϕ'.to_map).quotient).is_prime := bot_prime, have hcomm: φ'.comp ϕ.to_map = ϕ'.to_map.comp φ := ϕ.map_comp _, let f := quotient_map (I.comap ϕ'.to_map) φ le_rfl, let g := quotient_map I ϕ'.to_map le_rfl, have := is_maximal_comap_of_is_integral_of_is_maximal' φ' hφ' I (by convert hI; casesI _inst_4; refl), have := ((is_maximal_iff_is_maximal_disjoint ϕ _).1 this).left, have : ((I.comap ϕ'.to_map).comap φ).is_maximal, { rwa [comap_comap, hcomm, ← comap_comap] at this }, rw ← bot_quotient_is_maximal_iff at this ⊢, refine is_maximal_of_is_integral_of_is_maximal_comap' f _ ⊥ ((eq_bot_iff.2 (comap_bot_le_of_injective f quotient_map_injective)).symm ▸ this), exact f.is_integral_tower_bot_of_is_integral g quotient_map_injective ((comp_quotient_map_eq_of_comp_eq hcomm I).symm ▸ (ring_hom.is_integral_trans _ _ (ring_hom.is_integral_of_surjective _ (localization_map.surjective_quotient_map_of_maximal_of_localization (by rwa [comap_comap, hcomm, ← bot_quotient_is_maximal_iff]))) (ring_hom.is_integral_quotient_of_is_integral _ hφ'))), end /-- Used to bootstrap the proof of `is_jacobson_polynomial_iff_is_jacobson`. That theorem is more general and should be used instead of this one. -/ private lemma is_jacobson_polynomial_of_domain (R : Type*) [integral_domain R] [hR : is_jacobson R] (P : ideal (polynomial R)) [is_prime P] (hP : ∀ (x : R), C x ∈ P → x = 0) : P.jacobson = P := begin by_cases Pb : (P = ⊥), { exact Pb.symm ▸ jacobson_bot_polynomial_of_jacobson_bot (hR.out radical_bot_of_integral_domain) }, { refine jacobson_eq_iff_jacobson_quotient_eq_bot.mpr _, haveI : (P.comap (C : R →+* polynomial R)).is_prime := comap_is_prime C P, obtain ⟨p, pP, p0⟩ := exists_nonzero_mem_of_ne_bot Pb hP, refine jacobson_bot_of_integral_localization (quotient_map P C le_rfl) quotient_map_injective _ _ (localization.of (submonoid.powers (p.map (quotient.mk (P.comap C))).leading_coeff)) (localization.of _) (by apply (is_integral_localization_map_polynomial_quotient P _ pP _ _)), rwa [ne.def, leading_coeff_eq_zero] } end lemma is_jacobson_polynomial_of_is_jacobson (hR : is_jacobson R) : is_jacobson (polynomial R) := begin refine is_jacobson_iff_prime_eq.mpr (λ I, _), introI hI, let R' : subring I.quotient := ((quotient.mk I).comp C).range, let i : R →+* R' := ((quotient.mk I).comp C).range_restrict, have hi : function.surjective (i : R → R') := ((quotient.mk I).comp C).range_restrict_surjective, have hi' : (polynomial.map_ring_hom i : polynomial R →+* polynomial R').ker ≤ I, { refine λ f hf, polynomial_mem_ideal_of_coeff_mem_ideal I f (λ n, _), replace hf := congr_arg (λ (g : polynomial (((quotient.mk I).comp C).range)), g.coeff n) hf, change (polynomial.map ((quotient.mk I).comp C).range_restrict f).coeff n = 0 at hf, rw [coeff_map, subtype.ext_iff] at hf, rwa [mem_comap, ← quotient.eq_zero_iff_mem, ← ring_hom.comp_apply] }, haveI : (ideal.map (map_ring_hom i) I).is_prime := map_is_prime_of_surjective (map_surjective i hi) hi', suffices : (I.map (polynomial.map_ring_hom i)).jacobson = (I.map (polynomial.map_ring_hom i)), { replace this := congr_arg (comap (polynomial.map_ring_hom i)) this, rw [← map_jacobson_of_surjective _ hi', comap_map_of_surjective _ _, comap_map_of_surjective _ _] at this, refine le_antisymm (le_trans (le_sup_left_of_le le_rfl) (le_trans (le_of_eq this) (sup_le le_rfl hi'))) le_jacobson, all_goals {exact polynomial.map_surjective i hi} }, exact @is_jacobson_polynomial_of_domain R' _ (is_jacobson_of_surjective ⟨i, hi⟩) (map (map_ring_hom i) I) _ (eq_zero_of_polynomial_mem_map_range I), end theorem is_jacobson_polynomial_iff_is_jacobson : is_jacobson (polynomial R) ↔ is_jacobson R := begin refine ⟨_, is_jacobson_polynomial_of_is_jacobson⟩, introI H, exact is_jacobson_of_surjective ⟨eval₂_ring_hom (ring_hom.id _) 1, λ x, ⟨C x, by simp only [coe_eval₂_ring_hom, ring_hom.id_apply, eval₂_C]⟩⟩, end instance [is_jacobson R] : is_jacobson (polynomial R) := is_jacobson_polynomial_iff_is_jacobson.mpr ‹is_jacobson R› end comm_ring section integral_domain variables {R : Type*} [integral_domain R] [is_jacobson R] variables (P : ideal (polynomial R)) [hP : P.is_maximal] include P hP lemma is_maximal_comap_C_of_is_maximal (hP' : ∀ (x : R), C x ∈ P → x = 0) : is_maximal (comap C P : ideal R) := begin haveI hp'_prime : (P.comap C : ideal R).is_prime := comap_is_prime C P, obtain ⟨m, hm⟩ := submodule.nonzero_mem_of_bot_lt (bot_lt_of_maximal P polynomial_not_is_field), have : (m : polynomial R) ≠ 0, rwa [ne.def, submodule.coe_eq_zero], let φ : (P.comap C : ideal R).quotient →+* P.quotient := quotient_map P C le_rfl, let M : submonoid (P.comap C : ideal R).quotient := submonoid.powers ((m : polynomial R).map (quotient.mk (P.comap C : ideal R))).leading_coeff, rw ← bot_quotient_is_maximal_iff at hP ⊢, have hp0 : ((m : polynomial R).map (quotient.mk (P.comap C : ideal R))).leading_coeff ≠ 0 := λ hp0', this $ map_injective (quotient.mk (P.comap C : ideal R)) ((quotient.mk (P.comap C : ideal R)).injective_iff.2 (λ x hx, by rwa [quotient.eq_zero_iff_mem, (by rwa eq_bot_iff : (P.comap C : ideal R) = ⊥)] at hx)) (by simpa only [leading_coeff_eq_zero, map_zero] using hp0'), let ϕ : localization_map M (localization M) := localization.of M, have hM : (0 : ((P.comap C : ideal R)).quotient) ∉ M := λ ⟨n, hn⟩, hp0 (pow_eq_zero hn), suffices : (⊥ : ideal (localization M)).is_maximal, { rw ← ϕ.comap_map_of_is_prime_disjoint ⊥ bot_prime (λ x hx, hM (hx.2 ▸ hx.1)), refine ((is_maximal_iff_is_maximal_disjoint ϕ _).mp _).1, rwa map_bot }, let M' : submonoid P.quotient := M.map φ, have hM' : (0 : P.quotient) ∉ M' := λ ⟨z, hz⟩, hM (quotient_map_injective (trans hz.2 φ.map_zero.symm) ▸ hz.1), letI : integral_domain (localization M') := localization_map.integral_domain_localization (le_non_zero_divisors_of_domain hM'), let ϕ' : localization_map (M.map ↑φ) (localization (M.map ↑φ)) := localization.of (M.map ↑φ), suffices : (⊥ : ideal (localization M')).is_maximal, { rw le_antisymm bot_le (comap_bot_le_of_injective _ (map_injective_of_injective _ quotient_map_injective M ϕ ϕ' (le_non_zero_divisors_of_domain hM'))), refine is_maximal_comap_of_is_integral_of_is_maximal' _ _ ⊥ this, apply is_integral_localization_map_polynomial_quotient P _ (submodule.coe_mem m) ϕ (ϕ' : _) }, rw (map_bot.symm : (⊥ : ideal (localization M')) = map ϕ'.to_map ⊥), refine map.is_maximal ϕ'.to_map (localization_map_bijective_of_field hM' _ ϕ') hP, rwa [← quotient.maximal_ideal_iff_is_field_quotient, ← bot_quotient_is_maximal_iff], end /-- Used to bootstrap the more general `quotient_mk_comp_C_is_integral_of_jacobson` -/ private lemma quotient_mk_comp_C_is_integral_of_jacobson' (hR : is_jacobson R) (hP' : ∀ (x : R), C x ∈ P → x = 0) : ((quotient.mk P).comp C : R →+* P.quotient).is_integral := begin refine (is_integral_quotient_map_iff _).mp _, let P' : ideal R := P.comap C, obtain ⟨pX, hpX, hp0⟩ := exists_nonzero_mem_of_ne_bot (ne_of_lt (bot_lt_of_maximal P polynomial_not_is_field)).symm hP', let M : submonoid P'.quotient := submonoid.powers (pX.map (quotient.mk P')).leading_coeff, let φ : P'.quotient →+* P.quotient := quotient_map P C le_rfl, let ϕ' : localization_map (M.map ↑φ) (localization (M.map ↑φ)) := localization.of (M.map ↑φ), haveI hp'_prime : P'.is_prime := comap_is_prime C P, have hM : (0 : P'.quotient) ∉ M := λ ⟨n, hn⟩, hp0 $ leading_coeff_eq_zero.mp (pow_eq_zero hn), refine ((quotient_map P C le_rfl).is_integral_tower_bot_of_is_integral (localization.of (M.map ↑(quotient_map P C le_rfl))).to_map _ _), { refine ϕ'.injective (le_non_zero_divisors_of_domain (λ hM', hM _)), exact (let ⟨z, zM, z0⟩ := hM' in (quotient_map_injective (trans z0 φ.map_zero.symm)) ▸ zM) }, { let ϕ : localization_map M (localization M) := localization.of M, rw ← (ϕ.map_comp _), refine ring_hom.is_integral_trans ϕ.to_map (ϕ.map (M.mem_map_of_mem (φ : P'.quotient →* P.quotient)) ϕ') _ _, { exact ϕ.to_map.is_integral_of_surjective (localization_map_bijective_of_field hM ((quotient.maximal_ideal_iff_is_field_quotient _).mp (is_maximal_comap_C_of_is_maximal P hP')) _).2 }, { exact is_integral_localization_map_polynomial_quotient P pX hpX _ _ } } end /-- If `R` is a Jacobson ring, and `P` is a maximal ideal of `polynomial R`, then `R → (polynomial R)/P` is an integral map. -/ lemma quotient_mk_comp_C_is_integral_of_jacobson : ((quotient.mk P).comp C : R →+* P.quotient).is_integral := begin let P' : ideal R := P.comap C, haveI : P'.is_prime := comap_is_prime C P, let f : polynomial R →+* polynomial P'.quotient := polynomial.map_ring_hom (quotient.mk P'), have hf : function.surjective f := map_surjective (quotient.mk P') quotient.mk_surjective, have hPJ : P = (P.map f).comap f, { rw comap_map_of_surjective _ hf, refine le_antisymm (le_sup_left_of_le le_rfl) (sup_le le_rfl _), refine λ p hp, polynomial_mem_ideal_of_coeff_mem_ideal P p (λ n, quotient.eq_zero_iff_mem.mp _), simpa only [coeff_map, coe_map_ring_hom] using (polynomial.ext_iff.mp hp) n }, refine ring_hom.is_integral_tower_bot_of_is_integral _ _ (injective_quotient_le_comap_map P) _, rw ← quotient_mk_maps_eq, refine ring_hom.is_integral_trans _ _ ((quotient.mk P').is_integral_of_surjective quotient.mk_surjective) _, apply quotient_mk_comp_C_is_integral_of_jacobson' _ _ (λ x hx, _), any_goals { exact ideal.is_jacobson_quotient }, { exact or.rec_on (map_eq_top_or_is_maximal_of_surjective f hf hP) (λ h, absurd (trans (h ▸ hPJ : P = comap f ⊤) comap_top : P = ⊤) hP.ne_top) id }, { obtain ⟨z, rfl⟩ := quotient.mk_surjective x, rwa [quotient.eq_zero_iff_mem, mem_comap, hPJ, mem_comap, coe_map_ring_hom, map_C] } end lemma is_maximal_comap_C_of_is_jacobson : (P.comap (C : R →+* polynomial R)).is_maximal := begin rw [← @mk_ker _ _ P, ring_hom.ker_eq_comap_bot, comap_comap], exact is_maximal_comap_of_is_integral_of_is_maximal' _ (quotient_mk_comp_C_is_integral_of_jacobson P) ⊥ ((bot_quotient_is_maximal_iff _).mpr hP), end omit P hP lemma comp_C_integral_of_surjective_of_jacobson {S : Type*} [field S] (f : (polynomial R) →+* S) (hf : function.surjective f) : (f.comp C).is_integral := begin haveI : (f.ker).is_maximal := @comap_is_maximal_of_surjective _ _ _ _ f ⊥ hf bot_is_maximal, let g : f.ker.quotient →+* S := ideal.quotient.lift f.ker f (λ _ h, h), have hfg : (g.comp (quotient.mk f.ker)) = f := ring_hom_ext' rfl rfl, rw [← hfg, ring_hom.comp_assoc], refine ring_hom.is_integral_trans _ g (quotient_mk_comp_C_is_integral_of_jacobson f.ker) (g.is_integral_of_surjective _), --(quotient.lift_surjective f.ker f _ hf)), rw [← hfg] at hf, exact function.surjective.of_comp hf, end end integral_domain end polynomial namespace mv_polynomial open mv_polynomial ring_hom lemma is_jacobson_mv_polynomial_fin {R : Type*} [comm_ring R] [H : is_jacobson R] : ∀ (n : ℕ), is_jacobson (mv_polynomial (fin n) R) | 0 := ((is_jacobson_iso ((rename_equiv R (equiv.equiv_pempty $ fin.elim0)).to_ring_equiv.trans (pempty_ring_equiv R))).mpr H) | (n+1) := (is_jacobson_iso (fin_succ_equiv R n).to_ring_equiv).2 (polynomial.is_jacobson_polynomial_iff_is_jacobson.2 (is_jacobson_mv_polynomial_fin n)) /-- General form of the nullstellensatz for Jacobson rings, since in a Jacobson ring we have `Inf {P maximal | P ≥ I} = Inf {P prime | P ≥ I} = I.radical`. Fields are always Jacobson, and in that special case this is (most of) the classical Nullstellensatz, since `I(V(I))` is the intersection of maximal ideals containing `I`, which is then `I.radical` -/ instance {R : Type*} [comm_ring R] {ι : Type*} [fintype ι] [is_jacobson R] : is_jacobson (mv_polynomial ι R) := begin haveI := classical.dec_eq ι, let e := fintype.equiv_fin ι, rw is_jacobson_iso (rename_equiv R e).to_ring_equiv, exact is_jacobson_mv_polynomial_fin _ end variables {n : ℕ} lemma quotient_mk_comp_C_is_integral_of_jacobson {R : Type*} [integral_domain R] [is_jacobson R] (P : ideal (mv_polynomial (fin n) R)) [P.is_maximal] : ((quotient.mk P).comp mv_polynomial.C : R →+* P.quotient).is_integral := begin unfreezingI {induction n with n IH}, { refine ring_hom.is_integral_of_surjective _ (function.surjective.comp quotient.mk_surjective _), exact C_surjective_fin_0 }, { rw [← fin_succ_equiv_comp_C_eq_C, ← ring_hom.comp_assoc, ← ring_hom.comp_assoc, ← quotient_map_comp_mk le_rfl, ring_hom.comp_assoc (polynomial.C), ← quotient_map_comp_mk le_rfl, ring_hom.comp_assoc, ring_hom.comp_assoc, ← quotient_map_comp_mk le_rfl, ← ring_hom.comp_assoc (quotient.mk _)], refine ring_hom.is_integral_trans _ _ _ _, { refine ring_hom.is_integral_trans _ _ (is_integral_of_surjective _ quotient.mk_surjective) _, refine ring_hom.is_integral_trans _ _ _ _, { apply (is_integral_quotient_map_iff _).mpr (IH _), apply polynomial.is_maximal_comap_C_of_is_jacobson _, { exact mv_polynomial.is_jacobson_mv_polynomial_fin n }, { apply comap_is_maximal_of_surjective, exact (fin_succ_equiv R n).symm.surjective } }, { refine (is_integral_quotient_map_iff _).mpr _, rw ← quotient_map_comp_mk le_rfl, refine ring_hom.is_integral_trans _ _ _ ((is_integral_quotient_map_iff _).mpr _), { exact ring_hom.is_integral_of_surjective _ quotient.mk_surjective }, { apply polynomial.quotient_mk_comp_C_is_integral_of_jacobson _, { exact mv_polynomial.is_jacobson_mv_polynomial_fin n }, { exact comap_is_maximal_of_surjective _ (fin_succ_equiv R n).symm.surjective } } } }, { refine (is_integral_quotient_map_iff _).mpr _, refine ring_hom.is_integral_trans _ _ _ (is_integral_of_surjective _ quotient.mk_surjective), exact ring_hom.is_integral_of_surjective _ (fin_succ_equiv R n).symm.surjective } } end lemma comp_C_integral_of_surjective_of_jacobson {R : Type*} [integral_domain R] [is_jacobson R] {σ : Type*} [fintype σ] {S : Type*} [field S] (f : mv_polynomial σ R →+* S) (hf : function.surjective f) : (f.comp C).is_integral := begin haveI := classical.dec_eq σ, obtain ⟨e⟩ := fintype.trunc_equiv_fin σ, let f' : mv_polynomial (fin _) R →+* S := f.comp (rename_equiv R e.symm).to_ring_equiv.to_ring_hom, have hf' : function.surjective f' := ((function.surjective.comp hf (rename_equiv R e.symm).surjective)), have : (f'.comp C).is_integral, { haveI : (f'.ker).is_maximal := @comap_is_maximal_of_surjective _ _ _ _ f' ⊥ hf' bot_is_maximal, let g : f'.ker.quotient →+* S := ideal.quotient.lift f'.ker f' (λ _ h, h), have hfg : (g.comp (quotient.mk f'.ker)) = f' := ring_hom_ext (λ r, rfl) (λ i, rfl), rw [← hfg, ring_hom.comp_assoc], refine ring_hom.is_integral_trans _ g (quotient_mk_comp_C_is_integral_of_jacobson f'.ker) (g.is_integral_of_surjective _), rw ← hfg at hf', exact function.surjective.of_comp hf' }, rw ring_hom.comp_assoc at this, convert this, refine ring_hom.ext (λ x, _), exact ((rename_equiv R e.symm).commutes' x).symm, end end mv_polynomial end ideal
1540f2295111f3413226eae83cc472998b9c2425
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/adjunction/reflective.lean
732ccd7a39970adf86727a8f462fa40d9706cb43
[ "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
8,048
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import category_theory.adjunction.fully_faithful import category_theory.functor.reflects_isomorphisms import category_theory.epi_mono /-! # Reflective functors > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Basic properties of reflective functors, especially those relating to their essential image. Note properties of reflective functors relating to limits and colimits are included in `category_theory.monad.limits`. -/ universes v₁ v₂ v₃ u₁ u₂ u₃ noncomputable theory namespace category_theory open category adjunction variables {C : Type u₁} {D : Type u₂} {E : Type u₃} variables [category.{v₁} C] [category.{v₂} D] [category.{v₃} E] /-- A functor is *reflective*, or *a reflective inclusion*, if it is fully faithful and right adjoint. -/ class reflective (R : D ⥤ C) extends is_right_adjoint R, full R, faithful R. variables {i : D ⥤ C} /-- For a reflective functor `i` (with left adjoint `L`), with unit `η`, we have `η_iL = iL η`. -/ -- TODO: This holds more generally for idempotent adjunctions, not just reflective adjunctions. lemma unit_obj_eq_map_unit [reflective i] (X : C) : (of_right_adjoint i).unit.app (i.obj ((left_adjoint i).obj X)) = i.map ((left_adjoint i).map ((of_right_adjoint i).unit.app X)) := begin rw [←cancel_mono (i.map ((of_right_adjoint i).counit.app ((left_adjoint i).obj X))), ←i.map_comp], simp, end /-- When restricted to objects in `D` given by `i : D ⥤ C`, the unit is an isomorphism. In other words, `η_iX` is an isomorphism for any `X` in `D`. More generally this applies to objects essentially in the reflective subcategory, see `functor.ess_image.unit_iso`. -/ instance is_iso_unit_obj [reflective i] {B : D} : is_iso ((of_right_adjoint i).unit.app (i.obj B)) := begin have : (of_right_adjoint i).unit.app (i.obj B) = inv (i.map ((of_right_adjoint i).counit.app B)), { rw ← comp_hom_eq_id, apply (of_right_adjoint i).right_triangle_components }, rw this, exact is_iso.inv_is_iso, end /-- If `A` is essentially in the image of a reflective functor `i`, then `η_A` is an isomorphism. This gives that the "witness" for `A` being in the essential image can instead be given as the reflection of `A`, with the isomorphism as `η_A`. (For any `B` in the reflective subcategory, we automatically have that `ε_B` is an iso.) -/ lemma functor.ess_image.unit_is_iso [reflective i] {A : C} (h : A ∈ i.ess_image) : is_iso ((of_right_adjoint i).unit.app A) := begin suffices : (of_right_adjoint i).unit.app A = h.get_iso.inv ≫ (of_right_adjoint i).unit.app (i.obj h.witness) ≫ (left_adjoint i ⋙ i).map h.get_iso.hom, { rw this, apply_instance }, rw ← nat_trans.naturality, simp, end /-- If `η_A` is an isomorphism, then `A` is in the essential image of `i`. -/ lemma mem_ess_image_of_unit_is_iso [is_right_adjoint i] (A : C) [is_iso ((of_right_adjoint i).unit.app A)] : A ∈ i.ess_image := ⟨(left_adjoint i).obj A, ⟨(as_iso ((of_right_adjoint i).unit.app A)).symm⟩⟩ /-- If `η_A` is a split monomorphism, then `A` is in the reflective subcategory. -/ lemma mem_ess_image_of_unit_is_split_mono [reflective i] {A : C} [is_split_mono ((of_right_adjoint i).unit.app A)] : A ∈ i.ess_image := begin let η : 𝟭 C ⟶ left_adjoint i ⋙ i := (of_right_adjoint i).unit, haveI : is_iso (η.app (i.obj ((left_adjoint i).obj A))) := (i.obj_mem_ess_image _).unit_is_iso, have : epi (η.app A), { apply epi_of_epi (retraction (η.app A)) _, rw (show retraction _ ≫ η.app A = _, from η.naturality (retraction (η.app A))), apply epi_comp (η.app (i.obj ((left_adjoint i).obj A))) }, resetI, haveI := is_iso_of_epi_of_is_split_mono (η.app A), exact mem_ess_image_of_unit_is_iso A, end /-- Composition of reflective functors. -/ instance reflective.comp (F : C ⥤ D) (G : D ⥤ E) [Fr : reflective F] [Gr : reflective G] : reflective (F ⋙ G) := { to_faithful := faithful.comp F G, } /-- (Implementation) Auxiliary definition for `unit_comp_partial_bijective`. -/ def unit_comp_partial_bijective_aux [reflective i] (A : C) (B : D) : (A ⟶ i.obj B) ≃ (i.obj ((left_adjoint i).obj A) ⟶ i.obj B) := ((adjunction.of_right_adjoint i).hom_equiv _ _).symm.trans (equiv_of_fully_faithful i) /-- The description of the inverse of the bijection `unit_comp_partial_bijective_aux`. -/ lemma unit_comp_partial_bijective_aux_symm_apply [reflective i] {A : C} {B : D} (f : i.obj ((left_adjoint i).obj A) ⟶ i.obj B) : (unit_comp_partial_bijective_aux _ _).symm f = (of_right_adjoint i).unit.app A ≫ f := by simp [unit_comp_partial_bijective_aux] /-- If `i` has a reflector `L`, then the function `(i.obj (L.obj A) ⟶ B) → (A ⟶ B)` given by precomposing with `η.app A` is a bijection provided `B` is in the essential image of `i`. That is, the function `λ (f : i.obj (L.obj A) ⟶ B), η.app A ≫ f` is bijective, as long as `B` is in the essential image of `i`. This definition gives an equivalence: the key property that the inverse can be described nicely is shown in `unit_comp_partial_bijective_symm_apply`. This establishes there is a natural bijection `(A ⟶ B) ≃ (i.obj (L.obj A) ⟶ B)`. In other words, from the point of view of objects in `D`, `A` and `i.obj (L.obj A)` look the same: specifically that `η.app A` is an isomorphism. -/ def unit_comp_partial_bijective [reflective i] (A : C) {B : C} (hB : B ∈ i.ess_image) : (A ⟶ B) ≃ (i.obj ((left_adjoint i).obj A) ⟶ B) := calc (A ⟶ B) ≃ (A ⟶ i.obj hB.witness) : iso.hom_congr (iso.refl _) hB.get_iso.symm ... ≃ (i.obj _ ⟶ i.obj hB.witness) : unit_comp_partial_bijective_aux _ _ ... ≃ (i.obj ((left_adjoint i).obj A) ⟶ B) : iso.hom_congr (iso.refl _) hB.get_iso @[simp] lemma unit_comp_partial_bijective_symm_apply [reflective i] (A : C) {B : C} (hB : B ∈ i.ess_image) (f) : (unit_comp_partial_bijective A hB).symm f = (of_right_adjoint i).unit.app A ≫ f := by simp [unit_comp_partial_bijective, unit_comp_partial_bijective_aux_symm_apply] lemma unit_comp_partial_bijective_symm_natural [reflective i] (A : C) {B B' : C} (h : B ⟶ B') (hB : B ∈ i.ess_image) (hB' : B' ∈ i.ess_image) (f : i.obj ((left_adjoint i).obj A) ⟶ B) : (unit_comp_partial_bijective A hB').symm (f ≫ h) = (unit_comp_partial_bijective A hB).symm f ≫ h := by simp lemma unit_comp_partial_bijective_natural [reflective i] (A : C) {B B' : C} (h : B ⟶ B') (hB : B ∈ i.ess_image) (hB' : B' ∈ i.ess_image) (f : A ⟶ B) : (unit_comp_partial_bijective A hB') (f ≫ h) = unit_comp_partial_bijective A hB f ≫ h := by rw [←equiv.eq_symm_apply, unit_comp_partial_bijective_symm_natural A h, equiv.symm_apply_apply] /-- If `i : D ⥤ C` is reflective, the inverse functor of `i ≌ F.ess_image` can be explicitly defined by the reflector. -/ @[simps] def equiv_ess_image_of_reflective [reflective i] : D ≌ i.ess_image_subcategory := { functor := i.to_ess_image, inverse := i.ess_image_inclusion ⋙ (left_adjoint i : _), unit_iso := nat_iso.of_components (λ X, (as_iso $ (of_right_adjoint i).counit.app X).symm) (by { intros X Y f, dsimp, simp only [is_iso.eq_inv_comp, is_iso.comp_inv_eq, category.assoc], exact ((of_right_adjoint i).counit.naturality _).symm }), counit_iso := nat_iso.of_components (λ X, by { refine (iso.symm $ as_iso _), exact (of_right_adjoint i).unit.app X.obj, apply_with (is_iso_of_reflects_iso _ i.ess_image_inclusion) { instances := ff }, exact functor.ess_image.unit_is_iso X.property }) (by { intros X Y f, dsimp, rw [is_iso.comp_inv_eq, assoc], have h := ((of_right_adjoint i).unit.naturality f).symm, rw [functor.id_map] at h, erw [← h, is_iso.inv_hom_id_assoc, functor.comp_map] }) } end category_theory
2c542cf7798f616f86dda782088ae27251bade02
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/topology/metric_space/lipschitz.lean
b677ef612c74186f0847b970868ed2e257be2881
[ "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
14,217
lean
/- Copyright (c) 2018 Rohan Mitta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rohan Mitta, Kevin Buzzard, Alistair Tucker, Johannes Hölzl, Yury Kudryashov -/ import logic.function.iterate import topology.metric_space.basic import category_theory.endomorphism import category_theory.types /-! # Lipschitz continuous functions A map `f : α → β` between two (extended) metric spaces is called *Lipschitz continuous* with constant `K ≥ 0` if for all `x, y` we have `edist (f x) (f y) ≤ K * edist x y`. For a metric space, the latter inequality is equivalent to `dist (f x) (f y) ≤ K * dist x y`. There is also a version asserting this inequality only for `x` and `y` in some set `s`. In this file we provide various ways to prove that various combinations of Lipschitz continuous functions are Lipschitz continuous. We also prove that Lipschitz continuous functions are uniformly continuous. ## Main definitions and lemmas * `lipschitz_with K f`: states that `f` is Lipschitz with constant `K : ℝ≥0` * `lipschitz_on_with K f`: states that `f` is Lipschitz with constant `K : ℝ≥0` on a set `s` * `lipschitz_with.uniform_continuous`: a Lipschitz function is uniformly continuous * `lipschitz_on_with.uniform_continuous_on`: a function which is Lipschitz on a set is uniformly continuous on that set. ## Implementation notes The parameter `K` has type `ℝ≥0`. This way we avoid conjuction in the definition and have coercions both to `ℝ` and `ℝ≥0∞`. Constructors whose names end with `'` take `K : ℝ` as an argument, and return `lipschitz_with (nnreal.of_real K) f`. -/ universes u v w x open filter function set open_locale topological_space nnreal ennreal variables {α : Type u} {β : Type v} {γ : Type w} {ι : Type x} /-- A function `f` is Lipschitz continuous with constant `K ≥ 0` if for all `x, y` we have `dist (f x) (f y) ≤ K * dist x y` -/ def lipschitz_with [pseudo_emetric_space α] [pseudo_emetric_space β] (K : ℝ≥0) (f : α → β) := ∀x y, edist (f x) (f y) ≤ K * edist x y lemma lipschitz_with_iff_dist_le_mul [pseudo_metric_space α] [pseudo_metric_space β] {K : ℝ≥0} {f : α → β} : lipschitz_with K f ↔ ∀ x y, dist (f x) (f y) ≤ K * dist x y := by { simp only [lipschitz_with, edist_nndist, dist_nndist], norm_cast } alias lipschitz_with_iff_dist_le_mul ↔ lipschitz_with.dist_le_mul lipschitz_with.of_dist_le_mul /-- A function `f` is Lipschitz continuous with constant `K ≥ 0` on `s` if for all `x, y` in `s` we have `dist (f x) (f y) ≤ K * dist x y` -/ def lipschitz_on_with [pseudo_emetric_space α] [pseudo_emetric_space β] (K : ℝ≥0) (f : α → β) (s : set α) := ∀ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), edist (f x) (f y) ≤ K * edist x y @[simp] lemma lipschitz_on_with_empty [pseudo_emetric_space α] [pseudo_emetric_space β] (K : ℝ≥0) (f : α → β) : lipschitz_on_with K f ∅ := λ x x_in y y_in, false.elim x_in lemma lipschitz_on_with.mono [pseudo_emetric_space α] [pseudo_emetric_space β] {K : ℝ≥0} {s t : set α} {f : α → β} (hf : lipschitz_on_with K f t) (h : s ⊆ t) : lipschitz_on_with K f s := λ x x_in y y_in, hf (h x_in) (h y_in) lemma lipschitz_on_with_iff_dist_le_mul [pseudo_metric_space α] [pseudo_metric_space β] {K : ℝ≥0} {s : set α} {f : α → β} : lipschitz_on_with K f s ↔ ∀ (x ∈ s) (y ∈ s), dist (f x) (f y) ≤ K * dist x y := by { simp only [lipschitz_on_with, edist_nndist, dist_nndist], norm_cast } alias lipschitz_on_with_iff_dist_le_mul ↔ lipschitz_on_with.dist_le_mul lipschitz_on_with.of_dist_le_mul @[simp] lemma lipschitz_on_univ [pseudo_emetric_space α] [pseudo_emetric_space β] {K : ℝ≥0} {f : α → β} : lipschitz_on_with K f univ ↔ lipschitz_with K f := by simp [lipschitz_on_with, lipschitz_with] lemma lipschitz_on_with_iff_restrict [pseudo_emetric_space α] [pseudo_emetric_space β] {K : ℝ≥0} {f : α → β} {s : set α} : lipschitz_on_with K f s ↔ lipschitz_with K (s.restrict f) := by simp only [lipschitz_on_with, lipschitz_with, set_coe.forall', restrict, subtype.edist_eq] namespace lipschitz_with section emetric variables [pseudo_emetric_space α] [pseudo_emetric_space β] [pseudo_emetric_space γ] variables {K : ℝ≥0} {f : α → β} lemma edist_le_mul (h : lipschitz_with K f) (x y : α) : edist (f x) (f y) ≤ K * edist x y := h x y lemma edist_lt_top (hf : lipschitz_with K f) {x y : α} (h : edist x y < ⊤) : edist (f x) (f y) < ⊤ := lt_of_le_of_lt (hf x y) $ ennreal.mul_lt_top ennreal.coe_lt_top h lemma mul_edist_le (h : lipschitz_with K f) (x y : α) : (K⁻¹ : ℝ≥0∞) * edist (f x) (f y) ≤ edist x y := begin have := h x y, rw [mul_comm] at this, replace := ennreal.div_le_of_le_mul this, rwa [div_eq_mul_inv, mul_comm] at this end protected lemma of_edist_le (h : ∀ x y, edist (f x) (f y) ≤ edist x y) : lipschitz_with 1 f := λ x y, by simp only [ennreal.coe_one, one_mul, h] protected lemma weaken (hf : lipschitz_with K f) {K' : ℝ≥0} (h : K ≤ K') : lipschitz_with K' f := assume x y, le_trans (hf x y) $ ennreal.mul_right_mono (ennreal.coe_le_coe.2 h) lemma ediam_image_le (hf : lipschitz_with K f) (s : set α) : emetric.diam (f '' s) ≤ K * emetric.diam s := begin apply emetric.diam_le, rintros _ ⟨x, hx, rfl⟩ _ ⟨y, hy, rfl⟩, calc edist (f x) (f y) ≤ ↑K * edist x y : hf.edist_le_mul x y ... ≤ ↑K * emetric.diam s : ennreal.mul_left_mono (emetric.edist_le_diam_of_mem hx hy) end /-- A Lipschitz function is uniformly continuous -/ protected lemma uniform_continuous (hf : lipschitz_with K f) : uniform_continuous f := begin refine emetric.uniform_continuous_iff.2 (λε εpos, _), use [ε/K, canonically_ordered_semiring.mul_pos.2 ⟨εpos, ennreal.inv_pos.2 $ ennreal.coe_ne_top⟩], assume x y Dxy, apply lt_of_le_of_lt (hf.edist_le_mul x y), rw [mul_comm], exact ennreal.mul_lt_of_lt_div Dxy end /-- A Lipschitz function is continuous -/ protected lemma continuous (hf : lipschitz_with K f) : continuous f := hf.uniform_continuous.continuous protected lemma const (b : β) : lipschitz_with 0 (λa:α, b) := assume x y, by simp only [edist_self, zero_le] protected lemma id : lipschitz_with 1 (@id α) := lipschitz_with.of_edist_le $ assume x y, le_refl _ protected lemma subtype_val (s : set α) : lipschitz_with 1 (subtype.val : s → α) := lipschitz_with.of_edist_le $ assume x y, le_refl _ protected lemma subtype_coe (s : set α) : lipschitz_with 1 (coe : s → α) := lipschitz_with.subtype_val s protected lemma restrict (hf : lipschitz_with K f) (s : set α) : lipschitz_with K (s.restrict f) := λ x y, hf x y protected lemma comp {Kf Kg : ℝ≥0} {f : β → γ} {g : α → β} (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf * Kg) (f ∘ g) := assume x y, calc edist (f (g x)) (f (g y)) ≤ Kf * edist (g x) (g y) : hf _ _ ... ≤ Kf * (Kg * edist x y) : ennreal.mul_left_mono (hg _ _) ... = (Kf * Kg : ℝ≥0) * edist x y : by rw [← mul_assoc, ennreal.coe_mul] protected lemma prod_fst : lipschitz_with 1 (@prod.fst α β) := lipschitz_with.of_edist_le $ assume x y, le_max_left _ _ protected lemma prod_snd : lipschitz_with 1 (@prod.snd α β) := lipschitz_with.of_edist_le $ assume x y, le_max_right _ _ protected lemma prod {f : α → β} {Kf : ℝ≥0} (hf : lipschitz_with Kf f) {g : α → γ} {Kg : ℝ≥0} (hg : lipschitz_with Kg g) : lipschitz_with (max Kf Kg) (λ x, (f x, g x)) := begin assume x y, rw [ennreal.coe_mono.map_max, prod.edist_eq, ennreal.max_mul], exact max_le_max (hf x y) (hg x y) end protected lemma uncurry {f : α → β → γ} {Kα Kβ : ℝ≥0} (hα : ∀ b, lipschitz_with Kα (λ a, f a b)) (hβ : ∀ a, lipschitz_with Kβ (f a)) : lipschitz_with (Kα + Kβ) (function.uncurry f) := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, simp only [function.uncurry, ennreal.coe_add, add_mul], apply le_trans (edist_triangle _ (f a₂ b₁) _), exact add_le_add (le_trans (hα _ _ _) $ ennreal.mul_left_mono $ le_max_left _ _) (le_trans (hβ _ _ _) $ ennreal.mul_left_mono $ le_max_right _ _) end protected lemma iterate {f : α → α} (hf : lipschitz_with K f) : ∀n, lipschitz_with (K ^ n) (f^[n]) | 0 := lipschitz_with.id | (n + 1) := by rw [pow_succ']; exact (iterate n).comp hf lemma edist_iterate_succ_le_geometric {f : α → α} (hf : lipschitz_with K f) (x n) : edist (f^[n] x) (f^[n + 1] x) ≤ edist x (f x) * K ^ n := begin rw [iterate_succ, mul_comm], simpa only [ennreal.coe_pow] using (hf.iterate n) x (f x) end open category_theory protected lemma mul {f g : End α} {Kf Kg} (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf * Kg) (f * g : End α) := hf.comp hg /-- The product of a list of Lipschitz continuous endomorphisms is a Lipschitz continuous endomorphism. -/ protected lemma list_prod (f : ι → End α) (K : ι → ℝ≥0) (h : ∀ i, lipschitz_with (K i) (f i)) : ∀ l : list ι, lipschitz_with (l.map K).prod (l.map f).prod | [] := by simp [types_id, lipschitz_with.id] | (i :: l) := by { simp only [list.map_cons, list.prod_cons], exact (h i).mul (list_prod l) } protected lemma pow {f : End α} {K} (h : lipschitz_with K f) : ∀ n : ℕ, lipschitz_with (K^n) (f^n : End α) | 0 := lipschitz_with.id | (n + 1) := h.mul (pow n) end emetric section metric variables [pseudo_metric_space α] [pseudo_metric_space β] [pseudo_metric_space γ] {K : ℝ≥0} protected lemma of_dist_le' {f : α → β} {K : ℝ} (h : ∀ x y, dist (f x) (f y) ≤ K * dist x y) : lipschitz_with (nnreal.of_real K) f := of_dist_le_mul $ λ x y, le_trans (h x y) $ mul_le_mul_of_nonneg_right (nnreal.le_coe_of_real K) dist_nonneg protected lemma mk_one {f : α → β} (h : ∀ x y, dist (f x) (f y) ≤ dist x y) : lipschitz_with 1 f := of_dist_le_mul $ by simpa only [nnreal.coe_one, one_mul] using h /-- For functions to `ℝ`, it suffices to prove `f x ≤ f y + K * dist x y`; this version doesn't assume `0≤K`. -/ protected lemma of_le_add_mul' {f : α → ℝ} (K : ℝ) (h : ∀x y, f x ≤ f y + K * dist x y) : lipschitz_with (nnreal.of_real K) f := have I : ∀ x y, f x - f y ≤ K * dist x y, from assume x y, sub_le_iff_le_add'.2 (h x y), lipschitz_with.of_dist_le' $ assume x y, abs_sub_le_iff.2 ⟨I x y, dist_comm y x ▸ I y x⟩ /-- For functions to `ℝ`, it suffices to prove `f x ≤ f y + K * dist x y`; this version assumes `0≤K`. -/ protected lemma of_le_add_mul {f : α → ℝ} (K : ℝ≥0) (h : ∀x y, f x ≤ f y + K * dist x y) : lipschitz_with K f := by simpa only [nnreal.of_real_coe] using lipschitz_with.of_le_add_mul' K h protected lemma of_le_add {f : α → ℝ} (h : ∀ x y, f x ≤ f y + dist x y) : lipschitz_with 1 f := lipschitz_with.of_le_add_mul 1 $ by simpa only [nnreal.coe_one, one_mul] protected lemma le_add_mul {f : α → ℝ} {K : ℝ≥0} (h : lipschitz_with K f) (x y) : f x ≤ f y + K * dist x y := sub_le_iff_le_add'.1 $ le_trans (le_abs_self _) $ h.dist_le_mul x y protected lemma iff_le_add_mul {f : α → ℝ} {K : ℝ≥0} : lipschitz_with K f ↔ ∀ x y, f x ≤ f y + K * dist x y := ⟨lipschitz_with.le_add_mul, lipschitz_with.of_le_add_mul K⟩ lemma nndist_le {f : α → β} (hf : lipschitz_with K f) (x y : α) : nndist (f x) (f y) ≤ K * nndist x y := hf.dist_le_mul x y lemma diam_image_le {f : α → β} (hf : lipschitz_with K f) (s : set α) (hs : metric.bounded s) : metric.diam (f '' s) ≤ K * metric.diam s := begin apply metric.diam_le_of_forall_dist_le (mul_nonneg K.coe_nonneg metric.diam_nonneg), rintros _ ⟨x, hx, rfl⟩ _ ⟨y, hy, rfl⟩, calc dist (f x) (f y) ≤ ↑K * dist x y : hf.dist_le_mul x y ... ≤ ↑K * metric.diam s : mul_le_mul_of_nonneg_left (metric.dist_le_diam_of_mem hs hx hy) K.2 end protected lemma dist_left (y : α) : lipschitz_with 1 (λ x, dist x y) := lipschitz_with.of_le_add $ assume x z, by { rw [add_comm], apply dist_triangle } protected lemma dist_right (x : α) : lipschitz_with 1 (dist x) := lipschitz_with.of_le_add $ assume y z, dist_triangle_right _ _ _ protected lemma dist : lipschitz_with 2 (function.uncurry $ @dist α _) := lipschitz_with.uncurry lipschitz_with.dist_left lipschitz_with.dist_right lemma dist_iterate_succ_le_geometric {f : α → α} (hf : lipschitz_with K f) (x n) : dist (f^[n] x) (f^[n + 1] x) ≤ dist x (f x) * K ^ n := begin rw [iterate_succ, mul_comm], simpa only [nnreal.coe_pow] using (hf.iterate n).dist_le_mul x (f x) end end metric end lipschitz_with namespace lipschitz_on_with variables [pseudo_emetric_space α] [pseudo_emetric_space β] [pseudo_emetric_space γ] variables {K : ℝ≥0} {s : set α} {f : α → β} protected lemma uniform_continuous_on (hf : lipschitz_on_with K f s) : uniform_continuous_on f s := uniform_continuous_on_iff_restrict.mpr (lipschitz_on_with_iff_restrict.mp hf).uniform_continuous protected lemma continuous_on (hf : lipschitz_on_with K f s) : continuous_on f s := hf.uniform_continuous_on.continuous_on end lipschitz_on_with open metric /-- If a function is locally Lipschitz around a point, then it is continuous at this point. -/ lemma continuous_at_of_locally_lipschitz [metric_space α] [metric_space β] {f : α → β} {x : α} {r : ℝ} (hr : 0 < r) (K : ℝ) (h : ∀y, dist y x < r → dist (f y) (f x) ≤ K * dist y x) : continuous_at f x := begin refine (nhds_basis_ball.tendsto_iff nhds_basis_closed_ball).2 (λε εpos, ⟨min r (ε / max K 1), _, λ y hy, _⟩), { simp [hr, div_pos εpos, zero_lt_one] }, have A : max K 1 ≠ 0 := ne_of_gt (lt_max_iff.2 (or.inr zero_lt_one)), calc dist (f y) (f x) ≤ K * dist y x : h y (lt_of_lt_of_le hy (min_le_left _ _)) ... ≤ max K 1 * dist y x : mul_le_mul_of_nonneg_right (le_max_left K 1) dist_nonneg ... ≤ max K 1 * (ε / max K 1) : mul_le_mul_of_nonneg_left (le_of_lt (lt_of_lt_of_le hy (min_le_right _ _))) (le_trans zero_le_one (le_max_right K 1)) ... = ε : mul_div_cancel' _ A end
555e758d14c0b9f3c3d9ac3fdd3e2e8ff0f674d3
556aeb81a103e9e0ac4e1fe0ce1bc6e6161c3c5e
/src/starkware/cairo/common/cairo_secp/verification/verification/signature_recover_public_key_assert_nn_le_soundness.lean
c98725b6c0cf2161cc86b7dc8805073271a1ba8e
[]
permissive
starkware-libs/formal-proofs
d6b731604461bf99e6ba820e68acca62a21709e8
f5fa4ba6a471357fd171175183203d0b437f6527
refs/heads/master
1,691,085,444,753
1,690,507,386,000
1,690,507,386,000
410,476,629
32
9
Apache-2.0
1,690,506,773,000
1,632,639,790,000
Lean
UTF-8
Lean
false
false
6,602
lean
/- File: signature_recover_public_key_assert_nn_le_soundness.lean Autogenerated file. -/ import starkware.cairo.lean.semantics.soundness.hoare import .signature_recover_public_key_code import ..signature_recover_public_key_spec import .signature_recover_public_key_assert_le_soundness open tactic open starkware.cairo.common.math variables {F : Type} [field F] [decidable_eq F] [prelude_hyps F] variable mem : F → F variable σ : register_state F /- starkware.cairo.common.math.assert_nn_le autogenerated soundness theorem -/ theorem auto_sound_assert_nn_le -- arguments (range_check_ptr a b : F) -- code is in memory at σ.pc (h_mem : mem_at mem code_assert_nn_le σ.pc) -- all dependencies are in memory (h_mem_0 : mem_at mem code_assert_nn (σ.pc - 9)) (h_mem_1 : mem_at mem code_assert_le (σ.pc - 5)) -- input arguments on the stack (hin_range_check_ptr : range_check_ptr = mem (σ.fp - 5)) (hin_a : a = mem (σ.fp - 4)) (hin_b : b = mem (σ.fp - 3)) -- conclusion : ensures_ret mem σ (λ κ τ, τ.ap = σ.ap + 14 ∧ ∃ μ ≤ κ, rc_ensures mem (rc_bound F) μ (mem (σ.fp - 5)) (mem $ τ.ap - 1) (spec_assert_nn_le mem κ range_check_ptr a b (mem (τ.ap - 1)))) := begin apply ensures_of_ensuresb, intro νbound, have h_mem_rec := h_mem, unpack_memory code_assert_nn_le at h_mem with ⟨hpc0, hpc1, hpc2, hpc3, hpc4, hpc5, hpc6, hpc7, hpc8⟩, -- function call step_assert_eq hpc0 with arg0, step_assert_eq hpc1 with arg1, step_sub hpc2 (auto_sound_assert_nn mem _ range_check_ptr a _ _ _), { rw hpc3, norm_num2, exact h_mem_0 }, { try { simp only [add_neg_eq_sub, hin_range_check_ptr, hin_a, hin_b] }, try { arith_simps }, try { simp only [arg0, arg1] }, try { arith_simps; try { split }; triv <|> refl <|> simp <|> abel; try { norm_num } }, }, { try { simp only [add_neg_eq_sub, hin_range_check_ptr, hin_a, hin_b] }, try { arith_simps }, try { simp only [arg0, arg1] }, try { arith_simps; try { split }; triv <|> refl <|> simp <|> abel; try { norm_num } }, }, intros κ_call4 ap4 h_call4, rcases h_call4 with ⟨h_call4_ap_offset, h_call4⟩, rcases h_call4 with ⟨rc_m4, rc_mle4, hl_range_check_ptr₁, h_call4⟩, generalize' hr_rev_range_check_ptr₁: mem (ap4 - 1) = range_check_ptr₁, have htv_range_check_ptr₁ := hr_rev_range_check_ptr₁.symm, clear hr_rev_range_check_ptr₁, try { simp only [arg0 ,arg1] at hl_range_check_ptr₁ }, rw [←htv_range_check_ptr₁, ←hin_range_check_ptr] at hl_range_check_ptr₁, try { simp only [arg0 ,arg1] at h_call4 }, rw [hin_range_check_ptr] at h_call4, clear arg0 arg1, -- function call step_assert_eq hpc4 with arg0, step_assert_eq hpc5 with arg1, step_sub hpc6 (auto_sound_assert_le mem _ range_check_ptr₁ a b _ _ _ _ _), { rw hpc7, norm_num2, exact h_mem_1 }, { rw hpc7, norm_num2, exact h_mem_0 }, { try { simp only [add_neg_eq_sub, hin_range_check_ptr, hin_a, hin_b, htv_range_check_ptr₁] }, try { arith_simps }, try { simp only [arg0, arg1] }, try { simp only [h_call4_ap_offset] }, try { arith_simps; try { split }; triv <|> refl <|> simp <|> abel; try { norm_num } }, }, { try { simp only [add_neg_eq_sub, hin_range_check_ptr, hin_a, hin_b, htv_range_check_ptr₁] }, try { arith_simps }, try { simp only [arg0, arg1] }, try { simp only [h_call4_ap_offset] }, try { arith_simps; try { split }; triv <|> refl <|> simp <|> abel; try { norm_num } }, }, { try { simp only [add_neg_eq_sub, hin_range_check_ptr, hin_a, hin_b, htv_range_check_ptr₁] }, try { arith_simps }, try { simp only [arg0, arg1] }, try { simp only [h_call4_ap_offset] }, try { arith_simps; try { split }; triv <|> refl <|> simp <|> abel; try { norm_num } }, }, intros κ_call8 ap8 h_call8, rcases h_call8 with ⟨h_call8_ap_offset, h_call8⟩, rcases h_call8 with ⟨rc_m8, rc_mle8, hl_range_check_ptr₂, h_call8⟩, generalize' hr_rev_range_check_ptr₂: mem (ap8 - 1) = range_check_ptr₂, have htv_range_check_ptr₂ := hr_rev_range_check_ptr₂.symm, clear hr_rev_range_check_ptr₂, try { simp only [arg0 ,arg1] at hl_range_check_ptr₂ }, rw [←htv_range_check_ptr₂, ←htv_range_check_ptr₁] at hl_range_check_ptr₂, try { simp only [arg0 ,arg1] at h_call8 }, rw [←htv_range_check_ptr₁, hl_range_check_ptr₁, hin_range_check_ptr] at h_call8, clear arg0 arg1, -- return step_ret hpc8, -- finish step_done, use_only [rfl, rfl], split, { try { simp only [h_call4_ap_offset ,h_call8_ap_offset] }, try { arith_simps }, try { refl } }, -- range check condition use_only (rc_m4+rc_m8+0+0), split, linarith [rc_mle4, rc_mle8], split, { arith_simps, rw [←htv_range_check_ptr₂, hl_range_check_ptr₂, hl_range_check_ptr₁, hin_range_check_ptr], try { arith_simps, refl <|> norm_cast }, try { refl } }, intro rc_h_range_check_ptr, repeat { rw [add_assoc] at rc_h_range_check_ptr }, have rc_h_range_check_ptr' := range_checked_add_right rc_h_range_check_ptr, -- Final Proof -- user-provided reduction suffices auto_spec: auto_spec_assert_nn_le mem _ range_check_ptr a b _, { apply sound_assert_nn_le, apply auto_spec }, -- prove the auto generated assertion dsimp [auto_spec_assert_nn_le], try { norm_num1 }, try { arith_simps }, use_only [κ_call4], use_only [range_check_ptr₁], have rc_h_range_check_ptr₁ := range_checked_offset' rc_h_range_check_ptr, have rc_h_range_check_ptr₁' := range_checked_add_right rc_h_range_check_ptr₁, try { norm_cast at rc_h_range_check_ptr₁' }, have spec4 := h_call4 rc_h_range_check_ptr', rw [←hin_range_check_ptr, ←htv_range_check_ptr₁] at spec4, try { dsimp at spec4, arith_simps at spec4 }, use_only [spec4], use_only [κ_call8], use_only [range_check_ptr₂], have rc_h_range_check_ptr₂ := range_checked_offset' rc_h_range_check_ptr₁, have rc_h_range_check_ptr₂' := range_checked_add_right rc_h_range_check_ptr₂, try { norm_cast at rc_h_range_check_ptr₂' }, have spec8 := h_call8 rc_h_range_check_ptr₁', rw [←hin_range_check_ptr, ←hl_range_check_ptr₁, ←htv_range_check_ptr₂] at spec8, try { dsimp at spec8, arith_simps at spec8 }, use_only [spec8], try { split, linarith }, try { ensures_simps; try { simp only [add_neg_eq_sub, hin_range_check_ptr, hin_a, hin_b, htv_range_check_ptr₁, htv_range_check_ptr₂] }, }, try { simp only [h_call4_ap_offset, h_call8_ap_offset] }, try { arith_simps; try { split }; triv <|> refl <|> simp <|> abel; try { norm_num } }, end
476670b2ba9e80f41d974539353f6879dd5cc643
f4bff2062c030df03d65e8b69c88f79b63a359d8
/src/game/order/level02.lean
0ccfbadffca1f34d0ca576aa2030511d199fc866
[ "Apache-2.0" ]
permissive
adastra7470/real-number-game
776606961f52db0eb824555ed2f8e16f92216ea3
f9dcb7d9255a79b57e62038228a23346c2dc301b
refs/heads/master
1,669,221,575,893
1,594,669,800,000
1,594,669,800,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,198
lean
import data.real.basic import game.order.level01 namespace xena -- hide /- # Chapter 2 : Order ## Level 2 This level invites you to work out a property of the absolute value. In Lean the absolute value of $x$ is denoted by `abs x`. -/ /- Hint : The definition of the absolute value in mathlib: definition abs {α : Type u} [decidable_linear_ordered_add_comm_group α] (a : α) : α := max a (-a) -/ /- For ease of use, a notation can be wrapped around that definition as below. -/ notation `|` x `|` := abs x /- Lemma For any two real numbers $a$ and $b$, we have that $$|ab| = |a||b|$$. -/ theorem abs_prod (a b : ℝ) : |a * b| = |a| * |b| := begin rcases lt_trichotomy a 0 with haNeg | haZero | haPos, swap, { -- case a = 0 have h1 : a * b = 0, norm_num, left, exact haZero, have h2 : | a * b | = 0, exact (is_absolute_value.abv_eq_zero abs).mpr h1, have h3 : | a | = 0, exact (is_absolute_value.abv_eq_zero abs).mpr haZero, rw [h2,h3], norm_num, }, { -- case a < 0 rcases lt_trichotomy b 0 with hbNeg | hbZero | hbPos, swap, { -- case b = 0 have h1 : a * b = 0, norm_num, right, exact hbZero, have h2 : | a * b | = 0, exact (is_absolute_value.abv_eq_zero abs).mpr h1, have h3 : | b | = 0, exact (is_absolute_value.abv_eq_zero abs).mpr hbZero, rw [h2,h3], norm_num, }, { -- case b < 0 have h1 : 0 < a * b, exact mul_pos_of_neg_of_neg haNeg hbNeg, have h2 : | a * b | = a * b, exact abs_of_pos h1, have h3 : | a | = - a, exact abs_of_neg haNeg, have h4 : | b | = - b, exact abs_of_neg hbNeg, rw [h2, h3, h4], norm_num, }, { -- case 0 < b have h1 : a * b < 0, exact mul_neg_of_neg_of_pos haNeg hbPos, have h2 : | a * b | = - (a * b), exact abs_of_neg h1, have h3 : | a | = - a, exact abs_of_neg haNeg, have h4 : | b | = b, exact abs_of_pos hbPos, rw [h2, h3, h4], norm_num, } }, { -- case 0 < a rcases lt_trichotomy b 0 with hbNeg | hbZero | hbPos, swap, { -- case b = 0 have h1 : a * b = 0, norm_num, right, exact hbZero, have h2 : | a * b | = 0, exact (is_absolute_value.abv_eq_zero abs).mpr h1, have h3 : | b | = 0, exact (is_absolute_value.abv_eq_zero abs).mpr hbZero, rw [h2,h3], norm_num, }, { -- case b < 0 have h1 : a * b < 0, exact mul_neg_of_pos_of_neg haPos hbNeg, have h2 : | a * b | = -( a * b), exact abs_of_neg h1, have h3 : | a | = a, exact abs_of_pos haPos, have h4 : | b | = - b, exact abs_of_neg hbNeg, rw [h2, h3, h4], norm_num, }, { -- case 0 < b have h1 : 0 < a * b, exact mul_pos haPos hbPos, have h2 : | a * b | = a * b, exact abs_of_pos h1, have h3 : | a | = a, exact abs_of_pos haPos, have h4 : | b | = b, exact abs_of_pos hbPos, rw [h2, h3, h4], -- this is enough, rw closes the refl goal } }, done end end xena --hide
38032a107f99534224a6bc042a13baabefb8dae1
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/data/fintype.lean
cf918c46a79d5e6d368667f162eab251fc5d35e6
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
38,515
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Finite types. -/ import data.finset data.array.lemmas logic.unique import tactic.wlog universes u v variables {α : Type*} {β : Type*} {γ : Type*} /-- `fintype α` means that `α` is finite, i.e. there are only finitely many distinct elements of type `α`. The evidence of this is a finset `elems` (a list up to permutation without duplicates), together with a proof that everything of type `α` is in the list. -/ class fintype (α : Type*) := (elems : finset α) (complete : ∀ x : α, x ∈ elems) namespace finset variable [fintype α] /-- `univ` is the universal finite set of type `finset α` implied from the assumption `fintype α`. -/ def univ : finset α := fintype.elems α @[simp] theorem mem_univ (x : α) : x ∈ (univ : finset α) := fintype.complete x @[simp] theorem mem_univ_val : ∀ x, x ∈ (univ : finset α).1 := mem_univ @[simp] lemma coe_univ : ↑(univ : finset α) = (set.univ : set α) := by ext; simp theorem subset_univ (s : finset α) : s ⊆ univ := λ a _, mem_univ a theorem eq_univ_iff_forall {s : finset α} : s = univ ↔ ∀ x, x ∈ s := by simp [ext] @[simp] lemma piecewise_univ [∀i : α, decidable (i ∈ (univ : finset α))] {δ : α → Sort*} (f g : Πi, δ i) : univ.piecewise f g = f := by { ext i, simp [piecewise] } end finset open finset function namespace fintype instance decidable_pi_fintype {α} {β : α → Type*} [∀a, decidable_eq (β a)] [fintype α] : decidable_eq (Πa, β a) := assume f g, decidable_of_iff (∀ a ∈ fintype.elems α, f a = g a) (by simp [function.funext_iff, fintype.complete]) instance decidable_forall_fintype {p : α → Prop} [decidable_pred p] [fintype α] : decidable (∀ a, p a) := decidable_of_iff (∀ a ∈ @univ α _, p a) (by simp) instance decidable_exists_fintype {p : α → Prop} [decidable_pred p] [fintype α] : decidable (∃ a, p a) := decidable_of_iff (∃ a ∈ @univ α _, p a) (by simp) instance decidable_eq_equiv_fintype [decidable_eq β] [fintype α] : decidable_eq (α ≃ β) := λ a b, decidable_of_iff (a.1 = b.1) ⟨λ h, equiv.ext _ _ (congr_fun h), congr_arg _⟩ instance decidable_injective_fintype [decidable_eq α] [decidable_eq β] [fintype α] : decidable_pred (injective : (α → β) → Prop) := λ x, by unfold injective; apply_instance instance decidable_surjective_fintype [decidable_eq β] [fintype α] [fintype β] : decidable_pred (surjective : (α → β) → Prop) := λ x, by unfold surjective; apply_instance instance decidable_bijective_fintype [decidable_eq α] [decidable_eq β] [fintype α] [fintype β] : decidable_pred (bijective : (α → β) → Prop) := λ x, by unfold bijective; apply_instance instance decidable_left_inverse_fintype [decidable_eq α] [fintype α] (f : α → β) (g : β → α) : decidable (function.right_inverse f g) := show decidable (∀ x, g (f x) = x), by apply_instance instance decidable_right_inverse_fintype [decidable_eq β] [fintype β] (f : α → β) (g : β → α) : decidable (function.left_inverse f g) := show decidable (∀ x, f (g x) = x), by apply_instance /-- Construct a proof of `fintype α` from a universal multiset -/ def of_multiset [decidable_eq α] (s : multiset α) (H : ∀ x : α, x ∈ s) : fintype α := ⟨s.to_finset, by simpa using H⟩ /-- Construct a proof of `fintype α` from a universal list -/ def of_list [decidable_eq α] (l : list α) (H : ∀ x : α, x ∈ l) : fintype α := ⟨l.to_finset, by simpa using H⟩ theorem exists_univ_list (α) [fintype α] : ∃ l : list α, l.nodup ∧ ∀ x : α, x ∈ l := let ⟨l, e⟩ := quotient.exists_rep (@univ α _).1 in by have := and.intro univ.2 mem_univ_val; exact ⟨_, by rwa ← e at this⟩ /-- `card α` is the number of elements in `α`, defined when `α` is a fintype. -/ def card (α) [fintype α] : ℕ := (@univ α _).card /-- If `l` lists all the elements of `α` without duplicates, then `α ≃ fin (l.length)`. -/ def equiv_fin_of_forall_mem_list {α} [decidable_eq α] {l : list α} (h : ∀ x:α, x ∈ l) (nd : l.nodup) : α ≃ fin (l.length) := ⟨λ a, ⟨_, list.index_of_lt_length.2 (h a)⟩, λ i, l.nth_le i.1 i.2, λ a, by simp, λ ⟨i, h⟩, fin.eq_of_veq $ list.nodup_iff_nth_le_inj.1 nd _ _ (list.index_of_lt_length.2 (list.nth_le_mem _ _ _)) h $ by simp⟩ /-- There is (computably) a bijection between `α` and `fin n` where `n = card α`. Since it is not unique, and depends on which permutation of the universe list is used, the bijection is wrapped in `trunc` to preserve computability. -/ def equiv_fin (α) [fintype α] [decidable_eq α] : trunc (α ≃ fin (card α)) := by unfold card finset.card; exact quot.rec_on_subsingleton (@univ α _).1 (λ l (h : ∀ x:α, x ∈ l) (nd : l.nodup), trunc.mk (equiv_fin_of_forall_mem_list h nd)) mem_univ_val univ.2 theorem exists_equiv_fin (α) [fintype α] : ∃ n, nonempty (α ≃ fin n) := by haveI := classical.dec_eq α; exact ⟨card α, nonempty_of_trunc (equiv_fin α)⟩ /-- Given a linearly ordered fintype `α` of cardinal `k`, the equiv `mono_equiv_of_fin α h` is the increasing bijection between `fin k` and `α`. Here, `h` is a proof that the cardinality of `s` is `k`. We use this instead of a map `fin s.card → α` to avoid casting issues in further uses of this function. -/ noncomputable def mono_equiv_of_fin (α) [fintype α] [decidable_linear_order α] {k : ℕ} (h : fintype.card α = k) : fin k ≃ α := have A : bijective (mono_of_fin univ h) := begin apply set.bijective_iff_bij_on_univ.2, rw ← @coe_univ α _, exact bij_on_mono_of_fin (univ : finset α) h end, equiv.of_bijective A instance (α : Type*) : subsingleton (fintype α) := ⟨λ ⟨s₁, h₁⟩ ⟨s₂, h₂⟩, by congr; simp [finset.ext, h₁, h₂]⟩ protected def subtype {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) : fintype {x // p x} := ⟨⟨multiset.pmap subtype.mk s.1 (λ x, (H x).1), multiset.nodup_pmap (λ a _ b _, congr_arg subtype.val) s.2⟩, λ ⟨x, px⟩, multiset.mem_pmap.2 ⟨x, (H x).2 px, rfl⟩⟩ theorem subtype_card {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) : @card {x // p x} (fintype.subtype s H) = s.card := multiset.card_pmap _ _ _ theorem card_of_subtype {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) [fintype {x // p x}] : card {x // p x} = s.card := by rw ← subtype_card s H; congr /-- Construct a fintype from a finset with the same elements. -/ def of_finset {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : fintype p := fintype.subtype s H @[simp] theorem card_of_finset {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : @fintype.card p (of_finset s H) = s.card := fintype.subtype_card s H theorem card_of_finset' {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) [fintype p] : fintype.card p = s.card := by rw ← card_of_finset s H; congr /-- If `f : α → β` is a bijection and `α` is a fintype, then `β` is also a fintype. -/ def of_bijective [fintype α] (f : α → β) (H : function.bijective f) : fintype β := ⟨univ.map ⟨f, H.1⟩, λ b, let ⟨a, e⟩ := H.2 b in e ▸ mem_map_of_mem _ (mem_univ _)⟩ /-- If `f : α → β` is a surjection and `α` is a fintype, then `β` is also a fintype. -/ def of_surjective [fintype α] [decidable_eq β] (f : α → β) (H : function.surjective f) : fintype β := ⟨univ.image f, λ b, let ⟨a, e⟩ := H b in e ▸ mem_image_of_mem _ (mem_univ _)⟩ noncomputable def of_injective [fintype β] (f : α → β) (H : function.injective f) : fintype α := by letI := classical.dec; exact if hα : nonempty α then by letI := classical.inhabited_of_nonempty hα; exact of_surjective (inv_fun f) (inv_fun_surjective H) else ⟨∅, λ x, (hα ⟨x⟩).elim⟩ /-- If `f : α ≃ β` and `α` is a fintype, then `β` is also a fintype. -/ def of_equiv (α : Type*) [fintype α] (f : α ≃ β) : fintype β := of_bijective _ f.bijective theorem of_equiv_card [fintype α] (f : α ≃ β) : @card β (of_equiv α f) = card α := multiset.card_map _ _ theorem card_congr {α β} [fintype α] [fintype β] (f : α ≃ β) : card α = card β := by rw ← of_equiv_card f; congr theorem card_eq {α β} [F : fintype α] [G : fintype β] : card α = card β ↔ nonempty (α ≃ β) := ⟨λ h, ⟨by classical; calc α ≃ fin (card α) : trunc.out (equiv_fin α) ... ≃ fin (card β) : by rw h ... ≃ β : (trunc.out (equiv_fin β)).symm⟩, λ ⟨f⟩, card_congr f⟩ def of_subsingleton (a : α) [subsingleton α] : fintype α := ⟨finset.singleton a, λ b, finset.mem_singleton.2 (subsingleton.elim _ _)⟩ @[simp] theorem univ_of_subsingleton (a : α) [subsingleton α] : @univ _ (of_subsingleton a) = finset.singleton a := rfl @[simp] theorem card_of_subsingleton (a : α) [subsingleton α] : @fintype.card _ (of_subsingleton a) = 1 := rfl end fintype namespace set /-- Construct a finset enumerating a set `s`, given a `fintype` instance. -/ def to_finset (s : set α) [fintype s] : finset α := ⟨(@finset.univ s _).1.map subtype.val, multiset.nodup_map (λ a b, subtype.eq) finset.univ.2⟩ @[simp] theorem mem_to_finset {s : set α} [fintype s] {a : α} : a ∈ s.to_finset ↔ a ∈ s := by simp [to_finset] @[simp] theorem mem_to_finset_val {s : set α} [fintype s] {a : α} : a ∈ s.to_finset.1 ↔ a ∈ s := mem_to_finset end set lemma finset.card_univ [fintype α] : (finset.univ : finset α).card = fintype.card α := rfl lemma finset.card_univ_diff [fintype α] [decidable_eq α] (s : finset α) : (finset.univ \ s).card = fintype.card α - s.card := finset.card_sdiff (subset_univ s) instance (n : ℕ) : fintype (fin n) := ⟨⟨list.fin_range n, list.nodup_fin_range n⟩, list.mem_fin_range⟩ @[simp] theorem fintype.card_fin (n : ℕ) : fintype.card (fin n) = n := list.length_fin_range n @[simp] lemma finset.card_fin (n : ℕ) : finset.card (finset.univ : finset (fin n)) = n := by rw [finset.card_univ, fintype.card_fin] lemma fin.univ_succ (n : ℕ) : (univ : finset (fin $ n+1)) = insert 0 (univ.image fin.succ) := begin ext m, simp only [mem_univ, mem_insert, true_iff, mem_image, exists_prop], exact fin.cases (or.inl rfl) (λ i, or.inr ⟨i, trivial, rfl⟩) m end lemma fin.univ_cast_succ (n : ℕ) : (univ : finset (fin $ n+1)) = insert (fin.last n) (univ.image fin.cast_succ) := begin ext m, simp only [mem_univ, mem_insert, true_iff, mem_image, exists_prop, true_and], by_cases h : m.val < n, { right, use fin.cast_lt m h, rw fin.cast_succ_cast_lt }, { left, exact fin.eq_last_of_not_lt h } end @[instance, priority 10] def unique.fintype {α : Type*} [unique α] : fintype α := ⟨finset.singleton (default α), λ x, by rw [unique.eq_default x]; simp⟩ @[simp] lemma univ_unique {α : Type*} [unique α] [f : fintype α] : @finset.univ α _ = {default α} := by rw [subsingleton.elim f (@unique.fintype α _)]; refl instance : fintype empty := ⟨∅, empty.rec _⟩ @[simp] theorem fintype.univ_empty : @univ empty _ = ∅ := rfl @[simp] theorem fintype.card_empty : fintype.card empty = 0 := rfl instance : fintype pempty := ⟨∅, pempty.rec _⟩ @[simp] theorem fintype.univ_pempty : @univ pempty _ = ∅ := rfl @[simp] theorem fintype.card_pempty : fintype.card pempty = 0 := rfl instance : fintype unit := fintype.of_subsingleton () theorem fintype.univ_unit : @univ unit _ = {()} := rfl theorem fintype.card_unit : fintype.card unit = 1 := rfl instance : fintype punit := fintype.of_subsingleton punit.star @[simp] theorem fintype.univ_punit : @univ punit _ = {punit.star} := rfl @[simp] theorem fintype.card_punit : fintype.card punit = 1 := rfl instance : fintype bool := ⟨⟨tt::ff::0, by simp⟩, λ x, by cases x; simp⟩ @[simp] theorem fintype.univ_bool : @univ bool _ = {ff, tt} := rfl instance units_int.fintype : fintype (units ℤ) := ⟨{1, -1}, λ x, by cases int.units_eq_one_or x; simp *⟩ instance additive.fintype : Π [fintype α], fintype (additive α) := id instance multiplicative.fintype : Π [fintype α], fintype (multiplicative α) := id @[simp] theorem fintype.card_units_int : fintype.card (units ℤ) = 2 := rfl noncomputable instance [monoid α] [fintype α] : fintype (units α) := by classical; exact fintype.of_injective units.val units.ext @[simp] theorem fintype.card_bool : fintype.card bool = 2 := rfl def finset.insert_none (s : finset α) : finset (option α) := ⟨none :: s.1.map some, multiset.nodup_cons.2 ⟨by simp, multiset.nodup_map (λ a b, option.some.inj) s.2⟩⟩ @[simp] theorem finset.mem_insert_none {s : finset α} : ∀ {o : option α}, o ∈ s.insert_none ↔ ∀ a ∈ o, a ∈ s | none := iff_of_true (multiset.mem_cons_self _ _) (λ a h, by cases h) | (some a) := multiset.mem_cons.trans $ by simp; refl theorem finset.some_mem_insert_none {s : finset α} {a : α} : some a ∈ s.insert_none ↔ a ∈ s := by simp instance {α : Type*} [fintype α] : fintype (option α) := ⟨univ.insert_none, λ a, by simp⟩ @[simp] theorem fintype.card_option {α : Type*} [fintype α] : fintype.card (option α) = fintype.card α + 1 := (multiset.card_cons _ _).trans (by rw multiset.card_map; refl) instance {α : Type*} (β : α → Type*) [fintype α] [∀ a, fintype (β a)] : fintype (sigma β) := ⟨univ.sigma (λ _, univ), λ ⟨a, b⟩, by simp⟩ instance (α β : Type*) [fintype α] [fintype β] : fintype (α × β) := ⟨univ.product univ, λ ⟨a, b⟩, by simp⟩ @[simp] theorem fintype.card_prod (α β : Type*) [fintype α] [fintype β] : fintype.card (α × β) = fintype.card α * fintype.card β := card_product _ _ def fintype.fintype_prod_left {α β} [decidable_eq α] [fintype (α × β)] [nonempty β] : fintype α := ⟨(fintype.elems (α × β)).image prod.fst, assume a, let ⟨b⟩ := ‹nonempty β› in by simp; exact ⟨b, fintype.complete _⟩⟩ def fintype.fintype_prod_right {α β} [decidable_eq β] [fintype (α × β)] [nonempty α] : fintype β := ⟨(fintype.elems (α × β)).image prod.snd, assume b, let ⟨a⟩ := ‹nonempty α› in by simp; exact ⟨a, fintype.complete _⟩⟩ instance (α : Type*) [fintype α] : fintype (ulift α) := fintype.of_equiv _ equiv.ulift.symm @[simp] theorem fintype.card_ulift (α : Type*) [fintype α] : fintype.card (ulift α) = fintype.card α := fintype.of_equiv_card _ instance (α : Type u) (β : Type v) [fintype α] [fintype β] : fintype (α ⊕ β) := @fintype.of_equiv _ _ (@sigma.fintype _ (λ b, cond b (ulift α) (ulift.{(max u v) v} β)) _ (λ b, by cases b; apply ulift.fintype)) ((equiv.sum_equiv_sigma_bool _ _).symm.trans (equiv.sum_congr equiv.ulift equiv.ulift)) lemma fintype.card_le_of_injective [fintype α] [fintype β] (f : α → β) (hf : function.injective f) : fintype.card α ≤ fintype.card β := by haveI := classical.prop_decidable; exact finset.card_le_card_of_inj_on f (λ _ _, finset.mem_univ _) (λ _ _ _ _ h, hf h) lemma fintype.card_eq_one_iff [fintype α] : fintype.card α = 1 ↔ (∃ x : α, ∀ y, y = x) := by rw [← fintype.card_unit, fintype.card_eq]; exact ⟨λ ⟨a⟩, ⟨a.symm (), λ y, a.injective (subsingleton.elim _ _)⟩, λ ⟨x, hx⟩, ⟨⟨λ _, (), λ _, x, λ _, (hx _).trans (hx _).symm, λ _, subsingleton.elim _ _⟩⟩⟩ lemma fintype.card_eq_zero_iff [fintype α] : fintype.card α = 0 ↔ (α → false) := ⟨λ h a, have e : α ≃ empty := classical.choice (fintype.card_eq.1 (by simp [h])), (e a).elim, λ h, have e : α ≃ empty := ⟨λ a, (h a).elim, λ a, a.elim, λ a, (h a).elim, λ a, a.elim⟩, by simp [fintype.card_congr e]⟩ lemma fintype.card_pos_iff [fintype α] : 0 < fintype.card α ↔ nonempty α := ⟨λ h, classical.by_contradiction (λ h₁, have fintype.card α = 0 := fintype.card_eq_zero_iff.2 (λ a, h₁ ⟨a⟩), lt_irrefl 0 $ by rwa this at h), λ ⟨a⟩, nat.pos_of_ne_zero (mt fintype.card_eq_zero_iff.1 (λ h, h a))⟩ lemma fintype.card_le_one_iff [fintype α] : fintype.card α ≤ 1 ↔ (∀ a b : α, a = b) := let n := fintype.card α in have hn : n = fintype.card α := rfl, match n, hn with | 0 := λ ha, ⟨λ h, λ a, (fintype.card_eq_zero_iff.1 ha.symm a).elim, λ _, ha ▸ nat.le_succ _⟩ | 1 := λ ha, ⟨λ h, λ a b, let ⟨x, hx⟩ := fintype.card_eq_one_iff.1 ha.symm in by rw [hx a, hx b], λ _, ha ▸ le_refl _⟩ | (n+2) := λ ha, ⟨λ h, by rw ← ha at h; exact absurd h dec_trivial, (λ h, fintype.card_unit ▸ fintype.card_le_of_injective (λ _, ()) (λ _ _ _, h _ _))⟩ end lemma fintype.exists_ne_of_one_lt_card [fintype α] (h : 1 < fintype.card α) (a : α) : ∃ b : α, b ≠ a := let ⟨b, hb⟩ := classical.not_forall.1 (mt fintype.card_le_one_iff.2 (not_le_of_gt h)) in let ⟨c, hc⟩ := classical.not_forall.1 hb in by haveI := classical.dec_eq α; exact if hba : b = a then ⟨c, by cc⟩ else ⟨b, hba⟩ lemma fintype.injective_iff_surjective [fintype α] {f : α → α} : injective f ↔ surjective f := by haveI := classical.prop_decidable; exact have ∀ {f : α → α}, injective f → surjective f, from λ f hinj x, have h₁ : image f univ = univ := eq_of_subset_of_card_le (subset_univ _) ((card_image_of_injective univ hinj).symm ▸ le_refl _), have h₂ : x ∈ image f univ := h₁.symm ▸ mem_univ _, exists_of_bex (mem_image.1 h₂), ⟨this, λ hsurj, injective_of_has_left_inverse ⟨surj_inv hsurj, left_inverse_of_surjective_of_right_inverse (this (injective_surj_inv _)) (right_inverse_surj_inv _)⟩⟩ lemma fintype.injective_iff_bijective [fintype α] {f : α → α} : injective f ↔ bijective f := by simp [bijective, fintype.injective_iff_surjective] lemma fintype.surjective_iff_bijective [fintype α] {f : α → α} : surjective f ↔ bijective f := by simp [bijective, fintype.injective_iff_surjective] lemma fintype.injective_iff_surjective_of_equiv [fintype α] {f : α → β} (e : α ≃ β) : injective f ↔ surjective f := have injective (e.symm ∘ f) ↔ surjective (e.symm ∘ f), from fintype.injective_iff_surjective, ⟨λ hinj, by simpa [function.comp] using surjective_comp e.surjective (this.1 (injective_comp e.symm.injective hinj)), λ hsurj, by simpa [function.comp] using injective_comp e.injective (this.2 (surjective_comp e.symm.surjective hsurj))⟩ instance list.subtype.fintype [decidable_eq α] (l : list α) : fintype {x // x ∈ l} := fintype.of_list l.attach l.mem_attach instance multiset.subtype.fintype [decidable_eq α] (s : multiset α) : fintype {x // x ∈ s} := fintype.of_multiset s.attach s.mem_attach instance finset.subtype.fintype (s : finset α) : fintype {x // x ∈ s} := ⟨s.attach, s.mem_attach⟩ instance finset_coe.fintype (s : finset α) : fintype (↑s : set α) := finset.subtype.fintype s @[simp] lemma fintype.card_coe (s : finset α) : fintype.card (↑s : set α) = s.card := card_attach lemma finset.card_le_one_iff {s : finset α} : s.card ≤ 1 ↔ ∀ {x y}, x ∈ s → y ∈ s → x = y := begin let t : set α := ↑s, letI : fintype t := finset_coe.fintype s, have : fintype.card t = s.card := fintype.card_coe s, rw [← this, fintype.card_le_one_iff], split, { assume H x y hx hy, exact subtype.mk.inj (H ⟨x, hx⟩ ⟨y, hy⟩) }, { assume H x y, exact subtype.eq (H x.2 y.2) } end lemma finset.one_lt_card_iff {s : finset α} : 1 < s.card ↔ ∃ x y, (x ∈ s) ∧ (y ∈ s) ∧ x ≠ y := begin classical, rw ← not_iff_not, push_neg, simpa [classical.or_iff_not_imp_left] using finset.card_le_one_iff end instance plift.fintype (p : Prop) [decidable p] : fintype (plift p) := ⟨if h : p then finset.singleton ⟨h⟩ else ∅, λ ⟨h⟩, by simp [h]⟩ instance Prop.fintype : fintype Prop := ⟨⟨true::false::0, by simp [true_ne_false]⟩, classical.cases (by simp) (by simp)⟩ def set_fintype {α} [fintype α] (s : set α) [decidable_pred s] : fintype s := fintype.subtype (univ.filter (∈ s)) (by simp) /-! ### pi -/ /-- A dependent product of fintypes, indexed by a fintype, is a fintype. -/ instance pi.fintype {α : Type*} {β : α → Type*} [decidable_eq α] [fintype α] [∀a, fintype (β a)] : fintype (Πa, β a) := @fintype.of_equiv _ _ ⟨univ.pi $ λa:α, @univ (β a) _, λ f, finset.mem_pi.2 $ λ a ha, mem_univ _⟩ ⟨λ f a, f a (mem_univ _), λ f a _, f a, λ f, rfl, λ f, rfl⟩ namespace fintype variables [fintype α] [decidable_eq α] {δ : α → Type*} [decidable_eq (Π a, δ a)] /-- Given for all `a : α` a finset `t a` of `δ a`, then one can define the finset `fintype.pi_finset t` of all functions taking values in `t a` for all `a`. This is the analogue of `finset.pi` where the base finset is `univ` (but formally they are not the same, as there is an additional condition `i ∈ finset.univ` in the `finset.pi` definition). -/ def pi_finset (t : Πa, finset (δ a)) : finset (Πa, δ a) := (finset.univ.pi t).image (λ f a, f a (mem_univ a)) @[simp] lemma mem_pi_finset {t : Πa, finset (δ a)} {f : Πa, δ a} : f ∈ pi_finset t ↔ (∀a, f a ∈ t a) := begin split, { simp only [pi_finset, mem_image, and_imp, forall_prop_of_true, exists_prop, mem_univ, exists_imp_distrib, mem_pi], assume g hg hgf a, rw ← hgf, exact hg a }, { simp only [pi_finset, mem_image, forall_prop_of_true, exists_prop, mem_univ, mem_pi], assume hf, exact ⟨λ a ha, f a, hf, rfl⟩ } end lemma pi_finset_subset (t₁ t₂ : Πa, finset (δ a)) (h : ∀ a, t₁ a ⊆ t₂ a) : pi_finset t₁ ⊆ pi_finset t₂ := λ g hg, mem_pi_finset.2 $ λ a, h a $ mem_pi_finset.1 hg a lemma pi_finset_disjoint_of_disjoint [∀ a, decidable_eq (δ a)] (t₁ t₂ : Πa, finset (δ a)) {a : α} (h : disjoint (t₁ a) (t₂ a)) : disjoint (pi_finset t₁) (pi_finset t₂) := disjoint_iff_ne.2 $ λ f₁ hf₁ f₂ hf₂ eq₁₂, disjoint_iff_ne.1 h (f₁ a) (mem_pi_finset.1 hf₁ a) (f₂ a) (mem_pi_finset.1 hf₂ a) (congr_fun eq₁₂ a) @[simp] lemma pi_finset_univ [∀ a, fintype (δ a)]: pi_finset (λ a : α, (finset.univ : finset (δ a))) = (finset.univ : finset (Π a, δ a)) := by { ext f, simp } end fintype instance d_array.fintype {n : ℕ} {α : fin n → Type*} [∀n, fintype (α n)] : fintype (d_array n α) := fintype.of_equiv _ (equiv.d_array_equiv_fin _).symm instance array.fintype {n : ℕ} {α : Type*} [fintype α] : fintype (array n α) := d_array.fintype instance vector.fintype {α : Type*} [fintype α] {n : ℕ} : fintype (vector α n) := fintype.of_equiv _ (equiv.vector_equiv_fin _ _).symm instance quotient.fintype [fintype α] (s : setoid α) [decidable_rel ((≈) : α → α → Prop)] : fintype (quotient s) := fintype.of_surjective quotient.mk (λ x, quotient.induction_on x (λ x, ⟨x, rfl⟩)) instance finset.fintype [fintype α] : fintype (finset α) := ⟨univ.powerset, λ x, finset.mem_powerset.2 (finset.subset_univ _)⟩ @[simp] lemma fintype.card_finset [fintype α] : fintype.card (finset α) = 2 ^ (fintype.card α) := finset.card_powerset finset.univ instance subtype.fintype (p : α → Prop) [decidable_pred p] [fintype α] : fintype {x // p x} := set_fintype _ theorem fintype.card_subtype_le [fintype α] (p : α → Prop) [decidable_pred p] : fintype.card {x // p x} ≤ fintype.card α := by rw fintype.subtype_card; exact card_le_of_subset (subset_univ _) theorem fintype.card_subtype_lt [fintype α] {p : α → Prop} [decidable_pred p] {x : α} (hx : ¬ p x) : fintype.card {x // p x} < fintype.card α := by rw [fintype.subtype_card]; exact finset.card_lt_card ⟨subset_univ _, classical.not_forall.2 ⟨x, by simp [*, set.mem_def]⟩⟩ instance psigma.fintype {α : Type*} {β : α → Type*} [fintype α] [∀ a, fintype (β a)] : fintype (Σ' a, β a) := fintype.of_equiv _ (equiv.psigma_equiv_sigma _).symm instance psigma.fintype_prop_left {α : Prop} {β : α → Type*} [decidable α] [∀ a, fintype (β a)] : fintype (Σ' a, β a) := if h : α then fintype.of_equiv (β h) ⟨λ x, ⟨h, x⟩, psigma.snd, λ _, rfl, λ ⟨_, _⟩, rfl⟩ else ⟨∅, λ x, h x.1⟩ instance psigma.fintype_prop_right {α : Type*} {β : α → Prop} [∀ a, decidable (β a)] [fintype α] : fintype (Σ' a, β a) := fintype.of_equiv {a // β a} ⟨λ ⟨x, y⟩, ⟨x, y⟩, λ ⟨x, y⟩, ⟨x, y⟩, λ ⟨x, y⟩, rfl, λ ⟨x, y⟩, rfl⟩ instance psigma.fintype_prop_prop {α : Prop} {β : α → Prop} [decidable α] [∀ a, decidable (β a)] : fintype (Σ' a, β a) := if h : ∃ a, β a then ⟨{⟨h.fst, h.snd⟩}, λ ⟨_, _⟩, by simp⟩ else ⟨∅, λ ⟨x, y⟩, h ⟨x, y⟩⟩ instance set.fintype [decidable_eq α] [fintype α] : fintype (set α) := pi.fintype instance pfun_fintype (p : Prop) [decidable p] (α : p → Type*) [Π hp, fintype (α hp)] : fintype (Π hp : p, α hp) := if hp : p then fintype.of_equiv (α hp) ⟨λ a _, a, λ f, f hp, λ _, rfl, λ _, rfl⟩ else ⟨singleton (λ h, (hp h).elim), by simp [hp, function.funext_iff]⟩ 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 def quotient.fin_choice {ι : Type*} [fintype ι] [decidable_eq ι] {α : ι → 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*} [fintype ι] [decidable_eq ι] {α : ι → Type*} [∀ i, setoid (α i)] (f : ∀ i, α i) : quotient.fin_choice (λ i, ⟦f i⟧) = ⟦f⟧ := begin let q, swap, change quotient.lift_on q _ _ = _, have : q = ⟦λ i h, f i⟧, { dsimp [q], exact quotient.induction_on (@finset.univ ι _).1 (λ l, quotient.fin_choice_aux_eq _ _) }, simp [this], exact setoid.refl _ end section equiv open list equiv equiv.perm variables [decidable_eq α] [decidable_eq β] def perms_of_list : list α → list (perm α) | [] := [1] | (a :: l) := perms_of_list l ++ l.bind (λ b, (perms_of_list l).map (λ f, swap a b * f)) lemma length_perms_of_list : ∀ l : list α, length (perms_of_list l) = l.length.fact | [] := rfl | (a :: l) := begin rw [length_cons, nat.fact_succ], simp [perms_of_list, length_bind, length_perms_of_list, function.comp, nat.succ_mul], cc end lemma mem_perms_of_list_of_mem : ∀ {l : list α} {f : perm α} (h : ∀ x, f x ≠ x → x ∈ l), f ∈ perms_of_list l | [] f h := list.mem_singleton.2 $ equiv.ext _ _$ λ x, by simp [imp_false, *] at * | (a::l) f h := if hfa : f a = a then mem_append_left _ $ mem_perms_of_list_of_mem (λ x hx, mem_of_ne_of_mem (λ h, by rw h at hx; exact hx hfa) (h x hx)) else have hfa' : f (f a) ≠ f a, from mt (λ h, f.injective h) hfa, have ∀ (x : α), (swap a (f a) * f) x ≠ x → x ∈ l, from λ x hx, have hxa : x ≠ a, from λ h, by simpa [h, mul_apply] using hx, have hfxa : f x ≠ f a, from mt (λ h, f.injective h) hxa, list.mem_of_ne_of_mem hxa (h x (λ h, by simp [h, mul_apply, swap_apply_def] at hx; split_ifs at hx; cc)), suffices f ∈ perms_of_list l ∨ ∃ (b : α), b ∈ l ∧ ∃ g : perm α, g ∈ perms_of_list l ∧ swap a b * g = f, by simpa [perms_of_list], (@or_iff_not_imp_left _ _ (classical.prop_decidable _)).2 (λ hfl, ⟨f a, if hffa : f (f a) = a then mem_of_ne_of_mem hfa (h _ (mt (λ h, f.injective h) hfa)) else this _ $ by simp [mul_apply, swap_apply_def]; split_ifs; cc, ⟨swap a (f a) * f, mem_perms_of_list_of_mem this, by rw [← mul_assoc, mul_def (swap a (f a)) (swap a (f a)), swap_swap, ← equiv.perm.one_def, one_mul]⟩⟩) lemma mem_of_mem_perms_of_list : ∀ {l : list α} {f : perm α}, f ∈ perms_of_list l → ∀ {x}, f x ≠ x → x ∈ l | [] f h := have f = 1 := by simpa [perms_of_list] using h, by rw this; simp | (a::l) f h := (mem_append.1 h).elim (λ h x hx, mem_cons_of_mem _ (mem_of_mem_perms_of_list h hx)) (λ h x hx, let ⟨y, hy, hy'⟩ := list.mem_bind.1 h in let ⟨g, hg₁, hg₂⟩ := list.mem_map.1 hy' in if hxa : x = a then by simp [hxa] else if hxy : x = y then mem_cons_of_mem _ $ by rwa hxy else mem_cons_of_mem _ $ mem_of_mem_perms_of_list hg₁ $ by rw [eq_inv_mul_iff_mul_eq.2 hg₂, mul_apply, swap_inv, swap_apply_def]; split_ifs; cc) lemma mem_perms_of_list_iff {l : list α} {f : perm α} : f ∈ perms_of_list l ↔ ∀ {x}, f x ≠ x → x ∈ l := ⟨mem_of_mem_perms_of_list, mem_perms_of_list_of_mem⟩ lemma nodup_perms_of_list : ∀ {l : list α} (hl : l.nodup), (perms_of_list l).nodup | [] hl := by simp [perms_of_list] | (a::l) hl := have hl' : l.nodup, from nodup_of_nodup_cons hl, have hln' : (perms_of_list l).nodup, from nodup_perms_of_list hl', have hmeml : ∀ {f : perm α}, f ∈ perms_of_list l → f a = a, from λ f hf, not_not.1 (mt (mem_of_mem_perms_of_list hf) (nodup_cons.1 hl).1), by rw [perms_of_list, list.nodup_append, list.nodup_bind, pairwise_iff_nth_le]; exact ⟨hln', ⟨λ _ _, nodup_map (λ _ _, (mul_left_inj _).1) hln', λ i j hj hij x hx₁ hx₂, let ⟨f, hf⟩ := list.mem_map.1 hx₁ in let ⟨g, hg⟩ := list.mem_map.1 hx₂ in have hix : x a = nth_le l i (lt_trans hij hj), by rw [← hf.2, mul_apply, hmeml hf.1, swap_apply_left], have hiy : x a = nth_le l j hj, by rw [← hg.2, mul_apply, hmeml hg.1, swap_apply_left], absurd (hf.2.trans (hg.2.symm)) $ λ h, ne_of_lt hij $ nodup_iff_nth_le_inj.1 hl' i j (lt_trans hij hj) hj $ by rw [← hix, hiy]⟩, λ f hf₁ hf₂, let ⟨x, hx, hx'⟩ := list.mem_bind.1 hf₂ in let ⟨g, hg⟩ := list.mem_map.1 hx' in have hgxa : g⁻¹ x = a, from f.injective $ by rw [hmeml hf₁, ← hg.2]; simp, have hxa : x ≠ a, from λ h, (list.nodup_cons.1 hl).1 (h ▸ hx), (list.nodup_cons.1 hl).1 $ hgxa ▸ mem_of_mem_perms_of_list hg.1 (by rwa [apply_inv_self, hgxa])⟩ def perms_of_finset (s : finset α) : finset (perm α) := quotient.hrec_on s.1 (λ l hl, ⟨perms_of_list l, nodup_perms_of_list hl⟩) (λ a b hab, hfunext (congr_arg _ (quotient.sound hab)) (λ ha hb _, heq_of_eq $ finset.ext.2 $ by simp [mem_perms_of_list_iff,mem_of_perm hab])) s.2 lemma mem_perms_of_finset_iff : ∀ {s : finset α} {f : perm α}, f ∈ perms_of_finset s ↔ ∀ {x}, f x ≠ x → x ∈ s := by rintros ⟨⟨l⟩, hs⟩ f; exact mem_perms_of_list_iff lemma card_perms_of_finset : ∀ (s : finset α), (perms_of_finset s).card = s.card.fact := by rintros ⟨⟨l⟩, hs⟩; exact length_perms_of_list l def fintype_perm [fintype α] : fintype (perm α) := ⟨perms_of_finset (@finset.univ α _), by simp [mem_perms_of_finset_iff]⟩ instance [fintype α] [fintype β] : fintype (α ≃ β) := if h : fintype.card β = fintype.card α then trunc.rec_on_subsingleton (fintype.equiv_fin α) (λ eα, trunc.rec_on_subsingleton (fintype.equiv_fin β) (λ eβ, @fintype.of_equiv _ (perm α) fintype_perm (equiv_congr (equiv.refl α) (eα.trans (eq.rec_on h eβ.symm)) : (α ≃ α) ≃ (α ≃ β)))) else ⟨∅, λ x, false.elim (h (fintype.card_eq.2 ⟨x.symm⟩))⟩ lemma fintype.card_perm [fintype α] : fintype.card (perm α) = (fintype.card α).fact := subsingleton.elim (@fintype_perm α _ _) (@equiv.fintype α α _ _ _ _) ▸ card_perms_of_finset _ lemma fintype.card_equiv [fintype α] [fintype β] (e : α ≃ β) : fintype.card (α ≃ β) = (fintype.card α).fact := fintype.card_congr (equiv_congr (equiv.refl α) e) ▸ fintype.card_perm lemma univ_eq_singleton_of_card_one {α} [fintype α] (x : α) (h : fintype.card α = 1) : (univ : finset α) = finset.singleton x := begin apply symm, apply eq_of_subset_of_card_le (subset_univ (finset.singleton x)), apply le_of_eq, simp [h, finset.card_univ] end end equiv namespace fintype section choose open fintype open equiv variables [fintype α] [decidable_eq α] (p : α → Prop) [decidable_pred p] def choose_x (hp : ∃! a : α, p a) : {a // p a} := ⟨finset.choose p univ (by simp; exact hp), finset.choose_property _ _ _⟩ def choose (hp : ∃! a, p a) : α := choose_x p hp lemma choose_spec (hp : ∃! a, p a) : p (choose p hp) := (choose_x p hp).property end choose section bijection_inverse open function variables [fintype α] [decidable_eq α] variables [fintype β] [decidable_eq β] variables {f : α → β} /-- ` `bij_inv f` is the unique inverse to a bijection `f`. This acts as a computable alternative to `function.inv_fun`. -/ def bij_inv (f_bij : bijective f) (b : β) : α := fintype.choose (λ a, f a = b) begin rcases f_bij.right b with ⟨a', fa_eq_b⟩, rw ← fa_eq_b, exact ⟨a', ⟨rfl, (λ a h, f_bij.left h)⟩⟩ end lemma left_inverse_bij_inv (f_bij : bijective f) : left_inverse (bij_inv f_bij) f := λ a, f_bij.left (choose_spec (λ a', f a' = f a) _) lemma right_inverse_bij_inv (f_bij : bijective f) : right_inverse (bij_inv f_bij) f := λ b, choose_spec (λ a', f a' = b) _ lemma bijective_bij_inv (f_bij : bijective f) : bijective (bij_inv f_bij) := ⟨injective_of_left_inverse (right_inverse_bij_inv _), surjective_of_has_right_inverse ⟨f, left_inverse_bij_inv _⟩⟩ end bijection_inverse lemma well_founded_of_trans_of_irrefl [fintype α] (r : α → α → Prop) [is_trans α r] [is_irrefl α r] : well_founded r := by classical; exact have ∀ x y, r x y → (univ.filter (λ z, r z x)).card < (univ.filter (λ z, r z y)).card, from λ x y hxy, finset.card_lt_card $ by simp only [finset.lt_iff_ssubset.symm, lt_iff_le_not_le, finset.le_iff_subset, finset.subset_iff, mem_filter, true_and, mem_univ, hxy]; exact ⟨λ z hzx, trans hzx hxy, not_forall_of_exists_not ⟨x, not_imp.2 ⟨hxy, irrefl x⟩⟩⟩, subrelation.wf this (measure_wf _) lemma preorder.well_founded [fintype α] [preorder α] : well_founded ((<) : α → α → Prop) := well_founded_of_trans_of_irrefl _ @[instance, priority 10] lemma linear_order.is_well_order [fintype α] [linear_order α] : is_well_order α (<) := { wf := preorder.well_founded } end fintype class infinite (α : Type*) : Prop := (not_fintype : fintype α → false) @[simp] lemma not_nonempty_fintype {α : Type*} : ¬nonempty (fintype α) ↔ infinite α := ⟨λf, ⟨λ x, f ⟨x⟩⟩, λ⟨f⟩ ⟨x⟩, f x⟩ namespace infinite lemma exists_not_mem_finset [infinite α] (s : finset α) : ∃ x, x ∉ s := classical.not_forall.1 $ λ h, not_fintype ⟨s, h⟩ @[priority 100] -- see Note [lower instance priority] instance nonempty (α : Type*) [infinite α] : nonempty α := nonempty_of_exists (exists_not_mem_finset (∅ : finset α)) lemma of_injective [infinite β] (f : β → α) (hf : injective f) : infinite α := ⟨λ I, by exactI not_fintype (fintype.of_injective f hf)⟩ lemma of_surjective [infinite β] (f : α → β) (hf : surjective f) : infinite α := ⟨λ I, by classical; exactI not_fintype (fintype.of_surjective f hf)⟩ private noncomputable def nat_embedding_aux (α : Type*) [infinite α] : ℕ → α | n := by letI := classical.dec_eq α; exact classical.some (exists_not_mem_finset ((multiset.range n).pmap (λ m (hm : m < n), nat_embedding_aux m) (λ _, multiset.mem_range.1)).to_finset) private lemma nat_embedding_aux_injective (α : Type*) [infinite α] : function.injective (nat_embedding_aux α) := begin assume m n h, letI := classical.dec_eq α, wlog hmlen : m ≤ n using m n, by_contradiction hmn, have hmn : m < n, from lt_of_le_of_ne hmlen hmn, refine (classical.some_spec (exists_not_mem_finset ((multiset.range n).pmap (λ m (hm : m < n), nat_embedding_aux α m) (λ _, multiset.mem_range.1)).to_finset)) _, refine multiset.mem_to_finset.2 (multiset.mem_pmap.2 ⟨m, multiset.mem_range.2 hmn, _⟩), rw [h, nat_embedding_aux] end noncomputable def nat_embedding (α : Type*) [infinite α] : ℕ ↪ α := ⟨_, nat_embedding_aux_injective α⟩ end infinite lemma not_injective_infinite_fintype [infinite α] [fintype β] (f : α → β) : ¬ injective f := assume (hf : injective f), have H : fintype α := fintype.of_injective f hf, infinite.not_fintype H lemma not_surjective_fintype_infinite [fintype α] [infinite β] (f : α → β) : ¬ surjective f := assume (hf : surjective f), have H : infinite α := infinite.of_surjective f hf, @infinite.not_fintype _ H infer_instance instance nat.infinite : infinite ℕ := ⟨λ ⟨s, hs⟩, finset.not_mem_range_self $ s.subset_range_sup_succ (hs _)⟩ instance int.infinite : infinite ℤ := infinite.of_injective int.of_nat (λ _ _, int.of_nat_inj)
49c93c90ccdd7a2a1d2e5161e569e7e9863440a1
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/simple_module.lean
1fee19fed5a386d24b169241598906fc144d9d24
[ "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,396
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import linear_algebra.isomorphisms import order.jordan_holder /-! # Simple Modules > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Main Definitions * `is_simple_module` indicates that a module has no proper submodules (the only submodules are `⊥` and `⊤`). * `is_semisimple_module` indicates that every submodule has a complement, or equivalently, the module is a direct sum of simple modules. * A `division_ring` structure on the endomorphism ring of a simple module. ## Main Results * Schur's Lemma: `bijective_or_eq_zero` shows that a linear map between simple modules is either bijective or 0, leading to a `division_ring` structure on the endomorphism ring. ## TODO * Artin-Wedderburn Theory * Unify with the work on Schur's Lemma in a category theory context -/ variables (R : Type*) [ring R] (M : Type*) [add_comm_group M] [module R M] /-- A module is simple when it has only two submodules, `⊥` and `⊤`. -/ abbreviation is_simple_module := (is_simple_order (submodule R M)) /-- A module is semisimple when every submodule has a complement, or equivalently, the module is a direct sum of simple modules. -/ abbreviation is_semisimple_module := (complemented_lattice (submodule R M)) -- Making this an instance causes the linter to complain of "dangerous instances" theorem is_simple_module.nontrivial [is_simple_module R M] : nontrivial M := ⟨⟨0, begin have h : (⊥ : submodule R M) ≠ ⊤ := bot_ne_top, contrapose! h, ext, simp [submodule.mem_bot,submodule.mem_top, h x], end⟩⟩ variables {R} {M} {m : submodule R M} {N : Type*} [add_comm_group N] [module R N] lemma is_simple_module.congr (l : M ≃ₗ[R] N) [is_simple_module R N] : is_simple_module R M := (submodule.order_iso_map_comap l).is_simple_order theorem is_simple_module_iff_is_atom : is_simple_module R m ↔ is_atom m := begin rw ← set.is_simple_order_Iic_iff_is_atom, apply order_iso.is_simple_order_iff, exact submodule.map_subtype.rel_iso m, end theorem is_simple_module_iff_is_coatom : is_simple_module R (M ⧸ m) ↔ is_coatom m := begin rw ← set.is_simple_order_Ici_iff_is_coatom, apply order_iso.is_simple_order_iff, exact submodule.comap_mkq.rel_iso m, end theorem covby_iff_quot_is_simple {A B : submodule R M} (hAB : A ≤ B) : A ⋖ B ↔ is_simple_module R (B ⧸ submodule.comap B.subtype A) := begin set f : submodule R B ≃o set.Iic B := submodule.map_subtype.rel_iso B with hf, rw [covby_iff_coatom_Iic hAB, is_simple_module_iff_is_coatom, ←order_iso.is_coatom_iff f, hf], simp [-order_iso.is_coatom_iff, submodule.map_subtype.rel_iso, submodule.map_comap_subtype, inf_eq_right.2 hAB], end namespace is_simple_module variable [hm : is_simple_module R m] @[simp] lemma is_atom : is_atom m := is_simple_module_iff_is_atom.1 hm end is_simple_module theorem is_semisimple_of_Sup_simples_eq_top (h : Sup {m : submodule R M | is_simple_module R m} = ⊤) : is_semisimple_module R M := complemented_lattice_of_Sup_atoms_eq_top (by simp_rw [← h, is_simple_module_iff_is_atom]) namespace is_semisimple_module variable [is_semisimple_module R M] theorem Sup_simples_eq_top : Sup {m : submodule R M | is_simple_module R m} = ⊤ := begin simp_rw is_simple_module_iff_is_atom, exact Sup_atoms_eq_top, end instance is_semisimple_submodule {m : submodule R M} : is_semisimple_module R m := begin have f : submodule R m ≃o set.Iic m := submodule.map_subtype.rel_iso m, exact f.complemented_lattice_iff.2 is_modular_lattice.complemented_lattice_Iic, end end is_semisimple_module theorem is_semisimple_iff_top_eq_Sup_simples : Sup {m : submodule R M | is_simple_module R m} = ⊤ ↔ is_semisimple_module R M := ⟨is_semisimple_of_Sup_simples_eq_top, by { introI, exact is_semisimple_module.Sup_simples_eq_top }⟩ namespace linear_map theorem injective_or_eq_zero [is_simple_module R M] (f : M →ₗ[R] N) : function.injective f ∨ f = 0 := begin rw [← ker_eq_bot, ← ker_eq_top], apply eq_bot_or_eq_top, end theorem injective_of_ne_zero [is_simple_module R M] {f : M →ₗ[R] N} (h : f ≠ 0) : function.injective f := f.injective_or_eq_zero.resolve_right h theorem surjective_or_eq_zero [is_simple_module R N] (f : M →ₗ[R] N) : function.surjective f ∨ f = 0 := begin rw [← range_eq_top, ← range_eq_bot, or_comm], apply eq_bot_or_eq_top, end theorem surjective_of_ne_zero [is_simple_module R N] {f : M →ₗ[R] N} (h : f ≠ 0) : function.surjective f := f.surjective_or_eq_zero.resolve_right h /-- **Schur's Lemma** for linear maps between (possibly distinct) simple modules -/ theorem bijective_or_eq_zero [is_simple_module R M] [is_simple_module R N] (f : M →ₗ[R] N) : function.bijective f ∨ f = 0 := begin by_cases h : f = 0, { right, exact h }, exact or.intro_left _ ⟨injective_of_ne_zero h, surjective_of_ne_zero h⟩, end theorem bijective_of_ne_zero [is_simple_module R M] [is_simple_module R N] {f : M →ₗ[R] N} (h : f ≠ 0): function.bijective f := f.bijective_or_eq_zero.resolve_right h theorem is_coatom_ker_of_surjective [is_simple_module R N] {f : M →ₗ[R] N} (hf : function.surjective f) : is_coatom f.ker := begin rw ←is_simple_module_iff_is_coatom, exact is_simple_module.congr (f.quot_ker_equiv_of_surjective hf) end /-- Schur's Lemma makes the endomorphism ring of a simple module a division ring. -/ noncomputable instance _root_.module.End.division_ring [decidable_eq (module.End R M)] [is_simple_module R M] : division_ring (module.End R M) := { inv := λ f, if h : f = 0 then 0 else (linear_map.inverse f (equiv.of_bijective _ (bijective_of_ne_zero h)).inv_fun (equiv.of_bijective _ (bijective_of_ne_zero h)).left_inv (equiv.of_bijective _ (bijective_of_ne_zero h)).right_inv), exists_pair_ne := ⟨0, 1, begin haveI := is_simple_module.nontrivial R M, have h := exists_pair_ne M, contrapose! h, intros x y, simp_rw [ext_iff, one_apply, zero_apply] at h, rw [← h x, h y], end⟩, mul_inv_cancel := begin intros a a0, change (a * (dite _ _ _)) = 1, ext, rw [dif_neg a0, mul_eq_comp, one_apply, comp_apply], exact (equiv.of_bijective _ (bijective_of_ne_zero a0)).right_inv x, end, inv_zero := dif_pos rfl, .. (module.End.ring : ring (module.End R M))} end linear_map instance jordan_holder_module : jordan_holder_lattice (submodule R M) := { is_maximal := (⋖), lt_of_is_maximal := λ x y, covby.lt, sup_eq_of_is_maximal := λ x y z hxz hyz, wcovby.sup_eq hxz.wcovby hyz.wcovby, is_maximal_inf_left_of_is_maximal_sup := λ A B, inf_covby_of_covby_sup_of_covby_sup_left, iso := λ X Y, nonempty $ (X.2 ⧸ X.1.comap X.2.subtype) ≃ₗ[R] Y.2 ⧸ Y.1.comap Y.2.subtype, iso_symm := λ A B ⟨f⟩, ⟨f.symm⟩, iso_trans := λ A B C ⟨f⟩ ⟨g⟩, ⟨f.trans g⟩, second_iso := λ A B h, ⟨by { rw [sup_comm, inf_comm], exact (linear_map.quotient_inf_equiv_sup_quotient B A).symm }⟩}
2d49370adccc1a81d280342b1140a66f34ce035c
8b9f17008684d796c8022dab552e42f0cb6fb347
/hott/algebra/group.hlean
d5cddb2ee567fc4bdca9e721f33dc515f7502f9d
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
18,054
hlean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: algebra.group Authors: Jeremy Avigad, Leonardo de Moura Various multiplicative and additive structures. Partially modeled on Isabelle's library. -/ import algebra.binary open eq is_trunc binary -- note: ⁻¹ will be overloaded namespace path_algebra variable {A : Type} /- overloaded symbols -/ structure has_mul [class] (A : Type) := (mul : A → A → A) structure has_add [class] (A : Type) := (add : A → A → A) structure has_one [class] (A : Type) := (one : A) structure has_zero [class] (A : Type) := (zero : A) structure has_inv [class] (A : Type) := (inv : A → A) structure has_neg [class] (A : Type) := (neg : A → A) infixl `*` := has_mul.mul infixl `+` := has_add.add postfix `⁻¹` := has_inv.inv prefix `-` := has_neg.neg notation 1 := !has_one.one notation 0 := !has_zero.zero --a second notation for the inverse, which is not overloaded postfix [parsing-only] `⁻¹ᵍ`:std.prec.max_plus := has_inv.inv /- semigroup -/ structure semigroup [class] (A : Type) extends has_mul A := (carrier_hset : is_hset A) (mul_assoc : ∀a b c, mul (mul a b) c = mul a (mul b c)) attribute semigroup.carrier_hset [instance] theorem mul_assoc [s : semigroup A] (a b c : A) : a * b * c = a * (b * c) := !semigroup.mul_assoc structure comm_semigroup [class] (A : Type) extends semigroup A := (mul_comm : ∀a b, mul a b = mul b a) theorem mul_comm [s : comm_semigroup A] (a b : A) : a * b = b * a := !comm_semigroup.mul_comm theorem mul_left_comm [s : comm_semigroup A] (a b c : A) : a * (b * c) = b * (a * c) := binary.left_comm (@mul_comm A s) (@mul_assoc A s) a b c theorem mul_right_comm [s : comm_semigroup A] (a b c : A) : (a * b) * c = (a * c) * b := binary.right_comm (@mul_comm A s) (@mul_assoc A s) a b c structure left_cancel_semigroup [class] (A : Type) extends semigroup A := (mul_left_cancel : ∀a b c, mul a b = mul a c → b = c) theorem mul_left_cancel [s : left_cancel_semigroup A] {a b c : A} : a * b = a * c → b = c := !left_cancel_semigroup.mul_left_cancel structure right_cancel_semigroup [class] (A : Type) extends semigroup A := (mul_right_cancel : ∀a b c, mul a b = mul c b → a = c) theorem mul_right_cancel [s : right_cancel_semigroup A] {a b c : A} : a * b = c * b → a = c := !right_cancel_semigroup.mul_right_cancel /- additive semigroup -/ structure add_semigroup [class] (A : Type) extends has_add A := (add_assoc : ∀a b c, add (add a b) c = add a (add b c)) theorem add_assoc [s : add_semigroup A] (a b c : A) : a + b + c = a + (b + c) := !add_semigroup.add_assoc structure add_comm_semigroup [class] (A : Type) extends add_semigroup A := (add_comm : ∀a b, add a b = add b a) theorem add_comm [s : add_comm_semigroup A] (a b : A) : a + b = b + a := !add_comm_semigroup.add_comm theorem add_left_comm [s : add_comm_semigroup A] (a b c : A) : a + (b + c) = b + (a + c) := binary.left_comm (@add_comm A s) (@add_assoc A s) a b c theorem add_right_comm [s : add_comm_semigroup A] (a b c : A) : (a + b) + c = (a + c) + b := binary.right_comm (@add_comm A s) (@add_assoc A s) a b c structure add_left_cancel_semigroup [class] (A : Type) extends add_semigroup A := (add_left_cancel : ∀a b c, add a b = add a c → b = c) theorem add_left_cancel [s : add_left_cancel_semigroup A] {a b c : A} : a + b = a + c → b = c := !add_left_cancel_semigroup.add_left_cancel structure add_right_cancel_semigroup [class] (A : Type) extends add_semigroup A := (add_right_cancel : ∀a b c, add a b = add c b → a = c) theorem add_right_cancel [s : add_right_cancel_semigroup A] {a b c : A} : a + b = c + b → a = c := !add_right_cancel_semigroup.add_right_cancel /- monoid -/ structure monoid [class] (A : Type) extends semigroup A, has_one A := (one_mul : ∀a, mul one a = a) (mul_one : ∀a, mul a one = a) theorem one_mul [s : monoid A] (a : A) : 1 * a = a := !monoid.one_mul theorem mul_one [s : monoid A] (a : A) : a * 1 = a := !monoid.mul_one structure comm_monoid [class] (A : Type) extends monoid A, comm_semigroup A /- additive monoid -/ structure add_monoid [class] (A : Type) extends add_semigroup A, has_zero A := (zero_add : ∀a, add zero a = a) (add_zero : ∀a, add a zero = a) theorem zero_add [s : add_monoid A] (a : A) : 0 + a = a := !add_monoid.zero_add theorem add_zero [s : add_monoid A] (a : A) : a + 0 = a := !add_monoid.add_zero structure add_comm_monoid [class] (A : Type) extends add_monoid A, add_comm_semigroup A /- group -/ structure group [class] (A : Type) extends monoid A, has_inv A := (mul_left_inv : ∀a, mul (inv a) a = one) -- Note: with more work, we could derive the axiom one_mul section group variable [s : group A] include s theorem mul_left_inv (a : A) : a⁻¹ * a = 1 := !group.mul_left_inv theorem inv_mul_cancel_left (a b : A) : a⁻¹ * (a * b) = b := calc a⁻¹ * (a * b) = a⁻¹ * a * b : mul_assoc ... = 1 * b : mul_left_inv ... = b : one_mul theorem inv_mul_cancel_right (a b : A) : a * b⁻¹ * b = a := calc a * b⁻¹ * b = a * (b⁻¹ * b) : mul_assoc ... = a * 1 : mul_left_inv ... = a : mul_one theorem inv_eq_of_mul_eq_one {a b : A} (H : a * b = 1) : a⁻¹ = b := calc a⁻¹ = a⁻¹ * 1 : mul_one ... = a⁻¹ * (a * b) : H ... = b : inv_mul_cancel_left theorem inv_one : 1⁻¹ = 1 := inv_eq_of_mul_eq_one (one_mul 1) theorem inv_inv (a : A) : (a⁻¹)⁻¹ = a := inv_eq_of_mul_eq_one (mul_left_inv a) theorem inv_inj {a b : A} (H : a⁻¹ = b⁻¹) : a = b := calc a = (a⁻¹)⁻¹ : inv_inv ... = b : inv_eq_of_mul_eq_one (H⁻¹ ▹ (mul_left_inv _)) --theorem inv_eq_inv_iff_eq (a b : A) : a⁻¹ = b⁻¹ ↔ a = b := --iff.intro (assume H, inv_inj H) (assume H, congr_arg _ H) --theorem inv_eq_one_iff_eq_one (a b : A) : a⁻¹ = 1 ↔ a = 1 := --inv_one ▹ !inv_eq_inv_iff_eq theorem eq_inv_imp_eq_inv {a b : A} (H : a = b⁻¹) : b = a⁻¹ := H⁻¹ ▹ (inv_inv b)⁻¹ --theorem eq_inv_iff_eq_inv (a b : A) : a = b⁻¹ ↔ b = a⁻¹ := --iff.intro !eq_inv_imp_eq_inv !eq_inv_imp_eq_inv theorem mul_right_inv (a : A) : a * a⁻¹ = 1 := calc a * a⁻¹ = (a⁻¹)⁻¹ * a⁻¹ : inv_inv ... = 1 : mul_left_inv theorem mul_inv_cancel_left (a b : A) : a * (a⁻¹ * b) = b := calc a * (a⁻¹ * b) = a * a⁻¹ * b : mul_assoc ... = 1 * b : mul_right_inv ... = b : one_mul theorem mul_inv_cancel_right (a b : A) : a * b * b⁻¹ = a := calc a * b * b⁻¹ = a * (b * b⁻¹) : mul_assoc ... = a * 1 : mul_right_inv ... = a : mul_one theorem inv_mul (a b : A) : (a * b)⁻¹ = b⁻¹ * a⁻¹ := inv_eq_of_mul_eq_one (calc a * b * (b⁻¹ * a⁻¹) = a * (b * (b⁻¹ * a⁻¹)) : mul_assoc ... = a * a⁻¹ : mul_inv_cancel_left ... = 1 : mul_right_inv) theorem eq_of_mul_inv_eq_one {a b : A} (H : a * b⁻¹ = 1) : a = b := calc a = a * b⁻¹ * b : inv_mul_cancel_right ... = 1 * b : H ... = b : one_mul -- TODO: better names for the next eight theorems? (Also for additive ones.) theorem eq_mul_inv_of_mul_eq {a b c : A} (H : a * b = c) : a = c * b⁻¹ := H ▹ !mul_inv_cancel_right⁻¹ theorem eq_inv_mul_of_mul_eq {a b c : A} (H : a * b = c) : b = a⁻¹ * c := H ▹ !inv_mul_cancel_left⁻¹ theorem inv_mul_eq_of_eq_mul {a b c : A} (H : a = b * c) : b⁻¹ * a = c := H⁻¹ ▹ !inv_mul_cancel_left theorem mul_inv_eq_of_eq_mul {a b c : A} (H : a = b * c) : a * c⁻¹ = b := H⁻¹ ▹ !mul_inv_cancel_right theorem eq_mul_of_mul_inv_eq {a b c : A} (H : a * b⁻¹ = c) : a = c * b := !inv_inv ▹ (eq_mul_inv_of_mul_eq H) theorem eq_mul_of_inv_mul_eq {a b c : A} (H : a⁻¹ * b = c) : b = a * c := !inv_inv ▹ (eq_inv_mul_of_mul_eq H) theorem mul_eq_of_eq_inv_mul {a b c : A} (H : a = b⁻¹ * c) : b * a = c := !inv_inv ▹ (inv_mul_eq_of_eq_mul H) theorem mul_eq_of_eq_mul_inv {a b c : A} (H : a = b * c⁻¹) : a * c = b := !inv_inv ▹ (mul_inv_eq_of_eq_mul H) --theorem mul_eq_iff_eq_inv_mul (a b c : A) : a * b = c ↔ b = a⁻¹ * c := --iff.intro eq_inv_mul_of_mul_eq mul_eq_of_eq_inv_mul --theorem mul_eq_iff_eq_mul_inv (a b c : A) : a * b = c ↔ a = c * b⁻¹ := --iff.intro eq_mul_inv_of_mul_eq mul_eq_of_eq_mul_inv definition group.to_left_cancel_semigroup [instance] : left_cancel_semigroup A := left_cancel_semigroup.mk (@group.mul A s) (@group.carrier_hset A s) (@group.mul_assoc A s) (take a b c, assume H : a * b = a * c, calc b = a⁻¹ * (a * b) : inv_mul_cancel_left ... = a⁻¹ * (a * c) : H ... = c : inv_mul_cancel_left) definition group.to_right_cancel_semigroup [instance] : right_cancel_semigroup A := right_cancel_semigroup.mk (@group.mul A s) (@group.carrier_hset A s) (@group.mul_assoc A s) (take a b c, assume H : a * b = c * b, calc a = (a * b) * b⁻¹ : mul_inv_cancel_right ... = (c * b) * b⁻¹ : H ... = c : mul_inv_cancel_right) end group structure comm_group [class] (A : Type) extends group A, comm_monoid A /- additive group -/ structure add_group [class] (A : Type) extends add_monoid A, has_neg A := (add_left_inv : ∀a, add (neg a) a = zero) section add_group variables [s : add_group A] include s theorem add_left_inv (a : A) : -a + a = 0 := !add_group.add_left_inv theorem neg_add_cancel_left (a b : A) : -a + (a + b) = b := calc -a + (a + b) = -a + a + b : add_assoc ... = 0 + b : add_left_inv ... = b : zero_add theorem neg_add_cancel_right (a b : A) : a + -b + b = a := calc a + -b + b = a + (-b + b) : add_assoc ... = a + 0 : add_left_inv ... = a : add_zero theorem neq_eq_of_add_eq_zero {a b : A} (H : a + b = 0) : -a = b := calc -a = -a + 0 : add_zero ... = -a + (a + b) : H ... = b : neg_add_cancel_left theorem neg_zero : -0 = 0 := neq_eq_of_add_eq_zero (zero_add 0) theorem neg_neg (a : A) : -(-a) = a := neq_eq_of_add_eq_zero (add_left_inv a) theorem neg_inj {a b : A} (H : -a = -b) : a = b := calc a = -(-a) : neg_neg ... = b : neq_eq_of_add_eq_zero (H⁻¹ ▹ (add_left_inv _)) --theorem neg_eq_neg_iff_eq (a b : A) : -a = -b ↔ a = b := --iff.intro (assume H, neg_inj H) (assume H, congr_arg _ H) --theorem neg_eq_zero_iff_eq_zero (a b : A) : -a = 0 ↔ a = 0 := --neg_zero ▹ !neg_eq_neg_iff_eq theorem eq_neq_of_eq_neg {a b : A} (H : a = -b) : b = -a := H⁻¹ ▹ (neg_neg b)⁻¹ --theorem eq_neg_iff_eq_neg (a b : A) : a = -b ↔ b = -a := --iff.intro !eq_neq_of_eq_neg !eq_neq_of_eq_neg theorem add_right_inv (a : A) : a + -a = 0 := calc a + -a = -(-a) + -a : neg_neg ... = 0 : add_left_inv theorem add_neg_cancel_left (a b : A) : a + (-a + b) = b := calc a + (-a + b) = a + -a + b : add_assoc ... = 0 + b : add_right_inv ... = b : zero_add theorem add_neg_cancel_right (a b : A) : a + b + -b = a := calc a + b + -b = a + (b + -b) : add_assoc ... = a + 0 : add_right_inv ... = a : add_zero theorem neq_add_rev (a b : A) : -(a + b) = -b + -a := neq_eq_of_add_eq_zero (calc a + b + (-b + -a) = a + (b + (-b + -a)) : add_assoc ... = a + -a : add_neg_cancel_left ... = 0 : add_right_inv) theorem eq_add_neq_of_add_eq {a b c : A} (H : a + b = c) : a = c + -b := H ▹ !add_neg_cancel_right⁻¹ theorem eq_neg_add_of_add_eq {a b c : A} (H : a + b = c) : b = -a + c := H ▹ !neg_add_cancel_left⁻¹ theorem neg_add_eq_of_eq_add {a b c : A} (H : a = b + c) : -b + a = c := H⁻¹ ▹ !neg_add_cancel_left theorem add_neg_eq_of_eq_add {a b c : A} (H : a = b + c) : a + -c = b := H⁻¹ ▹ !add_neg_cancel_right theorem eq_add_of_add_neg_eq {a b c : A} (H : a + -b = c) : a = c + b := !neg_neg ▹ (eq_add_neq_of_add_eq H) theorem eq_add_of_neg_add_eq {a b c : A} (H : -a + b = c) : b = a + c := !neg_neg ▹ (eq_neg_add_of_add_eq H) theorem add_eq_of_eq_neg_add {a b c : A} (H : a = -b + c) : b + a = c := !neg_neg ▹ (neg_add_eq_of_eq_add H) theorem add_eq_of_eq_add_neg {a b c : A} (H : a = b + -c) : a + c = b := !neg_neg ▹ (add_neg_eq_of_eq_add H) --theorem add_eq_iff_eq_neg_add (a b c : A) : a + b = c ↔ b = -a + c := --iff.intro eq_neg_add_of_add_eq add_eq_of_eq_neg_add --theorem add_eq_iff_eq_add_neg (a b c : A) : a + b = c ↔ a = c + -b := --iff.intro eq_add_neq_of_add_eq add_eq_of_eq_add_neg definition add_group.to_left_cancel_semigroup [instance] : add_left_cancel_semigroup A := add_left_cancel_semigroup.mk (@add_group.add A s) (@add_group.add_assoc A s) (take a b c, assume H : a + b = a + c, calc b = -a + (a + b) : neg_add_cancel_left ... = -a + (a + c) : H ... = c : neg_add_cancel_left) definition add_group.to_add_right_cancel_semigroup [instance] : add_right_cancel_semigroup A := add_right_cancel_semigroup.mk (@add_group.add A s) (@add_group.add_assoc A s) (take a b c, assume H : a + b = c + b, calc a = (a + b) + -b : add_neg_cancel_right ... = (c + b) + -b : H ... = c : add_neg_cancel_right) /- sub -/ -- TODO: derive corresponding facts for div in a field definition sub [reducible] (a b : A) : A := a + -b infix `-` := sub theorem sub_self (a : A) : a - a = 0 := !add_right_inv theorem sub_add_cancel (a b : A) : a - b + b = a := !neg_add_cancel_right theorem add_sub_cancel (a b : A) : a + b - b = a := !add_neg_cancel_right theorem eq_of_sub_eq_zero {a b : A} (H : a - b = 0) : a = b := calc a = (a - b) + b : sub_add_cancel ... = 0 + b : H ... = b : zero_add --theorem eq_iff_minus_eq_zero (a b : A) : a = b ↔ a - b = 0 := --iff.intro (assume H, H ▹ !sub_self) (assume H, eq_of_sub_eq_zero H) theorem zero_sub (a : A) : 0 - a = -a := !zero_add theorem sub_zero (a : A) : a - 0 = a := neg_zero⁻¹ ▹ !add_zero theorem sub_neg_eq_add (a b : A) : a - (-b) = a + b := !neg_neg ▹ idp theorem neg_sub (a b : A) : -(a - b) = b - a := neq_eq_of_add_eq_zero (calc a - b + (b - a) = a - b + b - a : add_assoc ... = a - a : sub_add_cancel ... = 0 : sub_self) theorem add_sub (a b c : A) : a + (b - c) = a + b - c := !add_assoc⁻¹ theorem sub_add_eq_sub_sub_swap (a b c : A) : a - (b + c) = a - c - b := calc a - (b + c) = a + (-c - b) : neq_add_rev ... = a - c - b : add_assoc --theorem minus_eq_iff_eq_add (a b c : A) : a - b = c ↔ a = c + b := --iff.intro (assume H, eq_add_of_add_neg_eq H) (assume H, add_neg_eq_of_eq_add H) --theorem eq_minus_iff_add_eq (a b c : A) : a = b - c ↔ a + c = b := --iff.intro (assume H, add_eq_of_eq_add_neg H) (assume H, eq_add_neq_of_add_eq H) --theorem minus_eq_minus_iff {a b c d : A} (H : a - b = c - d) : a = b ↔ c = d := --calc -- a = b ↔ a - b = 0 : eq_iff_minus_eq_zero -- ... ↔ c - d = 0 : H ▹ !iff.refl -- ... ↔ c = d : iff.symm (eq_iff_minus_eq_zero c d) end add_group structure add_comm_group [class] (A : Type) extends add_group A, add_comm_monoid A section add_comm_group variable [s : add_comm_group A] include s theorem sub_add_eq_sub_sub (a b c : A) : a - (b + c) = a - b - c := !add_comm ▹ !sub_add_eq_sub_sub_swap theorem neq_add_eq_sub (a b : A) : -a + b = b - a := !add_comm theorem neg_add_distrib (a b : A) : -(a + b) = -a + -b := !add_comm ▹ !neq_add_rev theorem sub_add_eq_add_sub (a b c : A) : a - b + c = a + c - b := !add_right_comm theorem sub_sub (a b c : A) : a - b - c = a - (b + c) := calc a - b - c = a + (-b + -c) : add_assoc ... = a + -(b + c) : neg_add_distrib ... = a - (b + c) : idp theorem add_sub_add_left_eq_sub (a b c : A) : (c + a) - (c + b) = a - b := calc (c + a) - (c + b) = c + a - c - b : sub_add_eq_sub_sub ... = a + c - c - b : add_comm a c ... = a - b : add_sub_cancel end add_comm_group /- bundled structures -/ structure Semigroup := (carrier : Type) (struct : semigroup carrier) attribute Semigroup.carrier [coercion] attribute Semigroup.struct [instance] structure CommSemigroup := (carrier : Type) (struct : comm_semigroup carrier) attribute CommSemigroup.carrier [coercion] attribute CommSemigroup.struct [instance] structure Monoid := (carrier : Type) (struct : monoid carrier) attribute Monoid.carrier [coercion] attribute Monoid.struct [instance] structure CommMonoid := (carrier : Type) (struct : comm_monoid carrier) attribute CommMonoid.carrier [coercion] attribute CommMonoid.struct [instance] structure Group := (carrier : Type) (struct : group carrier) attribute Group.carrier [coercion] attribute Group.struct [instance] structure CommGroup := (carrier : Type) (struct : comm_group carrier) attribute CommGroup.carrier [coercion] attribute CommGroup.struct [instance] structure AddSemigroup := (carrier : Type) (struct : add_semigroup carrier) attribute AddSemigroup.carrier [coercion] attribute AddSemigroup.struct [instance] structure AddCommSemigroup := (carrier : Type) (struct : add_comm_semigroup carrier) attribute AddCommSemigroup.carrier [coercion] attribute AddCommSemigroup.struct [instance] structure AddMonoid := (carrier : Type) (struct : add_monoid carrier) attribute AddMonoid.carrier [coercion] attribute AddMonoid.struct [instance] structure AddCommMonoid := (carrier : Type) (struct : add_comm_monoid carrier) attribute AddCommMonoid.carrier [coercion] attribute AddCommMonoid.struct [instance] structure AddGroup := (carrier : Type) (struct : add_group carrier) attribute AddGroup.carrier [coercion] attribute AddGroup.struct [instance] structure AddCommGroup := (carrier : Type) (struct : add_comm_group carrier) attribute AddCommGroup.carrier [coercion] attribute AddCommGroup.struct [instance] end path_algebra
a39d94af3ee3b64078ea296ad6dc0bef031a115f
8b9f17008684d796c8022dab552e42f0cb6fb347
/tests/lean/run/dfun_tst.lean
13d53abd081e4571af5b88ac00e9ac3e0bf519b1
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
215
lean
import logic data.prod data.vector open prod nat inhabited vector theorem tst1 : inhabited (vector nat 2) theorem tst2 : inhabited (Prop × (Π n : nat, vector nat n)) (* print(get_env():find("tst2"):value()) *)
21cb2d2652fa4aaa5ba428dd022450c5855aef0d
367134ba5a65885e863bdc4507601606690974c1
/src/data/list/range.lean
9943c72f0ed93fc7cb10b03e7922c8f6ce45c039
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
10,092
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kenny Lau, Scott Morrison -/ import data.list.chain import data.list.nodup import data.list.of_fn import data.list.zip open nat namespace list /- iota and range(') -/ universe u variables {α : Type u} @[simp] theorem length_range' : ∀ (s n : ℕ), length (range' s n) = n | s 0 := rfl | s (n+1) := congr_arg succ (length_range' _ _) @[simp] theorem range'_eq_nil {s n : ℕ} : range' s n = [] ↔ n = 0 := by rw [← length_eq_zero, length_range'] @[simp] theorem mem_range' {m : ℕ} : ∀ {s n : ℕ}, m ∈ range' s n ↔ s ≤ m ∧ m < s + n | s 0 := (false_iff _).2 $ λ ⟨H1, H2⟩, not_le_of_lt H2 H1 | s (succ n) := have m = s → m < s + n + 1, from λ e, e ▸ lt_succ_of_le (le_add_right _ _), have l : m = s ∨ s + 1 ≤ m ↔ s ≤ m, by simpa only [eq_comm] using (@le_iff_eq_or_lt _ _ s m).symm, (mem_cons_iff _ _ _).trans $ by simp only [mem_range', or_and_distrib_left, or_iff_right_of_imp this, l, add_right_comm]; 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 map_sub_range' (a) : ∀ (s n : ℕ) (h : a ≤ s), map (λ x, x - a) (range' s n) = range' (s - a) n | s 0 _ := rfl | s (n+1) h := begin convert congr_arg (cons (s-a)) (map_sub_range' (s+1) n (nat.le_succ_of_le h)), rw nat.succ_sub h, refl, end theorem chain_succ_range' : ∀ s n : ℕ, chain (λ a b, b = succ a) s (range' (s+1) n) | s 0 := chain.nil | s (n+1) := (chain_succ_range' (s+1) n).cons rfl theorem chain_lt_range' (s n : ℕ) : chain (<) s (range' (s+1) n) := (chain_succ_range' s n).imp (λ a b e, e.symm ▸ lt_succ_self _) theorem pairwise_lt_range' : ∀ s n : ℕ, pairwise (<) (range' s n) | s 0 := pairwise.nil | s (n+1) := (chain_iff_pairwise (by exact λ a b c, lt_trans)).1 (chain_lt_range' s n) theorem nodup_range' (s n : ℕ) : nodup (range' s n) := (pairwise_lt_range' s n).imp (λ a b, ne_of_lt) @[simp] theorem range'_append : ∀ s m n : ℕ, range' s m ++ range' (s+m) n = range' s (n+m) | s 0 n := rfl | s (m+1) n := show s :: (range' (s+1) m ++ range' (s+m+1) n) = s :: range' (s+1) (n+m), by rw [add_right_comm, range'_append] theorem range'_sublist_right {s m n : ℕ} : range' s m <+ range' s n ↔ m ≤ n := ⟨λ h, by simpa only [length_range'] using length_le_of_sublist h, λ h, by rw [← nat.sub_add_cancel h, ← range'_append]; apply sublist_append_left⟩ theorem range'_subset_right {s m n : ℕ} : range' s m ⊆ range' s n ↔ m ≤ n := ⟨λ h, le_of_not_lt $ λ hn, lt_irrefl (s+n) $ (mem_range'.1 $ h $ mem_range'.2 ⟨le_add_right _ _, nat.add_lt_add_left hn s⟩).2, λ h, (range'_sublist_right.2 h).subset⟩ theorem nth_range' : ∀ s {m n : ℕ}, m < n → nth (range' s n) m = some (s + m) | s 0 (n+1) _ := rfl | s (m+1) (n+1) h := (nth_range' (s+1) (lt_of_add_lt_add_right h)).trans $ by rw add_right_comm; refl @[simp] lemma nth_le_range' {n m} (i) (H : i < (range' n m).length) : nth_le (range' n m) i H = n + i := option.some.inj $ by rw [←nth_le_nth _, nth_range' _ (by simpa using H)] theorem range'_concat (s n : ℕ) : range' s (n + 1) = range' s n ++ [s+n] := by rw add_comm n 1; exact (range'_append s n 1).symm theorem range_core_range' : ∀ s n : ℕ, range_core s (range' s n) = range' 0 (n + s) | 0 n := rfl | (s+1) n := by rw [show n+(s+1) = n+1+s, from add_right_comm n s 1]; exact range_core_range' s (n+1) theorem range_eq_range' (n : ℕ) : range n = range' 0 n := (range_core_range' n 0).trans $ by rw zero_add 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 range'_eq_map_range (s n : ℕ) : range' s n = map ((+) s) (range n) := by rw [range_eq_range', map_add_range']; refl @[simp] theorem length_range (n : ℕ) : length (range n) = n := by simp only [range_eq_range', length_range'] @[simp] theorem range_eq_nil {n : ℕ} : range n = [] ↔ n = 0 := by rw [← length_eq_zero, length_range] theorem pairwise_lt_range (n : ℕ) : pairwise (<) (range n) := by simp only [range_eq_range', pairwise_lt_range'] theorem nodup_range (n : ℕ) : nodup (range n) := by simp only [range_eq_range', nodup_range'] theorem range_sublist {m n : ℕ} : range m <+ range n ↔ m ≤ n := by simp only [range_eq_range', range'_sublist_right] theorem range_subset {m n : ℕ} : range m ⊆ range n ↔ m ≤ n := by simp only [range_eq_range', range'_subset_right] @[simp] theorem mem_range {m n : ℕ} : m ∈ range n ↔ m < n := by simp only [range_eq_range', mem_range', nat.zero_le, true_and, zero_add] @[simp] theorem not_mem_range_self {n : ℕ} : n ∉ range n := mt mem_range.1 $ lt_irrefl _ @[simp] theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) := by simp only [succ_pos', lt_add_iff_pos_right, mem_range] theorem nth_range {m n : ℕ} (h : m < n) : nth (range n) m = some m := by simp only [range_eq_range', nth_range' _ h, zero_add] theorem range_succ (n : ℕ) : range (succ n) = range n ++ [n] := by simp only [range_eq_range', range'_concat, zero_add] @[simp] lemma range_zero : range 0 = [] := rfl theorem iota_eq_reverse_range' : ∀ n : ℕ, iota n = reverse (range' 1 n) | 0 := rfl | (n+1) := by simp only [iota, range'_concat, iota_eq_reverse_range' n, reverse_append, add_comm]; refl @[simp] theorem length_iota (n : ℕ) : length (iota n) = n := by simp only [iota_eq_reverse_range', length_reverse, length_range'] theorem pairwise_gt_iota (n : ℕ) : pairwise (>) (iota n) := by simp only [iota_eq_reverse_range', pairwise_reverse, pairwise_lt_range'] theorem nodup_iota (n : ℕ) : nodup (iota n) := by simp only [iota_eq_reverse_range', nodup_reverse, nodup_range'] theorem mem_iota {m n : ℕ} : m ∈ iota n ↔ 1 ≤ m ∧ m ≤ n := by simp only [iota_eq_reverse_range', mem_reverse, mem_range', add_comm, lt_succ_iff] 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 only [show s + (n + 1) - 1 = s + n, from rfl, (∘), λ a i, show a - 1 - i = a - succ i, from pred_sub _ _, reverse_singleton, map_cons, nat.sub_zero, cons_append, nil_append, eq_self_iff_true, true_and, map_map] using reverse_range' s n /-- All elements of `fin n`, from `0` to `n-1`. -/ def fin_range (n : ℕ) : list (fin n) := (range n).pmap fin.mk (λ _, list.mem_range.1) @[simp] lemma fin_range_zero : fin_range 0 = [] := rfl @[simp] lemma mem_fin_range {n : ℕ} (a : fin n) : a ∈ fin_range n := mem_pmap.2 ⟨a.1, mem_range.2 a.2, fin.eta _ _⟩ lemma nodup_fin_range (n : ℕ) : (fin_range n).nodup := nodup_pmap (λ _ _ _ _, fin.veq_of_eq) (nodup_range _) @[simp] lemma length_fin_range (n : ℕ) : (fin_range n).length = n := by rw [fin_range, length_pmap, length_range] @[simp] lemma fin_range_eq_nil {n : ℕ} : fin_range n = [] ↔ n = 0 := by rw [← length_eq_zero, length_fin_range] @[to_additive] theorem prod_range_succ {α : Type u} [monoid α] (f : ℕ → α) (n : ℕ) : ((range n.succ).map f).prod = ((range n).map f).prod * f n := by rw [range_succ, map_append, map_singleton, prod_append, prod_cons, prod_nil, mul_one] /-- A variant of `prod_range_succ` which pulls off the first term in the product rather than the last.-/ @[to_additive "A variant of `sum_range_succ` which pulls off the first term in the sum rather than the last."] theorem prod_range_succ' {α : Type u} [monoid α] (f : ℕ → α) (n : ℕ) : ((range n.succ).map f).prod = f 0 * ((range n).map (λ i, f (succ i))).prod := nat.rec_on n (show 1 * f 0 = f 0 * 1, by rw [one_mul, mul_one]) (λ _ hd, by rw [list.prod_range_succ, hd, mul_assoc, ←list.prod_range_succ]) @[simp] theorem enum_from_map_fst : ∀ n (l : list α), map prod.fst (enum_from n l) = range' n l.length | n [] := rfl | n (a :: l) := congr_arg (cons _) (enum_from_map_fst _ _) @[simp] theorem enum_map_fst (l : list α) : map prod.fst (enum l) = range l.length := by simp only [enum, enum_from_map_fst, range_eq_range'] lemma enum_eq_zip_range (l : list α) : l.enum = (range l.length).zip l := zip_of_prod (enum_map_fst _) (enum_map_snd _) @[simp] lemma unzip_enum_eq_prod (l : list α) : l.enum.unzip = (range l.length, l) := by simp only [enum_eq_zip_range, unzip_zip, length_range] lemma enum_from_eq_zip_range' (l : list α) {n : ℕ} : l.enum_from n = (range' n l.length).zip l := zip_of_prod (enum_from_map_fst _ _) (enum_from_map_snd _ _) @[simp] lemma unzip_enum_from_eq_prod (l : list α) {n : ℕ} : (l.enum_from n).unzip = (range' n l.length, l) := by simp only [enum_from_eq_zip_range', unzip_zip, length_range'] @[simp] lemma nth_le_range {n} (i) (H : i < (range n).length) : nth_le (range n) i H = i := option.some.inj $ by rw [← nth_le_nth _, nth_range (by simpa using H)] @[simp] lemma nth_le_fin_range {n : ℕ} {i : ℕ} (h) : (fin_range n).nth_le i h = ⟨i, length_fin_range n ▸ h⟩ := by simp only [fin_range, nth_le_range, nth_le_pmap, fin.mk_eq_subtype_mk] theorem of_fn_eq_pmap {α n} {f : fin n → α} : of_fn f = pmap (λ i hi, f ⟨i, hi⟩) (range n) (λ _, mem_range.1) := by rw [pmap_eq_map_attach]; from ext_le (by simp) (λ i hi1 hi2, by { simp at hi1, simp [nth_le_of_fn f ⟨i, hi1⟩, -subtype.val_eq_coe] }) theorem of_fn_id (n) : of_fn id = fin_range n := of_fn_eq_pmap theorem of_fn_eq_map {α n} {f : fin n → α} : of_fn f = (fin_range n).map f := by rw [← of_fn_id, map_of_fn, function.right_id] theorem nodup_of_fn {α n} {f : fin n → α} (hf : function.injective f) : nodup (of_fn f) := by rw of_fn_eq_pmap; from nodup_pmap (λ _ _ _ _ H, fin.veq_of_eq $ hf H) (nodup_range n) end list
1b99f5a11aef1330483b6da18dfea6fb793a8951
76ce87faa6bc3c2aa9af5962009e01e04f2a074a
/HW/Quiz1.lean
65ca914d5ea9f81503288d48c01050e3cea0f4f1
[]
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
424
lean
-- Matthew Normansell (man9ej) def and_assoc_r { P Q R : Prop } (pfPQ_R: (P ∧ Q) ∧ R) : (P ∧ (Q ∧ R)) := begin have pfPQ := and.elim_left pfPQ_R, have pfR := and.elim_right pfPQ_R, have pfP := and.elim_left pfPQ, have pfQ := and.elim_right pfPQ, have pfQR := and.intro (pfQ) (pfR), have pfP_QR :=and.intro pfP pfQR, exact pfP_QR end #check and_assoc_r
b296b65555e25dcc401e8f9b58769fd2992cee31
a4673261e60b025e2c8c825dfa4ab9108246c32e
/tests/lean/run/typeclass_append.lean
b010958555de345dc6abfe03fc1995f68c39d58c
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,475
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Daniel Selsam Performance test to ensure quadratic blowup is avoided. -/ class AppendList {α : Type} (xs₁ xs₂ : List α) (out : outParam $ List α) : Type := (u : Unit := ()) instance AppendBase {α : Type} (xs₂ : List α) : AppendList [] xs₂ xs₂ := {} instance AppendStep {α : Type} (x : α) (xs₁ xs₂ out : List α) [AppendList xs₁ xs₂ out] : AppendList (x::xs₁) xs₂ (x::out) := {} #synth AppendList [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50] [200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250]
a966953175945b94ca6a198deefceeda0212a652
82e44445c70db0f03e30d7be725775f122d72f3e
/src/category_theory/abelian/basic.lean
a08958cd817d520152021b379d4424ad497a90d0
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
25,630
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 _ @[priority 100] instance has_zero_object : has_zero_object C := has_zero_object_of_has_initial_object 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 section variables {f} lemma comp_coimage_π_eq_zero {R : C} {g : Q ⟶ R} (h : f ≫ g = 0) : f ≫ coimages.coimage.π g = 0 := zero_of_comp_mono (coimages.factor_thru_coimage g) $ by simp [h] end 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] {W 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] {W 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 lemma epi_snd_of_is_limit [epi f] {s : pullback_cone f g} (hs : is_limit s) : epi s.snd := begin convert epi_of_epi_fac (is_limit.cone_point_unique_up_to_iso_hom_comp (limit.is_limit _) hs _), { refl }, { exact abelian.epi_pullback_of_epi_f _ _ } end lemma epi_fst_of_is_limit [epi g] {s : pullback_cone f g} (hs : is_limit s) : epi s.fst := begin convert epi_of_epi_fac (is_limit.cone_point_unique_up_to_iso_hom_comp (limit.is_limit _) hs _), { refl }, { exact abelian.epi_pullback_of_epi_g _ _ } end /-- Suppose `f` and `g` are two morphisms with a common codomain and suppose we have written `g` as an epimorphism followed by a monomorphism. If `f` factors through the mono part of this factorization, then any pullback of `g` along `f` is an epimorphism. -/ lemma epi_fst_of_factor_thru_epi_mono_factorization (g₁ : Y ⟶ W) [epi g₁] (g₂ : W ⟶ Z) [mono g₂] (hg : g₁ ≫ g₂ = g) (f' : X ⟶ W) (hf : f' ≫ g₂ = f) (t : pullback_cone f g) (ht : is_limit t) : epi t.fst := by apply epi_fst_of_is_limit _ _ (pullback_cone.is_limit_of_factors f g g₂ f' g₁ hf hg t ht) end epi_pullback section mono_pushout variables [limits.has_pushouts C] {W 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 lemma mono_inr_of_is_colimit [mono f] {s : pushout_cocone f g} (hs : is_colimit s) : mono s.inr := begin convert mono_of_mono_fac (is_colimit.comp_cocone_point_unique_up_to_iso_hom hs (colimit.is_colimit _) _), { refl }, { exact abelian.mono_pushout_of_mono_f _ _ } end lemma mono_inl_of_is_colimit [mono g] {s : pushout_cocone f g} (hs : is_colimit s) : mono s.inl := begin convert mono_of_mono_fac (is_colimit.comp_cocone_point_unique_up_to_iso_hom hs (colimit.is_colimit _) _), { refl }, { exact abelian.mono_pushout_of_mono_g _ _ } end /-- Suppose `f` and `g` are two morphisms with a common domain and suppose we have written `g` as an epimorphism followed by a monomorphism. If `f` factors through the epi part of this factorization, then any pushout of `g` along `f` is a monomorphism. -/ lemma mono_inl_of_factor_thru_epi_mono_factorization (f : X ⟶ Y) (g : X ⟶ Z) (g₁ : X ⟶ W) [epi g₁] (g₂ : W ⟶ Z) [mono g₂] (hg : g₁ ≫ g₂ = g) (f' : W ⟶ Y) (hf : g₁ ≫ f' = f) (t : pushout_cocone f g) (ht : is_colimit t) : mono t.inl := by apply mono_inl_of_is_colimit _ _ (pushout_cocone.is_colimit_of_factors _ _ _ _ _ hf hg t ht) 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
7320e65b380c165c56bb30a3216a4795cc5c22ca
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/implementedByIssue.lean
7b0e6aeb394ba8962ab2e231d4366e16eff333db
[ "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
577
lean
namespace Hidden structure Array (α : Type u) (n : Nat) : Type u where data : (i : Fin n) → α @[extern "some_extern"] def get {α} {n : Nat} (A : Array α n) (i : Fin n) : α := A.data i attribute [implemented_by get] Array.data -- ok def get_2 {α : Type} {n : Nat} (A : Array α n) (i : Fin n) : α := A.data i attribute [implemented_by get_2] Array.data -- error, number of universe parameters do not match def get_3 {α} {n : Nat} (i : Fin n) (A : Array α n) : α := A.data i attribute [implemented_by get_3] Array.data -- error, types do not match
4d4d8fe45b53c1652df9c412de16cea4d661bd62
f5f7e6fae601a5fe3cac7cc3ed353ed781d62419
/src/topology/basic.lean
7da0f17542129b5cd47e7a7825874fb07723e1e0
[ "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
35,274
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 Theory of topological spaces. Parts of the formalization is based on the books: N. Bourbaki: General Topology I. M. James: Topologies and Uniformities A major difference is that this formalization is heavily based on the filter library. -/ import order.filter data.set.countable tactic open set filter lattice classical local attribute [instance] prop_decidable universes u v w structure topological_space (α : Type u) := (is_open : set α → Prop) (is_open_univ : is_open univ) (is_open_inter : ∀s t, is_open s → is_open t → is_open (s ∩ t)) (is_open_sUnion : ∀s, (∀t∈s, is_open t) → is_open (⋃₀ s)) attribute [class] topological_space section topological_space variables {α : Type u} {β : Type v} {ι : Sort w} {a : α} {s s₁ s₂ : set α} {p p₁ p₂ : α → Prop} @[extensionality] lemma topological_space_eq : ∀ {f g : topological_space α}, f.is_open = g.is_open → f = g | ⟨a, _, _, _⟩ ⟨b, _, _, _⟩ rfl := rfl section variables [t : topological_space α] include t /-- `is_open s` means that `s` is open in the ambient topological space on `α` -/ def is_open (s : set α) : Prop := topological_space.is_open t s @[simp] lemma is_open_univ : is_open (univ : set α) := topological_space.is_open_univ t lemma is_open_inter (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∩ s₂) := topological_space.is_open_inter t s₁ s₂ h₁ h₂ lemma is_open_sUnion {s : set (set α)} (h : ∀t ∈ s, is_open t) : is_open (⋃₀ s) := topological_space.is_open_sUnion t s h end lemma is_open_fold {s : set α} {t : topological_space α} : t.is_open s = @is_open α t s := rfl variables [topological_space α] lemma is_open_Union {f : ι → set α} (h : ∀i, is_open (f i)) : is_open (⋃i, f i) := is_open_sUnion $ by rintro _ ⟨i, rfl⟩; exact h i lemma is_open_bUnion {s : set β} {f : β → set α} (h : ∀i∈s, is_open (f i)) : is_open (⋃i∈s, f i) := is_open_Union $ assume i, is_open_Union $ assume hi, h i hi lemma is_open_union (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∪ s₂) := by rw union_eq_Union; exact is_open_Union (bool.forall_bool.2 ⟨h₂, h₁⟩) @[simp] lemma is_open_empty : is_open (∅ : set α) := by rw ← sUnion_empty; exact is_open_sUnion (assume a, false.elim) lemma is_open_sInter {s : set (set α)} (hs : finite s) : (∀t ∈ s, is_open t) → is_open (⋂₀ s) := finite.induction_on hs (λ _, by rw sInter_empty; exact is_open_univ) $ λ a s has hs ih h, by rw sInter_insert; exact is_open_inter (h _ $ mem_insert _ _) (ih $ λ t, h t ∘ mem_insert_of_mem _) lemma is_open_bInter {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_open (f i)) → is_open (⋂i∈s, f i) := finite.induction_on hs (λ _, by rw bInter_empty; exact is_open_univ) (λ a s has hs ih h, by rw bInter_insert; exact is_open_inter (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_open_const {p : Prop} : is_open {a : α | p} := by_cases (assume : p, begin simp only [this]; exact is_open_univ end) (assume : ¬ p, begin simp only [this]; exact is_open_empty end) lemma is_open_and : is_open {a | p₁ a} → is_open {a | p₂ a} → is_open {a | p₁ a ∧ p₂ a} := is_open_inter /-- A set is closed if its complement is open -/ def is_closed (s : set α) : Prop := is_open (-s) @[simp] lemma is_closed_empty : is_closed (∅ : set α) := by unfold is_closed; rw compl_empty; exact is_open_univ @[simp] lemma is_closed_univ : is_closed (univ : set α) := by unfold is_closed; rw compl_univ; exact is_open_empty lemma is_closed_union : is_closed s₁ → is_closed s₂ → is_closed (s₁ ∪ s₂) := λ h₁ h₂, by unfold is_closed; rw compl_union; exact is_open_inter h₁ h₂ lemma is_closed_sInter {s : set (set α)} : (∀t ∈ s, is_closed t) → is_closed (⋂₀ s) := by simp only [is_closed, compl_sInter, sUnion_image]; exact assume h, is_open_Union $ assume t, is_open_Union $ assume ht, h t ht lemma is_closed_Inter {f : ι → set α} (h : ∀i, is_closed (f i)) : is_closed (⋂i, f i ) := is_closed_sInter $ assume t ⟨i, (heq : f i = t)⟩, heq ▸ h i @[simp] lemma is_open_compl_iff {s : set α} : is_open (-s) ↔ is_closed s := iff.rfl @[simp] lemma is_closed_compl_iff {s : set α} : is_closed (-s) ↔ is_open s := by rw [←is_open_compl_iff, compl_compl] lemma is_open_diff {s t : set α} (h₁ : is_open s) (h₂ : is_closed t) : is_open (s \ t) := is_open_inter h₁ $ is_open_compl_iff.mpr h₂ lemma is_closed_inter (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (s₁ ∩ s₂) := by rw [is_closed, compl_inter]; exact is_open_union h₁ h₂ lemma is_closed_Union {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_closed (f i)) → is_closed (⋃i∈s, f i) := finite.induction_on hs (λ _, by rw bUnion_empty; exact is_closed_empty) (λ a s has hs ih h, by rw bUnion_insert; exact is_closed_union (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_closed_imp {p q : α → Prop} (hp : is_open {x | p x}) (hq : is_closed {x | q x}) : is_closed {x | p x → q x} := have {x | p x → q x} = (- {x | p x}) ∪ {x | q x}, from set.ext $ λ x, imp_iff_not_or, by rw [this]; exact is_closed_union (is_closed_compl_iff.mpr hp) hq lemma is_open_neg : is_closed {a | p a} → is_open {a | ¬ p a} := is_open_compl_iff.mpr /-- The interior of a set `s` is the largest open subset of `s`. -/ def interior (s : set α) : set α := ⋃₀ {t | is_open t ∧ t ⊆ s} lemma mem_interior {s : set α} {x : α} : x ∈ interior s ↔ ∃ t ⊆ s, is_open t ∧ x ∈ t := by simp only [interior, mem_set_of_eq, exists_prop, and_assoc, and.left_comm] @[simp] lemma is_open_interior {s : set α} : is_open (interior s) := is_open_sUnion $ assume t ⟨h₁, h₂⟩, h₁ lemma interior_subset {s : set α} : interior s ⊆ s := sUnion_subset $ assume t ⟨h₁, h₂⟩, h₂ lemma interior_maximal {s t : set α} (h₁ : t ⊆ s) (h₂ : is_open t) : t ⊆ interior s := subset_sUnion_of_mem ⟨h₂, h₁⟩ lemma interior_eq_of_open {s : set α} (h : is_open s) : interior s = s := subset.antisymm interior_subset (interior_maximal (subset.refl s) h) lemma interior_eq_iff_open {s : set α} : interior s = s ↔ is_open s := ⟨assume h, h ▸ is_open_interior, interior_eq_of_open⟩ lemma subset_interior_iff_open {s : set α} : s ⊆ interior s ↔ is_open s := by simp only [interior_eq_iff_open.symm, subset.antisymm_iff, interior_subset, true_and] lemma subset_interior_iff_subset_of_open {s t : set α} (h₁ : is_open s) : s ⊆ interior t ↔ s ⊆ t := ⟨assume h, subset.trans h interior_subset, assume h₂, interior_maximal h₂ h₁⟩ lemma interior_mono {s t : set α} (h : s ⊆ t) : interior s ⊆ interior t := interior_maximal (subset.trans interior_subset h) is_open_interior @[simp] lemma interior_empty : interior (∅ : set α) = ∅ := interior_eq_of_open is_open_empty @[simp] lemma interior_univ : interior (univ : set α) = univ := interior_eq_of_open is_open_univ @[simp] lemma interior_interior {s : set α} : interior (interior s) = interior s := interior_eq_of_open is_open_interior @[simp] lemma interior_inter {s t : set α} : interior (s ∩ t) = interior s ∩ interior t := subset.antisymm (subset_inter (interior_mono $ inter_subset_left s t) (interior_mono $ inter_subset_right s t)) (interior_maximal (inter_subset_inter interior_subset interior_subset) $ is_open_inter is_open_interior is_open_interior) lemma interior_union_is_closed_of_interior_empty {s t : set α} (h₁ : is_closed s) (h₂ : interior t = ∅) : interior (s ∪ t) = interior s := have interior (s ∪ t) ⊆ s, from assume x ⟨u, ⟨(hu₁ : is_open u), (hu₂ : u ⊆ s ∪ t)⟩, (hx₁ : x ∈ u)⟩, classical.by_contradiction $ assume hx₂ : x ∉ s, have u \ s ⊆ t, from assume x ⟨h₁, h₂⟩, or.resolve_left (hu₂ h₁) h₂, have u \ s ⊆ interior t, by rwa subset_interior_iff_subset_of_open (is_open_diff hu₁ h₁), have u \ s ⊆ ∅, by rwa h₂ at this, this ⟨hx₁, hx₂⟩, subset.antisymm (interior_maximal this is_open_interior) (interior_mono $ subset_union_left _ _) lemma is_open_iff_forall_mem_open : is_open s ↔ ∀ x ∈ s, ∃ t ⊆ s, is_open t ∧ x ∈ t := by rw ← subset_interior_iff_open; simp only [subset_def, mem_interior] /-- The closure of `s` is the smallest closed set containing `s`. -/ def closure (s : set α) : set α := ⋂₀ {t | is_closed t ∧ s ⊆ t} @[simp] lemma is_closed_closure {s : set α} : is_closed (closure s) := is_closed_sInter $ assume t ⟨h₁, h₂⟩, h₁ lemma subset_closure {s : set α} : s ⊆ closure s := subset_sInter $ assume t ⟨h₁, h₂⟩, h₂ lemma closure_minimal {s t : set α} (h₁ : s ⊆ t) (h₂ : is_closed t) : closure s ⊆ t := sInter_subset_of_mem ⟨h₂, h₁⟩ lemma closure_eq_of_is_closed {s : set α} (h : is_closed s) : closure s = s := subset.antisymm (closure_minimal (subset.refl s) h) subset_closure lemma closure_eq_iff_is_closed {s : set α} : closure s = s ↔ is_closed s := ⟨assume h, h ▸ is_closed_closure, closure_eq_of_is_closed⟩ lemma closure_subset_iff_subset_of_is_closed {s t : set α} (h₁ : is_closed t) : closure s ⊆ t ↔ s ⊆ t := ⟨subset.trans subset_closure, assume h, closure_minimal h h₁⟩ lemma closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t := closure_minimal (subset.trans h subset_closure) is_closed_closure lemma is_closed_of_closure_subset {s : set α} (h : closure s ⊆ s) : is_closed s := by rw subset.antisymm subset_closure h; exact is_closed_closure @[simp] lemma closure_empty : closure (∅ : set α) = ∅ := closure_eq_of_is_closed is_closed_empty lemma closure_empty_iff (s : set α) : closure s = ∅ ↔ s = ∅ := begin split; intro h, { rw set.eq_empty_iff_forall_not_mem, intros x H, simpa only [h] using subset_closure H }, { exact (eq.symm h) ▸ closure_empty }, end @[simp] lemma closure_univ : closure (univ : set α) = univ := closure_eq_of_is_closed is_closed_univ @[simp] lemma closure_closure {s : set α} : closure (closure s) = closure s := closure_eq_of_is_closed is_closed_closure @[simp] lemma closure_union {s t : set α} : closure (s ∪ t) = closure s ∪ closure t := subset.antisymm (closure_minimal (union_subset_union subset_closure subset_closure) $ is_closed_union is_closed_closure is_closed_closure) (union_subset (closure_mono $ subset_union_left _ _) (closure_mono $ subset_union_right _ _)) lemma interior_subset_closure {s : set α} : interior s ⊆ closure s := subset.trans interior_subset subset_closure lemma closure_eq_compl_interior_compl {s : set α} : closure s = - interior (- s) := begin unfold interior closure is_closed, rw [compl_sUnion, compl_image_set_of], simp only [compl_subset_compl] end @[simp] lemma interior_compl {s : set α} : interior (- s) = - closure s := by simp [closure_eq_compl_interior_compl] @[simp] lemma closure_compl {s : set α} : closure (- s) = - interior s := by simp [closure_eq_compl_interior_compl] theorem mem_closure_iff {s : set α} {a : α} : a ∈ closure s ↔ ∀ o, is_open o → a ∈ o → o ∩ s ≠ ∅ := ⟨λ h o oo ao os, have s ⊆ -o, from λ x xs xo, @ne_empty_of_mem α (o∩s) x ⟨xo, xs⟩ os, closure_minimal this (is_closed_compl_iff.2 oo) h ao, λ H c ⟨h₁, h₂⟩, classical.by_contradiction $ λ nc, let ⟨x, hc, hs⟩ := exists_mem_of_ne_empty (H _ h₁ nc) in hc (h₂ hs)⟩ lemma dense_iff_inter_open {s : set α} : closure s = univ ↔ ∀ U, is_open U → U ≠ ∅ → U ∩ s ≠ ∅ := begin split ; intro h, { intros U U_op U_ne, cases exists_mem_of_ne_empty U_ne with x x_in, exact mem_closure_iff.1 (by simp only [h]) U U_op x_in }, { apply eq_univ_of_forall, intro x, rw mem_closure_iff, intros U U_op x_in, exact h U U_op (ne_empty_of_mem x_in) }, end /-- The frontier of a set is the set of points between the closure and interior. -/ def frontier (s : set α) : set α := closure s \ interior s lemma frontier_eq_closure_inter_closure {s : set α} : frontier s = closure s ∩ closure (- s) := by rw [closure_compl, frontier, diff_eq] @[simp] lemma frontier_compl (s : set α) : frontier (-s) = frontier s := by simp only [frontier_eq_closure_inter_closure, lattice.neg_neg, inter_comm] lemma is_closed_frontier {s : set α} : is_closed (frontier s) := by rw frontier_eq_closure_inter_closure; exact is_closed_inter is_closed_closure is_closed_closure lemma interior_frontier {s : set α} (h : is_closed s) : interior (frontier s) = ∅ := begin have A : frontier s = s \ interior s, by rw [frontier, closure_eq_of_is_closed h], have B : interior (frontier s) ⊆ interior s, by rw A; exact interior_mono (diff_subset _ _), have C : interior (frontier s) ⊆ frontier s := interior_subset, have : interior (frontier s) ⊆ (interior s) ∩ (s \ interior s) := subset_inter B (by simpa [A] using C), rwa [inter_diff_self, subset_empty_iff] at this, end /-- neighbourhood filter -/ def nhds (a : α) : filter α := (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal s) lemma nhds_sets {a : α} : (nhds a).sets = {s | ∃t⊆s, is_open t ∧ a ∈ t} := calc (nhds a).sets = (⋃s∈{s : set α| a ∈ s ∧ is_open s}, (principal s).sets) : infi_sets_eq' (assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩, ⟨x ∩ y, ⟨⟨hx₁, hy₁⟩, is_open_inter hx₂ hy₂⟩, le_principal_iff.2 (inter_subset_left _ _), le_principal_iff.2 (inter_subset_right _ _)⟩) ⟨univ, mem_univ _, is_open_univ⟩ ... = {s | ∃t⊆s, is_open t ∧ a ∈ t} : le_antisymm (supr_le $ assume i, supr_le $ assume ⟨hi₁, hi₂⟩ t ht, ⟨i, ht, hi₂, hi₁⟩) (assume t ⟨i, hi₁, hi₂, hi₃⟩, mem_Union.2 ⟨i, mem_Union.2 ⟨⟨hi₃, hi₂⟩, hi₁⟩⟩) lemma map_nhds {a : α} {f : α → β} : map f (nhds a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal (image f s)) := calc map f (nhds a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, map f (principal s)) : map_binfi_eq (assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩, ⟨x ∩ y, ⟨⟨hx₁, hy₁⟩, is_open_inter hx₂ hy₂⟩, le_principal_iff.2 (inter_subset_left _ _), le_principal_iff.2 (inter_subset_right _ _)⟩) ⟨univ, mem_univ _, is_open_univ⟩ ... = _ : by simp only [map_principal] lemma mem_nhds_sets_iff {a : α} {s : set α} : s ∈ nhds a ↔ ∃t⊆s, is_open t ∧ a ∈ t := by simp only [nhds_sets, mem_set_of_eq, exists_prop] lemma mem_of_nhds {a : α} {s : set α} : s ∈ nhds a → a ∈ s := λ H, let ⟨t, ht, _, hs⟩ := mem_nhds_sets_iff.1 H in ht hs lemma mem_nhds_sets {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) : s ∈ nhds a := mem_nhds_sets_iff.2 ⟨s, subset.refl _, hs, ha⟩ theorem all_mem_nhds (x : α) (P : set α → Prop) (hP : ∀ s t, s ⊆ t → P s → P t) : (∀ s ∈ nhds x, P s) ↔ (∀ s, is_open s → x ∈ s → P s) := iff.intro (λ h s os xs, h s (mem_nhds_sets os xs)) (λ h t, begin change t ∈ (nhds x).sets → P t, rw nhds_sets, rintros ⟨s, hs, opens, xs⟩, exact hP _ _ hs (h s opens xs), end) theorem all_mem_nhds_filter (x : α) (f : set α → set β) (hf : ∀ s t, s ⊆ t → f s ⊆ f t) (l : filter β) : (∀ s ∈ nhds x, f s ∈ l) ↔ (∀ s, is_open s → x ∈ s → f s ∈ l) := all_mem_nhds _ _ (λ s t ssubt h, mem_sets_of_superset h (hf s t ssubt)) theorem rtendsto_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto r l (nhds a) ↔ (∀ s, is_open s → a ∈ s → r.core s ∈ l) := all_mem_nhds_filter _ _ (λ s t h, λ x hx, λ y hy, h (hx y hy)) _ theorem rtendsto'_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto' r l (nhds a) ↔ (∀ s, is_open s → a ∈ s → r.preimage s ∈ l) := by { rw [rtendsto'_def], apply all_mem_nhds_filter, apply rel.preimage_mono } theorem ptendsto_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto f l (nhds a) ↔ (∀ s, is_open s → a ∈ s → f.core s ∈ l) := rtendsto_nhds theorem ptendsto'_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto' f l (nhds a) ↔ (∀ s, is_open s → a ∈ s → f.preimage s ∈ l) := rtendsto'_nhds theorem tendsto_nhds {f : β → α} {l : filter β} {a : α} : tendsto f l (nhds a) ↔ (∀ s, is_open s → a ∈ s → f ⁻¹' s ∈ l) := all_mem_nhds_filter _ _ (λ s t h, preimage_mono h) _ lemma tendsto_const_nhds {a : α} {f : filter β} : tendsto (λb:β, a) f (nhds a) := tendsto_nhds.mpr $ assume s hs ha, univ_mem_sets' $ assume _, ha lemma pure_le_nhds : pure ≤ (nhds : α → filter α) := assume a, le_infi $ assume s, le_infi $ assume ⟨h₁, _⟩, principal_mono.mpr $ singleton_subset_iff.2 h₁ lemma tendsto_pure_nhds [topological_space β] (f : α → β) (a : α) : tendsto f (pure a) (nhds (f a)) := begin rw [tendsto, filter.map_pure], exact pure_le_nhds (f a) end @[simp] lemma nhds_neq_bot {a : α} : nhds a ≠ ⊥ := assume : nhds a = ⊥, have pure a = (⊥ : filter α), from lattice.bot_unique $ this ▸ pure_le_nhds a, pure_neq_bot this lemma interior_eq_nhds {s : set α} : interior s = {a | nhds a ≤ principal s} := set.ext $ λ x, by simp only [mem_interior, le_principal_iff, mem_nhds_sets_iff]; refl lemma mem_interior_iff_mem_nhds {s : set α} {a : α} : a ∈ interior s ↔ s ∈ nhds a := by simp only [interior_eq_nhds, le_principal_iff]; refl lemma is_open_iff_nhds {s : set α} : is_open s ↔ ∀a∈s, nhds a ≤ principal s := calc is_open s ↔ s ⊆ interior s : subset_interior_iff_open.symm ... ↔ (∀a∈s, nhds a ≤ principal s) : by rw [interior_eq_nhds]; refl lemma is_open_iff_mem_nhds {s : set α} : is_open s ↔ ∀a∈s, s ∈ nhds a := is_open_iff_nhds.trans $ forall_congr $ λ _, imp_congr_right $ λ _, le_principal_iff lemma closure_eq_nhds {s : set α} : closure s = {a | nhds a ⊓ principal s ≠ ⊥} := calc closure s = - interior (- s) : closure_eq_compl_interior_compl ... = {a | ¬ nhds a ≤ principal (-s)} : by rw [interior_eq_nhds]; refl ... = {a | nhds a ⊓ principal s ≠ ⊥} : set.ext $ assume a, not_congr (inf_eq_bot_iff_le_compl (show principal s ⊔ principal (-s) = ⊤, by simp only [sup_principal, union_compl_self, principal_univ]) (by simp only [inf_principal, inter_compl_self, principal_empty])).symm theorem mem_closure_iff_nhds {s : set α} {a : α} : a ∈ closure s ↔ ∀ t ∈ nhds a, t ∩ s ≠ ∅ := mem_closure_iff.trans ⟨λ H t ht, subset_ne_empty (inter_subset_inter_left _ interior_subset) (H _ is_open_interior (mem_interior_iff_mem_nhds.2 ht)), λ H o oo ao, H _ (mem_nhds_sets oo ao)⟩ /-- `x` belongs to the closure of `s` if and only if some ultrafilter supported on `s` converges to `x`. -/ lemma mem_closure_iff_ultrafilter {s : set α} {x : α} : x ∈ closure s ↔ ∃ (u : ultrafilter α), s ∈ u.val ∧ u.val ≤ nhds x := begin rw closure_eq_nhds, change nhds x ⊓ principal s ≠ ⊥ ↔ _, symmetry, convert exists_ultrafilter_iff _, ext u, rw [←le_principal_iff, inf_comm, le_inf_iff] end lemma is_closed_iff_nhds {s : set α} : is_closed s ↔ ∀a, nhds a ⊓ principal s ≠ ⊥ → a ∈ s := calc is_closed s ↔ closure s = s : by rw [closure_eq_iff_is_closed] ... ↔ closure s ⊆ s : ⟨assume h, by rw h, assume h, subset.antisymm h subset_closure⟩ ... ↔ (∀a, nhds a ⊓ principal s ≠ ⊥ → a ∈ s) : by rw [closure_eq_nhds]; refl lemma closure_inter_open {s t : set α} (h : is_open s) : s ∩ closure t ⊆ closure (s ∩ t) := assume a ⟨hs, ht⟩, have s ∈ nhds a, from mem_nhds_sets h hs, have nhds a ⊓ principal s = nhds a, from inf_of_le_left $ by rwa le_principal_iff, have nhds a ⊓ principal (s ∩ t) ≠ ⊥, from calc nhds a ⊓ principal (s ∩ t) = nhds a ⊓ (principal s ⊓ principal t) : by rw inf_principal ... = nhds a ⊓ principal t : by rw [←inf_assoc, this] ... ≠ ⊥ : by rw [closure_eq_nhds] at ht; assumption, by rw [closure_eq_nhds]; assumption lemma closure_diff {s t : set α} : closure s - closure t ⊆ closure (s - t) := calc closure s \ closure t = (- closure t) ∩ closure s : by simp only [diff_eq, inter_comm] ... ⊆ closure (- closure t ∩ s) : closure_inter_open $ is_open_compl_iff.mpr $ is_closed_closure ... = closure (s \ closure t) : by simp only [diff_eq, inter_comm] ... ⊆ closure (s \ t) : closure_mono $ diff_subset_diff (subset.refl s) subset_closure lemma mem_of_closed_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} (hb : b ≠ ⊥) (hf : tendsto f b (nhds a)) (hs : is_closed s) (h : f ⁻¹' s ∈ b) : a ∈ s := have b.map f ≤ nhds a ⊓ principal s, from le_trans (le_inf (le_refl _) (le_principal_iff.mpr h)) (inf_le_inf hf (le_refl _)), is_closed_iff_nhds.mp hs a $ neq_bot_of_le_neq_bot (map_ne_bot hb) this lemma mem_of_closed_of_tendsto' {f : β → α} {x : filter β} {a : α} {s : set α} (hf : tendsto f x (nhds a)) (hs : is_closed s) (h : x ⊓ principal (f ⁻¹' s) ≠ ⊥) : a ∈ s := is_closed_iff_nhds.mp hs _ $ neq_bot_of_le_neq_bot (@map_ne_bot _ _ _ f h) $ le_inf (le_trans (map_mono $ inf_le_left) hf) $ le_trans (map_mono $ inf_le_right_of_le $ by simp only [comap_principal, le_principal_iff]; exact subset.refl _) (@map_comap_le _ _ _ f) lemma mem_closure_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} (hb : b ≠ ⊥) (hf : tendsto f b (nhds a)) (h : f ⁻¹' s ∈ b) : a ∈ closure s := mem_of_closed_of_tendsto hb hf (is_closed_closure) $ filter.mem_sets_of_superset h (preimage_mono subset_closure) section lim variables [inhabited α] /-- If `f` is a filter, then `lim f` is a limit of the filter, if it exists. -/ noncomputable def lim (f : filter α) : α := epsilon $ λa, f ≤ nhds a lemma lim_spec {f : filter α} (h : ∃a, f ≤ nhds a) : f ≤ nhds (lim f) := epsilon_spec h end lim /- The nhds_within filter. -/ def nhds_within (a : α) (s : set α) : filter α := nhds a ⊓ principal s theorem nhds_within_eq (a : α) (s : set α) : nhds_within a s = ⨅ t ∈ {t : set α | a ∈ t ∧ is_open t}, principal (t ∩ s) := have set.univ ∈ {s : set α | a ∈ s ∧ is_open s}, from ⟨set.mem_univ _, is_open_univ⟩, begin rw [nhds_within, nhds, lattice.binfi_inf]; try { exact this }, simp only [inf_principal] end theorem nhds_within_univ (a : α) : nhds_within a set.univ = nhds a := by rw [nhds_within, principal_univ, lattice.inf_top_eq] theorem mem_nhds_within (t : set α) (a : α) (s : set α) : t ∈ nhds_within a s ↔ ∃ u, is_open u ∧ a ∈ u ∧ u ∩ s ⊆ t := begin rw [nhds_within, mem_inf_principal, mem_nhds_sets_iff], split, { rintros ⟨u, hu, openu, au⟩, exact ⟨u, openu, au, λ x ⟨xu, xs⟩, hu xu xs⟩ }, rintros ⟨u, openu, au, hu⟩, exact ⟨u, λ x xu xs, hu ⟨xu, xs⟩, openu, au⟩ end theorem nhds_within_mono (a : α) {s t : set α} (h : s ⊆ t) : nhds_within a s ≤ nhds_within a t := lattice.inf_le_inf (le_refl _) (principal_mono.mpr h) theorem nhds_within_restrict {a : α} (s : set α) {t : set α} (h₀ : a ∈ t) (h₁ : is_open t) : nhds_within a s = nhds_within a (s ∩ t) := have s ∩ t ∈ nhds_within a s, from inter_mem_sets (mem_inf_sets_of_right (mem_principal_self s)) (mem_inf_sets_of_left (mem_nhds_sets h₁ h₀)), le_antisymm (lattice.le_inf lattice.inf_le_left (le_principal_iff.mpr this)) (lattice.inf_le_inf (le_refl _) (principal_mono.mpr (set.inter_subset_left _ _))) theorem nhds_within_eq_nhds_within {a : α} {s t u : set α} (h₀ : a ∈ s) (h₁ : is_open s) (h₂ : t ∩ s = u ∩ s) : nhds_within a t = nhds_within a u := by rw [nhds_within_restrict t h₀ h₁, nhds_within_restrict u h₀ h₁, h₂] theorem nhds_within_eq_of_open {a : α} {s : set α} (h₀ : a ∈ s) (h₁ : is_open s) : nhds_within a s = nhds a := by rw [←nhds_within_univ]; apply nhds_within_eq_nhds_within h₀ h₁; rw [set.univ_inter, set.inter_self] @[simp] theorem nhds_within_empty (a : α) : nhds_within a {} = ⊥ := by rw [nhds_within, principal_empty, lattice.inf_bot_eq] theorem nhds_within_union (a : α) (s t : set α) : nhds_within a (s ∪ t) = nhds_within a s ⊔ nhds_within a t := by unfold nhds_within; rw [←lattice.inf_sup_left, sup_principal] theorem nhds_within_inter (a : α) (s t : set α) : nhds_within a (s ∩ t) = nhds_within a s ⊓ nhds_within a t := by unfold nhds_within; rw [lattice.inf_left_comm, lattice.inf_assoc, inf_principal, ←lattice.inf_assoc, lattice.inf_idem] theorem nhds_within_inter' (a : α) (s t : set α) : nhds_within a (s ∩ t) = (nhds_within a s) ⊓ principal t := by { unfold nhds_within, rw [←inf_principal, lattice.inf_assoc] } theorem tendsto_if_nhds_within {f g : α → β} {p : α → Prop} [decidable_pred p] {a : α} {s : set α} {l : filter β} (h₀ : tendsto f (nhds_within a (s ∩ p)) l) (h₁ : tendsto g (nhds_within a (s ∩ {x | ¬ p x})) l) : tendsto (λ x, if p x then f x else g x) (nhds_within a s) l := by apply tendsto_if; rw [←nhds_within_inter']; assumption lemma map_nhds_within (f : α → β) (a : α) (s : set α) : map f (nhds_within a s) = ⨅ t ∈ {t : set α | a ∈ t ∧ is_open t}, principal (set.image f (t ∩ s)) := have h₀ : directed_on ((λ (i : set α), principal (i ∩ s)) ⁻¹'o ge) {x : set α | x ∈ {t : set α | a ∈ t ∧ is_open t}}, from assume x ⟨ax, openx⟩ y ⟨ay, openy⟩, ⟨x ∩ y, ⟨⟨ax, ay⟩, is_open_inter openx openy⟩, le_principal_iff.mpr (set.inter_subset_inter_left _ (set.inter_subset_left _ _)), le_principal_iff.mpr (set.inter_subset_inter_left _ (set.inter_subset_right _ _))⟩, have h₁ : ∃ (i : set α), i ∈ {t : set α | a ∈ t ∧ is_open t}, from ⟨set.univ, set.mem_univ _, is_open_univ⟩, by { rw [nhds_within_eq, map_binfi_eq h₀ h₁], simp only [map_principal] } theorem tendsto_nhds_within_mono_left {f : α → β} {a : α} {s t : set α} {l : filter β} (hst : s ⊆ t) (h : tendsto f (nhds_within a t) l) : tendsto f (nhds_within a s) l := tendsto_le_left (nhds_within_mono a hst) h theorem tendsto_nhds_within_mono_right {f : β → α} {l : filter β} {a : α} {s t : set α} (hst : s ⊆ t) (h : tendsto f l (nhds_within a s)) : tendsto f l (nhds_within a t) := tendsto_le_right (nhds_within_mono a hst) h theorem tendsto_nhds_within_of_tendsto_nhds {f : α → β} {a : α} {s : set α} {l : filter β} (h : tendsto f (nhds a) l) : tendsto f (nhds_within a s) l := by rw [←nhds_within_univ] at h; exact tendsto_nhds_within_mono_left (set.subset_univ _) h /- locally finite family [General Topology (Bourbaki, 1995)] -/ section locally_finite /-- A family of sets in `set α` is locally finite if at every point `x:α`, there is a neighborhood of `x` which meets only finitely many sets in the family -/ def locally_finite (f : β → set α) := ∀x:α, ∃t ∈ nhds x, finite {i | f i ∩ t ≠ ∅ } lemma locally_finite_of_finite {f : β → set α} (h : finite (univ : set β)) : locally_finite f := assume x, ⟨univ, univ_mem_sets, finite_subset h $ subset_univ _⟩ lemma locally_finite_subset {f₁ f₂ : β → set α} (hf₂ : locally_finite f₂) (hf : ∀b, f₁ b ⊆ f₂ b) : locally_finite f₁ := assume a, let ⟨t, ht₁, ht₂⟩ := hf₂ a in ⟨t, ht₁, finite_subset ht₂ $ assume i hi, neq_bot_of_le_neq_bot hi $ inter_subset_inter (hf i) $ subset.refl _⟩ lemma is_closed_Union_of_locally_finite {f : β → set α} (h₁ : locally_finite f) (h₂ : ∀i, is_closed (f i)) : is_closed (⋃i, f i) := is_open_iff_nhds.mpr $ assume a, assume h : a ∉ (⋃i, f i), have ∀i, a ∈ -f i, from assume i hi, h $ mem_Union.2 ⟨i, hi⟩, have ∀i, - f i ∈ (nhds a).sets, by rw [nhds_sets]; exact assume i, ⟨- f i, subset.refl _, h₂ i, this i⟩, let ⟨t, h_sets, (h_fin : finite {i | f i ∩ t ≠ ∅ })⟩ := h₁ a in calc nhds a ≤ principal (t ∩ (⋂ i∈{i | f i ∩ t ≠ ∅ }, - f i)) : begin rw [le_principal_iff], apply @filter.inter_mem_sets _ (nhds a) _ _ h_sets, apply @filter.Inter_mem_sets _ (nhds a) _ _ _ h_fin, exact assume i h, this i end ... ≤ principal (- ⋃i, f i) : begin simp only [principal_mono, subset_def, mem_compl_eq, mem_inter_eq, mem_Inter, mem_set_of_eq, mem_Union, and_imp, not_exists, not_eq_empty_iff_exists, exists_imp_distrib, (≠)], exact assume x xt ht i xfi, ht i x xfi xt xfi end end locally_finite end topological_space section continuous variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables [topological_space α] [topological_space β] [topological_space γ] /-- A function between topological spaces is continuous if the preimage of every open set is open. -/ def continuous (f : α → β) := ∀s, is_open s → is_open (f ⁻¹' s) def continuous_at (f : α → β) (x : α) := tendsto f (nhds x) (nhds (f x)) def continuous_at_within (f : α → β) (x : α) (s : set α) : Prop := tendsto f (nhds_within x s) (nhds (f x)) def continuous_on (f : α → β) (s : set α) : Prop := ∀ x ∈ s, continuous_at_within f x s lemma continuous_id : continuous (id : α → α) := assume s h, h lemma continuous.comp {f : α → β} {g : β → γ} (hf : continuous f) (hg : continuous g): continuous (g ∘ f) := assume s h, hf _ (hg s h) lemma continuous.tendsto {f : α → β} (hf : continuous f) (x) : tendsto f (nhds x) (nhds (f x)) | s := show s ∈ nhds (f x) → s ∈ map f (nhds x), by simp [nhds_sets]; exact assume t t_subset t_open fx_in_t, ⟨f ⁻¹' t, preimage_mono t_subset, hf t t_open, fx_in_t⟩ lemma continuous_iff_continuous_at {f : α → β} : continuous f ↔ ∀ x, continuous_at f x := ⟨continuous.tendsto, assume hf : ∀x, tendsto f (nhds x) (nhds (f x)), assume s, assume hs : is_open s, have ∀a, f a ∈ s → s ∈ nhds (f a), by simp [nhds_sets]; exact assume a ha, ⟨s, subset.refl s, hs, ha⟩, show is_open (f ⁻¹' s), by simp [is_open_iff_nhds]; exact assume a ha, hf a (this a ha)⟩ lemma continuous_const {b : β} : continuous (λa:α, b) := continuous_iff_continuous_at.mpr $ assume a, tendsto_const_nhds lemma continuous_iff_is_closed {f : α → β} : continuous f ↔ (∀s, is_closed s → is_closed (f ⁻¹' s)) := ⟨assume hf s hs, hf (-s) hs, assume hf s, by rw [←is_closed_compl_iff, ←is_closed_compl_iff]; exact hf _⟩ lemma continuous_at_iff_ultrafilter {f : α → β} (x) : continuous_at f x ↔ ∀ g, is_ultrafilter g → g ≤ nhds x → g.map f ≤ nhds (f x) := tendsto_iff_ultrafilter f (nhds x) (nhds (f x)) lemma continuous_iff_ultrafilter {f : α → β} : continuous f ↔ ∀ x g, is_ultrafilter g → g ≤ nhds x → g.map f ≤ nhds (f x) := by simp only [continuous_iff_continuous_at, continuous_at_iff_ultrafilter] lemma continuous_if {p : α → Prop} {f g : α → β} {h : ∀a, decidable (p a)} (hp : ∀a∈frontier {a | p a}, f a = g a) (hf : continuous f) (hg : continuous g) : continuous (λa, @ite (p a) (h a) β (f a) (g a)) := continuous_iff_is_closed.mpr $ assume s hs, have (λa, ite (p a) (f a) (g a)) ⁻¹' s = (closure {a | p a} ∩ f ⁻¹' s) ∪ (closure {a | ¬ p a} ∩ g ⁻¹' s), from set.ext $ assume a, classical.by_cases (assume : a ∈ frontier {a | p a}, have hac : a ∈ closure {a | p a}, from this.left, have hai : a ∈ closure {a | ¬ p a}, from have a ∈ - interior {a | p a}, from this.right, by rwa [←closure_compl] at this, by by_cases p a; simp [h, hp a this, hac, hai, iff_def] {contextual := tt}) (assume hf : a ∈ - frontier {a | p a}, classical.by_cases (assume : p a, have hc : a ∈ closure {a | p a}, from subset_closure this, have hnc : a ∉ closure {a | ¬ p a}, by show a ∉ closure (- {a | p a}); rw [closure_compl]; simpa [frontier, hc] using hf, by simp [this, hc, hnc]) (assume : ¬ p a, have hc : a ∈ closure {a | ¬ p a}, from subset_closure this, have hnc : a ∉ closure {a | p a}, begin have hc : a ∈ closure (- {a | p a}), from hc, simp [closure_compl] at hc, simpa [frontier, hc] using hf end, by simp [this, hc, hnc])), by rw [this]; exact is_closed_union (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hf s hs) (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hg s hs) /- Continuity and partial functions -/ def pcontinuous (f : α →. β) := ∀ s, is_open s → is_open (f.preimage s) lemma open_dom_of_pcontinuous {f : α →. β} (h : pcontinuous f) : is_open f.dom := by rw [←pfun.preimage_univ]; exact h _ is_open_univ lemma pcontinuous_iff' {f : α →. β} : pcontinuous f ↔ ∀ {x y} (h : y ∈ f x), ptendsto' f (nhds x) (nhds y) := begin split, { intros h x y h', rw [ptendsto'_def], change ∀ (s : set β), s ∈ (nhds y).sets → pfun.preimage f s ∈ (nhds x).sets, rw [nhds_sets, nhds_sets], rintros s ⟨t, tsubs, opent, yt⟩, exact ⟨f.preimage t, pfun.preimage_mono _ tsubs, h _ opent, ⟨y, yt, h'⟩⟩ }, intros hf s os, rw is_open_iff_nhds, rintros x ⟨y, ys, fxy⟩ t, rw [mem_principal_sets], assume h : f.preimage s ⊆ t, change t ∈ nhds x, apply mem_sets_of_superset _ h, have h' : ∀ s ∈ nhds y, f.preimage s ∈ nhds x, { intros s hs, have : ptendsto' f (nhds x) (nhds y) := hf fxy, rw ptendsto'_def at this, exact this s hs }, show f.preimage s ∈ nhds x, apply h', rw mem_nhds_sets_iff, exact ⟨s, set.subset.refl _, os, ys⟩ end lemma image_closure_subset_closure_image {f : α → β} {s : set α} (h : continuous f) : f '' closure s ⊆ closure (f '' s) := have ∀ (a : α), nhds a ⊓ principal s ≠ ⊥ → nhds (f a) ⊓ principal (f '' s) ≠ ⊥, from assume a ha, have h₁ : ¬ map f (nhds a ⊓ principal s) = ⊥, by rwa[map_eq_bot_iff], have h₂ : map f (nhds a ⊓ principal s) ≤ nhds (f a) ⊓ principal (f '' s), from le_inf (le_trans (map_mono inf_le_left) $ by rw [continuous_iff_continuous_at] at h; exact h a) (le_trans (map_mono inf_le_right) $ by simp; exact subset.refl _), neq_bot_of_le_neq_bot h₁ h₂, by simp [image_subset_iff, closure_eq_nhds]; assumption lemma mem_closure [topological_space α] [topological_space β] {s : set α} {t : set β} {f : α → β} {a : α} (hf : continuous f) (ha : a ∈ closure s) (ht : ∀a∈s, f a ∈ t) : f a ∈ closure t := subset.trans (image_closure_subset_closure_image hf) (closure_mono $ image_subset_iff.2 ht) $ (mem_image_of_mem f ha) end continuous
ee6e0d0dd0d187b3df5ce36bedd9bec7d9fc77c5
fe25de614feb5587799621c41487aaee0d083b08
/stage0/src/Lean/Meta/Tactic/Contradiction.lean
3f84f4fcac865eadfdbb9fdfc32e870c866ffd36
[ "Apache-2.0" ]
permissive
pollend/lean4
e8469c2f5fb8779b773618c3267883cf21fb9fac
c913886938c4b3b83238a3f99673c6c5a9cec270
refs/heads/master
1,687,973,251,481
1,628,039,739,000
1,628,039,739,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,601
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.MatchUtil import Lean.Meta.Tactic.Assumption import Lean.Meta.Tactic.Cases namespace Lean.Meta def elimEmptyInductive (mvarId : MVarId) (fvarId : FVarId) (searchDepth : Nat) : MetaM Bool := match searchDepth with | 0 => return false | searchDepth + 1 => withMVarContext mvarId do let localDecl ← getLocalDecl fvarId let type ← whnfD localDecl.type matchConstInduct type.getAppFn (fun _ => pure false) fun info _ => do if info.ctors.length == 0 || info.numIndices > 0 then -- We only consider inductives with no constructors and indexed families commitWhen do let subgoals ← try cases mvarId fvarId catch _ => return false for subgoal in subgoals do -- If one of the fields is uninhabited, then we are done let mut found := false for field in subgoal.fields do let field := subgoal.subst.apply field if field.isFVar then if (← elimEmptyInductive subgoal.mvarId field.fvarId! searchDepth) then found := true break unless found == true do -- TODO: check why we need true here return false return true else return false def contradictionCore (mvarId : MVarId) (useDecide : Bool) (searchDepth : Nat) : MetaM Bool := do withMVarContext mvarId do checkNotAssigned mvarId `contradiction for localDecl in (← getLCtx) do unless localDecl.isAuxDecl do -- (h : ¬ p) (h' : p) if let some p ← matchNot? localDecl.type then if let some pFVarId ← findLocalDeclWithType? p then assignExprMVar mvarId (← mkAbsurd (← getMVarType mvarId) (mkFVar pFVarId) localDecl.toExpr) return true -- (h : <empty-inductive-type>) if (← elimEmptyInductive mvarId localDecl.fvarId searchDepth) then return true -- (h : x ≠ x) if let some (_, lhs, rhs) ← matchNe? localDecl.type then if (← isDefEq lhs rhs) then assignExprMVar mvarId (← mkAbsurd (← getMVarType mvarId) (← mkEqRefl lhs) localDecl.toExpr) return true -- (h : ctor₁ ... = ctor₂ ...) if let some (_, lhs, rhs) ← matchEq? localDecl.type then if let some lhsCtor ← matchConstructorApp? lhs then if let some rhsCtor ← matchConstructorApp? rhs then if lhsCtor.name != rhsCtor.name then assignExprMVar mvarId (← mkNoConfusion (← getMVarType mvarId) localDecl.toExpr) return true -- (h : p) s.t. `decide p` evaluates to `false` if useDecide && !localDecl.type.hasFVar && !localDecl.type.hasMVar then try let d ← mkDecide localDecl.type let r ← withDefault <| whnf d if r.isConstOf ``false then let hn := mkAppN (mkConst ``ofDecideEqFalse) <| d.getAppArgs.push (← mkEqRefl d) assignExprMVar mvarId (← mkAbsurd (← getMVarType mvarId) localDecl.toExpr hn) return true catch _ => pure () return false def contradiction (mvarId : MVarId) (useDecide : Bool := true) (searchDepth : Nat := 2) : MetaM Unit := unless (← contradictionCore mvarId useDecide searchDepth) do throwTacticEx `contradiction mvarId "" end Lean.Meta
621d0974aa44dfee9e29719afdca9243ed13917c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/monoidal/internal/limits.lean
d66b5de8e8f2e8a9654845cf6f30638a569987c2
[]
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
4,358
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.monoidal.internal.functor_category import Mathlib.category_theory.monoidal.limits import Mathlib.category_theory.limits.preserves.basic import Mathlib.PostPort universes v u namespace Mathlib /-! # Limits of monoid objects. If `C` has limits, so does `Mon_ C`, and the forgetful functor preserves these limits. (This could potentially replace many individual constructions for concrete categories, in particular `Mon`, `SemiRing`, `Ring`, and `Algebra R`.) -/ namespace Mon_ /-- We construct the (candidate) limit of a functor `F : J ⥤ Mon_ C` by interpreting it as a functor `Mon_ (J ⥤ C)`, and noting that taking limits is a lax monoidal functor, and hence sends monoid objects to monoid objects. -/ def limit {J : Type v} [category_theory.small_category J] {C : Type u} [category_theory.category C] [category_theory.limits.has_limits C] [category_theory.monoidal_category C] (F : J ⥤ Mon_ C) : Mon_ C := category_theory.functor.obj (category_theory.lax_monoidal_functor.map_Mon category_theory.limits.lim_lax) (category_theory.functor.obj category_theory.monoidal.Mon_functor_category_equivalence.inverse F) /-- Implementation of `Mon_.has_limits`: a limiting cone over a functor `F : J ⥤ Mon_ C`. -/ def limit_cone {J : Type v} [category_theory.small_category J] {C : Type u} [category_theory.category C] [category_theory.limits.has_limits C] [category_theory.monoidal_category C] (F : J ⥤ Mon_ C) : category_theory.limits.cone F := category_theory.limits.cone.mk (limit F) (category_theory.nat_trans.mk fun (j : J) => hom.mk (category_theory.limits.limit.π (F ⋙ forget C) j)) /-- The image of the proposed limit cone for `F : J ⥤ Mon_ C` under the forgetful functor `forget C : Mon_ C ⥤ C` is isomorphic to the limit cone of `F ⋙ forget C`. -/ def forget_map_cone_limit_cone_iso {J : Type v} [category_theory.small_category J] {C : Type u} [category_theory.category C] [category_theory.limits.has_limits C] [category_theory.monoidal_category C] (F : J ⥤ Mon_ C) : category_theory.functor.map_cone (forget C) (limit_cone F) ≅ category_theory.limits.limit.cone (F ⋙ forget C) := category_theory.limits.cones.ext (category_theory.iso.refl (category_theory.limits.cone.X (category_theory.functor.map_cone (forget C) (limit_cone F)))) sorry /-- Implementation of `Mon_.has_limits`: the proposed cone over a functor `F : J ⥤ Mon_ C` is a limit cone. -/ def limit_cone_is_limit {J : Type v} [category_theory.small_category J] {C : Type u} [category_theory.category C] [category_theory.limits.has_limits C] [category_theory.monoidal_category C] (F : J ⥤ Mon_ C) : category_theory.limits.is_limit (limit_cone F) := category_theory.limits.is_limit.mk fun (s : category_theory.limits.cone F) => hom.mk (category_theory.limits.limit.lift (F ⋙ forget C) (category_theory.functor.map_cone (forget C) s)) protected instance has_limits {C : Type u} [category_theory.category C] [category_theory.limits.has_limits C] [category_theory.monoidal_category C] : category_theory.limits.has_limits (Mon_ C) := category_theory.limits.has_limits.mk fun (J : Type v) (𝒥 : category_theory.small_category J) => category_theory.limits.has_limits_of_shape.mk fun (F : J ⥤ Mon_ C) => category_theory.limits.has_limit.mk (category_theory.limits.limit_cone.mk (limit_cone F) (limit_cone_is_limit F)) protected instance forget_preserves_limits {C : Type u} [category_theory.category C] [category_theory.limits.has_limits C] [category_theory.monoidal_category C] : category_theory.limits.preserves_limits (forget C) := category_theory.limits.preserves_limits.mk fun (J : Type v) (𝒥 : category_theory.small_category J) => category_theory.limits.preserves_limits_of_shape.mk fun (F : J ⥤ Mon_ C) => category_theory.limits.preserves_limit_of_preserves_limit_cone (limit_cone_is_limit F) (category_theory.limits.is_limit.of_iso_limit (category_theory.limits.limit.is_limit (F ⋙ forget C)) (category_theory.iso.symm (forget_map_cone_limit_cone_iso F)))
907083aa4893ab0de78d844b5a9e5a60a50d63f9
969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb
/src/topology/metric_space/kuratowski.lean
d2c715fa54d702545791bad1cf13b10a4d94f4e4
[ "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,948
lean
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import topology.metric_space.isometry import topology.bounded_continuous_function import topology.compacts /-! # The Kuratowski embedding Any separable metric space can be embedded isometrically in `ℓ^∞(ℝ)`. -/ noncomputable theory open set universes u v w variables {α : Type u} {β : Type v} {γ : Type w} /-- The space of bounded sequences, with its sup norm -/ @[reducible] def ℓ_infty_ℝ : Type := bounded_continuous_function ℕ ℝ open bounded_continuous_function metric topological_space namespace Kuratowski_embedding /-! ### Any separable metric space can be embedded isometrically in ℓ^∞(ℝ) -/ variables {f g : ℓ_infty_ℝ} {n : ℕ} {C : ℝ} [metric_space α] (x : ℕ → α) (a b : α) /-- A metric space can be embedded in `l^∞(ℝ)` via the distances to points in a fixed countable set, if this set is dense. This map is given in `Kuratowski_embedding`, without density assumptions. -/ def embedding_of_subset : ℓ_infty_ℝ := of_normed_group_discrete (λn, dist a (x n) - dist (x 0) (x n)) (dist a (x 0)) (λ_, abs_dist_sub_le _ _ _) lemma embedding_of_subset_coe : embedding_of_subset x a n = dist a (x n) - dist (x 0) (x n) := rfl /-- The embedding map is always a semi-contraction. -/ lemma embedding_of_subset_dist_le (a b : α) : dist (embedding_of_subset x a) (embedding_of_subset x b) ≤ dist a b := begin refine (dist_le dist_nonneg).2 (λn, _), simp only [embedding_of_subset_coe, real.dist_eq], convert abs_dist_sub_le a b (x n) using 2, ring end /-- When the reference set is dense, the embedding map is an isometry on its image. -/ lemma embedding_of_subset_isometry (H : dense_range x) : isometry (embedding_of_subset x) := begin refine isometry_emetric_iff_metric.2 (λa b, _), refine (embedding_of_subset_dist_le x a b).antisymm (le_of_forall_pos_le_add (λe epos, _)), /- First step: find n with dist a (x n) < e -/ rcases metric.mem_closure_range_iff.1 (H a) (e/2) (half_pos epos) with ⟨n, hn⟩, /- Second step: use the norm control at index n to conclude -/ have C : dist b (x n) - dist a (x n) = embedding_of_subset x b n - embedding_of_subset x a n := by { simp only [embedding_of_subset_coe, sub_sub_sub_cancel_right] }, have := calc dist a b ≤ dist a (x n) + dist (x n) b : dist_triangle _ _ _ ... = 2 * dist a (x n) + (dist b (x n) - dist a (x n)) : by { simp [dist_comm], ring } ... ≤ 2 * dist a (x n) + abs (dist b (x n) - dist a (x n)) : by apply_rules [add_le_add_left, le_abs_self] ... ≤ 2 * (e/2) + abs (embedding_of_subset x b n - embedding_of_subset x a n) : begin rw C, apply_rules [add_le_add, mul_le_mul_of_nonneg_left, hn.le, le_refl], norm_num end ... ≤ 2 * (e/2) + dist (embedding_of_subset x b) (embedding_of_subset x a) : by simp [← real.dist_eq, dist_coe_le_dist] ... = dist (embedding_of_subset x b) (embedding_of_subset x a) + e : by ring, simpa [dist_comm] using this end /-- Every separable metric space embeds isometrically in `ℓ_infty_ℝ`. -/ theorem exists_isometric_embedding (α : Type u) [metric_space α] [separable_space α] : ∃(f : α → ℓ_infty_ℝ), isometry f := begin cases (univ : set α).eq_empty_or_nonempty with h h, { use (λ_, 0), assume x, exact absurd h (nonempty.ne_empty ⟨x, mem_univ x⟩) }, { /- We construct a map x : ℕ → α with dense image -/ rcases h with ⟨basepoint⟩, haveI : inhabited α := ⟨basepoint⟩, have : ∃s:set α, countable s ∧ dense s := exists_countable_dense α, rcases this with ⟨S, ⟨S_countable, S_dense⟩⟩, rcases countable_iff_exists_surjective.1 S_countable with ⟨x, x_range⟩, /- Use embedding_of_subset to construct the desired isometry -/ exact ⟨embedding_of_subset x, embedding_of_subset_isometry x (S_dense.mono x_range)⟩ } end end Kuratowski_embedding open topological_space Kuratowski_embedding /-- The Kuratowski embedding is an isometric embedding of a separable metric space in `ℓ^∞(ℝ)`. -/ def Kuratowski_embedding (α : Type u) [metric_space α] [separable_space α] : α → ℓ_infty_ℝ := classical.some (Kuratowski_embedding.exists_isometric_embedding α) /-- The Kuratowski embedding is an isometry. -/ protected lemma Kuratowski_embedding.isometry (α : Type u) [metric_space α] [separable_space α] : isometry (Kuratowski_embedding α) := classical.some_spec (exists_isometric_embedding α) /-- Version of the Kuratowski embedding for nonempty compacts -/ def nonempty_compacts.Kuratowski_embedding (α : Type u) [metric_space α] [compact_space α] [nonempty α] : nonempty_compacts ℓ_infty_ℝ := ⟨range (Kuratowski_embedding α), range_nonempty _, compact_range (Kuratowski_embedding.isometry α).continuous⟩
996778fee3f8349d7f006b2234a97273e1b5b7bb
8eeb99d0fdf8125f5d39a0ce8631653f588ee817
/src/data/set/finite.lean
93816bd20bde7108042c6386ce324a58f8bdde20
[ "Apache-2.0" ]
permissive
jesse-michael-han/mathlib
a15c58378846011b003669354cbab7062b893cfe
fa6312e4dc971985e6b7708d99a5bc3062485c89
refs/heads/master
1,625,200,760,912
1,602,081,753,000
1,602,081,753,000
181,787,230
0
0
null
1,555,460,682,000
1,555,460,682,000
null
UTF-8
Lean
false
false
24,526
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import data.fintype.basic /-! # Finite sets This file defines predicates `finite : set α → Prop` and `infinite : set α → Prop` and proves some basic facts about finite sets. -/ open set function universes u v w x variables {α : Type u} {β : Type v} {ι : Sort w} {γ : Type x} namespace set /-- A set is finite if the subtype is a fintype, i.e. there is a list that enumerates its members. -/ def finite (s : set α) : Prop := nonempty (fintype s) /-- A set is infinite if it is not finite. -/ def infinite (s : set α) : Prop := ¬ finite s /-- The subtype corresponding to a finite set is a finite type. Note that because `finite` isn't a typeclass, this will not fire if it is made into an instance -/ noncomputable def finite.fintype {s : set α} (h : finite s) : fintype s := classical.choice h /-- Get a finset from a finite set -/ noncomputable def finite.to_finset {s : set α} (h : finite s) : finset α := @set.to_finset _ _ h.fintype @[simp] theorem finite.mem_to_finset {s : set α} {h : finite s} {a : α} : a ∈ h.to_finset ↔ a ∈ s := @mem_to_finset _ _ h.fintype _ @[simp] theorem finite.to_finset.nonempty {s : set α} (h : finite s) : h.to_finset.nonempty ↔ s.nonempty := show (∃ x, x ∈ h.to_finset) ↔ (∃ x, x ∈ s), from exists_congr (λ _, finite.mem_to_finset) @[simp] lemma finite.coe_to_finset {α} {s : set α} (h : finite s) : ↑h.to_finset = s := @set.coe_to_finset _ s h.fintype theorem finite.exists_finset {s : set α} : finite s → ∃ s' : finset α, ∀ a : α, a ∈ s' ↔ a ∈ s | ⟨h⟩ := by exactI ⟨to_finset s, λ _, mem_to_finset⟩ theorem finite.exists_finset_coe {s : set α} (hs : finite s) : ∃ s' : finset α, ↑s' = s := ⟨hs.to_finset, hs.coe_to_finset⟩ /-- Finite sets can be lifted to finsets. -/ instance : can_lift (set α) (finset α) := { coe := coe, cond := finite, prf := λ s hs, hs.exists_finset_coe } theorem finite_mem_finset (s : finset α) : finite {a | a ∈ s} := ⟨fintype.of_finset s (λ _, iff.rfl)⟩ theorem finite.of_fintype [fintype α] (s : set α) : finite s := by classical; exact ⟨set_fintype s⟩ theorem exists_finite_iff_finset {p : set α → Prop} : (∃ s, finite s ∧ p s) ↔ ∃ s : finset α, p ↑s := ⟨λ ⟨s, hs, hps⟩, ⟨hs.to_finset, hs.coe_to_finset.symm ▸ hps⟩, λ ⟨s, hs⟩, ⟨↑s, finite_mem_finset s, hs⟩⟩ /-- Membership of a subset of a finite type is decidable. Using this as an instance leads to potential loops with `subtype.fintype` under certain decidability assumptions, so it should only be declared a local instance. -/ def decidable_mem_of_fintype [decidable_eq α] (s : set α) [fintype s] (a) : decidable (a ∈ s) := decidable_of_iff _ mem_to_finset instance fintype_empty : fintype (∅ : set α) := fintype.of_finset ∅ $ by simp theorem empty_card : fintype.card (∅ : set α) = 0 := rfl @[simp] theorem empty_card' {h : fintype.{u} (∅ : set α)} : @fintype.card (∅ : set α) h = 0 := eq.trans (by congr) empty_card @[simp] theorem finite_empty : @finite α ∅ := ⟨set.fintype_empty⟩ instance finite.inhabited : inhabited {s : set α // finite s} := ⟨⟨∅, finite_empty⟩⟩ /-- A `fintype` structure on `insert a s`. -/ def fintype_insert' {a : α} (s : set α) [fintype s] (h : a ∉ s) : fintype (insert a s : set α) := fintype.of_finset ⟨a :: s.to_finset.1, multiset.nodup_cons_of_nodup (by simp [h]) s.to_finset.2⟩ $ by simp theorem card_fintype_insert' {a : α} (s : set α) [fintype s] (h : a ∉ s) : @fintype.card _ (fintype_insert' s h) = fintype.card s + 1 := by rw [fintype_insert', fintype.card_of_finset]; simp [finset.card, to_finset]; refl @[simp] theorem card_insert {a : α} (s : set α) [fintype s] (h : a ∉ s) {d : fintype.{u} (insert a s : set α)} : @fintype.card _ d = fintype.card s + 1 := by rw ← card_fintype_insert' s h; congr lemma card_image_of_inj_on {s : set α} [fintype s] {f : α → β} [fintype (f '' s)] (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : fintype.card (f '' s) = fintype.card s := by haveI := classical.prop_decidable; exact calc fintype.card (f '' s) = (s.to_finset.image f).card : fintype.card_of_finset' _ (by simp) ... = s.to_finset.card : finset.card_image_of_inj_on (λ x hx y hy hxy, H x (mem_to_finset.1 hx) y (mem_to_finset.1 hy) hxy) ... = fintype.card s : (fintype.card_of_finset' _ (λ a, mem_to_finset)).symm lemma card_image_of_injective (s : set α) [fintype s] {f : α → β} [fintype (f '' s)] (H : function.injective f) : fintype.card (f '' s) = fintype.card s := card_image_of_inj_on $ λ _ _ _ _ h, H h section local attribute [instance] decidable_mem_of_fintype instance fintype_insert [decidable_eq α] (a : α) (s : set α) [fintype s] : fintype (insert a s : set α) := if h : a ∈ s then by rwa [insert_eq, union_eq_self_of_subset_left (singleton_subset_iff.2 h)] else fintype_insert' _ h end @[simp] theorem finite.insert (a : α) {s : set α} : finite s → finite (insert a s) | ⟨h⟩ := ⟨@set.fintype_insert _ (classical.dec_eq α) _ _ h⟩ lemma to_finset_insert [decidable_eq α] {a : α} {s : set α} (hs : finite s) : (hs.insert a).to_finset = insert a hs.to_finset := finset.ext $ by simp @[elab_as_eliminator] theorem finite.induction_on {C : set α → Prop} {s : set α} (h : finite s) (H0 : C ∅) (H1 : ∀ {a s}, a ∉ s → finite s → C s → C (insert a s)) : C s := let ⟨t⟩ := h in by exactI match s.to_finset, @mem_to_finset _ s _ with | ⟨l, nd⟩, al := begin change ∀ a, a ∈ l ↔ a ∈ s at al, clear _let_match _match t h, revert s nd al, refine multiset.induction_on l _ (λ a l IH, _); intros s nd al, { rw show s = ∅, from eq_empty_iff_forall_not_mem.2 (by simpa using al), exact H0 }, { rw ← show insert a {x | x ∈ l} = s, from set.ext (by simpa using al), cases multiset.nodup_cons.1 nd with m nd', refine H1 _ ⟨finset.subtype.fintype ⟨l, nd'⟩⟩ (IH nd' (λ _, iff.rfl)), exact m } end end @[elab_as_eliminator] theorem finite.dinduction_on {C : ∀s:set α, finite s → Prop} {s : set α} (h : finite s) (H0 : C ∅ finite_empty) (H1 : ∀ {a s}, a ∉ s → ∀h:finite s, C s h → C (insert a s) (h.insert a)) : C s h := have ∀h:finite s, C s h, from finite.induction_on h (assume h, H0) (assume a s has hs ih h, H1 has hs (ih _)), this h instance fintype_singleton (a : α) : fintype ({a} : set α) := unique.fintype @[simp] theorem card_singleton (a : α) : fintype.card ({a} : set α) = 1 := fintype.card_of_subsingleton _ @[simp] theorem finite_singleton (a : α) : finite ({a} : set α) := ⟨set.fintype_singleton _⟩ instance fintype_pure : ∀ a : α, fintype (pure a : set α) := set.fintype_singleton theorem finite_pure (a : α) : finite (pure a : set α) := ⟨set.fintype_pure a⟩ instance fintype_univ [fintype α] : fintype (@univ α) := fintype.of_equiv α $ (equiv.set.univ α).symm theorem finite_univ [fintype α] : finite (@univ α) := ⟨set.fintype_univ⟩ theorem infinite_univ_iff : (@univ α).infinite ↔ _root_.infinite α := ⟨λ h₁, ⟨λ h₂, h₁ $ @finite_univ α h₂⟩, λ ⟨h₁⟩ ⟨h₂⟩, h₁ $ @fintype.of_equiv _ _ h₂ $ equiv.set.univ _⟩ theorem infinite_univ [h : _root_.infinite α] : infinite (@univ α) := infinite_univ_iff.2 h theorem infinite_coe_iff {s : set α} : _root_.infinite s ↔ infinite s := ⟨λ ⟨h₁⟩ h₂, h₁ h₂.some, λ h₁, ⟨λ h₂, h₁ ⟨h₂⟩⟩⟩ theorem infinite.to_subtype {s : set α} (h : infinite s) : _root_.infinite s := infinite_coe_iff.2 h /-- Embedding of `ℕ` into an infinite set. -/ noncomputable def infinite.nat_embedding (s : set α) (h : infinite s) : ℕ ↪ s := by { haveI := h.to_subtype, exact infinite.nat_embedding s } lemma infinite.exists_subset_card_eq {s : set α} (hs : infinite s) (n : ℕ) : ∃ t : finset α, ↑t ⊆ s ∧ t.card = n := ⟨((finset.range n).map (hs.nat_embedding _)).map (embedding.subtype _), by simp⟩ instance fintype_union [decidable_eq α] (s t : set α) [fintype s] [fintype t] : fintype (s ∪ t : set α) := fintype.of_finset (s.to_finset ∪ t.to_finset) $ by simp theorem finite.union {s t : set α} : finite s → finite t → finite (s ∪ t) | ⟨hs⟩ ⟨ht⟩ := ⟨@set.fintype_union _ (classical.dec_eq α) _ _ hs ht⟩ instance fintype_sep (s : set α) (p : α → Prop) [fintype s] [decidable_pred p] : fintype ({a ∈ s | p a} : set α) := fintype.of_finset (s.to_finset.filter p) $ by simp instance fintype_inter (s t : set α) [fintype s] [decidable_pred t] : fintype (s ∩ t : set α) := set.fintype_sep s t /-- A `fintype` structure on a set defines a `fintype` structure on its subset. -/ def fintype_subset (s : set α) {t : set α} [fintype s] [decidable_pred t] (h : t ⊆ s) : fintype t := by rw ← inter_eq_self_of_subset_right h; apply_instance theorem finite.subset {s : set α} : finite s → ∀ {t : set α}, t ⊆ s → finite t | ⟨hs⟩ t h := ⟨@set.fintype_subset _ _ _ hs (classical.dec_pred t) h⟩ theorem infinite_mono {s t : set α} (h : s ⊆ t) : infinite s → infinite t := mt (λ ht, ht.subset h) instance fintype_image [decidable_eq β] (s : set α) (f : α → β) [fintype s] : fintype (f '' s) := fintype.of_finset (s.to_finset.image f) $ by simp instance fintype_range [decidable_eq β] (f : α → β) [fintype α] : fintype (range f) := fintype.of_finset (finset.univ.image f) $ by simp [range] theorem finite_range (f : α → β) [fintype α] : finite (range f) := by haveI := classical.dec_eq β; exact ⟨by apply_instance⟩ theorem finite.image {s : set α} (f : α → β) : finite s → finite (f '' s) | ⟨h⟩ := ⟨@set.fintype_image _ _ (classical.dec_eq β) _ _ h⟩ lemma finite.dependent_image {s : set α} (hs : finite s) {F : Π i ∈ s, β} {t : set β} (H : ∀ y ∈ t, ∃ x (hx : x ∈ s), y = F x hx) : set.finite t := begin let G : s → β := λ x, F x.1 x.2, have A : t ⊆ set.range G, { assume y hy, rcases H y hy with ⟨x, hx, xy⟩, refine ⟨⟨x, hx⟩, xy.symm⟩ }, letI : fintype s := finite.fintype hs, exact (finite_range G).subset A end instance fintype_map {α β} [decidable_eq β] : ∀ (s : set α) (f : α → β) [fintype s], fintype (f <$> s) := set.fintype_image theorem finite.map {α β} {s : set α} : ∀ (f : α → β), finite s → finite (f <$> s) := finite.image /-- If a function `f` has a partial inverse and sends a set `s` to a set with `[fintype]` instance, then `s` has a `fintype` structure as well. -/ def fintype_of_fintype_image (s : set α) {f : α → β} {g} (I : is_partial_inv f g) [fintype (f '' s)] : fintype s := fintype.of_finset ⟨_, @multiset.nodup_filter_map β α g _ (@injective_of_partial_inv_right _ _ f g I) (f '' s).to_finset.2⟩ $ λ a, begin suffices : (∃ b x, f x = b ∧ g b = some a ∧ x ∈ s) ↔ a ∈ s, by simpa [exists_and_distrib_left.symm, and.comm, and.left_comm, and.assoc], rw exists_swap, suffices : (∃ x, x ∈ s ∧ g (f x) = some a) ↔ a ∈ s, {simpa [and.comm, and.left_comm, and.assoc]}, simp [I _, (injective_of_partial_inv I).eq_iff] end theorem finite_of_finite_image {s : set α} {f : α → β} (hi : set.inj_on f s) : finite (f '' s) → finite s | ⟨h⟩ := ⟨@fintype.of_injective _ _ h (λa:s, ⟨f a.1, mem_image_of_mem f a.2⟩) $ assume a b eq, subtype.eq $ hi a.2 b.2 $ subtype.ext_iff_val.1 eq⟩ theorem finite_image_iff {s : set α} {f : α → β} (hi : inj_on f s) : finite (f '' s) ↔ finite s := ⟨finite_of_finite_image hi, finite.image _⟩ theorem finite.preimage {s : set β} {f : α → β} (I : set.inj_on f (f⁻¹' s)) (h : finite s) : finite (f ⁻¹' s) := finite_of_finite_image I (h.subset (image_preimage_subset f s)) instance fintype_Union [decidable_eq α] {ι : Type*} [fintype ι] (f : ι → set α) [∀ i, fintype (f i)] : fintype (⋃ i, f i) := fintype.of_finset (finset.univ.bind (λ i, (f i).to_finset)) $ by simp theorem finite_Union {ι : Type*} [fintype ι] {f : ι → set α} (H : ∀i, finite (f i)) : finite (⋃ i, f i) := ⟨@set.fintype_Union _ (classical.dec_eq α) _ _ _ (λ i, finite.fintype (H i))⟩ /-- A union of sets with `fintype` structure over a set with `fintype` structure has a `fintype` structure. -/ def fintype_bUnion [decidable_eq α] {ι : Type*} {s : set ι} [fintype s] (f : ι → set α) (H : ∀ i ∈ s, fintype (f i)) : fintype (⋃ i ∈ s, f i) := by rw bUnion_eq_Union; exact @set.fintype_Union _ _ _ _ _ (by rintro ⟨i, hi⟩; exact H i hi) instance fintype_bUnion' [decidable_eq α] {ι : Type*} {s : set ι} [fintype s] (f : ι → set α) [H : ∀ i, fintype (f i)] : fintype (⋃ i ∈ s, f i) := fintype_bUnion _ (λ i _, H i) theorem finite.sUnion {s : set (set α)} (h : finite s) (H : ∀t∈s, finite t) : finite (⋃₀ s) := by rw sUnion_eq_Union; haveI := finite.fintype h; apply finite_Union; simpa using H theorem finite.bUnion {α} {ι : Type*} {s : set ι} {f : Π i ∈ s, set α} : finite s → (∀ i ∈ s, finite (f i ‹_›)) → finite (⋃ i∈s, f i ‹_›) | ⟨hs⟩ h := by rw [bUnion_eq_Union]; exactI finite_Union (λ i, h _ _) instance fintype_lt_nat (n : ℕ) : fintype {i | i < n} := fintype.of_finset (finset.range n) $ by simp instance fintype_le_nat (n : ℕ) : fintype {i | i ≤ n} := by simpa [nat.lt_succ_iff] using set.fintype_lt_nat (n+1) lemma finite_le_nat (n : ℕ) : finite {i | i ≤ n} := ⟨set.fintype_le_nat _⟩ lemma finite_lt_nat (n : ℕ) : finite {i | i < n} := ⟨set.fintype_lt_nat _⟩ instance fintype_prod (s : set α) (t : set β) [fintype s] [fintype t] : fintype (set.prod s t) := fintype.of_finset (s.to_finset.product t.to_finset) $ by simp lemma finite.prod {s : set α} {t : set β} : finite s → finite t → finite (set.prod s t) | ⟨hs⟩ ⟨ht⟩ := by exactI ⟨set.fintype_prod s t⟩ /-- `image2 f s t` is finitype if `s` and `t` are. -/ instance fintype_image2 [decidable_eq γ] (f : α → β → γ) (s : set α) (t : set β) [hs : fintype s] [ht : fintype t] : fintype (image2 f s t : set γ) := by { rw ← image_prod, apply set.fintype_image } lemma finite.image2 (f : α → β → γ) {s : set α} {t : set β} (hs : finite s) (ht : finite t) : finite (image2 f s t) := by { rw ← image_prod, exact (hs.prod ht).image _ } /-- If `s : set α` is a set with `fintype` instance and `f : α → set β` is a function such that each `f a`, `a ∈ s`, has a `fintype` structure, then `s >>= f` has a `fintype` structure. -/ def fintype_bind {α β} [decidable_eq β] (s : set α) [fintype s] (f : α → set β) (H : ∀ a ∈ s, fintype (f a)) : fintype (s >>= f) := set.fintype_bUnion _ H instance fintype_bind' {α β} [decidable_eq β] (s : set α) [fintype s] (f : α → set β) [H : ∀ a, fintype (f a)] : fintype (s >>= f) := fintype_bind _ _ (λ i _, H i) theorem finite_bind {α β} {s : set α} {f : α → set β} : finite s → (∀ a ∈ s, finite (f a)) → finite (s >>= f) | ⟨hs⟩ H := ⟨@fintype_bind _ _ (classical.dec_eq β) _ hs _ (λ a ha, (H a ha).fintype)⟩ instance fintype_seq {α β : Type u} [decidable_eq β] (f : set (α → β)) (s : set α) [fintype f] [fintype s] : fintype (f <*> s) := by rw seq_eq_bind_map; apply set.fintype_bind' theorem finite.seq {α β : Type u} {f : set (α → β)} {s : set α} : finite f → finite s → finite (f <*> s) | ⟨hf⟩ ⟨hs⟩ := by { haveI := classical.dec_eq β, exactI ⟨set.fintype_seq _ _⟩ } /-- There are finitely many subsets of a given finite set -/ lemma finite.finite_subsets {α : Type u} {a : set α} (h : finite a) : finite {b | b ⊆ a} := begin -- we just need to translate the result, already known for finsets, -- to the language of finite sets let s : set (set α) := coe '' (↑(finset.powerset (finite.to_finset h)) : set (finset α)), have : finite s := (finite_mem_finset _).image _, apply this.subset, refine λ b hb, ⟨(h.subset hb).to_finset, _, finite.coe_to_finset _⟩, simpa [finset.subset_iff] end lemma exists_min_image [linear_order β] (s : set α) (f : α → β) (h1 : finite s) : s.nonempty → ∃ a ∈ s, ∀ b ∈ s, f a ≤ f b | ⟨x, hx⟩ := by simpa only [exists_prop, finite.mem_to_finset] using (finite.to_finset h1).exists_min_image f ⟨x, finite.mem_to_finset.2 hx⟩ lemma exists_max_image [linear_order β] (s : set α) (f : α → β) (h1 : finite s) : s.nonempty → ∃ a ∈ s, ∀ b ∈ s, f b ≤ f a | ⟨x, hx⟩ := by simpa only [exists_prop, finite.mem_to_finset] using (finite.to_finset h1).exists_max_image f ⟨x, finite.mem_to_finset.2 hx⟩ end set namespace finset variables [decidable_eq β] variables {s : finset α} lemma finite_to_set (s : finset α) : set.finite (↑s : set α) := set.finite_mem_finset s @[simp] lemma coe_bind {f : α → finset β} : ↑(s.bind f) = (⋃x ∈ (↑s : set α), ↑(f x) : set β) := by simp [set.ext_iff] @[simp] lemma finite_to_set_to_finset {α : Type*} (s : finset α) : (finite_to_set s).to_finset = s := by { ext, rw [set.finite.mem_to_finset, mem_coe] } end finset namespace set lemma finite_subset_Union {s : set α} (hs : finite s) {ι} {t : ι → set α} (h : s ⊆ ⋃ i, t i) : ∃ I : set ι, finite I ∧ s ⊆ ⋃ i ∈ I, t i := begin casesI hs, choose f hf using show ∀ x : s, ∃ i, x.1 ∈ t i, {simpa [subset_def] using h}, refine ⟨range f, finite_range f, _⟩, rintro x hx, simp, exact ⟨x, ⟨hx, hf _⟩⟩, end lemma eq_finite_Union_of_finite_subset_Union {ι} {s : ι → set α} {t : set α} (tfin : finite t) (h : t ⊆ ⋃ i, s i) : ∃ I : set ι, (finite I) ∧ ∃ σ : {i | i ∈ I} → set α, (∀ i, finite (σ i)) ∧ (∀ i, σ i ⊆ s i) ∧ t = ⋃ i, σ i := let ⟨I, Ifin, hI⟩ := finite_subset_Union tfin h in ⟨I, Ifin, λ x, s x ∩ t, λ i, tfin.subset (inter_subset_right _ _), λ i, inter_subset_left _ _, begin ext x, rw mem_Union, split, { intro x_in, rcases mem_Union.mp (hI x_in) with ⟨i, _, ⟨hi, rfl⟩, H⟩, use [i, hi, H, x_in] }, { rintros ⟨i, hi, H⟩, exact H } end⟩ instance nat.fintype_Iio (n : ℕ) : fintype (Iio n) := fintype.of_finset (finset.range n) $ by simp /-- If `P` is some relation between terms of `γ` and sets in `γ`, such that every finite set `t : set γ` has some `c : γ` related to it, then there is a recursively defined sequence `u` in `γ` so `u n` is related to the image of `{0, 1, ..., n-1}` under `u`. (We use this later to show sequentially compact sets are totally bounded.) -/ lemma seq_of_forall_finite_exists {γ : Type*} {P : γ → set γ → Prop} (h : ∀ t, finite t → ∃ c, P c t) : ∃ u : ℕ → γ, ∀ n, P (u n) (u '' Iio n) := ⟨λ n, @nat.strong_rec_on' (λ _, γ) n $ λ n ih, classical.some $ h (range $ λ m : Iio n, ih m.1 m.2) (finite_range _), λ n, begin classical, refine nat.strong_rec_on' n (λ n ih, _), rw nat.strong_rec_on_beta', convert classical.some_spec (h _ _), ext x, split, { rintros ⟨m, hmn, rfl⟩, exact ⟨⟨m, hmn⟩, rfl⟩ }, { rintros ⟨⟨m, hmn⟩, rfl⟩, exact ⟨m, hmn, rfl⟩ } end⟩ lemma finite_range_ite {p : α → Prop} [decidable_pred p] {f g : α → β} (hf : finite (range f)) (hg : finite (range g)) : finite (range (λ x, if p x then f x else g x)) := (hf.union hg).subset range_ite_subset lemma finite_range_const {c : β} : finite (range (λ x : α, c)) := (finite_singleton c).subset range_const_subset lemma range_find_greatest_subset {P : α → ℕ → Prop} [∀ x, decidable_pred (P x)] {b : ℕ}: range (λ x, nat.find_greatest (P x) b) ⊆ ↑(finset.range (b + 1)) := by { rw range_subset_iff, assume x, simp [nat.lt_succ_iff, nat.find_greatest_le] } lemma finite_range_find_greatest {P : α → ℕ → Prop} [∀ x, decidable_pred (P x)] {b : ℕ} : finite (range (λ x, nat.find_greatest (P x) b)) := (finset.range (b + 1)).finite_to_set.subset range_find_greatest_subset lemma card_lt_card {s t : set α} [fintype s] [fintype t] (h : s ⊂ t) : fintype.card s < fintype.card t := begin rw [← s.coe_to_finset, ← t.coe_to_finset, finset.coe_ssubset] at h, rw [fintype.card_of_finset' _ (λ x, mem_to_finset), fintype.card_of_finset' _ (λ x, mem_to_finset)], exact finset.card_lt_card h, end lemma card_le_of_subset {s t : set α} [fintype s] [fintype t] (hsub : s ⊆ t) : fintype.card s ≤ fintype.card t := calc fintype.card s = s.to_finset.card : fintype.card_of_finset' _ (by simp) ... ≤ t.to_finset.card : finset.card_le_of_subset (λ x hx, by simp [set.subset_def, *] at *) ... = fintype.card t : eq.symm (fintype.card_of_finset' _ (by simp)) lemma eq_of_subset_of_card_le {s t : set α} [fintype s] [fintype t] (hsub : s ⊆ t) (hcard : fintype.card t ≤ fintype.card s) : s = t := (eq_or_ssubset_of_subset hsub).elim id (λ h, absurd hcard $ not_le_of_lt $ card_lt_card h) lemma card_range_of_injective [fintype α] {f : α → β} (hf : injective f) [fintype (range f)] : fintype.card (range f) = fintype.card α := eq.symm $ fintype.card_congr $ equiv.set.range f hf lemma finite.exists_maximal_wrt [partial_order β] (f : α → β) (s : set α) (h : set.finite s) : s.nonempty → ∃a∈s, ∀a'∈s, f a ≤ f a' → f a = f a' := begin classical, refine h.induction_on _ _, { assume h, exact absurd h empty_not_nonempty }, assume a s his _ ih _, cases s.eq_empty_or_nonempty with h h, { use a, simp [h] }, rcases ih h with ⟨b, hb, ih⟩, by_cases f b ≤ f a, { refine ⟨a, set.mem_insert _ _, assume c hc hac, le_antisymm hac _⟩, rcases set.mem_insert_iff.1 hc with rfl | hcs, { refl }, { rwa [← ih c hcs (le_trans h hac)] } }, { refine ⟨b, set.mem_insert_of_mem _ hb, assume c hc hbc, _⟩, rcases set.mem_insert_iff.1 hc with rfl | hcs, { exact (h hbc).elim }, { exact ih c hcs hbc } } end lemma finite.card_to_finset {s : set α} [fintype s] (h : s.finite) : h.to_finset.card = fintype.card s := by { rw [← finset.card_attach, finset.attach_eq_univ, ← fintype.card], congr' 2, funext, rw set.finite.mem_to_finset } section local attribute [instance, priority 1] classical.prop_decidable lemma to_finset_inter {α : Type*} [fintype α] (s t : set α) : (s ∩ t).to_finset = s.to_finset ∩ t.to_finset := by ext; simp end section variables [semilattice_sup α] [nonempty α] {s : set α} /--A finite set is bounded above.-/ protected lemma finite.bdd_above (hs : finite s) : bdd_above s := finite.induction_on hs bdd_above_empty $ λ a s _ _ h, h.insert a /--A finite union of sets which are all bounded above is still bounded above.-/ lemma finite.bdd_above_bUnion {I : set β} {S : β → set α} (H : finite I) : (bdd_above (⋃i∈I, S i)) ↔ (∀i ∈ I, bdd_above (S i)) := finite.induction_on H (by simp only [bUnion_empty, bdd_above_empty, ball_empty_iff]) (λ a s ha _ hs, by simp only [bUnion_insert, ball_insert_iff, bdd_above_union, hs]) end section variables [semilattice_inf α] [nonempty α] {s : set α} /--A finite set is bounded below.-/ protected lemma finite.bdd_below (hs : finite s) : bdd_below s := @finite.bdd_above (order_dual α) _ _ _ hs /--A finite union of sets which are all bounded below is still bounded below.-/ lemma finite.bdd_below_bUnion {I : set β} {S : β → set α} (H : finite I) : (bdd_below (⋃i∈I, S i)) ↔ (∀i ∈ I, bdd_below (S i)) := @finite.bdd_above_bUnion (order_dual α) _ _ _ _ _ H end end set namespace finset /-- A finset is bounded above. -/ protected lemma bdd_above [semilattice_sup α] [nonempty α] (s : finset α) : bdd_above (↑s : set α) := s.finite_to_set.bdd_above /-- A finset is bounded below. -/ protected lemma bdd_below [semilattice_inf α] [nonempty α] (s : finset α) : bdd_below (↑s : set α) := s.finite_to_set.bdd_below end finset lemma fintype.exists_max [fintype α] [nonempty α] {β : Type*} [linear_order β] (f : α → β) : ∃ x₀ : α, ∀ x, f x ≤ f x₀ := begin rcases set.finite_univ.exists_maximal_wrt f _ univ_nonempty with ⟨x, _, hx⟩, exact ⟨x, λ y, (le_total (f x) (f y)).elim (λ h, ge_of_eq $ hx _ trivial h) id⟩ end